opcodes/
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
a253d456 2 Copyright 1993-2013 Free Software Foundation, Inc.
b49e97c9
TS
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark@codesourcery.com>
8 Traditional MIPS targets support added by Koundinya.K, Dansk Data
9 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
10
ae9a127f 11 This file is part of BFD, the Binary File Descriptor library.
b49e97c9 12
ae9a127f
NC
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
cd123cb7 15 the Free Software Foundation; either version 3 of the License, or
ae9a127f 16 (at your option) any later version.
b49e97c9 17
ae9a127f
NC
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
b49e97c9 22
ae9a127f
NC
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
cd123cb7
NC
25 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26 MA 02110-1301, USA. */
27
b49e97c9
TS
28
29/* This file handles functionality common to the different MIPS ABI's. */
30
b49e97c9 31#include "sysdep.h"
3db64b00 32#include "bfd.h"
b49e97c9 33#include "libbfd.h"
64543e1a 34#include "libiberty.h"
b49e97c9
TS
35#include "elf-bfd.h"
36#include "elfxx-mips.h"
37#include "elf/mips.h"
0a44bf69 38#include "elf-vxworks.h"
b49e97c9
TS
39
40/* Get the ECOFF swapping routines. */
41#include "coff/sym.h"
42#include "coff/symconst.h"
43#include "coff/ecoff.h"
44#include "coff/mips.h"
45
b15e6682
AO
46#include "hashtab.h"
47
9ab066b4
RS
48/* Types of TLS GOT entry. */
49enum mips_got_tls_type {
50 GOT_TLS_NONE,
51 GOT_TLS_GD,
52 GOT_TLS_LDM,
53 GOT_TLS_IE
54};
55
ead49a57 56/* This structure is used to hold information about one GOT entry.
3dff0dd1
RS
57 There are four types of entry:
58
59 (1) an absolute address
60 requires: abfd == NULL
61 fields: d.address
62
63 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
64 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
65 fields: abfd, symndx, d.addend, tls_type
66
67 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
68 requires: abfd != NULL, symndx == -1
69 fields: d.h, tls_type
70
71 (4) a TLS LDM slot
72 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
73 fields: none; there's only one of these per GOT. */
b15e6682
AO
74struct mips_got_entry
75{
3dff0dd1 76 /* One input bfd that needs the GOT entry. */
b15e6682 77 bfd *abfd;
f4416af6
AO
78 /* The index of the symbol, as stored in the relocation r_info, if
79 we have a local symbol; -1 otherwise. */
80 long symndx;
81 union
82 {
83 /* If abfd == NULL, an address that must be stored in the got. */
84 bfd_vma address;
85 /* If abfd != NULL && symndx != -1, the addend of the relocation
86 that should be added to the symbol value. */
87 bfd_vma addend;
88 /* If abfd != NULL && symndx == -1, the hash table entry
3dff0dd1 89 corresponding to a symbol in the GOT. The symbol's entry
020d7251
RS
90 is in the local area if h->global_got_area is GGA_NONE,
91 otherwise it is in the global area. */
f4416af6
AO
92 struct mips_elf_link_hash_entry *h;
93 } d;
0f20cc35 94
9ab066b4
RS
95 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
96 symbol entry with r_symndx == 0. */
0f20cc35
DJ
97 unsigned char tls_type;
98
9ab066b4
RS
99 /* True if we have filled in the GOT contents for a TLS entry,
100 and created the associated relocations. */
101 unsigned char tls_initialized;
102
b15e6682 103 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
104 corresponding to this symbol+addend. If it's a global symbol
105 whose offset is yet to be decided, it's going to be -1. */
106 long gotidx;
b15e6682
AO
107};
108
13db6b44
RS
109/* This structure represents a GOT page reference from an input bfd.
110 Each instance represents a symbol + ADDEND, where the representation
111 of the symbol depends on whether it is local to the input bfd.
112 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
113 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
114
115 Page references with SYMNDX >= 0 always become page references
116 in the output. Page references with SYMNDX < 0 only become page
117 references if the symbol binds locally; in other cases, the page
118 reference decays to a global GOT reference. */
119struct mips_got_page_ref
120{
121 long symndx;
122 union
123 {
124 struct mips_elf_link_hash_entry *h;
125 bfd *abfd;
126 } u;
127 bfd_vma addend;
128};
129
c224138d
RS
130/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
131 The structures form a non-overlapping list that is sorted by increasing
132 MIN_ADDEND. */
133struct mips_got_page_range
134{
135 struct mips_got_page_range *next;
136 bfd_signed_vma min_addend;
137 bfd_signed_vma max_addend;
138};
139
140/* This structure describes the range of addends that are applied to page
13db6b44 141 relocations against a given section. */
c224138d
RS
142struct mips_got_page_entry
143{
13db6b44
RS
144 /* The section that these entries are based on. */
145 asection *sec;
c224138d
RS
146 /* The ranges for this page entry. */
147 struct mips_got_page_range *ranges;
148 /* The maximum number of page entries needed for RANGES. */
149 bfd_vma num_pages;
150};
151
f0abc2a1 152/* This structure is used to hold .got information when linking. */
b49e97c9
TS
153
154struct mips_got_info
155{
b49e97c9
TS
156 /* The number of global .got entries. */
157 unsigned int global_gotno;
23cc69b6
RS
158 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
159 unsigned int reloc_only_gotno;
0f20cc35
DJ
160 /* The number of .got slots used for TLS. */
161 unsigned int tls_gotno;
162 /* The first unused TLS .got entry. Used only during
163 mips_elf_initialize_tls_index. */
164 unsigned int tls_assigned_gotno;
c224138d 165 /* The number of local .got entries, eventually including page entries. */
b49e97c9 166 unsigned int local_gotno;
c224138d
RS
167 /* The maximum number of page entries needed. */
168 unsigned int page_gotno;
ab361d49
RS
169 /* The number of relocations needed for the GOT entries. */
170 unsigned int relocs;
b49e97c9
TS
171 /* The number of local .got entries we have used. */
172 unsigned int assigned_gotno;
b15e6682
AO
173 /* A hash table holding members of the got. */
174 struct htab *got_entries;
13db6b44
RS
175 /* A hash table holding mips_got_page_ref structures. */
176 struct htab *got_page_refs;
c224138d
RS
177 /* A hash table of mips_got_page_entry structures. */
178 struct htab *got_page_entries;
f4416af6
AO
179 /* In multi-got links, a pointer to the next got (err, rather, most
180 of the time, it points to the previous got). */
181 struct mips_got_info *next;
182};
183
d7206569 184/* Structure passed when merging bfds' gots. */
f4416af6
AO
185
186struct mips_elf_got_per_bfd_arg
187{
f4416af6
AO
188 /* The output bfd. */
189 bfd *obfd;
190 /* The link information. */
191 struct bfd_link_info *info;
192 /* A pointer to the primary got, i.e., the one that's going to get
193 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
194 DT_MIPS_GOTSYM. */
195 struct mips_got_info *primary;
196 /* A non-primary got we're trying to merge with other input bfd's
197 gots. */
198 struct mips_got_info *current;
199 /* The maximum number of got entries that can be addressed with a
200 16-bit offset. */
201 unsigned int max_count;
c224138d
RS
202 /* The maximum number of page entries needed by each got. */
203 unsigned int max_pages;
0f20cc35
DJ
204 /* The total number of global entries which will live in the
205 primary got and be automatically relocated. This includes
206 those not referenced by the primary GOT but included in
207 the "master" GOT. */
208 unsigned int global_count;
f4416af6
AO
209};
210
ab361d49
RS
211/* A structure used to pass information to htab_traverse callbacks
212 when laying out the GOT. */
f4416af6 213
ab361d49 214struct mips_elf_traverse_got_arg
f4416af6 215{
ab361d49 216 struct bfd_link_info *info;
f4416af6
AO
217 struct mips_got_info *g;
218 int value;
0f20cc35
DJ
219};
220
f0abc2a1
AM
221struct _mips_elf_section_data
222{
223 struct bfd_elf_section_data elf;
224 union
225 {
f0abc2a1
AM
226 bfd_byte *tdata;
227 } u;
228};
229
230#define mips_elf_section_data(sec) \
68bfbfcc 231 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 232
d5eaccd7
RS
233#define is_mips_elf(bfd) \
234 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
235 && elf_tdata (bfd) != NULL \
4dfe6ac6 236 && elf_object_id (bfd) == MIPS_ELF_DATA)
d5eaccd7 237
634835ae
RS
238/* The ABI says that every symbol used by dynamic relocations must have
239 a global GOT entry. Among other things, this provides the dynamic
240 linker with a free, directly-indexed cache. The GOT can therefore
241 contain symbols that are not referenced by GOT relocations themselves
242 (in other words, it may have symbols that are not referenced by things
243 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
244
245 GOT relocations are less likely to overflow if we put the associated
246 GOT entries towards the beginning. We therefore divide the global
247 GOT entries into two areas: "normal" and "reloc-only". Entries in
248 the first area can be used for both dynamic relocations and GP-relative
249 accesses, while those in the "reloc-only" area are for dynamic
250 relocations only.
251
252 These GGA_* ("Global GOT Area") values are organised so that lower
253 values are more general than higher values. Also, non-GGA_NONE
254 values are ordered by the position of the area in the GOT. */
255#define GGA_NORMAL 0
256#define GGA_RELOC_ONLY 1
257#define GGA_NONE 2
258
861fb55a
DJ
259/* Information about a non-PIC interface to a PIC function. There are
260 two ways of creating these interfaces. The first is to add:
261
262 lui $25,%hi(func)
263 addiu $25,$25,%lo(func)
264
265 immediately before a PIC function "func". The second is to add:
266
267 lui $25,%hi(func)
268 j func
269 addiu $25,$25,%lo(func)
270
271 to a separate trampoline section.
272
273 Stubs of the first kind go in a new section immediately before the
274 target function. Stubs of the second kind go in a single section
275 pointed to by the hash table's "strampoline" field. */
276struct mips_elf_la25_stub {
277 /* The generated section that contains this stub. */
278 asection *stub_section;
279
280 /* The offset of the stub from the start of STUB_SECTION. */
281 bfd_vma offset;
282
283 /* One symbol for the original function. Its location is available
284 in H->root.root.u.def. */
285 struct mips_elf_link_hash_entry *h;
286};
287
288/* Macros for populating a mips_elf_la25_stub. */
289
290#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
291#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
292#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
d21911ea
MR
293#define LA25_LUI_MICROMIPS(VAL) \
294 (0x41b90000 | (VAL)) /* lui t9,VAL */
295#define LA25_J_MICROMIPS(VAL) \
296 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
297#define LA25_ADDIU_MICROMIPS(VAL) \
298 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
861fb55a 299
b49e97c9
TS
300/* This structure is passed to mips_elf_sort_hash_table_f when sorting
301 the dynamic symbols. */
302
303struct mips_elf_hash_sort_data
304{
305 /* The symbol in the global GOT with the lowest dynamic symbol table
306 index. */
307 struct elf_link_hash_entry *low;
0f20cc35
DJ
308 /* The least dynamic symbol table index corresponding to a non-TLS
309 symbol with a GOT entry. */
b49e97c9 310 long min_got_dynindx;
f4416af6
AO
311 /* The greatest dynamic symbol table index corresponding to a symbol
312 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 313 with dynamic relocations pointing to it from non-primary GOTs). */
f4416af6 314 long max_unref_got_dynindx;
b49e97c9
TS
315 /* The greatest dynamic symbol table index not corresponding to a
316 symbol without a GOT entry. */
317 long max_non_got_dynindx;
318};
319
1bbce132
MR
320/* We make up to two PLT entries if needed, one for standard MIPS code
321 and one for compressed code, either a MIPS16 or microMIPS one. We
322 keep a separate record of traditional lazy-binding stubs, for easier
323 processing. */
324
325struct plt_entry
326{
327 /* Traditional SVR4 stub offset, or -1 if none. */
328 bfd_vma stub_offset;
329
330 /* Standard PLT entry offset, or -1 if none. */
331 bfd_vma mips_offset;
332
333 /* Compressed PLT entry offset, or -1 if none. */
334 bfd_vma comp_offset;
335
336 /* The corresponding .got.plt index, or -1 if none. */
337 bfd_vma gotplt_index;
338
339 /* Whether we need a standard PLT entry. */
340 unsigned int need_mips : 1;
341
342 /* Whether we need a compressed PLT entry. */
343 unsigned int need_comp : 1;
344};
345
b49e97c9
TS
346/* The MIPS ELF linker needs additional information for each symbol in
347 the global hash table. */
348
349struct mips_elf_link_hash_entry
350{
351 struct elf_link_hash_entry root;
352
353 /* External symbol information. */
354 EXTR esym;
355
861fb55a
DJ
356 /* The la25 stub we have created for ths symbol, if any. */
357 struct mips_elf_la25_stub *la25_stub;
358
b49e97c9
TS
359 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
360 this symbol. */
361 unsigned int possibly_dynamic_relocs;
362
b49e97c9
TS
363 /* If there is a stub that 32 bit functions should use to call this
364 16 bit function, this points to the section containing the stub. */
365 asection *fn_stub;
366
b49e97c9
TS
367 /* If there is a stub that 16 bit functions should use to call this
368 32 bit function, this points to the section containing the stub. */
369 asection *call_stub;
370
371 /* This is like the call_stub field, but it is used if the function
372 being called returns a floating point value. */
373 asection *call_fp_stub;
7c5fcef7 374
634835ae
RS
375 /* The highest GGA_* value that satisfies all references to this symbol. */
376 unsigned int global_got_area : 2;
377
6ccf4795
RS
378 /* True if all GOT relocations against this symbol are for calls. This is
379 a looser condition than no_fn_stub below, because there may be other
380 non-call non-GOT relocations against the symbol. */
381 unsigned int got_only_for_calls : 1;
382
71782a75
RS
383 /* True if one of the relocations described by possibly_dynamic_relocs
384 is against a readonly section. */
385 unsigned int readonly_reloc : 1;
386
861fb55a
DJ
387 /* True if there is a relocation against this symbol that must be
388 resolved by the static linker (in other words, if the relocation
389 cannot possibly be made dynamic). */
390 unsigned int has_static_relocs : 1;
391
71782a75
RS
392 /* True if we must not create a .MIPS.stubs entry for this symbol.
393 This is set, for example, if there are relocations related to
394 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
395 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
396 unsigned int no_fn_stub : 1;
397
398 /* Whether we need the fn_stub; this is true if this symbol appears
399 in any relocs other than a 16 bit call. */
400 unsigned int need_fn_stub : 1;
401
861fb55a
DJ
402 /* True if this symbol is referenced by branch relocations from
403 any non-PIC input file. This is used to determine whether an
404 la25 stub is required. */
405 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
406
407 /* Does this symbol need a traditional MIPS lazy-binding stub
408 (as opposed to a PLT entry)? */
409 unsigned int needs_lazy_stub : 1;
1bbce132
MR
410
411 /* Does this symbol resolve to a PLT entry? */
412 unsigned int use_plt_entry : 1;
b49e97c9
TS
413};
414
415/* MIPS ELF linker hash table. */
416
417struct mips_elf_link_hash_table
418{
419 struct elf_link_hash_table root;
861fb55a 420
b49e97c9
TS
421 /* The number of .rtproc entries. */
422 bfd_size_type procedure_count;
861fb55a 423
b49e97c9
TS
424 /* The size of the .compact_rel section (if SGI_COMPAT). */
425 bfd_size_type compact_rel_size;
861fb55a 426
e6aea42d
MR
427 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
428 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
b34976b6 429 bfd_boolean use_rld_obj_head;
861fb55a 430
b4082c70
DD
431 /* The __rld_map or __rld_obj_head symbol. */
432 struct elf_link_hash_entry *rld_symbol;
861fb55a 433
b49e97c9 434 /* This is set if we see any mips16 stub sections. */
b34976b6 435 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
436
437 /* True if we can generate copy relocs and PLTs. */
438 bfd_boolean use_plts_and_copy_relocs;
439
833794fc
MR
440 /* True if we can only use 32-bit microMIPS instructions. */
441 bfd_boolean insn32;
442
0a44bf69
RS
443 /* True if we're generating code for VxWorks. */
444 bfd_boolean is_vxworks;
861fb55a 445
0e53d9da
AN
446 /* True if we already reported the small-data section overflow. */
447 bfd_boolean small_data_overflow_reported;
861fb55a 448
0a44bf69
RS
449 /* Shortcuts to some dynamic sections, or NULL if they are not
450 being used. */
451 asection *srelbss;
452 asection *sdynbss;
453 asection *srelplt;
454 asection *srelplt2;
455 asection *sgotplt;
456 asection *splt;
4e41d0d7 457 asection *sstubs;
a8028dd0 458 asection *sgot;
861fb55a 459
a8028dd0
RS
460 /* The master GOT information. */
461 struct mips_got_info *got_info;
861fb55a 462
d222d210
RS
463 /* The global symbol in the GOT with the lowest index in the dynamic
464 symbol table. */
465 struct elf_link_hash_entry *global_gotsym;
466
861fb55a 467 /* The size of the PLT header in bytes. */
0a44bf69 468 bfd_vma plt_header_size;
861fb55a 469
1bbce132
MR
470 /* The size of a standard PLT entry in bytes. */
471 bfd_vma plt_mips_entry_size;
472
473 /* The size of a compressed PLT entry in bytes. */
474 bfd_vma plt_comp_entry_size;
475
476 /* The offset of the next standard PLT entry to create. */
477 bfd_vma plt_mips_offset;
478
479 /* The offset of the next compressed PLT entry to create. */
480 bfd_vma plt_comp_offset;
481
482 /* The index of the next .got.plt entry to create. */
483 bfd_vma plt_got_index;
861fb55a 484
33bb52fb
RS
485 /* The number of functions that need a lazy-binding stub. */
486 bfd_vma lazy_stub_count;
861fb55a 487
5108fc1b
RS
488 /* The size of a function stub entry in bytes. */
489 bfd_vma function_stub_size;
861fb55a
DJ
490
491 /* The number of reserved entries at the beginning of the GOT. */
492 unsigned int reserved_gotno;
493
494 /* The section used for mips_elf_la25_stub trampolines.
495 See the comment above that structure for details. */
496 asection *strampoline;
497
498 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
499 pairs. */
500 htab_t la25_stubs;
501
502 /* A function FN (NAME, IS, OS) that creates a new input section
503 called NAME and links it to output section OS. If IS is nonnull,
504 the new section should go immediately before it, otherwise it
505 should go at the (current) beginning of OS.
506
507 The function returns the new section on success, otherwise it
508 returns null. */
509 asection *(*add_stub_section) (const char *, asection *, asection *);
13db6b44
RS
510
511 /* Small local sym cache. */
512 struct sym_cache sym_cache;
1bbce132
MR
513
514 /* Is the PLT header compressed? */
515 unsigned int plt_header_is_comp : 1;
861fb55a
DJ
516};
517
4dfe6ac6
NC
518/* Get the MIPS ELF linker hash table from a link_info structure. */
519
520#define mips_elf_hash_table(p) \
521 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
522 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
523
861fb55a 524/* A structure used to communicate with htab_traverse callbacks. */
4dfe6ac6
NC
525struct mips_htab_traverse_info
526{
861fb55a
DJ
527 /* The usual link-wide information. */
528 struct bfd_link_info *info;
529 bfd *output_bfd;
530
531 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
532 bfd_boolean error;
b49e97c9
TS
533};
534
6ae68ba3
MR
535/* MIPS ELF private object data. */
536
537struct mips_elf_obj_tdata
538{
539 /* Generic ELF private object data. */
540 struct elf_obj_tdata root;
541
542 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
543 bfd *abi_fp_bfd;
ee227692 544
b60bf9be
CF
545 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
546 bfd *abi_msa_bfd;
547
ee227692
RS
548 /* The GOT requirements of input bfds. */
549 struct mips_got_info *got;
698600e4
AM
550
551 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
552 included directly in this one, but there's no point to wasting
553 the memory just for the infrequently called find_nearest_line. */
554 struct mips_elf_find_line *find_line_info;
555
556 /* An array of stub sections indexed by symbol number. */
557 asection **local_stubs;
558 asection **local_call_stubs;
559
560 /* The Irix 5 support uses two virtual sections, which represent
561 text/data symbols defined in dynamic objects. */
562 asymbol *elf_data_symbol;
563 asymbol *elf_text_symbol;
564 asection *elf_data_section;
565 asection *elf_text_section;
6ae68ba3
MR
566};
567
568/* Get MIPS ELF private object data from BFD's tdata. */
569
570#define mips_elf_tdata(bfd) \
571 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
572
0f20cc35
DJ
573#define TLS_RELOC_P(r_type) \
574 (r_type == R_MIPS_TLS_DTPMOD32 \
575 || r_type == R_MIPS_TLS_DTPMOD64 \
576 || r_type == R_MIPS_TLS_DTPREL32 \
577 || r_type == R_MIPS_TLS_DTPREL64 \
578 || r_type == R_MIPS_TLS_GD \
579 || r_type == R_MIPS_TLS_LDM \
580 || r_type == R_MIPS_TLS_DTPREL_HI16 \
581 || r_type == R_MIPS_TLS_DTPREL_LO16 \
582 || r_type == R_MIPS_TLS_GOTTPREL \
583 || r_type == R_MIPS_TLS_TPREL32 \
584 || r_type == R_MIPS_TLS_TPREL64 \
585 || r_type == R_MIPS_TLS_TPREL_HI16 \
df58fc94 586 || r_type == R_MIPS_TLS_TPREL_LO16 \
d0f13682
CLT
587 || r_type == R_MIPS16_TLS_GD \
588 || r_type == R_MIPS16_TLS_LDM \
589 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
590 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
591 || r_type == R_MIPS16_TLS_GOTTPREL \
592 || r_type == R_MIPS16_TLS_TPREL_HI16 \
593 || r_type == R_MIPS16_TLS_TPREL_LO16 \
df58fc94
RS
594 || r_type == R_MICROMIPS_TLS_GD \
595 || r_type == R_MICROMIPS_TLS_LDM \
596 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
597 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
598 || r_type == R_MICROMIPS_TLS_GOTTPREL \
599 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
600 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
0f20cc35 601
b49e97c9
TS
602/* Structure used to pass information to mips_elf_output_extsym. */
603
604struct extsym_info
605{
9e4aeb93
RS
606 bfd *abfd;
607 struct bfd_link_info *info;
b49e97c9
TS
608 struct ecoff_debug_info *debug;
609 const struct ecoff_debug_swap *swap;
b34976b6 610 bfd_boolean failed;
b49e97c9
TS
611};
612
8dc1a139 613/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
614
615static const char * const mips_elf_dynsym_rtproc_names[] =
616{
617 "_procedure_table",
618 "_procedure_string_table",
619 "_procedure_table_size",
620 NULL
621};
622
623/* These structures are used to generate the .compact_rel section on
8dc1a139 624 IRIX5. */
b49e97c9
TS
625
626typedef struct
627{
628 unsigned long id1; /* Always one? */
629 unsigned long num; /* Number of compact relocation entries. */
630 unsigned long id2; /* Always two? */
631 unsigned long offset; /* The file offset of the first relocation. */
632 unsigned long reserved0; /* Zero? */
633 unsigned long reserved1; /* Zero? */
634} Elf32_compact_rel;
635
636typedef struct
637{
638 bfd_byte id1[4];
639 bfd_byte num[4];
640 bfd_byte id2[4];
641 bfd_byte offset[4];
642 bfd_byte reserved0[4];
643 bfd_byte reserved1[4];
644} Elf32_External_compact_rel;
645
646typedef struct
647{
648 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
649 unsigned int rtype : 4; /* Relocation types. See below. */
650 unsigned int dist2to : 8;
651 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
652 unsigned long konst; /* KONST field. See below. */
653 unsigned long vaddr; /* VADDR to be relocated. */
654} Elf32_crinfo;
655
656typedef struct
657{
658 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
659 unsigned int rtype : 4; /* Relocation types. See below. */
660 unsigned int dist2to : 8;
661 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
662 unsigned long konst; /* KONST field. See below. */
663} Elf32_crinfo2;
664
665typedef struct
666{
667 bfd_byte info[4];
668 bfd_byte konst[4];
669 bfd_byte vaddr[4];
670} Elf32_External_crinfo;
671
672typedef struct
673{
674 bfd_byte info[4];
675 bfd_byte konst[4];
676} Elf32_External_crinfo2;
677
678/* These are the constants used to swap the bitfields in a crinfo. */
679
680#define CRINFO_CTYPE (0x1)
681#define CRINFO_CTYPE_SH (31)
682#define CRINFO_RTYPE (0xf)
683#define CRINFO_RTYPE_SH (27)
684#define CRINFO_DIST2TO (0xff)
685#define CRINFO_DIST2TO_SH (19)
686#define CRINFO_RELVADDR (0x7ffff)
687#define CRINFO_RELVADDR_SH (0)
688
689/* A compact relocation info has long (3 words) or short (2 words)
690 formats. A short format doesn't have VADDR field and relvaddr
691 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
692#define CRF_MIPS_LONG 1
693#define CRF_MIPS_SHORT 0
694
695/* There are 4 types of compact relocation at least. The value KONST
696 has different meaning for each type:
697
698 (type) (konst)
699 CT_MIPS_REL32 Address in data
700 CT_MIPS_WORD Address in word (XXX)
701 CT_MIPS_GPHI_LO GP - vaddr
702 CT_MIPS_JMPAD Address to jump
703 */
704
705#define CRT_MIPS_REL32 0xa
706#define CRT_MIPS_WORD 0xb
707#define CRT_MIPS_GPHI_LO 0xc
708#define CRT_MIPS_JMPAD 0xd
709
710#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
711#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
712#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
713#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
714\f
715/* The structure of the runtime procedure descriptor created by the
716 loader for use by the static exception system. */
717
718typedef struct runtime_pdr {
ae9a127f
NC
719 bfd_vma adr; /* Memory address of start of procedure. */
720 long regmask; /* Save register mask. */
721 long regoffset; /* Save register offset. */
722 long fregmask; /* Save floating point register mask. */
723 long fregoffset; /* Save floating point register offset. */
724 long frameoffset; /* Frame size. */
725 short framereg; /* Frame pointer register. */
726 short pcreg; /* Offset or reg of return pc. */
727 long irpss; /* Index into the runtime string table. */
b49e97c9 728 long reserved;
ae9a127f 729 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
730} RPDR, *pRPDR;
731#define cbRPDR sizeof (RPDR)
732#define rpdNil ((pRPDR) 0)
733\f
b15e6682 734static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
735 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
736 struct mips_elf_link_hash_entry *, int);
b34976b6 737static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 738 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
739static bfd_vma mips_elf_high
740 (bfd_vma);
b34976b6 741static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
742 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
743 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
744 bfd_vma *, asection *);
f4416af6 745static bfd_vma mips_elf_adjust_gp
9719ad41 746 (bfd *, struct mips_got_info *, bfd *);
f4416af6 747
b49e97c9
TS
748/* This will be used when we sort the dynamic relocation records. */
749static bfd *reldyn_sorting_bfd;
750
6d30f5b2
NC
751/* True if ABFD is for CPUs with load interlocking that include
752 non-MIPS1 CPUs and R3900. */
753#define LOAD_INTERLOCKS_P(abfd) \
754 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
755 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
756
cd8d5a82
CF
757/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
758 This should be safe for all architectures. We enable this predicate
759 for RM9000 for now. */
760#define JAL_TO_BAL_P(abfd) \
761 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
762
763/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
764 This should be safe for all architectures. We enable this predicate for
765 all CPUs. */
766#define JALR_TO_BAL_P(abfd) 1
767
38a7df63
CF
768/* True if ABFD is for CPUs that are faster if JR is converted to B.
769 This should be safe for all architectures. We enable this predicate for
770 all CPUs. */
771#define JR_TO_B_P(abfd) 1
772
861fb55a
DJ
773/* True if ABFD is a PIC object. */
774#define PIC_OBJECT_P(abfd) \
775 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
776
b49e97c9 777/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
778#define ABI_N32_P(abfd) \
779 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
780
4a14403c 781/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 782#define ABI_64_P(abfd) \
141ff970 783 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 784
4a14403c
TS
785/* Nonzero if ABFD is using NewABI conventions. */
786#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
787
e8faf7d1
MR
788/* Nonzero if ABFD has microMIPS code. */
789#define MICROMIPS_P(abfd) \
790 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
791
4a14403c 792/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
793#define IRIX_COMPAT(abfd) \
794 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
795
b49e97c9
TS
796/* Whether we are trying to be compatible with IRIX at all. */
797#define SGI_COMPAT(abfd) \
798 (IRIX_COMPAT (abfd) != ict_none)
799
800/* The name of the options section. */
801#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 802 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 803
cc2e31b9
RS
804/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
805 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
806#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
807 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
808
943284cc
DJ
809/* Whether the section is readonly. */
810#define MIPS_ELF_READONLY_SECTION(sec) \
811 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
812 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
813
b49e97c9 814/* The name of the stub section. */
ca07892d 815#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
816
817/* The size of an external REL relocation. */
818#define MIPS_ELF_REL_SIZE(abfd) \
819 (get_elf_backend_data (abfd)->s->sizeof_rel)
820
0a44bf69
RS
821/* The size of an external RELA relocation. */
822#define MIPS_ELF_RELA_SIZE(abfd) \
823 (get_elf_backend_data (abfd)->s->sizeof_rela)
824
b49e97c9
TS
825/* The size of an external dynamic table entry. */
826#define MIPS_ELF_DYN_SIZE(abfd) \
827 (get_elf_backend_data (abfd)->s->sizeof_dyn)
828
829/* The size of a GOT entry. */
830#define MIPS_ELF_GOT_SIZE(abfd) \
831 (get_elf_backend_data (abfd)->s->arch_size / 8)
832
b4082c70
DD
833/* The size of the .rld_map section. */
834#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
835 (get_elf_backend_data (abfd)->s->arch_size / 8)
836
b49e97c9
TS
837/* The size of a symbol-table entry. */
838#define MIPS_ELF_SYM_SIZE(abfd) \
839 (get_elf_backend_data (abfd)->s->sizeof_sym)
840
841/* The default alignment for sections, as a power of two. */
842#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 843 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
844
845/* Get word-sized data. */
846#define MIPS_ELF_GET_WORD(abfd, ptr) \
847 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
848
849/* Put out word-sized data. */
850#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
851 (ABI_64_P (abfd) \
852 ? bfd_put_64 (abfd, val, ptr) \
853 : bfd_put_32 (abfd, val, ptr))
854
861fb55a
DJ
855/* The opcode for word-sized loads (LW or LD). */
856#define MIPS_ELF_LOAD_WORD(abfd) \
857 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
858
b49e97c9 859/* Add a dynamic symbol table-entry. */
9719ad41 860#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 861 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
862
863#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
864 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
865
0a44bf69
RS
866/* The name of the dynamic relocation section. */
867#define MIPS_ELF_REL_DYN_NAME(INFO) \
868 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
869
b49e97c9
TS
870/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
871 from smaller values. Start with zero, widen, *then* decrement. */
872#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 873#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 874
51e38d68
RS
875/* The value to write into got[1] for SVR4 targets, to identify it is
876 a GNU object. The dynamic linker can then use got[1] to store the
877 module pointer. */
878#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
879 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
880
f4416af6 881/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
882#define ELF_MIPS_GP_OFFSET(INFO) \
883 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
884
885/* The maximum size of the GOT for it to be addressable using 16-bit
886 offsets from $gp. */
0a44bf69 887#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 888
6a691779 889/* Instructions which appear in a stub. */
3d6746ca
DD
890#define STUB_LW(abfd) \
891 ((ABI_64_P (abfd) \
892 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
893 : 0x8f998010)) /* lw t9,0x8010(gp) */
894#define STUB_MOVE(abfd) \
895 ((ABI_64_P (abfd) \
896 ? 0x03e0782d /* daddu t7,ra */ \
897 : 0x03e07821)) /* addu t7,ra */
898#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
899#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
900#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
901#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
902#define STUB_LI16S(abfd, VAL) \
903 ((ABI_64_P (abfd) \
904 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
905 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
906
1bbce132
MR
907/* Likewise for the microMIPS ASE. */
908#define STUB_LW_MICROMIPS(abfd) \
909 (ABI_64_P (abfd) \
910 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
911 : 0xff3c8010) /* lw t9,0x8010(gp) */
912#define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
833794fc
MR
913#define STUB_MOVE32_MICROMIPS(abfd) \
914 (ABI_64_P (abfd) \
915 ? 0x581f7950 /* daddu t7,ra,zero */ \
916 : 0x001f7950) /* addu t7,ra,zero */
1bbce132
MR
917#define STUB_LUI_MICROMIPS(VAL) \
918 (0x41b80000 + (VAL)) /* lui t8,VAL */
919#define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
833794fc 920#define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
1bbce132
MR
921#define STUB_ORI_MICROMIPS(VAL) \
922 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
923#define STUB_LI16U_MICROMIPS(VAL) \
924 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
925#define STUB_LI16S_MICROMIPS(abfd, VAL) \
926 (ABI_64_P (abfd) \
927 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
928 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
929
5108fc1b
RS
930#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
931#define MIPS_FUNCTION_STUB_BIG_SIZE 20
1bbce132
MR
932#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
933#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
833794fc
MR
934#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
935#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
936
937/* The name of the dynamic interpreter. This is put in the .interp
938 section. */
939
940#define ELF_DYNAMIC_INTERPRETER(abfd) \
941 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
942 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
943 : "/usr/lib/libc.so.1")
944
945#ifdef BFD64
ee6423ed
AO
946#define MNAME(bfd,pre,pos) \
947 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
948#define ELF_R_SYM(bfd, i) \
949 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
950#define ELF_R_TYPE(bfd, i) \
951 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
952#define ELF_R_INFO(bfd, s, t) \
953 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
954#else
ee6423ed 955#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
956#define ELF_R_SYM(bfd, i) \
957 (ELF32_R_SYM (i))
958#define ELF_R_TYPE(bfd, i) \
959 (ELF32_R_TYPE (i))
960#define ELF_R_INFO(bfd, s, t) \
961 (ELF32_R_INFO (s, t))
962#endif
963\f
964 /* The mips16 compiler uses a couple of special sections to handle
965 floating point arguments.
966
967 Section names that look like .mips16.fn.FNNAME contain stubs that
968 copy floating point arguments from the fp regs to the gp regs and
969 then jump to FNNAME. If any 32 bit function calls FNNAME, the
970 call should be redirected to the stub instead. If no 32 bit
971 function calls FNNAME, the stub should be discarded. We need to
972 consider any reference to the function, not just a call, because
973 if the address of the function is taken we will need the stub,
974 since the address might be passed to a 32 bit function.
975
976 Section names that look like .mips16.call.FNNAME contain stubs
977 that copy floating point arguments from the gp regs to the fp
978 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
979 then any 16 bit function that calls FNNAME should be redirected
980 to the stub instead. If FNNAME is not a 32 bit function, the
981 stub should be discarded.
982
983 .mips16.call.fp.FNNAME sections are similar, but contain stubs
984 which call FNNAME and then copy the return value from the fp regs
985 to the gp regs. These stubs store the return value in $18 while
986 calling FNNAME; any function which might call one of these stubs
987 must arrange to save $18 around the call. (This case is not
988 needed for 32 bit functions that call 16 bit functions, because
989 16 bit functions always return floating point values in both
990 $f0/$f1 and $2/$3.)
991
992 Note that in all cases FNNAME might be defined statically.
993 Therefore, FNNAME is not used literally. Instead, the relocation
994 information will indicate which symbol the section is for.
995
996 We record any stubs that we find in the symbol table. */
997
998#define FN_STUB ".mips16.fn."
999#define CALL_STUB ".mips16.call."
1000#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
1001
1002#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1003#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1004#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 1005\f
861fb55a 1006/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
1007static const bfd_vma mips_o32_exec_plt0_entry[] =
1008{
861fb55a
DJ
1009 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1010 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1011 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1012 0x031cc023, /* subu $24, $24, $28 */
81f5d455 1013 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
861fb55a
DJ
1014 0x0018c082, /* srl $24, $24, 2 */
1015 0x0320f809, /* jalr $25 */
1016 0x2718fffe /* subu $24, $24, 2 */
1017};
1018
1019/* The format of the first PLT entry in an N32 executable. Different
1020 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
1021static const bfd_vma mips_n32_exec_plt0_entry[] =
1022{
861fb55a
DJ
1023 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1024 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1025 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1026 0x030ec023, /* subu $24, $24, $14 */
81f5d455 1027 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
861fb55a
DJ
1028 0x0018c082, /* srl $24, $24, 2 */
1029 0x0320f809, /* jalr $25 */
1030 0x2718fffe /* subu $24, $24, 2 */
1031};
1032
1033/* The format of the first PLT entry in an N64 executable. Different
1034 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
1035static const bfd_vma mips_n64_exec_plt0_entry[] =
1036{
861fb55a
DJ
1037 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1038 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1039 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1040 0x030ec023, /* subu $24, $24, $14 */
81f5d455 1041 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
861fb55a
DJ
1042 0x0018c0c2, /* srl $24, $24, 3 */
1043 0x0320f809, /* jalr $25 */
1044 0x2718fffe /* subu $24, $24, 2 */
1045};
1046
1bbce132
MR
1047/* The format of the microMIPS first PLT entry in an O32 executable.
1048 We rely on v0 ($2) rather than t8 ($24) to contain the address
1049 of the GOTPLT entry handled, so this stub may only be used when
1050 all the subsequent PLT entries are microMIPS code too.
1051
1052 The trailing NOP is for alignment and correct disassembly only. */
1053static const bfd_vma micromips_o32_exec_plt0_entry[] =
1054{
1055 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1056 0xff23, 0x0000, /* lw $25, 0($3) */
1057 0x0535, /* subu $2, $2, $3 */
1058 0x2525, /* srl $2, $2, 2 */
1059 0x3302, 0xfffe, /* subu $24, $2, 2 */
1060 0x0dff, /* move $15, $31 */
1061 0x45f9, /* jalrs $25 */
1062 0x0f83, /* move $28, $3 */
1063 0x0c00 /* nop */
1064};
1065
833794fc
MR
1066/* The format of the microMIPS first PLT entry in an O32 executable
1067 in the insn32 mode. */
1068static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1069{
1070 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1071 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1072 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1073 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1074 0x001f, 0x7950, /* move $15, $31 */
1075 0x0318, 0x1040, /* srl $24, $24, 2 */
1076 0x03f9, 0x0f3c, /* jalr $25 */
1077 0x3318, 0xfffe /* subu $24, $24, 2 */
1078};
1079
1bbce132 1080/* The format of subsequent standard PLT entries. */
6d30f5b2
NC
1081static const bfd_vma mips_exec_plt_entry[] =
1082{
861fb55a
DJ
1083 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1084 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1085 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1086 0x03200008 /* jr $25 */
1087};
1088
1bbce132
MR
1089/* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1090 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1091 directly addressable. */
1092static const bfd_vma mips16_o32_exec_plt_entry[] =
1093{
1094 0xb203, /* lw $2, 12($pc) */
1095 0x9a60, /* lw $3, 0($2) */
1096 0x651a, /* move $24, $2 */
1097 0xeb00, /* jr $3 */
1098 0x653b, /* move $25, $3 */
1099 0x6500, /* nop */
1100 0x0000, 0x0000 /* .word (.got.plt entry) */
1101};
1102
1103/* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1104 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1105static const bfd_vma micromips_o32_exec_plt_entry[] =
1106{
1107 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1108 0xff22, 0x0000, /* lw $25, 0($2) */
1109 0x4599, /* jr $25 */
1110 0x0f02 /* move $24, $2 */
1111};
1112
833794fc
MR
1113/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1114static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1115{
1116 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1117 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1118 0x0019, 0x0f3c, /* jr $25 */
1119 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1120};
1121
0a44bf69 1122/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
1123static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1124{
0a44bf69
RS
1125 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1126 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1127 0x8f390008, /* lw t9, 8(t9) */
1128 0x00000000, /* nop */
1129 0x03200008, /* jr t9 */
1130 0x00000000 /* nop */
1131};
1132
1133/* The format of subsequent PLT entries. */
6d30f5b2
NC
1134static const bfd_vma mips_vxworks_exec_plt_entry[] =
1135{
0a44bf69
RS
1136 0x10000000, /* b .PLT_resolver */
1137 0x24180000, /* li t8, <pltindex> */
1138 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1139 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1140 0x8f390000, /* lw t9, 0(t9) */
1141 0x00000000, /* nop */
1142 0x03200008, /* jr t9 */
1143 0x00000000 /* nop */
1144};
1145
1146/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
1147static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1148{
0a44bf69
RS
1149 0x8f990008, /* lw t9, 8(gp) */
1150 0x00000000, /* nop */
1151 0x03200008, /* jr t9 */
1152 0x00000000, /* nop */
1153 0x00000000, /* nop */
1154 0x00000000 /* nop */
1155};
1156
1157/* The format of subsequent PLT entries. */
6d30f5b2
NC
1158static const bfd_vma mips_vxworks_shared_plt_entry[] =
1159{
0a44bf69
RS
1160 0x10000000, /* b .PLT_resolver */
1161 0x24180000 /* li t8, <pltindex> */
1162};
1163\f
d21911ea
MR
1164/* microMIPS 32-bit opcode helper installer. */
1165
1166static void
1167bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1168{
1169 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1170 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1171}
1172
1173/* microMIPS 32-bit opcode helper retriever. */
1174
1175static bfd_vma
1176bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1177{
1178 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1179}
1180\f
b49e97c9
TS
1181/* Look up an entry in a MIPS ELF linker hash table. */
1182
1183#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1184 ((struct mips_elf_link_hash_entry *) \
1185 elf_link_hash_lookup (&(table)->root, (string), (create), \
1186 (copy), (follow)))
1187
1188/* Traverse a MIPS ELF linker hash table. */
1189
1190#define mips_elf_link_hash_traverse(table, func, info) \
1191 (elf_link_hash_traverse \
1192 (&(table)->root, \
9719ad41 1193 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1194 (info)))
1195
0f20cc35
DJ
1196/* Find the base offsets for thread-local storage in this object,
1197 for GD/LD and IE/LE respectively. */
1198
1199#define TP_OFFSET 0x7000
1200#define DTP_OFFSET 0x8000
1201
1202static bfd_vma
1203dtprel_base (struct bfd_link_info *info)
1204{
1205 /* If tls_sec is NULL, we should have signalled an error already. */
1206 if (elf_hash_table (info)->tls_sec == NULL)
1207 return 0;
1208 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1209}
1210
1211static bfd_vma
1212tprel_base (struct bfd_link_info *info)
1213{
1214 /* If tls_sec is NULL, we should have signalled an error already. */
1215 if (elf_hash_table (info)->tls_sec == NULL)
1216 return 0;
1217 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1218}
1219
b49e97c9
TS
1220/* Create an entry in a MIPS ELF linker hash table. */
1221
1222static struct bfd_hash_entry *
9719ad41
RS
1223mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1224 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1225{
1226 struct mips_elf_link_hash_entry *ret =
1227 (struct mips_elf_link_hash_entry *) entry;
1228
1229 /* Allocate the structure if it has not already been allocated by a
1230 subclass. */
9719ad41
RS
1231 if (ret == NULL)
1232 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1233 if (ret == NULL)
b49e97c9
TS
1234 return (struct bfd_hash_entry *) ret;
1235
1236 /* Call the allocation method of the superclass. */
1237 ret = ((struct mips_elf_link_hash_entry *)
1238 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1239 table, string));
9719ad41 1240 if (ret != NULL)
b49e97c9
TS
1241 {
1242 /* Set local fields. */
1243 memset (&ret->esym, 0, sizeof (EXTR));
1244 /* We use -2 as a marker to indicate that the information has
1245 not been set. -1 means there is no associated ifd. */
1246 ret->esym.ifd = -2;
861fb55a 1247 ret->la25_stub = 0;
b49e97c9 1248 ret->possibly_dynamic_relocs = 0;
b49e97c9 1249 ret->fn_stub = NULL;
b49e97c9
TS
1250 ret->call_stub = NULL;
1251 ret->call_fp_stub = NULL;
634835ae 1252 ret->global_got_area = GGA_NONE;
6ccf4795 1253 ret->got_only_for_calls = TRUE;
71782a75 1254 ret->readonly_reloc = FALSE;
861fb55a 1255 ret->has_static_relocs = FALSE;
71782a75
RS
1256 ret->no_fn_stub = FALSE;
1257 ret->need_fn_stub = FALSE;
861fb55a 1258 ret->has_nonpic_branches = FALSE;
33bb52fb 1259 ret->needs_lazy_stub = FALSE;
1bbce132 1260 ret->use_plt_entry = FALSE;
b49e97c9
TS
1261 }
1262
1263 return (struct bfd_hash_entry *) ret;
1264}
f0abc2a1 1265
6ae68ba3
MR
1266/* Allocate MIPS ELF private object data. */
1267
1268bfd_boolean
1269_bfd_mips_elf_mkobject (bfd *abfd)
1270{
1271 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1272 MIPS_ELF_DATA);
1273}
1274
f0abc2a1 1275bfd_boolean
9719ad41 1276_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1277{
f592407e
AM
1278 if (!sec->used_by_bfd)
1279 {
1280 struct _mips_elf_section_data *sdata;
1281 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1282
f592407e
AM
1283 sdata = bfd_zalloc (abfd, amt);
1284 if (sdata == NULL)
1285 return FALSE;
1286 sec->used_by_bfd = sdata;
1287 }
f0abc2a1
AM
1288
1289 return _bfd_elf_new_section_hook (abfd, sec);
1290}
b49e97c9
TS
1291\f
1292/* Read ECOFF debugging information from a .mdebug section into a
1293 ecoff_debug_info structure. */
1294
b34976b6 1295bfd_boolean
9719ad41
RS
1296_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1297 struct ecoff_debug_info *debug)
b49e97c9
TS
1298{
1299 HDRR *symhdr;
1300 const struct ecoff_debug_swap *swap;
9719ad41 1301 char *ext_hdr;
b49e97c9
TS
1302
1303 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1304 memset (debug, 0, sizeof (*debug));
1305
9719ad41 1306 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1307 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1308 goto error_return;
1309
9719ad41 1310 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1311 swap->external_hdr_size))
b49e97c9
TS
1312 goto error_return;
1313
1314 symhdr = &debug->symbolic_header;
1315 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1316
1317 /* The symbolic header contains absolute file offsets and sizes to
1318 read. */
1319#define READ(ptr, offset, count, size, type) \
1320 if (symhdr->count == 0) \
1321 debug->ptr = NULL; \
1322 else \
1323 { \
1324 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1325 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1326 if (debug->ptr == NULL) \
1327 goto error_return; \
9719ad41 1328 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1329 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1330 goto error_return; \
1331 }
1332
1333 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1334 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1335 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1336 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1337 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1338 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1339 union aux_ext *);
1340 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1341 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1342 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1343 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1344 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1345#undef READ
1346
1347 debug->fdr = NULL;
b49e97c9 1348
b34976b6 1349 return TRUE;
b49e97c9
TS
1350
1351 error_return:
1352 if (ext_hdr != NULL)
1353 free (ext_hdr);
1354 if (debug->line != NULL)
1355 free (debug->line);
1356 if (debug->external_dnr != NULL)
1357 free (debug->external_dnr);
1358 if (debug->external_pdr != NULL)
1359 free (debug->external_pdr);
1360 if (debug->external_sym != NULL)
1361 free (debug->external_sym);
1362 if (debug->external_opt != NULL)
1363 free (debug->external_opt);
1364 if (debug->external_aux != NULL)
1365 free (debug->external_aux);
1366 if (debug->ss != NULL)
1367 free (debug->ss);
1368 if (debug->ssext != NULL)
1369 free (debug->ssext);
1370 if (debug->external_fdr != NULL)
1371 free (debug->external_fdr);
1372 if (debug->external_rfd != NULL)
1373 free (debug->external_rfd);
1374 if (debug->external_ext != NULL)
1375 free (debug->external_ext);
b34976b6 1376 return FALSE;
b49e97c9
TS
1377}
1378\f
1379/* Swap RPDR (runtime procedure table entry) for output. */
1380
1381static void
9719ad41 1382ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1383{
1384 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1385 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1386 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1387 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1388 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1389 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1390
1391 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1392 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1393
1394 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1395}
1396
1397/* Create a runtime procedure table from the .mdebug section. */
1398
b34976b6 1399static bfd_boolean
9719ad41
RS
1400mips_elf_create_procedure_table (void *handle, bfd *abfd,
1401 struct bfd_link_info *info, asection *s,
1402 struct ecoff_debug_info *debug)
b49e97c9
TS
1403{
1404 const struct ecoff_debug_swap *swap;
1405 HDRR *hdr = &debug->symbolic_header;
1406 RPDR *rpdr, *rp;
1407 struct rpdr_ext *erp;
9719ad41 1408 void *rtproc;
b49e97c9
TS
1409 struct pdr_ext *epdr;
1410 struct sym_ext *esym;
1411 char *ss, **sv;
1412 char *str;
1413 bfd_size_type size;
1414 bfd_size_type count;
1415 unsigned long sindex;
1416 unsigned long i;
1417 PDR pdr;
1418 SYMR sym;
1419 const char *no_name_func = _("static procedure (no name)");
1420
1421 epdr = NULL;
1422 rpdr = NULL;
1423 esym = NULL;
1424 ss = NULL;
1425 sv = NULL;
1426
1427 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1428
1429 sindex = strlen (no_name_func) + 1;
1430 count = hdr->ipdMax;
1431 if (count > 0)
1432 {
1433 size = swap->external_pdr_size;
1434
9719ad41 1435 epdr = bfd_malloc (size * count);
b49e97c9
TS
1436 if (epdr == NULL)
1437 goto error_return;
1438
9719ad41 1439 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1440 goto error_return;
1441
1442 size = sizeof (RPDR);
9719ad41 1443 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1444 if (rpdr == NULL)
1445 goto error_return;
1446
1447 size = sizeof (char *);
9719ad41 1448 sv = bfd_malloc (size * count);
b49e97c9
TS
1449 if (sv == NULL)
1450 goto error_return;
1451
1452 count = hdr->isymMax;
1453 size = swap->external_sym_size;
9719ad41 1454 esym = bfd_malloc (size * count);
b49e97c9
TS
1455 if (esym == NULL)
1456 goto error_return;
1457
9719ad41 1458 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1459 goto error_return;
1460
1461 count = hdr->issMax;
9719ad41 1462 ss = bfd_malloc (count);
b49e97c9
TS
1463 if (ss == NULL)
1464 goto error_return;
f075ee0c 1465 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1466 goto error_return;
1467
1468 count = hdr->ipdMax;
1469 for (i = 0; i < (unsigned long) count; i++, rp++)
1470 {
9719ad41
RS
1471 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1472 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1473 rp->adr = sym.value;
1474 rp->regmask = pdr.regmask;
1475 rp->regoffset = pdr.regoffset;
1476 rp->fregmask = pdr.fregmask;
1477 rp->fregoffset = pdr.fregoffset;
1478 rp->frameoffset = pdr.frameoffset;
1479 rp->framereg = pdr.framereg;
1480 rp->pcreg = pdr.pcreg;
1481 rp->irpss = sindex;
1482 sv[i] = ss + sym.iss;
1483 sindex += strlen (sv[i]) + 1;
1484 }
1485 }
1486
1487 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1488 size = BFD_ALIGN (size, 16);
9719ad41 1489 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1490 if (rtproc == NULL)
1491 {
1492 mips_elf_hash_table (info)->procedure_count = 0;
1493 goto error_return;
1494 }
1495
1496 mips_elf_hash_table (info)->procedure_count = count + 2;
1497
9719ad41 1498 erp = rtproc;
b49e97c9
TS
1499 memset (erp, 0, sizeof (struct rpdr_ext));
1500 erp++;
1501 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1502 strcpy (str, no_name_func);
1503 str += strlen (no_name_func) + 1;
1504 for (i = 0; i < count; i++)
1505 {
1506 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1507 strcpy (str, sv[i]);
1508 str += strlen (sv[i]) + 1;
1509 }
1510 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1511
1512 /* Set the size and contents of .rtproc section. */
eea6121a 1513 s->size = size;
9719ad41 1514 s->contents = rtproc;
b49e97c9
TS
1515
1516 /* Skip this section later on (I don't think this currently
1517 matters, but someday it might). */
8423293d 1518 s->map_head.link_order = NULL;
b49e97c9
TS
1519
1520 if (epdr != NULL)
1521 free (epdr);
1522 if (rpdr != NULL)
1523 free (rpdr);
1524 if (esym != NULL)
1525 free (esym);
1526 if (ss != NULL)
1527 free (ss);
1528 if (sv != NULL)
1529 free (sv);
1530
b34976b6 1531 return TRUE;
b49e97c9
TS
1532
1533 error_return:
1534 if (epdr != NULL)
1535 free (epdr);
1536 if (rpdr != NULL)
1537 free (rpdr);
1538 if (esym != NULL)
1539 free (esym);
1540 if (ss != NULL)
1541 free (ss);
1542 if (sv != NULL)
1543 free (sv);
b34976b6 1544 return FALSE;
b49e97c9 1545}
738e5348 1546\f
861fb55a
DJ
1547/* We're going to create a stub for H. Create a symbol for the stub's
1548 value and size, to help make the disassembly easier to read. */
1549
1550static bfd_boolean
1551mips_elf_create_stub_symbol (struct bfd_link_info *info,
1552 struct mips_elf_link_hash_entry *h,
1553 const char *prefix, asection *s, bfd_vma value,
1554 bfd_vma size)
1555{
1556 struct bfd_link_hash_entry *bh;
1557 struct elf_link_hash_entry *elfh;
1558 const char *name;
1559
df58fc94
RS
1560 if (ELF_ST_IS_MICROMIPS (h->root.other))
1561 value |= 1;
1562
861fb55a
DJ
1563 /* Create a new symbol. */
1564 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1565 bh = NULL;
1566 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1567 BSF_LOCAL, s, value, NULL,
1568 TRUE, FALSE, &bh))
1569 return FALSE;
1570
1571 /* Make it a local function. */
1572 elfh = (struct elf_link_hash_entry *) bh;
1573 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1574 elfh->size = size;
1575 elfh->forced_local = 1;
1576 return TRUE;
1577}
1578
738e5348
RS
1579/* We're about to redefine H. Create a symbol to represent H's
1580 current value and size, to help make the disassembly easier
1581 to read. */
1582
1583static bfd_boolean
1584mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1585 struct mips_elf_link_hash_entry *h,
1586 const char *prefix)
1587{
1588 struct bfd_link_hash_entry *bh;
1589 struct elf_link_hash_entry *elfh;
1590 const char *name;
1591 asection *s;
1592 bfd_vma value;
1593
1594 /* Read the symbol's value. */
1595 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1596 || h->root.root.type == bfd_link_hash_defweak);
1597 s = h->root.root.u.def.section;
1598 value = h->root.root.u.def.value;
1599
1600 /* Create a new symbol. */
1601 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1602 bh = NULL;
1603 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1604 BSF_LOCAL, s, value, NULL,
1605 TRUE, FALSE, &bh))
1606 return FALSE;
1607
1608 /* Make it local and copy the other attributes from H. */
1609 elfh = (struct elf_link_hash_entry *) bh;
1610 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1611 elfh->other = h->root.other;
1612 elfh->size = h->root.size;
1613 elfh->forced_local = 1;
1614 return TRUE;
1615}
1616
1617/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1618 function rather than to a hard-float stub. */
1619
1620static bfd_boolean
1621section_allows_mips16_refs_p (asection *section)
1622{
1623 const char *name;
1624
1625 name = bfd_get_section_name (section->owner, section);
1626 return (FN_STUB_P (name)
1627 || CALL_STUB_P (name)
1628 || CALL_FP_STUB_P (name)
1629 || strcmp (name, ".pdr") == 0);
1630}
1631
1632/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1633 stub section of some kind. Return the R_SYMNDX of the target
1634 function, or 0 if we can't decide which function that is. */
1635
1636static unsigned long
cb4437b8
MR
1637mips16_stub_symndx (const struct elf_backend_data *bed,
1638 asection *sec ATTRIBUTE_UNUSED,
502e814e 1639 const Elf_Internal_Rela *relocs,
738e5348
RS
1640 const Elf_Internal_Rela *relend)
1641{
cb4437b8 1642 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
738e5348
RS
1643 const Elf_Internal_Rela *rel;
1644
cb4437b8
MR
1645 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1646 one in a compound relocation. */
1647 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
738e5348
RS
1648 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1649 return ELF_R_SYM (sec->owner, rel->r_info);
1650
1651 /* Otherwise trust the first relocation, whatever its kind. This is
1652 the traditional behavior. */
1653 if (relocs < relend)
1654 return ELF_R_SYM (sec->owner, relocs->r_info);
1655
1656 return 0;
1657}
b49e97c9
TS
1658
1659/* Check the mips16 stubs for a particular symbol, and see if we can
1660 discard them. */
1661
861fb55a
DJ
1662static void
1663mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1664 struct mips_elf_link_hash_entry *h)
b49e97c9 1665{
738e5348
RS
1666 /* Dynamic symbols must use the standard call interface, in case other
1667 objects try to call them. */
1668 if (h->fn_stub != NULL
1669 && h->root.dynindx != -1)
1670 {
1671 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1672 h->need_fn_stub = TRUE;
1673 }
1674
b49e97c9
TS
1675 if (h->fn_stub != NULL
1676 && ! h->need_fn_stub)
1677 {
1678 /* We don't need the fn_stub; the only references to this symbol
1679 are 16 bit calls. Clobber the size to 0 to prevent it from
1680 being included in the link. */
eea6121a 1681 h->fn_stub->size = 0;
b49e97c9
TS
1682 h->fn_stub->flags &= ~SEC_RELOC;
1683 h->fn_stub->reloc_count = 0;
1684 h->fn_stub->flags |= SEC_EXCLUDE;
1685 }
1686
1687 if (h->call_stub != NULL
30c09090 1688 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1689 {
1690 /* We don't need the call_stub; this is a 16 bit function, so
1691 calls from other 16 bit functions are OK. Clobber the size
1692 to 0 to prevent it from being included in the link. */
eea6121a 1693 h->call_stub->size = 0;
b49e97c9
TS
1694 h->call_stub->flags &= ~SEC_RELOC;
1695 h->call_stub->reloc_count = 0;
1696 h->call_stub->flags |= SEC_EXCLUDE;
1697 }
1698
1699 if (h->call_fp_stub != NULL
30c09090 1700 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1701 {
1702 /* We don't need the call_stub; this is a 16 bit function, so
1703 calls from other 16 bit functions are OK. Clobber the size
1704 to 0 to prevent it from being included in the link. */
eea6121a 1705 h->call_fp_stub->size = 0;
b49e97c9
TS
1706 h->call_fp_stub->flags &= ~SEC_RELOC;
1707 h->call_fp_stub->reloc_count = 0;
1708 h->call_fp_stub->flags |= SEC_EXCLUDE;
1709 }
861fb55a
DJ
1710}
1711
1712/* Hashtable callbacks for mips_elf_la25_stubs. */
1713
1714static hashval_t
1715mips_elf_la25_stub_hash (const void *entry_)
1716{
1717 const struct mips_elf_la25_stub *entry;
1718
1719 entry = (struct mips_elf_la25_stub *) entry_;
1720 return entry->h->root.root.u.def.section->id
1721 + entry->h->root.root.u.def.value;
1722}
1723
1724static int
1725mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1726{
1727 const struct mips_elf_la25_stub *entry1, *entry2;
1728
1729 entry1 = (struct mips_elf_la25_stub *) entry1_;
1730 entry2 = (struct mips_elf_la25_stub *) entry2_;
1731 return ((entry1->h->root.root.u.def.section
1732 == entry2->h->root.root.u.def.section)
1733 && (entry1->h->root.root.u.def.value
1734 == entry2->h->root.root.u.def.value));
1735}
1736
1737/* Called by the linker to set up the la25 stub-creation code. FN is
1738 the linker's implementation of add_stub_function. Return true on
1739 success. */
1740
1741bfd_boolean
1742_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1743 asection *(*fn) (const char *, asection *,
1744 asection *))
1745{
1746 struct mips_elf_link_hash_table *htab;
1747
1748 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1749 if (htab == NULL)
1750 return FALSE;
1751
861fb55a
DJ
1752 htab->add_stub_section = fn;
1753 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1754 mips_elf_la25_stub_eq, NULL);
1755 if (htab->la25_stubs == NULL)
1756 return FALSE;
1757
1758 return TRUE;
1759}
1760
1761/* Return true if H is a locally-defined PIC function, in the sense
8f0c309a
CLT
1762 that it or its fn_stub might need $25 to be valid on entry.
1763 Note that MIPS16 functions set up $gp using PC-relative instructions,
1764 so they themselves never need $25 to be valid. Only non-MIPS16
1765 entry points are of interest here. */
861fb55a
DJ
1766
1767static bfd_boolean
1768mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1769{
1770 return ((h->root.root.type == bfd_link_hash_defined
1771 || h->root.root.type == bfd_link_hash_defweak)
1772 && h->root.def_regular
1773 && !bfd_is_abs_section (h->root.root.u.def.section)
8f0c309a
CLT
1774 && (!ELF_ST_IS_MIPS16 (h->root.other)
1775 || (h->fn_stub && h->need_fn_stub))
861fb55a
DJ
1776 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1777 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1778}
1779
8f0c309a
CLT
1780/* Set *SEC to the input section that contains the target of STUB.
1781 Return the offset of the target from the start of that section. */
1782
1783static bfd_vma
1784mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1785 asection **sec)
1786{
1787 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1788 {
1789 BFD_ASSERT (stub->h->need_fn_stub);
1790 *sec = stub->h->fn_stub;
1791 return 0;
1792 }
1793 else
1794 {
1795 *sec = stub->h->root.root.u.def.section;
1796 return stub->h->root.root.u.def.value;
1797 }
1798}
1799
861fb55a
DJ
1800/* STUB describes an la25 stub that we have decided to implement
1801 by inserting an LUI/ADDIU pair before the target function.
1802 Create the section and redirect the function symbol to it. */
1803
1804static bfd_boolean
1805mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1806 struct bfd_link_info *info)
1807{
1808 struct mips_elf_link_hash_table *htab;
1809 char *name;
1810 asection *s, *input_section;
1811 unsigned int align;
1812
1813 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1814 if (htab == NULL)
1815 return FALSE;
861fb55a
DJ
1816
1817 /* Create a unique name for the new section. */
1818 name = bfd_malloc (11 + sizeof (".text.stub."));
1819 if (name == NULL)
1820 return FALSE;
1821 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1822
1823 /* Create the section. */
8f0c309a 1824 mips_elf_get_la25_target (stub, &input_section);
861fb55a
DJ
1825 s = htab->add_stub_section (name, input_section,
1826 input_section->output_section);
1827 if (s == NULL)
1828 return FALSE;
1829
1830 /* Make sure that any padding goes before the stub. */
1831 align = input_section->alignment_power;
1832 if (!bfd_set_section_alignment (s->owner, s, align))
1833 return FALSE;
1834 if (align > 3)
1835 s->size = (1 << align) - 8;
1836
1837 /* Create a symbol for the stub. */
1838 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1839 stub->stub_section = s;
1840 stub->offset = s->size;
1841
1842 /* Allocate room for it. */
1843 s->size += 8;
1844 return TRUE;
1845}
1846
1847/* STUB describes an la25 stub that we have decided to implement
1848 with a separate trampoline. Allocate room for it and redirect
1849 the function symbol to it. */
1850
1851static bfd_boolean
1852mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1853 struct bfd_link_info *info)
1854{
1855 struct mips_elf_link_hash_table *htab;
1856 asection *s;
1857
1858 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1859 if (htab == NULL)
1860 return FALSE;
861fb55a
DJ
1861
1862 /* Create a trampoline section, if we haven't already. */
1863 s = htab->strampoline;
1864 if (s == NULL)
1865 {
1866 asection *input_section = stub->h->root.root.u.def.section;
1867 s = htab->add_stub_section (".text", NULL,
1868 input_section->output_section);
1869 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1870 return FALSE;
1871 htab->strampoline = s;
1872 }
1873
1874 /* Create a symbol for the stub. */
1875 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1876 stub->stub_section = s;
1877 stub->offset = s->size;
1878
1879 /* Allocate room for it. */
1880 s->size += 16;
1881 return TRUE;
1882}
1883
1884/* H describes a symbol that needs an la25 stub. Make sure that an
1885 appropriate stub exists and point H at it. */
1886
1887static bfd_boolean
1888mips_elf_add_la25_stub (struct bfd_link_info *info,
1889 struct mips_elf_link_hash_entry *h)
1890{
1891 struct mips_elf_link_hash_table *htab;
1892 struct mips_elf_la25_stub search, *stub;
1893 bfd_boolean use_trampoline_p;
1894 asection *s;
1895 bfd_vma value;
1896 void **slot;
1897
861fb55a
DJ
1898 /* Describe the stub we want. */
1899 search.stub_section = NULL;
1900 search.offset = 0;
1901 search.h = h;
1902
1903 /* See if we've already created an equivalent stub. */
1904 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1905 if (htab == NULL)
1906 return FALSE;
1907
861fb55a
DJ
1908 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1909 if (slot == NULL)
1910 return FALSE;
1911
1912 stub = (struct mips_elf_la25_stub *) *slot;
1913 if (stub != NULL)
1914 {
1915 /* We can reuse the existing stub. */
1916 h->la25_stub = stub;
1917 return TRUE;
1918 }
1919
1920 /* Create a permanent copy of ENTRY and add it to the hash table. */
1921 stub = bfd_malloc (sizeof (search));
1922 if (stub == NULL)
1923 return FALSE;
1924 *stub = search;
1925 *slot = stub;
1926
8f0c309a
CLT
1927 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1928 of the section and if we would need no more than 2 nops. */
1929 value = mips_elf_get_la25_target (stub, &s);
1930 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1931
861fb55a
DJ
1932 h->la25_stub = stub;
1933 return (use_trampoline_p
1934 ? mips_elf_add_la25_trampoline (stub, info)
1935 : mips_elf_add_la25_intro (stub, info));
1936}
1937
1938/* A mips_elf_link_hash_traverse callback that is called before sizing
1939 sections. DATA points to a mips_htab_traverse_info structure. */
1940
1941static bfd_boolean
1942mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1943{
1944 struct mips_htab_traverse_info *hti;
1945
1946 hti = (struct mips_htab_traverse_info *) data;
861fb55a
DJ
1947 if (!hti->info->relocatable)
1948 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1949
861fb55a
DJ
1950 if (mips_elf_local_pic_function_p (h))
1951 {
ba85c43e
NC
1952 /* PR 12845: If H is in a section that has been garbage
1953 collected it will have its output section set to *ABS*. */
1954 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1955 return TRUE;
1956
861fb55a
DJ
1957 /* H is a function that might need $25 to be valid on entry.
1958 If we're creating a non-PIC relocatable object, mark H as
1959 being PIC. If we're creating a non-relocatable object with
1960 non-PIC branches and jumps to H, make sure that H has an la25
1961 stub. */
1962 if (hti->info->relocatable)
1963 {
1964 if (!PIC_OBJECT_P (hti->output_bfd))
1965 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1966 }
1967 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1968 {
1969 hti->error = TRUE;
1970 return FALSE;
1971 }
1972 }
b34976b6 1973 return TRUE;
b49e97c9
TS
1974}
1975\f
d6f16593
MR
1976/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1977 Most mips16 instructions are 16 bits, but these instructions
1978 are 32 bits.
1979
1980 The format of these instructions is:
1981
1982 +--------------+--------------------------------+
1983 | JALX | X| Imm 20:16 | Imm 25:21 |
1984 +--------------+--------------------------------+
1985 | Immediate 15:0 |
1986 +-----------------------------------------------+
1987
1988 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1989 Note that the immediate value in the first word is swapped.
1990
1991 When producing a relocatable object file, R_MIPS16_26 is
1992 handled mostly like R_MIPS_26. In particular, the addend is
1993 stored as a straight 26-bit value in a 32-bit instruction.
1994 (gas makes life simpler for itself by never adjusting a
1995 R_MIPS16_26 reloc to be against a section, so the addend is
1996 always zero). However, the 32 bit instruction is stored as 2
1997 16-bit values, rather than a single 32-bit value. In a
1998 big-endian file, the result is the same; in a little-endian
1999 file, the two 16-bit halves of the 32 bit value are swapped.
2000 This is so that a disassembler can recognize the jal
2001 instruction.
2002
2003 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2004 instruction stored as two 16-bit values. The addend A is the
2005 contents of the targ26 field. The calculation is the same as
2006 R_MIPS_26. When storing the calculated value, reorder the
2007 immediate value as shown above, and don't forget to store the
2008 value as two 16-bit values.
2009
2010 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2011 defined as
2012
2013 big-endian:
2014 +--------+----------------------+
2015 | | |
2016 | | targ26-16 |
2017 |31 26|25 0|
2018 +--------+----------------------+
2019
2020 little-endian:
2021 +----------+------+-------------+
2022 | | | |
2023 | sub1 | | sub2 |
2024 |0 9|10 15|16 31|
2025 +----------+--------------------+
2026 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2027 ((sub1 << 16) | sub2)).
2028
2029 When producing a relocatable object file, the calculation is
2030 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2031 When producing a fully linked file, the calculation is
2032 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2033 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2034
738e5348
RS
2035 The table below lists the other MIPS16 instruction relocations.
2036 Each one is calculated in the same way as the non-MIPS16 relocation
2037 given on the right, but using the extended MIPS16 layout of 16-bit
2038 immediate fields:
2039
2040 R_MIPS16_GPREL R_MIPS_GPREL16
2041 R_MIPS16_GOT16 R_MIPS_GOT16
2042 R_MIPS16_CALL16 R_MIPS_CALL16
2043 R_MIPS16_HI16 R_MIPS_HI16
2044 R_MIPS16_LO16 R_MIPS_LO16
2045
2046 A typical instruction will have a format like this:
d6f16593
MR
2047
2048 +--------------+--------------------------------+
2049 | EXTEND | Imm 10:5 | Imm 15:11 |
2050 +--------------+--------------------------------+
2051 | Major | rx | ry | Imm 4:0 |
2052 +--------------+--------------------------------+
2053
2054 EXTEND is the five bit value 11110. Major is the instruction
2055 opcode.
2056
738e5348
RS
2057 All we need to do here is shuffle the bits appropriately.
2058 As above, the two 16-bit halves must be swapped on a
2059 little-endian system. */
2060
2061static inline bfd_boolean
2062mips16_reloc_p (int r_type)
2063{
2064 switch (r_type)
2065 {
2066 case R_MIPS16_26:
2067 case R_MIPS16_GPREL:
2068 case R_MIPS16_GOT16:
2069 case R_MIPS16_CALL16:
2070 case R_MIPS16_HI16:
2071 case R_MIPS16_LO16:
d0f13682
CLT
2072 case R_MIPS16_TLS_GD:
2073 case R_MIPS16_TLS_LDM:
2074 case R_MIPS16_TLS_DTPREL_HI16:
2075 case R_MIPS16_TLS_DTPREL_LO16:
2076 case R_MIPS16_TLS_GOTTPREL:
2077 case R_MIPS16_TLS_TPREL_HI16:
2078 case R_MIPS16_TLS_TPREL_LO16:
738e5348
RS
2079 return TRUE;
2080
2081 default:
2082 return FALSE;
2083 }
2084}
2085
df58fc94
RS
2086/* Check if a microMIPS reloc. */
2087
2088static inline bfd_boolean
2089micromips_reloc_p (unsigned int r_type)
2090{
2091 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2092}
2093
2094/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2095 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2096 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2097
2098static inline bfd_boolean
2099micromips_reloc_shuffle_p (unsigned int r_type)
2100{
2101 return (micromips_reloc_p (r_type)
2102 && r_type != R_MICROMIPS_PC7_S1
2103 && r_type != R_MICROMIPS_PC10_S1);
2104}
2105
738e5348
RS
2106static inline bfd_boolean
2107got16_reloc_p (int r_type)
2108{
df58fc94
RS
2109 return (r_type == R_MIPS_GOT16
2110 || r_type == R_MIPS16_GOT16
2111 || r_type == R_MICROMIPS_GOT16);
738e5348
RS
2112}
2113
2114static inline bfd_boolean
2115call16_reloc_p (int r_type)
2116{
df58fc94
RS
2117 return (r_type == R_MIPS_CALL16
2118 || r_type == R_MIPS16_CALL16
2119 || r_type == R_MICROMIPS_CALL16);
2120}
2121
2122static inline bfd_boolean
2123got_disp_reloc_p (unsigned int r_type)
2124{
2125 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2126}
2127
2128static inline bfd_boolean
2129got_page_reloc_p (unsigned int r_type)
2130{
2131 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2132}
2133
2134static inline bfd_boolean
2135got_ofst_reloc_p (unsigned int r_type)
2136{
2137 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
2138}
2139
2140static inline bfd_boolean
2141got_hi16_reloc_p (unsigned int r_type)
2142{
2143 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
2144}
2145
2146static inline bfd_boolean
2147got_lo16_reloc_p (unsigned int r_type)
2148{
2149 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2150}
2151
2152static inline bfd_boolean
2153call_hi16_reloc_p (unsigned int r_type)
2154{
2155 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2156}
2157
2158static inline bfd_boolean
2159call_lo16_reloc_p (unsigned int r_type)
2160{
2161 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
738e5348
RS
2162}
2163
2164static inline bfd_boolean
2165hi16_reloc_p (int r_type)
2166{
df58fc94
RS
2167 return (r_type == R_MIPS_HI16
2168 || r_type == R_MIPS16_HI16
2169 || r_type == R_MICROMIPS_HI16);
738e5348 2170}
d6f16593 2171
738e5348
RS
2172static inline bfd_boolean
2173lo16_reloc_p (int r_type)
2174{
df58fc94
RS
2175 return (r_type == R_MIPS_LO16
2176 || r_type == R_MIPS16_LO16
2177 || r_type == R_MICROMIPS_LO16);
738e5348
RS
2178}
2179
2180static inline bfd_boolean
2181mips16_call_reloc_p (int r_type)
2182{
2183 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2184}
d6f16593 2185
38a7df63
CF
2186static inline bfd_boolean
2187jal_reloc_p (int r_type)
2188{
df58fc94
RS
2189 return (r_type == R_MIPS_26
2190 || r_type == R_MIPS16_26
2191 || r_type == R_MICROMIPS_26_S1);
2192}
2193
2194static inline bfd_boolean
2195micromips_branch_reloc_p (int r_type)
2196{
2197 return (r_type == R_MICROMIPS_26_S1
2198 || r_type == R_MICROMIPS_PC16_S1
2199 || r_type == R_MICROMIPS_PC10_S1
2200 || r_type == R_MICROMIPS_PC7_S1);
2201}
2202
2203static inline bfd_boolean
2204tls_gd_reloc_p (unsigned int r_type)
2205{
d0f13682
CLT
2206 return (r_type == R_MIPS_TLS_GD
2207 || r_type == R_MIPS16_TLS_GD
2208 || r_type == R_MICROMIPS_TLS_GD);
df58fc94
RS
2209}
2210
2211static inline bfd_boolean
2212tls_ldm_reloc_p (unsigned int r_type)
2213{
d0f13682
CLT
2214 return (r_type == R_MIPS_TLS_LDM
2215 || r_type == R_MIPS16_TLS_LDM
2216 || r_type == R_MICROMIPS_TLS_LDM);
df58fc94
RS
2217}
2218
2219static inline bfd_boolean
2220tls_gottprel_reloc_p (unsigned int r_type)
2221{
d0f13682
CLT
2222 return (r_type == R_MIPS_TLS_GOTTPREL
2223 || r_type == R_MIPS16_TLS_GOTTPREL
2224 || r_type == R_MICROMIPS_TLS_GOTTPREL);
38a7df63
CF
2225}
2226
d6f16593 2227void
df58fc94
RS
2228_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2229 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2230{
df58fc94 2231 bfd_vma first, second, val;
d6f16593 2232
df58fc94 2233 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2234 return;
2235
df58fc94
RS
2236 /* Pick up the first and second halfwords of the instruction. */
2237 first = bfd_get_16 (abfd, data);
2238 second = bfd_get_16 (abfd, data + 2);
2239 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2240 val = first << 16 | second;
2241 else if (r_type != R_MIPS16_26)
2242 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2243 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
d6f16593 2244 else
df58fc94
RS
2245 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2246 | ((first & 0x1f) << 21) | second);
d6f16593
MR
2247 bfd_put_32 (abfd, val, data);
2248}
2249
2250void
df58fc94
RS
2251_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2252 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2253{
df58fc94 2254 bfd_vma first, second, val;
d6f16593 2255
df58fc94 2256 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2257 return;
2258
2259 val = bfd_get_32 (abfd, data);
df58fc94 2260 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
d6f16593 2261 {
df58fc94
RS
2262 second = val & 0xffff;
2263 first = val >> 16;
2264 }
2265 else if (r_type != R_MIPS16_26)
2266 {
2267 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2268 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
d6f16593
MR
2269 }
2270 else
2271 {
df58fc94
RS
2272 second = val & 0xffff;
2273 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2274 | ((val >> 21) & 0x1f);
d6f16593 2275 }
df58fc94
RS
2276 bfd_put_16 (abfd, second, data + 2);
2277 bfd_put_16 (abfd, first, data);
d6f16593
MR
2278}
2279
b49e97c9 2280bfd_reloc_status_type
9719ad41
RS
2281_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2282 arelent *reloc_entry, asection *input_section,
2283 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
2284{
2285 bfd_vma relocation;
a7ebbfdf 2286 bfd_signed_vma val;
30ac9238 2287 bfd_reloc_status_type status;
b49e97c9
TS
2288
2289 if (bfd_is_com_section (symbol->section))
2290 relocation = 0;
2291 else
2292 relocation = symbol->value;
2293
2294 relocation += symbol->section->output_section->vma;
2295 relocation += symbol->section->output_offset;
2296
07515404 2297 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
2298 return bfd_reloc_outofrange;
2299
b49e97c9 2300 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
2301 val = reloc_entry->addend;
2302
30ac9238 2303 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 2304
b49e97c9 2305 /* Adjust val for the final section location and GP value. If we
1049f94e 2306 are producing relocatable output, we don't want to do this for
b49e97c9 2307 an external symbol. */
1049f94e 2308 if (! relocatable
b49e97c9
TS
2309 || (symbol->flags & BSF_SECTION_SYM) != 0)
2310 val += relocation - gp;
2311
a7ebbfdf
TS
2312 if (reloc_entry->howto->partial_inplace)
2313 {
30ac9238
RS
2314 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2315 (bfd_byte *) data
2316 + reloc_entry->address);
2317 if (status != bfd_reloc_ok)
2318 return status;
a7ebbfdf
TS
2319 }
2320 else
2321 reloc_entry->addend = val;
b49e97c9 2322
1049f94e 2323 if (relocatable)
b49e97c9 2324 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2325
2326 return bfd_reloc_ok;
2327}
2328
2329/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2330 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2331 that contains the relocation field and DATA points to the start of
2332 INPUT_SECTION. */
2333
2334struct mips_hi16
2335{
2336 struct mips_hi16 *next;
2337 bfd_byte *data;
2338 asection *input_section;
2339 arelent rel;
2340};
2341
2342/* FIXME: This should not be a static variable. */
2343
2344static struct mips_hi16 *mips_hi16_list;
2345
2346/* A howto special_function for REL *HI16 relocations. We can only
2347 calculate the correct value once we've seen the partnering
2348 *LO16 relocation, so just save the information for later.
2349
2350 The ABI requires that the *LO16 immediately follow the *HI16.
2351 However, as a GNU extension, we permit an arbitrary number of
2352 *HI16s to be associated with a single *LO16. This significantly
2353 simplies the relocation handling in gcc. */
2354
2355bfd_reloc_status_type
2356_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2357 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2358 asection *input_section, bfd *output_bfd,
2359 char **error_message ATTRIBUTE_UNUSED)
2360{
2361 struct mips_hi16 *n;
2362
07515404 2363 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2364 return bfd_reloc_outofrange;
2365
2366 n = bfd_malloc (sizeof *n);
2367 if (n == NULL)
2368 return bfd_reloc_outofrange;
2369
2370 n->next = mips_hi16_list;
2371 n->data = data;
2372 n->input_section = input_section;
2373 n->rel = *reloc_entry;
2374 mips_hi16_list = n;
2375
2376 if (output_bfd != NULL)
2377 reloc_entry->address += input_section->output_offset;
2378
2379 return bfd_reloc_ok;
2380}
2381
738e5348 2382/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2383 like any other 16-bit relocation when applied to global symbols, but is
2384 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2385
2386bfd_reloc_status_type
2387_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2388 void *data, asection *input_section,
2389 bfd *output_bfd, char **error_message)
2390{
2391 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2392 || bfd_is_und_section (bfd_get_section (symbol))
2393 || bfd_is_com_section (bfd_get_section (symbol)))
2394 /* The relocation is against a global symbol. */
2395 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2396 input_section, output_bfd,
2397 error_message);
2398
2399 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2400 input_section, output_bfd, error_message);
2401}
2402
2403/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2404 is a straightforward 16 bit inplace relocation, but we must deal with
2405 any partnering high-part relocations as well. */
2406
2407bfd_reloc_status_type
2408_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2409 void *data, asection *input_section,
2410 bfd *output_bfd, char **error_message)
2411{
2412 bfd_vma vallo;
d6f16593 2413 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2414
07515404 2415 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2416 return bfd_reloc_outofrange;
2417
df58fc94 2418 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
d6f16593 2419 location);
df58fc94
RS
2420 vallo = bfd_get_32 (abfd, location);
2421 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2422 location);
d6f16593 2423
30ac9238
RS
2424 while (mips_hi16_list != NULL)
2425 {
2426 bfd_reloc_status_type ret;
2427 struct mips_hi16 *hi;
2428
2429 hi = mips_hi16_list;
2430
738e5348
RS
2431 /* R_MIPS*_GOT16 relocations are something of a special case. We
2432 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2433 relocation (with a rightshift of 16). However, since GOT16
2434 relocations can also be used with global symbols, their howto
2435 has a rightshift of 0. */
2436 if (hi->rel.howto->type == R_MIPS_GOT16)
2437 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2438 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2439 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
df58fc94
RS
2440 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2441 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
30ac9238
RS
2442
2443 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2444 carry or borrow will induce a change of +1 or -1 in the high part. */
2445 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2446
30ac9238
RS
2447 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2448 hi->input_section, output_bfd,
2449 error_message);
2450 if (ret != bfd_reloc_ok)
2451 return ret;
2452
2453 mips_hi16_list = hi->next;
2454 free (hi);
2455 }
2456
2457 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2458 input_section, output_bfd,
2459 error_message);
2460}
2461
2462/* A generic howto special_function. This calculates and installs the
2463 relocation itself, thus avoiding the oft-discussed problems in
2464 bfd_perform_relocation and bfd_install_relocation. */
2465
2466bfd_reloc_status_type
2467_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2468 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2469 asection *input_section, bfd *output_bfd,
2470 char **error_message ATTRIBUTE_UNUSED)
2471{
2472 bfd_signed_vma val;
2473 bfd_reloc_status_type status;
2474 bfd_boolean relocatable;
2475
2476 relocatable = (output_bfd != NULL);
2477
07515404 2478 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2479 return bfd_reloc_outofrange;
2480
2481 /* Build up the field adjustment in VAL. */
2482 val = 0;
2483 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2484 {
2485 /* Either we're calculating the final field value or we have a
2486 relocation against a section symbol. Add in the section's
2487 offset or address. */
2488 val += symbol->section->output_section->vma;
2489 val += symbol->section->output_offset;
2490 }
2491
2492 if (!relocatable)
2493 {
2494 /* We're calculating the final field value. Add in the symbol's value
2495 and, if pc-relative, subtract the address of the field itself. */
2496 val += symbol->value;
2497 if (reloc_entry->howto->pc_relative)
2498 {
2499 val -= input_section->output_section->vma;
2500 val -= input_section->output_offset;
2501 val -= reloc_entry->address;
2502 }
2503 }
2504
2505 /* VAL is now the final adjustment. If we're keeping this relocation
2506 in the output file, and if the relocation uses a separate addend,
2507 we just need to add VAL to that addend. Otherwise we need to add
2508 VAL to the relocation field itself. */
2509 if (relocatable && !reloc_entry->howto->partial_inplace)
2510 reloc_entry->addend += val;
2511 else
2512 {
d6f16593
MR
2513 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2514
30ac9238
RS
2515 /* Add in the separate addend, if any. */
2516 val += reloc_entry->addend;
2517
2518 /* Add VAL to the relocation field. */
df58fc94
RS
2519 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2520 location);
30ac9238 2521 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593 2522 location);
df58fc94
RS
2523 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2524 location);
d6f16593 2525
30ac9238
RS
2526 if (status != bfd_reloc_ok)
2527 return status;
2528 }
2529
2530 if (relocatable)
2531 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2532
2533 return bfd_reloc_ok;
2534}
2535\f
2536/* Swap an entry in a .gptab section. Note that these routines rely
2537 on the equivalence of the two elements of the union. */
2538
2539static void
9719ad41
RS
2540bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2541 Elf32_gptab *in)
b49e97c9
TS
2542{
2543 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2544 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2545}
2546
2547static void
9719ad41
RS
2548bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2549 Elf32_External_gptab *ex)
b49e97c9
TS
2550{
2551 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2552 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2553}
2554
2555static void
9719ad41
RS
2556bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2557 Elf32_External_compact_rel *ex)
b49e97c9
TS
2558{
2559 H_PUT_32 (abfd, in->id1, ex->id1);
2560 H_PUT_32 (abfd, in->num, ex->num);
2561 H_PUT_32 (abfd, in->id2, ex->id2);
2562 H_PUT_32 (abfd, in->offset, ex->offset);
2563 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2564 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2565}
2566
2567static void
9719ad41
RS
2568bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2569 Elf32_External_crinfo *ex)
b49e97c9
TS
2570{
2571 unsigned long l;
2572
2573 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2574 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2575 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2576 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2577 H_PUT_32 (abfd, l, ex->info);
2578 H_PUT_32 (abfd, in->konst, ex->konst);
2579 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2580}
b49e97c9
TS
2581\f
2582/* A .reginfo section holds a single Elf32_RegInfo structure. These
2583 routines swap this structure in and out. They are used outside of
2584 BFD, so they are globally visible. */
2585
2586void
9719ad41
RS
2587bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2588 Elf32_RegInfo *in)
b49e97c9
TS
2589{
2590 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2591 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2592 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2593 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2594 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2595 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2596}
2597
2598void
9719ad41
RS
2599bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2600 Elf32_External_RegInfo *ex)
b49e97c9
TS
2601{
2602 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2603 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2604 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2605 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2606 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2607 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2608}
2609
2610/* In the 64 bit ABI, the .MIPS.options section holds register
2611 information in an Elf64_Reginfo structure. These routines swap
2612 them in and out. They are globally visible because they are used
2613 outside of BFD. These routines are here so that gas can call them
2614 without worrying about whether the 64 bit ABI has been included. */
2615
2616void
9719ad41
RS
2617bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2618 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2619{
2620 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2621 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2622 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2623 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2624 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2625 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2626 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2627}
2628
2629void
9719ad41
RS
2630bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2631 Elf64_External_RegInfo *ex)
b49e97c9
TS
2632{
2633 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2634 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2635 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2636 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2637 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2638 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2639 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2640}
2641
2642/* Swap in an options header. */
2643
2644void
9719ad41
RS
2645bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2646 Elf_Internal_Options *in)
b49e97c9
TS
2647{
2648 in->kind = H_GET_8 (abfd, ex->kind);
2649 in->size = H_GET_8 (abfd, ex->size);
2650 in->section = H_GET_16 (abfd, ex->section);
2651 in->info = H_GET_32 (abfd, ex->info);
2652}
2653
2654/* Swap out an options header. */
2655
2656void
9719ad41
RS
2657bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2658 Elf_External_Options *ex)
b49e97c9
TS
2659{
2660 H_PUT_8 (abfd, in->kind, ex->kind);
2661 H_PUT_8 (abfd, in->size, ex->size);
2662 H_PUT_16 (abfd, in->section, ex->section);
2663 H_PUT_32 (abfd, in->info, ex->info);
2664}
2665\f
2666/* This function is called via qsort() to sort the dynamic relocation
2667 entries by increasing r_symndx value. */
2668
2669static int
9719ad41 2670sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2671{
947216bf
AM
2672 Elf_Internal_Rela int_reloc1;
2673 Elf_Internal_Rela int_reloc2;
6870500c 2674 int diff;
b49e97c9 2675
947216bf
AM
2676 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2677 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2678
6870500c
RS
2679 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2680 if (diff != 0)
2681 return diff;
2682
2683 if (int_reloc1.r_offset < int_reloc2.r_offset)
2684 return -1;
2685 if (int_reloc1.r_offset > int_reloc2.r_offset)
2686 return 1;
2687 return 0;
b49e97c9
TS
2688}
2689
f4416af6
AO
2690/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2691
2692static int
7e3102a7
AM
2693sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2694 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2695{
7e3102a7 2696#ifdef BFD64
f4416af6
AO
2697 Elf_Internal_Rela int_reloc1[3];
2698 Elf_Internal_Rela int_reloc2[3];
2699
2700 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2701 (reldyn_sorting_bfd, arg1, int_reloc1);
2702 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2703 (reldyn_sorting_bfd, arg2, int_reloc2);
2704
6870500c
RS
2705 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2706 return -1;
2707 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2708 return 1;
2709
2710 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2711 return -1;
2712 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2713 return 1;
2714 return 0;
7e3102a7
AM
2715#else
2716 abort ();
2717#endif
f4416af6
AO
2718}
2719
2720
b49e97c9
TS
2721/* This routine is used to write out ECOFF debugging external symbol
2722 information. It is called via mips_elf_link_hash_traverse. The
2723 ECOFF external symbol information must match the ELF external
2724 symbol information. Unfortunately, at this point we don't know
2725 whether a symbol is required by reloc information, so the two
2726 tables may wind up being different. We must sort out the external
2727 symbol information before we can set the final size of the .mdebug
2728 section, and we must set the size of the .mdebug section before we
2729 can relocate any sections, and we can't know which symbols are
2730 required by relocation until we relocate the sections.
2731 Fortunately, it is relatively unlikely that any symbol will be
2732 stripped but required by a reloc. In particular, it can not happen
2733 when generating a final executable. */
2734
b34976b6 2735static bfd_boolean
9719ad41 2736mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2737{
9719ad41 2738 struct extsym_info *einfo = data;
b34976b6 2739 bfd_boolean strip;
b49e97c9
TS
2740 asection *sec, *output_section;
2741
b49e97c9 2742 if (h->root.indx == -2)
b34976b6 2743 strip = FALSE;
f5385ebf 2744 else if ((h->root.def_dynamic
77cfaee6
AM
2745 || h->root.ref_dynamic
2746 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2747 && !h->root.def_regular
2748 && !h->root.ref_regular)
b34976b6 2749 strip = TRUE;
b49e97c9
TS
2750 else if (einfo->info->strip == strip_all
2751 || (einfo->info->strip == strip_some
2752 && bfd_hash_lookup (einfo->info->keep_hash,
2753 h->root.root.root.string,
b34976b6
AM
2754 FALSE, FALSE) == NULL))
2755 strip = TRUE;
b49e97c9 2756 else
b34976b6 2757 strip = FALSE;
b49e97c9
TS
2758
2759 if (strip)
b34976b6 2760 return TRUE;
b49e97c9
TS
2761
2762 if (h->esym.ifd == -2)
2763 {
2764 h->esym.jmptbl = 0;
2765 h->esym.cobol_main = 0;
2766 h->esym.weakext = 0;
2767 h->esym.reserved = 0;
2768 h->esym.ifd = ifdNil;
2769 h->esym.asym.value = 0;
2770 h->esym.asym.st = stGlobal;
2771
2772 if (h->root.root.type == bfd_link_hash_undefined
2773 || h->root.root.type == bfd_link_hash_undefweak)
2774 {
2775 const char *name;
2776
2777 /* Use undefined class. Also, set class and type for some
2778 special symbols. */
2779 name = h->root.root.root.string;
2780 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2781 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2782 {
2783 h->esym.asym.sc = scData;
2784 h->esym.asym.st = stLabel;
2785 h->esym.asym.value = 0;
2786 }
2787 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2788 {
2789 h->esym.asym.sc = scAbs;
2790 h->esym.asym.st = stLabel;
2791 h->esym.asym.value =
2792 mips_elf_hash_table (einfo->info)->procedure_count;
2793 }
4a14403c 2794 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2795 {
2796 h->esym.asym.sc = scAbs;
2797 h->esym.asym.st = stLabel;
2798 h->esym.asym.value = elf_gp (einfo->abfd);
2799 }
2800 else
2801 h->esym.asym.sc = scUndefined;
2802 }
2803 else if (h->root.root.type != bfd_link_hash_defined
2804 && h->root.root.type != bfd_link_hash_defweak)
2805 h->esym.asym.sc = scAbs;
2806 else
2807 {
2808 const char *name;
2809
2810 sec = h->root.root.u.def.section;
2811 output_section = sec->output_section;
2812
2813 /* When making a shared library and symbol h is the one from
2814 the another shared library, OUTPUT_SECTION may be null. */
2815 if (output_section == NULL)
2816 h->esym.asym.sc = scUndefined;
2817 else
2818 {
2819 name = bfd_section_name (output_section->owner, output_section);
2820
2821 if (strcmp (name, ".text") == 0)
2822 h->esym.asym.sc = scText;
2823 else if (strcmp (name, ".data") == 0)
2824 h->esym.asym.sc = scData;
2825 else if (strcmp (name, ".sdata") == 0)
2826 h->esym.asym.sc = scSData;
2827 else if (strcmp (name, ".rodata") == 0
2828 || strcmp (name, ".rdata") == 0)
2829 h->esym.asym.sc = scRData;
2830 else if (strcmp (name, ".bss") == 0)
2831 h->esym.asym.sc = scBss;
2832 else if (strcmp (name, ".sbss") == 0)
2833 h->esym.asym.sc = scSBss;
2834 else if (strcmp (name, ".init") == 0)
2835 h->esym.asym.sc = scInit;
2836 else if (strcmp (name, ".fini") == 0)
2837 h->esym.asym.sc = scFini;
2838 else
2839 h->esym.asym.sc = scAbs;
2840 }
2841 }
2842
2843 h->esym.asym.reserved = 0;
2844 h->esym.asym.index = indexNil;
2845 }
2846
2847 if (h->root.root.type == bfd_link_hash_common)
2848 h->esym.asym.value = h->root.root.u.c.size;
2849 else if (h->root.root.type == bfd_link_hash_defined
2850 || h->root.root.type == bfd_link_hash_defweak)
2851 {
2852 if (h->esym.asym.sc == scCommon)
2853 h->esym.asym.sc = scBss;
2854 else if (h->esym.asym.sc == scSCommon)
2855 h->esym.asym.sc = scSBss;
2856
2857 sec = h->root.root.u.def.section;
2858 output_section = sec->output_section;
2859 if (output_section != NULL)
2860 h->esym.asym.value = (h->root.root.u.def.value
2861 + sec->output_offset
2862 + output_section->vma);
2863 else
2864 h->esym.asym.value = 0;
2865 }
33bb52fb 2866 else
b49e97c9
TS
2867 {
2868 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2869
2870 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2871 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2872
33bb52fb 2873 if (hd->needs_lazy_stub)
b49e97c9 2874 {
1bbce132
MR
2875 BFD_ASSERT (hd->root.plt.plist != NULL);
2876 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
b49e97c9
TS
2877 /* Set type and value for a symbol with a function stub. */
2878 h->esym.asym.st = stProc;
2879 sec = hd->root.root.u.def.section;
2880 if (sec == NULL)
2881 h->esym.asym.value = 0;
2882 else
2883 {
2884 output_section = sec->output_section;
2885 if (output_section != NULL)
1bbce132 2886 h->esym.asym.value = (hd->root.plt.plist->stub_offset
b49e97c9
TS
2887 + sec->output_offset
2888 + output_section->vma);
2889 else
2890 h->esym.asym.value = 0;
2891 }
b49e97c9
TS
2892 }
2893 }
2894
2895 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2896 h->root.root.root.string,
2897 &h->esym))
2898 {
b34976b6
AM
2899 einfo->failed = TRUE;
2900 return FALSE;
b49e97c9
TS
2901 }
2902
b34976b6 2903 return TRUE;
b49e97c9
TS
2904}
2905
2906/* A comparison routine used to sort .gptab entries. */
2907
2908static int
9719ad41 2909gptab_compare (const void *p1, const void *p2)
b49e97c9 2910{
9719ad41
RS
2911 const Elf32_gptab *a1 = p1;
2912 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2913
2914 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2915}
2916\f
b15e6682 2917/* Functions to manage the got entry hash table. */
f4416af6
AO
2918
2919/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2920 hash number. */
2921
2922static INLINE hashval_t
9719ad41 2923mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2924{
2925#ifdef BFD64
2926 return addr + (addr >> 32);
2927#else
2928 return addr;
2929#endif
2930}
2931
f4416af6 2932static hashval_t
d9bf376d 2933mips_elf_got_entry_hash (const void *entry_)
f4416af6
AO
2934{
2935 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2936
e641e783 2937 return (entry->symndx
9ab066b4
RS
2938 + ((entry->tls_type == GOT_TLS_LDM) << 18)
2939 + (entry->tls_type == GOT_TLS_LDM ? 0
e641e783
RS
2940 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2941 : entry->symndx >= 0 ? (entry->abfd->id
2942 + mips_elf_hash_bfd_vma (entry->d.addend))
2943 : entry->d.h->root.root.root.hash));
f4416af6
AO
2944}
2945
2946static int
3dff0dd1 2947mips_elf_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2948{
2949 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2950 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2951
e641e783 2952 return (e1->symndx == e2->symndx
9ab066b4
RS
2953 && e1->tls_type == e2->tls_type
2954 && (e1->tls_type == GOT_TLS_LDM ? TRUE
e641e783
RS
2955 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
2956 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
2957 && e1->d.addend == e2->d.addend)
2958 : e2->abfd && e1->d.h == e2->d.h));
b15e6682 2959}
c224138d 2960
13db6b44
RS
2961static hashval_t
2962mips_got_page_ref_hash (const void *ref_)
2963{
2964 const struct mips_got_page_ref *ref;
2965
2966 ref = (const struct mips_got_page_ref *) ref_;
2967 return ((ref->symndx >= 0
2968 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
2969 : ref->u.h->root.root.root.hash)
2970 + mips_elf_hash_bfd_vma (ref->addend));
2971}
2972
2973static int
2974mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
2975{
2976 const struct mips_got_page_ref *ref1, *ref2;
2977
2978 ref1 = (const struct mips_got_page_ref *) ref1_;
2979 ref2 = (const struct mips_got_page_ref *) ref2_;
2980 return (ref1->symndx == ref2->symndx
2981 && (ref1->symndx < 0
2982 ? ref1->u.h == ref2->u.h
2983 : ref1->u.abfd == ref2->u.abfd)
2984 && ref1->addend == ref2->addend);
2985}
2986
c224138d
RS
2987static hashval_t
2988mips_got_page_entry_hash (const void *entry_)
2989{
2990 const struct mips_got_page_entry *entry;
2991
2992 entry = (const struct mips_got_page_entry *) entry_;
13db6b44 2993 return entry->sec->id;
c224138d
RS
2994}
2995
2996static int
2997mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2998{
2999 const struct mips_got_page_entry *entry1, *entry2;
3000
3001 entry1 = (const struct mips_got_page_entry *) entry1_;
3002 entry2 = (const struct mips_got_page_entry *) entry2_;
13db6b44 3003 return entry1->sec == entry2->sec;
c224138d 3004}
b15e6682 3005\f
3dff0dd1 3006/* Create and return a new mips_got_info structure. */
5334aa52
RS
3007
3008static struct mips_got_info *
3dff0dd1 3009mips_elf_create_got_info (bfd *abfd)
5334aa52
RS
3010{
3011 struct mips_got_info *g;
3012
3013 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3014 if (g == NULL)
3015 return NULL;
3016
3dff0dd1
RS
3017 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3018 mips_elf_got_entry_eq, NULL);
5334aa52
RS
3019 if (g->got_entries == NULL)
3020 return NULL;
3021
13db6b44
RS
3022 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3023 mips_got_page_ref_eq, NULL);
3024 if (g->got_page_refs == NULL)
5334aa52
RS
3025 return NULL;
3026
3027 return g;
3028}
3029
ee227692
RS
3030/* Return the GOT info for input bfd ABFD, trying to create a new one if
3031 CREATE_P and if ABFD doesn't already have a GOT. */
3032
3033static struct mips_got_info *
3034mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3035{
3036 struct mips_elf_obj_tdata *tdata;
3037
3038 if (!is_mips_elf (abfd))
3039 return NULL;
3040
3041 tdata = mips_elf_tdata (abfd);
3042 if (!tdata->got && create_p)
3dff0dd1 3043 tdata->got = mips_elf_create_got_info (abfd);
ee227692
RS
3044 return tdata->got;
3045}
3046
d7206569
RS
3047/* Record that ABFD should use output GOT G. */
3048
3049static void
3050mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3051{
3052 struct mips_elf_obj_tdata *tdata;
3053
3054 BFD_ASSERT (is_mips_elf (abfd));
3055 tdata = mips_elf_tdata (abfd);
3056 if (tdata->got)
3057 {
3058 /* The GOT structure itself and the hash table entries are
3059 allocated to a bfd, but the hash tables aren't. */
3060 htab_delete (tdata->got->got_entries);
13db6b44
RS
3061 htab_delete (tdata->got->got_page_refs);
3062 if (tdata->got->got_page_entries)
3063 htab_delete (tdata->got->got_page_entries);
d7206569
RS
3064 }
3065 tdata->got = g;
3066}
3067
0a44bf69
RS
3068/* Return the dynamic relocation section. If it doesn't exist, try to
3069 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3070 if creation fails. */
f4416af6
AO
3071
3072static asection *
0a44bf69 3073mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 3074{
0a44bf69 3075 const char *dname;
f4416af6 3076 asection *sreloc;
0a44bf69 3077 bfd *dynobj;
f4416af6 3078
0a44bf69
RS
3079 dname = MIPS_ELF_REL_DYN_NAME (info);
3080 dynobj = elf_hash_table (info)->dynobj;
3d4d4302 3081 sreloc = bfd_get_linker_section (dynobj, dname);
f4416af6
AO
3082 if (sreloc == NULL && create_p)
3083 {
3d4d4302
AM
3084 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3085 (SEC_ALLOC
3086 | SEC_LOAD
3087 | SEC_HAS_CONTENTS
3088 | SEC_IN_MEMORY
3089 | SEC_LINKER_CREATED
3090 | SEC_READONLY));
f4416af6 3091 if (sreloc == NULL
f4416af6 3092 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 3093 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
3094 return NULL;
3095 }
3096 return sreloc;
3097}
3098
e641e783
RS
3099/* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3100
3101static int
3102mips_elf_reloc_tls_type (unsigned int r_type)
3103{
3104 if (tls_gd_reloc_p (r_type))
3105 return GOT_TLS_GD;
3106
3107 if (tls_ldm_reloc_p (r_type))
3108 return GOT_TLS_LDM;
3109
3110 if (tls_gottprel_reloc_p (r_type))
3111 return GOT_TLS_IE;
3112
9ab066b4 3113 return GOT_TLS_NONE;
e641e783
RS
3114}
3115
3116/* Return the number of GOT slots needed for GOT TLS type TYPE. */
3117
3118static int
3119mips_tls_got_entries (unsigned int type)
3120{
3121 switch (type)
3122 {
3123 case GOT_TLS_GD:
3124 case GOT_TLS_LDM:
3125 return 2;
3126
3127 case GOT_TLS_IE:
3128 return 1;
3129
9ab066b4 3130 case GOT_TLS_NONE:
e641e783
RS
3131 return 0;
3132 }
3133 abort ();
3134}
3135
0f20cc35
DJ
3136/* Count the number of relocations needed for a TLS GOT entry, with
3137 access types from TLS_TYPE, and symbol H (or a local symbol if H
3138 is NULL). */
3139
3140static int
3141mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3142 struct elf_link_hash_entry *h)
3143{
3144 int indx = 0;
0f20cc35
DJ
3145 bfd_boolean need_relocs = FALSE;
3146 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3147
3148 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3149 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
3150 indx = h->dynindx;
3151
3152 if ((info->shared || indx != 0)
3153 && (h == NULL
3154 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3155 || h->root.type != bfd_link_hash_undefweak))
3156 need_relocs = TRUE;
3157
3158 if (!need_relocs)
e641e783 3159 return 0;
0f20cc35 3160
9ab066b4 3161 switch (tls_type)
0f20cc35 3162 {
e641e783
RS
3163 case GOT_TLS_GD:
3164 return indx != 0 ? 2 : 1;
0f20cc35 3165
e641e783
RS
3166 case GOT_TLS_IE:
3167 return 1;
0f20cc35 3168
e641e783
RS
3169 case GOT_TLS_LDM:
3170 return info->shared ? 1 : 0;
0f20cc35 3171
e641e783
RS
3172 default:
3173 return 0;
3174 }
0f20cc35
DJ
3175}
3176
ab361d49
RS
3177/* Add the number of GOT entries and TLS relocations required by ENTRY
3178 to G. */
0f20cc35 3179
ab361d49
RS
3180static void
3181mips_elf_count_got_entry (struct bfd_link_info *info,
3182 struct mips_got_info *g,
3183 struct mips_got_entry *entry)
0f20cc35 3184{
9ab066b4 3185 if (entry->tls_type)
ab361d49 3186 {
9ab066b4
RS
3187 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3188 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
ab361d49
RS
3189 entry->symndx < 0
3190 ? &entry->d.h->root : NULL);
3191 }
3192 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3193 g->local_gotno += 1;
3194 else
3195 g->global_gotno += 1;
0f20cc35
DJ
3196}
3197
0f20cc35
DJ
3198/* Output a simple dynamic relocation into SRELOC. */
3199
3200static void
3201mips_elf_output_dynamic_relocation (bfd *output_bfd,
3202 asection *sreloc,
861fb55a 3203 unsigned long reloc_index,
0f20cc35
DJ
3204 unsigned long indx,
3205 int r_type,
3206 bfd_vma offset)
3207{
3208 Elf_Internal_Rela rel[3];
3209
3210 memset (rel, 0, sizeof (rel));
3211
3212 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3213 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3214
3215 if (ABI_64_P (output_bfd))
3216 {
3217 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3218 (output_bfd, &rel[0],
3219 (sreloc->contents
861fb55a 3220 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
3221 }
3222 else
3223 bfd_elf32_swap_reloc_out
3224 (output_bfd, &rel[0],
3225 (sreloc->contents
861fb55a 3226 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
3227}
3228
3229/* Initialize a set of TLS GOT entries for one symbol. */
3230
3231static void
9ab066b4
RS
3232mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3233 struct mips_got_entry *entry,
0f20cc35
DJ
3234 struct mips_elf_link_hash_entry *h,
3235 bfd_vma value)
3236{
23cc69b6 3237 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
3238 int indx;
3239 asection *sreloc, *sgot;
9ab066b4 3240 bfd_vma got_offset, got_offset2;
0f20cc35
DJ
3241 bfd_boolean need_relocs = FALSE;
3242
23cc69b6 3243 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3244 if (htab == NULL)
3245 return;
3246
23cc69b6 3247 sgot = htab->sgot;
0f20cc35
DJ
3248
3249 indx = 0;
3250 if (h != NULL)
3251 {
3252 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3253
3254 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3255 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3256 indx = h->root.dynindx;
3257 }
3258
9ab066b4 3259 if (entry->tls_initialized)
0f20cc35
DJ
3260 return;
3261
3262 if ((info->shared || indx != 0)
3263 && (h == NULL
3264 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3265 || h->root.type != bfd_link_hash_undefweak))
3266 need_relocs = TRUE;
3267
3268 /* MINUS_ONE means the symbol is not defined in this object. It may not
3269 be defined at all; assume that the value doesn't matter in that
3270 case. Otherwise complain if we would use the value. */
3271 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3272 || h->root.root.type == bfd_link_hash_undefweak);
3273
3274 /* Emit necessary relocations. */
0a44bf69 3275 sreloc = mips_elf_rel_dyn_section (info, FALSE);
9ab066b4 3276 got_offset = entry->gotidx;
0f20cc35 3277
9ab066b4 3278 switch (entry->tls_type)
0f20cc35 3279 {
e641e783
RS
3280 case GOT_TLS_GD:
3281 /* General Dynamic. */
3282 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
0f20cc35
DJ
3283
3284 if (need_relocs)
3285 {
3286 mips_elf_output_dynamic_relocation
861fb55a 3287 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3288 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
e641e783 3289 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3290
3291 if (indx)
3292 mips_elf_output_dynamic_relocation
861fb55a 3293 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3294 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
e641e783 3295 sgot->output_offset + sgot->output_section->vma + got_offset2);
0f20cc35
DJ
3296 else
3297 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3298 sgot->contents + got_offset2);
0f20cc35
DJ
3299 }
3300 else
3301 {
3302 MIPS_ELF_PUT_WORD (abfd, 1,
e641e783 3303 sgot->contents + got_offset);
0f20cc35 3304 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3305 sgot->contents + got_offset2);
0f20cc35 3306 }
e641e783 3307 break;
0f20cc35 3308
e641e783
RS
3309 case GOT_TLS_IE:
3310 /* Initial Exec model. */
0f20cc35
DJ
3311 if (need_relocs)
3312 {
3313 if (indx == 0)
3314 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
e641e783 3315 sgot->contents + got_offset);
0f20cc35
DJ
3316 else
3317 MIPS_ELF_PUT_WORD (abfd, 0,
e641e783 3318 sgot->contents + got_offset);
0f20cc35
DJ
3319
3320 mips_elf_output_dynamic_relocation
861fb55a 3321 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3322 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
e641e783 3323 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3324 }
3325 else
3326 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
e641e783
RS
3327 sgot->contents + got_offset);
3328 break;
0f20cc35 3329
e641e783 3330 case GOT_TLS_LDM:
0f20cc35
DJ
3331 /* The initial offset is zero, and the LD offsets will include the
3332 bias by DTP_OFFSET. */
3333 MIPS_ELF_PUT_WORD (abfd, 0,
3334 sgot->contents + got_offset
3335 + MIPS_ELF_GOT_SIZE (abfd));
3336
3337 if (!info->shared)
3338 MIPS_ELF_PUT_WORD (abfd, 1,
3339 sgot->contents + got_offset);
3340 else
3341 mips_elf_output_dynamic_relocation
861fb55a 3342 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3343 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3344 sgot->output_offset + sgot->output_section->vma + got_offset);
e641e783
RS
3345 break;
3346
3347 default:
3348 abort ();
0f20cc35
DJ
3349 }
3350
9ab066b4 3351 entry->tls_initialized = TRUE;
e641e783 3352}
0f20cc35 3353
0a44bf69
RS
3354/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3355 for global symbol H. .got.plt comes before the GOT, so the offset
3356 will be negative. */
3357
3358static bfd_vma
3359mips_elf_gotplt_index (struct bfd_link_info *info,
3360 struct elf_link_hash_entry *h)
3361{
1bbce132 3362 bfd_vma got_address, got_value;
0a44bf69
RS
3363 struct mips_elf_link_hash_table *htab;
3364
3365 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3366 BFD_ASSERT (htab != NULL);
3367
1bbce132
MR
3368 BFD_ASSERT (h->plt.plist != NULL);
3369 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
0a44bf69
RS
3370
3371 /* Calculate the address of the associated .got.plt entry. */
3372 got_address = (htab->sgotplt->output_section->vma
3373 + htab->sgotplt->output_offset
1bbce132
MR
3374 + (h->plt.plist->gotplt_index
3375 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
0a44bf69
RS
3376
3377 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3378 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3379 + htab->root.hgot->root.u.def.section->output_offset
3380 + htab->root.hgot->root.u.def.value);
3381
3382 return got_address - got_value;
3383}
3384
5c18022e 3385/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3386 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3387 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3388 offset can be found. */
b49e97c9
TS
3389
3390static bfd_vma
9719ad41 3391mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3392 bfd_vma value, unsigned long r_symndx,
0f20cc35 3393 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3394{
a8028dd0 3395 struct mips_elf_link_hash_table *htab;
b15e6682 3396 struct mips_got_entry *entry;
b49e97c9 3397
a8028dd0 3398 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3399 BFD_ASSERT (htab != NULL);
3400
a8028dd0
RS
3401 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3402 r_symndx, h, r_type);
0f20cc35 3403 if (!entry)
b15e6682 3404 return MINUS_ONE;
0f20cc35 3405
e641e783 3406 if (entry->tls_type)
9ab066b4
RS
3407 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3408 return entry->gotidx;
b49e97c9
TS
3409}
3410
13fbec83 3411/* Return the GOT index of global symbol H in the primary GOT. */
b49e97c9
TS
3412
3413static bfd_vma
13fbec83
RS
3414mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3415 struct elf_link_hash_entry *h)
3416{
3417 struct mips_elf_link_hash_table *htab;
3418 long global_got_dynindx;
3419 struct mips_got_info *g;
3420 bfd_vma got_index;
3421
3422 htab = mips_elf_hash_table (info);
3423 BFD_ASSERT (htab != NULL);
3424
3425 global_got_dynindx = 0;
3426 if (htab->global_gotsym != NULL)
3427 global_got_dynindx = htab->global_gotsym->dynindx;
3428
3429 /* Once we determine the global GOT entry with the lowest dynamic
3430 symbol table index, we must put all dynamic symbols with greater
3431 indices into the primary GOT. That makes it easy to calculate the
3432 GOT offset. */
3433 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3434 g = mips_elf_bfd_got (obfd, FALSE);
3435 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3436 * MIPS_ELF_GOT_SIZE (obfd));
3437 BFD_ASSERT (got_index < htab->sgot->size);
3438
3439 return got_index;
3440}
3441
3442/* Return the GOT index for the global symbol indicated by H, which is
3443 referenced by a relocation of type R_TYPE in IBFD. */
3444
3445static bfd_vma
3446mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3447 struct elf_link_hash_entry *h, int r_type)
b49e97c9 3448{
a8028dd0 3449 struct mips_elf_link_hash_table *htab;
6c42ddb9
RS
3450 struct mips_got_info *g;
3451 struct mips_got_entry lookup, *entry;
3452 bfd_vma gotidx;
b49e97c9 3453
a8028dd0 3454 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3455 BFD_ASSERT (htab != NULL);
3456
6c42ddb9
RS
3457 g = mips_elf_bfd_got (ibfd, FALSE);
3458 BFD_ASSERT (g);
f4416af6 3459
6c42ddb9
RS
3460 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3461 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3462 return mips_elf_primary_global_got_index (obfd, info, h);
f4416af6 3463
6c42ddb9
RS
3464 lookup.abfd = ibfd;
3465 lookup.symndx = -1;
3466 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3467 entry = htab_find (g->got_entries, &lookup);
3468 BFD_ASSERT (entry);
0f20cc35 3469
6c42ddb9
RS
3470 gotidx = entry->gotidx;
3471 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
f4416af6 3472
6c42ddb9 3473 if (lookup.tls_type)
0f20cc35 3474 {
0f20cc35
DJ
3475 bfd_vma value = MINUS_ONE;
3476
3477 if ((h->root.type == bfd_link_hash_defined
3478 || h->root.type == bfd_link_hash_defweak)
3479 && h->root.u.def.section->output_section)
3480 value = (h->root.u.def.value
3481 + h->root.u.def.section->output_offset
3482 + h->root.u.def.section->output_section->vma);
3483
9ab066b4 3484 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
0f20cc35 3485 }
6c42ddb9 3486 return gotidx;
b49e97c9
TS
3487}
3488
5c18022e
RS
3489/* Find a GOT page entry that points to within 32KB of VALUE. These
3490 entries are supposed to be placed at small offsets in the GOT, i.e.,
3491 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3492 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3493 offset of the GOT entry from VALUE. */
b49e97c9
TS
3494
3495static bfd_vma
9719ad41 3496mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3497 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3498{
91d6fa6a 3499 bfd_vma page, got_index;
b15e6682 3500 struct mips_got_entry *entry;
b49e97c9 3501
0a44bf69 3502 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3503 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3504 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3505
b15e6682
AO
3506 if (!entry)
3507 return MINUS_ONE;
143d77c5 3508
91d6fa6a 3509 got_index = entry->gotidx;
b49e97c9
TS
3510
3511 if (offsetp)
f4416af6 3512 *offsetp = value - entry->d.address;
b49e97c9 3513
91d6fa6a 3514 return got_index;
b49e97c9
TS
3515}
3516
738e5348 3517/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3518 EXTERNAL is true if the relocation was originally against a global
3519 symbol that binds locally. */
b49e97c9
TS
3520
3521static bfd_vma
9719ad41 3522mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3523 bfd_vma value, bfd_boolean external)
b49e97c9 3524{
b15e6682 3525 struct mips_got_entry *entry;
b49e97c9 3526
0a44bf69
RS
3527 /* GOT16 relocations against local symbols are followed by a LO16
3528 relocation; those against global symbols are not. Thus if the
3529 symbol was originally local, the GOT16 relocation should load the
3530 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3531 if (! external)
0a44bf69 3532 value = mips_elf_high (value) << 16;
b49e97c9 3533
738e5348
RS
3534 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3535 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3536 same in all cases. */
a8028dd0
RS
3537 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3538 NULL, R_MIPS_GOT16);
b15e6682
AO
3539 if (entry)
3540 return entry->gotidx;
3541 else
3542 return MINUS_ONE;
b49e97c9
TS
3543}
3544
3545/* Returns the offset for the entry at the INDEXth position
3546 in the GOT. */
3547
3548static bfd_vma
a8028dd0 3549mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3550 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3551{
a8028dd0 3552 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3553 asection *sgot;
3554 bfd_vma gp;
3555
a8028dd0 3556 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3557 BFD_ASSERT (htab != NULL);
3558
a8028dd0 3559 sgot = htab->sgot;
f4416af6 3560 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3561 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3562
91d6fa6a 3563 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3564}
3565
0a44bf69
RS
3566/* Create and return a local GOT entry for VALUE, which was calculated
3567 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3568 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3569 instead. */
b49e97c9 3570
b15e6682 3571static struct mips_got_entry *
0a44bf69 3572mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3573 bfd *ibfd, bfd_vma value,
5c18022e 3574 unsigned long r_symndx,
0f20cc35
DJ
3575 struct mips_elf_link_hash_entry *h,
3576 int r_type)
b49e97c9 3577{
ebc53538
RS
3578 struct mips_got_entry lookup, *entry;
3579 void **loc;
f4416af6 3580 struct mips_got_info *g;
0a44bf69 3581 struct mips_elf_link_hash_table *htab;
6c42ddb9 3582 bfd_vma gotidx;
0a44bf69
RS
3583
3584 htab = mips_elf_hash_table (info);
4dfe6ac6 3585 BFD_ASSERT (htab != NULL);
b15e6682 3586
d7206569 3587 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
3588 if (g == NULL)
3589 {
d7206569 3590 g = mips_elf_bfd_got (abfd, FALSE);
f4416af6
AO
3591 BFD_ASSERT (g != NULL);
3592 }
b15e6682 3593
020d7251
RS
3594 /* This function shouldn't be called for symbols that live in the global
3595 area of the GOT. */
3596 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35 3597
ebc53538
RS
3598 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3599 if (lookup.tls_type)
3600 {
3601 lookup.abfd = ibfd;
df58fc94 3602 if (tls_ldm_reloc_p (r_type))
0f20cc35 3603 {
ebc53538
RS
3604 lookup.symndx = 0;
3605 lookup.d.addend = 0;
0f20cc35
DJ
3606 }
3607 else if (h == NULL)
3608 {
ebc53538
RS
3609 lookup.symndx = r_symndx;
3610 lookup.d.addend = 0;
0f20cc35
DJ
3611 }
3612 else
ebc53538
RS
3613 {
3614 lookup.symndx = -1;
3615 lookup.d.h = h;
3616 }
0f20cc35 3617
ebc53538
RS
3618 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3619 BFD_ASSERT (entry);
0f20cc35 3620
6c42ddb9
RS
3621 gotidx = entry->gotidx;
3622 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3623
ebc53538 3624 return entry;
0f20cc35
DJ
3625 }
3626
ebc53538
RS
3627 lookup.abfd = NULL;
3628 lookup.symndx = -1;
3629 lookup.d.address = value;
3630 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3631 if (!loc)
b15e6682 3632 return NULL;
143d77c5 3633
ebc53538
RS
3634 entry = (struct mips_got_entry *) *loc;
3635 if (entry)
3636 return entry;
b15e6682 3637
ebc53538 3638 if (g->assigned_gotno >= g->local_gotno)
b49e97c9
TS
3639 {
3640 /* We didn't allocate enough space in the GOT. */
3641 (*_bfd_error_handler)
3642 (_("not enough GOT space for local GOT entries"));
3643 bfd_set_error (bfd_error_bad_value);
b15e6682 3644 return NULL;
b49e97c9
TS
3645 }
3646
ebc53538
RS
3647 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3648 if (!entry)
3649 return NULL;
3650
3651 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3652 *entry = lookup;
3653 *loc = entry;
3654
3655 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
b15e6682 3656
5c18022e 3657 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3658 if (htab->is_vxworks)
3659 {
3660 Elf_Internal_Rela outrel;
5c18022e 3661 asection *s;
91d6fa6a 3662 bfd_byte *rloc;
0a44bf69 3663 bfd_vma got_address;
0a44bf69
RS
3664
3665 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3666 got_address = (htab->sgot->output_section->vma
3667 + htab->sgot->output_offset
ebc53538 3668 + entry->gotidx);
0a44bf69 3669
91d6fa6a 3670 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3671 outrel.r_offset = got_address;
5c18022e
RS
3672 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3673 outrel.r_addend = value;
91d6fa6a 3674 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3675 }
3676
ebc53538 3677 return entry;
b49e97c9
TS
3678}
3679
d4596a51
RS
3680/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3681 The number might be exact or a worst-case estimate, depending on how
3682 much information is available to elf_backend_omit_section_dynsym at
3683 the current linking stage. */
3684
3685static bfd_size_type
3686count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3687{
3688 bfd_size_type count;
3689
3690 count = 0;
3691 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3692 {
3693 asection *p;
3694 const struct elf_backend_data *bed;
3695
3696 bed = get_elf_backend_data (output_bfd);
3697 for (p = output_bfd->sections; p ; p = p->next)
3698 if ((p->flags & SEC_EXCLUDE) == 0
3699 && (p->flags & SEC_ALLOC) != 0
3700 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3701 ++count;
3702 }
3703 return count;
3704}
3705
b49e97c9 3706/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3707 appear towards the end. */
b49e97c9 3708
b34976b6 3709static bfd_boolean
d4596a51 3710mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3711{
a8028dd0 3712 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3713 struct mips_elf_hash_sort_data hsd;
3714 struct mips_got_info *g;
b49e97c9 3715
d4596a51
RS
3716 if (elf_hash_table (info)->dynsymcount == 0)
3717 return TRUE;
3718
a8028dd0 3719 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3720 BFD_ASSERT (htab != NULL);
3721
a8028dd0 3722 g = htab->got_info;
d4596a51
RS
3723 if (g == NULL)
3724 return TRUE;
f4416af6 3725
b49e97c9 3726 hsd.low = NULL;
23cc69b6
RS
3727 hsd.max_unref_got_dynindx
3728 = hsd.min_got_dynindx
3729 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3730 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3731 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3732 elf_hash_table (info)),
3733 mips_elf_sort_hash_table_f,
3734 &hsd);
3735
3736 /* There should have been enough room in the symbol table to
44c410de 3737 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3738 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3739 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3740 == elf_hash_table (info)->dynsymcount);
3741 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3742 == g->global_gotno);
b49e97c9
TS
3743
3744 /* Now we know which dynamic symbol has the lowest dynamic symbol
3745 table index in the GOT. */
d222d210 3746 htab->global_gotsym = hsd.low;
b49e97c9 3747
b34976b6 3748 return TRUE;
b49e97c9
TS
3749}
3750
3751/* If H needs a GOT entry, assign it the highest available dynamic
3752 index. Otherwise, assign it the lowest available dynamic
3753 index. */
3754
b34976b6 3755static bfd_boolean
9719ad41 3756mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3757{
9719ad41 3758 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9 3759
b49e97c9
TS
3760 /* Symbols without dynamic symbol table entries aren't interesting
3761 at all. */
3762 if (h->root.dynindx == -1)
b34976b6 3763 return TRUE;
b49e97c9 3764
634835ae 3765 switch (h->global_got_area)
f4416af6 3766 {
634835ae
RS
3767 case GGA_NONE:
3768 h->root.dynindx = hsd->max_non_got_dynindx++;
3769 break;
0f20cc35 3770
634835ae 3771 case GGA_NORMAL:
b49e97c9
TS
3772 h->root.dynindx = --hsd->min_got_dynindx;
3773 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3774 break;
3775
3776 case GGA_RELOC_ONLY:
634835ae
RS
3777 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3778 hsd->low = (struct elf_link_hash_entry *) h;
3779 h->root.dynindx = hsd->max_unref_got_dynindx++;
3780 break;
b49e97c9
TS
3781 }
3782
b34976b6 3783 return TRUE;
b49e97c9
TS
3784}
3785
ee227692
RS
3786/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3787 (which is owned by the caller and shouldn't be added to the
3788 hash table directly). */
3789
3790static bfd_boolean
3791mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3792 struct mips_got_entry *lookup)
3793{
3794 struct mips_elf_link_hash_table *htab;
3795 struct mips_got_entry *entry;
3796 struct mips_got_info *g;
3797 void **loc, **bfd_loc;
3798
3799 /* Make sure there's a slot for this entry in the master GOT. */
3800 htab = mips_elf_hash_table (info);
3801 g = htab->got_info;
3802 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3803 if (!loc)
3804 return FALSE;
3805
3806 /* Populate the entry if it isn't already. */
3807 entry = (struct mips_got_entry *) *loc;
3808 if (!entry)
3809 {
3810 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3811 if (!entry)
3812 return FALSE;
3813
9ab066b4 3814 lookup->tls_initialized = FALSE;
ee227692
RS
3815 lookup->gotidx = -1;
3816 *entry = *lookup;
3817 *loc = entry;
3818 }
3819
3820 /* Reuse the same GOT entry for the BFD's GOT. */
3821 g = mips_elf_bfd_got (abfd, TRUE);
3822 if (!g)
3823 return FALSE;
3824
3825 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3826 if (!bfd_loc)
3827 return FALSE;
3828
3829 if (!*bfd_loc)
3830 *bfd_loc = entry;
3831 return TRUE;
3832}
3833
e641e783
RS
3834/* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3835 entry for it. FOR_CALL is true if the caller is only interested in
6ccf4795 3836 using the GOT entry for calls. */
b49e97c9 3837
b34976b6 3838static bfd_boolean
9719ad41
RS
3839mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3840 bfd *abfd, struct bfd_link_info *info,
e641e783 3841 bfd_boolean for_call, int r_type)
b49e97c9 3842{
a8028dd0 3843 struct mips_elf_link_hash_table *htab;
634835ae 3844 struct mips_elf_link_hash_entry *hmips;
ee227692
RS
3845 struct mips_got_entry entry;
3846 unsigned char tls_type;
a8028dd0
RS
3847
3848 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3849 BFD_ASSERT (htab != NULL);
3850
634835ae 3851 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795
RS
3852 if (!for_call)
3853 hmips->got_only_for_calls = FALSE;
f4416af6 3854
b49e97c9
TS
3855 /* A global symbol in the GOT must also be in the dynamic symbol
3856 table. */
7c5fcef7
L
3857 if (h->dynindx == -1)
3858 {
3859 switch (ELF_ST_VISIBILITY (h->other))
3860 {
3861 case STV_INTERNAL:
3862 case STV_HIDDEN:
33bb52fb 3863 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3864 break;
3865 }
c152c796 3866 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3867 return FALSE;
7c5fcef7 3868 }
b49e97c9 3869
ee227692 3870 tls_type = mips_elf_reloc_tls_type (r_type);
9ab066b4 3871 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
ee227692 3872 hmips->global_got_area = GGA_NORMAL;
86324f90 3873
f4416af6
AO
3874 entry.abfd = abfd;
3875 entry.symndx = -1;
3876 entry.d.h = (struct mips_elf_link_hash_entry *) h;
ee227692
RS
3877 entry.tls_type = tls_type;
3878 return mips_elf_record_got_entry (info, abfd, &entry);
b49e97c9 3879}
f4416af6 3880
e641e783
RS
3881/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3882 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
f4416af6
AO
3883
3884static bfd_boolean
9719ad41 3885mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
e641e783 3886 struct bfd_link_info *info, int r_type)
f4416af6 3887{
a8028dd0
RS
3888 struct mips_elf_link_hash_table *htab;
3889 struct mips_got_info *g;
ee227692 3890 struct mips_got_entry entry;
f4416af6 3891
a8028dd0 3892 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3893 BFD_ASSERT (htab != NULL);
3894
a8028dd0
RS
3895 g = htab->got_info;
3896 BFD_ASSERT (g != NULL);
3897
f4416af6
AO
3898 entry.abfd = abfd;
3899 entry.symndx = symndx;
3900 entry.d.addend = addend;
e641e783 3901 entry.tls_type = mips_elf_reloc_tls_type (r_type);
ee227692 3902 return mips_elf_record_got_entry (info, abfd, &entry);
f4416af6 3903}
c224138d 3904
13db6b44
RS
3905/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
3906 H is the symbol's hash table entry, or null if SYMNDX is local
3907 to ABFD. */
c224138d
RS
3908
3909static bfd_boolean
13db6b44
RS
3910mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
3911 long symndx, struct elf_link_hash_entry *h,
3912 bfd_signed_vma addend)
c224138d 3913{
a8028dd0 3914 struct mips_elf_link_hash_table *htab;
ee227692 3915 struct mips_got_info *g1, *g2;
13db6b44 3916 struct mips_got_page_ref lookup, *entry;
ee227692 3917 void **loc, **bfd_loc;
c224138d 3918
a8028dd0 3919 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3920 BFD_ASSERT (htab != NULL);
3921
ee227692
RS
3922 g1 = htab->got_info;
3923 BFD_ASSERT (g1 != NULL);
a8028dd0 3924
13db6b44
RS
3925 if (h)
3926 {
3927 lookup.symndx = -1;
3928 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
3929 }
3930 else
3931 {
3932 lookup.symndx = symndx;
3933 lookup.u.abfd = abfd;
3934 }
3935 lookup.addend = addend;
3936 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
c224138d
RS
3937 if (loc == NULL)
3938 return FALSE;
3939
13db6b44 3940 entry = (struct mips_got_page_ref *) *loc;
c224138d
RS
3941 if (!entry)
3942 {
3943 entry = bfd_alloc (abfd, sizeof (*entry));
3944 if (!entry)
3945 return FALSE;
3946
13db6b44 3947 *entry = lookup;
c224138d
RS
3948 *loc = entry;
3949 }
3950
ee227692
RS
3951 /* Add the same entry to the BFD's GOT. */
3952 g2 = mips_elf_bfd_got (abfd, TRUE);
3953 if (!g2)
3954 return FALSE;
3955
13db6b44 3956 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
ee227692
RS
3957 if (!bfd_loc)
3958 return FALSE;
3959
3960 if (!*bfd_loc)
3961 *bfd_loc = entry;
3962
c224138d
RS
3963 return TRUE;
3964}
33bb52fb
RS
3965
3966/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3967
3968static void
3969mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3970 unsigned int n)
3971{
3972 asection *s;
3973 struct mips_elf_link_hash_table *htab;
3974
3975 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3976 BFD_ASSERT (htab != NULL);
3977
33bb52fb
RS
3978 s = mips_elf_rel_dyn_section (info, FALSE);
3979 BFD_ASSERT (s != NULL);
3980
3981 if (htab->is_vxworks)
3982 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3983 else
3984 {
3985 if (s->size == 0)
3986 {
3987 /* Make room for a null element. */
3988 s->size += MIPS_ELF_REL_SIZE (abfd);
3989 ++s->reloc_count;
3990 }
3991 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3992 }
3993}
3994\f
476366af
RS
3995/* A htab_traverse callback for GOT entries, with DATA pointing to a
3996 mips_elf_traverse_got_arg structure. Count the number of GOT
3997 entries and TLS relocs. Set DATA->value to true if we need
3998 to resolve indirect or warning symbols and then recreate the GOT. */
33bb52fb
RS
3999
4000static int
4001mips_elf_check_recreate_got (void **entryp, void *data)
4002{
4003 struct mips_got_entry *entry;
476366af 4004 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4005
4006 entry = (struct mips_got_entry *) *entryp;
476366af 4007 arg = (struct mips_elf_traverse_got_arg *) data;
33bb52fb
RS
4008 if (entry->abfd != NULL && entry->symndx == -1)
4009 {
4010 struct mips_elf_link_hash_entry *h;
4011
4012 h = entry->d.h;
4013 if (h->root.root.type == bfd_link_hash_indirect
4014 || h->root.root.type == bfd_link_hash_warning)
4015 {
476366af 4016 arg->value = TRUE;
33bb52fb
RS
4017 return 0;
4018 }
4019 }
476366af 4020 mips_elf_count_got_entry (arg->info, arg->g, entry);
33bb52fb
RS
4021 return 1;
4022}
4023
476366af
RS
4024/* A htab_traverse callback for GOT entries, with DATA pointing to a
4025 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4026 converting entries for indirect and warning symbols into entries
4027 for the target symbol. Set DATA->g to null on error. */
33bb52fb
RS
4028
4029static int
4030mips_elf_recreate_got (void **entryp, void *data)
4031{
72e7511a 4032 struct mips_got_entry new_entry, *entry;
476366af 4033 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4034 void **slot;
4035
33bb52fb 4036 entry = (struct mips_got_entry *) *entryp;
476366af 4037 arg = (struct mips_elf_traverse_got_arg *) data;
72e7511a
RS
4038 if (entry->abfd != NULL
4039 && entry->symndx == -1
4040 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4041 || entry->d.h->root.root.type == bfd_link_hash_warning))
33bb52fb
RS
4042 {
4043 struct mips_elf_link_hash_entry *h;
4044
72e7511a
RS
4045 new_entry = *entry;
4046 entry = &new_entry;
33bb52fb 4047 h = entry->d.h;
72e7511a 4048 do
634835ae
RS
4049 {
4050 BFD_ASSERT (h->global_got_area == GGA_NONE);
4051 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4052 }
72e7511a
RS
4053 while (h->root.root.type == bfd_link_hash_indirect
4054 || h->root.root.type == bfd_link_hash_warning);
33bb52fb
RS
4055 entry->d.h = h;
4056 }
476366af 4057 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
33bb52fb
RS
4058 if (slot == NULL)
4059 {
476366af 4060 arg->g = NULL;
33bb52fb
RS
4061 return 0;
4062 }
4063 if (*slot == NULL)
72e7511a
RS
4064 {
4065 if (entry == &new_entry)
4066 {
4067 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4068 if (!entry)
4069 {
476366af 4070 arg->g = NULL;
72e7511a
RS
4071 return 0;
4072 }
4073 *entry = new_entry;
4074 }
4075 *slot = entry;
476366af 4076 mips_elf_count_got_entry (arg->info, arg->g, entry);
72e7511a 4077 }
33bb52fb
RS
4078 return 1;
4079}
4080
13db6b44
RS
4081/* Return the maximum number of GOT page entries required for RANGE. */
4082
4083static bfd_vma
4084mips_elf_pages_for_range (const struct mips_got_page_range *range)
4085{
4086 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4087}
4088
4089/* Record that G requires a page entry that can reach SEC + ADDEND. */
4090
4091static bfd_boolean
b75d42bc 4092mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
13db6b44
RS
4093 asection *sec, bfd_signed_vma addend)
4094{
b75d42bc 4095 struct mips_got_info *g = arg->g;
13db6b44
RS
4096 struct mips_got_page_entry lookup, *entry;
4097 struct mips_got_page_range **range_ptr, *range;
4098 bfd_vma old_pages, new_pages;
4099 void **loc;
4100
4101 /* Find the mips_got_page_entry hash table entry for this section. */
4102 lookup.sec = sec;
4103 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4104 if (loc == NULL)
4105 return FALSE;
4106
4107 /* Create a mips_got_page_entry if this is the first time we've
4108 seen the section. */
4109 entry = (struct mips_got_page_entry *) *loc;
4110 if (!entry)
4111 {
b75d42bc 4112 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
13db6b44
RS
4113 if (!entry)
4114 return FALSE;
4115
4116 entry->sec = sec;
4117 *loc = entry;
4118 }
4119
4120 /* Skip over ranges whose maximum extent cannot share a page entry
4121 with ADDEND. */
4122 range_ptr = &entry->ranges;
4123 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4124 range_ptr = &(*range_ptr)->next;
4125
4126 /* If we scanned to the end of the list, or found a range whose
4127 minimum extent cannot share a page entry with ADDEND, create
4128 a new singleton range. */
4129 range = *range_ptr;
4130 if (!range || addend < range->min_addend - 0xffff)
4131 {
b75d42bc 4132 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
13db6b44
RS
4133 if (!range)
4134 return FALSE;
4135
4136 range->next = *range_ptr;
4137 range->min_addend = addend;
4138 range->max_addend = addend;
4139
4140 *range_ptr = range;
4141 entry->num_pages++;
4142 g->page_gotno++;
4143 return TRUE;
4144 }
4145
4146 /* Remember how many pages the old range contributed. */
4147 old_pages = mips_elf_pages_for_range (range);
4148
4149 /* Update the ranges. */
4150 if (addend < range->min_addend)
4151 range->min_addend = addend;
4152 else if (addend > range->max_addend)
4153 {
4154 if (range->next && addend >= range->next->min_addend - 0xffff)
4155 {
4156 old_pages += mips_elf_pages_for_range (range->next);
4157 range->max_addend = range->next->max_addend;
4158 range->next = range->next->next;
4159 }
4160 else
4161 range->max_addend = addend;
4162 }
4163
4164 /* Record any change in the total estimate. */
4165 new_pages = mips_elf_pages_for_range (range);
4166 if (old_pages != new_pages)
4167 {
4168 entry->num_pages += new_pages - old_pages;
4169 g->page_gotno += new_pages - old_pages;
4170 }
4171
4172 return TRUE;
4173}
4174
4175/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4176 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4177 whether the page reference described by *REFP needs a GOT page entry,
4178 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4179
4180static bfd_boolean
4181mips_elf_resolve_got_page_ref (void **refp, void *data)
4182{
4183 struct mips_got_page_ref *ref;
4184 struct mips_elf_traverse_got_arg *arg;
4185 struct mips_elf_link_hash_table *htab;
4186 asection *sec;
4187 bfd_vma addend;
4188
4189 ref = (struct mips_got_page_ref *) *refp;
4190 arg = (struct mips_elf_traverse_got_arg *) data;
4191 htab = mips_elf_hash_table (arg->info);
4192
4193 if (ref->symndx < 0)
4194 {
4195 struct mips_elf_link_hash_entry *h;
4196
4197 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4198 h = ref->u.h;
4199 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4200 return 1;
4201
4202 /* Ignore undefined symbols; we'll issue an error later if
4203 appropriate. */
4204 if (!((h->root.root.type == bfd_link_hash_defined
4205 || h->root.root.type == bfd_link_hash_defweak)
4206 && h->root.root.u.def.section))
4207 return 1;
4208
4209 sec = h->root.root.u.def.section;
4210 addend = h->root.root.u.def.value + ref->addend;
4211 }
4212 else
4213 {
4214 Elf_Internal_Sym *isym;
4215
4216 /* Read in the symbol. */
4217 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4218 ref->symndx);
4219 if (isym == NULL)
4220 {
4221 arg->g = NULL;
4222 return 0;
4223 }
4224
4225 /* Get the associated input section. */
4226 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4227 if (sec == NULL)
4228 {
4229 arg->g = NULL;
4230 return 0;
4231 }
4232
4233 /* If this is a mergable section, work out the section and offset
4234 of the merged data. For section symbols, the addend specifies
4235 of the offset _of_ the first byte in the data, otherwise it
4236 specifies the offset _from_ the first byte. */
4237 if (sec->flags & SEC_MERGE)
4238 {
4239 void *secinfo;
4240
4241 secinfo = elf_section_data (sec)->sec_info;
4242 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4243 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4244 isym->st_value + ref->addend);
4245 else
4246 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4247 isym->st_value) + ref->addend;
4248 }
4249 else
4250 addend = isym->st_value + ref->addend;
4251 }
b75d42bc 4252 if (!mips_elf_record_got_page_entry (arg, sec, addend))
13db6b44
RS
4253 {
4254 arg->g = NULL;
4255 return 0;
4256 }
4257 return 1;
4258}
4259
33bb52fb 4260/* If any entries in G->got_entries are for indirect or warning symbols,
13db6b44
RS
4261 replace them with entries for the target symbol. Convert g->got_page_refs
4262 into got_page_entry structures and estimate the number of page entries
4263 that they require. */
33bb52fb
RS
4264
4265static bfd_boolean
476366af
RS
4266mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4267 struct mips_got_info *g)
33bb52fb 4268{
476366af
RS
4269 struct mips_elf_traverse_got_arg tga;
4270 struct mips_got_info oldg;
4271
4272 oldg = *g;
33bb52fb 4273
476366af
RS
4274 tga.info = info;
4275 tga.g = g;
4276 tga.value = FALSE;
4277 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4278 if (tga.value)
33bb52fb 4279 {
476366af
RS
4280 *g = oldg;
4281 g->got_entries = htab_create (htab_size (oldg.got_entries),
4282 mips_elf_got_entry_hash,
4283 mips_elf_got_entry_eq, NULL);
4284 if (!g->got_entries)
33bb52fb
RS
4285 return FALSE;
4286
476366af
RS
4287 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4288 if (!tga.g)
4289 return FALSE;
4290
4291 htab_delete (oldg.got_entries);
33bb52fb 4292 }
13db6b44
RS
4293
4294 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4295 mips_got_page_entry_eq, NULL);
4296 if (g->got_page_entries == NULL)
4297 return FALSE;
4298
4299 tga.info = info;
4300 tga.g = g;
4301 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4302
33bb52fb
RS
4303 return TRUE;
4304}
4305
c5d6fa44
RS
4306/* Return true if a GOT entry for H should live in the local rather than
4307 global GOT area. */
4308
4309static bfd_boolean
4310mips_use_local_got_p (struct bfd_link_info *info,
4311 struct mips_elf_link_hash_entry *h)
4312{
4313 /* Symbols that aren't in the dynamic symbol table must live in the
4314 local GOT. This includes symbols that are completely undefined
4315 and which therefore don't bind locally. We'll report undefined
4316 symbols later if appropriate. */
4317 if (h->root.dynindx == -1)
4318 return TRUE;
4319
4320 /* Symbols that bind locally can (and in the case of forced-local
4321 symbols, must) live in the local GOT. */
4322 if (h->got_only_for_calls
4323 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4324 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4325 return TRUE;
4326
4327 /* If this is an executable that must provide a definition of the symbol,
4328 either though PLTs or copy relocations, then that address should go in
4329 the local rather than global GOT. */
4330 if (info->executable && h->has_static_relocs)
4331 return TRUE;
4332
4333 return FALSE;
4334}
4335
6c42ddb9
RS
4336/* A mips_elf_link_hash_traverse callback for which DATA points to the
4337 link_info structure. Decide whether the hash entry needs an entry in
4338 the global part of the primary GOT, setting global_got_area accordingly.
4339 Count the number of global symbols that are in the primary GOT only
4340 because they have relocations against them (reloc_only_gotno). */
33bb52fb
RS
4341
4342static int
d4596a51 4343mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 4344{
020d7251 4345 struct bfd_link_info *info;
6ccf4795 4346 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
4347 struct mips_got_info *g;
4348
020d7251 4349 info = (struct bfd_link_info *) data;
6ccf4795
RS
4350 htab = mips_elf_hash_table (info);
4351 g = htab->got_info;
d4596a51 4352 if (h->global_got_area != GGA_NONE)
33bb52fb 4353 {
020d7251 4354 /* Make a final decision about whether the symbol belongs in the
c5d6fa44
RS
4355 local or global GOT. */
4356 if (mips_use_local_got_p (info, h))
6c42ddb9
RS
4357 /* The symbol belongs in the local GOT. We no longer need this
4358 entry if it was only used for relocations; those relocations
4359 will be against the null or section symbol instead of H. */
4360 h->global_got_area = GGA_NONE;
6ccf4795
RS
4361 else if (htab->is_vxworks
4362 && h->got_only_for_calls
1bbce132 4363 && h->root.plt.plist->mips_offset != MINUS_ONE)
6ccf4795
RS
4364 /* On VxWorks, calls can refer directly to the .got.plt entry;
4365 they don't need entries in the regular GOT. .got.plt entries
4366 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4367 h->global_got_area = GGA_NONE;
6c42ddb9 4368 else if (h->global_got_area == GGA_RELOC_ONLY)
23cc69b6 4369 {
6c42ddb9 4370 g->reloc_only_gotno++;
23cc69b6 4371 g->global_gotno++;
23cc69b6 4372 }
33bb52fb
RS
4373 }
4374 return 1;
4375}
f4416af6 4376\f
d7206569
RS
4377/* A htab_traverse callback for GOT entries. Add each one to the GOT
4378 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
f4416af6
AO
4379
4380static int
d7206569 4381mips_elf_add_got_entry (void **entryp, void *data)
f4416af6 4382{
d7206569
RS
4383 struct mips_got_entry *entry;
4384 struct mips_elf_traverse_got_arg *arg;
4385 void **slot;
f4416af6 4386
d7206569
RS
4387 entry = (struct mips_got_entry *) *entryp;
4388 arg = (struct mips_elf_traverse_got_arg *) data;
4389 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4390 if (!slot)
f4416af6 4391 {
d7206569
RS
4392 arg->g = NULL;
4393 return 0;
f4416af6 4394 }
d7206569 4395 if (!*slot)
c224138d 4396 {
d7206569
RS
4397 *slot = entry;
4398 mips_elf_count_got_entry (arg->info, arg->g, entry);
c224138d 4399 }
f4416af6
AO
4400 return 1;
4401}
4402
d7206569
RS
4403/* A htab_traverse callback for GOT page entries. Add each one to the GOT
4404 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
c224138d
RS
4405
4406static int
d7206569 4407mips_elf_add_got_page_entry (void **entryp, void *data)
c224138d 4408{
d7206569
RS
4409 struct mips_got_page_entry *entry;
4410 struct mips_elf_traverse_got_arg *arg;
4411 void **slot;
c224138d 4412
d7206569
RS
4413 entry = (struct mips_got_page_entry *) *entryp;
4414 arg = (struct mips_elf_traverse_got_arg *) data;
4415 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4416 if (!slot)
c224138d 4417 {
d7206569 4418 arg->g = NULL;
c224138d
RS
4419 return 0;
4420 }
d7206569
RS
4421 if (!*slot)
4422 {
4423 *slot = entry;
4424 arg->g->page_gotno += entry->num_pages;
4425 }
c224138d
RS
4426 return 1;
4427}
4428
d7206569
RS
4429/* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4430 this would lead to overflow, 1 if they were merged successfully,
4431 and 0 if a merge failed due to lack of memory. (These values are chosen
4432 so that nonnegative return values can be returned by a htab_traverse
4433 callback.) */
c224138d
RS
4434
4435static int
d7206569 4436mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
c224138d
RS
4437 struct mips_got_info *to,
4438 struct mips_elf_got_per_bfd_arg *arg)
4439{
d7206569 4440 struct mips_elf_traverse_got_arg tga;
c224138d
RS
4441 unsigned int estimate;
4442
4443 /* Work out how many page entries we would need for the combined GOT. */
4444 estimate = arg->max_pages;
4445 if (estimate >= from->page_gotno + to->page_gotno)
4446 estimate = from->page_gotno + to->page_gotno;
4447
e2ece73c 4448 /* And conservatively estimate how many local and TLS entries
c224138d 4449 would be needed. */
e2ece73c
RS
4450 estimate += from->local_gotno + to->local_gotno;
4451 estimate += from->tls_gotno + to->tls_gotno;
4452
17214937
RS
4453 /* If we're merging with the primary got, any TLS relocations will
4454 come after the full set of global entries. Otherwise estimate those
e2ece73c 4455 conservatively as well. */
17214937 4456 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
e2ece73c
RS
4457 estimate += arg->global_count;
4458 else
4459 estimate += from->global_gotno + to->global_gotno;
c224138d
RS
4460
4461 /* Bail out if the combined GOT might be too big. */
4462 if (estimate > arg->max_count)
4463 return -1;
4464
c224138d 4465 /* Transfer the bfd's got information from FROM to TO. */
d7206569
RS
4466 tga.info = arg->info;
4467 tga.g = to;
4468 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4469 if (!tga.g)
c224138d
RS
4470 return 0;
4471
d7206569
RS
4472 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4473 if (!tga.g)
c224138d
RS
4474 return 0;
4475
d7206569 4476 mips_elf_replace_bfd_got (abfd, to);
c224138d
RS
4477 return 1;
4478}
4479
d7206569 4480/* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
f4416af6
AO
4481 as possible of the primary got, since it doesn't require explicit
4482 dynamic relocations, but don't use bfds that would reference global
4483 symbols out of the addressable range. Failing the primary got,
4484 attempt to merge with the current got, or finish the current got
4485 and then make make the new got current. */
4486
d7206569
RS
4487static bfd_boolean
4488mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4489 struct mips_elf_got_per_bfd_arg *arg)
f4416af6 4490{
c224138d
RS
4491 unsigned int estimate;
4492 int result;
4493
476366af 4494 if (!mips_elf_resolve_final_got_entries (arg->info, g))
d7206569
RS
4495 return FALSE;
4496
c224138d
RS
4497 /* Work out the number of page, local and TLS entries. */
4498 estimate = arg->max_pages;
4499 if (estimate > g->page_gotno)
4500 estimate = g->page_gotno;
4501 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4502
4503 /* We place TLS GOT entries after both locals and globals. The globals
4504 for the primary GOT may overflow the normal GOT size limit, so be
4505 sure not to merge a GOT which requires TLS with the primary GOT in that
4506 case. This doesn't affect non-primary GOTs. */
c224138d 4507 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4508
c224138d 4509 if (estimate <= arg->max_count)
f4416af6 4510 {
c224138d
RS
4511 /* If we don't have a primary GOT, use it as
4512 a starting point for the primary GOT. */
4513 if (!arg->primary)
4514 {
d7206569
RS
4515 arg->primary = g;
4516 return TRUE;
c224138d 4517 }
f4416af6 4518
c224138d 4519 /* Try merging with the primary GOT. */
d7206569 4520 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
c224138d
RS
4521 if (result >= 0)
4522 return result;
f4416af6 4523 }
c224138d 4524
f4416af6 4525 /* If we can merge with the last-created got, do it. */
c224138d 4526 if (arg->current)
f4416af6 4527 {
d7206569 4528 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
c224138d
RS
4529 if (result >= 0)
4530 return result;
f4416af6 4531 }
c224138d 4532
f4416af6
AO
4533 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4534 fits; if it turns out that it doesn't, we'll get relocation
4535 overflows anyway. */
c224138d
RS
4536 g->next = arg->current;
4537 arg->current = g;
0f20cc35 4538
d7206569 4539 return TRUE;
0f20cc35
DJ
4540}
4541
72e7511a
RS
4542/* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4543 to GOTIDX, duplicating the entry if it has already been assigned
4544 an index in a different GOT. */
4545
4546static bfd_boolean
4547mips_elf_set_gotidx (void **entryp, long gotidx)
4548{
4549 struct mips_got_entry *entry;
4550
4551 entry = (struct mips_got_entry *) *entryp;
4552 if (entry->gotidx > 0)
4553 {
4554 struct mips_got_entry *new_entry;
4555
4556 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4557 if (!new_entry)
4558 return FALSE;
4559
4560 *new_entry = *entry;
4561 *entryp = new_entry;
4562 entry = new_entry;
4563 }
4564 entry->gotidx = gotidx;
4565 return TRUE;
4566}
4567
4568/* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4569 mips_elf_traverse_got_arg in which DATA->value is the size of one
4570 GOT entry. Set DATA->g to null on failure. */
0f20cc35
DJ
4571
4572static int
72e7511a 4573mips_elf_initialize_tls_index (void **entryp, void *data)
0f20cc35 4574{
72e7511a
RS
4575 struct mips_got_entry *entry;
4576 struct mips_elf_traverse_got_arg *arg;
0f20cc35
DJ
4577
4578 /* We're only interested in TLS symbols. */
72e7511a 4579 entry = (struct mips_got_entry *) *entryp;
9ab066b4 4580 if (entry->tls_type == GOT_TLS_NONE)
0f20cc35
DJ
4581 return 1;
4582
72e7511a 4583 arg = (struct mips_elf_traverse_got_arg *) data;
6c42ddb9 4584 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
ead49a57 4585 {
6c42ddb9
RS
4586 arg->g = NULL;
4587 return 0;
f4416af6
AO
4588 }
4589
ead49a57 4590 /* Account for the entries we've just allocated. */
9ab066b4 4591 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
f4416af6
AO
4592 return 1;
4593}
4594
ab361d49
RS
4595/* A htab_traverse callback for GOT entries, where DATA points to a
4596 mips_elf_traverse_got_arg. Set the global_got_area of each global
4597 symbol to DATA->value. */
f4416af6 4598
f4416af6 4599static int
ab361d49 4600mips_elf_set_global_got_area (void **entryp, void *data)
f4416af6 4601{
ab361d49
RS
4602 struct mips_got_entry *entry;
4603 struct mips_elf_traverse_got_arg *arg;
f4416af6 4604
ab361d49
RS
4605 entry = (struct mips_got_entry *) *entryp;
4606 arg = (struct mips_elf_traverse_got_arg *) data;
4607 if (entry->abfd != NULL
4608 && entry->symndx == -1
4609 && entry->d.h->global_got_area != GGA_NONE)
4610 entry->d.h->global_got_area = arg->value;
4611 return 1;
4612}
4613
4614/* A htab_traverse callback for secondary GOT entries, where DATA points
4615 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4616 and record the number of relocations they require. DATA->value is
72e7511a 4617 the size of one GOT entry. Set DATA->g to null on failure. */
ab361d49
RS
4618
4619static int
4620mips_elf_set_global_gotidx (void **entryp, void *data)
4621{
4622 struct mips_got_entry *entry;
4623 struct mips_elf_traverse_got_arg *arg;
0f20cc35 4624
ab361d49
RS
4625 entry = (struct mips_got_entry *) *entryp;
4626 arg = (struct mips_elf_traverse_got_arg *) data;
634835ae
RS
4627 if (entry->abfd != NULL
4628 && entry->symndx == -1
4629 && entry->d.h->global_got_area != GGA_NONE)
f4416af6 4630 {
72e7511a
RS
4631 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_gotno))
4632 {
4633 arg->g = NULL;
4634 return 0;
4635 }
4636 arg->g->assigned_gotno += 1;
4637
ab361d49
RS
4638 if (arg->info->shared
4639 || (elf_hash_table (arg->info)->dynamic_sections_created
4640 && entry->d.h->root.def_dynamic
4641 && !entry->d.h->root.def_regular))
4642 arg->g->relocs += 1;
f4416af6
AO
4643 }
4644
4645 return 1;
4646}
4647
33bb52fb
RS
4648/* A htab_traverse callback for GOT entries for which DATA is the
4649 bfd_link_info. Forbid any global symbols from having traditional
4650 lazy-binding stubs. */
4651
0626d451 4652static int
33bb52fb 4653mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4654{
33bb52fb
RS
4655 struct bfd_link_info *info;
4656 struct mips_elf_link_hash_table *htab;
4657 struct mips_got_entry *entry;
0626d451 4658
33bb52fb
RS
4659 entry = (struct mips_got_entry *) *entryp;
4660 info = (struct bfd_link_info *) data;
4661 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4662 BFD_ASSERT (htab != NULL);
4663
0626d451
RS
4664 if (entry->abfd != NULL
4665 && entry->symndx == -1
33bb52fb 4666 && entry->d.h->needs_lazy_stub)
f4416af6 4667 {
33bb52fb
RS
4668 entry->d.h->needs_lazy_stub = FALSE;
4669 htab->lazy_stub_count--;
f4416af6 4670 }
143d77c5 4671
f4416af6
AO
4672 return 1;
4673}
4674
f4416af6
AO
4675/* Return the offset of an input bfd IBFD's GOT from the beginning of
4676 the primary GOT. */
4677static bfd_vma
9719ad41 4678mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6 4679{
d7206569 4680 if (!g->next)
f4416af6
AO
4681 return 0;
4682
d7206569 4683 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
4684 if (! g)
4685 return 0;
4686
4687 BFD_ASSERT (g->next);
4688
4689 g = g->next;
143d77c5 4690
0f20cc35
DJ
4691 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4692 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4693}
4694
4695/* Turn a single GOT that is too big for 16-bit addressing into
4696 a sequence of GOTs, each one 16-bit addressable. */
4697
4698static bfd_boolean
9719ad41 4699mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4700 asection *got, bfd_size_type pages)
f4416af6 4701{
a8028dd0 4702 struct mips_elf_link_hash_table *htab;
f4416af6 4703 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
ab361d49 4704 struct mips_elf_traverse_got_arg tga;
a8028dd0 4705 struct mips_got_info *g, *gg;
33bb52fb 4706 unsigned int assign, needed_relocs;
d7206569 4707 bfd *dynobj, *ibfd;
f4416af6 4708
33bb52fb 4709 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4710 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4711 BFD_ASSERT (htab != NULL);
4712
a8028dd0 4713 g = htab->got_info;
f4416af6 4714
f4416af6
AO
4715 got_per_bfd_arg.obfd = abfd;
4716 got_per_bfd_arg.info = info;
f4416af6
AO
4717 got_per_bfd_arg.current = NULL;
4718 got_per_bfd_arg.primary = NULL;
0a44bf69 4719 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4720 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4721 - htab->reserved_gotno);
c224138d 4722 got_per_bfd_arg.max_pages = pages;
0f20cc35 4723 /* The number of globals that will be included in the primary GOT.
ab361d49 4724 See the calls to mips_elf_set_global_got_area below for more
0f20cc35
DJ
4725 information. */
4726 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4727
4728 /* Try to merge the GOTs of input bfds together, as long as they
4729 don't seem to exceed the maximum GOT size, choosing one of them
4730 to be the primary GOT. */
d7206569
RS
4731 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4732 {
4733 gg = mips_elf_bfd_got (ibfd, FALSE);
4734 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4735 return FALSE;
4736 }
f4416af6 4737
0f20cc35 4738 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6 4739 if (got_per_bfd_arg.primary == NULL)
3dff0dd1 4740 g->next = mips_elf_create_got_info (abfd);
f4416af6
AO
4741 else
4742 g->next = got_per_bfd_arg.primary;
4743 g->next->next = got_per_bfd_arg.current;
4744
4745 /* GG is now the master GOT, and G is the primary GOT. */
4746 gg = g;
4747 g = g->next;
4748
4749 /* Map the output bfd to the primary got. That's what we're going
4750 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4751 didn't mark in check_relocs, and we want a quick way to find it.
4752 We can't just use gg->next because we're going to reverse the
4753 list. */
d7206569 4754 mips_elf_replace_bfd_got (abfd, g);
f4416af6 4755
634835ae
RS
4756 /* Every symbol that is referenced in a dynamic relocation must be
4757 present in the primary GOT, so arrange for them to appear after
4758 those that are actually referenced. */
23cc69b6 4759 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4760 g->global_gotno = gg->global_gotno;
f4416af6 4761
ab361d49
RS
4762 tga.info = info;
4763 tga.value = GGA_RELOC_ONLY;
4764 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4765 tga.value = GGA_NORMAL;
4766 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
f4416af6
AO
4767
4768 /* Now go through the GOTs assigning them offset ranges.
4769 [assigned_gotno, local_gotno[ will be set to the range of local
4770 entries in each GOT. We can then compute the end of a GOT by
4771 adding local_gotno to global_gotno. We reverse the list and make
4772 it circular since then we'll be able to quickly compute the
4773 beginning of a GOT, by computing the end of its predecessor. To
4774 avoid special cases for the primary GOT, while still preserving
4775 assertions that are valid for both single- and multi-got links,
4776 we arrange for the main got struct to have the right number of
4777 global entries, but set its local_gotno such that the initial
4778 offset of the primary GOT is zero. Remember that the primary GOT
4779 will become the last item in the circular linked list, so it
4780 points back to the master GOT. */
4781 gg->local_gotno = -g->global_gotno;
4782 gg->global_gotno = g->global_gotno;
0f20cc35 4783 gg->tls_gotno = 0;
f4416af6
AO
4784 assign = 0;
4785 gg->next = gg;
4786
4787 do
4788 {
4789 struct mips_got_info *gn;
4790
861fb55a 4791 assign += htab->reserved_gotno;
f4416af6 4792 g->assigned_gotno = assign;
c224138d
RS
4793 g->local_gotno += assign;
4794 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4795 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4796
ead49a57
RS
4797 /* Take g out of the direct list, and push it onto the reversed
4798 list that gg points to. g->next is guaranteed to be nonnull after
4799 this operation, as required by mips_elf_initialize_tls_index. */
4800 gn = g->next;
4801 g->next = gg->next;
4802 gg->next = g;
4803
0f20cc35
DJ
4804 /* Set up any TLS entries. We always place the TLS entries after
4805 all non-TLS entries. */
4806 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
72e7511a
RS
4807 tga.g = g;
4808 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4809 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4810 if (!tga.g)
4811 return FALSE;
1fd20d70 4812 BFD_ASSERT (g->tls_assigned_gotno == assign);
f4416af6 4813
ead49a57 4814 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4815 g = gn;
0626d451 4816
33bb52fb
RS
4817 /* Forbid global symbols in every non-primary GOT from having
4818 lazy-binding stubs. */
0626d451 4819 if (g)
33bb52fb 4820 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4821 }
4822 while (g);
4823
59b08994 4824 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
33bb52fb
RS
4825
4826 needed_relocs = 0;
33bb52fb
RS
4827 for (g = gg->next; g && g->next != gg; g = g->next)
4828 {
4829 unsigned int save_assign;
4830
ab361d49
RS
4831 /* Assign offsets to global GOT entries and count how many
4832 relocations they need. */
33bb52fb
RS
4833 save_assign = g->assigned_gotno;
4834 g->assigned_gotno = g->local_gotno;
ab361d49
RS
4835 tga.info = info;
4836 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4837 tga.g = g;
4838 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
72e7511a
RS
4839 if (!tga.g)
4840 return FALSE;
4841 BFD_ASSERT (g->assigned_gotno == g->local_gotno + g->global_gotno);
33bb52fb 4842 g->assigned_gotno = save_assign;
72e7511a 4843
33bb52fb
RS
4844 if (info->shared)
4845 {
ab361d49 4846 g->relocs += g->local_gotno - g->assigned_gotno;
33bb52fb
RS
4847 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4848 + g->next->global_gotno
4849 + g->next->tls_gotno
861fb55a 4850 + htab->reserved_gotno);
33bb52fb 4851 }
ab361d49 4852 needed_relocs += g->relocs;
33bb52fb 4853 }
ab361d49 4854 needed_relocs += g->relocs;
33bb52fb
RS
4855
4856 if (needed_relocs)
4857 mips_elf_allocate_dynamic_relocations (dynobj, info,
4858 needed_relocs);
143d77c5 4859
f4416af6
AO
4860 return TRUE;
4861}
143d77c5 4862
b49e97c9
TS
4863\f
4864/* Returns the first relocation of type r_type found, beginning with
4865 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4866
4867static const Elf_Internal_Rela *
9719ad41
RS
4868mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4869 const Elf_Internal_Rela *relocation,
4870 const Elf_Internal_Rela *relend)
b49e97c9 4871{
c000e262
TS
4872 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4873
b49e97c9
TS
4874 while (relocation < relend)
4875 {
c000e262
TS
4876 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4877 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4878 return relocation;
4879
4880 ++relocation;
4881 }
4882
4883 /* We didn't find it. */
b49e97c9
TS
4884 return NULL;
4885}
4886
020d7251 4887/* Return whether an input relocation is against a local symbol. */
b49e97c9 4888
b34976b6 4889static bfd_boolean
9719ad41
RS
4890mips_elf_local_relocation_p (bfd *input_bfd,
4891 const Elf_Internal_Rela *relocation,
020d7251 4892 asection **local_sections)
b49e97c9
TS
4893{
4894 unsigned long r_symndx;
4895 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
4896 size_t extsymoff;
4897
4898 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4899 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4900 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4901
4902 if (r_symndx < extsymoff)
b34976b6 4903 return TRUE;
b49e97c9 4904 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4905 return TRUE;
b49e97c9 4906
b34976b6 4907 return FALSE;
b49e97c9
TS
4908}
4909\f
4910/* Sign-extend VALUE, which has the indicated number of BITS. */
4911
a7ebbfdf 4912bfd_vma
9719ad41 4913_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4914{
4915 if (value & ((bfd_vma) 1 << (bits - 1)))
4916 /* VALUE is negative. */
4917 value |= ((bfd_vma) - 1) << bits;
4918
4919 return value;
4920}
4921
4922/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4923 range expressible by a signed number with the indicated number of
b49e97c9
TS
4924 BITS. */
4925
b34976b6 4926static bfd_boolean
9719ad41 4927mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4928{
4929 bfd_signed_vma svalue = (bfd_signed_vma) value;
4930
4931 if (svalue > (1 << (bits - 1)) - 1)
4932 /* The value is too big. */
b34976b6 4933 return TRUE;
b49e97c9
TS
4934 else if (svalue < -(1 << (bits - 1)))
4935 /* The value is too small. */
b34976b6 4936 return TRUE;
b49e97c9
TS
4937
4938 /* All is well. */
b34976b6 4939 return FALSE;
b49e97c9
TS
4940}
4941
4942/* Calculate the %high function. */
4943
4944static bfd_vma
9719ad41 4945mips_elf_high (bfd_vma value)
b49e97c9
TS
4946{
4947 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4948}
4949
4950/* Calculate the %higher function. */
4951
4952static bfd_vma
9719ad41 4953mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4954{
4955#ifdef BFD64
4956 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4957#else
4958 abort ();
c5ae1840 4959 return MINUS_ONE;
b49e97c9
TS
4960#endif
4961}
4962
4963/* Calculate the %highest function. */
4964
4965static bfd_vma
9719ad41 4966mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4967{
4968#ifdef BFD64
b15e6682 4969 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4970#else
4971 abort ();
c5ae1840 4972 return MINUS_ONE;
b49e97c9
TS
4973#endif
4974}
4975\f
4976/* Create the .compact_rel section. */
4977
b34976b6 4978static bfd_boolean
9719ad41
RS
4979mips_elf_create_compact_rel_section
4980 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4981{
4982 flagword flags;
4983 register asection *s;
4984
3d4d4302 4985 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
b49e97c9
TS
4986 {
4987 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4988 | SEC_READONLY);
4989
3d4d4302 4990 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4991 if (s == NULL
b49e97c9
TS
4992 || ! bfd_set_section_alignment (abfd, s,
4993 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4994 return FALSE;
b49e97c9 4995
eea6121a 4996 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4997 }
4998
b34976b6 4999 return TRUE;
b49e97c9
TS
5000}
5001
5002/* Create the .got section to hold the global offset table. */
5003
b34976b6 5004static bfd_boolean
23cc69b6 5005mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
5006{
5007 flagword flags;
5008 register asection *s;
5009 struct elf_link_hash_entry *h;
14a793b2 5010 struct bfd_link_hash_entry *bh;
0a44bf69
RS
5011 struct mips_elf_link_hash_table *htab;
5012
5013 htab = mips_elf_hash_table (info);
4dfe6ac6 5014 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5015
5016 /* This function may be called more than once. */
23cc69b6
RS
5017 if (htab->sgot)
5018 return TRUE;
b49e97c9
TS
5019
5020 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5021 | SEC_LINKER_CREATED);
5022
72b4917c
TS
5023 /* We have to use an alignment of 2**4 here because this is hardcoded
5024 in the function stub generation and in the linker script. */
87e0a731 5025 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
b49e97c9 5026 if (s == NULL
72b4917c 5027 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 5028 return FALSE;
a8028dd0 5029 htab->sgot = s;
b49e97c9
TS
5030
5031 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5032 linker script because we don't want to define the symbol if we
5033 are not creating a global offset table. */
14a793b2 5034 bh = NULL;
b49e97c9
TS
5035 if (! (_bfd_generic_link_add_one_symbol
5036 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 5037 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5038 return FALSE;
14a793b2
AM
5039
5040 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5041 h->non_elf = 0;
5042 h->def_regular = 1;
b49e97c9 5043 h->type = STT_OBJECT;
2f9efdfc 5044 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d329bcd1 5045 elf_hash_table (info)->hgot = h;
b49e97c9
TS
5046
5047 if (info->shared
c152c796 5048 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5049 return FALSE;
b49e97c9 5050
3dff0dd1 5051 htab->got_info = mips_elf_create_got_info (abfd);
f0abc2a1 5052 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
5053 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5054
861fb55a 5055 /* We also need a .got.plt section when generating PLTs. */
87e0a731
AM
5056 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5057 SEC_ALLOC | SEC_LOAD
5058 | SEC_HAS_CONTENTS
5059 | SEC_IN_MEMORY
5060 | SEC_LINKER_CREATED);
861fb55a
DJ
5061 if (s == NULL)
5062 return FALSE;
5063 htab->sgotplt = s;
0a44bf69 5064
b34976b6 5065 return TRUE;
b49e97c9 5066}
b49e97c9 5067\f
0a44bf69
RS
5068/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5069 __GOTT_INDEX__ symbols. These symbols are only special for
5070 shared objects; they are not used in executables. */
5071
5072static bfd_boolean
5073is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5074{
5075 return (mips_elf_hash_table (info)->is_vxworks
5076 && info->shared
5077 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5078 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5079}
861fb55a
DJ
5080
5081/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5082 require an la25 stub. See also mips_elf_local_pic_function_p,
5083 which determines whether the destination function ever requires a
5084 stub. */
5085
5086static bfd_boolean
8f0c309a
CLT
5087mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5088 bfd_boolean target_is_16_bit_code_p)
861fb55a
DJ
5089{
5090 /* We specifically ignore branches and jumps from EF_PIC objects,
5091 where the onus is on the compiler or programmer to perform any
5092 necessary initialization of $25. Sometimes such initialization
5093 is unnecessary; for example, -mno-shared functions do not use
5094 the incoming value of $25, and may therefore be called directly. */
5095 if (PIC_OBJECT_P (input_bfd))
5096 return FALSE;
5097
5098 switch (r_type)
5099 {
5100 case R_MIPS_26:
5101 case R_MIPS_PC16:
df58fc94
RS
5102 case R_MICROMIPS_26_S1:
5103 case R_MICROMIPS_PC7_S1:
5104 case R_MICROMIPS_PC10_S1:
5105 case R_MICROMIPS_PC16_S1:
5106 case R_MICROMIPS_PC23_S2:
861fb55a
DJ
5107 return TRUE;
5108
8f0c309a
CLT
5109 case R_MIPS16_26:
5110 return !target_is_16_bit_code_p;
5111
861fb55a
DJ
5112 default:
5113 return FALSE;
5114 }
5115}
0a44bf69 5116\f
b49e97c9
TS
5117/* Calculate the value produced by the RELOCATION (which comes from
5118 the INPUT_BFD). The ADDEND is the addend to use for this
5119 RELOCATION; RELOCATION->R_ADDEND is ignored.
5120
5121 The result of the relocation calculation is stored in VALUEP.
38a7df63 5122 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
df58fc94 5123 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9
TS
5124
5125 This function returns bfd_reloc_continue if the caller need take no
5126 further action regarding this relocation, bfd_reloc_notsupported if
5127 something goes dramatically wrong, bfd_reloc_overflow if an
5128 overflow occurs, and bfd_reloc_ok to indicate success. */
5129
5130static bfd_reloc_status_type
9719ad41
RS
5131mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5132 asection *input_section,
5133 struct bfd_link_info *info,
5134 const Elf_Internal_Rela *relocation,
5135 bfd_vma addend, reloc_howto_type *howto,
5136 Elf_Internal_Sym *local_syms,
5137 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
5138 const char **namep,
5139 bfd_boolean *cross_mode_jump_p,
9719ad41 5140 bfd_boolean save_addend)
b49e97c9
TS
5141{
5142 /* The eventual value we will return. */
5143 bfd_vma value;
5144 /* The address of the symbol against which the relocation is
5145 occurring. */
5146 bfd_vma symbol = 0;
5147 /* The final GP value to be used for the relocatable, executable, or
5148 shared object file being produced. */
0a61c8c2 5149 bfd_vma gp;
b49e97c9
TS
5150 /* The place (section offset or address) of the storage unit being
5151 relocated. */
5152 bfd_vma p;
5153 /* The value of GP used to create the relocatable object. */
0a61c8c2 5154 bfd_vma gp0;
b49e97c9
TS
5155 /* The offset into the global offset table at which the address of
5156 the relocation entry symbol, adjusted by the addend, resides
5157 during execution. */
5158 bfd_vma g = MINUS_ONE;
5159 /* The section in which the symbol referenced by the relocation is
5160 located. */
5161 asection *sec = NULL;
5162 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 5163 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 5164 symbol. */
b34976b6
AM
5165 bfd_boolean local_p, was_local_p;
5166 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5167 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
5168 /* TRUE if the symbol referred to by this relocation is
5169 "__gnu_local_gp". */
5170 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
5171 Elf_Internal_Shdr *symtab_hdr;
5172 size_t extsymoff;
5173 unsigned long r_symndx;
5174 int r_type;
b34976b6 5175 /* TRUE if overflow occurred during the calculation of the
b49e97c9 5176 relocation value. */
b34976b6
AM
5177 bfd_boolean overflowed_p;
5178 /* TRUE if this relocation refers to a MIPS16 function. */
5179 bfd_boolean target_is_16_bit_code_p = FALSE;
df58fc94 5180 bfd_boolean target_is_micromips_code_p = FALSE;
0a44bf69
RS
5181 struct mips_elf_link_hash_table *htab;
5182 bfd *dynobj;
5183
5184 dynobj = elf_hash_table (info)->dynobj;
5185 htab = mips_elf_hash_table (info);
4dfe6ac6 5186 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5187
5188 /* Parse the relocation. */
5189 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5190 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5191 p = (input_section->output_section->vma
5192 + input_section->output_offset
5193 + relocation->r_offset);
5194
5195 /* Assume that there will be no overflow. */
b34976b6 5196 overflowed_p = FALSE;
b49e97c9
TS
5197
5198 /* Figure out whether or not the symbol is local, and get the offset
5199 used in the array of hash table entries. */
5200 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5201 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 5202 local_sections);
bce03d3d 5203 was_local_p = local_p;
b49e97c9
TS
5204 if (! elf_bad_symtab (input_bfd))
5205 extsymoff = symtab_hdr->sh_info;
5206 else
5207 {
5208 /* The symbol table does not follow the rule that local symbols
5209 must come before globals. */
5210 extsymoff = 0;
5211 }
5212
5213 /* Figure out the value of the symbol. */
5214 if (local_p)
5215 {
5216 Elf_Internal_Sym *sym;
5217
5218 sym = local_syms + r_symndx;
5219 sec = local_sections[r_symndx];
5220
5221 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
5222 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5223 || (sec->flags & SEC_MERGE))
b49e97c9 5224 symbol += sym->st_value;
d4df96e6
L
5225 if ((sec->flags & SEC_MERGE)
5226 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5227 {
5228 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5229 addend -= symbol;
5230 addend += sec->output_section->vma + sec->output_offset;
5231 }
b49e97c9 5232
df58fc94
RS
5233 /* MIPS16/microMIPS text labels should be treated as odd. */
5234 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
5235 ++symbol;
5236
5237 /* Record the name of this symbol, for our caller. */
5238 *namep = bfd_elf_string_from_elf_section (input_bfd,
5239 symtab_hdr->sh_link,
5240 sym->st_name);
5241 if (*namep == '\0')
5242 *namep = bfd_section_name (input_bfd, sec);
5243
30c09090 5244 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
df58fc94 5245 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
b49e97c9
TS
5246 }
5247 else
5248 {
560e09e9
NC
5249 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5250
b49e97c9
TS
5251 /* For global symbols we look up the symbol in the hash-table. */
5252 h = ((struct mips_elf_link_hash_entry *)
5253 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5254 /* Find the real hash-table entry for this symbol. */
5255 while (h->root.root.type == bfd_link_hash_indirect
5256 || h->root.root.type == bfd_link_hash_warning)
5257 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5258
5259 /* Record the name of this symbol, for our caller. */
5260 *namep = h->root.root.root.string;
5261
5262 /* See if this is the special _gp_disp symbol. Note that such a
5263 symbol must always be a global symbol. */
560e09e9 5264 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
5265 && ! NEWABI_P (input_bfd))
5266 {
5267 /* Relocations against _gp_disp are permitted only with
5268 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 5269 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
5270 return bfd_reloc_notsupported;
5271
b34976b6 5272 gp_disp_p = TRUE;
b49e97c9 5273 }
bbe506e8
TS
5274 /* See if this is the special _gp symbol. Note that such a
5275 symbol must always be a global symbol. */
5276 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5277 gnu_local_gp_p = TRUE;
5278
5279
b49e97c9
TS
5280 /* If this symbol is defined, calculate its address. Note that
5281 _gp_disp is a magic symbol, always implicitly defined by the
5282 linker, so it's inappropriate to check to see whether or not
5283 its defined. */
5284 else if ((h->root.root.type == bfd_link_hash_defined
5285 || h->root.root.type == bfd_link_hash_defweak)
5286 && h->root.root.u.def.section)
5287 {
5288 sec = h->root.root.u.def.section;
5289 if (sec->output_section)
5290 symbol = (h->root.root.u.def.value
5291 + sec->output_section->vma
5292 + sec->output_offset);
5293 else
5294 symbol = h->root.root.u.def.value;
5295 }
5296 else if (h->root.root.type == bfd_link_hash_undefweak)
5297 /* We allow relocations against undefined weak symbols, giving
5298 it the value zero, so that you can undefined weak functions
5299 and check to see if they exist by looking at their
5300 addresses. */
5301 symbol = 0;
59c2e50f 5302 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5303 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5304 symbol = 0;
a4d0f181
TS
5305 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5306 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5307 {
5308 /* If this is a dynamic link, we should have created a
5309 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5310 in in _bfd_mips_elf_create_dynamic_sections.
5311 Otherwise, we should define the symbol with a value of 0.
5312 FIXME: It should probably get into the symbol table
5313 somehow as well. */
5314 BFD_ASSERT (! info->shared);
5315 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5316 symbol = 0;
5317 }
5e2b0d47
NC
5318 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5319 {
5320 /* This is an optional symbol - an Irix specific extension to the
5321 ELF spec. Ignore it for now.
5322 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5323 than simply ignoring them, but we do not handle this for now.
5324 For information see the "64-bit ELF Object File Specification"
5325 which is available from here:
5326 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5327 symbol = 0;
5328 }
e7e2196d
MR
5329 else if ((*info->callbacks->undefined_symbol)
5330 (info, h->root.root.root.string, input_bfd,
5331 input_section, relocation->r_offset,
5332 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5333 || ELF_ST_VISIBILITY (h->root.other)))
5334 {
5335 return bfd_reloc_undefined;
5336 }
b49e97c9
TS
5337 else
5338 {
e7e2196d 5339 return bfd_reloc_notsupported;
b49e97c9
TS
5340 }
5341
30c09090 5342 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
1bbce132 5343 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
b49e97c9
TS
5344 }
5345
738e5348
RS
5346 /* If this is a reference to a 16-bit function with a stub, we need
5347 to redirect the relocation to the stub unless:
5348
5349 (a) the relocation is for a MIPS16 JAL;
5350
5351 (b) the relocation is for a MIPS16 PIC call, and there are no
5352 non-MIPS16 uses of the GOT slot; or
5353
5354 (c) the section allows direct references to MIPS16 functions. */
5355 if (r_type != R_MIPS16_26
5356 && !info->relocatable
5357 && ((h != NULL
5358 && h->fn_stub != NULL
5359 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71 5360 || (local_p
698600e4
AM
5361 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5362 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5363 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5364 {
5365 /* This is a 32- or 64-bit call to a 16-bit function. We should
5366 have already noticed that we were going to need the
5367 stub. */
5368 if (local_p)
8f0c309a 5369 {
698600e4 5370 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
8f0c309a
CLT
5371 value = 0;
5372 }
b49e97c9
TS
5373 else
5374 {
5375 BFD_ASSERT (h->need_fn_stub);
8f0c309a
CLT
5376 if (h->la25_stub)
5377 {
5378 /* If a LA25 header for the stub itself exists, point to the
5379 prepended LUI/ADDIU sequence. */
5380 sec = h->la25_stub->stub_section;
5381 value = h->la25_stub->offset;
5382 }
5383 else
5384 {
5385 sec = h->fn_stub;
5386 value = 0;
5387 }
b49e97c9
TS
5388 }
5389
8f0c309a 5390 symbol = sec->output_section->vma + sec->output_offset + value;
f38c2df5
TS
5391 /* The target is 16-bit, but the stub isn't. */
5392 target_is_16_bit_code_p = FALSE;
b49e97c9 5393 }
1bbce132
MR
5394 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5395 to a standard MIPS function, we need to redirect the call to the stub.
5396 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5397 indirect calls should use an indirect stub instead. */
1049f94e 5398 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5399 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71 5400 || (local_p
698600e4
AM
5401 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5402 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
1bbce132 5403 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
b49e97c9 5404 {
b9d58d71 5405 if (local_p)
698600e4 5406 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
b9d58d71 5407 else
b49e97c9 5408 {
b9d58d71
TS
5409 /* If both call_stub and call_fp_stub are defined, we can figure
5410 out which one to use by checking which one appears in the input
5411 file. */
5412 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5413 {
b9d58d71 5414 asection *o;
68ffbac6 5415
b9d58d71
TS
5416 sec = NULL;
5417 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5418 {
b9d58d71
TS
5419 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5420 {
5421 sec = h->call_fp_stub;
5422 break;
5423 }
b49e97c9 5424 }
b9d58d71
TS
5425 if (sec == NULL)
5426 sec = h->call_stub;
b49e97c9 5427 }
b9d58d71 5428 else if (h->call_stub != NULL)
b49e97c9 5429 sec = h->call_stub;
b9d58d71
TS
5430 else
5431 sec = h->call_fp_stub;
5432 }
b49e97c9 5433
eea6121a 5434 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5435 symbol = sec->output_section->vma + sec->output_offset;
5436 }
861fb55a
DJ
5437 /* If this is a direct call to a PIC function, redirect to the
5438 non-PIC stub. */
5439 else if (h != NULL && h->la25_stub
8f0c309a
CLT
5440 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5441 target_is_16_bit_code_p))
861fb55a
DJ
5442 symbol = (h->la25_stub->stub_section->output_section->vma
5443 + h->la25_stub->stub_section->output_offset
5444 + h->la25_stub->offset);
1bbce132
MR
5445 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5446 entry is used if a standard PLT entry has also been made. In this
5447 case the symbol will have been set by mips_elf_set_plt_sym_value
5448 to point to the standard PLT entry, so redirect to the compressed
5449 one. */
5450 else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
5451 && !info->relocatable
5452 && h != NULL
5453 && h->use_plt_entry
5454 && h->root.plt.plist->comp_offset != MINUS_ONE
5455 && h->root.plt.plist->mips_offset != MINUS_ONE)
5456 {
5457 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5458
5459 sec = htab->splt;
5460 symbol = (sec->output_section->vma
5461 + sec->output_offset
5462 + htab->plt_header_size
5463 + htab->plt_mips_offset
5464 + h->root.plt.plist->comp_offset
5465 + 1);
5466
5467 target_is_16_bit_code_p = !micromips_p;
5468 target_is_micromips_code_p = micromips_p;
5469 }
b49e97c9 5470
df58fc94
RS
5471 /* Make sure MIPS16 and microMIPS are not used together. */
5472 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5473 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5474 {
5475 (*_bfd_error_handler)
5476 (_("MIPS16 and microMIPS functions cannot call each other"));
5477 return bfd_reloc_notsupported;
5478 }
5479
b49e97c9 5480 /* Calls from 16-bit code to 32-bit code and vice versa require the
df58fc94
RS
5481 mode change. However, we can ignore calls to undefined weak symbols,
5482 which should never be executed at runtime. This exception is important
5483 because the assembly writer may have "known" that any definition of the
5484 symbol would be 16-bit code, and that direct jumps were therefore
5485 acceptable. */
5486 *cross_mode_jump_p = (!info->relocatable
5487 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5488 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5489 || (r_type == R_MICROMIPS_26_S1
5490 && !target_is_micromips_code_p)
5491 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5492 && (target_is_16_bit_code_p
5493 || target_is_micromips_code_p))));
b49e97c9 5494
c5d6fa44 5495 local_p = (h == NULL || mips_use_local_got_p (info, h));
b49e97c9 5496
0a61c8c2
RS
5497 gp0 = _bfd_get_gp_value (input_bfd);
5498 gp = _bfd_get_gp_value (abfd);
23cc69b6 5499 if (htab->got_info)
a8028dd0 5500 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5501
5502 if (gnu_local_gp_p)
5503 symbol = gp;
5504
df58fc94
RS
5505 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5506 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5507 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5508 if (got_page_reloc_p (r_type) && !local_p)
020d7251 5509 {
df58fc94
RS
5510 r_type = (micromips_reloc_p (r_type)
5511 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
020d7251
RS
5512 addend = 0;
5513 }
5514
e77760d2 5515 /* If we haven't already determined the GOT offset, and we're going
0a61c8c2 5516 to need it, get it now. */
b49e97c9
TS
5517 switch (r_type)
5518 {
738e5348
RS
5519 case R_MIPS16_CALL16:
5520 case R_MIPS16_GOT16:
b49e97c9
TS
5521 case R_MIPS_CALL16:
5522 case R_MIPS_GOT16:
5523 case R_MIPS_GOT_DISP:
5524 case R_MIPS_GOT_HI16:
5525 case R_MIPS_CALL_HI16:
5526 case R_MIPS_GOT_LO16:
5527 case R_MIPS_CALL_LO16:
df58fc94
RS
5528 case R_MICROMIPS_CALL16:
5529 case R_MICROMIPS_GOT16:
5530 case R_MICROMIPS_GOT_DISP:
5531 case R_MICROMIPS_GOT_HI16:
5532 case R_MICROMIPS_CALL_HI16:
5533 case R_MICROMIPS_GOT_LO16:
5534 case R_MICROMIPS_CALL_LO16:
0f20cc35
DJ
5535 case R_MIPS_TLS_GD:
5536 case R_MIPS_TLS_GOTTPREL:
5537 case R_MIPS_TLS_LDM:
d0f13682
CLT
5538 case R_MIPS16_TLS_GD:
5539 case R_MIPS16_TLS_GOTTPREL:
5540 case R_MIPS16_TLS_LDM:
df58fc94
RS
5541 case R_MICROMIPS_TLS_GD:
5542 case R_MICROMIPS_TLS_GOTTPREL:
5543 case R_MICROMIPS_TLS_LDM:
b49e97c9 5544 /* Find the index into the GOT where this value is located. */
df58fc94 5545 if (tls_ldm_reloc_p (r_type))
0f20cc35 5546 {
0a44bf69 5547 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5548 0, 0, NULL, r_type);
0f20cc35
DJ
5549 if (g == MINUS_ONE)
5550 return bfd_reloc_outofrange;
5551 }
5552 else if (!local_p)
b49e97c9 5553 {
0a44bf69
RS
5554 /* On VxWorks, CALL relocations should refer to the .got.plt
5555 entry, which is initialized to point at the PLT stub. */
5556 if (htab->is_vxworks
df58fc94
RS
5557 && (call_hi16_reloc_p (r_type)
5558 || call_lo16_reloc_p (r_type)
738e5348 5559 || call16_reloc_p (r_type)))
0a44bf69
RS
5560 {
5561 BFD_ASSERT (addend == 0);
5562 BFD_ASSERT (h->root.needs_plt);
5563 g = mips_elf_gotplt_index (info, &h->root);
5564 }
5565 else
b49e97c9 5566 {
020d7251 5567 BFD_ASSERT (addend == 0);
13fbec83
RS
5568 g = mips_elf_global_got_index (abfd, info, input_bfd,
5569 &h->root, r_type);
e641e783 5570 if (!TLS_RELOC_P (r_type)
020d7251
RS
5571 && !elf_hash_table (info)->dynamic_sections_created)
5572 /* This is a static link. We must initialize the GOT entry. */
a8028dd0 5573 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5574 }
5575 }
0a44bf69 5576 else if (!htab->is_vxworks
738e5348 5577 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5578 /* The calculation below does not involve "g". */
b49e97c9
TS
5579 break;
5580 else
5581 {
5c18022e 5582 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5583 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5584 if (g == MINUS_ONE)
5585 return bfd_reloc_outofrange;
5586 }
5587
5588 /* Convert GOT indices to actual offsets. */
a8028dd0 5589 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5590 break;
b49e97c9
TS
5591 }
5592
0a44bf69
RS
5593 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5594 symbols are resolved by the loader. Add them to .rela.dyn. */
5595 if (h != NULL && is_gott_symbol (info, &h->root))
5596 {
5597 Elf_Internal_Rela outrel;
5598 bfd_byte *loc;
5599 asection *s;
5600
5601 s = mips_elf_rel_dyn_section (info, FALSE);
5602 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5603
5604 outrel.r_offset = (input_section->output_section->vma
5605 + input_section->output_offset
5606 + relocation->r_offset);
5607 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5608 outrel.r_addend = addend;
5609 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5610
5611 /* If we've written this relocation for a readonly section,
5612 we need to set DF_TEXTREL again, so that we do not delete the
5613 DT_TEXTREL tag. */
5614 if (MIPS_ELF_READONLY_SECTION (input_section))
5615 info->flags |= DF_TEXTREL;
5616
0a44bf69
RS
5617 *valuep = 0;
5618 return bfd_reloc_ok;
5619 }
5620
b49e97c9
TS
5621 /* Figure out what kind of relocation is being performed. */
5622 switch (r_type)
5623 {
5624 case R_MIPS_NONE:
5625 return bfd_reloc_continue;
5626
5627 case R_MIPS_16:
a7ebbfdf 5628 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5629 overflowed_p = mips_elf_overflow_p (value, 16);
5630 break;
5631
5632 case R_MIPS_32:
5633 case R_MIPS_REL32:
5634 case R_MIPS_64:
5635 if ((info->shared
861fb55a 5636 || (htab->root.dynamic_sections_created
b49e97c9 5637 && h != NULL
f5385ebf 5638 && h->root.def_dynamic
861fb55a
DJ
5639 && !h->root.def_regular
5640 && !h->has_static_relocs))
cf35638d 5641 && r_symndx != STN_UNDEF
9a59ad6b
DJ
5642 && (h == NULL
5643 || h->root.root.type != bfd_link_hash_undefweak
5644 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5645 && (input_section->flags & SEC_ALLOC) != 0)
5646 {
861fb55a 5647 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5648 where the symbol will end up. So, we create a relocation
5649 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5650 linker. We must do the same for executable references to
5651 shared library symbols, unless we've decided to use copy
5652 relocs or PLTs instead. */
b49e97c9
TS
5653 value = addend;
5654 if (!mips_elf_create_dynamic_relocation (abfd,
5655 info,
5656 relocation,
5657 h,
5658 sec,
5659 symbol,
5660 &value,
5661 input_section))
5662 return bfd_reloc_undefined;
5663 }
5664 else
5665 {
5666 if (r_type != R_MIPS_REL32)
5667 value = symbol + addend;
5668 else
5669 value = addend;
5670 }
5671 value &= howto->dst_mask;
092dcd75
CD
5672 break;
5673
5674 case R_MIPS_PC32:
5675 value = symbol + addend - p;
5676 value &= howto->dst_mask;
b49e97c9
TS
5677 break;
5678
b49e97c9
TS
5679 case R_MIPS16_26:
5680 /* The calculation for R_MIPS16_26 is just the same as for an
5681 R_MIPS_26. It's only the storage of the relocated field into
5682 the output file that's different. That's handled in
5683 mips_elf_perform_relocation. So, we just fall through to the
5684 R_MIPS_26 case here. */
5685 case R_MIPS_26:
df58fc94
RS
5686 case R_MICROMIPS_26_S1:
5687 {
5688 unsigned int shift;
5689
5690 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5691 the correct ISA mode selector and bit 1 must be 0. */
5692 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5693 return bfd_reloc_outofrange;
5694
5695 /* Shift is 2, unusually, for microMIPS JALX. */
5696 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5697
5698 if (was_local_p)
5699 value = addend | ((p + 4) & (0xfc000000 << shift));
5700 else
5701 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5702 value = (value + symbol) >> shift;
5703 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5704 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5705 value &= howto->dst_mask;
5706 }
b49e97c9
TS
5707 break;
5708
0f20cc35 5709 case R_MIPS_TLS_DTPREL_HI16:
d0f13682 5710 case R_MIPS16_TLS_DTPREL_HI16:
df58fc94 5711 case R_MICROMIPS_TLS_DTPREL_HI16:
0f20cc35
DJ
5712 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5713 & howto->dst_mask);
5714 break;
5715
5716 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5717 case R_MIPS_TLS_DTPREL32:
5718 case R_MIPS_TLS_DTPREL64:
d0f13682 5719 case R_MIPS16_TLS_DTPREL_LO16:
df58fc94 5720 case R_MICROMIPS_TLS_DTPREL_LO16:
0f20cc35
DJ
5721 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5722 break;
5723
5724 case R_MIPS_TLS_TPREL_HI16:
d0f13682 5725 case R_MIPS16_TLS_TPREL_HI16:
df58fc94 5726 case R_MICROMIPS_TLS_TPREL_HI16:
0f20cc35
DJ
5727 value = (mips_elf_high (addend + symbol - tprel_base (info))
5728 & howto->dst_mask);
5729 break;
5730
5731 case R_MIPS_TLS_TPREL_LO16:
d0f13682
CLT
5732 case R_MIPS_TLS_TPREL32:
5733 case R_MIPS_TLS_TPREL64:
5734 case R_MIPS16_TLS_TPREL_LO16:
df58fc94 5735 case R_MICROMIPS_TLS_TPREL_LO16:
0f20cc35
DJ
5736 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5737 break;
5738
b49e97c9 5739 case R_MIPS_HI16:
d6f16593 5740 case R_MIPS16_HI16:
df58fc94 5741 case R_MICROMIPS_HI16:
b49e97c9
TS
5742 if (!gp_disp_p)
5743 {
5744 value = mips_elf_high (addend + symbol);
5745 value &= howto->dst_mask;
5746 }
5747 else
5748 {
d6f16593
MR
5749 /* For MIPS16 ABI code we generate this sequence
5750 0: li $v0,%hi(_gp_disp)
5751 4: addiupc $v1,%lo(_gp_disp)
5752 8: sll $v0,16
5753 12: addu $v0,$v1
5754 14: move $gp,$v0
5755 So the offsets of hi and lo relocs are the same, but the
888b9c01
CLT
5756 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5757 ADDIUPC clears the low two bits of the instruction address,
5758 so the base is ($t9 + 4) & ~3. */
d6f16593 5759 if (r_type == R_MIPS16_HI16)
888b9c01 5760 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
df58fc94
RS
5761 /* The microMIPS .cpload sequence uses the same assembly
5762 instructions as the traditional psABI version, but the
5763 incoming $t9 has the low bit set. */
5764 else if (r_type == R_MICROMIPS_HI16)
5765 value = mips_elf_high (addend + gp - p - 1);
d6f16593
MR
5766 else
5767 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5768 overflowed_p = mips_elf_overflow_p (value, 16);
5769 }
5770 break;
5771
5772 case R_MIPS_LO16:
d6f16593 5773 case R_MIPS16_LO16:
df58fc94
RS
5774 case R_MICROMIPS_LO16:
5775 case R_MICROMIPS_HI0_LO16:
b49e97c9
TS
5776 if (!gp_disp_p)
5777 value = (symbol + addend) & howto->dst_mask;
5778 else
5779 {
d6f16593
MR
5780 /* See the comment for R_MIPS16_HI16 above for the reason
5781 for this conditional. */
5782 if (r_type == R_MIPS16_LO16)
888b9c01 5783 value = addend + gp - (p & ~(bfd_vma) 0x3);
df58fc94
RS
5784 else if (r_type == R_MICROMIPS_LO16
5785 || r_type == R_MICROMIPS_HI0_LO16)
5786 value = addend + gp - p + 3;
d6f16593
MR
5787 else
5788 value = addend + gp - p + 4;
b49e97c9 5789 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5790 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5791 _gp_disp are normally generated from the .cpload
5792 pseudo-op. It generates code that normally looks like
5793 this:
5794
5795 lui $gp,%hi(_gp_disp)
5796 addiu $gp,$gp,%lo(_gp_disp)
5797 addu $gp,$gp,$t9
5798
5799 Here $t9 holds the address of the function being called,
5800 as required by the MIPS ELF ABI. The R_MIPS_LO16
5801 relocation can easily overflow in this situation, but the
5802 R_MIPS_HI16 relocation will handle the overflow.
5803 Therefore, we consider this a bug in the MIPS ABI, and do
5804 not check for overflow here. */
5805 }
5806 break;
5807
5808 case R_MIPS_LITERAL:
df58fc94 5809 case R_MICROMIPS_LITERAL:
b49e97c9
TS
5810 /* Because we don't merge literal sections, we can handle this
5811 just like R_MIPS_GPREL16. In the long run, we should merge
5812 shared literals, and then we will need to additional work
5813 here. */
5814
5815 /* Fall through. */
5816
5817 case R_MIPS16_GPREL:
5818 /* The R_MIPS16_GPREL performs the same calculation as
5819 R_MIPS_GPREL16, but stores the relocated bits in a different
5820 order. We don't need to do anything special here; the
5821 differences are handled in mips_elf_perform_relocation. */
5822 case R_MIPS_GPREL16:
df58fc94
RS
5823 case R_MICROMIPS_GPREL7_S2:
5824 case R_MICROMIPS_GPREL16:
bce03d3d
AO
5825 /* Only sign-extend the addend if it was extracted from the
5826 instruction. If the addend was separate, leave it alone,
5827 otherwise we may lose significant bits. */
5828 if (howto->partial_inplace)
a7ebbfdf 5829 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5830 value = symbol + addend - gp;
5831 /* If the symbol was local, any earlier relocatable links will
5832 have adjusted its addend with the gp offset, so compensate
5833 for that now. Don't do it for symbols forced local in this
5834 link, though, since they won't have had the gp offset applied
5835 to them before. */
5836 if (was_local_p)
5837 value += gp0;
b49e97c9
TS
5838 overflowed_p = mips_elf_overflow_p (value, 16);
5839 break;
5840
738e5348
RS
5841 case R_MIPS16_GOT16:
5842 case R_MIPS16_CALL16:
b49e97c9
TS
5843 case R_MIPS_GOT16:
5844 case R_MIPS_CALL16:
df58fc94
RS
5845 case R_MICROMIPS_GOT16:
5846 case R_MICROMIPS_CALL16:
0a44bf69 5847 /* VxWorks does not have separate local and global semantics for
738e5348 5848 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5849 if (!htab->is_vxworks && local_p)
b49e97c9 5850 {
5c18022e 5851 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 5852 symbol + addend, !was_local_p);
b49e97c9
TS
5853 if (value == MINUS_ONE)
5854 return bfd_reloc_outofrange;
5855 value
a8028dd0 5856 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5857 overflowed_p = mips_elf_overflow_p (value, 16);
5858 break;
5859 }
5860
5861 /* Fall through. */
5862
0f20cc35
DJ
5863 case R_MIPS_TLS_GD:
5864 case R_MIPS_TLS_GOTTPREL:
5865 case R_MIPS_TLS_LDM:
b49e97c9 5866 case R_MIPS_GOT_DISP:
d0f13682
CLT
5867 case R_MIPS16_TLS_GD:
5868 case R_MIPS16_TLS_GOTTPREL:
5869 case R_MIPS16_TLS_LDM:
df58fc94
RS
5870 case R_MICROMIPS_TLS_GD:
5871 case R_MICROMIPS_TLS_GOTTPREL:
5872 case R_MICROMIPS_TLS_LDM:
5873 case R_MICROMIPS_GOT_DISP:
b49e97c9
TS
5874 value = g;
5875 overflowed_p = mips_elf_overflow_p (value, 16);
5876 break;
5877
5878 case R_MIPS_GPREL32:
bce03d3d
AO
5879 value = (addend + symbol + gp0 - gp);
5880 if (!save_addend)
5881 value &= howto->dst_mask;
b49e97c9
TS
5882 break;
5883
5884 case R_MIPS_PC16:
bad36eac
DJ
5885 case R_MIPS_GNU_REL16_S2:
5886 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5887 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5888 value >>= howto->rightshift;
5889 value &= howto->dst_mask;
b49e97c9
TS
5890 break;
5891
df58fc94
RS
5892 case R_MICROMIPS_PC7_S1:
5893 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5894 overflowed_p = mips_elf_overflow_p (value, 8);
5895 value >>= howto->rightshift;
5896 value &= howto->dst_mask;
5897 break;
5898
5899 case R_MICROMIPS_PC10_S1:
5900 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5901 overflowed_p = mips_elf_overflow_p (value, 11);
5902 value >>= howto->rightshift;
5903 value &= howto->dst_mask;
5904 break;
5905
5906 case R_MICROMIPS_PC16_S1:
5907 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5908 overflowed_p = mips_elf_overflow_p (value, 17);
5909 value >>= howto->rightshift;
5910 value &= howto->dst_mask;
5911 break;
5912
5913 case R_MICROMIPS_PC23_S2:
5914 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5915 overflowed_p = mips_elf_overflow_p (value, 25);
5916 value >>= howto->rightshift;
5917 value &= howto->dst_mask;
5918 break;
5919
b49e97c9
TS
5920 case R_MIPS_GOT_HI16:
5921 case R_MIPS_CALL_HI16:
df58fc94
RS
5922 case R_MICROMIPS_GOT_HI16:
5923 case R_MICROMIPS_CALL_HI16:
b49e97c9
TS
5924 /* We're allowed to handle these two relocations identically.
5925 The dynamic linker is allowed to handle the CALL relocations
5926 differently by creating a lazy evaluation stub. */
5927 value = g;
5928 value = mips_elf_high (value);
5929 value &= howto->dst_mask;
5930 break;
5931
5932 case R_MIPS_GOT_LO16:
5933 case R_MIPS_CALL_LO16:
df58fc94
RS
5934 case R_MICROMIPS_GOT_LO16:
5935 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
5936 value = g & howto->dst_mask;
5937 break;
5938
5939 case R_MIPS_GOT_PAGE:
df58fc94 5940 case R_MICROMIPS_GOT_PAGE:
5c18022e 5941 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5942 if (value == MINUS_ONE)
5943 return bfd_reloc_outofrange;
a8028dd0 5944 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5945 overflowed_p = mips_elf_overflow_p (value, 16);
5946 break;
5947
5948 case R_MIPS_GOT_OFST:
df58fc94 5949 case R_MICROMIPS_GOT_OFST:
93a2b7ae 5950 if (local_p)
5c18022e 5951 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5952 else
5953 value = addend;
b49e97c9
TS
5954 overflowed_p = mips_elf_overflow_p (value, 16);
5955 break;
5956
5957 case R_MIPS_SUB:
df58fc94 5958 case R_MICROMIPS_SUB:
b49e97c9
TS
5959 value = symbol - addend;
5960 value &= howto->dst_mask;
5961 break;
5962
5963 case R_MIPS_HIGHER:
df58fc94 5964 case R_MICROMIPS_HIGHER:
b49e97c9
TS
5965 value = mips_elf_higher (addend + symbol);
5966 value &= howto->dst_mask;
5967 break;
5968
5969 case R_MIPS_HIGHEST:
df58fc94 5970 case R_MICROMIPS_HIGHEST:
b49e97c9
TS
5971 value = mips_elf_highest (addend + symbol);
5972 value &= howto->dst_mask;
5973 break;
5974
5975 case R_MIPS_SCN_DISP:
df58fc94 5976 case R_MICROMIPS_SCN_DISP:
b49e97c9
TS
5977 value = symbol + addend - sec->output_offset;
5978 value &= howto->dst_mask;
5979 break;
5980
b49e97c9 5981 case R_MIPS_JALR:
df58fc94 5982 case R_MICROMIPS_JALR:
1367d393
ILT
5983 /* This relocation is only a hint. In some cases, we optimize
5984 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5985 when the symbol does not resolve locally. */
5986 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5987 return bfd_reloc_continue;
5988 value = symbol + addend;
5989 break;
b49e97c9 5990
1367d393 5991 case R_MIPS_PJUMP:
b49e97c9
TS
5992 case R_MIPS_GNU_VTINHERIT:
5993 case R_MIPS_GNU_VTENTRY:
5994 /* We don't do anything with these at present. */
5995 return bfd_reloc_continue;
5996
5997 default:
5998 /* An unrecognized relocation type. */
5999 return bfd_reloc_notsupported;
6000 }
6001
6002 /* Store the VALUE for our caller. */
6003 *valuep = value;
6004 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6005}
6006
6007/* Obtain the field relocated by RELOCATION. */
6008
6009static bfd_vma
9719ad41
RS
6010mips_elf_obtain_contents (reloc_howto_type *howto,
6011 const Elf_Internal_Rela *relocation,
6012 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
6013{
6014 bfd_vma x;
6015 bfd_byte *location = contents + relocation->r_offset;
6016
6017 /* Obtain the bytes. */
6018 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
6019
b49e97c9
TS
6020 return x;
6021}
6022
6023/* It has been determined that the result of the RELOCATION is the
6024 VALUE. Use HOWTO to place VALUE into the output file at the
6025 appropriate position. The SECTION is the section to which the
68ffbac6 6026 relocation applies.
38a7df63 6027 CROSS_MODE_JUMP_P is true if the relocation field
df58fc94 6028 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9 6029
b34976b6 6030 Returns FALSE if anything goes wrong. */
b49e97c9 6031
b34976b6 6032static bfd_boolean
9719ad41
RS
6033mips_elf_perform_relocation (struct bfd_link_info *info,
6034 reloc_howto_type *howto,
6035 const Elf_Internal_Rela *relocation,
6036 bfd_vma value, bfd *input_bfd,
6037 asection *input_section, bfd_byte *contents,
38a7df63 6038 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
6039{
6040 bfd_vma x;
6041 bfd_byte *location;
6042 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6043
6044 /* Figure out where the relocation is occurring. */
6045 location = contents + relocation->r_offset;
6046
df58fc94 6047 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
d6f16593 6048
b49e97c9
TS
6049 /* Obtain the current value. */
6050 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6051
6052 /* Clear the field we are setting. */
6053 x &= ~howto->dst_mask;
6054
b49e97c9
TS
6055 /* Set the field. */
6056 x |= (value & howto->dst_mask);
6057
6058 /* If required, turn JAL into JALX. */
38a7df63 6059 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 6060 {
b34976b6 6061 bfd_boolean ok;
b49e97c9
TS
6062 bfd_vma opcode = x >> 26;
6063 bfd_vma jalx_opcode;
6064
6065 /* Check to see if the opcode is already JAL or JALX. */
6066 if (r_type == R_MIPS16_26)
6067 {
6068 ok = ((opcode == 0x6) || (opcode == 0x7));
6069 jalx_opcode = 0x7;
6070 }
df58fc94
RS
6071 else if (r_type == R_MICROMIPS_26_S1)
6072 {
6073 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6074 jalx_opcode = 0x3c;
6075 }
b49e97c9
TS
6076 else
6077 {
6078 ok = ((opcode == 0x3) || (opcode == 0x1d));
6079 jalx_opcode = 0x1d;
6080 }
6081
3bdf9505
MR
6082 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6083 convert J or JALS to JALX. */
b49e97c9
TS
6084 if (!ok)
6085 {
6086 (*_bfd_error_handler)
3bdf9505 6087 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
d003868e
AM
6088 input_bfd,
6089 input_section,
b49e97c9
TS
6090 (unsigned long) relocation->r_offset);
6091 bfd_set_error (bfd_error_bad_value);
b34976b6 6092 return FALSE;
b49e97c9
TS
6093 }
6094
6095 /* Make this the JALX opcode. */
6096 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6097 }
6098
38a7df63
CF
6099 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6100 range. */
cd8d5a82 6101 if (!info->relocatable
38a7df63 6102 && !cross_mode_jump_p
cd8d5a82
CF
6103 && ((JAL_TO_BAL_P (input_bfd)
6104 && r_type == R_MIPS_26
6105 && (x >> 26) == 0x3) /* jal addr */
6106 || (JALR_TO_BAL_P (input_bfd)
6107 && r_type == R_MIPS_JALR
38a7df63
CF
6108 && x == 0x0320f809) /* jalr t9 */
6109 || (JR_TO_B_P (input_bfd)
6110 && r_type == R_MIPS_JALR
6111 && x == 0x03200008))) /* jr t9 */
1367d393
ILT
6112 {
6113 bfd_vma addr;
6114 bfd_vma dest;
6115 bfd_signed_vma off;
6116
6117 addr = (input_section->output_section->vma
6118 + input_section->output_offset
6119 + relocation->r_offset
6120 + 4);
6121 if (r_type == R_MIPS_26)
6122 dest = (value << 2) | ((addr >> 28) << 28);
6123 else
6124 dest = value;
6125 off = dest - addr;
6126 if (off <= 0x1ffff && off >= -0x20000)
38a7df63
CF
6127 {
6128 if (x == 0x03200008) /* jr t9 */
6129 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6130 else
6131 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6132 }
1367d393
ILT
6133 }
6134
b49e97c9
TS
6135 /* Put the value into the output. */
6136 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593 6137
df58fc94
RS
6138 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
6139 location);
d6f16593 6140
b34976b6 6141 return TRUE;
b49e97c9 6142}
b49e97c9 6143\f
b49e97c9
TS
6144/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6145 is the original relocation, which is now being transformed into a
6146 dynamic relocation. The ADDENDP is adjusted if necessary; the
6147 caller should store the result in place of the original addend. */
6148
b34976b6 6149static bfd_boolean
9719ad41
RS
6150mips_elf_create_dynamic_relocation (bfd *output_bfd,
6151 struct bfd_link_info *info,
6152 const Elf_Internal_Rela *rel,
6153 struct mips_elf_link_hash_entry *h,
6154 asection *sec, bfd_vma symbol,
6155 bfd_vma *addendp, asection *input_section)
b49e97c9 6156{
947216bf 6157 Elf_Internal_Rela outrel[3];
b49e97c9
TS
6158 asection *sreloc;
6159 bfd *dynobj;
6160 int r_type;
5d41f0b6
RS
6161 long indx;
6162 bfd_boolean defined_p;
0a44bf69 6163 struct mips_elf_link_hash_table *htab;
b49e97c9 6164
0a44bf69 6165 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6166 BFD_ASSERT (htab != NULL);
6167
b49e97c9
TS
6168 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6169 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 6170 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
6171 BFD_ASSERT (sreloc != NULL);
6172 BFD_ASSERT (sreloc->contents != NULL);
6173 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 6174 < sreloc->size);
b49e97c9 6175
b49e97c9
TS
6176 outrel[0].r_offset =
6177 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
6178 if (ABI_64_P (output_bfd))
6179 {
6180 outrel[1].r_offset =
6181 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6182 outrel[2].r_offset =
6183 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6184 }
b49e97c9 6185
c5ae1840 6186 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 6187 /* The relocation field has been deleted. */
5d41f0b6
RS
6188 return TRUE;
6189
6190 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
6191 {
6192 /* The relocation field has been converted into a relative value of
6193 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6194 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 6195 *addendp += symbol;
5d41f0b6 6196 return TRUE;
0d591ff7 6197 }
b49e97c9 6198
5d41f0b6
RS
6199 /* We must now calculate the dynamic symbol table index to use
6200 in the relocation. */
d4a77f3f 6201 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 6202 {
020d7251 6203 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5d41f0b6
RS
6204 indx = h->root.dynindx;
6205 if (SGI_COMPAT (output_bfd))
6206 defined_p = h->root.def_regular;
6207 else
6208 /* ??? glibc's ld.so just adds the final GOT entry to the
6209 relocation field. It therefore treats relocs against
6210 defined symbols in the same way as relocs against
6211 undefined symbols. */
6212 defined_p = FALSE;
6213 }
b49e97c9
TS
6214 else
6215 {
5d41f0b6
RS
6216 if (sec != NULL && bfd_is_abs_section (sec))
6217 indx = 0;
6218 else if (sec == NULL || sec->owner == NULL)
fdd07405 6219 {
5d41f0b6
RS
6220 bfd_set_error (bfd_error_bad_value);
6221 return FALSE;
b49e97c9
TS
6222 }
6223 else
6224 {
5d41f0b6 6225 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
6226 if (indx == 0)
6227 {
6228 asection *osec = htab->root.text_index_section;
6229 indx = elf_section_data (osec)->dynindx;
6230 }
5d41f0b6
RS
6231 if (indx == 0)
6232 abort ();
b49e97c9
TS
6233 }
6234
5d41f0b6
RS
6235 /* Instead of generating a relocation using the section
6236 symbol, we may as well make it a fully relative
6237 relocation. We want to avoid generating relocations to
6238 local symbols because we used to generate them
6239 incorrectly, without adding the original symbol value,
6240 which is mandated by the ABI for section symbols. In
6241 order to give dynamic loaders and applications time to
6242 phase out the incorrect use, we refrain from emitting
6243 section-relative relocations. It's not like they're
6244 useful, after all. This should be a bit more efficient
6245 as well. */
6246 /* ??? Although this behavior is compatible with glibc's ld.so,
6247 the ABI says that relocations against STN_UNDEF should have
6248 a symbol value of 0. Irix rld honors this, so relocations
6249 against STN_UNDEF have no effect. */
6250 if (!SGI_COMPAT (output_bfd))
6251 indx = 0;
6252 defined_p = TRUE;
b49e97c9
TS
6253 }
6254
5d41f0b6
RS
6255 /* If the relocation was previously an absolute relocation and
6256 this symbol will not be referred to by the relocation, we must
6257 adjust it by the value we give it in the dynamic symbol table.
6258 Otherwise leave the job up to the dynamic linker. */
6259 if (defined_p && r_type != R_MIPS_REL32)
6260 *addendp += symbol;
6261
0a44bf69
RS
6262 if (htab->is_vxworks)
6263 /* VxWorks uses non-relative relocations for this. */
6264 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6265 else
6266 /* The relocation is always an REL32 relocation because we don't
6267 know where the shared library will wind up at load-time. */
6268 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6269 R_MIPS_REL32);
6270
5d41f0b6
RS
6271 /* For strict adherence to the ABI specification, we should
6272 generate a R_MIPS_64 relocation record by itself before the
6273 _REL32/_64 record as well, such that the addend is read in as
6274 a 64-bit value (REL32 is a 32-bit relocation, after all).
6275 However, since none of the existing ELF64 MIPS dynamic
6276 loaders seems to care, we don't waste space with these
6277 artificial relocations. If this turns out to not be true,
6278 mips_elf_allocate_dynamic_relocation() should be tweaked so
6279 as to make room for a pair of dynamic relocations per
6280 invocation if ABI_64_P, and here we should generate an
6281 additional relocation record with R_MIPS_64 by itself for a
6282 NULL symbol before this relocation record. */
6283 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6284 ABI_64_P (output_bfd)
6285 ? R_MIPS_64
6286 : R_MIPS_NONE);
6287 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6288
6289 /* Adjust the output offset of the relocation to reference the
6290 correct location in the output file. */
6291 outrel[0].r_offset += (input_section->output_section->vma
6292 + input_section->output_offset);
6293 outrel[1].r_offset += (input_section->output_section->vma
6294 + input_section->output_offset);
6295 outrel[2].r_offset += (input_section->output_section->vma
6296 + input_section->output_offset);
6297
b49e97c9
TS
6298 /* Put the relocation back out. We have to use the special
6299 relocation outputter in the 64-bit case since the 64-bit
6300 relocation format is non-standard. */
6301 if (ABI_64_P (output_bfd))
6302 {
6303 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6304 (output_bfd, &outrel[0],
6305 (sreloc->contents
6306 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6307 }
0a44bf69
RS
6308 else if (htab->is_vxworks)
6309 {
6310 /* VxWorks uses RELA rather than REL dynamic relocations. */
6311 outrel[0].r_addend = *addendp;
6312 bfd_elf32_swap_reloca_out
6313 (output_bfd, &outrel[0],
6314 (sreloc->contents
6315 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6316 }
b49e97c9 6317 else
947216bf
AM
6318 bfd_elf32_swap_reloc_out
6319 (output_bfd, &outrel[0],
6320 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 6321
b49e97c9
TS
6322 /* We've now added another relocation. */
6323 ++sreloc->reloc_count;
6324
6325 /* Make sure the output section is writable. The dynamic linker
6326 will be writing to it. */
6327 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6328 |= SHF_WRITE;
6329
6330 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 6331 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9 6332 {
3d4d4302 6333 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
6334 bfd_byte *cr;
6335
6336 if (scpt)
6337 {
6338 Elf32_crinfo cptrel;
6339
6340 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6341 cptrel.vaddr = (rel->r_offset
6342 + input_section->output_section->vma
6343 + input_section->output_offset);
6344 if (r_type == R_MIPS_REL32)
6345 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6346 else
6347 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6348 mips_elf_set_cr_dist2to (cptrel, 0);
6349 cptrel.konst = *addendp;
6350
6351 cr = (scpt->contents
6352 + sizeof (Elf32_External_compact_rel));
abc0f8d0 6353 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
6354 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6355 ((Elf32_External_crinfo *) cr
6356 + scpt->reloc_count));
6357 ++scpt->reloc_count;
6358 }
6359 }
6360
943284cc
DJ
6361 /* If we've written this relocation for a readonly section,
6362 we need to set DF_TEXTREL again, so that we do not delete the
6363 DT_TEXTREL tag. */
6364 if (MIPS_ELF_READONLY_SECTION (input_section))
6365 info->flags |= DF_TEXTREL;
6366
b34976b6 6367 return TRUE;
b49e97c9
TS
6368}
6369\f
b49e97c9
TS
6370/* Return the MACH for a MIPS e_flags value. */
6371
6372unsigned long
9719ad41 6373_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
6374{
6375 switch (flags & EF_MIPS_MACH)
6376 {
6377 case E_MIPS_MACH_3900:
6378 return bfd_mach_mips3900;
6379
6380 case E_MIPS_MACH_4010:
6381 return bfd_mach_mips4010;
6382
6383 case E_MIPS_MACH_4100:
6384 return bfd_mach_mips4100;
6385
6386 case E_MIPS_MACH_4111:
6387 return bfd_mach_mips4111;
6388
00707a0e
RS
6389 case E_MIPS_MACH_4120:
6390 return bfd_mach_mips4120;
6391
b49e97c9
TS
6392 case E_MIPS_MACH_4650:
6393 return bfd_mach_mips4650;
6394
00707a0e
RS
6395 case E_MIPS_MACH_5400:
6396 return bfd_mach_mips5400;
6397
6398 case E_MIPS_MACH_5500:
6399 return bfd_mach_mips5500;
6400
e407c74b
NC
6401 case E_MIPS_MACH_5900:
6402 return bfd_mach_mips5900;
6403
0d2e43ed
ILT
6404 case E_MIPS_MACH_9000:
6405 return bfd_mach_mips9000;
6406
b49e97c9
TS
6407 case E_MIPS_MACH_SB1:
6408 return bfd_mach_mips_sb1;
6409
350cc38d
MS
6410 case E_MIPS_MACH_LS2E:
6411 return bfd_mach_mips_loongson_2e;
6412
6413 case E_MIPS_MACH_LS2F:
6414 return bfd_mach_mips_loongson_2f;
6415
fd503541
NC
6416 case E_MIPS_MACH_LS3A:
6417 return bfd_mach_mips_loongson_3a;
6418
432233b3
AP
6419 case E_MIPS_MACH_OCTEON2:
6420 return bfd_mach_mips_octeon2;
6421
6f179bd0
AN
6422 case E_MIPS_MACH_OCTEON:
6423 return bfd_mach_mips_octeon;
6424
52b6b6b9
JM
6425 case E_MIPS_MACH_XLR:
6426 return bfd_mach_mips_xlr;
6427
b49e97c9
TS
6428 default:
6429 switch (flags & EF_MIPS_ARCH)
6430 {
6431 default:
6432 case E_MIPS_ARCH_1:
6433 return bfd_mach_mips3000;
b49e97c9
TS
6434
6435 case E_MIPS_ARCH_2:
6436 return bfd_mach_mips6000;
b49e97c9
TS
6437
6438 case E_MIPS_ARCH_3:
6439 return bfd_mach_mips4000;
b49e97c9
TS
6440
6441 case E_MIPS_ARCH_4:
6442 return bfd_mach_mips8000;
b49e97c9
TS
6443
6444 case E_MIPS_ARCH_5:
6445 return bfd_mach_mips5;
b49e97c9
TS
6446
6447 case E_MIPS_ARCH_32:
6448 return bfd_mach_mipsisa32;
b49e97c9
TS
6449
6450 case E_MIPS_ARCH_64:
6451 return bfd_mach_mipsisa64;
af7ee8bf
CD
6452
6453 case E_MIPS_ARCH_32R2:
6454 return bfd_mach_mipsisa32r2;
5f74bc13
CD
6455
6456 case E_MIPS_ARCH_64R2:
6457 return bfd_mach_mipsisa64r2;
b49e97c9
TS
6458 }
6459 }
6460
6461 return 0;
6462}
6463
6464/* Return printable name for ABI. */
6465
6466static INLINE char *
9719ad41 6467elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
6468{
6469 flagword flags;
6470
6471 flags = elf_elfheader (abfd)->e_flags;
6472 switch (flags & EF_MIPS_ABI)
6473 {
6474 case 0:
6475 if (ABI_N32_P (abfd))
6476 return "N32";
6477 else if (ABI_64_P (abfd))
6478 return "64";
6479 else
6480 return "none";
6481 case E_MIPS_ABI_O32:
6482 return "O32";
6483 case E_MIPS_ABI_O64:
6484 return "O64";
6485 case E_MIPS_ABI_EABI32:
6486 return "EABI32";
6487 case E_MIPS_ABI_EABI64:
6488 return "EABI64";
6489 default:
6490 return "unknown abi";
6491 }
6492}
6493\f
6494/* MIPS ELF uses two common sections. One is the usual one, and the
6495 other is for small objects. All the small objects are kept
6496 together, and then referenced via the gp pointer, which yields
6497 faster assembler code. This is what we use for the small common
6498 section. This approach is copied from ecoff.c. */
6499static asection mips_elf_scom_section;
6500static asymbol mips_elf_scom_symbol;
6501static asymbol *mips_elf_scom_symbol_ptr;
6502
6503/* MIPS ELF also uses an acommon section, which represents an
6504 allocated common symbol which may be overridden by a
6505 definition in a shared library. */
6506static asection mips_elf_acom_section;
6507static asymbol mips_elf_acom_symbol;
6508static asymbol *mips_elf_acom_symbol_ptr;
6509
738e5348 6510/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
6511
6512void
9719ad41 6513_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
6514{
6515 elf_symbol_type *elfsym;
6516
738e5348 6517 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6518 elfsym = (elf_symbol_type *) asym;
6519 switch (elfsym->internal_elf_sym.st_shndx)
6520 {
6521 case SHN_MIPS_ACOMMON:
6522 /* This section is used in a dynamically linked executable file.
6523 It is an allocated common section. The dynamic linker can
6524 either resolve these symbols to something in a shared
6525 library, or it can just leave them here. For our purposes,
6526 we can consider these symbols to be in a new section. */
6527 if (mips_elf_acom_section.name == NULL)
6528 {
6529 /* Initialize the acommon section. */
6530 mips_elf_acom_section.name = ".acommon";
6531 mips_elf_acom_section.flags = SEC_ALLOC;
6532 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6533 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6534 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6535 mips_elf_acom_symbol.name = ".acommon";
6536 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6537 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6538 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6539 }
6540 asym->section = &mips_elf_acom_section;
6541 break;
6542
6543 case SHN_COMMON:
6544 /* Common symbols less than the GP size are automatically
6545 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6546 if (asym->value > elf_gp_size (abfd)
b59eed79 6547 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6548 || IRIX_COMPAT (abfd) == ict_irix6)
6549 break;
6550 /* Fall through. */
6551 case SHN_MIPS_SCOMMON:
6552 if (mips_elf_scom_section.name == NULL)
6553 {
6554 /* Initialize the small common section. */
6555 mips_elf_scom_section.name = ".scommon";
6556 mips_elf_scom_section.flags = SEC_IS_COMMON;
6557 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6558 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6559 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6560 mips_elf_scom_symbol.name = ".scommon";
6561 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6562 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6563 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6564 }
6565 asym->section = &mips_elf_scom_section;
6566 asym->value = elfsym->internal_elf_sym.st_size;
6567 break;
6568
6569 case SHN_MIPS_SUNDEFINED:
6570 asym->section = bfd_und_section_ptr;
6571 break;
6572
b49e97c9 6573 case SHN_MIPS_TEXT:
00b4930b
TS
6574 {
6575 asection *section = bfd_get_section_by_name (abfd, ".text");
6576
00b4930b
TS
6577 if (section != NULL)
6578 {
6579 asym->section = section;
6580 /* MIPS_TEXT is a bit special, the address is not an offset
6581 to the base of the .text section. So substract the section
6582 base address to make it an offset. */
6583 asym->value -= section->vma;
6584 }
6585 }
b49e97c9
TS
6586 break;
6587
6588 case SHN_MIPS_DATA:
00b4930b
TS
6589 {
6590 asection *section = bfd_get_section_by_name (abfd, ".data");
6591
00b4930b
TS
6592 if (section != NULL)
6593 {
6594 asym->section = section;
6595 /* MIPS_DATA is a bit special, the address is not an offset
6596 to the base of the .data section. So substract the section
6597 base address to make it an offset. */
6598 asym->value -= section->vma;
6599 }
6600 }
b49e97c9 6601 break;
b49e97c9 6602 }
738e5348 6603
df58fc94
RS
6604 /* If this is an odd-valued function symbol, assume it's a MIPS16
6605 or microMIPS one. */
738e5348
RS
6606 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6607 && (asym->value & 1) != 0)
6608 {
6609 asym->value--;
e8faf7d1 6610 if (MICROMIPS_P (abfd))
df58fc94
RS
6611 elfsym->internal_elf_sym.st_other
6612 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6613 else
6614 elfsym->internal_elf_sym.st_other
6615 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
738e5348 6616 }
b49e97c9
TS
6617}
6618\f
8c946ed5
RS
6619/* Implement elf_backend_eh_frame_address_size. This differs from
6620 the default in the way it handles EABI64.
6621
6622 EABI64 was originally specified as an LP64 ABI, and that is what
6623 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6624 historically accepted the combination of -mabi=eabi and -mlong32,
6625 and this ILP32 variation has become semi-official over time.
6626 Both forms use elf32 and have pointer-sized FDE addresses.
6627
6628 If an EABI object was generated by GCC 4.0 or above, it will have
6629 an empty .gcc_compiled_longXX section, where XX is the size of longs
6630 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6631 have no special marking to distinguish them from LP64 objects.
6632
6633 We don't want users of the official LP64 ABI to be punished for the
6634 existence of the ILP32 variant, but at the same time, we don't want
6635 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6636 We therefore take the following approach:
6637
6638 - If ABFD contains a .gcc_compiled_longXX section, use it to
6639 determine the pointer size.
6640
6641 - Otherwise check the type of the first relocation. Assume that
6642 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6643
6644 - Otherwise punt.
6645
6646 The second check is enough to detect LP64 objects generated by pre-4.0
6647 compilers because, in the kind of output generated by those compilers,
6648 the first relocation will be associated with either a CIE personality
6649 routine or an FDE start address. Furthermore, the compilers never
6650 used a special (non-pointer) encoding for this ABI.
6651
6652 Checking the relocation type should also be safe because there is no
6653 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6654 did so. */
6655
6656unsigned int
6657_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6658{
6659 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6660 return 8;
6661 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6662 {
6663 bfd_boolean long32_p, long64_p;
6664
6665 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6666 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6667 if (long32_p && long64_p)
6668 return 0;
6669 if (long32_p)
6670 return 4;
6671 if (long64_p)
6672 return 8;
6673
6674 if (sec->reloc_count > 0
6675 && elf_section_data (sec)->relocs != NULL
6676 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6677 == R_MIPS_64))
6678 return 8;
6679
6680 return 0;
6681 }
6682 return 4;
6683}
6684\f
174fd7f9
RS
6685/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6686 relocations against two unnamed section symbols to resolve to the
6687 same address. For example, if we have code like:
6688
6689 lw $4,%got_disp(.data)($gp)
6690 lw $25,%got_disp(.text)($gp)
6691 jalr $25
6692
6693 then the linker will resolve both relocations to .data and the program
6694 will jump there rather than to .text.
6695
6696 We can work around this problem by giving names to local section symbols.
6697 This is also what the MIPSpro tools do. */
6698
6699bfd_boolean
6700_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6701{
6702 return SGI_COMPAT (abfd);
6703}
6704\f
b49e97c9
TS
6705/* Work over a section just before writing it out. This routine is
6706 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6707 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6708 a better way. */
6709
b34976b6 6710bfd_boolean
9719ad41 6711_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6712{
6713 if (hdr->sh_type == SHT_MIPS_REGINFO
6714 && hdr->sh_size > 0)
6715 {
6716 bfd_byte buf[4];
6717
6718 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6719 BFD_ASSERT (hdr->contents == NULL);
6720
6721 if (bfd_seek (abfd,
6722 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6723 SEEK_SET) != 0)
b34976b6 6724 return FALSE;
b49e97c9 6725 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6726 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6727 return FALSE;
b49e97c9
TS
6728 }
6729
6730 if (hdr->sh_type == SHT_MIPS_OPTIONS
6731 && hdr->bfd_section != NULL
f0abc2a1
AM
6732 && mips_elf_section_data (hdr->bfd_section) != NULL
6733 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6734 {
6735 bfd_byte *contents, *l, *lend;
6736
f0abc2a1
AM
6737 /* We stored the section contents in the tdata field in the
6738 set_section_contents routine. We save the section contents
6739 so that we don't have to read them again.
b49e97c9
TS
6740 At this point we know that elf_gp is set, so we can look
6741 through the section contents to see if there is an
6742 ODK_REGINFO structure. */
6743
f0abc2a1 6744 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6745 l = contents;
6746 lend = contents + hdr->sh_size;
6747 while (l + sizeof (Elf_External_Options) <= lend)
6748 {
6749 Elf_Internal_Options intopt;
6750
6751 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6752 &intopt);
1bc8074d
MR
6753 if (intopt.size < sizeof (Elf_External_Options))
6754 {
6755 (*_bfd_error_handler)
6756 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6757 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6758 break;
6759 }
b49e97c9
TS
6760 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6761 {
6762 bfd_byte buf[8];
6763
6764 if (bfd_seek (abfd,
6765 (hdr->sh_offset
6766 + (l - contents)
6767 + sizeof (Elf_External_Options)
6768 + (sizeof (Elf64_External_RegInfo) - 8)),
6769 SEEK_SET) != 0)
b34976b6 6770 return FALSE;
b49e97c9 6771 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6772 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6773 return FALSE;
b49e97c9
TS
6774 }
6775 else if (intopt.kind == ODK_REGINFO)
6776 {
6777 bfd_byte buf[4];
6778
6779 if (bfd_seek (abfd,
6780 (hdr->sh_offset
6781 + (l - contents)
6782 + sizeof (Elf_External_Options)
6783 + (sizeof (Elf32_External_RegInfo) - 4)),
6784 SEEK_SET) != 0)
b34976b6 6785 return FALSE;
b49e97c9 6786 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6787 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6788 return FALSE;
b49e97c9
TS
6789 }
6790 l += intopt.size;
6791 }
6792 }
6793
6794 if (hdr->bfd_section != NULL)
6795 {
6796 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6797
2d0f9ad9
JM
6798 /* .sbss is not handled specially here because the GNU/Linux
6799 prelinker can convert .sbss from NOBITS to PROGBITS and
6800 changing it back to NOBITS breaks the binary. The entry in
6801 _bfd_mips_elf_special_sections will ensure the correct flags
6802 are set on .sbss if BFD creates it without reading it from an
6803 input file, and without special handling here the flags set
6804 on it in an input file will be followed. */
b49e97c9
TS
6805 if (strcmp (name, ".sdata") == 0
6806 || strcmp (name, ".lit8") == 0
6807 || strcmp (name, ".lit4") == 0)
6808 {
6809 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6810 hdr->sh_type = SHT_PROGBITS;
6811 }
b49e97c9
TS
6812 else if (strcmp (name, ".srdata") == 0)
6813 {
6814 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6815 hdr->sh_type = SHT_PROGBITS;
6816 }
6817 else if (strcmp (name, ".compact_rel") == 0)
6818 {
6819 hdr->sh_flags = 0;
6820 hdr->sh_type = SHT_PROGBITS;
6821 }
6822 else if (strcmp (name, ".rtproc") == 0)
6823 {
6824 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6825 {
6826 unsigned int adjust;
6827
6828 adjust = hdr->sh_size % hdr->sh_addralign;
6829 if (adjust != 0)
6830 hdr->sh_size += hdr->sh_addralign - adjust;
6831 }
6832 }
6833 }
6834
b34976b6 6835 return TRUE;
b49e97c9
TS
6836}
6837
6838/* Handle a MIPS specific section when reading an object file. This
6839 is called when elfcode.h finds a section with an unknown type.
6840 This routine supports both the 32-bit and 64-bit ELF ABI.
6841
6842 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6843 how to. */
6844
b34976b6 6845bfd_boolean
6dc132d9
L
6846_bfd_mips_elf_section_from_shdr (bfd *abfd,
6847 Elf_Internal_Shdr *hdr,
6848 const char *name,
6849 int shindex)
b49e97c9
TS
6850{
6851 flagword flags = 0;
6852
6853 /* There ought to be a place to keep ELF backend specific flags, but
6854 at the moment there isn't one. We just keep track of the
6855 sections by their name, instead. Fortunately, the ABI gives
6856 suggested names for all the MIPS specific sections, so we will
6857 probably get away with this. */
6858 switch (hdr->sh_type)
6859 {
6860 case SHT_MIPS_LIBLIST:
6861 if (strcmp (name, ".liblist") != 0)
b34976b6 6862 return FALSE;
b49e97c9
TS
6863 break;
6864 case SHT_MIPS_MSYM:
6865 if (strcmp (name, ".msym") != 0)
b34976b6 6866 return FALSE;
b49e97c9
TS
6867 break;
6868 case SHT_MIPS_CONFLICT:
6869 if (strcmp (name, ".conflict") != 0)
b34976b6 6870 return FALSE;
b49e97c9
TS
6871 break;
6872 case SHT_MIPS_GPTAB:
0112cd26 6873 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6874 return FALSE;
b49e97c9
TS
6875 break;
6876 case SHT_MIPS_UCODE:
6877 if (strcmp (name, ".ucode") != 0)
b34976b6 6878 return FALSE;
b49e97c9
TS
6879 break;
6880 case SHT_MIPS_DEBUG:
6881 if (strcmp (name, ".mdebug") != 0)
b34976b6 6882 return FALSE;
b49e97c9
TS
6883 flags = SEC_DEBUGGING;
6884 break;
6885 case SHT_MIPS_REGINFO:
6886 if (strcmp (name, ".reginfo") != 0
6887 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6888 return FALSE;
b49e97c9
TS
6889 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6890 break;
6891 case SHT_MIPS_IFACE:
6892 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6893 return FALSE;
b49e97c9
TS
6894 break;
6895 case SHT_MIPS_CONTENT:
0112cd26 6896 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6897 return FALSE;
b49e97c9
TS
6898 break;
6899 case SHT_MIPS_OPTIONS:
cc2e31b9 6900 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6901 return FALSE;
b49e97c9
TS
6902 break;
6903 case SHT_MIPS_DWARF:
1b315056 6904 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6905 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6906 return FALSE;
b49e97c9
TS
6907 break;
6908 case SHT_MIPS_SYMBOL_LIB:
6909 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6910 return FALSE;
b49e97c9
TS
6911 break;
6912 case SHT_MIPS_EVENTS:
0112cd26
NC
6913 if (! CONST_STRNEQ (name, ".MIPS.events")
6914 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6915 return FALSE;
b49e97c9
TS
6916 break;
6917 default:
cc2e31b9 6918 break;
b49e97c9
TS
6919 }
6920
6dc132d9 6921 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6922 return FALSE;
b49e97c9
TS
6923
6924 if (flags)
6925 {
6926 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6927 (bfd_get_section_flags (abfd,
6928 hdr->bfd_section)
6929 | flags)))
b34976b6 6930 return FALSE;
b49e97c9
TS
6931 }
6932
6933 /* FIXME: We should record sh_info for a .gptab section. */
6934
6935 /* For a .reginfo section, set the gp value in the tdata information
6936 from the contents of this section. We need the gp value while
6937 processing relocs, so we just get it now. The .reginfo section
6938 is not used in the 64-bit MIPS ELF ABI. */
6939 if (hdr->sh_type == SHT_MIPS_REGINFO)
6940 {
6941 Elf32_External_RegInfo ext;
6942 Elf32_RegInfo s;
6943
9719ad41
RS
6944 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6945 &ext, 0, sizeof ext))
b34976b6 6946 return FALSE;
b49e97c9
TS
6947 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6948 elf_gp (abfd) = s.ri_gp_value;
6949 }
6950
6951 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6952 set the gp value based on what we find. We may see both
6953 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6954 they should agree. */
6955 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6956 {
6957 bfd_byte *contents, *l, *lend;
6958
9719ad41 6959 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6960 if (contents == NULL)
b34976b6 6961 return FALSE;
b49e97c9 6962 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6963 0, hdr->sh_size))
b49e97c9
TS
6964 {
6965 free (contents);
b34976b6 6966 return FALSE;
b49e97c9
TS
6967 }
6968 l = contents;
6969 lend = contents + hdr->sh_size;
6970 while (l + sizeof (Elf_External_Options) <= lend)
6971 {
6972 Elf_Internal_Options intopt;
6973
6974 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6975 &intopt);
1bc8074d
MR
6976 if (intopt.size < sizeof (Elf_External_Options))
6977 {
6978 (*_bfd_error_handler)
6979 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6980 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6981 break;
6982 }
b49e97c9
TS
6983 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6984 {
6985 Elf64_Internal_RegInfo intreg;
6986
6987 bfd_mips_elf64_swap_reginfo_in
6988 (abfd,
6989 ((Elf64_External_RegInfo *)
6990 (l + sizeof (Elf_External_Options))),
6991 &intreg);
6992 elf_gp (abfd) = intreg.ri_gp_value;
6993 }
6994 else if (intopt.kind == ODK_REGINFO)
6995 {
6996 Elf32_RegInfo intreg;
6997
6998 bfd_mips_elf32_swap_reginfo_in
6999 (abfd,
7000 ((Elf32_External_RegInfo *)
7001 (l + sizeof (Elf_External_Options))),
7002 &intreg);
7003 elf_gp (abfd) = intreg.ri_gp_value;
7004 }
7005 l += intopt.size;
7006 }
7007 free (contents);
7008 }
7009
b34976b6 7010 return TRUE;
b49e97c9
TS
7011}
7012
7013/* Set the correct type for a MIPS ELF section. We do this by the
7014 section name, which is a hack, but ought to work. This routine is
7015 used by both the 32-bit and the 64-bit ABI. */
7016
b34976b6 7017bfd_boolean
9719ad41 7018_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 7019{
0414f35b 7020 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
7021
7022 if (strcmp (name, ".liblist") == 0)
7023 {
7024 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 7025 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
7026 /* The sh_link field is set in final_write_processing. */
7027 }
7028 else if (strcmp (name, ".conflict") == 0)
7029 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 7030 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
7031 {
7032 hdr->sh_type = SHT_MIPS_GPTAB;
7033 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7034 /* The sh_info field is set in final_write_processing. */
7035 }
7036 else if (strcmp (name, ".ucode") == 0)
7037 hdr->sh_type = SHT_MIPS_UCODE;
7038 else if (strcmp (name, ".mdebug") == 0)
7039 {
7040 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 7041 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
7042 entsize of 0. FIXME: Does this matter? */
7043 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7044 hdr->sh_entsize = 0;
7045 else
7046 hdr->sh_entsize = 1;
7047 }
7048 else if (strcmp (name, ".reginfo") == 0)
7049 {
7050 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 7051 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
7052 entsize of 0x18. FIXME: Does this matter? */
7053 if (SGI_COMPAT (abfd))
7054 {
7055 if ((abfd->flags & DYNAMIC) != 0)
7056 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7057 else
7058 hdr->sh_entsize = 1;
7059 }
7060 else
7061 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7062 }
7063 else if (SGI_COMPAT (abfd)
7064 && (strcmp (name, ".hash") == 0
7065 || strcmp (name, ".dynamic") == 0
7066 || strcmp (name, ".dynstr") == 0))
7067 {
7068 if (SGI_COMPAT (abfd))
7069 hdr->sh_entsize = 0;
7070#if 0
8dc1a139 7071 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
7072 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7073#endif
7074 }
7075 else if (strcmp (name, ".got") == 0
7076 || strcmp (name, ".srdata") == 0
7077 || strcmp (name, ".sdata") == 0
7078 || strcmp (name, ".sbss") == 0
7079 || strcmp (name, ".lit4") == 0
7080 || strcmp (name, ".lit8") == 0)
7081 hdr->sh_flags |= SHF_MIPS_GPREL;
7082 else if (strcmp (name, ".MIPS.interfaces") == 0)
7083 {
7084 hdr->sh_type = SHT_MIPS_IFACE;
7085 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7086 }
0112cd26 7087 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
7088 {
7089 hdr->sh_type = SHT_MIPS_CONTENT;
7090 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7091 /* The sh_info field is set in final_write_processing. */
7092 }
cc2e31b9 7093 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
7094 {
7095 hdr->sh_type = SHT_MIPS_OPTIONS;
7096 hdr->sh_entsize = 1;
7097 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7098 }
1b315056
CS
7099 else if (CONST_STRNEQ (name, ".debug_")
7100 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
7101 {
7102 hdr->sh_type = SHT_MIPS_DWARF;
7103
7104 /* Irix facilities such as libexc expect a single .debug_frame
7105 per executable, the system ones have NOSTRIP set and the linker
7106 doesn't merge sections with different flags so ... */
7107 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7108 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7109 }
b49e97c9
TS
7110 else if (strcmp (name, ".MIPS.symlib") == 0)
7111 {
7112 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7113 /* The sh_link and sh_info fields are set in
7114 final_write_processing. */
7115 }
0112cd26
NC
7116 else if (CONST_STRNEQ (name, ".MIPS.events")
7117 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
7118 {
7119 hdr->sh_type = SHT_MIPS_EVENTS;
7120 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7121 /* The sh_link field is set in final_write_processing. */
7122 }
7123 else if (strcmp (name, ".msym") == 0)
7124 {
7125 hdr->sh_type = SHT_MIPS_MSYM;
7126 hdr->sh_flags |= SHF_ALLOC;
7127 hdr->sh_entsize = 8;
7128 }
7129
7a79a000
TS
7130 /* The generic elf_fake_sections will set up REL_HDR using the default
7131 kind of relocations. We used to set up a second header for the
7132 non-default kind of relocations here, but only NewABI would use
7133 these, and the IRIX ld doesn't like resulting empty RELA sections.
7134 Thus we create those header only on demand now. */
b49e97c9 7135
b34976b6 7136 return TRUE;
b49e97c9
TS
7137}
7138
7139/* Given a BFD section, try to locate the corresponding ELF section
7140 index. This is used by both the 32-bit and the 64-bit ABI.
7141 Actually, it's not clear to me that the 64-bit ABI supports these,
7142 but for non-PIC objects we will certainly want support for at least
7143 the .scommon section. */
7144
b34976b6 7145bfd_boolean
9719ad41
RS
7146_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7147 asection *sec, int *retval)
b49e97c9
TS
7148{
7149 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7150 {
7151 *retval = SHN_MIPS_SCOMMON;
b34976b6 7152 return TRUE;
b49e97c9
TS
7153 }
7154 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7155 {
7156 *retval = SHN_MIPS_ACOMMON;
b34976b6 7157 return TRUE;
b49e97c9 7158 }
b34976b6 7159 return FALSE;
b49e97c9
TS
7160}
7161\f
7162/* Hook called by the linker routine which adds symbols from an object
7163 file. We must handle the special MIPS section numbers here. */
7164
b34976b6 7165bfd_boolean
9719ad41 7166_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 7167 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
7168 flagword *flagsp ATTRIBUTE_UNUSED,
7169 asection **secp, bfd_vma *valp)
b49e97c9
TS
7170{
7171 if (SGI_COMPAT (abfd)
7172 && (abfd->flags & DYNAMIC) != 0
7173 && strcmp (*namep, "_rld_new_interface") == 0)
7174 {
8dc1a139 7175 /* Skip IRIX5 rld entry name. */
b49e97c9 7176 *namep = NULL;
b34976b6 7177 return TRUE;
b49e97c9
TS
7178 }
7179
eedecc07
DD
7180 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7181 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7182 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7183 a magic symbol resolved by the linker, we ignore this bogus definition
7184 of _gp_disp. New ABI objects do not suffer from this problem so this
7185 is not done for them. */
7186 if (!NEWABI_P(abfd)
7187 && (sym->st_shndx == SHN_ABS)
7188 && (strcmp (*namep, "_gp_disp") == 0))
7189 {
7190 *namep = NULL;
7191 return TRUE;
7192 }
7193
b49e97c9
TS
7194 switch (sym->st_shndx)
7195 {
7196 case SHN_COMMON:
7197 /* Common symbols less than the GP size are automatically
7198 treated as SHN_MIPS_SCOMMON symbols. */
7199 if (sym->st_size > elf_gp_size (abfd)
b59eed79 7200 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
7201 || IRIX_COMPAT (abfd) == ict_irix6)
7202 break;
7203 /* Fall through. */
7204 case SHN_MIPS_SCOMMON:
7205 *secp = bfd_make_section_old_way (abfd, ".scommon");
7206 (*secp)->flags |= SEC_IS_COMMON;
7207 *valp = sym->st_size;
7208 break;
7209
7210 case SHN_MIPS_TEXT:
7211 /* This section is used in a shared object. */
698600e4 7212 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
b49e97c9
TS
7213 {
7214 asymbol *elf_text_symbol;
7215 asection *elf_text_section;
7216 bfd_size_type amt = sizeof (asection);
7217
7218 elf_text_section = bfd_zalloc (abfd, amt);
7219 if (elf_text_section == NULL)
b34976b6 7220 return FALSE;
b49e97c9
TS
7221
7222 amt = sizeof (asymbol);
7223 elf_text_symbol = bfd_zalloc (abfd, amt);
7224 if (elf_text_symbol == NULL)
b34976b6 7225 return FALSE;
b49e97c9
TS
7226
7227 /* Initialize the section. */
7228
698600e4
AM
7229 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7230 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
b49e97c9
TS
7231
7232 elf_text_section->symbol = elf_text_symbol;
698600e4 7233 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
b49e97c9
TS
7234
7235 elf_text_section->name = ".text";
7236 elf_text_section->flags = SEC_NO_FLAGS;
7237 elf_text_section->output_section = NULL;
7238 elf_text_section->owner = abfd;
7239 elf_text_symbol->name = ".text";
7240 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7241 elf_text_symbol->section = elf_text_section;
7242 }
7243 /* This code used to do *secp = bfd_und_section_ptr if
7244 info->shared. I don't know why, and that doesn't make sense,
7245 so I took it out. */
698600e4 7246 *secp = mips_elf_tdata (abfd)->elf_text_section;
b49e97c9
TS
7247 break;
7248
7249 case SHN_MIPS_ACOMMON:
7250 /* Fall through. XXX Can we treat this as allocated data? */
7251 case SHN_MIPS_DATA:
7252 /* This section is used in a shared object. */
698600e4 7253 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
b49e97c9
TS
7254 {
7255 asymbol *elf_data_symbol;
7256 asection *elf_data_section;
7257 bfd_size_type amt = sizeof (asection);
7258
7259 elf_data_section = bfd_zalloc (abfd, amt);
7260 if (elf_data_section == NULL)
b34976b6 7261 return FALSE;
b49e97c9
TS
7262
7263 amt = sizeof (asymbol);
7264 elf_data_symbol = bfd_zalloc (abfd, amt);
7265 if (elf_data_symbol == NULL)
b34976b6 7266 return FALSE;
b49e97c9
TS
7267
7268 /* Initialize the section. */
7269
698600e4
AM
7270 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7271 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
b49e97c9
TS
7272
7273 elf_data_section->symbol = elf_data_symbol;
698600e4 7274 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
b49e97c9
TS
7275
7276 elf_data_section->name = ".data";
7277 elf_data_section->flags = SEC_NO_FLAGS;
7278 elf_data_section->output_section = NULL;
7279 elf_data_section->owner = abfd;
7280 elf_data_symbol->name = ".data";
7281 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7282 elf_data_symbol->section = elf_data_section;
7283 }
7284 /* This code used to do *secp = bfd_und_section_ptr if
7285 info->shared. I don't know why, and that doesn't make sense,
7286 so I took it out. */
698600e4 7287 *secp = mips_elf_tdata (abfd)->elf_data_section;
b49e97c9
TS
7288 break;
7289
7290 case SHN_MIPS_SUNDEFINED:
7291 *secp = bfd_und_section_ptr;
7292 break;
7293 }
7294
7295 if (SGI_COMPAT (abfd)
7296 && ! info->shared
f13a99db 7297 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
7298 && strcmp (*namep, "__rld_obj_head") == 0)
7299 {
7300 struct elf_link_hash_entry *h;
14a793b2 7301 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7302
7303 /* Mark __rld_obj_head as dynamic. */
14a793b2 7304 bh = NULL;
b49e97c9 7305 if (! (_bfd_generic_link_add_one_symbol
9719ad41 7306 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 7307 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7308 return FALSE;
14a793b2
AM
7309
7310 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7311 h->non_elf = 0;
7312 h->def_regular = 1;
b49e97c9
TS
7313 h->type = STT_OBJECT;
7314
c152c796 7315 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7316 return FALSE;
b49e97c9 7317
b34976b6 7318 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b4082c70 7319 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7320 }
7321
7322 /* If this is a mips16 text symbol, add 1 to the value to make it
7323 odd. This will cause something like .word SYM to come up with
7324 the right value when it is loaded into the PC. */
df58fc94 7325 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
7326 ++*valp;
7327
b34976b6 7328 return TRUE;
b49e97c9
TS
7329}
7330
7331/* This hook function is called before the linker writes out a global
7332 symbol. We mark symbols as small common if appropriate. This is
7333 also where we undo the increment of the value for a mips16 symbol. */
7334
6e0b88f1 7335int
9719ad41
RS
7336_bfd_mips_elf_link_output_symbol_hook
7337 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7338 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7339 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
7340{
7341 /* If we see a common symbol, which implies a relocatable link, then
7342 if a symbol was small common in an input file, mark it as small
7343 common in the output file. */
7344 if (sym->st_shndx == SHN_COMMON
7345 && strcmp (input_sec->name, ".scommon") == 0)
7346 sym->st_shndx = SHN_MIPS_SCOMMON;
7347
df58fc94 7348 if (ELF_ST_IS_COMPRESSED (sym->st_other))
79cda7cf 7349 sym->st_value &= ~1;
b49e97c9 7350
6e0b88f1 7351 return 1;
b49e97c9
TS
7352}
7353\f
7354/* Functions for the dynamic linker. */
7355
7356/* Create dynamic sections when linking against a dynamic object. */
7357
b34976b6 7358bfd_boolean
9719ad41 7359_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
7360{
7361 struct elf_link_hash_entry *h;
14a793b2 7362 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7363 flagword flags;
7364 register asection *s;
7365 const char * const *namep;
0a44bf69 7366 struct mips_elf_link_hash_table *htab;
b49e97c9 7367
0a44bf69 7368 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7369 BFD_ASSERT (htab != NULL);
7370
b49e97c9
TS
7371 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7372 | SEC_LINKER_CREATED | SEC_READONLY);
7373
0a44bf69
RS
7374 /* The psABI requires a read-only .dynamic section, but the VxWorks
7375 EABI doesn't. */
7376 if (!htab->is_vxworks)
b49e97c9 7377 {
3d4d4302 7378 s = bfd_get_linker_section (abfd, ".dynamic");
0a44bf69
RS
7379 if (s != NULL)
7380 {
7381 if (! bfd_set_section_flags (abfd, s, flags))
7382 return FALSE;
7383 }
b49e97c9
TS
7384 }
7385
7386 /* We need to create .got section. */
23cc69b6 7387 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
7388 return FALSE;
7389
0a44bf69 7390 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 7391 return FALSE;
b49e97c9 7392
b49e97c9 7393 /* Create .stub section. */
3d4d4302
AM
7394 s = bfd_make_section_anyway_with_flags (abfd,
7395 MIPS_ELF_STUB_SECTION_NAME (abfd),
7396 flags | SEC_CODE);
4e41d0d7
RS
7397 if (s == NULL
7398 || ! bfd_set_section_alignment (abfd, s,
7399 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7400 return FALSE;
7401 htab->sstubs = s;
b49e97c9 7402
e6aea42d 7403 if (!mips_elf_hash_table (info)->use_rld_obj_head
b49e97c9 7404 && !info->shared
3d4d4302 7405 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
b49e97c9 7406 {
3d4d4302
AM
7407 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7408 flags &~ (flagword) SEC_READONLY);
b49e97c9 7409 if (s == NULL
b49e97c9
TS
7410 || ! bfd_set_section_alignment (abfd, s,
7411 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 7412 return FALSE;
b49e97c9
TS
7413 }
7414
7415 /* On IRIX5, we adjust add some additional symbols and change the
7416 alignments of several sections. There is no ABI documentation
7417 indicating that this is necessary on IRIX6, nor any evidence that
7418 the linker takes such action. */
7419 if (IRIX_COMPAT (abfd) == ict_irix5)
7420 {
7421 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7422 {
14a793b2 7423 bh = NULL;
b49e97c9 7424 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
7425 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7426 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7427 return FALSE;
14a793b2
AM
7428
7429 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7430 h->non_elf = 0;
7431 h->def_regular = 1;
b49e97c9
TS
7432 h->type = STT_SECTION;
7433
c152c796 7434 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7435 return FALSE;
b49e97c9
TS
7436 }
7437
7438 /* We need to create a .compact_rel section. */
7439 if (SGI_COMPAT (abfd))
7440 {
7441 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 7442 return FALSE;
b49e97c9
TS
7443 }
7444
44c410de 7445 /* Change alignments of some sections. */
3d4d4302 7446 s = bfd_get_linker_section (abfd, ".hash");
b49e97c9 7447 if (s != NULL)
a253d456
NC
7448 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7449
3d4d4302 7450 s = bfd_get_linker_section (abfd, ".dynsym");
b49e97c9 7451 if (s != NULL)
a253d456
NC
7452 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7453
3d4d4302 7454 s = bfd_get_linker_section (abfd, ".dynstr");
b49e97c9 7455 if (s != NULL)
a253d456
NC
7456 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7457
3d4d4302 7458 /* ??? */
b49e97c9
TS
7459 s = bfd_get_section_by_name (abfd, ".reginfo");
7460 if (s != NULL)
a253d456
NC
7461 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7462
3d4d4302 7463 s = bfd_get_linker_section (abfd, ".dynamic");
b49e97c9 7464 if (s != NULL)
a253d456 7465 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7466 }
7467
7468 if (!info->shared)
7469 {
14a793b2
AM
7470 const char *name;
7471
7472 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7473 bh = NULL;
7474 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
7475 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7476 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7477 return FALSE;
14a793b2
AM
7478
7479 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7480 h->non_elf = 0;
7481 h->def_regular = 1;
b49e97c9
TS
7482 h->type = STT_SECTION;
7483
c152c796 7484 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7485 return FALSE;
b49e97c9
TS
7486
7487 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7488 {
7489 /* __rld_map is a four byte word located in the .data section
7490 and is filled in by the rtld to contain a pointer to
7491 the _r_debug structure. Its symbol value will be set in
7492 _bfd_mips_elf_finish_dynamic_symbol. */
3d4d4302 7493 s = bfd_get_linker_section (abfd, ".rld_map");
0abfb97a 7494 BFD_ASSERT (s != NULL);
14a793b2 7495
0abfb97a
L
7496 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7497 bh = NULL;
7498 if (!(_bfd_generic_link_add_one_symbol
7499 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7500 get_elf_backend_data (abfd)->collect, &bh)))
7501 return FALSE;
b49e97c9 7502
0abfb97a
L
7503 h = (struct elf_link_hash_entry *) bh;
7504 h->non_elf = 0;
7505 h->def_regular = 1;
7506 h->type = STT_OBJECT;
7507
7508 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7509 return FALSE;
b4082c70 7510 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7511 }
7512 }
7513
861fb55a 7514 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
c164a95d 7515 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
861fb55a
DJ
7516 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7517 return FALSE;
7518
7519 /* Cache the sections created above. */
3d4d4302
AM
7520 htab->splt = bfd_get_linker_section (abfd, ".plt");
7521 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
0a44bf69
RS
7522 if (htab->is_vxworks)
7523 {
3d4d4302
AM
7524 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7525 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
861fb55a
DJ
7526 }
7527 else
3d4d4302 7528 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
861fb55a
DJ
7529 if (!htab->sdynbss
7530 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7531 || !htab->srelplt
7532 || !htab->splt)
7533 abort ();
0a44bf69 7534
1bbce132
MR
7535 /* Do the usual VxWorks handling. */
7536 if (htab->is_vxworks
7537 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7538 return FALSE;
0a44bf69 7539
b34976b6 7540 return TRUE;
b49e97c9
TS
7541}
7542\f
c224138d
RS
7543/* Return true if relocation REL against section SEC is a REL rather than
7544 RELA relocation. RELOCS is the first relocation in the section and
7545 ABFD is the bfd that contains SEC. */
7546
7547static bfd_boolean
7548mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7549 const Elf_Internal_Rela *relocs,
7550 const Elf_Internal_Rela *rel)
7551{
7552 Elf_Internal_Shdr *rel_hdr;
7553 const struct elf_backend_data *bed;
7554
d4730f92
BS
7555 /* To determine which flavor of relocation this is, we depend on the
7556 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7557 rel_hdr = elf_section_data (sec)->rel.hdr;
7558 if (rel_hdr == NULL)
7559 return FALSE;
c224138d 7560 bed = get_elf_backend_data (abfd);
d4730f92
BS
7561 return ((size_t) (rel - relocs)
7562 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
c224138d
RS
7563}
7564
7565/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7566 HOWTO is the relocation's howto and CONTENTS points to the contents
7567 of the section that REL is against. */
7568
7569static bfd_vma
7570mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7571 reloc_howto_type *howto, bfd_byte *contents)
7572{
7573 bfd_byte *location;
7574 unsigned int r_type;
7575 bfd_vma addend;
7576
7577 r_type = ELF_R_TYPE (abfd, rel->r_info);
7578 location = contents + rel->r_offset;
7579
7580 /* Get the addend, which is stored in the input file. */
df58fc94 7581 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
c224138d 7582 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
df58fc94 7583 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
c224138d
RS
7584
7585 return addend & howto->src_mask;
7586}
7587
7588/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7589 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7590 and update *ADDEND with the final addend. Return true on success
7591 or false if the LO16 could not be found. RELEND is the exclusive
7592 upper bound on the relocations for REL's section. */
7593
7594static bfd_boolean
7595mips_elf_add_lo16_rel_addend (bfd *abfd,
7596 const Elf_Internal_Rela *rel,
7597 const Elf_Internal_Rela *relend,
7598 bfd_byte *contents, bfd_vma *addend)
7599{
7600 unsigned int r_type, lo16_type;
7601 const Elf_Internal_Rela *lo16_relocation;
7602 reloc_howto_type *lo16_howto;
7603 bfd_vma l;
7604
7605 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7606 if (mips16_reloc_p (r_type))
c224138d 7607 lo16_type = R_MIPS16_LO16;
df58fc94
RS
7608 else if (micromips_reloc_p (r_type))
7609 lo16_type = R_MICROMIPS_LO16;
c224138d
RS
7610 else
7611 lo16_type = R_MIPS_LO16;
7612
7613 /* The combined value is the sum of the HI16 addend, left-shifted by
7614 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7615 code does a `lui' of the HI16 value, and then an `addiu' of the
7616 LO16 value.)
7617
7618 Scan ahead to find a matching LO16 relocation.
7619
7620 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7621 be immediately following. However, for the IRIX6 ABI, the next
7622 relocation may be a composed relocation consisting of several
7623 relocations for the same address. In that case, the R_MIPS_LO16
7624 relocation may occur as one of these. We permit a similar
7625 extension in general, as that is useful for GCC.
7626
7627 In some cases GCC dead code elimination removes the LO16 but keeps
7628 the corresponding HI16. This is strictly speaking a violation of
7629 the ABI but not immediately harmful. */
7630 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7631 if (lo16_relocation == NULL)
7632 return FALSE;
7633
7634 /* Obtain the addend kept there. */
7635 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7636 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7637
7638 l <<= lo16_howto->rightshift;
7639 l = _bfd_mips_elf_sign_extend (l, 16);
7640
7641 *addend <<= 16;
7642 *addend += l;
7643 return TRUE;
7644}
7645
7646/* Try to read the contents of section SEC in bfd ABFD. Return true and
7647 store the contents in *CONTENTS on success. Assume that *CONTENTS
7648 already holds the contents if it is nonull on entry. */
7649
7650static bfd_boolean
7651mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7652{
7653 if (*contents)
7654 return TRUE;
7655
7656 /* Get cached copy if it exists. */
7657 if (elf_section_data (sec)->this_hdr.contents != NULL)
7658 {
7659 *contents = elf_section_data (sec)->this_hdr.contents;
7660 return TRUE;
7661 }
7662
7663 return bfd_malloc_and_get_section (abfd, sec, contents);
7664}
7665
1bbce132
MR
7666/* Make a new PLT record to keep internal data. */
7667
7668static struct plt_entry *
7669mips_elf_make_plt_record (bfd *abfd)
7670{
7671 struct plt_entry *entry;
7672
7673 entry = bfd_zalloc (abfd, sizeof (*entry));
7674 if (entry == NULL)
7675 return NULL;
7676
7677 entry->stub_offset = MINUS_ONE;
7678 entry->mips_offset = MINUS_ONE;
7679 entry->comp_offset = MINUS_ONE;
7680 entry->gotplt_index = MINUS_ONE;
7681 return entry;
7682}
7683
b49e97c9 7684/* Look through the relocs for a section during the first phase, and
1bbce132
MR
7685 allocate space in the global offset table and record the need for
7686 standard MIPS and compressed procedure linkage table entries. */
b49e97c9 7687
b34976b6 7688bfd_boolean
9719ad41
RS
7689_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7690 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7691{
7692 const char *name;
7693 bfd *dynobj;
7694 Elf_Internal_Shdr *symtab_hdr;
7695 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7696 size_t extsymoff;
7697 const Elf_Internal_Rela *rel;
7698 const Elf_Internal_Rela *rel_end;
b49e97c9 7699 asection *sreloc;
9c5bfbb7 7700 const struct elf_backend_data *bed;
0a44bf69 7701 struct mips_elf_link_hash_table *htab;
c224138d
RS
7702 bfd_byte *contents;
7703 bfd_vma addend;
7704 reloc_howto_type *howto;
b49e97c9 7705
1049f94e 7706 if (info->relocatable)
b34976b6 7707 return TRUE;
b49e97c9 7708
0a44bf69 7709 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7710 BFD_ASSERT (htab != NULL);
7711
b49e97c9
TS
7712 dynobj = elf_hash_table (info)->dynobj;
7713 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7714 sym_hashes = elf_sym_hashes (abfd);
7715 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7716
738e5348
RS
7717 bed = get_elf_backend_data (abfd);
7718 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7719
b49e97c9
TS
7720 /* Check for the mips16 stub sections. */
7721
7722 name = bfd_get_section_name (abfd, sec);
b9d58d71 7723 if (FN_STUB_P (name))
b49e97c9
TS
7724 {
7725 unsigned long r_symndx;
7726
7727 /* Look at the relocation information to figure out which symbol
7728 this is for. */
7729
cb4437b8 7730 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
7731 if (r_symndx == 0)
7732 {
7733 (*_bfd_error_handler)
7734 (_("%B: Warning: cannot determine the target function for"
7735 " stub section `%s'"),
7736 abfd, name);
7737 bfd_set_error (bfd_error_bad_value);
7738 return FALSE;
7739 }
b49e97c9
TS
7740
7741 if (r_symndx < extsymoff
7742 || sym_hashes[r_symndx - extsymoff] == NULL)
7743 {
7744 asection *o;
7745
7746 /* This stub is for a local symbol. This stub will only be
7747 needed if there is some relocation in this BFD, other
7748 than a 16 bit function call, which refers to this symbol. */
7749 for (o = abfd->sections; o != NULL; o = o->next)
7750 {
7751 Elf_Internal_Rela *sec_relocs;
7752 const Elf_Internal_Rela *r, *rend;
7753
7754 /* We can ignore stub sections when looking for relocs. */
7755 if ((o->flags & SEC_RELOC) == 0
7756 || o->reloc_count == 0
738e5348 7757 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7758 continue;
7759
45d6a902 7760 sec_relocs
9719ad41 7761 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7762 info->keep_memory);
b49e97c9 7763 if (sec_relocs == NULL)
b34976b6 7764 return FALSE;
b49e97c9
TS
7765
7766 rend = sec_relocs + o->reloc_count;
7767 for (r = sec_relocs; r < rend; r++)
7768 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7769 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7770 break;
7771
6cdc0ccc 7772 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7773 free (sec_relocs);
7774
7775 if (r < rend)
7776 break;
7777 }
7778
7779 if (o == NULL)
7780 {
7781 /* There is no non-call reloc for this stub, so we do
7782 not need it. Since this function is called before
7783 the linker maps input sections to output sections, we
7784 can easily discard it by setting the SEC_EXCLUDE
7785 flag. */
7786 sec->flags |= SEC_EXCLUDE;
b34976b6 7787 return TRUE;
b49e97c9
TS
7788 }
7789
7790 /* Record this stub in an array of local symbol stubs for
7791 this BFD. */
698600e4 7792 if (mips_elf_tdata (abfd)->local_stubs == NULL)
b49e97c9
TS
7793 {
7794 unsigned long symcount;
7795 asection **n;
7796 bfd_size_type amt;
7797
7798 if (elf_bad_symtab (abfd))
7799 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7800 else
7801 symcount = symtab_hdr->sh_info;
7802 amt = symcount * sizeof (asection *);
9719ad41 7803 n = bfd_zalloc (abfd, amt);
b49e97c9 7804 if (n == NULL)
b34976b6 7805 return FALSE;
698600e4 7806 mips_elf_tdata (abfd)->local_stubs = n;
b49e97c9
TS
7807 }
7808
b9d58d71 7809 sec->flags |= SEC_KEEP;
698600e4 7810 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
b49e97c9
TS
7811
7812 /* We don't need to set mips16_stubs_seen in this case.
7813 That flag is used to see whether we need to look through
7814 the global symbol table for stubs. We don't need to set
7815 it here, because we just have a local stub. */
7816 }
7817 else
7818 {
7819 struct mips_elf_link_hash_entry *h;
7820
7821 h = ((struct mips_elf_link_hash_entry *)
7822 sym_hashes[r_symndx - extsymoff]);
7823
973a3492
L
7824 while (h->root.root.type == bfd_link_hash_indirect
7825 || h->root.root.type == bfd_link_hash_warning)
7826 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7827
b49e97c9
TS
7828 /* H is the symbol this stub is for. */
7829
b9d58d71
TS
7830 /* If we already have an appropriate stub for this function, we
7831 don't need another one, so we can discard this one. Since
7832 this function is called before the linker maps input sections
7833 to output sections, we can easily discard it by setting the
7834 SEC_EXCLUDE flag. */
7835 if (h->fn_stub != NULL)
7836 {
7837 sec->flags |= SEC_EXCLUDE;
7838 return TRUE;
7839 }
7840
7841 sec->flags |= SEC_KEEP;
b49e97c9 7842 h->fn_stub = sec;
b34976b6 7843 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7844 }
7845 }
b9d58d71 7846 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7847 {
7848 unsigned long r_symndx;
7849 struct mips_elf_link_hash_entry *h;
7850 asection **loc;
7851
7852 /* Look at the relocation information to figure out which symbol
7853 this is for. */
7854
cb4437b8 7855 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
7856 if (r_symndx == 0)
7857 {
7858 (*_bfd_error_handler)
7859 (_("%B: Warning: cannot determine the target function for"
7860 " stub section `%s'"),
7861 abfd, name);
7862 bfd_set_error (bfd_error_bad_value);
7863 return FALSE;
7864 }
b49e97c9
TS
7865
7866 if (r_symndx < extsymoff
7867 || sym_hashes[r_symndx - extsymoff] == NULL)
7868 {
b9d58d71 7869 asection *o;
b49e97c9 7870
b9d58d71
TS
7871 /* This stub is for a local symbol. This stub will only be
7872 needed if there is some relocation (R_MIPS16_26) in this BFD
7873 that refers to this symbol. */
7874 for (o = abfd->sections; o != NULL; o = o->next)
7875 {
7876 Elf_Internal_Rela *sec_relocs;
7877 const Elf_Internal_Rela *r, *rend;
7878
7879 /* We can ignore stub sections when looking for relocs. */
7880 if ((o->flags & SEC_RELOC) == 0
7881 || o->reloc_count == 0
738e5348 7882 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7883 continue;
7884
7885 sec_relocs
7886 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7887 info->keep_memory);
7888 if (sec_relocs == NULL)
7889 return FALSE;
7890
7891 rend = sec_relocs + o->reloc_count;
7892 for (r = sec_relocs; r < rend; r++)
7893 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7894 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7895 break;
7896
7897 if (elf_section_data (o)->relocs != sec_relocs)
7898 free (sec_relocs);
7899
7900 if (r < rend)
7901 break;
7902 }
7903
7904 if (o == NULL)
7905 {
7906 /* There is no non-call reloc for this stub, so we do
7907 not need it. Since this function is called before
7908 the linker maps input sections to output sections, we
7909 can easily discard it by setting the SEC_EXCLUDE
7910 flag. */
7911 sec->flags |= SEC_EXCLUDE;
7912 return TRUE;
7913 }
7914
7915 /* Record this stub in an array of local symbol call_stubs for
7916 this BFD. */
698600e4 7917 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
b9d58d71
TS
7918 {
7919 unsigned long symcount;
7920 asection **n;
7921 bfd_size_type amt;
7922
7923 if (elf_bad_symtab (abfd))
7924 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7925 else
7926 symcount = symtab_hdr->sh_info;
7927 amt = symcount * sizeof (asection *);
7928 n = bfd_zalloc (abfd, amt);
7929 if (n == NULL)
7930 return FALSE;
698600e4 7931 mips_elf_tdata (abfd)->local_call_stubs = n;
b9d58d71 7932 }
b49e97c9 7933
b9d58d71 7934 sec->flags |= SEC_KEEP;
698600e4 7935 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7936
b9d58d71
TS
7937 /* We don't need to set mips16_stubs_seen in this case.
7938 That flag is used to see whether we need to look through
7939 the global symbol table for stubs. We don't need to set
7940 it here, because we just have a local stub. */
7941 }
b49e97c9 7942 else
b49e97c9 7943 {
b9d58d71
TS
7944 h = ((struct mips_elf_link_hash_entry *)
7945 sym_hashes[r_symndx - extsymoff]);
68ffbac6 7946
b9d58d71 7947 /* H is the symbol this stub is for. */
68ffbac6 7948
b9d58d71
TS
7949 if (CALL_FP_STUB_P (name))
7950 loc = &h->call_fp_stub;
7951 else
7952 loc = &h->call_stub;
68ffbac6 7953
b9d58d71
TS
7954 /* If we already have an appropriate stub for this function, we
7955 don't need another one, so we can discard this one. Since
7956 this function is called before the linker maps input sections
7957 to output sections, we can easily discard it by setting the
7958 SEC_EXCLUDE flag. */
7959 if (*loc != NULL)
7960 {
7961 sec->flags |= SEC_EXCLUDE;
7962 return TRUE;
7963 }
b49e97c9 7964
b9d58d71
TS
7965 sec->flags |= SEC_KEEP;
7966 *loc = sec;
7967 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7968 }
b49e97c9
TS
7969 }
7970
b49e97c9 7971 sreloc = NULL;
c224138d 7972 contents = NULL;
b49e97c9
TS
7973 for (rel = relocs; rel < rel_end; ++rel)
7974 {
7975 unsigned long r_symndx;
7976 unsigned int r_type;
7977 struct elf_link_hash_entry *h;
861fb55a 7978 bfd_boolean can_make_dynamic_p;
c5d6fa44
RS
7979 bfd_boolean call_reloc_p;
7980 bfd_boolean constrain_symbol_p;
b49e97c9
TS
7981
7982 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7983 r_type = ELF_R_TYPE (abfd, rel->r_info);
7984
7985 if (r_symndx < extsymoff)
7986 h = NULL;
7987 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7988 {
7989 (*_bfd_error_handler)
d003868e
AM
7990 (_("%B: Malformed reloc detected for section %s"),
7991 abfd, name);
b49e97c9 7992 bfd_set_error (bfd_error_bad_value);
b34976b6 7993 return FALSE;
b49e97c9
TS
7994 }
7995 else
7996 {
7997 h = sym_hashes[r_symndx - extsymoff];
81fbe831
AM
7998 if (h != NULL)
7999 {
8000 while (h->root.type == bfd_link_hash_indirect
8001 || h->root.type == bfd_link_hash_warning)
8002 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8003
8004 /* PR15323, ref flags aren't set for references in the
8005 same object. */
8006 h->root.non_ir_ref = 1;
8007 }
861fb55a 8008 }
b49e97c9 8009
861fb55a
DJ
8010 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8011 relocation into a dynamic one. */
8012 can_make_dynamic_p = FALSE;
c5d6fa44
RS
8013
8014 /* Set CALL_RELOC_P to true if the relocation is for a call,
8015 and if pointer equality therefore doesn't matter. */
8016 call_reloc_p = FALSE;
8017
8018 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8019 into account when deciding how to define the symbol.
8020 Relocations in nonallocatable sections such as .pdr and
8021 .debug* should have no effect. */
8022 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8023
861fb55a
DJ
8024 switch (r_type)
8025 {
861fb55a
DJ
8026 case R_MIPS_CALL16:
8027 case R_MIPS_CALL_HI16:
8028 case R_MIPS_CALL_LO16:
c5d6fa44
RS
8029 case R_MIPS16_CALL16:
8030 case R_MICROMIPS_CALL16:
8031 case R_MICROMIPS_CALL_HI16:
8032 case R_MICROMIPS_CALL_LO16:
8033 call_reloc_p = TRUE;
8034 /* Fall through. */
8035
8036 case R_MIPS_GOT16:
861fb55a
DJ
8037 case R_MIPS_GOT_HI16:
8038 case R_MIPS_GOT_LO16:
8039 case R_MIPS_GOT_PAGE:
8040 case R_MIPS_GOT_OFST:
8041 case R_MIPS_GOT_DISP:
8042 case R_MIPS_TLS_GOTTPREL:
8043 case R_MIPS_TLS_GD:
8044 case R_MIPS_TLS_LDM:
d0f13682 8045 case R_MIPS16_GOT16:
d0f13682
CLT
8046 case R_MIPS16_TLS_GOTTPREL:
8047 case R_MIPS16_TLS_GD:
8048 case R_MIPS16_TLS_LDM:
df58fc94 8049 case R_MICROMIPS_GOT16:
df58fc94
RS
8050 case R_MICROMIPS_GOT_HI16:
8051 case R_MICROMIPS_GOT_LO16:
8052 case R_MICROMIPS_GOT_PAGE:
8053 case R_MICROMIPS_GOT_OFST:
8054 case R_MICROMIPS_GOT_DISP:
8055 case R_MICROMIPS_TLS_GOTTPREL:
8056 case R_MICROMIPS_TLS_GD:
8057 case R_MICROMIPS_TLS_LDM:
861fb55a
DJ
8058 if (dynobj == NULL)
8059 elf_hash_table (info)->dynobj = dynobj = abfd;
8060 if (!mips_elf_create_got_section (dynobj, info))
8061 return FALSE;
8062 if (htab->is_vxworks && !info->shared)
b49e97c9 8063 {
861fb55a
DJ
8064 (*_bfd_error_handler)
8065 (_("%B: GOT reloc at 0x%lx not expected in executables"),
8066 abfd, (unsigned long) rel->r_offset);
8067 bfd_set_error (bfd_error_bad_value);
8068 return FALSE;
b49e97c9 8069 }
c5d6fa44 8070 can_make_dynamic_p = TRUE;
861fb55a 8071 break;
b49e97c9 8072
c5d6fa44 8073 case R_MIPS_NONE:
99da6b5f 8074 case R_MIPS_JALR:
df58fc94 8075 case R_MICROMIPS_JALR:
c5d6fa44
RS
8076 /* These relocations have empty fields and are purely there to
8077 provide link information. The symbol value doesn't matter. */
8078 constrain_symbol_p = FALSE;
8079 break;
8080
8081 case R_MIPS_GPREL16:
8082 case R_MIPS_GPREL32:
8083 case R_MIPS16_GPREL:
8084 case R_MICROMIPS_GPREL16:
8085 /* GP-relative relocations always resolve to a definition in a
8086 regular input file, ignoring the one-definition rule. This is
8087 important for the GP setup sequence in NewABI code, which
8088 always resolves to a local function even if other relocations
8089 against the symbol wouldn't. */
8090 constrain_symbol_p = FALSE;
99da6b5f
AN
8091 break;
8092
861fb55a
DJ
8093 case R_MIPS_32:
8094 case R_MIPS_REL32:
8095 case R_MIPS_64:
8096 /* In VxWorks executables, references to external symbols
8097 must be handled using copy relocs or PLT entries; it is not
8098 possible to convert this relocation into a dynamic one.
8099
8100 For executables that use PLTs and copy-relocs, we have a
8101 choice between converting the relocation into a dynamic
8102 one or using copy relocations or PLT entries. It is
8103 usually better to do the former, unless the relocation is
8104 against a read-only section. */
8105 if ((info->shared
8106 || (h != NULL
8107 && !htab->is_vxworks
8108 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8109 && !(!info->nocopyreloc
8110 && !PIC_OBJECT_P (abfd)
8111 && MIPS_ELF_READONLY_SECTION (sec))))
8112 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 8113 {
861fb55a 8114 can_make_dynamic_p = TRUE;
b49e97c9
TS
8115 if (dynobj == NULL)
8116 elf_hash_table (info)->dynobj = dynobj = abfd;
861fb55a 8117 }
c5d6fa44 8118 break;
b49e97c9 8119
861fb55a
DJ
8120 case R_MIPS_26:
8121 case R_MIPS_PC16:
8122 case R_MIPS16_26:
df58fc94
RS
8123 case R_MICROMIPS_26_S1:
8124 case R_MICROMIPS_PC7_S1:
8125 case R_MICROMIPS_PC10_S1:
8126 case R_MICROMIPS_PC16_S1:
8127 case R_MICROMIPS_PC23_S2:
c5d6fa44 8128 call_reloc_p = TRUE;
861fb55a 8129 break;
b49e97c9
TS
8130 }
8131
0a44bf69
RS
8132 if (h)
8133 {
c5d6fa44
RS
8134 if (constrain_symbol_p)
8135 {
8136 if (!can_make_dynamic_p)
8137 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8138
8139 if (!call_reloc_p)
8140 h->pointer_equality_needed = 1;
8141
8142 /* We must not create a stub for a symbol that has
8143 relocations related to taking the function's address.
8144 This doesn't apply to VxWorks, where CALL relocs refer
8145 to a .got.plt entry instead of a normal .got entry. */
8146 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8147 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8148 }
8149
0a44bf69
RS
8150 /* Relocations against the special VxWorks __GOTT_BASE__ and
8151 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8152 room for them in .rela.dyn. */
8153 if (is_gott_symbol (info, h))
8154 {
8155 if (sreloc == NULL)
8156 {
8157 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8158 if (sreloc == NULL)
8159 return FALSE;
8160 }
8161 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
8162 if (MIPS_ELF_READONLY_SECTION (sec))
8163 /* We tell the dynamic linker that there are
8164 relocations against the text segment. */
8165 info->flags |= DF_TEXTREL;
0a44bf69
RS
8166 }
8167 }
df58fc94
RS
8168 else if (call_lo16_reloc_p (r_type)
8169 || got_lo16_reloc_p (r_type)
8170 || got_disp_reloc_p (r_type)
738e5348 8171 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
8172 {
8173 /* We may need a local GOT entry for this relocation. We
8174 don't count R_MIPS_GOT_PAGE because we can estimate the
8175 maximum number of pages needed by looking at the size of
738e5348
RS
8176 the segment. Similar comments apply to R_MIPS*_GOT16 and
8177 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 8178 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 8179 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 8180 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0 8181 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
e641e783 8182 rel->r_addend, info, r_type))
f4416af6 8183 return FALSE;
b49e97c9
TS
8184 }
8185
8f0c309a
CLT
8186 if (h != NULL
8187 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8188 ELF_ST_IS_MIPS16 (h->other)))
861fb55a
DJ
8189 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8190
b49e97c9
TS
8191 switch (r_type)
8192 {
8193 case R_MIPS_CALL16:
738e5348 8194 case R_MIPS16_CALL16:
df58fc94 8195 case R_MICROMIPS_CALL16:
b49e97c9
TS
8196 if (h == NULL)
8197 {
8198 (*_bfd_error_handler)
d003868e
AM
8199 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
8200 abfd, (unsigned long) rel->r_offset);
b49e97c9 8201 bfd_set_error (bfd_error_bad_value);
b34976b6 8202 return FALSE;
b49e97c9
TS
8203 }
8204 /* Fall through. */
8205
8206 case R_MIPS_CALL_HI16:
8207 case R_MIPS_CALL_LO16:
df58fc94
RS
8208 case R_MICROMIPS_CALL_HI16:
8209 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
8210 if (h != NULL)
8211 {
6ccf4795
RS
8212 /* Make sure there is room in the regular GOT to hold the
8213 function's address. We may eliminate it in favour of
8214 a .got.plt entry later; see mips_elf_count_got_symbols. */
e641e783
RS
8215 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8216 r_type))
b34976b6 8217 return FALSE;
b49e97c9
TS
8218
8219 /* We need a stub, not a plt entry for the undefined
8220 function. But we record it as if it needs plt. See
c152c796 8221 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 8222 h->needs_plt = 1;
b49e97c9
TS
8223 h->type = STT_FUNC;
8224 }
8225 break;
8226
0fdc1bf1 8227 case R_MIPS_GOT_PAGE:
df58fc94 8228 case R_MICROMIPS_GOT_PAGE:
738e5348 8229 case R_MIPS16_GOT16:
b49e97c9
TS
8230 case R_MIPS_GOT16:
8231 case R_MIPS_GOT_HI16:
8232 case R_MIPS_GOT_LO16:
df58fc94
RS
8233 case R_MICROMIPS_GOT16:
8234 case R_MICROMIPS_GOT_HI16:
8235 case R_MICROMIPS_GOT_LO16:
8236 if (!h || got_page_reloc_p (r_type))
c224138d 8237 {
3a3b6725
DJ
8238 /* This relocation needs (or may need, if h != NULL) a
8239 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8240 know for sure until we know whether the symbol is
8241 preemptible. */
c224138d
RS
8242 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8243 {
8244 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8245 return FALSE;
8246 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8247 addend = mips_elf_read_rel_addend (abfd, rel,
8248 howto, contents);
9684f078 8249 if (got16_reloc_p (r_type))
c224138d
RS
8250 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8251 contents, &addend);
8252 else
8253 addend <<= howto->rightshift;
8254 }
8255 else
8256 addend = rel->r_addend;
13db6b44
RS
8257 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8258 h, addend))
c224138d 8259 return FALSE;
13db6b44
RS
8260
8261 if (h)
8262 {
8263 struct mips_elf_link_hash_entry *hmips =
8264 (struct mips_elf_link_hash_entry *) h;
8265
8266 /* This symbol is definitely not overridable. */
8267 if (hmips->root.def_regular
8268 && ! (info->shared && ! info->symbolic
8269 && ! hmips->root.forced_local))
8270 h = NULL;
8271 }
c224138d 8272 }
13db6b44
RS
8273 /* If this is a global, overridable symbol, GOT_PAGE will
8274 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d
RS
8275 /* Fall through. */
8276
b49e97c9 8277 case R_MIPS_GOT_DISP:
df58fc94 8278 case R_MICROMIPS_GOT_DISP:
6ccf4795 8279 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
e641e783 8280 FALSE, r_type))
b34976b6 8281 return FALSE;
b49e97c9
TS
8282 break;
8283
0f20cc35 8284 case R_MIPS_TLS_GOTTPREL:
d0f13682 8285 case R_MIPS16_TLS_GOTTPREL:
df58fc94 8286 case R_MICROMIPS_TLS_GOTTPREL:
0f20cc35
DJ
8287 if (info->shared)
8288 info->flags |= DF_STATIC_TLS;
8289 /* Fall through */
8290
8291 case R_MIPS_TLS_LDM:
d0f13682 8292 case R_MIPS16_TLS_LDM:
df58fc94
RS
8293 case R_MICROMIPS_TLS_LDM:
8294 if (tls_ldm_reloc_p (r_type))
0f20cc35 8295 {
cf35638d 8296 r_symndx = STN_UNDEF;
0f20cc35
DJ
8297 h = NULL;
8298 }
8299 /* Fall through */
8300
8301 case R_MIPS_TLS_GD:
d0f13682 8302 case R_MIPS16_TLS_GD:
df58fc94 8303 case R_MICROMIPS_TLS_GD:
0f20cc35
DJ
8304 /* This symbol requires a global offset table entry, or two
8305 for TLS GD relocations. */
e641e783
RS
8306 if (h != NULL)
8307 {
8308 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8309 FALSE, r_type))
8310 return FALSE;
8311 }
8312 else
8313 {
8314 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8315 rel->r_addend,
8316 info, r_type))
8317 return FALSE;
8318 }
0f20cc35
DJ
8319 break;
8320
b49e97c9
TS
8321 case R_MIPS_32:
8322 case R_MIPS_REL32:
8323 case R_MIPS_64:
0a44bf69
RS
8324 /* In VxWorks executables, references to external symbols
8325 are handled using copy relocs or PLT stubs, so there's
8326 no need to add a .rela.dyn entry for this relocation. */
861fb55a 8327 if (can_make_dynamic_p)
b49e97c9
TS
8328 {
8329 if (sreloc == NULL)
8330 {
0a44bf69 8331 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 8332 if (sreloc == NULL)
f4416af6 8333 return FALSE;
b49e97c9 8334 }
9a59ad6b 8335 if (info->shared && h == NULL)
82f0cfbd
EC
8336 {
8337 /* When creating a shared object, we must copy these
8338 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
8339 relocs. Make room for this reloc in .rel(a).dyn. */
8340 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 8341 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8342 /* We tell the dynamic linker that there are
8343 relocations against the text segment. */
8344 info->flags |= DF_TEXTREL;
8345 }
b49e97c9
TS
8346 else
8347 {
8348 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 8349
9a59ad6b
DJ
8350 /* For a shared object, we must copy this relocation
8351 unless the symbol turns out to be undefined and
8352 weak with non-default visibility, in which case
8353 it will be left as zero.
8354
8355 We could elide R_MIPS_REL32 for locally binding symbols
8356 in shared libraries, but do not yet do so.
8357
8358 For an executable, we only need to copy this
8359 reloc if the symbol is defined in a dynamic
8360 object. */
b49e97c9
TS
8361 hmips = (struct mips_elf_link_hash_entry *) h;
8362 ++hmips->possibly_dynamic_relocs;
943284cc 8363 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8364 /* We need it to tell the dynamic linker if there
8365 are relocations against the text segment. */
8366 hmips->readonly_reloc = TRUE;
b49e97c9 8367 }
b49e97c9
TS
8368 }
8369
8370 if (SGI_COMPAT (abfd))
8371 mips_elf_hash_table (info)->compact_rel_size +=
8372 sizeof (Elf32_External_crinfo);
8373 break;
8374
8375 case R_MIPS_26:
8376 case R_MIPS_GPREL16:
8377 case R_MIPS_LITERAL:
8378 case R_MIPS_GPREL32:
df58fc94
RS
8379 case R_MICROMIPS_26_S1:
8380 case R_MICROMIPS_GPREL16:
8381 case R_MICROMIPS_LITERAL:
8382 case R_MICROMIPS_GPREL7_S2:
b49e97c9
TS
8383 if (SGI_COMPAT (abfd))
8384 mips_elf_hash_table (info)->compact_rel_size +=
8385 sizeof (Elf32_External_crinfo);
8386 break;
8387
8388 /* This relocation describes the C++ object vtable hierarchy.
8389 Reconstruct it for later use during GC. */
8390 case R_MIPS_GNU_VTINHERIT:
c152c796 8391 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 8392 return FALSE;
b49e97c9
TS
8393 break;
8394
8395 /* This relocation describes which C++ vtable entries are actually
8396 used. Record for later use during GC. */
8397 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
8398 BFD_ASSERT (h != NULL);
8399 if (h != NULL
8400 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 8401 return FALSE;
b49e97c9
TS
8402 break;
8403
8404 default:
8405 break;
8406 }
8407
1bbce132
MR
8408 /* Record the need for a PLT entry. At this point we don't know
8409 yet if we are going to create a PLT in the first place, but
8410 we only record whether the relocation requires a standard MIPS
8411 or a compressed code entry anyway. If we don't make a PLT after
8412 all, then we'll just ignore these arrangements. Likewise if
8413 a PLT entry is not created because the symbol is satisfied
8414 locally. */
8415 if (h != NULL
8416 && jal_reloc_p (r_type)
8417 && !SYMBOL_CALLS_LOCAL (info, h))
8418 {
8419 if (h->plt.plist == NULL)
8420 h->plt.plist = mips_elf_make_plt_record (abfd);
8421 if (h->plt.plist == NULL)
8422 return FALSE;
8423
8424 if (r_type == R_MIPS_26)
8425 h->plt.plist->need_mips = TRUE;
8426 else
8427 h->plt.plist->need_comp = TRUE;
8428 }
8429
738e5348
RS
8430 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8431 if there is one. We only need to handle global symbols here;
8432 we decide whether to keep or delete stubs for local symbols
8433 when processing the stub's relocations. */
b49e97c9 8434 if (h != NULL
738e5348
RS
8435 && !mips16_call_reloc_p (r_type)
8436 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
8437 {
8438 struct mips_elf_link_hash_entry *mh;
8439
8440 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 8441 mh->need_fn_stub = TRUE;
b49e97c9 8442 }
861fb55a
DJ
8443
8444 /* Refuse some position-dependent relocations when creating a
8445 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8446 not PIC, but we can create dynamic relocations and the result
8447 will be fine. Also do not refuse R_MIPS_LO16, which can be
8448 combined with R_MIPS_GOT16. */
8449 if (info->shared)
8450 {
8451 switch (r_type)
8452 {
8453 case R_MIPS16_HI16:
8454 case R_MIPS_HI16:
8455 case R_MIPS_HIGHER:
8456 case R_MIPS_HIGHEST:
df58fc94
RS
8457 case R_MICROMIPS_HI16:
8458 case R_MICROMIPS_HIGHER:
8459 case R_MICROMIPS_HIGHEST:
861fb55a
DJ
8460 /* Don't refuse a high part relocation if it's against
8461 no symbol (e.g. part of a compound relocation). */
cf35638d 8462 if (r_symndx == STN_UNDEF)
861fb55a
DJ
8463 break;
8464
8465 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8466 and has a special meaning. */
8467 if (!NEWABI_P (abfd) && h != NULL
8468 && strcmp (h->root.root.string, "_gp_disp") == 0)
8469 break;
8470
0fc1eb3c
RS
8471 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8472 if (is_gott_symbol (info, h))
8473 break;
8474
861fb55a
DJ
8475 /* FALLTHROUGH */
8476
8477 case R_MIPS16_26:
8478 case R_MIPS_26:
df58fc94 8479 case R_MICROMIPS_26_S1:
861fb55a
DJ
8480 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8481 (*_bfd_error_handler)
8482 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8483 abfd, howto->name,
8484 (h) ? h->root.root.string : "a local symbol");
8485 bfd_set_error (bfd_error_bad_value);
8486 return FALSE;
8487 default:
8488 break;
8489 }
8490 }
b49e97c9
TS
8491 }
8492
b34976b6 8493 return TRUE;
b49e97c9
TS
8494}
8495\f
d0647110 8496bfd_boolean
9719ad41
RS
8497_bfd_mips_relax_section (bfd *abfd, asection *sec,
8498 struct bfd_link_info *link_info,
8499 bfd_boolean *again)
d0647110
AO
8500{
8501 Elf_Internal_Rela *internal_relocs;
8502 Elf_Internal_Rela *irel, *irelend;
8503 Elf_Internal_Shdr *symtab_hdr;
8504 bfd_byte *contents = NULL;
d0647110
AO
8505 size_t extsymoff;
8506 bfd_boolean changed_contents = FALSE;
8507 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8508 Elf_Internal_Sym *isymbuf = NULL;
8509
8510 /* We are not currently changing any sizes, so only one pass. */
8511 *again = FALSE;
8512
1049f94e 8513 if (link_info->relocatable)
d0647110
AO
8514 return TRUE;
8515
9719ad41 8516 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 8517 link_info->keep_memory);
d0647110
AO
8518 if (internal_relocs == NULL)
8519 return TRUE;
8520
8521 irelend = internal_relocs + sec->reloc_count
8522 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8523 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8524 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8525
8526 for (irel = internal_relocs; irel < irelend; irel++)
8527 {
8528 bfd_vma symval;
8529 bfd_signed_vma sym_offset;
8530 unsigned int r_type;
8531 unsigned long r_symndx;
8532 asection *sym_sec;
8533 unsigned long instruction;
8534
8535 /* Turn jalr into bgezal, and jr into beq, if they're marked
8536 with a JALR relocation, that indicate where they jump to.
8537 This saves some pipeline bubbles. */
8538 r_type = ELF_R_TYPE (abfd, irel->r_info);
8539 if (r_type != R_MIPS_JALR)
8540 continue;
8541
8542 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8543 /* Compute the address of the jump target. */
8544 if (r_symndx >= extsymoff)
8545 {
8546 struct mips_elf_link_hash_entry *h
8547 = ((struct mips_elf_link_hash_entry *)
8548 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8549
8550 while (h->root.root.type == bfd_link_hash_indirect
8551 || h->root.root.type == bfd_link_hash_warning)
8552 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 8553
d0647110
AO
8554 /* If a symbol is undefined, or if it may be overridden,
8555 skip it. */
8556 if (! ((h->root.root.type == bfd_link_hash_defined
8557 || h->root.root.type == bfd_link_hash_defweak)
8558 && h->root.root.u.def.section)
8559 || (link_info->shared && ! link_info->symbolic
f5385ebf 8560 && !h->root.forced_local))
d0647110
AO
8561 continue;
8562
8563 sym_sec = h->root.root.u.def.section;
8564 if (sym_sec->output_section)
8565 symval = (h->root.root.u.def.value
8566 + sym_sec->output_section->vma
8567 + sym_sec->output_offset);
8568 else
8569 symval = h->root.root.u.def.value;
8570 }
8571 else
8572 {
8573 Elf_Internal_Sym *isym;
8574
8575 /* Read this BFD's symbols if we haven't done so already. */
8576 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8577 {
8578 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8579 if (isymbuf == NULL)
8580 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8581 symtab_hdr->sh_info, 0,
8582 NULL, NULL, NULL);
8583 if (isymbuf == NULL)
8584 goto relax_return;
8585 }
8586
8587 isym = isymbuf + r_symndx;
8588 if (isym->st_shndx == SHN_UNDEF)
8589 continue;
8590 else if (isym->st_shndx == SHN_ABS)
8591 sym_sec = bfd_abs_section_ptr;
8592 else if (isym->st_shndx == SHN_COMMON)
8593 sym_sec = bfd_com_section_ptr;
8594 else
8595 sym_sec
8596 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8597 symval = isym->st_value
8598 + sym_sec->output_section->vma
8599 + sym_sec->output_offset;
8600 }
8601
8602 /* Compute branch offset, from delay slot of the jump to the
8603 branch target. */
8604 sym_offset = (symval + irel->r_addend)
8605 - (sec_start + irel->r_offset + 4);
8606
8607 /* Branch offset must be properly aligned. */
8608 if ((sym_offset & 3) != 0)
8609 continue;
8610
8611 sym_offset >>= 2;
8612
8613 /* Check that it's in range. */
8614 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8615 continue;
143d77c5 8616
d0647110 8617 /* Get the section contents if we haven't done so already. */
c224138d
RS
8618 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8619 goto relax_return;
d0647110
AO
8620
8621 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8622
8623 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8624 if ((instruction & 0xfc1fffff) == 0x0000f809)
8625 instruction = 0x04110000;
8626 /* If it was jr <reg>, turn it into b <target>. */
8627 else if ((instruction & 0xfc1fffff) == 0x00000008)
8628 instruction = 0x10000000;
8629 else
8630 continue;
8631
8632 instruction |= (sym_offset & 0xffff);
8633 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8634 changed_contents = TRUE;
8635 }
8636
8637 if (contents != NULL
8638 && elf_section_data (sec)->this_hdr.contents != contents)
8639 {
8640 if (!changed_contents && !link_info->keep_memory)
8641 free (contents);
8642 else
8643 {
8644 /* Cache the section contents for elf_link_input_bfd. */
8645 elf_section_data (sec)->this_hdr.contents = contents;
8646 }
8647 }
8648 return TRUE;
8649
143d77c5 8650 relax_return:
eea6121a
AM
8651 if (contents != NULL
8652 && elf_section_data (sec)->this_hdr.contents != contents)
8653 free (contents);
d0647110
AO
8654 return FALSE;
8655}
8656\f
9a59ad6b
DJ
8657/* Allocate space for global sym dynamic relocs. */
8658
8659static bfd_boolean
8660allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8661{
8662 struct bfd_link_info *info = inf;
8663 bfd *dynobj;
8664 struct mips_elf_link_hash_entry *hmips;
8665 struct mips_elf_link_hash_table *htab;
8666
8667 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8668 BFD_ASSERT (htab != NULL);
8669
9a59ad6b
DJ
8670 dynobj = elf_hash_table (info)->dynobj;
8671 hmips = (struct mips_elf_link_hash_entry *) h;
8672
8673 /* VxWorks executables are handled elsewhere; we only need to
8674 allocate relocations in shared objects. */
8675 if (htab->is_vxworks && !info->shared)
8676 return TRUE;
8677
7686d77d
AM
8678 /* Ignore indirect symbols. All relocations against such symbols
8679 will be redirected to the target symbol. */
8680 if (h->root.type == bfd_link_hash_indirect)
63897e2c
RS
8681 return TRUE;
8682
9a59ad6b
DJ
8683 /* If this symbol is defined in a dynamic object, or we are creating
8684 a shared library, we will need to copy any R_MIPS_32 or
8685 R_MIPS_REL32 relocs against it into the output file. */
8686 if (! info->relocatable
8687 && hmips->possibly_dynamic_relocs != 0
8688 && (h->root.type == bfd_link_hash_defweak
625ef6dc 8689 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9a59ad6b
DJ
8690 || info->shared))
8691 {
8692 bfd_boolean do_copy = TRUE;
8693
8694 if (h->root.type == bfd_link_hash_undefweak)
8695 {
8696 /* Do not copy relocations for undefined weak symbols with
8697 non-default visibility. */
8698 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8699 do_copy = FALSE;
8700
8701 /* Make sure undefined weak symbols are output as a dynamic
8702 symbol in PIEs. */
8703 else if (h->dynindx == -1 && !h->forced_local)
8704 {
8705 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8706 return FALSE;
8707 }
8708 }
8709
8710 if (do_copy)
8711 {
aff469fa 8712 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
8713 the SVR4 psABI requires it to have a dynamic symbol table
8714 index greater that DT_MIPS_GOTSYM if there are dynamic
8715 relocations against it.
8716
8717 VxWorks does not enforce the same mapping between the GOT
8718 and the symbol table, so the same requirement does not
8719 apply there. */
6ccf4795
RS
8720 if (!htab->is_vxworks)
8721 {
8722 if (hmips->global_got_area > GGA_RELOC_ONLY)
8723 hmips->global_got_area = GGA_RELOC_ONLY;
8724 hmips->got_only_for_calls = FALSE;
8725 }
aff469fa 8726
9a59ad6b
DJ
8727 mips_elf_allocate_dynamic_relocations
8728 (dynobj, info, hmips->possibly_dynamic_relocs);
8729 if (hmips->readonly_reloc)
8730 /* We tell the dynamic linker that there are relocations
8731 against the text segment. */
8732 info->flags |= DF_TEXTREL;
8733 }
8734 }
8735
8736 return TRUE;
8737}
8738
b49e97c9
TS
8739/* Adjust a symbol defined by a dynamic object and referenced by a
8740 regular object. The current definition is in some section of the
8741 dynamic object, but we're not including those sections. We have to
8742 change the definition to something the rest of the link can
8743 understand. */
8744
b34976b6 8745bfd_boolean
9719ad41
RS
8746_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8747 struct elf_link_hash_entry *h)
b49e97c9
TS
8748{
8749 bfd *dynobj;
8750 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8751 struct mips_elf_link_hash_table *htab;
b49e97c9 8752
5108fc1b 8753 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8754 BFD_ASSERT (htab != NULL);
8755
b49e97c9 8756 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8757 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8758
8759 /* Make sure we know what is going on here. */
8760 BFD_ASSERT (dynobj != NULL
f5385ebf 8761 && (h->needs_plt
f6e332e6 8762 || h->u.weakdef != NULL
f5385ebf
AM
8763 || (h->def_dynamic
8764 && h->ref_regular
8765 && !h->def_regular)));
b49e97c9 8766
b49e97c9 8767 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8768
861fb55a
DJ
8769 /* If there are call relocations against an externally-defined symbol,
8770 see whether we can create a MIPS lazy-binding stub for it. We can
8771 only do this if all references to the function are through call
8772 relocations, and in that case, the traditional lazy-binding stubs
8773 are much more efficient than PLT entries.
8774
8775 Traditional stubs are only available on SVR4 psABI-based systems;
8776 VxWorks always uses PLTs instead. */
8777 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8778 {
8779 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8780 return TRUE;
b49e97c9
TS
8781
8782 /* If this symbol is not defined in a regular file, then set
8783 the symbol to the stub location. This is required to make
8784 function pointers compare as equal between the normal
8785 executable and the shared library. */
f5385ebf 8786 if (!h->def_regular)
b49e97c9 8787 {
33bb52fb
RS
8788 hmips->needs_lazy_stub = TRUE;
8789 htab->lazy_stub_count++;
b34976b6 8790 return TRUE;
b49e97c9
TS
8791 }
8792 }
861fb55a
DJ
8793 /* As above, VxWorks requires PLT entries for externally-defined
8794 functions that are only accessed through call relocations.
b49e97c9 8795
861fb55a
DJ
8796 Both VxWorks and non-VxWorks targets also need PLT entries if there
8797 are static-only relocations against an externally-defined function.
8798 This can technically occur for shared libraries if there are
8799 branches to the symbol, although it is unlikely that this will be
8800 used in practice due to the short ranges involved. It can occur
8801 for any relative or absolute relocation in executables; in that
8802 case, the PLT entry becomes the function's canonical address. */
8803 else if (((h->needs_plt && !hmips->no_fn_stub)
8804 || (h->type == STT_FUNC && hmips->has_static_relocs))
8805 && htab->use_plts_and_copy_relocs
8806 && !SYMBOL_CALLS_LOCAL (info, h)
8807 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8808 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8809 {
1bbce132
MR
8810 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
8811 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
8812
8813 /* If this is the first symbol to need a PLT entry, then make some
8814 basic setup. Also work out PLT entry sizes. We'll need them
8815 for PLT offset calculations. */
8816 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
861fb55a
DJ
8817 {
8818 BFD_ASSERT (htab->sgotplt->size == 0);
1bbce132 8819 BFD_ASSERT (htab->plt_got_index == 0);
0a44bf69 8820
861fb55a
DJ
8821 /* If we're using the PLT additions to the psABI, each PLT
8822 entry is 16 bytes and the PLT0 entry is 32 bytes.
8823 Encourage better cache usage by aligning. We do this
8824 lazily to avoid pessimizing traditional objects. */
8825 if (!htab->is_vxworks
8826 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8827 return FALSE;
0a44bf69 8828
861fb55a
DJ
8829 /* Make sure that .got.plt is word-aligned. We do this lazily
8830 for the same reason as above. */
8831 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8832 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8833 return FALSE;
0a44bf69 8834
861fb55a
DJ
8835 /* On non-VxWorks targets, the first two entries in .got.plt
8836 are reserved. */
8837 if (!htab->is_vxworks)
1bbce132
MR
8838 htab->plt_got_index
8839 += (get_elf_backend_data (dynobj)->got_header_size
8840 / MIPS_ELF_GOT_SIZE (dynobj));
0a44bf69 8841
861fb55a
DJ
8842 /* On VxWorks, also allocate room for the header's
8843 .rela.plt.unloaded entries. */
8844 if (htab->is_vxworks && !info->shared)
0a44bf69 8845 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
1bbce132
MR
8846
8847 /* Now work out the sizes of individual PLT entries. */
8848 if (htab->is_vxworks && info->shared)
8849 htab->plt_mips_entry_size
8850 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
8851 else if (htab->is_vxworks)
8852 htab->plt_mips_entry_size
8853 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
8854 else if (newabi_p)
8855 htab->plt_mips_entry_size
8856 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
833794fc 8857 else if (!micromips_p)
1bbce132
MR
8858 {
8859 htab->plt_mips_entry_size
8860 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8861 htab->plt_comp_entry_size
833794fc
MR
8862 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
8863 }
8864 else if (htab->insn32)
8865 {
8866 htab->plt_mips_entry_size
8867 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8868 htab->plt_comp_entry_size
8869 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
1bbce132
MR
8870 }
8871 else
8872 {
8873 htab->plt_mips_entry_size
8874 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8875 htab->plt_comp_entry_size
833794fc 8876 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
1bbce132 8877 }
0a44bf69
RS
8878 }
8879
1bbce132
MR
8880 if (h->plt.plist == NULL)
8881 h->plt.plist = mips_elf_make_plt_record (dynobj);
8882 if (h->plt.plist == NULL)
8883 return FALSE;
8884
8885 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
8886 n32 or n64, so always use a standard entry there.
8887
8888 If the symbol has a MIPS16 call stub and gets a PLT entry, then
8889 all MIPS16 calls will go via that stub, and there is no benefit
8890 to having a MIPS16 entry. And in the case of call_stub a
8891 standard entry actually has to be used as the stub ends with a J
8892 instruction. */
8893 if (newabi_p
8894 || htab->is_vxworks
8895 || hmips->call_stub
8896 || hmips->call_fp_stub)
8897 {
8898 h->plt.plist->need_mips = TRUE;
8899 h->plt.plist->need_comp = FALSE;
8900 }
8901
8902 /* Otherwise, if there are no direct calls to the function, we
8903 have a free choice of whether to use standard or compressed
8904 entries. Prefer microMIPS entries if the object is known to
8905 contain microMIPS code, so that it becomes possible to create
8906 pure microMIPS binaries. Prefer standard entries otherwise,
8907 because MIPS16 ones are no smaller and are usually slower. */
8908 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
8909 {
8910 if (micromips_p)
8911 h->plt.plist->need_comp = TRUE;
8912 else
8913 h->plt.plist->need_mips = TRUE;
8914 }
8915
8916 if (h->plt.plist->need_mips)
8917 {
8918 h->plt.plist->mips_offset = htab->plt_mips_offset;
8919 htab->plt_mips_offset += htab->plt_mips_entry_size;
8920 }
8921 if (h->plt.plist->need_comp)
8922 {
8923 h->plt.plist->comp_offset = htab->plt_comp_offset;
8924 htab->plt_comp_offset += htab->plt_comp_entry_size;
8925 }
8926
8927 /* Reserve the corresponding .got.plt entry now too. */
8928 h->plt.plist->gotplt_index = htab->plt_got_index++;
0a44bf69
RS
8929
8930 /* If the output file has no definition of the symbol, set the
861fb55a 8931 symbol's value to the address of the stub. */
131eb6b7 8932 if (!info->shared && !h->def_regular)
1bbce132 8933 hmips->use_plt_entry = TRUE;
0a44bf69 8934
1bbce132 8935 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
861fb55a
DJ
8936 htab->srelplt->size += (htab->is_vxworks
8937 ? MIPS_ELF_RELA_SIZE (dynobj)
8938 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8939
8940 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8941 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8942 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8943
861fb55a
DJ
8944 /* All relocations against this symbol that could have been made
8945 dynamic will now refer to the PLT entry instead. */
8946 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8947
0a44bf69
RS
8948 return TRUE;
8949 }
8950
8951 /* If this is a weak symbol, and there is a real definition, the
8952 processor independent code will have arranged for us to see the
8953 real definition first, and we can just use the same value. */
8954 if (h->u.weakdef != NULL)
8955 {
8956 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8957 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8958 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8959 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8960 return TRUE;
8961 }
8962
861fb55a
DJ
8963 /* Otherwise, there is nothing further to do for symbols defined
8964 in regular objects. */
8965 if (h->def_regular)
0a44bf69
RS
8966 return TRUE;
8967
861fb55a
DJ
8968 /* There's also nothing more to do if we'll convert all relocations
8969 against this symbol into dynamic relocations. */
8970 if (!hmips->has_static_relocs)
8971 return TRUE;
8972
8973 /* We're now relying on copy relocations. Complain if we have
8974 some that we can't convert. */
8975 if (!htab->use_plts_and_copy_relocs || info->shared)
8976 {
8977 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8978 "dynamic symbol %s"),
8979 h->root.root.string);
8980 bfd_set_error (bfd_error_bad_value);
8981 return FALSE;
8982 }
8983
0a44bf69
RS
8984 /* We must allocate the symbol in our .dynbss section, which will
8985 become part of the .bss section of the executable. There will be
8986 an entry for this symbol in the .dynsym section. The dynamic
8987 object will contain position independent code, so all references
8988 from the dynamic object to this symbol will go through the global
8989 offset table. The dynamic linker will use the .dynsym entry to
8990 determine the address it must put in the global offset table, so
8991 both the dynamic object and the regular object will refer to the
8992 same memory location for the variable. */
8993
8994 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8995 {
861fb55a
DJ
8996 if (htab->is_vxworks)
8997 htab->srelbss->size += sizeof (Elf32_External_Rela);
8998 else
8999 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
9000 h->needs_copy = 1;
9001 }
9002
861fb55a
DJ
9003 /* All relocations against this symbol that could have been made
9004 dynamic will now refer to the local copy instead. */
9005 hmips->possibly_dynamic_relocs = 0;
9006
027297b7 9007 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 9008}
b49e97c9
TS
9009\f
9010/* This function is called after all the input files have been read,
9011 and the input sections have been assigned to output sections. We
9012 check for any mips16 stub sections that we can discard. */
9013
b34976b6 9014bfd_boolean
9719ad41
RS
9015_bfd_mips_elf_always_size_sections (bfd *output_bfd,
9016 struct bfd_link_info *info)
b49e97c9
TS
9017{
9018 asection *ri;
0a44bf69 9019 struct mips_elf_link_hash_table *htab;
861fb55a 9020 struct mips_htab_traverse_info hti;
0a44bf69
RS
9021
9022 htab = mips_elf_hash_table (info);
4dfe6ac6 9023 BFD_ASSERT (htab != NULL);
f4416af6 9024
b49e97c9
TS
9025 /* The .reginfo section has a fixed size. */
9026 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
9027 if (ri != NULL)
9719ad41 9028 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 9029
861fb55a
DJ
9030 hti.info = info;
9031 hti.output_bfd = output_bfd;
9032 hti.error = FALSE;
9033 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9034 mips_elf_check_symbols, &hti);
9035 if (hti.error)
9036 return FALSE;
f4416af6 9037
33bb52fb
RS
9038 return TRUE;
9039}
9040
9041/* If the link uses a GOT, lay it out and work out its size. */
9042
9043static bfd_boolean
9044mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9045{
9046 bfd *dynobj;
9047 asection *s;
9048 struct mips_got_info *g;
33bb52fb
RS
9049 bfd_size_type loadable_size = 0;
9050 bfd_size_type page_gotno;
d7206569 9051 bfd *ibfd;
ab361d49 9052 struct mips_elf_traverse_got_arg tga;
33bb52fb
RS
9053 struct mips_elf_link_hash_table *htab;
9054
9055 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9056 BFD_ASSERT (htab != NULL);
9057
a8028dd0 9058 s = htab->sgot;
f4416af6 9059 if (s == NULL)
b34976b6 9060 return TRUE;
b49e97c9 9061
33bb52fb 9062 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
9063 g = htab->got_info;
9064
861fb55a
DJ
9065 /* Allocate room for the reserved entries. VxWorks always reserves
9066 3 entries; other objects only reserve 2 entries. */
9067 BFD_ASSERT (g->assigned_gotno == 0);
9068 if (htab->is_vxworks)
9069 htab->reserved_gotno = 3;
9070 else
9071 htab->reserved_gotno = 2;
9072 g->local_gotno += htab->reserved_gotno;
9073 g->assigned_gotno = htab->reserved_gotno;
9074
6c42ddb9
RS
9075 /* Decide which symbols need to go in the global part of the GOT and
9076 count the number of reloc-only GOT symbols. */
020d7251 9077 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 9078
13db6b44
RS
9079 if (!mips_elf_resolve_final_got_entries (info, g))
9080 return FALSE;
9081
33bb52fb
RS
9082 /* Calculate the total loadable size of the output. That
9083 will give us the maximum number of GOT_PAGE entries
9084 required. */
d7206569 9085 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
33bb52fb
RS
9086 {
9087 asection *subsection;
5108fc1b 9088
d7206569 9089 for (subsection = ibfd->sections;
33bb52fb
RS
9090 subsection;
9091 subsection = subsection->next)
9092 {
9093 if ((subsection->flags & SEC_ALLOC) == 0)
9094 continue;
9095 loadable_size += ((subsection->size + 0xf)
9096 &~ (bfd_size_type) 0xf);
9097 }
9098 }
f4416af6 9099
0a44bf69 9100 if (htab->is_vxworks)
738e5348 9101 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
9102 relocations against local symbols evaluate to "G", and the EABI does
9103 not include R_MIPS_GOT_PAGE. */
c224138d 9104 page_gotno = 0;
0a44bf69
RS
9105 else
9106 /* Assume there are two loadable segments consisting of contiguous
9107 sections. Is 5 enough? */
c224138d
RS
9108 page_gotno = (loadable_size >> 16) + 5;
9109
13db6b44 9110 /* Choose the smaller of the two page estimates; both are intended to be
c224138d
RS
9111 conservative. */
9112 if (page_gotno > g->page_gotno)
9113 page_gotno = g->page_gotno;
f4416af6 9114
c224138d 9115 g->local_gotno += page_gotno;
ab361d49 9116
ab361d49
RS
9117 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9118 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
0f20cc35
DJ
9119 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9120
0a44bf69
RS
9121 /* VxWorks does not support multiple GOTs. It initializes $gp to
9122 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9123 dynamic loader. */
57093f5e 9124 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 9125 {
a8028dd0 9126 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
9127 return FALSE;
9128 }
9129 else
9130 {
d7206569
RS
9131 /* Record that all bfds use G. This also has the effect of freeing
9132 the per-bfd GOTs, which we no longer need. */
9133 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9134 if (mips_elf_bfd_got (ibfd, FALSE))
9135 mips_elf_replace_bfd_got (ibfd, g);
9136 mips_elf_replace_bfd_got (output_bfd, g);
9137
33bb52fb 9138 /* Set up TLS entries. */
0f20cc35 9139 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
72e7511a
RS
9140 tga.info = info;
9141 tga.g = g;
9142 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9143 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9144 if (!tga.g)
9145 return FALSE;
1fd20d70
RS
9146 BFD_ASSERT (g->tls_assigned_gotno
9147 == g->global_gotno + g->local_gotno + g->tls_gotno);
33bb52fb 9148
57093f5e
RS
9149 /* Each VxWorks GOT entry needs an explicit relocation. */
9150 if (htab->is_vxworks && info->shared)
9151 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9152
33bb52fb 9153 /* Allocate room for the TLS relocations. */
ab361d49
RS
9154 if (g->relocs)
9155 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
0f20cc35 9156 }
b49e97c9 9157
b34976b6 9158 return TRUE;
b49e97c9
TS
9159}
9160
33bb52fb
RS
9161/* Estimate the size of the .MIPS.stubs section. */
9162
9163static void
9164mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9165{
9166 struct mips_elf_link_hash_table *htab;
9167 bfd_size_type dynsymcount;
9168
9169 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9170 BFD_ASSERT (htab != NULL);
9171
33bb52fb
RS
9172 if (htab->lazy_stub_count == 0)
9173 return;
9174
9175 /* IRIX rld assumes that a function stub isn't at the end of the .text
9176 section, so add a dummy entry to the end. */
9177 htab->lazy_stub_count++;
9178
9179 /* Get a worst-case estimate of the number of dynamic symbols needed.
9180 At this point, dynsymcount does not account for section symbols
9181 and count_section_dynsyms may overestimate the number that will
9182 be needed. */
9183 dynsymcount = (elf_hash_table (info)->dynsymcount
9184 + count_section_dynsyms (output_bfd, info));
9185
1bbce132
MR
9186 /* Determine the size of one stub entry. There's no disadvantage
9187 from using microMIPS code here, so for the sake of pure-microMIPS
9188 binaries we prefer it whenever there's any microMIPS code in
9189 output produced at all. This has a benefit of stubs being
833794fc
MR
9190 shorter by 4 bytes each too, unless in the insn32 mode. */
9191 if (!MICROMIPS_P (output_bfd))
1bbce132
MR
9192 htab->function_stub_size = (dynsymcount > 0x10000
9193 ? MIPS_FUNCTION_STUB_BIG_SIZE
9194 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
833794fc
MR
9195 else if (htab->insn32)
9196 htab->function_stub_size = (dynsymcount > 0x10000
9197 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9198 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9199 else
9200 htab->function_stub_size = (dynsymcount > 0x10000
9201 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9202 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
33bb52fb
RS
9203
9204 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9205}
9206
1bbce132
MR
9207/* A mips_elf_link_hash_traverse callback for which DATA points to a
9208 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9209 stub, allocate an entry in the stubs section. */
33bb52fb
RS
9210
9211static bfd_boolean
af924177 9212mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 9213{
1bbce132 9214 struct mips_htab_traverse_info *hti = data;
33bb52fb 9215 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9216 struct bfd_link_info *info;
9217 bfd *output_bfd;
9218
9219 info = hti->info;
9220 output_bfd = hti->output_bfd;
9221 htab = mips_elf_hash_table (info);
9222 BFD_ASSERT (htab != NULL);
33bb52fb 9223
33bb52fb
RS
9224 if (h->needs_lazy_stub)
9225 {
1bbce132
MR
9226 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9227 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9228 bfd_vma isa_bit = micromips_p;
9229
9230 BFD_ASSERT (htab->root.dynobj != NULL);
9231 if (h->root.plt.plist == NULL)
9232 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9233 if (h->root.plt.plist == NULL)
9234 {
9235 hti->error = TRUE;
9236 return FALSE;
9237 }
33bb52fb 9238 h->root.root.u.def.section = htab->sstubs;
1bbce132
MR
9239 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9240 h->root.plt.plist->stub_offset = htab->sstubs->size;
9241 h->root.other = other;
33bb52fb
RS
9242 htab->sstubs->size += htab->function_stub_size;
9243 }
9244 return TRUE;
9245}
9246
9247/* Allocate offsets in the stubs section to each symbol that needs one.
9248 Set the final size of the .MIPS.stub section. */
9249
1bbce132 9250static bfd_boolean
33bb52fb
RS
9251mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9252{
1bbce132
MR
9253 bfd *output_bfd = info->output_bfd;
9254 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9255 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9256 bfd_vma isa_bit = micromips_p;
33bb52fb 9257 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9258 struct mips_htab_traverse_info hti;
9259 struct elf_link_hash_entry *h;
9260 bfd *dynobj;
33bb52fb
RS
9261
9262 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9263 BFD_ASSERT (htab != NULL);
9264
33bb52fb 9265 if (htab->lazy_stub_count == 0)
1bbce132 9266 return TRUE;
33bb52fb
RS
9267
9268 htab->sstubs->size = 0;
1bbce132
MR
9269 hti.info = info;
9270 hti.output_bfd = output_bfd;
9271 hti.error = FALSE;
9272 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9273 if (hti.error)
9274 return FALSE;
33bb52fb
RS
9275 htab->sstubs->size += htab->function_stub_size;
9276 BFD_ASSERT (htab->sstubs->size
9277 == htab->lazy_stub_count * htab->function_stub_size);
1bbce132
MR
9278
9279 dynobj = elf_hash_table (info)->dynobj;
9280 BFD_ASSERT (dynobj != NULL);
9281 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9282 if (h == NULL)
9283 return FALSE;
9284 h->root.u.def.value = isa_bit;
9285 h->other = other;
9286 h->type = STT_FUNC;
9287
9288 return TRUE;
9289}
9290
9291/* A mips_elf_link_hash_traverse callback for which DATA points to a
9292 bfd_link_info. If H uses the address of a PLT entry as the value
9293 of the symbol, then set the entry in the symbol table now. Prefer
9294 a standard MIPS PLT entry. */
9295
9296static bfd_boolean
9297mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9298{
9299 struct bfd_link_info *info = data;
9300 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9301 struct mips_elf_link_hash_table *htab;
9302 unsigned int other;
9303 bfd_vma isa_bit;
9304 bfd_vma val;
9305
9306 htab = mips_elf_hash_table (info);
9307 BFD_ASSERT (htab != NULL);
9308
9309 if (h->use_plt_entry)
9310 {
9311 BFD_ASSERT (h->root.plt.plist != NULL);
9312 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9313 || h->root.plt.plist->comp_offset != MINUS_ONE);
9314
9315 val = htab->plt_header_size;
9316 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9317 {
9318 isa_bit = 0;
9319 val += h->root.plt.plist->mips_offset;
9320 other = 0;
9321 }
9322 else
9323 {
9324 isa_bit = 1;
9325 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9326 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9327 }
9328 val += isa_bit;
9329 /* For VxWorks, point at the PLT load stub rather than the lazy
9330 resolution stub; this stub will become the canonical function
9331 address. */
9332 if (htab->is_vxworks)
9333 val += 8;
9334
9335 h->root.root.u.def.section = htab->splt;
9336 h->root.root.u.def.value = val;
9337 h->root.other = other;
9338 }
9339
9340 return TRUE;
33bb52fb
RS
9341}
9342
b49e97c9
TS
9343/* Set the sizes of the dynamic sections. */
9344
b34976b6 9345bfd_boolean
9719ad41
RS
9346_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9347 struct bfd_link_info *info)
b49e97c9
TS
9348{
9349 bfd *dynobj;
861fb55a 9350 asection *s, *sreldyn;
b34976b6 9351 bfd_boolean reltext;
0a44bf69 9352 struct mips_elf_link_hash_table *htab;
b49e97c9 9353
0a44bf69 9354 htab = mips_elf_hash_table (info);
4dfe6ac6 9355 BFD_ASSERT (htab != NULL);
b49e97c9
TS
9356 dynobj = elf_hash_table (info)->dynobj;
9357 BFD_ASSERT (dynobj != NULL);
9358
9359 if (elf_hash_table (info)->dynamic_sections_created)
9360 {
9361 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 9362 if (info->executable)
b49e97c9 9363 {
3d4d4302 9364 s = bfd_get_linker_section (dynobj, ".interp");
b49e97c9 9365 BFD_ASSERT (s != NULL);
eea6121a 9366 s->size
b49e97c9
TS
9367 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9368 s->contents
9369 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9370 }
861fb55a 9371
1bbce132
MR
9372 /* Figure out the size of the PLT header if we know that we
9373 are using it. For the sake of cache alignment always use
9374 a standard header whenever any standard entries are present
9375 even if microMIPS entries are present as well. This also
9376 lets the microMIPS header rely on the value of $v0 only set
9377 by microMIPS entries, for a small size reduction.
9378
9379 Set symbol table entry values for symbols that use the
9380 address of their PLT entry now that we can calculate it.
9381
9382 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9383 haven't already in _bfd_elf_create_dynamic_sections. */
9384 if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
861fb55a 9385 {
1bbce132
MR
9386 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9387 && !htab->plt_mips_offset);
9388 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9389 bfd_vma isa_bit = micromips_p;
861fb55a 9390 struct elf_link_hash_entry *h;
1bbce132 9391 bfd_vma size;
861fb55a
DJ
9392
9393 BFD_ASSERT (htab->use_plts_and_copy_relocs);
1bbce132
MR
9394 BFD_ASSERT (htab->sgotplt->size == 0);
9395 BFD_ASSERT (htab->splt->size == 0);
9396
9397 if (htab->is_vxworks && info->shared)
9398 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9399 else if (htab->is_vxworks)
9400 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9401 else if (ABI_64_P (output_bfd))
9402 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9403 else if (ABI_N32_P (output_bfd))
9404 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9405 else if (!micromips_p)
9406 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
833794fc
MR
9407 else if (htab->insn32)
9408 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
1bbce132
MR
9409 else
9410 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
861fb55a 9411
1bbce132
MR
9412 htab->plt_header_is_comp = micromips_p;
9413 htab->plt_header_size = size;
9414 htab->splt->size = (size
9415 + htab->plt_mips_offset
9416 + htab->plt_comp_offset);
9417 htab->sgotplt->size = (htab->plt_got_index
9418 * MIPS_ELF_GOT_SIZE (dynobj));
9419
9420 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9421
9422 if (htab->root.hplt == NULL)
9423 {
9424 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9425 "_PROCEDURE_LINKAGE_TABLE_");
9426 htab->root.hplt = h;
9427 if (h == NULL)
9428 return FALSE;
9429 }
9430
9431 h = htab->root.hplt;
9432 h->root.u.def.value = isa_bit;
9433 h->other = other;
861fb55a
DJ
9434 h->type = STT_FUNC;
9435 }
9436 }
4e41d0d7 9437
9a59ad6b 9438 /* Allocate space for global sym dynamic relocs. */
2c3fc389 9439 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9a59ad6b 9440
33bb52fb
RS
9441 mips_elf_estimate_stub_size (output_bfd, info);
9442
9443 if (!mips_elf_lay_out_got (output_bfd, info))
9444 return FALSE;
9445
9446 mips_elf_lay_out_lazy_stubs (info);
9447
b49e97c9
TS
9448 /* The check_relocs and adjust_dynamic_symbol entry points have
9449 determined the sizes of the various dynamic sections. Allocate
9450 memory for them. */
b34976b6 9451 reltext = FALSE;
b49e97c9
TS
9452 for (s = dynobj->sections; s != NULL; s = s->next)
9453 {
9454 const char *name;
b49e97c9
TS
9455
9456 /* It's OK to base decisions on the section name, because none
9457 of the dynobj section names depend upon the input files. */
9458 name = bfd_get_section_name (dynobj, s);
9459
9460 if ((s->flags & SEC_LINKER_CREATED) == 0)
9461 continue;
9462
0112cd26 9463 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 9464 {
c456f082 9465 if (s->size != 0)
b49e97c9
TS
9466 {
9467 const char *outname;
9468 asection *target;
9469
9470 /* If this relocation section applies to a read only
9471 section, then we probably need a DT_TEXTREL entry.
0a44bf69 9472 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
9473 assert a DT_TEXTREL entry rather than testing whether
9474 there exists a relocation to a read only section or
9475 not. */
9476 outname = bfd_get_section_name (output_bfd,
9477 s->output_section);
9478 target = bfd_get_section_by_name (output_bfd, outname + 4);
9479 if ((target != NULL
9480 && (target->flags & SEC_READONLY) != 0
9481 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 9482 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 9483 reltext = TRUE;
b49e97c9
TS
9484
9485 /* We use the reloc_count field as a counter if we need
9486 to copy relocs into the output file. */
0a44bf69 9487 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 9488 s->reloc_count = 0;
f4416af6
AO
9489
9490 /* If combreloc is enabled, elf_link_sort_relocs() will
9491 sort relocations, but in a different way than we do,
9492 and before we're done creating relocations. Also, it
9493 will move them around between input sections'
9494 relocation's contents, so our sorting would be
9495 broken, so don't let it run. */
9496 info->combreloc = 0;
b49e97c9
TS
9497 }
9498 }
b49e97c9
TS
9499 else if (! info->shared
9500 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 9501 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 9502 {
5108fc1b 9503 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 9504 rtld to contain a pointer to the _r_debug structure. */
b4082c70 9505 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
b49e97c9
TS
9506 }
9507 else if (SGI_COMPAT (output_bfd)
0112cd26 9508 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 9509 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
9510 else if (s == htab->splt)
9511 {
9512 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
9513 room for an extra nop to fill the delay slot. This is
9514 for CPUs without load interlocking. */
9515 if (! LOAD_INTERLOCKS_P (output_bfd)
9516 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
9517 s->size += 4;
9518 }
0112cd26 9519 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 9520 && s != htab->sgot
0a44bf69 9521 && s != htab->sgotplt
861fb55a
DJ
9522 && s != htab->sstubs
9523 && s != htab->sdynbss)
b49e97c9
TS
9524 {
9525 /* It's not one of our sections, so don't allocate space. */
9526 continue;
9527 }
9528
c456f082 9529 if (s->size == 0)
b49e97c9 9530 {
8423293d 9531 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
9532 continue;
9533 }
9534
c456f082
AM
9535 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9536 continue;
9537
b49e97c9 9538 /* Allocate memory for the section contents. */
eea6121a 9539 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 9540 if (s->contents == NULL)
b49e97c9
TS
9541 {
9542 bfd_set_error (bfd_error_no_memory);
b34976b6 9543 return FALSE;
b49e97c9
TS
9544 }
9545 }
9546
9547 if (elf_hash_table (info)->dynamic_sections_created)
9548 {
9549 /* Add some entries to the .dynamic section. We fill in the
9550 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9551 must add the entries now so that we get the correct size for
5750dcec 9552 the .dynamic section. */
af5978fb
RS
9553
9554 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec 9555 DT_MIPS_RLD_MAP entry. This must come first because glibc
6e6be592
MR
9556 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9557 may only look at the first one they see. */
af5978fb
RS
9558 if (!info->shared
9559 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9560 return FALSE;
b49e97c9 9561
5750dcec
DJ
9562 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9563 used by the debugger. */
9564 if (info->executable
9565 && !SGI_COMPAT (output_bfd)
9566 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9567 return FALSE;
9568
0a44bf69 9569 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
9570 info->flags |= DF_TEXTREL;
9571
9572 if ((info->flags & DF_TEXTREL) != 0)
9573 {
9574 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 9575 return FALSE;
943284cc
DJ
9576
9577 /* Clear the DF_TEXTREL flag. It will be set again if we
9578 write out an actual text relocation; we may not, because
9579 at this point we do not know whether e.g. any .eh_frame
9580 absolute relocations have been converted to PC-relative. */
9581 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
9582 }
9583
9584 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 9585 return FALSE;
b49e97c9 9586
861fb55a 9587 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 9588 if (htab->is_vxworks)
b49e97c9 9589 {
0a44bf69
RS
9590 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9591 use any of the DT_MIPS_* tags. */
861fb55a 9592 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
9593 {
9594 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9595 return FALSE;
b49e97c9 9596
0a44bf69
RS
9597 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9598 return FALSE;
b49e97c9 9599
0a44bf69
RS
9600 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9601 return FALSE;
9602 }
b49e97c9 9603 }
0a44bf69
RS
9604 else
9605 {
861fb55a 9606 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
9607 {
9608 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9609 return FALSE;
b49e97c9 9610
0a44bf69
RS
9611 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9612 return FALSE;
b49e97c9 9613
0a44bf69
RS
9614 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9615 return FALSE;
9616 }
b49e97c9 9617
0a44bf69
RS
9618 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9619 return FALSE;
b49e97c9 9620
0a44bf69
RS
9621 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9622 return FALSE;
b49e97c9 9623
0a44bf69
RS
9624 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9625 return FALSE;
b49e97c9 9626
0a44bf69
RS
9627 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9628 return FALSE;
b49e97c9 9629
0a44bf69
RS
9630 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9631 return FALSE;
b49e97c9 9632
0a44bf69
RS
9633 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9634 return FALSE;
b49e97c9 9635
0a44bf69
RS
9636 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9637 return FALSE;
9638
9639 if (IRIX_COMPAT (dynobj) == ict_irix5
9640 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9641 return FALSE;
9642
9643 if (IRIX_COMPAT (dynobj) == ict_irix6
9644 && (bfd_get_section_by_name
af0edeb8 9645 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
0a44bf69
RS
9646 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9647 return FALSE;
9648 }
861fb55a
DJ
9649 if (htab->splt->size > 0)
9650 {
9651 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9652 return FALSE;
9653
9654 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9655 return FALSE;
9656
9657 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9658 return FALSE;
9659
9660 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9661 return FALSE;
9662 }
7a2b07ff
NS
9663 if (htab->is_vxworks
9664 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9665 return FALSE;
b49e97c9
TS
9666 }
9667
b34976b6 9668 return TRUE;
b49e97c9
TS
9669}
9670\f
81d43bff
RS
9671/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9672 Adjust its R_ADDEND field so that it is correct for the output file.
9673 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9674 and sections respectively; both use symbol indexes. */
9675
9676static void
9677mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9678 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9679 asection **local_sections, Elf_Internal_Rela *rel)
9680{
9681 unsigned int r_type, r_symndx;
9682 Elf_Internal_Sym *sym;
9683 asection *sec;
9684
020d7251 9685 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
9686 {
9687 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
df58fc94 9688 if (gprel16_reloc_p (r_type)
81d43bff 9689 || r_type == R_MIPS_GPREL32
df58fc94 9690 || literal_reloc_p (r_type))
81d43bff
RS
9691 {
9692 rel->r_addend += _bfd_get_gp_value (input_bfd);
9693 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9694 }
9695
9696 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9697 sym = local_syms + r_symndx;
9698
9699 /* Adjust REL's addend to account for section merging. */
9700 if (!info->relocatable)
9701 {
9702 sec = local_sections[r_symndx];
9703 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9704 }
9705
9706 /* This would normally be done by the rela_normal code in elflink.c. */
9707 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9708 rel->r_addend += local_sections[r_symndx]->output_offset;
9709 }
9710}
9711
545fd46b
MR
9712/* Handle relocations against symbols from removed linkonce sections,
9713 or sections discarded by a linker script. We use this wrapper around
9714 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9715 on 64-bit ELF targets. In this case for any relocation handled, which
9716 always be the first in a triplet, the remaining two have to be processed
9717 together with the first, even if they are R_MIPS_NONE. It is the symbol
9718 index referred by the first reloc that applies to all the three and the
9719 remaining two never refer to an object symbol. And it is the final
9720 relocation (the last non-null one) that determines the output field of
9721 the whole relocation so retrieve the corresponding howto structure for
9722 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9723
9724 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9725 and therefore requires to be pasted in a loop. It also defines a block
9726 and does not protect any of its arguments, hence the extra brackets. */
9727
9728static void
9729mips_reloc_against_discarded_section (bfd *output_bfd,
9730 struct bfd_link_info *info,
9731 bfd *input_bfd, asection *input_section,
9732 Elf_Internal_Rela **rel,
9733 const Elf_Internal_Rela **relend,
9734 bfd_boolean rel_reloc,
9735 reloc_howto_type *howto,
9736 bfd_byte *contents)
9737{
9738 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9739 int count = bed->s->int_rels_per_ext_rel;
9740 unsigned int r_type;
9741 int i;
9742
9743 for (i = count - 1; i > 0; i--)
9744 {
9745 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9746 if (r_type != R_MIPS_NONE)
9747 {
9748 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9749 break;
9750 }
9751 }
9752 do
9753 {
9754 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9755 (*rel), count, (*relend),
9756 howto, i, contents);
9757 }
9758 while (0);
9759}
9760
b49e97c9
TS
9761/* Relocate a MIPS ELF section. */
9762
b34976b6 9763bfd_boolean
9719ad41
RS
9764_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9765 bfd *input_bfd, asection *input_section,
9766 bfd_byte *contents, Elf_Internal_Rela *relocs,
9767 Elf_Internal_Sym *local_syms,
9768 asection **local_sections)
b49e97c9
TS
9769{
9770 Elf_Internal_Rela *rel;
9771 const Elf_Internal_Rela *relend;
9772 bfd_vma addend = 0;
b34976b6 9773 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 9774 const struct elf_backend_data *bed;
b49e97c9
TS
9775
9776 bed = get_elf_backend_data (output_bfd);
9777 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9778 for (rel = relocs; rel < relend; ++rel)
9779 {
9780 const char *name;
c9adbffe 9781 bfd_vma value = 0;
b49e97c9 9782 reloc_howto_type *howto;
ad3d9127 9783 bfd_boolean cross_mode_jump_p = FALSE;
b34976b6 9784 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 9785 REL relocation. */
b34976b6 9786 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 9787 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 9788 const char *msg;
ab96bf03
AM
9789 unsigned long r_symndx;
9790 asection *sec;
749b8d9d
L
9791 Elf_Internal_Shdr *symtab_hdr;
9792 struct elf_link_hash_entry *h;
d4730f92 9793 bfd_boolean rel_reloc;
b49e97c9 9794
d4730f92
BS
9795 rel_reloc = (NEWABI_P (input_bfd)
9796 && mips_elf_rel_relocation_p (input_bfd, input_section,
9797 relocs, rel));
b49e97c9 9798 /* Find the relocation howto for this relocation. */
d4730f92 9799 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
ab96bf03
AM
9800
9801 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 9802 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 9803 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
9804 {
9805 sec = local_sections[r_symndx];
9806 h = NULL;
9807 }
ab96bf03
AM
9808 else
9809 {
ab96bf03 9810 unsigned long extsymoff;
ab96bf03 9811
ab96bf03
AM
9812 extsymoff = 0;
9813 if (!elf_bad_symtab (input_bfd))
9814 extsymoff = symtab_hdr->sh_info;
9815 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9816 while (h->root.type == bfd_link_hash_indirect
9817 || h->root.type == bfd_link_hash_warning)
9818 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9819
9820 sec = NULL;
9821 if (h->root.type == bfd_link_hash_defined
9822 || h->root.type == bfd_link_hash_defweak)
9823 sec = h->root.u.def.section;
9824 }
9825
dbaa2011 9826 if (sec != NULL && discarded_section (sec))
545fd46b
MR
9827 {
9828 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9829 input_section, &rel, &relend,
9830 rel_reloc, howto, contents);
9831 continue;
9832 }
ab96bf03 9833
4a14403c 9834 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
9835 {
9836 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9837 64-bit code, but make sure all their addresses are in the
9838 lowermost or uppermost 32-bit section of the 64-bit address
9839 space. Thus, when they use an R_MIPS_64 they mean what is
9840 usually meant by R_MIPS_32, with the exception that the
9841 stored value is sign-extended to 64 bits. */
b34976b6 9842 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
9843
9844 /* On big-endian systems, we need to lie about the position
9845 of the reloc. */
9846 if (bfd_big_endian (input_bfd))
9847 rel->r_offset += 4;
9848 }
b49e97c9
TS
9849
9850 if (!use_saved_addend_p)
9851 {
b49e97c9
TS
9852 /* If these relocations were originally of the REL variety,
9853 we must pull the addend out of the field that will be
9854 relocated. Otherwise, we simply use the contents of the
c224138d
RS
9855 RELA relocation. */
9856 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9857 relocs, rel))
b49e97c9 9858 {
b34976b6 9859 rela_relocation_p = FALSE;
c224138d
RS
9860 addend = mips_elf_read_rel_addend (input_bfd, rel,
9861 howto, contents);
738e5348
RS
9862 if (hi16_reloc_p (r_type)
9863 || (got16_reloc_p (r_type)
b49e97c9 9864 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 9865 local_sections)))
b49e97c9 9866 {
c224138d
RS
9867 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9868 contents, &addend))
749b8d9d 9869 {
749b8d9d
L
9870 if (h)
9871 name = h->root.root.string;
9872 else
9873 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9874 local_syms + r_symndx,
9875 sec);
9876 (*_bfd_error_handler)
9877 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9878 input_bfd, input_section, name, howto->name,
9879 rel->r_offset);
749b8d9d 9880 }
b49e97c9 9881 }
30ac9238
RS
9882 else
9883 addend <<= howto->rightshift;
b49e97c9
TS
9884 }
9885 else
9886 addend = rel->r_addend;
81d43bff
RS
9887 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9888 local_syms, local_sections, rel);
b49e97c9
TS
9889 }
9890
1049f94e 9891 if (info->relocatable)
b49e97c9 9892 {
4a14403c 9893 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9894 && bfd_big_endian (input_bfd))
9895 rel->r_offset -= 4;
9896
81d43bff 9897 if (!rela_relocation_p && rel->r_addend)
5a659663 9898 {
81d43bff 9899 addend += rel->r_addend;
738e5348 9900 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9901 addend = mips_elf_high (addend);
9902 else if (r_type == R_MIPS_HIGHER)
9903 addend = mips_elf_higher (addend);
9904 else if (r_type == R_MIPS_HIGHEST)
9905 addend = mips_elf_highest (addend);
30ac9238
RS
9906 else
9907 addend >>= howto->rightshift;
b49e97c9 9908
30ac9238
RS
9909 /* We use the source mask, rather than the destination
9910 mask because the place to which we are writing will be
9911 source of the addend in the final link. */
b49e97c9
TS
9912 addend &= howto->src_mask;
9913
5a659663 9914 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9915 /* See the comment above about using R_MIPS_64 in the 32-bit
9916 ABI. Here, we need to update the addend. It would be
9917 possible to get away with just using the R_MIPS_32 reloc
9918 but for endianness. */
9919 {
9920 bfd_vma sign_bits;
9921 bfd_vma low_bits;
9922 bfd_vma high_bits;
9923
9924 if (addend & ((bfd_vma) 1 << 31))
9925#ifdef BFD64
9926 sign_bits = ((bfd_vma) 1 << 32) - 1;
9927#else
9928 sign_bits = -1;
9929#endif
9930 else
9931 sign_bits = 0;
9932
9933 /* If we don't know that we have a 64-bit type,
9934 do two separate stores. */
9935 if (bfd_big_endian (input_bfd))
9936 {
9937 /* Store the sign-bits (which are most significant)
9938 first. */
9939 low_bits = sign_bits;
9940 high_bits = addend;
9941 }
9942 else
9943 {
9944 low_bits = addend;
9945 high_bits = sign_bits;
9946 }
9947 bfd_put_32 (input_bfd, low_bits,
9948 contents + rel->r_offset);
9949 bfd_put_32 (input_bfd, high_bits,
9950 contents + rel->r_offset + 4);
9951 continue;
9952 }
9953
9954 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9955 input_bfd, input_section,
b34976b6
AM
9956 contents, FALSE))
9957 return FALSE;
b49e97c9
TS
9958 }
9959
9960 /* Go on to the next relocation. */
9961 continue;
9962 }
9963
9964 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9965 relocations for the same offset. In that case we are
9966 supposed to treat the output of each relocation as the addend
9967 for the next. */
9968 if (rel + 1 < relend
9969 && rel->r_offset == rel[1].r_offset
9970 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9971 use_saved_addend_p = TRUE;
b49e97c9 9972 else
b34976b6 9973 use_saved_addend_p = FALSE;
b49e97c9
TS
9974
9975 /* Figure out what value we are supposed to relocate. */
9976 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9977 input_section, info, rel,
9978 addend, howto, local_syms,
9979 local_sections, &value,
38a7df63 9980 &name, &cross_mode_jump_p,
bce03d3d 9981 use_saved_addend_p))
b49e97c9
TS
9982 {
9983 case bfd_reloc_continue:
9984 /* There's nothing to do. */
9985 continue;
9986
9987 case bfd_reloc_undefined:
9988 /* mips_elf_calculate_relocation already called the
9989 undefined_symbol callback. There's no real point in
9990 trying to perform the relocation at this point, so we
9991 just skip ahead to the next relocation. */
9992 continue;
9993
9994 case bfd_reloc_notsupported:
9995 msg = _("internal error: unsupported relocation error");
9996 info->callbacks->warning
9997 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9998 return FALSE;
b49e97c9
TS
9999
10000 case bfd_reloc_overflow:
10001 if (use_saved_addend_p)
10002 /* Ignore overflow until we reach the last relocation for
10003 a given location. */
10004 ;
10005 else
10006 {
0e53d9da
AN
10007 struct mips_elf_link_hash_table *htab;
10008
10009 htab = mips_elf_hash_table (info);
4dfe6ac6 10010 BFD_ASSERT (htab != NULL);
b49e97c9 10011 BFD_ASSERT (name != NULL);
0e53d9da 10012 if (!htab->small_data_overflow_reported
9684f078 10013 && (gprel16_reloc_p (howto->type)
df58fc94 10014 || literal_reloc_p (howto->type)))
0e53d9da 10015 {
91d6fa6a
NC
10016 msg = _("small-data section exceeds 64KB;"
10017 " lower small-data size limit (see option -G)");
0e53d9da
AN
10018
10019 htab->small_data_overflow_reported = TRUE;
10020 (*info->callbacks->einfo) ("%P: %s\n", msg);
10021 }
b49e97c9 10022 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 10023 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 10024 input_bfd, input_section, rel->r_offset)))
b34976b6 10025 return FALSE;
b49e97c9
TS
10026 }
10027 break;
10028
10029 case bfd_reloc_ok:
10030 break;
10031
df58fc94
RS
10032 case bfd_reloc_outofrange:
10033 if (jal_reloc_p (howto->type))
10034 {
10035 msg = _("JALX to a non-word-aligned address");
10036 info->callbacks->warning
10037 (info, msg, name, input_bfd, input_section, rel->r_offset);
10038 return FALSE;
10039 }
10040 /* Fall through. */
10041
b49e97c9
TS
10042 default:
10043 abort ();
10044 break;
10045 }
10046
10047 /* If we've got another relocation for the address, keep going
10048 until we reach the last one. */
10049 if (use_saved_addend_p)
10050 {
10051 addend = value;
10052 continue;
10053 }
10054
4a14403c 10055 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10056 /* See the comment above about using R_MIPS_64 in the 32-bit
10057 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10058 that calculated the right value. Now, however, we
10059 sign-extend the 32-bit result to 64-bits, and store it as a
10060 64-bit value. We are especially generous here in that we
10061 go to extreme lengths to support this usage on systems with
10062 only a 32-bit VMA. */
10063 {
10064 bfd_vma sign_bits;
10065 bfd_vma low_bits;
10066 bfd_vma high_bits;
10067
10068 if (value & ((bfd_vma) 1 << 31))
10069#ifdef BFD64
10070 sign_bits = ((bfd_vma) 1 << 32) - 1;
10071#else
10072 sign_bits = -1;
10073#endif
10074 else
10075 sign_bits = 0;
10076
10077 /* If we don't know that we have a 64-bit type,
10078 do two separate stores. */
10079 if (bfd_big_endian (input_bfd))
10080 {
10081 /* Undo what we did above. */
10082 rel->r_offset -= 4;
10083 /* Store the sign-bits (which are most significant)
10084 first. */
10085 low_bits = sign_bits;
10086 high_bits = value;
10087 }
10088 else
10089 {
10090 low_bits = value;
10091 high_bits = sign_bits;
10092 }
10093 bfd_put_32 (input_bfd, low_bits,
10094 contents + rel->r_offset);
10095 bfd_put_32 (input_bfd, high_bits,
10096 contents + rel->r_offset + 4);
10097 continue;
10098 }
10099
10100 /* Actually perform the relocation. */
10101 if (! mips_elf_perform_relocation (info, howto, rel, value,
10102 input_bfd, input_section,
38a7df63 10103 contents, cross_mode_jump_p))
b34976b6 10104 return FALSE;
b49e97c9
TS
10105 }
10106
b34976b6 10107 return TRUE;
b49e97c9
TS
10108}
10109\f
861fb55a
DJ
10110/* A function that iterates over each entry in la25_stubs and fills
10111 in the code for each one. DATA points to a mips_htab_traverse_info. */
10112
10113static int
10114mips_elf_create_la25_stub (void **slot, void *data)
10115{
10116 struct mips_htab_traverse_info *hti;
10117 struct mips_elf_link_hash_table *htab;
10118 struct mips_elf_la25_stub *stub;
10119 asection *s;
10120 bfd_byte *loc;
10121 bfd_vma offset, target, target_high, target_low;
10122
10123 stub = (struct mips_elf_la25_stub *) *slot;
10124 hti = (struct mips_htab_traverse_info *) data;
10125 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 10126 BFD_ASSERT (htab != NULL);
861fb55a
DJ
10127
10128 /* Create the section contents, if we haven't already. */
10129 s = stub->stub_section;
10130 loc = s->contents;
10131 if (loc == NULL)
10132 {
10133 loc = bfd_malloc (s->size);
10134 if (loc == NULL)
10135 {
10136 hti->error = TRUE;
10137 return FALSE;
10138 }
10139 s->contents = loc;
10140 }
10141
10142 /* Work out where in the section this stub should go. */
10143 offset = stub->offset;
10144
10145 /* Work out the target address. */
8f0c309a
CLT
10146 target = mips_elf_get_la25_target (stub, &s);
10147 target += s->output_section->vma + s->output_offset;
10148
861fb55a
DJ
10149 target_high = ((target + 0x8000) >> 16) & 0xffff;
10150 target_low = (target & 0xffff);
10151
10152 if (stub->stub_section != htab->strampoline)
10153 {
df58fc94 10154 /* This is a simple LUI/ADDIU stub. Zero out the beginning
861fb55a
DJ
10155 of the section and write the two instructions at the end. */
10156 memset (loc, 0, offset);
10157 loc += offset;
df58fc94
RS
10158 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10159 {
d21911ea
MR
10160 bfd_put_micromips_32 (hti->output_bfd,
10161 LA25_LUI_MICROMIPS (target_high),
10162 loc);
10163 bfd_put_micromips_32 (hti->output_bfd,
10164 LA25_ADDIU_MICROMIPS (target_low),
10165 loc + 4);
df58fc94
RS
10166 }
10167 else
10168 {
10169 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10170 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10171 }
861fb55a
DJ
10172 }
10173 else
10174 {
10175 /* This is trampoline. */
10176 loc += offset;
df58fc94
RS
10177 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10178 {
d21911ea
MR
10179 bfd_put_micromips_32 (hti->output_bfd,
10180 LA25_LUI_MICROMIPS (target_high), loc);
10181 bfd_put_micromips_32 (hti->output_bfd,
10182 LA25_J_MICROMIPS (target), loc + 4);
10183 bfd_put_micromips_32 (hti->output_bfd,
10184 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
df58fc94
RS
10185 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10186 }
10187 else
10188 {
10189 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10190 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10191 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10192 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10193 }
861fb55a
DJ
10194 }
10195 return TRUE;
10196}
10197
b49e97c9
TS
10198/* If NAME is one of the special IRIX6 symbols defined by the linker,
10199 adjust it appropriately now. */
10200
10201static void
9719ad41
RS
10202mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10203 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
10204{
10205 /* The linker script takes care of providing names and values for
10206 these, but we must place them into the right sections. */
10207 static const char* const text_section_symbols[] = {
10208 "_ftext",
10209 "_etext",
10210 "__dso_displacement",
10211 "__elf_header",
10212 "__program_header_table",
10213 NULL
10214 };
10215
10216 static const char* const data_section_symbols[] = {
10217 "_fdata",
10218 "_edata",
10219 "_end",
10220 "_fbss",
10221 NULL
10222 };
10223
10224 const char* const *p;
10225 int i;
10226
10227 for (i = 0; i < 2; ++i)
10228 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10229 *p;
10230 ++p)
10231 if (strcmp (*p, name) == 0)
10232 {
10233 /* All of these symbols are given type STT_SECTION by the
10234 IRIX6 linker. */
10235 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 10236 sym->st_other = STO_PROTECTED;
b49e97c9
TS
10237
10238 /* The IRIX linker puts these symbols in special sections. */
10239 if (i == 0)
10240 sym->st_shndx = SHN_MIPS_TEXT;
10241 else
10242 sym->st_shndx = SHN_MIPS_DATA;
10243
10244 break;
10245 }
10246}
10247
10248/* Finish up dynamic symbol handling. We set the contents of various
10249 dynamic sections here. */
10250
b34976b6 10251bfd_boolean
9719ad41
RS
10252_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10253 struct bfd_link_info *info,
10254 struct elf_link_hash_entry *h,
10255 Elf_Internal_Sym *sym)
b49e97c9
TS
10256{
10257 bfd *dynobj;
b49e97c9 10258 asection *sgot;
f4416af6 10259 struct mips_got_info *g, *gg;
b49e97c9 10260 const char *name;
3d6746ca 10261 int idx;
5108fc1b 10262 struct mips_elf_link_hash_table *htab;
738e5348 10263 struct mips_elf_link_hash_entry *hmips;
b49e97c9 10264
5108fc1b 10265 htab = mips_elf_hash_table (info);
4dfe6ac6 10266 BFD_ASSERT (htab != NULL);
b49e97c9 10267 dynobj = elf_hash_table (info)->dynobj;
738e5348 10268 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 10269
861fb55a
DJ
10270 BFD_ASSERT (!htab->is_vxworks);
10271
1bbce132
MR
10272 if (h->plt.plist != NULL
10273 && (h->plt.plist->mips_offset != MINUS_ONE
10274 || h->plt.plist->comp_offset != MINUS_ONE))
861fb55a
DJ
10275 {
10276 /* We've decided to create a PLT entry for this symbol. */
10277 bfd_byte *loc;
1bbce132 10278 bfd_vma header_address, got_address;
861fb55a 10279 bfd_vma got_address_high, got_address_low, load;
1bbce132
MR
10280 bfd_vma got_index;
10281 bfd_vma isa_bit;
10282
10283 got_index = h->plt.plist->gotplt_index;
861fb55a
DJ
10284
10285 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10286 BFD_ASSERT (h->dynindx != -1);
10287 BFD_ASSERT (htab->splt != NULL);
1bbce132 10288 BFD_ASSERT (got_index != MINUS_ONE);
861fb55a
DJ
10289 BFD_ASSERT (!h->def_regular);
10290
10291 /* Calculate the address of the PLT header. */
1bbce132 10292 isa_bit = htab->plt_header_is_comp;
861fb55a 10293 header_address = (htab->splt->output_section->vma
1bbce132 10294 + htab->splt->output_offset + isa_bit);
861fb55a
DJ
10295
10296 /* Calculate the address of the .got.plt entry. */
10297 got_address = (htab->sgotplt->output_section->vma
10298 + htab->sgotplt->output_offset
1bbce132
MR
10299 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10300
861fb55a
DJ
10301 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10302 got_address_low = got_address & 0xffff;
10303
10304 /* Initially point the .got.plt entry at the PLT header. */
1bbce132 10305 loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
861fb55a
DJ
10306 if (ABI_64_P (output_bfd))
10307 bfd_put_64 (output_bfd, header_address, loc);
10308 else
10309 bfd_put_32 (output_bfd, header_address, loc);
10310
1bbce132
MR
10311 /* Now handle the PLT itself. First the standard entry (the order
10312 does not matter, we just have to pick one). */
10313 if (h->plt.plist->mips_offset != MINUS_ONE)
10314 {
10315 const bfd_vma *plt_entry;
10316 bfd_vma plt_offset;
861fb55a 10317
1bbce132 10318 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
861fb55a 10319
1bbce132 10320 BFD_ASSERT (plt_offset <= htab->splt->size);
6d30f5b2 10321
1bbce132
MR
10322 /* Find out where the .plt entry should go. */
10323 loc = htab->splt->contents + plt_offset;
10324
10325 /* Pick the load opcode. */
10326 load = MIPS_ELF_LOAD_WORD (output_bfd);
10327
10328 /* Fill in the PLT entry itself. */
10329 plt_entry = mips_exec_plt_entry;
10330 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10331 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10332 loc + 4);
10333
10334 if (! LOAD_INTERLOCKS_P (output_bfd))
10335 {
10336 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10337 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10338 }
10339 else
10340 {
10341 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10342 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10343 loc + 12);
10344 }
6d30f5b2 10345 }
1bbce132
MR
10346
10347 /* Now the compressed entry. They come after any standard ones. */
10348 if (h->plt.plist->comp_offset != MINUS_ONE)
6d30f5b2 10349 {
1bbce132
MR
10350 bfd_vma plt_offset;
10351
10352 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10353 + h->plt.plist->comp_offset);
10354
10355 BFD_ASSERT (plt_offset <= htab->splt->size);
10356
10357 /* Find out where the .plt entry should go. */
10358 loc = htab->splt->contents + plt_offset;
10359
10360 /* Fill in the PLT entry itself. */
833794fc
MR
10361 if (!MICROMIPS_P (output_bfd))
10362 {
10363 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10364
10365 bfd_put_16 (output_bfd, plt_entry[0], loc);
10366 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10367 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10368 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10369 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10370 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10371 bfd_put_32 (output_bfd, got_address, loc + 12);
10372 }
10373 else if (htab->insn32)
10374 {
10375 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10376
10377 bfd_put_16 (output_bfd, plt_entry[0], loc);
10378 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10379 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10380 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10381 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10382 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10383 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10384 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10385 }
10386 else
1bbce132
MR
10387 {
10388 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10389 bfd_signed_vma gotpc_offset;
10390 bfd_vma loc_address;
10391
10392 BFD_ASSERT (got_address % 4 == 0);
10393
10394 loc_address = (htab->splt->output_section->vma
10395 + htab->splt->output_offset + plt_offset);
10396 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10397
10398 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10399 if (gotpc_offset + 0x1000000 >= 0x2000000)
10400 {
10401 (*_bfd_error_handler)
10402 (_("%B: `%A' offset of %ld from `%A' "
10403 "beyond the range of ADDIUPC"),
10404 output_bfd,
10405 htab->sgotplt->output_section,
10406 htab->splt->output_section,
10407 (long) gotpc_offset);
10408 bfd_set_error (bfd_error_no_error);
10409 return FALSE;
10410 }
10411 bfd_put_16 (output_bfd,
10412 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10413 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10414 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10415 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10416 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10417 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10418 }
6d30f5b2 10419 }
861fb55a
DJ
10420
10421 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10422 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
1bbce132 10423 got_index - 2, h->dynindx,
861fb55a
DJ
10424 R_MIPS_JUMP_SLOT, got_address);
10425
10426 /* We distinguish between PLT entries and lazy-binding stubs by
10427 giving the former an st_other value of STO_MIPS_PLT. Set the
10428 flag and leave the value if there are any relocations in the
10429 binary where pointer equality matters. */
10430 sym->st_shndx = SHN_UNDEF;
10431 if (h->pointer_equality_needed)
1bbce132 10432 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
861fb55a 10433 else
1bbce132
MR
10434 {
10435 sym->st_value = 0;
10436 sym->st_other = 0;
10437 }
861fb55a 10438 }
1bbce132
MR
10439
10440 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
b49e97c9 10441 {
861fb55a 10442 /* We've decided to create a lazy-binding stub. */
1bbce132
MR
10443 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10444 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10445 bfd_vma stub_size = htab->function_stub_size;
5108fc1b 10446 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
1bbce132
MR
10447 bfd_vma isa_bit = micromips_p;
10448 bfd_vma stub_big_size;
10449
833794fc 10450 if (!micromips_p)
1bbce132 10451 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
833794fc
MR
10452 else if (htab->insn32)
10453 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10454 else
10455 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
b49e97c9
TS
10456
10457 /* This symbol has a stub. Set it up. */
10458
10459 BFD_ASSERT (h->dynindx != -1);
10460
1bbce132 10461 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
3d6746ca
DD
10462
10463 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
10464 sign extension at runtime in the stub, resulting in a negative
10465 index value. */
10466 if (h->dynindx & ~0x7fffffff)
b34976b6 10467 return FALSE;
b49e97c9
TS
10468
10469 /* Fill the stub. */
1bbce132
MR
10470 if (micromips_p)
10471 {
10472 idx = 0;
10473 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10474 stub + idx);
10475 idx += 4;
833794fc
MR
10476 if (htab->insn32)
10477 {
10478 bfd_put_micromips_32 (output_bfd,
10479 STUB_MOVE32_MICROMIPS (output_bfd),
10480 stub + idx);
10481 idx += 4;
10482 }
10483 else
10484 {
10485 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10486 idx += 2;
10487 }
1bbce132
MR
10488 if (stub_size == stub_big_size)
10489 {
10490 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10491
10492 bfd_put_micromips_32 (output_bfd,
10493 STUB_LUI_MICROMIPS (dynindx_hi),
10494 stub + idx);
10495 idx += 4;
10496 }
833794fc
MR
10497 if (htab->insn32)
10498 {
10499 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
10500 stub + idx);
10501 idx += 4;
10502 }
10503 else
10504 {
10505 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
10506 idx += 2;
10507 }
1bbce132
MR
10508
10509 /* If a large stub is not required and sign extension is not a
10510 problem, then use legacy code in the stub. */
10511 if (stub_size == stub_big_size)
10512 bfd_put_micromips_32 (output_bfd,
10513 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
10514 stub + idx);
10515 else if (h->dynindx & ~0x7fff)
10516 bfd_put_micromips_32 (output_bfd,
10517 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
10518 stub + idx);
10519 else
10520 bfd_put_micromips_32 (output_bfd,
10521 STUB_LI16S_MICROMIPS (output_bfd,
10522 h->dynindx),
10523 stub + idx);
10524 }
3d6746ca 10525 else
1bbce132
MR
10526 {
10527 idx = 0;
10528 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
10529 idx += 4;
10530 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
10531 idx += 4;
10532 if (stub_size == stub_big_size)
10533 {
10534 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
10535 stub + idx);
10536 idx += 4;
10537 }
10538 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
10539 idx += 4;
10540
10541 /* If a large stub is not required and sign extension is not a
10542 problem, then use legacy code in the stub. */
10543 if (stub_size == stub_big_size)
10544 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
10545 stub + idx);
10546 else if (h->dynindx & ~0x7fff)
10547 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
10548 stub + idx);
10549 else
10550 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
10551 stub + idx);
10552 }
5108fc1b 10553
1bbce132
MR
10554 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
10555 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
10556 stub, stub_size);
b49e97c9 10557
1bbce132 10558 /* Mark the symbol as undefined. stub_offset != -1 occurs
b49e97c9
TS
10559 only for the referenced symbol. */
10560 sym->st_shndx = SHN_UNDEF;
10561
10562 /* The run-time linker uses the st_value field of the symbol
10563 to reset the global offset table entry for this external
10564 to its stub address when unlinking a shared object. */
4e41d0d7
RS
10565 sym->st_value = (htab->sstubs->output_section->vma
10566 + htab->sstubs->output_offset
1bbce132
MR
10567 + h->plt.plist->stub_offset
10568 + isa_bit);
10569 sym->st_other = other;
b49e97c9
TS
10570 }
10571
738e5348
RS
10572 /* If we have a MIPS16 function with a stub, the dynamic symbol must
10573 refer to the stub, since only the stub uses the standard calling
10574 conventions. */
10575 if (h->dynindx != -1 && hmips->fn_stub != NULL)
10576 {
10577 BFD_ASSERT (hmips->need_fn_stub);
10578 sym->st_value = (hmips->fn_stub->output_section->vma
10579 + hmips->fn_stub->output_offset);
10580 sym->st_size = hmips->fn_stub->size;
10581 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10582 }
10583
b49e97c9 10584 BFD_ASSERT (h->dynindx != -1
f5385ebf 10585 || h->forced_local);
b49e97c9 10586
23cc69b6 10587 sgot = htab->sgot;
a8028dd0 10588 g = htab->got_info;
b49e97c9
TS
10589 BFD_ASSERT (g != NULL);
10590
10591 /* Run through the global symbol table, creating GOT entries for all
10592 the symbols that need them. */
020d7251 10593 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
10594 {
10595 bfd_vma offset;
10596 bfd_vma value;
10597
6eaa6adc 10598 value = sym->st_value;
13fbec83 10599 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
b49e97c9
TS
10600 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10601 }
10602
e641e783 10603 if (hmips->global_got_area != GGA_NONE && g->next)
f4416af6
AO
10604 {
10605 struct mips_got_entry e, *p;
0626d451 10606 bfd_vma entry;
f4416af6 10607 bfd_vma offset;
f4416af6
AO
10608
10609 gg = g;
10610
10611 e.abfd = output_bfd;
10612 e.symndx = -1;
738e5348 10613 e.d.h = hmips;
9ab066b4 10614 e.tls_type = GOT_TLS_NONE;
143d77c5 10615
f4416af6
AO
10616 for (g = g->next; g->next != gg; g = g->next)
10617 {
10618 if (g->got_entries
10619 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10620 &e)))
10621 {
10622 offset = p->gotidx;
6c42ddb9 10623 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
0626d451
RS
10624 if (info->shared
10625 || (elf_hash_table (info)->dynamic_sections_created
10626 && p->d.h != NULL
f5385ebf
AM
10627 && p->d.h->root.def_dynamic
10628 && !p->d.h->root.def_regular))
0626d451
RS
10629 {
10630 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10631 the various compatibility problems, it's easier to mock
10632 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10633 mips_elf_create_dynamic_relocation to calculate the
10634 appropriate addend. */
10635 Elf_Internal_Rela rel[3];
10636
10637 memset (rel, 0, sizeof (rel));
10638 if (ABI_64_P (output_bfd))
10639 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10640 else
10641 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10642 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10643
10644 entry = 0;
10645 if (! (mips_elf_create_dynamic_relocation
10646 (output_bfd, info, rel,
10647 e.d.h, NULL, sym->st_value, &entry, sgot)))
10648 return FALSE;
10649 }
10650 else
10651 entry = sym->st_value;
10652 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
10653 }
10654 }
10655 }
10656
b49e97c9
TS
10657 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10658 name = h->root.root.string;
9637f6ef 10659 if (h == elf_hash_table (info)->hdynamic
22edb2f1 10660 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
10661 sym->st_shndx = SHN_ABS;
10662 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10663 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10664 {
10665 sym->st_shndx = SHN_ABS;
10666 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10667 sym->st_value = 1;
10668 }
4a14403c 10669 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10670 {
10671 sym->st_shndx = SHN_ABS;
10672 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10673 sym->st_value = elf_gp (output_bfd);
10674 }
10675 else if (SGI_COMPAT (output_bfd))
10676 {
10677 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10678 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10679 {
10680 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10681 sym->st_other = STO_PROTECTED;
10682 sym->st_value = 0;
10683 sym->st_shndx = SHN_MIPS_DATA;
10684 }
10685 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10686 {
10687 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10688 sym->st_other = STO_PROTECTED;
10689 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10690 sym->st_shndx = SHN_ABS;
10691 }
10692 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10693 {
10694 if (h->type == STT_FUNC)
10695 sym->st_shndx = SHN_MIPS_TEXT;
10696 else if (h->type == STT_OBJECT)
10697 sym->st_shndx = SHN_MIPS_DATA;
10698 }
10699 }
10700
861fb55a
DJ
10701 /* Emit a copy reloc, if needed. */
10702 if (h->needs_copy)
10703 {
10704 asection *s;
10705 bfd_vma symval;
10706
10707 BFD_ASSERT (h->dynindx != -1);
10708 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10709
10710 s = mips_elf_rel_dyn_section (info, FALSE);
10711 symval = (h->root.u.def.section->output_section->vma
10712 + h->root.u.def.section->output_offset
10713 + h->root.u.def.value);
10714 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10715 h->dynindx, R_MIPS_COPY, symval);
10716 }
10717
b49e97c9
TS
10718 /* Handle the IRIX6-specific symbols. */
10719 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10720 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10721
cbf8d970
MR
10722 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
10723 to treat compressed symbols like any other. */
30c09090 10724 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
10725 {
10726 BFD_ASSERT (sym->st_value & 1);
10727 sym->st_other -= STO_MIPS16;
10728 }
cbf8d970
MR
10729 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
10730 {
10731 BFD_ASSERT (sym->st_value & 1);
10732 sym->st_other -= STO_MICROMIPS;
10733 }
b49e97c9 10734
b34976b6 10735 return TRUE;
b49e97c9
TS
10736}
10737
0a44bf69
RS
10738/* Likewise, for VxWorks. */
10739
10740bfd_boolean
10741_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10742 struct bfd_link_info *info,
10743 struct elf_link_hash_entry *h,
10744 Elf_Internal_Sym *sym)
10745{
10746 bfd *dynobj;
10747 asection *sgot;
10748 struct mips_got_info *g;
10749 struct mips_elf_link_hash_table *htab;
020d7251 10750 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
10751
10752 htab = mips_elf_hash_table (info);
4dfe6ac6 10753 BFD_ASSERT (htab != NULL);
0a44bf69 10754 dynobj = elf_hash_table (info)->dynobj;
020d7251 10755 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69 10756
1bbce132 10757 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
0a44bf69 10758 {
6d79d2ed 10759 bfd_byte *loc;
1bbce132 10760 bfd_vma plt_address, got_address, got_offset, branch_offset;
0a44bf69
RS
10761 Elf_Internal_Rela rel;
10762 static const bfd_vma *plt_entry;
1bbce132
MR
10763 bfd_vma gotplt_index;
10764 bfd_vma plt_offset;
10765
10766 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10767 gotplt_index = h->plt.plist->gotplt_index;
0a44bf69
RS
10768
10769 BFD_ASSERT (h->dynindx != -1);
10770 BFD_ASSERT (htab->splt != NULL);
1bbce132
MR
10771 BFD_ASSERT (gotplt_index != MINUS_ONE);
10772 BFD_ASSERT (plt_offset <= htab->splt->size);
0a44bf69
RS
10773
10774 /* Calculate the address of the .plt entry. */
10775 plt_address = (htab->splt->output_section->vma
10776 + htab->splt->output_offset
1bbce132 10777 + plt_offset);
0a44bf69
RS
10778
10779 /* Calculate the address of the .got.plt entry. */
10780 got_address = (htab->sgotplt->output_section->vma
10781 + htab->sgotplt->output_offset
1bbce132 10782 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
0a44bf69
RS
10783
10784 /* Calculate the offset of the .got.plt entry from
10785 _GLOBAL_OFFSET_TABLE_. */
10786 got_offset = mips_elf_gotplt_index (info, h);
10787
10788 /* Calculate the offset for the branch at the start of the PLT
10789 entry. The branch jumps to the beginning of .plt. */
1bbce132 10790 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
0a44bf69
RS
10791
10792 /* Fill in the initial value of the .got.plt entry. */
10793 bfd_put_32 (output_bfd, plt_address,
1bbce132
MR
10794 (htab->sgotplt->contents
10795 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
0a44bf69
RS
10796
10797 /* Find out where the .plt entry should go. */
1bbce132 10798 loc = htab->splt->contents + plt_offset;
0a44bf69
RS
10799
10800 if (info->shared)
10801 {
10802 plt_entry = mips_vxworks_shared_plt_entry;
10803 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 10804 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
10805 }
10806 else
10807 {
10808 bfd_vma got_address_high, got_address_low;
10809
10810 plt_entry = mips_vxworks_exec_plt_entry;
10811 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10812 got_address_low = got_address & 0xffff;
10813
10814 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 10815 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
10816 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10817 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10818 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10819 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10820 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10821 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10822
10823 loc = (htab->srelplt2->contents
1bbce132 10824 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
0a44bf69
RS
10825
10826 /* Emit a relocation for the .got.plt entry. */
10827 rel.r_offset = got_address;
10828 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
1bbce132 10829 rel.r_addend = plt_offset;
0a44bf69
RS
10830 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10831
10832 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10833 loc += sizeof (Elf32_External_Rela);
10834 rel.r_offset = plt_address + 8;
10835 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10836 rel.r_addend = got_offset;
10837 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10838
10839 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10840 loc += sizeof (Elf32_External_Rela);
10841 rel.r_offset += 4;
10842 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10843 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10844 }
10845
10846 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
1bbce132
MR
10847 loc = (htab->srelplt->contents
10848 + gotplt_index * sizeof (Elf32_External_Rela));
0a44bf69
RS
10849 rel.r_offset = got_address;
10850 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10851 rel.r_addend = 0;
10852 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10853
10854 if (!h->def_regular)
10855 sym->st_shndx = SHN_UNDEF;
10856 }
10857
10858 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10859
23cc69b6 10860 sgot = htab->sgot;
a8028dd0 10861 g = htab->got_info;
0a44bf69
RS
10862 BFD_ASSERT (g != NULL);
10863
10864 /* See if this symbol has an entry in the GOT. */
020d7251 10865 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
10866 {
10867 bfd_vma offset;
10868 Elf_Internal_Rela outrel;
10869 bfd_byte *loc;
10870 asection *s;
10871
10872 /* Install the symbol value in the GOT. */
13fbec83 10873 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
0a44bf69
RS
10874 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10875
10876 /* Add a dynamic relocation for it. */
10877 s = mips_elf_rel_dyn_section (info, FALSE);
10878 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10879 outrel.r_offset = (sgot->output_section->vma
10880 + sgot->output_offset
10881 + offset);
10882 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10883 outrel.r_addend = 0;
10884 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10885 }
10886
10887 /* Emit a copy reloc, if needed. */
10888 if (h->needs_copy)
10889 {
10890 Elf_Internal_Rela rel;
10891
10892 BFD_ASSERT (h->dynindx != -1);
10893
10894 rel.r_offset = (h->root.u.def.section->output_section->vma
10895 + h->root.u.def.section->output_offset
10896 + h->root.u.def.value);
10897 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10898 rel.r_addend = 0;
10899 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10900 htab->srelbss->contents
10901 + (htab->srelbss->reloc_count
10902 * sizeof (Elf32_External_Rela)));
10903 ++htab->srelbss->reloc_count;
10904 }
10905
df58fc94
RS
10906 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10907 if (ELF_ST_IS_COMPRESSED (sym->st_other))
0a44bf69
RS
10908 sym->st_value &= ~1;
10909
10910 return TRUE;
10911}
10912
861fb55a
DJ
10913/* Write out a plt0 entry to the beginning of .plt. */
10914
1bbce132 10915static bfd_boolean
861fb55a
DJ
10916mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10917{
10918 bfd_byte *loc;
10919 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10920 static const bfd_vma *plt_entry;
10921 struct mips_elf_link_hash_table *htab;
10922
10923 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10924 BFD_ASSERT (htab != NULL);
10925
861fb55a
DJ
10926 if (ABI_64_P (output_bfd))
10927 plt_entry = mips_n64_exec_plt0_entry;
10928 else if (ABI_N32_P (output_bfd))
10929 plt_entry = mips_n32_exec_plt0_entry;
833794fc 10930 else if (!htab->plt_header_is_comp)
861fb55a 10931 plt_entry = mips_o32_exec_plt0_entry;
833794fc
MR
10932 else if (htab->insn32)
10933 plt_entry = micromips_insn32_o32_exec_plt0_entry;
10934 else
10935 plt_entry = micromips_o32_exec_plt0_entry;
861fb55a
DJ
10936
10937 /* Calculate the value of .got.plt. */
10938 gotplt_value = (htab->sgotplt->output_section->vma
10939 + htab->sgotplt->output_offset);
10940 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10941 gotplt_value_low = gotplt_value & 0xffff;
10942
10943 /* The PLT sequence is not safe for N64 if .got.plt's address can
10944 not be loaded in two instructions. */
10945 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10946 || ~(gotplt_value | 0x7fffffff) == 0);
10947
10948 /* Install the PLT header. */
10949 loc = htab->splt->contents;
1bbce132
MR
10950 if (plt_entry == micromips_o32_exec_plt0_entry)
10951 {
10952 bfd_vma gotpc_offset;
10953 bfd_vma loc_address;
10954 size_t i;
10955
10956 BFD_ASSERT (gotplt_value % 4 == 0);
10957
10958 loc_address = (htab->splt->output_section->vma
10959 + htab->splt->output_offset);
10960 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
10961
10962 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10963 if (gotpc_offset + 0x1000000 >= 0x2000000)
10964 {
10965 (*_bfd_error_handler)
10966 (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
10967 output_bfd,
10968 htab->sgotplt->output_section,
10969 htab->splt->output_section,
10970 (long) gotpc_offset);
10971 bfd_set_error (bfd_error_no_error);
10972 return FALSE;
10973 }
10974 bfd_put_16 (output_bfd,
10975 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10976 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10977 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
10978 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10979 }
833794fc
MR
10980 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
10981 {
10982 size_t i;
10983
10984 bfd_put_16 (output_bfd, plt_entry[0], loc);
10985 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
10986 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10987 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
10988 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10989 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
10990 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
10991 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10992 }
1bbce132
MR
10993 else
10994 {
10995 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
10996 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
10997 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
10998 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10999 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11000 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11001 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11002 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11003 }
11004
11005 return TRUE;
861fb55a
DJ
11006}
11007
0a44bf69
RS
11008/* Install the PLT header for a VxWorks executable and finalize the
11009 contents of .rela.plt.unloaded. */
11010
11011static void
11012mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11013{
11014 Elf_Internal_Rela rela;
11015 bfd_byte *loc;
11016 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11017 static const bfd_vma *plt_entry;
11018 struct mips_elf_link_hash_table *htab;
11019
11020 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11021 BFD_ASSERT (htab != NULL);
11022
0a44bf69
RS
11023 plt_entry = mips_vxworks_exec_plt0_entry;
11024
11025 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11026 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11027 + htab->root.hgot->root.u.def.section->output_offset
11028 + htab->root.hgot->root.u.def.value);
11029
11030 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11031 got_value_low = got_value & 0xffff;
11032
11033 /* Calculate the address of the PLT header. */
11034 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
11035
11036 /* Install the PLT header. */
11037 loc = htab->splt->contents;
11038 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11039 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11040 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11041 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11042 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11043 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11044
11045 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11046 loc = htab->srelplt2->contents;
11047 rela.r_offset = plt_address;
11048 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11049 rela.r_addend = 0;
11050 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11051 loc += sizeof (Elf32_External_Rela);
11052
11053 /* Output the relocation for the following addiu of
11054 %lo(_GLOBAL_OFFSET_TABLE_). */
11055 rela.r_offset += 4;
11056 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11057 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11058 loc += sizeof (Elf32_External_Rela);
11059
11060 /* Fix up the remaining relocations. They may have the wrong
11061 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11062 in which symbols were output. */
11063 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11064 {
11065 Elf_Internal_Rela rel;
11066
11067 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11068 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11069 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11070 loc += sizeof (Elf32_External_Rela);
11071
11072 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11073 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11074 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11075 loc += sizeof (Elf32_External_Rela);
11076
11077 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11078 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11079 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11080 loc += sizeof (Elf32_External_Rela);
11081 }
11082}
11083
11084/* Install the PLT header for a VxWorks shared library. */
11085
11086static void
11087mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11088{
11089 unsigned int i;
11090 struct mips_elf_link_hash_table *htab;
11091
11092 htab = mips_elf_hash_table (info);
4dfe6ac6 11093 BFD_ASSERT (htab != NULL);
0a44bf69
RS
11094
11095 /* We just need to copy the entry byte-by-byte. */
11096 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11097 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11098 htab->splt->contents + i * 4);
11099}
11100
b49e97c9
TS
11101/* Finish up the dynamic sections. */
11102
b34976b6 11103bfd_boolean
9719ad41
RS
11104_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11105 struct bfd_link_info *info)
b49e97c9
TS
11106{
11107 bfd *dynobj;
11108 asection *sdyn;
11109 asection *sgot;
f4416af6 11110 struct mips_got_info *gg, *g;
0a44bf69 11111 struct mips_elf_link_hash_table *htab;
b49e97c9 11112
0a44bf69 11113 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11114 BFD_ASSERT (htab != NULL);
11115
b49e97c9
TS
11116 dynobj = elf_hash_table (info)->dynobj;
11117
3d4d4302 11118 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
b49e97c9 11119
23cc69b6
RS
11120 sgot = htab->sgot;
11121 gg = htab->got_info;
b49e97c9
TS
11122
11123 if (elf_hash_table (info)->dynamic_sections_created)
11124 {
11125 bfd_byte *b;
943284cc 11126 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
11127
11128 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
11129 BFD_ASSERT (gg != NULL);
11130
d7206569 11131 g = mips_elf_bfd_got (output_bfd, FALSE);
b49e97c9
TS
11132 BFD_ASSERT (g != NULL);
11133
11134 for (b = sdyn->contents;
eea6121a 11135 b < sdyn->contents + sdyn->size;
b49e97c9
TS
11136 b += MIPS_ELF_DYN_SIZE (dynobj))
11137 {
11138 Elf_Internal_Dyn dyn;
11139 const char *name;
11140 size_t elemsize;
11141 asection *s;
b34976b6 11142 bfd_boolean swap_out_p;
b49e97c9
TS
11143
11144 /* Read in the current dynamic entry. */
11145 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11146
11147 /* Assume that we're going to modify it and write it out. */
b34976b6 11148 swap_out_p = TRUE;
b49e97c9
TS
11149
11150 switch (dyn.d_tag)
11151 {
11152 case DT_RELENT:
b49e97c9
TS
11153 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11154 break;
11155
0a44bf69
RS
11156 case DT_RELAENT:
11157 BFD_ASSERT (htab->is_vxworks);
11158 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11159 break;
11160
b49e97c9
TS
11161 case DT_STRSZ:
11162 /* Rewrite DT_STRSZ. */
11163 dyn.d_un.d_val =
11164 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11165 break;
11166
11167 case DT_PLTGOT:
861fb55a
DJ
11168 s = htab->sgot;
11169 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11170 break;
11171
11172 case DT_MIPS_PLTGOT:
11173 s = htab->sgotplt;
11174 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
11175 break;
11176
11177 case DT_MIPS_RLD_VERSION:
11178 dyn.d_un.d_val = 1; /* XXX */
11179 break;
11180
11181 case DT_MIPS_FLAGS:
11182 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11183 break;
11184
b49e97c9 11185 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
11186 {
11187 time_t t;
11188 time (&t);
11189 dyn.d_un.d_val = t;
11190 }
b49e97c9
TS
11191 break;
11192
11193 case DT_MIPS_ICHECKSUM:
11194 /* XXX FIXME: */
b34976b6 11195 swap_out_p = FALSE;
b49e97c9
TS
11196 break;
11197
11198 case DT_MIPS_IVERSION:
11199 /* XXX FIXME: */
b34976b6 11200 swap_out_p = FALSE;
b49e97c9
TS
11201 break;
11202
11203 case DT_MIPS_BASE_ADDRESS:
11204 s = output_bfd->sections;
11205 BFD_ASSERT (s != NULL);
11206 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11207 break;
11208
11209 case DT_MIPS_LOCAL_GOTNO:
11210 dyn.d_un.d_val = g->local_gotno;
11211 break;
11212
11213 case DT_MIPS_UNREFEXTNO:
11214 /* The index into the dynamic symbol table which is the
11215 entry of the first external symbol that is not
11216 referenced within the same object. */
11217 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11218 break;
11219
11220 case DT_MIPS_GOTSYM:
d222d210 11221 if (htab->global_gotsym)
b49e97c9 11222 {
d222d210 11223 dyn.d_un.d_val = htab->global_gotsym->dynindx;
b49e97c9
TS
11224 break;
11225 }
11226 /* In case if we don't have global got symbols we default
11227 to setting DT_MIPS_GOTSYM to the same value as
11228 DT_MIPS_SYMTABNO, so we just fall through. */
11229
11230 case DT_MIPS_SYMTABNO:
11231 name = ".dynsym";
11232 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11233 s = bfd_get_section_by_name (output_bfd, name);
11234 BFD_ASSERT (s != NULL);
11235
eea6121a 11236 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
11237 break;
11238
11239 case DT_MIPS_HIPAGENO:
861fb55a 11240 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
11241 break;
11242
11243 case DT_MIPS_RLD_MAP:
b4082c70
DD
11244 {
11245 struct elf_link_hash_entry *h;
11246 h = mips_elf_hash_table (info)->rld_symbol;
11247 if (!h)
11248 {
11249 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11250 swap_out_p = FALSE;
11251 break;
11252 }
11253 s = h->root.u.def.section;
11254 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11255 + h->root.u.def.value);
11256 }
b49e97c9
TS
11257 break;
11258
11259 case DT_MIPS_OPTIONS:
11260 s = (bfd_get_section_by_name
11261 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11262 dyn.d_un.d_ptr = s->vma;
11263 break;
11264
0a44bf69
RS
11265 case DT_RELASZ:
11266 BFD_ASSERT (htab->is_vxworks);
11267 /* The count does not include the JUMP_SLOT relocations. */
11268 if (htab->srelplt)
11269 dyn.d_un.d_val -= htab->srelplt->size;
11270 break;
11271
11272 case DT_PLTREL:
861fb55a
DJ
11273 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11274 if (htab->is_vxworks)
11275 dyn.d_un.d_val = DT_RELA;
11276 else
11277 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
11278 break;
11279
11280 case DT_PLTRELSZ:
861fb55a 11281 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
11282 dyn.d_un.d_val = htab->srelplt->size;
11283 break;
11284
11285 case DT_JMPREL:
861fb55a
DJ
11286 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11287 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
11288 + htab->srelplt->output_offset);
11289 break;
11290
943284cc
DJ
11291 case DT_TEXTREL:
11292 /* If we didn't need any text relocations after all, delete
11293 the dynamic tag. */
11294 if (!(info->flags & DF_TEXTREL))
11295 {
11296 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11297 swap_out_p = FALSE;
11298 }
11299 break;
11300
11301 case DT_FLAGS:
11302 /* If we didn't need any text relocations after all, clear
11303 DF_TEXTREL from DT_FLAGS. */
11304 if (!(info->flags & DF_TEXTREL))
11305 dyn.d_un.d_val &= ~DF_TEXTREL;
11306 else
11307 swap_out_p = FALSE;
11308 break;
11309
b49e97c9 11310 default:
b34976b6 11311 swap_out_p = FALSE;
7a2b07ff
NS
11312 if (htab->is_vxworks
11313 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11314 swap_out_p = TRUE;
b49e97c9
TS
11315 break;
11316 }
11317
943284cc 11318 if (swap_out_p || dyn_skipped)
b49e97c9 11319 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
11320 (dynobj, &dyn, b - dyn_skipped);
11321
11322 if (dyn_to_skip)
11323 {
11324 dyn_skipped += dyn_to_skip;
11325 dyn_to_skip = 0;
11326 }
b49e97c9 11327 }
943284cc
DJ
11328
11329 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11330 if (dyn_skipped > 0)
11331 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
11332 }
11333
b55fd4d4
DJ
11334 if (sgot != NULL && sgot->size > 0
11335 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 11336 {
0a44bf69
RS
11337 if (htab->is_vxworks)
11338 {
11339 /* The first entry of the global offset table points to the
11340 ".dynamic" section. The second is initialized by the
11341 loader and contains the shared library identifier.
11342 The third is also initialized by the loader and points
11343 to the lazy resolution stub. */
11344 MIPS_ELF_PUT_WORD (output_bfd,
11345 sdyn->output_offset + sdyn->output_section->vma,
11346 sgot->contents);
11347 MIPS_ELF_PUT_WORD (output_bfd, 0,
11348 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11349 MIPS_ELF_PUT_WORD (output_bfd, 0,
11350 sgot->contents
11351 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11352 }
11353 else
11354 {
11355 /* The first entry of the global offset table will be filled at
11356 runtime. The second entry will be used by some runtime loaders.
11357 This isn't the case of IRIX rld. */
11358 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 11359 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
11360 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11361 }
b49e97c9 11362
54938e2a
TS
11363 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11364 = MIPS_ELF_GOT_SIZE (output_bfd);
11365 }
b49e97c9 11366
f4416af6
AO
11367 /* Generate dynamic relocations for the non-primary gots. */
11368 if (gg != NULL && gg->next)
11369 {
11370 Elf_Internal_Rela rel[3];
11371 bfd_vma addend = 0;
11372
11373 memset (rel, 0, sizeof (rel));
11374 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11375
11376 for (g = gg->next; g->next != gg; g = g->next)
11377 {
91d6fa6a 11378 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 11379 + g->next->tls_gotno;
f4416af6 11380
9719ad41 11381 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 11382 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
11383 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11384 sgot->contents
91d6fa6a 11385 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
11386
11387 if (! info->shared)
11388 continue;
11389
91d6fa6a 11390 while (got_index < g->assigned_gotno)
f4416af6
AO
11391 {
11392 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
91d6fa6a 11393 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
11394 if (!(mips_elf_create_dynamic_relocation
11395 (output_bfd, info, rel, NULL,
11396 bfd_abs_section_ptr,
11397 0, &addend, sgot)))
11398 return FALSE;
11399 BFD_ASSERT (addend == 0);
11400 }
11401 }
11402 }
11403
3133ddbf
DJ
11404 /* The generation of dynamic relocations for the non-primary gots
11405 adds more dynamic relocations. We cannot count them until
11406 here. */
11407
11408 if (elf_hash_table (info)->dynamic_sections_created)
11409 {
11410 bfd_byte *b;
11411 bfd_boolean swap_out_p;
11412
11413 BFD_ASSERT (sdyn != NULL);
11414
11415 for (b = sdyn->contents;
11416 b < sdyn->contents + sdyn->size;
11417 b += MIPS_ELF_DYN_SIZE (dynobj))
11418 {
11419 Elf_Internal_Dyn dyn;
11420 asection *s;
11421
11422 /* Read in the current dynamic entry. */
11423 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11424
11425 /* Assume that we're going to modify it and write it out. */
11426 swap_out_p = TRUE;
11427
11428 switch (dyn.d_tag)
11429 {
11430 case DT_RELSZ:
11431 /* Reduce DT_RELSZ to account for any relocations we
11432 decided not to make. This is for the n64 irix rld,
11433 which doesn't seem to apply any relocations if there
11434 are trailing null entries. */
0a44bf69 11435 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
11436 dyn.d_un.d_val = (s->reloc_count
11437 * (ABI_64_P (output_bfd)
11438 ? sizeof (Elf64_Mips_External_Rel)
11439 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
11440 /* Adjust the section size too. Tools like the prelinker
11441 can reasonably expect the values to the same. */
11442 elf_section_data (s->output_section)->this_hdr.sh_size
11443 = dyn.d_un.d_val;
3133ddbf
DJ
11444 break;
11445
11446 default:
11447 swap_out_p = FALSE;
11448 break;
11449 }
11450
11451 if (swap_out_p)
11452 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11453 (dynobj, &dyn, b);
11454 }
11455 }
11456
b49e97c9 11457 {
b49e97c9
TS
11458 asection *s;
11459 Elf32_compact_rel cpt;
11460
b49e97c9
TS
11461 if (SGI_COMPAT (output_bfd))
11462 {
11463 /* Write .compact_rel section out. */
3d4d4302 11464 s = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
11465 if (s != NULL)
11466 {
11467 cpt.id1 = 1;
11468 cpt.num = s->reloc_count;
11469 cpt.id2 = 2;
11470 cpt.offset = (s->output_section->filepos
11471 + sizeof (Elf32_External_compact_rel));
11472 cpt.reserved0 = 0;
11473 cpt.reserved1 = 0;
11474 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
11475 ((Elf32_External_compact_rel *)
11476 s->contents));
11477
11478 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 11479 if (htab->sstubs != NULL)
b49e97c9
TS
11480 {
11481 file_ptr dummy_offset;
11482
4e41d0d7
RS
11483 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
11484 dummy_offset = htab->sstubs->size - htab->function_stub_size;
11485 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 11486 htab->function_stub_size);
b49e97c9
TS
11487 }
11488 }
11489 }
11490
0a44bf69
RS
11491 /* The psABI says that the dynamic relocations must be sorted in
11492 increasing order of r_symndx. The VxWorks EABI doesn't require
11493 this, and because the code below handles REL rather than RELA
11494 relocations, using it for VxWorks would be outright harmful. */
11495 if (!htab->is_vxworks)
b49e97c9 11496 {
0a44bf69
RS
11497 s = mips_elf_rel_dyn_section (info, FALSE);
11498 if (s != NULL
11499 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
11500 {
11501 reldyn_sorting_bfd = output_bfd;
b49e97c9 11502
0a44bf69
RS
11503 if (ABI_64_P (output_bfd))
11504 qsort ((Elf64_External_Rel *) s->contents + 1,
11505 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
11506 sort_dynamic_relocs_64);
11507 else
11508 qsort ((Elf32_External_Rel *) s->contents + 1,
11509 s->reloc_count - 1, sizeof (Elf32_External_Rel),
11510 sort_dynamic_relocs);
11511 }
b49e97c9 11512 }
b49e97c9
TS
11513 }
11514
861fb55a 11515 if (htab->splt && htab->splt->size > 0)
0a44bf69 11516 {
861fb55a
DJ
11517 if (htab->is_vxworks)
11518 {
11519 if (info->shared)
11520 mips_vxworks_finish_shared_plt (output_bfd, info);
11521 else
11522 mips_vxworks_finish_exec_plt (output_bfd, info);
11523 }
0a44bf69 11524 else
861fb55a
DJ
11525 {
11526 BFD_ASSERT (!info->shared);
1bbce132
MR
11527 if (!mips_finish_exec_plt (output_bfd, info))
11528 return FALSE;
861fb55a 11529 }
0a44bf69 11530 }
b34976b6 11531 return TRUE;
b49e97c9
TS
11532}
11533
b49e97c9 11534
64543e1a
RS
11535/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
11536
11537static void
9719ad41 11538mips_set_isa_flags (bfd *abfd)
b49e97c9 11539{
64543e1a 11540 flagword val;
b49e97c9
TS
11541
11542 switch (bfd_get_mach (abfd))
11543 {
11544 default:
11545 case bfd_mach_mips3000:
11546 val = E_MIPS_ARCH_1;
11547 break;
11548
11549 case bfd_mach_mips3900:
11550 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
11551 break;
11552
11553 case bfd_mach_mips6000:
11554 val = E_MIPS_ARCH_2;
11555 break;
11556
11557 case bfd_mach_mips4000:
11558 case bfd_mach_mips4300:
11559 case bfd_mach_mips4400:
11560 case bfd_mach_mips4600:
11561 val = E_MIPS_ARCH_3;
11562 break;
11563
11564 case bfd_mach_mips4010:
11565 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
11566 break;
11567
11568 case bfd_mach_mips4100:
11569 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
11570 break;
11571
11572 case bfd_mach_mips4111:
11573 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
11574 break;
11575
00707a0e
RS
11576 case bfd_mach_mips4120:
11577 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
11578 break;
11579
b49e97c9
TS
11580 case bfd_mach_mips4650:
11581 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
11582 break;
11583
00707a0e
RS
11584 case bfd_mach_mips5400:
11585 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
11586 break;
11587
11588 case bfd_mach_mips5500:
11589 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
11590 break;
11591
e407c74b
NC
11592 case bfd_mach_mips5900:
11593 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
11594 break;
11595
0d2e43ed
ILT
11596 case bfd_mach_mips9000:
11597 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
11598 break;
11599
b49e97c9 11600 case bfd_mach_mips5000:
5a7ea749 11601 case bfd_mach_mips7000:
b49e97c9
TS
11602 case bfd_mach_mips8000:
11603 case bfd_mach_mips10000:
11604 case bfd_mach_mips12000:
3aa3176b
TS
11605 case bfd_mach_mips14000:
11606 case bfd_mach_mips16000:
b49e97c9
TS
11607 val = E_MIPS_ARCH_4;
11608 break;
11609
11610 case bfd_mach_mips5:
11611 val = E_MIPS_ARCH_5;
11612 break;
11613
350cc38d
MS
11614 case bfd_mach_mips_loongson_2e:
11615 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
11616 break;
11617
11618 case bfd_mach_mips_loongson_2f:
11619 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
11620 break;
11621
b49e97c9
TS
11622 case bfd_mach_mips_sb1:
11623 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
11624 break;
11625
d051516a
NC
11626 case bfd_mach_mips_loongson_3a:
11627 val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
11628 break;
11629
6f179bd0 11630 case bfd_mach_mips_octeon:
dd6a37e7 11631 case bfd_mach_mips_octeonp:
6f179bd0
AN
11632 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
11633 break;
11634
52b6b6b9
JM
11635 case bfd_mach_mips_xlr:
11636 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
11637 break;
11638
432233b3
AP
11639 case bfd_mach_mips_octeon2:
11640 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
11641 break;
11642
b49e97c9
TS
11643 case bfd_mach_mipsisa32:
11644 val = E_MIPS_ARCH_32;
11645 break;
11646
11647 case bfd_mach_mipsisa64:
11648 val = E_MIPS_ARCH_64;
af7ee8bf
CD
11649 break;
11650
11651 case bfd_mach_mipsisa32r2:
11652 val = E_MIPS_ARCH_32R2;
11653 break;
5f74bc13
CD
11654
11655 case bfd_mach_mipsisa64r2:
11656 val = E_MIPS_ARCH_64R2;
11657 break;
b49e97c9 11658 }
b49e97c9
TS
11659 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11660 elf_elfheader (abfd)->e_flags |= val;
11661
64543e1a
RS
11662}
11663
11664
11665/* The final processing done just before writing out a MIPS ELF object
11666 file. This gets the MIPS architecture right based on the machine
11667 number. This is used by both the 32-bit and the 64-bit ABI. */
11668
11669void
9719ad41
RS
11670_bfd_mips_elf_final_write_processing (bfd *abfd,
11671 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
11672{
11673 unsigned int i;
11674 Elf_Internal_Shdr **hdrpp;
11675 const char *name;
11676 asection *sec;
11677
11678 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11679 is nonzero. This is for compatibility with old objects, which used
11680 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11681 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11682 mips_set_isa_flags (abfd);
11683
b49e97c9
TS
11684 /* Set the sh_info field for .gptab sections and other appropriate
11685 info for each special section. */
11686 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11687 i < elf_numsections (abfd);
11688 i++, hdrpp++)
11689 {
11690 switch ((*hdrpp)->sh_type)
11691 {
11692 case SHT_MIPS_MSYM:
11693 case SHT_MIPS_LIBLIST:
11694 sec = bfd_get_section_by_name (abfd, ".dynstr");
11695 if (sec != NULL)
11696 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11697 break;
11698
11699 case SHT_MIPS_GPTAB:
11700 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11701 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11702 BFD_ASSERT (name != NULL
0112cd26 11703 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
11704 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11705 BFD_ASSERT (sec != NULL);
11706 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11707 break;
11708
11709 case SHT_MIPS_CONTENT:
11710 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11711 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11712 BFD_ASSERT (name != NULL
0112cd26 11713 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
11714 sec = bfd_get_section_by_name (abfd,
11715 name + sizeof ".MIPS.content" - 1);
11716 BFD_ASSERT (sec != NULL);
11717 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11718 break;
11719
11720 case SHT_MIPS_SYMBOL_LIB:
11721 sec = bfd_get_section_by_name (abfd, ".dynsym");
11722 if (sec != NULL)
11723 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11724 sec = bfd_get_section_by_name (abfd, ".liblist");
11725 if (sec != NULL)
11726 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11727 break;
11728
11729 case SHT_MIPS_EVENTS:
11730 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11731 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11732 BFD_ASSERT (name != NULL);
0112cd26 11733 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
11734 sec = bfd_get_section_by_name (abfd,
11735 name + sizeof ".MIPS.events" - 1);
11736 else
11737 {
0112cd26 11738 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
11739 sec = bfd_get_section_by_name (abfd,
11740 (name
11741 + sizeof ".MIPS.post_rel" - 1));
11742 }
11743 BFD_ASSERT (sec != NULL);
11744 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11745 break;
11746
11747 }
11748 }
11749}
11750\f
8dc1a139 11751/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
11752 segments. */
11753
11754int
a6b96beb
AM
11755_bfd_mips_elf_additional_program_headers (bfd *abfd,
11756 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
11757{
11758 asection *s;
11759 int ret = 0;
11760
11761 /* See if we need a PT_MIPS_REGINFO segment. */
11762 s = bfd_get_section_by_name (abfd, ".reginfo");
11763 if (s && (s->flags & SEC_LOAD))
11764 ++ret;
11765
11766 /* See if we need a PT_MIPS_OPTIONS segment. */
11767 if (IRIX_COMPAT (abfd) == ict_irix6
11768 && bfd_get_section_by_name (abfd,
11769 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11770 ++ret;
11771
11772 /* See if we need a PT_MIPS_RTPROC segment. */
11773 if (IRIX_COMPAT (abfd) == ict_irix5
11774 && bfd_get_section_by_name (abfd, ".dynamic")
11775 && bfd_get_section_by_name (abfd, ".mdebug"))
11776 ++ret;
11777
98c904a8
RS
11778 /* Allocate a PT_NULL header in dynamic objects. See
11779 _bfd_mips_elf_modify_segment_map for details. */
11780 if (!SGI_COMPAT (abfd)
11781 && bfd_get_section_by_name (abfd, ".dynamic"))
11782 ++ret;
11783
b49e97c9
TS
11784 return ret;
11785}
11786
8dc1a139 11787/* Modify the segment map for an IRIX5 executable. */
b49e97c9 11788
b34976b6 11789bfd_boolean
9719ad41 11790_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 11791 struct bfd_link_info *info)
b49e97c9
TS
11792{
11793 asection *s;
11794 struct elf_segment_map *m, **pm;
11795 bfd_size_type amt;
11796
11797 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11798 segment. */
11799 s = bfd_get_section_by_name (abfd, ".reginfo");
11800 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11801 {
12bd6957 11802 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
11803 if (m->p_type == PT_MIPS_REGINFO)
11804 break;
11805 if (m == NULL)
11806 {
11807 amt = sizeof *m;
9719ad41 11808 m = bfd_zalloc (abfd, amt);
b49e97c9 11809 if (m == NULL)
b34976b6 11810 return FALSE;
b49e97c9
TS
11811
11812 m->p_type = PT_MIPS_REGINFO;
11813 m->count = 1;
11814 m->sections[0] = s;
11815
11816 /* We want to put it after the PHDR and INTERP segments. */
12bd6957 11817 pm = &elf_seg_map (abfd);
b49e97c9
TS
11818 while (*pm != NULL
11819 && ((*pm)->p_type == PT_PHDR
11820 || (*pm)->p_type == PT_INTERP))
11821 pm = &(*pm)->next;
11822
11823 m->next = *pm;
11824 *pm = m;
11825 }
11826 }
11827
11828 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11829 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 11830 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 11831 table. */
c1fd6598
AO
11832 if (NEWABI_P (abfd)
11833 /* On non-IRIX6 new abi, we'll have already created a segment
11834 for this section, so don't create another. I'm not sure this
11835 is not also the case for IRIX 6, but I can't test it right
11836 now. */
11837 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
11838 {
11839 for (s = abfd->sections; s; s = s->next)
11840 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11841 break;
11842
11843 if (s)
11844 {
11845 struct elf_segment_map *options_segment;
11846
12bd6957 11847 pm = &elf_seg_map (abfd);
98a8deaf
RS
11848 while (*pm != NULL
11849 && ((*pm)->p_type == PT_PHDR
11850 || (*pm)->p_type == PT_INTERP))
11851 pm = &(*pm)->next;
b49e97c9 11852
8ded5a0f
AM
11853 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11854 {
11855 amt = sizeof (struct elf_segment_map);
11856 options_segment = bfd_zalloc (abfd, amt);
11857 options_segment->next = *pm;
11858 options_segment->p_type = PT_MIPS_OPTIONS;
11859 options_segment->p_flags = PF_R;
11860 options_segment->p_flags_valid = TRUE;
11861 options_segment->count = 1;
11862 options_segment->sections[0] = s;
11863 *pm = options_segment;
11864 }
b49e97c9
TS
11865 }
11866 }
11867 else
11868 {
11869 if (IRIX_COMPAT (abfd) == ict_irix5)
11870 {
11871 /* If there are .dynamic and .mdebug sections, we make a room
11872 for the RTPROC header. FIXME: Rewrite without section names. */
11873 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11874 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11875 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11876 {
12bd6957 11877 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
11878 if (m->p_type == PT_MIPS_RTPROC)
11879 break;
11880 if (m == NULL)
11881 {
11882 amt = sizeof *m;
9719ad41 11883 m = bfd_zalloc (abfd, amt);
b49e97c9 11884 if (m == NULL)
b34976b6 11885 return FALSE;
b49e97c9
TS
11886
11887 m->p_type = PT_MIPS_RTPROC;
11888
11889 s = bfd_get_section_by_name (abfd, ".rtproc");
11890 if (s == NULL)
11891 {
11892 m->count = 0;
11893 m->p_flags = 0;
11894 m->p_flags_valid = 1;
11895 }
11896 else
11897 {
11898 m->count = 1;
11899 m->sections[0] = s;
11900 }
11901
11902 /* We want to put it after the DYNAMIC segment. */
12bd6957 11903 pm = &elf_seg_map (abfd);
b49e97c9
TS
11904 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11905 pm = &(*pm)->next;
11906 if (*pm != NULL)
11907 pm = &(*pm)->next;
11908
11909 m->next = *pm;
11910 *pm = m;
11911 }
11912 }
11913 }
8dc1a139 11914 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
11915 .dynstr, .dynsym, and .hash sections, and everything in
11916 between. */
12bd6957 11917 for (pm = &elf_seg_map (abfd); *pm != NULL;
b49e97c9
TS
11918 pm = &(*pm)->next)
11919 if ((*pm)->p_type == PT_DYNAMIC)
11920 break;
11921 m = *pm;
f6f62d6f
RS
11922 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11923 glibc's dynamic linker has traditionally derived the number of
11924 tags from the p_filesz field, and sometimes allocates stack
11925 arrays of that size. An overly-big PT_DYNAMIC segment can
11926 be actively harmful in such cases. Making PT_DYNAMIC contain
11927 other sections can also make life hard for the prelinker,
11928 which might move one of the other sections to a different
11929 PT_LOAD segment. */
11930 if (SGI_COMPAT (abfd)
11931 && m != NULL
11932 && m->count == 1
11933 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
11934 {
11935 static const char *sec_names[] =
11936 {
11937 ".dynamic", ".dynstr", ".dynsym", ".hash"
11938 };
11939 bfd_vma low, high;
11940 unsigned int i, c;
11941 struct elf_segment_map *n;
11942
792b4a53 11943 low = ~(bfd_vma) 0;
b49e97c9
TS
11944 high = 0;
11945 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11946 {
11947 s = bfd_get_section_by_name (abfd, sec_names[i]);
11948 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11949 {
11950 bfd_size_type sz;
11951
11952 if (low > s->vma)
11953 low = s->vma;
eea6121a 11954 sz = s->size;
b49e97c9
TS
11955 if (high < s->vma + sz)
11956 high = s->vma + sz;
11957 }
11958 }
11959
11960 c = 0;
11961 for (s = abfd->sections; s != NULL; s = s->next)
11962 if ((s->flags & SEC_LOAD) != 0
11963 && s->vma >= low
eea6121a 11964 && s->vma + s->size <= high)
b49e97c9
TS
11965 ++c;
11966
11967 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 11968 n = bfd_zalloc (abfd, amt);
b49e97c9 11969 if (n == NULL)
b34976b6 11970 return FALSE;
b49e97c9
TS
11971 *n = *m;
11972 n->count = c;
11973
11974 i = 0;
11975 for (s = abfd->sections; s != NULL; s = s->next)
11976 {
11977 if ((s->flags & SEC_LOAD) != 0
11978 && s->vma >= low
eea6121a 11979 && s->vma + s->size <= high)
b49e97c9
TS
11980 {
11981 n->sections[i] = s;
11982 ++i;
11983 }
11984 }
11985
11986 *pm = n;
11987 }
11988 }
11989
98c904a8
RS
11990 /* Allocate a spare program header in dynamic objects so that tools
11991 like the prelinker can add an extra PT_LOAD entry.
11992
11993 If the prelinker needs to make room for a new PT_LOAD entry, its
11994 standard procedure is to move the first (read-only) sections into
11995 the new (writable) segment. However, the MIPS ABI requires
11996 .dynamic to be in a read-only segment, and the section will often
11997 start within sizeof (ElfNN_Phdr) bytes of the last program header.
11998
11999 Although the prelinker could in principle move .dynamic to a
12000 writable segment, it seems better to allocate a spare program
12001 header instead, and avoid the need to move any sections.
12002 There is a long tradition of allocating spare dynamic tags,
12003 so allocating a spare program header seems like a natural
7c8b76cc
JM
12004 extension.
12005
12006 If INFO is NULL, we may be copying an already prelinked binary
12007 with objcopy or strip, so do not add this header. */
12008 if (info != NULL
12009 && !SGI_COMPAT (abfd)
98c904a8
RS
12010 && bfd_get_section_by_name (abfd, ".dynamic"))
12011 {
12bd6957 12012 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
98c904a8
RS
12013 if ((*pm)->p_type == PT_NULL)
12014 break;
12015 if (*pm == NULL)
12016 {
12017 m = bfd_zalloc (abfd, sizeof (*m));
12018 if (m == NULL)
12019 return FALSE;
12020
12021 m->p_type = PT_NULL;
12022 *pm = m;
12023 }
12024 }
12025
b34976b6 12026 return TRUE;
b49e97c9
TS
12027}
12028\f
12029/* Return the section that should be marked against GC for a given
12030 relocation. */
12031
12032asection *
9719ad41 12033_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 12034 struct bfd_link_info *info,
9719ad41
RS
12035 Elf_Internal_Rela *rel,
12036 struct elf_link_hash_entry *h,
12037 Elf_Internal_Sym *sym)
b49e97c9
TS
12038{
12039 /* ??? Do mips16 stub sections need to be handled special? */
12040
12041 if (h != NULL)
07adf181
AM
12042 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12043 {
12044 case R_MIPS_GNU_VTINHERIT:
12045 case R_MIPS_GNU_VTENTRY:
12046 return NULL;
12047 }
b49e97c9 12048
07adf181 12049 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
12050}
12051
12052/* Update the got entry reference counts for the section being removed. */
12053
b34976b6 12054bfd_boolean
9719ad41
RS
12055_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
12056 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12057 asection *sec ATTRIBUTE_UNUSED,
12058 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
12059{
12060#if 0
12061 Elf_Internal_Shdr *symtab_hdr;
12062 struct elf_link_hash_entry **sym_hashes;
12063 bfd_signed_vma *local_got_refcounts;
12064 const Elf_Internal_Rela *rel, *relend;
12065 unsigned long r_symndx;
12066 struct elf_link_hash_entry *h;
12067
7dda2462
TG
12068 if (info->relocatable)
12069 return TRUE;
12070
b49e97c9
TS
12071 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12072 sym_hashes = elf_sym_hashes (abfd);
12073 local_got_refcounts = elf_local_got_refcounts (abfd);
12074
12075 relend = relocs + sec->reloc_count;
12076 for (rel = relocs; rel < relend; rel++)
12077 switch (ELF_R_TYPE (abfd, rel->r_info))
12078 {
738e5348
RS
12079 case R_MIPS16_GOT16:
12080 case R_MIPS16_CALL16:
b49e97c9
TS
12081 case R_MIPS_GOT16:
12082 case R_MIPS_CALL16:
12083 case R_MIPS_CALL_HI16:
12084 case R_MIPS_CALL_LO16:
12085 case R_MIPS_GOT_HI16:
12086 case R_MIPS_GOT_LO16:
4a14403c
TS
12087 case R_MIPS_GOT_DISP:
12088 case R_MIPS_GOT_PAGE:
12089 case R_MIPS_GOT_OFST:
df58fc94
RS
12090 case R_MICROMIPS_GOT16:
12091 case R_MICROMIPS_CALL16:
12092 case R_MICROMIPS_CALL_HI16:
12093 case R_MICROMIPS_CALL_LO16:
12094 case R_MICROMIPS_GOT_HI16:
12095 case R_MICROMIPS_GOT_LO16:
12096 case R_MICROMIPS_GOT_DISP:
12097 case R_MICROMIPS_GOT_PAGE:
12098 case R_MICROMIPS_GOT_OFST:
b49e97c9
TS
12099 /* ??? It would seem that the existing MIPS code does no sort
12100 of reference counting or whatnot on its GOT and PLT entries,
12101 so it is not possible to garbage collect them at this time. */
12102 break;
12103
12104 default:
12105 break;
12106 }
12107#endif
12108
b34976b6 12109 return TRUE;
b49e97c9
TS
12110}
12111\f
12112/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12113 hiding the old indirect symbol. Process additional relocation
12114 information. Also called for weakdefs, in which case we just let
12115 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12116
12117void
fcfa13d2 12118_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
12119 struct elf_link_hash_entry *dir,
12120 struct elf_link_hash_entry *ind)
b49e97c9
TS
12121{
12122 struct mips_elf_link_hash_entry *dirmips, *indmips;
12123
fcfa13d2 12124 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 12125
861fb55a
DJ
12126 dirmips = (struct mips_elf_link_hash_entry *) dir;
12127 indmips = (struct mips_elf_link_hash_entry *) ind;
12128 /* Any absolute non-dynamic relocations against an indirect or weak
12129 definition will be against the target symbol. */
12130 if (indmips->has_static_relocs)
12131 dirmips->has_static_relocs = TRUE;
12132
b49e97c9
TS
12133 if (ind->root.type != bfd_link_hash_indirect)
12134 return;
12135
b49e97c9
TS
12136 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12137 if (indmips->readonly_reloc)
b34976b6 12138 dirmips->readonly_reloc = TRUE;
b49e97c9 12139 if (indmips->no_fn_stub)
b34976b6 12140 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
12141 if (indmips->fn_stub)
12142 {
12143 dirmips->fn_stub = indmips->fn_stub;
12144 indmips->fn_stub = NULL;
12145 }
12146 if (indmips->need_fn_stub)
12147 {
12148 dirmips->need_fn_stub = TRUE;
12149 indmips->need_fn_stub = FALSE;
12150 }
12151 if (indmips->call_stub)
12152 {
12153 dirmips->call_stub = indmips->call_stub;
12154 indmips->call_stub = NULL;
12155 }
12156 if (indmips->call_fp_stub)
12157 {
12158 dirmips->call_fp_stub = indmips->call_fp_stub;
12159 indmips->call_fp_stub = NULL;
12160 }
634835ae
RS
12161 if (indmips->global_got_area < dirmips->global_got_area)
12162 dirmips->global_got_area = indmips->global_got_area;
12163 if (indmips->global_got_area < GGA_NONE)
12164 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
12165 if (indmips->has_nonpic_branches)
12166 dirmips->has_nonpic_branches = TRUE;
b49e97c9 12167}
b49e97c9 12168\f
d01414a5
TS
12169#define PDR_SIZE 32
12170
b34976b6 12171bfd_boolean
9719ad41
RS
12172_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12173 struct bfd_link_info *info)
d01414a5
TS
12174{
12175 asection *o;
b34976b6 12176 bfd_boolean ret = FALSE;
d01414a5
TS
12177 unsigned char *tdata;
12178 size_t i, skip;
12179
12180 o = bfd_get_section_by_name (abfd, ".pdr");
12181 if (! o)
b34976b6 12182 return FALSE;
eea6121a 12183 if (o->size == 0)
b34976b6 12184 return FALSE;
eea6121a 12185 if (o->size % PDR_SIZE != 0)
b34976b6 12186 return FALSE;
d01414a5
TS
12187 if (o->output_section != NULL
12188 && bfd_is_abs_section (o->output_section))
b34976b6 12189 return FALSE;
d01414a5 12190
eea6121a 12191 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 12192 if (! tdata)
b34976b6 12193 return FALSE;
d01414a5 12194
9719ad41 12195 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 12196 info->keep_memory);
d01414a5
TS
12197 if (!cookie->rels)
12198 {
12199 free (tdata);
b34976b6 12200 return FALSE;
d01414a5
TS
12201 }
12202
12203 cookie->rel = cookie->rels;
12204 cookie->relend = cookie->rels + o->reloc_count;
12205
eea6121a 12206 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 12207 {
c152c796 12208 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
12209 {
12210 tdata[i] = 1;
12211 skip ++;
12212 }
12213 }
12214
12215 if (skip != 0)
12216 {
f0abc2a1 12217 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 12218 o->size -= skip * PDR_SIZE;
b34976b6 12219 ret = TRUE;
d01414a5
TS
12220 }
12221 else
12222 free (tdata);
12223
12224 if (! info->keep_memory)
12225 free (cookie->rels);
12226
12227 return ret;
12228}
12229
b34976b6 12230bfd_boolean
9719ad41 12231_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
12232{
12233 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
12234 return TRUE;
12235 return FALSE;
53bfd6b4 12236}
d01414a5 12237
b34976b6 12238bfd_boolean
c7b8f16e
JB
12239_bfd_mips_elf_write_section (bfd *output_bfd,
12240 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12241 asection *sec, bfd_byte *contents)
d01414a5
TS
12242{
12243 bfd_byte *to, *from, *end;
12244 int i;
12245
12246 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 12247 return FALSE;
d01414a5 12248
f0abc2a1 12249 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 12250 return FALSE;
d01414a5
TS
12251
12252 to = contents;
eea6121a 12253 end = contents + sec->size;
d01414a5
TS
12254 for (from = contents, i = 0;
12255 from < end;
12256 from += PDR_SIZE, i++)
12257 {
f0abc2a1 12258 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
12259 continue;
12260 if (to != from)
12261 memcpy (to, from, PDR_SIZE);
12262 to += PDR_SIZE;
12263 }
12264 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 12265 sec->output_offset, sec->size);
b34976b6 12266 return TRUE;
d01414a5 12267}
53bfd6b4 12268\f
df58fc94
RS
12269/* microMIPS code retains local labels for linker relaxation. Omit them
12270 from output by default for clarity. */
12271
12272bfd_boolean
12273_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12274{
12275 return _bfd_elf_is_local_label_name (abfd, sym->name);
12276}
12277
b49e97c9
TS
12278/* MIPS ELF uses a special find_nearest_line routine in order the
12279 handle the ECOFF debugging information. */
12280
12281struct mips_elf_find_line
12282{
12283 struct ecoff_debug_info d;
12284 struct ecoff_find_line i;
12285};
12286
b34976b6 12287bfd_boolean
9719ad41
RS
12288_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
12289 asymbol **symbols, bfd_vma offset,
12290 const char **filename_ptr,
12291 const char **functionname_ptr,
12292 unsigned int *line_ptr)
b49e97c9
TS
12293{
12294 asection *msec;
12295
12296 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
12297 filename_ptr, functionname_ptr,
12298 line_ptr))
b34976b6 12299 return TRUE;
b49e97c9 12300
fc28f9aa
TG
12301 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
12302 section, symbols, offset,
b49e97c9 12303 filename_ptr, functionname_ptr,
9b8d1a36 12304 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 12305 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 12306 return TRUE;
b49e97c9
TS
12307
12308 msec = bfd_get_section_by_name (abfd, ".mdebug");
12309 if (msec != NULL)
12310 {
12311 flagword origflags;
12312 struct mips_elf_find_line *fi;
12313 const struct ecoff_debug_swap * const swap =
12314 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12315
12316 /* If we are called during a link, mips_elf_final_link may have
12317 cleared the SEC_HAS_CONTENTS field. We force it back on here
12318 if appropriate (which it normally will be). */
12319 origflags = msec->flags;
12320 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12321 msec->flags |= SEC_HAS_CONTENTS;
12322
698600e4 12323 fi = mips_elf_tdata (abfd)->find_line_info;
b49e97c9
TS
12324 if (fi == NULL)
12325 {
12326 bfd_size_type external_fdr_size;
12327 char *fraw_src;
12328 char *fraw_end;
12329 struct fdr *fdr_ptr;
12330 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12331
9719ad41 12332 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
12333 if (fi == NULL)
12334 {
12335 msec->flags = origflags;
b34976b6 12336 return FALSE;
b49e97c9
TS
12337 }
12338
12339 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12340 {
12341 msec->flags = origflags;
b34976b6 12342 return FALSE;
b49e97c9
TS
12343 }
12344
12345 /* Swap in the FDR information. */
12346 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 12347 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
12348 if (fi->d.fdr == NULL)
12349 {
12350 msec->flags = origflags;
b34976b6 12351 return FALSE;
b49e97c9
TS
12352 }
12353 external_fdr_size = swap->external_fdr_size;
12354 fdr_ptr = fi->d.fdr;
12355 fraw_src = (char *) fi->d.external_fdr;
12356 fraw_end = (fraw_src
12357 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12358 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 12359 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9 12360
698600e4 12361 mips_elf_tdata (abfd)->find_line_info = fi;
b49e97c9
TS
12362
12363 /* Note that we don't bother to ever free this information.
12364 find_nearest_line is either called all the time, as in
12365 objdump -l, so the information should be saved, or it is
12366 rarely called, as in ld error messages, so the memory
12367 wasted is unimportant. Still, it would probably be a
12368 good idea for free_cached_info to throw it away. */
12369 }
12370
12371 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
12372 &fi->i, filename_ptr, functionname_ptr,
12373 line_ptr))
12374 {
12375 msec->flags = origflags;
b34976b6 12376 return TRUE;
b49e97c9
TS
12377 }
12378
12379 msec->flags = origflags;
12380 }
12381
12382 /* Fall back on the generic ELF find_nearest_line routine. */
12383
12384 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
12385 filename_ptr, functionname_ptr,
12386 line_ptr);
12387}
4ab527b0
FF
12388
12389bfd_boolean
12390_bfd_mips_elf_find_inliner_info (bfd *abfd,
12391 const char **filename_ptr,
12392 const char **functionname_ptr,
12393 unsigned int *line_ptr)
12394{
12395 bfd_boolean found;
12396 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12397 functionname_ptr, line_ptr,
12398 & elf_tdata (abfd)->dwarf2_find_line_info);
12399 return found;
12400}
12401
b49e97c9
TS
12402\f
12403/* When are writing out the .options or .MIPS.options section,
12404 remember the bytes we are writing out, so that we can install the
12405 GP value in the section_processing routine. */
12406
b34976b6 12407bfd_boolean
9719ad41
RS
12408_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
12409 const void *location,
12410 file_ptr offset, bfd_size_type count)
b49e97c9 12411{
cc2e31b9 12412 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
12413 {
12414 bfd_byte *c;
12415
12416 if (elf_section_data (section) == NULL)
12417 {
12418 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 12419 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 12420 if (elf_section_data (section) == NULL)
b34976b6 12421 return FALSE;
b49e97c9 12422 }
f0abc2a1 12423 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
12424 if (c == NULL)
12425 {
eea6121a 12426 c = bfd_zalloc (abfd, section->size);
b49e97c9 12427 if (c == NULL)
b34976b6 12428 return FALSE;
f0abc2a1 12429 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
12430 }
12431
9719ad41 12432 memcpy (c + offset, location, count);
b49e97c9
TS
12433 }
12434
12435 return _bfd_elf_set_section_contents (abfd, section, location, offset,
12436 count);
12437}
12438
12439/* This is almost identical to bfd_generic_get_... except that some
12440 MIPS relocations need to be handled specially. Sigh. */
12441
12442bfd_byte *
9719ad41
RS
12443_bfd_elf_mips_get_relocated_section_contents
12444 (bfd *abfd,
12445 struct bfd_link_info *link_info,
12446 struct bfd_link_order *link_order,
12447 bfd_byte *data,
12448 bfd_boolean relocatable,
12449 asymbol **symbols)
b49e97c9
TS
12450{
12451 /* Get enough memory to hold the stuff */
12452 bfd *input_bfd = link_order->u.indirect.section->owner;
12453 asection *input_section = link_order->u.indirect.section;
eea6121a 12454 bfd_size_type sz;
b49e97c9
TS
12455
12456 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12457 arelent **reloc_vector = NULL;
12458 long reloc_count;
12459
12460 if (reloc_size < 0)
12461 goto error_return;
12462
9719ad41 12463 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
12464 if (reloc_vector == NULL && reloc_size != 0)
12465 goto error_return;
12466
12467 /* read in the section */
eea6121a
AM
12468 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
12469 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
12470 goto error_return;
12471
b49e97c9
TS
12472 reloc_count = bfd_canonicalize_reloc (input_bfd,
12473 input_section,
12474 reloc_vector,
12475 symbols);
12476 if (reloc_count < 0)
12477 goto error_return;
12478
12479 if (reloc_count > 0)
12480 {
12481 arelent **parent;
12482 /* for mips */
12483 int gp_found;
12484 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
12485
12486 {
12487 struct bfd_hash_entry *h;
12488 struct bfd_link_hash_entry *lh;
12489 /* Skip all this stuff if we aren't mixing formats. */
12490 if (abfd && input_bfd
12491 && abfd->xvec == input_bfd->xvec)
12492 lh = 0;
12493 else
12494 {
b34976b6 12495 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
12496 lh = (struct bfd_link_hash_entry *) h;
12497 }
12498 lookup:
12499 if (lh)
12500 {
12501 switch (lh->type)
12502 {
12503 case bfd_link_hash_undefined:
12504 case bfd_link_hash_undefweak:
12505 case bfd_link_hash_common:
12506 gp_found = 0;
12507 break;
12508 case bfd_link_hash_defined:
12509 case bfd_link_hash_defweak:
12510 gp_found = 1;
12511 gp = lh->u.def.value;
12512 break;
12513 case bfd_link_hash_indirect:
12514 case bfd_link_hash_warning:
12515 lh = lh->u.i.link;
12516 /* @@FIXME ignoring warning for now */
12517 goto lookup;
12518 case bfd_link_hash_new:
12519 default:
12520 abort ();
12521 }
12522 }
12523 else
12524 gp_found = 0;
12525 }
12526 /* end mips */
9719ad41 12527 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 12528 {
9719ad41 12529 char *error_message = NULL;
b49e97c9
TS
12530 bfd_reloc_status_type r;
12531
12532 /* Specific to MIPS: Deal with relocation types that require
12533 knowing the gp of the output bfd. */
12534 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 12535
8236346f
EC
12536 /* If we've managed to find the gp and have a special
12537 function for the relocation then go ahead, else default
12538 to the generic handling. */
12539 if (gp_found
12540 && (*parent)->howto->special_function
12541 == _bfd_mips_elf32_gprel16_reloc)
12542 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
12543 input_section, relocatable,
12544 data, gp);
12545 else
86324f90 12546 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
12547 input_section,
12548 relocatable ? abfd : NULL,
12549 &error_message);
b49e97c9 12550
1049f94e 12551 if (relocatable)
b49e97c9
TS
12552 {
12553 asection *os = input_section->output_section;
12554
12555 /* A partial link, so keep the relocs */
12556 os->orelocation[os->reloc_count] = *parent;
12557 os->reloc_count++;
12558 }
12559
12560 if (r != bfd_reloc_ok)
12561 {
12562 switch (r)
12563 {
12564 case bfd_reloc_undefined:
12565 if (!((*link_info->callbacks->undefined_symbol)
12566 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 12567 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
12568 goto error_return;
12569 break;
12570 case bfd_reloc_dangerous:
9719ad41 12571 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
12572 if (!((*link_info->callbacks->reloc_dangerous)
12573 (link_info, error_message, input_bfd, input_section,
12574 (*parent)->address)))
12575 goto error_return;
12576 break;
12577 case bfd_reloc_overflow:
12578 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
12579 (link_info, NULL,
12580 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
12581 (*parent)->howto->name, (*parent)->addend,
12582 input_bfd, input_section, (*parent)->address)))
12583 goto error_return;
12584 break;
12585 case bfd_reloc_outofrange:
12586 default:
12587 abort ();
12588 break;
12589 }
12590
12591 }
12592 }
12593 }
12594 if (reloc_vector != NULL)
12595 free (reloc_vector);
12596 return data;
12597
12598error_return:
12599 if (reloc_vector != NULL)
12600 free (reloc_vector);
12601 return NULL;
12602}
12603\f
df58fc94
RS
12604static bfd_boolean
12605mips_elf_relax_delete_bytes (bfd *abfd,
12606 asection *sec, bfd_vma addr, int count)
12607{
12608 Elf_Internal_Shdr *symtab_hdr;
12609 unsigned int sec_shndx;
12610 bfd_byte *contents;
12611 Elf_Internal_Rela *irel, *irelend;
12612 Elf_Internal_Sym *isym;
12613 Elf_Internal_Sym *isymend;
12614 struct elf_link_hash_entry **sym_hashes;
12615 struct elf_link_hash_entry **end_hashes;
12616 struct elf_link_hash_entry **start_hashes;
12617 unsigned int symcount;
12618
12619 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
12620 contents = elf_section_data (sec)->this_hdr.contents;
12621
12622 irel = elf_section_data (sec)->relocs;
12623 irelend = irel + sec->reloc_count;
12624
12625 /* Actually delete the bytes. */
12626 memmove (contents + addr, contents + addr + count,
12627 (size_t) (sec->size - addr - count));
12628 sec->size -= count;
12629
12630 /* Adjust all the relocs. */
12631 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12632 {
12633 /* Get the new reloc address. */
12634 if (irel->r_offset > addr)
12635 irel->r_offset -= count;
12636 }
12637
12638 BFD_ASSERT (addr % 2 == 0);
12639 BFD_ASSERT (count % 2 == 0);
12640
12641 /* Adjust the local symbols defined in this section. */
12642 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12643 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12644 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2309ddf2 12645 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
df58fc94
RS
12646 isym->st_value -= count;
12647
12648 /* Now adjust the global symbols defined in this section. */
12649 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
12650 - symtab_hdr->sh_info);
12651 sym_hashes = start_hashes = elf_sym_hashes (abfd);
12652 end_hashes = sym_hashes + symcount;
12653
12654 for (; sym_hashes < end_hashes; sym_hashes++)
12655 {
12656 struct elf_link_hash_entry *sym_hash = *sym_hashes;
12657
12658 if ((sym_hash->root.type == bfd_link_hash_defined
12659 || sym_hash->root.type == bfd_link_hash_defweak)
12660 && sym_hash->root.u.def.section == sec)
12661 {
2309ddf2 12662 bfd_vma value = sym_hash->root.u.def.value;
df58fc94 12663
df58fc94
RS
12664 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
12665 value &= MINUS_TWO;
12666 if (value > addr)
12667 sym_hash->root.u.def.value -= count;
12668 }
12669 }
12670
12671 return TRUE;
12672}
12673
12674
12675/* Opcodes needed for microMIPS relaxation as found in
12676 opcodes/micromips-opc.c. */
12677
12678struct opcode_descriptor {
12679 unsigned long match;
12680 unsigned long mask;
12681};
12682
12683/* The $ra register aka $31. */
12684
12685#define RA 31
12686
12687/* 32-bit instruction format register fields. */
12688
12689#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12690#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12691
12692/* Check if a 5-bit register index can be abbreviated to 3 bits. */
12693
12694#define OP16_VALID_REG(r) \
12695 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12696
12697
12698/* 32-bit and 16-bit branches. */
12699
12700static const struct opcode_descriptor b_insns_32[] = {
12701 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12702 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12703 { 0, 0 } /* End marker for find_match(). */
12704};
12705
12706static const struct opcode_descriptor bc_insn_32 =
12707 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12708
12709static const struct opcode_descriptor bz_insn_32 =
12710 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12711
12712static const struct opcode_descriptor bzal_insn_32 =
12713 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12714
12715static const struct opcode_descriptor beq_insn_32 =
12716 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12717
12718static const struct opcode_descriptor b_insn_16 =
12719 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12720
12721static const struct opcode_descriptor bz_insn_16 =
c088dedf 12722 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
df58fc94
RS
12723
12724
12725/* 32-bit and 16-bit branch EQ and NE zero. */
12726
12727/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12728 eq and second the ne. This convention is used when replacing a
12729 32-bit BEQ/BNE with the 16-bit version. */
12730
12731#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12732
12733static const struct opcode_descriptor bz_rs_insns_32[] = {
12734 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12735 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12736 { 0, 0 } /* End marker for find_match(). */
12737};
12738
12739static const struct opcode_descriptor bz_rt_insns_32[] = {
12740 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12741 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12742 { 0, 0 } /* End marker for find_match(). */
12743};
12744
12745static const struct opcode_descriptor bzc_insns_32[] = {
12746 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12747 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12748 { 0, 0 } /* End marker for find_match(). */
12749};
12750
12751static const struct opcode_descriptor bz_insns_16[] = {
12752 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12753 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12754 { 0, 0 } /* End marker for find_match(). */
12755};
12756
12757/* Switch between a 5-bit register index and its 3-bit shorthand. */
12758
12759#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12760#define BZ16_REG_FIELD(r) \
12761 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12762
12763
12764/* 32-bit instructions with a delay slot. */
12765
12766static const struct opcode_descriptor jal_insn_32_bd16 =
12767 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12768
12769static const struct opcode_descriptor jal_insn_32_bd32 =
12770 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12771
12772static const struct opcode_descriptor jal_x_insn_32_bd32 =
12773 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12774
12775static const struct opcode_descriptor j_insn_32 =
12776 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12777
12778static const struct opcode_descriptor jalr_insn_32 =
12779 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12780
12781/* This table can be compacted, because no opcode replacement is made. */
12782
12783static const struct opcode_descriptor ds_insns_32_bd16[] = {
12784 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12785
12786 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12787 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12788
12789 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12790 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12791 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12792 { 0, 0 } /* End marker for find_match(). */
12793};
12794
12795/* This table can be compacted, because no opcode replacement is made. */
12796
12797static const struct opcode_descriptor ds_insns_32_bd32[] = {
12798 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12799
12800 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12801 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12802 { 0, 0 } /* End marker for find_match(). */
12803};
12804
12805
12806/* 16-bit instructions with a delay slot. */
12807
12808static const struct opcode_descriptor jalr_insn_16_bd16 =
12809 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12810
12811static const struct opcode_descriptor jalr_insn_16_bd32 =
12812 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12813
12814static const struct opcode_descriptor jr_insn_16 =
12815 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12816
12817#define JR16_REG(opcode) ((opcode) & 0x1f)
12818
12819/* This table can be compacted, because no opcode replacement is made. */
12820
12821static const struct opcode_descriptor ds_insns_16_bd16[] = {
12822 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12823
12824 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12825 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12826 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12827 { 0, 0 } /* End marker for find_match(). */
12828};
12829
12830
12831/* LUI instruction. */
12832
12833static const struct opcode_descriptor lui_insn =
12834 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12835
12836
12837/* ADDIU instruction. */
12838
12839static const struct opcode_descriptor addiu_insn =
12840 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12841
12842static const struct opcode_descriptor addiupc_insn =
12843 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12844
12845#define ADDIUPC_REG_FIELD(r) \
12846 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12847
12848
12849/* Relaxable instructions in a JAL delay slot: MOVE. */
12850
12851/* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12852 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12853#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12854#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12855
12856#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12857#define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12858
12859static const struct opcode_descriptor move_insns_32[] = {
12860 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12861 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12862 { 0, 0 } /* End marker for find_match(). */
12863};
12864
12865static const struct opcode_descriptor move_insn_16 =
12866 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12867
12868
12869/* NOP instructions. */
12870
12871static const struct opcode_descriptor nop_insn_32 =
12872 { /* "nop", "", */ 0x00000000, 0xffffffff };
12873
12874static const struct opcode_descriptor nop_insn_16 =
12875 { /* "nop", "", */ 0x0c00, 0xffff };
12876
12877
12878/* Instruction match support. */
12879
12880#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12881
12882static int
12883find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12884{
12885 unsigned long indx;
12886
12887 for (indx = 0; insn[indx].mask != 0; indx++)
12888 if (MATCH (opcode, insn[indx]))
12889 return indx;
12890
12891 return -1;
12892}
12893
12894
12895/* Branch and delay slot decoding support. */
12896
12897/* If PTR points to what *might* be a 16-bit branch or jump, then
12898 return the minimum length of its delay slot, otherwise return 0.
12899 Non-zero results are not definitive as we might be checking against
12900 the second half of another instruction. */
12901
12902static int
12903check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12904{
12905 unsigned long opcode;
12906 int bdsize;
12907
12908 opcode = bfd_get_16 (abfd, ptr);
12909 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12910 /* 16-bit branch/jump with a 32-bit delay slot. */
12911 bdsize = 4;
12912 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12913 || find_match (opcode, ds_insns_16_bd16) >= 0)
12914 /* 16-bit branch/jump with a 16-bit delay slot. */
12915 bdsize = 2;
12916 else
12917 /* No delay slot. */
12918 bdsize = 0;
12919
12920 return bdsize;
12921}
12922
12923/* If PTR points to what *might* be a 32-bit branch or jump, then
12924 return the minimum length of its delay slot, otherwise return 0.
12925 Non-zero results are not definitive as we might be checking against
12926 the second half of another instruction. */
12927
12928static int
12929check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12930{
12931 unsigned long opcode;
12932 int bdsize;
12933
d21911ea 12934 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
12935 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12936 /* 32-bit branch/jump with a 32-bit delay slot. */
12937 bdsize = 4;
12938 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12939 /* 32-bit branch/jump with a 16-bit delay slot. */
12940 bdsize = 2;
12941 else
12942 /* No delay slot. */
12943 bdsize = 0;
12944
12945 return bdsize;
12946}
12947
12948/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12949 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12950
12951static bfd_boolean
12952check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12953{
12954 unsigned long opcode;
12955
12956 opcode = bfd_get_16 (abfd, ptr);
12957 if (MATCH (opcode, b_insn_16)
12958 /* B16 */
12959 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12960 /* JR16 */
12961 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12962 /* BEQZ16, BNEZ16 */
12963 || (MATCH (opcode, jalr_insn_16_bd32)
12964 /* JALR16 */
12965 && reg != JR16_REG (opcode) && reg != RA))
12966 return TRUE;
12967
12968 return FALSE;
12969}
12970
12971/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12972 then return TRUE, otherwise FALSE. */
12973
f41e5fcc 12974static bfd_boolean
df58fc94
RS
12975check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12976{
12977 unsigned long opcode;
12978
d21911ea 12979 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
12980 if (MATCH (opcode, j_insn_32)
12981 /* J */
12982 || MATCH (opcode, bc_insn_32)
12983 /* BC1F, BC1T, BC2F, BC2T */
12984 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
12985 /* JAL, JALX */
12986 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
12987 /* BGEZ, BGTZ, BLEZ, BLTZ */
12988 || (MATCH (opcode, bzal_insn_32)
12989 /* BGEZAL, BLTZAL */
12990 && reg != OP32_SREG (opcode) && reg != RA)
12991 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
12992 /* JALR, JALR.HB, BEQ, BNE */
12993 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
12994 return TRUE;
12995
12996 return FALSE;
12997}
12998
80cab405
MR
12999/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13000 IRELEND) at OFFSET indicate that there must be a compact branch there,
13001 then return TRUE, otherwise FALSE. */
df58fc94
RS
13002
13003static bfd_boolean
80cab405
MR
13004check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13005 const Elf_Internal_Rela *internal_relocs,
13006 const Elf_Internal_Rela *irelend)
df58fc94 13007{
80cab405
MR
13008 const Elf_Internal_Rela *irel;
13009 unsigned long opcode;
13010
d21911ea 13011 opcode = bfd_get_micromips_32 (abfd, ptr);
80cab405
MR
13012 if (find_match (opcode, bzc_insns_32) < 0)
13013 return FALSE;
df58fc94
RS
13014
13015 for (irel = internal_relocs; irel < irelend; irel++)
80cab405
MR
13016 if (irel->r_offset == offset
13017 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13018 return TRUE;
13019
df58fc94
RS
13020 return FALSE;
13021}
80cab405
MR
13022
13023/* Bitsize checking. */
13024#define IS_BITSIZE(val, N) \
13025 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13026 - (1ULL << ((N) - 1))) == (val))
13027
df58fc94
RS
13028\f
13029bfd_boolean
13030_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13031 struct bfd_link_info *link_info,
13032 bfd_boolean *again)
13033{
833794fc 13034 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
df58fc94
RS
13035 Elf_Internal_Shdr *symtab_hdr;
13036 Elf_Internal_Rela *internal_relocs;
13037 Elf_Internal_Rela *irel, *irelend;
13038 bfd_byte *contents = NULL;
13039 Elf_Internal_Sym *isymbuf = NULL;
13040
13041 /* Assume nothing changes. */
13042 *again = FALSE;
13043
13044 /* We don't have to do anything for a relocatable link, if
13045 this section does not have relocs, or if this is not a
13046 code section. */
13047
13048 if (link_info->relocatable
13049 || (sec->flags & SEC_RELOC) == 0
13050 || sec->reloc_count == 0
13051 || (sec->flags & SEC_CODE) == 0)
13052 return TRUE;
13053
13054 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13055
13056 /* Get a copy of the native relocations. */
13057 internal_relocs = (_bfd_elf_link_read_relocs
2c3fc389 13058 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
df58fc94
RS
13059 link_info->keep_memory));
13060 if (internal_relocs == NULL)
13061 goto error_return;
13062
13063 /* Walk through them looking for relaxing opportunities. */
13064 irelend = internal_relocs + sec->reloc_count;
13065 for (irel = internal_relocs; irel < irelend; irel++)
13066 {
13067 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13068 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13069 bfd_boolean target_is_micromips_code_p;
13070 unsigned long opcode;
13071 bfd_vma symval;
13072 bfd_vma pcrval;
2309ddf2 13073 bfd_byte *ptr;
df58fc94
RS
13074 int fndopc;
13075
13076 /* The number of bytes to delete for relaxation and from where
13077 to delete these bytes starting at irel->r_offset. */
13078 int delcnt = 0;
13079 int deloff = 0;
13080
13081 /* If this isn't something that can be relaxed, then ignore
13082 this reloc. */
13083 if (r_type != R_MICROMIPS_HI16
13084 && r_type != R_MICROMIPS_PC16_S1
2309ddf2 13085 && r_type != R_MICROMIPS_26_S1)
df58fc94
RS
13086 continue;
13087
13088 /* Get the section contents if we haven't done so already. */
13089 if (contents == NULL)
13090 {
13091 /* Get cached copy if it exists. */
13092 if (elf_section_data (sec)->this_hdr.contents != NULL)
13093 contents = elf_section_data (sec)->this_hdr.contents;
13094 /* Go get them off disk. */
13095 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13096 goto error_return;
13097 }
2309ddf2 13098 ptr = contents + irel->r_offset;
df58fc94
RS
13099
13100 /* Read this BFD's local symbols if we haven't done so already. */
13101 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13102 {
13103 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13104 if (isymbuf == NULL)
13105 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13106 symtab_hdr->sh_info, 0,
13107 NULL, NULL, NULL);
13108 if (isymbuf == NULL)
13109 goto error_return;
13110 }
13111
13112 /* Get the value of the symbol referred to by the reloc. */
13113 if (r_symndx < symtab_hdr->sh_info)
13114 {
13115 /* A local symbol. */
13116 Elf_Internal_Sym *isym;
13117 asection *sym_sec;
13118
13119 isym = isymbuf + r_symndx;
13120 if (isym->st_shndx == SHN_UNDEF)
13121 sym_sec = bfd_und_section_ptr;
13122 else if (isym->st_shndx == SHN_ABS)
13123 sym_sec = bfd_abs_section_ptr;
13124 else if (isym->st_shndx == SHN_COMMON)
13125 sym_sec = bfd_com_section_ptr;
13126 else
13127 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13128 symval = (isym->st_value
13129 + sym_sec->output_section->vma
13130 + sym_sec->output_offset);
13131 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13132 }
13133 else
13134 {
13135 unsigned long indx;
13136 struct elf_link_hash_entry *h;
13137
13138 /* An external symbol. */
13139 indx = r_symndx - symtab_hdr->sh_info;
13140 h = elf_sym_hashes (abfd)[indx];
13141 BFD_ASSERT (h != NULL);
13142
13143 if (h->root.type != bfd_link_hash_defined
13144 && h->root.type != bfd_link_hash_defweak)
13145 /* This appears to be a reference to an undefined
13146 symbol. Just ignore it -- it will be caught by the
13147 regular reloc processing. */
13148 continue;
13149
13150 symval = (h->root.u.def.value
13151 + h->root.u.def.section->output_section->vma
13152 + h->root.u.def.section->output_offset);
13153 target_is_micromips_code_p = (!h->needs_plt
13154 && ELF_ST_IS_MICROMIPS (h->other));
13155 }
13156
13157
13158 /* For simplicity of coding, we are going to modify the
13159 section contents, the section relocs, and the BFD symbol
13160 table. We must tell the rest of the code not to free up this
13161 information. It would be possible to instead create a table
13162 of changes which have to be made, as is done in coff-mips.c;
13163 that would be more work, but would require less memory when
13164 the linker is run. */
13165
13166 /* Only 32-bit instructions relaxed. */
13167 if (irel->r_offset + 4 > sec->size)
13168 continue;
13169
d21911ea 13170 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13171
13172 /* This is the pc-relative distance from the instruction the
13173 relocation is applied to, to the symbol referred. */
13174 pcrval = (symval
13175 - (sec->output_section->vma + sec->output_offset)
13176 - irel->r_offset);
13177
13178 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13179 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13180 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13181
13182 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13183
13184 where pcrval has first to be adjusted to apply against the LO16
13185 location (we make the adjustment later on, when we have figured
13186 out the offset). */
13187 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13188 {
80cab405 13189 bfd_boolean bzc = FALSE;
df58fc94
RS
13190 unsigned long nextopc;
13191 unsigned long reg;
13192 bfd_vma offset;
13193
13194 /* Give up if the previous reloc was a HI16 against this symbol
13195 too. */
13196 if (irel > internal_relocs
13197 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13198 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13199 continue;
13200
13201 /* Or if the next reloc is not a LO16 against this symbol. */
13202 if (irel + 1 >= irelend
13203 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13204 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13205 continue;
13206
13207 /* Or if the second next reloc is a LO16 against this symbol too. */
13208 if (irel + 2 >= irelend
13209 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13210 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13211 continue;
13212
80cab405
MR
13213 /* See if the LUI instruction *might* be in a branch delay slot.
13214 We check whether what looks like a 16-bit branch or jump is
13215 actually an immediate argument to a compact branch, and let
13216 it through if so. */
df58fc94 13217 if (irel->r_offset >= 2
2309ddf2 13218 && check_br16_dslot (abfd, ptr - 2)
df58fc94 13219 && !(irel->r_offset >= 4
80cab405
MR
13220 && (bzc = check_relocated_bzc (abfd,
13221 ptr - 4, irel->r_offset - 4,
13222 internal_relocs, irelend))))
df58fc94
RS
13223 continue;
13224 if (irel->r_offset >= 4
80cab405 13225 && !bzc
2309ddf2 13226 && check_br32_dslot (abfd, ptr - 4))
df58fc94
RS
13227 continue;
13228
13229 reg = OP32_SREG (opcode);
13230
13231 /* We only relax adjacent instructions or ones separated with
13232 a branch or jump that has a delay slot. The branch or jump
13233 must not fiddle with the register used to hold the address.
13234 Subtract 4 for the LUI itself. */
13235 offset = irel[1].r_offset - irel[0].r_offset;
13236 switch (offset - 4)
13237 {
13238 case 0:
13239 break;
13240 case 2:
2309ddf2 13241 if (check_br16 (abfd, ptr + 4, reg))
df58fc94
RS
13242 break;
13243 continue;
13244 case 4:
2309ddf2 13245 if (check_br32 (abfd, ptr + 4, reg))
df58fc94
RS
13246 break;
13247 continue;
13248 default:
13249 continue;
13250 }
13251
d21911ea 13252 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
df58fc94
RS
13253
13254 /* Give up unless the same register is used with both
13255 relocations. */
13256 if (OP32_SREG (nextopc) != reg)
13257 continue;
13258
13259 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13260 and rounding up to take masking of the two LSBs into account. */
13261 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13262
13263 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13264 if (IS_BITSIZE (symval, 16))
13265 {
13266 /* Fix the relocation's type. */
13267 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13268
13269 /* Instructions using R_MICROMIPS_LO16 have the base or
13270 source register in bits 20:16. This register becomes $0
13271 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13272 nextopc &= ~0x001f0000;
13273 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13274 contents + irel[1].r_offset);
13275 }
13276
13277 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13278 We add 4 to take LUI deletion into account while checking
13279 the PC-relative distance. */
13280 else if (symval % 4 == 0
13281 && IS_BITSIZE (pcrval + 4, 25)
13282 && MATCH (nextopc, addiu_insn)
13283 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13284 && OP16_VALID_REG (OP32_TREG (nextopc)))
13285 {
13286 /* Fix the relocation's type. */
13287 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13288
13289 /* Replace ADDIU with the ADDIUPC version. */
13290 nextopc = (addiupc_insn.match
13291 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13292
d21911ea
MR
13293 bfd_put_micromips_32 (abfd, nextopc,
13294 contents + irel[1].r_offset);
df58fc94
RS
13295 }
13296
13297 /* Can't do anything, give up, sigh... */
13298 else
13299 continue;
13300
13301 /* Fix the relocation's type. */
13302 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13303
13304 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13305 delcnt = 4;
13306 deloff = 0;
13307 }
13308
13309 /* Compact branch relaxation -- due to the multitude of macros
13310 employed by the compiler/assembler, compact branches are not
13311 always generated. Obviously, this can/will be fixed elsewhere,
13312 but there is no drawback in double checking it here. */
13313 else if (r_type == R_MICROMIPS_PC16_S1
13314 && irel->r_offset + 5 < sec->size
13315 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13316 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
833794fc
MR
13317 && ((!insn32
13318 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13319 nop_insn_16) ? 2 : 0))
13320 || (irel->r_offset + 7 < sec->size
13321 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13322 ptr + 4),
13323 nop_insn_32) ? 4 : 0))))
df58fc94
RS
13324 {
13325 unsigned long reg;
13326
13327 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13328
13329 /* Replace BEQZ/BNEZ with the compact version. */
13330 opcode = (bzc_insns_32[fndopc].match
13331 | BZC32_REG_FIELD (reg)
13332 | (opcode & 0xffff)); /* Addend value. */
13333
d21911ea 13334 bfd_put_micromips_32 (abfd, opcode, ptr);
df58fc94 13335
833794fc
MR
13336 /* Delete the delay slot NOP: two or four bytes from
13337 irel->offset + 4; delcnt has already been set above. */
df58fc94
RS
13338 deloff = 4;
13339 }
13340
13341 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
13342 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
13343 else if (!insn32
13344 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
13345 && IS_BITSIZE (pcrval - 2, 11)
13346 && find_match (opcode, b_insns_32) >= 0)
13347 {
13348 /* Fix the relocation's type. */
13349 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13350
a8685210 13351 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
13352 bfd_put_16 (abfd,
13353 (b_insn_16.match
13354 | (opcode & 0x3ff)), /* Addend value. */
2309ddf2 13355 ptr);
df58fc94
RS
13356
13357 /* Delete 2 bytes from irel->r_offset + 2. */
13358 delcnt = 2;
13359 deloff = 2;
13360 }
13361
13362 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
13363 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
13364 else if (!insn32
13365 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
13366 && IS_BITSIZE (pcrval - 2, 8)
13367 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13368 && OP16_VALID_REG (OP32_SREG (opcode)))
13369 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13370 && OP16_VALID_REG (OP32_TREG (opcode)))))
13371 {
13372 unsigned long reg;
13373
13374 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13375
13376 /* Fix the relocation's type. */
13377 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
13378
a8685210 13379 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
13380 bfd_put_16 (abfd,
13381 (bz_insns_16[fndopc].match
13382 | BZ16_REG_FIELD (reg)
13383 | (opcode & 0x7f)), /* Addend value. */
2309ddf2 13384 ptr);
df58fc94
RS
13385
13386 /* Delete 2 bytes from irel->r_offset + 2. */
13387 delcnt = 2;
13388 deloff = 2;
13389 }
13390
13391 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
833794fc
MR
13392 else if (!insn32
13393 && r_type == R_MICROMIPS_26_S1
df58fc94
RS
13394 && target_is_micromips_code_p
13395 && irel->r_offset + 7 < sec->size
13396 && MATCH (opcode, jal_insn_32_bd32))
13397 {
13398 unsigned long n32opc;
13399 bfd_boolean relaxed = FALSE;
13400
d21911ea 13401 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
df58fc94
RS
13402
13403 if (MATCH (n32opc, nop_insn_32))
13404 {
13405 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
2309ddf2 13406 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
df58fc94
RS
13407
13408 relaxed = TRUE;
13409 }
13410 else if (find_match (n32opc, move_insns_32) >= 0)
13411 {
13412 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
13413 bfd_put_16 (abfd,
13414 (move_insn_16.match
13415 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
13416 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
2309ddf2 13417 ptr + 4);
df58fc94
RS
13418
13419 relaxed = TRUE;
13420 }
13421 /* Other 32-bit instructions relaxable to 16-bit
13422 instructions will be handled here later. */
13423
13424 if (relaxed)
13425 {
13426 /* JAL with 32-bit delay slot that is changed to a JALS
13427 with 16-bit delay slot. */
d21911ea 13428 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
df58fc94
RS
13429
13430 /* Delete 2 bytes from irel->r_offset + 6. */
13431 delcnt = 2;
13432 deloff = 6;
13433 }
13434 }
13435
13436 if (delcnt != 0)
13437 {
13438 /* Note that we've changed the relocs, section contents, etc. */
13439 elf_section_data (sec)->relocs = internal_relocs;
13440 elf_section_data (sec)->this_hdr.contents = contents;
13441 symtab_hdr->contents = (unsigned char *) isymbuf;
13442
13443 /* Delete bytes depending on the delcnt and deloff. */
13444 if (!mips_elf_relax_delete_bytes (abfd, sec,
13445 irel->r_offset + deloff, delcnt))
13446 goto error_return;
13447
13448 /* That will change things, so we should relax again.
13449 Note that this is not required, and it may be slow. */
13450 *again = TRUE;
13451 }
13452 }
13453
13454 if (isymbuf != NULL
13455 && symtab_hdr->contents != (unsigned char *) isymbuf)
13456 {
13457 if (! link_info->keep_memory)
13458 free (isymbuf);
13459 else
13460 {
13461 /* Cache the symbols for elf_link_input_bfd. */
13462 symtab_hdr->contents = (unsigned char *) isymbuf;
13463 }
13464 }
13465
13466 if (contents != NULL
13467 && elf_section_data (sec)->this_hdr.contents != contents)
13468 {
13469 if (! link_info->keep_memory)
13470 free (contents);
13471 else
13472 {
13473 /* Cache the section contents for elf_link_input_bfd. */
13474 elf_section_data (sec)->this_hdr.contents = contents;
13475 }
13476 }
13477
13478 if (internal_relocs != NULL
13479 && elf_section_data (sec)->relocs != internal_relocs)
13480 free (internal_relocs);
13481
13482 return TRUE;
13483
13484 error_return:
13485 if (isymbuf != NULL
13486 && symtab_hdr->contents != (unsigned char *) isymbuf)
13487 free (isymbuf);
13488 if (contents != NULL
13489 && elf_section_data (sec)->this_hdr.contents != contents)
13490 free (contents);
13491 if (internal_relocs != NULL
13492 && elf_section_data (sec)->relocs != internal_relocs)
13493 free (internal_relocs);
13494
13495 return FALSE;
13496}
13497\f
b49e97c9
TS
13498/* Create a MIPS ELF linker hash table. */
13499
13500struct bfd_link_hash_table *
9719ad41 13501_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
13502{
13503 struct mips_elf_link_hash_table *ret;
13504 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
13505
7bf52ea2 13506 ret = bfd_zmalloc (amt);
9719ad41 13507 if (ret == NULL)
b49e97c9
TS
13508 return NULL;
13509
66eb6687
AM
13510 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
13511 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
13512 sizeof (struct mips_elf_link_hash_entry),
13513 MIPS_ELF_DATA))
b49e97c9 13514 {
e2d34d7d 13515 free (ret);
b49e97c9
TS
13516 return NULL;
13517 }
1bbce132
MR
13518 ret->root.init_plt_refcount.plist = NULL;
13519 ret->root.init_plt_offset.plist = NULL;
b49e97c9 13520
b49e97c9
TS
13521 return &ret->root.root;
13522}
0a44bf69
RS
13523
13524/* Likewise, but indicate that the target is VxWorks. */
13525
13526struct bfd_link_hash_table *
13527_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
13528{
13529 struct bfd_link_hash_table *ret;
13530
13531 ret = _bfd_mips_elf_link_hash_table_create (abfd);
13532 if (ret)
13533 {
13534 struct mips_elf_link_hash_table *htab;
13535
13536 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
13537 htab->use_plts_and_copy_relocs = TRUE;
13538 htab->is_vxworks = TRUE;
0a44bf69
RS
13539 }
13540 return ret;
13541}
861fb55a
DJ
13542
13543/* A function that the linker calls if we are allowed to use PLTs
13544 and copy relocs. */
13545
13546void
13547_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
13548{
13549 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
13550}
833794fc
MR
13551
13552/* A function that the linker calls to select between all or only
13553 32-bit microMIPS instructions. */
13554
13555void
13556_bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
13557{
13558 mips_elf_hash_table (info)->insn32 = on;
13559}
b49e97c9
TS
13560\f
13561/* We need to use a special link routine to handle the .reginfo and
13562 the .mdebug sections. We need to merge all instances of these
13563 sections together, not write them all out sequentially. */
13564
b34976b6 13565bfd_boolean
9719ad41 13566_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 13567{
b49e97c9
TS
13568 asection *o;
13569 struct bfd_link_order *p;
13570 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
13571 asection *rtproc_sec;
13572 Elf32_RegInfo reginfo;
13573 struct ecoff_debug_info debug;
861fb55a 13574 struct mips_htab_traverse_info hti;
7a2a6943
NC
13575 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13576 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 13577 HDRR *symhdr = &debug.symbolic_header;
9719ad41 13578 void *mdebug_handle = NULL;
b49e97c9
TS
13579 asection *s;
13580 EXTR esym;
13581 unsigned int i;
13582 bfd_size_type amt;
0a44bf69 13583 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
13584
13585 static const char * const secname[] =
13586 {
13587 ".text", ".init", ".fini", ".data",
13588 ".rodata", ".sdata", ".sbss", ".bss"
13589 };
13590 static const int sc[] =
13591 {
13592 scText, scInit, scFini, scData,
13593 scRData, scSData, scSBss, scBss
13594 };
13595
d4596a51
RS
13596 /* Sort the dynamic symbols so that those with GOT entries come after
13597 those without. */
0a44bf69 13598 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
13599 BFD_ASSERT (htab != NULL);
13600
d4596a51
RS
13601 if (!mips_elf_sort_hash_table (abfd, info))
13602 return FALSE;
b49e97c9 13603
861fb55a
DJ
13604 /* Create any scheduled LA25 stubs. */
13605 hti.info = info;
13606 hti.output_bfd = abfd;
13607 hti.error = FALSE;
13608 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
13609 if (hti.error)
13610 return FALSE;
13611
b49e97c9
TS
13612 /* Get a value for the GP register. */
13613 if (elf_gp (abfd) == 0)
13614 {
13615 struct bfd_link_hash_entry *h;
13616
b34976b6 13617 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 13618 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
13619 elf_gp (abfd) = (h->u.def.value
13620 + h->u.def.section->output_section->vma
13621 + h->u.def.section->output_offset);
0a44bf69
RS
13622 else if (htab->is_vxworks
13623 && (h = bfd_link_hash_lookup (info->hash,
13624 "_GLOBAL_OFFSET_TABLE_",
13625 FALSE, FALSE, TRUE))
13626 && h->type == bfd_link_hash_defined)
13627 elf_gp (abfd) = (h->u.def.section->output_section->vma
13628 + h->u.def.section->output_offset
13629 + h->u.def.value);
1049f94e 13630 else if (info->relocatable)
b49e97c9
TS
13631 {
13632 bfd_vma lo = MINUS_ONE;
13633
13634 /* Find the GP-relative section with the lowest offset. */
9719ad41 13635 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
13636 if (o->vma < lo
13637 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
13638 lo = o->vma;
13639
13640 /* And calculate GP relative to that. */
0a44bf69 13641 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
13642 }
13643 else
13644 {
13645 /* If the relocate_section function needs to do a reloc
13646 involving the GP value, it should make a reloc_dangerous
13647 callback to warn that GP is not defined. */
13648 }
13649 }
13650
13651 /* Go through the sections and collect the .reginfo and .mdebug
13652 information. */
13653 reginfo_sec = NULL;
13654 mdebug_sec = NULL;
13655 gptab_data_sec = NULL;
13656 gptab_bss_sec = NULL;
9719ad41 13657 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
13658 {
13659 if (strcmp (o->name, ".reginfo") == 0)
13660 {
13661 memset (&reginfo, 0, sizeof reginfo);
13662
13663 /* We have found the .reginfo section in the output file.
13664 Look through all the link_orders comprising it and merge
13665 the information together. */
8423293d 13666 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13667 {
13668 asection *input_section;
13669 bfd *input_bfd;
13670 Elf32_External_RegInfo ext;
13671 Elf32_RegInfo sub;
13672
13673 if (p->type != bfd_indirect_link_order)
13674 {
13675 if (p->type == bfd_data_link_order)
13676 continue;
13677 abort ();
13678 }
13679
13680 input_section = p->u.indirect.section;
13681 input_bfd = input_section->owner;
13682
b49e97c9 13683 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 13684 &ext, 0, sizeof ext))
b34976b6 13685 return FALSE;
b49e97c9
TS
13686
13687 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
13688
13689 reginfo.ri_gprmask |= sub.ri_gprmask;
13690 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
13691 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
13692 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
13693 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
13694
13695 /* ri_gp_value is set by the function
13696 mips_elf32_section_processing when the section is
13697 finally written out. */
13698
13699 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13700 elf_link_input_bfd ignores this section. */
13701 input_section->flags &= ~SEC_HAS_CONTENTS;
13702 }
13703
13704 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 13705 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
13706
13707 /* Skip this section later on (I don't think this currently
13708 matters, but someday it might). */
8423293d 13709 o->map_head.link_order = NULL;
b49e97c9
TS
13710
13711 reginfo_sec = o;
13712 }
13713
13714 if (strcmp (o->name, ".mdebug") == 0)
13715 {
13716 struct extsym_info einfo;
13717 bfd_vma last;
13718
13719 /* We have found the .mdebug section in the output file.
13720 Look through all the link_orders comprising it and merge
13721 the information together. */
13722 symhdr->magic = swap->sym_magic;
13723 /* FIXME: What should the version stamp be? */
13724 symhdr->vstamp = 0;
13725 symhdr->ilineMax = 0;
13726 symhdr->cbLine = 0;
13727 symhdr->idnMax = 0;
13728 symhdr->ipdMax = 0;
13729 symhdr->isymMax = 0;
13730 symhdr->ioptMax = 0;
13731 symhdr->iauxMax = 0;
13732 symhdr->issMax = 0;
13733 symhdr->issExtMax = 0;
13734 symhdr->ifdMax = 0;
13735 symhdr->crfd = 0;
13736 symhdr->iextMax = 0;
13737
13738 /* We accumulate the debugging information itself in the
13739 debug_info structure. */
13740 debug.line = NULL;
13741 debug.external_dnr = NULL;
13742 debug.external_pdr = NULL;
13743 debug.external_sym = NULL;
13744 debug.external_opt = NULL;
13745 debug.external_aux = NULL;
13746 debug.ss = NULL;
13747 debug.ssext = debug.ssext_end = NULL;
13748 debug.external_fdr = NULL;
13749 debug.external_rfd = NULL;
13750 debug.external_ext = debug.external_ext_end = NULL;
13751
13752 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 13753 if (mdebug_handle == NULL)
b34976b6 13754 return FALSE;
b49e97c9
TS
13755
13756 esym.jmptbl = 0;
13757 esym.cobol_main = 0;
13758 esym.weakext = 0;
13759 esym.reserved = 0;
13760 esym.ifd = ifdNil;
13761 esym.asym.iss = issNil;
13762 esym.asym.st = stLocal;
13763 esym.asym.reserved = 0;
13764 esym.asym.index = indexNil;
13765 last = 0;
13766 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13767 {
13768 esym.asym.sc = sc[i];
13769 s = bfd_get_section_by_name (abfd, secname[i]);
13770 if (s != NULL)
13771 {
13772 esym.asym.value = s->vma;
eea6121a 13773 last = s->vma + s->size;
b49e97c9
TS
13774 }
13775 else
13776 esym.asym.value = last;
13777 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13778 secname[i], &esym))
b34976b6 13779 return FALSE;
b49e97c9
TS
13780 }
13781
8423293d 13782 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13783 {
13784 asection *input_section;
13785 bfd *input_bfd;
13786 const struct ecoff_debug_swap *input_swap;
13787 struct ecoff_debug_info input_debug;
13788 char *eraw_src;
13789 char *eraw_end;
13790
13791 if (p->type != bfd_indirect_link_order)
13792 {
13793 if (p->type == bfd_data_link_order)
13794 continue;
13795 abort ();
13796 }
13797
13798 input_section = p->u.indirect.section;
13799 input_bfd = input_section->owner;
13800
d5eaccd7 13801 if (!is_mips_elf (input_bfd))
b49e97c9
TS
13802 {
13803 /* I don't know what a non MIPS ELF bfd would be
13804 doing with a .mdebug section, but I don't really
13805 want to deal with it. */
13806 continue;
13807 }
13808
13809 input_swap = (get_elf_backend_data (input_bfd)
13810 ->elf_backend_ecoff_debug_swap);
13811
eea6121a 13812 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
13813
13814 /* The ECOFF linking code expects that we have already
13815 read in the debugging information and set up an
13816 ecoff_debug_info structure, so we do that now. */
13817 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13818 &input_debug))
b34976b6 13819 return FALSE;
b49e97c9
TS
13820
13821 if (! (bfd_ecoff_debug_accumulate
13822 (mdebug_handle, abfd, &debug, swap, input_bfd,
13823 &input_debug, input_swap, info)))
b34976b6 13824 return FALSE;
b49e97c9
TS
13825
13826 /* Loop through the external symbols. For each one with
13827 interesting information, try to find the symbol in
13828 the linker global hash table and save the information
13829 for the output external symbols. */
13830 eraw_src = input_debug.external_ext;
13831 eraw_end = (eraw_src
13832 + (input_debug.symbolic_header.iextMax
13833 * input_swap->external_ext_size));
13834 for (;
13835 eraw_src < eraw_end;
13836 eraw_src += input_swap->external_ext_size)
13837 {
13838 EXTR ext;
13839 const char *name;
13840 struct mips_elf_link_hash_entry *h;
13841
9719ad41 13842 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
13843 if (ext.asym.sc == scNil
13844 || ext.asym.sc == scUndefined
13845 || ext.asym.sc == scSUndefined)
13846 continue;
13847
13848 name = input_debug.ssext + ext.asym.iss;
13849 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 13850 name, FALSE, FALSE, TRUE);
b49e97c9
TS
13851 if (h == NULL || h->esym.ifd != -2)
13852 continue;
13853
13854 if (ext.ifd != -1)
13855 {
13856 BFD_ASSERT (ext.ifd
13857 < input_debug.symbolic_header.ifdMax);
13858 ext.ifd = input_debug.ifdmap[ext.ifd];
13859 }
13860
13861 h->esym = ext;
13862 }
13863
13864 /* Free up the information we just read. */
13865 free (input_debug.line);
13866 free (input_debug.external_dnr);
13867 free (input_debug.external_pdr);
13868 free (input_debug.external_sym);
13869 free (input_debug.external_opt);
13870 free (input_debug.external_aux);
13871 free (input_debug.ss);
13872 free (input_debug.ssext);
13873 free (input_debug.external_fdr);
13874 free (input_debug.external_rfd);
13875 free (input_debug.external_ext);
13876
13877 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13878 elf_link_input_bfd ignores this section. */
13879 input_section->flags &= ~SEC_HAS_CONTENTS;
13880 }
13881
13882 if (SGI_COMPAT (abfd) && info->shared)
13883 {
13884 /* Create .rtproc section. */
87e0a731 13885 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
b49e97c9
TS
13886 if (rtproc_sec == NULL)
13887 {
13888 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13889 | SEC_LINKER_CREATED | SEC_READONLY);
13890
87e0a731
AM
13891 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
13892 ".rtproc",
13893 flags);
b49e97c9 13894 if (rtproc_sec == NULL
b49e97c9 13895 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 13896 return FALSE;
b49e97c9
TS
13897 }
13898
13899 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13900 info, rtproc_sec,
13901 &debug))
b34976b6 13902 return FALSE;
b49e97c9
TS
13903 }
13904
13905 /* Build the external symbol information. */
13906 einfo.abfd = abfd;
13907 einfo.info = info;
13908 einfo.debug = &debug;
13909 einfo.swap = swap;
b34976b6 13910 einfo.failed = FALSE;
b49e97c9 13911 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 13912 mips_elf_output_extsym, &einfo);
b49e97c9 13913 if (einfo.failed)
b34976b6 13914 return FALSE;
b49e97c9
TS
13915
13916 /* Set the size of the .mdebug section. */
eea6121a 13917 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
13918
13919 /* Skip this section later on (I don't think this currently
13920 matters, but someday it might). */
8423293d 13921 o->map_head.link_order = NULL;
b49e97c9
TS
13922
13923 mdebug_sec = o;
13924 }
13925
0112cd26 13926 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
13927 {
13928 const char *subname;
13929 unsigned int c;
13930 Elf32_gptab *tab;
13931 Elf32_External_gptab *ext_tab;
13932 unsigned int j;
13933
13934 /* The .gptab.sdata and .gptab.sbss sections hold
13935 information describing how the small data area would
13936 change depending upon the -G switch. These sections
13937 not used in executables files. */
1049f94e 13938 if (! info->relocatable)
b49e97c9 13939 {
8423293d 13940 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13941 {
13942 asection *input_section;
13943
13944 if (p->type != bfd_indirect_link_order)
13945 {
13946 if (p->type == bfd_data_link_order)
13947 continue;
13948 abort ();
13949 }
13950
13951 input_section = p->u.indirect.section;
13952
13953 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13954 elf_link_input_bfd ignores this section. */
13955 input_section->flags &= ~SEC_HAS_CONTENTS;
13956 }
13957
13958 /* Skip this section later on (I don't think this
13959 currently matters, but someday it might). */
8423293d 13960 o->map_head.link_order = NULL;
b49e97c9
TS
13961
13962 /* Really remove the section. */
5daa8fe7 13963 bfd_section_list_remove (abfd, o);
b49e97c9
TS
13964 --abfd->section_count;
13965
13966 continue;
13967 }
13968
13969 /* There is one gptab for initialized data, and one for
13970 uninitialized data. */
13971 if (strcmp (o->name, ".gptab.sdata") == 0)
13972 gptab_data_sec = o;
13973 else if (strcmp (o->name, ".gptab.sbss") == 0)
13974 gptab_bss_sec = o;
13975 else
13976 {
13977 (*_bfd_error_handler)
13978 (_("%s: illegal section name `%s'"),
13979 bfd_get_filename (abfd), o->name);
13980 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 13981 return FALSE;
b49e97c9
TS
13982 }
13983
13984 /* The linker script always combines .gptab.data and
13985 .gptab.sdata into .gptab.sdata, and likewise for
13986 .gptab.bss and .gptab.sbss. It is possible that there is
13987 no .sdata or .sbss section in the output file, in which
13988 case we must change the name of the output section. */
13989 subname = o->name + sizeof ".gptab" - 1;
13990 if (bfd_get_section_by_name (abfd, subname) == NULL)
13991 {
13992 if (o == gptab_data_sec)
13993 o->name = ".gptab.data";
13994 else
13995 o->name = ".gptab.bss";
13996 subname = o->name + sizeof ".gptab" - 1;
13997 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
13998 }
13999
14000 /* Set up the first entry. */
14001 c = 1;
14002 amt = c * sizeof (Elf32_gptab);
9719ad41 14003 tab = bfd_malloc (amt);
b49e97c9 14004 if (tab == NULL)
b34976b6 14005 return FALSE;
b49e97c9
TS
14006 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14007 tab[0].gt_header.gt_unused = 0;
14008
14009 /* Combine the input sections. */
8423293d 14010 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14011 {
14012 asection *input_section;
14013 bfd *input_bfd;
14014 bfd_size_type size;
14015 unsigned long last;
14016 bfd_size_type gpentry;
14017
14018 if (p->type != bfd_indirect_link_order)
14019 {
14020 if (p->type == bfd_data_link_order)
14021 continue;
14022 abort ();
14023 }
14024
14025 input_section = p->u.indirect.section;
14026 input_bfd = input_section->owner;
14027
14028 /* Combine the gptab entries for this input section one
14029 by one. We know that the input gptab entries are
14030 sorted by ascending -G value. */
eea6121a 14031 size = input_section->size;
b49e97c9
TS
14032 last = 0;
14033 for (gpentry = sizeof (Elf32_External_gptab);
14034 gpentry < size;
14035 gpentry += sizeof (Elf32_External_gptab))
14036 {
14037 Elf32_External_gptab ext_gptab;
14038 Elf32_gptab int_gptab;
14039 unsigned long val;
14040 unsigned long add;
b34976b6 14041 bfd_boolean exact;
b49e97c9
TS
14042 unsigned int look;
14043
14044 if (! (bfd_get_section_contents
9719ad41
RS
14045 (input_bfd, input_section, &ext_gptab, gpentry,
14046 sizeof (Elf32_External_gptab))))
b49e97c9
TS
14047 {
14048 free (tab);
b34976b6 14049 return FALSE;
b49e97c9
TS
14050 }
14051
14052 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
14053 &int_gptab);
14054 val = int_gptab.gt_entry.gt_g_value;
14055 add = int_gptab.gt_entry.gt_bytes - last;
14056
b34976b6 14057 exact = FALSE;
b49e97c9
TS
14058 for (look = 1; look < c; look++)
14059 {
14060 if (tab[look].gt_entry.gt_g_value >= val)
14061 tab[look].gt_entry.gt_bytes += add;
14062
14063 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 14064 exact = TRUE;
b49e97c9
TS
14065 }
14066
14067 if (! exact)
14068 {
14069 Elf32_gptab *new_tab;
14070 unsigned int max;
14071
14072 /* We need a new table entry. */
14073 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 14074 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
14075 if (new_tab == NULL)
14076 {
14077 free (tab);
b34976b6 14078 return FALSE;
b49e97c9
TS
14079 }
14080 tab = new_tab;
14081 tab[c].gt_entry.gt_g_value = val;
14082 tab[c].gt_entry.gt_bytes = add;
14083
14084 /* Merge in the size for the next smallest -G
14085 value, since that will be implied by this new
14086 value. */
14087 max = 0;
14088 for (look = 1; look < c; look++)
14089 {
14090 if (tab[look].gt_entry.gt_g_value < val
14091 && (max == 0
14092 || (tab[look].gt_entry.gt_g_value
14093 > tab[max].gt_entry.gt_g_value)))
14094 max = look;
14095 }
14096 if (max != 0)
14097 tab[c].gt_entry.gt_bytes +=
14098 tab[max].gt_entry.gt_bytes;
14099
14100 ++c;
14101 }
14102
14103 last = int_gptab.gt_entry.gt_bytes;
14104 }
14105
14106 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14107 elf_link_input_bfd ignores this section. */
14108 input_section->flags &= ~SEC_HAS_CONTENTS;
14109 }
14110
14111 /* The table must be sorted by -G value. */
14112 if (c > 2)
14113 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
14114
14115 /* Swap out the table. */
14116 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 14117 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
14118 if (ext_tab == NULL)
14119 {
14120 free (tab);
b34976b6 14121 return FALSE;
b49e97c9
TS
14122 }
14123
14124 for (j = 0; j < c; j++)
14125 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
14126 free (tab);
14127
eea6121a 14128 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
14129 o->contents = (bfd_byte *) ext_tab;
14130
14131 /* Skip this section later on (I don't think this currently
14132 matters, but someday it might). */
8423293d 14133 o->map_head.link_order = NULL;
b49e97c9
TS
14134 }
14135 }
14136
14137 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 14138 if (!bfd_elf_final_link (abfd, info))
b34976b6 14139 return FALSE;
b49e97c9
TS
14140
14141 /* Now write out the computed sections. */
14142
9719ad41 14143 if (reginfo_sec != NULL)
b49e97c9
TS
14144 {
14145 Elf32_External_RegInfo ext;
14146
14147 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 14148 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 14149 return FALSE;
b49e97c9
TS
14150 }
14151
9719ad41 14152 if (mdebug_sec != NULL)
b49e97c9
TS
14153 {
14154 BFD_ASSERT (abfd->output_has_begun);
14155 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
14156 swap, info,
14157 mdebug_sec->filepos))
b34976b6 14158 return FALSE;
b49e97c9
TS
14159
14160 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
14161 }
14162
9719ad41 14163 if (gptab_data_sec != NULL)
b49e97c9
TS
14164 {
14165 if (! bfd_set_section_contents (abfd, gptab_data_sec,
14166 gptab_data_sec->contents,
eea6121a 14167 0, gptab_data_sec->size))
b34976b6 14168 return FALSE;
b49e97c9
TS
14169 }
14170
9719ad41 14171 if (gptab_bss_sec != NULL)
b49e97c9
TS
14172 {
14173 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
14174 gptab_bss_sec->contents,
eea6121a 14175 0, gptab_bss_sec->size))
b34976b6 14176 return FALSE;
b49e97c9
TS
14177 }
14178
14179 if (SGI_COMPAT (abfd))
14180 {
14181 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
14182 if (rtproc_sec != NULL)
14183 {
14184 if (! bfd_set_section_contents (abfd, rtproc_sec,
14185 rtproc_sec->contents,
eea6121a 14186 0, rtproc_sec->size))
b34976b6 14187 return FALSE;
b49e97c9
TS
14188 }
14189 }
14190
b34976b6 14191 return TRUE;
b49e97c9
TS
14192}
14193\f
64543e1a
RS
14194/* Structure for saying that BFD machine EXTENSION extends BASE. */
14195
a253d456
NC
14196struct mips_mach_extension
14197{
64543e1a
RS
14198 unsigned long extension, base;
14199};
14200
14201
14202/* An array describing how BFD machines relate to one another. The entries
14203 are ordered topologically with MIPS I extensions listed last. */
14204
a253d456
NC
14205static const struct mips_mach_extension mips_mach_extensions[] =
14206{
6f179bd0 14207 /* MIPS64r2 extensions. */
432233b3 14208 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
dd6a37e7 14209 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
6f179bd0
AN
14210 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14211
64543e1a 14212 /* MIPS64 extensions. */
5f74bc13 14213 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 14214 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 14215 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
fd503541 14216 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
64543e1a
RS
14217
14218 /* MIPS V extensions. */
14219 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14220
14221 /* R10000 extensions. */
14222 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
14223 { bfd_mach_mips14000, bfd_mach_mips10000 },
14224 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
14225
14226 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14227 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14228 better to allow vr5400 and vr5500 code to be merged anyway, since
14229 many libraries will just use the core ISA. Perhaps we could add
14230 some sort of ASE flag if this ever proves a problem. */
14231 { bfd_mach_mips5500, bfd_mach_mips5400 },
14232 { bfd_mach_mips5400, bfd_mach_mips5000 },
14233
14234 /* MIPS IV extensions. */
14235 { bfd_mach_mips5, bfd_mach_mips8000 },
14236 { bfd_mach_mips10000, bfd_mach_mips8000 },
14237 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 14238 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 14239 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
14240
14241 /* VR4100 extensions. */
14242 { bfd_mach_mips4120, bfd_mach_mips4100 },
14243 { bfd_mach_mips4111, bfd_mach_mips4100 },
14244
14245 /* MIPS III extensions. */
350cc38d
MS
14246 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14247 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
14248 { bfd_mach_mips8000, bfd_mach_mips4000 },
14249 { bfd_mach_mips4650, bfd_mach_mips4000 },
14250 { bfd_mach_mips4600, bfd_mach_mips4000 },
14251 { bfd_mach_mips4400, bfd_mach_mips4000 },
14252 { bfd_mach_mips4300, bfd_mach_mips4000 },
14253 { bfd_mach_mips4100, bfd_mach_mips4000 },
14254 { bfd_mach_mips4010, bfd_mach_mips4000 },
e407c74b 14255 { bfd_mach_mips5900, bfd_mach_mips4000 },
64543e1a
RS
14256
14257 /* MIPS32 extensions. */
14258 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14259
14260 /* MIPS II extensions. */
14261 { bfd_mach_mips4000, bfd_mach_mips6000 },
14262 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14263
14264 /* MIPS I extensions. */
14265 { bfd_mach_mips6000, bfd_mach_mips3000 },
14266 { bfd_mach_mips3900, bfd_mach_mips3000 }
14267};
14268
14269
14270/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14271
14272static bfd_boolean
9719ad41 14273mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
14274{
14275 size_t i;
14276
c5211a54
RS
14277 if (extension == base)
14278 return TRUE;
14279
14280 if (base == bfd_mach_mipsisa32
14281 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14282 return TRUE;
14283
14284 if (base == bfd_mach_mipsisa32r2
14285 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14286 return TRUE;
14287
14288 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 14289 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
14290 {
14291 extension = mips_mach_extensions[i].base;
14292 if (extension == base)
14293 return TRUE;
14294 }
64543e1a 14295
c5211a54 14296 return FALSE;
64543e1a
RS
14297}
14298
14299
14300/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 14301
b34976b6 14302static bfd_boolean
9719ad41 14303mips_32bit_flags_p (flagword flags)
00707a0e 14304{
64543e1a
RS
14305 return ((flags & EF_MIPS_32BITMODE) != 0
14306 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14307 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14308 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14309 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14310 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14311 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
14312}
14313
64543e1a 14314
2cf19d5c
JM
14315/* Merge object attributes from IBFD into OBFD. Raise an error if
14316 there are conflicting attributes. */
14317static bfd_boolean
14318mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
14319{
14320 obj_attribute *in_attr;
14321 obj_attribute *out_attr;
6ae68ba3 14322 bfd *abi_fp_bfd;
b60bf9be 14323 bfd *abi_msa_bfd;
6ae68ba3
MR
14324
14325 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
14326 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
d929bc19 14327 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
6ae68ba3 14328 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
2cf19d5c 14329
b60bf9be
CF
14330 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
14331 if (!abi_msa_bfd
14332 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14333 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
14334
2cf19d5c
JM
14335 if (!elf_known_obj_attributes_proc (obfd)[0].i)
14336 {
14337 /* This is the first object. Copy the attributes. */
14338 _bfd_elf_copy_obj_attributes (ibfd, obfd);
14339
14340 /* Use the Tag_null value to indicate the attributes have been
14341 initialized. */
14342 elf_known_obj_attributes_proc (obfd)[0].i = 1;
14343
14344 return TRUE;
14345 }
14346
14347 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
14348 non-conflicting ones. */
2cf19d5c
JM
14349 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14350 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
14351 {
14352 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
d929bc19 14353 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
2cf19d5c 14354 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
d929bc19 14355 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
2cf19d5c
JM
14356 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
14357 {
d929bc19 14358 case Val_GNU_MIPS_ABI_FP_DOUBLE:
2cf19d5c
JM
14359 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14360 {
d929bc19 14361 case Val_GNU_MIPS_ABI_FP_SINGLE:
2cf19d5c 14362 _bfd_error_handler
6ae68ba3
MR
14363 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14364 obfd, abi_fp_bfd, ibfd, "-mdouble-float", "-msingle-float");
51a0dd31 14365 break;
2cf19d5c 14366
d929bc19 14367 case Val_GNU_MIPS_ABI_FP_SOFT:
2cf19d5c 14368 _bfd_error_handler
6ae68ba3
MR
14369 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14370 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
2cf19d5c
JM
14371 break;
14372
d929bc19 14373 case Val_GNU_MIPS_ABI_FP_64:
42554f6a 14374 _bfd_error_handler
6ae68ba3
MR
14375 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14376 obfd, abi_fp_bfd, ibfd,
14377 "-mdouble-float", "-mips32r2 -mfp64");
42554f6a
TS
14378 break;
14379
2cf19d5c 14380 default:
6ae68ba3
MR
14381 _bfd_error_handler
14382 (_("Warning: %B uses %s (set by %B), "
14383 "%B uses unknown floating point ABI %d"),
14384 obfd, abi_fp_bfd, ibfd,
14385 "-mdouble-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14386 break;
2cf19d5c
JM
14387 }
14388 break;
14389
d929bc19 14390 case Val_GNU_MIPS_ABI_FP_SINGLE:
2cf19d5c
JM
14391 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14392 {
d929bc19 14393 case Val_GNU_MIPS_ABI_FP_DOUBLE:
2cf19d5c 14394 _bfd_error_handler
6ae68ba3
MR
14395 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14396 obfd, abi_fp_bfd, ibfd, "-msingle-float", "-mdouble-float");
51a0dd31 14397 break;
2cf19d5c 14398
d929bc19 14399 case Val_GNU_MIPS_ABI_FP_SOFT:
2cf19d5c 14400 _bfd_error_handler
6ae68ba3
MR
14401 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14402 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
2cf19d5c
JM
14403 break;
14404
d929bc19 14405 case Val_GNU_MIPS_ABI_FP_64:
42554f6a 14406 _bfd_error_handler
6ae68ba3
MR
14407 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14408 obfd, abi_fp_bfd, ibfd,
14409 "-msingle-float", "-mips32r2 -mfp64");
42554f6a
TS
14410 break;
14411
2cf19d5c 14412 default:
6ae68ba3
MR
14413 _bfd_error_handler
14414 (_("Warning: %B uses %s (set by %B), "
14415 "%B uses unknown floating point ABI %d"),
14416 obfd, abi_fp_bfd, ibfd,
14417 "-msingle-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14418 break;
2cf19d5c
JM
14419 }
14420 break;
14421
d929bc19 14422 case Val_GNU_MIPS_ABI_FP_SOFT:
2cf19d5c
JM
14423 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14424 {
d929bc19
MR
14425 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14426 case Val_GNU_MIPS_ABI_FP_SINGLE:
14427 case Val_GNU_MIPS_ABI_FP_64:
2cf19d5c 14428 _bfd_error_handler
6ae68ba3
MR
14429 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14430 obfd, abi_fp_bfd, ibfd, "-msoft-float", "-mhard-float");
2cf19d5c
JM
14431 break;
14432
14433 default:
6ae68ba3
MR
14434 _bfd_error_handler
14435 (_("Warning: %B uses %s (set by %B), "
14436 "%B uses unknown floating point ABI %d"),
14437 obfd, abi_fp_bfd, ibfd,
14438 "-msoft-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14439 break;
2cf19d5c
JM
14440 }
14441 break;
14442
d929bc19 14443 case Val_GNU_MIPS_ABI_FP_64:
42554f6a
TS
14444 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14445 {
d929bc19 14446 case Val_GNU_MIPS_ABI_FP_DOUBLE:
42554f6a 14447 _bfd_error_handler
6ae68ba3
MR
14448 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14449 obfd, abi_fp_bfd, ibfd,
14450 "-mips32r2 -mfp64", "-mdouble-float");
42554f6a
TS
14451 break;
14452
d929bc19 14453 case Val_GNU_MIPS_ABI_FP_SINGLE:
42554f6a 14454 _bfd_error_handler
6ae68ba3
MR
14455 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14456 obfd, abi_fp_bfd, ibfd,
14457 "-mips32r2 -mfp64", "-msingle-float");
42554f6a
TS
14458 break;
14459
d929bc19 14460 case Val_GNU_MIPS_ABI_FP_SOFT:
42554f6a 14461 _bfd_error_handler
6ae68ba3
MR
14462 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14463 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
42554f6a
TS
14464 break;
14465
14466 default:
6ae68ba3
MR
14467 _bfd_error_handler
14468 (_("Warning: %B uses %s (set by %B), "
14469 "%B uses unknown floating point ABI %d"),
14470 obfd, abi_fp_bfd, ibfd,
14471 "-mips32r2 -mfp64", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14472 break;
42554f6a
TS
14473 }
14474 break;
14475
2cf19d5c 14476 default:
6ae68ba3
MR
14477 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14478 {
d929bc19 14479 case Val_GNU_MIPS_ABI_FP_DOUBLE:
6ae68ba3
MR
14480 _bfd_error_handler
14481 (_("Warning: %B uses unknown floating point ABI %d "
14482 "(set by %B), %B uses %s"),
14483 obfd, abi_fp_bfd, ibfd,
14484 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mdouble-float");
14485 break;
14486
d929bc19 14487 case Val_GNU_MIPS_ABI_FP_SINGLE:
6ae68ba3
MR
14488 _bfd_error_handler
14489 (_("Warning: %B uses unknown floating point ABI %d "
14490 "(set by %B), %B uses %s"),
14491 obfd, abi_fp_bfd, ibfd,
14492 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msingle-float");
14493 break;
14494
d929bc19 14495 case Val_GNU_MIPS_ABI_FP_SOFT:
6ae68ba3
MR
14496 _bfd_error_handler
14497 (_("Warning: %B uses unknown floating point ABI %d "
14498 "(set by %B), %B uses %s"),
14499 obfd, abi_fp_bfd, ibfd,
14500 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msoft-float");
14501 break;
14502
d929bc19 14503 case Val_GNU_MIPS_ABI_FP_64:
6ae68ba3
MR
14504 _bfd_error_handler
14505 (_("Warning: %B uses unknown floating point ABI %d "
14506 "(set by %B), %B uses %s"),
14507 obfd, abi_fp_bfd, ibfd,
14508 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mips32r2 -mfp64");
14509 break;
14510
14511 default:
14512 _bfd_error_handler
14513 (_("Warning: %B uses unknown floating point ABI %d "
14514 "(set by %B), %B uses unknown floating point ABI %d"),
14515 obfd, abi_fp_bfd, ibfd,
14516 out_attr[Tag_GNU_MIPS_ABI_FP].i,
14517 in_attr[Tag_GNU_MIPS_ABI_FP].i);
14518 break;
14519 }
14520 break;
2cf19d5c
JM
14521 }
14522 }
14523
b60bf9be
CF
14524 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
14525 non-conflicting ones. */
14526 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14527 {
14528 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
14529 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
14530 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
14531 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14532 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14533 {
14534 case Val_GNU_MIPS_ABI_MSA_128:
14535 _bfd_error_handler
14536 (_("Warning: %B uses %s (set by %B), "
14537 "%B uses unknown MSA ABI %d"),
14538 obfd, abi_msa_bfd, ibfd,
14539 "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14540 break;
14541
14542 default:
14543 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
14544 {
14545 case Val_GNU_MIPS_ABI_MSA_128:
14546 _bfd_error_handler
14547 (_("Warning: %B uses unknown MSA ABI %d "
14548 "(set by %B), %B uses %s"),
14549 obfd, abi_msa_bfd, ibfd,
14550 out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
14551 break;
14552
14553 default:
14554 _bfd_error_handler
14555 (_("Warning: %B uses unknown MSA ABI %d "
14556 "(set by %B), %B uses unknown MSA ABI %d"),
14557 obfd, abi_msa_bfd, ibfd,
14558 out_attr[Tag_GNU_MIPS_ABI_MSA].i,
14559 in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14560 break;
14561 }
14562 }
14563 }
14564
2cf19d5c
JM
14565 /* Merge Tag_compatibility attributes and any common GNU ones. */
14566 _bfd_elf_merge_object_attributes (ibfd, obfd);
14567
14568 return TRUE;
14569}
14570
b49e97c9
TS
14571/* Merge backend specific data from an object file to the output
14572 object file when linking. */
14573
b34976b6 14574bfd_boolean
9719ad41 14575_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
14576{
14577 flagword old_flags;
14578 flagword new_flags;
b34976b6
AM
14579 bfd_boolean ok;
14580 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
14581 asection *sec;
14582
58238693 14583 /* Check if we have the same endianness. */
82e51918 14584 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
14585 {
14586 (*_bfd_error_handler)
d003868e
AM
14587 (_("%B: endianness incompatible with that of the selected emulation"),
14588 ibfd);
aa701218
AO
14589 return FALSE;
14590 }
b49e97c9 14591
d5eaccd7 14592 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 14593 return TRUE;
b49e97c9 14594
aa701218
AO
14595 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
14596 {
14597 (*_bfd_error_handler)
d003868e
AM
14598 (_("%B: ABI is incompatible with that of the selected emulation"),
14599 ibfd);
aa701218
AO
14600 return FALSE;
14601 }
14602
2cf19d5c
JM
14603 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
14604 return FALSE;
14605
b49e97c9
TS
14606 new_flags = elf_elfheader (ibfd)->e_flags;
14607 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
14608 old_flags = elf_elfheader (obfd)->e_flags;
14609
14610 if (! elf_flags_init (obfd))
14611 {
b34976b6 14612 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
14613 elf_elfheader (obfd)->e_flags = new_flags;
14614 elf_elfheader (obfd)->e_ident[EI_CLASS]
14615 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
14616
14617 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861 14618 && (bfd_get_arch_info (obfd)->the_default
68ffbac6 14619 || mips_mach_extends_p (bfd_get_mach (obfd),
2907b861 14620 bfd_get_mach (ibfd))))
b49e97c9
TS
14621 {
14622 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
14623 bfd_get_mach (ibfd)))
b34976b6 14624 return FALSE;
b49e97c9
TS
14625 }
14626
b34976b6 14627 return TRUE;
b49e97c9
TS
14628 }
14629
14630 /* Check flag compatibility. */
14631
14632 new_flags &= ~EF_MIPS_NOREORDER;
14633 old_flags &= ~EF_MIPS_NOREORDER;
14634
f4416af6
AO
14635 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
14636 doesn't seem to matter. */
14637 new_flags &= ~EF_MIPS_XGOT;
14638 old_flags &= ~EF_MIPS_XGOT;
14639
98a8deaf
RS
14640 /* MIPSpro generates ucode info in n64 objects. Again, we should
14641 just be able to ignore this. */
14642 new_flags &= ~EF_MIPS_UCODE;
14643 old_flags &= ~EF_MIPS_UCODE;
14644
861fb55a
DJ
14645 /* DSOs should only be linked with CPIC code. */
14646 if ((ibfd->flags & DYNAMIC) != 0)
14647 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 14648
b49e97c9 14649 if (new_flags == old_flags)
b34976b6 14650 return TRUE;
b49e97c9
TS
14651
14652 /* Check to see if the input BFD actually contains any sections.
14653 If not, its flags may not have been initialised either, but it cannot
14654 actually cause any incompatibility. */
14655 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
14656 {
14657 /* Ignore synthetic sections and empty .text, .data and .bss sections
ed88c97e
RS
14658 which are automatically generated by gas. Also ignore fake
14659 (s)common sections, since merely defining a common symbol does
14660 not affect compatibility. */
14661 if ((sec->flags & SEC_IS_COMMON) == 0
14662 && strcmp (sec->name, ".reginfo")
b49e97c9 14663 && strcmp (sec->name, ".mdebug")
eea6121a 14664 && (sec->size != 0
d13d89fa
NS
14665 || (strcmp (sec->name, ".text")
14666 && strcmp (sec->name, ".data")
14667 && strcmp (sec->name, ".bss"))))
b49e97c9 14668 {
b34976b6 14669 null_input_bfd = FALSE;
b49e97c9
TS
14670 break;
14671 }
14672 }
14673 if (null_input_bfd)
b34976b6 14674 return TRUE;
b49e97c9 14675
b34976b6 14676 ok = TRUE;
b49e97c9 14677
143d77c5
EC
14678 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
14679 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 14680 {
b49e97c9 14681 (*_bfd_error_handler)
861fb55a 14682 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 14683 ibfd);
143d77c5 14684 ok = TRUE;
b49e97c9
TS
14685 }
14686
143d77c5
EC
14687 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
14688 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
14689 if (! (new_flags & EF_MIPS_PIC))
14690 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
14691
14692 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14693 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 14694
64543e1a
RS
14695 /* Compare the ISAs. */
14696 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 14697 {
64543e1a 14698 (*_bfd_error_handler)
d003868e
AM
14699 (_("%B: linking 32-bit code with 64-bit code"),
14700 ibfd);
64543e1a
RS
14701 ok = FALSE;
14702 }
14703 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
14704 {
14705 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
14706 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 14707 {
64543e1a
RS
14708 /* Copy the architecture info from IBFD to OBFD. Also copy
14709 the 32-bit flag (if set) so that we continue to recognise
14710 OBFD as a 32-bit binary. */
14711 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
14712 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
14713 elf_elfheader (obfd)->e_flags
14714 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14715
14716 /* Copy across the ABI flags if OBFD doesn't use them
14717 and if that was what caused us to treat IBFD as 32-bit. */
14718 if ((old_flags & EF_MIPS_ABI) == 0
14719 && mips_32bit_flags_p (new_flags)
14720 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
14721 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
14722 }
14723 else
14724 {
64543e1a 14725 /* The ISAs aren't compatible. */
b49e97c9 14726 (*_bfd_error_handler)
d003868e
AM
14727 (_("%B: linking %s module with previous %s modules"),
14728 ibfd,
64543e1a
RS
14729 bfd_printable_name (ibfd),
14730 bfd_printable_name (obfd));
b34976b6 14731 ok = FALSE;
b49e97c9 14732 }
b49e97c9
TS
14733 }
14734
64543e1a
RS
14735 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14736 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14737
14738 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
14739 does set EI_CLASS differently from any 32-bit ABI. */
14740 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
14741 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14742 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14743 {
14744 /* Only error if both are set (to different values). */
14745 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
14746 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14747 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14748 {
14749 (*_bfd_error_handler)
d003868e
AM
14750 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
14751 ibfd,
b49e97c9
TS
14752 elf_mips_abi_name (ibfd),
14753 elf_mips_abi_name (obfd));
b34976b6 14754 ok = FALSE;
b49e97c9
TS
14755 }
14756 new_flags &= ~EF_MIPS_ABI;
14757 old_flags &= ~EF_MIPS_ABI;
14758 }
14759
df58fc94
RS
14760 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
14761 and allow arbitrary mixing of the remaining ASEs (retain the union). */
fb39dac1
RS
14762 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
14763 {
df58fc94
RS
14764 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14765 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14766 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
14767 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
14768 int micro_mis = old_m16 && new_micro;
14769 int m16_mis = old_micro && new_m16;
14770
14771 if (m16_mis || micro_mis)
14772 {
14773 (*_bfd_error_handler)
14774 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
14775 ibfd,
14776 m16_mis ? "MIPS16" : "microMIPS",
14777 m16_mis ? "microMIPS" : "MIPS16");
14778 ok = FALSE;
14779 }
14780
fb39dac1
RS
14781 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
14782
14783 new_flags &= ~ EF_MIPS_ARCH_ASE;
14784 old_flags &= ~ EF_MIPS_ARCH_ASE;
14785 }
14786
ba92f887
MR
14787 /* Compare NaN encodings. */
14788 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
14789 {
14790 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
14791 ibfd,
14792 (new_flags & EF_MIPS_NAN2008
14793 ? "-mnan=2008" : "-mnan=legacy"),
14794 (old_flags & EF_MIPS_NAN2008
14795 ? "-mnan=2008" : "-mnan=legacy"));
14796 ok = FALSE;
14797 new_flags &= ~EF_MIPS_NAN2008;
14798 old_flags &= ~EF_MIPS_NAN2008;
14799 }
14800
b49e97c9
TS
14801 /* Warn about any other mismatches */
14802 if (new_flags != old_flags)
14803 {
14804 (*_bfd_error_handler)
d003868e
AM
14805 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
14806 ibfd, (unsigned long) new_flags,
b49e97c9 14807 (unsigned long) old_flags);
b34976b6 14808 ok = FALSE;
b49e97c9
TS
14809 }
14810
14811 if (! ok)
14812 {
14813 bfd_set_error (bfd_error_bad_value);
b34976b6 14814 return FALSE;
b49e97c9
TS
14815 }
14816
b34976b6 14817 return TRUE;
b49e97c9
TS
14818}
14819
14820/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
14821
b34976b6 14822bfd_boolean
9719ad41 14823_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
14824{
14825 BFD_ASSERT (!elf_flags_init (abfd)
14826 || elf_elfheader (abfd)->e_flags == flags);
14827
14828 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
14829 elf_flags_init (abfd) = TRUE;
14830 return TRUE;
b49e97c9
TS
14831}
14832
ad9563d6
CM
14833char *
14834_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14835{
14836 switch (dtag)
14837 {
14838 default: return "";
14839 case DT_MIPS_RLD_VERSION:
14840 return "MIPS_RLD_VERSION";
14841 case DT_MIPS_TIME_STAMP:
14842 return "MIPS_TIME_STAMP";
14843 case DT_MIPS_ICHECKSUM:
14844 return "MIPS_ICHECKSUM";
14845 case DT_MIPS_IVERSION:
14846 return "MIPS_IVERSION";
14847 case DT_MIPS_FLAGS:
14848 return "MIPS_FLAGS";
14849 case DT_MIPS_BASE_ADDRESS:
14850 return "MIPS_BASE_ADDRESS";
14851 case DT_MIPS_MSYM:
14852 return "MIPS_MSYM";
14853 case DT_MIPS_CONFLICT:
14854 return "MIPS_CONFLICT";
14855 case DT_MIPS_LIBLIST:
14856 return "MIPS_LIBLIST";
14857 case DT_MIPS_LOCAL_GOTNO:
14858 return "MIPS_LOCAL_GOTNO";
14859 case DT_MIPS_CONFLICTNO:
14860 return "MIPS_CONFLICTNO";
14861 case DT_MIPS_LIBLISTNO:
14862 return "MIPS_LIBLISTNO";
14863 case DT_MIPS_SYMTABNO:
14864 return "MIPS_SYMTABNO";
14865 case DT_MIPS_UNREFEXTNO:
14866 return "MIPS_UNREFEXTNO";
14867 case DT_MIPS_GOTSYM:
14868 return "MIPS_GOTSYM";
14869 case DT_MIPS_HIPAGENO:
14870 return "MIPS_HIPAGENO";
14871 case DT_MIPS_RLD_MAP:
14872 return "MIPS_RLD_MAP";
14873 case DT_MIPS_DELTA_CLASS:
14874 return "MIPS_DELTA_CLASS";
14875 case DT_MIPS_DELTA_CLASS_NO:
14876 return "MIPS_DELTA_CLASS_NO";
14877 case DT_MIPS_DELTA_INSTANCE:
14878 return "MIPS_DELTA_INSTANCE";
14879 case DT_MIPS_DELTA_INSTANCE_NO:
14880 return "MIPS_DELTA_INSTANCE_NO";
14881 case DT_MIPS_DELTA_RELOC:
14882 return "MIPS_DELTA_RELOC";
14883 case DT_MIPS_DELTA_RELOC_NO:
14884 return "MIPS_DELTA_RELOC_NO";
14885 case DT_MIPS_DELTA_SYM:
14886 return "MIPS_DELTA_SYM";
14887 case DT_MIPS_DELTA_SYM_NO:
14888 return "MIPS_DELTA_SYM_NO";
14889 case DT_MIPS_DELTA_CLASSSYM:
14890 return "MIPS_DELTA_CLASSSYM";
14891 case DT_MIPS_DELTA_CLASSSYM_NO:
14892 return "MIPS_DELTA_CLASSSYM_NO";
14893 case DT_MIPS_CXX_FLAGS:
14894 return "MIPS_CXX_FLAGS";
14895 case DT_MIPS_PIXIE_INIT:
14896 return "MIPS_PIXIE_INIT";
14897 case DT_MIPS_SYMBOL_LIB:
14898 return "MIPS_SYMBOL_LIB";
14899 case DT_MIPS_LOCALPAGE_GOTIDX:
14900 return "MIPS_LOCALPAGE_GOTIDX";
14901 case DT_MIPS_LOCAL_GOTIDX:
14902 return "MIPS_LOCAL_GOTIDX";
14903 case DT_MIPS_HIDDEN_GOTIDX:
14904 return "MIPS_HIDDEN_GOTIDX";
14905 case DT_MIPS_PROTECTED_GOTIDX:
14906 return "MIPS_PROTECTED_GOT_IDX";
14907 case DT_MIPS_OPTIONS:
14908 return "MIPS_OPTIONS";
14909 case DT_MIPS_INTERFACE:
14910 return "MIPS_INTERFACE";
14911 case DT_MIPS_DYNSTR_ALIGN:
14912 return "DT_MIPS_DYNSTR_ALIGN";
14913 case DT_MIPS_INTERFACE_SIZE:
14914 return "DT_MIPS_INTERFACE_SIZE";
14915 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14916 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14917 case DT_MIPS_PERF_SUFFIX:
14918 return "DT_MIPS_PERF_SUFFIX";
14919 case DT_MIPS_COMPACT_SIZE:
14920 return "DT_MIPS_COMPACT_SIZE";
14921 case DT_MIPS_GP_VALUE:
14922 return "DT_MIPS_GP_VALUE";
14923 case DT_MIPS_AUX_DYNAMIC:
14924 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
14925 case DT_MIPS_PLTGOT:
14926 return "DT_MIPS_PLTGOT";
14927 case DT_MIPS_RWPLT:
14928 return "DT_MIPS_RWPLT";
ad9563d6
CM
14929 }
14930}
14931
b34976b6 14932bfd_boolean
9719ad41 14933_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 14934{
9719ad41 14935 FILE *file = ptr;
b49e97c9
TS
14936
14937 BFD_ASSERT (abfd != NULL && ptr != NULL);
14938
14939 /* Print normal ELF private data. */
14940 _bfd_elf_print_private_bfd_data (abfd, ptr);
14941
14942 /* xgettext:c-format */
14943 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14944
14945 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14946 fprintf (file, _(" [abi=O32]"));
14947 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14948 fprintf (file, _(" [abi=O64]"));
14949 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14950 fprintf (file, _(" [abi=EABI32]"));
14951 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14952 fprintf (file, _(" [abi=EABI64]"));
14953 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14954 fprintf (file, _(" [abi unknown]"));
14955 else if (ABI_N32_P (abfd))
14956 fprintf (file, _(" [abi=N32]"));
14957 else if (ABI_64_P (abfd))
14958 fprintf (file, _(" [abi=64]"));
14959 else
14960 fprintf (file, _(" [no abi set]"));
14961
14962 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 14963 fprintf (file, " [mips1]");
b49e97c9 14964 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 14965 fprintf (file, " [mips2]");
b49e97c9 14966 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 14967 fprintf (file, " [mips3]");
b49e97c9 14968 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 14969 fprintf (file, " [mips4]");
b49e97c9 14970 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 14971 fprintf (file, " [mips5]");
b49e97c9 14972 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 14973 fprintf (file, " [mips32]");
b49e97c9 14974 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 14975 fprintf (file, " [mips64]");
af7ee8bf 14976 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 14977 fprintf (file, " [mips32r2]");
5f74bc13 14978 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 14979 fprintf (file, " [mips64r2]");
b49e97c9
TS
14980 else
14981 fprintf (file, _(" [unknown ISA]"));
14982
40d32fc6 14983 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 14984 fprintf (file, " [mdmx]");
40d32fc6
CD
14985
14986 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 14987 fprintf (file, " [mips16]");
40d32fc6 14988
df58fc94
RS
14989 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14990 fprintf (file, " [micromips]");
14991
ba92f887
MR
14992 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
14993 fprintf (file, " [nan2008]");
14994
5baf5e34
SE
14995 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
14996 fprintf (file, " [fp64]");
14997
b49e97c9 14998 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 14999 fprintf (file, " [32bitmode]");
b49e97c9
TS
15000 else
15001 fprintf (file, _(" [not 32bitmode]"));
15002
c0e3f241 15003 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 15004 fprintf (file, " [noreorder]");
c0e3f241
CD
15005
15006 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 15007 fprintf (file, " [PIC]");
c0e3f241
CD
15008
15009 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 15010 fprintf (file, " [CPIC]");
c0e3f241
CD
15011
15012 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 15013 fprintf (file, " [XGOT]");
c0e3f241
CD
15014
15015 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 15016 fprintf (file, " [UCODE]");
c0e3f241 15017
b49e97c9
TS
15018 fputc ('\n', file);
15019
b34976b6 15020 return TRUE;
b49e97c9 15021}
2f89ff8d 15022
b35d266b 15023const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 15024{
0112cd26
NC
15025 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15026 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15027 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
15028 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15029 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15030 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
15031 { NULL, 0, 0, 0, 0 }
2f89ff8d 15032};
5e2b0d47 15033
8992f0d7
TS
15034/* Merge non visibility st_other attributes. Ensure that the
15035 STO_OPTIONAL flag is copied into h->other, even if this is not a
15036 definiton of the symbol. */
5e2b0d47
NC
15037void
15038_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
15039 const Elf_Internal_Sym *isym,
15040 bfd_boolean definition,
15041 bfd_boolean dynamic ATTRIBUTE_UNUSED)
15042{
8992f0d7
TS
15043 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
15044 {
15045 unsigned char other;
15046
15047 other = (definition ? isym->st_other : h->other);
15048 other &= ~ELF_ST_VISIBILITY (-1);
15049 h->other = other | ELF_ST_VISIBILITY (h->other);
15050 }
15051
15052 if (!definition
5e2b0d47
NC
15053 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
15054 h->other |= STO_OPTIONAL;
15055}
12ac1cf5
NC
15056
15057/* Decide whether an undefined symbol is special and can be ignored.
15058 This is the case for OPTIONAL symbols on IRIX. */
15059bfd_boolean
15060_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
15061{
15062 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
15063}
e0764319
NC
15064
15065bfd_boolean
15066_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
15067{
15068 return (sym->st_shndx == SHN_COMMON
15069 || sym->st_shndx == SHN_MIPS_ACOMMON
15070 || sym->st_shndx == SHN_MIPS_SCOMMON);
15071}
861fb55a
DJ
15072
15073/* Return address for Ith PLT stub in section PLT, for relocation REL
15074 or (bfd_vma) -1 if it should not be included. */
15075
15076bfd_vma
15077_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
15078 const arelent *rel ATTRIBUTE_UNUSED)
15079{
15080 return (plt->vma
15081 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
15082 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
15083}
15084
1bbce132
MR
15085/* Build a table of synthetic symbols to represent the PLT. As with MIPS16
15086 and microMIPS PLT slots we may have a many-to-one mapping between .plt
15087 and .got.plt and also the slots may be of a different size each we walk
15088 the PLT manually fetching instructions and matching them against known
15089 patterns. To make things easier standard MIPS slots, if any, always come
15090 first. As we don't create proper ELF symbols we use the UDATA.I member
15091 of ASYMBOL to carry ISA annotation. The encoding used is the same as
15092 with the ST_OTHER member of the ELF symbol. */
15093
15094long
15095_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
15096 long symcount ATTRIBUTE_UNUSED,
15097 asymbol **syms ATTRIBUTE_UNUSED,
15098 long dynsymcount, asymbol **dynsyms,
15099 asymbol **ret)
15100{
15101 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
15102 static const char microsuffix[] = "@micromipsplt";
15103 static const char m16suffix[] = "@mips16plt";
15104 static const char mipssuffix[] = "@plt";
15105
15106 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
15107 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15108 bfd_boolean micromips_p = MICROMIPS_P (abfd);
15109 Elf_Internal_Shdr *hdr;
15110 bfd_byte *plt_data;
15111 bfd_vma plt_offset;
15112 unsigned int other;
15113 bfd_vma entry_size;
15114 bfd_vma plt0_size;
15115 asection *relplt;
15116 bfd_vma opcode;
15117 asection *plt;
15118 asymbol *send;
15119 size_t size;
15120 char *names;
15121 long counti;
15122 arelent *p;
15123 asymbol *s;
15124 char *nend;
15125 long count;
15126 long pi;
15127 long i;
15128 long n;
15129
15130 *ret = NULL;
15131
15132 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
15133 return 0;
15134
15135 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
15136 if (relplt == NULL)
15137 return 0;
15138
15139 hdr = &elf_section_data (relplt)->this_hdr;
15140 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
15141 return 0;
15142
15143 plt = bfd_get_section_by_name (abfd, ".plt");
15144 if (plt == NULL)
15145 return 0;
15146
15147 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
15148 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
15149 return -1;
15150 p = relplt->relocation;
15151
15152 /* Calculating the exact amount of space required for symbols would
15153 require two passes over the PLT, so just pessimise assuming two
15154 PLT slots per relocation. */
15155 count = relplt->size / hdr->sh_entsize;
15156 counti = count * bed->s->int_rels_per_ext_rel;
15157 size = 2 * count * sizeof (asymbol);
15158 size += count * (sizeof (mipssuffix) +
15159 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
15160 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
15161 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
15162
15163 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
15164 size += sizeof (asymbol) + sizeof (pltname);
15165
15166 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
15167 return -1;
15168
15169 if (plt->size < 16)
15170 return -1;
15171
15172 s = *ret = bfd_malloc (size);
15173 if (s == NULL)
15174 return -1;
15175 send = s + 2 * count + 1;
15176
15177 names = (char *) send;
15178 nend = (char *) s + size;
15179 n = 0;
15180
15181 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
15182 if (opcode == 0x3302fffe)
15183 {
15184 if (!micromips_p)
15185 return -1;
15186 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
15187 other = STO_MICROMIPS;
15188 }
833794fc
MR
15189 else if (opcode == 0x0398c1d0)
15190 {
15191 if (!micromips_p)
15192 return -1;
15193 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
15194 other = STO_MICROMIPS;
15195 }
1bbce132
MR
15196 else
15197 {
15198 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
15199 other = 0;
15200 }
15201
15202 s->the_bfd = abfd;
15203 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
15204 s->section = plt;
15205 s->value = 0;
15206 s->name = names;
15207 s->udata.i = other;
15208 memcpy (names, pltname, sizeof (pltname));
15209 names += sizeof (pltname);
15210 ++s, ++n;
15211
15212 pi = 0;
15213 for (plt_offset = plt0_size;
15214 plt_offset + 8 <= plt->size && s < send;
15215 plt_offset += entry_size)
15216 {
15217 bfd_vma gotplt_addr;
15218 const char *suffix;
15219 bfd_vma gotplt_hi;
15220 bfd_vma gotplt_lo;
15221 size_t suffixlen;
15222
15223 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
15224
15225 /* Check if the second word matches the expected MIPS16 instruction. */
15226 if (opcode == 0x651aeb00)
15227 {
15228 if (micromips_p)
15229 return -1;
15230 /* Truncated table??? */
15231 if (plt_offset + 16 > plt->size)
15232 break;
15233 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
15234 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
15235 suffixlen = sizeof (m16suffix);
15236 suffix = m16suffix;
15237 other = STO_MIPS16;
15238 }
833794fc 15239 /* Likewise the expected microMIPS instruction (no insn32 mode). */
1bbce132
MR
15240 else if (opcode == 0xff220000)
15241 {
15242 if (!micromips_p)
15243 return -1;
15244 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
15245 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15246 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
15247 gotplt_lo <<= 2;
15248 gotplt_addr = gotplt_hi + gotplt_lo;
15249 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
15250 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
15251 suffixlen = sizeof (microsuffix);
15252 suffix = microsuffix;
15253 other = STO_MICROMIPS;
15254 }
833794fc
MR
15255 /* Likewise the expected microMIPS instruction (insn32 mode). */
15256 else if ((opcode & 0xffff0000) == 0xff2f0000)
15257 {
15258 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15259 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
15260 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15261 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15262 gotplt_addr = gotplt_hi + gotplt_lo;
15263 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
15264 suffixlen = sizeof (microsuffix);
15265 suffix = microsuffix;
15266 other = STO_MICROMIPS;
15267 }
1bbce132
MR
15268 /* Otherwise assume standard MIPS code. */
15269 else
15270 {
15271 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
15272 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
15273 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15274 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15275 gotplt_addr = gotplt_hi + gotplt_lo;
15276 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
15277 suffixlen = sizeof (mipssuffix);
15278 suffix = mipssuffix;
15279 other = 0;
15280 }
15281 /* Truncated table??? */
15282 if (plt_offset + entry_size > plt->size)
15283 break;
15284
15285 for (i = 0;
15286 i < count && p[pi].address != gotplt_addr;
15287 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
15288
15289 if (i < count)
15290 {
15291 size_t namelen;
15292 size_t len;
15293
15294 *s = **p[pi].sym_ptr_ptr;
15295 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
15296 we are defining a symbol, ensure one of them is set. */
15297 if ((s->flags & BSF_LOCAL) == 0)
15298 s->flags |= BSF_GLOBAL;
15299 s->flags |= BSF_SYNTHETIC;
15300 s->section = plt;
15301 s->value = plt_offset;
15302 s->name = names;
15303 s->udata.i = other;
15304
15305 len = strlen ((*p[pi].sym_ptr_ptr)->name);
15306 namelen = len + suffixlen;
15307 if (names + namelen > nend)
15308 break;
15309
15310 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
15311 names += len;
15312 memcpy (names, suffix, suffixlen);
15313 names += suffixlen;
15314
15315 ++s, ++n;
15316 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
15317 }
15318 }
15319
15320 free (plt_data);
15321
15322 return n;
15323}
15324
861fb55a
DJ
15325void
15326_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
15327{
15328 struct mips_elf_link_hash_table *htab;
15329 Elf_Internal_Ehdr *i_ehdrp;
15330
15331 i_ehdrp = elf_elfheader (abfd);
15332 if (link_info)
15333 {
15334 htab = mips_elf_hash_table (link_info);
4dfe6ac6
NC
15335 BFD_ASSERT (htab != NULL);
15336
861fb55a
DJ
15337 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
15338 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
15339 }
0af03126
L
15340
15341 _bfd_elf_post_process_headers (abfd, link_info);
861fb55a 15342}
This page took 2.010402 seconds and 4 git commands to generate.