* gen-aout.c (main): Fix formatting. Close file.
[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
RS
544
545 /* The GOT requirements of input bfds. */
546 struct mips_got_info *got;
698600e4
AM
547
548 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
549 included directly in this one, but there's no point to wasting
550 the memory just for the infrequently called find_nearest_line. */
551 struct mips_elf_find_line *find_line_info;
552
553 /* An array of stub sections indexed by symbol number. */
554 asection **local_stubs;
555 asection **local_call_stubs;
556
557 /* The Irix 5 support uses two virtual sections, which represent
558 text/data symbols defined in dynamic objects. */
559 asymbol *elf_data_symbol;
560 asymbol *elf_text_symbol;
561 asection *elf_data_section;
562 asection *elf_text_section;
6ae68ba3
MR
563};
564
565/* Get MIPS ELF private object data from BFD's tdata. */
566
567#define mips_elf_tdata(bfd) \
568 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
569
0f20cc35
DJ
570#define TLS_RELOC_P(r_type) \
571 (r_type == R_MIPS_TLS_DTPMOD32 \
572 || r_type == R_MIPS_TLS_DTPMOD64 \
573 || r_type == R_MIPS_TLS_DTPREL32 \
574 || r_type == R_MIPS_TLS_DTPREL64 \
575 || r_type == R_MIPS_TLS_GD \
576 || r_type == R_MIPS_TLS_LDM \
577 || r_type == R_MIPS_TLS_DTPREL_HI16 \
578 || r_type == R_MIPS_TLS_DTPREL_LO16 \
579 || r_type == R_MIPS_TLS_GOTTPREL \
580 || r_type == R_MIPS_TLS_TPREL32 \
581 || r_type == R_MIPS_TLS_TPREL64 \
582 || r_type == R_MIPS_TLS_TPREL_HI16 \
df58fc94 583 || r_type == R_MIPS_TLS_TPREL_LO16 \
d0f13682
CLT
584 || r_type == R_MIPS16_TLS_GD \
585 || r_type == R_MIPS16_TLS_LDM \
586 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
587 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
588 || r_type == R_MIPS16_TLS_GOTTPREL \
589 || r_type == R_MIPS16_TLS_TPREL_HI16 \
590 || r_type == R_MIPS16_TLS_TPREL_LO16 \
df58fc94
RS
591 || r_type == R_MICROMIPS_TLS_GD \
592 || r_type == R_MICROMIPS_TLS_LDM \
593 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
594 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
595 || r_type == R_MICROMIPS_TLS_GOTTPREL \
596 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
597 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
0f20cc35 598
b49e97c9
TS
599/* Structure used to pass information to mips_elf_output_extsym. */
600
601struct extsym_info
602{
9e4aeb93
RS
603 bfd *abfd;
604 struct bfd_link_info *info;
b49e97c9
TS
605 struct ecoff_debug_info *debug;
606 const struct ecoff_debug_swap *swap;
b34976b6 607 bfd_boolean failed;
b49e97c9
TS
608};
609
8dc1a139 610/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
611
612static const char * const mips_elf_dynsym_rtproc_names[] =
613{
614 "_procedure_table",
615 "_procedure_string_table",
616 "_procedure_table_size",
617 NULL
618};
619
620/* These structures are used to generate the .compact_rel section on
8dc1a139 621 IRIX5. */
b49e97c9
TS
622
623typedef struct
624{
625 unsigned long id1; /* Always one? */
626 unsigned long num; /* Number of compact relocation entries. */
627 unsigned long id2; /* Always two? */
628 unsigned long offset; /* The file offset of the first relocation. */
629 unsigned long reserved0; /* Zero? */
630 unsigned long reserved1; /* Zero? */
631} Elf32_compact_rel;
632
633typedef struct
634{
635 bfd_byte id1[4];
636 bfd_byte num[4];
637 bfd_byte id2[4];
638 bfd_byte offset[4];
639 bfd_byte reserved0[4];
640 bfd_byte reserved1[4];
641} Elf32_External_compact_rel;
642
643typedef struct
644{
645 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
646 unsigned int rtype : 4; /* Relocation types. See below. */
647 unsigned int dist2to : 8;
648 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
649 unsigned long konst; /* KONST field. See below. */
650 unsigned long vaddr; /* VADDR to be relocated. */
651} Elf32_crinfo;
652
653typedef struct
654{
655 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
656 unsigned int rtype : 4; /* Relocation types. See below. */
657 unsigned int dist2to : 8;
658 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
659 unsigned long konst; /* KONST field. See below. */
660} Elf32_crinfo2;
661
662typedef struct
663{
664 bfd_byte info[4];
665 bfd_byte konst[4];
666 bfd_byte vaddr[4];
667} Elf32_External_crinfo;
668
669typedef struct
670{
671 bfd_byte info[4];
672 bfd_byte konst[4];
673} Elf32_External_crinfo2;
674
675/* These are the constants used to swap the bitfields in a crinfo. */
676
677#define CRINFO_CTYPE (0x1)
678#define CRINFO_CTYPE_SH (31)
679#define CRINFO_RTYPE (0xf)
680#define CRINFO_RTYPE_SH (27)
681#define CRINFO_DIST2TO (0xff)
682#define CRINFO_DIST2TO_SH (19)
683#define CRINFO_RELVADDR (0x7ffff)
684#define CRINFO_RELVADDR_SH (0)
685
686/* A compact relocation info has long (3 words) or short (2 words)
687 formats. A short format doesn't have VADDR field and relvaddr
688 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
689#define CRF_MIPS_LONG 1
690#define CRF_MIPS_SHORT 0
691
692/* There are 4 types of compact relocation at least. The value KONST
693 has different meaning for each type:
694
695 (type) (konst)
696 CT_MIPS_REL32 Address in data
697 CT_MIPS_WORD Address in word (XXX)
698 CT_MIPS_GPHI_LO GP - vaddr
699 CT_MIPS_JMPAD Address to jump
700 */
701
702#define CRT_MIPS_REL32 0xa
703#define CRT_MIPS_WORD 0xb
704#define CRT_MIPS_GPHI_LO 0xc
705#define CRT_MIPS_JMPAD 0xd
706
707#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
708#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
709#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
710#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
711\f
712/* The structure of the runtime procedure descriptor created by the
713 loader for use by the static exception system. */
714
715typedef struct runtime_pdr {
ae9a127f
NC
716 bfd_vma adr; /* Memory address of start of procedure. */
717 long regmask; /* Save register mask. */
718 long regoffset; /* Save register offset. */
719 long fregmask; /* Save floating point register mask. */
720 long fregoffset; /* Save floating point register offset. */
721 long frameoffset; /* Frame size. */
722 short framereg; /* Frame pointer register. */
723 short pcreg; /* Offset or reg of return pc. */
724 long irpss; /* Index into the runtime string table. */
b49e97c9 725 long reserved;
ae9a127f 726 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
727} RPDR, *pRPDR;
728#define cbRPDR sizeof (RPDR)
729#define rpdNil ((pRPDR) 0)
730\f
b15e6682 731static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
732 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
733 struct mips_elf_link_hash_entry *, int);
b34976b6 734static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 735 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
736static bfd_vma mips_elf_high
737 (bfd_vma);
b34976b6 738static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
739 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
740 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
741 bfd_vma *, asection *);
f4416af6 742static bfd_vma mips_elf_adjust_gp
9719ad41 743 (bfd *, struct mips_got_info *, bfd *);
f4416af6 744
b49e97c9
TS
745/* This will be used when we sort the dynamic relocation records. */
746static bfd *reldyn_sorting_bfd;
747
6d30f5b2
NC
748/* True if ABFD is for CPUs with load interlocking that include
749 non-MIPS1 CPUs and R3900. */
750#define LOAD_INTERLOCKS_P(abfd) \
751 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
752 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
753
cd8d5a82
CF
754/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
755 This should be safe for all architectures. We enable this predicate
756 for RM9000 for now. */
757#define JAL_TO_BAL_P(abfd) \
758 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
759
760/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
761 This should be safe for all architectures. We enable this predicate for
762 all CPUs. */
763#define JALR_TO_BAL_P(abfd) 1
764
38a7df63
CF
765/* True if ABFD is for CPUs that are faster if JR is converted to B.
766 This should be safe for all architectures. We enable this predicate for
767 all CPUs. */
768#define JR_TO_B_P(abfd) 1
769
861fb55a
DJ
770/* True if ABFD is a PIC object. */
771#define PIC_OBJECT_P(abfd) \
772 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
773
b49e97c9 774/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
775#define ABI_N32_P(abfd) \
776 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
777
4a14403c 778/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 779#define ABI_64_P(abfd) \
141ff970 780 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 781
4a14403c
TS
782/* Nonzero if ABFD is using NewABI conventions. */
783#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
784
e8faf7d1
MR
785/* Nonzero if ABFD has microMIPS code. */
786#define MICROMIPS_P(abfd) \
787 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
788
4a14403c 789/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
790#define IRIX_COMPAT(abfd) \
791 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
792
b49e97c9
TS
793/* Whether we are trying to be compatible with IRIX at all. */
794#define SGI_COMPAT(abfd) \
795 (IRIX_COMPAT (abfd) != ict_none)
796
797/* The name of the options section. */
798#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 799 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 800
cc2e31b9
RS
801/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
802 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
803#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
804 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
805
943284cc
DJ
806/* Whether the section is readonly. */
807#define MIPS_ELF_READONLY_SECTION(sec) \
808 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
809 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
810
b49e97c9 811/* The name of the stub section. */
ca07892d 812#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
813
814/* The size of an external REL relocation. */
815#define MIPS_ELF_REL_SIZE(abfd) \
816 (get_elf_backend_data (abfd)->s->sizeof_rel)
817
0a44bf69
RS
818/* The size of an external RELA relocation. */
819#define MIPS_ELF_RELA_SIZE(abfd) \
820 (get_elf_backend_data (abfd)->s->sizeof_rela)
821
b49e97c9
TS
822/* The size of an external dynamic table entry. */
823#define MIPS_ELF_DYN_SIZE(abfd) \
824 (get_elf_backend_data (abfd)->s->sizeof_dyn)
825
826/* The size of a GOT entry. */
827#define MIPS_ELF_GOT_SIZE(abfd) \
828 (get_elf_backend_data (abfd)->s->arch_size / 8)
829
b4082c70
DD
830/* The size of the .rld_map section. */
831#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
832 (get_elf_backend_data (abfd)->s->arch_size / 8)
833
b49e97c9
TS
834/* The size of a symbol-table entry. */
835#define MIPS_ELF_SYM_SIZE(abfd) \
836 (get_elf_backend_data (abfd)->s->sizeof_sym)
837
838/* The default alignment for sections, as a power of two. */
839#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 840 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
841
842/* Get word-sized data. */
843#define MIPS_ELF_GET_WORD(abfd, ptr) \
844 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
845
846/* Put out word-sized data. */
847#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
848 (ABI_64_P (abfd) \
849 ? bfd_put_64 (abfd, val, ptr) \
850 : bfd_put_32 (abfd, val, ptr))
851
861fb55a
DJ
852/* The opcode for word-sized loads (LW or LD). */
853#define MIPS_ELF_LOAD_WORD(abfd) \
854 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
855
b49e97c9 856/* Add a dynamic symbol table-entry. */
9719ad41 857#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 858 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
859
860#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
861 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
862
0a44bf69
RS
863/* The name of the dynamic relocation section. */
864#define MIPS_ELF_REL_DYN_NAME(INFO) \
865 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
866
b49e97c9
TS
867/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
868 from smaller values. Start with zero, widen, *then* decrement. */
869#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 870#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 871
51e38d68
RS
872/* The value to write into got[1] for SVR4 targets, to identify it is
873 a GNU object. The dynamic linker can then use got[1] to store the
874 module pointer. */
875#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
876 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
877
f4416af6 878/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
879#define ELF_MIPS_GP_OFFSET(INFO) \
880 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
881
882/* The maximum size of the GOT for it to be addressable using 16-bit
883 offsets from $gp. */
0a44bf69 884#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 885
6a691779 886/* Instructions which appear in a stub. */
3d6746ca
DD
887#define STUB_LW(abfd) \
888 ((ABI_64_P (abfd) \
889 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
890 : 0x8f998010)) /* lw t9,0x8010(gp) */
891#define STUB_MOVE(abfd) \
892 ((ABI_64_P (abfd) \
893 ? 0x03e0782d /* daddu t7,ra */ \
894 : 0x03e07821)) /* addu t7,ra */
895#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
896#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
897#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
898#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
899#define STUB_LI16S(abfd, VAL) \
900 ((ABI_64_P (abfd) \
901 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
902 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
903
1bbce132
MR
904/* Likewise for the microMIPS ASE. */
905#define STUB_LW_MICROMIPS(abfd) \
906 (ABI_64_P (abfd) \
907 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
908 : 0xff3c8010) /* lw t9,0x8010(gp) */
909#define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
833794fc
MR
910#define STUB_MOVE32_MICROMIPS(abfd) \
911 (ABI_64_P (abfd) \
912 ? 0x581f7950 /* daddu t7,ra,zero */ \
913 : 0x001f7950) /* addu t7,ra,zero */
1bbce132
MR
914#define STUB_LUI_MICROMIPS(VAL) \
915 (0x41b80000 + (VAL)) /* lui t8,VAL */
916#define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
833794fc 917#define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
1bbce132
MR
918#define STUB_ORI_MICROMIPS(VAL) \
919 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
920#define STUB_LI16U_MICROMIPS(VAL) \
921 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
922#define STUB_LI16S_MICROMIPS(abfd, VAL) \
923 (ABI_64_P (abfd) \
924 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
925 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
926
5108fc1b
RS
927#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
928#define MIPS_FUNCTION_STUB_BIG_SIZE 20
1bbce132
MR
929#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
930#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
833794fc
MR
931#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
932#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
933
934/* The name of the dynamic interpreter. This is put in the .interp
935 section. */
936
937#define ELF_DYNAMIC_INTERPRETER(abfd) \
938 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
939 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
940 : "/usr/lib/libc.so.1")
941
942#ifdef BFD64
ee6423ed
AO
943#define MNAME(bfd,pre,pos) \
944 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
945#define ELF_R_SYM(bfd, i) \
946 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
947#define ELF_R_TYPE(bfd, i) \
948 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
949#define ELF_R_INFO(bfd, s, t) \
950 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
951#else
ee6423ed 952#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
953#define ELF_R_SYM(bfd, i) \
954 (ELF32_R_SYM (i))
955#define ELF_R_TYPE(bfd, i) \
956 (ELF32_R_TYPE (i))
957#define ELF_R_INFO(bfd, s, t) \
958 (ELF32_R_INFO (s, t))
959#endif
960\f
961 /* The mips16 compiler uses a couple of special sections to handle
962 floating point arguments.
963
964 Section names that look like .mips16.fn.FNNAME contain stubs that
965 copy floating point arguments from the fp regs to the gp regs and
966 then jump to FNNAME. If any 32 bit function calls FNNAME, the
967 call should be redirected to the stub instead. If no 32 bit
968 function calls FNNAME, the stub should be discarded. We need to
969 consider any reference to the function, not just a call, because
970 if the address of the function is taken we will need the stub,
971 since the address might be passed to a 32 bit function.
972
973 Section names that look like .mips16.call.FNNAME contain stubs
974 that copy floating point arguments from the gp regs to the fp
975 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
976 then any 16 bit function that calls FNNAME should be redirected
977 to the stub instead. If FNNAME is not a 32 bit function, the
978 stub should be discarded.
979
980 .mips16.call.fp.FNNAME sections are similar, but contain stubs
981 which call FNNAME and then copy the return value from the fp regs
982 to the gp regs. These stubs store the return value in $18 while
983 calling FNNAME; any function which might call one of these stubs
984 must arrange to save $18 around the call. (This case is not
985 needed for 32 bit functions that call 16 bit functions, because
986 16 bit functions always return floating point values in both
987 $f0/$f1 and $2/$3.)
988
989 Note that in all cases FNNAME might be defined statically.
990 Therefore, FNNAME is not used literally. Instead, the relocation
991 information will indicate which symbol the section is for.
992
993 We record any stubs that we find in the symbol table. */
994
995#define FN_STUB ".mips16.fn."
996#define CALL_STUB ".mips16.call."
997#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
998
999#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1000#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1001#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 1002\f
861fb55a 1003/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
1004static const bfd_vma mips_o32_exec_plt0_entry[] =
1005{
861fb55a
DJ
1006 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1007 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1008 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1009 0x031cc023, /* subu $24, $24, $28 */
81f5d455 1010 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
861fb55a
DJ
1011 0x0018c082, /* srl $24, $24, 2 */
1012 0x0320f809, /* jalr $25 */
1013 0x2718fffe /* subu $24, $24, 2 */
1014};
1015
1016/* The format of the first PLT entry in an N32 executable. Different
1017 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
1018static const bfd_vma mips_n32_exec_plt0_entry[] =
1019{
861fb55a
DJ
1020 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1021 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1022 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1023 0x030ec023, /* subu $24, $24, $14 */
81f5d455 1024 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
861fb55a
DJ
1025 0x0018c082, /* srl $24, $24, 2 */
1026 0x0320f809, /* jalr $25 */
1027 0x2718fffe /* subu $24, $24, 2 */
1028};
1029
1030/* The format of the first PLT entry in an N64 executable. Different
1031 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
1032static const bfd_vma mips_n64_exec_plt0_entry[] =
1033{
861fb55a
DJ
1034 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1035 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1036 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1037 0x030ec023, /* subu $24, $24, $14 */
81f5d455 1038 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
861fb55a
DJ
1039 0x0018c0c2, /* srl $24, $24, 3 */
1040 0x0320f809, /* jalr $25 */
1041 0x2718fffe /* subu $24, $24, 2 */
1042};
1043
1bbce132
MR
1044/* The format of the microMIPS first PLT entry in an O32 executable.
1045 We rely on v0 ($2) rather than t8 ($24) to contain the address
1046 of the GOTPLT entry handled, so this stub may only be used when
1047 all the subsequent PLT entries are microMIPS code too.
1048
1049 The trailing NOP is for alignment and correct disassembly only. */
1050static const bfd_vma micromips_o32_exec_plt0_entry[] =
1051{
1052 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1053 0xff23, 0x0000, /* lw $25, 0($3) */
1054 0x0535, /* subu $2, $2, $3 */
1055 0x2525, /* srl $2, $2, 2 */
1056 0x3302, 0xfffe, /* subu $24, $2, 2 */
1057 0x0dff, /* move $15, $31 */
1058 0x45f9, /* jalrs $25 */
1059 0x0f83, /* move $28, $3 */
1060 0x0c00 /* nop */
1061};
1062
833794fc
MR
1063/* The format of the microMIPS first PLT entry in an O32 executable
1064 in the insn32 mode. */
1065static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1066{
1067 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1068 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1069 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1070 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1071 0x001f, 0x7950, /* move $15, $31 */
1072 0x0318, 0x1040, /* srl $24, $24, 2 */
1073 0x03f9, 0x0f3c, /* jalr $25 */
1074 0x3318, 0xfffe /* subu $24, $24, 2 */
1075};
1076
1bbce132 1077/* The format of subsequent standard PLT entries. */
6d30f5b2
NC
1078static const bfd_vma mips_exec_plt_entry[] =
1079{
861fb55a
DJ
1080 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1081 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1082 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1083 0x03200008 /* jr $25 */
1084};
1085
1bbce132
MR
1086/* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1087 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1088 directly addressable. */
1089static const bfd_vma mips16_o32_exec_plt_entry[] =
1090{
1091 0xb203, /* lw $2, 12($pc) */
1092 0x9a60, /* lw $3, 0($2) */
1093 0x651a, /* move $24, $2 */
1094 0xeb00, /* jr $3 */
1095 0x653b, /* move $25, $3 */
1096 0x6500, /* nop */
1097 0x0000, 0x0000 /* .word (.got.plt entry) */
1098};
1099
1100/* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1101 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1102static const bfd_vma micromips_o32_exec_plt_entry[] =
1103{
1104 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1105 0xff22, 0x0000, /* lw $25, 0($2) */
1106 0x4599, /* jr $25 */
1107 0x0f02 /* move $24, $2 */
1108};
1109
833794fc
MR
1110/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1111static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1112{
1113 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1114 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1115 0x0019, 0x0f3c, /* jr $25 */
1116 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1117};
1118
0a44bf69 1119/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
1120static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1121{
0a44bf69
RS
1122 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1123 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1124 0x8f390008, /* lw t9, 8(t9) */
1125 0x00000000, /* nop */
1126 0x03200008, /* jr t9 */
1127 0x00000000 /* nop */
1128};
1129
1130/* The format of subsequent PLT entries. */
6d30f5b2
NC
1131static const bfd_vma mips_vxworks_exec_plt_entry[] =
1132{
0a44bf69
RS
1133 0x10000000, /* b .PLT_resolver */
1134 0x24180000, /* li t8, <pltindex> */
1135 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1136 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1137 0x8f390000, /* lw t9, 0(t9) */
1138 0x00000000, /* nop */
1139 0x03200008, /* jr t9 */
1140 0x00000000 /* nop */
1141};
1142
1143/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
1144static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1145{
0a44bf69
RS
1146 0x8f990008, /* lw t9, 8(gp) */
1147 0x00000000, /* nop */
1148 0x03200008, /* jr t9 */
1149 0x00000000, /* nop */
1150 0x00000000, /* nop */
1151 0x00000000 /* nop */
1152};
1153
1154/* The format of subsequent PLT entries. */
6d30f5b2
NC
1155static const bfd_vma mips_vxworks_shared_plt_entry[] =
1156{
0a44bf69
RS
1157 0x10000000, /* b .PLT_resolver */
1158 0x24180000 /* li t8, <pltindex> */
1159};
1160\f
d21911ea
MR
1161/* microMIPS 32-bit opcode helper installer. */
1162
1163static void
1164bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1165{
1166 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1167 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1168}
1169
1170/* microMIPS 32-bit opcode helper retriever. */
1171
1172static bfd_vma
1173bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1174{
1175 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1176}
1177\f
b49e97c9
TS
1178/* Look up an entry in a MIPS ELF linker hash table. */
1179
1180#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1181 ((struct mips_elf_link_hash_entry *) \
1182 elf_link_hash_lookup (&(table)->root, (string), (create), \
1183 (copy), (follow)))
1184
1185/* Traverse a MIPS ELF linker hash table. */
1186
1187#define mips_elf_link_hash_traverse(table, func, info) \
1188 (elf_link_hash_traverse \
1189 (&(table)->root, \
9719ad41 1190 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1191 (info)))
1192
0f20cc35
DJ
1193/* Find the base offsets for thread-local storage in this object,
1194 for GD/LD and IE/LE respectively. */
1195
1196#define TP_OFFSET 0x7000
1197#define DTP_OFFSET 0x8000
1198
1199static bfd_vma
1200dtprel_base (struct bfd_link_info *info)
1201{
1202 /* If tls_sec is NULL, we should have signalled an error already. */
1203 if (elf_hash_table (info)->tls_sec == NULL)
1204 return 0;
1205 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1206}
1207
1208static bfd_vma
1209tprel_base (struct bfd_link_info *info)
1210{
1211 /* If tls_sec is NULL, we should have signalled an error already. */
1212 if (elf_hash_table (info)->tls_sec == NULL)
1213 return 0;
1214 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1215}
1216
b49e97c9
TS
1217/* Create an entry in a MIPS ELF linker hash table. */
1218
1219static struct bfd_hash_entry *
9719ad41
RS
1220mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1221 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1222{
1223 struct mips_elf_link_hash_entry *ret =
1224 (struct mips_elf_link_hash_entry *) entry;
1225
1226 /* Allocate the structure if it has not already been allocated by a
1227 subclass. */
9719ad41
RS
1228 if (ret == NULL)
1229 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1230 if (ret == NULL)
b49e97c9
TS
1231 return (struct bfd_hash_entry *) ret;
1232
1233 /* Call the allocation method of the superclass. */
1234 ret = ((struct mips_elf_link_hash_entry *)
1235 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1236 table, string));
9719ad41 1237 if (ret != NULL)
b49e97c9
TS
1238 {
1239 /* Set local fields. */
1240 memset (&ret->esym, 0, sizeof (EXTR));
1241 /* We use -2 as a marker to indicate that the information has
1242 not been set. -1 means there is no associated ifd. */
1243 ret->esym.ifd = -2;
861fb55a 1244 ret->la25_stub = 0;
b49e97c9 1245 ret->possibly_dynamic_relocs = 0;
b49e97c9 1246 ret->fn_stub = NULL;
b49e97c9
TS
1247 ret->call_stub = NULL;
1248 ret->call_fp_stub = NULL;
634835ae 1249 ret->global_got_area = GGA_NONE;
6ccf4795 1250 ret->got_only_for_calls = TRUE;
71782a75 1251 ret->readonly_reloc = FALSE;
861fb55a 1252 ret->has_static_relocs = FALSE;
71782a75
RS
1253 ret->no_fn_stub = FALSE;
1254 ret->need_fn_stub = FALSE;
861fb55a 1255 ret->has_nonpic_branches = FALSE;
33bb52fb 1256 ret->needs_lazy_stub = FALSE;
1bbce132 1257 ret->use_plt_entry = FALSE;
b49e97c9
TS
1258 }
1259
1260 return (struct bfd_hash_entry *) ret;
1261}
f0abc2a1 1262
6ae68ba3
MR
1263/* Allocate MIPS ELF private object data. */
1264
1265bfd_boolean
1266_bfd_mips_elf_mkobject (bfd *abfd)
1267{
1268 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1269 MIPS_ELF_DATA);
1270}
1271
f0abc2a1 1272bfd_boolean
9719ad41 1273_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1274{
f592407e
AM
1275 if (!sec->used_by_bfd)
1276 {
1277 struct _mips_elf_section_data *sdata;
1278 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1279
f592407e
AM
1280 sdata = bfd_zalloc (abfd, amt);
1281 if (sdata == NULL)
1282 return FALSE;
1283 sec->used_by_bfd = sdata;
1284 }
f0abc2a1
AM
1285
1286 return _bfd_elf_new_section_hook (abfd, sec);
1287}
b49e97c9
TS
1288\f
1289/* Read ECOFF debugging information from a .mdebug section into a
1290 ecoff_debug_info structure. */
1291
b34976b6 1292bfd_boolean
9719ad41
RS
1293_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1294 struct ecoff_debug_info *debug)
b49e97c9
TS
1295{
1296 HDRR *symhdr;
1297 const struct ecoff_debug_swap *swap;
9719ad41 1298 char *ext_hdr;
b49e97c9
TS
1299
1300 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1301 memset (debug, 0, sizeof (*debug));
1302
9719ad41 1303 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1304 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1305 goto error_return;
1306
9719ad41 1307 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1308 swap->external_hdr_size))
b49e97c9
TS
1309 goto error_return;
1310
1311 symhdr = &debug->symbolic_header;
1312 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1313
1314 /* The symbolic header contains absolute file offsets and sizes to
1315 read. */
1316#define READ(ptr, offset, count, size, type) \
1317 if (symhdr->count == 0) \
1318 debug->ptr = NULL; \
1319 else \
1320 { \
1321 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1322 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1323 if (debug->ptr == NULL) \
1324 goto error_return; \
9719ad41 1325 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1326 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1327 goto error_return; \
1328 }
1329
1330 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1331 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1332 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1333 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1334 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1335 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1336 union aux_ext *);
1337 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1338 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1339 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1340 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1341 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1342#undef READ
1343
1344 debug->fdr = NULL;
b49e97c9 1345
b34976b6 1346 return TRUE;
b49e97c9
TS
1347
1348 error_return:
1349 if (ext_hdr != NULL)
1350 free (ext_hdr);
1351 if (debug->line != NULL)
1352 free (debug->line);
1353 if (debug->external_dnr != NULL)
1354 free (debug->external_dnr);
1355 if (debug->external_pdr != NULL)
1356 free (debug->external_pdr);
1357 if (debug->external_sym != NULL)
1358 free (debug->external_sym);
1359 if (debug->external_opt != NULL)
1360 free (debug->external_opt);
1361 if (debug->external_aux != NULL)
1362 free (debug->external_aux);
1363 if (debug->ss != NULL)
1364 free (debug->ss);
1365 if (debug->ssext != NULL)
1366 free (debug->ssext);
1367 if (debug->external_fdr != NULL)
1368 free (debug->external_fdr);
1369 if (debug->external_rfd != NULL)
1370 free (debug->external_rfd);
1371 if (debug->external_ext != NULL)
1372 free (debug->external_ext);
b34976b6 1373 return FALSE;
b49e97c9
TS
1374}
1375\f
1376/* Swap RPDR (runtime procedure table entry) for output. */
1377
1378static void
9719ad41 1379ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1380{
1381 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1382 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1383 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1384 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1385 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1386 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1387
1388 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1389 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1390
1391 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1392}
1393
1394/* Create a runtime procedure table from the .mdebug section. */
1395
b34976b6 1396static bfd_boolean
9719ad41
RS
1397mips_elf_create_procedure_table (void *handle, bfd *abfd,
1398 struct bfd_link_info *info, asection *s,
1399 struct ecoff_debug_info *debug)
b49e97c9
TS
1400{
1401 const struct ecoff_debug_swap *swap;
1402 HDRR *hdr = &debug->symbolic_header;
1403 RPDR *rpdr, *rp;
1404 struct rpdr_ext *erp;
9719ad41 1405 void *rtproc;
b49e97c9
TS
1406 struct pdr_ext *epdr;
1407 struct sym_ext *esym;
1408 char *ss, **sv;
1409 char *str;
1410 bfd_size_type size;
1411 bfd_size_type count;
1412 unsigned long sindex;
1413 unsigned long i;
1414 PDR pdr;
1415 SYMR sym;
1416 const char *no_name_func = _("static procedure (no name)");
1417
1418 epdr = NULL;
1419 rpdr = NULL;
1420 esym = NULL;
1421 ss = NULL;
1422 sv = NULL;
1423
1424 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1425
1426 sindex = strlen (no_name_func) + 1;
1427 count = hdr->ipdMax;
1428 if (count > 0)
1429 {
1430 size = swap->external_pdr_size;
1431
9719ad41 1432 epdr = bfd_malloc (size * count);
b49e97c9
TS
1433 if (epdr == NULL)
1434 goto error_return;
1435
9719ad41 1436 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1437 goto error_return;
1438
1439 size = sizeof (RPDR);
9719ad41 1440 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1441 if (rpdr == NULL)
1442 goto error_return;
1443
1444 size = sizeof (char *);
9719ad41 1445 sv = bfd_malloc (size * count);
b49e97c9
TS
1446 if (sv == NULL)
1447 goto error_return;
1448
1449 count = hdr->isymMax;
1450 size = swap->external_sym_size;
9719ad41 1451 esym = bfd_malloc (size * count);
b49e97c9
TS
1452 if (esym == NULL)
1453 goto error_return;
1454
9719ad41 1455 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1456 goto error_return;
1457
1458 count = hdr->issMax;
9719ad41 1459 ss = bfd_malloc (count);
b49e97c9
TS
1460 if (ss == NULL)
1461 goto error_return;
f075ee0c 1462 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1463 goto error_return;
1464
1465 count = hdr->ipdMax;
1466 for (i = 0; i < (unsigned long) count; i++, rp++)
1467 {
9719ad41
RS
1468 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1469 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1470 rp->adr = sym.value;
1471 rp->regmask = pdr.regmask;
1472 rp->regoffset = pdr.regoffset;
1473 rp->fregmask = pdr.fregmask;
1474 rp->fregoffset = pdr.fregoffset;
1475 rp->frameoffset = pdr.frameoffset;
1476 rp->framereg = pdr.framereg;
1477 rp->pcreg = pdr.pcreg;
1478 rp->irpss = sindex;
1479 sv[i] = ss + sym.iss;
1480 sindex += strlen (sv[i]) + 1;
1481 }
1482 }
1483
1484 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1485 size = BFD_ALIGN (size, 16);
9719ad41 1486 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1487 if (rtproc == NULL)
1488 {
1489 mips_elf_hash_table (info)->procedure_count = 0;
1490 goto error_return;
1491 }
1492
1493 mips_elf_hash_table (info)->procedure_count = count + 2;
1494
9719ad41 1495 erp = rtproc;
b49e97c9
TS
1496 memset (erp, 0, sizeof (struct rpdr_ext));
1497 erp++;
1498 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1499 strcpy (str, no_name_func);
1500 str += strlen (no_name_func) + 1;
1501 for (i = 0; i < count; i++)
1502 {
1503 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1504 strcpy (str, sv[i]);
1505 str += strlen (sv[i]) + 1;
1506 }
1507 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1508
1509 /* Set the size and contents of .rtproc section. */
eea6121a 1510 s->size = size;
9719ad41 1511 s->contents = rtproc;
b49e97c9
TS
1512
1513 /* Skip this section later on (I don't think this currently
1514 matters, but someday it might). */
8423293d 1515 s->map_head.link_order = NULL;
b49e97c9
TS
1516
1517 if (epdr != NULL)
1518 free (epdr);
1519 if (rpdr != NULL)
1520 free (rpdr);
1521 if (esym != NULL)
1522 free (esym);
1523 if (ss != NULL)
1524 free (ss);
1525 if (sv != NULL)
1526 free (sv);
1527
b34976b6 1528 return TRUE;
b49e97c9
TS
1529
1530 error_return:
1531 if (epdr != NULL)
1532 free (epdr);
1533 if (rpdr != NULL)
1534 free (rpdr);
1535 if (esym != NULL)
1536 free (esym);
1537 if (ss != NULL)
1538 free (ss);
1539 if (sv != NULL)
1540 free (sv);
b34976b6 1541 return FALSE;
b49e97c9 1542}
738e5348 1543\f
861fb55a
DJ
1544/* We're going to create a stub for H. Create a symbol for the stub's
1545 value and size, to help make the disassembly easier to read. */
1546
1547static bfd_boolean
1548mips_elf_create_stub_symbol (struct bfd_link_info *info,
1549 struct mips_elf_link_hash_entry *h,
1550 const char *prefix, asection *s, bfd_vma value,
1551 bfd_vma size)
1552{
1553 struct bfd_link_hash_entry *bh;
1554 struct elf_link_hash_entry *elfh;
1555 const char *name;
1556
df58fc94
RS
1557 if (ELF_ST_IS_MICROMIPS (h->root.other))
1558 value |= 1;
1559
861fb55a
DJ
1560 /* Create a new symbol. */
1561 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1562 bh = NULL;
1563 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1564 BSF_LOCAL, s, value, NULL,
1565 TRUE, FALSE, &bh))
1566 return FALSE;
1567
1568 /* Make it a local function. */
1569 elfh = (struct elf_link_hash_entry *) bh;
1570 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1571 elfh->size = size;
1572 elfh->forced_local = 1;
1573 return TRUE;
1574}
1575
738e5348
RS
1576/* We're about to redefine H. Create a symbol to represent H's
1577 current value and size, to help make the disassembly easier
1578 to read. */
1579
1580static bfd_boolean
1581mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1582 struct mips_elf_link_hash_entry *h,
1583 const char *prefix)
1584{
1585 struct bfd_link_hash_entry *bh;
1586 struct elf_link_hash_entry *elfh;
1587 const char *name;
1588 asection *s;
1589 bfd_vma value;
1590
1591 /* Read the symbol's value. */
1592 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1593 || h->root.root.type == bfd_link_hash_defweak);
1594 s = h->root.root.u.def.section;
1595 value = h->root.root.u.def.value;
1596
1597 /* Create a new symbol. */
1598 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1599 bh = NULL;
1600 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1601 BSF_LOCAL, s, value, NULL,
1602 TRUE, FALSE, &bh))
1603 return FALSE;
1604
1605 /* Make it local and copy the other attributes from H. */
1606 elfh = (struct elf_link_hash_entry *) bh;
1607 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1608 elfh->other = h->root.other;
1609 elfh->size = h->root.size;
1610 elfh->forced_local = 1;
1611 return TRUE;
1612}
1613
1614/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1615 function rather than to a hard-float stub. */
1616
1617static bfd_boolean
1618section_allows_mips16_refs_p (asection *section)
1619{
1620 const char *name;
1621
1622 name = bfd_get_section_name (section->owner, section);
1623 return (FN_STUB_P (name)
1624 || CALL_STUB_P (name)
1625 || CALL_FP_STUB_P (name)
1626 || strcmp (name, ".pdr") == 0);
1627}
1628
1629/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1630 stub section of some kind. Return the R_SYMNDX of the target
1631 function, or 0 if we can't decide which function that is. */
1632
1633static unsigned long
cb4437b8
MR
1634mips16_stub_symndx (const struct elf_backend_data *bed,
1635 asection *sec ATTRIBUTE_UNUSED,
502e814e 1636 const Elf_Internal_Rela *relocs,
738e5348
RS
1637 const Elf_Internal_Rela *relend)
1638{
cb4437b8 1639 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
738e5348
RS
1640 const Elf_Internal_Rela *rel;
1641
cb4437b8
MR
1642 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1643 one in a compound relocation. */
1644 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
738e5348
RS
1645 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1646 return ELF_R_SYM (sec->owner, rel->r_info);
1647
1648 /* Otherwise trust the first relocation, whatever its kind. This is
1649 the traditional behavior. */
1650 if (relocs < relend)
1651 return ELF_R_SYM (sec->owner, relocs->r_info);
1652
1653 return 0;
1654}
b49e97c9
TS
1655
1656/* Check the mips16 stubs for a particular symbol, and see if we can
1657 discard them. */
1658
861fb55a
DJ
1659static void
1660mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1661 struct mips_elf_link_hash_entry *h)
b49e97c9 1662{
738e5348
RS
1663 /* Dynamic symbols must use the standard call interface, in case other
1664 objects try to call them. */
1665 if (h->fn_stub != NULL
1666 && h->root.dynindx != -1)
1667 {
1668 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1669 h->need_fn_stub = TRUE;
1670 }
1671
b49e97c9
TS
1672 if (h->fn_stub != NULL
1673 && ! h->need_fn_stub)
1674 {
1675 /* We don't need the fn_stub; the only references to this symbol
1676 are 16 bit calls. Clobber the size to 0 to prevent it from
1677 being included in the link. */
eea6121a 1678 h->fn_stub->size = 0;
b49e97c9
TS
1679 h->fn_stub->flags &= ~SEC_RELOC;
1680 h->fn_stub->reloc_count = 0;
1681 h->fn_stub->flags |= SEC_EXCLUDE;
1682 }
1683
1684 if (h->call_stub != NULL
30c09090 1685 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1686 {
1687 /* We don't need the call_stub; this is a 16 bit function, so
1688 calls from other 16 bit functions are OK. Clobber the size
1689 to 0 to prevent it from being included in the link. */
eea6121a 1690 h->call_stub->size = 0;
b49e97c9
TS
1691 h->call_stub->flags &= ~SEC_RELOC;
1692 h->call_stub->reloc_count = 0;
1693 h->call_stub->flags |= SEC_EXCLUDE;
1694 }
1695
1696 if (h->call_fp_stub != NULL
30c09090 1697 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1698 {
1699 /* We don't need the call_stub; this is a 16 bit function, so
1700 calls from other 16 bit functions are OK. Clobber the size
1701 to 0 to prevent it from being included in the link. */
eea6121a 1702 h->call_fp_stub->size = 0;
b49e97c9
TS
1703 h->call_fp_stub->flags &= ~SEC_RELOC;
1704 h->call_fp_stub->reloc_count = 0;
1705 h->call_fp_stub->flags |= SEC_EXCLUDE;
1706 }
861fb55a
DJ
1707}
1708
1709/* Hashtable callbacks for mips_elf_la25_stubs. */
1710
1711static hashval_t
1712mips_elf_la25_stub_hash (const void *entry_)
1713{
1714 const struct mips_elf_la25_stub *entry;
1715
1716 entry = (struct mips_elf_la25_stub *) entry_;
1717 return entry->h->root.root.u.def.section->id
1718 + entry->h->root.root.u.def.value;
1719}
1720
1721static int
1722mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1723{
1724 const struct mips_elf_la25_stub *entry1, *entry2;
1725
1726 entry1 = (struct mips_elf_la25_stub *) entry1_;
1727 entry2 = (struct mips_elf_la25_stub *) entry2_;
1728 return ((entry1->h->root.root.u.def.section
1729 == entry2->h->root.root.u.def.section)
1730 && (entry1->h->root.root.u.def.value
1731 == entry2->h->root.root.u.def.value));
1732}
1733
1734/* Called by the linker to set up the la25 stub-creation code. FN is
1735 the linker's implementation of add_stub_function. Return true on
1736 success. */
1737
1738bfd_boolean
1739_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1740 asection *(*fn) (const char *, asection *,
1741 asection *))
1742{
1743 struct mips_elf_link_hash_table *htab;
1744
1745 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1746 if (htab == NULL)
1747 return FALSE;
1748
861fb55a
DJ
1749 htab->add_stub_section = fn;
1750 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1751 mips_elf_la25_stub_eq, NULL);
1752 if (htab->la25_stubs == NULL)
1753 return FALSE;
1754
1755 return TRUE;
1756}
1757
1758/* Return true if H is a locally-defined PIC function, in the sense
8f0c309a
CLT
1759 that it or its fn_stub might need $25 to be valid on entry.
1760 Note that MIPS16 functions set up $gp using PC-relative instructions,
1761 so they themselves never need $25 to be valid. Only non-MIPS16
1762 entry points are of interest here. */
861fb55a
DJ
1763
1764static bfd_boolean
1765mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1766{
1767 return ((h->root.root.type == bfd_link_hash_defined
1768 || h->root.root.type == bfd_link_hash_defweak)
1769 && h->root.def_regular
1770 && !bfd_is_abs_section (h->root.root.u.def.section)
8f0c309a
CLT
1771 && (!ELF_ST_IS_MIPS16 (h->root.other)
1772 || (h->fn_stub && h->need_fn_stub))
861fb55a
DJ
1773 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1774 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1775}
1776
8f0c309a
CLT
1777/* Set *SEC to the input section that contains the target of STUB.
1778 Return the offset of the target from the start of that section. */
1779
1780static bfd_vma
1781mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1782 asection **sec)
1783{
1784 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1785 {
1786 BFD_ASSERT (stub->h->need_fn_stub);
1787 *sec = stub->h->fn_stub;
1788 return 0;
1789 }
1790 else
1791 {
1792 *sec = stub->h->root.root.u.def.section;
1793 return stub->h->root.root.u.def.value;
1794 }
1795}
1796
861fb55a
DJ
1797/* STUB describes an la25 stub that we have decided to implement
1798 by inserting an LUI/ADDIU pair before the target function.
1799 Create the section and redirect the function symbol to it. */
1800
1801static bfd_boolean
1802mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1803 struct bfd_link_info *info)
1804{
1805 struct mips_elf_link_hash_table *htab;
1806 char *name;
1807 asection *s, *input_section;
1808 unsigned int align;
1809
1810 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1811 if (htab == NULL)
1812 return FALSE;
861fb55a
DJ
1813
1814 /* Create a unique name for the new section. */
1815 name = bfd_malloc (11 + sizeof (".text.stub."));
1816 if (name == NULL)
1817 return FALSE;
1818 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1819
1820 /* Create the section. */
8f0c309a 1821 mips_elf_get_la25_target (stub, &input_section);
861fb55a
DJ
1822 s = htab->add_stub_section (name, input_section,
1823 input_section->output_section);
1824 if (s == NULL)
1825 return FALSE;
1826
1827 /* Make sure that any padding goes before the stub. */
1828 align = input_section->alignment_power;
1829 if (!bfd_set_section_alignment (s->owner, s, align))
1830 return FALSE;
1831 if (align > 3)
1832 s->size = (1 << align) - 8;
1833
1834 /* Create a symbol for the stub. */
1835 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1836 stub->stub_section = s;
1837 stub->offset = s->size;
1838
1839 /* Allocate room for it. */
1840 s->size += 8;
1841 return TRUE;
1842}
1843
1844/* STUB describes an la25 stub that we have decided to implement
1845 with a separate trampoline. Allocate room for it and redirect
1846 the function symbol to it. */
1847
1848static bfd_boolean
1849mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1850 struct bfd_link_info *info)
1851{
1852 struct mips_elf_link_hash_table *htab;
1853 asection *s;
1854
1855 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1856 if (htab == NULL)
1857 return FALSE;
861fb55a
DJ
1858
1859 /* Create a trampoline section, if we haven't already. */
1860 s = htab->strampoline;
1861 if (s == NULL)
1862 {
1863 asection *input_section = stub->h->root.root.u.def.section;
1864 s = htab->add_stub_section (".text", NULL,
1865 input_section->output_section);
1866 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1867 return FALSE;
1868 htab->strampoline = s;
1869 }
1870
1871 /* Create a symbol for the stub. */
1872 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1873 stub->stub_section = s;
1874 stub->offset = s->size;
1875
1876 /* Allocate room for it. */
1877 s->size += 16;
1878 return TRUE;
1879}
1880
1881/* H describes a symbol that needs an la25 stub. Make sure that an
1882 appropriate stub exists and point H at it. */
1883
1884static bfd_boolean
1885mips_elf_add_la25_stub (struct bfd_link_info *info,
1886 struct mips_elf_link_hash_entry *h)
1887{
1888 struct mips_elf_link_hash_table *htab;
1889 struct mips_elf_la25_stub search, *stub;
1890 bfd_boolean use_trampoline_p;
1891 asection *s;
1892 bfd_vma value;
1893 void **slot;
1894
861fb55a
DJ
1895 /* Describe the stub we want. */
1896 search.stub_section = NULL;
1897 search.offset = 0;
1898 search.h = h;
1899
1900 /* See if we've already created an equivalent stub. */
1901 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1902 if (htab == NULL)
1903 return FALSE;
1904
861fb55a
DJ
1905 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1906 if (slot == NULL)
1907 return FALSE;
1908
1909 stub = (struct mips_elf_la25_stub *) *slot;
1910 if (stub != NULL)
1911 {
1912 /* We can reuse the existing stub. */
1913 h->la25_stub = stub;
1914 return TRUE;
1915 }
1916
1917 /* Create a permanent copy of ENTRY and add it to the hash table. */
1918 stub = bfd_malloc (sizeof (search));
1919 if (stub == NULL)
1920 return FALSE;
1921 *stub = search;
1922 *slot = stub;
1923
8f0c309a
CLT
1924 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1925 of the section and if we would need no more than 2 nops. */
1926 value = mips_elf_get_la25_target (stub, &s);
1927 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1928
861fb55a
DJ
1929 h->la25_stub = stub;
1930 return (use_trampoline_p
1931 ? mips_elf_add_la25_trampoline (stub, info)
1932 : mips_elf_add_la25_intro (stub, info));
1933}
1934
1935/* A mips_elf_link_hash_traverse callback that is called before sizing
1936 sections. DATA points to a mips_htab_traverse_info structure. */
1937
1938static bfd_boolean
1939mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1940{
1941 struct mips_htab_traverse_info *hti;
1942
1943 hti = (struct mips_htab_traverse_info *) data;
861fb55a
DJ
1944 if (!hti->info->relocatable)
1945 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1946
861fb55a
DJ
1947 if (mips_elf_local_pic_function_p (h))
1948 {
ba85c43e
NC
1949 /* PR 12845: If H is in a section that has been garbage
1950 collected it will have its output section set to *ABS*. */
1951 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1952 return TRUE;
1953
861fb55a
DJ
1954 /* H is a function that might need $25 to be valid on entry.
1955 If we're creating a non-PIC relocatable object, mark H as
1956 being PIC. If we're creating a non-relocatable object with
1957 non-PIC branches and jumps to H, make sure that H has an la25
1958 stub. */
1959 if (hti->info->relocatable)
1960 {
1961 if (!PIC_OBJECT_P (hti->output_bfd))
1962 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1963 }
1964 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1965 {
1966 hti->error = TRUE;
1967 return FALSE;
1968 }
1969 }
b34976b6 1970 return TRUE;
b49e97c9
TS
1971}
1972\f
d6f16593
MR
1973/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1974 Most mips16 instructions are 16 bits, but these instructions
1975 are 32 bits.
1976
1977 The format of these instructions is:
1978
1979 +--------------+--------------------------------+
1980 | JALX | X| Imm 20:16 | Imm 25:21 |
1981 +--------------+--------------------------------+
1982 | Immediate 15:0 |
1983 +-----------------------------------------------+
1984
1985 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1986 Note that the immediate value in the first word is swapped.
1987
1988 When producing a relocatable object file, R_MIPS16_26 is
1989 handled mostly like R_MIPS_26. In particular, the addend is
1990 stored as a straight 26-bit value in a 32-bit instruction.
1991 (gas makes life simpler for itself by never adjusting a
1992 R_MIPS16_26 reloc to be against a section, so the addend is
1993 always zero). However, the 32 bit instruction is stored as 2
1994 16-bit values, rather than a single 32-bit value. In a
1995 big-endian file, the result is the same; in a little-endian
1996 file, the two 16-bit halves of the 32 bit value are swapped.
1997 This is so that a disassembler can recognize the jal
1998 instruction.
1999
2000 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2001 instruction stored as two 16-bit values. The addend A is the
2002 contents of the targ26 field. The calculation is the same as
2003 R_MIPS_26. When storing the calculated value, reorder the
2004 immediate value as shown above, and don't forget to store the
2005 value as two 16-bit values.
2006
2007 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2008 defined as
2009
2010 big-endian:
2011 +--------+----------------------+
2012 | | |
2013 | | targ26-16 |
2014 |31 26|25 0|
2015 +--------+----------------------+
2016
2017 little-endian:
2018 +----------+------+-------------+
2019 | | | |
2020 | sub1 | | sub2 |
2021 |0 9|10 15|16 31|
2022 +----------+--------------------+
2023 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2024 ((sub1 << 16) | sub2)).
2025
2026 When producing a relocatable object file, the calculation is
2027 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2028 When producing a fully linked file, the calculation is
2029 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2030 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2031
738e5348
RS
2032 The table below lists the other MIPS16 instruction relocations.
2033 Each one is calculated in the same way as the non-MIPS16 relocation
2034 given on the right, but using the extended MIPS16 layout of 16-bit
2035 immediate fields:
2036
2037 R_MIPS16_GPREL R_MIPS_GPREL16
2038 R_MIPS16_GOT16 R_MIPS_GOT16
2039 R_MIPS16_CALL16 R_MIPS_CALL16
2040 R_MIPS16_HI16 R_MIPS_HI16
2041 R_MIPS16_LO16 R_MIPS_LO16
2042
2043 A typical instruction will have a format like this:
d6f16593
MR
2044
2045 +--------------+--------------------------------+
2046 | EXTEND | Imm 10:5 | Imm 15:11 |
2047 +--------------+--------------------------------+
2048 | Major | rx | ry | Imm 4:0 |
2049 +--------------+--------------------------------+
2050
2051 EXTEND is the five bit value 11110. Major is the instruction
2052 opcode.
2053
738e5348
RS
2054 All we need to do here is shuffle the bits appropriately.
2055 As above, the two 16-bit halves must be swapped on a
2056 little-endian system. */
2057
2058static inline bfd_boolean
2059mips16_reloc_p (int r_type)
2060{
2061 switch (r_type)
2062 {
2063 case R_MIPS16_26:
2064 case R_MIPS16_GPREL:
2065 case R_MIPS16_GOT16:
2066 case R_MIPS16_CALL16:
2067 case R_MIPS16_HI16:
2068 case R_MIPS16_LO16:
d0f13682
CLT
2069 case R_MIPS16_TLS_GD:
2070 case R_MIPS16_TLS_LDM:
2071 case R_MIPS16_TLS_DTPREL_HI16:
2072 case R_MIPS16_TLS_DTPREL_LO16:
2073 case R_MIPS16_TLS_GOTTPREL:
2074 case R_MIPS16_TLS_TPREL_HI16:
2075 case R_MIPS16_TLS_TPREL_LO16:
738e5348
RS
2076 return TRUE;
2077
2078 default:
2079 return FALSE;
2080 }
2081}
2082
df58fc94
RS
2083/* Check if a microMIPS reloc. */
2084
2085static inline bfd_boolean
2086micromips_reloc_p (unsigned int r_type)
2087{
2088 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2089}
2090
2091/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2092 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2093 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2094
2095static inline bfd_boolean
2096micromips_reloc_shuffle_p (unsigned int r_type)
2097{
2098 return (micromips_reloc_p (r_type)
2099 && r_type != R_MICROMIPS_PC7_S1
2100 && r_type != R_MICROMIPS_PC10_S1);
2101}
2102
738e5348
RS
2103static inline bfd_boolean
2104got16_reloc_p (int r_type)
2105{
df58fc94
RS
2106 return (r_type == R_MIPS_GOT16
2107 || r_type == R_MIPS16_GOT16
2108 || r_type == R_MICROMIPS_GOT16);
738e5348
RS
2109}
2110
2111static inline bfd_boolean
2112call16_reloc_p (int r_type)
2113{
df58fc94
RS
2114 return (r_type == R_MIPS_CALL16
2115 || r_type == R_MIPS16_CALL16
2116 || r_type == R_MICROMIPS_CALL16);
2117}
2118
2119static inline bfd_boolean
2120got_disp_reloc_p (unsigned int r_type)
2121{
2122 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2123}
2124
2125static inline bfd_boolean
2126got_page_reloc_p (unsigned int r_type)
2127{
2128 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2129}
2130
2131static inline bfd_boolean
2132got_ofst_reloc_p (unsigned int r_type)
2133{
2134 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
2135}
2136
2137static inline bfd_boolean
2138got_hi16_reloc_p (unsigned int r_type)
2139{
2140 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
2141}
2142
2143static inline bfd_boolean
2144got_lo16_reloc_p (unsigned int r_type)
2145{
2146 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2147}
2148
2149static inline bfd_boolean
2150call_hi16_reloc_p (unsigned int r_type)
2151{
2152 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2153}
2154
2155static inline bfd_boolean
2156call_lo16_reloc_p (unsigned int r_type)
2157{
2158 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
738e5348
RS
2159}
2160
2161static inline bfd_boolean
2162hi16_reloc_p (int r_type)
2163{
df58fc94
RS
2164 return (r_type == R_MIPS_HI16
2165 || r_type == R_MIPS16_HI16
2166 || r_type == R_MICROMIPS_HI16);
738e5348 2167}
d6f16593 2168
738e5348
RS
2169static inline bfd_boolean
2170lo16_reloc_p (int r_type)
2171{
df58fc94
RS
2172 return (r_type == R_MIPS_LO16
2173 || r_type == R_MIPS16_LO16
2174 || r_type == R_MICROMIPS_LO16);
738e5348
RS
2175}
2176
2177static inline bfd_boolean
2178mips16_call_reloc_p (int r_type)
2179{
2180 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2181}
d6f16593 2182
38a7df63
CF
2183static inline bfd_boolean
2184jal_reloc_p (int r_type)
2185{
df58fc94
RS
2186 return (r_type == R_MIPS_26
2187 || r_type == R_MIPS16_26
2188 || r_type == R_MICROMIPS_26_S1);
2189}
2190
2191static inline bfd_boolean
2192micromips_branch_reloc_p (int r_type)
2193{
2194 return (r_type == R_MICROMIPS_26_S1
2195 || r_type == R_MICROMIPS_PC16_S1
2196 || r_type == R_MICROMIPS_PC10_S1
2197 || r_type == R_MICROMIPS_PC7_S1);
2198}
2199
2200static inline bfd_boolean
2201tls_gd_reloc_p (unsigned int r_type)
2202{
d0f13682
CLT
2203 return (r_type == R_MIPS_TLS_GD
2204 || r_type == R_MIPS16_TLS_GD
2205 || r_type == R_MICROMIPS_TLS_GD);
df58fc94
RS
2206}
2207
2208static inline bfd_boolean
2209tls_ldm_reloc_p (unsigned int r_type)
2210{
d0f13682
CLT
2211 return (r_type == R_MIPS_TLS_LDM
2212 || r_type == R_MIPS16_TLS_LDM
2213 || r_type == R_MICROMIPS_TLS_LDM);
df58fc94
RS
2214}
2215
2216static inline bfd_boolean
2217tls_gottprel_reloc_p (unsigned int r_type)
2218{
d0f13682
CLT
2219 return (r_type == R_MIPS_TLS_GOTTPREL
2220 || r_type == R_MIPS16_TLS_GOTTPREL
2221 || r_type == R_MICROMIPS_TLS_GOTTPREL);
38a7df63
CF
2222}
2223
d6f16593 2224void
df58fc94
RS
2225_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2226 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2227{
df58fc94 2228 bfd_vma first, second, val;
d6f16593 2229
df58fc94 2230 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2231 return;
2232
df58fc94
RS
2233 /* Pick up the first and second halfwords of the instruction. */
2234 first = bfd_get_16 (abfd, data);
2235 second = bfd_get_16 (abfd, data + 2);
2236 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2237 val = first << 16 | second;
2238 else if (r_type != R_MIPS16_26)
2239 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2240 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
d6f16593 2241 else
df58fc94
RS
2242 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2243 | ((first & 0x1f) << 21) | second);
d6f16593
MR
2244 bfd_put_32 (abfd, val, data);
2245}
2246
2247void
df58fc94
RS
2248_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2249 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2250{
df58fc94 2251 bfd_vma first, second, val;
d6f16593 2252
df58fc94 2253 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2254 return;
2255
2256 val = bfd_get_32 (abfd, data);
df58fc94 2257 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
d6f16593 2258 {
df58fc94
RS
2259 second = val & 0xffff;
2260 first = val >> 16;
2261 }
2262 else if (r_type != R_MIPS16_26)
2263 {
2264 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2265 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
d6f16593
MR
2266 }
2267 else
2268 {
df58fc94
RS
2269 second = val & 0xffff;
2270 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2271 | ((val >> 21) & 0x1f);
d6f16593 2272 }
df58fc94
RS
2273 bfd_put_16 (abfd, second, data + 2);
2274 bfd_put_16 (abfd, first, data);
d6f16593
MR
2275}
2276
b49e97c9 2277bfd_reloc_status_type
9719ad41
RS
2278_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2279 arelent *reloc_entry, asection *input_section,
2280 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
2281{
2282 bfd_vma relocation;
a7ebbfdf 2283 bfd_signed_vma val;
30ac9238 2284 bfd_reloc_status_type status;
b49e97c9
TS
2285
2286 if (bfd_is_com_section (symbol->section))
2287 relocation = 0;
2288 else
2289 relocation = symbol->value;
2290
2291 relocation += symbol->section->output_section->vma;
2292 relocation += symbol->section->output_offset;
2293
07515404 2294 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
2295 return bfd_reloc_outofrange;
2296
b49e97c9 2297 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
2298 val = reloc_entry->addend;
2299
30ac9238 2300 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 2301
b49e97c9 2302 /* Adjust val for the final section location and GP value. If we
1049f94e 2303 are producing relocatable output, we don't want to do this for
b49e97c9 2304 an external symbol. */
1049f94e 2305 if (! relocatable
b49e97c9
TS
2306 || (symbol->flags & BSF_SECTION_SYM) != 0)
2307 val += relocation - gp;
2308
a7ebbfdf
TS
2309 if (reloc_entry->howto->partial_inplace)
2310 {
30ac9238
RS
2311 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2312 (bfd_byte *) data
2313 + reloc_entry->address);
2314 if (status != bfd_reloc_ok)
2315 return status;
a7ebbfdf
TS
2316 }
2317 else
2318 reloc_entry->addend = val;
b49e97c9 2319
1049f94e 2320 if (relocatable)
b49e97c9 2321 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2322
2323 return bfd_reloc_ok;
2324}
2325
2326/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2327 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2328 that contains the relocation field and DATA points to the start of
2329 INPUT_SECTION. */
2330
2331struct mips_hi16
2332{
2333 struct mips_hi16 *next;
2334 bfd_byte *data;
2335 asection *input_section;
2336 arelent rel;
2337};
2338
2339/* FIXME: This should not be a static variable. */
2340
2341static struct mips_hi16 *mips_hi16_list;
2342
2343/* A howto special_function for REL *HI16 relocations. We can only
2344 calculate the correct value once we've seen the partnering
2345 *LO16 relocation, so just save the information for later.
2346
2347 The ABI requires that the *LO16 immediately follow the *HI16.
2348 However, as a GNU extension, we permit an arbitrary number of
2349 *HI16s to be associated with a single *LO16. This significantly
2350 simplies the relocation handling in gcc. */
2351
2352bfd_reloc_status_type
2353_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2354 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2355 asection *input_section, bfd *output_bfd,
2356 char **error_message ATTRIBUTE_UNUSED)
2357{
2358 struct mips_hi16 *n;
2359
07515404 2360 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2361 return bfd_reloc_outofrange;
2362
2363 n = bfd_malloc (sizeof *n);
2364 if (n == NULL)
2365 return bfd_reloc_outofrange;
2366
2367 n->next = mips_hi16_list;
2368 n->data = data;
2369 n->input_section = input_section;
2370 n->rel = *reloc_entry;
2371 mips_hi16_list = n;
2372
2373 if (output_bfd != NULL)
2374 reloc_entry->address += input_section->output_offset;
2375
2376 return bfd_reloc_ok;
2377}
2378
738e5348 2379/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2380 like any other 16-bit relocation when applied to global symbols, but is
2381 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2382
2383bfd_reloc_status_type
2384_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2385 void *data, asection *input_section,
2386 bfd *output_bfd, char **error_message)
2387{
2388 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2389 || bfd_is_und_section (bfd_get_section (symbol))
2390 || bfd_is_com_section (bfd_get_section (symbol)))
2391 /* The relocation is against a global symbol. */
2392 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2393 input_section, output_bfd,
2394 error_message);
2395
2396 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2397 input_section, output_bfd, error_message);
2398}
2399
2400/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2401 is a straightforward 16 bit inplace relocation, but we must deal with
2402 any partnering high-part relocations as well. */
2403
2404bfd_reloc_status_type
2405_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2406 void *data, asection *input_section,
2407 bfd *output_bfd, char **error_message)
2408{
2409 bfd_vma vallo;
d6f16593 2410 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2411
07515404 2412 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2413 return bfd_reloc_outofrange;
2414
df58fc94 2415 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
d6f16593 2416 location);
df58fc94
RS
2417 vallo = bfd_get_32 (abfd, location);
2418 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2419 location);
d6f16593 2420
30ac9238
RS
2421 while (mips_hi16_list != NULL)
2422 {
2423 bfd_reloc_status_type ret;
2424 struct mips_hi16 *hi;
2425
2426 hi = mips_hi16_list;
2427
738e5348
RS
2428 /* R_MIPS*_GOT16 relocations are something of a special case. We
2429 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2430 relocation (with a rightshift of 16). However, since GOT16
2431 relocations can also be used with global symbols, their howto
2432 has a rightshift of 0. */
2433 if (hi->rel.howto->type == R_MIPS_GOT16)
2434 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2435 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2436 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
df58fc94
RS
2437 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2438 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
30ac9238
RS
2439
2440 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2441 carry or borrow will induce a change of +1 or -1 in the high part. */
2442 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2443
30ac9238
RS
2444 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2445 hi->input_section, output_bfd,
2446 error_message);
2447 if (ret != bfd_reloc_ok)
2448 return ret;
2449
2450 mips_hi16_list = hi->next;
2451 free (hi);
2452 }
2453
2454 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2455 input_section, output_bfd,
2456 error_message);
2457}
2458
2459/* A generic howto special_function. This calculates and installs the
2460 relocation itself, thus avoiding the oft-discussed problems in
2461 bfd_perform_relocation and bfd_install_relocation. */
2462
2463bfd_reloc_status_type
2464_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2465 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2466 asection *input_section, bfd *output_bfd,
2467 char **error_message ATTRIBUTE_UNUSED)
2468{
2469 bfd_signed_vma val;
2470 bfd_reloc_status_type status;
2471 bfd_boolean relocatable;
2472
2473 relocatable = (output_bfd != NULL);
2474
07515404 2475 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2476 return bfd_reloc_outofrange;
2477
2478 /* Build up the field adjustment in VAL. */
2479 val = 0;
2480 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2481 {
2482 /* Either we're calculating the final field value or we have a
2483 relocation against a section symbol. Add in the section's
2484 offset or address. */
2485 val += symbol->section->output_section->vma;
2486 val += symbol->section->output_offset;
2487 }
2488
2489 if (!relocatable)
2490 {
2491 /* We're calculating the final field value. Add in the symbol's value
2492 and, if pc-relative, subtract the address of the field itself. */
2493 val += symbol->value;
2494 if (reloc_entry->howto->pc_relative)
2495 {
2496 val -= input_section->output_section->vma;
2497 val -= input_section->output_offset;
2498 val -= reloc_entry->address;
2499 }
2500 }
2501
2502 /* VAL is now the final adjustment. If we're keeping this relocation
2503 in the output file, and if the relocation uses a separate addend,
2504 we just need to add VAL to that addend. Otherwise we need to add
2505 VAL to the relocation field itself. */
2506 if (relocatable && !reloc_entry->howto->partial_inplace)
2507 reloc_entry->addend += val;
2508 else
2509 {
d6f16593
MR
2510 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2511
30ac9238
RS
2512 /* Add in the separate addend, if any. */
2513 val += reloc_entry->addend;
2514
2515 /* Add VAL to the relocation field. */
df58fc94
RS
2516 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2517 location);
30ac9238 2518 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593 2519 location);
df58fc94
RS
2520 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2521 location);
d6f16593 2522
30ac9238
RS
2523 if (status != bfd_reloc_ok)
2524 return status;
2525 }
2526
2527 if (relocatable)
2528 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2529
2530 return bfd_reloc_ok;
2531}
2532\f
2533/* Swap an entry in a .gptab section. Note that these routines rely
2534 on the equivalence of the two elements of the union. */
2535
2536static void
9719ad41
RS
2537bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2538 Elf32_gptab *in)
b49e97c9
TS
2539{
2540 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2541 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2542}
2543
2544static void
9719ad41
RS
2545bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2546 Elf32_External_gptab *ex)
b49e97c9
TS
2547{
2548 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2549 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2550}
2551
2552static void
9719ad41
RS
2553bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2554 Elf32_External_compact_rel *ex)
b49e97c9
TS
2555{
2556 H_PUT_32 (abfd, in->id1, ex->id1);
2557 H_PUT_32 (abfd, in->num, ex->num);
2558 H_PUT_32 (abfd, in->id2, ex->id2);
2559 H_PUT_32 (abfd, in->offset, ex->offset);
2560 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2561 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2562}
2563
2564static void
9719ad41
RS
2565bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2566 Elf32_External_crinfo *ex)
b49e97c9
TS
2567{
2568 unsigned long l;
2569
2570 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2571 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2572 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2573 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2574 H_PUT_32 (abfd, l, ex->info);
2575 H_PUT_32 (abfd, in->konst, ex->konst);
2576 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2577}
b49e97c9
TS
2578\f
2579/* A .reginfo section holds a single Elf32_RegInfo structure. These
2580 routines swap this structure in and out. They are used outside of
2581 BFD, so they are globally visible. */
2582
2583void
9719ad41
RS
2584bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2585 Elf32_RegInfo *in)
b49e97c9
TS
2586{
2587 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2588 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2589 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2590 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2591 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2592 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2593}
2594
2595void
9719ad41
RS
2596bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2597 Elf32_External_RegInfo *ex)
b49e97c9
TS
2598{
2599 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2600 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2601 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2602 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2603 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2604 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2605}
2606
2607/* In the 64 bit ABI, the .MIPS.options section holds register
2608 information in an Elf64_Reginfo structure. These routines swap
2609 them in and out. They are globally visible because they are used
2610 outside of BFD. These routines are here so that gas can call them
2611 without worrying about whether the 64 bit ABI has been included. */
2612
2613void
9719ad41
RS
2614bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2615 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2616{
2617 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2618 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2619 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2620 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2621 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2622 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2623 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2624}
2625
2626void
9719ad41
RS
2627bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2628 Elf64_External_RegInfo *ex)
b49e97c9
TS
2629{
2630 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2631 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2632 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2633 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2634 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2635 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2636 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2637}
2638
2639/* Swap in an options header. */
2640
2641void
9719ad41
RS
2642bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2643 Elf_Internal_Options *in)
b49e97c9
TS
2644{
2645 in->kind = H_GET_8 (abfd, ex->kind);
2646 in->size = H_GET_8 (abfd, ex->size);
2647 in->section = H_GET_16 (abfd, ex->section);
2648 in->info = H_GET_32 (abfd, ex->info);
2649}
2650
2651/* Swap out an options header. */
2652
2653void
9719ad41
RS
2654bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2655 Elf_External_Options *ex)
b49e97c9
TS
2656{
2657 H_PUT_8 (abfd, in->kind, ex->kind);
2658 H_PUT_8 (abfd, in->size, ex->size);
2659 H_PUT_16 (abfd, in->section, ex->section);
2660 H_PUT_32 (abfd, in->info, ex->info);
2661}
2662\f
2663/* This function is called via qsort() to sort the dynamic relocation
2664 entries by increasing r_symndx value. */
2665
2666static int
9719ad41 2667sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2668{
947216bf
AM
2669 Elf_Internal_Rela int_reloc1;
2670 Elf_Internal_Rela int_reloc2;
6870500c 2671 int diff;
b49e97c9 2672
947216bf
AM
2673 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2674 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2675
6870500c
RS
2676 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2677 if (diff != 0)
2678 return diff;
2679
2680 if (int_reloc1.r_offset < int_reloc2.r_offset)
2681 return -1;
2682 if (int_reloc1.r_offset > int_reloc2.r_offset)
2683 return 1;
2684 return 0;
b49e97c9
TS
2685}
2686
f4416af6
AO
2687/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2688
2689static int
7e3102a7
AM
2690sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2691 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2692{
7e3102a7 2693#ifdef BFD64
f4416af6
AO
2694 Elf_Internal_Rela int_reloc1[3];
2695 Elf_Internal_Rela int_reloc2[3];
2696
2697 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2698 (reldyn_sorting_bfd, arg1, int_reloc1);
2699 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2700 (reldyn_sorting_bfd, arg2, int_reloc2);
2701
6870500c
RS
2702 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2703 return -1;
2704 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2705 return 1;
2706
2707 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2708 return -1;
2709 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2710 return 1;
2711 return 0;
7e3102a7
AM
2712#else
2713 abort ();
2714#endif
f4416af6
AO
2715}
2716
2717
b49e97c9
TS
2718/* This routine is used to write out ECOFF debugging external symbol
2719 information. It is called via mips_elf_link_hash_traverse. The
2720 ECOFF external symbol information must match the ELF external
2721 symbol information. Unfortunately, at this point we don't know
2722 whether a symbol is required by reloc information, so the two
2723 tables may wind up being different. We must sort out the external
2724 symbol information before we can set the final size of the .mdebug
2725 section, and we must set the size of the .mdebug section before we
2726 can relocate any sections, and we can't know which symbols are
2727 required by relocation until we relocate the sections.
2728 Fortunately, it is relatively unlikely that any symbol will be
2729 stripped but required by a reloc. In particular, it can not happen
2730 when generating a final executable. */
2731
b34976b6 2732static bfd_boolean
9719ad41 2733mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2734{
9719ad41 2735 struct extsym_info *einfo = data;
b34976b6 2736 bfd_boolean strip;
b49e97c9
TS
2737 asection *sec, *output_section;
2738
b49e97c9 2739 if (h->root.indx == -2)
b34976b6 2740 strip = FALSE;
f5385ebf 2741 else if ((h->root.def_dynamic
77cfaee6
AM
2742 || h->root.ref_dynamic
2743 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2744 && !h->root.def_regular
2745 && !h->root.ref_regular)
b34976b6 2746 strip = TRUE;
b49e97c9
TS
2747 else if (einfo->info->strip == strip_all
2748 || (einfo->info->strip == strip_some
2749 && bfd_hash_lookup (einfo->info->keep_hash,
2750 h->root.root.root.string,
b34976b6
AM
2751 FALSE, FALSE) == NULL))
2752 strip = TRUE;
b49e97c9 2753 else
b34976b6 2754 strip = FALSE;
b49e97c9
TS
2755
2756 if (strip)
b34976b6 2757 return TRUE;
b49e97c9
TS
2758
2759 if (h->esym.ifd == -2)
2760 {
2761 h->esym.jmptbl = 0;
2762 h->esym.cobol_main = 0;
2763 h->esym.weakext = 0;
2764 h->esym.reserved = 0;
2765 h->esym.ifd = ifdNil;
2766 h->esym.asym.value = 0;
2767 h->esym.asym.st = stGlobal;
2768
2769 if (h->root.root.type == bfd_link_hash_undefined
2770 || h->root.root.type == bfd_link_hash_undefweak)
2771 {
2772 const char *name;
2773
2774 /* Use undefined class. Also, set class and type for some
2775 special symbols. */
2776 name = h->root.root.root.string;
2777 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2778 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2779 {
2780 h->esym.asym.sc = scData;
2781 h->esym.asym.st = stLabel;
2782 h->esym.asym.value = 0;
2783 }
2784 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2785 {
2786 h->esym.asym.sc = scAbs;
2787 h->esym.asym.st = stLabel;
2788 h->esym.asym.value =
2789 mips_elf_hash_table (einfo->info)->procedure_count;
2790 }
4a14403c 2791 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2792 {
2793 h->esym.asym.sc = scAbs;
2794 h->esym.asym.st = stLabel;
2795 h->esym.asym.value = elf_gp (einfo->abfd);
2796 }
2797 else
2798 h->esym.asym.sc = scUndefined;
2799 }
2800 else if (h->root.root.type != bfd_link_hash_defined
2801 && h->root.root.type != bfd_link_hash_defweak)
2802 h->esym.asym.sc = scAbs;
2803 else
2804 {
2805 const char *name;
2806
2807 sec = h->root.root.u.def.section;
2808 output_section = sec->output_section;
2809
2810 /* When making a shared library and symbol h is the one from
2811 the another shared library, OUTPUT_SECTION may be null. */
2812 if (output_section == NULL)
2813 h->esym.asym.sc = scUndefined;
2814 else
2815 {
2816 name = bfd_section_name (output_section->owner, output_section);
2817
2818 if (strcmp (name, ".text") == 0)
2819 h->esym.asym.sc = scText;
2820 else if (strcmp (name, ".data") == 0)
2821 h->esym.asym.sc = scData;
2822 else if (strcmp (name, ".sdata") == 0)
2823 h->esym.asym.sc = scSData;
2824 else if (strcmp (name, ".rodata") == 0
2825 || strcmp (name, ".rdata") == 0)
2826 h->esym.asym.sc = scRData;
2827 else if (strcmp (name, ".bss") == 0)
2828 h->esym.asym.sc = scBss;
2829 else if (strcmp (name, ".sbss") == 0)
2830 h->esym.asym.sc = scSBss;
2831 else if (strcmp (name, ".init") == 0)
2832 h->esym.asym.sc = scInit;
2833 else if (strcmp (name, ".fini") == 0)
2834 h->esym.asym.sc = scFini;
2835 else
2836 h->esym.asym.sc = scAbs;
2837 }
2838 }
2839
2840 h->esym.asym.reserved = 0;
2841 h->esym.asym.index = indexNil;
2842 }
2843
2844 if (h->root.root.type == bfd_link_hash_common)
2845 h->esym.asym.value = h->root.root.u.c.size;
2846 else if (h->root.root.type == bfd_link_hash_defined
2847 || h->root.root.type == bfd_link_hash_defweak)
2848 {
2849 if (h->esym.asym.sc == scCommon)
2850 h->esym.asym.sc = scBss;
2851 else if (h->esym.asym.sc == scSCommon)
2852 h->esym.asym.sc = scSBss;
2853
2854 sec = h->root.root.u.def.section;
2855 output_section = sec->output_section;
2856 if (output_section != NULL)
2857 h->esym.asym.value = (h->root.root.u.def.value
2858 + sec->output_offset
2859 + output_section->vma);
2860 else
2861 h->esym.asym.value = 0;
2862 }
33bb52fb 2863 else
b49e97c9
TS
2864 {
2865 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2866
2867 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2868 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2869
33bb52fb 2870 if (hd->needs_lazy_stub)
b49e97c9 2871 {
1bbce132
MR
2872 BFD_ASSERT (hd->root.plt.plist != NULL);
2873 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
b49e97c9
TS
2874 /* Set type and value for a symbol with a function stub. */
2875 h->esym.asym.st = stProc;
2876 sec = hd->root.root.u.def.section;
2877 if (sec == NULL)
2878 h->esym.asym.value = 0;
2879 else
2880 {
2881 output_section = sec->output_section;
2882 if (output_section != NULL)
1bbce132 2883 h->esym.asym.value = (hd->root.plt.plist->stub_offset
b49e97c9
TS
2884 + sec->output_offset
2885 + output_section->vma);
2886 else
2887 h->esym.asym.value = 0;
2888 }
b49e97c9
TS
2889 }
2890 }
2891
2892 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2893 h->root.root.root.string,
2894 &h->esym))
2895 {
b34976b6
AM
2896 einfo->failed = TRUE;
2897 return FALSE;
b49e97c9
TS
2898 }
2899
b34976b6 2900 return TRUE;
b49e97c9
TS
2901}
2902
2903/* A comparison routine used to sort .gptab entries. */
2904
2905static int
9719ad41 2906gptab_compare (const void *p1, const void *p2)
b49e97c9 2907{
9719ad41
RS
2908 const Elf32_gptab *a1 = p1;
2909 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2910
2911 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2912}
2913\f
b15e6682 2914/* Functions to manage the got entry hash table. */
f4416af6
AO
2915
2916/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2917 hash number. */
2918
2919static INLINE hashval_t
9719ad41 2920mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2921{
2922#ifdef BFD64
2923 return addr + (addr >> 32);
2924#else
2925 return addr;
2926#endif
2927}
2928
f4416af6 2929static hashval_t
d9bf376d 2930mips_elf_got_entry_hash (const void *entry_)
f4416af6
AO
2931{
2932 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2933
e641e783 2934 return (entry->symndx
9ab066b4
RS
2935 + ((entry->tls_type == GOT_TLS_LDM) << 18)
2936 + (entry->tls_type == GOT_TLS_LDM ? 0
e641e783
RS
2937 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2938 : entry->symndx >= 0 ? (entry->abfd->id
2939 + mips_elf_hash_bfd_vma (entry->d.addend))
2940 : entry->d.h->root.root.root.hash));
f4416af6
AO
2941}
2942
2943static int
3dff0dd1 2944mips_elf_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2945{
2946 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2947 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2948
e641e783 2949 return (e1->symndx == e2->symndx
9ab066b4
RS
2950 && e1->tls_type == e2->tls_type
2951 && (e1->tls_type == GOT_TLS_LDM ? TRUE
e641e783
RS
2952 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
2953 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
2954 && e1->d.addend == e2->d.addend)
2955 : e2->abfd && e1->d.h == e2->d.h));
b15e6682 2956}
c224138d 2957
13db6b44
RS
2958static hashval_t
2959mips_got_page_ref_hash (const void *ref_)
2960{
2961 const struct mips_got_page_ref *ref;
2962
2963 ref = (const struct mips_got_page_ref *) ref_;
2964 return ((ref->symndx >= 0
2965 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
2966 : ref->u.h->root.root.root.hash)
2967 + mips_elf_hash_bfd_vma (ref->addend));
2968}
2969
2970static int
2971mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
2972{
2973 const struct mips_got_page_ref *ref1, *ref2;
2974
2975 ref1 = (const struct mips_got_page_ref *) ref1_;
2976 ref2 = (const struct mips_got_page_ref *) ref2_;
2977 return (ref1->symndx == ref2->symndx
2978 && (ref1->symndx < 0
2979 ? ref1->u.h == ref2->u.h
2980 : ref1->u.abfd == ref2->u.abfd)
2981 && ref1->addend == ref2->addend);
2982}
2983
c224138d
RS
2984static hashval_t
2985mips_got_page_entry_hash (const void *entry_)
2986{
2987 const struct mips_got_page_entry *entry;
2988
2989 entry = (const struct mips_got_page_entry *) entry_;
13db6b44 2990 return entry->sec->id;
c224138d
RS
2991}
2992
2993static int
2994mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2995{
2996 const struct mips_got_page_entry *entry1, *entry2;
2997
2998 entry1 = (const struct mips_got_page_entry *) entry1_;
2999 entry2 = (const struct mips_got_page_entry *) entry2_;
13db6b44 3000 return entry1->sec == entry2->sec;
c224138d 3001}
b15e6682 3002\f
3dff0dd1 3003/* Create and return a new mips_got_info structure. */
5334aa52
RS
3004
3005static struct mips_got_info *
3dff0dd1 3006mips_elf_create_got_info (bfd *abfd)
5334aa52
RS
3007{
3008 struct mips_got_info *g;
3009
3010 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3011 if (g == NULL)
3012 return NULL;
3013
3dff0dd1
RS
3014 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3015 mips_elf_got_entry_eq, NULL);
5334aa52
RS
3016 if (g->got_entries == NULL)
3017 return NULL;
3018
13db6b44
RS
3019 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3020 mips_got_page_ref_eq, NULL);
3021 if (g->got_page_refs == NULL)
5334aa52
RS
3022 return NULL;
3023
3024 return g;
3025}
3026
ee227692
RS
3027/* Return the GOT info for input bfd ABFD, trying to create a new one if
3028 CREATE_P and if ABFD doesn't already have a GOT. */
3029
3030static struct mips_got_info *
3031mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3032{
3033 struct mips_elf_obj_tdata *tdata;
3034
3035 if (!is_mips_elf (abfd))
3036 return NULL;
3037
3038 tdata = mips_elf_tdata (abfd);
3039 if (!tdata->got && create_p)
3dff0dd1 3040 tdata->got = mips_elf_create_got_info (abfd);
ee227692
RS
3041 return tdata->got;
3042}
3043
d7206569
RS
3044/* Record that ABFD should use output GOT G. */
3045
3046static void
3047mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3048{
3049 struct mips_elf_obj_tdata *tdata;
3050
3051 BFD_ASSERT (is_mips_elf (abfd));
3052 tdata = mips_elf_tdata (abfd);
3053 if (tdata->got)
3054 {
3055 /* The GOT structure itself and the hash table entries are
3056 allocated to a bfd, but the hash tables aren't. */
3057 htab_delete (tdata->got->got_entries);
13db6b44
RS
3058 htab_delete (tdata->got->got_page_refs);
3059 if (tdata->got->got_page_entries)
3060 htab_delete (tdata->got->got_page_entries);
d7206569
RS
3061 }
3062 tdata->got = g;
3063}
3064
0a44bf69
RS
3065/* Return the dynamic relocation section. If it doesn't exist, try to
3066 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3067 if creation fails. */
f4416af6
AO
3068
3069static asection *
0a44bf69 3070mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 3071{
0a44bf69 3072 const char *dname;
f4416af6 3073 asection *sreloc;
0a44bf69 3074 bfd *dynobj;
f4416af6 3075
0a44bf69
RS
3076 dname = MIPS_ELF_REL_DYN_NAME (info);
3077 dynobj = elf_hash_table (info)->dynobj;
3d4d4302 3078 sreloc = bfd_get_linker_section (dynobj, dname);
f4416af6
AO
3079 if (sreloc == NULL && create_p)
3080 {
3d4d4302
AM
3081 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3082 (SEC_ALLOC
3083 | SEC_LOAD
3084 | SEC_HAS_CONTENTS
3085 | SEC_IN_MEMORY
3086 | SEC_LINKER_CREATED
3087 | SEC_READONLY));
f4416af6 3088 if (sreloc == NULL
f4416af6 3089 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 3090 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
3091 return NULL;
3092 }
3093 return sreloc;
3094}
3095
e641e783
RS
3096/* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3097
3098static int
3099mips_elf_reloc_tls_type (unsigned int r_type)
3100{
3101 if (tls_gd_reloc_p (r_type))
3102 return GOT_TLS_GD;
3103
3104 if (tls_ldm_reloc_p (r_type))
3105 return GOT_TLS_LDM;
3106
3107 if (tls_gottprel_reloc_p (r_type))
3108 return GOT_TLS_IE;
3109
9ab066b4 3110 return GOT_TLS_NONE;
e641e783
RS
3111}
3112
3113/* Return the number of GOT slots needed for GOT TLS type TYPE. */
3114
3115static int
3116mips_tls_got_entries (unsigned int type)
3117{
3118 switch (type)
3119 {
3120 case GOT_TLS_GD:
3121 case GOT_TLS_LDM:
3122 return 2;
3123
3124 case GOT_TLS_IE:
3125 return 1;
3126
9ab066b4 3127 case GOT_TLS_NONE:
e641e783
RS
3128 return 0;
3129 }
3130 abort ();
3131}
3132
0f20cc35
DJ
3133/* Count the number of relocations needed for a TLS GOT entry, with
3134 access types from TLS_TYPE, and symbol H (or a local symbol if H
3135 is NULL). */
3136
3137static int
3138mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3139 struct elf_link_hash_entry *h)
3140{
3141 int indx = 0;
0f20cc35
DJ
3142 bfd_boolean need_relocs = FALSE;
3143 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3144
3145 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3146 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
3147 indx = h->dynindx;
3148
3149 if ((info->shared || indx != 0)
3150 && (h == NULL
3151 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3152 || h->root.type != bfd_link_hash_undefweak))
3153 need_relocs = TRUE;
3154
3155 if (!need_relocs)
e641e783 3156 return 0;
0f20cc35 3157
9ab066b4 3158 switch (tls_type)
0f20cc35 3159 {
e641e783
RS
3160 case GOT_TLS_GD:
3161 return indx != 0 ? 2 : 1;
0f20cc35 3162
e641e783
RS
3163 case GOT_TLS_IE:
3164 return 1;
0f20cc35 3165
e641e783
RS
3166 case GOT_TLS_LDM:
3167 return info->shared ? 1 : 0;
0f20cc35 3168
e641e783
RS
3169 default:
3170 return 0;
3171 }
0f20cc35
DJ
3172}
3173
ab361d49
RS
3174/* Add the number of GOT entries and TLS relocations required by ENTRY
3175 to G. */
0f20cc35 3176
ab361d49
RS
3177static void
3178mips_elf_count_got_entry (struct bfd_link_info *info,
3179 struct mips_got_info *g,
3180 struct mips_got_entry *entry)
0f20cc35 3181{
9ab066b4 3182 if (entry->tls_type)
ab361d49 3183 {
9ab066b4
RS
3184 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3185 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
ab361d49
RS
3186 entry->symndx < 0
3187 ? &entry->d.h->root : NULL);
3188 }
3189 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3190 g->local_gotno += 1;
3191 else
3192 g->global_gotno += 1;
0f20cc35
DJ
3193}
3194
0f20cc35
DJ
3195/* Output a simple dynamic relocation into SRELOC. */
3196
3197static void
3198mips_elf_output_dynamic_relocation (bfd *output_bfd,
3199 asection *sreloc,
861fb55a 3200 unsigned long reloc_index,
0f20cc35
DJ
3201 unsigned long indx,
3202 int r_type,
3203 bfd_vma offset)
3204{
3205 Elf_Internal_Rela rel[3];
3206
3207 memset (rel, 0, sizeof (rel));
3208
3209 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3210 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3211
3212 if (ABI_64_P (output_bfd))
3213 {
3214 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3215 (output_bfd, &rel[0],
3216 (sreloc->contents
861fb55a 3217 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
3218 }
3219 else
3220 bfd_elf32_swap_reloc_out
3221 (output_bfd, &rel[0],
3222 (sreloc->contents
861fb55a 3223 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
3224}
3225
3226/* Initialize a set of TLS GOT entries for one symbol. */
3227
3228static void
9ab066b4
RS
3229mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3230 struct mips_got_entry *entry,
0f20cc35
DJ
3231 struct mips_elf_link_hash_entry *h,
3232 bfd_vma value)
3233{
23cc69b6 3234 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
3235 int indx;
3236 asection *sreloc, *sgot;
9ab066b4 3237 bfd_vma got_offset, got_offset2;
0f20cc35
DJ
3238 bfd_boolean need_relocs = FALSE;
3239
23cc69b6 3240 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3241 if (htab == NULL)
3242 return;
3243
23cc69b6 3244 sgot = htab->sgot;
0f20cc35
DJ
3245
3246 indx = 0;
3247 if (h != NULL)
3248 {
3249 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3250
3251 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3252 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3253 indx = h->root.dynindx;
3254 }
3255
9ab066b4 3256 if (entry->tls_initialized)
0f20cc35
DJ
3257 return;
3258
3259 if ((info->shared || indx != 0)
3260 && (h == NULL
3261 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3262 || h->root.type != bfd_link_hash_undefweak))
3263 need_relocs = TRUE;
3264
3265 /* MINUS_ONE means the symbol is not defined in this object. It may not
3266 be defined at all; assume that the value doesn't matter in that
3267 case. Otherwise complain if we would use the value. */
3268 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3269 || h->root.root.type == bfd_link_hash_undefweak);
3270
3271 /* Emit necessary relocations. */
0a44bf69 3272 sreloc = mips_elf_rel_dyn_section (info, FALSE);
9ab066b4 3273 got_offset = entry->gotidx;
0f20cc35 3274
9ab066b4 3275 switch (entry->tls_type)
0f20cc35 3276 {
e641e783
RS
3277 case GOT_TLS_GD:
3278 /* General Dynamic. */
3279 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
0f20cc35
DJ
3280
3281 if (need_relocs)
3282 {
3283 mips_elf_output_dynamic_relocation
861fb55a 3284 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3285 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
e641e783 3286 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3287
3288 if (indx)
3289 mips_elf_output_dynamic_relocation
861fb55a 3290 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3291 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
e641e783 3292 sgot->output_offset + sgot->output_section->vma + got_offset2);
0f20cc35
DJ
3293 else
3294 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3295 sgot->contents + got_offset2);
0f20cc35
DJ
3296 }
3297 else
3298 {
3299 MIPS_ELF_PUT_WORD (abfd, 1,
e641e783 3300 sgot->contents + got_offset);
0f20cc35 3301 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3302 sgot->contents + got_offset2);
0f20cc35 3303 }
e641e783 3304 break;
0f20cc35 3305
e641e783
RS
3306 case GOT_TLS_IE:
3307 /* Initial Exec model. */
0f20cc35
DJ
3308 if (need_relocs)
3309 {
3310 if (indx == 0)
3311 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
e641e783 3312 sgot->contents + got_offset);
0f20cc35
DJ
3313 else
3314 MIPS_ELF_PUT_WORD (abfd, 0,
e641e783 3315 sgot->contents + got_offset);
0f20cc35
DJ
3316
3317 mips_elf_output_dynamic_relocation
861fb55a 3318 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3319 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
e641e783 3320 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3321 }
3322 else
3323 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
e641e783
RS
3324 sgot->contents + got_offset);
3325 break;
0f20cc35 3326
e641e783 3327 case GOT_TLS_LDM:
0f20cc35
DJ
3328 /* The initial offset is zero, and the LD offsets will include the
3329 bias by DTP_OFFSET. */
3330 MIPS_ELF_PUT_WORD (abfd, 0,
3331 sgot->contents + got_offset
3332 + MIPS_ELF_GOT_SIZE (abfd));
3333
3334 if (!info->shared)
3335 MIPS_ELF_PUT_WORD (abfd, 1,
3336 sgot->contents + got_offset);
3337 else
3338 mips_elf_output_dynamic_relocation
861fb55a 3339 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3340 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3341 sgot->output_offset + sgot->output_section->vma + got_offset);
e641e783
RS
3342 break;
3343
3344 default:
3345 abort ();
0f20cc35
DJ
3346 }
3347
9ab066b4 3348 entry->tls_initialized = TRUE;
e641e783 3349}
0f20cc35 3350
0a44bf69
RS
3351/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3352 for global symbol H. .got.plt comes before the GOT, so the offset
3353 will be negative. */
3354
3355static bfd_vma
3356mips_elf_gotplt_index (struct bfd_link_info *info,
3357 struct elf_link_hash_entry *h)
3358{
1bbce132 3359 bfd_vma got_address, got_value;
0a44bf69
RS
3360 struct mips_elf_link_hash_table *htab;
3361
3362 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3363 BFD_ASSERT (htab != NULL);
3364
1bbce132
MR
3365 BFD_ASSERT (h->plt.plist != NULL);
3366 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
0a44bf69
RS
3367
3368 /* Calculate the address of the associated .got.plt entry. */
3369 got_address = (htab->sgotplt->output_section->vma
3370 + htab->sgotplt->output_offset
1bbce132
MR
3371 + (h->plt.plist->gotplt_index
3372 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
0a44bf69
RS
3373
3374 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3375 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3376 + htab->root.hgot->root.u.def.section->output_offset
3377 + htab->root.hgot->root.u.def.value);
3378
3379 return got_address - got_value;
3380}
3381
5c18022e 3382/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3383 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3384 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3385 offset can be found. */
b49e97c9
TS
3386
3387static bfd_vma
9719ad41 3388mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3389 bfd_vma value, unsigned long r_symndx,
0f20cc35 3390 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3391{
a8028dd0 3392 struct mips_elf_link_hash_table *htab;
b15e6682 3393 struct mips_got_entry *entry;
b49e97c9 3394
a8028dd0 3395 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3396 BFD_ASSERT (htab != NULL);
3397
a8028dd0
RS
3398 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3399 r_symndx, h, r_type);
0f20cc35 3400 if (!entry)
b15e6682 3401 return MINUS_ONE;
0f20cc35 3402
e641e783 3403 if (entry->tls_type)
9ab066b4
RS
3404 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3405 return entry->gotidx;
b49e97c9
TS
3406}
3407
13fbec83 3408/* Return the GOT index of global symbol H in the primary GOT. */
b49e97c9
TS
3409
3410static bfd_vma
13fbec83
RS
3411mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3412 struct elf_link_hash_entry *h)
3413{
3414 struct mips_elf_link_hash_table *htab;
3415 long global_got_dynindx;
3416 struct mips_got_info *g;
3417 bfd_vma got_index;
3418
3419 htab = mips_elf_hash_table (info);
3420 BFD_ASSERT (htab != NULL);
3421
3422 global_got_dynindx = 0;
3423 if (htab->global_gotsym != NULL)
3424 global_got_dynindx = htab->global_gotsym->dynindx;
3425
3426 /* Once we determine the global GOT entry with the lowest dynamic
3427 symbol table index, we must put all dynamic symbols with greater
3428 indices into the primary GOT. That makes it easy to calculate the
3429 GOT offset. */
3430 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3431 g = mips_elf_bfd_got (obfd, FALSE);
3432 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3433 * MIPS_ELF_GOT_SIZE (obfd));
3434 BFD_ASSERT (got_index < htab->sgot->size);
3435
3436 return got_index;
3437}
3438
3439/* Return the GOT index for the global symbol indicated by H, which is
3440 referenced by a relocation of type R_TYPE in IBFD. */
3441
3442static bfd_vma
3443mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3444 struct elf_link_hash_entry *h, int r_type)
b49e97c9 3445{
a8028dd0 3446 struct mips_elf_link_hash_table *htab;
6c42ddb9
RS
3447 struct mips_got_info *g;
3448 struct mips_got_entry lookup, *entry;
3449 bfd_vma gotidx;
b49e97c9 3450
a8028dd0 3451 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3452 BFD_ASSERT (htab != NULL);
3453
6c42ddb9
RS
3454 g = mips_elf_bfd_got (ibfd, FALSE);
3455 BFD_ASSERT (g);
f4416af6 3456
6c42ddb9
RS
3457 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3458 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3459 return mips_elf_primary_global_got_index (obfd, info, h);
f4416af6 3460
6c42ddb9
RS
3461 lookup.abfd = ibfd;
3462 lookup.symndx = -1;
3463 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3464 entry = htab_find (g->got_entries, &lookup);
3465 BFD_ASSERT (entry);
0f20cc35 3466
6c42ddb9
RS
3467 gotidx = entry->gotidx;
3468 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
f4416af6 3469
6c42ddb9 3470 if (lookup.tls_type)
0f20cc35 3471 {
0f20cc35
DJ
3472 bfd_vma value = MINUS_ONE;
3473
3474 if ((h->root.type == bfd_link_hash_defined
3475 || h->root.type == bfd_link_hash_defweak)
3476 && h->root.u.def.section->output_section)
3477 value = (h->root.u.def.value
3478 + h->root.u.def.section->output_offset
3479 + h->root.u.def.section->output_section->vma);
3480
9ab066b4 3481 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
0f20cc35 3482 }
6c42ddb9 3483 return gotidx;
b49e97c9
TS
3484}
3485
5c18022e
RS
3486/* Find a GOT page entry that points to within 32KB of VALUE. These
3487 entries are supposed to be placed at small offsets in the GOT, i.e.,
3488 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3489 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3490 offset of the GOT entry from VALUE. */
b49e97c9
TS
3491
3492static bfd_vma
9719ad41 3493mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3494 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3495{
91d6fa6a 3496 bfd_vma page, got_index;
b15e6682 3497 struct mips_got_entry *entry;
b49e97c9 3498
0a44bf69 3499 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3500 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3501 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3502
b15e6682
AO
3503 if (!entry)
3504 return MINUS_ONE;
143d77c5 3505
91d6fa6a 3506 got_index = entry->gotidx;
b49e97c9
TS
3507
3508 if (offsetp)
f4416af6 3509 *offsetp = value - entry->d.address;
b49e97c9 3510
91d6fa6a 3511 return got_index;
b49e97c9
TS
3512}
3513
738e5348 3514/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3515 EXTERNAL is true if the relocation was originally against a global
3516 symbol that binds locally. */
b49e97c9
TS
3517
3518static bfd_vma
9719ad41 3519mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3520 bfd_vma value, bfd_boolean external)
b49e97c9 3521{
b15e6682 3522 struct mips_got_entry *entry;
b49e97c9 3523
0a44bf69
RS
3524 /* GOT16 relocations against local symbols are followed by a LO16
3525 relocation; those against global symbols are not. Thus if the
3526 symbol was originally local, the GOT16 relocation should load the
3527 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3528 if (! external)
0a44bf69 3529 value = mips_elf_high (value) << 16;
b49e97c9 3530
738e5348
RS
3531 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3532 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3533 same in all cases. */
a8028dd0
RS
3534 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3535 NULL, R_MIPS_GOT16);
b15e6682
AO
3536 if (entry)
3537 return entry->gotidx;
3538 else
3539 return MINUS_ONE;
b49e97c9
TS
3540}
3541
3542/* Returns the offset for the entry at the INDEXth position
3543 in the GOT. */
3544
3545static bfd_vma
a8028dd0 3546mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3547 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3548{
a8028dd0 3549 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3550 asection *sgot;
3551 bfd_vma gp;
3552
a8028dd0 3553 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3554 BFD_ASSERT (htab != NULL);
3555
a8028dd0 3556 sgot = htab->sgot;
f4416af6 3557 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3558 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3559
91d6fa6a 3560 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3561}
3562
0a44bf69
RS
3563/* Create and return a local GOT entry for VALUE, which was calculated
3564 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3565 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3566 instead. */
b49e97c9 3567
b15e6682 3568static struct mips_got_entry *
0a44bf69 3569mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3570 bfd *ibfd, bfd_vma value,
5c18022e 3571 unsigned long r_symndx,
0f20cc35
DJ
3572 struct mips_elf_link_hash_entry *h,
3573 int r_type)
b49e97c9 3574{
ebc53538
RS
3575 struct mips_got_entry lookup, *entry;
3576 void **loc;
f4416af6 3577 struct mips_got_info *g;
0a44bf69 3578 struct mips_elf_link_hash_table *htab;
6c42ddb9 3579 bfd_vma gotidx;
0a44bf69
RS
3580
3581 htab = mips_elf_hash_table (info);
4dfe6ac6 3582 BFD_ASSERT (htab != NULL);
b15e6682 3583
d7206569 3584 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
3585 if (g == NULL)
3586 {
d7206569 3587 g = mips_elf_bfd_got (abfd, FALSE);
f4416af6
AO
3588 BFD_ASSERT (g != NULL);
3589 }
b15e6682 3590
020d7251
RS
3591 /* This function shouldn't be called for symbols that live in the global
3592 area of the GOT. */
3593 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35 3594
ebc53538
RS
3595 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3596 if (lookup.tls_type)
3597 {
3598 lookup.abfd = ibfd;
df58fc94 3599 if (tls_ldm_reloc_p (r_type))
0f20cc35 3600 {
ebc53538
RS
3601 lookup.symndx = 0;
3602 lookup.d.addend = 0;
0f20cc35
DJ
3603 }
3604 else if (h == NULL)
3605 {
ebc53538
RS
3606 lookup.symndx = r_symndx;
3607 lookup.d.addend = 0;
0f20cc35
DJ
3608 }
3609 else
ebc53538
RS
3610 {
3611 lookup.symndx = -1;
3612 lookup.d.h = h;
3613 }
0f20cc35 3614
ebc53538
RS
3615 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3616 BFD_ASSERT (entry);
0f20cc35 3617
6c42ddb9
RS
3618 gotidx = entry->gotidx;
3619 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3620
ebc53538 3621 return entry;
0f20cc35
DJ
3622 }
3623
ebc53538
RS
3624 lookup.abfd = NULL;
3625 lookup.symndx = -1;
3626 lookup.d.address = value;
3627 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3628 if (!loc)
b15e6682 3629 return NULL;
143d77c5 3630
ebc53538
RS
3631 entry = (struct mips_got_entry *) *loc;
3632 if (entry)
3633 return entry;
b15e6682 3634
ebc53538 3635 if (g->assigned_gotno >= g->local_gotno)
b49e97c9
TS
3636 {
3637 /* We didn't allocate enough space in the GOT. */
3638 (*_bfd_error_handler)
3639 (_("not enough GOT space for local GOT entries"));
3640 bfd_set_error (bfd_error_bad_value);
b15e6682 3641 return NULL;
b49e97c9
TS
3642 }
3643
ebc53538
RS
3644 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3645 if (!entry)
3646 return NULL;
3647
3648 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3649 *entry = lookup;
3650 *loc = entry;
3651
3652 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
b15e6682 3653
5c18022e 3654 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3655 if (htab->is_vxworks)
3656 {
3657 Elf_Internal_Rela outrel;
5c18022e 3658 asection *s;
91d6fa6a 3659 bfd_byte *rloc;
0a44bf69 3660 bfd_vma got_address;
0a44bf69
RS
3661
3662 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3663 got_address = (htab->sgot->output_section->vma
3664 + htab->sgot->output_offset
ebc53538 3665 + entry->gotidx);
0a44bf69 3666
91d6fa6a 3667 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3668 outrel.r_offset = got_address;
5c18022e
RS
3669 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3670 outrel.r_addend = value;
91d6fa6a 3671 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3672 }
3673
ebc53538 3674 return entry;
b49e97c9
TS
3675}
3676
d4596a51
RS
3677/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3678 The number might be exact or a worst-case estimate, depending on how
3679 much information is available to elf_backend_omit_section_dynsym at
3680 the current linking stage. */
3681
3682static bfd_size_type
3683count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3684{
3685 bfd_size_type count;
3686
3687 count = 0;
3688 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3689 {
3690 asection *p;
3691 const struct elf_backend_data *bed;
3692
3693 bed = get_elf_backend_data (output_bfd);
3694 for (p = output_bfd->sections; p ; p = p->next)
3695 if ((p->flags & SEC_EXCLUDE) == 0
3696 && (p->flags & SEC_ALLOC) != 0
3697 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3698 ++count;
3699 }
3700 return count;
3701}
3702
b49e97c9 3703/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3704 appear towards the end. */
b49e97c9 3705
b34976b6 3706static bfd_boolean
d4596a51 3707mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3708{
a8028dd0 3709 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3710 struct mips_elf_hash_sort_data hsd;
3711 struct mips_got_info *g;
b49e97c9 3712
d4596a51
RS
3713 if (elf_hash_table (info)->dynsymcount == 0)
3714 return TRUE;
3715
a8028dd0 3716 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3717 BFD_ASSERT (htab != NULL);
3718
a8028dd0 3719 g = htab->got_info;
d4596a51
RS
3720 if (g == NULL)
3721 return TRUE;
f4416af6 3722
b49e97c9 3723 hsd.low = NULL;
23cc69b6
RS
3724 hsd.max_unref_got_dynindx
3725 = hsd.min_got_dynindx
3726 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3727 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3728 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3729 elf_hash_table (info)),
3730 mips_elf_sort_hash_table_f,
3731 &hsd);
3732
3733 /* There should have been enough room in the symbol table to
44c410de 3734 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3735 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3736 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3737 == elf_hash_table (info)->dynsymcount);
3738 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3739 == g->global_gotno);
b49e97c9
TS
3740
3741 /* Now we know which dynamic symbol has the lowest dynamic symbol
3742 table index in the GOT. */
d222d210 3743 htab->global_gotsym = hsd.low;
b49e97c9 3744
b34976b6 3745 return TRUE;
b49e97c9
TS
3746}
3747
3748/* If H needs a GOT entry, assign it the highest available dynamic
3749 index. Otherwise, assign it the lowest available dynamic
3750 index. */
3751
b34976b6 3752static bfd_boolean
9719ad41 3753mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3754{
9719ad41 3755 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9 3756
b49e97c9
TS
3757 /* Symbols without dynamic symbol table entries aren't interesting
3758 at all. */
3759 if (h->root.dynindx == -1)
b34976b6 3760 return TRUE;
b49e97c9 3761
634835ae 3762 switch (h->global_got_area)
f4416af6 3763 {
634835ae
RS
3764 case GGA_NONE:
3765 h->root.dynindx = hsd->max_non_got_dynindx++;
3766 break;
0f20cc35 3767
634835ae 3768 case GGA_NORMAL:
b49e97c9
TS
3769 h->root.dynindx = --hsd->min_got_dynindx;
3770 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3771 break;
3772
3773 case GGA_RELOC_ONLY:
634835ae
RS
3774 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3775 hsd->low = (struct elf_link_hash_entry *) h;
3776 h->root.dynindx = hsd->max_unref_got_dynindx++;
3777 break;
b49e97c9
TS
3778 }
3779
b34976b6 3780 return TRUE;
b49e97c9
TS
3781}
3782
ee227692
RS
3783/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3784 (which is owned by the caller and shouldn't be added to the
3785 hash table directly). */
3786
3787static bfd_boolean
3788mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3789 struct mips_got_entry *lookup)
3790{
3791 struct mips_elf_link_hash_table *htab;
3792 struct mips_got_entry *entry;
3793 struct mips_got_info *g;
3794 void **loc, **bfd_loc;
3795
3796 /* Make sure there's a slot for this entry in the master GOT. */
3797 htab = mips_elf_hash_table (info);
3798 g = htab->got_info;
3799 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3800 if (!loc)
3801 return FALSE;
3802
3803 /* Populate the entry if it isn't already. */
3804 entry = (struct mips_got_entry *) *loc;
3805 if (!entry)
3806 {
3807 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3808 if (!entry)
3809 return FALSE;
3810
9ab066b4 3811 lookup->tls_initialized = FALSE;
ee227692
RS
3812 lookup->gotidx = -1;
3813 *entry = *lookup;
3814 *loc = entry;
3815 }
3816
3817 /* Reuse the same GOT entry for the BFD's GOT. */
3818 g = mips_elf_bfd_got (abfd, TRUE);
3819 if (!g)
3820 return FALSE;
3821
3822 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3823 if (!bfd_loc)
3824 return FALSE;
3825
3826 if (!*bfd_loc)
3827 *bfd_loc = entry;
3828 return TRUE;
3829}
3830
e641e783
RS
3831/* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3832 entry for it. FOR_CALL is true if the caller is only interested in
6ccf4795 3833 using the GOT entry for calls. */
b49e97c9 3834
b34976b6 3835static bfd_boolean
9719ad41
RS
3836mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3837 bfd *abfd, struct bfd_link_info *info,
e641e783 3838 bfd_boolean for_call, int r_type)
b49e97c9 3839{
a8028dd0 3840 struct mips_elf_link_hash_table *htab;
634835ae 3841 struct mips_elf_link_hash_entry *hmips;
ee227692
RS
3842 struct mips_got_entry entry;
3843 unsigned char tls_type;
a8028dd0
RS
3844
3845 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3846 BFD_ASSERT (htab != NULL);
3847
634835ae 3848 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795
RS
3849 if (!for_call)
3850 hmips->got_only_for_calls = FALSE;
f4416af6 3851
b49e97c9
TS
3852 /* A global symbol in the GOT must also be in the dynamic symbol
3853 table. */
7c5fcef7
L
3854 if (h->dynindx == -1)
3855 {
3856 switch (ELF_ST_VISIBILITY (h->other))
3857 {
3858 case STV_INTERNAL:
3859 case STV_HIDDEN:
33bb52fb 3860 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3861 break;
3862 }
c152c796 3863 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3864 return FALSE;
7c5fcef7 3865 }
b49e97c9 3866
ee227692 3867 tls_type = mips_elf_reloc_tls_type (r_type);
9ab066b4 3868 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
ee227692 3869 hmips->global_got_area = GGA_NORMAL;
86324f90 3870
f4416af6
AO
3871 entry.abfd = abfd;
3872 entry.symndx = -1;
3873 entry.d.h = (struct mips_elf_link_hash_entry *) h;
ee227692
RS
3874 entry.tls_type = tls_type;
3875 return mips_elf_record_got_entry (info, abfd, &entry);
b49e97c9 3876}
f4416af6 3877
e641e783
RS
3878/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3879 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
f4416af6
AO
3880
3881static bfd_boolean
9719ad41 3882mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
e641e783 3883 struct bfd_link_info *info, int r_type)
f4416af6 3884{
a8028dd0
RS
3885 struct mips_elf_link_hash_table *htab;
3886 struct mips_got_info *g;
ee227692 3887 struct mips_got_entry entry;
f4416af6 3888
a8028dd0 3889 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3890 BFD_ASSERT (htab != NULL);
3891
a8028dd0
RS
3892 g = htab->got_info;
3893 BFD_ASSERT (g != NULL);
3894
f4416af6
AO
3895 entry.abfd = abfd;
3896 entry.symndx = symndx;
3897 entry.d.addend = addend;
e641e783 3898 entry.tls_type = mips_elf_reloc_tls_type (r_type);
ee227692 3899 return mips_elf_record_got_entry (info, abfd, &entry);
f4416af6 3900}
c224138d 3901
13db6b44
RS
3902/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
3903 H is the symbol's hash table entry, or null if SYMNDX is local
3904 to ABFD. */
c224138d
RS
3905
3906static bfd_boolean
13db6b44
RS
3907mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
3908 long symndx, struct elf_link_hash_entry *h,
3909 bfd_signed_vma addend)
c224138d 3910{
a8028dd0 3911 struct mips_elf_link_hash_table *htab;
ee227692 3912 struct mips_got_info *g1, *g2;
13db6b44 3913 struct mips_got_page_ref lookup, *entry;
ee227692 3914 void **loc, **bfd_loc;
c224138d 3915
a8028dd0 3916 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3917 BFD_ASSERT (htab != NULL);
3918
ee227692
RS
3919 g1 = htab->got_info;
3920 BFD_ASSERT (g1 != NULL);
a8028dd0 3921
13db6b44
RS
3922 if (h)
3923 {
3924 lookup.symndx = -1;
3925 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
3926 }
3927 else
3928 {
3929 lookup.symndx = symndx;
3930 lookup.u.abfd = abfd;
3931 }
3932 lookup.addend = addend;
3933 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
c224138d
RS
3934 if (loc == NULL)
3935 return FALSE;
3936
13db6b44 3937 entry = (struct mips_got_page_ref *) *loc;
c224138d
RS
3938 if (!entry)
3939 {
3940 entry = bfd_alloc (abfd, sizeof (*entry));
3941 if (!entry)
3942 return FALSE;
3943
13db6b44 3944 *entry = lookup;
c224138d
RS
3945 *loc = entry;
3946 }
3947
ee227692
RS
3948 /* Add the same entry to the BFD's GOT. */
3949 g2 = mips_elf_bfd_got (abfd, TRUE);
3950 if (!g2)
3951 return FALSE;
3952
13db6b44 3953 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
ee227692
RS
3954 if (!bfd_loc)
3955 return FALSE;
3956
3957 if (!*bfd_loc)
3958 *bfd_loc = entry;
3959
c224138d
RS
3960 return TRUE;
3961}
33bb52fb
RS
3962
3963/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3964
3965static void
3966mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3967 unsigned int n)
3968{
3969 asection *s;
3970 struct mips_elf_link_hash_table *htab;
3971
3972 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3973 BFD_ASSERT (htab != NULL);
3974
33bb52fb
RS
3975 s = mips_elf_rel_dyn_section (info, FALSE);
3976 BFD_ASSERT (s != NULL);
3977
3978 if (htab->is_vxworks)
3979 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3980 else
3981 {
3982 if (s->size == 0)
3983 {
3984 /* Make room for a null element. */
3985 s->size += MIPS_ELF_REL_SIZE (abfd);
3986 ++s->reloc_count;
3987 }
3988 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3989 }
3990}
3991\f
476366af
RS
3992/* A htab_traverse callback for GOT entries, with DATA pointing to a
3993 mips_elf_traverse_got_arg structure. Count the number of GOT
3994 entries and TLS relocs. Set DATA->value to true if we need
3995 to resolve indirect or warning symbols and then recreate the GOT. */
33bb52fb
RS
3996
3997static int
3998mips_elf_check_recreate_got (void **entryp, void *data)
3999{
4000 struct mips_got_entry *entry;
476366af 4001 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4002
4003 entry = (struct mips_got_entry *) *entryp;
476366af 4004 arg = (struct mips_elf_traverse_got_arg *) data;
33bb52fb
RS
4005 if (entry->abfd != NULL && entry->symndx == -1)
4006 {
4007 struct mips_elf_link_hash_entry *h;
4008
4009 h = entry->d.h;
4010 if (h->root.root.type == bfd_link_hash_indirect
4011 || h->root.root.type == bfd_link_hash_warning)
4012 {
476366af 4013 arg->value = TRUE;
33bb52fb
RS
4014 return 0;
4015 }
4016 }
476366af 4017 mips_elf_count_got_entry (arg->info, arg->g, entry);
33bb52fb
RS
4018 return 1;
4019}
4020
476366af
RS
4021/* A htab_traverse callback for GOT entries, with DATA pointing to a
4022 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4023 converting entries for indirect and warning symbols into entries
4024 for the target symbol. Set DATA->g to null on error. */
33bb52fb
RS
4025
4026static int
4027mips_elf_recreate_got (void **entryp, void *data)
4028{
72e7511a 4029 struct mips_got_entry new_entry, *entry;
476366af 4030 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4031 void **slot;
4032
33bb52fb 4033 entry = (struct mips_got_entry *) *entryp;
476366af 4034 arg = (struct mips_elf_traverse_got_arg *) data;
72e7511a
RS
4035 if (entry->abfd != NULL
4036 && entry->symndx == -1
4037 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4038 || entry->d.h->root.root.type == bfd_link_hash_warning))
33bb52fb
RS
4039 {
4040 struct mips_elf_link_hash_entry *h;
4041
72e7511a
RS
4042 new_entry = *entry;
4043 entry = &new_entry;
33bb52fb 4044 h = entry->d.h;
72e7511a 4045 do
634835ae
RS
4046 {
4047 BFD_ASSERT (h->global_got_area == GGA_NONE);
4048 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4049 }
72e7511a
RS
4050 while (h->root.root.type == bfd_link_hash_indirect
4051 || h->root.root.type == bfd_link_hash_warning);
33bb52fb
RS
4052 entry->d.h = h;
4053 }
476366af 4054 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
33bb52fb
RS
4055 if (slot == NULL)
4056 {
476366af 4057 arg->g = NULL;
33bb52fb
RS
4058 return 0;
4059 }
4060 if (*slot == NULL)
72e7511a
RS
4061 {
4062 if (entry == &new_entry)
4063 {
4064 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4065 if (!entry)
4066 {
476366af 4067 arg->g = NULL;
72e7511a
RS
4068 return 0;
4069 }
4070 *entry = new_entry;
4071 }
4072 *slot = entry;
476366af 4073 mips_elf_count_got_entry (arg->info, arg->g, entry);
72e7511a 4074 }
33bb52fb
RS
4075 return 1;
4076}
4077
13db6b44
RS
4078/* Return the maximum number of GOT page entries required for RANGE. */
4079
4080static bfd_vma
4081mips_elf_pages_for_range (const struct mips_got_page_range *range)
4082{
4083 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4084}
4085
4086/* Record that G requires a page entry that can reach SEC + ADDEND. */
4087
4088static bfd_boolean
4089mips_elf_record_got_page_entry (struct mips_got_info *g,
4090 asection *sec, bfd_signed_vma addend)
4091{
4092 struct mips_got_page_entry lookup, *entry;
4093 struct mips_got_page_range **range_ptr, *range;
4094 bfd_vma old_pages, new_pages;
4095 void **loc;
4096
4097 /* Find the mips_got_page_entry hash table entry for this section. */
4098 lookup.sec = sec;
4099 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4100 if (loc == NULL)
4101 return FALSE;
4102
4103 /* Create a mips_got_page_entry if this is the first time we've
4104 seen the section. */
4105 entry = (struct mips_got_page_entry *) *loc;
4106 if (!entry)
4107 {
4108 entry = bfd_zalloc (sec->owner, sizeof (*entry));
4109 if (!entry)
4110 return FALSE;
4111
4112 entry->sec = sec;
4113 *loc = entry;
4114 }
4115
4116 /* Skip over ranges whose maximum extent cannot share a page entry
4117 with ADDEND. */
4118 range_ptr = &entry->ranges;
4119 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4120 range_ptr = &(*range_ptr)->next;
4121
4122 /* If we scanned to the end of the list, or found a range whose
4123 minimum extent cannot share a page entry with ADDEND, create
4124 a new singleton range. */
4125 range = *range_ptr;
4126 if (!range || addend < range->min_addend - 0xffff)
4127 {
4128 range = bfd_zalloc (sec->owner, sizeof (*range));
4129 if (!range)
4130 return FALSE;
4131
4132 range->next = *range_ptr;
4133 range->min_addend = addend;
4134 range->max_addend = addend;
4135
4136 *range_ptr = range;
4137 entry->num_pages++;
4138 g->page_gotno++;
4139 return TRUE;
4140 }
4141
4142 /* Remember how many pages the old range contributed. */
4143 old_pages = mips_elf_pages_for_range (range);
4144
4145 /* Update the ranges. */
4146 if (addend < range->min_addend)
4147 range->min_addend = addend;
4148 else if (addend > range->max_addend)
4149 {
4150 if (range->next && addend >= range->next->min_addend - 0xffff)
4151 {
4152 old_pages += mips_elf_pages_for_range (range->next);
4153 range->max_addend = range->next->max_addend;
4154 range->next = range->next->next;
4155 }
4156 else
4157 range->max_addend = addend;
4158 }
4159
4160 /* Record any change in the total estimate. */
4161 new_pages = mips_elf_pages_for_range (range);
4162 if (old_pages != new_pages)
4163 {
4164 entry->num_pages += new_pages - old_pages;
4165 g->page_gotno += new_pages - old_pages;
4166 }
4167
4168 return TRUE;
4169}
4170
4171/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4172 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4173 whether the page reference described by *REFP needs a GOT page entry,
4174 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4175
4176static bfd_boolean
4177mips_elf_resolve_got_page_ref (void **refp, void *data)
4178{
4179 struct mips_got_page_ref *ref;
4180 struct mips_elf_traverse_got_arg *arg;
4181 struct mips_elf_link_hash_table *htab;
4182 asection *sec;
4183 bfd_vma addend;
4184
4185 ref = (struct mips_got_page_ref *) *refp;
4186 arg = (struct mips_elf_traverse_got_arg *) data;
4187 htab = mips_elf_hash_table (arg->info);
4188
4189 if (ref->symndx < 0)
4190 {
4191 struct mips_elf_link_hash_entry *h;
4192
4193 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4194 h = ref->u.h;
4195 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4196 return 1;
4197
4198 /* Ignore undefined symbols; we'll issue an error later if
4199 appropriate. */
4200 if (!((h->root.root.type == bfd_link_hash_defined
4201 || h->root.root.type == bfd_link_hash_defweak)
4202 && h->root.root.u.def.section))
4203 return 1;
4204
4205 sec = h->root.root.u.def.section;
4206 addend = h->root.root.u.def.value + ref->addend;
4207 }
4208 else
4209 {
4210 Elf_Internal_Sym *isym;
4211
4212 /* Read in the symbol. */
4213 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4214 ref->symndx);
4215 if (isym == NULL)
4216 {
4217 arg->g = NULL;
4218 return 0;
4219 }
4220
4221 /* Get the associated input section. */
4222 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4223 if (sec == NULL)
4224 {
4225 arg->g = NULL;
4226 return 0;
4227 }
4228
4229 /* If this is a mergable section, work out the section and offset
4230 of the merged data. For section symbols, the addend specifies
4231 of the offset _of_ the first byte in the data, otherwise it
4232 specifies the offset _from_ the first byte. */
4233 if (sec->flags & SEC_MERGE)
4234 {
4235 void *secinfo;
4236
4237 secinfo = elf_section_data (sec)->sec_info;
4238 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4239 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4240 isym->st_value + ref->addend);
4241 else
4242 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4243 isym->st_value) + ref->addend;
4244 }
4245 else
4246 addend = isym->st_value + ref->addend;
4247 }
4248 if (!mips_elf_record_got_page_entry (arg->g, sec, addend))
4249 {
4250 arg->g = NULL;
4251 return 0;
4252 }
4253 return 1;
4254}
4255
33bb52fb 4256/* If any entries in G->got_entries are for indirect or warning symbols,
13db6b44
RS
4257 replace them with entries for the target symbol. Convert g->got_page_refs
4258 into got_page_entry structures and estimate the number of page entries
4259 that they require. */
33bb52fb
RS
4260
4261static bfd_boolean
476366af
RS
4262mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4263 struct mips_got_info *g)
33bb52fb 4264{
476366af
RS
4265 struct mips_elf_traverse_got_arg tga;
4266 struct mips_got_info oldg;
4267
4268 oldg = *g;
33bb52fb 4269
476366af
RS
4270 tga.info = info;
4271 tga.g = g;
4272 tga.value = FALSE;
4273 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4274 if (tga.value)
33bb52fb 4275 {
476366af
RS
4276 *g = oldg;
4277 g->got_entries = htab_create (htab_size (oldg.got_entries),
4278 mips_elf_got_entry_hash,
4279 mips_elf_got_entry_eq, NULL);
4280 if (!g->got_entries)
33bb52fb
RS
4281 return FALSE;
4282
476366af
RS
4283 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4284 if (!tga.g)
4285 return FALSE;
4286
4287 htab_delete (oldg.got_entries);
33bb52fb 4288 }
13db6b44
RS
4289
4290 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4291 mips_got_page_entry_eq, NULL);
4292 if (g->got_page_entries == NULL)
4293 return FALSE;
4294
4295 tga.info = info;
4296 tga.g = g;
4297 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4298
33bb52fb
RS
4299 return TRUE;
4300}
4301
c5d6fa44
RS
4302/* Return true if a GOT entry for H should live in the local rather than
4303 global GOT area. */
4304
4305static bfd_boolean
4306mips_use_local_got_p (struct bfd_link_info *info,
4307 struct mips_elf_link_hash_entry *h)
4308{
4309 /* Symbols that aren't in the dynamic symbol table must live in the
4310 local GOT. This includes symbols that are completely undefined
4311 and which therefore don't bind locally. We'll report undefined
4312 symbols later if appropriate. */
4313 if (h->root.dynindx == -1)
4314 return TRUE;
4315
4316 /* Symbols that bind locally can (and in the case of forced-local
4317 symbols, must) live in the local GOT. */
4318 if (h->got_only_for_calls
4319 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4320 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4321 return TRUE;
4322
4323 /* If this is an executable that must provide a definition of the symbol,
4324 either though PLTs or copy relocations, then that address should go in
4325 the local rather than global GOT. */
4326 if (info->executable && h->has_static_relocs)
4327 return TRUE;
4328
4329 return FALSE;
4330}
4331
6c42ddb9
RS
4332/* A mips_elf_link_hash_traverse callback for which DATA points to the
4333 link_info structure. Decide whether the hash entry needs an entry in
4334 the global part of the primary GOT, setting global_got_area accordingly.
4335 Count the number of global symbols that are in the primary GOT only
4336 because they have relocations against them (reloc_only_gotno). */
33bb52fb
RS
4337
4338static int
d4596a51 4339mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 4340{
020d7251 4341 struct bfd_link_info *info;
6ccf4795 4342 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
4343 struct mips_got_info *g;
4344
020d7251 4345 info = (struct bfd_link_info *) data;
6ccf4795
RS
4346 htab = mips_elf_hash_table (info);
4347 g = htab->got_info;
d4596a51 4348 if (h->global_got_area != GGA_NONE)
33bb52fb 4349 {
020d7251 4350 /* Make a final decision about whether the symbol belongs in the
c5d6fa44
RS
4351 local or global GOT. */
4352 if (mips_use_local_got_p (info, h))
6c42ddb9
RS
4353 /* The symbol belongs in the local GOT. We no longer need this
4354 entry if it was only used for relocations; those relocations
4355 will be against the null or section symbol instead of H. */
4356 h->global_got_area = GGA_NONE;
6ccf4795
RS
4357 else if (htab->is_vxworks
4358 && h->got_only_for_calls
1bbce132 4359 && h->root.plt.plist->mips_offset != MINUS_ONE)
6ccf4795
RS
4360 /* On VxWorks, calls can refer directly to the .got.plt entry;
4361 they don't need entries in the regular GOT. .got.plt entries
4362 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4363 h->global_got_area = GGA_NONE;
6c42ddb9 4364 else if (h->global_got_area == GGA_RELOC_ONLY)
23cc69b6 4365 {
6c42ddb9 4366 g->reloc_only_gotno++;
23cc69b6 4367 g->global_gotno++;
23cc69b6 4368 }
33bb52fb
RS
4369 }
4370 return 1;
4371}
f4416af6 4372\f
d7206569
RS
4373/* A htab_traverse callback for GOT entries. Add each one to the GOT
4374 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
f4416af6
AO
4375
4376static int
d7206569 4377mips_elf_add_got_entry (void **entryp, void *data)
f4416af6 4378{
d7206569
RS
4379 struct mips_got_entry *entry;
4380 struct mips_elf_traverse_got_arg *arg;
4381 void **slot;
f4416af6 4382
d7206569
RS
4383 entry = (struct mips_got_entry *) *entryp;
4384 arg = (struct mips_elf_traverse_got_arg *) data;
4385 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4386 if (!slot)
f4416af6 4387 {
d7206569
RS
4388 arg->g = NULL;
4389 return 0;
f4416af6 4390 }
d7206569 4391 if (!*slot)
c224138d 4392 {
d7206569
RS
4393 *slot = entry;
4394 mips_elf_count_got_entry (arg->info, arg->g, entry);
c224138d 4395 }
f4416af6
AO
4396 return 1;
4397}
4398
d7206569
RS
4399/* A htab_traverse callback for GOT page entries. Add each one to the GOT
4400 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
c224138d
RS
4401
4402static int
d7206569 4403mips_elf_add_got_page_entry (void **entryp, void *data)
c224138d 4404{
d7206569
RS
4405 struct mips_got_page_entry *entry;
4406 struct mips_elf_traverse_got_arg *arg;
4407 void **slot;
c224138d 4408
d7206569
RS
4409 entry = (struct mips_got_page_entry *) *entryp;
4410 arg = (struct mips_elf_traverse_got_arg *) data;
4411 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4412 if (!slot)
c224138d 4413 {
d7206569 4414 arg->g = NULL;
c224138d
RS
4415 return 0;
4416 }
d7206569
RS
4417 if (!*slot)
4418 {
4419 *slot = entry;
4420 arg->g->page_gotno += entry->num_pages;
4421 }
c224138d
RS
4422 return 1;
4423}
4424
d7206569
RS
4425/* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4426 this would lead to overflow, 1 if they were merged successfully,
4427 and 0 if a merge failed due to lack of memory. (These values are chosen
4428 so that nonnegative return values can be returned by a htab_traverse
4429 callback.) */
c224138d
RS
4430
4431static int
d7206569 4432mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
c224138d
RS
4433 struct mips_got_info *to,
4434 struct mips_elf_got_per_bfd_arg *arg)
4435{
d7206569 4436 struct mips_elf_traverse_got_arg tga;
c224138d
RS
4437 unsigned int estimate;
4438
4439 /* Work out how many page entries we would need for the combined GOT. */
4440 estimate = arg->max_pages;
4441 if (estimate >= from->page_gotno + to->page_gotno)
4442 estimate = from->page_gotno + to->page_gotno;
4443
e2ece73c 4444 /* And conservatively estimate how many local and TLS entries
c224138d 4445 would be needed. */
e2ece73c
RS
4446 estimate += from->local_gotno + to->local_gotno;
4447 estimate += from->tls_gotno + to->tls_gotno;
4448
17214937
RS
4449 /* If we're merging with the primary got, any TLS relocations will
4450 come after the full set of global entries. Otherwise estimate those
e2ece73c 4451 conservatively as well. */
17214937 4452 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
e2ece73c
RS
4453 estimate += arg->global_count;
4454 else
4455 estimate += from->global_gotno + to->global_gotno;
c224138d
RS
4456
4457 /* Bail out if the combined GOT might be too big. */
4458 if (estimate > arg->max_count)
4459 return -1;
4460
c224138d 4461 /* Transfer the bfd's got information from FROM to TO. */
d7206569
RS
4462 tga.info = arg->info;
4463 tga.g = to;
4464 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4465 if (!tga.g)
c224138d
RS
4466 return 0;
4467
d7206569
RS
4468 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4469 if (!tga.g)
c224138d
RS
4470 return 0;
4471
d7206569 4472 mips_elf_replace_bfd_got (abfd, to);
c224138d
RS
4473 return 1;
4474}
4475
d7206569 4476/* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
f4416af6
AO
4477 as possible of the primary got, since it doesn't require explicit
4478 dynamic relocations, but don't use bfds that would reference global
4479 symbols out of the addressable range. Failing the primary got,
4480 attempt to merge with the current got, or finish the current got
4481 and then make make the new got current. */
4482
d7206569
RS
4483static bfd_boolean
4484mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4485 struct mips_elf_got_per_bfd_arg *arg)
f4416af6 4486{
c224138d
RS
4487 unsigned int estimate;
4488 int result;
4489
476366af 4490 if (!mips_elf_resolve_final_got_entries (arg->info, g))
d7206569
RS
4491 return FALSE;
4492
c224138d
RS
4493 /* Work out the number of page, local and TLS entries. */
4494 estimate = arg->max_pages;
4495 if (estimate > g->page_gotno)
4496 estimate = g->page_gotno;
4497 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4498
4499 /* We place TLS GOT entries after both locals and globals. The globals
4500 for the primary GOT may overflow the normal GOT size limit, so be
4501 sure not to merge a GOT which requires TLS with the primary GOT in that
4502 case. This doesn't affect non-primary GOTs. */
c224138d 4503 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4504
c224138d 4505 if (estimate <= arg->max_count)
f4416af6 4506 {
c224138d
RS
4507 /* If we don't have a primary GOT, use it as
4508 a starting point for the primary GOT. */
4509 if (!arg->primary)
4510 {
d7206569
RS
4511 arg->primary = g;
4512 return TRUE;
c224138d 4513 }
f4416af6 4514
c224138d 4515 /* Try merging with the primary GOT. */
d7206569 4516 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
c224138d
RS
4517 if (result >= 0)
4518 return result;
f4416af6 4519 }
c224138d 4520
f4416af6 4521 /* If we can merge with the last-created got, do it. */
c224138d 4522 if (arg->current)
f4416af6 4523 {
d7206569 4524 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
c224138d
RS
4525 if (result >= 0)
4526 return result;
f4416af6 4527 }
c224138d 4528
f4416af6
AO
4529 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4530 fits; if it turns out that it doesn't, we'll get relocation
4531 overflows anyway. */
c224138d
RS
4532 g->next = arg->current;
4533 arg->current = g;
0f20cc35 4534
d7206569 4535 return TRUE;
0f20cc35
DJ
4536}
4537
72e7511a
RS
4538/* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4539 to GOTIDX, duplicating the entry if it has already been assigned
4540 an index in a different GOT. */
4541
4542static bfd_boolean
4543mips_elf_set_gotidx (void **entryp, long gotidx)
4544{
4545 struct mips_got_entry *entry;
4546
4547 entry = (struct mips_got_entry *) *entryp;
4548 if (entry->gotidx > 0)
4549 {
4550 struct mips_got_entry *new_entry;
4551
4552 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4553 if (!new_entry)
4554 return FALSE;
4555
4556 *new_entry = *entry;
4557 *entryp = new_entry;
4558 entry = new_entry;
4559 }
4560 entry->gotidx = gotidx;
4561 return TRUE;
4562}
4563
4564/* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4565 mips_elf_traverse_got_arg in which DATA->value is the size of one
4566 GOT entry. Set DATA->g to null on failure. */
0f20cc35
DJ
4567
4568static int
72e7511a 4569mips_elf_initialize_tls_index (void **entryp, void *data)
0f20cc35 4570{
72e7511a
RS
4571 struct mips_got_entry *entry;
4572 struct mips_elf_traverse_got_arg *arg;
0f20cc35
DJ
4573
4574 /* We're only interested in TLS symbols. */
72e7511a 4575 entry = (struct mips_got_entry *) *entryp;
9ab066b4 4576 if (entry->tls_type == GOT_TLS_NONE)
0f20cc35
DJ
4577 return 1;
4578
72e7511a 4579 arg = (struct mips_elf_traverse_got_arg *) data;
6c42ddb9 4580 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
ead49a57 4581 {
6c42ddb9
RS
4582 arg->g = NULL;
4583 return 0;
f4416af6
AO
4584 }
4585
ead49a57 4586 /* Account for the entries we've just allocated. */
9ab066b4 4587 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
f4416af6
AO
4588 return 1;
4589}
4590
ab361d49
RS
4591/* A htab_traverse callback for GOT entries, where DATA points to a
4592 mips_elf_traverse_got_arg. Set the global_got_area of each global
4593 symbol to DATA->value. */
f4416af6 4594
f4416af6 4595static int
ab361d49 4596mips_elf_set_global_got_area (void **entryp, void *data)
f4416af6 4597{
ab361d49
RS
4598 struct mips_got_entry *entry;
4599 struct mips_elf_traverse_got_arg *arg;
f4416af6 4600
ab361d49
RS
4601 entry = (struct mips_got_entry *) *entryp;
4602 arg = (struct mips_elf_traverse_got_arg *) data;
4603 if (entry->abfd != NULL
4604 && entry->symndx == -1
4605 && entry->d.h->global_got_area != GGA_NONE)
4606 entry->d.h->global_got_area = arg->value;
4607 return 1;
4608}
4609
4610/* A htab_traverse callback for secondary GOT entries, where DATA points
4611 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4612 and record the number of relocations they require. DATA->value is
72e7511a 4613 the size of one GOT entry. Set DATA->g to null on failure. */
ab361d49
RS
4614
4615static int
4616mips_elf_set_global_gotidx (void **entryp, void *data)
4617{
4618 struct mips_got_entry *entry;
4619 struct mips_elf_traverse_got_arg *arg;
0f20cc35 4620
ab361d49
RS
4621 entry = (struct mips_got_entry *) *entryp;
4622 arg = (struct mips_elf_traverse_got_arg *) data;
634835ae
RS
4623 if (entry->abfd != NULL
4624 && entry->symndx == -1
4625 && entry->d.h->global_got_area != GGA_NONE)
f4416af6 4626 {
72e7511a
RS
4627 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_gotno))
4628 {
4629 arg->g = NULL;
4630 return 0;
4631 }
4632 arg->g->assigned_gotno += 1;
4633
ab361d49
RS
4634 if (arg->info->shared
4635 || (elf_hash_table (arg->info)->dynamic_sections_created
4636 && entry->d.h->root.def_dynamic
4637 && !entry->d.h->root.def_regular))
4638 arg->g->relocs += 1;
f4416af6
AO
4639 }
4640
4641 return 1;
4642}
4643
33bb52fb
RS
4644/* A htab_traverse callback for GOT entries for which DATA is the
4645 bfd_link_info. Forbid any global symbols from having traditional
4646 lazy-binding stubs. */
4647
0626d451 4648static int
33bb52fb 4649mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4650{
33bb52fb
RS
4651 struct bfd_link_info *info;
4652 struct mips_elf_link_hash_table *htab;
4653 struct mips_got_entry *entry;
0626d451 4654
33bb52fb
RS
4655 entry = (struct mips_got_entry *) *entryp;
4656 info = (struct bfd_link_info *) data;
4657 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4658 BFD_ASSERT (htab != NULL);
4659
0626d451
RS
4660 if (entry->abfd != NULL
4661 && entry->symndx == -1
33bb52fb 4662 && entry->d.h->needs_lazy_stub)
f4416af6 4663 {
33bb52fb
RS
4664 entry->d.h->needs_lazy_stub = FALSE;
4665 htab->lazy_stub_count--;
f4416af6 4666 }
143d77c5 4667
f4416af6
AO
4668 return 1;
4669}
4670
f4416af6
AO
4671/* Return the offset of an input bfd IBFD's GOT from the beginning of
4672 the primary GOT. */
4673static bfd_vma
9719ad41 4674mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6 4675{
d7206569 4676 if (!g->next)
f4416af6
AO
4677 return 0;
4678
d7206569 4679 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
4680 if (! g)
4681 return 0;
4682
4683 BFD_ASSERT (g->next);
4684
4685 g = g->next;
143d77c5 4686
0f20cc35
DJ
4687 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4688 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4689}
4690
4691/* Turn a single GOT that is too big for 16-bit addressing into
4692 a sequence of GOTs, each one 16-bit addressable. */
4693
4694static bfd_boolean
9719ad41 4695mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4696 asection *got, bfd_size_type pages)
f4416af6 4697{
a8028dd0 4698 struct mips_elf_link_hash_table *htab;
f4416af6 4699 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
ab361d49 4700 struct mips_elf_traverse_got_arg tga;
a8028dd0 4701 struct mips_got_info *g, *gg;
33bb52fb 4702 unsigned int assign, needed_relocs;
d7206569 4703 bfd *dynobj, *ibfd;
f4416af6 4704
33bb52fb 4705 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4706 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4707 BFD_ASSERT (htab != NULL);
4708
a8028dd0 4709 g = htab->got_info;
f4416af6 4710
f4416af6
AO
4711 got_per_bfd_arg.obfd = abfd;
4712 got_per_bfd_arg.info = info;
f4416af6
AO
4713 got_per_bfd_arg.current = NULL;
4714 got_per_bfd_arg.primary = NULL;
0a44bf69 4715 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4716 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4717 - htab->reserved_gotno);
c224138d 4718 got_per_bfd_arg.max_pages = pages;
0f20cc35 4719 /* The number of globals that will be included in the primary GOT.
ab361d49 4720 See the calls to mips_elf_set_global_got_area below for more
0f20cc35
DJ
4721 information. */
4722 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4723
4724 /* Try to merge the GOTs of input bfds together, as long as they
4725 don't seem to exceed the maximum GOT size, choosing one of them
4726 to be the primary GOT. */
d7206569
RS
4727 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4728 {
4729 gg = mips_elf_bfd_got (ibfd, FALSE);
4730 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4731 return FALSE;
4732 }
f4416af6 4733
0f20cc35 4734 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6 4735 if (got_per_bfd_arg.primary == NULL)
3dff0dd1 4736 g->next = mips_elf_create_got_info (abfd);
f4416af6
AO
4737 else
4738 g->next = got_per_bfd_arg.primary;
4739 g->next->next = got_per_bfd_arg.current;
4740
4741 /* GG is now the master GOT, and G is the primary GOT. */
4742 gg = g;
4743 g = g->next;
4744
4745 /* Map the output bfd to the primary got. That's what we're going
4746 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4747 didn't mark in check_relocs, and we want a quick way to find it.
4748 We can't just use gg->next because we're going to reverse the
4749 list. */
d7206569 4750 mips_elf_replace_bfd_got (abfd, g);
f4416af6 4751
634835ae
RS
4752 /* Every symbol that is referenced in a dynamic relocation must be
4753 present in the primary GOT, so arrange for them to appear after
4754 those that are actually referenced. */
23cc69b6 4755 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4756 g->global_gotno = gg->global_gotno;
f4416af6 4757
ab361d49
RS
4758 tga.info = info;
4759 tga.value = GGA_RELOC_ONLY;
4760 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4761 tga.value = GGA_NORMAL;
4762 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
f4416af6
AO
4763
4764 /* Now go through the GOTs assigning them offset ranges.
4765 [assigned_gotno, local_gotno[ will be set to the range of local
4766 entries in each GOT. We can then compute the end of a GOT by
4767 adding local_gotno to global_gotno. We reverse the list and make
4768 it circular since then we'll be able to quickly compute the
4769 beginning of a GOT, by computing the end of its predecessor. To
4770 avoid special cases for the primary GOT, while still preserving
4771 assertions that are valid for both single- and multi-got links,
4772 we arrange for the main got struct to have the right number of
4773 global entries, but set its local_gotno such that the initial
4774 offset of the primary GOT is zero. Remember that the primary GOT
4775 will become the last item in the circular linked list, so it
4776 points back to the master GOT. */
4777 gg->local_gotno = -g->global_gotno;
4778 gg->global_gotno = g->global_gotno;
0f20cc35 4779 gg->tls_gotno = 0;
f4416af6
AO
4780 assign = 0;
4781 gg->next = gg;
4782
4783 do
4784 {
4785 struct mips_got_info *gn;
4786
861fb55a 4787 assign += htab->reserved_gotno;
f4416af6 4788 g->assigned_gotno = assign;
c224138d
RS
4789 g->local_gotno += assign;
4790 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4791 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4792
ead49a57
RS
4793 /* Take g out of the direct list, and push it onto the reversed
4794 list that gg points to. g->next is guaranteed to be nonnull after
4795 this operation, as required by mips_elf_initialize_tls_index. */
4796 gn = g->next;
4797 g->next = gg->next;
4798 gg->next = g;
4799
0f20cc35
DJ
4800 /* Set up any TLS entries. We always place the TLS entries after
4801 all non-TLS entries. */
4802 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
72e7511a
RS
4803 tga.g = g;
4804 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4805 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4806 if (!tga.g)
4807 return FALSE;
1fd20d70 4808 BFD_ASSERT (g->tls_assigned_gotno == assign);
f4416af6 4809
ead49a57 4810 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4811 g = gn;
0626d451 4812
33bb52fb
RS
4813 /* Forbid global symbols in every non-primary GOT from having
4814 lazy-binding stubs. */
0626d451 4815 if (g)
33bb52fb 4816 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4817 }
4818 while (g);
4819
59b08994 4820 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
33bb52fb
RS
4821
4822 needed_relocs = 0;
33bb52fb
RS
4823 for (g = gg->next; g && g->next != gg; g = g->next)
4824 {
4825 unsigned int save_assign;
4826
ab361d49
RS
4827 /* Assign offsets to global GOT entries and count how many
4828 relocations they need. */
33bb52fb
RS
4829 save_assign = g->assigned_gotno;
4830 g->assigned_gotno = g->local_gotno;
ab361d49
RS
4831 tga.info = info;
4832 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4833 tga.g = g;
4834 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
72e7511a
RS
4835 if (!tga.g)
4836 return FALSE;
4837 BFD_ASSERT (g->assigned_gotno == g->local_gotno + g->global_gotno);
33bb52fb 4838 g->assigned_gotno = save_assign;
72e7511a 4839
33bb52fb
RS
4840 if (info->shared)
4841 {
ab361d49 4842 g->relocs += g->local_gotno - g->assigned_gotno;
33bb52fb
RS
4843 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4844 + g->next->global_gotno
4845 + g->next->tls_gotno
861fb55a 4846 + htab->reserved_gotno);
33bb52fb 4847 }
ab361d49 4848 needed_relocs += g->relocs;
33bb52fb 4849 }
ab361d49 4850 needed_relocs += g->relocs;
33bb52fb
RS
4851
4852 if (needed_relocs)
4853 mips_elf_allocate_dynamic_relocations (dynobj, info,
4854 needed_relocs);
143d77c5 4855
f4416af6
AO
4856 return TRUE;
4857}
143d77c5 4858
b49e97c9
TS
4859\f
4860/* Returns the first relocation of type r_type found, beginning with
4861 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4862
4863static const Elf_Internal_Rela *
9719ad41
RS
4864mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4865 const Elf_Internal_Rela *relocation,
4866 const Elf_Internal_Rela *relend)
b49e97c9 4867{
c000e262
TS
4868 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4869
b49e97c9
TS
4870 while (relocation < relend)
4871 {
c000e262
TS
4872 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4873 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4874 return relocation;
4875
4876 ++relocation;
4877 }
4878
4879 /* We didn't find it. */
b49e97c9
TS
4880 return NULL;
4881}
4882
020d7251 4883/* Return whether an input relocation is against a local symbol. */
b49e97c9 4884
b34976b6 4885static bfd_boolean
9719ad41
RS
4886mips_elf_local_relocation_p (bfd *input_bfd,
4887 const Elf_Internal_Rela *relocation,
020d7251 4888 asection **local_sections)
b49e97c9
TS
4889{
4890 unsigned long r_symndx;
4891 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
4892 size_t extsymoff;
4893
4894 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4895 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4896 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4897
4898 if (r_symndx < extsymoff)
b34976b6 4899 return TRUE;
b49e97c9 4900 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4901 return TRUE;
b49e97c9 4902
b34976b6 4903 return FALSE;
b49e97c9
TS
4904}
4905\f
4906/* Sign-extend VALUE, which has the indicated number of BITS. */
4907
a7ebbfdf 4908bfd_vma
9719ad41 4909_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4910{
4911 if (value & ((bfd_vma) 1 << (bits - 1)))
4912 /* VALUE is negative. */
4913 value |= ((bfd_vma) - 1) << bits;
4914
4915 return value;
4916}
4917
4918/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4919 range expressible by a signed number with the indicated number of
b49e97c9
TS
4920 BITS. */
4921
b34976b6 4922static bfd_boolean
9719ad41 4923mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4924{
4925 bfd_signed_vma svalue = (bfd_signed_vma) value;
4926
4927 if (svalue > (1 << (bits - 1)) - 1)
4928 /* The value is too big. */
b34976b6 4929 return TRUE;
b49e97c9
TS
4930 else if (svalue < -(1 << (bits - 1)))
4931 /* The value is too small. */
b34976b6 4932 return TRUE;
b49e97c9
TS
4933
4934 /* All is well. */
b34976b6 4935 return FALSE;
b49e97c9
TS
4936}
4937
4938/* Calculate the %high function. */
4939
4940static bfd_vma
9719ad41 4941mips_elf_high (bfd_vma value)
b49e97c9
TS
4942{
4943 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4944}
4945
4946/* Calculate the %higher function. */
4947
4948static bfd_vma
9719ad41 4949mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4950{
4951#ifdef BFD64
4952 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4953#else
4954 abort ();
c5ae1840 4955 return MINUS_ONE;
b49e97c9
TS
4956#endif
4957}
4958
4959/* Calculate the %highest function. */
4960
4961static bfd_vma
9719ad41 4962mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4963{
4964#ifdef BFD64
b15e6682 4965 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4966#else
4967 abort ();
c5ae1840 4968 return MINUS_ONE;
b49e97c9
TS
4969#endif
4970}
4971\f
4972/* Create the .compact_rel section. */
4973
b34976b6 4974static bfd_boolean
9719ad41
RS
4975mips_elf_create_compact_rel_section
4976 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4977{
4978 flagword flags;
4979 register asection *s;
4980
3d4d4302 4981 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
b49e97c9
TS
4982 {
4983 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4984 | SEC_READONLY);
4985
3d4d4302 4986 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4987 if (s == NULL
b49e97c9
TS
4988 || ! bfd_set_section_alignment (abfd, s,
4989 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4990 return FALSE;
b49e97c9 4991
eea6121a 4992 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4993 }
4994
b34976b6 4995 return TRUE;
b49e97c9
TS
4996}
4997
4998/* Create the .got section to hold the global offset table. */
4999
b34976b6 5000static bfd_boolean
23cc69b6 5001mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
5002{
5003 flagword flags;
5004 register asection *s;
5005 struct elf_link_hash_entry *h;
14a793b2 5006 struct bfd_link_hash_entry *bh;
0a44bf69
RS
5007 struct mips_elf_link_hash_table *htab;
5008
5009 htab = mips_elf_hash_table (info);
4dfe6ac6 5010 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5011
5012 /* This function may be called more than once. */
23cc69b6
RS
5013 if (htab->sgot)
5014 return TRUE;
b49e97c9
TS
5015
5016 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5017 | SEC_LINKER_CREATED);
5018
72b4917c
TS
5019 /* We have to use an alignment of 2**4 here because this is hardcoded
5020 in the function stub generation and in the linker script. */
87e0a731 5021 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
b49e97c9 5022 if (s == NULL
72b4917c 5023 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 5024 return FALSE;
a8028dd0 5025 htab->sgot = s;
b49e97c9
TS
5026
5027 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5028 linker script because we don't want to define the symbol if we
5029 are not creating a global offset table. */
14a793b2 5030 bh = NULL;
b49e97c9
TS
5031 if (! (_bfd_generic_link_add_one_symbol
5032 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 5033 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5034 return FALSE;
14a793b2
AM
5035
5036 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5037 h->non_elf = 0;
5038 h->def_regular = 1;
b49e97c9 5039 h->type = STT_OBJECT;
2f9efdfc 5040 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d329bcd1 5041 elf_hash_table (info)->hgot = h;
b49e97c9
TS
5042
5043 if (info->shared
c152c796 5044 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5045 return FALSE;
b49e97c9 5046
3dff0dd1 5047 htab->got_info = mips_elf_create_got_info (abfd);
f0abc2a1 5048 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
5049 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5050
861fb55a 5051 /* We also need a .got.plt section when generating PLTs. */
87e0a731
AM
5052 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5053 SEC_ALLOC | SEC_LOAD
5054 | SEC_HAS_CONTENTS
5055 | SEC_IN_MEMORY
5056 | SEC_LINKER_CREATED);
861fb55a
DJ
5057 if (s == NULL)
5058 return FALSE;
5059 htab->sgotplt = s;
0a44bf69 5060
b34976b6 5061 return TRUE;
b49e97c9 5062}
b49e97c9 5063\f
0a44bf69
RS
5064/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5065 __GOTT_INDEX__ symbols. These symbols are only special for
5066 shared objects; they are not used in executables. */
5067
5068static bfd_boolean
5069is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5070{
5071 return (mips_elf_hash_table (info)->is_vxworks
5072 && info->shared
5073 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5074 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5075}
861fb55a
DJ
5076
5077/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5078 require an la25 stub. See also mips_elf_local_pic_function_p,
5079 which determines whether the destination function ever requires a
5080 stub. */
5081
5082static bfd_boolean
8f0c309a
CLT
5083mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5084 bfd_boolean target_is_16_bit_code_p)
861fb55a
DJ
5085{
5086 /* We specifically ignore branches and jumps from EF_PIC objects,
5087 where the onus is on the compiler or programmer to perform any
5088 necessary initialization of $25. Sometimes such initialization
5089 is unnecessary; for example, -mno-shared functions do not use
5090 the incoming value of $25, and may therefore be called directly. */
5091 if (PIC_OBJECT_P (input_bfd))
5092 return FALSE;
5093
5094 switch (r_type)
5095 {
5096 case R_MIPS_26:
5097 case R_MIPS_PC16:
df58fc94
RS
5098 case R_MICROMIPS_26_S1:
5099 case R_MICROMIPS_PC7_S1:
5100 case R_MICROMIPS_PC10_S1:
5101 case R_MICROMIPS_PC16_S1:
5102 case R_MICROMIPS_PC23_S2:
861fb55a
DJ
5103 return TRUE;
5104
8f0c309a
CLT
5105 case R_MIPS16_26:
5106 return !target_is_16_bit_code_p;
5107
861fb55a
DJ
5108 default:
5109 return FALSE;
5110 }
5111}
0a44bf69 5112\f
b49e97c9
TS
5113/* Calculate the value produced by the RELOCATION (which comes from
5114 the INPUT_BFD). The ADDEND is the addend to use for this
5115 RELOCATION; RELOCATION->R_ADDEND is ignored.
5116
5117 The result of the relocation calculation is stored in VALUEP.
38a7df63 5118 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
df58fc94 5119 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9
TS
5120
5121 This function returns bfd_reloc_continue if the caller need take no
5122 further action regarding this relocation, bfd_reloc_notsupported if
5123 something goes dramatically wrong, bfd_reloc_overflow if an
5124 overflow occurs, and bfd_reloc_ok to indicate success. */
5125
5126static bfd_reloc_status_type
9719ad41
RS
5127mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5128 asection *input_section,
5129 struct bfd_link_info *info,
5130 const Elf_Internal_Rela *relocation,
5131 bfd_vma addend, reloc_howto_type *howto,
5132 Elf_Internal_Sym *local_syms,
5133 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
5134 const char **namep,
5135 bfd_boolean *cross_mode_jump_p,
9719ad41 5136 bfd_boolean save_addend)
b49e97c9
TS
5137{
5138 /* The eventual value we will return. */
5139 bfd_vma value;
5140 /* The address of the symbol against which the relocation is
5141 occurring. */
5142 bfd_vma symbol = 0;
5143 /* The final GP value to be used for the relocatable, executable, or
5144 shared object file being produced. */
0a61c8c2 5145 bfd_vma gp;
b49e97c9
TS
5146 /* The place (section offset or address) of the storage unit being
5147 relocated. */
5148 bfd_vma p;
5149 /* The value of GP used to create the relocatable object. */
0a61c8c2 5150 bfd_vma gp0;
b49e97c9
TS
5151 /* The offset into the global offset table at which the address of
5152 the relocation entry symbol, adjusted by the addend, resides
5153 during execution. */
5154 bfd_vma g = MINUS_ONE;
5155 /* The section in which the symbol referenced by the relocation is
5156 located. */
5157 asection *sec = NULL;
5158 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 5159 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 5160 symbol. */
b34976b6
AM
5161 bfd_boolean local_p, was_local_p;
5162 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5163 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
5164 /* TRUE if the symbol referred to by this relocation is
5165 "__gnu_local_gp". */
5166 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
5167 Elf_Internal_Shdr *symtab_hdr;
5168 size_t extsymoff;
5169 unsigned long r_symndx;
5170 int r_type;
b34976b6 5171 /* TRUE if overflow occurred during the calculation of the
b49e97c9 5172 relocation value. */
b34976b6
AM
5173 bfd_boolean overflowed_p;
5174 /* TRUE if this relocation refers to a MIPS16 function. */
5175 bfd_boolean target_is_16_bit_code_p = FALSE;
df58fc94 5176 bfd_boolean target_is_micromips_code_p = FALSE;
0a44bf69
RS
5177 struct mips_elf_link_hash_table *htab;
5178 bfd *dynobj;
5179
5180 dynobj = elf_hash_table (info)->dynobj;
5181 htab = mips_elf_hash_table (info);
4dfe6ac6 5182 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5183
5184 /* Parse the relocation. */
5185 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5186 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5187 p = (input_section->output_section->vma
5188 + input_section->output_offset
5189 + relocation->r_offset);
5190
5191 /* Assume that there will be no overflow. */
b34976b6 5192 overflowed_p = FALSE;
b49e97c9
TS
5193
5194 /* Figure out whether or not the symbol is local, and get the offset
5195 used in the array of hash table entries. */
5196 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5197 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 5198 local_sections);
bce03d3d 5199 was_local_p = local_p;
b49e97c9
TS
5200 if (! elf_bad_symtab (input_bfd))
5201 extsymoff = symtab_hdr->sh_info;
5202 else
5203 {
5204 /* The symbol table does not follow the rule that local symbols
5205 must come before globals. */
5206 extsymoff = 0;
5207 }
5208
5209 /* Figure out the value of the symbol. */
5210 if (local_p)
5211 {
5212 Elf_Internal_Sym *sym;
5213
5214 sym = local_syms + r_symndx;
5215 sec = local_sections[r_symndx];
5216
5217 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
5218 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5219 || (sec->flags & SEC_MERGE))
b49e97c9 5220 symbol += sym->st_value;
d4df96e6
L
5221 if ((sec->flags & SEC_MERGE)
5222 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5223 {
5224 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5225 addend -= symbol;
5226 addend += sec->output_section->vma + sec->output_offset;
5227 }
b49e97c9 5228
df58fc94
RS
5229 /* MIPS16/microMIPS text labels should be treated as odd. */
5230 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
5231 ++symbol;
5232
5233 /* Record the name of this symbol, for our caller. */
5234 *namep = bfd_elf_string_from_elf_section (input_bfd,
5235 symtab_hdr->sh_link,
5236 sym->st_name);
5237 if (*namep == '\0')
5238 *namep = bfd_section_name (input_bfd, sec);
5239
30c09090 5240 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
df58fc94 5241 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
b49e97c9
TS
5242 }
5243 else
5244 {
560e09e9
NC
5245 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5246
b49e97c9
TS
5247 /* For global symbols we look up the symbol in the hash-table. */
5248 h = ((struct mips_elf_link_hash_entry *)
5249 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5250 /* Find the real hash-table entry for this symbol. */
5251 while (h->root.root.type == bfd_link_hash_indirect
5252 || h->root.root.type == bfd_link_hash_warning)
5253 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5254
5255 /* Record the name of this symbol, for our caller. */
5256 *namep = h->root.root.root.string;
5257
5258 /* See if this is the special _gp_disp symbol. Note that such a
5259 symbol must always be a global symbol. */
560e09e9 5260 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
5261 && ! NEWABI_P (input_bfd))
5262 {
5263 /* Relocations against _gp_disp are permitted only with
5264 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 5265 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
5266 return bfd_reloc_notsupported;
5267
b34976b6 5268 gp_disp_p = TRUE;
b49e97c9 5269 }
bbe506e8
TS
5270 /* See if this is the special _gp symbol. Note that such a
5271 symbol must always be a global symbol. */
5272 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5273 gnu_local_gp_p = TRUE;
5274
5275
b49e97c9
TS
5276 /* If this symbol is defined, calculate its address. Note that
5277 _gp_disp is a magic symbol, always implicitly defined by the
5278 linker, so it's inappropriate to check to see whether or not
5279 its defined. */
5280 else if ((h->root.root.type == bfd_link_hash_defined
5281 || h->root.root.type == bfd_link_hash_defweak)
5282 && h->root.root.u.def.section)
5283 {
5284 sec = h->root.root.u.def.section;
5285 if (sec->output_section)
5286 symbol = (h->root.root.u.def.value
5287 + sec->output_section->vma
5288 + sec->output_offset);
5289 else
5290 symbol = h->root.root.u.def.value;
5291 }
5292 else if (h->root.root.type == bfd_link_hash_undefweak)
5293 /* We allow relocations against undefined weak symbols, giving
5294 it the value zero, so that you can undefined weak functions
5295 and check to see if they exist by looking at their
5296 addresses. */
5297 symbol = 0;
59c2e50f 5298 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5299 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5300 symbol = 0;
a4d0f181
TS
5301 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5302 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5303 {
5304 /* If this is a dynamic link, we should have created a
5305 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5306 in in _bfd_mips_elf_create_dynamic_sections.
5307 Otherwise, we should define the symbol with a value of 0.
5308 FIXME: It should probably get into the symbol table
5309 somehow as well. */
5310 BFD_ASSERT (! info->shared);
5311 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5312 symbol = 0;
5313 }
5e2b0d47
NC
5314 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5315 {
5316 /* This is an optional symbol - an Irix specific extension to the
5317 ELF spec. Ignore it for now.
5318 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5319 than simply ignoring them, but we do not handle this for now.
5320 For information see the "64-bit ELF Object File Specification"
5321 which is available from here:
5322 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5323 symbol = 0;
5324 }
e7e2196d
MR
5325 else if ((*info->callbacks->undefined_symbol)
5326 (info, h->root.root.root.string, input_bfd,
5327 input_section, relocation->r_offset,
5328 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5329 || ELF_ST_VISIBILITY (h->root.other)))
5330 {
5331 return bfd_reloc_undefined;
5332 }
b49e97c9
TS
5333 else
5334 {
e7e2196d 5335 return bfd_reloc_notsupported;
b49e97c9
TS
5336 }
5337
30c09090 5338 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
1bbce132 5339 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
b49e97c9
TS
5340 }
5341
738e5348
RS
5342 /* If this is a reference to a 16-bit function with a stub, we need
5343 to redirect the relocation to the stub unless:
5344
5345 (a) the relocation is for a MIPS16 JAL;
5346
5347 (b) the relocation is for a MIPS16 PIC call, and there are no
5348 non-MIPS16 uses of the GOT slot; or
5349
5350 (c) the section allows direct references to MIPS16 functions. */
5351 if (r_type != R_MIPS16_26
5352 && !info->relocatable
5353 && ((h != NULL
5354 && h->fn_stub != NULL
5355 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71 5356 || (local_p
698600e4
AM
5357 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5358 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5359 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5360 {
5361 /* This is a 32- or 64-bit call to a 16-bit function. We should
5362 have already noticed that we were going to need the
5363 stub. */
5364 if (local_p)
8f0c309a 5365 {
698600e4 5366 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
8f0c309a
CLT
5367 value = 0;
5368 }
b49e97c9
TS
5369 else
5370 {
5371 BFD_ASSERT (h->need_fn_stub);
8f0c309a
CLT
5372 if (h->la25_stub)
5373 {
5374 /* If a LA25 header for the stub itself exists, point to the
5375 prepended LUI/ADDIU sequence. */
5376 sec = h->la25_stub->stub_section;
5377 value = h->la25_stub->offset;
5378 }
5379 else
5380 {
5381 sec = h->fn_stub;
5382 value = 0;
5383 }
b49e97c9
TS
5384 }
5385
8f0c309a 5386 symbol = sec->output_section->vma + sec->output_offset + value;
f38c2df5
TS
5387 /* The target is 16-bit, but the stub isn't. */
5388 target_is_16_bit_code_p = FALSE;
b49e97c9 5389 }
1bbce132
MR
5390 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5391 to a standard MIPS function, we need to redirect the call to the stub.
5392 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5393 indirect calls should use an indirect stub instead. */
1049f94e 5394 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5395 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71 5396 || (local_p
698600e4
AM
5397 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5398 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
1bbce132 5399 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
b49e97c9 5400 {
b9d58d71 5401 if (local_p)
698600e4 5402 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
b9d58d71 5403 else
b49e97c9 5404 {
b9d58d71
TS
5405 /* If both call_stub and call_fp_stub are defined, we can figure
5406 out which one to use by checking which one appears in the input
5407 file. */
5408 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5409 {
b9d58d71 5410 asection *o;
68ffbac6 5411
b9d58d71
TS
5412 sec = NULL;
5413 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5414 {
b9d58d71
TS
5415 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5416 {
5417 sec = h->call_fp_stub;
5418 break;
5419 }
b49e97c9 5420 }
b9d58d71
TS
5421 if (sec == NULL)
5422 sec = h->call_stub;
b49e97c9 5423 }
b9d58d71 5424 else if (h->call_stub != NULL)
b49e97c9 5425 sec = h->call_stub;
b9d58d71
TS
5426 else
5427 sec = h->call_fp_stub;
5428 }
b49e97c9 5429
eea6121a 5430 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5431 symbol = sec->output_section->vma + sec->output_offset;
5432 }
861fb55a
DJ
5433 /* If this is a direct call to a PIC function, redirect to the
5434 non-PIC stub. */
5435 else if (h != NULL && h->la25_stub
8f0c309a
CLT
5436 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5437 target_is_16_bit_code_p))
861fb55a
DJ
5438 symbol = (h->la25_stub->stub_section->output_section->vma
5439 + h->la25_stub->stub_section->output_offset
5440 + h->la25_stub->offset);
1bbce132
MR
5441 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5442 entry is used if a standard PLT entry has also been made. In this
5443 case the symbol will have been set by mips_elf_set_plt_sym_value
5444 to point to the standard PLT entry, so redirect to the compressed
5445 one. */
5446 else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
5447 && !info->relocatable
5448 && h != NULL
5449 && h->use_plt_entry
5450 && h->root.plt.plist->comp_offset != MINUS_ONE
5451 && h->root.plt.plist->mips_offset != MINUS_ONE)
5452 {
5453 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5454
5455 sec = htab->splt;
5456 symbol = (sec->output_section->vma
5457 + sec->output_offset
5458 + htab->plt_header_size
5459 + htab->plt_mips_offset
5460 + h->root.plt.plist->comp_offset
5461 + 1);
5462
5463 target_is_16_bit_code_p = !micromips_p;
5464 target_is_micromips_code_p = micromips_p;
5465 }
b49e97c9 5466
df58fc94
RS
5467 /* Make sure MIPS16 and microMIPS are not used together. */
5468 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5469 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5470 {
5471 (*_bfd_error_handler)
5472 (_("MIPS16 and microMIPS functions cannot call each other"));
5473 return bfd_reloc_notsupported;
5474 }
5475
b49e97c9 5476 /* Calls from 16-bit code to 32-bit code and vice versa require the
df58fc94
RS
5477 mode change. However, we can ignore calls to undefined weak symbols,
5478 which should never be executed at runtime. This exception is important
5479 because the assembly writer may have "known" that any definition of the
5480 symbol would be 16-bit code, and that direct jumps were therefore
5481 acceptable. */
5482 *cross_mode_jump_p = (!info->relocatable
5483 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5484 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5485 || (r_type == R_MICROMIPS_26_S1
5486 && !target_is_micromips_code_p)
5487 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5488 && (target_is_16_bit_code_p
5489 || target_is_micromips_code_p))));
b49e97c9 5490
c5d6fa44 5491 local_p = (h == NULL || mips_use_local_got_p (info, h));
b49e97c9 5492
0a61c8c2
RS
5493 gp0 = _bfd_get_gp_value (input_bfd);
5494 gp = _bfd_get_gp_value (abfd);
23cc69b6 5495 if (htab->got_info)
a8028dd0 5496 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5497
5498 if (gnu_local_gp_p)
5499 symbol = gp;
5500
df58fc94
RS
5501 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5502 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5503 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5504 if (got_page_reloc_p (r_type) && !local_p)
020d7251 5505 {
df58fc94
RS
5506 r_type = (micromips_reloc_p (r_type)
5507 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
020d7251
RS
5508 addend = 0;
5509 }
5510
e77760d2 5511 /* If we haven't already determined the GOT offset, and we're going
0a61c8c2 5512 to need it, get it now. */
b49e97c9
TS
5513 switch (r_type)
5514 {
738e5348
RS
5515 case R_MIPS16_CALL16:
5516 case R_MIPS16_GOT16:
b49e97c9
TS
5517 case R_MIPS_CALL16:
5518 case R_MIPS_GOT16:
5519 case R_MIPS_GOT_DISP:
5520 case R_MIPS_GOT_HI16:
5521 case R_MIPS_CALL_HI16:
5522 case R_MIPS_GOT_LO16:
5523 case R_MIPS_CALL_LO16:
df58fc94
RS
5524 case R_MICROMIPS_CALL16:
5525 case R_MICROMIPS_GOT16:
5526 case R_MICROMIPS_GOT_DISP:
5527 case R_MICROMIPS_GOT_HI16:
5528 case R_MICROMIPS_CALL_HI16:
5529 case R_MICROMIPS_GOT_LO16:
5530 case R_MICROMIPS_CALL_LO16:
0f20cc35
DJ
5531 case R_MIPS_TLS_GD:
5532 case R_MIPS_TLS_GOTTPREL:
5533 case R_MIPS_TLS_LDM:
d0f13682
CLT
5534 case R_MIPS16_TLS_GD:
5535 case R_MIPS16_TLS_GOTTPREL:
5536 case R_MIPS16_TLS_LDM:
df58fc94
RS
5537 case R_MICROMIPS_TLS_GD:
5538 case R_MICROMIPS_TLS_GOTTPREL:
5539 case R_MICROMIPS_TLS_LDM:
b49e97c9 5540 /* Find the index into the GOT where this value is located. */
df58fc94 5541 if (tls_ldm_reloc_p (r_type))
0f20cc35 5542 {
0a44bf69 5543 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5544 0, 0, NULL, r_type);
0f20cc35
DJ
5545 if (g == MINUS_ONE)
5546 return bfd_reloc_outofrange;
5547 }
5548 else if (!local_p)
b49e97c9 5549 {
0a44bf69
RS
5550 /* On VxWorks, CALL relocations should refer to the .got.plt
5551 entry, which is initialized to point at the PLT stub. */
5552 if (htab->is_vxworks
df58fc94
RS
5553 && (call_hi16_reloc_p (r_type)
5554 || call_lo16_reloc_p (r_type)
738e5348 5555 || call16_reloc_p (r_type)))
0a44bf69
RS
5556 {
5557 BFD_ASSERT (addend == 0);
5558 BFD_ASSERT (h->root.needs_plt);
5559 g = mips_elf_gotplt_index (info, &h->root);
5560 }
5561 else
b49e97c9 5562 {
020d7251 5563 BFD_ASSERT (addend == 0);
13fbec83
RS
5564 g = mips_elf_global_got_index (abfd, info, input_bfd,
5565 &h->root, r_type);
e641e783 5566 if (!TLS_RELOC_P (r_type)
020d7251
RS
5567 && !elf_hash_table (info)->dynamic_sections_created)
5568 /* This is a static link. We must initialize the GOT entry. */
a8028dd0 5569 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5570 }
5571 }
0a44bf69 5572 else if (!htab->is_vxworks
738e5348 5573 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5574 /* The calculation below does not involve "g". */
b49e97c9
TS
5575 break;
5576 else
5577 {
5c18022e 5578 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5579 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5580 if (g == MINUS_ONE)
5581 return bfd_reloc_outofrange;
5582 }
5583
5584 /* Convert GOT indices to actual offsets. */
a8028dd0 5585 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5586 break;
b49e97c9
TS
5587 }
5588
0a44bf69
RS
5589 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5590 symbols are resolved by the loader. Add them to .rela.dyn. */
5591 if (h != NULL && is_gott_symbol (info, &h->root))
5592 {
5593 Elf_Internal_Rela outrel;
5594 bfd_byte *loc;
5595 asection *s;
5596
5597 s = mips_elf_rel_dyn_section (info, FALSE);
5598 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5599
5600 outrel.r_offset = (input_section->output_section->vma
5601 + input_section->output_offset
5602 + relocation->r_offset);
5603 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5604 outrel.r_addend = addend;
5605 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5606
5607 /* If we've written this relocation for a readonly section,
5608 we need to set DF_TEXTREL again, so that we do not delete the
5609 DT_TEXTREL tag. */
5610 if (MIPS_ELF_READONLY_SECTION (input_section))
5611 info->flags |= DF_TEXTREL;
5612
0a44bf69
RS
5613 *valuep = 0;
5614 return bfd_reloc_ok;
5615 }
5616
b49e97c9
TS
5617 /* Figure out what kind of relocation is being performed. */
5618 switch (r_type)
5619 {
5620 case R_MIPS_NONE:
5621 return bfd_reloc_continue;
5622
5623 case R_MIPS_16:
a7ebbfdf 5624 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5625 overflowed_p = mips_elf_overflow_p (value, 16);
5626 break;
5627
5628 case R_MIPS_32:
5629 case R_MIPS_REL32:
5630 case R_MIPS_64:
5631 if ((info->shared
861fb55a 5632 || (htab->root.dynamic_sections_created
b49e97c9 5633 && h != NULL
f5385ebf 5634 && h->root.def_dynamic
861fb55a
DJ
5635 && !h->root.def_regular
5636 && !h->has_static_relocs))
cf35638d 5637 && r_symndx != STN_UNDEF
9a59ad6b
DJ
5638 && (h == NULL
5639 || h->root.root.type != bfd_link_hash_undefweak
5640 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5641 && (input_section->flags & SEC_ALLOC) != 0)
5642 {
861fb55a 5643 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5644 where the symbol will end up. So, we create a relocation
5645 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5646 linker. We must do the same for executable references to
5647 shared library symbols, unless we've decided to use copy
5648 relocs or PLTs instead. */
b49e97c9
TS
5649 value = addend;
5650 if (!mips_elf_create_dynamic_relocation (abfd,
5651 info,
5652 relocation,
5653 h,
5654 sec,
5655 symbol,
5656 &value,
5657 input_section))
5658 return bfd_reloc_undefined;
5659 }
5660 else
5661 {
5662 if (r_type != R_MIPS_REL32)
5663 value = symbol + addend;
5664 else
5665 value = addend;
5666 }
5667 value &= howto->dst_mask;
092dcd75
CD
5668 break;
5669
5670 case R_MIPS_PC32:
5671 value = symbol + addend - p;
5672 value &= howto->dst_mask;
b49e97c9
TS
5673 break;
5674
b49e97c9
TS
5675 case R_MIPS16_26:
5676 /* The calculation for R_MIPS16_26 is just the same as for an
5677 R_MIPS_26. It's only the storage of the relocated field into
5678 the output file that's different. That's handled in
5679 mips_elf_perform_relocation. So, we just fall through to the
5680 R_MIPS_26 case here. */
5681 case R_MIPS_26:
df58fc94
RS
5682 case R_MICROMIPS_26_S1:
5683 {
5684 unsigned int shift;
5685
5686 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5687 the correct ISA mode selector and bit 1 must be 0. */
5688 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5689 return bfd_reloc_outofrange;
5690
5691 /* Shift is 2, unusually, for microMIPS JALX. */
5692 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5693
5694 if (was_local_p)
5695 value = addend | ((p + 4) & (0xfc000000 << shift));
5696 else
5697 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5698 value = (value + symbol) >> shift;
5699 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5700 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5701 value &= howto->dst_mask;
5702 }
b49e97c9
TS
5703 break;
5704
0f20cc35 5705 case R_MIPS_TLS_DTPREL_HI16:
d0f13682 5706 case R_MIPS16_TLS_DTPREL_HI16:
df58fc94 5707 case R_MICROMIPS_TLS_DTPREL_HI16:
0f20cc35
DJ
5708 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5709 & howto->dst_mask);
5710 break;
5711
5712 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5713 case R_MIPS_TLS_DTPREL32:
5714 case R_MIPS_TLS_DTPREL64:
d0f13682 5715 case R_MIPS16_TLS_DTPREL_LO16:
df58fc94 5716 case R_MICROMIPS_TLS_DTPREL_LO16:
0f20cc35
DJ
5717 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5718 break;
5719
5720 case R_MIPS_TLS_TPREL_HI16:
d0f13682 5721 case R_MIPS16_TLS_TPREL_HI16:
df58fc94 5722 case R_MICROMIPS_TLS_TPREL_HI16:
0f20cc35
DJ
5723 value = (mips_elf_high (addend + symbol - tprel_base (info))
5724 & howto->dst_mask);
5725 break;
5726
5727 case R_MIPS_TLS_TPREL_LO16:
d0f13682
CLT
5728 case R_MIPS_TLS_TPREL32:
5729 case R_MIPS_TLS_TPREL64:
5730 case R_MIPS16_TLS_TPREL_LO16:
df58fc94 5731 case R_MICROMIPS_TLS_TPREL_LO16:
0f20cc35
DJ
5732 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5733 break;
5734
b49e97c9 5735 case R_MIPS_HI16:
d6f16593 5736 case R_MIPS16_HI16:
df58fc94 5737 case R_MICROMIPS_HI16:
b49e97c9
TS
5738 if (!gp_disp_p)
5739 {
5740 value = mips_elf_high (addend + symbol);
5741 value &= howto->dst_mask;
5742 }
5743 else
5744 {
d6f16593
MR
5745 /* For MIPS16 ABI code we generate this sequence
5746 0: li $v0,%hi(_gp_disp)
5747 4: addiupc $v1,%lo(_gp_disp)
5748 8: sll $v0,16
5749 12: addu $v0,$v1
5750 14: move $gp,$v0
5751 So the offsets of hi and lo relocs are the same, but the
888b9c01
CLT
5752 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5753 ADDIUPC clears the low two bits of the instruction address,
5754 so the base is ($t9 + 4) & ~3. */
d6f16593 5755 if (r_type == R_MIPS16_HI16)
888b9c01 5756 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
df58fc94
RS
5757 /* The microMIPS .cpload sequence uses the same assembly
5758 instructions as the traditional psABI version, but the
5759 incoming $t9 has the low bit set. */
5760 else if (r_type == R_MICROMIPS_HI16)
5761 value = mips_elf_high (addend + gp - p - 1);
d6f16593
MR
5762 else
5763 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5764 overflowed_p = mips_elf_overflow_p (value, 16);
5765 }
5766 break;
5767
5768 case R_MIPS_LO16:
d6f16593 5769 case R_MIPS16_LO16:
df58fc94
RS
5770 case R_MICROMIPS_LO16:
5771 case R_MICROMIPS_HI0_LO16:
b49e97c9
TS
5772 if (!gp_disp_p)
5773 value = (symbol + addend) & howto->dst_mask;
5774 else
5775 {
d6f16593
MR
5776 /* See the comment for R_MIPS16_HI16 above for the reason
5777 for this conditional. */
5778 if (r_type == R_MIPS16_LO16)
888b9c01 5779 value = addend + gp - (p & ~(bfd_vma) 0x3);
df58fc94
RS
5780 else if (r_type == R_MICROMIPS_LO16
5781 || r_type == R_MICROMIPS_HI0_LO16)
5782 value = addend + gp - p + 3;
d6f16593
MR
5783 else
5784 value = addend + gp - p + 4;
b49e97c9 5785 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5786 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5787 _gp_disp are normally generated from the .cpload
5788 pseudo-op. It generates code that normally looks like
5789 this:
5790
5791 lui $gp,%hi(_gp_disp)
5792 addiu $gp,$gp,%lo(_gp_disp)
5793 addu $gp,$gp,$t9
5794
5795 Here $t9 holds the address of the function being called,
5796 as required by the MIPS ELF ABI. The R_MIPS_LO16
5797 relocation can easily overflow in this situation, but the
5798 R_MIPS_HI16 relocation will handle the overflow.
5799 Therefore, we consider this a bug in the MIPS ABI, and do
5800 not check for overflow here. */
5801 }
5802 break;
5803
5804 case R_MIPS_LITERAL:
df58fc94 5805 case R_MICROMIPS_LITERAL:
b49e97c9
TS
5806 /* Because we don't merge literal sections, we can handle this
5807 just like R_MIPS_GPREL16. In the long run, we should merge
5808 shared literals, and then we will need to additional work
5809 here. */
5810
5811 /* Fall through. */
5812
5813 case R_MIPS16_GPREL:
5814 /* The R_MIPS16_GPREL performs the same calculation as
5815 R_MIPS_GPREL16, but stores the relocated bits in a different
5816 order. We don't need to do anything special here; the
5817 differences are handled in mips_elf_perform_relocation. */
5818 case R_MIPS_GPREL16:
df58fc94
RS
5819 case R_MICROMIPS_GPREL7_S2:
5820 case R_MICROMIPS_GPREL16:
bce03d3d
AO
5821 /* Only sign-extend the addend if it was extracted from the
5822 instruction. If the addend was separate, leave it alone,
5823 otherwise we may lose significant bits. */
5824 if (howto->partial_inplace)
a7ebbfdf 5825 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5826 value = symbol + addend - gp;
5827 /* If the symbol was local, any earlier relocatable links will
5828 have adjusted its addend with the gp offset, so compensate
5829 for that now. Don't do it for symbols forced local in this
5830 link, though, since they won't have had the gp offset applied
5831 to them before. */
5832 if (was_local_p)
5833 value += gp0;
b49e97c9
TS
5834 overflowed_p = mips_elf_overflow_p (value, 16);
5835 break;
5836
738e5348
RS
5837 case R_MIPS16_GOT16:
5838 case R_MIPS16_CALL16:
b49e97c9
TS
5839 case R_MIPS_GOT16:
5840 case R_MIPS_CALL16:
df58fc94
RS
5841 case R_MICROMIPS_GOT16:
5842 case R_MICROMIPS_CALL16:
0a44bf69 5843 /* VxWorks does not have separate local and global semantics for
738e5348 5844 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5845 if (!htab->is_vxworks && local_p)
b49e97c9 5846 {
5c18022e 5847 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 5848 symbol + addend, !was_local_p);
b49e97c9
TS
5849 if (value == MINUS_ONE)
5850 return bfd_reloc_outofrange;
5851 value
a8028dd0 5852 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5853 overflowed_p = mips_elf_overflow_p (value, 16);
5854 break;
5855 }
5856
5857 /* Fall through. */
5858
0f20cc35
DJ
5859 case R_MIPS_TLS_GD:
5860 case R_MIPS_TLS_GOTTPREL:
5861 case R_MIPS_TLS_LDM:
b49e97c9 5862 case R_MIPS_GOT_DISP:
d0f13682
CLT
5863 case R_MIPS16_TLS_GD:
5864 case R_MIPS16_TLS_GOTTPREL:
5865 case R_MIPS16_TLS_LDM:
df58fc94
RS
5866 case R_MICROMIPS_TLS_GD:
5867 case R_MICROMIPS_TLS_GOTTPREL:
5868 case R_MICROMIPS_TLS_LDM:
5869 case R_MICROMIPS_GOT_DISP:
b49e97c9
TS
5870 value = g;
5871 overflowed_p = mips_elf_overflow_p (value, 16);
5872 break;
5873
5874 case R_MIPS_GPREL32:
bce03d3d
AO
5875 value = (addend + symbol + gp0 - gp);
5876 if (!save_addend)
5877 value &= howto->dst_mask;
b49e97c9
TS
5878 break;
5879
5880 case R_MIPS_PC16:
bad36eac
DJ
5881 case R_MIPS_GNU_REL16_S2:
5882 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5883 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5884 value >>= howto->rightshift;
5885 value &= howto->dst_mask;
b49e97c9
TS
5886 break;
5887
df58fc94
RS
5888 case R_MICROMIPS_PC7_S1:
5889 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5890 overflowed_p = mips_elf_overflow_p (value, 8);
5891 value >>= howto->rightshift;
5892 value &= howto->dst_mask;
5893 break;
5894
5895 case R_MICROMIPS_PC10_S1:
5896 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5897 overflowed_p = mips_elf_overflow_p (value, 11);
5898 value >>= howto->rightshift;
5899 value &= howto->dst_mask;
5900 break;
5901
5902 case R_MICROMIPS_PC16_S1:
5903 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5904 overflowed_p = mips_elf_overflow_p (value, 17);
5905 value >>= howto->rightshift;
5906 value &= howto->dst_mask;
5907 break;
5908
5909 case R_MICROMIPS_PC23_S2:
5910 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5911 overflowed_p = mips_elf_overflow_p (value, 25);
5912 value >>= howto->rightshift;
5913 value &= howto->dst_mask;
5914 break;
5915
b49e97c9
TS
5916 case R_MIPS_GOT_HI16:
5917 case R_MIPS_CALL_HI16:
df58fc94
RS
5918 case R_MICROMIPS_GOT_HI16:
5919 case R_MICROMIPS_CALL_HI16:
b49e97c9
TS
5920 /* We're allowed to handle these two relocations identically.
5921 The dynamic linker is allowed to handle the CALL relocations
5922 differently by creating a lazy evaluation stub. */
5923 value = g;
5924 value = mips_elf_high (value);
5925 value &= howto->dst_mask;
5926 break;
5927
5928 case R_MIPS_GOT_LO16:
5929 case R_MIPS_CALL_LO16:
df58fc94
RS
5930 case R_MICROMIPS_GOT_LO16:
5931 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
5932 value = g & howto->dst_mask;
5933 break;
5934
5935 case R_MIPS_GOT_PAGE:
df58fc94 5936 case R_MICROMIPS_GOT_PAGE:
5c18022e 5937 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5938 if (value == MINUS_ONE)
5939 return bfd_reloc_outofrange;
a8028dd0 5940 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5941 overflowed_p = mips_elf_overflow_p (value, 16);
5942 break;
5943
5944 case R_MIPS_GOT_OFST:
df58fc94 5945 case R_MICROMIPS_GOT_OFST:
93a2b7ae 5946 if (local_p)
5c18022e 5947 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5948 else
5949 value = addend;
b49e97c9
TS
5950 overflowed_p = mips_elf_overflow_p (value, 16);
5951 break;
5952
5953 case R_MIPS_SUB:
df58fc94 5954 case R_MICROMIPS_SUB:
b49e97c9
TS
5955 value = symbol - addend;
5956 value &= howto->dst_mask;
5957 break;
5958
5959 case R_MIPS_HIGHER:
df58fc94 5960 case R_MICROMIPS_HIGHER:
b49e97c9
TS
5961 value = mips_elf_higher (addend + symbol);
5962 value &= howto->dst_mask;
5963 break;
5964
5965 case R_MIPS_HIGHEST:
df58fc94 5966 case R_MICROMIPS_HIGHEST:
b49e97c9
TS
5967 value = mips_elf_highest (addend + symbol);
5968 value &= howto->dst_mask;
5969 break;
5970
5971 case R_MIPS_SCN_DISP:
df58fc94 5972 case R_MICROMIPS_SCN_DISP:
b49e97c9
TS
5973 value = symbol + addend - sec->output_offset;
5974 value &= howto->dst_mask;
5975 break;
5976
b49e97c9 5977 case R_MIPS_JALR:
df58fc94 5978 case R_MICROMIPS_JALR:
1367d393
ILT
5979 /* This relocation is only a hint. In some cases, we optimize
5980 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5981 when the symbol does not resolve locally. */
5982 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5983 return bfd_reloc_continue;
5984 value = symbol + addend;
5985 break;
b49e97c9 5986
1367d393 5987 case R_MIPS_PJUMP:
b49e97c9
TS
5988 case R_MIPS_GNU_VTINHERIT:
5989 case R_MIPS_GNU_VTENTRY:
5990 /* We don't do anything with these at present. */
5991 return bfd_reloc_continue;
5992
5993 default:
5994 /* An unrecognized relocation type. */
5995 return bfd_reloc_notsupported;
5996 }
5997
5998 /* Store the VALUE for our caller. */
5999 *valuep = value;
6000 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6001}
6002
6003/* Obtain the field relocated by RELOCATION. */
6004
6005static bfd_vma
9719ad41
RS
6006mips_elf_obtain_contents (reloc_howto_type *howto,
6007 const Elf_Internal_Rela *relocation,
6008 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
6009{
6010 bfd_vma x;
6011 bfd_byte *location = contents + relocation->r_offset;
6012
6013 /* Obtain the bytes. */
6014 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
6015
b49e97c9
TS
6016 return x;
6017}
6018
6019/* It has been determined that the result of the RELOCATION is the
6020 VALUE. Use HOWTO to place VALUE into the output file at the
6021 appropriate position. The SECTION is the section to which the
68ffbac6 6022 relocation applies.
38a7df63 6023 CROSS_MODE_JUMP_P is true if the relocation field
df58fc94 6024 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9 6025
b34976b6 6026 Returns FALSE if anything goes wrong. */
b49e97c9 6027
b34976b6 6028static bfd_boolean
9719ad41
RS
6029mips_elf_perform_relocation (struct bfd_link_info *info,
6030 reloc_howto_type *howto,
6031 const Elf_Internal_Rela *relocation,
6032 bfd_vma value, bfd *input_bfd,
6033 asection *input_section, bfd_byte *contents,
38a7df63 6034 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
6035{
6036 bfd_vma x;
6037 bfd_byte *location;
6038 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6039
6040 /* Figure out where the relocation is occurring. */
6041 location = contents + relocation->r_offset;
6042
df58fc94 6043 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
d6f16593 6044
b49e97c9
TS
6045 /* Obtain the current value. */
6046 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6047
6048 /* Clear the field we are setting. */
6049 x &= ~howto->dst_mask;
6050
b49e97c9
TS
6051 /* Set the field. */
6052 x |= (value & howto->dst_mask);
6053
6054 /* If required, turn JAL into JALX. */
38a7df63 6055 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 6056 {
b34976b6 6057 bfd_boolean ok;
b49e97c9
TS
6058 bfd_vma opcode = x >> 26;
6059 bfd_vma jalx_opcode;
6060
6061 /* Check to see if the opcode is already JAL or JALX. */
6062 if (r_type == R_MIPS16_26)
6063 {
6064 ok = ((opcode == 0x6) || (opcode == 0x7));
6065 jalx_opcode = 0x7;
6066 }
df58fc94
RS
6067 else if (r_type == R_MICROMIPS_26_S1)
6068 {
6069 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6070 jalx_opcode = 0x3c;
6071 }
b49e97c9
TS
6072 else
6073 {
6074 ok = ((opcode == 0x3) || (opcode == 0x1d));
6075 jalx_opcode = 0x1d;
6076 }
6077
3bdf9505
MR
6078 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6079 convert J or JALS to JALX. */
b49e97c9
TS
6080 if (!ok)
6081 {
6082 (*_bfd_error_handler)
3bdf9505 6083 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
d003868e
AM
6084 input_bfd,
6085 input_section,
b49e97c9
TS
6086 (unsigned long) relocation->r_offset);
6087 bfd_set_error (bfd_error_bad_value);
b34976b6 6088 return FALSE;
b49e97c9
TS
6089 }
6090
6091 /* Make this the JALX opcode. */
6092 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6093 }
6094
38a7df63
CF
6095 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6096 range. */
cd8d5a82 6097 if (!info->relocatable
38a7df63 6098 && !cross_mode_jump_p
cd8d5a82
CF
6099 && ((JAL_TO_BAL_P (input_bfd)
6100 && r_type == R_MIPS_26
6101 && (x >> 26) == 0x3) /* jal addr */
6102 || (JALR_TO_BAL_P (input_bfd)
6103 && r_type == R_MIPS_JALR
38a7df63
CF
6104 && x == 0x0320f809) /* jalr t9 */
6105 || (JR_TO_B_P (input_bfd)
6106 && r_type == R_MIPS_JALR
6107 && x == 0x03200008))) /* jr t9 */
1367d393
ILT
6108 {
6109 bfd_vma addr;
6110 bfd_vma dest;
6111 bfd_signed_vma off;
6112
6113 addr = (input_section->output_section->vma
6114 + input_section->output_offset
6115 + relocation->r_offset
6116 + 4);
6117 if (r_type == R_MIPS_26)
6118 dest = (value << 2) | ((addr >> 28) << 28);
6119 else
6120 dest = value;
6121 off = dest - addr;
6122 if (off <= 0x1ffff && off >= -0x20000)
38a7df63
CF
6123 {
6124 if (x == 0x03200008) /* jr t9 */
6125 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6126 else
6127 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6128 }
1367d393
ILT
6129 }
6130
b49e97c9
TS
6131 /* Put the value into the output. */
6132 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593 6133
df58fc94
RS
6134 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
6135 location);
d6f16593 6136
b34976b6 6137 return TRUE;
b49e97c9 6138}
b49e97c9 6139\f
b49e97c9
TS
6140/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6141 is the original relocation, which is now being transformed into a
6142 dynamic relocation. The ADDENDP is adjusted if necessary; the
6143 caller should store the result in place of the original addend. */
6144
b34976b6 6145static bfd_boolean
9719ad41
RS
6146mips_elf_create_dynamic_relocation (bfd *output_bfd,
6147 struct bfd_link_info *info,
6148 const Elf_Internal_Rela *rel,
6149 struct mips_elf_link_hash_entry *h,
6150 asection *sec, bfd_vma symbol,
6151 bfd_vma *addendp, asection *input_section)
b49e97c9 6152{
947216bf 6153 Elf_Internal_Rela outrel[3];
b49e97c9
TS
6154 asection *sreloc;
6155 bfd *dynobj;
6156 int r_type;
5d41f0b6
RS
6157 long indx;
6158 bfd_boolean defined_p;
0a44bf69 6159 struct mips_elf_link_hash_table *htab;
b49e97c9 6160
0a44bf69 6161 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6162 BFD_ASSERT (htab != NULL);
6163
b49e97c9
TS
6164 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6165 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 6166 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
6167 BFD_ASSERT (sreloc != NULL);
6168 BFD_ASSERT (sreloc->contents != NULL);
6169 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 6170 < sreloc->size);
b49e97c9 6171
b49e97c9
TS
6172 outrel[0].r_offset =
6173 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
6174 if (ABI_64_P (output_bfd))
6175 {
6176 outrel[1].r_offset =
6177 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6178 outrel[2].r_offset =
6179 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6180 }
b49e97c9 6181
c5ae1840 6182 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 6183 /* The relocation field has been deleted. */
5d41f0b6
RS
6184 return TRUE;
6185
6186 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
6187 {
6188 /* The relocation field has been converted into a relative value of
6189 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6190 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 6191 *addendp += symbol;
5d41f0b6 6192 return TRUE;
0d591ff7 6193 }
b49e97c9 6194
5d41f0b6
RS
6195 /* We must now calculate the dynamic symbol table index to use
6196 in the relocation. */
d4a77f3f 6197 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 6198 {
020d7251 6199 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5d41f0b6
RS
6200 indx = h->root.dynindx;
6201 if (SGI_COMPAT (output_bfd))
6202 defined_p = h->root.def_regular;
6203 else
6204 /* ??? glibc's ld.so just adds the final GOT entry to the
6205 relocation field. It therefore treats relocs against
6206 defined symbols in the same way as relocs against
6207 undefined symbols. */
6208 defined_p = FALSE;
6209 }
b49e97c9
TS
6210 else
6211 {
5d41f0b6
RS
6212 if (sec != NULL && bfd_is_abs_section (sec))
6213 indx = 0;
6214 else if (sec == NULL || sec->owner == NULL)
fdd07405 6215 {
5d41f0b6
RS
6216 bfd_set_error (bfd_error_bad_value);
6217 return FALSE;
b49e97c9
TS
6218 }
6219 else
6220 {
5d41f0b6 6221 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
6222 if (indx == 0)
6223 {
6224 asection *osec = htab->root.text_index_section;
6225 indx = elf_section_data (osec)->dynindx;
6226 }
5d41f0b6
RS
6227 if (indx == 0)
6228 abort ();
b49e97c9
TS
6229 }
6230
5d41f0b6
RS
6231 /* Instead of generating a relocation using the section
6232 symbol, we may as well make it a fully relative
6233 relocation. We want to avoid generating relocations to
6234 local symbols because we used to generate them
6235 incorrectly, without adding the original symbol value,
6236 which is mandated by the ABI for section symbols. In
6237 order to give dynamic loaders and applications time to
6238 phase out the incorrect use, we refrain from emitting
6239 section-relative relocations. It's not like they're
6240 useful, after all. This should be a bit more efficient
6241 as well. */
6242 /* ??? Although this behavior is compatible with glibc's ld.so,
6243 the ABI says that relocations against STN_UNDEF should have
6244 a symbol value of 0. Irix rld honors this, so relocations
6245 against STN_UNDEF have no effect. */
6246 if (!SGI_COMPAT (output_bfd))
6247 indx = 0;
6248 defined_p = TRUE;
b49e97c9
TS
6249 }
6250
5d41f0b6
RS
6251 /* If the relocation was previously an absolute relocation and
6252 this symbol will not be referred to by the relocation, we must
6253 adjust it by the value we give it in the dynamic symbol table.
6254 Otherwise leave the job up to the dynamic linker. */
6255 if (defined_p && r_type != R_MIPS_REL32)
6256 *addendp += symbol;
6257
0a44bf69
RS
6258 if (htab->is_vxworks)
6259 /* VxWorks uses non-relative relocations for this. */
6260 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6261 else
6262 /* The relocation is always an REL32 relocation because we don't
6263 know where the shared library will wind up at load-time. */
6264 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6265 R_MIPS_REL32);
6266
5d41f0b6
RS
6267 /* For strict adherence to the ABI specification, we should
6268 generate a R_MIPS_64 relocation record by itself before the
6269 _REL32/_64 record as well, such that the addend is read in as
6270 a 64-bit value (REL32 is a 32-bit relocation, after all).
6271 However, since none of the existing ELF64 MIPS dynamic
6272 loaders seems to care, we don't waste space with these
6273 artificial relocations. If this turns out to not be true,
6274 mips_elf_allocate_dynamic_relocation() should be tweaked so
6275 as to make room for a pair of dynamic relocations per
6276 invocation if ABI_64_P, and here we should generate an
6277 additional relocation record with R_MIPS_64 by itself for a
6278 NULL symbol before this relocation record. */
6279 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6280 ABI_64_P (output_bfd)
6281 ? R_MIPS_64
6282 : R_MIPS_NONE);
6283 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6284
6285 /* Adjust the output offset of the relocation to reference the
6286 correct location in the output file. */
6287 outrel[0].r_offset += (input_section->output_section->vma
6288 + input_section->output_offset);
6289 outrel[1].r_offset += (input_section->output_section->vma
6290 + input_section->output_offset);
6291 outrel[2].r_offset += (input_section->output_section->vma
6292 + input_section->output_offset);
6293
b49e97c9
TS
6294 /* Put the relocation back out. We have to use the special
6295 relocation outputter in the 64-bit case since the 64-bit
6296 relocation format is non-standard. */
6297 if (ABI_64_P (output_bfd))
6298 {
6299 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6300 (output_bfd, &outrel[0],
6301 (sreloc->contents
6302 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6303 }
0a44bf69
RS
6304 else if (htab->is_vxworks)
6305 {
6306 /* VxWorks uses RELA rather than REL dynamic relocations. */
6307 outrel[0].r_addend = *addendp;
6308 bfd_elf32_swap_reloca_out
6309 (output_bfd, &outrel[0],
6310 (sreloc->contents
6311 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6312 }
b49e97c9 6313 else
947216bf
AM
6314 bfd_elf32_swap_reloc_out
6315 (output_bfd, &outrel[0],
6316 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 6317
b49e97c9
TS
6318 /* We've now added another relocation. */
6319 ++sreloc->reloc_count;
6320
6321 /* Make sure the output section is writable. The dynamic linker
6322 will be writing to it. */
6323 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6324 |= SHF_WRITE;
6325
6326 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 6327 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9 6328 {
3d4d4302 6329 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
6330 bfd_byte *cr;
6331
6332 if (scpt)
6333 {
6334 Elf32_crinfo cptrel;
6335
6336 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6337 cptrel.vaddr = (rel->r_offset
6338 + input_section->output_section->vma
6339 + input_section->output_offset);
6340 if (r_type == R_MIPS_REL32)
6341 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6342 else
6343 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6344 mips_elf_set_cr_dist2to (cptrel, 0);
6345 cptrel.konst = *addendp;
6346
6347 cr = (scpt->contents
6348 + sizeof (Elf32_External_compact_rel));
abc0f8d0 6349 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
6350 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6351 ((Elf32_External_crinfo *) cr
6352 + scpt->reloc_count));
6353 ++scpt->reloc_count;
6354 }
6355 }
6356
943284cc
DJ
6357 /* If we've written this relocation for a readonly section,
6358 we need to set DF_TEXTREL again, so that we do not delete the
6359 DT_TEXTREL tag. */
6360 if (MIPS_ELF_READONLY_SECTION (input_section))
6361 info->flags |= DF_TEXTREL;
6362
b34976b6 6363 return TRUE;
b49e97c9
TS
6364}
6365\f
b49e97c9
TS
6366/* Return the MACH for a MIPS e_flags value. */
6367
6368unsigned long
9719ad41 6369_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
6370{
6371 switch (flags & EF_MIPS_MACH)
6372 {
6373 case E_MIPS_MACH_3900:
6374 return bfd_mach_mips3900;
6375
6376 case E_MIPS_MACH_4010:
6377 return bfd_mach_mips4010;
6378
6379 case E_MIPS_MACH_4100:
6380 return bfd_mach_mips4100;
6381
6382 case E_MIPS_MACH_4111:
6383 return bfd_mach_mips4111;
6384
00707a0e
RS
6385 case E_MIPS_MACH_4120:
6386 return bfd_mach_mips4120;
6387
b49e97c9
TS
6388 case E_MIPS_MACH_4650:
6389 return bfd_mach_mips4650;
6390
00707a0e
RS
6391 case E_MIPS_MACH_5400:
6392 return bfd_mach_mips5400;
6393
6394 case E_MIPS_MACH_5500:
6395 return bfd_mach_mips5500;
6396
e407c74b
NC
6397 case E_MIPS_MACH_5900:
6398 return bfd_mach_mips5900;
6399
0d2e43ed
ILT
6400 case E_MIPS_MACH_9000:
6401 return bfd_mach_mips9000;
6402
b49e97c9
TS
6403 case E_MIPS_MACH_SB1:
6404 return bfd_mach_mips_sb1;
6405
350cc38d
MS
6406 case E_MIPS_MACH_LS2E:
6407 return bfd_mach_mips_loongson_2e;
6408
6409 case E_MIPS_MACH_LS2F:
6410 return bfd_mach_mips_loongson_2f;
6411
fd503541
NC
6412 case E_MIPS_MACH_LS3A:
6413 return bfd_mach_mips_loongson_3a;
6414
432233b3
AP
6415 case E_MIPS_MACH_OCTEON2:
6416 return bfd_mach_mips_octeon2;
6417
6f179bd0
AN
6418 case E_MIPS_MACH_OCTEON:
6419 return bfd_mach_mips_octeon;
6420
52b6b6b9
JM
6421 case E_MIPS_MACH_XLR:
6422 return bfd_mach_mips_xlr;
6423
b49e97c9
TS
6424 default:
6425 switch (flags & EF_MIPS_ARCH)
6426 {
6427 default:
6428 case E_MIPS_ARCH_1:
6429 return bfd_mach_mips3000;
b49e97c9
TS
6430
6431 case E_MIPS_ARCH_2:
6432 return bfd_mach_mips6000;
b49e97c9
TS
6433
6434 case E_MIPS_ARCH_3:
6435 return bfd_mach_mips4000;
b49e97c9
TS
6436
6437 case E_MIPS_ARCH_4:
6438 return bfd_mach_mips8000;
b49e97c9
TS
6439
6440 case E_MIPS_ARCH_5:
6441 return bfd_mach_mips5;
b49e97c9
TS
6442
6443 case E_MIPS_ARCH_32:
6444 return bfd_mach_mipsisa32;
b49e97c9
TS
6445
6446 case E_MIPS_ARCH_64:
6447 return bfd_mach_mipsisa64;
af7ee8bf
CD
6448
6449 case E_MIPS_ARCH_32R2:
6450 return bfd_mach_mipsisa32r2;
5f74bc13
CD
6451
6452 case E_MIPS_ARCH_64R2:
6453 return bfd_mach_mipsisa64r2;
b49e97c9
TS
6454 }
6455 }
6456
6457 return 0;
6458}
6459
6460/* Return printable name for ABI. */
6461
6462static INLINE char *
9719ad41 6463elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
6464{
6465 flagword flags;
6466
6467 flags = elf_elfheader (abfd)->e_flags;
6468 switch (flags & EF_MIPS_ABI)
6469 {
6470 case 0:
6471 if (ABI_N32_P (abfd))
6472 return "N32";
6473 else if (ABI_64_P (abfd))
6474 return "64";
6475 else
6476 return "none";
6477 case E_MIPS_ABI_O32:
6478 return "O32";
6479 case E_MIPS_ABI_O64:
6480 return "O64";
6481 case E_MIPS_ABI_EABI32:
6482 return "EABI32";
6483 case E_MIPS_ABI_EABI64:
6484 return "EABI64";
6485 default:
6486 return "unknown abi";
6487 }
6488}
6489\f
6490/* MIPS ELF uses two common sections. One is the usual one, and the
6491 other is for small objects. All the small objects are kept
6492 together, and then referenced via the gp pointer, which yields
6493 faster assembler code. This is what we use for the small common
6494 section. This approach is copied from ecoff.c. */
6495static asection mips_elf_scom_section;
6496static asymbol mips_elf_scom_symbol;
6497static asymbol *mips_elf_scom_symbol_ptr;
6498
6499/* MIPS ELF also uses an acommon section, which represents an
6500 allocated common symbol which may be overridden by a
6501 definition in a shared library. */
6502static asection mips_elf_acom_section;
6503static asymbol mips_elf_acom_symbol;
6504static asymbol *mips_elf_acom_symbol_ptr;
6505
738e5348 6506/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
6507
6508void
9719ad41 6509_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
6510{
6511 elf_symbol_type *elfsym;
6512
738e5348 6513 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6514 elfsym = (elf_symbol_type *) asym;
6515 switch (elfsym->internal_elf_sym.st_shndx)
6516 {
6517 case SHN_MIPS_ACOMMON:
6518 /* This section is used in a dynamically linked executable file.
6519 It is an allocated common section. The dynamic linker can
6520 either resolve these symbols to something in a shared
6521 library, or it can just leave them here. For our purposes,
6522 we can consider these symbols to be in a new section. */
6523 if (mips_elf_acom_section.name == NULL)
6524 {
6525 /* Initialize the acommon section. */
6526 mips_elf_acom_section.name = ".acommon";
6527 mips_elf_acom_section.flags = SEC_ALLOC;
6528 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6529 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6530 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6531 mips_elf_acom_symbol.name = ".acommon";
6532 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6533 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6534 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6535 }
6536 asym->section = &mips_elf_acom_section;
6537 break;
6538
6539 case SHN_COMMON:
6540 /* Common symbols less than the GP size are automatically
6541 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6542 if (asym->value > elf_gp_size (abfd)
b59eed79 6543 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6544 || IRIX_COMPAT (abfd) == ict_irix6)
6545 break;
6546 /* Fall through. */
6547 case SHN_MIPS_SCOMMON:
6548 if (mips_elf_scom_section.name == NULL)
6549 {
6550 /* Initialize the small common section. */
6551 mips_elf_scom_section.name = ".scommon";
6552 mips_elf_scom_section.flags = SEC_IS_COMMON;
6553 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6554 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6555 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6556 mips_elf_scom_symbol.name = ".scommon";
6557 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6558 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6559 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6560 }
6561 asym->section = &mips_elf_scom_section;
6562 asym->value = elfsym->internal_elf_sym.st_size;
6563 break;
6564
6565 case SHN_MIPS_SUNDEFINED:
6566 asym->section = bfd_und_section_ptr;
6567 break;
6568
b49e97c9 6569 case SHN_MIPS_TEXT:
00b4930b
TS
6570 {
6571 asection *section = bfd_get_section_by_name (abfd, ".text");
6572
00b4930b
TS
6573 if (section != NULL)
6574 {
6575 asym->section = section;
6576 /* MIPS_TEXT is a bit special, the address is not an offset
6577 to the base of the .text section. So substract the section
6578 base address to make it an offset. */
6579 asym->value -= section->vma;
6580 }
6581 }
b49e97c9
TS
6582 break;
6583
6584 case SHN_MIPS_DATA:
00b4930b
TS
6585 {
6586 asection *section = bfd_get_section_by_name (abfd, ".data");
6587
00b4930b
TS
6588 if (section != NULL)
6589 {
6590 asym->section = section;
6591 /* MIPS_DATA is a bit special, the address is not an offset
6592 to the base of the .data section. So substract the section
6593 base address to make it an offset. */
6594 asym->value -= section->vma;
6595 }
6596 }
b49e97c9 6597 break;
b49e97c9 6598 }
738e5348 6599
df58fc94
RS
6600 /* If this is an odd-valued function symbol, assume it's a MIPS16
6601 or microMIPS one. */
738e5348
RS
6602 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6603 && (asym->value & 1) != 0)
6604 {
6605 asym->value--;
e8faf7d1 6606 if (MICROMIPS_P (abfd))
df58fc94
RS
6607 elfsym->internal_elf_sym.st_other
6608 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6609 else
6610 elfsym->internal_elf_sym.st_other
6611 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
738e5348 6612 }
b49e97c9
TS
6613}
6614\f
8c946ed5
RS
6615/* Implement elf_backend_eh_frame_address_size. This differs from
6616 the default in the way it handles EABI64.
6617
6618 EABI64 was originally specified as an LP64 ABI, and that is what
6619 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6620 historically accepted the combination of -mabi=eabi and -mlong32,
6621 and this ILP32 variation has become semi-official over time.
6622 Both forms use elf32 and have pointer-sized FDE addresses.
6623
6624 If an EABI object was generated by GCC 4.0 or above, it will have
6625 an empty .gcc_compiled_longXX section, where XX is the size of longs
6626 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6627 have no special marking to distinguish them from LP64 objects.
6628
6629 We don't want users of the official LP64 ABI to be punished for the
6630 existence of the ILP32 variant, but at the same time, we don't want
6631 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6632 We therefore take the following approach:
6633
6634 - If ABFD contains a .gcc_compiled_longXX section, use it to
6635 determine the pointer size.
6636
6637 - Otherwise check the type of the first relocation. Assume that
6638 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6639
6640 - Otherwise punt.
6641
6642 The second check is enough to detect LP64 objects generated by pre-4.0
6643 compilers because, in the kind of output generated by those compilers,
6644 the first relocation will be associated with either a CIE personality
6645 routine or an FDE start address. Furthermore, the compilers never
6646 used a special (non-pointer) encoding for this ABI.
6647
6648 Checking the relocation type should also be safe because there is no
6649 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6650 did so. */
6651
6652unsigned int
6653_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6654{
6655 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6656 return 8;
6657 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6658 {
6659 bfd_boolean long32_p, long64_p;
6660
6661 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6662 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6663 if (long32_p && long64_p)
6664 return 0;
6665 if (long32_p)
6666 return 4;
6667 if (long64_p)
6668 return 8;
6669
6670 if (sec->reloc_count > 0
6671 && elf_section_data (sec)->relocs != NULL
6672 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6673 == R_MIPS_64))
6674 return 8;
6675
6676 return 0;
6677 }
6678 return 4;
6679}
6680\f
174fd7f9
RS
6681/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6682 relocations against two unnamed section symbols to resolve to the
6683 same address. For example, if we have code like:
6684
6685 lw $4,%got_disp(.data)($gp)
6686 lw $25,%got_disp(.text)($gp)
6687 jalr $25
6688
6689 then the linker will resolve both relocations to .data and the program
6690 will jump there rather than to .text.
6691
6692 We can work around this problem by giving names to local section symbols.
6693 This is also what the MIPSpro tools do. */
6694
6695bfd_boolean
6696_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6697{
6698 return SGI_COMPAT (abfd);
6699}
6700\f
b49e97c9
TS
6701/* Work over a section just before writing it out. This routine is
6702 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6703 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6704 a better way. */
6705
b34976b6 6706bfd_boolean
9719ad41 6707_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6708{
6709 if (hdr->sh_type == SHT_MIPS_REGINFO
6710 && hdr->sh_size > 0)
6711 {
6712 bfd_byte buf[4];
6713
6714 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6715 BFD_ASSERT (hdr->contents == NULL);
6716
6717 if (bfd_seek (abfd,
6718 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6719 SEEK_SET) != 0)
b34976b6 6720 return FALSE;
b49e97c9 6721 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6722 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6723 return FALSE;
b49e97c9
TS
6724 }
6725
6726 if (hdr->sh_type == SHT_MIPS_OPTIONS
6727 && hdr->bfd_section != NULL
f0abc2a1
AM
6728 && mips_elf_section_data (hdr->bfd_section) != NULL
6729 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6730 {
6731 bfd_byte *contents, *l, *lend;
6732
f0abc2a1
AM
6733 /* We stored the section contents in the tdata field in the
6734 set_section_contents routine. We save the section contents
6735 so that we don't have to read them again.
b49e97c9
TS
6736 At this point we know that elf_gp is set, so we can look
6737 through the section contents to see if there is an
6738 ODK_REGINFO structure. */
6739
f0abc2a1 6740 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6741 l = contents;
6742 lend = contents + hdr->sh_size;
6743 while (l + sizeof (Elf_External_Options) <= lend)
6744 {
6745 Elf_Internal_Options intopt;
6746
6747 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6748 &intopt);
1bc8074d
MR
6749 if (intopt.size < sizeof (Elf_External_Options))
6750 {
6751 (*_bfd_error_handler)
6752 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6753 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6754 break;
6755 }
b49e97c9
TS
6756 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6757 {
6758 bfd_byte buf[8];
6759
6760 if (bfd_seek (abfd,
6761 (hdr->sh_offset
6762 + (l - contents)
6763 + sizeof (Elf_External_Options)
6764 + (sizeof (Elf64_External_RegInfo) - 8)),
6765 SEEK_SET) != 0)
b34976b6 6766 return FALSE;
b49e97c9 6767 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6768 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6769 return FALSE;
b49e97c9
TS
6770 }
6771 else if (intopt.kind == ODK_REGINFO)
6772 {
6773 bfd_byte buf[4];
6774
6775 if (bfd_seek (abfd,
6776 (hdr->sh_offset
6777 + (l - contents)
6778 + sizeof (Elf_External_Options)
6779 + (sizeof (Elf32_External_RegInfo) - 4)),
6780 SEEK_SET) != 0)
b34976b6 6781 return FALSE;
b49e97c9 6782 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6783 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6784 return FALSE;
b49e97c9
TS
6785 }
6786 l += intopt.size;
6787 }
6788 }
6789
6790 if (hdr->bfd_section != NULL)
6791 {
6792 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6793
2d0f9ad9
JM
6794 /* .sbss is not handled specially here because the GNU/Linux
6795 prelinker can convert .sbss from NOBITS to PROGBITS and
6796 changing it back to NOBITS breaks the binary. The entry in
6797 _bfd_mips_elf_special_sections will ensure the correct flags
6798 are set on .sbss if BFD creates it without reading it from an
6799 input file, and without special handling here the flags set
6800 on it in an input file will be followed. */
b49e97c9
TS
6801 if (strcmp (name, ".sdata") == 0
6802 || strcmp (name, ".lit8") == 0
6803 || strcmp (name, ".lit4") == 0)
6804 {
6805 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6806 hdr->sh_type = SHT_PROGBITS;
6807 }
b49e97c9
TS
6808 else if (strcmp (name, ".srdata") == 0)
6809 {
6810 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6811 hdr->sh_type = SHT_PROGBITS;
6812 }
6813 else if (strcmp (name, ".compact_rel") == 0)
6814 {
6815 hdr->sh_flags = 0;
6816 hdr->sh_type = SHT_PROGBITS;
6817 }
6818 else if (strcmp (name, ".rtproc") == 0)
6819 {
6820 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6821 {
6822 unsigned int adjust;
6823
6824 adjust = hdr->sh_size % hdr->sh_addralign;
6825 if (adjust != 0)
6826 hdr->sh_size += hdr->sh_addralign - adjust;
6827 }
6828 }
6829 }
6830
b34976b6 6831 return TRUE;
b49e97c9
TS
6832}
6833
6834/* Handle a MIPS specific section when reading an object file. This
6835 is called when elfcode.h finds a section with an unknown type.
6836 This routine supports both the 32-bit and 64-bit ELF ABI.
6837
6838 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6839 how to. */
6840
b34976b6 6841bfd_boolean
6dc132d9
L
6842_bfd_mips_elf_section_from_shdr (bfd *abfd,
6843 Elf_Internal_Shdr *hdr,
6844 const char *name,
6845 int shindex)
b49e97c9
TS
6846{
6847 flagword flags = 0;
6848
6849 /* There ought to be a place to keep ELF backend specific flags, but
6850 at the moment there isn't one. We just keep track of the
6851 sections by their name, instead. Fortunately, the ABI gives
6852 suggested names for all the MIPS specific sections, so we will
6853 probably get away with this. */
6854 switch (hdr->sh_type)
6855 {
6856 case SHT_MIPS_LIBLIST:
6857 if (strcmp (name, ".liblist") != 0)
b34976b6 6858 return FALSE;
b49e97c9
TS
6859 break;
6860 case SHT_MIPS_MSYM:
6861 if (strcmp (name, ".msym") != 0)
b34976b6 6862 return FALSE;
b49e97c9
TS
6863 break;
6864 case SHT_MIPS_CONFLICT:
6865 if (strcmp (name, ".conflict") != 0)
b34976b6 6866 return FALSE;
b49e97c9
TS
6867 break;
6868 case SHT_MIPS_GPTAB:
0112cd26 6869 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6870 return FALSE;
b49e97c9
TS
6871 break;
6872 case SHT_MIPS_UCODE:
6873 if (strcmp (name, ".ucode") != 0)
b34976b6 6874 return FALSE;
b49e97c9
TS
6875 break;
6876 case SHT_MIPS_DEBUG:
6877 if (strcmp (name, ".mdebug") != 0)
b34976b6 6878 return FALSE;
b49e97c9
TS
6879 flags = SEC_DEBUGGING;
6880 break;
6881 case SHT_MIPS_REGINFO:
6882 if (strcmp (name, ".reginfo") != 0
6883 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6884 return FALSE;
b49e97c9
TS
6885 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6886 break;
6887 case SHT_MIPS_IFACE:
6888 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6889 return FALSE;
b49e97c9
TS
6890 break;
6891 case SHT_MIPS_CONTENT:
0112cd26 6892 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6893 return FALSE;
b49e97c9
TS
6894 break;
6895 case SHT_MIPS_OPTIONS:
cc2e31b9 6896 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6897 return FALSE;
b49e97c9
TS
6898 break;
6899 case SHT_MIPS_DWARF:
1b315056 6900 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6901 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6902 return FALSE;
b49e97c9
TS
6903 break;
6904 case SHT_MIPS_SYMBOL_LIB:
6905 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6906 return FALSE;
b49e97c9
TS
6907 break;
6908 case SHT_MIPS_EVENTS:
0112cd26
NC
6909 if (! CONST_STRNEQ (name, ".MIPS.events")
6910 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6911 return FALSE;
b49e97c9
TS
6912 break;
6913 default:
cc2e31b9 6914 break;
b49e97c9
TS
6915 }
6916
6dc132d9 6917 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6918 return FALSE;
b49e97c9
TS
6919
6920 if (flags)
6921 {
6922 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6923 (bfd_get_section_flags (abfd,
6924 hdr->bfd_section)
6925 | flags)))
b34976b6 6926 return FALSE;
b49e97c9
TS
6927 }
6928
6929 /* FIXME: We should record sh_info for a .gptab section. */
6930
6931 /* For a .reginfo section, set the gp value in the tdata information
6932 from the contents of this section. We need the gp value while
6933 processing relocs, so we just get it now. The .reginfo section
6934 is not used in the 64-bit MIPS ELF ABI. */
6935 if (hdr->sh_type == SHT_MIPS_REGINFO)
6936 {
6937 Elf32_External_RegInfo ext;
6938 Elf32_RegInfo s;
6939
9719ad41
RS
6940 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6941 &ext, 0, sizeof ext))
b34976b6 6942 return FALSE;
b49e97c9
TS
6943 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6944 elf_gp (abfd) = s.ri_gp_value;
6945 }
6946
6947 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6948 set the gp value based on what we find. We may see both
6949 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6950 they should agree. */
6951 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6952 {
6953 bfd_byte *contents, *l, *lend;
6954
9719ad41 6955 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6956 if (contents == NULL)
b34976b6 6957 return FALSE;
b49e97c9 6958 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6959 0, hdr->sh_size))
b49e97c9
TS
6960 {
6961 free (contents);
b34976b6 6962 return FALSE;
b49e97c9
TS
6963 }
6964 l = contents;
6965 lend = contents + hdr->sh_size;
6966 while (l + sizeof (Elf_External_Options) <= lend)
6967 {
6968 Elf_Internal_Options intopt;
6969
6970 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6971 &intopt);
1bc8074d
MR
6972 if (intopt.size < sizeof (Elf_External_Options))
6973 {
6974 (*_bfd_error_handler)
6975 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6976 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6977 break;
6978 }
b49e97c9
TS
6979 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6980 {
6981 Elf64_Internal_RegInfo intreg;
6982
6983 bfd_mips_elf64_swap_reginfo_in
6984 (abfd,
6985 ((Elf64_External_RegInfo *)
6986 (l + sizeof (Elf_External_Options))),
6987 &intreg);
6988 elf_gp (abfd) = intreg.ri_gp_value;
6989 }
6990 else if (intopt.kind == ODK_REGINFO)
6991 {
6992 Elf32_RegInfo intreg;
6993
6994 bfd_mips_elf32_swap_reginfo_in
6995 (abfd,
6996 ((Elf32_External_RegInfo *)
6997 (l + sizeof (Elf_External_Options))),
6998 &intreg);
6999 elf_gp (abfd) = intreg.ri_gp_value;
7000 }
7001 l += intopt.size;
7002 }
7003 free (contents);
7004 }
7005
b34976b6 7006 return TRUE;
b49e97c9
TS
7007}
7008
7009/* Set the correct type for a MIPS ELF section. We do this by the
7010 section name, which is a hack, but ought to work. This routine is
7011 used by both the 32-bit and the 64-bit ABI. */
7012
b34976b6 7013bfd_boolean
9719ad41 7014_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 7015{
0414f35b 7016 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
7017
7018 if (strcmp (name, ".liblist") == 0)
7019 {
7020 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 7021 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
7022 /* The sh_link field is set in final_write_processing. */
7023 }
7024 else if (strcmp (name, ".conflict") == 0)
7025 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 7026 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
7027 {
7028 hdr->sh_type = SHT_MIPS_GPTAB;
7029 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7030 /* The sh_info field is set in final_write_processing. */
7031 }
7032 else if (strcmp (name, ".ucode") == 0)
7033 hdr->sh_type = SHT_MIPS_UCODE;
7034 else if (strcmp (name, ".mdebug") == 0)
7035 {
7036 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 7037 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
7038 entsize of 0. FIXME: Does this matter? */
7039 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7040 hdr->sh_entsize = 0;
7041 else
7042 hdr->sh_entsize = 1;
7043 }
7044 else if (strcmp (name, ".reginfo") == 0)
7045 {
7046 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 7047 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
7048 entsize of 0x18. FIXME: Does this matter? */
7049 if (SGI_COMPAT (abfd))
7050 {
7051 if ((abfd->flags & DYNAMIC) != 0)
7052 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7053 else
7054 hdr->sh_entsize = 1;
7055 }
7056 else
7057 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7058 }
7059 else if (SGI_COMPAT (abfd)
7060 && (strcmp (name, ".hash") == 0
7061 || strcmp (name, ".dynamic") == 0
7062 || strcmp (name, ".dynstr") == 0))
7063 {
7064 if (SGI_COMPAT (abfd))
7065 hdr->sh_entsize = 0;
7066#if 0
8dc1a139 7067 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
7068 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7069#endif
7070 }
7071 else if (strcmp (name, ".got") == 0
7072 || strcmp (name, ".srdata") == 0
7073 || strcmp (name, ".sdata") == 0
7074 || strcmp (name, ".sbss") == 0
7075 || strcmp (name, ".lit4") == 0
7076 || strcmp (name, ".lit8") == 0)
7077 hdr->sh_flags |= SHF_MIPS_GPREL;
7078 else if (strcmp (name, ".MIPS.interfaces") == 0)
7079 {
7080 hdr->sh_type = SHT_MIPS_IFACE;
7081 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7082 }
0112cd26 7083 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
7084 {
7085 hdr->sh_type = SHT_MIPS_CONTENT;
7086 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7087 /* The sh_info field is set in final_write_processing. */
7088 }
cc2e31b9 7089 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
7090 {
7091 hdr->sh_type = SHT_MIPS_OPTIONS;
7092 hdr->sh_entsize = 1;
7093 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7094 }
1b315056
CS
7095 else if (CONST_STRNEQ (name, ".debug_")
7096 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
7097 {
7098 hdr->sh_type = SHT_MIPS_DWARF;
7099
7100 /* Irix facilities such as libexc expect a single .debug_frame
7101 per executable, the system ones have NOSTRIP set and the linker
7102 doesn't merge sections with different flags so ... */
7103 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7104 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7105 }
b49e97c9
TS
7106 else if (strcmp (name, ".MIPS.symlib") == 0)
7107 {
7108 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7109 /* The sh_link and sh_info fields are set in
7110 final_write_processing. */
7111 }
0112cd26
NC
7112 else if (CONST_STRNEQ (name, ".MIPS.events")
7113 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
7114 {
7115 hdr->sh_type = SHT_MIPS_EVENTS;
7116 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7117 /* The sh_link field is set in final_write_processing. */
7118 }
7119 else if (strcmp (name, ".msym") == 0)
7120 {
7121 hdr->sh_type = SHT_MIPS_MSYM;
7122 hdr->sh_flags |= SHF_ALLOC;
7123 hdr->sh_entsize = 8;
7124 }
7125
7a79a000
TS
7126 /* The generic elf_fake_sections will set up REL_HDR using the default
7127 kind of relocations. We used to set up a second header for the
7128 non-default kind of relocations here, but only NewABI would use
7129 these, and the IRIX ld doesn't like resulting empty RELA sections.
7130 Thus we create those header only on demand now. */
b49e97c9 7131
b34976b6 7132 return TRUE;
b49e97c9
TS
7133}
7134
7135/* Given a BFD section, try to locate the corresponding ELF section
7136 index. This is used by both the 32-bit and the 64-bit ABI.
7137 Actually, it's not clear to me that the 64-bit ABI supports these,
7138 but for non-PIC objects we will certainly want support for at least
7139 the .scommon section. */
7140
b34976b6 7141bfd_boolean
9719ad41
RS
7142_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7143 asection *sec, int *retval)
b49e97c9
TS
7144{
7145 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7146 {
7147 *retval = SHN_MIPS_SCOMMON;
b34976b6 7148 return TRUE;
b49e97c9
TS
7149 }
7150 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7151 {
7152 *retval = SHN_MIPS_ACOMMON;
b34976b6 7153 return TRUE;
b49e97c9 7154 }
b34976b6 7155 return FALSE;
b49e97c9
TS
7156}
7157\f
7158/* Hook called by the linker routine which adds symbols from an object
7159 file. We must handle the special MIPS section numbers here. */
7160
b34976b6 7161bfd_boolean
9719ad41 7162_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 7163 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
7164 flagword *flagsp ATTRIBUTE_UNUSED,
7165 asection **secp, bfd_vma *valp)
b49e97c9
TS
7166{
7167 if (SGI_COMPAT (abfd)
7168 && (abfd->flags & DYNAMIC) != 0
7169 && strcmp (*namep, "_rld_new_interface") == 0)
7170 {
8dc1a139 7171 /* Skip IRIX5 rld entry name. */
b49e97c9 7172 *namep = NULL;
b34976b6 7173 return TRUE;
b49e97c9
TS
7174 }
7175
eedecc07
DD
7176 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7177 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7178 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7179 a magic symbol resolved by the linker, we ignore this bogus definition
7180 of _gp_disp. New ABI objects do not suffer from this problem so this
7181 is not done for them. */
7182 if (!NEWABI_P(abfd)
7183 && (sym->st_shndx == SHN_ABS)
7184 && (strcmp (*namep, "_gp_disp") == 0))
7185 {
7186 *namep = NULL;
7187 return TRUE;
7188 }
7189
b49e97c9
TS
7190 switch (sym->st_shndx)
7191 {
7192 case SHN_COMMON:
7193 /* Common symbols less than the GP size are automatically
7194 treated as SHN_MIPS_SCOMMON symbols. */
7195 if (sym->st_size > elf_gp_size (abfd)
b59eed79 7196 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
7197 || IRIX_COMPAT (abfd) == ict_irix6)
7198 break;
7199 /* Fall through. */
7200 case SHN_MIPS_SCOMMON:
7201 *secp = bfd_make_section_old_way (abfd, ".scommon");
7202 (*secp)->flags |= SEC_IS_COMMON;
7203 *valp = sym->st_size;
7204 break;
7205
7206 case SHN_MIPS_TEXT:
7207 /* This section is used in a shared object. */
698600e4 7208 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
b49e97c9
TS
7209 {
7210 asymbol *elf_text_symbol;
7211 asection *elf_text_section;
7212 bfd_size_type amt = sizeof (asection);
7213
7214 elf_text_section = bfd_zalloc (abfd, amt);
7215 if (elf_text_section == NULL)
b34976b6 7216 return FALSE;
b49e97c9
TS
7217
7218 amt = sizeof (asymbol);
7219 elf_text_symbol = bfd_zalloc (abfd, amt);
7220 if (elf_text_symbol == NULL)
b34976b6 7221 return FALSE;
b49e97c9
TS
7222
7223 /* Initialize the section. */
7224
698600e4
AM
7225 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7226 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
b49e97c9
TS
7227
7228 elf_text_section->symbol = elf_text_symbol;
698600e4 7229 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
b49e97c9
TS
7230
7231 elf_text_section->name = ".text";
7232 elf_text_section->flags = SEC_NO_FLAGS;
7233 elf_text_section->output_section = NULL;
7234 elf_text_section->owner = abfd;
7235 elf_text_symbol->name = ".text";
7236 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7237 elf_text_symbol->section = elf_text_section;
7238 }
7239 /* This code used to do *secp = bfd_und_section_ptr if
7240 info->shared. I don't know why, and that doesn't make sense,
7241 so I took it out. */
698600e4 7242 *secp = mips_elf_tdata (abfd)->elf_text_section;
b49e97c9
TS
7243 break;
7244
7245 case SHN_MIPS_ACOMMON:
7246 /* Fall through. XXX Can we treat this as allocated data? */
7247 case SHN_MIPS_DATA:
7248 /* This section is used in a shared object. */
698600e4 7249 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
b49e97c9
TS
7250 {
7251 asymbol *elf_data_symbol;
7252 asection *elf_data_section;
7253 bfd_size_type amt = sizeof (asection);
7254
7255 elf_data_section = bfd_zalloc (abfd, amt);
7256 if (elf_data_section == NULL)
b34976b6 7257 return FALSE;
b49e97c9
TS
7258
7259 amt = sizeof (asymbol);
7260 elf_data_symbol = bfd_zalloc (abfd, amt);
7261 if (elf_data_symbol == NULL)
b34976b6 7262 return FALSE;
b49e97c9
TS
7263
7264 /* Initialize the section. */
7265
698600e4
AM
7266 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7267 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
b49e97c9
TS
7268
7269 elf_data_section->symbol = elf_data_symbol;
698600e4 7270 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
b49e97c9
TS
7271
7272 elf_data_section->name = ".data";
7273 elf_data_section->flags = SEC_NO_FLAGS;
7274 elf_data_section->output_section = NULL;
7275 elf_data_section->owner = abfd;
7276 elf_data_symbol->name = ".data";
7277 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7278 elf_data_symbol->section = elf_data_section;
7279 }
7280 /* This code used to do *secp = bfd_und_section_ptr if
7281 info->shared. I don't know why, and that doesn't make sense,
7282 so I took it out. */
698600e4 7283 *secp = mips_elf_tdata (abfd)->elf_data_section;
b49e97c9
TS
7284 break;
7285
7286 case SHN_MIPS_SUNDEFINED:
7287 *secp = bfd_und_section_ptr;
7288 break;
7289 }
7290
7291 if (SGI_COMPAT (abfd)
7292 && ! info->shared
f13a99db 7293 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
7294 && strcmp (*namep, "__rld_obj_head") == 0)
7295 {
7296 struct elf_link_hash_entry *h;
14a793b2 7297 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7298
7299 /* Mark __rld_obj_head as dynamic. */
14a793b2 7300 bh = NULL;
b49e97c9 7301 if (! (_bfd_generic_link_add_one_symbol
9719ad41 7302 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 7303 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7304 return FALSE;
14a793b2
AM
7305
7306 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7307 h->non_elf = 0;
7308 h->def_regular = 1;
b49e97c9
TS
7309 h->type = STT_OBJECT;
7310
c152c796 7311 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7312 return FALSE;
b49e97c9 7313
b34976b6 7314 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b4082c70 7315 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7316 }
7317
7318 /* If this is a mips16 text symbol, add 1 to the value to make it
7319 odd. This will cause something like .word SYM to come up with
7320 the right value when it is loaded into the PC. */
df58fc94 7321 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
7322 ++*valp;
7323
b34976b6 7324 return TRUE;
b49e97c9
TS
7325}
7326
7327/* This hook function is called before the linker writes out a global
7328 symbol. We mark symbols as small common if appropriate. This is
7329 also where we undo the increment of the value for a mips16 symbol. */
7330
6e0b88f1 7331int
9719ad41
RS
7332_bfd_mips_elf_link_output_symbol_hook
7333 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7334 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7335 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
7336{
7337 /* If we see a common symbol, which implies a relocatable link, then
7338 if a symbol was small common in an input file, mark it as small
7339 common in the output file. */
7340 if (sym->st_shndx == SHN_COMMON
7341 && strcmp (input_sec->name, ".scommon") == 0)
7342 sym->st_shndx = SHN_MIPS_SCOMMON;
7343
df58fc94 7344 if (ELF_ST_IS_COMPRESSED (sym->st_other))
79cda7cf 7345 sym->st_value &= ~1;
b49e97c9 7346
6e0b88f1 7347 return 1;
b49e97c9
TS
7348}
7349\f
7350/* Functions for the dynamic linker. */
7351
7352/* Create dynamic sections when linking against a dynamic object. */
7353
b34976b6 7354bfd_boolean
9719ad41 7355_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
7356{
7357 struct elf_link_hash_entry *h;
14a793b2 7358 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7359 flagword flags;
7360 register asection *s;
7361 const char * const *namep;
0a44bf69 7362 struct mips_elf_link_hash_table *htab;
b49e97c9 7363
0a44bf69 7364 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7365 BFD_ASSERT (htab != NULL);
7366
b49e97c9
TS
7367 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7368 | SEC_LINKER_CREATED | SEC_READONLY);
7369
0a44bf69
RS
7370 /* The psABI requires a read-only .dynamic section, but the VxWorks
7371 EABI doesn't. */
7372 if (!htab->is_vxworks)
b49e97c9 7373 {
3d4d4302 7374 s = bfd_get_linker_section (abfd, ".dynamic");
0a44bf69
RS
7375 if (s != NULL)
7376 {
7377 if (! bfd_set_section_flags (abfd, s, flags))
7378 return FALSE;
7379 }
b49e97c9
TS
7380 }
7381
7382 /* We need to create .got section. */
23cc69b6 7383 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
7384 return FALSE;
7385
0a44bf69 7386 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 7387 return FALSE;
b49e97c9 7388
b49e97c9 7389 /* Create .stub section. */
3d4d4302
AM
7390 s = bfd_make_section_anyway_with_flags (abfd,
7391 MIPS_ELF_STUB_SECTION_NAME (abfd),
7392 flags | SEC_CODE);
4e41d0d7
RS
7393 if (s == NULL
7394 || ! bfd_set_section_alignment (abfd, s,
7395 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7396 return FALSE;
7397 htab->sstubs = s;
b49e97c9 7398
e6aea42d 7399 if (!mips_elf_hash_table (info)->use_rld_obj_head
b49e97c9 7400 && !info->shared
3d4d4302 7401 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
b49e97c9 7402 {
3d4d4302
AM
7403 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7404 flags &~ (flagword) SEC_READONLY);
b49e97c9 7405 if (s == NULL
b49e97c9
TS
7406 || ! bfd_set_section_alignment (abfd, s,
7407 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 7408 return FALSE;
b49e97c9
TS
7409 }
7410
7411 /* On IRIX5, we adjust add some additional symbols and change the
7412 alignments of several sections. There is no ABI documentation
7413 indicating that this is necessary on IRIX6, nor any evidence that
7414 the linker takes such action. */
7415 if (IRIX_COMPAT (abfd) == ict_irix5)
7416 {
7417 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7418 {
14a793b2 7419 bh = NULL;
b49e97c9 7420 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
7421 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7422 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7423 return FALSE;
14a793b2
AM
7424
7425 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7426 h->non_elf = 0;
7427 h->def_regular = 1;
b49e97c9
TS
7428 h->type = STT_SECTION;
7429
c152c796 7430 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7431 return FALSE;
b49e97c9
TS
7432 }
7433
7434 /* We need to create a .compact_rel section. */
7435 if (SGI_COMPAT (abfd))
7436 {
7437 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 7438 return FALSE;
b49e97c9
TS
7439 }
7440
44c410de 7441 /* Change alignments of some sections. */
3d4d4302 7442 s = bfd_get_linker_section (abfd, ".hash");
b49e97c9 7443 if (s != NULL)
a253d456
NC
7444 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7445
3d4d4302 7446 s = bfd_get_linker_section (abfd, ".dynsym");
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, ".dynstr");
b49e97c9 7451 if (s != NULL)
a253d456
NC
7452 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7453
3d4d4302 7454 /* ??? */
b49e97c9
TS
7455 s = bfd_get_section_by_name (abfd, ".reginfo");
7456 if (s != NULL)
a253d456
NC
7457 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7458
3d4d4302 7459 s = bfd_get_linker_section (abfd, ".dynamic");
b49e97c9 7460 if (s != NULL)
a253d456 7461 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7462 }
7463
7464 if (!info->shared)
7465 {
14a793b2
AM
7466 const char *name;
7467
7468 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7469 bh = NULL;
7470 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
7471 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7472 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7473 return FALSE;
14a793b2
AM
7474
7475 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7476 h->non_elf = 0;
7477 h->def_regular = 1;
b49e97c9
TS
7478 h->type = STT_SECTION;
7479
c152c796 7480 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7481 return FALSE;
b49e97c9
TS
7482
7483 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7484 {
7485 /* __rld_map is a four byte word located in the .data section
7486 and is filled in by the rtld to contain a pointer to
7487 the _r_debug structure. Its symbol value will be set in
7488 _bfd_mips_elf_finish_dynamic_symbol. */
3d4d4302 7489 s = bfd_get_linker_section (abfd, ".rld_map");
0abfb97a 7490 BFD_ASSERT (s != NULL);
14a793b2 7491
0abfb97a
L
7492 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7493 bh = NULL;
7494 if (!(_bfd_generic_link_add_one_symbol
7495 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7496 get_elf_backend_data (abfd)->collect, &bh)))
7497 return FALSE;
b49e97c9 7498
0abfb97a
L
7499 h = (struct elf_link_hash_entry *) bh;
7500 h->non_elf = 0;
7501 h->def_regular = 1;
7502 h->type = STT_OBJECT;
7503
7504 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7505 return FALSE;
b4082c70 7506 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7507 }
7508 }
7509
861fb55a 7510 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
c164a95d 7511 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
861fb55a
DJ
7512 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7513 return FALSE;
7514
7515 /* Cache the sections created above. */
3d4d4302
AM
7516 htab->splt = bfd_get_linker_section (abfd, ".plt");
7517 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
0a44bf69
RS
7518 if (htab->is_vxworks)
7519 {
3d4d4302
AM
7520 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7521 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
861fb55a
DJ
7522 }
7523 else
3d4d4302 7524 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
861fb55a
DJ
7525 if (!htab->sdynbss
7526 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7527 || !htab->srelplt
7528 || !htab->splt)
7529 abort ();
0a44bf69 7530
1bbce132
MR
7531 /* Do the usual VxWorks handling. */
7532 if (htab->is_vxworks
7533 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7534 return FALSE;
0a44bf69 7535
b34976b6 7536 return TRUE;
b49e97c9
TS
7537}
7538\f
c224138d
RS
7539/* Return true if relocation REL against section SEC is a REL rather than
7540 RELA relocation. RELOCS is the first relocation in the section and
7541 ABFD is the bfd that contains SEC. */
7542
7543static bfd_boolean
7544mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7545 const Elf_Internal_Rela *relocs,
7546 const Elf_Internal_Rela *rel)
7547{
7548 Elf_Internal_Shdr *rel_hdr;
7549 const struct elf_backend_data *bed;
7550
d4730f92
BS
7551 /* To determine which flavor of relocation this is, we depend on the
7552 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7553 rel_hdr = elf_section_data (sec)->rel.hdr;
7554 if (rel_hdr == NULL)
7555 return FALSE;
c224138d 7556 bed = get_elf_backend_data (abfd);
d4730f92
BS
7557 return ((size_t) (rel - relocs)
7558 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
c224138d
RS
7559}
7560
7561/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7562 HOWTO is the relocation's howto and CONTENTS points to the contents
7563 of the section that REL is against. */
7564
7565static bfd_vma
7566mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7567 reloc_howto_type *howto, bfd_byte *contents)
7568{
7569 bfd_byte *location;
7570 unsigned int r_type;
7571 bfd_vma addend;
7572
7573 r_type = ELF_R_TYPE (abfd, rel->r_info);
7574 location = contents + rel->r_offset;
7575
7576 /* Get the addend, which is stored in the input file. */
df58fc94 7577 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
c224138d 7578 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
df58fc94 7579 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
c224138d
RS
7580
7581 return addend & howto->src_mask;
7582}
7583
7584/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7585 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7586 and update *ADDEND with the final addend. Return true on success
7587 or false if the LO16 could not be found. RELEND is the exclusive
7588 upper bound on the relocations for REL's section. */
7589
7590static bfd_boolean
7591mips_elf_add_lo16_rel_addend (bfd *abfd,
7592 const Elf_Internal_Rela *rel,
7593 const Elf_Internal_Rela *relend,
7594 bfd_byte *contents, bfd_vma *addend)
7595{
7596 unsigned int r_type, lo16_type;
7597 const Elf_Internal_Rela *lo16_relocation;
7598 reloc_howto_type *lo16_howto;
7599 bfd_vma l;
7600
7601 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7602 if (mips16_reloc_p (r_type))
c224138d 7603 lo16_type = R_MIPS16_LO16;
df58fc94
RS
7604 else if (micromips_reloc_p (r_type))
7605 lo16_type = R_MICROMIPS_LO16;
c224138d
RS
7606 else
7607 lo16_type = R_MIPS_LO16;
7608
7609 /* The combined value is the sum of the HI16 addend, left-shifted by
7610 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7611 code does a `lui' of the HI16 value, and then an `addiu' of the
7612 LO16 value.)
7613
7614 Scan ahead to find a matching LO16 relocation.
7615
7616 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7617 be immediately following. However, for the IRIX6 ABI, the next
7618 relocation may be a composed relocation consisting of several
7619 relocations for the same address. In that case, the R_MIPS_LO16
7620 relocation may occur as one of these. We permit a similar
7621 extension in general, as that is useful for GCC.
7622
7623 In some cases GCC dead code elimination removes the LO16 but keeps
7624 the corresponding HI16. This is strictly speaking a violation of
7625 the ABI but not immediately harmful. */
7626 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7627 if (lo16_relocation == NULL)
7628 return FALSE;
7629
7630 /* Obtain the addend kept there. */
7631 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7632 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7633
7634 l <<= lo16_howto->rightshift;
7635 l = _bfd_mips_elf_sign_extend (l, 16);
7636
7637 *addend <<= 16;
7638 *addend += l;
7639 return TRUE;
7640}
7641
7642/* Try to read the contents of section SEC in bfd ABFD. Return true and
7643 store the contents in *CONTENTS on success. Assume that *CONTENTS
7644 already holds the contents if it is nonull on entry. */
7645
7646static bfd_boolean
7647mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7648{
7649 if (*contents)
7650 return TRUE;
7651
7652 /* Get cached copy if it exists. */
7653 if (elf_section_data (sec)->this_hdr.contents != NULL)
7654 {
7655 *contents = elf_section_data (sec)->this_hdr.contents;
7656 return TRUE;
7657 }
7658
7659 return bfd_malloc_and_get_section (abfd, sec, contents);
7660}
7661
1bbce132
MR
7662/* Make a new PLT record to keep internal data. */
7663
7664static struct plt_entry *
7665mips_elf_make_plt_record (bfd *abfd)
7666{
7667 struct plt_entry *entry;
7668
7669 entry = bfd_zalloc (abfd, sizeof (*entry));
7670 if (entry == NULL)
7671 return NULL;
7672
7673 entry->stub_offset = MINUS_ONE;
7674 entry->mips_offset = MINUS_ONE;
7675 entry->comp_offset = MINUS_ONE;
7676 entry->gotplt_index = MINUS_ONE;
7677 return entry;
7678}
7679
b49e97c9 7680/* Look through the relocs for a section during the first phase, and
1bbce132
MR
7681 allocate space in the global offset table and record the need for
7682 standard MIPS and compressed procedure linkage table entries. */
b49e97c9 7683
b34976b6 7684bfd_boolean
9719ad41
RS
7685_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7686 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7687{
7688 const char *name;
7689 bfd *dynobj;
7690 Elf_Internal_Shdr *symtab_hdr;
7691 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7692 size_t extsymoff;
7693 const Elf_Internal_Rela *rel;
7694 const Elf_Internal_Rela *rel_end;
b49e97c9 7695 asection *sreloc;
9c5bfbb7 7696 const struct elf_backend_data *bed;
0a44bf69 7697 struct mips_elf_link_hash_table *htab;
c224138d
RS
7698 bfd_byte *contents;
7699 bfd_vma addend;
7700 reloc_howto_type *howto;
b49e97c9 7701
1049f94e 7702 if (info->relocatable)
b34976b6 7703 return TRUE;
b49e97c9 7704
0a44bf69 7705 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7706 BFD_ASSERT (htab != NULL);
7707
b49e97c9
TS
7708 dynobj = elf_hash_table (info)->dynobj;
7709 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7710 sym_hashes = elf_sym_hashes (abfd);
7711 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7712
738e5348
RS
7713 bed = get_elf_backend_data (abfd);
7714 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7715
b49e97c9
TS
7716 /* Check for the mips16 stub sections. */
7717
7718 name = bfd_get_section_name (abfd, sec);
b9d58d71 7719 if (FN_STUB_P (name))
b49e97c9
TS
7720 {
7721 unsigned long r_symndx;
7722
7723 /* Look at the relocation information to figure out which symbol
7724 this is for. */
7725
cb4437b8 7726 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
7727 if (r_symndx == 0)
7728 {
7729 (*_bfd_error_handler)
7730 (_("%B: Warning: cannot determine the target function for"
7731 " stub section `%s'"),
7732 abfd, name);
7733 bfd_set_error (bfd_error_bad_value);
7734 return FALSE;
7735 }
b49e97c9
TS
7736
7737 if (r_symndx < extsymoff
7738 || sym_hashes[r_symndx - extsymoff] == NULL)
7739 {
7740 asection *o;
7741
7742 /* This stub is for a local symbol. This stub will only be
7743 needed if there is some relocation in this BFD, other
7744 than a 16 bit function call, which refers to this symbol. */
7745 for (o = abfd->sections; o != NULL; o = o->next)
7746 {
7747 Elf_Internal_Rela *sec_relocs;
7748 const Elf_Internal_Rela *r, *rend;
7749
7750 /* We can ignore stub sections when looking for relocs. */
7751 if ((o->flags & SEC_RELOC) == 0
7752 || o->reloc_count == 0
738e5348 7753 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7754 continue;
7755
45d6a902 7756 sec_relocs
9719ad41 7757 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7758 info->keep_memory);
b49e97c9 7759 if (sec_relocs == NULL)
b34976b6 7760 return FALSE;
b49e97c9
TS
7761
7762 rend = sec_relocs + o->reloc_count;
7763 for (r = sec_relocs; r < rend; r++)
7764 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7765 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7766 break;
7767
6cdc0ccc 7768 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7769 free (sec_relocs);
7770
7771 if (r < rend)
7772 break;
7773 }
7774
7775 if (o == NULL)
7776 {
7777 /* There is no non-call reloc for this stub, so we do
7778 not need it. Since this function is called before
7779 the linker maps input sections to output sections, we
7780 can easily discard it by setting the SEC_EXCLUDE
7781 flag. */
7782 sec->flags |= SEC_EXCLUDE;
b34976b6 7783 return TRUE;
b49e97c9
TS
7784 }
7785
7786 /* Record this stub in an array of local symbol stubs for
7787 this BFD. */
698600e4 7788 if (mips_elf_tdata (abfd)->local_stubs == NULL)
b49e97c9
TS
7789 {
7790 unsigned long symcount;
7791 asection **n;
7792 bfd_size_type amt;
7793
7794 if (elf_bad_symtab (abfd))
7795 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7796 else
7797 symcount = symtab_hdr->sh_info;
7798 amt = symcount * sizeof (asection *);
9719ad41 7799 n = bfd_zalloc (abfd, amt);
b49e97c9 7800 if (n == NULL)
b34976b6 7801 return FALSE;
698600e4 7802 mips_elf_tdata (abfd)->local_stubs = n;
b49e97c9
TS
7803 }
7804
b9d58d71 7805 sec->flags |= SEC_KEEP;
698600e4 7806 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
b49e97c9
TS
7807
7808 /* We don't need to set mips16_stubs_seen in this case.
7809 That flag is used to see whether we need to look through
7810 the global symbol table for stubs. We don't need to set
7811 it here, because we just have a local stub. */
7812 }
7813 else
7814 {
7815 struct mips_elf_link_hash_entry *h;
7816
7817 h = ((struct mips_elf_link_hash_entry *)
7818 sym_hashes[r_symndx - extsymoff]);
7819
973a3492
L
7820 while (h->root.root.type == bfd_link_hash_indirect
7821 || h->root.root.type == bfd_link_hash_warning)
7822 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7823
b49e97c9
TS
7824 /* H is the symbol this stub is for. */
7825
b9d58d71
TS
7826 /* If we already have an appropriate stub for this function, we
7827 don't need another one, so we can discard this one. Since
7828 this function is called before the linker maps input sections
7829 to output sections, we can easily discard it by setting the
7830 SEC_EXCLUDE flag. */
7831 if (h->fn_stub != NULL)
7832 {
7833 sec->flags |= SEC_EXCLUDE;
7834 return TRUE;
7835 }
7836
7837 sec->flags |= SEC_KEEP;
b49e97c9 7838 h->fn_stub = sec;
b34976b6 7839 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7840 }
7841 }
b9d58d71 7842 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7843 {
7844 unsigned long r_symndx;
7845 struct mips_elf_link_hash_entry *h;
7846 asection **loc;
7847
7848 /* Look at the relocation information to figure out which symbol
7849 this is for. */
7850
cb4437b8 7851 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
7852 if (r_symndx == 0)
7853 {
7854 (*_bfd_error_handler)
7855 (_("%B: Warning: cannot determine the target function for"
7856 " stub section `%s'"),
7857 abfd, name);
7858 bfd_set_error (bfd_error_bad_value);
7859 return FALSE;
7860 }
b49e97c9
TS
7861
7862 if (r_symndx < extsymoff
7863 || sym_hashes[r_symndx - extsymoff] == NULL)
7864 {
b9d58d71 7865 asection *o;
b49e97c9 7866
b9d58d71
TS
7867 /* This stub is for a local symbol. This stub will only be
7868 needed if there is some relocation (R_MIPS16_26) in this BFD
7869 that refers to this symbol. */
7870 for (o = abfd->sections; o != NULL; o = o->next)
7871 {
7872 Elf_Internal_Rela *sec_relocs;
7873 const Elf_Internal_Rela *r, *rend;
7874
7875 /* We can ignore stub sections when looking for relocs. */
7876 if ((o->flags & SEC_RELOC) == 0
7877 || o->reloc_count == 0
738e5348 7878 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7879 continue;
7880
7881 sec_relocs
7882 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7883 info->keep_memory);
7884 if (sec_relocs == NULL)
7885 return FALSE;
7886
7887 rend = sec_relocs + o->reloc_count;
7888 for (r = sec_relocs; r < rend; r++)
7889 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7890 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7891 break;
7892
7893 if (elf_section_data (o)->relocs != sec_relocs)
7894 free (sec_relocs);
7895
7896 if (r < rend)
7897 break;
7898 }
7899
7900 if (o == NULL)
7901 {
7902 /* There is no non-call reloc for this stub, so we do
7903 not need it. Since this function is called before
7904 the linker maps input sections to output sections, we
7905 can easily discard it by setting the SEC_EXCLUDE
7906 flag. */
7907 sec->flags |= SEC_EXCLUDE;
7908 return TRUE;
7909 }
7910
7911 /* Record this stub in an array of local symbol call_stubs for
7912 this BFD. */
698600e4 7913 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
b9d58d71
TS
7914 {
7915 unsigned long symcount;
7916 asection **n;
7917 bfd_size_type amt;
7918
7919 if (elf_bad_symtab (abfd))
7920 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7921 else
7922 symcount = symtab_hdr->sh_info;
7923 amt = symcount * sizeof (asection *);
7924 n = bfd_zalloc (abfd, amt);
7925 if (n == NULL)
7926 return FALSE;
698600e4 7927 mips_elf_tdata (abfd)->local_call_stubs = n;
b9d58d71 7928 }
b49e97c9 7929
b9d58d71 7930 sec->flags |= SEC_KEEP;
698600e4 7931 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7932
b9d58d71
TS
7933 /* We don't need to set mips16_stubs_seen in this case.
7934 That flag is used to see whether we need to look through
7935 the global symbol table for stubs. We don't need to set
7936 it here, because we just have a local stub. */
7937 }
b49e97c9 7938 else
b49e97c9 7939 {
b9d58d71
TS
7940 h = ((struct mips_elf_link_hash_entry *)
7941 sym_hashes[r_symndx - extsymoff]);
68ffbac6 7942
b9d58d71 7943 /* H is the symbol this stub is for. */
68ffbac6 7944
b9d58d71
TS
7945 if (CALL_FP_STUB_P (name))
7946 loc = &h->call_fp_stub;
7947 else
7948 loc = &h->call_stub;
68ffbac6 7949
b9d58d71
TS
7950 /* If we already have an appropriate stub for this function, we
7951 don't need another one, so we can discard this one. Since
7952 this function is called before the linker maps input sections
7953 to output sections, we can easily discard it by setting the
7954 SEC_EXCLUDE flag. */
7955 if (*loc != NULL)
7956 {
7957 sec->flags |= SEC_EXCLUDE;
7958 return TRUE;
7959 }
b49e97c9 7960
b9d58d71
TS
7961 sec->flags |= SEC_KEEP;
7962 *loc = sec;
7963 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7964 }
b49e97c9
TS
7965 }
7966
b49e97c9 7967 sreloc = NULL;
c224138d 7968 contents = NULL;
b49e97c9
TS
7969 for (rel = relocs; rel < rel_end; ++rel)
7970 {
7971 unsigned long r_symndx;
7972 unsigned int r_type;
7973 struct elf_link_hash_entry *h;
861fb55a 7974 bfd_boolean can_make_dynamic_p;
c5d6fa44
RS
7975 bfd_boolean call_reloc_p;
7976 bfd_boolean constrain_symbol_p;
b49e97c9
TS
7977
7978 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7979 r_type = ELF_R_TYPE (abfd, rel->r_info);
7980
7981 if (r_symndx < extsymoff)
7982 h = NULL;
7983 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7984 {
7985 (*_bfd_error_handler)
d003868e
AM
7986 (_("%B: Malformed reloc detected for section %s"),
7987 abfd, name);
b49e97c9 7988 bfd_set_error (bfd_error_bad_value);
b34976b6 7989 return FALSE;
b49e97c9
TS
7990 }
7991 else
7992 {
7993 h = sym_hashes[r_symndx - extsymoff];
81fbe831
AM
7994 if (h != NULL)
7995 {
7996 while (h->root.type == bfd_link_hash_indirect
7997 || h->root.type == bfd_link_hash_warning)
7998 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7999
8000 /* PR15323, ref flags aren't set for references in the
8001 same object. */
8002 h->root.non_ir_ref = 1;
8003 }
861fb55a 8004 }
b49e97c9 8005
861fb55a
DJ
8006 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8007 relocation into a dynamic one. */
8008 can_make_dynamic_p = FALSE;
c5d6fa44
RS
8009
8010 /* Set CALL_RELOC_P to true if the relocation is for a call,
8011 and if pointer equality therefore doesn't matter. */
8012 call_reloc_p = FALSE;
8013
8014 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8015 into account when deciding how to define the symbol.
8016 Relocations in nonallocatable sections such as .pdr and
8017 .debug* should have no effect. */
8018 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8019
861fb55a
DJ
8020 switch (r_type)
8021 {
861fb55a
DJ
8022 case R_MIPS_CALL16:
8023 case R_MIPS_CALL_HI16:
8024 case R_MIPS_CALL_LO16:
c5d6fa44
RS
8025 case R_MIPS16_CALL16:
8026 case R_MICROMIPS_CALL16:
8027 case R_MICROMIPS_CALL_HI16:
8028 case R_MICROMIPS_CALL_LO16:
8029 call_reloc_p = TRUE;
8030 /* Fall through. */
8031
8032 case R_MIPS_GOT16:
861fb55a
DJ
8033 case R_MIPS_GOT_HI16:
8034 case R_MIPS_GOT_LO16:
8035 case R_MIPS_GOT_PAGE:
8036 case R_MIPS_GOT_OFST:
8037 case R_MIPS_GOT_DISP:
8038 case R_MIPS_TLS_GOTTPREL:
8039 case R_MIPS_TLS_GD:
8040 case R_MIPS_TLS_LDM:
d0f13682 8041 case R_MIPS16_GOT16:
d0f13682
CLT
8042 case R_MIPS16_TLS_GOTTPREL:
8043 case R_MIPS16_TLS_GD:
8044 case R_MIPS16_TLS_LDM:
df58fc94 8045 case R_MICROMIPS_GOT16:
df58fc94
RS
8046 case R_MICROMIPS_GOT_HI16:
8047 case R_MICROMIPS_GOT_LO16:
8048 case R_MICROMIPS_GOT_PAGE:
8049 case R_MICROMIPS_GOT_OFST:
8050 case R_MICROMIPS_GOT_DISP:
8051 case R_MICROMIPS_TLS_GOTTPREL:
8052 case R_MICROMIPS_TLS_GD:
8053 case R_MICROMIPS_TLS_LDM:
861fb55a
DJ
8054 if (dynobj == NULL)
8055 elf_hash_table (info)->dynobj = dynobj = abfd;
8056 if (!mips_elf_create_got_section (dynobj, info))
8057 return FALSE;
8058 if (htab->is_vxworks && !info->shared)
b49e97c9 8059 {
861fb55a
DJ
8060 (*_bfd_error_handler)
8061 (_("%B: GOT reloc at 0x%lx not expected in executables"),
8062 abfd, (unsigned long) rel->r_offset);
8063 bfd_set_error (bfd_error_bad_value);
8064 return FALSE;
b49e97c9 8065 }
c5d6fa44 8066 can_make_dynamic_p = TRUE;
861fb55a 8067 break;
b49e97c9 8068
c5d6fa44 8069 case R_MIPS_NONE:
99da6b5f 8070 case R_MIPS_JALR:
df58fc94 8071 case R_MICROMIPS_JALR:
c5d6fa44
RS
8072 /* These relocations have empty fields and are purely there to
8073 provide link information. The symbol value doesn't matter. */
8074 constrain_symbol_p = FALSE;
8075 break;
8076
8077 case R_MIPS_GPREL16:
8078 case R_MIPS_GPREL32:
8079 case R_MIPS16_GPREL:
8080 case R_MICROMIPS_GPREL16:
8081 /* GP-relative relocations always resolve to a definition in a
8082 regular input file, ignoring the one-definition rule. This is
8083 important for the GP setup sequence in NewABI code, which
8084 always resolves to a local function even if other relocations
8085 against the symbol wouldn't. */
8086 constrain_symbol_p = FALSE;
99da6b5f
AN
8087 break;
8088
861fb55a
DJ
8089 case R_MIPS_32:
8090 case R_MIPS_REL32:
8091 case R_MIPS_64:
8092 /* In VxWorks executables, references to external symbols
8093 must be handled using copy relocs or PLT entries; it is not
8094 possible to convert this relocation into a dynamic one.
8095
8096 For executables that use PLTs and copy-relocs, we have a
8097 choice between converting the relocation into a dynamic
8098 one or using copy relocations or PLT entries. It is
8099 usually better to do the former, unless the relocation is
8100 against a read-only section. */
8101 if ((info->shared
8102 || (h != NULL
8103 && !htab->is_vxworks
8104 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8105 && !(!info->nocopyreloc
8106 && !PIC_OBJECT_P (abfd)
8107 && MIPS_ELF_READONLY_SECTION (sec))))
8108 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 8109 {
861fb55a 8110 can_make_dynamic_p = TRUE;
b49e97c9
TS
8111 if (dynobj == NULL)
8112 elf_hash_table (info)->dynobj = dynobj = abfd;
861fb55a 8113 }
c5d6fa44 8114 break;
b49e97c9 8115
861fb55a
DJ
8116 case R_MIPS_26:
8117 case R_MIPS_PC16:
8118 case R_MIPS16_26:
df58fc94
RS
8119 case R_MICROMIPS_26_S1:
8120 case R_MICROMIPS_PC7_S1:
8121 case R_MICROMIPS_PC10_S1:
8122 case R_MICROMIPS_PC16_S1:
8123 case R_MICROMIPS_PC23_S2:
c5d6fa44 8124 call_reloc_p = TRUE;
861fb55a 8125 break;
b49e97c9
TS
8126 }
8127
0a44bf69
RS
8128 if (h)
8129 {
c5d6fa44
RS
8130 if (constrain_symbol_p)
8131 {
8132 if (!can_make_dynamic_p)
8133 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8134
8135 if (!call_reloc_p)
8136 h->pointer_equality_needed = 1;
8137
8138 /* We must not create a stub for a symbol that has
8139 relocations related to taking the function's address.
8140 This doesn't apply to VxWorks, where CALL relocs refer
8141 to a .got.plt entry instead of a normal .got entry. */
8142 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8143 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8144 }
8145
0a44bf69
RS
8146 /* Relocations against the special VxWorks __GOTT_BASE__ and
8147 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8148 room for them in .rela.dyn. */
8149 if (is_gott_symbol (info, h))
8150 {
8151 if (sreloc == NULL)
8152 {
8153 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8154 if (sreloc == NULL)
8155 return FALSE;
8156 }
8157 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
8158 if (MIPS_ELF_READONLY_SECTION (sec))
8159 /* We tell the dynamic linker that there are
8160 relocations against the text segment. */
8161 info->flags |= DF_TEXTREL;
0a44bf69
RS
8162 }
8163 }
df58fc94
RS
8164 else if (call_lo16_reloc_p (r_type)
8165 || got_lo16_reloc_p (r_type)
8166 || got_disp_reloc_p (r_type)
738e5348 8167 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
8168 {
8169 /* We may need a local GOT entry for this relocation. We
8170 don't count R_MIPS_GOT_PAGE because we can estimate the
8171 maximum number of pages needed by looking at the size of
738e5348
RS
8172 the segment. Similar comments apply to R_MIPS*_GOT16 and
8173 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 8174 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 8175 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 8176 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0 8177 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
e641e783 8178 rel->r_addend, info, r_type))
f4416af6 8179 return FALSE;
b49e97c9
TS
8180 }
8181
8f0c309a
CLT
8182 if (h != NULL
8183 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8184 ELF_ST_IS_MIPS16 (h->other)))
861fb55a
DJ
8185 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8186
b49e97c9
TS
8187 switch (r_type)
8188 {
8189 case R_MIPS_CALL16:
738e5348 8190 case R_MIPS16_CALL16:
df58fc94 8191 case R_MICROMIPS_CALL16:
b49e97c9
TS
8192 if (h == NULL)
8193 {
8194 (*_bfd_error_handler)
d003868e
AM
8195 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
8196 abfd, (unsigned long) rel->r_offset);
b49e97c9 8197 bfd_set_error (bfd_error_bad_value);
b34976b6 8198 return FALSE;
b49e97c9
TS
8199 }
8200 /* Fall through. */
8201
8202 case R_MIPS_CALL_HI16:
8203 case R_MIPS_CALL_LO16:
df58fc94
RS
8204 case R_MICROMIPS_CALL_HI16:
8205 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
8206 if (h != NULL)
8207 {
6ccf4795
RS
8208 /* Make sure there is room in the regular GOT to hold the
8209 function's address. We may eliminate it in favour of
8210 a .got.plt entry later; see mips_elf_count_got_symbols. */
e641e783
RS
8211 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8212 r_type))
b34976b6 8213 return FALSE;
b49e97c9
TS
8214
8215 /* We need a stub, not a plt entry for the undefined
8216 function. But we record it as if it needs plt. See
c152c796 8217 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 8218 h->needs_plt = 1;
b49e97c9
TS
8219 h->type = STT_FUNC;
8220 }
8221 break;
8222
0fdc1bf1 8223 case R_MIPS_GOT_PAGE:
df58fc94 8224 case R_MICROMIPS_GOT_PAGE:
738e5348 8225 case R_MIPS16_GOT16:
b49e97c9
TS
8226 case R_MIPS_GOT16:
8227 case R_MIPS_GOT_HI16:
8228 case R_MIPS_GOT_LO16:
df58fc94
RS
8229 case R_MICROMIPS_GOT16:
8230 case R_MICROMIPS_GOT_HI16:
8231 case R_MICROMIPS_GOT_LO16:
8232 if (!h || got_page_reloc_p (r_type))
c224138d 8233 {
3a3b6725
DJ
8234 /* This relocation needs (or may need, if h != NULL) a
8235 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8236 know for sure until we know whether the symbol is
8237 preemptible. */
c224138d
RS
8238 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8239 {
8240 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8241 return FALSE;
8242 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8243 addend = mips_elf_read_rel_addend (abfd, rel,
8244 howto, contents);
9684f078 8245 if (got16_reloc_p (r_type))
c224138d
RS
8246 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8247 contents, &addend);
8248 else
8249 addend <<= howto->rightshift;
8250 }
8251 else
8252 addend = rel->r_addend;
13db6b44
RS
8253 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8254 h, addend))
c224138d 8255 return FALSE;
13db6b44
RS
8256
8257 if (h)
8258 {
8259 struct mips_elf_link_hash_entry *hmips =
8260 (struct mips_elf_link_hash_entry *) h;
8261
8262 /* This symbol is definitely not overridable. */
8263 if (hmips->root.def_regular
8264 && ! (info->shared && ! info->symbolic
8265 && ! hmips->root.forced_local))
8266 h = NULL;
8267 }
c224138d 8268 }
13db6b44
RS
8269 /* If this is a global, overridable symbol, GOT_PAGE will
8270 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d
RS
8271 /* Fall through. */
8272
b49e97c9 8273 case R_MIPS_GOT_DISP:
df58fc94 8274 case R_MICROMIPS_GOT_DISP:
6ccf4795 8275 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
e641e783 8276 FALSE, r_type))
b34976b6 8277 return FALSE;
b49e97c9
TS
8278 break;
8279
0f20cc35 8280 case R_MIPS_TLS_GOTTPREL:
d0f13682 8281 case R_MIPS16_TLS_GOTTPREL:
df58fc94 8282 case R_MICROMIPS_TLS_GOTTPREL:
0f20cc35
DJ
8283 if (info->shared)
8284 info->flags |= DF_STATIC_TLS;
8285 /* Fall through */
8286
8287 case R_MIPS_TLS_LDM:
d0f13682 8288 case R_MIPS16_TLS_LDM:
df58fc94
RS
8289 case R_MICROMIPS_TLS_LDM:
8290 if (tls_ldm_reloc_p (r_type))
0f20cc35 8291 {
cf35638d 8292 r_symndx = STN_UNDEF;
0f20cc35
DJ
8293 h = NULL;
8294 }
8295 /* Fall through */
8296
8297 case R_MIPS_TLS_GD:
d0f13682 8298 case R_MIPS16_TLS_GD:
df58fc94 8299 case R_MICROMIPS_TLS_GD:
0f20cc35
DJ
8300 /* This symbol requires a global offset table entry, or two
8301 for TLS GD relocations. */
e641e783
RS
8302 if (h != NULL)
8303 {
8304 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8305 FALSE, r_type))
8306 return FALSE;
8307 }
8308 else
8309 {
8310 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8311 rel->r_addend,
8312 info, r_type))
8313 return FALSE;
8314 }
0f20cc35
DJ
8315 break;
8316
b49e97c9
TS
8317 case R_MIPS_32:
8318 case R_MIPS_REL32:
8319 case R_MIPS_64:
0a44bf69
RS
8320 /* In VxWorks executables, references to external symbols
8321 are handled using copy relocs or PLT stubs, so there's
8322 no need to add a .rela.dyn entry for this relocation. */
861fb55a 8323 if (can_make_dynamic_p)
b49e97c9
TS
8324 {
8325 if (sreloc == NULL)
8326 {
0a44bf69 8327 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 8328 if (sreloc == NULL)
f4416af6 8329 return FALSE;
b49e97c9 8330 }
9a59ad6b 8331 if (info->shared && h == NULL)
82f0cfbd
EC
8332 {
8333 /* When creating a shared object, we must copy these
8334 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
8335 relocs. Make room for this reloc in .rel(a).dyn. */
8336 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 8337 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8338 /* We tell the dynamic linker that there are
8339 relocations against the text segment. */
8340 info->flags |= DF_TEXTREL;
8341 }
b49e97c9
TS
8342 else
8343 {
8344 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 8345
9a59ad6b
DJ
8346 /* For a shared object, we must copy this relocation
8347 unless the symbol turns out to be undefined and
8348 weak with non-default visibility, in which case
8349 it will be left as zero.
8350
8351 We could elide R_MIPS_REL32 for locally binding symbols
8352 in shared libraries, but do not yet do so.
8353
8354 For an executable, we only need to copy this
8355 reloc if the symbol is defined in a dynamic
8356 object. */
b49e97c9
TS
8357 hmips = (struct mips_elf_link_hash_entry *) h;
8358 ++hmips->possibly_dynamic_relocs;
943284cc 8359 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8360 /* We need it to tell the dynamic linker if there
8361 are relocations against the text segment. */
8362 hmips->readonly_reloc = TRUE;
b49e97c9 8363 }
b49e97c9
TS
8364 }
8365
8366 if (SGI_COMPAT (abfd))
8367 mips_elf_hash_table (info)->compact_rel_size +=
8368 sizeof (Elf32_External_crinfo);
8369 break;
8370
8371 case R_MIPS_26:
8372 case R_MIPS_GPREL16:
8373 case R_MIPS_LITERAL:
8374 case R_MIPS_GPREL32:
df58fc94
RS
8375 case R_MICROMIPS_26_S1:
8376 case R_MICROMIPS_GPREL16:
8377 case R_MICROMIPS_LITERAL:
8378 case R_MICROMIPS_GPREL7_S2:
b49e97c9
TS
8379 if (SGI_COMPAT (abfd))
8380 mips_elf_hash_table (info)->compact_rel_size +=
8381 sizeof (Elf32_External_crinfo);
8382 break;
8383
8384 /* This relocation describes the C++ object vtable hierarchy.
8385 Reconstruct it for later use during GC. */
8386 case R_MIPS_GNU_VTINHERIT:
c152c796 8387 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 8388 return FALSE;
b49e97c9
TS
8389 break;
8390
8391 /* This relocation describes which C++ vtable entries are actually
8392 used. Record for later use during GC. */
8393 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
8394 BFD_ASSERT (h != NULL);
8395 if (h != NULL
8396 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 8397 return FALSE;
b49e97c9
TS
8398 break;
8399
8400 default:
8401 break;
8402 }
8403
1bbce132
MR
8404 /* Record the need for a PLT entry. At this point we don't know
8405 yet if we are going to create a PLT in the first place, but
8406 we only record whether the relocation requires a standard MIPS
8407 or a compressed code entry anyway. If we don't make a PLT after
8408 all, then we'll just ignore these arrangements. Likewise if
8409 a PLT entry is not created because the symbol is satisfied
8410 locally. */
8411 if (h != NULL
8412 && jal_reloc_p (r_type)
8413 && !SYMBOL_CALLS_LOCAL (info, h))
8414 {
8415 if (h->plt.plist == NULL)
8416 h->plt.plist = mips_elf_make_plt_record (abfd);
8417 if (h->plt.plist == NULL)
8418 return FALSE;
8419
8420 if (r_type == R_MIPS_26)
8421 h->plt.plist->need_mips = TRUE;
8422 else
8423 h->plt.plist->need_comp = TRUE;
8424 }
8425
738e5348
RS
8426 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8427 if there is one. We only need to handle global symbols here;
8428 we decide whether to keep or delete stubs for local symbols
8429 when processing the stub's relocations. */
b49e97c9 8430 if (h != NULL
738e5348
RS
8431 && !mips16_call_reloc_p (r_type)
8432 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
8433 {
8434 struct mips_elf_link_hash_entry *mh;
8435
8436 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 8437 mh->need_fn_stub = TRUE;
b49e97c9 8438 }
861fb55a
DJ
8439
8440 /* Refuse some position-dependent relocations when creating a
8441 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8442 not PIC, but we can create dynamic relocations and the result
8443 will be fine. Also do not refuse R_MIPS_LO16, which can be
8444 combined with R_MIPS_GOT16. */
8445 if (info->shared)
8446 {
8447 switch (r_type)
8448 {
8449 case R_MIPS16_HI16:
8450 case R_MIPS_HI16:
8451 case R_MIPS_HIGHER:
8452 case R_MIPS_HIGHEST:
df58fc94
RS
8453 case R_MICROMIPS_HI16:
8454 case R_MICROMIPS_HIGHER:
8455 case R_MICROMIPS_HIGHEST:
861fb55a
DJ
8456 /* Don't refuse a high part relocation if it's against
8457 no symbol (e.g. part of a compound relocation). */
cf35638d 8458 if (r_symndx == STN_UNDEF)
861fb55a
DJ
8459 break;
8460
8461 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8462 and has a special meaning. */
8463 if (!NEWABI_P (abfd) && h != NULL
8464 && strcmp (h->root.root.string, "_gp_disp") == 0)
8465 break;
8466
0fc1eb3c
RS
8467 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8468 if (is_gott_symbol (info, h))
8469 break;
8470
861fb55a
DJ
8471 /* FALLTHROUGH */
8472
8473 case R_MIPS16_26:
8474 case R_MIPS_26:
df58fc94 8475 case R_MICROMIPS_26_S1:
861fb55a
DJ
8476 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8477 (*_bfd_error_handler)
8478 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8479 abfd, howto->name,
8480 (h) ? h->root.root.string : "a local symbol");
8481 bfd_set_error (bfd_error_bad_value);
8482 return FALSE;
8483 default:
8484 break;
8485 }
8486 }
b49e97c9
TS
8487 }
8488
b34976b6 8489 return TRUE;
b49e97c9
TS
8490}
8491\f
d0647110 8492bfd_boolean
9719ad41
RS
8493_bfd_mips_relax_section (bfd *abfd, asection *sec,
8494 struct bfd_link_info *link_info,
8495 bfd_boolean *again)
d0647110
AO
8496{
8497 Elf_Internal_Rela *internal_relocs;
8498 Elf_Internal_Rela *irel, *irelend;
8499 Elf_Internal_Shdr *symtab_hdr;
8500 bfd_byte *contents = NULL;
d0647110
AO
8501 size_t extsymoff;
8502 bfd_boolean changed_contents = FALSE;
8503 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8504 Elf_Internal_Sym *isymbuf = NULL;
8505
8506 /* We are not currently changing any sizes, so only one pass. */
8507 *again = FALSE;
8508
1049f94e 8509 if (link_info->relocatable)
d0647110
AO
8510 return TRUE;
8511
9719ad41 8512 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 8513 link_info->keep_memory);
d0647110
AO
8514 if (internal_relocs == NULL)
8515 return TRUE;
8516
8517 irelend = internal_relocs + sec->reloc_count
8518 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8519 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8520 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8521
8522 for (irel = internal_relocs; irel < irelend; irel++)
8523 {
8524 bfd_vma symval;
8525 bfd_signed_vma sym_offset;
8526 unsigned int r_type;
8527 unsigned long r_symndx;
8528 asection *sym_sec;
8529 unsigned long instruction;
8530
8531 /* Turn jalr into bgezal, and jr into beq, if they're marked
8532 with a JALR relocation, that indicate where they jump to.
8533 This saves some pipeline bubbles. */
8534 r_type = ELF_R_TYPE (abfd, irel->r_info);
8535 if (r_type != R_MIPS_JALR)
8536 continue;
8537
8538 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8539 /* Compute the address of the jump target. */
8540 if (r_symndx >= extsymoff)
8541 {
8542 struct mips_elf_link_hash_entry *h
8543 = ((struct mips_elf_link_hash_entry *)
8544 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8545
8546 while (h->root.root.type == bfd_link_hash_indirect
8547 || h->root.root.type == bfd_link_hash_warning)
8548 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 8549
d0647110
AO
8550 /* If a symbol is undefined, or if it may be overridden,
8551 skip it. */
8552 if (! ((h->root.root.type == bfd_link_hash_defined
8553 || h->root.root.type == bfd_link_hash_defweak)
8554 && h->root.root.u.def.section)
8555 || (link_info->shared && ! link_info->symbolic
f5385ebf 8556 && !h->root.forced_local))
d0647110
AO
8557 continue;
8558
8559 sym_sec = h->root.root.u.def.section;
8560 if (sym_sec->output_section)
8561 symval = (h->root.root.u.def.value
8562 + sym_sec->output_section->vma
8563 + sym_sec->output_offset);
8564 else
8565 symval = h->root.root.u.def.value;
8566 }
8567 else
8568 {
8569 Elf_Internal_Sym *isym;
8570
8571 /* Read this BFD's symbols if we haven't done so already. */
8572 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8573 {
8574 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8575 if (isymbuf == NULL)
8576 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8577 symtab_hdr->sh_info, 0,
8578 NULL, NULL, NULL);
8579 if (isymbuf == NULL)
8580 goto relax_return;
8581 }
8582
8583 isym = isymbuf + r_symndx;
8584 if (isym->st_shndx == SHN_UNDEF)
8585 continue;
8586 else if (isym->st_shndx == SHN_ABS)
8587 sym_sec = bfd_abs_section_ptr;
8588 else if (isym->st_shndx == SHN_COMMON)
8589 sym_sec = bfd_com_section_ptr;
8590 else
8591 sym_sec
8592 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8593 symval = isym->st_value
8594 + sym_sec->output_section->vma
8595 + sym_sec->output_offset;
8596 }
8597
8598 /* Compute branch offset, from delay slot of the jump to the
8599 branch target. */
8600 sym_offset = (symval + irel->r_addend)
8601 - (sec_start + irel->r_offset + 4);
8602
8603 /* Branch offset must be properly aligned. */
8604 if ((sym_offset & 3) != 0)
8605 continue;
8606
8607 sym_offset >>= 2;
8608
8609 /* Check that it's in range. */
8610 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8611 continue;
143d77c5 8612
d0647110 8613 /* Get the section contents if we haven't done so already. */
c224138d
RS
8614 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8615 goto relax_return;
d0647110
AO
8616
8617 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8618
8619 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8620 if ((instruction & 0xfc1fffff) == 0x0000f809)
8621 instruction = 0x04110000;
8622 /* If it was jr <reg>, turn it into b <target>. */
8623 else if ((instruction & 0xfc1fffff) == 0x00000008)
8624 instruction = 0x10000000;
8625 else
8626 continue;
8627
8628 instruction |= (sym_offset & 0xffff);
8629 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8630 changed_contents = TRUE;
8631 }
8632
8633 if (contents != NULL
8634 && elf_section_data (sec)->this_hdr.contents != contents)
8635 {
8636 if (!changed_contents && !link_info->keep_memory)
8637 free (contents);
8638 else
8639 {
8640 /* Cache the section contents for elf_link_input_bfd. */
8641 elf_section_data (sec)->this_hdr.contents = contents;
8642 }
8643 }
8644 return TRUE;
8645
143d77c5 8646 relax_return:
eea6121a
AM
8647 if (contents != NULL
8648 && elf_section_data (sec)->this_hdr.contents != contents)
8649 free (contents);
d0647110
AO
8650 return FALSE;
8651}
8652\f
9a59ad6b
DJ
8653/* Allocate space for global sym dynamic relocs. */
8654
8655static bfd_boolean
8656allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8657{
8658 struct bfd_link_info *info = inf;
8659 bfd *dynobj;
8660 struct mips_elf_link_hash_entry *hmips;
8661 struct mips_elf_link_hash_table *htab;
8662
8663 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8664 BFD_ASSERT (htab != NULL);
8665
9a59ad6b
DJ
8666 dynobj = elf_hash_table (info)->dynobj;
8667 hmips = (struct mips_elf_link_hash_entry *) h;
8668
8669 /* VxWorks executables are handled elsewhere; we only need to
8670 allocate relocations in shared objects. */
8671 if (htab->is_vxworks && !info->shared)
8672 return TRUE;
8673
7686d77d
AM
8674 /* Ignore indirect symbols. All relocations against such symbols
8675 will be redirected to the target symbol. */
8676 if (h->root.type == bfd_link_hash_indirect)
63897e2c
RS
8677 return TRUE;
8678
9a59ad6b
DJ
8679 /* If this symbol is defined in a dynamic object, or we are creating
8680 a shared library, we will need to copy any R_MIPS_32 or
8681 R_MIPS_REL32 relocs against it into the output file. */
8682 if (! info->relocatable
8683 && hmips->possibly_dynamic_relocs != 0
8684 && (h->root.type == bfd_link_hash_defweak
625ef6dc 8685 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9a59ad6b
DJ
8686 || info->shared))
8687 {
8688 bfd_boolean do_copy = TRUE;
8689
8690 if (h->root.type == bfd_link_hash_undefweak)
8691 {
8692 /* Do not copy relocations for undefined weak symbols with
8693 non-default visibility. */
8694 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8695 do_copy = FALSE;
8696
8697 /* Make sure undefined weak symbols are output as a dynamic
8698 symbol in PIEs. */
8699 else if (h->dynindx == -1 && !h->forced_local)
8700 {
8701 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8702 return FALSE;
8703 }
8704 }
8705
8706 if (do_copy)
8707 {
aff469fa 8708 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
8709 the SVR4 psABI requires it to have a dynamic symbol table
8710 index greater that DT_MIPS_GOTSYM if there are dynamic
8711 relocations against it.
8712
8713 VxWorks does not enforce the same mapping between the GOT
8714 and the symbol table, so the same requirement does not
8715 apply there. */
6ccf4795
RS
8716 if (!htab->is_vxworks)
8717 {
8718 if (hmips->global_got_area > GGA_RELOC_ONLY)
8719 hmips->global_got_area = GGA_RELOC_ONLY;
8720 hmips->got_only_for_calls = FALSE;
8721 }
aff469fa 8722
9a59ad6b
DJ
8723 mips_elf_allocate_dynamic_relocations
8724 (dynobj, info, hmips->possibly_dynamic_relocs);
8725 if (hmips->readonly_reloc)
8726 /* We tell the dynamic linker that there are relocations
8727 against the text segment. */
8728 info->flags |= DF_TEXTREL;
8729 }
8730 }
8731
8732 return TRUE;
8733}
8734
b49e97c9
TS
8735/* Adjust a symbol defined by a dynamic object and referenced by a
8736 regular object. The current definition is in some section of the
8737 dynamic object, but we're not including those sections. We have to
8738 change the definition to something the rest of the link can
8739 understand. */
8740
b34976b6 8741bfd_boolean
9719ad41
RS
8742_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8743 struct elf_link_hash_entry *h)
b49e97c9
TS
8744{
8745 bfd *dynobj;
8746 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8747 struct mips_elf_link_hash_table *htab;
b49e97c9 8748
5108fc1b 8749 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8750 BFD_ASSERT (htab != NULL);
8751
b49e97c9 8752 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8753 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8754
8755 /* Make sure we know what is going on here. */
8756 BFD_ASSERT (dynobj != NULL
f5385ebf 8757 && (h->needs_plt
f6e332e6 8758 || h->u.weakdef != NULL
f5385ebf
AM
8759 || (h->def_dynamic
8760 && h->ref_regular
8761 && !h->def_regular)));
b49e97c9 8762
b49e97c9 8763 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8764
861fb55a
DJ
8765 /* If there are call relocations against an externally-defined symbol,
8766 see whether we can create a MIPS lazy-binding stub for it. We can
8767 only do this if all references to the function are through call
8768 relocations, and in that case, the traditional lazy-binding stubs
8769 are much more efficient than PLT entries.
8770
8771 Traditional stubs are only available on SVR4 psABI-based systems;
8772 VxWorks always uses PLTs instead. */
8773 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8774 {
8775 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8776 return TRUE;
b49e97c9
TS
8777
8778 /* If this symbol is not defined in a regular file, then set
8779 the symbol to the stub location. This is required to make
8780 function pointers compare as equal between the normal
8781 executable and the shared library. */
f5385ebf 8782 if (!h->def_regular)
b49e97c9 8783 {
33bb52fb
RS
8784 hmips->needs_lazy_stub = TRUE;
8785 htab->lazy_stub_count++;
b34976b6 8786 return TRUE;
b49e97c9
TS
8787 }
8788 }
861fb55a
DJ
8789 /* As above, VxWorks requires PLT entries for externally-defined
8790 functions that are only accessed through call relocations.
b49e97c9 8791
861fb55a
DJ
8792 Both VxWorks and non-VxWorks targets also need PLT entries if there
8793 are static-only relocations against an externally-defined function.
8794 This can technically occur for shared libraries if there are
8795 branches to the symbol, although it is unlikely that this will be
8796 used in practice due to the short ranges involved. It can occur
8797 for any relative or absolute relocation in executables; in that
8798 case, the PLT entry becomes the function's canonical address. */
8799 else if (((h->needs_plt && !hmips->no_fn_stub)
8800 || (h->type == STT_FUNC && hmips->has_static_relocs))
8801 && htab->use_plts_and_copy_relocs
8802 && !SYMBOL_CALLS_LOCAL (info, h)
8803 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8804 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8805 {
1bbce132
MR
8806 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
8807 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
8808
8809 /* If this is the first symbol to need a PLT entry, then make some
8810 basic setup. Also work out PLT entry sizes. We'll need them
8811 for PLT offset calculations. */
8812 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
861fb55a
DJ
8813 {
8814 BFD_ASSERT (htab->sgotplt->size == 0);
1bbce132 8815 BFD_ASSERT (htab->plt_got_index == 0);
0a44bf69 8816
861fb55a
DJ
8817 /* If we're using the PLT additions to the psABI, each PLT
8818 entry is 16 bytes and the PLT0 entry is 32 bytes.
8819 Encourage better cache usage by aligning. We do this
8820 lazily to avoid pessimizing traditional objects. */
8821 if (!htab->is_vxworks
8822 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8823 return FALSE;
0a44bf69 8824
861fb55a
DJ
8825 /* Make sure that .got.plt is word-aligned. We do this lazily
8826 for the same reason as above. */
8827 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8828 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8829 return FALSE;
0a44bf69 8830
861fb55a
DJ
8831 /* On non-VxWorks targets, the first two entries in .got.plt
8832 are reserved. */
8833 if (!htab->is_vxworks)
1bbce132
MR
8834 htab->plt_got_index
8835 += (get_elf_backend_data (dynobj)->got_header_size
8836 / MIPS_ELF_GOT_SIZE (dynobj));
0a44bf69 8837
861fb55a
DJ
8838 /* On VxWorks, also allocate room for the header's
8839 .rela.plt.unloaded entries. */
8840 if (htab->is_vxworks && !info->shared)
0a44bf69 8841 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
1bbce132
MR
8842
8843 /* Now work out the sizes of individual PLT entries. */
8844 if (htab->is_vxworks && info->shared)
8845 htab->plt_mips_entry_size
8846 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
8847 else if (htab->is_vxworks)
8848 htab->plt_mips_entry_size
8849 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
8850 else if (newabi_p)
8851 htab->plt_mips_entry_size
8852 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
833794fc 8853 else if (!micromips_p)
1bbce132
MR
8854 {
8855 htab->plt_mips_entry_size
8856 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8857 htab->plt_comp_entry_size
833794fc
MR
8858 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
8859 }
8860 else if (htab->insn32)
8861 {
8862 htab->plt_mips_entry_size
8863 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8864 htab->plt_comp_entry_size
8865 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
1bbce132
MR
8866 }
8867 else
8868 {
8869 htab->plt_mips_entry_size
8870 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8871 htab->plt_comp_entry_size
833794fc 8872 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
1bbce132 8873 }
0a44bf69
RS
8874 }
8875
1bbce132
MR
8876 if (h->plt.plist == NULL)
8877 h->plt.plist = mips_elf_make_plt_record (dynobj);
8878 if (h->plt.plist == NULL)
8879 return FALSE;
8880
8881 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
8882 n32 or n64, so always use a standard entry there.
8883
8884 If the symbol has a MIPS16 call stub and gets a PLT entry, then
8885 all MIPS16 calls will go via that stub, and there is no benefit
8886 to having a MIPS16 entry. And in the case of call_stub a
8887 standard entry actually has to be used as the stub ends with a J
8888 instruction. */
8889 if (newabi_p
8890 || htab->is_vxworks
8891 || hmips->call_stub
8892 || hmips->call_fp_stub)
8893 {
8894 h->plt.plist->need_mips = TRUE;
8895 h->plt.plist->need_comp = FALSE;
8896 }
8897
8898 /* Otherwise, if there are no direct calls to the function, we
8899 have a free choice of whether to use standard or compressed
8900 entries. Prefer microMIPS entries if the object is known to
8901 contain microMIPS code, so that it becomes possible to create
8902 pure microMIPS binaries. Prefer standard entries otherwise,
8903 because MIPS16 ones are no smaller and are usually slower. */
8904 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
8905 {
8906 if (micromips_p)
8907 h->plt.plist->need_comp = TRUE;
8908 else
8909 h->plt.plist->need_mips = TRUE;
8910 }
8911
8912 if (h->plt.plist->need_mips)
8913 {
8914 h->plt.plist->mips_offset = htab->plt_mips_offset;
8915 htab->plt_mips_offset += htab->plt_mips_entry_size;
8916 }
8917 if (h->plt.plist->need_comp)
8918 {
8919 h->plt.plist->comp_offset = htab->plt_comp_offset;
8920 htab->plt_comp_offset += htab->plt_comp_entry_size;
8921 }
8922
8923 /* Reserve the corresponding .got.plt entry now too. */
8924 h->plt.plist->gotplt_index = htab->plt_got_index++;
0a44bf69
RS
8925
8926 /* If the output file has no definition of the symbol, set the
861fb55a 8927 symbol's value to the address of the stub. */
131eb6b7 8928 if (!info->shared && !h->def_regular)
1bbce132 8929 hmips->use_plt_entry = TRUE;
0a44bf69 8930
1bbce132 8931 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
861fb55a
DJ
8932 htab->srelplt->size += (htab->is_vxworks
8933 ? MIPS_ELF_RELA_SIZE (dynobj)
8934 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8935
8936 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8937 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8938 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8939
861fb55a
DJ
8940 /* All relocations against this symbol that could have been made
8941 dynamic will now refer to the PLT entry instead. */
8942 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8943
0a44bf69
RS
8944 return TRUE;
8945 }
8946
8947 /* If this is a weak symbol, and there is a real definition, the
8948 processor independent code will have arranged for us to see the
8949 real definition first, and we can just use the same value. */
8950 if (h->u.weakdef != NULL)
8951 {
8952 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8953 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8954 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8955 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8956 return TRUE;
8957 }
8958
861fb55a
DJ
8959 /* Otherwise, there is nothing further to do for symbols defined
8960 in regular objects. */
8961 if (h->def_regular)
0a44bf69
RS
8962 return TRUE;
8963
861fb55a
DJ
8964 /* There's also nothing more to do if we'll convert all relocations
8965 against this symbol into dynamic relocations. */
8966 if (!hmips->has_static_relocs)
8967 return TRUE;
8968
8969 /* We're now relying on copy relocations. Complain if we have
8970 some that we can't convert. */
8971 if (!htab->use_plts_and_copy_relocs || info->shared)
8972 {
8973 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8974 "dynamic symbol %s"),
8975 h->root.root.string);
8976 bfd_set_error (bfd_error_bad_value);
8977 return FALSE;
8978 }
8979
0a44bf69
RS
8980 /* We must allocate the symbol in our .dynbss section, which will
8981 become part of the .bss section of the executable. There will be
8982 an entry for this symbol in the .dynsym section. The dynamic
8983 object will contain position independent code, so all references
8984 from the dynamic object to this symbol will go through the global
8985 offset table. The dynamic linker will use the .dynsym entry to
8986 determine the address it must put in the global offset table, so
8987 both the dynamic object and the regular object will refer to the
8988 same memory location for the variable. */
8989
8990 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8991 {
861fb55a
DJ
8992 if (htab->is_vxworks)
8993 htab->srelbss->size += sizeof (Elf32_External_Rela);
8994 else
8995 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
8996 h->needs_copy = 1;
8997 }
8998
861fb55a
DJ
8999 /* All relocations against this symbol that could have been made
9000 dynamic will now refer to the local copy instead. */
9001 hmips->possibly_dynamic_relocs = 0;
9002
027297b7 9003 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 9004}
b49e97c9
TS
9005\f
9006/* This function is called after all the input files have been read,
9007 and the input sections have been assigned to output sections. We
9008 check for any mips16 stub sections that we can discard. */
9009
b34976b6 9010bfd_boolean
9719ad41
RS
9011_bfd_mips_elf_always_size_sections (bfd *output_bfd,
9012 struct bfd_link_info *info)
b49e97c9
TS
9013{
9014 asection *ri;
0a44bf69 9015 struct mips_elf_link_hash_table *htab;
861fb55a 9016 struct mips_htab_traverse_info hti;
0a44bf69
RS
9017
9018 htab = mips_elf_hash_table (info);
4dfe6ac6 9019 BFD_ASSERT (htab != NULL);
f4416af6 9020
b49e97c9
TS
9021 /* The .reginfo section has a fixed size. */
9022 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
9023 if (ri != NULL)
9719ad41 9024 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 9025
861fb55a
DJ
9026 hti.info = info;
9027 hti.output_bfd = output_bfd;
9028 hti.error = FALSE;
9029 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9030 mips_elf_check_symbols, &hti);
9031 if (hti.error)
9032 return FALSE;
f4416af6 9033
33bb52fb
RS
9034 return TRUE;
9035}
9036
9037/* If the link uses a GOT, lay it out and work out its size. */
9038
9039static bfd_boolean
9040mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9041{
9042 bfd *dynobj;
9043 asection *s;
9044 struct mips_got_info *g;
33bb52fb
RS
9045 bfd_size_type loadable_size = 0;
9046 bfd_size_type page_gotno;
d7206569 9047 bfd *ibfd;
ab361d49 9048 struct mips_elf_traverse_got_arg tga;
33bb52fb
RS
9049 struct mips_elf_link_hash_table *htab;
9050
9051 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9052 BFD_ASSERT (htab != NULL);
9053
a8028dd0 9054 s = htab->sgot;
f4416af6 9055 if (s == NULL)
b34976b6 9056 return TRUE;
b49e97c9 9057
33bb52fb 9058 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
9059 g = htab->got_info;
9060
861fb55a
DJ
9061 /* Allocate room for the reserved entries. VxWorks always reserves
9062 3 entries; other objects only reserve 2 entries. */
9063 BFD_ASSERT (g->assigned_gotno == 0);
9064 if (htab->is_vxworks)
9065 htab->reserved_gotno = 3;
9066 else
9067 htab->reserved_gotno = 2;
9068 g->local_gotno += htab->reserved_gotno;
9069 g->assigned_gotno = htab->reserved_gotno;
9070
6c42ddb9
RS
9071 /* Decide which symbols need to go in the global part of the GOT and
9072 count the number of reloc-only GOT symbols. */
020d7251 9073 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 9074
13db6b44
RS
9075 if (!mips_elf_resolve_final_got_entries (info, g))
9076 return FALSE;
9077
33bb52fb
RS
9078 /* Calculate the total loadable size of the output. That
9079 will give us the maximum number of GOT_PAGE entries
9080 required. */
d7206569 9081 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
33bb52fb
RS
9082 {
9083 asection *subsection;
5108fc1b 9084
d7206569 9085 for (subsection = ibfd->sections;
33bb52fb
RS
9086 subsection;
9087 subsection = subsection->next)
9088 {
9089 if ((subsection->flags & SEC_ALLOC) == 0)
9090 continue;
9091 loadable_size += ((subsection->size + 0xf)
9092 &~ (bfd_size_type) 0xf);
9093 }
9094 }
f4416af6 9095
0a44bf69 9096 if (htab->is_vxworks)
738e5348 9097 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
9098 relocations against local symbols evaluate to "G", and the EABI does
9099 not include R_MIPS_GOT_PAGE. */
c224138d 9100 page_gotno = 0;
0a44bf69
RS
9101 else
9102 /* Assume there are two loadable segments consisting of contiguous
9103 sections. Is 5 enough? */
c224138d
RS
9104 page_gotno = (loadable_size >> 16) + 5;
9105
13db6b44 9106 /* Choose the smaller of the two page estimates; both are intended to be
c224138d
RS
9107 conservative. */
9108 if (page_gotno > g->page_gotno)
9109 page_gotno = g->page_gotno;
f4416af6 9110
c224138d 9111 g->local_gotno += page_gotno;
ab361d49 9112
ab361d49
RS
9113 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9114 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
0f20cc35
DJ
9115 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9116
0a44bf69
RS
9117 /* VxWorks does not support multiple GOTs. It initializes $gp to
9118 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9119 dynamic loader. */
57093f5e 9120 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 9121 {
a8028dd0 9122 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
9123 return FALSE;
9124 }
9125 else
9126 {
d7206569
RS
9127 /* Record that all bfds use G. This also has the effect of freeing
9128 the per-bfd GOTs, which we no longer need. */
9129 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9130 if (mips_elf_bfd_got (ibfd, FALSE))
9131 mips_elf_replace_bfd_got (ibfd, g);
9132 mips_elf_replace_bfd_got (output_bfd, g);
9133
33bb52fb 9134 /* Set up TLS entries. */
0f20cc35 9135 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
72e7511a
RS
9136 tga.info = info;
9137 tga.g = g;
9138 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9139 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9140 if (!tga.g)
9141 return FALSE;
1fd20d70
RS
9142 BFD_ASSERT (g->tls_assigned_gotno
9143 == g->global_gotno + g->local_gotno + g->tls_gotno);
33bb52fb 9144
57093f5e
RS
9145 /* Each VxWorks GOT entry needs an explicit relocation. */
9146 if (htab->is_vxworks && info->shared)
9147 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9148
33bb52fb 9149 /* Allocate room for the TLS relocations. */
ab361d49
RS
9150 if (g->relocs)
9151 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
0f20cc35 9152 }
b49e97c9 9153
b34976b6 9154 return TRUE;
b49e97c9
TS
9155}
9156
33bb52fb
RS
9157/* Estimate the size of the .MIPS.stubs section. */
9158
9159static void
9160mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9161{
9162 struct mips_elf_link_hash_table *htab;
9163 bfd_size_type dynsymcount;
9164
9165 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9166 BFD_ASSERT (htab != NULL);
9167
33bb52fb
RS
9168 if (htab->lazy_stub_count == 0)
9169 return;
9170
9171 /* IRIX rld assumes that a function stub isn't at the end of the .text
9172 section, so add a dummy entry to the end. */
9173 htab->lazy_stub_count++;
9174
9175 /* Get a worst-case estimate of the number of dynamic symbols needed.
9176 At this point, dynsymcount does not account for section symbols
9177 and count_section_dynsyms may overestimate the number that will
9178 be needed. */
9179 dynsymcount = (elf_hash_table (info)->dynsymcount
9180 + count_section_dynsyms (output_bfd, info));
9181
1bbce132
MR
9182 /* Determine the size of one stub entry. There's no disadvantage
9183 from using microMIPS code here, so for the sake of pure-microMIPS
9184 binaries we prefer it whenever there's any microMIPS code in
9185 output produced at all. This has a benefit of stubs being
833794fc
MR
9186 shorter by 4 bytes each too, unless in the insn32 mode. */
9187 if (!MICROMIPS_P (output_bfd))
1bbce132
MR
9188 htab->function_stub_size = (dynsymcount > 0x10000
9189 ? MIPS_FUNCTION_STUB_BIG_SIZE
9190 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
833794fc
MR
9191 else if (htab->insn32)
9192 htab->function_stub_size = (dynsymcount > 0x10000
9193 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9194 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9195 else
9196 htab->function_stub_size = (dynsymcount > 0x10000
9197 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9198 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
33bb52fb
RS
9199
9200 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9201}
9202
1bbce132
MR
9203/* A mips_elf_link_hash_traverse callback for which DATA points to a
9204 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9205 stub, allocate an entry in the stubs section. */
33bb52fb
RS
9206
9207static bfd_boolean
af924177 9208mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 9209{
1bbce132 9210 struct mips_htab_traverse_info *hti = data;
33bb52fb 9211 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9212 struct bfd_link_info *info;
9213 bfd *output_bfd;
9214
9215 info = hti->info;
9216 output_bfd = hti->output_bfd;
9217 htab = mips_elf_hash_table (info);
9218 BFD_ASSERT (htab != NULL);
33bb52fb 9219
33bb52fb
RS
9220 if (h->needs_lazy_stub)
9221 {
1bbce132
MR
9222 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9223 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9224 bfd_vma isa_bit = micromips_p;
9225
9226 BFD_ASSERT (htab->root.dynobj != NULL);
9227 if (h->root.plt.plist == NULL)
9228 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9229 if (h->root.plt.plist == NULL)
9230 {
9231 hti->error = TRUE;
9232 return FALSE;
9233 }
33bb52fb 9234 h->root.root.u.def.section = htab->sstubs;
1bbce132
MR
9235 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9236 h->root.plt.plist->stub_offset = htab->sstubs->size;
9237 h->root.other = other;
33bb52fb
RS
9238 htab->sstubs->size += htab->function_stub_size;
9239 }
9240 return TRUE;
9241}
9242
9243/* Allocate offsets in the stubs section to each symbol that needs one.
9244 Set the final size of the .MIPS.stub section. */
9245
1bbce132 9246static bfd_boolean
33bb52fb
RS
9247mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9248{
1bbce132
MR
9249 bfd *output_bfd = info->output_bfd;
9250 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9251 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9252 bfd_vma isa_bit = micromips_p;
33bb52fb 9253 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9254 struct mips_htab_traverse_info hti;
9255 struct elf_link_hash_entry *h;
9256 bfd *dynobj;
33bb52fb
RS
9257
9258 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9259 BFD_ASSERT (htab != NULL);
9260
33bb52fb 9261 if (htab->lazy_stub_count == 0)
1bbce132 9262 return TRUE;
33bb52fb
RS
9263
9264 htab->sstubs->size = 0;
1bbce132
MR
9265 hti.info = info;
9266 hti.output_bfd = output_bfd;
9267 hti.error = FALSE;
9268 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9269 if (hti.error)
9270 return FALSE;
33bb52fb
RS
9271 htab->sstubs->size += htab->function_stub_size;
9272 BFD_ASSERT (htab->sstubs->size
9273 == htab->lazy_stub_count * htab->function_stub_size);
1bbce132
MR
9274
9275 dynobj = elf_hash_table (info)->dynobj;
9276 BFD_ASSERT (dynobj != NULL);
9277 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9278 if (h == NULL)
9279 return FALSE;
9280 h->root.u.def.value = isa_bit;
9281 h->other = other;
9282 h->type = STT_FUNC;
9283
9284 return TRUE;
9285}
9286
9287/* A mips_elf_link_hash_traverse callback for which DATA points to a
9288 bfd_link_info. If H uses the address of a PLT entry as the value
9289 of the symbol, then set the entry in the symbol table now. Prefer
9290 a standard MIPS PLT entry. */
9291
9292static bfd_boolean
9293mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9294{
9295 struct bfd_link_info *info = data;
9296 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9297 struct mips_elf_link_hash_table *htab;
9298 unsigned int other;
9299 bfd_vma isa_bit;
9300 bfd_vma val;
9301
9302 htab = mips_elf_hash_table (info);
9303 BFD_ASSERT (htab != NULL);
9304
9305 if (h->use_plt_entry)
9306 {
9307 BFD_ASSERT (h->root.plt.plist != NULL);
9308 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9309 || h->root.plt.plist->comp_offset != MINUS_ONE);
9310
9311 val = htab->plt_header_size;
9312 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9313 {
9314 isa_bit = 0;
9315 val += h->root.plt.plist->mips_offset;
9316 other = 0;
9317 }
9318 else
9319 {
9320 isa_bit = 1;
9321 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9322 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9323 }
9324 val += isa_bit;
9325 /* For VxWorks, point at the PLT load stub rather than the lazy
9326 resolution stub; this stub will become the canonical function
9327 address. */
9328 if (htab->is_vxworks)
9329 val += 8;
9330
9331 h->root.root.u.def.section = htab->splt;
9332 h->root.root.u.def.value = val;
9333 h->root.other = other;
9334 }
9335
9336 return TRUE;
33bb52fb
RS
9337}
9338
b49e97c9
TS
9339/* Set the sizes of the dynamic sections. */
9340
b34976b6 9341bfd_boolean
9719ad41
RS
9342_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9343 struct bfd_link_info *info)
b49e97c9
TS
9344{
9345 bfd *dynobj;
861fb55a 9346 asection *s, *sreldyn;
b34976b6 9347 bfd_boolean reltext;
0a44bf69 9348 struct mips_elf_link_hash_table *htab;
b49e97c9 9349
0a44bf69 9350 htab = mips_elf_hash_table (info);
4dfe6ac6 9351 BFD_ASSERT (htab != NULL);
b49e97c9
TS
9352 dynobj = elf_hash_table (info)->dynobj;
9353 BFD_ASSERT (dynobj != NULL);
9354
9355 if (elf_hash_table (info)->dynamic_sections_created)
9356 {
9357 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 9358 if (info->executable)
b49e97c9 9359 {
3d4d4302 9360 s = bfd_get_linker_section (dynobj, ".interp");
b49e97c9 9361 BFD_ASSERT (s != NULL);
eea6121a 9362 s->size
b49e97c9
TS
9363 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9364 s->contents
9365 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9366 }
861fb55a 9367
1bbce132
MR
9368 /* Figure out the size of the PLT header if we know that we
9369 are using it. For the sake of cache alignment always use
9370 a standard header whenever any standard entries are present
9371 even if microMIPS entries are present as well. This also
9372 lets the microMIPS header rely on the value of $v0 only set
9373 by microMIPS entries, for a small size reduction.
9374
9375 Set symbol table entry values for symbols that use the
9376 address of their PLT entry now that we can calculate it.
9377
9378 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9379 haven't already in _bfd_elf_create_dynamic_sections. */
9380 if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
861fb55a 9381 {
1bbce132
MR
9382 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9383 && !htab->plt_mips_offset);
9384 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9385 bfd_vma isa_bit = micromips_p;
861fb55a 9386 struct elf_link_hash_entry *h;
1bbce132 9387 bfd_vma size;
861fb55a
DJ
9388
9389 BFD_ASSERT (htab->use_plts_and_copy_relocs);
1bbce132
MR
9390 BFD_ASSERT (htab->sgotplt->size == 0);
9391 BFD_ASSERT (htab->splt->size == 0);
9392
9393 if (htab->is_vxworks && info->shared)
9394 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9395 else if (htab->is_vxworks)
9396 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9397 else if (ABI_64_P (output_bfd))
9398 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9399 else if (ABI_N32_P (output_bfd))
9400 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9401 else if (!micromips_p)
9402 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
833794fc
MR
9403 else if (htab->insn32)
9404 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
1bbce132
MR
9405 else
9406 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
861fb55a 9407
1bbce132
MR
9408 htab->plt_header_is_comp = micromips_p;
9409 htab->plt_header_size = size;
9410 htab->splt->size = (size
9411 + htab->plt_mips_offset
9412 + htab->plt_comp_offset);
9413 htab->sgotplt->size = (htab->plt_got_index
9414 * MIPS_ELF_GOT_SIZE (dynobj));
9415
9416 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9417
9418 if (htab->root.hplt == NULL)
9419 {
9420 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9421 "_PROCEDURE_LINKAGE_TABLE_");
9422 htab->root.hplt = h;
9423 if (h == NULL)
9424 return FALSE;
9425 }
9426
9427 h = htab->root.hplt;
9428 h->root.u.def.value = isa_bit;
9429 h->other = other;
861fb55a
DJ
9430 h->type = STT_FUNC;
9431 }
9432 }
4e41d0d7 9433
9a59ad6b 9434 /* Allocate space for global sym dynamic relocs. */
2c3fc389 9435 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9a59ad6b 9436
33bb52fb
RS
9437 mips_elf_estimate_stub_size (output_bfd, info);
9438
9439 if (!mips_elf_lay_out_got (output_bfd, info))
9440 return FALSE;
9441
9442 mips_elf_lay_out_lazy_stubs (info);
9443
b49e97c9
TS
9444 /* The check_relocs and adjust_dynamic_symbol entry points have
9445 determined the sizes of the various dynamic sections. Allocate
9446 memory for them. */
b34976b6 9447 reltext = FALSE;
b49e97c9
TS
9448 for (s = dynobj->sections; s != NULL; s = s->next)
9449 {
9450 const char *name;
b49e97c9
TS
9451
9452 /* It's OK to base decisions on the section name, because none
9453 of the dynobj section names depend upon the input files. */
9454 name = bfd_get_section_name (dynobj, s);
9455
9456 if ((s->flags & SEC_LINKER_CREATED) == 0)
9457 continue;
9458
0112cd26 9459 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 9460 {
c456f082 9461 if (s->size != 0)
b49e97c9
TS
9462 {
9463 const char *outname;
9464 asection *target;
9465
9466 /* If this relocation section applies to a read only
9467 section, then we probably need a DT_TEXTREL entry.
0a44bf69 9468 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
9469 assert a DT_TEXTREL entry rather than testing whether
9470 there exists a relocation to a read only section or
9471 not. */
9472 outname = bfd_get_section_name (output_bfd,
9473 s->output_section);
9474 target = bfd_get_section_by_name (output_bfd, outname + 4);
9475 if ((target != NULL
9476 && (target->flags & SEC_READONLY) != 0
9477 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 9478 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 9479 reltext = TRUE;
b49e97c9
TS
9480
9481 /* We use the reloc_count field as a counter if we need
9482 to copy relocs into the output file. */
0a44bf69 9483 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 9484 s->reloc_count = 0;
f4416af6
AO
9485
9486 /* If combreloc is enabled, elf_link_sort_relocs() will
9487 sort relocations, but in a different way than we do,
9488 and before we're done creating relocations. Also, it
9489 will move them around between input sections'
9490 relocation's contents, so our sorting would be
9491 broken, so don't let it run. */
9492 info->combreloc = 0;
b49e97c9
TS
9493 }
9494 }
b49e97c9
TS
9495 else if (! info->shared
9496 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 9497 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 9498 {
5108fc1b 9499 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 9500 rtld to contain a pointer to the _r_debug structure. */
b4082c70 9501 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
b49e97c9
TS
9502 }
9503 else if (SGI_COMPAT (output_bfd)
0112cd26 9504 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 9505 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
9506 else if (s == htab->splt)
9507 {
9508 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
9509 room for an extra nop to fill the delay slot. This is
9510 for CPUs without load interlocking. */
9511 if (! LOAD_INTERLOCKS_P (output_bfd)
9512 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
9513 s->size += 4;
9514 }
0112cd26 9515 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 9516 && s != htab->sgot
0a44bf69 9517 && s != htab->sgotplt
861fb55a
DJ
9518 && s != htab->sstubs
9519 && s != htab->sdynbss)
b49e97c9
TS
9520 {
9521 /* It's not one of our sections, so don't allocate space. */
9522 continue;
9523 }
9524
c456f082 9525 if (s->size == 0)
b49e97c9 9526 {
8423293d 9527 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
9528 continue;
9529 }
9530
c456f082
AM
9531 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9532 continue;
9533
b49e97c9 9534 /* Allocate memory for the section contents. */
eea6121a 9535 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 9536 if (s->contents == NULL)
b49e97c9
TS
9537 {
9538 bfd_set_error (bfd_error_no_memory);
b34976b6 9539 return FALSE;
b49e97c9
TS
9540 }
9541 }
9542
9543 if (elf_hash_table (info)->dynamic_sections_created)
9544 {
9545 /* Add some entries to the .dynamic section. We fill in the
9546 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9547 must add the entries now so that we get the correct size for
5750dcec 9548 the .dynamic section. */
af5978fb
RS
9549
9550 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec 9551 DT_MIPS_RLD_MAP entry. This must come first because glibc
6e6be592
MR
9552 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9553 may only look at the first one they see. */
af5978fb
RS
9554 if (!info->shared
9555 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9556 return FALSE;
b49e97c9 9557
5750dcec
DJ
9558 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9559 used by the debugger. */
9560 if (info->executable
9561 && !SGI_COMPAT (output_bfd)
9562 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9563 return FALSE;
9564
0a44bf69 9565 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
9566 info->flags |= DF_TEXTREL;
9567
9568 if ((info->flags & DF_TEXTREL) != 0)
9569 {
9570 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 9571 return FALSE;
943284cc
DJ
9572
9573 /* Clear the DF_TEXTREL flag. It will be set again if we
9574 write out an actual text relocation; we may not, because
9575 at this point we do not know whether e.g. any .eh_frame
9576 absolute relocations have been converted to PC-relative. */
9577 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
9578 }
9579
9580 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 9581 return FALSE;
b49e97c9 9582
861fb55a 9583 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 9584 if (htab->is_vxworks)
b49e97c9 9585 {
0a44bf69
RS
9586 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9587 use any of the DT_MIPS_* tags. */
861fb55a 9588 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
9589 {
9590 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9591 return FALSE;
b49e97c9 9592
0a44bf69
RS
9593 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9594 return FALSE;
b49e97c9 9595
0a44bf69
RS
9596 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9597 return FALSE;
9598 }
b49e97c9 9599 }
0a44bf69
RS
9600 else
9601 {
861fb55a 9602 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
9603 {
9604 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9605 return FALSE;
b49e97c9 9606
0a44bf69
RS
9607 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9608 return FALSE;
b49e97c9 9609
0a44bf69
RS
9610 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9611 return FALSE;
9612 }
b49e97c9 9613
0a44bf69
RS
9614 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9615 return FALSE;
b49e97c9 9616
0a44bf69
RS
9617 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9618 return FALSE;
b49e97c9 9619
0a44bf69
RS
9620 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9621 return FALSE;
b49e97c9 9622
0a44bf69
RS
9623 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9624 return FALSE;
b49e97c9 9625
0a44bf69
RS
9626 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9627 return FALSE;
b49e97c9 9628
0a44bf69
RS
9629 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9630 return FALSE;
b49e97c9 9631
0a44bf69
RS
9632 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9633 return FALSE;
9634
9635 if (IRIX_COMPAT (dynobj) == ict_irix5
9636 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9637 return FALSE;
9638
9639 if (IRIX_COMPAT (dynobj) == ict_irix6
9640 && (bfd_get_section_by_name
af0edeb8 9641 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
0a44bf69
RS
9642 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9643 return FALSE;
9644 }
861fb55a
DJ
9645 if (htab->splt->size > 0)
9646 {
9647 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9648 return FALSE;
9649
9650 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9651 return FALSE;
9652
9653 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9654 return FALSE;
9655
9656 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9657 return FALSE;
9658 }
7a2b07ff
NS
9659 if (htab->is_vxworks
9660 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9661 return FALSE;
b49e97c9
TS
9662 }
9663
b34976b6 9664 return TRUE;
b49e97c9
TS
9665}
9666\f
81d43bff
RS
9667/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9668 Adjust its R_ADDEND field so that it is correct for the output file.
9669 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9670 and sections respectively; both use symbol indexes. */
9671
9672static void
9673mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9674 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9675 asection **local_sections, Elf_Internal_Rela *rel)
9676{
9677 unsigned int r_type, r_symndx;
9678 Elf_Internal_Sym *sym;
9679 asection *sec;
9680
020d7251 9681 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
9682 {
9683 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
df58fc94 9684 if (gprel16_reloc_p (r_type)
81d43bff 9685 || r_type == R_MIPS_GPREL32
df58fc94 9686 || literal_reloc_p (r_type))
81d43bff
RS
9687 {
9688 rel->r_addend += _bfd_get_gp_value (input_bfd);
9689 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9690 }
9691
9692 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9693 sym = local_syms + r_symndx;
9694
9695 /* Adjust REL's addend to account for section merging. */
9696 if (!info->relocatable)
9697 {
9698 sec = local_sections[r_symndx];
9699 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9700 }
9701
9702 /* This would normally be done by the rela_normal code in elflink.c. */
9703 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9704 rel->r_addend += local_sections[r_symndx]->output_offset;
9705 }
9706}
9707
545fd46b
MR
9708/* Handle relocations against symbols from removed linkonce sections,
9709 or sections discarded by a linker script. We use this wrapper around
9710 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9711 on 64-bit ELF targets. In this case for any relocation handled, which
9712 always be the first in a triplet, the remaining two have to be processed
9713 together with the first, even if they are R_MIPS_NONE. It is the symbol
9714 index referred by the first reloc that applies to all the three and the
9715 remaining two never refer to an object symbol. And it is the final
9716 relocation (the last non-null one) that determines the output field of
9717 the whole relocation so retrieve the corresponding howto structure for
9718 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9719
9720 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9721 and therefore requires to be pasted in a loop. It also defines a block
9722 and does not protect any of its arguments, hence the extra brackets. */
9723
9724static void
9725mips_reloc_against_discarded_section (bfd *output_bfd,
9726 struct bfd_link_info *info,
9727 bfd *input_bfd, asection *input_section,
9728 Elf_Internal_Rela **rel,
9729 const Elf_Internal_Rela **relend,
9730 bfd_boolean rel_reloc,
9731 reloc_howto_type *howto,
9732 bfd_byte *contents)
9733{
9734 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9735 int count = bed->s->int_rels_per_ext_rel;
9736 unsigned int r_type;
9737 int i;
9738
9739 for (i = count - 1; i > 0; i--)
9740 {
9741 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9742 if (r_type != R_MIPS_NONE)
9743 {
9744 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9745 break;
9746 }
9747 }
9748 do
9749 {
9750 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9751 (*rel), count, (*relend),
9752 howto, i, contents);
9753 }
9754 while (0);
9755}
9756
b49e97c9
TS
9757/* Relocate a MIPS ELF section. */
9758
b34976b6 9759bfd_boolean
9719ad41
RS
9760_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9761 bfd *input_bfd, asection *input_section,
9762 bfd_byte *contents, Elf_Internal_Rela *relocs,
9763 Elf_Internal_Sym *local_syms,
9764 asection **local_sections)
b49e97c9
TS
9765{
9766 Elf_Internal_Rela *rel;
9767 const Elf_Internal_Rela *relend;
9768 bfd_vma addend = 0;
b34976b6 9769 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 9770 const struct elf_backend_data *bed;
b49e97c9
TS
9771
9772 bed = get_elf_backend_data (output_bfd);
9773 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9774 for (rel = relocs; rel < relend; ++rel)
9775 {
9776 const char *name;
c9adbffe 9777 bfd_vma value = 0;
b49e97c9 9778 reloc_howto_type *howto;
ad3d9127 9779 bfd_boolean cross_mode_jump_p = FALSE;
b34976b6 9780 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 9781 REL relocation. */
b34976b6 9782 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 9783 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 9784 const char *msg;
ab96bf03
AM
9785 unsigned long r_symndx;
9786 asection *sec;
749b8d9d
L
9787 Elf_Internal_Shdr *symtab_hdr;
9788 struct elf_link_hash_entry *h;
d4730f92 9789 bfd_boolean rel_reloc;
b49e97c9 9790
d4730f92
BS
9791 rel_reloc = (NEWABI_P (input_bfd)
9792 && mips_elf_rel_relocation_p (input_bfd, input_section,
9793 relocs, rel));
b49e97c9 9794 /* Find the relocation howto for this relocation. */
d4730f92 9795 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
ab96bf03
AM
9796
9797 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 9798 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 9799 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
9800 {
9801 sec = local_sections[r_symndx];
9802 h = NULL;
9803 }
ab96bf03
AM
9804 else
9805 {
ab96bf03 9806 unsigned long extsymoff;
ab96bf03 9807
ab96bf03
AM
9808 extsymoff = 0;
9809 if (!elf_bad_symtab (input_bfd))
9810 extsymoff = symtab_hdr->sh_info;
9811 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9812 while (h->root.type == bfd_link_hash_indirect
9813 || h->root.type == bfd_link_hash_warning)
9814 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9815
9816 sec = NULL;
9817 if (h->root.type == bfd_link_hash_defined
9818 || h->root.type == bfd_link_hash_defweak)
9819 sec = h->root.u.def.section;
9820 }
9821
dbaa2011 9822 if (sec != NULL && discarded_section (sec))
545fd46b
MR
9823 {
9824 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9825 input_section, &rel, &relend,
9826 rel_reloc, howto, contents);
9827 continue;
9828 }
ab96bf03 9829
4a14403c 9830 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
9831 {
9832 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9833 64-bit code, but make sure all their addresses are in the
9834 lowermost or uppermost 32-bit section of the 64-bit address
9835 space. Thus, when they use an R_MIPS_64 they mean what is
9836 usually meant by R_MIPS_32, with the exception that the
9837 stored value is sign-extended to 64 bits. */
b34976b6 9838 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
9839
9840 /* On big-endian systems, we need to lie about the position
9841 of the reloc. */
9842 if (bfd_big_endian (input_bfd))
9843 rel->r_offset += 4;
9844 }
b49e97c9
TS
9845
9846 if (!use_saved_addend_p)
9847 {
b49e97c9
TS
9848 /* If these relocations were originally of the REL variety,
9849 we must pull the addend out of the field that will be
9850 relocated. Otherwise, we simply use the contents of the
c224138d
RS
9851 RELA relocation. */
9852 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9853 relocs, rel))
b49e97c9 9854 {
b34976b6 9855 rela_relocation_p = FALSE;
c224138d
RS
9856 addend = mips_elf_read_rel_addend (input_bfd, rel,
9857 howto, contents);
738e5348
RS
9858 if (hi16_reloc_p (r_type)
9859 || (got16_reloc_p (r_type)
b49e97c9 9860 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 9861 local_sections)))
b49e97c9 9862 {
c224138d
RS
9863 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9864 contents, &addend))
749b8d9d 9865 {
749b8d9d
L
9866 if (h)
9867 name = h->root.root.string;
9868 else
9869 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9870 local_syms + r_symndx,
9871 sec);
9872 (*_bfd_error_handler)
9873 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9874 input_bfd, input_section, name, howto->name,
9875 rel->r_offset);
749b8d9d 9876 }
b49e97c9 9877 }
30ac9238
RS
9878 else
9879 addend <<= howto->rightshift;
b49e97c9
TS
9880 }
9881 else
9882 addend = rel->r_addend;
81d43bff
RS
9883 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9884 local_syms, local_sections, rel);
b49e97c9
TS
9885 }
9886
1049f94e 9887 if (info->relocatable)
b49e97c9 9888 {
4a14403c 9889 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9890 && bfd_big_endian (input_bfd))
9891 rel->r_offset -= 4;
9892
81d43bff 9893 if (!rela_relocation_p && rel->r_addend)
5a659663 9894 {
81d43bff 9895 addend += rel->r_addend;
738e5348 9896 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9897 addend = mips_elf_high (addend);
9898 else if (r_type == R_MIPS_HIGHER)
9899 addend = mips_elf_higher (addend);
9900 else if (r_type == R_MIPS_HIGHEST)
9901 addend = mips_elf_highest (addend);
30ac9238
RS
9902 else
9903 addend >>= howto->rightshift;
b49e97c9 9904
30ac9238
RS
9905 /* We use the source mask, rather than the destination
9906 mask because the place to which we are writing will be
9907 source of the addend in the final link. */
b49e97c9
TS
9908 addend &= howto->src_mask;
9909
5a659663 9910 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9911 /* See the comment above about using R_MIPS_64 in the 32-bit
9912 ABI. Here, we need to update the addend. It would be
9913 possible to get away with just using the R_MIPS_32 reloc
9914 but for endianness. */
9915 {
9916 bfd_vma sign_bits;
9917 bfd_vma low_bits;
9918 bfd_vma high_bits;
9919
9920 if (addend & ((bfd_vma) 1 << 31))
9921#ifdef BFD64
9922 sign_bits = ((bfd_vma) 1 << 32) - 1;
9923#else
9924 sign_bits = -1;
9925#endif
9926 else
9927 sign_bits = 0;
9928
9929 /* If we don't know that we have a 64-bit type,
9930 do two separate stores. */
9931 if (bfd_big_endian (input_bfd))
9932 {
9933 /* Store the sign-bits (which are most significant)
9934 first. */
9935 low_bits = sign_bits;
9936 high_bits = addend;
9937 }
9938 else
9939 {
9940 low_bits = addend;
9941 high_bits = sign_bits;
9942 }
9943 bfd_put_32 (input_bfd, low_bits,
9944 contents + rel->r_offset);
9945 bfd_put_32 (input_bfd, high_bits,
9946 contents + rel->r_offset + 4);
9947 continue;
9948 }
9949
9950 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9951 input_bfd, input_section,
b34976b6
AM
9952 contents, FALSE))
9953 return FALSE;
b49e97c9
TS
9954 }
9955
9956 /* Go on to the next relocation. */
9957 continue;
9958 }
9959
9960 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9961 relocations for the same offset. In that case we are
9962 supposed to treat the output of each relocation as the addend
9963 for the next. */
9964 if (rel + 1 < relend
9965 && rel->r_offset == rel[1].r_offset
9966 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9967 use_saved_addend_p = TRUE;
b49e97c9 9968 else
b34976b6 9969 use_saved_addend_p = FALSE;
b49e97c9
TS
9970
9971 /* Figure out what value we are supposed to relocate. */
9972 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9973 input_section, info, rel,
9974 addend, howto, local_syms,
9975 local_sections, &value,
38a7df63 9976 &name, &cross_mode_jump_p,
bce03d3d 9977 use_saved_addend_p))
b49e97c9
TS
9978 {
9979 case bfd_reloc_continue:
9980 /* There's nothing to do. */
9981 continue;
9982
9983 case bfd_reloc_undefined:
9984 /* mips_elf_calculate_relocation already called the
9985 undefined_symbol callback. There's no real point in
9986 trying to perform the relocation at this point, so we
9987 just skip ahead to the next relocation. */
9988 continue;
9989
9990 case bfd_reloc_notsupported:
9991 msg = _("internal error: unsupported relocation error");
9992 info->callbacks->warning
9993 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9994 return FALSE;
b49e97c9
TS
9995
9996 case bfd_reloc_overflow:
9997 if (use_saved_addend_p)
9998 /* Ignore overflow until we reach the last relocation for
9999 a given location. */
10000 ;
10001 else
10002 {
0e53d9da
AN
10003 struct mips_elf_link_hash_table *htab;
10004
10005 htab = mips_elf_hash_table (info);
4dfe6ac6 10006 BFD_ASSERT (htab != NULL);
b49e97c9 10007 BFD_ASSERT (name != NULL);
0e53d9da 10008 if (!htab->small_data_overflow_reported
9684f078 10009 && (gprel16_reloc_p (howto->type)
df58fc94 10010 || literal_reloc_p (howto->type)))
0e53d9da 10011 {
91d6fa6a
NC
10012 msg = _("small-data section exceeds 64KB;"
10013 " lower small-data size limit (see option -G)");
0e53d9da
AN
10014
10015 htab->small_data_overflow_reported = TRUE;
10016 (*info->callbacks->einfo) ("%P: %s\n", msg);
10017 }
b49e97c9 10018 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 10019 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 10020 input_bfd, input_section, rel->r_offset)))
b34976b6 10021 return FALSE;
b49e97c9
TS
10022 }
10023 break;
10024
10025 case bfd_reloc_ok:
10026 break;
10027
df58fc94
RS
10028 case bfd_reloc_outofrange:
10029 if (jal_reloc_p (howto->type))
10030 {
10031 msg = _("JALX to a non-word-aligned address");
10032 info->callbacks->warning
10033 (info, msg, name, input_bfd, input_section, rel->r_offset);
10034 return FALSE;
10035 }
10036 /* Fall through. */
10037
b49e97c9
TS
10038 default:
10039 abort ();
10040 break;
10041 }
10042
10043 /* If we've got another relocation for the address, keep going
10044 until we reach the last one. */
10045 if (use_saved_addend_p)
10046 {
10047 addend = value;
10048 continue;
10049 }
10050
4a14403c 10051 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10052 /* See the comment above about using R_MIPS_64 in the 32-bit
10053 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10054 that calculated the right value. Now, however, we
10055 sign-extend the 32-bit result to 64-bits, and store it as a
10056 64-bit value. We are especially generous here in that we
10057 go to extreme lengths to support this usage on systems with
10058 only a 32-bit VMA. */
10059 {
10060 bfd_vma sign_bits;
10061 bfd_vma low_bits;
10062 bfd_vma high_bits;
10063
10064 if (value & ((bfd_vma) 1 << 31))
10065#ifdef BFD64
10066 sign_bits = ((bfd_vma) 1 << 32) - 1;
10067#else
10068 sign_bits = -1;
10069#endif
10070 else
10071 sign_bits = 0;
10072
10073 /* If we don't know that we have a 64-bit type,
10074 do two separate stores. */
10075 if (bfd_big_endian (input_bfd))
10076 {
10077 /* Undo what we did above. */
10078 rel->r_offset -= 4;
10079 /* Store the sign-bits (which are most significant)
10080 first. */
10081 low_bits = sign_bits;
10082 high_bits = value;
10083 }
10084 else
10085 {
10086 low_bits = value;
10087 high_bits = sign_bits;
10088 }
10089 bfd_put_32 (input_bfd, low_bits,
10090 contents + rel->r_offset);
10091 bfd_put_32 (input_bfd, high_bits,
10092 contents + rel->r_offset + 4);
10093 continue;
10094 }
10095
10096 /* Actually perform the relocation. */
10097 if (! mips_elf_perform_relocation (info, howto, rel, value,
10098 input_bfd, input_section,
38a7df63 10099 contents, cross_mode_jump_p))
b34976b6 10100 return FALSE;
b49e97c9
TS
10101 }
10102
b34976b6 10103 return TRUE;
b49e97c9
TS
10104}
10105\f
861fb55a
DJ
10106/* A function that iterates over each entry in la25_stubs and fills
10107 in the code for each one. DATA points to a mips_htab_traverse_info. */
10108
10109static int
10110mips_elf_create_la25_stub (void **slot, void *data)
10111{
10112 struct mips_htab_traverse_info *hti;
10113 struct mips_elf_link_hash_table *htab;
10114 struct mips_elf_la25_stub *stub;
10115 asection *s;
10116 bfd_byte *loc;
10117 bfd_vma offset, target, target_high, target_low;
10118
10119 stub = (struct mips_elf_la25_stub *) *slot;
10120 hti = (struct mips_htab_traverse_info *) data;
10121 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 10122 BFD_ASSERT (htab != NULL);
861fb55a
DJ
10123
10124 /* Create the section contents, if we haven't already. */
10125 s = stub->stub_section;
10126 loc = s->contents;
10127 if (loc == NULL)
10128 {
10129 loc = bfd_malloc (s->size);
10130 if (loc == NULL)
10131 {
10132 hti->error = TRUE;
10133 return FALSE;
10134 }
10135 s->contents = loc;
10136 }
10137
10138 /* Work out where in the section this stub should go. */
10139 offset = stub->offset;
10140
10141 /* Work out the target address. */
8f0c309a
CLT
10142 target = mips_elf_get_la25_target (stub, &s);
10143 target += s->output_section->vma + s->output_offset;
10144
861fb55a
DJ
10145 target_high = ((target + 0x8000) >> 16) & 0xffff;
10146 target_low = (target & 0xffff);
10147
10148 if (stub->stub_section != htab->strampoline)
10149 {
df58fc94 10150 /* This is a simple LUI/ADDIU stub. Zero out the beginning
861fb55a
DJ
10151 of the section and write the two instructions at the end. */
10152 memset (loc, 0, offset);
10153 loc += offset;
df58fc94
RS
10154 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10155 {
d21911ea
MR
10156 bfd_put_micromips_32 (hti->output_bfd,
10157 LA25_LUI_MICROMIPS (target_high),
10158 loc);
10159 bfd_put_micromips_32 (hti->output_bfd,
10160 LA25_ADDIU_MICROMIPS (target_low),
10161 loc + 4);
df58fc94
RS
10162 }
10163 else
10164 {
10165 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10166 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10167 }
861fb55a
DJ
10168 }
10169 else
10170 {
10171 /* This is trampoline. */
10172 loc += offset;
df58fc94
RS
10173 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10174 {
d21911ea
MR
10175 bfd_put_micromips_32 (hti->output_bfd,
10176 LA25_LUI_MICROMIPS (target_high), loc);
10177 bfd_put_micromips_32 (hti->output_bfd,
10178 LA25_J_MICROMIPS (target), loc + 4);
10179 bfd_put_micromips_32 (hti->output_bfd,
10180 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
df58fc94
RS
10181 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10182 }
10183 else
10184 {
10185 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10186 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10187 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10188 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10189 }
861fb55a
DJ
10190 }
10191 return TRUE;
10192}
10193
b49e97c9
TS
10194/* If NAME is one of the special IRIX6 symbols defined by the linker,
10195 adjust it appropriately now. */
10196
10197static void
9719ad41
RS
10198mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10199 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
10200{
10201 /* The linker script takes care of providing names and values for
10202 these, but we must place them into the right sections. */
10203 static const char* const text_section_symbols[] = {
10204 "_ftext",
10205 "_etext",
10206 "__dso_displacement",
10207 "__elf_header",
10208 "__program_header_table",
10209 NULL
10210 };
10211
10212 static const char* const data_section_symbols[] = {
10213 "_fdata",
10214 "_edata",
10215 "_end",
10216 "_fbss",
10217 NULL
10218 };
10219
10220 const char* const *p;
10221 int i;
10222
10223 for (i = 0; i < 2; ++i)
10224 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10225 *p;
10226 ++p)
10227 if (strcmp (*p, name) == 0)
10228 {
10229 /* All of these symbols are given type STT_SECTION by the
10230 IRIX6 linker. */
10231 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 10232 sym->st_other = STO_PROTECTED;
b49e97c9
TS
10233
10234 /* The IRIX linker puts these symbols in special sections. */
10235 if (i == 0)
10236 sym->st_shndx = SHN_MIPS_TEXT;
10237 else
10238 sym->st_shndx = SHN_MIPS_DATA;
10239
10240 break;
10241 }
10242}
10243
10244/* Finish up dynamic symbol handling. We set the contents of various
10245 dynamic sections here. */
10246
b34976b6 10247bfd_boolean
9719ad41
RS
10248_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10249 struct bfd_link_info *info,
10250 struct elf_link_hash_entry *h,
10251 Elf_Internal_Sym *sym)
b49e97c9
TS
10252{
10253 bfd *dynobj;
b49e97c9 10254 asection *sgot;
f4416af6 10255 struct mips_got_info *g, *gg;
b49e97c9 10256 const char *name;
3d6746ca 10257 int idx;
5108fc1b 10258 struct mips_elf_link_hash_table *htab;
738e5348 10259 struct mips_elf_link_hash_entry *hmips;
b49e97c9 10260
5108fc1b 10261 htab = mips_elf_hash_table (info);
4dfe6ac6 10262 BFD_ASSERT (htab != NULL);
b49e97c9 10263 dynobj = elf_hash_table (info)->dynobj;
738e5348 10264 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 10265
861fb55a
DJ
10266 BFD_ASSERT (!htab->is_vxworks);
10267
1bbce132
MR
10268 if (h->plt.plist != NULL
10269 && (h->plt.plist->mips_offset != MINUS_ONE
10270 || h->plt.plist->comp_offset != MINUS_ONE))
861fb55a
DJ
10271 {
10272 /* We've decided to create a PLT entry for this symbol. */
10273 bfd_byte *loc;
1bbce132 10274 bfd_vma header_address, got_address;
861fb55a 10275 bfd_vma got_address_high, got_address_low, load;
1bbce132
MR
10276 bfd_vma got_index;
10277 bfd_vma isa_bit;
10278
10279 got_index = h->plt.plist->gotplt_index;
861fb55a
DJ
10280
10281 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10282 BFD_ASSERT (h->dynindx != -1);
10283 BFD_ASSERT (htab->splt != NULL);
1bbce132 10284 BFD_ASSERT (got_index != MINUS_ONE);
861fb55a
DJ
10285 BFD_ASSERT (!h->def_regular);
10286
10287 /* Calculate the address of the PLT header. */
1bbce132 10288 isa_bit = htab->plt_header_is_comp;
861fb55a 10289 header_address = (htab->splt->output_section->vma
1bbce132 10290 + htab->splt->output_offset + isa_bit);
861fb55a
DJ
10291
10292 /* Calculate the address of the .got.plt entry. */
10293 got_address = (htab->sgotplt->output_section->vma
10294 + htab->sgotplt->output_offset
1bbce132
MR
10295 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10296
861fb55a
DJ
10297 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10298 got_address_low = got_address & 0xffff;
10299
10300 /* Initially point the .got.plt entry at the PLT header. */
1bbce132 10301 loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
861fb55a
DJ
10302 if (ABI_64_P (output_bfd))
10303 bfd_put_64 (output_bfd, header_address, loc);
10304 else
10305 bfd_put_32 (output_bfd, header_address, loc);
10306
1bbce132
MR
10307 /* Now handle the PLT itself. First the standard entry (the order
10308 does not matter, we just have to pick one). */
10309 if (h->plt.plist->mips_offset != MINUS_ONE)
10310 {
10311 const bfd_vma *plt_entry;
10312 bfd_vma plt_offset;
861fb55a 10313
1bbce132 10314 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
861fb55a 10315
1bbce132 10316 BFD_ASSERT (plt_offset <= htab->splt->size);
6d30f5b2 10317
1bbce132
MR
10318 /* Find out where the .plt entry should go. */
10319 loc = htab->splt->contents + plt_offset;
10320
10321 /* Pick the load opcode. */
10322 load = MIPS_ELF_LOAD_WORD (output_bfd);
10323
10324 /* Fill in the PLT entry itself. */
10325 plt_entry = mips_exec_plt_entry;
10326 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10327 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10328 loc + 4);
10329
10330 if (! LOAD_INTERLOCKS_P (output_bfd))
10331 {
10332 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10333 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10334 }
10335 else
10336 {
10337 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10338 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10339 loc + 12);
10340 }
6d30f5b2 10341 }
1bbce132
MR
10342
10343 /* Now the compressed entry. They come after any standard ones. */
10344 if (h->plt.plist->comp_offset != MINUS_ONE)
6d30f5b2 10345 {
1bbce132
MR
10346 bfd_vma plt_offset;
10347
10348 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10349 + h->plt.plist->comp_offset);
10350
10351 BFD_ASSERT (plt_offset <= htab->splt->size);
10352
10353 /* Find out where the .plt entry should go. */
10354 loc = htab->splt->contents + plt_offset;
10355
10356 /* Fill in the PLT entry itself. */
833794fc
MR
10357 if (!MICROMIPS_P (output_bfd))
10358 {
10359 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10360
10361 bfd_put_16 (output_bfd, plt_entry[0], loc);
10362 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10363 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10364 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10365 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10366 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10367 bfd_put_32 (output_bfd, got_address, loc + 12);
10368 }
10369 else if (htab->insn32)
10370 {
10371 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10372
10373 bfd_put_16 (output_bfd, plt_entry[0], loc);
10374 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10375 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10376 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10377 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10378 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10379 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10380 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10381 }
10382 else
1bbce132
MR
10383 {
10384 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10385 bfd_signed_vma gotpc_offset;
10386 bfd_vma loc_address;
10387
10388 BFD_ASSERT (got_address % 4 == 0);
10389
10390 loc_address = (htab->splt->output_section->vma
10391 + htab->splt->output_offset + plt_offset);
10392 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10393
10394 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10395 if (gotpc_offset + 0x1000000 >= 0x2000000)
10396 {
10397 (*_bfd_error_handler)
10398 (_("%B: `%A' offset of %ld from `%A' "
10399 "beyond the range of ADDIUPC"),
10400 output_bfd,
10401 htab->sgotplt->output_section,
10402 htab->splt->output_section,
10403 (long) gotpc_offset);
10404 bfd_set_error (bfd_error_no_error);
10405 return FALSE;
10406 }
10407 bfd_put_16 (output_bfd,
10408 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10409 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10410 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10411 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10412 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10413 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10414 }
6d30f5b2 10415 }
861fb55a
DJ
10416
10417 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10418 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
1bbce132 10419 got_index - 2, h->dynindx,
861fb55a
DJ
10420 R_MIPS_JUMP_SLOT, got_address);
10421
10422 /* We distinguish between PLT entries and lazy-binding stubs by
10423 giving the former an st_other value of STO_MIPS_PLT. Set the
10424 flag and leave the value if there are any relocations in the
10425 binary where pointer equality matters. */
10426 sym->st_shndx = SHN_UNDEF;
10427 if (h->pointer_equality_needed)
1bbce132 10428 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
861fb55a 10429 else
1bbce132
MR
10430 {
10431 sym->st_value = 0;
10432 sym->st_other = 0;
10433 }
861fb55a 10434 }
1bbce132
MR
10435
10436 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
b49e97c9 10437 {
861fb55a 10438 /* We've decided to create a lazy-binding stub. */
1bbce132
MR
10439 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10440 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10441 bfd_vma stub_size = htab->function_stub_size;
5108fc1b 10442 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
1bbce132
MR
10443 bfd_vma isa_bit = micromips_p;
10444 bfd_vma stub_big_size;
10445
833794fc 10446 if (!micromips_p)
1bbce132 10447 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
833794fc
MR
10448 else if (htab->insn32)
10449 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10450 else
10451 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
b49e97c9
TS
10452
10453 /* This symbol has a stub. Set it up. */
10454
10455 BFD_ASSERT (h->dynindx != -1);
10456
1bbce132 10457 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
3d6746ca
DD
10458
10459 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
10460 sign extension at runtime in the stub, resulting in a negative
10461 index value. */
10462 if (h->dynindx & ~0x7fffffff)
b34976b6 10463 return FALSE;
b49e97c9
TS
10464
10465 /* Fill the stub. */
1bbce132
MR
10466 if (micromips_p)
10467 {
10468 idx = 0;
10469 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10470 stub + idx);
10471 idx += 4;
833794fc
MR
10472 if (htab->insn32)
10473 {
10474 bfd_put_micromips_32 (output_bfd,
10475 STUB_MOVE32_MICROMIPS (output_bfd),
10476 stub + idx);
10477 idx += 4;
10478 }
10479 else
10480 {
10481 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10482 idx += 2;
10483 }
1bbce132
MR
10484 if (stub_size == stub_big_size)
10485 {
10486 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10487
10488 bfd_put_micromips_32 (output_bfd,
10489 STUB_LUI_MICROMIPS (dynindx_hi),
10490 stub + idx);
10491 idx += 4;
10492 }
833794fc
MR
10493 if (htab->insn32)
10494 {
10495 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
10496 stub + idx);
10497 idx += 4;
10498 }
10499 else
10500 {
10501 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
10502 idx += 2;
10503 }
1bbce132
MR
10504
10505 /* If a large stub is not required and sign extension is not a
10506 problem, then use legacy code in the stub. */
10507 if (stub_size == stub_big_size)
10508 bfd_put_micromips_32 (output_bfd,
10509 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
10510 stub + idx);
10511 else if (h->dynindx & ~0x7fff)
10512 bfd_put_micromips_32 (output_bfd,
10513 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
10514 stub + idx);
10515 else
10516 bfd_put_micromips_32 (output_bfd,
10517 STUB_LI16S_MICROMIPS (output_bfd,
10518 h->dynindx),
10519 stub + idx);
10520 }
3d6746ca 10521 else
1bbce132
MR
10522 {
10523 idx = 0;
10524 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
10525 idx += 4;
10526 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
10527 idx += 4;
10528 if (stub_size == stub_big_size)
10529 {
10530 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
10531 stub + idx);
10532 idx += 4;
10533 }
10534 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
10535 idx += 4;
10536
10537 /* If a large stub is not required and sign extension is not a
10538 problem, then use legacy code in the stub. */
10539 if (stub_size == stub_big_size)
10540 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
10541 stub + idx);
10542 else if (h->dynindx & ~0x7fff)
10543 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
10544 stub + idx);
10545 else
10546 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
10547 stub + idx);
10548 }
5108fc1b 10549
1bbce132
MR
10550 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
10551 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
10552 stub, stub_size);
b49e97c9 10553
1bbce132 10554 /* Mark the symbol as undefined. stub_offset != -1 occurs
b49e97c9
TS
10555 only for the referenced symbol. */
10556 sym->st_shndx = SHN_UNDEF;
10557
10558 /* The run-time linker uses the st_value field of the symbol
10559 to reset the global offset table entry for this external
10560 to its stub address when unlinking a shared object. */
4e41d0d7
RS
10561 sym->st_value = (htab->sstubs->output_section->vma
10562 + htab->sstubs->output_offset
1bbce132
MR
10563 + h->plt.plist->stub_offset
10564 + isa_bit);
10565 sym->st_other = other;
b49e97c9
TS
10566 }
10567
738e5348
RS
10568 /* If we have a MIPS16 function with a stub, the dynamic symbol must
10569 refer to the stub, since only the stub uses the standard calling
10570 conventions. */
10571 if (h->dynindx != -1 && hmips->fn_stub != NULL)
10572 {
10573 BFD_ASSERT (hmips->need_fn_stub);
10574 sym->st_value = (hmips->fn_stub->output_section->vma
10575 + hmips->fn_stub->output_offset);
10576 sym->st_size = hmips->fn_stub->size;
10577 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10578 }
10579
b49e97c9 10580 BFD_ASSERT (h->dynindx != -1
f5385ebf 10581 || h->forced_local);
b49e97c9 10582
23cc69b6 10583 sgot = htab->sgot;
a8028dd0 10584 g = htab->got_info;
b49e97c9
TS
10585 BFD_ASSERT (g != NULL);
10586
10587 /* Run through the global symbol table, creating GOT entries for all
10588 the symbols that need them. */
020d7251 10589 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
10590 {
10591 bfd_vma offset;
10592 bfd_vma value;
10593
6eaa6adc 10594 value = sym->st_value;
13fbec83 10595 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
b49e97c9
TS
10596 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10597 }
10598
e641e783 10599 if (hmips->global_got_area != GGA_NONE && g->next)
f4416af6
AO
10600 {
10601 struct mips_got_entry e, *p;
0626d451 10602 bfd_vma entry;
f4416af6 10603 bfd_vma offset;
f4416af6
AO
10604
10605 gg = g;
10606
10607 e.abfd = output_bfd;
10608 e.symndx = -1;
738e5348 10609 e.d.h = hmips;
9ab066b4 10610 e.tls_type = GOT_TLS_NONE;
143d77c5 10611
f4416af6
AO
10612 for (g = g->next; g->next != gg; g = g->next)
10613 {
10614 if (g->got_entries
10615 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10616 &e)))
10617 {
10618 offset = p->gotidx;
6c42ddb9 10619 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
0626d451
RS
10620 if (info->shared
10621 || (elf_hash_table (info)->dynamic_sections_created
10622 && p->d.h != NULL
f5385ebf
AM
10623 && p->d.h->root.def_dynamic
10624 && !p->d.h->root.def_regular))
0626d451
RS
10625 {
10626 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10627 the various compatibility problems, it's easier to mock
10628 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10629 mips_elf_create_dynamic_relocation to calculate the
10630 appropriate addend. */
10631 Elf_Internal_Rela rel[3];
10632
10633 memset (rel, 0, sizeof (rel));
10634 if (ABI_64_P (output_bfd))
10635 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10636 else
10637 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10638 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10639
10640 entry = 0;
10641 if (! (mips_elf_create_dynamic_relocation
10642 (output_bfd, info, rel,
10643 e.d.h, NULL, sym->st_value, &entry, sgot)))
10644 return FALSE;
10645 }
10646 else
10647 entry = sym->st_value;
10648 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
10649 }
10650 }
10651 }
10652
b49e97c9
TS
10653 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10654 name = h->root.root.string;
9637f6ef 10655 if (h == elf_hash_table (info)->hdynamic
22edb2f1 10656 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
10657 sym->st_shndx = SHN_ABS;
10658 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10659 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10660 {
10661 sym->st_shndx = SHN_ABS;
10662 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10663 sym->st_value = 1;
10664 }
4a14403c 10665 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10666 {
10667 sym->st_shndx = SHN_ABS;
10668 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10669 sym->st_value = elf_gp (output_bfd);
10670 }
10671 else if (SGI_COMPAT (output_bfd))
10672 {
10673 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10674 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10675 {
10676 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10677 sym->st_other = STO_PROTECTED;
10678 sym->st_value = 0;
10679 sym->st_shndx = SHN_MIPS_DATA;
10680 }
10681 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10682 {
10683 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10684 sym->st_other = STO_PROTECTED;
10685 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10686 sym->st_shndx = SHN_ABS;
10687 }
10688 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10689 {
10690 if (h->type == STT_FUNC)
10691 sym->st_shndx = SHN_MIPS_TEXT;
10692 else if (h->type == STT_OBJECT)
10693 sym->st_shndx = SHN_MIPS_DATA;
10694 }
10695 }
10696
861fb55a
DJ
10697 /* Emit a copy reloc, if needed. */
10698 if (h->needs_copy)
10699 {
10700 asection *s;
10701 bfd_vma symval;
10702
10703 BFD_ASSERT (h->dynindx != -1);
10704 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10705
10706 s = mips_elf_rel_dyn_section (info, FALSE);
10707 symval = (h->root.u.def.section->output_section->vma
10708 + h->root.u.def.section->output_offset
10709 + h->root.u.def.value);
10710 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10711 h->dynindx, R_MIPS_COPY, symval);
10712 }
10713
b49e97c9
TS
10714 /* Handle the IRIX6-specific symbols. */
10715 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10716 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10717
cbf8d970
MR
10718 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
10719 to treat compressed symbols like any other. */
30c09090 10720 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
10721 {
10722 BFD_ASSERT (sym->st_value & 1);
10723 sym->st_other -= STO_MIPS16;
10724 }
cbf8d970
MR
10725 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
10726 {
10727 BFD_ASSERT (sym->st_value & 1);
10728 sym->st_other -= STO_MICROMIPS;
10729 }
b49e97c9 10730
b34976b6 10731 return TRUE;
b49e97c9
TS
10732}
10733
0a44bf69
RS
10734/* Likewise, for VxWorks. */
10735
10736bfd_boolean
10737_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10738 struct bfd_link_info *info,
10739 struct elf_link_hash_entry *h,
10740 Elf_Internal_Sym *sym)
10741{
10742 bfd *dynobj;
10743 asection *sgot;
10744 struct mips_got_info *g;
10745 struct mips_elf_link_hash_table *htab;
020d7251 10746 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
10747
10748 htab = mips_elf_hash_table (info);
4dfe6ac6 10749 BFD_ASSERT (htab != NULL);
0a44bf69 10750 dynobj = elf_hash_table (info)->dynobj;
020d7251 10751 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69 10752
1bbce132 10753 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
0a44bf69 10754 {
6d79d2ed 10755 bfd_byte *loc;
1bbce132 10756 bfd_vma plt_address, got_address, got_offset, branch_offset;
0a44bf69
RS
10757 Elf_Internal_Rela rel;
10758 static const bfd_vma *plt_entry;
1bbce132
MR
10759 bfd_vma gotplt_index;
10760 bfd_vma plt_offset;
10761
10762 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10763 gotplt_index = h->plt.plist->gotplt_index;
0a44bf69
RS
10764
10765 BFD_ASSERT (h->dynindx != -1);
10766 BFD_ASSERT (htab->splt != NULL);
1bbce132
MR
10767 BFD_ASSERT (gotplt_index != MINUS_ONE);
10768 BFD_ASSERT (plt_offset <= htab->splt->size);
0a44bf69
RS
10769
10770 /* Calculate the address of the .plt entry. */
10771 plt_address = (htab->splt->output_section->vma
10772 + htab->splt->output_offset
1bbce132 10773 + plt_offset);
0a44bf69
RS
10774
10775 /* Calculate the address of the .got.plt entry. */
10776 got_address = (htab->sgotplt->output_section->vma
10777 + htab->sgotplt->output_offset
1bbce132 10778 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
0a44bf69
RS
10779
10780 /* Calculate the offset of the .got.plt entry from
10781 _GLOBAL_OFFSET_TABLE_. */
10782 got_offset = mips_elf_gotplt_index (info, h);
10783
10784 /* Calculate the offset for the branch at the start of the PLT
10785 entry. The branch jumps to the beginning of .plt. */
1bbce132 10786 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
0a44bf69
RS
10787
10788 /* Fill in the initial value of the .got.plt entry. */
10789 bfd_put_32 (output_bfd, plt_address,
1bbce132
MR
10790 (htab->sgotplt->contents
10791 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
0a44bf69
RS
10792
10793 /* Find out where the .plt entry should go. */
1bbce132 10794 loc = htab->splt->contents + plt_offset;
0a44bf69
RS
10795
10796 if (info->shared)
10797 {
10798 plt_entry = mips_vxworks_shared_plt_entry;
10799 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 10800 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
10801 }
10802 else
10803 {
10804 bfd_vma got_address_high, got_address_low;
10805
10806 plt_entry = mips_vxworks_exec_plt_entry;
10807 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10808 got_address_low = got_address & 0xffff;
10809
10810 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 10811 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
10812 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10813 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10814 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10815 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10816 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10817 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10818
10819 loc = (htab->srelplt2->contents
1bbce132 10820 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
0a44bf69
RS
10821
10822 /* Emit a relocation for the .got.plt entry. */
10823 rel.r_offset = got_address;
10824 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
1bbce132 10825 rel.r_addend = plt_offset;
0a44bf69
RS
10826 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10827
10828 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10829 loc += sizeof (Elf32_External_Rela);
10830 rel.r_offset = plt_address + 8;
10831 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10832 rel.r_addend = got_offset;
10833 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10834
10835 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10836 loc += sizeof (Elf32_External_Rela);
10837 rel.r_offset += 4;
10838 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10839 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10840 }
10841
10842 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
1bbce132
MR
10843 loc = (htab->srelplt->contents
10844 + gotplt_index * sizeof (Elf32_External_Rela));
0a44bf69
RS
10845 rel.r_offset = got_address;
10846 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10847 rel.r_addend = 0;
10848 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10849
10850 if (!h->def_regular)
10851 sym->st_shndx = SHN_UNDEF;
10852 }
10853
10854 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10855
23cc69b6 10856 sgot = htab->sgot;
a8028dd0 10857 g = htab->got_info;
0a44bf69
RS
10858 BFD_ASSERT (g != NULL);
10859
10860 /* See if this symbol has an entry in the GOT. */
020d7251 10861 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
10862 {
10863 bfd_vma offset;
10864 Elf_Internal_Rela outrel;
10865 bfd_byte *loc;
10866 asection *s;
10867
10868 /* Install the symbol value in the GOT. */
13fbec83 10869 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
0a44bf69
RS
10870 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10871
10872 /* Add a dynamic relocation for it. */
10873 s = mips_elf_rel_dyn_section (info, FALSE);
10874 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10875 outrel.r_offset = (sgot->output_section->vma
10876 + sgot->output_offset
10877 + offset);
10878 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10879 outrel.r_addend = 0;
10880 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10881 }
10882
10883 /* Emit a copy reloc, if needed. */
10884 if (h->needs_copy)
10885 {
10886 Elf_Internal_Rela rel;
10887
10888 BFD_ASSERT (h->dynindx != -1);
10889
10890 rel.r_offset = (h->root.u.def.section->output_section->vma
10891 + h->root.u.def.section->output_offset
10892 + h->root.u.def.value);
10893 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10894 rel.r_addend = 0;
10895 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10896 htab->srelbss->contents
10897 + (htab->srelbss->reloc_count
10898 * sizeof (Elf32_External_Rela)));
10899 ++htab->srelbss->reloc_count;
10900 }
10901
df58fc94
RS
10902 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10903 if (ELF_ST_IS_COMPRESSED (sym->st_other))
0a44bf69
RS
10904 sym->st_value &= ~1;
10905
10906 return TRUE;
10907}
10908
861fb55a
DJ
10909/* Write out a plt0 entry to the beginning of .plt. */
10910
1bbce132 10911static bfd_boolean
861fb55a
DJ
10912mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10913{
10914 bfd_byte *loc;
10915 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10916 static const bfd_vma *plt_entry;
10917 struct mips_elf_link_hash_table *htab;
10918
10919 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10920 BFD_ASSERT (htab != NULL);
10921
861fb55a
DJ
10922 if (ABI_64_P (output_bfd))
10923 plt_entry = mips_n64_exec_plt0_entry;
10924 else if (ABI_N32_P (output_bfd))
10925 plt_entry = mips_n32_exec_plt0_entry;
833794fc 10926 else if (!htab->plt_header_is_comp)
861fb55a 10927 plt_entry = mips_o32_exec_plt0_entry;
833794fc
MR
10928 else if (htab->insn32)
10929 plt_entry = micromips_insn32_o32_exec_plt0_entry;
10930 else
10931 plt_entry = micromips_o32_exec_plt0_entry;
861fb55a
DJ
10932
10933 /* Calculate the value of .got.plt. */
10934 gotplt_value = (htab->sgotplt->output_section->vma
10935 + htab->sgotplt->output_offset);
10936 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10937 gotplt_value_low = gotplt_value & 0xffff;
10938
10939 /* The PLT sequence is not safe for N64 if .got.plt's address can
10940 not be loaded in two instructions. */
10941 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10942 || ~(gotplt_value | 0x7fffffff) == 0);
10943
10944 /* Install the PLT header. */
10945 loc = htab->splt->contents;
1bbce132
MR
10946 if (plt_entry == micromips_o32_exec_plt0_entry)
10947 {
10948 bfd_vma gotpc_offset;
10949 bfd_vma loc_address;
10950 size_t i;
10951
10952 BFD_ASSERT (gotplt_value % 4 == 0);
10953
10954 loc_address = (htab->splt->output_section->vma
10955 + htab->splt->output_offset);
10956 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
10957
10958 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10959 if (gotpc_offset + 0x1000000 >= 0x2000000)
10960 {
10961 (*_bfd_error_handler)
10962 (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
10963 output_bfd,
10964 htab->sgotplt->output_section,
10965 htab->splt->output_section,
10966 (long) gotpc_offset);
10967 bfd_set_error (bfd_error_no_error);
10968 return FALSE;
10969 }
10970 bfd_put_16 (output_bfd,
10971 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10972 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10973 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
10974 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10975 }
833794fc
MR
10976 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
10977 {
10978 size_t i;
10979
10980 bfd_put_16 (output_bfd, plt_entry[0], loc);
10981 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
10982 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10983 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
10984 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10985 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
10986 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
10987 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10988 }
1bbce132
MR
10989 else
10990 {
10991 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
10992 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
10993 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
10994 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10995 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10996 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10997 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10998 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10999 }
11000
11001 return TRUE;
861fb55a
DJ
11002}
11003
0a44bf69
RS
11004/* Install the PLT header for a VxWorks executable and finalize the
11005 contents of .rela.plt.unloaded. */
11006
11007static void
11008mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11009{
11010 Elf_Internal_Rela rela;
11011 bfd_byte *loc;
11012 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11013 static const bfd_vma *plt_entry;
11014 struct mips_elf_link_hash_table *htab;
11015
11016 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11017 BFD_ASSERT (htab != NULL);
11018
0a44bf69
RS
11019 plt_entry = mips_vxworks_exec_plt0_entry;
11020
11021 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11022 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11023 + htab->root.hgot->root.u.def.section->output_offset
11024 + htab->root.hgot->root.u.def.value);
11025
11026 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11027 got_value_low = got_value & 0xffff;
11028
11029 /* Calculate the address of the PLT header. */
11030 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
11031
11032 /* Install the PLT header. */
11033 loc = htab->splt->contents;
11034 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11035 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11036 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11037 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11038 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11039 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11040
11041 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11042 loc = htab->srelplt2->contents;
11043 rela.r_offset = plt_address;
11044 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11045 rela.r_addend = 0;
11046 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11047 loc += sizeof (Elf32_External_Rela);
11048
11049 /* Output the relocation for the following addiu of
11050 %lo(_GLOBAL_OFFSET_TABLE_). */
11051 rela.r_offset += 4;
11052 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11053 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11054 loc += sizeof (Elf32_External_Rela);
11055
11056 /* Fix up the remaining relocations. They may have the wrong
11057 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11058 in which symbols were output. */
11059 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11060 {
11061 Elf_Internal_Rela rel;
11062
11063 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11064 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11065 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11066 loc += sizeof (Elf32_External_Rela);
11067
11068 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11069 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11070 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11071 loc += sizeof (Elf32_External_Rela);
11072
11073 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11074 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11075 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11076 loc += sizeof (Elf32_External_Rela);
11077 }
11078}
11079
11080/* Install the PLT header for a VxWorks shared library. */
11081
11082static void
11083mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11084{
11085 unsigned int i;
11086 struct mips_elf_link_hash_table *htab;
11087
11088 htab = mips_elf_hash_table (info);
4dfe6ac6 11089 BFD_ASSERT (htab != NULL);
0a44bf69
RS
11090
11091 /* We just need to copy the entry byte-by-byte. */
11092 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11093 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11094 htab->splt->contents + i * 4);
11095}
11096
b49e97c9
TS
11097/* Finish up the dynamic sections. */
11098
b34976b6 11099bfd_boolean
9719ad41
RS
11100_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11101 struct bfd_link_info *info)
b49e97c9
TS
11102{
11103 bfd *dynobj;
11104 asection *sdyn;
11105 asection *sgot;
f4416af6 11106 struct mips_got_info *gg, *g;
0a44bf69 11107 struct mips_elf_link_hash_table *htab;
b49e97c9 11108
0a44bf69 11109 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11110 BFD_ASSERT (htab != NULL);
11111
b49e97c9
TS
11112 dynobj = elf_hash_table (info)->dynobj;
11113
3d4d4302 11114 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
b49e97c9 11115
23cc69b6
RS
11116 sgot = htab->sgot;
11117 gg = htab->got_info;
b49e97c9
TS
11118
11119 if (elf_hash_table (info)->dynamic_sections_created)
11120 {
11121 bfd_byte *b;
943284cc 11122 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
11123
11124 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
11125 BFD_ASSERT (gg != NULL);
11126
d7206569 11127 g = mips_elf_bfd_got (output_bfd, FALSE);
b49e97c9
TS
11128 BFD_ASSERT (g != NULL);
11129
11130 for (b = sdyn->contents;
eea6121a 11131 b < sdyn->contents + sdyn->size;
b49e97c9
TS
11132 b += MIPS_ELF_DYN_SIZE (dynobj))
11133 {
11134 Elf_Internal_Dyn dyn;
11135 const char *name;
11136 size_t elemsize;
11137 asection *s;
b34976b6 11138 bfd_boolean swap_out_p;
b49e97c9
TS
11139
11140 /* Read in the current dynamic entry. */
11141 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11142
11143 /* Assume that we're going to modify it and write it out. */
b34976b6 11144 swap_out_p = TRUE;
b49e97c9
TS
11145
11146 switch (dyn.d_tag)
11147 {
11148 case DT_RELENT:
b49e97c9
TS
11149 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11150 break;
11151
0a44bf69
RS
11152 case DT_RELAENT:
11153 BFD_ASSERT (htab->is_vxworks);
11154 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11155 break;
11156
b49e97c9
TS
11157 case DT_STRSZ:
11158 /* Rewrite DT_STRSZ. */
11159 dyn.d_un.d_val =
11160 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11161 break;
11162
11163 case DT_PLTGOT:
861fb55a
DJ
11164 s = htab->sgot;
11165 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11166 break;
11167
11168 case DT_MIPS_PLTGOT:
11169 s = htab->sgotplt;
11170 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
11171 break;
11172
11173 case DT_MIPS_RLD_VERSION:
11174 dyn.d_un.d_val = 1; /* XXX */
11175 break;
11176
11177 case DT_MIPS_FLAGS:
11178 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11179 break;
11180
b49e97c9 11181 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
11182 {
11183 time_t t;
11184 time (&t);
11185 dyn.d_un.d_val = t;
11186 }
b49e97c9
TS
11187 break;
11188
11189 case DT_MIPS_ICHECKSUM:
11190 /* XXX FIXME: */
b34976b6 11191 swap_out_p = FALSE;
b49e97c9
TS
11192 break;
11193
11194 case DT_MIPS_IVERSION:
11195 /* XXX FIXME: */
b34976b6 11196 swap_out_p = FALSE;
b49e97c9
TS
11197 break;
11198
11199 case DT_MIPS_BASE_ADDRESS:
11200 s = output_bfd->sections;
11201 BFD_ASSERT (s != NULL);
11202 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11203 break;
11204
11205 case DT_MIPS_LOCAL_GOTNO:
11206 dyn.d_un.d_val = g->local_gotno;
11207 break;
11208
11209 case DT_MIPS_UNREFEXTNO:
11210 /* The index into the dynamic symbol table which is the
11211 entry of the first external symbol that is not
11212 referenced within the same object. */
11213 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11214 break;
11215
11216 case DT_MIPS_GOTSYM:
d222d210 11217 if (htab->global_gotsym)
b49e97c9 11218 {
d222d210 11219 dyn.d_un.d_val = htab->global_gotsym->dynindx;
b49e97c9
TS
11220 break;
11221 }
11222 /* In case if we don't have global got symbols we default
11223 to setting DT_MIPS_GOTSYM to the same value as
11224 DT_MIPS_SYMTABNO, so we just fall through. */
11225
11226 case DT_MIPS_SYMTABNO:
11227 name = ".dynsym";
11228 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11229 s = bfd_get_section_by_name (output_bfd, name);
11230 BFD_ASSERT (s != NULL);
11231
eea6121a 11232 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
11233 break;
11234
11235 case DT_MIPS_HIPAGENO:
861fb55a 11236 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
11237 break;
11238
11239 case DT_MIPS_RLD_MAP:
b4082c70
DD
11240 {
11241 struct elf_link_hash_entry *h;
11242 h = mips_elf_hash_table (info)->rld_symbol;
11243 if (!h)
11244 {
11245 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11246 swap_out_p = FALSE;
11247 break;
11248 }
11249 s = h->root.u.def.section;
11250 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11251 + h->root.u.def.value);
11252 }
b49e97c9
TS
11253 break;
11254
11255 case DT_MIPS_OPTIONS:
11256 s = (bfd_get_section_by_name
11257 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11258 dyn.d_un.d_ptr = s->vma;
11259 break;
11260
0a44bf69
RS
11261 case DT_RELASZ:
11262 BFD_ASSERT (htab->is_vxworks);
11263 /* The count does not include the JUMP_SLOT relocations. */
11264 if (htab->srelplt)
11265 dyn.d_un.d_val -= htab->srelplt->size;
11266 break;
11267
11268 case DT_PLTREL:
861fb55a
DJ
11269 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11270 if (htab->is_vxworks)
11271 dyn.d_un.d_val = DT_RELA;
11272 else
11273 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
11274 break;
11275
11276 case DT_PLTRELSZ:
861fb55a 11277 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
11278 dyn.d_un.d_val = htab->srelplt->size;
11279 break;
11280
11281 case DT_JMPREL:
861fb55a
DJ
11282 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11283 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
11284 + htab->srelplt->output_offset);
11285 break;
11286
943284cc
DJ
11287 case DT_TEXTREL:
11288 /* If we didn't need any text relocations after all, delete
11289 the dynamic tag. */
11290 if (!(info->flags & DF_TEXTREL))
11291 {
11292 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11293 swap_out_p = FALSE;
11294 }
11295 break;
11296
11297 case DT_FLAGS:
11298 /* If we didn't need any text relocations after all, clear
11299 DF_TEXTREL from DT_FLAGS. */
11300 if (!(info->flags & DF_TEXTREL))
11301 dyn.d_un.d_val &= ~DF_TEXTREL;
11302 else
11303 swap_out_p = FALSE;
11304 break;
11305
b49e97c9 11306 default:
b34976b6 11307 swap_out_p = FALSE;
7a2b07ff
NS
11308 if (htab->is_vxworks
11309 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11310 swap_out_p = TRUE;
b49e97c9
TS
11311 break;
11312 }
11313
943284cc 11314 if (swap_out_p || dyn_skipped)
b49e97c9 11315 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
11316 (dynobj, &dyn, b - dyn_skipped);
11317
11318 if (dyn_to_skip)
11319 {
11320 dyn_skipped += dyn_to_skip;
11321 dyn_to_skip = 0;
11322 }
b49e97c9 11323 }
943284cc
DJ
11324
11325 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11326 if (dyn_skipped > 0)
11327 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
11328 }
11329
b55fd4d4
DJ
11330 if (sgot != NULL && sgot->size > 0
11331 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 11332 {
0a44bf69
RS
11333 if (htab->is_vxworks)
11334 {
11335 /* The first entry of the global offset table points to the
11336 ".dynamic" section. The second is initialized by the
11337 loader and contains the shared library identifier.
11338 The third is also initialized by the loader and points
11339 to the lazy resolution stub. */
11340 MIPS_ELF_PUT_WORD (output_bfd,
11341 sdyn->output_offset + sdyn->output_section->vma,
11342 sgot->contents);
11343 MIPS_ELF_PUT_WORD (output_bfd, 0,
11344 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11345 MIPS_ELF_PUT_WORD (output_bfd, 0,
11346 sgot->contents
11347 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11348 }
11349 else
11350 {
11351 /* The first entry of the global offset table will be filled at
11352 runtime. The second entry will be used by some runtime loaders.
11353 This isn't the case of IRIX rld. */
11354 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 11355 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
11356 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11357 }
b49e97c9 11358
54938e2a
TS
11359 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11360 = MIPS_ELF_GOT_SIZE (output_bfd);
11361 }
b49e97c9 11362
f4416af6
AO
11363 /* Generate dynamic relocations for the non-primary gots. */
11364 if (gg != NULL && gg->next)
11365 {
11366 Elf_Internal_Rela rel[3];
11367 bfd_vma addend = 0;
11368
11369 memset (rel, 0, sizeof (rel));
11370 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11371
11372 for (g = gg->next; g->next != gg; g = g->next)
11373 {
91d6fa6a 11374 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 11375 + g->next->tls_gotno;
f4416af6 11376
9719ad41 11377 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 11378 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
11379 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11380 sgot->contents
91d6fa6a 11381 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
11382
11383 if (! info->shared)
11384 continue;
11385
91d6fa6a 11386 while (got_index < g->assigned_gotno)
f4416af6
AO
11387 {
11388 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
91d6fa6a 11389 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
11390 if (!(mips_elf_create_dynamic_relocation
11391 (output_bfd, info, rel, NULL,
11392 bfd_abs_section_ptr,
11393 0, &addend, sgot)))
11394 return FALSE;
11395 BFD_ASSERT (addend == 0);
11396 }
11397 }
11398 }
11399
3133ddbf
DJ
11400 /* The generation of dynamic relocations for the non-primary gots
11401 adds more dynamic relocations. We cannot count them until
11402 here. */
11403
11404 if (elf_hash_table (info)->dynamic_sections_created)
11405 {
11406 bfd_byte *b;
11407 bfd_boolean swap_out_p;
11408
11409 BFD_ASSERT (sdyn != NULL);
11410
11411 for (b = sdyn->contents;
11412 b < sdyn->contents + sdyn->size;
11413 b += MIPS_ELF_DYN_SIZE (dynobj))
11414 {
11415 Elf_Internal_Dyn dyn;
11416 asection *s;
11417
11418 /* Read in the current dynamic entry. */
11419 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11420
11421 /* Assume that we're going to modify it and write it out. */
11422 swap_out_p = TRUE;
11423
11424 switch (dyn.d_tag)
11425 {
11426 case DT_RELSZ:
11427 /* Reduce DT_RELSZ to account for any relocations we
11428 decided not to make. This is for the n64 irix rld,
11429 which doesn't seem to apply any relocations if there
11430 are trailing null entries. */
0a44bf69 11431 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
11432 dyn.d_un.d_val = (s->reloc_count
11433 * (ABI_64_P (output_bfd)
11434 ? sizeof (Elf64_Mips_External_Rel)
11435 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
11436 /* Adjust the section size too. Tools like the prelinker
11437 can reasonably expect the values to the same. */
11438 elf_section_data (s->output_section)->this_hdr.sh_size
11439 = dyn.d_un.d_val;
3133ddbf
DJ
11440 break;
11441
11442 default:
11443 swap_out_p = FALSE;
11444 break;
11445 }
11446
11447 if (swap_out_p)
11448 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11449 (dynobj, &dyn, b);
11450 }
11451 }
11452
b49e97c9 11453 {
b49e97c9
TS
11454 asection *s;
11455 Elf32_compact_rel cpt;
11456
b49e97c9
TS
11457 if (SGI_COMPAT (output_bfd))
11458 {
11459 /* Write .compact_rel section out. */
3d4d4302 11460 s = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
11461 if (s != NULL)
11462 {
11463 cpt.id1 = 1;
11464 cpt.num = s->reloc_count;
11465 cpt.id2 = 2;
11466 cpt.offset = (s->output_section->filepos
11467 + sizeof (Elf32_External_compact_rel));
11468 cpt.reserved0 = 0;
11469 cpt.reserved1 = 0;
11470 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
11471 ((Elf32_External_compact_rel *)
11472 s->contents));
11473
11474 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 11475 if (htab->sstubs != NULL)
b49e97c9
TS
11476 {
11477 file_ptr dummy_offset;
11478
4e41d0d7
RS
11479 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
11480 dummy_offset = htab->sstubs->size - htab->function_stub_size;
11481 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 11482 htab->function_stub_size);
b49e97c9
TS
11483 }
11484 }
11485 }
11486
0a44bf69
RS
11487 /* The psABI says that the dynamic relocations must be sorted in
11488 increasing order of r_symndx. The VxWorks EABI doesn't require
11489 this, and because the code below handles REL rather than RELA
11490 relocations, using it for VxWorks would be outright harmful. */
11491 if (!htab->is_vxworks)
b49e97c9 11492 {
0a44bf69
RS
11493 s = mips_elf_rel_dyn_section (info, FALSE);
11494 if (s != NULL
11495 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
11496 {
11497 reldyn_sorting_bfd = output_bfd;
b49e97c9 11498
0a44bf69
RS
11499 if (ABI_64_P (output_bfd))
11500 qsort ((Elf64_External_Rel *) s->contents + 1,
11501 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
11502 sort_dynamic_relocs_64);
11503 else
11504 qsort ((Elf32_External_Rel *) s->contents + 1,
11505 s->reloc_count - 1, sizeof (Elf32_External_Rel),
11506 sort_dynamic_relocs);
11507 }
b49e97c9 11508 }
b49e97c9
TS
11509 }
11510
861fb55a 11511 if (htab->splt && htab->splt->size > 0)
0a44bf69 11512 {
861fb55a
DJ
11513 if (htab->is_vxworks)
11514 {
11515 if (info->shared)
11516 mips_vxworks_finish_shared_plt (output_bfd, info);
11517 else
11518 mips_vxworks_finish_exec_plt (output_bfd, info);
11519 }
0a44bf69 11520 else
861fb55a
DJ
11521 {
11522 BFD_ASSERT (!info->shared);
1bbce132
MR
11523 if (!mips_finish_exec_plt (output_bfd, info))
11524 return FALSE;
861fb55a 11525 }
0a44bf69 11526 }
b34976b6 11527 return TRUE;
b49e97c9
TS
11528}
11529
b49e97c9 11530
64543e1a
RS
11531/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
11532
11533static void
9719ad41 11534mips_set_isa_flags (bfd *abfd)
b49e97c9 11535{
64543e1a 11536 flagword val;
b49e97c9
TS
11537
11538 switch (bfd_get_mach (abfd))
11539 {
11540 default:
11541 case bfd_mach_mips3000:
11542 val = E_MIPS_ARCH_1;
11543 break;
11544
11545 case bfd_mach_mips3900:
11546 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
11547 break;
11548
11549 case bfd_mach_mips6000:
11550 val = E_MIPS_ARCH_2;
11551 break;
11552
11553 case bfd_mach_mips4000:
11554 case bfd_mach_mips4300:
11555 case bfd_mach_mips4400:
11556 case bfd_mach_mips4600:
11557 val = E_MIPS_ARCH_3;
11558 break;
11559
11560 case bfd_mach_mips4010:
11561 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
11562 break;
11563
11564 case bfd_mach_mips4100:
11565 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
11566 break;
11567
11568 case bfd_mach_mips4111:
11569 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
11570 break;
11571
00707a0e
RS
11572 case bfd_mach_mips4120:
11573 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
11574 break;
11575
b49e97c9
TS
11576 case bfd_mach_mips4650:
11577 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
11578 break;
11579
00707a0e
RS
11580 case bfd_mach_mips5400:
11581 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
11582 break;
11583
11584 case bfd_mach_mips5500:
11585 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
11586 break;
11587
e407c74b
NC
11588 case bfd_mach_mips5900:
11589 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
11590 break;
11591
0d2e43ed
ILT
11592 case bfd_mach_mips9000:
11593 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
11594 break;
11595
b49e97c9 11596 case bfd_mach_mips5000:
5a7ea749 11597 case bfd_mach_mips7000:
b49e97c9
TS
11598 case bfd_mach_mips8000:
11599 case bfd_mach_mips10000:
11600 case bfd_mach_mips12000:
3aa3176b
TS
11601 case bfd_mach_mips14000:
11602 case bfd_mach_mips16000:
b49e97c9
TS
11603 val = E_MIPS_ARCH_4;
11604 break;
11605
11606 case bfd_mach_mips5:
11607 val = E_MIPS_ARCH_5;
11608 break;
11609
350cc38d
MS
11610 case bfd_mach_mips_loongson_2e:
11611 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
11612 break;
11613
11614 case bfd_mach_mips_loongson_2f:
11615 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
11616 break;
11617
b49e97c9
TS
11618 case bfd_mach_mips_sb1:
11619 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
11620 break;
11621
d051516a
NC
11622 case bfd_mach_mips_loongson_3a:
11623 val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
11624 break;
11625
6f179bd0 11626 case bfd_mach_mips_octeon:
dd6a37e7 11627 case bfd_mach_mips_octeonp:
6f179bd0
AN
11628 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
11629 break;
11630
52b6b6b9
JM
11631 case bfd_mach_mips_xlr:
11632 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
11633 break;
11634
432233b3
AP
11635 case bfd_mach_mips_octeon2:
11636 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
11637 break;
11638
b49e97c9
TS
11639 case bfd_mach_mipsisa32:
11640 val = E_MIPS_ARCH_32;
11641 break;
11642
11643 case bfd_mach_mipsisa64:
11644 val = E_MIPS_ARCH_64;
af7ee8bf
CD
11645 break;
11646
11647 case bfd_mach_mipsisa32r2:
11648 val = E_MIPS_ARCH_32R2;
11649 break;
5f74bc13
CD
11650
11651 case bfd_mach_mipsisa64r2:
11652 val = E_MIPS_ARCH_64R2;
11653 break;
b49e97c9 11654 }
b49e97c9
TS
11655 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11656 elf_elfheader (abfd)->e_flags |= val;
11657
64543e1a
RS
11658}
11659
11660
11661/* The final processing done just before writing out a MIPS ELF object
11662 file. This gets the MIPS architecture right based on the machine
11663 number. This is used by both the 32-bit and the 64-bit ABI. */
11664
11665void
9719ad41
RS
11666_bfd_mips_elf_final_write_processing (bfd *abfd,
11667 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
11668{
11669 unsigned int i;
11670 Elf_Internal_Shdr **hdrpp;
11671 const char *name;
11672 asection *sec;
11673
11674 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11675 is nonzero. This is for compatibility with old objects, which used
11676 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11677 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11678 mips_set_isa_flags (abfd);
11679
b49e97c9
TS
11680 /* Set the sh_info field for .gptab sections and other appropriate
11681 info for each special section. */
11682 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11683 i < elf_numsections (abfd);
11684 i++, hdrpp++)
11685 {
11686 switch ((*hdrpp)->sh_type)
11687 {
11688 case SHT_MIPS_MSYM:
11689 case SHT_MIPS_LIBLIST:
11690 sec = bfd_get_section_by_name (abfd, ".dynstr");
11691 if (sec != NULL)
11692 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11693 break;
11694
11695 case SHT_MIPS_GPTAB:
11696 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11697 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11698 BFD_ASSERT (name != NULL
0112cd26 11699 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
11700 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11701 BFD_ASSERT (sec != NULL);
11702 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11703 break;
11704
11705 case SHT_MIPS_CONTENT:
11706 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11707 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11708 BFD_ASSERT (name != NULL
0112cd26 11709 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
11710 sec = bfd_get_section_by_name (abfd,
11711 name + sizeof ".MIPS.content" - 1);
11712 BFD_ASSERT (sec != NULL);
11713 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11714 break;
11715
11716 case SHT_MIPS_SYMBOL_LIB:
11717 sec = bfd_get_section_by_name (abfd, ".dynsym");
11718 if (sec != NULL)
11719 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11720 sec = bfd_get_section_by_name (abfd, ".liblist");
11721 if (sec != NULL)
11722 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11723 break;
11724
11725 case SHT_MIPS_EVENTS:
11726 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11727 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11728 BFD_ASSERT (name != NULL);
0112cd26 11729 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
11730 sec = bfd_get_section_by_name (abfd,
11731 name + sizeof ".MIPS.events" - 1);
11732 else
11733 {
0112cd26 11734 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
11735 sec = bfd_get_section_by_name (abfd,
11736 (name
11737 + sizeof ".MIPS.post_rel" - 1));
11738 }
11739 BFD_ASSERT (sec != NULL);
11740 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11741 break;
11742
11743 }
11744 }
11745}
11746\f
8dc1a139 11747/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
11748 segments. */
11749
11750int
a6b96beb
AM
11751_bfd_mips_elf_additional_program_headers (bfd *abfd,
11752 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
11753{
11754 asection *s;
11755 int ret = 0;
11756
11757 /* See if we need a PT_MIPS_REGINFO segment. */
11758 s = bfd_get_section_by_name (abfd, ".reginfo");
11759 if (s && (s->flags & SEC_LOAD))
11760 ++ret;
11761
11762 /* See if we need a PT_MIPS_OPTIONS segment. */
11763 if (IRIX_COMPAT (abfd) == ict_irix6
11764 && bfd_get_section_by_name (abfd,
11765 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11766 ++ret;
11767
11768 /* See if we need a PT_MIPS_RTPROC segment. */
11769 if (IRIX_COMPAT (abfd) == ict_irix5
11770 && bfd_get_section_by_name (abfd, ".dynamic")
11771 && bfd_get_section_by_name (abfd, ".mdebug"))
11772 ++ret;
11773
98c904a8
RS
11774 /* Allocate a PT_NULL header in dynamic objects. See
11775 _bfd_mips_elf_modify_segment_map for details. */
11776 if (!SGI_COMPAT (abfd)
11777 && bfd_get_section_by_name (abfd, ".dynamic"))
11778 ++ret;
11779
b49e97c9
TS
11780 return ret;
11781}
11782
8dc1a139 11783/* Modify the segment map for an IRIX5 executable. */
b49e97c9 11784
b34976b6 11785bfd_boolean
9719ad41 11786_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 11787 struct bfd_link_info *info)
b49e97c9
TS
11788{
11789 asection *s;
11790 struct elf_segment_map *m, **pm;
11791 bfd_size_type amt;
11792
11793 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11794 segment. */
11795 s = bfd_get_section_by_name (abfd, ".reginfo");
11796 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11797 {
12bd6957 11798 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
11799 if (m->p_type == PT_MIPS_REGINFO)
11800 break;
11801 if (m == NULL)
11802 {
11803 amt = sizeof *m;
9719ad41 11804 m = bfd_zalloc (abfd, amt);
b49e97c9 11805 if (m == NULL)
b34976b6 11806 return FALSE;
b49e97c9
TS
11807
11808 m->p_type = PT_MIPS_REGINFO;
11809 m->count = 1;
11810 m->sections[0] = s;
11811
11812 /* We want to put it after the PHDR and INTERP segments. */
12bd6957 11813 pm = &elf_seg_map (abfd);
b49e97c9
TS
11814 while (*pm != NULL
11815 && ((*pm)->p_type == PT_PHDR
11816 || (*pm)->p_type == PT_INTERP))
11817 pm = &(*pm)->next;
11818
11819 m->next = *pm;
11820 *pm = m;
11821 }
11822 }
11823
11824 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11825 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 11826 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 11827 table. */
c1fd6598
AO
11828 if (NEWABI_P (abfd)
11829 /* On non-IRIX6 new abi, we'll have already created a segment
11830 for this section, so don't create another. I'm not sure this
11831 is not also the case for IRIX 6, but I can't test it right
11832 now. */
11833 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
11834 {
11835 for (s = abfd->sections; s; s = s->next)
11836 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11837 break;
11838
11839 if (s)
11840 {
11841 struct elf_segment_map *options_segment;
11842
12bd6957 11843 pm = &elf_seg_map (abfd);
98a8deaf
RS
11844 while (*pm != NULL
11845 && ((*pm)->p_type == PT_PHDR
11846 || (*pm)->p_type == PT_INTERP))
11847 pm = &(*pm)->next;
b49e97c9 11848
8ded5a0f
AM
11849 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11850 {
11851 amt = sizeof (struct elf_segment_map);
11852 options_segment = bfd_zalloc (abfd, amt);
11853 options_segment->next = *pm;
11854 options_segment->p_type = PT_MIPS_OPTIONS;
11855 options_segment->p_flags = PF_R;
11856 options_segment->p_flags_valid = TRUE;
11857 options_segment->count = 1;
11858 options_segment->sections[0] = s;
11859 *pm = options_segment;
11860 }
b49e97c9
TS
11861 }
11862 }
11863 else
11864 {
11865 if (IRIX_COMPAT (abfd) == ict_irix5)
11866 {
11867 /* If there are .dynamic and .mdebug sections, we make a room
11868 for the RTPROC header. FIXME: Rewrite without section names. */
11869 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11870 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11871 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11872 {
12bd6957 11873 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
11874 if (m->p_type == PT_MIPS_RTPROC)
11875 break;
11876 if (m == NULL)
11877 {
11878 amt = sizeof *m;
9719ad41 11879 m = bfd_zalloc (abfd, amt);
b49e97c9 11880 if (m == NULL)
b34976b6 11881 return FALSE;
b49e97c9
TS
11882
11883 m->p_type = PT_MIPS_RTPROC;
11884
11885 s = bfd_get_section_by_name (abfd, ".rtproc");
11886 if (s == NULL)
11887 {
11888 m->count = 0;
11889 m->p_flags = 0;
11890 m->p_flags_valid = 1;
11891 }
11892 else
11893 {
11894 m->count = 1;
11895 m->sections[0] = s;
11896 }
11897
11898 /* We want to put it after the DYNAMIC segment. */
12bd6957 11899 pm = &elf_seg_map (abfd);
b49e97c9
TS
11900 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11901 pm = &(*pm)->next;
11902 if (*pm != NULL)
11903 pm = &(*pm)->next;
11904
11905 m->next = *pm;
11906 *pm = m;
11907 }
11908 }
11909 }
8dc1a139 11910 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
11911 .dynstr, .dynsym, and .hash sections, and everything in
11912 between. */
12bd6957 11913 for (pm = &elf_seg_map (abfd); *pm != NULL;
b49e97c9
TS
11914 pm = &(*pm)->next)
11915 if ((*pm)->p_type == PT_DYNAMIC)
11916 break;
11917 m = *pm;
11918 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
11919 {
11920 /* For a normal mips executable the permissions for the PT_DYNAMIC
11921 segment are read, write and execute. We do that here since
11922 the code in elf.c sets only the read permission. This matters
11923 sometimes for the dynamic linker. */
11924 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
11925 {
11926 m->p_flags = PF_R | PF_W | PF_X;
11927 m->p_flags_valid = 1;
11928 }
11929 }
f6f62d6f
RS
11930 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11931 glibc's dynamic linker has traditionally derived the number of
11932 tags from the p_filesz field, and sometimes allocates stack
11933 arrays of that size. An overly-big PT_DYNAMIC segment can
11934 be actively harmful in such cases. Making PT_DYNAMIC contain
11935 other sections can also make life hard for the prelinker,
11936 which might move one of the other sections to a different
11937 PT_LOAD segment. */
11938 if (SGI_COMPAT (abfd)
11939 && m != NULL
11940 && m->count == 1
11941 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
11942 {
11943 static const char *sec_names[] =
11944 {
11945 ".dynamic", ".dynstr", ".dynsym", ".hash"
11946 };
11947 bfd_vma low, high;
11948 unsigned int i, c;
11949 struct elf_segment_map *n;
11950
792b4a53 11951 low = ~(bfd_vma) 0;
b49e97c9
TS
11952 high = 0;
11953 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11954 {
11955 s = bfd_get_section_by_name (abfd, sec_names[i]);
11956 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11957 {
11958 bfd_size_type sz;
11959
11960 if (low > s->vma)
11961 low = s->vma;
eea6121a 11962 sz = s->size;
b49e97c9
TS
11963 if (high < s->vma + sz)
11964 high = s->vma + sz;
11965 }
11966 }
11967
11968 c = 0;
11969 for (s = abfd->sections; s != NULL; s = s->next)
11970 if ((s->flags & SEC_LOAD) != 0
11971 && s->vma >= low
eea6121a 11972 && s->vma + s->size <= high)
b49e97c9
TS
11973 ++c;
11974
11975 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 11976 n = bfd_zalloc (abfd, amt);
b49e97c9 11977 if (n == NULL)
b34976b6 11978 return FALSE;
b49e97c9
TS
11979 *n = *m;
11980 n->count = c;
11981
11982 i = 0;
11983 for (s = abfd->sections; s != NULL; s = s->next)
11984 {
11985 if ((s->flags & SEC_LOAD) != 0
11986 && s->vma >= low
eea6121a 11987 && s->vma + s->size <= high)
b49e97c9
TS
11988 {
11989 n->sections[i] = s;
11990 ++i;
11991 }
11992 }
11993
11994 *pm = n;
11995 }
11996 }
11997
98c904a8
RS
11998 /* Allocate a spare program header in dynamic objects so that tools
11999 like the prelinker can add an extra PT_LOAD entry.
12000
12001 If the prelinker needs to make room for a new PT_LOAD entry, its
12002 standard procedure is to move the first (read-only) sections into
12003 the new (writable) segment. However, the MIPS ABI requires
12004 .dynamic to be in a read-only segment, and the section will often
12005 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12006
12007 Although the prelinker could in principle move .dynamic to a
12008 writable segment, it seems better to allocate a spare program
12009 header instead, and avoid the need to move any sections.
12010 There is a long tradition of allocating spare dynamic tags,
12011 so allocating a spare program header seems like a natural
7c8b76cc
JM
12012 extension.
12013
12014 If INFO is NULL, we may be copying an already prelinked binary
12015 with objcopy or strip, so do not add this header. */
12016 if (info != NULL
12017 && !SGI_COMPAT (abfd)
98c904a8
RS
12018 && bfd_get_section_by_name (abfd, ".dynamic"))
12019 {
12bd6957 12020 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
98c904a8
RS
12021 if ((*pm)->p_type == PT_NULL)
12022 break;
12023 if (*pm == NULL)
12024 {
12025 m = bfd_zalloc (abfd, sizeof (*m));
12026 if (m == NULL)
12027 return FALSE;
12028
12029 m->p_type = PT_NULL;
12030 *pm = m;
12031 }
12032 }
12033
b34976b6 12034 return TRUE;
b49e97c9
TS
12035}
12036\f
12037/* Return the section that should be marked against GC for a given
12038 relocation. */
12039
12040asection *
9719ad41 12041_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 12042 struct bfd_link_info *info,
9719ad41
RS
12043 Elf_Internal_Rela *rel,
12044 struct elf_link_hash_entry *h,
12045 Elf_Internal_Sym *sym)
b49e97c9
TS
12046{
12047 /* ??? Do mips16 stub sections need to be handled special? */
12048
12049 if (h != NULL)
07adf181
AM
12050 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12051 {
12052 case R_MIPS_GNU_VTINHERIT:
12053 case R_MIPS_GNU_VTENTRY:
12054 return NULL;
12055 }
b49e97c9 12056
07adf181 12057 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
12058}
12059
12060/* Update the got entry reference counts for the section being removed. */
12061
b34976b6 12062bfd_boolean
9719ad41
RS
12063_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
12064 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12065 asection *sec ATTRIBUTE_UNUSED,
12066 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
12067{
12068#if 0
12069 Elf_Internal_Shdr *symtab_hdr;
12070 struct elf_link_hash_entry **sym_hashes;
12071 bfd_signed_vma *local_got_refcounts;
12072 const Elf_Internal_Rela *rel, *relend;
12073 unsigned long r_symndx;
12074 struct elf_link_hash_entry *h;
12075
7dda2462
TG
12076 if (info->relocatable)
12077 return TRUE;
12078
b49e97c9
TS
12079 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12080 sym_hashes = elf_sym_hashes (abfd);
12081 local_got_refcounts = elf_local_got_refcounts (abfd);
12082
12083 relend = relocs + sec->reloc_count;
12084 for (rel = relocs; rel < relend; rel++)
12085 switch (ELF_R_TYPE (abfd, rel->r_info))
12086 {
738e5348
RS
12087 case R_MIPS16_GOT16:
12088 case R_MIPS16_CALL16:
b49e97c9
TS
12089 case R_MIPS_GOT16:
12090 case R_MIPS_CALL16:
12091 case R_MIPS_CALL_HI16:
12092 case R_MIPS_CALL_LO16:
12093 case R_MIPS_GOT_HI16:
12094 case R_MIPS_GOT_LO16:
4a14403c
TS
12095 case R_MIPS_GOT_DISP:
12096 case R_MIPS_GOT_PAGE:
12097 case R_MIPS_GOT_OFST:
df58fc94
RS
12098 case R_MICROMIPS_GOT16:
12099 case R_MICROMIPS_CALL16:
12100 case R_MICROMIPS_CALL_HI16:
12101 case R_MICROMIPS_CALL_LO16:
12102 case R_MICROMIPS_GOT_HI16:
12103 case R_MICROMIPS_GOT_LO16:
12104 case R_MICROMIPS_GOT_DISP:
12105 case R_MICROMIPS_GOT_PAGE:
12106 case R_MICROMIPS_GOT_OFST:
b49e97c9
TS
12107 /* ??? It would seem that the existing MIPS code does no sort
12108 of reference counting or whatnot on its GOT and PLT entries,
12109 so it is not possible to garbage collect them at this time. */
12110 break;
12111
12112 default:
12113 break;
12114 }
12115#endif
12116
b34976b6 12117 return TRUE;
b49e97c9
TS
12118}
12119\f
12120/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12121 hiding the old indirect symbol. Process additional relocation
12122 information. Also called for weakdefs, in which case we just let
12123 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12124
12125void
fcfa13d2 12126_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
12127 struct elf_link_hash_entry *dir,
12128 struct elf_link_hash_entry *ind)
b49e97c9
TS
12129{
12130 struct mips_elf_link_hash_entry *dirmips, *indmips;
12131
fcfa13d2 12132 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 12133
861fb55a
DJ
12134 dirmips = (struct mips_elf_link_hash_entry *) dir;
12135 indmips = (struct mips_elf_link_hash_entry *) ind;
12136 /* Any absolute non-dynamic relocations against an indirect or weak
12137 definition will be against the target symbol. */
12138 if (indmips->has_static_relocs)
12139 dirmips->has_static_relocs = TRUE;
12140
b49e97c9
TS
12141 if (ind->root.type != bfd_link_hash_indirect)
12142 return;
12143
b49e97c9
TS
12144 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12145 if (indmips->readonly_reloc)
b34976b6 12146 dirmips->readonly_reloc = TRUE;
b49e97c9 12147 if (indmips->no_fn_stub)
b34976b6 12148 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
12149 if (indmips->fn_stub)
12150 {
12151 dirmips->fn_stub = indmips->fn_stub;
12152 indmips->fn_stub = NULL;
12153 }
12154 if (indmips->need_fn_stub)
12155 {
12156 dirmips->need_fn_stub = TRUE;
12157 indmips->need_fn_stub = FALSE;
12158 }
12159 if (indmips->call_stub)
12160 {
12161 dirmips->call_stub = indmips->call_stub;
12162 indmips->call_stub = NULL;
12163 }
12164 if (indmips->call_fp_stub)
12165 {
12166 dirmips->call_fp_stub = indmips->call_fp_stub;
12167 indmips->call_fp_stub = NULL;
12168 }
634835ae
RS
12169 if (indmips->global_got_area < dirmips->global_got_area)
12170 dirmips->global_got_area = indmips->global_got_area;
12171 if (indmips->global_got_area < GGA_NONE)
12172 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
12173 if (indmips->has_nonpic_branches)
12174 dirmips->has_nonpic_branches = TRUE;
b49e97c9 12175}
b49e97c9 12176\f
d01414a5
TS
12177#define PDR_SIZE 32
12178
b34976b6 12179bfd_boolean
9719ad41
RS
12180_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12181 struct bfd_link_info *info)
d01414a5
TS
12182{
12183 asection *o;
b34976b6 12184 bfd_boolean ret = FALSE;
d01414a5
TS
12185 unsigned char *tdata;
12186 size_t i, skip;
12187
12188 o = bfd_get_section_by_name (abfd, ".pdr");
12189 if (! o)
b34976b6 12190 return FALSE;
eea6121a 12191 if (o->size == 0)
b34976b6 12192 return FALSE;
eea6121a 12193 if (o->size % PDR_SIZE != 0)
b34976b6 12194 return FALSE;
d01414a5
TS
12195 if (o->output_section != NULL
12196 && bfd_is_abs_section (o->output_section))
b34976b6 12197 return FALSE;
d01414a5 12198
eea6121a 12199 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 12200 if (! tdata)
b34976b6 12201 return FALSE;
d01414a5 12202
9719ad41 12203 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 12204 info->keep_memory);
d01414a5
TS
12205 if (!cookie->rels)
12206 {
12207 free (tdata);
b34976b6 12208 return FALSE;
d01414a5
TS
12209 }
12210
12211 cookie->rel = cookie->rels;
12212 cookie->relend = cookie->rels + o->reloc_count;
12213
eea6121a 12214 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 12215 {
c152c796 12216 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
12217 {
12218 tdata[i] = 1;
12219 skip ++;
12220 }
12221 }
12222
12223 if (skip != 0)
12224 {
f0abc2a1 12225 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 12226 o->size -= skip * PDR_SIZE;
b34976b6 12227 ret = TRUE;
d01414a5
TS
12228 }
12229 else
12230 free (tdata);
12231
12232 if (! info->keep_memory)
12233 free (cookie->rels);
12234
12235 return ret;
12236}
12237
b34976b6 12238bfd_boolean
9719ad41 12239_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
12240{
12241 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
12242 return TRUE;
12243 return FALSE;
53bfd6b4 12244}
d01414a5 12245
b34976b6 12246bfd_boolean
c7b8f16e
JB
12247_bfd_mips_elf_write_section (bfd *output_bfd,
12248 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12249 asection *sec, bfd_byte *contents)
d01414a5
TS
12250{
12251 bfd_byte *to, *from, *end;
12252 int i;
12253
12254 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 12255 return FALSE;
d01414a5 12256
f0abc2a1 12257 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 12258 return FALSE;
d01414a5
TS
12259
12260 to = contents;
eea6121a 12261 end = contents + sec->size;
d01414a5
TS
12262 for (from = contents, i = 0;
12263 from < end;
12264 from += PDR_SIZE, i++)
12265 {
f0abc2a1 12266 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
12267 continue;
12268 if (to != from)
12269 memcpy (to, from, PDR_SIZE);
12270 to += PDR_SIZE;
12271 }
12272 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 12273 sec->output_offset, sec->size);
b34976b6 12274 return TRUE;
d01414a5 12275}
53bfd6b4 12276\f
df58fc94
RS
12277/* microMIPS code retains local labels for linker relaxation. Omit them
12278 from output by default for clarity. */
12279
12280bfd_boolean
12281_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12282{
12283 return _bfd_elf_is_local_label_name (abfd, sym->name);
12284}
12285
b49e97c9
TS
12286/* MIPS ELF uses a special find_nearest_line routine in order the
12287 handle the ECOFF debugging information. */
12288
12289struct mips_elf_find_line
12290{
12291 struct ecoff_debug_info d;
12292 struct ecoff_find_line i;
12293};
12294
b34976b6 12295bfd_boolean
9719ad41
RS
12296_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
12297 asymbol **symbols, bfd_vma offset,
12298 const char **filename_ptr,
12299 const char **functionname_ptr,
12300 unsigned int *line_ptr)
b49e97c9
TS
12301{
12302 asection *msec;
12303
12304 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
12305 filename_ptr, functionname_ptr,
12306 line_ptr))
b34976b6 12307 return TRUE;
b49e97c9 12308
fc28f9aa
TG
12309 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
12310 section, symbols, offset,
b49e97c9 12311 filename_ptr, functionname_ptr,
9b8d1a36 12312 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 12313 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 12314 return TRUE;
b49e97c9
TS
12315
12316 msec = bfd_get_section_by_name (abfd, ".mdebug");
12317 if (msec != NULL)
12318 {
12319 flagword origflags;
12320 struct mips_elf_find_line *fi;
12321 const struct ecoff_debug_swap * const swap =
12322 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12323
12324 /* If we are called during a link, mips_elf_final_link may have
12325 cleared the SEC_HAS_CONTENTS field. We force it back on here
12326 if appropriate (which it normally will be). */
12327 origflags = msec->flags;
12328 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12329 msec->flags |= SEC_HAS_CONTENTS;
12330
698600e4 12331 fi = mips_elf_tdata (abfd)->find_line_info;
b49e97c9
TS
12332 if (fi == NULL)
12333 {
12334 bfd_size_type external_fdr_size;
12335 char *fraw_src;
12336 char *fraw_end;
12337 struct fdr *fdr_ptr;
12338 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12339
9719ad41 12340 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
12341 if (fi == NULL)
12342 {
12343 msec->flags = origflags;
b34976b6 12344 return FALSE;
b49e97c9
TS
12345 }
12346
12347 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12348 {
12349 msec->flags = origflags;
b34976b6 12350 return FALSE;
b49e97c9
TS
12351 }
12352
12353 /* Swap in the FDR information. */
12354 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 12355 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
12356 if (fi->d.fdr == NULL)
12357 {
12358 msec->flags = origflags;
b34976b6 12359 return FALSE;
b49e97c9
TS
12360 }
12361 external_fdr_size = swap->external_fdr_size;
12362 fdr_ptr = fi->d.fdr;
12363 fraw_src = (char *) fi->d.external_fdr;
12364 fraw_end = (fraw_src
12365 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12366 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 12367 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9 12368
698600e4 12369 mips_elf_tdata (abfd)->find_line_info = fi;
b49e97c9
TS
12370
12371 /* Note that we don't bother to ever free this information.
12372 find_nearest_line is either called all the time, as in
12373 objdump -l, so the information should be saved, or it is
12374 rarely called, as in ld error messages, so the memory
12375 wasted is unimportant. Still, it would probably be a
12376 good idea for free_cached_info to throw it away. */
12377 }
12378
12379 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
12380 &fi->i, filename_ptr, functionname_ptr,
12381 line_ptr))
12382 {
12383 msec->flags = origflags;
b34976b6 12384 return TRUE;
b49e97c9
TS
12385 }
12386
12387 msec->flags = origflags;
12388 }
12389
12390 /* Fall back on the generic ELF find_nearest_line routine. */
12391
12392 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
12393 filename_ptr, functionname_ptr,
12394 line_ptr);
12395}
4ab527b0
FF
12396
12397bfd_boolean
12398_bfd_mips_elf_find_inliner_info (bfd *abfd,
12399 const char **filename_ptr,
12400 const char **functionname_ptr,
12401 unsigned int *line_ptr)
12402{
12403 bfd_boolean found;
12404 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12405 functionname_ptr, line_ptr,
12406 & elf_tdata (abfd)->dwarf2_find_line_info);
12407 return found;
12408}
12409
b49e97c9
TS
12410\f
12411/* When are writing out the .options or .MIPS.options section,
12412 remember the bytes we are writing out, so that we can install the
12413 GP value in the section_processing routine. */
12414
b34976b6 12415bfd_boolean
9719ad41
RS
12416_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
12417 const void *location,
12418 file_ptr offset, bfd_size_type count)
b49e97c9 12419{
cc2e31b9 12420 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
12421 {
12422 bfd_byte *c;
12423
12424 if (elf_section_data (section) == NULL)
12425 {
12426 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 12427 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 12428 if (elf_section_data (section) == NULL)
b34976b6 12429 return FALSE;
b49e97c9 12430 }
f0abc2a1 12431 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
12432 if (c == NULL)
12433 {
eea6121a 12434 c = bfd_zalloc (abfd, section->size);
b49e97c9 12435 if (c == NULL)
b34976b6 12436 return FALSE;
f0abc2a1 12437 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
12438 }
12439
9719ad41 12440 memcpy (c + offset, location, count);
b49e97c9
TS
12441 }
12442
12443 return _bfd_elf_set_section_contents (abfd, section, location, offset,
12444 count);
12445}
12446
12447/* This is almost identical to bfd_generic_get_... except that some
12448 MIPS relocations need to be handled specially. Sigh. */
12449
12450bfd_byte *
9719ad41
RS
12451_bfd_elf_mips_get_relocated_section_contents
12452 (bfd *abfd,
12453 struct bfd_link_info *link_info,
12454 struct bfd_link_order *link_order,
12455 bfd_byte *data,
12456 bfd_boolean relocatable,
12457 asymbol **symbols)
b49e97c9
TS
12458{
12459 /* Get enough memory to hold the stuff */
12460 bfd *input_bfd = link_order->u.indirect.section->owner;
12461 asection *input_section = link_order->u.indirect.section;
eea6121a 12462 bfd_size_type sz;
b49e97c9
TS
12463
12464 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12465 arelent **reloc_vector = NULL;
12466 long reloc_count;
12467
12468 if (reloc_size < 0)
12469 goto error_return;
12470
9719ad41 12471 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
12472 if (reloc_vector == NULL && reloc_size != 0)
12473 goto error_return;
12474
12475 /* read in the section */
eea6121a
AM
12476 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
12477 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
12478 goto error_return;
12479
b49e97c9
TS
12480 reloc_count = bfd_canonicalize_reloc (input_bfd,
12481 input_section,
12482 reloc_vector,
12483 symbols);
12484 if (reloc_count < 0)
12485 goto error_return;
12486
12487 if (reloc_count > 0)
12488 {
12489 arelent **parent;
12490 /* for mips */
12491 int gp_found;
12492 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
12493
12494 {
12495 struct bfd_hash_entry *h;
12496 struct bfd_link_hash_entry *lh;
12497 /* Skip all this stuff if we aren't mixing formats. */
12498 if (abfd && input_bfd
12499 && abfd->xvec == input_bfd->xvec)
12500 lh = 0;
12501 else
12502 {
b34976b6 12503 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
12504 lh = (struct bfd_link_hash_entry *) h;
12505 }
12506 lookup:
12507 if (lh)
12508 {
12509 switch (lh->type)
12510 {
12511 case bfd_link_hash_undefined:
12512 case bfd_link_hash_undefweak:
12513 case bfd_link_hash_common:
12514 gp_found = 0;
12515 break;
12516 case bfd_link_hash_defined:
12517 case bfd_link_hash_defweak:
12518 gp_found = 1;
12519 gp = lh->u.def.value;
12520 break;
12521 case bfd_link_hash_indirect:
12522 case bfd_link_hash_warning:
12523 lh = lh->u.i.link;
12524 /* @@FIXME ignoring warning for now */
12525 goto lookup;
12526 case bfd_link_hash_new:
12527 default:
12528 abort ();
12529 }
12530 }
12531 else
12532 gp_found = 0;
12533 }
12534 /* end mips */
9719ad41 12535 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 12536 {
9719ad41 12537 char *error_message = NULL;
b49e97c9
TS
12538 bfd_reloc_status_type r;
12539
12540 /* Specific to MIPS: Deal with relocation types that require
12541 knowing the gp of the output bfd. */
12542 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 12543
8236346f
EC
12544 /* If we've managed to find the gp and have a special
12545 function for the relocation then go ahead, else default
12546 to the generic handling. */
12547 if (gp_found
12548 && (*parent)->howto->special_function
12549 == _bfd_mips_elf32_gprel16_reloc)
12550 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
12551 input_section, relocatable,
12552 data, gp);
12553 else
86324f90 12554 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
12555 input_section,
12556 relocatable ? abfd : NULL,
12557 &error_message);
b49e97c9 12558
1049f94e 12559 if (relocatable)
b49e97c9
TS
12560 {
12561 asection *os = input_section->output_section;
12562
12563 /* A partial link, so keep the relocs */
12564 os->orelocation[os->reloc_count] = *parent;
12565 os->reloc_count++;
12566 }
12567
12568 if (r != bfd_reloc_ok)
12569 {
12570 switch (r)
12571 {
12572 case bfd_reloc_undefined:
12573 if (!((*link_info->callbacks->undefined_symbol)
12574 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 12575 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
12576 goto error_return;
12577 break;
12578 case bfd_reloc_dangerous:
9719ad41 12579 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
12580 if (!((*link_info->callbacks->reloc_dangerous)
12581 (link_info, error_message, input_bfd, input_section,
12582 (*parent)->address)))
12583 goto error_return;
12584 break;
12585 case bfd_reloc_overflow:
12586 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
12587 (link_info, NULL,
12588 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
12589 (*parent)->howto->name, (*parent)->addend,
12590 input_bfd, input_section, (*parent)->address)))
12591 goto error_return;
12592 break;
12593 case bfd_reloc_outofrange:
12594 default:
12595 abort ();
12596 break;
12597 }
12598
12599 }
12600 }
12601 }
12602 if (reloc_vector != NULL)
12603 free (reloc_vector);
12604 return data;
12605
12606error_return:
12607 if (reloc_vector != NULL)
12608 free (reloc_vector);
12609 return NULL;
12610}
12611\f
df58fc94
RS
12612static bfd_boolean
12613mips_elf_relax_delete_bytes (bfd *abfd,
12614 asection *sec, bfd_vma addr, int count)
12615{
12616 Elf_Internal_Shdr *symtab_hdr;
12617 unsigned int sec_shndx;
12618 bfd_byte *contents;
12619 Elf_Internal_Rela *irel, *irelend;
12620 Elf_Internal_Sym *isym;
12621 Elf_Internal_Sym *isymend;
12622 struct elf_link_hash_entry **sym_hashes;
12623 struct elf_link_hash_entry **end_hashes;
12624 struct elf_link_hash_entry **start_hashes;
12625 unsigned int symcount;
12626
12627 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
12628 contents = elf_section_data (sec)->this_hdr.contents;
12629
12630 irel = elf_section_data (sec)->relocs;
12631 irelend = irel + sec->reloc_count;
12632
12633 /* Actually delete the bytes. */
12634 memmove (contents + addr, contents + addr + count,
12635 (size_t) (sec->size - addr - count));
12636 sec->size -= count;
12637
12638 /* Adjust all the relocs. */
12639 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12640 {
12641 /* Get the new reloc address. */
12642 if (irel->r_offset > addr)
12643 irel->r_offset -= count;
12644 }
12645
12646 BFD_ASSERT (addr % 2 == 0);
12647 BFD_ASSERT (count % 2 == 0);
12648
12649 /* Adjust the local symbols defined in this section. */
12650 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12651 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12652 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2309ddf2 12653 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
df58fc94
RS
12654 isym->st_value -= count;
12655
12656 /* Now adjust the global symbols defined in this section. */
12657 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
12658 - symtab_hdr->sh_info);
12659 sym_hashes = start_hashes = elf_sym_hashes (abfd);
12660 end_hashes = sym_hashes + symcount;
12661
12662 for (; sym_hashes < end_hashes; sym_hashes++)
12663 {
12664 struct elf_link_hash_entry *sym_hash = *sym_hashes;
12665
12666 if ((sym_hash->root.type == bfd_link_hash_defined
12667 || sym_hash->root.type == bfd_link_hash_defweak)
12668 && sym_hash->root.u.def.section == sec)
12669 {
2309ddf2 12670 bfd_vma value = sym_hash->root.u.def.value;
df58fc94 12671
df58fc94
RS
12672 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
12673 value &= MINUS_TWO;
12674 if (value > addr)
12675 sym_hash->root.u.def.value -= count;
12676 }
12677 }
12678
12679 return TRUE;
12680}
12681
12682
12683/* Opcodes needed for microMIPS relaxation as found in
12684 opcodes/micromips-opc.c. */
12685
12686struct opcode_descriptor {
12687 unsigned long match;
12688 unsigned long mask;
12689};
12690
12691/* The $ra register aka $31. */
12692
12693#define RA 31
12694
12695/* 32-bit instruction format register fields. */
12696
12697#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12698#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12699
12700/* Check if a 5-bit register index can be abbreviated to 3 bits. */
12701
12702#define OP16_VALID_REG(r) \
12703 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12704
12705
12706/* 32-bit and 16-bit branches. */
12707
12708static const struct opcode_descriptor b_insns_32[] = {
12709 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12710 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12711 { 0, 0 } /* End marker for find_match(). */
12712};
12713
12714static const struct opcode_descriptor bc_insn_32 =
12715 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12716
12717static const struct opcode_descriptor bz_insn_32 =
12718 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12719
12720static const struct opcode_descriptor bzal_insn_32 =
12721 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12722
12723static const struct opcode_descriptor beq_insn_32 =
12724 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12725
12726static const struct opcode_descriptor b_insn_16 =
12727 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12728
12729static const struct opcode_descriptor bz_insn_16 =
c088dedf 12730 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
df58fc94
RS
12731
12732
12733/* 32-bit and 16-bit branch EQ and NE zero. */
12734
12735/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12736 eq and second the ne. This convention is used when replacing a
12737 32-bit BEQ/BNE with the 16-bit version. */
12738
12739#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12740
12741static const struct opcode_descriptor bz_rs_insns_32[] = {
12742 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12743 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12744 { 0, 0 } /* End marker for find_match(). */
12745};
12746
12747static const struct opcode_descriptor bz_rt_insns_32[] = {
12748 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12749 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12750 { 0, 0 } /* End marker for find_match(). */
12751};
12752
12753static const struct opcode_descriptor bzc_insns_32[] = {
12754 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12755 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12756 { 0, 0 } /* End marker for find_match(). */
12757};
12758
12759static const struct opcode_descriptor bz_insns_16[] = {
12760 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12761 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12762 { 0, 0 } /* End marker for find_match(). */
12763};
12764
12765/* Switch between a 5-bit register index and its 3-bit shorthand. */
12766
12767#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12768#define BZ16_REG_FIELD(r) \
12769 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12770
12771
12772/* 32-bit instructions with a delay slot. */
12773
12774static const struct opcode_descriptor jal_insn_32_bd16 =
12775 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12776
12777static const struct opcode_descriptor jal_insn_32_bd32 =
12778 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12779
12780static const struct opcode_descriptor jal_x_insn_32_bd32 =
12781 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12782
12783static const struct opcode_descriptor j_insn_32 =
12784 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12785
12786static const struct opcode_descriptor jalr_insn_32 =
12787 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12788
12789/* This table can be compacted, because no opcode replacement is made. */
12790
12791static const struct opcode_descriptor ds_insns_32_bd16[] = {
12792 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12793
12794 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12795 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12796
12797 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12798 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12799 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12800 { 0, 0 } /* End marker for find_match(). */
12801};
12802
12803/* This table can be compacted, because no opcode replacement is made. */
12804
12805static const struct opcode_descriptor ds_insns_32_bd32[] = {
12806 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12807
12808 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12809 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12810 { 0, 0 } /* End marker for find_match(). */
12811};
12812
12813
12814/* 16-bit instructions with a delay slot. */
12815
12816static const struct opcode_descriptor jalr_insn_16_bd16 =
12817 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12818
12819static const struct opcode_descriptor jalr_insn_16_bd32 =
12820 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12821
12822static const struct opcode_descriptor jr_insn_16 =
12823 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12824
12825#define JR16_REG(opcode) ((opcode) & 0x1f)
12826
12827/* This table can be compacted, because no opcode replacement is made. */
12828
12829static const struct opcode_descriptor ds_insns_16_bd16[] = {
12830 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12831
12832 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12833 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12834 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12835 { 0, 0 } /* End marker for find_match(). */
12836};
12837
12838
12839/* LUI instruction. */
12840
12841static const struct opcode_descriptor lui_insn =
12842 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12843
12844
12845/* ADDIU instruction. */
12846
12847static const struct opcode_descriptor addiu_insn =
12848 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12849
12850static const struct opcode_descriptor addiupc_insn =
12851 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12852
12853#define ADDIUPC_REG_FIELD(r) \
12854 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12855
12856
12857/* Relaxable instructions in a JAL delay slot: MOVE. */
12858
12859/* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12860 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12861#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12862#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12863
12864#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12865#define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12866
12867static const struct opcode_descriptor move_insns_32[] = {
12868 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12869 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12870 { 0, 0 } /* End marker for find_match(). */
12871};
12872
12873static const struct opcode_descriptor move_insn_16 =
12874 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12875
12876
12877/* NOP instructions. */
12878
12879static const struct opcode_descriptor nop_insn_32 =
12880 { /* "nop", "", */ 0x00000000, 0xffffffff };
12881
12882static const struct opcode_descriptor nop_insn_16 =
12883 { /* "nop", "", */ 0x0c00, 0xffff };
12884
12885
12886/* Instruction match support. */
12887
12888#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12889
12890static int
12891find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12892{
12893 unsigned long indx;
12894
12895 for (indx = 0; insn[indx].mask != 0; indx++)
12896 if (MATCH (opcode, insn[indx]))
12897 return indx;
12898
12899 return -1;
12900}
12901
12902
12903/* Branch and delay slot decoding support. */
12904
12905/* If PTR points to what *might* be a 16-bit branch or jump, then
12906 return the minimum length of its delay slot, otherwise return 0.
12907 Non-zero results are not definitive as we might be checking against
12908 the second half of another instruction. */
12909
12910static int
12911check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12912{
12913 unsigned long opcode;
12914 int bdsize;
12915
12916 opcode = bfd_get_16 (abfd, ptr);
12917 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12918 /* 16-bit branch/jump with a 32-bit delay slot. */
12919 bdsize = 4;
12920 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12921 || find_match (opcode, ds_insns_16_bd16) >= 0)
12922 /* 16-bit branch/jump with a 16-bit delay slot. */
12923 bdsize = 2;
12924 else
12925 /* No delay slot. */
12926 bdsize = 0;
12927
12928 return bdsize;
12929}
12930
12931/* If PTR points to what *might* be a 32-bit branch or jump, then
12932 return the minimum length of its delay slot, otherwise return 0.
12933 Non-zero results are not definitive as we might be checking against
12934 the second half of another instruction. */
12935
12936static int
12937check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12938{
12939 unsigned long opcode;
12940 int bdsize;
12941
d21911ea 12942 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
12943 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12944 /* 32-bit branch/jump with a 32-bit delay slot. */
12945 bdsize = 4;
12946 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12947 /* 32-bit branch/jump with a 16-bit delay slot. */
12948 bdsize = 2;
12949 else
12950 /* No delay slot. */
12951 bdsize = 0;
12952
12953 return bdsize;
12954}
12955
12956/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12957 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12958
12959static bfd_boolean
12960check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12961{
12962 unsigned long opcode;
12963
12964 opcode = bfd_get_16 (abfd, ptr);
12965 if (MATCH (opcode, b_insn_16)
12966 /* B16 */
12967 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12968 /* JR16 */
12969 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12970 /* BEQZ16, BNEZ16 */
12971 || (MATCH (opcode, jalr_insn_16_bd32)
12972 /* JALR16 */
12973 && reg != JR16_REG (opcode) && reg != RA))
12974 return TRUE;
12975
12976 return FALSE;
12977}
12978
12979/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12980 then return TRUE, otherwise FALSE. */
12981
f41e5fcc 12982static bfd_boolean
df58fc94
RS
12983check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12984{
12985 unsigned long opcode;
12986
d21911ea 12987 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
12988 if (MATCH (opcode, j_insn_32)
12989 /* J */
12990 || MATCH (opcode, bc_insn_32)
12991 /* BC1F, BC1T, BC2F, BC2T */
12992 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
12993 /* JAL, JALX */
12994 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
12995 /* BGEZ, BGTZ, BLEZ, BLTZ */
12996 || (MATCH (opcode, bzal_insn_32)
12997 /* BGEZAL, BLTZAL */
12998 && reg != OP32_SREG (opcode) && reg != RA)
12999 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13000 /* JALR, JALR.HB, BEQ, BNE */
13001 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13002 return TRUE;
13003
13004 return FALSE;
13005}
13006
80cab405
MR
13007/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13008 IRELEND) at OFFSET indicate that there must be a compact branch there,
13009 then return TRUE, otherwise FALSE. */
df58fc94
RS
13010
13011static bfd_boolean
80cab405
MR
13012check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13013 const Elf_Internal_Rela *internal_relocs,
13014 const Elf_Internal_Rela *irelend)
df58fc94 13015{
80cab405
MR
13016 const Elf_Internal_Rela *irel;
13017 unsigned long opcode;
13018
d21911ea 13019 opcode = bfd_get_micromips_32 (abfd, ptr);
80cab405
MR
13020 if (find_match (opcode, bzc_insns_32) < 0)
13021 return FALSE;
df58fc94
RS
13022
13023 for (irel = internal_relocs; irel < irelend; irel++)
80cab405
MR
13024 if (irel->r_offset == offset
13025 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13026 return TRUE;
13027
df58fc94
RS
13028 return FALSE;
13029}
80cab405
MR
13030
13031/* Bitsize checking. */
13032#define IS_BITSIZE(val, N) \
13033 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13034 - (1ULL << ((N) - 1))) == (val))
13035
df58fc94
RS
13036\f
13037bfd_boolean
13038_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13039 struct bfd_link_info *link_info,
13040 bfd_boolean *again)
13041{
833794fc 13042 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
df58fc94
RS
13043 Elf_Internal_Shdr *symtab_hdr;
13044 Elf_Internal_Rela *internal_relocs;
13045 Elf_Internal_Rela *irel, *irelend;
13046 bfd_byte *contents = NULL;
13047 Elf_Internal_Sym *isymbuf = NULL;
13048
13049 /* Assume nothing changes. */
13050 *again = FALSE;
13051
13052 /* We don't have to do anything for a relocatable link, if
13053 this section does not have relocs, or if this is not a
13054 code section. */
13055
13056 if (link_info->relocatable
13057 || (sec->flags & SEC_RELOC) == 0
13058 || sec->reloc_count == 0
13059 || (sec->flags & SEC_CODE) == 0)
13060 return TRUE;
13061
13062 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13063
13064 /* Get a copy of the native relocations. */
13065 internal_relocs = (_bfd_elf_link_read_relocs
2c3fc389 13066 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
df58fc94
RS
13067 link_info->keep_memory));
13068 if (internal_relocs == NULL)
13069 goto error_return;
13070
13071 /* Walk through them looking for relaxing opportunities. */
13072 irelend = internal_relocs + sec->reloc_count;
13073 for (irel = internal_relocs; irel < irelend; irel++)
13074 {
13075 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13076 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13077 bfd_boolean target_is_micromips_code_p;
13078 unsigned long opcode;
13079 bfd_vma symval;
13080 bfd_vma pcrval;
2309ddf2 13081 bfd_byte *ptr;
df58fc94
RS
13082 int fndopc;
13083
13084 /* The number of bytes to delete for relaxation and from where
13085 to delete these bytes starting at irel->r_offset. */
13086 int delcnt = 0;
13087 int deloff = 0;
13088
13089 /* If this isn't something that can be relaxed, then ignore
13090 this reloc. */
13091 if (r_type != R_MICROMIPS_HI16
13092 && r_type != R_MICROMIPS_PC16_S1
2309ddf2 13093 && r_type != R_MICROMIPS_26_S1)
df58fc94
RS
13094 continue;
13095
13096 /* Get the section contents if we haven't done so already. */
13097 if (contents == NULL)
13098 {
13099 /* Get cached copy if it exists. */
13100 if (elf_section_data (sec)->this_hdr.contents != NULL)
13101 contents = elf_section_data (sec)->this_hdr.contents;
13102 /* Go get them off disk. */
13103 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13104 goto error_return;
13105 }
2309ddf2 13106 ptr = contents + irel->r_offset;
df58fc94
RS
13107
13108 /* Read this BFD's local symbols if we haven't done so already. */
13109 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13110 {
13111 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13112 if (isymbuf == NULL)
13113 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13114 symtab_hdr->sh_info, 0,
13115 NULL, NULL, NULL);
13116 if (isymbuf == NULL)
13117 goto error_return;
13118 }
13119
13120 /* Get the value of the symbol referred to by the reloc. */
13121 if (r_symndx < symtab_hdr->sh_info)
13122 {
13123 /* A local symbol. */
13124 Elf_Internal_Sym *isym;
13125 asection *sym_sec;
13126
13127 isym = isymbuf + r_symndx;
13128 if (isym->st_shndx == SHN_UNDEF)
13129 sym_sec = bfd_und_section_ptr;
13130 else if (isym->st_shndx == SHN_ABS)
13131 sym_sec = bfd_abs_section_ptr;
13132 else if (isym->st_shndx == SHN_COMMON)
13133 sym_sec = bfd_com_section_ptr;
13134 else
13135 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13136 symval = (isym->st_value
13137 + sym_sec->output_section->vma
13138 + sym_sec->output_offset);
13139 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13140 }
13141 else
13142 {
13143 unsigned long indx;
13144 struct elf_link_hash_entry *h;
13145
13146 /* An external symbol. */
13147 indx = r_symndx - symtab_hdr->sh_info;
13148 h = elf_sym_hashes (abfd)[indx];
13149 BFD_ASSERT (h != NULL);
13150
13151 if (h->root.type != bfd_link_hash_defined
13152 && h->root.type != bfd_link_hash_defweak)
13153 /* This appears to be a reference to an undefined
13154 symbol. Just ignore it -- it will be caught by the
13155 regular reloc processing. */
13156 continue;
13157
13158 symval = (h->root.u.def.value
13159 + h->root.u.def.section->output_section->vma
13160 + h->root.u.def.section->output_offset);
13161 target_is_micromips_code_p = (!h->needs_plt
13162 && ELF_ST_IS_MICROMIPS (h->other));
13163 }
13164
13165
13166 /* For simplicity of coding, we are going to modify the
13167 section contents, the section relocs, and the BFD symbol
13168 table. We must tell the rest of the code not to free up this
13169 information. It would be possible to instead create a table
13170 of changes which have to be made, as is done in coff-mips.c;
13171 that would be more work, but would require less memory when
13172 the linker is run. */
13173
13174 /* Only 32-bit instructions relaxed. */
13175 if (irel->r_offset + 4 > sec->size)
13176 continue;
13177
d21911ea 13178 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13179
13180 /* This is the pc-relative distance from the instruction the
13181 relocation is applied to, to the symbol referred. */
13182 pcrval = (symval
13183 - (sec->output_section->vma + sec->output_offset)
13184 - irel->r_offset);
13185
13186 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13187 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13188 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13189
13190 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13191
13192 where pcrval has first to be adjusted to apply against the LO16
13193 location (we make the adjustment later on, when we have figured
13194 out the offset). */
13195 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13196 {
80cab405 13197 bfd_boolean bzc = FALSE;
df58fc94
RS
13198 unsigned long nextopc;
13199 unsigned long reg;
13200 bfd_vma offset;
13201
13202 /* Give up if the previous reloc was a HI16 against this symbol
13203 too. */
13204 if (irel > internal_relocs
13205 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13206 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13207 continue;
13208
13209 /* Or if the next reloc is not a LO16 against this symbol. */
13210 if (irel + 1 >= irelend
13211 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13212 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13213 continue;
13214
13215 /* Or if the second next reloc is a LO16 against this symbol too. */
13216 if (irel + 2 >= irelend
13217 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13218 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13219 continue;
13220
80cab405
MR
13221 /* See if the LUI instruction *might* be in a branch delay slot.
13222 We check whether what looks like a 16-bit branch or jump is
13223 actually an immediate argument to a compact branch, and let
13224 it through if so. */
df58fc94 13225 if (irel->r_offset >= 2
2309ddf2 13226 && check_br16_dslot (abfd, ptr - 2)
df58fc94 13227 && !(irel->r_offset >= 4
80cab405
MR
13228 && (bzc = check_relocated_bzc (abfd,
13229 ptr - 4, irel->r_offset - 4,
13230 internal_relocs, irelend))))
df58fc94
RS
13231 continue;
13232 if (irel->r_offset >= 4
80cab405 13233 && !bzc
2309ddf2 13234 && check_br32_dslot (abfd, ptr - 4))
df58fc94
RS
13235 continue;
13236
13237 reg = OP32_SREG (opcode);
13238
13239 /* We only relax adjacent instructions or ones separated with
13240 a branch or jump that has a delay slot. The branch or jump
13241 must not fiddle with the register used to hold the address.
13242 Subtract 4 for the LUI itself. */
13243 offset = irel[1].r_offset - irel[0].r_offset;
13244 switch (offset - 4)
13245 {
13246 case 0:
13247 break;
13248 case 2:
2309ddf2 13249 if (check_br16 (abfd, ptr + 4, reg))
df58fc94
RS
13250 break;
13251 continue;
13252 case 4:
2309ddf2 13253 if (check_br32 (abfd, ptr + 4, reg))
df58fc94
RS
13254 break;
13255 continue;
13256 default:
13257 continue;
13258 }
13259
d21911ea 13260 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
df58fc94
RS
13261
13262 /* Give up unless the same register is used with both
13263 relocations. */
13264 if (OP32_SREG (nextopc) != reg)
13265 continue;
13266
13267 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13268 and rounding up to take masking of the two LSBs into account. */
13269 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13270
13271 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13272 if (IS_BITSIZE (symval, 16))
13273 {
13274 /* Fix the relocation's type. */
13275 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13276
13277 /* Instructions using R_MICROMIPS_LO16 have the base or
13278 source register in bits 20:16. This register becomes $0
13279 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13280 nextopc &= ~0x001f0000;
13281 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13282 contents + irel[1].r_offset);
13283 }
13284
13285 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13286 We add 4 to take LUI deletion into account while checking
13287 the PC-relative distance. */
13288 else if (symval % 4 == 0
13289 && IS_BITSIZE (pcrval + 4, 25)
13290 && MATCH (nextopc, addiu_insn)
13291 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13292 && OP16_VALID_REG (OP32_TREG (nextopc)))
13293 {
13294 /* Fix the relocation's type. */
13295 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13296
13297 /* Replace ADDIU with the ADDIUPC version. */
13298 nextopc = (addiupc_insn.match
13299 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13300
d21911ea
MR
13301 bfd_put_micromips_32 (abfd, nextopc,
13302 contents + irel[1].r_offset);
df58fc94
RS
13303 }
13304
13305 /* Can't do anything, give up, sigh... */
13306 else
13307 continue;
13308
13309 /* Fix the relocation's type. */
13310 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13311
13312 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13313 delcnt = 4;
13314 deloff = 0;
13315 }
13316
13317 /* Compact branch relaxation -- due to the multitude of macros
13318 employed by the compiler/assembler, compact branches are not
13319 always generated. Obviously, this can/will be fixed elsewhere,
13320 but there is no drawback in double checking it here. */
13321 else if (r_type == R_MICROMIPS_PC16_S1
13322 && irel->r_offset + 5 < sec->size
13323 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13324 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
833794fc
MR
13325 && ((!insn32
13326 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13327 nop_insn_16) ? 2 : 0))
13328 || (irel->r_offset + 7 < sec->size
13329 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13330 ptr + 4),
13331 nop_insn_32) ? 4 : 0))))
df58fc94
RS
13332 {
13333 unsigned long reg;
13334
13335 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13336
13337 /* Replace BEQZ/BNEZ with the compact version. */
13338 opcode = (bzc_insns_32[fndopc].match
13339 | BZC32_REG_FIELD (reg)
13340 | (opcode & 0xffff)); /* Addend value. */
13341
d21911ea 13342 bfd_put_micromips_32 (abfd, opcode, ptr);
df58fc94 13343
833794fc
MR
13344 /* Delete the delay slot NOP: two or four bytes from
13345 irel->offset + 4; delcnt has already been set above. */
df58fc94
RS
13346 deloff = 4;
13347 }
13348
13349 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
13350 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
13351 else if (!insn32
13352 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
13353 && IS_BITSIZE (pcrval - 2, 11)
13354 && find_match (opcode, b_insns_32) >= 0)
13355 {
13356 /* Fix the relocation's type. */
13357 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13358
a8685210 13359 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
13360 bfd_put_16 (abfd,
13361 (b_insn_16.match
13362 | (opcode & 0x3ff)), /* Addend value. */
2309ddf2 13363 ptr);
df58fc94
RS
13364
13365 /* Delete 2 bytes from irel->r_offset + 2. */
13366 delcnt = 2;
13367 deloff = 2;
13368 }
13369
13370 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
13371 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
13372 else if (!insn32
13373 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
13374 && IS_BITSIZE (pcrval - 2, 8)
13375 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13376 && OP16_VALID_REG (OP32_SREG (opcode)))
13377 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13378 && OP16_VALID_REG (OP32_TREG (opcode)))))
13379 {
13380 unsigned long reg;
13381
13382 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13383
13384 /* Fix the relocation's type. */
13385 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
13386
a8685210 13387 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
13388 bfd_put_16 (abfd,
13389 (bz_insns_16[fndopc].match
13390 | BZ16_REG_FIELD (reg)
13391 | (opcode & 0x7f)), /* Addend value. */
2309ddf2 13392 ptr);
df58fc94
RS
13393
13394 /* Delete 2 bytes from irel->r_offset + 2. */
13395 delcnt = 2;
13396 deloff = 2;
13397 }
13398
13399 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
833794fc
MR
13400 else if (!insn32
13401 && r_type == R_MICROMIPS_26_S1
df58fc94
RS
13402 && target_is_micromips_code_p
13403 && irel->r_offset + 7 < sec->size
13404 && MATCH (opcode, jal_insn_32_bd32))
13405 {
13406 unsigned long n32opc;
13407 bfd_boolean relaxed = FALSE;
13408
d21911ea 13409 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
df58fc94
RS
13410
13411 if (MATCH (n32opc, nop_insn_32))
13412 {
13413 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
2309ddf2 13414 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
df58fc94
RS
13415
13416 relaxed = TRUE;
13417 }
13418 else if (find_match (n32opc, move_insns_32) >= 0)
13419 {
13420 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
13421 bfd_put_16 (abfd,
13422 (move_insn_16.match
13423 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
13424 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
2309ddf2 13425 ptr + 4);
df58fc94
RS
13426
13427 relaxed = TRUE;
13428 }
13429 /* Other 32-bit instructions relaxable to 16-bit
13430 instructions will be handled here later. */
13431
13432 if (relaxed)
13433 {
13434 /* JAL with 32-bit delay slot that is changed to a JALS
13435 with 16-bit delay slot. */
d21911ea 13436 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
df58fc94
RS
13437
13438 /* Delete 2 bytes from irel->r_offset + 6. */
13439 delcnt = 2;
13440 deloff = 6;
13441 }
13442 }
13443
13444 if (delcnt != 0)
13445 {
13446 /* Note that we've changed the relocs, section contents, etc. */
13447 elf_section_data (sec)->relocs = internal_relocs;
13448 elf_section_data (sec)->this_hdr.contents = contents;
13449 symtab_hdr->contents = (unsigned char *) isymbuf;
13450
13451 /* Delete bytes depending on the delcnt and deloff. */
13452 if (!mips_elf_relax_delete_bytes (abfd, sec,
13453 irel->r_offset + deloff, delcnt))
13454 goto error_return;
13455
13456 /* That will change things, so we should relax again.
13457 Note that this is not required, and it may be slow. */
13458 *again = TRUE;
13459 }
13460 }
13461
13462 if (isymbuf != NULL
13463 && symtab_hdr->contents != (unsigned char *) isymbuf)
13464 {
13465 if (! link_info->keep_memory)
13466 free (isymbuf);
13467 else
13468 {
13469 /* Cache the symbols for elf_link_input_bfd. */
13470 symtab_hdr->contents = (unsigned char *) isymbuf;
13471 }
13472 }
13473
13474 if (contents != NULL
13475 && elf_section_data (sec)->this_hdr.contents != contents)
13476 {
13477 if (! link_info->keep_memory)
13478 free (contents);
13479 else
13480 {
13481 /* Cache the section contents for elf_link_input_bfd. */
13482 elf_section_data (sec)->this_hdr.contents = contents;
13483 }
13484 }
13485
13486 if (internal_relocs != NULL
13487 && elf_section_data (sec)->relocs != internal_relocs)
13488 free (internal_relocs);
13489
13490 return TRUE;
13491
13492 error_return:
13493 if (isymbuf != NULL
13494 && symtab_hdr->contents != (unsigned char *) isymbuf)
13495 free (isymbuf);
13496 if (contents != NULL
13497 && elf_section_data (sec)->this_hdr.contents != contents)
13498 free (contents);
13499 if (internal_relocs != NULL
13500 && elf_section_data (sec)->relocs != internal_relocs)
13501 free (internal_relocs);
13502
13503 return FALSE;
13504}
13505\f
b49e97c9
TS
13506/* Create a MIPS ELF linker hash table. */
13507
13508struct bfd_link_hash_table *
9719ad41 13509_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
13510{
13511 struct mips_elf_link_hash_table *ret;
13512 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
13513
7bf52ea2 13514 ret = bfd_zmalloc (amt);
9719ad41 13515 if (ret == NULL)
b49e97c9
TS
13516 return NULL;
13517
66eb6687
AM
13518 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
13519 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
13520 sizeof (struct mips_elf_link_hash_entry),
13521 MIPS_ELF_DATA))
b49e97c9 13522 {
e2d34d7d 13523 free (ret);
b49e97c9
TS
13524 return NULL;
13525 }
1bbce132
MR
13526 ret->root.init_plt_refcount.plist = NULL;
13527 ret->root.init_plt_offset.plist = NULL;
b49e97c9 13528
b49e97c9
TS
13529 return &ret->root.root;
13530}
0a44bf69
RS
13531
13532/* Likewise, but indicate that the target is VxWorks. */
13533
13534struct bfd_link_hash_table *
13535_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
13536{
13537 struct bfd_link_hash_table *ret;
13538
13539 ret = _bfd_mips_elf_link_hash_table_create (abfd);
13540 if (ret)
13541 {
13542 struct mips_elf_link_hash_table *htab;
13543
13544 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
13545 htab->use_plts_and_copy_relocs = TRUE;
13546 htab->is_vxworks = TRUE;
0a44bf69
RS
13547 }
13548 return ret;
13549}
861fb55a
DJ
13550
13551/* A function that the linker calls if we are allowed to use PLTs
13552 and copy relocs. */
13553
13554void
13555_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
13556{
13557 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
13558}
833794fc
MR
13559
13560/* A function that the linker calls to select between all or only
13561 32-bit microMIPS instructions. */
13562
13563void
13564_bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
13565{
13566 mips_elf_hash_table (info)->insn32 = on;
13567}
b49e97c9
TS
13568\f
13569/* We need to use a special link routine to handle the .reginfo and
13570 the .mdebug sections. We need to merge all instances of these
13571 sections together, not write them all out sequentially. */
13572
b34976b6 13573bfd_boolean
9719ad41 13574_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 13575{
b49e97c9
TS
13576 asection *o;
13577 struct bfd_link_order *p;
13578 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
13579 asection *rtproc_sec;
13580 Elf32_RegInfo reginfo;
13581 struct ecoff_debug_info debug;
861fb55a 13582 struct mips_htab_traverse_info hti;
7a2a6943
NC
13583 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13584 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 13585 HDRR *symhdr = &debug.symbolic_header;
9719ad41 13586 void *mdebug_handle = NULL;
b49e97c9
TS
13587 asection *s;
13588 EXTR esym;
13589 unsigned int i;
13590 bfd_size_type amt;
0a44bf69 13591 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
13592
13593 static const char * const secname[] =
13594 {
13595 ".text", ".init", ".fini", ".data",
13596 ".rodata", ".sdata", ".sbss", ".bss"
13597 };
13598 static const int sc[] =
13599 {
13600 scText, scInit, scFini, scData,
13601 scRData, scSData, scSBss, scBss
13602 };
13603
d4596a51
RS
13604 /* Sort the dynamic symbols so that those with GOT entries come after
13605 those without. */
0a44bf69 13606 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
13607 BFD_ASSERT (htab != NULL);
13608
d4596a51
RS
13609 if (!mips_elf_sort_hash_table (abfd, info))
13610 return FALSE;
b49e97c9 13611
861fb55a
DJ
13612 /* Create any scheduled LA25 stubs. */
13613 hti.info = info;
13614 hti.output_bfd = abfd;
13615 hti.error = FALSE;
13616 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
13617 if (hti.error)
13618 return FALSE;
13619
b49e97c9
TS
13620 /* Get a value for the GP register. */
13621 if (elf_gp (abfd) == 0)
13622 {
13623 struct bfd_link_hash_entry *h;
13624
b34976b6 13625 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 13626 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
13627 elf_gp (abfd) = (h->u.def.value
13628 + h->u.def.section->output_section->vma
13629 + h->u.def.section->output_offset);
0a44bf69
RS
13630 else if (htab->is_vxworks
13631 && (h = bfd_link_hash_lookup (info->hash,
13632 "_GLOBAL_OFFSET_TABLE_",
13633 FALSE, FALSE, TRUE))
13634 && h->type == bfd_link_hash_defined)
13635 elf_gp (abfd) = (h->u.def.section->output_section->vma
13636 + h->u.def.section->output_offset
13637 + h->u.def.value);
1049f94e 13638 else if (info->relocatable)
b49e97c9
TS
13639 {
13640 bfd_vma lo = MINUS_ONE;
13641
13642 /* Find the GP-relative section with the lowest offset. */
9719ad41 13643 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
13644 if (o->vma < lo
13645 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
13646 lo = o->vma;
13647
13648 /* And calculate GP relative to that. */
0a44bf69 13649 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
13650 }
13651 else
13652 {
13653 /* If the relocate_section function needs to do a reloc
13654 involving the GP value, it should make a reloc_dangerous
13655 callback to warn that GP is not defined. */
13656 }
13657 }
13658
13659 /* Go through the sections and collect the .reginfo and .mdebug
13660 information. */
13661 reginfo_sec = NULL;
13662 mdebug_sec = NULL;
13663 gptab_data_sec = NULL;
13664 gptab_bss_sec = NULL;
9719ad41 13665 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
13666 {
13667 if (strcmp (o->name, ".reginfo") == 0)
13668 {
13669 memset (&reginfo, 0, sizeof reginfo);
13670
13671 /* We have found the .reginfo section in the output file.
13672 Look through all the link_orders comprising it and merge
13673 the information together. */
8423293d 13674 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13675 {
13676 asection *input_section;
13677 bfd *input_bfd;
13678 Elf32_External_RegInfo ext;
13679 Elf32_RegInfo sub;
13680
13681 if (p->type != bfd_indirect_link_order)
13682 {
13683 if (p->type == bfd_data_link_order)
13684 continue;
13685 abort ();
13686 }
13687
13688 input_section = p->u.indirect.section;
13689 input_bfd = input_section->owner;
13690
b49e97c9 13691 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 13692 &ext, 0, sizeof ext))
b34976b6 13693 return FALSE;
b49e97c9
TS
13694
13695 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
13696
13697 reginfo.ri_gprmask |= sub.ri_gprmask;
13698 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
13699 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
13700 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
13701 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
13702
13703 /* ri_gp_value is set by the function
13704 mips_elf32_section_processing when the section is
13705 finally written out. */
13706
13707 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13708 elf_link_input_bfd ignores this section. */
13709 input_section->flags &= ~SEC_HAS_CONTENTS;
13710 }
13711
13712 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 13713 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
13714
13715 /* Skip this section later on (I don't think this currently
13716 matters, but someday it might). */
8423293d 13717 o->map_head.link_order = NULL;
b49e97c9
TS
13718
13719 reginfo_sec = o;
13720 }
13721
13722 if (strcmp (o->name, ".mdebug") == 0)
13723 {
13724 struct extsym_info einfo;
13725 bfd_vma last;
13726
13727 /* We have found the .mdebug section in the output file.
13728 Look through all the link_orders comprising it and merge
13729 the information together. */
13730 symhdr->magic = swap->sym_magic;
13731 /* FIXME: What should the version stamp be? */
13732 symhdr->vstamp = 0;
13733 symhdr->ilineMax = 0;
13734 symhdr->cbLine = 0;
13735 symhdr->idnMax = 0;
13736 symhdr->ipdMax = 0;
13737 symhdr->isymMax = 0;
13738 symhdr->ioptMax = 0;
13739 symhdr->iauxMax = 0;
13740 symhdr->issMax = 0;
13741 symhdr->issExtMax = 0;
13742 symhdr->ifdMax = 0;
13743 symhdr->crfd = 0;
13744 symhdr->iextMax = 0;
13745
13746 /* We accumulate the debugging information itself in the
13747 debug_info structure. */
13748 debug.line = NULL;
13749 debug.external_dnr = NULL;
13750 debug.external_pdr = NULL;
13751 debug.external_sym = NULL;
13752 debug.external_opt = NULL;
13753 debug.external_aux = NULL;
13754 debug.ss = NULL;
13755 debug.ssext = debug.ssext_end = NULL;
13756 debug.external_fdr = NULL;
13757 debug.external_rfd = NULL;
13758 debug.external_ext = debug.external_ext_end = NULL;
13759
13760 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 13761 if (mdebug_handle == NULL)
b34976b6 13762 return FALSE;
b49e97c9
TS
13763
13764 esym.jmptbl = 0;
13765 esym.cobol_main = 0;
13766 esym.weakext = 0;
13767 esym.reserved = 0;
13768 esym.ifd = ifdNil;
13769 esym.asym.iss = issNil;
13770 esym.asym.st = stLocal;
13771 esym.asym.reserved = 0;
13772 esym.asym.index = indexNil;
13773 last = 0;
13774 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13775 {
13776 esym.asym.sc = sc[i];
13777 s = bfd_get_section_by_name (abfd, secname[i]);
13778 if (s != NULL)
13779 {
13780 esym.asym.value = s->vma;
eea6121a 13781 last = s->vma + s->size;
b49e97c9
TS
13782 }
13783 else
13784 esym.asym.value = last;
13785 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13786 secname[i], &esym))
b34976b6 13787 return FALSE;
b49e97c9
TS
13788 }
13789
8423293d 13790 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13791 {
13792 asection *input_section;
13793 bfd *input_bfd;
13794 const struct ecoff_debug_swap *input_swap;
13795 struct ecoff_debug_info input_debug;
13796 char *eraw_src;
13797 char *eraw_end;
13798
13799 if (p->type != bfd_indirect_link_order)
13800 {
13801 if (p->type == bfd_data_link_order)
13802 continue;
13803 abort ();
13804 }
13805
13806 input_section = p->u.indirect.section;
13807 input_bfd = input_section->owner;
13808
d5eaccd7 13809 if (!is_mips_elf (input_bfd))
b49e97c9
TS
13810 {
13811 /* I don't know what a non MIPS ELF bfd would be
13812 doing with a .mdebug section, but I don't really
13813 want to deal with it. */
13814 continue;
13815 }
13816
13817 input_swap = (get_elf_backend_data (input_bfd)
13818 ->elf_backend_ecoff_debug_swap);
13819
eea6121a 13820 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
13821
13822 /* The ECOFF linking code expects that we have already
13823 read in the debugging information and set up an
13824 ecoff_debug_info structure, so we do that now. */
13825 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13826 &input_debug))
b34976b6 13827 return FALSE;
b49e97c9
TS
13828
13829 if (! (bfd_ecoff_debug_accumulate
13830 (mdebug_handle, abfd, &debug, swap, input_bfd,
13831 &input_debug, input_swap, info)))
b34976b6 13832 return FALSE;
b49e97c9
TS
13833
13834 /* Loop through the external symbols. For each one with
13835 interesting information, try to find the symbol in
13836 the linker global hash table and save the information
13837 for the output external symbols. */
13838 eraw_src = input_debug.external_ext;
13839 eraw_end = (eraw_src
13840 + (input_debug.symbolic_header.iextMax
13841 * input_swap->external_ext_size));
13842 for (;
13843 eraw_src < eraw_end;
13844 eraw_src += input_swap->external_ext_size)
13845 {
13846 EXTR ext;
13847 const char *name;
13848 struct mips_elf_link_hash_entry *h;
13849
9719ad41 13850 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
13851 if (ext.asym.sc == scNil
13852 || ext.asym.sc == scUndefined
13853 || ext.asym.sc == scSUndefined)
13854 continue;
13855
13856 name = input_debug.ssext + ext.asym.iss;
13857 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 13858 name, FALSE, FALSE, TRUE);
b49e97c9
TS
13859 if (h == NULL || h->esym.ifd != -2)
13860 continue;
13861
13862 if (ext.ifd != -1)
13863 {
13864 BFD_ASSERT (ext.ifd
13865 < input_debug.symbolic_header.ifdMax);
13866 ext.ifd = input_debug.ifdmap[ext.ifd];
13867 }
13868
13869 h->esym = ext;
13870 }
13871
13872 /* Free up the information we just read. */
13873 free (input_debug.line);
13874 free (input_debug.external_dnr);
13875 free (input_debug.external_pdr);
13876 free (input_debug.external_sym);
13877 free (input_debug.external_opt);
13878 free (input_debug.external_aux);
13879 free (input_debug.ss);
13880 free (input_debug.ssext);
13881 free (input_debug.external_fdr);
13882 free (input_debug.external_rfd);
13883 free (input_debug.external_ext);
13884
13885 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13886 elf_link_input_bfd ignores this section. */
13887 input_section->flags &= ~SEC_HAS_CONTENTS;
13888 }
13889
13890 if (SGI_COMPAT (abfd) && info->shared)
13891 {
13892 /* Create .rtproc section. */
87e0a731 13893 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
b49e97c9
TS
13894 if (rtproc_sec == NULL)
13895 {
13896 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13897 | SEC_LINKER_CREATED | SEC_READONLY);
13898
87e0a731
AM
13899 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
13900 ".rtproc",
13901 flags);
b49e97c9 13902 if (rtproc_sec == NULL
b49e97c9 13903 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 13904 return FALSE;
b49e97c9
TS
13905 }
13906
13907 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13908 info, rtproc_sec,
13909 &debug))
b34976b6 13910 return FALSE;
b49e97c9
TS
13911 }
13912
13913 /* Build the external symbol information. */
13914 einfo.abfd = abfd;
13915 einfo.info = info;
13916 einfo.debug = &debug;
13917 einfo.swap = swap;
b34976b6 13918 einfo.failed = FALSE;
b49e97c9 13919 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 13920 mips_elf_output_extsym, &einfo);
b49e97c9 13921 if (einfo.failed)
b34976b6 13922 return FALSE;
b49e97c9
TS
13923
13924 /* Set the size of the .mdebug section. */
eea6121a 13925 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
13926
13927 /* Skip this section later on (I don't think this currently
13928 matters, but someday it might). */
8423293d 13929 o->map_head.link_order = NULL;
b49e97c9
TS
13930
13931 mdebug_sec = o;
13932 }
13933
0112cd26 13934 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
13935 {
13936 const char *subname;
13937 unsigned int c;
13938 Elf32_gptab *tab;
13939 Elf32_External_gptab *ext_tab;
13940 unsigned int j;
13941
13942 /* The .gptab.sdata and .gptab.sbss sections hold
13943 information describing how the small data area would
13944 change depending upon the -G switch. These sections
13945 not used in executables files. */
1049f94e 13946 if (! info->relocatable)
b49e97c9 13947 {
8423293d 13948 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13949 {
13950 asection *input_section;
13951
13952 if (p->type != bfd_indirect_link_order)
13953 {
13954 if (p->type == bfd_data_link_order)
13955 continue;
13956 abort ();
13957 }
13958
13959 input_section = p->u.indirect.section;
13960
13961 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13962 elf_link_input_bfd ignores this section. */
13963 input_section->flags &= ~SEC_HAS_CONTENTS;
13964 }
13965
13966 /* Skip this section later on (I don't think this
13967 currently matters, but someday it might). */
8423293d 13968 o->map_head.link_order = NULL;
b49e97c9
TS
13969
13970 /* Really remove the section. */
5daa8fe7 13971 bfd_section_list_remove (abfd, o);
b49e97c9
TS
13972 --abfd->section_count;
13973
13974 continue;
13975 }
13976
13977 /* There is one gptab for initialized data, and one for
13978 uninitialized data. */
13979 if (strcmp (o->name, ".gptab.sdata") == 0)
13980 gptab_data_sec = o;
13981 else if (strcmp (o->name, ".gptab.sbss") == 0)
13982 gptab_bss_sec = o;
13983 else
13984 {
13985 (*_bfd_error_handler)
13986 (_("%s: illegal section name `%s'"),
13987 bfd_get_filename (abfd), o->name);
13988 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 13989 return FALSE;
b49e97c9
TS
13990 }
13991
13992 /* The linker script always combines .gptab.data and
13993 .gptab.sdata into .gptab.sdata, and likewise for
13994 .gptab.bss and .gptab.sbss. It is possible that there is
13995 no .sdata or .sbss section in the output file, in which
13996 case we must change the name of the output section. */
13997 subname = o->name + sizeof ".gptab" - 1;
13998 if (bfd_get_section_by_name (abfd, subname) == NULL)
13999 {
14000 if (o == gptab_data_sec)
14001 o->name = ".gptab.data";
14002 else
14003 o->name = ".gptab.bss";
14004 subname = o->name + sizeof ".gptab" - 1;
14005 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14006 }
14007
14008 /* Set up the first entry. */
14009 c = 1;
14010 amt = c * sizeof (Elf32_gptab);
9719ad41 14011 tab = bfd_malloc (amt);
b49e97c9 14012 if (tab == NULL)
b34976b6 14013 return FALSE;
b49e97c9
TS
14014 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14015 tab[0].gt_header.gt_unused = 0;
14016
14017 /* Combine the input sections. */
8423293d 14018 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14019 {
14020 asection *input_section;
14021 bfd *input_bfd;
14022 bfd_size_type size;
14023 unsigned long last;
14024 bfd_size_type gpentry;
14025
14026 if (p->type != bfd_indirect_link_order)
14027 {
14028 if (p->type == bfd_data_link_order)
14029 continue;
14030 abort ();
14031 }
14032
14033 input_section = p->u.indirect.section;
14034 input_bfd = input_section->owner;
14035
14036 /* Combine the gptab entries for this input section one
14037 by one. We know that the input gptab entries are
14038 sorted by ascending -G value. */
eea6121a 14039 size = input_section->size;
b49e97c9
TS
14040 last = 0;
14041 for (gpentry = sizeof (Elf32_External_gptab);
14042 gpentry < size;
14043 gpentry += sizeof (Elf32_External_gptab))
14044 {
14045 Elf32_External_gptab ext_gptab;
14046 Elf32_gptab int_gptab;
14047 unsigned long val;
14048 unsigned long add;
b34976b6 14049 bfd_boolean exact;
b49e97c9
TS
14050 unsigned int look;
14051
14052 if (! (bfd_get_section_contents
9719ad41
RS
14053 (input_bfd, input_section, &ext_gptab, gpentry,
14054 sizeof (Elf32_External_gptab))))
b49e97c9
TS
14055 {
14056 free (tab);
b34976b6 14057 return FALSE;
b49e97c9
TS
14058 }
14059
14060 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
14061 &int_gptab);
14062 val = int_gptab.gt_entry.gt_g_value;
14063 add = int_gptab.gt_entry.gt_bytes - last;
14064
b34976b6 14065 exact = FALSE;
b49e97c9
TS
14066 for (look = 1; look < c; look++)
14067 {
14068 if (tab[look].gt_entry.gt_g_value >= val)
14069 tab[look].gt_entry.gt_bytes += add;
14070
14071 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 14072 exact = TRUE;
b49e97c9
TS
14073 }
14074
14075 if (! exact)
14076 {
14077 Elf32_gptab *new_tab;
14078 unsigned int max;
14079
14080 /* We need a new table entry. */
14081 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 14082 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
14083 if (new_tab == NULL)
14084 {
14085 free (tab);
b34976b6 14086 return FALSE;
b49e97c9
TS
14087 }
14088 tab = new_tab;
14089 tab[c].gt_entry.gt_g_value = val;
14090 tab[c].gt_entry.gt_bytes = add;
14091
14092 /* Merge in the size for the next smallest -G
14093 value, since that will be implied by this new
14094 value. */
14095 max = 0;
14096 for (look = 1; look < c; look++)
14097 {
14098 if (tab[look].gt_entry.gt_g_value < val
14099 && (max == 0
14100 || (tab[look].gt_entry.gt_g_value
14101 > tab[max].gt_entry.gt_g_value)))
14102 max = look;
14103 }
14104 if (max != 0)
14105 tab[c].gt_entry.gt_bytes +=
14106 tab[max].gt_entry.gt_bytes;
14107
14108 ++c;
14109 }
14110
14111 last = int_gptab.gt_entry.gt_bytes;
14112 }
14113
14114 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14115 elf_link_input_bfd ignores this section. */
14116 input_section->flags &= ~SEC_HAS_CONTENTS;
14117 }
14118
14119 /* The table must be sorted by -G value. */
14120 if (c > 2)
14121 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
14122
14123 /* Swap out the table. */
14124 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 14125 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
14126 if (ext_tab == NULL)
14127 {
14128 free (tab);
b34976b6 14129 return FALSE;
b49e97c9
TS
14130 }
14131
14132 for (j = 0; j < c; j++)
14133 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
14134 free (tab);
14135
eea6121a 14136 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
14137 o->contents = (bfd_byte *) ext_tab;
14138
14139 /* Skip this section later on (I don't think this currently
14140 matters, but someday it might). */
8423293d 14141 o->map_head.link_order = NULL;
b49e97c9
TS
14142 }
14143 }
14144
14145 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 14146 if (!bfd_elf_final_link (abfd, info))
b34976b6 14147 return FALSE;
b49e97c9
TS
14148
14149 /* Now write out the computed sections. */
14150
9719ad41 14151 if (reginfo_sec != NULL)
b49e97c9
TS
14152 {
14153 Elf32_External_RegInfo ext;
14154
14155 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 14156 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 14157 return FALSE;
b49e97c9
TS
14158 }
14159
9719ad41 14160 if (mdebug_sec != NULL)
b49e97c9
TS
14161 {
14162 BFD_ASSERT (abfd->output_has_begun);
14163 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
14164 swap, info,
14165 mdebug_sec->filepos))
b34976b6 14166 return FALSE;
b49e97c9
TS
14167
14168 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
14169 }
14170
9719ad41 14171 if (gptab_data_sec != NULL)
b49e97c9
TS
14172 {
14173 if (! bfd_set_section_contents (abfd, gptab_data_sec,
14174 gptab_data_sec->contents,
eea6121a 14175 0, gptab_data_sec->size))
b34976b6 14176 return FALSE;
b49e97c9
TS
14177 }
14178
9719ad41 14179 if (gptab_bss_sec != NULL)
b49e97c9
TS
14180 {
14181 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
14182 gptab_bss_sec->contents,
eea6121a 14183 0, gptab_bss_sec->size))
b34976b6 14184 return FALSE;
b49e97c9
TS
14185 }
14186
14187 if (SGI_COMPAT (abfd))
14188 {
14189 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
14190 if (rtproc_sec != NULL)
14191 {
14192 if (! bfd_set_section_contents (abfd, rtproc_sec,
14193 rtproc_sec->contents,
eea6121a 14194 0, rtproc_sec->size))
b34976b6 14195 return FALSE;
b49e97c9
TS
14196 }
14197 }
14198
b34976b6 14199 return TRUE;
b49e97c9
TS
14200}
14201\f
64543e1a
RS
14202/* Structure for saying that BFD machine EXTENSION extends BASE. */
14203
a253d456
NC
14204struct mips_mach_extension
14205{
64543e1a
RS
14206 unsigned long extension, base;
14207};
14208
14209
14210/* An array describing how BFD machines relate to one another. The entries
14211 are ordered topologically with MIPS I extensions listed last. */
14212
a253d456
NC
14213static const struct mips_mach_extension mips_mach_extensions[] =
14214{
6f179bd0 14215 /* MIPS64r2 extensions. */
432233b3 14216 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
dd6a37e7 14217 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
6f179bd0
AN
14218 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14219
64543e1a 14220 /* MIPS64 extensions. */
5f74bc13 14221 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 14222 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 14223 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
fd503541 14224 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
64543e1a
RS
14225
14226 /* MIPS V extensions. */
14227 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14228
14229 /* R10000 extensions. */
14230 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
14231 { bfd_mach_mips14000, bfd_mach_mips10000 },
14232 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
14233
14234 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14235 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14236 better to allow vr5400 and vr5500 code to be merged anyway, since
14237 many libraries will just use the core ISA. Perhaps we could add
14238 some sort of ASE flag if this ever proves a problem. */
14239 { bfd_mach_mips5500, bfd_mach_mips5400 },
14240 { bfd_mach_mips5400, bfd_mach_mips5000 },
14241
14242 /* MIPS IV extensions. */
14243 { bfd_mach_mips5, bfd_mach_mips8000 },
14244 { bfd_mach_mips10000, bfd_mach_mips8000 },
14245 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 14246 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 14247 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
14248
14249 /* VR4100 extensions. */
14250 { bfd_mach_mips4120, bfd_mach_mips4100 },
14251 { bfd_mach_mips4111, bfd_mach_mips4100 },
14252
14253 /* MIPS III extensions. */
350cc38d
MS
14254 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14255 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
14256 { bfd_mach_mips8000, bfd_mach_mips4000 },
14257 { bfd_mach_mips4650, bfd_mach_mips4000 },
14258 { bfd_mach_mips4600, bfd_mach_mips4000 },
14259 { bfd_mach_mips4400, bfd_mach_mips4000 },
14260 { bfd_mach_mips4300, bfd_mach_mips4000 },
14261 { bfd_mach_mips4100, bfd_mach_mips4000 },
14262 { bfd_mach_mips4010, bfd_mach_mips4000 },
e407c74b 14263 { bfd_mach_mips5900, bfd_mach_mips4000 },
64543e1a
RS
14264
14265 /* MIPS32 extensions. */
14266 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14267
14268 /* MIPS II extensions. */
14269 { bfd_mach_mips4000, bfd_mach_mips6000 },
14270 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14271
14272 /* MIPS I extensions. */
14273 { bfd_mach_mips6000, bfd_mach_mips3000 },
14274 { bfd_mach_mips3900, bfd_mach_mips3000 }
14275};
14276
14277
14278/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14279
14280static bfd_boolean
9719ad41 14281mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
14282{
14283 size_t i;
14284
c5211a54
RS
14285 if (extension == base)
14286 return TRUE;
14287
14288 if (base == bfd_mach_mipsisa32
14289 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14290 return TRUE;
14291
14292 if (base == bfd_mach_mipsisa32r2
14293 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14294 return TRUE;
14295
14296 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 14297 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
14298 {
14299 extension = mips_mach_extensions[i].base;
14300 if (extension == base)
14301 return TRUE;
14302 }
64543e1a 14303
c5211a54 14304 return FALSE;
64543e1a
RS
14305}
14306
14307
14308/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 14309
b34976b6 14310static bfd_boolean
9719ad41 14311mips_32bit_flags_p (flagword flags)
00707a0e 14312{
64543e1a
RS
14313 return ((flags & EF_MIPS_32BITMODE) != 0
14314 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14315 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14316 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14317 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14318 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14319 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
14320}
14321
64543e1a 14322
2cf19d5c
JM
14323/* Merge object attributes from IBFD into OBFD. Raise an error if
14324 there are conflicting attributes. */
14325static bfd_boolean
14326mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
14327{
14328 obj_attribute *in_attr;
14329 obj_attribute *out_attr;
6ae68ba3
MR
14330 bfd *abi_fp_bfd;
14331
14332 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
14333 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
d929bc19 14334 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
6ae68ba3 14335 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
2cf19d5c
JM
14336
14337 if (!elf_known_obj_attributes_proc (obfd)[0].i)
14338 {
14339 /* This is the first object. Copy the attributes. */
14340 _bfd_elf_copy_obj_attributes (ibfd, obfd);
14341
14342 /* Use the Tag_null value to indicate the attributes have been
14343 initialized. */
14344 elf_known_obj_attributes_proc (obfd)[0].i = 1;
14345
14346 return TRUE;
14347 }
14348
14349 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
14350 non-conflicting ones. */
2cf19d5c
JM
14351 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14352 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
14353 {
14354 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
d929bc19 14355 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
2cf19d5c 14356 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
d929bc19 14357 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
2cf19d5c
JM
14358 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
14359 {
d929bc19 14360 case Val_GNU_MIPS_ABI_FP_DOUBLE:
2cf19d5c
JM
14361 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14362 {
d929bc19 14363 case Val_GNU_MIPS_ABI_FP_SINGLE:
2cf19d5c 14364 _bfd_error_handler
6ae68ba3
MR
14365 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14366 obfd, abi_fp_bfd, ibfd, "-mdouble-float", "-msingle-float");
51a0dd31 14367 break;
2cf19d5c 14368
d929bc19 14369 case Val_GNU_MIPS_ABI_FP_SOFT:
2cf19d5c 14370 _bfd_error_handler
6ae68ba3
MR
14371 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14372 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
2cf19d5c
JM
14373 break;
14374
d929bc19 14375 case Val_GNU_MIPS_ABI_FP_64:
42554f6a 14376 _bfd_error_handler
6ae68ba3
MR
14377 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14378 obfd, abi_fp_bfd, ibfd,
14379 "-mdouble-float", "-mips32r2 -mfp64");
42554f6a
TS
14380 break;
14381
2cf19d5c 14382 default:
6ae68ba3
MR
14383 _bfd_error_handler
14384 (_("Warning: %B uses %s (set by %B), "
14385 "%B uses unknown floating point ABI %d"),
14386 obfd, abi_fp_bfd, ibfd,
14387 "-mdouble-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14388 break;
2cf19d5c
JM
14389 }
14390 break;
14391
d929bc19 14392 case Val_GNU_MIPS_ABI_FP_SINGLE:
2cf19d5c
JM
14393 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14394 {
d929bc19 14395 case Val_GNU_MIPS_ABI_FP_DOUBLE:
2cf19d5c 14396 _bfd_error_handler
6ae68ba3
MR
14397 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14398 obfd, abi_fp_bfd, ibfd, "-msingle-float", "-mdouble-float");
51a0dd31 14399 break;
2cf19d5c 14400
d929bc19 14401 case Val_GNU_MIPS_ABI_FP_SOFT:
2cf19d5c 14402 _bfd_error_handler
6ae68ba3
MR
14403 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14404 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
2cf19d5c
JM
14405 break;
14406
d929bc19 14407 case Val_GNU_MIPS_ABI_FP_64:
42554f6a 14408 _bfd_error_handler
6ae68ba3
MR
14409 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14410 obfd, abi_fp_bfd, ibfd,
14411 "-msingle-float", "-mips32r2 -mfp64");
42554f6a
TS
14412 break;
14413
2cf19d5c 14414 default:
6ae68ba3
MR
14415 _bfd_error_handler
14416 (_("Warning: %B uses %s (set by %B), "
14417 "%B uses unknown floating point ABI %d"),
14418 obfd, abi_fp_bfd, ibfd,
14419 "-msingle-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14420 break;
2cf19d5c
JM
14421 }
14422 break;
14423
d929bc19 14424 case Val_GNU_MIPS_ABI_FP_SOFT:
2cf19d5c
JM
14425 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14426 {
d929bc19
MR
14427 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14428 case Val_GNU_MIPS_ABI_FP_SINGLE:
14429 case Val_GNU_MIPS_ABI_FP_64:
2cf19d5c 14430 _bfd_error_handler
6ae68ba3
MR
14431 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14432 obfd, abi_fp_bfd, ibfd, "-msoft-float", "-mhard-float");
2cf19d5c
JM
14433 break;
14434
14435 default:
6ae68ba3
MR
14436 _bfd_error_handler
14437 (_("Warning: %B uses %s (set by %B), "
14438 "%B uses unknown floating point ABI %d"),
14439 obfd, abi_fp_bfd, ibfd,
14440 "-msoft-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14441 break;
2cf19d5c
JM
14442 }
14443 break;
14444
d929bc19 14445 case Val_GNU_MIPS_ABI_FP_64:
42554f6a
TS
14446 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14447 {
d929bc19 14448 case Val_GNU_MIPS_ABI_FP_DOUBLE:
42554f6a 14449 _bfd_error_handler
6ae68ba3
MR
14450 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14451 obfd, abi_fp_bfd, ibfd,
14452 "-mips32r2 -mfp64", "-mdouble-float");
42554f6a
TS
14453 break;
14454
d929bc19 14455 case Val_GNU_MIPS_ABI_FP_SINGLE:
42554f6a 14456 _bfd_error_handler
6ae68ba3
MR
14457 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14458 obfd, abi_fp_bfd, ibfd,
14459 "-mips32r2 -mfp64", "-msingle-float");
42554f6a
TS
14460 break;
14461
d929bc19 14462 case Val_GNU_MIPS_ABI_FP_SOFT:
42554f6a 14463 _bfd_error_handler
6ae68ba3
MR
14464 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14465 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
42554f6a
TS
14466 break;
14467
14468 default:
6ae68ba3
MR
14469 _bfd_error_handler
14470 (_("Warning: %B uses %s (set by %B), "
14471 "%B uses unknown floating point ABI %d"),
14472 obfd, abi_fp_bfd, ibfd,
14473 "-mips32r2 -mfp64", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14474 break;
42554f6a
TS
14475 }
14476 break;
14477
2cf19d5c 14478 default:
6ae68ba3
MR
14479 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14480 {
d929bc19 14481 case Val_GNU_MIPS_ABI_FP_DOUBLE:
6ae68ba3
MR
14482 _bfd_error_handler
14483 (_("Warning: %B uses unknown floating point ABI %d "
14484 "(set by %B), %B uses %s"),
14485 obfd, abi_fp_bfd, ibfd,
14486 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mdouble-float");
14487 break;
14488
d929bc19 14489 case Val_GNU_MIPS_ABI_FP_SINGLE:
6ae68ba3
MR
14490 _bfd_error_handler
14491 (_("Warning: %B uses unknown floating point ABI %d "
14492 "(set by %B), %B uses %s"),
14493 obfd, abi_fp_bfd, ibfd,
14494 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msingle-float");
14495 break;
14496
d929bc19 14497 case Val_GNU_MIPS_ABI_FP_SOFT:
6ae68ba3
MR
14498 _bfd_error_handler
14499 (_("Warning: %B uses unknown floating point ABI %d "
14500 "(set by %B), %B uses %s"),
14501 obfd, abi_fp_bfd, ibfd,
14502 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msoft-float");
14503 break;
14504
d929bc19 14505 case Val_GNU_MIPS_ABI_FP_64:
6ae68ba3
MR
14506 _bfd_error_handler
14507 (_("Warning: %B uses unknown floating point ABI %d "
14508 "(set by %B), %B uses %s"),
14509 obfd, abi_fp_bfd, ibfd,
14510 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mips32r2 -mfp64");
14511 break;
14512
14513 default:
14514 _bfd_error_handler
14515 (_("Warning: %B uses unknown floating point ABI %d "
14516 "(set by %B), %B uses unknown floating point ABI %d"),
14517 obfd, abi_fp_bfd, ibfd,
14518 out_attr[Tag_GNU_MIPS_ABI_FP].i,
14519 in_attr[Tag_GNU_MIPS_ABI_FP].i);
14520 break;
14521 }
14522 break;
2cf19d5c
JM
14523 }
14524 }
14525
14526 /* Merge Tag_compatibility attributes and any common GNU ones. */
14527 _bfd_elf_merge_object_attributes (ibfd, obfd);
14528
14529 return TRUE;
14530}
14531
b49e97c9
TS
14532/* Merge backend specific data from an object file to the output
14533 object file when linking. */
14534
b34976b6 14535bfd_boolean
9719ad41 14536_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
14537{
14538 flagword old_flags;
14539 flagword new_flags;
b34976b6
AM
14540 bfd_boolean ok;
14541 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
14542 asection *sec;
14543
58238693 14544 /* Check if we have the same endianness. */
82e51918 14545 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
14546 {
14547 (*_bfd_error_handler)
d003868e
AM
14548 (_("%B: endianness incompatible with that of the selected emulation"),
14549 ibfd);
aa701218
AO
14550 return FALSE;
14551 }
b49e97c9 14552
d5eaccd7 14553 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 14554 return TRUE;
b49e97c9 14555
aa701218
AO
14556 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
14557 {
14558 (*_bfd_error_handler)
d003868e
AM
14559 (_("%B: ABI is incompatible with that of the selected emulation"),
14560 ibfd);
aa701218
AO
14561 return FALSE;
14562 }
14563
2cf19d5c
JM
14564 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
14565 return FALSE;
14566
b49e97c9
TS
14567 new_flags = elf_elfheader (ibfd)->e_flags;
14568 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
14569 old_flags = elf_elfheader (obfd)->e_flags;
14570
14571 if (! elf_flags_init (obfd))
14572 {
b34976b6 14573 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
14574 elf_elfheader (obfd)->e_flags = new_flags;
14575 elf_elfheader (obfd)->e_ident[EI_CLASS]
14576 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
14577
14578 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861 14579 && (bfd_get_arch_info (obfd)->the_default
68ffbac6 14580 || mips_mach_extends_p (bfd_get_mach (obfd),
2907b861 14581 bfd_get_mach (ibfd))))
b49e97c9
TS
14582 {
14583 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
14584 bfd_get_mach (ibfd)))
b34976b6 14585 return FALSE;
b49e97c9
TS
14586 }
14587
b34976b6 14588 return TRUE;
b49e97c9
TS
14589 }
14590
14591 /* Check flag compatibility. */
14592
14593 new_flags &= ~EF_MIPS_NOREORDER;
14594 old_flags &= ~EF_MIPS_NOREORDER;
14595
f4416af6
AO
14596 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
14597 doesn't seem to matter. */
14598 new_flags &= ~EF_MIPS_XGOT;
14599 old_flags &= ~EF_MIPS_XGOT;
14600
98a8deaf
RS
14601 /* MIPSpro generates ucode info in n64 objects. Again, we should
14602 just be able to ignore this. */
14603 new_flags &= ~EF_MIPS_UCODE;
14604 old_flags &= ~EF_MIPS_UCODE;
14605
861fb55a
DJ
14606 /* DSOs should only be linked with CPIC code. */
14607 if ((ibfd->flags & DYNAMIC) != 0)
14608 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 14609
b49e97c9 14610 if (new_flags == old_flags)
b34976b6 14611 return TRUE;
b49e97c9
TS
14612
14613 /* Check to see if the input BFD actually contains any sections.
14614 If not, its flags may not have been initialised either, but it cannot
14615 actually cause any incompatibility. */
14616 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
14617 {
14618 /* Ignore synthetic sections and empty .text, .data and .bss sections
ed88c97e
RS
14619 which are automatically generated by gas. Also ignore fake
14620 (s)common sections, since merely defining a common symbol does
14621 not affect compatibility. */
14622 if ((sec->flags & SEC_IS_COMMON) == 0
14623 && strcmp (sec->name, ".reginfo")
b49e97c9 14624 && strcmp (sec->name, ".mdebug")
eea6121a 14625 && (sec->size != 0
d13d89fa
NS
14626 || (strcmp (sec->name, ".text")
14627 && strcmp (sec->name, ".data")
14628 && strcmp (sec->name, ".bss"))))
b49e97c9 14629 {
b34976b6 14630 null_input_bfd = FALSE;
b49e97c9
TS
14631 break;
14632 }
14633 }
14634 if (null_input_bfd)
b34976b6 14635 return TRUE;
b49e97c9 14636
b34976b6 14637 ok = TRUE;
b49e97c9 14638
143d77c5
EC
14639 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
14640 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 14641 {
b49e97c9 14642 (*_bfd_error_handler)
861fb55a 14643 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 14644 ibfd);
143d77c5 14645 ok = TRUE;
b49e97c9
TS
14646 }
14647
143d77c5
EC
14648 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
14649 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
14650 if (! (new_flags & EF_MIPS_PIC))
14651 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
14652
14653 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14654 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 14655
64543e1a
RS
14656 /* Compare the ISAs. */
14657 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 14658 {
64543e1a 14659 (*_bfd_error_handler)
d003868e
AM
14660 (_("%B: linking 32-bit code with 64-bit code"),
14661 ibfd);
64543e1a
RS
14662 ok = FALSE;
14663 }
14664 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
14665 {
14666 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
14667 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 14668 {
64543e1a
RS
14669 /* Copy the architecture info from IBFD to OBFD. Also copy
14670 the 32-bit flag (if set) so that we continue to recognise
14671 OBFD as a 32-bit binary. */
14672 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
14673 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
14674 elf_elfheader (obfd)->e_flags
14675 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14676
14677 /* Copy across the ABI flags if OBFD doesn't use them
14678 and if that was what caused us to treat IBFD as 32-bit. */
14679 if ((old_flags & EF_MIPS_ABI) == 0
14680 && mips_32bit_flags_p (new_flags)
14681 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
14682 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
14683 }
14684 else
14685 {
64543e1a 14686 /* The ISAs aren't compatible. */
b49e97c9 14687 (*_bfd_error_handler)
d003868e
AM
14688 (_("%B: linking %s module with previous %s modules"),
14689 ibfd,
64543e1a
RS
14690 bfd_printable_name (ibfd),
14691 bfd_printable_name (obfd));
b34976b6 14692 ok = FALSE;
b49e97c9 14693 }
b49e97c9
TS
14694 }
14695
64543e1a
RS
14696 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14697 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14698
14699 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
14700 does set EI_CLASS differently from any 32-bit ABI. */
14701 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
14702 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14703 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14704 {
14705 /* Only error if both are set (to different values). */
14706 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
14707 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14708 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14709 {
14710 (*_bfd_error_handler)
d003868e
AM
14711 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
14712 ibfd,
b49e97c9
TS
14713 elf_mips_abi_name (ibfd),
14714 elf_mips_abi_name (obfd));
b34976b6 14715 ok = FALSE;
b49e97c9
TS
14716 }
14717 new_flags &= ~EF_MIPS_ABI;
14718 old_flags &= ~EF_MIPS_ABI;
14719 }
14720
df58fc94
RS
14721 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
14722 and allow arbitrary mixing of the remaining ASEs (retain the union). */
fb39dac1
RS
14723 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
14724 {
df58fc94
RS
14725 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14726 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14727 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
14728 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
14729 int micro_mis = old_m16 && new_micro;
14730 int m16_mis = old_micro && new_m16;
14731
14732 if (m16_mis || micro_mis)
14733 {
14734 (*_bfd_error_handler)
14735 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
14736 ibfd,
14737 m16_mis ? "MIPS16" : "microMIPS",
14738 m16_mis ? "microMIPS" : "MIPS16");
14739 ok = FALSE;
14740 }
14741
fb39dac1
RS
14742 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
14743
14744 new_flags &= ~ EF_MIPS_ARCH_ASE;
14745 old_flags &= ~ EF_MIPS_ARCH_ASE;
14746 }
14747
ba92f887
MR
14748 /* Compare NaN encodings. */
14749 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
14750 {
14751 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
14752 ibfd,
14753 (new_flags & EF_MIPS_NAN2008
14754 ? "-mnan=2008" : "-mnan=legacy"),
14755 (old_flags & EF_MIPS_NAN2008
14756 ? "-mnan=2008" : "-mnan=legacy"));
14757 ok = FALSE;
14758 new_flags &= ~EF_MIPS_NAN2008;
14759 old_flags &= ~EF_MIPS_NAN2008;
14760 }
14761
b49e97c9
TS
14762 /* Warn about any other mismatches */
14763 if (new_flags != old_flags)
14764 {
14765 (*_bfd_error_handler)
d003868e
AM
14766 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
14767 ibfd, (unsigned long) new_flags,
b49e97c9 14768 (unsigned long) old_flags);
b34976b6 14769 ok = FALSE;
b49e97c9
TS
14770 }
14771
14772 if (! ok)
14773 {
14774 bfd_set_error (bfd_error_bad_value);
b34976b6 14775 return FALSE;
b49e97c9
TS
14776 }
14777
b34976b6 14778 return TRUE;
b49e97c9
TS
14779}
14780
14781/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
14782
b34976b6 14783bfd_boolean
9719ad41 14784_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
14785{
14786 BFD_ASSERT (!elf_flags_init (abfd)
14787 || elf_elfheader (abfd)->e_flags == flags);
14788
14789 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
14790 elf_flags_init (abfd) = TRUE;
14791 return TRUE;
b49e97c9
TS
14792}
14793
ad9563d6
CM
14794char *
14795_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14796{
14797 switch (dtag)
14798 {
14799 default: return "";
14800 case DT_MIPS_RLD_VERSION:
14801 return "MIPS_RLD_VERSION";
14802 case DT_MIPS_TIME_STAMP:
14803 return "MIPS_TIME_STAMP";
14804 case DT_MIPS_ICHECKSUM:
14805 return "MIPS_ICHECKSUM";
14806 case DT_MIPS_IVERSION:
14807 return "MIPS_IVERSION";
14808 case DT_MIPS_FLAGS:
14809 return "MIPS_FLAGS";
14810 case DT_MIPS_BASE_ADDRESS:
14811 return "MIPS_BASE_ADDRESS";
14812 case DT_MIPS_MSYM:
14813 return "MIPS_MSYM";
14814 case DT_MIPS_CONFLICT:
14815 return "MIPS_CONFLICT";
14816 case DT_MIPS_LIBLIST:
14817 return "MIPS_LIBLIST";
14818 case DT_MIPS_LOCAL_GOTNO:
14819 return "MIPS_LOCAL_GOTNO";
14820 case DT_MIPS_CONFLICTNO:
14821 return "MIPS_CONFLICTNO";
14822 case DT_MIPS_LIBLISTNO:
14823 return "MIPS_LIBLISTNO";
14824 case DT_MIPS_SYMTABNO:
14825 return "MIPS_SYMTABNO";
14826 case DT_MIPS_UNREFEXTNO:
14827 return "MIPS_UNREFEXTNO";
14828 case DT_MIPS_GOTSYM:
14829 return "MIPS_GOTSYM";
14830 case DT_MIPS_HIPAGENO:
14831 return "MIPS_HIPAGENO";
14832 case DT_MIPS_RLD_MAP:
14833 return "MIPS_RLD_MAP";
14834 case DT_MIPS_DELTA_CLASS:
14835 return "MIPS_DELTA_CLASS";
14836 case DT_MIPS_DELTA_CLASS_NO:
14837 return "MIPS_DELTA_CLASS_NO";
14838 case DT_MIPS_DELTA_INSTANCE:
14839 return "MIPS_DELTA_INSTANCE";
14840 case DT_MIPS_DELTA_INSTANCE_NO:
14841 return "MIPS_DELTA_INSTANCE_NO";
14842 case DT_MIPS_DELTA_RELOC:
14843 return "MIPS_DELTA_RELOC";
14844 case DT_MIPS_DELTA_RELOC_NO:
14845 return "MIPS_DELTA_RELOC_NO";
14846 case DT_MIPS_DELTA_SYM:
14847 return "MIPS_DELTA_SYM";
14848 case DT_MIPS_DELTA_SYM_NO:
14849 return "MIPS_DELTA_SYM_NO";
14850 case DT_MIPS_DELTA_CLASSSYM:
14851 return "MIPS_DELTA_CLASSSYM";
14852 case DT_MIPS_DELTA_CLASSSYM_NO:
14853 return "MIPS_DELTA_CLASSSYM_NO";
14854 case DT_MIPS_CXX_FLAGS:
14855 return "MIPS_CXX_FLAGS";
14856 case DT_MIPS_PIXIE_INIT:
14857 return "MIPS_PIXIE_INIT";
14858 case DT_MIPS_SYMBOL_LIB:
14859 return "MIPS_SYMBOL_LIB";
14860 case DT_MIPS_LOCALPAGE_GOTIDX:
14861 return "MIPS_LOCALPAGE_GOTIDX";
14862 case DT_MIPS_LOCAL_GOTIDX:
14863 return "MIPS_LOCAL_GOTIDX";
14864 case DT_MIPS_HIDDEN_GOTIDX:
14865 return "MIPS_HIDDEN_GOTIDX";
14866 case DT_MIPS_PROTECTED_GOTIDX:
14867 return "MIPS_PROTECTED_GOT_IDX";
14868 case DT_MIPS_OPTIONS:
14869 return "MIPS_OPTIONS";
14870 case DT_MIPS_INTERFACE:
14871 return "MIPS_INTERFACE";
14872 case DT_MIPS_DYNSTR_ALIGN:
14873 return "DT_MIPS_DYNSTR_ALIGN";
14874 case DT_MIPS_INTERFACE_SIZE:
14875 return "DT_MIPS_INTERFACE_SIZE";
14876 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14877 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14878 case DT_MIPS_PERF_SUFFIX:
14879 return "DT_MIPS_PERF_SUFFIX";
14880 case DT_MIPS_COMPACT_SIZE:
14881 return "DT_MIPS_COMPACT_SIZE";
14882 case DT_MIPS_GP_VALUE:
14883 return "DT_MIPS_GP_VALUE";
14884 case DT_MIPS_AUX_DYNAMIC:
14885 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
14886 case DT_MIPS_PLTGOT:
14887 return "DT_MIPS_PLTGOT";
14888 case DT_MIPS_RWPLT:
14889 return "DT_MIPS_RWPLT";
ad9563d6
CM
14890 }
14891}
14892
b34976b6 14893bfd_boolean
9719ad41 14894_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 14895{
9719ad41 14896 FILE *file = ptr;
b49e97c9
TS
14897
14898 BFD_ASSERT (abfd != NULL && ptr != NULL);
14899
14900 /* Print normal ELF private data. */
14901 _bfd_elf_print_private_bfd_data (abfd, ptr);
14902
14903 /* xgettext:c-format */
14904 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14905
14906 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14907 fprintf (file, _(" [abi=O32]"));
14908 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14909 fprintf (file, _(" [abi=O64]"));
14910 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14911 fprintf (file, _(" [abi=EABI32]"));
14912 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14913 fprintf (file, _(" [abi=EABI64]"));
14914 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14915 fprintf (file, _(" [abi unknown]"));
14916 else if (ABI_N32_P (abfd))
14917 fprintf (file, _(" [abi=N32]"));
14918 else if (ABI_64_P (abfd))
14919 fprintf (file, _(" [abi=64]"));
14920 else
14921 fprintf (file, _(" [no abi set]"));
14922
14923 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 14924 fprintf (file, " [mips1]");
b49e97c9 14925 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 14926 fprintf (file, " [mips2]");
b49e97c9 14927 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 14928 fprintf (file, " [mips3]");
b49e97c9 14929 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 14930 fprintf (file, " [mips4]");
b49e97c9 14931 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 14932 fprintf (file, " [mips5]");
b49e97c9 14933 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 14934 fprintf (file, " [mips32]");
b49e97c9 14935 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 14936 fprintf (file, " [mips64]");
af7ee8bf 14937 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 14938 fprintf (file, " [mips32r2]");
5f74bc13 14939 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 14940 fprintf (file, " [mips64r2]");
b49e97c9
TS
14941 else
14942 fprintf (file, _(" [unknown ISA]"));
14943
40d32fc6 14944 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 14945 fprintf (file, " [mdmx]");
40d32fc6
CD
14946
14947 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 14948 fprintf (file, " [mips16]");
40d32fc6 14949
df58fc94
RS
14950 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14951 fprintf (file, " [micromips]");
14952
ba92f887
MR
14953 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
14954 fprintf (file, " [nan2008]");
14955
5baf5e34
SE
14956 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
14957 fprintf (file, " [fp64]");
14958
b49e97c9 14959 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 14960 fprintf (file, " [32bitmode]");
b49e97c9
TS
14961 else
14962 fprintf (file, _(" [not 32bitmode]"));
14963
c0e3f241 14964 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 14965 fprintf (file, " [noreorder]");
c0e3f241
CD
14966
14967 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 14968 fprintf (file, " [PIC]");
c0e3f241
CD
14969
14970 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 14971 fprintf (file, " [CPIC]");
c0e3f241
CD
14972
14973 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 14974 fprintf (file, " [XGOT]");
c0e3f241
CD
14975
14976 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 14977 fprintf (file, " [UCODE]");
c0e3f241 14978
b49e97c9
TS
14979 fputc ('\n', file);
14980
b34976b6 14981 return TRUE;
b49e97c9 14982}
2f89ff8d 14983
b35d266b 14984const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 14985{
0112cd26
NC
14986 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14987 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14988 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
14989 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14990 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14991 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
14992 { NULL, 0, 0, 0, 0 }
2f89ff8d 14993};
5e2b0d47 14994
8992f0d7
TS
14995/* Merge non visibility st_other attributes. Ensure that the
14996 STO_OPTIONAL flag is copied into h->other, even if this is not a
14997 definiton of the symbol. */
5e2b0d47
NC
14998void
14999_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
15000 const Elf_Internal_Sym *isym,
15001 bfd_boolean definition,
15002 bfd_boolean dynamic ATTRIBUTE_UNUSED)
15003{
8992f0d7
TS
15004 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
15005 {
15006 unsigned char other;
15007
15008 other = (definition ? isym->st_other : h->other);
15009 other &= ~ELF_ST_VISIBILITY (-1);
15010 h->other = other | ELF_ST_VISIBILITY (h->other);
15011 }
15012
15013 if (!definition
5e2b0d47
NC
15014 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
15015 h->other |= STO_OPTIONAL;
15016}
12ac1cf5
NC
15017
15018/* Decide whether an undefined symbol is special and can be ignored.
15019 This is the case for OPTIONAL symbols on IRIX. */
15020bfd_boolean
15021_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
15022{
15023 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
15024}
e0764319
NC
15025
15026bfd_boolean
15027_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
15028{
15029 return (sym->st_shndx == SHN_COMMON
15030 || sym->st_shndx == SHN_MIPS_ACOMMON
15031 || sym->st_shndx == SHN_MIPS_SCOMMON);
15032}
861fb55a
DJ
15033
15034/* Return address for Ith PLT stub in section PLT, for relocation REL
15035 or (bfd_vma) -1 if it should not be included. */
15036
15037bfd_vma
15038_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
15039 const arelent *rel ATTRIBUTE_UNUSED)
15040{
15041 return (plt->vma
15042 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
15043 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
15044}
15045
1bbce132
MR
15046/* Build a table of synthetic symbols to represent the PLT. As with MIPS16
15047 and microMIPS PLT slots we may have a many-to-one mapping between .plt
15048 and .got.plt and also the slots may be of a different size each we walk
15049 the PLT manually fetching instructions and matching them against known
15050 patterns. To make things easier standard MIPS slots, if any, always come
15051 first. As we don't create proper ELF symbols we use the UDATA.I member
15052 of ASYMBOL to carry ISA annotation. The encoding used is the same as
15053 with the ST_OTHER member of the ELF symbol. */
15054
15055long
15056_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
15057 long symcount ATTRIBUTE_UNUSED,
15058 asymbol **syms ATTRIBUTE_UNUSED,
15059 long dynsymcount, asymbol **dynsyms,
15060 asymbol **ret)
15061{
15062 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
15063 static const char microsuffix[] = "@micromipsplt";
15064 static const char m16suffix[] = "@mips16plt";
15065 static const char mipssuffix[] = "@plt";
15066
15067 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
15068 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15069 bfd_boolean micromips_p = MICROMIPS_P (abfd);
15070 Elf_Internal_Shdr *hdr;
15071 bfd_byte *plt_data;
15072 bfd_vma plt_offset;
15073 unsigned int other;
15074 bfd_vma entry_size;
15075 bfd_vma plt0_size;
15076 asection *relplt;
15077 bfd_vma opcode;
15078 asection *plt;
15079 asymbol *send;
15080 size_t size;
15081 char *names;
15082 long counti;
15083 arelent *p;
15084 asymbol *s;
15085 char *nend;
15086 long count;
15087 long pi;
15088 long i;
15089 long n;
15090
15091 *ret = NULL;
15092
15093 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
15094 return 0;
15095
15096 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
15097 if (relplt == NULL)
15098 return 0;
15099
15100 hdr = &elf_section_data (relplt)->this_hdr;
15101 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
15102 return 0;
15103
15104 plt = bfd_get_section_by_name (abfd, ".plt");
15105 if (plt == NULL)
15106 return 0;
15107
15108 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
15109 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
15110 return -1;
15111 p = relplt->relocation;
15112
15113 /* Calculating the exact amount of space required for symbols would
15114 require two passes over the PLT, so just pessimise assuming two
15115 PLT slots per relocation. */
15116 count = relplt->size / hdr->sh_entsize;
15117 counti = count * bed->s->int_rels_per_ext_rel;
15118 size = 2 * count * sizeof (asymbol);
15119 size += count * (sizeof (mipssuffix) +
15120 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
15121 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
15122 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
15123
15124 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
15125 size += sizeof (asymbol) + sizeof (pltname);
15126
15127 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
15128 return -1;
15129
15130 if (plt->size < 16)
15131 return -1;
15132
15133 s = *ret = bfd_malloc (size);
15134 if (s == NULL)
15135 return -1;
15136 send = s + 2 * count + 1;
15137
15138 names = (char *) send;
15139 nend = (char *) s + size;
15140 n = 0;
15141
15142 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
15143 if (opcode == 0x3302fffe)
15144 {
15145 if (!micromips_p)
15146 return -1;
15147 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
15148 other = STO_MICROMIPS;
15149 }
833794fc
MR
15150 else if (opcode == 0x0398c1d0)
15151 {
15152 if (!micromips_p)
15153 return -1;
15154 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
15155 other = STO_MICROMIPS;
15156 }
1bbce132
MR
15157 else
15158 {
15159 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
15160 other = 0;
15161 }
15162
15163 s->the_bfd = abfd;
15164 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
15165 s->section = plt;
15166 s->value = 0;
15167 s->name = names;
15168 s->udata.i = other;
15169 memcpy (names, pltname, sizeof (pltname));
15170 names += sizeof (pltname);
15171 ++s, ++n;
15172
15173 pi = 0;
15174 for (plt_offset = plt0_size;
15175 plt_offset + 8 <= plt->size && s < send;
15176 plt_offset += entry_size)
15177 {
15178 bfd_vma gotplt_addr;
15179 const char *suffix;
15180 bfd_vma gotplt_hi;
15181 bfd_vma gotplt_lo;
15182 size_t suffixlen;
15183
15184 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
15185
15186 /* Check if the second word matches the expected MIPS16 instruction. */
15187 if (opcode == 0x651aeb00)
15188 {
15189 if (micromips_p)
15190 return -1;
15191 /* Truncated table??? */
15192 if (plt_offset + 16 > plt->size)
15193 break;
15194 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
15195 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
15196 suffixlen = sizeof (m16suffix);
15197 suffix = m16suffix;
15198 other = STO_MIPS16;
15199 }
833794fc 15200 /* Likewise the expected microMIPS instruction (no insn32 mode). */
1bbce132
MR
15201 else if (opcode == 0xff220000)
15202 {
15203 if (!micromips_p)
15204 return -1;
15205 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
15206 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15207 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
15208 gotplt_lo <<= 2;
15209 gotplt_addr = gotplt_hi + gotplt_lo;
15210 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
15211 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
15212 suffixlen = sizeof (microsuffix);
15213 suffix = microsuffix;
15214 other = STO_MICROMIPS;
15215 }
833794fc
MR
15216 /* Likewise the expected microMIPS instruction (insn32 mode). */
15217 else if ((opcode & 0xffff0000) == 0xff2f0000)
15218 {
15219 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15220 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
15221 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15222 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15223 gotplt_addr = gotplt_hi + gotplt_lo;
15224 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
15225 suffixlen = sizeof (microsuffix);
15226 suffix = microsuffix;
15227 other = STO_MICROMIPS;
15228 }
1bbce132
MR
15229 /* Otherwise assume standard MIPS code. */
15230 else
15231 {
15232 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
15233 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
15234 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15235 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15236 gotplt_addr = gotplt_hi + gotplt_lo;
15237 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
15238 suffixlen = sizeof (mipssuffix);
15239 suffix = mipssuffix;
15240 other = 0;
15241 }
15242 /* Truncated table??? */
15243 if (plt_offset + entry_size > plt->size)
15244 break;
15245
15246 for (i = 0;
15247 i < count && p[pi].address != gotplt_addr;
15248 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
15249
15250 if (i < count)
15251 {
15252 size_t namelen;
15253 size_t len;
15254
15255 *s = **p[pi].sym_ptr_ptr;
15256 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
15257 we are defining a symbol, ensure one of them is set. */
15258 if ((s->flags & BSF_LOCAL) == 0)
15259 s->flags |= BSF_GLOBAL;
15260 s->flags |= BSF_SYNTHETIC;
15261 s->section = plt;
15262 s->value = plt_offset;
15263 s->name = names;
15264 s->udata.i = other;
15265
15266 len = strlen ((*p[pi].sym_ptr_ptr)->name);
15267 namelen = len + suffixlen;
15268 if (names + namelen > nend)
15269 break;
15270
15271 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
15272 names += len;
15273 memcpy (names, suffix, suffixlen);
15274 names += suffixlen;
15275
15276 ++s, ++n;
15277 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
15278 }
15279 }
15280
15281 free (plt_data);
15282
15283 return n;
15284}
15285
861fb55a
DJ
15286void
15287_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
15288{
15289 struct mips_elf_link_hash_table *htab;
15290 Elf_Internal_Ehdr *i_ehdrp;
15291
15292 i_ehdrp = elf_elfheader (abfd);
15293 if (link_info)
15294 {
15295 htab = mips_elf_hash_table (link_info);
4dfe6ac6
NC
15296 BFD_ASSERT (htab != NULL);
15297
861fb55a
DJ
15298 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
15299 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
15300 }
15301}
This page took 1.769752 seconds and 4 git commands to generate.