* ar.c (ranlib_usage): Describe -D.
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
64543e1a 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
58238693
NC
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
b49e97c9
TS
5
6 Most of the information added by Ian Lance Taylor, Cygnus Support,
7 <ian@cygnus.com>.
8 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
9 <mark@codesourcery.com>
10 Traditional MIPS targets support added by Koundinya.K, Dansk Data
11 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
12
ae9a127f 13 This file is part of BFD, the Binary File Descriptor library.
b49e97c9 14
ae9a127f
NC
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
cd123cb7 17 the Free Software Foundation; either version 3 of the License, or
ae9a127f 18 (at your option) any later version.
b49e97c9 19
ae9a127f
NC
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
b49e97c9 24
ae9a127f
NC
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
cd123cb7
NC
27 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
28 MA 02110-1301, USA. */
29
b49e97c9
TS
30
31/* This file handles functionality common to the different MIPS ABI's. */
32
b49e97c9 33#include "sysdep.h"
3db64b00 34#include "bfd.h"
b49e97c9 35#include "libbfd.h"
64543e1a 36#include "libiberty.h"
b49e97c9
TS
37#include "elf-bfd.h"
38#include "elfxx-mips.h"
39#include "elf/mips.h"
0a44bf69 40#include "elf-vxworks.h"
b49e97c9
TS
41
42/* Get the ECOFF swapping routines. */
43#include "coff/sym.h"
44#include "coff/symconst.h"
45#include "coff/ecoff.h"
46#include "coff/mips.h"
47
b15e6682
AO
48#include "hashtab.h"
49
ead49a57
RS
50/* This structure is used to hold information about one GOT entry.
51 There are three types of entry:
52
53 (1) absolute addresses
54 (abfd == NULL)
55 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
56 (abfd != NULL, symndx >= 0)
020d7251 57 (3) SYMBOL addresses, where SYMBOL is not local to an input bfd
ead49a57
RS
58 (abfd != NULL, symndx == -1)
59
60 Type (3) entries are treated differently for different types of GOT.
61 In the "master" GOT -- i.e. the one that describes every GOT
62 reference needed in the link -- the mips_got_entry is keyed on both
63 the symbol and the input bfd that references it. If it turns out
64 that we need multiple GOTs, we can then use this information to
65 create separate GOTs for each input bfd.
66
67 However, we want each of these separate GOTs to have at most one
68 entry for a given symbol, so their type (3) entries are keyed only
69 on the symbol. The input bfd given by the "abfd" field is somewhat
70 arbitrary in this case.
71
72 This means that when there are multiple GOTs, each GOT has a unique
73 mips_got_entry for every symbol within it. We can therefore use the
74 mips_got_entry fields (tls_type and gotidx) to track the symbol's
75 GOT index.
76
77 However, if it turns out that we need only a single GOT, we continue
78 to use the master GOT to describe it. There may therefore be several
79 mips_got_entries for the same symbol, each with a different input bfd.
80 We want to make sure that each symbol gets a unique GOT entry, so when
81 there's a single GOT, we use the symbol's hash entry, not the
82 mips_got_entry fields, to track a symbol's GOT index. */
b15e6682
AO
83struct mips_got_entry
84{
85 /* The input bfd in which the symbol is defined. */
86 bfd *abfd;
f4416af6
AO
87 /* The index of the symbol, as stored in the relocation r_info, if
88 we have a local symbol; -1 otherwise. */
89 long symndx;
90 union
91 {
92 /* If abfd == NULL, an address that must be stored in the got. */
93 bfd_vma address;
94 /* If abfd != NULL && symndx != -1, the addend of the relocation
95 that should be added to the symbol value. */
96 bfd_vma addend;
97 /* If abfd != NULL && symndx == -1, the hash table entry
020d7251
RS
98 corresponding to symbol in the GOT. The symbol's entry
99 is in the local area if h->global_got_area is GGA_NONE,
100 otherwise it is in the global area. */
f4416af6
AO
101 struct mips_elf_link_hash_entry *h;
102 } d;
0f20cc35
DJ
103
104 /* The TLS types included in this GOT entry (specifically, GD and
105 IE). The GD and IE flags can be added as we encounter new
106 relocations. LDM can also be set; it will always be alone, not
107 combined with any GD or IE flags. An LDM GOT entry will be
108 a local symbol entry with r_symndx == 0. */
109 unsigned char tls_type;
110
b15e6682 111 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
112 corresponding to this symbol+addend. If it's a global symbol
113 whose offset is yet to be decided, it's going to be -1. */
114 long gotidx;
b15e6682
AO
115};
116
c224138d
RS
117/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
118 The structures form a non-overlapping list that is sorted by increasing
119 MIN_ADDEND. */
120struct mips_got_page_range
121{
122 struct mips_got_page_range *next;
123 bfd_signed_vma min_addend;
124 bfd_signed_vma max_addend;
125};
126
127/* This structure describes the range of addends that are applied to page
128 relocations against a given symbol. */
129struct mips_got_page_entry
130{
131 /* The input bfd in which the symbol is defined. */
132 bfd *abfd;
133 /* The index of the symbol, as stored in the relocation r_info. */
134 long symndx;
135 /* The ranges for this page entry. */
136 struct mips_got_page_range *ranges;
137 /* The maximum number of page entries needed for RANGES. */
138 bfd_vma num_pages;
139};
140
f0abc2a1 141/* This structure is used to hold .got information when linking. */
b49e97c9
TS
142
143struct mips_got_info
144{
145 /* The global symbol in the GOT with the lowest index in the dynamic
146 symbol table. */
147 struct elf_link_hash_entry *global_gotsym;
148 /* The number of global .got entries. */
149 unsigned int global_gotno;
23cc69b6
RS
150 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
151 unsigned int reloc_only_gotno;
0f20cc35
DJ
152 /* The number of .got slots used for TLS. */
153 unsigned int tls_gotno;
154 /* The first unused TLS .got entry. Used only during
155 mips_elf_initialize_tls_index. */
156 unsigned int tls_assigned_gotno;
c224138d 157 /* The number of local .got entries, eventually including page entries. */
b49e97c9 158 unsigned int local_gotno;
c224138d
RS
159 /* The maximum number of page entries needed. */
160 unsigned int page_gotno;
b49e97c9
TS
161 /* The number of local .got entries we have used. */
162 unsigned int assigned_gotno;
b15e6682
AO
163 /* A hash table holding members of the got. */
164 struct htab *got_entries;
c224138d
RS
165 /* A hash table of mips_got_page_entry structures. */
166 struct htab *got_page_entries;
f4416af6
AO
167 /* A hash table mapping input bfds to other mips_got_info. NULL
168 unless multi-got was necessary. */
169 struct htab *bfd2got;
170 /* In multi-got links, a pointer to the next got (err, rather, most
171 of the time, it points to the previous got). */
172 struct mips_got_info *next;
0f20cc35
DJ
173 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
174 for none, or MINUS_TWO for not yet assigned. This is needed
175 because a single-GOT link may have multiple hash table entries
176 for the LDM. It does not get initialized in multi-GOT mode. */
177 bfd_vma tls_ldm_offset;
f4416af6
AO
178};
179
180/* Map an input bfd to a got in a multi-got link. */
181
91d6fa6a
NC
182struct mips_elf_bfd2got_hash
183{
f4416af6
AO
184 bfd *bfd;
185 struct mips_got_info *g;
186};
187
188/* Structure passed when traversing the bfd2got hash table, used to
189 create and merge bfd's gots. */
190
191struct mips_elf_got_per_bfd_arg
192{
193 /* A hashtable that maps bfds to gots. */
194 htab_t bfd2got;
195 /* The output bfd. */
196 bfd *obfd;
197 /* The link information. */
198 struct bfd_link_info *info;
199 /* A pointer to the primary got, i.e., the one that's going to get
200 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
201 DT_MIPS_GOTSYM. */
202 struct mips_got_info *primary;
203 /* A non-primary got we're trying to merge with other input bfd's
204 gots. */
205 struct mips_got_info *current;
206 /* The maximum number of got entries that can be addressed with a
207 16-bit offset. */
208 unsigned int max_count;
c224138d
RS
209 /* The maximum number of page entries needed by each got. */
210 unsigned int max_pages;
0f20cc35
DJ
211 /* The total number of global entries which will live in the
212 primary got and be automatically relocated. This includes
213 those not referenced by the primary GOT but included in
214 the "master" GOT. */
215 unsigned int global_count;
f4416af6
AO
216};
217
218/* Another structure used to pass arguments for got entries traversal. */
219
220struct mips_elf_set_global_got_offset_arg
221{
222 struct mips_got_info *g;
223 int value;
224 unsigned int needed_relocs;
225 struct bfd_link_info *info;
b49e97c9
TS
226};
227
0f20cc35
DJ
228/* A structure used to count TLS relocations or GOT entries, for GOT
229 entry or ELF symbol table traversal. */
230
231struct mips_elf_count_tls_arg
232{
233 struct bfd_link_info *info;
234 unsigned int needed;
235};
236
f0abc2a1
AM
237struct _mips_elf_section_data
238{
239 struct bfd_elf_section_data elf;
240 union
241 {
f0abc2a1
AM
242 bfd_byte *tdata;
243 } u;
244};
245
246#define mips_elf_section_data(sec) \
68bfbfcc 247 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 248
d5eaccd7
RS
249#define is_mips_elf(bfd) \
250 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
251 && elf_tdata (bfd) != NULL \
4dfe6ac6 252 && elf_object_id (bfd) == MIPS_ELF_DATA)
d5eaccd7 253
634835ae
RS
254/* The ABI says that every symbol used by dynamic relocations must have
255 a global GOT entry. Among other things, this provides the dynamic
256 linker with a free, directly-indexed cache. The GOT can therefore
257 contain symbols that are not referenced by GOT relocations themselves
258 (in other words, it may have symbols that are not referenced by things
259 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
260
261 GOT relocations are less likely to overflow if we put the associated
262 GOT entries towards the beginning. We therefore divide the global
263 GOT entries into two areas: "normal" and "reloc-only". Entries in
264 the first area can be used for both dynamic relocations and GP-relative
265 accesses, while those in the "reloc-only" area are for dynamic
266 relocations only.
267
268 These GGA_* ("Global GOT Area") values are organised so that lower
269 values are more general than higher values. Also, non-GGA_NONE
270 values are ordered by the position of the area in the GOT. */
271#define GGA_NORMAL 0
272#define GGA_RELOC_ONLY 1
273#define GGA_NONE 2
274
861fb55a
DJ
275/* Information about a non-PIC interface to a PIC function. There are
276 two ways of creating these interfaces. The first is to add:
277
278 lui $25,%hi(func)
279 addiu $25,$25,%lo(func)
280
281 immediately before a PIC function "func". The second is to add:
282
283 lui $25,%hi(func)
284 j func
285 addiu $25,$25,%lo(func)
286
287 to a separate trampoline section.
288
289 Stubs of the first kind go in a new section immediately before the
290 target function. Stubs of the second kind go in a single section
291 pointed to by the hash table's "strampoline" field. */
292struct mips_elf_la25_stub {
293 /* The generated section that contains this stub. */
294 asection *stub_section;
295
296 /* The offset of the stub from the start of STUB_SECTION. */
297 bfd_vma offset;
298
299 /* One symbol for the original function. Its location is available
300 in H->root.root.u.def. */
301 struct mips_elf_link_hash_entry *h;
302};
303
304/* Macros for populating a mips_elf_la25_stub. */
305
306#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
307#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
308#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
df58fc94
RS
309#define LA25_LUI_MICROMIPS_1(VAL) (0x41b9) /* lui t9,VAL */
310#define LA25_LUI_MICROMIPS_2(VAL) (VAL)
311#define LA25_J_MICROMIPS_1(VAL) (0xd400 | (((VAL) >> 17) & 0x3ff)) /* j VAL */
312#define LA25_J_MICROMIPS_2(VAL) ((VAL) >> 1)
313#define LA25_ADDIU_MICROMIPS_1(VAL) (0x3339) /* addiu t9,t9,VAL */
314#define LA25_ADDIU_MICROMIPS_2(VAL) (VAL)
861fb55a 315
b49e97c9
TS
316/* This structure is passed to mips_elf_sort_hash_table_f when sorting
317 the dynamic symbols. */
318
319struct mips_elf_hash_sort_data
320{
321 /* The symbol in the global GOT with the lowest dynamic symbol table
322 index. */
323 struct elf_link_hash_entry *low;
0f20cc35
DJ
324 /* The least dynamic symbol table index corresponding to a non-TLS
325 symbol with a GOT entry. */
b49e97c9 326 long min_got_dynindx;
f4416af6
AO
327 /* The greatest dynamic symbol table index corresponding to a symbol
328 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 329 with dynamic relocations pointing to it from non-primary GOTs). */
f4416af6 330 long max_unref_got_dynindx;
b49e97c9
TS
331 /* The greatest dynamic symbol table index not corresponding to a
332 symbol without a GOT entry. */
333 long max_non_got_dynindx;
334};
335
336/* The MIPS ELF linker needs additional information for each symbol in
337 the global hash table. */
338
339struct mips_elf_link_hash_entry
340{
341 struct elf_link_hash_entry root;
342
343 /* External symbol information. */
344 EXTR esym;
345
861fb55a
DJ
346 /* The la25 stub we have created for ths symbol, if any. */
347 struct mips_elf_la25_stub *la25_stub;
348
b49e97c9
TS
349 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
350 this symbol. */
351 unsigned int possibly_dynamic_relocs;
352
b49e97c9
TS
353 /* If there is a stub that 32 bit functions should use to call this
354 16 bit function, this points to the section containing the stub. */
355 asection *fn_stub;
356
b49e97c9
TS
357 /* If there is a stub that 16 bit functions should use to call this
358 32 bit function, this points to the section containing the stub. */
359 asection *call_stub;
360
361 /* This is like the call_stub field, but it is used if the function
362 being called returns a floating point value. */
363 asection *call_fp_stub;
7c5fcef7 364
0f20cc35
DJ
365#define GOT_NORMAL 0
366#define GOT_TLS_GD 1
367#define GOT_TLS_LDM 2
368#define GOT_TLS_IE 4
369#define GOT_TLS_OFFSET_DONE 0x40
370#define GOT_TLS_DONE 0x80
371 unsigned char tls_type;
71782a75 372
0f20cc35
DJ
373 /* This is only used in single-GOT mode; in multi-GOT mode there
374 is one mips_got_entry per GOT entry, so the offset is stored
375 there. In single-GOT mode there may be many mips_got_entry
376 structures all referring to the same GOT slot. It might be
377 possible to use root.got.offset instead, but that field is
378 overloaded already. */
379 bfd_vma tls_got_offset;
71782a75 380
634835ae
RS
381 /* The highest GGA_* value that satisfies all references to this symbol. */
382 unsigned int global_got_area : 2;
383
6ccf4795
RS
384 /* True if all GOT relocations against this symbol are for calls. This is
385 a looser condition than no_fn_stub below, because there may be other
386 non-call non-GOT relocations against the symbol. */
387 unsigned int got_only_for_calls : 1;
388
71782a75
RS
389 /* True if one of the relocations described by possibly_dynamic_relocs
390 is against a readonly section. */
391 unsigned int readonly_reloc : 1;
392
861fb55a
DJ
393 /* True if there is a relocation against this symbol that must be
394 resolved by the static linker (in other words, if the relocation
395 cannot possibly be made dynamic). */
396 unsigned int has_static_relocs : 1;
397
71782a75
RS
398 /* True if we must not create a .MIPS.stubs entry for this symbol.
399 This is set, for example, if there are relocations related to
400 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
401 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
402 unsigned int no_fn_stub : 1;
403
404 /* Whether we need the fn_stub; this is true if this symbol appears
405 in any relocs other than a 16 bit call. */
406 unsigned int need_fn_stub : 1;
407
861fb55a
DJ
408 /* True if this symbol is referenced by branch relocations from
409 any non-PIC input file. This is used to determine whether an
410 la25 stub is required. */
411 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
412
413 /* Does this symbol need a traditional MIPS lazy-binding stub
414 (as opposed to a PLT entry)? */
415 unsigned int needs_lazy_stub : 1;
b49e97c9
TS
416};
417
418/* MIPS ELF linker hash table. */
419
420struct mips_elf_link_hash_table
421{
422 struct elf_link_hash_table root;
423#if 0
424 /* We no longer use this. */
425 /* String section indices for the dynamic section symbols. */
426 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
427#endif
861fb55a 428
b49e97c9
TS
429 /* The number of .rtproc entries. */
430 bfd_size_type procedure_count;
861fb55a 431
b49e97c9
TS
432 /* The size of the .compact_rel section (if SGI_COMPAT). */
433 bfd_size_type compact_rel_size;
861fb55a 434
b49e97c9 435 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
8dc1a139 436 entry is set to the address of __rld_obj_head as in IRIX5. */
b34976b6 437 bfd_boolean use_rld_obj_head;
861fb55a 438
b49e97c9
TS
439 /* This is the value of the __rld_map or __rld_obj_head symbol. */
440 bfd_vma rld_value;
861fb55a 441
b49e97c9 442 /* This is set if we see any mips16 stub sections. */
b34976b6 443 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
444
445 /* True if we can generate copy relocs and PLTs. */
446 bfd_boolean use_plts_and_copy_relocs;
447
0a44bf69
RS
448 /* True if we're generating code for VxWorks. */
449 bfd_boolean is_vxworks;
861fb55a 450
0e53d9da
AN
451 /* True if we already reported the small-data section overflow. */
452 bfd_boolean small_data_overflow_reported;
861fb55a 453
0a44bf69
RS
454 /* Shortcuts to some dynamic sections, or NULL if they are not
455 being used. */
456 asection *srelbss;
457 asection *sdynbss;
458 asection *srelplt;
459 asection *srelplt2;
460 asection *sgotplt;
461 asection *splt;
4e41d0d7 462 asection *sstubs;
a8028dd0 463 asection *sgot;
861fb55a 464
a8028dd0
RS
465 /* The master GOT information. */
466 struct mips_got_info *got_info;
861fb55a
DJ
467
468 /* The size of the PLT header in bytes. */
0a44bf69 469 bfd_vma plt_header_size;
861fb55a
DJ
470
471 /* The size of a PLT entry in bytes. */
0a44bf69 472 bfd_vma plt_entry_size;
861fb55a 473
33bb52fb
RS
474 /* The number of functions that need a lazy-binding stub. */
475 bfd_vma lazy_stub_count;
861fb55a 476
5108fc1b
RS
477 /* The size of a function stub entry in bytes. */
478 bfd_vma function_stub_size;
861fb55a
DJ
479
480 /* The number of reserved entries at the beginning of the GOT. */
481 unsigned int reserved_gotno;
482
483 /* The section used for mips_elf_la25_stub trampolines.
484 See the comment above that structure for details. */
485 asection *strampoline;
486
487 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
488 pairs. */
489 htab_t la25_stubs;
490
491 /* A function FN (NAME, IS, OS) that creates a new input section
492 called NAME and links it to output section OS. If IS is nonnull,
493 the new section should go immediately before it, otherwise it
494 should go at the (current) beginning of OS.
495
496 The function returns the new section on success, otherwise it
497 returns null. */
498 asection *(*add_stub_section) (const char *, asection *, asection *);
499};
500
4dfe6ac6
NC
501/* Get the MIPS ELF linker hash table from a link_info structure. */
502
503#define mips_elf_hash_table(p) \
504 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
505 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
506
861fb55a 507/* A structure used to communicate with htab_traverse callbacks. */
4dfe6ac6
NC
508struct mips_htab_traverse_info
509{
861fb55a
DJ
510 /* The usual link-wide information. */
511 struct bfd_link_info *info;
512 bfd *output_bfd;
513
514 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
515 bfd_boolean error;
b49e97c9
TS
516};
517
0f20cc35
DJ
518#define TLS_RELOC_P(r_type) \
519 (r_type == R_MIPS_TLS_DTPMOD32 \
520 || r_type == R_MIPS_TLS_DTPMOD64 \
521 || r_type == R_MIPS_TLS_DTPREL32 \
522 || r_type == R_MIPS_TLS_DTPREL64 \
523 || r_type == R_MIPS_TLS_GD \
524 || r_type == R_MIPS_TLS_LDM \
525 || r_type == R_MIPS_TLS_DTPREL_HI16 \
526 || r_type == R_MIPS_TLS_DTPREL_LO16 \
527 || r_type == R_MIPS_TLS_GOTTPREL \
528 || r_type == R_MIPS_TLS_TPREL32 \
529 || r_type == R_MIPS_TLS_TPREL64 \
530 || r_type == R_MIPS_TLS_TPREL_HI16 \
df58fc94
RS
531 || r_type == R_MIPS_TLS_TPREL_LO16 \
532 || r_type == R_MICROMIPS_TLS_GD \
533 || r_type == R_MICROMIPS_TLS_LDM \
534 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
535 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
536 || r_type == R_MICROMIPS_TLS_GOTTPREL \
537 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
538 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
0f20cc35 539
b49e97c9
TS
540/* Structure used to pass information to mips_elf_output_extsym. */
541
542struct extsym_info
543{
9e4aeb93
RS
544 bfd *abfd;
545 struct bfd_link_info *info;
b49e97c9
TS
546 struct ecoff_debug_info *debug;
547 const struct ecoff_debug_swap *swap;
b34976b6 548 bfd_boolean failed;
b49e97c9
TS
549};
550
8dc1a139 551/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
552
553static const char * const mips_elf_dynsym_rtproc_names[] =
554{
555 "_procedure_table",
556 "_procedure_string_table",
557 "_procedure_table_size",
558 NULL
559};
560
561/* These structures are used to generate the .compact_rel section on
8dc1a139 562 IRIX5. */
b49e97c9
TS
563
564typedef struct
565{
566 unsigned long id1; /* Always one? */
567 unsigned long num; /* Number of compact relocation entries. */
568 unsigned long id2; /* Always two? */
569 unsigned long offset; /* The file offset of the first relocation. */
570 unsigned long reserved0; /* Zero? */
571 unsigned long reserved1; /* Zero? */
572} Elf32_compact_rel;
573
574typedef struct
575{
576 bfd_byte id1[4];
577 bfd_byte num[4];
578 bfd_byte id2[4];
579 bfd_byte offset[4];
580 bfd_byte reserved0[4];
581 bfd_byte reserved1[4];
582} Elf32_External_compact_rel;
583
584typedef struct
585{
586 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
587 unsigned int rtype : 4; /* Relocation types. See below. */
588 unsigned int dist2to : 8;
589 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
590 unsigned long konst; /* KONST field. See below. */
591 unsigned long vaddr; /* VADDR to be relocated. */
592} Elf32_crinfo;
593
594typedef struct
595{
596 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
597 unsigned int rtype : 4; /* Relocation types. See below. */
598 unsigned int dist2to : 8;
599 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
600 unsigned long konst; /* KONST field. See below. */
601} Elf32_crinfo2;
602
603typedef struct
604{
605 bfd_byte info[4];
606 bfd_byte konst[4];
607 bfd_byte vaddr[4];
608} Elf32_External_crinfo;
609
610typedef struct
611{
612 bfd_byte info[4];
613 bfd_byte konst[4];
614} Elf32_External_crinfo2;
615
616/* These are the constants used to swap the bitfields in a crinfo. */
617
618#define CRINFO_CTYPE (0x1)
619#define CRINFO_CTYPE_SH (31)
620#define CRINFO_RTYPE (0xf)
621#define CRINFO_RTYPE_SH (27)
622#define CRINFO_DIST2TO (0xff)
623#define CRINFO_DIST2TO_SH (19)
624#define CRINFO_RELVADDR (0x7ffff)
625#define CRINFO_RELVADDR_SH (0)
626
627/* A compact relocation info has long (3 words) or short (2 words)
628 formats. A short format doesn't have VADDR field and relvaddr
629 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
630#define CRF_MIPS_LONG 1
631#define CRF_MIPS_SHORT 0
632
633/* There are 4 types of compact relocation at least. The value KONST
634 has different meaning for each type:
635
636 (type) (konst)
637 CT_MIPS_REL32 Address in data
638 CT_MIPS_WORD Address in word (XXX)
639 CT_MIPS_GPHI_LO GP - vaddr
640 CT_MIPS_JMPAD Address to jump
641 */
642
643#define CRT_MIPS_REL32 0xa
644#define CRT_MIPS_WORD 0xb
645#define CRT_MIPS_GPHI_LO 0xc
646#define CRT_MIPS_JMPAD 0xd
647
648#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
649#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
650#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
651#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
652\f
653/* The structure of the runtime procedure descriptor created by the
654 loader for use by the static exception system. */
655
656typedef struct runtime_pdr {
ae9a127f
NC
657 bfd_vma adr; /* Memory address of start of procedure. */
658 long regmask; /* Save register mask. */
659 long regoffset; /* Save register offset. */
660 long fregmask; /* Save floating point register mask. */
661 long fregoffset; /* Save floating point register offset. */
662 long frameoffset; /* Frame size. */
663 short framereg; /* Frame pointer register. */
664 short pcreg; /* Offset or reg of return pc. */
665 long irpss; /* Index into the runtime string table. */
b49e97c9 666 long reserved;
ae9a127f 667 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
668} RPDR, *pRPDR;
669#define cbRPDR sizeof (RPDR)
670#define rpdNil ((pRPDR) 0)
671\f
b15e6682 672static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
673 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
674 struct mips_elf_link_hash_entry *, int);
b34976b6 675static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 676 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
677static bfd_vma mips_elf_high
678 (bfd_vma);
b34976b6 679static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
680 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
681 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
682 bfd_vma *, asection *);
9719ad41
RS
683static hashval_t mips_elf_got_entry_hash
684 (const void *);
f4416af6 685static bfd_vma mips_elf_adjust_gp
9719ad41 686 (bfd *, struct mips_got_info *, bfd *);
f4416af6 687static struct mips_got_info *mips_elf_got_for_ibfd
9719ad41 688 (struct mips_got_info *, bfd *);
f4416af6 689
b49e97c9
TS
690/* This will be used when we sort the dynamic relocation records. */
691static bfd *reldyn_sorting_bfd;
692
6d30f5b2
NC
693/* True if ABFD is for CPUs with load interlocking that include
694 non-MIPS1 CPUs and R3900. */
695#define LOAD_INTERLOCKS_P(abfd) \
696 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
697 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
698
cd8d5a82
CF
699/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
700 This should be safe for all architectures. We enable this predicate
701 for RM9000 for now. */
702#define JAL_TO_BAL_P(abfd) \
703 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
704
705/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
706 This should be safe for all architectures. We enable this predicate for
707 all CPUs. */
708#define JALR_TO_BAL_P(abfd) 1
709
38a7df63
CF
710/* True if ABFD is for CPUs that are faster if JR is converted to B.
711 This should be safe for all architectures. We enable this predicate for
712 all CPUs. */
713#define JR_TO_B_P(abfd) 1
714
861fb55a
DJ
715/* True if ABFD is a PIC object. */
716#define PIC_OBJECT_P(abfd) \
717 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
718
b49e97c9 719/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
720#define ABI_N32_P(abfd) \
721 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
722
4a14403c 723/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 724#define ABI_64_P(abfd) \
141ff970 725 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 726
4a14403c
TS
727/* Nonzero if ABFD is using NewABI conventions. */
728#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
729
730/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
731#define IRIX_COMPAT(abfd) \
732 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
733
b49e97c9
TS
734/* Whether we are trying to be compatible with IRIX at all. */
735#define SGI_COMPAT(abfd) \
736 (IRIX_COMPAT (abfd) != ict_none)
737
738/* The name of the options section. */
739#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 740 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 741
cc2e31b9
RS
742/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
743 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
744#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
745 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
746
943284cc
DJ
747/* Whether the section is readonly. */
748#define MIPS_ELF_READONLY_SECTION(sec) \
749 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
750 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
751
b49e97c9 752/* The name of the stub section. */
ca07892d 753#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
754
755/* The size of an external REL relocation. */
756#define MIPS_ELF_REL_SIZE(abfd) \
757 (get_elf_backend_data (abfd)->s->sizeof_rel)
758
0a44bf69
RS
759/* The size of an external RELA relocation. */
760#define MIPS_ELF_RELA_SIZE(abfd) \
761 (get_elf_backend_data (abfd)->s->sizeof_rela)
762
b49e97c9
TS
763/* The size of an external dynamic table entry. */
764#define MIPS_ELF_DYN_SIZE(abfd) \
765 (get_elf_backend_data (abfd)->s->sizeof_dyn)
766
767/* The size of a GOT entry. */
768#define MIPS_ELF_GOT_SIZE(abfd) \
769 (get_elf_backend_data (abfd)->s->arch_size / 8)
770
771/* The size of a symbol-table entry. */
772#define MIPS_ELF_SYM_SIZE(abfd) \
773 (get_elf_backend_data (abfd)->s->sizeof_sym)
774
775/* The default alignment for sections, as a power of two. */
776#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 777 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
778
779/* Get word-sized data. */
780#define MIPS_ELF_GET_WORD(abfd, ptr) \
781 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
782
783/* Put out word-sized data. */
784#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
785 (ABI_64_P (abfd) \
786 ? bfd_put_64 (abfd, val, ptr) \
787 : bfd_put_32 (abfd, val, ptr))
788
861fb55a
DJ
789/* The opcode for word-sized loads (LW or LD). */
790#define MIPS_ELF_LOAD_WORD(abfd) \
791 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
792
b49e97c9 793/* Add a dynamic symbol table-entry. */
9719ad41 794#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 795 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
796
797#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
798 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
799
0a44bf69
RS
800/* The name of the dynamic relocation section. */
801#define MIPS_ELF_REL_DYN_NAME(INFO) \
802 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
803
b49e97c9
TS
804/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
805 from smaller values. Start with zero, widen, *then* decrement. */
806#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 807#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 808
51e38d68
RS
809/* The value to write into got[1] for SVR4 targets, to identify it is
810 a GNU object. The dynamic linker can then use got[1] to store the
811 module pointer. */
812#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
813 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
814
f4416af6 815/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
816#define ELF_MIPS_GP_OFFSET(INFO) \
817 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
818
819/* The maximum size of the GOT for it to be addressable using 16-bit
820 offsets from $gp. */
0a44bf69 821#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 822
6a691779 823/* Instructions which appear in a stub. */
3d6746ca
DD
824#define STUB_LW(abfd) \
825 ((ABI_64_P (abfd) \
826 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
827 : 0x8f998010)) /* lw t9,0x8010(gp) */
828#define STUB_MOVE(abfd) \
829 ((ABI_64_P (abfd) \
830 ? 0x03e0782d /* daddu t7,ra */ \
831 : 0x03e07821)) /* addu t7,ra */
832#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
833#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
834#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
835#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
836#define STUB_LI16S(abfd, VAL) \
837 ((ABI_64_P (abfd) \
838 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
839 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
840
5108fc1b
RS
841#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
842#define MIPS_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
843
844/* The name of the dynamic interpreter. This is put in the .interp
845 section. */
846
847#define ELF_DYNAMIC_INTERPRETER(abfd) \
848 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
849 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
850 : "/usr/lib/libc.so.1")
851
852#ifdef BFD64
ee6423ed
AO
853#define MNAME(bfd,pre,pos) \
854 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
855#define ELF_R_SYM(bfd, i) \
856 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
857#define ELF_R_TYPE(bfd, i) \
858 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
859#define ELF_R_INFO(bfd, s, t) \
860 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
861#else
ee6423ed 862#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
863#define ELF_R_SYM(bfd, i) \
864 (ELF32_R_SYM (i))
865#define ELF_R_TYPE(bfd, i) \
866 (ELF32_R_TYPE (i))
867#define ELF_R_INFO(bfd, s, t) \
868 (ELF32_R_INFO (s, t))
869#endif
870\f
871 /* The mips16 compiler uses a couple of special sections to handle
872 floating point arguments.
873
874 Section names that look like .mips16.fn.FNNAME contain stubs that
875 copy floating point arguments from the fp regs to the gp regs and
876 then jump to FNNAME. If any 32 bit function calls FNNAME, the
877 call should be redirected to the stub instead. If no 32 bit
878 function calls FNNAME, the stub should be discarded. We need to
879 consider any reference to the function, not just a call, because
880 if the address of the function is taken we will need the stub,
881 since the address might be passed to a 32 bit function.
882
883 Section names that look like .mips16.call.FNNAME contain stubs
884 that copy floating point arguments from the gp regs to the fp
885 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
886 then any 16 bit function that calls FNNAME should be redirected
887 to the stub instead. If FNNAME is not a 32 bit function, the
888 stub should be discarded.
889
890 .mips16.call.fp.FNNAME sections are similar, but contain stubs
891 which call FNNAME and then copy the return value from the fp regs
892 to the gp regs. These stubs store the return value in $18 while
893 calling FNNAME; any function which might call one of these stubs
894 must arrange to save $18 around the call. (This case is not
895 needed for 32 bit functions that call 16 bit functions, because
896 16 bit functions always return floating point values in both
897 $f0/$f1 and $2/$3.)
898
899 Note that in all cases FNNAME might be defined statically.
900 Therefore, FNNAME is not used literally. Instead, the relocation
901 information will indicate which symbol the section is for.
902
903 We record any stubs that we find in the symbol table. */
904
905#define FN_STUB ".mips16.fn."
906#define CALL_STUB ".mips16.call."
907#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
908
909#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
910#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
911#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 912\f
861fb55a 913/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
914static const bfd_vma mips_o32_exec_plt0_entry[] =
915{
861fb55a
DJ
916 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
917 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
918 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
919 0x031cc023, /* subu $24, $24, $28 */
81f5d455 920 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
861fb55a
DJ
921 0x0018c082, /* srl $24, $24, 2 */
922 0x0320f809, /* jalr $25 */
923 0x2718fffe /* subu $24, $24, 2 */
924};
925
926/* The format of the first PLT entry in an N32 executable. Different
927 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
928static const bfd_vma mips_n32_exec_plt0_entry[] =
929{
861fb55a
DJ
930 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
931 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
932 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
933 0x030ec023, /* subu $24, $24, $14 */
81f5d455 934 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
861fb55a
DJ
935 0x0018c082, /* srl $24, $24, 2 */
936 0x0320f809, /* jalr $25 */
937 0x2718fffe /* subu $24, $24, 2 */
938};
939
940/* The format of the first PLT entry in an N64 executable. Different
941 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
942static const bfd_vma mips_n64_exec_plt0_entry[] =
943{
861fb55a
DJ
944 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
945 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
946 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
947 0x030ec023, /* subu $24, $24, $14 */
81f5d455 948 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
861fb55a
DJ
949 0x0018c0c2, /* srl $24, $24, 3 */
950 0x0320f809, /* jalr $25 */
951 0x2718fffe /* subu $24, $24, 2 */
952};
953
954/* The format of subsequent PLT entries. */
6d30f5b2
NC
955static const bfd_vma mips_exec_plt_entry[] =
956{
861fb55a
DJ
957 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
958 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
959 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
960 0x03200008 /* jr $25 */
961};
962
0a44bf69 963/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
964static const bfd_vma mips_vxworks_exec_plt0_entry[] =
965{
0a44bf69
RS
966 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
967 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
968 0x8f390008, /* lw t9, 8(t9) */
969 0x00000000, /* nop */
970 0x03200008, /* jr t9 */
971 0x00000000 /* nop */
972};
973
974/* The format of subsequent PLT entries. */
6d30f5b2
NC
975static const bfd_vma mips_vxworks_exec_plt_entry[] =
976{
0a44bf69
RS
977 0x10000000, /* b .PLT_resolver */
978 0x24180000, /* li t8, <pltindex> */
979 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
980 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
981 0x8f390000, /* lw t9, 0(t9) */
982 0x00000000, /* nop */
983 0x03200008, /* jr t9 */
984 0x00000000 /* nop */
985};
986
987/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
988static const bfd_vma mips_vxworks_shared_plt0_entry[] =
989{
0a44bf69
RS
990 0x8f990008, /* lw t9, 8(gp) */
991 0x00000000, /* nop */
992 0x03200008, /* jr t9 */
993 0x00000000, /* nop */
994 0x00000000, /* nop */
995 0x00000000 /* nop */
996};
997
998/* The format of subsequent PLT entries. */
6d30f5b2
NC
999static const bfd_vma mips_vxworks_shared_plt_entry[] =
1000{
0a44bf69
RS
1001 0x10000000, /* b .PLT_resolver */
1002 0x24180000 /* li t8, <pltindex> */
1003};
1004\f
b49e97c9
TS
1005/* Look up an entry in a MIPS ELF linker hash table. */
1006
1007#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1008 ((struct mips_elf_link_hash_entry *) \
1009 elf_link_hash_lookup (&(table)->root, (string), (create), \
1010 (copy), (follow)))
1011
1012/* Traverse a MIPS ELF linker hash table. */
1013
1014#define mips_elf_link_hash_traverse(table, func, info) \
1015 (elf_link_hash_traverse \
1016 (&(table)->root, \
9719ad41 1017 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1018 (info)))
1019
0f20cc35
DJ
1020/* Find the base offsets for thread-local storage in this object,
1021 for GD/LD and IE/LE respectively. */
1022
1023#define TP_OFFSET 0x7000
1024#define DTP_OFFSET 0x8000
1025
1026static bfd_vma
1027dtprel_base (struct bfd_link_info *info)
1028{
1029 /* If tls_sec is NULL, we should have signalled an error already. */
1030 if (elf_hash_table (info)->tls_sec == NULL)
1031 return 0;
1032 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1033}
1034
1035static bfd_vma
1036tprel_base (struct bfd_link_info *info)
1037{
1038 /* If tls_sec is NULL, we should have signalled an error already. */
1039 if (elf_hash_table (info)->tls_sec == NULL)
1040 return 0;
1041 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1042}
1043
b49e97c9
TS
1044/* Create an entry in a MIPS ELF linker hash table. */
1045
1046static struct bfd_hash_entry *
9719ad41
RS
1047mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1048 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1049{
1050 struct mips_elf_link_hash_entry *ret =
1051 (struct mips_elf_link_hash_entry *) entry;
1052
1053 /* Allocate the structure if it has not already been allocated by a
1054 subclass. */
9719ad41
RS
1055 if (ret == NULL)
1056 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1057 if (ret == NULL)
b49e97c9
TS
1058 return (struct bfd_hash_entry *) ret;
1059
1060 /* Call the allocation method of the superclass. */
1061 ret = ((struct mips_elf_link_hash_entry *)
1062 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1063 table, string));
9719ad41 1064 if (ret != NULL)
b49e97c9
TS
1065 {
1066 /* Set local fields. */
1067 memset (&ret->esym, 0, sizeof (EXTR));
1068 /* We use -2 as a marker to indicate that the information has
1069 not been set. -1 means there is no associated ifd. */
1070 ret->esym.ifd = -2;
861fb55a 1071 ret->la25_stub = 0;
b49e97c9 1072 ret->possibly_dynamic_relocs = 0;
b49e97c9 1073 ret->fn_stub = NULL;
b49e97c9
TS
1074 ret->call_stub = NULL;
1075 ret->call_fp_stub = NULL;
71782a75 1076 ret->tls_type = GOT_NORMAL;
634835ae 1077 ret->global_got_area = GGA_NONE;
6ccf4795 1078 ret->got_only_for_calls = TRUE;
71782a75 1079 ret->readonly_reloc = FALSE;
861fb55a 1080 ret->has_static_relocs = FALSE;
71782a75
RS
1081 ret->no_fn_stub = FALSE;
1082 ret->need_fn_stub = FALSE;
861fb55a 1083 ret->has_nonpic_branches = FALSE;
33bb52fb 1084 ret->needs_lazy_stub = FALSE;
b49e97c9
TS
1085 }
1086
1087 return (struct bfd_hash_entry *) ret;
1088}
f0abc2a1
AM
1089
1090bfd_boolean
9719ad41 1091_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1092{
f592407e
AM
1093 if (!sec->used_by_bfd)
1094 {
1095 struct _mips_elf_section_data *sdata;
1096 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1097
f592407e
AM
1098 sdata = bfd_zalloc (abfd, amt);
1099 if (sdata == NULL)
1100 return FALSE;
1101 sec->used_by_bfd = sdata;
1102 }
f0abc2a1
AM
1103
1104 return _bfd_elf_new_section_hook (abfd, sec);
1105}
b49e97c9
TS
1106\f
1107/* Read ECOFF debugging information from a .mdebug section into a
1108 ecoff_debug_info structure. */
1109
b34976b6 1110bfd_boolean
9719ad41
RS
1111_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1112 struct ecoff_debug_info *debug)
b49e97c9
TS
1113{
1114 HDRR *symhdr;
1115 const struct ecoff_debug_swap *swap;
9719ad41 1116 char *ext_hdr;
b49e97c9
TS
1117
1118 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1119 memset (debug, 0, sizeof (*debug));
1120
9719ad41 1121 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1122 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1123 goto error_return;
1124
9719ad41 1125 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1126 swap->external_hdr_size))
b49e97c9
TS
1127 goto error_return;
1128
1129 symhdr = &debug->symbolic_header;
1130 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1131
1132 /* The symbolic header contains absolute file offsets and sizes to
1133 read. */
1134#define READ(ptr, offset, count, size, type) \
1135 if (symhdr->count == 0) \
1136 debug->ptr = NULL; \
1137 else \
1138 { \
1139 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1140 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1141 if (debug->ptr == NULL) \
1142 goto error_return; \
9719ad41 1143 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1144 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1145 goto error_return; \
1146 }
1147
1148 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1149 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1150 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1151 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1152 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1153 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1154 union aux_ext *);
1155 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1156 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1157 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1158 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1159 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1160#undef READ
1161
1162 debug->fdr = NULL;
b49e97c9 1163
b34976b6 1164 return TRUE;
b49e97c9
TS
1165
1166 error_return:
1167 if (ext_hdr != NULL)
1168 free (ext_hdr);
1169 if (debug->line != NULL)
1170 free (debug->line);
1171 if (debug->external_dnr != NULL)
1172 free (debug->external_dnr);
1173 if (debug->external_pdr != NULL)
1174 free (debug->external_pdr);
1175 if (debug->external_sym != NULL)
1176 free (debug->external_sym);
1177 if (debug->external_opt != NULL)
1178 free (debug->external_opt);
1179 if (debug->external_aux != NULL)
1180 free (debug->external_aux);
1181 if (debug->ss != NULL)
1182 free (debug->ss);
1183 if (debug->ssext != NULL)
1184 free (debug->ssext);
1185 if (debug->external_fdr != NULL)
1186 free (debug->external_fdr);
1187 if (debug->external_rfd != NULL)
1188 free (debug->external_rfd);
1189 if (debug->external_ext != NULL)
1190 free (debug->external_ext);
b34976b6 1191 return FALSE;
b49e97c9
TS
1192}
1193\f
1194/* Swap RPDR (runtime procedure table entry) for output. */
1195
1196static void
9719ad41 1197ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1198{
1199 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1200 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1201 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1202 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1203 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1204 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1205
1206 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1207 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1208
1209 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1210}
1211
1212/* Create a runtime procedure table from the .mdebug section. */
1213
b34976b6 1214static bfd_boolean
9719ad41
RS
1215mips_elf_create_procedure_table (void *handle, bfd *abfd,
1216 struct bfd_link_info *info, asection *s,
1217 struct ecoff_debug_info *debug)
b49e97c9
TS
1218{
1219 const struct ecoff_debug_swap *swap;
1220 HDRR *hdr = &debug->symbolic_header;
1221 RPDR *rpdr, *rp;
1222 struct rpdr_ext *erp;
9719ad41 1223 void *rtproc;
b49e97c9
TS
1224 struct pdr_ext *epdr;
1225 struct sym_ext *esym;
1226 char *ss, **sv;
1227 char *str;
1228 bfd_size_type size;
1229 bfd_size_type count;
1230 unsigned long sindex;
1231 unsigned long i;
1232 PDR pdr;
1233 SYMR sym;
1234 const char *no_name_func = _("static procedure (no name)");
1235
1236 epdr = NULL;
1237 rpdr = NULL;
1238 esym = NULL;
1239 ss = NULL;
1240 sv = NULL;
1241
1242 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1243
1244 sindex = strlen (no_name_func) + 1;
1245 count = hdr->ipdMax;
1246 if (count > 0)
1247 {
1248 size = swap->external_pdr_size;
1249
9719ad41 1250 epdr = bfd_malloc (size * count);
b49e97c9
TS
1251 if (epdr == NULL)
1252 goto error_return;
1253
9719ad41 1254 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1255 goto error_return;
1256
1257 size = sizeof (RPDR);
9719ad41 1258 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1259 if (rpdr == NULL)
1260 goto error_return;
1261
1262 size = sizeof (char *);
9719ad41 1263 sv = bfd_malloc (size * count);
b49e97c9
TS
1264 if (sv == NULL)
1265 goto error_return;
1266
1267 count = hdr->isymMax;
1268 size = swap->external_sym_size;
9719ad41 1269 esym = bfd_malloc (size * count);
b49e97c9
TS
1270 if (esym == NULL)
1271 goto error_return;
1272
9719ad41 1273 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1274 goto error_return;
1275
1276 count = hdr->issMax;
9719ad41 1277 ss = bfd_malloc (count);
b49e97c9
TS
1278 if (ss == NULL)
1279 goto error_return;
f075ee0c 1280 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1281 goto error_return;
1282
1283 count = hdr->ipdMax;
1284 for (i = 0; i < (unsigned long) count; i++, rp++)
1285 {
9719ad41
RS
1286 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1287 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1288 rp->adr = sym.value;
1289 rp->regmask = pdr.regmask;
1290 rp->regoffset = pdr.regoffset;
1291 rp->fregmask = pdr.fregmask;
1292 rp->fregoffset = pdr.fregoffset;
1293 rp->frameoffset = pdr.frameoffset;
1294 rp->framereg = pdr.framereg;
1295 rp->pcreg = pdr.pcreg;
1296 rp->irpss = sindex;
1297 sv[i] = ss + sym.iss;
1298 sindex += strlen (sv[i]) + 1;
1299 }
1300 }
1301
1302 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1303 size = BFD_ALIGN (size, 16);
9719ad41 1304 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1305 if (rtproc == NULL)
1306 {
1307 mips_elf_hash_table (info)->procedure_count = 0;
1308 goto error_return;
1309 }
1310
1311 mips_elf_hash_table (info)->procedure_count = count + 2;
1312
9719ad41 1313 erp = rtproc;
b49e97c9
TS
1314 memset (erp, 0, sizeof (struct rpdr_ext));
1315 erp++;
1316 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1317 strcpy (str, no_name_func);
1318 str += strlen (no_name_func) + 1;
1319 for (i = 0; i < count; i++)
1320 {
1321 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1322 strcpy (str, sv[i]);
1323 str += strlen (sv[i]) + 1;
1324 }
1325 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1326
1327 /* Set the size and contents of .rtproc section. */
eea6121a 1328 s->size = size;
9719ad41 1329 s->contents = rtproc;
b49e97c9
TS
1330
1331 /* Skip this section later on (I don't think this currently
1332 matters, but someday it might). */
8423293d 1333 s->map_head.link_order = NULL;
b49e97c9
TS
1334
1335 if (epdr != NULL)
1336 free (epdr);
1337 if (rpdr != NULL)
1338 free (rpdr);
1339 if (esym != NULL)
1340 free (esym);
1341 if (ss != NULL)
1342 free (ss);
1343 if (sv != NULL)
1344 free (sv);
1345
b34976b6 1346 return TRUE;
b49e97c9
TS
1347
1348 error_return:
1349 if (epdr != NULL)
1350 free (epdr);
1351 if (rpdr != NULL)
1352 free (rpdr);
1353 if (esym != NULL)
1354 free (esym);
1355 if (ss != NULL)
1356 free (ss);
1357 if (sv != NULL)
1358 free (sv);
b34976b6 1359 return FALSE;
b49e97c9 1360}
738e5348 1361\f
861fb55a
DJ
1362/* We're going to create a stub for H. Create a symbol for the stub's
1363 value and size, to help make the disassembly easier to read. */
1364
1365static bfd_boolean
1366mips_elf_create_stub_symbol (struct bfd_link_info *info,
1367 struct mips_elf_link_hash_entry *h,
1368 const char *prefix, asection *s, bfd_vma value,
1369 bfd_vma size)
1370{
1371 struct bfd_link_hash_entry *bh;
1372 struct elf_link_hash_entry *elfh;
1373 const char *name;
1374
df58fc94
RS
1375 if (ELF_ST_IS_MICROMIPS (h->root.other))
1376 value |= 1;
1377
861fb55a
DJ
1378 /* Create a new symbol. */
1379 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1380 bh = NULL;
1381 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1382 BSF_LOCAL, s, value, NULL,
1383 TRUE, FALSE, &bh))
1384 return FALSE;
1385
1386 /* Make it a local function. */
1387 elfh = (struct elf_link_hash_entry *) bh;
1388 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1389 elfh->size = size;
1390 elfh->forced_local = 1;
1391 return TRUE;
1392}
1393
738e5348
RS
1394/* We're about to redefine H. Create a symbol to represent H's
1395 current value and size, to help make the disassembly easier
1396 to read. */
1397
1398static bfd_boolean
1399mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1400 struct mips_elf_link_hash_entry *h,
1401 const char *prefix)
1402{
1403 struct bfd_link_hash_entry *bh;
1404 struct elf_link_hash_entry *elfh;
1405 const char *name;
1406 asection *s;
1407 bfd_vma value;
1408
1409 /* Read the symbol's value. */
1410 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1411 || h->root.root.type == bfd_link_hash_defweak);
1412 s = h->root.root.u.def.section;
1413 value = h->root.root.u.def.value;
1414
1415 /* Create a new symbol. */
1416 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1417 bh = NULL;
1418 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1419 BSF_LOCAL, s, value, NULL,
1420 TRUE, FALSE, &bh))
1421 return FALSE;
1422
1423 /* Make it local and copy the other attributes from H. */
1424 elfh = (struct elf_link_hash_entry *) bh;
1425 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1426 elfh->other = h->root.other;
1427 elfh->size = h->root.size;
1428 elfh->forced_local = 1;
1429 return TRUE;
1430}
1431
1432/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1433 function rather than to a hard-float stub. */
1434
1435static bfd_boolean
1436section_allows_mips16_refs_p (asection *section)
1437{
1438 const char *name;
1439
1440 name = bfd_get_section_name (section->owner, section);
1441 return (FN_STUB_P (name)
1442 || CALL_STUB_P (name)
1443 || CALL_FP_STUB_P (name)
1444 || strcmp (name, ".pdr") == 0);
1445}
1446
1447/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1448 stub section of some kind. Return the R_SYMNDX of the target
1449 function, or 0 if we can't decide which function that is. */
1450
1451static unsigned long
502e814e
TT
1452mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1453 const Elf_Internal_Rela *relocs,
738e5348
RS
1454 const Elf_Internal_Rela *relend)
1455{
1456 const Elf_Internal_Rela *rel;
1457
1458 /* Trust the first R_MIPS_NONE relocation, if any. */
1459 for (rel = relocs; rel < relend; rel++)
1460 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1461 return ELF_R_SYM (sec->owner, rel->r_info);
1462
1463 /* Otherwise trust the first relocation, whatever its kind. This is
1464 the traditional behavior. */
1465 if (relocs < relend)
1466 return ELF_R_SYM (sec->owner, relocs->r_info);
1467
1468 return 0;
1469}
b49e97c9
TS
1470
1471/* Check the mips16 stubs for a particular symbol, and see if we can
1472 discard them. */
1473
861fb55a
DJ
1474static void
1475mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1476 struct mips_elf_link_hash_entry *h)
b49e97c9 1477{
738e5348
RS
1478 /* Dynamic symbols must use the standard call interface, in case other
1479 objects try to call them. */
1480 if (h->fn_stub != NULL
1481 && h->root.dynindx != -1)
1482 {
1483 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1484 h->need_fn_stub = TRUE;
1485 }
1486
b49e97c9
TS
1487 if (h->fn_stub != NULL
1488 && ! h->need_fn_stub)
1489 {
1490 /* We don't need the fn_stub; the only references to this symbol
1491 are 16 bit calls. Clobber the size to 0 to prevent it from
1492 being included in the link. */
eea6121a 1493 h->fn_stub->size = 0;
b49e97c9
TS
1494 h->fn_stub->flags &= ~SEC_RELOC;
1495 h->fn_stub->reloc_count = 0;
1496 h->fn_stub->flags |= SEC_EXCLUDE;
1497 }
1498
1499 if (h->call_stub != NULL
30c09090 1500 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1501 {
1502 /* We don't need the call_stub; this is a 16 bit function, so
1503 calls from other 16 bit functions are OK. Clobber the size
1504 to 0 to prevent it from being included in the link. */
eea6121a 1505 h->call_stub->size = 0;
b49e97c9
TS
1506 h->call_stub->flags &= ~SEC_RELOC;
1507 h->call_stub->reloc_count = 0;
1508 h->call_stub->flags |= SEC_EXCLUDE;
1509 }
1510
1511 if (h->call_fp_stub != NULL
30c09090 1512 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1513 {
1514 /* We don't need the call_stub; this is a 16 bit function, so
1515 calls from other 16 bit functions are OK. Clobber the size
1516 to 0 to prevent it from being included in the link. */
eea6121a 1517 h->call_fp_stub->size = 0;
b49e97c9
TS
1518 h->call_fp_stub->flags &= ~SEC_RELOC;
1519 h->call_fp_stub->reloc_count = 0;
1520 h->call_fp_stub->flags |= SEC_EXCLUDE;
1521 }
861fb55a
DJ
1522}
1523
1524/* Hashtable callbacks for mips_elf_la25_stubs. */
1525
1526static hashval_t
1527mips_elf_la25_stub_hash (const void *entry_)
1528{
1529 const struct mips_elf_la25_stub *entry;
1530
1531 entry = (struct mips_elf_la25_stub *) entry_;
1532 return entry->h->root.root.u.def.section->id
1533 + entry->h->root.root.u.def.value;
1534}
1535
1536static int
1537mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1538{
1539 const struct mips_elf_la25_stub *entry1, *entry2;
1540
1541 entry1 = (struct mips_elf_la25_stub *) entry1_;
1542 entry2 = (struct mips_elf_la25_stub *) entry2_;
1543 return ((entry1->h->root.root.u.def.section
1544 == entry2->h->root.root.u.def.section)
1545 && (entry1->h->root.root.u.def.value
1546 == entry2->h->root.root.u.def.value));
1547}
1548
1549/* Called by the linker to set up the la25 stub-creation code. FN is
1550 the linker's implementation of add_stub_function. Return true on
1551 success. */
1552
1553bfd_boolean
1554_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1555 asection *(*fn) (const char *, asection *,
1556 asection *))
1557{
1558 struct mips_elf_link_hash_table *htab;
1559
1560 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1561 if (htab == NULL)
1562 return FALSE;
1563
861fb55a
DJ
1564 htab->add_stub_section = fn;
1565 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1566 mips_elf_la25_stub_eq, NULL);
1567 if (htab->la25_stubs == NULL)
1568 return FALSE;
1569
1570 return TRUE;
1571}
1572
1573/* Return true if H is a locally-defined PIC function, in the sense
1574 that it might need $25 to be valid on entry. Note that MIPS16
1575 functions never need $25 to be valid on entry; they set up $gp
1576 using PC-relative instructions instead. */
1577
1578static bfd_boolean
1579mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1580{
1581 return ((h->root.root.type == bfd_link_hash_defined
1582 || h->root.root.type == bfd_link_hash_defweak)
1583 && h->root.def_regular
1584 && !bfd_is_abs_section (h->root.root.u.def.section)
1585 && !ELF_ST_IS_MIPS16 (h->root.other)
1586 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1587 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1588}
1589
1590/* STUB describes an la25 stub that we have decided to implement
1591 by inserting an LUI/ADDIU pair before the target function.
1592 Create the section and redirect the function symbol to it. */
1593
1594static bfd_boolean
1595mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1596 struct bfd_link_info *info)
1597{
1598 struct mips_elf_link_hash_table *htab;
1599 char *name;
1600 asection *s, *input_section;
1601 unsigned int align;
1602
1603 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1604 if (htab == NULL)
1605 return FALSE;
861fb55a
DJ
1606
1607 /* Create a unique name for the new section. */
1608 name = bfd_malloc (11 + sizeof (".text.stub."));
1609 if (name == NULL)
1610 return FALSE;
1611 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1612
1613 /* Create the section. */
1614 input_section = stub->h->root.root.u.def.section;
1615 s = htab->add_stub_section (name, input_section,
1616 input_section->output_section);
1617 if (s == NULL)
1618 return FALSE;
1619
1620 /* Make sure that any padding goes before the stub. */
1621 align = input_section->alignment_power;
1622 if (!bfd_set_section_alignment (s->owner, s, align))
1623 return FALSE;
1624 if (align > 3)
1625 s->size = (1 << align) - 8;
1626
1627 /* Create a symbol for the stub. */
1628 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1629 stub->stub_section = s;
1630 stub->offset = s->size;
1631
1632 /* Allocate room for it. */
1633 s->size += 8;
1634 return TRUE;
1635}
1636
1637/* STUB describes an la25 stub that we have decided to implement
1638 with a separate trampoline. Allocate room for it and redirect
1639 the function symbol to it. */
1640
1641static bfd_boolean
1642mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1643 struct bfd_link_info *info)
1644{
1645 struct mips_elf_link_hash_table *htab;
1646 asection *s;
1647
1648 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1649 if (htab == NULL)
1650 return FALSE;
861fb55a
DJ
1651
1652 /* Create a trampoline section, if we haven't already. */
1653 s = htab->strampoline;
1654 if (s == NULL)
1655 {
1656 asection *input_section = stub->h->root.root.u.def.section;
1657 s = htab->add_stub_section (".text", NULL,
1658 input_section->output_section);
1659 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1660 return FALSE;
1661 htab->strampoline = s;
1662 }
1663
1664 /* Create a symbol for the stub. */
1665 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1666 stub->stub_section = s;
1667 stub->offset = s->size;
1668
1669 /* Allocate room for it. */
1670 s->size += 16;
1671 return TRUE;
1672}
1673
1674/* H describes a symbol that needs an la25 stub. Make sure that an
1675 appropriate stub exists and point H at it. */
1676
1677static bfd_boolean
1678mips_elf_add_la25_stub (struct bfd_link_info *info,
1679 struct mips_elf_link_hash_entry *h)
1680{
1681 struct mips_elf_link_hash_table *htab;
1682 struct mips_elf_la25_stub search, *stub;
1683 bfd_boolean use_trampoline_p;
1684 asection *s;
1685 bfd_vma value;
1686 void **slot;
1687
1688 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1689 of the section and if we would need no more than 2 nops. */
1690 s = h->root.root.u.def.section;
1691 value = h->root.root.u.def.value;
1692 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1693
1694 /* Describe the stub we want. */
1695 search.stub_section = NULL;
1696 search.offset = 0;
1697 search.h = h;
1698
1699 /* See if we've already created an equivalent stub. */
1700 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1701 if (htab == NULL)
1702 return FALSE;
1703
861fb55a
DJ
1704 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1705 if (slot == NULL)
1706 return FALSE;
1707
1708 stub = (struct mips_elf_la25_stub *) *slot;
1709 if (stub != NULL)
1710 {
1711 /* We can reuse the existing stub. */
1712 h->la25_stub = stub;
1713 return TRUE;
1714 }
1715
1716 /* Create a permanent copy of ENTRY and add it to the hash table. */
1717 stub = bfd_malloc (sizeof (search));
1718 if (stub == NULL)
1719 return FALSE;
1720 *stub = search;
1721 *slot = stub;
1722
1723 h->la25_stub = stub;
1724 return (use_trampoline_p
1725 ? mips_elf_add_la25_trampoline (stub, info)
1726 : mips_elf_add_la25_intro (stub, info));
1727}
1728
1729/* A mips_elf_link_hash_traverse callback that is called before sizing
1730 sections. DATA points to a mips_htab_traverse_info structure. */
1731
1732static bfd_boolean
1733mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1734{
1735 struct mips_htab_traverse_info *hti;
1736
1737 hti = (struct mips_htab_traverse_info *) data;
861fb55a
DJ
1738 if (!hti->info->relocatable)
1739 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1740
861fb55a
DJ
1741 if (mips_elf_local_pic_function_p (h))
1742 {
ba85c43e
NC
1743 /* PR 12845: If H is in a section that has been garbage
1744 collected it will have its output section set to *ABS*. */
1745 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1746 return TRUE;
1747
861fb55a
DJ
1748 /* H is a function that might need $25 to be valid on entry.
1749 If we're creating a non-PIC relocatable object, mark H as
1750 being PIC. If we're creating a non-relocatable object with
1751 non-PIC branches and jumps to H, make sure that H has an la25
1752 stub. */
1753 if (hti->info->relocatable)
1754 {
1755 if (!PIC_OBJECT_P (hti->output_bfd))
1756 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1757 }
1758 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1759 {
1760 hti->error = TRUE;
1761 return FALSE;
1762 }
1763 }
b34976b6 1764 return TRUE;
b49e97c9
TS
1765}
1766\f
d6f16593
MR
1767/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1768 Most mips16 instructions are 16 bits, but these instructions
1769 are 32 bits.
1770
1771 The format of these instructions is:
1772
1773 +--------------+--------------------------------+
1774 | JALX | X| Imm 20:16 | Imm 25:21 |
1775 +--------------+--------------------------------+
1776 | Immediate 15:0 |
1777 +-----------------------------------------------+
1778
1779 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1780 Note that the immediate value in the first word is swapped.
1781
1782 When producing a relocatable object file, R_MIPS16_26 is
1783 handled mostly like R_MIPS_26. In particular, the addend is
1784 stored as a straight 26-bit value in a 32-bit instruction.
1785 (gas makes life simpler for itself by never adjusting a
1786 R_MIPS16_26 reloc to be against a section, so the addend is
1787 always zero). However, the 32 bit instruction is stored as 2
1788 16-bit values, rather than a single 32-bit value. In a
1789 big-endian file, the result is the same; in a little-endian
1790 file, the two 16-bit halves of the 32 bit value are swapped.
1791 This is so that a disassembler can recognize the jal
1792 instruction.
1793
1794 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1795 instruction stored as two 16-bit values. The addend A is the
1796 contents of the targ26 field. The calculation is the same as
1797 R_MIPS_26. When storing the calculated value, reorder the
1798 immediate value as shown above, and don't forget to store the
1799 value as two 16-bit values.
1800
1801 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1802 defined as
1803
1804 big-endian:
1805 +--------+----------------------+
1806 | | |
1807 | | targ26-16 |
1808 |31 26|25 0|
1809 +--------+----------------------+
1810
1811 little-endian:
1812 +----------+------+-------------+
1813 | | | |
1814 | sub1 | | sub2 |
1815 |0 9|10 15|16 31|
1816 +----------+--------------------+
1817 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1818 ((sub1 << 16) | sub2)).
1819
1820 When producing a relocatable object file, the calculation is
1821 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1822 When producing a fully linked file, the calculation is
1823 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1824 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1825
738e5348
RS
1826 The table below lists the other MIPS16 instruction relocations.
1827 Each one is calculated in the same way as the non-MIPS16 relocation
1828 given on the right, but using the extended MIPS16 layout of 16-bit
1829 immediate fields:
1830
1831 R_MIPS16_GPREL R_MIPS_GPREL16
1832 R_MIPS16_GOT16 R_MIPS_GOT16
1833 R_MIPS16_CALL16 R_MIPS_CALL16
1834 R_MIPS16_HI16 R_MIPS_HI16
1835 R_MIPS16_LO16 R_MIPS_LO16
1836
1837 A typical instruction will have a format like this:
d6f16593
MR
1838
1839 +--------------+--------------------------------+
1840 | EXTEND | Imm 10:5 | Imm 15:11 |
1841 +--------------+--------------------------------+
1842 | Major | rx | ry | Imm 4:0 |
1843 +--------------+--------------------------------+
1844
1845 EXTEND is the five bit value 11110. Major is the instruction
1846 opcode.
1847
738e5348
RS
1848 All we need to do here is shuffle the bits appropriately.
1849 As above, the two 16-bit halves must be swapped on a
1850 little-endian system. */
1851
1852static inline bfd_boolean
1853mips16_reloc_p (int r_type)
1854{
1855 switch (r_type)
1856 {
1857 case R_MIPS16_26:
1858 case R_MIPS16_GPREL:
1859 case R_MIPS16_GOT16:
1860 case R_MIPS16_CALL16:
1861 case R_MIPS16_HI16:
1862 case R_MIPS16_LO16:
1863 return TRUE;
1864
1865 default:
1866 return FALSE;
1867 }
1868}
1869
df58fc94
RS
1870/* Check if a microMIPS reloc. */
1871
1872static inline bfd_boolean
1873micromips_reloc_p (unsigned int r_type)
1874{
1875 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
1876}
1877
1878/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
1879 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
1880 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
1881
1882static inline bfd_boolean
1883micromips_reloc_shuffle_p (unsigned int r_type)
1884{
1885 return (micromips_reloc_p (r_type)
1886 && r_type != R_MICROMIPS_PC7_S1
1887 && r_type != R_MICROMIPS_PC10_S1);
1888}
1889
738e5348
RS
1890static inline bfd_boolean
1891got16_reloc_p (int r_type)
1892{
df58fc94
RS
1893 return (r_type == R_MIPS_GOT16
1894 || r_type == R_MIPS16_GOT16
1895 || r_type == R_MICROMIPS_GOT16);
738e5348
RS
1896}
1897
1898static inline bfd_boolean
1899call16_reloc_p (int r_type)
1900{
df58fc94
RS
1901 return (r_type == R_MIPS_CALL16
1902 || r_type == R_MIPS16_CALL16
1903 || r_type == R_MICROMIPS_CALL16);
1904}
1905
1906static inline bfd_boolean
1907got_disp_reloc_p (unsigned int r_type)
1908{
1909 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
1910}
1911
1912static inline bfd_boolean
1913got_page_reloc_p (unsigned int r_type)
1914{
1915 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
1916}
1917
1918static inline bfd_boolean
1919got_ofst_reloc_p (unsigned int r_type)
1920{
1921 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
1922}
1923
1924static inline bfd_boolean
1925got_hi16_reloc_p (unsigned int r_type)
1926{
1927 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
1928}
1929
1930static inline bfd_boolean
1931got_lo16_reloc_p (unsigned int r_type)
1932{
1933 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
1934}
1935
1936static inline bfd_boolean
1937call_hi16_reloc_p (unsigned int r_type)
1938{
1939 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
1940}
1941
1942static inline bfd_boolean
1943call_lo16_reloc_p (unsigned int r_type)
1944{
1945 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
738e5348
RS
1946}
1947
1948static inline bfd_boolean
1949hi16_reloc_p (int r_type)
1950{
df58fc94
RS
1951 return (r_type == R_MIPS_HI16
1952 || r_type == R_MIPS16_HI16
1953 || r_type == R_MICROMIPS_HI16);
738e5348 1954}
d6f16593 1955
738e5348
RS
1956static inline bfd_boolean
1957lo16_reloc_p (int r_type)
1958{
df58fc94
RS
1959 return (r_type == R_MIPS_LO16
1960 || r_type == R_MIPS16_LO16
1961 || r_type == R_MICROMIPS_LO16);
738e5348
RS
1962}
1963
1964static inline bfd_boolean
1965mips16_call_reloc_p (int r_type)
1966{
1967 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1968}
d6f16593 1969
38a7df63
CF
1970static inline bfd_boolean
1971jal_reloc_p (int r_type)
1972{
df58fc94
RS
1973 return (r_type == R_MIPS_26
1974 || r_type == R_MIPS16_26
1975 || r_type == R_MICROMIPS_26_S1);
1976}
1977
1978static inline bfd_boolean
1979micromips_branch_reloc_p (int r_type)
1980{
1981 return (r_type == R_MICROMIPS_26_S1
1982 || r_type == R_MICROMIPS_PC16_S1
1983 || r_type == R_MICROMIPS_PC10_S1
1984 || r_type == R_MICROMIPS_PC7_S1);
1985}
1986
1987static inline bfd_boolean
1988tls_gd_reloc_p (unsigned int r_type)
1989{
1990 return r_type == R_MIPS_TLS_GD || r_type == R_MICROMIPS_TLS_GD;
1991}
1992
1993static inline bfd_boolean
1994tls_ldm_reloc_p (unsigned int r_type)
1995{
1996 return r_type == R_MIPS_TLS_LDM || r_type == R_MICROMIPS_TLS_LDM;
1997}
1998
1999static inline bfd_boolean
2000tls_gottprel_reloc_p (unsigned int r_type)
2001{
2002 return r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MICROMIPS_TLS_GOTTPREL;
38a7df63
CF
2003}
2004
d6f16593 2005void
df58fc94
RS
2006_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2007 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2008{
df58fc94 2009 bfd_vma first, second, val;
d6f16593 2010
df58fc94 2011 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2012 return;
2013
df58fc94
RS
2014 /* Pick up the first and second halfwords of the instruction. */
2015 first = bfd_get_16 (abfd, data);
2016 second = bfd_get_16 (abfd, data + 2);
2017 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2018 val = first << 16 | second;
2019 else if (r_type != R_MIPS16_26)
2020 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2021 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
d6f16593 2022 else
df58fc94
RS
2023 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2024 | ((first & 0x1f) << 21) | second);
d6f16593
MR
2025 bfd_put_32 (abfd, val, data);
2026}
2027
2028void
df58fc94
RS
2029_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2030 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2031{
df58fc94 2032 bfd_vma first, second, val;
d6f16593 2033
df58fc94 2034 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2035 return;
2036
2037 val = bfd_get_32 (abfd, data);
df58fc94 2038 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
d6f16593 2039 {
df58fc94
RS
2040 second = val & 0xffff;
2041 first = val >> 16;
2042 }
2043 else if (r_type != R_MIPS16_26)
2044 {
2045 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2046 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
d6f16593
MR
2047 }
2048 else
2049 {
df58fc94
RS
2050 second = val & 0xffff;
2051 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2052 | ((val >> 21) & 0x1f);
d6f16593 2053 }
df58fc94
RS
2054 bfd_put_16 (abfd, second, data + 2);
2055 bfd_put_16 (abfd, first, data);
d6f16593
MR
2056}
2057
b49e97c9 2058bfd_reloc_status_type
9719ad41
RS
2059_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2060 arelent *reloc_entry, asection *input_section,
2061 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
2062{
2063 bfd_vma relocation;
a7ebbfdf 2064 bfd_signed_vma val;
30ac9238 2065 bfd_reloc_status_type status;
b49e97c9
TS
2066
2067 if (bfd_is_com_section (symbol->section))
2068 relocation = 0;
2069 else
2070 relocation = symbol->value;
2071
2072 relocation += symbol->section->output_section->vma;
2073 relocation += symbol->section->output_offset;
2074
07515404 2075 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
2076 return bfd_reloc_outofrange;
2077
b49e97c9 2078 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
2079 val = reloc_entry->addend;
2080
30ac9238 2081 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 2082
b49e97c9 2083 /* Adjust val for the final section location and GP value. If we
1049f94e 2084 are producing relocatable output, we don't want to do this for
b49e97c9 2085 an external symbol. */
1049f94e 2086 if (! relocatable
b49e97c9
TS
2087 || (symbol->flags & BSF_SECTION_SYM) != 0)
2088 val += relocation - gp;
2089
a7ebbfdf
TS
2090 if (reloc_entry->howto->partial_inplace)
2091 {
30ac9238
RS
2092 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2093 (bfd_byte *) data
2094 + reloc_entry->address);
2095 if (status != bfd_reloc_ok)
2096 return status;
a7ebbfdf
TS
2097 }
2098 else
2099 reloc_entry->addend = val;
b49e97c9 2100
1049f94e 2101 if (relocatable)
b49e97c9 2102 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2103
2104 return bfd_reloc_ok;
2105}
2106
2107/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2108 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2109 that contains the relocation field and DATA points to the start of
2110 INPUT_SECTION. */
2111
2112struct mips_hi16
2113{
2114 struct mips_hi16 *next;
2115 bfd_byte *data;
2116 asection *input_section;
2117 arelent rel;
2118};
2119
2120/* FIXME: This should not be a static variable. */
2121
2122static struct mips_hi16 *mips_hi16_list;
2123
2124/* A howto special_function for REL *HI16 relocations. We can only
2125 calculate the correct value once we've seen the partnering
2126 *LO16 relocation, so just save the information for later.
2127
2128 The ABI requires that the *LO16 immediately follow the *HI16.
2129 However, as a GNU extension, we permit an arbitrary number of
2130 *HI16s to be associated with a single *LO16. This significantly
2131 simplies the relocation handling in gcc. */
2132
2133bfd_reloc_status_type
2134_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2135 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2136 asection *input_section, bfd *output_bfd,
2137 char **error_message ATTRIBUTE_UNUSED)
2138{
2139 struct mips_hi16 *n;
2140
07515404 2141 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2142 return bfd_reloc_outofrange;
2143
2144 n = bfd_malloc (sizeof *n);
2145 if (n == NULL)
2146 return bfd_reloc_outofrange;
2147
2148 n->next = mips_hi16_list;
2149 n->data = data;
2150 n->input_section = input_section;
2151 n->rel = *reloc_entry;
2152 mips_hi16_list = n;
2153
2154 if (output_bfd != NULL)
2155 reloc_entry->address += input_section->output_offset;
2156
2157 return bfd_reloc_ok;
2158}
2159
738e5348 2160/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2161 like any other 16-bit relocation when applied to global symbols, but is
2162 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2163
2164bfd_reloc_status_type
2165_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2166 void *data, asection *input_section,
2167 bfd *output_bfd, char **error_message)
2168{
2169 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2170 || bfd_is_und_section (bfd_get_section (symbol))
2171 || bfd_is_com_section (bfd_get_section (symbol)))
2172 /* The relocation is against a global symbol. */
2173 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2174 input_section, output_bfd,
2175 error_message);
2176
2177 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2178 input_section, output_bfd, error_message);
2179}
2180
2181/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2182 is a straightforward 16 bit inplace relocation, but we must deal with
2183 any partnering high-part relocations as well. */
2184
2185bfd_reloc_status_type
2186_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2187 void *data, asection *input_section,
2188 bfd *output_bfd, char **error_message)
2189{
2190 bfd_vma vallo;
d6f16593 2191 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2192
07515404 2193 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2194 return bfd_reloc_outofrange;
2195
df58fc94 2196 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
d6f16593 2197 location);
df58fc94
RS
2198 vallo = bfd_get_32 (abfd, location);
2199 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2200 location);
d6f16593 2201
30ac9238
RS
2202 while (mips_hi16_list != NULL)
2203 {
2204 bfd_reloc_status_type ret;
2205 struct mips_hi16 *hi;
2206
2207 hi = mips_hi16_list;
2208
738e5348
RS
2209 /* R_MIPS*_GOT16 relocations are something of a special case. We
2210 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2211 relocation (with a rightshift of 16). However, since GOT16
2212 relocations can also be used with global symbols, their howto
2213 has a rightshift of 0. */
2214 if (hi->rel.howto->type == R_MIPS_GOT16)
2215 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2216 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2217 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
df58fc94
RS
2218 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2219 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
30ac9238
RS
2220
2221 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2222 carry or borrow will induce a change of +1 or -1 in the high part. */
2223 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2224
30ac9238
RS
2225 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2226 hi->input_section, output_bfd,
2227 error_message);
2228 if (ret != bfd_reloc_ok)
2229 return ret;
2230
2231 mips_hi16_list = hi->next;
2232 free (hi);
2233 }
2234
2235 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2236 input_section, output_bfd,
2237 error_message);
2238}
2239
2240/* A generic howto special_function. This calculates and installs the
2241 relocation itself, thus avoiding the oft-discussed problems in
2242 bfd_perform_relocation and bfd_install_relocation. */
2243
2244bfd_reloc_status_type
2245_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2246 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2247 asection *input_section, bfd *output_bfd,
2248 char **error_message ATTRIBUTE_UNUSED)
2249{
2250 bfd_signed_vma val;
2251 bfd_reloc_status_type status;
2252 bfd_boolean relocatable;
2253
2254 relocatable = (output_bfd != NULL);
2255
07515404 2256 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2257 return bfd_reloc_outofrange;
2258
2259 /* Build up the field adjustment in VAL. */
2260 val = 0;
2261 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2262 {
2263 /* Either we're calculating the final field value or we have a
2264 relocation against a section symbol. Add in the section's
2265 offset or address. */
2266 val += symbol->section->output_section->vma;
2267 val += symbol->section->output_offset;
2268 }
2269
2270 if (!relocatable)
2271 {
2272 /* We're calculating the final field value. Add in the symbol's value
2273 and, if pc-relative, subtract the address of the field itself. */
2274 val += symbol->value;
2275 if (reloc_entry->howto->pc_relative)
2276 {
2277 val -= input_section->output_section->vma;
2278 val -= input_section->output_offset;
2279 val -= reloc_entry->address;
2280 }
2281 }
2282
2283 /* VAL is now the final adjustment. If we're keeping this relocation
2284 in the output file, and if the relocation uses a separate addend,
2285 we just need to add VAL to that addend. Otherwise we need to add
2286 VAL to the relocation field itself. */
2287 if (relocatable && !reloc_entry->howto->partial_inplace)
2288 reloc_entry->addend += val;
2289 else
2290 {
d6f16593
MR
2291 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2292
30ac9238
RS
2293 /* Add in the separate addend, if any. */
2294 val += reloc_entry->addend;
2295
2296 /* Add VAL to the relocation field. */
df58fc94
RS
2297 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2298 location);
30ac9238 2299 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593 2300 location);
df58fc94
RS
2301 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2302 location);
d6f16593 2303
30ac9238
RS
2304 if (status != bfd_reloc_ok)
2305 return status;
2306 }
2307
2308 if (relocatable)
2309 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2310
2311 return bfd_reloc_ok;
2312}
2313\f
2314/* Swap an entry in a .gptab section. Note that these routines rely
2315 on the equivalence of the two elements of the union. */
2316
2317static void
9719ad41
RS
2318bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2319 Elf32_gptab *in)
b49e97c9
TS
2320{
2321 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2322 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2323}
2324
2325static void
9719ad41
RS
2326bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2327 Elf32_External_gptab *ex)
b49e97c9
TS
2328{
2329 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2330 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2331}
2332
2333static void
9719ad41
RS
2334bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2335 Elf32_External_compact_rel *ex)
b49e97c9
TS
2336{
2337 H_PUT_32 (abfd, in->id1, ex->id1);
2338 H_PUT_32 (abfd, in->num, ex->num);
2339 H_PUT_32 (abfd, in->id2, ex->id2);
2340 H_PUT_32 (abfd, in->offset, ex->offset);
2341 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2342 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2343}
2344
2345static void
9719ad41
RS
2346bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2347 Elf32_External_crinfo *ex)
b49e97c9
TS
2348{
2349 unsigned long l;
2350
2351 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2352 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2353 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2354 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2355 H_PUT_32 (abfd, l, ex->info);
2356 H_PUT_32 (abfd, in->konst, ex->konst);
2357 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2358}
b49e97c9
TS
2359\f
2360/* A .reginfo section holds a single Elf32_RegInfo structure. These
2361 routines swap this structure in and out. They are used outside of
2362 BFD, so they are globally visible. */
2363
2364void
9719ad41
RS
2365bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2366 Elf32_RegInfo *in)
b49e97c9
TS
2367{
2368 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2369 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2370 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2371 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2372 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2373 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2374}
2375
2376void
9719ad41
RS
2377bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2378 Elf32_External_RegInfo *ex)
b49e97c9
TS
2379{
2380 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2381 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2382 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2383 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2384 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2385 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2386}
2387
2388/* In the 64 bit ABI, the .MIPS.options section holds register
2389 information in an Elf64_Reginfo structure. These routines swap
2390 them in and out. They are globally visible because they are used
2391 outside of BFD. These routines are here so that gas can call them
2392 without worrying about whether the 64 bit ABI has been included. */
2393
2394void
9719ad41
RS
2395bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2396 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2397{
2398 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2399 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2400 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2401 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2402 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2403 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2404 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2405}
2406
2407void
9719ad41
RS
2408bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2409 Elf64_External_RegInfo *ex)
b49e97c9
TS
2410{
2411 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2412 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2413 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2414 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2415 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2416 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2417 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2418}
2419
2420/* Swap in an options header. */
2421
2422void
9719ad41
RS
2423bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2424 Elf_Internal_Options *in)
b49e97c9
TS
2425{
2426 in->kind = H_GET_8 (abfd, ex->kind);
2427 in->size = H_GET_8 (abfd, ex->size);
2428 in->section = H_GET_16 (abfd, ex->section);
2429 in->info = H_GET_32 (abfd, ex->info);
2430}
2431
2432/* Swap out an options header. */
2433
2434void
9719ad41
RS
2435bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2436 Elf_External_Options *ex)
b49e97c9
TS
2437{
2438 H_PUT_8 (abfd, in->kind, ex->kind);
2439 H_PUT_8 (abfd, in->size, ex->size);
2440 H_PUT_16 (abfd, in->section, ex->section);
2441 H_PUT_32 (abfd, in->info, ex->info);
2442}
2443\f
2444/* This function is called via qsort() to sort the dynamic relocation
2445 entries by increasing r_symndx value. */
2446
2447static int
9719ad41 2448sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2449{
947216bf
AM
2450 Elf_Internal_Rela int_reloc1;
2451 Elf_Internal_Rela int_reloc2;
6870500c 2452 int diff;
b49e97c9 2453
947216bf
AM
2454 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2455 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2456
6870500c
RS
2457 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2458 if (diff != 0)
2459 return diff;
2460
2461 if (int_reloc1.r_offset < int_reloc2.r_offset)
2462 return -1;
2463 if (int_reloc1.r_offset > int_reloc2.r_offset)
2464 return 1;
2465 return 0;
b49e97c9
TS
2466}
2467
f4416af6
AO
2468/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2469
2470static int
7e3102a7
AM
2471sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2472 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2473{
7e3102a7 2474#ifdef BFD64
f4416af6
AO
2475 Elf_Internal_Rela int_reloc1[3];
2476 Elf_Internal_Rela int_reloc2[3];
2477
2478 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2479 (reldyn_sorting_bfd, arg1, int_reloc1);
2480 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2481 (reldyn_sorting_bfd, arg2, int_reloc2);
2482
6870500c
RS
2483 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2484 return -1;
2485 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2486 return 1;
2487
2488 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2489 return -1;
2490 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2491 return 1;
2492 return 0;
7e3102a7
AM
2493#else
2494 abort ();
2495#endif
f4416af6
AO
2496}
2497
2498
b49e97c9
TS
2499/* This routine is used to write out ECOFF debugging external symbol
2500 information. It is called via mips_elf_link_hash_traverse. The
2501 ECOFF external symbol information must match the ELF external
2502 symbol information. Unfortunately, at this point we don't know
2503 whether a symbol is required by reloc information, so the two
2504 tables may wind up being different. We must sort out the external
2505 symbol information before we can set the final size of the .mdebug
2506 section, and we must set the size of the .mdebug section before we
2507 can relocate any sections, and we can't know which symbols are
2508 required by relocation until we relocate the sections.
2509 Fortunately, it is relatively unlikely that any symbol will be
2510 stripped but required by a reloc. In particular, it can not happen
2511 when generating a final executable. */
2512
b34976b6 2513static bfd_boolean
9719ad41 2514mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2515{
9719ad41 2516 struct extsym_info *einfo = data;
b34976b6 2517 bfd_boolean strip;
b49e97c9
TS
2518 asection *sec, *output_section;
2519
b49e97c9 2520 if (h->root.indx == -2)
b34976b6 2521 strip = FALSE;
f5385ebf 2522 else if ((h->root.def_dynamic
77cfaee6
AM
2523 || h->root.ref_dynamic
2524 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2525 && !h->root.def_regular
2526 && !h->root.ref_regular)
b34976b6 2527 strip = TRUE;
b49e97c9
TS
2528 else if (einfo->info->strip == strip_all
2529 || (einfo->info->strip == strip_some
2530 && bfd_hash_lookup (einfo->info->keep_hash,
2531 h->root.root.root.string,
b34976b6
AM
2532 FALSE, FALSE) == NULL))
2533 strip = TRUE;
b49e97c9 2534 else
b34976b6 2535 strip = FALSE;
b49e97c9
TS
2536
2537 if (strip)
b34976b6 2538 return TRUE;
b49e97c9
TS
2539
2540 if (h->esym.ifd == -2)
2541 {
2542 h->esym.jmptbl = 0;
2543 h->esym.cobol_main = 0;
2544 h->esym.weakext = 0;
2545 h->esym.reserved = 0;
2546 h->esym.ifd = ifdNil;
2547 h->esym.asym.value = 0;
2548 h->esym.asym.st = stGlobal;
2549
2550 if (h->root.root.type == bfd_link_hash_undefined
2551 || h->root.root.type == bfd_link_hash_undefweak)
2552 {
2553 const char *name;
2554
2555 /* Use undefined class. Also, set class and type for some
2556 special symbols. */
2557 name = h->root.root.root.string;
2558 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2559 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2560 {
2561 h->esym.asym.sc = scData;
2562 h->esym.asym.st = stLabel;
2563 h->esym.asym.value = 0;
2564 }
2565 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2566 {
2567 h->esym.asym.sc = scAbs;
2568 h->esym.asym.st = stLabel;
2569 h->esym.asym.value =
2570 mips_elf_hash_table (einfo->info)->procedure_count;
2571 }
4a14403c 2572 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2573 {
2574 h->esym.asym.sc = scAbs;
2575 h->esym.asym.st = stLabel;
2576 h->esym.asym.value = elf_gp (einfo->abfd);
2577 }
2578 else
2579 h->esym.asym.sc = scUndefined;
2580 }
2581 else if (h->root.root.type != bfd_link_hash_defined
2582 && h->root.root.type != bfd_link_hash_defweak)
2583 h->esym.asym.sc = scAbs;
2584 else
2585 {
2586 const char *name;
2587
2588 sec = h->root.root.u.def.section;
2589 output_section = sec->output_section;
2590
2591 /* When making a shared library and symbol h is the one from
2592 the another shared library, OUTPUT_SECTION may be null. */
2593 if (output_section == NULL)
2594 h->esym.asym.sc = scUndefined;
2595 else
2596 {
2597 name = bfd_section_name (output_section->owner, output_section);
2598
2599 if (strcmp (name, ".text") == 0)
2600 h->esym.asym.sc = scText;
2601 else if (strcmp (name, ".data") == 0)
2602 h->esym.asym.sc = scData;
2603 else if (strcmp (name, ".sdata") == 0)
2604 h->esym.asym.sc = scSData;
2605 else if (strcmp (name, ".rodata") == 0
2606 || strcmp (name, ".rdata") == 0)
2607 h->esym.asym.sc = scRData;
2608 else if (strcmp (name, ".bss") == 0)
2609 h->esym.asym.sc = scBss;
2610 else if (strcmp (name, ".sbss") == 0)
2611 h->esym.asym.sc = scSBss;
2612 else if (strcmp (name, ".init") == 0)
2613 h->esym.asym.sc = scInit;
2614 else if (strcmp (name, ".fini") == 0)
2615 h->esym.asym.sc = scFini;
2616 else
2617 h->esym.asym.sc = scAbs;
2618 }
2619 }
2620
2621 h->esym.asym.reserved = 0;
2622 h->esym.asym.index = indexNil;
2623 }
2624
2625 if (h->root.root.type == bfd_link_hash_common)
2626 h->esym.asym.value = h->root.root.u.c.size;
2627 else if (h->root.root.type == bfd_link_hash_defined
2628 || h->root.root.type == bfd_link_hash_defweak)
2629 {
2630 if (h->esym.asym.sc == scCommon)
2631 h->esym.asym.sc = scBss;
2632 else if (h->esym.asym.sc == scSCommon)
2633 h->esym.asym.sc = scSBss;
2634
2635 sec = h->root.root.u.def.section;
2636 output_section = sec->output_section;
2637 if (output_section != NULL)
2638 h->esym.asym.value = (h->root.root.u.def.value
2639 + sec->output_offset
2640 + output_section->vma);
2641 else
2642 h->esym.asym.value = 0;
2643 }
33bb52fb 2644 else
b49e97c9
TS
2645 {
2646 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2647
2648 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2649 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2650
33bb52fb 2651 if (hd->needs_lazy_stub)
b49e97c9
TS
2652 {
2653 /* Set type and value for a symbol with a function stub. */
2654 h->esym.asym.st = stProc;
2655 sec = hd->root.root.u.def.section;
2656 if (sec == NULL)
2657 h->esym.asym.value = 0;
2658 else
2659 {
2660 output_section = sec->output_section;
2661 if (output_section != NULL)
2662 h->esym.asym.value = (hd->root.plt.offset
2663 + sec->output_offset
2664 + output_section->vma);
2665 else
2666 h->esym.asym.value = 0;
2667 }
b49e97c9
TS
2668 }
2669 }
2670
2671 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2672 h->root.root.root.string,
2673 &h->esym))
2674 {
b34976b6
AM
2675 einfo->failed = TRUE;
2676 return FALSE;
b49e97c9
TS
2677 }
2678
b34976b6 2679 return TRUE;
b49e97c9
TS
2680}
2681
2682/* A comparison routine used to sort .gptab entries. */
2683
2684static int
9719ad41 2685gptab_compare (const void *p1, const void *p2)
b49e97c9 2686{
9719ad41
RS
2687 const Elf32_gptab *a1 = p1;
2688 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2689
2690 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2691}
2692\f
b15e6682 2693/* Functions to manage the got entry hash table. */
f4416af6
AO
2694
2695/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2696 hash number. */
2697
2698static INLINE hashval_t
9719ad41 2699mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2700{
2701#ifdef BFD64
2702 return addr + (addr >> 32);
2703#else
2704 return addr;
2705#endif
2706}
2707
2708/* got_entries only match if they're identical, except for gotidx, so
2709 use all fields to compute the hash, and compare the appropriate
2710 union members. */
2711
b15e6682 2712static hashval_t
9719ad41 2713mips_elf_got_entry_hash (const void *entry_)
b15e6682
AO
2714{
2715 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2716
38985a1c 2717 return entry->symndx
0f20cc35 2718 + ((entry->tls_type & GOT_TLS_LDM) << 17)
f4416af6 2719 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
38985a1c
AO
2720 : entry->abfd->id
2721 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2722 : entry->d.h->root.root.root.hash));
b15e6682
AO
2723}
2724
2725static int
9719ad41 2726mips_elf_got_entry_eq (const void *entry1, const void *entry2)
b15e6682
AO
2727{
2728 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2729 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2730
0f20cc35
DJ
2731 /* An LDM entry can only match another LDM entry. */
2732 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2733 return 0;
2734
b15e6682 2735 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
f4416af6
AO
2736 && (! e1->abfd ? e1->d.address == e2->d.address
2737 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2738 : e1->d.h == e2->d.h);
2739}
2740
2741/* multi_got_entries are still a match in the case of global objects,
2742 even if the input bfd in which they're referenced differs, so the
2743 hash computation and compare functions are adjusted
2744 accordingly. */
2745
2746static hashval_t
9719ad41 2747mips_elf_multi_got_entry_hash (const void *entry_)
f4416af6
AO
2748{
2749 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2750
2751 return entry->symndx
2752 + (! entry->abfd
2753 ? mips_elf_hash_bfd_vma (entry->d.address)
2754 : entry->symndx >= 0
0f20cc35
DJ
2755 ? ((entry->tls_type & GOT_TLS_LDM)
2756 ? (GOT_TLS_LDM << 17)
2757 : (entry->abfd->id
2758 + mips_elf_hash_bfd_vma (entry->d.addend)))
f4416af6
AO
2759 : entry->d.h->root.root.root.hash);
2760}
2761
2762static int
9719ad41 2763mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2764{
2765 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2766 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2767
0f20cc35
DJ
2768 /* Any two LDM entries match. */
2769 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2770 return 1;
2771
2772 /* Nothing else matches an LDM entry. */
2773 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2774 return 0;
2775
f4416af6
AO
2776 return e1->symndx == e2->symndx
2777 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2778 : e1->abfd == NULL || e2->abfd == NULL
2779 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2780 : e1->d.h == e2->d.h);
b15e6682 2781}
c224138d
RS
2782
2783static hashval_t
2784mips_got_page_entry_hash (const void *entry_)
2785{
2786 const struct mips_got_page_entry *entry;
2787
2788 entry = (const struct mips_got_page_entry *) entry_;
2789 return entry->abfd->id + entry->symndx;
2790}
2791
2792static int
2793mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2794{
2795 const struct mips_got_page_entry *entry1, *entry2;
2796
2797 entry1 = (const struct mips_got_page_entry *) entry1_;
2798 entry2 = (const struct mips_got_page_entry *) entry2_;
2799 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2800}
b15e6682 2801\f
0a44bf69
RS
2802/* Return the dynamic relocation section. If it doesn't exist, try to
2803 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2804 if creation fails. */
f4416af6
AO
2805
2806static asection *
0a44bf69 2807mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 2808{
0a44bf69 2809 const char *dname;
f4416af6 2810 asection *sreloc;
0a44bf69 2811 bfd *dynobj;
f4416af6 2812
0a44bf69
RS
2813 dname = MIPS_ELF_REL_DYN_NAME (info);
2814 dynobj = elf_hash_table (info)->dynobj;
f4416af6
AO
2815 sreloc = bfd_get_section_by_name (dynobj, dname);
2816 if (sreloc == NULL && create_p)
2817 {
3496cb2a
L
2818 sreloc = bfd_make_section_with_flags (dynobj, dname,
2819 (SEC_ALLOC
2820 | SEC_LOAD
2821 | SEC_HAS_CONTENTS
2822 | SEC_IN_MEMORY
2823 | SEC_LINKER_CREATED
2824 | SEC_READONLY));
f4416af6 2825 if (sreloc == NULL
f4416af6 2826 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 2827 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
2828 return NULL;
2829 }
2830 return sreloc;
2831}
2832
0f20cc35
DJ
2833/* Count the number of relocations needed for a TLS GOT entry, with
2834 access types from TLS_TYPE, and symbol H (or a local symbol if H
2835 is NULL). */
2836
2837static int
2838mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2839 struct elf_link_hash_entry *h)
2840{
2841 int indx = 0;
2842 int ret = 0;
2843 bfd_boolean need_relocs = FALSE;
2844 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2845
2846 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2847 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2848 indx = h->dynindx;
2849
2850 if ((info->shared || indx != 0)
2851 && (h == NULL
2852 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2853 || h->root.type != bfd_link_hash_undefweak))
2854 need_relocs = TRUE;
2855
2856 if (!need_relocs)
2857 return FALSE;
2858
2859 if (tls_type & GOT_TLS_GD)
2860 {
2861 ret++;
2862 if (indx != 0)
2863 ret++;
2864 }
2865
2866 if (tls_type & GOT_TLS_IE)
2867 ret++;
2868
2869 if ((tls_type & GOT_TLS_LDM) && info->shared)
2870 ret++;
2871
2872 return ret;
2873}
2874
2875/* Count the number of TLS relocations required for the GOT entry in
2876 ARG1, if it describes a local symbol. */
2877
2878static int
2879mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2880{
2881 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2882 struct mips_elf_count_tls_arg *arg = arg2;
2883
2884 if (entry->abfd != NULL && entry->symndx != -1)
2885 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2886
2887 return 1;
2888}
2889
2890/* Count the number of TLS GOT entries required for the global (or
2891 forced-local) symbol in ARG1. */
2892
2893static int
2894mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2895{
2896 struct mips_elf_link_hash_entry *hm
2897 = (struct mips_elf_link_hash_entry *) arg1;
2898 struct mips_elf_count_tls_arg *arg = arg2;
2899
2900 if (hm->tls_type & GOT_TLS_GD)
2901 arg->needed += 2;
2902 if (hm->tls_type & GOT_TLS_IE)
2903 arg->needed += 1;
2904
2905 return 1;
2906}
2907
2908/* Count the number of TLS relocations required for the global (or
2909 forced-local) symbol in ARG1. */
2910
2911static int
2912mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2913{
2914 struct mips_elf_link_hash_entry *hm
2915 = (struct mips_elf_link_hash_entry *) arg1;
2916 struct mips_elf_count_tls_arg *arg = arg2;
2917
2918 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2919
2920 return 1;
2921}
2922
2923/* Output a simple dynamic relocation into SRELOC. */
2924
2925static void
2926mips_elf_output_dynamic_relocation (bfd *output_bfd,
2927 asection *sreloc,
861fb55a 2928 unsigned long reloc_index,
0f20cc35
DJ
2929 unsigned long indx,
2930 int r_type,
2931 bfd_vma offset)
2932{
2933 Elf_Internal_Rela rel[3];
2934
2935 memset (rel, 0, sizeof (rel));
2936
2937 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2938 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2939
2940 if (ABI_64_P (output_bfd))
2941 {
2942 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2943 (output_bfd, &rel[0],
2944 (sreloc->contents
861fb55a 2945 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
2946 }
2947 else
2948 bfd_elf32_swap_reloc_out
2949 (output_bfd, &rel[0],
2950 (sreloc->contents
861fb55a 2951 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
2952}
2953
2954/* Initialize a set of TLS GOT entries for one symbol. */
2955
2956static void
2957mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2958 unsigned char *tls_type_p,
2959 struct bfd_link_info *info,
2960 struct mips_elf_link_hash_entry *h,
2961 bfd_vma value)
2962{
23cc69b6 2963 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
2964 int indx;
2965 asection *sreloc, *sgot;
2966 bfd_vma offset, offset2;
0f20cc35
DJ
2967 bfd_boolean need_relocs = FALSE;
2968
23cc69b6 2969 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
2970 if (htab == NULL)
2971 return;
2972
23cc69b6 2973 sgot = htab->sgot;
0f20cc35
DJ
2974
2975 indx = 0;
2976 if (h != NULL)
2977 {
2978 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2979
2980 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2981 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2982 indx = h->root.dynindx;
2983 }
2984
2985 if (*tls_type_p & GOT_TLS_DONE)
2986 return;
2987
2988 if ((info->shared || indx != 0)
2989 && (h == NULL
2990 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2991 || h->root.type != bfd_link_hash_undefweak))
2992 need_relocs = TRUE;
2993
2994 /* MINUS_ONE means the symbol is not defined in this object. It may not
2995 be defined at all; assume that the value doesn't matter in that
2996 case. Otherwise complain if we would use the value. */
2997 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2998 || h->root.root.type == bfd_link_hash_undefweak);
2999
3000 /* Emit necessary relocations. */
0a44bf69 3001 sreloc = mips_elf_rel_dyn_section (info, FALSE);
0f20cc35
DJ
3002
3003 /* General Dynamic. */
3004 if (*tls_type_p & GOT_TLS_GD)
3005 {
3006 offset = got_offset;
3007 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
3008
3009 if (need_relocs)
3010 {
3011 mips_elf_output_dynamic_relocation
861fb55a 3012 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3013 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3014 sgot->output_offset + sgot->output_section->vma + offset);
3015
3016 if (indx)
3017 mips_elf_output_dynamic_relocation
861fb55a 3018 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3019 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3020 sgot->output_offset + sgot->output_section->vma + offset2);
3021 else
3022 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3023 sgot->contents + offset2);
3024 }
3025 else
3026 {
3027 MIPS_ELF_PUT_WORD (abfd, 1,
3028 sgot->contents + offset);
3029 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3030 sgot->contents + offset2);
3031 }
3032
3033 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
3034 }
3035
3036 /* Initial Exec model. */
3037 if (*tls_type_p & GOT_TLS_IE)
3038 {
3039 offset = got_offset;
3040
3041 if (need_relocs)
3042 {
3043 if (indx == 0)
3044 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3045 sgot->contents + offset);
3046 else
3047 MIPS_ELF_PUT_WORD (abfd, 0,
3048 sgot->contents + offset);
3049
3050 mips_elf_output_dynamic_relocation
861fb55a 3051 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3052 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3053 sgot->output_offset + sgot->output_section->vma + offset);
3054 }
3055 else
3056 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3057 sgot->contents + offset);
3058 }
3059
3060 if (*tls_type_p & GOT_TLS_LDM)
3061 {
3062 /* The initial offset is zero, and the LD offsets will include the
3063 bias by DTP_OFFSET. */
3064 MIPS_ELF_PUT_WORD (abfd, 0,
3065 sgot->contents + got_offset
3066 + MIPS_ELF_GOT_SIZE (abfd));
3067
3068 if (!info->shared)
3069 MIPS_ELF_PUT_WORD (abfd, 1,
3070 sgot->contents + got_offset);
3071 else
3072 mips_elf_output_dynamic_relocation
861fb55a 3073 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3074 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3075 sgot->output_offset + sgot->output_section->vma + got_offset);
3076 }
3077
3078 *tls_type_p |= GOT_TLS_DONE;
3079}
3080
3081/* Return the GOT index to use for a relocation of type R_TYPE against
3082 a symbol accessed using TLS_TYPE models. The GOT entries for this
3083 symbol in this GOT start at GOT_INDEX. This function initializes the
3084 GOT entries and corresponding relocations. */
3085
3086static bfd_vma
3087mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
3088 int r_type, struct bfd_link_info *info,
3089 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
3090{
df58fc94
RS
3091 BFD_ASSERT (tls_gottprel_reloc_p (r_type)
3092 || tls_gd_reloc_p (r_type)
3093 || tls_ldm_reloc_p (r_type));
0f20cc35
DJ
3094
3095 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
3096
df58fc94 3097 if (tls_gottprel_reloc_p (r_type))
0f20cc35
DJ
3098 {
3099 BFD_ASSERT (*tls_type & GOT_TLS_IE);
3100 if (*tls_type & GOT_TLS_GD)
3101 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
3102 else
3103 return got_index;
3104 }
3105
df58fc94 3106 if (tls_gd_reloc_p (r_type))
0f20cc35
DJ
3107 {
3108 BFD_ASSERT (*tls_type & GOT_TLS_GD);
3109 return got_index;
3110 }
3111
df58fc94 3112 if (tls_ldm_reloc_p (r_type))
0f20cc35
DJ
3113 {
3114 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
3115 return got_index;
3116 }
3117
3118 return got_index;
3119}
3120
0a44bf69
RS
3121/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3122 for global symbol H. .got.plt comes before the GOT, so the offset
3123 will be negative. */
3124
3125static bfd_vma
3126mips_elf_gotplt_index (struct bfd_link_info *info,
3127 struct elf_link_hash_entry *h)
3128{
3129 bfd_vma plt_index, got_address, got_value;
3130 struct mips_elf_link_hash_table *htab;
3131
3132 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3133 BFD_ASSERT (htab != NULL);
3134
0a44bf69
RS
3135 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3136
861fb55a
DJ
3137 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3138 section starts with reserved entries. */
3139 BFD_ASSERT (htab->is_vxworks);
3140
0a44bf69
RS
3141 /* Calculate the index of the symbol's PLT entry. */
3142 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3143
3144 /* Calculate the address of the associated .got.plt entry. */
3145 got_address = (htab->sgotplt->output_section->vma
3146 + htab->sgotplt->output_offset
3147 + plt_index * 4);
3148
3149 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3150 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3151 + htab->root.hgot->root.u.def.section->output_offset
3152 + htab->root.hgot->root.u.def.value);
3153
3154 return got_address - got_value;
3155}
3156
5c18022e 3157/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3158 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3159 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3160 offset can be found. */
b49e97c9
TS
3161
3162static bfd_vma
9719ad41 3163mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3164 bfd_vma value, unsigned long r_symndx,
0f20cc35 3165 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3166{
a8028dd0 3167 struct mips_elf_link_hash_table *htab;
b15e6682 3168 struct mips_got_entry *entry;
b49e97c9 3169
a8028dd0 3170 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3171 BFD_ASSERT (htab != NULL);
3172
a8028dd0
RS
3173 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3174 r_symndx, h, r_type);
0f20cc35 3175 if (!entry)
b15e6682 3176 return MINUS_ONE;
0f20cc35
DJ
3177
3178 if (TLS_RELOC_P (r_type))
ead49a57 3179 {
a8028dd0 3180 if (entry->symndx == -1 && htab->got_info->next == NULL)
ead49a57
RS
3181 /* A type (3) entry in the single-GOT case. We use the symbol's
3182 hash table entry to track the index. */
3183 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3184 r_type, info, h, value);
3185 else
3186 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3187 r_type, info, h, value);
3188 }
0f20cc35
DJ
3189 else
3190 return entry->gotidx;
b49e97c9
TS
3191}
3192
3193/* Returns the GOT index for the global symbol indicated by H. */
3194
3195static bfd_vma
0f20cc35
DJ
3196mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3197 int r_type, struct bfd_link_info *info)
b49e97c9 3198{
a8028dd0 3199 struct mips_elf_link_hash_table *htab;
91d6fa6a 3200 bfd_vma got_index;
f4416af6 3201 struct mips_got_info *g, *gg;
d0c7ff07 3202 long global_got_dynindx = 0;
b49e97c9 3203
a8028dd0 3204 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3205 BFD_ASSERT (htab != NULL);
3206
a8028dd0 3207 gg = g = htab->got_info;
f4416af6
AO
3208 if (g->bfd2got && ibfd)
3209 {
3210 struct mips_got_entry e, *p;
143d77c5 3211
f4416af6
AO
3212 BFD_ASSERT (h->dynindx >= 0);
3213
3214 g = mips_elf_got_for_ibfd (g, ibfd);
0f20cc35 3215 if (g->next != gg || TLS_RELOC_P (r_type))
f4416af6
AO
3216 {
3217 e.abfd = ibfd;
3218 e.symndx = -1;
3219 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 3220 e.tls_type = 0;
f4416af6 3221
9719ad41 3222 p = htab_find (g->got_entries, &e);
f4416af6
AO
3223
3224 BFD_ASSERT (p->gotidx > 0);
0f20cc35
DJ
3225
3226 if (TLS_RELOC_P (r_type))
3227 {
3228 bfd_vma value = MINUS_ONE;
3229 if ((h->root.type == bfd_link_hash_defined
3230 || h->root.type == bfd_link_hash_defweak)
3231 && h->root.u.def.section->output_section)
3232 value = (h->root.u.def.value
3233 + h->root.u.def.section->output_offset
3234 + h->root.u.def.section->output_section->vma);
3235
3236 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3237 info, e.d.h, value);
3238 }
3239 else
3240 return p->gotidx;
f4416af6
AO
3241 }
3242 }
3243
3244 if (gg->global_gotsym != NULL)
3245 global_got_dynindx = gg->global_gotsym->dynindx;
b49e97c9 3246
0f20cc35
DJ
3247 if (TLS_RELOC_P (r_type))
3248 {
3249 struct mips_elf_link_hash_entry *hm
3250 = (struct mips_elf_link_hash_entry *) h;
3251 bfd_vma value = MINUS_ONE;
3252
3253 if ((h->root.type == bfd_link_hash_defined
3254 || h->root.type == bfd_link_hash_defweak)
3255 && h->root.u.def.section->output_section)
3256 value = (h->root.u.def.value
3257 + h->root.u.def.section->output_offset
3258 + h->root.u.def.section->output_section->vma);
3259
91d6fa6a
NC
3260 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3261 r_type, info, hm, value);
0f20cc35
DJ
3262 }
3263 else
3264 {
3265 /* Once we determine the global GOT entry with the lowest dynamic
3266 symbol table index, we must put all dynamic symbols with greater
3267 indices into the GOT. That makes it easy to calculate the GOT
3268 offset. */
3269 BFD_ASSERT (h->dynindx >= global_got_dynindx);
91d6fa6a
NC
3270 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3271 * MIPS_ELF_GOT_SIZE (abfd));
0f20cc35 3272 }
91d6fa6a 3273 BFD_ASSERT (got_index < htab->sgot->size);
b49e97c9 3274
91d6fa6a 3275 return got_index;
b49e97c9
TS
3276}
3277
5c18022e
RS
3278/* Find a GOT page entry that points to within 32KB of VALUE. These
3279 entries are supposed to be placed at small offsets in the GOT, i.e.,
3280 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3281 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3282 offset of the GOT entry from VALUE. */
b49e97c9
TS
3283
3284static bfd_vma
9719ad41 3285mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3286 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3287{
91d6fa6a 3288 bfd_vma page, got_index;
b15e6682 3289 struct mips_got_entry *entry;
b49e97c9 3290
0a44bf69 3291 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3292 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3293 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3294
b15e6682
AO
3295 if (!entry)
3296 return MINUS_ONE;
143d77c5 3297
91d6fa6a 3298 got_index = entry->gotidx;
b49e97c9
TS
3299
3300 if (offsetp)
f4416af6 3301 *offsetp = value - entry->d.address;
b49e97c9 3302
91d6fa6a 3303 return got_index;
b49e97c9
TS
3304}
3305
738e5348 3306/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3307 EXTERNAL is true if the relocation was originally against a global
3308 symbol that binds locally. */
b49e97c9
TS
3309
3310static bfd_vma
9719ad41 3311mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3312 bfd_vma value, bfd_boolean external)
b49e97c9 3313{
b15e6682 3314 struct mips_got_entry *entry;
b49e97c9 3315
0a44bf69
RS
3316 /* GOT16 relocations against local symbols are followed by a LO16
3317 relocation; those against global symbols are not. Thus if the
3318 symbol was originally local, the GOT16 relocation should load the
3319 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3320 if (! external)
0a44bf69 3321 value = mips_elf_high (value) << 16;
b49e97c9 3322
738e5348
RS
3323 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3324 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3325 same in all cases. */
a8028dd0
RS
3326 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3327 NULL, R_MIPS_GOT16);
b15e6682
AO
3328 if (entry)
3329 return entry->gotidx;
3330 else
3331 return MINUS_ONE;
b49e97c9
TS
3332}
3333
3334/* Returns the offset for the entry at the INDEXth position
3335 in the GOT. */
3336
3337static bfd_vma
a8028dd0 3338mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3339 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3340{
a8028dd0 3341 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3342 asection *sgot;
3343 bfd_vma gp;
3344
a8028dd0 3345 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3346 BFD_ASSERT (htab != NULL);
3347
a8028dd0 3348 sgot = htab->sgot;
f4416af6 3349 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3350 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3351
91d6fa6a 3352 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3353}
3354
0a44bf69
RS
3355/* Create and return a local GOT entry for VALUE, which was calculated
3356 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3357 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3358 instead. */
b49e97c9 3359
b15e6682 3360static struct mips_got_entry *
0a44bf69 3361mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3362 bfd *ibfd, bfd_vma value,
5c18022e 3363 unsigned long r_symndx,
0f20cc35
DJ
3364 struct mips_elf_link_hash_entry *h,
3365 int r_type)
b49e97c9 3366{
b15e6682 3367 struct mips_got_entry entry, **loc;
f4416af6 3368 struct mips_got_info *g;
0a44bf69
RS
3369 struct mips_elf_link_hash_table *htab;
3370
3371 htab = mips_elf_hash_table (info);
4dfe6ac6 3372 BFD_ASSERT (htab != NULL);
b15e6682 3373
f4416af6
AO
3374 entry.abfd = NULL;
3375 entry.symndx = -1;
3376 entry.d.address = value;
0f20cc35 3377 entry.tls_type = 0;
f4416af6 3378
a8028dd0 3379 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
f4416af6
AO
3380 if (g == NULL)
3381 {
a8028dd0 3382 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
f4416af6
AO
3383 BFD_ASSERT (g != NULL);
3384 }
b15e6682 3385
020d7251
RS
3386 /* This function shouldn't be called for symbols that live in the global
3387 area of the GOT. */
3388 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35
DJ
3389 if (TLS_RELOC_P (r_type))
3390 {
3391 struct mips_got_entry *p;
3392
3393 entry.abfd = ibfd;
df58fc94 3394 if (tls_ldm_reloc_p (r_type))
0f20cc35
DJ
3395 {
3396 entry.tls_type = GOT_TLS_LDM;
3397 entry.symndx = 0;
3398 entry.d.addend = 0;
3399 }
3400 else if (h == NULL)
3401 {
3402 entry.symndx = r_symndx;
3403 entry.d.addend = 0;
3404 }
3405 else
3406 entry.d.h = h;
3407
3408 p = (struct mips_got_entry *)
3409 htab_find (g->got_entries, &entry);
3410
3411 BFD_ASSERT (p);
3412 return p;
3413 }
3414
b15e6682
AO
3415 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3416 INSERT);
3417 if (*loc)
3418 return *loc;
143d77c5 3419
b15e6682 3420 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
0f20cc35 3421 entry.tls_type = 0;
b15e6682
AO
3422
3423 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3424
3425 if (! *loc)
3426 return NULL;
143d77c5 3427
b15e6682
AO
3428 memcpy (*loc, &entry, sizeof entry);
3429
8275b357 3430 if (g->assigned_gotno > g->local_gotno)
b49e97c9 3431 {
f4416af6 3432 (*loc)->gotidx = -1;
b49e97c9
TS
3433 /* We didn't allocate enough space in the GOT. */
3434 (*_bfd_error_handler)
3435 (_("not enough GOT space for local GOT entries"));
3436 bfd_set_error (bfd_error_bad_value);
b15e6682 3437 return NULL;
b49e97c9
TS
3438 }
3439
3440 MIPS_ELF_PUT_WORD (abfd, value,
a8028dd0 3441 (htab->sgot->contents + entry.gotidx));
b15e6682 3442
5c18022e 3443 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3444 if (htab->is_vxworks)
3445 {
3446 Elf_Internal_Rela outrel;
5c18022e 3447 asection *s;
91d6fa6a 3448 bfd_byte *rloc;
0a44bf69 3449 bfd_vma got_address;
0a44bf69
RS
3450
3451 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3452 got_address = (htab->sgot->output_section->vma
3453 + htab->sgot->output_offset
0a44bf69
RS
3454 + entry.gotidx);
3455
91d6fa6a 3456 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3457 outrel.r_offset = got_address;
5c18022e
RS
3458 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3459 outrel.r_addend = value;
91d6fa6a 3460 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3461 }
3462
b15e6682 3463 return *loc;
b49e97c9
TS
3464}
3465
d4596a51
RS
3466/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3467 The number might be exact or a worst-case estimate, depending on how
3468 much information is available to elf_backend_omit_section_dynsym at
3469 the current linking stage. */
3470
3471static bfd_size_type
3472count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3473{
3474 bfd_size_type count;
3475
3476 count = 0;
3477 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3478 {
3479 asection *p;
3480 const struct elf_backend_data *bed;
3481
3482 bed = get_elf_backend_data (output_bfd);
3483 for (p = output_bfd->sections; p ; p = p->next)
3484 if ((p->flags & SEC_EXCLUDE) == 0
3485 && (p->flags & SEC_ALLOC) != 0
3486 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3487 ++count;
3488 }
3489 return count;
3490}
3491
b49e97c9 3492/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3493 appear towards the end. */
b49e97c9 3494
b34976b6 3495static bfd_boolean
d4596a51 3496mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3497{
a8028dd0 3498 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3499 struct mips_elf_hash_sort_data hsd;
3500 struct mips_got_info *g;
b49e97c9 3501
d4596a51
RS
3502 if (elf_hash_table (info)->dynsymcount == 0)
3503 return TRUE;
3504
a8028dd0 3505 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3506 BFD_ASSERT (htab != NULL);
3507
a8028dd0 3508 g = htab->got_info;
d4596a51
RS
3509 if (g == NULL)
3510 return TRUE;
f4416af6 3511
b49e97c9 3512 hsd.low = NULL;
23cc69b6
RS
3513 hsd.max_unref_got_dynindx
3514 = hsd.min_got_dynindx
3515 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3516 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3517 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3518 elf_hash_table (info)),
3519 mips_elf_sort_hash_table_f,
3520 &hsd);
3521
3522 /* There should have been enough room in the symbol table to
44c410de 3523 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3524 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3525 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3526 == elf_hash_table (info)->dynsymcount);
3527 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3528 == g->global_gotno);
b49e97c9
TS
3529
3530 /* Now we know which dynamic symbol has the lowest dynamic symbol
3531 table index in the GOT. */
b49e97c9
TS
3532 g->global_gotsym = hsd.low;
3533
b34976b6 3534 return TRUE;
b49e97c9
TS
3535}
3536
3537/* If H needs a GOT entry, assign it the highest available dynamic
3538 index. Otherwise, assign it the lowest available dynamic
3539 index. */
3540
b34976b6 3541static bfd_boolean
9719ad41 3542mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3543{
9719ad41 3544 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9 3545
b49e97c9
TS
3546 /* Symbols without dynamic symbol table entries aren't interesting
3547 at all. */
3548 if (h->root.dynindx == -1)
b34976b6 3549 return TRUE;
b49e97c9 3550
634835ae 3551 switch (h->global_got_area)
f4416af6 3552 {
634835ae
RS
3553 case GGA_NONE:
3554 h->root.dynindx = hsd->max_non_got_dynindx++;
3555 break;
0f20cc35 3556
634835ae 3557 case GGA_NORMAL:
0f20cc35
DJ
3558 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3559
b49e97c9
TS
3560 h->root.dynindx = --hsd->min_got_dynindx;
3561 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3562 break;
3563
3564 case GGA_RELOC_ONLY:
3565 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3566
3567 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3568 hsd->low = (struct elf_link_hash_entry *) h;
3569 h->root.dynindx = hsd->max_unref_got_dynindx++;
3570 break;
b49e97c9
TS
3571 }
3572
b34976b6 3573 return TRUE;
b49e97c9
TS
3574}
3575
3576/* If H is a symbol that needs a global GOT entry, but has a dynamic
3577 symbol table index lower than any we've seen to date, record it for
6ccf4795
RS
3578 posterity. FOR_CALL is true if the caller is only interested in
3579 using the GOT entry for calls. */
b49e97c9 3580
b34976b6 3581static bfd_boolean
9719ad41
RS
3582mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3583 bfd *abfd, struct bfd_link_info *info,
6ccf4795 3584 bfd_boolean for_call,
0f20cc35 3585 unsigned char tls_flag)
b49e97c9 3586{
a8028dd0 3587 struct mips_elf_link_hash_table *htab;
634835ae 3588 struct mips_elf_link_hash_entry *hmips;
f4416af6 3589 struct mips_got_entry entry, **loc;
a8028dd0
RS
3590 struct mips_got_info *g;
3591
3592 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3593 BFD_ASSERT (htab != NULL);
3594
634835ae 3595 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795
RS
3596 if (!for_call)
3597 hmips->got_only_for_calls = FALSE;
f4416af6 3598
b49e97c9
TS
3599 /* A global symbol in the GOT must also be in the dynamic symbol
3600 table. */
7c5fcef7
L
3601 if (h->dynindx == -1)
3602 {
3603 switch (ELF_ST_VISIBILITY (h->other))
3604 {
3605 case STV_INTERNAL:
3606 case STV_HIDDEN:
33bb52fb 3607 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3608 break;
3609 }
c152c796 3610 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3611 return FALSE;
7c5fcef7 3612 }
b49e97c9 3613
86324f90 3614 /* Make sure we have a GOT to put this entry into. */
a8028dd0 3615 g = htab->got_info;
86324f90
EC
3616 BFD_ASSERT (g != NULL);
3617
f4416af6
AO
3618 entry.abfd = abfd;
3619 entry.symndx = -1;
3620 entry.d.h = (struct mips_elf_link_hash_entry *) h;
0f20cc35 3621 entry.tls_type = 0;
f4416af6
AO
3622
3623 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3624 INSERT);
3625
b49e97c9
TS
3626 /* If we've already marked this entry as needing GOT space, we don't
3627 need to do it again. */
f4416af6 3628 if (*loc)
0f20cc35
DJ
3629 {
3630 (*loc)->tls_type |= tls_flag;
3631 return TRUE;
3632 }
f4416af6
AO
3633
3634 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3635
3636 if (! *loc)
3637 return FALSE;
143d77c5 3638
f4416af6 3639 entry.gotidx = -1;
0f20cc35
DJ
3640 entry.tls_type = tls_flag;
3641
f4416af6
AO
3642 memcpy (*loc, &entry, sizeof entry);
3643
0f20cc35 3644 if (tls_flag == 0)
634835ae 3645 hmips->global_got_area = GGA_NORMAL;
b49e97c9 3646
b34976b6 3647 return TRUE;
b49e97c9 3648}
f4416af6
AO
3649
3650/* Reserve space in G for a GOT entry containing the value of symbol
3651 SYMNDX in input bfd ABDF, plus ADDEND. */
3652
3653static bfd_boolean
9719ad41 3654mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
a8028dd0 3655 struct bfd_link_info *info,
0f20cc35 3656 unsigned char tls_flag)
f4416af6 3657{
a8028dd0
RS
3658 struct mips_elf_link_hash_table *htab;
3659 struct mips_got_info *g;
f4416af6
AO
3660 struct mips_got_entry entry, **loc;
3661
a8028dd0 3662 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3663 BFD_ASSERT (htab != NULL);
3664
a8028dd0
RS
3665 g = htab->got_info;
3666 BFD_ASSERT (g != NULL);
3667
f4416af6
AO
3668 entry.abfd = abfd;
3669 entry.symndx = symndx;
3670 entry.d.addend = addend;
0f20cc35 3671 entry.tls_type = tls_flag;
f4416af6
AO
3672 loc = (struct mips_got_entry **)
3673 htab_find_slot (g->got_entries, &entry, INSERT);
3674
3675 if (*loc)
0f20cc35
DJ
3676 {
3677 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3678 {
3679 g->tls_gotno += 2;
3680 (*loc)->tls_type |= tls_flag;
3681 }
3682 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3683 {
3684 g->tls_gotno += 1;
3685 (*loc)->tls_type |= tls_flag;
3686 }
3687 return TRUE;
3688 }
f4416af6 3689
0f20cc35
DJ
3690 if (tls_flag != 0)
3691 {
3692 entry.gotidx = -1;
3693 entry.tls_type = tls_flag;
3694 if (tls_flag == GOT_TLS_IE)
3695 g->tls_gotno += 1;
3696 else if (tls_flag == GOT_TLS_GD)
3697 g->tls_gotno += 2;
3698 else if (g->tls_ldm_offset == MINUS_ONE)
3699 {
3700 g->tls_ldm_offset = MINUS_TWO;
3701 g->tls_gotno += 2;
3702 }
3703 }
3704 else
3705 {
3706 entry.gotidx = g->local_gotno++;
3707 entry.tls_type = 0;
3708 }
f4416af6
AO
3709
3710 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3711
3712 if (! *loc)
3713 return FALSE;
143d77c5 3714
f4416af6
AO
3715 memcpy (*loc, &entry, sizeof entry);
3716
3717 return TRUE;
3718}
c224138d
RS
3719
3720/* Return the maximum number of GOT page entries required for RANGE. */
3721
3722static bfd_vma
3723mips_elf_pages_for_range (const struct mips_got_page_range *range)
3724{
3725 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3726}
3727
3a3b6725 3728/* Record that ABFD has a page relocation against symbol SYMNDX and
a8028dd0
RS
3729 that ADDEND is the addend for that relocation.
3730
3731 This function creates an upper bound on the number of GOT slots
3732 required; no attempt is made to combine references to non-overridable
3733 global symbols across multiple input files. */
c224138d
RS
3734
3735static bfd_boolean
a8028dd0
RS
3736mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3737 long symndx, bfd_signed_vma addend)
c224138d 3738{
a8028dd0
RS
3739 struct mips_elf_link_hash_table *htab;
3740 struct mips_got_info *g;
c224138d
RS
3741 struct mips_got_page_entry lookup, *entry;
3742 struct mips_got_page_range **range_ptr, *range;
3743 bfd_vma old_pages, new_pages;
3744 void **loc;
3745
a8028dd0 3746 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3747 BFD_ASSERT (htab != NULL);
3748
a8028dd0
RS
3749 g = htab->got_info;
3750 BFD_ASSERT (g != NULL);
3751
c224138d
RS
3752 /* Find the mips_got_page_entry hash table entry for this symbol. */
3753 lookup.abfd = abfd;
3754 lookup.symndx = symndx;
3755 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3756 if (loc == NULL)
3757 return FALSE;
3758
3759 /* Create a mips_got_page_entry if this is the first time we've
3760 seen the symbol. */
3761 entry = (struct mips_got_page_entry *) *loc;
3762 if (!entry)
3763 {
3764 entry = bfd_alloc (abfd, sizeof (*entry));
3765 if (!entry)
3766 return FALSE;
3767
3768 entry->abfd = abfd;
3769 entry->symndx = symndx;
3770 entry->ranges = NULL;
3771 entry->num_pages = 0;
3772 *loc = entry;
3773 }
3774
3775 /* Skip over ranges whose maximum extent cannot share a page entry
3776 with ADDEND. */
3777 range_ptr = &entry->ranges;
3778 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3779 range_ptr = &(*range_ptr)->next;
3780
3781 /* If we scanned to the end of the list, or found a range whose
3782 minimum extent cannot share a page entry with ADDEND, create
3783 a new singleton range. */
3784 range = *range_ptr;
3785 if (!range || addend < range->min_addend - 0xffff)
3786 {
3787 range = bfd_alloc (abfd, sizeof (*range));
3788 if (!range)
3789 return FALSE;
3790
3791 range->next = *range_ptr;
3792 range->min_addend = addend;
3793 range->max_addend = addend;
3794
3795 *range_ptr = range;
3796 entry->num_pages++;
3797 g->page_gotno++;
3798 return TRUE;
3799 }
3800
3801 /* Remember how many pages the old range contributed. */
3802 old_pages = mips_elf_pages_for_range (range);
3803
3804 /* Update the ranges. */
3805 if (addend < range->min_addend)
3806 range->min_addend = addend;
3807 else if (addend > range->max_addend)
3808 {
3809 if (range->next && addend >= range->next->min_addend - 0xffff)
3810 {
3811 old_pages += mips_elf_pages_for_range (range->next);
3812 range->max_addend = range->next->max_addend;
3813 range->next = range->next->next;
3814 }
3815 else
3816 range->max_addend = addend;
3817 }
3818
3819 /* Record any change in the total estimate. */
3820 new_pages = mips_elf_pages_for_range (range);
3821 if (old_pages != new_pages)
3822 {
3823 entry->num_pages += new_pages - old_pages;
3824 g->page_gotno += new_pages - old_pages;
3825 }
3826
3827 return TRUE;
3828}
33bb52fb
RS
3829
3830/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3831
3832static void
3833mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3834 unsigned int n)
3835{
3836 asection *s;
3837 struct mips_elf_link_hash_table *htab;
3838
3839 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3840 BFD_ASSERT (htab != NULL);
3841
33bb52fb
RS
3842 s = mips_elf_rel_dyn_section (info, FALSE);
3843 BFD_ASSERT (s != NULL);
3844
3845 if (htab->is_vxworks)
3846 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3847 else
3848 {
3849 if (s->size == 0)
3850 {
3851 /* Make room for a null element. */
3852 s->size += MIPS_ELF_REL_SIZE (abfd);
3853 ++s->reloc_count;
3854 }
3855 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3856 }
3857}
3858\f
3859/* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3860 if the GOT entry is for an indirect or warning symbol. */
3861
3862static int
3863mips_elf_check_recreate_got (void **entryp, void *data)
3864{
3865 struct mips_got_entry *entry;
3866 bfd_boolean *must_recreate;
3867
3868 entry = (struct mips_got_entry *) *entryp;
3869 must_recreate = (bfd_boolean *) data;
3870 if (entry->abfd != NULL && entry->symndx == -1)
3871 {
3872 struct mips_elf_link_hash_entry *h;
3873
3874 h = entry->d.h;
3875 if (h->root.root.type == bfd_link_hash_indirect
3876 || h->root.root.type == bfd_link_hash_warning)
3877 {
3878 *must_recreate = TRUE;
3879 return 0;
3880 }
3881 }
3882 return 1;
3883}
3884
3885/* A htab_traverse callback for GOT entries. Add all entries to
3886 hash table *DATA, converting entries for indirect and warning
3887 symbols into entries for the target symbol. Set *DATA to null
3888 on error. */
3889
3890static int
3891mips_elf_recreate_got (void **entryp, void *data)
3892{
3893 htab_t *new_got;
3894 struct mips_got_entry *entry;
3895 void **slot;
3896
3897 new_got = (htab_t *) data;
3898 entry = (struct mips_got_entry *) *entryp;
3899 if (entry->abfd != NULL && entry->symndx == -1)
3900 {
3901 struct mips_elf_link_hash_entry *h;
3902
3903 h = entry->d.h;
3904 while (h->root.root.type == bfd_link_hash_indirect
3905 || h->root.root.type == bfd_link_hash_warning)
634835ae
RS
3906 {
3907 BFD_ASSERT (h->global_got_area == GGA_NONE);
3908 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3909 }
33bb52fb
RS
3910 entry->d.h = h;
3911 }
3912 slot = htab_find_slot (*new_got, entry, INSERT);
3913 if (slot == NULL)
3914 {
3915 *new_got = NULL;
3916 return 0;
3917 }
3918 if (*slot == NULL)
3919 *slot = entry;
3920 else
3921 free (entry);
3922 return 1;
3923}
3924
3925/* If any entries in G->got_entries are for indirect or warning symbols,
3926 replace them with entries for the target symbol. */
3927
3928static bfd_boolean
3929mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3930{
3931 bfd_boolean must_recreate;
3932 htab_t new_got;
3933
3934 must_recreate = FALSE;
3935 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3936 if (must_recreate)
3937 {
3938 new_got = htab_create (htab_size (g->got_entries),
3939 mips_elf_got_entry_hash,
3940 mips_elf_got_entry_eq, NULL);
3941 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3942 if (new_got == NULL)
3943 return FALSE;
3944
3945 /* Each entry in g->got_entries has either been copied to new_got
3946 or freed. Now delete the hash table itself. */
3947 htab_delete (g->got_entries);
3948 g->got_entries = new_got;
3949 }
3950 return TRUE;
3951}
3952
634835ae 3953/* A mips_elf_link_hash_traverse callback for which DATA points
020d7251
RS
3954 to the link_info structure. Count the number of type (3) entries
3955 in the master GOT. */
33bb52fb
RS
3956
3957static int
d4596a51 3958mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 3959{
020d7251 3960 struct bfd_link_info *info;
6ccf4795 3961 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
3962 struct mips_got_info *g;
3963
020d7251 3964 info = (struct bfd_link_info *) data;
6ccf4795
RS
3965 htab = mips_elf_hash_table (info);
3966 g = htab->got_info;
d4596a51 3967 if (h->global_got_area != GGA_NONE)
33bb52fb 3968 {
020d7251
RS
3969 /* Make a final decision about whether the symbol belongs in the
3970 local or global GOT. Symbols that bind locally can (and in the
3971 case of forced-local symbols, must) live in the local GOT.
3972 Those that are aren't in the dynamic symbol table must also
3973 live in the local GOT.
3974
3975 Note that the former condition does not always imply the
3976 latter: symbols do not bind locally if they are completely
3977 undefined. We'll report undefined symbols later if appropriate. */
6ccf4795
RS
3978 if (h->root.dynindx == -1
3979 || (h->got_only_for_calls
3980 ? SYMBOL_CALLS_LOCAL (info, &h->root)
3981 : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
d4596a51 3982 {
020d7251
RS
3983 /* The symbol belongs in the local GOT. We no longer need this
3984 entry if it was only used for relocations; those relocations
3985 will be against the null or section symbol instead of H. */
d4596a51
RS
3986 if (h->global_got_area != GGA_RELOC_ONLY)
3987 g->local_gotno++;
3988 h->global_got_area = GGA_NONE;
3989 }
6ccf4795
RS
3990 else if (htab->is_vxworks
3991 && h->got_only_for_calls
3992 && h->root.plt.offset != MINUS_ONE)
3993 /* On VxWorks, calls can refer directly to the .got.plt entry;
3994 they don't need entries in the regular GOT. .got.plt entries
3995 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
3996 h->global_got_area = GGA_NONE;
d4596a51 3997 else
23cc69b6
RS
3998 {
3999 g->global_gotno++;
4000 if (h->global_got_area == GGA_RELOC_ONLY)
4001 g->reloc_only_gotno++;
4002 }
33bb52fb
RS
4003 }
4004 return 1;
4005}
f4416af6
AO
4006\f
4007/* Compute the hash value of the bfd in a bfd2got hash entry. */
4008
4009static hashval_t
9719ad41 4010mips_elf_bfd2got_entry_hash (const void *entry_)
f4416af6
AO
4011{
4012 const struct mips_elf_bfd2got_hash *entry
4013 = (struct mips_elf_bfd2got_hash *)entry_;
4014
4015 return entry->bfd->id;
4016}
4017
4018/* Check whether two hash entries have the same bfd. */
4019
4020static int
9719ad41 4021mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
4022{
4023 const struct mips_elf_bfd2got_hash *e1
4024 = (const struct mips_elf_bfd2got_hash *)entry1;
4025 const struct mips_elf_bfd2got_hash *e2
4026 = (const struct mips_elf_bfd2got_hash *)entry2;
4027
4028 return e1->bfd == e2->bfd;
4029}
4030
bad36eac 4031/* In a multi-got link, determine the GOT to be used for IBFD. G must
f4416af6
AO
4032 be the master GOT data. */
4033
4034static struct mips_got_info *
9719ad41 4035mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
4036{
4037 struct mips_elf_bfd2got_hash e, *p;
4038
4039 if (! g->bfd2got)
4040 return g;
4041
4042 e.bfd = ibfd;
9719ad41 4043 p = htab_find (g->bfd2got, &e);
f4416af6
AO
4044 return p ? p->g : NULL;
4045}
4046
c224138d
RS
4047/* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
4048 Return NULL if an error occured. */
f4416af6 4049
c224138d
RS
4050static struct mips_got_info *
4051mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
4052 bfd *input_bfd)
f4416af6 4053{
f4416af6 4054 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
c224138d 4055 struct mips_got_info *g;
f4416af6 4056 void **bfdgotp;
143d77c5 4057
c224138d 4058 bfdgot_entry.bfd = input_bfd;
f4416af6 4059 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
c224138d 4060 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
f4416af6 4061
c224138d 4062 if (bfdgot == NULL)
f4416af6 4063 {
c224138d
RS
4064 bfdgot = ((struct mips_elf_bfd2got_hash *)
4065 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
f4416af6 4066 if (bfdgot == NULL)
c224138d 4067 return NULL;
f4416af6
AO
4068
4069 *bfdgotp = bfdgot;
4070
c224138d
RS
4071 g = ((struct mips_got_info *)
4072 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
f4416af6 4073 if (g == NULL)
c224138d
RS
4074 return NULL;
4075
4076 bfdgot->bfd = input_bfd;
4077 bfdgot->g = g;
f4416af6
AO
4078
4079 g->global_gotsym = NULL;
4080 g->global_gotno = 0;
23cc69b6 4081 g->reloc_only_gotno = 0;
f4416af6 4082 g->local_gotno = 0;
c224138d 4083 g->page_gotno = 0;
f4416af6 4084 g->assigned_gotno = -1;
0f20cc35
DJ
4085 g->tls_gotno = 0;
4086 g->tls_assigned_gotno = 0;
4087 g->tls_ldm_offset = MINUS_ONE;
f4416af6 4088 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
9719ad41 4089 mips_elf_multi_got_entry_eq, NULL);
f4416af6 4090 if (g->got_entries == NULL)
c224138d
RS
4091 return NULL;
4092
4093 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4094 mips_got_page_entry_eq, NULL);
4095 if (g->got_page_entries == NULL)
4096 return NULL;
f4416af6
AO
4097
4098 g->bfd2got = NULL;
4099 g->next = NULL;
4100 }
4101
c224138d
RS
4102 return bfdgot->g;
4103}
4104
4105/* A htab_traverse callback for the entries in the master got.
4106 Create one separate got for each bfd that has entries in the global
4107 got, such that we can tell how many local and global entries each
4108 bfd requires. */
4109
4110static int
4111mips_elf_make_got_per_bfd (void **entryp, void *p)
4112{
4113 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4114 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4115 struct mips_got_info *g;
4116
4117 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4118 if (g == NULL)
4119 {
4120 arg->obfd = NULL;
4121 return 0;
4122 }
4123
f4416af6
AO
4124 /* Insert the GOT entry in the bfd's got entry hash table. */
4125 entryp = htab_find_slot (g->got_entries, entry, INSERT);
4126 if (*entryp != NULL)
4127 return 1;
143d77c5 4128
f4416af6
AO
4129 *entryp = entry;
4130
0f20cc35
DJ
4131 if (entry->tls_type)
4132 {
4133 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4134 g->tls_gotno += 2;
4135 if (entry->tls_type & GOT_TLS_IE)
4136 g->tls_gotno += 1;
4137 }
020d7251 4138 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
f4416af6
AO
4139 ++g->local_gotno;
4140 else
4141 ++g->global_gotno;
4142
4143 return 1;
4144}
4145
c224138d
RS
4146/* A htab_traverse callback for the page entries in the master got.
4147 Associate each page entry with the bfd's got. */
4148
4149static int
4150mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
4151{
4152 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
4153 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
4154 struct mips_got_info *g;
4155
4156 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4157 if (g == NULL)
4158 {
4159 arg->obfd = NULL;
4160 return 0;
4161 }
4162
4163 /* Insert the GOT entry in the bfd's got entry hash table. */
4164 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4165 if (*entryp != NULL)
4166 return 1;
4167
4168 *entryp = entry;
4169 g->page_gotno += entry->num_pages;
4170 return 1;
4171}
4172
4173/* Consider merging the got described by BFD2GOT with TO, using the
4174 information given by ARG. Return -1 if this would lead to overflow,
4175 1 if they were merged successfully, and 0 if a merge failed due to
4176 lack of memory. (These values are chosen so that nonnegative return
4177 values can be returned by a htab_traverse callback.) */
4178
4179static int
4180mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4181 struct mips_got_info *to,
4182 struct mips_elf_got_per_bfd_arg *arg)
4183{
4184 struct mips_got_info *from = bfd2got->g;
4185 unsigned int estimate;
4186
4187 /* Work out how many page entries we would need for the combined GOT. */
4188 estimate = arg->max_pages;
4189 if (estimate >= from->page_gotno + to->page_gotno)
4190 estimate = from->page_gotno + to->page_gotno;
4191
e2ece73c 4192 /* And conservatively estimate how many local and TLS entries
c224138d 4193 would be needed. */
e2ece73c
RS
4194 estimate += from->local_gotno + to->local_gotno;
4195 estimate += from->tls_gotno + to->tls_gotno;
4196
4197 /* If we're merging with the primary got, we will always have
4198 the full set of global entries. Otherwise estimate those
4199 conservatively as well. */
4200 if (to == arg->primary)
4201 estimate += arg->global_count;
4202 else
4203 estimate += from->global_gotno + to->global_gotno;
c224138d
RS
4204
4205 /* Bail out if the combined GOT might be too big. */
4206 if (estimate > arg->max_count)
4207 return -1;
4208
4209 /* Commit to the merge. Record that TO is now the bfd for this got. */
4210 bfd2got->g = to;
4211
4212 /* Transfer the bfd's got information from FROM to TO. */
4213 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4214 if (arg->obfd == NULL)
4215 return 0;
4216
4217 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4218 if (arg->obfd == NULL)
4219 return 0;
4220
4221 /* We don't have to worry about releasing memory of the actual
4222 got entries, since they're all in the master got_entries hash
4223 table anyway. */
4224 htab_delete (from->got_entries);
4225 htab_delete (from->got_page_entries);
4226 return 1;
4227}
4228
f4416af6
AO
4229/* Attempt to merge gots of different input bfds. Try to use as much
4230 as possible of the primary got, since it doesn't require explicit
4231 dynamic relocations, but don't use bfds that would reference global
4232 symbols out of the addressable range. Failing the primary got,
4233 attempt to merge with the current got, or finish the current got
4234 and then make make the new got current. */
4235
4236static int
9719ad41 4237mips_elf_merge_gots (void **bfd2got_, void *p)
f4416af6
AO
4238{
4239 struct mips_elf_bfd2got_hash *bfd2got
4240 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4241 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
c224138d
RS
4242 struct mips_got_info *g;
4243 unsigned int estimate;
4244 int result;
4245
4246 g = bfd2got->g;
4247
4248 /* Work out the number of page, local and TLS entries. */
4249 estimate = arg->max_pages;
4250 if (estimate > g->page_gotno)
4251 estimate = g->page_gotno;
4252 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4253
4254 /* We place TLS GOT entries after both locals and globals. The globals
4255 for the primary GOT may overflow the normal GOT size limit, so be
4256 sure not to merge a GOT which requires TLS with the primary GOT in that
4257 case. This doesn't affect non-primary GOTs. */
c224138d 4258 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4259
c224138d 4260 if (estimate <= arg->max_count)
f4416af6 4261 {
c224138d
RS
4262 /* If we don't have a primary GOT, use it as
4263 a starting point for the primary GOT. */
4264 if (!arg->primary)
4265 {
4266 arg->primary = bfd2got->g;
4267 return 1;
4268 }
f4416af6 4269
c224138d
RS
4270 /* Try merging with the primary GOT. */
4271 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4272 if (result >= 0)
4273 return result;
f4416af6 4274 }
c224138d 4275
f4416af6 4276 /* If we can merge with the last-created got, do it. */
c224138d 4277 if (arg->current)
f4416af6 4278 {
c224138d
RS
4279 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4280 if (result >= 0)
4281 return result;
f4416af6 4282 }
c224138d 4283
f4416af6
AO
4284 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4285 fits; if it turns out that it doesn't, we'll get relocation
4286 overflows anyway. */
c224138d
RS
4287 g->next = arg->current;
4288 arg->current = g;
0f20cc35
DJ
4289
4290 return 1;
4291}
4292
ead49a57
RS
4293/* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4294 is null iff there is just a single GOT. */
0f20cc35
DJ
4295
4296static int
4297mips_elf_initialize_tls_index (void **entryp, void *p)
4298{
4299 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4300 struct mips_got_info *g = p;
ead49a57 4301 bfd_vma next_index;
cbf2cba4 4302 unsigned char tls_type;
0f20cc35
DJ
4303
4304 /* We're only interested in TLS symbols. */
4305 if (entry->tls_type == 0)
4306 return 1;
4307
ead49a57
RS
4308 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4309
4310 if (entry->symndx == -1 && g->next == NULL)
0f20cc35 4311 {
ead49a57
RS
4312 /* A type (3) got entry in the single-GOT case. We use the symbol's
4313 hash table entry to track its index. */
4314 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4315 return 1;
4316 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4317 entry->d.h->tls_got_offset = next_index;
cbf2cba4 4318 tls_type = entry->d.h->tls_type;
ead49a57
RS
4319 }
4320 else
4321 {
4322 if (entry->tls_type & GOT_TLS_LDM)
0f20cc35 4323 {
ead49a57
RS
4324 /* There are separate mips_got_entry objects for each input bfd
4325 that requires an LDM entry. Make sure that all LDM entries in
4326 a GOT resolve to the same index. */
4327 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4005427f 4328 {
ead49a57 4329 entry->gotidx = g->tls_ldm_offset;
4005427f
RS
4330 return 1;
4331 }
ead49a57 4332 g->tls_ldm_offset = next_index;
0f20cc35 4333 }
ead49a57 4334 entry->gotidx = next_index;
cbf2cba4 4335 tls_type = entry->tls_type;
f4416af6
AO
4336 }
4337
ead49a57 4338 /* Account for the entries we've just allocated. */
cbf2cba4 4339 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
0f20cc35 4340 g->tls_assigned_gotno += 2;
cbf2cba4 4341 if (tls_type & GOT_TLS_IE)
0f20cc35
DJ
4342 g->tls_assigned_gotno += 1;
4343
f4416af6
AO
4344 return 1;
4345}
4346
4347/* If passed a NULL mips_got_info in the argument, set the marker used
4348 to tell whether a global symbol needs a got entry (in the primary
4349 got) to the given VALUE.
4350
4351 If passed a pointer G to a mips_got_info in the argument (it must
4352 not be the primary GOT), compute the offset from the beginning of
4353 the (primary) GOT section to the entry in G corresponding to the
4354 global symbol. G's assigned_gotno must contain the index of the
4355 first available global GOT entry in G. VALUE must contain the size
4356 of a GOT entry in bytes. For each global GOT entry that requires a
4357 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4cc11e76 4358 marked as not eligible for lazy resolution through a function
f4416af6
AO
4359 stub. */
4360static int
9719ad41 4361mips_elf_set_global_got_offset (void **entryp, void *p)
f4416af6
AO
4362{
4363 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4364 struct mips_elf_set_global_got_offset_arg *arg
4365 = (struct mips_elf_set_global_got_offset_arg *)p;
4366 struct mips_got_info *g = arg->g;
4367
0f20cc35
DJ
4368 if (g && entry->tls_type != GOT_NORMAL)
4369 arg->needed_relocs +=
4370 mips_tls_got_relocs (arg->info, entry->tls_type,
4371 entry->symndx == -1 ? &entry->d.h->root : NULL);
4372
634835ae
RS
4373 if (entry->abfd != NULL
4374 && entry->symndx == -1
4375 && entry->d.h->global_got_area != GGA_NONE)
f4416af6
AO
4376 {
4377 if (g)
4378 {
4379 BFD_ASSERT (g->global_gotsym == NULL);
4380
4381 entry->gotidx = arg->value * (long) g->assigned_gotno++;
f4416af6
AO
4382 if (arg->info->shared
4383 || (elf_hash_table (arg->info)->dynamic_sections_created
f5385ebf
AM
4384 && entry->d.h->root.def_dynamic
4385 && !entry->d.h->root.def_regular))
f4416af6
AO
4386 ++arg->needed_relocs;
4387 }
4388 else
634835ae 4389 entry->d.h->global_got_area = arg->value;
f4416af6
AO
4390 }
4391
4392 return 1;
4393}
4394
33bb52fb
RS
4395/* A htab_traverse callback for GOT entries for which DATA is the
4396 bfd_link_info. Forbid any global symbols from having traditional
4397 lazy-binding stubs. */
4398
0626d451 4399static int
33bb52fb 4400mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4401{
33bb52fb
RS
4402 struct bfd_link_info *info;
4403 struct mips_elf_link_hash_table *htab;
4404 struct mips_got_entry *entry;
0626d451 4405
33bb52fb
RS
4406 entry = (struct mips_got_entry *) *entryp;
4407 info = (struct bfd_link_info *) data;
4408 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4409 BFD_ASSERT (htab != NULL);
4410
0626d451
RS
4411 if (entry->abfd != NULL
4412 && entry->symndx == -1
33bb52fb 4413 && entry->d.h->needs_lazy_stub)
f4416af6 4414 {
33bb52fb
RS
4415 entry->d.h->needs_lazy_stub = FALSE;
4416 htab->lazy_stub_count--;
f4416af6 4417 }
143d77c5 4418
f4416af6
AO
4419 return 1;
4420}
4421
f4416af6
AO
4422/* Return the offset of an input bfd IBFD's GOT from the beginning of
4423 the primary GOT. */
4424static bfd_vma
9719ad41 4425mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
4426{
4427 if (g->bfd2got == NULL)
4428 return 0;
4429
4430 g = mips_elf_got_for_ibfd (g, ibfd);
4431 if (! g)
4432 return 0;
4433
4434 BFD_ASSERT (g->next);
4435
4436 g = g->next;
143d77c5 4437
0f20cc35
DJ
4438 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4439 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4440}
4441
4442/* Turn a single GOT that is too big for 16-bit addressing into
4443 a sequence of GOTs, each one 16-bit addressable. */
4444
4445static bfd_boolean
9719ad41 4446mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4447 asection *got, bfd_size_type pages)
f4416af6 4448{
a8028dd0 4449 struct mips_elf_link_hash_table *htab;
f4416af6
AO
4450 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4451 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
a8028dd0 4452 struct mips_got_info *g, *gg;
33bb52fb
RS
4453 unsigned int assign, needed_relocs;
4454 bfd *dynobj;
f4416af6 4455
33bb52fb 4456 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4457 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4458 BFD_ASSERT (htab != NULL);
4459
a8028dd0 4460 g = htab->got_info;
f4416af6 4461 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
9719ad41 4462 mips_elf_bfd2got_entry_eq, NULL);
f4416af6
AO
4463 if (g->bfd2got == NULL)
4464 return FALSE;
4465
4466 got_per_bfd_arg.bfd2got = g->bfd2got;
4467 got_per_bfd_arg.obfd = abfd;
4468 got_per_bfd_arg.info = info;
4469
4470 /* Count how many GOT entries each input bfd requires, creating a
4471 map from bfd to got info while at that. */
f4416af6
AO
4472 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4473 if (got_per_bfd_arg.obfd == NULL)
4474 return FALSE;
4475
c224138d
RS
4476 /* Also count how many page entries each input bfd requires. */
4477 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4478 &got_per_bfd_arg);
4479 if (got_per_bfd_arg.obfd == NULL)
4480 return FALSE;
4481
f4416af6
AO
4482 got_per_bfd_arg.current = NULL;
4483 got_per_bfd_arg.primary = NULL;
0a44bf69 4484 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4485 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4486 - htab->reserved_gotno);
c224138d 4487 got_per_bfd_arg.max_pages = pages;
0f20cc35
DJ
4488 /* The number of globals that will be included in the primary GOT.
4489 See the calls to mips_elf_set_global_got_offset below for more
4490 information. */
4491 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4492
4493 /* Try to merge the GOTs of input bfds together, as long as they
4494 don't seem to exceed the maximum GOT size, choosing one of them
4495 to be the primary GOT. */
4496 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4497 if (got_per_bfd_arg.obfd == NULL)
4498 return FALSE;
4499
0f20cc35 4500 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6
AO
4501 if (got_per_bfd_arg.primary == NULL)
4502 {
4503 g->next = (struct mips_got_info *)
4504 bfd_alloc (abfd, sizeof (struct mips_got_info));
4505 if (g->next == NULL)
4506 return FALSE;
4507
4508 g->next->global_gotsym = NULL;
4509 g->next->global_gotno = 0;
23cc69b6 4510 g->next->reloc_only_gotno = 0;
f4416af6 4511 g->next->local_gotno = 0;
c224138d 4512 g->next->page_gotno = 0;
0f20cc35 4513 g->next->tls_gotno = 0;
f4416af6 4514 g->next->assigned_gotno = 0;
0f20cc35
DJ
4515 g->next->tls_assigned_gotno = 0;
4516 g->next->tls_ldm_offset = MINUS_ONE;
f4416af6
AO
4517 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4518 mips_elf_multi_got_entry_eq,
9719ad41 4519 NULL);
f4416af6
AO
4520 if (g->next->got_entries == NULL)
4521 return FALSE;
c224138d
RS
4522 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4523 mips_got_page_entry_eq,
4524 NULL);
4525 if (g->next->got_page_entries == NULL)
4526 return FALSE;
f4416af6
AO
4527 g->next->bfd2got = NULL;
4528 }
4529 else
4530 g->next = got_per_bfd_arg.primary;
4531 g->next->next = got_per_bfd_arg.current;
4532
4533 /* GG is now the master GOT, and G is the primary GOT. */
4534 gg = g;
4535 g = g->next;
4536
4537 /* Map the output bfd to the primary got. That's what we're going
4538 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4539 didn't mark in check_relocs, and we want a quick way to find it.
4540 We can't just use gg->next because we're going to reverse the
4541 list. */
4542 {
4543 struct mips_elf_bfd2got_hash *bfdgot;
4544 void **bfdgotp;
143d77c5 4545
f4416af6
AO
4546 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4547 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4548
4549 if (bfdgot == NULL)
4550 return FALSE;
4551
4552 bfdgot->bfd = abfd;
4553 bfdgot->g = g;
4554 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4555
4556 BFD_ASSERT (*bfdgotp == NULL);
4557 *bfdgotp = bfdgot;
4558 }
4559
634835ae
RS
4560 /* Every symbol that is referenced in a dynamic relocation must be
4561 present in the primary GOT, so arrange for them to appear after
4562 those that are actually referenced. */
23cc69b6 4563 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4564 g->global_gotno = gg->global_gotno;
f4416af6 4565
f4416af6 4566 set_got_offset_arg.g = NULL;
634835ae 4567 set_got_offset_arg.value = GGA_RELOC_ONLY;
f4416af6
AO
4568 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4569 &set_got_offset_arg);
634835ae 4570 set_got_offset_arg.value = GGA_NORMAL;
f4416af6
AO
4571 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4572 &set_got_offset_arg);
f4416af6
AO
4573
4574 /* Now go through the GOTs assigning them offset ranges.
4575 [assigned_gotno, local_gotno[ will be set to the range of local
4576 entries in each GOT. We can then compute the end of a GOT by
4577 adding local_gotno to global_gotno. We reverse the list and make
4578 it circular since then we'll be able to quickly compute the
4579 beginning of a GOT, by computing the end of its predecessor. To
4580 avoid special cases for the primary GOT, while still preserving
4581 assertions that are valid for both single- and multi-got links,
4582 we arrange for the main got struct to have the right number of
4583 global entries, but set its local_gotno such that the initial
4584 offset of the primary GOT is zero. Remember that the primary GOT
4585 will become the last item in the circular linked list, so it
4586 points back to the master GOT. */
4587 gg->local_gotno = -g->global_gotno;
4588 gg->global_gotno = g->global_gotno;
0f20cc35 4589 gg->tls_gotno = 0;
f4416af6
AO
4590 assign = 0;
4591 gg->next = gg;
4592
4593 do
4594 {
4595 struct mips_got_info *gn;
4596
861fb55a 4597 assign += htab->reserved_gotno;
f4416af6 4598 g->assigned_gotno = assign;
c224138d
RS
4599 g->local_gotno += assign;
4600 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4601 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4602
ead49a57
RS
4603 /* Take g out of the direct list, and push it onto the reversed
4604 list that gg points to. g->next is guaranteed to be nonnull after
4605 this operation, as required by mips_elf_initialize_tls_index. */
4606 gn = g->next;
4607 g->next = gg->next;
4608 gg->next = g;
4609
0f20cc35
DJ
4610 /* Set up any TLS entries. We always place the TLS entries after
4611 all non-TLS entries. */
4612 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4613 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
f4416af6 4614
ead49a57 4615 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4616 g = gn;
0626d451 4617
33bb52fb
RS
4618 /* Forbid global symbols in every non-primary GOT from having
4619 lazy-binding stubs. */
0626d451 4620 if (g)
33bb52fb 4621 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4622 }
4623 while (g);
4624
eea6121a 4625 got->size = (gg->next->local_gotno
33bb52fb
RS
4626 + gg->next->global_gotno
4627 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4628
4629 needed_relocs = 0;
4630 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4631 set_got_offset_arg.info = info;
4632 for (g = gg->next; g && g->next != gg; g = g->next)
4633 {
4634 unsigned int save_assign;
4635
4636 /* Assign offsets to global GOT entries. */
4637 save_assign = g->assigned_gotno;
4638 g->assigned_gotno = g->local_gotno;
4639 set_got_offset_arg.g = g;
4640 set_got_offset_arg.needed_relocs = 0;
4641 htab_traverse (g->got_entries,
4642 mips_elf_set_global_got_offset,
4643 &set_got_offset_arg);
4644 needed_relocs += set_got_offset_arg.needed_relocs;
4645 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4646
4647 g->assigned_gotno = save_assign;
4648 if (info->shared)
4649 {
4650 needed_relocs += g->local_gotno - g->assigned_gotno;
4651 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4652 + g->next->global_gotno
4653 + g->next->tls_gotno
861fb55a 4654 + htab->reserved_gotno);
33bb52fb
RS
4655 }
4656 }
4657
4658 if (needed_relocs)
4659 mips_elf_allocate_dynamic_relocations (dynobj, info,
4660 needed_relocs);
143d77c5 4661
f4416af6
AO
4662 return TRUE;
4663}
143d77c5 4664
b49e97c9
TS
4665\f
4666/* Returns the first relocation of type r_type found, beginning with
4667 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4668
4669static const Elf_Internal_Rela *
9719ad41
RS
4670mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4671 const Elf_Internal_Rela *relocation,
4672 const Elf_Internal_Rela *relend)
b49e97c9 4673{
c000e262
TS
4674 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4675
b49e97c9
TS
4676 while (relocation < relend)
4677 {
c000e262
TS
4678 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4679 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4680 return relocation;
4681
4682 ++relocation;
4683 }
4684
4685 /* We didn't find it. */
b49e97c9
TS
4686 return NULL;
4687}
4688
020d7251 4689/* Return whether an input relocation is against a local symbol. */
b49e97c9 4690
b34976b6 4691static bfd_boolean
9719ad41
RS
4692mips_elf_local_relocation_p (bfd *input_bfd,
4693 const Elf_Internal_Rela *relocation,
020d7251 4694 asection **local_sections)
b49e97c9
TS
4695{
4696 unsigned long r_symndx;
4697 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
4698 size_t extsymoff;
4699
4700 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4701 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4702 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4703
4704 if (r_symndx < extsymoff)
b34976b6 4705 return TRUE;
b49e97c9 4706 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4707 return TRUE;
b49e97c9 4708
b34976b6 4709 return FALSE;
b49e97c9
TS
4710}
4711\f
4712/* Sign-extend VALUE, which has the indicated number of BITS. */
4713
a7ebbfdf 4714bfd_vma
9719ad41 4715_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4716{
4717 if (value & ((bfd_vma) 1 << (bits - 1)))
4718 /* VALUE is negative. */
4719 value |= ((bfd_vma) - 1) << bits;
4720
4721 return value;
4722}
4723
4724/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4725 range expressible by a signed number with the indicated number of
b49e97c9
TS
4726 BITS. */
4727
b34976b6 4728static bfd_boolean
9719ad41 4729mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4730{
4731 bfd_signed_vma svalue = (bfd_signed_vma) value;
4732
4733 if (svalue > (1 << (bits - 1)) - 1)
4734 /* The value is too big. */
b34976b6 4735 return TRUE;
b49e97c9
TS
4736 else if (svalue < -(1 << (bits - 1)))
4737 /* The value is too small. */
b34976b6 4738 return TRUE;
b49e97c9
TS
4739
4740 /* All is well. */
b34976b6 4741 return FALSE;
b49e97c9
TS
4742}
4743
4744/* Calculate the %high function. */
4745
4746static bfd_vma
9719ad41 4747mips_elf_high (bfd_vma value)
b49e97c9
TS
4748{
4749 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4750}
4751
4752/* Calculate the %higher function. */
4753
4754static bfd_vma
9719ad41 4755mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4756{
4757#ifdef BFD64
4758 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4759#else
4760 abort ();
c5ae1840 4761 return MINUS_ONE;
b49e97c9
TS
4762#endif
4763}
4764
4765/* Calculate the %highest function. */
4766
4767static bfd_vma
9719ad41 4768mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4769{
4770#ifdef BFD64
b15e6682 4771 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4772#else
4773 abort ();
c5ae1840 4774 return MINUS_ONE;
b49e97c9
TS
4775#endif
4776}
4777\f
4778/* Create the .compact_rel section. */
4779
b34976b6 4780static bfd_boolean
9719ad41
RS
4781mips_elf_create_compact_rel_section
4782 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4783{
4784 flagword flags;
4785 register asection *s;
4786
4787 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4788 {
4789 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4790 | SEC_READONLY);
4791
3496cb2a 4792 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4793 if (s == NULL
b49e97c9
TS
4794 || ! bfd_set_section_alignment (abfd, s,
4795 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4796 return FALSE;
b49e97c9 4797
eea6121a 4798 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4799 }
4800
b34976b6 4801 return TRUE;
b49e97c9
TS
4802}
4803
4804/* Create the .got section to hold the global offset table. */
4805
b34976b6 4806static bfd_boolean
23cc69b6 4807mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
4808{
4809 flagword flags;
4810 register asection *s;
4811 struct elf_link_hash_entry *h;
14a793b2 4812 struct bfd_link_hash_entry *bh;
b49e97c9
TS
4813 struct mips_got_info *g;
4814 bfd_size_type amt;
0a44bf69
RS
4815 struct mips_elf_link_hash_table *htab;
4816
4817 htab = mips_elf_hash_table (info);
4dfe6ac6 4818 BFD_ASSERT (htab != NULL);
b49e97c9
TS
4819
4820 /* This function may be called more than once. */
23cc69b6
RS
4821 if (htab->sgot)
4822 return TRUE;
b49e97c9
TS
4823
4824 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4825 | SEC_LINKER_CREATED);
4826
72b4917c
TS
4827 /* We have to use an alignment of 2**4 here because this is hardcoded
4828 in the function stub generation and in the linker script. */
3496cb2a 4829 s = bfd_make_section_with_flags (abfd, ".got", flags);
b49e97c9 4830 if (s == NULL
72b4917c 4831 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 4832 return FALSE;
a8028dd0 4833 htab->sgot = s;
b49e97c9
TS
4834
4835 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4836 linker script because we don't want to define the symbol if we
4837 are not creating a global offset table. */
14a793b2 4838 bh = NULL;
b49e97c9
TS
4839 if (! (_bfd_generic_link_add_one_symbol
4840 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 4841 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 4842 return FALSE;
14a793b2
AM
4843
4844 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
4845 h->non_elf = 0;
4846 h->def_regular = 1;
b49e97c9 4847 h->type = STT_OBJECT;
d329bcd1 4848 elf_hash_table (info)->hgot = h;
b49e97c9
TS
4849
4850 if (info->shared
c152c796 4851 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4852 return FALSE;
b49e97c9 4853
b49e97c9 4854 amt = sizeof (struct mips_got_info);
9719ad41 4855 g = bfd_alloc (abfd, amt);
b49e97c9 4856 if (g == NULL)
b34976b6 4857 return FALSE;
b49e97c9 4858 g->global_gotsym = NULL;
e3d54347 4859 g->global_gotno = 0;
23cc69b6 4860 g->reloc_only_gotno = 0;
0f20cc35 4861 g->tls_gotno = 0;
861fb55a 4862 g->local_gotno = 0;
c224138d 4863 g->page_gotno = 0;
861fb55a 4864 g->assigned_gotno = 0;
f4416af6
AO
4865 g->bfd2got = NULL;
4866 g->next = NULL;
0f20cc35 4867 g->tls_ldm_offset = MINUS_ONE;
b15e6682 4868 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
9719ad41 4869 mips_elf_got_entry_eq, NULL);
b15e6682
AO
4870 if (g->got_entries == NULL)
4871 return FALSE;
c224138d
RS
4872 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4873 mips_got_page_entry_eq, NULL);
4874 if (g->got_page_entries == NULL)
4875 return FALSE;
a8028dd0 4876 htab->got_info = g;
f0abc2a1 4877 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
4878 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4879
861fb55a
DJ
4880 /* We also need a .got.plt section when generating PLTs. */
4881 s = bfd_make_section_with_flags (abfd, ".got.plt",
4882 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4883 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4884 if (s == NULL)
4885 return FALSE;
4886 htab->sgotplt = s;
0a44bf69 4887
b34976b6 4888 return TRUE;
b49e97c9 4889}
b49e97c9 4890\f
0a44bf69
RS
4891/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4892 __GOTT_INDEX__ symbols. These symbols are only special for
4893 shared objects; they are not used in executables. */
4894
4895static bfd_boolean
4896is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4897{
4898 return (mips_elf_hash_table (info)->is_vxworks
4899 && info->shared
4900 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4901 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4902}
861fb55a
DJ
4903
4904/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4905 require an la25 stub. See also mips_elf_local_pic_function_p,
4906 which determines whether the destination function ever requires a
4907 stub. */
4908
4909static bfd_boolean
4910mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4911{
4912 /* We specifically ignore branches and jumps from EF_PIC objects,
4913 where the onus is on the compiler or programmer to perform any
4914 necessary initialization of $25. Sometimes such initialization
4915 is unnecessary; for example, -mno-shared functions do not use
4916 the incoming value of $25, and may therefore be called directly. */
4917 if (PIC_OBJECT_P (input_bfd))
4918 return FALSE;
4919
4920 switch (r_type)
4921 {
4922 case R_MIPS_26:
4923 case R_MIPS_PC16:
4924 case R_MIPS16_26:
df58fc94
RS
4925 case R_MICROMIPS_26_S1:
4926 case R_MICROMIPS_PC7_S1:
4927 case R_MICROMIPS_PC10_S1:
4928 case R_MICROMIPS_PC16_S1:
4929 case R_MICROMIPS_PC23_S2:
861fb55a
DJ
4930 return TRUE;
4931
4932 default:
4933 return FALSE;
4934 }
4935}
0a44bf69 4936\f
b49e97c9
TS
4937/* Calculate the value produced by the RELOCATION (which comes from
4938 the INPUT_BFD). The ADDEND is the addend to use for this
4939 RELOCATION; RELOCATION->R_ADDEND is ignored.
4940
4941 The result of the relocation calculation is stored in VALUEP.
38a7df63 4942 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
df58fc94 4943 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9
TS
4944
4945 This function returns bfd_reloc_continue if the caller need take no
4946 further action regarding this relocation, bfd_reloc_notsupported if
4947 something goes dramatically wrong, bfd_reloc_overflow if an
4948 overflow occurs, and bfd_reloc_ok to indicate success. */
4949
4950static bfd_reloc_status_type
9719ad41
RS
4951mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4952 asection *input_section,
4953 struct bfd_link_info *info,
4954 const Elf_Internal_Rela *relocation,
4955 bfd_vma addend, reloc_howto_type *howto,
4956 Elf_Internal_Sym *local_syms,
4957 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
4958 const char **namep,
4959 bfd_boolean *cross_mode_jump_p,
9719ad41 4960 bfd_boolean save_addend)
b49e97c9
TS
4961{
4962 /* The eventual value we will return. */
4963 bfd_vma value;
4964 /* The address of the symbol against which the relocation is
4965 occurring. */
4966 bfd_vma symbol = 0;
4967 /* The final GP value to be used for the relocatable, executable, or
4968 shared object file being produced. */
0a61c8c2 4969 bfd_vma gp;
b49e97c9
TS
4970 /* The place (section offset or address) of the storage unit being
4971 relocated. */
4972 bfd_vma p;
4973 /* The value of GP used to create the relocatable object. */
0a61c8c2 4974 bfd_vma gp0;
b49e97c9
TS
4975 /* The offset into the global offset table at which the address of
4976 the relocation entry symbol, adjusted by the addend, resides
4977 during execution. */
4978 bfd_vma g = MINUS_ONE;
4979 /* The section in which the symbol referenced by the relocation is
4980 located. */
4981 asection *sec = NULL;
4982 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 4983 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 4984 symbol. */
b34976b6
AM
4985 bfd_boolean local_p, was_local_p;
4986 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4987 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
4988 /* TRUE if the symbol referred to by this relocation is
4989 "__gnu_local_gp". */
4990 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
4991 Elf_Internal_Shdr *symtab_hdr;
4992 size_t extsymoff;
4993 unsigned long r_symndx;
4994 int r_type;
b34976b6 4995 /* TRUE if overflow occurred during the calculation of the
b49e97c9 4996 relocation value. */
b34976b6
AM
4997 bfd_boolean overflowed_p;
4998 /* TRUE if this relocation refers to a MIPS16 function. */
4999 bfd_boolean target_is_16_bit_code_p = FALSE;
df58fc94 5000 bfd_boolean target_is_micromips_code_p = FALSE;
0a44bf69
RS
5001 struct mips_elf_link_hash_table *htab;
5002 bfd *dynobj;
5003
5004 dynobj = elf_hash_table (info)->dynobj;
5005 htab = mips_elf_hash_table (info);
4dfe6ac6 5006 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5007
5008 /* Parse the relocation. */
5009 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5010 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5011 p = (input_section->output_section->vma
5012 + input_section->output_offset
5013 + relocation->r_offset);
5014
5015 /* Assume that there will be no overflow. */
b34976b6 5016 overflowed_p = FALSE;
b49e97c9
TS
5017
5018 /* Figure out whether or not the symbol is local, and get the offset
5019 used in the array of hash table entries. */
5020 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5021 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 5022 local_sections);
bce03d3d 5023 was_local_p = local_p;
b49e97c9
TS
5024 if (! elf_bad_symtab (input_bfd))
5025 extsymoff = symtab_hdr->sh_info;
5026 else
5027 {
5028 /* The symbol table does not follow the rule that local symbols
5029 must come before globals. */
5030 extsymoff = 0;
5031 }
5032
5033 /* Figure out the value of the symbol. */
5034 if (local_p)
5035 {
5036 Elf_Internal_Sym *sym;
5037
5038 sym = local_syms + r_symndx;
5039 sec = local_sections[r_symndx];
5040
5041 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
5042 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5043 || (sec->flags & SEC_MERGE))
b49e97c9 5044 symbol += sym->st_value;
d4df96e6
L
5045 if ((sec->flags & SEC_MERGE)
5046 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5047 {
5048 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5049 addend -= symbol;
5050 addend += sec->output_section->vma + sec->output_offset;
5051 }
b49e97c9 5052
df58fc94
RS
5053 /* MIPS16/microMIPS text labels should be treated as odd. */
5054 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
5055 ++symbol;
5056
5057 /* Record the name of this symbol, for our caller. */
5058 *namep = bfd_elf_string_from_elf_section (input_bfd,
5059 symtab_hdr->sh_link,
5060 sym->st_name);
5061 if (*namep == '\0')
5062 *namep = bfd_section_name (input_bfd, sec);
5063
30c09090 5064 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
df58fc94 5065 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
b49e97c9
TS
5066 }
5067 else
5068 {
560e09e9
NC
5069 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5070
b49e97c9
TS
5071 /* For global symbols we look up the symbol in the hash-table. */
5072 h = ((struct mips_elf_link_hash_entry *)
5073 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5074 /* Find the real hash-table entry for this symbol. */
5075 while (h->root.root.type == bfd_link_hash_indirect
5076 || h->root.root.type == bfd_link_hash_warning)
5077 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5078
5079 /* Record the name of this symbol, for our caller. */
5080 *namep = h->root.root.root.string;
5081
5082 /* See if this is the special _gp_disp symbol. Note that such a
5083 symbol must always be a global symbol. */
560e09e9 5084 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
5085 && ! NEWABI_P (input_bfd))
5086 {
5087 /* Relocations against _gp_disp are permitted only with
5088 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 5089 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
5090 return bfd_reloc_notsupported;
5091
b34976b6 5092 gp_disp_p = TRUE;
b49e97c9 5093 }
bbe506e8
TS
5094 /* See if this is the special _gp symbol. Note that such a
5095 symbol must always be a global symbol. */
5096 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5097 gnu_local_gp_p = TRUE;
5098
5099
b49e97c9
TS
5100 /* If this symbol is defined, calculate its address. Note that
5101 _gp_disp is a magic symbol, always implicitly defined by the
5102 linker, so it's inappropriate to check to see whether or not
5103 its defined. */
5104 else if ((h->root.root.type == bfd_link_hash_defined
5105 || h->root.root.type == bfd_link_hash_defweak)
5106 && h->root.root.u.def.section)
5107 {
5108 sec = h->root.root.u.def.section;
5109 if (sec->output_section)
5110 symbol = (h->root.root.u.def.value
5111 + sec->output_section->vma
5112 + sec->output_offset);
5113 else
5114 symbol = h->root.root.u.def.value;
5115 }
5116 else if (h->root.root.type == bfd_link_hash_undefweak)
5117 /* We allow relocations against undefined weak symbols, giving
5118 it the value zero, so that you can undefined weak functions
5119 and check to see if they exist by looking at their
5120 addresses. */
5121 symbol = 0;
59c2e50f 5122 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5123 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5124 symbol = 0;
a4d0f181
TS
5125 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5126 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5127 {
5128 /* If this is a dynamic link, we should have created a
5129 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5130 in in _bfd_mips_elf_create_dynamic_sections.
5131 Otherwise, we should define the symbol with a value of 0.
5132 FIXME: It should probably get into the symbol table
5133 somehow as well. */
5134 BFD_ASSERT (! info->shared);
5135 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5136 symbol = 0;
5137 }
5e2b0d47
NC
5138 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5139 {
5140 /* This is an optional symbol - an Irix specific extension to the
5141 ELF spec. Ignore it for now.
5142 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5143 than simply ignoring them, but we do not handle this for now.
5144 For information see the "64-bit ELF Object File Specification"
5145 which is available from here:
5146 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5147 symbol = 0;
5148 }
e7e2196d
MR
5149 else if ((*info->callbacks->undefined_symbol)
5150 (info, h->root.root.root.string, input_bfd,
5151 input_section, relocation->r_offset,
5152 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5153 || ELF_ST_VISIBILITY (h->root.other)))
5154 {
5155 return bfd_reloc_undefined;
5156 }
b49e97c9
TS
5157 else
5158 {
e7e2196d 5159 return bfd_reloc_notsupported;
b49e97c9
TS
5160 }
5161
30c09090 5162 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
df58fc94
RS
5163 /* If the output section is the PLT section,
5164 then the target is not microMIPS. */
5165 target_is_micromips_code_p = (htab->splt != sec
5166 && ELF_ST_IS_MICROMIPS (h->root.other));
b49e97c9
TS
5167 }
5168
738e5348
RS
5169 /* If this is a reference to a 16-bit function with a stub, we need
5170 to redirect the relocation to the stub unless:
5171
5172 (a) the relocation is for a MIPS16 JAL;
5173
5174 (b) the relocation is for a MIPS16 PIC call, and there are no
5175 non-MIPS16 uses of the GOT slot; or
5176
5177 (c) the section allows direct references to MIPS16 functions. */
5178 if (r_type != R_MIPS16_26
5179 && !info->relocatable
5180 && ((h != NULL
5181 && h->fn_stub != NULL
5182 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71
TS
5183 || (local_p
5184 && elf_tdata (input_bfd)->local_stubs != NULL
b49e97c9 5185 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5186 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5187 {
5188 /* This is a 32- or 64-bit call to a 16-bit function. We should
5189 have already noticed that we were going to need the
5190 stub. */
5191 if (local_p)
5192 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5193 else
5194 {
5195 BFD_ASSERT (h->need_fn_stub);
5196 sec = h->fn_stub;
5197 }
5198
5199 symbol = sec->output_section->vma + sec->output_offset;
f38c2df5
TS
5200 /* The target is 16-bit, but the stub isn't. */
5201 target_is_16_bit_code_p = FALSE;
b49e97c9
TS
5202 }
5203 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
738e5348
RS
5204 need to redirect the call to the stub. Note that we specifically
5205 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5206 use an indirect stub instead. */
1049f94e 5207 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5208 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71
TS
5209 || (local_p
5210 && elf_tdata (input_bfd)->local_call_stubs != NULL
5211 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
b49e97c9
TS
5212 && !target_is_16_bit_code_p)
5213 {
b9d58d71
TS
5214 if (local_p)
5215 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5216 else
b49e97c9 5217 {
b9d58d71
TS
5218 /* If both call_stub and call_fp_stub are defined, we can figure
5219 out which one to use by checking which one appears in the input
5220 file. */
5221 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5222 {
b9d58d71
TS
5223 asection *o;
5224
5225 sec = NULL;
5226 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5227 {
b9d58d71
TS
5228 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5229 {
5230 sec = h->call_fp_stub;
5231 break;
5232 }
b49e97c9 5233 }
b9d58d71
TS
5234 if (sec == NULL)
5235 sec = h->call_stub;
b49e97c9 5236 }
b9d58d71 5237 else if (h->call_stub != NULL)
b49e97c9 5238 sec = h->call_stub;
b9d58d71
TS
5239 else
5240 sec = h->call_fp_stub;
5241 }
b49e97c9 5242
eea6121a 5243 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5244 symbol = sec->output_section->vma + sec->output_offset;
5245 }
861fb55a
DJ
5246 /* If this is a direct call to a PIC function, redirect to the
5247 non-PIC stub. */
5248 else if (h != NULL && h->la25_stub
5249 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5250 symbol = (h->la25_stub->stub_section->output_section->vma
5251 + h->la25_stub->stub_section->output_offset
5252 + h->la25_stub->offset);
b49e97c9 5253
df58fc94
RS
5254 /* Make sure MIPS16 and microMIPS are not used together. */
5255 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5256 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5257 {
5258 (*_bfd_error_handler)
5259 (_("MIPS16 and microMIPS functions cannot call each other"));
5260 return bfd_reloc_notsupported;
5261 }
5262
b49e97c9 5263 /* Calls from 16-bit code to 32-bit code and vice versa require the
df58fc94
RS
5264 mode change. However, we can ignore calls to undefined weak symbols,
5265 which should never be executed at runtime. This exception is important
5266 because the assembly writer may have "known" that any definition of the
5267 symbol would be 16-bit code, and that direct jumps were therefore
5268 acceptable. */
5269 *cross_mode_jump_p = (!info->relocatable
5270 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5271 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5272 || (r_type == R_MICROMIPS_26_S1
5273 && !target_is_micromips_code_p)
5274 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5275 && (target_is_16_bit_code_p
5276 || target_is_micromips_code_p))));
b49e97c9 5277
020d7251 5278 local_p = h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->root);
b49e97c9 5279
0a61c8c2
RS
5280 gp0 = _bfd_get_gp_value (input_bfd);
5281 gp = _bfd_get_gp_value (abfd);
23cc69b6 5282 if (htab->got_info)
a8028dd0 5283 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5284
5285 if (gnu_local_gp_p)
5286 symbol = gp;
5287
df58fc94
RS
5288 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5289 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5290 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5291 if (got_page_reloc_p (r_type) && !local_p)
020d7251 5292 {
df58fc94
RS
5293 r_type = (micromips_reloc_p (r_type)
5294 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
020d7251
RS
5295 addend = 0;
5296 }
5297
e77760d2 5298 /* If we haven't already determined the GOT offset, and we're going
0a61c8c2 5299 to need it, get it now. */
b49e97c9
TS
5300 switch (r_type)
5301 {
738e5348
RS
5302 case R_MIPS16_CALL16:
5303 case R_MIPS16_GOT16:
b49e97c9
TS
5304 case R_MIPS_CALL16:
5305 case R_MIPS_GOT16:
5306 case R_MIPS_GOT_DISP:
5307 case R_MIPS_GOT_HI16:
5308 case R_MIPS_CALL_HI16:
5309 case R_MIPS_GOT_LO16:
5310 case R_MIPS_CALL_LO16:
df58fc94
RS
5311 case R_MICROMIPS_CALL16:
5312 case R_MICROMIPS_GOT16:
5313 case R_MICROMIPS_GOT_DISP:
5314 case R_MICROMIPS_GOT_HI16:
5315 case R_MICROMIPS_CALL_HI16:
5316 case R_MICROMIPS_GOT_LO16:
5317 case R_MICROMIPS_CALL_LO16:
0f20cc35
DJ
5318 case R_MIPS_TLS_GD:
5319 case R_MIPS_TLS_GOTTPREL:
5320 case R_MIPS_TLS_LDM:
df58fc94
RS
5321 case R_MICROMIPS_TLS_GD:
5322 case R_MICROMIPS_TLS_GOTTPREL:
5323 case R_MICROMIPS_TLS_LDM:
b49e97c9 5324 /* Find the index into the GOT where this value is located. */
df58fc94 5325 if (tls_ldm_reloc_p (r_type))
0f20cc35 5326 {
0a44bf69 5327 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5328 0, 0, NULL, r_type);
0f20cc35
DJ
5329 if (g == MINUS_ONE)
5330 return bfd_reloc_outofrange;
5331 }
5332 else if (!local_p)
b49e97c9 5333 {
0a44bf69
RS
5334 /* On VxWorks, CALL relocations should refer to the .got.plt
5335 entry, which is initialized to point at the PLT stub. */
5336 if (htab->is_vxworks
df58fc94
RS
5337 && (call_hi16_reloc_p (r_type)
5338 || call_lo16_reloc_p (r_type)
738e5348 5339 || call16_reloc_p (r_type)))
0a44bf69
RS
5340 {
5341 BFD_ASSERT (addend == 0);
5342 BFD_ASSERT (h->root.needs_plt);
5343 g = mips_elf_gotplt_index (info, &h->root);
5344 }
5345 else
b49e97c9 5346 {
020d7251 5347 BFD_ASSERT (addend == 0);
0a44bf69
RS
5348 g = mips_elf_global_got_index (dynobj, input_bfd,
5349 &h->root, r_type, info);
5350 if (h->tls_type == GOT_NORMAL
020d7251
RS
5351 && !elf_hash_table (info)->dynamic_sections_created)
5352 /* This is a static link. We must initialize the GOT entry. */
a8028dd0 5353 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5354 }
5355 }
0a44bf69 5356 else if (!htab->is_vxworks
738e5348 5357 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5358 /* The calculation below does not involve "g". */
b49e97c9
TS
5359 break;
5360 else
5361 {
5c18022e 5362 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5363 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5364 if (g == MINUS_ONE)
5365 return bfd_reloc_outofrange;
5366 }
5367
5368 /* Convert GOT indices to actual offsets. */
a8028dd0 5369 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5370 break;
b49e97c9
TS
5371 }
5372
0a44bf69
RS
5373 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5374 symbols are resolved by the loader. Add them to .rela.dyn. */
5375 if (h != NULL && is_gott_symbol (info, &h->root))
5376 {
5377 Elf_Internal_Rela outrel;
5378 bfd_byte *loc;
5379 asection *s;
5380
5381 s = mips_elf_rel_dyn_section (info, FALSE);
5382 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5383
5384 outrel.r_offset = (input_section->output_section->vma
5385 + input_section->output_offset
5386 + relocation->r_offset);
5387 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5388 outrel.r_addend = addend;
5389 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5390
5391 /* If we've written this relocation for a readonly section,
5392 we need to set DF_TEXTREL again, so that we do not delete the
5393 DT_TEXTREL tag. */
5394 if (MIPS_ELF_READONLY_SECTION (input_section))
5395 info->flags |= DF_TEXTREL;
5396
0a44bf69
RS
5397 *valuep = 0;
5398 return bfd_reloc_ok;
5399 }
5400
b49e97c9
TS
5401 /* Figure out what kind of relocation is being performed. */
5402 switch (r_type)
5403 {
5404 case R_MIPS_NONE:
5405 return bfd_reloc_continue;
5406
5407 case R_MIPS_16:
a7ebbfdf 5408 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5409 overflowed_p = mips_elf_overflow_p (value, 16);
5410 break;
5411
5412 case R_MIPS_32:
5413 case R_MIPS_REL32:
5414 case R_MIPS_64:
5415 if ((info->shared
861fb55a 5416 || (htab->root.dynamic_sections_created
b49e97c9 5417 && h != NULL
f5385ebf 5418 && h->root.def_dynamic
861fb55a
DJ
5419 && !h->root.def_regular
5420 && !h->has_static_relocs))
cf35638d 5421 && r_symndx != STN_UNDEF
9a59ad6b
DJ
5422 && (h == NULL
5423 || h->root.root.type != bfd_link_hash_undefweak
5424 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5425 && (input_section->flags & SEC_ALLOC) != 0)
5426 {
861fb55a 5427 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5428 where the symbol will end up. So, we create a relocation
5429 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5430 linker. We must do the same for executable references to
5431 shared library symbols, unless we've decided to use copy
5432 relocs or PLTs instead. */
b49e97c9
TS
5433 value = addend;
5434 if (!mips_elf_create_dynamic_relocation (abfd,
5435 info,
5436 relocation,
5437 h,
5438 sec,
5439 symbol,
5440 &value,
5441 input_section))
5442 return bfd_reloc_undefined;
5443 }
5444 else
5445 {
5446 if (r_type != R_MIPS_REL32)
5447 value = symbol + addend;
5448 else
5449 value = addend;
5450 }
5451 value &= howto->dst_mask;
092dcd75
CD
5452 break;
5453
5454 case R_MIPS_PC32:
5455 value = symbol + addend - p;
5456 value &= howto->dst_mask;
b49e97c9
TS
5457 break;
5458
b49e97c9
TS
5459 case R_MIPS16_26:
5460 /* The calculation for R_MIPS16_26 is just the same as for an
5461 R_MIPS_26. It's only the storage of the relocated field into
5462 the output file that's different. That's handled in
5463 mips_elf_perform_relocation. So, we just fall through to the
5464 R_MIPS_26 case here. */
5465 case R_MIPS_26:
df58fc94
RS
5466 case R_MICROMIPS_26_S1:
5467 {
5468 unsigned int shift;
5469
5470 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5471 the correct ISA mode selector and bit 1 must be 0. */
5472 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5473 return bfd_reloc_outofrange;
5474
5475 /* Shift is 2, unusually, for microMIPS JALX. */
5476 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5477
5478 if (was_local_p)
5479 value = addend | ((p + 4) & (0xfc000000 << shift));
5480 else
5481 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5482 value = (value + symbol) >> shift;
5483 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5484 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5485 value &= howto->dst_mask;
5486 }
b49e97c9
TS
5487 break;
5488
0f20cc35 5489 case R_MIPS_TLS_DTPREL_HI16:
df58fc94 5490 case R_MICROMIPS_TLS_DTPREL_HI16:
0f20cc35
DJ
5491 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5492 & howto->dst_mask);
5493 break;
5494
5495 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5496 case R_MIPS_TLS_DTPREL32:
5497 case R_MIPS_TLS_DTPREL64:
df58fc94 5498 case R_MICROMIPS_TLS_DTPREL_LO16:
0f20cc35
DJ
5499 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5500 break;
5501
5502 case R_MIPS_TLS_TPREL_HI16:
df58fc94 5503 case R_MICROMIPS_TLS_TPREL_HI16:
0f20cc35
DJ
5504 value = (mips_elf_high (addend + symbol - tprel_base (info))
5505 & howto->dst_mask);
5506 break;
5507
5508 case R_MIPS_TLS_TPREL_LO16:
df58fc94 5509 case R_MICROMIPS_TLS_TPREL_LO16:
0f20cc35
DJ
5510 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5511 break;
5512
b49e97c9 5513 case R_MIPS_HI16:
d6f16593 5514 case R_MIPS16_HI16:
df58fc94 5515 case R_MICROMIPS_HI16:
b49e97c9
TS
5516 if (!gp_disp_p)
5517 {
5518 value = mips_elf_high (addend + symbol);
5519 value &= howto->dst_mask;
5520 }
5521 else
5522 {
d6f16593
MR
5523 /* For MIPS16 ABI code we generate this sequence
5524 0: li $v0,%hi(_gp_disp)
5525 4: addiupc $v1,%lo(_gp_disp)
5526 8: sll $v0,16
5527 12: addu $v0,$v1
5528 14: move $gp,$v0
5529 So the offsets of hi and lo relocs are the same, but the
5530 $pc is four higher than $t9 would be, so reduce
5531 both reloc addends by 4. */
5532 if (r_type == R_MIPS16_HI16)
5533 value = mips_elf_high (addend + gp - p - 4);
df58fc94
RS
5534 /* The microMIPS .cpload sequence uses the same assembly
5535 instructions as the traditional psABI version, but the
5536 incoming $t9 has the low bit set. */
5537 else if (r_type == R_MICROMIPS_HI16)
5538 value = mips_elf_high (addend + gp - p - 1);
d6f16593
MR
5539 else
5540 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5541 overflowed_p = mips_elf_overflow_p (value, 16);
5542 }
5543 break;
5544
5545 case R_MIPS_LO16:
d6f16593 5546 case R_MIPS16_LO16:
df58fc94
RS
5547 case R_MICROMIPS_LO16:
5548 case R_MICROMIPS_HI0_LO16:
b49e97c9
TS
5549 if (!gp_disp_p)
5550 value = (symbol + addend) & howto->dst_mask;
5551 else
5552 {
d6f16593
MR
5553 /* See the comment for R_MIPS16_HI16 above for the reason
5554 for this conditional. */
5555 if (r_type == R_MIPS16_LO16)
5556 value = addend + gp - p;
df58fc94
RS
5557 else if (r_type == R_MICROMIPS_LO16
5558 || r_type == R_MICROMIPS_HI0_LO16)
5559 value = addend + gp - p + 3;
d6f16593
MR
5560 else
5561 value = addend + gp - p + 4;
b49e97c9 5562 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5563 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5564 _gp_disp are normally generated from the .cpload
5565 pseudo-op. It generates code that normally looks like
5566 this:
5567
5568 lui $gp,%hi(_gp_disp)
5569 addiu $gp,$gp,%lo(_gp_disp)
5570 addu $gp,$gp,$t9
5571
5572 Here $t9 holds the address of the function being called,
5573 as required by the MIPS ELF ABI. The R_MIPS_LO16
5574 relocation can easily overflow in this situation, but the
5575 R_MIPS_HI16 relocation will handle the overflow.
5576 Therefore, we consider this a bug in the MIPS ABI, and do
5577 not check for overflow here. */
5578 }
5579 break;
5580
5581 case R_MIPS_LITERAL:
df58fc94 5582 case R_MICROMIPS_LITERAL:
b49e97c9
TS
5583 /* Because we don't merge literal sections, we can handle this
5584 just like R_MIPS_GPREL16. In the long run, we should merge
5585 shared literals, and then we will need to additional work
5586 here. */
5587
5588 /* Fall through. */
5589
5590 case R_MIPS16_GPREL:
5591 /* The R_MIPS16_GPREL performs the same calculation as
5592 R_MIPS_GPREL16, but stores the relocated bits in a different
5593 order. We don't need to do anything special here; the
5594 differences are handled in mips_elf_perform_relocation. */
5595 case R_MIPS_GPREL16:
df58fc94
RS
5596 case R_MICROMIPS_GPREL7_S2:
5597 case R_MICROMIPS_GPREL16:
bce03d3d
AO
5598 /* Only sign-extend the addend if it was extracted from the
5599 instruction. If the addend was separate, leave it alone,
5600 otherwise we may lose significant bits. */
5601 if (howto->partial_inplace)
a7ebbfdf 5602 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5603 value = symbol + addend - gp;
5604 /* If the symbol was local, any earlier relocatable links will
5605 have adjusted its addend with the gp offset, so compensate
5606 for that now. Don't do it for symbols forced local in this
5607 link, though, since they won't have had the gp offset applied
5608 to them before. */
5609 if (was_local_p)
5610 value += gp0;
b49e97c9
TS
5611 overflowed_p = mips_elf_overflow_p (value, 16);
5612 break;
5613
738e5348
RS
5614 case R_MIPS16_GOT16:
5615 case R_MIPS16_CALL16:
b49e97c9
TS
5616 case R_MIPS_GOT16:
5617 case R_MIPS_CALL16:
df58fc94
RS
5618 case R_MICROMIPS_GOT16:
5619 case R_MICROMIPS_CALL16:
0a44bf69 5620 /* VxWorks does not have separate local and global semantics for
738e5348 5621 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5622 if (!htab->is_vxworks && local_p)
b49e97c9 5623 {
5c18022e 5624 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 5625 symbol + addend, !was_local_p);
b49e97c9
TS
5626 if (value == MINUS_ONE)
5627 return bfd_reloc_outofrange;
5628 value
a8028dd0 5629 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5630 overflowed_p = mips_elf_overflow_p (value, 16);
5631 break;
5632 }
5633
5634 /* Fall through. */
5635
0f20cc35
DJ
5636 case R_MIPS_TLS_GD:
5637 case R_MIPS_TLS_GOTTPREL:
5638 case R_MIPS_TLS_LDM:
b49e97c9 5639 case R_MIPS_GOT_DISP:
df58fc94
RS
5640 case R_MICROMIPS_TLS_GD:
5641 case R_MICROMIPS_TLS_GOTTPREL:
5642 case R_MICROMIPS_TLS_LDM:
5643 case R_MICROMIPS_GOT_DISP:
b49e97c9
TS
5644 value = g;
5645 overflowed_p = mips_elf_overflow_p (value, 16);
5646 break;
5647
5648 case R_MIPS_GPREL32:
bce03d3d
AO
5649 value = (addend + symbol + gp0 - gp);
5650 if (!save_addend)
5651 value &= howto->dst_mask;
b49e97c9
TS
5652 break;
5653
5654 case R_MIPS_PC16:
bad36eac
DJ
5655 case R_MIPS_GNU_REL16_S2:
5656 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5657 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5658 value >>= howto->rightshift;
5659 value &= howto->dst_mask;
b49e97c9
TS
5660 break;
5661
df58fc94
RS
5662 case R_MICROMIPS_PC7_S1:
5663 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5664 overflowed_p = mips_elf_overflow_p (value, 8);
5665 value >>= howto->rightshift;
5666 value &= howto->dst_mask;
5667 break;
5668
5669 case R_MICROMIPS_PC10_S1:
5670 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5671 overflowed_p = mips_elf_overflow_p (value, 11);
5672 value >>= howto->rightshift;
5673 value &= howto->dst_mask;
5674 break;
5675
5676 case R_MICROMIPS_PC16_S1:
5677 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5678 overflowed_p = mips_elf_overflow_p (value, 17);
5679 value >>= howto->rightshift;
5680 value &= howto->dst_mask;
5681 break;
5682
5683 case R_MICROMIPS_PC23_S2:
5684 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5685 overflowed_p = mips_elf_overflow_p (value, 25);
5686 value >>= howto->rightshift;
5687 value &= howto->dst_mask;
5688 break;
5689
b49e97c9
TS
5690 case R_MIPS_GOT_HI16:
5691 case R_MIPS_CALL_HI16:
df58fc94
RS
5692 case R_MICROMIPS_GOT_HI16:
5693 case R_MICROMIPS_CALL_HI16:
b49e97c9
TS
5694 /* We're allowed to handle these two relocations identically.
5695 The dynamic linker is allowed to handle the CALL relocations
5696 differently by creating a lazy evaluation stub. */
5697 value = g;
5698 value = mips_elf_high (value);
5699 value &= howto->dst_mask;
5700 break;
5701
5702 case R_MIPS_GOT_LO16:
5703 case R_MIPS_CALL_LO16:
df58fc94
RS
5704 case R_MICROMIPS_GOT_LO16:
5705 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
5706 value = g & howto->dst_mask;
5707 break;
5708
5709 case R_MIPS_GOT_PAGE:
df58fc94 5710 case R_MICROMIPS_GOT_PAGE:
5c18022e 5711 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5712 if (value == MINUS_ONE)
5713 return bfd_reloc_outofrange;
a8028dd0 5714 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5715 overflowed_p = mips_elf_overflow_p (value, 16);
5716 break;
5717
5718 case R_MIPS_GOT_OFST:
df58fc94 5719 case R_MICROMIPS_GOT_OFST:
93a2b7ae 5720 if (local_p)
5c18022e 5721 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5722 else
5723 value = addend;
b49e97c9
TS
5724 overflowed_p = mips_elf_overflow_p (value, 16);
5725 break;
5726
5727 case R_MIPS_SUB:
df58fc94 5728 case R_MICROMIPS_SUB:
b49e97c9
TS
5729 value = symbol - addend;
5730 value &= howto->dst_mask;
5731 break;
5732
5733 case R_MIPS_HIGHER:
df58fc94 5734 case R_MICROMIPS_HIGHER:
b49e97c9
TS
5735 value = mips_elf_higher (addend + symbol);
5736 value &= howto->dst_mask;
5737 break;
5738
5739 case R_MIPS_HIGHEST:
df58fc94 5740 case R_MICROMIPS_HIGHEST:
b49e97c9
TS
5741 value = mips_elf_highest (addend + symbol);
5742 value &= howto->dst_mask;
5743 break;
5744
5745 case R_MIPS_SCN_DISP:
df58fc94 5746 case R_MICROMIPS_SCN_DISP:
b49e97c9
TS
5747 value = symbol + addend - sec->output_offset;
5748 value &= howto->dst_mask;
5749 break;
5750
b49e97c9 5751 case R_MIPS_JALR:
df58fc94 5752 case R_MICROMIPS_JALR:
1367d393
ILT
5753 /* This relocation is only a hint. In some cases, we optimize
5754 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5755 when the symbol does not resolve locally. */
5756 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5757 return bfd_reloc_continue;
5758 value = symbol + addend;
5759 break;
b49e97c9 5760
1367d393 5761 case R_MIPS_PJUMP:
b49e97c9
TS
5762 case R_MIPS_GNU_VTINHERIT:
5763 case R_MIPS_GNU_VTENTRY:
5764 /* We don't do anything with these at present. */
5765 return bfd_reloc_continue;
5766
5767 default:
5768 /* An unrecognized relocation type. */
5769 return bfd_reloc_notsupported;
5770 }
5771
5772 /* Store the VALUE for our caller. */
5773 *valuep = value;
5774 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5775}
5776
5777/* Obtain the field relocated by RELOCATION. */
5778
5779static bfd_vma
9719ad41
RS
5780mips_elf_obtain_contents (reloc_howto_type *howto,
5781 const Elf_Internal_Rela *relocation,
5782 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
5783{
5784 bfd_vma x;
5785 bfd_byte *location = contents + relocation->r_offset;
5786
5787 /* Obtain the bytes. */
5788 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5789
b49e97c9
TS
5790 return x;
5791}
5792
5793/* It has been determined that the result of the RELOCATION is the
5794 VALUE. Use HOWTO to place VALUE into the output file at the
5795 appropriate position. The SECTION is the section to which the
38a7df63
CF
5796 relocation applies.
5797 CROSS_MODE_JUMP_P is true if the relocation field
df58fc94 5798 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9 5799
b34976b6 5800 Returns FALSE if anything goes wrong. */
b49e97c9 5801
b34976b6 5802static bfd_boolean
9719ad41
RS
5803mips_elf_perform_relocation (struct bfd_link_info *info,
5804 reloc_howto_type *howto,
5805 const Elf_Internal_Rela *relocation,
5806 bfd_vma value, bfd *input_bfd,
5807 asection *input_section, bfd_byte *contents,
38a7df63 5808 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
5809{
5810 bfd_vma x;
5811 bfd_byte *location;
5812 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5813
5814 /* Figure out where the relocation is occurring. */
5815 location = contents + relocation->r_offset;
5816
df58fc94 5817 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
d6f16593 5818
b49e97c9
TS
5819 /* Obtain the current value. */
5820 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5821
5822 /* Clear the field we are setting. */
5823 x &= ~howto->dst_mask;
5824
b49e97c9
TS
5825 /* Set the field. */
5826 x |= (value & howto->dst_mask);
5827
5828 /* If required, turn JAL into JALX. */
38a7df63 5829 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 5830 {
b34976b6 5831 bfd_boolean ok;
b49e97c9
TS
5832 bfd_vma opcode = x >> 26;
5833 bfd_vma jalx_opcode;
5834
5835 /* Check to see if the opcode is already JAL or JALX. */
5836 if (r_type == R_MIPS16_26)
5837 {
5838 ok = ((opcode == 0x6) || (opcode == 0x7));
5839 jalx_opcode = 0x7;
5840 }
df58fc94
RS
5841 else if (r_type == R_MICROMIPS_26_S1)
5842 {
5843 ok = ((opcode == 0x3d) || (opcode == 0x3c));
5844 jalx_opcode = 0x3c;
5845 }
b49e97c9
TS
5846 else
5847 {
5848 ok = ((opcode == 0x3) || (opcode == 0x1d));
5849 jalx_opcode = 0x1d;
5850 }
5851
5852 /* If the opcode is not JAL or JALX, there's a problem. */
5853 if (!ok)
5854 {
5855 (*_bfd_error_handler)
776167e8 5856 (_("%B: %A+0x%lx: Direct jumps between ISA modes are not allowed; consider recompiling with interlinking enabled."),
d003868e
AM
5857 input_bfd,
5858 input_section,
b49e97c9
TS
5859 (unsigned long) relocation->r_offset);
5860 bfd_set_error (bfd_error_bad_value);
b34976b6 5861 return FALSE;
b49e97c9
TS
5862 }
5863
5864 /* Make this the JALX opcode. */
5865 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5866 }
5867
38a7df63
CF
5868 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5869 range. */
cd8d5a82 5870 if (!info->relocatable
38a7df63 5871 && !cross_mode_jump_p
cd8d5a82
CF
5872 && ((JAL_TO_BAL_P (input_bfd)
5873 && r_type == R_MIPS_26
5874 && (x >> 26) == 0x3) /* jal addr */
5875 || (JALR_TO_BAL_P (input_bfd)
5876 && r_type == R_MIPS_JALR
38a7df63
CF
5877 && x == 0x0320f809) /* jalr t9 */
5878 || (JR_TO_B_P (input_bfd)
5879 && r_type == R_MIPS_JALR
5880 && x == 0x03200008))) /* jr t9 */
1367d393
ILT
5881 {
5882 bfd_vma addr;
5883 bfd_vma dest;
5884 bfd_signed_vma off;
5885
5886 addr = (input_section->output_section->vma
5887 + input_section->output_offset
5888 + relocation->r_offset
5889 + 4);
5890 if (r_type == R_MIPS_26)
5891 dest = (value << 2) | ((addr >> 28) << 28);
5892 else
5893 dest = value;
5894 off = dest - addr;
5895 if (off <= 0x1ffff && off >= -0x20000)
38a7df63
CF
5896 {
5897 if (x == 0x03200008) /* jr t9 */
5898 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5899 else
5900 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5901 }
1367d393
ILT
5902 }
5903
b49e97c9
TS
5904 /* Put the value into the output. */
5905 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593 5906
df58fc94
RS
5907 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
5908 location);
d6f16593 5909
b34976b6 5910 return TRUE;
b49e97c9 5911}
b49e97c9 5912\f
b49e97c9
TS
5913/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5914 is the original relocation, which is now being transformed into a
5915 dynamic relocation. The ADDENDP is adjusted if necessary; the
5916 caller should store the result in place of the original addend. */
5917
b34976b6 5918static bfd_boolean
9719ad41
RS
5919mips_elf_create_dynamic_relocation (bfd *output_bfd,
5920 struct bfd_link_info *info,
5921 const Elf_Internal_Rela *rel,
5922 struct mips_elf_link_hash_entry *h,
5923 asection *sec, bfd_vma symbol,
5924 bfd_vma *addendp, asection *input_section)
b49e97c9 5925{
947216bf 5926 Elf_Internal_Rela outrel[3];
b49e97c9
TS
5927 asection *sreloc;
5928 bfd *dynobj;
5929 int r_type;
5d41f0b6
RS
5930 long indx;
5931 bfd_boolean defined_p;
0a44bf69 5932 struct mips_elf_link_hash_table *htab;
b49e97c9 5933
0a44bf69 5934 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
5935 BFD_ASSERT (htab != NULL);
5936
b49e97c9
TS
5937 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5938 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 5939 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
5940 BFD_ASSERT (sreloc != NULL);
5941 BFD_ASSERT (sreloc->contents != NULL);
5942 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 5943 < sreloc->size);
b49e97c9 5944
b49e97c9
TS
5945 outrel[0].r_offset =
5946 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
5947 if (ABI_64_P (output_bfd))
5948 {
5949 outrel[1].r_offset =
5950 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5951 outrel[2].r_offset =
5952 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5953 }
b49e97c9 5954
c5ae1840 5955 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 5956 /* The relocation field has been deleted. */
5d41f0b6
RS
5957 return TRUE;
5958
5959 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
5960 {
5961 /* The relocation field has been converted into a relative value of
5962 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5963 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 5964 *addendp += symbol;
5d41f0b6 5965 return TRUE;
0d591ff7 5966 }
b49e97c9 5967
5d41f0b6
RS
5968 /* We must now calculate the dynamic symbol table index to use
5969 in the relocation. */
d4a77f3f 5970 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 5971 {
020d7251 5972 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5d41f0b6
RS
5973 indx = h->root.dynindx;
5974 if (SGI_COMPAT (output_bfd))
5975 defined_p = h->root.def_regular;
5976 else
5977 /* ??? glibc's ld.so just adds the final GOT entry to the
5978 relocation field. It therefore treats relocs against
5979 defined symbols in the same way as relocs against
5980 undefined symbols. */
5981 defined_p = FALSE;
5982 }
b49e97c9
TS
5983 else
5984 {
5d41f0b6
RS
5985 if (sec != NULL && bfd_is_abs_section (sec))
5986 indx = 0;
5987 else if (sec == NULL || sec->owner == NULL)
fdd07405 5988 {
5d41f0b6
RS
5989 bfd_set_error (bfd_error_bad_value);
5990 return FALSE;
b49e97c9
TS
5991 }
5992 else
5993 {
5d41f0b6 5994 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
5995 if (indx == 0)
5996 {
5997 asection *osec = htab->root.text_index_section;
5998 indx = elf_section_data (osec)->dynindx;
5999 }
5d41f0b6
RS
6000 if (indx == 0)
6001 abort ();
b49e97c9
TS
6002 }
6003
5d41f0b6
RS
6004 /* Instead of generating a relocation using the section
6005 symbol, we may as well make it a fully relative
6006 relocation. We want to avoid generating relocations to
6007 local symbols because we used to generate them
6008 incorrectly, without adding the original symbol value,
6009 which is mandated by the ABI for section symbols. In
6010 order to give dynamic loaders and applications time to
6011 phase out the incorrect use, we refrain from emitting
6012 section-relative relocations. It's not like they're
6013 useful, after all. This should be a bit more efficient
6014 as well. */
6015 /* ??? Although this behavior is compatible with glibc's ld.so,
6016 the ABI says that relocations against STN_UNDEF should have
6017 a symbol value of 0. Irix rld honors this, so relocations
6018 against STN_UNDEF have no effect. */
6019 if (!SGI_COMPAT (output_bfd))
6020 indx = 0;
6021 defined_p = TRUE;
b49e97c9
TS
6022 }
6023
5d41f0b6
RS
6024 /* If the relocation was previously an absolute relocation and
6025 this symbol will not be referred to by the relocation, we must
6026 adjust it by the value we give it in the dynamic symbol table.
6027 Otherwise leave the job up to the dynamic linker. */
6028 if (defined_p && r_type != R_MIPS_REL32)
6029 *addendp += symbol;
6030
0a44bf69
RS
6031 if (htab->is_vxworks)
6032 /* VxWorks uses non-relative relocations for this. */
6033 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6034 else
6035 /* The relocation is always an REL32 relocation because we don't
6036 know where the shared library will wind up at load-time. */
6037 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6038 R_MIPS_REL32);
6039
5d41f0b6
RS
6040 /* For strict adherence to the ABI specification, we should
6041 generate a R_MIPS_64 relocation record by itself before the
6042 _REL32/_64 record as well, such that the addend is read in as
6043 a 64-bit value (REL32 is a 32-bit relocation, after all).
6044 However, since none of the existing ELF64 MIPS dynamic
6045 loaders seems to care, we don't waste space with these
6046 artificial relocations. If this turns out to not be true,
6047 mips_elf_allocate_dynamic_relocation() should be tweaked so
6048 as to make room for a pair of dynamic relocations per
6049 invocation if ABI_64_P, and here we should generate an
6050 additional relocation record with R_MIPS_64 by itself for a
6051 NULL symbol before this relocation record. */
6052 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6053 ABI_64_P (output_bfd)
6054 ? R_MIPS_64
6055 : R_MIPS_NONE);
6056 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6057
6058 /* Adjust the output offset of the relocation to reference the
6059 correct location in the output file. */
6060 outrel[0].r_offset += (input_section->output_section->vma
6061 + input_section->output_offset);
6062 outrel[1].r_offset += (input_section->output_section->vma
6063 + input_section->output_offset);
6064 outrel[2].r_offset += (input_section->output_section->vma
6065 + input_section->output_offset);
6066
b49e97c9
TS
6067 /* Put the relocation back out. We have to use the special
6068 relocation outputter in the 64-bit case since the 64-bit
6069 relocation format is non-standard. */
6070 if (ABI_64_P (output_bfd))
6071 {
6072 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6073 (output_bfd, &outrel[0],
6074 (sreloc->contents
6075 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6076 }
0a44bf69
RS
6077 else if (htab->is_vxworks)
6078 {
6079 /* VxWorks uses RELA rather than REL dynamic relocations. */
6080 outrel[0].r_addend = *addendp;
6081 bfd_elf32_swap_reloca_out
6082 (output_bfd, &outrel[0],
6083 (sreloc->contents
6084 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6085 }
b49e97c9 6086 else
947216bf
AM
6087 bfd_elf32_swap_reloc_out
6088 (output_bfd, &outrel[0],
6089 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 6090
b49e97c9
TS
6091 /* We've now added another relocation. */
6092 ++sreloc->reloc_count;
6093
6094 /* Make sure the output section is writable. The dynamic linker
6095 will be writing to it. */
6096 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6097 |= SHF_WRITE;
6098
6099 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 6100 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9
TS
6101 {
6102 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
6103 bfd_byte *cr;
6104
6105 if (scpt)
6106 {
6107 Elf32_crinfo cptrel;
6108
6109 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6110 cptrel.vaddr = (rel->r_offset
6111 + input_section->output_section->vma
6112 + input_section->output_offset);
6113 if (r_type == R_MIPS_REL32)
6114 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6115 else
6116 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6117 mips_elf_set_cr_dist2to (cptrel, 0);
6118 cptrel.konst = *addendp;
6119
6120 cr = (scpt->contents
6121 + sizeof (Elf32_External_compact_rel));
abc0f8d0 6122 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
6123 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6124 ((Elf32_External_crinfo *) cr
6125 + scpt->reloc_count));
6126 ++scpt->reloc_count;
6127 }
6128 }
6129
943284cc
DJ
6130 /* If we've written this relocation for a readonly section,
6131 we need to set DF_TEXTREL again, so that we do not delete the
6132 DT_TEXTREL tag. */
6133 if (MIPS_ELF_READONLY_SECTION (input_section))
6134 info->flags |= DF_TEXTREL;
6135
b34976b6 6136 return TRUE;
b49e97c9
TS
6137}
6138\f
b49e97c9
TS
6139/* Return the MACH for a MIPS e_flags value. */
6140
6141unsigned long
9719ad41 6142_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
6143{
6144 switch (flags & EF_MIPS_MACH)
6145 {
6146 case E_MIPS_MACH_3900:
6147 return bfd_mach_mips3900;
6148
6149 case E_MIPS_MACH_4010:
6150 return bfd_mach_mips4010;
6151
6152 case E_MIPS_MACH_4100:
6153 return bfd_mach_mips4100;
6154
6155 case E_MIPS_MACH_4111:
6156 return bfd_mach_mips4111;
6157
00707a0e
RS
6158 case E_MIPS_MACH_4120:
6159 return bfd_mach_mips4120;
6160
b49e97c9
TS
6161 case E_MIPS_MACH_4650:
6162 return bfd_mach_mips4650;
6163
00707a0e
RS
6164 case E_MIPS_MACH_5400:
6165 return bfd_mach_mips5400;
6166
6167 case E_MIPS_MACH_5500:
6168 return bfd_mach_mips5500;
6169
0d2e43ed
ILT
6170 case E_MIPS_MACH_9000:
6171 return bfd_mach_mips9000;
6172
b49e97c9
TS
6173 case E_MIPS_MACH_SB1:
6174 return bfd_mach_mips_sb1;
6175
350cc38d
MS
6176 case E_MIPS_MACH_LS2E:
6177 return bfd_mach_mips_loongson_2e;
6178
6179 case E_MIPS_MACH_LS2F:
6180 return bfd_mach_mips_loongson_2f;
6181
fd503541
NC
6182 case E_MIPS_MACH_LS3A:
6183 return bfd_mach_mips_loongson_3a;
6184
6f179bd0
AN
6185 case E_MIPS_MACH_OCTEON:
6186 return bfd_mach_mips_octeon;
6187
52b6b6b9
JM
6188 case E_MIPS_MACH_XLR:
6189 return bfd_mach_mips_xlr;
6190
b49e97c9
TS
6191 default:
6192 switch (flags & EF_MIPS_ARCH)
6193 {
6194 default:
6195 case E_MIPS_ARCH_1:
6196 return bfd_mach_mips3000;
b49e97c9
TS
6197
6198 case E_MIPS_ARCH_2:
6199 return bfd_mach_mips6000;
b49e97c9
TS
6200
6201 case E_MIPS_ARCH_3:
6202 return bfd_mach_mips4000;
b49e97c9
TS
6203
6204 case E_MIPS_ARCH_4:
6205 return bfd_mach_mips8000;
b49e97c9
TS
6206
6207 case E_MIPS_ARCH_5:
6208 return bfd_mach_mips5;
b49e97c9
TS
6209
6210 case E_MIPS_ARCH_32:
6211 return bfd_mach_mipsisa32;
b49e97c9
TS
6212
6213 case E_MIPS_ARCH_64:
6214 return bfd_mach_mipsisa64;
af7ee8bf
CD
6215
6216 case E_MIPS_ARCH_32R2:
6217 return bfd_mach_mipsisa32r2;
5f74bc13
CD
6218
6219 case E_MIPS_ARCH_64R2:
6220 return bfd_mach_mipsisa64r2;
b49e97c9
TS
6221 }
6222 }
6223
6224 return 0;
6225}
6226
6227/* Return printable name for ABI. */
6228
6229static INLINE char *
9719ad41 6230elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
6231{
6232 flagword flags;
6233
6234 flags = elf_elfheader (abfd)->e_flags;
6235 switch (flags & EF_MIPS_ABI)
6236 {
6237 case 0:
6238 if (ABI_N32_P (abfd))
6239 return "N32";
6240 else if (ABI_64_P (abfd))
6241 return "64";
6242 else
6243 return "none";
6244 case E_MIPS_ABI_O32:
6245 return "O32";
6246 case E_MIPS_ABI_O64:
6247 return "O64";
6248 case E_MIPS_ABI_EABI32:
6249 return "EABI32";
6250 case E_MIPS_ABI_EABI64:
6251 return "EABI64";
6252 default:
6253 return "unknown abi";
6254 }
6255}
6256\f
6257/* MIPS ELF uses two common sections. One is the usual one, and the
6258 other is for small objects. All the small objects are kept
6259 together, and then referenced via the gp pointer, which yields
6260 faster assembler code. This is what we use for the small common
6261 section. This approach is copied from ecoff.c. */
6262static asection mips_elf_scom_section;
6263static asymbol mips_elf_scom_symbol;
6264static asymbol *mips_elf_scom_symbol_ptr;
6265
6266/* MIPS ELF also uses an acommon section, which represents an
6267 allocated common symbol which may be overridden by a
6268 definition in a shared library. */
6269static asection mips_elf_acom_section;
6270static asymbol mips_elf_acom_symbol;
6271static asymbol *mips_elf_acom_symbol_ptr;
6272
738e5348 6273/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
6274
6275void
9719ad41 6276_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
6277{
6278 elf_symbol_type *elfsym;
6279
738e5348 6280 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6281 elfsym = (elf_symbol_type *) asym;
6282 switch (elfsym->internal_elf_sym.st_shndx)
6283 {
6284 case SHN_MIPS_ACOMMON:
6285 /* This section is used in a dynamically linked executable file.
6286 It is an allocated common section. The dynamic linker can
6287 either resolve these symbols to something in a shared
6288 library, or it can just leave them here. For our purposes,
6289 we can consider these symbols to be in a new section. */
6290 if (mips_elf_acom_section.name == NULL)
6291 {
6292 /* Initialize the acommon section. */
6293 mips_elf_acom_section.name = ".acommon";
6294 mips_elf_acom_section.flags = SEC_ALLOC;
6295 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6296 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6297 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6298 mips_elf_acom_symbol.name = ".acommon";
6299 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6300 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6301 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6302 }
6303 asym->section = &mips_elf_acom_section;
6304 break;
6305
6306 case SHN_COMMON:
6307 /* Common symbols less than the GP size are automatically
6308 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6309 if (asym->value > elf_gp_size (abfd)
b59eed79 6310 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6311 || IRIX_COMPAT (abfd) == ict_irix6)
6312 break;
6313 /* Fall through. */
6314 case SHN_MIPS_SCOMMON:
6315 if (mips_elf_scom_section.name == NULL)
6316 {
6317 /* Initialize the small common section. */
6318 mips_elf_scom_section.name = ".scommon";
6319 mips_elf_scom_section.flags = SEC_IS_COMMON;
6320 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6321 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6322 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6323 mips_elf_scom_symbol.name = ".scommon";
6324 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6325 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6326 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6327 }
6328 asym->section = &mips_elf_scom_section;
6329 asym->value = elfsym->internal_elf_sym.st_size;
6330 break;
6331
6332 case SHN_MIPS_SUNDEFINED:
6333 asym->section = bfd_und_section_ptr;
6334 break;
6335
b49e97c9 6336 case SHN_MIPS_TEXT:
00b4930b
TS
6337 {
6338 asection *section = bfd_get_section_by_name (abfd, ".text");
6339
00b4930b
TS
6340 if (section != NULL)
6341 {
6342 asym->section = section;
6343 /* MIPS_TEXT is a bit special, the address is not an offset
6344 to the base of the .text section. So substract the section
6345 base address to make it an offset. */
6346 asym->value -= section->vma;
6347 }
6348 }
b49e97c9
TS
6349 break;
6350
6351 case SHN_MIPS_DATA:
00b4930b
TS
6352 {
6353 asection *section = bfd_get_section_by_name (abfd, ".data");
6354
00b4930b
TS
6355 if (section != NULL)
6356 {
6357 asym->section = section;
6358 /* MIPS_DATA is a bit special, the address is not an offset
6359 to the base of the .data section. So substract the section
6360 base address to make it an offset. */
6361 asym->value -= section->vma;
6362 }
6363 }
b49e97c9 6364 break;
b49e97c9 6365 }
738e5348 6366
df58fc94
RS
6367 /* If this is an odd-valued function symbol, assume it's a MIPS16
6368 or microMIPS one. */
738e5348
RS
6369 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6370 && (asym->value & 1) != 0)
6371 {
6372 asym->value--;
df58fc94
RS
6373 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
6374 elfsym->internal_elf_sym.st_other
6375 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6376 else
6377 elfsym->internal_elf_sym.st_other
6378 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
738e5348 6379 }
b49e97c9
TS
6380}
6381\f
8c946ed5
RS
6382/* Implement elf_backend_eh_frame_address_size. This differs from
6383 the default in the way it handles EABI64.
6384
6385 EABI64 was originally specified as an LP64 ABI, and that is what
6386 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6387 historically accepted the combination of -mabi=eabi and -mlong32,
6388 and this ILP32 variation has become semi-official over time.
6389 Both forms use elf32 and have pointer-sized FDE addresses.
6390
6391 If an EABI object was generated by GCC 4.0 or above, it will have
6392 an empty .gcc_compiled_longXX section, where XX is the size of longs
6393 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6394 have no special marking to distinguish them from LP64 objects.
6395
6396 We don't want users of the official LP64 ABI to be punished for the
6397 existence of the ILP32 variant, but at the same time, we don't want
6398 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6399 We therefore take the following approach:
6400
6401 - If ABFD contains a .gcc_compiled_longXX section, use it to
6402 determine the pointer size.
6403
6404 - Otherwise check the type of the first relocation. Assume that
6405 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6406
6407 - Otherwise punt.
6408
6409 The second check is enough to detect LP64 objects generated by pre-4.0
6410 compilers because, in the kind of output generated by those compilers,
6411 the first relocation will be associated with either a CIE personality
6412 routine or an FDE start address. Furthermore, the compilers never
6413 used a special (non-pointer) encoding for this ABI.
6414
6415 Checking the relocation type should also be safe because there is no
6416 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6417 did so. */
6418
6419unsigned int
6420_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6421{
6422 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6423 return 8;
6424 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6425 {
6426 bfd_boolean long32_p, long64_p;
6427
6428 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6429 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6430 if (long32_p && long64_p)
6431 return 0;
6432 if (long32_p)
6433 return 4;
6434 if (long64_p)
6435 return 8;
6436
6437 if (sec->reloc_count > 0
6438 && elf_section_data (sec)->relocs != NULL
6439 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6440 == R_MIPS_64))
6441 return 8;
6442
6443 return 0;
6444 }
6445 return 4;
6446}
6447\f
174fd7f9
RS
6448/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6449 relocations against two unnamed section symbols to resolve to the
6450 same address. For example, if we have code like:
6451
6452 lw $4,%got_disp(.data)($gp)
6453 lw $25,%got_disp(.text)($gp)
6454 jalr $25
6455
6456 then the linker will resolve both relocations to .data and the program
6457 will jump there rather than to .text.
6458
6459 We can work around this problem by giving names to local section symbols.
6460 This is also what the MIPSpro tools do. */
6461
6462bfd_boolean
6463_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6464{
6465 return SGI_COMPAT (abfd);
6466}
6467\f
b49e97c9
TS
6468/* Work over a section just before writing it out. This routine is
6469 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6470 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6471 a better way. */
6472
b34976b6 6473bfd_boolean
9719ad41 6474_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6475{
6476 if (hdr->sh_type == SHT_MIPS_REGINFO
6477 && hdr->sh_size > 0)
6478 {
6479 bfd_byte buf[4];
6480
6481 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6482 BFD_ASSERT (hdr->contents == NULL);
6483
6484 if (bfd_seek (abfd,
6485 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6486 SEEK_SET) != 0)
b34976b6 6487 return FALSE;
b49e97c9 6488 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6489 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6490 return FALSE;
b49e97c9
TS
6491 }
6492
6493 if (hdr->sh_type == SHT_MIPS_OPTIONS
6494 && hdr->bfd_section != NULL
f0abc2a1
AM
6495 && mips_elf_section_data (hdr->bfd_section) != NULL
6496 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6497 {
6498 bfd_byte *contents, *l, *lend;
6499
f0abc2a1
AM
6500 /* We stored the section contents in the tdata field in the
6501 set_section_contents routine. We save the section contents
6502 so that we don't have to read them again.
b49e97c9
TS
6503 At this point we know that elf_gp is set, so we can look
6504 through the section contents to see if there is an
6505 ODK_REGINFO structure. */
6506
f0abc2a1 6507 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6508 l = contents;
6509 lend = contents + hdr->sh_size;
6510 while (l + sizeof (Elf_External_Options) <= lend)
6511 {
6512 Elf_Internal_Options intopt;
6513
6514 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6515 &intopt);
1bc8074d
MR
6516 if (intopt.size < sizeof (Elf_External_Options))
6517 {
6518 (*_bfd_error_handler)
6519 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6520 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6521 break;
6522 }
b49e97c9
TS
6523 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6524 {
6525 bfd_byte buf[8];
6526
6527 if (bfd_seek (abfd,
6528 (hdr->sh_offset
6529 + (l - contents)
6530 + sizeof (Elf_External_Options)
6531 + (sizeof (Elf64_External_RegInfo) - 8)),
6532 SEEK_SET) != 0)
b34976b6 6533 return FALSE;
b49e97c9 6534 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6535 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6536 return FALSE;
b49e97c9
TS
6537 }
6538 else if (intopt.kind == ODK_REGINFO)
6539 {
6540 bfd_byte buf[4];
6541
6542 if (bfd_seek (abfd,
6543 (hdr->sh_offset
6544 + (l - contents)
6545 + sizeof (Elf_External_Options)
6546 + (sizeof (Elf32_External_RegInfo) - 4)),
6547 SEEK_SET) != 0)
b34976b6 6548 return FALSE;
b49e97c9 6549 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6550 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6551 return FALSE;
b49e97c9
TS
6552 }
6553 l += intopt.size;
6554 }
6555 }
6556
6557 if (hdr->bfd_section != NULL)
6558 {
6559 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6560
2d0f9ad9
JM
6561 /* .sbss is not handled specially here because the GNU/Linux
6562 prelinker can convert .sbss from NOBITS to PROGBITS and
6563 changing it back to NOBITS breaks the binary. The entry in
6564 _bfd_mips_elf_special_sections will ensure the correct flags
6565 are set on .sbss if BFD creates it without reading it from an
6566 input file, and without special handling here the flags set
6567 on it in an input file will be followed. */
b49e97c9
TS
6568 if (strcmp (name, ".sdata") == 0
6569 || strcmp (name, ".lit8") == 0
6570 || strcmp (name, ".lit4") == 0)
6571 {
6572 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6573 hdr->sh_type = SHT_PROGBITS;
6574 }
b49e97c9
TS
6575 else if (strcmp (name, ".srdata") == 0)
6576 {
6577 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6578 hdr->sh_type = SHT_PROGBITS;
6579 }
6580 else if (strcmp (name, ".compact_rel") == 0)
6581 {
6582 hdr->sh_flags = 0;
6583 hdr->sh_type = SHT_PROGBITS;
6584 }
6585 else if (strcmp (name, ".rtproc") == 0)
6586 {
6587 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6588 {
6589 unsigned int adjust;
6590
6591 adjust = hdr->sh_size % hdr->sh_addralign;
6592 if (adjust != 0)
6593 hdr->sh_size += hdr->sh_addralign - adjust;
6594 }
6595 }
6596 }
6597
b34976b6 6598 return TRUE;
b49e97c9
TS
6599}
6600
6601/* Handle a MIPS specific section when reading an object file. This
6602 is called when elfcode.h finds a section with an unknown type.
6603 This routine supports both the 32-bit and 64-bit ELF ABI.
6604
6605 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6606 how to. */
6607
b34976b6 6608bfd_boolean
6dc132d9
L
6609_bfd_mips_elf_section_from_shdr (bfd *abfd,
6610 Elf_Internal_Shdr *hdr,
6611 const char *name,
6612 int shindex)
b49e97c9
TS
6613{
6614 flagword flags = 0;
6615
6616 /* There ought to be a place to keep ELF backend specific flags, but
6617 at the moment there isn't one. We just keep track of the
6618 sections by their name, instead. Fortunately, the ABI gives
6619 suggested names for all the MIPS specific sections, so we will
6620 probably get away with this. */
6621 switch (hdr->sh_type)
6622 {
6623 case SHT_MIPS_LIBLIST:
6624 if (strcmp (name, ".liblist") != 0)
b34976b6 6625 return FALSE;
b49e97c9
TS
6626 break;
6627 case SHT_MIPS_MSYM:
6628 if (strcmp (name, ".msym") != 0)
b34976b6 6629 return FALSE;
b49e97c9
TS
6630 break;
6631 case SHT_MIPS_CONFLICT:
6632 if (strcmp (name, ".conflict") != 0)
b34976b6 6633 return FALSE;
b49e97c9
TS
6634 break;
6635 case SHT_MIPS_GPTAB:
0112cd26 6636 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6637 return FALSE;
b49e97c9
TS
6638 break;
6639 case SHT_MIPS_UCODE:
6640 if (strcmp (name, ".ucode") != 0)
b34976b6 6641 return FALSE;
b49e97c9
TS
6642 break;
6643 case SHT_MIPS_DEBUG:
6644 if (strcmp (name, ".mdebug") != 0)
b34976b6 6645 return FALSE;
b49e97c9
TS
6646 flags = SEC_DEBUGGING;
6647 break;
6648 case SHT_MIPS_REGINFO:
6649 if (strcmp (name, ".reginfo") != 0
6650 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6651 return FALSE;
b49e97c9
TS
6652 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6653 break;
6654 case SHT_MIPS_IFACE:
6655 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6656 return FALSE;
b49e97c9
TS
6657 break;
6658 case SHT_MIPS_CONTENT:
0112cd26 6659 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6660 return FALSE;
b49e97c9
TS
6661 break;
6662 case SHT_MIPS_OPTIONS:
cc2e31b9 6663 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6664 return FALSE;
b49e97c9
TS
6665 break;
6666 case SHT_MIPS_DWARF:
1b315056 6667 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6668 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6669 return FALSE;
b49e97c9
TS
6670 break;
6671 case SHT_MIPS_SYMBOL_LIB:
6672 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6673 return FALSE;
b49e97c9
TS
6674 break;
6675 case SHT_MIPS_EVENTS:
0112cd26
NC
6676 if (! CONST_STRNEQ (name, ".MIPS.events")
6677 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6678 return FALSE;
b49e97c9
TS
6679 break;
6680 default:
cc2e31b9 6681 break;
b49e97c9
TS
6682 }
6683
6dc132d9 6684 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6685 return FALSE;
b49e97c9
TS
6686
6687 if (flags)
6688 {
6689 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6690 (bfd_get_section_flags (abfd,
6691 hdr->bfd_section)
6692 | flags)))
b34976b6 6693 return FALSE;
b49e97c9
TS
6694 }
6695
6696 /* FIXME: We should record sh_info for a .gptab section. */
6697
6698 /* For a .reginfo section, set the gp value in the tdata information
6699 from the contents of this section. We need the gp value while
6700 processing relocs, so we just get it now. The .reginfo section
6701 is not used in the 64-bit MIPS ELF ABI. */
6702 if (hdr->sh_type == SHT_MIPS_REGINFO)
6703 {
6704 Elf32_External_RegInfo ext;
6705 Elf32_RegInfo s;
6706
9719ad41
RS
6707 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6708 &ext, 0, sizeof ext))
b34976b6 6709 return FALSE;
b49e97c9
TS
6710 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6711 elf_gp (abfd) = s.ri_gp_value;
6712 }
6713
6714 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6715 set the gp value based on what we find. We may see both
6716 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6717 they should agree. */
6718 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6719 {
6720 bfd_byte *contents, *l, *lend;
6721
9719ad41 6722 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6723 if (contents == NULL)
b34976b6 6724 return FALSE;
b49e97c9 6725 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6726 0, hdr->sh_size))
b49e97c9
TS
6727 {
6728 free (contents);
b34976b6 6729 return FALSE;
b49e97c9
TS
6730 }
6731 l = contents;
6732 lend = contents + hdr->sh_size;
6733 while (l + sizeof (Elf_External_Options) <= lend)
6734 {
6735 Elf_Internal_Options intopt;
6736
6737 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6738 &intopt);
1bc8074d
MR
6739 if (intopt.size < sizeof (Elf_External_Options))
6740 {
6741 (*_bfd_error_handler)
6742 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6743 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6744 break;
6745 }
b49e97c9
TS
6746 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6747 {
6748 Elf64_Internal_RegInfo intreg;
6749
6750 bfd_mips_elf64_swap_reginfo_in
6751 (abfd,
6752 ((Elf64_External_RegInfo *)
6753 (l + sizeof (Elf_External_Options))),
6754 &intreg);
6755 elf_gp (abfd) = intreg.ri_gp_value;
6756 }
6757 else if (intopt.kind == ODK_REGINFO)
6758 {
6759 Elf32_RegInfo intreg;
6760
6761 bfd_mips_elf32_swap_reginfo_in
6762 (abfd,
6763 ((Elf32_External_RegInfo *)
6764 (l + sizeof (Elf_External_Options))),
6765 &intreg);
6766 elf_gp (abfd) = intreg.ri_gp_value;
6767 }
6768 l += intopt.size;
6769 }
6770 free (contents);
6771 }
6772
b34976b6 6773 return TRUE;
b49e97c9
TS
6774}
6775
6776/* Set the correct type for a MIPS ELF section. We do this by the
6777 section name, which is a hack, but ought to work. This routine is
6778 used by both the 32-bit and the 64-bit ABI. */
6779
b34976b6 6780bfd_boolean
9719ad41 6781_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 6782{
0414f35b 6783 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
6784
6785 if (strcmp (name, ".liblist") == 0)
6786 {
6787 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 6788 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
6789 /* The sh_link field is set in final_write_processing. */
6790 }
6791 else if (strcmp (name, ".conflict") == 0)
6792 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 6793 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
6794 {
6795 hdr->sh_type = SHT_MIPS_GPTAB;
6796 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6797 /* The sh_info field is set in final_write_processing. */
6798 }
6799 else if (strcmp (name, ".ucode") == 0)
6800 hdr->sh_type = SHT_MIPS_UCODE;
6801 else if (strcmp (name, ".mdebug") == 0)
6802 {
6803 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 6804 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
6805 entsize of 0. FIXME: Does this matter? */
6806 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6807 hdr->sh_entsize = 0;
6808 else
6809 hdr->sh_entsize = 1;
6810 }
6811 else if (strcmp (name, ".reginfo") == 0)
6812 {
6813 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 6814 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
6815 entsize of 0x18. FIXME: Does this matter? */
6816 if (SGI_COMPAT (abfd))
6817 {
6818 if ((abfd->flags & DYNAMIC) != 0)
6819 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6820 else
6821 hdr->sh_entsize = 1;
6822 }
6823 else
6824 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6825 }
6826 else if (SGI_COMPAT (abfd)
6827 && (strcmp (name, ".hash") == 0
6828 || strcmp (name, ".dynamic") == 0
6829 || strcmp (name, ".dynstr") == 0))
6830 {
6831 if (SGI_COMPAT (abfd))
6832 hdr->sh_entsize = 0;
6833#if 0
8dc1a139 6834 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
6835 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6836#endif
6837 }
6838 else if (strcmp (name, ".got") == 0
6839 || strcmp (name, ".srdata") == 0
6840 || strcmp (name, ".sdata") == 0
6841 || strcmp (name, ".sbss") == 0
6842 || strcmp (name, ".lit4") == 0
6843 || strcmp (name, ".lit8") == 0)
6844 hdr->sh_flags |= SHF_MIPS_GPREL;
6845 else if (strcmp (name, ".MIPS.interfaces") == 0)
6846 {
6847 hdr->sh_type = SHT_MIPS_IFACE;
6848 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6849 }
0112cd26 6850 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
6851 {
6852 hdr->sh_type = SHT_MIPS_CONTENT;
6853 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6854 /* The sh_info field is set in final_write_processing. */
6855 }
cc2e31b9 6856 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
6857 {
6858 hdr->sh_type = SHT_MIPS_OPTIONS;
6859 hdr->sh_entsize = 1;
6860 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6861 }
1b315056
CS
6862 else if (CONST_STRNEQ (name, ".debug_")
6863 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
6864 {
6865 hdr->sh_type = SHT_MIPS_DWARF;
6866
6867 /* Irix facilities such as libexc expect a single .debug_frame
6868 per executable, the system ones have NOSTRIP set and the linker
6869 doesn't merge sections with different flags so ... */
6870 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6871 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6872 }
b49e97c9
TS
6873 else if (strcmp (name, ".MIPS.symlib") == 0)
6874 {
6875 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6876 /* The sh_link and sh_info fields are set in
6877 final_write_processing. */
6878 }
0112cd26
NC
6879 else if (CONST_STRNEQ (name, ".MIPS.events")
6880 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
6881 {
6882 hdr->sh_type = SHT_MIPS_EVENTS;
6883 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6884 /* The sh_link field is set in final_write_processing. */
6885 }
6886 else if (strcmp (name, ".msym") == 0)
6887 {
6888 hdr->sh_type = SHT_MIPS_MSYM;
6889 hdr->sh_flags |= SHF_ALLOC;
6890 hdr->sh_entsize = 8;
6891 }
6892
7a79a000
TS
6893 /* The generic elf_fake_sections will set up REL_HDR using the default
6894 kind of relocations. We used to set up a second header for the
6895 non-default kind of relocations here, but only NewABI would use
6896 these, and the IRIX ld doesn't like resulting empty RELA sections.
6897 Thus we create those header only on demand now. */
b49e97c9 6898
b34976b6 6899 return TRUE;
b49e97c9
TS
6900}
6901
6902/* Given a BFD section, try to locate the corresponding ELF section
6903 index. This is used by both the 32-bit and the 64-bit ABI.
6904 Actually, it's not clear to me that the 64-bit ABI supports these,
6905 but for non-PIC objects we will certainly want support for at least
6906 the .scommon section. */
6907
b34976b6 6908bfd_boolean
9719ad41
RS
6909_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6910 asection *sec, int *retval)
b49e97c9
TS
6911{
6912 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6913 {
6914 *retval = SHN_MIPS_SCOMMON;
b34976b6 6915 return TRUE;
b49e97c9
TS
6916 }
6917 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6918 {
6919 *retval = SHN_MIPS_ACOMMON;
b34976b6 6920 return TRUE;
b49e97c9 6921 }
b34976b6 6922 return FALSE;
b49e97c9
TS
6923}
6924\f
6925/* Hook called by the linker routine which adds symbols from an object
6926 file. We must handle the special MIPS section numbers here. */
6927
b34976b6 6928bfd_boolean
9719ad41 6929_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 6930 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
6931 flagword *flagsp ATTRIBUTE_UNUSED,
6932 asection **secp, bfd_vma *valp)
b49e97c9
TS
6933{
6934 if (SGI_COMPAT (abfd)
6935 && (abfd->flags & DYNAMIC) != 0
6936 && strcmp (*namep, "_rld_new_interface") == 0)
6937 {
8dc1a139 6938 /* Skip IRIX5 rld entry name. */
b49e97c9 6939 *namep = NULL;
b34976b6 6940 return TRUE;
b49e97c9
TS
6941 }
6942
eedecc07
DD
6943 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6944 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6945 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6946 a magic symbol resolved by the linker, we ignore this bogus definition
6947 of _gp_disp. New ABI objects do not suffer from this problem so this
6948 is not done for them. */
6949 if (!NEWABI_P(abfd)
6950 && (sym->st_shndx == SHN_ABS)
6951 && (strcmp (*namep, "_gp_disp") == 0))
6952 {
6953 *namep = NULL;
6954 return TRUE;
6955 }
6956
b49e97c9
TS
6957 switch (sym->st_shndx)
6958 {
6959 case SHN_COMMON:
6960 /* Common symbols less than the GP size are automatically
6961 treated as SHN_MIPS_SCOMMON symbols. */
6962 if (sym->st_size > elf_gp_size (abfd)
b59eed79 6963 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
6964 || IRIX_COMPAT (abfd) == ict_irix6)
6965 break;
6966 /* Fall through. */
6967 case SHN_MIPS_SCOMMON:
6968 *secp = bfd_make_section_old_way (abfd, ".scommon");
6969 (*secp)->flags |= SEC_IS_COMMON;
6970 *valp = sym->st_size;
6971 break;
6972
6973 case SHN_MIPS_TEXT:
6974 /* This section is used in a shared object. */
6975 if (elf_tdata (abfd)->elf_text_section == NULL)
6976 {
6977 asymbol *elf_text_symbol;
6978 asection *elf_text_section;
6979 bfd_size_type amt = sizeof (asection);
6980
6981 elf_text_section = bfd_zalloc (abfd, amt);
6982 if (elf_text_section == NULL)
b34976b6 6983 return FALSE;
b49e97c9
TS
6984
6985 amt = sizeof (asymbol);
6986 elf_text_symbol = bfd_zalloc (abfd, amt);
6987 if (elf_text_symbol == NULL)
b34976b6 6988 return FALSE;
b49e97c9
TS
6989
6990 /* Initialize the section. */
6991
6992 elf_tdata (abfd)->elf_text_section = elf_text_section;
6993 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6994
6995 elf_text_section->symbol = elf_text_symbol;
6996 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6997
6998 elf_text_section->name = ".text";
6999 elf_text_section->flags = SEC_NO_FLAGS;
7000 elf_text_section->output_section = NULL;
7001 elf_text_section->owner = abfd;
7002 elf_text_symbol->name = ".text";
7003 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7004 elf_text_symbol->section = elf_text_section;
7005 }
7006 /* This code used to do *secp = bfd_und_section_ptr if
7007 info->shared. I don't know why, and that doesn't make sense,
7008 so I took it out. */
7009 *secp = elf_tdata (abfd)->elf_text_section;
7010 break;
7011
7012 case SHN_MIPS_ACOMMON:
7013 /* Fall through. XXX Can we treat this as allocated data? */
7014 case SHN_MIPS_DATA:
7015 /* This section is used in a shared object. */
7016 if (elf_tdata (abfd)->elf_data_section == NULL)
7017 {
7018 asymbol *elf_data_symbol;
7019 asection *elf_data_section;
7020 bfd_size_type amt = sizeof (asection);
7021
7022 elf_data_section = bfd_zalloc (abfd, amt);
7023 if (elf_data_section == NULL)
b34976b6 7024 return FALSE;
b49e97c9
TS
7025
7026 amt = sizeof (asymbol);
7027 elf_data_symbol = bfd_zalloc (abfd, amt);
7028 if (elf_data_symbol == NULL)
b34976b6 7029 return FALSE;
b49e97c9
TS
7030
7031 /* Initialize the section. */
7032
7033 elf_tdata (abfd)->elf_data_section = elf_data_section;
7034 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7035
7036 elf_data_section->symbol = elf_data_symbol;
7037 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
7038
7039 elf_data_section->name = ".data";
7040 elf_data_section->flags = SEC_NO_FLAGS;
7041 elf_data_section->output_section = NULL;
7042 elf_data_section->owner = abfd;
7043 elf_data_symbol->name = ".data";
7044 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7045 elf_data_symbol->section = elf_data_section;
7046 }
7047 /* This code used to do *secp = bfd_und_section_ptr if
7048 info->shared. I don't know why, and that doesn't make sense,
7049 so I took it out. */
7050 *secp = elf_tdata (abfd)->elf_data_section;
7051 break;
7052
7053 case SHN_MIPS_SUNDEFINED:
7054 *secp = bfd_und_section_ptr;
7055 break;
7056 }
7057
7058 if (SGI_COMPAT (abfd)
7059 && ! info->shared
f13a99db 7060 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
7061 && strcmp (*namep, "__rld_obj_head") == 0)
7062 {
7063 struct elf_link_hash_entry *h;
14a793b2 7064 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7065
7066 /* Mark __rld_obj_head as dynamic. */
14a793b2 7067 bh = NULL;
b49e97c9 7068 if (! (_bfd_generic_link_add_one_symbol
9719ad41 7069 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 7070 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7071 return FALSE;
14a793b2
AM
7072
7073 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7074 h->non_elf = 0;
7075 h->def_regular = 1;
b49e97c9
TS
7076 h->type = STT_OBJECT;
7077
c152c796 7078 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7079 return FALSE;
b49e97c9 7080
b34976b6 7081 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b49e97c9
TS
7082 }
7083
7084 /* If this is a mips16 text symbol, add 1 to the value to make it
7085 odd. This will cause something like .word SYM to come up with
7086 the right value when it is loaded into the PC. */
df58fc94 7087 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
7088 ++*valp;
7089
b34976b6 7090 return TRUE;
b49e97c9
TS
7091}
7092
7093/* This hook function is called before the linker writes out a global
7094 symbol. We mark symbols as small common if appropriate. This is
7095 also where we undo the increment of the value for a mips16 symbol. */
7096
6e0b88f1 7097int
9719ad41
RS
7098_bfd_mips_elf_link_output_symbol_hook
7099 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7100 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7101 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
7102{
7103 /* If we see a common symbol, which implies a relocatable link, then
7104 if a symbol was small common in an input file, mark it as small
7105 common in the output file. */
7106 if (sym->st_shndx == SHN_COMMON
7107 && strcmp (input_sec->name, ".scommon") == 0)
7108 sym->st_shndx = SHN_MIPS_SCOMMON;
7109
df58fc94 7110 if (ELF_ST_IS_COMPRESSED (sym->st_other))
79cda7cf 7111 sym->st_value &= ~1;
b49e97c9 7112
6e0b88f1 7113 return 1;
b49e97c9
TS
7114}
7115\f
7116/* Functions for the dynamic linker. */
7117
7118/* Create dynamic sections when linking against a dynamic object. */
7119
b34976b6 7120bfd_boolean
9719ad41 7121_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
7122{
7123 struct elf_link_hash_entry *h;
14a793b2 7124 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7125 flagword flags;
7126 register asection *s;
7127 const char * const *namep;
0a44bf69 7128 struct mips_elf_link_hash_table *htab;
b49e97c9 7129
0a44bf69 7130 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7131 BFD_ASSERT (htab != NULL);
7132
b49e97c9
TS
7133 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7134 | SEC_LINKER_CREATED | SEC_READONLY);
7135
0a44bf69
RS
7136 /* The psABI requires a read-only .dynamic section, but the VxWorks
7137 EABI doesn't. */
7138 if (!htab->is_vxworks)
b49e97c9 7139 {
0a44bf69
RS
7140 s = bfd_get_section_by_name (abfd, ".dynamic");
7141 if (s != NULL)
7142 {
7143 if (! bfd_set_section_flags (abfd, s, flags))
7144 return FALSE;
7145 }
b49e97c9
TS
7146 }
7147
7148 /* We need to create .got section. */
23cc69b6 7149 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
7150 return FALSE;
7151
0a44bf69 7152 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 7153 return FALSE;
b49e97c9 7154
b49e97c9 7155 /* Create .stub section. */
4e41d0d7
RS
7156 s = bfd_make_section_with_flags (abfd,
7157 MIPS_ELF_STUB_SECTION_NAME (abfd),
7158 flags | SEC_CODE);
7159 if (s == NULL
7160 || ! bfd_set_section_alignment (abfd, s,
7161 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7162 return FALSE;
7163 htab->sstubs = s;
b49e97c9
TS
7164
7165 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
7166 && !info->shared
7167 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
7168 {
3496cb2a
L
7169 s = bfd_make_section_with_flags (abfd, ".rld_map",
7170 flags &~ (flagword) SEC_READONLY);
b49e97c9 7171 if (s == NULL
b49e97c9
TS
7172 || ! bfd_set_section_alignment (abfd, s,
7173 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 7174 return FALSE;
b49e97c9
TS
7175 }
7176
7177 /* On IRIX5, we adjust add some additional symbols and change the
7178 alignments of several sections. There is no ABI documentation
7179 indicating that this is necessary on IRIX6, nor any evidence that
7180 the linker takes such action. */
7181 if (IRIX_COMPAT (abfd) == ict_irix5)
7182 {
7183 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7184 {
14a793b2 7185 bh = NULL;
b49e97c9 7186 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
7187 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7188 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7189 return FALSE;
14a793b2
AM
7190
7191 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7192 h->non_elf = 0;
7193 h->def_regular = 1;
b49e97c9
TS
7194 h->type = STT_SECTION;
7195
c152c796 7196 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7197 return FALSE;
b49e97c9
TS
7198 }
7199
7200 /* We need to create a .compact_rel section. */
7201 if (SGI_COMPAT (abfd))
7202 {
7203 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 7204 return FALSE;
b49e97c9
TS
7205 }
7206
44c410de 7207 /* Change alignments of some sections. */
b49e97c9
TS
7208 s = bfd_get_section_by_name (abfd, ".hash");
7209 if (s != NULL)
d80dcc6a 7210 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7211 s = bfd_get_section_by_name (abfd, ".dynsym");
7212 if (s != NULL)
d80dcc6a 7213 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7214 s = bfd_get_section_by_name (abfd, ".dynstr");
7215 if (s != NULL)
d80dcc6a 7216 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7217 s = bfd_get_section_by_name (abfd, ".reginfo");
7218 if (s != NULL)
d80dcc6a 7219 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7220 s = bfd_get_section_by_name (abfd, ".dynamic");
7221 if (s != NULL)
d80dcc6a 7222 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7223 }
7224
7225 if (!info->shared)
7226 {
14a793b2
AM
7227 const char *name;
7228
7229 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7230 bh = NULL;
7231 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
7232 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7233 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7234 return FALSE;
14a793b2
AM
7235
7236 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7237 h->non_elf = 0;
7238 h->def_regular = 1;
b49e97c9
TS
7239 h->type = STT_SECTION;
7240
c152c796 7241 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7242 return FALSE;
b49e97c9
TS
7243
7244 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7245 {
7246 /* __rld_map is a four byte word located in the .data section
7247 and is filled in by the rtld to contain a pointer to
7248 the _r_debug structure. Its symbol value will be set in
7249 _bfd_mips_elf_finish_dynamic_symbol. */
7250 s = bfd_get_section_by_name (abfd, ".rld_map");
0abfb97a 7251 BFD_ASSERT (s != NULL);
14a793b2 7252
0abfb97a
L
7253 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7254 bh = NULL;
7255 if (!(_bfd_generic_link_add_one_symbol
7256 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7257 get_elf_backend_data (abfd)->collect, &bh)))
7258 return FALSE;
b49e97c9 7259
0abfb97a
L
7260 h = (struct elf_link_hash_entry *) bh;
7261 h->non_elf = 0;
7262 h->def_regular = 1;
7263 h->type = STT_OBJECT;
7264
7265 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7266 return FALSE;
b49e97c9
TS
7267 }
7268 }
7269
861fb55a
DJ
7270 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7271 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7272 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7273 return FALSE;
7274
7275 /* Cache the sections created above. */
7276 htab->splt = bfd_get_section_by_name (abfd, ".plt");
7277 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
0a44bf69
RS
7278 if (htab->is_vxworks)
7279 {
0a44bf69
RS
7280 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7281 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
861fb55a
DJ
7282 }
7283 else
7284 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7285 if (!htab->sdynbss
7286 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7287 || !htab->srelplt
7288 || !htab->splt)
7289 abort ();
0a44bf69 7290
861fb55a
DJ
7291 if (htab->is_vxworks)
7292 {
0a44bf69
RS
7293 /* Do the usual VxWorks handling. */
7294 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7295 return FALSE;
7296
7297 /* Work out the PLT sizes. */
7298 if (info->shared)
7299 {
7300 htab->plt_header_size
7301 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7302 htab->plt_entry_size
7303 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7304 }
7305 else
7306 {
7307 htab->plt_header_size
7308 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7309 htab->plt_entry_size
7310 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7311 }
7312 }
861fb55a
DJ
7313 else if (!info->shared)
7314 {
7315 /* All variants of the plt0 entry are the same size. */
7316 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7317 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7318 }
0a44bf69 7319
b34976b6 7320 return TRUE;
b49e97c9
TS
7321}
7322\f
c224138d
RS
7323/* Return true if relocation REL against section SEC is a REL rather than
7324 RELA relocation. RELOCS is the first relocation in the section and
7325 ABFD is the bfd that contains SEC. */
7326
7327static bfd_boolean
7328mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7329 const Elf_Internal_Rela *relocs,
7330 const Elf_Internal_Rela *rel)
7331{
7332 Elf_Internal_Shdr *rel_hdr;
7333 const struct elf_backend_data *bed;
7334
d4730f92
BS
7335 /* To determine which flavor of relocation this is, we depend on the
7336 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7337 rel_hdr = elf_section_data (sec)->rel.hdr;
7338 if (rel_hdr == NULL)
7339 return FALSE;
c224138d 7340 bed = get_elf_backend_data (abfd);
d4730f92
BS
7341 return ((size_t) (rel - relocs)
7342 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
c224138d
RS
7343}
7344
7345/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7346 HOWTO is the relocation's howto and CONTENTS points to the contents
7347 of the section that REL is against. */
7348
7349static bfd_vma
7350mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7351 reloc_howto_type *howto, bfd_byte *contents)
7352{
7353 bfd_byte *location;
7354 unsigned int r_type;
7355 bfd_vma addend;
7356
7357 r_type = ELF_R_TYPE (abfd, rel->r_info);
7358 location = contents + rel->r_offset;
7359
7360 /* Get the addend, which is stored in the input file. */
df58fc94 7361 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
c224138d 7362 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
df58fc94 7363 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
c224138d
RS
7364
7365 return addend & howto->src_mask;
7366}
7367
7368/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7369 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7370 and update *ADDEND with the final addend. Return true on success
7371 or false if the LO16 could not be found. RELEND is the exclusive
7372 upper bound on the relocations for REL's section. */
7373
7374static bfd_boolean
7375mips_elf_add_lo16_rel_addend (bfd *abfd,
7376 const Elf_Internal_Rela *rel,
7377 const Elf_Internal_Rela *relend,
7378 bfd_byte *contents, bfd_vma *addend)
7379{
7380 unsigned int r_type, lo16_type;
7381 const Elf_Internal_Rela *lo16_relocation;
7382 reloc_howto_type *lo16_howto;
7383 bfd_vma l;
7384
7385 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7386 if (mips16_reloc_p (r_type))
c224138d 7387 lo16_type = R_MIPS16_LO16;
df58fc94
RS
7388 else if (micromips_reloc_p (r_type))
7389 lo16_type = R_MICROMIPS_LO16;
c224138d
RS
7390 else
7391 lo16_type = R_MIPS_LO16;
7392
7393 /* The combined value is the sum of the HI16 addend, left-shifted by
7394 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7395 code does a `lui' of the HI16 value, and then an `addiu' of the
7396 LO16 value.)
7397
7398 Scan ahead to find a matching LO16 relocation.
7399
7400 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7401 be immediately following. However, for the IRIX6 ABI, the next
7402 relocation may be a composed relocation consisting of several
7403 relocations for the same address. In that case, the R_MIPS_LO16
7404 relocation may occur as one of these. We permit a similar
7405 extension in general, as that is useful for GCC.
7406
7407 In some cases GCC dead code elimination removes the LO16 but keeps
7408 the corresponding HI16. This is strictly speaking a violation of
7409 the ABI but not immediately harmful. */
7410 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7411 if (lo16_relocation == NULL)
7412 return FALSE;
7413
7414 /* Obtain the addend kept there. */
7415 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7416 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7417
7418 l <<= lo16_howto->rightshift;
7419 l = _bfd_mips_elf_sign_extend (l, 16);
7420
7421 *addend <<= 16;
7422 *addend += l;
7423 return TRUE;
7424}
7425
7426/* Try to read the contents of section SEC in bfd ABFD. Return true and
7427 store the contents in *CONTENTS on success. Assume that *CONTENTS
7428 already holds the contents if it is nonull on entry. */
7429
7430static bfd_boolean
7431mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7432{
7433 if (*contents)
7434 return TRUE;
7435
7436 /* Get cached copy if it exists. */
7437 if (elf_section_data (sec)->this_hdr.contents != NULL)
7438 {
7439 *contents = elf_section_data (sec)->this_hdr.contents;
7440 return TRUE;
7441 }
7442
7443 return bfd_malloc_and_get_section (abfd, sec, contents);
7444}
7445
b49e97c9
TS
7446/* Look through the relocs for a section during the first phase, and
7447 allocate space in the global offset table. */
7448
b34976b6 7449bfd_boolean
9719ad41
RS
7450_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7451 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7452{
7453 const char *name;
7454 bfd *dynobj;
7455 Elf_Internal_Shdr *symtab_hdr;
7456 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7457 size_t extsymoff;
7458 const Elf_Internal_Rela *rel;
7459 const Elf_Internal_Rela *rel_end;
b49e97c9 7460 asection *sreloc;
9c5bfbb7 7461 const struct elf_backend_data *bed;
0a44bf69 7462 struct mips_elf_link_hash_table *htab;
c224138d
RS
7463 bfd_byte *contents;
7464 bfd_vma addend;
7465 reloc_howto_type *howto;
b49e97c9 7466
1049f94e 7467 if (info->relocatable)
b34976b6 7468 return TRUE;
b49e97c9 7469
0a44bf69 7470 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7471 BFD_ASSERT (htab != NULL);
7472
b49e97c9
TS
7473 dynobj = elf_hash_table (info)->dynobj;
7474 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7475 sym_hashes = elf_sym_hashes (abfd);
7476 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7477
738e5348
RS
7478 bed = get_elf_backend_data (abfd);
7479 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7480
b49e97c9
TS
7481 /* Check for the mips16 stub sections. */
7482
7483 name = bfd_get_section_name (abfd, sec);
b9d58d71 7484 if (FN_STUB_P (name))
b49e97c9
TS
7485 {
7486 unsigned long r_symndx;
7487
7488 /* Look at the relocation information to figure out which symbol
7489 this is for. */
7490
738e5348
RS
7491 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7492 if (r_symndx == 0)
7493 {
7494 (*_bfd_error_handler)
7495 (_("%B: Warning: cannot determine the target function for"
7496 " stub section `%s'"),
7497 abfd, name);
7498 bfd_set_error (bfd_error_bad_value);
7499 return FALSE;
7500 }
b49e97c9
TS
7501
7502 if (r_symndx < extsymoff
7503 || sym_hashes[r_symndx - extsymoff] == NULL)
7504 {
7505 asection *o;
7506
7507 /* This stub is for a local symbol. This stub will only be
7508 needed if there is some relocation in this BFD, other
7509 than a 16 bit function call, which refers to this symbol. */
7510 for (o = abfd->sections; o != NULL; o = o->next)
7511 {
7512 Elf_Internal_Rela *sec_relocs;
7513 const Elf_Internal_Rela *r, *rend;
7514
7515 /* We can ignore stub sections when looking for relocs. */
7516 if ((o->flags & SEC_RELOC) == 0
7517 || o->reloc_count == 0
738e5348 7518 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7519 continue;
7520
45d6a902 7521 sec_relocs
9719ad41 7522 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7523 info->keep_memory);
b49e97c9 7524 if (sec_relocs == NULL)
b34976b6 7525 return FALSE;
b49e97c9
TS
7526
7527 rend = sec_relocs + o->reloc_count;
7528 for (r = sec_relocs; r < rend; r++)
7529 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7530 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7531 break;
7532
6cdc0ccc 7533 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7534 free (sec_relocs);
7535
7536 if (r < rend)
7537 break;
7538 }
7539
7540 if (o == NULL)
7541 {
7542 /* There is no non-call reloc for this stub, so we do
7543 not need it. Since this function is called before
7544 the linker maps input sections to output sections, we
7545 can easily discard it by setting the SEC_EXCLUDE
7546 flag. */
7547 sec->flags |= SEC_EXCLUDE;
b34976b6 7548 return TRUE;
b49e97c9
TS
7549 }
7550
7551 /* Record this stub in an array of local symbol stubs for
7552 this BFD. */
7553 if (elf_tdata (abfd)->local_stubs == NULL)
7554 {
7555 unsigned long symcount;
7556 asection **n;
7557 bfd_size_type amt;
7558
7559 if (elf_bad_symtab (abfd))
7560 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7561 else
7562 symcount = symtab_hdr->sh_info;
7563 amt = symcount * sizeof (asection *);
9719ad41 7564 n = bfd_zalloc (abfd, amt);
b49e97c9 7565 if (n == NULL)
b34976b6 7566 return FALSE;
b49e97c9
TS
7567 elf_tdata (abfd)->local_stubs = n;
7568 }
7569
b9d58d71 7570 sec->flags |= SEC_KEEP;
b49e97c9
TS
7571 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7572
7573 /* We don't need to set mips16_stubs_seen in this case.
7574 That flag is used to see whether we need to look through
7575 the global symbol table for stubs. We don't need to set
7576 it here, because we just have a local stub. */
7577 }
7578 else
7579 {
7580 struct mips_elf_link_hash_entry *h;
7581
7582 h = ((struct mips_elf_link_hash_entry *)
7583 sym_hashes[r_symndx - extsymoff]);
7584
973a3492
L
7585 while (h->root.root.type == bfd_link_hash_indirect
7586 || h->root.root.type == bfd_link_hash_warning)
7587 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7588
b49e97c9
TS
7589 /* H is the symbol this stub is for. */
7590
b9d58d71
TS
7591 /* If we already have an appropriate stub for this function, we
7592 don't need another one, so we can discard this one. Since
7593 this function is called before the linker maps input sections
7594 to output sections, we can easily discard it by setting the
7595 SEC_EXCLUDE flag. */
7596 if (h->fn_stub != NULL)
7597 {
7598 sec->flags |= SEC_EXCLUDE;
7599 return TRUE;
7600 }
7601
7602 sec->flags |= SEC_KEEP;
b49e97c9 7603 h->fn_stub = sec;
b34976b6 7604 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7605 }
7606 }
b9d58d71 7607 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7608 {
7609 unsigned long r_symndx;
7610 struct mips_elf_link_hash_entry *h;
7611 asection **loc;
7612
7613 /* Look at the relocation information to figure out which symbol
7614 this is for. */
7615
738e5348
RS
7616 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7617 if (r_symndx == 0)
7618 {
7619 (*_bfd_error_handler)
7620 (_("%B: Warning: cannot determine the target function for"
7621 " stub section `%s'"),
7622 abfd, name);
7623 bfd_set_error (bfd_error_bad_value);
7624 return FALSE;
7625 }
b49e97c9
TS
7626
7627 if (r_symndx < extsymoff
7628 || sym_hashes[r_symndx - extsymoff] == NULL)
7629 {
b9d58d71 7630 asection *o;
b49e97c9 7631
b9d58d71
TS
7632 /* This stub is for a local symbol. This stub will only be
7633 needed if there is some relocation (R_MIPS16_26) in this BFD
7634 that refers to this symbol. */
7635 for (o = abfd->sections; o != NULL; o = o->next)
7636 {
7637 Elf_Internal_Rela *sec_relocs;
7638 const Elf_Internal_Rela *r, *rend;
7639
7640 /* We can ignore stub sections when looking for relocs. */
7641 if ((o->flags & SEC_RELOC) == 0
7642 || o->reloc_count == 0
738e5348 7643 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7644 continue;
7645
7646 sec_relocs
7647 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7648 info->keep_memory);
7649 if (sec_relocs == NULL)
7650 return FALSE;
7651
7652 rend = sec_relocs + o->reloc_count;
7653 for (r = sec_relocs; r < rend; r++)
7654 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7655 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7656 break;
7657
7658 if (elf_section_data (o)->relocs != sec_relocs)
7659 free (sec_relocs);
7660
7661 if (r < rend)
7662 break;
7663 }
7664
7665 if (o == NULL)
7666 {
7667 /* There is no non-call reloc for this stub, so we do
7668 not need it. Since this function is called before
7669 the linker maps input sections to output sections, we
7670 can easily discard it by setting the SEC_EXCLUDE
7671 flag. */
7672 sec->flags |= SEC_EXCLUDE;
7673 return TRUE;
7674 }
7675
7676 /* Record this stub in an array of local symbol call_stubs for
7677 this BFD. */
7678 if (elf_tdata (abfd)->local_call_stubs == NULL)
7679 {
7680 unsigned long symcount;
7681 asection **n;
7682 bfd_size_type amt;
7683
7684 if (elf_bad_symtab (abfd))
7685 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7686 else
7687 symcount = symtab_hdr->sh_info;
7688 amt = symcount * sizeof (asection *);
7689 n = bfd_zalloc (abfd, amt);
7690 if (n == NULL)
7691 return FALSE;
7692 elf_tdata (abfd)->local_call_stubs = n;
7693 }
b49e97c9 7694
b9d58d71
TS
7695 sec->flags |= SEC_KEEP;
7696 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7697
b9d58d71
TS
7698 /* We don't need to set mips16_stubs_seen in this case.
7699 That flag is used to see whether we need to look through
7700 the global symbol table for stubs. We don't need to set
7701 it here, because we just have a local stub. */
7702 }
b49e97c9 7703 else
b49e97c9 7704 {
b9d58d71
TS
7705 h = ((struct mips_elf_link_hash_entry *)
7706 sym_hashes[r_symndx - extsymoff]);
7707
7708 /* H is the symbol this stub is for. */
7709
7710 if (CALL_FP_STUB_P (name))
7711 loc = &h->call_fp_stub;
7712 else
7713 loc = &h->call_stub;
7714
7715 /* If we already have an appropriate stub for this function, we
7716 don't need another one, so we can discard this one. Since
7717 this function is called before the linker maps input sections
7718 to output sections, we can easily discard it by setting the
7719 SEC_EXCLUDE flag. */
7720 if (*loc != NULL)
7721 {
7722 sec->flags |= SEC_EXCLUDE;
7723 return TRUE;
7724 }
b49e97c9 7725
b9d58d71
TS
7726 sec->flags |= SEC_KEEP;
7727 *loc = sec;
7728 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7729 }
b49e97c9
TS
7730 }
7731
b49e97c9 7732 sreloc = NULL;
c224138d 7733 contents = NULL;
b49e97c9
TS
7734 for (rel = relocs; rel < rel_end; ++rel)
7735 {
7736 unsigned long r_symndx;
7737 unsigned int r_type;
7738 struct elf_link_hash_entry *h;
861fb55a 7739 bfd_boolean can_make_dynamic_p;
b49e97c9
TS
7740
7741 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7742 r_type = ELF_R_TYPE (abfd, rel->r_info);
7743
7744 if (r_symndx < extsymoff)
7745 h = NULL;
7746 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7747 {
7748 (*_bfd_error_handler)
d003868e
AM
7749 (_("%B: Malformed reloc detected for section %s"),
7750 abfd, name);
b49e97c9 7751 bfd_set_error (bfd_error_bad_value);
b34976b6 7752 return FALSE;
b49e97c9
TS
7753 }
7754 else
7755 {
7756 h = sym_hashes[r_symndx - extsymoff];
3e08fb72
NC
7757 while (h != NULL
7758 && (h->root.type == bfd_link_hash_indirect
7759 || h->root.type == bfd_link_hash_warning))
861fb55a
DJ
7760 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7761 }
b49e97c9 7762
861fb55a
DJ
7763 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7764 relocation into a dynamic one. */
7765 can_make_dynamic_p = FALSE;
7766 switch (r_type)
7767 {
7768 case R_MIPS16_GOT16:
7769 case R_MIPS16_CALL16:
7770 case R_MIPS_GOT16:
7771 case R_MIPS_CALL16:
7772 case R_MIPS_CALL_HI16:
7773 case R_MIPS_CALL_LO16:
7774 case R_MIPS_GOT_HI16:
7775 case R_MIPS_GOT_LO16:
7776 case R_MIPS_GOT_PAGE:
7777 case R_MIPS_GOT_OFST:
7778 case R_MIPS_GOT_DISP:
7779 case R_MIPS_TLS_GOTTPREL:
7780 case R_MIPS_TLS_GD:
7781 case R_MIPS_TLS_LDM:
df58fc94
RS
7782 case R_MICROMIPS_GOT16:
7783 case R_MICROMIPS_CALL16:
7784 case R_MICROMIPS_CALL_HI16:
7785 case R_MICROMIPS_CALL_LO16:
7786 case R_MICROMIPS_GOT_HI16:
7787 case R_MICROMIPS_GOT_LO16:
7788 case R_MICROMIPS_GOT_PAGE:
7789 case R_MICROMIPS_GOT_OFST:
7790 case R_MICROMIPS_GOT_DISP:
7791 case R_MICROMIPS_TLS_GOTTPREL:
7792 case R_MICROMIPS_TLS_GD:
7793 case R_MICROMIPS_TLS_LDM:
861fb55a
DJ
7794 if (dynobj == NULL)
7795 elf_hash_table (info)->dynobj = dynobj = abfd;
7796 if (!mips_elf_create_got_section (dynobj, info))
7797 return FALSE;
7798 if (htab->is_vxworks && !info->shared)
b49e97c9 7799 {
861fb55a
DJ
7800 (*_bfd_error_handler)
7801 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7802 abfd, (unsigned long) rel->r_offset);
7803 bfd_set_error (bfd_error_bad_value);
7804 return FALSE;
b49e97c9 7805 }
861fb55a 7806 break;
b49e97c9 7807
99da6b5f
AN
7808 /* This is just a hint; it can safely be ignored. Don't set
7809 has_static_relocs for the corresponding symbol. */
7810 case R_MIPS_JALR:
df58fc94 7811 case R_MICROMIPS_JALR:
99da6b5f
AN
7812 break;
7813
861fb55a
DJ
7814 case R_MIPS_32:
7815 case R_MIPS_REL32:
7816 case R_MIPS_64:
7817 /* In VxWorks executables, references to external symbols
7818 must be handled using copy relocs or PLT entries; it is not
7819 possible to convert this relocation into a dynamic one.
7820
7821 For executables that use PLTs and copy-relocs, we have a
7822 choice between converting the relocation into a dynamic
7823 one or using copy relocations or PLT entries. It is
7824 usually better to do the former, unless the relocation is
7825 against a read-only section. */
7826 if ((info->shared
7827 || (h != NULL
7828 && !htab->is_vxworks
7829 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7830 && !(!info->nocopyreloc
7831 && !PIC_OBJECT_P (abfd)
7832 && MIPS_ELF_READONLY_SECTION (sec))))
7833 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 7834 {
861fb55a 7835 can_make_dynamic_p = TRUE;
b49e97c9
TS
7836 if (dynobj == NULL)
7837 elf_hash_table (info)->dynobj = dynobj = abfd;
b49e97c9 7838 break;
861fb55a 7839 }
21d790b9
MR
7840 /* For sections that are not SEC_ALLOC a copy reloc would be
7841 output if possible (implying questionable semantics for
7842 read-only data objects) or otherwise the final link would
7843 fail as ld.so will not process them and could not therefore
7844 handle any outstanding dynamic relocations.
7845
7846 For such sections that are also SEC_DEBUGGING, we can avoid
7847 these problems by simply ignoring any relocs as these
7848 sections have a predefined use and we know it is safe to do
7849 so.
7850
7851 This is needed in cases such as a global symbol definition
7852 in a shared library causing a common symbol from an object
7853 file to be converted to an undefined reference. If that
7854 happens, then all the relocations against this symbol from
7855 SEC_DEBUGGING sections in the object file will resolve to
7856 nil. */
7857 if ((sec->flags & SEC_DEBUGGING) != 0)
7858 break;
861fb55a 7859 /* Fall through. */
b49e97c9 7860
861fb55a
DJ
7861 default:
7862 /* Most static relocations require pointer equality, except
7863 for branches. */
7864 if (h)
7865 h->pointer_equality_needed = TRUE;
7866 /* Fall through. */
b49e97c9 7867
861fb55a
DJ
7868 case R_MIPS_26:
7869 case R_MIPS_PC16:
7870 case R_MIPS16_26:
df58fc94
RS
7871 case R_MICROMIPS_26_S1:
7872 case R_MICROMIPS_PC7_S1:
7873 case R_MICROMIPS_PC10_S1:
7874 case R_MICROMIPS_PC16_S1:
7875 case R_MICROMIPS_PC23_S2:
861fb55a
DJ
7876 if (h)
7877 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7878 break;
b49e97c9
TS
7879 }
7880
0a44bf69
RS
7881 if (h)
7882 {
0a44bf69
RS
7883 /* Relocations against the special VxWorks __GOTT_BASE__ and
7884 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7885 room for them in .rela.dyn. */
7886 if (is_gott_symbol (info, h))
7887 {
7888 if (sreloc == NULL)
7889 {
7890 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7891 if (sreloc == NULL)
7892 return FALSE;
7893 }
7894 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
7895 if (MIPS_ELF_READONLY_SECTION (sec))
7896 /* We tell the dynamic linker that there are
7897 relocations against the text segment. */
7898 info->flags |= DF_TEXTREL;
0a44bf69
RS
7899 }
7900 }
df58fc94
RS
7901 else if (call_lo16_reloc_p (r_type)
7902 || got_lo16_reloc_p (r_type)
7903 || got_disp_reloc_p (r_type)
738e5348 7904 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
7905 {
7906 /* We may need a local GOT entry for this relocation. We
7907 don't count R_MIPS_GOT_PAGE because we can estimate the
7908 maximum number of pages needed by looking at the size of
738e5348
RS
7909 the segment. Similar comments apply to R_MIPS*_GOT16 and
7910 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 7911 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 7912 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 7913 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0
RS
7914 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7915 rel->r_addend, info, 0))
f4416af6 7916 return FALSE;
b49e97c9
TS
7917 }
7918
861fb55a
DJ
7919 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7920 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7921
b49e97c9
TS
7922 switch (r_type)
7923 {
7924 case R_MIPS_CALL16:
738e5348 7925 case R_MIPS16_CALL16:
df58fc94 7926 case R_MICROMIPS_CALL16:
b49e97c9
TS
7927 if (h == NULL)
7928 {
7929 (*_bfd_error_handler)
d003868e
AM
7930 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7931 abfd, (unsigned long) rel->r_offset);
b49e97c9 7932 bfd_set_error (bfd_error_bad_value);
b34976b6 7933 return FALSE;
b49e97c9
TS
7934 }
7935 /* Fall through. */
7936
7937 case R_MIPS_CALL_HI16:
7938 case R_MIPS_CALL_LO16:
df58fc94
RS
7939 case R_MICROMIPS_CALL_HI16:
7940 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
7941 if (h != NULL)
7942 {
6ccf4795
RS
7943 /* Make sure there is room in the regular GOT to hold the
7944 function's address. We may eliminate it in favour of
7945 a .got.plt entry later; see mips_elf_count_got_symbols. */
7946 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE, 0))
b34976b6 7947 return FALSE;
b49e97c9
TS
7948
7949 /* We need a stub, not a plt entry for the undefined
7950 function. But we record it as if it needs plt. See
c152c796 7951 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 7952 h->needs_plt = 1;
b49e97c9
TS
7953 h->type = STT_FUNC;
7954 }
7955 break;
7956
0fdc1bf1 7957 case R_MIPS_GOT_PAGE:
df58fc94 7958 case R_MICROMIPS_GOT_PAGE:
0fdc1bf1
AO
7959 /* If this is a global, overridable symbol, GOT_PAGE will
7960 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d 7961 if (h)
0fdc1bf1
AO
7962 {
7963 struct mips_elf_link_hash_entry *hmips =
7964 (struct mips_elf_link_hash_entry *) h;
143d77c5 7965
3a3b6725 7966 /* This symbol is definitely not overridable. */
f5385ebf 7967 if (hmips->root.def_regular
0fdc1bf1 7968 && ! (info->shared && ! info->symbolic
f5385ebf 7969 && ! hmips->root.forced_local))
c224138d 7970 h = NULL;
0fdc1bf1
AO
7971 }
7972 /* Fall through. */
7973
738e5348 7974 case R_MIPS16_GOT16:
b49e97c9
TS
7975 case R_MIPS_GOT16:
7976 case R_MIPS_GOT_HI16:
7977 case R_MIPS_GOT_LO16:
df58fc94
RS
7978 case R_MICROMIPS_GOT16:
7979 case R_MICROMIPS_GOT_HI16:
7980 case R_MICROMIPS_GOT_LO16:
7981 if (!h || got_page_reloc_p (r_type))
c224138d 7982 {
3a3b6725
DJ
7983 /* This relocation needs (or may need, if h != NULL) a
7984 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7985 know for sure until we know whether the symbol is
7986 preemptible. */
c224138d
RS
7987 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7988 {
7989 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7990 return FALSE;
7991 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7992 addend = mips_elf_read_rel_addend (abfd, rel,
7993 howto, contents);
9684f078 7994 if (got16_reloc_p (r_type))
c224138d
RS
7995 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7996 contents, &addend);
7997 else
7998 addend <<= howto->rightshift;
7999 }
8000 else
8001 addend = rel->r_addend;
a8028dd0
RS
8002 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
8003 addend))
c224138d 8004 return FALSE;
c224138d
RS
8005 }
8006 /* Fall through. */
8007
b49e97c9 8008 case R_MIPS_GOT_DISP:
df58fc94 8009 case R_MICROMIPS_GOT_DISP:
6ccf4795
RS
8010 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8011 FALSE, 0))
b34976b6 8012 return FALSE;
b49e97c9
TS
8013 break;
8014
0f20cc35 8015 case R_MIPS_TLS_GOTTPREL:
df58fc94 8016 case R_MICROMIPS_TLS_GOTTPREL:
0f20cc35
DJ
8017 if (info->shared)
8018 info->flags |= DF_STATIC_TLS;
8019 /* Fall through */
8020
8021 case R_MIPS_TLS_LDM:
df58fc94
RS
8022 case R_MICROMIPS_TLS_LDM:
8023 if (tls_ldm_reloc_p (r_type))
0f20cc35 8024 {
cf35638d 8025 r_symndx = STN_UNDEF;
0f20cc35
DJ
8026 h = NULL;
8027 }
8028 /* Fall through */
8029
8030 case R_MIPS_TLS_GD:
df58fc94 8031 case R_MICROMIPS_TLS_GD:
0f20cc35
DJ
8032 /* This symbol requires a global offset table entry, or two
8033 for TLS GD relocations. */
8034 {
df58fc94
RS
8035 unsigned char flag;
8036
8037 flag = (tls_gd_reloc_p (r_type)
8038 ? GOT_TLS_GD
8039 : tls_ldm_reloc_p (r_type) ? GOT_TLS_LDM : GOT_TLS_IE);
0f20cc35
DJ
8040 if (h != NULL)
8041 {
8042 struct mips_elf_link_hash_entry *hmips =
8043 (struct mips_elf_link_hash_entry *) h;
8044 hmips->tls_type |= flag;
8045
6ccf4795
RS
8046 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8047 FALSE, flag))
0f20cc35
DJ
8048 return FALSE;
8049 }
8050 else
8051 {
cf35638d 8052 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != STN_UNDEF);
0f20cc35 8053
a8028dd0
RS
8054 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8055 rel->r_addend,
8056 info, flag))
0f20cc35
DJ
8057 return FALSE;
8058 }
8059 }
8060 break;
8061
b49e97c9
TS
8062 case R_MIPS_32:
8063 case R_MIPS_REL32:
8064 case R_MIPS_64:
0a44bf69
RS
8065 /* In VxWorks executables, references to external symbols
8066 are handled using copy relocs or PLT stubs, so there's
8067 no need to add a .rela.dyn entry for this relocation. */
861fb55a 8068 if (can_make_dynamic_p)
b49e97c9
TS
8069 {
8070 if (sreloc == NULL)
8071 {
0a44bf69 8072 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 8073 if (sreloc == NULL)
f4416af6 8074 return FALSE;
b49e97c9 8075 }
9a59ad6b 8076 if (info->shared && h == NULL)
82f0cfbd
EC
8077 {
8078 /* When creating a shared object, we must copy these
8079 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
8080 relocs. Make room for this reloc in .rel(a).dyn. */
8081 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 8082 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8083 /* We tell the dynamic linker that there are
8084 relocations against the text segment. */
8085 info->flags |= DF_TEXTREL;
8086 }
b49e97c9
TS
8087 else
8088 {
8089 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 8090
9a59ad6b
DJ
8091 /* For a shared object, we must copy this relocation
8092 unless the symbol turns out to be undefined and
8093 weak with non-default visibility, in which case
8094 it will be left as zero.
8095
8096 We could elide R_MIPS_REL32 for locally binding symbols
8097 in shared libraries, but do not yet do so.
8098
8099 For an executable, we only need to copy this
8100 reloc if the symbol is defined in a dynamic
8101 object. */
b49e97c9
TS
8102 hmips = (struct mips_elf_link_hash_entry *) h;
8103 ++hmips->possibly_dynamic_relocs;
943284cc 8104 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8105 /* We need it to tell the dynamic linker if there
8106 are relocations against the text segment. */
8107 hmips->readonly_reloc = TRUE;
b49e97c9 8108 }
b49e97c9
TS
8109 }
8110
8111 if (SGI_COMPAT (abfd))
8112 mips_elf_hash_table (info)->compact_rel_size +=
8113 sizeof (Elf32_External_crinfo);
8114 break;
8115
8116 case R_MIPS_26:
8117 case R_MIPS_GPREL16:
8118 case R_MIPS_LITERAL:
8119 case R_MIPS_GPREL32:
df58fc94
RS
8120 case R_MICROMIPS_26_S1:
8121 case R_MICROMIPS_GPREL16:
8122 case R_MICROMIPS_LITERAL:
8123 case R_MICROMIPS_GPREL7_S2:
b49e97c9
TS
8124 if (SGI_COMPAT (abfd))
8125 mips_elf_hash_table (info)->compact_rel_size +=
8126 sizeof (Elf32_External_crinfo);
8127 break;
8128
8129 /* This relocation describes the C++ object vtable hierarchy.
8130 Reconstruct it for later use during GC. */
8131 case R_MIPS_GNU_VTINHERIT:
c152c796 8132 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 8133 return FALSE;
b49e97c9
TS
8134 break;
8135
8136 /* This relocation describes which C++ vtable entries are actually
8137 used. Record for later use during GC. */
8138 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
8139 BFD_ASSERT (h != NULL);
8140 if (h != NULL
8141 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 8142 return FALSE;
b49e97c9
TS
8143 break;
8144
8145 default:
8146 break;
8147 }
8148
8149 /* We must not create a stub for a symbol that has relocations
0a44bf69
RS
8150 related to taking the function's address. This doesn't apply to
8151 VxWorks, where CALL relocs refer to a .got.plt entry instead of
8152 a normal .got entry. */
8153 if (!htab->is_vxworks && h != NULL)
8154 switch (r_type)
8155 {
8156 default:
8157 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8158 break;
738e5348 8159 case R_MIPS16_CALL16:
0a44bf69
RS
8160 case R_MIPS_CALL16:
8161 case R_MIPS_CALL_HI16:
8162 case R_MIPS_CALL_LO16:
8163 case R_MIPS_JALR:
df58fc94
RS
8164 case R_MICROMIPS_CALL16:
8165 case R_MICROMIPS_CALL_HI16:
8166 case R_MICROMIPS_CALL_LO16:
8167 case R_MICROMIPS_JALR:
0a44bf69
RS
8168 break;
8169 }
b49e97c9 8170
738e5348
RS
8171 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8172 if there is one. We only need to handle global symbols here;
8173 we decide whether to keep or delete stubs for local symbols
8174 when processing the stub's relocations. */
b49e97c9 8175 if (h != NULL
738e5348
RS
8176 && !mips16_call_reloc_p (r_type)
8177 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
8178 {
8179 struct mips_elf_link_hash_entry *mh;
8180
8181 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 8182 mh->need_fn_stub = TRUE;
b49e97c9 8183 }
861fb55a
DJ
8184
8185 /* Refuse some position-dependent relocations when creating a
8186 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8187 not PIC, but we can create dynamic relocations and the result
8188 will be fine. Also do not refuse R_MIPS_LO16, which can be
8189 combined with R_MIPS_GOT16. */
8190 if (info->shared)
8191 {
8192 switch (r_type)
8193 {
8194 case R_MIPS16_HI16:
8195 case R_MIPS_HI16:
8196 case R_MIPS_HIGHER:
8197 case R_MIPS_HIGHEST:
df58fc94
RS
8198 case R_MICROMIPS_HI16:
8199 case R_MICROMIPS_HIGHER:
8200 case R_MICROMIPS_HIGHEST:
861fb55a
DJ
8201 /* Don't refuse a high part relocation if it's against
8202 no symbol (e.g. part of a compound relocation). */
cf35638d 8203 if (r_symndx == STN_UNDEF)
861fb55a
DJ
8204 break;
8205
8206 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8207 and has a special meaning. */
8208 if (!NEWABI_P (abfd) && h != NULL
8209 && strcmp (h->root.root.string, "_gp_disp") == 0)
8210 break;
8211
0fc1eb3c
RS
8212 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8213 if (is_gott_symbol (info, h))
8214 break;
8215
861fb55a
DJ
8216 /* FALLTHROUGH */
8217
8218 case R_MIPS16_26:
8219 case R_MIPS_26:
df58fc94 8220 case R_MICROMIPS_26_S1:
861fb55a
DJ
8221 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8222 (*_bfd_error_handler)
8223 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8224 abfd, howto->name,
8225 (h) ? h->root.root.string : "a local symbol");
8226 bfd_set_error (bfd_error_bad_value);
8227 return FALSE;
8228 default:
8229 break;
8230 }
8231 }
b49e97c9
TS
8232 }
8233
b34976b6 8234 return TRUE;
b49e97c9
TS
8235}
8236\f
d0647110 8237bfd_boolean
9719ad41
RS
8238_bfd_mips_relax_section (bfd *abfd, asection *sec,
8239 struct bfd_link_info *link_info,
8240 bfd_boolean *again)
d0647110
AO
8241{
8242 Elf_Internal_Rela *internal_relocs;
8243 Elf_Internal_Rela *irel, *irelend;
8244 Elf_Internal_Shdr *symtab_hdr;
8245 bfd_byte *contents = NULL;
d0647110
AO
8246 size_t extsymoff;
8247 bfd_boolean changed_contents = FALSE;
8248 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8249 Elf_Internal_Sym *isymbuf = NULL;
8250
8251 /* We are not currently changing any sizes, so only one pass. */
8252 *again = FALSE;
8253
1049f94e 8254 if (link_info->relocatable)
d0647110
AO
8255 return TRUE;
8256
9719ad41 8257 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 8258 link_info->keep_memory);
d0647110
AO
8259 if (internal_relocs == NULL)
8260 return TRUE;
8261
8262 irelend = internal_relocs + sec->reloc_count
8263 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8264 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8265 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8266
8267 for (irel = internal_relocs; irel < irelend; irel++)
8268 {
8269 bfd_vma symval;
8270 bfd_signed_vma sym_offset;
8271 unsigned int r_type;
8272 unsigned long r_symndx;
8273 asection *sym_sec;
8274 unsigned long instruction;
8275
8276 /* Turn jalr into bgezal, and jr into beq, if they're marked
8277 with a JALR relocation, that indicate where they jump to.
8278 This saves some pipeline bubbles. */
8279 r_type = ELF_R_TYPE (abfd, irel->r_info);
8280 if (r_type != R_MIPS_JALR)
8281 continue;
8282
8283 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8284 /* Compute the address of the jump target. */
8285 if (r_symndx >= extsymoff)
8286 {
8287 struct mips_elf_link_hash_entry *h
8288 = ((struct mips_elf_link_hash_entry *)
8289 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8290
8291 while (h->root.root.type == bfd_link_hash_indirect
8292 || h->root.root.type == bfd_link_hash_warning)
8293 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 8294
d0647110
AO
8295 /* If a symbol is undefined, or if it may be overridden,
8296 skip it. */
8297 if (! ((h->root.root.type == bfd_link_hash_defined
8298 || h->root.root.type == bfd_link_hash_defweak)
8299 && h->root.root.u.def.section)
8300 || (link_info->shared && ! link_info->symbolic
f5385ebf 8301 && !h->root.forced_local))
d0647110
AO
8302 continue;
8303
8304 sym_sec = h->root.root.u.def.section;
8305 if (sym_sec->output_section)
8306 symval = (h->root.root.u.def.value
8307 + sym_sec->output_section->vma
8308 + sym_sec->output_offset);
8309 else
8310 symval = h->root.root.u.def.value;
8311 }
8312 else
8313 {
8314 Elf_Internal_Sym *isym;
8315
8316 /* Read this BFD's symbols if we haven't done so already. */
8317 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8318 {
8319 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8320 if (isymbuf == NULL)
8321 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8322 symtab_hdr->sh_info, 0,
8323 NULL, NULL, NULL);
8324 if (isymbuf == NULL)
8325 goto relax_return;
8326 }
8327
8328 isym = isymbuf + r_symndx;
8329 if (isym->st_shndx == SHN_UNDEF)
8330 continue;
8331 else if (isym->st_shndx == SHN_ABS)
8332 sym_sec = bfd_abs_section_ptr;
8333 else if (isym->st_shndx == SHN_COMMON)
8334 sym_sec = bfd_com_section_ptr;
8335 else
8336 sym_sec
8337 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8338 symval = isym->st_value
8339 + sym_sec->output_section->vma
8340 + sym_sec->output_offset;
8341 }
8342
8343 /* Compute branch offset, from delay slot of the jump to the
8344 branch target. */
8345 sym_offset = (symval + irel->r_addend)
8346 - (sec_start + irel->r_offset + 4);
8347
8348 /* Branch offset must be properly aligned. */
8349 if ((sym_offset & 3) != 0)
8350 continue;
8351
8352 sym_offset >>= 2;
8353
8354 /* Check that it's in range. */
8355 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8356 continue;
143d77c5 8357
d0647110 8358 /* Get the section contents if we haven't done so already. */
c224138d
RS
8359 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8360 goto relax_return;
d0647110
AO
8361
8362 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8363
8364 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8365 if ((instruction & 0xfc1fffff) == 0x0000f809)
8366 instruction = 0x04110000;
8367 /* If it was jr <reg>, turn it into b <target>. */
8368 else if ((instruction & 0xfc1fffff) == 0x00000008)
8369 instruction = 0x10000000;
8370 else
8371 continue;
8372
8373 instruction |= (sym_offset & 0xffff);
8374 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8375 changed_contents = TRUE;
8376 }
8377
8378 if (contents != NULL
8379 && elf_section_data (sec)->this_hdr.contents != contents)
8380 {
8381 if (!changed_contents && !link_info->keep_memory)
8382 free (contents);
8383 else
8384 {
8385 /* Cache the section contents for elf_link_input_bfd. */
8386 elf_section_data (sec)->this_hdr.contents = contents;
8387 }
8388 }
8389 return TRUE;
8390
143d77c5 8391 relax_return:
eea6121a
AM
8392 if (contents != NULL
8393 && elf_section_data (sec)->this_hdr.contents != contents)
8394 free (contents);
d0647110
AO
8395 return FALSE;
8396}
8397\f
9a59ad6b
DJ
8398/* Allocate space for global sym dynamic relocs. */
8399
8400static bfd_boolean
8401allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8402{
8403 struct bfd_link_info *info = inf;
8404 bfd *dynobj;
8405 struct mips_elf_link_hash_entry *hmips;
8406 struct mips_elf_link_hash_table *htab;
8407
8408 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8409 BFD_ASSERT (htab != NULL);
8410
9a59ad6b
DJ
8411 dynobj = elf_hash_table (info)->dynobj;
8412 hmips = (struct mips_elf_link_hash_entry *) h;
8413
8414 /* VxWorks executables are handled elsewhere; we only need to
8415 allocate relocations in shared objects. */
8416 if (htab->is_vxworks && !info->shared)
8417 return TRUE;
8418
7686d77d
AM
8419 /* Ignore indirect symbols. All relocations against such symbols
8420 will be redirected to the target symbol. */
8421 if (h->root.type == bfd_link_hash_indirect)
63897e2c
RS
8422 return TRUE;
8423
9a59ad6b
DJ
8424 /* If this symbol is defined in a dynamic object, or we are creating
8425 a shared library, we will need to copy any R_MIPS_32 or
8426 R_MIPS_REL32 relocs against it into the output file. */
8427 if (! info->relocatable
8428 && hmips->possibly_dynamic_relocs != 0
8429 && (h->root.type == bfd_link_hash_defweak
8430 || !h->def_regular
8431 || info->shared))
8432 {
8433 bfd_boolean do_copy = TRUE;
8434
8435 if (h->root.type == bfd_link_hash_undefweak)
8436 {
8437 /* Do not copy relocations for undefined weak symbols with
8438 non-default visibility. */
8439 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8440 do_copy = FALSE;
8441
8442 /* Make sure undefined weak symbols are output as a dynamic
8443 symbol in PIEs. */
8444 else if (h->dynindx == -1 && !h->forced_local)
8445 {
8446 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8447 return FALSE;
8448 }
8449 }
8450
8451 if (do_copy)
8452 {
aff469fa 8453 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
8454 the SVR4 psABI requires it to have a dynamic symbol table
8455 index greater that DT_MIPS_GOTSYM if there are dynamic
8456 relocations against it.
8457
8458 VxWorks does not enforce the same mapping between the GOT
8459 and the symbol table, so the same requirement does not
8460 apply there. */
6ccf4795
RS
8461 if (!htab->is_vxworks)
8462 {
8463 if (hmips->global_got_area > GGA_RELOC_ONLY)
8464 hmips->global_got_area = GGA_RELOC_ONLY;
8465 hmips->got_only_for_calls = FALSE;
8466 }
aff469fa 8467
9a59ad6b
DJ
8468 mips_elf_allocate_dynamic_relocations
8469 (dynobj, info, hmips->possibly_dynamic_relocs);
8470 if (hmips->readonly_reloc)
8471 /* We tell the dynamic linker that there are relocations
8472 against the text segment. */
8473 info->flags |= DF_TEXTREL;
8474 }
8475 }
8476
8477 return TRUE;
8478}
8479
b49e97c9
TS
8480/* Adjust a symbol defined by a dynamic object and referenced by a
8481 regular object. The current definition is in some section of the
8482 dynamic object, but we're not including those sections. We have to
8483 change the definition to something the rest of the link can
8484 understand. */
8485
b34976b6 8486bfd_boolean
9719ad41
RS
8487_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8488 struct elf_link_hash_entry *h)
b49e97c9
TS
8489{
8490 bfd *dynobj;
8491 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8492 struct mips_elf_link_hash_table *htab;
b49e97c9 8493
5108fc1b 8494 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8495 BFD_ASSERT (htab != NULL);
8496
b49e97c9 8497 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8498 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8499
8500 /* Make sure we know what is going on here. */
8501 BFD_ASSERT (dynobj != NULL
f5385ebf 8502 && (h->needs_plt
f6e332e6 8503 || h->u.weakdef != NULL
f5385ebf
AM
8504 || (h->def_dynamic
8505 && h->ref_regular
8506 && !h->def_regular)));
b49e97c9 8507
b49e97c9 8508 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8509
861fb55a
DJ
8510 /* If there are call relocations against an externally-defined symbol,
8511 see whether we can create a MIPS lazy-binding stub for it. We can
8512 only do this if all references to the function are through call
8513 relocations, and in that case, the traditional lazy-binding stubs
8514 are much more efficient than PLT entries.
8515
8516 Traditional stubs are only available on SVR4 psABI-based systems;
8517 VxWorks always uses PLTs instead. */
8518 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8519 {
8520 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8521 return TRUE;
b49e97c9
TS
8522
8523 /* If this symbol is not defined in a regular file, then set
8524 the symbol to the stub location. This is required to make
8525 function pointers compare as equal between the normal
8526 executable and the shared library. */
f5385ebf 8527 if (!h->def_regular)
b49e97c9 8528 {
33bb52fb
RS
8529 hmips->needs_lazy_stub = TRUE;
8530 htab->lazy_stub_count++;
b34976b6 8531 return TRUE;
b49e97c9
TS
8532 }
8533 }
861fb55a
DJ
8534 /* As above, VxWorks requires PLT entries for externally-defined
8535 functions that are only accessed through call relocations.
b49e97c9 8536
861fb55a
DJ
8537 Both VxWorks and non-VxWorks targets also need PLT entries if there
8538 are static-only relocations against an externally-defined function.
8539 This can technically occur for shared libraries if there are
8540 branches to the symbol, although it is unlikely that this will be
8541 used in practice due to the short ranges involved. It can occur
8542 for any relative or absolute relocation in executables; in that
8543 case, the PLT entry becomes the function's canonical address. */
8544 else if (((h->needs_plt && !hmips->no_fn_stub)
8545 || (h->type == STT_FUNC && hmips->has_static_relocs))
8546 && htab->use_plts_and_copy_relocs
8547 && !SYMBOL_CALLS_LOCAL (info, h)
8548 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8549 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8550 {
861fb55a
DJ
8551 /* If this is the first symbol to need a PLT entry, allocate room
8552 for the header. */
8553 if (htab->splt->size == 0)
8554 {
8555 BFD_ASSERT (htab->sgotplt->size == 0);
0a44bf69 8556
861fb55a
DJ
8557 /* If we're using the PLT additions to the psABI, each PLT
8558 entry is 16 bytes and the PLT0 entry is 32 bytes.
8559 Encourage better cache usage by aligning. We do this
8560 lazily to avoid pessimizing traditional objects. */
8561 if (!htab->is_vxworks
8562 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8563 return FALSE;
0a44bf69 8564
861fb55a
DJ
8565 /* Make sure that .got.plt is word-aligned. We do this lazily
8566 for the same reason as above. */
8567 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8568 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8569 return FALSE;
0a44bf69 8570
861fb55a 8571 htab->splt->size += htab->plt_header_size;
0a44bf69 8572
861fb55a
DJ
8573 /* On non-VxWorks targets, the first two entries in .got.plt
8574 are reserved. */
8575 if (!htab->is_vxworks)
8576 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
0a44bf69 8577
861fb55a
DJ
8578 /* On VxWorks, also allocate room for the header's
8579 .rela.plt.unloaded entries. */
8580 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8581 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8582 }
8583
8584 /* Assign the next .plt entry to this symbol. */
8585 h->plt.offset = htab->splt->size;
8586 htab->splt->size += htab->plt_entry_size;
8587
8588 /* If the output file has no definition of the symbol, set the
861fb55a 8589 symbol's value to the address of the stub. */
131eb6b7 8590 if (!info->shared && !h->def_regular)
0a44bf69
RS
8591 {
8592 h->root.u.def.section = htab->splt;
8593 h->root.u.def.value = h->plt.offset;
861fb55a
DJ
8594 /* For VxWorks, point at the PLT load stub rather than the
8595 lazy resolution stub; this stub will become the canonical
8596 function address. */
8597 if (htab->is_vxworks)
8598 h->root.u.def.value += 8;
0a44bf69
RS
8599 }
8600
861fb55a
DJ
8601 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8602 relocation. */
8603 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8604 htab->srelplt->size += (htab->is_vxworks
8605 ? MIPS_ELF_RELA_SIZE (dynobj)
8606 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8607
8608 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8609 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8610 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8611
861fb55a
DJ
8612 /* All relocations against this symbol that could have been made
8613 dynamic will now refer to the PLT entry instead. */
8614 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8615
0a44bf69
RS
8616 return TRUE;
8617 }
8618
8619 /* If this is a weak symbol, and there is a real definition, the
8620 processor independent code will have arranged for us to see the
8621 real definition first, and we can just use the same value. */
8622 if (h->u.weakdef != NULL)
8623 {
8624 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8625 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8626 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8627 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8628 return TRUE;
8629 }
8630
861fb55a
DJ
8631 /* Otherwise, there is nothing further to do for symbols defined
8632 in regular objects. */
8633 if (h->def_regular)
0a44bf69
RS
8634 return TRUE;
8635
861fb55a
DJ
8636 /* There's also nothing more to do if we'll convert all relocations
8637 against this symbol into dynamic relocations. */
8638 if (!hmips->has_static_relocs)
8639 return TRUE;
8640
8641 /* We're now relying on copy relocations. Complain if we have
8642 some that we can't convert. */
8643 if (!htab->use_plts_and_copy_relocs || info->shared)
8644 {
8645 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8646 "dynamic symbol %s"),
8647 h->root.root.string);
8648 bfd_set_error (bfd_error_bad_value);
8649 return FALSE;
8650 }
8651
0a44bf69
RS
8652 /* We must allocate the symbol in our .dynbss section, which will
8653 become part of the .bss section of the executable. There will be
8654 an entry for this symbol in the .dynsym section. The dynamic
8655 object will contain position independent code, so all references
8656 from the dynamic object to this symbol will go through the global
8657 offset table. The dynamic linker will use the .dynsym entry to
8658 determine the address it must put in the global offset table, so
8659 both the dynamic object and the regular object will refer to the
8660 same memory location for the variable. */
8661
8662 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8663 {
861fb55a
DJ
8664 if (htab->is_vxworks)
8665 htab->srelbss->size += sizeof (Elf32_External_Rela);
8666 else
8667 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
8668 h->needs_copy = 1;
8669 }
8670
861fb55a
DJ
8671 /* All relocations against this symbol that could have been made
8672 dynamic will now refer to the local copy instead. */
8673 hmips->possibly_dynamic_relocs = 0;
8674
027297b7 8675 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 8676}
b49e97c9
TS
8677\f
8678/* This function is called after all the input files have been read,
8679 and the input sections have been assigned to output sections. We
8680 check for any mips16 stub sections that we can discard. */
8681
b34976b6 8682bfd_boolean
9719ad41
RS
8683_bfd_mips_elf_always_size_sections (bfd *output_bfd,
8684 struct bfd_link_info *info)
b49e97c9
TS
8685{
8686 asection *ri;
0a44bf69 8687 struct mips_elf_link_hash_table *htab;
861fb55a 8688 struct mips_htab_traverse_info hti;
0a44bf69
RS
8689
8690 htab = mips_elf_hash_table (info);
4dfe6ac6 8691 BFD_ASSERT (htab != NULL);
f4416af6 8692
b49e97c9
TS
8693 /* The .reginfo section has a fixed size. */
8694 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8695 if (ri != NULL)
9719ad41 8696 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 8697
861fb55a
DJ
8698 hti.info = info;
8699 hti.output_bfd = output_bfd;
8700 hti.error = FALSE;
8701 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8702 mips_elf_check_symbols, &hti);
8703 if (hti.error)
8704 return FALSE;
f4416af6 8705
33bb52fb
RS
8706 return TRUE;
8707}
8708
8709/* If the link uses a GOT, lay it out and work out its size. */
8710
8711static bfd_boolean
8712mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8713{
8714 bfd *dynobj;
8715 asection *s;
8716 struct mips_got_info *g;
33bb52fb
RS
8717 bfd_size_type loadable_size = 0;
8718 bfd_size_type page_gotno;
8719 bfd *sub;
8720 struct mips_elf_count_tls_arg count_tls_arg;
8721 struct mips_elf_link_hash_table *htab;
8722
8723 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8724 BFD_ASSERT (htab != NULL);
8725
a8028dd0 8726 s = htab->sgot;
f4416af6 8727 if (s == NULL)
b34976b6 8728 return TRUE;
b49e97c9 8729
33bb52fb 8730 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
8731 g = htab->got_info;
8732
861fb55a
DJ
8733 /* Allocate room for the reserved entries. VxWorks always reserves
8734 3 entries; other objects only reserve 2 entries. */
8735 BFD_ASSERT (g->assigned_gotno == 0);
8736 if (htab->is_vxworks)
8737 htab->reserved_gotno = 3;
8738 else
8739 htab->reserved_gotno = 2;
8740 g->local_gotno += htab->reserved_gotno;
8741 g->assigned_gotno = htab->reserved_gotno;
8742
33bb52fb
RS
8743 /* Replace entries for indirect and warning symbols with entries for
8744 the target symbol. */
8745 if (!mips_elf_resolve_final_got_entries (g))
8746 return FALSE;
f4416af6 8747
d4596a51 8748 /* Count the number of GOT symbols. */
020d7251 8749 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 8750
33bb52fb
RS
8751 /* Calculate the total loadable size of the output. That
8752 will give us the maximum number of GOT_PAGE entries
8753 required. */
8754 for (sub = info->input_bfds; sub; sub = sub->link_next)
8755 {
8756 asection *subsection;
5108fc1b 8757
33bb52fb
RS
8758 for (subsection = sub->sections;
8759 subsection;
8760 subsection = subsection->next)
8761 {
8762 if ((subsection->flags & SEC_ALLOC) == 0)
8763 continue;
8764 loadable_size += ((subsection->size + 0xf)
8765 &~ (bfd_size_type) 0xf);
8766 }
8767 }
f4416af6 8768
0a44bf69 8769 if (htab->is_vxworks)
738e5348 8770 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
8771 relocations against local symbols evaluate to "G", and the EABI does
8772 not include R_MIPS_GOT_PAGE. */
c224138d 8773 page_gotno = 0;
0a44bf69
RS
8774 else
8775 /* Assume there are two loadable segments consisting of contiguous
8776 sections. Is 5 enough? */
c224138d
RS
8777 page_gotno = (loadable_size >> 16) + 5;
8778
8779 /* Choose the smaller of the two estimates; both are intended to be
8780 conservative. */
8781 if (page_gotno > g->page_gotno)
8782 page_gotno = g->page_gotno;
f4416af6 8783
c224138d 8784 g->local_gotno += page_gotno;
eea6121a 8785 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
d4596a51 8786 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6 8787
0f20cc35
DJ
8788 /* We need to calculate tls_gotno for global symbols at this point
8789 instead of building it up earlier, to avoid doublecounting
8790 entries for one global symbol from multiple input files. */
8791 count_tls_arg.info = info;
8792 count_tls_arg.needed = 0;
8793 elf_link_hash_traverse (elf_hash_table (info),
8794 mips_elf_count_global_tls_entries,
8795 &count_tls_arg);
8796 g->tls_gotno += count_tls_arg.needed;
8797 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8798
0a44bf69
RS
8799 /* VxWorks does not support multiple GOTs. It initializes $gp to
8800 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8801 dynamic loader. */
33bb52fb
RS
8802 if (htab->is_vxworks)
8803 {
8804 /* VxWorks executables do not need a GOT. */
8805 if (info->shared)
8806 {
8807 /* Each VxWorks GOT entry needs an explicit relocation. */
8808 unsigned int count;
8809
861fb55a 8810 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
33bb52fb
RS
8811 if (count)
8812 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8813 }
8814 }
8815 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 8816 {
a8028dd0 8817 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
8818 return FALSE;
8819 }
8820 else
8821 {
33bb52fb
RS
8822 struct mips_elf_count_tls_arg arg;
8823
8824 /* Set up TLS entries. */
0f20cc35
DJ
8825 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8826 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
33bb52fb
RS
8827
8828 /* Allocate room for the TLS relocations. */
8829 arg.info = info;
8830 arg.needed = 0;
8831 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8832 elf_link_hash_traverse (elf_hash_table (info),
8833 mips_elf_count_global_tls_relocs,
8834 &arg);
8835 if (arg.needed)
8836 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
0f20cc35 8837 }
b49e97c9 8838
b34976b6 8839 return TRUE;
b49e97c9
TS
8840}
8841
33bb52fb
RS
8842/* Estimate the size of the .MIPS.stubs section. */
8843
8844static void
8845mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8846{
8847 struct mips_elf_link_hash_table *htab;
8848 bfd_size_type dynsymcount;
8849
8850 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8851 BFD_ASSERT (htab != NULL);
8852
33bb52fb
RS
8853 if (htab->lazy_stub_count == 0)
8854 return;
8855
8856 /* IRIX rld assumes that a function stub isn't at the end of the .text
8857 section, so add a dummy entry to the end. */
8858 htab->lazy_stub_count++;
8859
8860 /* Get a worst-case estimate of the number of dynamic symbols needed.
8861 At this point, dynsymcount does not account for section symbols
8862 and count_section_dynsyms may overestimate the number that will
8863 be needed. */
8864 dynsymcount = (elf_hash_table (info)->dynsymcount
8865 + count_section_dynsyms (output_bfd, info));
8866
8867 /* Determine the size of one stub entry. */
8868 htab->function_stub_size = (dynsymcount > 0x10000
8869 ? MIPS_FUNCTION_STUB_BIG_SIZE
8870 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8871
8872 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8873}
8874
8875/* A mips_elf_link_hash_traverse callback for which DATA points to the
8876 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8877 allocate an entry in the stubs section. */
8878
8879static bfd_boolean
8880mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8881{
8882 struct mips_elf_link_hash_table *htab;
8883
8884 htab = (struct mips_elf_link_hash_table *) data;
8885 if (h->needs_lazy_stub)
8886 {
8887 h->root.root.u.def.section = htab->sstubs;
8888 h->root.root.u.def.value = htab->sstubs->size;
8889 h->root.plt.offset = htab->sstubs->size;
8890 htab->sstubs->size += htab->function_stub_size;
8891 }
8892 return TRUE;
8893}
8894
8895/* Allocate offsets in the stubs section to each symbol that needs one.
8896 Set the final size of the .MIPS.stub section. */
8897
8898static void
8899mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8900{
8901 struct mips_elf_link_hash_table *htab;
8902
8903 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8904 BFD_ASSERT (htab != NULL);
8905
33bb52fb
RS
8906 if (htab->lazy_stub_count == 0)
8907 return;
8908
8909 htab->sstubs->size = 0;
4dfe6ac6 8910 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
33bb52fb
RS
8911 htab->sstubs->size += htab->function_stub_size;
8912 BFD_ASSERT (htab->sstubs->size
8913 == htab->lazy_stub_count * htab->function_stub_size);
8914}
8915
b49e97c9
TS
8916/* Set the sizes of the dynamic sections. */
8917
b34976b6 8918bfd_boolean
9719ad41
RS
8919_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8920 struct bfd_link_info *info)
b49e97c9
TS
8921{
8922 bfd *dynobj;
861fb55a 8923 asection *s, *sreldyn;
b34976b6 8924 bfd_boolean reltext;
0a44bf69 8925 struct mips_elf_link_hash_table *htab;
b49e97c9 8926
0a44bf69 8927 htab = mips_elf_hash_table (info);
4dfe6ac6 8928 BFD_ASSERT (htab != NULL);
b49e97c9
TS
8929 dynobj = elf_hash_table (info)->dynobj;
8930 BFD_ASSERT (dynobj != NULL);
8931
8932 if (elf_hash_table (info)->dynamic_sections_created)
8933 {
8934 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 8935 if (info->executable)
b49e97c9
TS
8936 {
8937 s = bfd_get_section_by_name (dynobj, ".interp");
8938 BFD_ASSERT (s != NULL);
eea6121a 8939 s->size
b49e97c9
TS
8940 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8941 s->contents
8942 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8943 }
861fb55a
DJ
8944
8945 /* Create a symbol for the PLT, if we know that we are using it. */
8946 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8947 {
8948 struct elf_link_hash_entry *h;
8949
8950 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8951
8952 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8953 "_PROCEDURE_LINKAGE_TABLE_");
8954 htab->root.hplt = h;
8955 if (h == NULL)
8956 return FALSE;
8957 h->type = STT_FUNC;
8958 }
8959 }
4e41d0d7 8960
9a59ad6b
DJ
8961 /* Allocate space for global sym dynamic relocs. */
8962 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8963
33bb52fb
RS
8964 mips_elf_estimate_stub_size (output_bfd, info);
8965
8966 if (!mips_elf_lay_out_got (output_bfd, info))
8967 return FALSE;
8968
8969 mips_elf_lay_out_lazy_stubs (info);
8970
b49e97c9
TS
8971 /* The check_relocs and adjust_dynamic_symbol entry points have
8972 determined the sizes of the various dynamic sections. Allocate
8973 memory for them. */
b34976b6 8974 reltext = FALSE;
b49e97c9
TS
8975 for (s = dynobj->sections; s != NULL; s = s->next)
8976 {
8977 const char *name;
b49e97c9
TS
8978
8979 /* It's OK to base decisions on the section name, because none
8980 of the dynobj section names depend upon the input files. */
8981 name = bfd_get_section_name (dynobj, s);
8982
8983 if ((s->flags & SEC_LINKER_CREATED) == 0)
8984 continue;
8985
0112cd26 8986 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 8987 {
c456f082 8988 if (s->size != 0)
b49e97c9
TS
8989 {
8990 const char *outname;
8991 asection *target;
8992
8993 /* If this relocation section applies to a read only
8994 section, then we probably need a DT_TEXTREL entry.
0a44bf69 8995 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
8996 assert a DT_TEXTREL entry rather than testing whether
8997 there exists a relocation to a read only section or
8998 not. */
8999 outname = bfd_get_section_name (output_bfd,
9000 s->output_section);
9001 target = bfd_get_section_by_name (output_bfd, outname + 4);
9002 if ((target != NULL
9003 && (target->flags & SEC_READONLY) != 0
9004 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 9005 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 9006 reltext = TRUE;
b49e97c9
TS
9007
9008 /* We use the reloc_count field as a counter if we need
9009 to copy relocs into the output file. */
0a44bf69 9010 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 9011 s->reloc_count = 0;
f4416af6
AO
9012
9013 /* If combreloc is enabled, elf_link_sort_relocs() will
9014 sort relocations, but in a different way than we do,
9015 and before we're done creating relocations. Also, it
9016 will move them around between input sections'
9017 relocation's contents, so our sorting would be
9018 broken, so don't let it run. */
9019 info->combreloc = 0;
b49e97c9
TS
9020 }
9021 }
b49e97c9
TS
9022 else if (! info->shared
9023 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 9024 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 9025 {
5108fc1b 9026 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 9027 rtld to contain a pointer to the _r_debug structure. */
eea6121a 9028 s->size += 4;
b49e97c9
TS
9029 }
9030 else if (SGI_COMPAT (output_bfd)
0112cd26 9031 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 9032 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
9033 else if (s == htab->splt)
9034 {
9035 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
9036 room for an extra nop to fill the delay slot. This is
9037 for CPUs without load interlocking. */
9038 if (! LOAD_INTERLOCKS_P (output_bfd)
9039 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
9040 s->size += 4;
9041 }
0112cd26 9042 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 9043 && s != htab->sgot
0a44bf69 9044 && s != htab->sgotplt
861fb55a
DJ
9045 && s != htab->sstubs
9046 && s != htab->sdynbss)
b49e97c9
TS
9047 {
9048 /* It's not one of our sections, so don't allocate space. */
9049 continue;
9050 }
9051
c456f082 9052 if (s->size == 0)
b49e97c9 9053 {
8423293d 9054 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
9055 continue;
9056 }
9057
c456f082
AM
9058 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9059 continue;
9060
b49e97c9 9061 /* Allocate memory for the section contents. */
eea6121a 9062 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 9063 if (s->contents == NULL)
b49e97c9
TS
9064 {
9065 bfd_set_error (bfd_error_no_memory);
b34976b6 9066 return FALSE;
b49e97c9
TS
9067 }
9068 }
9069
9070 if (elf_hash_table (info)->dynamic_sections_created)
9071 {
9072 /* Add some entries to the .dynamic section. We fill in the
9073 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9074 must add the entries now so that we get the correct size for
5750dcec 9075 the .dynamic section. */
af5978fb
RS
9076
9077 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec
DJ
9078 DT_MIPS_RLD_MAP entry. This must come first because glibc
9079 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
9080 looks at the first one it sees. */
af5978fb
RS
9081 if (!info->shared
9082 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9083 return FALSE;
b49e97c9 9084
5750dcec
DJ
9085 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9086 used by the debugger. */
9087 if (info->executable
9088 && !SGI_COMPAT (output_bfd)
9089 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9090 return FALSE;
9091
0a44bf69 9092 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
9093 info->flags |= DF_TEXTREL;
9094
9095 if ((info->flags & DF_TEXTREL) != 0)
9096 {
9097 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 9098 return FALSE;
943284cc
DJ
9099
9100 /* Clear the DF_TEXTREL flag. It will be set again if we
9101 write out an actual text relocation; we may not, because
9102 at this point we do not know whether e.g. any .eh_frame
9103 absolute relocations have been converted to PC-relative. */
9104 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
9105 }
9106
9107 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 9108 return FALSE;
b49e97c9 9109
861fb55a 9110 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 9111 if (htab->is_vxworks)
b49e97c9 9112 {
0a44bf69
RS
9113 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9114 use any of the DT_MIPS_* tags. */
861fb55a 9115 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
9116 {
9117 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9118 return FALSE;
b49e97c9 9119
0a44bf69
RS
9120 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9121 return FALSE;
b49e97c9 9122
0a44bf69
RS
9123 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9124 return FALSE;
9125 }
b49e97c9 9126 }
0a44bf69
RS
9127 else
9128 {
861fb55a 9129 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
9130 {
9131 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9132 return FALSE;
b49e97c9 9133
0a44bf69
RS
9134 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9135 return FALSE;
b49e97c9 9136
0a44bf69
RS
9137 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9138 return FALSE;
9139 }
b49e97c9 9140
0a44bf69
RS
9141 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9142 return FALSE;
b49e97c9 9143
0a44bf69
RS
9144 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9145 return FALSE;
b49e97c9 9146
0a44bf69
RS
9147 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9148 return FALSE;
b49e97c9 9149
0a44bf69
RS
9150 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9151 return FALSE;
b49e97c9 9152
0a44bf69
RS
9153 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9154 return FALSE;
b49e97c9 9155
0a44bf69
RS
9156 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9157 return FALSE;
b49e97c9 9158
0a44bf69
RS
9159 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9160 return FALSE;
9161
9162 if (IRIX_COMPAT (dynobj) == ict_irix5
9163 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9164 return FALSE;
9165
9166 if (IRIX_COMPAT (dynobj) == ict_irix6
9167 && (bfd_get_section_by_name
9168 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9169 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9170 return FALSE;
9171 }
861fb55a
DJ
9172 if (htab->splt->size > 0)
9173 {
9174 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9175 return FALSE;
9176
9177 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9178 return FALSE;
9179
9180 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9181 return FALSE;
9182
9183 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9184 return FALSE;
9185 }
7a2b07ff
NS
9186 if (htab->is_vxworks
9187 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9188 return FALSE;
b49e97c9
TS
9189 }
9190
b34976b6 9191 return TRUE;
b49e97c9
TS
9192}
9193\f
81d43bff
RS
9194/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9195 Adjust its R_ADDEND field so that it is correct for the output file.
9196 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9197 and sections respectively; both use symbol indexes. */
9198
9199static void
9200mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9201 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9202 asection **local_sections, Elf_Internal_Rela *rel)
9203{
9204 unsigned int r_type, r_symndx;
9205 Elf_Internal_Sym *sym;
9206 asection *sec;
9207
020d7251 9208 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
9209 {
9210 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
df58fc94 9211 if (gprel16_reloc_p (r_type)
81d43bff 9212 || r_type == R_MIPS_GPREL32
df58fc94 9213 || literal_reloc_p (r_type))
81d43bff
RS
9214 {
9215 rel->r_addend += _bfd_get_gp_value (input_bfd);
9216 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9217 }
9218
9219 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9220 sym = local_syms + r_symndx;
9221
9222 /* Adjust REL's addend to account for section merging. */
9223 if (!info->relocatable)
9224 {
9225 sec = local_sections[r_symndx];
9226 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9227 }
9228
9229 /* This would normally be done by the rela_normal code in elflink.c. */
9230 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9231 rel->r_addend += local_sections[r_symndx]->output_offset;
9232 }
9233}
9234
b49e97c9
TS
9235/* Relocate a MIPS ELF section. */
9236
b34976b6 9237bfd_boolean
9719ad41
RS
9238_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9239 bfd *input_bfd, asection *input_section,
9240 bfd_byte *contents, Elf_Internal_Rela *relocs,
9241 Elf_Internal_Sym *local_syms,
9242 asection **local_sections)
b49e97c9
TS
9243{
9244 Elf_Internal_Rela *rel;
9245 const Elf_Internal_Rela *relend;
9246 bfd_vma addend = 0;
b34976b6 9247 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 9248 const struct elf_backend_data *bed;
b49e97c9
TS
9249
9250 bed = get_elf_backend_data (output_bfd);
9251 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9252 for (rel = relocs; rel < relend; ++rel)
9253 {
9254 const char *name;
c9adbffe 9255 bfd_vma value = 0;
b49e97c9 9256 reloc_howto_type *howto;
38a7df63 9257 bfd_boolean cross_mode_jump_p;
b34976b6 9258 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 9259 REL relocation. */
b34976b6 9260 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 9261 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 9262 const char *msg;
ab96bf03
AM
9263 unsigned long r_symndx;
9264 asection *sec;
749b8d9d
L
9265 Elf_Internal_Shdr *symtab_hdr;
9266 struct elf_link_hash_entry *h;
d4730f92 9267 bfd_boolean rel_reloc;
b49e97c9 9268
d4730f92
BS
9269 rel_reloc = (NEWABI_P (input_bfd)
9270 && mips_elf_rel_relocation_p (input_bfd, input_section,
9271 relocs, rel));
b49e97c9 9272 /* Find the relocation howto for this relocation. */
d4730f92 9273 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
ab96bf03
AM
9274
9275 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 9276 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 9277 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
9278 {
9279 sec = local_sections[r_symndx];
9280 h = NULL;
9281 }
ab96bf03
AM
9282 else
9283 {
ab96bf03 9284 unsigned long extsymoff;
ab96bf03 9285
ab96bf03
AM
9286 extsymoff = 0;
9287 if (!elf_bad_symtab (input_bfd))
9288 extsymoff = symtab_hdr->sh_info;
9289 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9290 while (h->root.type == bfd_link_hash_indirect
9291 || h->root.type == bfd_link_hash_warning)
9292 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9293
9294 sec = NULL;
9295 if (h->root.type == bfd_link_hash_defined
9296 || h->root.type == bfd_link_hash_defweak)
9297 sec = h->root.u.def.section;
9298 }
9299
9300 if (sec != NULL && elf_discarded_section (sec))
e4067dbb
DJ
9301 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9302 rel, relend, howto, contents);
ab96bf03 9303
4a14403c 9304 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
9305 {
9306 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9307 64-bit code, but make sure all their addresses are in the
9308 lowermost or uppermost 32-bit section of the 64-bit address
9309 space. Thus, when they use an R_MIPS_64 they mean what is
9310 usually meant by R_MIPS_32, with the exception that the
9311 stored value is sign-extended to 64 bits. */
b34976b6 9312 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
9313
9314 /* On big-endian systems, we need to lie about the position
9315 of the reloc. */
9316 if (bfd_big_endian (input_bfd))
9317 rel->r_offset += 4;
9318 }
b49e97c9
TS
9319
9320 if (!use_saved_addend_p)
9321 {
b49e97c9
TS
9322 /* If these relocations were originally of the REL variety,
9323 we must pull the addend out of the field that will be
9324 relocated. Otherwise, we simply use the contents of the
c224138d
RS
9325 RELA relocation. */
9326 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9327 relocs, rel))
b49e97c9 9328 {
b34976b6 9329 rela_relocation_p = FALSE;
c224138d
RS
9330 addend = mips_elf_read_rel_addend (input_bfd, rel,
9331 howto, contents);
738e5348
RS
9332 if (hi16_reloc_p (r_type)
9333 || (got16_reloc_p (r_type)
b49e97c9 9334 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 9335 local_sections)))
b49e97c9 9336 {
c224138d
RS
9337 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9338 contents, &addend))
749b8d9d 9339 {
749b8d9d
L
9340 if (h)
9341 name = h->root.root.string;
9342 else
9343 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9344 local_syms + r_symndx,
9345 sec);
9346 (*_bfd_error_handler)
9347 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9348 input_bfd, input_section, name, howto->name,
9349 rel->r_offset);
749b8d9d 9350 }
b49e97c9 9351 }
30ac9238
RS
9352 else
9353 addend <<= howto->rightshift;
b49e97c9
TS
9354 }
9355 else
9356 addend = rel->r_addend;
81d43bff
RS
9357 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9358 local_syms, local_sections, rel);
b49e97c9
TS
9359 }
9360
1049f94e 9361 if (info->relocatable)
b49e97c9 9362 {
4a14403c 9363 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9364 && bfd_big_endian (input_bfd))
9365 rel->r_offset -= 4;
9366
81d43bff 9367 if (!rela_relocation_p && rel->r_addend)
5a659663 9368 {
81d43bff 9369 addend += rel->r_addend;
738e5348 9370 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9371 addend = mips_elf_high (addend);
9372 else if (r_type == R_MIPS_HIGHER)
9373 addend = mips_elf_higher (addend);
9374 else if (r_type == R_MIPS_HIGHEST)
9375 addend = mips_elf_highest (addend);
30ac9238
RS
9376 else
9377 addend >>= howto->rightshift;
b49e97c9 9378
30ac9238
RS
9379 /* We use the source mask, rather than the destination
9380 mask because the place to which we are writing will be
9381 source of the addend in the final link. */
b49e97c9
TS
9382 addend &= howto->src_mask;
9383
5a659663 9384 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9385 /* See the comment above about using R_MIPS_64 in the 32-bit
9386 ABI. Here, we need to update the addend. It would be
9387 possible to get away with just using the R_MIPS_32 reloc
9388 but for endianness. */
9389 {
9390 bfd_vma sign_bits;
9391 bfd_vma low_bits;
9392 bfd_vma high_bits;
9393
9394 if (addend & ((bfd_vma) 1 << 31))
9395#ifdef BFD64
9396 sign_bits = ((bfd_vma) 1 << 32) - 1;
9397#else
9398 sign_bits = -1;
9399#endif
9400 else
9401 sign_bits = 0;
9402
9403 /* If we don't know that we have a 64-bit type,
9404 do two separate stores. */
9405 if (bfd_big_endian (input_bfd))
9406 {
9407 /* Store the sign-bits (which are most significant)
9408 first. */
9409 low_bits = sign_bits;
9410 high_bits = addend;
9411 }
9412 else
9413 {
9414 low_bits = addend;
9415 high_bits = sign_bits;
9416 }
9417 bfd_put_32 (input_bfd, low_bits,
9418 contents + rel->r_offset);
9419 bfd_put_32 (input_bfd, high_bits,
9420 contents + rel->r_offset + 4);
9421 continue;
9422 }
9423
9424 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9425 input_bfd, input_section,
b34976b6
AM
9426 contents, FALSE))
9427 return FALSE;
b49e97c9
TS
9428 }
9429
9430 /* Go on to the next relocation. */
9431 continue;
9432 }
9433
9434 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9435 relocations for the same offset. In that case we are
9436 supposed to treat the output of each relocation as the addend
9437 for the next. */
9438 if (rel + 1 < relend
9439 && rel->r_offset == rel[1].r_offset
9440 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9441 use_saved_addend_p = TRUE;
b49e97c9 9442 else
b34976b6 9443 use_saved_addend_p = FALSE;
b49e97c9
TS
9444
9445 /* Figure out what value we are supposed to relocate. */
9446 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9447 input_section, info, rel,
9448 addend, howto, local_syms,
9449 local_sections, &value,
38a7df63 9450 &name, &cross_mode_jump_p,
bce03d3d 9451 use_saved_addend_p))
b49e97c9
TS
9452 {
9453 case bfd_reloc_continue:
9454 /* There's nothing to do. */
9455 continue;
9456
9457 case bfd_reloc_undefined:
9458 /* mips_elf_calculate_relocation already called the
9459 undefined_symbol callback. There's no real point in
9460 trying to perform the relocation at this point, so we
9461 just skip ahead to the next relocation. */
9462 continue;
9463
9464 case bfd_reloc_notsupported:
9465 msg = _("internal error: unsupported relocation error");
9466 info->callbacks->warning
9467 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9468 return FALSE;
b49e97c9
TS
9469
9470 case bfd_reloc_overflow:
9471 if (use_saved_addend_p)
9472 /* Ignore overflow until we reach the last relocation for
9473 a given location. */
9474 ;
9475 else
9476 {
0e53d9da
AN
9477 struct mips_elf_link_hash_table *htab;
9478
9479 htab = mips_elf_hash_table (info);
4dfe6ac6 9480 BFD_ASSERT (htab != NULL);
b49e97c9 9481 BFD_ASSERT (name != NULL);
0e53d9da 9482 if (!htab->small_data_overflow_reported
9684f078 9483 && (gprel16_reloc_p (howto->type)
df58fc94 9484 || literal_reloc_p (howto->type)))
0e53d9da 9485 {
91d6fa6a
NC
9486 msg = _("small-data section exceeds 64KB;"
9487 " lower small-data size limit (see option -G)");
0e53d9da
AN
9488
9489 htab->small_data_overflow_reported = TRUE;
9490 (*info->callbacks->einfo) ("%P: %s\n", msg);
9491 }
b49e97c9 9492 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 9493 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 9494 input_bfd, input_section, rel->r_offset)))
b34976b6 9495 return FALSE;
b49e97c9
TS
9496 }
9497 break;
9498
9499 case bfd_reloc_ok:
9500 break;
9501
df58fc94
RS
9502 case bfd_reloc_outofrange:
9503 if (jal_reloc_p (howto->type))
9504 {
9505 msg = _("JALX to a non-word-aligned address");
9506 info->callbacks->warning
9507 (info, msg, name, input_bfd, input_section, rel->r_offset);
9508 return FALSE;
9509 }
9510 /* Fall through. */
9511
b49e97c9
TS
9512 default:
9513 abort ();
9514 break;
9515 }
9516
9517 /* If we've got another relocation for the address, keep going
9518 until we reach the last one. */
9519 if (use_saved_addend_p)
9520 {
9521 addend = value;
9522 continue;
9523 }
9524
4a14403c 9525 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9526 /* See the comment above about using R_MIPS_64 in the 32-bit
9527 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9528 that calculated the right value. Now, however, we
9529 sign-extend the 32-bit result to 64-bits, and store it as a
9530 64-bit value. We are especially generous here in that we
9531 go to extreme lengths to support this usage on systems with
9532 only a 32-bit VMA. */
9533 {
9534 bfd_vma sign_bits;
9535 bfd_vma low_bits;
9536 bfd_vma high_bits;
9537
9538 if (value & ((bfd_vma) 1 << 31))
9539#ifdef BFD64
9540 sign_bits = ((bfd_vma) 1 << 32) - 1;
9541#else
9542 sign_bits = -1;
9543#endif
9544 else
9545 sign_bits = 0;
9546
9547 /* If we don't know that we have a 64-bit type,
9548 do two separate stores. */
9549 if (bfd_big_endian (input_bfd))
9550 {
9551 /* Undo what we did above. */
9552 rel->r_offset -= 4;
9553 /* Store the sign-bits (which are most significant)
9554 first. */
9555 low_bits = sign_bits;
9556 high_bits = value;
9557 }
9558 else
9559 {
9560 low_bits = value;
9561 high_bits = sign_bits;
9562 }
9563 bfd_put_32 (input_bfd, low_bits,
9564 contents + rel->r_offset);
9565 bfd_put_32 (input_bfd, high_bits,
9566 contents + rel->r_offset + 4);
9567 continue;
9568 }
9569
9570 /* Actually perform the relocation. */
9571 if (! mips_elf_perform_relocation (info, howto, rel, value,
9572 input_bfd, input_section,
38a7df63 9573 contents, cross_mode_jump_p))
b34976b6 9574 return FALSE;
b49e97c9
TS
9575 }
9576
b34976b6 9577 return TRUE;
b49e97c9
TS
9578}
9579\f
861fb55a
DJ
9580/* A function that iterates over each entry in la25_stubs and fills
9581 in the code for each one. DATA points to a mips_htab_traverse_info. */
9582
9583static int
9584mips_elf_create_la25_stub (void **slot, void *data)
9585{
9586 struct mips_htab_traverse_info *hti;
9587 struct mips_elf_link_hash_table *htab;
9588 struct mips_elf_la25_stub *stub;
9589 asection *s;
9590 bfd_byte *loc;
9591 bfd_vma offset, target, target_high, target_low;
9592
9593 stub = (struct mips_elf_la25_stub *) *slot;
9594 hti = (struct mips_htab_traverse_info *) data;
9595 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 9596 BFD_ASSERT (htab != NULL);
861fb55a
DJ
9597
9598 /* Create the section contents, if we haven't already. */
9599 s = stub->stub_section;
9600 loc = s->contents;
9601 if (loc == NULL)
9602 {
9603 loc = bfd_malloc (s->size);
9604 if (loc == NULL)
9605 {
9606 hti->error = TRUE;
9607 return FALSE;
9608 }
9609 s->contents = loc;
9610 }
9611
9612 /* Work out where in the section this stub should go. */
9613 offset = stub->offset;
9614
9615 /* Work out the target address. */
9616 target = (stub->h->root.root.u.def.section->output_section->vma
9617 + stub->h->root.root.u.def.section->output_offset
9618 + stub->h->root.root.u.def.value);
9619 target_high = ((target + 0x8000) >> 16) & 0xffff;
9620 target_low = (target & 0xffff);
9621
9622 if (stub->stub_section != htab->strampoline)
9623 {
df58fc94 9624 /* This is a simple LUI/ADDIU stub. Zero out the beginning
861fb55a
DJ
9625 of the section and write the two instructions at the end. */
9626 memset (loc, 0, offset);
9627 loc += offset;
df58fc94
RS
9628 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
9629 {
9630 bfd_put_16 (hti->output_bfd, LA25_LUI_MICROMIPS_1 (target_high),
9631 loc);
9632 bfd_put_16 (hti->output_bfd, LA25_LUI_MICROMIPS_2 (target_high),
9633 loc + 2);
9634 bfd_put_16 (hti->output_bfd, LA25_ADDIU_MICROMIPS_1 (target_low),
9635 loc + 4);
9636 bfd_put_16 (hti->output_bfd, LA25_ADDIU_MICROMIPS_2 (target_low),
9637 loc + 6);
9638 }
9639 else
9640 {
9641 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9642 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9643 }
861fb55a
DJ
9644 }
9645 else
9646 {
9647 /* This is trampoline. */
9648 loc += offset;
df58fc94
RS
9649 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
9650 {
9651 bfd_put_16 (hti->output_bfd, LA25_LUI_MICROMIPS_1 (target_high),
9652 loc);
9653 bfd_put_16 (hti->output_bfd, LA25_LUI_MICROMIPS_2 (target_high),
9654 loc + 2);
9655 bfd_put_16 (hti->output_bfd, LA25_J_MICROMIPS_1 (target), loc + 4);
9656 bfd_put_16 (hti->output_bfd, LA25_J_MICROMIPS_2 (target), loc + 6);
9657 bfd_put_16 (hti->output_bfd, LA25_ADDIU_MICROMIPS_1 (target_low),
9658 loc + 8);
9659 bfd_put_16 (hti->output_bfd, LA25_ADDIU_MICROMIPS_2 (target_low),
9660 loc + 10);
9661 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9662 }
9663 else
9664 {
9665 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9666 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9667 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9668 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9669 }
861fb55a
DJ
9670 }
9671 return TRUE;
9672}
9673
b49e97c9
TS
9674/* If NAME is one of the special IRIX6 symbols defined by the linker,
9675 adjust it appropriately now. */
9676
9677static void
9719ad41
RS
9678mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9679 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
9680{
9681 /* The linker script takes care of providing names and values for
9682 these, but we must place them into the right sections. */
9683 static const char* const text_section_symbols[] = {
9684 "_ftext",
9685 "_etext",
9686 "__dso_displacement",
9687 "__elf_header",
9688 "__program_header_table",
9689 NULL
9690 };
9691
9692 static const char* const data_section_symbols[] = {
9693 "_fdata",
9694 "_edata",
9695 "_end",
9696 "_fbss",
9697 NULL
9698 };
9699
9700 const char* const *p;
9701 int i;
9702
9703 for (i = 0; i < 2; ++i)
9704 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9705 *p;
9706 ++p)
9707 if (strcmp (*p, name) == 0)
9708 {
9709 /* All of these symbols are given type STT_SECTION by the
9710 IRIX6 linker. */
9711 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 9712 sym->st_other = STO_PROTECTED;
b49e97c9
TS
9713
9714 /* The IRIX linker puts these symbols in special sections. */
9715 if (i == 0)
9716 sym->st_shndx = SHN_MIPS_TEXT;
9717 else
9718 sym->st_shndx = SHN_MIPS_DATA;
9719
9720 break;
9721 }
9722}
9723
9724/* Finish up dynamic symbol handling. We set the contents of various
9725 dynamic sections here. */
9726
b34976b6 9727bfd_boolean
9719ad41
RS
9728_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9729 struct bfd_link_info *info,
9730 struct elf_link_hash_entry *h,
9731 Elf_Internal_Sym *sym)
b49e97c9
TS
9732{
9733 bfd *dynobj;
b49e97c9 9734 asection *sgot;
f4416af6 9735 struct mips_got_info *g, *gg;
b49e97c9 9736 const char *name;
3d6746ca 9737 int idx;
5108fc1b 9738 struct mips_elf_link_hash_table *htab;
738e5348 9739 struct mips_elf_link_hash_entry *hmips;
b49e97c9 9740
5108fc1b 9741 htab = mips_elf_hash_table (info);
4dfe6ac6 9742 BFD_ASSERT (htab != NULL);
b49e97c9 9743 dynobj = elf_hash_table (info)->dynobj;
738e5348 9744 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9745
861fb55a
DJ
9746 BFD_ASSERT (!htab->is_vxworks);
9747
9748 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9749 {
9750 /* We've decided to create a PLT entry for this symbol. */
9751 bfd_byte *loc;
9752 bfd_vma header_address, plt_index, got_address;
9753 bfd_vma got_address_high, got_address_low, load;
9754 const bfd_vma *plt_entry;
9755
9756 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9757 BFD_ASSERT (h->dynindx != -1);
9758 BFD_ASSERT (htab->splt != NULL);
9759 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9760 BFD_ASSERT (!h->def_regular);
9761
9762 /* Calculate the address of the PLT header. */
9763 header_address = (htab->splt->output_section->vma
9764 + htab->splt->output_offset);
9765
9766 /* Calculate the index of the entry. */
9767 plt_index = ((h->plt.offset - htab->plt_header_size)
9768 / htab->plt_entry_size);
9769
9770 /* Calculate the address of the .got.plt entry. */
9771 got_address = (htab->sgotplt->output_section->vma
9772 + htab->sgotplt->output_offset
9773 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9774 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9775 got_address_low = got_address & 0xffff;
9776
9777 /* Initially point the .got.plt entry at the PLT header. */
9778 loc = (htab->sgotplt->contents
9779 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9780 if (ABI_64_P (output_bfd))
9781 bfd_put_64 (output_bfd, header_address, loc);
9782 else
9783 bfd_put_32 (output_bfd, header_address, loc);
9784
9785 /* Find out where the .plt entry should go. */
9786 loc = htab->splt->contents + h->plt.offset;
9787
9788 /* Pick the load opcode. */
9789 load = MIPS_ELF_LOAD_WORD (output_bfd);
9790
9791 /* Fill in the PLT entry itself. */
9792 plt_entry = mips_exec_plt_entry;
9793 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9794 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
6d30f5b2
NC
9795
9796 if (! LOAD_INTERLOCKS_P (output_bfd))
9797 {
9798 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9799 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9800 }
9801 else
9802 {
9803 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9804 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9805 }
861fb55a
DJ
9806
9807 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9808 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9809 plt_index, h->dynindx,
9810 R_MIPS_JUMP_SLOT, got_address);
9811
9812 /* We distinguish between PLT entries and lazy-binding stubs by
9813 giving the former an st_other value of STO_MIPS_PLT. Set the
9814 flag and leave the value if there are any relocations in the
9815 binary where pointer equality matters. */
9816 sym->st_shndx = SHN_UNDEF;
9817 if (h->pointer_equality_needed)
9818 sym->st_other = STO_MIPS_PLT;
9819 else
9820 sym->st_value = 0;
9821 }
9822 else if (h->plt.offset != MINUS_ONE)
b49e97c9 9823 {
861fb55a 9824 /* We've decided to create a lazy-binding stub. */
5108fc1b 9825 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
b49e97c9
TS
9826
9827 /* This symbol has a stub. Set it up. */
9828
9829 BFD_ASSERT (h->dynindx != -1);
9830
5108fc1b
RS
9831 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9832 || (h->dynindx <= 0xffff));
3d6746ca
DD
9833
9834 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
9835 sign extension at runtime in the stub, resulting in a negative
9836 index value. */
9837 if (h->dynindx & ~0x7fffffff)
b34976b6 9838 return FALSE;
b49e97c9
TS
9839
9840 /* Fill the stub. */
3d6746ca
DD
9841 idx = 0;
9842 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9843 idx += 4;
9844 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9845 idx += 4;
5108fc1b 9846 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
3d6746ca 9847 {
5108fc1b 9848 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
3d6746ca
DD
9849 stub + idx);
9850 idx += 4;
9851 }
9852 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9853 idx += 4;
b49e97c9 9854
3d6746ca
DD
9855 /* If a large stub is not required and sign extension is not a
9856 problem, then use legacy code in the stub. */
5108fc1b
RS
9857 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9858 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9859 else if (h->dynindx & ~0x7fff)
3d6746ca
DD
9860 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9861 else
5108fc1b
RS
9862 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9863 stub + idx);
9864
4e41d0d7
RS
9865 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9866 memcpy (htab->sstubs->contents + h->plt.offset,
9867 stub, htab->function_stub_size);
b49e97c9
TS
9868
9869 /* Mark the symbol as undefined. plt.offset != -1 occurs
9870 only for the referenced symbol. */
9871 sym->st_shndx = SHN_UNDEF;
9872
9873 /* The run-time linker uses the st_value field of the symbol
9874 to reset the global offset table entry for this external
9875 to its stub address when unlinking a shared object. */
4e41d0d7
RS
9876 sym->st_value = (htab->sstubs->output_section->vma
9877 + htab->sstubs->output_offset
c5ae1840 9878 + h->plt.offset);
b49e97c9
TS
9879 }
9880
738e5348
RS
9881 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9882 refer to the stub, since only the stub uses the standard calling
9883 conventions. */
9884 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9885 {
9886 BFD_ASSERT (hmips->need_fn_stub);
9887 sym->st_value = (hmips->fn_stub->output_section->vma
9888 + hmips->fn_stub->output_offset);
9889 sym->st_size = hmips->fn_stub->size;
9890 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9891 }
9892
b49e97c9 9893 BFD_ASSERT (h->dynindx != -1
f5385ebf 9894 || h->forced_local);
b49e97c9 9895
23cc69b6 9896 sgot = htab->sgot;
a8028dd0 9897 g = htab->got_info;
b49e97c9
TS
9898 BFD_ASSERT (g != NULL);
9899
9900 /* Run through the global symbol table, creating GOT entries for all
9901 the symbols that need them. */
020d7251 9902 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
9903 {
9904 bfd_vma offset;
9905 bfd_vma value;
9906
6eaa6adc 9907 value = sym->st_value;
738e5348
RS
9908 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9909 R_MIPS_GOT16, info);
b49e97c9
TS
9910 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9911 }
9912
020d7251 9913 if (hmips->global_got_area != GGA_NONE && g->next && h->type != STT_TLS)
f4416af6
AO
9914 {
9915 struct mips_got_entry e, *p;
0626d451 9916 bfd_vma entry;
f4416af6 9917 bfd_vma offset;
f4416af6
AO
9918
9919 gg = g;
9920
9921 e.abfd = output_bfd;
9922 e.symndx = -1;
738e5348 9923 e.d.h = hmips;
0f20cc35 9924 e.tls_type = 0;
143d77c5 9925
f4416af6
AO
9926 for (g = g->next; g->next != gg; g = g->next)
9927 {
9928 if (g->got_entries
9929 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9930 &e)))
9931 {
9932 offset = p->gotidx;
0626d451
RS
9933 if (info->shared
9934 || (elf_hash_table (info)->dynamic_sections_created
9935 && p->d.h != NULL
f5385ebf
AM
9936 && p->d.h->root.def_dynamic
9937 && !p->d.h->root.def_regular))
0626d451
RS
9938 {
9939 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9940 the various compatibility problems, it's easier to mock
9941 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9942 mips_elf_create_dynamic_relocation to calculate the
9943 appropriate addend. */
9944 Elf_Internal_Rela rel[3];
9945
9946 memset (rel, 0, sizeof (rel));
9947 if (ABI_64_P (output_bfd))
9948 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9949 else
9950 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9951 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9952
9953 entry = 0;
9954 if (! (mips_elf_create_dynamic_relocation
9955 (output_bfd, info, rel,
9956 e.d.h, NULL, sym->st_value, &entry, sgot)))
9957 return FALSE;
9958 }
9959 else
9960 entry = sym->st_value;
9961 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
9962 }
9963 }
9964 }
9965
b49e97c9
TS
9966 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9967 name = h->root.root.string;
9968 if (strcmp (name, "_DYNAMIC") == 0
22edb2f1 9969 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
9970 sym->st_shndx = SHN_ABS;
9971 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9972 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9973 {
9974 sym->st_shndx = SHN_ABS;
9975 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9976 sym->st_value = 1;
9977 }
4a14403c 9978 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9979 {
9980 sym->st_shndx = SHN_ABS;
9981 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9982 sym->st_value = elf_gp (output_bfd);
9983 }
9984 else if (SGI_COMPAT (output_bfd))
9985 {
9986 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9987 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9988 {
9989 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9990 sym->st_other = STO_PROTECTED;
9991 sym->st_value = 0;
9992 sym->st_shndx = SHN_MIPS_DATA;
9993 }
9994 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9995 {
9996 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9997 sym->st_other = STO_PROTECTED;
9998 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9999 sym->st_shndx = SHN_ABS;
10000 }
10001 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10002 {
10003 if (h->type == STT_FUNC)
10004 sym->st_shndx = SHN_MIPS_TEXT;
10005 else if (h->type == STT_OBJECT)
10006 sym->st_shndx = SHN_MIPS_DATA;
10007 }
10008 }
10009
861fb55a
DJ
10010 /* Emit a copy reloc, if needed. */
10011 if (h->needs_copy)
10012 {
10013 asection *s;
10014 bfd_vma symval;
10015
10016 BFD_ASSERT (h->dynindx != -1);
10017 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10018
10019 s = mips_elf_rel_dyn_section (info, FALSE);
10020 symval = (h->root.u.def.section->output_section->vma
10021 + h->root.u.def.section->output_offset
10022 + h->root.u.def.value);
10023 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10024 h->dynindx, R_MIPS_COPY, symval);
10025 }
10026
b49e97c9
TS
10027 /* Handle the IRIX6-specific symbols. */
10028 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10029 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10030
10031 if (! info->shared)
10032 {
10033 if (! mips_elf_hash_table (info)->use_rld_obj_head
10034 && (strcmp (name, "__rld_map") == 0
10035 || strcmp (name, "__RLD_MAP") == 0))
10036 {
10037 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
10038 BFD_ASSERT (s != NULL);
10039 sym->st_value = s->output_section->vma + s->output_offset;
9719ad41 10040 bfd_put_32 (output_bfd, 0, s->contents);
b49e97c9
TS
10041 if (mips_elf_hash_table (info)->rld_value == 0)
10042 mips_elf_hash_table (info)->rld_value = sym->st_value;
10043 }
10044 else if (mips_elf_hash_table (info)->use_rld_obj_head
10045 && strcmp (name, "__rld_obj_head") == 0)
10046 {
10047 /* IRIX6 does not use a .rld_map section. */
10048 if (IRIX_COMPAT (output_bfd) == ict_irix5
10049 || IRIX_COMPAT (output_bfd) == ict_none)
10050 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
10051 != NULL);
10052 mips_elf_hash_table (info)->rld_value = sym->st_value;
10053 }
10054 }
10055
738e5348
RS
10056 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
10057 treat MIPS16 symbols like any other. */
30c09090 10058 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
10059 {
10060 BFD_ASSERT (sym->st_value & 1);
10061 sym->st_other -= STO_MIPS16;
10062 }
b49e97c9 10063
b34976b6 10064 return TRUE;
b49e97c9
TS
10065}
10066
0a44bf69
RS
10067/* Likewise, for VxWorks. */
10068
10069bfd_boolean
10070_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10071 struct bfd_link_info *info,
10072 struct elf_link_hash_entry *h,
10073 Elf_Internal_Sym *sym)
10074{
10075 bfd *dynobj;
10076 asection *sgot;
10077 struct mips_got_info *g;
10078 struct mips_elf_link_hash_table *htab;
020d7251 10079 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
10080
10081 htab = mips_elf_hash_table (info);
4dfe6ac6 10082 BFD_ASSERT (htab != NULL);
0a44bf69 10083 dynobj = elf_hash_table (info)->dynobj;
020d7251 10084 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69
RS
10085
10086 if (h->plt.offset != (bfd_vma) -1)
10087 {
6d79d2ed 10088 bfd_byte *loc;
0a44bf69
RS
10089 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
10090 Elf_Internal_Rela rel;
10091 static const bfd_vma *plt_entry;
10092
10093 BFD_ASSERT (h->dynindx != -1);
10094 BFD_ASSERT (htab->splt != NULL);
10095 BFD_ASSERT (h->plt.offset <= htab->splt->size);
10096
10097 /* Calculate the address of the .plt entry. */
10098 plt_address = (htab->splt->output_section->vma
10099 + htab->splt->output_offset
10100 + h->plt.offset);
10101
10102 /* Calculate the index of the entry. */
10103 plt_index = ((h->plt.offset - htab->plt_header_size)
10104 / htab->plt_entry_size);
10105
10106 /* Calculate the address of the .got.plt entry. */
10107 got_address = (htab->sgotplt->output_section->vma
10108 + htab->sgotplt->output_offset
10109 + plt_index * 4);
10110
10111 /* Calculate the offset of the .got.plt entry from
10112 _GLOBAL_OFFSET_TABLE_. */
10113 got_offset = mips_elf_gotplt_index (info, h);
10114
10115 /* Calculate the offset for the branch at the start of the PLT
10116 entry. The branch jumps to the beginning of .plt. */
10117 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
10118
10119 /* Fill in the initial value of the .got.plt entry. */
10120 bfd_put_32 (output_bfd, plt_address,
10121 htab->sgotplt->contents + plt_index * 4);
10122
10123 /* Find out where the .plt entry should go. */
10124 loc = htab->splt->contents + h->plt.offset;
10125
10126 if (info->shared)
10127 {
10128 plt_entry = mips_vxworks_shared_plt_entry;
10129 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10130 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
10131 }
10132 else
10133 {
10134 bfd_vma got_address_high, got_address_low;
10135
10136 plt_entry = mips_vxworks_exec_plt_entry;
10137 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10138 got_address_low = got_address & 0xffff;
10139
10140 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10141 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
10142 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10143 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10144 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10145 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10146 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10147 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10148
10149 loc = (htab->srelplt2->contents
10150 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
10151
10152 /* Emit a relocation for the .got.plt entry. */
10153 rel.r_offset = got_address;
10154 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10155 rel.r_addend = h->plt.offset;
10156 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10157
10158 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10159 loc += sizeof (Elf32_External_Rela);
10160 rel.r_offset = plt_address + 8;
10161 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10162 rel.r_addend = got_offset;
10163 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10164
10165 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10166 loc += sizeof (Elf32_External_Rela);
10167 rel.r_offset += 4;
10168 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10169 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10170 }
10171
10172 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10173 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
10174 rel.r_offset = got_address;
10175 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10176 rel.r_addend = 0;
10177 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10178
10179 if (!h->def_regular)
10180 sym->st_shndx = SHN_UNDEF;
10181 }
10182
10183 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10184
23cc69b6 10185 sgot = htab->sgot;
a8028dd0 10186 g = htab->got_info;
0a44bf69
RS
10187 BFD_ASSERT (g != NULL);
10188
10189 /* See if this symbol has an entry in the GOT. */
020d7251 10190 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
10191 {
10192 bfd_vma offset;
10193 Elf_Internal_Rela outrel;
10194 bfd_byte *loc;
10195 asection *s;
10196
10197 /* Install the symbol value in the GOT. */
10198 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
10199 R_MIPS_GOT16, info);
10200 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10201
10202 /* Add a dynamic relocation for it. */
10203 s = mips_elf_rel_dyn_section (info, FALSE);
10204 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10205 outrel.r_offset = (sgot->output_section->vma
10206 + sgot->output_offset
10207 + offset);
10208 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10209 outrel.r_addend = 0;
10210 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10211 }
10212
10213 /* Emit a copy reloc, if needed. */
10214 if (h->needs_copy)
10215 {
10216 Elf_Internal_Rela rel;
10217
10218 BFD_ASSERT (h->dynindx != -1);
10219
10220 rel.r_offset = (h->root.u.def.section->output_section->vma
10221 + h->root.u.def.section->output_offset
10222 + h->root.u.def.value);
10223 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10224 rel.r_addend = 0;
10225 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10226 htab->srelbss->contents
10227 + (htab->srelbss->reloc_count
10228 * sizeof (Elf32_External_Rela)));
10229 ++htab->srelbss->reloc_count;
10230 }
10231
df58fc94
RS
10232 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10233 if (ELF_ST_IS_COMPRESSED (sym->st_other))
0a44bf69
RS
10234 sym->st_value &= ~1;
10235
10236 return TRUE;
10237}
10238
861fb55a
DJ
10239/* Write out a plt0 entry to the beginning of .plt. */
10240
10241static void
10242mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10243{
10244 bfd_byte *loc;
10245 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10246 static const bfd_vma *plt_entry;
10247 struct mips_elf_link_hash_table *htab;
10248
10249 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10250 BFD_ASSERT (htab != NULL);
10251
861fb55a
DJ
10252 if (ABI_64_P (output_bfd))
10253 plt_entry = mips_n64_exec_plt0_entry;
10254 else if (ABI_N32_P (output_bfd))
10255 plt_entry = mips_n32_exec_plt0_entry;
10256 else
10257 plt_entry = mips_o32_exec_plt0_entry;
10258
10259 /* Calculate the value of .got.plt. */
10260 gotplt_value = (htab->sgotplt->output_section->vma
10261 + htab->sgotplt->output_offset);
10262 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10263 gotplt_value_low = gotplt_value & 0xffff;
10264
10265 /* The PLT sequence is not safe for N64 if .got.plt's address can
10266 not be loaded in two instructions. */
10267 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10268 || ~(gotplt_value | 0x7fffffff) == 0);
10269
10270 /* Install the PLT header. */
10271 loc = htab->splt->contents;
10272 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
10273 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
10274 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
10275 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10276 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10277 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10278 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10279 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10280}
10281
0a44bf69
RS
10282/* Install the PLT header for a VxWorks executable and finalize the
10283 contents of .rela.plt.unloaded. */
10284
10285static void
10286mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10287{
10288 Elf_Internal_Rela rela;
10289 bfd_byte *loc;
10290 bfd_vma got_value, got_value_high, got_value_low, plt_address;
10291 static const bfd_vma *plt_entry;
10292 struct mips_elf_link_hash_table *htab;
10293
10294 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10295 BFD_ASSERT (htab != NULL);
10296
0a44bf69
RS
10297 plt_entry = mips_vxworks_exec_plt0_entry;
10298
10299 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
10300 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
10301 + htab->root.hgot->root.u.def.section->output_offset
10302 + htab->root.hgot->root.u.def.value);
10303
10304 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
10305 got_value_low = got_value & 0xffff;
10306
10307 /* Calculate the address of the PLT header. */
10308 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
10309
10310 /* Install the PLT header. */
10311 loc = htab->splt->contents;
10312 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
10313 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
10314 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
10315 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10316 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10317 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10318
10319 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
10320 loc = htab->srelplt2->contents;
10321 rela.r_offset = plt_address;
10322 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10323 rela.r_addend = 0;
10324 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10325 loc += sizeof (Elf32_External_Rela);
10326
10327 /* Output the relocation for the following addiu of
10328 %lo(_GLOBAL_OFFSET_TABLE_). */
10329 rela.r_offset += 4;
10330 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10331 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10332 loc += sizeof (Elf32_External_Rela);
10333
10334 /* Fix up the remaining relocations. They may have the wrong
10335 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10336 in which symbols were output. */
10337 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10338 {
10339 Elf_Internal_Rela rel;
10340
10341 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10342 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10343 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10344 loc += sizeof (Elf32_External_Rela);
10345
10346 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10347 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10348 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10349 loc += sizeof (Elf32_External_Rela);
10350
10351 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10352 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10353 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10354 loc += sizeof (Elf32_External_Rela);
10355 }
10356}
10357
10358/* Install the PLT header for a VxWorks shared library. */
10359
10360static void
10361mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10362{
10363 unsigned int i;
10364 struct mips_elf_link_hash_table *htab;
10365
10366 htab = mips_elf_hash_table (info);
4dfe6ac6 10367 BFD_ASSERT (htab != NULL);
0a44bf69
RS
10368
10369 /* We just need to copy the entry byte-by-byte. */
10370 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10371 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10372 htab->splt->contents + i * 4);
10373}
10374
b49e97c9
TS
10375/* Finish up the dynamic sections. */
10376
b34976b6 10377bfd_boolean
9719ad41
RS
10378_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10379 struct bfd_link_info *info)
b49e97c9
TS
10380{
10381 bfd *dynobj;
10382 asection *sdyn;
10383 asection *sgot;
f4416af6 10384 struct mips_got_info *gg, *g;
0a44bf69 10385 struct mips_elf_link_hash_table *htab;
b49e97c9 10386
0a44bf69 10387 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10388 BFD_ASSERT (htab != NULL);
10389
b49e97c9
TS
10390 dynobj = elf_hash_table (info)->dynobj;
10391
10392 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10393
23cc69b6
RS
10394 sgot = htab->sgot;
10395 gg = htab->got_info;
b49e97c9
TS
10396
10397 if (elf_hash_table (info)->dynamic_sections_created)
10398 {
10399 bfd_byte *b;
943284cc 10400 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
10401
10402 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
10403 BFD_ASSERT (gg != NULL);
10404
10405 g = mips_elf_got_for_ibfd (gg, output_bfd);
b49e97c9
TS
10406 BFD_ASSERT (g != NULL);
10407
10408 for (b = sdyn->contents;
eea6121a 10409 b < sdyn->contents + sdyn->size;
b49e97c9
TS
10410 b += MIPS_ELF_DYN_SIZE (dynobj))
10411 {
10412 Elf_Internal_Dyn dyn;
10413 const char *name;
10414 size_t elemsize;
10415 asection *s;
b34976b6 10416 bfd_boolean swap_out_p;
b49e97c9
TS
10417
10418 /* Read in the current dynamic entry. */
10419 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10420
10421 /* Assume that we're going to modify it and write it out. */
b34976b6 10422 swap_out_p = TRUE;
b49e97c9
TS
10423
10424 switch (dyn.d_tag)
10425 {
10426 case DT_RELENT:
b49e97c9
TS
10427 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10428 break;
10429
0a44bf69
RS
10430 case DT_RELAENT:
10431 BFD_ASSERT (htab->is_vxworks);
10432 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10433 break;
10434
b49e97c9
TS
10435 case DT_STRSZ:
10436 /* Rewrite DT_STRSZ. */
10437 dyn.d_un.d_val =
10438 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10439 break;
10440
10441 case DT_PLTGOT:
861fb55a
DJ
10442 s = htab->sgot;
10443 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10444 break;
10445
10446 case DT_MIPS_PLTGOT:
10447 s = htab->sgotplt;
10448 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
10449 break;
10450
10451 case DT_MIPS_RLD_VERSION:
10452 dyn.d_un.d_val = 1; /* XXX */
10453 break;
10454
10455 case DT_MIPS_FLAGS:
10456 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10457 break;
10458
b49e97c9 10459 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
10460 {
10461 time_t t;
10462 time (&t);
10463 dyn.d_un.d_val = t;
10464 }
b49e97c9
TS
10465 break;
10466
10467 case DT_MIPS_ICHECKSUM:
10468 /* XXX FIXME: */
b34976b6 10469 swap_out_p = FALSE;
b49e97c9
TS
10470 break;
10471
10472 case DT_MIPS_IVERSION:
10473 /* XXX FIXME: */
b34976b6 10474 swap_out_p = FALSE;
b49e97c9
TS
10475 break;
10476
10477 case DT_MIPS_BASE_ADDRESS:
10478 s = output_bfd->sections;
10479 BFD_ASSERT (s != NULL);
10480 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10481 break;
10482
10483 case DT_MIPS_LOCAL_GOTNO:
10484 dyn.d_un.d_val = g->local_gotno;
10485 break;
10486
10487 case DT_MIPS_UNREFEXTNO:
10488 /* The index into the dynamic symbol table which is the
10489 entry of the first external symbol that is not
10490 referenced within the same object. */
10491 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10492 break;
10493
10494 case DT_MIPS_GOTSYM:
f4416af6 10495 if (gg->global_gotsym)
b49e97c9 10496 {
f4416af6 10497 dyn.d_un.d_val = gg->global_gotsym->dynindx;
b49e97c9
TS
10498 break;
10499 }
10500 /* In case if we don't have global got symbols we default
10501 to setting DT_MIPS_GOTSYM to the same value as
10502 DT_MIPS_SYMTABNO, so we just fall through. */
10503
10504 case DT_MIPS_SYMTABNO:
10505 name = ".dynsym";
10506 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10507 s = bfd_get_section_by_name (output_bfd, name);
10508 BFD_ASSERT (s != NULL);
10509
eea6121a 10510 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
10511 break;
10512
10513 case DT_MIPS_HIPAGENO:
861fb55a 10514 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
10515 break;
10516
10517 case DT_MIPS_RLD_MAP:
10518 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10519 break;
10520
10521 case DT_MIPS_OPTIONS:
10522 s = (bfd_get_section_by_name
10523 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10524 dyn.d_un.d_ptr = s->vma;
10525 break;
10526
0a44bf69
RS
10527 case DT_RELASZ:
10528 BFD_ASSERT (htab->is_vxworks);
10529 /* The count does not include the JUMP_SLOT relocations. */
10530 if (htab->srelplt)
10531 dyn.d_un.d_val -= htab->srelplt->size;
10532 break;
10533
10534 case DT_PLTREL:
861fb55a
DJ
10535 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10536 if (htab->is_vxworks)
10537 dyn.d_un.d_val = DT_RELA;
10538 else
10539 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
10540 break;
10541
10542 case DT_PLTRELSZ:
861fb55a 10543 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
10544 dyn.d_un.d_val = htab->srelplt->size;
10545 break;
10546
10547 case DT_JMPREL:
861fb55a
DJ
10548 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10549 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
10550 + htab->srelplt->output_offset);
10551 break;
10552
943284cc
DJ
10553 case DT_TEXTREL:
10554 /* If we didn't need any text relocations after all, delete
10555 the dynamic tag. */
10556 if (!(info->flags & DF_TEXTREL))
10557 {
10558 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10559 swap_out_p = FALSE;
10560 }
10561 break;
10562
10563 case DT_FLAGS:
10564 /* If we didn't need any text relocations after all, clear
10565 DF_TEXTREL from DT_FLAGS. */
10566 if (!(info->flags & DF_TEXTREL))
10567 dyn.d_un.d_val &= ~DF_TEXTREL;
10568 else
10569 swap_out_p = FALSE;
10570 break;
10571
b49e97c9 10572 default:
b34976b6 10573 swap_out_p = FALSE;
7a2b07ff
NS
10574 if (htab->is_vxworks
10575 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10576 swap_out_p = TRUE;
b49e97c9
TS
10577 break;
10578 }
10579
943284cc 10580 if (swap_out_p || dyn_skipped)
b49e97c9 10581 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
10582 (dynobj, &dyn, b - dyn_skipped);
10583
10584 if (dyn_to_skip)
10585 {
10586 dyn_skipped += dyn_to_skip;
10587 dyn_to_skip = 0;
10588 }
b49e97c9 10589 }
943284cc
DJ
10590
10591 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10592 if (dyn_skipped > 0)
10593 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
10594 }
10595
b55fd4d4
DJ
10596 if (sgot != NULL && sgot->size > 0
10597 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 10598 {
0a44bf69
RS
10599 if (htab->is_vxworks)
10600 {
10601 /* The first entry of the global offset table points to the
10602 ".dynamic" section. The second is initialized by the
10603 loader and contains the shared library identifier.
10604 The third is also initialized by the loader and points
10605 to the lazy resolution stub. */
10606 MIPS_ELF_PUT_WORD (output_bfd,
10607 sdyn->output_offset + sdyn->output_section->vma,
10608 sgot->contents);
10609 MIPS_ELF_PUT_WORD (output_bfd, 0,
10610 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10611 MIPS_ELF_PUT_WORD (output_bfd, 0,
10612 sgot->contents
10613 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10614 }
10615 else
10616 {
10617 /* The first entry of the global offset table will be filled at
10618 runtime. The second entry will be used by some runtime loaders.
10619 This isn't the case of IRIX rld. */
10620 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 10621 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
10622 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10623 }
b49e97c9 10624
54938e2a
TS
10625 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10626 = MIPS_ELF_GOT_SIZE (output_bfd);
10627 }
b49e97c9 10628
f4416af6
AO
10629 /* Generate dynamic relocations for the non-primary gots. */
10630 if (gg != NULL && gg->next)
10631 {
10632 Elf_Internal_Rela rel[3];
10633 bfd_vma addend = 0;
10634
10635 memset (rel, 0, sizeof (rel));
10636 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10637
10638 for (g = gg->next; g->next != gg; g = g->next)
10639 {
91d6fa6a 10640 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 10641 + g->next->tls_gotno;
f4416af6 10642
9719ad41 10643 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 10644 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
10645 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10646 sgot->contents
91d6fa6a 10647 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
10648
10649 if (! info->shared)
10650 continue;
10651
91d6fa6a 10652 while (got_index < g->assigned_gotno)
f4416af6
AO
10653 {
10654 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
91d6fa6a 10655 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
10656 if (!(mips_elf_create_dynamic_relocation
10657 (output_bfd, info, rel, NULL,
10658 bfd_abs_section_ptr,
10659 0, &addend, sgot)))
10660 return FALSE;
10661 BFD_ASSERT (addend == 0);
10662 }
10663 }
10664 }
10665
3133ddbf
DJ
10666 /* The generation of dynamic relocations for the non-primary gots
10667 adds more dynamic relocations. We cannot count them until
10668 here. */
10669
10670 if (elf_hash_table (info)->dynamic_sections_created)
10671 {
10672 bfd_byte *b;
10673 bfd_boolean swap_out_p;
10674
10675 BFD_ASSERT (sdyn != NULL);
10676
10677 for (b = sdyn->contents;
10678 b < sdyn->contents + sdyn->size;
10679 b += MIPS_ELF_DYN_SIZE (dynobj))
10680 {
10681 Elf_Internal_Dyn dyn;
10682 asection *s;
10683
10684 /* Read in the current dynamic entry. */
10685 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10686
10687 /* Assume that we're going to modify it and write it out. */
10688 swap_out_p = TRUE;
10689
10690 switch (dyn.d_tag)
10691 {
10692 case DT_RELSZ:
10693 /* Reduce DT_RELSZ to account for any relocations we
10694 decided not to make. This is for the n64 irix rld,
10695 which doesn't seem to apply any relocations if there
10696 are trailing null entries. */
0a44bf69 10697 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
10698 dyn.d_un.d_val = (s->reloc_count
10699 * (ABI_64_P (output_bfd)
10700 ? sizeof (Elf64_Mips_External_Rel)
10701 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
10702 /* Adjust the section size too. Tools like the prelinker
10703 can reasonably expect the values to the same. */
10704 elf_section_data (s->output_section)->this_hdr.sh_size
10705 = dyn.d_un.d_val;
3133ddbf
DJ
10706 break;
10707
10708 default:
10709 swap_out_p = FALSE;
10710 break;
10711 }
10712
10713 if (swap_out_p)
10714 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10715 (dynobj, &dyn, b);
10716 }
10717 }
10718
b49e97c9 10719 {
b49e97c9
TS
10720 asection *s;
10721 Elf32_compact_rel cpt;
10722
b49e97c9
TS
10723 if (SGI_COMPAT (output_bfd))
10724 {
10725 /* Write .compact_rel section out. */
10726 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10727 if (s != NULL)
10728 {
10729 cpt.id1 = 1;
10730 cpt.num = s->reloc_count;
10731 cpt.id2 = 2;
10732 cpt.offset = (s->output_section->filepos
10733 + sizeof (Elf32_External_compact_rel));
10734 cpt.reserved0 = 0;
10735 cpt.reserved1 = 0;
10736 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10737 ((Elf32_External_compact_rel *)
10738 s->contents));
10739
10740 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 10741 if (htab->sstubs != NULL)
b49e97c9
TS
10742 {
10743 file_ptr dummy_offset;
10744
4e41d0d7
RS
10745 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10746 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10747 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 10748 htab->function_stub_size);
b49e97c9
TS
10749 }
10750 }
10751 }
10752
0a44bf69
RS
10753 /* The psABI says that the dynamic relocations must be sorted in
10754 increasing order of r_symndx. The VxWorks EABI doesn't require
10755 this, and because the code below handles REL rather than RELA
10756 relocations, using it for VxWorks would be outright harmful. */
10757 if (!htab->is_vxworks)
b49e97c9 10758 {
0a44bf69
RS
10759 s = mips_elf_rel_dyn_section (info, FALSE);
10760 if (s != NULL
10761 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10762 {
10763 reldyn_sorting_bfd = output_bfd;
b49e97c9 10764
0a44bf69
RS
10765 if (ABI_64_P (output_bfd))
10766 qsort ((Elf64_External_Rel *) s->contents + 1,
10767 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10768 sort_dynamic_relocs_64);
10769 else
10770 qsort ((Elf32_External_Rel *) s->contents + 1,
10771 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10772 sort_dynamic_relocs);
10773 }
b49e97c9 10774 }
b49e97c9
TS
10775 }
10776
861fb55a 10777 if (htab->splt && htab->splt->size > 0)
0a44bf69 10778 {
861fb55a
DJ
10779 if (htab->is_vxworks)
10780 {
10781 if (info->shared)
10782 mips_vxworks_finish_shared_plt (output_bfd, info);
10783 else
10784 mips_vxworks_finish_exec_plt (output_bfd, info);
10785 }
0a44bf69 10786 else
861fb55a
DJ
10787 {
10788 BFD_ASSERT (!info->shared);
10789 mips_finish_exec_plt (output_bfd, info);
10790 }
0a44bf69 10791 }
b34976b6 10792 return TRUE;
b49e97c9
TS
10793}
10794
b49e97c9 10795
64543e1a
RS
10796/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10797
10798static void
9719ad41 10799mips_set_isa_flags (bfd *abfd)
b49e97c9 10800{
64543e1a 10801 flagword val;
b49e97c9
TS
10802
10803 switch (bfd_get_mach (abfd))
10804 {
10805 default:
10806 case bfd_mach_mips3000:
10807 val = E_MIPS_ARCH_1;
10808 break;
10809
10810 case bfd_mach_mips3900:
10811 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10812 break;
10813
10814 case bfd_mach_mips6000:
10815 val = E_MIPS_ARCH_2;
10816 break;
10817
10818 case bfd_mach_mips4000:
10819 case bfd_mach_mips4300:
10820 case bfd_mach_mips4400:
10821 case bfd_mach_mips4600:
10822 val = E_MIPS_ARCH_3;
10823 break;
10824
10825 case bfd_mach_mips4010:
10826 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10827 break;
10828
10829 case bfd_mach_mips4100:
10830 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10831 break;
10832
10833 case bfd_mach_mips4111:
10834 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10835 break;
10836
00707a0e
RS
10837 case bfd_mach_mips4120:
10838 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10839 break;
10840
b49e97c9
TS
10841 case bfd_mach_mips4650:
10842 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10843 break;
10844
00707a0e
RS
10845 case bfd_mach_mips5400:
10846 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10847 break;
10848
10849 case bfd_mach_mips5500:
10850 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10851 break;
10852
0d2e43ed
ILT
10853 case bfd_mach_mips9000:
10854 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10855 break;
10856
b49e97c9 10857 case bfd_mach_mips5000:
5a7ea749 10858 case bfd_mach_mips7000:
b49e97c9
TS
10859 case bfd_mach_mips8000:
10860 case bfd_mach_mips10000:
10861 case bfd_mach_mips12000:
3aa3176b
TS
10862 case bfd_mach_mips14000:
10863 case bfd_mach_mips16000:
b49e97c9
TS
10864 val = E_MIPS_ARCH_4;
10865 break;
10866
10867 case bfd_mach_mips5:
10868 val = E_MIPS_ARCH_5;
10869 break;
10870
350cc38d
MS
10871 case bfd_mach_mips_loongson_2e:
10872 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10873 break;
10874
10875 case bfd_mach_mips_loongson_2f:
10876 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10877 break;
10878
b49e97c9
TS
10879 case bfd_mach_mips_sb1:
10880 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10881 break;
10882
d051516a
NC
10883 case bfd_mach_mips_loongson_3a:
10884 val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
10885 break;
10886
6f179bd0
AN
10887 case bfd_mach_mips_octeon:
10888 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10889 break;
10890
52b6b6b9
JM
10891 case bfd_mach_mips_xlr:
10892 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10893 break;
10894
b49e97c9
TS
10895 case bfd_mach_mipsisa32:
10896 val = E_MIPS_ARCH_32;
10897 break;
10898
10899 case bfd_mach_mipsisa64:
10900 val = E_MIPS_ARCH_64;
af7ee8bf
CD
10901 break;
10902
10903 case bfd_mach_mipsisa32r2:
10904 val = E_MIPS_ARCH_32R2;
10905 break;
5f74bc13
CD
10906
10907 case bfd_mach_mipsisa64r2:
10908 val = E_MIPS_ARCH_64R2;
10909 break;
b49e97c9 10910 }
b49e97c9
TS
10911 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10912 elf_elfheader (abfd)->e_flags |= val;
10913
64543e1a
RS
10914}
10915
10916
10917/* The final processing done just before writing out a MIPS ELF object
10918 file. This gets the MIPS architecture right based on the machine
10919 number. This is used by both the 32-bit and the 64-bit ABI. */
10920
10921void
9719ad41
RS
10922_bfd_mips_elf_final_write_processing (bfd *abfd,
10923 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
10924{
10925 unsigned int i;
10926 Elf_Internal_Shdr **hdrpp;
10927 const char *name;
10928 asection *sec;
10929
10930 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10931 is nonzero. This is for compatibility with old objects, which used
10932 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10933 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10934 mips_set_isa_flags (abfd);
10935
b49e97c9
TS
10936 /* Set the sh_info field for .gptab sections and other appropriate
10937 info for each special section. */
10938 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10939 i < elf_numsections (abfd);
10940 i++, hdrpp++)
10941 {
10942 switch ((*hdrpp)->sh_type)
10943 {
10944 case SHT_MIPS_MSYM:
10945 case SHT_MIPS_LIBLIST:
10946 sec = bfd_get_section_by_name (abfd, ".dynstr");
10947 if (sec != NULL)
10948 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10949 break;
10950
10951 case SHT_MIPS_GPTAB:
10952 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10953 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10954 BFD_ASSERT (name != NULL
0112cd26 10955 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
10956 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10957 BFD_ASSERT (sec != NULL);
10958 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10959 break;
10960
10961 case SHT_MIPS_CONTENT:
10962 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10963 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10964 BFD_ASSERT (name != NULL
0112cd26 10965 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
10966 sec = bfd_get_section_by_name (abfd,
10967 name + sizeof ".MIPS.content" - 1);
10968 BFD_ASSERT (sec != NULL);
10969 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10970 break;
10971
10972 case SHT_MIPS_SYMBOL_LIB:
10973 sec = bfd_get_section_by_name (abfd, ".dynsym");
10974 if (sec != NULL)
10975 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10976 sec = bfd_get_section_by_name (abfd, ".liblist");
10977 if (sec != NULL)
10978 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10979 break;
10980
10981 case SHT_MIPS_EVENTS:
10982 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10983 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10984 BFD_ASSERT (name != NULL);
0112cd26 10985 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
10986 sec = bfd_get_section_by_name (abfd,
10987 name + sizeof ".MIPS.events" - 1);
10988 else
10989 {
0112cd26 10990 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
10991 sec = bfd_get_section_by_name (abfd,
10992 (name
10993 + sizeof ".MIPS.post_rel" - 1));
10994 }
10995 BFD_ASSERT (sec != NULL);
10996 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10997 break;
10998
10999 }
11000 }
11001}
11002\f
8dc1a139 11003/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
11004 segments. */
11005
11006int
a6b96beb
AM
11007_bfd_mips_elf_additional_program_headers (bfd *abfd,
11008 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
11009{
11010 asection *s;
11011 int ret = 0;
11012
11013 /* See if we need a PT_MIPS_REGINFO segment. */
11014 s = bfd_get_section_by_name (abfd, ".reginfo");
11015 if (s && (s->flags & SEC_LOAD))
11016 ++ret;
11017
11018 /* See if we need a PT_MIPS_OPTIONS segment. */
11019 if (IRIX_COMPAT (abfd) == ict_irix6
11020 && bfd_get_section_by_name (abfd,
11021 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11022 ++ret;
11023
11024 /* See if we need a PT_MIPS_RTPROC segment. */
11025 if (IRIX_COMPAT (abfd) == ict_irix5
11026 && bfd_get_section_by_name (abfd, ".dynamic")
11027 && bfd_get_section_by_name (abfd, ".mdebug"))
11028 ++ret;
11029
98c904a8
RS
11030 /* Allocate a PT_NULL header in dynamic objects. See
11031 _bfd_mips_elf_modify_segment_map for details. */
11032 if (!SGI_COMPAT (abfd)
11033 && bfd_get_section_by_name (abfd, ".dynamic"))
11034 ++ret;
11035
b49e97c9
TS
11036 return ret;
11037}
11038
8dc1a139 11039/* Modify the segment map for an IRIX5 executable. */
b49e97c9 11040
b34976b6 11041bfd_boolean
9719ad41 11042_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 11043 struct bfd_link_info *info)
b49e97c9
TS
11044{
11045 asection *s;
11046 struct elf_segment_map *m, **pm;
11047 bfd_size_type amt;
11048
11049 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11050 segment. */
11051 s = bfd_get_section_by_name (abfd, ".reginfo");
11052 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11053 {
11054 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11055 if (m->p_type == PT_MIPS_REGINFO)
11056 break;
11057 if (m == NULL)
11058 {
11059 amt = sizeof *m;
9719ad41 11060 m = bfd_zalloc (abfd, amt);
b49e97c9 11061 if (m == NULL)
b34976b6 11062 return FALSE;
b49e97c9
TS
11063
11064 m->p_type = PT_MIPS_REGINFO;
11065 m->count = 1;
11066 m->sections[0] = s;
11067
11068 /* We want to put it after the PHDR and INTERP segments. */
11069 pm = &elf_tdata (abfd)->segment_map;
11070 while (*pm != NULL
11071 && ((*pm)->p_type == PT_PHDR
11072 || (*pm)->p_type == PT_INTERP))
11073 pm = &(*pm)->next;
11074
11075 m->next = *pm;
11076 *pm = m;
11077 }
11078 }
11079
11080 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11081 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 11082 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 11083 table. */
c1fd6598
AO
11084 if (NEWABI_P (abfd)
11085 /* On non-IRIX6 new abi, we'll have already created a segment
11086 for this section, so don't create another. I'm not sure this
11087 is not also the case for IRIX 6, but I can't test it right
11088 now. */
11089 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
11090 {
11091 for (s = abfd->sections; s; s = s->next)
11092 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11093 break;
11094
11095 if (s)
11096 {
11097 struct elf_segment_map *options_segment;
11098
98a8deaf
RS
11099 pm = &elf_tdata (abfd)->segment_map;
11100 while (*pm != NULL
11101 && ((*pm)->p_type == PT_PHDR
11102 || (*pm)->p_type == PT_INTERP))
11103 pm = &(*pm)->next;
b49e97c9 11104
8ded5a0f
AM
11105 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11106 {
11107 amt = sizeof (struct elf_segment_map);
11108 options_segment = bfd_zalloc (abfd, amt);
11109 options_segment->next = *pm;
11110 options_segment->p_type = PT_MIPS_OPTIONS;
11111 options_segment->p_flags = PF_R;
11112 options_segment->p_flags_valid = TRUE;
11113 options_segment->count = 1;
11114 options_segment->sections[0] = s;
11115 *pm = options_segment;
11116 }
b49e97c9
TS
11117 }
11118 }
11119 else
11120 {
11121 if (IRIX_COMPAT (abfd) == ict_irix5)
11122 {
11123 /* If there are .dynamic and .mdebug sections, we make a room
11124 for the RTPROC header. FIXME: Rewrite without section names. */
11125 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11126 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11127 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11128 {
11129 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11130 if (m->p_type == PT_MIPS_RTPROC)
11131 break;
11132 if (m == NULL)
11133 {
11134 amt = sizeof *m;
9719ad41 11135 m = bfd_zalloc (abfd, amt);
b49e97c9 11136 if (m == NULL)
b34976b6 11137 return FALSE;
b49e97c9
TS
11138
11139 m->p_type = PT_MIPS_RTPROC;
11140
11141 s = bfd_get_section_by_name (abfd, ".rtproc");
11142 if (s == NULL)
11143 {
11144 m->count = 0;
11145 m->p_flags = 0;
11146 m->p_flags_valid = 1;
11147 }
11148 else
11149 {
11150 m->count = 1;
11151 m->sections[0] = s;
11152 }
11153
11154 /* We want to put it after the DYNAMIC segment. */
11155 pm = &elf_tdata (abfd)->segment_map;
11156 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11157 pm = &(*pm)->next;
11158 if (*pm != NULL)
11159 pm = &(*pm)->next;
11160
11161 m->next = *pm;
11162 *pm = m;
11163 }
11164 }
11165 }
8dc1a139 11166 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
11167 .dynstr, .dynsym, and .hash sections, and everything in
11168 between. */
11169 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
11170 pm = &(*pm)->next)
11171 if ((*pm)->p_type == PT_DYNAMIC)
11172 break;
11173 m = *pm;
11174 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
11175 {
11176 /* For a normal mips executable the permissions for the PT_DYNAMIC
11177 segment are read, write and execute. We do that here since
11178 the code in elf.c sets only the read permission. This matters
11179 sometimes for the dynamic linker. */
11180 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
11181 {
11182 m->p_flags = PF_R | PF_W | PF_X;
11183 m->p_flags_valid = 1;
11184 }
11185 }
f6f62d6f
RS
11186 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11187 glibc's dynamic linker has traditionally derived the number of
11188 tags from the p_filesz field, and sometimes allocates stack
11189 arrays of that size. An overly-big PT_DYNAMIC segment can
11190 be actively harmful in such cases. Making PT_DYNAMIC contain
11191 other sections can also make life hard for the prelinker,
11192 which might move one of the other sections to a different
11193 PT_LOAD segment. */
11194 if (SGI_COMPAT (abfd)
11195 && m != NULL
11196 && m->count == 1
11197 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
11198 {
11199 static const char *sec_names[] =
11200 {
11201 ".dynamic", ".dynstr", ".dynsym", ".hash"
11202 };
11203 bfd_vma low, high;
11204 unsigned int i, c;
11205 struct elf_segment_map *n;
11206
792b4a53 11207 low = ~(bfd_vma) 0;
b49e97c9
TS
11208 high = 0;
11209 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11210 {
11211 s = bfd_get_section_by_name (abfd, sec_names[i]);
11212 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11213 {
11214 bfd_size_type sz;
11215
11216 if (low > s->vma)
11217 low = s->vma;
eea6121a 11218 sz = s->size;
b49e97c9
TS
11219 if (high < s->vma + sz)
11220 high = s->vma + sz;
11221 }
11222 }
11223
11224 c = 0;
11225 for (s = abfd->sections; s != NULL; s = s->next)
11226 if ((s->flags & SEC_LOAD) != 0
11227 && s->vma >= low
eea6121a 11228 && s->vma + s->size <= high)
b49e97c9
TS
11229 ++c;
11230
11231 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 11232 n = bfd_zalloc (abfd, amt);
b49e97c9 11233 if (n == NULL)
b34976b6 11234 return FALSE;
b49e97c9
TS
11235 *n = *m;
11236 n->count = c;
11237
11238 i = 0;
11239 for (s = abfd->sections; s != NULL; s = s->next)
11240 {
11241 if ((s->flags & SEC_LOAD) != 0
11242 && s->vma >= low
eea6121a 11243 && s->vma + s->size <= high)
b49e97c9
TS
11244 {
11245 n->sections[i] = s;
11246 ++i;
11247 }
11248 }
11249
11250 *pm = n;
11251 }
11252 }
11253
98c904a8
RS
11254 /* Allocate a spare program header in dynamic objects so that tools
11255 like the prelinker can add an extra PT_LOAD entry.
11256
11257 If the prelinker needs to make room for a new PT_LOAD entry, its
11258 standard procedure is to move the first (read-only) sections into
11259 the new (writable) segment. However, the MIPS ABI requires
11260 .dynamic to be in a read-only segment, and the section will often
11261 start within sizeof (ElfNN_Phdr) bytes of the last program header.
11262
11263 Although the prelinker could in principle move .dynamic to a
11264 writable segment, it seems better to allocate a spare program
11265 header instead, and avoid the need to move any sections.
11266 There is a long tradition of allocating spare dynamic tags,
11267 so allocating a spare program header seems like a natural
7c8b76cc
JM
11268 extension.
11269
11270 If INFO is NULL, we may be copying an already prelinked binary
11271 with objcopy or strip, so do not add this header. */
11272 if (info != NULL
11273 && !SGI_COMPAT (abfd)
98c904a8
RS
11274 && bfd_get_section_by_name (abfd, ".dynamic"))
11275 {
11276 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
11277 if ((*pm)->p_type == PT_NULL)
11278 break;
11279 if (*pm == NULL)
11280 {
11281 m = bfd_zalloc (abfd, sizeof (*m));
11282 if (m == NULL)
11283 return FALSE;
11284
11285 m->p_type = PT_NULL;
11286 *pm = m;
11287 }
11288 }
11289
b34976b6 11290 return TRUE;
b49e97c9
TS
11291}
11292\f
11293/* Return the section that should be marked against GC for a given
11294 relocation. */
11295
11296asection *
9719ad41 11297_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 11298 struct bfd_link_info *info,
9719ad41
RS
11299 Elf_Internal_Rela *rel,
11300 struct elf_link_hash_entry *h,
11301 Elf_Internal_Sym *sym)
b49e97c9
TS
11302{
11303 /* ??? Do mips16 stub sections need to be handled special? */
11304
11305 if (h != NULL)
07adf181
AM
11306 switch (ELF_R_TYPE (sec->owner, rel->r_info))
11307 {
11308 case R_MIPS_GNU_VTINHERIT:
11309 case R_MIPS_GNU_VTENTRY:
11310 return NULL;
11311 }
b49e97c9 11312
07adf181 11313 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
11314}
11315
11316/* Update the got entry reference counts for the section being removed. */
11317
b34976b6 11318bfd_boolean
9719ad41
RS
11319_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
11320 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11321 asection *sec ATTRIBUTE_UNUSED,
11322 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
11323{
11324#if 0
11325 Elf_Internal_Shdr *symtab_hdr;
11326 struct elf_link_hash_entry **sym_hashes;
11327 bfd_signed_vma *local_got_refcounts;
11328 const Elf_Internal_Rela *rel, *relend;
11329 unsigned long r_symndx;
11330 struct elf_link_hash_entry *h;
11331
7dda2462
TG
11332 if (info->relocatable)
11333 return TRUE;
11334
b49e97c9
TS
11335 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11336 sym_hashes = elf_sym_hashes (abfd);
11337 local_got_refcounts = elf_local_got_refcounts (abfd);
11338
11339 relend = relocs + sec->reloc_count;
11340 for (rel = relocs; rel < relend; rel++)
11341 switch (ELF_R_TYPE (abfd, rel->r_info))
11342 {
738e5348
RS
11343 case R_MIPS16_GOT16:
11344 case R_MIPS16_CALL16:
b49e97c9
TS
11345 case R_MIPS_GOT16:
11346 case R_MIPS_CALL16:
11347 case R_MIPS_CALL_HI16:
11348 case R_MIPS_CALL_LO16:
11349 case R_MIPS_GOT_HI16:
11350 case R_MIPS_GOT_LO16:
4a14403c
TS
11351 case R_MIPS_GOT_DISP:
11352 case R_MIPS_GOT_PAGE:
11353 case R_MIPS_GOT_OFST:
df58fc94
RS
11354 case R_MICROMIPS_GOT16:
11355 case R_MICROMIPS_CALL16:
11356 case R_MICROMIPS_CALL_HI16:
11357 case R_MICROMIPS_CALL_LO16:
11358 case R_MICROMIPS_GOT_HI16:
11359 case R_MICROMIPS_GOT_LO16:
11360 case R_MICROMIPS_GOT_DISP:
11361 case R_MICROMIPS_GOT_PAGE:
11362 case R_MICROMIPS_GOT_OFST:
b49e97c9
TS
11363 /* ??? It would seem that the existing MIPS code does no sort
11364 of reference counting or whatnot on its GOT and PLT entries,
11365 so it is not possible to garbage collect them at this time. */
11366 break;
11367
11368 default:
11369 break;
11370 }
11371#endif
11372
b34976b6 11373 return TRUE;
b49e97c9
TS
11374}
11375\f
11376/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11377 hiding the old indirect symbol. Process additional relocation
11378 information. Also called for weakdefs, in which case we just let
11379 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11380
11381void
fcfa13d2 11382_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
11383 struct elf_link_hash_entry *dir,
11384 struct elf_link_hash_entry *ind)
b49e97c9
TS
11385{
11386 struct mips_elf_link_hash_entry *dirmips, *indmips;
11387
fcfa13d2 11388 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 11389
861fb55a
DJ
11390 dirmips = (struct mips_elf_link_hash_entry *) dir;
11391 indmips = (struct mips_elf_link_hash_entry *) ind;
11392 /* Any absolute non-dynamic relocations against an indirect or weak
11393 definition will be against the target symbol. */
11394 if (indmips->has_static_relocs)
11395 dirmips->has_static_relocs = TRUE;
11396
b49e97c9
TS
11397 if (ind->root.type != bfd_link_hash_indirect)
11398 return;
11399
b49e97c9
TS
11400 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11401 if (indmips->readonly_reloc)
b34976b6 11402 dirmips->readonly_reloc = TRUE;
b49e97c9 11403 if (indmips->no_fn_stub)
b34976b6 11404 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
11405 if (indmips->fn_stub)
11406 {
11407 dirmips->fn_stub = indmips->fn_stub;
11408 indmips->fn_stub = NULL;
11409 }
11410 if (indmips->need_fn_stub)
11411 {
11412 dirmips->need_fn_stub = TRUE;
11413 indmips->need_fn_stub = FALSE;
11414 }
11415 if (indmips->call_stub)
11416 {
11417 dirmips->call_stub = indmips->call_stub;
11418 indmips->call_stub = NULL;
11419 }
11420 if (indmips->call_fp_stub)
11421 {
11422 dirmips->call_fp_stub = indmips->call_fp_stub;
11423 indmips->call_fp_stub = NULL;
11424 }
634835ae
RS
11425 if (indmips->global_got_area < dirmips->global_got_area)
11426 dirmips->global_got_area = indmips->global_got_area;
11427 if (indmips->global_got_area < GGA_NONE)
11428 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
11429 if (indmips->has_nonpic_branches)
11430 dirmips->has_nonpic_branches = TRUE;
0f20cc35
DJ
11431
11432 if (dirmips->tls_type == 0)
11433 dirmips->tls_type = indmips->tls_type;
b49e97c9 11434}
b49e97c9 11435\f
d01414a5
TS
11436#define PDR_SIZE 32
11437
b34976b6 11438bfd_boolean
9719ad41
RS
11439_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11440 struct bfd_link_info *info)
d01414a5
TS
11441{
11442 asection *o;
b34976b6 11443 bfd_boolean ret = FALSE;
d01414a5
TS
11444 unsigned char *tdata;
11445 size_t i, skip;
11446
11447 o = bfd_get_section_by_name (abfd, ".pdr");
11448 if (! o)
b34976b6 11449 return FALSE;
eea6121a 11450 if (o->size == 0)
b34976b6 11451 return FALSE;
eea6121a 11452 if (o->size % PDR_SIZE != 0)
b34976b6 11453 return FALSE;
d01414a5
TS
11454 if (o->output_section != NULL
11455 && bfd_is_abs_section (o->output_section))
b34976b6 11456 return FALSE;
d01414a5 11457
eea6121a 11458 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 11459 if (! tdata)
b34976b6 11460 return FALSE;
d01414a5 11461
9719ad41 11462 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 11463 info->keep_memory);
d01414a5
TS
11464 if (!cookie->rels)
11465 {
11466 free (tdata);
b34976b6 11467 return FALSE;
d01414a5
TS
11468 }
11469
11470 cookie->rel = cookie->rels;
11471 cookie->relend = cookie->rels + o->reloc_count;
11472
eea6121a 11473 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 11474 {
c152c796 11475 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
11476 {
11477 tdata[i] = 1;
11478 skip ++;
11479 }
11480 }
11481
11482 if (skip != 0)
11483 {
f0abc2a1 11484 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 11485 o->size -= skip * PDR_SIZE;
b34976b6 11486 ret = TRUE;
d01414a5
TS
11487 }
11488 else
11489 free (tdata);
11490
11491 if (! info->keep_memory)
11492 free (cookie->rels);
11493
11494 return ret;
11495}
11496
b34976b6 11497bfd_boolean
9719ad41 11498_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
11499{
11500 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
11501 return TRUE;
11502 return FALSE;
53bfd6b4 11503}
d01414a5 11504
b34976b6 11505bfd_boolean
c7b8f16e
JB
11506_bfd_mips_elf_write_section (bfd *output_bfd,
11507 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11508 asection *sec, bfd_byte *contents)
d01414a5
TS
11509{
11510 bfd_byte *to, *from, *end;
11511 int i;
11512
11513 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 11514 return FALSE;
d01414a5 11515
f0abc2a1 11516 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 11517 return FALSE;
d01414a5
TS
11518
11519 to = contents;
eea6121a 11520 end = contents + sec->size;
d01414a5
TS
11521 for (from = contents, i = 0;
11522 from < end;
11523 from += PDR_SIZE, i++)
11524 {
f0abc2a1 11525 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
11526 continue;
11527 if (to != from)
11528 memcpy (to, from, PDR_SIZE);
11529 to += PDR_SIZE;
11530 }
11531 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 11532 sec->output_offset, sec->size);
b34976b6 11533 return TRUE;
d01414a5 11534}
53bfd6b4 11535\f
df58fc94
RS
11536/* microMIPS code retains local labels for linker relaxation. Omit them
11537 from output by default for clarity. */
11538
11539bfd_boolean
11540_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
11541{
11542 return _bfd_elf_is_local_label_name (abfd, sym->name);
11543}
11544
b49e97c9
TS
11545/* MIPS ELF uses a special find_nearest_line routine in order the
11546 handle the ECOFF debugging information. */
11547
11548struct mips_elf_find_line
11549{
11550 struct ecoff_debug_info d;
11551 struct ecoff_find_line i;
11552};
11553
b34976b6 11554bfd_boolean
9719ad41
RS
11555_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11556 asymbol **symbols, bfd_vma offset,
11557 const char **filename_ptr,
11558 const char **functionname_ptr,
11559 unsigned int *line_ptr)
b49e97c9
TS
11560{
11561 asection *msec;
11562
11563 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11564 filename_ptr, functionname_ptr,
11565 line_ptr))
b34976b6 11566 return TRUE;
b49e97c9 11567
fc28f9aa
TG
11568 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
11569 section, symbols, offset,
b49e97c9 11570 filename_ptr, functionname_ptr,
9719ad41 11571 line_ptr, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 11572 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 11573 return TRUE;
b49e97c9
TS
11574
11575 msec = bfd_get_section_by_name (abfd, ".mdebug");
11576 if (msec != NULL)
11577 {
11578 flagword origflags;
11579 struct mips_elf_find_line *fi;
11580 const struct ecoff_debug_swap * const swap =
11581 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11582
11583 /* If we are called during a link, mips_elf_final_link may have
11584 cleared the SEC_HAS_CONTENTS field. We force it back on here
11585 if appropriate (which it normally will be). */
11586 origflags = msec->flags;
11587 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11588 msec->flags |= SEC_HAS_CONTENTS;
11589
11590 fi = elf_tdata (abfd)->find_line_info;
11591 if (fi == NULL)
11592 {
11593 bfd_size_type external_fdr_size;
11594 char *fraw_src;
11595 char *fraw_end;
11596 struct fdr *fdr_ptr;
11597 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11598
9719ad41 11599 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
11600 if (fi == NULL)
11601 {
11602 msec->flags = origflags;
b34976b6 11603 return FALSE;
b49e97c9
TS
11604 }
11605
11606 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11607 {
11608 msec->flags = origflags;
b34976b6 11609 return FALSE;
b49e97c9
TS
11610 }
11611
11612 /* Swap in the FDR information. */
11613 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 11614 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
11615 if (fi->d.fdr == NULL)
11616 {
11617 msec->flags = origflags;
b34976b6 11618 return FALSE;
b49e97c9
TS
11619 }
11620 external_fdr_size = swap->external_fdr_size;
11621 fdr_ptr = fi->d.fdr;
11622 fraw_src = (char *) fi->d.external_fdr;
11623 fraw_end = (fraw_src
11624 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11625 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 11626 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9
TS
11627
11628 elf_tdata (abfd)->find_line_info = fi;
11629
11630 /* Note that we don't bother to ever free this information.
11631 find_nearest_line is either called all the time, as in
11632 objdump -l, so the information should be saved, or it is
11633 rarely called, as in ld error messages, so the memory
11634 wasted is unimportant. Still, it would probably be a
11635 good idea for free_cached_info to throw it away. */
11636 }
11637
11638 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11639 &fi->i, filename_ptr, functionname_ptr,
11640 line_ptr))
11641 {
11642 msec->flags = origflags;
b34976b6 11643 return TRUE;
b49e97c9
TS
11644 }
11645
11646 msec->flags = origflags;
11647 }
11648
11649 /* Fall back on the generic ELF find_nearest_line routine. */
11650
11651 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11652 filename_ptr, functionname_ptr,
11653 line_ptr);
11654}
4ab527b0
FF
11655
11656bfd_boolean
11657_bfd_mips_elf_find_inliner_info (bfd *abfd,
11658 const char **filename_ptr,
11659 const char **functionname_ptr,
11660 unsigned int *line_ptr)
11661{
11662 bfd_boolean found;
11663 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11664 functionname_ptr, line_ptr,
11665 & elf_tdata (abfd)->dwarf2_find_line_info);
11666 return found;
11667}
11668
b49e97c9
TS
11669\f
11670/* When are writing out the .options or .MIPS.options section,
11671 remember the bytes we are writing out, so that we can install the
11672 GP value in the section_processing routine. */
11673
b34976b6 11674bfd_boolean
9719ad41
RS
11675_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11676 const void *location,
11677 file_ptr offset, bfd_size_type count)
b49e97c9 11678{
cc2e31b9 11679 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
11680 {
11681 bfd_byte *c;
11682
11683 if (elf_section_data (section) == NULL)
11684 {
11685 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 11686 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 11687 if (elf_section_data (section) == NULL)
b34976b6 11688 return FALSE;
b49e97c9 11689 }
f0abc2a1 11690 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
11691 if (c == NULL)
11692 {
eea6121a 11693 c = bfd_zalloc (abfd, section->size);
b49e97c9 11694 if (c == NULL)
b34976b6 11695 return FALSE;
f0abc2a1 11696 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
11697 }
11698
9719ad41 11699 memcpy (c + offset, location, count);
b49e97c9
TS
11700 }
11701
11702 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11703 count);
11704}
11705
11706/* This is almost identical to bfd_generic_get_... except that some
11707 MIPS relocations need to be handled specially. Sigh. */
11708
11709bfd_byte *
9719ad41
RS
11710_bfd_elf_mips_get_relocated_section_contents
11711 (bfd *abfd,
11712 struct bfd_link_info *link_info,
11713 struct bfd_link_order *link_order,
11714 bfd_byte *data,
11715 bfd_boolean relocatable,
11716 asymbol **symbols)
b49e97c9
TS
11717{
11718 /* Get enough memory to hold the stuff */
11719 bfd *input_bfd = link_order->u.indirect.section->owner;
11720 asection *input_section = link_order->u.indirect.section;
eea6121a 11721 bfd_size_type sz;
b49e97c9
TS
11722
11723 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11724 arelent **reloc_vector = NULL;
11725 long reloc_count;
11726
11727 if (reloc_size < 0)
11728 goto error_return;
11729
9719ad41 11730 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
11731 if (reloc_vector == NULL && reloc_size != 0)
11732 goto error_return;
11733
11734 /* read in the section */
eea6121a
AM
11735 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11736 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
11737 goto error_return;
11738
b49e97c9
TS
11739 reloc_count = bfd_canonicalize_reloc (input_bfd,
11740 input_section,
11741 reloc_vector,
11742 symbols);
11743 if (reloc_count < 0)
11744 goto error_return;
11745
11746 if (reloc_count > 0)
11747 {
11748 arelent **parent;
11749 /* for mips */
11750 int gp_found;
11751 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11752
11753 {
11754 struct bfd_hash_entry *h;
11755 struct bfd_link_hash_entry *lh;
11756 /* Skip all this stuff if we aren't mixing formats. */
11757 if (abfd && input_bfd
11758 && abfd->xvec == input_bfd->xvec)
11759 lh = 0;
11760 else
11761 {
b34976b6 11762 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
11763 lh = (struct bfd_link_hash_entry *) h;
11764 }
11765 lookup:
11766 if (lh)
11767 {
11768 switch (lh->type)
11769 {
11770 case bfd_link_hash_undefined:
11771 case bfd_link_hash_undefweak:
11772 case bfd_link_hash_common:
11773 gp_found = 0;
11774 break;
11775 case bfd_link_hash_defined:
11776 case bfd_link_hash_defweak:
11777 gp_found = 1;
11778 gp = lh->u.def.value;
11779 break;
11780 case bfd_link_hash_indirect:
11781 case bfd_link_hash_warning:
11782 lh = lh->u.i.link;
11783 /* @@FIXME ignoring warning for now */
11784 goto lookup;
11785 case bfd_link_hash_new:
11786 default:
11787 abort ();
11788 }
11789 }
11790 else
11791 gp_found = 0;
11792 }
11793 /* end mips */
9719ad41 11794 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 11795 {
9719ad41 11796 char *error_message = NULL;
b49e97c9
TS
11797 bfd_reloc_status_type r;
11798
11799 /* Specific to MIPS: Deal with relocation types that require
11800 knowing the gp of the output bfd. */
11801 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 11802
8236346f
EC
11803 /* If we've managed to find the gp and have a special
11804 function for the relocation then go ahead, else default
11805 to the generic handling. */
11806 if (gp_found
11807 && (*parent)->howto->special_function
11808 == _bfd_mips_elf32_gprel16_reloc)
11809 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11810 input_section, relocatable,
11811 data, gp);
11812 else
86324f90 11813 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
11814 input_section,
11815 relocatable ? abfd : NULL,
11816 &error_message);
b49e97c9 11817
1049f94e 11818 if (relocatable)
b49e97c9
TS
11819 {
11820 asection *os = input_section->output_section;
11821
11822 /* A partial link, so keep the relocs */
11823 os->orelocation[os->reloc_count] = *parent;
11824 os->reloc_count++;
11825 }
11826
11827 if (r != bfd_reloc_ok)
11828 {
11829 switch (r)
11830 {
11831 case bfd_reloc_undefined:
11832 if (!((*link_info->callbacks->undefined_symbol)
11833 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 11834 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
11835 goto error_return;
11836 break;
11837 case bfd_reloc_dangerous:
9719ad41 11838 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
11839 if (!((*link_info->callbacks->reloc_dangerous)
11840 (link_info, error_message, input_bfd, input_section,
11841 (*parent)->address)))
11842 goto error_return;
11843 break;
11844 case bfd_reloc_overflow:
11845 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
11846 (link_info, NULL,
11847 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
11848 (*parent)->howto->name, (*parent)->addend,
11849 input_bfd, input_section, (*parent)->address)))
11850 goto error_return;
11851 break;
11852 case bfd_reloc_outofrange:
11853 default:
11854 abort ();
11855 break;
11856 }
11857
11858 }
11859 }
11860 }
11861 if (reloc_vector != NULL)
11862 free (reloc_vector);
11863 return data;
11864
11865error_return:
11866 if (reloc_vector != NULL)
11867 free (reloc_vector);
11868 return NULL;
11869}
11870\f
df58fc94
RS
11871static bfd_boolean
11872mips_elf_relax_delete_bytes (bfd *abfd,
11873 asection *sec, bfd_vma addr, int count)
11874{
11875 Elf_Internal_Shdr *symtab_hdr;
11876 unsigned int sec_shndx;
11877 bfd_byte *contents;
11878 Elf_Internal_Rela *irel, *irelend;
11879 Elf_Internal_Sym *isym;
11880 Elf_Internal_Sym *isymend;
11881 struct elf_link_hash_entry **sym_hashes;
11882 struct elf_link_hash_entry **end_hashes;
11883 struct elf_link_hash_entry **start_hashes;
11884 unsigned int symcount;
11885
11886 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
11887 contents = elf_section_data (sec)->this_hdr.contents;
11888
11889 irel = elf_section_data (sec)->relocs;
11890 irelend = irel + sec->reloc_count;
11891
11892 /* Actually delete the bytes. */
11893 memmove (contents + addr, contents + addr + count,
11894 (size_t) (sec->size - addr - count));
11895 sec->size -= count;
11896
11897 /* Adjust all the relocs. */
11898 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
11899 {
11900 /* Get the new reloc address. */
11901 if (irel->r_offset > addr)
11902 irel->r_offset -= count;
11903 }
11904
11905 BFD_ASSERT (addr % 2 == 0);
11906 BFD_ASSERT (count % 2 == 0);
11907
11908 /* Adjust the local symbols defined in this section. */
11909 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11910 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
11911 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2309ddf2 11912 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
df58fc94
RS
11913 isym->st_value -= count;
11914
11915 /* Now adjust the global symbols defined in this section. */
11916 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
11917 - symtab_hdr->sh_info);
11918 sym_hashes = start_hashes = elf_sym_hashes (abfd);
11919 end_hashes = sym_hashes + symcount;
11920
11921 for (; sym_hashes < end_hashes; sym_hashes++)
11922 {
11923 struct elf_link_hash_entry *sym_hash = *sym_hashes;
11924
11925 if ((sym_hash->root.type == bfd_link_hash_defined
11926 || sym_hash->root.type == bfd_link_hash_defweak)
11927 && sym_hash->root.u.def.section == sec)
11928 {
2309ddf2 11929 bfd_vma value = sym_hash->root.u.def.value;
df58fc94 11930
df58fc94
RS
11931 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
11932 value &= MINUS_TWO;
11933 if (value > addr)
11934 sym_hash->root.u.def.value -= count;
11935 }
11936 }
11937
11938 return TRUE;
11939}
11940
11941
11942/* Opcodes needed for microMIPS relaxation as found in
11943 opcodes/micromips-opc.c. */
11944
11945struct opcode_descriptor {
11946 unsigned long match;
11947 unsigned long mask;
11948};
11949
11950/* The $ra register aka $31. */
11951
11952#define RA 31
11953
11954/* 32-bit instruction format register fields. */
11955
11956#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
11957#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
11958
11959/* Check if a 5-bit register index can be abbreviated to 3 bits. */
11960
11961#define OP16_VALID_REG(r) \
11962 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
11963
11964
11965/* 32-bit and 16-bit branches. */
11966
11967static const struct opcode_descriptor b_insns_32[] = {
11968 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
11969 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
11970 { 0, 0 } /* End marker for find_match(). */
11971};
11972
11973static const struct opcode_descriptor bc_insn_32 =
11974 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
11975
11976static const struct opcode_descriptor bz_insn_32 =
11977 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
11978
11979static const struct opcode_descriptor bzal_insn_32 =
11980 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
11981
11982static const struct opcode_descriptor beq_insn_32 =
11983 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
11984
11985static const struct opcode_descriptor b_insn_16 =
11986 { /* "b", "mD", */ 0xcc00, 0xfc00 };
11987
11988static const struct opcode_descriptor bz_insn_16 =
c088dedf 11989 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
df58fc94
RS
11990
11991
11992/* 32-bit and 16-bit branch EQ and NE zero. */
11993
11994/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
11995 eq and second the ne. This convention is used when replacing a
11996 32-bit BEQ/BNE with the 16-bit version. */
11997
11998#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
11999
12000static const struct opcode_descriptor bz_rs_insns_32[] = {
12001 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12002 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12003 { 0, 0 } /* End marker for find_match(). */
12004};
12005
12006static const struct opcode_descriptor bz_rt_insns_32[] = {
12007 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12008 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12009 { 0, 0 } /* End marker for find_match(). */
12010};
12011
12012static const struct opcode_descriptor bzc_insns_32[] = {
12013 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12014 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12015 { 0, 0 } /* End marker for find_match(). */
12016};
12017
12018static const struct opcode_descriptor bz_insns_16[] = {
12019 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12020 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12021 { 0, 0 } /* End marker for find_match(). */
12022};
12023
12024/* Switch between a 5-bit register index and its 3-bit shorthand. */
12025
12026#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12027#define BZ16_REG_FIELD(r) \
12028 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12029
12030
12031/* 32-bit instructions with a delay slot. */
12032
12033static const struct opcode_descriptor jal_insn_32_bd16 =
12034 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12035
12036static const struct opcode_descriptor jal_insn_32_bd32 =
12037 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12038
12039static const struct opcode_descriptor jal_x_insn_32_bd32 =
12040 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12041
12042static const struct opcode_descriptor j_insn_32 =
12043 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12044
12045static const struct opcode_descriptor jalr_insn_32 =
12046 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12047
12048/* This table can be compacted, because no opcode replacement is made. */
12049
12050static const struct opcode_descriptor ds_insns_32_bd16[] = {
12051 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12052
12053 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12054 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12055
12056 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12057 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12058 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12059 { 0, 0 } /* End marker for find_match(). */
12060};
12061
12062/* This table can be compacted, because no opcode replacement is made. */
12063
12064static const struct opcode_descriptor ds_insns_32_bd32[] = {
12065 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12066
12067 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12068 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12069 { 0, 0 } /* End marker for find_match(). */
12070};
12071
12072
12073/* 16-bit instructions with a delay slot. */
12074
12075static const struct opcode_descriptor jalr_insn_16_bd16 =
12076 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12077
12078static const struct opcode_descriptor jalr_insn_16_bd32 =
12079 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12080
12081static const struct opcode_descriptor jr_insn_16 =
12082 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12083
12084#define JR16_REG(opcode) ((opcode) & 0x1f)
12085
12086/* This table can be compacted, because no opcode replacement is made. */
12087
12088static const struct opcode_descriptor ds_insns_16_bd16[] = {
12089 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12090
12091 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12092 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12093 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12094 { 0, 0 } /* End marker for find_match(). */
12095};
12096
12097
12098/* LUI instruction. */
12099
12100static const struct opcode_descriptor lui_insn =
12101 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12102
12103
12104/* ADDIU instruction. */
12105
12106static const struct opcode_descriptor addiu_insn =
12107 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12108
12109static const struct opcode_descriptor addiupc_insn =
12110 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12111
12112#define ADDIUPC_REG_FIELD(r) \
12113 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12114
12115
12116/* Relaxable instructions in a JAL delay slot: MOVE. */
12117
12118/* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12119 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12120#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12121#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12122
12123#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12124#define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12125
12126static const struct opcode_descriptor move_insns_32[] = {
12127 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12128 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12129 { 0, 0 } /* End marker for find_match(). */
12130};
12131
12132static const struct opcode_descriptor move_insn_16 =
12133 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12134
12135
12136/* NOP instructions. */
12137
12138static const struct opcode_descriptor nop_insn_32 =
12139 { /* "nop", "", */ 0x00000000, 0xffffffff };
12140
12141static const struct opcode_descriptor nop_insn_16 =
12142 { /* "nop", "", */ 0x0c00, 0xffff };
12143
12144
12145/* Instruction match support. */
12146
12147#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12148
12149static int
12150find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12151{
12152 unsigned long indx;
12153
12154 for (indx = 0; insn[indx].mask != 0; indx++)
12155 if (MATCH (opcode, insn[indx]))
12156 return indx;
12157
12158 return -1;
12159}
12160
12161
12162/* Branch and delay slot decoding support. */
12163
12164/* If PTR points to what *might* be a 16-bit branch or jump, then
12165 return the minimum length of its delay slot, otherwise return 0.
12166 Non-zero results are not definitive as we might be checking against
12167 the second half of another instruction. */
12168
12169static int
12170check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12171{
12172 unsigned long opcode;
12173 int bdsize;
12174
12175 opcode = bfd_get_16 (abfd, ptr);
12176 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12177 /* 16-bit branch/jump with a 32-bit delay slot. */
12178 bdsize = 4;
12179 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12180 || find_match (opcode, ds_insns_16_bd16) >= 0)
12181 /* 16-bit branch/jump with a 16-bit delay slot. */
12182 bdsize = 2;
12183 else
12184 /* No delay slot. */
12185 bdsize = 0;
12186
12187 return bdsize;
12188}
12189
12190/* If PTR points to what *might* be a 32-bit branch or jump, then
12191 return the minimum length of its delay slot, otherwise return 0.
12192 Non-zero results are not definitive as we might be checking against
12193 the second half of another instruction. */
12194
12195static int
12196check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12197{
12198 unsigned long opcode;
12199 int bdsize;
12200
12201 opcode = (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
12202 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12203 /* 32-bit branch/jump with a 32-bit delay slot. */
12204 bdsize = 4;
12205 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12206 /* 32-bit branch/jump with a 16-bit delay slot. */
12207 bdsize = 2;
12208 else
12209 /* No delay slot. */
12210 bdsize = 0;
12211
12212 return bdsize;
12213}
12214
12215/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12216 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12217
12218static bfd_boolean
12219check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12220{
12221 unsigned long opcode;
12222
12223 opcode = bfd_get_16 (abfd, ptr);
12224 if (MATCH (opcode, b_insn_16)
12225 /* B16 */
12226 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12227 /* JR16 */
12228 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12229 /* BEQZ16, BNEZ16 */
12230 || (MATCH (opcode, jalr_insn_16_bd32)
12231 /* JALR16 */
12232 && reg != JR16_REG (opcode) && reg != RA))
12233 return TRUE;
12234
12235 return FALSE;
12236}
12237
12238/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12239 then return TRUE, otherwise FALSE. */
12240
f41e5fcc 12241static bfd_boolean
df58fc94
RS
12242check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12243{
12244 unsigned long opcode;
12245
12246 opcode = (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
12247 if (MATCH (opcode, j_insn_32)
12248 /* J */
12249 || MATCH (opcode, bc_insn_32)
12250 /* BC1F, BC1T, BC2F, BC2T */
12251 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
12252 /* JAL, JALX */
12253 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
12254 /* BGEZ, BGTZ, BLEZ, BLTZ */
12255 || (MATCH (opcode, bzal_insn_32)
12256 /* BGEZAL, BLTZAL */
12257 && reg != OP32_SREG (opcode) && reg != RA)
12258 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
12259 /* JALR, JALR.HB, BEQ, BNE */
12260 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
12261 return TRUE;
12262
12263 return FALSE;
12264}
12265
80cab405
MR
12266/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
12267 IRELEND) at OFFSET indicate that there must be a compact branch there,
12268 then return TRUE, otherwise FALSE. */
df58fc94
RS
12269
12270static bfd_boolean
80cab405
MR
12271check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
12272 const Elf_Internal_Rela *internal_relocs,
12273 const Elf_Internal_Rela *irelend)
df58fc94 12274{
80cab405
MR
12275 const Elf_Internal_Rela *irel;
12276 unsigned long opcode;
12277
12278 opcode = bfd_get_16 (abfd, ptr);
12279 opcode <<= 16;
12280 opcode |= bfd_get_16 (abfd, ptr + 2);
12281 if (find_match (opcode, bzc_insns_32) < 0)
12282 return FALSE;
df58fc94
RS
12283
12284 for (irel = internal_relocs; irel < irelend; irel++)
80cab405
MR
12285 if (irel->r_offset == offset
12286 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
12287 return TRUE;
12288
df58fc94
RS
12289 return FALSE;
12290}
80cab405
MR
12291
12292/* Bitsize checking. */
12293#define IS_BITSIZE(val, N) \
12294 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
12295 - (1ULL << ((N) - 1))) == (val))
12296
df58fc94
RS
12297\f
12298bfd_boolean
12299_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
12300 struct bfd_link_info *link_info,
12301 bfd_boolean *again)
12302{
12303 Elf_Internal_Shdr *symtab_hdr;
12304 Elf_Internal_Rela *internal_relocs;
12305 Elf_Internal_Rela *irel, *irelend;
12306 bfd_byte *contents = NULL;
12307 Elf_Internal_Sym *isymbuf = NULL;
12308
12309 /* Assume nothing changes. */
12310 *again = FALSE;
12311
12312 /* We don't have to do anything for a relocatable link, if
12313 this section does not have relocs, or if this is not a
12314 code section. */
12315
12316 if (link_info->relocatable
12317 || (sec->flags & SEC_RELOC) == 0
12318 || sec->reloc_count == 0
12319 || (sec->flags & SEC_CODE) == 0)
12320 return TRUE;
12321
12322 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12323
12324 /* Get a copy of the native relocations. */
12325 internal_relocs = (_bfd_elf_link_read_relocs
12326 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
12327 link_info->keep_memory));
12328 if (internal_relocs == NULL)
12329 goto error_return;
12330
12331 /* Walk through them looking for relaxing opportunities. */
12332 irelend = internal_relocs + sec->reloc_count;
12333 for (irel = internal_relocs; irel < irelend; irel++)
12334 {
12335 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
12336 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
12337 bfd_boolean target_is_micromips_code_p;
12338 unsigned long opcode;
12339 bfd_vma symval;
12340 bfd_vma pcrval;
2309ddf2 12341 bfd_byte *ptr;
df58fc94
RS
12342 int fndopc;
12343
12344 /* The number of bytes to delete for relaxation and from where
12345 to delete these bytes starting at irel->r_offset. */
12346 int delcnt = 0;
12347 int deloff = 0;
12348
12349 /* If this isn't something that can be relaxed, then ignore
12350 this reloc. */
12351 if (r_type != R_MICROMIPS_HI16
12352 && r_type != R_MICROMIPS_PC16_S1
2309ddf2 12353 && r_type != R_MICROMIPS_26_S1)
df58fc94
RS
12354 continue;
12355
12356 /* Get the section contents if we haven't done so already. */
12357 if (contents == NULL)
12358 {
12359 /* Get cached copy if it exists. */
12360 if (elf_section_data (sec)->this_hdr.contents != NULL)
12361 contents = elf_section_data (sec)->this_hdr.contents;
12362 /* Go get them off disk. */
12363 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
12364 goto error_return;
12365 }
2309ddf2 12366 ptr = contents + irel->r_offset;
df58fc94
RS
12367
12368 /* Read this BFD's local symbols if we haven't done so already. */
12369 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
12370 {
12371 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
12372 if (isymbuf == NULL)
12373 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12374 symtab_hdr->sh_info, 0,
12375 NULL, NULL, NULL);
12376 if (isymbuf == NULL)
12377 goto error_return;
12378 }
12379
12380 /* Get the value of the symbol referred to by the reloc. */
12381 if (r_symndx < symtab_hdr->sh_info)
12382 {
12383 /* A local symbol. */
12384 Elf_Internal_Sym *isym;
12385 asection *sym_sec;
12386
12387 isym = isymbuf + r_symndx;
12388 if (isym->st_shndx == SHN_UNDEF)
12389 sym_sec = bfd_und_section_ptr;
12390 else if (isym->st_shndx == SHN_ABS)
12391 sym_sec = bfd_abs_section_ptr;
12392 else if (isym->st_shndx == SHN_COMMON)
12393 sym_sec = bfd_com_section_ptr;
12394 else
12395 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
12396 symval = (isym->st_value
12397 + sym_sec->output_section->vma
12398 + sym_sec->output_offset);
12399 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
12400 }
12401 else
12402 {
12403 unsigned long indx;
12404 struct elf_link_hash_entry *h;
12405
12406 /* An external symbol. */
12407 indx = r_symndx - symtab_hdr->sh_info;
12408 h = elf_sym_hashes (abfd)[indx];
12409 BFD_ASSERT (h != NULL);
12410
12411 if (h->root.type != bfd_link_hash_defined
12412 && h->root.type != bfd_link_hash_defweak)
12413 /* This appears to be a reference to an undefined
12414 symbol. Just ignore it -- it will be caught by the
12415 regular reloc processing. */
12416 continue;
12417
12418 symval = (h->root.u.def.value
12419 + h->root.u.def.section->output_section->vma
12420 + h->root.u.def.section->output_offset);
12421 target_is_micromips_code_p = (!h->needs_plt
12422 && ELF_ST_IS_MICROMIPS (h->other));
12423 }
12424
12425
12426 /* For simplicity of coding, we are going to modify the
12427 section contents, the section relocs, and the BFD symbol
12428 table. We must tell the rest of the code not to free up this
12429 information. It would be possible to instead create a table
12430 of changes which have to be made, as is done in coff-mips.c;
12431 that would be more work, but would require less memory when
12432 the linker is run. */
12433
12434 /* Only 32-bit instructions relaxed. */
12435 if (irel->r_offset + 4 > sec->size)
12436 continue;
12437
2309ddf2
MR
12438 opcode = bfd_get_16 (abfd, ptr ) << 16;
12439 opcode |= bfd_get_16 (abfd, ptr + 2);
df58fc94
RS
12440
12441 /* This is the pc-relative distance from the instruction the
12442 relocation is applied to, to the symbol referred. */
12443 pcrval = (symval
12444 - (sec->output_section->vma + sec->output_offset)
12445 - irel->r_offset);
12446
12447 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
12448 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
12449 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
12450
12451 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
12452
12453 where pcrval has first to be adjusted to apply against the LO16
12454 location (we make the adjustment later on, when we have figured
12455 out the offset). */
12456 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
12457 {
80cab405 12458 bfd_boolean bzc = FALSE;
df58fc94
RS
12459 unsigned long nextopc;
12460 unsigned long reg;
12461 bfd_vma offset;
12462
12463 /* Give up if the previous reloc was a HI16 against this symbol
12464 too. */
12465 if (irel > internal_relocs
12466 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
12467 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
12468 continue;
12469
12470 /* Or if the next reloc is not a LO16 against this symbol. */
12471 if (irel + 1 >= irelend
12472 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
12473 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
12474 continue;
12475
12476 /* Or if the second next reloc is a LO16 against this symbol too. */
12477 if (irel + 2 >= irelend
12478 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
12479 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
12480 continue;
12481
80cab405
MR
12482 /* See if the LUI instruction *might* be in a branch delay slot.
12483 We check whether what looks like a 16-bit branch or jump is
12484 actually an immediate argument to a compact branch, and let
12485 it through if so. */
df58fc94 12486 if (irel->r_offset >= 2
2309ddf2 12487 && check_br16_dslot (abfd, ptr - 2)
df58fc94 12488 && !(irel->r_offset >= 4
80cab405
MR
12489 && (bzc = check_relocated_bzc (abfd,
12490 ptr - 4, irel->r_offset - 4,
12491 internal_relocs, irelend))))
df58fc94
RS
12492 continue;
12493 if (irel->r_offset >= 4
80cab405 12494 && !bzc
2309ddf2 12495 && check_br32_dslot (abfd, ptr - 4))
df58fc94
RS
12496 continue;
12497
12498 reg = OP32_SREG (opcode);
12499
12500 /* We only relax adjacent instructions or ones separated with
12501 a branch or jump that has a delay slot. The branch or jump
12502 must not fiddle with the register used to hold the address.
12503 Subtract 4 for the LUI itself. */
12504 offset = irel[1].r_offset - irel[0].r_offset;
12505 switch (offset - 4)
12506 {
12507 case 0:
12508 break;
12509 case 2:
2309ddf2 12510 if (check_br16 (abfd, ptr + 4, reg))
df58fc94
RS
12511 break;
12512 continue;
12513 case 4:
2309ddf2 12514 if (check_br32 (abfd, ptr + 4, reg))
df58fc94
RS
12515 break;
12516 continue;
12517 default:
12518 continue;
12519 }
12520
12521 nextopc = bfd_get_16 (abfd, contents + irel[1].r_offset ) << 16;
12522 nextopc |= bfd_get_16 (abfd, contents + irel[1].r_offset + 2);
12523
12524 /* Give up unless the same register is used with both
12525 relocations. */
12526 if (OP32_SREG (nextopc) != reg)
12527 continue;
12528
12529 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
12530 and rounding up to take masking of the two LSBs into account. */
12531 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
12532
12533 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
12534 if (IS_BITSIZE (symval, 16))
12535 {
12536 /* Fix the relocation's type. */
12537 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
12538
12539 /* Instructions using R_MICROMIPS_LO16 have the base or
12540 source register in bits 20:16. This register becomes $0
12541 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
12542 nextopc &= ~0x001f0000;
12543 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
12544 contents + irel[1].r_offset);
12545 }
12546
12547 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
12548 We add 4 to take LUI deletion into account while checking
12549 the PC-relative distance. */
12550 else if (symval % 4 == 0
12551 && IS_BITSIZE (pcrval + 4, 25)
12552 && MATCH (nextopc, addiu_insn)
12553 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
12554 && OP16_VALID_REG (OP32_TREG (nextopc)))
12555 {
12556 /* Fix the relocation's type. */
12557 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
12558
12559 /* Replace ADDIU with the ADDIUPC version. */
12560 nextopc = (addiupc_insn.match
12561 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
12562
12563 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
12564 contents + irel[1].r_offset);
12565 bfd_put_16 (abfd, nextopc & 0xffff,
12566 contents + irel[1].r_offset + 2);
12567 }
12568
12569 /* Can't do anything, give up, sigh... */
12570 else
12571 continue;
12572
12573 /* Fix the relocation's type. */
12574 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
12575
12576 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
12577 delcnt = 4;
12578 deloff = 0;
12579 }
12580
12581 /* Compact branch relaxation -- due to the multitude of macros
12582 employed by the compiler/assembler, compact branches are not
12583 always generated. Obviously, this can/will be fixed elsewhere,
12584 but there is no drawback in double checking it here. */
12585 else if (r_type == R_MICROMIPS_PC16_S1
12586 && irel->r_offset + 5 < sec->size
12587 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
12588 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
2309ddf2 12589 && MATCH (bfd_get_16 (abfd, ptr + 4), nop_insn_16))
df58fc94
RS
12590 {
12591 unsigned long reg;
12592
12593 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
12594
12595 /* Replace BEQZ/BNEZ with the compact version. */
12596 opcode = (bzc_insns_32[fndopc].match
12597 | BZC32_REG_FIELD (reg)
12598 | (opcode & 0xffff)); /* Addend value. */
12599
2309ddf2
MR
12600 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
12601 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
df58fc94
RS
12602
12603 /* Delete the 16-bit delay slot NOP: two bytes from
12604 irel->offset + 4. */
12605 delcnt = 2;
12606 deloff = 4;
12607 }
12608
12609 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
12610 to check the distance from the next instruction, so subtract 2. */
12611 else if (r_type == R_MICROMIPS_PC16_S1
12612 && IS_BITSIZE (pcrval - 2, 11)
12613 && find_match (opcode, b_insns_32) >= 0)
12614 {
12615 /* Fix the relocation's type. */
12616 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
12617
12618 /* Replace the the 32-bit opcode with a 16-bit opcode. */
12619 bfd_put_16 (abfd,
12620 (b_insn_16.match
12621 | (opcode & 0x3ff)), /* Addend value. */
2309ddf2 12622 ptr);
df58fc94
RS
12623
12624 /* Delete 2 bytes from irel->r_offset + 2. */
12625 delcnt = 2;
12626 deloff = 2;
12627 }
12628
12629 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
12630 to check the distance from the next instruction, so subtract 2. */
12631 else if (r_type == R_MICROMIPS_PC16_S1
12632 && IS_BITSIZE (pcrval - 2, 8)
12633 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
12634 && OP16_VALID_REG (OP32_SREG (opcode)))
12635 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
12636 && OP16_VALID_REG (OP32_TREG (opcode)))))
12637 {
12638 unsigned long reg;
12639
12640 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
12641
12642 /* Fix the relocation's type. */
12643 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
12644
12645 /* Replace the the 32-bit opcode with a 16-bit opcode. */
12646 bfd_put_16 (abfd,
12647 (bz_insns_16[fndopc].match
12648 | BZ16_REG_FIELD (reg)
12649 | (opcode & 0x7f)), /* Addend value. */
2309ddf2 12650 ptr);
df58fc94
RS
12651
12652 /* Delete 2 bytes from irel->r_offset + 2. */
12653 delcnt = 2;
12654 deloff = 2;
12655 }
12656
12657 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
12658 else if (r_type == R_MICROMIPS_26_S1
12659 && target_is_micromips_code_p
12660 && irel->r_offset + 7 < sec->size
12661 && MATCH (opcode, jal_insn_32_bd32))
12662 {
12663 unsigned long n32opc;
12664 bfd_boolean relaxed = FALSE;
12665
2309ddf2
MR
12666 n32opc = bfd_get_16 (abfd, ptr + 4) << 16;
12667 n32opc |= bfd_get_16 (abfd, ptr + 6);
df58fc94
RS
12668
12669 if (MATCH (n32opc, nop_insn_32))
12670 {
12671 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
2309ddf2 12672 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
df58fc94
RS
12673
12674 relaxed = TRUE;
12675 }
12676 else if (find_match (n32opc, move_insns_32) >= 0)
12677 {
12678 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
12679 bfd_put_16 (abfd,
12680 (move_insn_16.match
12681 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
12682 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
2309ddf2 12683 ptr + 4);
df58fc94
RS
12684
12685 relaxed = TRUE;
12686 }
12687 /* Other 32-bit instructions relaxable to 16-bit
12688 instructions will be handled here later. */
12689
12690 if (relaxed)
12691 {
12692 /* JAL with 32-bit delay slot that is changed to a JALS
12693 with 16-bit delay slot. */
12694 bfd_put_16 (abfd, (jal_insn_32_bd16.match >> 16) & 0xffff,
2309ddf2 12695 ptr);
df58fc94 12696 bfd_put_16 (abfd, jal_insn_32_bd16.match & 0xffff,
2309ddf2 12697 ptr + 2);
df58fc94
RS
12698
12699 /* Delete 2 bytes from irel->r_offset + 6. */
12700 delcnt = 2;
12701 deloff = 6;
12702 }
12703 }
12704
12705 if (delcnt != 0)
12706 {
12707 /* Note that we've changed the relocs, section contents, etc. */
12708 elf_section_data (sec)->relocs = internal_relocs;
12709 elf_section_data (sec)->this_hdr.contents = contents;
12710 symtab_hdr->contents = (unsigned char *) isymbuf;
12711
12712 /* Delete bytes depending on the delcnt and deloff. */
12713 if (!mips_elf_relax_delete_bytes (abfd, sec,
12714 irel->r_offset + deloff, delcnt))
12715 goto error_return;
12716
12717 /* That will change things, so we should relax again.
12718 Note that this is not required, and it may be slow. */
12719 *again = TRUE;
12720 }
12721 }
12722
12723 if (isymbuf != NULL
12724 && symtab_hdr->contents != (unsigned char *) isymbuf)
12725 {
12726 if (! link_info->keep_memory)
12727 free (isymbuf);
12728 else
12729 {
12730 /* Cache the symbols for elf_link_input_bfd. */
12731 symtab_hdr->contents = (unsigned char *) isymbuf;
12732 }
12733 }
12734
12735 if (contents != NULL
12736 && elf_section_data (sec)->this_hdr.contents != contents)
12737 {
12738 if (! link_info->keep_memory)
12739 free (contents);
12740 else
12741 {
12742 /* Cache the section contents for elf_link_input_bfd. */
12743 elf_section_data (sec)->this_hdr.contents = contents;
12744 }
12745 }
12746
12747 if (internal_relocs != NULL
12748 && elf_section_data (sec)->relocs != internal_relocs)
12749 free (internal_relocs);
12750
12751 return TRUE;
12752
12753 error_return:
12754 if (isymbuf != NULL
12755 && symtab_hdr->contents != (unsigned char *) isymbuf)
12756 free (isymbuf);
12757 if (contents != NULL
12758 && elf_section_data (sec)->this_hdr.contents != contents)
12759 free (contents);
12760 if (internal_relocs != NULL
12761 && elf_section_data (sec)->relocs != internal_relocs)
12762 free (internal_relocs);
12763
12764 return FALSE;
12765}
12766\f
b49e97c9
TS
12767/* Create a MIPS ELF linker hash table. */
12768
12769struct bfd_link_hash_table *
9719ad41 12770_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
12771{
12772 struct mips_elf_link_hash_table *ret;
12773 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
12774
9719ad41
RS
12775 ret = bfd_malloc (amt);
12776 if (ret == NULL)
b49e97c9
TS
12777 return NULL;
12778
66eb6687
AM
12779 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
12780 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
12781 sizeof (struct mips_elf_link_hash_entry),
12782 MIPS_ELF_DATA))
b49e97c9 12783 {
e2d34d7d 12784 free (ret);
b49e97c9
TS
12785 return NULL;
12786 }
12787
12788#if 0
12789 /* We no longer use this. */
12790 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
12791 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
12792#endif
12793 ret->procedure_count = 0;
12794 ret->compact_rel_size = 0;
b34976b6 12795 ret->use_rld_obj_head = FALSE;
b49e97c9 12796 ret->rld_value = 0;
b34976b6 12797 ret->mips16_stubs_seen = FALSE;
861fb55a 12798 ret->use_plts_and_copy_relocs = FALSE;
0a44bf69 12799 ret->is_vxworks = FALSE;
0e53d9da 12800 ret->small_data_overflow_reported = FALSE;
0a44bf69
RS
12801 ret->srelbss = NULL;
12802 ret->sdynbss = NULL;
12803 ret->srelplt = NULL;
12804 ret->srelplt2 = NULL;
12805 ret->sgotplt = NULL;
12806 ret->splt = NULL;
4e41d0d7 12807 ret->sstubs = NULL;
a8028dd0
RS
12808 ret->sgot = NULL;
12809 ret->got_info = NULL;
0a44bf69
RS
12810 ret->plt_header_size = 0;
12811 ret->plt_entry_size = 0;
33bb52fb 12812 ret->lazy_stub_count = 0;
5108fc1b 12813 ret->function_stub_size = 0;
861fb55a
DJ
12814 ret->strampoline = NULL;
12815 ret->la25_stubs = NULL;
12816 ret->add_stub_section = NULL;
b49e97c9
TS
12817
12818 return &ret->root.root;
12819}
0a44bf69
RS
12820
12821/* Likewise, but indicate that the target is VxWorks. */
12822
12823struct bfd_link_hash_table *
12824_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
12825{
12826 struct bfd_link_hash_table *ret;
12827
12828 ret = _bfd_mips_elf_link_hash_table_create (abfd);
12829 if (ret)
12830 {
12831 struct mips_elf_link_hash_table *htab;
12832
12833 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
12834 htab->use_plts_and_copy_relocs = TRUE;
12835 htab->is_vxworks = TRUE;
0a44bf69
RS
12836 }
12837 return ret;
12838}
861fb55a
DJ
12839
12840/* A function that the linker calls if we are allowed to use PLTs
12841 and copy relocs. */
12842
12843void
12844_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
12845{
12846 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
12847}
b49e97c9
TS
12848\f
12849/* We need to use a special link routine to handle the .reginfo and
12850 the .mdebug sections. We need to merge all instances of these
12851 sections together, not write them all out sequentially. */
12852
b34976b6 12853bfd_boolean
9719ad41 12854_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 12855{
b49e97c9
TS
12856 asection *o;
12857 struct bfd_link_order *p;
12858 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
12859 asection *rtproc_sec;
12860 Elf32_RegInfo reginfo;
12861 struct ecoff_debug_info debug;
861fb55a 12862 struct mips_htab_traverse_info hti;
7a2a6943
NC
12863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12864 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 12865 HDRR *symhdr = &debug.symbolic_header;
9719ad41 12866 void *mdebug_handle = NULL;
b49e97c9
TS
12867 asection *s;
12868 EXTR esym;
12869 unsigned int i;
12870 bfd_size_type amt;
0a44bf69 12871 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
12872
12873 static const char * const secname[] =
12874 {
12875 ".text", ".init", ".fini", ".data",
12876 ".rodata", ".sdata", ".sbss", ".bss"
12877 };
12878 static const int sc[] =
12879 {
12880 scText, scInit, scFini, scData,
12881 scRData, scSData, scSBss, scBss
12882 };
12883
d4596a51
RS
12884 /* Sort the dynamic symbols so that those with GOT entries come after
12885 those without. */
0a44bf69 12886 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
12887 BFD_ASSERT (htab != NULL);
12888
d4596a51
RS
12889 if (!mips_elf_sort_hash_table (abfd, info))
12890 return FALSE;
b49e97c9 12891
861fb55a
DJ
12892 /* Create any scheduled LA25 stubs. */
12893 hti.info = info;
12894 hti.output_bfd = abfd;
12895 hti.error = FALSE;
12896 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
12897 if (hti.error)
12898 return FALSE;
12899
b49e97c9
TS
12900 /* Get a value for the GP register. */
12901 if (elf_gp (abfd) == 0)
12902 {
12903 struct bfd_link_hash_entry *h;
12904
b34976b6 12905 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 12906 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
12907 elf_gp (abfd) = (h->u.def.value
12908 + h->u.def.section->output_section->vma
12909 + h->u.def.section->output_offset);
0a44bf69
RS
12910 else if (htab->is_vxworks
12911 && (h = bfd_link_hash_lookup (info->hash,
12912 "_GLOBAL_OFFSET_TABLE_",
12913 FALSE, FALSE, TRUE))
12914 && h->type == bfd_link_hash_defined)
12915 elf_gp (abfd) = (h->u.def.section->output_section->vma
12916 + h->u.def.section->output_offset
12917 + h->u.def.value);
1049f94e 12918 else if (info->relocatable)
b49e97c9
TS
12919 {
12920 bfd_vma lo = MINUS_ONE;
12921
12922 /* Find the GP-relative section with the lowest offset. */
9719ad41 12923 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
12924 if (o->vma < lo
12925 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
12926 lo = o->vma;
12927
12928 /* And calculate GP relative to that. */
0a44bf69 12929 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
12930 }
12931 else
12932 {
12933 /* If the relocate_section function needs to do a reloc
12934 involving the GP value, it should make a reloc_dangerous
12935 callback to warn that GP is not defined. */
12936 }
12937 }
12938
12939 /* Go through the sections and collect the .reginfo and .mdebug
12940 information. */
12941 reginfo_sec = NULL;
12942 mdebug_sec = NULL;
12943 gptab_data_sec = NULL;
12944 gptab_bss_sec = NULL;
9719ad41 12945 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
12946 {
12947 if (strcmp (o->name, ".reginfo") == 0)
12948 {
12949 memset (&reginfo, 0, sizeof reginfo);
12950
12951 /* We have found the .reginfo section in the output file.
12952 Look through all the link_orders comprising it and merge
12953 the information together. */
8423293d 12954 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
12955 {
12956 asection *input_section;
12957 bfd *input_bfd;
12958 Elf32_External_RegInfo ext;
12959 Elf32_RegInfo sub;
12960
12961 if (p->type != bfd_indirect_link_order)
12962 {
12963 if (p->type == bfd_data_link_order)
12964 continue;
12965 abort ();
12966 }
12967
12968 input_section = p->u.indirect.section;
12969 input_bfd = input_section->owner;
12970
b49e97c9 12971 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 12972 &ext, 0, sizeof ext))
b34976b6 12973 return FALSE;
b49e97c9
TS
12974
12975 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
12976
12977 reginfo.ri_gprmask |= sub.ri_gprmask;
12978 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
12979 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
12980 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
12981 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
12982
12983 /* ri_gp_value is set by the function
12984 mips_elf32_section_processing when the section is
12985 finally written out. */
12986
12987 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12988 elf_link_input_bfd ignores this section. */
12989 input_section->flags &= ~SEC_HAS_CONTENTS;
12990 }
12991
12992 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 12993 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
12994
12995 /* Skip this section later on (I don't think this currently
12996 matters, but someday it might). */
8423293d 12997 o->map_head.link_order = NULL;
b49e97c9
TS
12998
12999 reginfo_sec = o;
13000 }
13001
13002 if (strcmp (o->name, ".mdebug") == 0)
13003 {
13004 struct extsym_info einfo;
13005 bfd_vma last;
13006
13007 /* We have found the .mdebug section in the output file.
13008 Look through all the link_orders comprising it and merge
13009 the information together. */
13010 symhdr->magic = swap->sym_magic;
13011 /* FIXME: What should the version stamp be? */
13012 symhdr->vstamp = 0;
13013 symhdr->ilineMax = 0;
13014 symhdr->cbLine = 0;
13015 symhdr->idnMax = 0;
13016 symhdr->ipdMax = 0;
13017 symhdr->isymMax = 0;
13018 symhdr->ioptMax = 0;
13019 symhdr->iauxMax = 0;
13020 symhdr->issMax = 0;
13021 symhdr->issExtMax = 0;
13022 symhdr->ifdMax = 0;
13023 symhdr->crfd = 0;
13024 symhdr->iextMax = 0;
13025
13026 /* We accumulate the debugging information itself in the
13027 debug_info structure. */
13028 debug.line = NULL;
13029 debug.external_dnr = NULL;
13030 debug.external_pdr = NULL;
13031 debug.external_sym = NULL;
13032 debug.external_opt = NULL;
13033 debug.external_aux = NULL;
13034 debug.ss = NULL;
13035 debug.ssext = debug.ssext_end = NULL;
13036 debug.external_fdr = NULL;
13037 debug.external_rfd = NULL;
13038 debug.external_ext = debug.external_ext_end = NULL;
13039
13040 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 13041 if (mdebug_handle == NULL)
b34976b6 13042 return FALSE;
b49e97c9
TS
13043
13044 esym.jmptbl = 0;
13045 esym.cobol_main = 0;
13046 esym.weakext = 0;
13047 esym.reserved = 0;
13048 esym.ifd = ifdNil;
13049 esym.asym.iss = issNil;
13050 esym.asym.st = stLocal;
13051 esym.asym.reserved = 0;
13052 esym.asym.index = indexNil;
13053 last = 0;
13054 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13055 {
13056 esym.asym.sc = sc[i];
13057 s = bfd_get_section_by_name (abfd, secname[i]);
13058 if (s != NULL)
13059 {
13060 esym.asym.value = s->vma;
eea6121a 13061 last = s->vma + s->size;
b49e97c9
TS
13062 }
13063 else
13064 esym.asym.value = last;
13065 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13066 secname[i], &esym))
b34976b6 13067 return FALSE;
b49e97c9
TS
13068 }
13069
8423293d 13070 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13071 {
13072 asection *input_section;
13073 bfd *input_bfd;
13074 const struct ecoff_debug_swap *input_swap;
13075 struct ecoff_debug_info input_debug;
13076 char *eraw_src;
13077 char *eraw_end;
13078
13079 if (p->type != bfd_indirect_link_order)
13080 {
13081 if (p->type == bfd_data_link_order)
13082 continue;
13083 abort ();
13084 }
13085
13086 input_section = p->u.indirect.section;
13087 input_bfd = input_section->owner;
13088
d5eaccd7 13089 if (!is_mips_elf (input_bfd))
b49e97c9
TS
13090 {
13091 /* I don't know what a non MIPS ELF bfd would be
13092 doing with a .mdebug section, but I don't really
13093 want to deal with it. */
13094 continue;
13095 }
13096
13097 input_swap = (get_elf_backend_data (input_bfd)
13098 ->elf_backend_ecoff_debug_swap);
13099
eea6121a 13100 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
13101
13102 /* The ECOFF linking code expects that we have already
13103 read in the debugging information and set up an
13104 ecoff_debug_info structure, so we do that now. */
13105 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13106 &input_debug))
b34976b6 13107 return FALSE;
b49e97c9
TS
13108
13109 if (! (bfd_ecoff_debug_accumulate
13110 (mdebug_handle, abfd, &debug, swap, input_bfd,
13111 &input_debug, input_swap, info)))
b34976b6 13112 return FALSE;
b49e97c9
TS
13113
13114 /* Loop through the external symbols. For each one with
13115 interesting information, try to find the symbol in
13116 the linker global hash table and save the information
13117 for the output external symbols. */
13118 eraw_src = input_debug.external_ext;
13119 eraw_end = (eraw_src
13120 + (input_debug.symbolic_header.iextMax
13121 * input_swap->external_ext_size));
13122 for (;
13123 eraw_src < eraw_end;
13124 eraw_src += input_swap->external_ext_size)
13125 {
13126 EXTR ext;
13127 const char *name;
13128 struct mips_elf_link_hash_entry *h;
13129
9719ad41 13130 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
13131 if (ext.asym.sc == scNil
13132 || ext.asym.sc == scUndefined
13133 || ext.asym.sc == scSUndefined)
13134 continue;
13135
13136 name = input_debug.ssext + ext.asym.iss;
13137 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 13138 name, FALSE, FALSE, TRUE);
b49e97c9
TS
13139 if (h == NULL || h->esym.ifd != -2)
13140 continue;
13141
13142 if (ext.ifd != -1)
13143 {
13144 BFD_ASSERT (ext.ifd
13145 < input_debug.symbolic_header.ifdMax);
13146 ext.ifd = input_debug.ifdmap[ext.ifd];
13147 }
13148
13149 h->esym = ext;
13150 }
13151
13152 /* Free up the information we just read. */
13153 free (input_debug.line);
13154 free (input_debug.external_dnr);
13155 free (input_debug.external_pdr);
13156 free (input_debug.external_sym);
13157 free (input_debug.external_opt);
13158 free (input_debug.external_aux);
13159 free (input_debug.ss);
13160 free (input_debug.ssext);
13161 free (input_debug.external_fdr);
13162 free (input_debug.external_rfd);
13163 free (input_debug.external_ext);
13164
13165 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13166 elf_link_input_bfd ignores this section. */
13167 input_section->flags &= ~SEC_HAS_CONTENTS;
13168 }
13169
13170 if (SGI_COMPAT (abfd) && info->shared)
13171 {
13172 /* Create .rtproc section. */
13173 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
13174 if (rtproc_sec == NULL)
13175 {
13176 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13177 | SEC_LINKER_CREATED | SEC_READONLY);
13178
3496cb2a
L
13179 rtproc_sec = bfd_make_section_with_flags (abfd,
13180 ".rtproc",
13181 flags);
b49e97c9 13182 if (rtproc_sec == NULL
b49e97c9 13183 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 13184 return FALSE;
b49e97c9
TS
13185 }
13186
13187 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13188 info, rtproc_sec,
13189 &debug))
b34976b6 13190 return FALSE;
b49e97c9
TS
13191 }
13192
13193 /* Build the external symbol information. */
13194 einfo.abfd = abfd;
13195 einfo.info = info;
13196 einfo.debug = &debug;
13197 einfo.swap = swap;
b34976b6 13198 einfo.failed = FALSE;
b49e97c9 13199 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 13200 mips_elf_output_extsym, &einfo);
b49e97c9 13201 if (einfo.failed)
b34976b6 13202 return FALSE;
b49e97c9
TS
13203
13204 /* Set the size of the .mdebug section. */
eea6121a 13205 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
13206
13207 /* Skip this section later on (I don't think this currently
13208 matters, but someday it might). */
8423293d 13209 o->map_head.link_order = NULL;
b49e97c9
TS
13210
13211 mdebug_sec = o;
13212 }
13213
0112cd26 13214 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
13215 {
13216 const char *subname;
13217 unsigned int c;
13218 Elf32_gptab *tab;
13219 Elf32_External_gptab *ext_tab;
13220 unsigned int j;
13221
13222 /* The .gptab.sdata and .gptab.sbss sections hold
13223 information describing how the small data area would
13224 change depending upon the -G switch. These sections
13225 not used in executables files. */
1049f94e 13226 if (! info->relocatable)
b49e97c9 13227 {
8423293d 13228 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13229 {
13230 asection *input_section;
13231
13232 if (p->type != bfd_indirect_link_order)
13233 {
13234 if (p->type == bfd_data_link_order)
13235 continue;
13236 abort ();
13237 }
13238
13239 input_section = p->u.indirect.section;
13240
13241 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13242 elf_link_input_bfd ignores this section. */
13243 input_section->flags &= ~SEC_HAS_CONTENTS;
13244 }
13245
13246 /* Skip this section later on (I don't think this
13247 currently matters, but someday it might). */
8423293d 13248 o->map_head.link_order = NULL;
b49e97c9
TS
13249
13250 /* Really remove the section. */
5daa8fe7 13251 bfd_section_list_remove (abfd, o);
b49e97c9
TS
13252 --abfd->section_count;
13253
13254 continue;
13255 }
13256
13257 /* There is one gptab for initialized data, and one for
13258 uninitialized data. */
13259 if (strcmp (o->name, ".gptab.sdata") == 0)
13260 gptab_data_sec = o;
13261 else if (strcmp (o->name, ".gptab.sbss") == 0)
13262 gptab_bss_sec = o;
13263 else
13264 {
13265 (*_bfd_error_handler)
13266 (_("%s: illegal section name `%s'"),
13267 bfd_get_filename (abfd), o->name);
13268 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 13269 return FALSE;
b49e97c9
TS
13270 }
13271
13272 /* The linker script always combines .gptab.data and
13273 .gptab.sdata into .gptab.sdata, and likewise for
13274 .gptab.bss and .gptab.sbss. It is possible that there is
13275 no .sdata or .sbss section in the output file, in which
13276 case we must change the name of the output section. */
13277 subname = o->name + sizeof ".gptab" - 1;
13278 if (bfd_get_section_by_name (abfd, subname) == NULL)
13279 {
13280 if (o == gptab_data_sec)
13281 o->name = ".gptab.data";
13282 else
13283 o->name = ".gptab.bss";
13284 subname = o->name + sizeof ".gptab" - 1;
13285 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
13286 }
13287
13288 /* Set up the first entry. */
13289 c = 1;
13290 amt = c * sizeof (Elf32_gptab);
9719ad41 13291 tab = bfd_malloc (amt);
b49e97c9 13292 if (tab == NULL)
b34976b6 13293 return FALSE;
b49e97c9
TS
13294 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
13295 tab[0].gt_header.gt_unused = 0;
13296
13297 /* Combine the input sections. */
8423293d 13298 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
13299 {
13300 asection *input_section;
13301 bfd *input_bfd;
13302 bfd_size_type size;
13303 unsigned long last;
13304 bfd_size_type gpentry;
13305
13306 if (p->type != bfd_indirect_link_order)
13307 {
13308 if (p->type == bfd_data_link_order)
13309 continue;
13310 abort ();
13311 }
13312
13313 input_section = p->u.indirect.section;
13314 input_bfd = input_section->owner;
13315
13316 /* Combine the gptab entries for this input section one
13317 by one. We know that the input gptab entries are
13318 sorted by ascending -G value. */
eea6121a 13319 size = input_section->size;
b49e97c9
TS
13320 last = 0;
13321 for (gpentry = sizeof (Elf32_External_gptab);
13322 gpentry < size;
13323 gpentry += sizeof (Elf32_External_gptab))
13324 {
13325 Elf32_External_gptab ext_gptab;
13326 Elf32_gptab int_gptab;
13327 unsigned long val;
13328 unsigned long add;
b34976b6 13329 bfd_boolean exact;
b49e97c9
TS
13330 unsigned int look;
13331
13332 if (! (bfd_get_section_contents
9719ad41
RS
13333 (input_bfd, input_section, &ext_gptab, gpentry,
13334 sizeof (Elf32_External_gptab))))
b49e97c9
TS
13335 {
13336 free (tab);
b34976b6 13337 return FALSE;
b49e97c9
TS
13338 }
13339
13340 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
13341 &int_gptab);
13342 val = int_gptab.gt_entry.gt_g_value;
13343 add = int_gptab.gt_entry.gt_bytes - last;
13344
b34976b6 13345 exact = FALSE;
b49e97c9
TS
13346 for (look = 1; look < c; look++)
13347 {
13348 if (tab[look].gt_entry.gt_g_value >= val)
13349 tab[look].gt_entry.gt_bytes += add;
13350
13351 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 13352 exact = TRUE;
b49e97c9
TS
13353 }
13354
13355 if (! exact)
13356 {
13357 Elf32_gptab *new_tab;
13358 unsigned int max;
13359
13360 /* We need a new table entry. */
13361 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 13362 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
13363 if (new_tab == NULL)
13364 {
13365 free (tab);
b34976b6 13366 return FALSE;
b49e97c9
TS
13367 }
13368 tab = new_tab;
13369 tab[c].gt_entry.gt_g_value = val;
13370 tab[c].gt_entry.gt_bytes = add;
13371
13372 /* Merge in the size for the next smallest -G
13373 value, since that will be implied by this new
13374 value. */
13375 max = 0;
13376 for (look = 1; look < c; look++)
13377 {
13378 if (tab[look].gt_entry.gt_g_value < val
13379 && (max == 0
13380 || (tab[look].gt_entry.gt_g_value
13381 > tab[max].gt_entry.gt_g_value)))
13382 max = look;
13383 }
13384 if (max != 0)
13385 tab[c].gt_entry.gt_bytes +=
13386 tab[max].gt_entry.gt_bytes;
13387
13388 ++c;
13389 }
13390
13391 last = int_gptab.gt_entry.gt_bytes;
13392 }
13393
13394 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13395 elf_link_input_bfd ignores this section. */
13396 input_section->flags &= ~SEC_HAS_CONTENTS;
13397 }
13398
13399 /* The table must be sorted by -G value. */
13400 if (c > 2)
13401 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
13402
13403 /* Swap out the table. */
13404 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 13405 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
13406 if (ext_tab == NULL)
13407 {
13408 free (tab);
b34976b6 13409 return FALSE;
b49e97c9
TS
13410 }
13411
13412 for (j = 0; j < c; j++)
13413 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
13414 free (tab);
13415
eea6121a 13416 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
13417 o->contents = (bfd_byte *) ext_tab;
13418
13419 /* Skip this section later on (I don't think this currently
13420 matters, but someday it might). */
8423293d 13421 o->map_head.link_order = NULL;
b49e97c9
TS
13422 }
13423 }
13424
13425 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 13426 if (!bfd_elf_final_link (abfd, info))
b34976b6 13427 return FALSE;
b49e97c9
TS
13428
13429 /* Now write out the computed sections. */
13430
9719ad41 13431 if (reginfo_sec != NULL)
b49e97c9
TS
13432 {
13433 Elf32_External_RegInfo ext;
13434
13435 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 13436 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 13437 return FALSE;
b49e97c9
TS
13438 }
13439
9719ad41 13440 if (mdebug_sec != NULL)
b49e97c9
TS
13441 {
13442 BFD_ASSERT (abfd->output_has_begun);
13443 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
13444 swap, info,
13445 mdebug_sec->filepos))
b34976b6 13446 return FALSE;
b49e97c9
TS
13447
13448 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
13449 }
13450
9719ad41 13451 if (gptab_data_sec != NULL)
b49e97c9
TS
13452 {
13453 if (! bfd_set_section_contents (abfd, gptab_data_sec,
13454 gptab_data_sec->contents,
eea6121a 13455 0, gptab_data_sec->size))
b34976b6 13456 return FALSE;
b49e97c9
TS
13457 }
13458
9719ad41 13459 if (gptab_bss_sec != NULL)
b49e97c9
TS
13460 {
13461 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
13462 gptab_bss_sec->contents,
eea6121a 13463 0, gptab_bss_sec->size))
b34976b6 13464 return FALSE;
b49e97c9
TS
13465 }
13466
13467 if (SGI_COMPAT (abfd))
13468 {
13469 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
13470 if (rtproc_sec != NULL)
13471 {
13472 if (! bfd_set_section_contents (abfd, rtproc_sec,
13473 rtproc_sec->contents,
eea6121a 13474 0, rtproc_sec->size))
b34976b6 13475 return FALSE;
b49e97c9
TS
13476 }
13477 }
13478
b34976b6 13479 return TRUE;
b49e97c9
TS
13480}
13481\f
64543e1a
RS
13482/* Structure for saying that BFD machine EXTENSION extends BASE. */
13483
13484struct mips_mach_extension {
13485 unsigned long extension, base;
13486};
13487
13488
13489/* An array describing how BFD machines relate to one another. The entries
13490 are ordered topologically with MIPS I extensions listed last. */
13491
13492static const struct mips_mach_extension mips_mach_extensions[] = {
6f179bd0
AN
13493 /* MIPS64r2 extensions. */
13494 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
13495
64543e1a 13496 /* MIPS64 extensions. */
5f74bc13 13497 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 13498 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 13499 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
fd503541 13500 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
64543e1a
RS
13501
13502 /* MIPS V extensions. */
13503 { bfd_mach_mipsisa64, bfd_mach_mips5 },
13504
13505 /* R10000 extensions. */
13506 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
13507 { bfd_mach_mips14000, bfd_mach_mips10000 },
13508 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
13509
13510 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
13511 vr5400 ISA, but doesn't include the multimedia stuff. It seems
13512 better to allow vr5400 and vr5500 code to be merged anyway, since
13513 many libraries will just use the core ISA. Perhaps we could add
13514 some sort of ASE flag if this ever proves a problem. */
13515 { bfd_mach_mips5500, bfd_mach_mips5400 },
13516 { bfd_mach_mips5400, bfd_mach_mips5000 },
13517
13518 /* MIPS IV extensions. */
13519 { bfd_mach_mips5, bfd_mach_mips8000 },
13520 { bfd_mach_mips10000, bfd_mach_mips8000 },
13521 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 13522 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 13523 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
13524
13525 /* VR4100 extensions. */
13526 { bfd_mach_mips4120, bfd_mach_mips4100 },
13527 { bfd_mach_mips4111, bfd_mach_mips4100 },
13528
13529 /* MIPS III extensions. */
350cc38d
MS
13530 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
13531 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
13532 { bfd_mach_mips8000, bfd_mach_mips4000 },
13533 { bfd_mach_mips4650, bfd_mach_mips4000 },
13534 { bfd_mach_mips4600, bfd_mach_mips4000 },
13535 { bfd_mach_mips4400, bfd_mach_mips4000 },
13536 { bfd_mach_mips4300, bfd_mach_mips4000 },
13537 { bfd_mach_mips4100, bfd_mach_mips4000 },
13538 { bfd_mach_mips4010, bfd_mach_mips4000 },
13539
13540 /* MIPS32 extensions. */
13541 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
13542
13543 /* MIPS II extensions. */
13544 { bfd_mach_mips4000, bfd_mach_mips6000 },
13545 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
13546
13547 /* MIPS I extensions. */
13548 { bfd_mach_mips6000, bfd_mach_mips3000 },
13549 { bfd_mach_mips3900, bfd_mach_mips3000 }
13550};
13551
13552
13553/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
13554
13555static bfd_boolean
9719ad41 13556mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
13557{
13558 size_t i;
13559
c5211a54
RS
13560 if (extension == base)
13561 return TRUE;
13562
13563 if (base == bfd_mach_mipsisa32
13564 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
13565 return TRUE;
13566
13567 if (base == bfd_mach_mipsisa32r2
13568 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
13569 return TRUE;
13570
13571 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 13572 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
13573 {
13574 extension = mips_mach_extensions[i].base;
13575 if (extension == base)
13576 return TRUE;
13577 }
64543e1a 13578
c5211a54 13579 return FALSE;
64543e1a
RS
13580}
13581
13582
13583/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 13584
b34976b6 13585static bfd_boolean
9719ad41 13586mips_32bit_flags_p (flagword flags)
00707a0e 13587{
64543e1a
RS
13588 return ((flags & EF_MIPS_32BITMODE) != 0
13589 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
13590 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
13591 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
13592 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
13593 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
13594 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
13595}
13596
64543e1a 13597
2cf19d5c
JM
13598/* Merge object attributes from IBFD into OBFD. Raise an error if
13599 there are conflicting attributes. */
13600static bfd_boolean
13601mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
13602{
13603 obj_attribute *in_attr;
13604 obj_attribute *out_attr;
13605
13606 if (!elf_known_obj_attributes_proc (obfd)[0].i)
13607 {
13608 /* This is the first object. Copy the attributes. */
13609 _bfd_elf_copy_obj_attributes (ibfd, obfd);
13610
13611 /* Use the Tag_null value to indicate the attributes have been
13612 initialized. */
13613 elf_known_obj_attributes_proc (obfd)[0].i = 1;
13614
13615 return TRUE;
13616 }
13617
13618 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
13619 non-conflicting ones. */
13620 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
13621 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
13622 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
13623 {
13624 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
13625 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
13626 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
13627 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
13628 ;
42554f6a 13629 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
13630 _bfd_error_handler
13631 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
13632 in_attr[Tag_GNU_MIPS_ABI_FP].i);
42554f6a 13633 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
13634 _bfd_error_handler
13635 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
13636 out_attr[Tag_GNU_MIPS_ABI_FP].i);
13637 else
13638 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
13639 {
13640 case 1:
13641 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13642 {
13643 case 2:
13644 _bfd_error_handler
13645 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
13646 obfd, ibfd);
51a0dd31 13647 break;
2cf19d5c
JM
13648
13649 case 3:
13650 _bfd_error_handler
13651 (_("Warning: %B uses hard float, %B uses soft float"),
13652 obfd, ibfd);
13653 break;
13654
42554f6a
TS
13655 case 4:
13656 _bfd_error_handler
13657 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
13658 obfd, ibfd);
13659 break;
13660
2cf19d5c
JM
13661 default:
13662 abort ();
13663 }
13664 break;
13665
13666 case 2:
13667 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13668 {
13669 case 1:
13670 _bfd_error_handler
13671 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
13672 ibfd, obfd);
51a0dd31 13673 break;
2cf19d5c
JM
13674
13675 case 3:
13676 _bfd_error_handler
13677 (_("Warning: %B uses hard float, %B uses soft float"),
13678 obfd, ibfd);
13679 break;
13680
42554f6a
TS
13681 case 4:
13682 _bfd_error_handler
13683 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
13684 obfd, ibfd);
13685 break;
13686
2cf19d5c
JM
13687 default:
13688 abort ();
13689 }
13690 break;
13691
13692 case 3:
13693 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13694 {
13695 case 1:
13696 case 2:
42554f6a 13697 case 4:
2cf19d5c
JM
13698 _bfd_error_handler
13699 (_("Warning: %B uses hard float, %B uses soft float"),
13700 ibfd, obfd);
13701 break;
13702
13703 default:
13704 abort ();
13705 }
13706 break;
13707
42554f6a
TS
13708 case 4:
13709 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13710 {
13711 case 1:
13712 _bfd_error_handler
13713 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
13714 ibfd, obfd);
13715 break;
13716
13717 case 2:
13718 _bfd_error_handler
13719 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
13720 ibfd, obfd);
13721 break;
13722
13723 case 3:
13724 _bfd_error_handler
13725 (_("Warning: %B uses hard float, %B uses soft float"),
13726 obfd, ibfd);
13727 break;
13728
13729 default:
13730 abort ();
13731 }
13732 break;
13733
2cf19d5c
JM
13734 default:
13735 abort ();
13736 }
13737 }
13738
13739 /* Merge Tag_compatibility attributes and any common GNU ones. */
13740 _bfd_elf_merge_object_attributes (ibfd, obfd);
13741
13742 return TRUE;
13743}
13744
b49e97c9
TS
13745/* Merge backend specific data from an object file to the output
13746 object file when linking. */
13747
b34976b6 13748bfd_boolean
9719ad41 13749_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
13750{
13751 flagword old_flags;
13752 flagword new_flags;
b34976b6
AM
13753 bfd_boolean ok;
13754 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
13755 asection *sec;
13756
58238693 13757 /* Check if we have the same endianness. */
82e51918 13758 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
13759 {
13760 (*_bfd_error_handler)
d003868e
AM
13761 (_("%B: endianness incompatible with that of the selected emulation"),
13762 ibfd);
aa701218
AO
13763 return FALSE;
13764 }
b49e97c9 13765
d5eaccd7 13766 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 13767 return TRUE;
b49e97c9 13768
aa701218
AO
13769 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
13770 {
13771 (*_bfd_error_handler)
d003868e
AM
13772 (_("%B: ABI is incompatible with that of the selected emulation"),
13773 ibfd);
aa701218
AO
13774 return FALSE;
13775 }
13776
2cf19d5c
JM
13777 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
13778 return FALSE;
13779
b49e97c9
TS
13780 new_flags = elf_elfheader (ibfd)->e_flags;
13781 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
13782 old_flags = elf_elfheader (obfd)->e_flags;
13783
13784 if (! elf_flags_init (obfd))
13785 {
b34976b6 13786 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
13787 elf_elfheader (obfd)->e_flags = new_flags;
13788 elf_elfheader (obfd)->e_ident[EI_CLASS]
13789 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
13790
13791 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861
TS
13792 && (bfd_get_arch_info (obfd)->the_default
13793 || mips_mach_extends_p (bfd_get_mach (obfd),
13794 bfd_get_mach (ibfd))))
b49e97c9
TS
13795 {
13796 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
13797 bfd_get_mach (ibfd)))
b34976b6 13798 return FALSE;
b49e97c9
TS
13799 }
13800
b34976b6 13801 return TRUE;
b49e97c9
TS
13802 }
13803
13804 /* Check flag compatibility. */
13805
13806 new_flags &= ~EF_MIPS_NOREORDER;
13807 old_flags &= ~EF_MIPS_NOREORDER;
13808
f4416af6
AO
13809 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
13810 doesn't seem to matter. */
13811 new_flags &= ~EF_MIPS_XGOT;
13812 old_flags &= ~EF_MIPS_XGOT;
13813
98a8deaf
RS
13814 /* MIPSpro generates ucode info in n64 objects. Again, we should
13815 just be able to ignore this. */
13816 new_flags &= ~EF_MIPS_UCODE;
13817 old_flags &= ~EF_MIPS_UCODE;
13818
861fb55a
DJ
13819 /* DSOs should only be linked with CPIC code. */
13820 if ((ibfd->flags & DYNAMIC) != 0)
13821 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 13822
b49e97c9 13823 if (new_flags == old_flags)
b34976b6 13824 return TRUE;
b49e97c9
TS
13825
13826 /* Check to see if the input BFD actually contains any sections.
13827 If not, its flags may not have been initialised either, but it cannot
13828 actually cause any incompatibility. */
13829 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
13830 {
13831 /* Ignore synthetic sections and empty .text, .data and .bss sections
ed88c97e
RS
13832 which are automatically generated by gas. Also ignore fake
13833 (s)common sections, since merely defining a common symbol does
13834 not affect compatibility. */
13835 if ((sec->flags & SEC_IS_COMMON) == 0
13836 && strcmp (sec->name, ".reginfo")
b49e97c9 13837 && strcmp (sec->name, ".mdebug")
eea6121a 13838 && (sec->size != 0
d13d89fa
NS
13839 || (strcmp (sec->name, ".text")
13840 && strcmp (sec->name, ".data")
13841 && strcmp (sec->name, ".bss"))))
b49e97c9 13842 {
b34976b6 13843 null_input_bfd = FALSE;
b49e97c9
TS
13844 break;
13845 }
13846 }
13847 if (null_input_bfd)
b34976b6 13848 return TRUE;
b49e97c9 13849
b34976b6 13850 ok = TRUE;
b49e97c9 13851
143d77c5
EC
13852 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
13853 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 13854 {
b49e97c9 13855 (*_bfd_error_handler)
861fb55a 13856 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 13857 ibfd);
143d77c5 13858 ok = TRUE;
b49e97c9
TS
13859 }
13860
143d77c5
EC
13861 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
13862 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
13863 if (! (new_flags & EF_MIPS_PIC))
13864 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
13865
13866 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
13867 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 13868
64543e1a
RS
13869 /* Compare the ISAs. */
13870 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 13871 {
64543e1a 13872 (*_bfd_error_handler)
d003868e
AM
13873 (_("%B: linking 32-bit code with 64-bit code"),
13874 ibfd);
64543e1a
RS
13875 ok = FALSE;
13876 }
13877 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
13878 {
13879 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
13880 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 13881 {
64543e1a
RS
13882 /* Copy the architecture info from IBFD to OBFD. Also copy
13883 the 32-bit flag (if set) so that we continue to recognise
13884 OBFD as a 32-bit binary. */
13885 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
13886 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
13887 elf_elfheader (obfd)->e_flags
13888 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13889
13890 /* Copy across the ABI flags if OBFD doesn't use them
13891 and if that was what caused us to treat IBFD as 32-bit. */
13892 if ((old_flags & EF_MIPS_ABI) == 0
13893 && mips_32bit_flags_p (new_flags)
13894 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
13895 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
13896 }
13897 else
13898 {
64543e1a 13899 /* The ISAs aren't compatible. */
b49e97c9 13900 (*_bfd_error_handler)
d003868e
AM
13901 (_("%B: linking %s module with previous %s modules"),
13902 ibfd,
64543e1a
RS
13903 bfd_printable_name (ibfd),
13904 bfd_printable_name (obfd));
b34976b6 13905 ok = FALSE;
b49e97c9 13906 }
b49e97c9
TS
13907 }
13908
64543e1a
RS
13909 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13910 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13911
13912 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
13913 does set EI_CLASS differently from any 32-bit ABI. */
13914 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
13915 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
13916 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
13917 {
13918 /* Only error if both are set (to different values). */
13919 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
13920 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
13921 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
13922 {
13923 (*_bfd_error_handler)
d003868e
AM
13924 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
13925 ibfd,
b49e97c9
TS
13926 elf_mips_abi_name (ibfd),
13927 elf_mips_abi_name (obfd));
b34976b6 13928 ok = FALSE;
b49e97c9
TS
13929 }
13930 new_flags &= ~EF_MIPS_ABI;
13931 old_flags &= ~EF_MIPS_ABI;
13932 }
13933
df58fc94
RS
13934 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
13935 and allow arbitrary mixing of the remaining ASEs (retain the union). */
fb39dac1
RS
13936 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
13937 {
df58fc94
RS
13938 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
13939 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
13940 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
13941 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
13942 int micro_mis = old_m16 && new_micro;
13943 int m16_mis = old_micro && new_m16;
13944
13945 if (m16_mis || micro_mis)
13946 {
13947 (*_bfd_error_handler)
13948 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
13949 ibfd,
13950 m16_mis ? "MIPS16" : "microMIPS",
13951 m16_mis ? "microMIPS" : "MIPS16");
13952 ok = FALSE;
13953 }
13954
fb39dac1
RS
13955 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
13956
13957 new_flags &= ~ EF_MIPS_ARCH_ASE;
13958 old_flags &= ~ EF_MIPS_ARCH_ASE;
13959 }
13960
b49e97c9
TS
13961 /* Warn about any other mismatches */
13962 if (new_flags != old_flags)
13963 {
13964 (*_bfd_error_handler)
d003868e
AM
13965 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
13966 ibfd, (unsigned long) new_flags,
b49e97c9 13967 (unsigned long) old_flags);
b34976b6 13968 ok = FALSE;
b49e97c9
TS
13969 }
13970
13971 if (! ok)
13972 {
13973 bfd_set_error (bfd_error_bad_value);
b34976b6 13974 return FALSE;
b49e97c9
TS
13975 }
13976
b34976b6 13977 return TRUE;
b49e97c9
TS
13978}
13979
13980/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
13981
b34976b6 13982bfd_boolean
9719ad41 13983_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
13984{
13985 BFD_ASSERT (!elf_flags_init (abfd)
13986 || elf_elfheader (abfd)->e_flags == flags);
13987
13988 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
13989 elf_flags_init (abfd) = TRUE;
13990 return TRUE;
b49e97c9
TS
13991}
13992
ad9563d6
CM
13993char *
13994_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
13995{
13996 switch (dtag)
13997 {
13998 default: return "";
13999 case DT_MIPS_RLD_VERSION:
14000 return "MIPS_RLD_VERSION";
14001 case DT_MIPS_TIME_STAMP:
14002 return "MIPS_TIME_STAMP";
14003 case DT_MIPS_ICHECKSUM:
14004 return "MIPS_ICHECKSUM";
14005 case DT_MIPS_IVERSION:
14006 return "MIPS_IVERSION";
14007 case DT_MIPS_FLAGS:
14008 return "MIPS_FLAGS";
14009 case DT_MIPS_BASE_ADDRESS:
14010 return "MIPS_BASE_ADDRESS";
14011 case DT_MIPS_MSYM:
14012 return "MIPS_MSYM";
14013 case DT_MIPS_CONFLICT:
14014 return "MIPS_CONFLICT";
14015 case DT_MIPS_LIBLIST:
14016 return "MIPS_LIBLIST";
14017 case DT_MIPS_LOCAL_GOTNO:
14018 return "MIPS_LOCAL_GOTNO";
14019 case DT_MIPS_CONFLICTNO:
14020 return "MIPS_CONFLICTNO";
14021 case DT_MIPS_LIBLISTNO:
14022 return "MIPS_LIBLISTNO";
14023 case DT_MIPS_SYMTABNO:
14024 return "MIPS_SYMTABNO";
14025 case DT_MIPS_UNREFEXTNO:
14026 return "MIPS_UNREFEXTNO";
14027 case DT_MIPS_GOTSYM:
14028 return "MIPS_GOTSYM";
14029 case DT_MIPS_HIPAGENO:
14030 return "MIPS_HIPAGENO";
14031 case DT_MIPS_RLD_MAP:
14032 return "MIPS_RLD_MAP";
14033 case DT_MIPS_DELTA_CLASS:
14034 return "MIPS_DELTA_CLASS";
14035 case DT_MIPS_DELTA_CLASS_NO:
14036 return "MIPS_DELTA_CLASS_NO";
14037 case DT_MIPS_DELTA_INSTANCE:
14038 return "MIPS_DELTA_INSTANCE";
14039 case DT_MIPS_DELTA_INSTANCE_NO:
14040 return "MIPS_DELTA_INSTANCE_NO";
14041 case DT_MIPS_DELTA_RELOC:
14042 return "MIPS_DELTA_RELOC";
14043 case DT_MIPS_DELTA_RELOC_NO:
14044 return "MIPS_DELTA_RELOC_NO";
14045 case DT_MIPS_DELTA_SYM:
14046 return "MIPS_DELTA_SYM";
14047 case DT_MIPS_DELTA_SYM_NO:
14048 return "MIPS_DELTA_SYM_NO";
14049 case DT_MIPS_DELTA_CLASSSYM:
14050 return "MIPS_DELTA_CLASSSYM";
14051 case DT_MIPS_DELTA_CLASSSYM_NO:
14052 return "MIPS_DELTA_CLASSSYM_NO";
14053 case DT_MIPS_CXX_FLAGS:
14054 return "MIPS_CXX_FLAGS";
14055 case DT_MIPS_PIXIE_INIT:
14056 return "MIPS_PIXIE_INIT";
14057 case DT_MIPS_SYMBOL_LIB:
14058 return "MIPS_SYMBOL_LIB";
14059 case DT_MIPS_LOCALPAGE_GOTIDX:
14060 return "MIPS_LOCALPAGE_GOTIDX";
14061 case DT_MIPS_LOCAL_GOTIDX:
14062 return "MIPS_LOCAL_GOTIDX";
14063 case DT_MIPS_HIDDEN_GOTIDX:
14064 return "MIPS_HIDDEN_GOTIDX";
14065 case DT_MIPS_PROTECTED_GOTIDX:
14066 return "MIPS_PROTECTED_GOT_IDX";
14067 case DT_MIPS_OPTIONS:
14068 return "MIPS_OPTIONS";
14069 case DT_MIPS_INTERFACE:
14070 return "MIPS_INTERFACE";
14071 case DT_MIPS_DYNSTR_ALIGN:
14072 return "DT_MIPS_DYNSTR_ALIGN";
14073 case DT_MIPS_INTERFACE_SIZE:
14074 return "DT_MIPS_INTERFACE_SIZE";
14075 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14076 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14077 case DT_MIPS_PERF_SUFFIX:
14078 return "DT_MIPS_PERF_SUFFIX";
14079 case DT_MIPS_COMPACT_SIZE:
14080 return "DT_MIPS_COMPACT_SIZE";
14081 case DT_MIPS_GP_VALUE:
14082 return "DT_MIPS_GP_VALUE";
14083 case DT_MIPS_AUX_DYNAMIC:
14084 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
14085 case DT_MIPS_PLTGOT:
14086 return "DT_MIPS_PLTGOT";
14087 case DT_MIPS_RWPLT:
14088 return "DT_MIPS_RWPLT";
ad9563d6
CM
14089 }
14090}
14091
b34976b6 14092bfd_boolean
9719ad41 14093_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 14094{
9719ad41 14095 FILE *file = ptr;
b49e97c9
TS
14096
14097 BFD_ASSERT (abfd != NULL && ptr != NULL);
14098
14099 /* Print normal ELF private data. */
14100 _bfd_elf_print_private_bfd_data (abfd, ptr);
14101
14102 /* xgettext:c-format */
14103 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14104
14105 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14106 fprintf (file, _(" [abi=O32]"));
14107 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14108 fprintf (file, _(" [abi=O64]"));
14109 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14110 fprintf (file, _(" [abi=EABI32]"));
14111 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14112 fprintf (file, _(" [abi=EABI64]"));
14113 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14114 fprintf (file, _(" [abi unknown]"));
14115 else if (ABI_N32_P (abfd))
14116 fprintf (file, _(" [abi=N32]"));
14117 else if (ABI_64_P (abfd))
14118 fprintf (file, _(" [abi=64]"));
14119 else
14120 fprintf (file, _(" [no abi set]"));
14121
14122 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 14123 fprintf (file, " [mips1]");
b49e97c9 14124 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 14125 fprintf (file, " [mips2]");
b49e97c9 14126 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 14127 fprintf (file, " [mips3]");
b49e97c9 14128 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 14129 fprintf (file, " [mips4]");
b49e97c9 14130 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 14131 fprintf (file, " [mips5]");
b49e97c9 14132 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 14133 fprintf (file, " [mips32]");
b49e97c9 14134 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 14135 fprintf (file, " [mips64]");
af7ee8bf 14136 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 14137 fprintf (file, " [mips32r2]");
5f74bc13 14138 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 14139 fprintf (file, " [mips64r2]");
b49e97c9
TS
14140 else
14141 fprintf (file, _(" [unknown ISA]"));
14142
40d32fc6 14143 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 14144 fprintf (file, " [mdmx]");
40d32fc6
CD
14145
14146 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 14147 fprintf (file, " [mips16]");
40d32fc6 14148
df58fc94
RS
14149 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14150 fprintf (file, " [micromips]");
14151
b49e97c9 14152 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 14153 fprintf (file, " [32bitmode]");
b49e97c9
TS
14154 else
14155 fprintf (file, _(" [not 32bitmode]"));
14156
c0e3f241 14157 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 14158 fprintf (file, " [noreorder]");
c0e3f241
CD
14159
14160 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 14161 fprintf (file, " [PIC]");
c0e3f241
CD
14162
14163 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 14164 fprintf (file, " [CPIC]");
c0e3f241
CD
14165
14166 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 14167 fprintf (file, " [XGOT]");
c0e3f241
CD
14168
14169 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 14170 fprintf (file, " [UCODE]");
c0e3f241 14171
b49e97c9
TS
14172 fputc ('\n', file);
14173
b34976b6 14174 return TRUE;
b49e97c9 14175}
2f89ff8d 14176
b35d266b 14177const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 14178{
0112cd26
NC
14179 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14180 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14181 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
14182 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14183 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14184 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
14185 { NULL, 0, 0, 0, 0 }
2f89ff8d 14186};
5e2b0d47 14187
8992f0d7
TS
14188/* Merge non visibility st_other attributes. Ensure that the
14189 STO_OPTIONAL flag is copied into h->other, even if this is not a
14190 definiton of the symbol. */
5e2b0d47
NC
14191void
14192_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
14193 const Elf_Internal_Sym *isym,
14194 bfd_boolean definition,
14195 bfd_boolean dynamic ATTRIBUTE_UNUSED)
14196{
8992f0d7
TS
14197 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
14198 {
14199 unsigned char other;
14200
14201 other = (definition ? isym->st_other : h->other);
14202 other &= ~ELF_ST_VISIBILITY (-1);
14203 h->other = other | ELF_ST_VISIBILITY (h->other);
14204 }
14205
14206 if (!definition
5e2b0d47
NC
14207 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
14208 h->other |= STO_OPTIONAL;
14209}
12ac1cf5
NC
14210
14211/* Decide whether an undefined symbol is special and can be ignored.
14212 This is the case for OPTIONAL symbols on IRIX. */
14213bfd_boolean
14214_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
14215{
14216 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
14217}
e0764319
NC
14218
14219bfd_boolean
14220_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
14221{
14222 return (sym->st_shndx == SHN_COMMON
14223 || sym->st_shndx == SHN_MIPS_ACOMMON
14224 || sym->st_shndx == SHN_MIPS_SCOMMON);
14225}
861fb55a
DJ
14226
14227/* Return address for Ith PLT stub in section PLT, for relocation REL
14228 or (bfd_vma) -1 if it should not be included. */
14229
14230bfd_vma
14231_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
14232 const arelent *rel ATTRIBUTE_UNUSED)
14233{
14234 return (plt->vma
14235 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
14236 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
14237}
14238
14239void
14240_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
14241{
14242 struct mips_elf_link_hash_table *htab;
14243 Elf_Internal_Ehdr *i_ehdrp;
14244
14245 i_ehdrp = elf_elfheader (abfd);
14246 if (link_info)
14247 {
14248 htab = mips_elf_hash_table (link_info);
4dfe6ac6
NC
14249 BFD_ASSERT (htab != NULL);
14250
861fb55a
DJ
14251 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
14252 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
14253 }
14254}
This page took 2.07711 seconds and 4 git commands to generate.