Fix an issue with "Rearrange MIPS INSN* masks" patch.
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
4b95cf5c 2 Copyright (C) 1993-2014 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;
cb22ccf4
KCY
171 /* The first unused local .got entry. */
172 unsigned int assigned_low_gotno;
173 /* The last unused local .got entry. */
174 unsigned int assigned_high_gotno;
b15e6682
AO
175 /* A hash table holding members of the got. */
176 struct htab *got_entries;
13db6b44
RS
177 /* A hash table holding mips_got_page_ref structures. */
178 struct htab *got_page_refs;
c224138d
RS
179 /* A hash table of mips_got_page_entry structures. */
180 struct htab *got_page_entries;
f4416af6
AO
181 /* In multi-got links, a pointer to the next got (err, rather, most
182 of the time, it points to the previous got). */
183 struct mips_got_info *next;
184};
185
d7206569 186/* Structure passed when merging bfds' gots. */
f4416af6
AO
187
188struct mips_elf_got_per_bfd_arg
189{
f4416af6
AO
190 /* The output bfd. */
191 bfd *obfd;
192 /* The link information. */
193 struct bfd_link_info *info;
194 /* A pointer to the primary got, i.e., the one that's going to get
195 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
196 DT_MIPS_GOTSYM. */
197 struct mips_got_info *primary;
198 /* A non-primary got we're trying to merge with other input bfd's
199 gots. */
200 struct mips_got_info *current;
201 /* The maximum number of got entries that can be addressed with a
202 16-bit offset. */
203 unsigned int max_count;
c224138d
RS
204 /* The maximum number of page entries needed by each got. */
205 unsigned int max_pages;
0f20cc35
DJ
206 /* The total number of global entries which will live in the
207 primary got and be automatically relocated. This includes
208 those not referenced by the primary GOT but included in
209 the "master" GOT. */
210 unsigned int global_count;
f4416af6
AO
211};
212
ab361d49
RS
213/* A structure used to pass information to htab_traverse callbacks
214 when laying out the GOT. */
f4416af6 215
ab361d49 216struct mips_elf_traverse_got_arg
f4416af6 217{
ab361d49 218 struct bfd_link_info *info;
f4416af6
AO
219 struct mips_got_info *g;
220 int value;
0f20cc35
DJ
221};
222
f0abc2a1
AM
223struct _mips_elf_section_data
224{
225 struct bfd_elf_section_data elf;
226 union
227 {
f0abc2a1
AM
228 bfd_byte *tdata;
229 } u;
230};
231
232#define mips_elf_section_data(sec) \
68bfbfcc 233 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 234
d5eaccd7
RS
235#define is_mips_elf(bfd) \
236 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
237 && elf_tdata (bfd) != NULL \
4dfe6ac6 238 && elf_object_id (bfd) == MIPS_ELF_DATA)
d5eaccd7 239
634835ae
RS
240/* The ABI says that every symbol used by dynamic relocations must have
241 a global GOT entry. Among other things, this provides the dynamic
242 linker with a free, directly-indexed cache. The GOT can therefore
243 contain symbols that are not referenced by GOT relocations themselves
244 (in other words, it may have symbols that are not referenced by things
245 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
246
247 GOT relocations are less likely to overflow if we put the associated
248 GOT entries towards the beginning. We therefore divide the global
249 GOT entries into two areas: "normal" and "reloc-only". Entries in
250 the first area can be used for both dynamic relocations and GP-relative
251 accesses, while those in the "reloc-only" area are for dynamic
252 relocations only.
253
254 These GGA_* ("Global GOT Area") values are organised so that lower
255 values are more general than higher values. Also, non-GGA_NONE
256 values are ordered by the position of the area in the GOT. */
257#define GGA_NORMAL 0
258#define GGA_RELOC_ONLY 1
259#define GGA_NONE 2
260
861fb55a
DJ
261/* Information about a non-PIC interface to a PIC function. There are
262 two ways of creating these interfaces. The first is to add:
263
264 lui $25,%hi(func)
265 addiu $25,$25,%lo(func)
266
267 immediately before a PIC function "func". The second is to add:
268
269 lui $25,%hi(func)
270 j func
271 addiu $25,$25,%lo(func)
272
273 to a separate trampoline section.
274
275 Stubs of the first kind go in a new section immediately before the
276 target function. Stubs of the second kind go in a single section
277 pointed to by the hash table's "strampoline" field. */
278struct mips_elf_la25_stub {
279 /* The generated section that contains this stub. */
280 asection *stub_section;
281
282 /* The offset of the stub from the start of STUB_SECTION. */
283 bfd_vma offset;
284
285 /* One symbol for the original function. Its location is available
286 in H->root.root.u.def. */
287 struct mips_elf_link_hash_entry *h;
288};
289
290/* Macros for populating a mips_elf_la25_stub. */
291
292#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
293#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
294#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
d21911ea
MR
295#define LA25_LUI_MICROMIPS(VAL) \
296 (0x41b90000 | (VAL)) /* lui t9,VAL */
297#define LA25_J_MICROMIPS(VAL) \
298 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
299#define LA25_ADDIU_MICROMIPS(VAL) \
300 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
861fb55a 301
b49e97c9
TS
302/* This structure is passed to mips_elf_sort_hash_table_f when sorting
303 the dynamic symbols. */
304
305struct mips_elf_hash_sort_data
306{
307 /* The symbol in the global GOT with the lowest dynamic symbol table
308 index. */
309 struct elf_link_hash_entry *low;
0f20cc35
DJ
310 /* The least dynamic symbol table index corresponding to a non-TLS
311 symbol with a GOT entry. */
b49e97c9 312 long min_got_dynindx;
f4416af6
AO
313 /* The greatest dynamic symbol table index corresponding to a symbol
314 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 315 with dynamic relocations pointing to it from non-primary GOTs). */
f4416af6 316 long max_unref_got_dynindx;
b49e97c9
TS
317 /* The greatest dynamic symbol table index not corresponding to a
318 symbol without a GOT entry. */
319 long max_non_got_dynindx;
320};
321
1bbce132
MR
322/* We make up to two PLT entries if needed, one for standard MIPS code
323 and one for compressed code, either a MIPS16 or microMIPS one. We
324 keep a separate record of traditional lazy-binding stubs, for easier
325 processing. */
326
327struct plt_entry
328{
329 /* Traditional SVR4 stub offset, or -1 if none. */
330 bfd_vma stub_offset;
331
332 /* Standard PLT entry offset, or -1 if none. */
333 bfd_vma mips_offset;
334
335 /* Compressed PLT entry offset, or -1 if none. */
336 bfd_vma comp_offset;
337
338 /* The corresponding .got.plt index, or -1 if none. */
339 bfd_vma gotplt_index;
340
341 /* Whether we need a standard PLT entry. */
342 unsigned int need_mips : 1;
343
344 /* Whether we need a compressed PLT entry. */
345 unsigned int need_comp : 1;
346};
347
b49e97c9
TS
348/* The MIPS ELF linker needs additional information for each symbol in
349 the global hash table. */
350
351struct mips_elf_link_hash_entry
352{
353 struct elf_link_hash_entry root;
354
355 /* External symbol information. */
356 EXTR esym;
357
861fb55a
DJ
358 /* The la25 stub we have created for ths symbol, if any. */
359 struct mips_elf_la25_stub *la25_stub;
360
b49e97c9
TS
361 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
362 this symbol. */
363 unsigned int possibly_dynamic_relocs;
364
b49e97c9
TS
365 /* If there is a stub that 32 bit functions should use to call this
366 16 bit function, this points to the section containing the stub. */
367 asection *fn_stub;
368
b49e97c9
TS
369 /* If there is a stub that 16 bit functions should use to call this
370 32 bit function, this points to the section containing the stub. */
371 asection *call_stub;
372
373 /* This is like the call_stub field, but it is used if the function
374 being called returns a floating point value. */
375 asection *call_fp_stub;
7c5fcef7 376
634835ae
RS
377 /* The highest GGA_* value that satisfies all references to this symbol. */
378 unsigned int global_got_area : 2;
379
6ccf4795
RS
380 /* True if all GOT relocations against this symbol are for calls. This is
381 a looser condition than no_fn_stub below, because there may be other
382 non-call non-GOT relocations against the symbol. */
383 unsigned int got_only_for_calls : 1;
384
71782a75
RS
385 /* True if one of the relocations described by possibly_dynamic_relocs
386 is against a readonly section. */
387 unsigned int readonly_reloc : 1;
388
861fb55a
DJ
389 /* True if there is a relocation against this symbol that must be
390 resolved by the static linker (in other words, if the relocation
391 cannot possibly be made dynamic). */
392 unsigned int has_static_relocs : 1;
393
71782a75
RS
394 /* True if we must not create a .MIPS.stubs entry for this symbol.
395 This is set, for example, if there are relocations related to
396 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
397 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
398 unsigned int no_fn_stub : 1;
399
400 /* Whether we need the fn_stub; this is true if this symbol appears
401 in any relocs other than a 16 bit call. */
402 unsigned int need_fn_stub : 1;
403
861fb55a
DJ
404 /* True if this symbol is referenced by branch relocations from
405 any non-PIC input file. This is used to determine whether an
406 la25 stub is required. */
407 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
408
409 /* Does this symbol need a traditional MIPS lazy-binding stub
410 (as opposed to a PLT entry)? */
411 unsigned int needs_lazy_stub : 1;
1bbce132
MR
412
413 /* Does this symbol resolve to a PLT entry? */
414 unsigned int use_plt_entry : 1;
b49e97c9
TS
415};
416
417/* MIPS ELF linker hash table. */
418
419struct mips_elf_link_hash_table
420{
421 struct elf_link_hash_table root;
861fb55a 422
b49e97c9
TS
423 /* The number of .rtproc entries. */
424 bfd_size_type procedure_count;
861fb55a 425
b49e97c9
TS
426 /* The size of the .compact_rel section (if SGI_COMPAT). */
427 bfd_size_type compact_rel_size;
861fb55a 428
e6aea42d
MR
429 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
430 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
b34976b6 431 bfd_boolean use_rld_obj_head;
861fb55a 432
b4082c70
DD
433 /* The __rld_map or __rld_obj_head symbol. */
434 struct elf_link_hash_entry *rld_symbol;
861fb55a 435
b49e97c9 436 /* This is set if we see any mips16 stub sections. */
b34976b6 437 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
438
439 /* True if we can generate copy relocs and PLTs. */
440 bfd_boolean use_plts_and_copy_relocs;
441
833794fc
MR
442 /* True if we can only use 32-bit microMIPS instructions. */
443 bfd_boolean insn32;
444
0a44bf69
RS
445 /* True if we're generating code for VxWorks. */
446 bfd_boolean is_vxworks;
861fb55a 447
0e53d9da
AN
448 /* True if we already reported the small-data section overflow. */
449 bfd_boolean small_data_overflow_reported;
861fb55a 450
0a44bf69
RS
451 /* Shortcuts to some dynamic sections, or NULL if they are not
452 being used. */
453 asection *srelbss;
454 asection *sdynbss;
455 asection *srelplt;
456 asection *srelplt2;
457 asection *sgotplt;
458 asection *splt;
4e41d0d7 459 asection *sstubs;
a8028dd0 460 asection *sgot;
861fb55a 461
a8028dd0
RS
462 /* The master GOT information. */
463 struct mips_got_info *got_info;
861fb55a 464
d222d210
RS
465 /* The global symbol in the GOT with the lowest index in the dynamic
466 symbol table. */
467 struct elf_link_hash_entry *global_gotsym;
468
861fb55a 469 /* The size of the PLT header in bytes. */
0a44bf69 470 bfd_vma plt_header_size;
861fb55a 471
1bbce132
MR
472 /* The size of a standard PLT entry in bytes. */
473 bfd_vma plt_mips_entry_size;
474
475 /* The size of a compressed PLT entry in bytes. */
476 bfd_vma plt_comp_entry_size;
477
478 /* The offset of the next standard PLT entry to create. */
479 bfd_vma plt_mips_offset;
480
481 /* The offset of the next compressed PLT entry to create. */
482 bfd_vma plt_comp_offset;
483
484 /* The index of the next .got.plt entry to create. */
485 bfd_vma plt_got_index;
861fb55a 486
33bb52fb
RS
487 /* The number of functions that need a lazy-binding stub. */
488 bfd_vma lazy_stub_count;
861fb55a 489
5108fc1b
RS
490 /* The size of a function stub entry in bytes. */
491 bfd_vma function_stub_size;
861fb55a
DJ
492
493 /* The number of reserved entries at the beginning of the GOT. */
494 unsigned int reserved_gotno;
495
496 /* The section used for mips_elf_la25_stub trampolines.
497 See the comment above that structure for details. */
498 asection *strampoline;
499
500 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
501 pairs. */
502 htab_t la25_stubs;
503
504 /* A function FN (NAME, IS, OS) that creates a new input section
505 called NAME and links it to output section OS. If IS is nonnull,
506 the new section should go immediately before it, otherwise it
507 should go at the (current) beginning of OS.
508
509 The function returns the new section on success, otherwise it
510 returns null. */
511 asection *(*add_stub_section) (const char *, asection *, asection *);
13db6b44
RS
512
513 /* Small local sym cache. */
514 struct sym_cache sym_cache;
1bbce132
MR
515
516 /* Is the PLT header compressed? */
517 unsigned int plt_header_is_comp : 1;
861fb55a
DJ
518};
519
4dfe6ac6
NC
520/* Get the MIPS ELF linker hash table from a link_info structure. */
521
522#define mips_elf_hash_table(p) \
523 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
524 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
525
861fb55a 526/* A structure used to communicate with htab_traverse callbacks. */
4dfe6ac6
NC
527struct mips_htab_traverse_info
528{
861fb55a
DJ
529 /* The usual link-wide information. */
530 struct bfd_link_info *info;
531 bfd *output_bfd;
532
533 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
534 bfd_boolean error;
b49e97c9
TS
535};
536
6ae68ba3
MR
537/* MIPS ELF private object data. */
538
539struct mips_elf_obj_tdata
540{
541 /* Generic ELF private object data. */
542 struct elf_obj_tdata root;
543
544 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
545 bfd *abi_fp_bfd;
ee227692 546
b60bf9be
CF
547 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
548 bfd *abi_msa_bfd;
549
ee227692
RS
550 /* The GOT requirements of input bfds. */
551 struct mips_got_info *got;
698600e4
AM
552
553 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
554 included directly in this one, but there's no point to wasting
555 the memory just for the infrequently called find_nearest_line. */
556 struct mips_elf_find_line *find_line_info;
557
558 /* An array of stub sections indexed by symbol number. */
559 asection **local_stubs;
560 asection **local_call_stubs;
561
562 /* The Irix 5 support uses two virtual sections, which represent
563 text/data symbols defined in dynamic objects. */
564 asymbol *elf_data_symbol;
565 asymbol *elf_text_symbol;
566 asection *elf_data_section;
567 asection *elf_text_section;
6ae68ba3
MR
568};
569
570/* Get MIPS ELF private object data from BFD's tdata. */
571
572#define mips_elf_tdata(bfd) \
573 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
574
0f20cc35
DJ
575#define TLS_RELOC_P(r_type) \
576 (r_type == R_MIPS_TLS_DTPMOD32 \
577 || r_type == R_MIPS_TLS_DTPMOD64 \
578 || r_type == R_MIPS_TLS_DTPREL32 \
579 || r_type == R_MIPS_TLS_DTPREL64 \
580 || r_type == R_MIPS_TLS_GD \
581 || r_type == R_MIPS_TLS_LDM \
582 || r_type == R_MIPS_TLS_DTPREL_HI16 \
583 || r_type == R_MIPS_TLS_DTPREL_LO16 \
584 || r_type == R_MIPS_TLS_GOTTPREL \
585 || r_type == R_MIPS_TLS_TPREL32 \
586 || r_type == R_MIPS_TLS_TPREL64 \
587 || r_type == R_MIPS_TLS_TPREL_HI16 \
df58fc94 588 || r_type == R_MIPS_TLS_TPREL_LO16 \
d0f13682
CLT
589 || r_type == R_MIPS16_TLS_GD \
590 || r_type == R_MIPS16_TLS_LDM \
591 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
592 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
593 || r_type == R_MIPS16_TLS_GOTTPREL \
594 || r_type == R_MIPS16_TLS_TPREL_HI16 \
595 || r_type == R_MIPS16_TLS_TPREL_LO16 \
df58fc94
RS
596 || r_type == R_MICROMIPS_TLS_GD \
597 || r_type == R_MICROMIPS_TLS_LDM \
598 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
599 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
600 || r_type == R_MICROMIPS_TLS_GOTTPREL \
601 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
602 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
0f20cc35 603
b49e97c9
TS
604/* Structure used to pass information to mips_elf_output_extsym. */
605
606struct extsym_info
607{
9e4aeb93
RS
608 bfd *abfd;
609 struct bfd_link_info *info;
b49e97c9
TS
610 struct ecoff_debug_info *debug;
611 const struct ecoff_debug_swap *swap;
b34976b6 612 bfd_boolean failed;
b49e97c9
TS
613};
614
8dc1a139 615/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
616
617static const char * const mips_elf_dynsym_rtproc_names[] =
618{
619 "_procedure_table",
620 "_procedure_string_table",
621 "_procedure_table_size",
622 NULL
623};
624
625/* These structures are used to generate the .compact_rel section on
8dc1a139 626 IRIX5. */
b49e97c9
TS
627
628typedef struct
629{
630 unsigned long id1; /* Always one? */
631 unsigned long num; /* Number of compact relocation entries. */
632 unsigned long id2; /* Always two? */
633 unsigned long offset; /* The file offset of the first relocation. */
634 unsigned long reserved0; /* Zero? */
635 unsigned long reserved1; /* Zero? */
636} Elf32_compact_rel;
637
638typedef struct
639{
640 bfd_byte id1[4];
641 bfd_byte num[4];
642 bfd_byte id2[4];
643 bfd_byte offset[4];
644 bfd_byte reserved0[4];
645 bfd_byte reserved1[4];
646} Elf32_External_compact_rel;
647
648typedef struct
649{
650 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
651 unsigned int rtype : 4; /* Relocation types. See below. */
652 unsigned int dist2to : 8;
653 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
654 unsigned long konst; /* KONST field. See below. */
655 unsigned long vaddr; /* VADDR to be relocated. */
656} Elf32_crinfo;
657
658typedef struct
659{
660 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
661 unsigned int rtype : 4; /* Relocation types. See below. */
662 unsigned int dist2to : 8;
663 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
664 unsigned long konst; /* KONST field. See below. */
665} Elf32_crinfo2;
666
667typedef struct
668{
669 bfd_byte info[4];
670 bfd_byte konst[4];
671 bfd_byte vaddr[4];
672} Elf32_External_crinfo;
673
674typedef struct
675{
676 bfd_byte info[4];
677 bfd_byte konst[4];
678} Elf32_External_crinfo2;
679
680/* These are the constants used to swap the bitfields in a crinfo. */
681
682#define CRINFO_CTYPE (0x1)
683#define CRINFO_CTYPE_SH (31)
684#define CRINFO_RTYPE (0xf)
685#define CRINFO_RTYPE_SH (27)
686#define CRINFO_DIST2TO (0xff)
687#define CRINFO_DIST2TO_SH (19)
688#define CRINFO_RELVADDR (0x7ffff)
689#define CRINFO_RELVADDR_SH (0)
690
691/* A compact relocation info has long (3 words) or short (2 words)
692 formats. A short format doesn't have VADDR field and relvaddr
693 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
694#define CRF_MIPS_LONG 1
695#define CRF_MIPS_SHORT 0
696
697/* There are 4 types of compact relocation at least. The value KONST
698 has different meaning for each type:
699
700 (type) (konst)
701 CT_MIPS_REL32 Address in data
702 CT_MIPS_WORD Address in word (XXX)
703 CT_MIPS_GPHI_LO GP - vaddr
704 CT_MIPS_JMPAD Address to jump
705 */
706
707#define CRT_MIPS_REL32 0xa
708#define CRT_MIPS_WORD 0xb
709#define CRT_MIPS_GPHI_LO 0xc
710#define CRT_MIPS_JMPAD 0xd
711
712#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
713#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
714#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
715#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
716\f
717/* The structure of the runtime procedure descriptor created by the
718 loader for use by the static exception system. */
719
720typedef struct runtime_pdr {
ae9a127f
NC
721 bfd_vma adr; /* Memory address of start of procedure. */
722 long regmask; /* Save register mask. */
723 long regoffset; /* Save register offset. */
724 long fregmask; /* Save floating point register mask. */
725 long fregoffset; /* Save floating point register offset. */
726 long frameoffset; /* Frame size. */
727 short framereg; /* Frame pointer register. */
728 short pcreg; /* Offset or reg of return pc. */
729 long irpss; /* Index into the runtime string table. */
b49e97c9 730 long reserved;
ae9a127f 731 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
732} RPDR, *pRPDR;
733#define cbRPDR sizeof (RPDR)
734#define rpdNil ((pRPDR) 0)
735\f
b15e6682 736static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
737 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
738 struct mips_elf_link_hash_entry *, int);
b34976b6 739static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 740 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
741static bfd_vma mips_elf_high
742 (bfd_vma);
b34976b6 743static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
744 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
745 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
746 bfd_vma *, asection *);
f4416af6 747static bfd_vma mips_elf_adjust_gp
9719ad41 748 (bfd *, struct mips_got_info *, bfd *);
f4416af6 749
b49e97c9
TS
750/* This will be used when we sort the dynamic relocation records. */
751static bfd *reldyn_sorting_bfd;
752
6d30f5b2
NC
753/* True if ABFD is for CPUs with load interlocking that include
754 non-MIPS1 CPUs and R3900. */
755#define LOAD_INTERLOCKS_P(abfd) \
756 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
757 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
758
cd8d5a82
CF
759/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
760 This should be safe for all architectures. We enable this predicate
761 for RM9000 for now. */
762#define JAL_TO_BAL_P(abfd) \
763 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
764
765/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
766 This should be safe for all architectures. We enable this predicate for
767 all CPUs. */
768#define JALR_TO_BAL_P(abfd) 1
769
38a7df63
CF
770/* True if ABFD is for CPUs that are faster if JR is converted to B.
771 This should be safe for all architectures. We enable this predicate for
772 all CPUs. */
773#define JR_TO_B_P(abfd) 1
774
861fb55a
DJ
775/* True if ABFD is a PIC object. */
776#define PIC_OBJECT_P(abfd) \
777 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
778
b49e97c9 779/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
780#define ABI_N32_P(abfd) \
781 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
782
4a14403c 783/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 784#define ABI_64_P(abfd) \
141ff970 785 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 786
4a14403c
TS
787/* Nonzero if ABFD is using NewABI conventions. */
788#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
789
e8faf7d1
MR
790/* Nonzero if ABFD has microMIPS code. */
791#define MICROMIPS_P(abfd) \
792 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
793
4a14403c 794/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
795#define IRIX_COMPAT(abfd) \
796 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
797
b49e97c9
TS
798/* Whether we are trying to be compatible with IRIX at all. */
799#define SGI_COMPAT(abfd) \
800 (IRIX_COMPAT (abfd) != ict_none)
801
802/* The name of the options section. */
803#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 804 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 805
cc2e31b9
RS
806/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
807 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
808#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
809 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
810
943284cc
DJ
811/* Whether the section is readonly. */
812#define MIPS_ELF_READONLY_SECTION(sec) \
813 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
814 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
815
b49e97c9 816/* The name of the stub section. */
ca07892d 817#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
818
819/* The size of an external REL relocation. */
820#define MIPS_ELF_REL_SIZE(abfd) \
821 (get_elf_backend_data (abfd)->s->sizeof_rel)
822
0a44bf69
RS
823/* The size of an external RELA relocation. */
824#define MIPS_ELF_RELA_SIZE(abfd) \
825 (get_elf_backend_data (abfd)->s->sizeof_rela)
826
b49e97c9
TS
827/* The size of an external dynamic table entry. */
828#define MIPS_ELF_DYN_SIZE(abfd) \
829 (get_elf_backend_data (abfd)->s->sizeof_dyn)
830
831/* The size of a GOT entry. */
832#define MIPS_ELF_GOT_SIZE(abfd) \
833 (get_elf_backend_data (abfd)->s->arch_size / 8)
834
b4082c70
DD
835/* The size of the .rld_map section. */
836#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
837 (get_elf_backend_data (abfd)->s->arch_size / 8)
838
b49e97c9
TS
839/* The size of a symbol-table entry. */
840#define MIPS_ELF_SYM_SIZE(abfd) \
841 (get_elf_backend_data (abfd)->s->sizeof_sym)
842
843/* The default alignment for sections, as a power of two. */
844#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 845 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
846
847/* Get word-sized data. */
848#define MIPS_ELF_GET_WORD(abfd, ptr) \
849 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
850
851/* Put out word-sized data. */
852#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
853 (ABI_64_P (abfd) \
854 ? bfd_put_64 (abfd, val, ptr) \
855 : bfd_put_32 (abfd, val, ptr))
856
861fb55a
DJ
857/* The opcode for word-sized loads (LW or LD). */
858#define MIPS_ELF_LOAD_WORD(abfd) \
859 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
860
b49e97c9 861/* Add a dynamic symbol table-entry. */
9719ad41 862#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 863 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
864
865#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
866 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
867
0a44bf69
RS
868/* The name of the dynamic relocation section. */
869#define MIPS_ELF_REL_DYN_NAME(INFO) \
870 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
871
b49e97c9
TS
872/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
873 from smaller values. Start with zero, widen, *then* decrement. */
874#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 875#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 876
51e38d68
RS
877/* The value to write into got[1] for SVR4 targets, to identify it is
878 a GNU object. The dynamic linker can then use got[1] to store the
879 module pointer. */
880#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
881 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
882
f4416af6 883/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
884#define ELF_MIPS_GP_OFFSET(INFO) \
885 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
886
887/* The maximum size of the GOT for it to be addressable using 16-bit
888 offsets from $gp. */
0a44bf69 889#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 890
6a691779 891/* Instructions which appear in a stub. */
3d6746ca
DD
892#define STUB_LW(abfd) \
893 ((ABI_64_P (abfd) \
894 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
895 : 0x8f998010)) /* lw t9,0x8010(gp) */
896#define STUB_MOVE(abfd) \
897 ((ABI_64_P (abfd) \
898 ? 0x03e0782d /* daddu t7,ra */ \
899 : 0x03e07821)) /* addu t7,ra */
900#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
901#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
902#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
903#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
904#define STUB_LI16S(abfd, VAL) \
905 ((ABI_64_P (abfd) \
906 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
907 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
908
1bbce132
MR
909/* Likewise for the microMIPS ASE. */
910#define STUB_LW_MICROMIPS(abfd) \
911 (ABI_64_P (abfd) \
912 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
913 : 0xff3c8010) /* lw t9,0x8010(gp) */
914#define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
833794fc
MR
915#define STUB_MOVE32_MICROMIPS(abfd) \
916 (ABI_64_P (abfd) \
917 ? 0x581f7950 /* daddu t7,ra,zero */ \
918 : 0x001f7950) /* addu t7,ra,zero */
1bbce132
MR
919#define STUB_LUI_MICROMIPS(VAL) \
920 (0x41b80000 + (VAL)) /* lui t8,VAL */
921#define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
833794fc 922#define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
1bbce132
MR
923#define STUB_ORI_MICROMIPS(VAL) \
924 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
925#define STUB_LI16U_MICROMIPS(VAL) \
926 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
927#define STUB_LI16S_MICROMIPS(abfd, VAL) \
928 (ABI_64_P (abfd) \
929 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
930 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
931
5108fc1b
RS
932#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
933#define MIPS_FUNCTION_STUB_BIG_SIZE 20
1bbce132
MR
934#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
935#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
833794fc
MR
936#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
937#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
938
939/* The name of the dynamic interpreter. This is put in the .interp
940 section. */
941
942#define ELF_DYNAMIC_INTERPRETER(abfd) \
943 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
944 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
945 : "/usr/lib/libc.so.1")
946
947#ifdef BFD64
ee6423ed
AO
948#define MNAME(bfd,pre,pos) \
949 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
950#define ELF_R_SYM(bfd, i) \
951 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
952#define ELF_R_TYPE(bfd, i) \
953 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
954#define ELF_R_INFO(bfd, s, t) \
955 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
956#else
ee6423ed 957#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
958#define ELF_R_SYM(bfd, i) \
959 (ELF32_R_SYM (i))
960#define ELF_R_TYPE(bfd, i) \
961 (ELF32_R_TYPE (i))
962#define ELF_R_INFO(bfd, s, t) \
963 (ELF32_R_INFO (s, t))
964#endif
965\f
966 /* The mips16 compiler uses a couple of special sections to handle
967 floating point arguments.
968
969 Section names that look like .mips16.fn.FNNAME contain stubs that
970 copy floating point arguments from the fp regs to the gp regs and
971 then jump to FNNAME. If any 32 bit function calls FNNAME, the
972 call should be redirected to the stub instead. If no 32 bit
973 function calls FNNAME, the stub should be discarded. We need to
974 consider any reference to the function, not just a call, because
975 if the address of the function is taken we will need the stub,
976 since the address might be passed to a 32 bit function.
977
978 Section names that look like .mips16.call.FNNAME contain stubs
979 that copy floating point arguments from the gp regs to the fp
980 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
981 then any 16 bit function that calls FNNAME should be redirected
982 to the stub instead. If FNNAME is not a 32 bit function, the
983 stub should be discarded.
984
985 .mips16.call.fp.FNNAME sections are similar, but contain stubs
986 which call FNNAME and then copy the return value from the fp regs
987 to the gp regs. These stubs store the return value in $18 while
988 calling FNNAME; any function which might call one of these stubs
989 must arrange to save $18 around the call. (This case is not
990 needed for 32 bit functions that call 16 bit functions, because
991 16 bit functions always return floating point values in both
992 $f0/$f1 and $2/$3.)
993
994 Note that in all cases FNNAME might be defined statically.
995 Therefore, FNNAME is not used literally. Instead, the relocation
996 information will indicate which symbol the section is for.
997
998 We record any stubs that we find in the symbol table. */
999
1000#define FN_STUB ".mips16.fn."
1001#define CALL_STUB ".mips16.call."
1002#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
1003
1004#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1005#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1006#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 1007\f
861fb55a 1008/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
1009static const bfd_vma mips_o32_exec_plt0_entry[] =
1010{
861fb55a
DJ
1011 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1012 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1013 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1014 0x031cc023, /* subu $24, $24, $28 */
81f5d455 1015 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
861fb55a
DJ
1016 0x0018c082, /* srl $24, $24, 2 */
1017 0x0320f809, /* jalr $25 */
1018 0x2718fffe /* subu $24, $24, 2 */
1019};
1020
1021/* The format of the first PLT entry in an N32 executable. Different
1022 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
1023static const bfd_vma mips_n32_exec_plt0_entry[] =
1024{
861fb55a
DJ
1025 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1026 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1027 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1028 0x030ec023, /* subu $24, $24, $14 */
81f5d455 1029 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
861fb55a
DJ
1030 0x0018c082, /* srl $24, $24, 2 */
1031 0x0320f809, /* jalr $25 */
1032 0x2718fffe /* subu $24, $24, 2 */
1033};
1034
1035/* The format of the first PLT entry in an N64 executable. Different
1036 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
1037static const bfd_vma mips_n64_exec_plt0_entry[] =
1038{
861fb55a
DJ
1039 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1040 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1041 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1042 0x030ec023, /* subu $24, $24, $14 */
81f5d455 1043 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
861fb55a
DJ
1044 0x0018c0c2, /* srl $24, $24, 3 */
1045 0x0320f809, /* jalr $25 */
1046 0x2718fffe /* subu $24, $24, 2 */
1047};
1048
1bbce132
MR
1049/* The format of the microMIPS first PLT entry in an O32 executable.
1050 We rely on v0 ($2) rather than t8 ($24) to contain the address
1051 of the GOTPLT entry handled, so this stub may only be used when
1052 all the subsequent PLT entries are microMIPS code too.
1053
1054 The trailing NOP is for alignment and correct disassembly only. */
1055static const bfd_vma micromips_o32_exec_plt0_entry[] =
1056{
1057 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1058 0xff23, 0x0000, /* lw $25, 0($3) */
1059 0x0535, /* subu $2, $2, $3 */
1060 0x2525, /* srl $2, $2, 2 */
1061 0x3302, 0xfffe, /* subu $24, $2, 2 */
1062 0x0dff, /* move $15, $31 */
1063 0x45f9, /* jalrs $25 */
1064 0x0f83, /* move $28, $3 */
1065 0x0c00 /* nop */
1066};
1067
833794fc
MR
1068/* The format of the microMIPS first PLT entry in an O32 executable
1069 in the insn32 mode. */
1070static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1071{
1072 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1073 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1074 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1075 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1076 0x001f, 0x7950, /* move $15, $31 */
1077 0x0318, 0x1040, /* srl $24, $24, 2 */
1078 0x03f9, 0x0f3c, /* jalr $25 */
1079 0x3318, 0xfffe /* subu $24, $24, 2 */
1080};
1081
1bbce132 1082/* The format of subsequent standard PLT entries. */
6d30f5b2
NC
1083static const bfd_vma mips_exec_plt_entry[] =
1084{
861fb55a
DJ
1085 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1086 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1087 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1088 0x03200008 /* jr $25 */
1089};
1090
1bbce132
MR
1091/* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1092 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1093 directly addressable. */
1094static const bfd_vma mips16_o32_exec_plt_entry[] =
1095{
1096 0xb203, /* lw $2, 12($pc) */
1097 0x9a60, /* lw $3, 0($2) */
1098 0x651a, /* move $24, $2 */
1099 0xeb00, /* jr $3 */
1100 0x653b, /* move $25, $3 */
1101 0x6500, /* nop */
1102 0x0000, 0x0000 /* .word (.got.plt entry) */
1103};
1104
1105/* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1106 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1107static const bfd_vma micromips_o32_exec_plt_entry[] =
1108{
1109 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1110 0xff22, 0x0000, /* lw $25, 0($2) */
1111 0x4599, /* jr $25 */
1112 0x0f02 /* move $24, $2 */
1113};
1114
833794fc
MR
1115/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1116static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1117{
1118 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1119 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1120 0x0019, 0x0f3c, /* jr $25 */
1121 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1122};
1123
0a44bf69 1124/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
1125static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1126{
0a44bf69
RS
1127 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1128 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1129 0x8f390008, /* lw t9, 8(t9) */
1130 0x00000000, /* nop */
1131 0x03200008, /* jr t9 */
1132 0x00000000 /* nop */
1133};
1134
1135/* The format of subsequent PLT entries. */
6d30f5b2
NC
1136static const bfd_vma mips_vxworks_exec_plt_entry[] =
1137{
0a44bf69
RS
1138 0x10000000, /* b .PLT_resolver */
1139 0x24180000, /* li t8, <pltindex> */
1140 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1141 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1142 0x8f390000, /* lw t9, 0(t9) */
1143 0x00000000, /* nop */
1144 0x03200008, /* jr t9 */
1145 0x00000000 /* nop */
1146};
1147
1148/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
1149static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1150{
0a44bf69
RS
1151 0x8f990008, /* lw t9, 8(gp) */
1152 0x00000000, /* nop */
1153 0x03200008, /* jr t9 */
1154 0x00000000, /* nop */
1155 0x00000000, /* nop */
1156 0x00000000 /* nop */
1157};
1158
1159/* The format of subsequent PLT entries. */
6d30f5b2
NC
1160static const bfd_vma mips_vxworks_shared_plt_entry[] =
1161{
0a44bf69
RS
1162 0x10000000, /* b .PLT_resolver */
1163 0x24180000 /* li t8, <pltindex> */
1164};
1165\f
d21911ea
MR
1166/* microMIPS 32-bit opcode helper installer. */
1167
1168static void
1169bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1170{
1171 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1172 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1173}
1174
1175/* microMIPS 32-bit opcode helper retriever. */
1176
1177static bfd_vma
1178bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1179{
1180 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1181}
1182\f
b49e97c9
TS
1183/* Look up an entry in a MIPS ELF linker hash table. */
1184
1185#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1186 ((struct mips_elf_link_hash_entry *) \
1187 elf_link_hash_lookup (&(table)->root, (string), (create), \
1188 (copy), (follow)))
1189
1190/* Traverse a MIPS ELF linker hash table. */
1191
1192#define mips_elf_link_hash_traverse(table, func, info) \
1193 (elf_link_hash_traverse \
1194 (&(table)->root, \
9719ad41 1195 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1196 (info)))
1197
0f20cc35
DJ
1198/* Find the base offsets for thread-local storage in this object,
1199 for GD/LD and IE/LE respectively. */
1200
1201#define TP_OFFSET 0x7000
1202#define DTP_OFFSET 0x8000
1203
1204static bfd_vma
1205dtprel_base (struct bfd_link_info *info)
1206{
1207 /* If tls_sec is NULL, we should have signalled an error already. */
1208 if (elf_hash_table (info)->tls_sec == NULL)
1209 return 0;
1210 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1211}
1212
1213static bfd_vma
1214tprel_base (struct bfd_link_info *info)
1215{
1216 /* If tls_sec is NULL, we should have signalled an error already. */
1217 if (elf_hash_table (info)->tls_sec == NULL)
1218 return 0;
1219 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1220}
1221
b49e97c9
TS
1222/* Create an entry in a MIPS ELF linker hash table. */
1223
1224static struct bfd_hash_entry *
9719ad41
RS
1225mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1226 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1227{
1228 struct mips_elf_link_hash_entry *ret =
1229 (struct mips_elf_link_hash_entry *) entry;
1230
1231 /* Allocate the structure if it has not already been allocated by a
1232 subclass. */
9719ad41
RS
1233 if (ret == NULL)
1234 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1235 if (ret == NULL)
b49e97c9
TS
1236 return (struct bfd_hash_entry *) ret;
1237
1238 /* Call the allocation method of the superclass. */
1239 ret = ((struct mips_elf_link_hash_entry *)
1240 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1241 table, string));
9719ad41 1242 if (ret != NULL)
b49e97c9
TS
1243 {
1244 /* Set local fields. */
1245 memset (&ret->esym, 0, sizeof (EXTR));
1246 /* We use -2 as a marker to indicate that the information has
1247 not been set. -1 means there is no associated ifd. */
1248 ret->esym.ifd = -2;
861fb55a 1249 ret->la25_stub = 0;
b49e97c9 1250 ret->possibly_dynamic_relocs = 0;
b49e97c9 1251 ret->fn_stub = NULL;
b49e97c9
TS
1252 ret->call_stub = NULL;
1253 ret->call_fp_stub = NULL;
634835ae 1254 ret->global_got_area = GGA_NONE;
6ccf4795 1255 ret->got_only_for_calls = TRUE;
71782a75 1256 ret->readonly_reloc = FALSE;
861fb55a 1257 ret->has_static_relocs = FALSE;
71782a75
RS
1258 ret->no_fn_stub = FALSE;
1259 ret->need_fn_stub = FALSE;
861fb55a 1260 ret->has_nonpic_branches = FALSE;
33bb52fb 1261 ret->needs_lazy_stub = FALSE;
1bbce132 1262 ret->use_plt_entry = FALSE;
b49e97c9
TS
1263 }
1264
1265 return (struct bfd_hash_entry *) ret;
1266}
f0abc2a1 1267
6ae68ba3
MR
1268/* Allocate MIPS ELF private object data. */
1269
1270bfd_boolean
1271_bfd_mips_elf_mkobject (bfd *abfd)
1272{
1273 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1274 MIPS_ELF_DATA);
1275}
1276
f0abc2a1 1277bfd_boolean
9719ad41 1278_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1279{
f592407e
AM
1280 if (!sec->used_by_bfd)
1281 {
1282 struct _mips_elf_section_data *sdata;
1283 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1284
f592407e
AM
1285 sdata = bfd_zalloc (abfd, amt);
1286 if (sdata == NULL)
1287 return FALSE;
1288 sec->used_by_bfd = sdata;
1289 }
f0abc2a1
AM
1290
1291 return _bfd_elf_new_section_hook (abfd, sec);
1292}
b49e97c9
TS
1293\f
1294/* Read ECOFF debugging information from a .mdebug section into a
1295 ecoff_debug_info structure. */
1296
b34976b6 1297bfd_boolean
9719ad41
RS
1298_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1299 struct ecoff_debug_info *debug)
b49e97c9
TS
1300{
1301 HDRR *symhdr;
1302 const struct ecoff_debug_swap *swap;
9719ad41 1303 char *ext_hdr;
b49e97c9
TS
1304
1305 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1306 memset (debug, 0, sizeof (*debug));
1307
9719ad41 1308 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1309 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1310 goto error_return;
1311
9719ad41 1312 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1313 swap->external_hdr_size))
b49e97c9
TS
1314 goto error_return;
1315
1316 symhdr = &debug->symbolic_header;
1317 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1318
1319 /* The symbolic header contains absolute file offsets and sizes to
1320 read. */
1321#define READ(ptr, offset, count, size, type) \
1322 if (symhdr->count == 0) \
1323 debug->ptr = NULL; \
1324 else \
1325 { \
1326 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1327 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1328 if (debug->ptr == NULL) \
1329 goto error_return; \
9719ad41 1330 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1331 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1332 goto error_return; \
1333 }
1334
1335 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1336 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1337 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1338 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1339 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1340 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1341 union aux_ext *);
1342 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1343 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1344 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1345 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1346 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1347#undef READ
1348
1349 debug->fdr = NULL;
b49e97c9 1350
b34976b6 1351 return TRUE;
b49e97c9
TS
1352
1353 error_return:
1354 if (ext_hdr != NULL)
1355 free (ext_hdr);
1356 if (debug->line != NULL)
1357 free (debug->line);
1358 if (debug->external_dnr != NULL)
1359 free (debug->external_dnr);
1360 if (debug->external_pdr != NULL)
1361 free (debug->external_pdr);
1362 if (debug->external_sym != NULL)
1363 free (debug->external_sym);
1364 if (debug->external_opt != NULL)
1365 free (debug->external_opt);
1366 if (debug->external_aux != NULL)
1367 free (debug->external_aux);
1368 if (debug->ss != NULL)
1369 free (debug->ss);
1370 if (debug->ssext != NULL)
1371 free (debug->ssext);
1372 if (debug->external_fdr != NULL)
1373 free (debug->external_fdr);
1374 if (debug->external_rfd != NULL)
1375 free (debug->external_rfd);
1376 if (debug->external_ext != NULL)
1377 free (debug->external_ext);
b34976b6 1378 return FALSE;
b49e97c9
TS
1379}
1380\f
1381/* Swap RPDR (runtime procedure table entry) for output. */
1382
1383static void
9719ad41 1384ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1385{
1386 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1387 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1388 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1389 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1390 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1391 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1392
1393 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1394 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1395
1396 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1397}
1398
1399/* Create a runtime procedure table from the .mdebug section. */
1400
b34976b6 1401static bfd_boolean
9719ad41
RS
1402mips_elf_create_procedure_table (void *handle, bfd *abfd,
1403 struct bfd_link_info *info, asection *s,
1404 struct ecoff_debug_info *debug)
b49e97c9
TS
1405{
1406 const struct ecoff_debug_swap *swap;
1407 HDRR *hdr = &debug->symbolic_header;
1408 RPDR *rpdr, *rp;
1409 struct rpdr_ext *erp;
9719ad41 1410 void *rtproc;
b49e97c9
TS
1411 struct pdr_ext *epdr;
1412 struct sym_ext *esym;
1413 char *ss, **sv;
1414 char *str;
1415 bfd_size_type size;
1416 bfd_size_type count;
1417 unsigned long sindex;
1418 unsigned long i;
1419 PDR pdr;
1420 SYMR sym;
1421 const char *no_name_func = _("static procedure (no name)");
1422
1423 epdr = NULL;
1424 rpdr = NULL;
1425 esym = NULL;
1426 ss = NULL;
1427 sv = NULL;
1428
1429 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1430
1431 sindex = strlen (no_name_func) + 1;
1432 count = hdr->ipdMax;
1433 if (count > 0)
1434 {
1435 size = swap->external_pdr_size;
1436
9719ad41 1437 epdr = bfd_malloc (size * count);
b49e97c9
TS
1438 if (epdr == NULL)
1439 goto error_return;
1440
9719ad41 1441 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1442 goto error_return;
1443
1444 size = sizeof (RPDR);
9719ad41 1445 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1446 if (rpdr == NULL)
1447 goto error_return;
1448
1449 size = sizeof (char *);
9719ad41 1450 sv = bfd_malloc (size * count);
b49e97c9
TS
1451 if (sv == NULL)
1452 goto error_return;
1453
1454 count = hdr->isymMax;
1455 size = swap->external_sym_size;
9719ad41 1456 esym = bfd_malloc (size * count);
b49e97c9
TS
1457 if (esym == NULL)
1458 goto error_return;
1459
9719ad41 1460 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1461 goto error_return;
1462
1463 count = hdr->issMax;
9719ad41 1464 ss = bfd_malloc (count);
b49e97c9
TS
1465 if (ss == NULL)
1466 goto error_return;
f075ee0c 1467 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1468 goto error_return;
1469
1470 count = hdr->ipdMax;
1471 for (i = 0; i < (unsigned long) count; i++, rp++)
1472 {
9719ad41
RS
1473 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1474 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1475 rp->adr = sym.value;
1476 rp->regmask = pdr.regmask;
1477 rp->regoffset = pdr.regoffset;
1478 rp->fregmask = pdr.fregmask;
1479 rp->fregoffset = pdr.fregoffset;
1480 rp->frameoffset = pdr.frameoffset;
1481 rp->framereg = pdr.framereg;
1482 rp->pcreg = pdr.pcreg;
1483 rp->irpss = sindex;
1484 sv[i] = ss + sym.iss;
1485 sindex += strlen (sv[i]) + 1;
1486 }
1487 }
1488
1489 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1490 size = BFD_ALIGN (size, 16);
9719ad41 1491 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1492 if (rtproc == NULL)
1493 {
1494 mips_elf_hash_table (info)->procedure_count = 0;
1495 goto error_return;
1496 }
1497
1498 mips_elf_hash_table (info)->procedure_count = count + 2;
1499
9719ad41 1500 erp = rtproc;
b49e97c9
TS
1501 memset (erp, 0, sizeof (struct rpdr_ext));
1502 erp++;
1503 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1504 strcpy (str, no_name_func);
1505 str += strlen (no_name_func) + 1;
1506 for (i = 0; i < count; i++)
1507 {
1508 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1509 strcpy (str, sv[i]);
1510 str += strlen (sv[i]) + 1;
1511 }
1512 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1513
1514 /* Set the size and contents of .rtproc section. */
eea6121a 1515 s->size = size;
9719ad41 1516 s->contents = rtproc;
b49e97c9
TS
1517
1518 /* Skip this section later on (I don't think this currently
1519 matters, but someday it might). */
8423293d 1520 s->map_head.link_order = NULL;
b49e97c9
TS
1521
1522 if (epdr != NULL)
1523 free (epdr);
1524 if (rpdr != NULL)
1525 free (rpdr);
1526 if (esym != NULL)
1527 free (esym);
1528 if (ss != NULL)
1529 free (ss);
1530 if (sv != NULL)
1531 free (sv);
1532
b34976b6 1533 return TRUE;
b49e97c9
TS
1534
1535 error_return:
1536 if (epdr != NULL)
1537 free (epdr);
1538 if (rpdr != NULL)
1539 free (rpdr);
1540 if (esym != NULL)
1541 free (esym);
1542 if (ss != NULL)
1543 free (ss);
1544 if (sv != NULL)
1545 free (sv);
b34976b6 1546 return FALSE;
b49e97c9 1547}
738e5348 1548\f
861fb55a
DJ
1549/* We're going to create a stub for H. Create a symbol for the stub's
1550 value and size, to help make the disassembly easier to read. */
1551
1552static bfd_boolean
1553mips_elf_create_stub_symbol (struct bfd_link_info *info,
1554 struct mips_elf_link_hash_entry *h,
1555 const char *prefix, asection *s, bfd_vma value,
1556 bfd_vma size)
1557{
1558 struct bfd_link_hash_entry *bh;
1559 struct elf_link_hash_entry *elfh;
1560 const char *name;
1561
df58fc94
RS
1562 if (ELF_ST_IS_MICROMIPS (h->root.other))
1563 value |= 1;
1564
861fb55a
DJ
1565 /* Create a new symbol. */
1566 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1567 bh = NULL;
1568 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1569 BSF_LOCAL, s, value, NULL,
1570 TRUE, FALSE, &bh))
1571 return FALSE;
1572
1573 /* Make it a local function. */
1574 elfh = (struct elf_link_hash_entry *) bh;
1575 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1576 elfh->size = size;
1577 elfh->forced_local = 1;
1578 return TRUE;
1579}
1580
738e5348
RS
1581/* We're about to redefine H. Create a symbol to represent H's
1582 current value and size, to help make the disassembly easier
1583 to read. */
1584
1585static bfd_boolean
1586mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1587 struct mips_elf_link_hash_entry *h,
1588 const char *prefix)
1589{
1590 struct bfd_link_hash_entry *bh;
1591 struct elf_link_hash_entry *elfh;
1592 const char *name;
1593 asection *s;
1594 bfd_vma value;
1595
1596 /* Read the symbol's value. */
1597 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1598 || h->root.root.type == bfd_link_hash_defweak);
1599 s = h->root.root.u.def.section;
1600 value = h->root.root.u.def.value;
1601
1602 /* Create a new symbol. */
1603 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1604 bh = NULL;
1605 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1606 BSF_LOCAL, s, value, NULL,
1607 TRUE, FALSE, &bh))
1608 return FALSE;
1609
1610 /* Make it local and copy the other attributes from H. */
1611 elfh = (struct elf_link_hash_entry *) bh;
1612 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1613 elfh->other = h->root.other;
1614 elfh->size = h->root.size;
1615 elfh->forced_local = 1;
1616 return TRUE;
1617}
1618
1619/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1620 function rather than to a hard-float stub. */
1621
1622static bfd_boolean
1623section_allows_mips16_refs_p (asection *section)
1624{
1625 const char *name;
1626
1627 name = bfd_get_section_name (section->owner, section);
1628 return (FN_STUB_P (name)
1629 || CALL_STUB_P (name)
1630 || CALL_FP_STUB_P (name)
1631 || strcmp (name, ".pdr") == 0);
1632}
1633
1634/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1635 stub section of some kind. Return the R_SYMNDX of the target
1636 function, or 0 if we can't decide which function that is. */
1637
1638static unsigned long
cb4437b8
MR
1639mips16_stub_symndx (const struct elf_backend_data *bed,
1640 asection *sec ATTRIBUTE_UNUSED,
502e814e 1641 const Elf_Internal_Rela *relocs,
738e5348
RS
1642 const Elf_Internal_Rela *relend)
1643{
cb4437b8 1644 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
738e5348
RS
1645 const Elf_Internal_Rela *rel;
1646
cb4437b8
MR
1647 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1648 one in a compound relocation. */
1649 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
738e5348
RS
1650 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1651 return ELF_R_SYM (sec->owner, rel->r_info);
1652
1653 /* Otherwise trust the first relocation, whatever its kind. This is
1654 the traditional behavior. */
1655 if (relocs < relend)
1656 return ELF_R_SYM (sec->owner, relocs->r_info);
1657
1658 return 0;
1659}
b49e97c9
TS
1660
1661/* Check the mips16 stubs for a particular symbol, and see if we can
1662 discard them. */
1663
861fb55a
DJ
1664static void
1665mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1666 struct mips_elf_link_hash_entry *h)
b49e97c9 1667{
738e5348
RS
1668 /* Dynamic symbols must use the standard call interface, in case other
1669 objects try to call them. */
1670 if (h->fn_stub != NULL
1671 && h->root.dynindx != -1)
1672 {
1673 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1674 h->need_fn_stub = TRUE;
1675 }
1676
b49e97c9
TS
1677 if (h->fn_stub != NULL
1678 && ! h->need_fn_stub)
1679 {
1680 /* We don't need the fn_stub; the only references to this symbol
1681 are 16 bit calls. Clobber the size to 0 to prevent it from
1682 being included in the link. */
eea6121a 1683 h->fn_stub->size = 0;
b49e97c9
TS
1684 h->fn_stub->flags &= ~SEC_RELOC;
1685 h->fn_stub->reloc_count = 0;
1686 h->fn_stub->flags |= SEC_EXCLUDE;
1687 }
1688
1689 if (h->call_stub != NULL
30c09090 1690 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1691 {
1692 /* We don't need the call_stub; this is a 16 bit function, so
1693 calls from other 16 bit functions are OK. Clobber the size
1694 to 0 to prevent it from being included in the link. */
eea6121a 1695 h->call_stub->size = 0;
b49e97c9
TS
1696 h->call_stub->flags &= ~SEC_RELOC;
1697 h->call_stub->reloc_count = 0;
1698 h->call_stub->flags |= SEC_EXCLUDE;
1699 }
1700
1701 if (h->call_fp_stub != NULL
30c09090 1702 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1703 {
1704 /* We don't need the call_stub; this is a 16 bit function, so
1705 calls from other 16 bit functions are OK. Clobber the size
1706 to 0 to prevent it from being included in the link. */
eea6121a 1707 h->call_fp_stub->size = 0;
b49e97c9
TS
1708 h->call_fp_stub->flags &= ~SEC_RELOC;
1709 h->call_fp_stub->reloc_count = 0;
1710 h->call_fp_stub->flags |= SEC_EXCLUDE;
1711 }
861fb55a
DJ
1712}
1713
1714/* Hashtable callbacks for mips_elf_la25_stubs. */
1715
1716static hashval_t
1717mips_elf_la25_stub_hash (const void *entry_)
1718{
1719 const struct mips_elf_la25_stub *entry;
1720
1721 entry = (struct mips_elf_la25_stub *) entry_;
1722 return entry->h->root.root.u.def.section->id
1723 + entry->h->root.root.u.def.value;
1724}
1725
1726static int
1727mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1728{
1729 const struct mips_elf_la25_stub *entry1, *entry2;
1730
1731 entry1 = (struct mips_elf_la25_stub *) entry1_;
1732 entry2 = (struct mips_elf_la25_stub *) entry2_;
1733 return ((entry1->h->root.root.u.def.section
1734 == entry2->h->root.root.u.def.section)
1735 && (entry1->h->root.root.u.def.value
1736 == entry2->h->root.root.u.def.value));
1737}
1738
1739/* Called by the linker to set up the la25 stub-creation code. FN is
1740 the linker's implementation of add_stub_function. Return true on
1741 success. */
1742
1743bfd_boolean
1744_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1745 asection *(*fn) (const char *, asection *,
1746 asection *))
1747{
1748 struct mips_elf_link_hash_table *htab;
1749
1750 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1751 if (htab == NULL)
1752 return FALSE;
1753
861fb55a
DJ
1754 htab->add_stub_section = fn;
1755 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1756 mips_elf_la25_stub_eq, NULL);
1757 if (htab->la25_stubs == NULL)
1758 return FALSE;
1759
1760 return TRUE;
1761}
1762
1763/* Return true if H is a locally-defined PIC function, in the sense
8f0c309a
CLT
1764 that it or its fn_stub might need $25 to be valid on entry.
1765 Note that MIPS16 functions set up $gp using PC-relative instructions,
1766 so they themselves never need $25 to be valid. Only non-MIPS16
1767 entry points are of interest here. */
861fb55a
DJ
1768
1769static bfd_boolean
1770mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1771{
1772 return ((h->root.root.type == bfd_link_hash_defined
1773 || h->root.root.type == bfd_link_hash_defweak)
1774 && h->root.def_regular
1775 && !bfd_is_abs_section (h->root.root.u.def.section)
8f0c309a
CLT
1776 && (!ELF_ST_IS_MIPS16 (h->root.other)
1777 || (h->fn_stub && h->need_fn_stub))
861fb55a
DJ
1778 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1779 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1780}
1781
8f0c309a
CLT
1782/* Set *SEC to the input section that contains the target of STUB.
1783 Return the offset of the target from the start of that section. */
1784
1785static bfd_vma
1786mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1787 asection **sec)
1788{
1789 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1790 {
1791 BFD_ASSERT (stub->h->need_fn_stub);
1792 *sec = stub->h->fn_stub;
1793 return 0;
1794 }
1795 else
1796 {
1797 *sec = stub->h->root.root.u.def.section;
1798 return stub->h->root.root.u.def.value;
1799 }
1800}
1801
861fb55a
DJ
1802/* STUB describes an la25 stub that we have decided to implement
1803 by inserting an LUI/ADDIU pair before the target function.
1804 Create the section and redirect the function symbol to it. */
1805
1806static bfd_boolean
1807mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1808 struct bfd_link_info *info)
1809{
1810 struct mips_elf_link_hash_table *htab;
1811 char *name;
1812 asection *s, *input_section;
1813 unsigned int align;
1814
1815 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1816 if (htab == NULL)
1817 return FALSE;
861fb55a
DJ
1818
1819 /* Create a unique name for the new section. */
1820 name = bfd_malloc (11 + sizeof (".text.stub."));
1821 if (name == NULL)
1822 return FALSE;
1823 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1824
1825 /* Create the section. */
8f0c309a 1826 mips_elf_get_la25_target (stub, &input_section);
861fb55a
DJ
1827 s = htab->add_stub_section (name, input_section,
1828 input_section->output_section);
1829 if (s == NULL)
1830 return FALSE;
1831
1832 /* Make sure that any padding goes before the stub. */
1833 align = input_section->alignment_power;
1834 if (!bfd_set_section_alignment (s->owner, s, align))
1835 return FALSE;
1836 if (align > 3)
1837 s->size = (1 << align) - 8;
1838
1839 /* Create a symbol for the stub. */
1840 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1841 stub->stub_section = s;
1842 stub->offset = s->size;
1843
1844 /* Allocate room for it. */
1845 s->size += 8;
1846 return TRUE;
1847}
1848
1849/* STUB describes an la25 stub that we have decided to implement
1850 with a separate trampoline. Allocate room for it and redirect
1851 the function symbol to it. */
1852
1853static bfd_boolean
1854mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1855 struct bfd_link_info *info)
1856{
1857 struct mips_elf_link_hash_table *htab;
1858 asection *s;
1859
1860 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1861 if (htab == NULL)
1862 return FALSE;
861fb55a
DJ
1863
1864 /* Create a trampoline section, if we haven't already. */
1865 s = htab->strampoline;
1866 if (s == NULL)
1867 {
1868 asection *input_section = stub->h->root.root.u.def.section;
1869 s = htab->add_stub_section (".text", NULL,
1870 input_section->output_section);
1871 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1872 return FALSE;
1873 htab->strampoline = s;
1874 }
1875
1876 /* Create a symbol for the stub. */
1877 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1878 stub->stub_section = s;
1879 stub->offset = s->size;
1880
1881 /* Allocate room for it. */
1882 s->size += 16;
1883 return TRUE;
1884}
1885
1886/* H describes a symbol that needs an la25 stub. Make sure that an
1887 appropriate stub exists and point H at it. */
1888
1889static bfd_boolean
1890mips_elf_add_la25_stub (struct bfd_link_info *info,
1891 struct mips_elf_link_hash_entry *h)
1892{
1893 struct mips_elf_link_hash_table *htab;
1894 struct mips_elf_la25_stub search, *stub;
1895 bfd_boolean use_trampoline_p;
1896 asection *s;
1897 bfd_vma value;
1898 void **slot;
1899
861fb55a
DJ
1900 /* Describe the stub we want. */
1901 search.stub_section = NULL;
1902 search.offset = 0;
1903 search.h = h;
1904
1905 /* See if we've already created an equivalent stub. */
1906 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1907 if (htab == NULL)
1908 return FALSE;
1909
861fb55a
DJ
1910 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1911 if (slot == NULL)
1912 return FALSE;
1913
1914 stub = (struct mips_elf_la25_stub *) *slot;
1915 if (stub != NULL)
1916 {
1917 /* We can reuse the existing stub. */
1918 h->la25_stub = stub;
1919 return TRUE;
1920 }
1921
1922 /* Create a permanent copy of ENTRY and add it to the hash table. */
1923 stub = bfd_malloc (sizeof (search));
1924 if (stub == NULL)
1925 return FALSE;
1926 *stub = search;
1927 *slot = stub;
1928
8f0c309a
CLT
1929 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1930 of the section and if we would need no more than 2 nops. */
1931 value = mips_elf_get_la25_target (stub, &s);
1932 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1933
861fb55a
DJ
1934 h->la25_stub = stub;
1935 return (use_trampoline_p
1936 ? mips_elf_add_la25_trampoline (stub, info)
1937 : mips_elf_add_la25_intro (stub, info));
1938}
1939
1940/* A mips_elf_link_hash_traverse callback that is called before sizing
1941 sections. DATA points to a mips_htab_traverse_info structure. */
1942
1943static bfd_boolean
1944mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1945{
1946 struct mips_htab_traverse_info *hti;
1947
1948 hti = (struct mips_htab_traverse_info *) data;
861fb55a
DJ
1949 if (!hti->info->relocatable)
1950 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1951
861fb55a
DJ
1952 if (mips_elf_local_pic_function_p (h))
1953 {
ba85c43e
NC
1954 /* PR 12845: If H is in a section that has been garbage
1955 collected it will have its output section set to *ABS*. */
1956 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1957 return TRUE;
1958
861fb55a
DJ
1959 /* H is a function that might need $25 to be valid on entry.
1960 If we're creating a non-PIC relocatable object, mark H as
1961 being PIC. If we're creating a non-relocatable object with
1962 non-PIC branches and jumps to H, make sure that H has an la25
1963 stub. */
1964 if (hti->info->relocatable)
1965 {
1966 if (!PIC_OBJECT_P (hti->output_bfd))
1967 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1968 }
1969 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1970 {
1971 hti->error = TRUE;
1972 return FALSE;
1973 }
1974 }
b34976b6 1975 return TRUE;
b49e97c9
TS
1976}
1977\f
d6f16593
MR
1978/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1979 Most mips16 instructions are 16 bits, but these instructions
1980 are 32 bits.
1981
1982 The format of these instructions is:
1983
1984 +--------------+--------------------------------+
1985 | JALX | X| Imm 20:16 | Imm 25:21 |
1986 +--------------+--------------------------------+
1987 | Immediate 15:0 |
1988 +-----------------------------------------------+
1989
1990 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1991 Note that the immediate value in the first word is swapped.
1992
1993 When producing a relocatable object file, R_MIPS16_26 is
1994 handled mostly like R_MIPS_26. In particular, the addend is
1995 stored as a straight 26-bit value in a 32-bit instruction.
1996 (gas makes life simpler for itself by never adjusting a
1997 R_MIPS16_26 reloc to be against a section, so the addend is
1998 always zero). However, the 32 bit instruction is stored as 2
1999 16-bit values, rather than a single 32-bit value. In a
2000 big-endian file, the result is the same; in a little-endian
2001 file, the two 16-bit halves of the 32 bit value are swapped.
2002 This is so that a disassembler can recognize the jal
2003 instruction.
2004
2005 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2006 instruction stored as two 16-bit values. The addend A is the
2007 contents of the targ26 field. The calculation is the same as
2008 R_MIPS_26. When storing the calculated value, reorder the
2009 immediate value as shown above, and don't forget to store the
2010 value as two 16-bit values.
2011
2012 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2013 defined as
2014
2015 big-endian:
2016 +--------+----------------------+
2017 | | |
2018 | | targ26-16 |
2019 |31 26|25 0|
2020 +--------+----------------------+
2021
2022 little-endian:
2023 +----------+------+-------------+
2024 | | | |
2025 | sub1 | | sub2 |
2026 |0 9|10 15|16 31|
2027 +----------+--------------------+
2028 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2029 ((sub1 << 16) | sub2)).
2030
2031 When producing a relocatable object file, the calculation is
2032 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2033 When producing a fully linked file, the calculation is
2034 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2035 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2036
738e5348
RS
2037 The table below lists the other MIPS16 instruction relocations.
2038 Each one is calculated in the same way as the non-MIPS16 relocation
2039 given on the right, but using the extended MIPS16 layout of 16-bit
2040 immediate fields:
2041
2042 R_MIPS16_GPREL R_MIPS_GPREL16
2043 R_MIPS16_GOT16 R_MIPS_GOT16
2044 R_MIPS16_CALL16 R_MIPS_CALL16
2045 R_MIPS16_HI16 R_MIPS_HI16
2046 R_MIPS16_LO16 R_MIPS_LO16
2047
2048 A typical instruction will have a format like this:
d6f16593
MR
2049
2050 +--------------+--------------------------------+
2051 | EXTEND | Imm 10:5 | Imm 15:11 |
2052 +--------------+--------------------------------+
2053 | Major | rx | ry | Imm 4:0 |
2054 +--------------+--------------------------------+
2055
2056 EXTEND is the five bit value 11110. Major is the instruction
2057 opcode.
2058
738e5348
RS
2059 All we need to do here is shuffle the bits appropriately.
2060 As above, the two 16-bit halves must be swapped on a
2061 little-endian system. */
2062
2063static inline bfd_boolean
2064mips16_reloc_p (int r_type)
2065{
2066 switch (r_type)
2067 {
2068 case R_MIPS16_26:
2069 case R_MIPS16_GPREL:
2070 case R_MIPS16_GOT16:
2071 case R_MIPS16_CALL16:
2072 case R_MIPS16_HI16:
2073 case R_MIPS16_LO16:
d0f13682
CLT
2074 case R_MIPS16_TLS_GD:
2075 case R_MIPS16_TLS_LDM:
2076 case R_MIPS16_TLS_DTPREL_HI16:
2077 case R_MIPS16_TLS_DTPREL_LO16:
2078 case R_MIPS16_TLS_GOTTPREL:
2079 case R_MIPS16_TLS_TPREL_HI16:
2080 case R_MIPS16_TLS_TPREL_LO16:
738e5348
RS
2081 return TRUE;
2082
2083 default:
2084 return FALSE;
2085 }
2086}
2087
df58fc94
RS
2088/* Check if a microMIPS reloc. */
2089
2090static inline bfd_boolean
2091micromips_reloc_p (unsigned int r_type)
2092{
2093 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2094}
2095
2096/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2097 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2098 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2099
2100static inline bfd_boolean
2101micromips_reloc_shuffle_p (unsigned int r_type)
2102{
2103 return (micromips_reloc_p (r_type)
2104 && r_type != R_MICROMIPS_PC7_S1
2105 && r_type != R_MICROMIPS_PC10_S1);
2106}
2107
738e5348
RS
2108static inline bfd_boolean
2109got16_reloc_p (int r_type)
2110{
df58fc94
RS
2111 return (r_type == R_MIPS_GOT16
2112 || r_type == R_MIPS16_GOT16
2113 || r_type == R_MICROMIPS_GOT16);
738e5348
RS
2114}
2115
2116static inline bfd_boolean
2117call16_reloc_p (int r_type)
2118{
df58fc94
RS
2119 return (r_type == R_MIPS_CALL16
2120 || r_type == R_MIPS16_CALL16
2121 || r_type == R_MICROMIPS_CALL16);
2122}
2123
2124static inline bfd_boolean
2125got_disp_reloc_p (unsigned int r_type)
2126{
2127 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2128}
2129
2130static inline bfd_boolean
2131got_page_reloc_p (unsigned int r_type)
2132{
2133 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2134}
2135
2136static inline bfd_boolean
2137got_ofst_reloc_p (unsigned int r_type)
2138{
2139 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
2140}
2141
2142static inline bfd_boolean
2143got_hi16_reloc_p (unsigned int r_type)
2144{
2145 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
2146}
2147
2148static inline bfd_boolean
2149got_lo16_reloc_p (unsigned int r_type)
2150{
2151 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2152}
2153
2154static inline bfd_boolean
2155call_hi16_reloc_p (unsigned int r_type)
2156{
2157 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2158}
2159
2160static inline bfd_boolean
2161call_lo16_reloc_p (unsigned int r_type)
2162{
2163 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
738e5348
RS
2164}
2165
2166static inline bfd_boolean
2167hi16_reloc_p (int r_type)
2168{
df58fc94
RS
2169 return (r_type == R_MIPS_HI16
2170 || r_type == R_MIPS16_HI16
2171 || r_type == R_MICROMIPS_HI16);
738e5348 2172}
d6f16593 2173
738e5348
RS
2174static inline bfd_boolean
2175lo16_reloc_p (int r_type)
2176{
df58fc94
RS
2177 return (r_type == R_MIPS_LO16
2178 || r_type == R_MIPS16_LO16
2179 || r_type == R_MICROMIPS_LO16);
738e5348
RS
2180}
2181
2182static inline bfd_boolean
2183mips16_call_reloc_p (int r_type)
2184{
2185 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2186}
d6f16593 2187
38a7df63
CF
2188static inline bfd_boolean
2189jal_reloc_p (int r_type)
2190{
df58fc94
RS
2191 return (r_type == R_MIPS_26
2192 || r_type == R_MIPS16_26
2193 || r_type == R_MICROMIPS_26_S1);
2194}
2195
2196static inline bfd_boolean
2197micromips_branch_reloc_p (int r_type)
2198{
2199 return (r_type == R_MICROMIPS_26_S1
2200 || r_type == R_MICROMIPS_PC16_S1
2201 || r_type == R_MICROMIPS_PC10_S1
2202 || r_type == R_MICROMIPS_PC7_S1);
2203}
2204
2205static inline bfd_boolean
2206tls_gd_reloc_p (unsigned int r_type)
2207{
d0f13682
CLT
2208 return (r_type == R_MIPS_TLS_GD
2209 || r_type == R_MIPS16_TLS_GD
2210 || r_type == R_MICROMIPS_TLS_GD);
df58fc94
RS
2211}
2212
2213static inline bfd_boolean
2214tls_ldm_reloc_p (unsigned int r_type)
2215{
d0f13682
CLT
2216 return (r_type == R_MIPS_TLS_LDM
2217 || r_type == R_MIPS16_TLS_LDM
2218 || r_type == R_MICROMIPS_TLS_LDM);
df58fc94
RS
2219}
2220
2221static inline bfd_boolean
2222tls_gottprel_reloc_p (unsigned int r_type)
2223{
d0f13682
CLT
2224 return (r_type == R_MIPS_TLS_GOTTPREL
2225 || r_type == R_MIPS16_TLS_GOTTPREL
2226 || r_type == R_MICROMIPS_TLS_GOTTPREL);
38a7df63
CF
2227}
2228
d6f16593 2229void
df58fc94
RS
2230_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2231 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2232{
df58fc94 2233 bfd_vma first, second, val;
d6f16593 2234
df58fc94 2235 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2236 return;
2237
df58fc94
RS
2238 /* Pick up the first and second halfwords of the instruction. */
2239 first = bfd_get_16 (abfd, data);
2240 second = bfd_get_16 (abfd, data + 2);
2241 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2242 val = first << 16 | second;
2243 else if (r_type != R_MIPS16_26)
2244 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2245 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
d6f16593 2246 else
df58fc94
RS
2247 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2248 | ((first & 0x1f) << 21) | second);
d6f16593
MR
2249 bfd_put_32 (abfd, val, data);
2250}
2251
2252void
df58fc94
RS
2253_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2254 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2255{
df58fc94 2256 bfd_vma first, second, val;
d6f16593 2257
df58fc94 2258 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2259 return;
2260
2261 val = bfd_get_32 (abfd, data);
df58fc94 2262 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
d6f16593 2263 {
df58fc94
RS
2264 second = val & 0xffff;
2265 first = val >> 16;
2266 }
2267 else if (r_type != R_MIPS16_26)
2268 {
2269 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2270 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
d6f16593
MR
2271 }
2272 else
2273 {
df58fc94
RS
2274 second = val & 0xffff;
2275 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2276 | ((val >> 21) & 0x1f);
d6f16593 2277 }
df58fc94
RS
2278 bfd_put_16 (abfd, second, data + 2);
2279 bfd_put_16 (abfd, first, data);
d6f16593
MR
2280}
2281
b49e97c9 2282bfd_reloc_status_type
9719ad41
RS
2283_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2284 arelent *reloc_entry, asection *input_section,
2285 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
2286{
2287 bfd_vma relocation;
a7ebbfdf 2288 bfd_signed_vma val;
30ac9238 2289 bfd_reloc_status_type status;
b49e97c9
TS
2290
2291 if (bfd_is_com_section (symbol->section))
2292 relocation = 0;
2293 else
2294 relocation = symbol->value;
2295
2296 relocation += symbol->section->output_section->vma;
2297 relocation += symbol->section->output_offset;
2298
07515404 2299 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
2300 return bfd_reloc_outofrange;
2301
b49e97c9 2302 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
2303 val = reloc_entry->addend;
2304
30ac9238 2305 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 2306
b49e97c9 2307 /* Adjust val for the final section location and GP value. If we
1049f94e 2308 are producing relocatable output, we don't want to do this for
b49e97c9 2309 an external symbol. */
1049f94e 2310 if (! relocatable
b49e97c9
TS
2311 || (symbol->flags & BSF_SECTION_SYM) != 0)
2312 val += relocation - gp;
2313
a7ebbfdf
TS
2314 if (reloc_entry->howto->partial_inplace)
2315 {
30ac9238
RS
2316 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2317 (bfd_byte *) data
2318 + reloc_entry->address);
2319 if (status != bfd_reloc_ok)
2320 return status;
a7ebbfdf
TS
2321 }
2322 else
2323 reloc_entry->addend = val;
b49e97c9 2324
1049f94e 2325 if (relocatable)
b49e97c9 2326 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2327
2328 return bfd_reloc_ok;
2329}
2330
2331/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2332 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2333 that contains the relocation field and DATA points to the start of
2334 INPUT_SECTION. */
2335
2336struct mips_hi16
2337{
2338 struct mips_hi16 *next;
2339 bfd_byte *data;
2340 asection *input_section;
2341 arelent rel;
2342};
2343
2344/* FIXME: This should not be a static variable. */
2345
2346static struct mips_hi16 *mips_hi16_list;
2347
2348/* A howto special_function for REL *HI16 relocations. We can only
2349 calculate the correct value once we've seen the partnering
2350 *LO16 relocation, so just save the information for later.
2351
2352 The ABI requires that the *LO16 immediately follow the *HI16.
2353 However, as a GNU extension, we permit an arbitrary number of
2354 *HI16s to be associated with a single *LO16. This significantly
2355 simplies the relocation handling in gcc. */
2356
2357bfd_reloc_status_type
2358_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2359 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2360 asection *input_section, bfd *output_bfd,
2361 char **error_message ATTRIBUTE_UNUSED)
2362{
2363 struct mips_hi16 *n;
2364
07515404 2365 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2366 return bfd_reloc_outofrange;
2367
2368 n = bfd_malloc (sizeof *n);
2369 if (n == NULL)
2370 return bfd_reloc_outofrange;
2371
2372 n->next = mips_hi16_list;
2373 n->data = data;
2374 n->input_section = input_section;
2375 n->rel = *reloc_entry;
2376 mips_hi16_list = n;
2377
2378 if (output_bfd != NULL)
2379 reloc_entry->address += input_section->output_offset;
2380
2381 return bfd_reloc_ok;
2382}
2383
738e5348 2384/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2385 like any other 16-bit relocation when applied to global symbols, but is
2386 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2387
2388bfd_reloc_status_type
2389_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2390 void *data, asection *input_section,
2391 bfd *output_bfd, char **error_message)
2392{
2393 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2394 || bfd_is_und_section (bfd_get_section (symbol))
2395 || bfd_is_com_section (bfd_get_section (symbol)))
2396 /* The relocation is against a global symbol. */
2397 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2398 input_section, output_bfd,
2399 error_message);
2400
2401 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2402 input_section, output_bfd, error_message);
2403}
2404
2405/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2406 is a straightforward 16 bit inplace relocation, but we must deal with
2407 any partnering high-part relocations as well. */
2408
2409bfd_reloc_status_type
2410_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2411 void *data, asection *input_section,
2412 bfd *output_bfd, char **error_message)
2413{
2414 bfd_vma vallo;
d6f16593 2415 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2416
07515404 2417 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2418 return bfd_reloc_outofrange;
2419
df58fc94 2420 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
d6f16593 2421 location);
df58fc94
RS
2422 vallo = bfd_get_32 (abfd, location);
2423 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2424 location);
d6f16593 2425
30ac9238
RS
2426 while (mips_hi16_list != NULL)
2427 {
2428 bfd_reloc_status_type ret;
2429 struct mips_hi16 *hi;
2430
2431 hi = mips_hi16_list;
2432
738e5348
RS
2433 /* R_MIPS*_GOT16 relocations are something of a special case. We
2434 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2435 relocation (with a rightshift of 16). However, since GOT16
2436 relocations can also be used with global symbols, their howto
2437 has a rightshift of 0. */
2438 if (hi->rel.howto->type == R_MIPS_GOT16)
2439 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2440 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2441 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
df58fc94
RS
2442 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2443 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
30ac9238
RS
2444
2445 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2446 carry or borrow will induce a change of +1 or -1 in the high part. */
2447 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2448
30ac9238
RS
2449 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2450 hi->input_section, output_bfd,
2451 error_message);
2452 if (ret != bfd_reloc_ok)
2453 return ret;
2454
2455 mips_hi16_list = hi->next;
2456 free (hi);
2457 }
2458
2459 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2460 input_section, output_bfd,
2461 error_message);
2462}
2463
2464/* A generic howto special_function. This calculates and installs the
2465 relocation itself, thus avoiding the oft-discussed problems in
2466 bfd_perform_relocation and bfd_install_relocation. */
2467
2468bfd_reloc_status_type
2469_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2470 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2471 asection *input_section, bfd *output_bfd,
2472 char **error_message ATTRIBUTE_UNUSED)
2473{
2474 bfd_signed_vma val;
2475 bfd_reloc_status_type status;
2476 bfd_boolean relocatable;
2477
2478 relocatable = (output_bfd != NULL);
2479
07515404 2480 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2481 return bfd_reloc_outofrange;
2482
2483 /* Build up the field adjustment in VAL. */
2484 val = 0;
2485 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2486 {
2487 /* Either we're calculating the final field value or we have a
2488 relocation against a section symbol. Add in the section's
2489 offset or address. */
2490 val += symbol->section->output_section->vma;
2491 val += symbol->section->output_offset;
2492 }
2493
2494 if (!relocatable)
2495 {
2496 /* We're calculating the final field value. Add in the symbol's value
2497 and, if pc-relative, subtract the address of the field itself. */
2498 val += symbol->value;
2499 if (reloc_entry->howto->pc_relative)
2500 {
2501 val -= input_section->output_section->vma;
2502 val -= input_section->output_offset;
2503 val -= reloc_entry->address;
2504 }
2505 }
2506
2507 /* VAL is now the final adjustment. If we're keeping this relocation
2508 in the output file, and if the relocation uses a separate addend,
2509 we just need to add VAL to that addend. Otherwise we need to add
2510 VAL to the relocation field itself. */
2511 if (relocatable && !reloc_entry->howto->partial_inplace)
2512 reloc_entry->addend += val;
2513 else
2514 {
d6f16593
MR
2515 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2516
30ac9238
RS
2517 /* Add in the separate addend, if any. */
2518 val += reloc_entry->addend;
2519
2520 /* Add VAL to the relocation field. */
df58fc94
RS
2521 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2522 location);
30ac9238 2523 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593 2524 location);
df58fc94
RS
2525 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2526 location);
d6f16593 2527
30ac9238
RS
2528 if (status != bfd_reloc_ok)
2529 return status;
2530 }
2531
2532 if (relocatable)
2533 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2534
2535 return bfd_reloc_ok;
2536}
2537\f
2538/* Swap an entry in a .gptab section. Note that these routines rely
2539 on the equivalence of the two elements of the union. */
2540
2541static void
9719ad41
RS
2542bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2543 Elf32_gptab *in)
b49e97c9
TS
2544{
2545 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2546 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2547}
2548
2549static void
9719ad41
RS
2550bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2551 Elf32_External_gptab *ex)
b49e97c9
TS
2552{
2553 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2554 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2555}
2556
2557static void
9719ad41
RS
2558bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2559 Elf32_External_compact_rel *ex)
b49e97c9
TS
2560{
2561 H_PUT_32 (abfd, in->id1, ex->id1);
2562 H_PUT_32 (abfd, in->num, ex->num);
2563 H_PUT_32 (abfd, in->id2, ex->id2);
2564 H_PUT_32 (abfd, in->offset, ex->offset);
2565 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2566 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2567}
2568
2569static void
9719ad41
RS
2570bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2571 Elf32_External_crinfo *ex)
b49e97c9
TS
2572{
2573 unsigned long l;
2574
2575 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2576 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2577 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2578 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2579 H_PUT_32 (abfd, l, ex->info);
2580 H_PUT_32 (abfd, in->konst, ex->konst);
2581 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2582}
b49e97c9
TS
2583\f
2584/* A .reginfo section holds a single Elf32_RegInfo structure. These
2585 routines swap this structure in and out. They are used outside of
2586 BFD, so they are globally visible. */
2587
2588void
9719ad41
RS
2589bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2590 Elf32_RegInfo *in)
b49e97c9
TS
2591{
2592 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2593 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2594 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2595 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2596 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2597 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2598}
2599
2600void
9719ad41
RS
2601bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2602 Elf32_External_RegInfo *ex)
b49e97c9
TS
2603{
2604 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2605 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2606 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2607 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2608 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2609 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2610}
2611
2612/* In the 64 bit ABI, the .MIPS.options section holds register
2613 information in an Elf64_Reginfo structure. These routines swap
2614 them in and out. They are globally visible because they are used
2615 outside of BFD. These routines are here so that gas can call them
2616 without worrying about whether the 64 bit ABI has been included. */
2617
2618void
9719ad41
RS
2619bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2620 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2621{
2622 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2623 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2624 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2625 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2626 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2627 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2628 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2629}
2630
2631void
9719ad41
RS
2632bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2633 Elf64_External_RegInfo *ex)
b49e97c9
TS
2634{
2635 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2636 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2637 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2638 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2639 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2640 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2641 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2642}
2643
2644/* Swap in an options header. */
2645
2646void
9719ad41
RS
2647bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2648 Elf_Internal_Options *in)
b49e97c9
TS
2649{
2650 in->kind = H_GET_8 (abfd, ex->kind);
2651 in->size = H_GET_8 (abfd, ex->size);
2652 in->section = H_GET_16 (abfd, ex->section);
2653 in->info = H_GET_32 (abfd, ex->info);
2654}
2655
2656/* Swap out an options header. */
2657
2658void
9719ad41
RS
2659bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2660 Elf_External_Options *ex)
b49e97c9
TS
2661{
2662 H_PUT_8 (abfd, in->kind, ex->kind);
2663 H_PUT_8 (abfd, in->size, ex->size);
2664 H_PUT_16 (abfd, in->section, ex->section);
2665 H_PUT_32 (abfd, in->info, ex->info);
2666}
2667\f
2668/* This function is called via qsort() to sort the dynamic relocation
2669 entries by increasing r_symndx value. */
2670
2671static int
9719ad41 2672sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2673{
947216bf
AM
2674 Elf_Internal_Rela int_reloc1;
2675 Elf_Internal_Rela int_reloc2;
6870500c 2676 int diff;
b49e97c9 2677
947216bf
AM
2678 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2679 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2680
6870500c
RS
2681 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2682 if (diff != 0)
2683 return diff;
2684
2685 if (int_reloc1.r_offset < int_reloc2.r_offset)
2686 return -1;
2687 if (int_reloc1.r_offset > int_reloc2.r_offset)
2688 return 1;
2689 return 0;
b49e97c9
TS
2690}
2691
f4416af6
AO
2692/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2693
2694static int
7e3102a7
AM
2695sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2696 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2697{
7e3102a7 2698#ifdef BFD64
f4416af6
AO
2699 Elf_Internal_Rela int_reloc1[3];
2700 Elf_Internal_Rela int_reloc2[3];
2701
2702 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2703 (reldyn_sorting_bfd, arg1, int_reloc1);
2704 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2705 (reldyn_sorting_bfd, arg2, int_reloc2);
2706
6870500c
RS
2707 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2708 return -1;
2709 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2710 return 1;
2711
2712 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2713 return -1;
2714 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2715 return 1;
2716 return 0;
7e3102a7
AM
2717#else
2718 abort ();
2719#endif
f4416af6
AO
2720}
2721
2722
b49e97c9
TS
2723/* This routine is used to write out ECOFF debugging external symbol
2724 information. It is called via mips_elf_link_hash_traverse. The
2725 ECOFF external symbol information must match the ELF external
2726 symbol information. Unfortunately, at this point we don't know
2727 whether a symbol is required by reloc information, so the two
2728 tables may wind up being different. We must sort out the external
2729 symbol information before we can set the final size of the .mdebug
2730 section, and we must set the size of the .mdebug section before we
2731 can relocate any sections, and we can't know which symbols are
2732 required by relocation until we relocate the sections.
2733 Fortunately, it is relatively unlikely that any symbol will be
2734 stripped but required by a reloc. In particular, it can not happen
2735 when generating a final executable. */
2736
b34976b6 2737static bfd_boolean
9719ad41 2738mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2739{
9719ad41 2740 struct extsym_info *einfo = data;
b34976b6 2741 bfd_boolean strip;
b49e97c9
TS
2742 asection *sec, *output_section;
2743
b49e97c9 2744 if (h->root.indx == -2)
b34976b6 2745 strip = FALSE;
f5385ebf 2746 else if ((h->root.def_dynamic
77cfaee6
AM
2747 || h->root.ref_dynamic
2748 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2749 && !h->root.def_regular
2750 && !h->root.ref_regular)
b34976b6 2751 strip = TRUE;
b49e97c9
TS
2752 else if (einfo->info->strip == strip_all
2753 || (einfo->info->strip == strip_some
2754 && bfd_hash_lookup (einfo->info->keep_hash,
2755 h->root.root.root.string,
b34976b6
AM
2756 FALSE, FALSE) == NULL))
2757 strip = TRUE;
b49e97c9 2758 else
b34976b6 2759 strip = FALSE;
b49e97c9
TS
2760
2761 if (strip)
b34976b6 2762 return TRUE;
b49e97c9
TS
2763
2764 if (h->esym.ifd == -2)
2765 {
2766 h->esym.jmptbl = 0;
2767 h->esym.cobol_main = 0;
2768 h->esym.weakext = 0;
2769 h->esym.reserved = 0;
2770 h->esym.ifd = ifdNil;
2771 h->esym.asym.value = 0;
2772 h->esym.asym.st = stGlobal;
2773
2774 if (h->root.root.type == bfd_link_hash_undefined
2775 || h->root.root.type == bfd_link_hash_undefweak)
2776 {
2777 const char *name;
2778
2779 /* Use undefined class. Also, set class and type for some
2780 special symbols. */
2781 name = h->root.root.root.string;
2782 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2783 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2784 {
2785 h->esym.asym.sc = scData;
2786 h->esym.asym.st = stLabel;
2787 h->esym.asym.value = 0;
2788 }
2789 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2790 {
2791 h->esym.asym.sc = scAbs;
2792 h->esym.asym.st = stLabel;
2793 h->esym.asym.value =
2794 mips_elf_hash_table (einfo->info)->procedure_count;
2795 }
4a14403c 2796 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2797 {
2798 h->esym.asym.sc = scAbs;
2799 h->esym.asym.st = stLabel;
2800 h->esym.asym.value = elf_gp (einfo->abfd);
2801 }
2802 else
2803 h->esym.asym.sc = scUndefined;
2804 }
2805 else if (h->root.root.type != bfd_link_hash_defined
2806 && h->root.root.type != bfd_link_hash_defweak)
2807 h->esym.asym.sc = scAbs;
2808 else
2809 {
2810 const char *name;
2811
2812 sec = h->root.root.u.def.section;
2813 output_section = sec->output_section;
2814
2815 /* When making a shared library and symbol h is the one from
2816 the another shared library, OUTPUT_SECTION may be null. */
2817 if (output_section == NULL)
2818 h->esym.asym.sc = scUndefined;
2819 else
2820 {
2821 name = bfd_section_name (output_section->owner, output_section);
2822
2823 if (strcmp (name, ".text") == 0)
2824 h->esym.asym.sc = scText;
2825 else if (strcmp (name, ".data") == 0)
2826 h->esym.asym.sc = scData;
2827 else if (strcmp (name, ".sdata") == 0)
2828 h->esym.asym.sc = scSData;
2829 else if (strcmp (name, ".rodata") == 0
2830 || strcmp (name, ".rdata") == 0)
2831 h->esym.asym.sc = scRData;
2832 else if (strcmp (name, ".bss") == 0)
2833 h->esym.asym.sc = scBss;
2834 else if (strcmp (name, ".sbss") == 0)
2835 h->esym.asym.sc = scSBss;
2836 else if (strcmp (name, ".init") == 0)
2837 h->esym.asym.sc = scInit;
2838 else if (strcmp (name, ".fini") == 0)
2839 h->esym.asym.sc = scFini;
2840 else
2841 h->esym.asym.sc = scAbs;
2842 }
2843 }
2844
2845 h->esym.asym.reserved = 0;
2846 h->esym.asym.index = indexNil;
2847 }
2848
2849 if (h->root.root.type == bfd_link_hash_common)
2850 h->esym.asym.value = h->root.root.u.c.size;
2851 else if (h->root.root.type == bfd_link_hash_defined
2852 || h->root.root.type == bfd_link_hash_defweak)
2853 {
2854 if (h->esym.asym.sc == scCommon)
2855 h->esym.asym.sc = scBss;
2856 else if (h->esym.asym.sc == scSCommon)
2857 h->esym.asym.sc = scSBss;
2858
2859 sec = h->root.root.u.def.section;
2860 output_section = sec->output_section;
2861 if (output_section != NULL)
2862 h->esym.asym.value = (h->root.root.u.def.value
2863 + sec->output_offset
2864 + output_section->vma);
2865 else
2866 h->esym.asym.value = 0;
2867 }
33bb52fb 2868 else
b49e97c9
TS
2869 {
2870 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2871
2872 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2873 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2874
33bb52fb 2875 if (hd->needs_lazy_stub)
b49e97c9 2876 {
1bbce132
MR
2877 BFD_ASSERT (hd->root.plt.plist != NULL);
2878 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
b49e97c9
TS
2879 /* Set type and value for a symbol with a function stub. */
2880 h->esym.asym.st = stProc;
2881 sec = hd->root.root.u.def.section;
2882 if (sec == NULL)
2883 h->esym.asym.value = 0;
2884 else
2885 {
2886 output_section = sec->output_section;
2887 if (output_section != NULL)
1bbce132 2888 h->esym.asym.value = (hd->root.plt.plist->stub_offset
b49e97c9
TS
2889 + sec->output_offset
2890 + output_section->vma);
2891 else
2892 h->esym.asym.value = 0;
2893 }
b49e97c9
TS
2894 }
2895 }
2896
2897 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2898 h->root.root.root.string,
2899 &h->esym))
2900 {
b34976b6
AM
2901 einfo->failed = TRUE;
2902 return FALSE;
b49e97c9
TS
2903 }
2904
b34976b6 2905 return TRUE;
b49e97c9
TS
2906}
2907
2908/* A comparison routine used to sort .gptab entries. */
2909
2910static int
9719ad41 2911gptab_compare (const void *p1, const void *p2)
b49e97c9 2912{
9719ad41
RS
2913 const Elf32_gptab *a1 = p1;
2914 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2915
2916 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2917}
2918\f
b15e6682 2919/* Functions to manage the got entry hash table. */
f4416af6
AO
2920
2921/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2922 hash number. */
2923
2924static INLINE hashval_t
9719ad41 2925mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2926{
2927#ifdef BFD64
2928 return addr + (addr >> 32);
2929#else
2930 return addr;
2931#endif
2932}
2933
f4416af6 2934static hashval_t
d9bf376d 2935mips_elf_got_entry_hash (const void *entry_)
f4416af6
AO
2936{
2937 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2938
e641e783 2939 return (entry->symndx
9ab066b4
RS
2940 + ((entry->tls_type == GOT_TLS_LDM) << 18)
2941 + (entry->tls_type == GOT_TLS_LDM ? 0
e641e783
RS
2942 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2943 : entry->symndx >= 0 ? (entry->abfd->id
2944 + mips_elf_hash_bfd_vma (entry->d.addend))
2945 : entry->d.h->root.root.root.hash));
f4416af6
AO
2946}
2947
2948static int
3dff0dd1 2949mips_elf_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2950{
2951 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2952 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2953
e641e783 2954 return (e1->symndx == e2->symndx
9ab066b4
RS
2955 && e1->tls_type == e2->tls_type
2956 && (e1->tls_type == GOT_TLS_LDM ? TRUE
e641e783
RS
2957 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
2958 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
2959 && e1->d.addend == e2->d.addend)
2960 : e2->abfd && e1->d.h == e2->d.h));
b15e6682 2961}
c224138d 2962
13db6b44
RS
2963static hashval_t
2964mips_got_page_ref_hash (const void *ref_)
2965{
2966 const struct mips_got_page_ref *ref;
2967
2968 ref = (const struct mips_got_page_ref *) ref_;
2969 return ((ref->symndx >= 0
2970 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
2971 : ref->u.h->root.root.root.hash)
2972 + mips_elf_hash_bfd_vma (ref->addend));
2973}
2974
2975static int
2976mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
2977{
2978 const struct mips_got_page_ref *ref1, *ref2;
2979
2980 ref1 = (const struct mips_got_page_ref *) ref1_;
2981 ref2 = (const struct mips_got_page_ref *) ref2_;
2982 return (ref1->symndx == ref2->symndx
2983 && (ref1->symndx < 0
2984 ? ref1->u.h == ref2->u.h
2985 : ref1->u.abfd == ref2->u.abfd)
2986 && ref1->addend == ref2->addend);
2987}
2988
c224138d
RS
2989static hashval_t
2990mips_got_page_entry_hash (const void *entry_)
2991{
2992 const struct mips_got_page_entry *entry;
2993
2994 entry = (const struct mips_got_page_entry *) entry_;
13db6b44 2995 return entry->sec->id;
c224138d
RS
2996}
2997
2998static int
2999mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3000{
3001 const struct mips_got_page_entry *entry1, *entry2;
3002
3003 entry1 = (const struct mips_got_page_entry *) entry1_;
3004 entry2 = (const struct mips_got_page_entry *) entry2_;
13db6b44 3005 return entry1->sec == entry2->sec;
c224138d 3006}
b15e6682 3007\f
3dff0dd1 3008/* Create and return a new mips_got_info structure. */
5334aa52
RS
3009
3010static struct mips_got_info *
3dff0dd1 3011mips_elf_create_got_info (bfd *abfd)
5334aa52
RS
3012{
3013 struct mips_got_info *g;
3014
3015 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3016 if (g == NULL)
3017 return NULL;
3018
3dff0dd1
RS
3019 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3020 mips_elf_got_entry_eq, NULL);
5334aa52
RS
3021 if (g->got_entries == NULL)
3022 return NULL;
3023
13db6b44
RS
3024 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3025 mips_got_page_ref_eq, NULL);
3026 if (g->got_page_refs == NULL)
5334aa52
RS
3027 return NULL;
3028
3029 return g;
3030}
3031
ee227692
RS
3032/* Return the GOT info for input bfd ABFD, trying to create a new one if
3033 CREATE_P and if ABFD doesn't already have a GOT. */
3034
3035static struct mips_got_info *
3036mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3037{
3038 struct mips_elf_obj_tdata *tdata;
3039
3040 if (!is_mips_elf (abfd))
3041 return NULL;
3042
3043 tdata = mips_elf_tdata (abfd);
3044 if (!tdata->got && create_p)
3dff0dd1 3045 tdata->got = mips_elf_create_got_info (abfd);
ee227692
RS
3046 return tdata->got;
3047}
3048
d7206569
RS
3049/* Record that ABFD should use output GOT G. */
3050
3051static void
3052mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3053{
3054 struct mips_elf_obj_tdata *tdata;
3055
3056 BFD_ASSERT (is_mips_elf (abfd));
3057 tdata = mips_elf_tdata (abfd);
3058 if (tdata->got)
3059 {
3060 /* The GOT structure itself and the hash table entries are
3061 allocated to a bfd, but the hash tables aren't. */
3062 htab_delete (tdata->got->got_entries);
13db6b44
RS
3063 htab_delete (tdata->got->got_page_refs);
3064 if (tdata->got->got_page_entries)
3065 htab_delete (tdata->got->got_page_entries);
d7206569
RS
3066 }
3067 tdata->got = g;
3068}
3069
0a44bf69
RS
3070/* Return the dynamic relocation section. If it doesn't exist, try to
3071 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3072 if creation fails. */
f4416af6
AO
3073
3074static asection *
0a44bf69 3075mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 3076{
0a44bf69 3077 const char *dname;
f4416af6 3078 asection *sreloc;
0a44bf69 3079 bfd *dynobj;
f4416af6 3080
0a44bf69
RS
3081 dname = MIPS_ELF_REL_DYN_NAME (info);
3082 dynobj = elf_hash_table (info)->dynobj;
3d4d4302 3083 sreloc = bfd_get_linker_section (dynobj, dname);
f4416af6
AO
3084 if (sreloc == NULL && create_p)
3085 {
3d4d4302
AM
3086 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3087 (SEC_ALLOC
3088 | SEC_LOAD
3089 | SEC_HAS_CONTENTS
3090 | SEC_IN_MEMORY
3091 | SEC_LINKER_CREATED
3092 | SEC_READONLY));
f4416af6 3093 if (sreloc == NULL
f4416af6 3094 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 3095 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
3096 return NULL;
3097 }
3098 return sreloc;
3099}
3100
e641e783
RS
3101/* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3102
3103static int
3104mips_elf_reloc_tls_type (unsigned int r_type)
3105{
3106 if (tls_gd_reloc_p (r_type))
3107 return GOT_TLS_GD;
3108
3109 if (tls_ldm_reloc_p (r_type))
3110 return GOT_TLS_LDM;
3111
3112 if (tls_gottprel_reloc_p (r_type))
3113 return GOT_TLS_IE;
3114
9ab066b4 3115 return GOT_TLS_NONE;
e641e783
RS
3116}
3117
3118/* Return the number of GOT slots needed for GOT TLS type TYPE. */
3119
3120static int
3121mips_tls_got_entries (unsigned int type)
3122{
3123 switch (type)
3124 {
3125 case GOT_TLS_GD:
3126 case GOT_TLS_LDM:
3127 return 2;
3128
3129 case GOT_TLS_IE:
3130 return 1;
3131
9ab066b4 3132 case GOT_TLS_NONE:
e641e783
RS
3133 return 0;
3134 }
3135 abort ();
3136}
3137
0f20cc35
DJ
3138/* Count the number of relocations needed for a TLS GOT entry, with
3139 access types from TLS_TYPE, and symbol H (or a local symbol if H
3140 is NULL). */
3141
3142static int
3143mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3144 struct elf_link_hash_entry *h)
3145{
3146 int indx = 0;
0f20cc35
DJ
3147 bfd_boolean need_relocs = FALSE;
3148 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3149
3150 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3151 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
3152 indx = h->dynindx;
3153
3154 if ((info->shared || indx != 0)
3155 && (h == NULL
3156 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3157 || h->root.type != bfd_link_hash_undefweak))
3158 need_relocs = TRUE;
3159
3160 if (!need_relocs)
e641e783 3161 return 0;
0f20cc35 3162
9ab066b4 3163 switch (tls_type)
0f20cc35 3164 {
e641e783
RS
3165 case GOT_TLS_GD:
3166 return indx != 0 ? 2 : 1;
0f20cc35 3167
e641e783
RS
3168 case GOT_TLS_IE:
3169 return 1;
0f20cc35 3170
e641e783
RS
3171 case GOT_TLS_LDM:
3172 return info->shared ? 1 : 0;
0f20cc35 3173
e641e783
RS
3174 default:
3175 return 0;
3176 }
0f20cc35
DJ
3177}
3178
ab361d49
RS
3179/* Add the number of GOT entries and TLS relocations required by ENTRY
3180 to G. */
0f20cc35 3181
ab361d49
RS
3182static void
3183mips_elf_count_got_entry (struct bfd_link_info *info,
3184 struct mips_got_info *g,
3185 struct mips_got_entry *entry)
0f20cc35 3186{
9ab066b4 3187 if (entry->tls_type)
ab361d49 3188 {
9ab066b4
RS
3189 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3190 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
ab361d49
RS
3191 entry->symndx < 0
3192 ? &entry->d.h->root : NULL);
3193 }
3194 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3195 g->local_gotno += 1;
3196 else
3197 g->global_gotno += 1;
0f20cc35
DJ
3198}
3199
0f20cc35
DJ
3200/* Output a simple dynamic relocation into SRELOC. */
3201
3202static void
3203mips_elf_output_dynamic_relocation (bfd *output_bfd,
3204 asection *sreloc,
861fb55a 3205 unsigned long reloc_index,
0f20cc35
DJ
3206 unsigned long indx,
3207 int r_type,
3208 bfd_vma offset)
3209{
3210 Elf_Internal_Rela rel[3];
3211
3212 memset (rel, 0, sizeof (rel));
3213
3214 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3215 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3216
3217 if (ABI_64_P (output_bfd))
3218 {
3219 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3220 (output_bfd, &rel[0],
3221 (sreloc->contents
861fb55a 3222 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
3223 }
3224 else
3225 bfd_elf32_swap_reloc_out
3226 (output_bfd, &rel[0],
3227 (sreloc->contents
861fb55a 3228 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
3229}
3230
3231/* Initialize a set of TLS GOT entries for one symbol. */
3232
3233static void
9ab066b4
RS
3234mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3235 struct mips_got_entry *entry,
0f20cc35
DJ
3236 struct mips_elf_link_hash_entry *h,
3237 bfd_vma value)
3238{
23cc69b6 3239 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
3240 int indx;
3241 asection *sreloc, *sgot;
9ab066b4 3242 bfd_vma got_offset, got_offset2;
0f20cc35
DJ
3243 bfd_boolean need_relocs = FALSE;
3244
23cc69b6 3245 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3246 if (htab == NULL)
3247 return;
3248
23cc69b6 3249 sgot = htab->sgot;
0f20cc35
DJ
3250
3251 indx = 0;
3252 if (h != NULL)
3253 {
3254 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3255
3256 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3257 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3258 indx = h->root.dynindx;
3259 }
3260
9ab066b4 3261 if (entry->tls_initialized)
0f20cc35
DJ
3262 return;
3263
3264 if ((info->shared || indx != 0)
3265 && (h == NULL
3266 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3267 || h->root.type != bfd_link_hash_undefweak))
3268 need_relocs = TRUE;
3269
3270 /* MINUS_ONE means the symbol is not defined in this object. It may not
3271 be defined at all; assume that the value doesn't matter in that
3272 case. Otherwise complain if we would use the value. */
3273 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3274 || h->root.root.type == bfd_link_hash_undefweak);
3275
3276 /* Emit necessary relocations. */
0a44bf69 3277 sreloc = mips_elf_rel_dyn_section (info, FALSE);
9ab066b4 3278 got_offset = entry->gotidx;
0f20cc35 3279
9ab066b4 3280 switch (entry->tls_type)
0f20cc35 3281 {
e641e783
RS
3282 case GOT_TLS_GD:
3283 /* General Dynamic. */
3284 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
0f20cc35
DJ
3285
3286 if (need_relocs)
3287 {
3288 mips_elf_output_dynamic_relocation
861fb55a 3289 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3290 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
e641e783 3291 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3292
3293 if (indx)
3294 mips_elf_output_dynamic_relocation
861fb55a 3295 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3296 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
e641e783 3297 sgot->output_offset + sgot->output_section->vma + got_offset2);
0f20cc35
DJ
3298 else
3299 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3300 sgot->contents + got_offset2);
0f20cc35
DJ
3301 }
3302 else
3303 {
3304 MIPS_ELF_PUT_WORD (abfd, 1,
e641e783 3305 sgot->contents + got_offset);
0f20cc35 3306 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3307 sgot->contents + got_offset2);
0f20cc35 3308 }
e641e783 3309 break;
0f20cc35 3310
e641e783
RS
3311 case GOT_TLS_IE:
3312 /* Initial Exec model. */
0f20cc35
DJ
3313 if (need_relocs)
3314 {
3315 if (indx == 0)
3316 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
e641e783 3317 sgot->contents + got_offset);
0f20cc35
DJ
3318 else
3319 MIPS_ELF_PUT_WORD (abfd, 0,
e641e783 3320 sgot->contents + got_offset);
0f20cc35
DJ
3321
3322 mips_elf_output_dynamic_relocation
861fb55a 3323 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3324 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
e641e783 3325 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3326 }
3327 else
3328 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
e641e783
RS
3329 sgot->contents + got_offset);
3330 break;
0f20cc35 3331
e641e783 3332 case GOT_TLS_LDM:
0f20cc35
DJ
3333 /* The initial offset is zero, and the LD offsets will include the
3334 bias by DTP_OFFSET. */
3335 MIPS_ELF_PUT_WORD (abfd, 0,
3336 sgot->contents + got_offset
3337 + MIPS_ELF_GOT_SIZE (abfd));
3338
3339 if (!info->shared)
3340 MIPS_ELF_PUT_WORD (abfd, 1,
3341 sgot->contents + got_offset);
3342 else
3343 mips_elf_output_dynamic_relocation
861fb55a 3344 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3345 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3346 sgot->output_offset + sgot->output_section->vma + got_offset);
e641e783
RS
3347 break;
3348
3349 default:
3350 abort ();
0f20cc35
DJ
3351 }
3352
9ab066b4 3353 entry->tls_initialized = TRUE;
e641e783 3354}
0f20cc35 3355
0a44bf69
RS
3356/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3357 for global symbol H. .got.plt comes before the GOT, so the offset
3358 will be negative. */
3359
3360static bfd_vma
3361mips_elf_gotplt_index (struct bfd_link_info *info,
3362 struct elf_link_hash_entry *h)
3363{
1bbce132 3364 bfd_vma got_address, got_value;
0a44bf69
RS
3365 struct mips_elf_link_hash_table *htab;
3366
3367 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3368 BFD_ASSERT (htab != NULL);
3369
1bbce132
MR
3370 BFD_ASSERT (h->plt.plist != NULL);
3371 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
0a44bf69
RS
3372
3373 /* Calculate the address of the associated .got.plt entry. */
3374 got_address = (htab->sgotplt->output_section->vma
3375 + htab->sgotplt->output_offset
1bbce132
MR
3376 + (h->plt.plist->gotplt_index
3377 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
0a44bf69
RS
3378
3379 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3380 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3381 + htab->root.hgot->root.u.def.section->output_offset
3382 + htab->root.hgot->root.u.def.value);
3383
3384 return got_address - got_value;
3385}
3386
5c18022e 3387/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3388 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3389 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3390 offset can be found. */
b49e97c9
TS
3391
3392static bfd_vma
9719ad41 3393mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3394 bfd_vma value, unsigned long r_symndx,
0f20cc35 3395 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3396{
a8028dd0 3397 struct mips_elf_link_hash_table *htab;
b15e6682 3398 struct mips_got_entry *entry;
b49e97c9 3399
a8028dd0 3400 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3401 BFD_ASSERT (htab != NULL);
3402
a8028dd0
RS
3403 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3404 r_symndx, h, r_type);
0f20cc35 3405 if (!entry)
b15e6682 3406 return MINUS_ONE;
0f20cc35 3407
e641e783 3408 if (entry->tls_type)
9ab066b4
RS
3409 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3410 return entry->gotidx;
b49e97c9
TS
3411}
3412
13fbec83 3413/* Return the GOT index of global symbol H in the primary GOT. */
b49e97c9
TS
3414
3415static bfd_vma
13fbec83
RS
3416mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3417 struct elf_link_hash_entry *h)
3418{
3419 struct mips_elf_link_hash_table *htab;
3420 long global_got_dynindx;
3421 struct mips_got_info *g;
3422 bfd_vma got_index;
3423
3424 htab = mips_elf_hash_table (info);
3425 BFD_ASSERT (htab != NULL);
3426
3427 global_got_dynindx = 0;
3428 if (htab->global_gotsym != NULL)
3429 global_got_dynindx = htab->global_gotsym->dynindx;
3430
3431 /* Once we determine the global GOT entry with the lowest dynamic
3432 symbol table index, we must put all dynamic symbols with greater
3433 indices into the primary GOT. That makes it easy to calculate the
3434 GOT offset. */
3435 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3436 g = mips_elf_bfd_got (obfd, FALSE);
3437 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3438 * MIPS_ELF_GOT_SIZE (obfd));
3439 BFD_ASSERT (got_index < htab->sgot->size);
3440
3441 return got_index;
3442}
3443
3444/* Return the GOT index for the global symbol indicated by H, which is
3445 referenced by a relocation of type R_TYPE in IBFD. */
3446
3447static bfd_vma
3448mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3449 struct elf_link_hash_entry *h, int r_type)
b49e97c9 3450{
a8028dd0 3451 struct mips_elf_link_hash_table *htab;
6c42ddb9
RS
3452 struct mips_got_info *g;
3453 struct mips_got_entry lookup, *entry;
3454 bfd_vma gotidx;
b49e97c9 3455
a8028dd0 3456 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3457 BFD_ASSERT (htab != NULL);
3458
6c42ddb9
RS
3459 g = mips_elf_bfd_got (ibfd, FALSE);
3460 BFD_ASSERT (g);
f4416af6 3461
6c42ddb9
RS
3462 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3463 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3464 return mips_elf_primary_global_got_index (obfd, info, h);
f4416af6 3465
6c42ddb9
RS
3466 lookup.abfd = ibfd;
3467 lookup.symndx = -1;
3468 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3469 entry = htab_find (g->got_entries, &lookup);
3470 BFD_ASSERT (entry);
0f20cc35 3471
6c42ddb9
RS
3472 gotidx = entry->gotidx;
3473 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
f4416af6 3474
6c42ddb9 3475 if (lookup.tls_type)
0f20cc35 3476 {
0f20cc35
DJ
3477 bfd_vma value = MINUS_ONE;
3478
3479 if ((h->root.type == bfd_link_hash_defined
3480 || h->root.type == bfd_link_hash_defweak)
3481 && h->root.u.def.section->output_section)
3482 value = (h->root.u.def.value
3483 + h->root.u.def.section->output_offset
3484 + h->root.u.def.section->output_section->vma);
3485
9ab066b4 3486 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
0f20cc35 3487 }
6c42ddb9 3488 return gotidx;
b49e97c9
TS
3489}
3490
5c18022e
RS
3491/* Find a GOT page entry that points to within 32KB of VALUE. These
3492 entries are supposed to be placed at small offsets in the GOT, i.e.,
3493 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3494 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3495 offset of the GOT entry from VALUE. */
b49e97c9
TS
3496
3497static bfd_vma
9719ad41 3498mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3499 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3500{
91d6fa6a 3501 bfd_vma page, got_index;
b15e6682 3502 struct mips_got_entry *entry;
b49e97c9 3503
0a44bf69 3504 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3505 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3506 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3507
b15e6682
AO
3508 if (!entry)
3509 return MINUS_ONE;
143d77c5 3510
91d6fa6a 3511 got_index = entry->gotidx;
b49e97c9
TS
3512
3513 if (offsetp)
f4416af6 3514 *offsetp = value - entry->d.address;
b49e97c9 3515
91d6fa6a 3516 return got_index;
b49e97c9
TS
3517}
3518
738e5348 3519/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3520 EXTERNAL is true if the relocation was originally against a global
3521 symbol that binds locally. */
b49e97c9
TS
3522
3523static bfd_vma
9719ad41 3524mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3525 bfd_vma value, bfd_boolean external)
b49e97c9 3526{
b15e6682 3527 struct mips_got_entry *entry;
b49e97c9 3528
0a44bf69
RS
3529 /* GOT16 relocations against local symbols are followed by a LO16
3530 relocation; those against global symbols are not. Thus if the
3531 symbol was originally local, the GOT16 relocation should load the
3532 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3533 if (! external)
0a44bf69 3534 value = mips_elf_high (value) << 16;
b49e97c9 3535
738e5348
RS
3536 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3537 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3538 same in all cases. */
a8028dd0
RS
3539 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3540 NULL, R_MIPS_GOT16);
b15e6682
AO
3541 if (entry)
3542 return entry->gotidx;
3543 else
3544 return MINUS_ONE;
b49e97c9
TS
3545}
3546
3547/* Returns the offset for the entry at the INDEXth position
3548 in the GOT. */
3549
3550static bfd_vma
a8028dd0 3551mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3552 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3553{
a8028dd0 3554 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3555 asection *sgot;
3556 bfd_vma gp;
3557
a8028dd0 3558 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3559 BFD_ASSERT (htab != NULL);
3560
a8028dd0 3561 sgot = htab->sgot;
f4416af6 3562 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3563 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3564
91d6fa6a 3565 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3566}
3567
0a44bf69
RS
3568/* Create and return a local GOT entry for VALUE, which was calculated
3569 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3570 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3571 instead. */
b49e97c9 3572
b15e6682 3573static struct mips_got_entry *
0a44bf69 3574mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3575 bfd *ibfd, bfd_vma value,
5c18022e 3576 unsigned long r_symndx,
0f20cc35
DJ
3577 struct mips_elf_link_hash_entry *h,
3578 int r_type)
b49e97c9 3579{
ebc53538
RS
3580 struct mips_got_entry lookup, *entry;
3581 void **loc;
f4416af6 3582 struct mips_got_info *g;
0a44bf69 3583 struct mips_elf_link_hash_table *htab;
6c42ddb9 3584 bfd_vma gotidx;
0a44bf69
RS
3585
3586 htab = mips_elf_hash_table (info);
4dfe6ac6 3587 BFD_ASSERT (htab != NULL);
b15e6682 3588
d7206569 3589 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
3590 if (g == NULL)
3591 {
d7206569 3592 g = mips_elf_bfd_got (abfd, FALSE);
f4416af6
AO
3593 BFD_ASSERT (g != NULL);
3594 }
b15e6682 3595
020d7251
RS
3596 /* This function shouldn't be called for symbols that live in the global
3597 area of the GOT. */
3598 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35 3599
ebc53538
RS
3600 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3601 if (lookup.tls_type)
3602 {
3603 lookup.abfd = ibfd;
df58fc94 3604 if (tls_ldm_reloc_p (r_type))
0f20cc35 3605 {
ebc53538
RS
3606 lookup.symndx = 0;
3607 lookup.d.addend = 0;
0f20cc35
DJ
3608 }
3609 else if (h == NULL)
3610 {
ebc53538
RS
3611 lookup.symndx = r_symndx;
3612 lookup.d.addend = 0;
0f20cc35
DJ
3613 }
3614 else
ebc53538
RS
3615 {
3616 lookup.symndx = -1;
3617 lookup.d.h = h;
3618 }
0f20cc35 3619
ebc53538
RS
3620 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3621 BFD_ASSERT (entry);
0f20cc35 3622
6c42ddb9
RS
3623 gotidx = entry->gotidx;
3624 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3625
ebc53538 3626 return entry;
0f20cc35
DJ
3627 }
3628
ebc53538
RS
3629 lookup.abfd = NULL;
3630 lookup.symndx = -1;
3631 lookup.d.address = value;
3632 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3633 if (!loc)
b15e6682 3634 return NULL;
143d77c5 3635
ebc53538
RS
3636 entry = (struct mips_got_entry *) *loc;
3637 if (entry)
3638 return entry;
b15e6682 3639
cb22ccf4 3640 if (g->assigned_low_gotno > g->assigned_high_gotno)
b49e97c9
TS
3641 {
3642 /* We didn't allocate enough space in the GOT. */
3643 (*_bfd_error_handler)
3644 (_("not enough GOT space for local GOT entries"));
3645 bfd_set_error (bfd_error_bad_value);
b15e6682 3646 return NULL;
b49e97c9
TS
3647 }
3648
ebc53538
RS
3649 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3650 if (!entry)
3651 return NULL;
3652
cb22ccf4
KCY
3653 if (got16_reloc_p (r_type)
3654 || call16_reloc_p (r_type)
3655 || got_page_reloc_p (r_type)
3656 || got_disp_reloc_p (r_type))
3657 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3658 else
3659 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3660
ebc53538
RS
3661 *entry = lookup;
3662 *loc = entry;
3663
3664 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
b15e6682 3665
5c18022e 3666 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3667 if (htab->is_vxworks)
3668 {
3669 Elf_Internal_Rela outrel;
5c18022e 3670 asection *s;
91d6fa6a 3671 bfd_byte *rloc;
0a44bf69 3672 bfd_vma got_address;
0a44bf69
RS
3673
3674 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3675 got_address = (htab->sgot->output_section->vma
3676 + htab->sgot->output_offset
ebc53538 3677 + entry->gotidx);
0a44bf69 3678
91d6fa6a 3679 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3680 outrel.r_offset = got_address;
5c18022e
RS
3681 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3682 outrel.r_addend = value;
91d6fa6a 3683 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3684 }
3685
ebc53538 3686 return entry;
b49e97c9
TS
3687}
3688
d4596a51
RS
3689/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3690 The number might be exact or a worst-case estimate, depending on how
3691 much information is available to elf_backend_omit_section_dynsym at
3692 the current linking stage. */
3693
3694static bfd_size_type
3695count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3696{
3697 bfd_size_type count;
3698
3699 count = 0;
3700 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3701 {
3702 asection *p;
3703 const struct elf_backend_data *bed;
3704
3705 bed = get_elf_backend_data (output_bfd);
3706 for (p = output_bfd->sections; p ; p = p->next)
3707 if ((p->flags & SEC_EXCLUDE) == 0
3708 && (p->flags & SEC_ALLOC) != 0
3709 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3710 ++count;
3711 }
3712 return count;
3713}
3714
b49e97c9 3715/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3716 appear towards the end. */
b49e97c9 3717
b34976b6 3718static bfd_boolean
d4596a51 3719mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3720{
a8028dd0 3721 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3722 struct mips_elf_hash_sort_data hsd;
3723 struct mips_got_info *g;
b49e97c9 3724
d4596a51
RS
3725 if (elf_hash_table (info)->dynsymcount == 0)
3726 return TRUE;
3727
a8028dd0 3728 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3729 BFD_ASSERT (htab != NULL);
3730
a8028dd0 3731 g = htab->got_info;
d4596a51
RS
3732 if (g == NULL)
3733 return TRUE;
f4416af6 3734
b49e97c9 3735 hsd.low = NULL;
23cc69b6
RS
3736 hsd.max_unref_got_dynindx
3737 = hsd.min_got_dynindx
3738 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3739 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3740 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3741 elf_hash_table (info)),
3742 mips_elf_sort_hash_table_f,
3743 &hsd);
3744
3745 /* There should have been enough room in the symbol table to
44c410de 3746 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3747 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3748 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3749 == elf_hash_table (info)->dynsymcount);
3750 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3751 == g->global_gotno);
b49e97c9
TS
3752
3753 /* Now we know which dynamic symbol has the lowest dynamic symbol
3754 table index in the GOT. */
d222d210 3755 htab->global_gotsym = hsd.low;
b49e97c9 3756
b34976b6 3757 return TRUE;
b49e97c9
TS
3758}
3759
3760/* If H needs a GOT entry, assign it the highest available dynamic
3761 index. Otherwise, assign it the lowest available dynamic
3762 index. */
3763
b34976b6 3764static bfd_boolean
9719ad41 3765mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3766{
9719ad41 3767 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9 3768
b49e97c9
TS
3769 /* Symbols without dynamic symbol table entries aren't interesting
3770 at all. */
3771 if (h->root.dynindx == -1)
b34976b6 3772 return TRUE;
b49e97c9 3773
634835ae 3774 switch (h->global_got_area)
f4416af6 3775 {
634835ae
RS
3776 case GGA_NONE:
3777 h->root.dynindx = hsd->max_non_got_dynindx++;
3778 break;
0f20cc35 3779
634835ae 3780 case GGA_NORMAL:
b49e97c9
TS
3781 h->root.dynindx = --hsd->min_got_dynindx;
3782 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3783 break;
3784
3785 case GGA_RELOC_ONLY:
634835ae
RS
3786 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3787 hsd->low = (struct elf_link_hash_entry *) h;
3788 h->root.dynindx = hsd->max_unref_got_dynindx++;
3789 break;
b49e97c9
TS
3790 }
3791
b34976b6 3792 return TRUE;
b49e97c9
TS
3793}
3794
ee227692
RS
3795/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3796 (which is owned by the caller and shouldn't be added to the
3797 hash table directly). */
3798
3799static bfd_boolean
3800mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3801 struct mips_got_entry *lookup)
3802{
3803 struct mips_elf_link_hash_table *htab;
3804 struct mips_got_entry *entry;
3805 struct mips_got_info *g;
3806 void **loc, **bfd_loc;
3807
3808 /* Make sure there's a slot for this entry in the master GOT. */
3809 htab = mips_elf_hash_table (info);
3810 g = htab->got_info;
3811 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3812 if (!loc)
3813 return FALSE;
3814
3815 /* Populate the entry if it isn't already. */
3816 entry = (struct mips_got_entry *) *loc;
3817 if (!entry)
3818 {
3819 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3820 if (!entry)
3821 return FALSE;
3822
9ab066b4 3823 lookup->tls_initialized = FALSE;
ee227692
RS
3824 lookup->gotidx = -1;
3825 *entry = *lookup;
3826 *loc = entry;
3827 }
3828
3829 /* Reuse the same GOT entry for the BFD's GOT. */
3830 g = mips_elf_bfd_got (abfd, TRUE);
3831 if (!g)
3832 return FALSE;
3833
3834 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3835 if (!bfd_loc)
3836 return FALSE;
3837
3838 if (!*bfd_loc)
3839 *bfd_loc = entry;
3840 return TRUE;
3841}
3842
e641e783
RS
3843/* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3844 entry for it. FOR_CALL is true if the caller is only interested in
6ccf4795 3845 using the GOT entry for calls. */
b49e97c9 3846
b34976b6 3847static bfd_boolean
9719ad41
RS
3848mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3849 bfd *abfd, struct bfd_link_info *info,
e641e783 3850 bfd_boolean for_call, int r_type)
b49e97c9 3851{
a8028dd0 3852 struct mips_elf_link_hash_table *htab;
634835ae 3853 struct mips_elf_link_hash_entry *hmips;
ee227692
RS
3854 struct mips_got_entry entry;
3855 unsigned char tls_type;
a8028dd0
RS
3856
3857 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3858 BFD_ASSERT (htab != NULL);
3859
634835ae 3860 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795
RS
3861 if (!for_call)
3862 hmips->got_only_for_calls = FALSE;
f4416af6 3863
b49e97c9
TS
3864 /* A global symbol in the GOT must also be in the dynamic symbol
3865 table. */
7c5fcef7
L
3866 if (h->dynindx == -1)
3867 {
3868 switch (ELF_ST_VISIBILITY (h->other))
3869 {
3870 case STV_INTERNAL:
3871 case STV_HIDDEN:
33bb52fb 3872 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3873 break;
3874 }
c152c796 3875 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3876 return FALSE;
7c5fcef7 3877 }
b49e97c9 3878
ee227692 3879 tls_type = mips_elf_reloc_tls_type (r_type);
9ab066b4 3880 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
ee227692 3881 hmips->global_got_area = GGA_NORMAL;
86324f90 3882
f4416af6
AO
3883 entry.abfd = abfd;
3884 entry.symndx = -1;
3885 entry.d.h = (struct mips_elf_link_hash_entry *) h;
ee227692
RS
3886 entry.tls_type = tls_type;
3887 return mips_elf_record_got_entry (info, abfd, &entry);
b49e97c9 3888}
f4416af6 3889
e641e783
RS
3890/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3891 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
f4416af6
AO
3892
3893static bfd_boolean
9719ad41 3894mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
e641e783 3895 struct bfd_link_info *info, int r_type)
f4416af6 3896{
a8028dd0
RS
3897 struct mips_elf_link_hash_table *htab;
3898 struct mips_got_info *g;
ee227692 3899 struct mips_got_entry entry;
f4416af6 3900
a8028dd0 3901 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3902 BFD_ASSERT (htab != NULL);
3903
a8028dd0
RS
3904 g = htab->got_info;
3905 BFD_ASSERT (g != NULL);
3906
f4416af6
AO
3907 entry.abfd = abfd;
3908 entry.symndx = symndx;
3909 entry.d.addend = addend;
e641e783 3910 entry.tls_type = mips_elf_reloc_tls_type (r_type);
ee227692 3911 return mips_elf_record_got_entry (info, abfd, &entry);
f4416af6 3912}
c224138d 3913
13db6b44
RS
3914/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
3915 H is the symbol's hash table entry, or null if SYMNDX is local
3916 to ABFD. */
c224138d
RS
3917
3918static bfd_boolean
13db6b44
RS
3919mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
3920 long symndx, struct elf_link_hash_entry *h,
3921 bfd_signed_vma addend)
c224138d 3922{
a8028dd0 3923 struct mips_elf_link_hash_table *htab;
ee227692 3924 struct mips_got_info *g1, *g2;
13db6b44 3925 struct mips_got_page_ref lookup, *entry;
ee227692 3926 void **loc, **bfd_loc;
c224138d 3927
a8028dd0 3928 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3929 BFD_ASSERT (htab != NULL);
3930
ee227692
RS
3931 g1 = htab->got_info;
3932 BFD_ASSERT (g1 != NULL);
a8028dd0 3933
13db6b44
RS
3934 if (h)
3935 {
3936 lookup.symndx = -1;
3937 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
3938 }
3939 else
3940 {
3941 lookup.symndx = symndx;
3942 lookup.u.abfd = abfd;
3943 }
3944 lookup.addend = addend;
3945 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
c224138d
RS
3946 if (loc == NULL)
3947 return FALSE;
3948
13db6b44 3949 entry = (struct mips_got_page_ref *) *loc;
c224138d
RS
3950 if (!entry)
3951 {
3952 entry = bfd_alloc (abfd, sizeof (*entry));
3953 if (!entry)
3954 return FALSE;
3955
13db6b44 3956 *entry = lookup;
c224138d
RS
3957 *loc = entry;
3958 }
3959
ee227692
RS
3960 /* Add the same entry to the BFD's GOT. */
3961 g2 = mips_elf_bfd_got (abfd, TRUE);
3962 if (!g2)
3963 return FALSE;
3964
13db6b44 3965 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
ee227692
RS
3966 if (!bfd_loc)
3967 return FALSE;
3968
3969 if (!*bfd_loc)
3970 *bfd_loc = entry;
3971
c224138d
RS
3972 return TRUE;
3973}
33bb52fb
RS
3974
3975/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3976
3977static void
3978mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3979 unsigned int n)
3980{
3981 asection *s;
3982 struct mips_elf_link_hash_table *htab;
3983
3984 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3985 BFD_ASSERT (htab != NULL);
3986
33bb52fb
RS
3987 s = mips_elf_rel_dyn_section (info, FALSE);
3988 BFD_ASSERT (s != NULL);
3989
3990 if (htab->is_vxworks)
3991 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3992 else
3993 {
3994 if (s->size == 0)
3995 {
3996 /* Make room for a null element. */
3997 s->size += MIPS_ELF_REL_SIZE (abfd);
3998 ++s->reloc_count;
3999 }
4000 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4001 }
4002}
4003\f
476366af
RS
4004/* A htab_traverse callback for GOT entries, with DATA pointing to a
4005 mips_elf_traverse_got_arg structure. Count the number of GOT
4006 entries and TLS relocs. Set DATA->value to true if we need
4007 to resolve indirect or warning symbols and then recreate the GOT. */
33bb52fb
RS
4008
4009static int
4010mips_elf_check_recreate_got (void **entryp, void *data)
4011{
4012 struct mips_got_entry *entry;
476366af 4013 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4014
4015 entry = (struct mips_got_entry *) *entryp;
476366af 4016 arg = (struct mips_elf_traverse_got_arg *) data;
33bb52fb
RS
4017 if (entry->abfd != NULL && entry->symndx == -1)
4018 {
4019 struct mips_elf_link_hash_entry *h;
4020
4021 h = entry->d.h;
4022 if (h->root.root.type == bfd_link_hash_indirect
4023 || h->root.root.type == bfd_link_hash_warning)
4024 {
476366af 4025 arg->value = TRUE;
33bb52fb
RS
4026 return 0;
4027 }
4028 }
476366af 4029 mips_elf_count_got_entry (arg->info, arg->g, entry);
33bb52fb
RS
4030 return 1;
4031}
4032
476366af
RS
4033/* A htab_traverse callback for GOT entries, with DATA pointing to a
4034 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4035 converting entries for indirect and warning symbols into entries
4036 for the target symbol. Set DATA->g to null on error. */
33bb52fb
RS
4037
4038static int
4039mips_elf_recreate_got (void **entryp, void *data)
4040{
72e7511a 4041 struct mips_got_entry new_entry, *entry;
476366af 4042 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4043 void **slot;
4044
33bb52fb 4045 entry = (struct mips_got_entry *) *entryp;
476366af 4046 arg = (struct mips_elf_traverse_got_arg *) data;
72e7511a
RS
4047 if (entry->abfd != NULL
4048 && entry->symndx == -1
4049 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4050 || entry->d.h->root.root.type == bfd_link_hash_warning))
33bb52fb
RS
4051 {
4052 struct mips_elf_link_hash_entry *h;
4053
72e7511a
RS
4054 new_entry = *entry;
4055 entry = &new_entry;
33bb52fb 4056 h = entry->d.h;
72e7511a 4057 do
634835ae
RS
4058 {
4059 BFD_ASSERT (h->global_got_area == GGA_NONE);
4060 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4061 }
72e7511a
RS
4062 while (h->root.root.type == bfd_link_hash_indirect
4063 || h->root.root.type == bfd_link_hash_warning);
33bb52fb
RS
4064 entry->d.h = h;
4065 }
476366af 4066 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
33bb52fb
RS
4067 if (slot == NULL)
4068 {
476366af 4069 arg->g = NULL;
33bb52fb
RS
4070 return 0;
4071 }
4072 if (*slot == NULL)
72e7511a
RS
4073 {
4074 if (entry == &new_entry)
4075 {
4076 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4077 if (!entry)
4078 {
476366af 4079 arg->g = NULL;
72e7511a
RS
4080 return 0;
4081 }
4082 *entry = new_entry;
4083 }
4084 *slot = entry;
476366af 4085 mips_elf_count_got_entry (arg->info, arg->g, entry);
72e7511a 4086 }
33bb52fb
RS
4087 return 1;
4088}
4089
13db6b44
RS
4090/* Return the maximum number of GOT page entries required for RANGE. */
4091
4092static bfd_vma
4093mips_elf_pages_for_range (const struct mips_got_page_range *range)
4094{
4095 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4096}
4097
4098/* Record that G requires a page entry that can reach SEC + ADDEND. */
4099
4100static bfd_boolean
b75d42bc 4101mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
13db6b44
RS
4102 asection *sec, bfd_signed_vma addend)
4103{
b75d42bc 4104 struct mips_got_info *g = arg->g;
13db6b44
RS
4105 struct mips_got_page_entry lookup, *entry;
4106 struct mips_got_page_range **range_ptr, *range;
4107 bfd_vma old_pages, new_pages;
4108 void **loc;
4109
4110 /* Find the mips_got_page_entry hash table entry for this section. */
4111 lookup.sec = sec;
4112 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4113 if (loc == NULL)
4114 return FALSE;
4115
4116 /* Create a mips_got_page_entry if this is the first time we've
4117 seen the section. */
4118 entry = (struct mips_got_page_entry *) *loc;
4119 if (!entry)
4120 {
b75d42bc 4121 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
13db6b44
RS
4122 if (!entry)
4123 return FALSE;
4124
4125 entry->sec = sec;
4126 *loc = entry;
4127 }
4128
4129 /* Skip over ranges whose maximum extent cannot share a page entry
4130 with ADDEND. */
4131 range_ptr = &entry->ranges;
4132 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4133 range_ptr = &(*range_ptr)->next;
4134
4135 /* If we scanned to the end of the list, or found a range whose
4136 minimum extent cannot share a page entry with ADDEND, create
4137 a new singleton range. */
4138 range = *range_ptr;
4139 if (!range || addend < range->min_addend - 0xffff)
4140 {
b75d42bc 4141 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
13db6b44
RS
4142 if (!range)
4143 return FALSE;
4144
4145 range->next = *range_ptr;
4146 range->min_addend = addend;
4147 range->max_addend = addend;
4148
4149 *range_ptr = range;
4150 entry->num_pages++;
4151 g->page_gotno++;
4152 return TRUE;
4153 }
4154
4155 /* Remember how many pages the old range contributed. */
4156 old_pages = mips_elf_pages_for_range (range);
4157
4158 /* Update the ranges. */
4159 if (addend < range->min_addend)
4160 range->min_addend = addend;
4161 else if (addend > range->max_addend)
4162 {
4163 if (range->next && addend >= range->next->min_addend - 0xffff)
4164 {
4165 old_pages += mips_elf_pages_for_range (range->next);
4166 range->max_addend = range->next->max_addend;
4167 range->next = range->next->next;
4168 }
4169 else
4170 range->max_addend = addend;
4171 }
4172
4173 /* Record any change in the total estimate. */
4174 new_pages = mips_elf_pages_for_range (range);
4175 if (old_pages != new_pages)
4176 {
4177 entry->num_pages += new_pages - old_pages;
4178 g->page_gotno += new_pages - old_pages;
4179 }
4180
4181 return TRUE;
4182}
4183
4184/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4185 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4186 whether the page reference described by *REFP needs a GOT page entry,
4187 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4188
4189static bfd_boolean
4190mips_elf_resolve_got_page_ref (void **refp, void *data)
4191{
4192 struct mips_got_page_ref *ref;
4193 struct mips_elf_traverse_got_arg *arg;
4194 struct mips_elf_link_hash_table *htab;
4195 asection *sec;
4196 bfd_vma addend;
4197
4198 ref = (struct mips_got_page_ref *) *refp;
4199 arg = (struct mips_elf_traverse_got_arg *) data;
4200 htab = mips_elf_hash_table (arg->info);
4201
4202 if (ref->symndx < 0)
4203 {
4204 struct mips_elf_link_hash_entry *h;
4205
4206 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4207 h = ref->u.h;
4208 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4209 return 1;
4210
4211 /* Ignore undefined symbols; we'll issue an error later if
4212 appropriate. */
4213 if (!((h->root.root.type == bfd_link_hash_defined
4214 || h->root.root.type == bfd_link_hash_defweak)
4215 && h->root.root.u.def.section))
4216 return 1;
4217
4218 sec = h->root.root.u.def.section;
4219 addend = h->root.root.u.def.value + ref->addend;
4220 }
4221 else
4222 {
4223 Elf_Internal_Sym *isym;
4224
4225 /* Read in the symbol. */
4226 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4227 ref->symndx);
4228 if (isym == NULL)
4229 {
4230 arg->g = NULL;
4231 return 0;
4232 }
4233
4234 /* Get the associated input section. */
4235 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4236 if (sec == NULL)
4237 {
4238 arg->g = NULL;
4239 return 0;
4240 }
4241
4242 /* If this is a mergable section, work out the section and offset
4243 of the merged data. For section symbols, the addend specifies
4244 of the offset _of_ the first byte in the data, otherwise it
4245 specifies the offset _from_ the first byte. */
4246 if (sec->flags & SEC_MERGE)
4247 {
4248 void *secinfo;
4249
4250 secinfo = elf_section_data (sec)->sec_info;
4251 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4252 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4253 isym->st_value + ref->addend);
4254 else
4255 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4256 isym->st_value) + ref->addend;
4257 }
4258 else
4259 addend = isym->st_value + ref->addend;
4260 }
b75d42bc 4261 if (!mips_elf_record_got_page_entry (arg, sec, addend))
13db6b44
RS
4262 {
4263 arg->g = NULL;
4264 return 0;
4265 }
4266 return 1;
4267}
4268
33bb52fb 4269/* If any entries in G->got_entries are for indirect or warning symbols,
13db6b44
RS
4270 replace them with entries for the target symbol. Convert g->got_page_refs
4271 into got_page_entry structures and estimate the number of page entries
4272 that they require. */
33bb52fb
RS
4273
4274static bfd_boolean
476366af
RS
4275mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4276 struct mips_got_info *g)
33bb52fb 4277{
476366af
RS
4278 struct mips_elf_traverse_got_arg tga;
4279 struct mips_got_info oldg;
4280
4281 oldg = *g;
33bb52fb 4282
476366af
RS
4283 tga.info = info;
4284 tga.g = g;
4285 tga.value = FALSE;
4286 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4287 if (tga.value)
33bb52fb 4288 {
476366af
RS
4289 *g = oldg;
4290 g->got_entries = htab_create (htab_size (oldg.got_entries),
4291 mips_elf_got_entry_hash,
4292 mips_elf_got_entry_eq, NULL);
4293 if (!g->got_entries)
33bb52fb
RS
4294 return FALSE;
4295
476366af
RS
4296 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4297 if (!tga.g)
4298 return FALSE;
4299
4300 htab_delete (oldg.got_entries);
33bb52fb 4301 }
13db6b44
RS
4302
4303 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4304 mips_got_page_entry_eq, NULL);
4305 if (g->got_page_entries == NULL)
4306 return FALSE;
4307
4308 tga.info = info;
4309 tga.g = g;
4310 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4311
33bb52fb
RS
4312 return TRUE;
4313}
4314
c5d6fa44
RS
4315/* Return true if a GOT entry for H should live in the local rather than
4316 global GOT area. */
4317
4318static bfd_boolean
4319mips_use_local_got_p (struct bfd_link_info *info,
4320 struct mips_elf_link_hash_entry *h)
4321{
4322 /* Symbols that aren't in the dynamic symbol table must live in the
4323 local GOT. This includes symbols that are completely undefined
4324 and which therefore don't bind locally. We'll report undefined
4325 symbols later if appropriate. */
4326 if (h->root.dynindx == -1)
4327 return TRUE;
4328
4329 /* Symbols that bind locally can (and in the case of forced-local
4330 symbols, must) live in the local GOT. */
4331 if (h->got_only_for_calls
4332 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4333 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4334 return TRUE;
4335
4336 /* If this is an executable that must provide a definition of the symbol,
4337 either though PLTs or copy relocations, then that address should go in
4338 the local rather than global GOT. */
4339 if (info->executable && h->has_static_relocs)
4340 return TRUE;
4341
4342 return FALSE;
4343}
4344
6c42ddb9
RS
4345/* A mips_elf_link_hash_traverse callback for which DATA points to the
4346 link_info structure. Decide whether the hash entry needs an entry in
4347 the global part of the primary GOT, setting global_got_area accordingly.
4348 Count the number of global symbols that are in the primary GOT only
4349 because they have relocations against them (reloc_only_gotno). */
33bb52fb
RS
4350
4351static int
d4596a51 4352mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 4353{
020d7251 4354 struct bfd_link_info *info;
6ccf4795 4355 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
4356 struct mips_got_info *g;
4357
020d7251 4358 info = (struct bfd_link_info *) data;
6ccf4795
RS
4359 htab = mips_elf_hash_table (info);
4360 g = htab->got_info;
d4596a51 4361 if (h->global_got_area != GGA_NONE)
33bb52fb 4362 {
020d7251 4363 /* Make a final decision about whether the symbol belongs in the
c5d6fa44
RS
4364 local or global GOT. */
4365 if (mips_use_local_got_p (info, h))
6c42ddb9
RS
4366 /* The symbol belongs in the local GOT. We no longer need this
4367 entry if it was only used for relocations; those relocations
4368 will be against the null or section symbol instead of H. */
4369 h->global_got_area = GGA_NONE;
6ccf4795
RS
4370 else if (htab->is_vxworks
4371 && h->got_only_for_calls
1bbce132 4372 && h->root.plt.plist->mips_offset != MINUS_ONE)
6ccf4795
RS
4373 /* On VxWorks, calls can refer directly to the .got.plt entry;
4374 they don't need entries in the regular GOT. .got.plt entries
4375 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4376 h->global_got_area = GGA_NONE;
6c42ddb9 4377 else if (h->global_got_area == GGA_RELOC_ONLY)
23cc69b6 4378 {
6c42ddb9 4379 g->reloc_only_gotno++;
23cc69b6 4380 g->global_gotno++;
23cc69b6 4381 }
33bb52fb
RS
4382 }
4383 return 1;
4384}
f4416af6 4385\f
d7206569
RS
4386/* A htab_traverse callback for GOT entries. Add each one to the GOT
4387 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
f4416af6
AO
4388
4389static int
d7206569 4390mips_elf_add_got_entry (void **entryp, void *data)
f4416af6 4391{
d7206569
RS
4392 struct mips_got_entry *entry;
4393 struct mips_elf_traverse_got_arg *arg;
4394 void **slot;
f4416af6 4395
d7206569
RS
4396 entry = (struct mips_got_entry *) *entryp;
4397 arg = (struct mips_elf_traverse_got_arg *) data;
4398 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4399 if (!slot)
f4416af6 4400 {
d7206569
RS
4401 arg->g = NULL;
4402 return 0;
f4416af6 4403 }
d7206569 4404 if (!*slot)
c224138d 4405 {
d7206569
RS
4406 *slot = entry;
4407 mips_elf_count_got_entry (arg->info, arg->g, entry);
c224138d 4408 }
f4416af6
AO
4409 return 1;
4410}
4411
d7206569
RS
4412/* A htab_traverse callback for GOT page entries. Add each one to the GOT
4413 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
c224138d
RS
4414
4415static int
d7206569 4416mips_elf_add_got_page_entry (void **entryp, void *data)
c224138d 4417{
d7206569
RS
4418 struct mips_got_page_entry *entry;
4419 struct mips_elf_traverse_got_arg *arg;
4420 void **slot;
c224138d 4421
d7206569
RS
4422 entry = (struct mips_got_page_entry *) *entryp;
4423 arg = (struct mips_elf_traverse_got_arg *) data;
4424 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4425 if (!slot)
c224138d 4426 {
d7206569 4427 arg->g = NULL;
c224138d
RS
4428 return 0;
4429 }
d7206569
RS
4430 if (!*slot)
4431 {
4432 *slot = entry;
4433 arg->g->page_gotno += entry->num_pages;
4434 }
c224138d
RS
4435 return 1;
4436}
4437
d7206569
RS
4438/* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4439 this would lead to overflow, 1 if they were merged successfully,
4440 and 0 if a merge failed due to lack of memory. (These values are chosen
4441 so that nonnegative return values can be returned by a htab_traverse
4442 callback.) */
c224138d
RS
4443
4444static int
d7206569 4445mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
c224138d
RS
4446 struct mips_got_info *to,
4447 struct mips_elf_got_per_bfd_arg *arg)
4448{
d7206569 4449 struct mips_elf_traverse_got_arg tga;
c224138d
RS
4450 unsigned int estimate;
4451
4452 /* Work out how many page entries we would need for the combined GOT. */
4453 estimate = arg->max_pages;
4454 if (estimate >= from->page_gotno + to->page_gotno)
4455 estimate = from->page_gotno + to->page_gotno;
4456
e2ece73c 4457 /* And conservatively estimate how many local and TLS entries
c224138d 4458 would be needed. */
e2ece73c
RS
4459 estimate += from->local_gotno + to->local_gotno;
4460 estimate += from->tls_gotno + to->tls_gotno;
4461
17214937
RS
4462 /* If we're merging with the primary got, any TLS relocations will
4463 come after the full set of global entries. Otherwise estimate those
e2ece73c 4464 conservatively as well. */
17214937 4465 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
e2ece73c
RS
4466 estimate += arg->global_count;
4467 else
4468 estimate += from->global_gotno + to->global_gotno;
c224138d
RS
4469
4470 /* Bail out if the combined GOT might be too big. */
4471 if (estimate > arg->max_count)
4472 return -1;
4473
c224138d 4474 /* Transfer the bfd's got information from FROM to TO. */
d7206569
RS
4475 tga.info = arg->info;
4476 tga.g = to;
4477 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4478 if (!tga.g)
c224138d
RS
4479 return 0;
4480
d7206569
RS
4481 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4482 if (!tga.g)
c224138d
RS
4483 return 0;
4484
d7206569 4485 mips_elf_replace_bfd_got (abfd, to);
c224138d
RS
4486 return 1;
4487}
4488
d7206569 4489/* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
f4416af6
AO
4490 as possible of the primary got, since it doesn't require explicit
4491 dynamic relocations, but don't use bfds that would reference global
4492 symbols out of the addressable range. Failing the primary got,
4493 attempt to merge with the current got, or finish the current got
4494 and then make make the new got current. */
4495
d7206569
RS
4496static bfd_boolean
4497mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4498 struct mips_elf_got_per_bfd_arg *arg)
f4416af6 4499{
c224138d
RS
4500 unsigned int estimate;
4501 int result;
4502
476366af 4503 if (!mips_elf_resolve_final_got_entries (arg->info, g))
d7206569
RS
4504 return FALSE;
4505
c224138d
RS
4506 /* Work out the number of page, local and TLS entries. */
4507 estimate = arg->max_pages;
4508 if (estimate > g->page_gotno)
4509 estimate = g->page_gotno;
4510 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4511
4512 /* We place TLS GOT entries after both locals and globals. The globals
4513 for the primary GOT may overflow the normal GOT size limit, so be
4514 sure not to merge a GOT which requires TLS with the primary GOT in that
4515 case. This doesn't affect non-primary GOTs. */
c224138d 4516 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4517
c224138d 4518 if (estimate <= arg->max_count)
f4416af6 4519 {
c224138d
RS
4520 /* If we don't have a primary GOT, use it as
4521 a starting point for the primary GOT. */
4522 if (!arg->primary)
4523 {
d7206569
RS
4524 arg->primary = g;
4525 return TRUE;
c224138d 4526 }
f4416af6 4527
c224138d 4528 /* Try merging with the primary GOT. */
d7206569 4529 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
c224138d
RS
4530 if (result >= 0)
4531 return result;
f4416af6 4532 }
c224138d 4533
f4416af6 4534 /* If we can merge with the last-created got, do it. */
c224138d 4535 if (arg->current)
f4416af6 4536 {
d7206569 4537 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
c224138d
RS
4538 if (result >= 0)
4539 return result;
f4416af6 4540 }
c224138d 4541
f4416af6
AO
4542 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4543 fits; if it turns out that it doesn't, we'll get relocation
4544 overflows anyway. */
c224138d
RS
4545 g->next = arg->current;
4546 arg->current = g;
0f20cc35 4547
d7206569 4548 return TRUE;
0f20cc35
DJ
4549}
4550
72e7511a
RS
4551/* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4552 to GOTIDX, duplicating the entry if it has already been assigned
4553 an index in a different GOT. */
4554
4555static bfd_boolean
4556mips_elf_set_gotidx (void **entryp, long gotidx)
4557{
4558 struct mips_got_entry *entry;
4559
4560 entry = (struct mips_got_entry *) *entryp;
4561 if (entry->gotidx > 0)
4562 {
4563 struct mips_got_entry *new_entry;
4564
4565 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4566 if (!new_entry)
4567 return FALSE;
4568
4569 *new_entry = *entry;
4570 *entryp = new_entry;
4571 entry = new_entry;
4572 }
4573 entry->gotidx = gotidx;
4574 return TRUE;
4575}
4576
4577/* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4578 mips_elf_traverse_got_arg in which DATA->value is the size of one
4579 GOT entry. Set DATA->g to null on failure. */
0f20cc35
DJ
4580
4581static int
72e7511a 4582mips_elf_initialize_tls_index (void **entryp, void *data)
0f20cc35 4583{
72e7511a
RS
4584 struct mips_got_entry *entry;
4585 struct mips_elf_traverse_got_arg *arg;
0f20cc35
DJ
4586
4587 /* We're only interested in TLS symbols. */
72e7511a 4588 entry = (struct mips_got_entry *) *entryp;
9ab066b4 4589 if (entry->tls_type == GOT_TLS_NONE)
0f20cc35
DJ
4590 return 1;
4591
72e7511a 4592 arg = (struct mips_elf_traverse_got_arg *) data;
6c42ddb9 4593 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
ead49a57 4594 {
6c42ddb9
RS
4595 arg->g = NULL;
4596 return 0;
f4416af6
AO
4597 }
4598
ead49a57 4599 /* Account for the entries we've just allocated. */
9ab066b4 4600 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
f4416af6
AO
4601 return 1;
4602}
4603
ab361d49
RS
4604/* A htab_traverse callback for GOT entries, where DATA points to a
4605 mips_elf_traverse_got_arg. Set the global_got_area of each global
4606 symbol to DATA->value. */
f4416af6 4607
f4416af6 4608static int
ab361d49 4609mips_elf_set_global_got_area (void **entryp, void *data)
f4416af6 4610{
ab361d49
RS
4611 struct mips_got_entry *entry;
4612 struct mips_elf_traverse_got_arg *arg;
f4416af6 4613
ab361d49
RS
4614 entry = (struct mips_got_entry *) *entryp;
4615 arg = (struct mips_elf_traverse_got_arg *) data;
4616 if (entry->abfd != NULL
4617 && entry->symndx == -1
4618 && entry->d.h->global_got_area != GGA_NONE)
4619 entry->d.h->global_got_area = arg->value;
4620 return 1;
4621}
4622
4623/* A htab_traverse callback for secondary GOT entries, where DATA points
4624 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4625 and record the number of relocations they require. DATA->value is
72e7511a 4626 the size of one GOT entry. Set DATA->g to null on failure. */
ab361d49
RS
4627
4628static int
4629mips_elf_set_global_gotidx (void **entryp, void *data)
4630{
4631 struct mips_got_entry *entry;
4632 struct mips_elf_traverse_got_arg *arg;
0f20cc35 4633
ab361d49
RS
4634 entry = (struct mips_got_entry *) *entryp;
4635 arg = (struct mips_elf_traverse_got_arg *) data;
634835ae
RS
4636 if (entry->abfd != NULL
4637 && entry->symndx == -1
4638 && entry->d.h->global_got_area != GGA_NONE)
f4416af6 4639 {
cb22ccf4 4640 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
72e7511a
RS
4641 {
4642 arg->g = NULL;
4643 return 0;
4644 }
cb22ccf4 4645 arg->g->assigned_low_gotno += 1;
72e7511a 4646
ab361d49
RS
4647 if (arg->info->shared
4648 || (elf_hash_table (arg->info)->dynamic_sections_created
4649 && entry->d.h->root.def_dynamic
4650 && !entry->d.h->root.def_regular))
4651 arg->g->relocs += 1;
f4416af6
AO
4652 }
4653
4654 return 1;
4655}
4656
33bb52fb
RS
4657/* A htab_traverse callback for GOT entries for which DATA is the
4658 bfd_link_info. Forbid any global symbols from having traditional
4659 lazy-binding stubs. */
4660
0626d451 4661static int
33bb52fb 4662mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4663{
33bb52fb
RS
4664 struct bfd_link_info *info;
4665 struct mips_elf_link_hash_table *htab;
4666 struct mips_got_entry *entry;
0626d451 4667
33bb52fb
RS
4668 entry = (struct mips_got_entry *) *entryp;
4669 info = (struct bfd_link_info *) data;
4670 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4671 BFD_ASSERT (htab != NULL);
4672
0626d451
RS
4673 if (entry->abfd != NULL
4674 && entry->symndx == -1
33bb52fb 4675 && entry->d.h->needs_lazy_stub)
f4416af6 4676 {
33bb52fb
RS
4677 entry->d.h->needs_lazy_stub = FALSE;
4678 htab->lazy_stub_count--;
f4416af6 4679 }
143d77c5 4680
f4416af6
AO
4681 return 1;
4682}
4683
f4416af6
AO
4684/* Return the offset of an input bfd IBFD's GOT from the beginning of
4685 the primary GOT. */
4686static bfd_vma
9719ad41 4687mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6 4688{
d7206569 4689 if (!g->next)
f4416af6
AO
4690 return 0;
4691
d7206569 4692 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
4693 if (! g)
4694 return 0;
4695
4696 BFD_ASSERT (g->next);
4697
4698 g = g->next;
143d77c5 4699
0f20cc35
DJ
4700 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4701 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4702}
4703
4704/* Turn a single GOT that is too big for 16-bit addressing into
4705 a sequence of GOTs, each one 16-bit addressable. */
4706
4707static bfd_boolean
9719ad41 4708mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4709 asection *got, bfd_size_type pages)
f4416af6 4710{
a8028dd0 4711 struct mips_elf_link_hash_table *htab;
f4416af6 4712 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
ab361d49 4713 struct mips_elf_traverse_got_arg tga;
a8028dd0 4714 struct mips_got_info *g, *gg;
33bb52fb 4715 unsigned int assign, needed_relocs;
d7206569 4716 bfd *dynobj, *ibfd;
f4416af6 4717
33bb52fb 4718 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4719 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4720 BFD_ASSERT (htab != NULL);
4721
a8028dd0 4722 g = htab->got_info;
f4416af6 4723
f4416af6
AO
4724 got_per_bfd_arg.obfd = abfd;
4725 got_per_bfd_arg.info = info;
f4416af6
AO
4726 got_per_bfd_arg.current = NULL;
4727 got_per_bfd_arg.primary = NULL;
0a44bf69 4728 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4729 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4730 - htab->reserved_gotno);
c224138d 4731 got_per_bfd_arg.max_pages = pages;
0f20cc35 4732 /* The number of globals that will be included in the primary GOT.
ab361d49 4733 See the calls to mips_elf_set_global_got_area below for more
0f20cc35
DJ
4734 information. */
4735 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4736
4737 /* Try to merge the GOTs of input bfds together, as long as they
4738 don't seem to exceed the maximum GOT size, choosing one of them
4739 to be the primary GOT. */
d7206569
RS
4740 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4741 {
4742 gg = mips_elf_bfd_got (ibfd, FALSE);
4743 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4744 return FALSE;
4745 }
f4416af6 4746
0f20cc35 4747 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6 4748 if (got_per_bfd_arg.primary == NULL)
3dff0dd1 4749 g->next = mips_elf_create_got_info (abfd);
f4416af6
AO
4750 else
4751 g->next = got_per_bfd_arg.primary;
4752 g->next->next = got_per_bfd_arg.current;
4753
4754 /* GG is now the master GOT, and G is the primary GOT. */
4755 gg = g;
4756 g = g->next;
4757
4758 /* Map the output bfd to the primary got. That's what we're going
4759 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4760 didn't mark in check_relocs, and we want a quick way to find it.
4761 We can't just use gg->next because we're going to reverse the
4762 list. */
d7206569 4763 mips_elf_replace_bfd_got (abfd, g);
f4416af6 4764
634835ae
RS
4765 /* Every symbol that is referenced in a dynamic relocation must be
4766 present in the primary GOT, so arrange for them to appear after
4767 those that are actually referenced. */
23cc69b6 4768 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4769 g->global_gotno = gg->global_gotno;
f4416af6 4770
ab361d49
RS
4771 tga.info = info;
4772 tga.value = GGA_RELOC_ONLY;
4773 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4774 tga.value = GGA_NORMAL;
4775 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
f4416af6
AO
4776
4777 /* Now go through the GOTs assigning them offset ranges.
cb22ccf4 4778 [assigned_low_gotno, local_gotno[ will be set to the range of local
f4416af6
AO
4779 entries in each GOT. We can then compute the end of a GOT by
4780 adding local_gotno to global_gotno. We reverse the list and make
4781 it circular since then we'll be able to quickly compute the
4782 beginning of a GOT, by computing the end of its predecessor. To
4783 avoid special cases for the primary GOT, while still preserving
4784 assertions that are valid for both single- and multi-got links,
4785 we arrange for the main got struct to have the right number of
4786 global entries, but set its local_gotno such that the initial
4787 offset of the primary GOT is zero. Remember that the primary GOT
4788 will become the last item in the circular linked list, so it
4789 points back to the master GOT. */
4790 gg->local_gotno = -g->global_gotno;
4791 gg->global_gotno = g->global_gotno;
0f20cc35 4792 gg->tls_gotno = 0;
f4416af6
AO
4793 assign = 0;
4794 gg->next = gg;
4795
4796 do
4797 {
4798 struct mips_got_info *gn;
4799
861fb55a 4800 assign += htab->reserved_gotno;
cb22ccf4 4801 g->assigned_low_gotno = assign;
c224138d
RS
4802 g->local_gotno += assign;
4803 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
cb22ccf4 4804 g->assigned_high_gotno = g->local_gotno - 1;
0f20cc35
DJ
4805 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4806
ead49a57
RS
4807 /* Take g out of the direct list, and push it onto the reversed
4808 list that gg points to. g->next is guaranteed to be nonnull after
4809 this operation, as required by mips_elf_initialize_tls_index. */
4810 gn = g->next;
4811 g->next = gg->next;
4812 gg->next = g;
4813
0f20cc35
DJ
4814 /* Set up any TLS entries. We always place the TLS entries after
4815 all non-TLS entries. */
4816 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
72e7511a
RS
4817 tga.g = g;
4818 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4819 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4820 if (!tga.g)
4821 return FALSE;
1fd20d70 4822 BFD_ASSERT (g->tls_assigned_gotno == assign);
f4416af6 4823
ead49a57 4824 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4825 g = gn;
0626d451 4826
33bb52fb
RS
4827 /* Forbid global symbols in every non-primary GOT from having
4828 lazy-binding stubs. */
0626d451 4829 if (g)
33bb52fb 4830 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4831 }
4832 while (g);
4833
59b08994 4834 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
33bb52fb
RS
4835
4836 needed_relocs = 0;
33bb52fb
RS
4837 for (g = gg->next; g && g->next != gg; g = g->next)
4838 {
4839 unsigned int save_assign;
4840
ab361d49
RS
4841 /* Assign offsets to global GOT entries and count how many
4842 relocations they need. */
cb22ccf4
KCY
4843 save_assign = g->assigned_low_gotno;
4844 g->assigned_low_gotno = g->local_gotno;
ab361d49
RS
4845 tga.info = info;
4846 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4847 tga.g = g;
4848 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
72e7511a
RS
4849 if (!tga.g)
4850 return FALSE;
cb22ccf4
KCY
4851 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
4852 g->assigned_low_gotno = save_assign;
72e7511a 4853
33bb52fb
RS
4854 if (info->shared)
4855 {
cb22ccf4
KCY
4856 g->relocs += g->local_gotno - g->assigned_low_gotno;
4857 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
33bb52fb
RS
4858 + g->next->global_gotno
4859 + g->next->tls_gotno
861fb55a 4860 + htab->reserved_gotno);
33bb52fb 4861 }
ab361d49 4862 needed_relocs += g->relocs;
33bb52fb 4863 }
ab361d49 4864 needed_relocs += g->relocs;
33bb52fb
RS
4865
4866 if (needed_relocs)
4867 mips_elf_allocate_dynamic_relocations (dynobj, info,
4868 needed_relocs);
143d77c5 4869
f4416af6
AO
4870 return TRUE;
4871}
143d77c5 4872
b49e97c9
TS
4873\f
4874/* Returns the first relocation of type r_type found, beginning with
4875 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4876
4877static const Elf_Internal_Rela *
9719ad41
RS
4878mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4879 const Elf_Internal_Rela *relocation,
4880 const Elf_Internal_Rela *relend)
b49e97c9 4881{
c000e262
TS
4882 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4883
b49e97c9
TS
4884 while (relocation < relend)
4885 {
c000e262
TS
4886 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4887 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4888 return relocation;
4889
4890 ++relocation;
4891 }
4892
4893 /* We didn't find it. */
b49e97c9
TS
4894 return NULL;
4895}
4896
020d7251 4897/* Return whether an input relocation is against a local symbol. */
b49e97c9 4898
b34976b6 4899static bfd_boolean
9719ad41
RS
4900mips_elf_local_relocation_p (bfd *input_bfd,
4901 const Elf_Internal_Rela *relocation,
020d7251 4902 asection **local_sections)
b49e97c9
TS
4903{
4904 unsigned long r_symndx;
4905 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
4906 size_t extsymoff;
4907
4908 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4909 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4910 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4911
4912 if (r_symndx < extsymoff)
b34976b6 4913 return TRUE;
b49e97c9 4914 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4915 return TRUE;
b49e97c9 4916
b34976b6 4917 return FALSE;
b49e97c9
TS
4918}
4919\f
4920/* Sign-extend VALUE, which has the indicated number of BITS. */
4921
a7ebbfdf 4922bfd_vma
9719ad41 4923_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4924{
4925 if (value & ((bfd_vma) 1 << (bits - 1)))
4926 /* VALUE is negative. */
4927 value |= ((bfd_vma) - 1) << bits;
4928
4929 return value;
4930}
4931
4932/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4933 range expressible by a signed number with the indicated number of
b49e97c9
TS
4934 BITS. */
4935
b34976b6 4936static bfd_boolean
9719ad41 4937mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4938{
4939 bfd_signed_vma svalue = (bfd_signed_vma) value;
4940
4941 if (svalue > (1 << (bits - 1)) - 1)
4942 /* The value is too big. */
b34976b6 4943 return TRUE;
b49e97c9
TS
4944 else if (svalue < -(1 << (bits - 1)))
4945 /* The value is too small. */
b34976b6 4946 return TRUE;
b49e97c9
TS
4947
4948 /* All is well. */
b34976b6 4949 return FALSE;
b49e97c9
TS
4950}
4951
4952/* Calculate the %high function. */
4953
4954static bfd_vma
9719ad41 4955mips_elf_high (bfd_vma value)
b49e97c9
TS
4956{
4957 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4958}
4959
4960/* Calculate the %higher function. */
4961
4962static bfd_vma
9719ad41 4963mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4964{
4965#ifdef BFD64
4966 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4967#else
4968 abort ();
c5ae1840 4969 return MINUS_ONE;
b49e97c9
TS
4970#endif
4971}
4972
4973/* Calculate the %highest function. */
4974
4975static bfd_vma
9719ad41 4976mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4977{
4978#ifdef BFD64
b15e6682 4979 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4980#else
4981 abort ();
c5ae1840 4982 return MINUS_ONE;
b49e97c9
TS
4983#endif
4984}
4985\f
4986/* Create the .compact_rel section. */
4987
b34976b6 4988static bfd_boolean
9719ad41
RS
4989mips_elf_create_compact_rel_section
4990 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4991{
4992 flagword flags;
4993 register asection *s;
4994
3d4d4302 4995 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
b49e97c9
TS
4996 {
4997 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4998 | SEC_READONLY);
4999
3d4d4302 5000 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
b49e97c9 5001 if (s == NULL
b49e97c9
TS
5002 || ! bfd_set_section_alignment (abfd, s,
5003 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 5004 return FALSE;
b49e97c9 5005
eea6121a 5006 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
5007 }
5008
b34976b6 5009 return TRUE;
b49e97c9
TS
5010}
5011
5012/* Create the .got section to hold the global offset table. */
5013
b34976b6 5014static bfd_boolean
23cc69b6 5015mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
5016{
5017 flagword flags;
5018 register asection *s;
5019 struct elf_link_hash_entry *h;
14a793b2 5020 struct bfd_link_hash_entry *bh;
0a44bf69
RS
5021 struct mips_elf_link_hash_table *htab;
5022
5023 htab = mips_elf_hash_table (info);
4dfe6ac6 5024 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5025
5026 /* This function may be called more than once. */
23cc69b6
RS
5027 if (htab->sgot)
5028 return TRUE;
b49e97c9
TS
5029
5030 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5031 | SEC_LINKER_CREATED);
5032
72b4917c
TS
5033 /* We have to use an alignment of 2**4 here because this is hardcoded
5034 in the function stub generation and in the linker script. */
87e0a731 5035 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
b49e97c9 5036 if (s == NULL
72b4917c 5037 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 5038 return FALSE;
a8028dd0 5039 htab->sgot = s;
b49e97c9
TS
5040
5041 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5042 linker script because we don't want to define the symbol if we
5043 are not creating a global offset table. */
14a793b2 5044 bh = NULL;
b49e97c9
TS
5045 if (! (_bfd_generic_link_add_one_symbol
5046 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 5047 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5048 return FALSE;
14a793b2
AM
5049
5050 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5051 h->non_elf = 0;
5052 h->def_regular = 1;
b49e97c9 5053 h->type = STT_OBJECT;
2f9efdfc 5054 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d329bcd1 5055 elf_hash_table (info)->hgot = h;
b49e97c9
TS
5056
5057 if (info->shared
c152c796 5058 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5059 return FALSE;
b49e97c9 5060
3dff0dd1 5061 htab->got_info = mips_elf_create_got_info (abfd);
f0abc2a1 5062 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
5063 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5064
861fb55a 5065 /* We also need a .got.plt section when generating PLTs. */
87e0a731
AM
5066 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5067 SEC_ALLOC | SEC_LOAD
5068 | SEC_HAS_CONTENTS
5069 | SEC_IN_MEMORY
5070 | SEC_LINKER_CREATED);
861fb55a
DJ
5071 if (s == NULL)
5072 return FALSE;
5073 htab->sgotplt = s;
0a44bf69 5074
b34976b6 5075 return TRUE;
b49e97c9 5076}
b49e97c9 5077\f
0a44bf69
RS
5078/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5079 __GOTT_INDEX__ symbols. These symbols are only special for
5080 shared objects; they are not used in executables. */
5081
5082static bfd_boolean
5083is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5084{
5085 return (mips_elf_hash_table (info)->is_vxworks
5086 && info->shared
5087 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5088 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5089}
861fb55a
DJ
5090
5091/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5092 require an la25 stub. See also mips_elf_local_pic_function_p,
5093 which determines whether the destination function ever requires a
5094 stub. */
5095
5096static bfd_boolean
8f0c309a
CLT
5097mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5098 bfd_boolean target_is_16_bit_code_p)
861fb55a
DJ
5099{
5100 /* We specifically ignore branches and jumps from EF_PIC objects,
5101 where the onus is on the compiler or programmer to perform any
5102 necessary initialization of $25. Sometimes such initialization
5103 is unnecessary; for example, -mno-shared functions do not use
5104 the incoming value of $25, and may therefore be called directly. */
5105 if (PIC_OBJECT_P (input_bfd))
5106 return FALSE;
5107
5108 switch (r_type)
5109 {
5110 case R_MIPS_26:
5111 case R_MIPS_PC16:
df58fc94
RS
5112 case R_MICROMIPS_26_S1:
5113 case R_MICROMIPS_PC7_S1:
5114 case R_MICROMIPS_PC10_S1:
5115 case R_MICROMIPS_PC16_S1:
5116 case R_MICROMIPS_PC23_S2:
861fb55a
DJ
5117 return TRUE;
5118
8f0c309a
CLT
5119 case R_MIPS16_26:
5120 return !target_is_16_bit_code_p;
5121
861fb55a
DJ
5122 default:
5123 return FALSE;
5124 }
5125}
0a44bf69 5126\f
b49e97c9
TS
5127/* Calculate the value produced by the RELOCATION (which comes from
5128 the INPUT_BFD). The ADDEND is the addend to use for this
5129 RELOCATION; RELOCATION->R_ADDEND is ignored.
5130
5131 The result of the relocation calculation is stored in VALUEP.
38a7df63 5132 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
df58fc94 5133 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9
TS
5134
5135 This function returns bfd_reloc_continue if the caller need take no
5136 further action regarding this relocation, bfd_reloc_notsupported if
5137 something goes dramatically wrong, bfd_reloc_overflow if an
5138 overflow occurs, and bfd_reloc_ok to indicate success. */
5139
5140static bfd_reloc_status_type
9719ad41
RS
5141mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5142 asection *input_section,
5143 struct bfd_link_info *info,
5144 const Elf_Internal_Rela *relocation,
5145 bfd_vma addend, reloc_howto_type *howto,
5146 Elf_Internal_Sym *local_syms,
5147 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
5148 const char **namep,
5149 bfd_boolean *cross_mode_jump_p,
9719ad41 5150 bfd_boolean save_addend)
b49e97c9
TS
5151{
5152 /* The eventual value we will return. */
5153 bfd_vma value;
5154 /* The address of the symbol against which the relocation is
5155 occurring. */
5156 bfd_vma symbol = 0;
5157 /* The final GP value to be used for the relocatable, executable, or
5158 shared object file being produced. */
0a61c8c2 5159 bfd_vma gp;
b49e97c9
TS
5160 /* The place (section offset or address) of the storage unit being
5161 relocated. */
5162 bfd_vma p;
5163 /* The value of GP used to create the relocatable object. */
0a61c8c2 5164 bfd_vma gp0;
b49e97c9
TS
5165 /* The offset into the global offset table at which the address of
5166 the relocation entry symbol, adjusted by the addend, resides
5167 during execution. */
5168 bfd_vma g = MINUS_ONE;
5169 /* The section in which the symbol referenced by the relocation is
5170 located. */
5171 asection *sec = NULL;
5172 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 5173 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 5174 symbol. */
b34976b6
AM
5175 bfd_boolean local_p, was_local_p;
5176 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5177 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
5178 /* TRUE if the symbol referred to by this relocation is
5179 "__gnu_local_gp". */
5180 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
5181 Elf_Internal_Shdr *symtab_hdr;
5182 size_t extsymoff;
5183 unsigned long r_symndx;
5184 int r_type;
b34976b6 5185 /* TRUE if overflow occurred during the calculation of the
b49e97c9 5186 relocation value. */
b34976b6
AM
5187 bfd_boolean overflowed_p;
5188 /* TRUE if this relocation refers to a MIPS16 function. */
5189 bfd_boolean target_is_16_bit_code_p = FALSE;
df58fc94 5190 bfd_boolean target_is_micromips_code_p = FALSE;
0a44bf69
RS
5191 struct mips_elf_link_hash_table *htab;
5192 bfd *dynobj;
5193
5194 dynobj = elf_hash_table (info)->dynobj;
5195 htab = mips_elf_hash_table (info);
4dfe6ac6 5196 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5197
5198 /* Parse the relocation. */
5199 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5200 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5201 p = (input_section->output_section->vma
5202 + input_section->output_offset
5203 + relocation->r_offset);
5204
5205 /* Assume that there will be no overflow. */
b34976b6 5206 overflowed_p = FALSE;
b49e97c9
TS
5207
5208 /* Figure out whether or not the symbol is local, and get the offset
5209 used in the array of hash table entries. */
5210 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5211 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 5212 local_sections);
bce03d3d 5213 was_local_p = local_p;
b49e97c9
TS
5214 if (! elf_bad_symtab (input_bfd))
5215 extsymoff = symtab_hdr->sh_info;
5216 else
5217 {
5218 /* The symbol table does not follow the rule that local symbols
5219 must come before globals. */
5220 extsymoff = 0;
5221 }
5222
5223 /* Figure out the value of the symbol. */
5224 if (local_p)
5225 {
5226 Elf_Internal_Sym *sym;
5227
5228 sym = local_syms + r_symndx;
5229 sec = local_sections[r_symndx];
5230
5231 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
5232 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5233 || (sec->flags & SEC_MERGE))
b49e97c9 5234 symbol += sym->st_value;
d4df96e6
L
5235 if ((sec->flags & SEC_MERGE)
5236 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5237 {
5238 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5239 addend -= symbol;
5240 addend += sec->output_section->vma + sec->output_offset;
5241 }
b49e97c9 5242
df58fc94
RS
5243 /* MIPS16/microMIPS text labels should be treated as odd. */
5244 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
5245 ++symbol;
5246
5247 /* Record the name of this symbol, for our caller. */
5248 *namep = bfd_elf_string_from_elf_section (input_bfd,
5249 symtab_hdr->sh_link,
5250 sym->st_name);
5251 if (*namep == '\0')
5252 *namep = bfd_section_name (input_bfd, sec);
5253
30c09090 5254 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
df58fc94 5255 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
b49e97c9
TS
5256 }
5257 else
5258 {
560e09e9
NC
5259 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5260
b49e97c9
TS
5261 /* For global symbols we look up the symbol in the hash-table. */
5262 h = ((struct mips_elf_link_hash_entry *)
5263 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5264 /* Find the real hash-table entry for this symbol. */
5265 while (h->root.root.type == bfd_link_hash_indirect
5266 || h->root.root.type == bfd_link_hash_warning)
5267 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5268
5269 /* Record the name of this symbol, for our caller. */
5270 *namep = h->root.root.root.string;
5271
5272 /* See if this is the special _gp_disp symbol. Note that such a
5273 symbol must always be a global symbol. */
560e09e9 5274 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
5275 && ! NEWABI_P (input_bfd))
5276 {
5277 /* Relocations against _gp_disp are permitted only with
5278 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 5279 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
5280 return bfd_reloc_notsupported;
5281
b34976b6 5282 gp_disp_p = TRUE;
b49e97c9 5283 }
bbe506e8
TS
5284 /* See if this is the special _gp symbol. Note that such a
5285 symbol must always be a global symbol. */
5286 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5287 gnu_local_gp_p = TRUE;
5288
5289
b49e97c9
TS
5290 /* If this symbol is defined, calculate its address. Note that
5291 _gp_disp is a magic symbol, always implicitly defined by the
5292 linker, so it's inappropriate to check to see whether or not
5293 its defined. */
5294 else if ((h->root.root.type == bfd_link_hash_defined
5295 || h->root.root.type == bfd_link_hash_defweak)
5296 && h->root.root.u.def.section)
5297 {
5298 sec = h->root.root.u.def.section;
5299 if (sec->output_section)
5300 symbol = (h->root.root.u.def.value
5301 + sec->output_section->vma
5302 + sec->output_offset);
5303 else
5304 symbol = h->root.root.u.def.value;
5305 }
5306 else if (h->root.root.type == bfd_link_hash_undefweak)
5307 /* We allow relocations against undefined weak symbols, giving
5308 it the value zero, so that you can undefined weak functions
5309 and check to see if they exist by looking at their
5310 addresses. */
5311 symbol = 0;
59c2e50f 5312 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5313 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5314 symbol = 0;
a4d0f181
TS
5315 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5316 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5317 {
5318 /* If this is a dynamic link, we should have created a
5319 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5320 in in _bfd_mips_elf_create_dynamic_sections.
5321 Otherwise, we should define the symbol with a value of 0.
5322 FIXME: It should probably get into the symbol table
5323 somehow as well. */
5324 BFD_ASSERT (! info->shared);
5325 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5326 symbol = 0;
5327 }
5e2b0d47
NC
5328 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5329 {
5330 /* This is an optional symbol - an Irix specific extension to the
5331 ELF spec. Ignore it for now.
5332 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5333 than simply ignoring them, but we do not handle this for now.
5334 For information see the "64-bit ELF Object File Specification"
5335 which is available from here:
5336 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5337 symbol = 0;
5338 }
e7e2196d
MR
5339 else if ((*info->callbacks->undefined_symbol)
5340 (info, h->root.root.root.string, input_bfd,
5341 input_section, relocation->r_offset,
5342 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5343 || ELF_ST_VISIBILITY (h->root.other)))
5344 {
5345 return bfd_reloc_undefined;
5346 }
b49e97c9
TS
5347 else
5348 {
e7e2196d 5349 return bfd_reloc_notsupported;
b49e97c9
TS
5350 }
5351
30c09090 5352 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
1bbce132 5353 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
b49e97c9
TS
5354 }
5355
738e5348
RS
5356 /* If this is a reference to a 16-bit function with a stub, we need
5357 to redirect the relocation to the stub unless:
5358
5359 (a) the relocation is for a MIPS16 JAL;
5360
5361 (b) the relocation is for a MIPS16 PIC call, and there are no
5362 non-MIPS16 uses of the GOT slot; or
5363
5364 (c) the section allows direct references to MIPS16 functions. */
5365 if (r_type != R_MIPS16_26
5366 && !info->relocatable
5367 && ((h != NULL
5368 && h->fn_stub != NULL
5369 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71 5370 || (local_p
698600e4
AM
5371 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5372 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5373 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5374 {
5375 /* This is a 32- or 64-bit call to a 16-bit function. We should
5376 have already noticed that we were going to need the
5377 stub. */
5378 if (local_p)
8f0c309a 5379 {
698600e4 5380 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
8f0c309a
CLT
5381 value = 0;
5382 }
b49e97c9
TS
5383 else
5384 {
5385 BFD_ASSERT (h->need_fn_stub);
8f0c309a
CLT
5386 if (h->la25_stub)
5387 {
5388 /* If a LA25 header for the stub itself exists, point to the
5389 prepended LUI/ADDIU sequence. */
5390 sec = h->la25_stub->stub_section;
5391 value = h->la25_stub->offset;
5392 }
5393 else
5394 {
5395 sec = h->fn_stub;
5396 value = 0;
5397 }
b49e97c9
TS
5398 }
5399
8f0c309a 5400 symbol = sec->output_section->vma + sec->output_offset + value;
f38c2df5
TS
5401 /* The target is 16-bit, but the stub isn't. */
5402 target_is_16_bit_code_p = FALSE;
b49e97c9 5403 }
1bbce132
MR
5404 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5405 to a standard MIPS function, we need to redirect the call to the stub.
5406 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5407 indirect calls should use an indirect stub instead. */
1049f94e 5408 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5409 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71 5410 || (local_p
698600e4
AM
5411 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5412 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
1bbce132 5413 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
b49e97c9 5414 {
b9d58d71 5415 if (local_p)
698600e4 5416 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
b9d58d71 5417 else
b49e97c9 5418 {
b9d58d71
TS
5419 /* If both call_stub and call_fp_stub are defined, we can figure
5420 out which one to use by checking which one appears in the input
5421 file. */
5422 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5423 {
b9d58d71 5424 asection *o;
68ffbac6 5425
b9d58d71
TS
5426 sec = NULL;
5427 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5428 {
b9d58d71
TS
5429 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5430 {
5431 sec = h->call_fp_stub;
5432 break;
5433 }
b49e97c9 5434 }
b9d58d71
TS
5435 if (sec == NULL)
5436 sec = h->call_stub;
b49e97c9 5437 }
b9d58d71 5438 else if (h->call_stub != NULL)
b49e97c9 5439 sec = h->call_stub;
b9d58d71
TS
5440 else
5441 sec = h->call_fp_stub;
5442 }
b49e97c9 5443
eea6121a 5444 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5445 symbol = sec->output_section->vma + sec->output_offset;
5446 }
861fb55a
DJ
5447 /* If this is a direct call to a PIC function, redirect to the
5448 non-PIC stub. */
5449 else if (h != NULL && h->la25_stub
8f0c309a
CLT
5450 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5451 target_is_16_bit_code_p))
861fb55a
DJ
5452 symbol = (h->la25_stub->stub_section->output_section->vma
5453 + h->la25_stub->stub_section->output_offset
5454 + h->la25_stub->offset);
1bbce132
MR
5455 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5456 entry is used if a standard PLT entry has also been made. In this
5457 case the symbol will have been set by mips_elf_set_plt_sym_value
5458 to point to the standard PLT entry, so redirect to the compressed
5459 one. */
5460 else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
5461 && !info->relocatable
5462 && h != NULL
5463 && h->use_plt_entry
5464 && h->root.plt.plist->comp_offset != MINUS_ONE
5465 && h->root.plt.plist->mips_offset != MINUS_ONE)
5466 {
5467 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5468
5469 sec = htab->splt;
5470 symbol = (sec->output_section->vma
5471 + sec->output_offset
5472 + htab->plt_header_size
5473 + htab->plt_mips_offset
5474 + h->root.plt.plist->comp_offset
5475 + 1);
5476
5477 target_is_16_bit_code_p = !micromips_p;
5478 target_is_micromips_code_p = micromips_p;
5479 }
b49e97c9 5480
df58fc94
RS
5481 /* Make sure MIPS16 and microMIPS are not used together. */
5482 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5483 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5484 {
5485 (*_bfd_error_handler)
5486 (_("MIPS16 and microMIPS functions cannot call each other"));
5487 return bfd_reloc_notsupported;
5488 }
5489
b49e97c9 5490 /* Calls from 16-bit code to 32-bit code and vice versa require the
df58fc94
RS
5491 mode change. However, we can ignore calls to undefined weak symbols,
5492 which should never be executed at runtime. This exception is important
5493 because the assembly writer may have "known" that any definition of the
5494 symbol would be 16-bit code, and that direct jumps were therefore
5495 acceptable. */
5496 *cross_mode_jump_p = (!info->relocatable
5497 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5498 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5499 || (r_type == R_MICROMIPS_26_S1
5500 && !target_is_micromips_code_p)
5501 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5502 && (target_is_16_bit_code_p
5503 || target_is_micromips_code_p))));
b49e97c9 5504
c5d6fa44 5505 local_p = (h == NULL || mips_use_local_got_p (info, h));
b49e97c9 5506
0a61c8c2
RS
5507 gp0 = _bfd_get_gp_value (input_bfd);
5508 gp = _bfd_get_gp_value (abfd);
23cc69b6 5509 if (htab->got_info)
a8028dd0 5510 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5511
5512 if (gnu_local_gp_p)
5513 symbol = gp;
5514
df58fc94
RS
5515 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5516 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5517 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5518 if (got_page_reloc_p (r_type) && !local_p)
020d7251 5519 {
df58fc94
RS
5520 r_type = (micromips_reloc_p (r_type)
5521 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
020d7251
RS
5522 addend = 0;
5523 }
5524
e77760d2 5525 /* If we haven't already determined the GOT offset, and we're going
0a61c8c2 5526 to need it, get it now. */
b49e97c9
TS
5527 switch (r_type)
5528 {
738e5348
RS
5529 case R_MIPS16_CALL16:
5530 case R_MIPS16_GOT16:
b49e97c9
TS
5531 case R_MIPS_CALL16:
5532 case R_MIPS_GOT16:
5533 case R_MIPS_GOT_DISP:
5534 case R_MIPS_GOT_HI16:
5535 case R_MIPS_CALL_HI16:
5536 case R_MIPS_GOT_LO16:
5537 case R_MIPS_CALL_LO16:
df58fc94
RS
5538 case R_MICROMIPS_CALL16:
5539 case R_MICROMIPS_GOT16:
5540 case R_MICROMIPS_GOT_DISP:
5541 case R_MICROMIPS_GOT_HI16:
5542 case R_MICROMIPS_CALL_HI16:
5543 case R_MICROMIPS_GOT_LO16:
5544 case R_MICROMIPS_CALL_LO16:
0f20cc35
DJ
5545 case R_MIPS_TLS_GD:
5546 case R_MIPS_TLS_GOTTPREL:
5547 case R_MIPS_TLS_LDM:
d0f13682
CLT
5548 case R_MIPS16_TLS_GD:
5549 case R_MIPS16_TLS_GOTTPREL:
5550 case R_MIPS16_TLS_LDM:
df58fc94
RS
5551 case R_MICROMIPS_TLS_GD:
5552 case R_MICROMIPS_TLS_GOTTPREL:
5553 case R_MICROMIPS_TLS_LDM:
b49e97c9 5554 /* Find the index into the GOT where this value is located. */
df58fc94 5555 if (tls_ldm_reloc_p (r_type))
0f20cc35 5556 {
0a44bf69 5557 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5558 0, 0, NULL, r_type);
0f20cc35
DJ
5559 if (g == MINUS_ONE)
5560 return bfd_reloc_outofrange;
5561 }
5562 else if (!local_p)
b49e97c9 5563 {
0a44bf69
RS
5564 /* On VxWorks, CALL relocations should refer to the .got.plt
5565 entry, which is initialized to point at the PLT stub. */
5566 if (htab->is_vxworks
df58fc94
RS
5567 && (call_hi16_reloc_p (r_type)
5568 || call_lo16_reloc_p (r_type)
738e5348 5569 || call16_reloc_p (r_type)))
0a44bf69
RS
5570 {
5571 BFD_ASSERT (addend == 0);
5572 BFD_ASSERT (h->root.needs_plt);
5573 g = mips_elf_gotplt_index (info, &h->root);
5574 }
5575 else
b49e97c9 5576 {
020d7251 5577 BFD_ASSERT (addend == 0);
13fbec83
RS
5578 g = mips_elf_global_got_index (abfd, info, input_bfd,
5579 &h->root, r_type);
e641e783 5580 if (!TLS_RELOC_P (r_type)
020d7251
RS
5581 && !elf_hash_table (info)->dynamic_sections_created)
5582 /* This is a static link. We must initialize the GOT entry. */
a8028dd0 5583 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5584 }
5585 }
0a44bf69 5586 else if (!htab->is_vxworks
738e5348 5587 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5588 /* The calculation below does not involve "g". */
b49e97c9
TS
5589 break;
5590 else
5591 {
5c18022e 5592 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5593 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5594 if (g == MINUS_ONE)
5595 return bfd_reloc_outofrange;
5596 }
5597
5598 /* Convert GOT indices to actual offsets. */
a8028dd0 5599 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5600 break;
b49e97c9
TS
5601 }
5602
0a44bf69
RS
5603 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5604 symbols are resolved by the loader. Add them to .rela.dyn. */
5605 if (h != NULL && is_gott_symbol (info, &h->root))
5606 {
5607 Elf_Internal_Rela outrel;
5608 bfd_byte *loc;
5609 asection *s;
5610
5611 s = mips_elf_rel_dyn_section (info, FALSE);
5612 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5613
5614 outrel.r_offset = (input_section->output_section->vma
5615 + input_section->output_offset
5616 + relocation->r_offset);
5617 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5618 outrel.r_addend = addend;
5619 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5620
5621 /* If we've written this relocation for a readonly section,
5622 we need to set DF_TEXTREL again, so that we do not delete the
5623 DT_TEXTREL tag. */
5624 if (MIPS_ELF_READONLY_SECTION (input_section))
5625 info->flags |= DF_TEXTREL;
5626
0a44bf69
RS
5627 *valuep = 0;
5628 return bfd_reloc_ok;
5629 }
5630
b49e97c9
TS
5631 /* Figure out what kind of relocation is being performed. */
5632 switch (r_type)
5633 {
5634 case R_MIPS_NONE:
5635 return bfd_reloc_continue;
5636
5637 case R_MIPS_16:
a7ebbfdf 5638 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5639 overflowed_p = mips_elf_overflow_p (value, 16);
5640 break;
5641
5642 case R_MIPS_32:
5643 case R_MIPS_REL32:
5644 case R_MIPS_64:
5645 if ((info->shared
861fb55a 5646 || (htab->root.dynamic_sections_created
b49e97c9 5647 && h != NULL
f5385ebf 5648 && h->root.def_dynamic
861fb55a
DJ
5649 && !h->root.def_regular
5650 && !h->has_static_relocs))
cf35638d 5651 && r_symndx != STN_UNDEF
9a59ad6b
DJ
5652 && (h == NULL
5653 || h->root.root.type != bfd_link_hash_undefweak
5654 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5655 && (input_section->flags & SEC_ALLOC) != 0)
5656 {
861fb55a 5657 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5658 where the symbol will end up. So, we create a relocation
5659 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5660 linker. We must do the same for executable references to
5661 shared library symbols, unless we've decided to use copy
5662 relocs or PLTs instead. */
b49e97c9
TS
5663 value = addend;
5664 if (!mips_elf_create_dynamic_relocation (abfd,
5665 info,
5666 relocation,
5667 h,
5668 sec,
5669 symbol,
5670 &value,
5671 input_section))
5672 return bfd_reloc_undefined;
5673 }
5674 else
5675 {
5676 if (r_type != R_MIPS_REL32)
5677 value = symbol + addend;
5678 else
5679 value = addend;
5680 }
5681 value &= howto->dst_mask;
092dcd75
CD
5682 break;
5683
5684 case R_MIPS_PC32:
5685 value = symbol + addend - p;
5686 value &= howto->dst_mask;
b49e97c9
TS
5687 break;
5688
b49e97c9
TS
5689 case R_MIPS16_26:
5690 /* The calculation for R_MIPS16_26 is just the same as for an
5691 R_MIPS_26. It's only the storage of the relocated field into
5692 the output file that's different. That's handled in
5693 mips_elf_perform_relocation. So, we just fall through to the
5694 R_MIPS_26 case here. */
5695 case R_MIPS_26:
df58fc94
RS
5696 case R_MICROMIPS_26_S1:
5697 {
5698 unsigned int shift;
5699
5700 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5701 the correct ISA mode selector and bit 1 must be 0. */
5702 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5703 return bfd_reloc_outofrange;
5704
5705 /* Shift is 2, unusually, for microMIPS JALX. */
5706 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5707
5708 if (was_local_p)
5709 value = addend | ((p + 4) & (0xfc000000 << shift));
5710 else
5711 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5712 value = (value + symbol) >> shift;
5713 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5714 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5715 value &= howto->dst_mask;
5716 }
b49e97c9
TS
5717 break;
5718
0f20cc35 5719 case R_MIPS_TLS_DTPREL_HI16:
d0f13682 5720 case R_MIPS16_TLS_DTPREL_HI16:
df58fc94 5721 case R_MICROMIPS_TLS_DTPREL_HI16:
0f20cc35
DJ
5722 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5723 & howto->dst_mask);
5724 break;
5725
5726 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5727 case R_MIPS_TLS_DTPREL32:
5728 case R_MIPS_TLS_DTPREL64:
d0f13682 5729 case R_MIPS16_TLS_DTPREL_LO16:
df58fc94 5730 case R_MICROMIPS_TLS_DTPREL_LO16:
0f20cc35
DJ
5731 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5732 break;
5733
5734 case R_MIPS_TLS_TPREL_HI16:
d0f13682 5735 case R_MIPS16_TLS_TPREL_HI16:
df58fc94 5736 case R_MICROMIPS_TLS_TPREL_HI16:
0f20cc35
DJ
5737 value = (mips_elf_high (addend + symbol - tprel_base (info))
5738 & howto->dst_mask);
5739 break;
5740
5741 case R_MIPS_TLS_TPREL_LO16:
d0f13682
CLT
5742 case R_MIPS_TLS_TPREL32:
5743 case R_MIPS_TLS_TPREL64:
5744 case R_MIPS16_TLS_TPREL_LO16:
df58fc94 5745 case R_MICROMIPS_TLS_TPREL_LO16:
0f20cc35
DJ
5746 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5747 break;
5748
b49e97c9 5749 case R_MIPS_HI16:
d6f16593 5750 case R_MIPS16_HI16:
df58fc94 5751 case R_MICROMIPS_HI16:
b49e97c9
TS
5752 if (!gp_disp_p)
5753 {
5754 value = mips_elf_high (addend + symbol);
5755 value &= howto->dst_mask;
5756 }
5757 else
5758 {
d6f16593
MR
5759 /* For MIPS16 ABI code we generate this sequence
5760 0: li $v0,%hi(_gp_disp)
5761 4: addiupc $v1,%lo(_gp_disp)
5762 8: sll $v0,16
5763 12: addu $v0,$v1
5764 14: move $gp,$v0
5765 So the offsets of hi and lo relocs are the same, but the
888b9c01
CLT
5766 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5767 ADDIUPC clears the low two bits of the instruction address,
5768 so the base is ($t9 + 4) & ~3. */
d6f16593 5769 if (r_type == R_MIPS16_HI16)
888b9c01 5770 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
df58fc94
RS
5771 /* The microMIPS .cpload sequence uses the same assembly
5772 instructions as the traditional psABI version, but the
5773 incoming $t9 has the low bit set. */
5774 else if (r_type == R_MICROMIPS_HI16)
5775 value = mips_elf_high (addend + gp - p - 1);
d6f16593
MR
5776 else
5777 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5778 overflowed_p = mips_elf_overflow_p (value, 16);
5779 }
5780 break;
5781
5782 case R_MIPS_LO16:
d6f16593 5783 case R_MIPS16_LO16:
df58fc94
RS
5784 case R_MICROMIPS_LO16:
5785 case R_MICROMIPS_HI0_LO16:
b49e97c9
TS
5786 if (!gp_disp_p)
5787 value = (symbol + addend) & howto->dst_mask;
5788 else
5789 {
d6f16593
MR
5790 /* See the comment for R_MIPS16_HI16 above for the reason
5791 for this conditional. */
5792 if (r_type == R_MIPS16_LO16)
888b9c01 5793 value = addend + gp - (p & ~(bfd_vma) 0x3);
df58fc94
RS
5794 else if (r_type == R_MICROMIPS_LO16
5795 || r_type == R_MICROMIPS_HI0_LO16)
5796 value = addend + gp - p + 3;
d6f16593
MR
5797 else
5798 value = addend + gp - p + 4;
b49e97c9 5799 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5800 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5801 _gp_disp are normally generated from the .cpload
5802 pseudo-op. It generates code that normally looks like
5803 this:
5804
5805 lui $gp,%hi(_gp_disp)
5806 addiu $gp,$gp,%lo(_gp_disp)
5807 addu $gp,$gp,$t9
5808
5809 Here $t9 holds the address of the function being called,
5810 as required by the MIPS ELF ABI. The R_MIPS_LO16
5811 relocation can easily overflow in this situation, but the
5812 R_MIPS_HI16 relocation will handle the overflow.
5813 Therefore, we consider this a bug in the MIPS ABI, and do
5814 not check for overflow here. */
5815 }
5816 break;
5817
5818 case R_MIPS_LITERAL:
df58fc94 5819 case R_MICROMIPS_LITERAL:
b49e97c9
TS
5820 /* Because we don't merge literal sections, we can handle this
5821 just like R_MIPS_GPREL16. In the long run, we should merge
5822 shared literals, and then we will need to additional work
5823 here. */
5824
5825 /* Fall through. */
5826
5827 case R_MIPS16_GPREL:
5828 /* The R_MIPS16_GPREL performs the same calculation as
5829 R_MIPS_GPREL16, but stores the relocated bits in a different
5830 order. We don't need to do anything special here; the
5831 differences are handled in mips_elf_perform_relocation. */
5832 case R_MIPS_GPREL16:
df58fc94
RS
5833 case R_MICROMIPS_GPREL7_S2:
5834 case R_MICROMIPS_GPREL16:
bce03d3d
AO
5835 /* Only sign-extend the addend if it was extracted from the
5836 instruction. If the addend was separate, leave it alone,
5837 otherwise we may lose significant bits. */
5838 if (howto->partial_inplace)
a7ebbfdf 5839 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5840 value = symbol + addend - gp;
5841 /* If the symbol was local, any earlier relocatable links will
5842 have adjusted its addend with the gp offset, so compensate
5843 for that now. Don't do it for symbols forced local in this
5844 link, though, since they won't have had the gp offset applied
5845 to them before. */
5846 if (was_local_p)
5847 value += gp0;
b49e97c9
TS
5848 overflowed_p = mips_elf_overflow_p (value, 16);
5849 break;
5850
738e5348
RS
5851 case R_MIPS16_GOT16:
5852 case R_MIPS16_CALL16:
b49e97c9
TS
5853 case R_MIPS_GOT16:
5854 case R_MIPS_CALL16:
df58fc94
RS
5855 case R_MICROMIPS_GOT16:
5856 case R_MICROMIPS_CALL16:
0a44bf69 5857 /* VxWorks does not have separate local and global semantics for
738e5348 5858 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5859 if (!htab->is_vxworks && local_p)
b49e97c9 5860 {
5c18022e 5861 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 5862 symbol + addend, !was_local_p);
b49e97c9
TS
5863 if (value == MINUS_ONE)
5864 return bfd_reloc_outofrange;
5865 value
a8028dd0 5866 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5867 overflowed_p = mips_elf_overflow_p (value, 16);
5868 break;
5869 }
5870
5871 /* Fall through. */
5872
0f20cc35
DJ
5873 case R_MIPS_TLS_GD:
5874 case R_MIPS_TLS_GOTTPREL:
5875 case R_MIPS_TLS_LDM:
b49e97c9 5876 case R_MIPS_GOT_DISP:
d0f13682
CLT
5877 case R_MIPS16_TLS_GD:
5878 case R_MIPS16_TLS_GOTTPREL:
5879 case R_MIPS16_TLS_LDM:
df58fc94
RS
5880 case R_MICROMIPS_TLS_GD:
5881 case R_MICROMIPS_TLS_GOTTPREL:
5882 case R_MICROMIPS_TLS_LDM:
5883 case R_MICROMIPS_GOT_DISP:
b49e97c9
TS
5884 value = g;
5885 overflowed_p = mips_elf_overflow_p (value, 16);
5886 break;
5887
5888 case R_MIPS_GPREL32:
bce03d3d
AO
5889 value = (addend + symbol + gp0 - gp);
5890 if (!save_addend)
5891 value &= howto->dst_mask;
b49e97c9
TS
5892 break;
5893
5894 case R_MIPS_PC16:
bad36eac
DJ
5895 case R_MIPS_GNU_REL16_S2:
5896 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5897 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5898 value >>= howto->rightshift;
5899 value &= howto->dst_mask;
b49e97c9
TS
5900 break;
5901
df58fc94
RS
5902 case R_MICROMIPS_PC7_S1:
5903 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5904 overflowed_p = mips_elf_overflow_p (value, 8);
5905 value >>= howto->rightshift;
5906 value &= howto->dst_mask;
5907 break;
5908
5909 case R_MICROMIPS_PC10_S1:
5910 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5911 overflowed_p = mips_elf_overflow_p (value, 11);
5912 value >>= howto->rightshift;
5913 value &= howto->dst_mask;
5914 break;
5915
5916 case R_MICROMIPS_PC16_S1:
5917 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5918 overflowed_p = mips_elf_overflow_p (value, 17);
5919 value >>= howto->rightshift;
5920 value &= howto->dst_mask;
5921 break;
5922
5923 case R_MICROMIPS_PC23_S2:
5924 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5925 overflowed_p = mips_elf_overflow_p (value, 25);
5926 value >>= howto->rightshift;
5927 value &= howto->dst_mask;
5928 break;
5929
b49e97c9
TS
5930 case R_MIPS_GOT_HI16:
5931 case R_MIPS_CALL_HI16:
df58fc94
RS
5932 case R_MICROMIPS_GOT_HI16:
5933 case R_MICROMIPS_CALL_HI16:
b49e97c9
TS
5934 /* We're allowed to handle these two relocations identically.
5935 The dynamic linker is allowed to handle the CALL relocations
5936 differently by creating a lazy evaluation stub. */
5937 value = g;
5938 value = mips_elf_high (value);
5939 value &= howto->dst_mask;
5940 break;
5941
5942 case R_MIPS_GOT_LO16:
5943 case R_MIPS_CALL_LO16:
df58fc94
RS
5944 case R_MICROMIPS_GOT_LO16:
5945 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
5946 value = g & howto->dst_mask;
5947 break;
5948
5949 case R_MIPS_GOT_PAGE:
df58fc94 5950 case R_MICROMIPS_GOT_PAGE:
5c18022e 5951 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5952 if (value == MINUS_ONE)
5953 return bfd_reloc_outofrange;
a8028dd0 5954 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5955 overflowed_p = mips_elf_overflow_p (value, 16);
5956 break;
5957
5958 case R_MIPS_GOT_OFST:
df58fc94 5959 case R_MICROMIPS_GOT_OFST:
93a2b7ae 5960 if (local_p)
5c18022e 5961 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5962 else
5963 value = addend;
b49e97c9
TS
5964 overflowed_p = mips_elf_overflow_p (value, 16);
5965 break;
5966
5967 case R_MIPS_SUB:
df58fc94 5968 case R_MICROMIPS_SUB:
b49e97c9
TS
5969 value = symbol - addend;
5970 value &= howto->dst_mask;
5971 break;
5972
5973 case R_MIPS_HIGHER:
df58fc94 5974 case R_MICROMIPS_HIGHER:
b49e97c9
TS
5975 value = mips_elf_higher (addend + symbol);
5976 value &= howto->dst_mask;
5977 break;
5978
5979 case R_MIPS_HIGHEST:
df58fc94 5980 case R_MICROMIPS_HIGHEST:
b49e97c9
TS
5981 value = mips_elf_highest (addend + symbol);
5982 value &= howto->dst_mask;
5983 break;
5984
5985 case R_MIPS_SCN_DISP:
df58fc94 5986 case R_MICROMIPS_SCN_DISP:
b49e97c9
TS
5987 value = symbol + addend - sec->output_offset;
5988 value &= howto->dst_mask;
5989 break;
5990
b49e97c9 5991 case R_MIPS_JALR:
df58fc94 5992 case R_MICROMIPS_JALR:
1367d393
ILT
5993 /* This relocation is only a hint. In some cases, we optimize
5994 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5995 when the symbol does not resolve locally. */
5996 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5997 return bfd_reloc_continue;
5998 value = symbol + addend;
5999 break;
b49e97c9 6000
1367d393 6001 case R_MIPS_PJUMP:
b49e97c9
TS
6002 case R_MIPS_GNU_VTINHERIT:
6003 case R_MIPS_GNU_VTENTRY:
6004 /* We don't do anything with these at present. */
6005 return bfd_reloc_continue;
6006
6007 default:
6008 /* An unrecognized relocation type. */
6009 return bfd_reloc_notsupported;
6010 }
6011
6012 /* Store the VALUE for our caller. */
6013 *valuep = value;
6014 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6015}
6016
6017/* Obtain the field relocated by RELOCATION. */
6018
6019static bfd_vma
9719ad41
RS
6020mips_elf_obtain_contents (reloc_howto_type *howto,
6021 const Elf_Internal_Rela *relocation,
6022 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
6023{
6024 bfd_vma x;
6025 bfd_byte *location = contents + relocation->r_offset;
6026
6027 /* Obtain the bytes. */
6028 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
6029
b49e97c9
TS
6030 return x;
6031}
6032
6033/* It has been determined that the result of the RELOCATION is the
6034 VALUE. Use HOWTO to place VALUE into the output file at the
6035 appropriate position. The SECTION is the section to which the
68ffbac6 6036 relocation applies.
38a7df63 6037 CROSS_MODE_JUMP_P is true if the relocation field
df58fc94 6038 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9 6039
b34976b6 6040 Returns FALSE if anything goes wrong. */
b49e97c9 6041
b34976b6 6042static bfd_boolean
9719ad41
RS
6043mips_elf_perform_relocation (struct bfd_link_info *info,
6044 reloc_howto_type *howto,
6045 const Elf_Internal_Rela *relocation,
6046 bfd_vma value, bfd *input_bfd,
6047 asection *input_section, bfd_byte *contents,
38a7df63 6048 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
6049{
6050 bfd_vma x;
6051 bfd_byte *location;
6052 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6053
6054 /* Figure out where the relocation is occurring. */
6055 location = contents + relocation->r_offset;
6056
df58fc94 6057 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
d6f16593 6058
b49e97c9
TS
6059 /* Obtain the current value. */
6060 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6061
6062 /* Clear the field we are setting. */
6063 x &= ~howto->dst_mask;
6064
b49e97c9
TS
6065 /* Set the field. */
6066 x |= (value & howto->dst_mask);
6067
6068 /* If required, turn JAL into JALX. */
38a7df63 6069 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 6070 {
b34976b6 6071 bfd_boolean ok;
b49e97c9
TS
6072 bfd_vma opcode = x >> 26;
6073 bfd_vma jalx_opcode;
6074
6075 /* Check to see if the opcode is already JAL or JALX. */
6076 if (r_type == R_MIPS16_26)
6077 {
6078 ok = ((opcode == 0x6) || (opcode == 0x7));
6079 jalx_opcode = 0x7;
6080 }
df58fc94
RS
6081 else if (r_type == R_MICROMIPS_26_S1)
6082 {
6083 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6084 jalx_opcode = 0x3c;
6085 }
b49e97c9
TS
6086 else
6087 {
6088 ok = ((opcode == 0x3) || (opcode == 0x1d));
6089 jalx_opcode = 0x1d;
6090 }
6091
3bdf9505
MR
6092 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6093 convert J or JALS to JALX. */
b49e97c9
TS
6094 if (!ok)
6095 {
6096 (*_bfd_error_handler)
3bdf9505 6097 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
d003868e
AM
6098 input_bfd,
6099 input_section,
b49e97c9
TS
6100 (unsigned long) relocation->r_offset);
6101 bfd_set_error (bfd_error_bad_value);
b34976b6 6102 return FALSE;
b49e97c9
TS
6103 }
6104
6105 /* Make this the JALX opcode. */
6106 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6107 }
6108
38a7df63
CF
6109 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6110 range. */
cd8d5a82 6111 if (!info->relocatable
38a7df63 6112 && !cross_mode_jump_p
cd8d5a82
CF
6113 && ((JAL_TO_BAL_P (input_bfd)
6114 && r_type == R_MIPS_26
6115 && (x >> 26) == 0x3) /* jal addr */
6116 || (JALR_TO_BAL_P (input_bfd)
6117 && r_type == R_MIPS_JALR
38a7df63
CF
6118 && x == 0x0320f809) /* jalr t9 */
6119 || (JR_TO_B_P (input_bfd)
6120 && r_type == R_MIPS_JALR
6121 && x == 0x03200008))) /* jr t9 */
1367d393
ILT
6122 {
6123 bfd_vma addr;
6124 bfd_vma dest;
6125 bfd_signed_vma off;
6126
6127 addr = (input_section->output_section->vma
6128 + input_section->output_offset
6129 + relocation->r_offset
6130 + 4);
6131 if (r_type == R_MIPS_26)
6132 dest = (value << 2) | ((addr >> 28) << 28);
6133 else
6134 dest = value;
6135 off = dest - addr;
6136 if (off <= 0x1ffff && off >= -0x20000)
38a7df63
CF
6137 {
6138 if (x == 0x03200008) /* jr t9 */
6139 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6140 else
6141 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6142 }
1367d393
ILT
6143 }
6144
b49e97c9
TS
6145 /* Put the value into the output. */
6146 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593 6147
df58fc94
RS
6148 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
6149 location);
d6f16593 6150
b34976b6 6151 return TRUE;
b49e97c9 6152}
b49e97c9 6153\f
b49e97c9
TS
6154/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6155 is the original relocation, which is now being transformed into a
6156 dynamic relocation. The ADDENDP is adjusted if necessary; the
6157 caller should store the result in place of the original addend. */
6158
b34976b6 6159static bfd_boolean
9719ad41
RS
6160mips_elf_create_dynamic_relocation (bfd *output_bfd,
6161 struct bfd_link_info *info,
6162 const Elf_Internal_Rela *rel,
6163 struct mips_elf_link_hash_entry *h,
6164 asection *sec, bfd_vma symbol,
6165 bfd_vma *addendp, asection *input_section)
b49e97c9 6166{
947216bf 6167 Elf_Internal_Rela outrel[3];
b49e97c9
TS
6168 asection *sreloc;
6169 bfd *dynobj;
6170 int r_type;
5d41f0b6
RS
6171 long indx;
6172 bfd_boolean defined_p;
0a44bf69 6173 struct mips_elf_link_hash_table *htab;
b49e97c9 6174
0a44bf69 6175 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6176 BFD_ASSERT (htab != NULL);
6177
b49e97c9
TS
6178 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6179 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 6180 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
6181 BFD_ASSERT (sreloc != NULL);
6182 BFD_ASSERT (sreloc->contents != NULL);
6183 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 6184 < sreloc->size);
b49e97c9 6185
b49e97c9
TS
6186 outrel[0].r_offset =
6187 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
6188 if (ABI_64_P (output_bfd))
6189 {
6190 outrel[1].r_offset =
6191 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6192 outrel[2].r_offset =
6193 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6194 }
b49e97c9 6195
c5ae1840 6196 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 6197 /* The relocation field has been deleted. */
5d41f0b6
RS
6198 return TRUE;
6199
6200 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
6201 {
6202 /* The relocation field has been converted into a relative value of
6203 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6204 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 6205 *addendp += symbol;
5d41f0b6 6206 return TRUE;
0d591ff7 6207 }
b49e97c9 6208
5d41f0b6
RS
6209 /* We must now calculate the dynamic symbol table index to use
6210 in the relocation. */
d4a77f3f 6211 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 6212 {
020d7251 6213 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5d41f0b6
RS
6214 indx = h->root.dynindx;
6215 if (SGI_COMPAT (output_bfd))
6216 defined_p = h->root.def_regular;
6217 else
6218 /* ??? glibc's ld.so just adds the final GOT entry to the
6219 relocation field. It therefore treats relocs against
6220 defined symbols in the same way as relocs against
6221 undefined symbols. */
6222 defined_p = FALSE;
6223 }
b49e97c9
TS
6224 else
6225 {
5d41f0b6
RS
6226 if (sec != NULL && bfd_is_abs_section (sec))
6227 indx = 0;
6228 else if (sec == NULL || sec->owner == NULL)
fdd07405 6229 {
5d41f0b6
RS
6230 bfd_set_error (bfd_error_bad_value);
6231 return FALSE;
b49e97c9
TS
6232 }
6233 else
6234 {
5d41f0b6 6235 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
6236 if (indx == 0)
6237 {
6238 asection *osec = htab->root.text_index_section;
6239 indx = elf_section_data (osec)->dynindx;
6240 }
5d41f0b6
RS
6241 if (indx == 0)
6242 abort ();
b49e97c9
TS
6243 }
6244
5d41f0b6
RS
6245 /* Instead of generating a relocation using the section
6246 symbol, we may as well make it a fully relative
6247 relocation. We want to avoid generating relocations to
6248 local symbols because we used to generate them
6249 incorrectly, without adding the original symbol value,
6250 which is mandated by the ABI for section symbols. In
6251 order to give dynamic loaders and applications time to
6252 phase out the incorrect use, we refrain from emitting
6253 section-relative relocations. It's not like they're
6254 useful, after all. This should be a bit more efficient
6255 as well. */
6256 /* ??? Although this behavior is compatible with glibc's ld.so,
6257 the ABI says that relocations against STN_UNDEF should have
6258 a symbol value of 0. Irix rld honors this, so relocations
6259 against STN_UNDEF have no effect. */
6260 if (!SGI_COMPAT (output_bfd))
6261 indx = 0;
6262 defined_p = TRUE;
b49e97c9
TS
6263 }
6264
5d41f0b6
RS
6265 /* If the relocation was previously an absolute relocation and
6266 this symbol will not be referred to by the relocation, we must
6267 adjust it by the value we give it in the dynamic symbol table.
6268 Otherwise leave the job up to the dynamic linker. */
6269 if (defined_p && r_type != R_MIPS_REL32)
6270 *addendp += symbol;
6271
0a44bf69
RS
6272 if (htab->is_vxworks)
6273 /* VxWorks uses non-relative relocations for this. */
6274 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6275 else
6276 /* The relocation is always an REL32 relocation because we don't
6277 know where the shared library will wind up at load-time. */
6278 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6279 R_MIPS_REL32);
6280
5d41f0b6
RS
6281 /* For strict adherence to the ABI specification, we should
6282 generate a R_MIPS_64 relocation record by itself before the
6283 _REL32/_64 record as well, such that the addend is read in as
6284 a 64-bit value (REL32 is a 32-bit relocation, after all).
6285 However, since none of the existing ELF64 MIPS dynamic
6286 loaders seems to care, we don't waste space with these
6287 artificial relocations. If this turns out to not be true,
6288 mips_elf_allocate_dynamic_relocation() should be tweaked so
6289 as to make room for a pair of dynamic relocations per
6290 invocation if ABI_64_P, and here we should generate an
6291 additional relocation record with R_MIPS_64 by itself for a
6292 NULL symbol before this relocation record. */
6293 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6294 ABI_64_P (output_bfd)
6295 ? R_MIPS_64
6296 : R_MIPS_NONE);
6297 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6298
6299 /* Adjust the output offset of the relocation to reference the
6300 correct location in the output file. */
6301 outrel[0].r_offset += (input_section->output_section->vma
6302 + input_section->output_offset);
6303 outrel[1].r_offset += (input_section->output_section->vma
6304 + input_section->output_offset);
6305 outrel[2].r_offset += (input_section->output_section->vma
6306 + input_section->output_offset);
6307
b49e97c9
TS
6308 /* Put the relocation back out. We have to use the special
6309 relocation outputter in the 64-bit case since the 64-bit
6310 relocation format is non-standard. */
6311 if (ABI_64_P (output_bfd))
6312 {
6313 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6314 (output_bfd, &outrel[0],
6315 (sreloc->contents
6316 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6317 }
0a44bf69
RS
6318 else if (htab->is_vxworks)
6319 {
6320 /* VxWorks uses RELA rather than REL dynamic relocations. */
6321 outrel[0].r_addend = *addendp;
6322 bfd_elf32_swap_reloca_out
6323 (output_bfd, &outrel[0],
6324 (sreloc->contents
6325 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6326 }
b49e97c9 6327 else
947216bf
AM
6328 bfd_elf32_swap_reloc_out
6329 (output_bfd, &outrel[0],
6330 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 6331
b49e97c9
TS
6332 /* We've now added another relocation. */
6333 ++sreloc->reloc_count;
6334
6335 /* Make sure the output section is writable. The dynamic linker
6336 will be writing to it. */
6337 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6338 |= SHF_WRITE;
6339
6340 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 6341 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9 6342 {
3d4d4302 6343 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
6344 bfd_byte *cr;
6345
6346 if (scpt)
6347 {
6348 Elf32_crinfo cptrel;
6349
6350 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6351 cptrel.vaddr = (rel->r_offset
6352 + input_section->output_section->vma
6353 + input_section->output_offset);
6354 if (r_type == R_MIPS_REL32)
6355 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6356 else
6357 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6358 mips_elf_set_cr_dist2to (cptrel, 0);
6359 cptrel.konst = *addendp;
6360
6361 cr = (scpt->contents
6362 + sizeof (Elf32_External_compact_rel));
abc0f8d0 6363 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
6364 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6365 ((Elf32_External_crinfo *) cr
6366 + scpt->reloc_count));
6367 ++scpt->reloc_count;
6368 }
6369 }
6370
943284cc
DJ
6371 /* If we've written this relocation for a readonly section,
6372 we need to set DF_TEXTREL again, so that we do not delete the
6373 DT_TEXTREL tag. */
6374 if (MIPS_ELF_READONLY_SECTION (input_section))
6375 info->flags |= DF_TEXTREL;
6376
b34976b6 6377 return TRUE;
b49e97c9
TS
6378}
6379\f
b49e97c9
TS
6380/* Return the MACH for a MIPS e_flags value. */
6381
6382unsigned long
9719ad41 6383_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
6384{
6385 switch (flags & EF_MIPS_MACH)
6386 {
6387 case E_MIPS_MACH_3900:
6388 return bfd_mach_mips3900;
6389
6390 case E_MIPS_MACH_4010:
6391 return bfd_mach_mips4010;
6392
6393 case E_MIPS_MACH_4100:
6394 return bfd_mach_mips4100;
6395
6396 case E_MIPS_MACH_4111:
6397 return bfd_mach_mips4111;
6398
00707a0e
RS
6399 case E_MIPS_MACH_4120:
6400 return bfd_mach_mips4120;
6401
b49e97c9
TS
6402 case E_MIPS_MACH_4650:
6403 return bfd_mach_mips4650;
6404
00707a0e
RS
6405 case E_MIPS_MACH_5400:
6406 return bfd_mach_mips5400;
6407
6408 case E_MIPS_MACH_5500:
6409 return bfd_mach_mips5500;
6410
e407c74b
NC
6411 case E_MIPS_MACH_5900:
6412 return bfd_mach_mips5900;
6413
0d2e43ed
ILT
6414 case E_MIPS_MACH_9000:
6415 return bfd_mach_mips9000;
6416
b49e97c9
TS
6417 case E_MIPS_MACH_SB1:
6418 return bfd_mach_mips_sb1;
6419
350cc38d
MS
6420 case E_MIPS_MACH_LS2E:
6421 return bfd_mach_mips_loongson_2e;
6422
6423 case E_MIPS_MACH_LS2F:
6424 return bfd_mach_mips_loongson_2f;
6425
fd503541
NC
6426 case E_MIPS_MACH_LS3A:
6427 return bfd_mach_mips_loongson_3a;
6428
432233b3
AP
6429 case E_MIPS_MACH_OCTEON2:
6430 return bfd_mach_mips_octeon2;
6431
6f179bd0
AN
6432 case E_MIPS_MACH_OCTEON:
6433 return bfd_mach_mips_octeon;
6434
52b6b6b9
JM
6435 case E_MIPS_MACH_XLR:
6436 return bfd_mach_mips_xlr;
6437
b49e97c9
TS
6438 default:
6439 switch (flags & EF_MIPS_ARCH)
6440 {
6441 default:
6442 case E_MIPS_ARCH_1:
6443 return bfd_mach_mips3000;
b49e97c9
TS
6444
6445 case E_MIPS_ARCH_2:
6446 return bfd_mach_mips6000;
b49e97c9
TS
6447
6448 case E_MIPS_ARCH_3:
6449 return bfd_mach_mips4000;
b49e97c9
TS
6450
6451 case E_MIPS_ARCH_4:
6452 return bfd_mach_mips8000;
b49e97c9
TS
6453
6454 case E_MIPS_ARCH_5:
6455 return bfd_mach_mips5;
b49e97c9
TS
6456
6457 case E_MIPS_ARCH_32:
6458 return bfd_mach_mipsisa32;
b49e97c9
TS
6459
6460 case E_MIPS_ARCH_64:
6461 return bfd_mach_mipsisa64;
af7ee8bf
CD
6462
6463 case E_MIPS_ARCH_32R2:
6464 return bfd_mach_mipsisa32r2;
5f74bc13
CD
6465
6466 case E_MIPS_ARCH_64R2:
6467 return bfd_mach_mipsisa64r2;
b49e97c9
TS
6468 }
6469 }
6470
6471 return 0;
6472}
6473
6474/* Return printable name for ABI. */
6475
6476static INLINE char *
9719ad41 6477elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
6478{
6479 flagword flags;
6480
6481 flags = elf_elfheader (abfd)->e_flags;
6482 switch (flags & EF_MIPS_ABI)
6483 {
6484 case 0:
6485 if (ABI_N32_P (abfd))
6486 return "N32";
6487 else if (ABI_64_P (abfd))
6488 return "64";
6489 else
6490 return "none";
6491 case E_MIPS_ABI_O32:
6492 return "O32";
6493 case E_MIPS_ABI_O64:
6494 return "O64";
6495 case E_MIPS_ABI_EABI32:
6496 return "EABI32";
6497 case E_MIPS_ABI_EABI64:
6498 return "EABI64";
6499 default:
6500 return "unknown abi";
6501 }
6502}
6503\f
6504/* MIPS ELF uses two common sections. One is the usual one, and the
6505 other is for small objects. All the small objects are kept
6506 together, and then referenced via the gp pointer, which yields
6507 faster assembler code. This is what we use for the small common
6508 section. This approach is copied from ecoff.c. */
6509static asection mips_elf_scom_section;
6510static asymbol mips_elf_scom_symbol;
6511static asymbol *mips_elf_scom_symbol_ptr;
6512
6513/* MIPS ELF also uses an acommon section, which represents an
6514 allocated common symbol which may be overridden by a
6515 definition in a shared library. */
6516static asection mips_elf_acom_section;
6517static asymbol mips_elf_acom_symbol;
6518static asymbol *mips_elf_acom_symbol_ptr;
6519
738e5348 6520/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
6521
6522void
9719ad41 6523_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
6524{
6525 elf_symbol_type *elfsym;
6526
738e5348 6527 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6528 elfsym = (elf_symbol_type *) asym;
6529 switch (elfsym->internal_elf_sym.st_shndx)
6530 {
6531 case SHN_MIPS_ACOMMON:
6532 /* This section is used in a dynamically linked executable file.
6533 It is an allocated common section. The dynamic linker can
6534 either resolve these symbols to something in a shared
6535 library, or it can just leave them here. For our purposes,
6536 we can consider these symbols to be in a new section. */
6537 if (mips_elf_acom_section.name == NULL)
6538 {
6539 /* Initialize the acommon section. */
6540 mips_elf_acom_section.name = ".acommon";
6541 mips_elf_acom_section.flags = SEC_ALLOC;
6542 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6543 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6544 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6545 mips_elf_acom_symbol.name = ".acommon";
6546 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6547 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6548 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6549 }
6550 asym->section = &mips_elf_acom_section;
6551 break;
6552
6553 case SHN_COMMON:
6554 /* Common symbols less than the GP size are automatically
6555 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6556 if (asym->value > elf_gp_size (abfd)
b59eed79 6557 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6558 || IRIX_COMPAT (abfd) == ict_irix6)
6559 break;
6560 /* Fall through. */
6561 case SHN_MIPS_SCOMMON:
6562 if (mips_elf_scom_section.name == NULL)
6563 {
6564 /* Initialize the small common section. */
6565 mips_elf_scom_section.name = ".scommon";
6566 mips_elf_scom_section.flags = SEC_IS_COMMON;
6567 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6568 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6569 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6570 mips_elf_scom_symbol.name = ".scommon";
6571 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6572 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6573 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6574 }
6575 asym->section = &mips_elf_scom_section;
6576 asym->value = elfsym->internal_elf_sym.st_size;
6577 break;
6578
6579 case SHN_MIPS_SUNDEFINED:
6580 asym->section = bfd_und_section_ptr;
6581 break;
6582
b49e97c9 6583 case SHN_MIPS_TEXT:
00b4930b
TS
6584 {
6585 asection *section = bfd_get_section_by_name (abfd, ".text");
6586
00b4930b
TS
6587 if (section != NULL)
6588 {
6589 asym->section = section;
6590 /* MIPS_TEXT is a bit special, the address is not an offset
6591 to the base of the .text section. So substract the section
6592 base address to make it an offset. */
6593 asym->value -= section->vma;
6594 }
6595 }
b49e97c9
TS
6596 break;
6597
6598 case SHN_MIPS_DATA:
00b4930b
TS
6599 {
6600 asection *section = bfd_get_section_by_name (abfd, ".data");
6601
00b4930b
TS
6602 if (section != NULL)
6603 {
6604 asym->section = section;
6605 /* MIPS_DATA is a bit special, the address is not an offset
6606 to the base of the .data section. So substract the section
6607 base address to make it an offset. */
6608 asym->value -= section->vma;
6609 }
6610 }
b49e97c9 6611 break;
b49e97c9 6612 }
738e5348 6613
df58fc94
RS
6614 /* If this is an odd-valued function symbol, assume it's a MIPS16
6615 or microMIPS one. */
738e5348
RS
6616 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6617 && (asym->value & 1) != 0)
6618 {
6619 asym->value--;
e8faf7d1 6620 if (MICROMIPS_P (abfd))
df58fc94
RS
6621 elfsym->internal_elf_sym.st_other
6622 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6623 else
6624 elfsym->internal_elf_sym.st_other
6625 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
738e5348 6626 }
b49e97c9
TS
6627}
6628\f
8c946ed5
RS
6629/* Implement elf_backend_eh_frame_address_size. This differs from
6630 the default in the way it handles EABI64.
6631
6632 EABI64 was originally specified as an LP64 ABI, and that is what
6633 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6634 historically accepted the combination of -mabi=eabi and -mlong32,
6635 and this ILP32 variation has become semi-official over time.
6636 Both forms use elf32 and have pointer-sized FDE addresses.
6637
6638 If an EABI object was generated by GCC 4.0 or above, it will have
6639 an empty .gcc_compiled_longXX section, where XX is the size of longs
6640 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6641 have no special marking to distinguish them from LP64 objects.
6642
6643 We don't want users of the official LP64 ABI to be punished for the
6644 existence of the ILP32 variant, but at the same time, we don't want
6645 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6646 We therefore take the following approach:
6647
6648 - If ABFD contains a .gcc_compiled_longXX section, use it to
6649 determine the pointer size.
6650
6651 - Otherwise check the type of the first relocation. Assume that
6652 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6653
6654 - Otherwise punt.
6655
6656 The second check is enough to detect LP64 objects generated by pre-4.0
6657 compilers because, in the kind of output generated by those compilers,
6658 the first relocation will be associated with either a CIE personality
6659 routine or an FDE start address. Furthermore, the compilers never
6660 used a special (non-pointer) encoding for this ABI.
6661
6662 Checking the relocation type should also be safe because there is no
6663 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6664 did so. */
6665
6666unsigned int
6667_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6668{
6669 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6670 return 8;
6671 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6672 {
6673 bfd_boolean long32_p, long64_p;
6674
6675 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6676 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6677 if (long32_p && long64_p)
6678 return 0;
6679 if (long32_p)
6680 return 4;
6681 if (long64_p)
6682 return 8;
6683
6684 if (sec->reloc_count > 0
6685 && elf_section_data (sec)->relocs != NULL
6686 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6687 == R_MIPS_64))
6688 return 8;
6689
6690 return 0;
6691 }
6692 return 4;
6693}
6694\f
174fd7f9
RS
6695/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6696 relocations against two unnamed section symbols to resolve to the
6697 same address. For example, if we have code like:
6698
6699 lw $4,%got_disp(.data)($gp)
6700 lw $25,%got_disp(.text)($gp)
6701 jalr $25
6702
6703 then the linker will resolve both relocations to .data and the program
6704 will jump there rather than to .text.
6705
6706 We can work around this problem by giving names to local section symbols.
6707 This is also what the MIPSpro tools do. */
6708
6709bfd_boolean
6710_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6711{
6712 return SGI_COMPAT (abfd);
6713}
6714\f
b49e97c9
TS
6715/* Work over a section just before writing it out. This routine is
6716 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6717 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6718 a better way. */
6719
b34976b6 6720bfd_boolean
9719ad41 6721_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6722{
6723 if (hdr->sh_type == SHT_MIPS_REGINFO
6724 && hdr->sh_size > 0)
6725 {
6726 bfd_byte buf[4];
6727
6728 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6729 BFD_ASSERT (hdr->contents == NULL);
6730
6731 if (bfd_seek (abfd,
6732 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6733 SEEK_SET) != 0)
b34976b6 6734 return FALSE;
b49e97c9 6735 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6736 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6737 return FALSE;
b49e97c9
TS
6738 }
6739
6740 if (hdr->sh_type == SHT_MIPS_OPTIONS
6741 && hdr->bfd_section != NULL
f0abc2a1
AM
6742 && mips_elf_section_data (hdr->bfd_section) != NULL
6743 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6744 {
6745 bfd_byte *contents, *l, *lend;
6746
f0abc2a1
AM
6747 /* We stored the section contents in the tdata field in the
6748 set_section_contents routine. We save the section contents
6749 so that we don't have to read them again.
b49e97c9
TS
6750 At this point we know that elf_gp is set, so we can look
6751 through the section contents to see if there is an
6752 ODK_REGINFO structure. */
6753
f0abc2a1 6754 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6755 l = contents;
6756 lend = contents + hdr->sh_size;
6757 while (l + sizeof (Elf_External_Options) <= lend)
6758 {
6759 Elf_Internal_Options intopt;
6760
6761 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6762 &intopt);
1bc8074d
MR
6763 if (intopt.size < sizeof (Elf_External_Options))
6764 {
6765 (*_bfd_error_handler)
6766 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6767 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6768 break;
6769 }
b49e97c9
TS
6770 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6771 {
6772 bfd_byte buf[8];
6773
6774 if (bfd_seek (abfd,
6775 (hdr->sh_offset
6776 + (l - contents)
6777 + sizeof (Elf_External_Options)
6778 + (sizeof (Elf64_External_RegInfo) - 8)),
6779 SEEK_SET) != 0)
b34976b6 6780 return FALSE;
b49e97c9 6781 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6782 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6783 return FALSE;
b49e97c9
TS
6784 }
6785 else if (intopt.kind == ODK_REGINFO)
6786 {
6787 bfd_byte buf[4];
6788
6789 if (bfd_seek (abfd,
6790 (hdr->sh_offset
6791 + (l - contents)
6792 + sizeof (Elf_External_Options)
6793 + (sizeof (Elf32_External_RegInfo) - 4)),
6794 SEEK_SET) != 0)
b34976b6 6795 return FALSE;
b49e97c9 6796 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6797 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6798 return FALSE;
b49e97c9
TS
6799 }
6800 l += intopt.size;
6801 }
6802 }
6803
6804 if (hdr->bfd_section != NULL)
6805 {
6806 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6807
2d0f9ad9
JM
6808 /* .sbss is not handled specially here because the GNU/Linux
6809 prelinker can convert .sbss from NOBITS to PROGBITS and
6810 changing it back to NOBITS breaks the binary. The entry in
6811 _bfd_mips_elf_special_sections will ensure the correct flags
6812 are set on .sbss if BFD creates it without reading it from an
6813 input file, and without special handling here the flags set
6814 on it in an input file will be followed. */
b49e97c9
TS
6815 if (strcmp (name, ".sdata") == 0
6816 || strcmp (name, ".lit8") == 0
6817 || strcmp (name, ".lit4") == 0)
6818 {
6819 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6820 hdr->sh_type = SHT_PROGBITS;
6821 }
b49e97c9
TS
6822 else if (strcmp (name, ".srdata") == 0)
6823 {
6824 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6825 hdr->sh_type = SHT_PROGBITS;
6826 }
6827 else if (strcmp (name, ".compact_rel") == 0)
6828 {
6829 hdr->sh_flags = 0;
6830 hdr->sh_type = SHT_PROGBITS;
6831 }
6832 else if (strcmp (name, ".rtproc") == 0)
6833 {
6834 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6835 {
6836 unsigned int adjust;
6837
6838 adjust = hdr->sh_size % hdr->sh_addralign;
6839 if (adjust != 0)
6840 hdr->sh_size += hdr->sh_addralign - adjust;
6841 }
6842 }
6843 }
6844
b34976b6 6845 return TRUE;
b49e97c9
TS
6846}
6847
6848/* Handle a MIPS specific section when reading an object file. This
6849 is called when elfcode.h finds a section with an unknown type.
6850 This routine supports both the 32-bit and 64-bit ELF ABI.
6851
6852 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6853 how to. */
6854
b34976b6 6855bfd_boolean
6dc132d9
L
6856_bfd_mips_elf_section_from_shdr (bfd *abfd,
6857 Elf_Internal_Shdr *hdr,
6858 const char *name,
6859 int shindex)
b49e97c9
TS
6860{
6861 flagword flags = 0;
6862
6863 /* There ought to be a place to keep ELF backend specific flags, but
6864 at the moment there isn't one. We just keep track of the
6865 sections by their name, instead. Fortunately, the ABI gives
6866 suggested names for all the MIPS specific sections, so we will
6867 probably get away with this. */
6868 switch (hdr->sh_type)
6869 {
6870 case SHT_MIPS_LIBLIST:
6871 if (strcmp (name, ".liblist") != 0)
b34976b6 6872 return FALSE;
b49e97c9
TS
6873 break;
6874 case SHT_MIPS_MSYM:
6875 if (strcmp (name, ".msym") != 0)
b34976b6 6876 return FALSE;
b49e97c9
TS
6877 break;
6878 case SHT_MIPS_CONFLICT:
6879 if (strcmp (name, ".conflict") != 0)
b34976b6 6880 return FALSE;
b49e97c9
TS
6881 break;
6882 case SHT_MIPS_GPTAB:
0112cd26 6883 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6884 return FALSE;
b49e97c9
TS
6885 break;
6886 case SHT_MIPS_UCODE:
6887 if (strcmp (name, ".ucode") != 0)
b34976b6 6888 return FALSE;
b49e97c9
TS
6889 break;
6890 case SHT_MIPS_DEBUG:
6891 if (strcmp (name, ".mdebug") != 0)
b34976b6 6892 return FALSE;
b49e97c9
TS
6893 flags = SEC_DEBUGGING;
6894 break;
6895 case SHT_MIPS_REGINFO:
6896 if (strcmp (name, ".reginfo") != 0
6897 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6898 return FALSE;
b49e97c9
TS
6899 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6900 break;
6901 case SHT_MIPS_IFACE:
6902 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6903 return FALSE;
b49e97c9
TS
6904 break;
6905 case SHT_MIPS_CONTENT:
0112cd26 6906 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6907 return FALSE;
b49e97c9
TS
6908 break;
6909 case SHT_MIPS_OPTIONS:
cc2e31b9 6910 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6911 return FALSE;
b49e97c9
TS
6912 break;
6913 case SHT_MIPS_DWARF:
1b315056 6914 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6915 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6916 return FALSE;
b49e97c9
TS
6917 break;
6918 case SHT_MIPS_SYMBOL_LIB:
6919 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6920 return FALSE;
b49e97c9
TS
6921 break;
6922 case SHT_MIPS_EVENTS:
0112cd26
NC
6923 if (! CONST_STRNEQ (name, ".MIPS.events")
6924 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6925 return FALSE;
b49e97c9
TS
6926 break;
6927 default:
cc2e31b9 6928 break;
b49e97c9
TS
6929 }
6930
6dc132d9 6931 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6932 return FALSE;
b49e97c9
TS
6933
6934 if (flags)
6935 {
6936 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6937 (bfd_get_section_flags (abfd,
6938 hdr->bfd_section)
6939 | flags)))
b34976b6 6940 return FALSE;
b49e97c9
TS
6941 }
6942
6943 /* FIXME: We should record sh_info for a .gptab section. */
6944
6945 /* For a .reginfo section, set the gp value in the tdata information
6946 from the contents of this section. We need the gp value while
6947 processing relocs, so we just get it now. The .reginfo section
6948 is not used in the 64-bit MIPS ELF ABI. */
6949 if (hdr->sh_type == SHT_MIPS_REGINFO)
6950 {
6951 Elf32_External_RegInfo ext;
6952 Elf32_RegInfo s;
6953
9719ad41
RS
6954 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6955 &ext, 0, sizeof ext))
b34976b6 6956 return FALSE;
b49e97c9
TS
6957 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6958 elf_gp (abfd) = s.ri_gp_value;
6959 }
6960
6961 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6962 set the gp value based on what we find. We may see both
6963 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6964 they should agree. */
6965 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6966 {
6967 bfd_byte *contents, *l, *lend;
6968
9719ad41 6969 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6970 if (contents == NULL)
b34976b6 6971 return FALSE;
b49e97c9 6972 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6973 0, hdr->sh_size))
b49e97c9
TS
6974 {
6975 free (contents);
b34976b6 6976 return FALSE;
b49e97c9
TS
6977 }
6978 l = contents;
6979 lend = contents + hdr->sh_size;
6980 while (l + sizeof (Elf_External_Options) <= lend)
6981 {
6982 Elf_Internal_Options intopt;
6983
6984 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6985 &intopt);
1bc8074d
MR
6986 if (intopt.size < sizeof (Elf_External_Options))
6987 {
6988 (*_bfd_error_handler)
6989 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6990 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6991 break;
6992 }
b49e97c9
TS
6993 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6994 {
6995 Elf64_Internal_RegInfo intreg;
6996
6997 bfd_mips_elf64_swap_reginfo_in
6998 (abfd,
6999 ((Elf64_External_RegInfo *)
7000 (l + sizeof (Elf_External_Options))),
7001 &intreg);
7002 elf_gp (abfd) = intreg.ri_gp_value;
7003 }
7004 else if (intopt.kind == ODK_REGINFO)
7005 {
7006 Elf32_RegInfo intreg;
7007
7008 bfd_mips_elf32_swap_reginfo_in
7009 (abfd,
7010 ((Elf32_External_RegInfo *)
7011 (l + sizeof (Elf_External_Options))),
7012 &intreg);
7013 elf_gp (abfd) = intreg.ri_gp_value;
7014 }
7015 l += intopt.size;
7016 }
7017 free (contents);
7018 }
7019
b34976b6 7020 return TRUE;
b49e97c9
TS
7021}
7022
7023/* Set the correct type for a MIPS ELF section. We do this by the
7024 section name, which is a hack, but ought to work. This routine is
7025 used by both the 32-bit and the 64-bit ABI. */
7026
b34976b6 7027bfd_boolean
9719ad41 7028_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 7029{
0414f35b 7030 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
7031
7032 if (strcmp (name, ".liblist") == 0)
7033 {
7034 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 7035 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
7036 /* The sh_link field is set in final_write_processing. */
7037 }
7038 else if (strcmp (name, ".conflict") == 0)
7039 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 7040 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
7041 {
7042 hdr->sh_type = SHT_MIPS_GPTAB;
7043 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7044 /* The sh_info field is set in final_write_processing. */
7045 }
7046 else if (strcmp (name, ".ucode") == 0)
7047 hdr->sh_type = SHT_MIPS_UCODE;
7048 else if (strcmp (name, ".mdebug") == 0)
7049 {
7050 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 7051 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
7052 entsize of 0. FIXME: Does this matter? */
7053 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7054 hdr->sh_entsize = 0;
7055 else
7056 hdr->sh_entsize = 1;
7057 }
7058 else if (strcmp (name, ".reginfo") == 0)
7059 {
7060 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 7061 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
7062 entsize of 0x18. FIXME: Does this matter? */
7063 if (SGI_COMPAT (abfd))
7064 {
7065 if ((abfd->flags & DYNAMIC) != 0)
7066 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7067 else
7068 hdr->sh_entsize = 1;
7069 }
7070 else
7071 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7072 }
7073 else if (SGI_COMPAT (abfd)
7074 && (strcmp (name, ".hash") == 0
7075 || strcmp (name, ".dynamic") == 0
7076 || strcmp (name, ".dynstr") == 0))
7077 {
7078 if (SGI_COMPAT (abfd))
7079 hdr->sh_entsize = 0;
7080#if 0
8dc1a139 7081 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
7082 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7083#endif
7084 }
7085 else if (strcmp (name, ".got") == 0
7086 || strcmp (name, ".srdata") == 0
7087 || strcmp (name, ".sdata") == 0
7088 || strcmp (name, ".sbss") == 0
7089 || strcmp (name, ".lit4") == 0
7090 || strcmp (name, ".lit8") == 0)
7091 hdr->sh_flags |= SHF_MIPS_GPREL;
7092 else if (strcmp (name, ".MIPS.interfaces") == 0)
7093 {
7094 hdr->sh_type = SHT_MIPS_IFACE;
7095 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7096 }
0112cd26 7097 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
7098 {
7099 hdr->sh_type = SHT_MIPS_CONTENT;
7100 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7101 /* The sh_info field is set in final_write_processing. */
7102 }
cc2e31b9 7103 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
7104 {
7105 hdr->sh_type = SHT_MIPS_OPTIONS;
7106 hdr->sh_entsize = 1;
7107 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7108 }
1b315056
CS
7109 else if (CONST_STRNEQ (name, ".debug_")
7110 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
7111 {
7112 hdr->sh_type = SHT_MIPS_DWARF;
7113
7114 /* Irix facilities such as libexc expect a single .debug_frame
7115 per executable, the system ones have NOSTRIP set and the linker
7116 doesn't merge sections with different flags so ... */
7117 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7118 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7119 }
b49e97c9
TS
7120 else if (strcmp (name, ".MIPS.symlib") == 0)
7121 {
7122 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7123 /* The sh_link and sh_info fields are set in
7124 final_write_processing. */
7125 }
0112cd26
NC
7126 else if (CONST_STRNEQ (name, ".MIPS.events")
7127 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
7128 {
7129 hdr->sh_type = SHT_MIPS_EVENTS;
7130 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7131 /* The sh_link field is set in final_write_processing. */
7132 }
7133 else if (strcmp (name, ".msym") == 0)
7134 {
7135 hdr->sh_type = SHT_MIPS_MSYM;
7136 hdr->sh_flags |= SHF_ALLOC;
7137 hdr->sh_entsize = 8;
7138 }
7139
7a79a000
TS
7140 /* The generic elf_fake_sections will set up REL_HDR using the default
7141 kind of relocations. We used to set up a second header for the
7142 non-default kind of relocations here, but only NewABI would use
7143 these, and the IRIX ld doesn't like resulting empty RELA sections.
7144 Thus we create those header only on demand now. */
b49e97c9 7145
b34976b6 7146 return TRUE;
b49e97c9
TS
7147}
7148
7149/* Given a BFD section, try to locate the corresponding ELF section
7150 index. This is used by both the 32-bit and the 64-bit ABI.
7151 Actually, it's not clear to me that the 64-bit ABI supports these,
7152 but for non-PIC objects we will certainly want support for at least
7153 the .scommon section. */
7154
b34976b6 7155bfd_boolean
9719ad41
RS
7156_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7157 asection *sec, int *retval)
b49e97c9
TS
7158{
7159 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7160 {
7161 *retval = SHN_MIPS_SCOMMON;
b34976b6 7162 return TRUE;
b49e97c9
TS
7163 }
7164 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7165 {
7166 *retval = SHN_MIPS_ACOMMON;
b34976b6 7167 return TRUE;
b49e97c9 7168 }
b34976b6 7169 return FALSE;
b49e97c9
TS
7170}
7171\f
7172/* Hook called by the linker routine which adds symbols from an object
7173 file. We must handle the special MIPS section numbers here. */
7174
b34976b6 7175bfd_boolean
9719ad41 7176_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 7177 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
7178 flagword *flagsp ATTRIBUTE_UNUSED,
7179 asection **secp, bfd_vma *valp)
b49e97c9
TS
7180{
7181 if (SGI_COMPAT (abfd)
7182 && (abfd->flags & DYNAMIC) != 0
7183 && strcmp (*namep, "_rld_new_interface") == 0)
7184 {
8dc1a139 7185 /* Skip IRIX5 rld entry name. */
b49e97c9 7186 *namep = NULL;
b34976b6 7187 return TRUE;
b49e97c9
TS
7188 }
7189
eedecc07
DD
7190 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7191 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7192 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7193 a magic symbol resolved by the linker, we ignore this bogus definition
7194 of _gp_disp. New ABI objects do not suffer from this problem so this
7195 is not done for them. */
7196 if (!NEWABI_P(abfd)
7197 && (sym->st_shndx == SHN_ABS)
7198 && (strcmp (*namep, "_gp_disp") == 0))
7199 {
7200 *namep = NULL;
7201 return TRUE;
7202 }
7203
b49e97c9
TS
7204 switch (sym->st_shndx)
7205 {
7206 case SHN_COMMON:
7207 /* Common symbols less than the GP size are automatically
7208 treated as SHN_MIPS_SCOMMON symbols. */
7209 if (sym->st_size > elf_gp_size (abfd)
b59eed79 7210 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
7211 || IRIX_COMPAT (abfd) == ict_irix6)
7212 break;
7213 /* Fall through. */
7214 case SHN_MIPS_SCOMMON:
7215 *secp = bfd_make_section_old_way (abfd, ".scommon");
7216 (*secp)->flags |= SEC_IS_COMMON;
7217 *valp = sym->st_size;
7218 break;
7219
7220 case SHN_MIPS_TEXT:
7221 /* This section is used in a shared object. */
698600e4 7222 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
b49e97c9
TS
7223 {
7224 asymbol *elf_text_symbol;
7225 asection *elf_text_section;
7226 bfd_size_type amt = sizeof (asection);
7227
7228 elf_text_section = bfd_zalloc (abfd, amt);
7229 if (elf_text_section == NULL)
b34976b6 7230 return FALSE;
b49e97c9
TS
7231
7232 amt = sizeof (asymbol);
7233 elf_text_symbol = bfd_zalloc (abfd, amt);
7234 if (elf_text_symbol == NULL)
b34976b6 7235 return FALSE;
b49e97c9
TS
7236
7237 /* Initialize the section. */
7238
698600e4
AM
7239 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7240 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
b49e97c9
TS
7241
7242 elf_text_section->symbol = elf_text_symbol;
698600e4 7243 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
b49e97c9
TS
7244
7245 elf_text_section->name = ".text";
7246 elf_text_section->flags = SEC_NO_FLAGS;
7247 elf_text_section->output_section = NULL;
7248 elf_text_section->owner = abfd;
7249 elf_text_symbol->name = ".text";
7250 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7251 elf_text_symbol->section = elf_text_section;
7252 }
7253 /* This code used to do *secp = bfd_und_section_ptr if
7254 info->shared. I don't know why, and that doesn't make sense,
7255 so I took it out. */
698600e4 7256 *secp = mips_elf_tdata (abfd)->elf_text_section;
b49e97c9
TS
7257 break;
7258
7259 case SHN_MIPS_ACOMMON:
7260 /* Fall through. XXX Can we treat this as allocated data? */
7261 case SHN_MIPS_DATA:
7262 /* This section is used in a shared object. */
698600e4 7263 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
b49e97c9
TS
7264 {
7265 asymbol *elf_data_symbol;
7266 asection *elf_data_section;
7267 bfd_size_type amt = sizeof (asection);
7268
7269 elf_data_section = bfd_zalloc (abfd, amt);
7270 if (elf_data_section == NULL)
b34976b6 7271 return FALSE;
b49e97c9
TS
7272
7273 amt = sizeof (asymbol);
7274 elf_data_symbol = bfd_zalloc (abfd, amt);
7275 if (elf_data_symbol == NULL)
b34976b6 7276 return FALSE;
b49e97c9
TS
7277
7278 /* Initialize the section. */
7279
698600e4
AM
7280 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7281 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
b49e97c9
TS
7282
7283 elf_data_section->symbol = elf_data_symbol;
698600e4 7284 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
b49e97c9
TS
7285
7286 elf_data_section->name = ".data";
7287 elf_data_section->flags = SEC_NO_FLAGS;
7288 elf_data_section->output_section = NULL;
7289 elf_data_section->owner = abfd;
7290 elf_data_symbol->name = ".data";
7291 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7292 elf_data_symbol->section = elf_data_section;
7293 }
7294 /* This code used to do *secp = bfd_und_section_ptr if
7295 info->shared. I don't know why, and that doesn't make sense,
7296 so I took it out. */
698600e4 7297 *secp = mips_elf_tdata (abfd)->elf_data_section;
b49e97c9
TS
7298 break;
7299
7300 case SHN_MIPS_SUNDEFINED:
7301 *secp = bfd_und_section_ptr;
7302 break;
7303 }
7304
7305 if (SGI_COMPAT (abfd)
7306 && ! info->shared
f13a99db 7307 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
7308 && strcmp (*namep, "__rld_obj_head") == 0)
7309 {
7310 struct elf_link_hash_entry *h;
14a793b2 7311 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7312
7313 /* Mark __rld_obj_head as dynamic. */
14a793b2 7314 bh = NULL;
b49e97c9 7315 if (! (_bfd_generic_link_add_one_symbol
9719ad41 7316 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 7317 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7318 return FALSE;
14a793b2
AM
7319
7320 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7321 h->non_elf = 0;
7322 h->def_regular = 1;
b49e97c9
TS
7323 h->type = STT_OBJECT;
7324
c152c796 7325 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7326 return FALSE;
b49e97c9 7327
b34976b6 7328 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b4082c70 7329 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7330 }
7331
7332 /* If this is a mips16 text symbol, add 1 to the value to make it
7333 odd. This will cause something like .word SYM to come up with
7334 the right value when it is loaded into the PC. */
df58fc94 7335 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
7336 ++*valp;
7337
b34976b6 7338 return TRUE;
b49e97c9
TS
7339}
7340
7341/* This hook function is called before the linker writes out a global
7342 symbol. We mark symbols as small common if appropriate. This is
7343 also where we undo the increment of the value for a mips16 symbol. */
7344
6e0b88f1 7345int
9719ad41
RS
7346_bfd_mips_elf_link_output_symbol_hook
7347 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7348 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7349 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
7350{
7351 /* If we see a common symbol, which implies a relocatable link, then
7352 if a symbol was small common in an input file, mark it as small
7353 common in the output file. */
7354 if (sym->st_shndx == SHN_COMMON
7355 && strcmp (input_sec->name, ".scommon") == 0)
7356 sym->st_shndx = SHN_MIPS_SCOMMON;
7357
df58fc94 7358 if (ELF_ST_IS_COMPRESSED (sym->st_other))
79cda7cf 7359 sym->st_value &= ~1;
b49e97c9 7360
6e0b88f1 7361 return 1;
b49e97c9
TS
7362}
7363\f
7364/* Functions for the dynamic linker. */
7365
7366/* Create dynamic sections when linking against a dynamic object. */
7367
b34976b6 7368bfd_boolean
9719ad41 7369_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
7370{
7371 struct elf_link_hash_entry *h;
14a793b2 7372 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7373 flagword flags;
7374 register asection *s;
7375 const char * const *namep;
0a44bf69 7376 struct mips_elf_link_hash_table *htab;
b49e97c9 7377
0a44bf69 7378 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7379 BFD_ASSERT (htab != NULL);
7380
b49e97c9
TS
7381 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7382 | SEC_LINKER_CREATED | SEC_READONLY);
7383
0a44bf69
RS
7384 /* The psABI requires a read-only .dynamic section, but the VxWorks
7385 EABI doesn't. */
7386 if (!htab->is_vxworks)
b49e97c9 7387 {
3d4d4302 7388 s = bfd_get_linker_section (abfd, ".dynamic");
0a44bf69
RS
7389 if (s != NULL)
7390 {
7391 if (! bfd_set_section_flags (abfd, s, flags))
7392 return FALSE;
7393 }
b49e97c9
TS
7394 }
7395
7396 /* We need to create .got section. */
23cc69b6 7397 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
7398 return FALSE;
7399
0a44bf69 7400 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 7401 return FALSE;
b49e97c9 7402
b49e97c9 7403 /* Create .stub section. */
3d4d4302
AM
7404 s = bfd_make_section_anyway_with_flags (abfd,
7405 MIPS_ELF_STUB_SECTION_NAME (abfd),
7406 flags | SEC_CODE);
4e41d0d7
RS
7407 if (s == NULL
7408 || ! bfd_set_section_alignment (abfd, s,
7409 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7410 return FALSE;
7411 htab->sstubs = s;
b49e97c9 7412
e6aea42d 7413 if (!mips_elf_hash_table (info)->use_rld_obj_head
b49e97c9 7414 && !info->shared
3d4d4302 7415 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
b49e97c9 7416 {
3d4d4302
AM
7417 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7418 flags &~ (flagword) SEC_READONLY);
b49e97c9 7419 if (s == NULL
b49e97c9
TS
7420 || ! bfd_set_section_alignment (abfd, s,
7421 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 7422 return FALSE;
b49e97c9
TS
7423 }
7424
7425 /* On IRIX5, we adjust add some additional symbols and change the
7426 alignments of several sections. There is no ABI documentation
7427 indicating that this is necessary on IRIX6, nor any evidence that
7428 the linker takes such action. */
7429 if (IRIX_COMPAT (abfd) == ict_irix5)
7430 {
7431 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7432 {
14a793b2 7433 bh = NULL;
b49e97c9 7434 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
7435 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7436 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7437 return FALSE;
14a793b2
AM
7438
7439 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7440 h->non_elf = 0;
7441 h->def_regular = 1;
b49e97c9
TS
7442 h->type = STT_SECTION;
7443
c152c796 7444 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7445 return FALSE;
b49e97c9
TS
7446 }
7447
7448 /* We need to create a .compact_rel section. */
7449 if (SGI_COMPAT (abfd))
7450 {
7451 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 7452 return FALSE;
b49e97c9
TS
7453 }
7454
44c410de 7455 /* Change alignments of some sections. */
3d4d4302 7456 s = bfd_get_linker_section (abfd, ".hash");
b49e97c9 7457 if (s != NULL)
a253d456
NC
7458 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7459
3d4d4302 7460 s = bfd_get_linker_section (abfd, ".dynsym");
b49e97c9 7461 if (s != NULL)
a253d456
NC
7462 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7463
3d4d4302 7464 s = bfd_get_linker_section (abfd, ".dynstr");
b49e97c9 7465 if (s != NULL)
a253d456
NC
7466 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7467
3d4d4302 7468 /* ??? */
b49e97c9
TS
7469 s = bfd_get_section_by_name (abfd, ".reginfo");
7470 if (s != NULL)
a253d456
NC
7471 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7472
3d4d4302 7473 s = bfd_get_linker_section (abfd, ".dynamic");
b49e97c9 7474 if (s != NULL)
a253d456 7475 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7476 }
7477
7478 if (!info->shared)
7479 {
14a793b2
AM
7480 const char *name;
7481
7482 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7483 bh = NULL;
7484 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
7485 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7486 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7487 return FALSE;
14a793b2
AM
7488
7489 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7490 h->non_elf = 0;
7491 h->def_regular = 1;
b49e97c9
TS
7492 h->type = STT_SECTION;
7493
c152c796 7494 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7495 return FALSE;
b49e97c9
TS
7496
7497 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7498 {
7499 /* __rld_map is a four byte word located in the .data section
7500 and is filled in by the rtld to contain a pointer to
7501 the _r_debug structure. Its symbol value will be set in
7502 _bfd_mips_elf_finish_dynamic_symbol. */
3d4d4302 7503 s = bfd_get_linker_section (abfd, ".rld_map");
0abfb97a 7504 BFD_ASSERT (s != NULL);
14a793b2 7505
0abfb97a
L
7506 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7507 bh = NULL;
7508 if (!(_bfd_generic_link_add_one_symbol
7509 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7510 get_elf_backend_data (abfd)->collect, &bh)))
7511 return FALSE;
b49e97c9 7512
0abfb97a
L
7513 h = (struct elf_link_hash_entry *) bh;
7514 h->non_elf = 0;
7515 h->def_regular = 1;
7516 h->type = STT_OBJECT;
7517
7518 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7519 return FALSE;
b4082c70 7520 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7521 }
7522 }
7523
861fb55a 7524 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
c164a95d 7525 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
861fb55a
DJ
7526 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7527 return FALSE;
7528
7529 /* Cache the sections created above. */
3d4d4302
AM
7530 htab->splt = bfd_get_linker_section (abfd, ".plt");
7531 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
0a44bf69
RS
7532 if (htab->is_vxworks)
7533 {
3d4d4302
AM
7534 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7535 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
861fb55a
DJ
7536 }
7537 else
3d4d4302 7538 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
861fb55a
DJ
7539 if (!htab->sdynbss
7540 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7541 || !htab->srelplt
7542 || !htab->splt)
7543 abort ();
0a44bf69 7544
1bbce132
MR
7545 /* Do the usual VxWorks handling. */
7546 if (htab->is_vxworks
7547 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7548 return FALSE;
0a44bf69 7549
b34976b6 7550 return TRUE;
b49e97c9
TS
7551}
7552\f
c224138d
RS
7553/* Return true if relocation REL against section SEC is a REL rather than
7554 RELA relocation. RELOCS is the first relocation in the section and
7555 ABFD is the bfd that contains SEC. */
7556
7557static bfd_boolean
7558mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7559 const Elf_Internal_Rela *relocs,
7560 const Elf_Internal_Rela *rel)
7561{
7562 Elf_Internal_Shdr *rel_hdr;
7563 const struct elf_backend_data *bed;
7564
d4730f92
BS
7565 /* To determine which flavor of relocation this is, we depend on the
7566 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7567 rel_hdr = elf_section_data (sec)->rel.hdr;
7568 if (rel_hdr == NULL)
7569 return FALSE;
c224138d 7570 bed = get_elf_backend_data (abfd);
d4730f92
BS
7571 return ((size_t) (rel - relocs)
7572 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
c224138d
RS
7573}
7574
7575/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7576 HOWTO is the relocation's howto and CONTENTS points to the contents
7577 of the section that REL is against. */
7578
7579static bfd_vma
7580mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7581 reloc_howto_type *howto, bfd_byte *contents)
7582{
7583 bfd_byte *location;
7584 unsigned int r_type;
7585 bfd_vma addend;
7586
7587 r_type = ELF_R_TYPE (abfd, rel->r_info);
7588 location = contents + rel->r_offset;
7589
7590 /* Get the addend, which is stored in the input file. */
df58fc94 7591 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
c224138d 7592 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
df58fc94 7593 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
c224138d
RS
7594
7595 return addend & howto->src_mask;
7596}
7597
7598/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7599 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7600 and update *ADDEND with the final addend. Return true on success
7601 or false if the LO16 could not be found. RELEND is the exclusive
7602 upper bound on the relocations for REL's section. */
7603
7604static bfd_boolean
7605mips_elf_add_lo16_rel_addend (bfd *abfd,
7606 const Elf_Internal_Rela *rel,
7607 const Elf_Internal_Rela *relend,
7608 bfd_byte *contents, bfd_vma *addend)
7609{
7610 unsigned int r_type, lo16_type;
7611 const Elf_Internal_Rela *lo16_relocation;
7612 reloc_howto_type *lo16_howto;
7613 bfd_vma l;
7614
7615 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7616 if (mips16_reloc_p (r_type))
c224138d 7617 lo16_type = R_MIPS16_LO16;
df58fc94
RS
7618 else if (micromips_reloc_p (r_type))
7619 lo16_type = R_MICROMIPS_LO16;
c224138d
RS
7620 else
7621 lo16_type = R_MIPS_LO16;
7622
7623 /* The combined value is the sum of the HI16 addend, left-shifted by
7624 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7625 code does a `lui' of the HI16 value, and then an `addiu' of the
7626 LO16 value.)
7627
7628 Scan ahead to find a matching LO16 relocation.
7629
7630 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7631 be immediately following. However, for the IRIX6 ABI, the next
7632 relocation may be a composed relocation consisting of several
7633 relocations for the same address. In that case, the R_MIPS_LO16
7634 relocation may occur as one of these. We permit a similar
7635 extension in general, as that is useful for GCC.
7636
7637 In some cases GCC dead code elimination removes the LO16 but keeps
7638 the corresponding HI16. This is strictly speaking a violation of
7639 the ABI but not immediately harmful. */
7640 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7641 if (lo16_relocation == NULL)
7642 return FALSE;
7643
7644 /* Obtain the addend kept there. */
7645 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7646 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7647
7648 l <<= lo16_howto->rightshift;
7649 l = _bfd_mips_elf_sign_extend (l, 16);
7650
7651 *addend <<= 16;
7652 *addend += l;
7653 return TRUE;
7654}
7655
7656/* Try to read the contents of section SEC in bfd ABFD. Return true and
7657 store the contents in *CONTENTS on success. Assume that *CONTENTS
7658 already holds the contents if it is nonull on entry. */
7659
7660static bfd_boolean
7661mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7662{
7663 if (*contents)
7664 return TRUE;
7665
7666 /* Get cached copy if it exists. */
7667 if (elf_section_data (sec)->this_hdr.contents != NULL)
7668 {
7669 *contents = elf_section_data (sec)->this_hdr.contents;
7670 return TRUE;
7671 }
7672
7673 return bfd_malloc_and_get_section (abfd, sec, contents);
7674}
7675
1bbce132
MR
7676/* Make a new PLT record to keep internal data. */
7677
7678static struct plt_entry *
7679mips_elf_make_plt_record (bfd *abfd)
7680{
7681 struct plt_entry *entry;
7682
7683 entry = bfd_zalloc (abfd, sizeof (*entry));
7684 if (entry == NULL)
7685 return NULL;
7686
7687 entry->stub_offset = MINUS_ONE;
7688 entry->mips_offset = MINUS_ONE;
7689 entry->comp_offset = MINUS_ONE;
7690 entry->gotplt_index = MINUS_ONE;
7691 return entry;
7692}
7693
b49e97c9 7694/* Look through the relocs for a section during the first phase, and
1bbce132
MR
7695 allocate space in the global offset table and record the need for
7696 standard MIPS and compressed procedure linkage table entries. */
b49e97c9 7697
b34976b6 7698bfd_boolean
9719ad41
RS
7699_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7700 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7701{
7702 const char *name;
7703 bfd *dynobj;
7704 Elf_Internal_Shdr *symtab_hdr;
7705 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7706 size_t extsymoff;
7707 const Elf_Internal_Rela *rel;
7708 const Elf_Internal_Rela *rel_end;
b49e97c9 7709 asection *sreloc;
9c5bfbb7 7710 const struct elf_backend_data *bed;
0a44bf69 7711 struct mips_elf_link_hash_table *htab;
c224138d
RS
7712 bfd_byte *contents;
7713 bfd_vma addend;
7714 reloc_howto_type *howto;
b49e97c9 7715
1049f94e 7716 if (info->relocatable)
b34976b6 7717 return TRUE;
b49e97c9 7718
0a44bf69 7719 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7720 BFD_ASSERT (htab != NULL);
7721
b49e97c9
TS
7722 dynobj = elf_hash_table (info)->dynobj;
7723 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7724 sym_hashes = elf_sym_hashes (abfd);
7725 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7726
738e5348
RS
7727 bed = get_elf_backend_data (abfd);
7728 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7729
b49e97c9
TS
7730 /* Check for the mips16 stub sections. */
7731
7732 name = bfd_get_section_name (abfd, sec);
b9d58d71 7733 if (FN_STUB_P (name))
b49e97c9
TS
7734 {
7735 unsigned long r_symndx;
7736
7737 /* Look at the relocation information to figure out which symbol
7738 this is for. */
7739
cb4437b8 7740 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
7741 if (r_symndx == 0)
7742 {
7743 (*_bfd_error_handler)
7744 (_("%B: Warning: cannot determine the target function for"
7745 " stub section `%s'"),
7746 abfd, name);
7747 bfd_set_error (bfd_error_bad_value);
7748 return FALSE;
7749 }
b49e97c9
TS
7750
7751 if (r_symndx < extsymoff
7752 || sym_hashes[r_symndx - extsymoff] == NULL)
7753 {
7754 asection *o;
7755
7756 /* This stub is for a local symbol. This stub will only be
7757 needed if there is some relocation in this BFD, other
7758 than a 16 bit function call, which refers to this symbol. */
7759 for (o = abfd->sections; o != NULL; o = o->next)
7760 {
7761 Elf_Internal_Rela *sec_relocs;
7762 const Elf_Internal_Rela *r, *rend;
7763
7764 /* We can ignore stub sections when looking for relocs. */
7765 if ((o->flags & SEC_RELOC) == 0
7766 || o->reloc_count == 0
738e5348 7767 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7768 continue;
7769
45d6a902 7770 sec_relocs
9719ad41 7771 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7772 info->keep_memory);
b49e97c9 7773 if (sec_relocs == NULL)
b34976b6 7774 return FALSE;
b49e97c9
TS
7775
7776 rend = sec_relocs + o->reloc_count;
7777 for (r = sec_relocs; r < rend; r++)
7778 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7779 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7780 break;
7781
6cdc0ccc 7782 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7783 free (sec_relocs);
7784
7785 if (r < rend)
7786 break;
7787 }
7788
7789 if (o == NULL)
7790 {
7791 /* There is no non-call reloc for this stub, so we do
7792 not need it. Since this function is called before
7793 the linker maps input sections to output sections, we
7794 can easily discard it by setting the SEC_EXCLUDE
7795 flag. */
7796 sec->flags |= SEC_EXCLUDE;
b34976b6 7797 return TRUE;
b49e97c9
TS
7798 }
7799
7800 /* Record this stub in an array of local symbol stubs for
7801 this BFD. */
698600e4 7802 if (mips_elf_tdata (abfd)->local_stubs == NULL)
b49e97c9
TS
7803 {
7804 unsigned long symcount;
7805 asection **n;
7806 bfd_size_type amt;
7807
7808 if (elf_bad_symtab (abfd))
7809 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7810 else
7811 symcount = symtab_hdr->sh_info;
7812 amt = symcount * sizeof (asection *);
9719ad41 7813 n = bfd_zalloc (abfd, amt);
b49e97c9 7814 if (n == NULL)
b34976b6 7815 return FALSE;
698600e4 7816 mips_elf_tdata (abfd)->local_stubs = n;
b49e97c9
TS
7817 }
7818
b9d58d71 7819 sec->flags |= SEC_KEEP;
698600e4 7820 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
b49e97c9
TS
7821
7822 /* We don't need to set mips16_stubs_seen in this case.
7823 That flag is used to see whether we need to look through
7824 the global symbol table for stubs. We don't need to set
7825 it here, because we just have a local stub. */
7826 }
7827 else
7828 {
7829 struct mips_elf_link_hash_entry *h;
7830
7831 h = ((struct mips_elf_link_hash_entry *)
7832 sym_hashes[r_symndx - extsymoff]);
7833
973a3492
L
7834 while (h->root.root.type == bfd_link_hash_indirect
7835 || h->root.root.type == bfd_link_hash_warning)
7836 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7837
b49e97c9
TS
7838 /* H is the symbol this stub is for. */
7839
b9d58d71
TS
7840 /* If we already have an appropriate stub for this function, we
7841 don't need another one, so we can discard this one. Since
7842 this function is called before the linker maps input sections
7843 to output sections, we can easily discard it by setting the
7844 SEC_EXCLUDE flag. */
7845 if (h->fn_stub != NULL)
7846 {
7847 sec->flags |= SEC_EXCLUDE;
7848 return TRUE;
7849 }
7850
7851 sec->flags |= SEC_KEEP;
b49e97c9 7852 h->fn_stub = sec;
b34976b6 7853 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7854 }
7855 }
b9d58d71 7856 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7857 {
7858 unsigned long r_symndx;
7859 struct mips_elf_link_hash_entry *h;
7860 asection **loc;
7861
7862 /* Look at the relocation information to figure out which symbol
7863 this is for. */
7864
cb4437b8 7865 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
7866 if (r_symndx == 0)
7867 {
7868 (*_bfd_error_handler)
7869 (_("%B: Warning: cannot determine the target function for"
7870 " stub section `%s'"),
7871 abfd, name);
7872 bfd_set_error (bfd_error_bad_value);
7873 return FALSE;
7874 }
b49e97c9
TS
7875
7876 if (r_symndx < extsymoff
7877 || sym_hashes[r_symndx - extsymoff] == NULL)
7878 {
b9d58d71 7879 asection *o;
b49e97c9 7880
b9d58d71
TS
7881 /* This stub is for a local symbol. This stub will only be
7882 needed if there is some relocation (R_MIPS16_26) in this BFD
7883 that refers to this symbol. */
7884 for (o = abfd->sections; o != NULL; o = o->next)
7885 {
7886 Elf_Internal_Rela *sec_relocs;
7887 const Elf_Internal_Rela *r, *rend;
7888
7889 /* We can ignore stub sections when looking for relocs. */
7890 if ((o->flags & SEC_RELOC) == 0
7891 || o->reloc_count == 0
738e5348 7892 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7893 continue;
7894
7895 sec_relocs
7896 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7897 info->keep_memory);
7898 if (sec_relocs == NULL)
7899 return FALSE;
7900
7901 rend = sec_relocs + o->reloc_count;
7902 for (r = sec_relocs; r < rend; r++)
7903 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7904 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7905 break;
7906
7907 if (elf_section_data (o)->relocs != sec_relocs)
7908 free (sec_relocs);
7909
7910 if (r < rend)
7911 break;
7912 }
7913
7914 if (o == NULL)
7915 {
7916 /* There is no non-call reloc for this stub, so we do
7917 not need it. Since this function is called before
7918 the linker maps input sections to output sections, we
7919 can easily discard it by setting the SEC_EXCLUDE
7920 flag. */
7921 sec->flags |= SEC_EXCLUDE;
7922 return TRUE;
7923 }
7924
7925 /* Record this stub in an array of local symbol call_stubs for
7926 this BFD. */
698600e4 7927 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
b9d58d71
TS
7928 {
7929 unsigned long symcount;
7930 asection **n;
7931 bfd_size_type amt;
7932
7933 if (elf_bad_symtab (abfd))
7934 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7935 else
7936 symcount = symtab_hdr->sh_info;
7937 amt = symcount * sizeof (asection *);
7938 n = bfd_zalloc (abfd, amt);
7939 if (n == NULL)
7940 return FALSE;
698600e4 7941 mips_elf_tdata (abfd)->local_call_stubs = n;
b9d58d71 7942 }
b49e97c9 7943
b9d58d71 7944 sec->flags |= SEC_KEEP;
698600e4 7945 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7946
b9d58d71
TS
7947 /* We don't need to set mips16_stubs_seen in this case.
7948 That flag is used to see whether we need to look through
7949 the global symbol table for stubs. We don't need to set
7950 it here, because we just have a local stub. */
7951 }
b49e97c9 7952 else
b49e97c9 7953 {
b9d58d71
TS
7954 h = ((struct mips_elf_link_hash_entry *)
7955 sym_hashes[r_symndx - extsymoff]);
68ffbac6 7956
b9d58d71 7957 /* H is the symbol this stub is for. */
68ffbac6 7958
b9d58d71
TS
7959 if (CALL_FP_STUB_P (name))
7960 loc = &h->call_fp_stub;
7961 else
7962 loc = &h->call_stub;
68ffbac6 7963
b9d58d71
TS
7964 /* If we already have an appropriate stub for this function, we
7965 don't need another one, so we can discard this one. Since
7966 this function is called before the linker maps input sections
7967 to output sections, we can easily discard it by setting the
7968 SEC_EXCLUDE flag. */
7969 if (*loc != NULL)
7970 {
7971 sec->flags |= SEC_EXCLUDE;
7972 return TRUE;
7973 }
b49e97c9 7974
b9d58d71
TS
7975 sec->flags |= SEC_KEEP;
7976 *loc = sec;
7977 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7978 }
b49e97c9
TS
7979 }
7980
b49e97c9 7981 sreloc = NULL;
c224138d 7982 contents = NULL;
b49e97c9
TS
7983 for (rel = relocs; rel < rel_end; ++rel)
7984 {
7985 unsigned long r_symndx;
7986 unsigned int r_type;
7987 struct elf_link_hash_entry *h;
861fb55a 7988 bfd_boolean can_make_dynamic_p;
c5d6fa44
RS
7989 bfd_boolean call_reloc_p;
7990 bfd_boolean constrain_symbol_p;
b49e97c9
TS
7991
7992 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7993 r_type = ELF_R_TYPE (abfd, rel->r_info);
7994
7995 if (r_symndx < extsymoff)
7996 h = NULL;
7997 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7998 {
7999 (*_bfd_error_handler)
d003868e
AM
8000 (_("%B: Malformed reloc detected for section %s"),
8001 abfd, name);
b49e97c9 8002 bfd_set_error (bfd_error_bad_value);
b34976b6 8003 return FALSE;
b49e97c9
TS
8004 }
8005 else
8006 {
8007 h = sym_hashes[r_symndx - extsymoff];
81fbe831
AM
8008 if (h != NULL)
8009 {
8010 while (h->root.type == bfd_link_hash_indirect
8011 || h->root.type == bfd_link_hash_warning)
8012 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8013
8014 /* PR15323, ref flags aren't set for references in the
8015 same object. */
8016 h->root.non_ir_ref = 1;
8017 }
861fb55a 8018 }
b49e97c9 8019
861fb55a
DJ
8020 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8021 relocation into a dynamic one. */
8022 can_make_dynamic_p = FALSE;
c5d6fa44
RS
8023
8024 /* Set CALL_RELOC_P to true if the relocation is for a call,
8025 and if pointer equality therefore doesn't matter. */
8026 call_reloc_p = FALSE;
8027
8028 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8029 into account when deciding how to define the symbol.
8030 Relocations in nonallocatable sections such as .pdr and
8031 .debug* should have no effect. */
8032 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8033
861fb55a
DJ
8034 switch (r_type)
8035 {
861fb55a
DJ
8036 case R_MIPS_CALL16:
8037 case R_MIPS_CALL_HI16:
8038 case R_MIPS_CALL_LO16:
c5d6fa44
RS
8039 case R_MIPS16_CALL16:
8040 case R_MICROMIPS_CALL16:
8041 case R_MICROMIPS_CALL_HI16:
8042 case R_MICROMIPS_CALL_LO16:
8043 call_reloc_p = TRUE;
8044 /* Fall through. */
8045
8046 case R_MIPS_GOT16:
861fb55a
DJ
8047 case R_MIPS_GOT_HI16:
8048 case R_MIPS_GOT_LO16:
8049 case R_MIPS_GOT_PAGE:
8050 case R_MIPS_GOT_OFST:
8051 case R_MIPS_GOT_DISP:
8052 case R_MIPS_TLS_GOTTPREL:
8053 case R_MIPS_TLS_GD:
8054 case R_MIPS_TLS_LDM:
d0f13682 8055 case R_MIPS16_GOT16:
d0f13682
CLT
8056 case R_MIPS16_TLS_GOTTPREL:
8057 case R_MIPS16_TLS_GD:
8058 case R_MIPS16_TLS_LDM:
df58fc94 8059 case R_MICROMIPS_GOT16:
df58fc94
RS
8060 case R_MICROMIPS_GOT_HI16:
8061 case R_MICROMIPS_GOT_LO16:
8062 case R_MICROMIPS_GOT_PAGE:
8063 case R_MICROMIPS_GOT_OFST:
8064 case R_MICROMIPS_GOT_DISP:
8065 case R_MICROMIPS_TLS_GOTTPREL:
8066 case R_MICROMIPS_TLS_GD:
8067 case R_MICROMIPS_TLS_LDM:
861fb55a
DJ
8068 if (dynobj == NULL)
8069 elf_hash_table (info)->dynobj = dynobj = abfd;
8070 if (!mips_elf_create_got_section (dynobj, info))
8071 return FALSE;
8072 if (htab->is_vxworks && !info->shared)
b49e97c9 8073 {
861fb55a
DJ
8074 (*_bfd_error_handler)
8075 (_("%B: GOT reloc at 0x%lx not expected in executables"),
8076 abfd, (unsigned long) rel->r_offset);
8077 bfd_set_error (bfd_error_bad_value);
8078 return FALSE;
b49e97c9 8079 }
c5d6fa44 8080 can_make_dynamic_p = TRUE;
861fb55a 8081 break;
b49e97c9 8082
c5d6fa44 8083 case R_MIPS_NONE:
99da6b5f 8084 case R_MIPS_JALR:
df58fc94 8085 case R_MICROMIPS_JALR:
c5d6fa44
RS
8086 /* These relocations have empty fields and are purely there to
8087 provide link information. The symbol value doesn't matter. */
8088 constrain_symbol_p = FALSE;
8089 break;
8090
8091 case R_MIPS_GPREL16:
8092 case R_MIPS_GPREL32:
8093 case R_MIPS16_GPREL:
8094 case R_MICROMIPS_GPREL16:
8095 /* GP-relative relocations always resolve to a definition in a
8096 regular input file, ignoring the one-definition rule. This is
8097 important for the GP setup sequence in NewABI code, which
8098 always resolves to a local function even if other relocations
8099 against the symbol wouldn't. */
8100 constrain_symbol_p = FALSE;
99da6b5f
AN
8101 break;
8102
861fb55a
DJ
8103 case R_MIPS_32:
8104 case R_MIPS_REL32:
8105 case R_MIPS_64:
8106 /* In VxWorks executables, references to external symbols
8107 must be handled using copy relocs or PLT entries; it is not
8108 possible to convert this relocation into a dynamic one.
8109
8110 For executables that use PLTs and copy-relocs, we have a
8111 choice between converting the relocation into a dynamic
8112 one or using copy relocations or PLT entries. It is
8113 usually better to do the former, unless the relocation is
8114 against a read-only section. */
8115 if ((info->shared
8116 || (h != NULL
8117 && !htab->is_vxworks
8118 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8119 && !(!info->nocopyreloc
8120 && !PIC_OBJECT_P (abfd)
8121 && MIPS_ELF_READONLY_SECTION (sec))))
8122 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 8123 {
861fb55a 8124 can_make_dynamic_p = TRUE;
b49e97c9
TS
8125 if (dynobj == NULL)
8126 elf_hash_table (info)->dynobj = dynobj = abfd;
861fb55a 8127 }
c5d6fa44 8128 break;
b49e97c9 8129
861fb55a
DJ
8130 case R_MIPS_26:
8131 case R_MIPS_PC16:
8132 case R_MIPS16_26:
df58fc94
RS
8133 case R_MICROMIPS_26_S1:
8134 case R_MICROMIPS_PC7_S1:
8135 case R_MICROMIPS_PC10_S1:
8136 case R_MICROMIPS_PC16_S1:
8137 case R_MICROMIPS_PC23_S2:
c5d6fa44 8138 call_reloc_p = TRUE;
861fb55a 8139 break;
b49e97c9
TS
8140 }
8141
0a44bf69
RS
8142 if (h)
8143 {
c5d6fa44
RS
8144 if (constrain_symbol_p)
8145 {
8146 if (!can_make_dynamic_p)
8147 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8148
8149 if (!call_reloc_p)
8150 h->pointer_equality_needed = 1;
8151
8152 /* We must not create a stub for a symbol that has
8153 relocations related to taking the function's address.
8154 This doesn't apply to VxWorks, where CALL relocs refer
8155 to a .got.plt entry instead of a normal .got entry. */
8156 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8157 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8158 }
8159
0a44bf69
RS
8160 /* Relocations against the special VxWorks __GOTT_BASE__ and
8161 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8162 room for them in .rela.dyn. */
8163 if (is_gott_symbol (info, h))
8164 {
8165 if (sreloc == NULL)
8166 {
8167 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8168 if (sreloc == NULL)
8169 return FALSE;
8170 }
8171 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
8172 if (MIPS_ELF_READONLY_SECTION (sec))
8173 /* We tell the dynamic linker that there are
8174 relocations against the text segment. */
8175 info->flags |= DF_TEXTREL;
0a44bf69
RS
8176 }
8177 }
df58fc94
RS
8178 else if (call_lo16_reloc_p (r_type)
8179 || got_lo16_reloc_p (r_type)
8180 || got_disp_reloc_p (r_type)
738e5348 8181 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
8182 {
8183 /* We may need a local GOT entry for this relocation. We
8184 don't count R_MIPS_GOT_PAGE because we can estimate the
8185 maximum number of pages needed by looking at the size of
738e5348
RS
8186 the segment. Similar comments apply to R_MIPS*_GOT16 and
8187 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 8188 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 8189 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 8190 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0 8191 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
e641e783 8192 rel->r_addend, info, r_type))
f4416af6 8193 return FALSE;
b49e97c9
TS
8194 }
8195
8f0c309a
CLT
8196 if (h != NULL
8197 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8198 ELF_ST_IS_MIPS16 (h->other)))
861fb55a
DJ
8199 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8200
b49e97c9
TS
8201 switch (r_type)
8202 {
8203 case R_MIPS_CALL16:
738e5348 8204 case R_MIPS16_CALL16:
df58fc94 8205 case R_MICROMIPS_CALL16:
b49e97c9
TS
8206 if (h == NULL)
8207 {
8208 (*_bfd_error_handler)
d003868e
AM
8209 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
8210 abfd, (unsigned long) rel->r_offset);
b49e97c9 8211 bfd_set_error (bfd_error_bad_value);
b34976b6 8212 return FALSE;
b49e97c9
TS
8213 }
8214 /* Fall through. */
8215
8216 case R_MIPS_CALL_HI16:
8217 case R_MIPS_CALL_LO16:
df58fc94
RS
8218 case R_MICROMIPS_CALL_HI16:
8219 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
8220 if (h != NULL)
8221 {
6ccf4795
RS
8222 /* Make sure there is room in the regular GOT to hold the
8223 function's address. We may eliminate it in favour of
8224 a .got.plt entry later; see mips_elf_count_got_symbols. */
e641e783
RS
8225 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8226 r_type))
b34976b6 8227 return FALSE;
b49e97c9
TS
8228
8229 /* We need a stub, not a plt entry for the undefined
8230 function. But we record it as if it needs plt. See
c152c796 8231 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 8232 h->needs_plt = 1;
b49e97c9
TS
8233 h->type = STT_FUNC;
8234 }
8235 break;
8236
0fdc1bf1 8237 case R_MIPS_GOT_PAGE:
df58fc94 8238 case R_MICROMIPS_GOT_PAGE:
738e5348 8239 case R_MIPS16_GOT16:
b49e97c9
TS
8240 case R_MIPS_GOT16:
8241 case R_MIPS_GOT_HI16:
8242 case R_MIPS_GOT_LO16:
df58fc94
RS
8243 case R_MICROMIPS_GOT16:
8244 case R_MICROMIPS_GOT_HI16:
8245 case R_MICROMIPS_GOT_LO16:
8246 if (!h || got_page_reloc_p (r_type))
c224138d 8247 {
3a3b6725
DJ
8248 /* This relocation needs (or may need, if h != NULL) a
8249 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8250 know for sure until we know whether the symbol is
8251 preemptible. */
c224138d
RS
8252 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8253 {
8254 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8255 return FALSE;
8256 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8257 addend = mips_elf_read_rel_addend (abfd, rel,
8258 howto, contents);
9684f078 8259 if (got16_reloc_p (r_type))
c224138d
RS
8260 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8261 contents, &addend);
8262 else
8263 addend <<= howto->rightshift;
8264 }
8265 else
8266 addend = rel->r_addend;
13db6b44
RS
8267 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8268 h, addend))
c224138d 8269 return FALSE;
13db6b44
RS
8270
8271 if (h)
8272 {
8273 struct mips_elf_link_hash_entry *hmips =
8274 (struct mips_elf_link_hash_entry *) h;
8275
8276 /* This symbol is definitely not overridable. */
8277 if (hmips->root.def_regular
8278 && ! (info->shared && ! info->symbolic
8279 && ! hmips->root.forced_local))
8280 h = NULL;
8281 }
c224138d 8282 }
13db6b44
RS
8283 /* If this is a global, overridable symbol, GOT_PAGE will
8284 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d
RS
8285 /* Fall through. */
8286
b49e97c9 8287 case R_MIPS_GOT_DISP:
df58fc94 8288 case R_MICROMIPS_GOT_DISP:
6ccf4795 8289 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
e641e783 8290 FALSE, r_type))
b34976b6 8291 return FALSE;
b49e97c9
TS
8292 break;
8293
0f20cc35 8294 case R_MIPS_TLS_GOTTPREL:
d0f13682 8295 case R_MIPS16_TLS_GOTTPREL:
df58fc94 8296 case R_MICROMIPS_TLS_GOTTPREL:
0f20cc35
DJ
8297 if (info->shared)
8298 info->flags |= DF_STATIC_TLS;
8299 /* Fall through */
8300
8301 case R_MIPS_TLS_LDM:
d0f13682 8302 case R_MIPS16_TLS_LDM:
df58fc94
RS
8303 case R_MICROMIPS_TLS_LDM:
8304 if (tls_ldm_reloc_p (r_type))
0f20cc35 8305 {
cf35638d 8306 r_symndx = STN_UNDEF;
0f20cc35
DJ
8307 h = NULL;
8308 }
8309 /* Fall through */
8310
8311 case R_MIPS_TLS_GD:
d0f13682 8312 case R_MIPS16_TLS_GD:
df58fc94 8313 case R_MICROMIPS_TLS_GD:
0f20cc35
DJ
8314 /* This symbol requires a global offset table entry, or two
8315 for TLS GD relocations. */
e641e783
RS
8316 if (h != NULL)
8317 {
8318 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8319 FALSE, r_type))
8320 return FALSE;
8321 }
8322 else
8323 {
8324 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8325 rel->r_addend,
8326 info, r_type))
8327 return FALSE;
8328 }
0f20cc35
DJ
8329 break;
8330
b49e97c9
TS
8331 case R_MIPS_32:
8332 case R_MIPS_REL32:
8333 case R_MIPS_64:
0a44bf69
RS
8334 /* In VxWorks executables, references to external symbols
8335 are handled using copy relocs or PLT stubs, so there's
8336 no need to add a .rela.dyn entry for this relocation. */
861fb55a 8337 if (can_make_dynamic_p)
b49e97c9
TS
8338 {
8339 if (sreloc == NULL)
8340 {
0a44bf69 8341 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 8342 if (sreloc == NULL)
f4416af6 8343 return FALSE;
b49e97c9 8344 }
9a59ad6b 8345 if (info->shared && h == NULL)
82f0cfbd
EC
8346 {
8347 /* When creating a shared object, we must copy these
8348 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
8349 relocs. Make room for this reloc in .rel(a).dyn. */
8350 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 8351 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8352 /* We tell the dynamic linker that there are
8353 relocations against the text segment. */
8354 info->flags |= DF_TEXTREL;
8355 }
b49e97c9
TS
8356 else
8357 {
8358 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 8359
9a59ad6b
DJ
8360 /* For a shared object, we must copy this relocation
8361 unless the symbol turns out to be undefined and
8362 weak with non-default visibility, in which case
8363 it will be left as zero.
8364
8365 We could elide R_MIPS_REL32 for locally binding symbols
8366 in shared libraries, but do not yet do so.
8367
8368 For an executable, we only need to copy this
8369 reloc if the symbol is defined in a dynamic
8370 object. */
b49e97c9
TS
8371 hmips = (struct mips_elf_link_hash_entry *) h;
8372 ++hmips->possibly_dynamic_relocs;
943284cc 8373 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8374 /* We need it to tell the dynamic linker if there
8375 are relocations against the text segment. */
8376 hmips->readonly_reloc = TRUE;
b49e97c9 8377 }
b49e97c9
TS
8378 }
8379
8380 if (SGI_COMPAT (abfd))
8381 mips_elf_hash_table (info)->compact_rel_size +=
8382 sizeof (Elf32_External_crinfo);
8383 break;
8384
8385 case R_MIPS_26:
8386 case R_MIPS_GPREL16:
8387 case R_MIPS_LITERAL:
8388 case R_MIPS_GPREL32:
df58fc94
RS
8389 case R_MICROMIPS_26_S1:
8390 case R_MICROMIPS_GPREL16:
8391 case R_MICROMIPS_LITERAL:
8392 case R_MICROMIPS_GPREL7_S2:
b49e97c9
TS
8393 if (SGI_COMPAT (abfd))
8394 mips_elf_hash_table (info)->compact_rel_size +=
8395 sizeof (Elf32_External_crinfo);
8396 break;
8397
8398 /* This relocation describes the C++ object vtable hierarchy.
8399 Reconstruct it for later use during GC. */
8400 case R_MIPS_GNU_VTINHERIT:
c152c796 8401 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 8402 return FALSE;
b49e97c9
TS
8403 break;
8404
8405 /* This relocation describes which C++ vtable entries are actually
8406 used. Record for later use during GC. */
8407 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
8408 BFD_ASSERT (h != NULL);
8409 if (h != NULL
8410 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 8411 return FALSE;
b49e97c9
TS
8412 break;
8413
8414 default:
8415 break;
8416 }
8417
1bbce132
MR
8418 /* Record the need for a PLT entry. At this point we don't know
8419 yet if we are going to create a PLT in the first place, but
8420 we only record whether the relocation requires a standard MIPS
8421 or a compressed code entry anyway. If we don't make a PLT after
8422 all, then we'll just ignore these arrangements. Likewise if
8423 a PLT entry is not created because the symbol is satisfied
8424 locally. */
8425 if (h != NULL
8426 && jal_reloc_p (r_type)
8427 && !SYMBOL_CALLS_LOCAL (info, h))
8428 {
8429 if (h->plt.plist == NULL)
8430 h->plt.plist = mips_elf_make_plt_record (abfd);
8431 if (h->plt.plist == NULL)
8432 return FALSE;
8433
8434 if (r_type == R_MIPS_26)
8435 h->plt.plist->need_mips = TRUE;
8436 else
8437 h->plt.plist->need_comp = TRUE;
8438 }
8439
738e5348
RS
8440 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8441 if there is one. We only need to handle global symbols here;
8442 we decide whether to keep or delete stubs for local symbols
8443 when processing the stub's relocations. */
b49e97c9 8444 if (h != NULL
738e5348
RS
8445 && !mips16_call_reloc_p (r_type)
8446 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
8447 {
8448 struct mips_elf_link_hash_entry *mh;
8449
8450 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 8451 mh->need_fn_stub = TRUE;
b49e97c9 8452 }
861fb55a
DJ
8453
8454 /* Refuse some position-dependent relocations when creating a
8455 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8456 not PIC, but we can create dynamic relocations and the result
8457 will be fine. Also do not refuse R_MIPS_LO16, which can be
8458 combined with R_MIPS_GOT16. */
8459 if (info->shared)
8460 {
8461 switch (r_type)
8462 {
8463 case R_MIPS16_HI16:
8464 case R_MIPS_HI16:
8465 case R_MIPS_HIGHER:
8466 case R_MIPS_HIGHEST:
df58fc94
RS
8467 case R_MICROMIPS_HI16:
8468 case R_MICROMIPS_HIGHER:
8469 case R_MICROMIPS_HIGHEST:
861fb55a
DJ
8470 /* Don't refuse a high part relocation if it's against
8471 no symbol (e.g. part of a compound relocation). */
cf35638d 8472 if (r_symndx == STN_UNDEF)
861fb55a
DJ
8473 break;
8474
8475 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8476 and has a special meaning. */
8477 if (!NEWABI_P (abfd) && h != NULL
8478 && strcmp (h->root.root.string, "_gp_disp") == 0)
8479 break;
8480
0fc1eb3c
RS
8481 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8482 if (is_gott_symbol (info, h))
8483 break;
8484
861fb55a
DJ
8485 /* FALLTHROUGH */
8486
8487 case R_MIPS16_26:
8488 case R_MIPS_26:
df58fc94 8489 case R_MICROMIPS_26_S1:
861fb55a
DJ
8490 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8491 (*_bfd_error_handler)
8492 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8493 abfd, howto->name,
8494 (h) ? h->root.root.string : "a local symbol");
8495 bfd_set_error (bfd_error_bad_value);
8496 return FALSE;
8497 default:
8498 break;
8499 }
8500 }
b49e97c9
TS
8501 }
8502
b34976b6 8503 return TRUE;
b49e97c9
TS
8504}
8505\f
d0647110 8506bfd_boolean
9719ad41
RS
8507_bfd_mips_relax_section (bfd *abfd, asection *sec,
8508 struct bfd_link_info *link_info,
8509 bfd_boolean *again)
d0647110
AO
8510{
8511 Elf_Internal_Rela *internal_relocs;
8512 Elf_Internal_Rela *irel, *irelend;
8513 Elf_Internal_Shdr *symtab_hdr;
8514 bfd_byte *contents = NULL;
d0647110
AO
8515 size_t extsymoff;
8516 bfd_boolean changed_contents = FALSE;
8517 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8518 Elf_Internal_Sym *isymbuf = NULL;
8519
8520 /* We are not currently changing any sizes, so only one pass. */
8521 *again = FALSE;
8522
1049f94e 8523 if (link_info->relocatable)
d0647110
AO
8524 return TRUE;
8525
9719ad41 8526 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 8527 link_info->keep_memory);
d0647110
AO
8528 if (internal_relocs == NULL)
8529 return TRUE;
8530
8531 irelend = internal_relocs + sec->reloc_count
8532 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8533 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8534 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8535
8536 for (irel = internal_relocs; irel < irelend; irel++)
8537 {
8538 bfd_vma symval;
8539 bfd_signed_vma sym_offset;
8540 unsigned int r_type;
8541 unsigned long r_symndx;
8542 asection *sym_sec;
8543 unsigned long instruction;
8544
8545 /* Turn jalr into bgezal, and jr into beq, if they're marked
8546 with a JALR relocation, that indicate where they jump to.
8547 This saves some pipeline bubbles. */
8548 r_type = ELF_R_TYPE (abfd, irel->r_info);
8549 if (r_type != R_MIPS_JALR)
8550 continue;
8551
8552 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8553 /* Compute the address of the jump target. */
8554 if (r_symndx >= extsymoff)
8555 {
8556 struct mips_elf_link_hash_entry *h
8557 = ((struct mips_elf_link_hash_entry *)
8558 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8559
8560 while (h->root.root.type == bfd_link_hash_indirect
8561 || h->root.root.type == bfd_link_hash_warning)
8562 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 8563
d0647110
AO
8564 /* If a symbol is undefined, or if it may be overridden,
8565 skip it. */
8566 if (! ((h->root.root.type == bfd_link_hash_defined
8567 || h->root.root.type == bfd_link_hash_defweak)
8568 && h->root.root.u.def.section)
8569 || (link_info->shared && ! link_info->symbolic
f5385ebf 8570 && !h->root.forced_local))
d0647110
AO
8571 continue;
8572
8573 sym_sec = h->root.root.u.def.section;
8574 if (sym_sec->output_section)
8575 symval = (h->root.root.u.def.value
8576 + sym_sec->output_section->vma
8577 + sym_sec->output_offset);
8578 else
8579 symval = h->root.root.u.def.value;
8580 }
8581 else
8582 {
8583 Elf_Internal_Sym *isym;
8584
8585 /* Read this BFD's symbols if we haven't done so already. */
8586 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8587 {
8588 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8589 if (isymbuf == NULL)
8590 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8591 symtab_hdr->sh_info, 0,
8592 NULL, NULL, NULL);
8593 if (isymbuf == NULL)
8594 goto relax_return;
8595 }
8596
8597 isym = isymbuf + r_symndx;
8598 if (isym->st_shndx == SHN_UNDEF)
8599 continue;
8600 else if (isym->st_shndx == SHN_ABS)
8601 sym_sec = bfd_abs_section_ptr;
8602 else if (isym->st_shndx == SHN_COMMON)
8603 sym_sec = bfd_com_section_ptr;
8604 else
8605 sym_sec
8606 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8607 symval = isym->st_value
8608 + sym_sec->output_section->vma
8609 + sym_sec->output_offset;
8610 }
8611
8612 /* Compute branch offset, from delay slot of the jump to the
8613 branch target. */
8614 sym_offset = (symval + irel->r_addend)
8615 - (sec_start + irel->r_offset + 4);
8616
8617 /* Branch offset must be properly aligned. */
8618 if ((sym_offset & 3) != 0)
8619 continue;
8620
8621 sym_offset >>= 2;
8622
8623 /* Check that it's in range. */
8624 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8625 continue;
143d77c5 8626
d0647110 8627 /* Get the section contents if we haven't done so already. */
c224138d
RS
8628 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8629 goto relax_return;
d0647110
AO
8630
8631 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8632
8633 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8634 if ((instruction & 0xfc1fffff) == 0x0000f809)
8635 instruction = 0x04110000;
8636 /* If it was jr <reg>, turn it into b <target>. */
8637 else if ((instruction & 0xfc1fffff) == 0x00000008)
8638 instruction = 0x10000000;
8639 else
8640 continue;
8641
8642 instruction |= (sym_offset & 0xffff);
8643 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8644 changed_contents = TRUE;
8645 }
8646
8647 if (contents != NULL
8648 && elf_section_data (sec)->this_hdr.contents != contents)
8649 {
8650 if (!changed_contents && !link_info->keep_memory)
8651 free (contents);
8652 else
8653 {
8654 /* Cache the section contents for elf_link_input_bfd. */
8655 elf_section_data (sec)->this_hdr.contents = contents;
8656 }
8657 }
8658 return TRUE;
8659
143d77c5 8660 relax_return:
eea6121a
AM
8661 if (contents != NULL
8662 && elf_section_data (sec)->this_hdr.contents != contents)
8663 free (contents);
d0647110
AO
8664 return FALSE;
8665}
8666\f
9a59ad6b
DJ
8667/* Allocate space for global sym dynamic relocs. */
8668
8669static bfd_boolean
8670allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8671{
8672 struct bfd_link_info *info = inf;
8673 bfd *dynobj;
8674 struct mips_elf_link_hash_entry *hmips;
8675 struct mips_elf_link_hash_table *htab;
8676
8677 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8678 BFD_ASSERT (htab != NULL);
8679
9a59ad6b
DJ
8680 dynobj = elf_hash_table (info)->dynobj;
8681 hmips = (struct mips_elf_link_hash_entry *) h;
8682
8683 /* VxWorks executables are handled elsewhere; we only need to
8684 allocate relocations in shared objects. */
8685 if (htab->is_vxworks && !info->shared)
8686 return TRUE;
8687
7686d77d
AM
8688 /* Ignore indirect symbols. All relocations against such symbols
8689 will be redirected to the target symbol. */
8690 if (h->root.type == bfd_link_hash_indirect)
63897e2c
RS
8691 return TRUE;
8692
9a59ad6b
DJ
8693 /* If this symbol is defined in a dynamic object, or we are creating
8694 a shared library, we will need to copy any R_MIPS_32 or
8695 R_MIPS_REL32 relocs against it into the output file. */
8696 if (! info->relocatable
8697 && hmips->possibly_dynamic_relocs != 0
8698 && (h->root.type == bfd_link_hash_defweak
625ef6dc 8699 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9a59ad6b
DJ
8700 || info->shared))
8701 {
8702 bfd_boolean do_copy = TRUE;
8703
8704 if (h->root.type == bfd_link_hash_undefweak)
8705 {
8706 /* Do not copy relocations for undefined weak symbols with
8707 non-default visibility. */
8708 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8709 do_copy = FALSE;
8710
8711 /* Make sure undefined weak symbols are output as a dynamic
8712 symbol in PIEs. */
8713 else if (h->dynindx == -1 && !h->forced_local)
8714 {
8715 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8716 return FALSE;
8717 }
8718 }
8719
8720 if (do_copy)
8721 {
aff469fa 8722 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
8723 the SVR4 psABI requires it to have a dynamic symbol table
8724 index greater that DT_MIPS_GOTSYM if there are dynamic
8725 relocations against it.
8726
8727 VxWorks does not enforce the same mapping between the GOT
8728 and the symbol table, so the same requirement does not
8729 apply there. */
6ccf4795
RS
8730 if (!htab->is_vxworks)
8731 {
8732 if (hmips->global_got_area > GGA_RELOC_ONLY)
8733 hmips->global_got_area = GGA_RELOC_ONLY;
8734 hmips->got_only_for_calls = FALSE;
8735 }
aff469fa 8736
9a59ad6b
DJ
8737 mips_elf_allocate_dynamic_relocations
8738 (dynobj, info, hmips->possibly_dynamic_relocs);
8739 if (hmips->readonly_reloc)
8740 /* We tell the dynamic linker that there are relocations
8741 against the text segment. */
8742 info->flags |= DF_TEXTREL;
8743 }
8744 }
8745
8746 return TRUE;
8747}
8748
b49e97c9
TS
8749/* Adjust a symbol defined by a dynamic object and referenced by a
8750 regular object. The current definition is in some section of the
8751 dynamic object, but we're not including those sections. We have to
8752 change the definition to something the rest of the link can
8753 understand. */
8754
b34976b6 8755bfd_boolean
9719ad41
RS
8756_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8757 struct elf_link_hash_entry *h)
b49e97c9
TS
8758{
8759 bfd *dynobj;
8760 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8761 struct mips_elf_link_hash_table *htab;
b49e97c9 8762
5108fc1b 8763 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8764 BFD_ASSERT (htab != NULL);
8765
b49e97c9 8766 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8767 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8768
8769 /* Make sure we know what is going on here. */
8770 BFD_ASSERT (dynobj != NULL
f5385ebf 8771 && (h->needs_plt
f6e332e6 8772 || h->u.weakdef != NULL
f5385ebf
AM
8773 || (h->def_dynamic
8774 && h->ref_regular
8775 && !h->def_regular)));
b49e97c9 8776
b49e97c9 8777 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8778
861fb55a
DJ
8779 /* If there are call relocations against an externally-defined symbol,
8780 see whether we can create a MIPS lazy-binding stub for it. We can
8781 only do this if all references to the function are through call
8782 relocations, and in that case, the traditional lazy-binding stubs
8783 are much more efficient than PLT entries.
8784
8785 Traditional stubs are only available on SVR4 psABI-based systems;
8786 VxWorks always uses PLTs instead. */
8787 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8788 {
8789 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8790 return TRUE;
b49e97c9
TS
8791
8792 /* If this symbol is not defined in a regular file, then set
8793 the symbol to the stub location. This is required to make
8794 function pointers compare as equal between the normal
8795 executable and the shared library. */
f5385ebf 8796 if (!h->def_regular)
b49e97c9 8797 {
33bb52fb
RS
8798 hmips->needs_lazy_stub = TRUE;
8799 htab->lazy_stub_count++;
b34976b6 8800 return TRUE;
b49e97c9
TS
8801 }
8802 }
861fb55a
DJ
8803 /* As above, VxWorks requires PLT entries for externally-defined
8804 functions that are only accessed through call relocations.
b49e97c9 8805
861fb55a
DJ
8806 Both VxWorks and non-VxWorks targets also need PLT entries if there
8807 are static-only relocations against an externally-defined function.
8808 This can technically occur for shared libraries if there are
8809 branches to the symbol, although it is unlikely that this will be
8810 used in practice due to the short ranges involved. It can occur
8811 for any relative or absolute relocation in executables; in that
8812 case, the PLT entry becomes the function's canonical address. */
8813 else if (((h->needs_plt && !hmips->no_fn_stub)
8814 || (h->type == STT_FUNC && hmips->has_static_relocs))
8815 && htab->use_plts_and_copy_relocs
8816 && !SYMBOL_CALLS_LOCAL (info, h)
8817 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8818 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8819 {
1bbce132
MR
8820 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
8821 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
8822
8823 /* If this is the first symbol to need a PLT entry, then make some
8824 basic setup. Also work out PLT entry sizes. We'll need them
8825 for PLT offset calculations. */
8826 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
861fb55a
DJ
8827 {
8828 BFD_ASSERT (htab->sgotplt->size == 0);
1bbce132 8829 BFD_ASSERT (htab->plt_got_index == 0);
0a44bf69 8830
861fb55a
DJ
8831 /* If we're using the PLT additions to the psABI, each PLT
8832 entry is 16 bytes and the PLT0 entry is 32 bytes.
8833 Encourage better cache usage by aligning. We do this
8834 lazily to avoid pessimizing traditional objects. */
8835 if (!htab->is_vxworks
8836 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8837 return FALSE;
0a44bf69 8838
861fb55a
DJ
8839 /* Make sure that .got.plt is word-aligned. We do this lazily
8840 for the same reason as above. */
8841 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8842 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8843 return FALSE;
0a44bf69 8844
861fb55a
DJ
8845 /* On non-VxWorks targets, the first two entries in .got.plt
8846 are reserved. */
8847 if (!htab->is_vxworks)
1bbce132
MR
8848 htab->plt_got_index
8849 += (get_elf_backend_data (dynobj)->got_header_size
8850 / MIPS_ELF_GOT_SIZE (dynobj));
0a44bf69 8851
861fb55a
DJ
8852 /* On VxWorks, also allocate room for the header's
8853 .rela.plt.unloaded entries. */
8854 if (htab->is_vxworks && !info->shared)
0a44bf69 8855 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
1bbce132
MR
8856
8857 /* Now work out the sizes of individual PLT entries. */
8858 if (htab->is_vxworks && info->shared)
8859 htab->plt_mips_entry_size
8860 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
8861 else if (htab->is_vxworks)
8862 htab->plt_mips_entry_size
8863 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
8864 else if (newabi_p)
8865 htab->plt_mips_entry_size
8866 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
833794fc 8867 else if (!micromips_p)
1bbce132
MR
8868 {
8869 htab->plt_mips_entry_size
8870 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8871 htab->plt_comp_entry_size
833794fc
MR
8872 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
8873 }
8874 else if (htab->insn32)
8875 {
8876 htab->plt_mips_entry_size
8877 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8878 htab->plt_comp_entry_size
8879 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
1bbce132
MR
8880 }
8881 else
8882 {
8883 htab->plt_mips_entry_size
8884 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8885 htab->plt_comp_entry_size
833794fc 8886 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
1bbce132 8887 }
0a44bf69
RS
8888 }
8889
1bbce132
MR
8890 if (h->plt.plist == NULL)
8891 h->plt.plist = mips_elf_make_plt_record (dynobj);
8892 if (h->plt.plist == NULL)
8893 return FALSE;
8894
8895 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
8896 n32 or n64, so always use a standard entry there.
8897
8898 If the symbol has a MIPS16 call stub and gets a PLT entry, then
8899 all MIPS16 calls will go via that stub, and there is no benefit
8900 to having a MIPS16 entry. And in the case of call_stub a
8901 standard entry actually has to be used as the stub ends with a J
8902 instruction. */
8903 if (newabi_p
8904 || htab->is_vxworks
8905 || hmips->call_stub
8906 || hmips->call_fp_stub)
8907 {
8908 h->plt.plist->need_mips = TRUE;
8909 h->plt.plist->need_comp = FALSE;
8910 }
8911
8912 /* Otherwise, if there are no direct calls to the function, we
8913 have a free choice of whether to use standard or compressed
8914 entries. Prefer microMIPS entries if the object is known to
8915 contain microMIPS code, so that it becomes possible to create
8916 pure microMIPS binaries. Prefer standard entries otherwise,
8917 because MIPS16 ones are no smaller and are usually slower. */
8918 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
8919 {
8920 if (micromips_p)
8921 h->plt.plist->need_comp = TRUE;
8922 else
8923 h->plt.plist->need_mips = TRUE;
8924 }
8925
8926 if (h->plt.plist->need_mips)
8927 {
8928 h->plt.plist->mips_offset = htab->plt_mips_offset;
8929 htab->plt_mips_offset += htab->plt_mips_entry_size;
8930 }
8931 if (h->plt.plist->need_comp)
8932 {
8933 h->plt.plist->comp_offset = htab->plt_comp_offset;
8934 htab->plt_comp_offset += htab->plt_comp_entry_size;
8935 }
8936
8937 /* Reserve the corresponding .got.plt entry now too. */
8938 h->plt.plist->gotplt_index = htab->plt_got_index++;
0a44bf69
RS
8939
8940 /* If the output file has no definition of the symbol, set the
861fb55a 8941 symbol's value to the address of the stub. */
131eb6b7 8942 if (!info->shared && !h->def_regular)
1bbce132 8943 hmips->use_plt_entry = TRUE;
0a44bf69 8944
1bbce132 8945 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
861fb55a
DJ
8946 htab->srelplt->size += (htab->is_vxworks
8947 ? MIPS_ELF_RELA_SIZE (dynobj)
8948 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8949
8950 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8951 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8952 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8953
861fb55a
DJ
8954 /* All relocations against this symbol that could have been made
8955 dynamic will now refer to the PLT entry instead. */
8956 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8957
0a44bf69
RS
8958 return TRUE;
8959 }
8960
8961 /* If this is a weak symbol, and there is a real definition, the
8962 processor independent code will have arranged for us to see the
8963 real definition first, and we can just use the same value. */
8964 if (h->u.weakdef != NULL)
8965 {
8966 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8967 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8968 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8969 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8970 return TRUE;
8971 }
8972
861fb55a
DJ
8973 /* Otherwise, there is nothing further to do for symbols defined
8974 in regular objects. */
8975 if (h->def_regular)
0a44bf69
RS
8976 return TRUE;
8977
861fb55a
DJ
8978 /* There's also nothing more to do if we'll convert all relocations
8979 against this symbol into dynamic relocations. */
8980 if (!hmips->has_static_relocs)
8981 return TRUE;
8982
8983 /* We're now relying on copy relocations. Complain if we have
8984 some that we can't convert. */
8985 if (!htab->use_plts_and_copy_relocs || info->shared)
8986 {
8987 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8988 "dynamic symbol %s"),
8989 h->root.root.string);
8990 bfd_set_error (bfd_error_bad_value);
8991 return FALSE;
8992 }
8993
0a44bf69
RS
8994 /* We must allocate the symbol in our .dynbss section, which will
8995 become part of the .bss section of the executable. There will be
8996 an entry for this symbol in the .dynsym section. The dynamic
8997 object will contain position independent code, so all references
8998 from the dynamic object to this symbol will go through the global
8999 offset table. The dynamic linker will use the .dynsym entry to
9000 determine the address it must put in the global offset table, so
9001 both the dynamic object and the regular object will refer to the
9002 same memory location for the variable. */
9003
9004 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9005 {
861fb55a
DJ
9006 if (htab->is_vxworks)
9007 htab->srelbss->size += sizeof (Elf32_External_Rela);
9008 else
9009 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
9010 h->needs_copy = 1;
9011 }
9012
861fb55a
DJ
9013 /* All relocations against this symbol that could have been made
9014 dynamic will now refer to the local copy instead. */
9015 hmips->possibly_dynamic_relocs = 0;
9016
027297b7 9017 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 9018}
b49e97c9
TS
9019\f
9020/* This function is called after all the input files have been read,
9021 and the input sections have been assigned to output sections. We
9022 check for any mips16 stub sections that we can discard. */
9023
b34976b6 9024bfd_boolean
9719ad41
RS
9025_bfd_mips_elf_always_size_sections (bfd *output_bfd,
9026 struct bfd_link_info *info)
b49e97c9
TS
9027{
9028 asection *ri;
0a44bf69 9029 struct mips_elf_link_hash_table *htab;
861fb55a 9030 struct mips_htab_traverse_info hti;
0a44bf69
RS
9031
9032 htab = mips_elf_hash_table (info);
4dfe6ac6 9033 BFD_ASSERT (htab != NULL);
f4416af6 9034
b49e97c9
TS
9035 /* The .reginfo section has a fixed size. */
9036 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
9037 if (ri != NULL)
9719ad41 9038 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 9039
861fb55a
DJ
9040 hti.info = info;
9041 hti.output_bfd = output_bfd;
9042 hti.error = FALSE;
9043 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9044 mips_elf_check_symbols, &hti);
9045 if (hti.error)
9046 return FALSE;
f4416af6 9047
33bb52fb
RS
9048 return TRUE;
9049}
9050
9051/* If the link uses a GOT, lay it out and work out its size. */
9052
9053static bfd_boolean
9054mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9055{
9056 bfd *dynobj;
9057 asection *s;
9058 struct mips_got_info *g;
33bb52fb
RS
9059 bfd_size_type loadable_size = 0;
9060 bfd_size_type page_gotno;
d7206569 9061 bfd *ibfd;
ab361d49 9062 struct mips_elf_traverse_got_arg tga;
33bb52fb
RS
9063 struct mips_elf_link_hash_table *htab;
9064
9065 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9066 BFD_ASSERT (htab != NULL);
9067
a8028dd0 9068 s = htab->sgot;
f4416af6 9069 if (s == NULL)
b34976b6 9070 return TRUE;
b49e97c9 9071
33bb52fb 9072 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
9073 g = htab->got_info;
9074
861fb55a
DJ
9075 /* Allocate room for the reserved entries. VxWorks always reserves
9076 3 entries; other objects only reserve 2 entries. */
cb22ccf4 9077 BFD_ASSERT (g->assigned_low_gotno == 0);
861fb55a
DJ
9078 if (htab->is_vxworks)
9079 htab->reserved_gotno = 3;
9080 else
9081 htab->reserved_gotno = 2;
9082 g->local_gotno += htab->reserved_gotno;
cb22ccf4 9083 g->assigned_low_gotno = htab->reserved_gotno;
861fb55a 9084
6c42ddb9
RS
9085 /* Decide which symbols need to go in the global part of the GOT and
9086 count the number of reloc-only GOT symbols. */
020d7251 9087 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 9088
13db6b44
RS
9089 if (!mips_elf_resolve_final_got_entries (info, g))
9090 return FALSE;
9091
33bb52fb
RS
9092 /* Calculate the total loadable size of the output. That
9093 will give us the maximum number of GOT_PAGE entries
9094 required. */
d7206569 9095 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
33bb52fb
RS
9096 {
9097 asection *subsection;
5108fc1b 9098
d7206569 9099 for (subsection = ibfd->sections;
33bb52fb
RS
9100 subsection;
9101 subsection = subsection->next)
9102 {
9103 if ((subsection->flags & SEC_ALLOC) == 0)
9104 continue;
9105 loadable_size += ((subsection->size + 0xf)
9106 &~ (bfd_size_type) 0xf);
9107 }
9108 }
f4416af6 9109
0a44bf69 9110 if (htab->is_vxworks)
738e5348 9111 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
9112 relocations against local symbols evaluate to "G", and the EABI does
9113 not include R_MIPS_GOT_PAGE. */
c224138d 9114 page_gotno = 0;
0a44bf69
RS
9115 else
9116 /* Assume there are two loadable segments consisting of contiguous
9117 sections. Is 5 enough? */
c224138d
RS
9118 page_gotno = (loadable_size >> 16) + 5;
9119
13db6b44 9120 /* Choose the smaller of the two page estimates; both are intended to be
c224138d
RS
9121 conservative. */
9122 if (page_gotno > g->page_gotno)
9123 page_gotno = g->page_gotno;
f4416af6 9124
c224138d 9125 g->local_gotno += page_gotno;
cb22ccf4 9126 g->assigned_high_gotno = g->local_gotno - 1;
ab361d49 9127
ab361d49
RS
9128 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9129 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
0f20cc35
DJ
9130 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9131
0a44bf69
RS
9132 /* VxWorks does not support multiple GOTs. It initializes $gp to
9133 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9134 dynamic loader. */
57093f5e 9135 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 9136 {
a8028dd0 9137 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
9138 return FALSE;
9139 }
9140 else
9141 {
d7206569
RS
9142 /* Record that all bfds use G. This also has the effect of freeing
9143 the per-bfd GOTs, which we no longer need. */
9144 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9145 if (mips_elf_bfd_got (ibfd, FALSE))
9146 mips_elf_replace_bfd_got (ibfd, g);
9147 mips_elf_replace_bfd_got (output_bfd, g);
9148
33bb52fb 9149 /* Set up TLS entries. */
0f20cc35 9150 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
72e7511a
RS
9151 tga.info = info;
9152 tga.g = g;
9153 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9154 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9155 if (!tga.g)
9156 return FALSE;
1fd20d70
RS
9157 BFD_ASSERT (g->tls_assigned_gotno
9158 == g->global_gotno + g->local_gotno + g->tls_gotno);
33bb52fb 9159
57093f5e
RS
9160 /* Each VxWorks GOT entry needs an explicit relocation. */
9161 if (htab->is_vxworks && info->shared)
9162 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9163
33bb52fb 9164 /* Allocate room for the TLS relocations. */
ab361d49
RS
9165 if (g->relocs)
9166 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
0f20cc35 9167 }
b49e97c9 9168
b34976b6 9169 return TRUE;
b49e97c9
TS
9170}
9171
33bb52fb
RS
9172/* Estimate the size of the .MIPS.stubs section. */
9173
9174static void
9175mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9176{
9177 struct mips_elf_link_hash_table *htab;
9178 bfd_size_type dynsymcount;
9179
9180 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9181 BFD_ASSERT (htab != NULL);
9182
33bb52fb
RS
9183 if (htab->lazy_stub_count == 0)
9184 return;
9185
9186 /* IRIX rld assumes that a function stub isn't at the end of the .text
9187 section, so add a dummy entry to the end. */
9188 htab->lazy_stub_count++;
9189
9190 /* Get a worst-case estimate of the number of dynamic symbols needed.
9191 At this point, dynsymcount does not account for section symbols
9192 and count_section_dynsyms may overestimate the number that will
9193 be needed. */
9194 dynsymcount = (elf_hash_table (info)->dynsymcount
9195 + count_section_dynsyms (output_bfd, info));
9196
1bbce132
MR
9197 /* Determine the size of one stub entry. There's no disadvantage
9198 from using microMIPS code here, so for the sake of pure-microMIPS
9199 binaries we prefer it whenever there's any microMIPS code in
9200 output produced at all. This has a benefit of stubs being
833794fc
MR
9201 shorter by 4 bytes each too, unless in the insn32 mode. */
9202 if (!MICROMIPS_P (output_bfd))
1bbce132
MR
9203 htab->function_stub_size = (dynsymcount > 0x10000
9204 ? MIPS_FUNCTION_STUB_BIG_SIZE
9205 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
833794fc
MR
9206 else if (htab->insn32)
9207 htab->function_stub_size = (dynsymcount > 0x10000
9208 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9209 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9210 else
9211 htab->function_stub_size = (dynsymcount > 0x10000
9212 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9213 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
33bb52fb
RS
9214
9215 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9216}
9217
1bbce132
MR
9218/* A mips_elf_link_hash_traverse callback for which DATA points to a
9219 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9220 stub, allocate an entry in the stubs section. */
33bb52fb
RS
9221
9222static bfd_boolean
af924177 9223mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 9224{
1bbce132 9225 struct mips_htab_traverse_info *hti = data;
33bb52fb 9226 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9227 struct bfd_link_info *info;
9228 bfd *output_bfd;
9229
9230 info = hti->info;
9231 output_bfd = hti->output_bfd;
9232 htab = mips_elf_hash_table (info);
9233 BFD_ASSERT (htab != NULL);
33bb52fb 9234
33bb52fb
RS
9235 if (h->needs_lazy_stub)
9236 {
1bbce132
MR
9237 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9238 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9239 bfd_vma isa_bit = micromips_p;
9240
9241 BFD_ASSERT (htab->root.dynobj != NULL);
9242 if (h->root.plt.plist == NULL)
9243 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9244 if (h->root.plt.plist == NULL)
9245 {
9246 hti->error = TRUE;
9247 return FALSE;
9248 }
33bb52fb 9249 h->root.root.u.def.section = htab->sstubs;
1bbce132
MR
9250 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9251 h->root.plt.plist->stub_offset = htab->sstubs->size;
9252 h->root.other = other;
33bb52fb
RS
9253 htab->sstubs->size += htab->function_stub_size;
9254 }
9255 return TRUE;
9256}
9257
9258/* Allocate offsets in the stubs section to each symbol that needs one.
9259 Set the final size of the .MIPS.stub section. */
9260
1bbce132 9261static bfd_boolean
33bb52fb
RS
9262mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9263{
1bbce132
MR
9264 bfd *output_bfd = info->output_bfd;
9265 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9266 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9267 bfd_vma isa_bit = micromips_p;
33bb52fb 9268 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9269 struct mips_htab_traverse_info hti;
9270 struct elf_link_hash_entry *h;
9271 bfd *dynobj;
33bb52fb
RS
9272
9273 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9274 BFD_ASSERT (htab != NULL);
9275
33bb52fb 9276 if (htab->lazy_stub_count == 0)
1bbce132 9277 return TRUE;
33bb52fb
RS
9278
9279 htab->sstubs->size = 0;
1bbce132
MR
9280 hti.info = info;
9281 hti.output_bfd = output_bfd;
9282 hti.error = FALSE;
9283 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9284 if (hti.error)
9285 return FALSE;
33bb52fb
RS
9286 htab->sstubs->size += htab->function_stub_size;
9287 BFD_ASSERT (htab->sstubs->size
9288 == htab->lazy_stub_count * htab->function_stub_size);
1bbce132
MR
9289
9290 dynobj = elf_hash_table (info)->dynobj;
9291 BFD_ASSERT (dynobj != NULL);
9292 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9293 if (h == NULL)
9294 return FALSE;
9295 h->root.u.def.value = isa_bit;
9296 h->other = other;
9297 h->type = STT_FUNC;
9298
9299 return TRUE;
9300}
9301
9302/* A mips_elf_link_hash_traverse callback for which DATA points to a
9303 bfd_link_info. If H uses the address of a PLT entry as the value
9304 of the symbol, then set the entry in the symbol table now. Prefer
9305 a standard MIPS PLT entry. */
9306
9307static bfd_boolean
9308mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9309{
9310 struct bfd_link_info *info = data;
9311 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9312 struct mips_elf_link_hash_table *htab;
9313 unsigned int other;
9314 bfd_vma isa_bit;
9315 bfd_vma val;
9316
9317 htab = mips_elf_hash_table (info);
9318 BFD_ASSERT (htab != NULL);
9319
9320 if (h->use_plt_entry)
9321 {
9322 BFD_ASSERT (h->root.plt.plist != NULL);
9323 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9324 || h->root.plt.plist->comp_offset != MINUS_ONE);
9325
9326 val = htab->plt_header_size;
9327 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9328 {
9329 isa_bit = 0;
9330 val += h->root.plt.plist->mips_offset;
9331 other = 0;
9332 }
9333 else
9334 {
9335 isa_bit = 1;
9336 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9337 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9338 }
9339 val += isa_bit;
9340 /* For VxWorks, point at the PLT load stub rather than the lazy
9341 resolution stub; this stub will become the canonical function
9342 address. */
9343 if (htab->is_vxworks)
9344 val += 8;
9345
9346 h->root.root.u.def.section = htab->splt;
9347 h->root.root.u.def.value = val;
9348 h->root.other = other;
9349 }
9350
9351 return TRUE;
33bb52fb
RS
9352}
9353
b49e97c9
TS
9354/* Set the sizes of the dynamic sections. */
9355
b34976b6 9356bfd_boolean
9719ad41
RS
9357_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9358 struct bfd_link_info *info)
b49e97c9
TS
9359{
9360 bfd *dynobj;
861fb55a 9361 asection *s, *sreldyn;
b34976b6 9362 bfd_boolean reltext;
0a44bf69 9363 struct mips_elf_link_hash_table *htab;
b49e97c9 9364
0a44bf69 9365 htab = mips_elf_hash_table (info);
4dfe6ac6 9366 BFD_ASSERT (htab != NULL);
b49e97c9
TS
9367 dynobj = elf_hash_table (info)->dynobj;
9368 BFD_ASSERT (dynobj != NULL);
9369
9370 if (elf_hash_table (info)->dynamic_sections_created)
9371 {
9372 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 9373 if (info->executable)
b49e97c9 9374 {
3d4d4302 9375 s = bfd_get_linker_section (dynobj, ".interp");
b49e97c9 9376 BFD_ASSERT (s != NULL);
eea6121a 9377 s->size
b49e97c9
TS
9378 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9379 s->contents
9380 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9381 }
861fb55a 9382
1bbce132
MR
9383 /* Figure out the size of the PLT header if we know that we
9384 are using it. For the sake of cache alignment always use
9385 a standard header whenever any standard entries are present
9386 even if microMIPS entries are present as well. This also
9387 lets the microMIPS header rely on the value of $v0 only set
9388 by microMIPS entries, for a small size reduction.
9389
9390 Set symbol table entry values for symbols that use the
9391 address of their PLT entry now that we can calculate it.
9392
9393 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9394 haven't already in _bfd_elf_create_dynamic_sections. */
9395 if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
861fb55a 9396 {
1bbce132
MR
9397 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9398 && !htab->plt_mips_offset);
9399 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9400 bfd_vma isa_bit = micromips_p;
861fb55a 9401 struct elf_link_hash_entry *h;
1bbce132 9402 bfd_vma size;
861fb55a
DJ
9403
9404 BFD_ASSERT (htab->use_plts_and_copy_relocs);
1bbce132
MR
9405 BFD_ASSERT (htab->sgotplt->size == 0);
9406 BFD_ASSERT (htab->splt->size == 0);
9407
9408 if (htab->is_vxworks && info->shared)
9409 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9410 else if (htab->is_vxworks)
9411 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9412 else if (ABI_64_P (output_bfd))
9413 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9414 else if (ABI_N32_P (output_bfd))
9415 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9416 else if (!micromips_p)
9417 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
833794fc
MR
9418 else if (htab->insn32)
9419 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
1bbce132
MR
9420 else
9421 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
861fb55a 9422
1bbce132
MR
9423 htab->plt_header_is_comp = micromips_p;
9424 htab->plt_header_size = size;
9425 htab->splt->size = (size
9426 + htab->plt_mips_offset
9427 + htab->plt_comp_offset);
9428 htab->sgotplt->size = (htab->plt_got_index
9429 * MIPS_ELF_GOT_SIZE (dynobj));
9430
9431 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9432
9433 if (htab->root.hplt == NULL)
9434 {
9435 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9436 "_PROCEDURE_LINKAGE_TABLE_");
9437 htab->root.hplt = h;
9438 if (h == NULL)
9439 return FALSE;
9440 }
9441
9442 h = htab->root.hplt;
9443 h->root.u.def.value = isa_bit;
9444 h->other = other;
861fb55a
DJ
9445 h->type = STT_FUNC;
9446 }
9447 }
4e41d0d7 9448
9a59ad6b 9449 /* Allocate space for global sym dynamic relocs. */
2c3fc389 9450 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9a59ad6b 9451
33bb52fb
RS
9452 mips_elf_estimate_stub_size (output_bfd, info);
9453
9454 if (!mips_elf_lay_out_got (output_bfd, info))
9455 return FALSE;
9456
9457 mips_elf_lay_out_lazy_stubs (info);
9458
b49e97c9
TS
9459 /* The check_relocs and adjust_dynamic_symbol entry points have
9460 determined the sizes of the various dynamic sections. Allocate
9461 memory for them. */
b34976b6 9462 reltext = FALSE;
b49e97c9
TS
9463 for (s = dynobj->sections; s != NULL; s = s->next)
9464 {
9465 const char *name;
b49e97c9
TS
9466
9467 /* It's OK to base decisions on the section name, because none
9468 of the dynobj section names depend upon the input files. */
9469 name = bfd_get_section_name (dynobj, s);
9470
9471 if ((s->flags & SEC_LINKER_CREATED) == 0)
9472 continue;
9473
0112cd26 9474 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 9475 {
c456f082 9476 if (s->size != 0)
b49e97c9
TS
9477 {
9478 const char *outname;
9479 asection *target;
9480
9481 /* If this relocation section applies to a read only
9482 section, then we probably need a DT_TEXTREL entry.
0a44bf69 9483 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
9484 assert a DT_TEXTREL entry rather than testing whether
9485 there exists a relocation to a read only section or
9486 not. */
9487 outname = bfd_get_section_name (output_bfd,
9488 s->output_section);
9489 target = bfd_get_section_by_name (output_bfd, outname + 4);
9490 if ((target != NULL
9491 && (target->flags & SEC_READONLY) != 0
9492 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 9493 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 9494 reltext = TRUE;
b49e97c9
TS
9495
9496 /* We use the reloc_count field as a counter if we need
9497 to copy relocs into the output file. */
0a44bf69 9498 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 9499 s->reloc_count = 0;
f4416af6
AO
9500
9501 /* If combreloc is enabled, elf_link_sort_relocs() will
9502 sort relocations, but in a different way than we do,
9503 and before we're done creating relocations. Also, it
9504 will move them around between input sections'
9505 relocation's contents, so our sorting would be
9506 broken, so don't let it run. */
9507 info->combreloc = 0;
b49e97c9
TS
9508 }
9509 }
b49e97c9
TS
9510 else if (! info->shared
9511 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 9512 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 9513 {
5108fc1b 9514 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 9515 rtld to contain a pointer to the _r_debug structure. */
b4082c70 9516 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
b49e97c9
TS
9517 }
9518 else if (SGI_COMPAT (output_bfd)
0112cd26 9519 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 9520 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
9521 else if (s == htab->splt)
9522 {
9523 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
9524 room for an extra nop to fill the delay slot. This is
9525 for CPUs without load interlocking. */
9526 if (! LOAD_INTERLOCKS_P (output_bfd)
9527 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
9528 s->size += 4;
9529 }
0112cd26 9530 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 9531 && s != htab->sgot
0a44bf69 9532 && s != htab->sgotplt
861fb55a
DJ
9533 && s != htab->sstubs
9534 && s != htab->sdynbss)
b49e97c9
TS
9535 {
9536 /* It's not one of our sections, so don't allocate space. */
9537 continue;
9538 }
9539
c456f082 9540 if (s->size == 0)
b49e97c9 9541 {
8423293d 9542 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
9543 continue;
9544 }
9545
c456f082
AM
9546 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9547 continue;
9548
b49e97c9 9549 /* Allocate memory for the section contents. */
eea6121a 9550 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 9551 if (s->contents == NULL)
b49e97c9
TS
9552 {
9553 bfd_set_error (bfd_error_no_memory);
b34976b6 9554 return FALSE;
b49e97c9
TS
9555 }
9556 }
9557
9558 if (elf_hash_table (info)->dynamic_sections_created)
9559 {
9560 /* Add some entries to the .dynamic section. We fill in the
9561 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9562 must add the entries now so that we get the correct size for
5750dcec 9563 the .dynamic section. */
af5978fb
RS
9564
9565 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec 9566 DT_MIPS_RLD_MAP entry. This must come first because glibc
6e6be592
MR
9567 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9568 may only look at the first one they see. */
af5978fb
RS
9569 if (!info->shared
9570 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9571 return FALSE;
b49e97c9 9572
5750dcec
DJ
9573 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9574 used by the debugger. */
9575 if (info->executable
9576 && !SGI_COMPAT (output_bfd)
9577 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9578 return FALSE;
9579
0a44bf69 9580 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
9581 info->flags |= DF_TEXTREL;
9582
9583 if ((info->flags & DF_TEXTREL) != 0)
9584 {
9585 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 9586 return FALSE;
943284cc
DJ
9587
9588 /* Clear the DF_TEXTREL flag. It will be set again if we
9589 write out an actual text relocation; we may not, because
9590 at this point we do not know whether e.g. any .eh_frame
9591 absolute relocations have been converted to PC-relative. */
9592 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
9593 }
9594
9595 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 9596 return FALSE;
b49e97c9 9597
861fb55a 9598 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 9599 if (htab->is_vxworks)
b49e97c9 9600 {
0a44bf69
RS
9601 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9602 use any of the DT_MIPS_* tags. */
861fb55a 9603 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
9604 {
9605 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9606 return FALSE;
b49e97c9 9607
0a44bf69
RS
9608 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9609 return FALSE;
b49e97c9 9610
0a44bf69
RS
9611 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9612 return FALSE;
9613 }
b49e97c9 9614 }
0a44bf69
RS
9615 else
9616 {
861fb55a 9617 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
9618 {
9619 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9620 return FALSE;
b49e97c9 9621
0a44bf69
RS
9622 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9623 return FALSE;
b49e97c9 9624
0a44bf69
RS
9625 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9626 return FALSE;
9627 }
b49e97c9 9628
0a44bf69
RS
9629 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9630 return FALSE;
b49e97c9 9631
0a44bf69
RS
9632 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9633 return FALSE;
b49e97c9 9634
0a44bf69
RS
9635 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9636 return FALSE;
b49e97c9 9637
0a44bf69
RS
9638 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9639 return FALSE;
b49e97c9 9640
0a44bf69
RS
9641 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9642 return FALSE;
b49e97c9 9643
0a44bf69
RS
9644 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9645 return FALSE;
b49e97c9 9646
0a44bf69
RS
9647 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9648 return FALSE;
9649
9650 if (IRIX_COMPAT (dynobj) == ict_irix5
9651 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9652 return FALSE;
9653
9654 if (IRIX_COMPAT (dynobj) == ict_irix6
9655 && (bfd_get_section_by_name
af0edeb8 9656 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
0a44bf69
RS
9657 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9658 return FALSE;
9659 }
861fb55a
DJ
9660 if (htab->splt->size > 0)
9661 {
9662 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9663 return FALSE;
9664
9665 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9666 return FALSE;
9667
9668 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9669 return FALSE;
9670
9671 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9672 return FALSE;
9673 }
7a2b07ff
NS
9674 if (htab->is_vxworks
9675 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9676 return FALSE;
b49e97c9
TS
9677 }
9678
b34976b6 9679 return TRUE;
b49e97c9
TS
9680}
9681\f
81d43bff
RS
9682/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9683 Adjust its R_ADDEND field so that it is correct for the output file.
9684 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9685 and sections respectively; both use symbol indexes. */
9686
9687static void
9688mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9689 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9690 asection **local_sections, Elf_Internal_Rela *rel)
9691{
9692 unsigned int r_type, r_symndx;
9693 Elf_Internal_Sym *sym;
9694 asection *sec;
9695
020d7251 9696 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
9697 {
9698 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
df58fc94 9699 if (gprel16_reloc_p (r_type)
81d43bff 9700 || r_type == R_MIPS_GPREL32
df58fc94 9701 || literal_reloc_p (r_type))
81d43bff
RS
9702 {
9703 rel->r_addend += _bfd_get_gp_value (input_bfd);
9704 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9705 }
9706
9707 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9708 sym = local_syms + r_symndx;
9709
9710 /* Adjust REL's addend to account for section merging. */
9711 if (!info->relocatable)
9712 {
9713 sec = local_sections[r_symndx];
9714 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9715 }
9716
9717 /* This would normally be done by the rela_normal code in elflink.c. */
9718 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9719 rel->r_addend += local_sections[r_symndx]->output_offset;
9720 }
9721}
9722
545fd46b
MR
9723/* Handle relocations against symbols from removed linkonce sections,
9724 or sections discarded by a linker script. We use this wrapper around
9725 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9726 on 64-bit ELF targets. In this case for any relocation handled, which
9727 always be the first in a triplet, the remaining two have to be processed
9728 together with the first, even if they are R_MIPS_NONE. It is the symbol
9729 index referred by the first reloc that applies to all the three and the
9730 remaining two never refer to an object symbol. And it is the final
9731 relocation (the last non-null one) that determines the output field of
9732 the whole relocation so retrieve the corresponding howto structure for
9733 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9734
9735 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9736 and therefore requires to be pasted in a loop. It also defines a block
9737 and does not protect any of its arguments, hence the extra brackets. */
9738
9739static void
9740mips_reloc_against_discarded_section (bfd *output_bfd,
9741 struct bfd_link_info *info,
9742 bfd *input_bfd, asection *input_section,
9743 Elf_Internal_Rela **rel,
9744 const Elf_Internal_Rela **relend,
9745 bfd_boolean rel_reloc,
9746 reloc_howto_type *howto,
9747 bfd_byte *contents)
9748{
9749 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9750 int count = bed->s->int_rels_per_ext_rel;
9751 unsigned int r_type;
9752 int i;
9753
9754 for (i = count - 1; i > 0; i--)
9755 {
9756 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9757 if (r_type != R_MIPS_NONE)
9758 {
9759 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9760 break;
9761 }
9762 }
9763 do
9764 {
9765 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9766 (*rel), count, (*relend),
9767 howto, i, contents);
9768 }
9769 while (0);
9770}
9771
b49e97c9
TS
9772/* Relocate a MIPS ELF section. */
9773
b34976b6 9774bfd_boolean
9719ad41
RS
9775_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9776 bfd *input_bfd, asection *input_section,
9777 bfd_byte *contents, Elf_Internal_Rela *relocs,
9778 Elf_Internal_Sym *local_syms,
9779 asection **local_sections)
b49e97c9
TS
9780{
9781 Elf_Internal_Rela *rel;
9782 const Elf_Internal_Rela *relend;
9783 bfd_vma addend = 0;
b34976b6 9784 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 9785 const struct elf_backend_data *bed;
b49e97c9
TS
9786
9787 bed = get_elf_backend_data (output_bfd);
9788 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9789 for (rel = relocs; rel < relend; ++rel)
9790 {
9791 const char *name;
c9adbffe 9792 bfd_vma value = 0;
b49e97c9 9793 reloc_howto_type *howto;
ad3d9127 9794 bfd_boolean cross_mode_jump_p = FALSE;
b34976b6 9795 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 9796 REL relocation. */
b34976b6 9797 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 9798 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 9799 const char *msg;
ab96bf03
AM
9800 unsigned long r_symndx;
9801 asection *sec;
749b8d9d
L
9802 Elf_Internal_Shdr *symtab_hdr;
9803 struct elf_link_hash_entry *h;
d4730f92 9804 bfd_boolean rel_reloc;
b49e97c9 9805
d4730f92
BS
9806 rel_reloc = (NEWABI_P (input_bfd)
9807 && mips_elf_rel_relocation_p (input_bfd, input_section,
9808 relocs, rel));
b49e97c9 9809 /* Find the relocation howto for this relocation. */
d4730f92 9810 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
ab96bf03
AM
9811
9812 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 9813 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 9814 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
9815 {
9816 sec = local_sections[r_symndx];
9817 h = NULL;
9818 }
ab96bf03
AM
9819 else
9820 {
ab96bf03 9821 unsigned long extsymoff;
ab96bf03 9822
ab96bf03
AM
9823 extsymoff = 0;
9824 if (!elf_bad_symtab (input_bfd))
9825 extsymoff = symtab_hdr->sh_info;
9826 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9827 while (h->root.type == bfd_link_hash_indirect
9828 || h->root.type == bfd_link_hash_warning)
9829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9830
9831 sec = NULL;
9832 if (h->root.type == bfd_link_hash_defined
9833 || h->root.type == bfd_link_hash_defweak)
9834 sec = h->root.u.def.section;
9835 }
9836
dbaa2011 9837 if (sec != NULL && discarded_section (sec))
545fd46b
MR
9838 {
9839 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9840 input_section, &rel, &relend,
9841 rel_reloc, howto, contents);
9842 continue;
9843 }
ab96bf03 9844
4a14403c 9845 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
9846 {
9847 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9848 64-bit code, but make sure all their addresses are in the
9849 lowermost or uppermost 32-bit section of the 64-bit address
9850 space. Thus, when they use an R_MIPS_64 they mean what is
9851 usually meant by R_MIPS_32, with the exception that the
9852 stored value is sign-extended to 64 bits. */
b34976b6 9853 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
9854
9855 /* On big-endian systems, we need to lie about the position
9856 of the reloc. */
9857 if (bfd_big_endian (input_bfd))
9858 rel->r_offset += 4;
9859 }
b49e97c9
TS
9860
9861 if (!use_saved_addend_p)
9862 {
b49e97c9
TS
9863 /* If these relocations were originally of the REL variety,
9864 we must pull the addend out of the field that will be
9865 relocated. Otherwise, we simply use the contents of the
c224138d
RS
9866 RELA relocation. */
9867 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9868 relocs, rel))
b49e97c9 9869 {
b34976b6 9870 rela_relocation_p = FALSE;
c224138d
RS
9871 addend = mips_elf_read_rel_addend (input_bfd, rel,
9872 howto, contents);
738e5348
RS
9873 if (hi16_reloc_p (r_type)
9874 || (got16_reloc_p (r_type)
b49e97c9 9875 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 9876 local_sections)))
b49e97c9 9877 {
c224138d
RS
9878 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9879 contents, &addend))
749b8d9d 9880 {
749b8d9d
L
9881 if (h)
9882 name = h->root.root.string;
9883 else
9884 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9885 local_syms + r_symndx,
9886 sec);
9887 (*_bfd_error_handler)
9888 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9889 input_bfd, input_section, name, howto->name,
9890 rel->r_offset);
749b8d9d 9891 }
b49e97c9 9892 }
30ac9238
RS
9893 else
9894 addend <<= howto->rightshift;
b49e97c9
TS
9895 }
9896 else
9897 addend = rel->r_addend;
81d43bff
RS
9898 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9899 local_syms, local_sections, rel);
b49e97c9
TS
9900 }
9901
1049f94e 9902 if (info->relocatable)
b49e97c9 9903 {
4a14403c 9904 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9905 && bfd_big_endian (input_bfd))
9906 rel->r_offset -= 4;
9907
81d43bff 9908 if (!rela_relocation_p && rel->r_addend)
5a659663 9909 {
81d43bff 9910 addend += rel->r_addend;
738e5348 9911 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9912 addend = mips_elf_high (addend);
9913 else if (r_type == R_MIPS_HIGHER)
9914 addend = mips_elf_higher (addend);
9915 else if (r_type == R_MIPS_HIGHEST)
9916 addend = mips_elf_highest (addend);
30ac9238
RS
9917 else
9918 addend >>= howto->rightshift;
b49e97c9 9919
30ac9238
RS
9920 /* We use the source mask, rather than the destination
9921 mask because the place to which we are writing will be
9922 source of the addend in the final link. */
b49e97c9
TS
9923 addend &= howto->src_mask;
9924
5a659663 9925 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9926 /* See the comment above about using R_MIPS_64 in the 32-bit
9927 ABI. Here, we need to update the addend. It would be
9928 possible to get away with just using the R_MIPS_32 reloc
9929 but for endianness. */
9930 {
9931 bfd_vma sign_bits;
9932 bfd_vma low_bits;
9933 bfd_vma high_bits;
9934
9935 if (addend & ((bfd_vma) 1 << 31))
9936#ifdef BFD64
9937 sign_bits = ((bfd_vma) 1 << 32) - 1;
9938#else
9939 sign_bits = -1;
9940#endif
9941 else
9942 sign_bits = 0;
9943
9944 /* If we don't know that we have a 64-bit type,
9945 do two separate stores. */
9946 if (bfd_big_endian (input_bfd))
9947 {
9948 /* Store the sign-bits (which are most significant)
9949 first. */
9950 low_bits = sign_bits;
9951 high_bits = addend;
9952 }
9953 else
9954 {
9955 low_bits = addend;
9956 high_bits = sign_bits;
9957 }
9958 bfd_put_32 (input_bfd, low_bits,
9959 contents + rel->r_offset);
9960 bfd_put_32 (input_bfd, high_bits,
9961 contents + rel->r_offset + 4);
9962 continue;
9963 }
9964
9965 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9966 input_bfd, input_section,
b34976b6
AM
9967 contents, FALSE))
9968 return FALSE;
b49e97c9
TS
9969 }
9970
9971 /* Go on to the next relocation. */
9972 continue;
9973 }
9974
9975 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9976 relocations for the same offset. In that case we are
9977 supposed to treat the output of each relocation as the addend
9978 for the next. */
9979 if (rel + 1 < relend
9980 && rel->r_offset == rel[1].r_offset
9981 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9982 use_saved_addend_p = TRUE;
b49e97c9 9983 else
b34976b6 9984 use_saved_addend_p = FALSE;
b49e97c9
TS
9985
9986 /* Figure out what value we are supposed to relocate. */
9987 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9988 input_section, info, rel,
9989 addend, howto, local_syms,
9990 local_sections, &value,
38a7df63 9991 &name, &cross_mode_jump_p,
bce03d3d 9992 use_saved_addend_p))
b49e97c9
TS
9993 {
9994 case bfd_reloc_continue:
9995 /* There's nothing to do. */
9996 continue;
9997
9998 case bfd_reloc_undefined:
9999 /* mips_elf_calculate_relocation already called the
10000 undefined_symbol callback. There's no real point in
10001 trying to perform the relocation at this point, so we
10002 just skip ahead to the next relocation. */
10003 continue;
10004
10005 case bfd_reloc_notsupported:
10006 msg = _("internal error: unsupported relocation error");
10007 info->callbacks->warning
10008 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 10009 return FALSE;
b49e97c9
TS
10010
10011 case bfd_reloc_overflow:
10012 if (use_saved_addend_p)
10013 /* Ignore overflow until we reach the last relocation for
10014 a given location. */
10015 ;
10016 else
10017 {
0e53d9da
AN
10018 struct mips_elf_link_hash_table *htab;
10019
10020 htab = mips_elf_hash_table (info);
4dfe6ac6 10021 BFD_ASSERT (htab != NULL);
b49e97c9 10022 BFD_ASSERT (name != NULL);
0e53d9da 10023 if (!htab->small_data_overflow_reported
9684f078 10024 && (gprel16_reloc_p (howto->type)
df58fc94 10025 || literal_reloc_p (howto->type)))
0e53d9da 10026 {
91d6fa6a
NC
10027 msg = _("small-data section exceeds 64KB;"
10028 " lower small-data size limit (see option -G)");
0e53d9da
AN
10029
10030 htab->small_data_overflow_reported = TRUE;
10031 (*info->callbacks->einfo) ("%P: %s\n", msg);
10032 }
b49e97c9 10033 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 10034 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 10035 input_bfd, input_section, rel->r_offset)))
b34976b6 10036 return FALSE;
b49e97c9
TS
10037 }
10038 break;
10039
10040 case bfd_reloc_ok:
10041 break;
10042
df58fc94
RS
10043 case bfd_reloc_outofrange:
10044 if (jal_reloc_p (howto->type))
10045 {
10046 msg = _("JALX to a non-word-aligned address");
10047 info->callbacks->warning
10048 (info, msg, name, input_bfd, input_section, rel->r_offset);
10049 return FALSE;
10050 }
10051 /* Fall through. */
10052
b49e97c9
TS
10053 default:
10054 abort ();
10055 break;
10056 }
10057
10058 /* If we've got another relocation for the address, keep going
10059 until we reach the last one. */
10060 if (use_saved_addend_p)
10061 {
10062 addend = value;
10063 continue;
10064 }
10065
4a14403c 10066 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10067 /* See the comment above about using R_MIPS_64 in the 32-bit
10068 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10069 that calculated the right value. Now, however, we
10070 sign-extend the 32-bit result to 64-bits, and store it as a
10071 64-bit value. We are especially generous here in that we
10072 go to extreme lengths to support this usage on systems with
10073 only a 32-bit VMA. */
10074 {
10075 bfd_vma sign_bits;
10076 bfd_vma low_bits;
10077 bfd_vma high_bits;
10078
10079 if (value & ((bfd_vma) 1 << 31))
10080#ifdef BFD64
10081 sign_bits = ((bfd_vma) 1 << 32) - 1;
10082#else
10083 sign_bits = -1;
10084#endif
10085 else
10086 sign_bits = 0;
10087
10088 /* If we don't know that we have a 64-bit type,
10089 do two separate stores. */
10090 if (bfd_big_endian (input_bfd))
10091 {
10092 /* Undo what we did above. */
10093 rel->r_offset -= 4;
10094 /* Store the sign-bits (which are most significant)
10095 first. */
10096 low_bits = sign_bits;
10097 high_bits = value;
10098 }
10099 else
10100 {
10101 low_bits = value;
10102 high_bits = sign_bits;
10103 }
10104 bfd_put_32 (input_bfd, low_bits,
10105 contents + rel->r_offset);
10106 bfd_put_32 (input_bfd, high_bits,
10107 contents + rel->r_offset + 4);
10108 continue;
10109 }
10110
10111 /* Actually perform the relocation. */
10112 if (! mips_elf_perform_relocation (info, howto, rel, value,
10113 input_bfd, input_section,
38a7df63 10114 contents, cross_mode_jump_p))
b34976b6 10115 return FALSE;
b49e97c9
TS
10116 }
10117
b34976b6 10118 return TRUE;
b49e97c9
TS
10119}
10120\f
861fb55a
DJ
10121/* A function that iterates over each entry in la25_stubs and fills
10122 in the code for each one. DATA points to a mips_htab_traverse_info. */
10123
10124static int
10125mips_elf_create_la25_stub (void **slot, void *data)
10126{
10127 struct mips_htab_traverse_info *hti;
10128 struct mips_elf_link_hash_table *htab;
10129 struct mips_elf_la25_stub *stub;
10130 asection *s;
10131 bfd_byte *loc;
10132 bfd_vma offset, target, target_high, target_low;
10133
10134 stub = (struct mips_elf_la25_stub *) *slot;
10135 hti = (struct mips_htab_traverse_info *) data;
10136 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 10137 BFD_ASSERT (htab != NULL);
861fb55a
DJ
10138
10139 /* Create the section contents, if we haven't already. */
10140 s = stub->stub_section;
10141 loc = s->contents;
10142 if (loc == NULL)
10143 {
10144 loc = bfd_malloc (s->size);
10145 if (loc == NULL)
10146 {
10147 hti->error = TRUE;
10148 return FALSE;
10149 }
10150 s->contents = loc;
10151 }
10152
10153 /* Work out where in the section this stub should go. */
10154 offset = stub->offset;
10155
10156 /* Work out the target address. */
8f0c309a
CLT
10157 target = mips_elf_get_la25_target (stub, &s);
10158 target += s->output_section->vma + s->output_offset;
10159
861fb55a
DJ
10160 target_high = ((target + 0x8000) >> 16) & 0xffff;
10161 target_low = (target & 0xffff);
10162
10163 if (stub->stub_section != htab->strampoline)
10164 {
df58fc94 10165 /* This is a simple LUI/ADDIU stub. Zero out the beginning
861fb55a
DJ
10166 of the section and write the two instructions at the end. */
10167 memset (loc, 0, offset);
10168 loc += offset;
df58fc94
RS
10169 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10170 {
d21911ea
MR
10171 bfd_put_micromips_32 (hti->output_bfd,
10172 LA25_LUI_MICROMIPS (target_high),
10173 loc);
10174 bfd_put_micromips_32 (hti->output_bfd,
10175 LA25_ADDIU_MICROMIPS (target_low),
10176 loc + 4);
df58fc94
RS
10177 }
10178 else
10179 {
10180 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10181 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10182 }
861fb55a
DJ
10183 }
10184 else
10185 {
10186 /* This is trampoline. */
10187 loc += offset;
df58fc94
RS
10188 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10189 {
d21911ea
MR
10190 bfd_put_micromips_32 (hti->output_bfd,
10191 LA25_LUI_MICROMIPS (target_high), loc);
10192 bfd_put_micromips_32 (hti->output_bfd,
10193 LA25_J_MICROMIPS (target), loc + 4);
10194 bfd_put_micromips_32 (hti->output_bfd,
10195 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
df58fc94
RS
10196 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10197 }
10198 else
10199 {
10200 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10201 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10202 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10203 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10204 }
861fb55a
DJ
10205 }
10206 return TRUE;
10207}
10208
b49e97c9
TS
10209/* If NAME is one of the special IRIX6 symbols defined by the linker,
10210 adjust it appropriately now. */
10211
10212static void
9719ad41
RS
10213mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10214 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
10215{
10216 /* The linker script takes care of providing names and values for
10217 these, but we must place them into the right sections. */
10218 static const char* const text_section_symbols[] = {
10219 "_ftext",
10220 "_etext",
10221 "__dso_displacement",
10222 "__elf_header",
10223 "__program_header_table",
10224 NULL
10225 };
10226
10227 static const char* const data_section_symbols[] = {
10228 "_fdata",
10229 "_edata",
10230 "_end",
10231 "_fbss",
10232 NULL
10233 };
10234
10235 const char* const *p;
10236 int i;
10237
10238 for (i = 0; i < 2; ++i)
10239 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10240 *p;
10241 ++p)
10242 if (strcmp (*p, name) == 0)
10243 {
10244 /* All of these symbols are given type STT_SECTION by the
10245 IRIX6 linker. */
10246 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 10247 sym->st_other = STO_PROTECTED;
b49e97c9
TS
10248
10249 /* The IRIX linker puts these symbols in special sections. */
10250 if (i == 0)
10251 sym->st_shndx = SHN_MIPS_TEXT;
10252 else
10253 sym->st_shndx = SHN_MIPS_DATA;
10254
10255 break;
10256 }
10257}
10258
10259/* Finish up dynamic symbol handling. We set the contents of various
10260 dynamic sections here. */
10261
b34976b6 10262bfd_boolean
9719ad41
RS
10263_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10264 struct bfd_link_info *info,
10265 struct elf_link_hash_entry *h,
10266 Elf_Internal_Sym *sym)
b49e97c9
TS
10267{
10268 bfd *dynobj;
b49e97c9 10269 asection *sgot;
f4416af6 10270 struct mips_got_info *g, *gg;
b49e97c9 10271 const char *name;
3d6746ca 10272 int idx;
5108fc1b 10273 struct mips_elf_link_hash_table *htab;
738e5348 10274 struct mips_elf_link_hash_entry *hmips;
b49e97c9 10275
5108fc1b 10276 htab = mips_elf_hash_table (info);
4dfe6ac6 10277 BFD_ASSERT (htab != NULL);
b49e97c9 10278 dynobj = elf_hash_table (info)->dynobj;
738e5348 10279 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 10280
861fb55a
DJ
10281 BFD_ASSERT (!htab->is_vxworks);
10282
1bbce132
MR
10283 if (h->plt.plist != NULL
10284 && (h->plt.plist->mips_offset != MINUS_ONE
10285 || h->plt.plist->comp_offset != MINUS_ONE))
861fb55a
DJ
10286 {
10287 /* We've decided to create a PLT entry for this symbol. */
10288 bfd_byte *loc;
1bbce132 10289 bfd_vma header_address, got_address;
861fb55a 10290 bfd_vma got_address_high, got_address_low, load;
1bbce132
MR
10291 bfd_vma got_index;
10292 bfd_vma isa_bit;
10293
10294 got_index = h->plt.plist->gotplt_index;
861fb55a
DJ
10295
10296 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10297 BFD_ASSERT (h->dynindx != -1);
10298 BFD_ASSERT (htab->splt != NULL);
1bbce132 10299 BFD_ASSERT (got_index != MINUS_ONE);
861fb55a
DJ
10300 BFD_ASSERT (!h->def_regular);
10301
10302 /* Calculate the address of the PLT header. */
1bbce132 10303 isa_bit = htab->plt_header_is_comp;
861fb55a 10304 header_address = (htab->splt->output_section->vma
1bbce132 10305 + htab->splt->output_offset + isa_bit);
861fb55a
DJ
10306
10307 /* Calculate the address of the .got.plt entry. */
10308 got_address = (htab->sgotplt->output_section->vma
10309 + htab->sgotplt->output_offset
1bbce132
MR
10310 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10311
861fb55a
DJ
10312 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10313 got_address_low = got_address & 0xffff;
10314
10315 /* Initially point the .got.plt entry at the PLT header. */
1bbce132 10316 loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
861fb55a
DJ
10317 if (ABI_64_P (output_bfd))
10318 bfd_put_64 (output_bfd, header_address, loc);
10319 else
10320 bfd_put_32 (output_bfd, header_address, loc);
10321
1bbce132
MR
10322 /* Now handle the PLT itself. First the standard entry (the order
10323 does not matter, we just have to pick one). */
10324 if (h->plt.plist->mips_offset != MINUS_ONE)
10325 {
10326 const bfd_vma *plt_entry;
10327 bfd_vma plt_offset;
861fb55a 10328
1bbce132 10329 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
861fb55a 10330
1bbce132 10331 BFD_ASSERT (plt_offset <= htab->splt->size);
6d30f5b2 10332
1bbce132
MR
10333 /* Find out where the .plt entry should go. */
10334 loc = htab->splt->contents + plt_offset;
10335
10336 /* Pick the load opcode. */
10337 load = MIPS_ELF_LOAD_WORD (output_bfd);
10338
10339 /* Fill in the PLT entry itself. */
10340 plt_entry = mips_exec_plt_entry;
10341 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10342 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10343 loc + 4);
10344
10345 if (! LOAD_INTERLOCKS_P (output_bfd))
10346 {
10347 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10348 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10349 }
10350 else
10351 {
10352 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10353 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10354 loc + 12);
10355 }
6d30f5b2 10356 }
1bbce132
MR
10357
10358 /* Now the compressed entry. They come after any standard ones. */
10359 if (h->plt.plist->comp_offset != MINUS_ONE)
6d30f5b2 10360 {
1bbce132
MR
10361 bfd_vma plt_offset;
10362
10363 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10364 + h->plt.plist->comp_offset);
10365
10366 BFD_ASSERT (plt_offset <= htab->splt->size);
10367
10368 /* Find out where the .plt entry should go. */
10369 loc = htab->splt->contents + plt_offset;
10370
10371 /* Fill in the PLT entry itself. */
833794fc
MR
10372 if (!MICROMIPS_P (output_bfd))
10373 {
10374 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10375
10376 bfd_put_16 (output_bfd, plt_entry[0], loc);
10377 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10378 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10379 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10380 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10381 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10382 bfd_put_32 (output_bfd, got_address, loc + 12);
10383 }
10384 else if (htab->insn32)
10385 {
10386 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10387
10388 bfd_put_16 (output_bfd, plt_entry[0], loc);
10389 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10390 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10391 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10392 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10393 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10394 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10395 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10396 }
10397 else
1bbce132
MR
10398 {
10399 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10400 bfd_signed_vma gotpc_offset;
10401 bfd_vma loc_address;
10402
10403 BFD_ASSERT (got_address % 4 == 0);
10404
10405 loc_address = (htab->splt->output_section->vma
10406 + htab->splt->output_offset + plt_offset);
10407 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10408
10409 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10410 if (gotpc_offset + 0x1000000 >= 0x2000000)
10411 {
10412 (*_bfd_error_handler)
10413 (_("%B: `%A' offset of %ld from `%A' "
10414 "beyond the range of ADDIUPC"),
10415 output_bfd,
10416 htab->sgotplt->output_section,
10417 htab->splt->output_section,
10418 (long) gotpc_offset);
10419 bfd_set_error (bfd_error_no_error);
10420 return FALSE;
10421 }
10422 bfd_put_16 (output_bfd,
10423 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10424 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10425 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10426 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10427 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10428 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10429 }
6d30f5b2 10430 }
861fb55a
DJ
10431
10432 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10433 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
1bbce132 10434 got_index - 2, h->dynindx,
861fb55a
DJ
10435 R_MIPS_JUMP_SLOT, got_address);
10436
10437 /* We distinguish between PLT entries and lazy-binding stubs by
10438 giving the former an st_other value of STO_MIPS_PLT. Set the
10439 flag and leave the value if there are any relocations in the
10440 binary where pointer equality matters. */
10441 sym->st_shndx = SHN_UNDEF;
10442 if (h->pointer_equality_needed)
1bbce132 10443 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
861fb55a 10444 else
1bbce132
MR
10445 {
10446 sym->st_value = 0;
10447 sym->st_other = 0;
10448 }
861fb55a 10449 }
1bbce132
MR
10450
10451 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
b49e97c9 10452 {
861fb55a 10453 /* We've decided to create a lazy-binding stub. */
1bbce132
MR
10454 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10455 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10456 bfd_vma stub_size = htab->function_stub_size;
5108fc1b 10457 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
1bbce132
MR
10458 bfd_vma isa_bit = micromips_p;
10459 bfd_vma stub_big_size;
10460
833794fc 10461 if (!micromips_p)
1bbce132 10462 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
833794fc
MR
10463 else if (htab->insn32)
10464 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10465 else
10466 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
b49e97c9
TS
10467
10468 /* This symbol has a stub. Set it up. */
10469
10470 BFD_ASSERT (h->dynindx != -1);
10471
1bbce132 10472 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
3d6746ca
DD
10473
10474 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
10475 sign extension at runtime in the stub, resulting in a negative
10476 index value. */
10477 if (h->dynindx & ~0x7fffffff)
b34976b6 10478 return FALSE;
b49e97c9
TS
10479
10480 /* Fill the stub. */
1bbce132
MR
10481 if (micromips_p)
10482 {
10483 idx = 0;
10484 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10485 stub + idx);
10486 idx += 4;
833794fc
MR
10487 if (htab->insn32)
10488 {
10489 bfd_put_micromips_32 (output_bfd,
10490 STUB_MOVE32_MICROMIPS (output_bfd),
10491 stub + idx);
10492 idx += 4;
10493 }
10494 else
10495 {
10496 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10497 idx += 2;
10498 }
1bbce132
MR
10499 if (stub_size == stub_big_size)
10500 {
10501 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10502
10503 bfd_put_micromips_32 (output_bfd,
10504 STUB_LUI_MICROMIPS (dynindx_hi),
10505 stub + idx);
10506 idx += 4;
10507 }
833794fc
MR
10508 if (htab->insn32)
10509 {
10510 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
10511 stub + idx);
10512 idx += 4;
10513 }
10514 else
10515 {
10516 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
10517 idx += 2;
10518 }
1bbce132
MR
10519
10520 /* If a large stub is not required and sign extension is not a
10521 problem, then use legacy code in the stub. */
10522 if (stub_size == stub_big_size)
10523 bfd_put_micromips_32 (output_bfd,
10524 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
10525 stub + idx);
10526 else if (h->dynindx & ~0x7fff)
10527 bfd_put_micromips_32 (output_bfd,
10528 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
10529 stub + idx);
10530 else
10531 bfd_put_micromips_32 (output_bfd,
10532 STUB_LI16S_MICROMIPS (output_bfd,
10533 h->dynindx),
10534 stub + idx);
10535 }
3d6746ca 10536 else
1bbce132
MR
10537 {
10538 idx = 0;
10539 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
10540 idx += 4;
10541 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
10542 idx += 4;
10543 if (stub_size == stub_big_size)
10544 {
10545 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
10546 stub + idx);
10547 idx += 4;
10548 }
10549 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
10550 idx += 4;
10551
10552 /* If a large stub is not required and sign extension is not a
10553 problem, then use legacy code in the stub. */
10554 if (stub_size == stub_big_size)
10555 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
10556 stub + idx);
10557 else if (h->dynindx & ~0x7fff)
10558 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
10559 stub + idx);
10560 else
10561 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
10562 stub + idx);
10563 }
5108fc1b 10564
1bbce132
MR
10565 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
10566 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
10567 stub, stub_size);
b49e97c9 10568
1bbce132 10569 /* Mark the symbol as undefined. stub_offset != -1 occurs
b49e97c9
TS
10570 only for the referenced symbol. */
10571 sym->st_shndx = SHN_UNDEF;
10572
10573 /* The run-time linker uses the st_value field of the symbol
10574 to reset the global offset table entry for this external
10575 to its stub address when unlinking a shared object. */
4e41d0d7
RS
10576 sym->st_value = (htab->sstubs->output_section->vma
10577 + htab->sstubs->output_offset
1bbce132
MR
10578 + h->plt.plist->stub_offset
10579 + isa_bit);
10580 sym->st_other = other;
b49e97c9
TS
10581 }
10582
738e5348
RS
10583 /* If we have a MIPS16 function with a stub, the dynamic symbol must
10584 refer to the stub, since only the stub uses the standard calling
10585 conventions. */
10586 if (h->dynindx != -1 && hmips->fn_stub != NULL)
10587 {
10588 BFD_ASSERT (hmips->need_fn_stub);
10589 sym->st_value = (hmips->fn_stub->output_section->vma
10590 + hmips->fn_stub->output_offset);
10591 sym->st_size = hmips->fn_stub->size;
10592 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10593 }
10594
b49e97c9 10595 BFD_ASSERT (h->dynindx != -1
f5385ebf 10596 || h->forced_local);
b49e97c9 10597
23cc69b6 10598 sgot = htab->sgot;
a8028dd0 10599 g = htab->got_info;
b49e97c9
TS
10600 BFD_ASSERT (g != NULL);
10601
10602 /* Run through the global symbol table, creating GOT entries for all
10603 the symbols that need them. */
020d7251 10604 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
10605 {
10606 bfd_vma offset;
10607 bfd_vma value;
10608
6eaa6adc 10609 value = sym->st_value;
13fbec83 10610 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
b49e97c9
TS
10611 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10612 }
10613
e641e783 10614 if (hmips->global_got_area != GGA_NONE && g->next)
f4416af6
AO
10615 {
10616 struct mips_got_entry e, *p;
0626d451 10617 bfd_vma entry;
f4416af6 10618 bfd_vma offset;
f4416af6
AO
10619
10620 gg = g;
10621
10622 e.abfd = output_bfd;
10623 e.symndx = -1;
738e5348 10624 e.d.h = hmips;
9ab066b4 10625 e.tls_type = GOT_TLS_NONE;
143d77c5 10626
f4416af6
AO
10627 for (g = g->next; g->next != gg; g = g->next)
10628 {
10629 if (g->got_entries
10630 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10631 &e)))
10632 {
10633 offset = p->gotidx;
6c42ddb9 10634 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
0626d451
RS
10635 if (info->shared
10636 || (elf_hash_table (info)->dynamic_sections_created
10637 && p->d.h != NULL
f5385ebf
AM
10638 && p->d.h->root.def_dynamic
10639 && !p->d.h->root.def_regular))
0626d451
RS
10640 {
10641 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10642 the various compatibility problems, it's easier to mock
10643 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10644 mips_elf_create_dynamic_relocation to calculate the
10645 appropriate addend. */
10646 Elf_Internal_Rela rel[3];
10647
10648 memset (rel, 0, sizeof (rel));
10649 if (ABI_64_P (output_bfd))
10650 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10651 else
10652 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10653 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10654
10655 entry = 0;
10656 if (! (mips_elf_create_dynamic_relocation
10657 (output_bfd, info, rel,
10658 e.d.h, NULL, sym->st_value, &entry, sgot)))
10659 return FALSE;
10660 }
10661 else
10662 entry = sym->st_value;
10663 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
10664 }
10665 }
10666 }
10667
b49e97c9
TS
10668 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10669 name = h->root.root.string;
9637f6ef 10670 if (h == elf_hash_table (info)->hdynamic
22edb2f1 10671 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
10672 sym->st_shndx = SHN_ABS;
10673 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10674 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10675 {
10676 sym->st_shndx = SHN_ABS;
10677 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10678 sym->st_value = 1;
10679 }
4a14403c 10680 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10681 {
10682 sym->st_shndx = SHN_ABS;
10683 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10684 sym->st_value = elf_gp (output_bfd);
10685 }
10686 else if (SGI_COMPAT (output_bfd))
10687 {
10688 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10689 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10690 {
10691 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10692 sym->st_other = STO_PROTECTED;
10693 sym->st_value = 0;
10694 sym->st_shndx = SHN_MIPS_DATA;
10695 }
10696 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10697 {
10698 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10699 sym->st_other = STO_PROTECTED;
10700 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10701 sym->st_shndx = SHN_ABS;
10702 }
10703 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10704 {
10705 if (h->type == STT_FUNC)
10706 sym->st_shndx = SHN_MIPS_TEXT;
10707 else if (h->type == STT_OBJECT)
10708 sym->st_shndx = SHN_MIPS_DATA;
10709 }
10710 }
10711
861fb55a
DJ
10712 /* Emit a copy reloc, if needed. */
10713 if (h->needs_copy)
10714 {
10715 asection *s;
10716 bfd_vma symval;
10717
10718 BFD_ASSERT (h->dynindx != -1);
10719 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10720
10721 s = mips_elf_rel_dyn_section (info, FALSE);
10722 symval = (h->root.u.def.section->output_section->vma
10723 + h->root.u.def.section->output_offset
10724 + h->root.u.def.value);
10725 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10726 h->dynindx, R_MIPS_COPY, symval);
10727 }
10728
b49e97c9
TS
10729 /* Handle the IRIX6-specific symbols. */
10730 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10731 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10732
cbf8d970
MR
10733 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
10734 to treat compressed symbols like any other. */
30c09090 10735 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
10736 {
10737 BFD_ASSERT (sym->st_value & 1);
10738 sym->st_other -= STO_MIPS16;
10739 }
cbf8d970
MR
10740 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
10741 {
10742 BFD_ASSERT (sym->st_value & 1);
10743 sym->st_other -= STO_MICROMIPS;
10744 }
b49e97c9 10745
b34976b6 10746 return TRUE;
b49e97c9
TS
10747}
10748
0a44bf69
RS
10749/* Likewise, for VxWorks. */
10750
10751bfd_boolean
10752_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10753 struct bfd_link_info *info,
10754 struct elf_link_hash_entry *h,
10755 Elf_Internal_Sym *sym)
10756{
10757 bfd *dynobj;
10758 asection *sgot;
10759 struct mips_got_info *g;
10760 struct mips_elf_link_hash_table *htab;
020d7251 10761 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
10762
10763 htab = mips_elf_hash_table (info);
4dfe6ac6 10764 BFD_ASSERT (htab != NULL);
0a44bf69 10765 dynobj = elf_hash_table (info)->dynobj;
020d7251 10766 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69 10767
1bbce132 10768 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
0a44bf69 10769 {
6d79d2ed 10770 bfd_byte *loc;
1bbce132 10771 bfd_vma plt_address, got_address, got_offset, branch_offset;
0a44bf69
RS
10772 Elf_Internal_Rela rel;
10773 static const bfd_vma *plt_entry;
1bbce132
MR
10774 bfd_vma gotplt_index;
10775 bfd_vma plt_offset;
10776
10777 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10778 gotplt_index = h->plt.plist->gotplt_index;
0a44bf69
RS
10779
10780 BFD_ASSERT (h->dynindx != -1);
10781 BFD_ASSERT (htab->splt != NULL);
1bbce132
MR
10782 BFD_ASSERT (gotplt_index != MINUS_ONE);
10783 BFD_ASSERT (plt_offset <= htab->splt->size);
0a44bf69
RS
10784
10785 /* Calculate the address of the .plt entry. */
10786 plt_address = (htab->splt->output_section->vma
10787 + htab->splt->output_offset
1bbce132 10788 + plt_offset);
0a44bf69
RS
10789
10790 /* Calculate the address of the .got.plt entry. */
10791 got_address = (htab->sgotplt->output_section->vma
10792 + htab->sgotplt->output_offset
1bbce132 10793 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
0a44bf69
RS
10794
10795 /* Calculate the offset of the .got.plt entry from
10796 _GLOBAL_OFFSET_TABLE_. */
10797 got_offset = mips_elf_gotplt_index (info, h);
10798
10799 /* Calculate the offset for the branch at the start of the PLT
10800 entry. The branch jumps to the beginning of .plt. */
1bbce132 10801 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
0a44bf69
RS
10802
10803 /* Fill in the initial value of the .got.plt entry. */
10804 bfd_put_32 (output_bfd, plt_address,
1bbce132
MR
10805 (htab->sgotplt->contents
10806 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
0a44bf69
RS
10807
10808 /* Find out where the .plt entry should go. */
1bbce132 10809 loc = htab->splt->contents + plt_offset;
0a44bf69
RS
10810
10811 if (info->shared)
10812 {
10813 plt_entry = mips_vxworks_shared_plt_entry;
10814 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 10815 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
10816 }
10817 else
10818 {
10819 bfd_vma got_address_high, got_address_low;
10820
10821 plt_entry = mips_vxworks_exec_plt_entry;
10822 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10823 got_address_low = got_address & 0xffff;
10824
10825 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 10826 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
10827 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10828 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10829 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10830 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10831 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10832 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10833
10834 loc = (htab->srelplt2->contents
1bbce132 10835 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
0a44bf69
RS
10836
10837 /* Emit a relocation for the .got.plt entry. */
10838 rel.r_offset = got_address;
10839 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
1bbce132 10840 rel.r_addend = plt_offset;
0a44bf69
RS
10841 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10842
10843 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10844 loc += sizeof (Elf32_External_Rela);
10845 rel.r_offset = plt_address + 8;
10846 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10847 rel.r_addend = got_offset;
10848 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10849
10850 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10851 loc += sizeof (Elf32_External_Rela);
10852 rel.r_offset += 4;
10853 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10854 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10855 }
10856
10857 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
1bbce132
MR
10858 loc = (htab->srelplt->contents
10859 + gotplt_index * sizeof (Elf32_External_Rela));
0a44bf69
RS
10860 rel.r_offset = got_address;
10861 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10862 rel.r_addend = 0;
10863 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10864
10865 if (!h->def_regular)
10866 sym->st_shndx = SHN_UNDEF;
10867 }
10868
10869 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10870
23cc69b6 10871 sgot = htab->sgot;
a8028dd0 10872 g = htab->got_info;
0a44bf69
RS
10873 BFD_ASSERT (g != NULL);
10874
10875 /* See if this symbol has an entry in the GOT. */
020d7251 10876 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
10877 {
10878 bfd_vma offset;
10879 Elf_Internal_Rela outrel;
10880 bfd_byte *loc;
10881 asection *s;
10882
10883 /* Install the symbol value in the GOT. */
13fbec83 10884 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
0a44bf69
RS
10885 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10886
10887 /* Add a dynamic relocation for it. */
10888 s = mips_elf_rel_dyn_section (info, FALSE);
10889 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10890 outrel.r_offset = (sgot->output_section->vma
10891 + sgot->output_offset
10892 + offset);
10893 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10894 outrel.r_addend = 0;
10895 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10896 }
10897
10898 /* Emit a copy reloc, if needed. */
10899 if (h->needs_copy)
10900 {
10901 Elf_Internal_Rela rel;
10902
10903 BFD_ASSERT (h->dynindx != -1);
10904
10905 rel.r_offset = (h->root.u.def.section->output_section->vma
10906 + h->root.u.def.section->output_offset
10907 + h->root.u.def.value);
10908 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10909 rel.r_addend = 0;
10910 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10911 htab->srelbss->contents
10912 + (htab->srelbss->reloc_count
10913 * sizeof (Elf32_External_Rela)));
10914 ++htab->srelbss->reloc_count;
10915 }
10916
df58fc94
RS
10917 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10918 if (ELF_ST_IS_COMPRESSED (sym->st_other))
0a44bf69
RS
10919 sym->st_value &= ~1;
10920
10921 return TRUE;
10922}
10923
861fb55a
DJ
10924/* Write out a plt0 entry to the beginning of .plt. */
10925
1bbce132 10926static bfd_boolean
861fb55a
DJ
10927mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10928{
10929 bfd_byte *loc;
10930 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10931 static const bfd_vma *plt_entry;
10932 struct mips_elf_link_hash_table *htab;
10933
10934 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10935 BFD_ASSERT (htab != NULL);
10936
861fb55a
DJ
10937 if (ABI_64_P (output_bfd))
10938 plt_entry = mips_n64_exec_plt0_entry;
10939 else if (ABI_N32_P (output_bfd))
10940 plt_entry = mips_n32_exec_plt0_entry;
833794fc 10941 else if (!htab->plt_header_is_comp)
861fb55a 10942 plt_entry = mips_o32_exec_plt0_entry;
833794fc
MR
10943 else if (htab->insn32)
10944 plt_entry = micromips_insn32_o32_exec_plt0_entry;
10945 else
10946 plt_entry = micromips_o32_exec_plt0_entry;
861fb55a
DJ
10947
10948 /* Calculate the value of .got.plt. */
10949 gotplt_value = (htab->sgotplt->output_section->vma
10950 + htab->sgotplt->output_offset);
10951 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10952 gotplt_value_low = gotplt_value & 0xffff;
10953
10954 /* The PLT sequence is not safe for N64 if .got.plt's address can
10955 not be loaded in two instructions. */
10956 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10957 || ~(gotplt_value | 0x7fffffff) == 0);
10958
10959 /* Install the PLT header. */
10960 loc = htab->splt->contents;
1bbce132
MR
10961 if (plt_entry == micromips_o32_exec_plt0_entry)
10962 {
10963 bfd_vma gotpc_offset;
10964 bfd_vma loc_address;
10965 size_t i;
10966
10967 BFD_ASSERT (gotplt_value % 4 == 0);
10968
10969 loc_address = (htab->splt->output_section->vma
10970 + htab->splt->output_offset);
10971 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
10972
10973 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10974 if (gotpc_offset + 0x1000000 >= 0x2000000)
10975 {
10976 (*_bfd_error_handler)
10977 (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
10978 output_bfd,
10979 htab->sgotplt->output_section,
10980 htab->splt->output_section,
10981 (long) gotpc_offset);
10982 bfd_set_error (bfd_error_no_error);
10983 return FALSE;
10984 }
10985 bfd_put_16 (output_bfd,
10986 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10987 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10988 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
10989 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10990 }
833794fc
MR
10991 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
10992 {
10993 size_t i;
10994
10995 bfd_put_16 (output_bfd, plt_entry[0], loc);
10996 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
10997 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10998 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
10999 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11000 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11001 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11002 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11003 }
1bbce132
MR
11004 else
11005 {
11006 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11007 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11008 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11009 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11010 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11011 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11012 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11013 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11014 }
11015
11016 return TRUE;
861fb55a
DJ
11017}
11018
0a44bf69
RS
11019/* Install the PLT header for a VxWorks executable and finalize the
11020 contents of .rela.plt.unloaded. */
11021
11022static void
11023mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11024{
11025 Elf_Internal_Rela rela;
11026 bfd_byte *loc;
11027 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11028 static const bfd_vma *plt_entry;
11029 struct mips_elf_link_hash_table *htab;
11030
11031 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11032 BFD_ASSERT (htab != NULL);
11033
0a44bf69
RS
11034 plt_entry = mips_vxworks_exec_plt0_entry;
11035
11036 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11037 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11038 + htab->root.hgot->root.u.def.section->output_offset
11039 + htab->root.hgot->root.u.def.value);
11040
11041 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11042 got_value_low = got_value & 0xffff;
11043
11044 /* Calculate the address of the PLT header. */
11045 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
11046
11047 /* Install the PLT header. */
11048 loc = htab->splt->contents;
11049 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11050 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11051 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11052 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11053 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11054 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11055
11056 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11057 loc = htab->srelplt2->contents;
11058 rela.r_offset = plt_address;
11059 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11060 rela.r_addend = 0;
11061 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11062 loc += sizeof (Elf32_External_Rela);
11063
11064 /* Output the relocation for the following addiu of
11065 %lo(_GLOBAL_OFFSET_TABLE_). */
11066 rela.r_offset += 4;
11067 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11068 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11069 loc += sizeof (Elf32_External_Rela);
11070
11071 /* Fix up the remaining relocations. They may have the wrong
11072 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11073 in which symbols were output. */
11074 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11075 {
11076 Elf_Internal_Rela rel;
11077
11078 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11079 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11080 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11081 loc += sizeof (Elf32_External_Rela);
11082
11083 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11084 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11085 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11086 loc += sizeof (Elf32_External_Rela);
11087
11088 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11089 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11090 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11091 loc += sizeof (Elf32_External_Rela);
11092 }
11093}
11094
11095/* Install the PLT header for a VxWorks shared library. */
11096
11097static void
11098mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11099{
11100 unsigned int i;
11101 struct mips_elf_link_hash_table *htab;
11102
11103 htab = mips_elf_hash_table (info);
4dfe6ac6 11104 BFD_ASSERT (htab != NULL);
0a44bf69
RS
11105
11106 /* We just need to copy the entry byte-by-byte. */
11107 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11108 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11109 htab->splt->contents + i * 4);
11110}
11111
b49e97c9
TS
11112/* Finish up the dynamic sections. */
11113
b34976b6 11114bfd_boolean
9719ad41
RS
11115_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11116 struct bfd_link_info *info)
b49e97c9
TS
11117{
11118 bfd *dynobj;
11119 asection *sdyn;
11120 asection *sgot;
f4416af6 11121 struct mips_got_info *gg, *g;
0a44bf69 11122 struct mips_elf_link_hash_table *htab;
b49e97c9 11123
0a44bf69 11124 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11125 BFD_ASSERT (htab != NULL);
11126
b49e97c9
TS
11127 dynobj = elf_hash_table (info)->dynobj;
11128
3d4d4302 11129 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
b49e97c9 11130
23cc69b6
RS
11131 sgot = htab->sgot;
11132 gg = htab->got_info;
b49e97c9
TS
11133
11134 if (elf_hash_table (info)->dynamic_sections_created)
11135 {
11136 bfd_byte *b;
943284cc 11137 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
11138
11139 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
11140 BFD_ASSERT (gg != NULL);
11141
d7206569 11142 g = mips_elf_bfd_got (output_bfd, FALSE);
b49e97c9
TS
11143 BFD_ASSERT (g != NULL);
11144
11145 for (b = sdyn->contents;
eea6121a 11146 b < sdyn->contents + sdyn->size;
b49e97c9
TS
11147 b += MIPS_ELF_DYN_SIZE (dynobj))
11148 {
11149 Elf_Internal_Dyn dyn;
11150 const char *name;
11151 size_t elemsize;
11152 asection *s;
b34976b6 11153 bfd_boolean swap_out_p;
b49e97c9
TS
11154
11155 /* Read in the current dynamic entry. */
11156 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11157
11158 /* Assume that we're going to modify it and write it out. */
b34976b6 11159 swap_out_p = TRUE;
b49e97c9
TS
11160
11161 switch (dyn.d_tag)
11162 {
11163 case DT_RELENT:
b49e97c9
TS
11164 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11165 break;
11166
0a44bf69
RS
11167 case DT_RELAENT:
11168 BFD_ASSERT (htab->is_vxworks);
11169 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11170 break;
11171
b49e97c9
TS
11172 case DT_STRSZ:
11173 /* Rewrite DT_STRSZ. */
11174 dyn.d_un.d_val =
11175 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11176 break;
11177
11178 case DT_PLTGOT:
861fb55a
DJ
11179 s = htab->sgot;
11180 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11181 break;
11182
11183 case DT_MIPS_PLTGOT:
11184 s = htab->sgotplt;
11185 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
11186 break;
11187
11188 case DT_MIPS_RLD_VERSION:
11189 dyn.d_un.d_val = 1; /* XXX */
11190 break;
11191
11192 case DT_MIPS_FLAGS:
11193 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11194 break;
11195
b49e97c9 11196 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
11197 {
11198 time_t t;
11199 time (&t);
11200 dyn.d_un.d_val = t;
11201 }
b49e97c9
TS
11202 break;
11203
11204 case DT_MIPS_ICHECKSUM:
11205 /* XXX FIXME: */
b34976b6 11206 swap_out_p = FALSE;
b49e97c9
TS
11207 break;
11208
11209 case DT_MIPS_IVERSION:
11210 /* XXX FIXME: */
b34976b6 11211 swap_out_p = FALSE;
b49e97c9
TS
11212 break;
11213
11214 case DT_MIPS_BASE_ADDRESS:
11215 s = output_bfd->sections;
11216 BFD_ASSERT (s != NULL);
11217 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11218 break;
11219
11220 case DT_MIPS_LOCAL_GOTNO:
11221 dyn.d_un.d_val = g->local_gotno;
11222 break;
11223
11224 case DT_MIPS_UNREFEXTNO:
11225 /* The index into the dynamic symbol table which is the
11226 entry of the first external symbol that is not
11227 referenced within the same object. */
11228 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11229 break;
11230
11231 case DT_MIPS_GOTSYM:
d222d210 11232 if (htab->global_gotsym)
b49e97c9 11233 {
d222d210 11234 dyn.d_un.d_val = htab->global_gotsym->dynindx;
b49e97c9
TS
11235 break;
11236 }
11237 /* In case if we don't have global got symbols we default
11238 to setting DT_MIPS_GOTSYM to the same value as
11239 DT_MIPS_SYMTABNO, so we just fall through. */
11240
11241 case DT_MIPS_SYMTABNO:
11242 name = ".dynsym";
11243 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11244 s = bfd_get_section_by_name (output_bfd, name);
11245 BFD_ASSERT (s != NULL);
11246
eea6121a 11247 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
11248 break;
11249
11250 case DT_MIPS_HIPAGENO:
861fb55a 11251 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
11252 break;
11253
11254 case DT_MIPS_RLD_MAP:
b4082c70
DD
11255 {
11256 struct elf_link_hash_entry *h;
11257 h = mips_elf_hash_table (info)->rld_symbol;
11258 if (!h)
11259 {
11260 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11261 swap_out_p = FALSE;
11262 break;
11263 }
11264 s = h->root.u.def.section;
11265 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11266 + h->root.u.def.value);
11267 }
b49e97c9
TS
11268 break;
11269
11270 case DT_MIPS_OPTIONS:
11271 s = (bfd_get_section_by_name
11272 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11273 dyn.d_un.d_ptr = s->vma;
11274 break;
11275
0a44bf69
RS
11276 case DT_RELASZ:
11277 BFD_ASSERT (htab->is_vxworks);
11278 /* The count does not include the JUMP_SLOT relocations. */
11279 if (htab->srelplt)
11280 dyn.d_un.d_val -= htab->srelplt->size;
11281 break;
11282
11283 case DT_PLTREL:
861fb55a
DJ
11284 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11285 if (htab->is_vxworks)
11286 dyn.d_un.d_val = DT_RELA;
11287 else
11288 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
11289 break;
11290
11291 case DT_PLTRELSZ:
861fb55a 11292 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
11293 dyn.d_un.d_val = htab->srelplt->size;
11294 break;
11295
11296 case DT_JMPREL:
861fb55a
DJ
11297 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11298 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
11299 + htab->srelplt->output_offset);
11300 break;
11301
943284cc
DJ
11302 case DT_TEXTREL:
11303 /* If we didn't need any text relocations after all, delete
11304 the dynamic tag. */
11305 if (!(info->flags & DF_TEXTREL))
11306 {
11307 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11308 swap_out_p = FALSE;
11309 }
11310 break;
11311
11312 case DT_FLAGS:
11313 /* If we didn't need any text relocations after all, clear
11314 DF_TEXTREL from DT_FLAGS. */
11315 if (!(info->flags & DF_TEXTREL))
11316 dyn.d_un.d_val &= ~DF_TEXTREL;
11317 else
11318 swap_out_p = FALSE;
11319 break;
11320
b49e97c9 11321 default:
b34976b6 11322 swap_out_p = FALSE;
7a2b07ff
NS
11323 if (htab->is_vxworks
11324 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11325 swap_out_p = TRUE;
b49e97c9
TS
11326 break;
11327 }
11328
943284cc 11329 if (swap_out_p || dyn_skipped)
b49e97c9 11330 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
11331 (dynobj, &dyn, b - dyn_skipped);
11332
11333 if (dyn_to_skip)
11334 {
11335 dyn_skipped += dyn_to_skip;
11336 dyn_to_skip = 0;
11337 }
b49e97c9 11338 }
943284cc
DJ
11339
11340 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11341 if (dyn_skipped > 0)
11342 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
11343 }
11344
b55fd4d4
DJ
11345 if (sgot != NULL && sgot->size > 0
11346 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 11347 {
0a44bf69
RS
11348 if (htab->is_vxworks)
11349 {
11350 /* The first entry of the global offset table points to the
11351 ".dynamic" section. The second is initialized by the
11352 loader and contains the shared library identifier.
11353 The third is also initialized by the loader and points
11354 to the lazy resolution stub. */
11355 MIPS_ELF_PUT_WORD (output_bfd,
11356 sdyn->output_offset + sdyn->output_section->vma,
11357 sgot->contents);
11358 MIPS_ELF_PUT_WORD (output_bfd, 0,
11359 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11360 MIPS_ELF_PUT_WORD (output_bfd, 0,
11361 sgot->contents
11362 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11363 }
11364 else
11365 {
11366 /* The first entry of the global offset table will be filled at
11367 runtime. The second entry will be used by some runtime loaders.
11368 This isn't the case of IRIX rld. */
11369 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 11370 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
11371 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11372 }
b49e97c9 11373
54938e2a
TS
11374 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11375 = MIPS_ELF_GOT_SIZE (output_bfd);
11376 }
b49e97c9 11377
f4416af6
AO
11378 /* Generate dynamic relocations for the non-primary gots. */
11379 if (gg != NULL && gg->next)
11380 {
11381 Elf_Internal_Rela rel[3];
11382 bfd_vma addend = 0;
11383
11384 memset (rel, 0, sizeof (rel));
11385 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11386
11387 for (g = gg->next; g->next != gg; g = g->next)
11388 {
91d6fa6a 11389 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 11390 + g->next->tls_gotno;
f4416af6 11391
9719ad41 11392 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 11393 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
11394 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11395 sgot->contents
91d6fa6a 11396 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
11397
11398 if (! info->shared)
11399 continue;
11400
cb22ccf4 11401 for (; got_index < g->local_gotno; got_index++)
f4416af6 11402 {
cb22ccf4
KCY
11403 if (got_index >= g->assigned_low_gotno
11404 && got_index <= g->assigned_high_gotno)
11405 continue;
11406
f4416af6 11407 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
cb22ccf4 11408 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
11409 if (!(mips_elf_create_dynamic_relocation
11410 (output_bfd, info, rel, NULL,
11411 bfd_abs_section_ptr,
11412 0, &addend, sgot)))
11413 return FALSE;
11414 BFD_ASSERT (addend == 0);
11415 }
11416 }
11417 }
11418
3133ddbf
DJ
11419 /* The generation of dynamic relocations for the non-primary gots
11420 adds more dynamic relocations. We cannot count them until
11421 here. */
11422
11423 if (elf_hash_table (info)->dynamic_sections_created)
11424 {
11425 bfd_byte *b;
11426 bfd_boolean swap_out_p;
11427
11428 BFD_ASSERT (sdyn != NULL);
11429
11430 for (b = sdyn->contents;
11431 b < sdyn->contents + sdyn->size;
11432 b += MIPS_ELF_DYN_SIZE (dynobj))
11433 {
11434 Elf_Internal_Dyn dyn;
11435 asection *s;
11436
11437 /* Read in the current dynamic entry. */
11438 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11439
11440 /* Assume that we're going to modify it and write it out. */
11441 swap_out_p = TRUE;
11442
11443 switch (dyn.d_tag)
11444 {
11445 case DT_RELSZ:
11446 /* Reduce DT_RELSZ to account for any relocations we
11447 decided not to make. This is for the n64 irix rld,
11448 which doesn't seem to apply any relocations if there
11449 are trailing null entries. */
0a44bf69 11450 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
11451 dyn.d_un.d_val = (s->reloc_count
11452 * (ABI_64_P (output_bfd)
11453 ? sizeof (Elf64_Mips_External_Rel)
11454 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
11455 /* Adjust the section size too. Tools like the prelinker
11456 can reasonably expect the values to the same. */
11457 elf_section_data (s->output_section)->this_hdr.sh_size
11458 = dyn.d_un.d_val;
3133ddbf
DJ
11459 break;
11460
11461 default:
11462 swap_out_p = FALSE;
11463 break;
11464 }
11465
11466 if (swap_out_p)
11467 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11468 (dynobj, &dyn, b);
11469 }
11470 }
11471
b49e97c9 11472 {
b49e97c9
TS
11473 asection *s;
11474 Elf32_compact_rel cpt;
11475
b49e97c9
TS
11476 if (SGI_COMPAT (output_bfd))
11477 {
11478 /* Write .compact_rel section out. */
3d4d4302 11479 s = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
11480 if (s != NULL)
11481 {
11482 cpt.id1 = 1;
11483 cpt.num = s->reloc_count;
11484 cpt.id2 = 2;
11485 cpt.offset = (s->output_section->filepos
11486 + sizeof (Elf32_External_compact_rel));
11487 cpt.reserved0 = 0;
11488 cpt.reserved1 = 0;
11489 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
11490 ((Elf32_External_compact_rel *)
11491 s->contents));
11492
11493 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 11494 if (htab->sstubs != NULL)
b49e97c9
TS
11495 {
11496 file_ptr dummy_offset;
11497
4e41d0d7
RS
11498 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
11499 dummy_offset = htab->sstubs->size - htab->function_stub_size;
11500 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 11501 htab->function_stub_size);
b49e97c9
TS
11502 }
11503 }
11504 }
11505
0a44bf69
RS
11506 /* The psABI says that the dynamic relocations must be sorted in
11507 increasing order of r_symndx. The VxWorks EABI doesn't require
11508 this, and because the code below handles REL rather than RELA
11509 relocations, using it for VxWorks would be outright harmful. */
11510 if (!htab->is_vxworks)
b49e97c9 11511 {
0a44bf69
RS
11512 s = mips_elf_rel_dyn_section (info, FALSE);
11513 if (s != NULL
11514 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
11515 {
11516 reldyn_sorting_bfd = output_bfd;
b49e97c9 11517
0a44bf69
RS
11518 if (ABI_64_P (output_bfd))
11519 qsort ((Elf64_External_Rel *) s->contents + 1,
11520 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
11521 sort_dynamic_relocs_64);
11522 else
11523 qsort ((Elf32_External_Rel *) s->contents + 1,
11524 s->reloc_count - 1, sizeof (Elf32_External_Rel),
11525 sort_dynamic_relocs);
11526 }
b49e97c9 11527 }
b49e97c9
TS
11528 }
11529
861fb55a 11530 if (htab->splt && htab->splt->size > 0)
0a44bf69 11531 {
861fb55a
DJ
11532 if (htab->is_vxworks)
11533 {
11534 if (info->shared)
11535 mips_vxworks_finish_shared_plt (output_bfd, info);
11536 else
11537 mips_vxworks_finish_exec_plt (output_bfd, info);
11538 }
0a44bf69 11539 else
861fb55a
DJ
11540 {
11541 BFD_ASSERT (!info->shared);
1bbce132
MR
11542 if (!mips_finish_exec_plt (output_bfd, info))
11543 return FALSE;
861fb55a 11544 }
0a44bf69 11545 }
b34976b6 11546 return TRUE;
b49e97c9
TS
11547}
11548
b49e97c9 11549
64543e1a
RS
11550/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
11551
11552static void
9719ad41 11553mips_set_isa_flags (bfd *abfd)
b49e97c9 11554{
64543e1a 11555 flagword val;
b49e97c9
TS
11556
11557 switch (bfd_get_mach (abfd))
11558 {
11559 default:
11560 case bfd_mach_mips3000:
11561 val = E_MIPS_ARCH_1;
11562 break;
11563
11564 case bfd_mach_mips3900:
11565 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
11566 break;
11567
11568 case bfd_mach_mips6000:
11569 val = E_MIPS_ARCH_2;
11570 break;
11571
11572 case bfd_mach_mips4000:
11573 case bfd_mach_mips4300:
11574 case bfd_mach_mips4400:
11575 case bfd_mach_mips4600:
11576 val = E_MIPS_ARCH_3;
11577 break;
11578
11579 case bfd_mach_mips4010:
11580 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
11581 break;
11582
11583 case bfd_mach_mips4100:
11584 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
11585 break;
11586
11587 case bfd_mach_mips4111:
11588 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
11589 break;
11590
00707a0e
RS
11591 case bfd_mach_mips4120:
11592 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
11593 break;
11594
b49e97c9
TS
11595 case bfd_mach_mips4650:
11596 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
11597 break;
11598
00707a0e
RS
11599 case bfd_mach_mips5400:
11600 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
11601 break;
11602
11603 case bfd_mach_mips5500:
11604 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
11605 break;
11606
e407c74b
NC
11607 case bfd_mach_mips5900:
11608 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
11609 break;
11610
0d2e43ed
ILT
11611 case bfd_mach_mips9000:
11612 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
11613 break;
11614
b49e97c9 11615 case bfd_mach_mips5000:
5a7ea749 11616 case bfd_mach_mips7000:
b49e97c9
TS
11617 case bfd_mach_mips8000:
11618 case bfd_mach_mips10000:
11619 case bfd_mach_mips12000:
3aa3176b
TS
11620 case bfd_mach_mips14000:
11621 case bfd_mach_mips16000:
b49e97c9
TS
11622 val = E_MIPS_ARCH_4;
11623 break;
11624
11625 case bfd_mach_mips5:
11626 val = E_MIPS_ARCH_5;
11627 break;
11628
350cc38d
MS
11629 case bfd_mach_mips_loongson_2e:
11630 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
11631 break;
11632
11633 case bfd_mach_mips_loongson_2f:
11634 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
11635 break;
11636
b49e97c9
TS
11637 case bfd_mach_mips_sb1:
11638 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
11639 break;
11640
d051516a 11641 case bfd_mach_mips_loongson_3a:
4ba154f5 11642 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_LS3A;
d051516a
NC
11643 break;
11644
6f179bd0 11645 case bfd_mach_mips_octeon:
dd6a37e7 11646 case bfd_mach_mips_octeonp:
6f179bd0
AN
11647 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
11648 break;
11649
52b6b6b9
JM
11650 case bfd_mach_mips_xlr:
11651 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
11652 break;
11653
432233b3
AP
11654 case bfd_mach_mips_octeon2:
11655 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
11656 break;
11657
b49e97c9
TS
11658 case bfd_mach_mipsisa32:
11659 val = E_MIPS_ARCH_32;
11660 break;
11661
11662 case bfd_mach_mipsisa64:
11663 val = E_MIPS_ARCH_64;
af7ee8bf
CD
11664 break;
11665
11666 case bfd_mach_mipsisa32r2:
11667 val = E_MIPS_ARCH_32R2;
11668 break;
5f74bc13
CD
11669
11670 case bfd_mach_mipsisa64r2:
11671 val = E_MIPS_ARCH_64R2;
11672 break;
b49e97c9 11673 }
b49e97c9
TS
11674 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11675 elf_elfheader (abfd)->e_flags |= val;
11676
64543e1a
RS
11677}
11678
11679
11680/* The final processing done just before writing out a MIPS ELF object
11681 file. This gets the MIPS architecture right based on the machine
11682 number. This is used by both the 32-bit and the 64-bit ABI. */
11683
11684void
9719ad41
RS
11685_bfd_mips_elf_final_write_processing (bfd *abfd,
11686 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
11687{
11688 unsigned int i;
11689 Elf_Internal_Shdr **hdrpp;
11690 const char *name;
11691 asection *sec;
11692
11693 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11694 is nonzero. This is for compatibility with old objects, which used
11695 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11696 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11697 mips_set_isa_flags (abfd);
11698
b49e97c9
TS
11699 /* Set the sh_info field for .gptab sections and other appropriate
11700 info for each special section. */
11701 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11702 i < elf_numsections (abfd);
11703 i++, hdrpp++)
11704 {
11705 switch ((*hdrpp)->sh_type)
11706 {
11707 case SHT_MIPS_MSYM:
11708 case SHT_MIPS_LIBLIST:
11709 sec = bfd_get_section_by_name (abfd, ".dynstr");
11710 if (sec != NULL)
11711 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11712 break;
11713
11714 case SHT_MIPS_GPTAB:
11715 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11716 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11717 BFD_ASSERT (name != NULL
0112cd26 11718 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
11719 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11720 BFD_ASSERT (sec != NULL);
11721 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11722 break;
11723
11724 case SHT_MIPS_CONTENT:
11725 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11726 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11727 BFD_ASSERT (name != NULL
0112cd26 11728 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
11729 sec = bfd_get_section_by_name (abfd,
11730 name + sizeof ".MIPS.content" - 1);
11731 BFD_ASSERT (sec != NULL);
11732 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11733 break;
11734
11735 case SHT_MIPS_SYMBOL_LIB:
11736 sec = bfd_get_section_by_name (abfd, ".dynsym");
11737 if (sec != NULL)
11738 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11739 sec = bfd_get_section_by_name (abfd, ".liblist");
11740 if (sec != NULL)
11741 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11742 break;
11743
11744 case SHT_MIPS_EVENTS:
11745 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11746 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11747 BFD_ASSERT (name != NULL);
0112cd26 11748 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
11749 sec = bfd_get_section_by_name (abfd,
11750 name + sizeof ".MIPS.events" - 1);
11751 else
11752 {
0112cd26 11753 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
11754 sec = bfd_get_section_by_name (abfd,
11755 (name
11756 + sizeof ".MIPS.post_rel" - 1));
11757 }
11758 BFD_ASSERT (sec != NULL);
11759 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11760 break;
11761
11762 }
11763 }
11764}
11765\f
8dc1a139 11766/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
11767 segments. */
11768
11769int
a6b96beb
AM
11770_bfd_mips_elf_additional_program_headers (bfd *abfd,
11771 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
11772{
11773 asection *s;
11774 int ret = 0;
11775
11776 /* See if we need a PT_MIPS_REGINFO segment. */
11777 s = bfd_get_section_by_name (abfd, ".reginfo");
11778 if (s && (s->flags & SEC_LOAD))
11779 ++ret;
11780
11781 /* See if we need a PT_MIPS_OPTIONS segment. */
11782 if (IRIX_COMPAT (abfd) == ict_irix6
11783 && bfd_get_section_by_name (abfd,
11784 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11785 ++ret;
11786
11787 /* See if we need a PT_MIPS_RTPROC segment. */
11788 if (IRIX_COMPAT (abfd) == ict_irix5
11789 && bfd_get_section_by_name (abfd, ".dynamic")
11790 && bfd_get_section_by_name (abfd, ".mdebug"))
11791 ++ret;
11792
98c904a8
RS
11793 /* Allocate a PT_NULL header in dynamic objects. See
11794 _bfd_mips_elf_modify_segment_map for details. */
11795 if (!SGI_COMPAT (abfd)
11796 && bfd_get_section_by_name (abfd, ".dynamic"))
11797 ++ret;
11798
b49e97c9
TS
11799 return ret;
11800}
11801
8dc1a139 11802/* Modify the segment map for an IRIX5 executable. */
b49e97c9 11803
b34976b6 11804bfd_boolean
9719ad41 11805_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 11806 struct bfd_link_info *info)
b49e97c9
TS
11807{
11808 asection *s;
11809 struct elf_segment_map *m, **pm;
11810 bfd_size_type amt;
11811
11812 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11813 segment. */
11814 s = bfd_get_section_by_name (abfd, ".reginfo");
11815 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11816 {
12bd6957 11817 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
11818 if (m->p_type == PT_MIPS_REGINFO)
11819 break;
11820 if (m == NULL)
11821 {
11822 amt = sizeof *m;
9719ad41 11823 m = bfd_zalloc (abfd, amt);
b49e97c9 11824 if (m == NULL)
b34976b6 11825 return FALSE;
b49e97c9
TS
11826
11827 m->p_type = PT_MIPS_REGINFO;
11828 m->count = 1;
11829 m->sections[0] = s;
11830
11831 /* We want to put it after the PHDR and INTERP segments. */
12bd6957 11832 pm = &elf_seg_map (abfd);
b49e97c9
TS
11833 while (*pm != NULL
11834 && ((*pm)->p_type == PT_PHDR
11835 || (*pm)->p_type == PT_INTERP))
11836 pm = &(*pm)->next;
11837
11838 m->next = *pm;
11839 *pm = m;
11840 }
11841 }
11842
11843 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11844 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 11845 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 11846 table. */
c1fd6598
AO
11847 if (NEWABI_P (abfd)
11848 /* On non-IRIX6 new abi, we'll have already created a segment
11849 for this section, so don't create another. I'm not sure this
11850 is not also the case for IRIX 6, but I can't test it right
11851 now. */
11852 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
11853 {
11854 for (s = abfd->sections; s; s = s->next)
11855 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11856 break;
11857
11858 if (s)
11859 {
11860 struct elf_segment_map *options_segment;
11861
12bd6957 11862 pm = &elf_seg_map (abfd);
98a8deaf
RS
11863 while (*pm != NULL
11864 && ((*pm)->p_type == PT_PHDR
11865 || (*pm)->p_type == PT_INTERP))
11866 pm = &(*pm)->next;
b49e97c9 11867
8ded5a0f
AM
11868 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11869 {
11870 amt = sizeof (struct elf_segment_map);
11871 options_segment = bfd_zalloc (abfd, amt);
11872 options_segment->next = *pm;
11873 options_segment->p_type = PT_MIPS_OPTIONS;
11874 options_segment->p_flags = PF_R;
11875 options_segment->p_flags_valid = TRUE;
11876 options_segment->count = 1;
11877 options_segment->sections[0] = s;
11878 *pm = options_segment;
11879 }
b49e97c9
TS
11880 }
11881 }
11882 else
11883 {
11884 if (IRIX_COMPAT (abfd) == ict_irix5)
11885 {
11886 /* If there are .dynamic and .mdebug sections, we make a room
11887 for the RTPROC header. FIXME: Rewrite without section names. */
11888 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11889 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11890 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11891 {
12bd6957 11892 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
11893 if (m->p_type == PT_MIPS_RTPROC)
11894 break;
11895 if (m == NULL)
11896 {
11897 amt = sizeof *m;
9719ad41 11898 m = bfd_zalloc (abfd, amt);
b49e97c9 11899 if (m == NULL)
b34976b6 11900 return FALSE;
b49e97c9
TS
11901
11902 m->p_type = PT_MIPS_RTPROC;
11903
11904 s = bfd_get_section_by_name (abfd, ".rtproc");
11905 if (s == NULL)
11906 {
11907 m->count = 0;
11908 m->p_flags = 0;
11909 m->p_flags_valid = 1;
11910 }
11911 else
11912 {
11913 m->count = 1;
11914 m->sections[0] = s;
11915 }
11916
11917 /* We want to put it after the DYNAMIC segment. */
12bd6957 11918 pm = &elf_seg_map (abfd);
b49e97c9
TS
11919 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11920 pm = &(*pm)->next;
11921 if (*pm != NULL)
11922 pm = &(*pm)->next;
11923
11924 m->next = *pm;
11925 *pm = m;
11926 }
11927 }
11928 }
8dc1a139 11929 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
11930 .dynstr, .dynsym, and .hash sections, and everything in
11931 between. */
12bd6957 11932 for (pm = &elf_seg_map (abfd); *pm != NULL;
b49e97c9
TS
11933 pm = &(*pm)->next)
11934 if ((*pm)->p_type == PT_DYNAMIC)
11935 break;
11936 m = *pm;
f6f62d6f
RS
11937 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11938 glibc's dynamic linker has traditionally derived the number of
11939 tags from the p_filesz field, and sometimes allocates stack
11940 arrays of that size. An overly-big PT_DYNAMIC segment can
11941 be actively harmful in such cases. Making PT_DYNAMIC contain
11942 other sections can also make life hard for the prelinker,
11943 which might move one of the other sections to a different
11944 PT_LOAD segment. */
11945 if (SGI_COMPAT (abfd)
11946 && m != NULL
11947 && m->count == 1
11948 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
11949 {
11950 static const char *sec_names[] =
11951 {
11952 ".dynamic", ".dynstr", ".dynsym", ".hash"
11953 };
11954 bfd_vma low, high;
11955 unsigned int i, c;
11956 struct elf_segment_map *n;
11957
792b4a53 11958 low = ~(bfd_vma) 0;
b49e97c9
TS
11959 high = 0;
11960 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11961 {
11962 s = bfd_get_section_by_name (abfd, sec_names[i]);
11963 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11964 {
11965 bfd_size_type sz;
11966
11967 if (low > s->vma)
11968 low = s->vma;
eea6121a 11969 sz = s->size;
b49e97c9
TS
11970 if (high < s->vma + sz)
11971 high = s->vma + sz;
11972 }
11973 }
11974
11975 c = 0;
11976 for (s = abfd->sections; s != NULL; s = s->next)
11977 if ((s->flags & SEC_LOAD) != 0
11978 && s->vma >= low
eea6121a 11979 && s->vma + s->size <= high)
b49e97c9
TS
11980 ++c;
11981
11982 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 11983 n = bfd_zalloc (abfd, amt);
b49e97c9 11984 if (n == NULL)
b34976b6 11985 return FALSE;
b49e97c9
TS
11986 *n = *m;
11987 n->count = c;
11988
11989 i = 0;
11990 for (s = abfd->sections; s != NULL; s = s->next)
11991 {
11992 if ((s->flags & SEC_LOAD) != 0
11993 && s->vma >= low
eea6121a 11994 && s->vma + s->size <= high)
b49e97c9
TS
11995 {
11996 n->sections[i] = s;
11997 ++i;
11998 }
11999 }
12000
12001 *pm = n;
12002 }
12003 }
12004
98c904a8
RS
12005 /* Allocate a spare program header in dynamic objects so that tools
12006 like the prelinker can add an extra PT_LOAD entry.
12007
12008 If the prelinker needs to make room for a new PT_LOAD entry, its
12009 standard procedure is to move the first (read-only) sections into
12010 the new (writable) segment. However, the MIPS ABI requires
12011 .dynamic to be in a read-only segment, and the section will often
12012 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12013
12014 Although the prelinker could in principle move .dynamic to a
12015 writable segment, it seems better to allocate a spare program
12016 header instead, and avoid the need to move any sections.
12017 There is a long tradition of allocating spare dynamic tags,
12018 so allocating a spare program header seems like a natural
7c8b76cc
JM
12019 extension.
12020
12021 If INFO is NULL, we may be copying an already prelinked binary
12022 with objcopy or strip, so do not add this header. */
12023 if (info != NULL
12024 && !SGI_COMPAT (abfd)
98c904a8
RS
12025 && bfd_get_section_by_name (abfd, ".dynamic"))
12026 {
12bd6957 12027 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
98c904a8
RS
12028 if ((*pm)->p_type == PT_NULL)
12029 break;
12030 if (*pm == NULL)
12031 {
12032 m = bfd_zalloc (abfd, sizeof (*m));
12033 if (m == NULL)
12034 return FALSE;
12035
12036 m->p_type = PT_NULL;
12037 *pm = m;
12038 }
12039 }
12040
b34976b6 12041 return TRUE;
b49e97c9
TS
12042}
12043\f
12044/* Return the section that should be marked against GC for a given
12045 relocation. */
12046
12047asection *
9719ad41 12048_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 12049 struct bfd_link_info *info,
9719ad41
RS
12050 Elf_Internal_Rela *rel,
12051 struct elf_link_hash_entry *h,
12052 Elf_Internal_Sym *sym)
b49e97c9
TS
12053{
12054 /* ??? Do mips16 stub sections need to be handled special? */
12055
12056 if (h != NULL)
07adf181
AM
12057 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12058 {
12059 case R_MIPS_GNU_VTINHERIT:
12060 case R_MIPS_GNU_VTENTRY:
12061 return NULL;
12062 }
b49e97c9 12063
07adf181 12064 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
12065}
12066
12067/* Update the got entry reference counts for the section being removed. */
12068
b34976b6 12069bfd_boolean
9719ad41
RS
12070_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
12071 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12072 asection *sec ATTRIBUTE_UNUSED,
12073 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
12074{
12075#if 0
12076 Elf_Internal_Shdr *symtab_hdr;
12077 struct elf_link_hash_entry **sym_hashes;
12078 bfd_signed_vma *local_got_refcounts;
12079 const Elf_Internal_Rela *rel, *relend;
12080 unsigned long r_symndx;
12081 struct elf_link_hash_entry *h;
12082
7dda2462
TG
12083 if (info->relocatable)
12084 return TRUE;
12085
b49e97c9
TS
12086 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12087 sym_hashes = elf_sym_hashes (abfd);
12088 local_got_refcounts = elf_local_got_refcounts (abfd);
12089
12090 relend = relocs + sec->reloc_count;
12091 for (rel = relocs; rel < relend; rel++)
12092 switch (ELF_R_TYPE (abfd, rel->r_info))
12093 {
738e5348
RS
12094 case R_MIPS16_GOT16:
12095 case R_MIPS16_CALL16:
b49e97c9
TS
12096 case R_MIPS_GOT16:
12097 case R_MIPS_CALL16:
12098 case R_MIPS_CALL_HI16:
12099 case R_MIPS_CALL_LO16:
12100 case R_MIPS_GOT_HI16:
12101 case R_MIPS_GOT_LO16:
4a14403c
TS
12102 case R_MIPS_GOT_DISP:
12103 case R_MIPS_GOT_PAGE:
12104 case R_MIPS_GOT_OFST:
df58fc94
RS
12105 case R_MICROMIPS_GOT16:
12106 case R_MICROMIPS_CALL16:
12107 case R_MICROMIPS_CALL_HI16:
12108 case R_MICROMIPS_CALL_LO16:
12109 case R_MICROMIPS_GOT_HI16:
12110 case R_MICROMIPS_GOT_LO16:
12111 case R_MICROMIPS_GOT_DISP:
12112 case R_MICROMIPS_GOT_PAGE:
12113 case R_MICROMIPS_GOT_OFST:
b49e97c9
TS
12114 /* ??? It would seem that the existing MIPS code does no sort
12115 of reference counting or whatnot on its GOT and PLT entries,
12116 so it is not possible to garbage collect them at this time. */
12117 break;
12118
12119 default:
12120 break;
12121 }
12122#endif
12123
b34976b6 12124 return TRUE;
b49e97c9
TS
12125}
12126\f
12127/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12128 hiding the old indirect symbol. Process additional relocation
12129 information. Also called for weakdefs, in which case we just let
12130 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12131
12132void
fcfa13d2 12133_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
12134 struct elf_link_hash_entry *dir,
12135 struct elf_link_hash_entry *ind)
b49e97c9
TS
12136{
12137 struct mips_elf_link_hash_entry *dirmips, *indmips;
12138
fcfa13d2 12139 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 12140
861fb55a
DJ
12141 dirmips = (struct mips_elf_link_hash_entry *) dir;
12142 indmips = (struct mips_elf_link_hash_entry *) ind;
12143 /* Any absolute non-dynamic relocations against an indirect or weak
12144 definition will be against the target symbol. */
12145 if (indmips->has_static_relocs)
12146 dirmips->has_static_relocs = TRUE;
12147
b49e97c9
TS
12148 if (ind->root.type != bfd_link_hash_indirect)
12149 return;
12150
b49e97c9
TS
12151 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12152 if (indmips->readonly_reloc)
b34976b6 12153 dirmips->readonly_reloc = TRUE;
b49e97c9 12154 if (indmips->no_fn_stub)
b34976b6 12155 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
12156 if (indmips->fn_stub)
12157 {
12158 dirmips->fn_stub = indmips->fn_stub;
12159 indmips->fn_stub = NULL;
12160 }
12161 if (indmips->need_fn_stub)
12162 {
12163 dirmips->need_fn_stub = TRUE;
12164 indmips->need_fn_stub = FALSE;
12165 }
12166 if (indmips->call_stub)
12167 {
12168 dirmips->call_stub = indmips->call_stub;
12169 indmips->call_stub = NULL;
12170 }
12171 if (indmips->call_fp_stub)
12172 {
12173 dirmips->call_fp_stub = indmips->call_fp_stub;
12174 indmips->call_fp_stub = NULL;
12175 }
634835ae
RS
12176 if (indmips->global_got_area < dirmips->global_got_area)
12177 dirmips->global_got_area = indmips->global_got_area;
12178 if (indmips->global_got_area < GGA_NONE)
12179 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
12180 if (indmips->has_nonpic_branches)
12181 dirmips->has_nonpic_branches = TRUE;
b49e97c9 12182}
b49e97c9 12183\f
d01414a5
TS
12184#define PDR_SIZE 32
12185
b34976b6 12186bfd_boolean
9719ad41
RS
12187_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12188 struct bfd_link_info *info)
d01414a5
TS
12189{
12190 asection *o;
b34976b6 12191 bfd_boolean ret = FALSE;
d01414a5
TS
12192 unsigned char *tdata;
12193 size_t i, skip;
12194
12195 o = bfd_get_section_by_name (abfd, ".pdr");
12196 if (! o)
b34976b6 12197 return FALSE;
eea6121a 12198 if (o->size == 0)
b34976b6 12199 return FALSE;
eea6121a 12200 if (o->size % PDR_SIZE != 0)
b34976b6 12201 return FALSE;
d01414a5
TS
12202 if (o->output_section != NULL
12203 && bfd_is_abs_section (o->output_section))
b34976b6 12204 return FALSE;
d01414a5 12205
eea6121a 12206 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 12207 if (! tdata)
b34976b6 12208 return FALSE;
d01414a5 12209
9719ad41 12210 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 12211 info->keep_memory);
d01414a5
TS
12212 if (!cookie->rels)
12213 {
12214 free (tdata);
b34976b6 12215 return FALSE;
d01414a5
TS
12216 }
12217
12218 cookie->rel = cookie->rels;
12219 cookie->relend = cookie->rels + o->reloc_count;
12220
eea6121a 12221 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 12222 {
c152c796 12223 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
12224 {
12225 tdata[i] = 1;
12226 skip ++;
12227 }
12228 }
12229
12230 if (skip != 0)
12231 {
f0abc2a1 12232 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 12233 o->size -= skip * PDR_SIZE;
b34976b6 12234 ret = TRUE;
d01414a5
TS
12235 }
12236 else
12237 free (tdata);
12238
12239 if (! info->keep_memory)
12240 free (cookie->rels);
12241
12242 return ret;
12243}
12244
b34976b6 12245bfd_boolean
9719ad41 12246_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
12247{
12248 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
12249 return TRUE;
12250 return FALSE;
53bfd6b4 12251}
d01414a5 12252
b34976b6 12253bfd_boolean
c7b8f16e
JB
12254_bfd_mips_elf_write_section (bfd *output_bfd,
12255 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12256 asection *sec, bfd_byte *contents)
d01414a5
TS
12257{
12258 bfd_byte *to, *from, *end;
12259 int i;
12260
12261 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 12262 return FALSE;
d01414a5 12263
f0abc2a1 12264 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 12265 return FALSE;
d01414a5
TS
12266
12267 to = contents;
eea6121a 12268 end = contents + sec->size;
d01414a5
TS
12269 for (from = contents, i = 0;
12270 from < end;
12271 from += PDR_SIZE, i++)
12272 {
f0abc2a1 12273 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
12274 continue;
12275 if (to != from)
12276 memcpy (to, from, PDR_SIZE);
12277 to += PDR_SIZE;
12278 }
12279 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 12280 sec->output_offset, sec->size);
b34976b6 12281 return TRUE;
d01414a5 12282}
53bfd6b4 12283\f
df58fc94
RS
12284/* microMIPS code retains local labels for linker relaxation. Omit them
12285 from output by default for clarity. */
12286
12287bfd_boolean
12288_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12289{
12290 return _bfd_elf_is_local_label_name (abfd, sym->name);
12291}
12292
b49e97c9
TS
12293/* MIPS ELF uses a special find_nearest_line routine in order the
12294 handle the ECOFF debugging information. */
12295
12296struct mips_elf_find_line
12297{
12298 struct ecoff_debug_info d;
12299 struct ecoff_find_line i;
12300};
12301
b34976b6 12302bfd_boolean
9719ad41
RS
12303_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
12304 asymbol **symbols, bfd_vma offset,
12305 const char **filename_ptr,
12306 const char **functionname_ptr,
12307 unsigned int *line_ptr)
b49e97c9
TS
12308{
12309 asection *msec;
12310
12311 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
12312 filename_ptr, functionname_ptr,
12313 line_ptr))
b34976b6 12314 return TRUE;
b49e97c9 12315
fc28f9aa
TG
12316 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
12317 section, symbols, offset,
b49e97c9 12318 filename_ptr, functionname_ptr,
9b8d1a36 12319 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 12320 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 12321 return TRUE;
b49e97c9
TS
12322
12323 msec = bfd_get_section_by_name (abfd, ".mdebug");
12324 if (msec != NULL)
12325 {
12326 flagword origflags;
12327 struct mips_elf_find_line *fi;
12328 const struct ecoff_debug_swap * const swap =
12329 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12330
12331 /* If we are called during a link, mips_elf_final_link may have
12332 cleared the SEC_HAS_CONTENTS field. We force it back on here
12333 if appropriate (which it normally will be). */
12334 origflags = msec->flags;
12335 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12336 msec->flags |= SEC_HAS_CONTENTS;
12337
698600e4 12338 fi = mips_elf_tdata (abfd)->find_line_info;
b49e97c9
TS
12339 if (fi == NULL)
12340 {
12341 bfd_size_type external_fdr_size;
12342 char *fraw_src;
12343 char *fraw_end;
12344 struct fdr *fdr_ptr;
12345 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12346
9719ad41 12347 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
12348 if (fi == NULL)
12349 {
12350 msec->flags = origflags;
b34976b6 12351 return FALSE;
b49e97c9
TS
12352 }
12353
12354 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12355 {
12356 msec->flags = origflags;
b34976b6 12357 return FALSE;
b49e97c9
TS
12358 }
12359
12360 /* Swap in the FDR information. */
12361 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 12362 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
12363 if (fi->d.fdr == NULL)
12364 {
12365 msec->flags = origflags;
b34976b6 12366 return FALSE;
b49e97c9
TS
12367 }
12368 external_fdr_size = swap->external_fdr_size;
12369 fdr_ptr = fi->d.fdr;
12370 fraw_src = (char *) fi->d.external_fdr;
12371 fraw_end = (fraw_src
12372 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12373 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 12374 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9 12375
698600e4 12376 mips_elf_tdata (abfd)->find_line_info = fi;
b49e97c9
TS
12377
12378 /* Note that we don't bother to ever free this information.
12379 find_nearest_line is either called all the time, as in
12380 objdump -l, so the information should be saved, or it is
12381 rarely called, as in ld error messages, so the memory
12382 wasted is unimportant. Still, it would probably be a
12383 good idea for free_cached_info to throw it away. */
12384 }
12385
12386 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
12387 &fi->i, filename_ptr, functionname_ptr,
12388 line_ptr))
12389 {
12390 msec->flags = origflags;
b34976b6 12391 return TRUE;
b49e97c9
TS
12392 }
12393
12394 msec->flags = origflags;
12395 }
12396
12397 /* Fall back on the generic ELF find_nearest_line routine. */
12398
12399 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
12400 filename_ptr, functionname_ptr,
12401 line_ptr);
12402}
4ab527b0
FF
12403
12404bfd_boolean
12405_bfd_mips_elf_find_inliner_info (bfd *abfd,
12406 const char **filename_ptr,
12407 const char **functionname_ptr,
12408 unsigned int *line_ptr)
12409{
12410 bfd_boolean found;
12411 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12412 functionname_ptr, line_ptr,
12413 & elf_tdata (abfd)->dwarf2_find_line_info);
12414 return found;
12415}
12416
b49e97c9
TS
12417\f
12418/* When are writing out the .options or .MIPS.options section,
12419 remember the bytes we are writing out, so that we can install the
12420 GP value in the section_processing routine. */
12421
b34976b6 12422bfd_boolean
9719ad41
RS
12423_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
12424 const void *location,
12425 file_ptr offset, bfd_size_type count)
b49e97c9 12426{
cc2e31b9 12427 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
12428 {
12429 bfd_byte *c;
12430
12431 if (elf_section_data (section) == NULL)
12432 {
12433 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 12434 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 12435 if (elf_section_data (section) == NULL)
b34976b6 12436 return FALSE;
b49e97c9 12437 }
f0abc2a1 12438 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
12439 if (c == NULL)
12440 {
eea6121a 12441 c = bfd_zalloc (abfd, section->size);
b49e97c9 12442 if (c == NULL)
b34976b6 12443 return FALSE;
f0abc2a1 12444 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
12445 }
12446
9719ad41 12447 memcpy (c + offset, location, count);
b49e97c9
TS
12448 }
12449
12450 return _bfd_elf_set_section_contents (abfd, section, location, offset,
12451 count);
12452}
12453
12454/* This is almost identical to bfd_generic_get_... except that some
12455 MIPS relocations need to be handled specially. Sigh. */
12456
12457bfd_byte *
9719ad41
RS
12458_bfd_elf_mips_get_relocated_section_contents
12459 (bfd *abfd,
12460 struct bfd_link_info *link_info,
12461 struct bfd_link_order *link_order,
12462 bfd_byte *data,
12463 bfd_boolean relocatable,
12464 asymbol **symbols)
b49e97c9
TS
12465{
12466 /* Get enough memory to hold the stuff */
12467 bfd *input_bfd = link_order->u.indirect.section->owner;
12468 asection *input_section = link_order->u.indirect.section;
eea6121a 12469 bfd_size_type sz;
b49e97c9
TS
12470
12471 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12472 arelent **reloc_vector = NULL;
12473 long reloc_count;
12474
12475 if (reloc_size < 0)
12476 goto error_return;
12477
9719ad41 12478 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
12479 if (reloc_vector == NULL && reloc_size != 0)
12480 goto error_return;
12481
12482 /* read in the section */
eea6121a
AM
12483 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
12484 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
12485 goto error_return;
12486
b49e97c9
TS
12487 reloc_count = bfd_canonicalize_reloc (input_bfd,
12488 input_section,
12489 reloc_vector,
12490 symbols);
12491 if (reloc_count < 0)
12492 goto error_return;
12493
12494 if (reloc_count > 0)
12495 {
12496 arelent **parent;
12497 /* for mips */
12498 int gp_found;
12499 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
12500
12501 {
12502 struct bfd_hash_entry *h;
12503 struct bfd_link_hash_entry *lh;
12504 /* Skip all this stuff if we aren't mixing formats. */
12505 if (abfd && input_bfd
12506 && abfd->xvec == input_bfd->xvec)
12507 lh = 0;
12508 else
12509 {
b34976b6 12510 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
12511 lh = (struct bfd_link_hash_entry *) h;
12512 }
12513 lookup:
12514 if (lh)
12515 {
12516 switch (lh->type)
12517 {
12518 case bfd_link_hash_undefined:
12519 case bfd_link_hash_undefweak:
12520 case bfd_link_hash_common:
12521 gp_found = 0;
12522 break;
12523 case bfd_link_hash_defined:
12524 case bfd_link_hash_defweak:
12525 gp_found = 1;
12526 gp = lh->u.def.value;
12527 break;
12528 case bfd_link_hash_indirect:
12529 case bfd_link_hash_warning:
12530 lh = lh->u.i.link;
12531 /* @@FIXME ignoring warning for now */
12532 goto lookup;
12533 case bfd_link_hash_new:
12534 default:
12535 abort ();
12536 }
12537 }
12538 else
12539 gp_found = 0;
12540 }
12541 /* end mips */
9719ad41 12542 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 12543 {
9719ad41 12544 char *error_message = NULL;
b49e97c9
TS
12545 bfd_reloc_status_type r;
12546
12547 /* Specific to MIPS: Deal with relocation types that require
12548 knowing the gp of the output bfd. */
12549 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 12550
8236346f
EC
12551 /* If we've managed to find the gp and have a special
12552 function for the relocation then go ahead, else default
12553 to the generic handling. */
12554 if (gp_found
12555 && (*parent)->howto->special_function
12556 == _bfd_mips_elf32_gprel16_reloc)
12557 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
12558 input_section, relocatable,
12559 data, gp);
12560 else
86324f90 12561 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
12562 input_section,
12563 relocatable ? abfd : NULL,
12564 &error_message);
b49e97c9 12565
1049f94e 12566 if (relocatable)
b49e97c9
TS
12567 {
12568 asection *os = input_section->output_section;
12569
12570 /* A partial link, so keep the relocs */
12571 os->orelocation[os->reloc_count] = *parent;
12572 os->reloc_count++;
12573 }
12574
12575 if (r != bfd_reloc_ok)
12576 {
12577 switch (r)
12578 {
12579 case bfd_reloc_undefined:
12580 if (!((*link_info->callbacks->undefined_symbol)
12581 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 12582 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
12583 goto error_return;
12584 break;
12585 case bfd_reloc_dangerous:
9719ad41 12586 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
12587 if (!((*link_info->callbacks->reloc_dangerous)
12588 (link_info, error_message, input_bfd, input_section,
12589 (*parent)->address)))
12590 goto error_return;
12591 break;
12592 case bfd_reloc_overflow:
12593 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
12594 (link_info, NULL,
12595 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
12596 (*parent)->howto->name, (*parent)->addend,
12597 input_bfd, input_section, (*parent)->address)))
12598 goto error_return;
12599 break;
12600 case bfd_reloc_outofrange:
12601 default:
12602 abort ();
12603 break;
12604 }
12605
12606 }
12607 }
12608 }
12609 if (reloc_vector != NULL)
12610 free (reloc_vector);
12611 return data;
12612
12613error_return:
12614 if (reloc_vector != NULL)
12615 free (reloc_vector);
12616 return NULL;
12617}
12618\f
df58fc94
RS
12619static bfd_boolean
12620mips_elf_relax_delete_bytes (bfd *abfd,
12621 asection *sec, bfd_vma addr, int count)
12622{
12623 Elf_Internal_Shdr *symtab_hdr;
12624 unsigned int sec_shndx;
12625 bfd_byte *contents;
12626 Elf_Internal_Rela *irel, *irelend;
12627 Elf_Internal_Sym *isym;
12628 Elf_Internal_Sym *isymend;
12629 struct elf_link_hash_entry **sym_hashes;
12630 struct elf_link_hash_entry **end_hashes;
12631 struct elf_link_hash_entry **start_hashes;
12632 unsigned int symcount;
12633
12634 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
12635 contents = elf_section_data (sec)->this_hdr.contents;
12636
12637 irel = elf_section_data (sec)->relocs;
12638 irelend = irel + sec->reloc_count;
12639
12640 /* Actually delete the bytes. */
12641 memmove (contents + addr, contents + addr + count,
12642 (size_t) (sec->size - addr - count));
12643 sec->size -= count;
12644
12645 /* Adjust all the relocs. */
12646 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12647 {
12648 /* Get the new reloc address. */
12649 if (irel->r_offset > addr)
12650 irel->r_offset -= count;
12651 }
12652
12653 BFD_ASSERT (addr % 2 == 0);
12654 BFD_ASSERT (count % 2 == 0);
12655
12656 /* Adjust the local symbols defined in this section. */
12657 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12658 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12659 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2309ddf2 12660 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
df58fc94
RS
12661 isym->st_value -= count;
12662
12663 /* Now adjust the global symbols defined in this section. */
12664 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
12665 - symtab_hdr->sh_info);
12666 sym_hashes = start_hashes = elf_sym_hashes (abfd);
12667 end_hashes = sym_hashes + symcount;
12668
12669 for (; sym_hashes < end_hashes; sym_hashes++)
12670 {
12671 struct elf_link_hash_entry *sym_hash = *sym_hashes;
12672
12673 if ((sym_hash->root.type == bfd_link_hash_defined
12674 || sym_hash->root.type == bfd_link_hash_defweak)
12675 && sym_hash->root.u.def.section == sec)
12676 {
2309ddf2 12677 bfd_vma value = sym_hash->root.u.def.value;
df58fc94 12678
df58fc94
RS
12679 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
12680 value &= MINUS_TWO;
12681 if (value > addr)
12682 sym_hash->root.u.def.value -= count;
12683 }
12684 }
12685
12686 return TRUE;
12687}
12688
12689
12690/* Opcodes needed for microMIPS relaxation as found in
12691 opcodes/micromips-opc.c. */
12692
12693struct opcode_descriptor {
12694 unsigned long match;
12695 unsigned long mask;
12696};
12697
12698/* The $ra register aka $31. */
12699
12700#define RA 31
12701
12702/* 32-bit instruction format register fields. */
12703
12704#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12705#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12706
12707/* Check if a 5-bit register index can be abbreviated to 3 bits. */
12708
12709#define OP16_VALID_REG(r) \
12710 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12711
12712
12713/* 32-bit and 16-bit branches. */
12714
12715static const struct opcode_descriptor b_insns_32[] = {
12716 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12717 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12718 { 0, 0 } /* End marker for find_match(). */
12719};
12720
12721static const struct opcode_descriptor bc_insn_32 =
12722 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12723
12724static const struct opcode_descriptor bz_insn_32 =
12725 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12726
12727static const struct opcode_descriptor bzal_insn_32 =
12728 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12729
12730static const struct opcode_descriptor beq_insn_32 =
12731 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12732
12733static const struct opcode_descriptor b_insn_16 =
12734 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12735
12736static const struct opcode_descriptor bz_insn_16 =
c088dedf 12737 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
df58fc94
RS
12738
12739
12740/* 32-bit and 16-bit branch EQ and NE zero. */
12741
12742/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12743 eq and second the ne. This convention is used when replacing a
12744 32-bit BEQ/BNE with the 16-bit version. */
12745
12746#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12747
12748static const struct opcode_descriptor bz_rs_insns_32[] = {
12749 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12750 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12751 { 0, 0 } /* End marker for find_match(). */
12752};
12753
12754static const struct opcode_descriptor bz_rt_insns_32[] = {
12755 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12756 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12757 { 0, 0 } /* End marker for find_match(). */
12758};
12759
12760static const struct opcode_descriptor bzc_insns_32[] = {
12761 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12762 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12763 { 0, 0 } /* End marker for find_match(). */
12764};
12765
12766static const struct opcode_descriptor bz_insns_16[] = {
12767 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12768 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12769 { 0, 0 } /* End marker for find_match(). */
12770};
12771
12772/* Switch between a 5-bit register index and its 3-bit shorthand. */
12773
12774#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12775#define BZ16_REG_FIELD(r) \
12776 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12777
12778
12779/* 32-bit instructions with a delay slot. */
12780
12781static const struct opcode_descriptor jal_insn_32_bd16 =
12782 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12783
12784static const struct opcode_descriptor jal_insn_32_bd32 =
12785 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12786
12787static const struct opcode_descriptor jal_x_insn_32_bd32 =
12788 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12789
12790static const struct opcode_descriptor j_insn_32 =
12791 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12792
12793static const struct opcode_descriptor jalr_insn_32 =
12794 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12795
12796/* This table can be compacted, because no opcode replacement is made. */
12797
12798static const struct opcode_descriptor ds_insns_32_bd16[] = {
12799 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12800
12801 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12802 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12803
12804 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12805 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12806 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12807 { 0, 0 } /* End marker for find_match(). */
12808};
12809
12810/* This table can be compacted, because no opcode replacement is made. */
12811
12812static const struct opcode_descriptor ds_insns_32_bd32[] = {
12813 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12814
12815 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12816 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12817 { 0, 0 } /* End marker for find_match(). */
12818};
12819
12820
12821/* 16-bit instructions with a delay slot. */
12822
12823static const struct opcode_descriptor jalr_insn_16_bd16 =
12824 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12825
12826static const struct opcode_descriptor jalr_insn_16_bd32 =
12827 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12828
12829static const struct opcode_descriptor jr_insn_16 =
12830 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12831
12832#define JR16_REG(opcode) ((opcode) & 0x1f)
12833
12834/* This table can be compacted, because no opcode replacement is made. */
12835
12836static const struct opcode_descriptor ds_insns_16_bd16[] = {
12837 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12838
12839 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12840 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12841 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12842 { 0, 0 } /* End marker for find_match(). */
12843};
12844
12845
12846/* LUI instruction. */
12847
12848static const struct opcode_descriptor lui_insn =
12849 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12850
12851
12852/* ADDIU instruction. */
12853
12854static const struct opcode_descriptor addiu_insn =
12855 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12856
12857static const struct opcode_descriptor addiupc_insn =
12858 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12859
12860#define ADDIUPC_REG_FIELD(r) \
12861 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12862
12863
12864/* Relaxable instructions in a JAL delay slot: MOVE. */
12865
12866/* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12867 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12868#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12869#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12870
12871#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12872#define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12873
12874static const struct opcode_descriptor move_insns_32[] = {
12875 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12876 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12877 { 0, 0 } /* End marker for find_match(). */
12878};
12879
12880static const struct opcode_descriptor move_insn_16 =
12881 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12882
12883
12884/* NOP instructions. */
12885
12886static const struct opcode_descriptor nop_insn_32 =
12887 { /* "nop", "", */ 0x00000000, 0xffffffff };
12888
12889static const struct opcode_descriptor nop_insn_16 =
12890 { /* "nop", "", */ 0x0c00, 0xffff };
12891
12892
12893/* Instruction match support. */
12894
12895#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12896
12897static int
12898find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12899{
12900 unsigned long indx;
12901
12902 for (indx = 0; insn[indx].mask != 0; indx++)
12903 if (MATCH (opcode, insn[indx]))
12904 return indx;
12905
12906 return -1;
12907}
12908
12909
12910/* Branch and delay slot decoding support. */
12911
12912/* If PTR points to what *might* be a 16-bit branch or jump, then
12913 return the minimum length of its delay slot, otherwise return 0.
12914 Non-zero results are not definitive as we might be checking against
12915 the second half of another instruction. */
12916
12917static int
12918check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12919{
12920 unsigned long opcode;
12921 int bdsize;
12922
12923 opcode = bfd_get_16 (abfd, ptr);
12924 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12925 /* 16-bit branch/jump with a 32-bit delay slot. */
12926 bdsize = 4;
12927 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12928 || find_match (opcode, ds_insns_16_bd16) >= 0)
12929 /* 16-bit branch/jump with a 16-bit delay slot. */
12930 bdsize = 2;
12931 else
12932 /* No delay slot. */
12933 bdsize = 0;
12934
12935 return bdsize;
12936}
12937
12938/* If PTR points to what *might* be a 32-bit branch or jump, then
12939 return the minimum length of its delay slot, otherwise return 0.
12940 Non-zero results are not definitive as we might be checking against
12941 the second half of another instruction. */
12942
12943static int
12944check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12945{
12946 unsigned long opcode;
12947 int bdsize;
12948
d21911ea 12949 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
12950 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12951 /* 32-bit branch/jump with a 32-bit delay slot. */
12952 bdsize = 4;
12953 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12954 /* 32-bit branch/jump with a 16-bit delay slot. */
12955 bdsize = 2;
12956 else
12957 /* No delay slot. */
12958 bdsize = 0;
12959
12960 return bdsize;
12961}
12962
12963/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12964 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12965
12966static bfd_boolean
12967check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12968{
12969 unsigned long opcode;
12970
12971 opcode = bfd_get_16 (abfd, ptr);
12972 if (MATCH (opcode, b_insn_16)
12973 /* B16 */
12974 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12975 /* JR16 */
12976 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12977 /* BEQZ16, BNEZ16 */
12978 || (MATCH (opcode, jalr_insn_16_bd32)
12979 /* JALR16 */
12980 && reg != JR16_REG (opcode) && reg != RA))
12981 return TRUE;
12982
12983 return FALSE;
12984}
12985
12986/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12987 then return TRUE, otherwise FALSE. */
12988
f41e5fcc 12989static bfd_boolean
df58fc94
RS
12990check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12991{
12992 unsigned long opcode;
12993
d21911ea 12994 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
12995 if (MATCH (opcode, j_insn_32)
12996 /* J */
12997 || MATCH (opcode, bc_insn_32)
12998 /* BC1F, BC1T, BC2F, BC2T */
12999 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13000 /* JAL, JALX */
13001 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13002 /* BGEZ, BGTZ, BLEZ, BLTZ */
13003 || (MATCH (opcode, bzal_insn_32)
13004 /* BGEZAL, BLTZAL */
13005 && reg != OP32_SREG (opcode) && reg != RA)
13006 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13007 /* JALR, JALR.HB, BEQ, BNE */
13008 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13009 return TRUE;
13010
13011 return FALSE;
13012}
13013
80cab405
MR
13014/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13015 IRELEND) at OFFSET indicate that there must be a compact branch there,
13016 then return TRUE, otherwise FALSE. */
df58fc94
RS
13017
13018static bfd_boolean
80cab405
MR
13019check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13020 const Elf_Internal_Rela *internal_relocs,
13021 const Elf_Internal_Rela *irelend)
df58fc94 13022{
80cab405
MR
13023 const Elf_Internal_Rela *irel;
13024 unsigned long opcode;
13025
d21911ea 13026 opcode = bfd_get_micromips_32 (abfd, ptr);
80cab405
MR
13027 if (find_match (opcode, bzc_insns_32) < 0)
13028 return FALSE;
df58fc94
RS
13029
13030 for (irel = internal_relocs; irel < irelend; irel++)
80cab405
MR
13031 if (irel->r_offset == offset
13032 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13033 return TRUE;
13034
df58fc94
RS
13035 return FALSE;
13036}
80cab405
MR
13037
13038/* Bitsize checking. */
13039#define IS_BITSIZE(val, N) \
13040 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13041 - (1ULL << ((N) - 1))) == (val))
13042
df58fc94
RS
13043\f
13044bfd_boolean
13045_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13046 struct bfd_link_info *link_info,
13047 bfd_boolean *again)
13048{
833794fc 13049 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
df58fc94
RS
13050 Elf_Internal_Shdr *symtab_hdr;
13051 Elf_Internal_Rela *internal_relocs;
13052 Elf_Internal_Rela *irel, *irelend;
13053 bfd_byte *contents = NULL;
13054 Elf_Internal_Sym *isymbuf = NULL;
13055
13056 /* Assume nothing changes. */
13057 *again = FALSE;
13058
13059 /* We don't have to do anything for a relocatable link, if
13060 this section does not have relocs, or if this is not a
13061 code section. */
13062
13063 if (link_info->relocatable
13064 || (sec->flags & SEC_RELOC) == 0
13065 || sec->reloc_count == 0
13066 || (sec->flags & SEC_CODE) == 0)
13067 return TRUE;
13068
13069 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13070
13071 /* Get a copy of the native relocations. */
13072 internal_relocs = (_bfd_elf_link_read_relocs
2c3fc389 13073 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
df58fc94
RS
13074 link_info->keep_memory));
13075 if (internal_relocs == NULL)
13076 goto error_return;
13077
13078 /* Walk through them looking for relaxing opportunities. */
13079 irelend = internal_relocs + sec->reloc_count;
13080 for (irel = internal_relocs; irel < irelend; irel++)
13081 {
13082 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13083 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13084 bfd_boolean target_is_micromips_code_p;
13085 unsigned long opcode;
13086 bfd_vma symval;
13087 bfd_vma pcrval;
2309ddf2 13088 bfd_byte *ptr;
df58fc94
RS
13089 int fndopc;
13090
13091 /* The number of bytes to delete for relaxation and from where
13092 to delete these bytes starting at irel->r_offset. */
13093 int delcnt = 0;
13094 int deloff = 0;
13095
13096 /* If this isn't something that can be relaxed, then ignore
13097 this reloc. */
13098 if (r_type != R_MICROMIPS_HI16
13099 && r_type != R_MICROMIPS_PC16_S1
2309ddf2 13100 && r_type != R_MICROMIPS_26_S1)
df58fc94
RS
13101 continue;
13102
13103 /* Get the section contents if we haven't done so already. */
13104 if (contents == NULL)
13105 {
13106 /* Get cached copy if it exists. */
13107 if (elf_section_data (sec)->this_hdr.contents != NULL)
13108 contents = elf_section_data (sec)->this_hdr.contents;
13109 /* Go get them off disk. */
13110 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13111 goto error_return;
13112 }
2309ddf2 13113 ptr = contents + irel->r_offset;
df58fc94
RS
13114
13115 /* Read this BFD's local symbols if we haven't done so already. */
13116 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13117 {
13118 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13119 if (isymbuf == NULL)
13120 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13121 symtab_hdr->sh_info, 0,
13122 NULL, NULL, NULL);
13123 if (isymbuf == NULL)
13124 goto error_return;
13125 }
13126
13127 /* Get the value of the symbol referred to by the reloc. */
13128 if (r_symndx < symtab_hdr->sh_info)
13129 {
13130 /* A local symbol. */
13131 Elf_Internal_Sym *isym;
13132 asection *sym_sec;
13133
13134 isym = isymbuf + r_symndx;
13135 if (isym->st_shndx == SHN_UNDEF)
13136 sym_sec = bfd_und_section_ptr;
13137 else if (isym->st_shndx == SHN_ABS)
13138 sym_sec = bfd_abs_section_ptr;
13139 else if (isym->st_shndx == SHN_COMMON)
13140 sym_sec = bfd_com_section_ptr;
13141 else
13142 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13143 symval = (isym->st_value
13144 + sym_sec->output_section->vma
13145 + sym_sec->output_offset);
13146 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13147 }
13148 else
13149 {
13150 unsigned long indx;
13151 struct elf_link_hash_entry *h;
13152
13153 /* An external symbol. */
13154 indx = r_symndx - symtab_hdr->sh_info;
13155 h = elf_sym_hashes (abfd)[indx];
13156 BFD_ASSERT (h != NULL);
13157
13158 if (h->root.type != bfd_link_hash_defined
13159 && h->root.type != bfd_link_hash_defweak)
13160 /* This appears to be a reference to an undefined
13161 symbol. Just ignore it -- it will be caught by the
13162 regular reloc processing. */
13163 continue;
13164
13165 symval = (h->root.u.def.value
13166 + h->root.u.def.section->output_section->vma
13167 + h->root.u.def.section->output_offset);
13168 target_is_micromips_code_p = (!h->needs_plt
13169 && ELF_ST_IS_MICROMIPS (h->other));
13170 }
13171
13172
13173 /* For simplicity of coding, we are going to modify the
13174 section contents, the section relocs, and the BFD symbol
13175 table. We must tell the rest of the code not to free up this
13176 information. It would be possible to instead create a table
13177 of changes which have to be made, as is done in coff-mips.c;
13178 that would be more work, but would require less memory when
13179 the linker is run. */
13180
13181 /* Only 32-bit instructions relaxed. */
13182 if (irel->r_offset + 4 > sec->size)
13183 continue;
13184
d21911ea 13185 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13186
13187 /* This is the pc-relative distance from the instruction the
13188 relocation is applied to, to the symbol referred. */
13189 pcrval = (symval
13190 - (sec->output_section->vma + sec->output_offset)
13191 - irel->r_offset);
13192
13193 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13194 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13195 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13196
13197 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13198
13199 where pcrval has first to be adjusted to apply against the LO16
13200 location (we make the adjustment later on, when we have figured
13201 out the offset). */
13202 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13203 {
80cab405 13204 bfd_boolean bzc = FALSE;
df58fc94
RS
13205 unsigned long nextopc;
13206 unsigned long reg;
13207 bfd_vma offset;
13208
13209 /* Give up if the previous reloc was a HI16 against this symbol
13210 too. */
13211 if (irel > internal_relocs
13212 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13213 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13214 continue;
13215
13216 /* Or if the next reloc is not a LO16 against this symbol. */
13217 if (irel + 1 >= irelend
13218 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13219 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13220 continue;
13221
13222 /* Or if the second next reloc is a LO16 against this symbol too. */
13223 if (irel + 2 >= irelend
13224 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13225 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13226 continue;
13227
80cab405
MR
13228 /* See if the LUI instruction *might* be in a branch delay slot.
13229 We check whether what looks like a 16-bit branch or jump is
13230 actually an immediate argument to a compact branch, and let
13231 it through if so. */
df58fc94 13232 if (irel->r_offset >= 2
2309ddf2 13233 && check_br16_dslot (abfd, ptr - 2)
df58fc94 13234 && !(irel->r_offset >= 4
80cab405
MR
13235 && (bzc = check_relocated_bzc (abfd,
13236 ptr - 4, irel->r_offset - 4,
13237 internal_relocs, irelend))))
df58fc94
RS
13238 continue;
13239 if (irel->r_offset >= 4
80cab405 13240 && !bzc
2309ddf2 13241 && check_br32_dslot (abfd, ptr - 4))
df58fc94
RS
13242 continue;
13243
13244 reg = OP32_SREG (opcode);
13245
13246 /* We only relax adjacent instructions or ones separated with
13247 a branch or jump that has a delay slot. The branch or jump
13248 must not fiddle with the register used to hold the address.
13249 Subtract 4 for the LUI itself. */
13250 offset = irel[1].r_offset - irel[0].r_offset;
13251 switch (offset - 4)
13252 {
13253 case 0:
13254 break;
13255 case 2:
2309ddf2 13256 if (check_br16 (abfd, ptr + 4, reg))
df58fc94
RS
13257 break;
13258 continue;
13259 case 4:
2309ddf2 13260 if (check_br32 (abfd, ptr + 4, reg))
df58fc94
RS
13261 break;
13262 continue;
13263 default:
13264 continue;
13265 }
13266
d21911ea 13267 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
df58fc94
RS
13268
13269 /* Give up unless the same register is used with both
13270 relocations. */
13271 if (OP32_SREG (nextopc) != reg)
13272 continue;
13273
13274 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13275 and rounding up to take masking of the two LSBs into account. */
13276 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13277
13278 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13279 if (IS_BITSIZE (symval, 16))
13280 {
13281 /* Fix the relocation's type. */
13282 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13283
13284 /* Instructions using R_MICROMIPS_LO16 have the base or
13285 source register in bits 20:16. This register becomes $0
13286 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13287 nextopc &= ~0x001f0000;
13288 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13289 contents + irel[1].r_offset);
13290 }
13291
13292 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13293 We add 4 to take LUI deletion into account while checking
13294 the PC-relative distance. */
13295 else if (symval % 4 == 0
13296 && IS_BITSIZE (pcrval + 4, 25)
13297 && MATCH (nextopc, addiu_insn)
13298 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13299 && OP16_VALID_REG (OP32_TREG (nextopc)))
13300 {
13301 /* Fix the relocation's type. */
13302 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13303
13304 /* Replace ADDIU with the ADDIUPC version. */
13305 nextopc = (addiupc_insn.match
13306 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13307
d21911ea
MR
13308 bfd_put_micromips_32 (abfd, nextopc,
13309 contents + irel[1].r_offset);
df58fc94
RS
13310 }
13311
13312 /* Can't do anything, give up, sigh... */
13313 else
13314 continue;
13315
13316 /* Fix the relocation's type. */
13317 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13318
13319 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13320 delcnt = 4;
13321 deloff = 0;
13322 }
13323
13324 /* Compact branch relaxation -- due to the multitude of macros
13325 employed by the compiler/assembler, compact branches are not
13326 always generated. Obviously, this can/will be fixed elsewhere,
13327 but there is no drawback in double checking it here. */
13328 else if (r_type == R_MICROMIPS_PC16_S1
13329 && irel->r_offset + 5 < sec->size
13330 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13331 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
833794fc
MR
13332 && ((!insn32
13333 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13334 nop_insn_16) ? 2 : 0))
13335 || (irel->r_offset + 7 < sec->size
13336 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13337 ptr + 4),
13338 nop_insn_32) ? 4 : 0))))
df58fc94
RS
13339 {
13340 unsigned long reg;
13341
13342 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13343
13344 /* Replace BEQZ/BNEZ with the compact version. */
13345 opcode = (bzc_insns_32[fndopc].match
13346 | BZC32_REG_FIELD (reg)
13347 | (opcode & 0xffff)); /* Addend value. */
13348
d21911ea 13349 bfd_put_micromips_32 (abfd, opcode, ptr);
df58fc94 13350
833794fc
MR
13351 /* Delete the delay slot NOP: two or four bytes from
13352 irel->offset + 4; delcnt has already been set above. */
df58fc94
RS
13353 deloff = 4;
13354 }
13355
13356 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
13357 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
13358 else if (!insn32
13359 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
13360 && IS_BITSIZE (pcrval - 2, 11)
13361 && find_match (opcode, b_insns_32) >= 0)
13362 {
13363 /* Fix the relocation's type. */
13364 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13365
a8685210 13366 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
13367 bfd_put_16 (abfd,
13368 (b_insn_16.match
13369 | (opcode & 0x3ff)), /* Addend value. */
2309ddf2 13370 ptr);
df58fc94
RS
13371
13372 /* Delete 2 bytes from irel->r_offset + 2. */
13373 delcnt = 2;
13374 deloff = 2;
13375 }
13376
13377 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
13378 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
13379 else if (!insn32
13380 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
13381 && IS_BITSIZE (pcrval - 2, 8)
13382 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13383 && OP16_VALID_REG (OP32_SREG (opcode)))
13384 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13385 && OP16_VALID_REG (OP32_TREG (opcode)))))
13386 {
13387 unsigned long reg;
13388
13389 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13390
13391 /* Fix the relocation's type. */
13392 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
13393
a8685210 13394 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
13395 bfd_put_16 (abfd,
13396 (bz_insns_16[fndopc].match
13397 | BZ16_REG_FIELD (reg)
13398 | (opcode & 0x7f)), /* Addend value. */
2309ddf2 13399 ptr);
df58fc94
RS
13400
13401 /* Delete 2 bytes from irel->r_offset + 2. */
13402 delcnt = 2;
13403 deloff = 2;
13404 }
13405
13406 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
833794fc
MR
13407 else if (!insn32
13408 && r_type == R_MICROMIPS_26_S1
df58fc94
RS
13409 && target_is_micromips_code_p
13410 && irel->r_offset + 7 < sec->size
13411 && MATCH (opcode, jal_insn_32_bd32))
13412 {
13413 unsigned long n32opc;
13414 bfd_boolean relaxed = FALSE;
13415
d21911ea 13416 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
df58fc94
RS
13417
13418 if (MATCH (n32opc, nop_insn_32))
13419 {
13420 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
2309ddf2 13421 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
df58fc94
RS
13422
13423 relaxed = TRUE;
13424 }
13425 else if (find_match (n32opc, move_insns_32) >= 0)
13426 {
13427 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
13428 bfd_put_16 (abfd,
13429 (move_insn_16.match
13430 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
13431 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
2309ddf2 13432 ptr + 4);
df58fc94
RS
13433
13434 relaxed = TRUE;
13435 }
13436 /* Other 32-bit instructions relaxable to 16-bit
13437 instructions will be handled here later. */
13438
13439 if (relaxed)
13440 {
13441 /* JAL with 32-bit delay slot that is changed to a JALS
13442 with 16-bit delay slot. */
d21911ea 13443 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
df58fc94
RS
13444
13445 /* Delete 2 bytes from irel->r_offset + 6. */
13446 delcnt = 2;
13447 deloff = 6;
13448 }
13449 }
13450
13451 if (delcnt != 0)
13452 {
13453 /* Note that we've changed the relocs, section contents, etc. */
13454 elf_section_data (sec)->relocs = internal_relocs;
13455 elf_section_data (sec)->this_hdr.contents = contents;
13456 symtab_hdr->contents = (unsigned char *) isymbuf;
13457
13458 /* Delete bytes depending on the delcnt and deloff. */
13459 if (!mips_elf_relax_delete_bytes (abfd, sec,
13460 irel->r_offset + deloff, delcnt))
13461 goto error_return;
13462
13463 /* That will change things, so we should relax again.
13464 Note that this is not required, and it may be slow. */
13465 *again = TRUE;
13466 }
13467 }
13468
13469 if (isymbuf != NULL
13470 && symtab_hdr->contents != (unsigned char *) isymbuf)
13471 {
13472 if (! link_info->keep_memory)
13473 free (isymbuf);
13474 else
13475 {
13476 /* Cache the symbols for elf_link_input_bfd. */
13477 symtab_hdr->contents = (unsigned char *) isymbuf;
13478 }
13479 }
13480
13481 if (contents != NULL
13482 && elf_section_data (sec)->this_hdr.contents != contents)
13483 {
13484 if (! link_info->keep_memory)
13485 free (contents);
13486 else
13487 {
13488 /* Cache the section contents for elf_link_input_bfd. */
13489 elf_section_data (sec)->this_hdr.contents = contents;
13490 }
13491 }
13492
13493 if (internal_relocs != NULL
13494 && elf_section_data (sec)->relocs != internal_relocs)
13495 free (internal_relocs);
13496
13497 return TRUE;
13498
13499 error_return:
13500 if (isymbuf != NULL
13501 && symtab_hdr->contents != (unsigned char *) isymbuf)
13502 free (isymbuf);
13503 if (contents != NULL
13504 && elf_section_data (sec)->this_hdr.contents != contents)
13505 free (contents);
13506 if (internal_relocs != NULL
13507 && elf_section_data (sec)->relocs != internal_relocs)
13508 free (internal_relocs);
13509
13510 return FALSE;
13511}
13512\f
b49e97c9
TS
13513/* Create a MIPS ELF linker hash table. */
13514
13515struct bfd_link_hash_table *
9719ad41 13516_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
13517{
13518 struct mips_elf_link_hash_table *ret;
13519 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
13520
7bf52ea2 13521 ret = bfd_zmalloc (amt);
9719ad41 13522 if (ret == NULL)
b49e97c9
TS
13523 return NULL;
13524
66eb6687
AM
13525 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
13526 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
13527 sizeof (struct mips_elf_link_hash_entry),
13528 MIPS_ELF_DATA))
b49e97c9 13529 {
e2d34d7d 13530 free (ret);
b49e97c9
TS
13531 return NULL;
13532 }
1bbce132
MR
13533 ret->root.init_plt_refcount.plist = NULL;
13534 ret->root.init_plt_offset.plist = NULL;
b49e97c9 13535
b49e97c9
TS
13536 return &ret->root.root;
13537}
0a44bf69
RS
13538
13539/* Likewise, but indicate that the target is VxWorks. */
13540
13541struct bfd_link_hash_table *
13542_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
13543{
13544 struct bfd_link_hash_table *ret;
13545
13546 ret = _bfd_mips_elf_link_hash_table_create (abfd);
13547 if (ret)
13548 {
13549 struct mips_elf_link_hash_table *htab;
13550
13551 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
13552 htab->use_plts_and_copy_relocs = TRUE;
13553 htab->is_vxworks = TRUE;
0a44bf69
RS
13554 }
13555 return ret;
13556}
861fb55a
DJ
13557
13558/* A function that the linker calls if we are allowed to use PLTs
13559 and copy relocs. */
13560
13561void
13562_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
13563{
13564 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
13565}
833794fc
MR
13566
13567/* A function that the linker calls to select between all or only
13568 32-bit microMIPS instructions. */
13569
13570void
13571_bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
13572{
13573 mips_elf_hash_table (info)->insn32 = on;
13574}
b49e97c9
TS
13575\f
13576/* We need to use a special link routine to handle the .reginfo and
13577 the .mdebug sections. We need to merge all instances of these
13578 sections together, not write them all out sequentially. */
13579
b34976b6 13580bfd_boolean
9719ad41 13581_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 13582{
b49e97c9
TS
13583 asection *o;
13584 struct bfd_link_order *p;
13585 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
13586 asection *rtproc_sec;
13587 Elf32_RegInfo reginfo;
13588 struct ecoff_debug_info debug;
861fb55a 13589 struct mips_htab_traverse_info hti;
7a2a6943
NC
13590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13591 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 13592 HDRR *symhdr = &debug.symbolic_header;
9719ad41 13593 void *mdebug_handle = NULL;
b49e97c9
TS
13594 asection *s;
13595 EXTR esym;
13596 unsigned int i;
13597 bfd_size_type amt;
0a44bf69 13598 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
13599
13600 static const char * const secname[] =
13601 {
13602 ".text", ".init", ".fini", ".data",
13603 ".rodata", ".sdata", ".sbss", ".bss"
13604 };
13605 static const int sc[] =
13606 {
13607 scText, scInit, scFini, scData,
13608 scRData, scSData, scSBss, scBss
13609 };
13610
d4596a51
RS
13611 /* Sort the dynamic symbols so that those with GOT entries come after
13612 those without. */
0a44bf69 13613 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
13614 BFD_ASSERT (htab != NULL);
13615
d4596a51
RS
13616 if (!mips_elf_sort_hash_table (abfd, info))
13617 return FALSE;
b49e97c9 13618
861fb55a
DJ
13619 /* Create any scheduled LA25 stubs. */
13620 hti.info = info;
13621 hti.output_bfd = abfd;
13622 hti.error = FALSE;
13623 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
13624 if (hti.error)
13625 return FALSE;
13626
b49e97c9
TS
13627 /* Get a value for the GP register. */
13628 if (elf_gp (abfd) == 0)
13629 {
13630 struct bfd_link_hash_entry *h;
13631
b34976b6 13632 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 13633 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
13634 elf_gp (abfd) = (h->u.def.value
13635 + h->u.def.section->output_section->vma
13636 + h->u.def.section->output_offset);
0a44bf69
RS
13637 else if (htab->is_vxworks
13638 && (h = bfd_link_hash_lookup (info->hash,
13639 "_GLOBAL_OFFSET_TABLE_",
13640 FALSE, FALSE, TRUE))
13641 && h->type == bfd_link_hash_defined)
13642 elf_gp (abfd) = (h->u.def.section->output_section->vma
13643 + h->u.def.section->output_offset
13644 + h->u.def.value);
1049f94e 13645 else if (info->relocatable)
b49e97c9
TS
13646 {
13647 bfd_vma lo = MINUS_ONE;
13648
13649 /* Find the GP-relative section with the lowest offset. */
9719ad41 13650 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
13651 if (o->vma < lo
13652 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
13653 lo = o->vma;
13654
13655 /* And calculate GP relative to that. */
0a44bf69 13656 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
13657 }
13658 else
13659 {
13660 /* If the relocate_section function needs to do a reloc
13661 involving the GP value, it should make a reloc_dangerous
13662 callback to warn that GP is not defined. */
13663 }
13664 }
13665
13666 /* Go through the sections and collect the .reginfo and .mdebug
13667 information. */
13668 reginfo_sec = NULL;
13669 mdebug_sec = NULL;
13670 gptab_data_sec = NULL;
13671 gptab_bss_sec = NULL;
9719ad41 13672 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
13673 {
13674 if (strcmp (o->name, ".reginfo") == 0)
13675 {
13676 memset (&reginfo, 0, sizeof reginfo);
13677
13678 /* We have found the .reginfo section in the output file.
13679 Look through all the link_orders comprising it and merge
13680 the information together. */
8423293d 13681 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13682 {
13683 asection *input_section;
13684 bfd *input_bfd;
13685 Elf32_External_RegInfo ext;
13686 Elf32_RegInfo sub;
13687
13688 if (p->type != bfd_indirect_link_order)
13689 {
13690 if (p->type == bfd_data_link_order)
13691 continue;
13692 abort ();
13693 }
13694
13695 input_section = p->u.indirect.section;
13696 input_bfd = input_section->owner;
13697
b49e97c9 13698 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 13699 &ext, 0, sizeof ext))
b34976b6 13700 return FALSE;
b49e97c9
TS
13701
13702 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
13703
13704 reginfo.ri_gprmask |= sub.ri_gprmask;
13705 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
13706 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
13707 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
13708 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
13709
13710 /* ri_gp_value is set by the function
13711 mips_elf32_section_processing when the section is
13712 finally written out. */
13713
13714 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13715 elf_link_input_bfd ignores this section. */
13716 input_section->flags &= ~SEC_HAS_CONTENTS;
13717 }
13718
13719 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 13720 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
13721
13722 /* Skip this section later on (I don't think this currently
13723 matters, but someday it might). */
8423293d 13724 o->map_head.link_order = NULL;
b49e97c9
TS
13725
13726 reginfo_sec = o;
13727 }
13728
13729 if (strcmp (o->name, ".mdebug") == 0)
13730 {
13731 struct extsym_info einfo;
13732 bfd_vma last;
13733
13734 /* We have found the .mdebug section in the output file.
13735 Look through all the link_orders comprising it and merge
13736 the information together. */
13737 symhdr->magic = swap->sym_magic;
13738 /* FIXME: What should the version stamp be? */
13739 symhdr->vstamp = 0;
13740 symhdr->ilineMax = 0;
13741 symhdr->cbLine = 0;
13742 symhdr->idnMax = 0;
13743 symhdr->ipdMax = 0;
13744 symhdr->isymMax = 0;
13745 symhdr->ioptMax = 0;
13746 symhdr->iauxMax = 0;
13747 symhdr->issMax = 0;
13748 symhdr->issExtMax = 0;
13749 symhdr->ifdMax = 0;
13750 symhdr->crfd = 0;
13751 symhdr->iextMax = 0;
13752
13753 /* We accumulate the debugging information itself in the
13754 debug_info structure. */
13755 debug.line = NULL;
13756 debug.external_dnr = NULL;
13757 debug.external_pdr = NULL;
13758 debug.external_sym = NULL;
13759 debug.external_opt = NULL;
13760 debug.external_aux = NULL;
13761 debug.ss = NULL;
13762 debug.ssext = debug.ssext_end = NULL;
13763 debug.external_fdr = NULL;
13764 debug.external_rfd = NULL;
13765 debug.external_ext = debug.external_ext_end = NULL;
13766
13767 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 13768 if (mdebug_handle == NULL)
b34976b6 13769 return FALSE;
b49e97c9
TS
13770
13771 esym.jmptbl = 0;
13772 esym.cobol_main = 0;
13773 esym.weakext = 0;
13774 esym.reserved = 0;
13775 esym.ifd = ifdNil;
13776 esym.asym.iss = issNil;
13777 esym.asym.st = stLocal;
13778 esym.asym.reserved = 0;
13779 esym.asym.index = indexNil;
13780 last = 0;
13781 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13782 {
13783 esym.asym.sc = sc[i];
13784 s = bfd_get_section_by_name (abfd, secname[i]);
13785 if (s != NULL)
13786 {
13787 esym.asym.value = s->vma;
eea6121a 13788 last = s->vma + s->size;
b49e97c9
TS
13789 }
13790 else
13791 esym.asym.value = last;
13792 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13793 secname[i], &esym))
b34976b6 13794 return FALSE;
b49e97c9
TS
13795 }
13796
8423293d 13797 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13798 {
13799 asection *input_section;
13800 bfd *input_bfd;
13801 const struct ecoff_debug_swap *input_swap;
13802 struct ecoff_debug_info input_debug;
13803 char *eraw_src;
13804 char *eraw_end;
13805
13806 if (p->type != bfd_indirect_link_order)
13807 {
13808 if (p->type == bfd_data_link_order)
13809 continue;
13810 abort ();
13811 }
13812
13813 input_section = p->u.indirect.section;
13814 input_bfd = input_section->owner;
13815
d5eaccd7 13816 if (!is_mips_elf (input_bfd))
b49e97c9
TS
13817 {
13818 /* I don't know what a non MIPS ELF bfd would be
13819 doing with a .mdebug section, but I don't really
13820 want to deal with it. */
13821 continue;
13822 }
13823
13824 input_swap = (get_elf_backend_data (input_bfd)
13825 ->elf_backend_ecoff_debug_swap);
13826
eea6121a 13827 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
13828
13829 /* The ECOFF linking code expects that we have already
13830 read in the debugging information and set up an
13831 ecoff_debug_info structure, so we do that now. */
13832 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13833 &input_debug))
b34976b6 13834 return FALSE;
b49e97c9
TS
13835
13836 if (! (bfd_ecoff_debug_accumulate
13837 (mdebug_handle, abfd, &debug, swap, input_bfd,
13838 &input_debug, input_swap, info)))
b34976b6 13839 return FALSE;
b49e97c9
TS
13840
13841 /* Loop through the external symbols. For each one with
13842 interesting information, try to find the symbol in
13843 the linker global hash table and save the information
13844 for the output external symbols. */
13845 eraw_src = input_debug.external_ext;
13846 eraw_end = (eraw_src
13847 + (input_debug.symbolic_header.iextMax
13848 * input_swap->external_ext_size));
13849 for (;
13850 eraw_src < eraw_end;
13851 eraw_src += input_swap->external_ext_size)
13852 {
13853 EXTR ext;
13854 const char *name;
13855 struct mips_elf_link_hash_entry *h;
13856
9719ad41 13857 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
13858 if (ext.asym.sc == scNil
13859 || ext.asym.sc == scUndefined
13860 || ext.asym.sc == scSUndefined)
13861 continue;
13862
13863 name = input_debug.ssext + ext.asym.iss;
13864 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 13865 name, FALSE, FALSE, TRUE);
b49e97c9
TS
13866 if (h == NULL || h->esym.ifd != -2)
13867 continue;
13868
13869 if (ext.ifd != -1)
13870 {
13871 BFD_ASSERT (ext.ifd
13872 < input_debug.symbolic_header.ifdMax);
13873 ext.ifd = input_debug.ifdmap[ext.ifd];
13874 }
13875
13876 h->esym = ext;
13877 }
13878
13879 /* Free up the information we just read. */
13880 free (input_debug.line);
13881 free (input_debug.external_dnr);
13882 free (input_debug.external_pdr);
13883 free (input_debug.external_sym);
13884 free (input_debug.external_opt);
13885 free (input_debug.external_aux);
13886 free (input_debug.ss);
13887 free (input_debug.ssext);
13888 free (input_debug.external_fdr);
13889 free (input_debug.external_rfd);
13890 free (input_debug.external_ext);
13891
13892 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13893 elf_link_input_bfd ignores this section. */
13894 input_section->flags &= ~SEC_HAS_CONTENTS;
13895 }
13896
13897 if (SGI_COMPAT (abfd) && info->shared)
13898 {
13899 /* Create .rtproc section. */
87e0a731 13900 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
b49e97c9
TS
13901 if (rtproc_sec == NULL)
13902 {
13903 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13904 | SEC_LINKER_CREATED | SEC_READONLY);
13905
87e0a731
AM
13906 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
13907 ".rtproc",
13908 flags);
b49e97c9 13909 if (rtproc_sec == NULL
b49e97c9 13910 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 13911 return FALSE;
b49e97c9
TS
13912 }
13913
13914 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13915 info, rtproc_sec,
13916 &debug))
b34976b6 13917 return FALSE;
b49e97c9
TS
13918 }
13919
13920 /* Build the external symbol information. */
13921 einfo.abfd = abfd;
13922 einfo.info = info;
13923 einfo.debug = &debug;
13924 einfo.swap = swap;
b34976b6 13925 einfo.failed = FALSE;
b49e97c9 13926 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 13927 mips_elf_output_extsym, &einfo);
b49e97c9 13928 if (einfo.failed)
b34976b6 13929 return FALSE;
b49e97c9
TS
13930
13931 /* Set the size of the .mdebug section. */
eea6121a 13932 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
13933
13934 /* Skip this section later on (I don't think this currently
13935 matters, but someday it might). */
8423293d 13936 o->map_head.link_order = NULL;
b49e97c9
TS
13937
13938 mdebug_sec = o;
13939 }
13940
0112cd26 13941 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
13942 {
13943 const char *subname;
13944 unsigned int c;
13945 Elf32_gptab *tab;
13946 Elf32_External_gptab *ext_tab;
13947 unsigned int j;
13948
13949 /* The .gptab.sdata and .gptab.sbss sections hold
13950 information describing how the small data area would
13951 change depending upon the -G switch. These sections
13952 not used in executables files. */
1049f94e 13953 if (! info->relocatable)
b49e97c9 13954 {
8423293d 13955 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13956 {
13957 asection *input_section;
13958
13959 if (p->type != bfd_indirect_link_order)
13960 {
13961 if (p->type == bfd_data_link_order)
13962 continue;
13963 abort ();
13964 }
13965
13966 input_section = p->u.indirect.section;
13967
13968 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13969 elf_link_input_bfd ignores this section. */
13970 input_section->flags &= ~SEC_HAS_CONTENTS;
13971 }
13972
13973 /* Skip this section later on (I don't think this
13974 currently matters, but someday it might). */
8423293d 13975 o->map_head.link_order = NULL;
b49e97c9
TS
13976
13977 /* Really remove the section. */
5daa8fe7 13978 bfd_section_list_remove (abfd, o);
b49e97c9
TS
13979 --abfd->section_count;
13980
13981 continue;
13982 }
13983
13984 /* There is one gptab for initialized data, and one for
13985 uninitialized data. */
13986 if (strcmp (o->name, ".gptab.sdata") == 0)
13987 gptab_data_sec = o;
13988 else if (strcmp (o->name, ".gptab.sbss") == 0)
13989 gptab_bss_sec = o;
13990 else
13991 {
13992 (*_bfd_error_handler)
13993 (_("%s: illegal section name `%s'"),
13994 bfd_get_filename (abfd), o->name);
13995 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 13996 return FALSE;
b49e97c9
TS
13997 }
13998
13999 /* The linker script always combines .gptab.data and
14000 .gptab.sdata into .gptab.sdata, and likewise for
14001 .gptab.bss and .gptab.sbss. It is possible that there is
14002 no .sdata or .sbss section in the output file, in which
14003 case we must change the name of the output section. */
14004 subname = o->name + sizeof ".gptab" - 1;
14005 if (bfd_get_section_by_name (abfd, subname) == NULL)
14006 {
14007 if (o == gptab_data_sec)
14008 o->name = ".gptab.data";
14009 else
14010 o->name = ".gptab.bss";
14011 subname = o->name + sizeof ".gptab" - 1;
14012 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14013 }
14014
14015 /* Set up the first entry. */
14016 c = 1;
14017 amt = c * sizeof (Elf32_gptab);
9719ad41 14018 tab = bfd_malloc (amt);
b49e97c9 14019 if (tab == NULL)
b34976b6 14020 return FALSE;
b49e97c9
TS
14021 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14022 tab[0].gt_header.gt_unused = 0;
14023
14024 /* Combine the input sections. */
8423293d 14025 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14026 {
14027 asection *input_section;
14028 bfd *input_bfd;
14029 bfd_size_type size;
14030 unsigned long last;
14031 bfd_size_type gpentry;
14032
14033 if (p->type != bfd_indirect_link_order)
14034 {
14035 if (p->type == bfd_data_link_order)
14036 continue;
14037 abort ();
14038 }
14039
14040 input_section = p->u.indirect.section;
14041 input_bfd = input_section->owner;
14042
14043 /* Combine the gptab entries for this input section one
14044 by one. We know that the input gptab entries are
14045 sorted by ascending -G value. */
eea6121a 14046 size = input_section->size;
b49e97c9
TS
14047 last = 0;
14048 for (gpentry = sizeof (Elf32_External_gptab);
14049 gpentry < size;
14050 gpentry += sizeof (Elf32_External_gptab))
14051 {
14052 Elf32_External_gptab ext_gptab;
14053 Elf32_gptab int_gptab;
14054 unsigned long val;
14055 unsigned long add;
b34976b6 14056 bfd_boolean exact;
b49e97c9
TS
14057 unsigned int look;
14058
14059 if (! (bfd_get_section_contents
9719ad41
RS
14060 (input_bfd, input_section, &ext_gptab, gpentry,
14061 sizeof (Elf32_External_gptab))))
b49e97c9
TS
14062 {
14063 free (tab);
b34976b6 14064 return FALSE;
b49e97c9
TS
14065 }
14066
14067 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
14068 &int_gptab);
14069 val = int_gptab.gt_entry.gt_g_value;
14070 add = int_gptab.gt_entry.gt_bytes - last;
14071
b34976b6 14072 exact = FALSE;
b49e97c9
TS
14073 for (look = 1; look < c; look++)
14074 {
14075 if (tab[look].gt_entry.gt_g_value >= val)
14076 tab[look].gt_entry.gt_bytes += add;
14077
14078 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 14079 exact = TRUE;
b49e97c9
TS
14080 }
14081
14082 if (! exact)
14083 {
14084 Elf32_gptab *new_tab;
14085 unsigned int max;
14086
14087 /* We need a new table entry. */
14088 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 14089 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
14090 if (new_tab == NULL)
14091 {
14092 free (tab);
b34976b6 14093 return FALSE;
b49e97c9
TS
14094 }
14095 tab = new_tab;
14096 tab[c].gt_entry.gt_g_value = val;
14097 tab[c].gt_entry.gt_bytes = add;
14098
14099 /* Merge in the size for the next smallest -G
14100 value, since that will be implied by this new
14101 value. */
14102 max = 0;
14103 for (look = 1; look < c; look++)
14104 {
14105 if (tab[look].gt_entry.gt_g_value < val
14106 && (max == 0
14107 || (tab[look].gt_entry.gt_g_value
14108 > tab[max].gt_entry.gt_g_value)))
14109 max = look;
14110 }
14111 if (max != 0)
14112 tab[c].gt_entry.gt_bytes +=
14113 tab[max].gt_entry.gt_bytes;
14114
14115 ++c;
14116 }
14117
14118 last = int_gptab.gt_entry.gt_bytes;
14119 }
14120
14121 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14122 elf_link_input_bfd ignores this section. */
14123 input_section->flags &= ~SEC_HAS_CONTENTS;
14124 }
14125
14126 /* The table must be sorted by -G value. */
14127 if (c > 2)
14128 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
14129
14130 /* Swap out the table. */
14131 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 14132 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
14133 if (ext_tab == NULL)
14134 {
14135 free (tab);
b34976b6 14136 return FALSE;
b49e97c9
TS
14137 }
14138
14139 for (j = 0; j < c; j++)
14140 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
14141 free (tab);
14142
eea6121a 14143 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
14144 o->contents = (bfd_byte *) ext_tab;
14145
14146 /* Skip this section later on (I don't think this currently
14147 matters, but someday it might). */
8423293d 14148 o->map_head.link_order = NULL;
b49e97c9
TS
14149 }
14150 }
14151
14152 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 14153 if (!bfd_elf_final_link (abfd, info))
b34976b6 14154 return FALSE;
b49e97c9
TS
14155
14156 /* Now write out the computed sections. */
14157
9719ad41 14158 if (reginfo_sec != NULL)
b49e97c9
TS
14159 {
14160 Elf32_External_RegInfo ext;
14161
14162 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 14163 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 14164 return FALSE;
b49e97c9
TS
14165 }
14166
9719ad41 14167 if (mdebug_sec != NULL)
b49e97c9
TS
14168 {
14169 BFD_ASSERT (abfd->output_has_begun);
14170 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
14171 swap, info,
14172 mdebug_sec->filepos))
b34976b6 14173 return FALSE;
b49e97c9
TS
14174
14175 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
14176 }
14177
9719ad41 14178 if (gptab_data_sec != NULL)
b49e97c9
TS
14179 {
14180 if (! bfd_set_section_contents (abfd, gptab_data_sec,
14181 gptab_data_sec->contents,
eea6121a 14182 0, gptab_data_sec->size))
b34976b6 14183 return FALSE;
b49e97c9
TS
14184 }
14185
9719ad41 14186 if (gptab_bss_sec != NULL)
b49e97c9
TS
14187 {
14188 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
14189 gptab_bss_sec->contents,
eea6121a 14190 0, gptab_bss_sec->size))
b34976b6 14191 return FALSE;
b49e97c9
TS
14192 }
14193
14194 if (SGI_COMPAT (abfd))
14195 {
14196 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
14197 if (rtproc_sec != NULL)
14198 {
14199 if (! bfd_set_section_contents (abfd, rtproc_sec,
14200 rtproc_sec->contents,
eea6121a 14201 0, rtproc_sec->size))
b34976b6 14202 return FALSE;
b49e97c9
TS
14203 }
14204 }
14205
b34976b6 14206 return TRUE;
b49e97c9
TS
14207}
14208\f
64543e1a
RS
14209/* Structure for saying that BFD machine EXTENSION extends BASE. */
14210
a253d456
NC
14211struct mips_mach_extension
14212{
64543e1a
RS
14213 unsigned long extension, base;
14214};
14215
14216
14217/* An array describing how BFD machines relate to one another. The entries
14218 are ordered topologically with MIPS I extensions listed last. */
14219
a253d456
NC
14220static const struct mips_mach_extension mips_mach_extensions[] =
14221{
6f179bd0 14222 /* MIPS64r2 extensions. */
432233b3 14223 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
dd6a37e7 14224 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
6f179bd0 14225 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
4ba154f5 14226 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64r2 },
6f179bd0 14227
64543e1a 14228 /* MIPS64 extensions. */
5f74bc13 14229 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 14230 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 14231 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
64543e1a
RS
14232
14233 /* MIPS V extensions. */
14234 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14235
14236 /* R10000 extensions. */
14237 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
14238 { bfd_mach_mips14000, bfd_mach_mips10000 },
14239 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
14240
14241 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14242 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14243 better to allow vr5400 and vr5500 code to be merged anyway, since
14244 many libraries will just use the core ISA. Perhaps we could add
14245 some sort of ASE flag if this ever proves a problem. */
14246 { bfd_mach_mips5500, bfd_mach_mips5400 },
14247 { bfd_mach_mips5400, bfd_mach_mips5000 },
14248
14249 /* MIPS IV extensions. */
14250 { bfd_mach_mips5, bfd_mach_mips8000 },
14251 { bfd_mach_mips10000, bfd_mach_mips8000 },
14252 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 14253 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 14254 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
14255
14256 /* VR4100 extensions. */
14257 { bfd_mach_mips4120, bfd_mach_mips4100 },
14258 { bfd_mach_mips4111, bfd_mach_mips4100 },
14259
14260 /* MIPS III extensions. */
350cc38d
MS
14261 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14262 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
14263 { bfd_mach_mips8000, bfd_mach_mips4000 },
14264 { bfd_mach_mips4650, bfd_mach_mips4000 },
14265 { bfd_mach_mips4600, bfd_mach_mips4000 },
14266 { bfd_mach_mips4400, bfd_mach_mips4000 },
14267 { bfd_mach_mips4300, bfd_mach_mips4000 },
14268 { bfd_mach_mips4100, bfd_mach_mips4000 },
14269 { bfd_mach_mips4010, bfd_mach_mips4000 },
e407c74b 14270 { bfd_mach_mips5900, bfd_mach_mips4000 },
64543e1a
RS
14271
14272 /* MIPS32 extensions. */
14273 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14274
14275 /* MIPS II extensions. */
14276 { bfd_mach_mips4000, bfd_mach_mips6000 },
14277 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14278
14279 /* MIPS I extensions. */
14280 { bfd_mach_mips6000, bfd_mach_mips3000 },
14281 { bfd_mach_mips3900, bfd_mach_mips3000 }
14282};
14283
14284
14285/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14286
14287static bfd_boolean
9719ad41 14288mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
14289{
14290 size_t i;
14291
c5211a54
RS
14292 if (extension == base)
14293 return TRUE;
14294
14295 if (base == bfd_mach_mipsisa32
14296 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14297 return TRUE;
14298
14299 if (base == bfd_mach_mipsisa32r2
14300 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14301 return TRUE;
14302
14303 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 14304 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
14305 {
14306 extension = mips_mach_extensions[i].base;
14307 if (extension == base)
14308 return TRUE;
14309 }
64543e1a 14310
c5211a54 14311 return FALSE;
64543e1a
RS
14312}
14313
14314
14315/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 14316
b34976b6 14317static bfd_boolean
9719ad41 14318mips_32bit_flags_p (flagword flags)
00707a0e 14319{
64543e1a
RS
14320 return ((flags & EF_MIPS_32BITMODE) != 0
14321 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14322 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14323 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14324 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14325 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14326 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
14327}
14328
64543e1a 14329
2cf19d5c
JM
14330/* Merge object attributes from IBFD into OBFD. Raise an error if
14331 there are conflicting attributes. */
14332static bfd_boolean
14333mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
14334{
14335 obj_attribute *in_attr;
14336 obj_attribute *out_attr;
6ae68ba3 14337 bfd *abi_fp_bfd;
b60bf9be 14338 bfd *abi_msa_bfd;
6ae68ba3
MR
14339
14340 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
14341 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
d929bc19 14342 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
6ae68ba3 14343 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
2cf19d5c 14344
b60bf9be
CF
14345 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
14346 if (!abi_msa_bfd
14347 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14348 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
14349
2cf19d5c
JM
14350 if (!elf_known_obj_attributes_proc (obfd)[0].i)
14351 {
14352 /* This is the first object. Copy the attributes. */
14353 _bfd_elf_copy_obj_attributes (ibfd, obfd);
14354
14355 /* Use the Tag_null value to indicate the attributes have been
14356 initialized. */
14357 elf_known_obj_attributes_proc (obfd)[0].i = 1;
14358
14359 return TRUE;
14360 }
14361
14362 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
14363 non-conflicting ones. */
2cf19d5c
JM
14364 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14365 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
14366 {
757a636f 14367 int out_fp, in_fp;
6ae68ba3 14368
757a636f
RS
14369 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
14370 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14371 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
14372 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
14373 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
14374 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
14375 {
14376 const char *out_string, *in_string;
6ae68ba3 14377
757a636f
RS
14378 out_string = _bfd_mips_fp_abi_string (out_fp);
14379 in_string = _bfd_mips_fp_abi_string (in_fp);
14380 /* First warn about cases involving unrecognised ABIs. */
14381 if (!out_string && !in_string)
14382 _bfd_error_handler
14383 (_("Warning: %B uses unknown floating point ABI %d "
14384 "(set by %B), %B uses unknown floating point ABI %d"),
14385 obfd, abi_fp_bfd, ibfd, out_fp, in_fp);
14386 else if (!out_string)
14387 _bfd_error_handler
14388 (_("Warning: %B uses unknown floating point ABI %d "
14389 "(set by %B), %B uses %s"),
14390 obfd, abi_fp_bfd, ibfd, out_fp, in_string);
14391 else if (!in_string)
14392 _bfd_error_handler
14393 (_("Warning: %B uses %s (set by %B), "
14394 "%B uses unknown floating point ABI %d"),
14395 obfd, abi_fp_bfd, ibfd, out_string, in_fp);
14396 else
14397 {
14398 /* If one of the bfds is soft-float, the other must be
14399 hard-float. The exact choice of hard-float ABI isn't
14400 really relevant to the error message. */
14401 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
14402 out_string = "-mhard-float";
14403 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
14404 in_string = "-mhard-float";
14405 _bfd_error_handler
14406 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14407 obfd, abi_fp_bfd, ibfd, out_string, in_string);
14408 }
14409 }
2cf19d5c
JM
14410 }
14411
b60bf9be
CF
14412 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
14413 non-conflicting ones. */
14414 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14415 {
14416 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
14417 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
14418 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
14419 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14420 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14421 {
14422 case Val_GNU_MIPS_ABI_MSA_128:
14423 _bfd_error_handler
14424 (_("Warning: %B uses %s (set by %B), "
14425 "%B uses unknown MSA ABI %d"),
14426 obfd, abi_msa_bfd, ibfd,
14427 "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14428 break;
14429
14430 default:
14431 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
14432 {
14433 case Val_GNU_MIPS_ABI_MSA_128:
14434 _bfd_error_handler
14435 (_("Warning: %B uses unknown MSA ABI %d "
14436 "(set by %B), %B uses %s"),
14437 obfd, abi_msa_bfd, ibfd,
14438 out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
14439 break;
14440
14441 default:
14442 _bfd_error_handler
14443 (_("Warning: %B uses unknown MSA ABI %d "
14444 "(set by %B), %B uses unknown MSA ABI %d"),
14445 obfd, abi_msa_bfd, ibfd,
14446 out_attr[Tag_GNU_MIPS_ABI_MSA].i,
14447 in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14448 break;
14449 }
14450 }
14451 }
14452
2cf19d5c
JM
14453 /* Merge Tag_compatibility attributes and any common GNU ones. */
14454 _bfd_elf_merge_object_attributes (ibfd, obfd);
14455
14456 return TRUE;
14457}
14458
b49e97c9
TS
14459/* Merge backend specific data from an object file to the output
14460 object file when linking. */
14461
b34976b6 14462bfd_boolean
9719ad41 14463_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
14464{
14465 flagword old_flags;
14466 flagword new_flags;
b34976b6
AM
14467 bfd_boolean ok;
14468 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
14469 asection *sec;
14470
58238693 14471 /* Check if we have the same endianness. */
82e51918 14472 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
14473 {
14474 (*_bfd_error_handler)
d003868e
AM
14475 (_("%B: endianness incompatible with that of the selected emulation"),
14476 ibfd);
aa701218
AO
14477 return FALSE;
14478 }
b49e97c9 14479
d5eaccd7 14480 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 14481 return TRUE;
b49e97c9 14482
aa701218
AO
14483 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
14484 {
14485 (*_bfd_error_handler)
d003868e
AM
14486 (_("%B: ABI is incompatible with that of the selected emulation"),
14487 ibfd);
aa701218
AO
14488 return FALSE;
14489 }
14490
2cf19d5c
JM
14491 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
14492 return FALSE;
14493
b49e97c9
TS
14494 new_flags = elf_elfheader (ibfd)->e_flags;
14495 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
14496 old_flags = elf_elfheader (obfd)->e_flags;
14497
14498 if (! elf_flags_init (obfd))
14499 {
b34976b6 14500 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
14501 elf_elfheader (obfd)->e_flags = new_flags;
14502 elf_elfheader (obfd)->e_ident[EI_CLASS]
14503 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
14504
14505 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861 14506 && (bfd_get_arch_info (obfd)->the_default
68ffbac6 14507 || mips_mach_extends_p (bfd_get_mach (obfd),
2907b861 14508 bfd_get_mach (ibfd))))
b49e97c9
TS
14509 {
14510 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
14511 bfd_get_mach (ibfd)))
b34976b6 14512 return FALSE;
b49e97c9
TS
14513 }
14514
b34976b6 14515 return TRUE;
b49e97c9
TS
14516 }
14517
14518 /* Check flag compatibility. */
14519
14520 new_flags &= ~EF_MIPS_NOREORDER;
14521 old_flags &= ~EF_MIPS_NOREORDER;
14522
f4416af6
AO
14523 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
14524 doesn't seem to matter. */
14525 new_flags &= ~EF_MIPS_XGOT;
14526 old_flags &= ~EF_MIPS_XGOT;
14527
98a8deaf
RS
14528 /* MIPSpro generates ucode info in n64 objects. Again, we should
14529 just be able to ignore this. */
14530 new_flags &= ~EF_MIPS_UCODE;
14531 old_flags &= ~EF_MIPS_UCODE;
14532
861fb55a
DJ
14533 /* DSOs should only be linked with CPIC code. */
14534 if ((ibfd->flags & DYNAMIC) != 0)
14535 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 14536
b49e97c9 14537 if (new_flags == old_flags)
b34976b6 14538 return TRUE;
b49e97c9
TS
14539
14540 /* Check to see if the input BFD actually contains any sections.
14541 If not, its flags may not have been initialised either, but it cannot
14542 actually cause any incompatibility. */
14543 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
14544 {
14545 /* Ignore synthetic sections and empty .text, .data and .bss sections
ed88c97e
RS
14546 which are automatically generated by gas. Also ignore fake
14547 (s)common sections, since merely defining a common symbol does
14548 not affect compatibility. */
14549 if ((sec->flags & SEC_IS_COMMON) == 0
14550 && strcmp (sec->name, ".reginfo")
b49e97c9 14551 && strcmp (sec->name, ".mdebug")
eea6121a 14552 && (sec->size != 0
d13d89fa
NS
14553 || (strcmp (sec->name, ".text")
14554 && strcmp (sec->name, ".data")
14555 && strcmp (sec->name, ".bss"))))
b49e97c9 14556 {
b34976b6 14557 null_input_bfd = FALSE;
b49e97c9
TS
14558 break;
14559 }
14560 }
14561 if (null_input_bfd)
b34976b6 14562 return TRUE;
b49e97c9 14563
b34976b6 14564 ok = TRUE;
b49e97c9 14565
143d77c5
EC
14566 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
14567 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 14568 {
b49e97c9 14569 (*_bfd_error_handler)
861fb55a 14570 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 14571 ibfd);
143d77c5 14572 ok = TRUE;
b49e97c9
TS
14573 }
14574
143d77c5
EC
14575 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
14576 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
14577 if (! (new_flags & EF_MIPS_PIC))
14578 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
14579
14580 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14581 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 14582
64543e1a
RS
14583 /* Compare the ISAs. */
14584 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 14585 {
64543e1a 14586 (*_bfd_error_handler)
d003868e
AM
14587 (_("%B: linking 32-bit code with 64-bit code"),
14588 ibfd);
64543e1a
RS
14589 ok = FALSE;
14590 }
14591 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
14592 {
14593 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
14594 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 14595 {
64543e1a
RS
14596 /* Copy the architecture info from IBFD to OBFD. Also copy
14597 the 32-bit flag (if set) so that we continue to recognise
14598 OBFD as a 32-bit binary. */
14599 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
14600 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
14601 elf_elfheader (obfd)->e_flags
14602 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14603
14604 /* Copy across the ABI flags if OBFD doesn't use them
14605 and if that was what caused us to treat IBFD as 32-bit. */
14606 if ((old_flags & EF_MIPS_ABI) == 0
14607 && mips_32bit_flags_p (new_flags)
14608 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
14609 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
14610 }
14611 else
14612 {
64543e1a 14613 /* The ISAs aren't compatible. */
b49e97c9 14614 (*_bfd_error_handler)
d003868e
AM
14615 (_("%B: linking %s module with previous %s modules"),
14616 ibfd,
64543e1a
RS
14617 bfd_printable_name (ibfd),
14618 bfd_printable_name (obfd));
b34976b6 14619 ok = FALSE;
b49e97c9 14620 }
b49e97c9
TS
14621 }
14622
64543e1a
RS
14623 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14624 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14625
14626 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
14627 does set EI_CLASS differently from any 32-bit ABI. */
14628 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
14629 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14630 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14631 {
14632 /* Only error if both are set (to different values). */
14633 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
14634 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14635 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14636 {
14637 (*_bfd_error_handler)
d003868e
AM
14638 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
14639 ibfd,
b49e97c9
TS
14640 elf_mips_abi_name (ibfd),
14641 elf_mips_abi_name (obfd));
b34976b6 14642 ok = FALSE;
b49e97c9
TS
14643 }
14644 new_flags &= ~EF_MIPS_ABI;
14645 old_flags &= ~EF_MIPS_ABI;
14646 }
14647
df58fc94
RS
14648 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
14649 and allow arbitrary mixing of the remaining ASEs (retain the union). */
fb39dac1
RS
14650 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
14651 {
df58fc94
RS
14652 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14653 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14654 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
14655 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
14656 int micro_mis = old_m16 && new_micro;
14657 int m16_mis = old_micro && new_m16;
14658
14659 if (m16_mis || micro_mis)
14660 {
14661 (*_bfd_error_handler)
14662 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
14663 ibfd,
14664 m16_mis ? "MIPS16" : "microMIPS",
14665 m16_mis ? "microMIPS" : "MIPS16");
14666 ok = FALSE;
14667 }
14668
fb39dac1
RS
14669 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
14670
14671 new_flags &= ~ EF_MIPS_ARCH_ASE;
14672 old_flags &= ~ EF_MIPS_ARCH_ASE;
14673 }
14674
ba92f887
MR
14675 /* Compare NaN encodings. */
14676 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
14677 {
14678 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
14679 ibfd,
14680 (new_flags & EF_MIPS_NAN2008
14681 ? "-mnan=2008" : "-mnan=legacy"),
14682 (old_flags & EF_MIPS_NAN2008
14683 ? "-mnan=2008" : "-mnan=legacy"));
14684 ok = FALSE;
14685 new_flags &= ~EF_MIPS_NAN2008;
14686 old_flags &= ~EF_MIPS_NAN2008;
14687 }
14688
b49e97c9
TS
14689 /* Warn about any other mismatches */
14690 if (new_flags != old_flags)
14691 {
14692 (*_bfd_error_handler)
d003868e
AM
14693 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
14694 ibfd, (unsigned long) new_flags,
b49e97c9 14695 (unsigned long) old_flags);
b34976b6 14696 ok = FALSE;
b49e97c9
TS
14697 }
14698
14699 if (! ok)
14700 {
14701 bfd_set_error (bfd_error_bad_value);
b34976b6 14702 return FALSE;
b49e97c9
TS
14703 }
14704
b34976b6 14705 return TRUE;
b49e97c9
TS
14706}
14707
14708/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
14709
b34976b6 14710bfd_boolean
9719ad41 14711_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
14712{
14713 BFD_ASSERT (!elf_flags_init (abfd)
14714 || elf_elfheader (abfd)->e_flags == flags);
14715
14716 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
14717 elf_flags_init (abfd) = TRUE;
14718 return TRUE;
b49e97c9
TS
14719}
14720
ad9563d6
CM
14721char *
14722_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14723{
14724 switch (dtag)
14725 {
14726 default: return "";
14727 case DT_MIPS_RLD_VERSION:
14728 return "MIPS_RLD_VERSION";
14729 case DT_MIPS_TIME_STAMP:
14730 return "MIPS_TIME_STAMP";
14731 case DT_MIPS_ICHECKSUM:
14732 return "MIPS_ICHECKSUM";
14733 case DT_MIPS_IVERSION:
14734 return "MIPS_IVERSION";
14735 case DT_MIPS_FLAGS:
14736 return "MIPS_FLAGS";
14737 case DT_MIPS_BASE_ADDRESS:
14738 return "MIPS_BASE_ADDRESS";
14739 case DT_MIPS_MSYM:
14740 return "MIPS_MSYM";
14741 case DT_MIPS_CONFLICT:
14742 return "MIPS_CONFLICT";
14743 case DT_MIPS_LIBLIST:
14744 return "MIPS_LIBLIST";
14745 case DT_MIPS_LOCAL_GOTNO:
14746 return "MIPS_LOCAL_GOTNO";
14747 case DT_MIPS_CONFLICTNO:
14748 return "MIPS_CONFLICTNO";
14749 case DT_MIPS_LIBLISTNO:
14750 return "MIPS_LIBLISTNO";
14751 case DT_MIPS_SYMTABNO:
14752 return "MIPS_SYMTABNO";
14753 case DT_MIPS_UNREFEXTNO:
14754 return "MIPS_UNREFEXTNO";
14755 case DT_MIPS_GOTSYM:
14756 return "MIPS_GOTSYM";
14757 case DT_MIPS_HIPAGENO:
14758 return "MIPS_HIPAGENO";
14759 case DT_MIPS_RLD_MAP:
14760 return "MIPS_RLD_MAP";
14761 case DT_MIPS_DELTA_CLASS:
14762 return "MIPS_DELTA_CLASS";
14763 case DT_MIPS_DELTA_CLASS_NO:
14764 return "MIPS_DELTA_CLASS_NO";
14765 case DT_MIPS_DELTA_INSTANCE:
14766 return "MIPS_DELTA_INSTANCE";
14767 case DT_MIPS_DELTA_INSTANCE_NO:
14768 return "MIPS_DELTA_INSTANCE_NO";
14769 case DT_MIPS_DELTA_RELOC:
14770 return "MIPS_DELTA_RELOC";
14771 case DT_MIPS_DELTA_RELOC_NO:
14772 return "MIPS_DELTA_RELOC_NO";
14773 case DT_MIPS_DELTA_SYM:
14774 return "MIPS_DELTA_SYM";
14775 case DT_MIPS_DELTA_SYM_NO:
14776 return "MIPS_DELTA_SYM_NO";
14777 case DT_MIPS_DELTA_CLASSSYM:
14778 return "MIPS_DELTA_CLASSSYM";
14779 case DT_MIPS_DELTA_CLASSSYM_NO:
14780 return "MIPS_DELTA_CLASSSYM_NO";
14781 case DT_MIPS_CXX_FLAGS:
14782 return "MIPS_CXX_FLAGS";
14783 case DT_MIPS_PIXIE_INIT:
14784 return "MIPS_PIXIE_INIT";
14785 case DT_MIPS_SYMBOL_LIB:
14786 return "MIPS_SYMBOL_LIB";
14787 case DT_MIPS_LOCALPAGE_GOTIDX:
14788 return "MIPS_LOCALPAGE_GOTIDX";
14789 case DT_MIPS_LOCAL_GOTIDX:
14790 return "MIPS_LOCAL_GOTIDX";
14791 case DT_MIPS_HIDDEN_GOTIDX:
14792 return "MIPS_HIDDEN_GOTIDX";
14793 case DT_MIPS_PROTECTED_GOTIDX:
14794 return "MIPS_PROTECTED_GOT_IDX";
14795 case DT_MIPS_OPTIONS:
14796 return "MIPS_OPTIONS";
14797 case DT_MIPS_INTERFACE:
14798 return "MIPS_INTERFACE";
14799 case DT_MIPS_DYNSTR_ALIGN:
14800 return "DT_MIPS_DYNSTR_ALIGN";
14801 case DT_MIPS_INTERFACE_SIZE:
14802 return "DT_MIPS_INTERFACE_SIZE";
14803 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14804 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14805 case DT_MIPS_PERF_SUFFIX:
14806 return "DT_MIPS_PERF_SUFFIX";
14807 case DT_MIPS_COMPACT_SIZE:
14808 return "DT_MIPS_COMPACT_SIZE";
14809 case DT_MIPS_GP_VALUE:
14810 return "DT_MIPS_GP_VALUE";
14811 case DT_MIPS_AUX_DYNAMIC:
14812 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
14813 case DT_MIPS_PLTGOT:
14814 return "DT_MIPS_PLTGOT";
14815 case DT_MIPS_RWPLT:
14816 return "DT_MIPS_RWPLT";
ad9563d6
CM
14817 }
14818}
14819
757a636f
RS
14820/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
14821 not known. */
14822
14823const char *
14824_bfd_mips_fp_abi_string (int fp)
14825{
14826 switch (fp)
14827 {
14828 /* These strings aren't translated because they're simply
14829 option lists. */
14830 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14831 return "-mdouble-float";
14832
14833 case Val_GNU_MIPS_ABI_FP_SINGLE:
14834 return "-msingle-float";
14835
14836 case Val_GNU_MIPS_ABI_FP_SOFT:
14837 return "-msoft-float";
14838
14839 case Val_GNU_MIPS_ABI_FP_64:
14840 return "-mips32r2 -mfp64";
14841
14842 default:
14843 return 0;
14844 }
14845}
14846
b34976b6 14847bfd_boolean
9719ad41 14848_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 14849{
9719ad41 14850 FILE *file = ptr;
b49e97c9
TS
14851
14852 BFD_ASSERT (abfd != NULL && ptr != NULL);
14853
14854 /* Print normal ELF private data. */
14855 _bfd_elf_print_private_bfd_data (abfd, ptr);
14856
14857 /* xgettext:c-format */
14858 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14859
14860 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14861 fprintf (file, _(" [abi=O32]"));
14862 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14863 fprintf (file, _(" [abi=O64]"));
14864 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14865 fprintf (file, _(" [abi=EABI32]"));
14866 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14867 fprintf (file, _(" [abi=EABI64]"));
14868 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14869 fprintf (file, _(" [abi unknown]"));
14870 else if (ABI_N32_P (abfd))
14871 fprintf (file, _(" [abi=N32]"));
14872 else if (ABI_64_P (abfd))
14873 fprintf (file, _(" [abi=64]"));
14874 else
14875 fprintf (file, _(" [no abi set]"));
14876
14877 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 14878 fprintf (file, " [mips1]");
b49e97c9 14879 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 14880 fprintf (file, " [mips2]");
b49e97c9 14881 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 14882 fprintf (file, " [mips3]");
b49e97c9 14883 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 14884 fprintf (file, " [mips4]");
b49e97c9 14885 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 14886 fprintf (file, " [mips5]");
b49e97c9 14887 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 14888 fprintf (file, " [mips32]");
b49e97c9 14889 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 14890 fprintf (file, " [mips64]");
af7ee8bf 14891 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 14892 fprintf (file, " [mips32r2]");
5f74bc13 14893 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 14894 fprintf (file, " [mips64r2]");
b49e97c9
TS
14895 else
14896 fprintf (file, _(" [unknown ISA]"));
14897
40d32fc6 14898 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 14899 fprintf (file, " [mdmx]");
40d32fc6
CD
14900
14901 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 14902 fprintf (file, " [mips16]");
40d32fc6 14903
df58fc94
RS
14904 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14905 fprintf (file, " [micromips]");
14906
ba92f887
MR
14907 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
14908 fprintf (file, " [nan2008]");
14909
5baf5e34
SE
14910 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
14911 fprintf (file, " [fp64]");
14912
b49e97c9 14913 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 14914 fprintf (file, " [32bitmode]");
b49e97c9
TS
14915 else
14916 fprintf (file, _(" [not 32bitmode]"));
14917
c0e3f241 14918 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 14919 fprintf (file, " [noreorder]");
c0e3f241
CD
14920
14921 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 14922 fprintf (file, " [PIC]");
c0e3f241
CD
14923
14924 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 14925 fprintf (file, " [CPIC]");
c0e3f241
CD
14926
14927 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 14928 fprintf (file, " [XGOT]");
c0e3f241
CD
14929
14930 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 14931 fprintf (file, " [UCODE]");
c0e3f241 14932
b49e97c9
TS
14933 fputc ('\n', file);
14934
b34976b6 14935 return TRUE;
b49e97c9 14936}
2f89ff8d 14937
b35d266b 14938const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 14939{
0112cd26
NC
14940 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14941 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14942 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
14943 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14944 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14945 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
14946 { NULL, 0, 0, 0, 0 }
2f89ff8d 14947};
5e2b0d47 14948
8992f0d7
TS
14949/* Merge non visibility st_other attributes. Ensure that the
14950 STO_OPTIONAL flag is copied into h->other, even if this is not a
14951 definiton of the symbol. */
5e2b0d47
NC
14952void
14953_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
14954 const Elf_Internal_Sym *isym,
14955 bfd_boolean definition,
14956 bfd_boolean dynamic ATTRIBUTE_UNUSED)
14957{
8992f0d7
TS
14958 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
14959 {
14960 unsigned char other;
14961
14962 other = (definition ? isym->st_other : h->other);
14963 other &= ~ELF_ST_VISIBILITY (-1);
14964 h->other = other | ELF_ST_VISIBILITY (h->other);
14965 }
14966
14967 if (!definition
5e2b0d47
NC
14968 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
14969 h->other |= STO_OPTIONAL;
14970}
12ac1cf5
NC
14971
14972/* Decide whether an undefined symbol is special and can be ignored.
14973 This is the case for OPTIONAL symbols on IRIX. */
14974bfd_boolean
14975_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
14976{
14977 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
14978}
e0764319
NC
14979
14980bfd_boolean
14981_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
14982{
14983 return (sym->st_shndx == SHN_COMMON
14984 || sym->st_shndx == SHN_MIPS_ACOMMON
14985 || sym->st_shndx == SHN_MIPS_SCOMMON);
14986}
861fb55a
DJ
14987
14988/* Return address for Ith PLT stub in section PLT, for relocation REL
14989 or (bfd_vma) -1 if it should not be included. */
14990
14991bfd_vma
14992_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
14993 const arelent *rel ATTRIBUTE_UNUSED)
14994{
14995 return (plt->vma
14996 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
14997 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
14998}
14999
1bbce132
MR
15000/* Build a table of synthetic symbols to represent the PLT. As with MIPS16
15001 and microMIPS PLT slots we may have a many-to-one mapping between .plt
15002 and .got.plt and also the slots may be of a different size each we walk
15003 the PLT manually fetching instructions and matching them against known
15004 patterns. To make things easier standard MIPS slots, if any, always come
15005 first. As we don't create proper ELF symbols we use the UDATA.I member
15006 of ASYMBOL to carry ISA annotation. The encoding used is the same as
15007 with the ST_OTHER member of the ELF symbol. */
15008
15009long
15010_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
15011 long symcount ATTRIBUTE_UNUSED,
15012 asymbol **syms ATTRIBUTE_UNUSED,
15013 long dynsymcount, asymbol **dynsyms,
15014 asymbol **ret)
15015{
15016 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
15017 static const char microsuffix[] = "@micromipsplt";
15018 static const char m16suffix[] = "@mips16plt";
15019 static const char mipssuffix[] = "@plt";
15020
15021 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
15022 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15023 bfd_boolean micromips_p = MICROMIPS_P (abfd);
15024 Elf_Internal_Shdr *hdr;
15025 bfd_byte *plt_data;
15026 bfd_vma plt_offset;
15027 unsigned int other;
15028 bfd_vma entry_size;
15029 bfd_vma plt0_size;
15030 asection *relplt;
15031 bfd_vma opcode;
15032 asection *plt;
15033 asymbol *send;
15034 size_t size;
15035 char *names;
15036 long counti;
15037 arelent *p;
15038 asymbol *s;
15039 char *nend;
15040 long count;
15041 long pi;
15042 long i;
15043 long n;
15044
15045 *ret = NULL;
15046
15047 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
15048 return 0;
15049
15050 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
15051 if (relplt == NULL)
15052 return 0;
15053
15054 hdr = &elf_section_data (relplt)->this_hdr;
15055 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
15056 return 0;
15057
15058 plt = bfd_get_section_by_name (abfd, ".plt");
15059 if (plt == NULL)
15060 return 0;
15061
15062 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
15063 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
15064 return -1;
15065 p = relplt->relocation;
15066
15067 /* Calculating the exact amount of space required for symbols would
15068 require two passes over the PLT, so just pessimise assuming two
15069 PLT slots per relocation. */
15070 count = relplt->size / hdr->sh_entsize;
15071 counti = count * bed->s->int_rels_per_ext_rel;
15072 size = 2 * count * sizeof (asymbol);
15073 size += count * (sizeof (mipssuffix) +
15074 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
15075 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
15076 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
15077
15078 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
15079 size += sizeof (asymbol) + sizeof (pltname);
15080
15081 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
15082 return -1;
15083
15084 if (plt->size < 16)
15085 return -1;
15086
15087 s = *ret = bfd_malloc (size);
15088 if (s == NULL)
15089 return -1;
15090 send = s + 2 * count + 1;
15091
15092 names = (char *) send;
15093 nend = (char *) s + size;
15094 n = 0;
15095
15096 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
15097 if (opcode == 0x3302fffe)
15098 {
15099 if (!micromips_p)
15100 return -1;
15101 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
15102 other = STO_MICROMIPS;
15103 }
833794fc
MR
15104 else if (opcode == 0x0398c1d0)
15105 {
15106 if (!micromips_p)
15107 return -1;
15108 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
15109 other = STO_MICROMIPS;
15110 }
1bbce132
MR
15111 else
15112 {
15113 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
15114 other = 0;
15115 }
15116
15117 s->the_bfd = abfd;
15118 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
15119 s->section = plt;
15120 s->value = 0;
15121 s->name = names;
15122 s->udata.i = other;
15123 memcpy (names, pltname, sizeof (pltname));
15124 names += sizeof (pltname);
15125 ++s, ++n;
15126
15127 pi = 0;
15128 for (plt_offset = plt0_size;
15129 plt_offset + 8 <= plt->size && s < send;
15130 plt_offset += entry_size)
15131 {
15132 bfd_vma gotplt_addr;
15133 const char *suffix;
15134 bfd_vma gotplt_hi;
15135 bfd_vma gotplt_lo;
15136 size_t suffixlen;
15137
15138 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
15139
15140 /* Check if the second word matches the expected MIPS16 instruction. */
15141 if (opcode == 0x651aeb00)
15142 {
15143 if (micromips_p)
15144 return -1;
15145 /* Truncated table??? */
15146 if (plt_offset + 16 > plt->size)
15147 break;
15148 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
15149 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
15150 suffixlen = sizeof (m16suffix);
15151 suffix = m16suffix;
15152 other = STO_MIPS16;
15153 }
833794fc 15154 /* Likewise the expected microMIPS instruction (no insn32 mode). */
1bbce132
MR
15155 else if (opcode == 0xff220000)
15156 {
15157 if (!micromips_p)
15158 return -1;
15159 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
15160 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15161 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
15162 gotplt_lo <<= 2;
15163 gotplt_addr = gotplt_hi + gotplt_lo;
15164 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
15165 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
15166 suffixlen = sizeof (microsuffix);
15167 suffix = microsuffix;
15168 other = STO_MICROMIPS;
15169 }
833794fc
MR
15170 /* Likewise the expected microMIPS instruction (insn32 mode). */
15171 else if ((opcode & 0xffff0000) == 0xff2f0000)
15172 {
15173 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15174 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
15175 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15176 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15177 gotplt_addr = gotplt_hi + gotplt_lo;
15178 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
15179 suffixlen = sizeof (microsuffix);
15180 suffix = microsuffix;
15181 other = STO_MICROMIPS;
15182 }
1bbce132
MR
15183 /* Otherwise assume standard MIPS code. */
15184 else
15185 {
15186 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
15187 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
15188 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15189 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15190 gotplt_addr = gotplt_hi + gotplt_lo;
15191 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
15192 suffixlen = sizeof (mipssuffix);
15193 suffix = mipssuffix;
15194 other = 0;
15195 }
15196 /* Truncated table??? */
15197 if (plt_offset + entry_size > plt->size)
15198 break;
15199
15200 for (i = 0;
15201 i < count && p[pi].address != gotplt_addr;
15202 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
15203
15204 if (i < count)
15205 {
15206 size_t namelen;
15207 size_t len;
15208
15209 *s = **p[pi].sym_ptr_ptr;
15210 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
15211 we are defining a symbol, ensure one of them is set. */
15212 if ((s->flags & BSF_LOCAL) == 0)
15213 s->flags |= BSF_GLOBAL;
15214 s->flags |= BSF_SYNTHETIC;
15215 s->section = plt;
15216 s->value = plt_offset;
15217 s->name = names;
15218 s->udata.i = other;
15219
15220 len = strlen ((*p[pi].sym_ptr_ptr)->name);
15221 namelen = len + suffixlen;
15222 if (names + namelen > nend)
15223 break;
15224
15225 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
15226 names += len;
15227 memcpy (names, suffix, suffixlen);
15228 names += suffixlen;
15229
15230 ++s, ++n;
15231 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
15232 }
15233 }
15234
15235 free (plt_data);
15236
15237 return n;
15238}
15239
861fb55a
DJ
15240void
15241_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
15242{
15243 struct mips_elf_link_hash_table *htab;
15244 Elf_Internal_Ehdr *i_ehdrp;
15245
15246 i_ehdrp = elf_elfheader (abfd);
15247 if (link_info)
15248 {
15249 htab = mips_elf_hash_table (link_info);
4dfe6ac6
NC
15250 BFD_ASSERT (htab != NULL);
15251
861fb55a
DJ
15252 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
15253 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
15254 }
0af03126
L
15255
15256 _bfd_elf_post_process_headers (abfd, link_info);
861fb55a 15257}
This page took 1.838683 seconds and 4 git commands to generate.