bfd/
[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
0a44bf69
RS
786/* The name of the dynamic relocation section. */
787#define MIPS_ELF_REL_DYN_NAME(INFO) \
788 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
789
b49e97c9
TS
790/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
791 from smaller values. Start with zero, widen, *then* decrement. */
792#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 793#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 794
51e38d68
RS
795/* The value to write into got[1] for SVR4 targets, to identify it is
796 a GNU object. The dynamic linker can then use got[1] to store the
797 module pointer. */
798#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
799 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
800
f4416af6 801/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
802#define ELF_MIPS_GP_OFFSET(INFO) \
803 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
804
805/* The maximum size of the GOT for it to be addressable using 16-bit
806 offsets from $gp. */
0a44bf69 807#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 808
6a691779 809/* Instructions which appear in a stub. */
3d6746ca
DD
810#define STUB_LW(abfd) \
811 ((ABI_64_P (abfd) \
812 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
813 : 0x8f998010)) /* lw t9,0x8010(gp) */
814#define STUB_MOVE(abfd) \
815 ((ABI_64_P (abfd) \
816 ? 0x03e0782d /* daddu t7,ra */ \
817 : 0x03e07821)) /* addu t7,ra */
818#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
819#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
820#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
821#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
822#define STUB_LI16S(abfd, VAL) \
823 ((ABI_64_P (abfd) \
824 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
825 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
826
5108fc1b
RS
827#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
828#define MIPS_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
829
830/* The name of the dynamic interpreter. This is put in the .interp
831 section. */
832
833#define ELF_DYNAMIC_INTERPRETER(abfd) \
834 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
835 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
836 : "/usr/lib/libc.so.1")
837
838#ifdef BFD64
ee6423ed
AO
839#define MNAME(bfd,pre,pos) \
840 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
841#define ELF_R_SYM(bfd, i) \
842 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
843#define ELF_R_TYPE(bfd, i) \
844 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
845#define ELF_R_INFO(bfd, s, t) \
846 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
847#else
ee6423ed 848#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
849#define ELF_R_SYM(bfd, i) \
850 (ELF32_R_SYM (i))
851#define ELF_R_TYPE(bfd, i) \
852 (ELF32_R_TYPE (i))
853#define ELF_R_INFO(bfd, s, t) \
854 (ELF32_R_INFO (s, t))
855#endif
856\f
857 /* The mips16 compiler uses a couple of special sections to handle
858 floating point arguments.
859
860 Section names that look like .mips16.fn.FNNAME contain stubs that
861 copy floating point arguments from the fp regs to the gp regs and
862 then jump to FNNAME. If any 32 bit function calls FNNAME, the
863 call should be redirected to the stub instead. If no 32 bit
864 function calls FNNAME, the stub should be discarded. We need to
865 consider any reference to the function, not just a call, because
866 if the address of the function is taken we will need the stub,
867 since the address might be passed to a 32 bit function.
868
869 Section names that look like .mips16.call.FNNAME contain stubs
870 that copy floating point arguments from the gp regs to the fp
871 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
872 then any 16 bit function that calls FNNAME should be redirected
873 to the stub instead. If FNNAME is not a 32 bit function, the
874 stub should be discarded.
875
876 .mips16.call.fp.FNNAME sections are similar, but contain stubs
877 which call FNNAME and then copy the return value from the fp regs
878 to the gp regs. These stubs store the return value in $18 while
879 calling FNNAME; any function which might call one of these stubs
880 must arrange to save $18 around the call. (This case is not
881 needed for 32 bit functions that call 16 bit functions, because
882 16 bit functions always return floating point values in both
883 $f0/$f1 and $2/$3.)
884
885 Note that in all cases FNNAME might be defined statically.
886 Therefore, FNNAME is not used literally. Instead, the relocation
887 information will indicate which symbol the section is for.
888
889 We record any stubs that we find in the symbol table. */
890
891#define FN_STUB ".mips16.fn."
892#define CALL_STUB ".mips16.call."
893#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
894
895#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
896#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
897#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 898\f
861fb55a 899/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
900static const bfd_vma mips_o32_exec_plt0_entry[] =
901{
861fb55a
DJ
902 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
903 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
904 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
905 0x031cc023, /* subu $24, $24, $28 */
906 0x03e07821, /* move $15, $31 */
907 0x0018c082, /* srl $24, $24, 2 */
908 0x0320f809, /* jalr $25 */
909 0x2718fffe /* subu $24, $24, 2 */
910};
911
912/* The format of the first PLT entry in an N32 executable. Different
913 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
914static const bfd_vma mips_n32_exec_plt0_entry[] =
915{
861fb55a
DJ
916 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
917 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
918 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
919 0x030ec023, /* subu $24, $24, $14 */
920 0x03e07821, /* move $15, $31 */
921 0x0018c082, /* srl $24, $24, 2 */
922 0x0320f809, /* jalr $25 */
923 0x2718fffe /* subu $24, $24, 2 */
924};
925
926/* The format of the first PLT entry in an N64 executable. Different
927 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
928static const bfd_vma mips_n64_exec_plt0_entry[] =
929{
861fb55a
DJ
930 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
931 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
932 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
933 0x030ec023, /* subu $24, $24, $14 */
934 0x03e07821, /* move $15, $31 */
935 0x0018c0c2, /* srl $24, $24, 3 */
936 0x0320f809, /* jalr $25 */
937 0x2718fffe /* subu $24, $24, 2 */
938};
939
940/* The format of subsequent PLT entries. */
6d30f5b2
NC
941static const bfd_vma mips_exec_plt_entry[] =
942{
861fb55a
DJ
943 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
944 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
945 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
946 0x03200008 /* jr $25 */
947};
948
0a44bf69 949/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
950static const bfd_vma mips_vxworks_exec_plt0_entry[] =
951{
0a44bf69
RS
952 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
953 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
954 0x8f390008, /* lw t9, 8(t9) */
955 0x00000000, /* nop */
956 0x03200008, /* jr t9 */
957 0x00000000 /* nop */
958};
959
960/* The format of subsequent PLT entries. */
6d30f5b2
NC
961static const bfd_vma mips_vxworks_exec_plt_entry[] =
962{
0a44bf69
RS
963 0x10000000, /* b .PLT_resolver */
964 0x24180000, /* li t8, <pltindex> */
965 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
966 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
967 0x8f390000, /* lw t9, 0(t9) */
968 0x00000000, /* nop */
969 0x03200008, /* jr t9 */
970 0x00000000 /* nop */
971};
972
973/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
974static const bfd_vma mips_vxworks_shared_plt0_entry[] =
975{
0a44bf69
RS
976 0x8f990008, /* lw t9, 8(gp) */
977 0x00000000, /* nop */
978 0x03200008, /* jr t9 */
979 0x00000000, /* nop */
980 0x00000000, /* nop */
981 0x00000000 /* nop */
982};
983
984/* The format of subsequent PLT entries. */
6d30f5b2
NC
985static const bfd_vma mips_vxworks_shared_plt_entry[] =
986{
0a44bf69
RS
987 0x10000000, /* b .PLT_resolver */
988 0x24180000 /* li t8, <pltindex> */
989};
990\f
b49e97c9
TS
991/* Look up an entry in a MIPS ELF linker hash table. */
992
993#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
994 ((struct mips_elf_link_hash_entry *) \
995 elf_link_hash_lookup (&(table)->root, (string), (create), \
996 (copy), (follow)))
997
998/* Traverse a MIPS ELF linker hash table. */
999
1000#define mips_elf_link_hash_traverse(table, func, info) \
1001 (elf_link_hash_traverse \
1002 (&(table)->root, \
9719ad41 1003 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1004 (info)))
1005
0f20cc35
DJ
1006/* Find the base offsets for thread-local storage in this object,
1007 for GD/LD and IE/LE respectively. */
1008
1009#define TP_OFFSET 0x7000
1010#define DTP_OFFSET 0x8000
1011
1012static bfd_vma
1013dtprel_base (struct bfd_link_info *info)
1014{
1015 /* If tls_sec is NULL, we should have signalled an error already. */
1016 if (elf_hash_table (info)->tls_sec == NULL)
1017 return 0;
1018 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1019}
1020
1021static bfd_vma
1022tprel_base (struct bfd_link_info *info)
1023{
1024 /* If tls_sec is NULL, we should have signalled an error already. */
1025 if (elf_hash_table (info)->tls_sec == NULL)
1026 return 0;
1027 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1028}
1029
b49e97c9
TS
1030/* Create an entry in a MIPS ELF linker hash table. */
1031
1032static struct bfd_hash_entry *
9719ad41
RS
1033mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1034 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1035{
1036 struct mips_elf_link_hash_entry *ret =
1037 (struct mips_elf_link_hash_entry *) entry;
1038
1039 /* Allocate the structure if it has not already been allocated by a
1040 subclass. */
9719ad41
RS
1041 if (ret == NULL)
1042 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1043 if (ret == NULL)
b49e97c9
TS
1044 return (struct bfd_hash_entry *) ret;
1045
1046 /* Call the allocation method of the superclass. */
1047 ret = ((struct mips_elf_link_hash_entry *)
1048 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1049 table, string));
9719ad41 1050 if (ret != NULL)
b49e97c9
TS
1051 {
1052 /* Set local fields. */
1053 memset (&ret->esym, 0, sizeof (EXTR));
1054 /* We use -2 as a marker to indicate that the information has
1055 not been set. -1 means there is no associated ifd. */
1056 ret->esym.ifd = -2;
861fb55a 1057 ret->la25_stub = 0;
b49e97c9 1058 ret->possibly_dynamic_relocs = 0;
b49e97c9 1059 ret->fn_stub = NULL;
b49e97c9
TS
1060 ret->call_stub = NULL;
1061 ret->call_fp_stub = NULL;
71782a75 1062 ret->tls_type = GOT_NORMAL;
634835ae 1063 ret->global_got_area = GGA_NONE;
6ccf4795 1064 ret->got_only_for_calls = TRUE;
71782a75 1065 ret->readonly_reloc = FALSE;
861fb55a 1066 ret->has_static_relocs = FALSE;
71782a75
RS
1067 ret->no_fn_stub = FALSE;
1068 ret->need_fn_stub = FALSE;
861fb55a 1069 ret->has_nonpic_branches = FALSE;
33bb52fb 1070 ret->needs_lazy_stub = FALSE;
b49e97c9
TS
1071 }
1072
1073 return (struct bfd_hash_entry *) ret;
1074}
f0abc2a1
AM
1075
1076bfd_boolean
9719ad41 1077_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1078{
f592407e
AM
1079 if (!sec->used_by_bfd)
1080 {
1081 struct _mips_elf_section_data *sdata;
1082 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1083
f592407e
AM
1084 sdata = bfd_zalloc (abfd, amt);
1085 if (sdata == NULL)
1086 return FALSE;
1087 sec->used_by_bfd = sdata;
1088 }
f0abc2a1
AM
1089
1090 return _bfd_elf_new_section_hook (abfd, sec);
1091}
b49e97c9
TS
1092\f
1093/* Read ECOFF debugging information from a .mdebug section into a
1094 ecoff_debug_info structure. */
1095
b34976b6 1096bfd_boolean
9719ad41
RS
1097_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1098 struct ecoff_debug_info *debug)
b49e97c9
TS
1099{
1100 HDRR *symhdr;
1101 const struct ecoff_debug_swap *swap;
9719ad41 1102 char *ext_hdr;
b49e97c9
TS
1103
1104 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1105 memset (debug, 0, sizeof (*debug));
1106
9719ad41 1107 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1108 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1109 goto error_return;
1110
9719ad41 1111 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1112 swap->external_hdr_size))
b49e97c9
TS
1113 goto error_return;
1114
1115 symhdr = &debug->symbolic_header;
1116 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1117
1118 /* The symbolic header contains absolute file offsets and sizes to
1119 read. */
1120#define READ(ptr, offset, count, size, type) \
1121 if (symhdr->count == 0) \
1122 debug->ptr = NULL; \
1123 else \
1124 { \
1125 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1126 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1127 if (debug->ptr == NULL) \
1128 goto error_return; \
9719ad41 1129 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1130 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1131 goto error_return; \
1132 }
1133
1134 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1135 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1136 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1137 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1138 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1139 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1140 union aux_ext *);
1141 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1142 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1143 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1144 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1145 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1146#undef READ
1147
1148 debug->fdr = NULL;
b49e97c9 1149
b34976b6 1150 return TRUE;
b49e97c9
TS
1151
1152 error_return:
1153 if (ext_hdr != NULL)
1154 free (ext_hdr);
1155 if (debug->line != NULL)
1156 free (debug->line);
1157 if (debug->external_dnr != NULL)
1158 free (debug->external_dnr);
1159 if (debug->external_pdr != NULL)
1160 free (debug->external_pdr);
1161 if (debug->external_sym != NULL)
1162 free (debug->external_sym);
1163 if (debug->external_opt != NULL)
1164 free (debug->external_opt);
1165 if (debug->external_aux != NULL)
1166 free (debug->external_aux);
1167 if (debug->ss != NULL)
1168 free (debug->ss);
1169 if (debug->ssext != NULL)
1170 free (debug->ssext);
1171 if (debug->external_fdr != NULL)
1172 free (debug->external_fdr);
1173 if (debug->external_rfd != NULL)
1174 free (debug->external_rfd);
1175 if (debug->external_ext != NULL)
1176 free (debug->external_ext);
b34976b6 1177 return FALSE;
b49e97c9
TS
1178}
1179\f
1180/* Swap RPDR (runtime procedure table entry) for output. */
1181
1182static void
9719ad41 1183ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1184{
1185 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1186 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1187 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1188 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1189 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1190 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1191
1192 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1193 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1194
1195 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1196}
1197
1198/* Create a runtime procedure table from the .mdebug section. */
1199
b34976b6 1200static bfd_boolean
9719ad41
RS
1201mips_elf_create_procedure_table (void *handle, bfd *abfd,
1202 struct bfd_link_info *info, asection *s,
1203 struct ecoff_debug_info *debug)
b49e97c9
TS
1204{
1205 const struct ecoff_debug_swap *swap;
1206 HDRR *hdr = &debug->symbolic_header;
1207 RPDR *rpdr, *rp;
1208 struct rpdr_ext *erp;
9719ad41 1209 void *rtproc;
b49e97c9
TS
1210 struct pdr_ext *epdr;
1211 struct sym_ext *esym;
1212 char *ss, **sv;
1213 char *str;
1214 bfd_size_type size;
1215 bfd_size_type count;
1216 unsigned long sindex;
1217 unsigned long i;
1218 PDR pdr;
1219 SYMR sym;
1220 const char *no_name_func = _("static procedure (no name)");
1221
1222 epdr = NULL;
1223 rpdr = NULL;
1224 esym = NULL;
1225 ss = NULL;
1226 sv = NULL;
1227
1228 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1229
1230 sindex = strlen (no_name_func) + 1;
1231 count = hdr->ipdMax;
1232 if (count > 0)
1233 {
1234 size = swap->external_pdr_size;
1235
9719ad41 1236 epdr = bfd_malloc (size * count);
b49e97c9
TS
1237 if (epdr == NULL)
1238 goto error_return;
1239
9719ad41 1240 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1241 goto error_return;
1242
1243 size = sizeof (RPDR);
9719ad41 1244 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1245 if (rpdr == NULL)
1246 goto error_return;
1247
1248 size = sizeof (char *);
9719ad41 1249 sv = bfd_malloc (size * count);
b49e97c9
TS
1250 if (sv == NULL)
1251 goto error_return;
1252
1253 count = hdr->isymMax;
1254 size = swap->external_sym_size;
9719ad41 1255 esym = bfd_malloc (size * count);
b49e97c9
TS
1256 if (esym == NULL)
1257 goto error_return;
1258
9719ad41 1259 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1260 goto error_return;
1261
1262 count = hdr->issMax;
9719ad41 1263 ss = bfd_malloc (count);
b49e97c9
TS
1264 if (ss == NULL)
1265 goto error_return;
f075ee0c 1266 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1267 goto error_return;
1268
1269 count = hdr->ipdMax;
1270 for (i = 0; i < (unsigned long) count; i++, rp++)
1271 {
9719ad41
RS
1272 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1273 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1274 rp->adr = sym.value;
1275 rp->regmask = pdr.regmask;
1276 rp->regoffset = pdr.regoffset;
1277 rp->fregmask = pdr.fregmask;
1278 rp->fregoffset = pdr.fregoffset;
1279 rp->frameoffset = pdr.frameoffset;
1280 rp->framereg = pdr.framereg;
1281 rp->pcreg = pdr.pcreg;
1282 rp->irpss = sindex;
1283 sv[i] = ss + sym.iss;
1284 sindex += strlen (sv[i]) + 1;
1285 }
1286 }
1287
1288 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1289 size = BFD_ALIGN (size, 16);
9719ad41 1290 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1291 if (rtproc == NULL)
1292 {
1293 mips_elf_hash_table (info)->procedure_count = 0;
1294 goto error_return;
1295 }
1296
1297 mips_elf_hash_table (info)->procedure_count = count + 2;
1298
9719ad41 1299 erp = rtproc;
b49e97c9
TS
1300 memset (erp, 0, sizeof (struct rpdr_ext));
1301 erp++;
1302 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1303 strcpy (str, no_name_func);
1304 str += strlen (no_name_func) + 1;
1305 for (i = 0; i < count; i++)
1306 {
1307 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1308 strcpy (str, sv[i]);
1309 str += strlen (sv[i]) + 1;
1310 }
1311 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1312
1313 /* Set the size and contents of .rtproc section. */
eea6121a 1314 s->size = size;
9719ad41 1315 s->contents = rtproc;
b49e97c9
TS
1316
1317 /* Skip this section later on (I don't think this currently
1318 matters, but someday it might). */
8423293d 1319 s->map_head.link_order = NULL;
b49e97c9
TS
1320
1321 if (epdr != NULL)
1322 free (epdr);
1323 if (rpdr != NULL)
1324 free (rpdr);
1325 if (esym != NULL)
1326 free (esym);
1327 if (ss != NULL)
1328 free (ss);
1329 if (sv != NULL)
1330 free (sv);
1331
b34976b6 1332 return TRUE;
b49e97c9
TS
1333
1334 error_return:
1335 if (epdr != NULL)
1336 free (epdr);
1337 if (rpdr != NULL)
1338 free (rpdr);
1339 if (esym != NULL)
1340 free (esym);
1341 if (ss != NULL)
1342 free (ss);
1343 if (sv != NULL)
1344 free (sv);
b34976b6 1345 return FALSE;
b49e97c9 1346}
738e5348 1347\f
861fb55a
DJ
1348/* We're going to create a stub for H. Create a symbol for the stub's
1349 value and size, to help make the disassembly easier to read. */
1350
1351static bfd_boolean
1352mips_elf_create_stub_symbol (struct bfd_link_info *info,
1353 struct mips_elf_link_hash_entry *h,
1354 const char *prefix, asection *s, bfd_vma value,
1355 bfd_vma size)
1356{
1357 struct bfd_link_hash_entry *bh;
1358 struct elf_link_hash_entry *elfh;
1359 const char *name;
1360
1361 /* Create a new symbol. */
1362 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1363 bh = NULL;
1364 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1365 BSF_LOCAL, s, value, NULL,
1366 TRUE, FALSE, &bh))
1367 return FALSE;
1368
1369 /* Make it a local function. */
1370 elfh = (struct elf_link_hash_entry *) bh;
1371 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1372 elfh->size = size;
1373 elfh->forced_local = 1;
1374 return TRUE;
1375}
1376
738e5348
RS
1377/* We're about to redefine H. Create a symbol to represent H's
1378 current value and size, to help make the disassembly easier
1379 to read. */
1380
1381static bfd_boolean
1382mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1383 struct mips_elf_link_hash_entry *h,
1384 const char *prefix)
1385{
1386 struct bfd_link_hash_entry *bh;
1387 struct elf_link_hash_entry *elfh;
1388 const char *name;
1389 asection *s;
1390 bfd_vma value;
1391
1392 /* Read the symbol's value. */
1393 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1394 || h->root.root.type == bfd_link_hash_defweak);
1395 s = h->root.root.u.def.section;
1396 value = h->root.root.u.def.value;
1397
1398 /* Create a new symbol. */
1399 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1400 bh = NULL;
1401 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1402 BSF_LOCAL, s, value, NULL,
1403 TRUE, FALSE, &bh))
1404 return FALSE;
1405
1406 /* Make it local and copy the other attributes from H. */
1407 elfh = (struct elf_link_hash_entry *) bh;
1408 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1409 elfh->other = h->root.other;
1410 elfh->size = h->root.size;
1411 elfh->forced_local = 1;
1412 return TRUE;
1413}
1414
1415/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1416 function rather than to a hard-float stub. */
1417
1418static bfd_boolean
1419section_allows_mips16_refs_p (asection *section)
1420{
1421 const char *name;
1422
1423 name = bfd_get_section_name (section->owner, section);
1424 return (FN_STUB_P (name)
1425 || CALL_STUB_P (name)
1426 || CALL_FP_STUB_P (name)
1427 || strcmp (name, ".pdr") == 0);
1428}
1429
1430/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1431 stub section of some kind. Return the R_SYMNDX of the target
1432 function, or 0 if we can't decide which function that is. */
1433
1434static unsigned long
502e814e
TT
1435mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1436 const Elf_Internal_Rela *relocs,
738e5348
RS
1437 const Elf_Internal_Rela *relend)
1438{
1439 const Elf_Internal_Rela *rel;
1440
1441 /* Trust the first R_MIPS_NONE relocation, if any. */
1442 for (rel = relocs; rel < relend; rel++)
1443 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1444 return ELF_R_SYM (sec->owner, rel->r_info);
1445
1446 /* Otherwise trust the first relocation, whatever its kind. This is
1447 the traditional behavior. */
1448 if (relocs < relend)
1449 return ELF_R_SYM (sec->owner, relocs->r_info);
1450
1451 return 0;
1452}
b49e97c9
TS
1453
1454/* Check the mips16 stubs for a particular symbol, and see if we can
1455 discard them. */
1456
861fb55a
DJ
1457static void
1458mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1459 struct mips_elf_link_hash_entry *h)
b49e97c9 1460{
738e5348
RS
1461 /* Dynamic symbols must use the standard call interface, in case other
1462 objects try to call them. */
1463 if (h->fn_stub != NULL
1464 && h->root.dynindx != -1)
1465 {
1466 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1467 h->need_fn_stub = TRUE;
1468 }
1469
b49e97c9
TS
1470 if (h->fn_stub != NULL
1471 && ! h->need_fn_stub)
1472 {
1473 /* We don't need the fn_stub; the only references to this symbol
1474 are 16 bit calls. Clobber the size to 0 to prevent it from
1475 being included in the link. */
eea6121a 1476 h->fn_stub->size = 0;
b49e97c9
TS
1477 h->fn_stub->flags &= ~SEC_RELOC;
1478 h->fn_stub->reloc_count = 0;
1479 h->fn_stub->flags |= SEC_EXCLUDE;
1480 }
1481
1482 if (h->call_stub != NULL
30c09090 1483 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1484 {
1485 /* We don't need the call_stub; this is a 16 bit function, so
1486 calls from other 16 bit functions are OK. Clobber the size
1487 to 0 to prevent it from being included in the link. */
eea6121a 1488 h->call_stub->size = 0;
b49e97c9
TS
1489 h->call_stub->flags &= ~SEC_RELOC;
1490 h->call_stub->reloc_count = 0;
1491 h->call_stub->flags |= SEC_EXCLUDE;
1492 }
1493
1494 if (h->call_fp_stub != NULL
30c09090 1495 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1496 {
1497 /* We don't need the call_stub; this is a 16 bit function, so
1498 calls from other 16 bit functions are OK. Clobber the size
1499 to 0 to prevent it from being included in the link. */
eea6121a 1500 h->call_fp_stub->size = 0;
b49e97c9
TS
1501 h->call_fp_stub->flags &= ~SEC_RELOC;
1502 h->call_fp_stub->reloc_count = 0;
1503 h->call_fp_stub->flags |= SEC_EXCLUDE;
1504 }
861fb55a
DJ
1505}
1506
1507/* Hashtable callbacks for mips_elf_la25_stubs. */
1508
1509static hashval_t
1510mips_elf_la25_stub_hash (const void *entry_)
1511{
1512 const struct mips_elf_la25_stub *entry;
1513
1514 entry = (struct mips_elf_la25_stub *) entry_;
1515 return entry->h->root.root.u.def.section->id
1516 + entry->h->root.root.u.def.value;
1517}
1518
1519static int
1520mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1521{
1522 const struct mips_elf_la25_stub *entry1, *entry2;
1523
1524 entry1 = (struct mips_elf_la25_stub *) entry1_;
1525 entry2 = (struct mips_elf_la25_stub *) entry2_;
1526 return ((entry1->h->root.root.u.def.section
1527 == entry2->h->root.root.u.def.section)
1528 && (entry1->h->root.root.u.def.value
1529 == entry2->h->root.root.u.def.value));
1530}
1531
1532/* Called by the linker to set up the la25 stub-creation code. FN is
1533 the linker's implementation of add_stub_function. Return true on
1534 success. */
1535
1536bfd_boolean
1537_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1538 asection *(*fn) (const char *, asection *,
1539 asection *))
1540{
1541 struct mips_elf_link_hash_table *htab;
1542
1543 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1544 if (htab == NULL)
1545 return FALSE;
1546
861fb55a
DJ
1547 htab->add_stub_section = fn;
1548 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1549 mips_elf_la25_stub_eq, NULL);
1550 if (htab->la25_stubs == NULL)
1551 return FALSE;
1552
1553 return TRUE;
1554}
1555
1556/* Return true if H is a locally-defined PIC function, in the sense
1557 that it might need $25 to be valid on entry. Note that MIPS16
1558 functions never need $25 to be valid on entry; they set up $gp
1559 using PC-relative instructions instead. */
1560
1561static bfd_boolean
1562mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1563{
1564 return ((h->root.root.type == bfd_link_hash_defined
1565 || h->root.root.type == bfd_link_hash_defweak)
1566 && h->root.def_regular
1567 && !bfd_is_abs_section (h->root.root.u.def.section)
1568 && !ELF_ST_IS_MIPS16 (h->root.other)
1569 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1570 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1571}
1572
1573/* STUB describes an la25 stub that we have decided to implement
1574 by inserting an LUI/ADDIU pair before the target function.
1575 Create the section and redirect the function symbol to it. */
1576
1577static bfd_boolean
1578mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1579 struct bfd_link_info *info)
1580{
1581 struct mips_elf_link_hash_table *htab;
1582 char *name;
1583 asection *s, *input_section;
1584 unsigned int align;
1585
1586 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1587 if (htab == NULL)
1588 return FALSE;
861fb55a
DJ
1589
1590 /* Create a unique name for the new section. */
1591 name = bfd_malloc (11 + sizeof (".text.stub."));
1592 if (name == NULL)
1593 return FALSE;
1594 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1595
1596 /* Create the section. */
1597 input_section = stub->h->root.root.u.def.section;
1598 s = htab->add_stub_section (name, input_section,
1599 input_section->output_section);
1600 if (s == NULL)
1601 return FALSE;
1602
1603 /* Make sure that any padding goes before the stub. */
1604 align = input_section->alignment_power;
1605 if (!bfd_set_section_alignment (s->owner, s, align))
1606 return FALSE;
1607 if (align > 3)
1608 s->size = (1 << align) - 8;
1609
1610 /* Create a symbol for the stub. */
1611 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1612 stub->stub_section = s;
1613 stub->offset = s->size;
1614
1615 /* Allocate room for it. */
1616 s->size += 8;
1617 return TRUE;
1618}
1619
1620/* STUB describes an la25 stub that we have decided to implement
1621 with a separate trampoline. Allocate room for it and redirect
1622 the function symbol to it. */
1623
1624static bfd_boolean
1625mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1626 struct bfd_link_info *info)
1627{
1628 struct mips_elf_link_hash_table *htab;
1629 asection *s;
1630
1631 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1632 if (htab == NULL)
1633 return FALSE;
861fb55a
DJ
1634
1635 /* Create a trampoline section, if we haven't already. */
1636 s = htab->strampoline;
1637 if (s == NULL)
1638 {
1639 asection *input_section = stub->h->root.root.u.def.section;
1640 s = htab->add_stub_section (".text", NULL,
1641 input_section->output_section);
1642 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1643 return FALSE;
1644 htab->strampoline = s;
1645 }
1646
1647 /* Create a symbol for the stub. */
1648 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1649 stub->stub_section = s;
1650 stub->offset = s->size;
1651
1652 /* Allocate room for it. */
1653 s->size += 16;
1654 return TRUE;
1655}
1656
1657/* H describes a symbol that needs an la25 stub. Make sure that an
1658 appropriate stub exists and point H at it. */
1659
1660static bfd_boolean
1661mips_elf_add_la25_stub (struct bfd_link_info *info,
1662 struct mips_elf_link_hash_entry *h)
1663{
1664 struct mips_elf_link_hash_table *htab;
1665 struct mips_elf_la25_stub search, *stub;
1666 bfd_boolean use_trampoline_p;
1667 asection *s;
1668 bfd_vma value;
1669 void **slot;
1670
1671 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1672 of the section and if we would need no more than 2 nops. */
1673 s = h->root.root.u.def.section;
1674 value = h->root.root.u.def.value;
1675 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1676
1677 /* Describe the stub we want. */
1678 search.stub_section = NULL;
1679 search.offset = 0;
1680 search.h = h;
1681
1682 /* See if we've already created an equivalent stub. */
1683 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1684 if (htab == NULL)
1685 return FALSE;
1686
861fb55a
DJ
1687 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1688 if (slot == NULL)
1689 return FALSE;
1690
1691 stub = (struct mips_elf_la25_stub *) *slot;
1692 if (stub != NULL)
1693 {
1694 /* We can reuse the existing stub. */
1695 h->la25_stub = stub;
1696 return TRUE;
1697 }
1698
1699 /* Create a permanent copy of ENTRY and add it to the hash table. */
1700 stub = bfd_malloc (sizeof (search));
1701 if (stub == NULL)
1702 return FALSE;
1703 *stub = search;
1704 *slot = stub;
1705
1706 h->la25_stub = stub;
1707 return (use_trampoline_p
1708 ? mips_elf_add_la25_trampoline (stub, info)
1709 : mips_elf_add_la25_intro (stub, info));
1710}
1711
1712/* A mips_elf_link_hash_traverse callback that is called before sizing
1713 sections. DATA points to a mips_htab_traverse_info structure. */
1714
1715static bfd_boolean
1716mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1717{
1718 struct mips_htab_traverse_info *hti;
1719
1720 hti = (struct mips_htab_traverse_info *) data;
1721 if (h->root.root.type == bfd_link_hash_warning)
1722 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1723
1724 if (!hti->info->relocatable)
1725 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1726
861fb55a
DJ
1727 if (mips_elf_local_pic_function_p (h))
1728 {
1729 /* H is a function that might need $25 to be valid on entry.
1730 If we're creating a non-PIC relocatable object, mark H as
1731 being PIC. If we're creating a non-relocatable object with
1732 non-PIC branches and jumps to H, make sure that H has an la25
1733 stub. */
1734 if (hti->info->relocatable)
1735 {
1736 if (!PIC_OBJECT_P (hti->output_bfd))
1737 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1738 }
1739 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1740 {
1741 hti->error = TRUE;
1742 return FALSE;
1743 }
1744 }
b34976b6 1745 return TRUE;
b49e97c9
TS
1746}
1747\f
d6f16593
MR
1748/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1749 Most mips16 instructions are 16 bits, but these instructions
1750 are 32 bits.
1751
1752 The format of these instructions is:
1753
1754 +--------------+--------------------------------+
1755 | JALX | X| Imm 20:16 | Imm 25:21 |
1756 +--------------+--------------------------------+
1757 | Immediate 15:0 |
1758 +-----------------------------------------------+
1759
1760 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1761 Note that the immediate value in the first word is swapped.
1762
1763 When producing a relocatable object file, R_MIPS16_26 is
1764 handled mostly like R_MIPS_26. In particular, the addend is
1765 stored as a straight 26-bit value in a 32-bit instruction.
1766 (gas makes life simpler for itself by never adjusting a
1767 R_MIPS16_26 reloc to be against a section, so the addend is
1768 always zero). However, the 32 bit instruction is stored as 2
1769 16-bit values, rather than a single 32-bit value. In a
1770 big-endian file, the result is the same; in a little-endian
1771 file, the two 16-bit halves of the 32 bit value are swapped.
1772 This is so that a disassembler can recognize the jal
1773 instruction.
1774
1775 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1776 instruction stored as two 16-bit values. The addend A is the
1777 contents of the targ26 field. The calculation is the same as
1778 R_MIPS_26. When storing the calculated value, reorder the
1779 immediate value as shown above, and don't forget to store the
1780 value as two 16-bit values.
1781
1782 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1783 defined as
1784
1785 big-endian:
1786 +--------+----------------------+
1787 | | |
1788 | | targ26-16 |
1789 |31 26|25 0|
1790 +--------+----------------------+
1791
1792 little-endian:
1793 +----------+------+-------------+
1794 | | | |
1795 | sub1 | | sub2 |
1796 |0 9|10 15|16 31|
1797 +----------+--------------------+
1798 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1799 ((sub1 << 16) | sub2)).
1800
1801 When producing a relocatable object file, the calculation is
1802 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1803 When producing a fully linked file, the calculation is
1804 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1805 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1806
738e5348
RS
1807 The table below lists the other MIPS16 instruction relocations.
1808 Each one is calculated in the same way as the non-MIPS16 relocation
1809 given on the right, but using the extended MIPS16 layout of 16-bit
1810 immediate fields:
1811
1812 R_MIPS16_GPREL R_MIPS_GPREL16
1813 R_MIPS16_GOT16 R_MIPS_GOT16
1814 R_MIPS16_CALL16 R_MIPS_CALL16
1815 R_MIPS16_HI16 R_MIPS_HI16
1816 R_MIPS16_LO16 R_MIPS_LO16
1817
1818 A typical instruction will have a format like this:
d6f16593
MR
1819
1820 +--------------+--------------------------------+
1821 | EXTEND | Imm 10:5 | Imm 15:11 |
1822 +--------------+--------------------------------+
1823 | Major | rx | ry | Imm 4:0 |
1824 +--------------+--------------------------------+
1825
1826 EXTEND is the five bit value 11110. Major is the instruction
1827 opcode.
1828
738e5348
RS
1829 All we need to do here is shuffle the bits appropriately.
1830 As above, the two 16-bit halves must be swapped on a
1831 little-endian system. */
1832
1833static inline bfd_boolean
1834mips16_reloc_p (int r_type)
1835{
1836 switch (r_type)
1837 {
1838 case R_MIPS16_26:
1839 case R_MIPS16_GPREL:
1840 case R_MIPS16_GOT16:
1841 case R_MIPS16_CALL16:
1842 case R_MIPS16_HI16:
1843 case R_MIPS16_LO16:
1844 return TRUE;
1845
1846 default:
1847 return FALSE;
1848 }
1849}
1850
1851static inline bfd_boolean
1852got16_reloc_p (int r_type)
1853{
1854 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1855}
1856
1857static inline bfd_boolean
1858call16_reloc_p (int r_type)
1859{
1860 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1861}
1862
1863static inline bfd_boolean
1864hi16_reloc_p (int r_type)
1865{
1866 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1867}
d6f16593 1868
738e5348
RS
1869static inline bfd_boolean
1870lo16_reloc_p (int r_type)
1871{
1872 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1873}
1874
1875static inline bfd_boolean
1876mips16_call_reloc_p (int r_type)
1877{
1878 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1879}
d6f16593 1880
38a7df63
CF
1881static inline bfd_boolean
1882jal_reloc_p (int r_type)
1883{
1884 return r_type == R_MIPS_26 || r_type == R_MIPS16_26;
1885}
1886
d6f16593
MR
1887void
1888_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1889 bfd_boolean jal_shuffle, bfd_byte *data)
1890{
1891 bfd_vma extend, insn, val;
1892
738e5348 1893 if (!mips16_reloc_p (r_type))
d6f16593
MR
1894 return;
1895
1896 /* Pick up the mips16 extend instruction and the real instruction. */
1897 extend = bfd_get_16 (abfd, data);
1898 insn = bfd_get_16 (abfd, data + 2);
1899 if (r_type == R_MIPS16_26)
1900 {
1901 if (jal_shuffle)
1902 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1903 | ((extend & 0x1f) << 21) | insn;
1904 else
1905 val = extend << 16 | insn;
1906 }
1907 else
1908 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1909 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1910 bfd_put_32 (abfd, val, data);
1911}
1912
1913void
1914_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1915 bfd_boolean jal_shuffle, bfd_byte *data)
1916{
1917 bfd_vma extend, insn, val;
1918
738e5348 1919 if (!mips16_reloc_p (r_type))
d6f16593
MR
1920 return;
1921
1922 val = bfd_get_32 (abfd, data);
1923 if (r_type == R_MIPS16_26)
1924 {
1925 if (jal_shuffle)
1926 {
1927 insn = val & 0xffff;
1928 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1929 | ((val >> 21) & 0x1f);
1930 }
1931 else
1932 {
1933 insn = val & 0xffff;
1934 extend = val >> 16;
1935 }
1936 }
1937 else
1938 {
1939 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1940 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1941 }
1942 bfd_put_16 (abfd, insn, data + 2);
1943 bfd_put_16 (abfd, extend, data);
1944}
1945
b49e97c9 1946bfd_reloc_status_type
9719ad41
RS
1947_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1948 arelent *reloc_entry, asection *input_section,
1949 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
1950{
1951 bfd_vma relocation;
a7ebbfdf 1952 bfd_signed_vma val;
30ac9238 1953 bfd_reloc_status_type status;
b49e97c9
TS
1954
1955 if (bfd_is_com_section (symbol->section))
1956 relocation = 0;
1957 else
1958 relocation = symbol->value;
1959
1960 relocation += symbol->section->output_section->vma;
1961 relocation += symbol->section->output_offset;
1962
07515404 1963 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
1964 return bfd_reloc_outofrange;
1965
b49e97c9 1966 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
1967 val = reloc_entry->addend;
1968
30ac9238 1969 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 1970
b49e97c9 1971 /* Adjust val for the final section location and GP value. If we
1049f94e 1972 are producing relocatable output, we don't want to do this for
b49e97c9 1973 an external symbol. */
1049f94e 1974 if (! relocatable
b49e97c9
TS
1975 || (symbol->flags & BSF_SECTION_SYM) != 0)
1976 val += relocation - gp;
1977
a7ebbfdf
TS
1978 if (reloc_entry->howto->partial_inplace)
1979 {
30ac9238
RS
1980 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1981 (bfd_byte *) data
1982 + reloc_entry->address);
1983 if (status != bfd_reloc_ok)
1984 return status;
a7ebbfdf
TS
1985 }
1986 else
1987 reloc_entry->addend = val;
b49e97c9 1988
1049f94e 1989 if (relocatable)
b49e97c9 1990 reloc_entry->address += input_section->output_offset;
30ac9238
RS
1991
1992 return bfd_reloc_ok;
1993}
1994
1995/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1996 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1997 that contains the relocation field and DATA points to the start of
1998 INPUT_SECTION. */
1999
2000struct mips_hi16
2001{
2002 struct mips_hi16 *next;
2003 bfd_byte *data;
2004 asection *input_section;
2005 arelent rel;
2006};
2007
2008/* FIXME: This should not be a static variable. */
2009
2010static struct mips_hi16 *mips_hi16_list;
2011
2012/* A howto special_function for REL *HI16 relocations. We can only
2013 calculate the correct value once we've seen the partnering
2014 *LO16 relocation, so just save the information for later.
2015
2016 The ABI requires that the *LO16 immediately follow the *HI16.
2017 However, as a GNU extension, we permit an arbitrary number of
2018 *HI16s to be associated with a single *LO16. This significantly
2019 simplies the relocation handling in gcc. */
2020
2021bfd_reloc_status_type
2022_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2023 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2024 asection *input_section, bfd *output_bfd,
2025 char **error_message ATTRIBUTE_UNUSED)
2026{
2027 struct mips_hi16 *n;
2028
07515404 2029 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2030 return bfd_reloc_outofrange;
2031
2032 n = bfd_malloc (sizeof *n);
2033 if (n == NULL)
2034 return bfd_reloc_outofrange;
2035
2036 n->next = mips_hi16_list;
2037 n->data = data;
2038 n->input_section = input_section;
2039 n->rel = *reloc_entry;
2040 mips_hi16_list = n;
2041
2042 if (output_bfd != NULL)
2043 reloc_entry->address += input_section->output_offset;
2044
2045 return bfd_reloc_ok;
2046}
2047
738e5348 2048/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2049 like any other 16-bit relocation when applied to global symbols, but is
2050 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2051
2052bfd_reloc_status_type
2053_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2054 void *data, asection *input_section,
2055 bfd *output_bfd, char **error_message)
2056{
2057 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2058 || bfd_is_und_section (bfd_get_section (symbol))
2059 || bfd_is_com_section (bfd_get_section (symbol)))
2060 /* The relocation is against a global symbol. */
2061 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2062 input_section, output_bfd,
2063 error_message);
2064
2065 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2066 input_section, output_bfd, error_message);
2067}
2068
2069/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2070 is a straightforward 16 bit inplace relocation, but we must deal with
2071 any partnering high-part relocations as well. */
2072
2073bfd_reloc_status_type
2074_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2075 void *data, asection *input_section,
2076 bfd *output_bfd, char **error_message)
2077{
2078 bfd_vma vallo;
d6f16593 2079 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2080
07515404 2081 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2082 return bfd_reloc_outofrange;
2083
d6f16593
MR
2084 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2085 location);
2086 vallo = bfd_get_32 (abfd, location);
2087 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2088 location);
2089
30ac9238
RS
2090 while (mips_hi16_list != NULL)
2091 {
2092 bfd_reloc_status_type ret;
2093 struct mips_hi16 *hi;
2094
2095 hi = mips_hi16_list;
2096
738e5348
RS
2097 /* R_MIPS*_GOT16 relocations are something of a special case. We
2098 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2099 relocation (with a rightshift of 16). However, since GOT16
2100 relocations can also be used with global symbols, their howto
2101 has a rightshift of 0. */
2102 if (hi->rel.howto->type == R_MIPS_GOT16)
2103 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2104 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2105 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
30ac9238
RS
2106
2107 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2108 carry or borrow will induce a change of +1 or -1 in the high part. */
2109 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2110
30ac9238
RS
2111 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2112 hi->input_section, output_bfd,
2113 error_message);
2114 if (ret != bfd_reloc_ok)
2115 return ret;
2116
2117 mips_hi16_list = hi->next;
2118 free (hi);
2119 }
2120
2121 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2122 input_section, output_bfd,
2123 error_message);
2124}
2125
2126/* A generic howto special_function. This calculates and installs the
2127 relocation itself, thus avoiding the oft-discussed problems in
2128 bfd_perform_relocation and bfd_install_relocation. */
2129
2130bfd_reloc_status_type
2131_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2132 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2133 asection *input_section, bfd *output_bfd,
2134 char **error_message ATTRIBUTE_UNUSED)
2135{
2136 bfd_signed_vma val;
2137 bfd_reloc_status_type status;
2138 bfd_boolean relocatable;
2139
2140 relocatable = (output_bfd != NULL);
2141
07515404 2142 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2143 return bfd_reloc_outofrange;
2144
2145 /* Build up the field adjustment in VAL. */
2146 val = 0;
2147 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2148 {
2149 /* Either we're calculating the final field value or we have a
2150 relocation against a section symbol. Add in the section's
2151 offset or address. */
2152 val += symbol->section->output_section->vma;
2153 val += symbol->section->output_offset;
2154 }
2155
2156 if (!relocatable)
2157 {
2158 /* We're calculating the final field value. Add in the symbol's value
2159 and, if pc-relative, subtract the address of the field itself. */
2160 val += symbol->value;
2161 if (reloc_entry->howto->pc_relative)
2162 {
2163 val -= input_section->output_section->vma;
2164 val -= input_section->output_offset;
2165 val -= reloc_entry->address;
2166 }
2167 }
2168
2169 /* VAL is now the final adjustment. If we're keeping this relocation
2170 in the output file, and if the relocation uses a separate addend,
2171 we just need to add VAL to that addend. Otherwise we need to add
2172 VAL to the relocation field itself. */
2173 if (relocatable && !reloc_entry->howto->partial_inplace)
2174 reloc_entry->addend += val;
2175 else
2176 {
d6f16593
MR
2177 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2178
30ac9238
RS
2179 /* Add in the separate addend, if any. */
2180 val += reloc_entry->addend;
2181
2182 /* Add VAL to the relocation field. */
d6f16593
MR
2183 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2184 location);
30ac9238 2185 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593
MR
2186 location);
2187 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2188 location);
2189
30ac9238
RS
2190 if (status != bfd_reloc_ok)
2191 return status;
2192 }
2193
2194 if (relocatable)
2195 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2196
2197 return bfd_reloc_ok;
2198}
2199\f
2200/* Swap an entry in a .gptab section. Note that these routines rely
2201 on the equivalence of the two elements of the union. */
2202
2203static void
9719ad41
RS
2204bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2205 Elf32_gptab *in)
b49e97c9
TS
2206{
2207 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2208 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2209}
2210
2211static void
9719ad41
RS
2212bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2213 Elf32_External_gptab *ex)
b49e97c9
TS
2214{
2215 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2216 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2217}
2218
2219static void
9719ad41
RS
2220bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2221 Elf32_External_compact_rel *ex)
b49e97c9
TS
2222{
2223 H_PUT_32 (abfd, in->id1, ex->id1);
2224 H_PUT_32 (abfd, in->num, ex->num);
2225 H_PUT_32 (abfd, in->id2, ex->id2);
2226 H_PUT_32 (abfd, in->offset, ex->offset);
2227 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2228 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2229}
2230
2231static void
9719ad41
RS
2232bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2233 Elf32_External_crinfo *ex)
b49e97c9
TS
2234{
2235 unsigned long l;
2236
2237 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2238 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2239 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2240 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2241 H_PUT_32 (abfd, l, ex->info);
2242 H_PUT_32 (abfd, in->konst, ex->konst);
2243 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2244}
b49e97c9
TS
2245\f
2246/* A .reginfo section holds a single Elf32_RegInfo structure. These
2247 routines swap this structure in and out. They are used outside of
2248 BFD, so they are globally visible. */
2249
2250void
9719ad41
RS
2251bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2252 Elf32_RegInfo *in)
b49e97c9
TS
2253{
2254 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2255 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2256 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2257 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2258 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2259 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2260}
2261
2262void
9719ad41
RS
2263bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2264 Elf32_External_RegInfo *ex)
b49e97c9
TS
2265{
2266 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2267 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2268 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2269 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2270 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2271 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2272}
2273
2274/* In the 64 bit ABI, the .MIPS.options section holds register
2275 information in an Elf64_Reginfo structure. These routines swap
2276 them in and out. They are globally visible because they are used
2277 outside of BFD. These routines are here so that gas can call them
2278 without worrying about whether the 64 bit ABI has been included. */
2279
2280void
9719ad41
RS
2281bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2282 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2283{
2284 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2285 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2286 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2287 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2288 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2289 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2290 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2291}
2292
2293void
9719ad41
RS
2294bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2295 Elf64_External_RegInfo *ex)
b49e97c9
TS
2296{
2297 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2298 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2299 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2300 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2301 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2302 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2303 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2304}
2305
2306/* Swap in an options header. */
2307
2308void
9719ad41
RS
2309bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2310 Elf_Internal_Options *in)
b49e97c9
TS
2311{
2312 in->kind = H_GET_8 (abfd, ex->kind);
2313 in->size = H_GET_8 (abfd, ex->size);
2314 in->section = H_GET_16 (abfd, ex->section);
2315 in->info = H_GET_32 (abfd, ex->info);
2316}
2317
2318/* Swap out an options header. */
2319
2320void
9719ad41
RS
2321bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2322 Elf_External_Options *ex)
b49e97c9
TS
2323{
2324 H_PUT_8 (abfd, in->kind, ex->kind);
2325 H_PUT_8 (abfd, in->size, ex->size);
2326 H_PUT_16 (abfd, in->section, ex->section);
2327 H_PUT_32 (abfd, in->info, ex->info);
2328}
2329\f
2330/* This function is called via qsort() to sort the dynamic relocation
2331 entries by increasing r_symndx value. */
2332
2333static int
9719ad41 2334sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2335{
947216bf
AM
2336 Elf_Internal_Rela int_reloc1;
2337 Elf_Internal_Rela int_reloc2;
6870500c 2338 int diff;
b49e97c9 2339
947216bf
AM
2340 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2341 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2342
6870500c
RS
2343 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2344 if (diff != 0)
2345 return diff;
2346
2347 if (int_reloc1.r_offset < int_reloc2.r_offset)
2348 return -1;
2349 if (int_reloc1.r_offset > int_reloc2.r_offset)
2350 return 1;
2351 return 0;
b49e97c9
TS
2352}
2353
f4416af6
AO
2354/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2355
2356static int
7e3102a7
AM
2357sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2358 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2359{
7e3102a7 2360#ifdef BFD64
f4416af6
AO
2361 Elf_Internal_Rela int_reloc1[3];
2362 Elf_Internal_Rela int_reloc2[3];
2363
2364 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2365 (reldyn_sorting_bfd, arg1, int_reloc1);
2366 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2367 (reldyn_sorting_bfd, arg2, int_reloc2);
2368
6870500c
RS
2369 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2370 return -1;
2371 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2372 return 1;
2373
2374 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2375 return -1;
2376 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2377 return 1;
2378 return 0;
7e3102a7
AM
2379#else
2380 abort ();
2381#endif
f4416af6
AO
2382}
2383
2384
b49e97c9
TS
2385/* This routine is used to write out ECOFF debugging external symbol
2386 information. It is called via mips_elf_link_hash_traverse. The
2387 ECOFF external symbol information must match the ELF external
2388 symbol information. Unfortunately, at this point we don't know
2389 whether a symbol is required by reloc information, so the two
2390 tables may wind up being different. We must sort out the external
2391 symbol information before we can set the final size of the .mdebug
2392 section, and we must set the size of the .mdebug section before we
2393 can relocate any sections, and we can't know which symbols are
2394 required by relocation until we relocate the sections.
2395 Fortunately, it is relatively unlikely that any symbol will be
2396 stripped but required by a reloc. In particular, it can not happen
2397 when generating a final executable. */
2398
b34976b6 2399static bfd_boolean
9719ad41 2400mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2401{
9719ad41 2402 struct extsym_info *einfo = data;
b34976b6 2403 bfd_boolean strip;
b49e97c9
TS
2404 asection *sec, *output_section;
2405
2406 if (h->root.root.type == bfd_link_hash_warning)
2407 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2408
2409 if (h->root.indx == -2)
b34976b6 2410 strip = FALSE;
f5385ebf 2411 else if ((h->root.def_dynamic
77cfaee6
AM
2412 || h->root.ref_dynamic
2413 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2414 && !h->root.def_regular
2415 && !h->root.ref_regular)
b34976b6 2416 strip = TRUE;
b49e97c9
TS
2417 else if (einfo->info->strip == strip_all
2418 || (einfo->info->strip == strip_some
2419 && bfd_hash_lookup (einfo->info->keep_hash,
2420 h->root.root.root.string,
b34976b6
AM
2421 FALSE, FALSE) == NULL))
2422 strip = TRUE;
b49e97c9 2423 else
b34976b6 2424 strip = FALSE;
b49e97c9
TS
2425
2426 if (strip)
b34976b6 2427 return TRUE;
b49e97c9
TS
2428
2429 if (h->esym.ifd == -2)
2430 {
2431 h->esym.jmptbl = 0;
2432 h->esym.cobol_main = 0;
2433 h->esym.weakext = 0;
2434 h->esym.reserved = 0;
2435 h->esym.ifd = ifdNil;
2436 h->esym.asym.value = 0;
2437 h->esym.asym.st = stGlobal;
2438
2439 if (h->root.root.type == bfd_link_hash_undefined
2440 || h->root.root.type == bfd_link_hash_undefweak)
2441 {
2442 const char *name;
2443
2444 /* Use undefined class. Also, set class and type for some
2445 special symbols. */
2446 name = h->root.root.root.string;
2447 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2448 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2449 {
2450 h->esym.asym.sc = scData;
2451 h->esym.asym.st = stLabel;
2452 h->esym.asym.value = 0;
2453 }
2454 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2455 {
2456 h->esym.asym.sc = scAbs;
2457 h->esym.asym.st = stLabel;
2458 h->esym.asym.value =
2459 mips_elf_hash_table (einfo->info)->procedure_count;
2460 }
4a14403c 2461 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2462 {
2463 h->esym.asym.sc = scAbs;
2464 h->esym.asym.st = stLabel;
2465 h->esym.asym.value = elf_gp (einfo->abfd);
2466 }
2467 else
2468 h->esym.asym.sc = scUndefined;
2469 }
2470 else if (h->root.root.type != bfd_link_hash_defined
2471 && h->root.root.type != bfd_link_hash_defweak)
2472 h->esym.asym.sc = scAbs;
2473 else
2474 {
2475 const char *name;
2476
2477 sec = h->root.root.u.def.section;
2478 output_section = sec->output_section;
2479
2480 /* When making a shared library and symbol h is the one from
2481 the another shared library, OUTPUT_SECTION may be null. */
2482 if (output_section == NULL)
2483 h->esym.asym.sc = scUndefined;
2484 else
2485 {
2486 name = bfd_section_name (output_section->owner, output_section);
2487
2488 if (strcmp (name, ".text") == 0)
2489 h->esym.asym.sc = scText;
2490 else if (strcmp (name, ".data") == 0)
2491 h->esym.asym.sc = scData;
2492 else if (strcmp (name, ".sdata") == 0)
2493 h->esym.asym.sc = scSData;
2494 else if (strcmp (name, ".rodata") == 0
2495 || strcmp (name, ".rdata") == 0)
2496 h->esym.asym.sc = scRData;
2497 else if (strcmp (name, ".bss") == 0)
2498 h->esym.asym.sc = scBss;
2499 else if (strcmp (name, ".sbss") == 0)
2500 h->esym.asym.sc = scSBss;
2501 else if (strcmp (name, ".init") == 0)
2502 h->esym.asym.sc = scInit;
2503 else if (strcmp (name, ".fini") == 0)
2504 h->esym.asym.sc = scFini;
2505 else
2506 h->esym.asym.sc = scAbs;
2507 }
2508 }
2509
2510 h->esym.asym.reserved = 0;
2511 h->esym.asym.index = indexNil;
2512 }
2513
2514 if (h->root.root.type == bfd_link_hash_common)
2515 h->esym.asym.value = h->root.root.u.c.size;
2516 else if (h->root.root.type == bfd_link_hash_defined
2517 || h->root.root.type == bfd_link_hash_defweak)
2518 {
2519 if (h->esym.asym.sc == scCommon)
2520 h->esym.asym.sc = scBss;
2521 else if (h->esym.asym.sc == scSCommon)
2522 h->esym.asym.sc = scSBss;
2523
2524 sec = h->root.root.u.def.section;
2525 output_section = sec->output_section;
2526 if (output_section != NULL)
2527 h->esym.asym.value = (h->root.root.u.def.value
2528 + sec->output_offset
2529 + output_section->vma);
2530 else
2531 h->esym.asym.value = 0;
2532 }
33bb52fb 2533 else
b49e97c9
TS
2534 {
2535 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2536
2537 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2538 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2539
33bb52fb 2540 if (hd->needs_lazy_stub)
b49e97c9
TS
2541 {
2542 /* Set type and value for a symbol with a function stub. */
2543 h->esym.asym.st = stProc;
2544 sec = hd->root.root.u.def.section;
2545 if (sec == NULL)
2546 h->esym.asym.value = 0;
2547 else
2548 {
2549 output_section = sec->output_section;
2550 if (output_section != NULL)
2551 h->esym.asym.value = (hd->root.plt.offset
2552 + sec->output_offset
2553 + output_section->vma);
2554 else
2555 h->esym.asym.value = 0;
2556 }
b49e97c9
TS
2557 }
2558 }
2559
2560 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2561 h->root.root.root.string,
2562 &h->esym))
2563 {
b34976b6
AM
2564 einfo->failed = TRUE;
2565 return FALSE;
b49e97c9
TS
2566 }
2567
b34976b6 2568 return TRUE;
b49e97c9
TS
2569}
2570
2571/* A comparison routine used to sort .gptab entries. */
2572
2573static int
9719ad41 2574gptab_compare (const void *p1, const void *p2)
b49e97c9 2575{
9719ad41
RS
2576 const Elf32_gptab *a1 = p1;
2577 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2578
2579 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2580}
2581\f
b15e6682 2582/* Functions to manage the got entry hash table. */
f4416af6
AO
2583
2584/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2585 hash number. */
2586
2587static INLINE hashval_t
9719ad41 2588mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2589{
2590#ifdef BFD64
2591 return addr + (addr >> 32);
2592#else
2593 return addr;
2594#endif
2595}
2596
2597/* got_entries only match if they're identical, except for gotidx, so
2598 use all fields to compute the hash, and compare the appropriate
2599 union members. */
2600
b15e6682 2601static hashval_t
9719ad41 2602mips_elf_got_entry_hash (const void *entry_)
b15e6682
AO
2603{
2604 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2605
38985a1c 2606 return entry->symndx
0f20cc35 2607 + ((entry->tls_type & GOT_TLS_LDM) << 17)
f4416af6 2608 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
38985a1c
AO
2609 : entry->abfd->id
2610 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2611 : entry->d.h->root.root.root.hash));
b15e6682
AO
2612}
2613
2614static int
9719ad41 2615mips_elf_got_entry_eq (const void *entry1, const void *entry2)
b15e6682
AO
2616{
2617 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2618 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2619
0f20cc35
DJ
2620 /* An LDM entry can only match another LDM entry. */
2621 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2622 return 0;
2623
b15e6682 2624 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
f4416af6
AO
2625 && (! e1->abfd ? e1->d.address == e2->d.address
2626 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2627 : e1->d.h == e2->d.h);
2628}
2629
2630/* multi_got_entries are still a match in the case of global objects,
2631 even if the input bfd in which they're referenced differs, so the
2632 hash computation and compare functions are adjusted
2633 accordingly. */
2634
2635static hashval_t
9719ad41 2636mips_elf_multi_got_entry_hash (const void *entry_)
f4416af6
AO
2637{
2638 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2639
2640 return entry->symndx
2641 + (! entry->abfd
2642 ? mips_elf_hash_bfd_vma (entry->d.address)
2643 : entry->symndx >= 0
0f20cc35
DJ
2644 ? ((entry->tls_type & GOT_TLS_LDM)
2645 ? (GOT_TLS_LDM << 17)
2646 : (entry->abfd->id
2647 + mips_elf_hash_bfd_vma (entry->d.addend)))
f4416af6
AO
2648 : entry->d.h->root.root.root.hash);
2649}
2650
2651static int
9719ad41 2652mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2653{
2654 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2655 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2656
0f20cc35
DJ
2657 /* Any two LDM entries match. */
2658 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2659 return 1;
2660
2661 /* Nothing else matches an LDM entry. */
2662 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2663 return 0;
2664
f4416af6
AO
2665 return e1->symndx == e2->symndx
2666 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2667 : e1->abfd == NULL || e2->abfd == NULL
2668 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2669 : e1->d.h == e2->d.h);
b15e6682 2670}
c224138d
RS
2671
2672static hashval_t
2673mips_got_page_entry_hash (const void *entry_)
2674{
2675 const struct mips_got_page_entry *entry;
2676
2677 entry = (const struct mips_got_page_entry *) entry_;
2678 return entry->abfd->id + entry->symndx;
2679}
2680
2681static int
2682mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2683{
2684 const struct mips_got_page_entry *entry1, *entry2;
2685
2686 entry1 = (const struct mips_got_page_entry *) entry1_;
2687 entry2 = (const struct mips_got_page_entry *) entry2_;
2688 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2689}
b15e6682 2690\f
0a44bf69
RS
2691/* Return the dynamic relocation section. If it doesn't exist, try to
2692 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2693 if creation fails. */
f4416af6
AO
2694
2695static asection *
0a44bf69 2696mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 2697{
0a44bf69 2698 const char *dname;
f4416af6 2699 asection *sreloc;
0a44bf69 2700 bfd *dynobj;
f4416af6 2701
0a44bf69
RS
2702 dname = MIPS_ELF_REL_DYN_NAME (info);
2703 dynobj = elf_hash_table (info)->dynobj;
f4416af6
AO
2704 sreloc = bfd_get_section_by_name (dynobj, dname);
2705 if (sreloc == NULL && create_p)
2706 {
3496cb2a
L
2707 sreloc = bfd_make_section_with_flags (dynobj, dname,
2708 (SEC_ALLOC
2709 | SEC_LOAD
2710 | SEC_HAS_CONTENTS
2711 | SEC_IN_MEMORY
2712 | SEC_LINKER_CREATED
2713 | SEC_READONLY));
f4416af6 2714 if (sreloc == NULL
f4416af6 2715 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 2716 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
2717 return NULL;
2718 }
2719 return sreloc;
2720}
2721
0f20cc35
DJ
2722/* Count the number of relocations needed for a TLS GOT entry, with
2723 access types from TLS_TYPE, and symbol H (or a local symbol if H
2724 is NULL). */
2725
2726static int
2727mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2728 struct elf_link_hash_entry *h)
2729{
2730 int indx = 0;
2731 int ret = 0;
2732 bfd_boolean need_relocs = FALSE;
2733 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2734
2735 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2736 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2737 indx = h->dynindx;
2738
2739 if ((info->shared || indx != 0)
2740 && (h == NULL
2741 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2742 || h->root.type != bfd_link_hash_undefweak))
2743 need_relocs = TRUE;
2744
2745 if (!need_relocs)
2746 return FALSE;
2747
2748 if (tls_type & GOT_TLS_GD)
2749 {
2750 ret++;
2751 if (indx != 0)
2752 ret++;
2753 }
2754
2755 if (tls_type & GOT_TLS_IE)
2756 ret++;
2757
2758 if ((tls_type & GOT_TLS_LDM) && info->shared)
2759 ret++;
2760
2761 return ret;
2762}
2763
2764/* Count the number of TLS relocations required for the GOT entry in
2765 ARG1, if it describes a local symbol. */
2766
2767static int
2768mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2769{
2770 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2771 struct mips_elf_count_tls_arg *arg = arg2;
2772
2773 if (entry->abfd != NULL && entry->symndx != -1)
2774 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2775
2776 return 1;
2777}
2778
2779/* Count the number of TLS GOT entries required for the global (or
2780 forced-local) symbol in ARG1. */
2781
2782static int
2783mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2784{
2785 struct mips_elf_link_hash_entry *hm
2786 = (struct mips_elf_link_hash_entry *) arg1;
2787 struct mips_elf_count_tls_arg *arg = arg2;
2788
2789 if (hm->tls_type & GOT_TLS_GD)
2790 arg->needed += 2;
2791 if (hm->tls_type & GOT_TLS_IE)
2792 arg->needed += 1;
2793
2794 return 1;
2795}
2796
2797/* Count the number of TLS relocations required for the global (or
2798 forced-local) symbol in ARG1. */
2799
2800static int
2801mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2802{
2803 struct mips_elf_link_hash_entry *hm
2804 = (struct mips_elf_link_hash_entry *) arg1;
2805 struct mips_elf_count_tls_arg *arg = arg2;
2806
2807 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2808
2809 return 1;
2810}
2811
2812/* Output a simple dynamic relocation into SRELOC. */
2813
2814static void
2815mips_elf_output_dynamic_relocation (bfd *output_bfd,
2816 asection *sreloc,
861fb55a 2817 unsigned long reloc_index,
0f20cc35
DJ
2818 unsigned long indx,
2819 int r_type,
2820 bfd_vma offset)
2821{
2822 Elf_Internal_Rela rel[3];
2823
2824 memset (rel, 0, sizeof (rel));
2825
2826 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2827 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2828
2829 if (ABI_64_P (output_bfd))
2830 {
2831 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2832 (output_bfd, &rel[0],
2833 (sreloc->contents
861fb55a 2834 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
2835 }
2836 else
2837 bfd_elf32_swap_reloc_out
2838 (output_bfd, &rel[0],
2839 (sreloc->contents
861fb55a 2840 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
2841}
2842
2843/* Initialize a set of TLS GOT entries for one symbol. */
2844
2845static void
2846mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2847 unsigned char *tls_type_p,
2848 struct bfd_link_info *info,
2849 struct mips_elf_link_hash_entry *h,
2850 bfd_vma value)
2851{
23cc69b6 2852 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
2853 int indx;
2854 asection *sreloc, *sgot;
2855 bfd_vma offset, offset2;
0f20cc35
DJ
2856 bfd_boolean need_relocs = FALSE;
2857
23cc69b6 2858 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
2859 if (htab == NULL)
2860 return;
2861
23cc69b6 2862 sgot = htab->sgot;
0f20cc35
DJ
2863
2864 indx = 0;
2865 if (h != NULL)
2866 {
2867 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2868
2869 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2870 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2871 indx = h->root.dynindx;
2872 }
2873
2874 if (*tls_type_p & GOT_TLS_DONE)
2875 return;
2876
2877 if ((info->shared || indx != 0)
2878 && (h == NULL
2879 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2880 || h->root.type != bfd_link_hash_undefweak))
2881 need_relocs = TRUE;
2882
2883 /* MINUS_ONE means the symbol is not defined in this object. It may not
2884 be defined at all; assume that the value doesn't matter in that
2885 case. Otherwise complain if we would use the value. */
2886 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2887 || h->root.root.type == bfd_link_hash_undefweak);
2888
2889 /* Emit necessary relocations. */
0a44bf69 2890 sreloc = mips_elf_rel_dyn_section (info, FALSE);
0f20cc35
DJ
2891
2892 /* General Dynamic. */
2893 if (*tls_type_p & GOT_TLS_GD)
2894 {
2895 offset = got_offset;
2896 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2897
2898 if (need_relocs)
2899 {
2900 mips_elf_output_dynamic_relocation
861fb55a 2901 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2902 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2903 sgot->output_offset + sgot->output_section->vma + offset);
2904
2905 if (indx)
2906 mips_elf_output_dynamic_relocation
861fb55a 2907 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2908 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2909 sgot->output_offset + sgot->output_section->vma + offset2);
2910 else
2911 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2912 sgot->contents + offset2);
2913 }
2914 else
2915 {
2916 MIPS_ELF_PUT_WORD (abfd, 1,
2917 sgot->contents + offset);
2918 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2919 sgot->contents + offset2);
2920 }
2921
2922 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2923 }
2924
2925 /* Initial Exec model. */
2926 if (*tls_type_p & GOT_TLS_IE)
2927 {
2928 offset = got_offset;
2929
2930 if (need_relocs)
2931 {
2932 if (indx == 0)
2933 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2934 sgot->contents + offset);
2935 else
2936 MIPS_ELF_PUT_WORD (abfd, 0,
2937 sgot->contents + offset);
2938
2939 mips_elf_output_dynamic_relocation
861fb55a 2940 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2941 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2942 sgot->output_offset + sgot->output_section->vma + offset);
2943 }
2944 else
2945 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2946 sgot->contents + offset);
2947 }
2948
2949 if (*tls_type_p & GOT_TLS_LDM)
2950 {
2951 /* The initial offset is zero, and the LD offsets will include the
2952 bias by DTP_OFFSET. */
2953 MIPS_ELF_PUT_WORD (abfd, 0,
2954 sgot->contents + got_offset
2955 + MIPS_ELF_GOT_SIZE (abfd));
2956
2957 if (!info->shared)
2958 MIPS_ELF_PUT_WORD (abfd, 1,
2959 sgot->contents + got_offset);
2960 else
2961 mips_elf_output_dynamic_relocation
861fb55a 2962 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2963 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2964 sgot->output_offset + sgot->output_section->vma + got_offset);
2965 }
2966
2967 *tls_type_p |= GOT_TLS_DONE;
2968}
2969
2970/* Return the GOT index to use for a relocation of type R_TYPE against
2971 a symbol accessed using TLS_TYPE models. The GOT entries for this
2972 symbol in this GOT start at GOT_INDEX. This function initializes the
2973 GOT entries and corresponding relocations. */
2974
2975static bfd_vma
2976mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2977 int r_type, struct bfd_link_info *info,
2978 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2979{
2980 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2981 || r_type == R_MIPS_TLS_LDM);
2982
2983 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2984
2985 if (r_type == R_MIPS_TLS_GOTTPREL)
2986 {
2987 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2988 if (*tls_type & GOT_TLS_GD)
2989 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2990 else
2991 return got_index;
2992 }
2993
2994 if (r_type == R_MIPS_TLS_GD)
2995 {
2996 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2997 return got_index;
2998 }
2999
3000 if (r_type == R_MIPS_TLS_LDM)
3001 {
3002 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
3003 return got_index;
3004 }
3005
3006 return got_index;
3007}
3008
0a44bf69
RS
3009/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3010 for global symbol H. .got.plt comes before the GOT, so the offset
3011 will be negative. */
3012
3013static bfd_vma
3014mips_elf_gotplt_index (struct bfd_link_info *info,
3015 struct elf_link_hash_entry *h)
3016{
3017 bfd_vma plt_index, got_address, got_value;
3018 struct mips_elf_link_hash_table *htab;
3019
3020 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3021 BFD_ASSERT (htab != NULL);
3022
0a44bf69
RS
3023 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3024
861fb55a
DJ
3025 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3026 section starts with reserved entries. */
3027 BFD_ASSERT (htab->is_vxworks);
3028
0a44bf69
RS
3029 /* Calculate the index of the symbol's PLT entry. */
3030 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3031
3032 /* Calculate the address of the associated .got.plt entry. */
3033 got_address = (htab->sgotplt->output_section->vma
3034 + htab->sgotplt->output_offset
3035 + plt_index * 4);
3036
3037 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3038 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3039 + htab->root.hgot->root.u.def.section->output_offset
3040 + htab->root.hgot->root.u.def.value);
3041
3042 return got_address - got_value;
3043}
3044
5c18022e 3045/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3046 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3047 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3048 offset can be found. */
b49e97c9
TS
3049
3050static bfd_vma
9719ad41 3051mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3052 bfd_vma value, unsigned long r_symndx,
0f20cc35 3053 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3054{
a8028dd0 3055 struct mips_elf_link_hash_table *htab;
b15e6682 3056 struct mips_got_entry *entry;
b49e97c9 3057
a8028dd0 3058 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3059 BFD_ASSERT (htab != NULL);
3060
a8028dd0
RS
3061 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3062 r_symndx, h, r_type);
0f20cc35 3063 if (!entry)
b15e6682 3064 return MINUS_ONE;
0f20cc35
DJ
3065
3066 if (TLS_RELOC_P (r_type))
ead49a57 3067 {
a8028dd0 3068 if (entry->symndx == -1 && htab->got_info->next == NULL)
ead49a57
RS
3069 /* A type (3) entry in the single-GOT case. We use the symbol's
3070 hash table entry to track the index. */
3071 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3072 r_type, info, h, value);
3073 else
3074 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3075 r_type, info, h, value);
3076 }
0f20cc35
DJ
3077 else
3078 return entry->gotidx;
b49e97c9
TS
3079}
3080
3081/* Returns the GOT index for the global symbol indicated by H. */
3082
3083static bfd_vma
0f20cc35
DJ
3084mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3085 int r_type, struct bfd_link_info *info)
b49e97c9 3086{
a8028dd0 3087 struct mips_elf_link_hash_table *htab;
91d6fa6a 3088 bfd_vma got_index;
f4416af6 3089 struct mips_got_info *g, *gg;
d0c7ff07 3090 long global_got_dynindx = 0;
b49e97c9 3091
a8028dd0 3092 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3093 BFD_ASSERT (htab != NULL);
3094
a8028dd0 3095 gg = g = htab->got_info;
f4416af6
AO
3096 if (g->bfd2got && ibfd)
3097 {
3098 struct mips_got_entry e, *p;
143d77c5 3099
f4416af6
AO
3100 BFD_ASSERT (h->dynindx >= 0);
3101
3102 g = mips_elf_got_for_ibfd (g, ibfd);
0f20cc35 3103 if (g->next != gg || TLS_RELOC_P (r_type))
f4416af6
AO
3104 {
3105 e.abfd = ibfd;
3106 e.symndx = -1;
3107 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 3108 e.tls_type = 0;
f4416af6 3109
9719ad41 3110 p = htab_find (g->got_entries, &e);
f4416af6
AO
3111
3112 BFD_ASSERT (p->gotidx > 0);
0f20cc35
DJ
3113
3114 if (TLS_RELOC_P (r_type))
3115 {
3116 bfd_vma value = MINUS_ONE;
3117 if ((h->root.type == bfd_link_hash_defined
3118 || h->root.type == bfd_link_hash_defweak)
3119 && h->root.u.def.section->output_section)
3120 value = (h->root.u.def.value
3121 + h->root.u.def.section->output_offset
3122 + h->root.u.def.section->output_section->vma);
3123
3124 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3125 info, e.d.h, value);
3126 }
3127 else
3128 return p->gotidx;
f4416af6
AO
3129 }
3130 }
3131
3132 if (gg->global_gotsym != NULL)
3133 global_got_dynindx = gg->global_gotsym->dynindx;
b49e97c9 3134
0f20cc35
DJ
3135 if (TLS_RELOC_P (r_type))
3136 {
3137 struct mips_elf_link_hash_entry *hm
3138 = (struct mips_elf_link_hash_entry *) h;
3139 bfd_vma value = MINUS_ONE;
3140
3141 if ((h->root.type == bfd_link_hash_defined
3142 || h->root.type == bfd_link_hash_defweak)
3143 && h->root.u.def.section->output_section)
3144 value = (h->root.u.def.value
3145 + h->root.u.def.section->output_offset
3146 + h->root.u.def.section->output_section->vma);
3147
91d6fa6a
NC
3148 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3149 r_type, info, hm, value);
0f20cc35
DJ
3150 }
3151 else
3152 {
3153 /* Once we determine the global GOT entry with the lowest dynamic
3154 symbol table index, we must put all dynamic symbols with greater
3155 indices into the GOT. That makes it easy to calculate the GOT
3156 offset. */
3157 BFD_ASSERT (h->dynindx >= global_got_dynindx);
91d6fa6a
NC
3158 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3159 * MIPS_ELF_GOT_SIZE (abfd));
0f20cc35 3160 }
91d6fa6a 3161 BFD_ASSERT (got_index < htab->sgot->size);
b49e97c9 3162
91d6fa6a 3163 return got_index;
b49e97c9
TS
3164}
3165
5c18022e
RS
3166/* Find a GOT page entry that points to within 32KB of VALUE. These
3167 entries are supposed to be placed at small offsets in the GOT, i.e.,
3168 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3169 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3170 offset of the GOT entry from VALUE. */
b49e97c9
TS
3171
3172static bfd_vma
9719ad41 3173mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3174 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3175{
91d6fa6a 3176 bfd_vma page, got_index;
b15e6682 3177 struct mips_got_entry *entry;
b49e97c9 3178
0a44bf69 3179 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3180 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3181 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3182
b15e6682
AO
3183 if (!entry)
3184 return MINUS_ONE;
143d77c5 3185
91d6fa6a 3186 got_index = entry->gotidx;
b49e97c9
TS
3187
3188 if (offsetp)
f4416af6 3189 *offsetp = value - entry->d.address;
b49e97c9 3190
91d6fa6a 3191 return got_index;
b49e97c9
TS
3192}
3193
738e5348 3194/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3195 EXTERNAL is true if the relocation was originally against a global
3196 symbol that binds locally. */
b49e97c9
TS
3197
3198static bfd_vma
9719ad41 3199mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3200 bfd_vma value, bfd_boolean external)
b49e97c9 3201{
b15e6682 3202 struct mips_got_entry *entry;
b49e97c9 3203
0a44bf69
RS
3204 /* GOT16 relocations against local symbols are followed by a LO16
3205 relocation; those against global symbols are not. Thus if the
3206 symbol was originally local, the GOT16 relocation should load the
3207 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3208 if (! external)
0a44bf69 3209 value = mips_elf_high (value) << 16;
b49e97c9 3210
738e5348
RS
3211 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3212 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3213 same in all cases. */
a8028dd0
RS
3214 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3215 NULL, R_MIPS_GOT16);
b15e6682
AO
3216 if (entry)
3217 return entry->gotidx;
3218 else
3219 return MINUS_ONE;
b49e97c9
TS
3220}
3221
3222/* Returns the offset for the entry at the INDEXth position
3223 in the GOT. */
3224
3225static bfd_vma
a8028dd0 3226mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3227 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3228{
a8028dd0 3229 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3230 asection *sgot;
3231 bfd_vma gp;
3232
a8028dd0 3233 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3234 BFD_ASSERT (htab != NULL);
3235
a8028dd0 3236 sgot = htab->sgot;
f4416af6 3237 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3238 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3239
91d6fa6a 3240 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3241}
3242
0a44bf69
RS
3243/* Create and return a local GOT entry for VALUE, which was calculated
3244 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3245 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3246 instead. */
b49e97c9 3247
b15e6682 3248static struct mips_got_entry *
0a44bf69 3249mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3250 bfd *ibfd, bfd_vma value,
5c18022e 3251 unsigned long r_symndx,
0f20cc35
DJ
3252 struct mips_elf_link_hash_entry *h,
3253 int r_type)
b49e97c9 3254{
b15e6682 3255 struct mips_got_entry entry, **loc;
f4416af6 3256 struct mips_got_info *g;
0a44bf69
RS
3257 struct mips_elf_link_hash_table *htab;
3258
3259 htab = mips_elf_hash_table (info);
4dfe6ac6 3260 BFD_ASSERT (htab != NULL);
b15e6682 3261
f4416af6
AO
3262 entry.abfd = NULL;
3263 entry.symndx = -1;
3264 entry.d.address = value;
0f20cc35 3265 entry.tls_type = 0;
f4416af6 3266
a8028dd0 3267 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
f4416af6
AO
3268 if (g == NULL)
3269 {
a8028dd0 3270 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
f4416af6
AO
3271 BFD_ASSERT (g != NULL);
3272 }
b15e6682 3273
020d7251
RS
3274 /* This function shouldn't be called for symbols that live in the global
3275 area of the GOT. */
3276 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35
DJ
3277 if (TLS_RELOC_P (r_type))
3278 {
3279 struct mips_got_entry *p;
3280
3281 entry.abfd = ibfd;
3282 if (r_type == R_MIPS_TLS_LDM)
3283 {
3284 entry.tls_type = GOT_TLS_LDM;
3285 entry.symndx = 0;
3286 entry.d.addend = 0;
3287 }
3288 else if (h == NULL)
3289 {
3290 entry.symndx = r_symndx;
3291 entry.d.addend = 0;
3292 }
3293 else
3294 entry.d.h = h;
3295
3296 p = (struct mips_got_entry *)
3297 htab_find (g->got_entries, &entry);
3298
3299 BFD_ASSERT (p);
3300 return p;
3301 }
3302
b15e6682
AO
3303 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3304 INSERT);
3305 if (*loc)
3306 return *loc;
143d77c5 3307
b15e6682 3308 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
0f20cc35 3309 entry.tls_type = 0;
b15e6682
AO
3310
3311 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3312
3313 if (! *loc)
3314 return NULL;
143d77c5 3315
b15e6682
AO
3316 memcpy (*loc, &entry, sizeof entry);
3317
8275b357 3318 if (g->assigned_gotno > g->local_gotno)
b49e97c9 3319 {
f4416af6 3320 (*loc)->gotidx = -1;
b49e97c9
TS
3321 /* We didn't allocate enough space in the GOT. */
3322 (*_bfd_error_handler)
3323 (_("not enough GOT space for local GOT entries"));
3324 bfd_set_error (bfd_error_bad_value);
b15e6682 3325 return NULL;
b49e97c9
TS
3326 }
3327
3328 MIPS_ELF_PUT_WORD (abfd, value,
a8028dd0 3329 (htab->sgot->contents + entry.gotidx));
b15e6682 3330
5c18022e 3331 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3332 if (htab->is_vxworks)
3333 {
3334 Elf_Internal_Rela outrel;
5c18022e 3335 asection *s;
91d6fa6a 3336 bfd_byte *rloc;
0a44bf69 3337 bfd_vma got_address;
0a44bf69
RS
3338
3339 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3340 got_address = (htab->sgot->output_section->vma
3341 + htab->sgot->output_offset
0a44bf69
RS
3342 + entry.gotidx);
3343
91d6fa6a 3344 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3345 outrel.r_offset = got_address;
5c18022e
RS
3346 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3347 outrel.r_addend = value;
91d6fa6a 3348 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3349 }
3350
b15e6682 3351 return *loc;
b49e97c9
TS
3352}
3353
d4596a51
RS
3354/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3355 The number might be exact or a worst-case estimate, depending on how
3356 much information is available to elf_backend_omit_section_dynsym at
3357 the current linking stage. */
3358
3359static bfd_size_type
3360count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3361{
3362 bfd_size_type count;
3363
3364 count = 0;
3365 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3366 {
3367 asection *p;
3368 const struct elf_backend_data *bed;
3369
3370 bed = get_elf_backend_data (output_bfd);
3371 for (p = output_bfd->sections; p ; p = p->next)
3372 if ((p->flags & SEC_EXCLUDE) == 0
3373 && (p->flags & SEC_ALLOC) != 0
3374 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3375 ++count;
3376 }
3377 return count;
3378}
3379
b49e97c9 3380/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3381 appear towards the end. */
b49e97c9 3382
b34976b6 3383static bfd_boolean
d4596a51 3384mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3385{
a8028dd0 3386 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3387 struct mips_elf_hash_sort_data hsd;
3388 struct mips_got_info *g;
b49e97c9 3389
d4596a51
RS
3390 if (elf_hash_table (info)->dynsymcount == 0)
3391 return TRUE;
3392
a8028dd0 3393 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3394 BFD_ASSERT (htab != NULL);
3395
a8028dd0 3396 g = htab->got_info;
d4596a51
RS
3397 if (g == NULL)
3398 return TRUE;
f4416af6 3399
b49e97c9 3400 hsd.low = NULL;
23cc69b6
RS
3401 hsd.max_unref_got_dynindx
3402 = hsd.min_got_dynindx
3403 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3404 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3405 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3406 elf_hash_table (info)),
3407 mips_elf_sort_hash_table_f,
3408 &hsd);
3409
3410 /* There should have been enough room in the symbol table to
44c410de 3411 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3412 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3413 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3414 == elf_hash_table (info)->dynsymcount);
3415 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3416 == g->global_gotno);
b49e97c9
TS
3417
3418 /* Now we know which dynamic symbol has the lowest dynamic symbol
3419 table index in the GOT. */
b49e97c9
TS
3420 g->global_gotsym = hsd.low;
3421
b34976b6 3422 return TRUE;
b49e97c9
TS
3423}
3424
3425/* If H needs a GOT entry, assign it the highest available dynamic
3426 index. Otherwise, assign it the lowest available dynamic
3427 index. */
3428
b34976b6 3429static bfd_boolean
9719ad41 3430mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3431{
9719ad41 3432 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9
TS
3433
3434 if (h->root.root.type == bfd_link_hash_warning)
3435 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3436
3437 /* Symbols without dynamic symbol table entries aren't interesting
3438 at all. */
3439 if (h->root.dynindx == -1)
b34976b6 3440 return TRUE;
b49e97c9 3441
634835ae 3442 switch (h->global_got_area)
f4416af6 3443 {
634835ae
RS
3444 case GGA_NONE:
3445 h->root.dynindx = hsd->max_non_got_dynindx++;
3446 break;
0f20cc35 3447
634835ae 3448 case GGA_NORMAL:
0f20cc35
DJ
3449 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3450
b49e97c9
TS
3451 h->root.dynindx = --hsd->min_got_dynindx;
3452 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3453 break;
3454
3455 case GGA_RELOC_ONLY:
3456 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3457
3458 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3459 hsd->low = (struct elf_link_hash_entry *) h;
3460 h->root.dynindx = hsd->max_unref_got_dynindx++;
3461 break;
b49e97c9
TS
3462 }
3463
b34976b6 3464 return TRUE;
b49e97c9
TS
3465}
3466
3467/* If H is a symbol that needs a global GOT entry, but has a dynamic
3468 symbol table index lower than any we've seen to date, record it for
6ccf4795
RS
3469 posterity. FOR_CALL is true if the caller is only interested in
3470 using the GOT entry for calls. */
b49e97c9 3471
b34976b6 3472static bfd_boolean
9719ad41
RS
3473mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3474 bfd *abfd, struct bfd_link_info *info,
6ccf4795 3475 bfd_boolean for_call,
0f20cc35 3476 unsigned char tls_flag)
b49e97c9 3477{
a8028dd0 3478 struct mips_elf_link_hash_table *htab;
634835ae 3479 struct mips_elf_link_hash_entry *hmips;
f4416af6 3480 struct mips_got_entry entry, **loc;
a8028dd0
RS
3481 struct mips_got_info *g;
3482
3483 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3484 BFD_ASSERT (htab != NULL);
3485
634835ae 3486 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795
RS
3487 if (!for_call)
3488 hmips->got_only_for_calls = FALSE;
f4416af6 3489
b49e97c9
TS
3490 /* A global symbol in the GOT must also be in the dynamic symbol
3491 table. */
7c5fcef7
L
3492 if (h->dynindx == -1)
3493 {
3494 switch (ELF_ST_VISIBILITY (h->other))
3495 {
3496 case STV_INTERNAL:
3497 case STV_HIDDEN:
33bb52fb 3498 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3499 break;
3500 }
c152c796 3501 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3502 return FALSE;
7c5fcef7 3503 }
b49e97c9 3504
86324f90 3505 /* Make sure we have a GOT to put this entry into. */
a8028dd0 3506 g = htab->got_info;
86324f90
EC
3507 BFD_ASSERT (g != NULL);
3508
f4416af6
AO
3509 entry.abfd = abfd;
3510 entry.symndx = -1;
3511 entry.d.h = (struct mips_elf_link_hash_entry *) h;
0f20cc35 3512 entry.tls_type = 0;
f4416af6
AO
3513
3514 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3515 INSERT);
3516
b49e97c9
TS
3517 /* If we've already marked this entry as needing GOT space, we don't
3518 need to do it again. */
f4416af6 3519 if (*loc)
0f20cc35
DJ
3520 {
3521 (*loc)->tls_type |= tls_flag;
3522 return TRUE;
3523 }
f4416af6
AO
3524
3525 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3526
3527 if (! *loc)
3528 return FALSE;
143d77c5 3529
f4416af6 3530 entry.gotidx = -1;
0f20cc35
DJ
3531 entry.tls_type = tls_flag;
3532
f4416af6
AO
3533 memcpy (*loc, &entry, sizeof entry);
3534
0f20cc35 3535 if (tls_flag == 0)
634835ae 3536 hmips->global_got_area = GGA_NORMAL;
b49e97c9 3537
b34976b6 3538 return TRUE;
b49e97c9 3539}
f4416af6
AO
3540
3541/* Reserve space in G for a GOT entry containing the value of symbol
3542 SYMNDX in input bfd ABDF, plus ADDEND. */
3543
3544static bfd_boolean
9719ad41 3545mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
a8028dd0 3546 struct bfd_link_info *info,
0f20cc35 3547 unsigned char tls_flag)
f4416af6 3548{
a8028dd0
RS
3549 struct mips_elf_link_hash_table *htab;
3550 struct mips_got_info *g;
f4416af6
AO
3551 struct mips_got_entry entry, **loc;
3552
a8028dd0 3553 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3554 BFD_ASSERT (htab != NULL);
3555
a8028dd0
RS
3556 g = htab->got_info;
3557 BFD_ASSERT (g != NULL);
3558
f4416af6
AO
3559 entry.abfd = abfd;
3560 entry.symndx = symndx;
3561 entry.d.addend = addend;
0f20cc35 3562 entry.tls_type = tls_flag;
f4416af6
AO
3563 loc = (struct mips_got_entry **)
3564 htab_find_slot (g->got_entries, &entry, INSERT);
3565
3566 if (*loc)
0f20cc35
DJ
3567 {
3568 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3569 {
3570 g->tls_gotno += 2;
3571 (*loc)->tls_type |= tls_flag;
3572 }
3573 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3574 {
3575 g->tls_gotno += 1;
3576 (*loc)->tls_type |= tls_flag;
3577 }
3578 return TRUE;
3579 }
f4416af6 3580
0f20cc35
DJ
3581 if (tls_flag != 0)
3582 {
3583 entry.gotidx = -1;
3584 entry.tls_type = tls_flag;
3585 if (tls_flag == GOT_TLS_IE)
3586 g->tls_gotno += 1;
3587 else if (tls_flag == GOT_TLS_GD)
3588 g->tls_gotno += 2;
3589 else if (g->tls_ldm_offset == MINUS_ONE)
3590 {
3591 g->tls_ldm_offset = MINUS_TWO;
3592 g->tls_gotno += 2;
3593 }
3594 }
3595 else
3596 {
3597 entry.gotidx = g->local_gotno++;
3598 entry.tls_type = 0;
3599 }
f4416af6
AO
3600
3601 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3602
3603 if (! *loc)
3604 return FALSE;
143d77c5 3605
f4416af6
AO
3606 memcpy (*loc, &entry, sizeof entry);
3607
3608 return TRUE;
3609}
c224138d
RS
3610
3611/* Return the maximum number of GOT page entries required for RANGE. */
3612
3613static bfd_vma
3614mips_elf_pages_for_range (const struct mips_got_page_range *range)
3615{
3616 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3617}
3618
3a3b6725 3619/* Record that ABFD has a page relocation against symbol SYMNDX and
a8028dd0
RS
3620 that ADDEND is the addend for that relocation.
3621
3622 This function creates an upper bound on the number of GOT slots
3623 required; no attempt is made to combine references to non-overridable
3624 global symbols across multiple input files. */
c224138d
RS
3625
3626static bfd_boolean
a8028dd0
RS
3627mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3628 long symndx, bfd_signed_vma addend)
c224138d 3629{
a8028dd0
RS
3630 struct mips_elf_link_hash_table *htab;
3631 struct mips_got_info *g;
c224138d
RS
3632 struct mips_got_page_entry lookup, *entry;
3633 struct mips_got_page_range **range_ptr, *range;
3634 bfd_vma old_pages, new_pages;
3635 void **loc;
3636
a8028dd0 3637 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3638 BFD_ASSERT (htab != NULL);
3639
a8028dd0
RS
3640 g = htab->got_info;
3641 BFD_ASSERT (g != NULL);
3642
c224138d
RS
3643 /* Find the mips_got_page_entry hash table entry for this symbol. */
3644 lookup.abfd = abfd;
3645 lookup.symndx = symndx;
3646 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3647 if (loc == NULL)
3648 return FALSE;
3649
3650 /* Create a mips_got_page_entry if this is the first time we've
3651 seen the symbol. */
3652 entry = (struct mips_got_page_entry *) *loc;
3653 if (!entry)
3654 {
3655 entry = bfd_alloc (abfd, sizeof (*entry));
3656 if (!entry)
3657 return FALSE;
3658
3659 entry->abfd = abfd;
3660 entry->symndx = symndx;
3661 entry->ranges = NULL;
3662 entry->num_pages = 0;
3663 *loc = entry;
3664 }
3665
3666 /* Skip over ranges whose maximum extent cannot share a page entry
3667 with ADDEND. */
3668 range_ptr = &entry->ranges;
3669 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3670 range_ptr = &(*range_ptr)->next;
3671
3672 /* If we scanned to the end of the list, or found a range whose
3673 minimum extent cannot share a page entry with ADDEND, create
3674 a new singleton range. */
3675 range = *range_ptr;
3676 if (!range || addend < range->min_addend - 0xffff)
3677 {
3678 range = bfd_alloc (abfd, sizeof (*range));
3679 if (!range)
3680 return FALSE;
3681
3682 range->next = *range_ptr;
3683 range->min_addend = addend;
3684 range->max_addend = addend;
3685
3686 *range_ptr = range;
3687 entry->num_pages++;
3688 g->page_gotno++;
3689 return TRUE;
3690 }
3691
3692 /* Remember how many pages the old range contributed. */
3693 old_pages = mips_elf_pages_for_range (range);
3694
3695 /* Update the ranges. */
3696 if (addend < range->min_addend)
3697 range->min_addend = addend;
3698 else if (addend > range->max_addend)
3699 {
3700 if (range->next && addend >= range->next->min_addend - 0xffff)
3701 {
3702 old_pages += mips_elf_pages_for_range (range->next);
3703 range->max_addend = range->next->max_addend;
3704 range->next = range->next->next;
3705 }
3706 else
3707 range->max_addend = addend;
3708 }
3709
3710 /* Record any change in the total estimate. */
3711 new_pages = mips_elf_pages_for_range (range);
3712 if (old_pages != new_pages)
3713 {
3714 entry->num_pages += new_pages - old_pages;
3715 g->page_gotno += new_pages - old_pages;
3716 }
3717
3718 return TRUE;
3719}
33bb52fb
RS
3720
3721/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3722
3723static void
3724mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3725 unsigned int n)
3726{
3727 asection *s;
3728 struct mips_elf_link_hash_table *htab;
3729
3730 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3731 BFD_ASSERT (htab != NULL);
3732
33bb52fb
RS
3733 s = mips_elf_rel_dyn_section (info, FALSE);
3734 BFD_ASSERT (s != NULL);
3735
3736 if (htab->is_vxworks)
3737 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3738 else
3739 {
3740 if (s->size == 0)
3741 {
3742 /* Make room for a null element. */
3743 s->size += MIPS_ELF_REL_SIZE (abfd);
3744 ++s->reloc_count;
3745 }
3746 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3747 }
3748}
3749\f
3750/* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3751 if the GOT entry is for an indirect or warning symbol. */
3752
3753static int
3754mips_elf_check_recreate_got (void **entryp, void *data)
3755{
3756 struct mips_got_entry *entry;
3757 bfd_boolean *must_recreate;
3758
3759 entry = (struct mips_got_entry *) *entryp;
3760 must_recreate = (bfd_boolean *) data;
3761 if (entry->abfd != NULL && entry->symndx == -1)
3762 {
3763 struct mips_elf_link_hash_entry *h;
3764
3765 h = entry->d.h;
3766 if (h->root.root.type == bfd_link_hash_indirect
3767 || h->root.root.type == bfd_link_hash_warning)
3768 {
3769 *must_recreate = TRUE;
3770 return 0;
3771 }
3772 }
3773 return 1;
3774}
3775
3776/* A htab_traverse callback for GOT entries. Add all entries to
3777 hash table *DATA, converting entries for indirect and warning
3778 symbols into entries for the target symbol. Set *DATA to null
3779 on error. */
3780
3781static int
3782mips_elf_recreate_got (void **entryp, void *data)
3783{
3784 htab_t *new_got;
3785 struct mips_got_entry *entry;
3786 void **slot;
3787
3788 new_got = (htab_t *) data;
3789 entry = (struct mips_got_entry *) *entryp;
3790 if (entry->abfd != NULL && entry->symndx == -1)
3791 {
3792 struct mips_elf_link_hash_entry *h;
3793
3794 h = entry->d.h;
3795 while (h->root.root.type == bfd_link_hash_indirect
3796 || h->root.root.type == bfd_link_hash_warning)
634835ae
RS
3797 {
3798 BFD_ASSERT (h->global_got_area == GGA_NONE);
3799 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3800 }
33bb52fb
RS
3801 entry->d.h = h;
3802 }
3803 slot = htab_find_slot (*new_got, entry, INSERT);
3804 if (slot == NULL)
3805 {
3806 *new_got = NULL;
3807 return 0;
3808 }
3809 if (*slot == NULL)
3810 *slot = entry;
3811 else
3812 free (entry);
3813 return 1;
3814}
3815
3816/* If any entries in G->got_entries are for indirect or warning symbols,
3817 replace them with entries for the target symbol. */
3818
3819static bfd_boolean
3820mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3821{
3822 bfd_boolean must_recreate;
3823 htab_t new_got;
3824
3825 must_recreate = FALSE;
3826 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3827 if (must_recreate)
3828 {
3829 new_got = htab_create (htab_size (g->got_entries),
3830 mips_elf_got_entry_hash,
3831 mips_elf_got_entry_eq, NULL);
3832 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3833 if (new_got == NULL)
3834 return FALSE;
3835
3836 /* Each entry in g->got_entries has either been copied to new_got
3837 or freed. Now delete the hash table itself. */
3838 htab_delete (g->got_entries);
3839 g->got_entries = new_got;
3840 }
3841 return TRUE;
3842}
3843
634835ae 3844/* A mips_elf_link_hash_traverse callback for which DATA points
020d7251
RS
3845 to the link_info structure. Count the number of type (3) entries
3846 in the master GOT. */
33bb52fb
RS
3847
3848static int
d4596a51 3849mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 3850{
020d7251 3851 struct bfd_link_info *info;
6ccf4795 3852 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
3853 struct mips_got_info *g;
3854
020d7251 3855 info = (struct bfd_link_info *) data;
6ccf4795
RS
3856 htab = mips_elf_hash_table (info);
3857 g = htab->got_info;
d4596a51 3858 if (h->global_got_area != GGA_NONE)
33bb52fb 3859 {
020d7251
RS
3860 /* Make a final decision about whether the symbol belongs in the
3861 local or global GOT. Symbols that bind locally can (and in the
3862 case of forced-local symbols, must) live in the local GOT.
3863 Those that are aren't in the dynamic symbol table must also
3864 live in the local GOT.
3865
3866 Note that the former condition does not always imply the
3867 latter: symbols do not bind locally if they are completely
3868 undefined. We'll report undefined symbols later if appropriate. */
6ccf4795
RS
3869 if (h->root.dynindx == -1
3870 || (h->got_only_for_calls
3871 ? SYMBOL_CALLS_LOCAL (info, &h->root)
3872 : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
d4596a51 3873 {
020d7251
RS
3874 /* The symbol belongs in the local GOT. We no longer need this
3875 entry if it was only used for relocations; those relocations
3876 will be against the null or section symbol instead of H. */
d4596a51
RS
3877 if (h->global_got_area != GGA_RELOC_ONLY)
3878 g->local_gotno++;
3879 h->global_got_area = GGA_NONE;
3880 }
6ccf4795
RS
3881 else if (htab->is_vxworks
3882 && h->got_only_for_calls
3883 && h->root.plt.offset != MINUS_ONE)
3884 /* On VxWorks, calls can refer directly to the .got.plt entry;
3885 they don't need entries in the regular GOT. .got.plt entries
3886 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
3887 h->global_got_area = GGA_NONE;
d4596a51 3888 else
23cc69b6
RS
3889 {
3890 g->global_gotno++;
3891 if (h->global_got_area == GGA_RELOC_ONLY)
3892 g->reloc_only_gotno++;
3893 }
33bb52fb
RS
3894 }
3895 return 1;
3896}
f4416af6
AO
3897\f
3898/* Compute the hash value of the bfd in a bfd2got hash entry. */
3899
3900static hashval_t
9719ad41 3901mips_elf_bfd2got_entry_hash (const void *entry_)
f4416af6
AO
3902{
3903 const struct mips_elf_bfd2got_hash *entry
3904 = (struct mips_elf_bfd2got_hash *)entry_;
3905
3906 return entry->bfd->id;
3907}
3908
3909/* Check whether two hash entries have the same bfd. */
3910
3911static int
9719ad41 3912mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3913{
3914 const struct mips_elf_bfd2got_hash *e1
3915 = (const struct mips_elf_bfd2got_hash *)entry1;
3916 const struct mips_elf_bfd2got_hash *e2
3917 = (const struct mips_elf_bfd2got_hash *)entry2;
3918
3919 return e1->bfd == e2->bfd;
3920}
3921
bad36eac 3922/* In a multi-got link, determine the GOT to be used for IBFD. G must
f4416af6
AO
3923 be the master GOT data. */
3924
3925static struct mips_got_info *
9719ad41 3926mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
3927{
3928 struct mips_elf_bfd2got_hash e, *p;
3929
3930 if (! g->bfd2got)
3931 return g;
3932
3933 e.bfd = ibfd;
9719ad41 3934 p = htab_find (g->bfd2got, &e);
f4416af6
AO
3935 return p ? p->g : NULL;
3936}
3937
c224138d
RS
3938/* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3939 Return NULL if an error occured. */
f4416af6 3940
c224138d
RS
3941static struct mips_got_info *
3942mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3943 bfd *input_bfd)
f4416af6 3944{
f4416af6 3945 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
c224138d 3946 struct mips_got_info *g;
f4416af6 3947 void **bfdgotp;
143d77c5 3948
c224138d 3949 bfdgot_entry.bfd = input_bfd;
f4416af6 3950 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
c224138d 3951 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
f4416af6 3952
c224138d 3953 if (bfdgot == NULL)
f4416af6 3954 {
c224138d
RS
3955 bfdgot = ((struct mips_elf_bfd2got_hash *)
3956 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
f4416af6 3957 if (bfdgot == NULL)
c224138d 3958 return NULL;
f4416af6
AO
3959
3960 *bfdgotp = bfdgot;
3961
c224138d
RS
3962 g = ((struct mips_got_info *)
3963 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
f4416af6 3964 if (g == NULL)
c224138d
RS
3965 return NULL;
3966
3967 bfdgot->bfd = input_bfd;
3968 bfdgot->g = g;
f4416af6
AO
3969
3970 g->global_gotsym = NULL;
3971 g->global_gotno = 0;
23cc69b6 3972 g->reloc_only_gotno = 0;
f4416af6 3973 g->local_gotno = 0;
c224138d 3974 g->page_gotno = 0;
f4416af6 3975 g->assigned_gotno = -1;
0f20cc35
DJ
3976 g->tls_gotno = 0;
3977 g->tls_assigned_gotno = 0;
3978 g->tls_ldm_offset = MINUS_ONE;
f4416af6 3979 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
9719ad41 3980 mips_elf_multi_got_entry_eq, NULL);
f4416af6 3981 if (g->got_entries == NULL)
c224138d
RS
3982 return NULL;
3983
3984 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3985 mips_got_page_entry_eq, NULL);
3986 if (g->got_page_entries == NULL)
3987 return NULL;
f4416af6
AO
3988
3989 g->bfd2got = NULL;
3990 g->next = NULL;
3991 }
3992
c224138d
RS
3993 return bfdgot->g;
3994}
3995
3996/* A htab_traverse callback for the entries in the master got.
3997 Create one separate got for each bfd that has entries in the global
3998 got, such that we can tell how many local and global entries each
3999 bfd requires. */
4000
4001static int
4002mips_elf_make_got_per_bfd (void **entryp, void *p)
4003{
4004 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4005 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4006 struct mips_got_info *g;
4007
4008 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4009 if (g == NULL)
4010 {
4011 arg->obfd = NULL;
4012 return 0;
4013 }
4014
f4416af6
AO
4015 /* Insert the GOT entry in the bfd's got entry hash table. */
4016 entryp = htab_find_slot (g->got_entries, entry, INSERT);
4017 if (*entryp != NULL)
4018 return 1;
143d77c5 4019
f4416af6
AO
4020 *entryp = entry;
4021
0f20cc35
DJ
4022 if (entry->tls_type)
4023 {
4024 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4025 g->tls_gotno += 2;
4026 if (entry->tls_type & GOT_TLS_IE)
4027 g->tls_gotno += 1;
4028 }
020d7251 4029 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
f4416af6
AO
4030 ++g->local_gotno;
4031 else
4032 ++g->global_gotno;
4033
4034 return 1;
4035}
4036
c224138d
RS
4037/* A htab_traverse callback for the page entries in the master got.
4038 Associate each page entry with the bfd's got. */
4039
4040static int
4041mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
4042{
4043 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
4044 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
4045 struct mips_got_info *g;
4046
4047 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4048 if (g == NULL)
4049 {
4050 arg->obfd = NULL;
4051 return 0;
4052 }
4053
4054 /* Insert the GOT entry in the bfd's got entry hash table. */
4055 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4056 if (*entryp != NULL)
4057 return 1;
4058
4059 *entryp = entry;
4060 g->page_gotno += entry->num_pages;
4061 return 1;
4062}
4063
4064/* Consider merging the got described by BFD2GOT with TO, using the
4065 information given by ARG. Return -1 if this would lead to overflow,
4066 1 if they were merged successfully, and 0 if a merge failed due to
4067 lack of memory. (These values are chosen so that nonnegative return
4068 values can be returned by a htab_traverse callback.) */
4069
4070static int
4071mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4072 struct mips_got_info *to,
4073 struct mips_elf_got_per_bfd_arg *arg)
4074{
4075 struct mips_got_info *from = bfd2got->g;
4076 unsigned int estimate;
4077
4078 /* Work out how many page entries we would need for the combined GOT. */
4079 estimate = arg->max_pages;
4080 if (estimate >= from->page_gotno + to->page_gotno)
4081 estimate = from->page_gotno + to->page_gotno;
4082
4083 /* And conservatively estimate how many local, global and TLS entries
4084 would be needed. */
4085 estimate += (from->local_gotno
4086 + from->global_gotno
4087 + from->tls_gotno
4088 + to->local_gotno
4089 + to->global_gotno
4090 + to->tls_gotno);
4091
4092 /* Bail out if the combined GOT might be too big. */
4093 if (estimate > arg->max_count)
4094 return -1;
4095
4096 /* Commit to the merge. Record that TO is now the bfd for this got. */
4097 bfd2got->g = to;
4098
4099 /* Transfer the bfd's got information from FROM to TO. */
4100 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4101 if (arg->obfd == NULL)
4102 return 0;
4103
4104 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4105 if (arg->obfd == NULL)
4106 return 0;
4107
4108 /* We don't have to worry about releasing memory of the actual
4109 got entries, since they're all in the master got_entries hash
4110 table anyway. */
4111 htab_delete (from->got_entries);
4112 htab_delete (from->got_page_entries);
4113 return 1;
4114}
4115
f4416af6
AO
4116/* Attempt to merge gots of different input bfds. Try to use as much
4117 as possible of the primary got, since it doesn't require explicit
4118 dynamic relocations, but don't use bfds that would reference global
4119 symbols out of the addressable range. Failing the primary got,
4120 attempt to merge with the current got, or finish the current got
4121 and then make make the new got current. */
4122
4123static int
9719ad41 4124mips_elf_merge_gots (void **bfd2got_, void *p)
f4416af6
AO
4125{
4126 struct mips_elf_bfd2got_hash *bfd2got
4127 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4128 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
c224138d
RS
4129 struct mips_got_info *g;
4130 unsigned int estimate;
4131 int result;
4132
4133 g = bfd2got->g;
4134
4135 /* Work out the number of page, local and TLS entries. */
4136 estimate = arg->max_pages;
4137 if (estimate > g->page_gotno)
4138 estimate = g->page_gotno;
4139 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4140
4141 /* We place TLS GOT entries after both locals and globals. The globals
4142 for the primary GOT may overflow the normal GOT size limit, so be
4143 sure not to merge a GOT which requires TLS with the primary GOT in that
4144 case. This doesn't affect non-primary GOTs. */
c224138d 4145 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4146
c224138d 4147 if (estimate <= arg->max_count)
f4416af6 4148 {
c224138d
RS
4149 /* If we don't have a primary GOT, use it as
4150 a starting point for the primary GOT. */
4151 if (!arg->primary)
4152 {
4153 arg->primary = bfd2got->g;
4154 return 1;
4155 }
f4416af6 4156
c224138d
RS
4157 /* Try merging with the primary GOT. */
4158 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4159 if (result >= 0)
4160 return result;
f4416af6 4161 }
c224138d 4162
f4416af6 4163 /* If we can merge with the last-created got, do it. */
c224138d 4164 if (arg->current)
f4416af6 4165 {
c224138d
RS
4166 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4167 if (result >= 0)
4168 return result;
f4416af6 4169 }
c224138d 4170
f4416af6
AO
4171 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4172 fits; if it turns out that it doesn't, we'll get relocation
4173 overflows anyway. */
c224138d
RS
4174 g->next = arg->current;
4175 arg->current = g;
0f20cc35
DJ
4176
4177 return 1;
4178}
4179
ead49a57
RS
4180/* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4181 is null iff there is just a single GOT. */
0f20cc35
DJ
4182
4183static int
4184mips_elf_initialize_tls_index (void **entryp, void *p)
4185{
4186 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4187 struct mips_got_info *g = p;
ead49a57 4188 bfd_vma next_index;
cbf2cba4 4189 unsigned char tls_type;
0f20cc35
DJ
4190
4191 /* We're only interested in TLS symbols. */
4192 if (entry->tls_type == 0)
4193 return 1;
4194
ead49a57
RS
4195 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4196
4197 if (entry->symndx == -1 && g->next == NULL)
0f20cc35 4198 {
ead49a57
RS
4199 /* A type (3) got entry in the single-GOT case. We use the symbol's
4200 hash table entry to track its index. */
4201 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4202 return 1;
4203 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4204 entry->d.h->tls_got_offset = next_index;
cbf2cba4 4205 tls_type = entry->d.h->tls_type;
ead49a57
RS
4206 }
4207 else
4208 {
4209 if (entry->tls_type & GOT_TLS_LDM)
0f20cc35 4210 {
ead49a57
RS
4211 /* There are separate mips_got_entry objects for each input bfd
4212 that requires an LDM entry. Make sure that all LDM entries in
4213 a GOT resolve to the same index. */
4214 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4005427f 4215 {
ead49a57 4216 entry->gotidx = g->tls_ldm_offset;
4005427f
RS
4217 return 1;
4218 }
ead49a57 4219 g->tls_ldm_offset = next_index;
0f20cc35 4220 }
ead49a57 4221 entry->gotidx = next_index;
cbf2cba4 4222 tls_type = entry->tls_type;
f4416af6
AO
4223 }
4224
ead49a57 4225 /* Account for the entries we've just allocated. */
cbf2cba4 4226 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
0f20cc35 4227 g->tls_assigned_gotno += 2;
cbf2cba4 4228 if (tls_type & GOT_TLS_IE)
0f20cc35
DJ
4229 g->tls_assigned_gotno += 1;
4230
f4416af6
AO
4231 return 1;
4232}
4233
4234/* If passed a NULL mips_got_info in the argument, set the marker used
4235 to tell whether a global symbol needs a got entry (in the primary
4236 got) to the given VALUE.
4237
4238 If passed a pointer G to a mips_got_info in the argument (it must
4239 not be the primary GOT), compute the offset from the beginning of
4240 the (primary) GOT section to the entry in G corresponding to the
4241 global symbol. G's assigned_gotno must contain the index of the
4242 first available global GOT entry in G. VALUE must contain the size
4243 of a GOT entry in bytes. For each global GOT entry that requires a
4244 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4cc11e76 4245 marked as not eligible for lazy resolution through a function
f4416af6
AO
4246 stub. */
4247static int
9719ad41 4248mips_elf_set_global_got_offset (void **entryp, void *p)
f4416af6
AO
4249{
4250 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4251 struct mips_elf_set_global_got_offset_arg *arg
4252 = (struct mips_elf_set_global_got_offset_arg *)p;
4253 struct mips_got_info *g = arg->g;
4254
0f20cc35
DJ
4255 if (g && entry->tls_type != GOT_NORMAL)
4256 arg->needed_relocs +=
4257 mips_tls_got_relocs (arg->info, entry->tls_type,
4258 entry->symndx == -1 ? &entry->d.h->root : NULL);
4259
634835ae
RS
4260 if (entry->abfd != NULL
4261 && entry->symndx == -1
4262 && entry->d.h->global_got_area != GGA_NONE)
f4416af6
AO
4263 {
4264 if (g)
4265 {
4266 BFD_ASSERT (g->global_gotsym == NULL);
4267
4268 entry->gotidx = arg->value * (long) g->assigned_gotno++;
f4416af6
AO
4269 if (arg->info->shared
4270 || (elf_hash_table (arg->info)->dynamic_sections_created
f5385ebf
AM
4271 && entry->d.h->root.def_dynamic
4272 && !entry->d.h->root.def_regular))
f4416af6
AO
4273 ++arg->needed_relocs;
4274 }
4275 else
634835ae 4276 entry->d.h->global_got_area = arg->value;
f4416af6
AO
4277 }
4278
4279 return 1;
4280}
4281
33bb52fb
RS
4282/* A htab_traverse callback for GOT entries for which DATA is the
4283 bfd_link_info. Forbid any global symbols from having traditional
4284 lazy-binding stubs. */
4285
0626d451 4286static int
33bb52fb 4287mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4288{
33bb52fb
RS
4289 struct bfd_link_info *info;
4290 struct mips_elf_link_hash_table *htab;
4291 struct mips_got_entry *entry;
0626d451 4292
33bb52fb
RS
4293 entry = (struct mips_got_entry *) *entryp;
4294 info = (struct bfd_link_info *) data;
4295 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4296 BFD_ASSERT (htab != NULL);
4297
0626d451
RS
4298 if (entry->abfd != NULL
4299 && entry->symndx == -1
33bb52fb 4300 && entry->d.h->needs_lazy_stub)
f4416af6 4301 {
33bb52fb
RS
4302 entry->d.h->needs_lazy_stub = FALSE;
4303 htab->lazy_stub_count--;
f4416af6 4304 }
143d77c5 4305
f4416af6
AO
4306 return 1;
4307}
4308
f4416af6
AO
4309/* Return the offset of an input bfd IBFD's GOT from the beginning of
4310 the primary GOT. */
4311static bfd_vma
9719ad41 4312mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
4313{
4314 if (g->bfd2got == NULL)
4315 return 0;
4316
4317 g = mips_elf_got_for_ibfd (g, ibfd);
4318 if (! g)
4319 return 0;
4320
4321 BFD_ASSERT (g->next);
4322
4323 g = g->next;
143d77c5 4324
0f20cc35
DJ
4325 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4326 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4327}
4328
4329/* Turn a single GOT that is too big for 16-bit addressing into
4330 a sequence of GOTs, each one 16-bit addressable. */
4331
4332static bfd_boolean
9719ad41 4333mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4334 asection *got, bfd_size_type pages)
f4416af6 4335{
a8028dd0 4336 struct mips_elf_link_hash_table *htab;
f4416af6
AO
4337 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4338 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
a8028dd0 4339 struct mips_got_info *g, *gg;
33bb52fb
RS
4340 unsigned int assign, needed_relocs;
4341 bfd *dynobj;
f4416af6 4342
33bb52fb 4343 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4344 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4345 BFD_ASSERT (htab != NULL);
4346
a8028dd0 4347 g = htab->got_info;
f4416af6 4348 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
9719ad41 4349 mips_elf_bfd2got_entry_eq, NULL);
f4416af6
AO
4350 if (g->bfd2got == NULL)
4351 return FALSE;
4352
4353 got_per_bfd_arg.bfd2got = g->bfd2got;
4354 got_per_bfd_arg.obfd = abfd;
4355 got_per_bfd_arg.info = info;
4356
4357 /* Count how many GOT entries each input bfd requires, creating a
4358 map from bfd to got info while at that. */
f4416af6
AO
4359 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4360 if (got_per_bfd_arg.obfd == NULL)
4361 return FALSE;
4362
c224138d
RS
4363 /* Also count how many page entries each input bfd requires. */
4364 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4365 &got_per_bfd_arg);
4366 if (got_per_bfd_arg.obfd == NULL)
4367 return FALSE;
4368
f4416af6
AO
4369 got_per_bfd_arg.current = NULL;
4370 got_per_bfd_arg.primary = NULL;
0a44bf69 4371 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4372 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4373 - htab->reserved_gotno);
c224138d 4374 got_per_bfd_arg.max_pages = pages;
0f20cc35
DJ
4375 /* The number of globals that will be included in the primary GOT.
4376 See the calls to mips_elf_set_global_got_offset below for more
4377 information. */
4378 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4379
4380 /* Try to merge the GOTs of input bfds together, as long as they
4381 don't seem to exceed the maximum GOT size, choosing one of them
4382 to be the primary GOT. */
4383 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4384 if (got_per_bfd_arg.obfd == NULL)
4385 return FALSE;
4386
0f20cc35 4387 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6
AO
4388 if (got_per_bfd_arg.primary == NULL)
4389 {
4390 g->next = (struct mips_got_info *)
4391 bfd_alloc (abfd, sizeof (struct mips_got_info));
4392 if (g->next == NULL)
4393 return FALSE;
4394
4395 g->next->global_gotsym = NULL;
4396 g->next->global_gotno = 0;
23cc69b6 4397 g->next->reloc_only_gotno = 0;
f4416af6 4398 g->next->local_gotno = 0;
c224138d 4399 g->next->page_gotno = 0;
0f20cc35 4400 g->next->tls_gotno = 0;
f4416af6 4401 g->next->assigned_gotno = 0;
0f20cc35
DJ
4402 g->next->tls_assigned_gotno = 0;
4403 g->next->tls_ldm_offset = MINUS_ONE;
f4416af6
AO
4404 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4405 mips_elf_multi_got_entry_eq,
9719ad41 4406 NULL);
f4416af6
AO
4407 if (g->next->got_entries == NULL)
4408 return FALSE;
c224138d
RS
4409 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4410 mips_got_page_entry_eq,
4411 NULL);
4412 if (g->next->got_page_entries == NULL)
4413 return FALSE;
f4416af6
AO
4414 g->next->bfd2got = NULL;
4415 }
4416 else
4417 g->next = got_per_bfd_arg.primary;
4418 g->next->next = got_per_bfd_arg.current;
4419
4420 /* GG is now the master GOT, and G is the primary GOT. */
4421 gg = g;
4422 g = g->next;
4423
4424 /* Map the output bfd to the primary got. That's what we're going
4425 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4426 didn't mark in check_relocs, and we want a quick way to find it.
4427 We can't just use gg->next because we're going to reverse the
4428 list. */
4429 {
4430 struct mips_elf_bfd2got_hash *bfdgot;
4431 void **bfdgotp;
143d77c5 4432
f4416af6
AO
4433 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4434 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4435
4436 if (bfdgot == NULL)
4437 return FALSE;
4438
4439 bfdgot->bfd = abfd;
4440 bfdgot->g = g;
4441 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4442
4443 BFD_ASSERT (*bfdgotp == NULL);
4444 *bfdgotp = bfdgot;
4445 }
4446
634835ae
RS
4447 /* Every symbol that is referenced in a dynamic relocation must be
4448 present in the primary GOT, so arrange for them to appear after
4449 those that are actually referenced. */
23cc69b6 4450 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4451 g->global_gotno = gg->global_gotno;
f4416af6 4452
f4416af6 4453 set_got_offset_arg.g = NULL;
634835ae 4454 set_got_offset_arg.value = GGA_RELOC_ONLY;
f4416af6
AO
4455 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4456 &set_got_offset_arg);
634835ae 4457 set_got_offset_arg.value = GGA_NORMAL;
f4416af6
AO
4458 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4459 &set_got_offset_arg);
f4416af6
AO
4460
4461 /* Now go through the GOTs assigning them offset ranges.
4462 [assigned_gotno, local_gotno[ will be set to the range of local
4463 entries in each GOT. We can then compute the end of a GOT by
4464 adding local_gotno to global_gotno. We reverse the list and make
4465 it circular since then we'll be able to quickly compute the
4466 beginning of a GOT, by computing the end of its predecessor. To
4467 avoid special cases for the primary GOT, while still preserving
4468 assertions that are valid for both single- and multi-got links,
4469 we arrange for the main got struct to have the right number of
4470 global entries, but set its local_gotno such that the initial
4471 offset of the primary GOT is zero. Remember that the primary GOT
4472 will become the last item in the circular linked list, so it
4473 points back to the master GOT. */
4474 gg->local_gotno = -g->global_gotno;
4475 gg->global_gotno = g->global_gotno;
0f20cc35 4476 gg->tls_gotno = 0;
f4416af6
AO
4477 assign = 0;
4478 gg->next = gg;
4479
4480 do
4481 {
4482 struct mips_got_info *gn;
4483
861fb55a 4484 assign += htab->reserved_gotno;
f4416af6 4485 g->assigned_gotno = assign;
c224138d
RS
4486 g->local_gotno += assign;
4487 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4488 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4489
ead49a57
RS
4490 /* Take g out of the direct list, and push it onto the reversed
4491 list that gg points to. g->next is guaranteed to be nonnull after
4492 this operation, as required by mips_elf_initialize_tls_index. */
4493 gn = g->next;
4494 g->next = gg->next;
4495 gg->next = g;
4496
0f20cc35
DJ
4497 /* Set up any TLS entries. We always place the TLS entries after
4498 all non-TLS entries. */
4499 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4500 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
f4416af6 4501
ead49a57 4502 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4503 g = gn;
0626d451 4504
33bb52fb
RS
4505 /* Forbid global symbols in every non-primary GOT from having
4506 lazy-binding stubs. */
0626d451 4507 if (g)
33bb52fb 4508 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4509 }
4510 while (g);
4511
eea6121a 4512 got->size = (gg->next->local_gotno
33bb52fb
RS
4513 + gg->next->global_gotno
4514 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4515
4516 needed_relocs = 0;
4517 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4518 set_got_offset_arg.info = info;
4519 for (g = gg->next; g && g->next != gg; g = g->next)
4520 {
4521 unsigned int save_assign;
4522
4523 /* Assign offsets to global GOT entries. */
4524 save_assign = g->assigned_gotno;
4525 g->assigned_gotno = g->local_gotno;
4526 set_got_offset_arg.g = g;
4527 set_got_offset_arg.needed_relocs = 0;
4528 htab_traverse (g->got_entries,
4529 mips_elf_set_global_got_offset,
4530 &set_got_offset_arg);
4531 needed_relocs += set_got_offset_arg.needed_relocs;
4532 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4533
4534 g->assigned_gotno = save_assign;
4535 if (info->shared)
4536 {
4537 needed_relocs += g->local_gotno - g->assigned_gotno;
4538 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4539 + g->next->global_gotno
4540 + g->next->tls_gotno
861fb55a 4541 + htab->reserved_gotno);
33bb52fb
RS
4542 }
4543 }
4544
4545 if (needed_relocs)
4546 mips_elf_allocate_dynamic_relocations (dynobj, info,
4547 needed_relocs);
143d77c5 4548
f4416af6
AO
4549 return TRUE;
4550}
143d77c5 4551
b49e97c9
TS
4552\f
4553/* Returns the first relocation of type r_type found, beginning with
4554 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4555
4556static const Elf_Internal_Rela *
9719ad41
RS
4557mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4558 const Elf_Internal_Rela *relocation,
4559 const Elf_Internal_Rela *relend)
b49e97c9 4560{
c000e262
TS
4561 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4562
b49e97c9
TS
4563 while (relocation < relend)
4564 {
c000e262
TS
4565 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4566 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4567 return relocation;
4568
4569 ++relocation;
4570 }
4571
4572 /* We didn't find it. */
b49e97c9
TS
4573 return NULL;
4574}
4575
020d7251 4576/* Return whether an input relocation is against a local symbol. */
b49e97c9 4577
b34976b6 4578static bfd_boolean
9719ad41
RS
4579mips_elf_local_relocation_p (bfd *input_bfd,
4580 const Elf_Internal_Rela *relocation,
020d7251 4581 asection **local_sections)
b49e97c9
TS
4582{
4583 unsigned long r_symndx;
4584 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
4585 size_t extsymoff;
4586
4587 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4588 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4589 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4590
4591 if (r_symndx < extsymoff)
b34976b6 4592 return TRUE;
b49e97c9 4593 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4594 return TRUE;
b49e97c9 4595
b34976b6 4596 return FALSE;
b49e97c9
TS
4597}
4598\f
4599/* Sign-extend VALUE, which has the indicated number of BITS. */
4600
a7ebbfdf 4601bfd_vma
9719ad41 4602_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4603{
4604 if (value & ((bfd_vma) 1 << (bits - 1)))
4605 /* VALUE is negative. */
4606 value |= ((bfd_vma) - 1) << bits;
4607
4608 return value;
4609}
4610
4611/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4612 range expressible by a signed number with the indicated number of
b49e97c9
TS
4613 BITS. */
4614
b34976b6 4615static bfd_boolean
9719ad41 4616mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4617{
4618 bfd_signed_vma svalue = (bfd_signed_vma) value;
4619
4620 if (svalue > (1 << (bits - 1)) - 1)
4621 /* The value is too big. */
b34976b6 4622 return TRUE;
b49e97c9
TS
4623 else if (svalue < -(1 << (bits - 1)))
4624 /* The value is too small. */
b34976b6 4625 return TRUE;
b49e97c9
TS
4626
4627 /* All is well. */
b34976b6 4628 return FALSE;
b49e97c9
TS
4629}
4630
4631/* Calculate the %high function. */
4632
4633static bfd_vma
9719ad41 4634mips_elf_high (bfd_vma value)
b49e97c9
TS
4635{
4636 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4637}
4638
4639/* Calculate the %higher function. */
4640
4641static bfd_vma
9719ad41 4642mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4643{
4644#ifdef BFD64
4645 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4646#else
4647 abort ();
c5ae1840 4648 return MINUS_ONE;
b49e97c9
TS
4649#endif
4650}
4651
4652/* Calculate the %highest function. */
4653
4654static bfd_vma
9719ad41 4655mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4656{
4657#ifdef BFD64
b15e6682 4658 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4659#else
4660 abort ();
c5ae1840 4661 return MINUS_ONE;
b49e97c9
TS
4662#endif
4663}
4664\f
4665/* Create the .compact_rel section. */
4666
b34976b6 4667static bfd_boolean
9719ad41
RS
4668mips_elf_create_compact_rel_section
4669 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4670{
4671 flagword flags;
4672 register asection *s;
4673
4674 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4675 {
4676 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4677 | SEC_READONLY);
4678
3496cb2a 4679 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4680 if (s == NULL
b49e97c9
TS
4681 || ! bfd_set_section_alignment (abfd, s,
4682 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4683 return FALSE;
b49e97c9 4684
eea6121a 4685 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4686 }
4687
b34976b6 4688 return TRUE;
b49e97c9
TS
4689}
4690
4691/* Create the .got section to hold the global offset table. */
4692
b34976b6 4693static bfd_boolean
23cc69b6 4694mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
4695{
4696 flagword flags;
4697 register asection *s;
4698 struct elf_link_hash_entry *h;
14a793b2 4699 struct bfd_link_hash_entry *bh;
b49e97c9
TS
4700 struct mips_got_info *g;
4701 bfd_size_type amt;
0a44bf69
RS
4702 struct mips_elf_link_hash_table *htab;
4703
4704 htab = mips_elf_hash_table (info);
4dfe6ac6 4705 BFD_ASSERT (htab != NULL);
b49e97c9
TS
4706
4707 /* This function may be called more than once. */
23cc69b6
RS
4708 if (htab->sgot)
4709 return TRUE;
b49e97c9
TS
4710
4711 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4712 | SEC_LINKER_CREATED);
4713
72b4917c
TS
4714 /* We have to use an alignment of 2**4 here because this is hardcoded
4715 in the function stub generation and in the linker script. */
3496cb2a 4716 s = bfd_make_section_with_flags (abfd, ".got", flags);
b49e97c9 4717 if (s == NULL
72b4917c 4718 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 4719 return FALSE;
a8028dd0 4720 htab->sgot = s;
b49e97c9
TS
4721
4722 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4723 linker script because we don't want to define the symbol if we
4724 are not creating a global offset table. */
14a793b2 4725 bh = NULL;
b49e97c9
TS
4726 if (! (_bfd_generic_link_add_one_symbol
4727 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 4728 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 4729 return FALSE;
14a793b2
AM
4730
4731 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
4732 h->non_elf = 0;
4733 h->def_regular = 1;
b49e97c9 4734 h->type = STT_OBJECT;
d329bcd1 4735 elf_hash_table (info)->hgot = h;
b49e97c9
TS
4736
4737 if (info->shared
c152c796 4738 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4739 return FALSE;
b49e97c9 4740
b49e97c9 4741 amt = sizeof (struct mips_got_info);
9719ad41 4742 g = bfd_alloc (abfd, amt);
b49e97c9 4743 if (g == NULL)
b34976b6 4744 return FALSE;
b49e97c9 4745 g->global_gotsym = NULL;
e3d54347 4746 g->global_gotno = 0;
23cc69b6 4747 g->reloc_only_gotno = 0;
0f20cc35 4748 g->tls_gotno = 0;
861fb55a 4749 g->local_gotno = 0;
c224138d 4750 g->page_gotno = 0;
861fb55a 4751 g->assigned_gotno = 0;
f4416af6
AO
4752 g->bfd2got = NULL;
4753 g->next = NULL;
0f20cc35 4754 g->tls_ldm_offset = MINUS_ONE;
b15e6682 4755 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
9719ad41 4756 mips_elf_got_entry_eq, NULL);
b15e6682
AO
4757 if (g->got_entries == NULL)
4758 return FALSE;
c224138d
RS
4759 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4760 mips_got_page_entry_eq, NULL);
4761 if (g->got_page_entries == NULL)
4762 return FALSE;
a8028dd0 4763 htab->got_info = g;
f0abc2a1 4764 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
4765 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4766
861fb55a
DJ
4767 /* We also need a .got.plt section when generating PLTs. */
4768 s = bfd_make_section_with_flags (abfd, ".got.plt",
4769 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4770 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4771 if (s == NULL)
4772 return FALSE;
4773 htab->sgotplt = s;
0a44bf69 4774
b34976b6 4775 return TRUE;
b49e97c9 4776}
b49e97c9 4777\f
0a44bf69
RS
4778/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4779 __GOTT_INDEX__ symbols. These symbols are only special for
4780 shared objects; they are not used in executables. */
4781
4782static bfd_boolean
4783is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4784{
4785 return (mips_elf_hash_table (info)->is_vxworks
4786 && info->shared
4787 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4788 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4789}
861fb55a
DJ
4790
4791/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4792 require an la25 stub. See also mips_elf_local_pic_function_p,
4793 which determines whether the destination function ever requires a
4794 stub. */
4795
4796static bfd_boolean
4797mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4798{
4799 /* We specifically ignore branches and jumps from EF_PIC objects,
4800 where the onus is on the compiler or programmer to perform any
4801 necessary initialization of $25. Sometimes such initialization
4802 is unnecessary; for example, -mno-shared functions do not use
4803 the incoming value of $25, and may therefore be called directly. */
4804 if (PIC_OBJECT_P (input_bfd))
4805 return FALSE;
4806
4807 switch (r_type)
4808 {
4809 case R_MIPS_26:
4810 case R_MIPS_PC16:
4811 case R_MIPS16_26:
4812 return TRUE;
4813
4814 default:
4815 return FALSE;
4816 }
4817}
0a44bf69 4818\f
b49e97c9
TS
4819/* Calculate the value produced by the RELOCATION (which comes from
4820 the INPUT_BFD). The ADDEND is the addend to use for this
4821 RELOCATION; RELOCATION->R_ADDEND is ignored.
4822
4823 The result of the relocation calculation is stored in VALUEP.
38a7df63
CF
4824 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4825 is a MIPS16 jump to non-MIPS16 code, or vice versa.
b49e97c9
TS
4826
4827 This function returns bfd_reloc_continue if the caller need take no
4828 further action regarding this relocation, bfd_reloc_notsupported if
4829 something goes dramatically wrong, bfd_reloc_overflow if an
4830 overflow occurs, and bfd_reloc_ok to indicate success. */
4831
4832static bfd_reloc_status_type
9719ad41
RS
4833mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4834 asection *input_section,
4835 struct bfd_link_info *info,
4836 const Elf_Internal_Rela *relocation,
4837 bfd_vma addend, reloc_howto_type *howto,
4838 Elf_Internal_Sym *local_syms,
4839 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
4840 const char **namep,
4841 bfd_boolean *cross_mode_jump_p,
9719ad41 4842 bfd_boolean save_addend)
b49e97c9
TS
4843{
4844 /* The eventual value we will return. */
4845 bfd_vma value;
4846 /* The address of the symbol against which the relocation is
4847 occurring. */
4848 bfd_vma symbol = 0;
4849 /* The final GP value to be used for the relocatable, executable, or
4850 shared object file being produced. */
0a61c8c2 4851 bfd_vma gp;
b49e97c9
TS
4852 /* The place (section offset or address) of the storage unit being
4853 relocated. */
4854 bfd_vma p;
4855 /* The value of GP used to create the relocatable object. */
0a61c8c2 4856 bfd_vma gp0;
b49e97c9
TS
4857 /* The offset into the global offset table at which the address of
4858 the relocation entry symbol, adjusted by the addend, resides
4859 during execution. */
4860 bfd_vma g = MINUS_ONE;
4861 /* The section in which the symbol referenced by the relocation is
4862 located. */
4863 asection *sec = NULL;
4864 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 4865 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 4866 symbol. */
b34976b6
AM
4867 bfd_boolean local_p, was_local_p;
4868 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4869 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
4870 /* TRUE if the symbol referred to by this relocation is
4871 "__gnu_local_gp". */
4872 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
4873 Elf_Internal_Shdr *symtab_hdr;
4874 size_t extsymoff;
4875 unsigned long r_symndx;
4876 int r_type;
b34976b6 4877 /* TRUE if overflow occurred during the calculation of the
b49e97c9 4878 relocation value. */
b34976b6
AM
4879 bfd_boolean overflowed_p;
4880 /* TRUE if this relocation refers to a MIPS16 function. */
4881 bfd_boolean target_is_16_bit_code_p = FALSE;
0a44bf69
RS
4882 struct mips_elf_link_hash_table *htab;
4883 bfd *dynobj;
4884
4885 dynobj = elf_hash_table (info)->dynobj;
4886 htab = mips_elf_hash_table (info);
4dfe6ac6 4887 BFD_ASSERT (htab != NULL);
b49e97c9
TS
4888
4889 /* Parse the relocation. */
4890 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4891 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4892 p = (input_section->output_section->vma
4893 + input_section->output_offset
4894 + relocation->r_offset);
4895
4896 /* Assume that there will be no overflow. */
b34976b6 4897 overflowed_p = FALSE;
b49e97c9
TS
4898
4899 /* Figure out whether or not the symbol is local, and get the offset
4900 used in the array of hash table entries. */
4901 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4902 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 4903 local_sections);
bce03d3d 4904 was_local_p = local_p;
b49e97c9
TS
4905 if (! elf_bad_symtab (input_bfd))
4906 extsymoff = symtab_hdr->sh_info;
4907 else
4908 {
4909 /* The symbol table does not follow the rule that local symbols
4910 must come before globals. */
4911 extsymoff = 0;
4912 }
4913
4914 /* Figure out the value of the symbol. */
4915 if (local_p)
4916 {
4917 Elf_Internal_Sym *sym;
4918
4919 sym = local_syms + r_symndx;
4920 sec = local_sections[r_symndx];
4921
4922 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
4923 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4924 || (sec->flags & SEC_MERGE))
b49e97c9 4925 symbol += sym->st_value;
d4df96e6
L
4926 if ((sec->flags & SEC_MERGE)
4927 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4928 {
4929 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4930 addend -= symbol;
4931 addend += sec->output_section->vma + sec->output_offset;
4932 }
b49e97c9
TS
4933
4934 /* MIPS16 text labels should be treated as odd. */
30c09090 4935 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
4936 ++symbol;
4937
4938 /* Record the name of this symbol, for our caller. */
4939 *namep = bfd_elf_string_from_elf_section (input_bfd,
4940 symtab_hdr->sh_link,
4941 sym->st_name);
4942 if (*namep == '\0')
4943 *namep = bfd_section_name (input_bfd, sec);
4944
30c09090 4945 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
b49e97c9
TS
4946 }
4947 else
4948 {
560e09e9
NC
4949 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4950
b49e97c9
TS
4951 /* For global symbols we look up the symbol in the hash-table. */
4952 h = ((struct mips_elf_link_hash_entry *)
4953 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4954 /* Find the real hash-table entry for this symbol. */
4955 while (h->root.root.type == bfd_link_hash_indirect
4956 || h->root.root.type == bfd_link_hash_warning)
4957 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4958
4959 /* Record the name of this symbol, for our caller. */
4960 *namep = h->root.root.root.string;
4961
4962 /* See if this is the special _gp_disp symbol. Note that such a
4963 symbol must always be a global symbol. */
560e09e9 4964 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
4965 && ! NEWABI_P (input_bfd))
4966 {
4967 /* Relocations against _gp_disp are permitted only with
4968 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 4969 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
4970 return bfd_reloc_notsupported;
4971
b34976b6 4972 gp_disp_p = TRUE;
b49e97c9 4973 }
bbe506e8
TS
4974 /* See if this is the special _gp symbol. Note that such a
4975 symbol must always be a global symbol. */
4976 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4977 gnu_local_gp_p = TRUE;
4978
4979
b49e97c9
TS
4980 /* If this symbol is defined, calculate its address. Note that
4981 _gp_disp is a magic symbol, always implicitly defined by the
4982 linker, so it's inappropriate to check to see whether or not
4983 its defined. */
4984 else if ((h->root.root.type == bfd_link_hash_defined
4985 || h->root.root.type == bfd_link_hash_defweak)
4986 && h->root.root.u.def.section)
4987 {
4988 sec = h->root.root.u.def.section;
4989 if (sec->output_section)
4990 symbol = (h->root.root.u.def.value
4991 + sec->output_section->vma
4992 + sec->output_offset);
4993 else
4994 symbol = h->root.root.u.def.value;
4995 }
4996 else if (h->root.root.type == bfd_link_hash_undefweak)
4997 /* We allow relocations against undefined weak symbols, giving
4998 it the value zero, so that you can undefined weak functions
4999 and check to see if they exist by looking at their
5000 addresses. */
5001 symbol = 0;
59c2e50f 5002 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5003 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5004 symbol = 0;
a4d0f181
TS
5005 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5006 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5007 {
5008 /* If this is a dynamic link, we should have created a
5009 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5010 in in _bfd_mips_elf_create_dynamic_sections.
5011 Otherwise, we should define the symbol with a value of 0.
5012 FIXME: It should probably get into the symbol table
5013 somehow as well. */
5014 BFD_ASSERT (! info->shared);
5015 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5016 symbol = 0;
5017 }
5e2b0d47
NC
5018 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5019 {
5020 /* This is an optional symbol - an Irix specific extension to the
5021 ELF spec. Ignore it for now.
5022 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5023 than simply ignoring them, but we do not handle this for now.
5024 For information see the "64-bit ELF Object File Specification"
5025 which is available from here:
5026 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5027 symbol = 0;
5028 }
e7e2196d
MR
5029 else if ((*info->callbacks->undefined_symbol)
5030 (info, h->root.root.root.string, input_bfd,
5031 input_section, relocation->r_offset,
5032 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5033 || ELF_ST_VISIBILITY (h->root.other)))
5034 {
5035 return bfd_reloc_undefined;
5036 }
b49e97c9
TS
5037 else
5038 {
e7e2196d 5039 return bfd_reloc_notsupported;
b49e97c9
TS
5040 }
5041
30c09090 5042 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
b49e97c9
TS
5043 }
5044
738e5348
RS
5045 /* If this is a reference to a 16-bit function with a stub, we need
5046 to redirect the relocation to the stub unless:
5047
5048 (a) the relocation is for a MIPS16 JAL;
5049
5050 (b) the relocation is for a MIPS16 PIC call, and there are no
5051 non-MIPS16 uses of the GOT slot; or
5052
5053 (c) the section allows direct references to MIPS16 functions. */
5054 if (r_type != R_MIPS16_26
5055 && !info->relocatable
5056 && ((h != NULL
5057 && h->fn_stub != NULL
5058 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71
TS
5059 || (local_p
5060 && elf_tdata (input_bfd)->local_stubs != NULL
b49e97c9 5061 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5062 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5063 {
5064 /* This is a 32- or 64-bit call to a 16-bit function. We should
5065 have already noticed that we were going to need the
5066 stub. */
5067 if (local_p)
5068 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5069 else
5070 {
5071 BFD_ASSERT (h->need_fn_stub);
5072 sec = h->fn_stub;
5073 }
5074
5075 symbol = sec->output_section->vma + sec->output_offset;
f38c2df5
TS
5076 /* The target is 16-bit, but the stub isn't. */
5077 target_is_16_bit_code_p = FALSE;
b49e97c9
TS
5078 }
5079 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
738e5348
RS
5080 need to redirect the call to the stub. Note that we specifically
5081 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5082 use an indirect stub instead. */
1049f94e 5083 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5084 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71
TS
5085 || (local_p
5086 && elf_tdata (input_bfd)->local_call_stubs != NULL
5087 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
b49e97c9
TS
5088 && !target_is_16_bit_code_p)
5089 {
b9d58d71
TS
5090 if (local_p)
5091 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5092 else
b49e97c9 5093 {
b9d58d71
TS
5094 /* If both call_stub and call_fp_stub are defined, we can figure
5095 out which one to use by checking which one appears in the input
5096 file. */
5097 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5098 {
b9d58d71
TS
5099 asection *o;
5100
5101 sec = NULL;
5102 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5103 {
b9d58d71
TS
5104 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5105 {
5106 sec = h->call_fp_stub;
5107 break;
5108 }
b49e97c9 5109 }
b9d58d71
TS
5110 if (sec == NULL)
5111 sec = h->call_stub;
b49e97c9 5112 }
b9d58d71 5113 else if (h->call_stub != NULL)
b49e97c9 5114 sec = h->call_stub;
b9d58d71
TS
5115 else
5116 sec = h->call_fp_stub;
5117 }
b49e97c9 5118
eea6121a 5119 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5120 symbol = sec->output_section->vma + sec->output_offset;
5121 }
861fb55a
DJ
5122 /* If this is a direct call to a PIC function, redirect to the
5123 non-PIC stub. */
5124 else if (h != NULL && h->la25_stub
5125 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5126 symbol = (h->la25_stub->stub_section->output_section->vma
5127 + h->la25_stub->stub_section->output_offset
5128 + h->la25_stub->offset);
b49e97c9
TS
5129
5130 /* Calls from 16-bit code to 32-bit code and vice versa require the
38a7df63
CF
5131 mode change. */
5132 *cross_mode_jump_p = !info->relocatable
5133 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5134 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5135 && target_is_16_bit_code_p));
b49e97c9 5136
020d7251 5137 local_p = h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->root);
b49e97c9 5138
0a61c8c2
RS
5139 gp0 = _bfd_get_gp_value (input_bfd);
5140 gp = _bfd_get_gp_value (abfd);
23cc69b6 5141 if (htab->got_info)
a8028dd0 5142 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5143
5144 if (gnu_local_gp_p)
5145 symbol = gp;
5146
020d7251
RS
5147 /* Global R_MIPS_GOT_PAGE relocations are equivalent to R_MIPS_GOT_DISP.
5148 The addend is applied by the corresponding R_MIPS_GOT_OFST. */
5149 if (r_type == R_MIPS_GOT_PAGE && !local_p)
5150 {
5151 r_type = R_MIPS_GOT_DISP;
5152 addend = 0;
5153 }
5154
0a61c8c2
RS
5155 /* If we haven't already determined the GOT offset, oand we're going
5156 to need it, get it now. */
b49e97c9
TS
5157 switch (r_type)
5158 {
738e5348
RS
5159 case R_MIPS16_CALL16:
5160 case R_MIPS16_GOT16:
b49e97c9
TS
5161 case R_MIPS_CALL16:
5162 case R_MIPS_GOT16:
5163 case R_MIPS_GOT_DISP:
5164 case R_MIPS_GOT_HI16:
5165 case R_MIPS_CALL_HI16:
5166 case R_MIPS_GOT_LO16:
5167 case R_MIPS_CALL_LO16:
0f20cc35
DJ
5168 case R_MIPS_TLS_GD:
5169 case R_MIPS_TLS_GOTTPREL:
5170 case R_MIPS_TLS_LDM:
b49e97c9 5171 /* Find the index into the GOT where this value is located. */
0f20cc35
DJ
5172 if (r_type == R_MIPS_TLS_LDM)
5173 {
0a44bf69 5174 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5175 0, 0, NULL, r_type);
0f20cc35
DJ
5176 if (g == MINUS_ONE)
5177 return bfd_reloc_outofrange;
5178 }
5179 else if (!local_p)
b49e97c9 5180 {
0a44bf69
RS
5181 /* On VxWorks, CALL relocations should refer to the .got.plt
5182 entry, which is initialized to point at the PLT stub. */
5183 if (htab->is_vxworks
5184 && (r_type == R_MIPS_CALL_HI16
5185 || r_type == R_MIPS_CALL_LO16
738e5348 5186 || call16_reloc_p (r_type)))
0a44bf69
RS
5187 {
5188 BFD_ASSERT (addend == 0);
5189 BFD_ASSERT (h->root.needs_plt);
5190 g = mips_elf_gotplt_index (info, &h->root);
5191 }
5192 else
b49e97c9 5193 {
020d7251 5194 BFD_ASSERT (addend == 0);
0a44bf69
RS
5195 g = mips_elf_global_got_index (dynobj, input_bfd,
5196 &h->root, r_type, info);
5197 if (h->tls_type == GOT_NORMAL
020d7251
RS
5198 && !elf_hash_table (info)->dynamic_sections_created)
5199 /* This is a static link. We must initialize the GOT entry. */
a8028dd0 5200 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5201 }
5202 }
0a44bf69 5203 else if (!htab->is_vxworks
738e5348 5204 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5205 /* The calculation below does not involve "g". */
b49e97c9
TS
5206 break;
5207 else
5208 {
5c18022e 5209 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5210 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5211 if (g == MINUS_ONE)
5212 return bfd_reloc_outofrange;
5213 }
5214
5215 /* Convert GOT indices to actual offsets. */
a8028dd0 5216 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5217 break;
b49e97c9
TS
5218 }
5219
0a44bf69
RS
5220 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5221 symbols are resolved by the loader. Add them to .rela.dyn. */
5222 if (h != NULL && is_gott_symbol (info, &h->root))
5223 {
5224 Elf_Internal_Rela outrel;
5225 bfd_byte *loc;
5226 asection *s;
5227
5228 s = mips_elf_rel_dyn_section (info, FALSE);
5229 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5230
5231 outrel.r_offset = (input_section->output_section->vma
5232 + input_section->output_offset
5233 + relocation->r_offset);
5234 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5235 outrel.r_addend = addend;
5236 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5237
5238 /* If we've written this relocation for a readonly section,
5239 we need to set DF_TEXTREL again, so that we do not delete the
5240 DT_TEXTREL tag. */
5241 if (MIPS_ELF_READONLY_SECTION (input_section))
5242 info->flags |= DF_TEXTREL;
5243
0a44bf69
RS
5244 *valuep = 0;
5245 return bfd_reloc_ok;
5246 }
5247
b49e97c9
TS
5248 /* Figure out what kind of relocation is being performed. */
5249 switch (r_type)
5250 {
5251 case R_MIPS_NONE:
5252 return bfd_reloc_continue;
5253
5254 case R_MIPS_16:
a7ebbfdf 5255 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5256 overflowed_p = mips_elf_overflow_p (value, 16);
5257 break;
5258
5259 case R_MIPS_32:
5260 case R_MIPS_REL32:
5261 case R_MIPS_64:
5262 if ((info->shared
861fb55a 5263 || (htab->root.dynamic_sections_created
b49e97c9 5264 && h != NULL
f5385ebf 5265 && h->root.def_dynamic
861fb55a
DJ
5266 && !h->root.def_regular
5267 && !h->has_static_relocs))
cf35638d 5268 && r_symndx != STN_UNDEF
9a59ad6b
DJ
5269 && (h == NULL
5270 || h->root.root.type != bfd_link_hash_undefweak
5271 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5272 && (input_section->flags & SEC_ALLOC) != 0)
5273 {
861fb55a 5274 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5275 where the symbol will end up. So, we create a relocation
5276 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5277 linker. We must do the same for executable references to
5278 shared library symbols, unless we've decided to use copy
5279 relocs or PLTs instead. */
b49e97c9
TS
5280 value = addend;
5281 if (!mips_elf_create_dynamic_relocation (abfd,
5282 info,
5283 relocation,
5284 h,
5285 sec,
5286 symbol,
5287 &value,
5288 input_section))
5289 return bfd_reloc_undefined;
5290 }
5291 else
5292 {
5293 if (r_type != R_MIPS_REL32)
5294 value = symbol + addend;
5295 else
5296 value = addend;
5297 }
5298 value &= howto->dst_mask;
092dcd75
CD
5299 break;
5300
5301 case R_MIPS_PC32:
5302 value = symbol + addend - p;
5303 value &= howto->dst_mask;
b49e97c9
TS
5304 break;
5305
b49e97c9
TS
5306 case R_MIPS16_26:
5307 /* The calculation for R_MIPS16_26 is just the same as for an
5308 R_MIPS_26. It's only the storage of the relocated field into
5309 the output file that's different. That's handled in
5310 mips_elf_perform_relocation. So, we just fall through to the
5311 R_MIPS_26 case here. */
5312 case R_MIPS_26:
020d7251 5313 if (was_local_p)
30ac9238 5314 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
b49e97c9 5315 else
728b2f21
ILT
5316 {
5317 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
c314987d
RS
5318 if (h->root.root.type != bfd_link_hash_undefweak)
5319 overflowed_p = (value >> 26) != ((p + 4) >> 28);
728b2f21 5320 }
b49e97c9
TS
5321 value &= howto->dst_mask;
5322 break;
5323
0f20cc35
DJ
5324 case R_MIPS_TLS_DTPREL_HI16:
5325 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5326 & howto->dst_mask);
5327 break;
5328
5329 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5330 case R_MIPS_TLS_DTPREL32:
5331 case R_MIPS_TLS_DTPREL64:
0f20cc35
DJ
5332 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5333 break;
5334
5335 case R_MIPS_TLS_TPREL_HI16:
5336 value = (mips_elf_high (addend + symbol - tprel_base (info))
5337 & howto->dst_mask);
5338 break;
5339
5340 case R_MIPS_TLS_TPREL_LO16:
5341 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5342 break;
5343
b49e97c9 5344 case R_MIPS_HI16:
d6f16593 5345 case R_MIPS16_HI16:
b49e97c9
TS
5346 if (!gp_disp_p)
5347 {
5348 value = mips_elf_high (addend + symbol);
5349 value &= howto->dst_mask;
5350 }
5351 else
5352 {
d6f16593
MR
5353 /* For MIPS16 ABI code we generate this sequence
5354 0: li $v0,%hi(_gp_disp)
5355 4: addiupc $v1,%lo(_gp_disp)
5356 8: sll $v0,16
5357 12: addu $v0,$v1
5358 14: move $gp,$v0
5359 So the offsets of hi and lo relocs are the same, but the
5360 $pc is four higher than $t9 would be, so reduce
5361 both reloc addends by 4. */
5362 if (r_type == R_MIPS16_HI16)
5363 value = mips_elf_high (addend + gp - p - 4);
5364 else
5365 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5366 overflowed_p = mips_elf_overflow_p (value, 16);
5367 }
5368 break;
5369
5370 case R_MIPS_LO16:
d6f16593 5371 case R_MIPS16_LO16:
b49e97c9
TS
5372 if (!gp_disp_p)
5373 value = (symbol + addend) & howto->dst_mask;
5374 else
5375 {
d6f16593
MR
5376 /* See the comment for R_MIPS16_HI16 above for the reason
5377 for this conditional. */
5378 if (r_type == R_MIPS16_LO16)
5379 value = addend + gp - p;
5380 else
5381 value = addend + gp - p + 4;
b49e97c9 5382 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5383 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5384 _gp_disp are normally generated from the .cpload
5385 pseudo-op. It generates code that normally looks like
5386 this:
5387
5388 lui $gp,%hi(_gp_disp)
5389 addiu $gp,$gp,%lo(_gp_disp)
5390 addu $gp,$gp,$t9
5391
5392 Here $t9 holds the address of the function being called,
5393 as required by the MIPS ELF ABI. The R_MIPS_LO16
5394 relocation can easily overflow in this situation, but the
5395 R_MIPS_HI16 relocation will handle the overflow.
5396 Therefore, we consider this a bug in the MIPS ABI, and do
5397 not check for overflow here. */
5398 }
5399 break;
5400
5401 case R_MIPS_LITERAL:
5402 /* Because we don't merge literal sections, we can handle this
5403 just like R_MIPS_GPREL16. In the long run, we should merge
5404 shared literals, and then we will need to additional work
5405 here. */
5406
5407 /* Fall through. */
5408
5409 case R_MIPS16_GPREL:
5410 /* The R_MIPS16_GPREL performs the same calculation as
5411 R_MIPS_GPREL16, but stores the relocated bits in a different
5412 order. We don't need to do anything special here; the
5413 differences are handled in mips_elf_perform_relocation. */
5414 case R_MIPS_GPREL16:
bce03d3d
AO
5415 /* Only sign-extend the addend if it was extracted from the
5416 instruction. If the addend was separate, leave it alone,
5417 otherwise we may lose significant bits. */
5418 if (howto->partial_inplace)
a7ebbfdf 5419 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5420 value = symbol + addend - gp;
5421 /* If the symbol was local, any earlier relocatable links will
5422 have adjusted its addend with the gp offset, so compensate
5423 for that now. Don't do it for symbols forced local in this
5424 link, though, since they won't have had the gp offset applied
5425 to them before. */
5426 if (was_local_p)
5427 value += gp0;
b49e97c9
TS
5428 overflowed_p = mips_elf_overflow_p (value, 16);
5429 break;
5430
738e5348
RS
5431 case R_MIPS16_GOT16:
5432 case R_MIPS16_CALL16:
b49e97c9
TS
5433 case R_MIPS_GOT16:
5434 case R_MIPS_CALL16:
0a44bf69 5435 /* VxWorks does not have separate local and global semantics for
738e5348 5436 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5437 if (!htab->is_vxworks && local_p)
b49e97c9 5438 {
5c18022e 5439 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 5440 symbol + addend, !was_local_p);
b49e97c9
TS
5441 if (value == MINUS_ONE)
5442 return bfd_reloc_outofrange;
5443 value
a8028dd0 5444 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5445 overflowed_p = mips_elf_overflow_p (value, 16);
5446 break;
5447 }
5448
5449 /* Fall through. */
5450
0f20cc35
DJ
5451 case R_MIPS_TLS_GD:
5452 case R_MIPS_TLS_GOTTPREL:
5453 case R_MIPS_TLS_LDM:
b49e97c9
TS
5454 case R_MIPS_GOT_DISP:
5455 value = g;
5456 overflowed_p = mips_elf_overflow_p (value, 16);
5457 break;
5458
5459 case R_MIPS_GPREL32:
bce03d3d
AO
5460 value = (addend + symbol + gp0 - gp);
5461 if (!save_addend)
5462 value &= howto->dst_mask;
b49e97c9
TS
5463 break;
5464
5465 case R_MIPS_PC16:
bad36eac
DJ
5466 case R_MIPS_GNU_REL16_S2:
5467 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5468 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5469 value >>= howto->rightshift;
5470 value &= howto->dst_mask;
b49e97c9
TS
5471 break;
5472
5473 case R_MIPS_GOT_HI16:
5474 case R_MIPS_CALL_HI16:
5475 /* We're allowed to handle these two relocations identically.
5476 The dynamic linker is allowed to handle the CALL relocations
5477 differently by creating a lazy evaluation stub. */
5478 value = g;
5479 value = mips_elf_high (value);
5480 value &= howto->dst_mask;
5481 break;
5482
5483 case R_MIPS_GOT_LO16:
5484 case R_MIPS_CALL_LO16:
5485 value = g & howto->dst_mask;
5486 break;
5487
5488 case R_MIPS_GOT_PAGE:
5c18022e 5489 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5490 if (value == MINUS_ONE)
5491 return bfd_reloc_outofrange;
a8028dd0 5492 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5493 overflowed_p = mips_elf_overflow_p (value, 16);
5494 break;
5495
5496 case R_MIPS_GOT_OFST:
93a2b7ae 5497 if (local_p)
5c18022e 5498 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5499 else
5500 value = addend;
b49e97c9
TS
5501 overflowed_p = mips_elf_overflow_p (value, 16);
5502 break;
5503
5504 case R_MIPS_SUB:
5505 value = symbol - addend;
5506 value &= howto->dst_mask;
5507 break;
5508
5509 case R_MIPS_HIGHER:
5510 value = mips_elf_higher (addend + symbol);
5511 value &= howto->dst_mask;
5512 break;
5513
5514 case R_MIPS_HIGHEST:
5515 value = mips_elf_highest (addend + symbol);
5516 value &= howto->dst_mask;
5517 break;
5518
5519 case R_MIPS_SCN_DISP:
5520 value = symbol + addend - sec->output_offset;
5521 value &= howto->dst_mask;
5522 break;
5523
b49e97c9 5524 case R_MIPS_JALR:
1367d393
ILT
5525 /* This relocation is only a hint. In some cases, we optimize
5526 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5527 when the symbol does not resolve locally. */
5528 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5529 return bfd_reloc_continue;
5530 value = symbol + addend;
5531 break;
b49e97c9 5532
1367d393 5533 case R_MIPS_PJUMP:
b49e97c9
TS
5534 case R_MIPS_GNU_VTINHERIT:
5535 case R_MIPS_GNU_VTENTRY:
5536 /* We don't do anything with these at present. */
5537 return bfd_reloc_continue;
5538
5539 default:
5540 /* An unrecognized relocation type. */
5541 return bfd_reloc_notsupported;
5542 }
5543
5544 /* Store the VALUE for our caller. */
5545 *valuep = value;
5546 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5547}
5548
5549/* Obtain the field relocated by RELOCATION. */
5550
5551static bfd_vma
9719ad41
RS
5552mips_elf_obtain_contents (reloc_howto_type *howto,
5553 const Elf_Internal_Rela *relocation,
5554 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
5555{
5556 bfd_vma x;
5557 bfd_byte *location = contents + relocation->r_offset;
5558
5559 /* Obtain the bytes. */
5560 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5561
b49e97c9
TS
5562 return x;
5563}
5564
5565/* It has been determined that the result of the RELOCATION is the
5566 VALUE. Use HOWTO to place VALUE into the output file at the
5567 appropriate position. The SECTION is the section to which the
38a7df63
CF
5568 relocation applies.
5569 CROSS_MODE_JUMP_P is true if the relocation field
5570 is a MIPS16 jump to non-MIPS16 code, or vice versa.
b49e97c9 5571
b34976b6 5572 Returns FALSE if anything goes wrong. */
b49e97c9 5573
b34976b6 5574static bfd_boolean
9719ad41
RS
5575mips_elf_perform_relocation (struct bfd_link_info *info,
5576 reloc_howto_type *howto,
5577 const Elf_Internal_Rela *relocation,
5578 bfd_vma value, bfd *input_bfd,
5579 asection *input_section, bfd_byte *contents,
38a7df63 5580 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
5581{
5582 bfd_vma x;
5583 bfd_byte *location;
5584 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5585
5586 /* Figure out where the relocation is occurring. */
5587 location = contents + relocation->r_offset;
5588
d6f16593
MR
5589 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5590
b49e97c9
TS
5591 /* Obtain the current value. */
5592 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5593
5594 /* Clear the field we are setting. */
5595 x &= ~howto->dst_mask;
5596
b49e97c9
TS
5597 /* Set the field. */
5598 x |= (value & howto->dst_mask);
5599
5600 /* If required, turn JAL into JALX. */
38a7df63 5601 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 5602 {
b34976b6 5603 bfd_boolean ok;
b49e97c9
TS
5604 bfd_vma opcode = x >> 26;
5605 bfd_vma jalx_opcode;
5606
5607 /* Check to see if the opcode is already JAL or JALX. */
5608 if (r_type == R_MIPS16_26)
5609 {
5610 ok = ((opcode == 0x6) || (opcode == 0x7));
5611 jalx_opcode = 0x7;
5612 }
5613 else
5614 {
5615 ok = ((opcode == 0x3) || (opcode == 0x1d));
5616 jalx_opcode = 0x1d;
5617 }
5618
5619 /* If the opcode is not JAL or JALX, there's a problem. */
5620 if (!ok)
5621 {
5622 (*_bfd_error_handler)
776167e8 5623 (_("%B: %A+0x%lx: Direct jumps between ISA modes are not allowed; consider recompiling with interlinking enabled."),
d003868e
AM
5624 input_bfd,
5625 input_section,
b49e97c9
TS
5626 (unsigned long) relocation->r_offset);
5627 bfd_set_error (bfd_error_bad_value);
b34976b6 5628 return FALSE;
b49e97c9
TS
5629 }
5630
5631 /* Make this the JALX opcode. */
5632 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5633 }
5634
38a7df63
CF
5635 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5636 range. */
cd8d5a82 5637 if (!info->relocatable
38a7df63 5638 && !cross_mode_jump_p
cd8d5a82
CF
5639 && ((JAL_TO_BAL_P (input_bfd)
5640 && r_type == R_MIPS_26
5641 && (x >> 26) == 0x3) /* jal addr */
5642 || (JALR_TO_BAL_P (input_bfd)
5643 && r_type == R_MIPS_JALR
38a7df63
CF
5644 && x == 0x0320f809) /* jalr t9 */
5645 || (JR_TO_B_P (input_bfd)
5646 && r_type == R_MIPS_JALR
5647 && x == 0x03200008))) /* jr t9 */
1367d393
ILT
5648 {
5649 bfd_vma addr;
5650 bfd_vma dest;
5651 bfd_signed_vma off;
5652
5653 addr = (input_section->output_section->vma
5654 + input_section->output_offset
5655 + relocation->r_offset
5656 + 4);
5657 if (r_type == R_MIPS_26)
5658 dest = (value << 2) | ((addr >> 28) << 28);
5659 else
5660 dest = value;
5661 off = dest - addr;
5662 if (off <= 0x1ffff && off >= -0x20000)
38a7df63
CF
5663 {
5664 if (x == 0x03200008) /* jr t9 */
5665 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5666 else
5667 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5668 }
1367d393
ILT
5669 }
5670
b49e97c9
TS
5671 /* Put the value into the output. */
5672 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593
MR
5673
5674 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5675 location);
5676
b34976b6 5677 return TRUE;
b49e97c9 5678}
b49e97c9 5679\f
b49e97c9
TS
5680/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5681 is the original relocation, which is now being transformed into a
5682 dynamic relocation. The ADDENDP is adjusted if necessary; the
5683 caller should store the result in place of the original addend. */
5684
b34976b6 5685static bfd_boolean
9719ad41
RS
5686mips_elf_create_dynamic_relocation (bfd *output_bfd,
5687 struct bfd_link_info *info,
5688 const Elf_Internal_Rela *rel,
5689 struct mips_elf_link_hash_entry *h,
5690 asection *sec, bfd_vma symbol,
5691 bfd_vma *addendp, asection *input_section)
b49e97c9 5692{
947216bf 5693 Elf_Internal_Rela outrel[3];
b49e97c9
TS
5694 asection *sreloc;
5695 bfd *dynobj;
5696 int r_type;
5d41f0b6
RS
5697 long indx;
5698 bfd_boolean defined_p;
0a44bf69 5699 struct mips_elf_link_hash_table *htab;
b49e97c9 5700
0a44bf69 5701 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
5702 BFD_ASSERT (htab != NULL);
5703
b49e97c9
TS
5704 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5705 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 5706 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
5707 BFD_ASSERT (sreloc != NULL);
5708 BFD_ASSERT (sreloc->contents != NULL);
5709 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 5710 < sreloc->size);
b49e97c9 5711
b49e97c9
TS
5712 outrel[0].r_offset =
5713 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
5714 if (ABI_64_P (output_bfd))
5715 {
5716 outrel[1].r_offset =
5717 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5718 outrel[2].r_offset =
5719 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5720 }
b49e97c9 5721
c5ae1840 5722 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 5723 /* The relocation field has been deleted. */
5d41f0b6
RS
5724 return TRUE;
5725
5726 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
5727 {
5728 /* The relocation field has been converted into a relative value of
5729 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5730 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 5731 *addendp += symbol;
5d41f0b6 5732 return TRUE;
0d591ff7 5733 }
b49e97c9 5734
5d41f0b6
RS
5735 /* We must now calculate the dynamic symbol table index to use
5736 in the relocation. */
d4a77f3f 5737 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 5738 {
020d7251 5739 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5d41f0b6
RS
5740 indx = h->root.dynindx;
5741 if (SGI_COMPAT (output_bfd))
5742 defined_p = h->root.def_regular;
5743 else
5744 /* ??? glibc's ld.so just adds the final GOT entry to the
5745 relocation field. It therefore treats relocs against
5746 defined symbols in the same way as relocs against
5747 undefined symbols. */
5748 defined_p = FALSE;
5749 }
b49e97c9
TS
5750 else
5751 {
5d41f0b6
RS
5752 if (sec != NULL && bfd_is_abs_section (sec))
5753 indx = 0;
5754 else if (sec == NULL || sec->owner == NULL)
fdd07405 5755 {
5d41f0b6
RS
5756 bfd_set_error (bfd_error_bad_value);
5757 return FALSE;
b49e97c9
TS
5758 }
5759 else
5760 {
5d41f0b6 5761 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
5762 if (indx == 0)
5763 {
5764 asection *osec = htab->root.text_index_section;
5765 indx = elf_section_data (osec)->dynindx;
5766 }
5d41f0b6
RS
5767 if (indx == 0)
5768 abort ();
b49e97c9
TS
5769 }
5770
5d41f0b6
RS
5771 /* Instead of generating a relocation using the section
5772 symbol, we may as well make it a fully relative
5773 relocation. We want to avoid generating relocations to
5774 local symbols because we used to generate them
5775 incorrectly, without adding the original symbol value,
5776 which is mandated by the ABI for section symbols. In
5777 order to give dynamic loaders and applications time to
5778 phase out the incorrect use, we refrain from emitting
5779 section-relative relocations. It's not like they're
5780 useful, after all. This should be a bit more efficient
5781 as well. */
5782 /* ??? Although this behavior is compatible with glibc's ld.so,
5783 the ABI says that relocations against STN_UNDEF should have
5784 a symbol value of 0. Irix rld honors this, so relocations
5785 against STN_UNDEF have no effect. */
5786 if (!SGI_COMPAT (output_bfd))
5787 indx = 0;
5788 defined_p = TRUE;
b49e97c9
TS
5789 }
5790
5d41f0b6
RS
5791 /* If the relocation was previously an absolute relocation and
5792 this symbol will not be referred to by the relocation, we must
5793 adjust it by the value we give it in the dynamic symbol table.
5794 Otherwise leave the job up to the dynamic linker. */
5795 if (defined_p && r_type != R_MIPS_REL32)
5796 *addendp += symbol;
5797
0a44bf69
RS
5798 if (htab->is_vxworks)
5799 /* VxWorks uses non-relative relocations for this. */
5800 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5801 else
5802 /* The relocation is always an REL32 relocation because we don't
5803 know where the shared library will wind up at load-time. */
5804 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5805 R_MIPS_REL32);
5806
5d41f0b6
RS
5807 /* For strict adherence to the ABI specification, we should
5808 generate a R_MIPS_64 relocation record by itself before the
5809 _REL32/_64 record as well, such that the addend is read in as
5810 a 64-bit value (REL32 is a 32-bit relocation, after all).
5811 However, since none of the existing ELF64 MIPS dynamic
5812 loaders seems to care, we don't waste space with these
5813 artificial relocations. If this turns out to not be true,
5814 mips_elf_allocate_dynamic_relocation() should be tweaked so
5815 as to make room for a pair of dynamic relocations per
5816 invocation if ABI_64_P, and here we should generate an
5817 additional relocation record with R_MIPS_64 by itself for a
5818 NULL symbol before this relocation record. */
5819 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5820 ABI_64_P (output_bfd)
5821 ? R_MIPS_64
5822 : R_MIPS_NONE);
5823 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5824
5825 /* Adjust the output offset of the relocation to reference the
5826 correct location in the output file. */
5827 outrel[0].r_offset += (input_section->output_section->vma
5828 + input_section->output_offset);
5829 outrel[1].r_offset += (input_section->output_section->vma
5830 + input_section->output_offset);
5831 outrel[2].r_offset += (input_section->output_section->vma
5832 + input_section->output_offset);
5833
b49e97c9
TS
5834 /* Put the relocation back out. We have to use the special
5835 relocation outputter in the 64-bit case since the 64-bit
5836 relocation format is non-standard. */
5837 if (ABI_64_P (output_bfd))
5838 {
5839 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5840 (output_bfd, &outrel[0],
5841 (sreloc->contents
5842 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5843 }
0a44bf69
RS
5844 else if (htab->is_vxworks)
5845 {
5846 /* VxWorks uses RELA rather than REL dynamic relocations. */
5847 outrel[0].r_addend = *addendp;
5848 bfd_elf32_swap_reloca_out
5849 (output_bfd, &outrel[0],
5850 (sreloc->contents
5851 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5852 }
b49e97c9 5853 else
947216bf
AM
5854 bfd_elf32_swap_reloc_out
5855 (output_bfd, &outrel[0],
5856 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 5857
b49e97c9
TS
5858 /* We've now added another relocation. */
5859 ++sreloc->reloc_count;
5860
5861 /* Make sure the output section is writable. The dynamic linker
5862 will be writing to it. */
5863 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5864 |= SHF_WRITE;
5865
5866 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 5867 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9
TS
5868 {
5869 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5870 bfd_byte *cr;
5871
5872 if (scpt)
5873 {
5874 Elf32_crinfo cptrel;
5875
5876 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5877 cptrel.vaddr = (rel->r_offset
5878 + input_section->output_section->vma
5879 + input_section->output_offset);
5880 if (r_type == R_MIPS_REL32)
5881 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5882 else
5883 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5884 mips_elf_set_cr_dist2to (cptrel, 0);
5885 cptrel.konst = *addendp;
5886
5887 cr = (scpt->contents
5888 + sizeof (Elf32_External_compact_rel));
abc0f8d0 5889 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
5890 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5891 ((Elf32_External_crinfo *) cr
5892 + scpt->reloc_count));
5893 ++scpt->reloc_count;
5894 }
5895 }
5896
943284cc
DJ
5897 /* If we've written this relocation for a readonly section,
5898 we need to set DF_TEXTREL again, so that we do not delete the
5899 DT_TEXTREL tag. */
5900 if (MIPS_ELF_READONLY_SECTION (input_section))
5901 info->flags |= DF_TEXTREL;
5902
b34976b6 5903 return TRUE;
b49e97c9
TS
5904}
5905\f
b49e97c9
TS
5906/* Return the MACH for a MIPS e_flags value. */
5907
5908unsigned long
9719ad41 5909_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
5910{
5911 switch (flags & EF_MIPS_MACH)
5912 {
5913 case E_MIPS_MACH_3900:
5914 return bfd_mach_mips3900;
5915
5916 case E_MIPS_MACH_4010:
5917 return bfd_mach_mips4010;
5918
5919 case E_MIPS_MACH_4100:
5920 return bfd_mach_mips4100;
5921
5922 case E_MIPS_MACH_4111:
5923 return bfd_mach_mips4111;
5924
00707a0e
RS
5925 case E_MIPS_MACH_4120:
5926 return bfd_mach_mips4120;
5927
b49e97c9
TS
5928 case E_MIPS_MACH_4650:
5929 return bfd_mach_mips4650;
5930
00707a0e
RS
5931 case E_MIPS_MACH_5400:
5932 return bfd_mach_mips5400;
5933
5934 case E_MIPS_MACH_5500:
5935 return bfd_mach_mips5500;
5936
0d2e43ed
ILT
5937 case E_MIPS_MACH_9000:
5938 return bfd_mach_mips9000;
5939
b49e97c9
TS
5940 case E_MIPS_MACH_SB1:
5941 return bfd_mach_mips_sb1;
5942
350cc38d
MS
5943 case E_MIPS_MACH_LS2E:
5944 return bfd_mach_mips_loongson_2e;
5945
5946 case E_MIPS_MACH_LS2F:
5947 return bfd_mach_mips_loongson_2f;
5948
6f179bd0
AN
5949 case E_MIPS_MACH_OCTEON:
5950 return bfd_mach_mips_octeon;
5951
52b6b6b9
JM
5952 case E_MIPS_MACH_XLR:
5953 return bfd_mach_mips_xlr;
5954
b49e97c9
TS
5955 default:
5956 switch (flags & EF_MIPS_ARCH)
5957 {
5958 default:
5959 case E_MIPS_ARCH_1:
5960 return bfd_mach_mips3000;
b49e97c9
TS
5961
5962 case E_MIPS_ARCH_2:
5963 return bfd_mach_mips6000;
b49e97c9
TS
5964
5965 case E_MIPS_ARCH_3:
5966 return bfd_mach_mips4000;
b49e97c9
TS
5967
5968 case E_MIPS_ARCH_4:
5969 return bfd_mach_mips8000;
b49e97c9
TS
5970
5971 case E_MIPS_ARCH_5:
5972 return bfd_mach_mips5;
b49e97c9
TS
5973
5974 case E_MIPS_ARCH_32:
5975 return bfd_mach_mipsisa32;
b49e97c9
TS
5976
5977 case E_MIPS_ARCH_64:
5978 return bfd_mach_mipsisa64;
af7ee8bf
CD
5979
5980 case E_MIPS_ARCH_32R2:
5981 return bfd_mach_mipsisa32r2;
5f74bc13
CD
5982
5983 case E_MIPS_ARCH_64R2:
5984 return bfd_mach_mipsisa64r2;
b49e97c9
TS
5985 }
5986 }
5987
5988 return 0;
5989}
5990
5991/* Return printable name for ABI. */
5992
5993static INLINE char *
9719ad41 5994elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
5995{
5996 flagword flags;
5997
5998 flags = elf_elfheader (abfd)->e_flags;
5999 switch (flags & EF_MIPS_ABI)
6000 {
6001 case 0:
6002 if (ABI_N32_P (abfd))
6003 return "N32";
6004 else if (ABI_64_P (abfd))
6005 return "64";
6006 else
6007 return "none";
6008 case E_MIPS_ABI_O32:
6009 return "O32";
6010 case E_MIPS_ABI_O64:
6011 return "O64";
6012 case E_MIPS_ABI_EABI32:
6013 return "EABI32";
6014 case E_MIPS_ABI_EABI64:
6015 return "EABI64";
6016 default:
6017 return "unknown abi";
6018 }
6019}
6020\f
6021/* MIPS ELF uses two common sections. One is the usual one, and the
6022 other is for small objects. All the small objects are kept
6023 together, and then referenced via the gp pointer, which yields
6024 faster assembler code. This is what we use for the small common
6025 section. This approach is copied from ecoff.c. */
6026static asection mips_elf_scom_section;
6027static asymbol mips_elf_scom_symbol;
6028static asymbol *mips_elf_scom_symbol_ptr;
6029
6030/* MIPS ELF also uses an acommon section, which represents an
6031 allocated common symbol which may be overridden by a
6032 definition in a shared library. */
6033static asection mips_elf_acom_section;
6034static asymbol mips_elf_acom_symbol;
6035static asymbol *mips_elf_acom_symbol_ptr;
6036
738e5348 6037/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
6038
6039void
9719ad41 6040_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
6041{
6042 elf_symbol_type *elfsym;
6043
738e5348 6044 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6045 elfsym = (elf_symbol_type *) asym;
6046 switch (elfsym->internal_elf_sym.st_shndx)
6047 {
6048 case SHN_MIPS_ACOMMON:
6049 /* This section is used in a dynamically linked executable file.
6050 It is an allocated common section. The dynamic linker can
6051 either resolve these symbols to something in a shared
6052 library, or it can just leave them here. For our purposes,
6053 we can consider these symbols to be in a new section. */
6054 if (mips_elf_acom_section.name == NULL)
6055 {
6056 /* Initialize the acommon section. */
6057 mips_elf_acom_section.name = ".acommon";
6058 mips_elf_acom_section.flags = SEC_ALLOC;
6059 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6060 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6061 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6062 mips_elf_acom_symbol.name = ".acommon";
6063 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6064 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6065 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6066 }
6067 asym->section = &mips_elf_acom_section;
6068 break;
6069
6070 case SHN_COMMON:
6071 /* Common symbols less than the GP size are automatically
6072 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6073 if (asym->value > elf_gp_size (abfd)
b59eed79 6074 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6075 || IRIX_COMPAT (abfd) == ict_irix6)
6076 break;
6077 /* Fall through. */
6078 case SHN_MIPS_SCOMMON:
6079 if (mips_elf_scom_section.name == NULL)
6080 {
6081 /* Initialize the small common section. */
6082 mips_elf_scom_section.name = ".scommon";
6083 mips_elf_scom_section.flags = SEC_IS_COMMON;
6084 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6085 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6086 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6087 mips_elf_scom_symbol.name = ".scommon";
6088 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6089 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6090 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6091 }
6092 asym->section = &mips_elf_scom_section;
6093 asym->value = elfsym->internal_elf_sym.st_size;
6094 break;
6095
6096 case SHN_MIPS_SUNDEFINED:
6097 asym->section = bfd_und_section_ptr;
6098 break;
6099
b49e97c9 6100 case SHN_MIPS_TEXT:
00b4930b
TS
6101 {
6102 asection *section = bfd_get_section_by_name (abfd, ".text");
6103
6104 BFD_ASSERT (SGI_COMPAT (abfd));
6105 if (section != NULL)
6106 {
6107 asym->section = section;
6108 /* MIPS_TEXT is a bit special, the address is not an offset
6109 to the base of the .text section. So substract the section
6110 base address to make it an offset. */
6111 asym->value -= section->vma;
6112 }
6113 }
b49e97c9
TS
6114 break;
6115
6116 case SHN_MIPS_DATA:
00b4930b
TS
6117 {
6118 asection *section = bfd_get_section_by_name (abfd, ".data");
6119
6120 BFD_ASSERT (SGI_COMPAT (abfd));
6121 if (section != NULL)
6122 {
6123 asym->section = section;
6124 /* MIPS_DATA is a bit special, the address is not an offset
6125 to the base of the .data section. So substract the section
6126 base address to make it an offset. */
6127 asym->value -= section->vma;
6128 }
6129 }
b49e97c9 6130 break;
b49e97c9 6131 }
738e5348
RS
6132
6133 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6134 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6135 && (asym->value & 1) != 0)
6136 {
6137 asym->value--;
6138 elfsym->internal_elf_sym.st_other
6139 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6140 }
b49e97c9
TS
6141}
6142\f
8c946ed5
RS
6143/* Implement elf_backend_eh_frame_address_size. This differs from
6144 the default in the way it handles EABI64.
6145
6146 EABI64 was originally specified as an LP64 ABI, and that is what
6147 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6148 historically accepted the combination of -mabi=eabi and -mlong32,
6149 and this ILP32 variation has become semi-official over time.
6150 Both forms use elf32 and have pointer-sized FDE addresses.
6151
6152 If an EABI object was generated by GCC 4.0 or above, it will have
6153 an empty .gcc_compiled_longXX section, where XX is the size of longs
6154 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6155 have no special marking to distinguish them from LP64 objects.
6156
6157 We don't want users of the official LP64 ABI to be punished for the
6158 existence of the ILP32 variant, but at the same time, we don't want
6159 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6160 We therefore take the following approach:
6161
6162 - If ABFD contains a .gcc_compiled_longXX section, use it to
6163 determine the pointer size.
6164
6165 - Otherwise check the type of the first relocation. Assume that
6166 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6167
6168 - Otherwise punt.
6169
6170 The second check is enough to detect LP64 objects generated by pre-4.0
6171 compilers because, in the kind of output generated by those compilers,
6172 the first relocation will be associated with either a CIE personality
6173 routine or an FDE start address. Furthermore, the compilers never
6174 used a special (non-pointer) encoding for this ABI.
6175
6176 Checking the relocation type should also be safe because there is no
6177 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6178 did so. */
6179
6180unsigned int
6181_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6182{
6183 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6184 return 8;
6185 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6186 {
6187 bfd_boolean long32_p, long64_p;
6188
6189 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6190 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6191 if (long32_p && long64_p)
6192 return 0;
6193 if (long32_p)
6194 return 4;
6195 if (long64_p)
6196 return 8;
6197
6198 if (sec->reloc_count > 0
6199 && elf_section_data (sec)->relocs != NULL
6200 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6201 == R_MIPS_64))
6202 return 8;
6203
6204 return 0;
6205 }
6206 return 4;
6207}
6208\f
174fd7f9
RS
6209/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6210 relocations against two unnamed section symbols to resolve to the
6211 same address. For example, if we have code like:
6212
6213 lw $4,%got_disp(.data)($gp)
6214 lw $25,%got_disp(.text)($gp)
6215 jalr $25
6216
6217 then the linker will resolve both relocations to .data and the program
6218 will jump there rather than to .text.
6219
6220 We can work around this problem by giving names to local section symbols.
6221 This is also what the MIPSpro tools do. */
6222
6223bfd_boolean
6224_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6225{
6226 return SGI_COMPAT (abfd);
6227}
6228\f
b49e97c9
TS
6229/* Work over a section just before writing it out. This routine is
6230 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6231 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6232 a better way. */
6233
b34976b6 6234bfd_boolean
9719ad41 6235_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6236{
6237 if (hdr->sh_type == SHT_MIPS_REGINFO
6238 && hdr->sh_size > 0)
6239 {
6240 bfd_byte buf[4];
6241
6242 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6243 BFD_ASSERT (hdr->contents == NULL);
6244
6245 if (bfd_seek (abfd,
6246 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6247 SEEK_SET) != 0)
b34976b6 6248 return FALSE;
b49e97c9 6249 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6250 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6251 return FALSE;
b49e97c9
TS
6252 }
6253
6254 if (hdr->sh_type == SHT_MIPS_OPTIONS
6255 && hdr->bfd_section != NULL
f0abc2a1
AM
6256 && mips_elf_section_data (hdr->bfd_section) != NULL
6257 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6258 {
6259 bfd_byte *contents, *l, *lend;
6260
f0abc2a1
AM
6261 /* We stored the section contents in the tdata field in the
6262 set_section_contents routine. We save the section contents
6263 so that we don't have to read them again.
b49e97c9
TS
6264 At this point we know that elf_gp is set, so we can look
6265 through the section contents to see if there is an
6266 ODK_REGINFO structure. */
6267
f0abc2a1 6268 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6269 l = contents;
6270 lend = contents + hdr->sh_size;
6271 while (l + sizeof (Elf_External_Options) <= lend)
6272 {
6273 Elf_Internal_Options intopt;
6274
6275 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6276 &intopt);
1bc8074d
MR
6277 if (intopt.size < sizeof (Elf_External_Options))
6278 {
6279 (*_bfd_error_handler)
6280 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6281 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6282 break;
6283 }
b49e97c9
TS
6284 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6285 {
6286 bfd_byte buf[8];
6287
6288 if (bfd_seek (abfd,
6289 (hdr->sh_offset
6290 + (l - contents)
6291 + sizeof (Elf_External_Options)
6292 + (sizeof (Elf64_External_RegInfo) - 8)),
6293 SEEK_SET) != 0)
b34976b6 6294 return FALSE;
b49e97c9 6295 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6296 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6297 return FALSE;
b49e97c9
TS
6298 }
6299 else if (intopt.kind == ODK_REGINFO)
6300 {
6301 bfd_byte buf[4];
6302
6303 if (bfd_seek (abfd,
6304 (hdr->sh_offset
6305 + (l - contents)
6306 + sizeof (Elf_External_Options)
6307 + (sizeof (Elf32_External_RegInfo) - 4)),
6308 SEEK_SET) != 0)
b34976b6 6309 return FALSE;
b49e97c9 6310 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6311 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6312 return FALSE;
b49e97c9
TS
6313 }
6314 l += intopt.size;
6315 }
6316 }
6317
6318 if (hdr->bfd_section != NULL)
6319 {
6320 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6321
2d0f9ad9
JM
6322 /* .sbss is not handled specially here because the GNU/Linux
6323 prelinker can convert .sbss from NOBITS to PROGBITS and
6324 changing it back to NOBITS breaks the binary. The entry in
6325 _bfd_mips_elf_special_sections will ensure the correct flags
6326 are set on .sbss if BFD creates it without reading it from an
6327 input file, and without special handling here the flags set
6328 on it in an input file will be followed. */
b49e97c9
TS
6329 if (strcmp (name, ".sdata") == 0
6330 || strcmp (name, ".lit8") == 0
6331 || strcmp (name, ".lit4") == 0)
6332 {
6333 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6334 hdr->sh_type = SHT_PROGBITS;
6335 }
b49e97c9
TS
6336 else if (strcmp (name, ".srdata") == 0)
6337 {
6338 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6339 hdr->sh_type = SHT_PROGBITS;
6340 }
6341 else if (strcmp (name, ".compact_rel") == 0)
6342 {
6343 hdr->sh_flags = 0;
6344 hdr->sh_type = SHT_PROGBITS;
6345 }
6346 else if (strcmp (name, ".rtproc") == 0)
6347 {
6348 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6349 {
6350 unsigned int adjust;
6351
6352 adjust = hdr->sh_size % hdr->sh_addralign;
6353 if (adjust != 0)
6354 hdr->sh_size += hdr->sh_addralign - adjust;
6355 }
6356 }
6357 }
6358
b34976b6 6359 return TRUE;
b49e97c9
TS
6360}
6361
6362/* Handle a MIPS specific section when reading an object file. This
6363 is called when elfcode.h finds a section with an unknown type.
6364 This routine supports both the 32-bit and 64-bit ELF ABI.
6365
6366 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6367 how to. */
6368
b34976b6 6369bfd_boolean
6dc132d9
L
6370_bfd_mips_elf_section_from_shdr (bfd *abfd,
6371 Elf_Internal_Shdr *hdr,
6372 const char *name,
6373 int shindex)
b49e97c9
TS
6374{
6375 flagword flags = 0;
6376
6377 /* There ought to be a place to keep ELF backend specific flags, but
6378 at the moment there isn't one. We just keep track of the
6379 sections by their name, instead. Fortunately, the ABI gives
6380 suggested names for all the MIPS specific sections, so we will
6381 probably get away with this. */
6382 switch (hdr->sh_type)
6383 {
6384 case SHT_MIPS_LIBLIST:
6385 if (strcmp (name, ".liblist") != 0)
b34976b6 6386 return FALSE;
b49e97c9
TS
6387 break;
6388 case SHT_MIPS_MSYM:
6389 if (strcmp (name, ".msym") != 0)
b34976b6 6390 return FALSE;
b49e97c9
TS
6391 break;
6392 case SHT_MIPS_CONFLICT:
6393 if (strcmp (name, ".conflict") != 0)
b34976b6 6394 return FALSE;
b49e97c9
TS
6395 break;
6396 case SHT_MIPS_GPTAB:
0112cd26 6397 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6398 return FALSE;
b49e97c9
TS
6399 break;
6400 case SHT_MIPS_UCODE:
6401 if (strcmp (name, ".ucode") != 0)
b34976b6 6402 return FALSE;
b49e97c9
TS
6403 break;
6404 case SHT_MIPS_DEBUG:
6405 if (strcmp (name, ".mdebug") != 0)
b34976b6 6406 return FALSE;
b49e97c9
TS
6407 flags = SEC_DEBUGGING;
6408 break;
6409 case SHT_MIPS_REGINFO:
6410 if (strcmp (name, ".reginfo") != 0
6411 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6412 return FALSE;
b49e97c9
TS
6413 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6414 break;
6415 case SHT_MIPS_IFACE:
6416 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6417 return FALSE;
b49e97c9
TS
6418 break;
6419 case SHT_MIPS_CONTENT:
0112cd26 6420 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6421 return FALSE;
b49e97c9
TS
6422 break;
6423 case SHT_MIPS_OPTIONS:
cc2e31b9 6424 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6425 return FALSE;
b49e97c9
TS
6426 break;
6427 case SHT_MIPS_DWARF:
1b315056 6428 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6429 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6430 return FALSE;
b49e97c9
TS
6431 break;
6432 case SHT_MIPS_SYMBOL_LIB:
6433 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6434 return FALSE;
b49e97c9
TS
6435 break;
6436 case SHT_MIPS_EVENTS:
0112cd26
NC
6437 if (! CONST_STRNEQ (name, ".MIPS.events")
6438 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6439 return FALSE;
b49e97c9
TS
6440 break;
6441 default:
cc2e31b9 6442 break;
b49e97c9
TS
6443 }
6444
6dc132d9 6445 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6446 return FALSE;
b49e97c9
TS
6447
6448 if (flags)
6449 {
6450 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6451 (bfd_get_section_flags (abfd,
6452 hdr->bfd_section)
6453 | flags)))
b34976b6 6454 return FALSE;
b49e97c9
TS
6455 }
6456
6457 /* FIXME: We should record sh_info for a .gptab section. */
6458
6459 /* For a .reginfo section, set the gp value in the tdata information
6460 from the contents of this section. We need the gp value while
6461 processing relocs, so we just get it now. The .reginfo section
6462 is not used in the 64-bit MIPS ELF ABI. */
6463 if (hdr->sh_type == SHT_MIPS_REGINFO)
6464 {
6465 Elf32_External_RegInfo ext;
6466 Elf32_RegInfo s;
6467
9719ad41
RS
6468 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6469 &ext, 0, sizeof ext))
b34976b6 6470 return FALSE;
b49e97c9
TS
6471 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6472 elf_gp (abfd) = s.ri_gp_value;
6473 }
6474
6475 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6476 set the gp value based on what we find. We may see both
6477 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6478 they should agree. */
6479 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6480 {
6481 bfd_byte *contents, *l, *lend;
6482
9719ad41 6483 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6484 if (contents == NULL)
b34976b6 6485 return FALSE;
b49e97c9 6486 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6487 0, hdr->sh_size))
b49e97c9
TS
6488 {
6489 free (contents);
b34976b6 6490 return FALSE;
b49e97c9
TS
6491 }
6492 l = contents;
6493 lend = contents + hdr->sh_size;
6494 while (l + sizeof (Elf_External_Options) <= lend)
6495 {
6496 Elf_Internal_Options intopt;
6497
6498 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6499 &intopt);
1bc8074d
MR
6500 if (intopt.size < sizeof (Elf_External_Options))
6501 {
6502 (*_bfd_error_handler)
6503 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6504 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6505 break;
6506 }
b49e97c9
TS
6507 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6508 {
6509 Elf64_Internal_RegInfo intreg;
6510
6511 bfd_mips_elf64_swap_reginfo_in
6512 (abfd,
6513 ((Elf64_External_RegInfo *)
6514 (l + sizeof (Elf_External_Options))),
6515 &intreg);
6516 elf_gp (abfd) = intreg.ri_gp_value;
6517 }
6518 else if (intopt.kind == ODK_REGINFO)
6519 {
6520 Elf32_RegInfo intreg;
6521
6522 bfd_mips_elf32_swap_reginfo_in
6523 (abfd,
6524 ((Elf32_External_RegInfo *)
6525 (l + sizeof (Elf_External_Options))),
6526 &intreg);
6527 elf_gp (abfd) = intreg.ri_gp_value;
6528 }
6529 l += intopt.size;
6530 }
6531 free (contents);
6532 }
6533
b34976b6 6534 return TRUE;
b49e97c9
TS
6535}
6536
6537/* Set the correct type for a MIPS ELF section. We do this by the
6538 section name, which is a hack, but ought to work. This routine is
6539 used by both the 32-bit and the 64-bit ABI. */
6540
b34976b6 6541bfd_boolean
9719ad41 6542_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 6543{
0414f35b 6544 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
6545
6546 if (strcmp (name, ".liblist") == 0)
6547 {
6548 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 6549 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
6550 /* The sh_link field is set in final_write_processing. */
6551 }
6552 else if (strcmp (name, ".conflict") == 0)
6553 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 6554 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
6555 {
6556 hdr->sh_type = SHT_MIPS_GPTAB;
6557 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6558 /* The sh_info field is set in final_write_processing. */
6559 }
6560 else if (strcmp (name, ".ucode") == 0)
6561 hdr->sh_type = SHT_MIPS_UCODE;
6562 else if (strcmp (name, ".mdebug") == 0)
6563 {
6564 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 6565 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
6566 entsize of 0. FIXME: Does this matter? */
6567 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6568 hdr->sh_entsize = 0;
6569 else
6570 hdr->sh_entsize = 1;
6571 }
6572 else if (strcmp (name, ".reginfo") == 0)
6573 {
6574 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 6575 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
6576 entsize of 0x18. FIXME: Does this matter? */
6577 if (SGI_COMPAT (abfd))
6578 {
6579 if ((abfd->flags & DYNAMIC) != 0)
6580 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6581 else
6582 hdr->sh_entsize = 1;
6583 }
6584 else
6585 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6586 }
6587 else if (SGI_COMPAT (abfd)
6588 && (strcmp (name, ".hash") == 0
6589 || strcmp (name, ".dynamic") == 0
6590 || strcmp (name, ".dynstr") == 0))
6591 {
6592 if (SGI_COMPAT (abfd))
6593 hdr->sh_entsize = 0;
6594#if 0
8dc1a139 6595 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
6596 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6597#endif
6598 }
6599 else if (strcmp (name, ".got") == 0
6600 || strcmp (name, ".srdata") == 0
6601 || strcmp (name, ".sdata") == 0
6602 || strcmp (name, ".sbss") == 0
6603 || strcmp (name, ".lit4") == 0
6604 || strcmp (name, ".lit8") == 0)
6605 hdr->sh_flags |= SHF_MIPS_GPREL;
6606 else if (strcmp (name, ".MIPS.interfaces") == 0)
6607 {
6608 hdr->sh_type = SHT_MIPS_IFACE;
6609 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6610 }
0112cd26 6611 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
6612 {
6613 hdr->sh_type = SHT_MIPS_CONTENT;
6614 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6615 /* The sh_info field is set in final_write_processing. */
6616 }
cc2e31b9 6617 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
6618 {
6619 hdr->sh_type = SHT_MIPS_OPTIONS;
6620 hdr->sh_entsize = 1;
6621 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6622 }
1b315056
CS
6623 else if (CONST_STRNEQ (name, ".debug_")
6624 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
6625 {
6626 hdr->sh_type = SHT_MIPS_DWARF;
6627
6628 /* Irix facilities such as libexc expect a single .debug_frame
6629 per executable, the system ones have NOSTRIP set and the linker
6630 doesn't merge sections with different flags so ... */
6631 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6632 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6633 }
b49e97c9
TS
6634 else if (strcmp (name, ".MIPS.symlib") == 0)
6635 {
6636 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6637 /* The sh_link and sh_info fields are set in
6638 final_write_processing. */
6639 }
0112cd26
NC
6640 else if (CONST_STRNEQ (name, ".MIPS.events")
6641 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
6642 {
6643 hdr->sh_type = SHT_MIPS_EVENTS;
6644 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6645 /* The sh_link field is set in final_write_processing. */
6646 }
6647 else if (strcmp (name, ".msym") == 0)
6648 {
6649 hdr->sh_type = SHT_MIPS_MSYM;
6650 hdr->sh_flags |= SHF_ALLOC;
6651 hdr->sh_entsize = 8;
6652 }
6653
7a79a000
TS
6654 /* The generic elf_fake_sections will set up REL_HDR using the default
6655 kind of relocations. We used to set up a second header for the
6656 non-default kind of relocations here, but only NewABI would use
6657 these, and the IRIX ld doesn't like resulting empty RELA sections.
6658 Thus we create those header only on demand now. */
b49e97c9 6659
b34976b6 6660 return TRUE;
b49e97c9
TS
6661}
6662
6663/* Given a BFD section, try to locate the corresponding ELF section
6664 index. This is used by both the 32-bit and the 64-bit ABI.
6665 Actually, it's not clear to me that the 64-bit ABI supports these,
6666 but for non-PIC objects we will certainly want support for at least
6667 the .scommon section. */
6668
b34976b6 6669bfd_boolean
9719ad41
RS
6670_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6671 asection *sec, int *retval)
b49e97c9
TS
6672{
6673 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6674 {
6675 *retval = SHN_MIPS_SCOMMON;
b34976b6 6676 return TRUE;
b49e97c9
TS
6677 }
6678 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6679 {
6680 *retval = SHN_MIPS_ACOMMON;
b34976b6 6681 return TRUE;
b49e97c9 6682 }
b34976b6 6683 return FALSE;
b49e97c9
TS
6684}
6685\f
6686/* Hook called by the linker routine which adds symbols from an object
6687 file. We must handle the special MIPS section numbers here. */
6688
b34976b6 6689bfd_boolean
9719ad41 6690_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 6691 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
6692 flagword *flagsp ATTRIBUTE_UNUSED,
6693 asection **secp, bfd_vma *valp)
b49e97c9
TS
6694{
6695 if (SGI_COMPAT (abfd)
6696 && (abfd->flags & DYNAMIC) != 0
6697 && strcmp (*namep, "_rld_new_interface") == 0)
6698 {
8dc1a139 6699 /* Skip IRIX5 rld entry name. */
b49e97c9 6700 *namep = NULL;
b34976b6 6701 return TRUE;
b49e97c9
TS
6702 }
6703
eedecc07
DD
6704 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6705 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6706 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6707 a magic symbol resolved by the linker, we ignore this bogus definition
6708 of _gp_disp. New ABI objects do not suffer from this problem so this
6709 is not done for them. */
6710 if (!NEWABI_P(abfd)
6711 && (sym->st_shndx == SHN_ABS)
6712 && (strcmp (*namep, "_gp_disp") == 0))
6713 {
6714 *namep = NULL;
6715 return TRUE;
6716 }
6717
b49e97c9
TS
6718 switch (sym->st_shndx)
6719 {
6720 case SHN_COMMON:
6721 /* Common symbols less than the GP size are automatically
6722 treated as SHN_MIPS_SCOMMON symbols. */
6723 if (sym->st_size > elf_gp_size (abfd)
b59eed79 6724 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
6725 || IRIX_COMPAT (abfd) == ict_irix6)
6726 break;
6727 /* Fall through. */
6728 case SHN_MIPS_SCOMMON:
6729 *secp = bfd_make_section_old_way (abfd, ".scommon");
6730 (*secp)->flags |= SEC_IS_COMMON;
6731 *valp = sym->st_size;
6732 break;
6733
6734 case SHN_MIPS_TEXT:
6735 /* This section is used in a shared object. */
6736 if (elf_tdata (abfd)->elf_text_section == NULL)
6737 {
6738 asymbol *elf_text_symbol;
6739 asection *elf_text_section;
6740 bfd_size_type amt = sizeof (asection);
6741
6742 elf_text_section = bfd_zalloc (abfd, amt);
6743 if (elf_text_section == NULL)
b34976b6 6744 return FALSE;
b49e97c9
TS
6745
6746 amt = sizeof (asymbol);
6747 elf_text_symbol = bfd_zalloc (abfd, amt);
6748 if (elf_text_symbol == NULL)
b34976b6 6749 return FALSE;
b49e97c9
TS
6750
6751 /* Initialize the section. */
6752
6753 elf_tdata (abfd)->elf_text_section = elf_text_section;
6754 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6755
6756 elf_text_section->symbol = elf_text_symbol;
6757 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6758
6759 elf_text_section->name = ".text";
6760 elf_text_section->flags = SEC_NO_FLAGS;
6761 elf_text_section->output_section = NULL;
6762 elf_text_section->owner = abfd;
6763 elf_text_symbol->name = ".text";
6764 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6765 elf_text_symbol->section = elf_text_section;
6766 }
6767 /* This code used to do *secp = bfd_und_section_ptr if
6768 info->shared. I don't know why, and that doesn't make sense,
6769 so I took it out. */
6770 *secp = elf_tdata (abfd)->elf_text_section;
6771 break;
6772
6773 case SHN_MIPS_ACOMMON:
6774 /* Fall through. XXX Can we treat this as allocated data? */
6775 case SHN_MIPS_DATA:
6776 /* This section is used in a shared object. */
6777 if (elf_tdata (abfd)->elf_data_section == NULL)
6778 {
6779 asymbol *elf_data_symbol;
6780 asection *elf_data_section;
6781 bfd_size_type amt = sizeof (asection);
6782
6783 elf_data_section = bfd_zalloc (abfd, amt);
6784 if (elf_data_section == NULL)
b34976b6 6785 return FALSE;
b49e97c9
TS
6786
6787 amt = sizeof (asymbol);
6788 elf_data_symbol = bfd_zalloc (abfd, amt);
6789 if (elf_data_symbol == NULL)
b34976b6 6790 return FALSE;
b49e97c9
TS
6791
6792 /* Initialize the section. */
6793
6794 elf_tdata (abfd)->elf_data_section = elf_data_section;
6795 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6796
6797 elf_data_section->symbol = elf_data_symbol;
6798 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6799
6800 elf_data_section->name = ".data";
6801 elf_data_section->flags = SEC_NO_FLAGS;
6802 elf_data_section->output_section = NULL;
6803 elf_data_section->owner = abfd;
6804 elf_data_symbol->name = ".data";
6805 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6806 elf_data_symbol->section = elf_data_section;
6807 }
6808 /* This code used to do *secp = bfd_und_section_ptr if
6809 info->shared. I don't know why, and that doesn't make sense,
6810 so I took it out. */
6811 *secp = elf_tdata (abfd)->elf_data_section;
6812 break;
6813
6814 case SHN_MIPS_SUNDEFINED:
6815 *secp = bfd_und_section_ptr;
6816 break;
6817 }
6818
6819 if (SGI_COMPAT (abfd)
6820 && ! info->shared
f13a99db 6821 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
6822 && strcmp (*namep, "__rld_obj_head") == 0)
6823 {
6824 struct elf_link_hash_entry *h;
14a793b2 6825 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6826
6827 /* Mark __rld_obj_head as dynamic. */
14a793b2 6828 bh = NULL;
b49e97c9 6829 if (! (_bfd_generic_link_add_one_symbol
9719ad41 6830 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 6831 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6832 return FALSE;
14a793b2
AM
6833
6834 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6835 h->non_elf = 0;
6836 h->def_regular = 1;
b49e97c9
TS
6837 h->type = STT_OBJECT;
6838
c152c796 6839 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6840 return FALSE;
b49e97c9 6841
b34976b6 6842 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b49e97c9
TS
6843 }
6844
6845 /* If this is a mips16 text symbol, add 1 to the value to make it
6846 odd. This will cause something like .word SYM to come up with
6847 the right value when it is loaded into the PC. */
30c09090 6848 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
6849 ++*valp;
6850
b34976b6 6851 return TRUE;
b49e97c9
TS
6852}
6853
6854/* This hook function is called before the linker writes out a global
6855 symbol. We mark symbols as small common if appropriate. This is
6856 also where we undo the increment of the value for a mips16 symbol. */
6857
6e0b88f1 6858int
9719ad41
RS
6859_bfd_mips_elf_link_output_symbol_hook
6860 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6861 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6862 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
6863{
6864 /* If we see a common symbol, which implies a relocatable link, then
6865 if a symbol was small common in an input file, mark it as small
6866 common in the output file. */
6867 if (sym->st_shndx == SHN_COMMON
6868 && strcmp (input_sec->name, ".scommon") == 0)
6869 sym->st_shndx = SHN_MIPS_SCOMMON;
6870
30c09090 6871 if (ELF_ST_IS_MIPS16 (sym->st_other))
79cda7cf 6872 sym->st_value &= ~1;
b49e97c9 6873
6e0b88f1 6874 return 1;
b49e97c9
TS
6875}
6876\f
6877/* Functions for the dynamic linker. */
6878
6879/* Create dynamic sections when linking against a dynamic object. */
6880
b34976b6 6881bfd_boolean
9719ad41 6882_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
6883{
6884 struct elf_link_hash_entry *h;
14a793b2 6885 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6886 flagword flags;
6887 register asection *s;
6888 const char * const *namep;
0a44bf69 6889 struct mips_elf_link_hash_table *htab;
b49e97c9 6890
0a44bf69 6891 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6892 BFD_ASSERT (htab != NULL);
6893
b49e97c9
TS
6894 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6895 | SEC_LINKER_CREATED | SEC_READONLY);
6896
0a44bf69
RS
6897 /* The psABI requires a read-only .dynamic section, but the VxWorks
6898 EABI doesn't. */
6899 if (!htab->is_vxworks)
b49e97c9 6900 {
0a44bf69
RS
6901 s = bfd_get_section_by_name (abfd, ".dynamic");
6902 if (s != NULL)
6903 {
6904 if (! bfd_set_section_flags (abfd, s, flags))
6905 return FALSE;
6906 }
b49e97c9
TS
6907 }
6908
6909 /* We need to create .got section. */
23cc69b6 6910 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
6911 return FALSE;
6912
0a44bf69 6913 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 6914 return FALSE;
b49e97c9 6915
b49e97c9 6916 /* Create .stub section. */
4e41d0d7
RS
6917 s = bfd_make_section_with_flags (abfd,
6918 MIPS_ELF_STUB_SECTION_NAME (abfd),
6919 flags | SEC_CODE);
6920 if (s == NULL
6921 || ! bfd_set_section_alignment (abfd, s,
6922 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6923 return FALSE;
6924 htab->sstubs = s;
b49e97c9
TS
6925
6926 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6927 && !info->shared
6928 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6929 {
3496cb2a
L
6930 s = bfd_make_section_with_flags (abfd, ".rld_map",
6931 flags &~ (flagword) SEC_READONLY);
b49e97c9 6932 if (s == NULL
b49e97c9
TS
6933 || ! bfd_set_section_alignment (abfd, s,
6934 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 6935 return FALSE;
b49e97c9
TS
6936 }
6937
6938 /* On IRIX5, we adjust add some additional symbols and change the
6939 alignments of several sections. There is no ABI documentation
6940 indicating that this is necessary on IRIX6, nor any evidence that
6941 the linker takes such action. */
6942 if (IRIX_COMPAT (abfd) == ict_irix5)
6943 {
6944 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6945 {
14a793b2 6946 bh = NULL;
b49e97c9 6947 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
6948 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6949 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6950 return FALSE;
14a793b2
AM
6951
6952 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6953 h->non_elf = 0;
6954 h->def_regular = 1;
b49e97c9
TS
6955 h->type = STT_SECTION;
6956
c152c796 6957 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6958 return FALSE;
b49e97c9
TS
6959 }
6960
6961 /* We need to create a .compact_rel section. */
6962 if (SGI_COMPAT (abfd))
6963 {
6964 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 6965 return FALSE;
b49e97c9
TS
6966 }
6967
44c410de 6968 /* Change alignments of some sections. */
b49e97c9
TS
6969 s = bfd_get_section_by_name (abfd, ".hash");
6970 if (s != NULL)
d80dcc6a 6971 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6972 s = bfd_get_section_by_name (abfd, ".dynsym");
6973 if (s != NULL)
d80dcc6a 6974 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6975 s = bfd_get_section_by_name (abfd, ".dynstr");
6976 if (s != NULL)
d80dcc6a 6977 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6978 s = bfd_get_section_by_name (abfd, ".reginfo");
6979 if (s != NULL)
d80dcc6a 6980 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6981 s = bfd_get_section_by_name (abfd, ".dynamic");
6982 if (s != NULL)
d80dcc6a 6983 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6984 }
6985
6986 if (!info->shared)
6987 {
14a793b2
AM
6988 const char *name;
6989
6990 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6991 bh = NULL;
6992 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
6993 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6994 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6995 return FALSE;
14a793b2
AM
6996
6997 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6998 h->non_elf = 0;
6999 h->def_regular = 1;
b49e97c9
TS
7000 h->type = STT_SECTION;
7001
c152c796 7002 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7003 return FALSE;
b49e97c9
TS
7004
7005 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7006 {
7007 /* __rld_map is a four byte word located in the .data section
7008 and is filled in by the rtld to contain a pointer to
7009 the _r_debug structure. Its symbol value will be set in
7010 _bfd_mips_elf_finish_dynamic_symbol. */
7011 s = bfd_get_section_by_name (abfd, ".rld_map");
0abfb97a 7012 BFD_ASSERT (s != NULL);
14a793b2 7013
0abfb97a
L
7014 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7015 bh = NULL;
7016 if (!(_bfd_generic_link_add_one_symbol
7017 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7018 get_elf_backend_data (abfd)->collect, &bh)))
7019 return FALSE;
b49e97c9 7020
0abfb97a
L
7021 h = (struct elf_link_hash_entry *) bh;
7022 h->non_elf = 0;
7023 h->def_regular = 1;
7024 h->type = STT_OBJECT;
7025
7026 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7027 return FALSE;
b49e97c9
TS
7028 }
7029 }
7030
861fb55a
DJ
7031 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7032 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7033 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7034 return FALSE;
7035
7036 /* Cache the sections created above. */
7037 htab->splt = bfd_get_section_by_name (abfd, ".plt");
7038 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
0a44bf69
RS
7039 if (htab->is_vxworks)
7040 {
0a44bf69
RS
7041 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7042 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
861fb55a
DJ
7043 }
7044 else
7045 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7046 if (!htab->sdynbss
7047 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7048 || !htab->srelplt
7049 || !htab->splt)
7050 abort ();
0a44bf69 7051
861fb55a
DJ
7052 if (htab->is_vxworks)
7053 {
0a44bf69
RS
7054 /* Do the usual VxWorks handling. */
7055 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7056 return FALSE;
7057
7058 /* Work out the PLT sizes. */
7059 if (info->shared)
7060 {
7061 htab->plt_header_size
7062 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7063 htab->plt_entry_size
7064 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7065 }
7066 else
7067 {
7068 htab->plt_header_size
7069 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7070 htab->plt_entry_size
7071 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7072 }
7073 }
861fb55a
DJ
7074 else if (!info->shared)
7075 {
7076 /* All variants of the plt0 entry are the same size. */
7077 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7078 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7079 }
0a44bf69 7080
b34976b6 7081 return TRUE;
b49e97c9
TS
7082}
7083\f
c224138d
RS
7084/* Return true if relocation REL against section SEC is a REL rather than
7085 RELA relocation. RELOCS is the first relocation in the section and
7086 ABFD is the bfd that contains SEC. */
7087
7088static bfd_boolean
7089mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7090 const Elf_Internal_Rela *relocs,
7091 const Elf_Internal_Rela *rel)
7092{
7093 Elf_Internal_Shdr *rel_hdr;
7094 const struct elf_backend_data *bed;
7095
d4730f92
BS
7096 /* To determine which flavor of relocation this is, we depend on the
7097 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7098 rel_hdr = elf_section_data (sec)->rel.hdr;
7099 if (rel_hdr == NULL)
7100 return FALSE;
c224138d 7101 bed = get_elf_backend_data (abfd);
d4730f92
BS
7102 return ((size_t) (rel - relocs)
7103 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
c224138d
RS
7104}
7105
7106/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7107 HOWTO is the relocation's howto and CONTENTS points to the contents
7108 of the section that REL is against. */
7109
7110static bfd_vma
7111mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7112 reloc_howto_type *howto, bfd_byte *contents)
7113{
7114 bfd_byte *location;
7115 unsigned int r_type;
7116 bfd_vma addend;
7117
7118 r_type = ELF_R_TYPE (abfd, rel->r_info);
7119 location = contents + rel->r_offset;
7120
7121 /* Get the addend, which is stored in the input file. */
7122 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7123 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7124 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7125
7126 return addend & howto->src_mask;
7127}
7128
7129/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7130 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7131 and update *ADDEND with the final addend. Return true on success
7132 or false if the LO16 could not be found. RELEND is the exclusive
7133 upper bound on the relocations for REL's section. */
7134
7135static bfd_boolean
7136mips_elf_add_lo16_rel_addend (bfd *abfd,
7137 const Elf_Internal_Rela *rel,
7138 const Elf_Internal_Rela *relend,
7139 bfd_byte *contents, bfd_vma *addend)
7140{
7141 unsigned int r_type, lo16_type;
7142 const Elf_Internal_Rela *lo16_relocation;
7143 reloc_howto_type *lo16_howto;
7144 bfd_vma l;
7145
7146 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7147 if (mips16_reloc_p (r_type))
c224138d
RS
7148 lo16_type = R_MIPS16_LO16;
7149 else
7150 lo16_type = R_MIPS_LO16;
7151
7152 /* The combined value is the sum of the HI16 addend, left-shifted by
7153 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7154 code does a `lui' of the HI16 value, and then an `addiu' of the
7155 LO16 value.)
7156
7157 Scan ahead to find a matching LO16 relocation.
7158
7159 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7160 be immediately following. However, for the IRIX6 ABI, the next
7161 relocation may be a composed relocation consisting of several
7162 relocations for the same address. In that case, the R_MIPS_LO16
7163 relocation may occur as one of these. We permit a similar
7164 extension in general, as that is useful for GCC.
7165
7166 In some cases GCC dead code elimination removes the LO16 but keeps
7167 the corresponding HI16. This is strictly speaking a violation of
7168 the ABI but not immediately harmful. */
7169 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7170 if (lo16_relocation == NULL)
7171 return FALSE;
7172
7173 /* Obtain the addend kept there. */
7174 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7175 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7176
7177 l <<= lo16_howto->rightshift;
7178 l = _bfd_mips_elf_sign_extend (l, 16);
7179
7180 *addend <<= 16;
7181 *addend += l;
7182 return TRUE;
7183}
7184
7185/* Try to read the contents of section SEC in bfd ABFD. Return true and
7186 store the contents in *CONTENTS on success. Assume that *CONTENTS
7187 already holds the contents if it is nonull on entry. */
7188
7189static bfd_boolean
7190mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7191{
7192 if (*contents)
7193 return TRUE;
7194
7195 /* Get cached copy if it exists. */
7196 if (elf_section_data (sec)->this_hdr.contents != NULL)
7197 {
7198 *contents = elf_section_data (sec)->this_hdr.contents;
7199 return TRUE;
7200 }
7201
7202 return bfd_malloc_and_get_section (abfd, sec, contents);
7203}
7204
b49e97c9
TS
7205/* Look through the relocs for a section during the first phase, and
7206 allocate space in the global offset table. */
7207
b34976b6 7208bfd_boolean
9719ad41
RS
7209_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7210 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7211{
7212 const char *name;
7213 bfd *dynobj;
7214 Elf_Internal_Shdr *symtab_hdr;
7215 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7216 size_t extsymoff;
7217 const Elf_Internal_Rela *rel;
7218 const Elf_Internal_Rela *rel_end;
b49e97c9 7219 asection *sreloc;
9c5bfbb7 7220 const struct elf_backend_data *bed;
0a44bf69 7221 struct mips_elf_link_hash_table *htab;
c224138d
RS
7222 bfd_byte *contents;
7223 bfd_vma addend;
7224 reloc_howto_type *howto;
b49e97c9 7225
1049f94e 7226 if (info->relocatable)
b34976b6 7227 return TRUE;
b49e97c9 7228
0a44bf69 7229 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7230 BFD_ASSERT (htab != NULL);
7231
b49e97c9
TS
7232 dynobj = elf_hash_table (info)->dynobj;
7233 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7234 sym_hashes = elf_sym_hashes (abfd);
7235 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7236
738e5348
RS
7237 bed = get_elf_backend_data (abfd);
7238 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7239
b49e97c9
TS
7240 /* Check for the mips16 stub sections. */
7241
7242 name = bfd_get_section_name (abfd, sec);
b9d58d71 7243 if (FN_STUB_P (name))
b49e97c9
TS
7244 {
7245 unsigned long r_symndx;
7246
7247 /* Look at the relocation information to figure out which symbol
7248 this is for. */
7249
738e5348
RS
7250 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7251 if (r_symndx == 0)
7252 {
7253 (*_bfd_error_handler)
7254 (_("%B: Warning: cannot determine the target function for"
7255 " stub section `%s'"),
7256 abfd, name);
7257 bfd_set_error (bfd_error_bad_value);
7258 return FALSE;
7259 }
b49e97c9
TS
7260
7261 if (r_symndx < extsymoff
7262 || sym_hashes[r_symndx - extsymoff] == NULL)
7263 {
7264 asection *o;
7265
7266 /* This stub is for a local symbol. This stub will only be
7267 needed if there is some relocation in this BFD, other
7268 than a 16 bit function call, which refers to this symbol. */
7269 for (o = abfd->sections; o != NULL; o = o->next)
7270 {
7271 Elf_Internal_Rela *sec_relocs;
7272 const Elf_Internal_Rela *r, *rend;
7273
7274 /* We can ignore stub sections when looking for relocs. */
7275 if ((o->flags & SEC_RELOC) == 0
7276 || o->reloc_count == 0
738e5348 7277 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7278 continue;
7279
45d6a902 7280 sec_relocs
9719ad41 7281 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7282 info->keep_memory);
b49e97c9 7283 if (sec_relocs == NULL)
b34976b6 7284 return FALSE;
b49e97c9
TS
7285
7286 rend = sec_relocs + o->reloc_count;
7287 for (r = sec_relocs; r < rend; r++)
7288 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7289 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7290 break;
7291
6cdc0ccc 7292 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7293 free (sec_relocs);
7294
7295 if (r < rend)
7296 break;
7297 }
7298
7299 if (o == NULL)
7300 {
7301 /* There is no non-call reloc for this stub, so we do
7302 not need it. Since this function is called before
7303 the linker maps input sections to output sections, we
7304 can easily discard it by setting the SEC_EXCLUDE
7305 flag. */
7306 sec->flags |= SEC_EXCLUDE;
b34976b6 7307 return TRUE;
b49e97c9
TS
7308 }
7309
7310 /* Record this stub in an array of local symbol stubs for
7311 this BFD. */
7312 if (elf_tdata (abfd)->local_stubs == NULL)
7313 {
7314 unsigned long symcount;
7315 asection **n;
7316 bfd_size_type amt;
7317
7318 if (elf_bad_symtab (abfd))
7319 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7320 else
7321 symcount = symtab_hdr->sh_info;
7322 amt = symcount * sizeof (asection *);
9719ad41 7323 n = bfd_zalloc (abfd, amt);
b49e97c9 7324 if (n == NULL)
b34976b6 7325 return FALSE;
b49e97c9
TS
7326 elf_tdata (abfd)->local_stubs = n;
7327 }
7328
b9d58d71 7329 sec->flags |= SEC_KEEP;
b49e97c9
TS
7330 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7331
7332 /* We don't need to set mips16_stubs_seen in this case.
7333 That flag is used to see whether we need to look through
7334 the global symbol table for stubs. We don't need to set
7335 it here, because we just have a local stub. */
7336 }
7337 else
7338 {
7339 struct mips_elf_link_hash_entry *h;
7340
7341 h = ((struct mips_elf_link_hash_entry *)
7342 sym_hashes[r_symndx - extsymoff]);
7343
973a3492
L
7344 while (h->root.root.type == bfd_link_hash_indirect
7345 || h->root.root.type == bfd_link_hash_warning)
7346 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7347
b49e97c9
TS
7348 /* H is the symbol this stub is for. */
7349
b9d58d71
TS
7350 /* If we already have an appropriate stub for this function, we
7351 don't need another one, so we can discard this one. Since
7352 this function is called before the linker maps input sections
7353 to output sections, we can easily discard it by setting the
7354 SEC_EXCLUDE flag. */
7355 if (h->fn_stub != NULL)
7356 {
7357 sec->flags |= SEC_EXCLUDE;
7358 return TRUE;
7359 }
7360
7361 sec->flags |= SEC_KEEP;
b49e97c9 7362 h->fn_stub = sec;
b34976b6 7363 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7364 }
7365 }
b9d58d71 7366 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7367 {
7368 unsigned long r_symndx;
7369 struct mips_elf_link_hash_entry *h;
7370 asection **loc;
7371
7372 /* Look at the relocation information to figure out which symbol
7373 this is for. */
7374
738e5348
RS
7375 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7376 if (r_symndx == 0)
7377 {
7378 (*_bfd_error_handler)
7379 (_("%B: Warning: cannot determine the target function for"
7380 " stub section `%s'"),
7381 abfd, name);
7382 bfd_set_error (bfd_error_bad_value);
7383 return FALSE;
7384 }
b49e97c9
TS
7385
7386 if (r_symndx < extsymoff
7387 || sym_hashes[r_symndx - extsymoff] == NULL)
7388 {
b9d58d71 7389 asection *o;
b49e97c9 7390
b9d58d71
TS
7391 /* This stub is for a local symbol. This stub will only be
7392 needed if there is some relocation (R_MIPS16_26) in this BFD
7393 that refers to this symbol. */
7394 for (o = abfd->sections; o != NULL; o = o->next)
7395 {
7396 Elf_Internal_Rela *sec_relocs;
7397 const Elf_Internal_Rela *r, *rend;
7398
7399 /* We can ignore stub sections when looking for relocs. */
7400 if ((o->flags & SEC_RELOC) == 0
7401 || o->reloc_count == 0
738e5348 7402 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7403 continue;
7404
7405 sec_relocs
7406 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7407 info->keep_memory);
7408 if (sec_relocs == NULL)
7409 return FALSE;
7410
7411 rend = sec_relocs + o->reloc_count;
7412 for (r = sec_relocs; r < rend; r++)
7413 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7414 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7415 break;
7416
7417 if (elf_section_data (o)->relocs != sec_relocs)
7418 free (sec_relocs);
7419
7420 if (r < rend)
7421 break;
7422 }
7423
7424 if (o == NULL)
7425 {
7426 /* There is no non-call reloc for this stub, so we do
7427 not need it. Since this function is called before
7428 the linker maps input sections to output sections, we
7429 can easily discard it by setting the SEC_EXCLUDE
7430 flag. */
7431 sec->flags |= SEC_EXCLUDE;
7432 return TRUE;
7433 }
7434
7435 /* Record this stub in an array of local symbol call_stubs for
7436 this BFD. */
7437 if (elf_tdata (abfd)->local_call_stubs == NULL)
7438 {
7439 unsigned long symcount;
7440 asection **n;
7441 bfd_size_type amt;
7442
7443 if (elf_bad_symtab (abfd))
7444 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7445 else
7446 symcount = symtab_hdr->sh_info;
7447 amt = symcount * sizeof (asection *);
7448 n = bfd_zalloc (abfd, amt);
7449 if (n == NULL)
7450 return FALSE;
7451 elf_tdata (abfd)->local_call_stubs = n;
7452 }
b49e97c9 7453
b9d58d71
TS
7454 sec->flags |= SEC_KEEP;
7455 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7456
b9d58d71
TS
7457 /* We don't need to set mips16_stubs_seen in this case.
7458 That flag is used to see whether we need to look through
7459 the global symbol table for stubs. We don't need to set
7460 it here, because we just have a local stub. */
7461 }
b49e97c9 7462 else
b49e97c9 7463 {
b9d58d71
TS
7464 h = ((struct mips_elf_link_hash_entry *)
7465 sym_hashes[r_symndx - extsymoff]);
7466
7467 /* H is the symbol this stub is for. */
7468
7469 if (CALL_FP_STUB_P (name))
7470 loc = &h->call_fp_stub;
7471 else
7472 loc = &h->call_stub;
7473
7474 /* If we already have an appropriate stub for this function, we
7475 don't need another one, so we can discard this one. Since
7476 this function is called before the linker maps input sections
7477 to output sections, we can easily discard it by setting the
7478 SEC_EXCLUDE flag. */
7479 if (*loc != NULL)
7480 {
7481 sec->flags |= SEC_EXCLUDE;
7482 return TRUE;
7483 }
b49e97c9 7484
b9d58d71
TS
7485 sec->flags |= SEC_KEEP;
7486 *loc = sec;
7487 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7488 }
b49e97c9
TS
7489 }
7490
b49e97c9 7491 sreloc = NULL;
c224138d 7492 contents = NULL;
b49e97c9
TS
7493 for (rel = relocs; rel < rel_end; ++rel)
7494 {
7495 unsigned long r_symndx;
7496 unsigned int r_type;
7497 struct elf_link_hash_entry *h;
861fb55a 7498 bfd_boolean can_make_dynamic_p;
b49e97c9
TS
7499
7500 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7501 r_type = ELF_R_TYPE (abfd, rel->r_info);
7502
7503 if (r_symndx < extsymoff)
7504 h = NULL;
7505 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7506 {
7507 (*_bfd_error_handler)
d003868e
AM
7508 (_("%B: Malformed reloc detected for section %s"),
7509 abfd, name);
b49e97c9 7510 bfd_set_error (bfd_error_bad_value);
b34976b6 7511 return FALSE;
b49e97c9
TS
7512 }
7513 else
7514 {
7515 h = sym_hashes[r_symndx - extsymoff];
3e08fb72
NC
7516 while (h != NULL
7517 && (h->root.type == bfd_link_hash_indirect
7518 || h->root.type == bfd_link_hash_warning))
861fb55a
DJ
7519 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7520 }
b49e97c9 7521
861fb55a
DJ
7522 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7523 relocation into a dynamic one. */
7524 can_make_dynamic_p = FALSE;
7525 switch (r_type)
7526 {
7527 case R_MIPS16_GOT16:
7528 case R_MIPS16_CALL16:
7529 case R_MIPS_GOT16:
7530 case R_MIPS_CALL16:
7531 case R_MIPS_CALL_HI16:
7532 case R_MIPS_CALL_LO16:
7533 case R_MIPS_GOT_HI16:
7534 case R_MIPS_GOT_LO16:
7535 case R_MIPS_GOT_PAGE:
7536 case R_MIPS_GOT_OFST:
7537 case R_MIPS_GOT_DISP:
7538 case R_MIPS_TLS_GOTTPREL:
7539 case R_MIPS_TLS_GD:
7540 case R_MIPS_TLS_LDM:
7541 if (dynobj == NULL)
7542 elf_hash_table (info)->dynobj = dynobj = abfd;
7543 if (!mips_elf_create_got_section (dynobj, info))
7544 return FALSE;
7545 if (htab->is_vxworks && !info->shared)
b49e97c9 7546 {
861fb55a
DJ
7547 (*_bfd_error_handler)
7548 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7549 abfd, (unsigned long) rel->r_offset);
7550 bfd_set_error (bfd_error_bad_value);
7551 return FALSE;
b49e97c9 7552 }
861fb55a 7553 break;
b49e97c9 7554
99da6b5f
AN
7555 /* This is just a hint; it can safely be ignored. Don't set
7556 has_static_relocs for the corresponding symbol. */
7557 case R_MIPS_JALR:
7558 break;
7559
861fb55a
DJ
7560 case R_MIPS_32:
7561 case R_MIPS_REL32:
7562 case R_MIPS_64:
7563 /* In VxWorks executables, references to external symbols
7564 must be handled using copy relocs or PLT entries; it is not
7565 possible to convert this relocation into a dynamic one.
7566
7567 For executables that use PLTs and copy-relocs, we have a
7568 choice between converting the relocation into a dynamic
7569 one or using copy relocations or PLT entries. It is
7570 usually better to do the former, unless the relocation is
7571 against a read-only section. */
7572 if ((info->shared
7573 || (h != NULL
7574 && !htab->is_vxworks
7575 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7576 && !(!info->nocopyreloc
7577 && !PIC_OBJECT_P (abfd)
7578 && MIPS_ELF_READONLY_SECTION (sec))))
7579 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 7580 {
861fb55a 7581 can_make_dynamic_p = TRUE;
b49e97c9
TS
7582 if (dynobj == NULL)
7583 elf_hash_table (info)->dynobj = dynobj = abfd;
b49e97c9 7584 break;
861fb55a
DJ
7585 }
7586 /* Fall through. */
b49e97c9 7587
861fb55a
DJ
7588 default:
7589 /* Most static relocations require pointer equality, except
7590 for branches. */
7591 if (h)
7592 h->pointer_equality_needed = TRUE;
7593 /* Fall through. */
b49e97c9 7594
861fb55a
DJ
7595 case R_MIPS_26:
7596 case R_MIPS_PC16:
7597 case R_MIPS16_26:
7598 if (h)
7599 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7600 break;
b49e97c9
TS
7601 }
7602
0a44bf69
RS
7603 if (h)
7604 {
0a44bf69
RS
7605 /* Relocations against the special VxWorks __GOTT_BASE__ and
7606 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7607 room for them in .rela.dyn. */
7608 if (is_gott_symbol (info, h))
7609 {
7610 if (sreloc == NULL)
7611 {
7612 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7613 if (sreloc == NULL)
7614 return FALSE;
7615 }
7616 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
7617 if (MIPS_ELF_READONLY_SECTION (sec))
7618 /* We tell the dynamic linker that there are
7619 relocations against the text segment. */
7620 info->flags |= DF_TEXTREL;
0a44bf69
RS
7621 }
7622 }
7623 else if (r_type == R_MIPS_CALL_LO16
7624 || r_type == R_MIPS_GOT_LO16
7625 || r_type == R_MIPS_GOT_DISP
738e5348 7626 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
7627 {
7628 /* We may need a local GOT entry for this relocation. We
7629 don't count R_MIPS_GOT_PAGE because we can estimate the
7630 maximum number of pages needed by looking at the size of
738e5348
RS
7631 the segment. Similar comments apply to R_MIPS*_GOT16 and
7632 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 7633 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 7634 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 7635 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0
RS
7636 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7637 rel->r_addend, info, 0))
f4416af6 7638 return FALSE;
b49e97c9
TS
7639 }
7640
861fb55a
DJ
7641 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7642 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7643
b49e97c9
TS
7644 switch (r_type)
7645 {
7646 case R_MIPS_CALL16:
738e5348 7647 case R_MIPS16_CALL16:
b49e97c9
TS
7648 if (h == NULL)
7649 {
7650 (*_bfd_error_handler)
d003868e
AM
7651 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7652 abfd, (unsigned long) rel->r_offset);
b49e97c9 7653 bfd_set_error (bfd_error_bad_value);
b34976b6 7654 return FALSE;
b49e97c9
TS
7655 }
7656 /* Fall through. */
7657
7658 case R_MIPS_CALL_HI16:
7659 case R_MIPS_CALL_LO16:
7660 if (h != NULL)
7661 {
6ccf4795
RS
7662 /* Make sure there is room in the regular GOT to hold the
7663 function's address. We may eliminate it in favour of
7664 a .got.plt entry later; see mips_elf_count_got_symbols. */
7665 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE, 0))
b34976b6 7666 return FALSE;
b49e97c9
TS
7667
7668 /* We need a stub, not a plt entry for the undefined
7669 function. But we record it as if it needs plt. See
c152c796 7670 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 7671 h->needs_plt = 1;
b49e97c9
TS
7672 h->type = STT_FUNC;
7673 }
7674 break;
7675
0fdc1bf1
AO
7676 case R_MIPS_GOT_PAGE:
7677 /* If this is a global, overridable symbol, GOT_PAGE will
7678 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d 7679 if (h)
0fdc1bf1
AO
7680 {
7681 struct mips_elf_link_hash_entry *hmips =
7682 (struct mips_elf_link_hash_entry *) h;
143d77c5 7683
3a3b6725 7684 /* This symbol is definitely not overridable. */
f5385ebf 7685 if (hmips->root.def_regular
0fdc1bf1 7686 && ! (info->shared && ! info->symbolic
f5385ebf 7687 && ! hmips->root.forced_local))
c224138d 7688 h = NULL;
0fdc1bf1
AO
7689 }
7690 /* Fall through. */
7691
738e5348 7692 case R_MIPS16_GOT16:
b49e97c9
TS
7693 case R_MIPS_GOT16:
7694 case R_MIPS_GOT_HI16:
7695 case R_MIPS_GOT_LO16:
3a3b6725 7696 if (!h || r_type == R_MIPS_GOT_PAGE)
c224138d 7697 {
3a3b6725
DJ
7698 /* This relocation needs (or may need, if h != NULL) a
7699 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7700 know for sure until we know whether the symbol is
7701 preemptible. */
c224138d
RS
7702 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7703 {
7704 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7705 return FALSE;
7706 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7707 addend = mips_elf_read_rel_addend (abfd, rel,
7708 howto, contents);
9684f078 7709 if (got16_reloc_p (r_type))
c224138d
RS
7710 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7711 contents, &addend);
7712 else
7713 addend <<= howto->rightshift;
7714 }
7715 else
7716 addend = rel->r_addend;
a8028dd0
RS
7717 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7718 addend))
c224138d
RS
7719 return FALSE;
7720 break;
7721 }
7722 /* Fall through. */
7723
b49e97c9 7724 case R_MIPS_GOT_DISP:
6ccf4795
RS
7725 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
7726 FALSE, 0))
b34976b6 7727 return FALSE;
b49e97c9
TS
7728 break;
7729
0f20cc35
DJ
7730 case R_MIPS_TLS_GOTTPREL:
7731 if (info->shared)
7732 info->flags |= DF_STATIC_TLS;
7733 /* Fall through */
7734
7735 case R_MIPS_TLS_LDM:
7736 if (r_type == R_MIPS_TLS_LDM)
7737 {
cf35638d 7738 r_symndx = STN_UNDEF;
0f20cc35
DJ
7739 h = NULL;
7740 }
7741 /* Fall through */
7742
7743 case R_MIPS_TLS_GD:
7744 /* This symbol requires a global offset table entry, or two
7745 for TLS GD relocations. */
7746 {
7747 unsigned char flag = (r_type == R_MIPS_TLS_GD
7748 ? GOT_TLS_GD
7749 : r_type == R_MIPS_TLS_LDM
7750 ? GOT_TLS_LDM
7751 : GOT_TLS_IE);
7752 if (h != NULL)
7753 {
7754 struct mips_elf_link_hash_entry *hmips =
7755 (struct mips_elf_link_hash_entry *) h;
7756 hmips->tls_type |= flag;
7757
6ccf4795
RS
7758 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
7759 FALSE, flag))
0f20cc35
DJ
7760 return FALSE;
7761 }
7762 else
7763 {
cf35638d 7764 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != STN_UNDEF);
0f20cc35 7765
a8028dd0
RS
7766 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7767 rel->r_addend,
7768 info, flag))
0f20cc35
DJ
7769 return FALSE;
7770 }
7771 }
7772 break;
7773
b49e97c9
TS
7774 case R_MIPS_32:
7775 case R_MIPS_REL32:
7776 case R_MIPS_64:
0a44bf69
RS
7777 /* In VxWorks executables, references to external symbols
7778 are handled using copy relocs or PLT stubs, so there's
7779 no need to add a .rela.dyn entry for this relocation. */
861fb55a 7780 if (can_make_dynamic_p)
b49e97c9
TS
7781 {
7782 if (sreloc == NULL)
7783 {
0a44bf69 7784 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 7785 if (sreloc == NULL)
f4416af6 7786 return FALSE;
b49e97c9 7787 }
9a59ad6b 7788 if (info->shared && h == NULL)
82f0cfbd
EC
7789 {
7790 /* When creating a shared object, we must copy these
7791 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
7792 relocs. Make room for this reloc in .rel(a).dyn. */
7793 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 7794 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7795 /* We tell the dynamic linker that there are
7796 relocations against the text segment. */
7797 info->flags |= DF_TEXTREL;
7798 }
b49e97c9
TS
7799 else
7800 {
7801 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 7802
9a59ad6b
DJ
7803 /* For a shared object, we must copy this relocation
7804 unless the symbol turns out to be undefined and
7805 weak with non-default visibility, in which case
7806 it will be left as zero.
7807
7808 We could elide R_MIPS_REL32 for locally binding symbols
7809 in shared libraries, but do not yet do so.
7810
7811 For an executable, we only need to copy this
7812 reloc if the symbol is defined in a dynamic
7813 object. */
b49e97c9
TS
7814 hmips = (struct mips_elf_link_hash_entry *) h;
7815 ++hmips->possibly_dynamic_relocs;
943284cc 7816 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7817 /* We need it to tell the dynamic linker if there
7818 are relocations against the text segment. */
7819 hmips->readonly_reloc = TRUE;
b49e97c9 7820 }
b49e97c9
TS
7821 }
7822
7823 if (SGI_COMPAT (abfd))
7824 mips_elf_hash_table (info)->compact_rel_size +=
7825 sizeof (Elf32_External_crinfo);
7826 break;
7827
7828 case R_MIPS_26:
7829 case R_MIPS_GPREL16:
7830 case R_MIPS_LITERAL:
7831 case R_MIPS_GPREL32:
7832 if (SGI_COMPAT (abfd))
7833 mips_elf_hash_table (info)->compact_rel_size +=
7834 sizeof (Elf32_External_crinfo);
7835 break;
7836
7837 /* This relocation describes the C++ object vtable hierarchy.
7838 Reconstruct it for later use during GC. */
7839 case R_MIPS_GNU_VTINHERIT:
c152c796 7840 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 7841 return FALSE;
b49e97c9
TS
7842 break;
7843
7844 /* This relocation describes which C++ vtable entries are actually
7845 used. Record for later use during GC. */
7846 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
7847 BFD_ASSERT (h != NULL);
7848 if (h != NULL
7849 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 7850 return FALSE;
b49e97c9
TS
7851 break;
7852
7853 default:
7854 break;
7855 }
7856
7857 /* We must not create a stub for a symbol that has relocations
0a44bf69
RS
7858 related to taking the function's address. This doesn't apply to
7859 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7860 a normal .got entry. */
7861 if (!htab->is_vxworks && h != NULL)
7862 switch (r_type)
7863 {
7864 default:
7865 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7866 break;
738e5348 7867 case R_MIPS16_CALL16:
0a44bf69
RS
7868 case R_MIPS_CALL16:
7869 case R_MIPS_CALL_HI16:
7870 case R_MIPS_CALL_LO16:
7871 case R_MIPS_JALR:
7872 break;
7873 }
b49e97c9 7874
738e5348
RS
7875 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7876 if there is one. We only need to handle global symbols here;
7877 we decide whether to keep or delete stubs for local symbols
7878 when processing the stub's relocations. */
b49e97c9 7879 if (h != NULL
738e5348
RS
7880 && !mips16_call_reloc_p (r_type)
7881 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
7882 {
7883 struct mips_elf_link_hash_entry *mh;
7884
7885 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 7886 mh->need_fn_stub = TRUE;
b49e97c9 7887 }
861fb55a
DJ
7888
7889 /* Refuse some position-dependent relocations when creating a
7890 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7891 not PIC, but we can create dynamic relocations and the result
7892 will be fine. Also do not refuse R_MIPS_LO16, which can be
7893 combined with R_MIPS_GOT16. */
7894 if (info->shared)
7895 {
7896 switch (r_type)
7897 {
7898 case R_MIPS16_HI16:
7899 case R_MIPS_HI16:
7900 case R_MIPS_HIGHER:
7901 case R_MIPS_HIGHEST:
7902 /* Don't refuse a high part relocation if it's against
7903 no symbol (e.g. part of a compound relocation). */
cf35638d 7904 if (r_symndx == STN_UNDEF)
861fb55a
DJ
7905 break;
7906
7907 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7908 and has a special meaning. */
7909 if (!NEWABI_P (abfd) && h != NULL
7910 && strcmp (h->root.root.string, "_gp_disp") == 0)
7911 break;
7912
0fc1eb3c
RS
7913 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
7914 if (is_gott_symbol (info, h))
7915 break;
7916
861fb55a
DJ
7917 /* FALLTHROUGH */
7918
7919 case R_MIPS16_26:
7920 case R_MIPS_26:
7921 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7922 (*_bfd_error_handler)
7923 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7924 abfd, howto->name,
7925 (h) ? h->root.root.string : "a local symbol");
7926 bfd_set_error (bfd_error_bad_value);
7927 return FALSE;
7928 default:
7929 break;
7930 }
7931 }
b49e97c9
TS
7932 }
7933
b34976b6 7934 return TRUE;
b49e97c9
TS
7935}
7936\f
d0647110 7937bfd_boolean
9719ad41
RS
7938_bfd_mips_relax_section (bfd *abfd, asection *sec,
7939 struct bfd_link_info *link_info,
7940 bfd_boolean *again)
d0647110
AO
7941{
7942 Elf_Internal_Rela *internal_relocs;
7943 Elf_Internal_Rela *irel, *irelend;
7944 Elf_Internal_Shdr *symtab_hdr;
7945 bfd_byte *contents = NULL;
d0647110
AO
7946 size_t extsymoff;
7947 bfd_boolean changed_contents = FALSE;
7948 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7949 Elf_Internal_Sym *isymbuf = NULL;
7950
7951 /* We are not currently changing any sizes, so only one pass. */
7952 *again = FALSE;
7953
1049f94e 7954 if (link_info->relocatable)
d0647110
AO
7955 return TRUE;
7956
9719ad41 7957 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 7958 link_info->keep_memory);
d0647110
AO
7959 if (internal_relocs == NULL)
7960 return TRUE;
7961
7962 irelend = internal_relocs + sec->reloc_count
7963 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7964 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7965 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7966
7967 for (irel = internal_relocs; irel < irelend; irel++)
7968 {
7969 bfd_vma symval;
7970 bfd_signed_vma sym_offset;
7971 unsigned int r_type;
7972 unsigned long r_symndx;
7973 asection *sym_sec;
7974 unsigned long instruction;
7975
7976 /* Turn jalr into bgezal, and jr into beq, if they're marked
7977 with a JALR relocation, that indicate where they jump to.
7978 This saves some pipeline bubbles. */
7979 r_type = ELF_R_TYPE (abfd, irel->r_info);
7980 if (r_type != R_MIPS_JALR)
7981 continue;
7982
7983 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7984 /* Compute the address of the jump target. */
7985 if (r_symndx >= extsymoff)
7986 {
7987 struct mips_elf_link_hash_entry *h
7988 = ((struct mips_elf_link_hash_entry *)
7989 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7990
7991 while (h->root.root.type == bfd_link_hash_indirect
7992 || h->root.root.type == bfd_link_hash_warning)
7993 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 7994
d0647110
AO
7995 /* If a symbol is undefined, or if it may be overridden,
7996 skip it. */
7997 if (! ((h->root.root.type == bfd_link_hash_defined
7998 || h->root.root.type == bfd_link_hash_defweak)
7999 && h->root.root.u.def.section)
8000 || (link_info->shared && ! link_info->symbolic
f5385ebf 8001 && !h->root.forced_local))
d0647110
AO
8002 continue;
8003
8004 sym_sec = h->root.root.u.def.section;
8005 if (sym_sec->output_section)
8006 symval = (h->root.root.u.def.value
8007 + sym_sec->output_section->vma
8008 + sym_sec->output_offset);
8009 else
8010 symval = h->root.root.u.def.value;
8011 }
8012 else
8013 {
8014 Elf_Internal_Sym *isym;
8015
8016 /* Read this BFD's symbols if we haven't done so already. */
8017 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8018 {
8019 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8020 if (isymbuf == NULL)
8021 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8022 symtab_hdr->sh_info, 0,
8023 NULL, NULL, NULL);
8024 if (isymbuf == NULL)
8025 goto relax_return;
8026 }
8027
8028 isym = isymbuf + r_symndx;
8029 if (isym->st_shndx == SHN_UNDEF)
8030 continue;
8031 else if (isym->st_shndx == SHN_ABS)
8032 sym_sec = bfd_abs_section_ptr;
8033 else if (isym->st_shndx == SHN_COMMON)
8034 sym_sec = bfd_com_section_ptr;
8035 else
8036 sym_sec
8037 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8038 symval = isym->st_value
8039 + sym_sec->output_section->vma
8040 + sym_sec->output_offset;
8041 }
8042
8043 /* Compute branch offset, from delay slot of the jump to the
8044 branch target. */
8045 sym_offset = (symval + irel->r_addend)
8046 - (sec_start + irel->r_offset + 4);
8047
8048 /* Branch offset must be properly aligned. */
8049 if ((sym_offset & 3) != 0)
8050 continue;
8051
8052 sym_offset >>= 2;
8053
8054 /* Check that it's in range. */
8055 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8056 continue;
143d77c5 8057
d0647110 8058 /* Get the section contents if we haven't done so already. */
c224138d
RS
8059 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8060 goto relax_return;
d0647110
AO
8061
8062 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8063
8064 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8065 if ((instruction & 0xfc1fffff) == 0x0000f809)
8066 instruction = 0x04110000;
8067 /* If it was jr <reg>, turn it into b <target>. */
8068 else if ((instruction & 0xfc1fffff) == 0x00000008)
8069 instruction = 0x10000000;
8070 else
8071 continue;
8072
8073 instruction |= (sym_offset & 0xffff);
8074 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8075 changed_contents = TRUE;
8076 }
8077
8078 if (contents != NULL
8079 && elf_section_data (sec)->this_hdr.contents != contents)
8080 {
8081 if (!changed_contents && !link_info->keep_memory)
8082 free (contents);
8083 else
8084 {
8085 /* Cache the section contents for elf_link_input_bfd. */
8086 elf_section_data (sec)->this_hdr.contents = contents;
8087 }
8088 }
8089 return TRUE;
8090
143d77c5 8091 relax_return:
eea6121a
AM
8092 if (contents != NULL
8093 && elf_section_data (sec)->this_hdr.contents != contents)
8094 free (contents);
d0647110
AO
8095 return FALSE;
8096}
8097\f
9a59ad6b
DJ
8098/* Allocate space for global sym dynamic relocs. */
8099
8100static bfd_boolean
8101allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8102{
8103 struct bfd_link_info *info = inf;
8104 bfd *dynobj;
8105 struct mips_elf_link_hash_entry *hmips;
8106 struct mips_elf_link_hash_table *htab;
8107
8108 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8109 BFD_ASSERT (htab != NULL);
8110
9a59ad6b
DJ
8111 dynobj = elf_hash_table (info)->dynobj;
8112 hmips = (struct mips_elf_link_hash_entry *) h;
8113
8114 /* VxWorks executables are handled elsewhere; we only need to
8115 allocate relocations in shared objects. */
8116 if (htab->is_vxworks && !info->shared)
8117 return TRUE;
8118
63897e2c
RS
8119 /* Ignore indirect and warning symbols. All relocations against
8120 such symbols will be redirected to the target symbol. */
8121 if (h->root.type == bfd_link_hash_indirect
8122 || h->root.type == bfd_link_hash_warning)
8123 return TRUE;
8124
9a59ad6b
DJ
8125 /* If this symbol is defined in a dynamic object, or we are creating
8126 a shared library, we will need to copy any R_MIPS_32 or
8127 R_MIPS_REL32 relocs against it into the output file. */
8128 if (! info->relocatable
8129 && hmips->possibly_dynamic_relocs != 0
8130 && (h->root.type == bfd_link_hash_defweak
8131 || !h->def_regular
8132 || info->shared))
8133 {
8134 bfd_boolean do_copy = TRUE;
8135
8136 if (h->root.type == bfd_link_hash_undefweak)
8137 {
8138 /* Do not copy relocations for undefined weak symbols with
8139 non-default visibility. */
8140 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8141 do_copy = FALSE;
8142
8143 /* Make sure undefined weak symbols are output as a dynamic
8144 symbol in PIEs. */
8145 else if (h->dynindx == -1 && !h->forced_local)
8146 {
8147 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8148 return FALSE;
8149 }
8150 }
8151
8152 if (do_copy)
8153 {
aff469fa 8154 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
8155 the SVR4 psABI requires it to have a dynamic symbol table
8156 index greater that DT_MIPS_GOTSYM if there are dynamic
8157 relocations against it.
8158
8159 VxWorks does not enforce the same mapping between the GOT
8160 and the symbol table, so the same requirement does not
8161 apply there. */
6ccf4795
RS
8162 if (!htab->is_vxworks)
8163 {
8164 if (hmips->global_got_area > GGA_RELOC_ONLY)
8165 hmips->global_got_area = GGA_RELOC_ONLY;
8166 hmips->got_only_for_calls = FALSE;
8167 }
aff469fa 8168
9a59ad6b
DJ
8169 mips_elf_allocate_dynamic_relocations
8170 (dynobj, info, hmips->possibly_dynamic_relocs);
8171 if (hmips->readonly_reloc)
8172 /* We tell the dynamic linker that there are relocations
8173 against the text segment. */
8174 info->flags |= DF_TEXTREL;
8175 }
8176 }
8177
8178 return TRUE;
8179}
8180
b49e97c9
TS
8181/* Adjust a symbol defined by a dynamic object and referenced by a
8182 regular object. The current definition is in some section of the
8183 dynamic object, but we're not including those sections. We have to
8184 change the definition to something the rest of the link can
8185 understand. */
8186
b34976b6 8187bfd_boolean
9719ad41
RS
8188_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8189 struct elf_link_hash_entry *h)
b49e97c9
TS
8190{
8191 bfd *dynobj;
8192 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8193 struct mips_elf_link_hash_table *htab;
b49e97c9 8194
5108fc1b 8195 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8196 BFD_ASSERT (htab != NULL);
8197
b49e97c9 8198 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8199 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8200
8201 /* Make sure we know what is going on here. */
8202 BFD_ASSERT (dynobj != NULL
f5385ebf 8203 && (h->needs_plt
f6e332e6 8204 || h->u.weakdef != NULL
f5385ebf
AM
8205 || (h->def_dynamic
8206 && h->ref_regular
8207 && !h->def_regular)));
b49e97c9 8208
b49e97c9 8209 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8210
861fb55a
DJ
8211 /* If there are call relocations against an externally-defined symbol,
8212 see whether we can create a MIPS lazy-binding stub for it. We can
8213 only do this if all references to the function are through call
8214 relocations, and in that case, the traditional lazy-binding stubs
8215 are much more efficient than PLT entries.
8216
8217 Traditional stubs are only available on SVR4 psABI-based systems;
8218 VxWorks always uses PLTs instead. */
8219 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8220 {
8221 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8222 return TRUE;
b49e97c9
TS
8223
8224 /* If this symbol is not defined in a regular file, then set
8225 the symbol to the stub location. This is required to make
8226 function pointers compare as equal between the normal
8227 executable and the shared library. */
f5385ebf 8228 if (!h->def_regular)
b49e97c9 8229 {
33bb52fb
RS
8230 hmips->needs_lazy_stub = TRUE;
8231 htab->lazy_stub_count++;
b34976b6 8232 return TRUE;
b49e97c9
TS
8233 }
8234 }
861fb55a
DJ
8235 /* As above, VxWorks requires PLT entries for externally-defined
8236 functions that are only accessed through call relocations.
b49e97c9 8237
861fb55a
DJ
8238 Both VxWorks and non-VxWorks targets also need PLT entries if there
8239 are static-only relocations against an externally-defined function.
8240 This can technically occur for shared libraries if there are
8241 branches to the symbol, although it is unlikely that this will be
8242 used in practice due to the short ranges involved. It can occur
8243 for any relative or absolute relocation in executables; in that
8244 case, the PLT entry becomes the function's canonical address. */
8245 else if (((h->needs_plt && !hmips->no_fn_stub)
8246 || (h->type == STT_FUNC && hmips->has_static_relocs))
8247 && htab->use_plts_and_copy_relocs
8248 && !SYMBOL_CALLS_LOCAL (info, h)
8249 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8250 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8251 {
861fb55a
DJ
8252 /* If this is the first symbol to need a PLT entry, allocate room
8253 for the header. */
8254 if (htab->splt->size == 0)
8255 {
8256 BFD_ASSERT (htab->sgotplt->size == 0);
0a44bf69 8257
861fb55a
DJ
8258 /* If we're using the PLT additions to the psABI, each PLT
8259 entry is 16 bytes and the PLT0 entry is 32 bytes.
8260 Encourage better cache usage by aligning. We do this
8261 lazily to avoid pessimizing traditional objects. */
8262 if (!htab->is_vxworks
8263 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8264 return FALSE;
0a44bf69 8265
861fb55a
DJ
8266 /* Make sure that .got.plt is word-aligned. We do this lazily
8267 for the same reason as above. */
8268 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8269 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8270 return FALSE;
0a44bf69 8271
861fb55a 8272 htab->splt->size += htab->plt_header_size;
0a44bf69 8273
861fb55a
DJ
8274 /* On non-VxWorks targets, the first two entries in .got.plt
8275 are reserved. */
8276 if (!htab->is_vxworks)
8277 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
0a44bf69 8278
861fb55a
DJ
8279 /* On VxWorks, also allocate room for the header's
8280 .rela.plt.unloaded entries. */
8281 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8282 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8283 }
8284
8285 /* Assign the next .plt entry to this symbol. */
8286 h->plt.offset = htab->splt->size;
8287 htab->splt->size += htab->plt_entry_size;
8288
8289 /* If the output file has no definition of the symbol, set the
861fb55a 8290 symbol's value to the address of the stub. */
131eb6b7 8291 if (!info->shared && !h->def_regular)
0a44bf69
RS
8292 {
8293 h->root.u.def.section = htab->splt;
8294 h->root.u.def.value = h->plt.offset;
861fb55a
DJ
8295 /* For VxWorks, point at the PLT load stub rather than the
8296 lazy resolution stub; this stub will become the canonical
8297 function address. */
8298 if (htab->is_vxworks)
8299 h->root.u.def.value += 8;
0a44bf69
RS
8300 }
8301
861fb55a
DJ
8302 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8303 relocation. */
8304 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8305 htab->srelplt->size += (htab->is_vxworks
8306 ? MIPS_ELF_RELA_SIZE (dynobj)
8307 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8308
8309 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8310 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8311 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8312
861fb55a
DJ
8313 /* All relocations against this symbol that could have been made
8314 dynamic will now refer to the PLT entry instead. */
8315 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8316
0a44bf69
RS
8317 return TRUE;
8318 }
8319
8320 /* If this is a weak symbol, and there is a real definition, the
8321 processor independent code will have arranged for us to see the
8322 real definition first, and we can just use the same value. */
8323 if (h->u.weakdef != NULL)
8324 {
8325 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8326 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8327 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8328 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8329 return TRUE;
8330 }
8331
861fb55a
DJ
8332 /* Otherwise, there is nothing further to do for symbols defined
8333 in regular objects. */
8334 if (h->def_regular)
0a44bf69
RS
8335 return TRUE;
8336
861fb55a
DJ
8337 /* There's also nothing more to do if we'll convert all relocations
8338 against this symbol into dynamic relocations. */
8339 if (!hmips->has_static_relocs)
8340 return TRUE;
8341
8342 /* We're now relying on copy relocations. Complain if we have
8343 some that we can't convert. */
8344 if (!htab->use_plts_and_copy_relocs || info->shared)
8345 {
8346 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8347 "dynamic symbol %s"),
8348 h->root.root.string);
8349 bfd_set_error (bfd_error_bad_value);
8350 return FALSE;
8351 }
8352
0a44bf69
RS
8353 /* We must allocate the symbol in our .dynbss section, which will
8354 become part of the .bss section of the executable. There will be
8355 an entry for this symbol in the .dynsym section. The dynamic
8356 object will contain position independent code, so all references
8357 from the dynamic object to this symbol will go through the global
8358 offset table. The dynamic linker will use the .dynsym entry to
8359 determine the address it must put in the global offset table, so
8360 both the dynamic object and the regular object will refer to the
8361 same memory location for the variable. */
8362
8363 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8364 {
861fb55a
DJ
8365 if (htab->is_vxworks)
8366 htab->srelbss->size += sizeof (Elf32_External_Rela);
8367 else
8368 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
8369 h->needs_copy = 1;
8370 }
8371
861fb55a
DJ
8372 /* All relocations against this symbol that could have been made
8373 dynamic will now refer to the local copy instead. */
8374 hmips->possibly_dynamic_relocs = 0;
8375
027297b7 8376 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 8377}
b49e97c9
TS
8378\f
8379/* This function is called after all the input files have been read,
8380 and the input sections have been assigned to output sections. We
8381 check for any mips16 stub sections that we can discard. */
8382
b34976b6 8383bfd_boolean
9719ad41
RS
8384_bfd_mips_elf_always_size_sections (bfd *output_bfd,
8385 struct bfd_link_info *info)
b49e97c9
TS
8386{
8387 asection *ri;
0a44bf69 8388 struct mips_elf_link_hash_table *htab;
861fb55a 8389 struct mips_htab_traverse_info hti;
0a44bf69
RS
8390
8391 htab = mips_elf_hash_table (info);
4dfe6ac6 8392 BFD_ASSERT (htab != NULL);
f4416af6 8393
b49e97c9
TS
8394 /* The .reginfo section has a fixed size. */
8395 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8396 if (ri != NULL)
9719ad41 8397 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 8398
861fb55a
DJ
8399 hti.info = info;
8400 hti.output_bfd = output_bfd;
8401 hti.error = FALSE;
8402 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8403 mips_elf_check_symbols, &hti);
8404 if (hti.error)
8405 return FALSE;
f4416af6 8406
33bb52fb
RS
8407 return TRUE;
8408}
8409
8410/* If the link uses a GOT, lay it out and work out its size. */
8411
8412static bfd_boolean
8413mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8414{
8415 bfd *dynobj;
8416 asection *s;
8417 struct mips_got_info *g;
33bb52fb
RS
8418 bfd_size_type loadable_size = 0;
8419 bfd_size_type page_gotno;
8420 bfd *sub;
8421 struct mips_elf_count_tls_arg count_tls_arg;
8422 struct mips_elf_link_hash_table *htab;
8423
8424 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8425 BFD_ASSERT (htab != NULL);
8426
a8028dd0 8427 s = htab->sgot;
f4416af6 8428 if (s == NULL)
b34976b6 8429 return TRUE;
b49e97c9 8430
33bb52fb 8431 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
8432 g = htab->got_info;
8433
861fb55a
DJ
8434 /* Allocate room for the reserved entries. VxWorks always reserves
8435 3 entries; other objects only reserve 2 entries. */
8436 BFD_ASSERT (g->assigned_gotno == 0);
8437 if (htab->is_vxworks)
8438 htab->reserved_gotno = 3;
8439 else
8440 htab->reserved_gotno = 2;
8441 g->local_gotno += htab->reserved_gotno;
8442 g->assigned_gotno = htab->reserved_gotno;
8443
33bb52fb
RS
8444 /* Replace entries for indirect and warning symbols with entries for
8445 the target symbol. */
8446 if (!mips_elf_resolve_final_got_entries (g))
8447 return FALSE;
f4416af6 8448
d4596a51 8449 /* Count the number of GOT symbols. */
020d7251 8450 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 8451
33bb52fb
RS
8452 /* Calculate the total loadable size of the output. That
8453 will give us the maximum number of GOT_PAGE entries
8454 required. */
8455 for (sub = info->input_bfds; sub; sub = sub->link_next)
8456 {
8457 asection *subsection;
5108fc1b 8458
33bb52fb
RS
8459 for (subsection = sub->sections;
8460 subsection;
8461 subsection = subsection->next)
8462 {
8463 if ((subsection->flags & SEC_ALLOC) == 0)
8464 continue;
8465 loadable_size += ((subsection->size + 0xf)
8466 &~ (bfd_size_type) 0xf);
8467 }
8468 }
f4416af6 8469
0a44bf69 8470 if (htab->is_vxworks)
738e5348 8471 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
8472 relocations against local symbols evaluate to "G", and the EABI does
8473 not include R_MIPS_GOT_PAGE. */
c224138d 8474 page_gotno = 0;
0a44bf69
RS
8475 else
8476 /* Assume there are two loadable segments consisting of contiguous
8477 sections. Is 5 enough? */
c224138d
RS
8478 page_gotno = (loadable_size >> 16) + 5;
8479
8480 /* Choose the smaller of the two estimates; both are intended to be
8481 conservative. */
8482 if (page_gotno > g->page_gotno)
8483 page_gotno = g->page_gotno;
f4416af6 8484
c224138d 8485 g->local_gotno += page_gotno;
eea6121a 8486 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
d4596a51 8487 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6 8488
0f20cc35
DJ
8489 /* We need to calculate tls_gotno for global symbols at this point
8490 instead of building it up earlier, to avoid doublecounting
8491 entries for one global symbol from multiple input files. */
8492 count_tls_arg.info = info;
8493 count_tls_arg.needed = 0;
8494 elf_link_hash_traverse (elf_hash_table (info),
8495 mips_elf_count_global_tls_entries,
8496 &count_tls_arg);
8497 g->tls_gotno += count_tls_arg.needed;
8498 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8499
0a44bf69
RS
8500 /* VxWorks does not support multiple GOTs. It initializes $gp to
8501 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8502 dynamic loader. */
33bb52fb
RS
8503 if (htab->is_vxworks)
8504 {
8505 /* VxWorks executables do not need a GOT. */
8506 if (info->shared)
8507 {
8508 /* Each VxWorks GOT entry needs an explicit relocation. */
8509 unsigned int count;
8510
861fb55a 8511 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
33bb52fb
RS
8512 if (count)
8513 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8514 }
8515 }
8516 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 8517 {
a8028dd0 8518 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
8519 return FALSE;
8520 }
8521 else
8522 {
33bb52fb
RS
8523 struct mips_elf_count_tls_arg arg;
8524
8525 /* Set up TLS entries. */
0f20cc35
DJ
8526 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8527 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
33bb52fb
RS
8528
8529 /* Allocate room for the TLS relocations. */
8530 arg.info = info;
8531 arg.needed = 0;
8532 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8533 elf_link_hash_traverse (elf_hash_table (info),
8534 mips_elf_count_global_tls_relocs,
8535 &arg);
8536 if (arg.needed)
8537 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
0f20cc35 8538 }
b49e97c9 8539
b34976b6 8540 return TRUE;
b49e97c9
TS
8541}
8542
33bb52fb
RS
8543/* Estimate the size of the .MIPS.stubs section. */
8544
8545static void
8546mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8547{
8548 struct mips_elf_link_hash_table *htab;
8549 bfd_size_type dynsymcount;
8550
8551 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8552 BFD_ASSERT (htab != NULL);
8553
33bb52fb
RS
8554 if (htab->lazy_stub_count == 0)
8555 return;
8556
8557 /* IRIX rld assumes that a function stub isn't at the end of the .text
8558 section, so add a dummy entry to the end. */
8559 htab->lazy_stub_count++;
8560
8561 /* Get a worst-case estimate of the number of dynamic symbols needed.
8562 At this point, dynsymcount does not account for section symbols
8563 and count_section_dynsyms may overestimate the number that will
8564 be needed. */
8565 dynsymcount = (elf_hash_table (info)->dynsymcount
8566 + count_section_dynsyms (output_bfd, info));
8567
8568 /* Determine the size of one stub entry. */
8569 htab->function_stub_size = (dynsymcount > 0x10000
8570 ? MIPS_FUNCTION_STUB_BIG_SIZE
8571 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8572
8573 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8574}
8575
8576/* A mips_elf_link_hash_traverse callback for which DATA points to the
8577 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8578 allocate an entry in the stubs section. */
8579
8580static bfd_boolean
8581mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8582{
8583 struct mips_elf_link_hash_table *htab;
8584
8585 htab = (struct mips_elf_link_hash_table *) data;
8586 if (h->needs_lazy_stub)
8587 {
8588 h->root.root.u.def.section = htab->sstubs;
8589 h->root.root.u.def.value = htab->sstubs->size;
8590 h->root.plt.offset = htab->sstubs->size;
8591 htab->sstubs->size += htab->function_stub_size;
8592 }
8593 return TRUE;
8594}
8595
8596/* Allocate offsets in the stubs section to each symbol that needs one.
8597 Set the final size of the .MIPS.stub section. */
8598
8599static void
8600mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8601{
8602 struct mips_elf_link_hash_table *htab;
8603
8604 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8605 BFD_ASSERT (htab != NULL);
8606
33bb52fb
RS
8607 if (htab->lazy_stub_count == 0)
8608 return;
8609
8610 htab->sstubs->size = 0;
4dfe6ac6 8611 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
33bb52fb
RS
8612 htab->sstubs->size += htab->function_stub_size;
8613 BFD_ASSERT (htab->sstubs->size
8614 == htab->lazy_stub_count * htab->function_stub_size);
8615}
8616
b49e97c9
TS
8617/* Set the sizes of the dynamic sections. */
8618
b34976b6 8619bfd_boolean
9719ad41
RS
8620_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8621 struct bfd_link_info *info)
b49e97c9
TS
8622{
8623 bfd *dynobj;
861fb55a 8624 asection *s, *sreldyn;
b34976b6 8625 bfd_boolean reltext;
0a44bf69 8626 struct mips_elf_link_hash_table *htab;
b49e97c9 8627
0a44bf69 8628 htab = mips_elf_hash_table (info);
4dfe6ac6 8629 BFD_ASSERT (htab != NULL);
b49e97c9
TS
8630 dynobj = elf_hash_table (info)->dynobj;
8631 BFD_ASSERT (dynobj != NULL);
8632
8633 if (elf_hash_table (info)->dynamic_sections_created)
8634 {
8635 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 8636 if (info->executable)
b49e97c9
TS
8637 {
8638 s = bfd_get_section_by_name (dynobj, ".interp");
8639 BFD_ASSERT (s != NULL);
eea6121a 8640 s->size
b49e97c9
TS
8641 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8642 s->contents
8643 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8644 }
861fb55a
DJ
8645
8646 /* Create a symbol for the PLT, if we know that we are using it. */
8647 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8648 {
8649 struct elf_link_hash_entry *h;
8650
8651 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8652
8653 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8654 "_PROCEDURE_LINKAGE_TABLE_");
8655 htab->root.hplt = h;
8656 if (h == NULL)
8657 return FALSE;
8658 h->type = STT_FUNC;
8659 }
8660 }
4e41d0d7 8661
9a59ad6b
DJ
8662 /* Allocate space for global sym dynamic relocs. */
8663 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8664
33bb52fb
RS
8665 mips_elf_estimate_stub_size (output_bfd, info);
8666
8667 if (!mips_elf_lay_out_got (output_bfd, info))
8668 return FALSE;
8669
8670 mips_elf_lay_out_lazy_stubs (info);
8671
b49e97c9
TS
8672 /* The check_relocs and adjust_dynamic_symbol entry points have
8673 determined the sizes of the various dynamic sections. Allocate
8674 memory for them. */
b34976b6 8675 reltext = FALSE;
b49e97c9
TS
8676 for (s = dynobj->sections; s != NULL; s = s->next)
8677 {
8678 const char *name;
b49e97c9
TS
8679
8680 /* It's OK to base decisions on the section name, because none
8681 of the dynobj section names depend upon the input files. */
8682 name = bfd_get_section_name (dynobj, s);
8683
8684 if ((s->flags & SEC_LINKER_CREATED) == 0)
8685 continue;
8686
0112cd26 8687 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 8688 {
c456f082 8689 if (s->size != 0)
b49e97c9
TS
8690 {
8691 const char *outname;
8692 asection *target;
8693
8694 /* If this relocation section applies to a read only
8695 section, then we probably need a DT_TEXTREL entry.
0a44bf69 8696 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
8697 assert a DT_TEXTREL entry rather than testing whether
8698 there exists a relocation to a read only section or
8699 not. */
8700 outname = bfd_get_section_name (output_bfd,
8701 s->output_section);
8702 target = bfd_get_section_by_name (output_bfd, outname + 4);
8703 if ((target != NULL
8704 && (target->flags & SEC_READONLY) != 0
8705 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 8706 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 8707 reltext = TRUE;
b49e97c9
TS
8708
8709 /* We use the reloc_count field as a counter if we need
8710 to copy relocs into the output file. */
0a44bf69 8711 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 8712 s->reloc_count = 0;
f4416af6
AO
8713
8714 /* If combreloc is enabled, elf_link_sort_relocs() will
8715 sort relocations, but in a different way than we do,
8716 and before we're done creating relocations. Also, it
8717 will move them around between input sections'
8718 relocation's contents, so our sorting would be
8719 broken, so don't let it run. */
8720 info->combreloc = 0;
b49e97c9
TS
8721 }
8722 }
b49e97c9
TS
8723 else if (! info->shared
8724 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 8725 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 8726 {
5108fc1b 8727 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 8728 rtld to contain a pointer to the _r_debug structure. */
eea6121a 8729 s->size += 4;
b49e97c9
TS
8730 }
8731 else if (SGI_COMPAT (output_bfd)
0112cd26 8732 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 8733 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
8734 else if (s == htab->splt)
8735 {
8736 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
8737 room for an extra nop to fill the delay slot. This is
8738 for CPUs without load interlocking. */
8739 if (! LOAD_INTERLOCKS_P (output_bfd)
8740 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
8741 s->size += 4;
8742 }
0112cd26 8743 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 8744 && s != htab->sgot
0a44bf69 8745 && s != htab->sgotplt
861fb55a
DJ
8746 && s != htab->sstubs
8747 && s != htab->sdynbss)
b49e97c9
TS
8748 {
8749 /* It's not one of our sections, so don't allocate space. */
8750 continue;
8751 }
8752
c456f082 8753 if (s->size == 0)
b49e97c9 8754 {
8423293d 8755 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
8756 continue;
8757 }
8758
c456f082
AM
8759 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8760 continue;
8761
b49e97c9 8762 /* Allocate memory for the section contents. */
eea6121a 8763 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 8764 if (s->contents == NULL)
b49e97c9
TS
8765 {
8766 bfd_set_error (bfd_error_no_memory);
b34976b6 8767 return FALSE;
b49e97c9
TS
8768 }
8769 }
8770
8771 if (elf_hash_table (info)->dynamic_sections_created)
8772 {
8773 /* Add some entries to the .dynamic section. We fill in the
8774 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8775 must add the entries now so that we get the correct size for
5750dcec 8776 the .dynamic section. */
af5978fb
RS
8777
8778 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec
DJ
8779 DT_MIPS_RLD_MAP entry. This must come first because glibc
8780 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8781 looks at the first one it sees. */
af5978fb
RS
8782 if (!info->shared
8783 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8784 return FALSE;
b49e97c9 8785
5750dcec
DJ
8786 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8787 used by the debugger. */
8788 if (info->executable
8789 && !SGI_COMPAT (output_bfd)
8790 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8791 return FALSE;
8792
0a44bf69 8793 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
8794 info->flags |= DF_TEXTREL;
8795
8796 if ((info->flags & DF_TEXTREL) != 0)
8797 {
8798 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 8799 return FALSE;
943284cc
DJ
8800
8801 /* Clear the DF_TEXTREL flag. It will be set again if we
8802 write out an actual text relocation; we may not, because
8803 at this point we do not know whether e.g. any .eh_frame
8804 absolute relocations have been converted to PC-relative. */
8805 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
8806 }
8807
8808 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 8809 return FALSE;
b49e97c9 8810
861fb55a 8811 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 8812 if (htab->is_vxworks)
b49e97c9 8813 {
0a44bf69
RS
8814 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8815 use any of the DT_MIPS_* tags. */
861fb55a 8816 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8817 {
8818 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8819 return FALSE;
b49e97c9 8820
0a44bf69
RS
8821 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8822 return FALSE;
b49e97c9 8823
0a44bf69
RS
8824 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8825 return FALSE;
8826 }
b49e97c9 8827 }
0a44bf69
RS
8828 else
8829 {
861fb55a 8830 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8831 {
8832 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8833 return FALSE;
b49e97c9 8834
0a44bf69
RS
8835 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8836 return FALSE;
b49e97c9 8837
0a44bf69
RS
8838 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8839 return FALSE;
8840 }
b49e97c9 8841
0a44bf69
RS
8842 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8843 return FALSE;
b49e97c9 8844
0a44bf69
RS
8845 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8846 return FALSE;
b49e97c9 8847
0a44bf69
RS
8848 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8849 return FALSE;
b49e97c9 8850
0a44bf69
RS
8851 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8852 return FALSE;
b49e97c9 8853
0a44bf69
RS
8854 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8855 return FALSE;
b49e97c9 8856
0a44bf69
RS
8857 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8858 return FALSE;
b49e97c9 8859
0a44bf69
RS
8860 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8861 return FALSE;
8862
8863 if (IRIX_COMPAT (dynobj) == ict_irix5
8864 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8865 return FALSE;
8866
8867 if (IRIX_COMPAT (dynobj) == ict_irix6
8868 && (bfd_get_section_by_name
8869 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8870 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8871 return FALSE;
8872 }
861fb55a
DJ
8873 if (htab->splt->size > 0)
8874 {
8875 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8876 return FALSE;
8877
8878 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8879 return FALSE;
8880
8881 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8882 return FALSE;
8883
8884 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8885 return FALSE;
8886 }
7a2b07ff
NS
8887 if (htab->is_vxworks
8888 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8889 return FALSE;
b49e97c9
TS
8890 }
8891
b34976b6 8892 return TRUE;
b49e97c9
TS
8893}
8894\f
81d43bff
RS
8895/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8896 Adjust its R_ADDEND field so that it is correct for the output file.
8897 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8898 and sections respectively; both use symbol indexes. */
8899
8900static void
8901mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8902 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8903 asection **local_sections, Elf_Internal_Rela *rel)
8904{
8905 unsigned int r_type, r_symndx;
8906 Elf_Internal_Sym *sym;
8907 asection *sec;
8908
020d7251 8909 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
8910 {
8911 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8912 if (r_type == R_MIPS16_GPREL
8913 || r_type == R_MIPS_GPREL16
8914 || r_type == R_MIPS_GPREL32
8915 || r_type == R_MIPS_LITERAL)
8916 {
8917 rel->r_addend += _bfd_get_gp_value (input_bfd);
8918 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8919 }
8920
8921 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8922 sym = local_syms + r_symndx;
8923
8924 /* Adjust REL's addend to account for section merging. */
8925 if (!info->relocatable)
8926 {
8927 sec = local_sections[r_symndx];
8928 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8929 }
8930
8931 /* This would normally be done by the rela_normal code in elflink.c. */
8932 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8933 rel->r_addend += local_sections[r_symndx]->output_offset;
8934 }
8935}
8936
b49e97c9
TS
8937/* Relocate a MIPS ELF section. */
8938
b34976b6 8939bfd_boolean
9719ad41
RS
8940_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8941 bfd *input_bfd, asection *input_section,
8942 bfd_byte *contents, Elf_Internal_Rela *relocs,
8943 Elf_Internal_Sym *local_syms,
8944 asection **local_sections)
b49e97c9
TS
8945{
8946 Elf_Internal_Rela *rel;
8947 const Elf_Internal_Rela *relend;
8948 bfd_vma addend = 0;
b34976b6 8949 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 8950 const struct elf_backend_data *bed;
b49e97c9
TS
8951
8952 bed = get_elf_backend_data (output_bfd);
8953 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8954 for (rel = relocs; rel < relend; ++rel)
8955 {
8956 const char *name;
c9adbffe 8957 bfd_vma value = 0;
b49e97c9 8958 reloc_howto_type *howto;
38a7df63 8959 bfd_boolean cross_mode_jump_p;
b34976b6 8960 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 8961 REL relocation. */
b34976b6 8962 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 8963 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 8964 const char *msg;
ab96bf03
AM
8965 unsigned long r_symndx;
8966 asection *sec;
749b8d9d
L
8967 Elf_Internal_Shdr *symtab_hdr;
8968 struct elf_link_hash_entry *h;
d4730f92 8969 bfd_boolean rel_reloc;
b49e97c9 8970
d4730f92
BS
8971 rel_reloc = (NEWABI_P (input_bfd)
8972 && mips_elf_rel_relocation_p (input_bfd, input_section,
8973 relocs, rel));
b49e97c9 8974 /* Find the relocation howto for this relocation. */
d4730f92 8975 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
ab96bf03
AM
8976
8977 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 8978 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 8979 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
8980 {
8981 sec = local_sections[r_symndx];
8982 h = NULL;
8983 }
ab96bf03
AM
8984 else
8985 {
ab96bf03 8986 unsigned long extsymoff;
ab96bf03 8987
ab96bf03
AM
8988 extsymoff = 0;
8989 if (!elf_bad_symtab (input_bfd))
8990 extsymoff = symtab_hdr->sh_info;
8991 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8992 while (h->root.type == bfd_link_hash_indirect
8993 || h->root.type == bfd_link_hash_warning)
8994 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8995
8996 sec = NULL;
8997 if (h->root.type == bfd_link_hash_defined
8998 || h->root.type == bfd_link_hash_defweak)
8999 sec = h->root.u.def.section;
9000 }
9001
9002 if (sec != NULL && elf_discarded_section (sec))
9003 {
9004 /* For relocs against symbols from removed linkonce sections,
9005 or sections discarded by a linker script, we just want the
9006 section contents zeroed. Avoid any special processing. */
9007 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
9008 rel->r_info = 0;
9009 rel->r_addend = 0;
9010 continue;
9011 }
9012
4a14403c 9013 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
9014 {
9015 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9016 64-bit code, but make sure all their addresses are in the
9017 lowermost or uppermost 32-bit section of the 64-bit address
9018 space. Thus, when they use an R_MIPS_64 they mean what is
9019 usually meant by R_MIPS_32, with the exception that the
9020 stored value is sign-extended to 64 bits. */
b34976b6 9021 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
9022
9023 /* On big-endian systems, we need to lie about the position
9024 of the reloc. */
9025 if (bfd_big_endian (input_bfd))
9026 rel->r_offset += 4;
9027 }
b49e97c9
TS
9028
9029 if (!use_saved_addend_p)
9030 {
b49e97c9
TS
9031 /* If these relocations were originally of the REL variety,
9032 we must pull the addend out of the field that will be
9033 relocated. Otherwise, we simply use the contents of the
c224138d
RS
9034 RELA relocation. */
9035 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9036 relocs, rel))
b49e97c9 9037 {
b34976b6 9038 rela_relocation_p = FALSE;
c224138d
RS
9039 addend = mips_elf_read_rel_addend (input_bfd, rel,
9040 howto, contents);
738e5348
RS
9041 if (hi16_reloc_p (r_type)
9042 || (got16_reloc_p (r_type)
b49e97c9 9043 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 9044 local_sections)))
b49e97c9 9045 {
c224138d
RS
9046 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9047 contents, &addend))
749b8d9d 9048 {
749b8d9d
L
9049 if (h)
9050 name = h->root.root.string;
9051 else
9052 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9053 local_syms + r_symndx,
9054 sec);
9055 (*_bfd_error_handler)
9056 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9057 input_bfd, input_section, name, howto->name,
9058 rel->r_offset);
749b8d9d 9059 }
b49e97c9 9060 }
30ac9238
RS
9061 else
9062 addend <<= howto->rightshift;
b49e97c9
TS
9063 }
9064 else
9065 addend = rel->r_addend;
81d43bff
RS
9066 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9067 local_syms, local_sections, rel);
b49e97c9
TS
9068 }
9069
1049f94e 9070 if (info->relocatable)
b49e97c9 9071 {
4a14403c 9072 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9073 && bfd_big_endian (input_bfd))
9074 rel->r_offset -= 4;
9075
81d43bff 9076 if (!rela_relocation_p && rel->r_addend)
5a659663 9077 {
81d43bff 9078 addend += rel->r_addend;
738e5348 9079 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9080 addend = mips_elf_high (addend);
9081 else if (r_type == R_MIPS_HIGHER)
9082 addend = mips_elf_higher (addend);
9083 else if (r_type == R_MIPS_HIGHEST)
9084 addend = mips_elf_highest (addend);
30ac9238
RS
9085 else
9086 addend >>= howto->rightshift;
b49e97c9 9087
30ac9238
RS
9088 /* We use the source mask, rather than the destination
9089 mask because the place to which we are writing will be
9090 source of the addend in the final link. */
b49e97c9
TS
9091 addend &= howto->src_mask;
9092
5a659663 9093 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9094 /* See the comment above about using R_MIPS_64 in the 32-bit
9095 ABI. Here, we need to update the addend. It would be
9096 possible to get away with just using the R_MIPS_32 reloc
9097 but for endianness. */
9098 {
9099 bfd_vma sign_bits;
9100 bfd_vma low_bits;
9101 bfd_vma high_bits;
9102
9103 if (addend & ((bfd_vma) 1 << 31))
9104#ifdef BFD64
9105 sign_bits = ((bfd_vma) 1 << 32) - 1;
9106#else
9107 sign_bits = -1;
9108#endif
9109 else
9110 sign_bits = 0;
9111
9112 /* If we don't know that we have a 64-bit type,
9113 do two separate stores. */
9114 if (bfd_big_endian (input_bfd))
9115 {
9116 /* Store the sign-bits (which are most significant)
9117 first. */
9118 low_bits = sign_bits;
9119 high_bits = addend;
9120 }
9121 else
9122 {
9123 low_bits = addend;
9124 high_bits = sign_bits;
9125 }
9126 bfd_put_32 (input_bfd, low_bits,
9127 contents + rel->r_offset);
9128 bfd_put_32 (input_bfd, high_bits,
9129 contents + rel->r_offset + 4);
9130 continue;
9131 }
9132
9133 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9134 input_bfd, input_section,
b34976b6
AM
9135 contents, FALSE))
9136 return FALSE;
b49e97c9
TS
9137 }
9138
9139 /* Go on to the next relocation. */
9140 continue;
9141 }
9142
9143 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9144 relocations for the same offset. In that case we are
9145 supposed to treat the output of each relocation as the addend
9146 for the next. */
9147 if (rel + 1 < relend
9148 && rel->r_offset == rel[1].r_offset
9149 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9150 use_saved_addend_p = TRUE;
b49e97c9 9151 else
b34976b6 9152 use_saved_addend_p = FALSE;
b49e97c9
TS
9153
9154 /* Figure out what value we are supposed to relocate. */
9155 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9156 input_section, info, rel,
9157 addend, howto, local_syms,
9158 local_sections, &value,
38a7df63 9159 &name, &cross_mode_jump_p,
bce03d3d 9160 use_saved_addend_p))
b49e97c9
TS
9161 {
9162 case bfd_reloc_continue:
9163 /* There's nothing to do. */
9164 continue;
9165
9166 case bfd_reloc_undefined:
9167 /* mips_elf_calculate_relocation already called the
9168 undefined_symbol callback. There's no real point in
9169 trying to perform the relocation at this point, so we
9170 just skip ahead to the next relocation. */
9171 continue;
9172
9173 case bfd_reloc_notsupported:
9174 msg = _("internal error: unsupported relocation error");
9175 info->callbacks->warning
9176 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9177 return FALSE;
b49e97c9
TS
9178
9179 case bfd_reloc_overflow:
9180 if (use_saved_addend_p)
9181 /* Ignore overflow until we reach the last relocation for
9182 a given location. */
9183 ;
9184 else
9185 {
0e53d9da
AN
9186 struct mips_elf_link_hash_table *htab;
9187
9188 htab = mips_elf_hash_table (info);
4dfe6ac6 9189 BFD_ASSERT (htab != NULL);
b49e97c9 9190 BFD_ASSERT (name != NULL);
0e53d9da 9191 if (!htab->small_data_overflow_reported
9684f078 9192 && (gprel16_reloc_p (howto->type)
0e53d9da
AN
9193 || howto->type == R_MIPS_LITERAL))
9194 {
91d6fa6a
NC
9195 msg = _("small-data section exceeds 64KB;"
9196 " lower small-data size limit (see option -G)");
0e53d9da
AN
9197
9198 htab->small_data_overflow_reported = TRUE;
9199 (*info->callbacks->einfo) ("%P: %s\n", msg);
9200 }
b49e97c9 9201 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 9202 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 9203 input_bfd, input_section, rel->r_offset)))
b34976b6 9204 return FALSE;
b49e97c9
TS
9205 }
9206 break;
9207
9208 case bfd_reloc_ok:
9209 break;
9210
9211 default:
9212 abort ();
9213 break;
9214 }
9215
9216 /* If we've got another relocation for the address, keep going
9217 until we reach the last one. */
9218 if (use_saved_addend_p)
9219 {
9220 addend = value;
9221 continue;
9222 }
9223
4a14403c 9224 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9225 /* See the comment above about using R_MIPS_64 in the 32-bit
9226 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9227 that calculated the right value. Now, however, we
9228 sign-extend the 32-bit result to 64-bits, and store it as a
9229 64-bit value. We are especially generous here in that we
9230 go to extreme lengths to support this usage on systems with
9231 only a 32-bit VMA. */
9232 {
9233 bfd_vma sign_bits;
9234 bfd_vma low_bits;
9235 bfd_vma high_bits;
9236
9237 if (value & ((bfd_vma) 1 << 31))
9238#ifdef BFD64
9239 sign_bits = ((bfd_vma) 1 << 32) - 1;
9240#else
9241 sign_bits = -1;
9242#endif
9243 else
9244 sign_bits = 0;
9245
9246 /* If we don't know that we have a 64-bit type,
9247 do two separate stores. */
9248 if (bfd_big_endian (input_bfd))
9249 {
9250 /* Undo what we did above. */
9251 rel->r_offset -= 4;
9252 /* Store the sign-bits (which are most significant)
9253 first. */
9254 low_bits = sign_bits;
9255 high_bits = value;
9256 }
9257 else
9258 {
9259 low_bits = value;
9260 high_bits = sign_bits;
9261 }
9262 bfd_put_32 (input_bfd, low_bits,
9263 contents + rel->r_offset);
9264 bfd_put_32 (input_bfd, high_bits,
9265 contents + rel->r_offset + 4);
9266 continue;
9267 }
9268
9269 /* Actually perform the relocation. */
9270 if (! mips_elf_perform_relocation (info, howto, rel, value,
9271 input_bfd, input_section,
38a7df63 9272 contents, cross_mode_jump_p))
b34976b6 9273 return FALSE;
b49e97c9
TS
9274 }
9275
b34976b6 9276 return TRUE;
b49e97c9
TS
9277}
9278\f
861fb55a
DJ
9279/* A function that iterates over each entry in la25_stubs and fills
9280 in the code for each one. DATA points to a mips_htab_traverse_info. */
9281
9282static int
9283mips_elf_create_la25_stub (void **slot, void *data)
9284{
9285 struct mips_htab_traverse_info *hti;
9286 struct mips_elf_link_hash_table *htab;
9287 struct mips_elf_la25_stub *stub;
9288 asection *s;
9289 bfd_byte *loc;
9290 bfd_vma offset, target, target_high, target_low;
9291
9292 stub = (struct mips_elf_la25_stub *) *slot;
9293 hti = (struct mips_htab_traverse_info *) data;
9294 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 9295 BFD_ASSERT (htab != NULL);
861fb55a
DJ
9296
9297 /* Create the section contents, if we haven't already. */
9298 s = stub->stub_section;
9299 loc = s->contents;
9300 if (loc == NULL)
9301 {
9302 loc = bfd_malloc (s->size);
9303 if (loc == NULL)
9304 {
9305 hti->error = TRUE;
9306 return FALSE;
9307 }
9308 s->contents = loc;
9309 }
9310
9311 /* Work out where in the section this stub should go. */
9312 offset = stub->offset;
9313
9314 /* Work out the target address. */
9315 target = (stub->h->root.root.u.def.section->output_section->vma
9316 + stub->h->root.root.u.def.section->output_offset
9317 + stub->h->root.root.u.def.value);
9318 target_high = ((target + 0x8000) >> 16) & 0xffff;
9319 target_low = (target & 0xffff);
9320
9321 if (stub->stub_section != htab->strampoline)
9322 {
9323 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9324 of the section and write the two instructions at the end. */
9325 memset (loc, 0, offset);
9326 loc += offset;
9327 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9328 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9329 }
9330 else
9331 {
9332 /* This is trampoline. */
9333 loc += offset;
9334 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9335 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9336 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9337 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9338 }
9339 return TRUE;
9340}
9341
b49e97c9
TS
9342/* If NAME is one of the special IRIX6 symbols defined by the linker,
9343 adjust it appropriately now. */
9344
9345static void
9719ad41
RS
9346mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9347 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
9348{
9349 /* The linker script takes care of providing names and values for
9350 these, but we must place them into the right sections. */
9351 static const char* const text_section_symbols[] = {
9352 "_ftext",
9353 "_etext",
9354 "__dso_displacement",
9355 "__elf_header",
9356 "__program_header_table",
9357 NULL
9358 };
9359
9360 static const char* const data_section_symbols[] = {
9361 "_fdata",
9362 "_edata",
9363 "_end",
9364 "_fbss",
9365 NULL
9366 };
9367
9368 const char* const *p;
9369 int i;
9370
9371 for (i = 0; i < 2; ++i)
9372 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9373 *p;
9374 ++p)
9375 if (strcmp (*p, name) == 0)
9376 {
9377 /* All of these symbols are given type STT_SECTION by the
9378 IRIX6 linker. */
9379 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 9380 sym->st_other = STO_PROTECTED;
b49e97c9
TS
9381
9382 /* The IRIX linker puts these symbols in special sections. */
9383 if (i == 0)
9384 sym->st_shndx = SHN_MIPS_TEXT;
9385 else
9386 sym->st_shndx = SHN_MIPS_DATA;
9387
9388 break;
9389 }
9390}
9391
9392/* Finish up dynamic symbol handling. We set the contents of various
9393 dynamic sections here. */
9394
b34976b6 9395bfd_boolean
9719ad41
RS
9396_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9397 struct bfd_link_info *info,
9398 struct elf_link_hash_entry *h,
9399 Elf_Internal_Sym *sym)
b49e97c9
TS
9400{
9401 bfd *dynobj;
b49e97c9 9402 asection *sgot;
f4416af6 9403 struct mips_got_info *g, *gg;
b49e97c9 9404 const char *name;
3d6746ca 9405 int idx;
5108fc1b 9406 struct mips_elf_link_hash_table *htab;
738e5348 9407 struct mips_elf_link_hash_entry *hmips;
b49e97c9 9408
5108fc1b 9409 htab = mips_elf_hash_table (info);
4dfe6ac6 9410 BFD_ASSERT (htab != NULL);
b49e97c9 9411 dynobj = elf_hash_table (info)->dynobj;
738e5348 9412 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9413
861fb55a
DJ
9414 BFD_ASSERT (!htab->is_vxworks);
9415
9416 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9417 {
9418 /* We've decided to create a PLT entry for this symbol. */
9419 bfd_byte *loc;
9420 bfd_vma header_address, plt_index, got_address;
9421 bfd_vma got_address_high, got_address_low, load;
9422 const bfd_vma *plt_entry;
9423
9424 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9425 BFD_ASSERT (h->dynindx != -1);
9426 BFD_ASSERT (htab->splt != NULL);
9427 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9428 BFD_ASSERT (!h->def_regular);
9429
9430 /* Calculate the address of the PLT header. */
9431 header_address = (htab->splt->output_section->vma
9432 + htab->splt->output_offset);
9433
9434 /* Calculate the index of the entry. */
9435 plt_index = ((h->plt.offset - htab->plt_header_size)
9436 / htab->plt_entry_size);
9437
9438 /* Calculate the address of the .got.plt entry. */
9439 got_address = (htab->sgotplt->output_section->vma
9440 + htab->sgotplt->output_offset
9441 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9442 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9443 got_address_low = got_address & 0xffff;
9444
9445 /* Initially point the .got.plt entry at the PLT header. */
9446 loc = (htab->sgotplt->contents
9447 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9448 if (ABI_64_P (output_bfd))
9449 bfd_put_64 (output_bfd, header_address, loc);
9450 else
9451 bfd_put_32 (output_bfd, header_address, loc);
9452
9453 /* Find out where the .plt entry should go. */
9454 loc = htab->splt->contents + h->plt.offset;
9455
9456 /* Pick the load opcode. */
9457 load = MIPS_ELF_LOAD_WORD (output_bfd);
9458
9459 /* Fill in the PLT entry itself. */
9460 plt_entry = mips_exec_plt_entry;
9461 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9462 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
6d30f5b2
NC
9463
9464 if (! LOAD_INTERLOCKS_P (output_bfd))
9465 {
9466 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9467 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9468 }
9469 else
9470 {
9471 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9472 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9473 }
861fb55a
DJ
9474
9475 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9476 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9477 plt_index, h->dynindx,
9478 R_MIPS_JUMP_SLOT, got_address);
9479
9480 /* We distinguish between PLT entries and lazy-binding stubs by
9481 giving the former an st_other value of STO_MIPS_PLT. Set the
9482 flag and leave the value if there are any relocations in the
9483 binary where pointer equality matters. */
9484 sym->st_shndx = SHN_UNDEF;
9485 if (h->pointer_equality_needed)
9486 sym->st_other = STO_MIPS_PLT;
9487 else
9488 sym->st_value = 0;
9489 }
9490 else if (h->plt.offset != MINUS_ONE)
b49e97c9 9491 {
861fb55a 9492 /* We've decided to create a lazy-binding stub. */
5108fc1b 9493 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
b49e97c9
TS
9494
9495 /* This symbol has a stub. Set it up. */
9496
9497 BFD_ASSERT (h->dynindx != -1);
9498
5108fc1b
RS
9499 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9500 || (h->dynindx <= 0xffff));
3d6746ca
DD
9501
9502 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
9503 sign extension at runtime in the stub, resulting in a negative
9504 index value. */
9505 if (h->dynindx & ~0x7fffffff)
b34976b6 9506 return FALSE;
b49e97c9
TS
9507
9508 /* Fill the stub. */
3d6746ca
DD
9509 idx = 0;
9510 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9511 idx += 4;
9512 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9513 idx += 4;
5108fc1b 9514 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
3d6746ca 9515 {
5108fc1b 9516 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
3d6746ca
DD
9517 stub + idx);
9518 idx += 4;
9519 }
9520 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9521 idx += 4;
b49e97c9 9522
3d6746ca
DD
9523 /* If a large stub is not required and sign extension is not a
9524 problem, then use legacy code in the stub. */
5108fc1b
RS
9525 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9526 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9527 else if (h->dynindx & ~0x7fff)
3d6746ca
DD
9528 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9529 else
5108fc1b
RS
9530 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9531 stub + idx);
9532
4e41d0d7
RS
9533 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9534 memcpy (htab->sstubs->contents + h->plt.offset,
9535 stub, htab->function_stub_size);
b49e97c9
TS
9536
9537 /* Mark the symbol as undefined. plt.offset != -1 occurs
9538 only for the referenced symbol. */
9539 sym->st_shndx = SHN_UNDEF;
9540
9541 /* The run-time linker uses the st_value field of the symbol
9542 to reset the global offset table entry for this external
9543 to its stub address when unlinking a shared object. */
4e41d0d7
RS
9544 sym->st_value = (htab->sstubs->output_section->vma
9545 + htab->sstubs->output_offset
c5ae1840 9546 + h->plt.offset);
b49e97c9
TS
9547 }
9548
738e5348
RS
9549 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9550 refer to the stub, since only the stub uses the standard calling
9551 conventions. */
9552 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9553 {
9554 BFD_ASSERT (hmips->need_fn_stub);
9555 sym->st_value = (hmips->fn_stub->output_section->vma
9556 + hmips->fn_stub->output_offset);
9557 sym->st_size = hmips->fn_stub->size;
9558 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9559 }
9560
b49e97c9 9561 BFD_ASSERT (h->dynindx != -1
f5385ebf 9562 || h->forced_local);
b49e97c9 9563
23cc69b6 9564 sgot = htab->sgot;
a8028dd0 9565 g = htab->got_info;
b49e97c9
TS
9566 BFD_ASSERT (g != NULL);
9567
9568 /* Run through the global symbol table, creating GOT entries for all
9569 the symbols that need them. */
020d7251 9570 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
9571 {
9572 bfd_vma offset;
9573 bfd_vma value;
9574
6eaa6adc 9575 value = sym->st_value;
738e5348
RS
9576 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9577 R_MIPS_GOT16, info);
b49e97c9
TS
9578 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9579 }
9580
020d7251 9581 if (hmips->global_got_area != GGA_NONE && g->next && h->type != STT_TLS)
f4416af6
AO
9582 {
9583 struct mips_got_entry e, *p;
0626d451 9584 bfd_vma entry;
f4416af6 9585 bfd_vma offset;
f4416af6
AO
9586
9587 gg = g;
9588
9589 e.abfd = output_bfd;
9590 e.symndx = -1;
738e5348 9591 e.d.h = hmips;
0f20cc35 9592 e.tls_type = 0;
143d77c5 9593
f4416af6
AO
9594 for (g = g->next; g->next != gg; g = g->next)
9595 {
9596 if (g->got_entries
9597 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9598 &e)))
9599 {
9600 offset = p->gotidx;
0626d451
RS
9601 if (info->shared
9602 || (elf_hash_table (info)->dynamic_sections_created
9603 && p->d.h != NULL
f5385ebf
AM
9604 && p->d.h->root.def_dynamic
9605 && !p->d.h->root.def_regular))
0626d451
RS
9606 {
9607 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9608 the various compatibility problems, it's easier to mock
9609 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9610 mips_elf_create_dynamic_relocation to calculate the
9611 appropriate addend. */
9612 Elf_Internal_Rela rel[3];
9613
9614 memset (rel, 0, sizeof (rel));
9615 if (ABI_64_P (output_bfd))
9616 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9617 else
9618 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9619 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9620
9621 entry = 0;
9622 if (! (mips_elf_create_dynamic_relocation
9623 (output_bfd, info, rel,
9624 e.d.h, NULL, sym->st_value, &entry, sgot)))
9625 return FALSE;
9626 }
9627 else
9628 entry = sym->st_value;
9629 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
9630 }
9631 }
9632 }
9633
b49e97c9
TS
9634 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9635 name = h->root.root.string;
9636 if (strcmp (name, "_DYNAMIC") == 0
22edb2f1 9637 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
9638 sym->st_shndx = SHN_ABS;
9639 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9640 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9641 {
9642 sym->st_shndx = SHN_ABS;
9643 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9644 sym->st_value = 1;
9645 }
4a14403c 9646 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9647 {
9648 sym->st_shndx = SHN_ABS;
9649 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9650 sym->st_value = elf_gp (output_bfd);
9651 }
9652 else if (SGI_COMPAT (output_bfd))
9653 {
9654 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9655 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9656 {
9657 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9658 sym->st_other = STO_PROTECTED;
9659 sym->st_value = 0;
9660 sym->st_shndx = SHN_MIPS_DATA;
9661 }
9662 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9663 {
9664 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9665 sym->st_other = STO_PROTECTED;
9666 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9667 sym->st_shndx = SHN_ABS;
9668 }
9669 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9670 {
9671 if (h->type == STT_FUNC)
9672 sym->st_shndx = SHN_MIPS_TEXT;
9673 else if (h->type == STT_OBJECT)
9674 sym->st_shndx = SHN_MIPS_DATA;
9675 }
9676 }
9677
861fb55a
DJ
9678 /* Emit a copy reloc, if needed. */
9679 if (h->needs_copy)
9680 {
9681 asection *s;
9682 bfd_vma symval;
9683
9684 BFD_ASSERT (h->dynindx != -1);
9685 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9686
9687 s = mips_elf_rel_dyn_section (info, FALSE);
9688 symval = (h->root.u.def.section->output_section->vma
9689 + h->root.u.def.section->output_offset
9690 + h->root.u.def.value);
9691 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9692 h->dynindx, R_MIPS_COPY, symval);
9693 }
9694
b49e97c9
TS
9695 /* Handle the IRIX6-specific symbols. */
9696 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9697 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9698
9699 if (! info->shared)
9700 {
9701 if (! mips_elf_hash_table (info)->use_rld_obj_head
9702 && (strcmp (name, "__rld_map") == 0
9703 || strcmp (name, "__RLD_MAP") == 0))
9704 {
9705 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9706 BFD_ASSERT (s != NULL);
9707 sym->st_value = s->output_section->vma + s->output_offset;
9719ad41 9708 bfd_put_32 (output_bfd, 0, s->contents);
b49e97c9
TS
9709 if (mips_elf_hash_table (info)->rld_value == 0)
9710 mips_elf_hash_table (info)->rld_value = sym->st_value;
9711 }
9712 else if (mips_elf_hash_table (info)->use_rld_obj_head
9713 && strcmp (name, "__rld_obj_head") == 0)
9714 {
9715 /* IRIX6 does not use a .rld_map section. */
9716 if (IRIX_COMPAT (output_bfd) == ict_irix5
9717 || IRIX_COMPAT (output_bfd) == ict_none)
9718 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9719 != NULL);
9720 mips_elf_hash_table (info)->rld_value = sym->st_value;
9721 }
9722 }
9723
738e5348
RS
9724 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9725 treat MIPS16 symbols like any other. */
30c09090 9726 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
9727 {
9728 BFD_ASSERT (sym->st_value & 1);
9729 sym->st_other -= STO_MIPS16;
9730 }
b49e97c9 9731
b34976b6 9732 return TRUE;
b49e97c9
TS
9733}
9734
0a44bf69
RS
9735/* Likewise, for VxWorks. */
9736
9737bfd_boolean
9738_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9739 struct bfd_link_info *info,
9740 struct elf_link_hash_entry *h,
9741 Elf_Internal_Sym *sym)
9742{
9743 bfd *dynobj;
9744 asection *sgot;
9745 struct mips_got_info *g;
9746 struct mips_elf_link_hash_table *htab;
020d7251 9747 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
9748
9749 htab = mips_elf_hash_table (info);
4dfe6ac6 9750 BFD_ASSERT (htab != NULL);
0a44bf69 9751 dynobj = elf_hash_table (info)->dynobj;
020d7251 9752 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69
RS
9753
9754 if (h->plt.offset != (bfd_vma) -1)
9755 {
6d79d2ed 9756 bfd_byte *loc;
0a44bf69
RS
9757 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9758 Elf_Internal_Rela rel;
9759 static const bfd_vma *plt_entry;
9760
9761 BFD_ASSERT (h->dynindx != -1);
9762 BFD_ASSERT (htab->splt != NULL);
9763 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9764
9765 /* Calculate the address of the .plt entry. */
9766 plt_address = (htab->splt->output_section->vma
9767 + htab->splt->output_offset
9768 + h->plt.offset);
9769
9770 /* Calculate the index of the entry. */
9771 plt_index = ((h->plt.offset - htab->plt_header_size)
9772 / htab->plt_entry_size);
9773
9774 /* Calculate the address of the .got.plt entry. */
9775 got_address = (htab->sgotplt->output_section->vma
9776 + htab->sgotplt->output_offset
9777 + plt_index * 4);
9778
9779 /* Calculate the offset of the .got.plt entry from
9780 _GLOBAL_OFFSET_TABLE_. */
9781 got_offset = mips_elf_gotplt_index (info, h);
9782
9783 /* Calculate the offset for the branch at the start of the PLT
9784 entry. The branch jumps to the beginning of .plt. */
9785 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9786
9787 /* Fill in the initial value of the .got.plt entry. */
9788 bfd_put_32 (output_bfd, plt_address,
9789 htab->sgotplt->contents + plt_index * 4);
9790
9791 /* Find out where the .plt entry should go. */
9792 loc = htab->splt->contents + h->plt.offset;
9793
9794 if (info->shared)
9795 {
9796 plt_entry = mips_vxworks_shared_plt_entry;
9797 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9798 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9799 }
9800 else
9801 {
9802 bfd_vma got_address_high, got_address_low;
9803
9804 plt_entry = mips_vxworks_exec_plt_entry;
9805 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9806 got_address_low = got_address & 0xffff;
9807
9808 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9809 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9810 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9811 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9812 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9813 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9814 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9815 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9816
9817 loc = (htab->srelplt2->contents
9818 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9819
9820 /* Emit a relocation for the .got.plt entry. */
9821 rel.r_offset = got_address;
9822 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9823 rel.r_addend = h->plt.offset;
9824 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9825
9826 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9827 loc += sizeof (Elf32_External_Rela);
9828 rel.r_offset = plt_address + 8;
9829 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9830 rel.r_addend = got_offset;
9831 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9832
9833 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9834 loc += sizeof (Elf32_External_Rela);
9835 rel.r_offset += 4;
9836 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9837 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9838 }
9839
9840 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9841 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9842 rel.r_offset = got_address;
9843 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9844 rel.r_addend = 0;
9845 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9846
9847 if (!h->def_regular)
9848 sym->st_shndx = SHN_UNDEF;
9849 }
9850
9851 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9852
23cc69b6 9853 sgot = htab->sgot;
a8028dd0 9854 g = htab->got_info;
0a44bf69
RS
9855 BFD_ASSERT (g != NULL);
9856
9857 /* See if this symbol has an entry in the GOT. */
020d7251 9858 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
9859 {
9860 bfd_vma offset;
9861 Elf_Internal_Rela outrel;
9862 bfd_byte *loc;
9863 asection *s;
9864
9865 /* Install the symbol value in the GOT. */
9866 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9867 R_MIPS_GOT16, info);
9868 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9869
9870 /* Add a dynamic relocation for it. */
9871 s = mips_elf_rel_dyn_section (info, FALSE);
9872 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9873 outrel.r_offset = (sgot->output_section->vma
9874 + sgot->output_offset
9875 + offset);
9876 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9877 outrel.r_addend = 0;
9878 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9879 }
9880
9881 /* Emit a copy reloc, if needed. */
9882 if (h->needs_copy)
9883 {
9884 Elf_Internal_Rela rel;
9885
9886 BFD_ASSERT (h->dynindx != -1);
9887
9888 rel.r_offset = (h->root.u.def.section->output_section->vma
9889 + h->root.u.def.section->output_offset
9890 + h->root.u.def.value);
9891 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9892 rel.r_addend = 0;
9893 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9894 htab->srelbss->contents
9895 + (htab->srelbss->reloc_count
9896 * sizeof (Elf32_External_Rela)));
9897 ++htab->srelbss->reloc_count;
9898 }
9899
9900 /* If this is a mips16 symbol, force the value to be even. */
30c09090 9901 if (ELF_ST_IS_MIPS16 (sym->st_other))
0a44bf69
RS
9902 sym->st_value &= ~1;
9903
9904 return TRUE;
9905}
9906
861fb55a
DJ
9907/* Write out a plt0 entry to the beginning of .plt. */
9908
9909static void
9910mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9911{
9912 bfd_byte *loc;
9913 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9914 static const bfd_vma *plt_entry;
9915 struct mips_elf_link_hash_table *htab;
9916
9917 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9918 BFD_ASSERT (htab != NULL);
9919
861fb55a
DJ
9920 if (ABI_64_P (output_bfd))
9921 plt_entry = mips_n64_exec_plt0_entry;
9922 else if (ABI_N32_P (output_bfd))
9923 plt_entry = mips_n32_exec_plt0_entry;
9924 else
9925 plt_entry = mips_o32_exec_plt0_entry;
9926
9927 /* Calculate the value of .got.plt. */
9928 gotplt_value = (htab->sgotplt->output_section->vma
9929 + htab->sgotplt->output_offset);
9930 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9931 gotplt_value_low = gotplt_value & 0xffff;
9932
9933 /* The PLT sequence is not safe for N64 if .got.plt's address can
9934 not be loaded in two instructions. */
9935 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9936 || ~(gotplt_value | 0x7fffffff) == 0);
9937
9938 /* Install the PLT header. */
9939 loc = htab->splt->contents;
9940 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9941 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9942 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9943 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9944 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9945 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9946 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9947 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9948}
9949
0a44bf69
RS
9950/* Install the PLT header for a VxWorks executable and finalize the
9951 contents of .rela.plt.unloaded. */
9952
9953static void
9954mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9955{
9956 Elf_Internal_Rela rela;
9957 bfd_byte *loc;
9958 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9959 static const bfd_vma *plt_entry;
9960 struct mips_elf_link_hash_table *htab;
9961
9962 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9963 BFD_ASSERT (htab != NULL);
9964
0a44bf69
RS
9965 plt_entry = mips_vxworks_exec_plt0_entry;
9966
9967 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9968 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9969 + htab->root.hgot->root.u.def.section->output_offset
9970 + htab->root.hgot->root.u.def.value);
9971
9972 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9973 got_value_low = got_value & 0xffff;
9974
9975 /* Calculate the address of the PLT header. */
9976 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9977
9978 /* Install the PLT header. */
9979 loc = htab->splt->contents;
9980 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9981 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9982 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9983 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9984 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9985 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9986
9987 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9988 loc = htab->srelplt2->contents;
9989 rela.r_offset = plt_address;
9990 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9991 rela.r_addend = 0;
9992 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9993 loc += sizeof (Elf32_External_Rela);
9994
9995 /* Output the relocation for the following addiu of
9996 %lo(_GLOBAL_OFFSET_TABLE_). */
9997 rela.r_offset += 4;
9998 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9999 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10000 loc += sizeof (Elf32_External_Rela);
10001
10002 /* Fix up the remaining relocations. They may have the wrong
10003 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10004 in which symbols were output. */
10005 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10006 {
10007 Elf_Internal_Rela rel;
10008
10009 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10010 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10011 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10012 loc += sizeof (Elf32_External_Rela);
10013
10014 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10015 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10016 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10017 loc += sizeof (Elf32_External_Rela);
10018
10019 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10020 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10021 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10022 loc += sizeof (Elf32_External_Rela);
10023 }
10024}
10025
10026/* Install the PLT header for a VxWorks shared library. */
10027
10028static void
10029mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10030{
10031 unsigned int i;
10032 struct mips_elf_link_hash_table *htab;
10033
10034 htab = mips_elf_hash_table (info);
4dfe6ac6 10035 BFD_ASSERT (htab != NULL);
0a44bf69
RS
10036
10037 /* We just need to copy the entry byte-by-byte. */
10038 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10039 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10040 htab->splt->contents + i * 4);
10041}
10042
b49e97c9
TS
10043/* Finish up the dynamic sections. */
10044
b34976b6 10045bfd_boolean
9719ad41
RS
10046_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10047 struct bfd_link_info *info)
b49e97c9
TS
10048{
10049 bfd *dynobj;
10050 asection *sdyn;
10051 asection *sgot;
f4416af6 10052 struct mips_got_info *gg, *g;
0a44bf69 10053 struct mips_elf_link_hash_table *htab;
b49e97c9 10054
0a44bf69 10055 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10056 BFD_ASSERT (htab != NULL);
10057
b49e97c9
TS
10058 dynobj = elf_hash_table (info)->dynobj;
10059
10060 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10061
23cc69b6
RS
10062 sgot = htab->sgot;
10063 gg = htab->got_info;
b49e97c9
TS
10064
10065 if (elf_hash_table (info)->dynamic_sections_created)
10066 {
10067 bfd_byte *b;
943284cc 10068 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
10069
10070 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
10071 BFD_ASSERT (gg != NULL);
10072
10073 g = mips_elf_got_for_ibfd (gg, output_bfd);
b49e97c9
TS
10074 BFD_ASSERT (g != NULL);
10075
10076 for (b = sdyn->contents;
eea6121a 10077 b < sdyn->contents + sdyn->size;
b49e97c9
TS
10078 b += MIPS_ELF_DYN_SIZE (dynobj))
10079 {
10080 Elf_Internal_Dyn dyn;
10081 const char *name;
10082 size_t elemsize;
10083 asection *s;
b34976b6 10084 bfd_boolean swap_out_p;
b49e97c9
TS
10085
10086 /* Read in the current dynamic entry. */
10087 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10088
10089 /* Assume that we're going to modify it and write it out. */
b34976b6 10090 swap_out_p = TRUE;
b49e97c9
TS
10091
10092 switch (dyn.d_tag)
10093 {
10094 case DT_RELENT:
b49e97c9
TS
10095 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10096 break;
10097
0a44bf69
RS
10098 case DT_RELAENT:
10099 BFD_ASSERT (htab->is_vxworks);
10100 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10101 break;
10102
b49e97c9
TS
10103 case DT_STRSZ:
10104 /* Rewrite DT_STRSZ. */
10105 dyn.d_un.d_val =
10106 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10107 break;
10108
10109 case DT_PLTGOT:
861fb55a
DJ
10110 s = htab->sgot;
10111 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10112 break;
10113
10114 case DT_MIPS_PLTGOT:
10115 s = htab->sgotplt;
10116 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
10117 break;
10118
10119 case DT_MIPS_RLD_VERSION:
10120 dyn.d_un.d_val = 1; /* XXX */
10121 break;
10122
10123 case DT_MIPS_FLAGS:
10124 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10125 break;
10126
b49e97c9 10127 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
10128 {
10129 time_t t;
10130 time (&t);
10131 dyn.d_un.d_val = t;
10132 }
b49e97c9
TS
10133 break;
10134
10135 case DT_MIPS_ICHECKSUM:
10136 /* XXX FIXME: */
b34976b6 10137 swap_out_p = FALSE;
b49e97c9
TS
10138 break;
10139
10140 case DT_MIPS_IVERSION:
10141 /* XXX FIXME: */
b34976b6 10142 swap_out_p = FALSE;
b49e97c9
TS
10143 break;
10144
10145 case DT_MIPS_BASE_ADDRESS:
10146 s = output_bfd->sections;
10147 BFD_ASSERT (s != NULL);
10148 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10149 break;
10150
10151 case DT_MIPS_LOCAL_GOTNO:
10152 dyn.d_un.d_val = g->local_gotno;
10153 break;
10154
10155 case DT_MIPS_UNREFEXTNO:
10156 /* The index into the dynamic symbol table which is the
10157 entry of the first external symbol that is not
10158 referenced within the same object. */
10159 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10160 break;
10161
10162 case DT_MIPS_GOTSYM:
f4416af6 10163 if (gg->global_gotsym)
b49e97c9 10164 {
f4416af6 10165 dyn.d_un.d_val = gg->global_gotsym->dynindx;
b49e97c9
TS
10166 break;
10167 }
10168 /* In case if we don't have global got symbols we default
10169 to setting DT_MIPS_GOTSYM to the same value as
10170 DT_MIPS_SYMTABNO, so we just fall through. */
10171
10172 case DT_MIPS_SYMTABNO:
10173 name = ".dynsym";
10174 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10175 s = bfd_get_section_by_name (output_bfd, name);
10176 BFD_ASSERT (s != NULL);
10177
eea6121a 10178 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
10179 break;
10180
10181 case DT_MIPS_HIPAGENO:
861fb55a 10182 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
10183 break;
10184
10185 case DT_MIPS_RLD_MAP:
10186 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10187 break;
10188
10189 case DT_MIPS_OPTIONS:
10190 s = (bfd_get_section_by_name
10191 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10192 dyn.d_un.d_ptr = s->vma;
10193 break;
10194
0a44bf69
RS
10195 case DT_RELASZ:
10196 BFD_ASSERT (htab->is_vxworks);
10197 /* The count does not include the JUMP_SLOT relocations. */
10198 if (htab->srelplt)
10199 dyn.d_un.d_val -= htab->srelplt->size;
10200 break;
10201
10202 case DT_PLTREL:
861fb55a
DJ
10203 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10204 if (htab->is_vxworks)
10205 dyn.d_un.d_val = DT_RELA;
10206 else
10207 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
10208 break;
10209
10210 case DT_PLTRELSZ:
861fb55a 10211 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
10212 dyn.d_un.d_val = htab->srelplt->size;
10213 break;
10214
10215 case DT_JMPREL:
861fb55a
DJ
10216 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10217 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
10218 + htab->srelplt->output_offset);
10219 break;
10220
943284cc
DJ
10221 case DT_TEXTREL:
10222 /* If we didn't need any text relocations after all, delete
10223 the dynamic tag. */
10224 if (!(info->flags & DF_TEXTREL))
10225 {
10226 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10227 swap_out_p = FALSE;
10228 }
10229 break;
10230
10231 case DT_FLAGS:
10232 /* If we didn't need any text relocations after all, clear
10233 DF_TEXTREL from DT_FLAGS. */
10234 if (!(info->flags & DF_TEXTREL))
10235 dyn.d_un.d_val &= ~DF_TEXTREL;
10236 else
10237 swap_out_p = FALSE;
10238 break;
10239
b49e97c9 10240 default:
b34976b6 10241 swap_out_p = FALSE;
7a2b07ff
NS
10242 if (htab->is_vxworks
10243 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10244 swap_out_p = TRUE;
b49e97c9
TS
10245 break;
10246 }
10247
943284cc 10248 if (swap_out_p || dyn_skipped)
b49e97c9 10249 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
10250 (dynobj, &dyn, b - dyn_skipped);
10251
10252 if (dyn_to_skip)
10253 {
10254 dyn_skipped += dyn_to_skip;
10255 dyn_to_skip = 0;
10256 }
b49e97c9 10257 }
943284cc
DJ
10258
10259 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10260 if (dyn_skipped > 0)
10261 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
10262 }
10263
b55fd4d4
DJ
10264 if (sgot != NULL && sgot->size > 0
10265 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 10266 {
0a44bf69
RS
10267 if (htab->is_vxworks)
10268 {
10269 /* The first entry of the global offset table points to the
10270 ".dynamic" section. The second is initialized by the
10271 loader and contains the shared library identifier.
10272 The third is also initialized by the loader and points
10273 to the lazy resolution stub. */
10274 MIPS_ELF_PUT_WORD (output_bfd,
10275 sdyn->output_offset + sdyn->output_section->vma,
10276 sgot->contents);
10277 MIPS_ELF_PUT_WORD (output_bfd, 0,
10278 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10279 MIPS_ELF_PUT_WORD (output_bfd, 0,
10280 sgot->contents
10281 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10282 }
10283 else
10284 {
10285 /* The first entry of the global offset table will be filled at
10286 runtime. The second entry will be used by some runtime loaders.
10287 This isn't the case of IRIX rld. */
10288 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 10289 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
10290 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10291 }
b49e97c9 10292
54938e2a
TS
10293 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10294 = MIPS_ELF_GOT_SIZE (output_bfd);
10295 }
b49e97c9 10296
f4416af6
AO
10297 /* Generate dynamic relocations for the non-primary gots. */
10298 if (gg != NULL && gg->next)
10299 {
10300 Elf_Internal_Rela rel[3];
10301 bfd_vma addend = 0;
10302
10303 memset (rel, 0, sizeof (rel));
10304 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10305
10306 for (g = gg->next; g->next != gg; g = g->next)
10307 {
91d6fa6a 10308 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 10309 + g->next->tls_gotno;
f4416af6 10310
9719ad41 10311 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 10312 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
10313 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10314 sgot->contents
91d6fa6a 10315 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
10316
10317 if (! info->shared)
10318 continue;
10319
91d6fa6a 10320 while (got_index < g->assigned_gotno)
f4416af6
AO
10321 {
10322 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
91d6fa6a 10323 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
10324 if (!(mips_elf_create_dynamic_relocation
10325 (output_bfd, info, rel, NULL,
10326 bfd_abs_section_ptr,
10327 0, &addend, sgot)))
10328 return FALSE;
10329 BFD_ASSERT (addend == 0);
10330 }
10331 }
10332 }
10333
3133ddbf
DJ
10334 /* The generation of dynamic relocations for the non-primary gots
10335 adds more dynamic relocations. We cannot count them until
10336 here. */
10337
10338 if (elf_hash_table (info)->dynamic_sections_created)
10339 {
10340 bfd_byte *b;
10341 bfd_boolean swap_out_p;
10342
10343 BFD_ASSERT (sdyn != NULL);
10344
10345 for (b = sdyn->contents;
10346 b < sdyn->contents + sdyn->size;
10347 b += MIPS_ELF_DYN_SIZE (dynobj))
10348 {
10349 Elf_Internal_Dyn dyn;
10350 asection *s;
10351
10352 /* Read in the current dynamic entry. */
10353 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10354
10355 /* Assume that we're going to modify it and write it out. */
10356 swap_out_p = TRUE;
10357
10358 switch (dyn.d_tag)
10359 {
10360 case DT_RELSZ:
10361 /* Reduce DT_RELSZ to account for any relocations we
10362 decided not to make. This is for the n64 irix rld,
10363 which doesn't seem to apply any relocations if there
10364 are trailing null entries. */
0a44bf69 10365 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
10366 dyn.d_un.d_val = (s->reloc_count
10367 * (ABI_64_P (output_bfd)
10368 ? sizeof (Elf64_Mips_External_Rel)
10369 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
10370 /* Adjust the section size too. Tools like the prelinker
10371 can reasonably expect the values to the same. */
10372 elf_section_data (s->output_section)->this_hdr.sh_size
10373 = dyn.d_un.d_val;
3133ddbf
DJ
10374 break;
10375
10376 default:
10377 swap_out_p = FALSE;
10378 break;
10379 }
10380
10381 if (swap_out_p)
10382 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10383 (dynobj, &dyn, b);
10384 }
10385 }
10386
b49e97c9 10387 {
b49e97c9
TS
10388 asection *s;
10389 Elf32_compact_rel cpt;
10390
b49e97c9
TS
10391 if (SGI_COMPAT (output_bfd))
10392 {
10393 /* Write .compact_rel section out. */
10394 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10395 if (s != NULL)
10396 {
10397 cpt.id1 = 1;
10398 cpt.num = s->reloc_count;
10399 cpt.id2 = 2;
10400 cpt.offset = (s->output_section->filepos
10401 + sizeof (Elf32_External_compact_rel));
10402 cpt.reserved0 = 0;
10403 cpt.reserved1 = 0;
10404 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10405 ((Elf32_External_compact_rel *)
10406 s->contents));
10407
10408 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 10409 if (htab->sstubs != NULL)
b49e97c9
TS
10410 {
10411 file_ptr dummy_offset;
10412
4e41d0d7
RS
10413 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10414 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10415 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 10416 htab->function_stub_size);
b49e97c9
TS
10417 }
10418 }
10419 }
10420
0a44bf69
RS
10421 /* The psABI says that the dynamic relocations must be sorted in
10422 increasing order of r_symndx. The VxWorks EABI doesn't require
10423 this, and because the code below handles REL rather than RELA
10424 relocations, using it for VxWorks would be outright harmful. */
10425 if (!htab->is_vxworks)
b49e97c9 10426 {
0a44bf69
RS
10427 s = mips_elf_rel_dyn_section (info, FALSE);
10428 if (s != NULL
10429 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10430 {
10431 reldyn_sorting_bfd = output_bfd;
b49e97c9 10432
0a44bf69
RS
10433 if (ABI_64_P (output_bfd))
10434 qsort ((Elf64_External_Rel *) s->contents + 1,
10435 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10436 sort_dynamic_relocs_64);
10437 else
10438 qsort ((Elf32_External_Rel *) s->contents + 1,
10439 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10440 sort_dynamic_relocs);
10441 }
b49e97c9 10442 }
b49e97c9
TS
10443 }
10444
861fb55a 10445 if (htab->splt && htab->splt->size > 0)
0a44bf69 10446 {
861fb55a
DJ
10447 if (htab->is_vxworks)
10448 {
10449 if (info->shared)
10450 mips_vxworks_finish_shared_plt (output_bfd, info);
10451 else
10452 mips_vxworks_finish_exec_plt (output_bfd, info);
10453 }
0a44bf69 10454 else
861fb55a
DJ
10455 {
10456 BFD_ASSERT (!info->shared);
10457 mips_finish_exec_plt (output_bfd, info);
10458 }
0a44bf69 10459 }
b34976b6 10460 return TRUE;
b49e97c9
TS
10461}
10462
b49e97c9 10463
64543e1a
RS
10464/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10465
10466static void
9719ad41 10467mips_set_isa_flags (bfd *abfd)
b49e97c9 10468{
64543e1a 10469 flagword val;
b49e97c9
TS
10470
10471 switch (bfd_get_mach (abfd))
10472 {
10473 default:
10474 case bfd_mach_mips3000:
10475 val = E_MIPS_ARCH_1;
10476 break;
10477
10478 case bfd_mach_mips3900:
10479 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10480 break;
10481
10482 case bfd_mach_mips6000:
10483 val = E_MIPS_ARCH_2;
10484 break;
10485
10486 case bfd_mach_mips4000:
10487 case bfd_mach_mips4300:
10488 case bfd_mach_mips4400:
10489 case bfd_mach_mips4600:
10490 val = E_MIPS_ARCH_3;
10491 break;
10492
10493 case bfd_mach_mips4010:
10494 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10495 break;
10496
10497 case bfd_mach_mips4100:
10498 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10499 break;
10500
10501 case bfd_mach_mips4111:
10502 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10503 break;
10504
00707a0e
RS
10505 case bfd_mach_mips4120:
10506 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10507 break;
10508
b49e97c9
TS
10509 case bfd_mach_mips4650:
10510 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10511 break;
10512
00707a0e
RS
10513 case bfd_mach_mips5400:
10514 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10515 break;
10516
10517 case bfd_mach_mips5500:
10518 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10519 break;
10520
0d2e43ed
ILT
10521 case bfd_mach_mips9000:
10522 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10523 break;
10524
b49e97c9 10525 case bfd_mach_mips5000:
5a7ea749 10526 case bfd_mach_mips7000:
b49e97c9
TS
10527 case bfd_mach_mips8000:
10528 case bfd_mach_mips10000:
10529 case bfd_mach_mips12000:
3aa3176b
TS
10530 case bfd_mach_mips14000:
10531 case bfd_mach_mips16000:
b49e97c9
TS
10532 val = E_MIPS_ARCH_4;
10533 break;
10534
10535 case bfd_mach_mips5:
10536 val = E_MIPS_ARCH_5;
10537 break;
10538
350cc38d
MS
10539 case bfd_mach_mips_loongson_2e:
10540 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10541 break;
10542
10543 case bfd_mach_mips_loongson_2f:
10544 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10545 break;
10546
b49e97c9
TS
10547 case bfd_mach_mips_sb1:
10548 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10549 break;
10550
6f179bd0
AN
10551 case bfd_mach_mips_octeon:
10552 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10553 break;
10554
52b6b6b9
JM
10555 case bfd_mach_mips_xlr:
10556 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10557 break;
10558
b49e97c9
TS
10559 case bfd_mach_mipsisa32:
10560 val = E_MIPS_ARCH_32;
10561 break;
10562
10563 case bfd_mach_mipsisa64:
10564 val = E_MIPS_ARCH_64;
af7ee8bf
CD
10565 break;
10566
10567 case bfd_mach_mipsisa32r2:
10568 val = E_MIPS_ARCH_32R2;
10569 break;
5f74bc13
CD
10570
10571 case bfd_mach_mipsisa64r2:
10572 val = E_MIPS_ARCH_64R2;
10573 break;
b49e97c9 10574 }
b49e97c9
TS
10575 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10576 elf_elfheader (abfd)->e_flags |= val;
10577
64543e1a
RS
10578}
10579
10580
10581/* The final processing done just before writing out a MIPS ELF object
10582 file. This gets the MIPS architecture right based on the machine
10583 number. This is used by both the 32-bit and the 64-bit ABI. */
10584
10585void
9719ad41
RS
10586_bfd_mips_elf_final_write_processing (bfd *abfd,
10587 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
10588{
10589 unsigned int i;
10590 Elf_Internal_Shdr **hdrpp;
10591 const char *name;
10592 asection *sec;
10593
10594 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10595 is nonzero. This is for compatibility with old objects, which used
10596 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10597 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10598 mips_set_isa_flags (abfd);
10599
b49e97c9
TS
10600 /* Set the sh_info field for .gptab sections and other appropriate
10601 info for each special section. */
10602 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10603 i < elf_numsections (abfd);
10604 i++, hdrpp++)
10605 {
10606 switch ((*hdrpp)->sh_type)
10607 {
10608 case SHT_MIPS_MSYM:
10609 case SHT_MIPS_LIBLIST:
10610 sec = bfd_get_section_by_name (abfd, ".dynstr");
10611 if (sec != NULL)
10612 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10613 break;
10614
10615 case SHT_MIPS_GPTAB:
10616 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10617 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10618 BFD_ASSERT (name != NULL
0112cd26 10619 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
10620 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10621 BFD_ASSERT (sec != NULL);
10622 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10623 break;
10624
10625 case SHT_MIPS_CONTENT:
10626 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10627 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10628 BFD_ASSERT (name != NULL
0112cd26 10629 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
10630 sec = bfd_get_section_by_name (abfd,
10631 name + sizeof ".MIPS.content" - 1);
10632 BFD_ASSERT (sec != NULL);
10633 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10634 break;
10635
10636 case SHT_MIPS_SYMBOL_LIB:
10637 sec = bfd_get_section_by_name (abfd, ".dynsym");
10638 if (sec != NULL)
10639 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10640 sec = bfd_get_section_by_name (abfd, ".liblist");
10641 if (sec != NULL)
10642 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10643 break;
10644
10645 case SHT_MIPS_EVENTS:
10646 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10647 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10648 BFD_ASSERT (name != NULL);
0112cd26 10649 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
10650 sec = bfd_get_section_by_name (abfd,
10651 name + sizeof ".MIPS.events" - 1);
10652 else
10653 {
0112cd26 10654 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
10655 sec = bfd_get_section_by_name (abfd,
10656 (name
10657 + sizeof ".MIPS.post_rel" - 1));
10658 }
10659 BFD_ASSERT (sec != NULL);
10660 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10661 break;
10662
10663 }
10664 }
10665}
10666\f
8dc1a139 10667/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
10668 segments. */
10669
10670int
a6b96beb
AM
10671_bfd_mips_elf_additional_program_headers (bfd *abfd,
10672 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
10673{
10674 asection *s;
10675 int ret = 0;
10676
10677 /* See if we need a PT_MIPS_REGINFO segment. */
10678 s = bfd_get_section_by_name (abfd, ".reginfo");
10679 if (s && (s->flags & SEC_LOAD))
10680 ++ret;
10681
10682 /* See if we need a PT_MIPS_OPTIONS segment. */
10683 if (IRIX_COMPAT (abfd) == ict_irix6
10684 && bfd_get_section_by_name (abfd,
10685 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10686 ++ret;
10687
10688 /* See if we need a PT_MIPS_RTPROC segment. */
10689 if (IRIX_COMPAT (abfd) == ict_irix5
10690 && bfd_get_section_by_name (abfd, ".dynamic")
10691 && bfd_get_section_by_name (abfd, ".mdebug"))
10692 ++ret;
10693
98c904a8
RS
10694 /* Allocate a PT_NULL header in dynamic objects. See
10695 _bfd_mips_elf_modify_segment_map for details. */
10696 if (!SGI_COMPAT (abfd)
10697 && bfd_get_section_by_name (abfd, ".dynamic"))
10698 ++ret;
10699
b49e97c9
TS
10700 return ret;
10701}
10702
8dc1a139 10703/* Modify the segment map for an IRIX5 executable. */
b49e97c9 10704
b34976b6 10705bfd_boolean
9719ad41 10706_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 10707 struct bfd_link_info *info)
b49e97c9
TS
10708{
10709 asection *s;
10710 struct elf_segment_map *m, **pm;
10711 bfd_size_type amt;
10712
10713 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10714 segment. */
10715 s = bfd_get_section_by_name (abfd, ".reginfo");
10716 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10717 {
10718 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10719 if (m->p_type == PT_MIPS_REGINFO)
10720 break;
10721 if (m == NULL)
10722 {
10723 amt = sizeof *m;
9719ad41 10724 m = bfd_zalloc (abfd, amt);
b49e97c9 10725 if (m == NULL)
b34976b6 10726 return FALSE;
b49e97c9
TS
10727
10728 m->p_type = PT_MIPS_REGINFO;
10729 m->count = 1;
10730 m->sections[0] = s;
10731
10732 /* We want to put it after the PHDR and INTERP segments. */
10733 pm = &elf_tdata (abfd)->segment_map;
10734 while (*pm != NULL
10735 && ((*pm)->p_type == PT_PHDR
10736 || (*pm)->p_type == PT_INTERP))
10737 pm = &(*pm)->next;
10738
10739 m->next = *pm;
10740 *pm = m;
10741 }
10742 }
10743
10744 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10745 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 10746 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 10747 table. */
c1fd6598
AO
10748 if (NEWABI_P (abfd)
10749 /* On non-IRIX6 new abi, we'll have already created a segment
10750 for this section, so don't create another. I'm not sure this
10751 is not also the case for IRIX 6, but I can't test it right
10752 now. */
10753 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
10754 {
10755 for (s = abfd->sections; s; s = s->next)
10756 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10757 break;
10758
10759 if (s)
10760 {
10761 struct elf_segment_map *options_segment;
10762
98a8deaf
RS
10763 pm = &elf_tdata (abfd)->segment_map;
10764 while (*pm != NULL
10765 && ((*pm)->p_type == PT_PHDR
10766 || (*pm)->p_type == PT_INTERP))
10767 pm = &(*pm)->next;
b49e97c9 10768
8ded5a0f
AM
10769 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10770 {
10771 amt = sizeof (struct elf_segment_map);
10772 options_segment = bfd_zalloc (abfd, amt);
10773 options_segment->next = *pm;
10774 options_segment->p_type = PT_MIPS_OPTIONS;
10775 options_segment->p_flags = PF_R;
10776 options_segment->p_flags_valid = TRUE;
10777 options_segment->count = 1;
10778 options_segment->sections[0] = s;
10779 *pm = options_segment;
10780 }
b49e97c9
TS
10781 }
10782 }
10783 else
10784 {
10785 if (IRIX_COMPAT (abfd) == ict_irix5)
10786 {
10787 /* If there are .dynamic and .mdebug sections, we make a room
10788 for the RTPROC header. FIXME: Rewrite without section names. */
10789 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10790 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10791 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10792 {
10793 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10794 if (m->p_type == PT_MIPS_RTPROC)
10795 break;
10796 if (m == NULL)
10797 {
10798 amt = sizeof *m;
9719ad41 10799 m = bfd_zalloc (abfd, amt);
b49e97c9 10800 if (m == NULL)
b34976b6 10801 return FALSE;
b49e97c9
TS
10802
10803 m->p_type = PT_MIPS_RTPROC;
10804
10805 s = bfd_get_section_by_name (abfd, ".rtproc");
10806 if (s == NULL)
10807 {
10808 m->count = 0;
10809 m->p_flags = 0;
10810 m->p_flags_valid = 1;
10811 }
10812 else
10813 {
10814 m->count = 1;
10815 m->sections[0] = s;
10816 }
10817
10818 /* We want to put it after the DYNAMIC segment. */
10819 pm = &elf_tdata (abfd)->segment_map;
10820 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10821 pm = &(*pm)->next;
10822 if (*pm != NULL)
10823 pm = &(*pm)->next;
10824
10825 m->next = *pm;
10826 *pm = m;
10827 }
10828 }
10829 }
8dc1a139 10830 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
10831 .dynstr, .dynsym, and .hash sections, and everything in
10832 between. */
10833 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10834 pm = &(*pm)->next)
10835 if ((*pm)->p_type == PT_DYNAMIC)
10836 break;
10837 m = *pm;
10838 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10839 {
10840 /* For a normal mips executable the permissions for the PT_DYNAMIC
10841 segment are read, write and execute. We do that here since
10842 the code in elf.c sets only the read permission. This matters
10843 sometimes for the dynamic linker. */
10844 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10845 {
10846 m->p_flags = PF_R | PF_W | PF_X;
10847 m->p_flags_valid = 1;
10848 }
10849 }
f6f62d6f
RS
10850 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10851 glibc's dynamic linker has traditionally derived the number of
10852 tags from the p_filesz field, and sometimes allocates stack
10853 arrays of that size. An overly-big PT_DYNAMIC segment can
10854 be actively harmful in such cases. Making PT_DYNAMIC contain
10855 other sections can also make life hard for the prelinker,
10856 which might move one of the other sections to a different
10857 PT_LOAD segment. */
10858 if (SGI_COMPAT (abfd)
10859 && m != NULL
10860 && m->count == 1
10861 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
10862 {
10863 static const char *sec_names[] =
10864 {
10865 ".dynamic", ".dynstr", ".dynsym", ".hash"
10866 };
10867 bfd_vma low, high;
10868 unsigned int i, c;
10869 struct elf_segment_map *n;
10870
792b4a53 10871 low = ~(bfd_vma) 0;
b49e97c9
TS
10872 high = 0;
10873 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10874 {
10875 s = bfd_get_section_by_name (abfd, sec_names[i]);
10876 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10877 {
10878 bfd_size_type sz;
10879
10880 if (low > s->vma)
10881 low = s->vma;
eea6121a 10882 sz = s->size;
b49e97c9
TS
10883 if (high < s->vma + sz)
10884 high = s->vma + sz;
10885 }
10886 }
10887
10888 c = 0;
10889 for (s = abfd->sections; s != NULL; s = s->next)
10890 if ((s->flags & SEC_LOAD) != 0
10891 && s->vma >= low
eea6121a 10892 && s->vma + s->size <= high)
b49e97c9
TS
10893 ++c;
10894
10895 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 10896 n = bfd_zalloc (abfd, amt);
b49e97c9 10897 if (n == NULL)
b34976b6 10898 return FALSE;
b49e97c9
TS
10899 *n = *m;
10900 n->count = c;
10901
10902 i = 0;
10903 for (s = abfd->sections; s != NULL; s = s->next)
10904 {
10905 if ((s->flags & SEC_LOAD) != 0
10906 && s->vma >= low
eea6121a 10907 && s->vma + s->size <= high)
b49e97c9
TS
10908 {
10909 n->sections[i] = s;
10910 ++i;
10911 }
10912 }
10913
10914 *pm = n;
10915 }
10916 }
10917
98c904a8
RS
10918 /* Allocate a spare program header in dynamic objects so that tools
10919 like the prelinker can add an extra PT_LOAD entry.
10920
10921 If the prelinker needs to make room for a new PT_LOAD entry, its
10922 standard procedure is to move the first (read-only) sections into
10923 the new (writable) segment. However, the MIPS ABI requires
10924 .dynamic to be in a read-only segment, and the section will often
10925 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10926
10927 Although the prelinker could in principle move .dynamic to a
10928 writable segment, it seems better to allocate a spare program
10929 header instead, and avoid the need to move any sections.
10930 There is a long tradition of allocating spare dynamic tags,
10931 so allocating a spare program header seems like a natural
7c8b76cc
JM
10932 extension.
10933
10934 If INFO is NULL, we may be copying an already prelinked binary
10935 with objcopy or strip, so do not add this header. */
10936 if (info != NULL
10937 && !SGI_COMPAT (abfd)
98c904a8
RS
10938 && bfd_get_section_by_name (abfd, ".dynamic"))
10939 {
10940 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10941 if ((*pm)->p_type == PT_NULL)
10942 break;
10943 if (*pm == NULL)
10944 {
10945 m = bfd_zalloc (abfd, sizeof (*m));
10946 if (m == NULL)
10947 return FALSE;
10948
10949 m->p_type = PT_NULL;
10950 *pm = m;
10951 }
10952 }
10953
b34976b6 10954 return TRUE;
b49e97c9
TS
10955}
10956\f
10957/* Return the section that should be marked against GC for a given
10958 relocation. */
10959
10960asection *
9719ad41 10961_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 10962 struct bfd_link_info *info,
9719ad41
RS
10963 Elf_Internal_Rela *rel,
10964 struct elf_link_hash_entry *h,
10965 Elf_Internal_Sym *sym)
b49e97c9
TS
10966{
10967 /* ??? Do mips16 stub sections need to be handled special? */
10968
10969 if (h != NULL)
07adf181
AM
10970 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10971 {
10972 case R_MIPS_GNU_VTINHERIT:
10973 case R_MIPS_GNU_VTENTRY:
10974 return NULL;
10975 }
b49e97c9 10976
07adf181 10977 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
10978}
10979
10980/* Update the got entry reference counts for the section being removed. */
10981
b34976b6 10982bfd_boolean
9719ad41
RS
10983_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10984 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10985 asection *sec ATTRIBUTE_UNUSED,
10986 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
10987{
10988#if 0
10989 Elf_Internal_Shdr *symtab_hdr;
10990 struct elf_link_hash_entry **sym_hashes;
10991 bfd_signed_vma *local_got_refcounts;
10992 const Elf_Internal_Rela *rel, *relend;
10993 unsigned long r_symndx;
10994 struct elf_link_hash_entry *h;
10995
7dda2462
TG
10996 if (info->relocatable)
10997 return TRUE;
10998
b49e97c9
TS
10999 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11000 sym_hashes = elf_sym_hashes (abfd);
11001 local_got_refcounts = elf_local_got_refcounts (abfd);
11002
11003 relend = relocs + sec->reloc_count;
11004 for (rel = relocs; rel < relend; rel++)
11005 switch (ELF_R_TYPE (abfd, rel->r_info))
11006 {
738e5348
RS
11007 case R_MIPS16_GOT16:
11008 case R_MIPS16_CALL16:
b49e97c9
TS
11009 case R_MIPS_GOT16:
11010 case R_MIPS_CALL16:
11011 case R_MIPS_CALL_HI16:
11012 case R_MIPS_CALL_LO16:
11013 case R_MIPS_GOT_HI16:
11014 case R_MIPS_GOT_LO16:
4a14403c
TS
11015 case R_MIPS_GOT_DISP:
11016 case R_MIPS_GOT_PAGE:
11017 case R_MIPS_GOT_OFST:
b49e97c9
TS
11018 /* ??? It would seem that the existing MIPS code does no sort
11019 of reference counting or whatnot on its GOT and PLT entries,
11020 so it is not possible to garbage collect them at this time. */
11021 break;
11022
11023 default:
11024 break;
11025 }
11026#endif
11027
b34976b6 11028 return TRUE;
b49e97c9
TS
11029}
11030\f
11031/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11032 hiding the old indirect symbol. Process additional relocation
11033 information. Also called for weakdefs, in which case we just let
11034 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11035
11036void
fcfa13d2 11037_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
11038 struct elf_link_hash_entry *dir,
11039 struct elf_link_hash_entry *ind)
b49e97c9
TS
11040{
11041 struct mips_elf_link_hash_entry *dirmips, *indmips;
11042
fcfa13d2 11043 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 11044
861fb55a
DJ
11045 dirmips = (struct mips_elf_link_hash_entry *) dir;
11046 indmips = (struct mips_elf_link_hash_entry *) ind;
11047 /* Any absolute non-dynamic relocations against an indirect or weak
11048 definition will be against the target symbol. */
11049 if (indmips->has_static_relocs)
11050 dirmips->has_static_relocs = TRUE;
11051
b49e97c9
TS
11052 if (ind->root.type != bfd_link_hash_indirect)
11053 return;
11054
b49e97c9
TS
11055 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11056 if (indmips->readonly_reloc)
b34976b6 11057 dirmips->readonly_reloc = TRUE;
b49e97c9 11058 if (indmips->no_fn_stub)
b34976b6 11059 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
11060 if (indmips->fn_stub)
11061 {
11062 dirmips->fn_stub = indmips->fn_stub;
11063 indmips->fn_stub = NULL;
11064 }
11065 if (indmips->need_fn_stub)
11066 {
11067 dirmips->need_fn_stub = TRUE;
11068 indmips->need_fn_stub = FALSE;
11069 }
11070 if (indmips->call_stub)
11071 {
11072 dirmips->call_stub = indmips->call_stub;
11073 indmips->call_stub = NULL;
11074 }
11075 if (indmips->call_fp_stub)
11076 {
11077 dirmips->call_fp_stub = indmips->call_fp_stub;
11078 indmips->call_fp_stub = NULL;
11079 }
634835ae
RS
11080 if (indmips->global_got_area < dirmips->global_got_area)
11081 dirmips->global_got_area = indmips->global_got_area;
11082 if (indmips->global_got_area < GGA_NONE)
11083 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
11084 if (indmips->has_nonpic_branches)
11085 dirmips->has_nonpic_branches = TRUE;
0f20cc35
DJ
11086
11087 if (dirmips->tls_type == 0)
11088 dirmips->tls_type = indmips->tls_type;
b49e97c9 11089}
b49e97c9 11090\f
d01414a5
TS
11091#define PDR_SIZE 32
11092
b34976b6 11093bfd_boolean
9719ad41
RS
11094_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11095 struct bfd_link_info *info)
d01414a5
TS
11096{
11097 asection *o;
b34976b6 11098 bfd_boolean ret = FALSE;
d01414a5
TS
11099 unsigned char *tdata;
11100 size_t i, skip;
11101
11102 o = bfd_get_section_by_name (abfd, ".pdr");
11103 if (! o)
b34976b6 11104 return FALSE;
eea6121a 11105 if (o->size == 0)
b34976b6 11106 return FALSE;
eea6121a 11107 if (o->size % PDR_SIZE != 0)
b34976b6 11108 return FALSE;
d01414a5
TS
11109 if (o->output_section != NULL
11110 && bfd_is_abs_section (o->output_section))
b34976b6 11111 return FALSE;
d01414a5 11112
eea6121a 11113 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 11114 if (! tdata)
b34976b6 11115 return FALSE;
d01414a5 11116
9719ad41 11117 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 11118 info->keep_memory);
d01414a5
TS
11119 if (!cookie->rels)
11120 {
11121 free (tdata);
b34976b6 11122 return FALSE;
d01414a5
TS
11123 }
11124
11125 cookie->rel = cookie->rels;
11126 cookie->relend = cookie->rels + o->reloc_count;
11127
eea6121a 11128 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 11129 {
c152c796 11130 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
11131 {
11132 tdata[i] = 1;
11133 skip ++;
11134 }
11135 }
11136
11137 if (skip != 0)
11138 {
f0abc2a1 11139 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 11140 o->size -= skip * PDR_SIZE;
b34976b6 11141 ret = TRUE;
d01414a5
TS
11142 }
11143 else
11144 free (tdata);
11145
11146 if (! info->keep_memory)
11147 free (cookie->rels);
11148
11149 return ret;
11150}
11151
b34976b6 11152bfd_boolean
9719ad41 11153_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
11154{
11155 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
11156 return TRUE;
11157 return FALSE;
53bfd6b4 11158}
d01414a5 11159
b34976b6 11160bfd_boolean
c7b8f16e
JB
11161_bfd_mips_elf_write_section (bfd *output_bfd,
11162 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11163 asection *sec, bfd_byte *contents)
d01414a5
TS
11164{
11165 bfd_byte *to, *from, *end;
11166 int i;
11167
11168 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 11169 return FALSE;
d01414a5 11170
f0abc2a1 11171 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 11172 return FALSE;
d01414a5
TS
11173
11174 to = contents;
eea6121a 11175 end = contents + sec->size;
d01414a5
TS
11176 for (from = contents, i = 0;
11177 from < end;
11178 from += PDR_SIZE, i++)
11179 {
f0abc2a1 11180 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
11181 continue;
11182 if (to != from)
11183 memcpy (to, from, PDR_SIZE);
11184 to += PDR_SIZE;
11185 }
11186 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 11187 sec->output_offset, sec->size);
b34976b6 11188 return TRUE;
d01414a5 11189}
53bfd6b4 11190\f
b49e97c9
TS
11191/* MIPS ELF uses a special find_nearest_line routine in order the
11192 handle the ECOFF debugging information. */
11193
11194struct mips_elf_find_line
11195{
11196 struct ecoff_debug_info d;
11197 struct ecoff_find_line i;
11198};
11199
b34976b6 11200bfd_boolean
9719ad41
RS
11201_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11202 asymbol **symbols, bfd_vma offset,
11203 const char **filename_ptr,
11204 const char **functionname_ptr,
11205 unsigned int *line_ptr)
b49e97c9
TS
11206{
11207 asection *msec;
11208
11209 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11210 filename_ptr, functionname_ptr,
11211 line_ptr))
b34976b6 11212 return TRUE;
b49e97c9
TS
11213
11214 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11215 filename_ptr, functionname_ptr,
9719ad41 11216 line_ptr, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 11217 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 11218 return TRUE;
b49e97c9
TS
11219
11220 msec = bfd_get_section_by_name (abfd, ".mdebug");
11221 if (msec != NULL)
11222 {
11223 flagword origflags;
11224 struct mips_elf_find_line *fi;
11225 const struct ecoff_debug_swap * const swap =
11226 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11227
11228 /* If we are called during a link, mips_elf_final_link may have
11229 cleared the SEC_HAS_CONTENTS field. We force it back on here
11230 if appropriate (which it normally will be). */
11231 origflags = msec->flags;
11232 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11233 msec->flags |= SEC_HAS_CONTENTS;
11234
11235 fi = elf_tdata (abfd)->find_line_info;
11236 if (fi == NULL)
11237 {
11238 bfd_size_type external_fdr_size;
11239 char *fraw_src;
11240 char *fraw_end;
11241 struct fdr *fdr_ptr;
11242 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11243
9719ad41 11244 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
11245 if (fi == NULL)
11246 {
11247 msec->flags = origflags;
b34976b6 11248 return FALSE;
b49e97c9
TS
11249 }
11250
11251 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11252 {
11253 msec->flags = origflags;
b34976b6 11254 return FALSE;
b49e97c9
TS
11255 }
11256
11257 /* Swap in the FDR information. */
11258 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 11259 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
11260 if (fi->d.fdr == NULL)
11261 {
11262 msec->flags = origflags;
b34976b6 11263 return FALSE;
b49e97c9
TS
11264 }
11265 external_fdr_size = swap->external_fdr_size;
11266 fdr_ptr = fi->d.fdr;
11267 fraw_src = (char *) fi->d.external_fdr;
11268 fraw_end = (fraw_src
11269 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11270 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 11271 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9
TS
11272
11273 elf_tdata (abfd)->find_line_info = fi;
11274
11275 /* Note that we don't bother to ever free this information.
11276 find_nearest_line is either called all the time, as in
11277 objdump -l, so the information should be saved, or it is
11278 rarely called, as in ld error messages, so the memory
11279 wasted is unimportant. Still, it would probably be a
11280 good idea for free_cached_info to throw it away. */
11281 }
11282
11283 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11284 &fi->i, filename_ptr, functionname_ptr,
11285 line_ptr))
11286 {
11287 msec->flags = origflags;
b34976b6 11288 return TRUE;
b49e97c9
TS
11289 }
11290
11291 msec->flags = origflags;
11292 }
11293
11294 /* Fall back on the generic ELF find_nearest_line routine. */
11295
11296 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11297 filename_ptr, functionname_ptr,
11298 line_ptr);
11299}
4ab527b0
FF
11300
11301bfd_boolean
11302_bfd_mips_elf_find_inliner_info (bfd *abfd,
11303 const char **filename_ptr,
11304 const char **functionname_ptr,
11305 unsigned int *line_ptr)
11306{
11307 bfd_boolean found;
11308 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11309 functionname_ptr, line_ptr,
11310 & elf_tdata (abfd)->dwarf2_find_line_info);
11311 return found;
11312}
11313
b49e97c9
TS
11314\f
11315/* When are writing out the .options or .MIPS.options section,
11316 remember the bytes we are writing out, so that we can install the
11317 GP value in the section_processing routine. */
11318
b34976b6 11319bfd_boolean
9719ad41
RS
11320_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11321 const void *location,
11322 file_ptr offset, bfd_size_type count)
b49e97c9 11323{
cc2e31b9 11324 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
11325 {
11326 bfd_byte *c;
11327
11328 if (elf_section_data (section) == NULL)
11329 {
11330 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 11331 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 11332 if (elf_section_data (section) == NULL)
b34976b6 11333 return FALSE;
b49e97c9 11334 }
f0abc2a1 11335 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
11336 if (c == NULL)
11337 {
eea6121a 11338 c = bfd_zalloc (abfd, section->size);
b49e97c9 11339 if (c == NULL)
b34976b6 11340 return FALSE;
f0abc2a1 11341 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
11342 }
11343
9719ad41 11344 memcpy (c + offset, location, count);
b49e97c9
TS
11345 }
11346
11347 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11348 count);
11349}
11350
11351/* This is almost identical to bfd_generic_get_... except that some
11352 MIPS relocations need to be handled specially. Sigh. */
11353
11354bfd_byte *
9719ad41
RS
11355_bfd_elf_mips_get_relocated_section_contents
11356 (bfd *abfd,
11357 struct bfd_link_info *link_info,
11358 struct bfd_link_order *link_order,
11359 bfd_byte *data,
11360 bfd_boolean relocatable,
11361 asymbol **symbols)
b49e97c9
TS
11362{
11363 /* Get enough memory to hold the stuff */
11364 bfd *input_bfd = link_order->u.indirect.section->owner;
11365 asection *input_section = link_order->u.indirect.section;
eea6121a 11366 bfd_size_type sz;
b49e97c9
TS
11367
11368 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11369 arelent **reloc_vector = NULL;
11370 long reloc_count;
11371
11372 if (reloc_size < 0)
11373 goto error_return;
11374
9719ad41 11375 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
11376 if (reloc_vector == NULL && reloc_size != 0)
11377 goto error_return;
11378
11379 /* read in the section */
eea6121a
AM
11380 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11381 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
11382 goto error_return;
11383
b49e97c9
TS
11384 reloc_count = bfd_canonicalize_reloc (input_bfd,
11385 input_section,
11386 reloc_vector,
11387 symbols);
11388 if (reloc_count < 0)
11389 goto error_return;
11390
11391 if (reloc_count > 0)
11392 {
11393 arelent **parent;
11394 /* for mips */
11395 int gp_found;
11396 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11397
11398 {
11399 struct bfd_hash_entry *h;
11400 struct bfd_link_hash_entry *lh;
11401 /* Skip all this stuff if we aren't mixing formats. */
11402 if (abfd && input_bfd
11403 && abfd->xvec == input_bfd->xvec)
11404 lh = 0;
11405 else
11406 {
b34976b6 11407 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
11408 lh = (struct bfd_link_hash_entry *) h;
11409 }
11410 lookup:
11411 if (lh)
11412 {
11413 switch (lh->type)
11414 {
11415 case bfd_link_hash_undefined:
11416 case bfd_link_hash_undefweak:
11417 case bfd_link_hash_common:
11418 gp_found = 0;
11419 break;
11420 case bfd_link_hash_defined:
11421 case bfd_link_hash_defweak:
11422 gp_found = 1;
11423 gp = lh->u.def.value;
11424 break;
11425 case bfd_link_hash_indirect:
11426 case bfd_link_hash_warning:
11427 lh = lh->u.i.link;
11428 /* @@FIXME ignoring warning for now */
11429 goto lookup;
11430 case bfd_link_hash_new:
11431 default:
11432 abort ();
11433 }
11434 }
11435 else
11436 gp_found = 0;
11437 }
11438 /* end mips */
9719ad41 11439 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 11440 {
9719ad41 11441 char *error_message = NULL;
b49e97c9
TS
11442 bfd_reloc_status_type r;
11443
11444 /* Specific to MIPS: Deal with relocation types that require
11445 knowing the gp of the output bfd. */
11446 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 11447
8236346f
EC
11448 /* If we've managed to find the gp and have a special
11449 function for the relocation then go ahead, else default
11450 to the generic handling. */
11451 if (gp_found
11452 && (*parent)->howto->special_function
11453 == _bfd_mips_elf32_gprel16_reloc)
11454 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11455 input_section, relocatable,
11456 data, gp);
11457 else
86324f90 11458 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
11459 input_section,
11460 relocatable ? abfd : NULL,
11461 &error_message);
b49e97c9 11462
1049f94e 11463 if (relocatable)
b49e97c9
TS
11464 {
11465 asection *os = input_section->output_section;
11466
11467 /* A partial link, so keep the relocs */
11468 os->orelocation[os->reloc_count] = *parent;
11469 os->reloc_count++;
11470 }
11471
11472 if (r != bfd_reloc_ok)
11473 {
11474 switch (r)
11475 {
11476 case bfd_reloc_undefined:
11477 if (!((*link_info->callbacks->undefined_symbol)
11478 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 11479 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
11480 goto error_return;
11481 break;
11482 case bfd_reloc_dangerous:
9719ad41 11483 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
11484 if (!((*link_info->callbacks->reloc_dangerous)
11485 (link_info, error_message, input_bfd, input_section,
11486 (*parent)->address)))
11487 goto error_return;
11488 break;
11489 case bfd_reloc_overflow:
11490 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
11491 (link_info, NULL,
11492 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
11493 (*parent)->howto->name, (*parent)->addend,
11494 input_bfd, input_section, (*parent)->address)))
11495 goto error_return;
11496 break;
11497 case bfd_reloc_outofrange:
11498 default:
11499 abort ();
11500 break;
11501 }
11502
11503 }
11504 }
11505 }
11506 if (reloc_vector != NULL)
11507 free (reloc_vector);
11508 return data;
11509
11510error_return:
11511 if (reloc_vector != NULL)
11512 free (reloc_vector);
11513 return NULL;
11514}
11515\f
11516/* Create a MIPS ELF linker hash table. */
11517
11518struct bfd_link_hash_table *
9719ad41 11519_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
11520{
11521 struct mips_elf_link_hash_table *ret;
11522 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11523
9719ad41
RS
11524 ret = bfd_malloc (amt);
11525 if (ret == NULL)
b49e97c9
TS
11526 return NULL;
11527
66eb6687
AM
11528 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11529 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
11530 sizeof (struct mips_elf_link_hash_entry),
11531 MIPS_ELF_DATA))
b49e97c9 11532 {
e2d34d7d 11533 free (ret);
b49e97c9
TS
11534 return NULL;
11535 }
11536
11537#if 0
11538 /* We no longer use this. */
11539 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11540 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11541#endif
11542 ret->procedure_count = 0;
11543 ret->compact_rel_size = 0;
b34976b6 11544 ret->use_rld_obj_head = FALSE;
b49e97c9 11545 ret->rld_value = 0;
b34976b6 11546 ret->mips16_stubs_seen = FALSE;
861fb55a 11547 ret->use_plts_and_copy_relocs = FALSE;
0a44bf69 11548 ret->is_vxworks = FALSE;
0e53d9da 11549 ret->small_data_overflow_reported = FALSE;
0a44bf69
RS
11550 ret->srelbss = NULL;
11551 ret->sdynbss = NULL;
11552 ret->srelplt = NULL;
11553 ret->srelplt2 = NULL;
11554 ret->sgotplt = NULL;
11555 ret->splt = NULL;
4e41d0d7 11556 ret->sstubs = NULL;
a8028dd0
RS
11557 ret->sgot = NULL;
11558 ret->got_info = NULL;
0a44bf69
RS
11559 ret->plt_header_size = 0;
11560 ret->plt_entry_size = 0;
33bb52fb 11561 ret->lazy_stub_count = 0;
5108fc1b 11562 ret->function_stub_size = 0;
861fb55a
DJ
11563 ret->strampoline = NULL;
11564 ret->la25_stubs = NULL;
11565 ret->add_stub_section = NULL;
b49e97c9
TS
11566
11567 return &ret->root.root;
11568}
0a44bf69
RS
11569
11570/* Likewise, but indicate that the target is VxWorks. */
11571
11572struct bfd_link_hash_table *
11573_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11574{
11575 struct bfd_link_hash_table *ret;
11576
11577 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11578 if (ret)
11579 {
11580 struct mips_elf_link_hash_table *htab;
11581
11582 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
11583 htab->use_plts_and_copy_relocs = TRUE;
11584 htab->is_vxworks = TRUE;
0a44bf69
RS
11585 }
11586 return ret;
11587}
861fb55a
DJ
11588
11589/* A function that the linker calls if we are allowed to use PLTs
11590 and copy relocs. */
11591
11592void
11593_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11594{
11595 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11596}
b49e97c9
TS
11597\f
11598/* We need to use a special link routine to handle the .reginfo and
11599 the .mdebug sections. We need to merge all instances of these
11600 sections together, not write them all out sequentially. */
11601
b34976b6 11602bfd_boolean
9719ad41 11603_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 11604{
b49e97c9
TS
11605 asection *o;
11606 struct bfd_link_order *p;
11607 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11608 asection *rtproc_sec;
11609 Elf32_RegInfo reginfo;
11610 struct ecoff_debug_info debug;
861fb55a 11611 struct mips_htab_traverse_info hti;
7a2a6943
NC
11612 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11613 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 11614 HDRR *symhdr = &debug.symbolic_header;
9719ad41 11615 void *mdebug_handle = NULL;
b49e97c9
TS
11616 asection *s;
11617 EXTR esym;
11618 unsigned int i;
11619 bfd_size_type amt;
0a44bf69 11620 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
11621
11622 static const char * const secname[] =
11623 {
11624 ".text", ".init", ".fini", ".data",
11625 ".rodata", ".sdata", ".sbss", ".bss"
11626 };
11627 static const int sc[] =
11628 {
11629 scText, scInit, scFini, scData,
11630 scRData, scSData, scSBss, scBss
11631 };
11632
d4596a51
RS
11633 /* Sort the dynamic symbols so that those with GOT entries come after
11634 those without. */
0a44bf69 11635 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11636 BFD_ASSERT (htab != NULL);
11637
d4596a51
RS
11638 if (!mips_elf_sort_hash_table (abfd, info))
11639 return FALSE;
b49e97c9 11640
861fb55a
DJ
11641 /* Create any scheduled LA25 stubs. */
11642 hti.info = info;
11643 hti.output_bfd = abfd;
11644 hti.error = FALSE;
11645 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11646 if (hti.error)
11647 return FALSE;
11648
b49e97c9
TS
11649 /* Get a value for the GP register. */
11650 if (elf_gp (abfd) == 0)
11651 {
11652 struct bfd_link_hash_entry *h;
11653
b34976b6 11654 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 11655 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
11656 elf_gp (abfd) = (h->u.def.value
11657 + h->u.def.section->output_section->vma
11658 + h->u.def.section->output_offset);
0a44bf69
RS
11659 else if (htab->is_vxworks
11660 && (h = bfd_link_hash_lookup (info->hash,
11661 "_GLOBAL_OFFSET_TABLE_",
11662 FALSE, FALSE, TRUE))
11663 && h->type == bfd_link_hash_defined)
11664 elf_gp (abfd) = (h->u.def.section->output_section->vma
11665 + h->u.def.section->output_offset
11666 + h->u.def.value);
1049f94e 11667 else if (info->relocatable)
b49e97c9
TS
11668 {
11669 bfd_vma lo = MINUS_ONE;
11670
11671 /* Find the GP-relative section with the lowest offset. */
9719ad41 11672 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11673 if (o->vma < lo
11674 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11675 lo = o->vma;
11676
11677 /* And calculate GP relative to that. */
0a44bf69 11678 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
11679 }
11680 else
11681 {
11682 /* If the relocate_section function needs to do a reloc
11683 involving the GP value, it should make a reloc_dangerous
11684 callback to warn that GP is not defined. */
11685 }
11686 }
11687
11688 /* Go through the sections and collect the .reginfo and .mdebug
11689 information. */
11690 reginfo_sec = NULL;
11691 mdebug_sec = NULL;
11692 gptab_data_sec = NULL;
11693 gptab_bss_sec = NULL;
9719ad41 11694 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11695 {
11696 if (strcmp (o->name, ".reginfo") == 0)
11697 {
11698 memset (&reginfo, 0, sizeof reginfo);
11699
11700 /* We have found the .reginfo section in the output file.
11701 Look through all the link_orders comprising it and merge
11702 the information together. */
8423293d 11703 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11704 {
11705 asection *input_section;
11706 bfd *input_bfd;
11707 Elf32_External_RegInfo ext;
11708 Elf32_RegInfo sub;
11709
11710 if (p->type != bfd_indirect_link_order)
11711 {
11712 if (p->type == bfd_data_link_order)
11713 continue;
11714 abort ();
11715 }
11716
11717 input_section = p->u.indirect.section;
11718 input_bfd = input_section->owner;
11719
b49e97c9 11720 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 11721 &ext, 0, sizeof ext))
b34976b6 11722 return FALSE;
b49e97c9
TS
11723
11724 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11725
11726 reginfo.ri_gprmask |= sub.ri_gprmask;
11727 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11728 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11729 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11730 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11731
11732 /* ri_gp_value is set by the function
11733 mips_elf32_section_processing when the section is
11734 finally written out. */
11735
11736 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11737 elf_link_input_bfd ignores this section. */
11738 input_section->flags &= ~SEC_HAS_CONTENTS;
11739 }
11740
11741 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 11742 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
11743
11744 /* Skip this section later on (I don't think this currently
11745 matters, but someday it might). */
8423293d 11746 o->map_head.link_order = NULL;
b49e97c9
TS
11747
11748 reginfo_sec = o;
11749 }
11750
11751 if (strcmp (o->name, ".mdebug") == 0)
11752 {
11753 struct extsym_info einfo;
11754 bfd_vma last;
11755
11756 /* We have found the .mdebug section in the output file.
11757 Look through all the link_orders comprising it and merge
11758 the information together. */
11759 symhdr->magic = swap->sym_magic;
11760 /* FIXME: What should the version stamp be? */
11761 symhdr->vstamp = 0;
11762 symhdr->ilineMax = 0;
11763 symhdr->cbLine = 0;
11764 symhdr->idnMax = 0;
11765 symhdr->ipdMax = 0;
11766 symhdr->isymMax = 0;
11767 symhdr->ioptMax = 0;
11768 symhdr->iauxMax = 0;
11769 symhdr->issMax = 0;
11770 symhdr->issExtMax = 0;
11771 symhdr->ifdMax = 0;
11772 symhdr->crfd = 0;
11773 symhdr->iextMax = 0;
11774
11775 /* We accumulate the debugging information itself in the
11776 debug_info structure. */
11777 debug.line = NULL;
11778 debug.external_dnr = NULL;
11779 debug.external_pdr = NULL;
11780 debug.external_sym = NULL;
11781 debug.external_opt = NULL;
11782 debug.external_aux = NULL;
11783 debug.ss = NULL;
11784 debug.ssext = debug.ssext_end = NULL;
11785 debug.external_fdr = NULL;
11786 debug.external_rfd = NULL;
11787 debug.external_ext = debug.external_ext_end = NULL;
11788
11789 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 11790 if (mdebug_handle == NULL)
b34976b6 11791 return FALSE;
b49e97c9
TS
11792
11793 esym.jmptbl = 0;
11794 esym.cobol_main = 0;
11795 esym.weakext = 0;
11796 esym.reserved = 0;
11797 esym.ifd = ifdNil;
11798 esym.asym.iss = issNil;
11799 esym.asym.st = stLocal;
11800 esym.asym.reserved = 0;
11801 esym.asym.index = indexNil;
11802 last = 0;
11803 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11804 {
11805 esym.asym.sc = sc[i];
11806 s = bfd_get_section_by_name (abfd, secname[i]);
11807 if (s != NULL)
11808 {
11809 esym.asym.value = s->vma;
eea6121a 11810 last = s->vma + s->size;
b49e97c9
TS
11811 }
11812 else
11813 esym.asym.value = last;
11814 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11815 secname[i], &esym))
b34976b6 11816 return FALSE;
b49e97c9
TS
11817 }
11818
8423293d 11819 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11820 {
11821 asection *input_section;
11822 bfd *input_bfd;
11823 const struct ecoff_debug_swap *input_swap;
11824 struct ecoff_debug_info input_debug;
11825 char *eraw_src;
11826 char *eraw_end;
11827
11828 if (p->type != bfd_indirect_link_order)
11829 {
11830 if (p->type == bfd_data_link_order)
11831 continue;
11832 abort ();
11833 }
11834
11835 input_section = p->u.indirect.section;
11836 input_bfd = input_section->owner;
11837
d5eaccd7 11838 if (!is_mips_elf (input_bfd))
b49e97c9
TS
11839 {
11840 /* I don't know what a non MIPS ELF bfd would be
11841 doing with a .mdebug section, but I don't really
11842 want to deal with it. */
11843 continue;
11844 }
11845
11846 input_swap = (get_elf_backend_data (input_bfd)
11847 ->elf_backend_ecoff_debug_swap);
11848
eea6121a 11849 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
11850
11851 /* The ECOFF linking code expects that we have already
11852 read in the debugging information and set up an
11853 ecoff_debug_info structure, so we do that now. */
11854 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11855 &input_debug))
b34976b6 11856 return FALSE;
b49e97c9
TS
11857
11858 if (! (bfd_ecoff_debug_accumulate
11859 (mdebug_handle, abfd, &debug, swap, input_bfd,
11860 &input_debug, input_swap, info)))
b34976b6 11861 return FALSE;
b49e97c9
TS
11862
11863 /* Loop through the external symbols. For each one with
11864 interesting information, try to find the symbol in
11865 the linker global hash table and save the information
11866 for the output external symbols. */
11867 eraw_src = input_debug.external_ext;
11868 eraw_end = (eraw_src
11869 + (input_debug.symbolic_header.iextMax
11870 * input_swap->external_ext_size));
11871 for (;
11872 eraw_src < eraw_end;
11873 eraw_src += input_swap->external_ext_size)
11874 {
11875 EXTR ext;
11876 const char *name;
11877 struct mips_elf_link_hash_entry *h;
11878
9719ad41 11879 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
11880 if (ext.asym.sc == scNil
11881 || ext.asym.sc == scUndefined
11882 || ext.asym.sc == scSUndefined)
11883 continue;
11884
11885 name = input_debug.ssext + ext.asym.iss;
11886 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 11887 name, FALSE, FALSE, TRUE);
b49e97c9
TS
11888 if (h == NULL || h->esym.ifd != -2)
11889 continue;
11890
11891 if (ext.ifd != -1)
11892 {
11893 BFD_ASSERT (ext.ifd
11894 < input_debug.symbolic_header.ifdMax);
11895 ext.ifd = input_debug.ifdmap[ext.ifd];
11896 }
11897
11898 h->esym = ext;
11899 }
11900
11901 /* Free up the information we just read. */
11902 free (input_debug.line);
11903 free (input_debug.external_dnr);
11904 free (input_debug.external_pdr);
11905 free (input_debug.external_sym);
11906 free (input_debug.external_opt);
11907 free (input_debug.external_aux);
11908 free (input_debug.ss);
11909 free (input_debug.ssext);
11910 free (input_debug.external_fdr);
11911 free (input_debug.external_rfd);
11912 free (input_debug.external_ext);
11913
11914 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11915 elf_link_input_bfd ignores this section. */
11916 input_section->flags &= ~SEC_HAS_CONTENTS;
11917 }
11918
11919 if (SGI_COMPAT (abfd) && info->shared)
11920 {
11921 /* Create .rtproc section. */
11922 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11923 if (rtproc_sec == NULL)
11924 {
11925 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11926 | SEC_LINKER_CREATED | SEC_READONLY);
11927
3496cb2a
L
11928 rtproc_sec = bfd_make_section_with_flags (abfd,
11929 ".rtproc",
11930 flags);
b49e97c9 11931 if (rtproc_sec == NULL
b49e97c9 11932 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 11933 return FALSE;
b49e97c9
TS
11934 }
11935
11936 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11937 info, rtproc_sec,
11938 &debug))
b34976b6 11939 return FALSE;
b49e97c9
TS
11940 }
11941
11942 /* Build the external symbol information. */
11943 einfo.abfd = abfd;
11944 einfo.info = info;
11945 einfo.debug = &debug;
11946 einfo.swap = swap;
b34976b6 11947 einfo.failed = FALSE;
b49e97c9 11948 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 11949 mips_elf_output_extsym, &einfo);
b49e97c9 11950 if (einfo.failed)
b34976b6 11951 return FALSE;
b49e97c9
TS
11952
11953 /* Set the size of the .mdebug section. */
eea6121a 11954 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
11955
11956 /* Skip this section later on (I don't think this currently
11957 matters, but someday it might). */
8423293d 11958 o->map_head.link_order = NULL;
b49e97c9
TS
11959
11960 mdebug_sec = o;
11961 }
11962
0112cd26 11963 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
11964 {
11965 const char *subname;
11966 unsigned int c;
11967 Elf32_gptab *tab;
11968 Elf32_External_gptab *ext_tab;
11969 unsigned int j;
11970
11971 /* The .gptab.sdata and .gptab.sbss sections hold
11972 information describing how the small data area would
11973 change depending upon the -G switch. These sections
11974 not used in executables files. */
1049f94e 11975 if (! info->relocatable)
b49e97c9 11976 {
8423293d 11977 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11978 {
11979 asection *input_section;
11980
11981 if (p->type != bfd_indirect_link_order)
11982 {
11983 if (p->type == bfd_data_link_order)
11984 continue;
11985 abort ();
11986 }
11987
11988 input_section = p->u.indirect.section;
11989
11990 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11991 elf_link_input_bfd ignores this section. */
11992 input_section->flags &= ~SEC_HAS_CONTENTS;
11993 }
11994
11995 /* Skip this section later on (I don't think this
11996 currently matters, but someday it might). */
8423293d 11997 o->map_head.link_order = NULL;
b49e97c9
TS
11998
11999 /* Really remove the section. */
5daa8fe7 12000 bfd_section_list_remove (abfd, o);
b49e97c9
TS
12001 --abfd->section_count;
12002
12003 continue;
12004 }
12005
12006 /* There is one gptab for initialized data, and one for
12007 uninitialized data. */
12008 if (strcmp (o->name, ".gptab.sdata") == 0)
12009 gptab_data_sec = o;
12010 else if (strcmp (o->name, ".gptab.sbss") == 0)
12011 gptab_bss_sec = o;
12012 else
12013 {
12014 (*_bfd_error_handler)
12015 (_("%s: illegal section name `%s'"),
12016 bfd_get_filename (abfd), o->name);
12017 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 12018 return FALSE;
b49e97c9
TS
12019 }
12020
12021 /* The linker script always combines .gptab.data and
12022 .gptab.sdata into .gptab.sdata, and likewise for
12023 .gptab.bss and .gptab.sbss. It is possible that there is
12024 no .sdata or .sbss section in the output file, in which
12025 case we must change the name of the output section. */
12026 subname = o->name + sizeof ".gptab" - 1;
12027 if (bfd_get_section_by_name (abfd, subname) == NULL)
12028 {
12029 if (o == gptab_data_sec)
12030 o->name = ".gptab.data";
12031 else
12032 o->name = ".gptab.bss";
12033 subname = o->name + sizeof ".gptab" - 1;
12034 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
12035 }
12036
12037 /* Set up the first entry. */
12038 c = 1;
12039 amt = c * sizeof (Elf32_gptab);
9719ad41 12040 tab = bfd_malloc (amt);
b49e97c9 12041 if (tab == NULL)
b34976b6 12042 return FALSE;
b49e97c9
TS
12043 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
12044 tab[0].gt_header.gt_unused = 0;
12045
12046 /* Combine the input sections. */
8423293d 12047 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
12048 {
12049 asection *input_section;
12050 bfd *input_bfd;
12051 bfd_size_type size;
12052 unsigned long last;
12053 bfd_size_type gpentry;
12054
12055 if (p->type != bfd_indirect_link_order)
12056 {
12057 if (p->type == bfd_data_link_order)
12058 continue;
12059 abort ();
12060 }
12061
12062 input_section = p->u.indirect.section;
12063 input_bfd = input_section->owner;
12064
12065 /* Combine the gptab entries for this input section one
12066 by one. We know that the input gptab entries are
12067 sorted by ascending -G value. */
eea6121a 12068 size = input_section->size;
b49e97c9
TS
12069 last = 0;
12070 for (gpentry = sizeof (Elf32_External_gptab);
12071 gpentry < size;
12072 gpentry += sizeof (Elf32_External_gptab))
12073 {
12074 Elf32_External_gptab ext_gptab;
12075 Elf32_gptab int_gptab;
12076 unsigned long val;
12077 unsigned long add;
b34976b6 12078 bfd_boolean exact;
b49e97c9
TS
12079 unsigned int look;
12080
12081 if (! (bfd_get_section_contents
9719ad41
RS
12082 (input_bfd, input_section, &ext_gptab, gpentry,
12083 sizeof (Elf32_External_gptab))))
b49e97c9
TS
12084 {
12085 free (tab);
b34976b6 12086 return FALSE;
b49e97c9
TS
12087 }
12088
12089 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12090 &int_gptab);
12091 val = int_gptab.gt_entry.gt_g_value;
12092 add = int_gptab.gt_entry.gt_bytes - last;
12093
b34976b6 12094 exact = FALSE;
b49e97c9
TS
12095 for (look = 1; look < c; look++)
12096 {
12097 if (tab[look].gt_entry.gt_g_value >= val)
12098 tab[look].gt_entry.gt_bytes += add;
12099
12100 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 12101 exact = TRUE;
b49e97c9
TS
12102 }
12103
12104 if (! exact)
12105 {
12106 Elf32_gptab *new_tab;
12107 unsigned int max;
12108
12109 /* We need a new table entry. */
12110 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 12111 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
12112 if (new_tab == NULL)
12113 {
12114 free (tab);
b34976b6 12115 return FALSE;
b49e97c9
TS
12116 }
12117 tab = new_tab;
12118 tab[c].gt_entry.gt_g_value = val;
12119 tab[c].gt_entry.gt_bytes = add;
12120
12121 /* Merge in the size for the next smallest -G
12122 value, since that will be implied by this new
12123 value. */
12124 max = 0;
12125 for (look = 1; look < c; look++)
12126 {
12127 if (tab[look].gt_entry.gt_g_value < val
12128 && (max == 0
12129 || (tab[look].gt_entry.gt_g_value
12130 > tab[max].gt_entry.gt_g_value)))
12131 max = look;
12132 }
12133 if (max != 0)
12134 tab[c].gt_entry.gt_bytes +=
12135 tab[max].gt_entry.gt_bytes;
12136
12137 ++c;
12138 }
12139
12140 last = int_gptab.gt_entry.gt_bytes;
12141 }
12142
12143 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12144 elf_link_input_bfd ignores this section. */
12145 input_section->flags &= ~SEC_HAS_CONTENTS;
12146 }
12147
12148 /* The table must be sorted by -G value. */
12149 if (c > 2)
12150 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12151
12152 /* Swap out the table. */
12153 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 12154 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
12155 if (ext_tab == NULL)
12156 {
12157 free (tab);
b34976b6 12158 return FALSE;
b49e97c9
TS
12159 }
12160
12161 for (j = 0; j < c; j++)
12162 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12163 free (tab);
12164
eea6121a 12165 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
12166 o->contents = (bfd_byte *) ext_tab;
12167
12168 /* Skip this section later on (I don't think this currently
12169 matters, but someday it might). */
8423293d 12170 o->map_head.link_order = NULL;
b49e97c9
TS
12171 }
12172 }
12173
12174 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 12175 if (!bfd_elf_final_link (abfd, info))
b34976b6 12176 return FALSE;
b49e97c9
TS
12177
12178 /* Now write out the computed sections. */
12179
9719ad41 12180 if (reginfo_sec != NULL)
b49e97c9
TS
12181 {
12182 Elf32_External_RegInfo ext;
12183
12184 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 12185 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 12186 return FALSE;
b49e97c9
TS
12187 }
12188
9719ad41 12189 if (mdebug_sec != NULL)
b49e97c9
TS
12190 {
12191 BFD_ASSERT (abfd->output_has_begun);
12192 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12193 swap, info,
12194 mdebug_sec->filepos))
b34976b6 12195 return FALSE;
b49e97c9
TS
12196
12197 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12198 }
12199
9719ad41 12200 if (gptab_data_sec != NULL)
b49e97c9
TS
12201 {
12202 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12203 gptab_data_sec->contents,
eea6121a 12204 0, gptab_data_sec->size))
b34976b6 12205 return FALSE;
b49e97c9
TS
12206 }
12207
9719ad41 12208 if (gptab_bss_sec != NULL)
b49e97c9
TS
12209 {
12210 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12211 gptab_bss_sec->contents,
eea6121a 12212 0, gptab_bss_sec->size))
b34976b6 12213 return FALSE;
b49e97c9
TS
12214 }
12215
12216 if (SGI_COMPAT (abfd))
12217 {
12218 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12219 if (rtproc_sec != NULL)
12220 {
12221 if (! bfd_set_section_contents (abfd, rtproc_sec,
12222 rtproc_sec->contents,
eea6121a 12223 0, rtproc_sec->size))
b34976b6 12224 return FALSE;
b49e97c9
TS
12225 }
12226 }
12227
b34976b6 12228 return TRUE;
b49e97c9
TS
12229}
12230\f
64543e1a
RS
12231/* Structure for saying that BFD machine EXTENSION extends BASE. */
12232
12233struct mips_mach_extension {
12234 unsigned long extension, base;
12235};
12236
12237
12238/* An array describing how BFD machines relate to one another. The entries
12239 are ordered topologically with MIPS I extensions listed last. */
12240
12241static const struct mips_mach_extension mips_mach_extensions[] = {
6f179bd0
AN
12242 /* MIPS64r2 extensions. */
12243 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12244
64543e1a 12245 /* MIPS64 extensions. */
5f74bc13 12246 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 12247 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 12248 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
64543e1a
RS
12249
12250 /* MIPS V extensions. */
12251 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12252
12253 /* R10000 extensions. */
12254 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
12255 { bfd_mach_mips14000, bfd_mach_mips10000 },
12256 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
12257
12258 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12259 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12260 better to allow vr5400 and vr5500 code to be merged anyway, since
12261 many libraries will just use the core ISA. Perhaps we could add
12262 some sort of ASE flag if this ever proves a problem. */
12263 { bfd_mach_mips5500, bfd_mach_mips5400 },
12264 { bfd_mach_mips5400, bfd_mach_mips5000 },
12265
12266 /* MIPS IV extensions. */
12267 { bfd_mach_mips5, bfd_mach_mips8000 },
12268 { bfd_mach_mips10000, bfd_mach_mips8000 },
12269 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 12270 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 12271 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
12272
12273 /* VR4100 extensions. */
12274 { bfd_mach_mips4120, bfd_mach_mips4100 },
12275 { bfd_mach_mips4111, bfd_mach_mips4100 },
12276
12277 /* MIPS III extensions. */
350cc38d
MS
12278 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12279 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
12280 { bfd_mach_mips8000, bfd_mach_mips4000 },
12281 { bfd_mach_mips4650, bfd_mach_mips4000 },
12282 { bfd_mach_mips4600, bfd_mach_mips4000 },
12283 { bfd_mach_mips4400, bfd_mach_mips4000 },
12284 { bfd_mach_mips4300, bfd_mach_mips4000 },
12285 { bfd_mach_mips4100, bfd_mach_mips4000 },
12286 { bfd_mach_mips4010, bfd_mach_mips4000 },
12287
12288 /* MIPS32 extensions. */
12289 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12290
12291 /* MIPS II extensions. */
12292 { bfd_mach_mips4000, bfd_mach_mips6000 },
12293 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12294
12295 /* MIPS I extensions. */
12296 { bfd_mach_mips6000, bfd_mach_mips3000 },
12297 { bfd_mach_mips3900, bfd_mach_mips3000 }
12298};
12299
12300
12301/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12302
12303static bfd_boolean
9719ad41 12304mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
12305{
12306 size_t i;
12307
c5211a54
RS
12308 if (extension == base)
12309 return TRUE;
12310
12311 if (base == bfd_mach_mipsisa32
12312 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12313 return TRUE;
12314
12315 if (base == bfd_mach_mipsisa32r2
12316 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12317 return TRUE;
12318
12319 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 12320 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
12321 {
12322 extension = mips_mach_extensions[i].base;
12323 if (extension == base)
12324 return TRUE;
12325 }
64543e1a 12326
c5211a54 12327 return FALSE;
64543e1a
RS
12328}
12329
12330
12331/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 12332
b34976b6 12333static bfd_boolean
9719ad41 12334mips_32bit_flags_p (flagword flags)
00707a0e 12335{
64543e1a
RS
12336 return ((flags & EF_MIPS_32BITMODE) != 0
12337 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12338 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12339 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12340 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12341 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12342 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
12343}
12344
64543e1a 12345
2cf19d5c
JM
12346/* Merge object attributes from IBFD into OBFD. Raise an error if
12347 there are conflicting attributes. */
12348static bfd_boolean
12349mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12350{
12351 obj_attribute *in_attr;
12352 obj_attribute *out_attr;
12353
12354 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12355 {
12356 /* This is the first object. Copy the attributes. */
12357 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12358
12359 /* Use the Tag_null value to indicate the attributes have been
12360 initialized. */
12361 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12362
12363 return TRUE;
12364 }
12365
12366 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12367 non-conflicting ones. */
12368 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12369 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12370 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12371 {
12372 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12373 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12374 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12375 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12376 ;
42554f6a 12377 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12378 _bfd_error_handler
12379 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12380 in_attr[Tag_GNU_MIPS_ABI_FP].i);
42554f6a 12381 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12382 _bfd_error_handler
12383 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12384 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12385 else
12386 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12387 {
12388 case 1:
12389 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12390 {
12391 case 2:
12392 _bfd_error_handler
12393 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12394 obfd, ibfd);
51a0dd31 12395 break;
2cf19d5c
JM
12396
12397 case 3:
12398 _bfd_error_handler
12399 (_("Warning: %B uses hard float, %B uses soft float"),
12400 obfd, ibfd);
12401 break;
12402
42554f6a
TS
12403 case 4:
12404 _bfd_error_handler
12405 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12406 obfd, ibfd);
12407 break;
12408
2cf19d5c
JM
12409 default:
12410 abort ();
12411 }
12412 break;
12413
12414 case 2:
12415 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12416 {
12417 case 1:
12418 _bfd_error_handler
12419 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12420 ibfd, obfd);
51a0dd31 12421 break;
2cf19d5c
JM
12422
12423 case 3:
12424 _bfd_error_handler
12425 (_("Warning: %B uses hard float, %B uses soft float"),
12426 obfd, ibfd);
12427 break;
12428
42554f6a
TS
12429 case 4:
12430 _bfd_error_handler
12431 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12432 obfd, ibfd);
12433 break;
12434
2cf19d5c
JM
12435 default:
12436 abort ();
12437 }
12438 break;
12439
12440 case 3:
12441 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12442 {
12443 case 1:
12444 case 2:
42554f6a 12445 case 4:
2cf19d5c
JM
12446 _bfd_error_handler
12447 (_("Warning: %B uses hard float, %B uses soft float"),
12448 ibfd, obfd);
12449 break;
12450
12451 default:
12452 abort ();
12453 }
12454 break;
12455
42554f6a
TS
12456 case 4:
12457 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12458 {
12459 case 1:
12460 _bfd_error_handler
12461 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12462 ibfd, obfd);
12463 break;
12464
12465 case 2:
12466 _bfd_error_handler
12467 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12468 ibfd, obfd);
12469 break;
12470
12471 case 3:
12472 _bfd_error_handler
12473 (_("Warning: %B uses hard float, %B uses soft float"),
12474 obfd, ibfd);
12475 break;
12476
12477 default:
12478 abort ();
12479 }
12480 break;
12481
2cf19d5c
JM
12482 default:
12483 abort ();
12484 }
12485 }
12486
12487 /* Merge Tag_compatibility attributes and any common GNU ones. */
12488 _bfd_elf_merge_object_attributes (ibfd, obfd);
12489
12490 return TRUE;
12491}
12492
b49e97c9
TS
12493/* Merge backend specific data from an object file to the output
12494 object file when linking. */
12495
b34976b6 12496bfd_boolean
9719ad41 12497_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
12498{
12499 flagword old_flags;
12500 flagword new_flags;
b34976b6
AM
12501 bfd_boolean ok;
12502 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
12503 asection *sec;
12504
12505 /* Check if we have the same endianess */
82e51918 12506 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
12507 {
12508 (*_bfd_error_handler)
d003868e
AM
12509 (_("%B: endianness incompatible with that of the selected emulation"),
12510 ibfd);
aa701218
AO
12511 return FALSE;
12512 }
b49e97c9 12513
d5eaccd7 12514 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 12515 return TRUE;
b49e97c9 12516
aa701218
AO
12517 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12518 {
12519 (*_bfd_error_handler)
d003868e
AM
12520 (_("%B: ABI is incompatible with that of the selected emulation"),
12521 ibfd);
aa701218
AO
12522 return FALSE;
12523 }
12524
2cf19d5c
JM
12525 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12526 return FALSE;
12527
b49e97c9
TS
12528 new_flags = elf_elfheader (ibfd)->e_flags;
12529 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12530 old_flags = elf_elfheader (obfd)->e_flags;
12531
12532 if (! elf_flags_init (obfd))
12533 {
b34976b6 12534 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
12535 elf_elfheader (obfd)->e_flags = new_flags;
12536 elf_elfheader (obfd)->e_ident[EI_CLASS]
12537 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12538
12539 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861
TS
12540 && (bfd_get_arch_info (obfd)->the_default
12541 || mips_mach_extends_p (bfd_get_mach (obfd),
12542 bfd_get_mach (ibfd))))
b49e97c9
TS
12543 {
12544 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12545 bfd_get_mach (ibfd)))
b34976b6 12546 return FALSE;
b49e97c9
TS
12547 }
12548
b34976b6 12549 return TRUE;
b49e97c9
TS
12550 }
12551
12552 /* Check flag compatibility. */
12553
12554 new_flags &= ~EF_MIPS_NOREORDER;
12555 old_flags &= ~EF_MIPS_NOREORDER;
12556
f4416af6
AO
12557 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12558 doesn't seem to matter. */
12559 new_flags &= ~EF_MIPS_XGOT;
12560 old_flags &= ~EF_MIPS_XGOT;
12561
98a8deaf
RS
12562 /* MIPSpro generates ucode info in n64 objects. Again, we should
12563 just be able to ignore this. */
12564 new_flags &= ~EF_MIPS_UCODE;
12565 old_flags &= ~EF_MIPS_UCODE;
12566
861fb55a
DJ
12567 /* DSOs should only be linked with CPIC code. */
12568 if ((ibfd->flags & DYNAMIC) != 0)
12569 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 12570
b49e97c9 12571 if (new_flags == old_flags)
b34976b6 12572 return TRUE;
b49e97c9
TS
12573
12574 /* Check to see if the input BFD actually contains any sections.
12575 If not, its flags may not have been initialised either, but it cannot
12576 actually cause any incompatibility. */
12577 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12578 {
12579 /* Ignore synthetic sections and empty .text, .data and .bss sections
12580 which are automatically generated by gas. */
12581 if (strcmp (sec->name, ".reginfo")
12582 && strcmp (sec->name, ".mdebug")
eea6121a 12583 && (sec->size != 0
d13d89fa
NS
12584 || (strcmp (sec->name, ".text")
12585 && strcmp (sec->name, ".data")
12586 && strcmp (sec->name, ".bss"))))
b49e97c9 12587 {
b34976b6 12588 null_input_bfd = FALSE;
b49e97c9
TS
12589 break;
12590 }
12591 }
12592 if (null_input_bfd)
b34976b6 12593 return TRUE;
b49e97c9 12594
b34976b6 12595 ok = TRUE;
b49e97c9 12596
143d77c5
EC
12597 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12598 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 12599 {
b49e97c9 12600 (*_bfd_error_handler)
861fb55a 12601 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 12602 ibfd);
143d77c5 12603 ok = TRUE;
b49e97c9
TS
12604 }
12605
143d77c5
EC
12606 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12607 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12608 if (! (new_flags & EF_MIPS_PIC))
12609 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12610
12611 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12612 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 12613
64543e1a
RS
12614 /* Compare the ISAs. */
12615 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 12616 {
64543e1a 12617 (*_bfd_error_handler)
d003868e
AM
12618 (_("%B: linking 32-bit code with 64-bit code"),
12619 ibfd);
64543e1a
RS
12620 ok = FALSE;
12621 }
12622 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12623 {
12624 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12625 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 12626 {
64543e1a
RS
12627 /* Copy the architecture info from IBFD to OBFD. Also copy
12628 the 32-bit flag (if set) so that we continue to recognise
12629 OBFD as a 32-bit binary. */
12630 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12631 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12632 elf_elfheader (obfd)->e_flags
12633 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12634
12635 /* Copy across the ABI flags if OBFD doesn't use them
12636 and if that was what caused us to treat IBFD as 32-bit. */
12637 if ((old_flags & EF_MIPS_ABI) == 0
12638 && mips_32bit_flags_p (new_flags)
12639 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12640 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
12641 }
12642 else
12643 {
64543e1a 12644 /* The ISAs aren't compatible. */
b49e97c9 12645 (*_bfd_error_handler)
d003868e
AM
12646 (_("%B: linking %s module with previous %s modules"),
12647 ibfd,
64543e1a
RS
12648 bfd_printable_name (ibfd),
12649 bfd_printable_name (obfd));
b34976b6 12650 ok = FALSE;
b49e97c9 12651 }
b49e97c9
TS
12652 }
12653
64543e1a
RS
12654 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12655 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12656
12657 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
12658 does set EI_CLASS differently from any 32-bit ABI. */
12659 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12660 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12661 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12662 {
12663 /* Only error if both are set (to different values). */
12664 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12665 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12666 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12667 {
12668 (*_bfd_error_handler)
d003868e
AM
12669 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12670 ibfd,
b49e97c9
TS
12671 elf_mips_abi_name (ibfd),
12672 elf_mips_abi_name (obfd));
b34976b6 12673 ok = FALSE;
b49e97c9
TS
12674 }
12675 new_flags &= ~EF_MIPS_ABI;
12676 old_flags &= ~EF_MIPS_ABI;
12677 }
12678
fb39dac1
RS
12679 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12680 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12681 {
12682 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12683
12684 new_flags &= ~ EF_MIPS_ARCH_ASE;
12685 old_flags &= ~ EF_MIPS_ARCH_ASE;
12686 }
12687
b49e97c9
TS
12688 /* Warn about any other mismatches */
12689 if (new_flags != old_flags)
12690 {
12691 (*_bfd_error_handler)
d003868e
AM
12692 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12693 ibfd, (unsigned long) new_flags,
b49e97c9 12694 (unsigned long) old_flags);
b34976b6 12695 ok = FALSE;
b49e97c9
TS
12696 }
12697
12698 if (! ok)
12699 {
12700 bfd_set_error (bfd_error_bad_value);
b34976b6 12701 return FALSE;
b49e97c9
TS
12702 }
12703
b34976b6 12704 return TRUE;
b49e97c9
TS
12705}
12706
12707/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12708
b34976b6 12709bfd_boolean
9719ad41 12710_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
12711{
12712 BFD_ASSERT (!elf_flags_init (abfd)
12713 || elf_elfheader (abfd)->e_flags == flags);
12714
12715 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
12716 elf_flags_init (abfd) = TRUE;
12717 return TRUE;
b49e97c9
TS
12718}
12719
ad9563d6
CM
12720char *
12721_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12722{
12723 switch (dtag)
12724 {
12725 default: return "";
12726 case DT_MIPS_RLD_VERSION:
12727 return "MIPS_RLD_VERSION";
12728 case DT_MIPS_TIME_STAMP:
12729 return "MIPS_TIME_STAMP";
12730 case DT_MIPS_ICHECKSUM:
12731 return "MIPS_ICHECKSUM";
12732 case DT_MIPS_IVERSION:
12733 return "MIPS_IVERSION";
12734 case DT_MIPS_FLAGS:
12735 return "MIPS_FLAGS";
12736 case DT_MIPS_BASE_ADDRESS:
12737 return "MIPS_BASE_ADDRESS";
12738 case DT_MIPS_MSYM:
12739 return "MIPS_MSYM";
12740 case DT_MIPS_CONFLICT:
12741 return "MIPS_CONFLICT";
12742 case DT_MIPS_LIBLIST:
12743 return "MIPS_LIBLIST";
12744 case DT_MIPS_LOCAL_GOTNO:
12745 return "MIPS_LOCAL_GOTNO";
12746 case DT_MIPS_CONFLICTNO:
12747 return "MIPS_CONFLICTNO";
12748 case DT_MIPS_LIBLISTNO:
12749 return "MIPS_LIBLISTNO";
12750 case DT_MIPS_SYMTABNO:
12751 return "MIPS_SYMTABNO";
12752 case DT_MIPS_UNREFEXTNO:
12753 return "MIPS_UNREFEXTNO";
12754 case DT_MIPS_GOTSYM:
12755 return "MIPS_GOTSYM";
12756 case DT_MIPS_HIPAGENO:
12757 return "MIPS_HIPAGENO";
12758 case DT_MIPS_RLD_MAP:
12759 return "MIPS_RLD_MAP";
12760 case DT_MIPS_DELTA_CLASS:
12761 return "MIPS_DELTA_CLASS";
12762 case DT_MIPS_DELTA_CLASS_NO:
12763 return "MIPS_DELTA_CLASS_NO";
12764 case DT_MIPS_DELTA_INSTANCE:
12765 return "MIPS_DELTA_INSTANCE";
12766 case DT_MIPS_DELTA_INSTANCE_NO:
12767 return "MIPS_DELTA_INSTANCE_NO";
12768 case DT_MIPS_DELTA_RELOC:
12769 return "MIPS_DELTA_RELOC";
12770 case DT_MIPS_DELTA_RELOC_NO:
12771 return "MIPS_DELTA_RELOC_NO";
12772 case DT_MIPS_DELTA_SYM:
12773 return "MIPS_DELTA_SYM";
12774 case DT_MIPS_DELTA_SYM_NO:
12775 return "MIPS_DELTA_SYM_NO";
12776 case DT_MIPS_DELTA_CLASSSYM:
12777 return "MIPS_DELTA_CLASSSYM";
12778 case DT_MIPS_DELTA_CLASSSYM_NO:
12779 return "MIPS_DELTA_CLASSSYM_NO";
12780 case DT_MIPS_CXX_FLAGS:
12781 return "MIPS_CXX_FLAGS";
12782 case DT_MIPS_PIXIE_INIT:
12783 return "MIPS_PIXIE_INIT";
12784 case DT_MIPS_SYMBOL_LIB:
12785 return "MIPS_SYMBOL_LIB";
12786 case DT_MIPS_LOCALPAGE_GOTIDX:
12787 return "MIPS_LOCALPAGE_GOTIDX";
12788 case DT_MIPS_LOCAL_GOTIDX:
12789 return "MIPS_LOCAL_GOTIDX";
12790 case DT_MIPS_HIDDEN_GOTIDX:
12791 return "MIPS_HIDDEN_GOTIDX";
12792 case DT_MIPS_PROTECTED_GOTIDX:
12793 return "MIPS_PROTECTED_GOT_IDX";
12794 case DT_MIPS_OPTIONS:
12795 return "MIPS_OPTIONS";
12796 case DT_MIPS_INTERFACE:
12797 return "MIPS_INTERFACE";
12798 case DT_MIPS_DYNSTR_ALIGN:
12799 return "DT_MIPS_DYNSTR_ALIGN";
12800 case DT_MIPS_INTERFACE_SIZE:
12801 return "DT_MIPS_INTERFACE_SIZE";
12802 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12803 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12804 case DT_MIPS_PERF_SUFFIX:
12805 return "DT_MIPS_PERF_SUFFIX";
12806 case DT_MIPS_COMPACT_SIZE:
12807 return "DT_MIPS_COMPACT_SIZE";
12808 case DT_MIPS_GP_VALUE:
12809 return "DT_MIPS_GP_VALUE";
12810 case DT_MIPS_AUX_DYNAMIC:
12811 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
12812 case DT_MIPS_PLTGOT:
12813 return "DT_MIPS_PLTGOT";
12814 case DT_MIPS_RWPLT:
12815 return "DT_MIPS_RWPLT";
ad9563d6
CM
12816 }
12817}
12818
b34976b6 12819bfd_boolean
9719ad41 12820_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 12821{
9719ad41 12822 FILE *file = ptr;
b49e97c9
TS
12823
12824 BFD_ASSERT (abfd != NULL && ptr != NULL);
12825
12826 /* Print normal ELF private data. */
12827 _bfd_elf_print_private_bfd_data (abfd, ptr);
12828
12829 /* xgettext:c-format */
12830 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12831
12832 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12833 fprintf (file, _(" [abi=O32]"));
12834 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12835 fprintf (file, _(" [abi=O64]"));
12836 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12837 fprintf (file, _(" [abi=EABI32]"));
12838 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12839 fprintf (file, _(" [abi=EABI64]"));
12840 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12841 fprintf (file, _(" [abi unknown]"));
12842 else if (ABI_N32_P (abfd))
12843 fprintf (file, _(" [abi=N32]"));
12844 else if (ABI_64_P (abfd))
12845 fprintf (file, _(" [abi=64]"));
12846 else
12847 fprintf (file, _(" [no abi set]"));
12848
12849 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 12850 fprintf (file, " [mips1]");
b49e97c9 12851 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 12852 fprintf (file, " [mips2]");
b49e97c9 12853 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 12854 fprintf (file, " [mips3]");
b49e97c9 12855 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 12856 fprintf (file, " [mips4]");
b49e97c9 12857 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 12858 fprintf (file, " [mips5]");
b49e97c9 12859 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 12860 fprintf (file, " [mips32]");
b49e97c9 12861 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 12862 fprintf (file, " [mips64]");
af7ee8bf 12863 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 12864 fprintf (file, " [mips32r2]");
5f74bc13 12865 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 12866 fprintf (file, " [mips64r2]");
b49e97c9
TS
12867 else
12868 fprintf (file, _(" [unknown ISA]"));
12869
40d32fc6 12870 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 12871 fprintf (file, " [mdmx]");
40d32fc6
CD
12872
12873 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 12874 fprintf (file, " [mips16]");
40d32fc6 12875
b49e97c9 12876 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 12877 fprintf (file, " [32bitmode]");
b49e97c9
TS
12878 else
12879 fprintf (file, _(" [not 32bitmode]"));
12880
c0e3f241 12881 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 12882 fprintf (file, " [noreorder]");
c0e3f241
CD
12883
12884 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 12885 fprintf (file, " [PIC]");
c0e3f241
CD
12886
12887 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 12888 fprintf (file, " [CPIC]");
c0e3f241
CD
12889
12890 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 12891 fprintf (file, " [XGOT]");
c0e3f241
CD
12892
12893 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 12894 fprintf (file, " [UCODE]");
c0e3f241 12895
b49e97c9
TS
12896 fputc ('\n', file);
12897
b34976b6 12898 return TRUE;
b49e97c9 12899}
2f89ff8d 12900
b35d266b 12901const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 12902{
0112cd26
NC
12903 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12904 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12905 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12906 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12907 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12908 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12909 { NULL, 0, 0, 0, 0 }
2f89ff8d 12910};
5e2b0d47 12911
8992f0d7
TS
12912/* Merge non visibility st_other attributes. Ensure that the
12913 STO_OPTIONAL flag is copied into h->other, even if this is not a
12914 definiton of the symbol. */
5e2b0d47
NC
12915void
12916_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12917 const Elf_Internal_Sym *isym,
12918 bfd_boolean definition,
12919 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12920{
8992f0d7
TS
12921 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12922 {
12923 unsigned char other;
12924
12925 other = (definition ? isym->st_other : h->other);
12926 other &= ~ELF_ST_VISIBILITY (-1);
12927 h->other = other | ELF_ST_VISIBILITY (h->other);
12928 }
12929
12930 if (!definition
5e2b0d47
NC
12931 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12932 h->other |= STO_OPTIONAL;
12933}
12ac1cf5
NC
12934
12935/* Decide whether an undefined symbol is special and can be ignored.
12936 This is the case for OPTIONAL symbols on IRIX. */
12937bfd_boolean
12938_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12939{
12940 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12941}
e0764319
NC
12942
12943bfd_boolean
12944_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12945{
12946 return (sym->st_shndx == SHN_COMMON
12947 || sym->st_shndx == SHN_MIPS_ACOMMON
12948 || sym->st_shndx == SHN_MIPS_SCOMMON);
12949}
861fb55a
DJ
12950
12951/* Return address for Ith PLT stub in section PLT, for relocation REL
12952 or (bfd_vma) -1 if it should not be included. */
12953
12954bfd_vma
12955_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12956 const arelent *rel ATTRIBUTE_UNUSED)
12957{
12958 return (plt->vma
12959 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12960 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12961}
12962
12963void
12964_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12965{
12966 struct mips_elf_link_hash_table *htab;
12967 Elf_Internal_Ehdr *i_ehdrp;
12968
12969 i_ehdrp = elf_elfheader (abfd);
12970 if (link_info)
12971 {
12972 htab = mips_elf_hash_table (link_info);
4dfe6ac6
NC
12973 BFD_ASSERT (htab != NULL);
12974
861fb55a
DJ
12975 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12976 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
12977 }
12978}
This page took 1.34073 seconds and 4 git commands to generate.