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