2010-09-24 Thomas Schwinge <thomas@codesourcery.com>
[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,
4dfe6ac6 3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
b49e97c9
TS
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
ae9a127f 12 This file is part of BFD, the Binary File Descriptor library.
b49e97c9 13
ae9a127f
NC
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
cd123cb7 16 the Free Software Foundation; either version 3 of the License, or
ae9a127f 17 (at your option) any later version.
b49e97c9 18
ae9a127f
NC
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
b49e97c9 23
ae9a127f
NC
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
cd123cb7
NC
26 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
27 MA 02110-1301, USA. */
28
b49e97c9
TS
29
30/* This file handles functionality common to the different MIPS ABI's. */
31
b49e97c9 32#include "sysdep.h"
3db64b00 33#include "bfd.h"
b49e97c9 34#include "libbfd.h"
64543e1a 35#include "libiberty.h"
b49e97c9
TS
36#include "elf-bfd.h"
37#include "elfxx-mips.h"
38#include "elf/mips.h"
0a44bf69 39#include "elf-vxworks.h"
b49e97c9
TS
40
41/* Get the ECOFF swapping routines. */
42#include "coff/sym.h"
43#include "coff/symconst.h"
44#include "coff/ecoff.h"
45#include "coff/mips.h"
46
b15e6682
AO
47#include "hashtab.h"
48
ead49a57
RS
49/* This structure is used to hold information about one GOT entry.
50 There are three types of entry:
51
52 (1) absolute addresses
53 (abfd == NULL)
54 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
55 (abfd != NULL, symndx >= 0)
020d7251 56 (3) SYMBOL addresses, where SYMBOL is not local to an input bfd
ead49a57
RS
57 (abfd != NULL, symndx == -1)
58
59 Type (3) entries are treated differently for different types of GOT.
60 In the "master" GOT -- i.e. the one that describes every GOT
61 reference needed in the link -- the mips_got_entry is keyed on both
62 the symbol and the input bfd that references it. If it turns out
63 that we need multiple GOTs, we can then use this information to
64 create separate GOTs for each input bfd.
65
66 However, we want each of these separate GOTs to have at most one
67 entry for a given symbol, so their type (3) entries are keyed only
68 on the symbol. The input bfd given by the "abfd" field is somewhat
69 arbitrary in this case.
70
71 This means that when there are multiple GOTs, each GOT has a unique
72 mips_got_entry for every symbol within it. We can therefore use the
73 mips_got_entry fields (tls_type and gotidx) to track the symbol's
74 GOT index.
75
76 However, if it turns out that we need only a single GOT, we continue
77 to use the master GOT to describe it. There may therefore be several
78 mips_got_entries for the same symbol, each with a different input bfd.
79 We want to make sure that each symbol gets a unique GOT entry, so when
80 there's a single GOT, we use the symbol's hash entry, not the
81 mips_got_entry fields, to track a symbol's GOT index. */
b15e6682
AO
82struct mips_got_entry
83{
84 /* The input bfd in which the symbol is defined. */
85 bfd *abfd;
f4416af6
AO
86 /* The index of the symbol, as stored in the relocation r_info, if
87 we have a local symbol; -1 otherwise. */
88 long symndx;
89 union
90 {
91 /* If abfd == NULL, an address that must be stored in the got. */
92 bfd_vma address;
93 /* If abfd != NULL && symndx != -1, the addend of the relocation
94 that should be added to the symbol value. */
95 bfd_vma addend;
96 /* If abfd != NULL && symndx == -1, the hash table entry
020d7251
RS
97 corresponding to symbol in the GOT. The symbol's entry
98 is in the local area if h->global_got_area is GGA_NONE,
99 otherwise it is in the global area. */
f4416af6
AO
100 struct mips_elf_link_hash_entry *h;
101 } d;
0f20cc35
DJ
102
103 /* The TLS types included in this GOT entry (specifically, GD and
104 IE). The GD and IE flags can be added as we encounter new
105 relocations. LDM can also be set; it will always be alone, not
106 combined with any GD or IE flags. An LDM GOT entry will be
107 a local symbol entry with r_symndx == 0. */
108 unsigned char tls_type;
109
b15e6682 110 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
111 corresponding to this symbol+addend. If it's a global symbol
112 whose offset is yet to be decided, it's going to be -1. */
113 long gotidx;
b15e6682
AO
114};
115
c224138d
RS
116/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
117 The structures form a non-overlapping list that is sorted by increasing
118 MIN_ADDEND. */
119struct mips_got_page_range
120{
121 struct mips_got_page_range *next;
122 bfd_signed_vma min_addend;
123 bfd_signed_vma max_addend;
124};
125
126/* This structure describes the range of addends that are applied to page
127 relocations against a given symbol. */
128struct mips_got_page_entry
129{
130 /* The input bfd in which the symbol is defined. */
131 bfd *abfd;
132 /* The index of the symbol, as stored in the relocation r_info. */
133 long symndx;
134 /* The ranges for this page entry. */
135 struct mips_got_page_range *ranges;
136 /* The maximum number of page entries needed for RANGES. */
137 bfd_vma num_pages;
138};
139
f0abc2a1 140/* This structure is used to hold .got information when linking. */
b49e97c9
TS
141
142struct mips_got_info
143{
144 /* The global symbol in the GOT with the lowest index in the dynamic
145 symbol table. */
146 struct elf_link_hash_entry *global_gotsym;
147 /* The number of global .got entries. */
148 unsigned int global_gotno;
23cc69b6
RS
149 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
150 unsigned int reloc_only_gotno;
0f20cc35
DJ
151 /* The number of .got slots used for TLS. */
152 unsigned int tls_gotno;
153 /* The first unused TLS .got entry. Used only during
154 mips_elf_initialize_tls_index. */
155 unsigned int tls_assigned_gotno;
c224138d 156 /* The number of local .got entries, eventually including page entries. */
b49e97c9 157 unsigned int local_gotno;
c224138d
RS
158 /* The maximum number of page entries needed. */
159 unsigned int page_gotno;
b49e97c9
TS
160 /* The number of local .got entries we have used. */
161 unsigned int assigned_gotno;
b15e6682
AO
162 /* A hash table holding members of the got. */
163 struct htab *got_entries;
c224138d
RS
164 /* A hash table of mips_got_page_entry structures. */
165 struct htab *got_page_entries;
f4416af6
AO
166 /* A hash table mapping input bfds to other mips_got_info. NULL
167 unless multi-got was necessary. */
168 struct htab *bfd2got;
169 /* In multi-got links, a pointer to the next got (err, rather, most
170 of the time, it points to the previous got). */
171 struct mips_got_info *next;
0f20cc35
DJ
172 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
173 for none, or MINUS_TWO for not yet assigned. This is needed
174 because a single-GOT link may have multiple hash table entries
175 for the LDM. It does not get initialized in multi-GOT mode. */
176 bfd_vma tls_ldm_offset;
f4416af6
AO
177};
178
179/* Map an input bfd to a got in a multi-got link. */
180
91d6fa6a
NC
181struct mips_elf_bfd2got_hash
182{
f4416af6
AO
183 bfd *bfd;
184 struct mips_got_info *g;
185};
186
187/* Structure passed when traversing the bfd2got hash table, used to
188 create and merge bfd's gots. */
189
190struct mips_elf_got_per_bfd_arg
191{
192 /* A hashtable that maps bfds to gots. */
193 htab_t bfd2got;
194 /* The output bfd. */
195 bfd *obfd;
196 /* The link information. */
197 struct bfd_link_info *info;
198 /* A pointer to the primary got, i.e., the one that's going to get
199 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
200 DT_MIPS_GOTSYM. */
201 struct mips_got_info *primary;
202 /* A non-primary got we're trying to merge with other input bfd's
203 gots. */
204 struct mips_got_info *current;
205 /* The maximum number of got entries that can be addressed with a
206 16-bit offset. */
207 unsigned int max_count;
c224138d
RS
208 /* The maximum number of page entries needed by each got. */
209 unsigned int max_pages;
0f20cc35
DJ
210 /* The total number of global entries which will live in the
211 primary got and be automatically relocated. This includes
212 those not referenced by the primary GOT but included in
213 the "master" GOT. */
214 unsigned int global_count;
f4416af6
AO
215};
216
217/* Another structure used to pass arguments for got entries traversal. */
218
219struct mips_elf_set_global_got_offset_arg
220{
221 struct mips_got_info *g;
222 int value;
223 unsigned int needed_relocs;
224 struct bfd_link_info *info;
b49e97c9
TS
225};
226
0f20cc35
DJ
227/* A structure used to count TLS relocations or GOT entries, for GOT
228 entry or ELF symbol table traversal. */
229
230struct mips_elf_count_tls_arg
231{
232 struct bfd_link_info *info;
233 unsigned int needed;
234};
235
f0abc2a1
AM
236struct _mips_elf_section_data
237{
238 struct bfd_elf_section_data elf;
239 union
240 {
f0abc2a1
AM
241 bfd_byte *tdata;
242 } u;
243};
244
245#define mips_elf_section_data(sec) \
68bfbfcc 246 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 247
d5eaccd7
RS
248#define is_mips_elf(bfd) \
249 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
250 && elf_tdata (bfd) != NULL \
4dfe6ac6 251 && elf_object_id (bfd) == MIPS_ELF_DATA)
d5eaccd7 252
634835ae
RS
253/* The ABI says that every symbol used by dynamic relocations must have
254 a global GOT entry. Among other things, this provides the dynamic
255 linker with a free, directly-indexed cache. The GOT can therefore
256 contain symbols that are not referenced by GOT relocations themselves
257 (in other words, it may have symbols that are not referenced by things
258 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
259
260 GOT relocations are less likely to overflow if we put the associated
261 GOT entries towards the beginning. We therefore divide the global
262 GOT entries into two areas: "normal" and "reloc-only". Entries in
263 the first area can be used for both dynamic relocations and GP-relative
264 accesses, while those in the "reloc-only" area are for dynamic
265 relocations only.
266
267 These GGA_* ("Global GOT Area") values are organised so that lower
268 values are more general than higher values. Also, non-GGA_NONE
269 values are ordered by the position of the area in the GOT. */
270#define GGA_NORMAL 0
271#define GGA_RELOC_ONLY 1
272#define GGA_NONE 2
273
861fb55a
DJ
274/* Information about a non-PIC interface to a PIC function. There are
275 two ways of creating these interfaces. The first is to add:
276
277 lui $25,%hi(func)
278 addiu $25,$25,%lo(func)
279
280 immediately before a PIC function "func". The second is to add:
281
282 lui $25,%hi(func)
283 j func
284 addiu $25,$25,%lo(func)
285
286 to a separate trampoline section.
287
288 Stubs of the first kind go in a new section immediately before the
289 target function. Stubs of the second kind go in a single section
290 pointed to by the hash table's "strampoline" field. */
291struct mips_elf_la25_stub {
292 /* The generated section that contains this stub. */
293 asection *stub_section;
294
295 /* The offset of the stub from the start of STUB_SECTION. */
296 bfd_vma offset;
297
298 /* One symbol for the original function. Its location is available
299 in H->root.root.u.def. */
300 struct mips_elf_link_hash_entry *h;
301};
302
303/* Macros for populating a mips_elf_la25_stub. */
304
305#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
306#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
307#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
308
b49e97c9
TS
309/* This structure is passed to mips_elf_sort_hash_table_f when sorting
310 the dynamic symbols. */
311
312struct mips_elf_hash_sort_data
313{
314 /* The symbol in the global GOT with the lowest dynamic symbol table
315 index. */
316 struct elf_link_hash_entry *low;
0f20cc35
DJ
317 /* The least dynamic symbol table index corresponding to a non-TLS
318 symbol with a GOT entry. */
b49e97c9 319 long min_got_dynindx;
f4416af6
AO
320 /* The greatest dynamic symbol table index corresponding to a symbol
321 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 322 with dynamic relocations pointing to it from non-primary GOTs). */
f4416af6 323 long max_unref_got_dynindx;
b49e97c9
TS
324 /* The greatest dynamic symbol table index not corresponding to a
325 symbol without a GOT entry. */
326 long max_non_got_dynindx;
327};
328
329/* The MIPS ELF linker needs additional information for each symbol in
330 the global hash table. */
331
332struct mips_elf_link_hash_entry
333{
334 struct elf_link_hash_entry root;
335
336 /* External symbol information. */
337 EXTR esym;
338
861fb55a
DJ
339 /* The la25 stub we have created for ths symbol, if any. */
340 struct mips_elf_la25_stub *la25_stub;
341
b49e97c9
TS
342 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
343 this symbol. */
344 unsigned int possibly_dynamic_relocs;
345
b49e97c9
TS
346 /* If there is a stub that 32 bit functions should use to call this
347 16 bit function, this points to the section containing the stub. */
348 asection *fn_stub;
349
b49e97c9
TS
350 /* If there is a stub that 16 bit functions should use to call this
351 32 bit function, this points to the section containing the stub. */
352 asection *call_stub;
353
354 /* This is like the call_stub field, but it is used if the function
355 being called returns a floating point value. */
356 asection *call_fp_stub;
7c5fcef7 357
0f20cc35
DJ
358#define GOT_NORMAL 0
359#define GOT_TLS_GD 1
360#define GOT_TLS_LDM 2
361#define GOT_TLS_IE 4
362#define GOT_TLS_OFFSET_DONE 0x40
363#define GOT_TLS_DONE 0x80
364 unsigned char tls_type;
71782a75 365
0f20cc35
DJ
366 /* This is only used in single-GOT mode; in multi-GOT mode there
367 is one mips_got_entry per GOT entry, so the offset is stored
368 there. In single-GOT mode there may be many mips_got_entry
369 structures all referring to the same GOT slot. It might be
370 possible to use root.got.offset instead, but that field is
371 overloaded already. */
372 bfd_vma tls_got_offset;
71782a75 373
634835ae
RS
374 /* The highest GGA_* value that satisfies all references to this symbol. */
375 unsigned int global_got_area : 2;
376
6ccf4795
RS
377 /* True if all GOT relocations against this symbol are for calls. This is
378 a looser condition than no_fn_stub below, because there may be other
379 non-call non-GOT relocations against the symbol. */
380 unsigned int got_only_for_calls : 1;
381
71782a75
RS
382 /* True if one of the relocations described by possibly_dynamic_relocs
383 is against a readonly section. */
384 unsigned int readonly_reloc : 1;
385
861fb55a
DJ
386 /* True if there is a relocation against this symbol that must be
387 resolved by the static linker (in other words, if the relocation
388 cannot possibly be made dynamic). */
389 unsigned int has_static_relocs : 1;
390
71782a75
RS
391 /* True if we must not create a .MIPS.stubs entry for this symbol.
392 This is set, for example, if there are relocations related to
393 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
394 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
395 unsigned int no_fn_stub : 1;
396
397 /* Whether we need the fn_stub; this is true if this symbol appears
398 in any relocs other than a 16 bit call. */
399 unsigned int need_fn_stub : 1;
400
861fb55a
DJ
401 /* True if this symbol is referenced by branch relocations from
402 any non-PIC input file. This is used to determine whether an
403 la25 stub is required. */
404 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
405
406 /* Does this symbol need a traditional MIPS lazy-binding stub
407 (as opposed to a PLT entry)? */
408 unsigned int needs_lazy_stub : 1;
b49e97c9
TS
409};
410
411/* MIPS ELF linker hash table. */
412
413struct mips_elf_link_hash_table
414{
415 struct elf_link_hash_table root;
416#if 0
417 /* We no longer use this. */
418 /* String section indices for the dynamic section symbols. */
419 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
420#endif
861fb55a 421
b49e97c9
TS
422 /* The number of .rtproc entries. */
423 bfd_size_type procedure_count;
861fb55a 424
b49e97c9
TS
425 /* The size of the .compact_rel section (if SGI_COMPAT). */
426 bfd_size_type compact_rel_size;
861fb55a 427
b49e97c9 428 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
8dc1a139 429 entry is set to the address of __rld_obj_head as in IRIX5. */
b34976b6 430 bfd_boolean use_rld_obj_head;
861fb55a 431
b49e97c9
TS
432 /* This is the value of the __rld_map or __rld_obj_head symbol. */
433 bfd_vma rld_value;
861fb55a 434
b49e97c9 435 /* This is set if we see any mips16 stub sections. */
b34976b6 436 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
437
438 /* True if we can generate copy relocs and PLTs. */
439 bfd_boolean use_plts_and_copy_relocs;
440
0a44bf69
RS
441 /* True if we're generating code for VxWorks. */
442 bfd_boolean is_vxworks;
861fb55a 443
0e53d9da
AN
444 /* True if we already reported the small-data section overflow. */
445 bfd_boolean small_data_overflow_reported;
861fb55a 446
0a44bf69
RS
447 /* Shortcuts to some dynamic sections, or NULL if they are not
448 being used. */
449 asection *srelbss;
450 asection *sdynbss;
451 asection *srelplt;
452 asection *srelplt2;
453 asection *sgotplt;
454 asection *splt;
4e41d0d7 455 asection *sstubs;
a8028dd0 456 asection *sgot;
861fb55a 457
a8028dd0
RS
458 /* The master GOT information. */
459 struct mips_got_info *got_info;
861fb55a
DJ
460
461 /* The size of the PLT header in bytes. */
0a44bf69 462 bfd_vma plt_header_size;
861fb55a
DJ
463
464 /* The size of a PLT entry in bytes. */
0a44bf69 465 bfd_vma plt_entry_size;
861fb55a 466
33bb52fb
RS
467 /* The number of functions that need a lazy-binding stub. */
468 bfd_vma lazy_stub_count;
861fb55a 469
5108fc1b
RS
470 /* The size of a function stub entry in bytes. */
471 bfd_vma function_stub_size;
861fb55a
DJ
472
473 /* The number of reserved entries at the beginning of the GOT. */
474 unsigned int reserved_gotno;
475
476 /* The section used for mips_elf_la25_stub trampolines.
477 See the comment above that structure for details. */
478 asection *strampoline;
479
480 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
481 pairs. */
482 htab_t la25_stubs;
483
484 /* A function FN (NAME, IS, OS) that creates a new input section
485 called NAME and links it to output section OS. If IS is nonnull,
486 the new section should go immediately before it, otherwise it
487 should go at the (current) beginning of OS.
488
489 The function returns the new section on success, otherwise it
490 returns null. */
491 asection *(*add_stub_section) (const char *, asection *, asection *);
492};
493
4dfe6ac6
NC
494/* Get the MIPS ELF linker hash table from a link_info structure. */
495
496#define mips_elf_hash_table(p) \
497 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
498 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
499
861fb55a 500/* A structure used to communicate with htab_traverse callbacks. */
4dfe6ac6
NC
501struct mips_htab_traverse_info
502{
861fb55a
DJ
503 /* The usual link-wide information. */
504 struct bfd_link_info *info;
505 bfd *output_bfd;
506
507 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
508 bfd_boolean error;
b49e97c9
TS
509};
510
0f20cc35
DJ
511#define TLS_RELOC_P(r_type) \
512 (r_type == R_MIPS_TLS_DTPMOD32 \
513 || r_type == R_MIPS_TLS_DTPMOD64 \
514 || r_type == R_MIPS_TLS_DTPREL32 \
515 || r_type == R_MIPS_TLS_DTPREL64 \
516 || r_type == R_MIPS_TLS_GD \
517 || r_type == R_MIPS_TLS_LDM \
518 || r_type == R_MIPS_TLS_DTPREL_HI16 \
519 || r_type == R_MIPS_TLS_DTPREL_LO16 \
520 || r_type == R_MIPS_TLS_GOTTPREL \
521 || r_type == R_MIPS_TLS_TPREL32 \
522 || r_type == R_MIPS_TLS_TPREL64 \
523 || r_type == R_MIPS_TLS_TPREL_HI16 \
524 || r_type == R_MIPS_TLS_TPREL_LO16)
525
b49e97c9
TS
526/* Structure used to pass information to mips_elf_output_extsym. */
527
528struct extsym_info
529{
9e4aeb93
RS
530 bfd *abfd;
531 struct bfd_link_info *info;
b49e97c9
TS
532 struct ecoff_debug_info *debug;
533 const struct ecoff_debug_swap *swap;
b34976b6 534 bfd_boolean failed;
b49e97c9
TS
535};
536
8dc1a139 537/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
538
539static const char * const mips_elf_dynsym_rtproc_names[] =
540{
541 "_procedure_table",
542 "_procedure_string_table",
543 "_procedure_table_size",
544 NULL
545};
546
547/* These structures are used to generate the .compact_rel section on
8dc1a139 548 IRIX5. */
b49e97c9
TS
549
550typedef struct
551{
552 unsigned long id1; /* Always one? */
553 unsigned long num; /* Number of compact relocation entries. */
554 unsigned long id2; /* Always two? */
555 unsigned long offset; /* The file offset of the first relocation. */
556 unsigned long reserved0; /* Zero? */
557 unsigned long reserved1; /* Zero? */
558} Elf32_compact_rel;
559
560typedef struct
561{
562 bfd_byte id1[4];
563 bfd_byte num[4];
564 bfd_byte id2[4];
565 bfd_byte offset[4];
566 bfd_byte reserved0[4];
567 bfd_byte reserved1[4];
568} Elf32_External_compact_rel;
569
570typedef struct
571{
572 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
573 unsigned int rtype : 4; /* Relocation types. See below. */
574 unsigned int dist2to : 8;
575 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
576 unsigned long konst; /* KONST field. See below. */
577 unsigned long vaddr; /* VADDR to be relocated. */
578} Elf32_crinfo;
579
580typedef struct
581{
582 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
583 unsigned int rtype : 4; /* Relocation types. See below. */
584 unsigned int dist2to : 8;
585 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
586 unsigned long konst; /* KONST field. See below. */
587} Elf32_crinfo2;
588
589typedef struct
590{
591 bfd_byte info[4];
592 bfd_byte konst[4];
593 bfd_byte vaddr[4];
594} Elf32_External_crinfo;
595
596typedef struct
597{
598 bfd_byte info[4];
599 bfd_byte konst[4];
600} Elf32_External_crinfo2;
601
602/* These are the constants used to swap the bitfields in a crinfo. */
603
604#define CRINFO_CTYPE (0x1)
605#define CRINFO_CTYPE_SH (31)
606#define CRINFO_RTYPE (0xf)
607#define CRINFO_RTYPE_SH (27)
608#define CRINFO_DIST2TO (0xff)
609#define CRINFO_DIST2TO_SH (19)
610#define CRINFO_RELVADDR (0x7ffff)
611#define CRINFO_RELVADDR_SH (0)
612
613/* A compact relocation info has long (3 words) or short (2 words)
614 formats. A short format doesn't have VADDR field and relvaddr
615 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
616#define CRF_MIPS_LONG 1
617#define CRF_MIPS_SHORT 0
618
619/* There are 4 types of compact relocation at least. The value KONST
620 has different meaning for each type:
621
622 (type) (konst)
623 CT_MIPS_REL32 Address in data
624 CT_MIPS_WORD Address in word (XXX)
625 CT_MIPS_GPHI_LO GP - vaddr
626 CT_MIPS_JMPAD Address to jump
627 */
628
629#define CRT_MIPS_REL32 0xa
630#define CRT_MIPS_WORD 0xb
631#define CRT_MIPS_GPHI_LO 0xc
632#define CRT_MIPS_JMPAD 0xd
633
634#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
635#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
636#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
637#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
638\f
639/* The structure of the runtime procedure descriptor created by the
640 loader for use by the static exception system. */
641
642typedef struct runtime_pdr {
ae9a127f
NC
643 bfd_vma adr; /* Memory address of start of procedure. */
644 long regmask; /* Save register mask. */
645 long regoffset; /* Save register offset. */
646 long fregmask; /* Save floating point register mask. */
647 long fregoffset; /* Save floating point register offset. */
648 long frameoffset; /* Frame size. */
649 short framereg; /* Frame pointer register. */
650 short pcreg; /* Offset or reg of return pc. */
651 long irpss; /* Index into the runtime string table. */
b49e97c9 652 long reserved;
ae9a127f 653 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
654} RPDR, *pRPDR;
655#define cbRPDR sizeof (RPDR)
656#define rpdNil ((pRPDR) 0)
657\f
b15e6682 658static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
659 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
660 struct mips_elf_link_hash_entry *, int);
b34976b6 661static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 662 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
663static bfd_vma mips_elf_high
664 (bfd_vma);
b34976b6 665static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
666 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
667 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
668 bfd_vma *, asection *);
9719ad41
RS
669static hashval_t mips_elf_got_entry_hash
670 (const void *);
f4416af6 671static bfd_vma mips_elf_adjust_gp
9719ad41 672 (bfd *, struct mips_got_info *, bfd *);
f4416af6 673static struct mips_got_info *mips_elf_got_for_ibfd
9719ad41 674 (struct mips_got_info *, bfd *);
f4416af6 675
b49e97c9
TS
676/* This will be used when we sort the dynamic relocation records. */
677static bfd *reldyn_sorting_bfd;
678
6d30f5b2
NC
679/* True if ABFD is for CPUs with load interlocking that include
680 non-MIPS1 CPUs and R3900. */
681#define LOAD_INTERLOCKS_P(abfd) \
682 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
683 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
684
cd8d5a82
CF
685/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
686 This should be safe for all architectures. We enable this predicate
687 for RM9000 for now. */
688#define JAL_TO_BAL_P(abfd) \
689 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
690
691/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
692 This should be safe for all architectures. We enable this predicate for
693 all CPUs. */
694#define JALR_TO_BAL_P(abfd) 1
695
38a7df63
CF
696/* True if ABFD is for CPUs that are faster if JR is converted to B.
697 This should be safe for all architectures. We enable this predicate for
698 all CPUs. */
699#define JR_TO_B_P(abfd) 1
700
861fb55a
DJ
701/* True if ABFD is a PIC object. */
702#define PIC_OBJECT_P(abfd) \
703 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
704
b49e97c9 705/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
706#define ABI_N32_P(abfd) \
707 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
708
4a14403c 709/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 710#define ABI_64_P(abfd) \
141ff970 711 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 712
4a14403c
TS
713/* Nonzero if ABFD is using NewABI conventions. */
714#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
715
716/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
717#define IRIX_COMPAT(abfd) \
718 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
719
b49e97c9
TS
720/* Whether we are trying to be compatible with IRIX at all. */
721#define SGI_COMPAT(abfd) \
722 (IRIX_COMPAT (abfd) != ict_none)
723
724/* The name of the options section. */
725#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 726 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 727
cc2e31b9
RS
728/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
729 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
730#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
731 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
732
943284cc
DJ
733/* Whether the section is readonly. */
734#define MIPS_ELF_READONLY_SECTION(sec) \
735 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
736 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
737
b49e97c9 738/* The name of the stub section. */
ca07892d 739#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
740
741/* The size of an external REL relocation. */
742#define MIPS_ELF_REL_SIZE(abfd) \
743 (get_elf_backend_data (abfd)->s->sizeof_rel)
744
0a44bf69
RS
745/* The size of an external RELA relocation. */
746#define MIPS_ELF_RELA_SIZE(abfd) \
747 (get_elf_backend_data (abfd)->s->sizeof_rela)
748
b49e97c9
TS
749/* The size of an external dynamic table entry. */
750#define MIPS_ELF_DYN_SIZE(abfd) \
751 (get_elf_backend_data (abfd)->s->sizeof_dyn)
752
753/* The size of a GOT entry. */
754#define MIPS_ELF_GOT_SIZE(abfd) \
755 (get_elf_backend_data (abfd)->s->arch_size / 8)
756
757/* The size of a symbol-table entry. */
758#define MIPS_ELF_SYM_SIZE(abfd) \
759 (get_elf_backend_data (abfd)->s->sizeof_sym)
760
761/* The default alignment for sections, as a power of two. */
762#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 763 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
764
765/* Get word-sized data. */
766#define MIPS_ELF_GET_WORD(abfd, ptr) \
767 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
768
769/* Put out word-sized data. */
770#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
771 (ABI_64_P (abfd) \
772 ? bfd_put_64 (abfd, val, ptr) \
773 : bfd_put_32 (abfd, val, ptr))
774
861fb55a
DJ
775/* The opcode for word-sized loads (LW or LD). */
776#define MIPS_ELF_LOAD_WORD(abfd) \
777 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
778
b49e97c9 779/* Add a dynamic symbol table-entry. */
9719ad41 780#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 781 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
782
783#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
784 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
785
4ffba85c
AO
786/* Determine whether the internal relocation of index REL_IDX is REL
787 (zero) or RELA (non-zero). The assumption is that, if there are
788 two relocation sections for this section, one of them is REL and
789 the other is RELA. If the index of the relocation we're testing is
790 in range for the first relocation section, check that the external
791 relocation size is that for RELA. It is also assumed that, if
792 rel_idx is not in range for the first section, and this first
793 section contains REL relocs, then the relocation is in the second
794 section, that is RELA. */
795#define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
796 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
797 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
798 > (bfd_vma)(rel_idx)) \
799 == (elf_section_data (sec)->rel_hdr.sh_entsize \
800 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
801 : sizeof (Elf32_External_Rela))))
802
0a44bf69
RS
803/* The name of the dynamic relocation section. */
804#define MIPS_ELF_REL_DYN_NAME(INFO) \
805 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
806
b49e97c9
TS
807/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
808 from smaller values. Start with zero, widen, *then* decrement. */
809#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 810#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 811
51e38d68
RS
812/* The value to write into got[1] for SVR4 targets, to identify it is
813 a GNU object. The dynamic linker can then use got[1] to store the
814 module pointer. */
815#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
816 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
817
f4416af6 818/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
819#define ELF_MIPS_GP_OFFSET(INFO) \
820 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
821
822/* The maximum size of the GOT for it to be addressable using 16-bit
823 offsets from $gp. */
0a44bf69 824#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 825
6a691779 826/* Instructions which appear in a stub. */
3d6746ca
DD
827#define STUB_LW(abfd) \
828 ((ABI_64_P (abfd) \
829 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
830 : 0x8f998010)) /* lw t9,0x8010(gp) */
831#define STUB_MOVE(abfd) \
832 ((ABI_64_P (abfd) \
833 ? 0x03e0782d /* daddu t7,ra */ \
834 : 0x03e07821)) /* addu t7,ra */
835#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
836#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
837#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
838#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
839#define STUB_LI16S(abfd, VAL) \
840 ((ABI_64_P (abfd) \
841 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
842 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
843
5108fc1b
RS
844#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
845#define MIPS_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
846
847/* The name of the dynamic interpreter. This is put in the .interp
848 section. */
849
850#define ELF_DYNAMIC_INTERPRETER(abfd) \
851 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
852 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
853 : "/usr/lib/libc.so.1")
854
855#ifdef BFD64
ee6423ed
AO
856#define MNAME(bfd,pre,pos) \
857 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
858#define ELF_R_SYM(bfd, i) \
859 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
860#define ELF_R_TYPE(bfd, i) \
861 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
862#define ELF_R_INFO(bfd, s, t) \
863 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
864#else
ee6423ed 865#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
866#define ELF_R_SYM(bfd, i) \
867 (ELF32_R_SYM (i))
868#define ELF_R_TYPE(bfd, i) \
869 (ELF32_R_TYPE (i))
870#define ELF_R_INFO(bfd, s, t) \
871 (ELF32_R_INFO (s, t))
872#endif
873\f
874 /* The mips16 compiler uses a couple of special sections to handle
875 floating point arguments.
876
877 Section names that look like .mips16.fn.FNNAME contain stubs that
878 copy floating point arguments from the fp regs to the gp regs and
879 then jump to FNNAME. If any 32 bit function calls FNNAME, the
880 call should be redirected to the stub instead. If no 32 bit
881 function calls FNNAME, the stub should be discarded. We need to
882 consider any reference to the function, not just a call, because
883 if the address of the function is taken we will need the stub,
884 since the address might be passed to a 32 bit function.
885
886 Section names that look like .mips16.call.FNNAME contain stubs
887 that copy floating point arguments from the gp regs to the fp
888 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
889 then any 16 bit function that calls FNNAME should be redirected
890 to the stub instead. If FNNAME is not a 32 bit function, the
891 stub should be discarded.
892
893 .mips16.call.fp.FNNAME sections are similar, but contain stubs
894 which call FNNAME and then copy the return value from the fp regs
895 to the gp regs. These stubs store the return value in $18 while
896 calling FNNAME; any function which might call one of these stubs
897 must arrange to save $18 around the call. (This case is not
898 needed for 32 bit functions that call 16 bit functions, because
899 16 bit functions always return floating point values in both
900 $f0/$f1 and $2/$3.)
901
902 Note that in all cases FNNAME might be defined statically.
903 Therefore, FNNAME is not used literally. Instead, the relocation
904 information will indicate which symbol the section is for.
905
906 We record any stubs that we find in the symbol table. */
907
908#define FN_STUB ".mips16.fn."
909#define CALL_STUB ".mips16.call."
910#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
911
912#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
913#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
914#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 915\f
861fb55a 916/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
917static const bfd_vma mips_o32_exec_plt0_entry[] =
918{
861fb55a
DJ
919 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
920 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
921 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
922 0x031cc023, /* subu $24, $24, $28 */
923 0x03e07821, /* move $15, $31 */
924 0x0018c082, /* srl $24, $24, 2 */
925 0x0320f809, /* jalr $25 */
926 0x2718fffe /* subu $24, $24, 2 */
927};
928
929/* The format of the first PLT entry in an N32 executable. Different
930 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
931static const bfd_vma mips_n32_exec_plt0_entry[] =
932{
861fb55a
DJ
933 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
934 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
935 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
936 0x030ec023, /* subu $24, $24, $14 */
937 0x03e07821, /* move $15, $31 */
938 0x0018c082, /* srl $24, $24, 2 */
939 0x0320f809, /* jalr $25 */
940 0x2718fffe /* subu $24, $24, 2 */
941};
942
943/* The format of the first PLT entry in an N64 executable. Different
944 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
945static const bfd_vma mips_n64_exec_plt0_entry[] =
946{
861fb55a
DJ
947 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
948 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
949 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
950 0x030ec023, /* subu $24, $24, $14 */
951 0x03e07821, /* move $15, $31 */
952 0x0018c0c2, /* srl $24, $24, 3 */
953 0x0320f809, /* jalr $25 */
954 0x2718fffe /* subu $24, $24, 2 */
955};
956
957/* The format of subsequent PLT entries. */
6d30f5b2
NC
958static const bfd_vma mips_exec_plt_entry[] =
959{
861fb55a
DJ
960 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
961 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
962 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
963 0x03200008 /* jr $25 */
964};
965
0a44bf69 966/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
967static const bfd_vma mips_vxworks_exec_plt0_entry[] =
968{
0a44bf69
RS
969 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
970 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
971 0x8f390008, /* lw t9, 8(t9) */
972 0x00000000, /* nop */
973 0x03200008, /* jr t9 */
974 0x00000000 /* nop */
975};
976
977/* The format of subsequent PLT entries. */
6d30f5b2
NC
978static const bfd_vma mips_vxworks_exec_plt_entry[] =
979{
0a44bf69
RS
980 0x10000000, /* b .PLT_resolver */
981 0x24180000, /* li t8, <pltindex> */
982 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
983 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
984 0x8f390000, /* lw t9, 0(t9) */
985 0x00000000, /* nop */
986 0x03200008, /* jr t9 */
987 0x00000000 /* nop */
988};
989
990/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
991static const bfd_vma mips_vxworks_shared_plt0_entry[] =
992{
0a44bf69
RS
993 0x8f990008, /* lw t9, 8(gp) */
994 0x00000000, /* nop */
995 0x03200008, /* jr t9 */
996 0x00000000, /* nop */
997 0x00000000, /* nop */
998 0x00000000 /* nop */
999};
1000
1001/* The format of subsequent PLT entries. */
6d30f5b2
NC
1002static const bfd_vma mips_vxworks_shared_plt_entry[] =
1003{
0a44bf69
RS
1004 0x10000000, /* b .PLT_resolver */
1005 0x24180000 /* li t8, <pltindex> */
1006};
1007\f
b49e97c9
TS
1008/* Look up an entry in a MIPS ELF linker hash table. */
1009
1010#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1011 ((struct mips_elf_link_hash_entry *) \
1012 elf_link_hash_lookup (&(table)->root, (string), (create), \
1013 (copy), (follow)))
1014
1015/* Traverse a MIPS ELF linker hash table. */
1016
1017#define mips_elf_link_hash_traverse(table, func, info) \
1018 (elf_link_hash_traverse \
1019 (&(table)->root, \
9719ad41 1020 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1021 (info)))
1022
0f20cc35
DJ
1023/* Find the base offsets for thread-local storage in this object,
1024 for GD/LD and IE/LE respectively. */
1025
1026#define TP_OFFSET 0x7000
1027#define DTP_OFFSET 0x8000
1028
1029static bfd_vma
1030dtprel_base (struct bfd_link_info *info)
1031{
1032 /* If tls_sec is NULL, we should have signalled an error already. */
1033 if (elf_hash_table (info)->tls_sec == NULL)
1034 return 0;
1035 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1036}
1037
1038static bfd_vma
1039tprel_base (struct bfd_link_info *info)
1040{
1041 /* If tls_sec is NULL, we should have signalled an error already. */
1042 if (elf_hash_table (info)->tls_sec == NULL)
1043 return 0;
1044 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1045}
1046
b49e97c9
TS
1047/* Create an entry in a MIPS ELF linker hash table. */
1048
1049static struct bfd_hash_entry *
9719ad41
RS
1050mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1051 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1052{
1053 struct mips_elf_link_hash_entry *ret =
1054 (struct mips_elf_link_hash_entry *) entry;
1055
1056 /* Allocate the structure if it has not already been allocated by a
1057 subclass. */
9719ad41
RS
1058 if (ret == NULL)
1059 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1060 if (ret == NULL)
b49e97c9
TS
1061 return (struct bfd_hash_entry *) ret;
1062
1063 /* Call the allocation method of the superclass. */
1064 ret = ((struct mips_elf_link_hash_entry *)
1065 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1066 table, string));
9719ad41 1067 if (ret != NULL)
b49e97c9
TS
1068 {
1069 /* Set local fields. */
1070 memset (&ret->esym, 0, sizeof (EXTR));
1071 /* We use -2 as a marker to indicate that the information has
1072 not been set. -1 means there is no associated ifd. */
1073 ret->esym.ifd = -2;
861fb55a 1074 ret->la25_stub = 0;
b49e97c9 1075 ret->possibly_dynamic_relocs = 0;
b49e97c9 1076 ret->fn_stub = NULL;
b49e97c9
TS
1077 ret->call_stub = NULL;
1078 ret->call_fp_stub = NULL;
71782a75 1079 ret->tls_type = GOT_NORMAL;
634835ae 1080 ret->global_got_area = GGA_NONE;
6ccf4795 1081 ret->got_only_for_calls = TRUE;
71782a75 1082 ret->readonly_reloc = FALSE;
861fb55a 1083 ret->has_static_relocs = FALSE;
71782a75
RS
1084 ret->no_fn_stub = FALSE;
1085 ret->need_fn_stub = FALSE;
861fb55a 1086 ret->has_nonpic_branches = FALSE;
33bb52fb 1087 ret->needs_lazy_stub = FALSE;
b49e97c9
TS
1088 }
1089
1090 return (struct bfd_hash_entry *) ret;
1091}
f0abc2a1
AM
1092
1093bfd_boolean
9719ad41 1094_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1095{
f592407e
AM
1096 if (!sec->used_by_bfd)
1097 {
1098 struct _mips_elf_section_data *sdata;
1099 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1100
f592407e
AM
1101 sdata = bfd_zalloc (abfd, amt);
1102 if (sdata == NULL)
1103 return FALSE;
1104 sec->used_by_bfd = sdata;
1105 }
f0abc2a1
AM
1106
1107 return _bfd_elf_new_section_hook (abfd, sec);
1108}
b49e97c9
TS
1109\f
1110/* Read ECOFF debugging information from a .mdebug section into a
1111 ecoff_debug_info structure. */
1112
b34976b6 1113bfd_boolean
9719ad41
RS
1114_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1115 struct ecoff_debug_info *debug)
b49e97c9
TS
1116{
1117 HDRR *symhdr;
1118 const struct ecoff_debug_swap *swap;
9719ad41 1119 char *ext_hdr;
b49e97c9
TS
1120
1121 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1122 memset (debug, 0, sizeof (*debug));
1123
9719ad41 1124 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1125 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1126 goto error_return;
1127
9719ad41 1128 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1129 swap->external_hdr_size))
b49e97c9
TS
1130 goto error_return;
1131
1132 symhdr = &debug->symbolic_header;
1133 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1134
1135 /* The symbolic header contains absolute file offsets and sizes to
1136 read. */
1137#define READ(ptr, offset, count, size, type) \
1138 if (symhdr->count == 0) \
1139 debug->ptr = NULL; \
1140 else \
1141 { \
1142 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1143 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1144 if (debug->ptr == NULL) \
1145 goto error_return; \
9719ad41 1146 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1147 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1148 goto error_return; \
1149 }
1150
1151 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1152 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1153 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1154 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1155 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1156 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1157 union aux_ext *);
1158 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1159 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1160 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1161 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1162 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1163#undef READ
1164
1165 debug->fdr = NULL;
b49e97c9 1166
b34976b6 1167 return TRUE;
b49e97c9
TS
1168
1169 error_return:
1170 if (ext_hdr != NULL)
1171 free (ext_hdr);
1172 if (debug->line != NULL)
1173 free (debug->line);
1174 if (debug->external_dnr != NULL)
1175 free (debug->external_dnr);
1176 if (debug->external_pdr != NULL)
1177 free (debug->external_pdr);
1178 if (debug->external_sym != NULL)
1179 free (debug->external_sym);
1180 if (debug->external_opt != NULL)
1181 free (debug->external_opt);
1182 if (debug->external_aux != NULL)
1183 free (debug->external_aux);
1184 if (debug->ss != NULL)
1185 free (debug->ss);
1186 if (debug->ssext != NULL)
1187 free (debug->ssext);
1188 if (debug->external_fdr != NULL)
1189 free (debug->external_fdr);
1190 if (debug->external_rfd != NULL)
1191 free (debug->external_rfd);
1192 if (debug->external_ext != NULL)
1193 free (debug->external_ext);
b34976b6 1194 return FALSE;
b49e97c9
TS
1195}
1196\f
1197/* Swap RPDR (runtime procedure table entry) for output. */
1198
1199static void
9719ad41 1200ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1201{
1202 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1203 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1204 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1205 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1206 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1207 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1208
1209 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1210 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1211
1212 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1213}
1214
1215/* Create a runtime procedure table from the .mdebug section. */
1216
b34976b6 1217static bfd_boolean
9719ad41
RS
1218mips_elf_create_procedure_table (void *handle, bfd *abfd,
1219 struct bfd_link_info *info, asection *s,
1220 struct ecoff_debug_info *debug)
b49e97c9
TS
1221{
1222 const struct ecoff_debug_swap *swap;
1223 HDRR *hdr = &debug->symbolic_header;
1224 RPDR *rpdr, *rp;
1225 struct rpdr_ext *erp;
9719ad41 1226 void *rtproc;
b49e97c9
TS
1227 struct pdr_ext *epdr;
1228 struct sym_ext *esym;
1229 char *ss, **sv;
1230 char *str;
1231 bfd_size_type size;
1232 bfd_size_type count;
1233 unsigned long sindex;
1234 unsigned long i;
1235 PDR pdr;
1236 SYMR sym;
1237 const char *no_name_func = _("static procedure (no name)");
1238
1239 epdr = NULL;
1240 rpdr = NULL;
1241 esym = NULL;
1242 ss = NULL;
1243 sv = NULL;
1244
1245 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1246
1247 sindex = strlen (no_name_func) + 1;
1248 count = hdr->ipdMax;
1249 if (count > 0)
1250 {
1251 size = swap->external_pdr_size;
1252
9719ad41 1253 epdr = bfd_malloc (size * count);
b49e97c9
TS
1254 if (epdr == NULL)
1255 goto error_return;
1256
9719ad41 1257 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1258 goto error_return;
1259
1260 size = sizeof (RPDR);
9719ad41 1261 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1262 if (rpdr == NULL)
1263 goto error_return;
1264
1265 size = sizeof (char *);
9719ad41 1266 sv = bfd_malloc (size * count);
b49e97c9
TS
1267 if (sv == NULL)
1268 goto error_return;
1269
1270 count = hdr->isymMax;
1271 size = swap->external_sym_size;
9719ad41 1272 esym = bfd_malloc (size * count);
b49e97c9
TS
1273 if (esym == NULL)
1274 goto error_return;
1275
9719ad41 1276 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1277 goto error_return;
1278
1279 count = hdr->issMax;
9719ad41 1280 ss = bfd_malloc (count);
b49e97c9
TS
1281 if (ss == NULL)
1282 goto error_return;
f075ee0c 1283 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1284 goto error_return;
1285
1286 count = hdr->ipdMax;
1287 for (i = 0; i < (unsigned long) count; i++, rp++)
1288 {
9719ad41
RS
1289 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1290 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1291 rp->adr = sym.value;
1292 rp->regmask = pdr.regmask;
1293 rp->regoffset = pdr.regoffset;
1294 rp->fregmask = pdr.fregmask;
1295 rp->fregoffset = pdr.fregoffset;
1296 rp->frameoffset = pdr.frameoffset;
1297 rp->framereg = pdr.framereg;
1298 rp->pcreg = pdr.pcreg;
1299 rp->irpss = sindex;
1300 sv[i] = ss + sym.iss;
1301 sindex += strlen (sv[i]) + 1;
1302 }
1303 }
1304
1305 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1306 size = BFD_ALIGN (size, 16);
9719ad41 1307 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1308 if (rtproc == NULL)
1309 {
1310 mips_elf_hash_table (info)->procedure_count = 0;
1311 goto error_return;
1312 }
1313
1314 mips_elf_hash_table (info)->procedure_count = count + 2;
1315
9719ad41 1316 erp = rtproc;
b49e97c9
TS
1317 memset (erp, 0, sizeof (struct rpdr_ext));
1318 erp++;
1319 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1320 strcpy (str, no_name_func);
1321 str += strlen (no_name_func) + 1;
1322 for (i = 0; i < count; i++)
1323 {
1324 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1325 strcpy (str, sv[i]);
1326 str += strlen (sv[i]) + 1;
1327 }
1328 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1329
1330 /* Set the size and contents of .rtproc section. */
eea6121a 1331 s->size = size;
9719ad41 1332 s->contents = rtproc;
b49e97c9
TS
1333
1334 /* Skip this section later on (I don't think this currently
1335 matters, but someday it might). */
8423293d 1336 s->map_head.link_order = NULL;
b49e97c9
TS
1337
1338 if (epdr != NULL)
1339 free (epdr);
1340 if (rpdr != NULL)
1341 free (rpdr);
1342 if (esym != NULL)
1343 free (esym);
1344 if (ss != NULL)
1345 free (ss);
1346 if (sv != NULL)
1347 free (sv);
1348
b34976b6 1349 return TRUE;
b49e97c9
TS
1350
1351 error_return:
1352 if (epdr != NULL)
1353 free (epdr);
1354 if (rpdr != NULL)
1355 free (rpdr);
1356 if (esym != NULL)
1357 free (esym);
1358 if (ss != NULL)
1359 free (ss);
1360 if (sv != NULL)
1361 free (sv);
b34976b6 1362 return FALSE;
b49e97c9 1363}
738e5348 1364\f
861fb55a
DJ
1365/* We're going to create a stub for H. Create a symbol for the stub's
1366 value and size, to help make the disassembly easier to read. */
1367
1368static bfd_boolean
1369mips_elf_create_stub_symbol (struct bfd_link_info *info,
1370 struct mips_elf_link_hash_entry *h,
1371 const char *prefix, asection *s, bfd_vma value,
1372 bfd_vma size)
1373{
1374 struct bfd_link_hash_entry *bh;
1375 struct elf_link_hash_entry *elfh;
1376 const char *name;
1377
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;
1738 if (h->root.root.type == bfd_link_hash_warning)
1739 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1740
1741 if (!hti->info->relocatable)
1742 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1743
861fb55a
DJ
1744 if (mips_elf_local_pic_function_p (h))
1745 {
1746 /* H is a function that might need $25 to be valid on entry.
1747 If we're creating a non-PIC relocatable object, mark H as
1748 being PIC. If we're creating a non-relocatable object with
1749 non-PIC branches and jumps to H, make sure that H has an la25
1750 stub. */
1751 if (hti->info->relocatable)
1752 {
1753 if (!PIC_OBJECT_P (hti->output_bfd))
1754 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1755 }
1756 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1757 {
1758 hti->error = TRUE;
1759 return FALSE;
1760 }
1761 }
b34976b6 1762 return TRUE;
b49e97c9
TS
1763}
1764\f
d6f16593
MR
1765/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1766 Most mips16 instructions are 16 bits, but these instructions
1767 are 32 bits.
1768
1769 The format of these instructions is:
1770
1771 +--------------+--------------------------------+
1772 | JALX | X| Imm 20:16 | Imm 25:21 |
1773 +--------------+--------------------------------+
1774 | Immediate 15:0 |
1775 +-----------------------------------------------+
1776
1777 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1778 Note that the immediate value in the first word is swapped.
1779
1780 When producing a relocatable object file, R_MIPS16_26 is
1781 handled mostly like R_MIPS_26. In particular, the addend is
1782 stored as a straight 26-bit value in a 32-bit instruction.
1783 (gas makes life simpler for itself by never adjusting a
1784 R_MIPS16_26 reloc to be against a section, so the addend is
1785 always zero). However, the 32 bit instruction is stored as 2
1786 16-bit values, rather than a single 32-bit value. In a
1787 big-endian file, the result is the same; in a little-endian
1788 file, the two 16-bit halves of the 32 bit value are swapped.
1789 This is so that a disassembler can recognize the jal
1790 instruction.
1791
1792 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1793 instruction stored as two 16-bit values. The addend A is the
1794 contents of the targ26 field. The calculation is the same as
1795 R_MIPS_26. When storing the calculated value, reorder the
1796 immediate value as shown above, and don't forget to store the
1797 value as two 16-bit values.
1798
1799 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1800 defined as
1801
1802 big-endian:
1803 +--------+----------------------+
1804 | | |
1805 | | targ26-16 |
1806 |31 26|25 0|
1807 +--------+----------------------+
1808
1809 little-endian:
1810 +----------+------+-------------+
1811 | | | |
1812 | sub1 | | sub2 |
1813 |0 9|10 15|16 31|
1814 +----------+--------------------+
1815 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1816 ((sub1 << 16) | sub2)).
1817
1818 When producing a relocatable object file, the calculation is
1819 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1820 When producing a fully linked file, the calculation is
1821 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1822 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1823
738e5348
RS
1824 The table below lists the other MIPS16 instruction relocations.
1825 Each one is calculated in the same way as the non-MIPS16 relocation
1826 given on the right, but using the extended MIPS16 layout of 16-bit
1827 immediate fields:
1828
1829 R_MIPS16_GPREL R_MIPS_GPREL16
1830 R_MIPS16_GOT16 R_MIPS_GOT16
1831 R_MIPS16_CALL16 R_MIPS_CALL16
1832 R_MIPS16_HI16 R_MIPS_HI16
1833 R_MIPS16_LO16 R_MIPS_LO16
1834
1835 A typical instruction will have a format like this:
d6f16593
MR
1836
1837 +--------------+--------------------------------+
1838 | EXTEND | Imm 10:5 | Imm 15:11 |
1839 +--------------+--------------------------------+
1840 | Major | rx | ry | Imm 4:0 |
1841 +--------------+--------------------------------+
1842
1843 EXTEND is the five bit value 11110. Major is the instruction
1844 opcode.
1845
738e5348
RS
1846 All we need to do here is shuffle the bits appropriately.
1847 As above, the two 16-bit halves must be swapped on a
1848 little-endian system. */
1849
1850static inline bfd_boolean
1851mips16_reloc_p (int r_type)
1852{
1853 switch (r_type)
1854 {
1855 case R_MIPS16_26:
1856 case R_MIPS16_GPREL:
1857 case R_MIPS16_GOT16:
1858 case R_MIPS16_CALL16:
1859 case R_MIPS16_HI16:
1860 case R_MIPS16_LO16:
1861 return TRUE;
1862
1863 default:
1864 return FALSE;
1865 }
1866}
1867
1868static inline bfd_boolean
1869got16_reloc_p (int r_type)
1870{
1871 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1872}
1873
1874static inline bfd_boolean
1875call16_reloc_p (int r_type)
1876{
1877 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1878}
1879
1880static inline bfd_boolean
1881hi16_reloc_p (int r_type)
1882{
1883 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1884}
d6f16593 1885
738e5348
RS
1886static inline bfd_boolean
1887lo16_reloc_p (int r_type)
1888{
1889 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1890}
1891
1892static inline bfd_boolean
1893mips16_call_reloc_p (int r_type)
1894{
1895 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1896}
d6f16593 1897
38a7df63
CF
1898static inline bfd_boolean
1899jal_reloc_p (int r_type)
1900{
1901 return r_type == R_MIPS_26 || r_type == R_MIPS16_26;
1902}
1903
d6f16593
MR
1904void
1905_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1906 bfd_boolean jal_shuffle, bfd_byte *data)
1907{
1908 bfd_vma extend, insn, val;
1909
738e5348 1910 if (!mips16_reloc_p (r_type))
d6f16593
MR
1911 return;
1912
1913 /* Pick up the mips16 extend instruction and the real instruction. */
1914 extend = bfd_get_16 (abfd, data);
1915 insn = bfd_get_16 (abfd, data + 2);
1916 if (r_type == R_MIPS16_26)
1917 {
1918 if (jal_shuffle)
1919 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1920 | ((extend & 0x1f) << 21) | insn;
1921 else
1922 val = extend << 16 | insn;
1923 }
1924 else
1925 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1926 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1927 bfd_put_32 (abfd, val, data);
1928}
1929
1930void
1931_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1932 bfd_boolean jal_shuffle, bfd_byte *data)
1933{
1934 bfd_vma extend, insn, val;
1935
738e5348 1936 if (!mips16_reloc_p (r_type))
d6f16593
MR
1937 return;
1938
1939 val = bfd_get_32 (abfd, data);
1940 if (r_type == R_MIPS16_26)
1941 {
1942 if (jal_shuffle)
1943 {
1944 insn = val & 0xffff;
1945 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1946 | ((val >> 21) & 0x1f);
1947 }
1948 else
1949 {
1950 insn = val & 0xffff;
1951 extend = val >> 16;
1952 }
1953 }
1954 else
1955 {
1956 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1957 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1958 }
1959 bfd_put_16 (abfd, insn, data + 2);
1960 bfd_put_16 (abfd, extend, data);
1961}
1962
b49e97c9 1963bfd_reloc_status_type
9719ad41
RS
1964_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1965 arelent *reloc_entry, asection *input_section,
1966 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
1967{
1968 bfd_vma relocation;
a7ebbfdf 1969 bfd_signed_vma val;
30ac9238 1970 bfd_reloc_status_type status;
b49e97c9
TS
1971
1972 if (bfd_is_com_section (symbol->section))
1973 relocation = 0;
1974 else
1975 relocation = symbol->value;
1976
1977 relocation += symbol->section->output_section->vma;
1978 relocation += symbol->section->output_offset;
1979
07515404 1980 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
1981 return bfd_reloc_outofrange;
1982
b49e97c9 1983 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
1984 val = reloc_entry->addend;
1985
30ac9238 1986 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 1987
b49e97c9 1988 /* Adjust val for the final section location and GP value. If we
1049f94e 1989 are producing relocatable output, we don't want to do this for
b49e97c9 1990 an external symbol. */
1049f94e 1991 if (! relocatable
b49e97c9
TS
1992 || (symbol->flags & BSF_SECTION_SYM) != 0)
1993 val += relocation - gp;
1994
a7ebbfdf
TS
1995 if (reloc_entry->howto->partial_inplace)
1996 {
30ac9238
RS
1997 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1998 (bfd_byte *) data
1999 + reloc_entry->address);
2000 if (status != bfd_reloc_ok)
2001 return status;
a7ebbfdf
TS
2002 }
2003 else
2004 reloc_entry->addend = val;
b49e97c9 2005
1049f94e 2006 if (relocatable)
b49e97c9 2007 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2008
2009 return bfd_reloc_ok;
2010}
2011
2012/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2013 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2014 that contains the relocation field and DATA points to the start of
2015 INPUT_SECTION. */
2016
2017struct mips_hi16
2018{
2019 struct mips_hi16 *next;
2020 bfd_byte *data;
2021 asection *input_section;
2022 arelent rel;
2023};
2024
2025/* FIXME: This should not be a static variable. */
2026
2027static struct mips_hi16 *mips_hi16_list;
2028
2029/* A howto special_function for REL *HI16 relocations. We can only
2030 calculate the correct value once we've seen the partnering
2031 *LO16 relocation, so just save the information for later.
2032
2033 The ABI requires that the *LO16 immediately follow the *HI16.
2034 However, as a GNU extension, we permit an arbitrary number of
2035 *HI16s to be associated with a single *LO16. This significantly
2036 simplies the relocation handling in gcc. */
2037
2038bfd_reloc_status_type
2039_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2040 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2041 asection *input_section, bfd *output_bfd,
2042 char **error_message ATTRIBUTE_UNUSED)
2043{
2044 struct mips_hi16 *n;
2045
07515404 2046 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2047 return bfd_reloc_outofrange;
2048
2049 n = bfd_malloc (sizeof *n);
2050 if (n == NULL)
2051 return bfd_reloc_outofrange;
2052
2053 n->next = mips_hi16_list;
2054 n->data = data;
2055 n->input_section = input_section;
2056 n->rel = *reloc_entry;
2057 mips_hi16_list = n;
2058
2059 if (output_bfd != NULL)
2060 reloc_entry->address += input_section->output_offset;
2061
2062 return bfd_reloc_ok;
2063}
2064
738e5348 2065/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2066 like any other 16-bit relocation when applied to global symbols, but is
2067 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2068
2069bfd_reloc_status_type
2070_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2071 void *data, asection *input_section,
2072 bfd *output_bfd, char **error_message)
2073{
2074 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2075 || bfd_is_und_section (bfd_get_section (symbol))
2076 || bfd_is_com_section (bfd_get_section (symbol)))
2077 /* The relocation is against a global symbol. */
2078 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2079 input_section, output_bfd,
2080 error_message);
2081
2082 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2083 input_section, output_bfd, error_message);
2084}
2085
2086/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2087 is a straightforward 16 bit inplace relocation, but we must deal with
2088 any partnering high-part relocations as well. */
2089
2090bfd_reloc_status_type
2091_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2092 void *data, asection *input_section,
2093 bfd *output_bfd, char **error_message)
2094{
2095 bfd_vma vallo;
d6f16593 2096 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2097
07515404 2098 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2099 return bfd_reloc_outofrange;
2100
d6f16593
MR
2101 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2102 location);
2103 vallo = bfd_get_32 (abfd, location);
2104 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2105 location);
2106
30ac9238
RS
2107 while (mips_hi16_list != NULL)
2108 {
2109 bfd_reloc_status_type ret;
2110 struct mips_hi16 *hi;
2111
2112 hi = mips_hi16_list;
2113
738e5348
RS
2114 /* R_MIPS*_GOT16 relocations are something of a special case. We
2115 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2116 relocation (with a rightshift of 16). However, since GOT16
2117 relocations can also be used with global symbols, their howto
2118 has a rightshift of 0. */
2119 if (hi->rel.howto->type == R_MIPS_GOT16)
2120 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2121 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2122 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
30ac9238
RS
2123
2124 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2125 carry or borrow will induce a change of +1 or -1 in the high part. */
2126 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2127
30ac9238
RS
2128 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2129 hi->input_section, output_bfd,
2130 error_message);
2131 if (ret != bfd_reloc_ok)
2132 return ret;
2133
2134 mips_hi16_list = hi->next;
2135 free (hi);
2136 }
2137
2138 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2139 input_section, output_bfd,
2140 error_message);
2141}
2142
2143/* A generic howto special_function. This calculates and installs the
2144 relocation itself, thus avoiding the oft-discussed problems in
2145 bfd_perform_relocation and bfd_install_relocation. */
2146
2147bfd_reloc_status_type
2148_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2149 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2150 asection *input_section, bfd *output_bfd,
2151 char **error_message ATTRIBUTE_UNUSED)
2152{
2153 bfd_signed_vma val;
2154 bfd_reloc_status_type status;
2155 bfd_boolean relocatable;
2156
2157 relocatable = (output_bfd != NULL);
2158
07515404 2159 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2160 return bfd_reloc_outofrange;
2161
2162 /* Build up the field adjustment in VAL. */
2163 val = 0;
2164 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2165 {
2166 /* Either we're calculating the final field value or we have a
2167 relocation against a section symbol. Add in the section's
2168 offset or address. */
2169 val += symbol->section->output_section->vma;
2170 val += symbol->section->output_offset;
2171 }
2172
2173 if (!relocatable)
2174 {
2175 /* We're calculating the final field value. Add in the symbol's value
2176 and, if pc-relative, subtract the address of the field itself. */
2177 val += symbol->value;
2178 if (reloc_entry->howto->pc_relative)
2179 {
2180 val -= input_section->output_section->vma;
2181 val -= input_section->output_offset;
2182 val -= reloc_entry->address;
2183 }
2184 }
2185
2186 /* VAL is now the final adjustment. If we're keeping this relocation
2187 in the output file, and if the relocation uses a separate addend,
2188 we just need to add VAL to that addend. Otherwise we need to add
2189 VAL to the relocation field itself. */
2190 if (relocatable && !reloc_entry->howto->partial_inplace)
2191 reloc_entry->addend += val;
2192 else
2193 {
d6f16593
MR
2194 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2195
30ac9238
RS
2196 /* Add in the separate addend, if any. */
2197 val += reloc_entry->addend;
2198
2199 /* Add VAL to the relocation field. */
d6f16593
MR
2200 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2201 location);
30ac9238 2202 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593
MR
2203 location);
2204 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2205 location);
2206
30ac9238
RS
2207 if (status != bfd_reloc_ok)
2208 return status;
2209 }
2210
2211 if (relocatable)
2212 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2213
2214 return bfd_reloc_ok;
2215}
2216\f
2217/* Swap an entry in a .gptab section. Note that these routines rely
2218 on the equivalence of the two elements of the union. */
2219
2220static void
9719ad41
RS
2221bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2222 Elf32_gptab *in)
b49e97c9
TS
2223{
2224 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2225 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2226}
2227
2228static void
9719ad41
RS
2229bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2230 Elf32_External_gptab *ex)
b49e97c9
TS
2231{
2232 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2233 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2234}
2235
2236static void
9719ad41
RS
2237bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2238 Elf32_External_compact_rel *ex)
b49e97c9
TS
2239{
2240 H_PUT_32 (abfd, in->id1, ex->id1);
2241 H_PUT_32 (abfd, in->num, ex->num);
2242 H_PUT_32 (abfd, in->id2, ex->id2);
2243 H_PUT_32 (abfd, in->offset, ex->offset);
2244 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2245 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2246}
2247
2248static void
9719ad41
RS
2249bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2250 Elf32_External_crinfo *ex)
b49e97c9
TS
2251{
2252 unsigned long l;
2253
2254 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2255 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2256 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2257 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2258 H_PUT_32 (abfd, l, ex->info);
2259 H_PUT_32 (abfd, in->konst, ex->konst);
2260 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2261}
b49e97c9
TS
2262\f
2263/* A .reginfo section holds a single Elf32_RegInfo structure. These
2264 routines swap this structure in and out. They are used outside of
2265 BFD, so they are globally visible. */
2266
2267void
9719ad41
RS
2268bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2269 Elf32_RegInfo *in)
b49e97c9
TS
2270{
2271 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2272 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2273 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2274 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2275 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2276 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2277}
2278
2279void
9719ad41
RS
2280bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2281 Elf32_External_RegInfo *ex)
b49e97c9
TS
2282{
2283 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2284 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2285 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2286 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2287 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2288 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2289}
2290
2291/* In the 64 bit ABI, the .MIPS.options section holds register
2292 information in an Elf64_Reginfo structure. These routines swap
2293 them in and out. They are globally visible because they are used
2294 outside of BFD. These routines are here so that gas can call them
2295 without worrying about whether the 64 bit ABI has been included. */
2296
2297void
9719ad41
RS
2298bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2299 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2300{
2301 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2302 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2303 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2304 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2305 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2306 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2307 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2308}
2309
2310void
9719ad41
RS
2311bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2312 Elf64_External_RegInfo *ex)
b49e97c9
TS
2313{
2314 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2315 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2316 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2317 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2318 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2319 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2320 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2321}
2322
2323/* Swap in an options header. */
2324
2325void
9719ad41
RS
2326bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2327 Elf_Internal_Options *in)
b49e97c9
TS
2328{
2329 in->kind = H_GET_8 (abfd, ex->kind);
2330 in->size = H_GET_8 (abfd, ex->size);
2331 in->section = H_GET_16 (abfd, ex->section);
2332 in->info = H_GET_32 (abfd, ex->info);
2333}
2334
2335/* Swap out an options header. */
2336
2337void
9719ad41
RS
2338bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2339 Elf_External_Options *ex)
b49e97c9
TS
2340{
2341 H_PUT_8 (abfd, in->kind, ex->kind);
2342 H_PUT_8 (abfd, in->size, ex->size);
2343 H_PUT_16 (abfd, in->section, ex->section);
2344 H_PUT_32 (abfd, in->info, ex->info);
2345}
2346\f
2347/* This function is called via qsort() to sort the dynamic relocation
2348 entries by increasing r_symndx value. */
2349
2350static int
9719ad41 2351sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2352{
947216bf
AM
2353 Elf_Internal_Rela int_reloc1;
2354 Elf_Internal_Rela int_reloc2;
6870500c 2355 int diff;
b49e97c9 2356
947216bf
AM
2357 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2358 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2359
6870500c
RS
2360 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2361 if (diff != 0)
2362 return diff;
2363
2364 if (int_reloc1.r_offset < int_reloc2.r_offset)
2365 return -1;
2366 if (int_reloc1.r_offset > int_reloc2.r_offset)
2367 return 1;
2368 return 0;
b49e97c9
TS
2369}
2370
f4416af6
AO
2371/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2372
2373static int
7e3102a7
AM
2374sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2375 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2376{
7e3102a7 2377#ifdef BFD64
f4416af6
AO
2378 Elf_Internal_Rela int_reloc1[3];
2379 Elf_Internal_Rela int_reloc2[3];
2380
2381 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2382 (reldyn_sorting_bfd, arg1, int_reloc1);
2383 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2384 (reldyn_sorting_bfd, arg2, int_reloc2);
2385
6870500c
RS
2386 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2387 return -1;
2388 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2389 return 1;
2390
2391 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2392 return -1;
2393 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2394 return 1;
2395 return 0;
7e3102a7
AM
2396#else
2397 abort ();
2398#endif
f4416af6
AO
2399}
2400
2401
b49e97c9
TS
2402/* This routine is used to write out ECOFF debugging external symbol
2403 information. It is called via mips_elf_link_hash_traverse. The
2404 ECOFF external symbol information must match the ELF external
2405 symbol information. Unfortunately, at this point we don't know
2406 whether a symbol is required by reloc information, so the two
2407 tables may wind up being different. We must sort out the external
2408 symbol information before we can set the final size of the .mdebug
2409 section, and we must set the size of the .mdebug section before we
2410 can relocate any sections, and we can't know which symbols are
2411 required by relocation until we relocate the sections.
2412 Fortunately, it is relatively unlikely that any symbol will be
2413 stripped but required by a reloc. In particular, it can not happen
2414 when generating a final executable. */
2415
b34976b6 2416static bfd_boolean
9719ad41 2417mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2418{
9719ad41 2419 struct extsym_info *einfo = data;
b34976b6 2420 bfd_boolean strip;
b49e97c9
TS
2421 asection *sec, *output_section;
2422
2423 if (h->root.root.type == bfd_link_hash_warning)
2424 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2425
2426 if (h->root.indx == -2)
b34976b6 2427 strip = FALSE;
f5385ebf 2428 else if ((h->root.def_dynamic
77cfaee6
AM
2429 || h->root.ref_dynamic
2430 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2431 && !h->root.def_regular
2432 && !h->root.ref_regular)
b34976b6 2433 strip = TRUE;
b49e97c9
TS
2434 else if (einfo->info->strip == strip_all
2435 || (einfo->info->strip == strip_some
2436 && bfd_hash_lookup (einfo->info->keep_hash,
2437 h->root.root.root.string,
b34976b6
AM
2438 FALSE, FALSE) == NULL))
2439 strip = TRUE;
b49e97c9 2440 else
b34976b6 2441 strip = FALSE;
b49e97c9
TS
2442
2443 if (strip)
b34976b6 2444 return TRUE;
b49e97c9
TS
2445
2446 if (h->esym.ifd == -2)
2447 {
2448 h->esym.jmptbl = 0;
2449 h->esym.cobol_main = 0;
2450 h->esym.weakext = 0;
2451 h->esym.reserved = 0;
2452 h->esym.ifd = ifdNil;
2453 h->esym.asym.value = 0;
2454 h->esym.asym.st = stGlobal;
2455
2456 if (h->root.root.type == bfd_link_hash_undefined
2457 || h->root.root.type == bfd_link_hash_undefweak)
2458 {
2459 const char *name;
2460
2461 /* Use undefined class. Also, set class and type for some
2462 special symbols. */
2463 name = h->root.root.root.string;
2464 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2465 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2466 {
2467 h->esym.asym.sc = scData;
2468 h->esym.asym.st = stLabel;
2469 h->esym.asym.value = 0;
2470 }
2471 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2472 {
2473 h->esym.asym.sc = scAbs;
2474 h->esym.asym.st = stLabel;
2475 h->esym.asym.value =
2476 mips_elf_hash_table (einfo->info)->procedure_count;
2477 }
4a14403c 2478 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2479 {
2480 h->esym.asym.sc = scAbs;
2481 h->esym.asym.st = stLabel;
2482 h->esym.asym.value = elf_gp (einfo->abfd);
2483 }
2484 else
2485 h->esym.asym.sc = scUndefined;
2486 }
2487 else if (h->root.root.type != bfd_link_hash_defined
2488 && h->root.root.type != bfd_link_hash_defweak)
2489 h->esym.asym.sc = scAbs;
2490 else
2491 {
2492 const char *name;
2493
2494 sec = h->root.root.u.def.section;
2495 output_section = sec->output_section;
2496
2497 /* When making a shared library and symbol h is the one from
2498 the another shared library, OUTPUT_SECTION may be null. */
2499 if (output_section == NULL)
2500 h->esym.asym.sc = scUndefined;
2501 else
2502 {
2503 name = bfd_section_name (output_section->owner, output_section);
2504
2505 if (strcmp (name, ".text") == 0)
2506 h->esym.asym.sc = scText;
2507 else if (strcmp (name, ".data") == 0)
2508 h->esym.asym.sc = scData;
2509 else if (strcmp (name, ".sdata") == 0)
2510 h->esym.asym.sc = scSData;
2511 else if (strcmp (name, ".rodata") == 0
2512 || strcmp (name, ".rdata") == 0)
2513 h->esym.asym.sc = scRData;
2514 else if (strcmp (name, ".bss") == 0)
2515 h->esym.asym.sc = scBss;
2516 else if (strcmp (name, ".sbss") == 0)
2517 h->esym.asym.sc = scSBss;
2518 else if (strcmp (name, ".init") == 0)
2519 h->esym.asym.sc = scInit;
2520 else if (strcmp (name, ".fini") == 0)
2521 h->esym.asym.sc = scFini;
2522 else
2523 h->esym.asym.sc = scAbs;
2524 }
2525 }
2526
2527 h->esym.asym.reserved = 0;
2528 h->esym.asym.index = indexNil;
2529 }
2530
2531 if (h->root.root.type == bfd_link_hash_common)
2532 h->esym.asym.value = h->root.root.u.c.size;
2533 else if (h->root.root.type == bfd_link_hash_defined
2534 || h->root.root.type == bfd_link_hash_defweak)
2535 {
2536 if (h->esym.asym.sc == scCommon)
2537 h->esym.asym.sc = scBss;
2538 else if (h->esym.asym.sc == scSCommon)
2539 h->esym.asym.sc = scSBss;
2540
2541 sec = h->root.root.u.def.section;
2542 output_section = sec->output_section;
2543 if (output_section != NULL)
2544 h->esym.asym.value = (h->root.root.u.def.value
2545 + sec->output_offset
2546 + output_section->vma);
2547 else
2548 h->esym.asym.value = 0;
2549 }
33bb52fb 2550 else
b49e97c9
TS
2551 {
2552 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2553
2554 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2555 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2556
33bb52fb 2557 if (hd->needs_lazy_stub)
b49e97c9
TS
2558 {
2559 /* Set type and value for a symbol with a function stub. */
2560 h->esym.asym.st = stProc;
2561 sec = hd->root.root.u.def.section;
2562 if (sec == NULL)
2563 h->esym.asym.value = 0;
2564 else
2565 {
2566 output_section = sec->output_section;
2567 if (output_section != NULL)
2568 h->esym.asym.value = (hd->root.plt.offset
2569 + sec->output_offset
2570 + output_section->vma);
2571 else
2572 h->esym.asym.value = 0;
2573 }
b49e97c9
TS
2574 }
2575 }
2576
2577 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2578 h->root.root.root.string,
2579 &h->esym))
2580 {
b34976b6
AM
2581 einfo->failed = TRUE;
2582 return FALSE;
b49e97c9
TS
2583 }
2584
b34976b6 2585 return TRUE;
b49e97c9
TS
2586}
2587
2588/* A comparison routine used to sort .gptab entries. */
2589
2590static int
9719ad41 2591gptab_compare (const void *p1, const void *p2)
b49e97c9 2592{
9719ad41
RS
2593 const Elf32_gptab *a1 = p1;
2594 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2595
2596 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2597}
2598\f
b15e6682 2599/* Functions to manage the got entry hash table. */
f4416af6
AO
2600
2601/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2602 hash number. */
2603
2604static INLINE hashval_t
9719ad41 2605mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2606{
2607#ifdef BFD64
2608 return addr + (addr >> 32);
2609#else
2610 return addr;
2611#endif
2612}
2613
2614/* got_entries only match if they're identical, except for gotidx, so
2615 use all fields to compute the hash, and compare the appropriate
2616 union members. */
2617
b15e6682 2618static hashval_t
9719ad41 2619mips_elf_got_entry_hash (const void *entry_)
b15e6682
AO
2620{
2621 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2622
38985a1c 2623 return entry->symndx
0f20cc35 2624 + ((entry->tls_type & GOT_TLS_LDM) << 17)
f4416af6 2625 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
38985a1c
AO
2626 : entry->abfd->id
2627 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2628 : entry->d.h->root.root.root.hash));
b15e6682
AO
2629}
2630
2631static int
9719ad41 2632mips_elf_got_entry_eq (const void *entry1, const void *entry2)
b15e6682
AO
2633{
2634 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2635 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2636
0f20cc35
DJ
2637 /* An LDM entry can only match another LDM entry. */
2638 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2639 return 0;
2640
b15e6682 2641 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
f4416af6
AO
2642 && (! e1->abfd ? e1->d.address == e2->d.address
2643 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2644 : e1->d.h == e2->d.h);
2645}
2646
2647/* multi_got_entries are still a match in the case of global objects,
2648 even if the input bfd in which they're referenced differs, so the
2649 hash computation and compare functions are adjusted
2650 accordingly. */
2651
2652static hashval_t
9719ad41 2653mips_elf_multi_got_entry_hash (const void *entry_)
f4416af6
AO
2654{
2655 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2656
2657 return entry->symndx
2658 + (! entry->abfd
2659 ? mips_elf_hash_bfd_vma (entry->d.address)
2660 : entry->symndx >= 0
0f20cc35
DJ
2661 ? ((entry->tls_type & GOT_TLS_LDM)
2662 ? (GOT_TLS_LDM << 17)
2663 : (entry->abfd->id
2664 + mips_elf_hash_bfd_vma (entry->d.addend)))
f4416af6
AO
2665 : entry->d.h->root.root.root.hash);
2666}
2667
2668static int
9719ad41 2669mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2670{
2671 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2672 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2673
0f20cc35
DJ
2674 /* Any two LDM entries match. */
2675 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2676 return 1;
2677
2678 /* Nothing else matches an LDM entry. */
2679 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2680 return 0;
2681
f4416af6
AO
2682 return e1->symndx == e2->symndx
2683 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2684 : e1->abfd == NULL || e2->abfd == NULL
2685 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2686 : e1->d.h == e2->d.h);
b15e6682 2687}
c224138d
RS
2688
2689static hashval_t
2690mips_got_page_entry_hash (const void *entry_)
2691{
2692 const struct mips_got_page_entry *entry;
2693
2694 entry = (const struct mips_got_page_entry *) entry_;
2695 return entry->abfd->id + entry->symndx;
2696}
2697
2698static int
2699mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2700{
2701 const struct mips_got_page_entry *entry1, *entry2;
2702
2703 entry1 = (const struct mips_got_page_entry *) entry1_;
2704 entry2 = (const struct mips_got_page_entry *) entry2_;
2705 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2706}
b15e6682 2707\f
0a44bf69
RS
2708/* Return the dynamic relocation section. If it doesn't exist, try to
2709 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2710 if creation fails. */
f4416af6
AO
2711
2712static asection *
0a44bf69 2713mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 2714{
0a44bf69 2715 const char *dname;
f4416af6 2716 asection *sreloc;
0a44bf69 2717 bfd *dynobj;
f4416af6 2718
0a44bf69
RS
2719 dname = MIPS_ELF_REL_DYN_NAME (info);
2720 dynobj = elf_hash_table (info)->dynobj;
f4416af6
AO
2721 sreloc = bfd_get_section_by_name (dynobj, dname);
2722 if (sreloc == NULL && create_p)
2723 {
3496cb2a
L
2724 sreloc = bfd_make_section_with_flags (dynobj, dname,
2725 (SEC_ALLOC
2726 | SEC_LOAD
2727 | SEC_HAS_CONTENTS
2728 | SEC_IN_MEMORY
2729 | SEC_LINKER_CREATED
2730 | SEC_READONLY));
f4416af6 2731 if (sreloc == NULL
f4416af6 2732 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 2733 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
2734 return NULL;
2735 }
2736 return sreloc;
2737}
2738
0f20cc35
DJ
2739/* Count the number of relocations needed for a TLS GOT entry, with
2740 access types from TLS_TYPE, and symbol H (or a local symbol if H
2741 is NULL). */
2742
2743static int
2744mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2745 struct elf_link_hash_entry *h)
2746{
2747 int indx = 0;
2748 int ret = 0;
2749 bfd_boolean need_relocs = FALSE;
2750 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2751
2752 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2753 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2754 indx = h->dynindx;
2755
2756 if ((info->shared || indx != 0)
2757 && (h == NULL
2758 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2759 || h->root.type != bfd_link_hash_undefweak))
2760 need_relocs = TRUE;
2761
2762 if (!need_relocs)
2763 return FALSE;
2764
2765 if (tls_type & GOT_TLS_GD)
2766 {
2767 ret++;
2768 if (indx != 0)
2769 ret++;
2770 }
2771
2772 if (tls_type & GOT_TLS_IE)
2773 ret++;
2774
2775 if ((tls_type & GOT_TLS_LDM) && info->shared)
2776 ret++;
2777
2778 return ret;
2779}
2780
2781/* Count the number of TLS relocations required for the GOT entry in
2782 ARG1, if it describes a local symbol. */
2783
2784static int
2785mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2786{
2787 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2788 struct mips_elf_count_tls_arg *arg = arg2;
2789
2790 if (entry->abfd != NULL && entry->symndx != -1)
2791 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2792
2793 return 1;
2794}
2795
2796/* Count the number of TLS GOT entries required for the global (or
2797 forced-local) symbol in ARG1. */
2798
2799static int
2800mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2801{
2802 struct mips_elf_link_hash_entry *hm
2803 = (struct mips_elf_link_hash_entry *) arg1;
2804 struct mips_elf_count_tls_arg *arg = arg2;
2805
2806 if (hm->tls_type & GOT_TLS_GD)
2807 arg->needed += 2;
2808 if (hm->tls_type & GOT_TLS_IE)
2809 arg->needed += 1;
2810
2811 return 1;
2812}
2813
2814/* Count the number of TLS relocations required for the global (or
2815 forced-local) symbol in ARG1. */
2816
2817static int
2818mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2819{
2820 struct mips_elf_link_hash_entry *hm
2821 = (struct mips_elf_link_hash_entry *) arg1;
2822 struct mips_elf_count_tls_arg *arg = arg2;
2823
2824 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2825
2826 return 1;
2827}
2828
2829/* Output a simple dynamic relocation into SRELOC. */
2830
2831static void
2832mips_elf_output_dynamic_relocation (bfd *output_bfd,
2833 asection *sreloc,
861fb55a 2834 unsigned long reloc_index,
0f20cc35
DJ
2835 unsigned long indx,
2836 int r_type,
2837 bfd_vma offset)
2838{
2839 Elf_Internal_Rela rel[3];
2840
2841 memset (rel, 0, sizeof (rel));
2842
2843 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2844 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2845
2846 if (ABI_64_P (output_bfd))
2847 {
2848 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2849 (output_bfd, &rel[0],
2850 (sreloc->contents
861fb55a 2851 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
2852 }
2853 else
2854 bfd_elf32_swap_reloc_out
2855 (output_bfd, &rel[0],
2856 (sreloc->contents
861fb55a 2857 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
2858}
2859
2860/* Initialize a set of TLS GOT entries for one symbol. */
2861
2862static void
2863mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2864 unsigned char *tls_type_p,
2865 struct bfd_link_info *info,
2866 struct mips_elf_link_hash_entry *h,
2867 bfd_vma value)
2868{
23cc69b6 2869 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
2870 int indx;
2871 asection *sreloc, *sgot;
2872 bfd_vma offset, offset2;
0f20cc35
DJ
2873 bfd_boolean need_relocs = FALSE;
2874
23cc69b6 2875 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
2876 if (htab == NULL)
2877 return;
2878
23cc69b6 2879 sgot = htab->sgot;
0f20cc35
DJ
2880
2881 indx = 0;
2882 if (h != NULL)
2883 {
2884 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2885
2886 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2887 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2888 indx = h->root.dynindx;
2889 }
2890
2891 if (*tls_type_p & GOT_TLS_DONE)
2892 return;
2893
2894 if ((info->shared || indx != 0)
2895 && (h == NULL
2896 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2897 || h->root.type != bfd_link_hash_undefweak))
2898 need_relocs = TRUE;
2899
2900 /* MINUS_ONE means the symbol is not defined in this object. It may not
2901 be defined at all; assume that the value doesn't matter in that
2902 case. Otherwise complain if we would use the value. */
2903 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2904 || h->root.root.type == bfd_link_hash_undefweak);
2905
2906 /* Emit necessary relocations. */
0a44bf69 2907 sreloc = mips_elf_rel_dyn_section (info, FALSE);
0f20cc35
DJ
2908
2909 /* General Dynamic. */
2910 if (*tls_type_p & GOT_TLS_GD)
2911 {
2912 offset = got_offset;
2913 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2914
2915 if (need_relocs)
2916 {
2917 mips_elf_output_dynamic_relocation
861fb55a 2918 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2919 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2920 sgot->output_offset + sgot->output_section->vma + offset);
2921
2922 if (indx)
2923 mips_elf_output_dynamic_relocation
861fb55a 2924 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2925 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2926 sgot->output_offset + sgot->output_section->vma + offset2);
2927 else
2928 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2929 sgot->contents + offset2);
2930 }
2931 else
2932 {
2933 MIPS_ELF_PUT_WORD (abfd, 1,
2934 sgot->contents + offset);
2935 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2936 sgot->contents + offset2);
2937 }
2938
2939 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2940 }
2941
2942 /* Initial Exec model. */
2943 if (*tls_type_p & GOT_TLS_IE)
2944 {
2945 offset = got_offset;
2946
2947 if (need_relocs)
2948 {
2949 if (indx == 0)
2950 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2951 sgot->contents + offset);
2952 else
2953 MIPS_ELF_PUT_WORD (abfd, 0,
2954 sgot->contents + offset);
2955
2956 mips_elf_output_dynamic_relocation
861fb55a 2957 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2958 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2959 sgot->output_offset + sgot->output_section->vma + offset);
2960 }
2961 else
2962 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2963 sgot->contents + offset);
2964 }
2965
2966 if (*tls_type_p & GOT_TLS_LDM)
2967 {
2968 /* The initial offset is zero, and the LD offsets will include the
2969 bias by DTP_OFFSET. */
2970 MIPS_ELF_PUT_WORD (abfd, 0,
2971 sgot->contents + got_offset
2972 + MIPS_ELF_GOT_SIZE (abfd));
2973
2974 if (!info->shared)
2975 MIPS_ELF_PUT_WORD (abfd, 1,
2976 sgot->contents + got_offset);
2977 else
2978 mips_elf_output_dynamic_relocation
861fb55a 2979 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2980 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2981 sgot->output_offset + sgot->output_section->vma + got_offset);
2982 }
2983
2984 *tls_type_p |= GOT_TLS_DONE;
2985}
2986
2987/* Return the GOT index to use for a relocation of type R_TYPE against
2988 a symbol accessed using TLS_TYPE models. The GOT entries for this
2989 symbol in this GOT start at GOT_INDEX. This function initializes the
2990 GOT entries and corresponding relocations. */
2991
2992static bfd_vma
2993mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2994 int r_type, struct bfd_link_info *info,
2995 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2996{
2997 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2998 || r_type == R_MIPS_TLS_LDM);
2999
3000 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
3001
3002 if (r_type == R_MIPS_TLS_GOTTPREL)
3003 {
3004 BFD_ASSERT (*tls_type & GOT_TLS_IE);
3005 if (*tls_type & GOT_TLS_GD)
3006 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
3007 else
3008 return got_index;
3009 }
3010
3011 if (r_type == R_MIPS_TLS_GD)
3012 {
3013 BFD_ASSERT (*tls_type & GOT_TLS_GD);
3014 return got_index;
3015 }
3016
3017 if (r_type == R_MIPS_TLS_LDM)
3018 {
3019 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
3020 return got_index;
3021 }
3022
3023 return got_index;
3024}
3025
0a44bf69
RS
3026/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3027 for global symbol H. .got.plt comes before the GOT, so the offset
3028 will be negative. */
3029
3030static bfd_vma
3031mips_elf_gotplt_index (struct bfd_link_info *info,
3032 struct elf_link_hash_entry *h)
3033{
3034 bfd_vma plt_index, got_address, got_value;
3035 struct mips_elf_link_hash_table *htab;
3036
3037 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3038 BFD_ASSERT (htab != NULL);
3039
0a44bf69
RS
3040 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3041
861fb55a
DJ
3042 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3043 section starts with reserved entries. */
3044 BFD_ASSERT (htab->is_vxworks);
3045
0a44bf69
RS
3046 /* Calculate the index of the symbol's PLT entry. */
3047 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3048
3049 /* Calculate the address of the associated .got.plt entry. */
3050 got_address = (htab->sgotplt->output_section->vma
3051 + htab->sgotplt->output_offset
3052 + plt_index * 4);
3053
3054 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3055 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3056 + htab->root.hgot->root.u.def.section->output_offset
3057 + htab->root.hgot->root.u.def.value);
3058
3059 return got_address - got_value;
3060}
3061
5c18022e 3062/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3063 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3064 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3065 offset can be found. */
b49e97c9
TS
3066
3067static bfd_vma
9719ad41 3068mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3069 bfd_vma value, unsigned long r_symndx,
0f20cc35 3070 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3071{
a8028dd0 3072 struct mips_elf_link_hash_table *htab;
b15e6682 3073 struct mips_got_entry *entry;
b49e97c9 3074
a8028dd0 3075 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3076 BFD_ASSERT (htab != NULL);
3077
a8028dd0
RS
3078 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3079 r_symndx, h, r_type);
0f20cc35 3080 if (!entry)
b15e6682 3081 return MINUS_ONE;
0f20cc35
DJ
3082
3083 if (TLS_RELOC_P (r_type))
ead49a57 3084 {
a8028dd0 3085 if (entry->symndx == -1 && htab->got_info->next == NULL)
ead49a57
RS
3086 /* A type (3) entry in the single-GOT case. We use the symbol's
3087 hash table entry to track the index. */
3088 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3089 r_type, info, h, value);
3090 else
3091 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3092 r_type, info, h, value);
3093 }
0f20cc35
DJ
3094 else
3095 return entry->gotidx;
b49e97c9
TS
3096}
3097
3098/* Returns the GOT index for the global symbol indicated by H. */
3099
3100static bfd_vma
0f20cc35
DJ
3101mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3102 int r_type, struct bfd_link_info *info)
b49e97c9 3103{
a8028dd0 3104 struct mips_elf_link_hash_table *htab;
91d6fa6a 3105 bfd_vma got_index;
f4416af6 3106 struct mips_got_info *g, *gg;
d0c7ff07 3107 long global_got_dynindx = 0;
b49e97c9 3108
a8028dd0 3109 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3110 BFD_ASSERT (htab != NULL);
3111
a8028dd0 3112 gg = g = htab->got_info;
f4416af6
AO
3113 if (g->bfd2got && ibfd)
3114 {
3115 struct mips_got_entry e, *p;
143d77c5 3116
f4416af6
AO
3117 BFD_ASSERT (h->dynindx >= 0);
3118
3119 g = mips_elf_got_for_ibfd (g, ibfd);
0f20cc35 3120 if (g->next != gg || TLS_RELOC_P (r_type))
f4416af6
AO
3121 {
3122 e.abfd = ibfd;
3123 e.symndx = -1;
3124 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 3125 e.tls_type = 0;
f4416af6 3126
9719ad41 3127 p = htab_find (g->got_entries, &e);
f4416af6
AO
3128
3129 BFD_ASSERT (p->gotidx > 0);
0f20cc35
DJ
3130
3131 if (TLS_RELOC_P (r_type))
3132 {
3133 bfd_vma value = MINUS_ONE;
3134 if ((h->root.type == bfd_link_hash_defined
3135 || h->root.type == bfd_link_hash_defweak)
3136 && h->root.u.def.section->output_section)
3137 value = (h->root.u.def.value
3138 + h->root.u.def.section->output_offset
3139 + h->root.u.def.section->output_section->vma);
3140
3141 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3142 info, e.d.h, value);
3143 }
3144 else
3145 return p->gotidx;
f4416af6
AO
3146 }
3147 }
3148
3149 if (gg->global_gotsym != NULL)
3150 global_got_dynindx = gg->global_gotsym->dynindx;
b49e97c9 3151
0f20cc35
DJ
3152 if (TLS_RELOC_P (r_type))
3153 {
3154 struct mips_elf_link_hash_entry *hm
3155 = (struct mips_elf_link_hash_entry *) h;
3156 bfd_vma value = MINUS_ONE;
3157
3158 if ((h->root.type == bfd_link_hash_defined
3159 || h->root.type == bfd_link_hash_defweak)
3160 && h->root.u.def.section->output_section)
3161 value = (h->root.u.def.value
3162 + h->root.u.def.section->output_offset
3163 + h->root.u.def.section->output_section->vma);
3164
91d6fa6a
NC
3165 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3166 r_type, info, hm, value);
0f20cc35
DJ
3167 }
3168 else
3169 {
3170 /* Once we determine the global GOT entry with the lowest dynamic
3171 symbol table index, we must put all dynamic symbols with greater
3172 indices into the GOT. That makes it easy to calculate the GOT
3173 offset. */
3174 BFD_ASSERT (h->dynindx >= global_got_dynindx);
91d6fa6a
NC
3175 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3176 * MIPS_ELF_GOT_SIZE (abfd));
0f20cc35 3177 }
91d6fa6a 3178 BFD_ASSERT (got_index < htab->sgot->size);
b49e97c9 3179
91d6fa6a 3180 return got_index;
b49e97c9
TS
3181}
3182
5c18022e
RS
3183/* Find a GOT page entry that points to within 32KB of VALUE. These
3184 entries are supposed to be placed at small offsets in the GOT, i.e.,
3185 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3186 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3187 offset of the GOT entry from VALUE. */
b49e97c9
TS
3188
3189static bfd_vma
9719ad41 3190mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3191 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3192{
91d6fa6a 3193 bfd_vma page, got_index;
b15e6682 3194 struct mips_got_entry *entry;
b49e97c9 3195
0a44bf69 3196 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3197 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3198 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3199
b15e6682
AO
3200 if (!entry)
3201 return MINUS_ONE;
143d77c5 3202
91d6fa6a 3203 got_index = entry->gotidx;
b49e97c9
TS
3204
3205 if (offsetp)
f4416af6 3206 *offsetp = value - entry->d.address;
b49e97c9 3207
91d6fa6a 3208 return got_index;
b49e97c9
TS
3209}
3210
738e5348 3211/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3212 EXTERNAL is true if the relocation was originally against a global
3213 symbol that binds locally. */
b49e97c9
TS
3214
3215static bfd_vma
9719ad41 3216mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3217 bfd_vma value, bfd_boolean external)
b49e97c9 3218{
b15e6682 3219 struct mips_got_entry *entry;
b49e97c9 3220
0a44bf69
RS
3221 /* GOT16 relocations against local symbols are followed by a LO16
3222 relocation; those against global symbols are not. Thus if the
3223 symbol was originally local, the GOT16 relocation should load the
3224 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3225 if (! external)
0a44bf69 3226 value = mips_elf_high (value) << 16;
b49e97c9 3227
738e5348
RS
3228 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3229 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3230 same in all cases. */
a8028dd0
RS
3231 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3232 NULL, R_MIPS_GOT16);
b15e6682
AO
3233 if (entry)
3234 return entry->gotidx;
3235 else
3236 return MINUS_ONE;
b49e97c9
TS
3237}
3238
3239/* Returns the offset for the entry at the INDEXth position
3240 in the GOT. */
3241
3242static bfd_vma
a8028dd0 3243mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3244 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3245{
a8028dd0 3246 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3247 asection *sgot;
3248 bfd_vma gp;
3249
a8028dd0 3250 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3251 BFD_ASSERT (htab != NULL);
3252
a8028dd0 3253 sgot = htab->sgot;
f4416af6 3254 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3255 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3256
91d6fa6a 3257 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3258}
3259
0a44bf69
RS
3260/* Create and return a local GOT entry for VALUE, which was calculated
3261 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3262 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3263 instead. */
b49e97c9 3264
b15e6682 3265static struct mips_got_entry *
0a44bf69 3266mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3267 bfd *ibfd, bfd_vma value,
5c18022e 3268 unsigned long r_symndx,
0f20cc35
DJ
3269 struct mips_elf_link_hash_entry *h,
3270 int r_type)
b49e97c9 3271{
b15e6682 3272 struct mips_got_entry entry, **loc;
f4416af6 3273 struct mips_got_info *g;
0a44bf69
RS
3274 struct mips_elf_link_hash_table *htab;
3275
3276 htab = mips_elf_hash_table (info);
4dfe6ac6 3277 BFD_ASSERT (htab != NULL);
b15e6682 3278
f4416af6
AO
3279 entry.abfd = NULL;
3280 entry.symndx = -1;
3281 entry.d.address = value;
0f20cc35 3282 entry.tls_type = 0;
f4416af6 3283
a8028dd0 3284 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
f4416af6
AO
3285 if (g == NULL)
3286 {
a8028dd0 3287 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
f4416af6
AO
3288 BFD_ASSERT (g != NULL);
3289 }
b15e6682 3290
020d7251
RS
3291 /* This function shouldn't be called for symbols that live in the global
3292 area of the GOT. */
3293 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35
DJ
3294 if (TLS_RELOC_P (r_type))
3295 {
3296 struct mips_got_entry *p;
3297
3298 entry.abfd = ibfd;
3299 if (r_type == R_MIPS_TLS_LDM)
3300 {
3301 entry.tls_type = GOT_TLS_LDM;
3302 entry.symndx = 0;
3303 entry.d.addend = 0;
3304 }
3305 else if (h == NULL)
3306 {
3307 entry.symndx = r_symndx;
3308 entry.d.addend = 0;
3309 }
3310 else
3311 entry.d.h = h;
3312
3313 p = (struct mips_got_entry *)
3314 htab_find (g->got_entries, &entry);
3315
3316 BFD_ASSERT (p);
3317 return p;
3318 }
3319
b15e6682
AO
3320 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3321 INSERT);
3322 if (*loc)
3323 return *loc;
143d77c5 3324
b15e6682 3325 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
0f20cc35 3326 entry.tls_type = 0;
b15e6682
AO
3327
3328 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3329
3330 if (! *loc)
3331 return NULL;
143d77c5 3332
b15e6682
AO
3333 memcpy (*loc, &entry, sizeof entry);
3334
8275b357 3335 if (g->assigned_gotno > g->local_gotno)
b49e97c9 3336 {
f4416af6 3337 (*loc)->gotidx = -1;
b49e97c9
TS
3338 /* We didn't allocate enough space in the GOT. */
3339 (*_bfd_error_handler)
3340 (_("not enough GOT space for local GOT entries"));
3341 bfd_set_error (bfd_error_bad_value);
b15e6682 3342 return NULL;
b49e97c9
TS
3343 }
3344
3345 MIPS_ELF_PUT_WORD (abfd, value,
a8028dd0 3346 (htab->sgot->contents + entry.gotidx));
b15e6682 3347
5c18022e 3348 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3349 if (htab->is_vxworks)
3350 {
3351 Elf_Internal_Rela outrel;
5c18022e 3352 asection *s;
91d6fa6a 3353 bfd_byte *rloc;
0a44bf69 3354 bfd_vma got_address;
0a44bf69
RS
3355
3356 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3357 got_address = (htab->sgot->output_section->vma
3358 + htab->sgot->output_offset
0a44bf69
RS
3359 + entry.gotidx);
3360
91d6fa6a 3361 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3362 outrel.r_offset = got_address;
5c18022e
RS
3363 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3364 outrel.r_addend = value;
91d6fa6a 3365 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3366 }
3367
b15e6682 3368 return *loc;
b49e97c9
TS
3369}
3370
d4596a51
RS
3371/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3372 The number might be exact or a worst-case estimate, depending on how
3373 much information is available to elf_backend_omit_section_dynsym at
3374 the current linking stage. */
3375
3376static bfd_size_type
3377count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3378{
3379 bfd_size_type count;
3380
3381 count = 0;
3382 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3383 {
3384 asection *p;
3385 const struct elf_backend_data *bed;
3386
3387 bed = get_elf_backend_data (output_bfd);
3388 for (p = output_bfd->sections; p ; p = p->next)
3389 if ((p->flags & SEC_EXCLUDE) == 0
3390 && (p->flags & SEC_ALLOC) != 0
3391 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3392 ++count;
3393 }
3394 return count;
3395}
3396
b49e97c9 3397/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3398 appear towards the end. */
b49e97c9 3399
b34976b6 3400static bfd_boolean
d4596a51 3401mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3402{
a8028dd0 3403 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3404 struct mips_elf_hash_sort_data hsd;
3405 struct mips_got_info *g;
b49e97c9 3406
d4596a51
RS
3407 if (elf_hash_table (info)->dynsymcount == 0)
3408 return TRUE;
3409
a8028dd0 3410 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3411 BFD_ASSERT (htab != NULL);
3412
a8028dd0 3413 g = htab->got_info;
d4596a51
RS
3414 if (g == NULL)
3415 return TRUE;
f4416af6 3416
b49e97c9 3417 hsd.low = NULL;
23cc69b6
RS
3418 hsd.max_unref_got_dynindx
3419 = hsd.min_got_dynindx
3420 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3421 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3422 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3423 elf_hash_table (info)),
3424 mips_elf_sort_hash_table_f,
3425 &hsd);
3426
3427 /* There should have been enough room in the symbol table to
44c410de 3428 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3429 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3430 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3431 == elf_hash_table (info)->dynsymcount);
3432 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3433 == g->global_gotno);
b49e97c9
TS
3434
3435 /* Now we know which dynamic symbol has the lowest dynamic symbol
3436 table index in the GOT. */
b49e97c9
TS
3437 g->global_gotsym = hsd.low;
3438
b34976b6 3439 return TRUE;
b49e97c9
TS
3440}
3441
3442/* If H needs a GOT entry, assign it the highest available dynamic
3443 index. Otherwise, assign it the lowest available dynamic
3444 index. */
3445
b34976b6 3446static bfd_boolean
9719ad41 3447mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3448{
9719ad41 3449 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9
TS
3450
3451 if (h->root.root.type == bfd_link_hash_warning)
3452 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3453
3454 /* Symbols without dynamic symbol table entries aren't interesting
3455 at all. */
3456 if (h->root.dynindx == -1)
b34976b6 3457 return TRUE;
b49e97c9 3458
634835ae 3459 switch (h->global_got_area)
f4416af6 3460 {
634835ae
RS
3461 case GGA_NONE:
3462 h->root.dynindx = hsd->max_non_got_dynindx++;
3463 break;
0f20cc35 3464
634835ae 3465 case GGA_NORMAL:
0f20cc35
DJ
3466 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3467
b49e97c9
TS
3468 h->root.dynindx = --hsd->min_got_dynindx;
3469 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3470 break;
3471
3472 case GGA_RELOC_ONLY:
3473 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3474
3475 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3476 hsd->low = (struct elf_link_hash_entry *) h;
3477 h->root.dynindx = hsd->max_unref_got_dynindx++;
3478 break;
b49e97c9
TS
3479 }
3480
b34976b6 3481 return TRUE;
b49e97c9
TS
3482}
3483
3484/* If H is a symbol that needs a global GOT entry, but has a dynamic
3485 symbol table index lower than any we've seen to date, record it for
6ccf4795
RS
3486 posterity. FOR_CALL is true if the caller is only interested in
3487 using the GOT entry for calls. */
b49e97c9 3488
b34976b6 3489static bfd_boolean
9719ad41
RS
3490mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3491 bfd *abfd, struct bfd_link_info *info,
6ccf4795 3492 bfd_boolean for_call,
0f20cc35 3493 unsigned char tls_flag)
b49e97c9 3494{
a8028dd0 3495 struct mips_elf_link_hash_table *htab;
634835ae 3496 struct mips_elf_link_hash_entry *hmips;
f4416af6 3497 struct mips_got_entry entry, **loc;
a8028dd0
RS
3498 struct mips_got_info *g;
3499
3500 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3501 BFD_ASSERT (htab != NULL);
3502
634835ae 3503 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795
RS
3504 if (!for_call)
3505 hmips->got_only_for_calls = FALSE;
f4416af6 3506
b49e97c9
TS
3507 /* A global symbol in the GOT must also be in the dynamic symbol
3508 table. */
7c5fcef7
L
3509 if (h->dynindx == -1)
3510 {
3511 switch (ELF_ST_VISIBILITY (h->other))
3512 {
3513 case STV_INTERNAL:
3514 case STV_HIDDEN:
33bb52fb 3515 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3516 break;
3517 }
c152c796 3518 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3519 return FALSE;
7c5fcef7 3520 }
b49e97c9 3521
86324f90 3522 /* Make sure we have a GOT to put this entry into. */
a8028dd0 3523 g = htab->got_info;
86324f90
EC
3524 BFD_ASSERT (g != NULL);
3525
f4416af6
AO
3526 entry.abfd = abfd;
3527 entry.symndx = -1;
3528 entry.d.h = (struct mips_elf_link_hash_entry *) h;
0f20cc35 3529 entry.tls_type = 0;
f4416af6
AO
3530
3531 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3532 INSERT);
3533
b49e97c9
TS
3534 /* If we've already marked this entry as needing GOT space, we don't
3535 need to do it again. */
f4416af6 3536 if (*loc)
0f20cc35
DJ
3537 {
3538 (*loc)->tls_type |= tls_flag;
3539 return TRUE;
3540 }
f4416af6
AO
3541
3542 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3543
3544 if (! *loc)
3545 return FALSE;
143d77c5 3546
f4416af6 3547 entry.gotidx = -1;
0f20cc35
DJ
3548 entry.tls_type = tls_flag;
3549
f4416af6
AO
3550 memcpy (*loc, &entry, sizeof entry);
3551
0f20cc35 3552 if (tls_flag == 0)
634835ae 3553 hmips->global_got_area = GGA_NORMAL;
b49e97c9 3554
b34976b6 3555 return TRUE;
b49e97c9 3556}
f4416af6
AO
3557
3558/* Reserve space in G for a GOT entry containing the value of symbol
3559 SYMNDX in input bfd ABDF, plus ADDEND. */
3560
3561static bfd_boolean
9719ad41 3562mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
a8028dd0 3563 struct bfd_link_info *info,
0f20cc35 3564 unsigned char tls_flag)
f4416af6 3565{
a8028dd0
RS
3566 struct mips_elf_link_hash_table *htab;
3567 struct mips_got_info *g;
f4416af6
AO
3568 struct mips_got_entry entry, **loc;
3569
a8028dd0 3570 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3571 BFD_ASSERT (htab != NULL);
3572
a8028dd0
RS
3573 g = htab->got_info;
3574 BFD_ASSERT (g != NULL);
3575
f4416af6
AO
3576 entry.abfd = abfd;
3577 entry.symndx = symndx;
3578 entry.d.addend = addend;
0f20cc35 3579 entry.tls_type = tls_flag;
f4416af6
AO
3580 loc = (struct mips_got_entry **)
3581 htab_find_slot (g->got_entries, &entry, INSERT);
3582
3583 if (*loc)
0f20cc35
DJ
3584 {
3585 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3586 {
3587 g->tls_gotno += 2;
3588 (*loc)->tls_type |= tls_flag;
3589 }
3590 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3591 {
3592 g->tls_gotno += 1;
3593 (*loc)->tls_type |= tls_flag;
3594 }
3595 return TRUE;
3596 }
f4416af6 3597
0f20cc35
DJ
3598 if (tls_flag != 0)
3599 {
3600 entry.gotidx = -1;
3601 entry.tls_type = tls_flag;
3602 if (tls_flag == GOT_TLS_IE)
3603 g->tls_gotno += 1;
3604 else if (tls_flag == GOT_TLS_GD)
3605 g->tls_gotno += 2;
3606 else if (g->tls_ldm_offset == MINUS_ONE)
3607 {
3608 g->tls_ldm_offset = MINUS_TWO;
3609 g->tls_gotno += 2;
3610 }
3611 }
3612 else
3613 {
3614 entry.gotidx = g->local_gotno++;
3615 entry.tls_type = 0;
3616 }
f4416af6
AO
3617
3618 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3619
3620 if (! *loc)
3621 return FALSE;
143d77c5 3622
f4416af6
AO
3623 memcpy (*loc, &entry, sizeof entry);
3624
3625 return TRUE;
3626}
c224138d
RS
3627
3628/* Return the maximum number of GOT page entries required for RANGE. */
3629
3630static bfd_vma
3631mips_elf_pages_for_range (const struct mips_got_page_range *range)
3632{
3633 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3634}
3635
3a3b6725 3636/* Record that ABFD has a page relocation against symbol SYMNDX and
a8028dd0
RS
3637 that ADDEND is the addend for that relocation.
3638
3639 This function creates an upper bound on the number of GOT slots
3640 required; no attempt is made to combine references to non-overridable
3641 global symbols across multiple input files. */
c224138d
RS
3642
3643static bfd_boolean
a8028dd0
RS
3644mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3645 long symndx, bfd_signed_vma addend)
c224138d 3646{
a8028dd0
RS
3647 struct mips_elf_link_hash_table *htab;
3648 struct mips_got_info *g;
c224138d
RS
3649 struct mips_got_page_entry lookup, *entry;
3650 struct mips_got_page_range **range_ptr, *range;
3651 bfd_vma old_pages, new_pages;
3652 void **loc;
3653
a8028dd0 3654 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3655 BFD_ASSERT (htab != NULL);
3656
a8028dd0
RS
3657 g = htab->got_info;
3658 BFD_ASSERT (g != NULL);
3659
c224138d
RS
3660 /* Find the mips_got_page_entry hash table entry for this symbol. */
3661 lookup.abfd = abfd;
3662 lookup.symndx = symndx;
3663 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3664 if (loc == NULL)
3665 return FALSE;
3666
3667 /* Create a mips_got_page_entry if this is the first time we've
3668 seen the symbol. */
3669 entry = (struct mips_got_page_entry *) *loc;
3670 if (!entry)
3671 {
3672 entry = bfd_alloc (abfd, sizeof (*entry));
3673 if (!entry)
3674 return FALSE;
3675
3676 entry->abfd = abfd;
3677 entry->symndx = symndx;
3678 entry->ranges = NULL;
3679 entry->num_pages = 0;
3680 *loc = entry;
3681 }
3682
3683 /* Skip over ranges whose maximum extent cannot share a page entry
3684 with ADDEND. */
3685 range_ptr = &entry->ranges;
3686 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3687 range_ptr = &(*range_ptr)->next;
3688
3689 /* If we scanned to the end of the list, or found a range whose
3690 minimum extent cannot share a page entry with ADDEND, create
3691 a new singleton range. */
3692 range = *range_ptr;
3693 if (!range || addend < range->min_addend - 0xffff)
3694 {
3695 range = bfd_alloc (abfd, sizeof (*range));
3696 if (!range)
3697 return FALSE;
3698
3699 range->next = *range_ptr;
3700 range->min_addend = addend;
3701 range->max_addend = addend;
3702
3703 *range_ptr = range;
3704 entry->num_pages++;
3705 g->page_gotno++;
3706 return TRUE;
3707 }
3708
3709 /* Remember how many pages the old range contributed. */
3710 old_pages = mips_elf_pages_for_range (range);
3711
3712 /* Update the ranges. */
3713 if (addend < range->min_addend)
3714 range->min_addend = addend;
3715 else if (addend > range->max_addend)
3716 {
3717 if (range->next && addend >= range->next->min_addend - 0xffff)
3718 {
3719 old_pages += mips_elf_pages_for_range (range->next);
3720 range->max_addend = range->next->max_addend;
3721 range->next = range->next->next;
3722 }
3723 else
3724 range->max_addend = addend;
3725 }
3726
3727 /* Record any change in the total estimate. */
3728 new_pages = mips_elf_pages_for_range (range);
3729 if (old_pages != new_pages)
3730 {
3731 entry->num_pages += new_pages - old_pages;
3732 g->page_gotno += new_pages - old_pages;
3733 }
3734
3735 return TRUE;
3736}
33bb52fb
RS
3737
3738/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3739
3740static void
3741mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3742 unsigned int n)
3743{
3744 asection *s;
3745 struct mips_elf_link_hash_table *htab;
3746
3747 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3748 BFD_ASSERT (htab != NULL);
3749
33bb52fb
RS
3750 s = mips_elf_rel_dyn_section (info, FALSE);
3751 BFD_ASSERT (s != NULL);
3752
3753 if (htab->is_vxworks)
3754 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3755 else
3756 {
3757 if (s->size == 0)
3758 {
3759 /* Make room for a null element. */
3760 s->size += MIPS_ELF_REL_SIZE (abfd);
3761 ++s->reloc_count;
3762 }
3763 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3764 }
3765}
3766\f
3767/* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3768 if the GOT entry is for an indirect or warning symbol. */
3769
3770static int
3771mips_elf_check_recreate_got (void **entryp, void *data)
3772{
3773 struct mips_got_entry *entry;
3774 bfd_boolean *must_recreate;
3775
3776 entry = (struct mips_got_entry *) *entryp;
3777 must_recreate = (bfd_boolean *) data;
3778 if (entry->abfd != NULL && entry->symndx == -1)
3779 {
3780 struct mips_elf_link_hash_entry *h;
3781
3782 h = entry->d.h;
3783 if (h->root.root.type == bfd_link_hash_indirect
3784 || h->root.root.type == bfd_link_hash_warning)
3785 {
3786 *must_recreate = TRUE;
3787 return 0;
3788 }
3789 }
3790 return 1;
3791}
3792
3793/* A htab_traverse callback for GOT entries. Add all entries to
3794 hash table *DATA, converting entries for indirect and warning
3795 symbols into entries for the target symbol. Set *DATA to null
3796 on error. */
3797
3798static int
3799mips_elf_recreate_got (void **entryp, void *data)
3800{
3801 htab_t *new_got;
3802 struct mips_got_entry *entry;
3803 void **slot;
3804
3805 new_got = (htab_t *) data;
3806 entry = (struct mips_got_entry *) *entryp;
3807 if (entry->abfd != NULL && entry->symndx == -1)
3808 {
3809 struct mips_elf_link_hash_entry *h;
3810
3811 h = entry->d.h;
3812 while (h->root.root.type == bfd_link_hash_indirect
3813 || h->root.root.type == bfd_link_hash_warning)
634835ae
RS
3814 {
3815 BFD_ASSERT (h->global_got_area == GGA_NONE);
3816 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3817 }
33bb52fb
RS
3818 entry->d.h = h;
3819 }
3820 slot = htab_find_slot (*new_got, entry, INSERT);
3821 if (slot == NULL)
3822 {
3823 *new_got = NULL;
3824 return 0;
3825 }
3826 if (*slot == NULL)
3827 *slot = entry;
3828 else
3829 free (entry);
3830 return 1;
3831}
3832
3833/* If any entries in G->got_entries are for indirect or warning symbols,
3834 replace them with entries for the target symbol. */
3835
3836static bfd_boolean
3837mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3838{
3839 bfd_boolean must_recreate;
3840 htab_t new_got;
3841
3842 must_recreate = FALSE;
3843 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3844 if (must_recreate)
3845 {
3846 new_got = htab_create (htab_size (g->got_entries),
3847 mips_elf_got_entry_hash,
3848 mips_elf_got_entry_eq, NULL);
3849 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3850 if (new_got == NULL)
3851 return FALSE;
3852
3853 /* Each entry in g->got_entries has either been copied to new_got
3854 or freed. Now delete the hash table itself. */
3855 htab_delete (g->got_entries);
3856 g->got_entries = new_got;
3857 }
3858 return TRUE;
3859}
3860
634835ae 3861/* A mips_elf_link_hash_traverse callback for which DATA points
020d7251
RS
3862 to the link_info structure. Count the number of type (3) entries
3863 in the master GOT. */
33bb52fb
RS
3864
3865static int
d4596a51 3866mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 3867{
020d7251 3868 struct bfd_link_info *info;
6ccf4795 3869 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
3870 struct mips_got_info *g;
3871
020d7251 3872 info = (struct bfd_link_info *) data;
6ccf4795
RS
3873 htab = mips_elf_hash_table (info);
3874 g = htab->got_info;
d4596a51 3875 if (h->global_got_area != GGA_NONE)
33bb52fb 3876 {
020d7251
RS
3877 /* Make a final decision about whether the symbol belongs in the
3878 local or global GOT. Symbols that bind locally can (and in the
3879 case of forced-local symbols, must) live in the local GOT.
3880 Those that are aren't in the dynamic symbol table must also
3881 live in the local GOT.
3882
3883 Note that the former condition does not always imply the
3884 latter: symbols do not bind locally if they are completely
3885 undefined. We'll report undefined symbols later if appropriate. */
6ccf4795
RS
3886 if (h->root.dynindx == -1
3887 || (h->got_only_for_calls
3888 ? SYMBOL_CALLS_LOCAL (info, &h->root)
3889 : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
d4596a51 3890 {
020d7251
RS
3891 /* The symbol belongs in the local GOT. We no longer need this
3892 entry if it was only used for relocations; those relocations
3893 will be against the null or section symbol instead of H. */
d4596a51
RS
3894 if (h->global_got_area != GGA_RELOC_ONLY)
3895 g->local_gotno++;
3896 h->global_got_area = GGA_NONE;
3897 }
6ccf4795
RS
3898 else if (htab->is_vxworks
3899 && h->got_only_for_calls
3900 && h->root.plt.offset != MINUS_ONE)
3901 /* On VxWorks, calls can refer directly to the .got.plt entry;
3902 they don't need entries in the regular GOT. .got.plt entries
3903 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
3904 h->global_got_area = GGA_NONE;
d4596a51 3905 else
23cc69b6
RS
3906 {
3907 g->global_gotno++;
3908 if (h->global_got_area == GGA_RELOC_ONLY)
3909 g->reloc_only_gotno++;
3910 }
33bb52fb
RS
3911 }
3912 return 1;
3913}
f4416af6
AO
3914\f
3915/* Compute the hash value of the bfd in a bfd2got hash entry. */
3916
3917static hashval_t
9719ad41 3918mips_elf_bfd2got_entry_hash (const void *entry_)
f4416af6
AO
3919{
3920 const struct mips_elf_bfd2got_hash *entry
3921 = (struct mips_elf_bfd2got_hash *)entry_;
3922
3923 return entry->bfd->id;
3924}
3925
3926/* Check whether two hash entries have the same bfd. */
3927
3928static int
9719ad41 3929mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3930{
3931 const struct mips_elf_bfd2got_hash *e1
3932 = (const struct mips_elf_bfd2got_hash *)entry1;
3933 const struct mips_elf_bfd2got_hash *e2
3934 = (const struct mips_elf_bfd2got_hash *)entry2;
3935
3936 return e1->bfd == e2->bfd;
3937}
3938
bad36eac 3939/* In a multi-got link, determine the GOT to be used for IBFD. G must
f4416af6
AO
3940 be the master GOT data. */
3941
3942static struct mips_got_info *
9719ad41 3943mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
3944{
3945 struct mips_elf_bfd2got_hash e, *p;
3946
3947 if (! g->bfd2got)
3948 return g;
3949
3950 e.bfd = ibfd;
9719ad41 3951 p = htab_find (g->bfd2got, &e);
f4416af6
AO
3952 return p ? p->g : NULL;
3953}
3954
c224138d
RS
3955/* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3956 Return NULL if an error occured. */
f4416af6 3957
c224138d
RS
3958static struct mips_got_info *
3959mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3960 bfd *input_bfd)
f4416af6 3961{
f4416af6 3962 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
c224138d 3963 struct mips_got_info *g;
f4416af6 3964 void **bfdgotp;
143d77c5 3965
c224138d 3966 bfdgot_entry.bfd = input_bfd;
f4416af6 3967 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
c224138d 3968 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
f4416af6 3969
c224138d 3970 if (bfdgot == NULL)
f4416af6 3971 {
c224138d
RS
3972 bfdgot = ((struct mips_elf_bfd2got_hash *)
3973 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
f4416af6 3974 if (bfdgot == NULL)
c224138d 3975 return NULL;
f4416af6
AO
3976
3977 *bfdgotp = bfdgot;
3978
c224138d
RS
3979 g = ((struct mips_got_info *)
3980 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
f4416af6 3981 if (g == NULL)
c224138d
RS
3982 return NULL;
3983
3984 bfdgot->bfd = input_bfd;
3985 bfdgot->g = g;
f4416af6
AO
3986
3987 g->global_gotsym = NULL;
3988 g->global_gotno = 0;
23cc69b6 3989 g->reloc_only_gotno = 0;
f4416af6 3990 g->local_gotno = 0;
c224138d 3991 g->page_gotno = 0;
f4416af6 3992 g->assigned_gotno = -1;
0f20cc35
DJ
3993 g->tls_gotno = 0;
3994 g->tls_assigned_gotno = 0;
3995 g->tls_ldm_offset = MINUS_ONE;
f4416af6 3996 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
9719ad41 3997 mips_elf_multi_got_entry_eq, NULL);
f4416af6 3998 if (g->got_entries == NULL)
c224138d
RS
3999 return NULL;
4000
4001 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4002 mips_got_page_entry_eq, NULL);
4003 if (g->got_page_entries == NULL)
4004 return NULL;
f4416af6
AO
4005
4006 g->bfd2got = NULL;
4007 g->next = NULL;
4008 }
4009
c224138d
RS
4010 return bfdgot->g;
4011}
4012
4013/* A htab_traverse callback for the entries in the master got.
4014 Create one separate got for each bfd that has entries in the global
4015 got, such that we can tell how many local and global entries each
4016 bfd requires. */
4017
4018static int
4019mips_elf_make_got_per_bfd (void **entryp, void *p)
4020{
4021 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4022 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4023 struct mips_got_info *g;
4024
4025 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4026 if (g == NULL)
4027 {
4028 arg->obfd = NULL;
4029 return 0;
4030 }
4031
f4416af6
AO
4032 /* Insert the GOT entry in the bfd's got entry hash table. */
4033 entryp = htab_find_slot (g->got_entries, entry, INSERT);
4034 if (*entryp != NULL)
4035 return 1;
143d77c5 4036
f4416af6
AO
4037 *entryp = entry;
4038
0f20cc35
DJ
4039 if (entry->tls_type)
4040 {
4041 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4042 g->tls_gotno += 2;
4043 if (entry->tls_type & GOT_TLS_IE)
4044 g->tls_gotno += 1;
4045 }
020d7251 4046 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
f4416af6
AO
4047 ++g->local_gotno;
4048 else
4049 ++g->global_gotno;
4050
4051 return 1;
4052}
4053
c224138d
RS
4054/* A htab_traverse callback for the page entries in the master got.
4055 Associate each page entry with the bfd's got. */
4056
4057static int
4058mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
4059{
4060 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
4061 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
4062 struct mips_got_info *g;
4063
4064 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4065 if (g == NULL)
4066 {
4067 arg->obfd = NULL;
4068 return 0;
4069 }
4070
4071 /* Insert the GOT entry in the bfd's got entry hash table. */
4072 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4073 if (*entryp != NULL)
4074 return 1;
4075
4076 *entryp = entry;
4077 g->page_gotno += entry->num_pages;
4078 return 1;
4079}
4080
4081/* Consider merging the got described by BFD2GOT with TO, using the
4082 information given by ARG. Return -1 if this would lead to overflow,
4083 1 if they were merged successfully, and 0 if a merge failed due to
4084 lack of memory. (These values are chosen so that nonnegative return
4085 values can be returned by a htab_traverse callback.) */
4086
4087static int
4088mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4089 struct mips_got_info *to,
4090 struct mips_elf_got_per_bfd_arg *arg)
4091{
4092 struct mips_got_info *from = bfd2got->g;
4093 unsigned int estimate;
4094
4095 /* Work out how many page entries we would need for the combined GOT. */
4096 estimate = arg->max_pages;
4097 if (estimate >= from->page_gotno + to->page_gotno)
4098 estimate = from->page_gotno + to->page_gotno;
4099
4100 /* And conservatively estimate how many local, global and TLS entries
4101 would be needed. */
4102 estimate += (from->local_gotno
4103 + from->global_gotno
4104 + from->tls_gotno
4105 + to->local_gotno
4106 + to->global_gotno
4107 + to->tls_gotno);
4108
4109 /* Bail out if the combined GOT might be too big. */
4110 if (estimate > arg->max_count)
4111 return -1;
4112
4113 /* Commit to the merge. Record that TO is now the bfd for this got. */
4114 bfd2got->g = to;
4115
4116 /* Transfer the bfd's got information from FROM to TO. */
4117 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4118 if (arg->obfd == NULL)
4119 return 0;
4120
4121 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4122 if (arg->obfd == NULL)
4123 return 0;
4124
4125 /* We don't have to worry about releasing memory of the actual
4126 got entries, since they're all in the master got_entries hash
4127 table anyway. */
4128 htab_delete (from->got_entries);
4129 htab_delete (from->got_page_entries);
4130 return 1;
4131}
4132
f4416af6
AO
4133/* Attempt to merge gots of different input bfds. Try to use as much
4134 as possible of the primary got, since it doesn't require explicit
4135 dynamic relocations, but don't use bfds that would reference global
4136 symbols out of the addressable range. Failing the primary got,
4137 attempt to merge with the current got, or finish the current got
4138 and then make make the new got current. */
4139
4140static int
9719ad41 4141mips_elf_merge_gots (void **bfd2got_, void *p)
f4416af6
AO
4142{
4143 struct mips_elf_bfd2got_hash *bfd2got
4144 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4145 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
c224138d
RS
4146 struct mips_got_info *g;
4147 unsigned int estimate;
4148 int result;
4149
4150 g = bfd2got->g;
4151
4152 /* Work out the number of page, local and TLS entries. */
4153 estimate = arg->max_pages;
4154 if (estimate > g->page_gotno)
4155 estimate = g->page_gotno;
4156 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4157
4158 /* We place TLS GOT entries after both locals and globals. The globals
4159 for the primary GOT may overflow the normal GOT size limit, so be
4160 sure not to merge a GOT which requires TLS with the primary GOT in that
4161 case. This doesn't affect non-primary GOTs. */
c224138d 4162 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4163
c224138d 4164 if (estimate <= arg->max_count)
f4416af6 4165 {
c224138d
RS
4166 /* If we don't have a primary GOT, use it as
4167 a starting point for the primary GOT. */
4168 if (!arg->primary)
4169 {
4170 arg->primary = bfd2got->g;
4171 return 1;
4172 }
f4416af6 4173
c224138d
RS
4174 /* Try merging with the primary GOT. */
4175 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4176 if (result >= 0)
4177 return result;
f4416af6 4178 }
c224138d 4179
f4416af6 4180 /* If we can merge with the last-created got, do it. */
c224138d 4181 if (arg->current)
f4416af6 4182 {
c224138d
RS
4183 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4184 if (result >= 0)
4185 return result;
f4416af6 4186 }
c224138d 4187
f4416af6
AO
4188 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4189 fits; if it turns out that it doesn't, we'll get relocation
4190 overflows anyway. */
c224138d
RS
4191 g->next = arg->current;
4192 arg->current = g;
0f20cc35
DJ
4193
4194 return 1;
4195}
4196
ead49a57
RS
4197/* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4198 is null iff there is just a single GOT. */
0f20cc35
DJ
4199
4200static int
4201mips_elf_initialize_tls_index (void **entryp, void *p)
4202{
4203 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4204 struct mips_got_info *g = p;
ead49a57 4205 bfd_vma next_index;
cbf2cba4 4206 unsigned char tls_type;
0f20cc35
DJ
4207
4208 /* We're only interested in TLS symbols. */
4209 if (entry->tls_type == 0)
4210 return 1;
4211
ead49a57
RS
4212 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4213
4214 if (entry->symndx == -1 && g->next == NULL)
0f20cc35 4215 {
ead49a57
RS
4216 /* A type (3) got entry in the single-GOT case. We use the symbol's
4217 hash table entry to track its index. */
4218 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4219 return 1;
4220 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4221 entry->d.h->tls_got_offset = next_index;
cbf2cba4 4222 tls_type = entry->d.h->tls_type;
ead49a57
RS
4223 }
4224 else
4225 {
4226 if (entry->tls_type & GOT_TLS_LDM)
0f20cc35 4227 {
ead49a57
RS
4228 /* There are separate mips_got_entry objects for each input bfd
4229 that requires an LDM entry. Make sure that all LDM entries in
4230 a GOT resolve to the same index. */
4231 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4005427f 4232 {
ead49a57 4233 entry->gotidx = g->tls_ldm_offset;
4005427f
RS
4234 return 1;
4235 }
ead49a57 4236 g->tls_ldm_offset = next_index;
0f20cc35 4237 }
ead49a57 4238 entry->gotidx = next_index;
cbf2cba4 4239 tls_type = entry->tls_type;
f4416af6
AO
4240 }
4241
ead49a57 4242 /* Account for the entries we've just allocated. */
cbf2cba4 4243 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
0f20cc35 4244 g->tls_assigned_gotno += 2;
cbf2cba4 4245 if (tls_type & GOT_TLS_IE)
0f20cc35
DJ
4246 g->tls_assigned_gotno += 1;
4247
f4416af6
AO
4248 return 1;
4249}
4250
4251/* If passed a NULL mips_got_info in the argument, set the marker used
4252 to tell whether a global symbol needs a got entry (in the primary
4253 got) to the given VALUE.
4254
4255 If passed a pointer G to a mips_got_info in the argument (it must
4256 not be the primary GOT), compute the offset from the beginning of
4257 the (primary) GOT section to the entry in G corresponding to the
4258 global symbol. G's assigned_gotno must contain the index of the
4259 first available global GOT entry in G. VALUE must contain the size
4260 of a GOT entry in bytes. For each global GOT entry that requires a
4261 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4cc11e76 4262 marked as not eligible for lazy resolution through a function
f4416af6
AO
4263 stub. */
4264static int
9719ad41 4265mips_elf_set_global_got_offset (void **entryp, void *p)
f4416af6
AO
4266{
4267 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4268 struct mips_elf_set_global_got_offset_arg *arg
4269 = (struct mips_elf_set_global_got_offset_arg *)p;
4270 struct mips_got_info *g = arg->g;
4271
0f20cc35
DJ
4272 if (g && entry->tls_type != GOT_NORMAL)
4273 arg->needed_relocs +=
4274 mips_tls_got_relocs (arg->info, entry->tls_type,
4275 entry->symndx == -1 ? &entry->d.h->root : NULL);
4276
634835ae
RS
4277 if (entry->abfd != NULL
4278 && entry->symndx == -1
4279 && entry->d.h->global_got_area != GGA_NONE)
f4416af6
AO
4280 {
4281 if (g)
4282 {
4283 BFD_ASSERT (g->global_gotsym == NULL);
4284
4285 entry->gotidx = arg->value * (long) g->assigned_gotno++;
f4416af6
AO
4286 if (arg->info->shared
4287 || (elf_hash_table (arg->info)->dynamic_sections_created
f5385ebf
AM
4288 && entry->d.h->root.def_dynamic
4289 && !entry->d.h->root.def_regular))
f4416af6
AO
4290 ++arg->needed_relocs;
4291 }
4292 else
634835ae 4293 entry->d.h->global_got_area = arg->value;
f4416af6
AO
4294 }
4295
4296 return 1;
4297}
4298
33bb52fb
RS
4299/* A htab_traverse callback for GOT entries for which DATA is the
4300 bfd_link_info. Forbid any global symbols from having traditional
4301 lazy-binding stubs. */
4302
0626d451 4303static int
33bb52fb 4304mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4305{
33bb52fb
RS
4306 struct bfd_link_info *info;
4307 struct mips_elf_link_hash_table *htab;
4308 struct mips_got_entry *entry;
0626d451 4309
33bb52fb
RS
4310 entry = (struct mips_got_entry *) *entryp;
4311 info = (struct bfd_link_info *) data;
4312 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4313 BFD_ASSERT (htab != NULL);
4314
0626d451
RS
4315 if (entry->abfd != NULL
4316 && entry->symndx == -1
33bb52fb 4317 && entry->d.h->needs_lazy_stub)
f4416af6 4318 {
33bb52fb
RS
4319 entry->d.h->needs_lazy_stub = FALSE;
4320 htab->lazy_stub_count--;
f4416af6 4321 }
143d77c5 4322
f4416af6
AO
4323 return 1;
4324}
4325
f4416af6
AO
4326/* Return the offset of an input bfd IBFD's GOT from the beginning of
4327 the primary GOT. */
4328static bfd_vma
9719ad41 4329mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
4330{
4331 if (g->bfd2got == NULL)
4332 return 0;
4333
4334 g = mips_elf_got_for_ibfd (g, ibfd);
4335 if (! g)
4336 return 0;
4337
4338 BFD_ASSERT (g->next);
4339
4340 g = g->next;
143d77c5 4341
0f20cc35
DJ
4342 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4343 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4344}
4345
4346/* Turn a single GOT that is too big for 16-bit addressing into
4347 a sequence of GOTs, each one 16-bit addressable. */
4348
4349static bfd_boolean
9719ad41 4350mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4351 asection *got, bfd_size_type pages)
f4416af6 4352{
a8028dd0 4353 struct mips_elf_link_hash_table *htab;
f4416af6
AO
4354 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4355 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
a8028dd0 4356 struct mips_got_info *g, *gg;
33bb52fb
RS
4357 unsigned int assign, needed_relocs;
4358 bfd *dynobj;
f4416af6 4359
33bb52fb 4360 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4361 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4362 BFD_ASSERT (htab != NULL);
4363
a8028dd0 4364 g = htab->got_info;
f4416af6 4365 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
9719ad41 4366 mips_elf_bfd2got_entry_eq, NULL);
f4416af6
AO
4367 if (g->bfd2got == NULL)
4368 return FALSE;
4369
4370 got_per_bfd_arg.bfd2got = g->bfd2got;
4371 got_per_bfd_arg.obfd = abfd;
4372 got_per_bfd_arg.info = info;
4373
4374 /* Count how many GOT entries each input bfd requires, creating a
4375 map from bfd to got info while at that. */
f4416af6
AO
4376 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4377 if (got_per_bfd_arg.obfd == NULL)
4378 return FALSE;
4379
c224138d
RS
4380 /* Also count how many page entries each input bfd requires. */
4381 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4382 &got_per_bfd_arg);
4383 if (got_per_bfd_arg.obfd == NULL)
4384 return FALSE;
4385
f4416af6
AO
4386 got_per_bfd_arg.current = NULL;
4387 got_per_bfd_arg.primary = NULL;
0a44bf69 4388 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4389 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4390 - htab->reserved_gotno);
c224138d 4391 got_per_bfd_arg.max_pages = pages;
0f20cc35
DJ
4392 /* The number of globals that will be included in the primary GOT.
4393 See the calls to mips_elf_set_global_got_offset below for more
4394 information. */
4395 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4396
4397 /* Try to merge the GOTs of input bfds together, as long as they
4398 don't seem to exceed the maximum GOT size, choosing one of them
4399 to be the primary GOT. */
4400 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4401 if (got_per_bfd_arg.obfd == NULL)
4402 return FALSE;
4403
0f20cc35 4404 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6
AO
4405 if (got_per_bfd_arg.primary == NULL)
4406 {
4407 g->next = (struct mips_got_info *)
4408 bfd_alloc (abfd, sizeof (struct mips_got_info));
4409 if (g->next == NULL)
4410 return FALSE;
4411
4412 g->next->global_gotsym = NULL;
4413 g->next->global_gotno = 0;
23cc69b6 4414 g->next->reloc_only_gotno = 0;
f4416af6 4415 g->next->local_gotno = 0;
c224138d 4416 g->next->page_gotno = 0;
0f20cc35 4417 g->next->tls_gotno = 0;
f4416af6 4418 g->next->assigned_gotno = 0;
0f20cc35
DJ
4419 g->next->tls_assigned_gotno = 0;
4420 g->next->tls_ldm_offset = MINUS_ONE;
f4416af6
AO
4421 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4422 mips_elf_multi_got_entry_eq,
9719ad41 4423 NULL);
f4416af6
AO
4424 if (g->next->got_entries == NULL)
4425 return FALSE;
c224138d
RS
4426 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4427 mips_got_page_entry_eq,
4428 NULL);
4429 if (g->next->got_page_entries == NULL)
4430 return FALSE;
f4416af6
AO
4431 g->next->bfd2got = NULL;
4432 }
4433 else
4434 g->next = got_per_bfd_arg.primary;
4435 g->next->next = got_per_bfd_arg.current;
4436
4437 /* GG is now the master GOT, and G is the primary GOT. */
4438 gg = g;
4439 g = g->next;
4440
4441 /* Map the output bfd to the primary got. That's what we're going
4442 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4443 didn't mark in check_relocs, and we want a quick way to find it.
4444 We can't just use gg->next because we're going to reverse the
4445 list. */
4446 {
4447 struct mips_elf_bfd2got_hash *bfdgot;
4448 void **bfdgotp;
143d77c5 4449
f4416af6
AO
4450 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4451 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4452
4453 if (bfdgot == NULL)
4454 return FALSE;
4455
4456 bfdgot->bfd = abfd;
4457 bfdgot->g = g;
4458 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4459
4460 BFD_ASSERT (*bfdgotp == NULL);
4461 *bfdgotp = bfdgot;
4462 }
4463
634835ae
RS
4464 /* Every symbol that is referenced in a dynamic relocation must be
4465 present in the primary GOT, so arrange for them to appear after
4466 those that are actually referenced. */
23cc69b6 4467 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4468 g->global_gotno = gg->global_gotno;
f4416af6 4469
f4416af6 4470 set_got_offset_arg.g = NULL;
634835ae 4471 set_got_offset_arg.value = GGA_RELOC_ONLY;
f4416af6
AO
4472 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4473 &set_got_offset_arg);
634835ae 4474 set_got_offset_arg.value = GGA_NORMAL;
f4416af6
AO
4475 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4476 &set_got_offset_arg);
f4416af6
AO
4477
4478 /* Now go through the GOTs assigning them offset ranges.
4479 [assigned_gotno, local_gotno[ will be set to the range of local
4480 entries in each GOT. We can then compute the end of a GOT by
4481 adding local_gotno to global_gotno. We reverse the list and make
4482 it circular since then we'll be able to quickly compute the
4483 beginning of a GOT, by computing the end of its predecessor. To
4484 avoid special cases for the primary GOT, while still preserving
4485 assertions that are valid for both single- and multi-got links,
4486 we arrange for the main got struct to have the right number of
4487 global entries, but set its local_gotno such that the initial
4488 offset of the primary GOT is zero. Remember that the primary GOT
4489 will become the last item in the circular linked list, so it
4490 points back to the master GOT. */
4491 gg->local_gotno = -g->global_gotno;
4492 gg->global_gotno = g->global_gotno;
0f20cc35 4493 gg->tls_gotno = 0;
f4416af6
AO
4494 assign = 0;
4495 gg->next = gg;
4496
4497 do
4498 {
4499 struct mips_got_info *gn;
4500
861fb55a 4501 assign += htab->reserved_gotno;
f4416af6 4502 g->assigned_gotno = assign;
c224138d
RS
4503 g->local_gotno += assign;
4504 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4505 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4506
ead49a57
RS
4507 /* Take g out of the direct list, and push it onto the reversed
4508 list that gg points to. g->next is guaranteed to be nonnull after
4509 this operation, as required by mips_elf_initialize_tls_index. */
4510 gn = g->next;
4511 g->next = gg->next;
4512 gg->next = g;
4513
0f20cc35
DJ
4514 /* Set up any TLS entries. We always place the TLS entries after
4515 all non-TLS entries. */
4516 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4517 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
f4416af6 4518
ead49a57 4519 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4520 g = gn;
0626d451 4521
33bb52fb
RS
4522 /* Forbid global symbols in every non-primary GOT from having
4523 lazy-binding stubs. */
0626d451 4524 if (g)
33bb52fb 4525 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4526 }
4527 while (g);
4528
eea6121a 4529 got->size = (gg->next->local_gotno
33bb52fb
RS
4530 + gg->next->global_gotno
4531 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4532
4533 needed_relocs = 0;
4534 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4535 set_got_offset_arg.info = info;
4536 for (g = gg->next; g && g->next != gg; g = g->next)
4537 {
4538 unsigned int save_assign;
4539
4540 /* Assign offsets to global GOT entries. */
4541 save_assign = g->assigned_gotno;
4542 g->assigned_gotno = g->local_gotno;
4543 set_got_offset_arg.g = g;
4544 set_got_offset_arg.needed_relocs = 0;
4545 htab_traverse (g->got_entries,
4546 mips_elf_set_global_got_offset,
4547 &set_got_offset_arg);
4548 needed_relocs += set_got_offset_arg.needed_relocs;
4549 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4550
4551 g->assigned_gotno = save_assign;
4552 if (info->shared)
4553 {
4554 needed_relocs += g->local_gotno - g->assigned_gotno;
4555 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4556 + g->next->global_gotno
4557 + g->next->tls_gotno
861fb55a 4558 + htab->reserved_gotno);
33bb52fb
RS
4559 }
4560 }
4561
4562 if (needed_relocs)
4563 mips_elf_allocate_dynamic_relocations (dynobj, info,
4564 needed_relocs);
143d77c5 4565
f4416af6
AO
4566 return TRUE;
4567}
143d77c5 4568
b49e97c9
TS
4569\f
4570/* Returns the first relocation of type r_type found, beginning with
4571 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4572
4573static const Elf_Internal_Rela *
9719ad41
RS
4574mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4575 const Elf_Internal_Rela *relocation,
4576 const Elf_Internal_Rela *relend)
b49e97c9 4577{
c000e262
TS
4578 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4579
b49e97c9
TS
4580 while (relocation < relend)
4581 {
c000e262
TS
4582 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4583 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4584 return relocation;
4585
4586 ++relocation;
4587 }
4588
4589 /* We didn't find it. */
b49e97c9
TS
4590 return NULL;
4591}
4592
020d7251 4593/* Return whether an input relocation is against a local symbol. */
b49e97c9 4594
b34976b6 4595static bfd_boolean
9719ad41
RS
4596mips_elf_local_relocation_p (bfd *input_bfd,
4597 const Elf_Internal_Rela *relocation,
020d7251 4598 asection **local_sections)
b49e97c9
TS
4599{
4600 unsigned long r_symndx;
4601 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
4602 size_t extsymoff;
4603
4604 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4605 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4606 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4607
4608 if (r_symndx < extsymoff)
b34976b6 4609 return TRUE;
b49e97c9 4610 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4611 return TRUE;
b49e97c9 4612
b34976b6 4613 return FALSE;
b49e97c9
TS
4614}
4615\f
4616/* Sign-extend VALUE, which has the indicated number of BITS. */
4617
a7ebbfdf 4618bfd_vma
9719ad41 4619_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4620{
4621 if (value & ((bfd_vma) 1 << (bits - 1)))
4622 /* VALUE is negative. */
4623 value |= ((bfd_vma) - 1) << bits;
4624
4625 return value;
4626}
4627
4628/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4629 range expressible by a signed number with the indicated number of
b49e97c9
TS
4630 BITS. */
4631
b34976b6 4632static bfd_boolean
9719ad41 4633mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4634{
4635 bfd_signed_vma svalue = (bfd_signed_vma) value;
4636
4637 if (svalue > (1 << (bits - 1)) - 1)
4638 /* The value is too big. */
b34976b6 4639 return TRUE;
b49e97c9
TS
4640 else if (svalue < -(1 << (bits - 1)))
4641 /* The value is too small. */
b34976b6 4642 return TRUE;
b49e97c9
TS
4643
4644 /* All is well. */
b34976b6 4645 return FALSE;
b49e97c9
TS
4646}
4647
4648/* Calculate the %high function. */
4649
4650static bfd_vma
9719ad41 4651mips_elf_high (bfd_vma value)
b49e97c9
TS
4652{
4653 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4654}
4655
4656/* Calculate the %higher function. */
4657
4658static bfd_vma
9719ad41 4659mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4660{
4661#ifdef BFD64
4662 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4663#else
4664 abort ();
c5ae1840 4665 return MINUS_ONE;
b49e97c9
TS
4666#endif
4667}
4668
4669/* Calculate the %highest function. */
4670
4671static bfd_vma
9719ad41 4672mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4673{
4674#ifdef BFD64
b15e6682 4675 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4676#else
4677 abort ();
c5ae1840 4678 return MINUS_ONE;
b49e97c9
TS
4679#endif
4680}
4681\f
4682/* Create the .compact_rel section. */
4683
b34976b6 4684static bfd_boolean
9719ad41
RS
4685mips_elf_create_compact_rel_section
4686 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4687{
4688 flagword flags;
4689 register asection *s;
4690
4691 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4692 {
4693 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4694 | SEC_READONLY);
4695
3496cb2a 4696 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4697 if (s == NULL
b49e97c9
TS
4698 || ! bfd_set_section_alignment (abfd, s,
4699 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4700 return FALSE;
b49e97c9 4701
eea6121a 4702 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4703 }
4704
b34976b6 4705 return TRUE;
b49e97c9
TS
4706}
4707
4708/* Create the .got section to hold the global offset table. */
4709
b34976b6 4710static bfd_boolean
23cc69b6 4711mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
4712{
4713 flagword flags;
4714 register asection *s;
4715 struct elf_link_hash_entry *h;
14a793b2 4716 struct bfd_link_hash_entry *bh;
b49e97c9
TS
4717 struct mips_got_info *g;
4718 bfd_size_type amt;
0a44bf69
RS
4719 struct mips_elf_link_hash_table *htab;
4720
4721 htab = mips_elf_hash_table (info);
4dfe6ac6 4722 BFD_ASSERT (htab != NULL);
b49e97c9
TS
4723
4724 /* This function may be called more than once. */
23cc69b6
RS
4725 if (htab->sgot)
4726 return TRUE;
b49e97c9
TS
4727
4728 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4729 | SEC_LINKER_CREATED);
4730
72b4917c
TS
4731 /* We have to use an alignment of 2**4 here because this is hardcoded
4732 in the function stub generation and in the linker script. */
3496cb2a 4733 s = bfd_make_section_with_flags (abfd, ".got", flags);
b49e97c9 4734 if (s == NULL
72b4917c 4735 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 4736 return FALSE;
a8028dd0 4737 htab->sgot = s;
b49e97c9
TS
4738
4739 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4740 linker script because we don't want to define the symbol if we
4741 are not creating a global offset table. */
14a793b2 4742 bh = NULL;
b49e97c9
TS
4743 if (! (_bfd_generic_link_add_one_symbol
4744 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 4745 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 4746 return FALSE;
14a793b2
AM
4747
4748 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
4749 h->non_elf = 0;
4750 h->def_regular = 1;
b49e97c9 4751 h->type = STT_OBJECT;
d329bcd1 4752 elf_hash_table (info)->hgot = h;
b49e97c9
TS
4753
4754 if (info->shared
c152c796 4755 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4756 return FALSE;
b49e97c9 4757
b49e97c9 4758 amt = sizeof (struct mips_got_info);
9719ad41 4759 g = bfd_alloc (abfd, amt);
b49e97c9 4760 if (g == NULL)
b34976b6 4761 return FALSE;
b49e97c9 4762 g->global_gotsym = NULL;
e3d54347 4763 g->global_gotno = 0;
23cc69b6 4764 g->reloc_only_gotno = 0;
0f20cc35 4765 g->tls_gotno = 0;
861fb55a 4766 g->local_gotno = 0;
c224138d 4767 g->page_gotno = 0;
861fb55a 4768 g->assigned_gotno = 0;
f4416af6
AO
4769 g->bfd2got = NULL;
4770 g->next = NULL;
0f20cc35 4771 g->tls_ldm_offset = MINUS_ONE;
b15e6682 4772 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
9719ad41 4773 mips_elf_got_entry_eq, NULL);
b15e6682
AO
4774 if (g->got_entries == NULL)
4775 return FALSE;
c224138d
RS
4776 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4777 mips_got_page_entry_eq, NULL);
4778 if (g->got_page_entries == NULL)
4779 return FALSE;
a8028dd0 4780 htab->got_info = g;
f0abc2a1 4781 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
4782 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4783
861fb55a
DJ
4784 /* We also need a .got.plt section when generating PLTs. */
4785 s = bfd_make_section_with_flags (abfd, ".got.plt",
4786 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4787 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4788 if (s == NULL)
4789 return FALSE;
4790 htab->sgotplt = s;
0a44bf69 4791
b34976b6 4792 return TRUE;
b49e97c9 4793}
b49e97c9 4794\f
0a44bf69
RS
4795/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4796 __GOTT_INDEX__ symbols. These symbols are only special for
4797 shared objects; they are not used in executables. */
4798
4799static bfd_boolean
4800is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4801{
4802 return (mips_elf_hash_table (info)->is_vxworks
4803 && info->shared
4804 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4805 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4806}
861fb55a
DJ
4807
4808/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4809 require an la25 stub. See also mips_elf_local_pic_function_p,
4810 which determines whether the destination function ever requires a
4811 stub. */
4812
4813static bfd_boolean
4814mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4815{
4816 /* We specifically ignore branches and jumps from EF_PIC objects,
4817 where the onus is on the compiler or programmer to perform any
4818 necessary initialization of $25. Sometimes such initialization
4819 is unnecessary; for example, -mno-shared functions do not use
4820 the incoming value of $25, and may therefore be called directly. */
4821 if (PIC_OBJECT_P (input_bfd))
4822 return FALSE;
4823
4824 switch (r_type)
4825 {
4826 case R_MIPS_26:
4827 case R_MIPS_PC16:
4828 case R_MIPS16_26:
4829 return TRUE;
4830
4831 default:
4832 return FALSE;
4833 }
4834}
0a44bf69 4835\f
b49e97c9
TS
4836/* Calculate the value produced by the RELOCATION (which comes from
4837 the INPUT_BFD). The ADDEND is the addend to use for this
4838 RELOCATION; RELOCATION->R_ADDEND is ignored.
4839
4840 The result of the relocation calculation is stored in VALUEP.
38a7df63
CF
4841 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4842 is a MIPS16 jump to non-MIPS16 code, or vice versa.
b49e97c9
TS
4843
4844 This function returns bfd_reloc_continue if the caller need take no
4845 further action regarding this relocation, bfd_reloc_notsupported if
4846 something goes dramatically wrong, bfd_reloc_overflow if an
4847 overflow occurs, and bfd_reloc_ok to indicate success. */
4848
4849static bfd_reloc_status_type
9719ad41
RS
4850mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4851 asection *input_section,
4852 struct bfd_link_info *info,
4853 const Elf_Internal_Rela *relocation,
4854 bfd_vma addend, reloc_howto_type *howto,
4855 Elf_Internal_Sym *local_syms,
4856 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
4857 const char **namep,
4858 bfd_boolean *cross_mode_jump_p,
9719ad41 4859 bfd_boolean save_addend)
b49e97c9
TS
4860{
4861 /* The eventual value we will return. */
4862 bfd_vma value;
4863 /* The address of the symbol against which the relocation is
4864 occurring. */
4865 bfd_vma symbol = 0;
4866 /* The final GP value to be used for the relocatable, executable, or
4867 shared object file being produced. */
0a61c8c2 4868 bfd_vma gp;
b49e97c9
TS
4869 /* The place (section offset or address) of the storage unit being
4870 relocated. */
4871 bfd_vma p;
4872 /* The value of GP used to create the relocatable object. */
0a61c8c2 4873 bfd_vma gp0;
b49e97c9
TS
4874 /* The offset into the global offset table at which the address of
4875 the relocation entry symbol, adjusted by the addend, resides
4876 during execution. */
4877 bfd_vma g = MINUS_ONE;
4878 /* The section in which the symbol referenced by the relocation is
4879 located. */
4880 asection *sec = NULL;
4881 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 4882 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 4883 symbol. */
b34976b6
AM
4884 bfd_boolean local_p, was_local_p;
4885 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4886 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
4887 /* TRUE if the symbol referred to by this relocation is
4888 "__gnu_local_gp". */
4889 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
4890 Elf_Internal_Shdr *symtab_hdr;
4891 size_t extsymoff;
4892 unsigned long r_symndx;
4893 int r_type;
b34976b6 4894 /* TRUE if overflow occurred during the calculation of the
b49e97c9 4895 relocation value. */
b34976b6
AM
4896 bfd_boolean overflowed_p;
4897 /* TRUE if this relocation refers to a MIPS16 function. */
4898 bfd_boolean target_is_16_bit_code_p = FALSE;
0a44bf69
RS
4899 struct mips_elf_link_hash_table *htab;
4900 bfd *dynobj;
4901
4902 dynobj = elf_hash_table (info)->dynobj;
4903 htab = mips_elf_hash_table (info);
4dfe6ac6 4904 BFD_ASSERT (htab != NULL);
b49e97c9
TS
4905
4906 /* Parse the relocation. */
4907 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4908 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4909 p = (input_section->output_section->vma
4910 + input_section->output_offset
4911 + relocation->r_offset);
4912
4913 /* Assume that there will be no overflow. */
b34976b6 4914 overflowed_p = FALSE;
b49e97c9
TS
4915
4916 /* Figure out whether or not the symbol is local, and get the offset
4917 used in the array of hash table entries. */
4918 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4919 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 4920 local_sections);
bce03d3d 4921 was_local_p = local_p;
b49e97c9
TS
4922 if (! elf_bad_symtab (input_bfd))
4923 extsymoff = symtab_hdr->sh_info;
4924 else
4925 {
4926 /* The symbol table does not follow the rule that local symbols
4927 must come before globals. */
4928 extsymoff = 0;
4929 }
4930
4931 /* Figure out the value of the symbol. */
4932 if (local_p)
4933 {
4934 Elf_Internal_Sym *sym;
4935
4936 sym = local_syms + r_symndx;
4937 sec = local_sections[r_symndx];
4938
4939 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
4940 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4941 || (sec->flags & SEC_MERGE))
b49e97c9 4942 symbol += sym->st_value;
d4df96e6
L
4943 if ((sec->flags & SEC_MERGE)
4944 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4945 {
4946 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4947 addend -= symbol;
4948 addend += sec->output_section->vma + sec->output_offset;
4949 }
b49e97c9
TS
4950
4951 /* MIPS16 text labels should be treated as odd. */
30c09090 4952 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
4953 ++symbol;
4954
4955 /* Record the name of this symbol, for our caller. */
4956 *namep = bfd_elf_string_from_elf_section (input_bfd,
4957 symtab_hdr->sh_link,
4958 sym->st_name);
4959 if (*namep == '\0')
4960 *namep = bfd_section_name (input_bfd, sec);
4961
30c09090 4962 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
b49e97c9
TS
4963 }
4964 else
4965 {
560e09e9
NC
4966 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4967
b49e97c9
TS
4968 /* For global symbols we look up the symbol in the hash-table. */
4969 h = ((struct mips_elf_link_hash_entry *)
4970 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4971 /* Find the real hash-table entry for this symbol. */
4972 while (h->root.root.type == bfd_link_hash_indirect
4973 || h->root.root.type == bfd_link_hash_warning)
4974 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4975
4976 /* Record the name of this symbol, for our caller. */
4977 *namep = h->root.root.root.string;
4978
4979 /* See if this is the special _gp_disp symbol. Note that such a
4980 symbol must always be a global symbol. */
560e09e9 4981 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
4982 && ! NEWABI_P (input_bfd))
4983 {
4984 /* Relocations against _gp_disp are permitted only with
4985 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 4986 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
4987 return bfd_reloc_notsupported;
4988
b34976b6 4989 gp_disp_p = TRUE;
b49e97c9 4990 }
bbe506e8
TS
4991 /* See if this is the special _gp symbol. Note that such a
4992 symbol must always be a global symbol. */
4993 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4994 gnu_local_gp_p = TRUE;
4995
4996
b49e97c9
TS
4997 /* If this symbol is defined, calculate its address. Note that
4998 _gp_disp is a magic symbol, always implicitly defined by the
4999 linker, so it's inappropriate to check to see whether or not
5000 its defined. */
5001 else if ((h->root.root.type == bfd_link_hash_defined
5002 || h->root.root.type == bfd_link_hash_defweak)
5003 && h->root.root.u.def.section)
5004 {
5005 sec = h->root.root.u.def.section;
5006 if (sec->output_section)
5007 symbol = (h->root.root.u.def.value
5008 + sec->output_section->vma
5009 + sec->output_offset);
5010 else
5011 symbol = h->root.root.u.def.value;
5012 }
5013 else if (h->root.root.type == bfd_link_hash_undefweak)
5014 /* We allow relocations against undefined weak symbols, giving
5015 it the value zero, so that you can undefined weak functions
5016 and check to see if they exist by looking at their
5017 addresses. */
5018 symbol = 0;
59c2e50f 5019 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5020 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5021 symbol = 0;
a4d0f181
TS
5022 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5023 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5024 {
5025 /* If this is a dynamic link, we should have created a
5026 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5027 in in _bfd_mips_elf_create_dynamic_sections.
5028 Otherwise, we should define the symbol with a value of 0.
5029 FIXME: It should probably get into the symbol table
5030 somehow as well. */
5031 BFD_ASSERT (! info->shared);
5032 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5033 symbol = 0;
5034 }
5e2b0d47
NC
5035 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5036 {
5037 /* This is an optional symbol - an Irix specific extension to the
5038 ELF spec. Ignore it for now.
5039 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5040 than simply ignoring them, but we do not handle this for now.
5041 For information see the "64-bit ELF Object File Specification"
5042 which is available from here:
5043 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5044 symbol = 0;
5045 }
e7e2196d
MR
5046 else if ((*info->callbacks->undefined_symbol)
5047 (info, h->root.root.root.string, input_bfd,
5048 input_section, relocation->r_offset,
5049 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5050 || ELF_ST_VISIBILITY (h->root.other)))
5051 {
5052 return bfd_reloc_undefined;
5053 }
b49e97c9
TS
5054 else
5055 {
e7e2196d 5056 return bfd_reloc_notsupported;
b49e97c9
TS
5057 }
5058
30c09090 5059 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
b49e97c9
TS
5060 }
5061
738e5348
RS
5062 /* If this is a reference to a 16-bit function with a stub, we need
5063 to redirect the relocation to the stub unless:
5064
5065 (a) the relocation is for a MIPS16 JAL;
5066
5067 (b) the relocation is for a MIPS16 PIC call, and there are no
5068 non-MIPS16 uses of the GOT slot; or
5069
5070 (c) the section allows direct references to MIPS16 functions. */
5071 if (r_type != R_MIPS16_26
5072 && !info->relocatable
5073 && ((h != NULL
5074 && h->fn_stub != NULL
5075 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71
TS
5076 || (local_p
5077 && elf_tdata (input_bfd)->local_stubs != NULL
b49e97c9 5078 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5079 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5080 {
5081 /* This is a 32- or 64-bit call to a 16-bit function. We should
5082 have already noticed that we were going to need the
5083 stub. */
5084 if (local_p)
5085 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5086 else
5087 {
5088 BFD_ASSERT (h->need_fn_stub);
5089 sec = h->fn_stub;
5090 }
5091
5092 symbol = sec->output_section->vma + sec->output_offset;
f38c2df5
TS
5093 /* The target is 16-bit, but the stub isn't. */
5094 target_is_16_bit_code_p = FALSE;
b49e97c9
TS
5095 }
5096 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
738e5348
RS
5097 need to redirect the call to the stub. Note that we specifically
5098 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5099 use an indirect stub instead. */
1049f94e 5100 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5101 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71
TS
5102 || (local_p
5103 && elf_tdata (input_bfd)->local_call_stubs != NULL
5104 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
b49e97c9
TS
5105 && !target_is_16_bit_code_p)
5106 {
b9d58d71
TS
5107 if (local_p)
5108 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5109 else
b49e97c9 5110 {
b9d58d71
TS
5111 /* If both call_stub and call_fp_stub are defined, we can figure
5112 out which one to use by checking which one appears in the input
5113 file. */
5114 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5115 {
b9d58d71
TS
5116 asection *o;
5117
5118 sec = NULL;
5119 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5120 {
b9d58d71
TS
5121 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5122 {
5123 sec = h->call_fp_stub;
5124 break;
5125 }
b49e97c9 5126 }
b9d58d71
TS
5127 if (sec == NULL)
5128 sec = h->call_stub;
b49e97c9 5129 }
b9d58d71 5130 else if (h->call_stub != NULL)
b49e97c9 5131 sec = h->call_stub;
b9d58d71
TS
5132 else
5133 sec = h->call_fp_stub;
5134 }
b49e97c9 5135
eea6121a 5136 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5137 symbol = sec->output_section->vma + sec->output_offset;
5138 }
861fb55a
DJ
5139 /* If this is a direct call to a PIC function, redirect to the
5140 non-PIC stub. */
5141 else if (h != NULL && h->la25_stub
5142 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5143 symbol = (h->la25_stub->stub_section->output_section->vma
5144 + h->la25_stub->stub_section->output_offset
5145 + h->la25_stub->offset);
b49e97c9
TS
5146
5147 /* Calls from 16-bit code to 32-bit code and vice versa require the
38a7df63
CF
5148 mode change. */
5149 *cross_mode_jump_p = !info->relocatable
5150 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5151 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5152 && target_is_16_bit_code_p));
b49e97c9 5153
020d7251 5154 local_p = h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->root);
b49e97c9 5155
0a61c8c2
RS
5156 gp0 = _bfd_get_gp_value (input_bfd);
5157 gp = _bfd_get_gp_value (abfd);
23cc69b6 5158 if (htab->got_info)
a8028dd0 5159 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5160
5161 if (gnu_local_gp_p)
5162 symbol = gp;
5163
020d7251
RS
5164 /* Global R_MIPS_GOT_PAGE relocations are equivalent to R_MIPS_GOT_DISP.
5165 The addend is applied by the corresponding R_MIPS_GOT_OFST. */
5166 if (r_type == R_MIPS_GOT_PAGE && !local_p)
5167 {
5168 r_type = R_MIPS_GOT_DISP;
5169 addend = 0;
5170 }
5171
0a61c8c2
RS
5172 /* If we haven't already determined the GOT offset, oand we're going
5173 to need it, get it now. */
b49e97c9
TS
5174 switch (r_type)
5175 {
738e5348
RS
5176 case R_MIPS16_CALL16:
5177 case R_MIPS16_GOT16:
b49e97c9
TS
5178 case R_MIPS_CALL16:
5179 case R_MIPS_GOT16:
5180 case R_MIPS_GOT_DISP:
5181 case R_MIPS_GOT_HI16:
5182 case R_MIPS_CALL_HI16:
5183 case R_MIPS_GOT_LO16:
5184 case R_MIPS_CALL_LO16:
0f20cc35
DJ
5185 case R_MIPS_TLS_GD:
5186 case R_MIPS_TLS_GOTTPREL:
5187 case R_MIPS_TLS_LDM:
b49e97c9 5188 /* Find the index into the GOT where this value is located. */
0f20cc35
DJ
5189 if (r_type == R_MIPS_TLS_LDM)
5190 {
0a44bf69 5191 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5192 0, 0, NULL, r_type);
0f20cc35
DJ
5193 if (g == MINUS_ONE)
5194 return bfd_reloc_outofrange;
5195 }
5196 else if (!local_p)
b49e97c9 5197 {
0a44bf69
RS
5198 /* On VxWorks, CALL relocations should refer to the .got.plt
5199 entry, which is initialized to point at the PLT stub. */
5200 if (htab->is_vxworks
5201 && (r_type == R_MIPS_CALL_HI16
5202 || r_type == R_MIPS_CALL_LO16
738e5348 5203 || call16_reloc_p (r_type)))
0a44bf69
RS
5204 {
5205 BFD_ASSERT (addend == 0);
5206 BFD_ASSERT (h->root.needs_plt);
5207 g = mips_elf_gotplt_index (info, &h->root);
5208 }
5209 else
b49e97c9 5210 {
020d7251 5211 BFD_ASSERT (addend == 0);
0a44bf69
RS
5212 g = mips_elf_global_got_index (dynobj, input_bfd,
5213 &h->root, r_type, info);
5214 if (h->tls_type == GOT_NORMAL
020d7251
RS
5215 && !elf_hash_table (info)->dynamic_sections_created)
5216 /* This is a static link. We must initialize the GOT entry. */
a8028dd0 5217 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5218 }
5219 }
0a44bf69 5220 else if (!htab->is_vxworks
738e5348 5221 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5222 /* The calculation below does not involve "g". */
b49e97c9
TS
5223 break;
5224 else
5225 {
5c18022e 5226 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5227 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5228 if (g == MINUS_ONE)
5229 return bfd_reloc_outofrange;
5230 }
5231
5232 /* Convert GOT indices to actual offsets. */
a8028dd0 5233 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5234 break;
b49e97c9
TS
5235 }
5236
0a44bf69
RS
5237 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5238 symbols are resolved by the loader. Add them to .rela.dyn. */
5239 if (h != NULL && is_gott_symbol (info, &h->root))
5240 {
5241 Elf_Internal_Rela outrel;
5242 bfd_byte *loc;
5243 asection *s;
5244
5245 s = mips_elf_rel_dyn_section (info, FALSE);
5246 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5247
5248 outrel.r_offset = (input_section->output_section->vma
5249 + input_section->output_offset
5250 + relocation->r_offset);
5251 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5252 outrel.r_addend = addend;
5253 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5254
5255 /* If we've written this relocation for a readonly section,
5256 we need to set DF_TEXTREL again, so that we do not delete the
5257 DT_TEXTREL tag. */
5258 if (MIPS_ELF_READONLY_SECTION (input_section))
5259 info->flags |= DF_TEXTREL;
5260
0a44bf69
RS
5261 *valuep = 0;
5262 return bfd_reloc_ok;
5263 }
5264
b49e97c9
TS
5265 /* Figure out what kind of relocation is being performed. */
5266 switch (r_type)
5267 {
5268 case R_MIPS_NONE:
5269 return bfd_reloc_continue;
5270
5271 case R_MIPS_16:
a7ebbfdf 5272 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5273 overflowed_p = mips_elf_overflow_p (value, 16);
5274 break;
5275
5276 case R_MIPS_32:
5277 case R_MIPS_REL32:
5278 case R_MIPS_64:
5279 if ((info->shared
861fb55a 5280 || (htab->root.dynamic_sections_created
b49e97c9 5281 && h != NULL
f5385ebf 5282 && h->root.def_dynamic
861fb55a
DJ
5283 && !h->root.def_regular
5284 && !h->has_static_relocs))
b49e97c9 5285 && r_symndx != 0
9a59ad6b
DJ
5286 && (h == NULL
5287 || h->root.root.type != bfd_link_hash_undefweak
5288 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5289 && (input_section->flags & SEC_ALLOC) != 0)
5290 {
861fb55a 5291 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5292 where the symbol will end up. So, we create a relocation
5293 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5294 linker. We must do the same for executable references to
5295 shared library symbols, unless we've decided to use copy
5296 relocs or PLTs instead. */
b49e97c9
TS
5297 value = addend;
5298 if (!mips_elf_create_dynamic_relocation (abfd,
5299 info,
5300 relocation,
5301 h,
5302 sec,
5303 symbol,
5304 &value,
5305 input_section))
5306 return bfd_reloc_undefined;
5307 }
5308 else
5309 {
5310 if (r_type != R_MIPS_REL32)
5311 value = symbol + addend;
5312 else
5313 value = addend;
5314 }
5315 value &= howto->dst_mask;
092dcd75
CD
5316 break;
5317
5318 case R_MIPS_PC32:
5319 value = symbol + addend - p;
5320 value &= howto->dst_mask;
b49e97c9
TS
5321 break;
5322
b49e97c9
TS
5323 case R_MIPS16_26:
5324 /* The calculation for R_MIPS16_26 is just the same as for an
5325 R_MIPS_26. It's only the storage of the relocated field into
5326 the output file that's different. That's handled in
5327 mips_elf_perform_relocation. So, we just fall through to the
5328 R_MIPS_26 case here. */
5329 case R_MIPS_26:
020d7251 5330 if (was_local_p)
30ac9238 5331 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
b49e97c9 5332 else
728b2f21
ILT
5333 {
5334 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
c314987d
RS
5335 if (h->root.root.type != bfd_link_hash_undefweak)
5336 overflowed_p = (value >> 26) != ((p + 4) >> 28);
728b2f21 5337 }
b49e97c9
TS
5338 value &= howto->dst_mask;
5339 break;
5340
0f20cc35
DJ
5341 case R_MIPS_TLS_DTPREL_HI16:
5342 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5343 & howto->dst_mask);
5344 break;
5345
5346 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5347 case R_MIPS_TLS_DTPREL32:
5348 case R_MIPS_TLS_DTPREL64:
0f20cc35
DJ
5349 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5350 break;
5351
5352 case R_MIPS_TLS_TPREL_HI16:
5353 value = (mips_elf_high (addend + symbol - tprel_base (info))
5354 & howto->dst_mask);
5355 break;
5356
5357 case R_MIPS_TLS_TPREL_LO16:
5358 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5359 break;
5360
b49e97c9 5361 case R_MIPS_HI16:
d6f16593 5362 case R_MIPS16_HI16:
b49e97c9
TS
5363 if (!gp_disp_p)
5364 {
5365 value = mips_elf_high (addend + symbol);
5366 value &= howto->dst_mask;
5367 }
5368 else
5369 {
d6f16593
MR
5370 /* For MIPS16 ABI code we generate this sequence
5371 0: li $v0,%hi(_gp_disp)
5372 4: addiupc $v1,%lo(_gp_disp)
5373 8: sll $v0,16
5374 12: addu $v0,$v1
5375 14: move $gp,$v0
5376 So the offsets of hi and lo relocs are the same, but the
5377 $pc is four higher than $t9 would be, so reduce
5378 both reloc addends by 4. */
5379 if (r_type == R_MIPS16_HI16)
5380 value = mips_elf_high (addend + gp - p - 4);
5381 else
5382 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5383 overflowed_p = mips_elf_overflow_p (value, 16);
5384 }
5385 break;
5386
5387 case R_MIPS_LO16:
d6f16593 5388 case R_MIPS16_LO16:
b49e97c9
TS
5389 if (!gp_disp_p)
5390 value = (symbol + addend) & howto->dst_mask;
5391 else
5392 {
d6f16593
MR
5393 /* See the comment for R_MIPS16_HI16 above for the reason
5394 for this conditional. */
5395 if (r_type == R_MIPS16_LO16)
5396 value = addend + gp - p;
5397 else
5398 value = addend + gp - p + 4;
b49e97c9 5399 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5400 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5401 _gp_disp are normally generated from the .cpload
5402 pseudo-op. It generates code that normally looks like
5403 this:
5404
5405 lui $gp,%hi(_gp_disp)
5406 addiu $gp,$gp,%lo(_gp_disp)
5407 addu $gp,$gp,$t9
5408
5409 Here $t9 holds the address of the function being called,
5410 as required by the MIPS ELF ABI. The R_MIPS_LO16
5411 relocation can easily overflow in this situation, but the
5412 R_MIPS_HI16 relocation will handle the overflow.
5413 Therefore, we consider this a bug in the MIPS ABI, and do
5414 not check for overflow here. */
5415 }
5416 break;
5417
5418 case R_MIPS_LITERAL:
5419 /* Because we don't merge literal sections, we can handle this
5420 just like R_MIPS_GPREL16. In the long run, we should merge
5421 shared literals, and then we will need to additional work
5422 here. */
5423
5424 /* Fall through. */
5425
5426 case R_MIPS16_GPREL:
5427 /* The R_MIPS16_GPREL performs the same calculation as
5428 R_MIPS_GPREL16, but stores the relocated bits in a different
5429 order. We don't need to do anything special here; the
5430 differences are handled in mips_elf_perform_relocation. */
5431 case R_MIPS_GPREL16:
bce03d3d
AO
5432 /* Only sign-extend the addend if it was extracted from the
5433 instruction. If the addend was separate, leave it alone,
5434 otherwise we may lose significant bits. */
5435 if (howto->partial_inplace)
a7ebbfdf 5436 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5437 value = symbol + addend - gp;
5438 /* If the symbol was local, any earlier relocatable links will
5439 have adjusted its addend with the gp offset, so compensate
5440 for that now. Don't do it for symbols forced local in this
5441 link, though, since they won't have had the gp offset applied
5442 to them before. */
5443 if (was_local_p)
5444 value += gp0;
b49e97c9
TS
5445 overflowed_p = mips_elf_overflow_p (value, 16);
5446 break;
5447
738e5348
RS
5448 case R_MIPS16_GOT16:
5449 case R_MIPS16_CALL16:
b49e97c9
TS
5450 case R_MIPS_GOT16:
5451 case R_MIPS_CALL16:
0a44bf69 5452 /* VxWorks does not have separate local and global semantics for
738e5348 5453 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5454 if (!htab->is_vxworks && local_p)
b49e97c9 5455 {
5c18022e 5456 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 5457 symbol + addend, !was_local_p);
b49e97c9
TS
5458 if (value == MINUS_ONE)
5459 return bfd_reloc_outofrange;
5460 value
a8028dd0 5461 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5462 overflowed_p = mips_elf_overflow_p (value, 16);
5463 break;
5464 }
5465
5466 /* Fall through. */
5467
0f20cc35
DJ
5468 case R_MIPS_TLS_GD:
5469 case R_MIPS_TLS_GOTTPREL:
5470 case R_MIPS_TLS_LDM:
b49e97c9
TS
5471 case R_MIPS_GOT_DISP:
5472 value = g;
5473 overflowed_p = mips_elf_overflow_p (value, 16);
5474 break;
5475
5476 case R_MIPS_GPREL32:
bce03d3d
AO
5477 value = (addend + symbol + gp0 - gp);
5478 if (!save_addend)
5479 value &= howto->dst_mask;
b49e97c9
TS
5480 break;
5481
5482 case R_MIPS_PC16:
bad36eac
DJ
5483 case R_MIPS_GNU_REL16_S2:
5484 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5485 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5486 value >>= howto->rightshift;
5487 value &= howto->dst_mask;
b49e97c9
TS
5488 break;
5489
5490 case R_MIPS_GOT_HI16:
5491 case R_MIPS_CALL_HI16:
5492 /* We're allowed to handle these two relocations identically.
5493 The dynamic linker is allowed to handle the CALL relocations
5494 differently by creating a lazy evaluation stub. */
5495 value = g;
5496 value = mips_elf_high (value);
5497 value &= howto->dst_mask;
5498 break;
5499
5500 case R_MIPS_GOT_LO16:
5501 case R_MIPS_CALL_LO16:
5502 value = g & howto->dst_mask;
5503 break;
5504
5505 case R_MIPS_GOT_PAGE:
5c18022e 5506 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5507 if (value == MINUS_ONE)
5508 return bfd_reloc_outofrange;
a8028dd0 5509 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5510 overflowed_p = mips_elf_overflow_p (value, 16);
5511 break;
5512
5513 case R_MIPS_GOT_OFST:
93a2b7ae 5514 if (local_p)
5c18022e 5515 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5516 else
5517 value = addend;
b49e97c9
TS
5518 overflowed_p = mips_elf_overflow_p (value, 16);
5519 break;
5520
5521 case R_MIPS_SUB:
5522 value = symbol - addend;
5523 value &= howto->dst_mask;
5524 break;
5525
5526 case R_MIPS_HIGHER:
5527 value = mips_elf_higher (addend + symbol);
5528 value &= howto->dst_mask;
5529 break;
5530
5531 case R_MIPS_HIGHEST:
5532 value = mips_elf_highest (addend + symbol);
5533 value &= howto->dst_mask;
5534 break;
5535
5536 case R_MIPS_SCN_DISP:
5537 value = symbol + addend - sec->output_offset;
5538 value &= howto->dst_mask;
5539 break;
5540
b49e97c9 5541 case R_MIPS_JALR:
1367d393
ILT
5542 /* This relocation is only a hint. In some cases, we optimize
5543 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5544 when the symbol does not resolve locally. */
5545 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5546 return bfd_reloc_continue;
5547 value = symbol + addend;
5548 break;
b49e97c9 5549
1367d393 5550 case R_MIPS_PJUMP:
b49e97c9
TS
5551 case R_MIPS_GNU_VTINHERIT:
5552 case R_MIPS_GNU_VTENTRY:
5553 /* We don't do anything with these at present. */
5554 return bfd_reloc_continue;
5555
5556 default:
5557 /* An unrecognized relocation type. */
5558 return bfd_reloc_notsupported;
5559 }
5560
5561 /* Store the VALUE for our caller. */
5562 *valuep = value;
5563 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5564}
5565
5566/* Obtain the field relocated by RELOCATION. */
5567
5568static bfd_vma
9719ad41
RS
5569mips_elf_obtain_contents (reloc_howto_type *howto,
5570 const Elf_Internal_Rela *relocation,
5571 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
5572{
5573 bfd_vma x;
5574 bfd_byte *location = contents + relocation->r_offset;
5575
5576 /* Obtain the bytes. */
5577 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5578
b49e97c9
TS
5579 return x;
5580}
5581
5582/* It has been determined that the result of the RELOCATION is the
5583 VALUE. Use HOWTO to place VALUE into the output file at the
5584 appropriate position. The SECTION is the section to which the
38a7df63
CF
5585 relocation applies.
5586 CROSS_MODE_JUMP_P is true if the relocation field
5587 is a MIPS16 jump to non-MIPS16 code, or vice versa.
b49e97c9 5588
b34976b6 5589 Returns FALSE if anything goes wrong. */
b49e97c9 5590
b34976b6 5591static bfd_boolean
9719ad41
RS
5592mips_elf_perform_relocation (struct bfd_link_info *info,
5593 reloc_howto_type *howto,
5594 const Elf_Internal_Rela *relocation,
5595 bfd_vma value, bfd *input_bfd,
5596 asection *input_section, bfd_byte *contents,
38a7df63 5597 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
5598{
5599 bfd_vma x;
5600 bfd_byte *location;
5601 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5602
5603 /* Figure out where the relocation is occurring. */
5604 location = contents + relocation->r_offset;
5605
d6f16593
MR
5606 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5607
b49e97c9
TS
5608 /* Obtain the current value. */
5609 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5610
5611 /* Clear the field we are setting. */
5612 x &= ~howto->dst_mask;
5613
b49e97c9
TS
5614 /* Set the field. */
5615 x |= (value & howto->dst_mask);
5616
5617 /* If required, turn JAL into JALX. */
38a7df63 5618 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 5619 {
b34976b6 5620 bfd_boolean ok;
b49e97c9
TS
5621 bfd_vma opcode = x >> 26;
5622 bfd_vma jalx_opcode;
5623
5624 /* Check to see if the opcode is already JAL or JALX. */
5625 if (r_type == R_MIPS16_26)
5626 {
5627 ok = ((opcode == 0x6) || (opcode == 0x7));
5628 jalx_opcode = 0x7;
5629 }
5630 else
5631 {
5632 ok = ((opcode == 0x3) || (opcode == 0x1d));
5633 jalx_opcode = 0x1d;
5634 }
5635
5636 /* If the opcode is not JAL or JALX, there's a problem. */
5637 if (!ok)
5638 {
5639 (*_bfd_error_handler)
776167e8 5640 (_("%B: %A+0x%lx: Direct jumps between ISA modes are not allowed; consider recompiling with interlinking enabled."),
d003868e
AM
5641 input_bfd,
5642 input_section,
b49e97c9
TS
5643 (unsigned long) relocation->r_offset);
5644 bfd_set_error (bfd_error_bad_value);
b34976b6 5645 return FALSE;
b49e97c9
TS
5646 }
5647
5648 /* Make this the JALX opcode. */
5649 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5650 }
5651
38a7df63
CF
5652 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5653 range. */
cd8d5a82 5654 if (!info->relocatable
38a7df63 5655 && !cross_mode_jump_p
cd8d5a82
CF
5656 && ((JAL_TO_BAL_P (input_bfd)
5657 && r_type == R_MIPS_26
5658 && (x >> 26) == 0x3) /* jal addr */
5659 || (JALR_TO_BAL_P (input_bfd)
5660 && r_type == R_MIPS_JALR
38a7df63
CF
5661 && x == 0x0320f809) /* jalr t9 */
5662 || (JR_TO_B_P (input_bfd)
5663 && r_type == R_MIPS_JALR
5664 && x == 0x03200008))) /* jr t9 */
1367d393
ILT
5665 {
5666 bfd_vma addr;
5667 bfd_vma dest;
5668 bfd_signed_vma off;
5669
5670 addr = (input_section->output_section->vma
5671 + input_section->output_offset
5672 + relocation->r_offset
5673 + 4);
5674 if (r_type == R_MIPS_26)
5675 dest = (value << 2) | ((addr >> 28) << 28);
5676 else
5677 dest = value;
5678 off = dest - addr;
5679 if (off <= 0x1ffff && off >= -0x20000)
38a7df63
CF
5680 {
5681 if (x == 0x03200008) /* jr t9 */
5682 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5683 else
5684 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5685 }
1367d393
ILT
5686 }
5687
b49e97c9
TS
5688 /* Put the value into the output. */
5689 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593
MR
5690
5691 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5692 location);
5693
b34976b6 5694 return TRUE;
b49e97c9 5695}
b49e97c9 5696\f
b49e97c9
TS
5697/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5698 is the original relocation, which is now being transformed into a
5699 dynamic relocation. The ADDENDP is adjusted if necessary; the
5700 caller should store the result in place of the original addend. */
5701
b34976b6 5702static bfd_boolean
9719ad41
RS
5703mips_elf_create_dynamic_relocation (bfd *output_bfd,
5704 struct bfd_link_info *info,
5705 const Elf_Internal_Rela *rel,
5706 struct mips_elf_link_hash_entry *h,
5707 asection *sec, bfd_vma symbol,
5708 bfd_vma *addendp, asection *input_section)
b49e97c9 5709{
947216bf 5710 Elf_Internal_Rela outrel[3];
b49e97c9
TS
5711 asection *sreloc;
5712 bfd *dynobj;
5713 int r_type;
5d41f0b6
RS
5714 long indx;
5715 bfd_boolean defined_p;
0a44bf69 5716 struct mips_elf_link_hash_table *htab;
b49e97c9 5717
0a44bf69 5718 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
5719 BFD_ASSERT (htab != NULL);
5720
b49e97c9
TS
5721 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5722 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 5723 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
5724 BFD_ASSERT (sreloc != NULL);
5725 BFD_ASSERT (sreloc->contents != NULL);
5726 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 5727 < sreloc->size);
b49e97c9 5728
b49e97c9
TS
5729 outrel[0].r_offset =
5730 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
5731 if (ABI_64_P (output_bfd))
5732 {
5733 outrel[1].r_offset =
5734 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5735 outrel[2].r_offset =
5736 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5737 }
b49e97c9 5738
c5ae1840 5739 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 5740 /* The relocation field has been deleted. */
5d41f0b6
RS
5741 return TRUE;
5742
5743 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
5744 {
5745 /* The relocation field has been converted into a relative value of
5746 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5747 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 5748 *addendp += symbol;
5d41f0b6 5749 return TRUE;
0d591ff7 5750 }
b49e97c9 5751
5d41f0b6
RS
5752 /* We must now calculate the dynamic symbol table index to use
5753 in the relocation. */
d4a77f3f 5754 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 5755 {
020d7251 5756 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5d41f0b6
RS
5757 indx = h->root.dynindx;
5758 if (SGI_COMPAT (output_bfd))
5759 defined_p = h->root.def_regular;
5760 else
5761 /* ??? glibc's ld.so just adds the final GOT entry to the
5762 relocation field. It therefore treats relocs against
5763 defined symbols in the same way as relocs against
5764 undefined symbols. */
5765 defined_p = FALSE;
5766 }
b49e97c9
TS
5767 else
5768 {
5d41f0b6
RS
5769 if (sec != NULL && bfd_is_abs_section (sec))
5770 indx = 0;
5771 else if (sec == NULL || sec->owner == NULL)
fdd07405 5772 {
5d41f0b6
RS
5773 bfd_set_error (bfd_error_bad_value);
5774 return FALSE;
b49e97c9
TS
5775 }
5776 else
5777 {
5d41f0b6 5778 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
5779 if (indx == 0)
5780 {
5781 asection *osec = htab->root.text_index_section;
5782 indx = elf_section_data (osec)->dynindx;
5783 }
5d41f0b6
RS
5784 if (indx == 0)
5785 abort ();
b49e97c9
TS
5786 }
5787
5d41f0b6
RS
5788 /* Instead of generating a relocation using the section
5789 symbol, we may as well make it a fully relative
5790 relocation. We want to avoid generating relocations to
5791 local symbols because we used to generate them
5792 incorrectly, without adding the original symbol value,
5793 which is mandated by the ABI for section symbols. In
5794 order to give dynamic loaders and applications time to
5795 phase out the incorrect use, we refrain from emitting
5796 section-relative relocations. It's not like they're
5797 useful, after all. This should be a bit more efficient
5798 as well. */
5799 /* ??? Although this behavior is compatible with glibc's ld.so,
5800 the ABI says that relocations against STN_UNDEF should have
5801 a symbol value of 0. Irix rld honors this, so relocations
5802 against STN_UNDEF have no effect. */
5803 if (!SGI_COMPAT (output_bfd))
5804 indx = 0;
5805 defined_p = TRUE;
b49e97c9
TS
5806 }
5807
5d41f0b6
RS
5808 /* If the relocation was previously an absolute relocation and
5809 this symbol will not be referred to by the relocation, we must
5810 adjust it by the value we give it in the dynamic symbol table.
5811 Otherwise leave the job up to the dynamic linker. */
5812 if (defined_p && r_type != R_MIPS_REL32)
5813 *addendp += symbol;
5814
0a44bf69
RS
5815 if (htab->is_vxworks)
5816 /* VxWorks uses non-relative relocations for this. */
5817 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5818 else
5819 /* The relocation is always an REL32 relocation because we don't
5820 know where the shared library will wind up at load-time. */
5821 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5822 R_MIPS_REL32);
5823
5d41f0b6
RS
5824 /* For strict adherence to the ABI specification, we should
5825 generate a R_MIPS_64 relocation record by itself before the
5826 _REL32/_64 record as well, such that the addend is read in as
5827 a 64-bit value (REL32 is a 32-bit relocation, after all).
5828 However, since none of the existing ELF64 MIPS dynamic
5829 loaders seems to care, we don't waste space with these
5830 artificial relocations. If this turns out to not be true,
5831 mips_elf_allocate_dynamic_relocation() should be tweaked so
5832 as to make room for a pair of dynamic relocations per
5833 invocation if ABI_64_P, and here we should generate an
5834 additional relocation record with R_MIPS_64 by itself for a
5835 NULL symbol before this relocation record. */
5836 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5837 ABI_64_P (output_bfd)
5838 ? R_MIPS_64
5839 : R_MIPS_NONE);
5840 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5841
5842 /* Adjust the output offset of the relocation to reference the
5843 correct location in the output file. */
5844 outrel[0].r_offset += (input_section->output_section->vma
5845 + input_section->output_offset);
5846 outrel[1].r_offset += (input_section->output_section->vma
5847 + input_section->output_offset);
5848 outrel[2].r_offset += (input_section->output_section->vma
5849 + input_section->output_offset);
5850
b49e97c9
TS
5851 /* Put the relocation back out. We have to use the special
5852 relocation outputter in the 64-bit case since the 64-bit
5853 relocation format is non-standard. */
5854 if (ABI_64_P (output_bfd))
5855 {
5856 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5857 (output_bfd, &outrel[0],
5858 (sreloc->contents
5859 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5860 }
0a44bf69
RS
5861 else if (htab->is_vxworks)
5862 {
5863 /* VxWorks uses RELA rather than REL dynamic relocations. */
5864 outrel[0].r_addend = *addendp;
5865 bfd_elf32_swap_reloca_out
5866 (output_bfd, &outrel[0],
5867 (sreloc->contents
5868 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5869 }
b49e97c9 5870 else
947216bf
AM
5871 bfd_elf32_swap_reloc_out
5872 (output_bfd, &outrel[0],
5873 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 5874
b49e97c9
TS
5875 /* We've now added another relocation. */
5876 ++sreloc->reloc_count;
5877
5878 /* Make sure the output section is writable. The dynamic linker
5879 will be writing to it. */
5880 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5881 |= SHF_WRITE;
5882
5883 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 5884 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9
TS
5885 {
5886 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5887 bfd_byte *cr;
5888
5889 if (scpt)
5890 {
5891 Elf32_crinfo cptrel;
5892
5893 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5894 cptrel.vaddr = (rel->r_offset
5895 + input_section->output_section->vma
5896 + input_section->output_offset);
5897 if (r_type == R_MIPS_REL32)
5898 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5899 else
5900 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5901 mips_elf_set_cr_dist2to (cptrel, 0);
5902 cptrel.konst = *addendp;
5903
5904 cr = (scpt->contents
5905 + sizeof (Elf32_External_compact_rel));
abc0f8d0 5906 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
5907 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5908 ((Elf32_External_crinfo *) cr
5909 + scpt->reloc_count));
5910 ++scpt->reloc_count;
5911 }
5912 }
5913
943284cc
DJ
5914 /* If we've written this relocation for a readonly section,
5915 we need to set DF_TEXTREL again, so that we do not delete the
5916 DT_TEXTREL tag. */
5917 if (MIPS_ELF_READONLY_SECTION (input_section))
5918 info->flags |= DF_TEXTREL;
5919
b34976b6 5920 return TRUE;
b49e97c9
TS
5921}
5922\f
b49e97c9
TS
5923/* Return the MACH for a MIPS e_flags value. */
5924
5925unsigned long
9719ad41 5926_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
5927{
5928 switch (flags & EF_MIPS_MACH)
5929 {
5930 case E_MIPS_MACH_3900:
5931 return bfd_mach_mips3900;
5932
5933 case E_MIPS_MACH_4010:
5934 return bfd_mach_mips4010;
5935
5936 case E_MIPS_MACH_4100:
5937 return bfd_mach_mips4100;
5938
5939 case E_MIPS_MACH_4111:
5940 return bfd_mach_mips4111;
5941
00707a0e
RS
5942 case E_MIPS_MACH_4120:
5943 return bfd_mach_mips4120;
5944
b49e97c9
TS
5945 case E_MIPS_MACH_4650:
5946 return bfd_mach_mips4650;
5947
00707a0e
RS
5948 case E_MIPS_MACH_5400:
5949 return bfd_mach_mips5400;
5950
5951 case E_MIPS_MACH_5500:
5952 return bfd_mach_mips5500;
5953
0d2e43ed
ILT
5954 case E_MIPS_MACH_9000:
5955 return bfd_mach_mips9000;
5956
b49e97c9
TS
5957 case E_MIPS_MACH_SB1:
5958 return bfd_mach_mips_sb1;
5959
350cc38d
MS
5960 case E_MIPS_MACH_LS2E:
5961 return bfd_mach_mips_loongson_2e;
5962
5963 case E_MIPS_MACH_LS2F:
5964 return bfd_mach_mips_loongson_2f;
5965
6f179bd0
AN
5966 case E_MIPS_MACH_OCTEON:
5967 return bfd_mach_mips_octeon;
5968
52b6b6b9
JM
5969 case E_MIPS_MACH_XLR:
5970 return bfd_mach_mips_xlr;
5971
b49e97c9
TS
5972 default:
5973 switch (flags & EF_MIPS_ARCH)
5974 {
5975 default:
5976 case E_MIPS_ARCH_1:
5977 return bfd_mach_mips3000;
b49e97c9
TS
5978
5979 case E_MIPS_ARCH_2:
5980 return bfd_mach_mips6000;
b49e97c9
TS
5981
5982 case E_MIPS_ARCH_3:
5983 return bfd_mach_mips4000;
b49e97c9
TS
5984
5985 case E_MIPS_ARCH_4:
5986 return bfd_mach_mips8000;
b49e97c9
TS
5987
5988 case E_MIPS_ARCH_5:
5989 return bfd_mach_mips5;
b49e97c9
TS
5990
5991 case E_MIPS_ARCH_32:
5992 return bfd_mach_mipsisa32;
b49e97c9
TS
5993
5994 case E_MIPS_ARCH_64:
5995 return bfd_mach_mipsisa64;
af7ee8bf
CD
5996
5997 case E_MIPS_ARCH_32R2:
5998 return bfd_mach_mipsisa32r2;
5f74bc13
CD
5999
6000 case E_MIPS_ARCH_64R2:
6001 return bfd_mach_mipsisa64r2;
b49e97c9
TS
6002 }
6003 }
6004
6005 return 0;
6006}
6007
6008/* Return printable name for ABI. */
6009
6010static INLINE char *
9719ad41 6011elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
6012{
6013 flagword flags;
6014
6015 flags = elf_elfheader (abfd)->e_flags;
6016 switch (flags & EF_MIPS_ABI)
6017 {
6018 case 0:
6019 if (ABI_N32_P (abfd))
6020 return "N32";
6021 else if (ABI_64_P (abfd))
6022 return "64";
6023 else
6024 return "none";
6025 case E_MIPS_ABI_O32:
6026 return "O32";
6027 case E_MIPS_ABI_O64:
6028 return "O64";
6029 case E_MIPS_ABI_EABI32:
6030 return "EABI32";
6031 case E_MIPS_ABI_EABI64:
6032 return "EABI64";
6033 default:
6034 return "unknown abi";
6035 }
6036}
6037\f
6038/* MIPS ELF uses two common sections. One is the usual one, and the
6039 other is for small objects. All the small objects are kept
6040 together, and then referenced via the gp pointer, which yields
6041 faster assembler code. This is what we use for the small common
6042 section. This approach is copied from ecoff.c. */
6043static asection mips_elf_scom_section;
6044static asymbol mips_elf_scom_symbol;
6045static asymbol *mips_elf_scom_symbol_ptr;
6046
6047/* MIPS ELF also uses an acommon section, which represents an
6048 allocated common symbol which may be overridden by a
6049 definition in a shared library. */
6050static asection mips_elf_acom_section;
6051static asymbol mips_elf_acom_symbol;
6052static asymbol *mips_elf_acom_symbol_ptr;
6053
738e5348 6054/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
6055
6056void
9719ad41 6057_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
6058{
6059 elf_symbol_type *elfsym;
6060
738e5348 6061 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6062 elfsym = (elf_symbol_type *) asym;
6063 switch (elfsym->internal_elf_sym.st_shndx)
6064 {
6065 case SHN_MIPS_ACOMMON:
6066 /* This section is used in a dynamically linked executable file.
6067 It is an allocated common section. The dynamic linker can
6068 either resolve these symbols to something in a shared
6069 library, or it can just leave them here. For our purposes,
6070 we can consider these symbols to be in a new section. */
6071 if (mips_elf_acom_section.name == NULL)
6072 {
6073 /* Initialize the acommon section. */
6074 mips_elf_acom_section.name = ".acommon";
6075 mips_elf_acom_section.flags = SEC_ALLOC;
6076 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6077 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6078 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6079 mips_elf_acom_symbol.name = ".acommon";
6080 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6081 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6082 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6083 }
6084 asym->section = &mips_elf_acom_section;
6085 break;
6086
6087 case SHN_COMMON:
6088 /* Common symbols less than the GP size are automatically
6089 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6090 if (asym->value > elf_gp_size (abfd)
b59eed79 6091 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6092 || IRIX_COMPAT (abfd) == ict_irix6)
6093 break;
6094 /* Fall through. */
6095 case SHN_MIPS_SCOMMON:
6096 if (mips_elf_scom_section.name == NULL)
6097 {
6098 /* Initialize the small common section. */
6099 mips_elf_scom_section.name = ".scommon";
6100 mips_elf_scom_section.flags = SEC_IS_COMMON;
6101 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6102 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6103 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6104 mips_elf_scom_symbol.name = ".scommon";
6105 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6106 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6107 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6108 }
6109 asym->section = &mips_elf_scom_section;
6110 asym->value = elfsym->internal_elf_sym.st_size;
6111 break;
6112
6113 case SHN_MIPS_SUNDEFINED:
6114 asym->section = bfd_und_section_ptr;
6115 break;
6116
b49e97c9 6117 case SHN_MIPS_TEXT:
00b4930b
TS
6118 {
6119 asection *section = bfd_get_section_by_name (abfd, ".text");
6120
6121 BFD_ASSERT (SGI_COMPAT (abfd));
6122 if (section != NULL)
6123 {
6124 asym->section = section;
6125 /* MIPS_TEXT is a bit special, the address is not an offset
6126 to the base of the .text section. So substract the section
6127 base address to make it an offset. */
6128 asym->value -= section->vma;
6129 }
6130 }
b49e97c9
TS
6131 break;
6132
6133 case SHN_MIPS_DATA:
00b4930b
TS
6134 {
6135 asection *section = bfd_get_section_by_name (abfd, ".data");
6136
6137 BFD_ASSERT (SGI_COMPAT (abfd));
6138 if (section != NULL)
6139 {
6140 asym->section = section;
6141 /* MIPS_DATA is a bit special, the address is not an offset
6142 to the base of the .data section. So substract the section
6143 base address to make it an offset. */
6144 asym->value -= section->vma;
6145 }
6146 }
b49e97c9 6147 break;
b49e97c9 6148 }
738e5348
RS
6149
6150 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6151 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6152 && (asym->value & 1) != 0)
6153 {
6154 asym->value--;
6155 elfsym->internal_elf_sym.st_other
6156 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6157 }
b49e97c9
TS
6158}
6159\f
8c946ed5
RS
6160/* Implement elf_backend_eh_frame_address_size. This differs from
6161 the default in the way it handles EABI64.
6162
6163 EABI64 was originally specified as an LP64 ABI, and that is what
6164 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6165 historically accepted the combination of -mabi=eabi and -mlong32,
6166 and this ILP32 variation has become semi-official over time.
6167 Both forms use elf32 and have pointer-sized FDE addresses.
6168
6169 If an EABI object was generated by GCC 4.0 or above, it will have
6170 an empty .gcc_compiled_longXX section, where XX is the size of longs
6171 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6172 have no special marking to distinguish them from LP64 objects.
6173
6174 We don't want users of the official LP64 ABI to be punished for the
6175 existence of the ILP32 variant, but at the same time, we don't want
6176 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6177 We therefore take the following approach:
6178
6179 - If ABFD contains a .gcc_compiled_longXX section, use it to
6180 determine the pointer size.
6181
6182 - Otherwise check the type of the first relocation. Assume that
6183 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6184
6185 - Otherwise punt.
6186
6187 The second check is enough to detect LP64 objects generated by pre-4.0
6188 compilers because, in the kind of output generated by those compilers,
6189 the first relocation will be associated with either a CIE personality
6190 routine or an FDE start address. Furthermore, the compilers never
6191 used a special (non-pointer) encoding for this ABI.
6192
6193 Checking the relocation type should also be safe because there is no
6194 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6195 did so. */
6196
6197unsigned int
6198_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6199{
6200 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6201 return 8;
6202 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6203 {
6204 bfd_boolean long32_p, long64_p;
6205
6206 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6207 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6208 if (long32_p && long64_p)
6209 return 0;
6210 if (long32_p)
6211 return 4;
6212 if (long64_p)
6213 return 8;
6214
6215 if (sec->reloc_count > 0
6216 && elf_section_data (sec)->relocs != NULL
6217 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6218 == R_MIPS_64))
6219 return 8;
6220
6221 return 0;
6222 }
6223 return 4;
6224}
6225\f
174fd7f9
RS
6226/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6227 relocations against two unnamed section symbols to resolve to the
6228 same address. For example, if we have code like:
6229
6230 lw $4,%got_disp(.data)($gp)
6231 lw $25,%got_disp(.text)($gp)
6232 jalr $25
6233
6234 then the linker will resolve both relocations to .data and the program
6235 will jump there rather than to .text.
6236
6237 We can work around this problem by giving names to local section symbols.
6238 This is also what the MIPSpro tools do. */
6239
6240bfd_boolean
6241_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6242{
6243 return SGI_COMPAT (abfd);
6244}
6245\f
b49e97c9
TS
6246/* Work over a section just before writing it out. This routine is
6247 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6248 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6249 a better way. */
6250
b34976b6 6251bfd_boolean
9719ad41 6252_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6253{
6254 if (hdr->sh_type == SHT_MIPS_REGINFO
6255 && hdr->sh_size > 0)
6256 {
6257 bfd_byte buf[4];
6258
6259 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6260 BFD_ASSERT (hdr->contents == NULL);
6261
6262 if (bfd_seek (abfd,
6263 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6264 SEEK_SET) != 0)
b34976b6 6265 return FALSE;
b49e97c9 6266 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6267 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6268 return FALSE;
b49e97c9
TS
6269 }
6270
6271 if (hdr->sh_type == SHT_MIPS_OPTIONS
6272 && hdr->bfd_section != NULL
f0abc2a1
AM
6273 && mips_elf_section_data (hdr->bfd_section) != NULL
6274 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6275 {
6276 bfd_byte *contents, *l, *lend;
6277
f0abc2a1
AM
6278 /* We stored the section contents in the tdata field in the
6279 set_section_contents routine. We save the section contents
6280 so that we don't have to read them again.
b49e97c9
TS
6281 At this point we know that elf_gp is set, so we can look
6282 through the section contents to see if there is an
6283 ODK_REGINFO structure. */
6284
f0abc2a1 6285 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6286 l = contents;
6287 lend = contents + hdr->sh_size;
6288 while (l + sizeof (Elf_External_Options) <= lend)
6289 {
6290 Elf_Internal_Options intopt;
6291
6292 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6293 &intopt);
1bc8074d
MR
6294 if (intopt.size < sizeof (Elf_External_Options))
6295 {
6296 (*_bfd_error_handler)
6297 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6298 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6299 break;
6300 }
b49e97c9
TS
6301 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6302 {
6303 bfd_byte buf[8];
6304
6305 if (bfd_seek (abfd,
6306 (hdr->sh_offset
6307 + (l - contents)
6308 + sizeof (Elf_External_Options)
6309 + (sizeof (Elf64_External_RegInfo) - 8)),
6310 SEEK_SET) != 0)
b34976b6 6311 return FALSE;
b49e97c9 6312 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6313 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6314 return FALSE;
b49e97c9
TS
6315 }
6316 else if (intopt.kind == ODK_REGINFO)
6317 {
6318 bfd_byte buf[4];
6319
6320 if (bfd_seek (abfd,
6321 (hdr->sh_offset
6322 + (l - contents)
6323 + sizeof (Elf_External_Options)
6324 + (sizeof (Elf32_External_RegInfo) - 4)),
6325 SEEK_SET) != 0)
b34976b6 6326 return FALSE;
b49e97c9 6327 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6328 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6329 return FALSE;
b49e97c9
TS
6330 }
6331 l += intopt.size;
6332 }
6333 }
6334
6335 if (hdr->bfd_section != NULL)
6336 {
6337 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6338
2d0f9ad9
JM
6339 /* .sbss is not handled specially here because the GNU/Linux
6340 prelinker can convert .sbss from NOBITS to PROGBITS and
6341 changing it back to NOBITS breaks the binary. The entry in
6342 _bfd_mips_elf_special_sections will ensure the correct flags
6343 are set on .sbss if BFD creates it without reading it from an
6344 input file, and without special handling here the flags set
6345 on it in an input file will be followed. */
b49e97c9
TS
6346 if (strcmp (name, ".sdata") == 0
6347 || strcmp (name, ".lit8") == 0
6348 || strcmp (name, ".lit4") == 0)
6349 {
6350 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6351 hdr->sh_type = SHT_PROGBITS;
6352 }
b49e97c9
TS
6353 else if (strcmp (name, ".srdata") == 0)
6354 {
6355 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6356 hdr->sh_type = SHT_PROGBITS;
6357 }
6358 else if (strcmp (name, ".compact_rel") == 0)
6359 {
6360 hdr->sh_flags = 0;
6361 hdr->sh_type = SHT_PROGBITS;
6362 }
6363 else if (strcmp (name, ".rtproc") == 0)
6364 {
6365 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6366 {
6367 unsigned int adjust;
6368
6369 adjust = hdr->sh_size % hdr->sh_addralign;
6370 if (adjust != 0)
6371 hdr->sh_size += hdr->sh_addralign - adjust;
6372 }
6373 }
6374 }
6375
b34976b6 6376 return TRUE;
b49e97c9
TS
6377}
6378
6379/* Handle a MIPS specific section when reading an object file. This
6380 is called when elfcode.h finds a section with an unknown type.
6381 This routine supports both the 32-bit and 64-bit ELF ABI.
6382
6383 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6384 how to. */
6385
b34976b6 6386bfd_boolean
6dc132d9
L
6387_bfd_mips_elf_section_from_shdr (bfd *abfd,
6388 Elf_Internal_Shdr *hdr,
6389 const char *name,
6390 int shindex)
b49e97c9
TS
6391{
6392 flagword flags = 0;
6393
6394 /* There ought to be a place to keep ELF backend specific flags, but
6395 at the moment there isn't one. We just keep track of the
6396 sections by their name, instead. Fortunately, the ABI gives
6397 suggested names for all the MIPS specific sections, so we will
6398 probably get away with this. */
6399 switch (hdr->sh_type)
6400 {
6401 case SHT_MIPS_LIBLIST:
6402 if (strcmp (name, ".liblist") != 0)
b34976b6 6403 return FALSE;
b49e97c9
TS
6404 break;
6405 case SHT_MIPS_MSYM:
6406 if (strcmp (name, ".msym") != 0)
b34976b6 6407 return FALSE;
b49e97c9
TS
6408 break;
6409 case SHT_MIPS_CONFLICT:
6410 if (strcmp (name, ".conflict") != 0)
b34976b6 6411 return FALSE;
b49e97c9
TS
6412 break;
6413 case SHT_MIPS_GPTAB:
0112cd26 6414 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6415 return FALSE;
b49e97c9
TS
6416 break;
6417 case SHT_MIPS_UCODE:
6418 if (strcmp (name, ".ucode") != 0)
b34976b6 6419 return FALSE;
b49e97c9
TS
6420 break;
6421 case SHT_MIPS_DEBUG:
6422 if (strcmp (name, ".mdebug") != 0)
b34976b6 6423 return FALSE;
b49e97c9
TS
6424 flags = SEC_DEBUGGING;
6425 break;
6426 case SHT_MIPS_REGINFO:
6427 if (strcmp (name, ".reginfo") != 0
6428 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6429 return FALSE;
b49e97c9
TS
6430 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6431 break;
6432 case SHT_MIPS_IFACE:
6433 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6434 return FALSE;
b49e97c9
TS
6435 break;
6436 case SHT_MIPS_CONTENT:
0112cd26 6437 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6438 return FALSE;
b49e97c9
TS
6439 break;
6440 case SHT_MIPS_OPTIONS:
cc2e31b9 6441 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6442 return FALSE;
b49e97c9
TS
6443 break;
6444 case SHT_MIPS_DWARF:
1b315056 6445 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6446 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6447 return FALSE;
b49e97c9
TS
6448 break;
6449 case SHT_MIPS_SYMBOL_LIB:
6450 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6451 return FALSE;
b49e97c9
TS
6452 break;
6453 case SHT_MIPS_EVENTS:
0112cd26
NC
6454 if (! CONST_STRNEQ (name, ".MIPS.events")
6455 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6456 return FALSE;
b49e97c9
TS
6457 break;
6458 default:
cc2e31b9 6459 break;
b49e97c9
TS
6460 }
6461
6dc132d9 6462 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6463 return FALSE;
b49e97c9
TS
6464
6465 if (flags)
6466 {
6467 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6468 (bfd_get_section_flags (abfd,
6469 hdr->bfd_section)
6470 | flags)))
b34976b6 6471 return FALSE;
b49e97c9
TS
6472 }
6473
6474 /* FIXME: We should record sh_info for a .gptab section. */
6475
6476 /* For a .reginfo section, set the gp value in the tdata information
6477 from the contents of this section. We need the gp value while
6478 processing relocs, so we just get it now. The .reginfo section
6479 is not used in the 64-bit MIPS ELF ABI. */
6480 if (hdr->sh_type == SHT_MIPS_REGINFO)
6481 {
6482 Elf32_External_RegInfo ext;
6483 Elf32_RegInfo s;
6484
9719ad41
RS
6485 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6486 &ext, 0, sizeof ext))
b34976b6 6487 return FALSE;
b49e97c9
TS
6488 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6489 elf_gp (abfd) = s.ri_gp_value;
6490 }
6491
6492 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6493 set the gp value based on what we find. We may see both
6494 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6495 they should agree. */
6496 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6497 {
6498 bfd_byte *contents, *l, *lend;
6499
9719ad41 6500 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6501 if (contents == NULL)
b34976b6 6502 return FALSE;
b49e97c9 6503 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6504 0, hdr->sh_size))
b49e97c9
TS
6505 {
6506 free (contents);
b34976b6 6507 return FALSE;
b49e97c9
TS
6508 }
6509 l = contents;
6510 lend = contents + hdr->sh_size;
6511 while (l + sizeof (Elf_External_Options) <= lend)
6512 {
6513 Elf_Internal_Options intopt;
6514
6515 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6516 &intopt);
1bc8074d
MR
6517 if (intopt.size < sizeof (Elf_External_Options))
6518 {
6519 (*_bfd_error_handler)
6520 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6521 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6522 break;
6523 }
b49e97c9
TS
6524 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6525 {
6526 Elf64_Internal_RegInfo intreg;
6527
6528 bfd_mips_elf64_swap_reginfo_in
6529 (abfd,
6530 ((Elf64_External_RegInfo *)
6531 (l + sizeof (Elf_External_Options))),
6532 &intreg);
6533 elf_gp (abfd) = intreg.ri_gp_value;
6534 }
6535 else if (intopt.kind == ODK_REGINFO)
6536 {
6537 Elf32_RegInfo intreg;
6538
6539 bfd_mips_elf32_swap_reginfo_in
6540 (abfd,
6541 ((Elf32_External_RegInfo *)
6542 (l + sizeof (Elf_External_Options))),
6543 &intreg);
6544 elf_gp (abfd) = intreg.ri_gp_value;
6545 }
6546 l += intopt.size;
6547 }
6548 free (contents);
6549 }
6550
b34976b6 6551 return TRUE;
b49e97c9
TS
6552}
6553
6554/* Set the correct type for a MIPS ELF section. We do this by the
6555 section name, which is a hack, but ought to work. This routine is
6556 used by both the 32-bit and the 64-bit ABI. */
6557
b34976b6 6558bfd_boolean
9719ad41 6559_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 6560{
0414f35b 6561 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
6562
6563 if (strcmp (name, ".liblist") == 0)
6564 {
6565 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 6566 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
6567 /* The sh_link field is set in final_write_processing. */
6568 }
6569 else if (strcmp (name, ".conflict") == 0)
6570 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 6571 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
6572 {
6573 hdr->sh_type = SHT_MIPS_GPTAB;
6574 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6575 /* The sh_info field is set in final_write_processing. */
6576 }
6577 else if (strcmp (name, ".ucode") == 0)
6578 hdr->sh_type = SHT_MIPS_UCODE;
6579 else if (strcmp (name, ".mdebug") == 0)
6580 {
6581 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 6582 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
6583 entsize of 0. FIXME: Does this matter? */
6584 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6585 hdr->sh_entsize = 0;
6586 else
6587 hdr->sh_entsize = 1;
6588 }
6589 else if (strcmp (name, ".reginfo") == 0)
6590 {
6591 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 6592 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
6593 entsize of 0x18. FIXME: Does this matter? */
6594 if (SGI_COMPAT (abfd))
6595 {
6596 if ((abfd->flags & DYNAMIC) != 0)
6597 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6598 else
6599 hdr->sh_entsize = 1;
6600 }
6601 else
6602 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6603 }
6604 else if (SGI_COMPAT (abfd)
6605 && (strcmp (name, ".hash") == 0
6606 || strcmp (name, ".dynamic") == 0
6607 || strcmp (name, ".dynstr") == 0))
6608 {
6609 if (SGI_COMPAT (abfd))
6610 hdr->sh_entsize = 0;
6611#if 0
8dc1a139 6612 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
6613 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6614#endif
6615 }
6616 else if (strcmp (name, ".got") == 0
6617 || strcmp (name, ".srdata") == 0
6618 || strcmp (name, ".sdata") == 0
6619 || strcmp (name, ".sbss") == 0
6620 || strcmp (name, ".lit4") == 0
6621 || strcmp (name, ".lit8") == 0)
6622 hdr->sh_flags |= SHF_MIPS_GPREL;
6623 else if (strcmp (name, ".MIPS.interfaces") == 0)
6624 {
6625 hdr->sh_type = SHT_MIPS_IFACE;
6626 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6627 }
0112cd26 6628 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
6629 {
6630 hdr->sh_type = SHT_MIPS_CONTENT;
6631 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6632 /* The sh_info field is set in final_write_processing. */
6633 }
cc2e31b9 6634 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
6635 {
6636 hdr->sh_type = SHT_MIPS_OPTIONS;
6637 hdr->sh_entsize = 1;
6638 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6639 }
1b315056
CS
6640 else if (CONST_STRNEQ (name, ".debug_")
6641 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
6642 {
6643 hdr->sh_type = SHT_MIPS_DWARF;
6644
6645 /* Irix facilities such as libexc expect a single .debug_frame
6646 per executable, the system ones have NOSTRIP set and the linker
6647 doesn't merge sections with different flags so ... */
6648 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6649 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6650 }
b49e97c9
TS
6651 else if (strcmp (name, ".MIPS.symlib") == 0)
6652 {
6653 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6654 /* The sh_link and sh_info fields are set in
6655 final_write_processing. */
6656 }
0112cd26
NC
6657 else if (CONST_STRNEQ (name, ".MIPS.events")
6658 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
6659 {
6660 hdr->sh_type = SHT_MIPS_EVENTS;
6661 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6662 /* The sh_link field is set in final_write_processing. */
6663 }
6664 else if (strcmp (name, ".msym") == 0)
6665 {
6666 hdr->sh_type = SHT_MIPS_MSYM;
6667 hdr->sh_flags |= SHF_ALLOC;
6668 hdr->sh_entsize = 8;
6669 }
6670
7a79a000
TS
6671 /* The generic elf_fake_sections will set up REL_HDR using the default
6672 kind of relocations. We used to set up a second header for the
6673 non-default kind of relocations here, but only NewABI would use
6674 these, and the IRIX ld doesn't like resulting empty RELA sections.
6675 Thus we create those header only on demand now. */
b49e97c9 6676
b34976b6 6677 return TRUE;
b49e97c9
TS
6678}
6679
6680/* Given a BFD section, try to locate the corresponding ELF section
6681 index. This is used by both the 32-bit and the 64-bit ABI.
6682 Actually, it's not clear to me that the 64-bit ABI supports these,
6683 but for non-PIC objects we will certainly want support for at least
6684 the .scommon section. */
6685
b34976b6 6686bfd_boolean
9719ad41
RS
6687_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6688 asection *sec, int *retval)
b49e97c9
TS
6689{
6690 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6691 {
6692 *retval = SHN_MIPS_SCOMMON;
b34976b6 6693 return TRUE;
b49e97c9
TS
6694 }
6695 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6696 {
6697 *retval = SHN_MIPS_ACOMMON;
b34976b6 6698 return TRUE;
b49e97c9 6699 }
b34976b6 6700 return FALSE;
b49e97c9
TS
6701}
6702\f
6703/* Hook called by the linker routine which adds symbols from an object
6704 file. We must handle the special MIPS section numbers here. */
6705
b34976b6 6706bfd_boolean
9719ad41 6707_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 6708 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
6709 flagword *flagsp ATTRIBUTE_UNUSED,
6710 asection **secp, bfd_vma *valp)
b49e97c9
TS
6711{
6712 if (SGI_COMPAT (abfd)
6713 && (abfd->flags & DYNAMIC) != 0
6714 && strcmp (*namep, "_rld_new_interface") == 0)
6715 {
8dc1a139 6716 /* Skip IRIX5 rld entry name. */
b49e97c9 6717 *namep = NULL;
b34976b6 6718 return TRUE;
b49e97c9
TS
6719 }
6720
eedecc07
DD
6721 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6722 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6723 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6724 a magic symbol resolved by the linker, we ignore this bogus definition
6725 of _gp_disp. New ABI objects do not suffer from this problem so this
6726 is not done for them. */
6727 if (!NEWABI_P(abfd)
6728 && (sym->st_shndx == SHN_ABS)
6729 && (strcmp (*namep, "_gp_disp") == 0))
6730 {
6731 *namep = NULL;
6732 return TRUE;
6733 }
6734
b49e97c9
TS
6735 switch (sym->st_shndx)
6736 {
6737 case SHN_COMMON:
6738 /* Common symbols less than the GP size are automatically
6739 treated as SHN_MIPS_SCOMMON symbols. */
6740 if (sym->st_size > elf_gp_size (abfd)
b59eed79 6741 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
6742 || IRIX_COMPAT (abfd) == ict_irix6)
6743 break;
6744 /* Fall through. */
6745 case SHN_MIPS_SCOMMON:
6746 *secp = bfd_make_section_old_way (abfd, ".scommon");
6747 (*secp)->flags |= SEC_IS_COMMON;
6748 *valp = sym->st_size;
6749 break;
6750
6751 case SHN_MIPS_TEXT:
6752 /* This section is used in a shared object. */
6753 if (elf_tdata (abfd)->elf_text_section == NULL)
6754 {
6755 asymbol *elf_text_symbol;
6756 asection *elf_text_section;
6757 bfd_size_type amt = sizeof (asection);
6758
6759 elf_text_section = bfd_zalloc (abfd, amt);
6760 if (elf_text_section == NULL)
b34976b6 6761 return FALSE;
b49e97c9
TS
6762
6763 amt = sizeof (asymbol);
6764 elf_text_symbol = bfd_zalloc (abfd, amt);
6765 if (elf_text_symbol == NULL)
b34976b6 6766 return FALSE;
b49e97c9
TS
6767
6768 /* Initialize the section. */
6769
6770 elf_tdata (abfd)->elf_text_section = elf_text_section;
6771 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6772
6773 elf_text_section->symbol = elf_text_symbol;
6774 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6775
6776 elf_text_section->name = ".text";
6777 elf_text_section->flags = SEC_NO_FLAGS;
6778 elf_text_section->output_section = NULL;
6779 elf_text_section->owner = abfd;
6780 elf_text_symbol->name = ".text";
6781 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6782 elf_text_symbol->section = elf_text_section;
6783 }
6784 /* This code used to do *secp = bfd_und_section_ptr if
6785 info->shared. I don't know why, and that doesn't make sense,
6786 so I took it out. */
6787 *secp = elf_tdata (abfd)->elf_text_section;
6788 break;
6789
6790 case SHN_MIPS_ACOMMON:
6791 /* Fall through. XXX Can we treat this as allocated data? */
6792 case SHN_MIPS_DATA:
6793 /* This section is used in a shared object. */
6794 if (elf_tdata (abfd)->elf_data_section == NULL)
6795 {
6796 asymbol *elf_data_symbol;
6797 asection *elf_data_section;
6798 bfd_size_type amt = sizeof (asection);
6799
6800 elf_data_section = bfd_zalloc (abfd, amt);
6801 if (elf_data_section == NULL)
b34976b6 6802 return FALSE;
b49e97c9
TS
6803
6804 amt = sizeof (asymbol);
6805 elf_data_symbol = bfd_zalloc (abfd, amt);
6806 if (elf_data_symbol == NULL)
b34976b6 6807 return FALSE;
b49e97c9
TS
6808
6809 /* Initialize the section. */
6810
6811 elf_tdata (abfd)->elf_data_section = elf_data_section;
6812 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6813
6814 elf_data_section->symbol = elf_data_symbol;
6815 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6816
6817 elf_data_section->name = ".data";
6818 elf_data_section->flags = SEC_NO_FLAGS;
6819 elf_data_section->output_section = NULL;
6820 elf_data_section->owner = abfd;
6821 elf_data_symbol->name = ".data";
6822 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6823 elf_data_symbol->section = elf_data_section;
6824 }
6825 /* This code used to do *secp = bfd_und_section_ptr if
6826 info->shared. I don't know why, and that doesn't make sense,
6827 so I took it out. */
6828 *secp = elf_tdata (abfd)->elf_data_section;
6829 break;
6830
6831 case SHN_MIPS_SUNDEFINED:
6832 *secp = bfd_und_section_ptr;
6833 break;
6834 }
6835
6836 if (SGI_COMPAT (abfd)
6837 && ! info->shared
f13a99db 6838 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
6839 && strcmp (*namep, "__rld_obj_head") == 0)
6840 {
6841 struct elf_link_hash_entry *h;
14a793b2 6842 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6843
6844 /* Mark __rld_obj_head as dynamic. */
14a793b2 6845 bh = NULL;
b49e97c9 6846 if (! (_bfd_generic_link_add_one_symbol
9719ad41 6847 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 6848 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6849 return FALSE;
14a793b2
AM
6850
6851 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6852 h->non_elf = 0;
6853 h->def_regular = 1;
b49e97c9
TS
6854 h->type = STT_OBJECT;
6855
c152c796 6856 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6857 return FALSE;
b49e97c9 6858
b34976b6 6859 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b49e97c9
TS
6860 }
6861
6862 /* If this is a mips16 text symbol, add 1 to the value to make it
6863 odd. This will cause something like .word SYM to come up with
6864 the right value when it is loaded into the PC. */
30c09090 6865 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
6866 ++*valp;
6867
b34976b6 6868 return TRUE;
b49e97c9
TS
6869}
6870
6871/* This hook function is called before the linker writes out a global
6872 symbol. We mark symbols as small common if appropriate. This is
6873 also where we undo the increment of the value for a mips16 symbol. */
6874
6e0b88f1 6875int
9719ad41
RS
6876_bfd_mips_elf_link_output_symbol_hook
6877 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6878 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6879 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
6880{
6881 /* If we see a common symbol, which implies a relocatable link, then
6882 if a symbol was small common in an input file, mark it as small
6883 common in the output file. */
6884 if (sym->st_shndx == SHN_COMMON
6885 && strcmp (input_sec->name, ".scommon") == 0)
6886 sym->st_shndx = SHN_MIPS_SCOMMON;
6887
30c09090 6888 if (ELF_ST_IS_MIPS16 (sym->st_other))
79cda7cf 6889 sym->st_value &= ~1;
b49e97c9 6890
6e0b88f1 6891 return 1;
b49e97c9
TS
6892}
6893\f
6894/* Functions for the dynamic linker. */
6895
6896/* Create dynamic sections when linking against a dynamic object. */
6897
b34976b6 6898bfd_boolean
9719ad41 6899_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
6900{
6901 struct elf_link_hash_entry *h;
14a793b2 6902 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6903 flagword flags;
6904 register asection *s;
6905 const char * const *namep;
0a44bf69 6906 struct mips_elf_link_hash_table *htab;
b49e97c9 6907
0a44bf69 6908 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6909 BFD_ASSERT (htab != NULL);
6910
b49e97c9
TS
6911 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6912 | SEC_LINKER_CREATED | SEC_READONLY);
6913
0a44bf69
RS
6914 /* The psABI requires a read-only .dynamic section, but the VxWorks
6915 EABI doesn't. */
6916 if (!htab->is_vxworks)
b49e97c9 6917 {
0a44bf69
RS
6918 s = bfd_get_section_by_name (abfd, ".dynamic");
6919 if (s != NULL)
6920 {
6921 if (! bfd_set_section_flags (abfd, s, flags))
6922 return FALSE;
6923 }
b49e97c9
TS
6924 }
6925
6926 /* We need to create .got section. */
23cc69b6 6927 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
6928 return FALSE;
6929
0a44bf69 6930 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 6931 return FALSE;
b49e97c9 6932
b49e97c9 6933 /* Create .stub section. */
4e41d0d7
RS
6934 s = bfd_make_section_with_flags (abfd,
6935 MIPS_ELF_STUB_SECTION_NAME (abfd),
6936 flags | SEC_CODE);
6937 if (s == NULL
6938 || ! bfd_set_section_alignment (abfd, s,
6939 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6940 return FALSE;
6941 htab->sstubs = s;
b49e97c9
TS
6942
6943 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6944 && !info->shared
6945 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6946 {
3496cb2a
L
6947 s = bfd_make_section_with_flags (abfd, ".rld_map",
6948 flags &~ (flagword) SEC_READONLY);
b49e97c9 6949 if (s == NULL
b49e97c9
TS
6950 || ! bfd_set_section_alignment (abfd, s,
6951 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 6952 return FALSE;
b49e97c9
TS
6953 }
6954
6955 /* On IRIX5, we adjust add some additional symbols and change the
6956 alignments of several sections. There is no ABI documentation
6957 indicating that this is necessary on IRIX6, nor any evidence that
6958 the linker takes such action. */
6959 if (IRIX_COMPAT (abfd) == ict_irix5)
6960 {
6961 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6962 {
14a793b2 6963 bh = NULL;
b49e97c9 6964 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
6965 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6966 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6967 return FALSE;
14a793b2
AM
6968
6969 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6970 h->non_elf = 0;
6971 h->def_regular = 1;
b49e97c9
TS
6972 h->type = STT_SECTION;
6973
c152c796 6974 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6975 return FALSE;
b49e97c9
TS
6976 }
6977
6978 /* We need to create a .compact_rel section. */
6979 if (SGI_COMPAT (abfd))
6980 {
6981 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 6982 return FALSE;
b49e97c9
TS
6983 }
6984
44c410de 6985 /* Change alignments of some sections. */
b49e97c9
TS
6986 s = bfd_get_section_by_name (abfd, ".hash");
6987 if (s != NULL)
d80dcc6a 6988 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6989 s = bfd_get_section_by_name (abfd, ".dynsym");
6990 if (s != NULL)
d80dcc6a 6991 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6992 s = bfd_get_section_by_name (abfd, ".dynstr");
6993 if (s != NULL)
d80dcc6a 6994 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6995 s = bfd_get_section_by_name (abfd, ".reginfo");
6996 if (s != NULL)
d80dcc6a 6997 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6998 s = bfd_get_section_by_name (abfd, ".dynamic");
6999 if (s != NULL)
d80dcc6a 7000 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7001 }
7002
7003 if (!info->shared)
7004 {
14a793b2
AM
7005 const char *name;
7006
7007 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7008 bh = NULL;
7009 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
7010 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7011 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7012 return FALSE;
14a793b2
AM
7013
7014 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7015 h->non_elf = 0;
7016 h->def_regular = 1;
b49e97c9
TS
7017 h->type = STT_SECTION;
7018
c152c796 7019 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7020 return FALSE;
b49e97c9
TS
7021
7022 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7023 {
7024 /* __rld_map is a four byte word located in the .data section
7025 and is filled in by the rtld to contain a pointer to
7026 the _r_debug structure. Its symbol value will be set in
7027 _bfd_mips_elf_finish_dynamic_symbol. */
7028 s = bfd_get_section_by_name (abfd, ".rld_map");
0abfb97a 7029 BFD_ASSERT (s != NULL);
14a793b2 7030
0abfb97a
L
7031 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7032 bh = NULL;
7033 if (!(_bfd_generic_link_add_one_symbol
7034 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7035 get_elf_backend_data (abfd)->collect, &bh)))
7036 return FALSE;
b49e97c9 7037
0abfb97a
L
7038 h = (struct elf_link_hash_entry *) bh;
7039 h->non_elf = 0;
7040 h->def_regular = 1;
7041 h->type = STT_OBJECT;
7042
7043 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7044 return FALSE;
b49e97c9
TS
7045 }
7046 }
7047
861fb55a
DJ
7048 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7049 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7050 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7051 return FALSE;
7052
7053 /* Cache the sections created above. */
7054 htab->splt = bfd_get_section_by_name (abfd, ".plt");
7055 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
0a44bf69
RS
7056 if (htab->is_vxworks)
7057 {
0a44bf69
RS
7058 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7059 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
861fb55a
DJ
7060 }
7061 else
7062 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7063 if (!htab->sdynbss
7064 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7065 || !htab->srelplt
7066 || !htab->splt)
7067 abort ();
0a44bf69 7068
861fb55a
DJ
7069 if (htab->is_vxworks)
7070 {
0a44bf69
RS
7071 /* Do the usual VxWorks handling. */
7072 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7073 return FALSE;
7074
7075 /* Work out the PLT sizes. */
7076 if (info->shared)
7077 {
7078 htab->plt_header_size
7079 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7080 htab->plt_entry_size
7081 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7082 }
7083 else
7084 {
7085 htab->plt_header_size
7086 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7087 htab->plt_entry_size
7088 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7089 }
7090 }
861fb55a
DJ
7091 else if (!info->shared)
7092 {
7093 /* All variants of the plt0 entry are the same size. */
7094 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7095 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7096 }
0a44bf69 7097
b34976b6 7098 return TRUE;
b49e97c9
TS
7099}
7100\f
c224138d
RS
7101/* Return true if relocation REL against section SEC is a REL rather than
7102 RELA relocation. RELOCS is the first relocation in the section and
7103 ABFD is the bfd that contains SEC. */
7104
7105static bfd_boolean
7106mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7107 const Elf_Internal_Rela *relocs,
7108 const Elf_Internal_Rela *rel)
7109{
7110 Elf_Internal_Shdr *rel_hdr;
7111 const struct elf_backend_data *bed;
7112
7113 /* To determine which flavor or relocation this is, we depend on the
7114 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
7115 rel_hdr = &elf_section_data (sec)->rel_hdr;
7116 bed = get_elf_backend_data (abfd);
7117 if ((size_t) (rel - relocs)
7118 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7119 rel_hdr = elf_section_data (sec)->rel_hdr2;
7120 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
7121}
7122
7123/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7124 HOWTO is the relocation's howto and CONTENTS points to the contents
7125 of the section that REL is against. */
7126
7127static bfd_vma
7128mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7129 reloc_howto_type *howto, bfd_byte *contents)
7130{
7131 bfd_byte *location;
7132 unsigned int r_type;
7133 bfd_vma addend;
7134
7135 r_type = ELF_R_TYPE (abfd, rel->r_info);
7136 location = contents + rel->r_offset;
7137
7138 /* Get the addend, which is stored in the input file. */
7139 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7140 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7141 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7142
7143 return addend & howto->src_mask;
7144}
7145
7146/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7147 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7148 and update *ADDEND with the final addend. Return true on success
7149 or false if the LO16 could not be found. RELEND is the exclusive
7150 upper bound on the relocations for REL's section. */
7151
7152static bfd_boolean
7153mips_elf_add_lo16_rel_addend (bfd *abfd,
7154 const Elf_Internal_Rela *rel,
7155 const Elf_Internal_Rela *relend,
7156 bfd_byte *contents, bfd_vma *addend)
7157{
7158 unsigned int r_type, lo16_type;
7159 const Elf_Internal_Rela *lo16_relocation;
7160 reloc_howto_type *lo16_howto;
7161 bfd_vma l;
7162
7163 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7164 if (mips16_reloc_p (r_type))
c224138d
RS
7165 lo16_type = R_MIPS16_LO16;
7166 else
7167 lo16_type = R_MIPS_LO16;
7168
7169 /* The combined value is the sum of the HI16 addend, left-shifted by
7170 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7171 code does a `lui' of the HI16 value, and then an `addiu' of the
7172 LO16 value.)
7173
7174 Scan ahead to find a matching LO16 relocation.
7175
7176 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7177 be immediately following. However, for the IRIX6 ABI, the next
7178 relocation may be a composed relocation consisting of several
7179 relocations for the same address. In that case, the R_MIPS_LO16
7180 relocation may occur as one of these. We permit a similar
7181 extension in general, as that is useful for GCC.
7182
7183 In some cases GCC dead code elimination removes the LO16 but keeps
7184 the corresponding HI16. This is strictly speaking a violation of
7185 the ABI but not immediately harmful. */
7186 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7187 if (lo16_relocation == NULL)
7188 return FALSE;
7189
7190 /* Obtain the addend kept there. */
7191 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7192 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7193
7194 l <<= lo16_howto->rightshift;
7195 l = _bfd_mips_elf_sign_extend (l, 16);
7196
7197 *addend <<= 16;
7198 *addend += l;
7199 return TRUE;
7200}
7201
7202/* Try to read the contents of section SEC in bfd ABFD. Return true and
7203 store the contents in *CONTENTS on success. Assume that *CONTENTS
7204 already holds the contents if it is nonull on entry. */
7205
7206static bfd_boolean
7207mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7208{
7209 if (*contents)
7210 return TRUE;
7211
7212 /* Get cached copy if it exists. */
7213 if (elf_section_data (sec)->this_hdr.contents != NULL)
7214 {
7215 *contents = elf_section_data (sec)->this_hdr.contents;
7216 return TRUE;
7217 }
7218
7219 return bfd_malloc_and_get_section (abfd, sec, contents);
7220}
7221
b49e97c9
TS
7222/* Look through the relocs for a section during the first phase, and
7223 allocate space in the global offset table. */
7224
b34976b6 7225bfd_boolean
9719ad41
RS
7226_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7227 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7228{
7229 const char *name;
7230 bfd *dynobj;
7231 Elf_Internal_Shdr *symtab_hdr;
7232 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7233 size_t extsymoff;
7234 const Elf_Internal_Rela *rel;
7235 const Elf_Internal_Rela *rel_end;
b49e97c9 7236 asection *sreloc;
9c5bfbb7 7237 const struct elf_backend_data *bed;
0a44bf69 7238 struct mips_elf_link_hash_table *htab;
c224138d
RS
7239 bfd_byte *contents;
7240 bfd_vma addend;
7241 reloc_howto_type *howto;
b49e97c9 7242
1049f94e 7243 if (info->relocatable)
b34976b6 7244 return TRUE;
b49e97c9 7245
0a44bf69 7246 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7247 BFD_ASSERT (htab != NULL);
7248
b49e97c9
TS
7249 dynobj = elf_hash_table (info)->dynobj;
7250 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7251 sym_hashes = elf_sym_hashes (abfd);
7252 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7253
738e5348
RS
7254 bed = get_elf_backend_data (abfd);
7255 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7256
b49e97c9
TS
7257 /* Check for the mips16 stub sections. */
7258
7259 name = bfd_get_section_name (abfd, sec);
b9d58d71 7260 if (FN_STUB_P (name))
b49e97c9
TS
7261 {
7262 unsigned long r_symndx;
7263
7264 /* Look at the relocation information to figure out which symbol
7265 this is for. */
7266
738e5348
RS
7267 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7268 if (r_symndx == 0)
7269 {
7270 (*_bfd_error_handler)
7271 (_("%B: Warning: cannot determine the target function for"
7272 " stub section `%s'"),
7273 abfd, name);
7274 bfd_set_error (bfd_error_bad_value);
7275 return FALSE;
7276 }
b49e97c9
TS
7277
7278 if (r_symndx < extsymoff
7279 || sym_hashes[r_symndx - extsymoff] == NULL)
7280 {
7281 asection *o;
7282
7283 /* This stub is for a local symbol. This stub will only be
7284 needed if there is some relocation in this BFD, other
7285 than a 16 bit function call, which refers to this symbol. */
7286 for (o = abfd->sections; o != NULL; o = o->next)
7287 {
7288 Elf_Internal_Rela *sec_relocs;
7289 const Elf_Internal_Rela *r, *rend;
7290
7291 /* We can ignore stub sections when looking for relocs. */
7292 if ((o->flags & SEC_RELOC) == 0
7293 || o->reloc_count == 0
738e5348 7294 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7295 continue;
7296
45d6a902 7297 sec_relocs
9719ad41 7298 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7299 info->keep_memory);
b49e97c9 7300 if (sec_relocs == NULL)
b34976b6 7301 return FALSE;
b49e97c9
TS
7302
7303 rend = sec_relocs + o->reloc_count;
7304 for (r = sec_relocs; r < rend; r++)
7305 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7306 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7307 break;
7308
6cdc0ccc 7309 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7310 free (sec_relocs);
7311
7312 if (r < rend)
7313 break;
7314 }
7315
7316 if (o == NULL)
7317 {
7318 /* There is no non-call reloc for this stub, so we do
7319 not need it. Since this function is called before
7320 the linker maps input sections to output sections, we
7321 can easily discard it by setting the SEC_EXCLUDE
7322 flag. */
7323 sec->flags |= SEC_EXCLUDE;
b34976b6 7324 return TRUE;
b49e97c9
TS
7325 }
7326
7327 /* Record this stub in an array of local symbol stubs for
7328 this BFD. */
7329 if (elf_tdata (abfd)->local_stubs == NULL)
7330 {
7331 unsigned long symcount;
7332 asection **n;
7333 bfd_size_type amt;
7334
7335 if (elf_bad_symtab (abfd))
7336 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7337 else
7338 symcount = symtab_hdr->sh_info;
7339 amt = symcount * sizeof (asection *);
9719ad41 7340 n = bfd_zalloc (abfd, amt);
b49e97c9 7341 if (n == NULL)
b34976b6 7342 return FALSE;
b49e97c9
TS
7343 elf_tdata (abfd)->local_stubs = n;
7344 }
7345
b9d58d71 7346 sec->flags |= SEC_KEEP;
b49e97c9
TS
7347 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7348
7349 /* We don't need to set mips16_stubs_seen in this case.
7350 That flag is used to see whether we need to look through
7351 the global symbol table for stubs. We don't need to set
7352 it here, because we just have a local stub. */
7353 }
7354 else
7355 {
7356 struct mips_elf_link_hash_entry *h;
7357
7358 h = ((struct mips_elf_link_hash_entry *)
7359 sym_hashes[r_symndx - extsymoff]);
7360
973a3492
L
7361 while (h->root.root.type == bfd_link_hash_indirect
7362 || h->root.root.type == bfd_link_hash_warning)
7363 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7364
b49e97c9
TS
7365 /* H is the symbol this stub is for. */
7366
b9d58d71
TS
7367 /* If we already have an appropriate stub for this function, we
7368 don't need another one, so we can discard this one. Since
7369 this function is called before the linker maps input sections
7370 to output sections, we can easily discard it by setting the
7371 SEC_EXCLUDE flag. */
7372 if (h->fn_stub != NULL)
7373 {
7374 sec->flags |= SEC_EXCLUDE;
7375 return TRUE;
7376 }
7377
7378 sec->flags |= SEC_KEEP;
b49e97c9 7379 h->fn_stub = sec;
b34976b6 7380 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7381 }
7382 }
b9d58d71 7383 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7384 {
7385 unsigned long r_symndx;
7386 struct mips_elf_link_hash_entry *h;
7387 asection **loc;
7388
7389 /* Look at the relocation information to figure out which symbol
7390 this is for. */
7391
738e5348
RS
7392 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7393 if (r_symndx == 0)
7394 {
7395 (*_bfd_error_handler)
7396 (_("%B: Warning: cannot determine the target function for"
7397 " stub section `%s'"),
7398 abfd, name);
7399 bfd_set_error (bfd_error_bad_value);
7400 return FALSE;
7401 }
b49e97c9
TS
7402
7403 if (r_symndx < extsymoff
7404 || sym_hashes[r_symndx - extsymoff] == NULL)
7405 {
b9d58d71 7406 asection *o;
b49e97c9 7407
b9d58d71
TS
7408 /* This stub is for a local symbol. This stub will only be
7409 needed if there is some relocation (R_MIPS16_26) in this BFD
7410 that refers to this symbol. */
7411 for (o = abfd->sections; o != NULL; o = o->next)
7412 {
7413 Elf_Internal_Rela *sec_relocs;
7414 const Elf_Internal_Rela *r, *rend;
7415
7416 /* We can ignore stub sections when looking for relocs. */
7417 if ((o->flags & SEC_RELOC) == 0
7418 || o->reloc_count == 0
738e5348 7419 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7420 continue;
7421
7422 sec_relocs
7423 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7424 info->keep_memory);
7425 if (sec_relocs == NULL)
7426 return FALSE;
7427
7428 rend = sec_relocs + o->reloc_count;
7429 for (r = sec_relocs; r < rend; r++)
7430 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7431 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7432 break;
7433
7434 if (elf_section_data (o)->relocs != sec_relocs)
7435 free (sec_relocs);
7436
7437 if (r < rend)
7438 break;
7439 }
7440
7441 if (o == NULL)
7442 {
7443 /* There is no non-call reloc for this stub, so we do
7444 not need it. Since this function is called before
7445 the linker maps input sections to output sections, we
7446 can easily discard it by setting the SEC_EXCLUDE
7447 flag. */
7448 sec->flags |= SEC_EXCLUDE;
7449 return TRUE;
7450 }
7451
7452 /* Record this stub in an array of local symbol call_stubs for
7453 this BFD. */
7454 if (elf_tdata (abfd)->local_call_stubs == NULL)
7455 {
7456 unsigned long symcount;
7457 asection **n;
7458 bfd_size_type amt;
7459
7460 if (elf_bad_symtab (abfd))
7461 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7462 else
7463 symcount = symtab_hdr->sh_info;
7464 amt = symcount * sizeof (asection *);
7465 n = bfd_zalloc (abfd, amt);
7466 if (n == NULL)
7467 return FALSE;
7468 elf_tdata (abfd)->local_call_stubs = n;
7469 }
b49e97c9 7470
b9d58d71
TS
7471 sec->flags |= SEC_KEEP;
7472 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7473
b9d58d71
TS
7474 /* We don't need to set mips16_stubs_seen in this case.
7475 That flag is used to see whether we need to look through
7476 the global symbol table for stubs. We don't need to set
7477 it here, because we just have a local stub. */
7478 }
b49e97c9 7479 else
b49e97c9 7480 {
b9d58d71
TS
7481 h = ((struct mips_elf_link_hash_entry *)
7482 sym_hashes[r_symndx - extsymoff]);
7483
7484 /* H is the symbol this stub is for. */
7485
7486 if (CALL_FP_STUB_P (name))
7487 loc = &h->call_fp_stub;
7488 else
7489 loc = &h->call_stub;
7490
7491 /* If we already have an appropriate stub for this function, we
7492 don't need another one, so we can discard this one. Since
7493 this function is called before the linker maps input sections
7494 to output sections, we can easily discard it by setting the
7495 SEC_EXCLUDE flag. */
7496 if (*loc != NULL)
7497 {
7498 sec->flags |= SEC_EXCLUDE;
7499 return TRUE;
7500 }
b49e97c9 7501
b9d58d71
TS
7502 sec->flags |= SEC_KEEP;
7503 *loc = sec;
7504 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7505 }
b49e97c9
TS
7506 }
7507
b49e97c9 7508 sreloc = NULL;
c224138d 7509 contents = NULL;
b49e97c9
TS
7510 for (rel = relocs; rel < rel_end; ++rel)
7511 {
7512 unsigned long r_symndx;
7513 unsigned int r_type;
7514 struct elf_link_hash_entry *h;
861fb55a 7515 bfd_boolean can_make_dynamic_p;
b49e97c9
TS
7516
7517 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7518 r_type = ELF_R_TYPE (abfd, rel->r_info);
7519
7520 if (r_symndx < extsymoff)
7521 h = NULL;
7522 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7523 {
7524 (*_bfd_error_handler)
d003868e
AM
7525 (_("%B: Malformed reloc detected for section %s"),
7526 abfd, name);
b49e97c9 7527 bfd_set_error (bfd_error_bad_value);
b34976b6 7528 return FALSE;
b49e97c9
TS
7529 }
7530 else
7531 {
7532 h = sym_hashes[r_symndx - extsymoff];
3e08fb72
NC
7533 while (h != NULL
7534 && (h->root.type == bfd_link_hash_indirect
7535 || h->root.type == bfd_link_hash_warning))
861fb55a
DJ
7536 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7537 }
b49e97c9 7538
861fb55a
DJ
7539 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7540 relocation into a dynamic one. */
7541 can_make_dynamic_p = FALSE;
7542 switch (r_type)
7543 {
7544 case R_MIPS16_GOT16:
7545 case R_MIPS16_CALL16:
7546 case R_MIPS_GOT16:
7547 case R_MIPS_CALL16:
7548 case R_MIPS_CALL_HI16:
7549 case R_MIPS_CALL_LO16:
7550 case R_MIPS_GOT_HI16:
7551 case R_MIPS_GOT_LO16:
7552 case R_MIPS_GOT_PAGE:
7553 case R_MIPS_GOT_OFST:
7554 case R_MIPS_GOT_DISP:
7555 case R_MIPS_TLS_GOTTPREL:
7556 case R_MIPS_TLS_GD:
7557 case R_MIPS_TLS_LDM:
7558 if (dynobj == NULL)
7559 elf_hash_table (info)->dynobj = dynobj = abfd;
7560 if (!mips_elf_create_got_section (dynobj, info))
7561 return FALSE;
7562 if (htab->is_vxworks && !info->shared)
b49e97c9 7563 {
861fb55a
DJ
7564 (*_bfd_error_handler)
7565 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7566 abfd, (unsigned long) rel->r_offset);
7567 bfd_set_error (bfd_error_bad_value);
7568 return FALSE;
b49e97c9 7569 }
861fb55a 7570 break;
b49e97c9 7571
99da6b5f
AN
7572 /* This is just a hint; it can safely be ignored. Don't set
7573 has_static_relocs for the corresponding symbol. */
7574 case R_MIPS_JALR:
7575 break;
7576
861fb55a
DJ
7577 case R_MIPS_32:
7578 case R_MIPS_REL32:
7579 case R_MIPS_64:
7580 /* In VxWorks executables, references to external symbols
7581 must be handled using copy relocs or PLT entries; it is not
7582 possible to convert this relocation into a dynamic one.
7583
7584 For executables that use PLTs and copy-relocs, we have a
7585 choice between converting the relocation into a dynamic
7586 one or using copy relocations or PLT entries. It is
7587 usually better to do the former, unless the relocation is
7588 against a read-only section. */
7589 if ((info->shared
7590 || (h != NULL
7591 && !htab->is_vxworks
7592 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7593 && !(!info->nocopyreloc
7594 && !PIC_OBJECT_P (abfd)
7595 && MIPS_ELF_READONLY_SECTION (sec))))
7596 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 7597 {
861fb55a 7598 can_make_dynamic_p = TRUE;
b49e97c9
TS
7599 if (dynobj == NULL)
7600 elf_hash_table (info)->dynobj = dynobj = abfd;
b49e97c9 7601 break;
861fb55a
DJ
7602 }
7603 /* Fall through. */
b49e97c9 7604
861fb55a
DJ
7605 default:
7606 /* Most static relocations require pointer equality, except
7607 for branches. */
7608 if (h)
7609 h->pointer_equality_needed = TRUE;
7610 /* Fall through. */
b49e97c9 7611
861fb55a
DJ
7612 case R_MIPS_26:
7613 case R_MIPS_PC16:
7614 case R_MIPS16_26:
7615 if (h)
7616 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7617 break;
b49e97c9
TS
7618 }
7619
0a44bf69
RS
7620 if (h)
7621 {
0a44bf69
RS
7622 /* Relocations against the special VxWorks __GOTT_BASE__ and
7623 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7624 room for them in .rela.dyn. */
7625 if (is_gott_symbol (info, h))
7626 {
7627 if (sreloc == NULL)
7628 {
7629 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7630 if (sreloc == NULL)
7631 return FALSE;
7632 }
7633 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
7634 if (MIPS_ELF_READONLY_SECTION (sec))
7635 /* We tell the dynamic linker that there are
7636 relocations against the text segment. */
7637 info->flags |= DF_TEXTREL;
0a44bf69
RS
7638 }
7639 }
7640 else if (r_type == R_MIPS_CALL_LO16
7641 || r_type == R_MIPS_GOT_LO16
7642 || r_type == R_MIPS_GOT_DISP
738e5348 7643 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
7644 {
7645 /* We may need a local GOT entry for this relocation. We
7646 don't count R_MIPS_GOT_PAGE because we can estimate the
7647 maximum number of pages needed by looking at the size of
738e5348
RS
7648 the segment. Similar comments apply to R_MIPS*_GOT16 and
7649 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 7650 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 7651 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 7652 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0
RS
7653 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7654 rel->r_addend, info, 0))
f4416af6 7655 return FALSE;
b49e97c9
TS
7656 }
7657
861fb55a
DJ
7658 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7659 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7660
b49e97c9
TS
7661 switch (r_type)
7662 {
7663 case R_MIPS_CALL16:
738e5348 7664 case R_MIPS16_CALL16:
b49e97c9
TS
7665 if (h == NULL)
7666 {
7667 (*_bfd_error_handler)
d003868e
AM
7668 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7669 abfd, (unsigned long) rel->r_offset);
b49e97c9 7670 bfd_set_error (bfd_error_bad_value);
b34976b6 7671 return FALSE;
b49e97c9
TS
7672 }
7673 /* Fall through. */
7674
7675 case R_MIPS_CALL_HI16:
7676 case R_MIPS_CALL_LO16:
7677 if (h != NULL)
7678 {
6ccf4795
RS
7679 /* Make sure there is room in the regular GOT to hold the
7680 function's address. We may eliminate it in favour of
7681 a .got.plt entry later; see mips_elf_count_got_symbols. */
7682 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE, 0))
b34976b6 7683 return FALSE;
b49e97c9
TS
7684
7685 /* We need a stub, not a plt entry for the undefined
7686 function. But we record it as if it needs plt. See
c152c796 7687 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 7688 h->needs_plt = 1;
b49e97c9
TS
7689 h->type = STT_FUNC;
7690 }
7691 break;
7692
0fdc1bf1
AO
7693 case R_MIPS_GOT_PAGE:
7694 /* If this is a global, overridable symbol, GOT_PAGE will
7695 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d 7696 if (h)
0fdc1bf1
AO
7697 {
7698 struct mips_elf_link_hash_entry *hmips =
7699 (struct mips_elf_link_hash_entry *) h;
143d77c5 7700
3a3b6725 7701 /* This symbol is definitely not overridable. */
f5385ebf 7702 if (hmips->root.def_regular
0fdc1bf1 7703 && ! (info->shared && ! info->symbolic
f5385ebf 7704 && ! hmips->root.forced_local))
c224138d 7705 h = NULL;
0fdc1bf1
AO
7706 }
7707 /* Fall through. */
7708
738e5348 7709 case R_MIPS16_GOT16:
b49e97c9
TS
7710 case R_MIPS_GOT16:
7711 case R_MIPS_GOT_HI16:
7712 case R_MIPS_GOT_LO16:
3a3b6725 7713 if (!h || r_type == R_MIPS_GOT_PAGE)
c224138d 7714 {
3a3b6725
DJ
7715 /* This relocation needs (or may need, if h != NULL) a
7716 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7717 know for sure until we know whether the symbol is
7718 preemptible. */
c224138d
RS
7719 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7720 {
7721 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7722 return FALSE;
7723 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7724 addend = mips_elf_read_rel_addend (abfd, rel,
7725 howto, contents);
9684f078 7726 if (got16_reloc_p (r_type))
c224138d
RS
7727 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7728 contents, &addend);
7729 else
7730 addend <<= howto->rightshift;
7731 }
7732 else
7733 addend = rel->r_addend;
a8028dd0
RS
7734 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7735 addend))
c224138d
RS
7736 return FALSE;
7737 break;
7738 }
7739 /* Fall through. */
7740
b49e97c9 7741 case R_MIPS_GOT_DISP:
6ccf4795
RS
7742 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
7743 FALSE, 0))
b34976b6 7744 return FALSE;
b49e97c9
TS
7745 break;
7746
0f20cc35
DJ
7747 case R_MIPS_TLS_GOTTPREL:
7748 if (info->shared)
7749 info->flags |= DF_STATIC_TLS;
7750 /* Fall through */
7751
7752 case R_MIPS_TLS_LDM:
7753 if (r_type == R_MIPS_TLS_LDM)
7754 {
7755 r_symndx = 0;
7756 h = NULL;
7757 }
7758 /* Fall through */
7759
7760 case R_MIPS_TLS_GD:
7761 /* This symbol requires a global offset table entry, or two
7762 for TLS GD relocations. */
7763 {
7764 unsigned char flag = (r_type == R_MIPS_TLS_GD
7765 ? GOT_TLS_GD
7766 : r_type == R_MIPS_TLS_LDM
7767 ? GOT_TLS_LDM
7768 : GOT_TLS_IE);
7769 if (h != NULL)
7770 {
7771 struct mips_elf_link_hash_entry *hmips =
7772 (struct mips_elf_link_hash_entry *) h;
7773 hmips->tls_type |= flag;
7774
6ccf4795
RS
7775 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
7776 FALSE, flag))
0f20cc35
DJ
7777 return FALSE;
7778 }
7779 else
7780 {
7781 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7782
a8028dd0
RS
7783 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7784 rel->r_addend,
7785 info, flag))
0f20cc35
DJ
7786 return FALSE;
7787 }
7788 }
7789 break;
7790
b49e97c9
TS
7791 case R_MIPS_32:
7792 case R_MIPS_REL32:
7793 case R_MIPS_64:
0a44bf69
RS
7794 /* In VxWorks executables, references to external symbols
7795 are handled using copy relocs or PLT stubs, so there's
7796 no need to add a .rela.dyn entry for this relocation. */
861fb55a 7797 if (can_make_dynamic_p)
b49e97c9
TS
7798 {
7799 if (sreloc == NULL)
7800 {
0a44bf69 7801 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 7802 if (sreloc == NULL)
f4416af6 7803 return FALSE;
b49e97c9 7804 }
9a59ad6b 7805 if (info->shared && h == NULL)
82f0cfbd
EC
7806 {
7807 /* When creating a shared object, we must copy these
7808 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
7809 relocs. Make room for this reloc in .rel(a).dyn. */
7810 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 7811 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7812 /* We tell the dynamic linker that there are
7813 relocations against the text segment. */
7814 info->flags |= DF_TEXTREL;
7815 }
b49e97c9
TS
7816 else
7817 {
7818 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 7819
9a59ad6b
DJ
7820 /* For a shared object, we must copy this relocation
7821 unless the symbol turns out to be undefined and
7822 weak with non-default visibility, in which case
7823 it will be left as zero.
7824
7825 We could elide R_MIPS_REL32 for locally binding symbols
7826 in shared libraries, but do not yet do so.
7827
7828 For an executable, we only need to copy this
7829 reloc if the symbol is defined in a dynamic
7830 object. */
b49e97c9
TS
7831 hmips = (struct mips_elf_link_hash_entry *) h;
7832 ++hmips->possibly_dynamic_relocs;
943284cc 7833 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7834 /* We need it to tell the dynamic linker if there
7835 are relocations against the text segment. */
7836 hmips->readonly_reloc = TRUE;
b49e97c9 7837 }
b49e97c9
TS
7838 }
7839
7840 if (SGI_COMPAT (abfd))
7841 mips_elf_hash_table (info)->compact_rel_size +=
7842 sizeof (Elf32_External_crinfo);
7843 break;
7844
7845 case R_MIPS_26:
7846 case R_MIPS_GPREL16:
7847 case R_MIPS_LITERAL:
7848 case R_MIPS_GPREL32:
7849 if (SGI_COMPAT (abfd))
7850 mips_elf_hash_table (info)->compact_rel_size +=
7851 sizeof (Elf32_External_crinfo);
7852 break;
7853
7854 /* This relocation describes the C++ object vtable hierarchy.
7855 Reconstruct it for later use during GC. */
7856 case R_MIPS_GNU_VTINHERIT:
c152c796 7857 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 7858 return FALSE;
b49e97c9
TS
7859 break;
7860
7861 /* This relocation describes which C++ vtable entries are actually
7862 used. Record for later use during GC. */
7863 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
7864 BFD_ASSERT (h != NULL);
7865 if (h != NULL
7866 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 7867 return FALSE;
b49e97c9
TS
7868 break;
7869
7870 default:
7871 break;
7872 }
7873
7874 /* We must not create a stub for a symbol that has relocations
0a44bf69
RS
7875 related to taking the function's address. This doesn't apply to
7876 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7877 a normal .got entry. */
7878 if (!htab->is_vxworks && h != NULL)
7879 switch (r_type)
7880 {
7881 default:
7882 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7883 break;
738e5348 7884 case R_MIPS16_CALL16:
0a44bf69
RS
7885 case R_MIPS_CALL16:
7886 case R_MIPS_CALL_HI16:
7887 case R_MIPS_CALL_LO16:
7888 case R_MIPS_JALR:
7889 break;
7890 }
b49e97c9 7891
738e5348
RS
7892 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7893 if there is one. We only need to handle global symbols here;
7894 we decide whether to keep or delete stubs for local symbols
7895 when processing the stub's relocations. */
b49e97c9 7896 if (h != NULL
738e5348
RS
7897 && !mips16_call_reloc_p (r_type)
7898 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
7899 {
7900 struct mips_elf_link_hash_entry *mh;
7901
7902 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 7903 mh->need_fn_stub = TRUE;
b49e97c9 7904 }
861fb55a
DJ
7905
7906 /* Refuse some position-dependent relocations when creating a
7907 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7908 not PIC, but we can create dynamic relocations and the result
7909 will be fine. Also do not refuse R_MIPS_LO16, which can be
7910 combined with R_MIPS_GOT16. */
7911 if (info->shared)
7912 {
7913 switch (r_type)
7914 {
7915 case R_MIPS16_HI16:
7916 case R_MIPS_HI16:
7917 case R_MIPS_HIGHER:
7918 case R_MIPS_HIGHEST:
7919 /* Don't refuse a high part relocation if it's against
7920 no symbol (e.g. part of a compound relocation). */
7921 if (r_symndx == 0)
7922 break;
7923
7924 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7925 and has a special meaning. */
7926 if (!NEWABI_P (abfd) && h != NULL
7927 && strcmp (h->root.root.string, "_gp_disp") == 0)
7928 break;
7929
0fc1eb3c
RS
7930 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
7931 if (is_gott_symbol (info, h))
7932 break;
7933
861fb55a
DJ
7934 /* FALLTHROUGH */
7935
7936 case R_MIPS16_26:
7937 case R_MIPS_26:
7938 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7939 (*_bfd_error_handler)
7940 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7941 abfd, howto->name,
7942 (h) ? h->root.root.string : "a local symbol");
7943 bfd_set_error (bfd_error_bad_value);
7944 return FALSE;
7945 default:
7946 break;
7947 }
7948 }
b49e97c9
TS
7949 }
7950
b34976b6 7951 return TRUE;
b49e97c9
TS
7952}
7953\f
d0647110 7954bfd_boolean
9719ad41
RS
7955_bfd_mips_relax_section (bfd *abfd, asection *sec,
7956 struct bfd_link_info *link_info,
7957 bfd_boolean *again)
d0647110
AO
7958{
7959 Elf_Internal_Rela *internal_relocs;
7960 Elf_Internal_Rela *irel, *irelend;
7961 Elf_Internal_Shdr *symtab_hdr;
7962 bfd_byte *contents = NULL;
d0647110
AO
7963 size_t extsymoff;
7964 bfd_boolean changed_contents = FALSE;
7965 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7966 Elf_Internal_Sym *isymbuf = NULL;
7967
7968 /* We are not currently changing any sizes, so only one pass. */
7969 *again = FALSE;
7970
1049f94e 7971 if (link_info->relocatable)
d0647110
AO
7972 return TRUE;
7973
9719ad41 7974 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 7975 link_info->keep_memory);
d0647110
AO
7976 if (internal_relocs == NULL)
7977 return TRUE;
7978
7979 irelend = internal_relocs + sec->reloc_count
7980 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7981 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7982 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7983
7984 for (irel = internal_relocs; irel < irelend; irel++)
7985 {
7986 bfd_vma symval;
7987 bfd_signed_vma sym_offset;
7988 unsigned int r_type;
7989 unsigned long r_symndx;
7990 asection *sym_sec;
7991 unsigned long instruction;
7992
7993 /* Turn jalr into bgezal, and jr into beq, if they're marked
7994 with a JALR relocation, that indicate where they jump to.
7995 This saves some pipeline bubbles. */
7996 r_type = ELF_R_TYPE (abfd, irel->r_info);
7997 if (r_type != R_MIPS_JALR)
7998 continue;
7999
8000 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8001 /* Compute the address of the jump target. */
8002 if (r_symndx >= extsymoff)
8003 {
8004 struct mips_elf_link_hash_entry *h
8005 = ((struct mips_elf_link_hash_entry *)
8006 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8007
8008 while (h->root.root.type == bfd_link_hash_indirect
8009 || h->root.root.type == bfd_link_hash_warning)
8010 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 8011
d0647110
AO
8012 /* If a symbol is undefined, or if it may be overridden,
8013 skip it. */
8014 if (! ((h->root.root.type == bfd_link_hash_defined
8015 || h->root.root.type == bfd_link_hash_defweak)
8016 && h->root.root.u.def.section)
8017 || (link_info->shared && ! link_info->symbolic
f5385ebf 8018 && !h->root.forced_local))
d0647110
AO
8019 continue;
8020
8021 sym_sec = h->root.root.u.def.section;
8022 if (sym_sec->output_section)
8023 symval = (h->root.root.u.def.value
8024 + sym_sec->output_section->vma
8025 + sym_sec->output_offset);
8026 else
8027 symval = h->root.root.u.def.value;
8028 }
8029 else
8030 {
8031 Elf_Internal_Sym *isym;
8032
8033 /* Read this BFD's symbols if we haven't done so already. */
8034 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8035 {
8036 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8037 if (isymbuf == NULL)
8038 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8039 symtab_hdr->sh_info, 0,
8040 NULL, NULL, NULL);
8041 if (isymbuf == NULL)
8042 goto relax_return;
8043 }
8044
8045 isym = isymbuf + r_symndx;
8046 if (isym->st_shndx == SHN_UNDEF)
8047 continue;
8048 else if (isym->st_shndx == SHN_ABS)
8049 sym_sec = bfd_abs_section_ptr;
8050 else if (isym->st_shndx == SHN_COMMON)
8051 sym_sec = bfd_com_section_ptr;
8052 else
8053 sym_sec
8054 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8055 symval = isym->st_value
8056 + sym_sec->output_section->vma
8057 + sym_sec->output_offset;
8058 }
8059
8060 /* Compute branch offset, from delay slot of the jump to the
8061 branch target. */
8062 sym_offset = (symval + irel->r_addend)
8063 - (sec_start + irel->r_offset + 4);
8064
8065 /* Branch offset must be properly aligned. */
8066 if ((sym_offset & 3) != 0)
8067 continue;
8068
8069 sym_offset >>= 2;
8070
8071 /* Check that it's in range. */
8072 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8073 continue;
143d77c5 8074
d0647110 8075 /* Get the section contents if we haven't done so already. */
c224138d
RS
8076 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8077 goto relax_return;
d0647110
AO
8078
8079 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8080
8081 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8082 if ((instruction & 0xfc1fffff) == 0x0000f809)
8083 instruction = 0x04110000;
8084 /* If it was jr <reg>, turn it into b <target>. */
8085 else if ((instruction & 0xfc1fffff) == 0x00000008)
8086 instruction = 0x10000000;
8087 else
8088 continue;
8089
8090 instruction |= (sym_offset & 0xffff);
8091 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8092 changed_contents = TRUE;
8093 }
8094
8095 if (contents != NULL
8096 && elf_section_data (sec)->this_hdr.contents != contents)
8097 {
8098 if (!changed_contents && !link_info->keep_memory)
8099 free (contents);
8100 else
8101 {
8102 /* Cache the section contents for elf_link_input_bfd. */
8103 elf_section_data (sec)->this_hdr.contents = contents;
8104 }
8105 }
8106 return TRUE;
8107
143d77c5 8108 relax_return:
eea6121a
AM
8109 if (contents != NULL
8110 && elf_section_data (sec)->this_hdr.contents != contents)
8111 free (contents);
d0647110
AO
8112 return FALSE;
8113}
8114\f
9a59ad6b
DJ
8115/* Allocate space for global sym dynamic relocs. */
8116
8117static bfd_boolean
8118allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8119{
8120 struct bfd_link_info *info = inf;
8121 bfd *dynobj;
8122 struct mips_elf_link_hash_entry *hmips;
8123 struct mips_elf_link_hash_table *htab;
8124
8125 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8126 BFD_ASSERT (htab != NULL);
8127
9a59ad6b
DJ
8128 dynobj = elf_hash_table (info)->dynobj;
8129 hmips = (struct mips_elf_link_hash_entry *) h;
8130
8131 /* VxWorks executables are handled elsewhere; we only need to
8132 allocate relocations in shared objects. */
8133 if (htab->is_vxworks && !info->shared)
8134 return TRUE;
8135
63897e2c
RS
8136 /* Ignore indirect and warning symbols. All relocations against
8137 such symbols will be redirected to the target symbol. */
8138 if (h->root.type == bfd_link_hash_indirect
8139 || h->root.type == bfd_link_hash_warning)
8140 return TRUE;
8141
9a59ad6b
DJ
8142 /* If this symbol is defined in a dynamic object, or we are creating
8143 a shared library, we will need to copy any R_MIPS_32 or
8144 R_MIPS_REL32 relocs against it into the output file. */
8145 if (! info->relocatable
8146 && hmips->possibly_dynamic_relocs != 0
8147 && (h->root.type == bfd_link_hash_defweak
8148 || !h->def_regular
8149 || info->shared))
8150 {
8151 bfd_boolean do_copy = TRUE;
8152
8153 if (h->root.type == bfd_link_hash_undefweak)
8154 {
8155 /* Do not copy relocations for undefined weak symbols with
8156 non-default visibility. */
8157 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8158 do_copy = FALSE;
8159
8160 /* Make sure undefined weak symbols are output as a dynamic
8161 symbol in PIEs. */
8162 else if (h->dynindx == -1 && !h->forced_local)
8163 {
8164 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8165 return FALSE;
8166 }
8167 }
8168
8169 if (do_copy)
8170 {
aff469fa 8171 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
8172 the SVR4 psABI requires it to have a dynamic symbol table
8173 index greater that DT_MIPS_GOTSYM if there are dynamic
8174 relocations against it.
8175
8176 VxWorks does not enforce the same mapping between the GOT
8177 and the symbol table, so the same requirement does not
8178 apply there. */
6ccf4795
RS
8179 if (!htab->is_vxworks)
8180 {
8181 if (hmips->global_got_area > GGA_RELOC_ONLY)
8182 hmips->global_got_area = GGA_RELOC_ONLY;
8183 hmips->got_only_for_calls = FALSE;
8184 }
aff469fa 8185
9a59ad6b
DJ
8186 mips_elf_allocate_dynamic_relocations
8187 (dynobj, info, hmips->possibly_dynamic_relocs);
8188 if (hmips->readonly_reloc)
8189 /* We tell the dynamic linker that there are relocations
8190 against the text segment. */
8191 info->flags |= DF_TEXTREL;
8192 }
8193 }
8194
8195 return TRUE;
8196}
8197
b49e97c9
TS
8198/* Adjust a symbol defined by a dynamic object and referenced by a
8199 regular object. The current definition is in some section of the
8200 dynamic object, but we're not including those sections. We have to
8201 change the definition to something the rest of the link can
8202 understand. */
8203
b34976b6 8204bfd_boolean
9719ad41
RS
8205_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8206 struct elf_link_hash_entry *h)
b49e97c9
TS
8207{
8208 bfd *dynobj;
8209 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8210 struct mips_elf_link_hash_table *htab;
b49e97c9 8211
5108fc1b 8212 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8213 BFD_ASSERT (htab != NULL);
8214
b49e97c9 8215 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8216 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8217
8218 /* Make sure we know what is going on here. */
8219 BFD_ASSERT (dynobj != NULL
f5385ebf 8220 && (h->needs_plt
f6e332e6 8221 || h->u.weakdef != NULL
f5385ebf
AM
8222 || (h->def_dynamic
8223 && h->ref_regular
8224 && !h->def_regular)));
b49e97c9 8225
b49e97c9 8226 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8227
861fb55a
DJ
8228 /* If there are call relocations against an externally-defined symbol,
8229 see whether we can create a MIPS lazy-binding stub for it. We can
8230 only do this if all references to the function are through call
8231 relocations, and in that case, the traditional lazy-binding stubs
8232 are much more efficient than PLT entries.
8233
8234 Traditional stubs are only available on SVR4 psABI-based systems;
8235 VxWorks always uses PLTs instead. */
8236 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8237 {
8238 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8239 return TRUE;
b49e97c9
TS
8240
8241 /* If this symbol is not defined in a regular file, then set
8242 the symbol to the stub location. This is required to make
8243 function pointers compare as equal between the normal
8244 executable and the shared library. */
f5385ebf 8245 if (!h->def_regular)
b49e97c9 8246 {
33bb52fb
RS
8247 hmips->needs_lazy_stub = TRUE;
8248 htab->lazy_stub_count++;
b34976b6 8249 return TRUE;
b49e97c9
TS
8250 }
8251 }
861fb55a
DJ
8252 /* As above, VxWorks requires PLT entries for externally-defined
8253 functions that are only accessed through call relocations.
b49e97c9 8254
861fb55a
DJ
8255 Both VxWorks and non-VxWorks targets also need PLT entries if there
8256 are static-only relocations against an externally-defined function.
8257 This can technically occur for shared libraries if there are
8258 branches to the symbol, although it is unlikely that this will be
8259 used in practice due to the short ranges involved. It can occur
8260 for any relative or absolute relocation in executables; in that
8261 case, the PLT entry becomes the function's canonical address. */
8262 else if (((h->needs_plt && !hmips->no_fn_stub)
8263 || (h->type == STT_FUNC && hmips->has_static_relocs))
8264 && htab->use_plts_and_copy_relocs
8265 && !SYMBOL_CALLS_LOCAL (info, h)
8266 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8267 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8268 {
861fb55a
DJ
8269 /* If this is the first symbol to need a PLT entry, allocate room
8270 for the header. */
8271 if (htab->splt->size == 0)
8272 {
8273 BFD_ASSERT (htab->sgotplt->size == 0);
0a44bf69 8274
861fb55a
DJ
8275 /* If we're using the PLT additions to the psABI, each PLT
8276 entry is 16 bytes and the PLT0 entry is 32 bytes.
8277 Encourage better cache usage by aligning. We do this
8278 lazily to avoid pessimizing traditional objects. */
8279 if (!htab->is_vxworks
8280 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8281 return FALSE;
0a44bf69 8282
861fb55a
DJ
8283 /* Make sure that .got.plt is word-aligned. We do this lazily
8284 for the same reason as above. */
8285 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8286 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8287 return FALSE;
0a44bf69 8288
861fb55a 8289 htab->splt->size += htab->plt_header_size;
0a44bf69 8290
861fb55a
DJ
8291 /* On non-VxWorks targets, the first two entries in .got.plt
8292 are reserved. */
8293 if (!htab->is_vxworks)
8294 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
0a44bf69 8295
861fb55a
DJ
8296 /* On VxWorks, also allocate room for the header's
8297 .rela.plt.unloaded entries. */
8298 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8299 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8300 }
8301
8302 /* Assign the next .plt entry to this symbol. */
8303 h->plt.offset = htab->splt->size;
8304 htab->splt->size += htab->plt_entry_size;
8305
8306 /* If the output file has no definition of the symbol, set the
861fb55a 8307 symbol's value to the address of the stub. */
131eb6b7 8308 if (!info->shared && !h->def_regular)
0a44bf69
RS
8309 {
8310 h->root.u.def.section = htab->splt;
8311 h->root.u.def.value = h->plt.offset;
861fb55a
DJ
8312 /* For VxWorks, point at the PLT load stub rather than the
8313 lazy resolution stub; this stub will become the canonical
8314 function address. */
8315 if (htab->is_vxworks)
8316 h->root.u.def.value += 8;
0a44bf69
RS
8317 }
8318
861fb55a
DJ
8319 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8320 relocation. */
8321 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8322 htab->srelplt->size += (htab->is_vxworks
8323 ? MIPS_ELF_RELA_SIZE (dynobj)
8324 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8325
8326 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8327 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8328 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8329
861fb55a
DJ
8330 /* All relocations against this symbol that could have been made
8331 dynamic will now refer to the PLT entry instead. */
8332 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8333
0a44bf69
RS
8334 return TRUE;
8335 }
8336
8337 /* If this is a weak symbol, and there is a real definition, the
8338 processor independent code will have arranged for us to see the
8339 real definition first, and we can just use the same value. */
8340 if (h->u.weakdef != NULL)
8341 {
8342 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8343 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8344 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8345 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8346 return TRUE;
8347 }
8348
861fb55a
DJ
8349 /* Otherwise, there is nothing further to do for symbols defined
8350 in regular objects. */
8351 if (h->def_regular)
0a44bf69
RS
8352 return TRUE;
8353
861fb55a
DJ
8354 /* There's also nothing more to do if we'll convert all relocations
8355 against this symbol into dynamic relocations. */
8356 if (!hmips->has_static_relocs)
8357 return TRUE;
8358
8359 /* We're now relying on copy relocations. Complain if we have
8360 some that we can't convert. */
8361 if (!htab->use_plts_and_copy_relocs || info->shared)
8362 {
8363 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8364 "dynamic symbol %s"),
8365 h->root.root.string);
8366 bfd_set_error (bfd_error_bad_value);
8367 return FALSE;
8368 }
8369
0a44bf69
RS
8370 /* We must allocate the symbol in our .dynbss section, which will
8371 become part of the .bss section of the executable. There will be
8372 an entry for this symbol in the .dynsym section. The dynamic
8373 object will contain position independent code, so all references
8374 from the dynamic object to this symbol will go through the global
8375 offset table. The dynamic linker will use the .dynsym entry to
8376 determine the address it must put in the global offset table, so
8377 both the dynamic object and the regular object will refer to the
8378 same memory location for the variable. */
8379
8380 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8381 {
861fb55a
DJ
8382 if (htab->is_vxworks)
8383 htab->srelbss->size += sizeof (Elf32_External_Rela);
8384 else
8385 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
8386 h->needs_copy = 1;
8387 }
8388
861fb55a
DJ
8389 /* All relocations against this symbol that could have been made
8390 dynamic will now refer to the local copy instead. */
8391 hmips->possibly_dynamic_relocs = 0;
8392
027297b7 8393 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 8394}
b49e97c9
TS
8395\f
8396/* This function is called after all the input files have been read,
8397 and the input sections have been assigned to output sections. We
8398 check for any mips16 stub sections that we can discard. */
8399
b34976b6 8400bfd_boolean
9719ad41
RS
8401_bfd_mips_elf_always_size_sections (bfd *output_bfd,
8402 struct bfd_link_info *info)
b49e97c9
TS
8403{
8404 asection *ri;
0a44bf69 8405 struct mips_elf_link_hash_table *htab;
861fb55a 8406 struct mips_htab_traverse_info hti;
0a44bf69
RS
8407
8408 htab = mips_elf_hash_table (info);
4dfe6ac6 8409 BFD_ASSERT (htab != NULL);
f4416af6 8410
b49e97c9
TS
8411 /* The .reginfo section has a fixed size. */
8412 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8413 if (ri != NULL)
9719ad41 8414 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 8415
861fb55a
DJ
8416 hti.info = info;
8417 hti.output_bfd = output_bfd;
8418 hti.error = FALSE;
8419 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8420 mips_elf_check_symbols, &hti);
8421 if (hti.error)
8422 return FALSE;
f4416af6 8423
33bb52fb
RS
8424 return TRUE;
8425}
8426
8427/* If the link uses a GOT, lay it out and work out its size. */
8428
8429static bfd_boolean
8430mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8431{
8432 bfd *dynobj;
8433 asection *s;
8434 struct mips_got_info *g;
33bb52fb
RS
8435 bfd_size_type loadable_size = 0;
8436 bfd_size_type page_gotno;
8437 bfd *sub;
8438 struct mips_elf_count_tls_arg count_tls_arg;
8439 struct mips_elf_link_hash_table *htab;
8440
8441 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8442 BFD_ASSERT (htab != NULL);
8443
a8028dd0 8444 s = htab->sgot;
f4416af6 8445 if (s == NULL)
b34976b6 8446 return TRUE;
b49e97c9 8447
33bb52fb 8448 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
8449 g = htab->got_info;
8450
861fb55a
DJ
8451 /* Allocate room for the reserved entries. VxWorks always reserves
8452 3 entries; other objects only reserve 2 entries. */
8453 BFD_ASSERT (g->assigned_gotno == 0);
8454 if (htab->is_vxworks)
8455 htab->reserved_gotno = 3;
8456 else
8457 htab->reserved_gotno = 2;
8458 g->local_gotno += htab->reserved_gotno;
8459 g->assigned_gotno = htab->reserved_gotno;
8460
33bb52fb
RS
8461 /* Replace entries for indirect and warning symbols with entries for
8462 the target symbol. */
8463 if (!mips_elf_resolve_final_got_entries (g))
8464 return FALSE;
f4416af6 8465
d4596a51 8466 /* Count the number of GOT symbols. */
020d7251 8467 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 8468
33bb52fb
RS
8469 /* Calculate the total loadable size of the output. That
8470 will give us the maximum number of GOT_PAGE entries
8471 required. */
8472 for (sub = info->input_bfds; sub; sub = sub->link_next)
8473 {
8474 asection *subsection;
5108fc1b 8475
33bb52fb
RS
8476 for (subsection = sub->sections;
8477 subsection;
8478 subsection = subsection->next)
8479 {
8480 if ((subsection->flags & SEC_ALLOC) == 0)
8481 continue;
8482 loadable_size += ((subsection->size + 0xf)
8483 &~ (bfd_size_type) 0xf);
8484 }
8485 }
f4416af6 8486
0a44bf69 8487 if (htab->is_vxworks)
738e5348 8488 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
8489 relocations against local symbols evaluate to "G", and the EABI does
8490 not include R_MIPS_GOT_PAGE. */
c224138d 8491 page_gotno = 0;
0a44bf69
RS
8492 else
8493 /* Assume there are two loadable segments consisting of contiguous
8494 sections. Is 5 enough? */
c224138d
RS
8495 page_gotno = (loadable_size >> 16) + 5;
8496
8497 /* Choose the smaller of the two estimates; both are intended to be
8498 conservative. */
8499 if (page_gotno > g->page_gotno)
8500 page_gotno = g->page_gotno;
f4416af6 8501
c224138d 8502 g->local_gotno += page_gotno;
eea6121a 8503 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
d4596a51 8504 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6 8505
0f20cc35
DJ
8506 /* We need to calculate tls_gotno for global symbols at this point
8507 instead of building it up earlier, to avoid doublecounting
8508 entries for one global symbol from multiple input files. */
8509 count_tls_arg.info = info;
8510 count_tls_arg.needed = 0;
8511 elf_link_hash_traverse (elf_hash_table (info),
8512 mips_elf_count_global_tls_entries,
8513 &count_tls_arg);
8514 g->tls_gotno += count_tls_arg.needed;
8515 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8516
0a44bf69
RS
8517 /* VxWorks does not support multiple GOTs. It initializes $gp to
8518 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8519 dynamic loader. */
33bb52fb
RS
8520 if (htab->is_vxworks)
8521 {
8522 /* VxWorks executables do not need a GOT. */
8523 if (info->shared)
8524 {
8525 /* Each VxWorks GOT entry needs an explicit relocation. */
8526 unsigned int count;
8527
861fb55a 8528 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
33bb52fb
RS
8529 if (count)
8530 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8531 }
8532 }
8533 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 8534 {
a8028dd0 8535 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
8536 return FALSE;
8537 }
8538 else
8539 {
33bb52fb
RS
8540 struct mips_elf_count_tls_arg arg;
8541
8542 /* Set up TLS entries. */
0f20cc35
DJ
8543 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8544 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
33bb52fb
RS
8545
8546 /* Allocate room for the TLS relocations. */
8547 arg.info = info;
8548 arg.needed = 0;
8549 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8550 elf_link_hash_traverse (elf_hash_table (info),
8551 mips_elf_count_global_tls_relocs,
8552 &arg);
8553 if (arg.needed)
8554 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
0f20cc35 8555 }
b49e97c9 8556
b34976b6 8557 return TRUE;
b49e97c9
TS
8558}
8559
33bb52fb
RS
8560/* Estimate the size of the .MIPS.stubs section. */
8561
8562static void
8563mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8564{
8565 struct mips_elf_link_hash_table *htab;
8566 bfd_size_type dynsymcount;
8567
8568 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8569 BFD_ASSERT (htab != NULL);
8570
33bb52fb
RS
8571 if (htab->lazy_stub_count == 0)
8572 return;
8573
8574 /* IRIX rld assumes that a function stub isn't at the end of the .text
8575 section, so add a dummy entry to the end. */
8576 htab->lazy_stub_count++;
8577
8578 /* Get a worst-case estimate of the number of dynamic symbols needed.
8579 At this point, dynsymcount does not account for section symbols
8580 and count_section_dynsyms may overestimate the number that will
8581 be needed. */
8582 dynsymcount = (elf_hash_table (info)->dynsymcount
8583 + count_section_dynsyms (output_bfd, info));
8584
8585 /* Determine the size of one stub entry. */
8586 htab->function_stub_size = (dynsymcount > 0x10000
8587 ? MIPS_FUNCTION_STUB_BIG_SIZE
8588 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8589
8590 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8591}
8592
8593/* A mips_elf_link_hash_traverse callback for which DATA points to the
8594 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8595 allocate an entry in the stubs section. */
8596
8597static bfd_boolean
8598mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8599{
8600 struct mips_elf_link_hash_table *htab;
8601
8602 htab = (struct mips_elf_link_hash_table *) data;
8603 if (h->needs_lazy_stub)
8604 {
8605 h->root.root.u.def.section = htab->sstubs;
8606 h->root.root.u.def.value = htab->sstubs->size;
8607 h->root.plt.offset = htab->sstubs->size;
8608 htab->sstubs->size += htab->function_stub_size;
8609 }
8610 return TRUE;
8611}
8612
8613/* Allocate offsets in the stubs section to each symbol that needs one.
8614 Set the final size of the .MIPS.stub section. */
8615
8616static void
8617mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8618{
8619 struct mips_elf_link_hash_table *htab;
8620
8621 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8622 BFD_ASSERT (htab != NULL);
8623
33bb52fb
RS
8624 if (htab->lazy_stub_count == 0)
8625 return;
8626
8627 htab->sstubs->size = 0;
4dfe6ac6 8628 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
33bb52fb
RS
8629 htab->sstubs->size += htab->function_stub_size;
8630 BFD_ASSERT (htab->sstubs->size
8631 == htab->lazy_stub_count * htab->function_stub_size);
8632}
8633
b49e97c9
TS
8634/* Set the sizes of the dynamic sections. */
8635
b34976b6 8636bfd_boolean
9719ad41
RS
8637_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8638 struct bfd_link_info *info)
b49e97c9
TS
8639{
8640 bfd *dynobj;
861fb55a 8641 asection *s, *sreldyn;
b34976b6 8642 bfd_boolean reltext;
0a44bf69 8643 struct mips_elf_link_hash_table *htab;
b49e97c9 8644
0a44bf69 8645 htab = mips_elf_hash_table (info);
4dfe6ac6 8646 BFD_ASSERT (htab != NULL);
b49e97c9
TS
8647 dynobj = elf_hash_table (info)->dynobj;
8648 BFD_ASSERT (dynobj != NULL);
8649
8650 if (elf_hash_table (info)->dynamic_sections_created)
8651 {
8652 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 8653 if (info->executable)
b49e97c9
TS
8654 {
8655 s = bfd_get_section_by_name (dynobj, ".interp");
8656 BFD_ASSERT (s != NULL);
eea6121a 8657 s->size
b49e97c9
TS
8658 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8659 s->contents
8660 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8661 }
861fb55a
DJ
8662
8663 /* Create a symbol for the PLT, if we know that we are using it. */
8664 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8665 {
8666 struct elf_link_hash_entry *h;
8667
8668 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8669
8670 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8671 "_PROCEDURE_LINKAGE_TABLE_");
8672 htab->root.hplt = h;
8673 if (h == NULL)
8674 return FALSE;
8675 h->type = STT_FUNC;
8676 }
8677 }
4e41d0d7 8678
9a59ad6b
DJ
8679 /* Allocate space for global sym dynamic relocs. */
8680 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8681
33bb52fb
RS
8682 mips_elf_estimate_stub_size (output_bfd, info);
8683
8684 if (!mips_elf_lay_out_got (output_bfd, info))
8685 return FALSE;
8686
8687 mips_elf_lay_out_lazy_stubs (info);
8688
b49e97c9
TS
8689 /* The check_relocs and adjust_dynamic_symbol entry points have
8690 determined the sizes of the various dynamic sections. Allocate
8691 memory for them. */
b34976b6 8692 reltext = FALSE;
b49e97c9
TS
8693 for (s = dynobj->sections; s != NULL; s = s->next)
8694 {
8695 const char *name;
b49e97c9
TS
8696
8697 /* It's OK to base decisions on the section name, because none
8698 of the dynobj section names depend upon the input files. */
8699 name = bfd_get_section_name (dynobj, s);
8700
8701 if ((s->flags & SEC_LINKER_CREATED) == 0)
8702 continue;
8703
0112cd26 8704 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 8705 {
c456f082 8706 if (s->size != 0)
b49e97c9
TS
8707 {
8708 const char *outname;
8709 asection *target;
8710
8711 /* If this relocation section applies to a read only
8712 section, then we probably need a DT_TEXTREL entry.
0a44bf69 8713 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
8714 assert a DT_TEXTREL entry rather than testing whether
8715 there exists a relocation to a read only section or
8716 not. */
8717 outname = bfd_get_section_name (output_bfd,
8718 s->output_section);
8719 target = bfd_get_section_by_name (output_bfd, outname + 4);
8720 if ((target != NULL
8721 && (target->flags & SEC_READONLY) != 0
8722 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 8723 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 8724 reltext = TRUE;
b49e97c9
TS
8725
8726 /* We use the reloc_count field as a counter if we need
8727 to copy relocs into the output file. */
0a44bf69 8728 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 8729 s->reloc_count = 0;
f4416af6
AO
8730
8731 /* If combreloc is enabled, elf_link_sort_relocs() will
8732 sort relocations, but in a different way than we do,
8733 and before we're done creating relocations. Also, it
8734 will move them around between input sections'
8735 relocation's contents, so our sorting would be
8736 broken, so don't let it run. */
8737 info->combreloc = 0;
b49e97c9
TS
8738 }
8739 }
b49e97c9
TS
8740 else if (! info->shared
8741 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 8742 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 8743 {
5108fc1b 8744 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 8745 rtld to contain a pointer to the _r_debug structure. */
eea6121a 8746 s->size += 4;
b49e97c9
TS
8747 }
8748 else if (SGI_COMPAT (output_bfd)
0112cd26 8749 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 8750 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
8751 else if (s == htab->splt)
8752 {
8753 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
8754 room for an extra nop to fill the delay slot. This is
8755 for CPUs without load interlocking. */
8756 if (! LOAD_INTERLOCKS_P (output_bfd)
8757 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
8758 s->size += 4;
8759 }
0112cd26 8760 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 8761 && s != htab->sgot
0a44bf69 8762 && s != htab->sgotplt
861fb55a
DJ
8763 && s != htab->sstubs
8764 && s != htab->sdynbss)
b49e97c9
TS
8765 {
8766 /* It's not one of our sections, so don't allocate space. */
8767 continue;
8768 }
8769
c456f082 8770 if (s->size == 0)
b49e97c9 8771 {
8423293d 8772 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
8773 continue;
8774 }
8775
c456f082
AM
8776 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8777 continue;
8778
b49e97c9 8779 /* Allocate memory for the section contents. */
eea6121a 8780 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 8781 if (s->contents == NULL)
b49e97c9
TS
8782 {
8783 bfd_set_error (bfd_error_no_memory);
b34976b6 8784 return FALSE;
b49e97c9
TS
8785 }
8786 }
8787
8788 if (elf_hash_table (info)->dynamic_sections_created)
8789 {
8790 /* Add some entries to the .dynamic section. We fill in the
8791 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8792 must add the entries now so that we get the correct size for
5750dcec 8793 the .dynamic section. */
af5978fb
RS
8794
8795 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec
DJ
8796 DT_MIPS_RLD_MAP entry. This must come first because glibc
8797 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8798 looks at the first one it sees. */
af5978fb
RS
8799 if (!info->shared
8800 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8801 return FALSE;
b49e97c9 8802
5750dcec
DJ
8803 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8804 used by the debugger. */
8805 if (info->executable
8806 && !SGI_COMPAT (output_bfd)
8807 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8808 return FALSE;
8809
0a44bf69 8810 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
8811 info->flags |= DF_TEXTREL;
8812
8813 if ((info->flags & DF_TEXTREL) != 0)
8814 {
8815 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 8816 return FALSE;
943284cc
DJ
8817
8818 /* Clear the DF_TEXTREL flag. It will be set again if we
8819 write out an actual text relocation; we may not, because
8820 at this point we do not know whether e.g. any .eh_frame
8821 absolute relocations have been converted to PC-relative. */
8822 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
8823 }
8824
8825 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 8826 return FALSE;
b49e97c9 8827
861fb55a 8828 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 8829 if (htab->is_vxworks)
b49e97c9 8830 {
0a44bf69
RS
8831 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8832 use any of the DT_MIPS_* tags. */
861fb55a 8833 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8834 {
8835 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8836 return FALSE;
b49e97c9 8837
0a44bf69
RS
8838 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8839 return FALSE;
b49e97c9 8840
0a44bf69
RS
8841 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8842 return FALSE;
8843 }
b49e97c9 8844 }
0a44bf69
RS
8845 else
8846 {
861fb55a 8847 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8848 {
8849 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8850 return FALSE;
b49e97c9 8851
0a44bf69
RS
8852 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8853 return FALSE;
b49e97c9 8854
0a44bf69
RS
8855 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8856 return FALSE;
8857 }
b49e97c9 8858
0a44bf69
RS
8859 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8860 return FALSE;
b49e97c9 8861
0a44bf69
RS
8862 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8863 return FALSE;
b49e97c9 8864
0a44bf69
RS
8865 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8866 return FALSE;
b49e97c9 8867
0a44bf69
RS
8868 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8869 return FALSE;
b49e97c9 8870
0a44bf69
RS
8871 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8872 return FALSE;
b49e97c9 8873
0a44bf69
RS
8874 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8875 return FALSE;
b49e97c9 8876
0a44bf69
RS
8877 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8878 return FALSE;
8879
8880 if (IRIX_COMPAT (dynobj) == ict_irix5
8881 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8882 return FALSE;
8883
8884 if (IRIX_COMPAT (dynobj) == ict_irix6
8885 && (bfd_get_section_by_name
8886 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8887 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8888 return FALSE;
8889 }
861fb55a
DJ
8890 if (htab->splt->size > 0)
8891 {
8892 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8893 return FALSE;
8894
8895 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8896 return FALSE;
8897
8898 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8899 return FALSE;
8900
8901 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8902 return FALSE;
8903 }
7a2b07ff
NS
8904 if (htab->is_vxworks
8905 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8906 return FALSE;
b49e97c9
TS
8907 }
8908
b34976b6 8909 return TRUE;
b49e97c9
TS
8910}
8911\f
81d43bff
RS
8912/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8913 Adjust its R_ADDEND field so that it is correct for the output file.
8914 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8915 and sections respectively; both use symbol indexes. */
8916
8917static void
8918mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8919 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8920 asection **local_sections, Elf_Internal_Rela *rel)
8921{
8922 unsigned int r_type, r_symndx;
8923 Elf_Internal_Sym *sym;
8924 asection *sec;
8925
020d7251 8926 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
8927 {
8928 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8929 if (r_type == R_MIPS16_GPREL
8930 || r_type == R_MIPS_GPREL16
8931 || r_type == R_MIPS_GPREL32
8932 || r_type == R_MIPS_LITERAL)
8933 {
8934 rel->r_addend += _bfd_get_gp_value (input_bfd);
8935 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8936 }
8937
8938 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8939 sym = local_syms + r_symndx;
8940
8941 /* Adjust REL's addend to account for section merging. */
8942 if (!info->relocatable)
8943 {
8944 sec = local_sections[r_symndx];
8945 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8946 }
8947
8948 /* This would normally be done by the rela_normal code in elflink.c. */
8949 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8950 rel->r_addend += local_sections[r_symndx]->output_offset;
8951 }
8952}
8953
b49e97c9
TS
8954/* Relocate a MIPS ELF section. */
8955
b34976b6 8956bfd_boolean
9719ad41
RS
8957_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8958 bfd *input_bfd, asection *input_section,
8959 bfd_byte *contents, Elf_Internal_Rela *relocs,
8960 Elf_Internal_Sym *local_syms,
8961 asection **local_sections)
b49e97c9
TS
8962{
8963 Elf_Internal_Rela *rel;
8964 const Elf_Internal_Rela *relend;
8965 bfd_vma addend = 0;
b34976b6 8966 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 8967 const struct elf_backend_data *bed;
b49e97c9
TS
8968
8969 bed = get_elf_backend_data (output_bfd);
8970 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8971 for (rel = relocs; rel < relend; ++rel)
8972 {
8973 const char *name;
c9adbffe 8974 bfd_vma value = 0;
b49e97c9 8975 reloc_howto_type *howto;
38a7df63 8976 bfd_boolean cross_mode_jump_p;
b34976b6 8977 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 8978 REL relocation. */
b34976b6 8979 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 8980 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 8981 const char *msg;
ab96bf03
AM
8982 unsigned long r_symndx;
8983 asection *sec;
749b8d9d
L
8984 Elf_Internal_Shdr *symtab_hdr;
8985 struct elf_link_hash_entry *h;
b49e97c9
TS
8986
8987 /* Find the relocation howto for this relocation. */
ab96bf03
AM
8988 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8989 NEWABI_P (input_bfd)
8990 && (MIPS_RELOC_RELA_P
8991 (input_bfd, input_section,
8992 rel - relocs)));
8993
8994 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 8995 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 8996 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
8997 {
8998 sec = local_sections[r_symndx];
8999 h = NULL;
9000 }
ab96bf03
AM
9001 else
9002 {
ab96bf03 9003 unsigned long extsymoff;
ab96bf03 9004
ab96bf03
AM
9005 extsymoff = 0;
9006 if (!elf_bad_symtab (input_bfd))
9007 extsymoff = symtab_hdr->sh_info;
9008 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9009 while (h->root.type == bfd_link_hash_indirect
9010 || h->root.type == bfd_link_hash_warning)
9011 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9012
9013 sec = NULL;
9014 if (h->root.type == bfd_link_hash_defined
9015 || h->root.type == bfd_link_hash_defweak)
9016 sec = h->root.u.def.section;
9017 }
9018
9019 if (sec != NULL && elf_discarded_section (sec))
9020 {
9021 /* For relocs against symbols from removed linkonce sections,
9022 or sections discarded by a linker script, we just want the
9023 section contents zeroed. Avoid any special processing. */
9024 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
9025 rel->r_info = 0;
9026 rel->r_addend = 0;
9027 continue;
9028 }
9029
4a14403c 9030 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
9031 {
9032 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9033 64-bit code, but make sure all their addresses are in the
9034 lowermost or uppermost 32-bit section of the 64-bit address
9035 space. Thus, when they use an R_MIPS_64 they mean what is
9036 usually meant by R_MIPS_32, with the exception that the
9037 stored value is sign-extended to 64 bits. */
b34976b6 9038 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
9039
9040 /* On big-endian systems, we need to lie about the position
9041 of the reloc. */
9042 if (bfd_big_endian (input_bfd))
9043 rel->r_offset += 4;
9044 }
b49e97c9
TS
9045
9046 if (!use_saved_addend_p)
9047 {
b49e97c9
TS
9048 /* If these relocations were originally of the REL variety,
9049 we must pull the addend out of the field that will be
9050 relocated. Otherwise, we simply use the contents of the
c224138d
RS
9051 RELA relocation. */
9052 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9053 relocs, rel))
b49e97c9 9054 {
b34976b6 9055 rela_relocation_p = FALSE;
c224138d
RS
9056 addend = mips_elf_read_rel_addend (input_bfd, rel,
9057 howto, contents);
738e5348
RS
9058 if (hi16_reloc_p (r_type)
9059 || (got16_reloc_p (r_type)
b49e97c9 9060 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 9061 local_sections)))
b49e97c9 9062 {
c224138d
RS
9063 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9064 contents, &addend))
749b8d9d 9065 {
749b8d9d
L
9066 if (h)
9067 name = h->root.root.string;
9068 else
9069 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9070 local_syms + r_symndx,
9071 sec);
9072 (*_bfd_error_handler)
9073 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9074 input_bfd, input_section, name, howto->name,
9075 rel->r_offset);
749b8d9d 9076 }
b49e97c9 9077 }
30ac9238
RS
9078 else
9079 addend <<= howto->rightshift;
b49e97c9
TS
9080 }
9081 else
9082 addend = rel->r_addend;
81d43bff
RS
9083 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9084 local_syms, local_sections, rel);
b49e97c9
TS
9085 }
9086
1049f94e 9087 if (info->relocatable)
b49e97c9 9088 {
4a14403c 9089 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9090 && bfd_big_endian (input_bfd))
9091 rel->r_offset -= 4;
9092
81d43bff 9093 if (!rela_relocation_p && rel->r_addend)
5a659663 9094 {
81d43bff 9095 addend += rel->r_addend;
738e5348 9096 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9097 addend = mips_elf_high (addend);
9098 else if (r_type == R_MIPS_HIGHER)
9099 addend = mips_elf_higher (addend);
9100 else if (r_type == R_MIPS_HIGHEST)
9101 addend = mips_elf_highest (addend);
30ac9238
RS
9102 else
9103 addend >>= howto->rightshift;
b49e97c9 9104
30ac9238
RS
9105 /* We use the source mask, rather than the destination
9106 mask because the place to which we are writing will be
9107 source of the addend in the final link. */
b49e97c9
TS
9108 addend &= howto->src_mask;
9109
5a659663 9110 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9111 /* See the comment above about using R_MIPS_64 in the 32-bit
9112 ABI. Here, we need to update the addend. It would be
9113 possible to get away with just using the R_MIPS_32 reloc
9114 but for endianness. */
9115 {
9116 bfd_vma sign_bits;
9117 bfd_vma low_bits;
9118 bfd_vma high_bits;
9119
9120 if (addend & ((bfd_vma) 1 << 31))
9121#ifdef BFD64
9122 sign_bits = ((bfd_vma) 1 << 32) - 1;
9123#else
9124 sign_bits = -1;
9125#endif
9126 else
9127 sign_bits = 0;
9128
9129 /* If we don't know that we have a 64-bit type,
9130 do two separate stores. */
9131 if (bfd_big_endian (input_bfd))
9132 {
9133 /* Store the sign-bits (which are most significant)
9134 first. */
9135 low_bits = sign_bits;
9136 high_bits = addend;
9137 }
9138 else
9139 {
9140 low_bits = addend;
9141 high_bits = sign_bits;
9142 }
9143 bfd_put_32 (input_bfd, low_bits,
9144 contents + rel->r_offset);
9145 bfd_put_32 (input_bfd, high_bits,
9146 contents + rel->r_offset + 4);
9147 continue;
9148 }
9149
9150 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9151 input_bfd, input_section,
b34976b6
AM
9152 contents, FALSE))
9153 return FALSE;
b49e97c9
TS
9154 }
9155
9156 /* Go on to the next relocation. */
9157 continue;
9158 }
9159
9160 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9161 relocations for the same offset. In that case we are
9162 supposed to treat the output of each relocation as the addend
9163 for the next. */
9164 if (rel + 1 < relend
9165 && rel->r_offset == rel[1].r_offset
9166 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9167 use_saved_addend_p = TRUE;
b49e97c9 9168 else
b34976b6 9169 use_saved_addend_p = FALSE;
b49e97c9
TS
9170
9171 /* Figure out what value we are supposed to relocate. */
9172 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9173 input_section, info, rel,
9174 addend, howto, local_syms,
9175 local_sections, &value,
38a7df63 9176 &name, &cross_mode_jump_p,
bce03d3d 9177 use_saved_addend_p))
b49e97c9
TS
9178 {
9179 case bfd_reloc_continue:
9180 /* There's nothing to do. */
9181 continue;
9182
9183 case bfd_reloc_undefined:
9184 /* mips_elf_calculate_relocation already called the
9185 undefined_symbol callback. There's no real point in
9186 trying to perform the relocation at this point, so we
9187 just skip ahead to the next relocation. */
9188 continue;
9189
9190 case bfd_reloc_notsupported:
9191 msg = _("internal error: unsupported relocation error");
9192 info->callbacks->warning
9193 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9194 return FALSE;
b49e97c9
TS
9195
9196 case bfd_reloc_overflow:
9197 if (use_saved_addend_p)
9198 /* Ignore overflow until we reach the last relocation for
9199 a given location. */
9200 ;
9201 else
9202 {
0e53d9da
AN
9203 struct mips_elf_link_hash_table *htab;
9204
9205 htab = mips_elf_hash_table (info);
4dfe6ac6 9206 BFD_ASSERT (htab != NULL);
b49e97c9 9207 BFD_ASSERT (name != NULL);
0e53d9da 9208 if (!htab->small_data_overflow_reported
9684f078 9209 && (gprel16_reloc_p (howto->type)
0e53d9da
AN
9210 || howto->type == R_MIPS_LITERAL))
9211 {
91d6fa6a
NC
9212 msg = _("small-data section exceeds 64KB;"
9213 " lower small-data size limit (see option -G)");
0e53d9da
AN
9214
9215 htab->small_data_overflow_reported = TRUE;
9216 (*info->callbacks->einfo) ("%P: %s\n", msg);
9217 }
b49e97c9 9218 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 9219 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 9220 input_bfd, input_section, rel->r_offset)))
b34976b6 9221 return FALSE;
b49e97c9
TS
9222 }
9223 break;
9224
9225 case bfd_reloc_ok:
9226 break;
9227
9228 default:
9229 abort ();
9230 break;
9231 }
9232
9233 /* If we've got another relocation for the address, keep going
9234 until we reach the last one. */
9235 if (use_saved_addend_p)
9236 {
9237 addend = value;
9238 continue;
9239 }
9240
4a14403c 9241 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9242 /* See the comment above about using R_MIPS_64 in the 32-bit
9243 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9244 that calculated the right value. Now, however, we
9245 sign-extend the 32-bit result to 64-bits, and store it as a
9246 64-bit value. We are especially generous here in that we
9247 go to extreme lengths to support this usage on systems with
9248 only a 32-bit VMA. */
9249 {
9250 bfd_vma sign_bits;
9251 bfd_vma low_bits;
9252 bfd_vma high_bits;
9253
9254 if (value & ((bfd_vma) 1 << 31))
9255#ifdef BFD64
9256 sign_bits = ((bfd_vma) 1 << 32) - 1;
9257#else
9258 sign_bits = -1;
9259#endif
9260 else
9261 sign_bits = 0;
9262
9263 /* If we don't know that we have a 64-bit type,
9264 do two separate stores. */
9265 if (bfd_big_endian (input_bfd))
9266 {
9267 /* Undo what we did above. */
9268 rel->r_offset -= 4;
9269 /* Store the sign-bits (which are most significant)
9270 first. */
9271 low_bits = sign_bits;
9272 high_bits = value;
9273 }
9274 else
9275 {
9276 low_bits = value;
9277 high_bits = sign_bits;
9278 }
9279 bfd_put_32 (input_bfd, low_bits,
9280 contents + rel->r_offset);
9281 bfd_put_32 (input_bfd, high_bits,
9282 contents + rel->r_offset + 4);
9283 continue;
9284 }
9285
9286 /* Actually perform the relocation. */
9287 if (! mips_elf_perform_relocation (info, howto, rel, value,
9288 input_bfd, input_section,
38a7df63 9289 contents, cross_mode_jump_p))
b34976b6 9290 return FALSE;
b49e97c9
TS
9291 }
9292
b34976b6 9293 return TRUE;
b49e97c9
TS
9294}
9295\f
861fb55a
DJ
9296/* A function that iterates over each entry in la25_stubs and fills
9297 in the code for each one. DATA points to a mips_htab_traverse_info. */
9298
9299static int
9300mips_elf_create_la25_stub (void **slot, void *data)
9301{
9302 struct mips_htab_traverse_info *hti;
9303 struct mips_elf_link_hash_table *htab;
9304 struct mips_elf_la25_stub *stub;
9305 asection *s;
9306 bfd_byte *loc;
9307 bfd_vma offset, target, target_high, target_low;
9308
9309 stub = (struct mips_elf_la25_stub *) *slot;
9310 hti = (struct mips_htab_traverse_info *) data;
9311 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 9312 BFD_ASSERT (htab != NULL);
861fb55a
DJ
9313
9314 /* Create the section contents, if we haven't already. */
9315 s = stub->stub_section;
9316 loc = s->contents;
9317 if (loc == NULL)
9318 {
9319 loc = bfd_malloc (s->size);
9320 if (loc == NULL)
9321 {
9322 hti->error = TRUE;
9323 return FALSE;
9324 }
9325 s->contents = loc;
9326 }
9327
9328 /* Work out where in the section this stub should go. */
9329 offset = stub->offset;
9330
9331 /* Work out the target address. */
9332 target = (stub->h->root.root.u.def.section->output_section->vma
9333 + stub->h->root.root.u.def.section->output_offset
9334 + stub->h->root.root.u.def.value);
9335 target_high = ((target + 0x8000) >> 16) & 0xffff;
9336 target_low = (target & 0xffff);
9337
9338 if (stub->stub_section != htab->strampoline)
9339 {
9340 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9341 of the section and write the two instructions at the end. */
9342 memset (loc, 0, offset);
9343 loc += offset;
9344 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9345 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9346 }
9347 else
9348 {
9349 /* This is trampoline. */
9350 loc += offset;
9351 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9352 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9353 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9354 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9355 }
9356 return TRUE;
9357}
9358
b49e97c9
TS
9359/* If NAME is one of the special IRIX6 symbols defined by the linker,
9360 adjust it appropriately now. */
9361
9362static void
9719ad41
RS
9363mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9364 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
9365{
9366 /* The linker script takes care of providing names and values for
9367 these, but we must place them into the right sections. */
9368 static const char* const text_section_symbols[] = {
9369 "_ftext",
9370 "_etext",
9371 "__dso_displacement",
9372 "__elf_header",
9373 "__program_header_table",
9374 NULL
9375 };
9376
9377 static const char* const data_section_symbols[] = {
9378 "_fdata",
9379 "_edata",
9380 "_end",
9381 "_fbss",
9382 NULL
9383 };
9384
9385 const char* const *p;
9386 int i;
9387
9388 for (i = 0; i < 2; ++i)
9389 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9390 *p;
9391 ++p)
9392 if (strcmp (*p, name) == 0)
9393 {
9394 /* All of these symbols are given type STT_SECTION by the
9395 IRIX6 linker. */
9396 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 9397 sym->st_other = STO_PROTECTED;
b49e97c9
TS
9398
9399 /* The IRIX linker puts these symbols in special sections. */
9400 if (i == 0)
9401 sym->st_shndx = SHN_MIPS_TEXT;
9402 else
9403 sym->st_shndx = SHN_MIPS_DATA;
9404
9405 break;
9406 }
9407}
9408
9409/* Finish up dynamic symbol handling. We set the contents of various
9410 dynamic sections here. */
9411
b34976b6 9412bfd_boolean
9719ad41
RS
9413_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9414 struct bfd_link_info *info,
9415 struct elf_link_hash_entry *h,
9416 Elf_Internal_Sym *sym)
b49e97c9
TS
9417{
9418 bfd *dynobj;
b49e97c9 9419 asection *sgot;
f4416af6 9420 struct mips_got_info *g, *gg;
b49e97c9 9421 const char *name;
3d6746ca 9422 int idx;
5108fc1b 9423 struct mips_elf_link_hash_table *htab;
738e5348 9424 struct mips_elf_link_hash_entry *hmips;
b49e97c9 9425
5108fc1b 9426 htab = mips_elf_hash_table (info);
4dfe6ac6 9427 BFD_ASSERT (htab != NULL);
b49e97c9 9428 dynobj = elf_hash_table (info)->dynobj;
738e5348 9429 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9430
861fb55a
DJ
9431 BFD_ASSERT (!htab->is_vxworks);
9432
9433 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9434 {
9435 /* We've decided to create a PLT entry for this symbol. */
9436 bfd_byte *loc;
9437 bfd_vma header_address, plt_index, got_address;
9438 bfd_vma got_address_high, got_address_low, load;
9439 const bfd_vma *plt_entry;
9440
9441 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9442 BFD_ASSERT (h->dynindx != -1);
9443 BFD_ASSERT (htab->splt != NULL);
9444 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9445 BFD_ASSERT (!h->def_regular);
9446
9447 /* Calculate the address of the PLT header. */
9448 header_address = (htab->splt->output_section->vma
9449 + htab->splt->output_offset);
9450
9451 /* Calculate the index of the entry. */
9452 plt_index = ((h->plt.offset - htab->plt_header_size)
9453 / htab->plt_entry_size);
9454
9455 /* Calculate the address of the .got.plt entry. */
9456 got_address = (htab->sgotplt->output_section->vma
9457 + htab->sgotplt->output_offset
9458 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9459 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9460 got_address_low = got_address & 0xffff;
9461
9462 /* Initially point the .got.plt entry at the PLT header. */
9463 loc = (htab->sgotplt->contents
9464 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9465 if (ABI_64_P (output_bfd))
9466 bfd_put_64 (output_bfd, header_address, loc);
9467 else
9468 bfd_put_32 (output_bfd, header_address, loc);
9469
9470 /* Find out where the .plt entry should go. */
9471 loc = htab->splt->contents + h->plt.offset;
9472
9473 /* Pick the load opcode. */
9474 load = MIPS_ELF_LOAD_WORD (output_bfd);
9475
9476 /* Fill in the PLT entry itself. */
9477 plt_entry = mips_exec_plt_entry;
9478 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9479 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
6d30f5b2
NC
9480
9481 if (! LOAD_INTERLOCKS_P (output_bfd))
9482 {
9483 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9484 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9485 }
9486 else
9487 {
9488 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9489 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9490 }
861fb55a
DJ
9491
9492 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9493 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9494 plt_index, h->dynindx,
9495 R_MIPS_JUMP_SLOT, got_address);
9496
9497 /* We distinguish between PLT entries and lazy-binding stubs by
9498 giving the former an st_other value of STO_MIPS_PLT. Set the
9499 flag and leave the value if there are any relocations in the
9500 binary where pointer equality matters. */
9501 sym->st_shndx = SHN_UNDEF;
9502 if (h->pointer_equality_needed)
9503 sym->st_other = STO_MIPS_PLT;
9504 else
9505 sym->st_value = 0;
9506 }
9507 else if (h->plt.offset != MINUS_ONE)
b49e97c9 9508 {
861fb55a 9509 /* We've decided to create a lazy-binding stub. */
5108fc1b 9510 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
b49e97c9
TS
9511
9512 /* This symbol has a stub. Set it up. */
9513
9514 BFD_ASSERT (h->dynindx != -1);
9515
5108fc1b
RS
9516 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9517 || (h->dynindx <= 0xffff));
3d6746ca
DD
9518
9519 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
9520 sign extension at runtime in the stub, resulting in a negative
9521 index value. */
9522 if (h->dynindx & ~0x7fffffff)
b34976b6 9523 return FALSE;
b49e97c9
TS
9524
9525 /* Fill the stub. */
3d6746ca
DD
9526 idx = 0;
9527 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9528 idx += 4;
9529 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9530 idx += 4;
5108fc1b 9531 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
3d6746ca 9532 {
5108fc1b 9533 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
3d6746ca
DD
9534 stub + idx);
9535 idx += 4;
9536 }
9537 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9538 idx += 4;
b49e97c9 9539
3d6746ca
DD
9540 /* If a large stub is not required and sign extension is not a
9541 problem, then use legacy code in the stub. */
5108fc1b
RS
9542 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9543 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9544 else if (h->dynindx & ~0x7fff)
3d6746ca
DD
9545 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9546 else
5108fc1b
RS
9547 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9548 stub + idx);
9549
4e41d0d7
RS
9550 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9551 memcpy (htab->sstubs->contents + h->plt.offset,
9552 stub, htab->function_stub_size);
b49e97c9
TS
9553
9554 /* Mark the symbol as undefined. plt.offset != -1 occurs
9555 only for the referenced symbol. */
9556 sym->st_shndx = SHN_UNDEF;
9557
9558 /* The run-time linker uses the st_value field of the symbol
9559 to reset the global offset table entry for this external
9560 to its stub address when unlinking a shared object. */
4e41d0d7
RS
9561 sym->st_value = (htab->sstubs->output_section->vma
9562 + htab->sstubs->output_offset
c5ae1840 9563 + h->plt.offset);
b49e97c9
TS
9564 }
9565
738e5348
RS
9566 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9567 refer to the stub, since only the stub uses the standard calling
9568 conventions. */
9569 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9570 {
9571 BFD_ASSERT (hmips->need_fn_stub);
9572 sym->st_value = (hmips->fn_stub->output_section->vma
9573 + hmips->fn_stub->output_offset);
9574 sym->st_size = hmips->fn_stub->size;
9575 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9576 }
9577
b49e97c9 9578 BFD_ASSERT (h->dynindx != -1
f5385ebf 9579 || h->forced_local);
b49e97c9 9580
23cc69b6 9581 sgot = htab->sgot;
a8028dd0 9582 g = htab->got_info;
b49e97c9
TS
9583 BFD_ASSERT (g != NULL);
9584
9585 /* Run through the global symbol table, creating GOT entries for all
9586 the symbols that need them. */
020d7251 9587 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
9588 {
9589 bfd_vma offset;
9590 bfd_vma value;
9591
6eaa6adc 9592 value = sym->st_value;
738e5348
RS
9593 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9594 R_MIPS_GOT16, info);
b49e97c9
TS
9595 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9596 }
9597
020d7251 9598 if (hmips->global_got_area != GGA_NONE && g->next && h->type != STT_TLS)
f4416af6
AO
9599 {
9600 struct mips_got_entry e, *p;
0626d451 9601 bfd_vma entry;
f4416af6 9602 bfd_vma offset;
f4416af6
AO
9603
9604 gg = g;
9605
9606 e.abfd = output_bfd;
9607 e.symndx = -1;
738e5348 9608 e.d.h = hmips;
0f20cc35 9609 e.tls_type = 0;
143d77c5 9610
f4416af6
AO
9611 for (g = g->next; g->next != gg; g = g->next)
9612 {
9613 if (g->got_entries
9614 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9615 &e)))
9616 {
9617 offset = p->gotidx;
0626d451
RS
9618 if (info->shared
9619 || (elf_hash_table (info)->dynamic_sections_created
9620 && p->d.h != NULL
f5385ebf
AM
9621 && p->d.h->root.def_dynamic
9622 && !p->d.h->root.def_regular))
0626d451
RS
9623 {
9624 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9625 the various compatibility problems, it's easier to mock
9626 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9627 mips_elf_create_dynamic_relocation to calculate the
9628 appropriate addend. */
9629 Elf_Internal_Rela rel[3];
9630
9631 memset (rel, 0, sizeof (rel));
9632 if (ABI_64_P (output_bfd))
9633 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9634 else
9635 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9636 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9637
9638 entry = 0;
9639 if (! (mips_elf_create_dynamic_relocation
9640 (output_bfd, info, rel,
9641 e.d.h, NULL, sym->st_value, &entry, sgot)))
9642 return FALSE;
9643 }
9644 else
9645 entry = sym->st_value;
9646 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
9647 }
9648 }
9649 }
9650
b49e97c9
TS
9651 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9652 name = h->root.root.string;
9653 if (strcmp (name, "_DYNAMIC") == 0
22edb2f1 9654 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
9655 sym->st_shndx = SHN_ABS;
9656 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9657 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9658 {
9659 sym->st_shndx = SHN_ABS;
9660 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9661 sym->st_value = 1;
9662 }
4a14403c 9663 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9664 {
9665 sym->st_shndx = SHN_ABS;
9666 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9667 sym->st_value = elf_gp (output_bfd);
9668 }
9669 else if (SGI_COMPAT (output_bfd))
9670 {
9671 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9672 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9673 {
9674 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9675 sym->st_other = STO_PROTECTED;
9676 sym->st_value = 0;
9677 sym->st_shndx = SHN_MIPS_DATA;
9678 }
9679 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9680 {
9681 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9682 sym->st_other = STO_PROTECTED;
9683 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9684 sym->st_shndx = SHN_ABS;
9685 }
9686 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9687 {
9688 if (h->type == STT_FUNC)
9689 sym->st_shndx = SHN_MIPS_TEXT;
9690 else if (h->type == STT_OBJECT)
9691 sym->st_shndx = SHN_MIPS_DATA;
9692 }
9693 }
9694
861fb55a
DJ
9695 /* Emit a copy reloc, if needed. */
9696 if (h->needs_copy)
9697 {
9698 asection *s;
9699 bfd_vma symval;
9700
9701 BFD_ASSERT (h->dynindx != -1);
9702 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9703
9704 s = mips_elf_rel_dyn_section (info, FALSE);
9705 symval = (h->root.u.def.section->output_section->vma
9706 + h->root.u.def.section->output_offset
9707 + h->root.u.def.value);
9708 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9709 h->dynindx, R_MIPS_COPY, symval);
9710 }
9711
b49e97c9
TS
9712 /* Handle the IRIX6-specific symbols. */
9713 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9714 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9715
9716 if (! info->shared)
9717 {
9718 if (! mips_elf_hash_table (info)->use_rld_obj_head
9719 && (strcmp (name, "__rld_map") == 0
9720 || strcmp (name, "__RLD_MAP") == 0))
9721 {
9722 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9723 BFD_ASSERT (s != NULL);
9724 sym->st_value = s->output_section->vma + s->output_offset;
9719ad41 9725 bfd_put_32 (output_bfd, 0, s->contents);
b49e97c9
TS
9726 if (mips_elf_hash_table (info)->rld_value == 0)
9727 mips_elf_hash_table (info)->rld_value = sym->st_value;
9728 }
9729 else if (mips_elf_hash_table (info)->use_rld_obj_head
9730 && strcmp (name, "__rld_obj_head") == 0)
9731 {
9732 /* IRIX6 does not use a .rld_map section. */
9733 if (IRIX_COMPAT (output_bfd) == ict_irix5
9734 || IRIX_COMPAT (output_bfd) == ict_none)
9735 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9736 != NULL);
9737 mips_elf_hash_table (info)->rld_value = sym->st_value;
9738 }
9739 }
9740
738e5348
RS
9741 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9742 treat MIPS16 symbols like any other. */
30c09090 9743 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
9744 {
9745 BFD_ASSERT (sym->st_value & 1);
9746 sym->st_other -= STO_MIPS16;
9747 }
b49e97c9 9748
b34976b6 9749 return TRUE;
b49e97c9
TS
9750}
9751
0a44bf69
RS
9752/* Likewise, for VxWorks. */
9753
9754bfd_boolean
9755_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9756 struct bfd_link_info *info,
9757 struct elf_link_hash_entry *h,
9758 Elf_Internal_Sym *sym)
9759{
9760 bfd *dynobj;
9761 asection *sgot;
9762 struct mips_got_info *g;
9763 struct mips_elf_link_hash_table *htab;
020d7251 9764 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
9765
9766 htab = mips_elf_hash_table (info);
4dfe6ac6 9767 BFD_ASSERT (htab != NULL);
0a44bf69 9768 dynobj = elf_hash_table (info)->dynobj;
020d7251 9769 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69
RS
9770
9771 if (h->plt.offset != (bfd_vma) -1)
9772 {
6d79d2ed 9773 bfd_byte *loc;
0a44bf69
RS
9774 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9775 Elf_Internal_Rela rel;
9776 static const bfd_vma *plt_entry;
9777
9778 BFD_ASSERT (h->dynindx != -1);
9779 BFD_ASSERT (htab->splt != NULL);
9780 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9781
9782 /* Calculate the address of the .plt entry. */
9783 plt_address = (htab->splt->output_section->vma
9784 + htab->splt->output_offset
9785 + h->plt.offset);
9786
9787 /* Calculate the index of the entry. */
9788 plt_index = ((h->plt.offset - htab->plt_header_size)
9789 / htab->plt_entry_size);
9790
9791 /* Calculate the address of the .got.plt entry. */
9792 got_address = (htab->sgotplt->output_section->vma
9793 + htab->sgotplt->output_offset
9794 + plt_index * 4);
9795
9796 /* Calculate the offset of the .got.plt entry from
9797 _GLOBAL_OFFSET_TABLE_. */
9798 got_offset = mips_elf_gotplt_index (info, h);
9799
9800 /* Calculate the offset for the branch at the start of the PLT
9801 entry. The branch jumps to the beginning of .plt. */
9802 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9803
9804 /* Fill in the initial value of the .got.plt entry. */
9805 bfd_put_32 (output_bfd, plt_address,
9806 htab->sgotplt->contents + plt_index * 4);
9807
9808 /* Find out where the .plt entry should go. */
9809 loc = htab->splt->contents + h->plt.offset;
9810
9811 if (info->shared)
9812 {
9813 plt_entry = mips_vxworks_shared_plt_entry;
9814 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9815 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9816 }
9817 else
9818 {
9819 bfd_vma got_address_high, got_address_low;
9820
9821 plt_entry = mips_vxworks_exec_plt_entry;
9822 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9823 got_address_low = got_address & 0xffff;
9824
9825 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9826 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9827 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9828 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9829 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9830 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9831 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9832 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9833
9834 loc = (htab->srelplt2->contents
9835 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9836
9837 /* Emit a relocation for the .got.plt entry. */
9838 rel.r_offset = got_address;
9839 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9840 rel.r_addend = h->plt.offset;
9841 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9842
9843 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9844 loc += sizeof (Elf32_External_Rela);
9845 rel.r_offset = plt_address + 8;
9846 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9847 rel.r_addend = got_offset;
9848 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9849
9850 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9851 loc += sizeof (Elf32_External_Rela);
9852 rel.r_offset += 4;
9853 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9854 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9855 }
9856
9857 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9858 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9859 rel.r_offset = got_address;
9860 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9861 rel.r_addend = 0;
9862 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9863
9864 if (!h->def_regular)
9865 sym->st_shndx = SHN_UNDEF;
9866 }
9867
9868 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9869
23cc69b6 9870 sgot = htab->sgot;
a8028dd0 9871 g = htab->got_info;
0a44bf69
RS
9872 BFD_ASSERT (g != NULL);
9873
9874 /* See if this symbol has an entry in the GOT. */
020d7251 9875 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
9876 {
9877 bfd_vma offset;
9878 Elf_Internal_Rela outrel;
9879 bfd_byte *loc;
9880 asection *s;
9881
9882 /* Install the symbol value in the GOT. */
9883 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9884 R_MIPS_GOT16, info);
9885 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9886
9887 /* Add a dynamic relocation for it. */
9888 s = mips_elf_rel_dyn_section (info, FALSE);
9889 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9890 outrel.r_offset = (sgot->output_section->vma
9891 + sgot->output_offset
9892 + offset);
9893 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9894 outrel.r_addend = 0;
9895 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9896 }
9897
9898 /* Emit a copy reloc, if needed. */
9899 if (h->needs_copy)
9900 {
9901 Elf_Internal_Rela rel;
9902
9903 BFD_ASSERT (h->dynindx != -1);
9904
9905 rel.r_offset = (h->root.u.def.section->output_section->vma
9906 + h->root.u.def.section->output_offset
9907 + h->root.u.def.value);
9908 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9909 rel.r_addend = 0;
9910 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9911 htab->srelbss->contents
9912 + (htab->srelbss->reloc_count
9913 * sizeof (Elf32_External_Rela)));
9914 ++htab->srelbss->reloc_count;
9915 }
9916
9917 /* If this is a mips16 symbol, force the value to be even. */
30c09090 9918 if (ELF_ST_IS_MIPS16 (sym->st_other))
0a44bf69
RS
9919 sym->st_value &= ~1;
9920
9921 return TRUE;
9922}
9923
861fb55a
DJ
9924/* Write out a plt0 entry to the beginning of .plt. */
9925
9926static void
9927mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9928{
9929 bfd_byte *loc;
9930 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9931 static const bfd_vma *plt_entry;
9932 struct mips_elf_link_hash_table *htab;
9933
9934 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9935 BFD_ASSERT (htab != NULL);
9936
861fb55a
DJ
9937 if (ABI_64_P (output_bfd))
9938 plt_entry = mips_n64_exec_plt0_entry;
9939 else if (ABI_N32_P (output_bfd))
9940 plt_entry = mips_n32_exec_plt0_entry;
9941 else
9942 plt_entry = mips_o32_exec_plt0_entry;
9943
9944 /* Calculate the value of .got.plt. */
9945 gotplt_value = (htab->sgotplt->output_section->vma
9946 + htab->sgotplt->output_offset);
9947 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9948 gotplt_value_low = gotplt_value & 0xffff;
9949
9950 /* The PLT sequence is not safe for N64 if .got.plt's address can
9951 not be loaded in two instructions. */
9952 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9953 || ~(gotplt_value | 0x7fffffff) == 0);
9954
9955 /* Install the PLT header. */
9956 loc = htab->splt->contents;
9957 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9958 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9959 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9960 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9961 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9962 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9963 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9964 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9965}
9966
0a44bf69
RS
9967/* Install the PLT header for a VxWorks executable and finalize the
9968 contents of .rela.plt.unloaded. */
9969
9970static void
9971mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9972{
9973 Elf_Internal_Rela rela;
9974 bfd_byte *loc;
9975 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9976 static const bfd_vma *plt_entry;
9977 struct mips_elf_link_hash_table *htab;
9978
9979 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9980 BFD_ASSERT (htab != NULL);
9981
0a44bf69
RS
9982 plt_entry = mips_vxworks_exec_plt0_entry;
9983
9984 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9985 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9986 + htab->root.hgot->root.u.def.section->output_offset
9987 + htab->root.hgot->root.u.def.value);
9988
9989 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9990 got_value_low = got_value & 0xffff;
9991
9992 /* Calculate the address of the PLT header. */
9993 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9994
9995 /* Install the PLT header. */
9996 loc = htab->splt->contents;
9997 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9998 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9999 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
10000 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10001 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10002 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10003
10004 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
10005 loc = htab->srelplt2->contents;
10006 rela.r_offset = plt_address;
10007 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10008 rela.r_addend = 0;
10009 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10010 loc += sizeof (Elf32_External_Rela);
10011
10012 /* Output the relocation for the following addiu of
10013 %lo(_GLOBAL_OFFSET_TABLE_). */
10014 rela.r_offset += 4;
10015 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10016 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10017 loc += sizeof (Elf32_External_Rela);
10018
10019 /* Fix up the remaining relocations. They may have the wrong
10020 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10021 in which symbols were output. */
10022 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10023 {
10024 Elf_Internal_Rela rel;
10025
10026 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10027 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10028 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10029 loc += sizeof (Elf32_External_Rela);
10030
10031 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10032 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10033 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10034 loc += sizeof (Elf32_External_Rela);
10035
10036 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10037 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10038 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10039 loc += sizeof (Elf32_External_Rela);
10040 }
10041}
10042
10043/* Install the PLT header for a VxWorks shared library. */
10044
10045static void
10046mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10047{
10048 unsigned int i;
10049 struct mips_elf_link_hash_table *htab;
10050
10051 htab = mips_elf_hash_table (info);
4dfe6ac6 10052 BFD_ASSERT (htab != NULL);
0a44bf69
RS
10053
10054 /* We just need to copy the entry byte-by-byte. */
10055 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10056 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10057 htab->splt->contents + i * 4);
10058}
10059
b49e97c9
TS
10060/* Finish up the dynamic sections. */
10061
b34976b6 10062bfd_boolean
9719ad41
RS
10063_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10064 struct bfd_link_info *info)
b49e97c9
TS
10065{
10066 bfd *dynobj;
10067 asection *sdyn;
10068 asection *sgot;
f4416af6 10069 struct mips_got_info *gg, *g;
0a44bf69 10070 struct mips_elf_link_hash_table *htab;
b49e97c9 10071
0a44bf69 10072 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10073 BFD_ASSERT (htab != NULL);
10074
b49e97c9
TS
10075 dynobj = elf_hash_table (info)->dynobj;
10076
10077 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10078
23cc69b6
RS
10079 sgot = htab->sgot;
10080 gg = htab->got_info;
b49e97c9
TS
10081
10082 if (elf_hash_table (info)->dynamic_sections_created)
10083 {
10084 bfd_byte *b;
943284cc 10085 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
10086
10087 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
10088 BFD_ASSERT (gg != NULL);
10089
10090 g = mips_elf_got_for_ibfd (gg, output_bfd);
b49e97c9
TS
10091 BFD_ASSERT (g != NULL);
10092
10093 for (b = sdyn->contents;
eea6121a 10094 b < sdyn->contents + sdyn->size;
b49e97c9
TS
10095 b += MIPS_ELF_DYN_SIZE (dynobj))
10096 {
10097 Elf_Internal_Dyn dyn;
10098 const char *name;
10099 size_t elemsize;
10100 asection *s;
b34976b6 10101 bfd_boolean swap_out_p;
b49e97c9
TS
10102
10103 /* Read in the current dynamic entry. */
10104 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10105
10106 /* Assume that we're going to modify it and write it out. */
b34976b6 10107 swap_out_p = TRUE;
b49e97c9
TS
10108
10109 switch (dyn.d_tag)
10110 {
10111 case DT_RELENT:
b49e97c9
TS
10112 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10113 break;
10114
0a44bf69
RS
10115 case DT_RELAENT:
10116 BFD_ASSERT (htab->is_vxworks);
10117 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10118 break;
10119
b49e97c9
TS
10120 case DT_STRSZ:
10121 /* Rewrite DT_STRSZ. */
10122 dyn.d_un.d_val =
10123 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10124 break;
10125
10126 case DT_PLTGOT:
861fb55a
DJ
10127 s = htab->sgot;
10128 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10129 break;
10130
10131 case DT_MIPS_PLTGOT:
10132 s = htab->sgotplt;
10133 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
10134 break;
10135
10136 case DT_MIPS_RLD_VERSION:
10137 dyn.d_un.d_val = 1; /* XXX */
10138 break;
10139
10140 case DT_MIPS_FLAGS:
10141 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10142 break;
10143
b49e97c9 10144 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
10145 {
10146 time_t t;
10147 time (&t);
10148 dyn.d_un.d_val = t;
10149 }
b49e97c9
TS
10150 break;
10151
10152 case DT_MIPS_ICHECKSUM:
10153 /* XXX FIXME: */
b34976b6 10154 swap_out_p = FALSE;
b49e97c9
TS
10155 break;
10156
10157 case DT_MIPS_IVERSION:
10158 /* XXX FIXME: */
b34976b6 10159 swap_out_p = FALSE;
b49e97c9
TS
10160 break;
10161
10162 case DT_MIPS_BASE_ADDRESS:
10163 s = output_bfd->sections;
10164 BFD_ASSERT (s != NULL);
10165 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10166 break;
10167
10168 case DT_MIPS_LOCAL_GOTNO:
10169 dyn.d_un.d_val = g->local_gotno;
10170 break;
10171
10172 case DT_MIPS_UNREFEXTNO:
10173 /* The index into the dynamic symbol table which is the
10174 entry of the first external symbol that is not
10175 referenced within the same object. */
10176 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10177 break;
10178
10179 case DT_MIPS_GOTSYM:
f4416af6 10180 if (gg->global_gotsym)
b49e97c9 10181 {
f4416af6 10182 dyn.d_un.d_val = gg->global_gotsym->dynindx;
b49e97c9
TS
10183 break;
10184 }
10185 /* In case if we don't have global got symbols we default
10186 to setting DT_MIPS_GOTSYM to the same value as
10187 DT_MIPS_SYMTABNO, so we just fall through. */
10188
10189 case DT_MIPS_SYMTABNO:
10190 name = ".dynsym";
10191 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10192 s = bfd_get_section_by_name (output_bfd, name);
10193 BFD_ASSERT (s != NULL);
10194
eea6121a 10195 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
10196 break;
10197
10198 case DT_MIPS_HIPAGENO:
861fb55a 10199 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
10200 break;
10201
10202 case DT_MIPS_RLD_MAP:
10203 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10204 break;
10205
10206 case DT_MIPS_OPTIONS:
10207 s = (bfd_get_section_by_name
10208 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10209 dyn.d_un.d_ptr = s->vma;
10210 break;
10211
0a44bf69
RS
10212 case DT_RELASZ:
10213 BFD_ASSERT (htab->is_vxworks);
10214 /* The count does not include the JUMP_SLOT relocations. */
10215 if (htab->srelplt)
10216 dyn.d_un.d_val -= htab->srelplt->size;
10217 break;
10218
10219 case DT_PLTREL:
861fb55a
DJ
10220 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10221 if (htab->is_vxworks)
10222 dyn.d_un.d_val = DT_RELA;
10223 else
10224 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
10225 break;
10226
10227 case DT_PLTRELSZ:
861fb55a 10228 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
10229 dyn.d_un.d_val = htab->srelplt->size;
10230 break;
10231
10232 case DT_JMPREL:
861fb55a
DJ
10233 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10234 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
10235 + htab->srelplt->output_offset);
10236 break;
10237
943284cc
DJ
10238 case DT_TEXTREL:
10239 /* If we didn't need any text relocations after all, delete
10240 the dynamic tag. */
10241 if (!(info->flags & DF_TEXTREL))
10242 {
10243 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10244 swap_out_p = FALSE;
10245 }
10246 break;
10247
10248 case DT_FLAGS:
10249 /* If we didn't need any text relocations after all, clear
10250 DF_TEXTREL from DT_FLAGS. */
10251 if (!(info->flags & DF_TEXTREL))
10252 dyn.d_un.d_val &= ~DF_TEXTREL;
10253 else
10254 swap_out_p = FALSE;
10255 break;
10256
b49e97c9 10257 default:
b34976b6 10258 swap_out_p = FALSE;
7a2b07ff
NS
10259 if (htab->is_vxworks
10260 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10261 swap_out_p = TRUE;
b49e97c9
TS
10262 break;
10263 }
10264
943284cc 10265 if (swap_out_p || dyn_skipped)
b49e97c9 10266 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
10267 (dynobj, &dyn, b - dyn_skipped);
10268
10269 if (dyn_to_skip)
10270 {
10271 dyn_skipped += dyn_to_skip;
10272 dyn_to_skip = 0;
10273 }
b49e97c9 10274 }
943284cc
DJ
10275
10276 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10277 if (dyn_skipped > 0)
10278 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
10279 }
10280
b55fd4d4
DJ
10281 if (sgot != NULL && sgot->size > 0
10282 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 10283 {
0a44bf69
RS
10284 if (htab->is_vxworks)
10285 {
10286 /* The first entry of the global offset table points to the
10287 ".dynamic" section. The second is initialized by the
10288 loader and contains the shared library identifier.
10289 The third is also initialized by the loader and points
10290 to the lazy resolution stub. */
10291 MIPS_ELF_PUT_WORD (output_bfd,
10292 sdyn->output_offset + sdyn->output_section->vma,
10293 sgot->contents);
10294 MIPS_ELF_PUT_WORD (output_bfd, 0,
10295 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10296 MIPS_ELF_PUT_WORD (output_bfd, 0,
10297 sgot->contents
10298 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10299 }
10300 else
10301 {
10302 /* The first entry of the global offset table will be filled at
10303 runtime. The second entry will be used by some runtime loaders.
10304 This isn't the case of IRIX rld. */
10305 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 10306 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
10307 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10308 }
b49e97c9 10309
54938e2a
TS
10310 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10311 = MIPS_ELF_GOT_SIZE (output_bfd);
10312 }
b49e97c9 10313
f4416af6
AO
10314 /* Generate dynamic relocations for the non-primary gots. */
10315 if (gg != NULL && gg->next)
10316 {
10317 Elf_Internal_Rela rel[3];
10318 bfd_vma addend = 0;
10319
10320 memset (rel, 0, sizeof (rel));
10321 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10322
10323 for (g = gg->next; g->next != gg; g = g->next)
10324 {
91d6fa6a 10325 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 10326 + g->next->tls_gotno;
f4416af6 10327
9719ad41 10328 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 10329 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
10330 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10331 sgot->contents
91d6fa6a 10332 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
10333
10334 if (! info->shared)
10335 continue;
10336
91d6fa6a 10337 while (got_index < g->assigned_gotno)
f4416af6
AO
10338 {
10339 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
91d6fa6a 10340 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
10341 if (!(mips_elf_create_dynamic_relocation
10342 (output_bfd, info, rel, NULL,
10343 bfd_abs_section_ptr,
10344 0, &addend, sgot)))
10345 return FALSE;
10346 BFD_ASSERT (addend == 0);
10347 }
10348 }
10349 }
10350
3133ddbf
DJ
10351 /* The generation of dynamic relocations for the non-primary gots
10352 adds more dynamic relocations. We cannot count them until
10353 here. */
10354
10355 if (elf_hash_table (info)->dynamic_sections_created)
10356 {
10357 bfd_byte *b;
10358 bfd_boolean swap_out_p;
10359
10360 BFD_ASSERT (sdyn != NULL);
10361
10362 for (b = sdyn->contents;
10363 b < sdyn->contents + sdyn->size;
10364 b += MIPS_ELF_DYN_SIZE (dynobj))
10365 {
10366 Elf_Internal_Dyn dyn;
10367 asection *s;
10368
10369 /* Read in the current dynamic entry. */
10370 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10371
10372 /* Assume that we're going to modify it and write it out. */
10373 swap_out_p = TRUE;
10374
10375 switch (dyn.d_tag)
10376 {
10377 case DT_RELSZ:
10378 /* Reduce DT_RELSZ to account for any relocations we
10379 decided not to make. This is for the n64 irix rld,
10380 which doesn't seem to apply any relocations if there
10381 are trailing null entries. */
0a44bf69 10382 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
10383 dyn.d_un.d_val = (s->reloc_count
10384 * (ABI_64_P (output_bfd)
10385 ? sizeof (Elf64_Mips_External_Rel)
10386 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
10387 /* Adjust the section size too. Tools like the prelinker
10388 can reasonably expect the values to the same. */
10389 elf_section_data (s->output_section)->this_hdr.sh_size
10390 = dyn.d_un.d_val;
3133ddbf
DJ
10391 break;
10392
10393 default:
10394 swap_out_p = FALSE;
10395 break;
10396 }
10397
10398 if (swap_out_p)
10399 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10400 (dynobj, &dyn, b);
10401 }
10402 }
10403
b49e97c9 10404 {
b49e97c9
TS
10405 asection *s;
10406 Elf32_compact_rel cpt;
10407
b49e97c9
TS
10408 if (SGI_COMPAT (output_bfd))
10409 {
10410 /* Write .compact_rel section out. */
10411 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10412 if (s != NULL)
10413 {
10414 cpt.id1 = 1;
10415 cpt.num = s->reloc_count;
10416 cpt.id2 = 2;
10417 cpt.offset = (s->output_section->filepos
10418 + sizeof (Elf32_External_compact_rel));
10419 cpt.reserved0 = 0;
10420 cpt.reserved1 = 0;
10421 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10422 ((Elf32_External_compact_rel *)
10423 s->contents));
10424
10425 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 10426 if (htab->sstubs != NULL)
b49e97c9
TS
10427 {
10428 file_ptr dummy_offset;
10429
4e41d0d7
RS
10430 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10431 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10432 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 10433 htab->function_stub_size);
b49e97c9
TS
10434 }
10435 }
10436 }
10437
0a44bf69
RS
10438 /* The psABI says that the dynamic relocations must be sorted in
10439 increasing order of r_symndx. The VxWorks EABI doesn't require
10440 this, and because the code below handles REL rather than RELA
10441 relocations, using it for VxWorks would be outright harmful. */
10442 if (!htab->is_vxworks)
b49e97c9 10443 {
0a44bf69
RS
10444 s = mips_elf_rel_dyn_section (info, FALSE);
10445 if (s != NULL
10446 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10447 {
10448 reldyn_sorting_bfd = output_bfd;
b49e97c9 10449
0a44bf69
RS
10450 if (ABI_64_P (output_bfd))
10451 qsort ((Elf64_External_Rel *) s->contents + 1,
10452 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10453 sort_dynamic_relocs_64);
10454 else
10455 qsort ((Elf32_External_Rel *) s->contents + 1,
10456 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10457 sort_dynamic_relocs);
10458 }
b49e97c9 10459 }
b49e97c9
TS
10460 }
10461
861fb55a 10462 if (htab->splt && htab->splt->size > 0)
0a44bf69 10463 {
861fb55a
DJ
10464 if (htab->is_vxworks)
10465 {
10466 if (info->shared)
10467 mips_vxworks_finish_shared_plt (output_bfd, info);
10468 else
10469 mips_vxworks_finish_exec_plt (output_bfd, info);
10470 }
0a44bf69 10471 else
861fb55a
DJ
10472 {
10473 BFD_ASSERT (!info->shared);
10474 mips_finish_exec_plt (output_bfd, info);
10475 }
0a44bf69 10476 }
b34976b6 10477 return TRUE;
b49e97c9
TS
10478}
10479
b49e97c9 10480
64543e1a
RS
10481/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10482
10483static void
9719ad41 10484mips_set_isa_flags (bfd *abfd)
b49e97c9 10485{
64543e1a 10486 flagword val;
b49e97c9
TS
10487
10488 switch (bfd_get_mach (abfd))
10489 {
10490 default:
10491 case bfd_mach_mips3000:
10492 val = E_MIPS_ARCH_1;
10493 break;
10494
10495 case bfd_mach_mips3900:
10496 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10497 break;
10498
10499 case bfd_mach_mips6000:
10500 val = E_MIPS_ARCH_2;
10501 break;
10502
10503 case bfd_mach_mips4000:
10504 case bfd_mach_mips4300:
10505 case bfd_mach_mips4400:
10506 case bfd_mach_mips4600:
10507 val = E_MIPS_ARCH_3;
10508 break;
10509
10510 case bfd_mach_mips4010:
10511 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10512 break;
10513
10514 case bfd_mach_mips4100:
10515 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10516 break;
10517
10518 case bfd_mach_mips4111:
10519 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10520 break;
10521
00707a0e
RS
10522 case bfd_mach_mips4120:
10523 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10524 break;
10525
b49e97c9
TS
10526 case bfd_mach_mips4650:
10527 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10528 break;
10529
00707a0e
RS
10530 case bfd_mach_mips5400:
10531 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10532 break;
10533
10534 case bfd_mach_mips5500:
10535 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10536 break;
10537
0d2e43ed
ILT
10538 case bfd_mach_mips9000:
10539 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10540 break;
10541
b49e97c9 10542 case bfd_mach_mips5000:
5a7ea749 10543 case bfd_mach_mips7000:
b49e97c9
TS
10544 case bfd_mach_mips8000:
10545 case bfd_mach_mips10000:
10546 case bfd_mach_mips12000:
3aa3176b
TS
10547 case bfd_mach_mips14000:
10548 case bfd_mach_mips16000:
b49e97c9
TS
10549 val = E_MIPS_ARCH_4;
10550 break;
10551
10552 case bfd_mach_mips5:
10553 val = E_MIPS_ARCH_5;
10554 break;
10555
350cc38d
MS
10556 case bfd_mach_mips_loongson_2e:
10557 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10558 break;
10559
10560 case bfd_mach_mips_loongson_2f:
10561 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10562 break;
10563
b49e97c9
TS
10564 case bfd_mach_mips_sb1:
10565 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10566 break;
10567
6f179bd0
AN
10568 case bfd_mach_mips_octeon:
10569 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10570 break;
10571
52b6b6b9
JM
10572 case bfd_mach_mips_xlr:
10573 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10574 break;
10575
b49e97c9
TS
10576 case bfd_mach_mipsisa32:
10577 val = E_MIPS_ARCH_32;
10578 break;
10579
10580 case bfd_mach_mipsisa64:
10581 val = E_MIPS_ARCH_64;
af7ee8bf
CD
10582 break;
10583
10584 case bfd_mach_mipsisa32r2:
10585 val = E_MIPS_ARCH_32R2;
10586 break;
5f74bc13
CD
10587
10588 case bfd_mach_mipsisa64r2:
10589 val = E_MIPS_ARCH_64R2;
10590 break;
b49e97c9 10591 }
b49e97c9
TS
10592 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10593 elf_elfheader (abfd)->e_flags |= val;
10594
64543e1a
RS
10595}
10596
10597
10598/* The final processing done just before writing out a MIPS ELF object
10599 file. This gets the MIPS architecture right based on the machine
10600 number. This is used by both the 32-bit and the 64-bit ABI. */
10601
10602void
9719ad41
RS
10603_bfd_mips_elf_final_write_processing (bfd *abfd,
10604 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
10605{
10606 unsigned int i;
10607 Elf_Internal_Shdr **hdrpp;
10608 const char *name;
10609 asection *sec;
10610
10611 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10612 is nonzero. This is for compatibility with old objects, which used
10613 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10614 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10615 mips_set_isa_flags (abfd);
10616
b49e97c9
TS
10617 /* Set the sh_info field for .gptab sections and other appropriate
10618 info for each special section. */
10619 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10620 i < elf_numsections (abfd);
10621 i++, hdrpp++)
10622 {
10623 switch ((*hdrpp)->sh_type)
10624 {
10625 case SHT_MIPS_MSYM:
10626 case SHT_MIPS_LIBLIST:
10627 sec = bfd_get_section_by_name (abfd, ".dynstr");
10628 if (sec != NULL)
10629 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10630 break;
10631
10632 case SHT_MIPS_GPTAB:
10633 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10634 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10635 BFD_ASSERT (name != NULL
0112cd26 10636 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
10637 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10638 BFD_ASSERT (sec != NULL);
10639 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10640 break;
10641
10642 case SHT_MIPS_CONTENT:
10643 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10644 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10645 BFD_ASSERT (name != NULL
0112cd26 10646 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
10647 sec = bfd_get_section_by_name (abfd,
10648 name + sizeof ".MIPS.content" - 1);
10649 BFD_ASSERT (sec != NULL);
10650 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10651 break;
10652
10653 case SHT_MIPS_SYMBOL_LIB:
10654 sec = bfd_get_section_by_name (abfd, ".dynsym");
10655 if (sec != NULL)
10656 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10657 sec = bfd_get_section_by_name (abfd, ".liblist");
10658 if (sec != NULL)
10659 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10660 break;
10661
10662 case SHT_MIPS_EVENTS:
10663 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10664 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10665 BFD_ASSERT (name != NULL);
0112cd26 10666 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
10667 sec = bfd_get_section_by_name (abfd,
10668 name + sizeof ".MIPS.events" - 1);
10669 else
10670 {
0112cd26 10671 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
10672 sec = bfd_get_section_by_name (abfd,
10673 (name
10674 + sizeof ".MIPS.post_rel" - 1));
10675 }
10676 BFD_ASSERT (sec != NULL);
10677 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10678 break;
10679
10680 }
10681 }
10682}
10683\f
8dc1a139 10684/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
10685 segments. */
10686
10687int
a6b96beb
AM
10688_bfd_mips_elf_additional_program_headers (bfd *abfd,
10689 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
10690{
10691 asection *s;
10692 int ret = 0;
10693
10694 /* See if we need a PT_MIPS_REGINFO segment. */
10695 s = bfd_get_section_by_name (abfd, ".reginfo");
10696 if (s && (s->flags & SEC_LOAD))
10697 ++ret;
10698
10699 /* See if we need a PT_MIPS_OPTIONS segment. */
10700 if (IRIX_COMPAT (abfd) == ict_irix6
10701 && bfd_get_section_by_name (abfd,
10702 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10703 ++ret;
10704
10705 /* See if we need a PT_MIPS_RTPROC segment. */
10706 if (IRIX_COMPAT (abfd) == ict_irix5
10707 && bfd_get_section_by_name (abfd, ".dynamic")
10708 && bfd_get_section_by_name (abfd, ".mdebug"))
10709 ++ret;
10710
98c904a8
RS
10711 /* Allocate a PT_NULL header in dynamic objects. See
10712 _bfd_mips_elf_modify_segment_map for details. */
10713 if (!SGI_COMPAT (abfd)
10714 && bfd_get_section_by_name (abfd, ".dynamic"))
10715 ++ret;
10716
b49e97c9
TS
10717 return ret;
10718}
10719
8dc1a139 10720/* Modify the segment map for an IRIX5 executable. */
b49e97c9 10721
b34976b6 10722bfd_boolean
9719ad41 10723_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 10724 struct bfd_link_info *info)
b49e97c9
TS
10725{
10726 asection *s;
10727 struct elf_segment_map *m, **pm;
10728 bfd_size_type amt;
10729
10730 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10731 segment. */
10732 s = bfd_get_section_by_name (abfd, ".reginfo");
10733 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10734 {
10735 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10736 if (m->p_type == PT_MIPS_REGINFO)
10737 break;
10738 if (m == NULL)
10739 {
10740 amt = sizeof *m;
9719ad41 10741 m = bfd_zalloc (abfd, amt);
b49e97c9 10742 if (m == NULL)
b34976b6 10743 return FALSE;
b49e97c9
TS
10744
10745 m->p_type = PT_MIPS_REGINFO;
10746 m->count = 1;
10747 m->sections[0] = s;
10748
10749 /* We want to put it after the PHDR and INTERP segments. */
10750 pm = &elf_tdata (abfd)->segment_map;
10751 while (*pm != NULL
10752 && ((*pm)->p_type == PT_PHDR
10753 || (*pm)->p_type == PT_INTERP))
10754 pm = &(*pm)->next;
10755
10756 m->next = *pm;
10757 *pm = m;
10758 }
10759 }
10760
10761 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10762 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 10763 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 10764 table. */
c1fd6598
AO
10765 if (NEWABI_P (abfd)
10766 /* On non-IRIX6 new abi, we'll have already created a segment
10767 for this section, so don't create another. I'm not sure this
10768 is not also the case for IRIX 6, but I can't test it right
10769 now. */
10770 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
10771 {
10772 for (s = abfd->sections; s; s = s->next)
10773 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10774 break;
10775
10776 if (s)
10777 {
10778 struct elf_segment_map *options_segment;
10779
98a8deaf
RS
10780 pm = &elf_tdata (abfd)->segment_map;
10781 while (*pm != NULL
10782 && ((*pm)->p_type == PT_PHDR
10783 || (*pm)->p_type == PT_INTERP))
10784 pm = &(*pm)->next;
b49e97c9 10785
8ded5a0f
AM
10786 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10787 {
10788 amt = sizeof (struct elf_segment_map);
10789 options_segment = bfd_zalloc (abfd, amt);
10790 options_segment->next = *pm;
10791 options_segment->p_type = PT_MIPS_OPTIONS;
10792 options_segment->p_flags = PF_R;
10793 options_segment->p_flags_valid = TRUE;
10794 options_segment->count = 1;
10795 options_segment->sections[0] = s;
10796 *pm = options_segment;
10797 }
b49e97c9
TS
10798 }
10799 }
10800 else
10801 {
10802 if (IRIX_COMPAT (abfd) == ict_irix5)
10803 {
10804 /* If there are .dynamic and .mdebug sections, we make a room
10805 for the RTPROC header. FIXME: Rewrite without section names. */
10806 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10807 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10808 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10809 {
10810 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10811 if (m->p_type == PT_MIPS_RTPROC)
10812 break;
10813 if (m == NULL)
10814 {
10815 amt = sizeof *m;
9719ad41 10816 m = bfd_zalloc (abfd, amt);
b49e97c9 10817 if (m == NULL)
b34976b6 10818 return FALSE;
b49e97c9
TS
10819
10820 m->p_type = PT_MIPS_RTPROC;
10821
10822 s = bfd_get_section_by_name (abfd, ".rtproc");
10823 if (s == NULL)
10824 {
10825 m->count = 0;
10826 m->p_flags = 0;
10827 m->p_flags_valid = 1;
10828 }
10829 else
10830 {
10831 m->count = 1;
10832 m->sections[0] = s;
10833 }
10834
10835 /* We want to put it after the DYNAMIC segment. */
10836 pm = &elf_tdata (abfd)->segment_map;
10837 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10838 pm = &(*pm)->next;
10839 if (*pm != NULL)
10840 pm = &(*pm)->next;
10841
10842 m->next = *pm;
10843 *pm = m;
10844 }
10845 }
10846 }
8dc1a139 10847 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
10848 .dynstr, .dynsym, and .hash sections, and everything in
10849 between. */
10850 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10851 pm = &(*pm)->next)
10852 if ((*pm)->p_type == PT_DYNAMIC)
10853 break;
10854 m = *pm;
10855 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10856 {
10857 /* For a normal mips executable the permissions for the PT_DYNAMIC
10858 segment are read, write and execute. We do that here since
10859 the code in elf.c sets only the read permission. This matters
10860 sometimes for the dynamic linker. */
10861 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10862 {
10863 m->p_flags = PF_R | PF_W | PF_X;
10864 m->p_flags_valid = 1;
10865 }
10866 }
f6f62d6f
RS
10867 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10868 glibc's dynamic linker has traditionally derived the number of
10869 tags from the p_filesz field, and sometimes allocates stack
10870 arrays of that size. An overly-big PT_DYNAMIC segment can
10871 be actively harmful in such cases. Making PT_DYNAMIC contain
10872 other sections can also make life hard for the prelinker,
10873 which might move one of the other sections to a different
10874 PT_LOAD segment. */
10875 if (SGI_COMPAT (abfd)
10876 && m != NULL
10877 && m->count == 1
10878 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
10879 {
10880 static const char *sec_names[] =
10881 {
10882 ".dynamic", ".dynstr", ".dynsym", ".hash"
10883 };
10884 bfd_vma low, high;
10885 unsigned int i, c;
10886 struct elf_segment_map *n;
10887
792b4a53 10888 low = ~(bfd_vma) 0;
b49e97c9
TS
10889 high = 0;
10890 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10891 {
10892 s = bfd_get_section_by_name (abfd, sec_names[i]);
10893 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10894 {
10895 bfd_size_type sz;
10896
10897 if (low > s->vma)
10898 low = s->vma;
eea6121a 10899 sz = s->size;
b49e97c9
TS
10900 if (high < s->vma + sz)
10901 high = s->vma + sz;
10902 }
10903 }
10904
10905 c = 0;
10906 for (s = abfd->sections; s != NULL; s = s->next)
10907 if ((s->flags & SEC_LOAD) != 0
10908 && s->vma >= low
eea6121a 10909 && s->vma + s->size <= high)
b49e97c9
TS
10910 ++c;
10911
10912 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 10913 n = bfd_zalloc (abfd, amt);
b49e97c9 10914 if (n == NULL)
b34976b6 10915 return FALSE;
b49e97c9
TS
10916 *n = *m;
10917 n->count = c;
10918
10919 i = 0;
10920 for (s = abfd->sections; s != NULL; s = s->next)
10921 {
10922 if ((s->flags & SEC_LOAD) != 0
10923 && s->vma >= low
eea6121a 10924 && s->vma + s->size <= high)
b49e97c9
TS
10925 {
10926 n->sections[i] = s;
10927 ++i;
10928 }
10929 }
10930
10931 *pm = n;
10932 }
10933 }
10934
98c904a8
RS
10935 /* Allocate a spare program header in dynamic objects so that tools
10936 like the prelinker can add an extra PT_LOAD entry.
10937
10938 If the prelinker needs to make room for a new PT_LOAD entry, its
10939 standard procedure is to move the first (read-only) sections into
10940 the new (writable) segment. However, the MIPS ABI requires
10941 .dynamic to be in a read-only segment, and the section will often
10942 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10943
10944 Although the prelinker could in principle move .dynamic to a
10945 writable segment, it seems better to allocate a spare program
10946 header instead, and avoid the need to move any sections.
10947 There is a long tradition of allocating spare dynamic tags,
10948 so allocating a spare program header seems like a natural
7c8b76cc
JM
10949 extension.
10950
10951 If INFO is NULL, we may be copying an already prelinked binary
10952 with objcopy or strip, so do not add this header. */
10953 if (info != NULL
10954 && !SGI_COMPAT (abfd)
98c904a8
RS
10955 && bfd_get_section_by_name (abfd, ".dynamic"))
10956 {
10957 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10958 if ((*pm)->p_type == PT_NULL)
10959 break;
10960 if (*pm == NULL)
10961 {
10962 m = bfd_zalloc (abfd, sizeof (*m));
10963 if (m == NULL)
10964 return FALSE;
10965
10966 m->p_type = PT_NULL;
10967 *pm = m;
10968 }
10969 }
10970
b34976b6 10971 return TRUE;
b49e97c9
TS
10972}
10973\f
10974/* Return the section that should be marked against GC for a given
10975 relocation. */
10976
10977asection *
9719ad41 10978_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 10979 struct bfd_link_info *info,
9719ad41
RS
10980 Elf_Internal_Rela *rel,
10981 struct elf_link_hash_entry *h,
10982 Elf_Internal_Sym *sym)
b49e97c9
TS
10983{
10984 /* ??? Do mips16 stub sections need to be handled special? */
10985
10986 if (h != NULL)
07adf181
AM
10987 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10988 {
10989 case R_MIPS_GNU_VTINHERIT:
10990 case R_MIPS_GNU_VTENTRY:
10991 return NULL;
10992 }
b49e97c9 10993
07adf181 10994 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
10995}
10996
10997/* Update the got entry reference counts for the section being removed. */
10998
b34976b6 10999bfd_boolean
9719ad41
RS
11000_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
11001 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11002 asection *sec ATTRIBUTE_UNUSED,
11003 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
11004{
11005#if 0
11006 Elf_Internal_Shdr *symtab_hdr;
11007 struct elf_link_hash_entry **sym_hashes;
11008 bfd_signed_vma *local_got_refcounts;
11009 const Elf_Internal_Rela *rel, *relend;
11010 unsigned long r_symndx;
11011 struct elf_link_hash_entry *h;
11012
7dda2462
TG
11013 if (info->relocatable)
11014 return TRUE;
11015
b49e97c9
TS
11016 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11017 sym_hashes = elf_sym_hashes (abfd);
11018 local_got_refcounts = elf_local_got_refcounts (abfd);
11019
11020 relend = relocs + sec->reloc_count;
11021 for (rel = relocs; rel < relend; rel++)
11022 switch (ELF_R_TYPE (abfd, rel->r_info))
11023 {
738e5348
RS
11024 case R_MIPS16_GOT16:
11025 case R_MIPS16_CALL16:
b49e97c9
TS
11026 case R_MIPS_GOT16:
11027 case R_MIPS_CALL16:
11028 case R_MIPS_CALL_HI16:
11029 case R_MIPS_CALL_LO16:
11030 case R_MIPS_GOT_HI16:
11031 case R_MIPS_GOT_LO16:
4a14403c
TS
11032 case R_MIPS_GOT_DISP:
11033 case R_MIPS_GOT_PAGE:
11034 case R_MIPS_GOT_OFST:
b49e97c9
TS
11035 /* ??? It would seem that the existing MIPS code does no sort
11036 of reference counting or whatnot on its GOT and PLT entries,
11037 so it is not possible to garbage collect them at this time. */
11038 break;
11039
11040 default:
11041 break;
11042 }
11043#endif
11044
b34976b6 11045 return TRUE;
b49e97c9
TS
11046}
11047\f
11048/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11049 hiding the old indirect symbol. Process additional relocation
11050 information. Also called for weakdefs, in which case we just let
11051 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11052
11053void
fcfa13d2 11054_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
11055 struct elf_link_hash_entry *dir,
11056 struct elf_link_hash_entry *ind)
b49e97c9
TS
11057{
11058 struct mips_elf_link_hash_entry *dirmips, *indmips;
11059
fcfa13d2 11060 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 11061
861fb55a
DJ
11062 dirmips = (struct mips_elf_link_hash_entry *) dir;
11063 indmips = (struct mips_elf_link_hash_entry *) ind;
11064 /* Any absolute non-dynamic relocations against an indirect or weak
11065 definition will be against the target symbol. */
11066 if (indmips->has_static_relocs)
11067 dirmips->has_static_relocs = TRUE;
11068
b49e97c9
TS
11069 if (ind->root.type != bfd_link_hash_indirect)
11070 return;
11071
b49e97c9
TS
11072 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11073 if (indmips->readonly_reloc)
b34976b6 11074 dirmips->readonly_reloc = TRUE;
b49e97c9 11075 if (indmips->no_fn_stub)
b34976b6 11076 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
11077 if (indmips->fn_stub)
11078 {
11079 dirmips->fn_stub = indmips->fn_stub;
11080 indmips->fn_stub = NULL;
11081 }
11082 if (indmips->need_fn_stub)
11083 {
11084 dirmips->need_fn_stub = TRUE;
11085 indmips->need_fn_stub = FALSE;
11086 }
11087 if (indmips->call_stub)
11088 {
11089 dirmips->call_stub = indmips->call_stub;
11090 indmips->call_stub = NULL;
11091 }
11092 if (indmips->call_fp_stub)
11093 {
11094 dirmips->call_fp_stub = indmips->call_fp_stub;
11095 indmips->call_fp_stub = NULL;
11096 }
634835ae
RS
11097 if (indmips->global_got_area < dirmips->global_got_area)
11098 dirmips->global_got_area = indmips->global_got_area;
11099 if (indmips->global_got_area < GGA_NONE)
11100 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
11101 if (indmips->has_nonpic_branches)
11102 dirmips->has_nonpic_branches = TRUE;
0f20cc35
DJ
11103
11104 if (dirmips->tls_type == 0)
11105 dirmips->tls_type = indmips->tls_type;
b49e97c9 11106}
b49e97c9 11107\f
d01414a5
TS
11108#define PDR_SIZE 32
11109
b34976b6 11110bfd_boolean
9719ad41
RS
11111_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11112 struct bfd_link_info *info)
d01414a5
TS
11113{
11114 asection *o;
b34976b6 11115 bfd_boolean ret = FALSE;
d01414a5
TS
11116 unsigned char *tdata;
11117 size_t i, skip;
11118
11119 o = bfd_get_section_by_name (abfd, ".pdr");
11120 if (! o)
b34976b6 11121 return FALSE;
eea6121a 11122 if (o->size == 0)
b34976b6 11123 return FALSE;
eea6121a 11124 if (o->size % PDR_SIZE != 0)
b34976b6 11125 return FALSE;
d01414a5
TS
11126 if (o->output_section != NULL
11127 && bfd_is_abs_section (o->output_section))
b34976b6 11128 return FALSE;
d01414a5 11129
eea6121a 11130 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 11131 if (! tdata)
b34976b6 11132 return FALSE;
d01414a5 11133
9719ad41 11134 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 11135 info->keep_memory);
d01414a5
TS
11136 if (!cookie->rels)
11137 {
11138 free (tdata);
b34976b6 11139 return FALSE;
d01414a5
TS
11140 }
11141
11142 cookie->rel = cookie->rels;
11143 cookie->relend = cookie->rels + o->reloc_count;
11144
eea6121a 11145 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 11146 {
c152c796 11147 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
11148 {
11149 tdata[i] = 1;
11150 skip ++;
11151 }
11152 }
11153
11154 if (skip != 0)
11155 {
f0abc2a1 11156 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 11157 o->size -= skip * PDR_SIZE;
b34976b6 11158 ret = TRUE;
d01414a5
TS
11159 }
11160 else
11161 free (tdata);
11162
11163 if (! info->keep_memory)
11164 free (cookie->rels);
11165
11166 return ret;
11167}
11168
b34976b6 11169bfd_boolean
9719ad41 11170_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
11171{
11172 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
11173 return TRUE;
11174 return FALSE;
53bfd6b4 11175}
d01414a5 11176
b34976b6 11177bfd_boolean
c7b8f16e
JB
11178_bfd_mips_elf_write_section (bfd *output_bfd,
11179 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11180 asection *sec, bfd_byte *contents)
d01414a5
TS
11181{
11182 bfd_byte *to, *from, *end;
11183 int i;
11184
11185 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 11186 return FALSE;
d01414a5 11187
f0abc2a1 11188 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 11189 return FALSE;
d01414a5
TS
11190
11191 to = contents;
eea6121a 11192 end = contents + sec->size;
d01414a5
TS
11193 for (from = contents, i = 0;
11194 from < end;
11195 from += PDR_SIZE, i++)
11196 {
f0abc2a1 11197 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
11198 continue;
11199 if (to != from)
11200 memcpy (to, from, PDR_SIZE);
11201 to += PDR_SIZE;
11202 }
11203 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 11204 sec->output_offset, sec->size);
b34976b6 11205 return TRUE;
d01414a5 11206}
53bfd6b4 11207\f
b49e97c9
TS
11208/* MIPS ELF uses a special find_nearest_line routine in order the
11209 handle the ECOFF debugging information. */
11210
11211struct mips_elf_find_line
11212{
11213 struct ecoff_debug_info d;
11214 struct ecoff_find_line i;
11215};
11216
b34976b6 11217bfd_boolean
9719ad41
RS
11218_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11219 asymbol **symbols, bfd_vma offset,
11220 const char **filename_ptr,
11221 const char **functionname_ptr,
11222 unsigned int *line_ptr)
b49e97c9
TS
11223{
11224 asection *msec;
11225
11226 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11227 filename_ptr, functionname_ptr,
11228 line_ptr))
b34976b6 11229 return TRUE;
b49e97c9
TS
11230
11231 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11232 filename_ptr, functionname_ptr,
9719ad41 11233 line_ptr, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 11234 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 11235 return TRUE;
b49e97c9
TS
11236
11237 msec = bfd_get_section_by_name (abfd, ".mdebug");
11238 if (msec != NULL)
11239 {
11240 flagword origflags;
11241 struct mips_elf_find_line *fi;
11242 const struct ecoff_debug_swap * const swap =
11243 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11244
11245 /* If we are called during a link, mips_elf_final_link may have
11246 cleared the SEC_HAS_CONTENTS field. We force it back on here
11247 if appropriate (which it normally will be). */
11248 origflags = msec->flags;
11249 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11250 msec->flags |= SEC_HAS_CONTENTS;
11251
11252 fi = elf_tdata (abfd)->find_line_info;
11253 if (fi == NULL)
11254 {
11255 bfd_size_type external_fdr_size;
11256 char *fraw_src;
11257 char *fraw_end;
11258 struct fdr *fdr_ptr;
11259 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11260
9719ad41 11261 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
11262 if (fi == NULL)
11263 {
11264 msec->flags = origflags;
b34976b6 11265 return FALSE;
b49e97c9
TS
11266 }
11267
11268 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11269 {
11270 msec->flags = origflags;
b34976b6 11271 return FALSE;
b49e97c9
TS
11272 }
11273
11274 /* Swap in the FDR information. */
11275 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 11276 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
11277 if (fi->d.fdr == NULL)
11278 {
11279 msec->flags = origflags;
b34976b6 11280 return FALSE;
b49e97c9
TS
11281 }
11282 external_fdr_size = swap->external_fdr_size;
11283 fdr_ptr = fi->d.fdr;
11284 fraw_src = (char *) fi->d.external_fdr;
11285 fraw_end = (fraw_src
11286 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11287 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 11288 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9
TS
11289
11290 elf_tdata (abfd)->find_line_info = fi;
11291
11292 /* Note that we don't bother to ever free this information.
11293 find_nearest_line is either called all the time, as in
11294 objdump -l, so the information should be saved, or it is
11295 rarely called, as in ld error messages, so the memory
11296 wasted is unimportant. Still, it would probably be a
11297 good idea for free_cached_info to throw it away. */
11298 }
11299
11300 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11301 &fi->i, filename_ptr, functionname_ptr,
11302 line_ptr))
11303 {
11304 msec->flags = origflags;
b34976b6 11305 return TRUE;
b49e97c9
TS
11306 }
11307
11308 msec->flags = origflags;
11309 }
11310
11311 /* Fall back on the generic ELF find_nearest_line routine. */
11312
11313 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11314 filename_ptr, functionname_ptr,
11315 line_ptr);
11316}
4ab527b0
FF
11317
11318bfd_boolean
11319_bfd_mips_elf_find_inliner_info (bfd *abfd,
11320 const char **filename_ptr,
11321 const char **functionname_ptr,
11322 unsigned int *line_ptr)
11323{
11324 bfd_boolean found;
11325 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11326 functionname_ptr, line_ptr,
11327 & elf_tdata (abfd)->dwarf2_find_line_info);
11328 return found;
11329}
11330
b49e97c9
TS
11331\f
11332/* When are writing out the .options or .MIPS.options section,
11333 remember the bytes we are writing out, so that we can install the
11334 GP value in the section_processing routine. */
11335
b34976b6 11336bfd_boolean
9719ad41
RS
11337_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11338 const void *location,
11339 file_ptr offset, bfd_size_type count)
b49e97c9 11340{
cc2e31b9 11341 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
11342 {
11343 bfd_byte *c;
11344
11345 if (elf_section_data (section) == NULL)
11346 {
11347 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 11348 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 11349 if (elf_section_data (section) == NULL)
b34976b6 11350 return FALSE;
b49e97c9 11351 }
f0abc2a1 11352 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
11353 if (c == NULL)
11354 {
eea6121a 11355 c = bfd_zalloc (abfd, section->size);
b49e97c9 11356 if (c == NULL)
b34976b6 11357 return FALSE;
f0abc2a1 11358 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
11359 }
11360
9719ad41 11361 memcpy (c + offset, location, count);
b49e97c9
TS
11362 }
11363
11364 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11365 count);
11366}
11367
11368/* This is almost identical to bfd_generic_get_... except that some
11369 MIPS relocations need to be handled specially. Sigh. */
11370
11371bfd_byte *
9719ad41
RS
11372_bfd_elf_mips_get_relocated_section_contents
11373 (bfd *abfd,
11374 struct bfd_link_info *link_info,
11375 struct bfd_link_order *link_order,
11376 bfd_byte *data,
11377 bfd_boolean relocatable,
11378 asymbol **symbols)
b49e97c9
TS
11379{
11380 /* Get enough memory to hold the stuff */
11381 bfd *input_bfd = link_order->u.indirect.section->owner;
11382 asection *input_section = link_order->u.indirect.section;
eea6121a 11383 bfd_size_type sz;
b49e97c9
TS
11384
11385 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11386 arelent **reloc_vector = NULL;
11387 long reloc_count;
11388
11389 if (reloc_size < 0)
11390 goto error_return;
11391
9719ad41 11392 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
11393 if (reloc_vector == NULL && reloc_size != 0)
11394 goto error_return;
11395
11396 /* read in the section */
eea6121a
AM
11397 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11398 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
11399 goto error_return;
11400
b49e97c9
TS
11401 reloc_count = bfd_canonicalize_reloc (input_bfd,
11402 input_section,
11403 reloc_vector,
11404 symbols);
11405 if (reloc_count < 0)
11406 goto error_return;
11407
11408 if (reloc_count > 0)
11409 {
11410 arelent **parent;
11411 /* for mips */
11412 int gp_found;
11413 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11414
11415 {
11416 struct bfd_hash_entry *h;
11417 struct bfd_link_hash_entry *lh;
11418 /* Skip all this stuff if we aren't mixing formats. */
11419 if (abfd && input_bfd
11420 && abfd->xvec == input_bfd->xvec)
11421 lh = 0;
11422 else
11423 {
b34976b6 11424 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
11425 lh = (struct bfd_link_hash_entry *) h;
11426 }
11427 lookup:
11428 if (lh)
11429 {
11430 switch (lh->type)
11431 {
11432 case bfd_link_hash_undefined:
11433 case bfd_link_hash_undefweak:
11434 case bfd_link_hash_common:
11435 gp_found = 0;
11436 break;
11437 case bfd_link_hash_defined:
11438 case bfd_link_hash_defweak:
11439 gp_found = 1;
11440 gp = lh->u.def.value;
11441 break;
11442 case bfd_link_hash_indirect:
11443 case bfd_link_hash_warning:
11444 lh = lh->u.i.link;
11445 /* @@FIXME ignoring warning for now */
11446 goto lookup;
11447 case bfd_link_hash_new:
11448 default:
11449 abort ();
11450 }
11451 }
11452 else
11453 gp_found = 0;
11454 }
11455 /* end mips */
9719ad41 11456 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 11457 {
9719ad41 11458 char *error_message = NULL;
b49e97c9
TS
11459 bfd_reloc_status_type r;
11460
11461 /* Specific to MIPS: Deal with relocation types that require
11462 knowing the gp of the output bfd. */
11463 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 11464
8236346f
EC
11465 /* If we've managed to find the gp and have a special
11466 function for the relocation then go ahead, else default
11467 to the generic handling. */
11468 if (gp_found
11469 && (*parent)->howto->special_function
11470 == _bfd_mips_elf32_gprel16_reloc)
11471 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11472 input_section, relocatable,
11473 data, gp);
11474 else
86324f90 11475 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
11476 input_section,
11477 relocatable ? abfd : NULL,
11478 &error_message);
b49e97c9 11479
1049f94e 11480 if (relocatable)
b49e97c9
TS
11481 {
11482 asection *os = input_section->output_section;
11483
11484 /* A partial link, so keep the relocs */
11485 os->orelocation[os->reloc_count] = *parent;
11486 os->reloc_count++;
11487 }
11488
11489 if (r != bfd_reloc_ok)
11490 {
11491 switch (r)
11492 {
11493 case bfd_reloc_undefined:
11494 if (!((*link_info->callbacks->undefined_symbol)
11495 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 11496 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
11497 goto error_return;
11498 break;
11499 case bfd_reloc_dangerous:
9719ad41 11500 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
11501 if (!((*link_info->callbacks->reloc_dangerous)
11502 (link_info, error_message, input_bfd, input_section,
11503 (*parent)->address)))
11504 goto error_return;
11505 break;
11506 case bfd_reloc_overflow:
11507 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
11508 (link_info, NULL,
11509 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
11510 (*parent)->howto->name, (*parent)->addend,
11511 input_bfd, input_section, (*parent)->address)))
11512 goto error_return;
11513 break;
11514 case bfd_reloc_outofrange:
11515 default:
11516 abort ();
11517 break;
11518 }
11519
11520 }
11521 }
11522 }
11523 if (reloc_vector != NULL)
11524 free (reloc_vector);
11525 return data;
11526
11527error_return:
11528 if (reloc_vector != NULL)
11529 free (reloc_vector);
11530 return NULL;
11531}
11532\f
11533/* Create a MIPS ELF linker hash table. */
11534
11535struct bfd_link_hash_table *
9719ad41 11536_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
11537{
11538 struct mips_elf_link_hash_table *ret;
11539 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11540
9719ad41
RS
11541 ret = bfd_malloc (amt);
11542 if (ret == NULL)
b49e97c9
TS
11543 return NULL;
11544
66eb6687
AM
11545 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11546 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
11547 sizeof (struct mips_elf_link_hash_entry),
11548 MIPS_ELF_DATA))
b49e97c9 11549 {
e2d34d7d 11550 free (ret);
b49e97c9
TS
11551 return NULL;
11552 }
11553
11554#if 0
11555 /* We no longer use this. */
11556 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11557 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11558#endif
11559 ret->procedure_count = 0;
11560 ret->compact_rel_size = 0;
b34976b6 11561 ret->use_rld_obj_head = FALSE;
b49e97c9 11562 ret->rld_value = 0;
b34976b6 11563 ret->mips16_stubs_seen = FALSE;
861fb55a 11564 ret->use_plts_and_copy_relocs = FALSE;
0a44bf69 11565 ret->is_vxworks = FALSE;
0e53d9da 11566 ret->small_data_overflow_reported = FALSE;
0a44bf69
RS
11567 ret->srelbss = NULL;
11568 ret->sdynbss = NULL;
11569 ret->srelplt = NULL;
11570 ret->srelplt2 = NULL;
11571 ret->sgotplt = NULL;
11572 ret->splt = NULL;
4e41d0d7 11573 ret->sstubs = NULL;
a8028dd0
RS
11574 ret->sgot = NULL;
11575 ret->got_info = NULL;
0a44bf69
RS
11576 ret->plt_header_size = 0;
11577 ret->plt_entry_size = 0;
33bb52fb 11578 ret->lazy_stub_count = 0;
5108fc1b 11579 ret->function_stub_size = 0;
861fb55a
DJ
11580 ret->strampoline = NULL;
11581 ret->la25_stubs = NULL;
11582 ret->add_stub_section = NULL;
b49e97c9
TS
11583
11584 return &ret->root.root;
11585}
0a44bf69
RS
11586
11587/* Likewise, but indicate that the target is VxWorks. */
11588
11589struct bfd_link_hash_table *
11590_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11591{
11592 struct bfd_link_hash_table *ret;
11593
11594 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11595 if (ret)
11596 {
11597 struct mips_elf_link_hash_table *htab;
11598
11599 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
11600 htab->use_plts_and_copy_relocs = TRUE;
11601 htab->is_vxworks = TRUE;
0a44bf69
RS
11602 }
11603 return ret;
11604}
861fb55a
DJ
11605
11606/* A function that the linker calls if we are allowed to use PLTs
11607 and copy relocs. */
11608
11609void
11610_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11611{
11612 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11613}
b49e97c9
TS
11614\f
11615/* We need to use a special link routine to handle the .reginfo and
11616 the .mdebug sections. We need to merge all instances of these
11617 sections together, not write them all out sequentially. */
11618
b34976b6 11619bfd_boolean
9719ad41 11620_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 11621{
b49e97c9
TS
11622 asection *o;
11623 struct bfd_link_order *p;
11624 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11625 asection *rtproc_sec;
11626 Elf32_RegInfo reginfo;
11627 struct ecoff_debug_info debug;
861fb55a 11628 struct mips_htab_traverse_info hti;
7a2a6943
NC
11629 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11630 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 11631 HDRR *symhdr = &debug.symbolic_header;
9719ad41 11632 void *mdebug_handle = NULL;
b49e97c9
TS
11633 asection *s;
11634 EXTR esym;
11635 unsigned int i;
11636 bfd_size_type amt;
0a44bf69 11637 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
11638
11639 static const char * const secname[] =
11640 {
11641 ".text", ".init", ".fini", ".data",
11642 ".rodata", ".sdata", ".sbss", ".bss"
11643 };
11644 static const int sc[] =
11645 {
11646 scText, scInit, scFini, scData,
11647 scRData, scSData, scSBss, scBss
11648 };
11649
d4596a51
RS
11650 /* Sort the dynamic symbols so that those with GOT entries come after
11651 those without. */
0a44bf69 11652 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11653 BFD_ASSERT (htab != NULL);
11654
d4596a51
RS
11655 if (!mips_elf_sort_hash_table (abfd, info))
11656 return FALSE;
b49e97c9 11657
861fb55a
DJ
11658 /* Create any scheduled LA25 stubs. */
11659 hti.info = info;
11660 hti.output_bfd = abfd;
11661 hti.error = FALSE;
11662 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11663 if (hti.error)
11664 return FALSE;
11665
b49e97c9
TS
11666 /* Get a value for the GP register. */
11667 if (elf_gp (abfd) == 0)
11668 {
11669 struct bfd_link_hash_entry *h;
11670
b34976b6 11671 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 11672 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
11673 elf_gp (abfd) = (h->u.def.value
11674 + h->u.def.section->output_section->vma
11675 + h->u.def.section->output_offset);
0a44bf69
RS
11676 else if (htab->is_vxworks
11677 && (h = bfd_link_hash_lookup (info->hash,
11678 "_GLOBAL_OFFSET_TABLE_",
11679 FALSE, FALSE, TRUE))
11680 && h->type == bfd_link_hash_defined)
11681 elf_gp (abfd) = (h->u.def.section->output_section->vma
11682 + h->u.def.section->output_offset
11683 + h->u.def.value);
1049f94e 11684 else if (info->relocatable)
b49e97c9
TS
11685 {
11686 bfd_vma lo = MINUS_ONE;
11687
11688 /* Find the GP-relative section with the lowest offset. */
9719ad41 11689 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11690 if (o->vma < lo
11691 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11692 lo = o->vma;
11693
11694 /* And calculate GP relative to that. */
0a44bf69 11695 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
11696 }
11697 else
11698 {
11699 /* If the relocate_section function needs to do a reloc
11700 involving the GP value, it should make a reloc_dangerous
11701 callback to warn that GP is not defined. */
11702 }
11703 }
11704
11705 /* Go through the sections and collect the .reginfo and .mdebug
11706 information. */
11707 reginfo_sec = NULL;
11708 mdebug_sec = NULL;
11709 gptab_data_sec = NULL;
11710 gptab_bss_sec = NULL;
9719ad41 11711 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11712 {
11713 if (strcmp (o->name, ".reginfo") == 0)
11714 {
11715 memset (&reginfo, 0, sizeof reginfo);
11716
11717 /* We have found the .reginfo section in the output file.
11718 Look through all the link_orders comprising it and merge
11719 the information together. */
8423293d 11720 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11721 {
11722 asection *input_section;
11723 bfd *input_bfd;
11724 Elf32_External_RegInfo ext;
11725 Elf32_RegInfo sub;
11726
11727 if (p->type != bfd_indirect_link_order)
11728 {
11729 if (p->type == bfd_data_link_order)
11730 continue;
11731 abort ();
11732 }
11733
11734 input_section = p->u.indirect.section;
11735 input_bfd = input_section->owner;
11736
b49e97c9 11737 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 11738 &ext, 0, sizeof ext))
b34976b6 11739 return FALSE;
b49e97c9
TS
11740
11741 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11742
11743 reginfo.ri_gprmask |= sub.ri_gprmask;
11744 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11745 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11746 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11747 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11748
11749 /* ri_gp_value is set by the function
11750 mips_elf32_section_processing when the section is
11751 finally written out. */
11752
11753 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11754 elf_link_input_bfd ignores this section. */
11755 input_section->flags &= ~SEC_HAS_CONTENTS;
11756 }
11757
11758 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 11759 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
11760
11761 /* Skip this section later on (I don't think this currently
11762 matters, but someday it might). */
8423293d 11763 o->map_head.link_order = NULL;
b49e97c9
TS
11764
11765 reginfo_sec = o;
11766 }
11767
11768 if (strcmp (o->name, ".mdebug") == 0)
11769 {
11770 struct extsym_info einfo;
11771 bfd_vma last;
11772
11773 /* We have found the .mdebug section in the output file.
11774 Look through all the link_orders comprising it and merge
11775 the information together. */
11776 symhdr->magic = swap->sym_magic;
11777 /* FIXME: What should the version stamp be? */
11778 symhdr->vstamp = 0;
11779 symhdr->ilineMax = 0;
11780 symhdr->cbLine = 0;
11781 symhdr->idnMax = 0;
11782 symhdr->ipdMax = 0;
11783 symhdr->isymMax = 0;
11784 symhdr->ioptMax = 0;
11785 symhdr->iauxMax = 0;
11786 symhdr->issMax = 0;
11787 symhdr->issExtMax = 0;
11788 symhdr->ifdMax = 0;
11789 symhdr->crfd = 0;
11790 symhdr->iextMax = 0;
11791
11792 /* We accumulate the debugging information itself in the
11793 debug_info structure. */
11794 debug.line = NULL;
11795 debug.external_dnr = NULL;
11796 debug.external_pdr = NULL;
11797 debug.external_sym = NULL;
11798 debug.external_opt = NULL;
11799 debug.external_aux = NULL;
11800 debug.ss = NULL;
11801 debug.ssext = debug.ssext_end = NULL;
11802 debug.external_fdr = NULL;
11803 debug.external_rfd = NULL;
11804 debug.external_ext = debug.external_ext_end = NULL;
11805
11806 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 11807 if (mdebug_handle == NULL)
b34976b6 11808 return FALSE;
b49e97c9
TS
11809
11810 esym.jmptbl = 0;
11811 esym.cobol_main = 0;
11812 esym.weakext = 0;
11813 esym.reserved = 0;
11814 esym.ifd = ifdNil;
11815 esym.asym.iss = issNil;
11816 esym.asym.st = stLocal;
11817 esym.asym.reserved = 0;
11818 esym.asym.index = indexNil;
11819 last = 0;
11820 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11821 {
11822 esym.asym.sc = sc[i];
11823 s = bfd_get_section_by_name (abfd, secname[i]);
11824 if (s != NULL)
11825 {
11826 esym.asym.value = s->vma;
eea6121a 11827 last = s->vma + s->size;
b49e97c9
TS
11828 }
11829 else
11830 esym.asym.value = last;
11831 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11832 secname[i], &esym))
b34976b6 11833 return FALSE;
b49e97c9
TS
11834 }
11835
8423293d 11836 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11837 {
11838 asection *input_section;
11839 bfd *input_bfd;
11840 const struct ecoff_debug_swap *input_swap;
11841 struct ecoff_debug_info input_debug;
11842 char *eraw_src;
11843 char *eraw_end;
11844
11845 if (p->type != bfd_indirect_link_order)
11846 {
11847 if (p->type == bfd_data_link_order)
11848 continue;
11849 abort ();
11850 }
11851
11852 input_section = p->u.indirect.section;
11853 input_bfd = input_section->owner;
11854
d5eaccd7 11855 if (!is_mips_elf (input_bfd))
b49e97c9
TS
11856 {
11857 /* I don't know what a non MIPS ELF bfd would be
11858 doing with a .mdebug section, but I don't really
11859 want to deal with it. */
11860 continue;
11861 }
11862
11863 input_swap = (get_elf_backend_data (input_bfd)
11864 ->elf_backend_ecoff_debug_swap);
11865
eea6121a 11866 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
11867
11868 /* The ECOFF linking code expects that we have already
11869 read in the debugging information and set up an
11870 ecoff_debug_info structure, so we do that now. */
11871 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11872 &input_debug))
b34976b6 11873 return FALSE;
b49e97c9
TS
11874
11875 if (! (bfd_ecoff_debug_accumulate
11876 (mdebug_handle, abfd, &debug, swap, input_bfd,
11877 &input_debug, input_swap, info)))
b34976b6 11878 return FALSE;
b49e97c9
TS
11879
11880 /* Loop through the external symbols. For each one with
11881 interesting information, try to find the symbol in
11882 the linker global hash table and save the information
11883 for the output external symbols. */
11884 eraw_src = input_debug.external_ext;
11885 eraw_end = (eraw_src
11886 + (input_debug.symbolic_header.iextMax
11887 * input_swap->external_ext_size));
11888 for (;
11889 eraw_src < eraw_end;
11890 eraw_src += input_swap->external_ext_size)
11891 {
11892 EXTR ext;
11893 const char *name;
11894 struct mips_elf_link_hash_entry *h;
11895
9719ad41 11896 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
11897 if (ext.asym.sc == scNil
11898 || ext.asym.sc == scUndefined
11899 || ext.asym.sc == scSUndefined)
11900 continue;
11901
11902 name = input_debug.ssext + ext.asym.iss;
11903 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 11904 name, FALSE, FALSE, TRUE);
b49e97c9
TS
11905 if (h == NULL || h->esym.ifd != -2)
11906 continue;
11907
11908 if (ext.ifd != -1)
11909 {
11910 BFD_ASSERT (ext.ifd
11911 < input_debug.symbolic_header.ifdMax);
11912 ext.ifd = input_debug.ifdmap[ext.ifd];
11913 }
11914
11915 h->esym = ext;
11916 }
11917
11918 /* Free up the information we just read. */
11919 free (input_debug.line);
11920 free (input_debug.external_dnr);
11921 free (input_debug.external_pdr);
11922 free (input_debug.external_sym);
11923 free (input_debug.external_opt);
11924 free (input_debug.external_aux);
11925 free (input_debug.ss);
11926 free (input_debug.ssext);
11927 free (input_debug.external_fdr);
11928 free (input_debug.external_rfd);
11929 free (input_debug.external_ext);
11930
11931 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11932 elf_link_input_bfd ignores this section. */
11933 input_section->flags &= ~SEC_HAS_CONTENTS;
11934 }
11935
11936 if (SGI_COMPAT (abfd) && info->shared)
11937 {
11938 /* Create .rtproc section. */
11939 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11940 if (rtproc_sec == NULL)
11941 {
11942 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11943 | SEC_LINKER_CREATED | SEC_READONLY);
11944
3496cb2a
L
11945 rtproc_sec = bfd_make_section_with_flags (abfd,
11946 ".rtproc",
11947 flags);
b49e97c9 11948 if (rtproc_sec == NULL
b49e97c9 11949 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 11950 return FALSE;
b49e97c9
TS
11951 }
11952
11953 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11954 info, rtproc_sec,
11955 &debug))
b34976b6 11956 return FALSE;
b49e97c9
TS
11957 }
11958
11959 /* Build the external symbol information. */
11960 einfo.abfd = abfd;
11961 einfo.info = info;
11962 einfo.debug = &debug;
11963 einfo.swap = swap;
b34976b6 11964 einfo.failed = FALSE;
b49e97c9 11965 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 11966 mips_elf_output_extsym, &einfo);
b49e97c9 11967 if (einfo.failed)
b34976b6 11968 return FALSE;
b49e97c9
TS
11969
11970 /* Set the size of the .mdebug section. */
eea6121a 11971 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
11972
11973 /* Skip this section later on (I don't think this currently
11974 matters, but someday it might). */
8423293d 11975 o->map_head.link_order = NULL;
b49e97c9
TS
11976
11977 mdebug_sec = o;
11978 }
11979
0112cd26 11980 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
11981 {
11982 const char *subname;
11983 unsigned int c;
11984 Elf32_gptab *tab;
11985 Elf32_External_gptab *ext_tab;
11986 unsigned int j;
11987
11988 /* The .gptab.sdata and .gptab.sbss sections hold
11989 information describing how the small data area would
11990 change depending upon the -G switch. These sections
11991 not used in executables files. */
1049f94e 11992 if (! info->relocatable)
b49e97c9 11993 {
8423293d 11994 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11995 {
11996 asection *input_section;
11997
11998 if (p->type != bfd_indirect_link_order)
11999 {
12000 if (p->type == bfd_data_link_order)
12001 continue;
12002 abort ();
12003 }
12004
12005 input_section = p->u.indirect.section;
12006
12007 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12008 elf_link_input_bfd ignores this section. */
12009 input_section->flags &= ~SEC_HAS_CONTENTS;
12010 }
12011
12012 /* Skip this section later on (I don't think this
12013 currently matters, but someday it might). */
8423293d 12014 o->map_head.link_order = NULL;
b49e97c9
TS
12015
12016 /* Really remove the section. */
5daa8fe7 12017 bfd_section_list_remove (abfd, o);
b49e97c9
TS
12018 --abfd->section_count;
12019
12020 continue;
12021 }
12022
12023 /* There is one gptab for initialized data, and one for
12024 uninitialized data. */
12025 if (strcmp (o->name, ".gptab.sdata") == 0)
12026 gptab_data_sec = o;
12027 else if (strcmp (o->name, ".gptab.sbss") == 0)
12028 gptab_bss_sec = o;
12029 else
12030 {
12031 (*_bfd_error_handler)
12032 (_("%s: illegal section name `%s'"),
12033 bfd_get_filename (abfd), o->name);
12034 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 12035 return FALSE;
b49e97c9
TS
12036 }
12037
12038 /* The linker script always combines .gptab.data and
12039 .gptab.sdata into .gptab.sdata, and likewise for
12040 .gptab.bss and .gptab.sbss. It is possible that there is
12041 no .sdata or .sbss section in the output file, in which
12042 case we must change the name of the output section. */
12043 subname = o->name + sizeof ".gptab" - 1;
12044 if (bfd_get_section_by_name (abfd, subname) == NULL)
12045 {
12046 if (o == gptab_data_sec)
12047 o->name = ".gptab.data";
12048 else
12049 o->name = ".gptab.bss";
12050 subname = o->name + sizeof ".gptab" - 1;
12051 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
12052 }
12053
12054 /* Set up the first entry. */
12055 c = 1;
12056 amt = c * sizeof (Elf32_gptab);
9719ad41 12057 tab = bfd_malloc (amt);
b49e97c9 12058 if (tab == NULL)
b34976b6 12059 return FALSE;
b49e97c9
TS
12060 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
12061 tab[0].gt_header.gt_unused = 0;
12062
12063 /* Combine the input sections. */
8423293d 12064 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
12065 {
12066 asection *input_section;
12067 bfd *input_bfd;
12068 bfd_size_type size;
12069 unsigned long last;
12070 bfd_size_type gpentry;
12071
12072 if (p->type != bfd_indirect_link_order)
12073 {
12074 if (p->type == bfd_data_link_order)
12075 continue;
12076 abort ();
12077 }
12078
12079 input_section = p->u.indirect.section;
12080 input_bfd = input_section->owner;
12081
12082 /* Combine the gptab entries for this input section one
12083 by one. We know that the input gptab entries are
12084 sorted by ascending -G value. */
eea6121a 12085 size = input_section->size;
b49e97c9
TS
12086 last = 0;
12087 for (gpentry = sizeof (Elf32_External_gptab);
12088 gpentry < size;
12089 gpentry += sizeof (Elf32_External_gptab))
12090 {
12091 Elf32_External_gptab ext_gptab;
12092 Elf32_gptab int_gptab;
12093 unsigned long val;
12094 unsigned long add;
b34976b6 12095 bfd_boolean exact;
b49e97c9
TS
12096 unsigned int look;
12097
12098 if (! (bfd_get_section_contents
9719ad41
RS
12099 (input_bfd, input_section, &ext_gptab, gpentry,
12100 sizeof (Elf32_External_gptab))))
b49e97c9
TS
12101 {
12102 free (tab);
b34976b6 12103 return FALSE;
b49e97c9
TS
12104 }
12105
12106 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12107 &int_gptab);
12108 val = int_gptab.gt_entry.gt_g_value;
12109 add = int_gptab.gt_entry.gt_bytes - last;
12110
b34976b6 12111 exact = FALSE;
b49e97c9
TS
12112 for (look = 1; look < c; look++)
12113 {
12114 if (tab[look].gt_entry.gt_g_value >= val)
12115 tab[look].gt_entry.gt_bytes += add;
12116
12117 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 12118 exact = TRUE;
b49e97c9
TS
12119 }
12120
12121 if (! exact)
12122 {
12123 Elf32_gptab *new_tab;
12124 unsigned int max;
12125
12126 /* We need a new table entry. */
12127 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 12128 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
12129 if (new_tab == NULL)
12130 {
12131 free (tab);
b34976b6 12132 return FALSE;
b49e97c9
TS
12133 }
12134 tab = new_tab;
12135 tab[c].gt_entry.gt_g_value = val;
12136 tab[c].gt_entry.gt_bytes = add;
12137
12138 /* Merge in the size for the next smallest -G
12139 value, since that will be implied by this new
12140 value. */
12141 max = 0;
12142 for (look = 1; look < c; look++)
12143 {
12144 if (tab[look].gt_entry.gt_g_value < val
12145 && (max == 0
12146 || (tab[look].gt_entry.gt_g_value
12147 > tab[max].gt_entry.gt_g_value)))
12148 max = look;
12149 }
12150 if (max != 0)
12151 tab[c].gt_entry.gt_bytes +=
12152 tab[max].gt_entry.gt_bytes;
12153
12154 ++c;
12155 }
12156
12157 last = int_gptab.gt_entry.gt_bytes;
12158 }
12159
12160 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12161 elf_link_input_bfd ignores this section. */
12162 input_section->flags &= ~SEC_HAS_CONTENTS;
12163 }
12164
12165 /* The table must be sorted by -G value. */
12166 if (c > 2)
12167 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12168
12169 /* Swap out the table. */
12170 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 12171 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
12172 if (ext_tab == NULL)
12173 {
12174 free (tab);
b34976b6 12175 return FALSE;
b49e97c9
TS
12176 }
12177
12178 for (j = 0; j < c; j++)
12179 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12180 free (tab);
12181
eea6121a 12182 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
12183 o->contents = (bfd_byte *) ext_tab;
12184
12185 /* Skip this section later on (I don't think this currently
12186 matters, but someday it might). */
8423293d 12187 o->map_head.link_order = NULL;
b49e97c9
TS
12188 }
12189 }
12190
12191 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 12192 if (!bfd_elf_final_link (abfd, info))
b34976b6 12193 return FALSE;
b49e97c9
TS
12194
12195 /* Now write out the computed sections. */
12196
9719ad41 12197 if (reginfo_sec != NULL)
b49e97c9
TS
12198 {
12199 Elf32_External_RegInfo ext;
12200
12201 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 12202 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 12203 return FALSE;
b49e97c9
TS
12204 }
12205
9719ad41 12206 if (mdebug_sec != NULL)
b49e97c9
TS
12207 {
12208 BFD_ASSERT (abfd->output_has_begun);
12209 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12210 swap, info,
12211 mdebug_sec->filepos))
b34976b6 12212 return FALSE;
b49e97c9
TS
12213
12214 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12215 }
12216
9719ad41 12217 if (gptab_data_sec != NULL)
b49e97c9
TS
12218 {
12219 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12220 gptab_data_sec->contents,
eea6121a 12221 0, gptab_data_sec->size))
b34976b6 12222 return FALSE;
b49e97c9
TS
12223 }
12224
9719ad41 12225 if (gptab_bss_sec != NULL)
b49e97c9
TS
12226 {
12227 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12228 gptab_bss_sec->contents,
eea6121a 12229 0, gptab_bss_sec->size))
b34976b6 12230 return FALSE;
b49e97c9
TS
12231 }
12232
12233 if (SGI_COMPAT (abfd))
12234 {
12235 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12236 if (rtproc_sec != NULL)
12237 {
12238 if (! bfd_set_section_contents (abfd, rtproc_sec,
12239 rtproc_sec->contents,
eea6121a 12240 0, rtproc_sec->size))
b34976b6 12241 return FALSE;
b49e97c9
TS
12242 }
12243 }
12244
b34976b6 12245 return TRUE;
b49e97c9
TS
12246}
12247\f
64543e1a
RS
12248/* Structure for saying that BFD machine EXTENSION extends BASE. */
12249
12250struct mips_mach_extension {
12251 unsigned long extension, base;
12252};
12253
12254
12255/* An array describing how BFD machines relate to one another. The entries
12256 are ordered topologically with MIPS I extensions listed last. */
12257
12258static const struct mips_mach_extension mips_mach_extensions[] = {
6f179bd0
AN
12259 /* MIPS64r2 extensions. */
12260 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12261
64543e1a 12262 /* MIPS64 extensions. */
5f74bc13 12263 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 12264 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 12265 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
64543e1a
RS
12266
12267 /* MIPS V extensions. */
12268 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12269
12270 /* R10000 extensions. */
12271 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
12272 { bfd_mach_mips14000, bfd_mach_mips10000 },
12273 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
12274
12275 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12276 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12277 better to allow vr5400 and vr5500 code to be merged anyway, since
12278 many libraries will just use the core ISA. Perhaps we could add
12279 some sort of ASE flag if this ever proves a problem. */
12280 { bfd_mach_mips5500, bfd_mach_mips5400 },
12281 { bfd_mach_mips5400, bfd_mach_mips5000 },
12282
12283 /* MIPS IV extensions. */
12284 { bfd_mach_mips5, bfd_mach_mips8000 },
12285 { bfd_mach_mips10000, bfd_mach_mips8000 },
12286 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 12287 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 12288 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
12289
12290 /* VR4100 extensions. */
12291 { bfd_mach_mips4120, bfd_mach_mips4100 },
12292 { bfd_mach_mips4111, bfd_mach_mips4100 },
12293
12294 /* MIPS III extensions. */
350cc38d
MS
12295 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12296 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
12297 { bfd_mach_mips8000, bfd_mach_mips4000 },
12298 { bfd_mach_mips4650, bfd_mach_mips4000 },
12299 { bfd_mach_mips4600, bfd_mach_mips4000 },
12300 { bfd_mach_mips4400, bfd_mach_mips4000 },
12301 { bfd_mach_mips4300, bfd_mach_mips4000 },
12302 { bfd_mach_mips4100, bfd_mach_mips4000 },
12303 { bfd_mach_mips4010, bfd_mach_mips4000 },
12304
12305 /* MIPS32 extensions. */
12306 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12307
12308 /* MIPS II extensions. */
12309 { bfd_mach_mips4000, bfd_mach_mips6000 },
12310 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12311
12312 /* MIPS I extensions. */
12313 { bfd_mach_mips6000, bfd_mach_mips3000 },
12314 { bfd_mach_mips3900, bfd_mach_mips3000 }
12315};
12316
12317
12318/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12319
12320static bfd_boolean
9719ad41 12321mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
12322{
12323 size_t i;
12324
c5211a54
RS
12325 if (extension == base)
12326 return TRUE;
12327
12328 if (base == bfd_mach_mipsisa32
12329 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12330 return TRUE;
12331
12332 if (base == bfd_mach_mipsisa32r2
12333 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12334 return TRUE;
12335
12336 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 12337 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
12338 {
12339 extension = mips_mach_extensions[i].base;
12340 if (extension == base)
12341 return TRUE;
12342 }
64543e1a 12343
c5211a54 12344 return FALSE;
64543e1a
RS
12345}
12346
12347
12348/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 12349
b34976b6 12350static bfd_boolean
9719ad41 12351mips_32bit_flags_p (flagword flags)
00707a0e 12352{
64543e1a
RS
12353 return ((flags & EF_MIPS_32BITMODE) != 0
12354 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12355 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12356 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12357 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12358 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12359 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
12360}
12361
64543e1a 12362
2cf19d5c
JM
12363/* Merge object attributes from IBFD into OBFD. Raise an error if
12364 there are conflicting attributes. */
12365static bfd_boolean
12366mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12367{
12368 obj_attribute *in_attr;
12369 obj_attribute *out_attr;
12370
12371 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12372 {
12373 /* This is the first object. Copy the attributes. */
12374 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12375
12376 /* Use the Tag_null value to indicate the attributes have been
12377 initialized. */
12378 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12379
12380 return TRUE;
12381 }
12382
12383 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12384 non-conflicting ones. */
12385 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12386 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12387 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12388 {
12389 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12390 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12391 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12392 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12393 ;
42554f6a 12394 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12395 _bfd_error_handler
12396 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12397 in_attr[Tag_GNU_MIPS_ABI_FP].i);
42554f6a 12398 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12399 _bfd_error_handler
12400 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12401 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12402 else
12403 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12404 {
12405 case 1:
12406 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12407 {
12408 case 2:
12409 _bfd_error_handler
12410 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12411 obfd, ibfd);
51a0dd31 12412 break;
2cf19d5c
JM
12413
12414 case 3:
12415 _bfd_error_handler
12416 (_("Warning: %B uses hard float, %B uses soft float"),
12417 obfd, ibfd);
12418 break;
12419
42554f6a
TS
12420 case 4:
12421 _bfd_error_handler
12422 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12423 obfd, ibfd);
12424 break;
12425
2cf19d5c
JM
12426 default:
12427 abort ();
12428 }
12429 break;
12430
12431 case 2:
12432 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12433 {
12434 case 1:
12435 _bfd_error_handler
12436 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12437 ibfd, obfd);
51a0dd31 12438 break;
2cf19d5c
JM
12439
12440 case 3:
12441 _bfd_error_handler
12442 (_("Warning: %B uses hard float, %B uses soft float"),
12443 obfd, ibfd);
12444 break;
12445
42554f6a
TS
12446 case 4:
12447 _bfd_error_handler
12448 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12449 obfd, ibfd);
12450 break;
12451
2cf19d5c
JM
12452 default:
12453 abort ();
12454 }
12455 break;
12456
12457 case 3:
12458 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12459 {
12460 case 1:
12461 case 2:
42554f6a 12462 case 4:
2cf19d5c
JM
12463 _bfd_error_handler
12464 (_("Warning: %B uses hard float, %B uses soft float"),
12465 ibfd, obfd);
12466 break;
12467
12468 default:
12469 abort ();
12470 }
12471 break;
12472
42554f6a
TS
12473 case 4:
12474 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12475 {
12476 case 1:
12477 _bfd_error_handler
12478 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12479 ibfd, obfd);
12480 break;
12481
12482 case 2:
12483 _bfd_error_handler
12484 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12485 ibfd, obfd);
12486 break;
12487
12488 case 3:
12489 _bfd_error_handler
12490 (_("Warning: %B uses hard float, %B uses soft float"),
12491 obfd, ibfd);
12492 break;
12493
12494 default:
12495 abort ();
12496 }
12497 break;
12498
2cf19d5c
JM
12499 default:
12500 abort ();
12501 }
12502 }
12503
12504 /* Merge Tag_compatibility attributes and any common GNU ones. */
12505 _bfd_elf_merge_object_attributes (ibfd, obfd);
12506
12507 return TRUE;
12508}
12509
b49e97c9
TS
12510/* Merge backend specific data from an object file to the output
12511 object file when linking. */
12512
b34976b6 12513bfd_boolean
9719ad41 12514_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
12515{
12516 flagword old_flags;
12517 flagword new_flags;
b34976b6
AM
12518 bfd_boolean ok;
12519 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
12520 asection *sec;
12521
12522 /* Check if we have the same endianess */
82e51918 12523 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
12524 {
12525 (*_bfd_error_handler)
d003868e
AM
12526 (_("%B: endianness incompatible with that of the selected emulation"),
12527 ibfd);
aa701218
AO
12528 return FALSE;
12529 }
b49e97c9 12530
d5eaccd7 12531 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 12532 return TRUE;
b49e97c9 12533
aa701218
AO
12534 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12535 {
12536 (*_bfd_error_handler)
d003868e
AM
12537 (_("%B: ABI is incompatible with that of the selected emulation"),
12538 ibfd);
aa701218
AO
12539 return FALSE;
12540 }
12541
2cf19d5c
JM
12542 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12543 return FALSE;
12544
b49e97c9
TS
12545 new_flags = elf_elfheader (ibfd)->e_flags;
12546 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12547 old_flags = elf_elfheader (obfd)->e_flags;
12548
12549 if (! elf_flags_init (obfd))
12550 {
b34976b6 12551 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
12552 elf_elfheader (obfd)->e_flags = new_flags;
12553 elf_elfheader (obfd)->e_ident[EI_CLASS]
12554 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12555
12556 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861
TS
12557 && (bfd_get_arch_info (obfd)->the_default
12558 || mips_mach_extends_p (bfd_get_mach (obfd),
12559 bfd_get_mach (ibfd))))
b49e97c9
TS
12560 {
12561 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12562 bfd_get_mach (ibfd)))
b34976b6 12563 return FALSE;
b49e97c9
TS
12564 }
12565
b34976b6 12566 return TRUE;
b49e97c9
TS
12567 }
12568
12569 /* Check flag compatibility. */
12570
12571 new_flags &= ~EF_MIPS_NOREORDER;
12572 old_flags &= ~EF_MIPS_NOREORDER;
12573
f4416af6
AO
12574 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12575 doesn't seem to matter. */
12576 new_flags &= ~EF_MIPS_XGOT;
12577 old_flags &= ~EF_MIPS_XGOT;
12578
98a8deaf
RS
12579 /* MIPSpro generates ucode info in n64 objects. Again, we should
12580 just be able to ignore this. */
12581 new_flags &= ~EF_MIPS_UCODE;
12582 old_flags &= ~EF_MIPS_UCODE;
12583
861fb55a
DJ
12584 /* DSOs should only be linked with CPIC code. */
12585 if ((ibfd->flags & DYNAMIC) != 0)
12586 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 12587
b49e97c9 12588 if (new_flags == old_flags)
b34976b6 12589 return TRUE;
b49e97c9
TS
12590
12591 /* Check to see if the input BFD actually contains any sections.
12592 If not, its flags may not have been initialised either, but it cannot
12593 actually cause any incompatibility. */
12594 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12595 {
12596 /* Ignore synthetic sections and empty .text, .data and .bss sections
12597 which are automatically generated by gas. */
12598 if (strcmp (sec->name, ".reginfo")
12599 && strcmp (sec->name, ".mdebug")
eea6121a 12600 && (sec->size != 0
d13d89fa
NS
12601 || (strcmp (sec->name, ".text")
12602 && strcmp (sec->name, ".data")
12603 && strcmp (sec->name, ".bss"))))
b49e97c9 12604 {
b34976b6 12605 null_input_bfd = FALSE;
b49e97c9
TS
12606 break;
12607 }
12608 }
12609 if (null_input_bfd)
b34976b6 12610 return TRUE;
b49e97c9 12611
b34976b6 12612 ok = TRUE;
b49e97c9 12613
143d77c5
EC
12614 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12615 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 12616 {
b49e97c9 12617 (*_bfd_error_handler)
861fb55a 12618 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 12619 ibfd);
143d77c5 12620 ok = TRUE;
b49e97c9
TS
12621 }
12622
143d77c5
EC
12623 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12624 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12625 if (! (new_flags & EF_MIPS_PIC))
12626 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12627
12628 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12629 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 12630
64543e1a
RS
12631 /* Compare the ISAs. */
12632 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 12633 {
64543e1a 12634 (*_bfd_error_handler)
d003868e
AM
12635 (_("%B: linking 32-bit code with 64-bit code"),
12636 ibfd);
64543e1a
RS
12637 ok = FALSE;
12638 }
12639 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12640 {
12641 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12642 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 12643 {
64543e1a
RS
12644 /* Copy the architecture info from IBFD to OBFD. Also copy
12645 the 32-bit flag (if set) so that we continue to recognise
12646 OBFD as a 32-bit binary. */
12647 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12648 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12649 elf_elfheader (obfd)->e_flags
12650 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12651
12652 /* Copy across the ABI flags if OBFD doesn't use them
12653 and if that was what caused us to treat IBFD as 32-bit. */
12654 if ((old_flags & EF_MIPS_ABI) == 0
12655 && mips_32bit_flags_p (new_flags)
12656 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12657 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
12658 }
12659 else
12660 {
64543e1a 12661 /* The ISAs aren't compatible. */
b49e97c9 12662 (*_bfd_error_handler)
d003868e
AM
12663 (_("%B: linking %s module with previous %s modules"),
12664 ibfd,
64543e1a
RS
12665 bfd_printable_name (ibfd),
12666 bfd_printable_name (obfd));
b34976b6 12667 ok = FALSE;
b49e97c9 12668 }
b49e97c9
TS
12669 }
12670
64543e1a
RS
12671 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12672 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12673
12674 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
12675 does set EI_CLASS differently from any 32-bit ABI. */
12676 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12677 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12678 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12679 {
12680 /* Only error if both are set (to different values). */
12681 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12682 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12683 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12684 {
12685 (*_bfd_error_handler)
d003868e
AM
12686 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12687 ibfd,
b49e97c9
TS
12688 elf_mips_abi_name (ibfd),
12689 elf_mips_abi_name (obfd));
b34976b6 12690 ok = FALSE;
b49e97c9
TS
12691 }
12692 new_flags &= ~EF_MIPS_ABI;
12693 old_flags &= ~EF_MIPS_ABI;
12694 }
12695
fb39dac1
RS
12696 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12697 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12698 {
12699 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12700
12701 new_flags &= ~ EF_MIPS_ARCH_ASE;
12702 old_flags &= ~ EF_MIPS_ARCH_ASE;
12703 }
12704
b49e97c9
TS
12705 /* Warn about any other mismatches */
12706 if (new_flags != old_flags)
12707 {
12708 (*_bfd_error_handler)
d003868e
AM
12709 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12710 ibfd, (unsigned long) new_flags,
b49e97c9 12711 (unsigned long) old_flags);
b34976b6 12712 ok = FALSE;
b49e97c9
TS
12713 }
12714
12715 if (! ok)
12716 {
12717 bfd_set_error (bfd_error_bad_value);
b34976b6 12718 return FALSE;
b49e97c9
TS
12719 }
12720
b34976b6 12721 return TRUE;
b49e97c9
TS
12722}
12723
12724/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12725
b34976b6 12726bfd_boolean
9719ad41 12727_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
12728{
12729 BFD_ASSERT (!elf_flags_init (abfd)
12730 || elf_elfheader (abfd)->e_flags == flags);
12731
12732 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
12733 elf_flags_init (abfd) = TRUE;
12734 return TRUE;
b49e97c9
TS
12735}
12736
ad9563d6
CM
12737char *
12738_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12739{
12740 switch (dtag)
12741 {
12742 default: return "";
12743 case DT_MIPS_RLD_VERSION:
12744 return "MIPS_RLD_VERSION";
12745 case DT_MIPS_TIME_STAMP:
12746 return "MIPS_TIME_STAMP";
12747 case DT_MIPS_ICHECKSUM:
12748 return "MIPS_ICHECKSUM";
12749 case DT_MIPS_IVERSION:
12750 return "MIPS_IVERSION";
12751 case DT_MIPS_FLAGS:
12752 return "MIPS_FLAGS";
12753 case DT_MIPS_BASE_ADDRESS:
12754 return "MIPS_BASE_ADDRESS";
12755 case DT_MIPS_MSYM:
12756 return "MIPS_MSYM";
12757 case DT_MIPS_CONFLICT:
12758 return "MIPS_CONFLICT";
12759 case DT_MIPS_LIBLIST:
12760 return "MIPS_LIBLIST";
12761 case DT_MIPS_LOCAL_GOTNO:
12762 return "MIPS_LOCAL_GOTNO";
12763 case DT_MIPS_CONFLICTNO:
12764 return "MIPS_CONFLICTNO";
12765 case DT_MIPS_LIBLISTNO:
12766 return "MIPS_LIBLISTNO";
12767 case DT_MIPS_SYMTABNO:
12768 return "MIPS_SYMTABNO";
12769 case DT_MIPS_UNREFEXTNO:
12770 return "MIPS_UNREFEXTNO";
12771 case DT_MIPS_GOTSYM:
12772 return "MIPS_GOTSYM";
12773 case DT_MIPS_HIPAGENO:
12774 return "MIPS_HIPAGENO";
12775 case DT_MIPS_RLD_MAP:
12776 return "MIPS_RLD_MAP";
12777 case DT_MIPS_DELTA_CLASS:
12778 return "MIPS_DELTA_CLASS";
12779 case DT_MIPS_DELTA_CLASS_NO:
12780 return "MIPS_DELTA_CLASS_NO";
12781 case DT_MIPS_DELTA_INSTANCE:
12782 return "MIPS_DELTA_INSTANCE";
12783 case DT_MIPS_DELTA_INSTANCE_NO:
12784 return "MIPS_DELTA_INSTANCE_NO";
12785 case DT_MIPS_DELTA_RELOC:
12786 return "MIPS_DELTA_RELOC";
12787 case DT_MIPS_DELTA_RELOC_NO:
12788 return "MIPS_DELTA_RELOC_NO";
12789 case DT_MIPS_DELTA_SYM:
12790 return "MIPS_DELTA_SYM";
12791 case DT_MIPS_DELTA_SYM_NO:
12792 return "MIPS_DELTA_SYM_NO";
12793 case DT_MIPS_DELTA_CLASSSYM:
12794 return "MIPS_DELTA_CLASSSYM";
12795 case DT_MIPS_DELTA_CLASSSYM_NO:
12796 return "MIPS_DELTA_CLASSSYM_NO";
12797 case DT_MIPS_CXX_FLAGS:
12798 return "MIPS_CXX_FLAGS";
12799 case DT_MIPS_PIXIE_INIT:
12800 return "MIPS_PIXIE_INIT";
12801 case DT_MIPS_SYMBOL_LIB:
12802 return "MIPS_SYMBOL_LIB";
12803 case DT_MIPS_LOCALPAGE_GOTIDX:
12804 return "MIPS_LOCALPAGE_GOTIDX";
12805 case DT_MIPS_LOCAL_GOTIDX:
12806 return "MIPS_LOCAL_GOTIDX";
12807 case DT_MIPS_HIDDEN_GOTIDX:
12808 return "MIPS_HIDDEN_GOTIDX";
12809 case DT_MIPS_PROTECTED_GOTIDX:
12810 return "MIPS_PROTECTED_GOT_IDX";
12811 case DT_MIPS_OPTIONS:
12812 return "MIPS_OPTIONS";
12813 case DT_MIPS_INTERFACE:
12814 return "MIPS_INTERFACE";
12815 case DT_MIPS_DYNSTR_ALIGN:
12816 return "DT_MIPS_DYNSTR_ALIGN";
12817 case DT_MIPS_INTERFACE_SIZE:
12818 return "DT_MIPS_INTERFACE_SIZE";
12819 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12820 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12821 case DT_MIPS_PERF_SUFFIX:
12822 return "DT_MIPS_PERF_SUFFIX";
12823 case DT_MIPS_COMPACT_SIZE:
12824 return "DT_MIPS_COMPACT_SIZE";
12825 case DT_MIPS_GP_VALUE:
12826 return "DT_MIPS_GP_VALUE";
12827 case DT_MIPS_AUX_DYNAMIC:
12828 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
12829 case DT_MIPS_PLTGOT:
12830 return "DT_MIPS_PLTGOT";
12831 case DT_MIPS_RWPLT:
12832 return "DT_MIPS_RWPLT";
ad9563d6
CM
12833 }
12834}
12835
b34976b6 12836bfd_boolean
9719ad41 12837_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 12838{
9719ad41 12839 FILE *file = ptr;
b49e97c9
TS
12840
12841 BFD_ASSERT (abfd != NULL && ptr != NULL);
12842
12843 /* Print normal ELF private data. */
12844 _bfd_elf_print_private_bfd_data (abfd, ptr);
12845
12846 /* xgettext:c-format */
12847 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12848
12849 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12850 fprintf (file, _(" [abi=O32]"));
12851 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12852 fprintf (file, _(" [abi=O64]"));
12853 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12854 fprintf (file, _(" [abi=EABI32]"));
12855 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12856 fprintf (file, _(" [abi=EABI64]"));
12857 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12858 fprintf (file, _(" [abi unknown]"));
12859 else if (ABI_N32_P (abfd))
12860 fprintf (file, _(" [abi=N32]"));
12861 else if (ABI_64_P (abfd))
12862 fprintf (file, _(" [abi=64]"));
12863 else
12864 fprintf (file, _(" [no abi set]"));
12865
12866 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 12867 fprintf (file, " [mips1]");
b49e97c9 12868 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 12869 fprintf (file, " [mips2]");
b49e97c9 12870 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 12871 fprintf (file, " [mips3]");
b49e97c9 12872 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 12873 fprintf (file, " [mips4]");
b49e97c9 12874 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 12875 fprintf (file, " [mips5]");
b49e97c9 12876 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 12877 fprintf (file, " [mips32]");
b49e97c9 12878 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 12879 fprintf (file, " [mips64]");
af7ee8bf 12880 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 12881 fprintf (file, " [mips32r2]");
5f74bc13 12882 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 12883 fprintf (file, " [mips64r2]");
b49e97c9
TS
12884 else
12885 fprintf (file, _(" [unknown ISA]"));
12886
40d32fc6 12887 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 12888 fprintf (file, " [mdmx]");
40d32fc6
CD
12889
12890 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 12891 fprintf (file, " [mips16]");
40d32fc6 12892
b49e97c9 12893 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 12894 fprintf (file, " [32bitmode]");
b49e97c9
TS
12895 else
12896 fprintf (file, _(" [not 32bitmode]"));
12897
c0e3f241 12898 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 12899 fprintf (file, " [noreorder]");
c0e3f241
CD
12900
12901 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 12902 fprintf (file, " [PIC]");
c0e3f241
CD
12903
12904 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 12905 fprintf (file, " [CPIC]");
c0e3f241
CD
12906
12907 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 12908 fprintf (file, " [XGOT]");
c0e3f241
CD
12909
12910 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 12911 fprintf (file, " [UCODE]");
c0e3f241 12912
b49e97c9
TS
12913 fputc ('\n', file);
12914
b34976b6 12915 return TRUE;
b49e97c9 12916}
2f89ff8d 12917
b35d266b 12918const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 12919{
0112cd26
NC
12920 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12921 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12922 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12923 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12924 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12925 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12926 { NULL, 0, 0, 0, 0 }
2f89ff8d 12927};
5e2b0d47 12928
8992f0d7
TS
12929/* Merge non visibility st_other attributes. Ensure that the
12930 STO_OPTIONAL flag is copied into h->other, even if this is not a
12931 definiton of the symbol. */
5e2b0d47
NC
12932void
12933_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12934 const Elf_Internal_Sym *isym,
12935 bfd_boolean definition,
12936 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12937{
8992f0d7
TS
12938 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12939 {
12940 unsigned char other;
12941
12942 other = (definition ? isym->st_other : h->other);
12943 other &= ~ELF_ST_VISIBILITY (-1);
12944 h->other = other | ELF_ST_VISIBILITY (h->other);
12945 }
12946
12947 if (!definition
5e2b0d47
NC
12948 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12949 h->other |= STO_OPTIONAL;
12950}
12ac1cf5
NC
12951
12952/* Decide whether an undefined symbol is special and can be ignored.
12953 This is the case for OPTIONAL symbols on IRIX. */
12954bfd_boolean
12955_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12956{
12957 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12958}
e0764319
NC
12959
12960bfd_boolean
12961_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12962{
12963 return (sym->st_shndx == SHN_COMMON
12964 || sym->st_shndx == SHN_MIPS_ACOMMON
12965 || sym->st_shndx == SHN_MIPS_SCOMMON);
12966}
861fb55a
DJ
12967
12968/* Return address for Ith PLT stub in section PLT, for relocation REL
12969 or (bfd_vma) -1 if it should not be included. */
12970
12971bfd_vma
12972_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12973 const arelent *rel ATTRIBUTE_UNUSED)
12974{
12975 return (plt->vma
12976 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12977 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12978}
12979
12980void
12981_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12982{
12983 struct mips_elf_link_hash_table *htab;
12984 Elf_Internal_Ehdr *i_ehdrp;
12985
12986 i_ehdrp = elf_elfheader (abfd);
12987 if (link_info)
12988 {
12989 htab = mips_elf_hash_table (link_info);
4dfe6ac6
NC
12990 BFD_ASSERT (htab != NULL);
12991
861fb55a
DJ
12992 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12993 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
12994 }
12995}
This page took 1.327658 seconds and 4 git commands to generate.