2009-12-10 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
64543e1a 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
aa820537 3 2003, 2004, 2005, 2006, 2007, 2008, 2009 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)
56 (3) global and forced-local symbols
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
97 corresponding to a global symbol in the got (or, local, if
98 h->forced_local). */
99 struct mips_elf_link_hash_entry *h;
100 } d;
0f20cc35
DJ
101
102 /* The TLS types included in this GOT entry (specifically, GD and
103 IE). The GD and IE flags can be added as we encounter new
104 relocations. LDM can also be set; it will always be alone, not
105 combined with any GD or IE flags. An LDM GOT entry will be
106 a local symbol entry with r_symndx == 0. */
107 unsigned char tls_type;
108
b15e6682 109 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
110 corresponding to this symbol+addend. If it's a global symbol
111 whose offset is yet to be decided, it's going to be -1. */
112 long gotidx;
b15e6682
AO
113};
114
c224138d
RS
115/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
116 The structures form a non-overlapping list that is sorted by increasing
117 MIN_ADDEND. */
118struct mips_got_page_range
119{
120 struct mips_got_page_range *next;
121 bfd_signed_vma min_addend;
122 bfd_signed_vma max_addend;
123};
124
125/* This structure describes the range of addends that are applied to page
126 relocations against a given symbol. */
127struct mips_got_page_entry
128{
129 /* The input bfd in which the symbol is defined. */
130 bfd *abfd;
131 /* The index of the symbol, as stored in the relocation r_info. */
132 long symndx;
133 /* The ranges for this page entry. */
134 struct mips_got_page_range *ranges;
135 /* The maximum number of page entries needed for RANGES. */
136 bfd_vma num_pages;
137};
138
f0abc2a1 139/* This structure is used to hold .got information when linking. */
b49e97c9
TS
140
141struct mips_got_info
142{
143 /* The global symbol in the GOT with the lowest index in the dynamic
144 symbol table. */
145 struct elf_link_hash_entry *global_gotsym;
146 /* The number of global .got entries. */
147 unsigned int global_gotno;
23cc69b6
RS
148 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
149 unsigned int reloc_only_gotno;
0f20cc35
DJ
150 /* The number of .got slots used for TLS. */
151 unsigned int tls_gotno;
152 /* The first unused TLS .got entry. Used only during
153 mips_elf_initialize_tls_index. */
154 unsigned int tls_assigned_gotno;
c224138d 155 /* The number of local .got entries, eventually including page entries. */
b49e97c9 156 unsigned int local_gotno;
c224138d
RS
157 /* The maximum number of page entries needed. */
158 unsigned int page_gotno;
b49e97c9
TS
159 /* The number of local .got entries we have used. */
160 unsigned int assigned_gotno;
b15e6682
AO
161 /* A hash table holding members of the got. */
162 struct htab *got_entries;
c224138d
RS
163 /* A hash table of mips_got_page_entry structures. */
164 struct htab *got_page_entries;
f4416af6
AO
165 /* A hash table mapping input bfds to other mips_got_info. NULL
166 unless multi-got was necessary. */
167 struct htab *bfd2got;
168 /* In multi-got links, a pointer to the next got (err, rather, most
169 of the time, it points to the previous got). */
170 struct mips_got_info *next;
0f20cc35
DJ
171 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
172 for none, or MINUS_TWO for not yet assigned. This is needed
173 because a single-GOT link may have multiple hash table entries
174 for the LDM. It does not get initialized in multi-GOT mode. */
175 bfd_vma tls_ldm_offset;
f4416af6
AO
176};
177
178/* Map an input bfd to a got in a multi-got link. */
179
180struct mips_elf_bfd2got_hash {
181 bfd *bfd;
182 struct mips_got_info *g;
183};
184
185/* Structure passed when traversing the bfd2got hash table, used to
186 create and merge bfd's gots. */
187
188struct mips_elf_got_per_bfd_arg
189{
190 /* A hashtable that maps bfds to gots. */
191 htab_t bfd2got;
192 /* The output bfd. */
193 bfd *obfd;
194 /* The link information. */
195 struct bfd_link_info *info;
196 /* A pointer to the primary got, i.e., the one that's going to get
197 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198 DT_MIPS_GOTSYM. */
199 struct mips_got_info *primary;
200 /* A non-primary got we're trying to merge with other input bfd's
201 gots. */
202 struct mips_got_info *current;
203 /* The maximum number of got entries that can be addressed with a
204 16-bit offset. */
205 unsigned int max_count;
c224138d
RS
206 /* The maximum number of page entries needed by each got. */
207 unsigned int max_pages;
0f20cc35
DJ
208 /* The total number of global entries which will live in the
209 primary got and be automatically relocated. This includes
210 those not referenced by the primary GOT but included in
211 the "master" GOT. */
212 unsigned int global_count;
f4416af6
AO
213};
214
215/* Another structure used to pass arguments for got entries traversal. */
216
217struct mips_elf_set_global_got_offset_arg
218{
219 struct mips_got_info *g;
220 int value;
221 unsigned int needed_relocs;
222 struct bfd_link_info *info;
b49e97c9
TS
223};
224
0f20cc35
DJ
225/* A structure used to count TLS relocations or GOT entries, for GOT
226 entry or ELF symbol table traversal. */
227
228struct mips_elf_count_tls_arg
229{
230 struct bfd_link_info *info;
231 unsigned int needed;
232};
233
f0abc2a1
AM
234struct _mips_elf_section_data
235{
236 struct bfd_elf_section_data elf;
237 union
238 {
f0abc2a1
AM
239 bfd_byte *tdata;
240 } u;
241};
242
243#define mips_elf_section_data(sec) \
68bfbfcc 244 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 245
d5eaccd7
RS
246#define is_mips_elf(bfd) \
247 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
248 && elf_tdata (bfd) != NULL \
249 && elf_object_id (bfd) == MIPS_ELF_TDATA)
250
634835ae
RS
251/* The ABI says that every symbol used by dynamic relocations must have
252 a global GOT entry. Among other things, this provides the dynamic
253 linker with a free, directly-indexed cache. The GOT can therefore
254 contain symbols that are not referenced by GOT relocations themselves
255 (in other words, it may have symbols that are not referenced by things
256 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
257
258 GOT relocations are less likely to overflow if we put the associated
259 GOT entries towards the beginning. We therefore divide the global
260 GOT entries into two areas: "normal" and "reloc-only". Entries in
261 the first area can be used for both dynamic relocations and GP-relative
262 accesses, while those in the "reloc-only" area are for dynamic
263 relocations only.
264
265 These GGA_* ("Global GOT Area") values are organised so that lower
266 values are more general than higher values. Also, non-GGA_NONE
267 values are ordered by the position of the area in the GOT. */
268#define GGA_NORMAL 0
269#define GGA_RELOC_ONLY 1
270#define GGA_NONE 2
271
861fb55a
DJ
272/* Information about a non-PIC interface to a PIC function. There are
273 two ways of creating these interfaces. The first is to add:
274
275 lui $25,%hi(func)
276 addiu $25,$25,%lo(func)
277
278 immediately before a PIC function "func". The second is to add:
279
280 lui $25,%hi(func)
281 j func
282 addiu $25,$25,%lo(func)
283
284 to a separate trampoline section.
285
286 Stubs of the first kind go in a new section immediately before the
287 target function. Stubs of the second kind go in a single section
288 pointed to by the hash table's "strampoline" field. */
289struct mips_elf_la25_stub {
290 /* The generated section that contains this stub. */
291 asection *stub_section;
292
293 /* The offset of the stub from the start of STUB_SECTION. */
294 bfd_vma offset;
295
296 /* One symbol for the original function. Its location is available
297 in H->root.root.u.def. */
298 struct mips_elf_link_hash_entry *h;
299};
300
301/* Macros for populating a mips_elf_la25_stub. */
302
303#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
304#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
305#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
306
b49e97c9
TS
307/* This structure is passed to mips_elf_sort_hash_table_f when sorting
308 the dynamic symbols. */
309
310struct mips_elf_hash_sort_data
311{
312 /* The symbol in the global GOT with the lowest dynamic symbol table
313 index. */
314 struct elf_link_hash_entry *low;
0f20cc35
DJ
315 /* The least dynamic symbol table index corresponding to a non-TLS
316 symbol with a GOT entry. */
b49e97c9 317 long min_got_dynindx;
f4416af6
AO
318 /* The greatest dynamic symbol table index corresponding to a symbol
319 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 320 with dynamic relocations pointing to it from non-primary GOTs). */
f4416af6 321 long max_unref_got_dynindx;
b49e97c9
TS
322 /* The greatest dynamic symbol table index not corresponding to a
323 symbol without a GOT entry. */
324 long max_non_got_dynindx;
325};
326
327/* The MIPS ELF linker needs additional information for each symbol in
328 the global hash table. */
329
330struct mips_elf_link_hash_entry
331{
332 struct elf_link_hash_entry root;
333
334 /* External symbol information. */
335 EXTR esym;
336
861fb55a
DJ
337 /* The la25 stub we have created for ths symbol, if any. */
338 struct mips_elf_la25_stub *la25_stub;
339
b49e97c9
TS
340 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
341 this symbol. */
342 unsigned int possibly_dynamic_relocs;
343
b49e97c9
TS
344 /* If there is a stub that 32 bit functions should use to call this
345 16 bit function, this points to the section containing the stub. */
346 asection *fn_stub;
347
b49e97c9
TS
348 /* If there is a stub that 16 bit functions should use to call this
349 32 bit function, this points to the section containing the stub. */
350 asection *call_stub;
351
352 /* This is like the call_stub field, but it is used if the function
353 being called returns a floating point value. */
354 asection *call_fp_stub;
7c5fcef7 355
0f20cc35
DJ
356#define GOT_NORMAL 0
357#define GOT_TLS_GD 1
358#define GOT_TLS_LDM 2
359#define GOT_TLS_IE 4
360#define GOT_TLS_OFFSET_DONE 0x40
361#define GOT_TLS_DONE 0x80
362 unsigned char tls_type;
71782a75 363
0f20cc35
DJ
364 /* This is only used in single-GOT mode; in multi-GOT mode there
365 is one mips_got_entry per GOT entry, so the offset is stored
366 there. In single-GOT mode there may be many mips_got_entry
367 structures all referring to the same GOT slot. It might be
368 possible to use root.got.offset instead, but that field is
369 overloaded already. */
370 bfd_vma tls_got_offset;
71782a75 371
634835ae
RS
372 /* The highest GGA_* value that satisfies all references to this symbol. */
373 unsigned int global_got_area : 2;
374
71782a75
RS
375 /* True if one of the relocations described by possibly_dynamic_relocs
376 is against a readonly section. */
377 unsigned int readonly_reloc : 1;
378
861fb55a
DJ
379 /* True if there is a relocation against this symbol that must be
380 resolved by the static linker (in other words, if the relocation
381 cannot possibly be made dynamic). */
382 unsigned int has_static_relocs : 1;
383
71782a75
RS
384 /* True if we must not create a .MIPS.stubs entry for this symbol.
385 This is set, for example, if there are relocations related to
386 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
387 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
388 unsigned int no_fn_stub : 1;
389
390 /* Whether we need the fn_stub; this is true if this symbol appears
391 in any relocs other than a 16 bit call. */
392 unsigned int need_fn_stub : 1;
393
861fb55a
DJ
394 /* True if this symbol is referenced by branch relocations from
395 any non-PIC input file. This is used to determine whether an
396 la25 stub is required. */
397 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
398
399 /* Does this symbol need a traditional MIPS lazy-binding stub
400 (as opposed to a PLT entry)? */
401 unsigned int needs_lazy_stub : 1;
b49e97c9
TS
402};
403
404/* MIPS ELF linker hash table. */
405
406struct mips_elf_link_hash_table
407{
408 struct elf_link_hash_table root;
409#if 0
410 /* We no longer use this. */
411 /* String section indices for the dynamic section symbols. */
412 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
413#endif
861fb55a 414
b49e97c9
TS
415 /* The number of .rtproc entries. */
416 bfd_size_type procedure_count;
861fb55a 417
b49e97c9
TS
418 /* The size of the .compact_rel section (if SGI_COMPAT). */
419 bfd_size_type compact_rel_size;
861fb55a 420
b49e97c9 421 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
8dc1a139 422 entry is set to the address of __rld_obj_head as in IRIX5. */
b34976b6 423 bfd_boolean use_rld_obj_head;
861fb55a 424
b49e97c9
TS
425 /* This is the value of the __rld_map or __rld_obj_head symbol. */
426 bfd_vma rld_value;
861fb55a 427
b49e97c9 428 /* This is set if we see any mips16 stub sections. */
b34976b6 429 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
430
431 /* True if we can generate copy relocs and PLTs. */
432 bfd_boolean use_plts_and_copy_relocs;
433
0a44bf69
RS
434 /* True if we're generating code for VxWorks. */
435 bfd_boolean is_vxworks;
861fb55a 436
0e53d9da
AN
437 /* True if we already reported the small-data section overflow. */
438 bfd_boolean small_data_overflow_reported;
861fb55a 439
0a44bf69
RS
440 /* Shortcuts to some dynamic sections, or NULL if they are not
441 being used. */
442 asection *srelbss;
443 asection *sdynbss;
444 asection *srelplt;
445 asection *srelplt2;
446 asection *sgotplt;
447 asection *splt;
4e41d0d7 448 asection *sstubs;
a8028dd0 449 asection *sgot;
861fb55a 450
a8028dd0
RS
451 /* The master GOT information. */
452 struct mips_got_info *got_info;
861fb55a
DJ
453
454 /* The size of the PLT header in bytes. */
0a44bf69 455 bfd_vma plt_header_size;
861fb55a
DJ
456
457 /* The size of a PLT entry in bytes. */
0a44bf69 458 bfd_vma plt_entry_size;
861fb55a 459
33bb52fb
RS
460 /* The number of functions that need a lazy-binding stub. */
461 bfd_vma lazy_stub_count;
861fb55a 462
5108fc1b
RS
463 /* The size of a function stub entry in bytes. */
464 bfd_vma function_stub_size;
861fb55a
DJ
465
466 /* The number of reserved entries at the beginning of the GOT. */
467 unsigned int reserved_gotno;
468
469 /* The section used for mips_elf_la25_stub trampolines.
470 See the comment above that structure for details. */
471 asection *strampoline;
472
473 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
474 pairs. */
475 htab_t la25_stubs;
476
477 /* A function FN (NAME, IS, OS) that creates a new input section
478 called NAME and links it to output section OS. If IS is nonnull,
479 the new section should go immediately before it, otherwise it
480 should go at the (current) beginning of OS.
481
482 The function returns the new section on success, otherwise it
483 returns null. */
484 asection *(*add_stub_section) (const char *, asection *, asection *);
485};
486
487/* A structure used to communicate with htab_traverse callbacks. */
488struct mips_htab_traverse_info {
489 /* The usual link-wide information. */
490 struct bfd_link_info *info;
491 bfd *output_bfd;
492
493 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
494 bfd_boolean error;
b49e97c9
TS
495};
496
0f20cc35
DJ
497#define TLS_RELOC_P(r_type) \
498 (r_type == R_MIPS_TLS_DTPMOD32 \
499 || r_type == R_MIPS_TLS_DTPMOD64 \
500 || r_type == R_MIPS_TLS_DTPREL32 \
501 || r_type == R_MIPS_TLS_DTPREL64 \
502 || r_type == R_MIPS_TLS_GD \
503 || r_type == R_MIPS_TLS_LDM \
504 || r_type == R_MIPS_TLS_DTPREL_HI16 \
505 || r_type == R_MIPS_TLS_DTPREL_LO16 \
506 || r_type == R_MIPS_TLS_GOTTPREL \
507 || r_type == R_MIPS_TLS_TPREL32 \
508 || r_type == R_MIPS_TLS_TPREL64 \
509 || r_type == R_MIPS_TLS_TPREL_HI16 \
510 || r_type == R_MIPS_TLS_TPREL_LO16)
511
b49e97c9
TS
512/* Structure used to pass information to mips_elf_output_extsym. */
513
514struct extsym_info
515{
9e4aeb93
RS
516 bfd *abfd;
517 struct bfd_link_info *info;
b49e97c9
TS
518 struct ecoff_debug_info *debug;
519 const struct ecoff_debug_swap *swap;
b34976b6 520 bfd_boolean failed;
b49e97c9
TS
521};
522
8dc1a139 523/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
524
525static const char * const mips_elf_dynsym_rtproc_names[] =
526{
527 "_procedure_table",
528 "_procedure_string_table",
529 "_procedure_table_size",
530 NULL
531};
532
533/* These structures are used to generate the .compact_rel section on
8dc1a139 534 IRIX5. */
b49e97c9
TS
535
536typedef struct
537{
538 unsigned long id1; /* Always one? */
539 unsigned long num; /* Number of compact relocation entries. */
540 unsigned long id2; /* Always two? */
541 unsigned long offset; /* The file offset of the first relocation. */
542 unsigned long reserved0; /* Zero? */
543 unsigned long reserved1; /* Zero? */
544} Elf32_compact_rel;
545
546typedef struct
547{
548 bfd_byte id1[4];
549 bfd_byte num[4];
550 bfd_byte id2[4];
551 bfd_byte offset[4];
552 bfd_byte reserved0[4];
553 bfd_byte reserved1[4];
554} Elf32_External_compact_rel;
555
556typedef struct
557{
558 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
559 unsigned int rtype : 4; /* Relocation types. See below. */
560 unsigned int dist2to : 8;
561 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
562 unsigned long konst; /* KONST field. See below. */
563 unsigned long vaddr; /* VADDR to be relocated. */
564} Elf32_crinfo;
565
566typedef struct
567{
568 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
569 unsigned int rtype : 4; /* Relocation types. See below. */
570 unsigned int dist2to : 8;
571 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
572 unsigned long konst; /* KONST field. See below. */
573} Elf32_crinfo2;
574
575typedef struct
576{
577 bfd_byte info[4];
578 bfd_byte konst[4];
579 bfd_byte vaddr[4];
580} Elf32_External_crinfo;
581
582typedef struct
583{
584 bfd_byte info[4];
585 bfd_byte konst[4];
586} Elf32_External_crinfo2;
587
588/* These are the constants used to swap the bitfields in a crinfo. */
589
590#define CRINFO_CTYPE (0x1)
591#define CRINFO_CTYPE_SH (31)
592#define CRINFO_RTYPE (0xf)
593#define CRINFO_RTYPE_SH (27)
594#define CRINFO_DIST2TO (0xff)
595#define CRINFO_DIST2TO_SH (19)
596#define CRINFO_RELVADDR (0x7ffff)
597#define CRINFO_RELVADDR_SH (0)
598
599/* A compact relocation info has long (3 words) or short (2 words)
600 formats. A short format doesn't have VADDR field and relvaddr
601 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
602#define CRF_MIPS_LONG 1
603#define CRF_MIPS_SHORT 0
604
605/* There are 4 types of compact relocation at least. The value KONST
606 has different meaning for each type:
607
608 (type) (konst)
609 CT_MIPS_REL32 Address in data
610 CT_MIPS_WORD Address in word (XXX)
611 CT_MIPS_GPHI_LO GP - vaddr
612 CT_MIPS_JMPAD Address to jump
613 */
614
615#define CRT_MIPS_REL32 0xa
616#define CRT_MIPS_WORD 0xb
617#define CRT_MIPS_GPHI_LO 0xc
618#define CRT_MIPS_JMPAD 0xd
619
620#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
621#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
622#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
623#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
624\f
625/* The structure of the runtime procedure descriptor created by the
626 loader for use by the static exception system. */
627
628typedef struct runtime_pdr {
ae9a127f
NC
629 bfd_vma adr; /* Memory address of start of procedure. */
630 long regmask; /* Save register mask. */
631 long regoffset; /* Save register offset. */
632 long fregmask; /* Save floating point register mask. */
633 long fregoffset; /* Save floating point register offset. */
634 long frameoffset; /* Frame size. */
635 short framereg; /* Frame pointer register. */
636 short pcreg; /* Offset or reg of return pc. */
637 long irpss; /* Index into the runtime string table. */
b49e97c9 638 long reserved;
ae9a127f 639 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
640} RPDR, *pRPDR;
641#define cbRPDR sizeof (RPDR)
642#define rpdNil ((pRPDR) 0)
643\f
b15e6682 644static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
645 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
646 struct mips_elf_link_hash_entry *, int);
b34976b6 647static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 648 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
649static bfd_vma mips_elf_high
650 (bfd_vma);
b34976b6 651static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
652 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
653 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
654 bfd_vma *, asection *);
9719ad41
RS
655static hashval_t mips_elf_got_entry_hash
656 (const void *);
f4416af6 657static bfd_vma mips_elf_adjust_gp
9719ad41 658 (bfd *, struct mips_got_info *, bfd *);
f4416af6 659static struct mips_got_info *mips_elf_got_for_ibfd
9719ad41 660 (struct mips_got_info *, bfd *);
f4416af6 661
b49e97c9
TS
662/* This will be used when we sort the dynamic relocation records. */
663static bfd *reldyn_sorting_bfd;
664
6d30f5b2
NC
665/* True if ABFD is for CPUs with load interlocking that include
666 non-MIPS1 CPUs and R3900. */
667#define LOAD_INTERLOCKS_P(abfd) \
668 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
669 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
670
cd8d5a82
CF
671/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
672 This should be safe for all architectures. We enable this predicate
673 for RM9000 for now. */
674#define JAL_TO_BAL_P(abfd) \
675 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
676
677/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
678 This should be safe for all architectures. We enable this predicate for
679 all CPUs. */
680#define JALR_TO_BAL_P(abfd) 1
681
861fb55a
DJ
682/* True if ABFD is a PIC object. */
683#define PIC_OBJECT_P(abfd) \
684 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
685
b49e97c9 686/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
687#define ABI_N32_P(abfd) \
688 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
689
4a14403c 690/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 691#define ABI_64_P(abfd) \
141ff970 692 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 693
4a14403c
TS
694/* Nonzero if ABFD is using NewABI conventions. */
695#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
696
697/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
698#define IRIX_COMPAT(abfd) \
699 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
700
b49e97c9
TS
701/* Whether we are trying to be compatible with IRIX at all. */
702#define SGI_COMPAT(abfd) \
703 (IRIX_COMPAT (abfd) != ict_none)
704
705/* The name of the options section. */
706#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 707 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 708
cc2e31b9
RS
709/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
710 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
711#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
712 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
713
943284cc
DJ
714/* Whether the section is readonly. */
715#define MIPS_ELF_READONLY_SECTION(sec) \
716 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
717 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
718
b49e97c9 719/* The name of the stub section. */
ca07892d 720#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
721
722/* The size of an external REL relocation. */
723#define MIPS_ELF_REL_SIZE(abfd) \
724 (get_elf_backend_data (abfd)->s->sizeof_rel)
725
0a44bf69
RS
726/* The size of an external RELA relocation. */
727#define MIPS_ELF_RELA_SIZE(abfd) \
728 (get_elf_backend_data (abfd)->s->sizeof_rela)
729
b49e97c9
TS
730/* The size of an external dynamic table entry. */
731#define MIPS_ELF_DYN_SIZE(abfd) \
732 (get_elf_backend_data (abfd)->s->sizeof_dyn)
733
734/* The size of a GOT entry. */
735#define MIPS_ELF_GOT_SIZE(abfd) \
736 (get_elf_backend_data (abfd)->s->arch_size / 8)
737
738/* The size of a symbol-table entry. */
739#define MIPS_ELF_SYM_SIZE(abfd) \
740 (get_elf_backend_data (abfd)->s->sizeof_sym)
741
742/* The default alignment for sections, as a power of two. */
743#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 744 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
745
746/* Get word-sized data. */
747#define MIPS_ELF_GET_WORD(abfd, ptr) \
748 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
749
750/* Put out word-sized data. */
751#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
752 (ABI_64_P (abfd) \
753 ? bfd_put_64 (abfd, val, ptr) \
754 : bfd_put_32 (abfd, val, ptr))
755
861fb55a
DJ
756/* The opcode for word-sized loads (LW or LD). */
757#define MIPS_ELF_LOAD_WORD(abfd) \
758 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
759
b49e97c9 760/* Add a dynamic symbol table-entry. */
9719ad41 761#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 762 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
763
764#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
765 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
766
4ffba85c
AO
767/* Determine whether the internal relocation of index REL_IDX is REL
768 (zero) or RELA (non-zero). The assumption is that, if there are
769 two relocation sections for this section, one of them is REL and
770 the other is RELA. If the index of the relocation we're testing is
771 in range for the first relocation section, check that the external
772 relocation size is that for RELA. It is also assumed that, if
773 rel_idx is not in range for the first section, and this first
774 section contains REL relocs, then the relocation is in the second
775 section, that is RELA. */
776#define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
777 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
778 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
779 > (bfd_vma)(rel_idx)) \
780 == (elf_section_data (sec)->rel_hdr.sh_entsize \
781 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
782 : sizeof (Elf32_External_Rela))))
783
0a44bf69
RS
784/* The name of the dynamic relocation section. */
785#define MIPS_ELF_REL_DYN_NAME(INFO) \
786 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
787
b49e97c9
TS
788/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
789 from smaller values. Start with zero, widen, *then* decrement. */
790#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 791#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 792
51e38d68
RS
793/* The value to write into got[1] for SVR4 targets, to identify it is
794 a GNU object. The dynamic linker can then use got[1] to store the
795 module pointer. */
796#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
797 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
798
f4416af6 799/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
800#define ELF_MIPS_GP_OFFSET(INFO) \
801 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
802
803/* The maximum size of the GOT for it to be addressable using 16-bit
804 offsets from $gp. */
0a44bf69 805#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 806
6a691779 807/* Instructions which appear in a stub. */
3d6746ca
DD
808#define STUB_LW(abfd) \
809 ((ABI_64_P (abfd) \
810 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
811 : 0x8f998010)) /* lw t9,0x8010(gp) */
812#define STUB_MOVE(abfd) \
813 ((ABI_64_P (abfd) \
814 ? 0x03e0782d /* daddu t7,ra */ \
815 : 0x03e07821)) /* addu t7,ra */
816#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
817#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
818#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
819#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
820#define STUB_LI16S(abfd, VAL) \
821 ((ABI_64_P (abfd) \
822 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
823 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
824
5108fc1b
RS
825#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
826#define MIPS_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
827
828/* The name of the dynamic interpreter. This is put in the .interp
829 section. */
830
831#define ELF_DYNAMIC_INTERPRETER(abfd) \
832 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
833 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
834 : "/usr/lib/libc.so.1")
835
836#ifdef BFD64
ee6423ed
AO
837#define MNAME(bfd,pre,pos) \
838 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
839#define ELF_R_SYM(bfd, i) \
840 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
841#define ELF_R_TYPE(bfd, i) \
842 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
843#define ELF_R_INFO(bfd, s, t) \
844 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
845#else
ee6423ed 846#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
847#define ELF_R_SYM(bfd, i) \
848 (ELF32_R_SYM (i))
849#define ELF_R_TYPE(bfd, i) \
850 (ELF32_R_TYPE (i))
851#define ELF_R_INFO(bfd, s, t) \
852 (ELF32_R_INFO (s, t))
853#endif
854\f
855 /* The mips16 compiler uses a couple of special sections to handle
856 floating point arguments.
857
858 Section names that look like .mips16.fn.FNNAME contain stubs that
859 copy floating point arguments from the fp regs to the gp regs and
860 then jump to FNNAME. If any 32 bit function calls FNNAME, the
861 call should be redirected to the stub instead. If no 32 bit
862 function calls FNNAME, the stub should be discarded. We need to
863 consider any reference to the function, not just a call, because
864 if the address of the function is taken we will need the stub,
865 since the address might be passed to a 32 bit function.
866
867 Section names that look like .mips16.call.FNNAME contain stubs
868 that copy floating point arguments from the gp regs to the fp
869 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
870 then any 16 bit function that calls FNNAME should be redirected
871 to the stub instead. If FNNAME is not a 32 bit function, the
872 stub should be discarded.
873
874 .mips16.call.fp.FNNAME sections are similar, but contain stubs
875 which call FNNAME and then copy the return value from the fp regs
876 to the gp regs. These stubs store the return value in $18 while
877 calling FNNAME; any function which might call one of these stubs
878 must arrange to save $18 around the call. (This case is not
879 needed for 32 bit functions that call 16 bit functions, because
880 16 bit functions always return floating point values in both
881 $f0/$f1 and $2/$3.)
882
883 Note that in all cases FNNAME might be defined statically.
884 Therefore, FNNAME is not used literally. Instead, the relocation
885 information will indicate which symbol the section is for.
886
887 We record any stubs that we find in the symbol table. */
888
889#define FN_STUB ".mips16.fn."
890#define CALL_STUB ".mips16.call."
891#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
892
893#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
894#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
895#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 896\f
861fb55a 897/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
898static const bfd_vma mips_o32_exec_plt0_entry[] =
899{
861fb55a
DJ
900 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
901 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
902 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
903 0x031cc023, /* subu $24, $24, $28 */
904 0x03e07821, /* move $15, $31 */
905 0x0018c082, /* srl $24, $24, 2 */
906 0x0320f809, /* jalr $25 */
907 0x2718fffe /* subu $24, $24, 2 */
908};
909
910/* The format of the first PLT entry in an N32 executable. Different
911 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
912static const bfd_vma mips_n32_exec_plt0_entry[] =
913{
861fb55a
DJ
914 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
915 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
916 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
917 0x030ec023, /* subu $24, $24, $14 */
918 0x03e07821, /* move $15, $31 */
919 0x0018c082, /* srl $24, $24, 2 */
920 0x0320f809, /* jalr $25 */
921 0x2718fffe /* subu $24, $24, 2 */
922};
923
924/* The format of the first PLT entry in an N64 executable. Different
925 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
926static const bfd_vma mips_n64_exec_plt0_entry[] =
927{
861fb55a
DJ
928 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
929 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
930 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
931 0x030ec023, /* subu $24, $24, $14 */
932 0x03e07821, /* move $15, $31 */
933 0x0018c0c2, /* srl $24, $24, 3 */
934 0x0320f809, /* jalr $25 */
935 0x2718fffe /* subu $24, $24, 2 */
936};
937
938/* The format of subsequent PLT entries. */
6d30f5b2
NC
939static const bfd_vma mips_exec_plt_entry[] =
940{
861fb55a
DJ
941 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
942 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
943 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
944 0x03200008 /* jr $25 */
945};
946
0a44bf69 947/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
948static const bfd_vma mips_vxworks_exec_plt0_entry[] =
949{
0a44bf69
RS
950 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
951 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
952 0x8f390008, /* lw t9, 8(t9) */
953 0x00000000, /* nop */
954 0x03200008, /* jr t9 */
955 0x00000000 /* nop */
956};
957
958/* The format of subsequent PLT entries. */
6d30f5b2
NC
959static const bfd_vma mips_vxworks_exec_plt_entry[] =
960{
0a44bf69
RS
961 0x10000000, /* b .PLT_resolver */
962 0x24180000, /* li t8, <pltindex> */
963 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
964 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
965 0x8f390000, /* lw t9, 0(t9) */
966 0x00000000, /* nop */
967 0x03200008, /* jr t9 */
968 0x00000000 /* nop */
969};
970
971/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
972static const bfd_vma mips_vxworks_shared_plt0_entry[] =
973{
0a44bf69
RS
974 0x8f990008, /* lw t9, 8(gp) */
975 0x00000000, /* nop */
976 0x03200008, /* jr t9 */
977 0x00000000, /* nop */
978 0x00000000, /* nop */
979 0x00000000 /* nop */
980};
981
982/* The format of subsequent PLT entries. */
6d30f5b2
NC
983static const bfd_vma mips_vxworks_shared_plt_entry[] =
984{
0a44bf69
RS
985 0x10000000, /* b .PLT_resolver */
986 0x24180000 /* li t8, <pltindex> */
987};
988\f
b49e97c9
TS
989/* Look up an entry in a MIPS ELF linker hash table. */
990
991#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
992 ((struct mips_elf_link_hash_entry *) \
993 elf_link_hash_lookup (&(table)->root, (string), (create), \
994 (copy), (follow)))
995
996/* Traverse a MIPS ELF linker hash table. */
997
998#define mips_elf_link_hash_traverse(table, func, info) \
999 (elf_link_hash_traverse \
1000 (&(table)->root, \
9719ad41 1001 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1002 (info)))
1003
1004/* Get the MIPS ELF linker hash table from a link_info structure. */
1005
1006#define mips_elf_hash_table(p) \
1007 ((struct mips_elf_link_hash_table *) ((p)->hash))
1008
0f20cc35
DJ
1009/* Find the base offsets for thread-local storage in this object,
1010 for GD/LD and IE/LE respectively. */
1011
1012#define TP_OFFSET 0x7000
1013#define DTP_OFFSET 0x8000
1014
1015static bfd_vma
1016dtprel_base (struct bfd_link_info *info)
1017{
1018 /* If tls_sec is NULL, we should have signalled an error already. */
1019 if (elf_hash_table (info)->tls_sec == NULL)
1020 return 0;
1021 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1022}
1023
1024static bfd_vma
1025tprel_base (struct bfd_link_info *info)
1026{
1027 /* If tls_sec is NULL, we should have signalled an error already. */
1028 if (elf_hash_table (info)->tls_sec == NULL)
1029 return 0;
1030 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1031}
1032
b49e97c9
TS
1033/* Create an entry in a MIPS ELF linker hash table. */
1034
1035static struct bfd_hash_entry *
9719ad41
RS
1036mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1037 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1038{
1039 struct mips_elf_link_hash_entry *ret =
1040 (struct mips_elf_link_hash_entry *) entry;
1041
1042 /* Allocate the structure if it has not already been allocated by a
1043 subclass. */
9719ad41
RS
1044 if (ret == NULL)
1045 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1046 if (ret == NULL)
b49e97c9
TS
1047 return (struct bfd_hash_entry *) ret;
1048
1049 /* Call the allocation method of the superclass. */
1050 ret = ((struct mips_elf_link_hash_entry *)
1051 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1052 table, string));
9719ad41 1053 if (ret != NULL)
b49e97c9
TS
1054 {
1055 /* Set local fields. */
1056 memset (&ret->esym, 0, sizeof (EXTR));
1057 /* We use -2 as a marker to indicate that the information has
1058 not been set. -1 means there is no associated ifd. */
1059 ret->esym.ifd = -2;
861fb55a 1060 ret->la25_stub = 0;
b49e97c9 1061 ret->possibly_dynamic_relocs = 0;
b49e97c9 1062 ret->fn_stub = NULL;
b49e97c9
TS
1063 ret->call_stub = NULL;
1064 ret->call_fp_stub = NULL;
71782a75 1065 ret->tls_type = GOT_NORMAL;
634835ae 1066 ret->global_got_area = GGA_NONE;
71782a75 1067 ret->readonly_reloc = FALSE;
861fb55a 1068 ret->has_static_relocs = FALSE;
71782a75
RS
1069 ret->no_fn_stub = FALSE;
1070 ret->need_fn_stub = FALSE;
861fb55a 1071 ret->has_nonpic_branches = FALSE;
33bb52fb 1072 ret->needs_lazy_stub = FALSE;
b49e97c9
TS
1073 }
1074
1075 return (struct bfd_hash_entry *) ret;
1076}
f0abc2a1
AM
1077
1078bfd_boolean
9719ad41 1079_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1080{
f592407e
AM
1081 if (!sec->used_by_bfd)
1082 {
1083 struct _mips_elf_section_data *sdata;
1084 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1085
f592407e
AM
1086 sdata = bfd_zalloc (abfd, amt);
1087 if (sdata == NULL)
1088 return FALSE;
1089 sec->used_by_bfd = sdata;
1090 }
f0abc2a1
AM
1091
1092 return _bfd_elf_new_section_hook (abfd, sec);
1093}
b49e97c9
TS
1094\f
1095/* Read ECOFF debugging information from a .mdebug section into a
1096 ecoff_debug_info structure. */
1097
b34976b6 1098bfd_boolean
9719ad41
RS
1099_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1100 struct ecoff_debug_info *debug)
b49e97c9
TS
1101{
1102 HDRR *symhdr;
1103 const struct ecoff_debug_swap *swap;
9719ad41 1104 char *ext_hdr;
b49e97c9
TS
1105
1106 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1107 memset (debug, 0, sizeof (*debug));
1108
9719ad41 1109 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1110 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1111 goto error_return;
1112
9719ad41 1113 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1114 swap->external_hdr_size))
b49e97c9
TS
1115 goto error_return;
1116
1117 symhdr = &debug->symbolic_header;
1118 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1119
1120 /* The symbolic header contains absolute file offsets and sizes to
1121 read. */
1122#define READ(ptr, offset, count, size, type) \
1123 if (symhdr->count == 0) \
1124 debug->ptr = NULL; \
1125 else \
1126 { \
1127 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1128 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1129 if (debug->ptr == NULL) \
1130 goto error_return; \
9719ad41 1131 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1132 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1133 goto error_return; \
1134 }
1135
1136 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1137 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1138 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1139 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1140 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1141 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1142 union aux_ext *);
1143 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1144 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1145 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1146 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1147 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1148#undef READ
1149
1150 debug->fdr = NULL;
b49e97c9 1151
b34976b6 1152 return TRUE;
b49e97c9
TS
1153
1154 error_return:
1155 if (ext_hdr != NULL)
1156 free (ext_hdr);
1157 if (debug->line != NULL)
1158 free (debug->line);
1159 if (debug->external_dnr != NULL)
1160 free (debug->external_dnr);
1161 if (debug->external_pdr != NULL)
1162 free (debug->external_pdr);
1163 if (debug->external_sym != NULL)
1164 free (debug->external_sym);
1165 if (debug->external_opt != NULL)
1166 free (debug->external_opt);
1167 if (debug->external_aux != NULL)
1168 free (debug->external_aux);
1169 if (debug->ss != NULL)
1170 free (debug->ss);
1171 if (debug->ssext != NULL)
1172 free (debug->ssext);
1173 if (debug->external_fdr != NULL)
1174 free (debug->external_fdr);
1175 if (debug->external_rfd != NULL)
1176 free (debug->external_rfd);
1177 if (debug->external_ext != NULL)
1178 free (debug->external_ext);
b34976b6 1179 return FALSE;
b49e97c9
TS
1180}
1181\f
1182/* Swap RPDR (runtime procedure table entry) for output. */
1183
1184static void
9719ad41 1185ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1186{
1187 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1188 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1189 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1190 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1191 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1192 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1193
1194 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1195 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1196
1197 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1198}
1199
1200/* Create a runtime procedure table from the .mdebug section. */
1201
b34976b6 1202static bfd_boolean
9719ad41
RS
1203mips_elf_create_procedure_table (void *handle, bfd *abfd,
1204 struct bfd_link_info *info, asection *s,
1205 struct ecoff_debug_info *debug)
b49e97c9
TS
1206{
1207 const struct ecoff_debug_swap *swap;
1208 HDRR *hdr = &debug->symbolic_header;
1209 RPDR *rpdr, *rp;
1210 struct rpdr_ext *erp;
9719ad41 1211 void *rtproc;
b49e97c9
TS
1212 struct pdr_ext *epdr;
1213 struct sym_ext *esym;
1214 char *ss, **sv;
1215 char *str;
1216 bfd_size_type size;
1217 bfd_size_type count;
1218 unsigned long sindex;
1219 unsigned long i;
1220 PDR pdr;
1221 SYMR sym;
1222 const char *no_name_func = _("static procedure (no name)");
1223
1224 epdr = NULL;
1225 rpdr = NULL;
1226 esym = NULL;
1227 ss = NULL;
1228 sv = NULL;
1229
1230 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1231
1232 sindex = strlen (no_name_func) + 1;
1233 count = hdr->ipdMax;
1234 if (count > 0)
1235 {
1236 size = swap->external_pdr_size;
1237
9719ad41 1238 epdr = bfd_malloc (size * count);
b49e97c9
TS
1239 if (epdr == NULL)
1240 goto error_return;
1241
9719ad41 1242 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1243 goto error_return;
1244
1245 size = sizeof (RPDR);
9719ad41 1246 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1247 if (rpdr == NULL)
1248 goto error_return;
1249
1250 size = sizeof (char *);
9719ad41 1251 sv = bfd_malloc (size * count);
b49e97c9
TS
1252 if (sv == NULL)
1253 goto error_return;
1254
1255 count = hdr->isymMax;
1256 size = swap->external_sym_size;
9719ad41 1257 esym = bfd_malloc (size * count);
b49e97c9
TS
1258 if (esym == NULL)
1259 goto error_return;
1260
9719ad41 1261 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1262 goto error_return;
1263
1264 count = hdr->issMax;
9719ad41 1265 ss = bfd_malloc (count);
b49e97c9
TS
1266 if (ss == NULL)
1267 goto error_return;
f075ee0c 1268 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1269 goto error_return;
1270
1271 count = hdr->ipdMax;
1272 for (i = 0; i < (unsigned long) count; i++, rp++)
1273 {
9719ad41
RS
1274 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1275 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1276 rp->adr = sym.value;
1277 rp->regmask = pdr.regmask;
1278 rp->regoffset = pdr.regoffset;
1279 rp->fregmask = pdr.fregmask;
1280 rp->fregoffset = pdr.fregoffset;
1281 rp->frameoffset = pdr.frameoffset;
1282 rp->framereg = pdr.framereg;
1283 rp->pcreg = pdr.pcreg;
1284 rp->irpss = sindex;
1285 sv[i] = ss + sym.iss;
1286 sindex += strlen (sv[i]) + 1;
1287 }
1288 }
1289
1290 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1291 size = BFD_ALIGN (size, 16);
9719ad41 1292 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1293 if (rtproc == NULL)
1294 {
1295 mips_elf_hash_table (info)->procedure_count = 0;
1296 goto error_return;
1297 }
1298
1299 mips_elf_hash_table (info)->procedure_count = count + 2;
1300
9719ad41 1301 erp = rtproc;
b49e97c9
TS
1302 memset (erp, 0, sizeof (struct rpdr_ext));
1303 erp++;
1304 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1305 strcpy (str, no_name_func);
1306 str += strlen (no_name_func) + 1;
1307 for (i = 0; i < count; i++)
1308 {
1309 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1310 strcpy (str, sv[i]);
1311 str += strlen (sv[i]) + 1;
1312 }
1313 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1314
1315 /* Set the size and contents of .rtproc section. */
eea6121a 1316 s->size = size;
9719ad41 1317 s->contents = rtproc;
b49e97c9
TS
1318
1319 /* Skip this section later on (I don't think this currently
1320 matters, but someday it might). */
8423293d 1321 s->map_head.link_order = NULL;
b49e97c9
TS
1322
1323 if (epdr != NULL)
1324 free (epdr);
1325 if (rpdr != NULL)
1326 free (rpdr);
1327 if (esym != NULL)
1328 free (esym);
1329 if (ss != NULL)
1330 free (ss);
1331 if (sv != NULL)
1332 free (sv);
1333
b34976b6 1334 return TRUE;
b49e97c9
TS
1335
1336 error_return:
1337 if (epdr != NULL)
1338 free (epdr);
1339 if (rpdr != NULL)
1340 free (rpdr);
1341 if (esym != NULL)
1342 free (esym);
1343 if (ss != NULL)
1344 free (ss);
1345 if (sv != NULL)
1346 free (sv);
b34976b6 1347 return FALSE;
b49e97c9 1348}
738e5348 1349\f
861fb55a
DJ
1350/* We're going to create a stub for H. Create a symbol for the stub's
1351 value and size, to help make the disassembly easier to read. */
1352
1353static bfd_boolean
1354mips_elf_create_stub_symbol (struct bfd_link_info *info,
1355 struct mips_elf_link_hash_entry *h,
1356 const char *prefix, asection *s, bfd_vma value,
1357 bfd_vma size)
1358{
1359 struct bfd_link_hash_entry *bh;
1360 struct elf_link_hash_entry *elfh;
1361 const char *name;
1362
1363 /* Create a new symbol. */
1364 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1365 bh = NULL;
1366 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1367 BSF_LOCAL, s, value, NULL,
1368 TRUE, FALSE, &bh))
1369 return FALSE;
1370
1371 /* Make it a local function. */
1372 elfh = (struct elf_link_hash_entry *) bh;
1373 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1374 elfh->size = size;
1375 elfh->forced_local = 1;
1376 return TRUE;
1377}
1378
738e5348
RS
1379/* We're about to redefine H. Create a symbol to represent H's
1380 current value and size, to help make the disassembly easier
1381 to read. */
1382
1383static bfd_boolean
1384mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1385 struct mips_elf_link_hash_entry *h,
1386 const char *prefix)
1387{
1388 struct bfd_link_hash_entry *bh;
1389 struct elf_link_hash_entry *elfh;
1390 const char *name;
1391 asection *s;
1392 bfd_vma value;
1393
1394 /* Read the symbol's value. */
1395 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1396 || h->root.root.type == bfd_link_hash_defweak);
1397 s = h->root.root.u.def.section;
1398 value = h->root.root.u.def.value;
1399
1400 /* Create a new symbol. */
1401 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1402 bh = NULL;
1403 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1404 BSF_LOCAL, s, value, NULL,
1405 TRUE, FALSE, &bh))
1406 return FALSE;
1407
1408 /* Make it local and copy the other attributes from H. */
1409 elfh = (struct elf_link_hash_entry *) bh;
1410 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1411 elfh->other = h->root.other;
1412 elfh->size = h->root.size;
1413 elfh->forced_local = 1;
1414 return TRUE;
1415}
1416
1417/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1418 function rather than to a hard-float stub. */
1419
1420static bfd_boolean
1421section_allows_mips16_refs_p (asection *section)
1422{
1423 const char *name;
1424
1425 name = bfd_get_section_name (section->owner, section);
1426 return (FN_STUB_P (name)
1427 || CALL_STUB_P (name)
1428 || CALL_FP_STUB_P (name)
1429 || strcmp (name, ".pdr") == 0);
1430}
1431
1432/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1433 stub section of some kind. Return the R_SYMNDX of the target
1434 function, or 0 if we can't decide which function that is. */
1435
1436static unsigned long
502e814e
TT
1437mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1438 const Elf_Internal_Rela *relocs,
738e5348
RS
1439 const Elf_Internal_Rela *relend)
1440{
1441 const Elf_Internal_Rela *rel;
1442
1443 /* Trust the first R_MIPS_NONE relocation, if any. */
1444 for (rel = relocs; rel < relend; rel++)
1445 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1446 return ELF_R_SYM (sec->owner, rel->r_info);
1447
1448 /* Otherwise trust the first relocation, whatever its kind. This is
1449 the traditional behavior. */
1450 if (relocs < relend)
1451 return ELF_R_SYM (sec->owner, relocs->r_info);
1452
1453 return 0;
1454}
b49e97c9
TS
1455
1456/* Check the mips16 stubs for a particular symbol, and see if we can
1457 discard them. */
1458
861fb55a
DJ
1459static void
1460mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1461 struct mips_elf_link_hash_entry *h)
b49e97c9 1462{
738e5348
RS
1463 /* Dynamic symbols must use the standard call interface, in case other
1464 objects try to call them. */
1465 if (h->fn_stub != NULL
1466 && h->root.dynindx != -1)
1467 {
1468 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1469 h->need_fn_stub = TRUE;
1470 }
1471
b49e97c9
TS
1472 if (h->fn_stub != NULL
1473 && ! h->need_fn_stub)
1474 {
1475 /* We don't need the fn_stub; the only references to this symbol
1476 are 16 bit calls. Clobber the size to 0 to prevent it from
1477 being included in the link. */
eea6121a 1478 h->fn_stub->size = 0;
b49e97c9
TS
1479 h->fn_stub->flags &= ~SEC_RELOC;
1480 h->fn_stub->reloc_count = 0;
1481 h->fn_stub->flags |= SEC_EXCLUDE;
1482 }
1483
1484 if (h->call_stub != NULL
30c09090 1485 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1486 {
1487 /* We don't need the call_stub; this is a 16 bit function, so
1488 calls from other 16 bit functions are OK. Clobber the size
1489 to 0 to prevent it from being included in the link. */
eea6121a 1490 h->call_stub->size = 0;
b49e97c9
TS
1491 h->call_stub->flags &= ~SEC_RELOC;
1492 h->call_stub->reloc_count = 0;
1493 h->call_stub->flags |= SEC_EXCLUDE;
1494 }
1495
1496 if (h->call_fp_stub != NULL
30c09090 1497 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1498 {
1499 /* We don't need the call_stub; this is a 16 bit function, so
1500 calls from other 16 bit functions are OK. Clobber the size
1501 to 0 to prevent it from being included in the link. */
eea6121a 1502 h->call_fp_stub->size = 0;
b49e97c9
TS
1503 h->call_fp_stub->flags &= ~SEC_RELOC;
1504 h->call_fp_stub->reloc_count = 0;
1505 h->call_fp_stub->flags |= SEC_EXCLUDE;
1506 }
861fb55a
DJ
1507}
1508
1509/* Hashtable callbacks for mips_elf_la25_stubs. */
1510
1511static hashval_t
1512mips_elf_la25_stub_hash (const void *entry_)
1513{
1514 const struct mips_elf_la25_stub *entry;
1515
1516 entry = (struct mips_elf_la25_stub *) entry_;
1517 return entry->h->root.root.u.def.section->id
1518 + entry->h->root.root.u.def.value;
1519}
1520
1521static int
1522mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1523{
1524 const struct mips_elf_la25_stub *entry1, *entry2;
1525
1526 entry1 = (struct mips_elf_la25_stub *) entry1_;
1527 entry2 = (struct mips_elf_la25_stub *) entry2_;
1528 return ((entry1->h->root.root.u.def.section
1529 == entry2->h->root.root.u.def.section)
1530 && (entry1->h->root.root.u.def.value
1531 == entry2->h->root.root.u.def.value));
1532}
1533
1534/* Called by the linker to set up the la25 stub-creation code. FN is
1535 the linker's implementation of add_stub_function. Return true on
1536 success. */
1537
1538bfd_boolean
1539_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1540 asection *(*fn) (const char *, asection *,
1541 asection *))
1542{
1543 struct mips_elf_link_hash_table *htab;
1544
1545 htab = mips_elf_hash_table (info);
1546 htab->add_stub_section = fn;
1547 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1548 mips_elf_la25_stub_eq, NULL);
1549 if (htab->la25_stubs == NULL)
1550 return FALSE;
1551
1552 return TRUE;
1553}
1554
1555/* Return true if H is a locally-defined PIC function, in the sense
1556 that it might need $25 to be valid on entry. Note that MIPS16
1557 functions never need $25 to be valid on entry; they set up $gp
1558 using PC-relative instructions instead. */
1559
1560static bfd_boolean
1561mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1562{
1563 return ((h->root.root.type == bfd_link_hash_defined
1564 || h->root.root.type == bfd_link_hash_defweak)
1565 && h->root.def_regular
1566 && !bfd_is_abs_section (h->root.root.u.def.section)
1567 && !ELF_ST_IS_MIPS16 (h->root.other)
1568 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1569 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1570}
1571
1572/* STUB describes an la25 stub that we have decided to implement
1573 by inserting an LUI/ADDIU pair before the target function.
1574 Create the section and redirect the function symbol to it. */
1575
1576static bfd_boolean
1577mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1578 struct bfd_link_info *info)
1579{
1580 struct mips_elf_link_hash_table *htab;
1581 char *name;
1582 asection *s, *input_section;
1583 unsigned int align;
1584
1585 htab = mips_elf_hash_table (info);
1586
1587 /* Create a unique name for the new section. */
1588 name = bfd_malloc (11 + sizeof (".text.stub."));
1589 if (name == NULL)
1590 return FALSE;
1591 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1592
1593 /* Create the section. */
1594 input_section = stub->h->root.root.u.def.section;
1595 s = htab->add_stub_section (name, input_section,
1596 input_section->output_section);
1597 if (s == NULL)
1598 return FALSE;
1599
1600 /* Make sure that any padding goes before the stub. */
1601 align = input_section->alignment_power;
1602 if (!bfd_set_section_alignment (s->owner, s, align))
1603 return FALSE;
1604 if (align > 3)
1605 s->size = (1 << align) - 8;
1606
1607 /* Create a symbol for the stub. */
1608 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1609 stub->stub_section = s;
1610 stub->offset = s->size;
1611
1612 /* Allocate room for it. */
1613 s->size += 8;
1614 return TRUE;
1615}
1616
1617/* STUB describes an la25 stub that we have decided to implement
1618 with a separate trampoline. Allocate room for it and redirect
1619 the function symbol to it. */
1620
1621static bfd_boolean
1622mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1623 struct bfd_link_info *info)
1624{
1625 struct mips_elf_link_hash_table *htab;
1626 asection *s;
1627
1628 htab = mips_elf_hash_table (info);
1629
1630 /* Create a trampoline section, if we haven't already. */
1631 s = htab->strampoline;
1632 if (s == NULL)
1633 {
1634 asection *input_section = stub->h->root.root.u.def.section;
1635 s = htab->add_stub_section (".text", NULL,
1636 input_section->output_section);
1637 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1638 return FALSE;
1639 htab->strampoline = s;
1640 }
1641
1642 /* Create a symbol for the stub. */
1643 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1644 stub->stub_section = s;
1645 stub->offset = s->size;
1646
1647 /* Allocate room for it. */
1648 s->size += 16;
1649 return TRUE;
1650}
1651
1652/* H describes a symbol that needs an la25 stub. Make sure that an
1653 appropriate stub exists and point H at it. */
1654
1655static bfd_boolean
1656mips_elf_add_la25_stub (struct bfd_link_info *info,
1657 struct mips_elf_link_hash_entry *h)
1658{
1659 struct mips_elf_link_hash_table *htab;
1660 struct mips_elf_la25_stub search, *stub;
1661 bfd_boolean use_trampoline_p;
1662 asection *s;
1663 bfd_vma value;
1664 void **slot;
1665
1666 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1667 of the section and if we would need no more than 2 nops. */
1668 s = h->root.root.u.def.section;
1669 value = h->root.root.u.def.value;
1670 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1671
1672 /* Describe the stub we want. */
1673 search.stub_section = NULL;
1674 search.offset = 0;
1675 search.h = h;
1676
1677 /* See if we've already created an equivalent stub. */
1678 htab = mips_elf_hash_table (info);
1679 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1680 if (slot == NULL)
1681 return FALSE;
1682
1683 stub = (struct mips_elf_la25_stub *) *slot;
1684 if (stub != NULL)
1685 {
1686 /* We can reuse the existing stub. */
1687 h->la25_stub = stub;
1688 return TRUE;
1689 }
1690
1691 /* Create a permanent copy of ENTRY and add it to the hash table. */
1692 stub = bfd_malloc (sizeof (search));
1693 if (stub == NULL)
1694 return FALSE;
1695 *stub = search;
1696 *slot = stub;
1697
1698 h->la25_stub = stub;
1699 return (use_trampoline_p
1700 ? mips_elf_add_la25_trampoline (stub, info)
1701 : mips_elf_add_la25_intro (stub, info));
1702}
1703
1704/* A mips_elf_link_hash_traverse callback that is called before sizing
1705 sections. DATA points to a mips_htab_traverse_info structure. */
1706
1707static bfd_boolean
1708mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1709{
1710 struct mips_htab_traverse_info *hti;
1711
1712 hti = (struct mips_htab_traverse_info *) data;
1713 if (h->root.root.type == bfd_link_hash_warning)
1714 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1715
1716 if (!hti->info->relocatable)
1717 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1718
861fb55a
DJ
1719 if (mips_elf_local_pic_function_p (h))
1720 {
1721 /* H is a function that might need $25 to be valid on entry.
1722 If we're creating a non-PIC relocatable object, mark H as
1723 being PIC. If we're creating a non-relocatable object with
1724 non-PIC branches and jumps to H, make sure that H has an la25
1725 stub. */
1726 if (hti->info->relocatable)
1727 {
1728 if (!PIC_OBJECT_P (hti->output_bfd))
1729 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1730 }
1731 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1732 {
1733 hti->error = TRUE;
1734 return FALSE;
1735 }
1736 }
b34976b6 1737 return TRUE;
b49e97c9
TS
1738}
1739\f
d6f16593
MR
1740/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1741 Most mips16 instructions are 16 bits, but these instructions
1742 are 32 bits.
1743
1744 The format of these instructions is:
1745
1746 +--------------+--------------------------------+
1747 | JALX | X| Imm 20:16 | Imm 25:21 |
1748 +--------------+--------------------------------+
1749 | Immediate 15:0 |
1750 +-----------------------------------------------+
1751
1752 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1753 Note that the immediate value in the first word is swapped.
1754
1755 When producing a relocatable object file, R_MIPS16_26 is
1756 handled mostly like R_MIPS_26. In particular, the addend is
1757 stored as a straight 26-bit value in a 32-bit instruction.
1758 (gas makes life simpler for itself by never adjusting a
1759 R_MIPS16_26 reloc to be against a section, so the addend is
1760 always zero). However, the 32 bit instruction is stored as 2
1761 16-bit values, rather than a single 32-bit value. In a
1762 big-endian file, the result is the same; in a little-endian
1763 file, the two 16-bit halves of the 32 bit value are swapped.
1764 This is so that a disassembler can recognize the jal
1765 instruction.
1766
1767 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1768 instruction stored as two 16-bit values. The addend A is the
1769 contents of the targ26 field. The calculation is the same as
1770 R_MIPS_26. When storing the calculated value, reorder the
1771 immediate value as shown above, and don't forget to store the
1772 value as two 16-bit values.
1773
1774 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1775 defined as
1776
1777 big-endian:
1778 +--------+----------------------+
1779 | | |
1780 | | targ26-16 |
1781 |31 26|25 0|
1782 +--------+----------------------+
1783
1784 little-endian:
1785 +----------+------+-------------+
1786 | | | |
1787 | sub1 | | sub2 |
1788 |0 9|10 15|16 31|
1789 +----------+--------------------+
1790 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1791 ((sub1 << 16) | sub2)).
1792
1793 When producing a relocatable object file, the calculation is
1794 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1795 When producing a fully linked file, the calculation is
1796 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1797 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1798
738e5348
RS
1799 The table below lists the other MIPS16 instruction relocations.
1800 Each one is calculated in the same way as the non-MIPS16 relocation
1801 given on the right, but using the extended MIPS16 layout of 16-bit
1802 immediate fields:
1803
1804 R_MIPS16_GPREL R_MIPS_GPREL16
1805 R_MIPS16_GOT16 R_MIPS_GOT16
1806 R_MIPS16_CALL16 R_MIPS_CALL16
1807 R_MIPS16_HI16 R_MIPS_HI16
1808 R_MIPS16_LO16 R_MIPS_LO16
1809
1810 A typical instruction will have a format like this:
d6f16593
MR
1811
1812 +--------------+--------------------------------+
1813 | EXTEND | Imm 10:5 | Imm 15:11 |
1814 +--------------+--------------------------------+
1815 | Major | rx | ry | Imm 4:0 |
1816 +--------------+--------------------------------+
1817
1818 EXTEND is the five bit value 11110. Major is the instruction
1819 opcode.
1820
738e5348
RS
1821 All we need to do here is shuffle the bits appropriately.
1822 As above, the two 16-bit halves must be swapped on a
1823 little-endian system. */
1824
1825static inline bfd_boolean
1826mips16_reloc_p (int r_type)
1827{
1828 switch (r_type)
1829 {
1830 case R_MIPS16_26:
1831 case R_MIPS16_GPREL:
1832 case R_MIPS16_GOT16:
1833 case R_MIPS16_CALL16:
1834 case R_MIPS16_HI16:
1835 case R_MIPS16_LO16:
1836 return TRUE;
1837
1838 default:
1839 return FALSE;
1840 }
1841}
1842
1843static inline bfd_boolean
1844got16_reloc_p (int r_type)
1845{
1846 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1847}
1848
1849static inline bfd_boolean
1850call16_reloc_p (int r_type)
1851{
1852 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1853}
1854
1855static inline bfd_boolean
1856hi16_reloc_p (int r_type)
1857{
1858 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1859}
d6f16593 1860
738e5348
RS
1861static inline bfd_boolean
1862lo16_reloc_p (int r_type)
1863{
1864 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1865}
1866
1867static inline bfd_boolean
1868mips16_call_reloc_p (int r_type)
1869{
1870 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1871}
d6f16593 1872
d6f16593
MR
1873void
1874_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1875 bfd_boolean jal_shuffle, bfd_byte *data)
1876{
1877 bfd_vma extend, insn, val;
1878
738e5348 1879 if (!mips16_reloc_p (r_type))
d6f16593
MR
1880 return;
1881
1882 /* Pick up the mips16 extend instruction and the real instruction. */
1883 extend = bfd_get_16 (abfd, data);
1884 insn = bfd_get_16 (abfd, data + 2);
1885 if (r_type == R_MIPS16_26)
1886 {
1887 if (jal_shuffle)
1888 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1889 | ((extend & 0x1f) << 21) | insn;
1890 else
1891 val = extend << 16 | insn;
1892 }
1893 else
1894 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1895 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1896 bfd_put_32 (abfd, val, data);
1897}
1898
1899void
1900_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1901 bfd_boolean jal_shuffle, bfd_byte *data)
1902{
1903 bfd_vma extend, insn, val;
1904
738e5348 1905 if (!mips16_reloc_p (r_type))
d6f16593
MR
1906 return;
1907
1908 val = bfd_get_32 (abfd, data);
1909 if (r_type == R_MIPS16_26)
1910 {
1911 if (jal_shuffle)
1912 {
1913 insn = val & 0xffff;
1914 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1915 | ((val >> 21) & 0x1f);
1916 }
1917 else
1918 {
1919 insn = val & 0xffff;
1920 extend = val >> 16;
1921 }
1922 }
1923 else
1924 {
1925 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1926 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1927 }
1928 bfd_put_16 (abfd, insn, data + 2);
1929 bfd_put_16 (abfd, extend, data);
1930}
1931
b49e97c9 1932bfd_reloc_status_type
9719ad41
RS
1933_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1934 arelent *reloc_entry, asection *input_section,
1935 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
1936{
1937 bfd_vma relocation;
a7ebbfdf 1938 bfd_signed_vma val;
30ac9238 1939 bfd_reloc_status_type status;
b49e97c9
TS
1940
1941 if (bfd_is_com_section (symbol->section))
1942 relocation = 0;
1943 else
1944 relocation = symbol->value;
1945
1946 relocation += symbol->section->output_section->vma;
1947 relocation += symbol->section->output_offset;
1948
07515404 1949 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
1950 return bfd_reloc_outofrange;
1951
b49e97c9 1952 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
1953 val = reloc_entry->addend;
1954
30ac9238 1955 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 1956
b49e97c9 1957 /* Adjust val for the final section location and GP value. If we
1049f94e 1958 are producing relocatable output, we don't want to do this for
b49e97c9 1959 an external symbol. */
1049f94e 1960 if (! relocatable
b49e97c9
TS
1961 || (symbol->flags & BSF_SECTION_SYM) != 0)
1962 val += relocation - gp;
1963
a7ebbfdf
TS
1964 if (reloc_entry->howto->partial_inplace)
1965 {
30ac9238
RS
1966 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1967 (bfd_byte *) data
1968 + reloc_entry->address);
1969 if (status != bfd_reloc_ok)
1970 return status;
a7ebbfdf
TS
1971 }
1972 else
1973 reloc_entry->addend = val;
b49e97c9 1974
1049f94e 1975 if (relocatable)
b49e97c9 1976 reloc_entry->address += input_section->output_offset;
30ac9238
RS
1977
1978 return bfd_reloc_ok;
1979}
1980
1981/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1982 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1983 that contains the relocation field and DATA points to the start of
1984 INPUT_SECTION. */
1985
1986struct mips_hi16
1987{
1988 struct mips_hi16 *next;
1989 bfd_byte *data;
1990 asection *input_section;
1991 arelent rel;
1992};
1993
1994/* FIXME: This should not be a static variable. */
1995
1996static struct mips_hi16 *mips_hi16_list;
1997
1998/* A howto special_function for REL *HI16 relocations. We can only
1999 calculate the correct value once we've seen the partnering
2000 *LO16 relocation, so just save the information for later.
2001
2002 The ABI requires that the *LO16 immediately follow the *HI16.
2003 However, as a GNU extension, we permit an arbitrary number of
2004 *HI16s to be associated with a single *LO16. This significantly
2005 simplies the relocation handling in gcc. */
2006
2007bfd_reloc_status_type
2008_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2009 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2010 asection *input_section, bfd *output_bfd,
2011 char **error_message ATTRIBUTE_UNUSED)
2012{
2013 struct mips_hi16 *n;
2014
07515404 2015 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2016 return bfd_reloc_outofrange;
2017
2018 n = bfd_malloc (sizeof *n);
2019 if (n == NULL)
2020 return bfd_reloc_outofrange;
2021
2022 n->next = mips_hi16_list;
2023 n->data = data;
2024 n->input_section = input_section;
2025 n->rel = *reloc_entry;
2026 mips_hi16_list = n;
2027
2028 if (output_bfd != NULL)
2029 reloc_entry->address += input_section->output_offset;
2030
2031 return bfd_reloc_ok;
2032}
2033
738e5348 2034/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2035 like any other 16-bit relocation when applied to global symbols, but is
2036 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2037
2038bfd_reloc_status_type
2039_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2040 void *data, asection *input_section,
2041 bfd *output_bfd, char **error_message)
2042{
2043 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2044 || bfd_is_und_section (bfd_get_section (symbol))
2045 || bfd_is_com_section (bfd_get_section (symbol)))
2046 /* The relocation is against a global symbol. */
2047 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2048 input_section, output_bfd,
2049 error_message);
2050
2051 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2052 input_section, output_bfd, error_message);
2053}
2054
2055/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2056 is a straightforward 16 bit inplace relocation, but we must deal with
2057 any partnering high-part relocations as well. */
2058
2059bfd_reloc_status_type
2060_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2061 void *data, asection *input_section,
2062 bfd *output_bfd, char **error_message)
2063{
2064 bfd_vma vallo;
d6f16593 2065 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2066
07515404 2067 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2068 return bfd_reloc_outofrange;
2069
d6f16593
MR
2070 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2071 location);
2072 vallo = bfd_get_32 (abfd, location);
2073 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2074 location);
2075
30ac9238
RS
2076 while (mips_hi16_list != NULL)
2077 {
2078 bfd_reloc_status_type ret;
2079 struct mips_hi16 *hi;
2080
2081 hi = mips_hi16_list;
2082
738e5348
RS
2083 /* R_MIPS*_GOT16 relocations are something of a special case. We
2084 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2085 relocation (with a rightshift of 16). However, since GOT16
2086 relocations can also be used with global symbols, their howto
2087 has a rightshift of 0. */
2088 if (hi->rel.howto->type == R_MIPS_GOT16)
2089 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2090 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2091 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
30ac9238
RS
2092
2093 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2094 carry or borrow will induce a change of +1 or -1 in the high part. */
2095 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2096
30ac9238
RS
2097 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2098 hi->input_section, output_bfd,
2099 error_message);
2100 if (ret != bfd_reloc_ok)
2101 return ret;
2102
2103 mips_hi16_list = hi->next;
2104 free (hi);
2105 }
2106
2107 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2108 input_section, output_bfd,
2109 error_message);
2110}
2111
2112/* A generic howto special_function. This calculates and installs the
2113 relocation itself, thus avoiding the oft-discussed problems in
2114 bfd_perform_relocation and bfd_install_relocation. */
2115
2116bfd_reloc_status_type
2117_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2118 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2119 asection *input_section, bfd *output_bfd,
2120 char **error_message ATTRIBUTE_UNUSED)
2121{
2122 bfd_signed_vma val;
2123 bfd_reloc_status_type status;
2124 bfd_boolean relocatable;
2125
2126 relocatable = (output_bfd != NULL);
2127
07515404 2128 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2129 return bfd_reloc_outofrange;
2130
2131 /* Build up the field adjustment in VAL. */
2132 val = 0;
2133 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2134 {
2135 /* Either we're calculating the final field value or we have a
2136 relocation against a section symbol. Add in the section's
2137 offset or address. */
2138 val += symbol->section->output_section->vma;
2139 val += symbol->section->output_offset;
2140 }
2141
2142 if (!relocatable)
2143 {
2144 /* We're calculating the final field value. Add in the symbol's value
2145 and, if pc-relative, subtract the address of the field itself. */
2146 val += symbol->value;
2147 if (reloc_entry->howto->pc_relative)
2148 {
2149 val -= input_section->output_section->vma;
2150 val -= input_section->output_offset;
2151 val -= reloc_entry->address;
2152 }
2153 }
2154
2155 /* VAL is now the final adjustment. If we're keeping this relocation
2156 in the output file, and if the relocation uses a separate addend,
2157 we just need to add VAL to that addend. Otherwise we need to add
2158 VAL to the relocation field itself. */
2159 if (relocatable && !reloc_entry->howto->partial_inplace)
2160 reloc_entry->addend += val;
2161 else
2162 {
d6f16593
MR
2163 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2164
30ac9238
RS
2165 /* Add in the separate addend, if any. */
2166 val += reloc_entry->addend;
2167
2168 /* Add VAL to the relocation field. */
d6f16593
MR
2169 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2170 location);
30ac9238 2171 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593
MR
2172 location);
2173 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2174 location);
2175
30ac9238
RS
2176 if (status != bfd_reloc_ok)
2177 return status;
2178 }
2179
2180 if (relocatable)
2181 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2182
2183 return bfd_reloc_ok;
2184}
2185\f
2186/* Swap an entry in a .gptab section. Note that these routines rely
2187 on the equivalence of the two elements of the union. */
2188
2189static void
9719ad41
RS
2190bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2191 Elf32_gptab *in)
b49e97c9
TS
2192{
2193 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2194 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2195}
2196
2197static void
9719ad41
RS
2198bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2199 Elf32_External_gptab *ex)
b49e97c9
TS
2200{
2201 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2202 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2203}
2204
2205static void
9719ad41
RS
2206bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2207 Elf32_External_compact_rel *ex)
b49e97c9
TS
2208{
2209 H_PUT_32 (abfd, in->id1, ex->id1);
2210 H_PUT_32 (abfd, in->num, ex->num);
2211 H_PUT_32 (abfd, in->id2, ex->id2);
2212 H_PUT_32 (abfd, in->offset, ex->offset);
2213 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2214 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2215}
2216
2217static void
9719ad41
RS
2218bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2219 Elf32_External_crinfo *ex)
b49e97c9
TS
2220{
2221 unsigned long l;
2222
2223 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2224 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2225 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2226 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2227 H_PUT_32 (abfd, l, ex->info);
2228 H_PUT_32 (abfd, in->konst, ex->konst);
2229 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2230}
b49e97c9
TS
2231\f
2232/* A .reginfo section holds a single Elf32_RegInfo structure. These
2233 routines swap this structure in and out. They are used outside of
2234 BFD, so they are globally visible. */
2235
2236void
9719ad41
RS
2237bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2238 Elf32_RegInfo *in)
b49e97c9
TS
2239{
2240 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2241 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2242 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2243 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2244 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2245 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2246}
2247
2248void
9719ad41
RS
2249bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2250 Elf32_External_RegInfo *ex)
b49e97c9
TS
2251{
2252 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2253 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2254 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2255 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2256 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2257 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2258}
2259
2260/* In the 64 bit ABI, the .MIPS.options section holds register
2261 information in an Elf64_Reginfo structure. These routines swap
2262 them in and out. They are globally visible because they are used
2263 outside of BFD. These routines are here so that gas can call them
2264 without worrying about whether the 64 bit ABI has been included. */
2265
2266void
9719ad41
RS
2267bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2268 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2269{
2270 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2271 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2272 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2273 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2274 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2275 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2276 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2277}
2278
2279void
9719ad41
RS
2280bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2281 Elf64_External_RegInfo *ex)
b49e97c9
TS
2282{
2283 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2284 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2285 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2286 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2287 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2288 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2289 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2290}
2291
2292/* Swap in an options header. */
2293
2294void
9719ad41
RS
2295bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2296 Elf_Internal_Options *in)
b49e97c9
TS
2297{
2298 in->kind = H_GET_8 (abfd, ex->kind);
2299 in->size = H_GET_8 (abfd, ex->size);
2300 in->section = H_GET_16 (abfd, ex->section);
2301 in->info = H_GET_32 (abfd, ex->info);
2302}
2303
2304/* Swap out an options header. */
2305
2306void
9719ad41
RS
2307bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2308 Elf_External_Options *ex)
b49e97c9
TS
2309{
2310 H_PUT_8 (abfd, in->kind, ex->kind);
2311 H_PUT_8 (abfd, in->size, ex->size);
2312 H_PUT_16 (abfd, in->section, ex->section);
2313 H_PUT_32 (abfd, in->info, ex->info);
2314}
2315\f
2316/* This function is called via qsort() to sort the dynamic relocation
2317 entries by increasing r_symndx value. */
2318
2319static int
9719ad41 2320sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2321{
947216bf
AM
2322 Elf_Internal_Rela int_reloc1;
2323 Elf_Internal_Rela int_reloc2;
6870500c 2324 int diff;
b49e97c9 2325
947216bf
AM
2326 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2327 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2328
6870500c
RS
2329 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2330 if (diff != 0)
2331 return diff;
2332
2333 if (int_reloc1.r_offset < int_reloc2.r_offset)
2334 return -1;
2335 if (int_reloc1.r_offset > int_reloc2.r_offset)
2336 return 1;
2337 return 0;
b49e97c9
TS
2338}
2339
f4416af6
AO
2340/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2341
2342static int
7e3102a7
AM
2343sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2344 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2345{
7e3102a7 2346#ifdef BFD64
f4416af6
AO
2347 Elf_Internal_Rela int_reloc1[3];
2348 Elf_Internal_Rela int_reloc2[3];
2349
2350 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2351 (reldyn_sorting_bfd, arg1, int_reloc1);
2352 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2353 (reldyn_sorting_bfd, arg2, int_reloc2);
2354
6870500c
RS
2355 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2356 return -1;
2357 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2358 return 1;
2359
2360 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2361 return -1;
2362 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2363 return 1;
2364 return 0;
7e3102a7
AM
2365#else
2366 abort ();
2367#endif
f4416af6
AO
2368}
2369
2370
b49e97c9
TS
2371/* This routine is used to write out ECOFF debugging external symbol
2372 information. It is called via mips_elf_link_hash_traverse. The
2373 ECOFF external symbol information must match the ELF external
2374 symbol information. Unfortunately, at this point we don't know
2375 whether a symbol is required by reloc information, so the two
2376 tables may wind up being different. We must sort out the external
2377 symbol information before we can set the final size of the .mdebug
2378 section, and we must set the size of the .mdebug section before we
2379 can relocate any sections, and we can't know which symbols are
2380 required by relocation until we relocate the sections.
2381 Fortunately, it is relatively unlikely that any symbol will be
2382 stripped but required by a reloc. In particular, it can not happen
2383 when generating a final executable. */
2384
b34976b6 2385static bfd_boolean
9719ad41 2386mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2387{
9719ad41 2388 struct extsym_info *einfo = data;
b34976b6 2389 bfd_boolean strip;
b49e97c9
TS
2390 asection *sec, *output_section;
2391
2392 if (h->root.root.type == bfd_link_hash_warning)
2393 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2394
2395 if (h->root.indx == -2)
b34976b6 2396 strip = FALSE;
f5385ebf 2397 else if ((h->root.def_dynamic
77cfaee6
AM
2398 || h->root.ref_dynamic
2399 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2400 && !h->root.def_regular
2401 && !h->root.ref_regular)
b34976b6 2402 strip = TRUE;
b49e97c9
TS
2403 else if (einfo->info->strip == strip_all
2404 || (einfo->info->strip == strip_some
2405 && bfd_hash_lookup (einfo->info->keep_hash,
2406 h->root.root.root.string,
b34976b6
AM
2407 FALSE, FALSE) == NULL))
2408 strip = TRUE;
b49e97c9 2409 else
b34976b6 2410 strip = FALSE;
b49e97c9
TS
2411
2412 if (strip)
b34976b6 2413 return TRUE;
b49e97c9
TS
2414
2415 if (h->esym.ifd == -2)
2416 {
2417 h->esym.jmptbl = 0;
2418 h->esym.cobol_main = 0;
2419 h->esym.weakext = 0;
2420 h->esym.reserved = 0;
2421 h->esym.ifd = ifdNil;
2422 h->esym.asym.value = 0;
2423 h->esym.asym.st = stGlobal;
2424
2425 if (h->root.root.type == bfd_link_hash_undefined
2426 || h->root.root.type == bfd_link_hash_undefweak)
2427 {
2428 const char *name;
2429
2430 /* Use undefined class. Also, set class and type for some
2431 special symbols. */
2432 name = h->root.root.root.string;
2433 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2434 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2435 {
2436 h->esym.asym.sc = scData;
2437 h->esym.asym.st = stLabel;
2438 h->esym.asym.value = 0;
2439 }
2440 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2441 {
2442 h->esym.asym.sc = scAbs;
2443 h->esym.asym.st = stLabel;
2444 h->esym.asym.value =
2445 mips_elf_hash_table (einfo->info)->procedure_count;
2446 }
4a14403c 2447 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2448 {
2449 h->esym.asym.sc = scAbs;
2450 h->esym.asym.st = stLabel;
2451 h->esym.asym.value = elf_gp (einfo->abfd);
2452 }
2453 else
2454 h->esym.asym.sc = scUndefined;
2455 }
2456 else if (h->root.root.type != bfd_link_hash_defined
2457 && h->root.root.type != bfd_link_hash_defweak)
2458 h->esym.asym.sc = scAbs;
2459 else
2460 {
2461 const char *name;
2462
2463 sec = h->root.root.u.def.section;
2464 output_section = sec->output_section;
2465
2466 /* When making a shared library and symbol h is the one from
2467 the another shared library, OUTPUT_SECTION may be null. */
2468 if (output_section == NULL)
2469 h->esym.asym.sc = scUndefined;
2470 else
2471 {
2472 name = bfd_section_name (output_section->owner, output_section);
2473
2474 if (strcmp (name, ".text") == 0)
2475 h->esym.asym.sc = scText;
2476 else if (strcmp (name, ".data") == 0)
2477 h->esym.asym.sc = scData;
2478 else if (strcmp (name, ".sdata") == 0)
2479 h->esym.asym.sc = scSData;
2480 else if (strcmp (name, ".rodata") == 0
2481 || strcmp (name, ".rdata") == 0)
2482 h->esym.asym.sc = scRData;
2483 else if (strcmp (name, ".bss") == 0)
2484 h->esym.asym.sc = scBss;
2485 else if (strcmp (name, ".sbss") == 0)
2486 h->esym.asym.sc = scSBss;
2487 else if (strcmp (name, ".init") == 0)
2488 h->esym.asym.sc = scInit;
2489 else if (strcmp (name, ".fini") == 0)
2490 h->esym.asym.sc = scFini;
2491 else
2492 h->esym.asym.sc = scAbs;
2493 }
2494 }
2495
2496 h->esym.asym.reserved = 0;
2497 h->esym.asym.index = indexNil;
2498 }
2499
2500 if (h->root.root.type == bfd_link_hash_common)
2501 h->esym.asym.value = h->root.root.u.c.size;
2502 else if (h->root.root.type == bfd_link_hash_defined
2503 || h->root.root.type == bfd_link_hash_defweak)
2504 {
2505 if (h->esym.asym.sc == scCommon)
2506 h->esym.asym.sc = scBss;
2507 else if (h->esym.asym.sc == scSCommon)
2508 h->esym.asym.sc = scSBss;
2509
2510 sec = h->root.root.u.def.section;
2511 output_section = sec->output_section;
2512 if (output_section != NULL)
2513 h->esym.asym.value = (h->root.root.u.def.value
2514 + sec->output_offset
2515 + output_section->vma);
2516 else
2517 h->esym.asym.value = 0;
2518 }
33bb52fb 2519 else
b49e97c9
TS
2520 {
2521 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2522
2523 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2524 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2525
33bb52fb 2526 if (hd->needs_lazy_stub)
b49e97c9
TS
2527 {
2528 /* Set type and value for a symbol with a function stub. */
2529 h->esym.asym.st = stProc;
2530 sec = hd->root.root.u.def.section;
2531 if (sec == NULL)
2532 h->esym.asym.value = 0;
2533 else
2534 {
2535 output_section = sec->output_section;
2536 if (output_section != NULL)
2537 h->esym.asym.value = (hd->root.plt.offset
2538 + sec->output_offset
2539 + output_section->vma);
2540 else
2541 h->esym.asym.value = 0;
2542 }
b49e97c9
TS
2543 }
2544 }
2545
2546 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2547 h->root.root.root.string,
2548 &h->esym))
2549 {
b34976b6
AM
2550 einfo->failed = TRUE;
2551 return FALSE;
b49e97c9
TS
2552 }
2553
b34976b6 2554 return TRUE;
b49e97c9
TS
2555}
2556
2557/* A comparison routine used to sort .gptab entries. */
2558
2559static int
9719ad41 2560gptab_compare (const void *p1, const void *p2)
b49e97c9 2561{
9719ad41
RS
2562 const Elf32_gptab *a1 = p1;
2563 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2564
2565 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2566}
2567\f
b15e6682 2568/* Functions to manage the got entry hash table. */
f4416af6
AO
2569
2570/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2571 hash number. */
2572
2573static INLINE hashval_t
9719ad41 2574mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2575{
2576#ifdef BFD64
2577 return addr + (addr >> 32);
2578#else
2579 return addr;
2580#endif
2581}
2582
2583/* got_entries only match if they're identical, except for gotidx, so
2584 use all fields to compute the hash, and compare the appropriate
2585 union members. */
2586
b15e6682 2587static hashval_t
9719ad41 2588mips_elf_got_entry_hash (const void *entry_)
b15e6682
AO
2589{
2590 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2591
38985a1c 2592 return entry->symndx
0f20cc35 2593 + ((entry->tls_type & GOT_TLS_LDM) << 17)
f4416af6 2594 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
38985a1c
AO
2595 : entry->abfd->id
2596 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2597 : entry->d.h->root.root.root.hash));
b15e6682
AO
2598}
2599
2600static int
9719ad41 2601mips_elf_got_entry_eq (const void *entry1, const void *entry2)
b15e6682
AO
2602{
2603 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2604 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2605
0f20cc35
DJ
2606 /* An LDM entry can only match another LDM entry. */
2607 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2608 return 0;
2609
b15e6682 2610 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
f4416af6
AO
2611 && (! e1->abfd ? e1->d.address == e2->d.address
2612 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2613 : e1->d.h == e2->d.h);
2614}
2615
2616/* multi_got_entries are still a match in the case of global objects,
2617 even if the input bfd in which they're referenced differs, so the
2618 hash computation and compare functions are adjusted
2619 accordingly. */
2620
2621static hashval_t
9719ad41 2622mips_elf_multi_got_entry_hash (const void *entry_)
f4416af6
AO
2623{
2624 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2625
2626 return entry->symndx
2627 + (! entry->abfd
2628 ? mips_elf_hash_bfd_vma (entry->d.address)
2629 : entry->symndx >= 0
0f20cc35
DJ
2630 ? ((entry->tls_type & GOT_TLS_LDM)
2631 ? (GOT_TLS_LDM << 17)
2632 : (entry->abfd->id
2633 + mips_elf_hash_bfd_vma (entry->d.addend)))
f4416af6
AO
2634 : entry->d.h->root.root.root.hash);
2635}
2636
2637static int
9719ad41 2638mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2639{
2640 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2641 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2642
0f20cc35
DJ
2643 /* Any two LDM entries match. */
2644 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2645 return 1;
2646
2647 /* Nothing else matches an LDM entry. */
2648 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2649 return 0;
2650
f4416af6
AO
2651 return e1->symndx == e2->symndx
2652 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2653 : e1->abfd == NULL || e2->abfd == NULL
2654 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2655 : e1->d.h == e2->d.h);
b15e6682 2656}
c224138d
RS
2657
2658static hashval_t
2659mips_got_page_entry_hash (const void *entry_)
2660{
2661 const struct mips_got_page_entry *entry;
2662
2663 entry = (const struct mips_got_page_entry *) entry_;
2664 return entry->abfd->id + entry->symndx;
2665}
2666
2667static int
2668mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2669{
2670 const struct mips_got_page_entry *entry1, *entry2;
2671
2672 entry1 = (const struct mips_got_page_entry *) entry1_;
2673 entry2 = (const struct mips_got_page_entry *) entry2_;
2674 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2675}
b15e6682 2676\f
0a44bf69
RS
2677/* Return the dynamic relocation section. If it doesn't exist, try to
2678 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2679 if creation fails. */
f4416af6
AO
2680
2681static asection *
0a44bf69 2682mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 2683{
0a44bf69 2684 const char *dname;
f4416af6 2685 asection *sreloc;
0a44bf69 2686 bfd *dynobj;
f4416af6 2687
0a44bf69
RS
2688 dname = MIPS_ELF_REL_DYN_NAME (info);
2689 dynobj = elf_hash_table (info)->dynobj;
f4416af6
AO
2690 sreloc = bfd_get_section_by_name (dynobj, dname);
2691 if (sreloc == NULL && create_p)
2692 {
3496cb2a
L
2693 sreloc = bfd_make_section_with_flags (dynobj, dname,
2694 (SEC_ALLOC
2695 | SEC_LOAD
2696 | SEC_HAS_CONTENTS
2697 | SEC_IN_MEMORY
2698 | SEC_LINKER_CREATED
2699 | SEC_READONLY));
f4416af6 2700 if (sreloc == NULL
f4416af6 2701 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 2702 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
2703 return NULL;
2704 }
2705 return sreloc;
2706}
2707
0f20cc35
DJ
2708/* Count the number of relocations needed for a TLS GOT entry, with
2709 access types from TLS_TYPE, and symbol H (or a local symbol if H
2710 is NULL). */
2711
2712static int
2713mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2714 struct elf_link_hash_entry *h)
2715{
2716 int indx = 0;
2717 int ret = 0;
2718 bfd_boolean need_relocs = FALSE;
2719 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2720
2721 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2722 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2723 indx = h->dynindx;
2724
2725 if ((info->shared || indx != 0)
2726 && (h == NULL
2727 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2728 || h->root.type != bfd_link_hash_undefweak))
2729 need_relocs = TRUE;
2730
2731 if (!need_relocs)
2732 return FALSE;
2733
2734 if (tls_type & GOT_TLS_GD)
2735 {
2736 ret++;
2737 if (indx != 0)
2738 ret++;
2739 }
2740
2741 if (tls_type & GOT_TLS_IE)
2742 ret++;
2743
2744 if ((tls_type & GOT_TLS_LDM) && info->shared)
2745 ret++;
2746
2747 return ret;
2748}
2749
2750/* Count the number of TLS relocations required for the GOT entry in
2751 ARG1, if it describes a local symbol. */
2752
2753static int
2754mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2755{
2756 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2757 struct mips_elf_count_tls_arg *arg = arg2;
2758
2759 if (entry->abfd != NULL && entry->symndx != -1)
2760 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2761
2762 return 1;
2763}
2764
2765/* Count the number of TLS GOT entries required for the global (or
2766 forced-local) symbol in ARG1. */
2767
2768static int
2769mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2770{
2771 struct mips_elf_link_hash_entry *hm
2772 = (struct mips_elf_link_hash_entry *) arg1;
2773 struct mips_elf_count_tls_arg *arg = arg2;
2774
2775 if (hm->tls_type & GOT_TLS_GD)
2776 arg->needed += 2;
2777 if (hm->tls_type & GOT_TLS_IE)
2778 arg->needed += 1;
2779
2780 return 1;
2781}
2782
2783/* Count the number of TLS relocations required for the global (or
2784 forced-local) symbol in ARG1. */
2785
2786static int
2787mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2788{
2789 struct mips_elf_link_hash_entry *hm
2790 = (struct mips_elf_link_hash_entry *) arg1;
2791 struct mips_elf_count_tls_arg *arg = arg2;
2792
2793 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2794
2795 return 1;
2796}
2797
2798/* Output a simple dynamic relocation into SRELOC. */
2799
2800static void
2801mips_elf_output_dynamic_relocation (bfd *output_bfd,
2802 asection *sreloc,
861fb55a 2803 unsigned long reloc_index,
0f20cc35
DJ
2804 unsigned long indx,
2805 int r_type,
2806 bfd_vma offset)
2807{
2808 Elf_Internal_Rela rel[3];
2809
2810 memset (rel, 0, sizeof (rel));
2811
2812 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2813 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2814
2815 if (ABI_64_P (output_bfd))
2816 {
2817 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2818 (output_bfd, &rel[0],
2819 (sreloc->contents
861fb55a 2820 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
2821 }
2822 else
2823 bfd_elf32_swap_reloc_out
2824 (output_bfd, &rel[0],
2825 (sreloc->contents
861fb55a 2826 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
2827}
2828
2829/* Initialize a set of TLS GOT entries for one symbol. */
2830
2831static void
2832mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2833 unsigned char *tls_type_p,
2834 struct bfd_link_info *info,
2835 struct mips_elf_link_hash_entry *h,
2836 bfd_vma value)
2837{
23cc69b6 2838 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
2839 int indx;
2840 asection *sreloc, *sgot;
2841 bfd_vma offset, offset2;
0f20cc35
DJ
2842 bfd_boolean need_relocs = FALSE;
2843
23cc69b6
RS
2844 htab = mips_elf_hash_table (info);
2845 sgot = htab->sgot;
0f20cc35
DJ
2846
2847 indx = 0;
2848 if (h != NULL)
2849 {
2850 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2851
2852 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2853 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2854 indx = h->root.dynindx;
2855 }
2856
2857 if (*tls_type_p & GOT_TLS_DONE)
2858 return;
2859
2860 if ((info->shared || indx != 0)
2861 && (h == NULL
2862 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2863 || h->root.type != bfd_link_hash_undefweak))
2864 need_relocs = TRUE;
2865
2866 /* MINUS_ONE means the symbol is not defined in this object. It may not
2867 be defined at all; assume that the value doesn't matter in that
2868 case. Otherwise complain if we would use the value. */
2869 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2870 || h->root.root.type == bfd_link_hash_undefweak);
2871
2872 /* Emit necessary relocations. */
0a44bf69 2873 sreloc = mips_elf_rel_dyn_section (info, FALSE);
0f20cc35
DJ
2874
2875 /* General Dynamic. */
2876 if (*tls_type_p & GOT_TLS_GD)
2877 {
2878 offset = got_offset;
2879 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2880
2881 if (need_relocs)
2882 {
2883 mips_elf_output_dynamic_relocation
861fb55a 2884 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2885 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2886 sgot->output_offset + sgot->output_section->vma + offset);
2887
2888 if (indx)
2889 mips_elf_output_dynamic_relocation
861fb55a 2890 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2891 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2892 sgot->output_offset + sgot->output_section->vma + offset2);
2893 else
2894 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2895 sgot->contents + offset2);
2896 }
2897 else
2898 {
2899 MIPS_ELF_PUT_WORD (abfd, 1,
2900 sgot->contents + offset);
2901 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2902 sgot->contents + offset2);
2903 }
2904
2905 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2906 }
2907
2908 /* Initial Exec model. */
2909 if (*tls_type_p & GOT_TLS_IE)
2910 {
2911 offset = got_offset;
2912
2913 if (need_relocs)
2914 {
2915 if (indx == 0)
2916 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2917 sgot->contents + offset);
2918 else
2919 MIPS_ELF_PUT_WORD (abfd, 0,
2920 sgot->contents + offset);
2921
2922 mips_elf_output_dynamic_relocation
861fb55a 2923 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2924 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2925 sgot->output_offset + sgot->output_section->vma + offset);
2926 }
2927 else
2928 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2929 sgot->contents + offset);
2930 }
2931
2932 if (*tls_type_p & GOT_TLS_LDM)
2933 {
2934 /* The initial offset is zero, and the LD offsets will include the
2935 bias by DTP_OFFSET. */
2936 MIPS_ELF_PUT_WORD (abfd, 0,
2937 sgot->contents + got_offset
2938 + MIPS_ELF_GOT_SIZE (abfd));
2939
2940 if (!info->shared)
2941 MIPS_ELF_PUT_WORD (abfd, 1,
2942 sgot->contents + got_offset);
2943 else
2944 mips_elf_output_dynamic_relocation
861fb55a 2945 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2946 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2947 sgot->output_offset + sgot->output_section->vma + got_offset);
2948 }
2949
2950 *tls_type_p |= GOT_TLS_DONE;
2951}
2952
2953/* Return the GOT index to use for a relocation of type R_TYPE against
2954 a symbol accessed using TLS_TYPE models. The GOT entries for this
2955 symbol in this GOT start at GOT_INDEX. This function initializes the
2956 GOT entries and corresponding relocations. */
2957
2958static bfd_vma
2959mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2960 int r_type, struct bfd_link_info *info,
2961 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2962{
2963 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2964 || r_type == R_MIPS_TLS_LDM);
2965
2966 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2967
2968 if (r_type == R_MIPS_TLS_GOTTPREL)
2969 {
2970 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2971 if (*tls_type & GOT_TLS_GD)
2972 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2973 else
2974 return got_index;
2975 }
2976
2977 if (r_type == R_MIPS_TLS_GD)
2978 {
2979 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2980 return got_index;
2981 }
2982
2983 if (r_type == R_MIPS_TLS_LDM)
2984 {
2985 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2986 return got_index;
2987 }
2988
2989 return got_index;
2990}
2991
0a44bf69
RS
2992/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2993 for global symbol H. .got.plt comes before the GOT, so the offset
2994 will be negative. */
2995
2996static bfd_vma
2997mips_elf_gotplt_index (struct bfd_link_info *info,
2998 struct elf_link_hash_entry *h)
2999{
3000 bfd_vma plt_index, got_address, got_value;
3001 struct mips_elf_link_hash_table *htab;
3002
3003 htab = mips_elf_hash_table (info);
3004 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3005
861fb55a
DJ
3006 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3007 section starts with reserved entries. */
3008 BFD_ASSERT (htab->is_vxworks);
3009
0a44bf69
RS
3010 /* Calculate the index of the symbol's PLT entry. */
3011 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3012
3013 /* Calculate the address of the associated .got.plt entry. */
3014 got_address = (htab->sgotplt->output_section->vma
3015 + htab->sgotplt->output_offset
3016 + plt_index * 4);
3017
3018 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3019 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3020 + htab->root.hgot->root.u.def.section->output_offset
3021 + htab->root.hgot->root.u.def.value);
3022
3023 return got_address - got_value;
3024}
3025
5c18022e 3026/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3027 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3028 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3029 offset can be found. */
b49e97c9
TS
3030
3031static bfd_vma
9719ad41 3032mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3033 bfd_vma value, unsigned long r_symndx,
0f20cc35 3034 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3035{
a8028dd0 3036 struct mips_elf_link_hash_table *htab;
b15e6682 3037 struct mips_got_entry *entry;
b49e97c9 3038
a8028dd0
RS
3039 htab = mips_elf_hash_table (info);
3040 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3041 r_symndx, h, r_type);
0f20cc35 3042 if (!entry)
b15e6682 3043 return MINUS_ONE;
0f20cc35
DJ
3044
3045 if (TLS_RELOC_P (r_type))
ead49a57 3046 {
a8028dd0 3047 if (entry->symndx == -1 && htab->got_info->next == NULL)
ead49a57
RS
3048 /* A type (3) entry in the single-GOT case. We use the symbol's
3049 hash table entry to track the index. */
3050 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3051 r_type, info, h, value);
3052 else
3053 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3054 r_type, info, h, value);
3055 }
0f20cc35
DJ
3056 else
3057 return entry->gotidx;
b49e97c9
TS
3058}
3059
3060/* Returns the GOT index for the global symbol indicated by H. */
3061
3062static bfd_vma
0f20cc35
DJ
3063mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3064 int r_type, struct bfd_link_info *info)
b49e97c9 3065{
a8028dd0 3066 struct mips_elf_link_hash_table *htab;
b49e97c9 3067 bfd_vma index;
f4416af6 3068 struct mips_got_info *g, *gg;
d0c7ff07 3069 long global_got_dynindx = 0;
b49e97c9 3070
a8028dd0
RS
3071 htab = mips_elf_hash_table (info);
3072 gg = g = htab->got_info;
f4416af6
AO
3073 if (g->bfd2got && ibfd)
3074 {
3075 struct mips_got_entry e, *p;
143d77c5 3076
f4416af6
AO
3077 BFD_ASSERT (h->dynindx >= 0);
3078
3079 g = mips_elf_got_for_ibfd (g, ibfd);
0f20cc35 3080 if (g->next != gg || TLS_RELOC_P (r_type))
f4416af6
AO
3081 {
3082 e.abfd = ibfd;
3083 e.symndx = -1;
3084 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 3085 e.tls_type = 0;
f4416af6 3086
9719ad41 3087 p = htab_find (g->got_entries, &e);
f4416af6
AO
3088
3089 BFD_ASSERT (p->gotidx > 0);
0f20cc35
DJ
3090
3091 if (TLS_RELOC_P (r_type))
3092 {
3093 bfd_vma value = MINUS_ONE;
3094 if ((h->root.type == bfd_link_hash_defined
3095 || h->root.type == bfd_link_hash_defweak)
3096 && h->root.u.def.section->output_section)
3097 value = (h->root.u.def.value
3098 + h->root.u.def.section->output_offset
3099 + h->root.u.def.section->output_section->vma);
3100
3101 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3102 info, e.d.h, value);
3103 }
3104 else
3105 return p->gotidx;
f4416af6
AO
3106 }
3107 }
3108
3109 if (gg->global_gotsym != NULL)
3110 global_got_dynindx = gg->global_gotsym->dynindx;
b49e97c9 3111
0f20cc35
DJ
3112 if (TLS_RELOC_P (r_type))
3113 {
3114 struct mips_elf_link_hash_entry *hm
3115 = (struct mips_elf_link_hash_entry *) h;
3116 bfd_vma value = MINUS_ONE;
3117
3118 if ((h->root.type == bfd_link_hash_defined
3119 || h->root.type == bfd_link_hash_defweak)
3120 && h->root.u.def.section->output_section)
3121 value = (h->root.u.def.value
3122 + h->root.u.def.section->output_offset
3123 + h->root.u.def.section->output_section->vma);
3124
3125 index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3126 r_type, info, hm, value);
3127 }
3128 else
3129 {
3130 /* Once we determine the global GOT entry with the lowest dynamic
3131 symbol table index, we must put all dynamic symbols with greater
3132 indices into the GOT. That makes it easy to calculate the GOT
3133 offset. */
3134 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3135 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3136 * MIPS_ELF_GOT_SIZE (abfd));
3137 }
a8028dd0 3138 BFD_ASSERT (index < htab->sgot->size);
b49e97c9
TS
3139
3140 return index;
3141}
3142
5c18022e
RS
3143/* Find a GOT page entry that points to within 32KB of VALUE. These
3144 entries are supposed to be placed at small offsets in the GOT, i.e.,
3145 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3146 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3147 offset of the GOT entry from VALUE. */
b49e97c9
TS
3148
3149static bfd_vma
9719ad41 3150mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3151 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3152{
0a44bf69 3153 bfd_vma page, index;
b15e6682 3154 struct mips_got_entry *entry;
b49e97c9 3155
0a44bf69 3156 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3157 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3158 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3159
b15e6682
AO
3160 if (!entry)
3161 return MINUS_ONE;
143d77c5 3162
b15e6682 3163 index = entry->gotidx;
b49e97c9
TS
3164
3165 if (offsetp)
f4416af6 3166 *offsetp = value - entry->d.address;
b49e97c9
TS
3167
3168 return index;
3169}
3170
738e5348 3171/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
0a44bf69
RS
3172 EXTERNAL is true if the relocation was against a global symbol
3173 that has been forced local. */
b49e97c9
TS
3174
3175static bfd_vma
9719ad41 3176mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3177 bfd_vma value, bfd_boolean external)
b49e97c9 3178{
b15e6682 3179 struct mips_got_entry *entry;
b49e97c9 3180
0a44bf69
RS
3181 /* GOT16 relocations against local symbols are followed by a LO16
3182 relocation; those against global symbols are not. Thus if the
3183 symbol was originally local, the GOT16 relocation should load the
3184 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3185 if (! external)
0a44bf69 3186 value = mips_elf_high (value) << 16;
b49e97c9 3187
738e5348
RS
3188 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3189 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3190 same in all cases. */
a8028dd0
RS
3191 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3192 NULL, R_MIPS_GOT16);
b15e6682
AO
3193 if (entry)
3194 return entry->gotidx;
3195 else
3196 return MINUS_ONE;
b49e97c9
TS
3197}
3198
3199/* Returns the offset for the entry at the INDEXth position
3200 in the GOT. */
3201
3202static bfd_vma
a8028dd0 3203mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
9719ad41 3204 bfd *input_bfd, bfd_vma index)
b49e97c9 3205{
a8028dd0 3206 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3207 asection *sgot;
3208 bfd_vma gp;
3209
a8028dd0
RS
3210 htab = mips_elf_hash_table (info);
3211 sgot = htab->sgot;
f4416af6 3212 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3213 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3214
f4416af6 3215 return sgot->output_section->vma + sgot->output_offset + index - gp;
b49e97c9
TS
3216}
3217
0a44bf69
RS
3218/* Create and return a local GOT entry for VALUE, which was calculated
3219 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3220 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3221 instead. */
b49e97c9 3222
b15e6682 3223static struct mips_got_entry *
0a44bf69 3224mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3225 bfd *ibfd, bfd_vma value,
5c18022e 3226 unsigned long r_symndx,
0f20cc35
DJ
3227 struct mips_elf_link_hash_entry *h,
3228 int r_type)
b49e97c9 3229{
b15e6682 3230 struct mips_got_entry entry, **loc;
f4416af6 3231 struct mips_got_info *g;
0a44bf69
RS
3232 struct mips_elf_link_hash_table *htab;
3233
3234 htab = mips_elf_hash_table (info);
b15e6682 3235
f4416af6
AO
3236 entry.abfd = NULL;
3237 entry.symndx = -1;
3238 entry.d.address = value;
0f20cc35 3239 entry.tls_type = 0;
f4416af6 3240
a8028dd0 3241 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
f4416af6
AO
3242 if (g == NULL)
3243 {
a8028dd0 3244 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
f4416af6
AO
3245 BFD_ASSERT (g != NULL);
3246 }
b15e6682 3247
0f20cc35
DJ
3248 /* We might have a symbol, H, if it has been forced local. Use the
3249 global entry then. It doesn't matter whether an entry is local
3250 or global for TLS, since the dynamic linker does not
3251 automatically relocate TLS GOT entries. */
a008ac03 3252 BFD_ASSERT (h == NULL || h->root.forced_local);
0f20cc35
DJ
3253 if (TLS_RELOC_P (r_type))
3254 {
3255 struct mips_got_entry *p;
3256
3257 entry.abfd = ibfd;
3258 if (r_type == R_MIPS_TLS_LDM)
3259 {
3260 entry.tls_type = GOT_TLS_LDM;
3261 entry.symndx = 0;
3262 entry.d.addend = 0;
3263 }
3264 else if (h == NULL)
3265 {
3266 entry.symndx = r_symndx;
3267 entry.d.addend = 0;
3268 }
3269 else
3270 entry.d.h = h;
3271
3272 p = (struct mips_got_entry *)
3273 htab_find (g->got_entries, &entry);
3274
3275 BFD_ASSERT (p);
3276 return p;
3277 }
3278
b15e6682
AO
3279 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3280 INSERT);
3281 if (*loc)
3282 return *loc;
143d77c5 3283
b15e6682 3284 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
0f20cc35 3285 entry.tls_type = 0;
b15e6682
AO
3286
3287 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3288
3289 if (! *loc)
3290 return NULL;
143d77c5 3291
b15e6682
AO
3292 memcpy (*loc, &entry, sizeof entry);
3293
8275b357 3294 if (g->assigned_gotno > g->local_gotno)
b49e97c9 3295 {
f4416af6 3296 (*loc)->gotidx = -1;
b49e97c9
TS
3297 /* We didn't allocate enough space in the GOT. */
3298 (*_bfd_error_handler)
3299 (_("not enough GOT space for local GOT entries"));
3300 bfd_set_error (bfd_error_bad_value);
b15e6682 3301 return NULL;
b49e97c9
TS
3302 }
3303
3304 MIPS_ELF_PUT_WORD (abfd, value,
a8028dd0 3305 (htab->sgot->contents + entry.gotidx));
b15e6682 3306
5c18022e 3307 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3308 if (htab->is_vxworks)
3309 {
3310 Elf_Internal_Rela outrel;
5c18022e 3311 asection *s;
0a44bf69
RS
3312 bfd_byte *loc;
3313 bfd_vma got_address;
0a44bf69
RS
3314
3315 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3316 got_address = (htab->sgot->output_section->vma
3317 + htab->sgot->output_offset
0a44bf69
RS
3318 + entry.gotidx);
3319
3320 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3321 outrel.r_offset = got_address;
5c18022e
RS
3322 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3323 outrel.r_addend = value;
0a44bf69
RS
3324 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
3325 }
3326
b15e6682 3327 return *loc;
b49e97c9
TS
3328}
3329
d4596a51
RS
3330/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3331 The number might be exact or a worst-case estimate, depending on how
3332 much information is available to elf_backend_omit_section_dynsym at
3333 the current linking stage. */
3334
3335static bfd_size_type
3336count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3337{
3338 bfd_size_type count;
3339
3340 count = 0;
3341 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3342 {
3343 asection *p;
3344 const struct elf_backend_data *bed;
3345
3346 bed = get_elf_backend_data (output_bfd);
3347 for (p = output_bfd->sections; p ; p = p->next)
3348 if ((p->flags & SEC_EXCLUDE) == 0
3349 && (p->flags & SEC_ALLOC) != 0
3350 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3351 ++count;
3352 }
3353 return count;
3354}
3355
b49e97c9 3356/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3357 appear towards the end. */
b49e97c9 3358
b34976b6 3359static bfd_boolean
d4596a51 3360mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3361{
a8028dd0 3362 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3363 struct mips_elf_hash_sort_data hsd;
3364 struct mips_got_info *g;
b49e97c9 3365
d4596a51
RS
3366 if (elf_hash_table (info)->dynsymcount == 0)
3367 return TRUE;
3368
a8028dd0
RS
3369 htab = mips_elf_hash_table (info);
3370 g = htab->got_info;
d4596a51
RS
3371 if (g == NULL)
3372 return TRUE;
f4416af6 3373
b49e97c9 3374 hsd.low = NULL;
23cc69b6
RS
3375 hsd.max_unref_got_dynindx
3376 = hsd.min_got_dynindx
3377 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3378 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3379 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3380 elf_hash_table (info)),
3381 mips_elf_sort_hash_table_f,
3382 &hsd);
3383
3384 /* There should have been enough room in the symbol table to
44c410de 3385 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3386 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3387 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3388 == elf_hash_table (info)->dynsymcount);
3389 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3390 == g->global_gotno);
b49e97c9
TS
3391
3392 /* Now we know which dynamic symbol has the lowest dynamic symbol
3393 table index in the GOT. */
b49e97c9
TS
3394 g->global_gotsym = hsd.low;
3395
b34976b6 3396 return TRUE;
b49e97c9
TS
3397}
3398
3399/* If H needs a GOT entry, assign it the highest available dynamic
3400 index. Otherwise, assign it the lowest available dynamic
3401 index. */
3402
b34976b6 3403static bfd_boolean
9719ad41 3404mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3405{
9719ad41 3406 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9
TS
3407
3408 if (h->root.root.type == bfd_link_hash_warning)
3409 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3410
3411 /* Symbols without dynamic symbol table entries aren't interesting
3412 at all. */
3413 if (h->root.dynindx == -1)
b34976b6 3414 return TRUE;
b49e97c9 3415
634835ae 3416 switch (h->global_got_area)
f4416af6 3417 {
634835ae
RS
3418 case GGA_NONE:
3419 h->root.dynindx = hsd->max_non_got_dynindx++;
3420 break;
0f20cc35 3421
634835ae 3422 case GGA_NORMAL:
0f20cc35
DJ
3423 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3424
b49e97c9
TS
3425 h->root.dynindx = --hsd->min_got_dynindx;
3426 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3427 break;
3428
3429 case GGA_RELOC_ONLY:
3430 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3431
3432 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3433 hsd->low = (struct elf_link_hash_entry *) h;
3434 h->root.dynindx = hsd->max_unref_got_dynindx++;
3435 break;
b49e97c9
TS
3436 }
3437
b34976b6 3438 return TRUE;
b49e97c9
TS
3439}
3440
3441/* If H is a symbol that needs a global GOT entry, but has a dynamic
3442 symbol table index lower than any we've seen to date, record it for
3443 posterity. */
3444
b34976b6 3445static bfd_boolean
9719ad41
RS
3446mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3447 bfd *abfd, struct bfd_link_info *info,
0f20cc35 3448 unsigned char tls_flag)
b49e97c9 3449{
a8028dd0 3450 struct mips_elf_link_hash_table *htab;
634835ae 3451 struct mips_elf_link_hash_entry *hmips;
f4416af6 3452 struct mips_got_entry entry, **loc;
a8028dd0
RS
3453 struct mips_got_info *g;
3454
3455 htab = mips_elf_hash_table (info);
634835ae 3456 hmips = (struct mips_elf_link_hash_entry *) h;
f4416af6 3457
b49e97c9
TS
3458 /* A global symbol in the GOT must also be in the dynamic symbol
3459 table. */
7c5fcef7
L
3460 if (h->dynindx == -1)
3461 {
3462 switch (ELF_ST_VISIBILITY (h->other))
3463 {
3464 case STV_INTERNAL:
3465 case STV_HIDDEN:
33bb52fb 3466 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3467 break;
3468 }
c152c796 3469 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3470 return FALSE;
7c5fcef7 3471 }
b49e97c9 3472
86324f90 3473 /* Make sure we have a GOT to put this entry into. */
a8028dd0 3474 g = htab->got_info;
86324f90
EC
3475 BFD_ASSERT (g != NULL);
3476
f4416af6
AO
3477 entry.abfd = abfd;
3478 entry.symndx = -1;
3479 entry.d.h = (struct mips_elf_link_hash_entry *) h;
0f20cc35 3480 entry.tls_type = 0;
f4416af6
AO
3481
3482 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3483 INSERT);
3484
b49e97c9
TS
3485 /* If we've already marked this entry as needing GOT space, we don't
3486 need to do it again. */
f4416af6 3487 if (*loc)
0f20cc35
DJ
3488 {
3489 (*loc)->tls_type |= tls_flag;
3490 return TRUE;
3491 }
f4416af6
AO
3492
3493 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3494
3495 if (! *loc)
3496 return FALSE;
143d77c5 3497
f4416af6 3498 entry.gotidx = -1;
0f20cc35
DJ
3499 entry.tls_type = tls_flag;
3500
f4416af6
AO
3501 memcpy (*loc, &entry, sizeof entry);
3502
0f20cc35 3503 if (tls_flag == 0)
634835ae 3504 hmips->global_got_area = GGA_NORMAL;
b49e97c9 3505
b34976b6 3506 return TRUE;
b49e97c9 3507}
f4416af6
AO
3508
3509/* Reserve space in G for a GOT entry containing the value of symbol
3510 SYMNDX in input bfd ABDF, plus ADDEND. */
3511
3512static bfd_boolean
9719ad41 3513mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
a8028dd0 3514 struct bfd_link_info *info,
0f20cc35 3515 unsigned char tls_flag)
f4416af6 3516{
a8028dd0
RS
3517 struct mips_elf_link_hash_table *htab;
3518 struct mips_got_info *g;
f4416af6
AO
3519 struct mips_got_entry entry, **loc;
3520
a8028dd0
RS
3521 htab = mips_elf_hash_table (info);
3522 g = htab->got_info;
3523 BFD_ASSERT (g != NULL);
3524
f4416af6
AO
3525 entry.abfd = abfd;
3526 entry.symndx = symndx;
3527 entry.d.addend = addend;
0f20cc35 3528 entry.tls_type = tls_flag;
f4416af6
AO
3529 loc = (struct mips_got_entry **)
3530 htab_find_slot (g->got_entries, &entry, INSERT);
3531
3532 if (*loc)
0f20cc35
DJ
3533 {
3534 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3535 {
3536 g->tls_gotno += 2;
3537 (*loc)->tls_type |= tls_flag;
3538 }
3539 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3540 {
3541 g->tls_gotno += 1;
3542 (*loc)->tls_type |= tls_flag;
3543 }
3544 return TRUE;
3545 }
f4416af6 3546
0f20cc35
DJ
3547 if (tls_flag != 0)
3548 {
3549 entry.gotidx = -1;
3550 entry.tls_type = tls_flag;
3551 if (tls_flag == GOT_TLS_IE)
3552 g->tls_gotno += 1;
3553 else if (tls_flag == GOT_TLS_GD)
3554 g->tls_gotno += 2;
3555 else if (g->tls_ldm_offset == MINUS_ONE)
3556 {
3557 g->tls_ldm_offset = MINUS_TWO;
3558 g->tls_gotno += 2;
3559 }
3560 }
3561 else
3562 {
3563 entry.gotidx = g->local_gotno++;
3564 entry.tls_type = 0;
3565 }
f4416af6
AO
3566
3567 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3568
3569 if (! *loc)
3570 return FALSE;
143d77c5 3571
f4416af6
AO
3572 memcpy (*loc, &entry, sizeof entry);
3573
3574 return TRUE;
3575}
c224138d
RS
3576
3577/* Return the maximum number of GOT page entries required for RANGE. */
3578
3579static bfd_vma
3580mips_elf_pages_for_range (const struct mips_got_page_range *range)
3581{
3582 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3583}
3584
3a3b6725 3585/* Record that ABFD has a page relocation against symbol SYMNDX and
a8028dd0
RS
3586 that ADDEND is the addend for that relocation.
3587
3588 This function creates an upper bound on the number of GOT slots
3589 required; no attempt is made to combine references to non-overridable
3590 global symbols across multiple input files. */
c224138d
RS
3591
3592static bfd_boolean
a8028dd0
RS
3593mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3594 long symndx, bfd_signed_vma addend)
c224138d 3595{
a8028dd0
RS
3596 struct mips_elf_link_hash_table *htab;
3597 struct mips_got_info *g;
c224138d
RS
3598 struct mips_got_page_entry lookup, *entry;
3599 struct mips_got_page_range **range_ptr, *range;
3600 bfd_vma old_pages, new_pages;
3601 void **loc;
3602
a8028dd0
RS
3603 htab = mips_elf_hash_table (info);
3604 g = htab->got_info;
3605 BFD_ASSERT (g != NULL);
3606
c224138d
RS
3607 /* Find the mips_got_page_entry hash table entry for this symbol. */
3608 lookup.abfd = abfd;
3609 lookup.symndx = symndx;
3610 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3611 if (loc == NULL)
3612 return FALSE;
3613
3614 /* Create a mips_got_page_entry if this is the first time we've
3615 seen the symbol. */
3616 entry = (struct mips_got_page_entry *) *loc;
3617 if (!entry)
3618 {
3619 entry = bfd_alloc (abfd, sizeof (*entry));
3620 if (!entry)
3621 return FALSE;
3622
3623 entry->abfd = abfd;
3624 entry->symndx = symndx;
3625 entry->ranges = NULL;
3626 entry->num_pages = 0;
3627 *loc = entry;
3628 }
3629
3630 /* Skip over ranges whose maximum extent cannot share a page entry
3631 with ADDEND. */
3632 range_ptr = &entry->ranges;
3633 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3634 range_ptr = &(*range_ptr)->next;
3635
3636 /* If we scanned to the end of the list, or found a range whose
3637 minimum extent cannot share a page entry with ADDEND, create
3638 a new singleton range. */
3639 range = *range_ptr;
3640 if (!range || addend < range->min_addend - 0xffff)
3641 {
3642 range = bfd_alloc (abfd, sizeof (*range));
3643 if (!range)
3644 return FALSE;
3645
3646 range->next = *range_ptr;
3647 range->min_addend = addend;
3648 range->max_addend = addend;
3649
3650 *range_ptr = range;
3651 entry->num_pages++;
3652 g->page_gotno++;
3653 return TRUE;
3654 }
3655
3656 /* Remember how many pages the old range contributed. */
3657 old_pages = mips_elf_pages_for_range (range);
3658
3659 /* Update the ranges. */
3660 if (addend < range->min_addend)
3661 range->min_addend = addend;
3662 else if (addend > range->max_addend)
3663 {
3664 if (range->next && addend >= range->next->min_addend - 0xffff)
3665 {
3666 old_pages += mips_elf_pages_for_range (range->next);
3667 range->max_addend = range->next->max_addend;
3668 range->next = range->next->next;
3669 }
3670 else
3671 range->max_addend = addend;
3672 }
3673
3674 /* Record any change in the total estimate. */
3675 new_pages = mips_elf_pages_for_range (range);
3676 if (old_pages != new_pages)
3677 {
3678 entry->num_pages += new_pages - old_pages;
3679 g->page_gotno += new_pages - old_pages;
3680 }
3681
3682 return TRUE;
3683}
33bb52fb
RS
3684
3685/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3686
3687static void
3688mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3689 unsigned int n)
3690{
3691 asection *s;
3692 struct mips_elf_link_hash_table *htab;
3693
3694 htab = mips_elf_hash_table (info);
3695 s = mips_elf_rel_dyn_section (info, FALSE);
3696 BFD_ASSERT (s != NULL);
3697
3698 if (htab->is_vxworks)
3699 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3700 else
3701 {
3702 if (s->size == 0)
3703 {
3704 /* Make room for a null element. */
3705 s->size += MIPS_ELF_REL_SIZE (abfd);
3706 ++s->reloc_count;
3707 }
3708 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3709 }
3710}
3711\f
3712/* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3713 if the GOT entry is for an indirect or warning symbol. */
3714
3715static int
3716mips_elf_check_recreate_got (void **entryp, void *data)
3717{
3718 struct mips_got_entry *entry;
3719 bfd_boolean *must_recreate;
3720
3721 entry = (struct mips_got_entry *) *entryp;
3722 must_recreate = (bfd_boolean *) data;
3723 if (entry->abfd != NULL && entry->symndx == -1)
3724 {
3725 struct mips_elf_link_hash_entry *h;
3726
3727 h = entry->d.h;
3728 if (h->root.root.type == bfd_link_hash_indirect
3729 || h->root.root.type == bfd_link_hash_warning)
3730 {
3731 *must_recreate = TRUE;
3732 return 0;
3733 }
3734 }
3735 return 1;
3736}
3737
3738/* A htab_traverse callback for GOT entries. Add all entries to
3739 hash table *DATA, converting entries for indirect and warning
3740 symbols into entries for the target symbol. Set *DATA to null
3741 on error. */
3742
3743static int
3744mips_elf_recreate_got (void **entryp, void *data)
3745{
3746 htab_t *new_got;
3747 struct mips_got_entry *entry;
3748 void **slot;
3749
3750 new_got = (htab_t *) data;
3751 entry = (struct mips_got_entry *) *entryp;
3752 if (entry->abfd != NULL && entry->symndx == -1)
3753 {
3754 struct mips_elf_link_hash_entry *h;
3755
3756 h = entry->d.h;
3757 while (h->root.root.type == bfd_link_hash_indirect
3758 || h->root.root.type == bfd_link_hash_warning)
634835ae
RS
3759 {
3760 BFD_ASSERT (h->global_got_area == GGA_NONE);
3761 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3762 }
33bb52fb
RS
3763 entry->d.h = h;
3764 }
3765 slot = htab_find_slot (*new_got, entry, INSERT);
3766 if (slot == NULL)
3767 {
3768 *new_got = NULL;
3769 return 0;
3770 }
3771 if (*slot == NULL)
3772 *slot = entry;
3773 else
3774 free (entry);
3775 return 1;
3776}
3777
3778/* If any entries in G->got_entries are for indirect or warning symbols,
3779 replace them with entries for the target symbol. */
3780
3781static bfd_boolean
3782mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3783{
3784 bfd_boolean must_recreate;
3785 htab_t new_got;
3786
3787 must_recreate = FALSE;
3788 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3789 if (must_recreate)
3790 {
3791 new_got = htab_create (htab_size (g->got_entries),
3792 mips_elf_got_entry_hash,
3793 mips_elf_got_entry_eq, NULL);
3794 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3795 if (new_got == NULL)
3796 return FALSE;
3797
3798 /* Each entry in g->got_entries has either been copied to new_got
3799 or freed. Now delete the hash table itself. */
3800 htab_delete (g->got_entries);
3801 g->got_entries = new_got;
3802 }
3803 return TRUE;
3804}
3805
634835ae 3806/* A mips_elf_link_hash_traverse callback for which DATA points
d4596a51 3807 to a mips_got_info. Count the number of type (3) entries. */
33bb52fb
RS
3808
3809static int
d4596a51 3810mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb
RS
3811{
3812 struct mips_got_info *g;
3813
3814 g = (struct mips_got_info *) data;
d4596a51 3815 if (h->global_got_area != GGA_NONE)
33bb52fb 3816 {
d4596a51
RS
3817 if (h->root.forced_local || h->root.dynindx == -1)
3818 {
3819 /* We no longer need this entry if it was only used for
3820 relocations; those relocations will be against the
3821 null or section symbol instead of H. */
3822 if (h->global_got_area != GGA_RELOC_ONLY)
3823 g->local_gotno++;
3824 h->global_got_area = GGA_NONE;
3825 }
3826 else
23cc69b6
RS
3827 {
3828 g->global_gotno++;
3829 if (h->global_got_area == GGA_RELOC_ONLY)
3830 g->reloc_only_gotno++;
3831 }
33bb52fb
RS
3832 }
3833 return 1;
3834}
f4416af6
AO
3835\f
3836/* Compute the hash value of the bfd in a bfd2got hash entry. */
3837
3838static hashval_t
9719ad41 3839mips_elf_bfd2got_entry_hash (const void *entry_)
f4416af6
AO
3840{
3841 const struct mips_elf_bfd2got_hash *entry
3842 = (struct mips_elf_bfd2got_hash *)entry_;
3843
3844 return entry->bfd->id;
3845}
3846
3847/* Check whether two hash entries have the same bfd. */
3848
3849static int
9719ad41 3850mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3851{
3852 const struct mips_elf_bfd2got_hash *e1
3853 = (const struct mips_elf_bfd2got_hash *)entry1;
3854 const struct mips_elf_bfd2got_hash *e2
3855 = (const struct mips_elf_bfd2got_hash *)entry2;
3856
3857 return e1->bfd == e2->bfd;
3858}
3859
bad36eac 3860/* In a multi-got link, determine the GOT to be used for IBFD. G must
f4416af6
AO
3861 be the master GOT data. */
3862
3863static struct mips_got_info *
9719ad41 3864mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
3865{
3866 struct mips_elf_bfd2got_hash e, *p;
3867
3868 if (! g->bfd2got)
3869 return g;
3870
3871 e.bfd = ibfd;
9719ad41 3872 p = htab_find (g->bfd2got, &e);
f4416af6
AO
3873 return p ? p->g : NULL;
3874}
3875
c224138d
RS
3876/* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3877 Return NULL if an error occured. */
f4416af6 3878
c224138d
RS
3879static struct mips_got_info *
3880mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3881 bfd *input_bfd)
f4416af6 3882{
f4416af6 3883 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
c224138d 3884 struct mips_got_info *g;
f4416af6 3885 void **bfdgotp;
143d77c5 3886
c224138d 3887 bfdgot_entry.bfd = input_bfd;
f4416af6 3888 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
c224138d 3889 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
f4416af6 3890
c224138d 3891 if (bfdgot == NULL)
f4416af6 3892 {
c224138d
RS
3893 bfdgot = ((struct mips_elf_bfd2got_hash *)
3894 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
f4416af6 3895 if (bfdgot == NULL)
c224138d 3896 return NULL;
f4416af6
AO
3897
3898 *bfdgotp = bfdgot;
3899
c224138d
RS
3900 g = ((struct mips_got_info *)
3901 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
f4416af6 3902 if (g == NULL)
c224138d
RS
3903 return NULL;
3904
3905 bfdgot->bfd = input_bfd;
3906 bfdgot->g = g;
f4416af6
AO
3907
3908 g->global_gotsym = NULL;
3909 g->global_gotno = 0;
23cc69b6 3910 g->reloc_only_gotno = 0;
f4416af6 3911 g->local_gotno = 0;
c224138d 3912 g->page_gotno = 0;
f4416af6 3913 g->assigned_gotno = -1;
0f20cc35
DJ
3914 g->tls_gotno = 0;
3915 g->tls_assigned_gotno = 0;
3916 g->tls_ldm_offset = MINUS_ONE;
f4416af6 3917 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
9719ad41 3918 mips_elf_multi_got_entry_eq, NULL);
f4416af6 3919 if (g->got_entries == NULL)
c224138d
RS
3920 return NULL;
3921
3922 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3923 mips_got_page_entry_eq, NULL);
3924 if (g->got_page_entries == NULL)
3925 return NULL;
f4416af6
AO
3926
3927 g->bfd2got = NULL;
3928 g->next = NULL;
3929 }
3930
c224138d
RS
3931 return bfdgot->g;
3932}
3933
3934/* A htab_traverse callback for the entries in the master got.
3935 Create one separate got for each bfd that has entries in the global
3936 got, such that we can tell how many local and global entries each
3937 bfd requires. */
3938
3939static int
3940mips_elf_make_got_per_bfd (void **entryp, void *p)
3941{
3942 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3943 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3944 struct mips_got_info *g;
3945
3946 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3947 if (g == NULL)
3948 {
3949 arg->obfd = NULL;
3950 return 0;
3951 }
3952
f4416af6
AO
3953 /* Insert the GOT entry in the bfd's got entry hash table. */
3954 entryp = htab_find_slot (g->got_entries, entry, INSERT);
3955 if (*entryp != NULL)
3956 return 1;
143d77c5 3957
f4416af6
AO
3958 *entryp = entry;
3959
0f20cc35
DJ
3960 if (entry->tls_type)
3961 {
3962 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3963 g->tls_gotno += 2;
3964 if (entry->tls_type & GOT_TLS_IE)
3965 g->tls_gotno += 1;
3966 }
33bb52fb 3967 else if (entry->symndx >= 0 || entry->d.h->root.forced_local)
f4416af6
AO
3968 ++g->local_gotno;
3969 else
3970 ++g->global_gotno;
3971
3972 return 1;
3973}
3974
c224138d
RS
3975/* A htab_traverse callback for the page entries in the master got.
3976 Associate each page entry with the bfd's got. */
3977
3978static int
3979mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
3980{
3981 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
3982 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
3983 struct mips_got_info *g;
3984
3985 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3986 if (g == NULL)
3987 {
3988 arg->obfd = NULL;
3989 return 0;
3990 }
3991
3992 /* Insert the GOT entry in the bfd's got entry hash table. */
3993 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
3994 if (*entryp != NULL)
3995 return 1;
3996
3997 *entryp = entry;
3998 g->page_gotno += entry->num_pages;
3999 return 1;
4000}
4001
4002/* Consider merging the got described by BFD2GOT with TO, using the
4003 information given by ARG. Return -1 if this would lead to overflow,
4004 1 if they were merged successfully, and 0 if a merge failed due to
4005 lack of memory. (These values are chosen so that nonnegative return
4006 values can be returned by a htab_traverse callback.) */
4007
4008static int
4009mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4010 struct mips_got_info *to,
4011 struct mips_elf_got_per_bfd_arg *arg)
4012{
4013 struct mips_got_info *from = bfd2got->g;
4014 unsigned int estimate;
4015
4016 /* Work out how many page entries we would need for the combined GOT. */
4017 estimate = arg->max_pages;
4018 if (estimate >= from->page_gotno + to->page_gotno)
4019 estimate = from->page_gotno + to->page_gotno;
4020
4021 /* And conservatively estimate how many local, global and TLS entries
4022 would be needed. */
4023 estimate += (from->local_gotno
4024 + from->global_gotno
4025 + from->tls_gotno
4026 + to->local_gotno
4027 + to->global_gotno
4028 + to->tls_gotno);
4029
4030 /* Bail out if the combined GOT might be too big. */
4031 if (estimate > arg->max_count)
4032 return -1;
4033
4034 /* Commit to the merge. Record that TO is now the bfd for this got. */
4035 bfd2got->g = to;
4036
4037 /* Transfer the bfd's got information from FROM to TO. */
4038 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4039 if (arg->obfd == NULL)
4040 return 0;
4041
4042 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4043 if (arg->obfd == NULL)
4044 return 0;
4045
4046 /* We don't have to worry about releasing memory of the actual
4047 got entries, since they're all in the master got_entries hash
4048 table anyway. */
4049 htab_delete (from->got_entries);
4050 htab_delete (from->got_page_entries);
4051 return 1;
4052}
4053
f4416af6
AO
4054/* Attempt to merge gots of different input bfds. Try to use as much
4055 as possible of the primary got, since it doesn't require explicit
4056 dynamic relocations, but don't use bfds that would reference global
4057 symbols out of the addressable range. Failing the primary got,
4058 attempt to merge with the current got, or finish the current got
4059 and then make make the new got current. */
4060
4061static int
9719ad41 4062mips_elf_merge_gots (void **bfd2got_, void *p)
f4416af6
AO
4063{
4064 struct mips_elf_bfd2got_hash *bfd2got
4065 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4066 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
c224138d
RS
4067 struct mips_got_info *g;
4068 unsigned int estimate;
4069 int result;
4070
4071 g = bfd2got->g;
4072
4073 /* Work out the number of page, local and TLS entries. */
4074 estimate = arg->max_pages;
4075 if (estimate > g->page_gotno)
4076 estimate = g->page_gotno;
4077 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4078
4079 /* We place TLS GOT entries after both locals and globals. The globals
4080 for the primary GOT may overflow the normal GOT size limit, so be
4081 sure not to merge a GOT which requires TLS with the primary GOT in that
4082 case. This doesn't affect non-primary GOTs. */
c224138d 4083 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4084
c224138d 4085 if (estimate <= arg->max_count)
f4416af6 4086 {
c224138d
RS
4087 /* If we don't have a primary GOT, use it as
4088 a starting point for the primary GOT. */
4089 if (!arg->primary)
4090 {
4091 arg->primary = bfd2got->g;
4092 return 1;
4093 }
f4416af6 4094
c224138d
RS
4095 /* Try merging with the primary GOT. */
4096 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4097 if (result >= 0)
4098 return result;
f4416af6 4099 }
c224138d 4100
f4416af6 4101 /* If we can merge with the last-created got, do it. */
c224138d 4102 if (arg->current)
f4416af6 4103 {
c224138d
RS
4104 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4105 if (result >= 0)
4106 return result;
f4416af6 4107 }
c224138d 4108
f4416af6
AO
4109 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4110 fits; if it turns out that it doesn't, we'll get relocation
4111 overflows anyway. */
c224138d
RS
4112 g->next = arg->current;
4113 arg->current = g;
0f20cc35
DJ
4114
4115 return 1;
4116}
4117
ead49a57
RS
4118/* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4119 is null iff there is just a single GOT. */
0f20cc35
DJ
4120
4121static int
4122mips_elf_initialize_tls_index (void **entryp, void *p)
4123{
4124 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4125 struct mips_got_info *g = p;
ead49a57 4126 bfd_vma next_index;
cbf2cba4 4127 unsigned char tls_type;
0f20cc35
DJ
4128
4129 /* We're only interested in TLS symbols. */
4130 if (entry->tls_type == 0)
4131 return 1;
4132
ead49a57
RS
4133 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4134
4135 if (entry->symndx == -1 && g->next == NULL)
0f20cc35 4136 {
ead49a57
RS
4137 /* A type (3) got entry in the single-GOT case. We use the symbol's
4138 hash table entry to track its index. */
4139 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4140 return 1;
4141 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4142 entry->d.h->tls_got_offset = next_index;
cbf2cba4 4143 tls_type = entry->d.h->tls_type;
ead49a57
RS
4144 }
4145 else
4146 {
4147 if (entry->tls_type & GOT_TLS_LDM)
0f20cc35 4148 {
ead49a57
RS
4149 /* There are separate mips_got_entry objects for each input bfd
4150 that requires an LDM entry. Make sure that all LDM entries in
4151 a GOT resolve to the same index. */
4152 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4005427f 4153 {
ead49a57 4154 entry->gotidx = g->tls_ldm_offset;
4005427f
RS
4155 return 1;
4156 }
ead49a57 4157 g->tls_ldm_offset = next_index;
0f20cc35 4158 }
ead49a57 4159 entry->gotidx = next_index;
cbf2cba4 4160 tls_type = entry->tls_type;
f4416af6
AO
4161 }
4162
ead49a57 4163 /* Account for the entries we've just allocated. */
cbf2cba4 4164 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
0f20cc35 4165 g->tls_assigned_gotno += 2;
cbf2cba4 4166 if (tls_type & GOT_TLS_IE)
0f20cc35
DJ
4167 g->tls_assigned_gotno += 1;
4168
f4416af6
AO
4169 return 1;
4170}
4171
4172/* If passed a NULL mips_got_info in the argument, set the marker used
4173 to tell whether a global symbol needs a got entry (in the primary
4174 got) to the given VALUE.
4175
4176 If passed a pointer G to a mips_got_info in the argument (it must
4177 not be the primary GOT), compute the offset from the beginning of
4178 the (primary) GOT section to the entry in G corresponding to the
4179 global symbol. G's assigned_gotno must contain the index of the
4180 first available global GOT entry in G. VALUE must contain the size
4181 of a GOT entry in bytes. For each global GOT entry that requires a
4182 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4cc11e76 4183 marked as not eligible for lazy resolution through a function
f4416af6
AO
4184 stub. */
4185static int
9719ad41 4186mips_elf_set_global_got_offset (void **entryp, void *p)
f4416af6
AO
4187{
4188 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4189 struct mips_elf_set_global_got_offset_arg *arg
4190 = (struct mips_elf_set_global_got_offset_arg *)p;
4191 struct mips_got_info *g = arg->g;
4192
0f20cc35
DJ
4193 if (g && entry->tls_type != GOT_NORMAL)
4194 arg->needed_relocs +=
4195 mips_tls_got_relocs (arg->info, entry->tls_type,
4196 entry->symndx == -1 ? &entry->d.h->root : NULL);
4197
634835ae
RS
4198 if (entry->abfd != NULL
4199 && entry->symndx == -1
4200 && entry->d.h->global_got_area != GGA_NONE)
f4416af6
AO
4201 {
4202 if (g)
4203 {
4204 BFD_ASSERT (g->global_gotsym == NULL);
4205
4206 entry->gotidx = arg->value * (long) g->assigned_gotno++;
f4416af6
AO
4207 if (arg->info->shared
4208 || (elf_hash_table (arg->info)->dynamic_sections_created
f5385ebf
AM
4209 && entry->d.h->root.def_dynamic
4210 && !entry->d.h->root.def_regular))
f4416af6
AO
4211 ++arg->needed_relocs;
4212 }
4213 else
634835ae 4214 entry->d.h->global_got_area = arg->value;
f4416af6
AO
4215 }
4216
4217 return 1;
4218}
4219
33bb52fb
RS
4220/* A htab_traverse callback for GOT entries for which DATA is the
4221 bfd_link_info. Forbid any global symbols from having traditional
4222 lazy-binding stubs. */
4223
0626d451 4224static int
33bb52fb 4225mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4226{
33bb52fb
RS
4227 struct bfd_link_info *info;
4228 struct mips_elf_link_hash_table *htab;
4229 struct mips_got_entry *entry;
0626d451 4230
33bb52fb
RS
4231 entry = (struct mips_got_entry *) *entryp;
4232 info = (struct bfd_link_info *) data;
4233 htab = mips_elf_hash_table (info);
0626d451
RS
4234 if (entry->abfd != NULL
4235 && entry->symndx == -1
33bb52fb 4236 && entry->d.h->needs_lazy_stub)
f4416af6 4237 {
33bb52fb
RS
4238 entry->d.h->needs_lazy_stub = FALSE;
4239 htab->lazy_stub_count--;
f4416af6 4240 }
143d77c5 4241
f4416af6
AO
4242 return 1;
4243}
4244
f4416af6
AO
4245/* Return the offset of an input bfd IBFD's GOT from the beginning of
4246 the primary GOT. */
4247static bfd_vma
9719ad41 4248mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
4249{
4250 if (g->bfd2got == NULL)
4251 return 0;
4252
4253 g = mips_elf_got_for_ibfd (g, ibfd);
4254 if (! g)
4255 return 0;
4256
4257 BFD_ASSERT (g->next);
4258
4259 g = g->next;
143d77c5 4260
0f20cc35
DJ
4261 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4262 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4263}
4264
4265/* Turn a single GOT that is too big for 16-bit addressing into
4266 a sequence of GOTs, each one 16-bit addressable. */
4267
4268static bfd_boolean
9719ad41 4269mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4270 asection *got, bfd_size_type pages)
f4416af6 4271{
a8028dd0 4272 struct mips_elf_link_hash_table *htab;
f4416af6
AO
4273 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4274 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
a8028dd0 4275 struct mips_got_info *g, *gg;
33bb52fb
RS
4276 unsigned int assign, needed_relocs;
4277 bfd *dynobj;
f4416af6 4278
33bb52fb 4279 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
4280 htab = mips_elf_hash_table (info);
4281 g = htab->got_info;
f4416af6 4282 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
9719ad41 4283 mips_elf_bfd2got_entry_eq, NULL);
f4416af6
AO
4284 if (g->bfd2got == NULL)
4285 return FALSE;
4286
4287 got_per_bfd_arg.bfd2got = g->bfd2got;
4288 got_per_bfd_arg.obfd = abfd;
4289 got_per_bfd_arg.info = info;
4290
4291 /* Count how many GOT entries each input bfd requires, creating a
4292 map from bfd to got info while at that. */
f4416af6
AO
4293 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4294 if (got_per_bfd_arg.obfd == NULL)
4295 return FALSE;
4296
c224138d
RS
4297 /* Also count how many page entries each input bfd requires. */
4298 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4299 &got_per_bfd_arg);
4300 if (got_per_bfd_arg.obfd == NULL)
4301 return FALSE;
4302
f4416af6
AO
4303 got_per_bfd_arg.current = NULL;
4304 got_per_bfd_arg.primary = NULL;
0a44bf69 4305 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4306 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4307 - htab->reserved_gotno);
c224138d 4308 got_per_bfd_arg.max_pages = pages;
0f20cc35
DJ
4309 /* The number of globals that will be included in the primary GOT.
4310 See the calls to mips_elf_set_global_got_offset below for more
4311 information. */
4312 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4313
4314 /* Try to merge the GOTs of input bfds together, as long as they
4315 don't seem to exceed the maximum GOT size, choosing one of them
4316 to be the primary GOT. */
4317 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4318 if (got_per_bfd_arg.obfd == NULL)
4319 return FALSE;
4320
0f20cc35 4321 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6
AO
4322 if (got_per_bfd_arg.primary == NULL)
4323 {
4324 g->next = (struct mips_got_info *)
4325 bfd_alloc (abfd, sizeof (struct mips_got_info));
4326 if (g->next == NULL)
4327 return FALSE;
4328
4329 g->next->global_gotsym = NULL;
4330 g->next->global_gotno = 0;
23cc69b6 4331 g->next->reloc_only_gotno = 0;
f4416af6 4332 g->next->local_gotno = 0;
c224138d 4333 g->next->page_gotno = 0;
0f20cc35 4334 g->next->tls_gotno = 0;
f4416af6 4335 g->next->assigned_gotno = 0;
0f20cc35
DJ
4336 g->next->tls_assigned_gotno = 0;
4337 g->next->tls_ldm_offset = MINUS_ONE;
f4416af6
AO
4338 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4339 mips_elf_multi_got_entry_eq,
9719ad41 4340 NULL);
f4416af6
AO
4341 if (g->next->got_entries == NULL)
4342 return FALSE;
c224138d
RS
4343 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4344 mips_got_page_entry_eq,
4345 NULL);
4346 if (g->next->got_page_entries == NULL)
4347 return FALSE;
f4416af6
AO
4348 g->next->bfd2got = NULL;
4349 }
4350 else
4351 g->next = got_per_bfd_arg.primary;
4352 g->next->next = got_per_bfd_arg.current;
4353
4354 /* GG is now the master GOT, and G is the primary GOT. */
4355 gg = g;
4356 g = g->next;
4357
4358 /* Map the output bfd to the primary got. That's what we're going
4359 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4360 didn't mark in check_relocs, and we want a quick way to find it.
4361 We can't just use gg->next because we're going to reverse the
4362 list. */
4363 {
4364 struct mips_elf_bfd2got_hash *bfdgot;
4365 void **bfdgotp;
143d77c5 4366
f4416af6
AO
4367 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4368 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4369
4370 if (bfdgot == NULL)
4371 return FALSE;
4372
4373 bfdgot->bfd = abfd;
4374 bfdgot->g = g;
4375 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4376
4377 BFD_ASSERT (*bfdgotp == NULL);
4378 *bfdgotp = bfdgot;
4379 }
4380
634835ae
RS
4381 /* Every symbol that is referenced in a dynamic relocation must be
4382 present in the primary GOT, so arrange for them to appear after
4383 those that are actually referenced. */
23cc69b6 4384 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4385 g->global_gotno = gg->global_gotno;
f4416af6 4386
f4416af6 4387 set_got_offset_arg.g = NULL;
634835ae 4388 set_got_offset_arg.value = GGA_RELOC_ONLY;
f4416af6
AO
4389 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4390 &set_got_offset_arg);
634835ae 4391 set_got_offset_arg.value = GGA_NORMAL;
f4416af6
AO
4392 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4393 &set_got_offset_arg);
f4416af6
AO
4394
4395 /* Now go through the GOTs assigning them offset ranges.
4396 [assigned_gotno, local_gotno[ will be set to the range of local
4397 entries in each GOT. We can then compute the end of a GOT by
4398 adding local_gotno to global_gotno. We reverse the list and make
4399 it circular since then we'll be able to quickly compute the
4400 beginning of a GOT, by computing the end of its predecessor. To
4401 avoid special cases for the primary GOT, while still preserving
4402 assertions that are valid for both single- and multi-got links,
4403 we arrange for the main got struct to have the right number of
4404 global entries, but set its local_gotno such that the initial
4405 offset of the primary GOT is zero. Remember that the primary GOT
4406 will become the last item in the circular linked list, so it
4407 points back to the master GOT. */
4408 gg->local_gotno = -g->global_gotno;
4409 gg->global_gotno = g->global_gotno;
0f20cc35 4410 gg->tls_gotno = 0;
f4416af6
AO
4411 assign = 0;
4412 gg->next = gg;
4413
4414 do
4415 {
4416 struct mips_got_info *gn;
4417
861fb55a 4418 assign += htab->reserved_gotno;
f4416af6 4419 g->assigned_gotno = assign;
c224138d
RS
4420 g->local_gotno += assign;
4421 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4422 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4423
ead49a57
RS
4424 /* Take g out of the direct list, and push it onto the reversed
4425 list that gg points to. g->next is guaranteed to be nonnull after
4426 this operation, as required by mips_elf_initialize_tls_index. */
4427 gn = g->next;
4428 g->next = gg->next;
4429 gg->next = g;
4430
0f20cc35
DJ
4431 /* Set up any TLS entries. We always place the TLS entries after
4432 all non-TLS entries. */
4433 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4434 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
f4416af6 4435
ead49a57 4436 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4437 g = gn;
0626d451 4438
33bb52fb
RS
4439 /* Forbid global symbols in every non-primary GOT from having
4440 lazy-binding stubs. */
0626d451 4441 if (g)
33bb52fb 4442 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4443 }
4444 while (g);
4445
eea6121a 4446 got->size = (gg->next->local_gotno
33bb52fb
RS
4447 + gg->next->global_gotno
4448 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4449
4450 needed_relocs = 0;
4451 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4452 set_got_offset_arg.info = info;
4453 for (g = gg->next; g && g->next != gg; g = g->next)
4454 {
4455 unsigned int save_assign;
4456
4457 /* Assign offsets to global GOT entries. */
4458 save_assign = g->assigned_gotno;
4459 g->assigned_gotno = g->local_gotno;
4460 set_got_offset_arg.g = g;
4461 set_got_offset_arg.needed_relocs = 0;
4462 htab_traverse (g->got_entries,
4463 mips_elf_set_global_got_offset,
4464 &set_got_offset_arg);
4465 needed_relocs += set_got_offset_arg.needed_relocs;
4466 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4467
4468 g->assigned_gotno = save_assign;
4469 if (info->shared)
4470 {
4471 needed_relocs += g->local_gotno - g->assigned_gotno;
4472 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4473 + g->next->global_gotno
4474 + g->next->tls_gotno
861fb55a 4475 + htab->reserved_gotno);
33bb52fb
RS
4476 }
4477 }
4478
4479 if (needed_relocs)
4480 mips_elf_allocate_dynamic_relocations (dynobj, info,
4481 needed_relocs);
143d77c5 4482
f4416af6
AO
4483 return TRUE;
4484}
143d77c5 4485
b49e97c9
TS
4486\f
4487/* Returns the first relocation of type r_type found, beginning with
4488 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4489
4490static const Elf_Internal_Rela *
9719ad41
RS
4491mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4492 const Elf_Internal_Rela *relocation,
4493 const Elf_Internal_Rela *relend)
b49e97c9 4494{
c000e262
TS
4495 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4496
b49e97c9
TS
4497 while (relocation < relend)
4498 {
c000e262
TS
4499 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4500 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4501 return relocation;
4502
4503 ++relocation;
4504 }
4505
4506 /* We didn't find it. */
b49e97c9
TS
4507 return NULL;
4508}
4509
4510/* Return whether a relocation is against a local symbol. */
4511
b34976b6 4512static bfd_boolean
9719ad41
RS
4513mips_elf_local_relocation_p (bfd *input_bfd,
4514 const Elf_Internal_Rela *relocation,
4515 asection **local_sections,
4516 bfd_boolean check_forced)
b49e97c9
TS
4517{
4518 unsigned long r_symndx;
4519 Elf_Internal_Shdr *symtab_hdr;
4520 struct mips_elf_link_hash_entry *h;
4521 size_t extsymoff;
4522
4523 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4524 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4525 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4526
4527 if (r_symndx < extsymoff)
b34976b6 4528 return TRUE;
b49e97c9 4529 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4530 return TRUE;
b49e97c9
TS
4531
4532 if (check_forced)
4533 {
4534 /* Look up the hash table to check whether the symbol
4535 was forced local. */
4536 h = (struct mips_elf_link_hash_entry *)
4537 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
4538 /* Find the real hash-table entry for this symbol. */
4539 while (h->root.root.type == bfd_link_hash_indirect
4540 || h->root.root.type == bfd_link_hash_warning)
4541 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
f5385ebf 4542 if (h->root.forced_local)
b34976b6 4543 return TRUE;
b49e97c9
TS
4544 }
4545
b34976b6 4546 return FALSE;
b49e97c9
TS
4547}
4548\f
4549/* Sign-extend VALUE, which has the indicated number of BITS. */
4550
a7ebbfdf 4551bfd_vma
9719ad41 4552_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4553{
4554 if (value & ((bfd_vma) 1 << (bits - 1)))
4555 /* VALUE is negative. */
4556 value |= ((bfd_vma) - 1) << bits;
4557
4558 return value;
4559}
4560
4561/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4562 range expressible by a signed number with the indicated number of
b49e97c9
TS
4563 BITS. */
4564
b34976b6 4565static bfd_boolean
9719ad41 4566mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4567{
4568 bfd_signed_vma svalue = (bfd_signed_vma) value;
4569
4570 if (svalue > (1 << (bits - 1)) - 1)
4571 /* The value is too big. */
b34976b6 4572 return TRUE;
b49e97c9
TS
4573 else if (svalue < -(1 << (bits - 1)))
4574 /* The value is too small. */
b34976b6 4575 return TRUE;
b49e97c9
TS
4576
4577 /* All is well. */
b34976b6 4578 return FALSE;
b49e97c9
TS
4579}
4580
4581/* Calculate the %high function. */
4582
4583static bfd_vma
9719ad41 4584mips_elf_high (bfd_vma value)
b49e97c9
TS
4585{
4586 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4587}
4588
4589/* Calculate the %higher function. */
4590
4591static bfd_vma
9719ad41 4592mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4593{
4594#ifdef BFD64
4595 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4596#else
4597 abort ();
c5ae1840 4598 return MINUS_ONE;
b49e97c9
TS
4599#endif
4600}
4601
4602/* Calculate the %highest function. */
4603
4604static bfd_vma
9719ad41 4605mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4606{
4607#ifdef BFD64
b15e6682 4608 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4609#else
4610 abort ();
c5ae1840 4611 return MINUS_ONE;
b49e97c9
TS
4612#endif
4613}
4614\f
4615/* Create the .compact_rel section. */
4616
b34976b6 4617static bfd_boolean
9719ad41
RS
4618mips_elf_create_compact_rel_section
4619 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4620{
4621 flagword flags;
4622 register asection *s;
4623
4624 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4625 {
4626 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4627 | SEC_READONLY);
4628
3496cb2a 4629 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4630 if (s == NULL
b49e97c9
TS
4631 || ! bfd_set_section_alignment (abfd, s,
4632 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4633 return FALSE;
b49e97c9 4634
eea6121a 4635 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4636 }
4637
b34976b6 4638 return TRUE;
b49e97c9
TS
4639}
4640
4641/* Create the .got section to hold the global offset table. */
4642
b34976b6 4643static bfd_boolean
23cc69b6 4644mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
4645{
4646 flagword flags;
4647 register asection *s;
4648 struct elf_link_hash_entry *h;
14a793b2 4649 struct bfd_link_hash_entry *bh;
b49e97c9
TS
4650 struct mips_got_info *g;
4651 bfd_size_type amt;
0a44bf69
RS
4652 struct mips_elf_link_hash_table *htab;
4653
4654 htab = mips_elf_hash_table (info);
b49e97c9
TS
4655
4656 /* This function may be called more than once. */
23cc69b6
RS
4657 if (htab->sgot)
4658 return TRUE;
b49e97c9
TS
4659
4660 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4661 | SEC_LINKER_CREATED);
4662
72b4917c
TS
4663 /* We have to use an alignment of 2**4 here because this is hardcoded
4664 in the function stub generation and in the linker script. */
3496cb2a 4665 s = bfd_make_section_with_flags (abfd, ".got", flags);
b49e97c9 4666 if (s == NULL
72b4917c 4667 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 4668 return FALSE;
a8028dd0 4669 htab->sgot = s;
b49e97c9
TS
4670
4671 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4672 linker script because we don't want to define the symbol if we
4673 are not creating a global offset table. */
14a793b2 4674 bh = NULL;
b49e97c9
TS
4675 if (! (_bfd_generic_link_add_one_symbol
4676 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 4677 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 4678 return FALSE;
14a793b2
AM
4679
4680 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
4681 h->non_elf = 0;
4682 h->def_regular = 1;
b49e97c9 4683 h->type = STT_OBJECT;
d329bcd1 4684 elf_hash_table (info)->hgot = h;
b49e97c9
TS
4685
4686 if (info->shared
c152c796 4687 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4688 return FALSE;
b49e97c9 4689
b49e97c9 4690 amt = sizeof (struct mips_got_info);
9719ad41 4691 g = bfd_alloc (abfd, amt);
b49e97c9 4692 if (g == NULL)
b34976b6 4693 return FALSE;
b49e97c9 4694 g->global_gotsym = NULL;
e3d54347 4695 g->global_gotno = 0;
23cc69b6 4696 g->reloc_only_gotno = 0;
0f20cc35 4697 g->tls_gotno = 0;
861fb55a 4698 g->local_gotno = 0;
c224138d 4699 g->page_gotno = 0;
861fb55a 4700 g->assigned_gotno = 0;
f4416af6
AO
4701 g->bfd2got = NULL;
4702 g->next = NULL;
0f20cc35 4703 g->tls_ldm_offset = MINUS_ONE;
b15e6682 4704 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
9719ad41 4705 mips_elf_got_entry_eq, NULL);
b15e6682
AO
4706 if (g->got_entries == NULL)
4707 return FALSE;
c224138d
RS
4708 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4709 mips_got_page_entry_eq, NULL);
4710 if (g->got_page_entries == NULL)
4711 return FALSE;
a8028dd0 4712 htab->got_info = g;
f0abc2a1 4713 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
4714 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4715
861fb55a
DJ
4716 /* We also need a .got.plt section when generating PLTs. */
4717 s = bfd_make_section_with_flags (abfd, ".got.plt",
4718 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4719 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4720 if (s == NULL)
4721 return FALSE;
4722 htab->sgotplt = s;
0a44bf69 4723
b34976b6 4724 return TRUE;
b49e97c9 4725}
b49e97c9 4726\f
0a44bf69
RS
4727/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4728 __GOTT_INDEX__ symbols. These symbols are only special for
4729 shared objects; they are not used in executables. */
4730
4731static bfd_boolean
4732is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4733{
4734 return (mips_elf_hash_table (info)->is_vxworks
4735 && info->shared
4736 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4737 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4738}
861fb55a
DJ
4739
4740/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4741 require an la25 stub. See also mips_elf_local_pic_function_p,
4742 which determines whether the destination function ever requires a
4743 stub. */
4744
4745static bfd_boolean
4746mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4747{
4748 /* We specifically ignore branches and jumps from EF_PIC objects,
4749 where the onus is on the compiler or programmer to perform any
4750 necessary initialization of $25. Sometimes such initialization
4751 is unnecessary; for example, -mno-shared functions do not use
4752 the incoming value of $25, and may therefore be called directly. */
4753 if (PIC_OBJECT_P (input_bfd))
4754 return FALSE;
4755
4756 switch (r_type)
4757 {
4758 case R_MIPS_26:
4759 case R_MIPS_PC16:
4760 case R_MIPS16_26:
4761 return TRUE;
4762
4763 default:
4764 return FALSE;
4765 }
4766}
0a44bf69 4767\f
b49e97c9
TS
4768/* Calculate the value produced by the RELOCATION (which comes from
4769 the INPUT_BFD). The ADDEND is the addend to use for this
4770 RELOCATION; RELOCATION->R_ADDEND is ignored.
4771
4772 The result of the relocation calculation is stored in VALUEP.
4773 REQUIRE_JALXP indicates whether or not the opcode used with this
4774 relocation must be JALX.
4775
4776 This function returns bfd_reloc_continue if the caller need take no
4777 further action regarding this relocation, bfd_reloc_notsupported if
4778 something goes dramatically wrong, bfd_reloc_overflow if an
4779 overflow occurs, and bfd_reloc_ok to indicate success. */
4780
4781static bfd_reloc_status_type
9719ad41
RS
4782mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4783 asection *input_section,
4784 struct bfd_link_info *info,
4785 const Elf_Internal_Rela *relocation,
4786 bfd_vma addend, reloc_howto_type *howto,
4787 Elf_Internal_Sym *local_syms,
4788 asection **local_sections, bfd_vma *valuep,
4789 const char **namep, bfd_boolean *require_jalxp,
4790 bfd_boolean save_addend)
b49e97c9
TS
4791{
4792 /* The eventual value we will return. */
4793 bfd_vma value;
4794 /* The address of the symbol against which the relocation is
4795 occurring. */
4796 bfd_vma symbol = 0;
4797 /* The final GP value to be used for the relocatable, executable, or
4798 shared object file being produced. */
0a61c8c2 4799 bfd_vma gp;
b49e97c9
TS
4800 /* The place (section offset or address) of the storage unit being
4801 relocated. */
4802 bfd_vma p;
4803 /* The value of GP used to create the relocatable object. */
0a61c8c2 4804 bfd_vma gp0;
b49e97c9
TS
4805 /* The offset into the global offset table at which the address of
4806 the relocation entry symbol, adjusted by the addend, resides
4807 during execution. */
4808 bfd_vma g = MINUS_ONE;
4809 /* The section in which the symbol referenced by the relocation is
4810 located. */
4811 asection *sec = NULL;
4812 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 4813 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 4814 symbol. */
b34976b6
AM
4815 bfd_boolean local_p, was_local_p;
4816 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4817 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
4818 /* TRUE if the symbol referred to by this relocation is
4819 "__gnu_local_gp". */
4820 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
4821 Elf_Internal_Shdr *symtab_hdr;
4822 size_t extsymoff;
4823 unsigned long r_symndx;
4824 int r_type;
b34976b6 4825 /* TRUE if overflow occurred during the calculation of the
b49e97c9 4826 relocation value. */
b34976b6
AM
4827 bfd_boolean overflowed_p;
4828 /* TRUE if this relocation refers to a MIPS16 function. */
4829 bfd_boolean target_is_16_bit_code_p = FALSE;
0a44bf69
RS
4830 struct mips_elf_link_hash_table *htab;
4831 bfd *dynobj;
4832
4833 dynobj = elf_hash_table (info)->dynobj;
4834 htab = mips_elf_hash_table (info);
b49e97c9
TS
4835
4836 /* Parse the relocation. */
4837 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4838 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4839 p = (input_section->output_section->vma
4840 + input_section->output_offset
4841 + relocation->r_offset);
4842
4843 /* Assume that there will be no overflow. */
b34976b6 4844 overflowed_p = FALSE;
b49e97c9
TS
4845
4846 /* Figure out whether or not the symbol is local, and get the offset
4847 used in the array of hash table entries. */
4848 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4849 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 4850 local_sections, FALSE);
bce03d3d 4851 was_local_p = local_p;
b49e97c9
TS
4852 if (! elf_bad_symtab (input_bfd))
4853 extsymoff = symtab_hdr->sh_info;
4854 else
4855 {
4856 /* The symbol table does not follow the rule that local symbols
4857 must come before globals. */
4858 extsymoff = 0;
4859 }
4860
4861 /* Figure out the value of the symbol. */
4862 if (local_p)
4863 {
4864 Elf_Internal_Sym *sym;
4865
4866 sym = local_syms + r_symndx;
4867 sec = local_sections[r_symndx];
4868
4869 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
4870 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4871 || (sec->flags & SEC_MERGE))
b49e97c9 4872 symbol += sym->st_value;
d4df96e6
L
4873 if ((sec->flags & SEC_MERGE)
4874 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4875 {
4876 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4877 addend -= symbol;
4878 addend += sec->output_section->vma + sec->output_offset;
4879 }
b49e97c9
TS
4880
4881 /* MIPS16 text labels should be treated as odd. */
30c09090 4882 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
4883 ++symbol;
4884
4885 /* Record the name of this symbol, for our caller. */
4886 *namep = bfd_elf_string_from_elf_section (input_bfd,
4887 symtab_hdr->sh_link,
4888 sym->st_name);
4889 if (*namep == '\0')
4890 *namep = bfd_section_name (input_bfd, sec);
4891
30c09090 4892 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
b49e97c9
TS
4893 }
4894 else
4895 {
560e09e9
NC
4896 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4897
b49e97c9
TS
4898 /* For global symbols we look up the symbol in the hash-table. */
4899 h = ((struct mips_elf_link_hash_entry *)
4900 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4901 /* Find the real hash-table entry for this symbol. */
4902 while (h->root.root.type == bfd_link_hash_indirect
4903 || h->root.root.type == bfd_link_hash_warning)
4904 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4905
4906 /* Record the name of this symbol, for our caller. */
4907 *namep = h->root.root.root.string;
4908
4909 /* See if this is the special _gp_disp symbol. Note that such a
4910 symbol must always be a global symbol. */
560e09e9 4911 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
4912 && ! NEWABI_P (input_bfd))
4913 {
4914 /* Relocations against _gp_disp are permitted only with
4915 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 4916 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
4917 return bfd_reloc_notsupported;
4918
b34976b6 4919 gp_disp_p = TRUE;
b49e97c9 4920 }
bbe506e8
TS
4921 /* See if this is the special _gp symbol. Note that such a
4922 symbol must always be a global symbol. */
4923 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4924 gnu_local_gp_p = TRUE;
4925
4926
b49e97c9
TS
4927 /* If this symbol is defined, calculate its address. Note that
4928 _gp_disp is a magic symbol, always implicitly defined by the
4929 linker, so it's inappropriate to check to see whether or not
4930 its defined. */
4931 else if ((h->root.root.type == bfd_link_hash_defined
4932 || h->root.root.type == bfd_link_hash_defweak)
4933 && h->root.root.u.def.section)
4934 {
4935 sec = h->root.root.u.def.section;
4936 if (sec->output_section)
4937 symbol = (h->root.root.u.def.value
4938 + sec->output_section->vma
4939 + sec->output_offset);
4940 else
4941 symbol = h->root.root.u.def.value;
4942 }
4943 else if (h->root.root.type == bfd_link_hash_undefweak)
4944 /* We allow relocations against undefined weak symbols, giving
4945 it the value zero, so that you can undefined weak functions
4946 and check to see if they exist by looking at their
4947 addresses. */
4948 symbol = 0;
59c2e50f 4949 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
4950 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4951 symbol = 0;
a4d0f181
TS
4952 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4953 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
4954 {
4955 /* If this is a dynamic link, we should have created a
4956 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4957 in in _bfd_mips_elf_create_dynamic_sections.
4958 Otherwise, we should define the symbol with a value of 0.
4959 FIXME: It should probably get into the symbol table
4960 somehow as well. */
4961 BFD_ASSERT (! info->shared);
4962 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4963 symbol = 0;
4964 }
5e2b0d47
NC
4965 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4966 {
4967 /* This is an optional symbol - an Irix specific extension to the
4968 ELF spec. Ignore it for now.
4969 XXX - FIXME - there is more to the spec for OPTIONAL symbols
4970 than simply ignoring them, but we do not handle this for now.
4971 For information see the "64-bit ELF Object File Specification"
4972 which is available from here:
4973 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
4974 symbol = 0;
4975 }
e7e2196d
MR
4976 else if ((*info->callbacks->undefined_symbol)
4977 (info, h->root.root.root.string, input_bfd,
4978 input_section, relocation->r_offset,
4979 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4980 || ELF_ST_VISIBILITY (h->root.other)))
4981 {
4982 return bfd_reloc_undefined;
4983 }
b49e97c9
TS
4984 else
4985 {
e7e2196d 4986 return bfd_reloc_notsupported;
b49e97c9
TS
4987 }
4988
30c09090 4989 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
b49e97c9
TS
4990 }
4991
738e5348
RS
4992 /* If this is a reference to a 16-bit function with a stub, we need
4993 to redirect the relocation to the stub unless:
4994
4995 (a) the relocation is for a MIPS16 JAL;
4996
4997 (b) the relocation is for a MIPS16 PIC call, and there are no
4998 non-MIPS16 uses of the GOT slot; or
4999
5000 (c) the section allows direct references to MIPS16 functions. */
5001 if (r_type != R_MIPS16_26
5002 && !info->relocatable
5003 && ((h != NULL
5004 && h->fn_stub != NULL
5005 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71
TS
5006 || (local_p
5007 && elf_tdata (input_bfd)->local_stubs != NULL
b49e97c9 5008 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5009 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5010 {
5011 /* This is a 32- or 64-bit call to a 16-bit function. We should
5012 have already noticed that we were going to need the
5013 stub. */
5014 if (local_p)
5015 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5016 else
5017 {
5018 BFD_ASSERT (h->need_fn_stub);
5019 sec = h->fn_stub;
5020 }
5021
5022 symbol = sec->output_section->vma + sec->output_offset;
f38c2df5
TS
5023 /* The target is 16-bit, but the stub isn't. */
5024 target_is_16_bit_code_p = FALSE;
b49e97c9
TS
5025 }
5026 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
738e5348
RS
5027 need to redirect the call to the stub. Note that we specifically
5028 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5029 use an indirect stub instead. */
1049f94e 5030 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5031 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71
TS
5032 || (local_p
5033 && elf_tdata (input_bfd)->local_call_stubs != NULL
5034 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
b49e97c9
TS
5035 && !target_is_16_bit_code_p)
5036 {
b9d58d71
TS
5037 if (local_p)
5038 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5039 else
b49e97c9 5040 {
b9d58d71
TS
5041 /* If both call_stub and call_fp_stub are defined, we can figure
5042 out which one to use by checking which one appears in the input
5043 file. */
5044 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5045 {
b9d58d71
TS
5046 asection *o;
5047
5048 sec = NULL;
5049 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5050 {
b9d58d71
TS
5051 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5052 {
5053 sec = h->call_fp_stub;
5054 break;
5055 }
b49e97c9 5056 }
b9d58d71
TS
5057 if (sec == NULL)
5058 sec = h->call_stub;
b49e97c9 5059 }
b9d58d71 5060 else if (h->call_stub != NULL)
b49e97c9 5061 sec = h->call_stub;
b9d58d71
TS
5062 else
5063 sec = h->call_fp_stub;
5064 }
b49e97c9 5065
eea6121a 5066 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5067 symbol = sec->output_section->vma + sec->output_offset;
5068 }
861fb55a
DJ
5069 /* If this is a direct call to a PIC function, redirect to the
5070 non-PIC stub. */
5071 else if (h != NULL && h->la25_stub
5072 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5073 symbol = (h->la25_stub->stub_section->output_section->vma
5074 + h->la25_stub->stub_section->output_offset
5075 + h->la25_stub->offset);
b49e97c9
TS
5076
5077 /* Calls from 16-bit code to 32-bit code and vice versa require the
5078 special jalx instruction. */
1049f94e 5079 *require_jalxp = (!info->relocatable
b49e97c9
TS
5080 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
5081 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
5082
5083 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 5084 local_sections, TRUE);
b49e97c9 5085
0a61c8c2
RS
5086 gp0 = _bfd_get_gp_value (input_bfd);
5087 gp = _bfd_get_gp_value (abfd);
23cc69b6 5088 if (htab->got_info)
a8028dd0 5089 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5090
5091 if (gnu_local_gp_p)
5092 symbol = gp;
5093
5094 /* If we haven't already determined the GOT offset, oand we're going
5095 to need it, get it now. */
b49e97c9
TS
5096 switch (r_type)
5097 {
0fdc1bf1 5098 case R_MIPS_GOT_PAGE:
93a2b7ae 5099 case R_MIPS_GOT_OFST:
d25aed71
RS
5100 /* We need to decay to GOT_DISP/addend if the symbol doesn't
5101 bind locally. */
5102 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
93a2b7ae 5103 if (local_p || r_type == R_MIPS_GOT_OFST)
0fdc1bf1
AO
5104 break;
5105 /* Fall through. */
5106
738e5348
RS
5107 case R_MIPS16_CALL16:
5108 case R_MIPS16_GOT16:
b49e97c9
TS
5109 case R_MIPS_CALL16:
5110 case R_MIPS_GOT16:
5111 case R_MIPS_GOT_DISP:
5112 case R_MIPS_GOT_HI16:
5113 case R_MIPS_CALL_HI16:
5114 case R_MIPS_GOT_LO16:
5115 case R_MIPS_CALL_LO16:
0f20cc35
DJ
5116 case R_MIPS_TLS_GD:
5117 case R_MIPS_TLS_GOTTPREL:
5118 case R_MIPS_TLS_LDM:
b49e97c9 5119 /* Find the index into the GOT where this value is located. */
0f20cc35
DJ
5120 if (r_type == R_MIPS_TLS_LDM)
5121 {
0a44bf69 5122 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5123 0, 0, NULL, r_type);
0f20cc35
DJ
5124 if (g == MINUS_ONE)
5125 return bfd_reloc_outofrange;
5126 }
5127 else if (!local_p)
b49e97c9 5128 {
0a44bf69
RS
5129 /* On VxWorks, CALL relocations should refer to the .got.plt
5130 entry, which is initialized to point at the PLT stub. */
5131 if (htab->is_vxworks
5132 && (r_type == R_MIPS_CALL_HI16
5133 || r_type == R_MIPS_CALL_LO16
738e5348 5134 || call16_reloc_p (r_type)))
0a44bf69
RS
5135 {
5136 BFD_ASSERT (addend == 0);
5137 BFD_ASSERT (h->root.needs_plt);
5138 g = mips_elf_gotplt_index (info, &h->root);
5139 }
5140 else
b49e97c9 5141 {
0a44bf69
RS
5142 /* GOT_PAGE may take a non-zero addend, that is ignored in a
5143 GOT_PAGE relocation that decays to GOT_DISP because the
5144 symbol turns out to be global. The addend is then added
5145 as GOT_OFST. */
5146 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
5147 g = mips_elf_global_got_index (dynobj, input_bfd,
5148 &h->root, r_type, info);
5149 if (h->tls_type == GOT_NORMAL
5150 && (! elf_hash_table(info)->dynamic_sections_created
5151 || (info->shared
5152 && (info->symbolic || h->root.forced_local)
5153 && h->root.def_regular)))
a8028dd0
RS
5154 /* This is a static link or a -Bsymbolic link. The
5155 symbol is defined locally, or was forced to be local.
5156 We must initialize this entry in the GOT. */
5157 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5158 }
5159 }
0a44bf69 5160 else if (!htab->is_vxworks
738e5348 5161 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5162 /* The calculation below does not involve "g". */
b49e97c9
TS
5163 break;
5164 else
5165 {
5c18022e 5166 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5167 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5168 if (g == MINUS_ONE)
5169 return bfd_reloc_outofrange;
5170 }
5171
5172 /* Convert GOT indices to actual offsets. */
a8028dd0 5173 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5174 break;
b49e97c9
TS
5175 }
5176
0a44bf69
RS
5177 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5178 symbols are resolved by the loader. Add them to .rela.dyn. */
5179 if (h != NULL && is_gott_symbol (info, &h->root))
5180 {
5181 Elf_Internal_Rela outrel;
5182 bfd_byte *loc;
5183 asection *s;
5184
5185 s = mips_elf_rel_dyn_section (info, FALSE);
5186 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5187
5188 outrel.r_offset = (input_section->output_section->vma
5189 + input_section->output_offset
5190 + relocation->r_offset);
5191 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5192 outrel.r_addend = addend;
5193 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5194
5195 /* If we've written this relocation for a readonly section,
5196 we need to set DF_TEXTREL again, so that we do not delete the
5197 DT_TEXTREL tag. */
5198 if (MIPS_ELF_READONLY_SECTION (input_section))
5199 info->flags |= DF_TEXTREL;
5200
0a44bf69
RS
5201 *valuep = 0;
5202 return bfd_reloc_ok;
5203 }
5204
b49e97c9
TS
5205 /* Figure out what kind of relocation is being performed. */
5206 switch (r_type)
5207 {
5208 case R_MIPS_NONE:
5209 return bfd_reloc_continue;
5210
5211 case R_MIPS_16:
a7ebbfdf 5212 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5213 overflowed_p = mips_elf_overflow_p (value, 16);
5214 break;
5215
5216 case R_MIPS_32:
5217 case R_MIPS_REL32:
5218 case R_MIPS_64:
5219 if ((info->shared
861fb55a 5220 || (htab->root.dynamic_sections_created
b49e97c9 5221 && h != NULL
f5385ebf 5222 && h->root.def_dynamic
861fb55a
DJ
5223 && !h->root.def_regular
5224 && !h->has_static_relocs))
b49e97c9 5225 && r_symndx != 0
9a59ad6b
DJ
5226 && (h == NULL
5227 || h->root.root.type != bfd_link_hash_undefweak
5228 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5229 && (input_section->flags & SEC_ALLOC) != 0)
5230 {
861fb55a 5231 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5232 where the symbol will end up. So, we create a relocation
5233 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5234 linker. We must do the same for executable references to
5235 shared library symbols, unless we've decided to use copy
5236 relocs or PLTs instead. */
b49e97c9
TS
5237 value = addend;
5238 if (!mips_elf_create_dynamic_relocation (abfd,
5239 info,
5240 relocation,
5241 h,
5242 sec,
5243 symbol,
5244 &value,
5245 input_section))
5246 return bfd_reloc_undefined;
5247 }
5248 else
5249 {
5250 if (r_type != R_MIPS_REL32)
5251 value = symbol + addend;
5252 else
5253 value = addend;
5254 }
5255 value &= howto->dst_mask;
092dcd75
CD
5256 break;
5257
5258 case R_MIPS_PC32:
5259 value = symbol + addend - p;
5260 value &= howto->dst_mask;
b49e97c9
TS
5261 break;
5262
b49e97c9
TS
5263 case R_MIPS16_26:
5264 /* The calculation for R_MIPS16_26 is just the same as for an
5265 R_MIPS_26. It's only the storage of the relocated field into
5266 the output file that's different. That's handled in
5267 mips_elf_perform_relocation. So, we just fall through to the
5268 R_MIPS_26 case here. */
5269 case R_MIPS_26:
5270 if (local_p)
30ac9238 5271 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
b49e97c9 5272 else
728b2f21
ILT
5273 {
5274 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
c314987d
RS
5275 if (h->root.root.type != bfd_link_hash_undefweak)
5276 overflowed_p = (value >> 26) != ((p + 4) >> 28);
728b2f21 5277 }
b49e97c9
TS
5278 value &= howto->dst_mask;
5279 break;
5280
0f20cc35
DJ
5281 case R_MIPS_TLS_DTPREL_HI16:
5282 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5283 & howto->dst_mask);
5284 break;
5285
5286 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5287 case R_MIPS_TLS_DTPREL32:
5288 case R_MIPS_TLS_DTPREL64:
0f20cc35
DJ
5289 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5290 break;
5291
5292 case R_MIPS_TLS_TPREL_HI16:
5293 value = (mips_elf_high (addend + symbol - tprel_base (info))
5294 & howto->dst_mask);
5295 break;
5296
5297 case R_MIPS_TLS_TPREL_LO16:
5298 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5299 break;
5300
b49e97c9 5301 case R_MIPS_HI16:
d6f16593 5302 case R_MIPS16_HI16:
b49e97c9
TS
5303 if (!gp_disp_p)
5304 {
5305 value = mips_elf_high (addend + symbol);
5306 value &= howto->dst_mask;
5307 }
5308 else
5309 {
d6f16593
MR
5310 /* For MIPS16 ABI code we generate this sequence
5311 0: li $v0,%hi(_gp_disp)
5312 4: addiupc $v1,%lo(_gp_disp)
5313 8: sll $v0,16
5314 12: addu $v0,$v1
5315 14: move $gp,$v0
5316 So the offsets of hi and lo relocs are the same, but the
5317 $pc is four higher than $t9 would be, so reduce
5318 both reloc addends by 4. */
5319 if (r_type == R_MIPS16_HI16)
5320 value = mips_elf_high (addend + gp - p - 4);
5321 else
5322 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5323 overflowed_p = mips_elf_overflow_p (value, 16);
5324 }
5325 break;
5326
5327 case R_MIPS_LO16:
d6f16593 5328 case R_MIPS16_LO16:
b49e97c9
TS
5329 if (!gp_disp_p)
5330 value = (symbol + addend) & howto->dst_mask;
5331 else
5332 {
d6f16593
MR
5333 /* See the comment for R_MIPS16_HI16 above for the reason
5334 for this conditional. */
5335 if (r_type == R_MIPS16_LO16)
5336 value = addend + gp - p;
5337 else
5338 value = addend + gp - p + 4;
b49e97c9 5339 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5340 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5341 _gp_disp are normally generated from the .cpload
5342 pseudo-op. It generates code that normally looks like
5343 this:
5344
5345 lui $gp,%hi(_gp_disp)
5346 addiu $gp,$gp,%lo(_gp_disp)
5347 addu $gp,$gp,$t9
5348
5349 Here $t9 holds the address of the function being called,
5350 as required by the MIPS ELF ABI. The R_MIPS_LO16
5351 relocation can easily overflow in this situation, but the
5352 R_MIPS_HI16 relocation will handle the overflow.
5353 Therefore, we consider this a bug in the MIPS ABI, and do
5354 not check for overflow here. */
5355 }
5356 break;
5357
5358 case R_MIPS_LITERAL:
5359 /* Because we don't merge literal sections, we can handle this
5360 just like R_MIPS_GPREL16. In the long run, we should merge
5361 shared literals, and then we will need to additional work
5362 here. */
5363
5364 /* Fall through. */
5365
5366 case R_MIPS16_GPREL:
5367 /* The R_MIPS16_GPREL performs the same calculation as
5368 R_MIPS_GPREL16, but stores the relocated bits in a different
5369 order. We don't need to do anything special here; the
5370 differences are handled in mips_elf_perform_relocation. */
5371 case R_MIPS_GPREL16:
bce03d3d
AO
5372 /* Only sign-extend the addend if it was extracted from the
5373 instruction. If the addend was separate, leave it alone,
5374 otherwise we may lose significant bits. */
5375 if (howto->partial_inplace)
a7ebbfdf 5376 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5377 value = symbol + addend - gp;
5378 /* If the symbol was local, any earlier relocatable links will
5379 have adjusted its addend with the gp offset, so compensate
5380 for that now. Don't do it for symbols forced local in this
5381 link, though, since they won't have had the gp offset applied
5382 to them before. */
5383 if (was_local_p)
5384 value += gp0;
b49e97c9
TS
5385 overflowed_p = mips_elf_overflow_p (value, 16);
5386 break;
5387
738e5348
RS
5388 case R_MIPS16_GOT16:
5389 case R_MIPS16_CALL16:
b49e97c9
TS
5390 case R_MIPS_GOT16:
5391 case R_MIPS_CALL16:
0a44bf69 5392 /* VxWorks does not have separate local and global semantics for
738e5348 5393 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5394 if (!htab->is_vxworks && local_p)
b49e97c9 5395 {
b34976b6 5396 bfd_boolean forced;
b49e97c9 5397
b49e97c9 5398 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 5399 local_sections, FALSE);
5c18022e 5400 value = mips_elf_got16_entry (abfd, input_bfd, info,
f4416af6 5401 symbol + addend, forced);
b49e97c9
TS
5402 if (value == MINUS_ONE)
5403 return bfd_reloc_outofrange;
5404 value
a8028dd0 5405 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5406 overflowed_p = mips_elf_overflow_p (value, 16);
5407 break;
5408 }
5409
5410 /* Fall through. */
5411
0f20cc35
DJ
5412 case R_MIPS_TLS_GD:
5413 case R_MIPS_TLS_GOTTPREL:
5414 case R_MIPS_TLS_LDM:
b49e97c9 5415 case R_MIPS_GOT_DISP:
0fdc1bf1 5416 got_disp:
b49e97c9
TS
5417 value = g;
5418 overflowed_p = mips_elf_overflow_p (value, 16);
5419 break;
5420
5421 case R_MIPS_GPREL32:
bce03d3d
AO
5422 value = (addend + symbol + gp0 - gp);
5423 if (!save_addend)
5424 value &= howto->dst_mask;
b49e97c9
TS
5425 break;
5426
5427 case R_MIPS_PC16:
bad36eac
DJ
5428 case R_MIPS_GNU_REL16_S2:
5429 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5430 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5431 value >>= howto->rightshift;
5432 value &= howto->dst_mask;
b49e97c9
TS
5433 break;
5434
5435 case R_MIPS_GOT_HI16:
5436 case R_MIPS_CALL_HI16:
5437 /* We're allowed to handle these two relocations identically.
5438 The dynamic linker is allowed to handle the CALL relocations
5439 differently by creating a lazy evaluation stub. */
5440 value = g;
5441 value = mips_elf_high (value);
5442 value &= howto->dst_mask;
5443 break;
5444
5445 case R_MIPS_GOT_LO16:
5446 case R_MIPS_CALL_LO16:
5447 value = g & howto->dst_mask;
5448 break;
5449
5450 case R_MIPS_GOT_PAGE:
0fdc1bf1
AO
5451 /* GOT_PAGE relocations that reference non-local symbols decay
5452 to GOT_DISP. The corresponding GOT_OFST relocation decays to
5453 0. */
93a2b7ae 5454 if (! local_p)
0fdc1bf1 5455 goto got_disp;
5c18022e 5456 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5457 if (value == MINUS_ONE)
5458 return bfd_reloc_outofrange;
a8028dd0 5459 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5460 overflowed_p = mips_elf_overflow_p (value, 16);
5461 break;
5462
5463 case R_MIPS_GOT_OFST:
93a2b7ae 5464 if (local_p)
5c18022e 5465 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5466 else
5467 value = addend;
b49e97c9
TS
5468 overflowed_p = mips_elf_overflow_p (value, 16);
5469 break;
5470
5471 case R_MIPS_SUB:
5472 value = symbol - addend;
5473 value &= howto->dst_mask;
5474 break;
5475
5476 case R_MIPS_HIGHER:
5477 value = mips_elf_higher (addend + symbol);
5478 value &= howto->dst_mask;
5479 break;
5480
5481 case R_MIPS_HIGHEST:
5482 value = mips_elf_highest (addend + symbol);
5483 value &= howto->dst_mask;
5484 break;
5485
5486 case R_MIPS_SCN_DISP:
5487 value = symbol + addend - sec->output_offset;
5488 value &= howto->dst_mask;
5489 break;
5490
b49e97c9 5491 case R_MIPS_JALR:
1367d393
ILT
5492 /* This relocation is only a hint. In some cases, we optimize
5493 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5494 when the symbol does not resolve locally. */
5495 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5496 return bfd_reloc_continue;
5497 value = symbol + addend;
5498 break;
b49e97c9 5499
1367d393 5500 case R_MIPS_PJUMP:
b49e97c9
TS
5501 case R_MIPS_GNU_VTINHERIT:
5502 case R_MIPS_GNU_VTENTRY:
5503 /* We don't do anything with these at present. */
5504 return bfd_reloc_continue;
5505
5506 default:
5507 /* An unrecognized relocation type. */
5508 return bfd_reloc_notsupported;
5509 }
5510
5511 /* Store the VALUE for our caller. */
5512 *valuep = value;
5513 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5514}
5515
5516/* Obtain the field relocated by RELOCATION. */
5517
5518static bfd_vma
9719ad41
RS
5519mips_elf_obtain_contents (reloc_howto_type *howto,
5520 const Elf_Internal_Rela *relocation,
5521 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
5522{
5523 bfd_vma x;
5524 bfd_byte *location = contents + relocation->r_offset;
5525
5526 /* Obtain the bytes. */
5527 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5528
b49e97c9
TS
5529 return x;
5530}
5531
5532/* It has been determined that the result of the RELOCATION is the
5533 VALUE. Use HOWTO to place VALUE into the output file at the
5534 appropriate position. The SECTION is the section to which the
b34976b6 5535 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
b49e97c9
TS
5536 for the relocation must be either JAL or JALX, and it is
5537 unconditionally converted to JALX.
5538
b34976b6 5539 Returns FALSE if anything goes wrong. */
b49e97c9 5540
b34976b6 5541static bfd_boolean
9719ad41
RS
5542mips_elf_perform_relocation (struct bfd_link_info *info,
5543 reloc_howto_type *howto,
5544 const Elf_Internal_Rela *relocation,
5545 bfd_vma value, bfd *input_bfd,
5546 asection *input_section, bfd_byte *contents,
5547 bfd_boolean require_jalx)
b49e97c9
TS
5548{
5549 bfd_vma x;
5550 bfd_byte *location;
5551 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5552
5553 /* Figure out where the relocation is occurring. */
5554 location = contents + relocation->r_offset;
5555
d6f16593
MR
5556 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5557
b49e97c9
TS
5558 /* Obtain the current value. */
5559 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5560
5561 /* Clear the field we are setting. */
5562 x &= ~howto->dst_mask;
5563
b49e97c9
TS
5564 /* Set the field. */
5565 x |= (value & howto->dst_mask);
5566
5567 /* If required, turn JAL into JALX. */
5568 if (require_jalx)
5569 {
b34976b6 5570 bfd_boolean ok;
b49e97c9
TS
5571 bfd_vma opcode = x >> 26;
5572 bfd_vma jalx_opcode;
5573
5574 /* Check to see if the opcode is already JAL or JALX. */
5575 if (r_type == R_MIPS16_26)
5576 {
5577 ok = ((opcode == 0x6) || (opcode == 0x7));
5578 jalx_opcode = 0x7;
5579 }
5580 else
5581 {
5582 ok = ((opcode == 0x3) || (opcode == 0x1d));
5583 jalx_opcode = 0x1d;
5584 }
5585
5586 /* If the opcode is not JAL or JALX, there's a problem. */
5587 if (!ok)
5588 {
5589 (*_bfd_error_handler)
d003868e
AM
5590 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5591 input_bfd,
5592 input_section,
b49e97c9
TS
5593 (unsigned long) relocation->r_offset);
5594 bfd_set_error (bfd_error_bad_value);
b34976b6 5595 return FALSE;
b49e97c9
TS
5596 }
5597
5598 /* Make this the JALX opcode. */
5599 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5600 }
5601
cd8d5a82
CF
5602 /* Try converting JAL and JALR to BAL, if the target is in range. */
5603 if (!info->relocatable
1367d393 5604 && !require_jalx
cd8d5a82
CF
5605 && ((JAL_TO_BAL_P (input_bfd)
5606 && r_type == R_MIPS_26
5607 && (x >> 26) == 0x3) /* jal addr */
5608 || (JALR_TO_BAL_P (input_bfd)
5609 && r_type == R_MIPS_JALR
5610 && x == 0x0320f809))) /* jalr t9 */
1367d393
ILT
5611 {
5612 bfd_vma addr;
5613 bfd_vma dest;
5614 bfd_signed_vma off;
5615
5616 addr = (input_section->output_section->vma
5617 + input_section->output_offset
5618 + relocation->r_offset
5619 + 4);
5620 if (r_type == R_MIPS_26)
5621 dest = (value << 2) | ((addr >> 28) << 28);
5622 else
5623 dest = value;
5624 off = dest - addr;
5625 if (off <= 0x1ffff && off >= -0x20000)
5626 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5627 }
5628
b49e97c9
TS
5629 /* Put the value into the output. */
5630 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593
MR
5631
5632 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5633 location);
5634
b34976b6 5635 return TRUE;
b49e97c9 5636}
b49e97c9 5637\f
b49e97c9
TS
5638/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5639 is the original relocation, which is now being transformed into a
5640 dynamic relocation. The ADDENDP is adjusted if necessary; the
5641 caller should store the result in place of the original addend. */
5642
b34976b6 5643static bfd_boolean
9719ad41
RS
5644mips_elf_create_dynamic_relocation (bfd *output_bfd,
5645 struct bfd_link_info *info,
5646 const Elf_Internal_Rela *rel,
5647 struct mips_elf_link_hash_entry *h,
5648 asection *sec, bfd_vma symbol,
5649 bfd_vma *addendp, asection *input_section)
b49e97c9 5650{
947216bf 5651 Elf_Internal_Rela outrel[3];
b49e97c9
TS
5652 asection *sreloc;
5653 bfd *dynobj;
5654 int r_type;
5d41f0b6
RS
5655 long indx;
5656 bfd_boolean defined_p;
0a44bf69 5657 struct mips_elf_link_hash_table *htab;
b49e97c9 5658
0a44bf69 5659 htab = mips_elf_hash_table (info);
b49e97c9
TS
5660 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5661 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 5662 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
5663 BFD_ASSERT (sreloc != NULL);
5664 BFD_ASSERT (sreloc->contents != NULL);
5665 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 5666 < sreloc->size);
b49e97c9 5667
b49e97c9
TS
5668 outrel[0].r_offset =
5669 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
5670 if (ABI_64_P (output_bfd))
5671 {
5672 outrel[1].r_offset =
5673 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5674 outrel[2].r_offset =
5675 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5676 }
b49e97c9 5677
c5ae1840 5678 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 5679 /* The relocation field has been deleted. */
5d41f0b6
RS
5680 return TRUE;
5681
5682 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
5683 {
5684 /* The relocation field has been converted into a relative value of
5685 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5686 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 5687 *addendp += symbol;
5d41f0b6 5688 return TRUE;
0d591ff7 5689 }
b49e97c9 5690
5d41f0b6
RS
5691 /* We must now calculate the dynamic symbol table index to use
5692 in the relocation. */
5693 if (h != NULL
6ece8836
TS
5694 && (!h->root.def_regular
5695 || (info->shared && !info->symbolic && !h->root.forced_local)))
5d41f0b6
RS
5696 {
5697 indx = h->root.dynindx;
5698 if (SGI_COMPAT (output_bfd))
5699 defined_p = h->root.def_regular;
5700 else
5701 /* ??? glibc's ld.so just adds the final GOT entry to the
5702 relocation field. It therefore treats relocs against
5703 defined symbols in the same way as relocs against
5704 undefined symbols. */
5705 defined_p = FALSE;
5706 }
b49e97c9
TS
5707 else
5708 {
5d41f0b6
RS
5709 if (sec != NULL && bfd_is_abs_section (sec))
5710 indx = 0;
5711 else if (sec == NULL || sec->owner == NULL)
fdd07405 5712 {
5d41f0b6
RS
5713 bfd_set_error (bfd_error_bad_value);
5714 return FALSE;
b49e97c9
TS
5715 }
5716 else
5717 {
5d41f0b6 5718 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
5719 if (indx == 0)
5720 {
5721 asection *osec = htab->root.text_index_section;
5722 indx = elf_section_data (osec)->dynindx;
5723 }
5d41f0b6
RS
5724 if (indx == 0)
5725 abort ();
b49e97c9
TS
5726 }
5727
5d41f0b6
RS
5728 /* Instead of generating a relocation using the section
5729 symbol, we may as well make it a fully relative
5730 relocation. We want to avoid generating relocations to
5731 local symbols because we used to generate them
5732 incorrectly, without adding the original symbol value,
5733 which is mandated by the ABI for section symbols. In
5734 order to give dynamic loaders and applications time to
5735 phase out the incorrect use, we refrain from emitting
5736 section-relative relocations. It's not like they're
5737 useful, after all. This should be a bit more efficient
5738 as well. */
5739 /* ??? Although this behavior is compatible with glibc's ld.so,
5740 the ABI says that relocations against STN_UNDEF should have
5741 a symbol value of 0. Irix rld honors this, so relocations
5742 against STN_UNDEF have no effect. */
5743 if (!SGI_COMPAT (output_bfd))
5744 indx = 0;
5745 defined_p = TRUE;
b49e97c9
TS
5746 }
5747
5d41f0b6
RS
5748 /* If the relocation was previously an absolute relocation and
5749 this symbol will not be referred to by the relocation, we must
5750 adjust it by the value we give it in the dynamic symbol table.
5751 Otherwise leave the job up to the dynamic linker. */
5752 if (defined_p && r_type != R_MIPS_REL32)
5753 *addendp += symbol;
5754
0a44bf69
RS
5755 if (htab->is_vxworks)
5756 /* VxWorks uses non-relative relocations for this. */
5757 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5758 else
5759 /* The relocation is always an REL32 relocation because we don't
5760 know where the shared library will wind up at load-time. */
5761 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5762 R_MIPS_REL32);
5763
5d41f0b6
RS
5764 /* For strict adherence to the ABI specification, we should
5765 generate a R_MIPS_64 relocation record by itself before the
5766 _REL32/_64 record as well, such that the addend is read in as
5767 a 64-bit value (REL32 is a 32-bit relocation, after all).
5768 However, since none of the existing ELF64 MIPS dynamic
5769 loaders seems to care, we don't waste space with these
5770 artificial relocations. If this turns out to not be true,
5771 mips_elf_allocate_dynamic_relocation() should be tweaked so
5772 as to make room for a pair of dynamic relocations per
5773 invocation if ABI_64_P, and here we should generate an
5774 additional relocation record with R_MIPS_64 by itself for a
5775 NULL symbol before this relocation record. */
5776 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5777 ABI_64_P (output_bfd)
5778 ? R_MIPS_64
5779 : R_MIPS_NONE);
5780 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5781
5782 /* Adjust the output offset of the relocation to reference the
5783 correct location in the output file. */
5784 outrel[0].r_offset += (input_section->output_section->vma
5785 + input_section->output_offset);
5786 outrel[1].r_offset += (input_section->output_section->vma
5787 + input_section->output_offset);
5788 outrel[2].r_offset += (input_section->output_section->vma
5789 + input_section->output_offset);
5790
b49e97c9
TS
5791 /* Put the relocation back out. We have to use the special
5792 relocation outputter in the 64-bit case since the 64-bit
5793 relocation format is non-standard. */
5794 if (ABI_64_P (output_bfd))
5795 {
5796 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5797 (output_bfd, &outrel[0],
5798 (sreloc->contents
5799 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5800 }
0a44bf69
RS
5801 else if (htab->is_vxworks)
5802 {
5803 /* VxWorks uses RELA rather than REL dynamic relocations. */
5804 outrel[0].r_addend = *addendp;
5805 bfd_elf32_swap_reloca_out
5806 (output_bfd, &outrel[0],
5807 (sreloc->contents
5808 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5809 }
b49e97c9 5810 else
947216bf
AM
5811 bfd_elf32_swap_reloc_out
5812 (output_bfd, &outrel[0],
5813 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 5814
b49e97c9
TS
5815 /* We've now added another relocation. */
5816 ++sreloc->reloc_count;
5817
5818 /* Make sure the output section is writable. The dynamic linker
5819 will be writing to it. */
5820 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5821 |= SHF_WRITE;
5822
5823 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 5824 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9
TS
5825 {
5826 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5827 bfd_byte *cr;
5828
5829 if (scpt)
5830 {
5831 Elf32_crinfo cptrel;
5832
5833 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5834 cptrel.vaddr = (rel->r_offset
5835 + input_section->output_section->vma
5836 + input_section->output_offset);
5837 if (r_type == R_MIPS_REL32)
5838 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5839 else
5840 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5841 mips_elf_set_cr_dist2to (cptrel, 0);
5842 cptrel.konst = *addendp;
5843
5844 cr = (scpt->contents
5845 + sizeof (Elf32_External_compact_rel));
abc0f8d0 5846 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
5847 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5848 ((Elf32_External_crinfo *) cr
5849 + scpt->reloc_count));
5850 ++scpt->reloc_count;
5851 }
5852 }
5853
943284cc
DJ
5854 /* If we've written this relocation for a readonly section,
5855 we need to set DF_TEXTREL again, so that we do not delete the
5856 DT_TEXTREL tag. */
5857 if (MIPS_ELF_READONLY_SECTION (input_section))
5858 info->flags |= DF_TEXTREL;
5859
b34976b6 5860 return TRUE;
b49e97c9
TS
5861}
5862\f
b49e97c9
TS
5863/* Return the MACH for a MIPS e_flags value. */
5864
5865unsigned long
9719ad41 5866_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
5867{
5868 switch (flags & EF_MIPS_MACH)
5869 {
5870 case E_MIPS_MACH_3900:
5871 return bfd_mach_mips3900;
5872
5873 case E_MIPS_MACH_4010:
5874 return bfd_mach_mips4010;
5875
5876 case E_MIPS_MACH_4100:
5877 return bfd_mach_mips4100;
5878
5879 case E_MIPS_MACH_4111:
5880 return bfd_mach_mips4111;
5881
00707a0e
RS
5882 case E_MIPS_MACH_4120:
5883 return bfd_mach_mips4120;
5884
b49e97c9
TS
5885 case E_MIPS_MACH_4650:
5886 return bfd_mach_mips4650;
5887
00707a0e
RS
5888 case E_MIPS_MACH_5400:
5889 return bfd_mach_mips5400;
5890
5891 case E_MIPS_MACH_5500:
5892 return bfd_mach_mips5500;
5893
0d2e43ed
ILT
5894 case E_MIPS_MACH_9000:
5895 return bfd_mach_mips9000;
5896
b49e97c9
TS
5897 case E_MIPS_MACH_SB1:
5898 return bfd_mach_mips_sb1;
5899
350cc38d
MS
5900 case E_MIPS_MACH_LS2E:
5901 return bfd_mach_mips_loongson_2e;
5902
5903 case E_MIPS_MACH_LS2F:
5904 return bfd_mach_mips_loongson_2f;
5905
6f179bd0
AN
5906 case E_MIPS_MACH_OCTEON:
5907 return bfd_mach_mips_octeon;
5908
52b6b6b9
JM
5909 case E_MIPS_MACH_XLR:
5910 return bfd_mach_mips_xlr;
5911
b49e97c9
TS
5912 default:
5913 switch (flags & EF_MIPS_ARCH)
5914 {
5915 default:
5916 case E_MIPS_ARCH_1:
5917 return bfd_mach_mips3000;
b49e97c9
TS
5918
5919 case E_MIPS_ARCH_2:
5920 return bfd_mach_mips6000;
b49e97c9
TS
5921
5922 case E_MIPS_ARCH_3:
5923 return bfd_mach_mips4000;
b49e97c9
TS
5924
5925 case E_MIPS_ARCH_4:
5926 return bfd_mach_mips8000;
b49e97c9
TS
5927
5928 case E_MIPS_ARCH_5:
5929 return bfd_mach_mips5;
b49e97c9
TS
5930
5931 case E_MIPS_ARCH_32:
5932 return bfd_mach_mipsisa32;
b49e97c9
TS
5933
5934 case E_MIPS_ARCH_64:
5935 return bfd_mach_mipsisa64;
af7ee8bf
CD
5936
5937 case E_MIPS_ARCH_32R2:
5938 return bfd_mach_mipsisa32r2;
5f74bc13
CD
5939
5940 case E_MIPS_ARCH_64R2:
5941 return bfd_mach_mipsisa64r2;
b49e97c9
TS
5942 }
5943 }
5944
5945 return 0;
5946}
5947
5948/* Return printable name for ABI. */
5949
5950static INLINE char *
9719ad41 5951elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
5952{
5953 flagword flags;
5954
5955 flags = elf_elfheader (abfd)->e_flags;
5956 switch (flags & EF_MIPS_ABI)
5957 {
5958 case 0:
5959 if (ABI_N32_P (abfd))
5960 return "N32";
5961 else if (ABI_64_P (abfd))
5962 return "64";
5963 else
5964 return "none";
5965 case E_MIPS_ABI_O32:
5966 return "O32";
5967 case E_MIPS_ABI_O64:
5968 return "O64";
5969 case E_MIPS_ABI_EABI32:
5970 return "EABI32";
5971 case E_MIPS_ABI_EABI64:
5972 return "EABI64";
5973 default:
5974 return "unknown abi";
5975 }
5976}
5977\f
5978/* MIPS ELF uses two common sections. One is the usual one, and the
5979 other is for small objects. All the small objects are kept
5980 together, and then referenced via the gp pointer, which yields
5981 faster assembler code. This is what we use for the small common
5982 section. This approach is copied from ecoff.c. */
5983static asection mips_elf_scom_section;
5984static asymbol mips_elf_scom_symbol;
5985static asymbol *mips_elf_scom_symbol_ptr;
5986
5987/* MIPS ELF also uses an acommon section, which represents an
5988 allocated common symbol which may be overridden by a
5989 definition in a shared library. */
5990static asection mips_elf_acom_section;
5991static asymbol mips_elf_acom_symbol;
5992static asymbol *mips_elf_acom_symbol_ptr;
5993
738e5348 5994/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
5995
5996void
9719ad41 5997_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
5998{
5999 elf_symbol_type *elfsym;
6000
738e5348 6001 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6002 elfsym = (elf_symbol_type *) asym;
6003 switch (elfsym->internal_elf_sym.st_shndx)
6004 {
6005 case SHN_MIPS_ACOMMON:
6006 /* This section is used in a dynamically linked executable file.
6007 It is an allocated common section. The dynamic linker can
6008 either resolve these symbols to something in a shared
6009 library, or it can just leave them here. For our purposes,
6010 we can consider these symbols to be in a new section. */
6011 if (mips_elf_acom_section.name == NULL)
6012 {
6013 /* Initialize the acommon section. */
6014 mips_elf_acom_section.name = ".acommon";
6015 mips_elf_acom_section.flags = SEC_ALLOC;
6016 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6017 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6018 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6019 mips_elf_acom_symbol.name = ".acommon";
6020 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6021 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6022 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6023 }
6024 asym->section = &mips_elf_acom_section;
6025 break;
6026
6027 case SHN_COMMON:
6028 /* Common symbols less than the GP size are automatically
6029 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6030 if (asym->value > elf_gp_size (abfd)
b59eed79 6031 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6032 || IRIX_COMPAT (abfd) == ict_irix6)
6033 break;
6034 /* Fall through. */
6035 case SHN_MIPS_SCOMMON:
6036 if (mips_elf_scom_section.name == NULL)
6037 {
6038 /* Initialize the small common section. */
6039 mips_elf_scom_section.name = ".scommon";
6040 mips_elf_scom_section.flags = SEC_IS_COMMON;
6041 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6042 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6043 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6044 mips_elf_scom_symbol.name = ".scommon";
6045 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6046 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6047 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6048 }
6049 asym->section = &mips_elf_scom_section;
6050 asym->value = elfsym->internal_elf_sym.st_size;
6051 break;
6052
6053 case SHN_MIPS_SUNDEFINED:
6054 asym->section = bfd_und_section_ptr;
6055 break;
6056
b49e97c9 6057 case SHN_MIPS_TEXT:
00b4930b
TS
6058 {
6059 asection *section = bfd_get_section_by_name (abfd, ".text");
6060
6061 BFD_ASSERT (SGI_COMPAT (abfd));
6062 if (section != NULL)
6063 {
6064 asym->section = section;
6065 /* MIPS_TEXT is a bit special, the address is not an offset
6066 to the base of the .text section. So substract the section
6067 base address to make it an offset. */
6068 asym->value -= section->vma;
6069 }
6070 }
b49e97c9
TS
6071 break;
6072
6073 case SHN_MIPS_DATA:
00b4930b
TS
6074 {
6075 asection *section = bfd_get_section_by_name (abfd, ".data");
6076
6077 BFD_ASSERT (SGI_COMPAT (abfd));
6078 if (section != NULL)
6079 {
6080 asym->section = section;
6081 /* MIPS_DATA is a bit special, the address is not an offset
6082 to the base of the .data section. So substract the section
6083 base address to make it an offset. */
6084 asym->value -= section->vma;
6085 }
6086 }
b49e97c9 6087 break;
b49e97c9 6088 }
738e5348
RS
6089
6090 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6091 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6092 && (asym->value & 1) != 0)
6093 {
6094 asym->value--;
6095 elfsym->internal_elf_sym.st_other
6096 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6097 }
b49e97c9
TS
6098}
6099\f
8c946ed5
RS
6100/* Implement elf_backend_eh_frame_address_size. This differs from
6101 the default in the way it handles EABI64.
6102
6103 EABI64 was originally specified as an LP64 ABI, and that is what
6104 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6105 historically accepted the combination of -mabi=eabi and -mlong32,
6106 and this ILP32 variation has become semi-official over time.
6107 Both forms use elf32 and have pointer-sized FDE addresses.
6108
6109 If an EABI object was generated by GCC 4.0 or above, it will have
6110 an empty .gcc_compiled_longXX section, where XX is the size of longs
6111 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6112 have no special marking to distinguish them from LP64 objects.
6113
6114 We don't want users of the official LP64 ABI to be punished for the
6115 existence of the ILP32 variant, but at the same time, we don't want
6116 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6117 We therefore take the following approach:
6118
6119 - If ABFD contains a .gcc_compiled_longXX section, use it to
6120 determine the pointer size.
6121
6122 - Otherwise check the type of the first relocation. Assume that
6123 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6124
6125 - Otherwise punt.
6126
6127 The second check is enough to detect LP64 objects generated by pre-4.0
6128 compilers because, in the kind of output generated by those compilers,
6129 the first relocation will be associated with either a CIE personality
6130 routine or an FDE start address. Furthermore, the compilers never
6131 used a special (non-pointer) encoding for this ABI.
6132
6133 Checking the relocation type should also be safe because there is no
6134 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6135 did so. */
6136
6137unsigned int
6138_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6139{
6140 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6141 return 8;
6142 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6143 {
6144 bfd_boolean long32_p, long64_p;
6145
6146 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6147 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6148 if (long32_p && long64_p)
6149 return 0;
6150 if (long32_p)
6151 return 4;
6152 if (long64_p)
6153 return 8;
6154
6155 if (sec->reloc_count > 0
6156 && elf_section_data (sec)->relocs != NULL
6157 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6158 == R_MIPS_64))
6159 return 8;
6160
6161 return 0;
6162 }
6163 return 4;
6164}
6165\f
174fd7f9
RS
6166/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6167 relocations against two unnamed section symbols to resolve to the
6168 same address. For example, if we have code like:
6169
6170 lw $4,%got_disp(.data)($gp)
6171 lw $25,%got_disp(.text)($gp)
6172 jalr $25
6173
6174 then the linker will resolve both relocations to .data and the program
6175 will jump there rather than to .text.
6176
6177 We can work around this problem by giving names to local section symbols.
6178 This is also what the MIPSpro tools do. */
6179
6180bfd_boolean
6181_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6182{
6183 return SGI_COMPAT (abfd);
6184}
6185\f
b49e97c9
TS
6186/* Work over a section just before writing it out. This routine is
6187 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6188 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6189 a better way. */
6190
b34976b6 6191bfd_boolean
9719ad41 6192_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6193{
6194 if (hdr->sh_type == SHT_MIPS_REGINFO
6195 && hdr->sh_size > 0)
6196 {
6197 bfd_byte buf[4];
6198
6199 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6200 BFD_ASSERT (hdr->contents == NULL);
6201
6202 if (bfd_seek (abfd,
6203 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6204 SEEK_SET) != 0)
b34976b6 6205 return FALSE;
b49e97c9 6206 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6207 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6208 return FALSE;
b49e97c9
TS
6209 }
6210
6211 if (hdr->sh_type == SHT_MIPS_OPTIONS
6212 && hdr->bfd_section != NULL
f0abc2a1
AM
6213 && mips_elf_section_data (hdr->bfd_section) != NULL
6214 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6215 {
6216 bfd_byte *contents, *l, *lend;
6217
f0abc2a1
AM
6218 /* We stored the section contents in the tdata field in the
6219 set_section_contents routine. We save the section contents
6220 so that we don't have to read them again.
b49e97c9
TS
6221 At this point we know that elf_gp is set, so we can look
6222 through the section contents to see if there is an
6223 ODK_REGINFO structure. */
6224
f0abc2a1 6225 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6226 l = contents;
6227 lend = contents + hdr->sh_size;
6228 while (l + sizeof (Elf_External_Options) <= lend)
6229 {
6230 Elf_Internal_Options intopt;
6231
6232 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6233 &intopt);
1bc8074d
MR
6234 if (intopt.size < sizeof (Elf_External_Options))
6235 {
6236 (*_bfd_error_handler)
6237 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6238 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6239 break;
6240 }
b49e97c9
TS
6241 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6242 {
6243 bfd_byte buf[8];
6244
6245 if (bfd_seek (abfd,
6246 (hdr->sh_offset
6247 + (l - contents)
6248 + sizeof (Elf_External_Options)
6249 + (sizeof (Elf64_External_RegInfo) - 8)),
6250 SEEK_SET) != 0)
b34976b6 6251 return FALSE;
b49e97c9 6252 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6253 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6254 return FALSE;
b49e97c9
TS
6255 }
6256 else if (intopt.kind == ODK_REGINFO)
6257 {
6258 bfd_byte buf[4];
6259
6260 if (bfd_seek (abfd,
6261 (hdr->sh_offset
6262 + (l - contents)
6263 + sizeof (Elf_External_Options)
6264 + (sizeof (Elf32_External_RegInfo) - 4)),
6265 SEEK_SET) != 0)
b34976b6 6266 return FALSE;
b49e97c9 6267 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6268 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6269 return FALSE;
b49e97c9
TS
6270 }
6271 l += intopt.size;
6272 }
6273 }
6274
6275 if (hdr->bfd_section != NULL)
6276 {
6277 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6278
2d0f9ad9
JM
6279 /* .sbss is not handled specially here because the GNU/Linux
6280 prelinker can convert .sbss from NOBITS to PROGBITS and
6281 changing it back to NOBITS breaks the binary. The entry in
6282 _bfd_mips_elf_special_sections will ensure the correct flags
6283 are set on .sbss if BFD creates it without reading it from an
6284 input file, and without special handling here the flags set
6285 on it in an input file will be followed. */
b49e97c9
TS
6286 if (strcmp (name, ".sdata") == 0
6287 || strcmp (name, ".lit8") == 0
6288 || strcmp (name, ".lit4") == 0)
6289 {
6290 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6291 hdr->sh_type = SHT_PROGBITS;
6292 }
b49e97c9
TS
6293 else if (strcmp (name, ".srdata") == 0)
6294 {
6295 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6296 hdr->sh_type = SHT_PROGBITS;
6297 }
6298 else if (strcmp (name, ".compact_rel") == 0)
6299 {
6300 hdr->sh_flags = 0;
6301 hdr->sh_type = SHT_PROGBITS;
6302 }
6303 else if (strcmp (name, ".rtproc") == 0)
6304 {
6305 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6306 {
6307 unsigned int adjust;
6308
6309 adjust = hdr->sh_size % hdr->sh_addralign;
6310 if (adjust != 0)
6311 hdr->sh_size += hdr->sh_addralign - adjust;
6312 }
6313 }
6314 }
6315
b34976b6 6316 return TRUE;
b49e97c9
TS
6317}
6318
6319/* Handle a MIPS specific section when reading an object file. This
6320 is called when elfcode.h finds a section with an unknown type.
6321 This routine supports both the 32-bit and 64-bit ELF ABI.
6322
6323 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6324 how to. */
6325
b34976b6 6326bfd_boolean
6dc132d9
L
6327_bfd_mips_elf_section_from_shdr (bfd *abfd,
6328 Elf_Internal_Shdr *hdr,
6329 const char *name,
6330 int shindex)
b49e97c9
TS
6331{
6332 flagword flags = 0;
6333
6334 /* There ought to be a place to keep ELF backend specific flags, but
6335 at the moment there isn't one. We just keep track of the
6336 sections by their name, instead. Fortunately, the ABI gives
6337 suggested names for all the MIPS specific sections, so we will
6338 probably get away with this. */
6339 switch (hdr->sh_type)
6340 {
6341 case SHT_MIPS_LIBLIST:
6342 if (strcmp (name, ".liblist") != 0)
b34976b6 6343 return FALSE;
b49e97c9
TS
6344 break;
6345 case SHT_MIPS_MSYM:
6346 if (strcmp (name, ".msym") != 0)
b34976b6 6347 return FALSE;
b49e97c9
TS
6348 break;
6349 case SHT_MIPS_CONFLICT:
6350 if (strcmp (name, ".conflict") != 0)
b34976b6 6351 return FALSE;
b49e97c9
TS
6352 break;
6353 case SHT_MIPS_GPTAB:
0112cd26 6354 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6355 return FALSE;
b49e97c9
TS
6356 break;
6357 case SHT_MIPS_UCODE:
6358 if (strcmp (name, ".ucode") != 0)
b34976b6 6359 return FALSE;
b49e97c9
TS
6360 break;
6361 case SHT_MIPS_DEBUG:
6362 if (strcmp (name, ".mdebug") != 0)
b34976b6 6363 return FALSE;
b49e97c9
TS
6364 flags = SEC_DEBUGGING;
6365 break;
6366 case SHT_MIPS_REGINFO:
6367 if (strcmp (name, ".reginfo") != 0
6368 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6369 return FALSE;
b49e97c9
TS
6370 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6371 break;
6372 case SHT_MIPS_IFACE:
6373 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6374 return FALSE;
b49e97c9
TS
6375 break;
6376 case SHT_MIPS_CONTENT:
0112cd26 6377 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6378 return FALSE;
b49e97c9
TS
6379 break;
6380 case SHT_MIPS_OPTIONS:
cc2e31b9 6381 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6382 return FALSE;
b49e97c9
TS
6383 break;
6384 case SHT_MIPS_DWARF:
1b315056 6385 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6386 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6387 return FALSE;
b49e97c9
TS
6388 break;
6389 case SHT_MIPS_SYMBOL_LIB:
6390 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6391 return FALSE;
b49e97c9
TS
6392 break;
6393 case SHT_MIPS_EVENTS:
0112cd26
NC
6394 if (! CONST_STRNEQ (name, ".MIPS.events")
6395 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6396 return FALSE;
b49e97c9
TS
6397 break;
6398 default:
cc2e31b9 6399 break;
b49e97c9
TS
6400 }
6401
6dc132d9 6402 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6403 return FALSE;
b49e97c9
TS
6404
6405 if (flags)
6406 {
6407 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6408 (bfd_get_section_flags (abfd,
6409 hdr->bfd_section)
6410 | flags)))
b34976b6 6411 return FALSE;
b49e97c9
TS
6412 }
6413
6414 /* FIXME: We should record sh_info for a .gptab section. */
6415
6416 /* For a .reginfo section, set the gp value in the tdata information
6417 from the contents of this section. We need the gp value while
6418 processing relocs, so we just get it now. The .reginfo section
6419 is not used in the 64-bit MIPS ELF ABI. */
6420 if (hdr->sh_type == SHT_MIPS_REGINFO)
6421 {
6422 Elf32_External_RegInfo ext;
6423 Elf32_RegInfo s;
6424
9719ad41
RS
6425 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6426 &ext, 0, sizeof ext))
b34976b6 6427 return FALSE;
b49e97c9
TS
6428 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6429 elf_gp (abfd) = s.ri_gp_value;
6430 }
6431
6432 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6433 set the gp value based on what we find. We may see both
6434 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6435 they should agree. */
6436 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6437 {
6438 bfd_byte *contents, *l, *lend;
6439
9719ad41 6440 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6441 if (contents == NULL)
b34976b6 6442 return FALSE;
b49e97c9 6443 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6444 0, hdr->sh_size))
b49e97c9
TS
6445 {
6446 free (contents);
b34976b6 6447 return FALSE;
b49e97c9
TS
6448 }
6449 l = contents;
6450 lend = contents + hdr->sh_size;
6451 while (l + sizeof (Elf_External_Options) <= lend)
6452 {
6453 Elf_Internal_Options intopt;
6454
6455 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6456 &intopt);
1bc8074d
MR
6457 if (intopt.size < sizeof (Elf_External_Options))
6458 {
6459 (*_bfd_error_handler)
6460 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6461 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6462 break;
6463 }
b49e97c9
TS
6464 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6465 {
6466 Elf64_Internal_RegInfo intreg;
6467
6468 bfd_mips_elf64_swap_reginfo_in
6469 (abfd,
6470 ((Elf64_External_RegInfo *)
6471 (l + sizeof (Elf_External_Options))),
6472 &intreg);
6473 elf_gp (abfd) = intreg.ri_gp_value;
6474 }
6475 else if (intopt.kind == ODK_REGINFO)
6476 {
6477 Elf32_RegInfo intreg;
6478
6479 bfd_mips_elf32_swap_reginfo_in
6480 (abfd,
6481 ((Elf32_External_RegInfo *)
6482 (l + sizeof (Elf_External_Options))),
6483 &intreg);
6484 elf_gp (abfd) = intreg.ri_gp_value;
6485 }
6486 l += intopt.size;
6487 }
6488 free (contents);
6489 }
6490
b34976b6 6491 return TRUE;
b49e97c9
TS
6492}
6493
6494/* Set the correct type for a MIPS ELF section. We do this by the
6495 section name, which is a hack, but ought to work. This routine is
6496 used by both the 32-bit and the 64-bit ABI. */
6497
b34976b6 6498bfd_boolean
9719ad41 6499_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 6500{
0414f35b 6501 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
6502
6503 if (strcmp (name, ".liblist") == 0)
6504 {
6505 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 6506 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
6507 /* The sh_link field is set in final_write_processing. */
6508 }
6509 else if (strcmp (name, ".conflict") == 0)
6510 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 6511 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
6512 {
6513 hdr->sh_type = SHT_MIPS_GPTAB;
6514 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6515 /* The sh_info field is set in final_write_processing. */
6516 }
6517 else if (strcmp (name, ".ucode") == 0)
6518 hdr->sh_type = SHT_MIPS_UCODE;
6519 else if (strcmp (name, ".mdebug") == 0)
6520 {
6521 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 6522 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
6523 entsize of 0. FIXME: Does this matter? */
6524 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6525 hdr->sh_entsize = 0;
6526 else
6527 hdr->sh_entsize = 1;
6528 }
6529 else if (strcmp (name, ".reginfo") == 0)
6530 {
6531 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 6532 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
6533 entsize of 0x18. FIXME: Does this matter? */
6534 if (SGI_COMPAT (abfd))
6535 {
6536 if ((abfd->flags & DYNAMIC) != 0)
6537 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6538 else
6539 hdr->sh_entsize = 1;
6540 }
6541 else
6542 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6543 }
6544 else if (SGI_COMPAT (abfd)
6545 && (strcmp (name, ".hash") == 0
6546 || strcmp (name, ".dynamic") == 0
6547 || strcmp (name, ".dynstr") == 0))
6548 {
6549 if (SGI_COMPAT (abfd))
6550 hdr->sh_entsize = 0;
6551#if 0
8dc1a139 6552 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
6553 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6554#endif
6555 }
6556 else if (strcmp (name, ".got") == 0
6557 || strcmp (name, ".srdata") == 0
6558 || strcmp (name, ".sdata") == 0
6559 || strcmp (name, ".sbss") == 0
6560 || strcmp (name, ".lit4") == 0
6561 || strcmp (name, ".lit8") == 0)
6562 hdr->sh_flags |= SHF_MIPS_GPREL;
6563 else if (strcmp (name, ".MIPS.interfaces") == 0)
6564 {
6565 hdr->sh_type = SHT_MIPS_IFACE;
6566 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6567 }
0112cd26 6568 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
6569 {
6570 hdr->sh_type = SHT_MIPS_CONTENT;
6571 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6572 /* The sh_info field is set in final_write_processing. */
6573 }
cc2e31b9 6574 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
6575 {
6576 hdr->sh_type = SHT_MIPS_OPTIONS;
6577 hdr->sh_entsize = 1;
6578 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6579 }
1b315056
CS
6580 else if (CONST_STRNEQ (name, ".debug_")
6581 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
6582 {
6583 hdr->sh_type = SHT_MIPS_DWARF;
6584
6585 /* Irix facilities such as libexc expect a single .debug_frame
6586 per executable, the system ones have NOSTRIP set and the linker
6587 doesn't merge sections with different flags so ... */
6588 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6589 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6590 }
b49e97c9
TS
6591 else if (strcmp (name, ".MIPS.symlib") == 0)
6592 {
6593 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6594 /* The sh_link and sh_info fields are set in
6595 final_write_processing. */
6596 }
0112cd26
NC
6597 else if (CONST_STRNEQ (name, ".MIPS.events")
6598 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
6599 {
6600 hdr->sh_type = SHT_MIPS_EVENTS;
6601 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6602 /* The sh_link field is set in final_write_processing. */
6603 }
6604 else if (strcmp (name, ".msym") == 0)
6605 {
6606 hdr->sh_type = SHT_MIPS_MSYM;
6607 hdr->sh_flags |= SHF_ALLOC;
6608 hdr->sh_entsize = 8;
6609 }
6610
7a79a000
TS
6611 /* The generic elf_fake_sections will set up REL_HDR using the default
6612 kind of relocations. We used to set up a second header for the
6613 non-default kind of relocations here, but only NewABI would use
6614 these, and the IRIX ld doesn't like resulting empty RELA sections.
6615 Thus we create those header only on demand now. */
b49e97c9 6616
b34976b6 6617 return TRUE;
b49e97c9
TS
6618}
6619
6620/* Given a BFD section, try to locate the corresponding ELF section
6621 index. This is used by both the 32-bit and the 64-bit ABI.
6622 Actually, it's not clear to me that the 64-bit ABI supports these,
6623 but for non-PIC objects we will certainly want support for at least
6624 the .scommon section. */
6625
b34976b6 6626bfd_boolean
9719ad41
RS
6627_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6628 asection *sec, int *retval)
b49e97c9
TS
6629{
6630 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6631 {
6632 *retval = SHN_MIPS_SCOMMON;
b34976b6 6633 return TRUE;
b49e97c9
TS
6634 }
6635 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6636 {
6637 *retval = SHN_MIPS_ACOMMON;
b34976b6 6638 return TRUE;
b49e97c9 6639 }
b34976b6 6640 return FALSE;
b49e97c9
TS
6641}
6642\f
6643/* Hook called by the linker routine which adds symbols from an object
6644 file. We must handle the special MIPS section numbers here. */
6645
b34976b6 6646bfd_boolean
9719ad41 6647_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 6648 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
6649 flagword *flagsp ATTRIBUTE_UNUSED,
6650 asection **secp, bfd_vma *valp)
b49e97c9
TS
6651{
6652 if (SGI_COMPAT (abfd)
6653 && (abfd->flags & DYNAMIC) != 0
6654 && strcmp (*namep, "_rld_new_interface") == 0)
6655 {
8dc1a139 6656 /* Skip IRIX5 rld entry name. */
b49e97c9 6657 *namep = NULL;
b34976b6 6658 return TRUE;
b49e97c9
TS
6659 }
6660
eedecc07
DD
6661 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6662 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6663 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6664 a magic symbol resolved by the linker, we ignore this bogus definition
6665 of _gp_disp. New ABI objects do not suffer from this problem so this
6666 is not done for them. */
6667 if (!NEWABI_P(abfd)
6668 && (sym->st_shndx == SHN_ABS)
6669 && (strcmp (*namep, "_gp_disp") == 0))
6670 {
6671 *namep = NULL;
6672 return TRUE;
6673 }
6674
b49e97c9
TS
6675 switch (sym->st_shndx)
6676 {
6677 case SHN_COMMON:
6678 /* Common symbols less than the GP size are automatically
6679 treated as SHN_MIPS_SCOMMON symbols. */
6680 if (sym->st_size > elf_gp_size (abfd)
b59eed79 6681 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
6682 || IRIX_COMPAT (abfd) == ict_irix6)
6683 break;
6684 /* Fall through. */
6685 case SHN_MIPS_SCOMMON:
6686 *secp = bfd_make_section_old_way (abfd, ".scommon");
6687 (*secp)->flags |= SEC_IS_COMMON;
6688 *valp = sym->st_size;
6689 break;
6690
6691 case SHN_MIPS_TEXT:
6692 /* This section is used in a shared object. */
6693 if (elf_tdata (abfd)->elf_text_section == NULL)
6694 {
6695 asymbol *elf_text_symbol;
6696 asection *elf_text_section;
6697 bfd_size_type amt = sizeof (asection);
6698
6699 elf_text_section = bfd_zalloc (abfd, amt);
6700 if (elf_text_section == NULL)
b34976b6 6701 return FALSE;
b49e97c9
TS
6702
6703 amt = sizeof (asymbol);
6704 elf_text_symbol = bfd_zalloc (abfd, amt);
6705 if (elf_text_symbol == NULL)
b34976b6 6706 return FALSE;
b49e97c9
TS
6707
6708 /* Initialize the section. */
6709
6710 elf_tdata (abfd)->elf_text_section = elf_text_section;
6711 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6712
6713 elf_text_section->symbol = elf_text_symbol;
6714 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6715
6716 elf_text_section->name = ".text";
6717 elf_text_section->flags = SEC_NO_FLAGS;
6718 elf_text_section->output_section = NULL;
6719 elf_text_section->owner = abfd;
6720 elf_text_symbol->name = ".text";
6721 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6722 elf_text_symbol->section = elf_text_section;
6723 }
6724 /* This code used to do *secp = bfd_und_section_ptr if
6725 info->shared. I don't know why, and that doesn't make sense,
6726 so I took it out. */
6727 *secp = elf_tdata (abfd)->elf_text_section;
6728 break;
6729
6730 case SHN_MIPS_ACOMMON:
6731 /* Fall through. XXX Can we treat this as allocated data? */
6732 case SHN_MIPS_DATA:
6733 /* This section is used in a shared object. */
6734 if (elf_tdata (abfd)->elf_data_section == NULL)
6735 {
6736 asymbol *elf_data_symbol;
6737 asection *elf_data_section;
6738 bfd_size_type amt = sizeof (asection);
6739
6740 elf_data_section = bfd_zalloc (abfd, amt);
6741 if (elf_data_section == NULL)
b34976b6 6742 return FALSE;
b49e97c9
TS
6743
6744 amt = sizeof (asymbol);
6745 elf_data_symbol = bfd_zalloc (abfd, amt);
6746 if (elf_data_symbol == NULL)
b34976b6 6747 return FALSE;
b49e97c9
TS
6748
6749 /* Initialize the section. */
6750
6751 elf_tdata (abfd)->elf_data_section = elf_data_section;
6752 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6753
6754 elf_data_section->symbol = elf_data_symbol;
6755 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6756
6757 elf_data_section->name = ".data";
6758 elf_data_section->flags = SEC_NO_FLAGS;
6759 elf_data_section->output_section = NULL;
6760 elf_data_section->owner = abfd;
6761 elf_data_symbol->name = ".data";
6762 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6763 elf_data_symbol->section = elf_data_section;
6764 }
6765 /* This code used to do *secp = bfd_und_section_ptr if
6766 info->shared. I don't know why, and that doesn't make sense,
6767 so I took it out. */
6768 *secp = elf_tdata (abfd)->elf_data_section;
6769 break;
6770
6771 case SHN_MIPS_SUNDEFINED:
6772 *secp = bfd_und_section_ptr;
6773 break;
6774 }
6775
6776 if (SGI_COMPAT (abfd)
6777 && ! info->shared
f13a99db 6778 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
6779 && strcmp (*namep, "__rld_obj_head") == 0)
6780 {
6781 struct elf_link_hash_entry *h;
14a793b2 6782 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6783
6784 /* Mark __rld_obj_head as dynamic. */
14a793b2 6785 bh = NULL;
b49e97c9 6786 if (! (_bfd_generic_link_add_one_symbol
9719ad41 6787 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 6788 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6789 return FALSE;
14a793b2
AM
6790
6791 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6792 h->non_elf = 0;
6793 h->def_regular = 1;
b49e97c9
TS
6794 h->type = STT_OBJECT;
6795
c152c796 6796 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6797 return FALSE;
b49e97c9 6798
b34976b6 6799 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b49e97c9
TS
6800 }
6801
6802 /* If this is a mips16 text symbol, add 1 to the value to make it
6803 odd. This will cause something like .word SYM to come up with
6804 the right value when it is loaded into the PC. */
30c09090 6805 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
6806 ++*valp;
6807
b34976b6 6808 return TRUE;
b49e97c9
TS
6809}
6810
6811/* This hook function is called before the linker writes out a global
6812 symbol. We mark symbols as small common if appropriate. This is
6813 also where we undo the increment of the value for a mips16 symbol. */
6814
6e0b88f1 6815int
9719ad41
RS
6816_bfd_mips_elf_link_output_symbol_hook
6817 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6818 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6819 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
6820{
6821 /* If we see a common symbol, which implies a relocatable link, then
6822 if a symbol was small common in an input file, mark it as small
6823 common in the output file. */
6824 if (sym->st_shndx == SHN_COMMON
6825 && strcmp (input_sec->name, ".scommon") == 0)
6826 sym->st_shndx = SHN_MIPS_SCOMMON;
6827
30c09090 6828 if (ELF_ST_IS_MIPS16 (sym->st_other))
79cda7cf 6829 sym->st_value &= ~1;
b49e97c9 6830
6e0b88f1 6831 return 1;
b49e97c9
TS
6832}
6833\f
6834/* Functions for the dynamic linker. */
6835
6836/* Create dynamic sections when linking against a dynamic object. */
6837
b34976b6 6838bfd_boolean
9719ad41 6839_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
6840{
6841 struct elf_link_hash_entry *h;
14a793b2 6842 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6843 flagword flags;
6844 register asection *s;
6845 const char * const *namep;
0a44bf69 6846 struct mips_elf_link_hash_table *htab;
b49e97c9 6847
0a44bf69 6848 htab = mips_elf_hash_table (info);
b49e97c9
TS
6849 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6850 | SEC_LINKER_CREATED | SEC_READONLY);
6851
0a44bf69
RS
6852 /* The psABI requires a read-only .dynamic section, but the VxWorks
6853 EABI doesn't. */
6854 if (!htab->is_vxworks)
b49e97c9 6855 {
0a44bf69
RS
6856 s = bfd_get_section_by_name (abfd, ".dynamic");
6857 if (s != NULL)
6858 {
6859 if (! bfd_set_section_flags (abfd, s, flags))
6860 return FALSE;
6861 }
b49e97c9
TS
6862 }
6863
6864 /* We need to create .got section. */
23cc69b6 6865 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
6866 return FALSE;
6867
0a44bf69 6868 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 6869 return FALSE;
b49e97c9 6870
b49e97c9 6871 /* Create .stub section. */
4e41d0d7
RS
6872 s = bfd_make_section_with_flags (abfd,
6873 MIPS_ELF_STUB_SECTION_NAME (abfd),
6874 flags | SEC_CODE);
6875 if (s == NULL
6876 || ! bfd_set_section_alignment (abfd, s,
6877 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6878 return FALSE;
6879 htab->sstubs = s;
b49e97c9
TS
6880
6881 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6882 && !info->shared
6883 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6884 {
3496cb2a
L
6885 s = bfd_make_section_with_flags (abfd, ".rld_map",
6886 flags &~ (flagword) SEC_READONLY);
b49e97c9 6887 if (s == NULL
b49e97c9
TS
6888 || ! bfd_set_section_alignment (abfd, s,
6889 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 6890 return FALSE;
b49e97c9
TS
6891 }
6892
6893 /* On IRIX5, we adjust add some additional symbols and change the
6894 alignments of several sections. There is no ABI documentation
6895 indicating that this is necessary on IRIX6, nor any evidence that
6896 the linker takes such action. */
6897 if (IRIX_COMPAT (abfd) == ict_irix5)
6898 {
6899 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6900 {
14a793b2 6901 bh = NULL;
b49e97c9 6902 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
6903 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6904 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6905 return FALSE;
14a793b2
AM
6906
6907 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6908 h->non_elf = 0;
6909 h->def_regular = 1;
b49e97c9
TS
6910 h->type = STT_SECTION;
6911
c152c796 6912 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6913 return FALSE;
b49e97c9
TS
6914 }
6915
6916 /* We need to create a .compact_rel section. */
6917 if (SGI_COMPAT (abfd))
6918 {
6919 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 6920 return FALSE;
b49e97c9
TS
6921 }
6922
44c410de 6923 /* Change alignments of some sections. */
b49e97c9
TS
6924 s = bfd_get_section_by_name (abfd, ".hash");
6925 if (s != NULL)
d80dcc6a 6926 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6927 s = bfd_get_section_by_name (abfd, ".dynsym");
6928 if (s != NULL)
d80dcc6a 6929 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6930 s = bfd_get_section_by_name (abfd, ".dynstr");
6931 if (s != NULL)
d80dcc6a 6932 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6933 s = bfd_get_section_by_name (abfd, ".reginfo");
6934 if (s != NULL)
d80dcc6a 6935 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6936 s = bfd_get_section_by_name (abfd, ".dynamic");
6937 if (s != NULL)
d80dcc6a 6938 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6939 }
6940
6941 if (!info->shared)
6942 {
14a793b2
AM
6943 const char *name;
6944
6945 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6946 bh = NULL;
6947 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
6948 (info, abfd, name, BSF_GLOBAL, bfd_abs_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 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6961 {
6962 /* __rld_map is a four byte word located in the .data section
6963 and is filled in by the rtld to contain a pointer to
6964 the _r_debug structure. Its symbol value will be set in
6965 _bfd_mips_elf_finish_dynamic_symbol. */
6966 s = bfd_get_section_by_name (abfd, ".rld_map");
0abfb97a 6967 BFD_ASSERT (s != NULL);
14a793b2 6968
0abfb97a
L
6969 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6970 bh = NULL;
6971 if (!(_bfd_generic_link_add_one_symbol
6972 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6973 get_elf_backend_data (abfd)->collect, &bh)))
6974 return FALSE;
b49e97c9 6975
0abfb97a
L
6976 h = (struct elf_link_hash_entry *) bh;
6977 h->non_elf = 0;
6978 h->def_regular = 1;
6979 h->type = STT_OBJECT;
6980
6981 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6982 return FALSE;
b49e97c9
TS
6983 }
6984 }
6985
861fb55a
DJ
6986 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
6987 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
6988 if (!_bfd_elf_create_dynamic_sections (abfd, info))
6989 return FALSE;
6990
6991 /* Cache the sections created above. */
6992 htab->splt = bfd_get_section_by_name (abfd, ".plt");
6993 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
0a44bf69
RS
6994 if (htab->is_vxworks)
6995 {
0a44bf69
RS
6996 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
6997 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
861fb55a
DJ
6998 }
6999 else
7000 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7001 if (!htab->sdynbss
7002 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7003 || !htab->srelplt
7004 || !htab->splt)
7005 abort ();
0a44bf69 7006
861fb55a
DJ
7007 if (htab->is_vxworks)
7008 {
0a44bf69
RS
7009 /* Do the usual VxWorks handling. */
7010 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7011 return FALSE;
7012
7013 /* Work out the PLT sizes. */
7014 if (info->shared)
7015 {
7016 htab->plt_header_size
7017 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7018 htab->plt_entry_size
7019 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7020 }
7021 else
7022 {
7023 htab->plt_header_size
7024 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7025 htab->plt_entry_size
7026 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7027 }
7028 }
861fb55a
DJ
7029 else if (!info->shared)
7030 {
7031 /* All variants of the plt0 entry are the same size. */
7032 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7033 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7034 }
0a44bf69 7035
b34976b6 7036 return TRUE;
b49e97c9
TS
7037}
7038\f
c224138d
RS
7039/* Return true if relocation REL against section SEC is a REL rather than
7040 RELA relocation. RELOCS is the first relocation in the section and
7041 ABFD is the bfd that contains SEC. */
7042
7043static bfd_boolean
7044mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7045 const Elf_Internal_Rela *relocs,
7046 const Elf_Internal_Rela *rel)
7047{
7048 Elf_Internal_Shdr *rel_hdr;
7049 const struct elf_backend_data *bed;
7050
7051 /* To determine which flavor or relocation this is, we depend on the
7052 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
7053 rel_hdr = &elf_section_data (sec)->rel_hdr;
7054 bed = get_elf_backend_data (abfd);
7055 if ((size_t) (rel - relocs)
7056 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7057 rel_hdr = elf_section_data (sec)->rel_hdr2;
7058 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
7059}
7060
7061/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7062 HOWTO is the relocation's howto and CONTENTS points to the contents
7063 of the section that REL is against. */
7064
7065static bfd_vma
7066mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7067 reloc_howto_type *howto, bfd_byte *contents)
7068{
7069 bfd_byte *location;
7070 unsigned int r_type;
7071 bfd_vma addend;
7072
7073 r_type = ELF_R_TYPE (abfd, rel->r_info);
7074 location = contents + rel->r_offset;
7075
7076 /* Get the addend, which is stored in the input file. */
7077 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7078 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7079 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7080
7081 return addend & howto->src_mask;
7082}
7083
7084/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7085 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7086 and update *ADDEND with the final addend. Return true on success
7087 or false if the LO16 could not be found. RELEND is the exclusive
7088 upper bound on the relocations for REL's section. */
7089
7090static bfd_boolean
7091mips_elf_add_lo16_rel_addend (bfd *abfd,
7092 const Elf_Internal_Rela *rel,
7093 const Elf_Internal_Rela *relend,
7094 bfd_byte *contents, bfd_vma *addend)
7095{
7096 unsigned int r_type, lo16_type;
7097 const Elf_Internal_Rela *lo16_relocation;
7098 reloc_howto_type *lo16_howto;
7099 bfd_vma l;
7100
7101 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7102 if (mips16_reloc_p (r_type))
c224138d
RS
7103 lo16_type = R_MIPS16_LO16;
7104 else
7105 lo16_type = R_MIPS_LO16;
7106
7107 /* The combined value is the sum of the HI16 addend, left-shifted by
7108 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7109 code does a `lui' of the HI16 value, and then an `addiu' of the
7110 LO16 value.)
7111
7112 Scan ahead to find a matching LO16 relocation.
7113
7114 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7115 be immediately following. However, for the IRIX6 ABI, the next
7116 relocation may be a composed relocation consisting of several
7117 relocations for the same address. In that case, the R_MIPS_LO16
7118 relocation may occur as one of these. We permit a similar
7119 extension in general, as that is useful for GCC.
7120
7121 In some cases GCC dead code elimination removes the LO16 but keeps
7122 the corresponding HI16. This is strictly speaking a violation of
7123 the ABI but not immediately harmful. */
7124 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7125 if (lo16_relocation == NULL)
7126 return FALSE;
7127
7128 /* Obtain the addend kept there. */
7129 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7130 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7131
7132 l <<= lo16_howto->rightshift;
7133 l = _bfd_mips_elf_sign_extend (l, 16);
7134
7135 *addend <<= 16;
7136 *addend += l;
7137 return TRUE;
7138}
7139
7140/* Try to read the contents of section SEC in bfd ABFD. Return true and
7141 store the contents in *CONTENTS on success. Assume that *CONTENTS
7142 already holds the contents if it is nonull on entry. */
7143
7144static bfd_boolean
7145mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7146{
7147 if (*contents)
7148 return TRUE;
7149
7150 /* Get cached copy if it exists. */
7151 if (elf_section_data (sec)->this_hdr.contents != NULL)
7152 {
7153 *contents = elf_section_data (sec)->this_hdr.contents;
7154 return TRUE;
7155 }
7156
7157 return bfd_malloc_and_get_section (abfd, sec, contents);
7158}
7159
b49e97c9
TS
7160/* Look through the relocs for a section during the first phase, and
7161 allocate space in the global offset table. */
7162
b34976b6 7163bfd_boolean
9719ad41
RS
7164_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7165 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7166{
7167 const char *name;
7168 bfd *dynobj;
7169 Elf_Internal_Shdr *symtab_hdr;
7170 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7171 size_t extsymoff;
7172 const Elf_Internal_Rela *rel;
7173 const Elf_Internal_Rela *rel_end;
b49e97c9 7174 asection *sreloc;
9c5bfbb7 7175 const struct elf_backend_data *bed;
0a44bf69 7176 struct mips_elf_link_hash_table *htab;
c224138d
RS
7177 bfd_byte *contents;
7178 bfd_vma addend;
7179 reloc_howto_type *howto;
b49e97c9 7180
1049f94e 7181 if (info->relocatable)
b34976b6 7182 return TRUE;
b49e97c9 7183
0a44bf69 7184 htab = mips_elf_hash_table (info);
b49e97c9
TS
7185 dynobj = elf_hash_table (info)->dynobj;
7186 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7187 sym_hashes = elf_sym_hashes (abfd);
7188 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7189
738e5348
RS
7190 bed = get_elf_backend_data (abfd);
7191 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7192
b49e97c9
TS
7193 /* Check for the mips16 stub sections. */
7194
7195 name = bfd_get_section_name (abfd, sec);
b9d58d71 7196 if (FN_STUB_P (name))
b49e97c9
TS
7197 {
7198 unsigned long r_symndx;
7199
7200 /* Look at the relocation information to figure out which symbol
7201 this is for. */
7202
738e5348
RS
7203 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7204 if (r_symndx == 0)
7205 {
7206 (*_bfd_error_handler)
7207 (_("%B: Warning: cannot determine the target function for"
7208 " stub section `%s'"),
7209 abfd, name);
7210 bfd_set_error (bfd_error_bad_value);
7211 return FALSE;
7212 }
b49e97c9
TS
7213
7214 if (r_symndx < extsymoff
7215 || sym_hashes[r_symndx - extsymoff] == NULL)
7216 {
7217 asection *o;
7218
7219 /* This stub is for a local symbol. This stub will only be
7220 needed if there is some relocation in this BFD, other
7221 than a 16 bit function call, which refers to this symbol. */
7222 for (o = abfd->sections; o != NULL; o = o->next)
7223 {
7224 Elf_Internal_Rela *sec_relocs;
7225 const Elf_Internal_Rela *r, *rend;
7226
7227 /* We can ignore stub sections when looking for relocs. */
7228 if ((o->flags & SEC_RELOC) == 0
7229 || o->reloc_count == 0
738e5348 7230 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7231 continue;
7232
45d6a902 7233 sec_relocs
9719ad41 7234 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7235 info->keep_memory);
b49e97c9 7236 if (sec_relocs == NULL)
b34976b6 7237 return FALSE;
b49e97c9
TS
7238
7239 rend = sec_relocs + o->reloc_count;
7240 for (r = sec_relocs; r < rend; r++)
7241 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7242 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7243 break;
7244
6cdc0ccc 7245 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7246 free (sec_relocs);
7247
7248 if (r < rend)
7249 break;
7250 }
7251
7252 if (o == NULL)
7253 {
7254 /* There is no non-call reloc for this stub, so we do
7255 not need it. Since this function is called before
7256 the linker maps input sections to output sections, we
7257 can easily discard it by setting the SEC_EXCLUDE
7258 flag. */
7259 sec->flags |= SEC_EXCLUDE;
b34976b6 7260 return TRUE;
b49e97c9
TS
7261 }
7262
7263 /* Record this stub in an array of local symbol stubs for
7264 this BFD. */
7265 if (elf_tdata (abfd)->local_stubs == NULL)
7266 {
7267 unsigned long symcount;
7268 asection **n;
7269 bfd_size_type amt;
7270
7271 if (elf_bad_symtab (abfd))
7272 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7273 else
7274 symcount = symtab_hdr->sh_info;
7275 amt = symcount * sizeof (asection *);
9719ad41 7276 n = bfd_zalloc (abfd, amt);
b49e97c9 7277 if (n == NULL)
b34976b6 7278 return FALSE;
b49e97c9
TS
7279 elf_tdata (abfd)->local_stubs = n;
7280 }
7281
b9d58d71 7282 sec->flags |= SEC_KEEP;
b49e97c9
TS
7283 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7284
7285 /* We don't need to set mips16_stubs_seen in this case.
7286 That flag is used to see whether we need to look through
7287 the global symbol table for stubs. We don't need to set
7288 it here, because we just have a local stub. */
7289 }
7290 else
7291 {
7292 struct mips_elf_link_hash_entry *h;
7293
7294 h = ((struct mips_elf_link_hash_entry *)
7295 sym_hashes[r_symndx - extsymoff]);
7296
973a3492
L
7297 while (h->root.root.type == bfd_link_hash_indirect
7298 || h->root.root.type == bfd_link_hash_warning)
7299 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7300
b49e97c9
TS
7301 /* H is the symbol this stub is for. */
7302
b9d58d71
TS
7303 /* If we already have an appropriate stub for this function, we
7304 don't need another one, so we can discard this one. Since
7305 this function is called before the linker maps input sections
7306 to output sections, we can easily discard it by setting the
7307 SEC_EXCLUDE flag. */
7308 if (h->fn_stub != NULL)
7309 {
7310 sec->flags |= SEC_EXCLUDE;
7311 return TRUE;
7312 }
7313
7314 sec->flags |= SEC_KEEP;
b49e97c9 7315 h->fn_stub = sec;
b34976b6 7316 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7317 }
7318 }
b9d58d71 7319 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7320 {
7321 unsigned long r_symndx;
7322 struct mips_elf_link_hash_entry *h;
7323 asection **loc;
7324
7325 /* Look at the relocation information to figure out which symbol
7326 this is for. */
7327
738e5348
RS
7328 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7329 if (r_symndx == 0)
7330 {
7331 (*_bfd_error_handler)
7332 (_("%B: Warning: cannot determine the target function for"
7333 " stub section `%s'"),
7334 abfd, name);
7335 bfd_set_error (bfd_error_bad_value);
7336 return FALSE;
7337 }
b49e97c9
TS
7338
7339 if (r_symndx < extsymoff
7340 || sym_hashes[r_symndx - extsymoff] == NULL)
7341 {
b9d58d71 7342 asection *o;
b49e97c9 7343
b9d58d71
TS
7344 /* This stub is for a local symbol. This stub will only be
7345 needed if there is some relocation (R_MIPS16_26) in this BFD
7346 that refers to this symbol. */
7347 for (o = abfd->sections; o != NULL; o = o->next)
7348 {
7349 Elf_Internal_Rela *sec_relocs;
7350 const Elf_Internal_Rela *r, *rend;
7351
7352 /* We can ignore stub sections when looking for relocs. */
7353 if ((o->flags & SEC_RELOC) == 0
7354 || o->reloc_count == 0
738e5348 7355 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7356 continue;
7357
7358 sec_relocs
7359 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7360 info->keep_memory);
7361 if (sec_relocs == NULL)
7362 return FALSE;
7363
7364 rend = sec_relocs + o->reloc_count;
7365 for (r = sec_relocs; r < rend; r++)
7366 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7367 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7368 break;
7369
7370 if (elf_section_data (o)->relocs != sec_relocs)
7371 free (sec_relocs);
7372
7373 if (r < rend)
7374 break;
7375 }
7376
7377 if (o == NULL)
7378 {
7379 /* There is no non-call reloc for this stub, so we do
7380 not need it. Since this function is called before
7381 the linker maps input sections to output sections, we
7382 can easily discard it by setting the SEC_EXCLUDE
7383 flag. */
7384 sec->flags |= SEC_EXCLUDE;
7385 return TRUE;
7386 }
7387
7388 /* Record this stub in an array of local symbol call_stubs for
7389 this BFD. */
7390 if (elf_tdata (abfd)->local_call_stubs == NULL)
7391 {
7392 unsigned long symcount;
7393 asection **n;
7394 bfd_size_type amt;
7395
7396 if (elf_bad_symtab (abfd))
7397 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7398 else
7399 symcount = symtab_hdr->sh_info;
7400 amt = symcount * sizeof (asection *);
7401 n = bfd_zalloc (abfd, amt);
7402 if (n == NULL)
7403 return FALSE;
7404 elf_tdata (abfd)->local_call_stubs = n;
7405 }
b49e97c9 7406
b9d58d71
TS
7407 sec->flags |= SEC_KEEP;
7408 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7409
b9d58d71
TS
7410 /* We don't need to set mips16_stubs_seen in this case.
7411 That flag is used to see whether we need to look through
7412 the global symbol table for stubs. We don't need to set
7413 it here, because we just have a local stub. */
7414 }
b49e97c9 7415 else
b49e97c9 7416 {
b9d58d71
TS
7417 h = ((struct mips_elf_link_hash_entry *)
7418 sym_hashes[r_symndx - extsymoff]);
7419
7420 /* H is the symbol this stub is for. */
7421
7422 if (CALL_FP_STUB_P (name))
7423 loc = &h->call_fp_stub;
7424 else
7425 loc = &h->call_stub;
7426
7427 /* If we already have an appropriate stub for this function, we
7428 don't need another one, so we can discard this one. Since
7429 this function is called before the linker maps input sections
7430 to output sections, we can easily discard it by setting the
7431 SEC_EXCLUDE flag. */
7432 if (*loc != NULL)
7433 {
7434 sec->flags |= SEC_EXCLUDE;
7435 return TRUE;
7436 }
b49e97c9 7437
b9d58d71
TS
7438 sec->flags |= SEC_KEEP;
7439 *loc = sec;
7440 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7441 }
b49e97c9
TS
7442 }
7443
b49e97c9 7444 sreloc = NULL;
c224138d 7445 contents = NULL;
b49e97c9
TS
7446 for (rel = relocs; rel < rel_end; ++rel)
7447 {
7448 unsigned long r_symndx;
7449 unsigned int r_type;
7450 struct elf_link_hash_entry *h;
861fb55a 7451 bfd_boolean can_make_dynamic_p;
b49e97c9
TS
7452
7453 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7454 r_type = ELF_R_TYPE (abfd, rel->r_info);
7455
7456 if (r_symndx < extsymoff)
7457 h = NULL;
7458 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7459 {
7460 (*_bfd_error_handler)
d003868e
AM
7461 (_("%B: Malformed reloc detected for section %s"),
7462 abfd, name);
b49e97c9 7463 bfd_set_error (bfd_error_bad_value);
b34976b6 7464 return FALSE;
b49e97c9
TS
7465 }
7466 else
7467 {
7468 h = sym_hashes[r_symndx - extsymoff];
3e08fb72
NC
7469 while (h != NULL
7470 && (h->root.type == bfd_link_hash_indirect
7471 || h->root.type == bfd_link_hash_warning))
861fb55a
DJ
7472 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7473 }
b49e97c9 7474
861fb55a
DJ
7475 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7476 relocation into a dynamic one. */
7477 can_make_dynamic_p = FALSE;
7478 switch (r_type)
7479 {
7480 case R_MIPS16_GOT16:
7481 case R_MIPS16_CALL16:
7482 case R_MIPS_GOT16:
7483 case R_MIPS_CALL16:
7484 case R_MIPS_CALL_HI16:
7485 case R_MIPS_CALL_LO16:
7486 case R_MIPS_GOT_HI16:
7487 case R_MIPS_GOT_LO16:
7488 case R_MIPS_GOT_PAGE:
7489 case R_MIPS_GOT_OFST:
7490 case R_MIPS_GOT_DISP:
7491 case R_MIPS_TLS_GOTTPREL:
7492 case R_MIPS_TLS_GD:
7493 case R_MIPS_TLS_LDM:
7494 if (dynobj == NULL)
7495 elf_hash_table (info)->dynobj = dynobj = abfd;
7496 if (!mips_elf_create_got_section (dynobj, info))
7497 return FALSE;
7498 if (htab->is_vxworks && !info->shared)
b49e97c9 7499 {
861fb55a
DJ
7500 (*_bfd_error_handler)
7501 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7502 abfd, (unsigned long) rel->r_offset);
7503 bfd_set_error (bfd_error_bad_value);
7504 return FALSE;
b49e97c9 7505 }
861fb55a 7506 break;
b49e97c9 7507
99da6b5f
AN
7508 /* This is just a hint; it can safely be ignored. Don't set
7509 has_static_relocs for the corresponding symbol. */
7510 case R_MIPS_JALR:
7511 break;
7512
861fb55a
DJ
7513 case R_MIPS_32:
7514 case R_MIPS_REL32:
7515 case R_MIPS_64:
7516 /* In VxWorks executables, references to external symbols
7517 must be handled using copy relocs or PLT entries; it is not
7518 possible to convert this relocation into a dynamic one.
7519
7520 For executables that use PLTs and copy-relocs, we have a
7521 choice between converting the relocation into a dynamic
7522 one or using copy relocations or PLT entries. It is
7523 usually better to do the former, unless the relocation is
7524 against a read-only section. */
7525 if ((info->shared
7526 || (h != NULL
7527 && !htab->is_vxworks
7528 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7529 && !(!info->nocopyreloc
7530 && !PIC_OBJECT_P (abfd)
7531 && MIPS_ELF_READONLY_SECTION (sec))))
7532 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 7533 {
861fb55a 7534 can_make_dynamic_p = TRUE;
b49e97c9
TS
7535 if (dynobj == NULL)
7536 elf_hash_table (info)->dynobj = dynobj = abfd;
b49e97c9 7537 break;
861fb55a
DJ
7538 }
7539 /* Fall through. */
b49e97c9 7540
861fb55a
DJ
7541 default:
7542 /* Most static relocations require pointer equality, except
7543 for branches. */
7544 if (h)
7545 h->pointer_equality_needed = TRUE;
7546 /* Fall through. */
b49e97c9 7547
861fb55a
DJ
7548 case R_MIPS_26:
7549 case R_MIPS_PC16:
7550 case R_MIPS16_26:
7551 if (h)
7552 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7553 break;
b49e97c9
TS
7554 }
7555
0a44bf69
RS
7556 if (h)
7557 {
0a44bf69
RS
7558 /* Relocations against the special VxWorks __GOTT_BASE__ and
7559 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7560 room for them in .rela.dyn. */
7561 if (is_gott_symbol (info, h))
7562 {
7563 if (sreloc == NULL)
7564 {
7565 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7566 if (sreloc == NULL)
7567 return FALSE;
7568 }
7569 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
7570 if (MIPS_ELF_READONLY_SECTION (sec))
7571 /* We tell the dynamic linker that there are
7572 relocations against the text segment. */
7573 info->flags |= DF_TEXTREL;
0a44bf69
RS
7574 }
7575 }
7576 else if (r_type == R_MIPS_CALL_LO16
7577 || r_type == R_MIPS_GOT_LO16
7578 || r_type == R_MIPS_GOT_DISP
738e5348 7579 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
7580 {
7581 /* We may need a local GOT entry for this relocation. We
7582 don't count R_MIPS_GOT_PAGE because we can estimate the
7583 maximum number of pages needed by looking at the size of
738e5348
RS
7584 the segment. Similar comments apply to R_MIPS*_GOT16 and
7585 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 7586 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 7587 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 7588 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0
RS
7589 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7590 rel->r_addend, info, 0))
f4416af6 7591 return FALSE;
b49e97c9
TS
7592 }
7593
861fb55a
DJ
7594 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7595 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7596
b49e97c9
TS
7597 switch (r_type)
7598 {
7599 case R_MIPS_CALL16:
738e5348 7600 case R_MIPS16_CALL16:
b49e97c9
TS
7601 if (h == NULL)
7602 {
7603 (*_bfd_error_handler)
d003868e
AM
7604 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7605 abfd, (unsigned long) rel->r_offset);
b49e97c9 7606 bfd_set_error (bfd_error_bad_value);
b34976b6 7607 return FALSE;
b49e97c9
TS
7608 }
7609 /* Fall through. */
7610
7611 case R_MIPS_CALL_HI16:
7612 case R_MIPS_CALL_LO16:
7613 if (h != NULL)
7614 {
d334575b 7615 /* VxWorks call relocations point at the function's .got.plt
0a44bf69
RS
7616 entry, which will be allocated by adjust_dynamic_symbol.
7617 Otherwise, this symbol requires a global GOT entry. */
8275b357 7618 if ((!htab->is_vxworks || h->forced_local)
a8028dd0 7619 && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
b34976b6 7620 return FALSE;
b49e97c9
TS
7621
7622 /* We need a stub, not a plt entry for the undefined
7623 function. But we record it as if it needs plt. See
c152c796 7624 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 7625 h->needs_plt = 1;
b49e97c9
TS
7626 h->type = STT_FUNC;
7627 }
7628 break;
7629
0fdc1bf1
AO
7630 case R_MIPS_GOT_PAGE:
7631 /* If this is a global, overridable symbol, GOT_PAGE will
7632 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d 7633 if (h)
0fdc1bf1
AO
7634 {
7635 struct mips_elf_link_hash_entry *hmips =
7636 (struct mips_elf_link_hash_entry *) h;
143d77c5 7637
3a3b6725 7638 /* This symbol is definitely not overridable. */
f5385ebf 7639 if (hmips->root.def_regular
0fdc1bf1 7640 && ! (info->shared && ! info->symbolic
f5385ebf 7641 && ! hmips->root.forced_local))
c224138d 7642 h = NULL;
0fdc1bf1
AO
7643 }
7644 /* Fall through. */
7645
738e5348 7646 case R_MIPS16_GOT16:
b49e97c9
TS
7647 case R_MIPS_GOT16:
7648 case R_MIPS_GOT_HI16:
7649 case R_MIPS_GOT_LO16:
3a3b6725 7650 if (!h || r_type == R_MIPS_GOT_PAGE)
c224138d 7651 {
3a3b6725
DJ
7652 /* This relocation needs (or may need, if h != NULL) a
7653 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7654 know for sure until we know whether the symbol is
7655 preemptible. */
c224138d
RS
7656 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7657 {
7658 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7659 return FALSE;
7660 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7661 addend = mips_elf_read_rel_addend (abfd, rel,
7662 howto, contents);
7663 if (r_type == R_MIPS_GOT16)
7664 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7665 contents, &addend);
7666 else
7667 addend <<= howto->rightshift;
7668 }
7669 else
7670 addend = rel->r_addend;
a8028dd0
RS
7671 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7672 addend))
c224138d
RS
7673 return FALSE;
7674 break;
7675 }
7676 /* Fall through. */
7677
b49e97c9 7678 case R_MIPS_GOT_DISP:
a8028dd0 7679 if (h && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
b34976b6 7680 return FALSE;
b49e97c9
TS
7681 break;
7682
0f20cc35
DJ
7683 case R_MIPS_TLS_GOTTPREL:
7684 if (info->shared)
7685 info->flags |= DF_STATIC_TLS;
7686 /* Fall through */
7687
7688 case R_MIPS_TLS_LDM:
7689 if (r_type == R_MIPS_TLS_LDM)
7690 {
7691 r_symndx = 0;
7692 h = NULL;
7693 }
7694 /* Fall through */
7695
7696 case R_MIPS_TLS_GD:
7697 /* This symbol requires a global offset table entry, or two
7698 for TLS GD relocations. */
7699 {
7700 unsigned char flag = (r_type == R_MIPS_TLS_GD
7701 ? GOT_TLS_GD
7702 : r_type == R_MIPS_TLS_LDM
7703 ? GOT_TLS_LDM
7704 : GOT_TLS_IE);
7705 if (h != NULL)
7706 {
7707 struct mips_elf_link_hash_entry *hmips =
7708 (struct mips_elf_link_hash_entry *) h;
7709 hmips->tls_type |= flag;
7710
a8028dd0
RS
7711 if (h && !mips_elf_record_global_got_symbol (h, abfd,
7712 info, flag))
0f20cc35
DJ
7713 return FALSE;
7714 }
7715 else
7716 {
7717 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7718
a8028dd0
RS
7719 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7720 rel->r_addend,
7721 info, flag))
0f20cc35
DJ
7722 return FALSE;
7723 }
7724 }
7725 break;
7726
b49e97c9
TS
7727 case R_MIPS_32:
7728 case R_MIPS_REL32:
7729 case R_MIPS_64:
0a44bf69
RS
7730 /* In VxWorks executables, references to external symbols
7731 are handled using copy relocs or PLT stubs, so there's
7732 no need to add a .rela.dyn entry for this relocation. */
861fb55a 7733 if (can_make_dynamic_p)
b49e97c9
TS
7734 {
7735 if (sreloc == NULL)
7736 {
0a44bf69 7737 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 7738 if (sreloc == NULL)
f4416af6 7739 return FALSE;
b49e97c9 7740 }
9a59ad6b 7741 if (info->shared && h == NULL)
82f0cfbd
EC
7742 {
7743 /* When creating a shared object, we must copy these
7744 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
7745 relocs. Make room for this reloc in .rel(a).dyn. */
7746 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 7747 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7748 /* We tell the dynamic linker that there are
7749 relocations against the text segment. */
7750 info->flags |= DF_TEXTREL;
7751 }
b49e97c9
TS
7752 else
7753 {
7754 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 7755
9a59ad6b
DJ
7756 /* For a shared object, we must copy this relocation
7757 unless the symbol turns out to be undefined and
7758 weak with non-default visibility, in which case
7759 it will be left as zero.
7760
7761 We could elide R_MIPS_REL32 for locally binding symbols
7762 in shared libraries, but do not yet do so.
7763
7764 For an executable, we only need to copy this
7765 reloc if the symbol is defined in a dynamic
7766 object. */
b49e97c9
TS
7767 hmips = (struct mips_elf_link_hash_entry *) h;
7768 ++hmips->possibly_dynamic_relocs;
943284cc 7769 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7770 /* We need it to tell the dynamic linker if there
7771 are relocations against the text segment. */
7772 hmips->readonly_reloc = TRUE;
b49e97c9 7773 }
b49e97c9
TS
7774 }
7775
7776 if (SGI_COMPAT (abfd))
7777 mips_elf_hash_table (info)->compact_rel_size +=
7778 sizeof (Elf32_External_crinfo);
7779 break;
7780
7781 case R_MIPS_26:
7782 case R_MIPS_GPREL16:
7783 case R_MIPS_LITERAL:
7784 case R_MIPS_GPREL32:
7785 if (SGI_COMPAT (abfd))
7786 mips_elf_hash_table (info)->compact_rel_size +=
7787 sizeof (Elf32_External_crinfo);
7788 break;
7789
7790 /* This relocation describes the C++ object vtable hierarchy.
7791 Reconstruct it for later use during GC. */
7792 case R_MIPS_GNU_VTINHERIT:
c152c796 7793 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 7794 return FALSE;
b49e97c9
TS
7795 break;
7796
7797 /* This relocation describes which C++ vtable entries are actually
7798 used. Record for later use during GC. */
7799 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
7800 BFD_ASSERT (h != NULL);
7801 if (h != NULL
7802 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 7803 return FALSE;
b49e97c9
TS
7804 break;
7805
7806 default:
7807 break;
7808 }
7809
7810 /* We must not create a stub for a symbol that has relocations
0a44bf69
RS
7811 related to taking the function's address. This doesn't apply to
7812 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7813 a normal .got entry. */
7814 if (!htab->is_vxworks && h != NULL)
7815 switch (r_type)
7816 {
7817 default:
7818 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7819 break;
738e5348 7820 case R_MIPS16_CALL16:
0a44bf69
RS
7821 case R_MIPS_CALL16:
7822 case R_MIPS_CALL_HI16:
7823 case R_MIPS_CALL_LO16:
7824 case R_MIPS_JALR:
7825 break;
7826 }
b49e97c9 7827
738e5348
RS
7828 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7829 if there is one. We only need to handle global symbols here;
7830 we decide whether to keep or delete stubs for local symbols
7831 when processing the stub's relocations. */
b49e97c9 7832 if (h != NULL
738e5348
RS
7833 && !mips16_call_reloc_p (r_type)
7834 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
7835 {
7836 struct mips_elf_link_hash_entry *mh;
7837
7838 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 7839 mh->need_fn_stub = TRUE;
b49e97c9 7840 }
861fb55a
DJ
7841
7842 /* Refuse some position-dependent relocations when creating a
7843 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7844 not PIC, but we can create dynamic relocations and the result
7845 will be fine. Also do not refuse R_MIPS_LO16, which can be
7846 combined with R_MIPS_GOT16. */
7847 if (info->shared)
7848 {
7849 switch (r_type)
7850 {
7851 case R_MIPS16_HI16:
7852 case R_MIPS_HI16:
7853 case R_MIPS_HIGHER:
7854 case R_MIPS_HIGHEST:
7855 /* Don't refuse a high part relocation if it's against
7856 no symbol (e.g. part of a compound relocation). */
7857 if (r_symndx == 0)
7858 break;
7859
7860 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7861 and has a special meaning. */
7862 if (!NEWABI_P (abfd) && h != NULL
7863 && strcmp (h->root.root.string, "_gp_disp") == 0)
7864 break;
7865
7866 /* FALLTHROUGH */
7867
7868 case R_MIPS16_26:
7869 case R_MIPS_26:
7870 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7871 (*_bfd_error_handler)
7872 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7873 abfd, howto->name,
7874 (h) ? h->root.root.string : "a local symbol");
7875 bfd_set_error (bfd_error_bad_value);
7876 return FALSE;
7877 default:
7878 break;
7879 }
7880 }
b49e97c9
TS
7881 }
7882
b34976b6 7883 return TRUE;
b49e97c9
TS
7884}
7885\f
d0647110 7886bfd_boolean
9719ad41
RS
7887_bfd_mips_relax_section (bfd *abfd, asection *sec,
7888 struct bfd_link_info *link_info,
7889 bfd_boolean *again)
d0647110
AO
7890{
7891 Elf_Internal_Rela *internal_relocs;
7892 Elf_Internal_Rela *irel, *irelend;
7893 Elf_Internal_Shdr *symtab_hdr;
7894 bfd_byte *contents = NULL;
d0647110
AO
7895 size_t extsymoff;
7896 bfd_boolean changed_contents = FALSE;
7897 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7898 Elf_Internal_Sym *isymbuf = NULL;
7899
7900 /* We are not currently changing any sizes, so only one pass. */
7901 *again = FALSE;
7902
1049f94e 7903 if (link_info->relocatable)
d0647110
AO
7904 return TRUE;
7905
9719ad41 7906 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 7907 link_info->keep_memory);
d0647110
AO
7908 if (internal_relocs == NULL)
7909 return TRUE;
7910
7911 irelend = internal_relocs + sec->reloc_count
7912 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7913 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7914 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7915
7916 for (irel = internal_relocs; irel < irelend; irel++)
7917 {
7918 bfd_vma symval;
7919 bfd_signed_vma sym_offset;
7920 unsigned int r_type;
7921 unsigned long r_symndx;
7922 asection *sym_sec;
7923 unsigned long instruction;
7924
7925 /* Turn jalr into bgezal, and jr into beq, if they're marked
7926 with a JALR relocation, that indicate where they jump to.
7927 This saves some pipeline bubbles. */
7928 r_type = ELF_R_TYPE (abfd, irel->r_info);
7929 if (r_type != R_MIPS_JALR)
7930 continue;
7931
7932 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7933 /* Compute the address of the jump target. */
7934 if (r_symndx >= extsymoff)
7935 {
7936 struct mips_elf_link_hash_entry *h
7937 = ((struct mips_elf_link_hash_entry *)
7938 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7939
7940 while (h->root.root.type == bfd_link_hash_indirect
7941 || h->root.root.type == bfd_link_hash_warning)
7942 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 7943
d0647110
AO
7944 /* If a symbol is undefined, or if it may be overridden,
7945 skip it. */
7946 if (! ((h->root.root.type == bfd_link_hash_defined
7947 || h->root.root.type == bfd_link_hash_defweak)
7948 && h->root.root.u.def.section)
7949 || (link_info->shared && ! link_info->symbolic
f5385ebf 7950 && !h->root.forced_local))
d0647110
AO
7951 continue;
7952
7953 sym_sec = h->root.root.u.def.section;
7954 if (sym_sec->output_section)
7955 symval = (h->root.root.u.def.value
7956 + sym_sec->output_section->vma
7957 + sym_sec->output_offset);
7958 else
7959 symval = h->root.root.u.def.value;
7960 }
7961 else
7962 {
7963 Elf_Internal_Sym *isym;
7964
7965 /* Read this BFD's symbols if we haven't done so already. */
7966 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7967 {
7968 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7969 if (isymbuf == NULL)
7970 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7971 symtab_hdr->sh_info, 0,
7972 NULL, NULL, NULL);
7973 if (isymbuf == NULL)
7974 goto relax_return;
7975 }
7976
7977 isym = isymbuf + r_symndx;
7978 if (isym->st_shndx == SHN_UNDEF)
7979 continue;
7980 else if (isym->st_shndx == SHN_ABS)
7981 sym_sec = bfd_abs_section_ptr;
7982 else if (isym->st_shndx == SHN_COMMON)
7983 sym_sec = bfd_com_section_ptr;
7984 else
7985 sym_sec
7986 = bfd_section_from_elf_index (abfd, isym->st_shndx);
7987 symval = isym->st_value
7988 + sym_sec->output_section->vma
7989 + sym_sec->output_offset;
7990 }
7991
7992 /* Compute branch offset, from delay slot of the jump to the
7993 branch target. */
7994 sym_offset = (symval + irel->r_addend)
7995 - (sec_start + irel->r_offset + 4);
7996
7997 /* Branch offset must be properly aligned. */
7998 if ((sym_offset & 3) != 0)
7999 continue;
8000
8001 sym_offset >>= 2;
8002
8003 /* Check that it's in range. */
8004 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8005 continue;
143d77c5 8006
d0647110 8007 /* Get the section contents if we haven't done so already. */
c224138d
RS
8008 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8009 goto relax_return;
d0647110
AO
8010
8011 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8012
8013 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8014 if ((instruction & 0xfc1fffff) == 0x0000f809)
8015 instruction = 0x04110000;
8016 /* If it was jr <reg>, turn it into b <target>. */
8017 else if ((instruction & 0xfc1fffff) == 0x00000008)
8018 instruction = 0x10000000;
8019 else
8020 continue;
8021
8022 instruction |= (sym_offset & 0xffff);
8023 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8024 changed_contents = TRUE;
8025 }
8026
8027 if (contents != NULL
8028 && elf_section_data (sec)->this_hdr.contents != contents)
8029 {
8030 if (!changed_contents && !link_info->keep_memory)
8031 free (contents);
8032 else
8033 {
8034 /* Cache the section contents for elf_link_input_bfd. */
8035 elf_section_data (sec)->this_hdr.contents = contents;
8036 }
8037 }
8038 return TRUE;
8039
143d77c5 8040 relax_return:
eea6121a
AM
8041 if (contents != NULL
8042 && elf_section_data (sec)->this_hdr.contents != contents)
8043 free (contents);
d0647110
AO
8044 return FALSE;
8045}
8046\f
9a59ad6b
DJ
8047/* Allocate space for global sym dynamic relocs. */
8048
8049static bfd_boolean
8050allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8051{
8052 struct bfd_link_info *info = inf;
8053 bfd *dynobj;
8054 struct mips_elf_link_hash_entry *hmips;
8055 struct mips_elf_link_hash_table *htab;
8056
8057 htab = mips_elf_hash_table (info);
8058 dynobj = elf_hash_table (info)->dynobj;
8059 hmips = (struct mips_elf_link_hash_entry *) h;
8060
8061 /* VxWorks executables are handled elsewhere; we only need to
8062 allocate relocations in shared objects. */
8063 if (htab->is_vxworks && !info->shared)
8064 return TRUE;
8065
63897e2c
RS
8066 /* Ignore indirect and warning symbols. All relocations against
8067 such symbols will be redirected to the target symbol. */
8068 if (h->root.type == bfd_link_hash_indirect
8069 || h->root.type == bfd_link_hash_warning)
8070 return TRUE;
8071
9a59ad6b
DJ
8072 /* If this symbol is defined in a dynamic object, or we are creating
8073 a shared library, we will need to copy any R_MIPS_32 or
8074 R_MIPS_REL32 relocs against it into the output file. */
8075 if (! info->relocatable
8076 && hmips->possibly_dynamic_relocs != 0
8077 && (h->root.type == bfd_link_hash_defweak
8078 || !h->def_regular
8079 || info->shared))
8080 {
8081 bfd_boolean do_copy = TRUE;
8082
8083 if (h->root.type == bfd_link_hash_undefweak)
8084 {
8085 /* Do not copy relocations for undefined weak symbols with
8086 non-default visibility. */
8087 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8088 do_copy = FALSE;
8089
8090 /* Make sure undefined weak symbols are output as a dynamic
8091 symbol in PIEs. */
8092 else if (h->dynindx == -1 && !h->forced_local)
8093 {
8094 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8095 return FALSE;
8096 }
8097 }
8098
8099 if (do_copy)
8100 {
aff469fa
RS
8101 /* Even though we don't directly need a GOT entry for this symbol,
8102 a symbol must have a dynamic symbol table index greater that
8103 DT_MIPS_GOTSYM if there are dynamic relocations against it. */
8104 if (hmips->global_got_area > GGA_RELOC_ONLY)
8105 hmips->global_got_area = GGA_RELOC_ONLY;
8106
9a59ad6b
DJ
8107 mips_elf_allocate_dynamic_relocations
8108 (dynobj, info, hmips->possibly_dynamic_relocs);
8109 if (hmips->readonly_reloc)
8110 /* We tell the dynamic linker that there are relocations
8111 against the text segment. */
8112 info->flags |= DF_TEXTREL;
8113 }
8114 }
8115
8116 return TRUE;
8117}
8118
b49e97c9
TS
8119/* Adjust a symbol defined by a dynamic object and referenced by a
8120 regular object. The current definition is in some section of the
8121 dynamic object, but we're not including those sections. We have to
8122 change the definition to something the rest of the link can
8123 understand. */
8124
b34976b6 8125bfd_boolean
9719ad41
RS
8126_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8127 struct elf_link_hash_entry *h)
b49e97c9
TS
8128{
8129 bfd *dynobj;
8130 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8131 struct mips_elf_link_hash_table *htab;
b49e97c9 8132
5108fc1b 8133 htab = mips_elf_hash_table (info);
b49e97c9 8134 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8135 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8136
8137 /* Make sure we know what is going on here. */
8138 BFD_ASSERT (dynobj != NULL
f5385ebf 8139 && (h->needs_plt
f6e332e6 8140 || h->u.weakdef != NULL
f5385ebf
AM
8141 || (h->def_dynamic
8142 && h->ref_regular
8143 && !h->def_regular)));
b49e97c9 8144
b49e97c9 8145 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8146
861fb55a
DJ
8147 /* If there are call relocations against an externally-defined symbol,
8148 see whether we can create a MIPS lazy-binding stub for it. We can
8149 only do this if all references to the function are through call
8150 relocations, and in that case, the traditional lazy-binding stubs
8151 are much more efficient than PLT entries.
8152
8153 Traditional stubs are only available on SVR4 psABI-based systems;
8154 VxWorks always uses PLTs instead. */
8155 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8156 {
8157 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8158 return TRUE;
b49e97c9
TS
8159
8160 /* If this symbol is not defined in a regular file, then set
8161 the symbol to the stub location. This is required to make
8162 function pointers compare as equal between the normal
8163 executable and the shared library. */
f5385ebf 8164 if (!h->def_regular)
b49e97c9 8165 {
33bb52fb
RS
8166 hmips->needs_lazy_stub = TRUE;
8167 htab->lazy_stub_count++;
b34976b6 8168 return TRUE;
b49e97c9
TS
8169 }
8170 }
861fb55a
DJ
8171 /* As above, VxWorks requires PLT entries for externally-defined
8172 functions that are only accessed through call relocations.
b49e97c9 8173
861fb55a
DJ
8174 Both VxWorks and non-VxWorks targets also need PLT entries if there
8175 are static-only relocations against an externally-defined function.
8176 This can technically occur for shared libraries if there are
8177 branches to the symbol, although it is unlikely that this will be
8178 used in practice due to the short ranges involved. It can occur
8179 for any relative or absolute relocation in executables; in that
8180 case, the PLT entry becomes the function's canonical address. */
8181 else if (((h->needs_plt && !hmips->no_fn_stub)
8182 || (h->type == STT_FUNC && hmips->has_static_relocs))
8183 && htab->use_plts_and_copy_relocs
8184 && !SYMBOL_CALLS_LOCAL (info, h)
8185 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8186 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8187 {
861fb55a
DJ
8188 /* If this is the first symbol to need a PLT entry, allocate room
8189 for the header. */
8190 if (htab->splt->size == 0)
8191 {
8192 BFD_ASSERT (htab->sgotplt->size == 0);
0a44bf69 8193
861fb55a
DJ
8194 /* If we're using the PLT additions to the psABI, each PLT
8195 entry is 16 bytes and the PLT0 entry is 32 bytes.
8196 Encourage better cache usage by aligning. We do this
8197 lazily to avoid pessimizing traditional objects. */
8198 if (!htab->is_vxworks
8199 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8200 return FALSE;
0a44bf69 8201
861fb55a
DJ
8202 /* Make sure that .got.plt is word-aligned. We do this lazily
8203 for the same reason as above. */
8204 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8205 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8206 return FALSE;
0a44bf69 8207
861fb55a 8208 htab->splt->size += htab->plt_header_size;
0a44bf69 8209
861fb55a
DJ
8210 /* On non-VxWorks targets, the first two entries in .got.plt
8211 are reserved. */
8212 if (!htab->is_vxworks)
8213 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
0a44bf69 8214
861fb55a
DJ
8215 /* On VxWorks, also allocate room for the header's
8216 .rela.plt.unloaded entries. */
8217 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8218 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8219 }
8220
8221 /* Assign the next .plt entry to this symbol. */
8222 h->plt.offset = htab->splt->size;
8223 htab->splt->size += htab->plt_entry_size;
8224
8225 /* If the output file has no definition of the symbol, set the
861fb55a 8226 symbol's value to the address of the stub. */
131eb6b7 8227 if (!info->shared && !h->def_regular)
0a44bf69
RS
8228 {
8229 h->root.u.def.section = htab->splt;
8230 h->root.u.def.value = h->plt.offset;
861fb55a
DJ
8231 /* For VxWorks, point at the PLT load stub rather than the
8232 lazy resolution stub; this stub will become the canonical
8233 function address. */
8234 if (htab->is_vxworks)
8235 h->root.u.def.value += 8;
0a44bf69
RS
8236 }
8237
861fb55a
DJ
8238 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8239 relocation. */
8240 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8241 htab->srelplt->size += (htab->is_vxworks
8242 ? MIPS_ELF_RELA_SIZE (dynobj)
8243 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8244
8245 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8246 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8247 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8248
861fb55a
DJ
8249 /* All relocations against this symbol that could have been made
8250 dynamic will now refer to the PLT entry instead. */
8251 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8252
0a44bf69
RS
8253 return TRUE;
8254 }
8255
8256 /* If this is a weak symbol, and there is a real definition, the
8257 processor independent code will have arranged for us to see the
8258 real definition first, and we can just use the same value. */
8259 if (h->u.weakdef != NULL)
8260 {
8261 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8262 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8263 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8264 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8265 return TRUE;
8266 }
8267
861fb55a
DJ
8268 /* Otherwise, there is nothing further to do for symbols defined
8269 in regular objects. */
8270 if (h->def_regular)
0a44bf69
RS
8271 return TRUE;
8272
861fb55a
DJ
8273 /* There's also nothing more to do if we'll convert all relocations
8274 against this symbol into dynamic relocations. */
8275 if (!hmips->has_static_relocs)
8276 return TRUE;
8277
8278 /* We're now relying on copy relocations. Complain if we have
8279 some that we can't convert. */
8280 if (!htab->use_plts_and_copy_relocs || info->shared)
8281 {
8282 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8283 "dynamic symbol %s"),
8284 h->root.root.string);
8285 bfd_set_error (bfd_error_bad_value);
8286 return FALSE;
8287 }
8288
0a44bf69
RS
8289 /* We must allocate the symbol in our .dynbss section, which will
8290 become part of the .bss section of the executable. There will be
8291 an entry for this symbol in the .dynsym section. The dynamic
8292 object will contain position independent code, so all references
8293 from the dynamic object to this symbol will go through the global
8294 offset table. The dynamic linker will use the .dynsym entry to
8295 determine the address it must put in the global offset table, so
8296 both the dynamic object and the regular object will refer to the
8297 same memory location for the variable. */
8298
8299 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8300 {
861fb55a
DJ
8301 if (htab->is_vxworks)
8302 htab->srelbss->size += sizeof (Elf32_External_Rela);
8303 else
8304 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
8305 h->needs_copy = 1;
8306 }
8307
861fb55a
DJ
8308 /* All relocations against this symbol that could have been made
8309 dynamic will now refer to the local copy instead. */
8310 hmips->possibly_dynamic_relocs = 0;
8311
027297b7 8312 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 8313}
b49e97c9
TS
8314\f
8315/* This function is called after all the input files have been read,
8316 and the input sections have been assigned to output sections. We
8317 check for any mips16 stub sections that we can discard. */
8318
b34976b6 8319bfd_boolean
9719ad41
RS
8320_bfd_mips_elf_always_size_sections (bfd *output_bfd,
8321 struct bfd_link_info *info)
b49e97c9
TS
8322{
8323 asection *ri;
0a44bf69 8324 struct mips_elf_link_hash_table *htab;
861fb55a 8325 struct mips_htab_traverse_info hti;
0a44bf69
RS
8326
8327 htab = mips_elf_hash_table (info);
f4416af6 8328
b49e97c9
TS
8329 /* The .reginfo section has a fixed size. */
8330 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8331 if (ri != NULL)
9719ad41 8332 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 8333
861fb55a
DJ
8334 hti.info = info;
8335 hti.output_bfd = output_bfd;
8336 hti.error = FALSE;
8337 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8338 mips_elf_check_symbols, &hti);
8339 if (hti.error)
8340 return FALSE;
f4416af6 8341
33bb52fb
RS
8342 return TRUE;
8343}
8344
8345/* If the link uses a GOT, lay it out and work out its size. */
8346
8347static bfd_boolean
8348mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8349{
8350 bfd *dynobj;
8351 asection *s;
8352 struct mips_got_info *g;
33bb52fb
RS
8353 bfd_size_type loadable_size = 0;
8354 bfd_size_type page_gotno;
8355 bfd *sub;
8356 struct mips_elf_count_tls_arg count_tls_arg;
8357 struct mips_elf_link_hash_table *htab;
8358
8359 htab = mips_elf_hash_table (info);
a8028dd0 8360 s = htab->sgot;
f4416af6 8361 if (s == NULL)
b34976b6 8362 return TRUE;
b49e97c9 8363
33bb52fb 8364 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
8365 g = htab->got_info;
8366
861fb55a
DJ
8367 /* Allocate room for the reserved entries. VxWorks always reserves
8368 3 entries; other objects only reserve 2 entries. */
8369 BFD_ASSERT (g->assigned_gotno == 0);
8370 if (htab->is_vxworks)
8371 htab->reserved_gotno = 3;
8372 else
8373 htab->reserved_gotno = 2;
8374 g->local_gotno += htab->reserved_gotno;
8375 g->assigned_gotno = htab->reserved_gotno;
8376
33bb52fb
RS
8377 /* Replace entries for indirect and warning symbols with entries for
8378 the target symbol. */
8379 if (!mips_elf_resolve_final_got_entries (g))
8380 return FALSE;
f4416af6 8381
d4596a51
RS
8382 /* Count the number of GOT symbols. */
8383 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, g);
f4416af6 8384
33bb52fb
RS
8385 /* Calculate the total loadable size of the output. That
8386 will give us the maximum number of GOT_PAGE entries
8387 required. */
8388 for (sub = info->input_bfds; sub; sub = sub->link_next)
8389 {
8390 asection *subsection;
5108fc1b 8391
33bb52fb
RS
8392 for (subsection = sub->sections;
8393 subsection;
8394 subsection = subsection->next)
8395 {
8396 if ((subsection->flags & SEC_ALLOC) == 0)
8397 continue;
8398 loadable_size += ((subsection->size + 0xf)
8399 &~ (bfd_size_type) 0xf);
8400 }
8401 }
f4416af6 8402
0a44bf69 8403 if (htab->is_vxworks)
738e5348 8404 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
8405 relocations against local symbols evaluate to "G", and the EABI does
8406 not include R_MIPS_GOT_PAGE. */
c224138d 8407 page_gotno = 0;
0a44bf69
RS
8408 else
8409 /* Assume there are two loadable segments consisting of contiguous
8410 sections. Is 5 enough? */
c224138d
RS
8411 page_gotno = (loadable_size >> 16) + 5;
8412
8413 /* Choose the smaller of the two estimates; both are intended to be
8414 conservative. */
8415 if (page_gotno > g->page_gotno)
8416 page_gotno = g->page_gotno;
f4416af6 8417
c224138d 8418 g->local_gotno += page_gotno;
eea6121a 8419 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
d4596a51 8420 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6 8421
0f20cc35
DJ
8422 /* We need to calculate tls_gotno for global symbols at this point
8423 instead of building it up earlier, to avoid doublecounting
8424 entries for one global symbol from multiple input files. */
8425 count_tls_arg.info = info;
8426 count_tls_arg.needed = 0;
8427 elf_link_hash_traverse (elf_hash_table (info),
8428 mips_elf_count_global_tls_entries,
8429 &count_tls_arg);
8430 g->tls_gotno += count_tls_arg.needed;
8431 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8432
0a44bf69
RS
8433 /* VxWorks does not support multiple GOTs. It initializes $gp to
8434 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8435 dynamic loader. */
33bb52fb
RS
8436 if (htab->is_vxworks)
8437 {
8438 /* VxWorks executables do not need a GOT. */
8439 if (info->shared)
8440 {
8441 /* Each VxWorks GOT entry needs an explicit relocation. */
8442 unsigned int count;
8443
861fb55a 8444 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
33bb52fb
RS
8445 if (count)
8446 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8447 }
8448 }
8449 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 8450 {
a8028dd0 8451 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
8452 return FALSE;
8453 }
8454 else
8455 {
33bb52fb
RS
8456 struct mips_elf_count_tls_arg arg;
8457
8458 /* Set up TLS entries. */
0f20cc35
DJ
8459 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8460 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
33bb52fb
RS
8461
8462 /* Allocate room for the TLS relocations. */
8463 arg.info = info;
8464 arg.needed = 0;
8465 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8466 elf_link_hash_traverse (elf_hash_table (info),
8467 mips_elf_count_global_tls_relocs,
8468 &arg);
8469 if (arg.needed)
8470 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
0f20cc35 8471 }
b49e97c9 8472
b34976b6 8473 return TRUE;
b49e97c9
TS
8474}
8475
33bb52fb
RS
8476/* Estimate the size of the .MIPS.stubs section. */
8477
8478static void
8479mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8480{
8481 struct mips_elf_link_hash_table *htab;
8482 bfd_size_type dynsymcount;
8483
8484 htab = mips_elf_hash_table (info);
8485 if (htab->lazy_stub_count == 0)
8486 return;
8487
8488 /* IRIX rld assumes that a function stub isn't at the end of the .text
8489 section, so add a dummy entry to the end. */
8490 htab->lazy_stub_count++;
8491
8492 /* Get a worst-case estimate of the number of dynamic symbols needed.
8493 At this point, dynsymcount does not account for section symbols
8494 and count_section_dynsyms may overestimate the number that will
8495 be needed. */
8496 dynsymcount = (elf_hash_table (info)->dynsymcount
8497 + count_section_dynsyms (output_bfd, info));
8498
8499 /* Determine the size of one stub entry. */
8500 htab->function_stub_size = (dynsymcount > 0x10000
8501 ? MIPS_FUNCTION_STUB_BIG_SIZE
8502 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8503
8504 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8505}
8506
8507/* A mips_elf_link_hash_traverse callback for which DATA points to the
8508 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8509 allocate an entry in the stubs section. */
8510
8511static bfd_boolean
8512mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8513{
8514 struct mips_elf_link_hash_table *htab;
8515
8516 htab = (struct mips_elf_link_hash_table *) data;
8517 if (h->needs_lazy_stub)
8518 {
8519 h->root.root.u.def.section = htab->sstubs;
8520 h->root.root.u.def.value = htab->sstubs->size;
8521 h->root.plt.offset = htab->sstubs->size;
8522 htab->sstubs->size += htab->function_stub_size;
8523 }
8524 return TRUE;
8525}
8526
8527/* Allocate offsets in the stubs section to each symbol that needs one.
8528 Set the final size of the .MIPS.stub section. */
8529
8530static void
8531mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8532{
8533 struct mips_elf_link_hash_table *htab;
8534
8535 htab = mips_elf_hash_table (info);
8536 if (htab->lazy_stub_count == 0)
8537 return;
8538
8539 htab->sstubs->size = 0;
8540 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8541 mips_elf_allocate_lazy_stub, htab);
8542 htab->sstubs->size += htab->function_stub_size;
8543 BFD_ASSERT (htab->sstubs->size
8544 == htab->lazy_stub_count * htab->function_stub_size);
8545}
8546
b49e97c9
TS
8547/* Set the sizes of the dynamic sections. */
8548
b34976b6 8549bfd_boolean
9719ad41
RS
8550_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8551 struct bfd_link_info *info)
b49e97c9
TS
8552{
8553 bfd *dynobj;
861fb55a 8554 asection *s, *sreldyn;
b34976b6 8555 bfd_boolean reltext;
0a44bf69 8556 struct mips_elf_link_hash_table *htab;
b49e97c9 8557
0a44bf69 8558 htab = mips_elf_hash_table (info);
b49e97c9
TS
8559 dynobj = elf_hash_table (info)->dynobj;
8560 BFD_ASSERT (dynobj != NULL);
8561
8562 if (elf_hash_table (info)->dynamic_sections_created)
8563 {
8564 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 8565 if (info->executable)
b49e97c9
TS
8566 {
8567 s = bfd_get_section_by_name (dynobj, ".interp");
8568 BFD_ASSERT (s != NULL);
eea6121a 8569 s->size
b49e97c9
TS
8570 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8571 s->contents
8572 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8573 }
861fb55a
DJ
8574
8575 /* Create a symbol for the PLT, if we know that we are using it. */
8576 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8577 {
8578 struct elf_link_hash_entry *h;
8579
8580 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8581
8582 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8583 "_PROCEDURE_LINKAGE_TABLE_");
8584 htab->root.hplt = h;
8585 if (h == NULL)
8586 return FALSE;
8587 h->type = STT_FUNC;
8588 }
8589 }
4e41d0d7 8590
9a59ad6b
DJ
8591 /* Allocate space for global sym dynamic relocs. */
8592 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8593
33bb52fb
RS
8594 mips_elf_estimate_stub_size (output_bfd, info);
8595
8596 if (!mips_elf_lay_out_got (output_bfd, info))
8597 return FALSE;
8598
8599 mips_elf_lay_out_lazy_stubs (info);
8600
b49e97c9
TS
8601 /* The check_relocs and adjust_dynamic_symbol entry points have
8602 determined the sizes of the various dynamic sections. Allocate
8603 memory for them. */
b34976b6 8604 reltext = FALSE;
b49e97c9
TS
8605 for (s = dynobj->sections; s != NULL; s = s->next)
8606 {
8607 const char *name;
b49e97c9
TS
8608
8609 /* It's OK to base decisions on the section name, because none
8610 of the dynobj section names depend upon the input files. */
8611 name = bfd_get_section_name (dynobj, s);
8612
8613 if ((s->flags & SEC_LINKER_CREATED) == 0)
8614 continue;
8615
0112cd26 8616 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 8617 {
c456f082 8618 if (s->size != 0)
b49e97c9
TS
8619 {
8620 const char *outname;
8621 asection *target;
8622
8623 /* If this relocation section applies to a read only
8624 section, then we probably need a DT_TEXTREL entry.
0a44bf69 8625 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
8626 assert a DT_TEXTREL entry rather than testing whether
8627 there exists a relocation to a read only section or
8628 not. */
8629 outname = bfd_get_section_name (output_bfd,
8630 s->output_section);
8631 target = bfd_get_section_by_name (output_bfd, outname + 4);
8632 if ((target != NULL
8633 && (target->flags & SEC_READONLY) != 0
8634 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 8635 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 8636 reltext = TRUE;
b49e97c9
TS
8637
8638 /* We use the reloc_count field as a counter if we need
8639 to copy relocs into the output file. */
0a44bf69 8640 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 8641 s->reloc_count = 0;
f4416af6
AO
8642
8643 /* If combreloc is enabled, elf_link_sort_relocs() will
8644 sort relocations, but in a different way than we do,
8645 and before we're done creating relocations. Also, it
8646 will move them around between input sections'
8647 relocation's contents, so our sorting would be
8648 broken, so don't let it run. */
8649 info->combreloc = 0;
b49e97c9
TS
8650 }
8651 }
b49e97c9
TS
8652 else if (! info->shared
8653 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 8654 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 8655 {
5108fc1b 8656 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 8657 rtld to contain a pointer to the _r_debug structure. */
eea6121a 8658 s->size += 4;
b49e97c9
TS
8659 }
8660 else if (SGI_COMPAT (output_bfd)
0112cd26 8661 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 8662 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
8663 else if (s == htab->splt)
8664 {
8665 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
8666 room for an extra nop to fill the delay slot. This is
8667 for CPUs without load interlocking. */
8668 if (! LOAD_INTERLOCKS_P (output_bfd)
8669 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
8670 s->size += 4;
8671 }
0112cd26 8672 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 8673 && s != htab->sgot
0a44bf69 8674 && s != htab->sgotplt
861fb55a
DJ
8675 && s != htab->sstubs
8676 && s != htab->sdynbss)
b49e97c9
TS
8677 {
8678 /* It's not one of our sections, so don't allocate space. */
8679 continue;
8680 }
8681
c456f082 8682 if (s->size == 0)
b49e97c9 8683 {
8423293d 8684 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
8685 continue;
8686 }
8687
c456f082
AM
8688 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8689 continue;
8690
b49e97c9 8691 /* Allocate memory for the section contents. */
eea6121a 8692 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 8693 if (s->contents == NULL)
b49e97c9
TS
8694 {
8695 bfd_set_error (bfd_error_no_memory);
b34976b6 8696 return FALSE;
b49e97c9
TS
8697 }
8698 }
8699
8700 if (elf_hash_table (info)->dynamic_sections_created)
8701 {
8702 /* Add some entries to the .dynamic section. We fill in the
8703 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8704 must add the entries now so that we get the correct size for
5750dcec 8705 the .dynamic section. */
af5978fb
RS
8706
8707 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec
DJ
8708 DT_MIPS_RLD_MAP entry. This must come first because glibc
8709 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8710 looks at the first one it sees. */
af5978fb
RS
8711 if (!info->shared
8712 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8713 return FALSE;
b49e97c9 8714
5750dcec
DJ
8715 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8716 used by the debugger. */
8717 if (info->executable
8718 && !SGI_COMPAT (output_bfd)
8719 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8720 return FALSE;
8721
0a44bf69 8722 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
8723 info->flags |= DF_TEXTREL;
8724
8725 if ((info->flags & DF_TEXTREL) != 0)
8726 {
8727 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 8728 return FALSE;
943284cc
DJ
8729
8730 /* Clear the DF_TEXTREL flag. It will be set again if we
8731 write out an actual text relocation; we may not, because
8732 at this point we do not know whether e.g. any .eh_frame
8733 absolute relocations have been converted to PC-relative. */
8734 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
8735 }
8736
8737 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 8738 return FALSE;
b49e97c9 8739
861fb55a 8740 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 8741 if (htab->is_vxworks)
b49e97c9 8742 {
0a44bf69
RS
8743 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8744 use any of the DT_MIPS_* tags. */
861fb55a 8745 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8746 {
8747 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8748 return FALSE;
b49e97c9 8749
0a44bf69
RS
8750 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8751 return FALSE;
b49e97c9 8752
0a44bf69
RS
8753 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8754 return FALSE;
8755 }
b49e97c9 8756 }
0a44bf69
RS
8757 else
8758 {
861fb55a 8759 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8760 {
8761 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8762 return FALSE;
b49e97c9 8763
0a44bf69
RS
8764 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8765 return FALSE;
b49e97c9 8766
0a44bf69
RS
8767 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8768 return FALSE;
8769 }
b49e97c9 8770
0a44bf69
RS
8771 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8772 return FALSE;
b49e97c9 8773
0a44bf69
RS
8774 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8775 return FALSE;
b49e97c9 8776
0a44bf69
RS
8777 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8778 return FALSE;
b49e97c9 8779
0a44bf69
RS
8780 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8781 return FALSE;
b49e97c9 8782
0a44bf69
RS
8783 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8784 return FALSE;
b49e97c9 8785
0a44bf69
RS
8786 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8787 return FALSE;
b49e97c9 8788
0a44bf69
RS
8789 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8790 return FALSE;
8791
8792 if (IRIX_COMPAT (dynobj) == ict_irix5
8793 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8794 return FALSE;
8795
8796 if (IRIX_COMPAT (dynobj) == ict_irix6
8797 && (bfd_get_section_by_name
8798 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8799 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8800 return FALSE;
8801 }
861fb55a
DJ
8802 if (htab->splt->size > 0)
8803 {
8804 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8805 return FALSE;
8806
8807 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8808 return FALSE;
8809
8810 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8811 return FALSE;
8812
8813 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8814 return FALSE;
8815 }
7a2b07ff
NS
8816 if (htab->is_vxworks
8817 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8818 return FALSE;
b49e97c9
TS
8819 }
8820
b34976b6 8821 return TRUE;
b49e97c9
TS
8822}
8823\f
81d43bff
RS
8824/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8825 Adjust its R_ADDEND field so that it is correct for the output file.
8826 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8827 and sections respectively; both use symbol indexes. */
8828
8829static void
8830mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8831 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8832 asection **local_sections, Elf_Internal_Rela *rel)
8833{
8834 unsigned int r_type, r_symndx;
8835 Elf_Internal_Sym *sym;
8836 asection *sec;
8837
8838 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8839 {
8840 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8841 if (r_type == R_MIPS16_GPREL
8842 || r_type == R_MIPS_GPREL16
8843 || r_type == R_MIPS_GPREL32
8844 || r_type == R_MIPS_LITERAL)
8845 {
8846 rel->r_addend += _bfd_get_gp_value (input_bfd);
8847 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8848 }
8849
8850 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8851 sym = local_syms + r_symndx;
8852
8853 /* Adjust REL's addend to account for section merging. */
8854 if (!info->relocatable)
8855 {
8856 sec = local_sections[r_symndx];
8857 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8858 }
8859
8860 /* This would normally be done by the rela_normal code in elflink.c. */
8861 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8862 rel->r_addend += local_sections[r_symndx]->output_offset;
8863 }
8864}
8865
b49e97c9
TS
8866/* Relocate a MIPS ELF section. */
8867
b34976b6 8868bfd_boolean
9719ad41
RS
8869_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8870 bfd *input_bfd, asection *input_section,
8871 bfd_byte *contents, Elf_Internal_Rela *relocs,
8872 Elf_Internal_Sym *local_syms,
8873 asection **local_sections)
b49e97c9
TS
8874{
8875 Elf_Internal_Rela *rel;
8876 const Elf_Internal_Rela *relend;
8877 bfd_vma addend = 0;
b34976b6 8878 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 8879 const struct elf_backend_data *bed;
b49e97c9
TS
8880
8881 bed = get_elf_backend_data (output_bfd);
8882 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8883 for (rel = relocs; rel < relend; ++rel)
8884 {
8885 const char *name;
c9adbffe 8886 bfd_vma value = 0;
b49e97c9 8887 reloc_howto_type *howto;
b34976b6
AM
8888 bfd_boolean require_jalx;
8889 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 8890 REL relocation. */
b34976b6 8891 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 8892 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 8893 const char *msg;
ab96bf03
AM
8894 unsigned long r_symndx;
8895 asection *sec;
749b8d9d
L
8896 Elf_Internal_Shdr *symtab_hdr;
8897 struct elf_link_hash_entry *h;
b49e97c9
TS
8898
8899 /* Find the relocation howto for this relocation. */
ab96bf03
AM
8900 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8901 NEWABI_P (input_bfd)
8902 && (MIPS_RELOC_RELA_P
8903 (input_bfd, input_section,
8904 rel - relocs)));
8905
8906 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 8907 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
ab96bf03 8908 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
749b8d9d
L
8909 {
8910 sec = local_sections[r_symndx];
8911 h = NULL;
8912 }
ab96bf03
AM
8913 else
8914 {
ab96bf03 8915 unsigned long extsymoff;
ab96bf03 8916
ab96bf03
AM
8917 extsymoff = 0;
8918 if (!elf_bad_symtab (input_bfd))
8919 extsymoff = symtab_hdr->sh_info;
8920 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8921 while (h->root.type == bfd_link_hash_indirect
8922 || h->root.type == bfd_link_hash_warning)
8923 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8924
8925 sec = NULL;
8926 if (h->root.type == bfd_link_hash_defined
8927 || h->root.type == bfd_link_hash_defweak)
8928 sec = h->root.u.def.section;
8929 }
8930
8931 if (sec != NULL && elf_discarded_section (sec))
8932 {
8933 /* For relocs against symbols from removed linkonce sections,
8934 or sections discarded by a linker script, we just want the
8935 section contents zeroed. Avoid any special processing. */
8936 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8937 rel->r_info = 0;
8938 rel->r_addend = 0;
8939 continue;
8940 }
8941
4a14403c 8942 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
8943 {
8944 /* Some 32-bit code uses R_MIPS_64. In particular, people use
8945 64-bit code, but make sure all their addresses are in the
8946 lowermost or uppermost 32-bit section of the 64-bit address
8947 space. Thus, when they use an R_MIPS_64 they mean what is
8948 usually meant by R_MIPS_32, with the exception that the
8949 stored value is sign-extended to 64 bits. */
b34976b6 8950 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
8951
8952 /* On big-endian systems, we need to lie about the position
8953 of the reloc. */
8954 if (bfd_big_endian (input_bfd))
8955 rel->r_offset += 4;
8956 }
b49e97c9
TS
8957
8958 if (!use_saved_addend_p)
8959 {
b49e97c9
TS
8960 /* If these relocations were originally of the REL variety,
8961 we must pull the addend out of the field that will be
8962 relocated. Otherwise, we simply use the contents of the
c224138d
RS
8963 RELA relocation. */
8964 if (mips_elf_rel_relocation_p (input_bfd, input_section,
8965 relocs, rel))
b49e97c9 8966 {
b34976b6 8967 rela_relocation_p = FALSE;
c224138d
RS
8968 addend = mips_elf_read_rel_addend (input_bfd, rel,
8969 howto, contents);
738e5348
RS
8970 if (hi16_reloc_p (r_type)
8971 || (got16_reloc_p (r_type)
b49e97c9 8972 && mips_elf_local_relocation_p (input_bfd, rel,
b34976b6 8973 local_sections, FALSE)))
b49e97c9 8974 {
c224138d
RS
8975 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8976 contents, &addend))
749b8d9d
L
8977 {
8978 const char *name;
8979
8980 if (h)
8981 name = h->root.root.string;
8982 else
8983 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
8984 local_syms + r_symndx,
8985 sec);
8986 (*_bfd_error_handler)
8987 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
8988 input_bfd, input_section, name, howto->name,
8989 rel->r_offset);
749b8d9d 8990 }
b49e97c9 8991 }
30ac9238
RS
8992 else
8993 addend <<= howto->rightshift;
b49e97c9
TS
8994 }
8995 else
8996 addend = rel->r_addend;
81d43bff
RS
8997 mips_elf_adjust_addend (output_bfd, info, input_bfd,
8998 local_syms, local_sections, rel);
b49e97c9
TS
8999 }
9000
1049f94e 9001 if (info->relocatable)
b49e97c9 9002 {
4a14403c 9003 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9004 && bfd_big_endian (input_bfd))
9005 rel->r_offset -= 4;
9006
81d43bff 9007 if (!rela_relocation_p && rel->r_addend)
5a659663 9008 {
81d43bff 9009 addend += rel->r_addend;
738e5348 9010 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9011 addend = mips_elf_high (addend);
9012 else if (r_type == R_MIPS_HIGHER)
9013 addend = mips_elf_higher (addend);
9014 else if (r_type == R_MIPS_HIGHEST)
9015 addend = mips_elf_highest (addend);
30ac9238
RS
9016 else
9017 addend >>= howto->rightshift;
b49e97c9 9018
30ac9238
RS
9019 /* We use the source mask, rather than the destination
9020 mask because the place to which we are writing will be
9021 source of the addend in the final link. */
b49e97c9
TS
9022 addend &= howto->src_mask;
9023
5a659663 9024 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9025 /* See the comment above about using R_MIPS_64 in the 32-bit
9026 ABI. Here, we need to update the addend. It would be
9027 possible to get away with just using the R_MIPS_32 reloc
9028 but for endianness. */
9029 {
9030 bfd_vma sign_bits;
9031 bfd_vma low_bits;
9032 bfd_vma high_bits;
9033
9034 if (addend & ((bfd_vma) 1 << 31))
9035#ifdef BFD64
9036 sign_bits = ((bfd_vma) 1 << 32) - 1;
9037#else
9038 sign_bits = -1;
9039#endif
9040 else
9041 sign_bits = 0;
9042
9043 /* If we don't know that we have a 64-bit type,
9044 do two separate stores. */
9045 if (bfd_big_endian (input_bfd))
9046 {
9047 /* Store the sign-bits (which are most significant)
9048 first. */
9049 low_bits = sign_bits;
9050 high_bits = addend;
9051 }
9052 else
9053 {
9054 low_bits = addend;
9055 high_bits = sign_bits;
9056 }
9057 bfd_put_32 (input_bfd, low_bits,
9058 contents + rel->r_offset);
9059 bfd_put_32 (input_bfd, high_bits,
9060 contents + rel->r_offset + 4);
9061 continue;
9062 }
9063
9064 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9065 input_bfd, input_section,
b34976b6
AM
9066 contents, FALSE))
9067 return FALSE;
b49e97c9
TS
9068 }
9069
9070 /* Go on to the next relocation. */
9071 continue;
9072 }
9073
9074 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9075 relocations for the same offset. In that case we are
9076 supposed to treat the output of each relocation as the addend
9077 for the next. */
9078 if (rel + 1 < relend
9079 && rel->r_offset == rel[1].r_offset
9080 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9081 use_saved_addend_p = TRUE;
b49e97c9 9082 else
b34976b6 9083 use_saved_addend_p = FALSE;
b49e97c9
TS
9084
9085 /* Figure out what value we are supposed to relocate. */
9086 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9087 input_section, info, rel,
9088 addend, howto, local_syms,
9089 local_sections, &value,
bce03d3d
AO
9090 &name, &require_jalx,
9091 use_saved_addend_p))
b49e97c9
TS
9092 {
9093 case bfd_reloc_continue:
9094 /* There's nothing to do. */
9095 continue;
9096
9097 case bfd_reloc_undefined:
9098 /* mips_elf_calculate_relocation already called the
9099 undefined_symbol callback. There's no real point in
9100 trying to perform the relocation at this point, so we
9101 just skip ahead to the next relocation. */
9102 continue;
9103
9104 case bfd_reloc_notsupported:
9105 msg = _("internal error: unsupported relocation error");
9106 info->callbacks->warning
9107 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9108 return FALSE;
b49e97c9
TS
9109
9110 case bfd_reloc_overflow:
9111 if (use_saved_addend_p)
9112 /* Ignore overflow until we reach the last relocation for
9113 a given location. */
9114 ;
9115 else
9116 {
0e53d9da
AN
9117 struct mips_elf_link_hash_table *htab;
9118
9119 htab = mips_elf_hash_table (info);
b49e97c9 9120 BFD_ASSERT (name != NULL);
0e53d9da
AN
9121 if (!htab->small_data_overflow_reported
9122 && (howto->type == R_MIPS_GPREL16
9123 || howto->type == R_MIPS_LITERAL))
9124 {
9125 const char *msg =
9126 _("small-data section exceeds 64KB;"
9127 " lower small-data size limit (see option -G)");
9128
9129 htab->small_data_overflow_reported = TRUE;
9130 (*info->callbacks->einfo) ("%P: %s\n", msg);
9131 }
b49e97c9 9132 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 9133 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 9134 input_bfd, input_section, rel->r_offset)))
b34976b6 9135 return FALSE;
b49e97c9
TS
9136 }
9137 break;
9138
9139 case bfd_reloc_ok:
9140 break;
9141
9142 default:
9143 abort ();
9144 break;
9145 }
9146
9147 /* If we've got another relocation for the address, keep going
9148 until we reach the last one. */
9149 if (use_saved_addend_p)
9150 {
9151 addend = value;
9152 continue;
9153 }
9154
4a14403c 9155 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9156 /* See the comment above about using R_MIPS_64 in the 32-bit
9157 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9158 that calculated the right value. Now, however, we
9159 sign-extend the 32-bit result to 64-bits, and store it as a
9160 64-bit value. We are especially generous here in that we
9161 go to extreme lengths to support this usage on systems with
9162 only a 32-bit VMA. */
9163 {
9164 bfd_vma sign_bits;
9165 bfd_vma low_bits;
9166 bfd_vma high_bits;
9167
9168 if (value & ((bfd_vma) 1 << 31))
9169#ifdef BFD64
9170 sign_bits = ((bfd_vma) 1 << 32) - 1;
9171#else
9172 sign_bits = -1;
9173#endif
9174 else
9175 sign_bits = 0;
9176
9177 /* If we don't know that we have a 64-bit type,
9178 do two separate stores. */
9179 if (bfd_big_endian (input_bfd))
9180 {
9181 /* Undo what we did above. */
9182 rel->r_offset -= 4;
9183 /* Store the sign-bits (which are most significant)
9184 first. */
9185 low_bits = sign_bits;
9186 high_bits = value;
9187 }
9188 else
9189 {
9190 low_bits = value;
9191 high_bits = sign_bits;
9192 }
9193 bfd_put_32 (input_bfd, low_bits,
9194 contents + rel->r_offset);
9195 bfd_put_32 (input_bfd, high_bits,
9196 contents + rel->r_offset + 4);
9197 continue;
9198 }
9199
9200 /* Actually perform the relocation. */
9201 if (! mips_elf_perform_relocation (info, howto, rel, value,
9202 input_bfd, input_section,
9203 contents, require_jalx))
b34976b6 9204 return FALSE;
b49e97c9
TS
9205 }
9206
b34976b6 9207 return TRUE;
b49e97c9
TS
9208}
9209\f
861fb55a
DJ
9210/* A function that iterates over each entry in la25_stubs and fills
9211 in the code for each one. DATA points to a mips_htab_traverse_info. */
9212
9213static int
9214mips_elf_create_la25_stub (void **slot, void *data)
9215{
9216 struct mips_htab_traverse_info *hti;
9217 struct mips_elf_link_hash_table *htab;
9218 struct mips_elf_la25_stub *stub;
9219 asection *s;
9220 bfd_byte *loc;
9221 bfd_vma offset, target, target_high, target_low;
9222
9223 stub = (struct mips_elf_la25_stub *) *slot;
9224 hti = (struct mips_htab_traverse_info *) data;
9225 htab = mips_elf_hash_table (hti->info);
9226
9227 /* Create the section contents, if we haven't already. */
9228 s = stub->stub_section;
9229 loc = s->contents;
9230 if (loc == NULL)
9231 {
9232 loc = bfd_malloc (s->size);
9233 if (loc == NULL)
9234 {
9235 hti->error = TRUE;
9236 return FALSE;
9237 }
9238 s->contents = loc;
9239 }
9240
9241 /* Work out where in the section this stub should go. */
9242 offset = stub->offset;
9243
9244 /* Work out the target address. */
9245 target = (stub->h->root.root.u.def.section->output_section->vma
9246 + stub->h->root.root.u.def.section->output_offset
9247 + stub->h->root.root.u.def.value);
9248 target_high = ((target + 0x8000) >> 16) & 0xffff;
9249 target_low = (target & 0xffff);
9250
9251 if (stub->stub_section != htab->strampoline)
9252 {
9253 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9254 of the section and write the two instructions at the end. */
9255 memset (loc, 0, offset);
9256 loc += offset;
9257 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9258 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9259 }
9260 else
9261 {
9262 /* This is trampoline. */
9263 loc += offset;
9264 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9265 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9266 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9267 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9268 }
9269 return TRUE;
9270}
9271
b49e97c9
TS
9272/* If NAME is one of the special IRIX6 symbols defined by the linker,
9273 adjust it appropriately now. */
9274
9275static void
9719ad41
RS
9276mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9277 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
9278{
9279 /* The linker script takes care of providing names and values for
9280 these, but we must place them into the right sections. */
9281 static const char* const text_section_symbols[] = {
9282 "_ftext",
9283 "_etext",
9284 "__dso_displacement",
9285 "__elf_header",
9286 "__program_header_table",
9287 NULL
9288 };
9289
9290 static const char* const data_section_symbols[] = {
9291 "_fdata",
9292 "_edata",
9293 "_end",
9294 "_fbss",
9295 NULL
9296 };
9297
9298 const char* const *p;
9299 int i;
9300
9301 for (i = 0; i < 2; ++i)
9302 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9303 *p;
9304 ++p)
9305 if (strcmp (*p, name) == 0)
9306 {
9307 /* All of these symbols are given type STT_SECTION by the
9308 IRIX6 linker. */
9309 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 9310 sym->st_other = STO_PROTECTED;
b49e97c9
TS
9311
9312 /* The IRIX linker puts these symbols in special sections. */
9313 if (i == 0)
9314 sym->st_shndx = SHN_MIPS_TEXT;
9315 else
9316 sym->st_shndx = SHN_MIPS_DATA;
9317
9318 break;
9319 }
9320}
9321
9322/* Finish up dynamic symbol handling. We set the contents of various
9323 dynamic sections here. */
9324
b34976b6 9325bfd_boolean
9719ad41
RS
9326_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9327 struct bfd_link_info *info,
9328 struct elf_link_hash_entry *h,
9329 Elf_Internal_Sym *sym)
b49e97c9
TS
9330{
9331 bfd *dynobj;
b49e97c9 9332 asection *sgot;
f4416af6 9333 struct mips_got_info *g, *gg;
b49e97c9 9334 const char *name;
3d6746ca 9335 int idx;
5108fc1b 9336 struct mips_elf_link_hash_table *htab;
738e5348 9337 struct mips_elf_link_hash_entry *hmips;
b49e97c9 9338
5108fc1b 9339 htab = mips_elf_hash_table (info);
b49e97c9 9340 dynobj = elf_hash_table (info)->dynobj;
738e5348 9341 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9342
861fb55a
DJ
9343 BFD_ASSERT (!htab->is_vxworks);
9344
9345 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9346 {
9347 /* We've decided to create a PLT entry for this symbol. */
9348 bfd_byte *loc;
9349 bfd_vma header_address, plt_index, got_address;
9350 bfd_vma got_address_high, got_address_low, load;
9351 const bfd_vma *plt_entry;
9352
9353 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9354 BFD_ASSERT (h->dynindx != -1);
9355 BFD_ASSERT (htab->splt != NULL);
9356 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9357 BFD_ASSERT (!h->def_regular);
9358
9359 /* Calculate the address of the PLT header. */
9360 header_address = (htab->splt->output_section->vma
9361 + htab->splt->output_offset);
9362
9363 /* Calculate the index of the entry. */
9364 plt_index = ((h->plt.offset - htab->plt_header_size)
9365 / htab->plt_entry_size);
9366
9367 /* Calculate the address of the .got.plt entry. */
9368 got_address = (htab->sgotplt->output_section->vma
9369 + htab->sgotplt->output_offset
9370 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9371 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9372 got_address_low = got_address & 0xffff;
9373
9374 /* Initially point the .got.plt entry at the PLT header. */
9375 loc = (htab->sgotplt->contents
9376 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9377 if (ABI_64_P (output_bfd))
9378 bfd_put_64 (output_bfd, header_address, loc);
9379 else
9380 bfd_put_32 (output_bfd, header_address, loc);
9381
9382 /* Find out where the .plt entry should go. */
9383 loc = htab->splt->contents + h->plt.offset;
9384
9385 /* Pick the load opcode. */
9386 load = MIPS_ELF_LOAD_WORD (output_bfd);
9387
9388 /* Fill in the PLT entry itself. */
9389 plt_entry = mips_exec_plt_entry;
9390 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9391 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
6d30f5b2
NC
9392
9393 if (! LOAD_INTERLOCKS_P (output_bfd))
9394 {
9395 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9396 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9397 }
9398 else
9399 {
9400 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9401 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9402 }
861fb55a
DJ
9403
9404 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9405 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9406 plt_index, h->dynindx,
9407 R_MIPS_JUMP_SLOT, got_address);
9408
9409 /* We distinguish between PLT entries and lazy-binding stubs by
9410 giving the former an st_other value of STO_MIPS_PLT. Set the
9411 flag and leave the value if there are any relocations in the
9412 binary where pointer equality matters. */
9413 sym->st_shndx = SHN_UNDEF;
9414 if (h->pointer_equality_needed)
9415 sym->st_other = STO_MIPS_PLT;
9416 else
9417 sym->st_value = 0;
9418 }
9419 else if (h->plt.offset != MINUS_ONE)
b49e97c9 9420 {
861fb55a 9421 /* We've decided to create a lazy-binding stub. */
5108fc1b 9422 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
b49e97c9
TS
9423
9424 /* This symbol has a stub. Set it up. */
9425
9426 BFD_ASSERT (h->dynindx != -1);
9427
5108fc1b
RS
9428 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9429 || (h->dynindx <= 0xffff));
3d6746ca
DD
9430
9431 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
9432 sign extension at runtime in the stub, resulting in a negative
9433 index value. */
9434 if (h->dynindx & ~0x7fffffff)
b34976b6 9435 return FALSE;
b49e97c9
TS
9436
9437 /* Fill the stub. */
3d6746ca
DD
9438 idx = 0;
9439 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9440 idx += 4;
9441 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9442 idx += 4;
5108fc1b 9443 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
3d6746ca 9444 {
5108fc1b 9445 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
3d6746ca
DD
9446 stub + idx);
9447 idx += 4;
9448 }
9449 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9450 idx += 4;
b49e97c9 9451
3d6746ca
DD
9452 /* If a large stub is not required and sign extension is not a
9453 problem, then use legacy code in the stub. */
5108fc1b
RS
9454 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9455 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9456 else if (h->dynindx & ~0x7fff)
3d6746ca
DD
9457 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9458 else
5108fc1b
RS
9459 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9460 stub + idx);
9461
4e41d0d7
RS
9462 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9463 memcpy (htab->sstubs->contents + h->plt.offset,
9464 stub, htab->function_stub_size);
b49e97c9
TS
9465
9466 /* Mark the symbol as undefined. plt.offset != -1 occurs
9467 only for the referenced symbol. */
9468 sym->st_shndx = SHN_UNDEF;
9469
9470 /* The run-time linker uses the st_value field of the symbol
9471 to reset the global offset table entry for this external
9472 to its stub address when unlinking a shared object. */
4e41d0d7
RS
9473 sym->st_value = (htab->sstubs->output_section->vma
9474 + htab->sstubs->output_offset
c5ae1840 9475 + h->plt.offset);
b49e97c9
TS
9476 }
9477
738e5348
RS
9478 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9479 refer to the stub, since only the stub uses the standard calling
9480 conventions. */
9481 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9482 {
9483 BFD_ASSERT (hmips->need_fn_stub);
9484 sym->st_value = (hmips->fn_stub->output_section->vma
9485 + hmips->fn_stub->output_offset);
9486 sym->st_size = hmips->fn_stub->size;
9487 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9488 }
9489
b49e97c9 9490 BFD_ASSERT (h->dynindx != -1
f5385ebf 9491 || h->forced_local);
b49e97c9 9492
23cc69b6 9493 sgot = htab->sgot;
a8028dd0 9494 g = htab->got_info;
b49e97c9
TS
9495 BFD_ASSERT (g != NULL);
9496
9497 /* Run through the global symbol table, creating GOT entries for all
9498 the symbols that need them. */
9499 if (g->global_gotsym != NULL
9500 && h->dynindx >= g->global_gotsym->dynindx)
9501 {
9502 bfd_vma offset;
9503 bfd_vma value;
9504
6eaa6adc 9505 value = sym->st_value;
738e5348
RS
9506 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9507 R_MIPS_GOT16, info);
b49e97c9
TS
9508 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9509 }
9510
0f20cc35 9511 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
f4416af6
AO
9512 {
9513 struct mips_got_entry e, *p;
0626d451 9514 bfd_vma entry;
f4416af6 9515 bfd_vma offset;
f4416af6
AO
9516
9517 gg = g;
9518
9519 e.abfd = output_bfd;
9520 e.symndx = -1;
738e5348 9521 e.d.h = hmips;
0f20cc35 9522 e.tls_type = 0;
143d77c5 9523
f4416af6
AO
9524 for (g = g->next; g->next != gg; g = g->next)
9525 {
9526 if (g->got_entries
9527 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9528 &e)))
9529 {
9530 offset = p->gotidx;
0626d451
RS
9531 if (info->shared
9532 || (elf_hash_table (info)->dynamic_sections_created
9533 && p->d.h != NULL
f5385ebf
AM
9534 && p->d.h->root.def_dynamic
9535 && !p->d.h->root.def_regular))
0626d451
RS
9536 {
9537 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9538 the various compatibility problems, it's easier to mock
9539 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9540 mips_elf_create_dynamic_relocation to calculate the
9541 appropriate addend. */
9542 Elf_Internal_Rela rel[3];
9543
9544 memset (rel, 0, sizeof (rel));
9545 if (ABI_64_P (output_bfd))
9546 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9547 else
9548 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9549 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9550
9551 entry = 0;
9552 if (! (mips_elf_create_dynamic_relocation
9553 (output_bfd, info, rel,
9554 e.d.h, NULL, sym->st_value, &entry, sgot)))
9555 return FALSE;
9556 }
9557 else
9558 entry = sym->st_value;
9559 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
9560 }
9561 }
9562 }
9563
b49e97c9
TS
9564 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9565 name = h->root.root.string;
9566 if (strcmp (name, "_DYNAMIC") == 0
22edb2f1 9567 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
9568 sym->st_shndx = SHN_ABS;
9569 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9570 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9571 {
9572 sym->st_shndx = SHN_ABS;
9573 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9574 sym->st_value = 1;
9575 }
4a14403c 9576 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9577 {
9578 sym->st_shndx = SHN_ABS;
9579 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9580 sym->st_value = elf_gp (output_bfd);
9581 }
9582 else if (SGI_COMPAT (output_bfd))
9583 {
9584 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9585 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9586 {
9587 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9588 sym->st_other = STO_PROTECTED;
9589 sym->st_value = 0;
9590 sym->st_shndx = SHN_MIPS_DATA;
9591 }
9592 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9593 {
9594 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9595 sym->st_other = STO_PROTECTED;
9596 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9597 sym->st_shndx = SHN_ABS;
9598 }
9599 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9600 {
9601 if (h->type == STT_FUNC)
9602 sym->st_shndx = SHN_MIPS_TEXT;
9603 else if (h->type == STT_OBJECT)
9604 sym->st_shndx = SHN_MIPS_DATA;
9605 }
9606 }
9607
861fb55a
DJ
9608 /* Emit a copy reloc, if needed. */
9609 if (h->needs_copy)
9610 {
9611 asection *s;
9612 bfd_vma symval;
9613
9614 BFD_ASSERT (h->dynindx != -1);
9615 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9616
9617 s = mips_elf_rel_dyn_section (info, FALSE);
9618 symval = (h->root.u.def.section->output_section->vma
9619 + h->root.u.def.section->output_offset
9620 + h->root.u.def.value);
9621 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9622 h->dynindx, R_MIPS_COPY, symval);
9623 }
9624
b49e97c9
TS
9625 /* Handle the IRIX6-specific symbols. */
9626 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9627 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9628
9629 if (! info->shared)
9630 {
9631 if (! mips_elf_hash_table (info)->use_rld_obj_head
9632 && (strcmp (name, "__rld_map") == 0
9633 || strcmp (name, "__RLD_MAP") == 0))
9634 {
9635 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9636 BFD_ASSERT (s != NULL);
9637 sym->st_value = s->output_section->vma + s->output_offset;
9719ad41 9638 bfd_put_32 (output_bfd, 0, s->contents);
b49e97c9
TS
9639 if (mips_elf_hash_table (info)->rld_value == 0)
9640 mips_elf_hash_table (info)->rld_value = sym->st_value;
9641 }
9642 else if (mips_elf_hash_table (info)->use_rld_obj_head
9643 && strcmp (name, "__rld_obj_head") == 0)
9644 {
9645 /* IRIX6 does not use a .rld_map section. */
9646 if (IRIX_COMPAT (output_bfd) == ict_irix5
9647 || IRIX_COMPAT (output_bfd) == ict_none)
9648 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9649 != NULL);
9650 mips_elf_hash_table (info)->rld_value = sym->st_value;
9651 }
9652 }
9653
738e5348
RS
9654 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9655 treat MIPS16 symbols like any other. */
30c09090 9656 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
9657 {
9658 BFD_ASSERT (sym->st_value & 1);
9659 sym->st_other -= STO_MIPS16;
9660 }
b49e97c9 9661
b34976b6 9662 return TRUE;
b49e97c9
TS
9663}
9664
0a44bf69
RS
9665/* Likewise, for VxWorks. */
9666
9667bfd_boolean
9668_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9669 struct bfd_link_info *info,
9670 struct elf_link_hash_entry *h,
9671 Elf_Internal_Sym *sym)
9672{
9673 bfd *dynobj;
9674 asection *sgot;
9675 struct mips_got_info *g;
9676 struct mips_elf_link_hash_table *htab;
9677
9678 htab = mips_elf_hash_table (info);
9679 dynobj = elf_hash_table (info)->dynobj;
9680
9681 if (h->plt.offset != (bfd_vma) -1)
9682 {
6d79d2ed 9683 bfd_byte *loc;
0a44bf69
RS
9684 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9685 Elf_Internal_Rela rel;
9686 static const bfd_vma *plt_entry;
9687
9688 BFD_ASSERT (h->dynindx != -1);
9689 BFD_ASSERT (htab->splt != NULL);
9690 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9691
9692 /* Calculate the address of the .plt entry. */
9693 plt_address = (htab->splt->output_section->vma
9694 + htab->splt->output_offset
9695 + h->plt.offset);
9696
9697 /* Calculate the index of the entry. */
9698 plt_index = ((h->plt.offset - htab->plt_header_size)
9699 / htab->plt_entry_size);
9700
9701 /* Calculate the address of the .got.plt entry. */
9702 got_address = (htab->sgotplt->output_section->vma
9703 + htab->sgotplt->output_offset
9704 + plt_index * 4);
9705
9706 /* Calculate the offset of the .got.plt entry from
9707 _GLOBAL_OFFSET_TABLE_. */
9708 got_offset = mips_elf_gotplt_index (info, h);
9709
9710 /* Calculate the offset for the branch at the start of the PLT
9711 entry. The branch jumps to the beginning of .plt. */
9712 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9713
9714 /* Fill in the initial value of the .got.plt entry. */
9715 bfd_put_32 (output_bfd, plt_address,
9716 htab->sgotplt->contents + plt_index * 4);
9717
9718 /* Find out where the .plt entry should go. */
9719 loc = htab->splt->contents + h->plt.offset;
9720
9721 if (info->shared)
9722 {
9723 plt_entry = mips_vxworks_shared_plt_entry;
9724 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9725 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9726 }
9727 else
9728 {
9729 bfd_vma got_address_high, got_address_low;
9730
9731 plt_entry = mips_vxworks_exec_plt_entry;
9732 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9733 got_address_low = got_address & 0xffff;
9734
9735 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9736 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9737 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9738 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9739 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9740 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9741 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9742 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9743
9744 loc = (htab->srelplt2->contents
9745 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9746
9747 /* Emit a relocation for the .got.plt entry. */
9748 rel.r_offset = got_address;
9749 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9750 rel.r_addend = h->plt.offset;
9751 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9752
9753 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9754 loc += sizeof (Elf32_External_Rela);
9755 rel.r_offset = plt_address + 8;
9756 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9757 rel.r_addend = got_offset;
9758 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9759
9760 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9761 loc += sizeof (Elf32_External_Rela);
9762 rel.r_offset += 4;
9763 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9764 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9765 }
9766
9767 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9768 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9769 rel.r_offset = got_address;
9770 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9771 rel.r_addend = 0;
9772 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9773
9774 if (!h->def_regular)
9775 sym->st_shndx = SHN_UNDEF;
9776 }
9777
9778 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9779
23cc69b6 9780 sgot = htab->sgot;
a8028dd0 9781 g = htab->got_info;
0a44bf69
RS
9782 BFD_ASSERT (g != NULL);
9783
9784 /* See if this symbol has an entry in the GOT. */
9785 if (g->global_gotsym != NULL
9786 && h->dynindx >= g->global_gotsym->dynindx)
9787 {
9788 bfd_vma offset;
9789 Elf_Internal_Rela outrel;
9790 bfd_byte *loc;
9791 asection *s;
9792
9793 /* Install the symbol value in the GOT. */
9794 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9795 R_MIPS_GOT16, info);
9796 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9797
9798 /* Add a dynamic relocation for it. */
9799 s = mips_elf_rel_dyn_section (info, FALSE);
9800 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9801 outrel.r_offset = (sgot->output_section->vma
9802 + sgot->output_offset
9803 + offset);
9804 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9805 outrel.r_addend = 0;
9806 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9807 }
9808
9809 /* Emit a copy reloc, if needed. */
9810 if (h->needs_copy)
9811 {
9812 Elf_Internal_Rela rel;
9813
9814 BFD_ASSERT (h->dynindx != -1);
9815
9816 rel.r_offset = (h->root.u.def.section->output_section->vma
9817 + h->root.u.def.section->output_offset
9818 + h->root.u.def.value);
9819 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9820 rel.r_addend = 0;
9821 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9822 htab->srelbss->contents
9823 + (htab->srelbss->reloc_count
9824 * sizeof (Elf32_External_Rela)));
9825 ++htab->srelbss->reloc_count;
9826 }
9827
9828 /* If this is a mips16 symbol, force the value to be even. */
30c09090 9829 if (ELF_ST_IS_MIPS16 (sym->st_other))
0a44bf69
RS
9830 sym->st_value &= ~1;
9831
9832 return TRUE;
9833}
9834
861fb55a
DJ
9835/* Write out a plt0 entry to the beginning of .plt. */
9836
9837static void
9838mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9839{
9840 bfd_byte *loc;
9841 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9842 static const bfd_vma *plt_entry;
9843 struct mips_elf_link_hash_table *htab;
9844
9845 htab = mips_elf_hash_table (info);
9846 if (ABI_64_P (output_bfd))
9847 plt_entry = mips_n64_exec_plt0_entry;
9848 else if (ABI_N32_P (output_bfd))
9849 plt_entry = mips_n32_exec_plt0_entry;
9850 else
9851 plt_entry = mips_o32_exec_plt0_entry;
9852
9853 /* Calculate the value of .got.plt. */
9854 gotplt_value = (htab->sgotplt->output_section->vma
9855 + htab->sgotplt->output_offset);
9856 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9857 gotplt_value_low = gotplt_value & 0xffff;
9858
9859 /* The PLT sequence is not safe for N64 if .got.plt's address can
9860 not be loaded in two instructions. */
9861 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9862 || ~(gotplt_value | 0x7fffffff) == 0);
9863
9864 /* Install the PLT header. */
9865 loc = htab->splt->contents;
9866 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9867 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9868 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9869 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9870 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9871 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9872 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9873 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9874}
9875
0a44bf69
RS
9876/* Install the PLT header for a VxWorks executable and finalize the
9877 contents of .rela.plt.unloaded. */
9878
9879static void
9880mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9881{
9882 Elf_Internal_Rela rela;
9883 bfd_byte *loc;
9884 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9885 static const bfd_vma *plt_entry;
9886 struct mips_elf_link_hash_table *htab;
9887
9888 htab = mips_elf_hash_table (info);
9889 plt_entry = mips_vxworks_exec_plt0_entry;
9890
9891 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9892 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9893 + htab->root.hgot->root.u.def.section->output_offset
9894 + htab->root.hgot->root.u.def.value);
9895
9896 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9897 got_value_low = got_value & 0xffff;
9898
9899 /* Calculate the address of the PLT header. */
9900 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9901
9902 /* Install the PLT header. */
9903 loc = htab->splt->contents;
9904 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9905 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9906 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9907 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9908 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9909 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9910
9911 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9912 loc = htab->srelplt2->contents;
9913 rela.r_offset = plt_address;
9914 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9915 rela.r_addend = 0;
9916 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9917 loc += sizeof (Elf32_External_Rela);
9918
9919 /* Output the relocation for the following addiu of
9920 %lo(_GLOBAL_OFFSET_TABLE_). */
9921 rela.r_offset += 4;
9922 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9923 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9924 loc += sizeof (Elf32_External_Rela);
9925
9926 /* Fix up the remaining relocations. They may have the wrong
9927 symbol index for _G_O_T_ or _P_L_T_ depending on the order
9928 in which symbols were output. */
9929 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9930 {
9931 Elf_Internal_Rela rel;
9932
9933 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9934 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9935 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9936 loc += sizeof (Elf32_External_Rela);
9937
9938 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9939 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9940 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9941 loc += sizeof (Elf32_External_Rela);
9942
9943 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9944 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9945 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9946 loc += sizeof (Elf32_External_Rela);
9947 }
9948}
9949
9950/* Install the PLT header for a VxWorks shared library. */
9951
9952static void
9953mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9954{
9955 unsigned int i;
9956 struct mips_elf_link_hash_table *htab;
9957
9958 htab = mips_elf_hash_table (info);
9959
9960 /* We just need to copy the entry byte-by-byte. */
9961 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9962 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9963 htab->splt->contents + i * 4);
9964}
9965
b49e97c9
TS
9966/* Finish up the dynamic sections. */
9967
b34976b6 9968bfd_boolean
9719ad41
RS
9969_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9970 struct bfd_link_info *info)
b49e97c9
TS
9971{
9972 bfd *dynobj;
9973 asection *sdyn;
9974 asection *sgot;
f4416af6 9975 struct mips_got_info *gg, *g;
0a44bf69 9976 struct mips_elf_link_hash_table *htab;
b49e97c9 9977
0a44bf69 9978 htab = mips_elf_hash_table (info);
b49e97c9
TS
9979 dynobj = elf_hash_table (info)->dynobj;
9980
9981 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9982
23cc69b6
RS
9983 sgot = htab->sgot;
9984 gg = htab->got_info;
b49e97c9
TS
9985
9986 if (elf_hash_table (info)->dynamic_sections_created)
9987 {
9988 bfd_byte *b;
943284cc 9989 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
9990
9991 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
9992 BFD_ASSERT (gg != NULL);
9993
9994 g = mips_elf_got_for_ibfd (gg, output_bfd);
b49e97c9
TS
9995 BFD_ASSERT (g != NULL);
9996
9997 for (b = sdyn->contents;
eea6121a 9998 b < sdyn->contents + sdyn->size;
b49e97c9
TS
9999 b += MIPS_ELF_DYN_SIZE (dynobj))
10000 {
10001 Elf_Internal_Dyn dyn;
10002 const char *name;
10003 size_t elemsize;
10004 asection *s;
b34976b6 10005 bfd_boolean swap_out_p;
b49e97c9
TS
10006
10007 /* Read in the current dynamic entry. */
10008 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10009
10010 /* Assume that we're going to modify it and write it out. */
b34976b6 10011 swap_out_p = TRUE;
b49e97c9
TS
10012
10013 switch (dyn.d_tag)
10014 {
10015 case DT_RELENT:
b49e97c9
TS
10016 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10017 break;
10018
0a44bf69
RS
10019 case DT_RELAENT:
10020 BFD_ASSERT (htab->is_vxworks);
10021 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10022 break;
10023
b49e97c9
TS
10024 case DT_STRSZ:
10025 /* Rewrite DT_STRSZ. */
10026 dyn.d_un.d_val =
10027 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10028 break;
10029
10030 case DT_PLTGOT:
861fb55a
DJ
10031 s = htab->sgot;
10032 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10033 break;
10034
10035 case DT_MIPS_PLTGOT:
10036 s = htab->sgotplt;
10037 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
10038 break;
10039
10040 case DT_MIPS_RLD_VERSION:
10041 dyn.d_un.d_val = 1; /* XXX */
10042 break;
10043
10044 case DT_MIPS_FLAGS:
10045 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10046 break;
10047
b49e97c9 10048 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
10049 {
10050 time_t t;
10051 time (&t);
10052 dyn.d_un.d_val = t;
10053 }
b49e97c9
TS
10054 break;
10055
10056 case DT_MIPS_ICHECKSUM:
10057 /* XXX FIXME: */
b34976b6 10058 swap_out_p = FALSE;
b49e97c9
TS
10059 break;
10060
10061 case DT_MIPS_IVERSION:
10062 /* XXX FIXME: */
b34976b6 10063 swap_out_p = FALSE;
b49e97c9
TS
10064 break;
10065
10066 case DT_MIPS_BASE_ADDRESS:
10067 s = output_bfd->sections;
10068 BFD_ASSERT (s != NULL);
10069 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10070 break;
10071
10072 case DT_MIPS_LOCAL_GOTNO:
10073 dyn.d_un.d_val = g->local_gotno;
10074 break;
10075
10076 case DT_MIPS_UNREFEXTNO:
10077 /* The index into the dynamic symbol table which is the
10078 entry of the first external symbol that is not
10079 referenced within the same object. */
10080 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10081 break;
10082
10083 case DT_MIPS_GOTSYM:
f4416af6 10084 if (gg->global_gotsym)
b49e97c9 10085 {
f4416af6 10086 dyn.d_un.d_val = gg->global_gotsym->dynindx;
b49e97c9
TS
10087 break;
10088 }
10089 /* In case if we don't have global got symbols we default
10090 to setting DT_MIPS_GOTSYM to the same value as
10091 DT_MIPS_SYMTABNO, so we just fall through. */
10092
10093 case DT_MIPS_SYMTABNO:
10094 name = ".dynsym";
10095 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10096 s = bfd_get_section_by_name (output_bfd, name);
10097 BFD_ASSERT (s != NULL);
10098
eea6121a 10099 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
10100 break;
10101
10102 case DT_MIPS_HIPAGENO:
861fb55a 10103 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
10104 break;
10105
10106 case DT_MIPS_RLD_MAP:
10107 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10108 break;
10109
10110 case DT_MIPS_OPTIONS:
10111 s = (bfd_get_section_by_name
10112 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10113 dyn.d_un.d_ptr = s->vma;
10114 break;
10115
0a44bf69
RS
10116 case DT_RELASZ:
10117 BFD_ASSERT (htab->is_vxworks);
10118 /* The count does not include the JUMP_SLOT relocations. */
10119 if (htab->srelplt)
10120 dyn.d_un.d_val -= htab->srelplt->size;
10121 break;
10122
10123 case DT_PLTREL:
861fb55a
DJ
10124 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10125 if (htab->is_vxworks)
10126 dyn.d_un.d_val = DT_RELA;
10127 else
10128 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
10129 break;
10130
10131 case DT_PLTRELSZ:
861fb55a 10132 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
10133 dyn.d_un.d_val = htab->srelplt->size;
10134 break;
10135
10136 case DT_JMPREL:
861fb55a
DJ
10137 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10138 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
10139 + htab->srelplt->output_offset);
10140 break;
10141
943284cc
DJ
10142 case DT_TEXTREL:
10143 /* If we didn't need any text relocations after all, delete
10144 the dynamic tag. */
10145 if (!(info->flags & DF_TEXTREL))
10146 {
10147 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10148 swap_out_p = FALSE;
10149 }
10150 break;
10151
10152 case DT_FLAGS:
10153 /* If we didn't need any text relocations after all, clear
10154 DF_TEXTREL from DT_FLAGS. */
10155 if (!(info->flags & DF_TEXTREL))
10156 dyn.d_un.d_val &= ~DF_TEXTREL;
10157 else
10158 swap_out_p = FALSE;
10159 break;
10160
b49e97c9 10161 default:
b34976b6 10162 swap_out_p = FALSE;
7a2b07ff
NS
10163 if (htab->is_vxworks
10164 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10165 swap_out_p = TRUE;
b49e97c9
TS
10166 break;
10167 }
10168
943284cc 10169 if (swap_out_p || dyn_skipped)
b49e97c9 10170 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
10171 (dynobj, &dyn, b - dyn_skipped);
10172
10173 if (dyn_to_skip)
10174 {
10175 dyn_skipped += dyn_to_skip;
10176 dyn_to_skip = 0;
10177 }
b49e97c9 10178 }
943284cc
DJ
10179
10180 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10181 if (dyn_skipped > 0)
10182 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
10183 }
10184
b55fd4d4
DJ
10185 if (sgot != NULL && sgot->size > 0
10186 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 10187 {
0a44bf69
RS
10188 if (htab->is_vxworks)
10189 {
10190 /* The first entry of the global offset table points to the
10191 ".dynamic" section. The second is initialized by the
10192 loader and contains the shared library identifier.
10193 The third is also initialized by the loader and points
10194 to the lazy resolution stub. */
10195 MIPS_ELF_PUT_WORD (output_bfd,
10196 sdyn->output_offset + sdyn->output_section->vma,
10197 sgot->contents);
10198 MIPS_ELF_PUT_WORD (output_bfd, 0,
10199 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10200 MIPS_ELF_PUT_WORD (output_bfd, 0,
10201 sgot->contents
10202 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10203 }
10204 else
10205 {
10206 /* The first entry of the global offset table will be filled at
10207 runtime. The second entry will be used by some runtime loaders.
10208 This isn't the case of IRIX rld. */
10209 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 10210 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
10211 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10212 }
b49e97c9 10213
54938e2a
TS
10214 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10215 = MIPS_ELF_GOT_SIZE (output_bfd);
10216 }
b49e97c9 10217
f4416af6
AO
10218 /* Generate dynamic relocations for the non-primary gots. */
10219 if (gg != NULL && gg->next)
10220 {
10221 Elf_Internal_Rela rel[3];
10222 bfd_vma addend = 0;
10223
10224 memset (rel, 0, sizeof (rel));
10225 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10226
10227 for (g = gg->next; g->next != gg; g = g->next)
10228 {
0f20cc35
DJ
10229 bfd_vma index = g->next->local_gotno + g->next->global_gotno
10230 + g->next->tls_gotno;
f4416af6 10231
9719ad41 10232 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
f4416af6 10233 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
10234 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10235 sgot->contents
f4416af6
AO
10236 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10237
10238 if (! info->shared)
10239 continue;
10240
10241 while (index < g->assigned_gotno)
10242 {
10243 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10244 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10245 if (!(mips_elf_create_dynamic_relocation
10246 (output_bfd, info, rel, NULL,
10247 bfd_abs_section_ptr,
10248 0, &addend, sgot)))
10249 return FALSE;
10250 BFD_ASSERT (addend == 0);
10251 }
10252 }
10253 }
10254
3133ddbf
DJ
10255 /* The generation of dynamic relocations for the non-primary gots
10256 adds more dynamic relocations. We cannot count them until
10257 here. */
10258
10259 if (elf_hash_table (info)->dynamic_sections_created)
10260 {
10261 bfd_byte *b;
10262 bfd_boolean swap_out_p;
10263
10264 BFD_ASSERT (sdyn != NULL);
10265
10266 for (b = sdyn->contents;
10267 b < sdyn->contents + sdyn->size;
10268 b += MIPS_ELF_DYN_SIZE (dynobj))
10269 {
10270 Elf_Internal_Dyn dyn;
10271 asection *s;
10272
10273 /* Read in the current dynamic entry. */
10274 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10275
10276 /* Assume that we're going to modify it and write it out. */
10277 swap_out_p = TRUE;
10278
10279 switch (dyn.d_tag)
10280 {
10281 case DT_RELSZ:
10282 /* Reduce DT_RELSZ to account for any relocations we
10283 decided not to make. This is for the n64 irix rld,
10284 which doesn't seem to apply any relocations if there
10285 are trailing null entries. */
0a44bf69 10286 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
10287 dyn.d_un.d_val = (s->reloc_count
10288 * (ABI_64_P (output_bfd)
10289 ? sizeof (Elf64_Mips_External_Rel)
10290 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
10291 /* Adjust the section size too. Tools like the prelinker
10292 can reasonably expect the values to the same. */
10293 elf_section_data (s->output_section)->this_hdr.sh_size
10294 = dyn.d_un.d_val;
3133ddbf
DJ
10295 break;
10296
10297 default:
10298 swap_out_p = FALSE;
10299 break;
10300 }
10301
10302 if (swap_out_p)
10303 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10304 (dynobj, &dyn, b);
10305 }
10306 }
10307
b49e97c9 10308 {
b49e97c9
TS
10309 asection *s;
10310 Elf32_compact_rel cpt;
10311
b49e97c9
TS
10312 if (SGI_COMPAT (output_bfd))
10313 {
10314 /* Write .compact_rel section out. */
10315 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10316 if (s != NULL)
10317 {
10318 cpt.id1 = 1;
10319 cpt.num = s->reloc_count;
10320 cpt.id2 = 2;
10321 cpt.offset = (s->output_section->filepos
10322 + sizeof (Elf32_External_compact_rel));
10323 cpt.reserved0 = 0;
10324 cpt.reserved1 = 0;
10325 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10326 ((Elf32_External_compact_rel *)
10327 s->contents));
10328
10329 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 10330 if (htab->sstubs != NULL)
b49e97c9
TS
10331 {
10332 file_ptr dummy_offset;
10333
4e41d0d7
RS
10334 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10335 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10336 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 10337 htab->function_stub_size);
b49e97c9
TS
10338 }
10339 }
10340 }
10341
0a44bf69
RS
10342 /* The psABI says that the dynamic relocations must be sorted in
10343 increasing order of r_symndx. The VxWorks EABI doesn't require
10344 this, and because the code below handles REL rather than RELA
10345 relocations, using it for VxWorks would be outright harmful. */
10346 if (!htab->is_vxworks)
b49e97c9 10347 {
0a44bf69
RS
10348 s = mips_elf_rel_dyn_section (info, FALSE);
10349 if (s != NULL
10350 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10351 {
10352 reldyn_sorting_bfd = output_bfd;
b49e97c9 10353
0a44bf69
RS
10354 if (ABI_64_P (output_bfd))
10355 qsort ((Elf64_External_Rel *) s->contents + 1,
10356 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10357 sort_dynamic_relocs_64);
10358 else
10359 qsort ((Elf32_External_Rel *) s->contents + 1,
10360 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10361 sort_dynamic_relocs);
10362 }
b49e97c9 10363 }
b49e97c9
TS
10364 }
10365
861fb55a 10366 if (htab->splt && htab->splt->size > 0)
0a44bf69 10367 {
861fb55a
DJ
10368 if (htab->is_vxworks)
10369 {
10370 if (info->shared)
10371 mips_vxworks_finish_shared_plt (output_bfd, info);
10372 else
10373 mips_vxworks_finish_exec_plt (output_bfd, info);
10374 }
0a44bf69 10375 else
861fb55a
DJ
10376 {
10377 BFD_ASSERT (!info->shared);
10378 mips_finish_exec_plt (output_bfd, info);
10379 }
0a44bf69 10380 }
b34976b6 10381 return TRUE;
b49e97c9
TS
10382}
10383
b49e97c9 10384
64543e1a
RS
10385/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10386
10387static void
9719ad41 10388mips_set_isa_flags (bfd *abfd)
b49e97c9 10389{
64543e1a 10390 flagword val;
b49e97c9
TS
10391
10392 switch (bfd_get_mach (abfd))
10393 {
10394 default:
10395 case bfd_mach_mips3000:
10396 val = E_MIPS_ARCH_1;
10397 break;
10398
10399 case bfd_mach_mips3900:
10400 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10401 break;
10402
10403 case bfd_mach_mips6000:
10404 val = E_MIPS_ARCH_2;
10405 break;
10406
10407 case bfd_mach_mips4000:
10408 case bfd_mach_mips4300:
10409 case bfd_mach_mips4400:
10410 case bfd_mach_mips4600:
10411 val = E_MIPS_ARCH_3;
10412 break;
10413
10414 case bfd_mach_mips4010:
10415 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10416 break;
10417
10418 case bfd_mach_mips4100:
10419 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10420 break;
10421
10422 case bfd_mach_mips4111:
10423 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10424 break;
10425
00707a0e
RS
10426 case bfd_mach_mips4120:
10427 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10428 break;
10429
b49e97c9
TS
10430 case bfd_mach_mips4650:
10431 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10432 break;
10433
00707a0e
RS
10434 case bfd_mach_mips5400:
10435 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10436 break;
10437
10438 case bfd_mach_mips5500:
10439 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10440 break;
10441
0d2e43ed
ILT
10442 case bfd_mach_mips9000:
10443 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10444 break;
10445
b49e97c9 10446 case bfd_mach_mips5000:
5a7ea749 10447 case bfd_mach_mips7000:
b49e97c9
TS
10448 case bfd_mach_mips8000:
10449 case bfd_mach_mips10000:
10450 case bfd_mach_mips12000:
3aa3176b
TS
10451 case bfd_mach_mips14000:
10452 case bfd_mach_mips16000:
b49e97c9
TS
10453 val = E_MIPS_ARCH_4;
10454 break;
10455
10456 case bfd_mach_mips5:
10457 val = E_MIPS_ARCH_5;
10458 break;
10459
350cc38d
MS
10460 case bfd_mach_mips_loongson_2e:
10461 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10462 break;
10463
10464 case bfd_mach_mips_loongson_2f:
10465 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10466 break;
10467
b49e97c9
TS
10468 case bfd_mach_mips_sb1:
10469 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10470 break;
10471
6f179bd0
AN
10472 case bfd_mach_mips_octeon:
10473 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10474 break;
10475
52b6b6b9
JM
10476 case bfd_mach_mips_xlr:
10477 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10478 break;
10479
b49e97c9
TS
10480 case bfd_mach_mipsisa32:
10481 val = E_MIPS_ARCH_32;
10482 break;
10483
10484 case bfd_mach_mipsisa64:
10485 val = E_MIPS_ARCH_64;
af7ee8bf
CD
10486 break;
10487
10488 case bfd_mach_mipsisa32r2:
10489 val = E_MIPS_ARCH_32R2;
10490 break;
5f74bc13
CD
10491
10492 case bfd_mach_mipsisa64r2:
10493 val = E_MIPS_ARCH_64R2;
10494 break;
b49e97c9 10495 }
b49e97c9
TS
10496 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10497 elf_elfheader (abfd)->e_flags |= val;
10498
64543e1a
RS
10499}
10500
10501
10502/* The final processing done just before writing out a MIPS ELF object
10503 file. This gets the MIPS architecture right based on the machine
10504 number. This is used by both the 32-bit and the 64-bit ABI. */
10505
10506void
9719ad41
RS
10507_bfd_mips_elf_final_write_processing (bfd *abfd,
10508 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
10509{
10510 unsigned int i;
10511 Elf_Internal_Shdr **hdrpp;
10512 const char *name;
10513 asection *sec;
10514
10515 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10516 is nonzero. This is for compatibility with old objects, which used
10517 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10518 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10519 mips_set_isa_flags (abfd);
10520
b49e97c9
TS
10521 /* Set the sh_info field for .gptab sections and other appropriate
10522 info for each special section. */
10523 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10524 i < elf_numsections (abfd);
10525 i++, hdrpp++)
10526 {
10527 switch ((*hdrpp)->sh_type)
10528 {
10529 case SHT_MIPS_MSYM:
10530 case SHT_MIPS_LIBLIST:
10531 sec = bfd_get_section_by_name (abfd, ".dynstr");
10532 if (sec != NULL)
10533 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10534 break;
10535
10536 case SHT_MIPS_GPTAB:
10537 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10538 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10539 BFD_ASSERT (name != NULL
0112cd26 10540 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
10541 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10542 BFD_ASSERT (sec != NULL);
10543 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10544 break;
10545
10546 case SHT_MIPS_CONTENT:
10547 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10548 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10549 BFD_ASSERT (name != NULL
0112cd26 10550 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
10551 sec = bfd_get_section_by_name (abfd,
10552 name + sizeof ".MIPS.content" - 1);
10553 BFD_ASSERT (sec != NULL);
10554 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10555 break;
10556
10557 case SHT_MIPS_SYMBOL_LIB:
10558 sec = bfd_get_section_by_name (abfd, ".dynsym");
10559 if (sec != NULL)
10560 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10561 sec = bfd_get_section_by_name (abfd, ".liblist");
10562 if (sec != NULL)
10563 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10564 break;
10565
10566 case SHT_MIPS_EVENTS:
10567 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10568 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10569 BFD_ASSERT (name != NULL);
0112cd26 10570 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
10571 sec = bfd_get_section_by_name (abfd,
10572 name + sizeof ".MIPS.events" - 1);
10573 else
10574 {
0112cd26 10575 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
10576 sec = bfd_get_section_by_name (abfd,
10577 (name
10578 + sizeof ".MIPS.post_rel" - 1));
10579 }
10580 BFD_ASSERT (sec != NULL);
10581 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10582 break;
10583
10584 }
10585 }
10586}
10587\f
8dc1a139 10588/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
10589 segments. */
10590
10591int
a6b96beb
AM
10592_bfd_mips_elf_additional_program_headers (bfd *abfd,
10593 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
10594{
10595 asection *s;
10596 int ret = 0;
10597
10598 /* See if we need a PT_MIPS_REGINFO segment. */
10599 s = bfd_get_section_by_name (abfd, ".reginfo");
10600 if (s && (s->flags & SEC_LOAD))
10601 ++ret;
10602
10603 /* See if we need a PT_MIPS_OPTIONS segment. */
10604 if (IRIX_COMPAT (abfd) == ict_irix6
10605 && bfd_get_section_by_name (abfd,
10606 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10607 ++ret;
10608
10609 /* See if we need a PT_MIPS_RTPROC segment. */
10610 if (IRIX_COMPAT (abfd) == ict_irix5
10611 && bfd_get_section_by_name (abfd, ".dynamic")
10612 && bfd_get_section_by_name (abfd, ".mdebug"))
10613 ++ret;
10614
98c904a8
RS
10615 /* Allocate a PT_NULL header in dynamic objects. See
10616 _bfd_mips_elf_modify_segment_map for details. */
10617 if (!SGI_COMPAT (abfd)
10618 && bfd_get_section_by_name (abfd, ".dynamic"))
10619 ++ret;
10620
b49e97c9
TS
10621 return ret;
10622}
10623
8dc1a139 10624/* Modify the segment map for an IRIX5 executable. */
b49e97c9 10625
b34976b6 10626bfd_boolean
9719ad41 10627_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 10628 struct bfd_link_info *info)
b49e97c9
TS
10629{
10630 asection *s;
10631 struct elf_segment_map *m, **pm;
10632 bfd_size_type amt;
10633
10634 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10635 segment. */
10636 s = bfd_get_section_by_name (abfd, ".reginfo");
10637 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10638 {
10639 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10640 if (m->p_type == PT_MIPS_REGINFO)
10641 break;
10642 if (m == NULL)
10643 {
10644 amt = sizeof *m;
9719ad41 10645 m = bfd_zalloc (abfd, amt);
b49e97c9 10646 if (m == NULL)
b34976b6 10647 return FALSE;
b49e97c9
TS
10648
10649 m->p_type = PT_MIPS_REGINFO;
10650 m->count = 1;
10651 m->sections[0] = s;
10652
10653 /* We want to put it after the PHDR and INTERP segments. */
10654 pm = &elf_tdata (abfd)->segment_map;
10655 while (*pm != NULL
10656 && ((*pm)->p_type == PT_PHDR
10657 || (*pm)->p_type == PT_INTERP))
10658 pm = &(*pm)->next;
10659
10660 m->next = *pm;
10661 *pm = m;
10662 }
10663 }
10664
10665 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10666 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 10667 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 10668 table. */
c1fd6598
AO
10669 if (NEWABI_P (abfd)
10670 /* On non-IRIX6 new abi, we'll have already created a segment
10671 for this section, so don't create another. I'm not sure this
10672 is not also the case for IRIX 6, but I can't test it right
10673 now. */
10674 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
10675 {
10676 for (s = abfd->sections; s; s = s->next)
10677 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10678 break;
10679
10680 if (s)
10681 {
10682 struct elf_segment_map *options_segment;
10683
98a8deaf
RS
10684 pm = &elf_tdata (abfd)->segment_map;
10685 while (*pm != NULL
10686 && ((*pm)->p_type == PT_PHDR
10687 || (*pm)->p_type == PT_INTERP))
10688 pm = &(*pm)->next;
b49e97c9 10689
8ded5a0f
AM
10690 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10691 {
10692 amt = sizeof (struct elf_segment_map);
10693 options_segment = bfd_zalloc (abfd, amt);
10694 options_segment->next = *pm;
10695 options_segment->p_type = PT_MIPS_OPTIONS;
10696 options_segment->p_flags = PF_R;
10697 options_segment->p_flags_valid = TRUE;
10698 options_segment->count = 1;
10699 options_segment->sections[0] = s;
10700 *pm = options_segment;
10701 }
b49e97c9
TS
10702 }
10703 }
10704 else
10705 {
10706 if (IRIX_COMPAT (abfd) == ict_irix5)
10707 {
10708 /* If there are .dynamic and .mdebug sections, we make a room
10709 for the RTPROC header. FIXME: Rewrite without section names. */
10710 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10711 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10712 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10713 {
10714 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10715 if (m->p_type == PT_MIPS_RTPROC)
10716 break;
10717 if (m == NULL)
10718 {
10719 amt = sizeof *m;
9719ad41 10720 m = bfd_zalloc (abfd, amt);
b49e97c9 10721 if (m == NULL)
b34976b6 10722 return FALSE;
b49e97c9
TS
10723
10724 m->p_type = PT_MIPS_RTPROC;
10725
10726 s = bfd_get_section_by_name (abfd, ".rtproc");
10727 if (s == NULL)
10728 {
10729 m->count = 0;
10730 m->p_flags = 0;
10731 m->p_flags_valid = 1;
10732 }
10733 else
10734 {
10735 m->count = 1;
10736 m->sections[0] = s;
10737 }
10738
10739 /* We want to put it after the DYNAMIC segment. */
10740 pm = &elf_tdata (abfd)->segment_map;
10741 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10742 pm = &(*pm)->next;
10743 if (*pm != NULL)
10744 pm = &(*pm)->next;
10745
10746 m->next = *pm;
10747 *pm = m;
10748 }
10749 }
10750 }
8dc1a139 10751 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
10752 .dynstr, .dynsym, and .hash sections, and everything in
10753 between. */
10754 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10755 pm = &(*pm)->next)
10756 if ((*pm)->p_type == PT_DYNAMIC)
10757 break;
10758 m = *pm;
10759 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10760 {
10761 /* For a normal mips executable the permissions for the PT_DYNAMIC
10762 segment are read, write and execute. We do that here since
10763 the code in elf.c sets only the read permission. This matters
10764 sometimes for the dynamic linker. */
10765 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10766 {
10767 m->p_flags = PF_R | PF_W | PF_X;
10768 m->p_flags_valid = 1;
10769 }
10770 }
f6f62d6f
RS
10771 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10772 glibc's dynamic linker has traditionally derived the number of
10773 tags from the p_filesz field, and sometimes allocates stack
10774 arrays of that size. An overly-big PT_DYNAMIC segment can
10775 be actively harmful in such cases. Making PT_DYNAMIC contain
10776 other sections can also make life hard for the prelinker,
10777 which might move one of the other sections to a different
10778 PT_LOAD segment. */
10779 if (SGI_COMPAT (abfd)
10780 && m != NULL
10781 && m->count == 1
10782 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
10783 {
10784 static const char *sec_names[] =
10785 {
10786 ".dynamic", ".dynstr", ".dynsym", ".hash"
10787 };
10788 bfd_vma low, high;
10789 unsigned int i, c;
10790 struct elf_segment_map *n;
10791
792b4a53 10792 low = ~(bfd_vma) 0;
b49e97c9
TS
10793 high = 0;
10794 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10795 {
10796 s = bfd_get_section_by_name (abfd, sec_names[i]);
10797 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10798 {
10799 bfd_size_type sz;
10800
10801 if (low > s->vma)
10802 low = s->vma;
eea6121a 10803 sz = s->size;
b49e97c9
TS
10804 if (high < s->vma + sz)
10805 high = s->vma + sz;
10806 }
10807 }
10808
10809 c = 0;
10810 for (s = abfd->sections; s != NULL; s = s->next)
10811 if ((s->flags & SEC_LOAD) != 0
10812 && s->vma >= low
eea6121a 10813 && s->vma + s->size <= high)
b49e97c9
TS
10814 ++c;
10815
10816 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 10817 n = bfd_zalloc (abfd, amt);
b49e97c9 10818 if (n == NULL)
b34976b6 10819 return FALSE;
b49e97c9
TS
10820 *n = *m;
10821 n->count = c;
10822
10823 i = 0;
10824 for (s = abfd->sections; s != NULL; s = s->next)
10825 {
10826 if ((s->flags & SEC_LOAD) != 0
10827 && s->vma >= low
eea6121a 10828 && s->vma + s->size <= high)
b49e97c9
TS
10829 {
10830 n->sections[i] = s;
10831 ++i;
10832 }
10833 }
10834
10835 *pm = n;
10836 }
10837 }
10838
98c904a8
RS
10839 /* Allocate a spare program header in dynamic objects so that tools
10840 like the prelinker can add an extra PT_LOAD entry.
10841
10842 If the prelinker needs to make room for a new PT_LOAD entry, its
10843 standard procedure is to move the first (read-only) sections into
10844 the new (writable) segment. However, the MIPS ABI requires
10845 .dynamic to be in a read-only segment, and the section will often
10846 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10847
10848 Although the prelinker could in principle move .dynamic to a
10849 writable segment, it seems better to allocate a spare program
10850 header instead, and avoid the need to move any sections.
10851 There is a long tradition of allocating spare dynamic tags,
10852 so allocating a spare program header seems like a natural
7c8b76cc
JM
10853 extension.
10854
10855 If INFO is NULL, we may be copying an already prelinked binary
10856 with objcopy or strip, so do not add this header. */
10857 if (info != NULL
10858 && !SGI_COMPAT (abfd)
98c904a8
RS
10859 && bfd_get_section_by_name (abfd, ".dynamic"))
10860 {
10861 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10862 if ((*pm)->p_type == PT_NULL)
10863 break;
10864 if (*pm == NULL)
10865 {
10866 m = bfd_zalloc (abfd, sizeof (*m));
10867 if (m == NULL)
10868 return FALSE;
10869
10870 m->p_type = PT_NULL;
10871 *pm = m;
10872 }
10873 }
10874
b34976b6 10875 return TRUE;
b49e97c9
TS
10876}
10877\f
10878/* Return the section that should be marked against GC for a given
10879 relocation. */
10880
10881asection *
9719ad41 10882_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 10883 struct bfd_link_info *info,
9719ad41
RS
10884 Elf_Internal_Rela *rel,
10885 struct elf_link_hash_entry *h,
10886 Elf_Internal_Sym *sym)
b49e97c9
TS
10887{
10888 /* ??? Do mips16 stub sections need to be handled special? */
10889
10890 if (h != NULL)
07adf181
AM
10891 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10892 {
10893 case R_MIPS_GNU_VTINHERIT:
10894 case R_MIPS_GNU_VTENTRY:
10895 return NULL;
10896 }
b49e97c9 10897
07adf181 10898 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
10899}
10900
10901/* Update the got entry reference counts for the section being removed. */
10902
b34976b6 10903bfd_boolean
9719ad41
RS
10904_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10905 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10906 asection *sec ATTRIBUTE_UNUSED,
10907 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
10908{
10909#if 0
10910 Elf_Internal_Shdr *symtab_hdr;
10911 struct elf_link_hash_entry **sym_hashes;
10912 bfd_signed_vma *local_got_refcounts;
10913 const Elf_Internal_Rela *rel, *relend;
10914 unsigned long r_symndx;
10915 struct elf_link_hash_entry *h;
10916
7dda2462
TG
10917 if (info->relocatable)
10918 return TRUE;
10919
b49e97c9
TS
10920 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10921 sym_hashes = elf_sym_hashes (abfd);
10922 local_got_refcounts = elf_local_got_refcounts (abfd);
10923
10924 relend = relocs + sec->reloc_count;
10925 for (rel = relocs; rel < relend; rel++)
10926 switch (ELF_R_TYPE (abfd, rel->r_info))
10927 {
738e5348
RS
10928 case R_MIPS16_GOT16:
10929 case R_MIPS16_CALL16:
b49e97c9
TS
10930 case R_MIPS_GOT16:
10931 case R_MIPS_CALL16:
10932 case R_MIPS_CALL_HI16:
10933 case R_MIPS_CALL_LO16:
10934 case R_MIPS_GOT_HI16:
10935 case R_MIPS_GOT_LO16:
4a14403c
TS
10936 case R_MIPS_GOT_DISP:
10937 case R_MIPS_GOT_PAGE:
10938 case R_MIPS_GOT_OFST:
b49e97c9
TS
10939 /* ??? It would seem that the existing MIPS code does no sort
10940 of reference counting or whatnot on its GOT and PLT entries,
10941 so it is not possible to garbage collect them at this time. */
10942 break;
10943
10944 default:
10945 break;
10946 }
10947#endif
10948
b34976b6 10949 return TRUE;
b49e97c9
TS
10950}
10951\f
10952/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10953 hiding the old indirect symbol. Process additional relocation
10954 information. Also called for weakdefs, in which case we just let
10955 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
10956
10957void
fcfa13d2 10958_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
10959 struct elf_link_hash_entry *dir,
10960 struct elf_link_hash_entry *ind)
b49e97c9
TS
10961{
10962 struct mips_elf_link_hash_entry *dirmips, *indmips;
10963
fcfa13d2 10964 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 10965
861fb55a
DJ
10966 dirmips = (struct mips_elf_link_hash_entry *) dir;
10967 indmips = (struct mips_elf_link_hash_entry *) ind;
10968 /* Any absolute non-dynamic relocations against an indirect or weak
10969 definition will be against the target symbol. */
10970 if (indmips->has_static_relocs)
10971 dirmips->has_static_relocs = TRUE;
10972
b49e97c9
TS
10973 if (ind->root.type != bfd_link_hash_indirect)
10974 return;
10975
b49e97c9
TS
10976 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10977 if (indmips->readonly_reloc)
b34976b6 10978 dirmips->readonly_reloc = TRUE;
b49e97c9 10979 if (indmips->no_fn_stub)
b34976b6 10980 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
10981 if (indmips->fn_stub)
10982 {
10983 dirmips->fn_stub = indmips->fn_stub;
10984 indmips->fn_stub = NULL;
10985 }
10986 if (indmips->need_fn_stub)
10987 {
10988 dirmips->need_fn_stub = TRUE;
10989 indmips->need_fn_stub = FALSE;
10990 }
10991 if (indmips->call_stub)
10992 {
10993 dirmips->call_stub = indmips->call_stub;
10994 indmips->call_stub = NULL;
10995 }
10996 if (indmips->call_fp_stub)
10997 {
10998 dirmips->call_fp_stub = indmips->call_fp_stub;
10999 indmips->call_fp_stub = NULL;
11000 }
634835ae
RS
11001 if (indmips->global_got_area < dirmips->global_got_area)
11002 dirmips->global_got_area = indmips->global_got_area;
11003 if (indmips->global_got_area < GGA_NONE)
11004 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
11005 if (indmips->has_nonpic_branches)
11006 dirmips->has_nonpic_branches = TRUE;
0f20cc35
DJ
11007
11008 if (dirmips->tls_type == 0)
11009 dirmips->tls_type = indmips->tls_type;
b49e97c9 11010}
b49e97c9 11011\f
d01414a5
TS
11012#define PDR_SIZE 32
11013
b34976b6 11014bfd_boolean
9719ad41
RS
11015_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11016 struct bfd_link_info *info)
d01414a5
TS
11017{
11018 asection *o;
b34976b6 11019 bfd_boolean ret = FALSE;
d01414a5
TS
11020 unsigned char *tdata;
11021 size_t i, skip;
11022
11023 o = bfd_get_section_by_name (abfd, ".pdr");
11024 if (! o)
b34976b6 11025 return FALSE;
eea6121a 11026 if (o->size == 0)
b34976b6 11027 return FALSE;
eea6121a 11028 if (o->size % PDR_SIZE != 0)
b34976b6 11029 return FALSE;
d01414a5
TS
11030 if (o->output_section != NULL
11031 && bfd_is_abs_section (o->output_section))
b34976b6 11032 return FALSE;
d01414a5 11033
eea6121a 11034 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 11035 if (! tdata)
b34976b6 11036 return FALSE;
d01414a5 11037
9719ad41 11038 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 11039 info->keep_memory);
d01414a5
TS
11040 if (!cookie->rels)
11041 {
11042 free (tdata);
b34976b6 11043 return FALSE;
d01414a5
TS
11044 }
11045
11046 cookie->rel = cookie->rels;
11047 cookie->relend = cookie->rels + o->reloc_count;
11048
eea6121a 11049 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 11050 {
c152c796 11051 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
11052 {
11053 tdata[i] = 1;
11054 skip ++;
11055 }
11056 }
11057
11058 if (skip != 0)
11059 {
f0abc2a1 11060 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 11061 o->size -= skip * PDR_SIZE;
b34976b6 11062 ret = TRUE;
d01414a5
TS
11063 }
11064 else
11065 free (tdata);
11066
11067 if (! info->keep_memory)
11068 free (cookie->rels);
11069
11070 return ret;
11071}
11072
b34976b6 11073bfd_boolean
9719ad41 11074_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
11075{
11076 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
11077 return TRUE;
11078 return FALSE;
53bfd6b4 11079}
d01414a5 11080
b34976b6 11081bfd_boolean
c7b8f16e
JB
11082_bfd_mips_elf_write_section (bfd *output_bfd,
11083 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11084 asection *sec, bfd_byte *contents)
d01414a5
TS
11085{
11086 bfd_byte *to, *from, *end;
11087 int i;
11088
11089 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 11090 return FALSE;
d01414a5 11091
f0abc2a1 11092 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 11093 return FALSE;
d01414a5
TS
11094
11095 to = contents;
eea6121a 11096 end = contents + sec->size;
d01414a5
TS
11097 for (from = contents, i = 0;
11098 from < end;
11099 from += PDR_SIZE, i++)
11100 {
f0abc2a1 11101 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
11102 continue;
11103 if (to != from)
11104 memcpy (to, from, PDR_SIZE);
11105 to += PDR_SIZE;
11106 }
11107 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 11108 sec->output_offset, sec->size);
b34976b6 11109 return TRUE;
d01414a5 11110}
53bfd6b4 11111\f
b49e97c9
TS
11112/* MIPS ELF uses a special find_nearest_line routine in order the
11113 handle the ECOFF debugging information. */
11114
11115struct mips_elf_find_line
11116{
11117 struct ecoff_debug_info d;
11118 struct ecoff_find_line i;
11119};
11120
b34976b6 11121bfd_boolean
9719ad41
RS
11122_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11123 asymbol **symbols, bfd_vma offset,
11124 const char **filename_ptr,
11125 const char **functionname_ptr,
11126 unsigned int *line_ptr)
b49e97c9
TS
11127{
11128 asection *msec;
11129
11130 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11131 filename_ptr, functionname_ptr,
11132 line_ptr))
b34976b6 11133 return TRUE;
b49e97c9
TS
11134
11135 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11136 filename_ptr, functionname_ptr,
9719ad41 11137 line_ptr, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 11138 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 11139 return TRUE;
b49e97c9
TS
11140
11141 msec = bfd_get_section_by_name (abfd, ".mdebug");
11142 if (msec != NULL)
11143 {
11144 flagword origflags;
11145 struct mips_elf_find_line *fi;
11146 const struct ecoff_debug_swap * const swap =
11147 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11148
11149 /* If we are called during a link, mips_elf_final_link may have
11150 cleared the SEC_HAS_CONTENTS field. We force it back on here
11151 if appropriate (which it normally will be). */
11152 origflags = msec->flags;
11153 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11154 msec->flags |= SEC_HAS_CONTENTS;
11155
11156 fi = elf_tdata (abfd)->find_line_info;
11157 if (fi == NULL)
11158 {
11159 bfd_size_type external_fdr_size;
11160 char *fraw_src;
11161 char *fraw_end;
11162 struct fdr *fdr_ptr;
11163 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11164
9719ad41 11165 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
11166 if (fi == NULL)
11167 {
11168 msec->flags = origflags;
b34976b6 11169 return FALSE;
b49e97c9
TS
11170 }
11171
11172 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11173 {
11174 msec->flags = origflags;
b34976b6 11175 return FALSE;
b49e97c9
TS
11176 }
11177
11178 /* Swap in the FDR information. */
11179 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 11180 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
11181 if (fi->d.fdr == NULL)
11182 {
11183 msec->flags = origflags;
b34976b6 11184 return FALSE;
b49e97c9
TS
11185 }
11186 external_fdr_size = swap->external_fdr_size;
11187 fdr_ptr = fi->d.fdr;
11188 fraw_src = (char *) fi->d.external_fdr;
11189 fraw_end = (fraw_src
11190 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11191 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 11192 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9
TS
11193
11194 elf_tdata (abfd)->find_line_info = fi;
11195
11196 /* Note that we don't bother to ever free this information.
11197 find_nearest_line is either called all the time, as in
11198 objdump -l, so the information should be saved, or it is
11199 rarely called, as in ld error messages, so the memory
11200 wasted is unimportant. Still, it would probably be a
11201 good idea for free_cached_info to throw it away. */
11202 }
11203
11204 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11205 &fi->i, filename_ptr, functionname_ptr,
11206 line_ptr))
11207 {
11208 msec->flags = origflags;
b34976b6 11209 return TRUE;
b49e97c9
TS
11210 }
11211
11212 msec->flags = origflags;
11213 }
11214
11215 /* Fall back on the generic ELF find_nearest_line routine. */
11216
11217 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11218 filename_ptr, functionname_ptr,
11219 line_ptr);
11220}
4ab527b0
FF
11221
11222bfd_boolean
11223_bfd_mips_elf_find_inliner_info (bfd *abfd,
11224 const char **filename_ptr,
11225 const char **functionname_ptr,
11226 unsigned int *line_ptr)
11227{
11228 bfd_boolean found;
11229 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11230 functionname_ptr, line_ptr,
11231 & elf_tdata (abfd)->dwarf2_find_line_info);
11232 return found;
11233}
11234
b49e97c9
TS
11235\f
11236/* When are writing out the .options or .MIPS.options section,
11237 remember the bytes we are writing out, so that we can install the
11238 GP value in the section_processing routine. */
11239
b34976b6 11240bfd_boolean
9719ad41
RS
11241_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11242 const void *location,
11243 file_ptr offset, bfd_size_type count)
b49e97c9 11244{
cc2e31b9 11245 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
11246 {
11247 bfd_byte *c;
11248
11249 if (elf_section_data (section) == NULL)
11250 {
11251 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 11252 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 11253 if (elf_section_data (section) == NULL)
b34976b6 11254 return FALSE;
b49e97c9 11255 }
f0abc2a1 11256 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
11257 if (c == NULL)
11258 {
eea6121a 11259 c = bfd_zalloc (abfd, section->size);
b49e97c9 11260 if (c == NULL)
b34976b6 11261 return FALSE;
f0abc2a1 11262 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
11263 }
11264
9719ad41 11265 memcpy (c + offset, location, count);
b49e97c9
TS
11266 }
11267
11268 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11269 count);
11270}
11271
11272/* This is almost identical to bfd_generic_get_... except that some
11273 MIPS relocations need to be handled specially. Sigh. */
11274
11275bfd_byte *
9719ad41
RS
11276_bfd_elf_mips_get_relocated_section_contents
11277 (bfd *abfd,
11278 struct bfd_link_info *link_info,
11279 struct bfd_link_order *link_order,
11280 bfd_byte *data,
11281 bfd_boolean relocatable,
11282 asymbol **symbols)
b49e97c9
TS
11283{
11284 /* Get enough memory to hold the stuff */
11285 bfd *input_bfd = link_order->u.indirect.section->owner;
11286 asection *input_section = link_order->u.indirect.section;
eea6121a 11287 bfd_size_type sz;
b49e97c9
TS
11288
11289 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11290 arelent **reloc_vector = NULL;
11291 long reloc_count;
11292
11293 if (reloc_size < 0)
11294 goto error_return;
11295
9719ad41 11296 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
11297 if (reloc_vector == NULL && reloc_size != 0)
11298 goto error_return;
11299
11300 /* read in the section */
eea6121a
AM
11301 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11302 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
11303 goto error_return;
11304
b49e97c9
TS
11305 reloc_count = bfd_canonicalize_reloc (input_bfd,
11306 input_section,
11307 reloc_vector,
11308 symbols);
11309 if (reloc_count < 0)
11310 goto error_return;
11311
11312 if (reloc_count > 0)
11313 {
11314 arelent **parent;
11315 /* for mips */
11316 int gp_found;
11317 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11318
11319 {
11320 struct bfd_hash_entry *h;
11321 struct bfd_link_hash_entry *lh;
11322 /* Skip all this stuff if we aren't mixing formats. */
11323 if (abfd && input_bfd
11324 && abfd->xvec == input_bfd->xvec)
11325 lh = 0;
11326 else
11327 {
b34976b6 11328 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
11329 lh = (struct bfd_link_hash_entry *) h;
11330 }
11331 lookup:
11332 if (lh)
11333 {
11334 switch (lh->type)
11335 {
11336 case bfd_link_hash_undefined:
11337 case bfd_link_hash_undefweak:
11338 case bfd_link_hash_common:
11339 gp_found = 0;
11340 break;
11341 case bfd_link_hash_defined:
11342 case bfd_link_hash_defweak:
11343 gp_found = 1;
11344 gp = lh->u.def.value;
11345 break;
11346 case bfd_link_hash_indirect:
11347 case bfd_link_hash_warning:
11348 lh = lh->u.i.link;
11349 /* @@FIXME ignoring warning for now */
11350 goto lookup;
11351 case bfd_link_hash_new:
11352 default:
11353 abort ();
11354 }
11355 }
11356 else
11357 gp_found = 0;
11358 }
11359 /* end mips */
9719ad41 11360 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 11361 {
9719ad41 11362 char *error_message = NULL;
b49e97c9
TS
11363 bfd_reloc_status_type r;
11364
11365 /* Specific to MIPS: Deal with relocation types that require
11366 knowing the gp of the output bfd. */
11367 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 11368
8236346f
EC
11369 /* If we've managed to find the gp and have a special
11370 function for the relocation then go ahead, else default
11371 to the generic handling. */
11372 if (gp_found
11373 && (*parent)->howto->special_function
11374 == _bfd_mips_elf32_gprel16_reloc)
11375 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11376 input_section, relocatable,
11377 data, gp);
11378 else
86324f90 11379 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
11380 input_section,
11381 relocatable ? abfd : NULL,
11382 &error_message);
b49e97c9 11383
1049f94e 11384 if (relocatable)
b49e97c9
TS
11385 {
11386 asection *os = input_section->output_section;
11387
11388 /* A partial link, so keep the relocs */
11389 os->orelocation[os->reloc_count] = *parent;
11390 os->reloc_count++;
11391 }
11392
11393 if (r != bfd_reloc_ok)
11394 {
11395 switch (r)
11396 {
11397 case bfd_reloc_undefined:
11398 if (!((*link_info->callbacks->undefined_symbol)
11399 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 11400 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
11401 goto error_return;
11402 break;
11403 case bfd_reloc_dangerous:
9719ad41 11404 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
11405 if (!((*link_info->callbacks->reloc_dangerous)
11406 (link_info, error_message, input_bfd, input_section,
11407 (*parent)->address)))
11408 goto error_return;
11409 break;
11410 case bfd_reloc_overflow:
11411 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
11412 (link_info, NULL,
11413 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
11414 (*parent)->howto->name, (*parent)->addend,
11415 input_bfd, input_section, (*parent)->address)))
11416 goto error_return;
11417 break;
11418 case bfd_reloc_outofrange:
11419 default:
11420 abort ();
11421 break;
11422 }
11423
11424 }
11425 }
11426 }
11427 if (reloc_vector != NULL)
11428 free (reloc_vector);
11429 return data;
11430
11431error_return:
11432 if (reloc_vector != NULL)
11433 free (reloc_vector);
11434 return NULL;
11435}
11436\f
d5eaccd7
RS
11437/* Allocate ABFD's target-dependent data. */
11438
11439bfd_boolean
11440_bfd_mips_elf_mkobject (bfd *abfd)
11441{
11442 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
11443 MIPS_ELF_TDATA);
11444}
11445
b49e97c9
TS
11446/* Create a MIPS ELF linker hash table. */
11447
11448struct bfd_link_hash_table *
9719ad41 11449_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
11450{
11451 struct mips_elf_link_hash_table *ret;
11452 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11453
9719ad41
RS
11454 ret = bfd_malloc (amt);
11455 if (ret == NULL)
b49e97c9
TS
11456 return NULL;
11457
66eb6687
AM
11458 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11459 mips_elf_link_hash_newfunc,
11460 sizeof (struct mips_elf_link_hash_entry)))
b49e97c9 11461 {
e2d34d7d 11462 free (ret);
b49e97c9
TS
11463 return NULL;
11464 }
11465
11466#if 0
11467 /* We no longer use this. */
11468 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11469 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11470#endif
11471 ret->procedure_count = 0;
11472 ret->compact_rel_size = 0;
b34976b6 11473 ret->use_rld_obj_head = FALSE;
b49e97c9 11474 ret->rld_value = 0;
b34976b6 11475 ret->mips16_stubs_seen = FALSE;
861fb55a 11476 ret->use_plts_and_copy_relocs = FALSE;
0a44bf69 11477 ret->is_vxworks = FALSE;
0e53d9da 11478 ret->small_data_overflow_reported = FALSE;
0a44bf69
RS
11479 ret->srelbss = NULL;
11480 ret->sdynbss = NULL;
11481 ret->srelplt = NULL;
11482 ret->srelplt2 = NULL;
11483 ret->sgotplt = NULL;
11484 ret->splt = NULL;
4e41d0d7 11485 ret->sstubs = NULL;
a8028dd0
RS
11486 ret->sgot = NULL;
11487 ret->got_info = NULL;
0a44bf69
RS
11488 ret->plt_header_size = 0;
11489 ret->plt_entry_size = 0;
33bb52fb 11490 ret->lazy_stub_count = 0;
5108fc1b 11491 ret->function_stub_size = 0;
861fb55a
DJ
11492 ret->strampoline = NULL;
11493 ret->la25_stubs = NULL;
11494 ret->add_stub_section = NULL;
b49e97c9
TS
11495
11496 return &ret->root.root;
11497}
0a44bf69
RS
11498
11499/* Likewise, but indicate that the target is VxWorks. */
11500
11501struct bfd_link_hash_table *
11502_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11503{
11504 struct bfd_link_hash_table *ret;
11505
11506 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11507 if (ret)
11508 {
11509 struct mips_elf_link_hash_table *htab;
11510
11511 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
11512 htab->use_plts_and_copy_relocs = TRUE;
11513 htab->is_vxworks = TRUE;
0a44bf69
RS
11514 }
11515 return ret;
11516}
861fb55a
DJ
11517
11518/* A function that the linker calls if we are allowed to use PLTs
11519 and copy relocs. */
11520
11521void
11522_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11523{
11524 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11525}
b49e97c9
TS
11526\f
11527/* We need to use a special link routine to handle the .reginfo and
11528 the .mdebug sections. We need to merge all instances of these
11529 sections together, not write them all out sequentially. */
11530
b34976b6 11531bfd_boolean
9719ad41 11532_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 11533{
b49e97c9
TS
11534 asection *o;
11535 struct bfd_link_order *p;
11536 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11537 asection *rtproc_sec;
11538 Elf32_RegInfo reginfo;
11539 struct ecoff_debug_info debug;
861fb55a 11540 struct mips_htab_traverse_info hti;
7a2a6943
NC
11541 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11542 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 11543 HDRR *symhdr = &debug.symbolic_header;
9719ad41 11544 void *mdebug_handle = NULL;
b49e97c9
TS
11545 asection *s;
11546 EXTR esym;
11547 unsigned int i;
11548 bfd_size_type amt;
0a44bf69 11549 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
11550
11551 static const char * const secname[] =
11552 {
11553 ".text", ".init", ".fini", ".data",
11554 ".rodata", ".sdata", ".sbss", ".bss"
11555 };
11556 static const int sc[] =
11557 {
11558 scText, scInit, scFini, scData,
11559 scRData, scSData, scSBss, scBss
11560 };
11561
d4596a51
RS
11562 /* Sort the dynamic symbols so that those with GOT entries come after
11563 those without. */
0a44bf69 11564 htab = mips_elf_hash_table (info);
d4596a51
RS
11565 if (!mips_elf_sort_hash_table (abfd, info))
11566 return FALSE;
b49e97c9 11567
861fb55a
DJ
11568 /* Create any scheduled LA25 stubs. */
11569 hti.info = info;
11570 hti.output_bfd = abfd;
11571 hti.error = FALSE;
11572 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11573 if (hti.error)
11574 return FALSE;
11575
b49e97c9
TS
11576 /* Get a value for the GP register. */
11577 if (elf_gp (abfd) == 0)
11578 {
11579 struct bfd_link_hash_entry *h;
11580
b34976b6 11581 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 11582 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
11583 elf_gp (abfd) = (h->u.def.value
11584 + h->u.def.section->output_section->vma
11585 + h->u.def.section->output_offset);
0a44bf69
RS
11586 else if (htab->is_vxworks
11587 && (h = bfd_link_hash_lookup (info->hash,
11588 "_GLOBAL_OFFSET_TABLE_",
11589 FALSE, FALSE, TRUE))
11590 && h->type == bfd_link_hash_defined)
11591 elf_gp (abfd) = (h->u.def.section->output_section->vma
11592 + h->u.def.section->output_offset
11593 + h->u.def.value);
1049f94e 11594 else if (info->relocatable)
b49e97c9
TS
11595 {
11596 bfd_vma lo = MINUS_ONE;
11597
11598 /* Find the GP-relative section with the lowest offset. */
9719ad41 11599 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11600 if (o->vma < lo
11601 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11602 lo = o->vma;
11603
11604 /* And calculate GP relative to that. */
0a44bf69 11605 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
11606 }
11607 else
11608 {
11609 /* If the relocate_section function needs to do a reloc
11610 involving the GP value, it should make a reloc_dangerous
11611 callback to warn that GP is not defined. */
11612 }
11613 }
11614
11615 /* Go through the sections and collect the .reginfo and .mdebug
11616 information. */
11617 reginfo_sec = NULL;
11618 mdebug_sec = NULL;
11619 gptab_data_sec = NULL;
11620 gptab_bss_sec = NULL;
9719ad41 11621 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11622 {
11623 if (strcmp (o->name, ".reginfo") == 0)
11624 {
11625 memset (&reginfo, 0, sizeof reginfo);
11626
11627 /* We have found the .reginfo section in the output file.
11628 Look through all the link_orders comprising it and merge
11629 the information together. */
8423293d 11630 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11631 {
11632 asection *input_section;
11633 bfd *input_bfd;
11634 Elf32_External_RegInfo ext;
11635 Elf32_RegInfo sub;
11636
11637 if (p->type != bfd_indirect_link_order)
11638 {
11639 if (p->type == bfd_data_link_order)
11640 continue;
11641 abort ();
11642 }
11643
11644 input_section = p->u.indirect.section;
11645 input_bfd = input_section->owner;
11646
b49e97c9 11647 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 11648 &ext, 0, sizeof ext))
b34976b6 11649 return FALSE;
b49e97c9
TS
11650
11651 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11652
11653 reginfo.ri_gprmask |= sub.ri_gprmask;
11654 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11655 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11656 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11657 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11658
11659 /* ri_gp_value is set by the function
11660 mips_elf32_section_processing when the section is
11661 finally written out. */
11662
11663 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11664 elf_link_input_bfd ignores this section. */
11665 input_section->flags &= ~SEC_HAS_CONTENTS;
11666 }
11667
11668 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 11669 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
11670
11671 /* Skip this section later on (I don't think this currently
11672 matters, but someday it might). */
8423293d 11673 o->map_head.link_order = NULL;
b49e97c9
TS
11674
11675 reginfo_sec = o;
11676 }
11677
11678 if (strcmp (o->name, ".mdebug") == 0)
11679 {
11680 struct extsym_info einfo;
11681 bfd_vma last;
11682
11683 /* We have found the .mdebug section in the output file.
11684 Look through all the link_orders comprising it and merge
11685 the information together. */
11686 symhdr->magic = swap->sym_magic;
11687 /* FIXME: What should the version stamp be? */
11688 symhdr->vstamp = 0;
11689 symhdr->ilineMax = 0;
11690 symhdr->cbLine = 0;
11691 symhdr->idnMax = 0;
11692 symhdr->ipdMax = 0;
11693 symhdr->isymMax = 0;
11694 symhdr->ioptMax = 0;
11695 symhdr->iauxMax = 0;
11696 symhdr->issMax = 0;
11697 symhdr->issExtMax = 0;
11698 symhdr->ifdMax = 0;
11699 symhdr->crfd = 0;
11700 symhdr->iextMax = 0;
11701
11702 /* We accumulate the debugging information itself in the
11703 debug_info structure. */
11704 debug.line = NULL;
11705 debug.external_dnr = NULL;
11706 debug.external_pdr = NULL;
11707 debug.external_sym = NULL;
11708 debug.external_opt = NULL;
11709 debug.external_aux = NULL;
11710 debug.ss = NULL;
11711 debug.ssext = debug.ssext_end = NULL;
11712 debug.external_fdr = NULL;
11713 debug.external_rfd = NULL;
11714 debug.external_ext = debug.external_ext_end = NULL;
11715
11716 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 11717 if (mdebug_handle == NULL)
b34976b6 11718 return FALSE;
b49e97c9
TS
11719
11720 esym.jmptbl = 0;
11721 esym.cobol_main = 0;
11722 esym.weakext = 0;
11723 esym.reserved = 0;
11724 esym.ifd = ifdNil;
11725 esym.asym.iss = issNil;
11726 esym.asym.st = stLocal;
11727 esym.asym.reserved = 0;
11728 esym.asym.index = indexNil;
11729 last = 0;
11730 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11731 {
11732 esym.asym.sc = sc[i];
11733 s = bfd_get_section_by_name (abfd, secname[i]);
11734 if (s != NULL)
11735 {
11736 esym.asym.value = s->vma;
eea6121a 11737 last = s->vma + s->size;
b49e97c9
TS
11738 }
11739 else
11740 esym.asym.value = last;
11741 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11742 secname[i], &esym))
b34976b6 11743 return FALSE;
b49e97c9
TS
11744 }
11745
8423293d 11746 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11747 {
11748 asection *input_section;
11749 bfd *input_bfd;
11750 const struct ecoff_debug_swap *input_swap;
11751 struct ecoff_debug_info input_debug;
11752 char *eraw_src;
11753 char *eraw_end;
11754
11755 if (p->type != bfd_indirect_link_order)
11756 {
11757 if (p->type == bfd_data_link_order)
11758 continue;
11759 abort ();
11760 }
11761
11762 input_section = p->u.indirect.section;
11763 input_bfd = input_section->owner;
11764
d5eaccd7 11765 if (!is_mips_elf (input_bfd))
b49e97c9
TS
11766 {
11767 /* I don't know what a non MIPS ELF bfd would be
11768 doing with a .mdebug section, but I don't really
11769 want to deal with it. */
11770 continue;
11771 }
11772
11773 input_swap = (get_elf_backend_data (input_bfd)
11774 ->elf_backend_ecoff_debug_swap);
11775
eea6121a 11776 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
11777
11778 /* The ECOFF linking code expects that we have already
11779 read in the debugging information and set up an
11780 ecoff_debug_info structure, so we do that now. */
11781 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11782 &input_debug))
b34976b6 11783 return FALSE;
b49e97c9
TS
11784
11785 if (! (bfd_ecoff_debug_accumulate
11786 (mdebug_handle, abfd, &debug, swap, input_bfd,
11787 &input_debug, input_swap, info)))
b34976b6 11788 return FALSE;
b49e97c9
TS
11789
11790 /* Loop through the external symbols. For each one with
11791 interesting information, try to find the symbol in
11792 the linker global hash table and save the information
11793 for the output external symbols. */
11794 eraw_src = input_debug.external_ext;
11795 eraw_end = (eraw_src
11796 + (input_debug.symbolic_header.iextMax
11797 * input_swap->external_ext_size));
11798 for (;
11799 eraw_src < eraw_end;
11800 eraw_src += input_swap->external_ext_size)
11801 {
11802 EXTR ext;
11803 const char *name;
11804 struct mips_elf_link_hash_entry *h;
11805
9719ad41 11806 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
11807 if (ext.asym.sc == scNil
11808 || ext.asym.sc == scUndefined
11809 || ext.asym.sc == scSUndefined)
11810 continue;
11811
11812 name = input_debug.ssext + ext.asym.iss;
11813 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 11814 name, FALSE, FALSE, TRUE);
b49e97c9
TS
11815 if (h == NULL || h->esym.ifd != -2)
11816 continue;
11817
11818 if (ext.ifd != -1)
11819 {
11820 BFD_ASSERT (ext.ifd
11821 < input_debug.symbolic_header.ifdMax);
11822 ext.ifd = input_debug.ifdmap[ext.ifd];
11823 }
11824
11825 h->esym = ext;
11826 }
11827
11828 /* Free up the information we just read. */
11829 free (input_debug.line);
11830 free (input_debug.external_dnr);
11831 free (input_debug.external_pdr);
11832 free (input_debug.external_sym);
11833 free (input_debug.external_opt);
11834 free (input_debug.external_aux);
11835 free (input_debug.ss);
11836 free (input_debug.ssext);
11837 free (input_debug.external_fdr);
11838 free (input_debug.external_rfd);
11839 free (input_debug.external_ext);
11840
11841 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11842 elf_link_input_bfd ignores this section. */
11843 input_section->flags &= ~SEC_HAS_CONTENTS;
11844 }
11845
11846 if (SGI_COMPAT (abfd) && info->shared)
11847 {
11848 /* Create .rtproc section. */
11849 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11850 if (rtproc_sec == NULL)
11851 {
11852 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11853 | SEC_LINKER_CREATED | SEC_READONLY);
11854
3496cb2a
L
11855 rtproc_sec = bfd_make_section_with_flags (abfd,
11856 ".rtproc",
11857 flags);
b49e97c9 11858 if (rtproc_sec == NULL
b49e97c9 11859 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 11860 return FALSE;
b49e97c9
TS
11861 }
11862
11863 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11864 info, rtproc_sec,
11865 &debug))
b34976b6 11866 return FALSE;
b49e97c9
TS
11867 }
11868
11869 /* Build the external symbol information. */
11870 einfo.abfd = abfd;
11871 einfo.info = info;
11872 einfo.debug = &debug;
11873 einfo.swap = swap;
b34976b6 11874 einfo.failed = FALSE;
b49e97c9 11875 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 11876 mips_elf_output_extsym, &einfo);
b49e97c9 11877 if (einfo.failed)
b34976b6 11878 return FALSE;
b49e97c9
TS
11879
11880 /* Set the size of the .mdebug section. */
eea6121a 11881 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
11882
11883 /* Skip this section later on (I don't think this currently
11884 matters, but someday it might). */
8423293d 11885 o->map_head.link_order = NULL;
b49e97c9
TS
11886
11887 mdebug_sec = o;
11888 }
11889
0112cd26 11890 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
11891 {
11892 const char *subname;
11893 unsigned int c;
11894 Elf32_gptab *tab;
11895 Elf32_External_gptab *ext_tab;
11896 unsigned int j;
11897
11898 /* The .gptab.sdata and .gptab.sbss sections hold
11899 information describing how the small data area would
11900 change depending upon the -G switch. These sections
11901 not used in executables files. */
1049f94e 11902 if (! info->relocatable)
b49e97c9 11903 {
8423293d 11904 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11905 {
11906 asection *input_section;
11907
11908 if (p->type != bfd_indirect_link_order)
11909 {
11910 if (p->type == bfd_data_link_order)
11911 continue;
11912 abort ();
11913 }
11914
11915 input_section = p->u.indirect.section;
11916
11917 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11918 elf_link_input_bfd ignores this section. */
11919 input_section->flags &= ~SEC_HAS_CONTENTS;
11920 }
11921
11922 /* Skip this section later on (I don't think this
11923 currently matters, but someday it might). */
8423293d 11924 o->map_head.link_order = NULL;
b49e97c9
TS
11925
11926 /* Really remove the section. */
5daa8fe7 11927 bfd_section_list_remove (abfd, o);
b49e97c9
TS
11928 --abfd->section_count;
11929
11930 continue;
11931 }
11932
11933 /* There is one gptab for initialized data, and one for
11934 uninitialized data. */
11935 if (strcmp (o->name, ".gptab.sdata") == 0)
11936 gptab_data_sec = o;
11937 else if (strcmp (o->name, ".gptab.sbss") == 0)
11938 gptab_bss_sec = o;
11939 else
11940 {
11941 (*_bfd_error_handler)
11942 (_("%s: illegal section name `%s'"),
11943 bfd_get_filename (abfd), o->name);
11944 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 11945 return FALSE;
b49e97c9
TS
11946 }
11947
11948 /* The linker script always combines .gptab.data and
11949 .gptab.sdata into .gptab.sdata, and likewise for
11950 .gptab.bss and .gptab.sbss. It is possible that there is
11951 no .sdata or .sbss section in the output file, in which
11952 case we must change the name of the output section. */
11953 subname = o->name + sizeof ".gptab" - 1;
11954 if (bfd_get_section_by_name (abfd, subname) == NULL)
11955 {
11956 if (o == gptab_data_sec)
11957 o->name = ".gptab.data";
11958 else
11959 o->name = ".gptab.bss";
11960 subname = o->name + sizeof ".gptab" - 1;
11961 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11962 }
11963
11964 /* Set up the first entry. */
11965 c = 1;
11966 amt = c * sizeof (Elf32_gptab);
9719ad41 11967 tab = bfd_malloc (amt);
b49e97c9 11968 if (tab == NULL)
b34976b6 11969 return FALSE;
b49e97c9
TS
11970 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11971 tab[0].gt_header.gt_unused = 0;
11972
11973 /* Combine the input sections. */
8423293d 11974 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11975 {
11976 asection *input_section;
11977 bfd *input_bfd;
11978 bfd_size_type size;
11979 unsigned long last;
11980 bfd_size_type gpentry;
11981
11982 if (p->type != bfd_indirect_link_order)
11983 {
11984 if (p->type == bfd_data_link_order)
11985 continue;
11986 abort ();
11987 }
11988
11989 input_section = p->u.indirect.section;
11990 input_bfd = input_section->owner;
11991
11992 /* Combine the gptab entries for this input section one
11993 by one. We know that the input gptab entries are
11994 sorted by ascending -G value. */
eea6121a 11995 size = input_section->size;
b49e97c9
TS
11996 last = 0;
11997 for (gpentry = sizeof (Elf32_External_gptab);
11998 gpentry < size;
11999 gpentry += sizeof (Elf32_External_gptab))
12000 {
12001 Elf32_External_gptab ext_gptab;
12002 Elf32_gptab int_gptab;
12003 unsigned long val;
12004 unsigned long add;
b34976b6 12005 bfd_boolean exact;
b49e97c9
TS
12006 unsigned int look;
12007
12008 if (! (bfd_get_section_contents
9719ad41
RS
12009 (input_bfd, input_section, &ext_gptab, gpentry,
12010 sizeof (Elf32_External_gptab))))
b49e97c9
TS
12011 {
12012 free (tab);
b34976b6 12013 return FALSE;
b49e97c9
TS
12014 }
12015
12016 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12017 &int_gptab);
12018 val = int_gptab.gt_entry.gt_g_value;
12019 add = int_gptab.gt_entry.gt_bytes - last;
12020
b34976b6 12021 exact = FALSE;
b49e97c9
TS
12022 for (look = 1; look < c; look++)
12023 {
12024 if (tab[look].gt_entry.gt_g_value >= val)
12025 tab[look].gt_entry.gt_bytes += add;
12026
12027 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 12028 exact = TRUE;
b49e97c9
TS
12029 }
12030
12031 if (! exact)
12032 {
12033 Elf32_gptab *new_tab;
12034 unsigned int max;
12035
12036 /* We need a new table entry. */
12037 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 12038 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
12039 if (new_tab == NULL)
12040 {
12041 free (tab);
b34976b6 12042 return FALSE;
b49e97c9
TS
12043 }
12044 tab = new_tab;
12045 tab[c].gt_entry.gt_g_value = val;
12046 tab[c].gt_entry.gt_bytes = add;
12047
12048 /* Merge in the size for the next smallest -G
12049 value, since that will be implied by this new
12050 value. */
12051 max = 0;
12052 for (look = 1; look < c; look++)
12053 {
12054 if (tab[look].gt_entry.gt_g_value < val
12055 && (max == 0
12056 || (tab[look].gt_entry.gt_g_value
12057 > tab[max].gt_entry.gt_g_value)))
12058 max = look;
12059 }
12060 if (max != 0)
12061 tab[c].gt_entry.gt_bytes +=
12062 tab[max].gt_entry.gt_bytes;
12063
12064 ++c;
12065 }
12066
12067 last = int_gptab.gt_entry.gt_bytes;
12068 }
12069
12070 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12071 elf_link_input_bfd ignores this section. */
12072 input_section->flags &= ~SEC_HAS_CONTENTS;
12073 }
12074
12075 /* The table must be sorted by -G value. */
12076 if (c > 2)
12077 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12078
12079 /* Swap out the table. */
12080 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 12081 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
12082 if (ext_tab == NULL)
12083 {
12084 free (tab);
b34976b6 12085 return FALSE;
b49e97c9
TS
12086 }
12087
12088 for (j = 0; j < c; j++)
12089 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12090 free (tab);
12091
eea6121a 12092 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
12093 o->contents = (bfd_byte *) ext_tab;
12094
12095 /* Skip this section later on (I don't think this currently
12096 matters, but someday it might). */
8423293d 12097 o->map_head.link_order = NULL;
b49e97c9
TS
12098 }
12099 }
12100
12101 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 12102 if (!bfd_elf_final_link (abfd, info))
b34976b6 12103 return FALSE;
b49e97c9
TS
12104
12105 /* Now write out the computed sections. */
12106
9719ad41 12107 if (reginfo_sec != NULL)
b49e97c9
TS
12108 {
12109 Elf32_External_RegInfo ext;
12110
12111 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 12112 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 12113 return FALSE;
b49e97c9
TS
12114 }
12115
9719ad41 12116 if (mdebug_sec != NULL)
b49e97c9
TS
12117 {
12118 BFD_ASSERT (abfd->output_has_begun);
12119 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12120 swap, info,
12121 mdebug_sec->filepos))
b34976b6 12122 return FALSE;
b49e97c9
TS
12123
12124 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12125 }
12126
9719ad41 12127 if (gptab_data_sec != NULL)
b49e97c9
TS
12128 {
12129 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12130 gptab_data_sec->contents,
eea6121a 12131 0, gptab_data_sec->size))
b34976b6 12132 return FALSE;
b49e97c9
TS
12133 }
12134
9719ad41 12135 if (gptab_bss_sec != NULL)
b49e97c9
TS
12136 {
12137 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12138 gptab_bss_sec->contents,
eea6121a 12139 0, gptab_bss_sec->size))
b34976b6 12140 return FALSE;
b49e97c9
TS
12141 }
12142
12143 if (SGI_COMPAT (abfd))
12144 {
12145 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12146 if (rtproc_sec != NULL)
12147 {
12148 if (! bfd_set_section_contents (abfd, rtproc_sec,
12149 rtproc_sec->contents,
eea6121a 12150 0, rtproc_sec->size))
b34976b6 12151 return FALSE;
b49e97c9
TS
12152 }
12153 }
12154
b34976b6 12155 return TRUE;
b49e97c9
TS
12156}
12157\f
64543e1a
RS
12158/* Structure for saying that BFD machine EXTENSION extends BASE. */
12159
12160struct mips_mach_extension {
12161 unsigned long extension, base;
12162};
12163
12164
12165/* An array describing how BFD machines relate to one another. The entries
12166 are ordered topologically with MIPS I extensions listed last. */
12167
12168static const struct mips_mach_extension mips_mach_extensions[] = {
6f179bd0
AN
12169 /* MIPS64r2 extensions. */
12170 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12171
64543e1a 12172 /* MIPS64 extensions. */
5f74bc13 12173 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 12174 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 12175 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
64543e1a
RS
12176
12177 /* MIPS V extensions. */
12178 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12179
12180 /* R10000 extensions. */
12181 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
12182 { bfd_mach_mips14000, bfd_mach_mips10000 },
12183 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
12184
12185 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12186 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12187 better to allow vr5400 and vr5500 code to be merged anyway, since
12188 many libraries will just use the core ISA. Perhaps we could add
12189 some sort of ASE flag if this ever proves a problem. */
12190 { bfd_mach_mips5500, bfd_mach_mips5400 },
12191 { bfd_mach_mips5400, bfd_mach_mips5000 },
12192
12193 /* MIPS IV extensions. */
12194 { bfd_mach_mips5, bfd_mach_mips8000 },
12195 { bfd_mach_mips10000, bfd_mach_mips8000 },
12196 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 12197 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 12198 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
12199
12200 /* VR4100 extensions. */
12201 { bfd_mach_mips4120, bfd_mach_mips4100 },
12202 { bfd_mach_mips4111, bfd_mach_mips4100 },
12203
12204 /* MIPS III extensions. */
350cc38d
MS
12205 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12206 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
12207 { bfd_mach_mips8000, bfd_mach_mips4000 },
12208 { bfd_mach_mips4650, bfd_mach_mips4000 },
12209 { bfd_mach_mips4600, bfd_mach_mips4000 },
12210 { bfd_mach_mips4400, bfd_mach_mips4000 },
12211 { bfd_mach_mips4300, bfd_mach_mips4000 },
12212 { bfd_mach_mips4100, bfd_mach_mips4000 },
12213 { bfd_mach_mips4010, bfd_mach_mips4000 },
12214
12215 /* MIPS32 extensions. */
12216 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12217
12218 /* MIPS II extensions. */
12219 { bfd_mach_mips4000, bfd_mach_mips6000 },
12220 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12221
12222 /* MIPS I extensions. */
12223 { bfd_mach_mips6000, bfd_mach_mips3000 },
12224 { bfd_mach_mips3900, bfd_mach_mips3000 }
12225};
12226
12227
12228/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12229
12230static bfd_boolean
9719ad41 12231mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
12232{
12233 size_t i;
12234
c5211a54
RS
12235 if (extension == base)
12236 return TRUE;
12237
12238 if (base == bfd_mach_mipsisa32
12239 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12240 return TRUE;
12241
12242 if (base == bfd_mach_mipsisa32r2
12243 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12244 return TRUE;
12245
12246 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 12247 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
12248 {
12249 extension = mips_mach_extensions[i].base;
12250 if (extension == base)
12251 return TRUE;
12252 }
64543e1a 12253
c5211a54 12254 return FALSE;
64543e1a
RS
12255}
12256
12257
12258/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 12259
b34976b6 12260static bfd_boolean
9719ad41 12261mips_32bit_flags_p (flagword flags)
00707a0e 12262{
64543e1a
RS
12263 return ((flags & EF_MIPS_32BITMODE) != 0
12264 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12265 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12266 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12267 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12268 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12269 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
12270}
12271
64543e1a 12272
2cf19d5c
JM
12273/* Merge object attributes from IBFD into OBFD. Raise an error if
12274 there are conflicting attributes. */
12275static bfd_boolean
12276mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12277{
12278 obj_attribute *in_attr;
12279 obj_attribute *out_attr;
12280
12281 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12282 {
12283 /* This is the first object. Copy the attributes. */
12284 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12285
12286 /* Use the Tag_null value to indicate the attributes have been
12287 initialized. */
12288 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12289
12290 return TRUE;
12291 }
12292
12293 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12294 non-conflicting ones. */
12295 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12296 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12297 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12298 {
12299 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12300 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12301 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12302 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12303 ;
42554f6a 12304 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12305 _bfd_error_handler
12306 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12307 in_attr[Tag_GNU_MIPS_ABI_FP].i);
42554f6a 12308 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12309 _bfd_error_handler
12310 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12311 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12312 else
12313 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12314 {
12315 case 1:
12316 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12317 {
12318 case 2:
12319 _bfd_error_handler
12320 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12321 obfd, ibfd);
51a0dd31 12322 break;
2cf19d5c
JM
12323
12324 case 3:
12325 _bfd_error_handler
12326 (_("Warning: %B uses hard float, %B uses soft float"),
12327 obfd, ibfd);
12328 break;
12329
42554f6a
TS
12330 case 4:
12331 _bfd_error_handler
12332 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12333 obfd, ibfd);
12334 break;
12335
2cf19d5c
JM
12336 default:
12337 abort ();
12338 }
12339 break;
12340
12341 case 2:
12342 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12343 {
12344 case 1:
12345 _bfd_error_handler
12346 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12347 ibfd, obfd);
51a0dd31 12348 break;
2cf19d5c
JM
12349
12350 case 3:
12351 _bfd_error_handler
12352 (_("Warning: %B uses hard float, %B uses soft float"),
12353 obfd, ibfd);
12354 break;
12355
42554f6a
TS
12356 case 4:
12357 _bfd_error_handler
12358 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12359 obfd, ibfd);
12360 break;
12361
2cf19d5c
JM
12362 default:
12363 abort ();
12364 }
12365 break;
12366
12367 case 3:
12368 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12369 {
12370 case 1:
12371 case 2:
42554f6a 12372 case 4:
2cf19d5c
JM
12373 _bfd_error_handler
12374 (_("Warning: %B uses hard float, %B uses soft float"),
12375 ibfd, obfd);
12376 break;
12377
12378 default:
12379 abort ();
12380 }
12381 break;
12382
42554f6a
TS
12383 case 4:
12384 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12385 {
12386 case 1:
12387 _bfd_error_handler
12388 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12389 ibfd, obfd);
12390 break;
12391
12392 case 2:
12393 _bfd_error_handler
12394 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12395 ibfd, obfd);
12396 break;
12397
12398 case 3:
12399 _bfd_error_handler
12400 (_("Warning: %B uses hard float, %B uses soft float"),
12401 obfd, ibfd);
12402 break;
12403
12404 default:
12405 abort ();
12406 }
12407 break;
12408
2cf19d5c
JM
12409 default:
12410 abort ();
12411 }
12412 }
12413
12414 /* Merge Tag_compatibility attributes and any common GNU ones. */
12415 _bfd_elf_merge_object_attributes (ibfd, obfd);
12416
12417 return TRUE;
12418}
12419
b49e97c9
TS
12420/* Merge backend specific data from an object file to the output
12421 object file when linking. */
12422
b34976b6 12423bfd_boolean
9719ad41 12424_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
12425{
12426 flagword old_flags;
12427 flagword new_flags;
b34976b6
AM
12428 bfd_boolean ok;
12429 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
12430 asection *sec;
12431
12432 /* Check if we have the same endianess */
82e51918 12433 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
12434 {
12435 (*_bfd_error_handler)
d003868e
AM
12436 (_("%B: endianness incompatible with that of the selected emulation"),
12437 ibfd);
aa701218
AO
12438 return FALSE;
12439 }
b49e97c9 12440
d5eaccd7 12441 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 12442 return TRUE;
b49e97c9 12443
aa701218
AO
12444 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12445 {
12446 (*_bfd_error_handler)
d003868e
AM
12447 (_("%B: ABI is incompatible with that of the selected emulation"),
12448 ibfd);
aa701218
AO
12449 return FALSE;
12450 }
12451
2cf19d5c
JM
12452 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12453 return FALSE;
12454
b49e97c9
TS
12455 new_flags = elf_elfheader (ibfd)->e_flags;
12456 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12457 old_flags = elf_elfheader (obfd)->e_flags;
12458
12459 if (! elf_flags_init (obfd))
12460 {
b34976b6 12461 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
12462 elf_elfheader (obfd)->e_flags = new_flags;
12463 elf_elfheader (obfd)->e_ident[EI_CLASS]
12464 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12465
12466 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861
TS
12467 && (bfd_get_arch_info (obfd)->the_default
12468 || mips_mach_extends_p (bfd_get_mach (obfd),
12469 bfd_get_mach (ibfd))))
b49e97c9
TS
12470 {
12471 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12472 bfd_get_mach (ibfd)))
b34976b6 12473 return FALSE;
b49e97c9
TS
12474 }
12475
b34976b6 12476 return TRUE;
b49e97c9
TS
12477 }
12478
12479 /* Check flag compatibility. */
12480
12481 new_flags &= ~EF_MIPS_NOREORDER;
12482 old_flags &= ~EF_MIPS_NOREORDER;
12483
f4416af6
AO
12484 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12485 doesn't seem to matter. */
12486 new_flags &= ~EF_MIPS_XGOT;
12487 old_flags &= ~EF_MIPS_XGOT;
12488
98a8deaf
RS
12489 /* MIPSpro generates ucode info in n64 objects. Again, we should
12490 just be able to ignore this. */
12491 new_flags &= ~EF_MIPS_UCODE;
12492 old_flags &= ~EF_MIPS_UCODE;
12493
861fb55a
DJ
12494 /* DSOs should only be linked with CPIC code. */
12495 if ((ibfd->flags & DYNAMIC) != 0)
12496 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 12497
b49e97c9 12498 if (new_flags == old_flags)
b34976b6 12499 return TRUE;
b49e97c9
TS
12500
12501 /* Check to see if the input BFD actually contains any sections.
12502 If not, its flags may not have been initialised either, but it cannot
12503 actually cause any incompatibility. */
12504 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12505 {
12506 /* Ignore synthetic sections and empty .text, .data and .bss sections
12507 which are automatically generated by gas. */
12508 if (strcmp (sec->name, ".reginfo")
12509 && strcmp (sec->name, ".mdebug")
eea6121a 12510 && (sec->size != 0
d13d89fa
NS
12511 || (strcmp (sec->name, ".text")
12512 && strcmp (sec->name, ".data")
12513 && strcmp (sec->name, ".bss"))))
b49e97c9 12514 {
b34976b6 12515 null_input_bfd = FALSE;
b49e97c9
TS
12516 break;
12517 }
12518 }
12519 if (null_input_bfd)
b34976b6 12520 return TRUE;
b49e97c9 12521
b34976b6 12522 ok = TRUE;
b49e97c9 12523
143d77c5
EC
12524 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12525 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 12526 {
b49e97c9 12527 (*_bfd_error_handler)
861fb55a 12528 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 12529 ibfd);
143d77c5 12530 ok = TRUE;
b49e97c9
TS
12531 }
12532
143d77c5
EC
12533 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12534 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12535 if (! (new_flags & EF_MIPS_PIC))
12536 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12537
12538 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12539 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 12540
64543e1a
RS
12541 /* Compare the ISAs. */
12542 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 12543 {
64543e1a 12544 (*_bfd_error_handler)
d003868e
AM
12545 (_("%B: linking 32-bit code with 64-bit code"),
12546 ibfd);
64543e1a
RS
12547 ok = FALSE;
12548 }
12549 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12550 {
12551 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12552 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 12553 {
64543e1a
RS
12554 /* Copy the architecture info from IBFD to OBFD. Also copy
12555 the 32-bit flag (if set) so that we continue to recognise
12556 OBFD as a 32-bit binary. */
12557 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12558 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12559 elf_elfheader (obfd)->e_flags
12560 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12561
12562 /* Copy across the ABI flags if OBFD doesn't use them
12563 and if that was what caused us to treat IBFD as 32-bit. */
12564 if ((old_flags & EF_MIPS_ABI) == 0
12565 && mips_32bit_flags_p (new_flags)
12566 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12567 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
12568 }
12569 else
12570 {
64543e1a 12571 /* The ISAs aren't compatible. */
b49e97c9 12572 (*_bfd_error_handler)
d003868e
AM
12573 (_("%B: linking %s module with previous %s modules"),
12574 ibfd,
64543e1a
RS
12575 bfd_printable_name (ibfd),
12576 bfd_printable_name (obfd));
b34976b6 12577 ok = FALSE;
b49e97c9 12578 }
b49e97c9
TS
12579 }
12580
64543e1a
RS
12581 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12582 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12583
12584 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
12585 does set EI_CLASS differently from any 32-bit ABI. */
12586 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12587 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12588 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12589 {
12590 /* Only error if both are set (to different values). */
12591 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12592 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12593 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12594 {
12595 (*_bfd_error_handler)
d003868e
AM
12596 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12597 ibfd,
b49e97c9
TS
12598 elf_mips_abi_name (ibfd),
12599 elf_mips_abi_name (obfd));
b34976b6 12600 ok = FALSE;
b49e97c9
TS
12601 }
12602 new_flags &= ~EF_MIPS_ABI;
12603 old_flags &= ~EF_MIPS_ABI;
12604 }
12605
fb39dac1
RS
12606 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12607 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12608 {
12609 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12610
12611 new_flags &= ~ EF_MIPS_ARCH_ASE;
12612 old_flags &= ~ EF_MIPS_ARCH_ASE;
12613 }
12614
b49e97c9
TS
12615 /* Warn about any other mismatches */
12616 if (new_flags != old_flags)
12617 {
12618 (*_bfd_error_handler)
d003868e
AM
12619 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12620 ibfd, (unsigned long) new_flags,
b49e97c9 12621 (unsigned long) old_flags);
b34976b6 12622 ok = FALSE;
b49e97c9
TS
12623 }
12624
12625 if (! ok)
12626 {
12627 bfd_set_error (bfd_error_bad_value);
b34976b6 12628 return FALSE;
b49e97c9
TS
12629 }
12630
b34976b6 12631 return TRUE;
b49e97c9
TS
12632}
12633
12634/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12635
b34976b6 12636bfd_boolean
9719ad41 12637_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
12638{
12639 BFD_ASSERT (!elf_flags_init (abfd)
12640 || elf_elfheader (abfd)->e_flags == flags);
12641
12642 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
12643 elf_flags_init (abfd) = TRUE;
12644 return TRUE;
b49e97c9
TS
12645}
12646
ad9563d6
CM
12647char *
12648_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12649{
12650 switch (dtag)
12651 {
12652 default: return "";
12653 case DT_MIPS_RLD_VERSION:
12654 return "MIPS_RLD_VERSION";
12655 case DT_MIPS_TIME_STAMP:
12656 return "MIPS_TIME_STAMP";
12657 case DT_MIPS_ICHECKSUM:
12658 return "MIPS_ICHECKSUM";
12659 case DT_MIPS_IVERSION:
12660 return "MIPS_IVERSION";
12661 case DT_MIPS_FLAGS:
12662 return "MIPS_FLAGS";
12663 case DT_MIPS_BASE_ADDRESS:
12664 return "MIPS_BASE_ADDRESS";
12665 case DT_MIPS_MSYM:
12666 return "MIPS_MSYM";
12667 case DT_MIPS_CONFLICT:
12668 return "MIPS_CONFLICT";
12669 case DT_MIPS_LIBLIST:
12670 return "MIPS_LIBLIST";
12671 case DT_MIPS_LOCAL_GOTNO:
12672 return "MIPS_LOCAL_GOTNO";
12673 case DT_MIPS_CONFLICTNO:
12674 return "MIPS_CONFLICTNO";
12675 case DT_MIPS_LIBLISTNO:
12676 return "MIPS_LIBLISTNO";
12677 case DT_MIPS_SYMTABNO:
12678 return "MIPS_SYMTABNO";
12679 case DT_MIPS_UNREFEXTNO:
12680 return "MIPS_UNREFEXTNO";
12681 case DT_MIPS_GOTSYM:
12682 return "MIPS_GOTSYM";
12683 case DT_MIPS_HIPAGENO:
12684 return "MIPS_HIPAGENO";
12685 case DT_MIPS_RLD_MAP:
12686 return "MIPS_RLD_MAP";
12687 case DT_MIPS_DELTA_CLASS:
12688 return "MIPS_DELTA_CLASS";
12689 case DT_MIPS_DELTA_CLASS_NO:
12690 return "MIPS_DELTA_CLASS_NO";
12691 case DT_MIPS_DELTA_INSTANCE:
12692 return "MIPS_DELTA_INSTANCE";
12693 case DT_MIPS_DELTA_INSTANCE_NO:
12694 return "MIPS_DELTA_INSTANCE_NO";
12695 case DT_MIPS_DELTA_RELOC:
12696 return "MIPS_DELTA_RELOC";
12697 case DT_MIPS_DELTA_RELOC_NO:
12698 return "MIPS_DELTA_RELOC_NO";
12699 case DT_MIPS_DELTA_SYM:
12700 return "MIPS_DELTA_SYM";
12701 case DT_MIPS_DELTA_SYM_NO:
12702 return "MIPS_DELTA_SYM_NO";
12703 case DT_MIPS_DELTA_CLASSSYM:
12704 return "MIPS_DELTA_CLASSSYM";
12705 case DT_MIPS_DELTA_CLASSSYM_NO:
12706 return "MIPS_DELTA_CLASSSYM_NO";
12707 case DT_MIPS_CXX_FLAGS:
12708 return "MIPS_CXX_FLAGS";
12709 case DT_MIPS_PIXIE_INIT:
12710 return "MIPS_PIXIE_INIT";
12711 case DT_MIPS_SYMBOL_LIB:
12712 return "MIPS_SYMBOL_LIB";
12713 case DT_MIPS_LOCALPAGE_GOTIDX:
12714 return "MIPS_LOCALPAGE_GOTIDX";
12715 case DT_MIPS_LOCAL_GOTIDX:
12716 return "MIPS_LOCAL_GOTIDX";
12717 case DT_MIPS_HIDDEN_GOTIDX:
12718 return "MIPS_HIDDEN_GOTIDX";
12719 case DT_MIPS_PROTECTED_GOTIDX:
12720 return "MIPS_PROTECTED_GOT_IDX";
12721 case DT_MIPS_OPTIONS:
12722 return "MIPS_OPTIONS";
12723 case DT_MIPS_INTERFACE:
12724 return "MIPS_INTERFACE";
12725 case DT_MIPS_DYNSTR_ALIGN:
12726 return "DT_MIPS_DYNSTR_ALIGN";
12727 case DT_MIPS_INTERFACE_SIZE:
12728 return "DT_MIPS_INTERFACE_SIZE";
12729 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12730 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12731 case DT_MIPS_PERF_SUFFIX:
12732 return "DT_MIPS_PERF_SUFFIX";
12733 case DT_MIPS_COMPACT_SIZE:
12734 return "DT_MIPS_COMPACT_SIZE";
12735 case DT_MIPS_GP_VALUE:
12736 return "DT_MIPS_GP_VALUE";
12737 case DT_MIPS_AUX_DYNAMIC:
12738 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
12739 case DT_MIPS_PLTGOT:
12740 return "DT_MIPS_PLTGOT";
12741 case DT_MIPS_RWPLT:
12742 return "DT_MIPS_RWPLT";
ad9563d6
CM
12743 }
12744}
12745
b34976b6 12746bfd_boolean
9719ad41 12747_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 12748{
9719ad41 12749 FILE *file = ptr;
b49e97c9
TS
12750
12751 BFD_ASSERT (abfd != NULL && ptr != NULL);
12752
12753 /* Print normal ELF private data. */
12754 _bfd_elf_print_private_bfd_data (abfd, ptr);
12755
12756 /* xgettext:c-format */
12757 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12758
12759 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12760 fprintf (file, _(" [abi=O32]"));
12761 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12762 fprintf (file, _(" [abi=O64]"));
12763 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12764 fprintf (file, _(" [abi=EABI32]"));
12765 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12766 fprintf (file, _(" [abi=EABI64]"));
12767 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12768 fprintf (file, _(" [abi unknown]"));
12769 else if (ABI_N32_P (abfd))
12770 fprintf (file, _(" [abi=N32]"));
12771 else if (ABI_64_P (abfd))
12772 fprintf (file, _(" [abi=64]"));
12773 else
12774 fprintf (file, _(" [no abi set]"));
12775
12776 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 12777 fprintf (file, " [mips1]");
b49e97c9 12778 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 12779 fprintf (file, " [mips2]");
b49e97c9 12780 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 12781 fprintf (file, " [mips3]");
b49e97c9 12782 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 12783 fprintf (file, " [mips4]");
b49e97c9 12784 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 12785 fprintf (file, " [mips5]");
b49e97c9 12786 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 12787 fprintf (file, " [mips32]");
b49e97c9 12788 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 12789 fprintf (file, " [mips64]");
af7ee8bf 12790 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 12791 fprintf (file, " [mips32r2]");
5f74bc13 12792 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 12793 fprintf (file, " [mips64r2]");
b49e97c9
TS
12794 else
12795 fprintf (file, _(" [unknown ISA]"));
12796
40d32fc6 12797 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 12798 fprintf (file, " [mdmx]");
40d32fc6
CD
12799
12800 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 12801 fprintf (file, " [mips16]");
40d32fc6 12802
b49e97c9 12803 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 12804 fprintf (file, " [32bitmode]");
b49e97c9
TS
12805 else
12806 fprintf (file, _(" [not 32bitmode]"));
12807
c0e3f241 12808 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 12809 fprintf (file, " [noreorder]");
c0e3f241
CD
12810
12811 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 12812 fprintf (file, " [PIC]");
c0e3f241
CD
12813
12814 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 12815 fprintf (file, " [CPIC]");
c0e3f241
CD
12816
12817 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 12818 fprintf (file, " [XGOT]");
c0e3f241
CD
12819
12820 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 12821 fprintf (file, " [UCODE]");
c0e3f241 12822
b49e97c9
TS
12823 fputc ('\n', file);
12824
b34976b6 12825 return TRUE;
b49e97c9 12826}
2f89ff8d 12827
b35d266b 12828const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 12829{
0112cd26
NC
12830 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12831 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12832 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12833 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12834 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12835 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12836 { NULL, 0, 0, 0, 0 }
2f89ff8d 12837};
5e2b0d47 12838
8992f0d7
TS
12839/* Merge non visibility st_other attributes. Ensure that the
12840 STO_OPTIONAL flag is copied into h->other, even if this is not a
12841 definiton of the symbol. */
5e2b0d47
NC
12842void
12843_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12844 const Elf_Internal_Sym *isym,
12845 bfd_boolean definition,
12846 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12847{
8992f0d7
TS
12848 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12849 {
12850 unsigned char other;
12851
12852 other = (definition ? isym->st_other : h->other);
12853 other &= ~ELF_ST_VISIBILITY (-1);
12854 h->other = other | ELF_ST_VISIBILITY (h->other);
12855 }
12856
12857 if (!definition
5e2b0d47
NC
12858 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12859 h->other |= STO_OPTIONAL;
12860}
12ac1cf5
NC
12861
12862/* Decide whether an undefined symbol is special and can be ignored.
12863 This is the case for OPTIONAL symbols on IRIX. */
12864bfd_boolean
12865_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12866{
12867 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12868}
e0764319
NC
12869
12870bfd_boolean
12871_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12872{
12873 return (sym->st_shndx == SHN_COMMON
12874 || sym->st_shndx == SHN_MIPS_ACOMMON
12875 || sym->st_shndx == SHN_MIPS_SCOMMON);
12876}
861fb55a
DJ
12877
12878/* Return address for Ith PLT stub in section PLT, for relocation REL
12879 or (bfd_vma) -1 if it should not be included. */
12880
12881bfd_vma
12882_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12883 const arelent *rel ATTRIBUTE_UNUSED)
12884{
12885 return (plt->vma
12886 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12887 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12888}
12889
12890void
12891_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12892{
12893 struct mips_elf_link_hash_table *htab;
12894 Elf_Internal_Ehdr *i_ehdrp;
12895
12896 i_ehdrp = elf_elfheader (abfd);
12897 if (link_info)
12898 {
12899 htab = mips_elf_hash_table (link_info);
12900 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12901 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
12902 }
12903}
This page took 1.327085 seconds and 4 git commands to generate.