daily update
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
64543e1a 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
aa820537 3 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
b49e97c9
TS
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
ae9a127f 12 This file is part of BFD, the Binary File Descriptor library.
b49e97c9 13
ae9a127f
NC
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
cd123cb7 16 the Free Software Foundation; either version 3 of the License, or
ae9a127f 17 (at your option) any later version.
b49e97c9 18
ae9a127f
NC
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
b49e97c9 23
ae9a127f
NC
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
cd123cb7
NC
26 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
27 MA 02110-1301, USA. */
28
b49e97c9
TS
29
30/* This file handles functionality common to the different MIPS ABI's. */
31
b49e97c9 32#include "sysdep.h"
3db64b00 33#include "bfd.h"
b49e97c9 34#include "libbfd.h"
64543e1a 35#include "libiberty.h"
b49e97c9
TS
36#include "elf-bfd.h"
37#include "elfxx-mips.h"
38#include "elf/mips.h"
0a44bf69 39#include "elf-vxworks.h"
b49e97c9
TS
40
41/* Get the ECOFF swapping routines. */
42#include "coff/sym.h"
43#include "coff/symconst.h"
44#include "coff/ecoff.h"
45#include "coff/mips.h"
46
b15e6682
AO
47#include "hashtab.h"
48
ead49a57
RS
49/* This structure is used to hold information about one GOT entry.
50 There are three types of entry:
51
52 (1) absolute addresses
53 (abfd == NULL)
54 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
55 (abfd != NULL, symndx >= 0)
56 (3) global and forced-local symbols
57 (abfd != NULL, symndx == -1)
58
59 Type (3) entries are treated differently for different types of GOT.
60 In the "master" GOT -- i.e. the one that describes every GOT
61 reference needed in the link -- the mips_got_entry is keyed on both
62 the symbol and the input bfd that references it. If it turns out
63 that we need multiple GOTs, we can then use this information to
64 create separate GOTs for each input bfd.
65
66 However, we want each of these separate GOTs to have at most one
67 entry for a given symbol, so their type (3) entries are keyed only
68 on the symbol. The input bfd given by the "abfd" field is somewhat
69 arbitrary in this case.
70
71 This means that when there are multiple GOTs, each GOT has a unique
72 mips_got_entry for every symbol within it. We can therefore use the
73 mips_got_entry fields (tls_type and gotidx) to track the symbol's
74 GOT index.
75
76 However, if it turns out that we need only a single GOT, we continue
77 to use the master GOT to describe it. There may therefore be several
78 mips_got_entries for the same symbol, each with a different input bfd.
79 We want to make sure that each symbol gets a unique GOT entry, so when
80 there's a single GOT, we use the symbol's hash entry, not the
81 mips_got_entry fields, to track a symbol's GOT index. */
b15e6682
AO
82struct mips_got_entry
83{
84 /* The input bfd in which the symbol is defined. */
85 bfd *abfd;
f4416af6
AO
86 /* The index of the symbol, as stored in the relocation r_info, if
87 we have a local symbol; -1 otherwise. */
88 long symndx;
89 union
90 {
91 /* If abfd == NULL, an address that must be stored in the got. */
92 bfd_vma address;
93 /* If abfd != NULL && symndx != -1, the addend of the relocation
94 that should be added to the symbol value. */
95 bfd_vma addend;
96 /* If abfd != NULL && symndx == -1, the hash table entry
97 corresponding to a global symbol in the got (or, local, if
98 h->forced_local). */
99 struct mips_elf_link_hash_entry *h;
100 } d;
0f20cc35
DJ
101
102 /* The TLS types included in this GOT entry (specifically, GD and
103 IE). The GD and IE flags can be added as we encounter new
104 relocations. LDM can also be set; it will always be alone, not
105 combined with any GD or IE flags. An LDM GOT entry will be
106 a local symbol entry with r_symndx == 0. */
107 unsigned char tls_type;
108
b15e6682 109 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
110 corresponding to this symbol+addend. If it's a global symbol
111 whose offset is yet to be decided, it's going to be -1. */
112 long gotidx;
b15e6682
AO
113};
114
c224138d
RS
115/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
116 The structures form a non-overlapping list that is sorted by increasing
117 MIN_ADDEND. */
118struct mips_got_page_range
119{
120 struct mips_got_page_range *next;
121 bfd_signed_vma min_addend;
122 bfd_signed_vma max_addend;
123};
124
125/* This structure describes the range of addends that are applied to page
126 relocations against a given symbol. */
127struct mips_got_page_entry
128{
129 /* The input bfd in which the symbol is defined. */
130 bfd *abfd;
131 /* The index of the symbol, as stored in the relocation r_info. */
132 long symndx;
133 /* The ranges for this page entry. */
134 struct mips_got_page_range *ranges;
135 /* The maximum number of page entries needed for RANGES. */
136 bfd_vma num_pages;
137};
138
f0abc2a1 139/* This structure is used to hold .got information when linking. */
b49e97c9
TS
140
141struct mips_got_info
142{
143 /* The global symbol in the GOT with the lowest index in the dynamic
144 symbol table. */
145 struct elf_link_hash_entry *global_gotsym;
146 /* The number of global .got entries. */
147 unsigned int global_gotno;
23cc69b6
RS
148 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
149 unsigned int reloc_only_gotno;
0f20cc35
DJ
150 /* The number of .got slots used for TLS. */
151 unsigned int tls_gotno;
152 /* The first unused TLS .got entry. Used only during
153 mips_elf_initialize_tls_index. */
154 unsigned int tls_assigned_gotno;
c224138d 155 /* The number of local .got entries, eventually including page entries. */
b49e97c9 156 unsigned int local_gotno;
c224138d
RS
157 /* The maximum number of page entries needed. */
158 unsigned int page_gotno;
b49e97c9
TS
159 /* The number of local .got entries we have used. */
160 unsigned int assigned_gotno;
b15e6682
AO
161 /* A hash table holding members of the got. */
162 struct htab *got_entries;
c224138d
RS
163 /* A hash table of mips_got_page_entry structures. */
164 struct htab *got_page_entries;
f4416af6
AO
165 /* A hash table mapping input bfds to other mips_got_info. NULL
166 unless multi-got was necessary. */
167 struct htab *bfd2got;
168 /* In multi-got links, a pointer to the next got (err, rather, most
169 of the time, it points to the previous got). */
170 struct mips_got_info *next;
0f20cc35
DJ
171 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
172 for none, or MINUS_TWO for not yet assigned. This is needed
173 because a single-GOT link may have multiple hash table entries
174 for the LDM. It does not get initialized in multi-GOT mode. */
175 bfd_vma tls_ldm_offset;
f4416af6
AO
176};
177
178/* Map an input bfd to a got in a multi-got link. */
179
91d6fa6a
NC
180struct mips_elf_bfd2got_hash
181{
f4416af6
AO
182 bfd *bfd;
183 struct mips_got_info *g;
184};
185
186/* Structure passed when traversing the bfd2got hash table, used to
187 create and merge bfd's gots. */
188
189struct mips_elf_got_per_bfd_arg
190{
191 /* A hashtable that maps bfds to gots. */
192 htab_t bfd2got;
193 /* The output bfd. */
194 bfd *obfd;
195 /* The link information. */
196 struct bfd_link_info *info;
197 /* A pointer to the primary got, i.e., the one that's going to get
198 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
199 DT_MIPS_GOTSYM. */
200 struct mips_got_info *primary;
201 /* A non-primary got we're trying to merge with other input bfd's
202 gots. */
203 struct mips_got_info *current;
204 /* The maximum number of got entries that can be addressed with a
205 16-bit offset. */
206 unsigned int max_count;
c224138d
RS
207 /* The maximum number of page entries needed by each got. */
208 unsigned int max_pages;
0f20cc35
DJ
209 /* The total number of global entries which will live in the
210 primary got and be automatically relocated. This includes
211 those not referenced by the primary GOT but included in
212 the "master" GOT. */
213 unsigned int global_count;
f4416af6
AO
214};
215
216/* Another structure used to pass arguments for got entries traversal. */
217
218struct mips_elf_set_global_got_offset_arg
219{
220 struct mips_got_info *g;
221 int value;
222 unsigned int needed_relocs;
223 struct bfd_link_info *info;
b49e97c9
TS
224};
225
0f20cc35
DJ
226/* A structure used to count TLS relocations or GOT entries, for GOT
227 entry or ELF symbol table traversal. */
228
229struct mips_elf_count_tls_arg
230{
231 struct bfd_link_info *info;
232 unsigned int needed;
233};
234
f0abc2a1
AM
235struct _mips_elf_section_data
236{
237 struct bfd_elf_section_data elf;
238 union
239 {
f0abc2a1
AM
240 bfd_byte *tdata;
241 } u;
242};
243
244#define mips_elf_section_data(sec) \
68bfbfcc 245 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 246
d5eaccd7
RS
247#define is_mips_elf(bfd) \
248 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
249 && elf_tdata (bfd) != NULL \
250 && elf_object_id (bfd) == MIPS_ELF_TDATA)
251
634835ae
RS
252/* The ABI says that every symbol used by dynamic relocations must have
253 a global GOT entry. Among other things, this provides the dynamic
254 linker with a free, directly-indexed cache. The GOT can therefore
255 contain symbols that are not referenced by GOT relocations themselves
256 (in other words, it may have symbols that are not referenced by things
257 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
258
259 GOT relocations are less likely to overflow if we put the associated
260 GOT entries towards the beginning. We therefore divide the global
261 GOT entries into two areas: "normal" and "reloc-only". Entries in
262 the first area can be used for both dynamic relocations and GP-relative
263 accesses, while those in the "reloc-only" area are for dynamic
264 relocations only.
265
266 These GGA_* ("Global GOT Area") values are organised so that lower
267 values are more general than higher values. Also, non-GGA_NONE
268 values are ordered by the position of the area in the GOT. */
269#define GGA_NORMAL 0
270#define GGA_RELOC_ONLY 1
271#define GGA_NONE 2
272
861fb55a
DJ
273/* Information about a non-PIC interface to a PIC function. There are
274 two ways of creating these interfaces. The first is to add:
275
276 lui $25,%hi(func)
277 addiu $25,$25,%lo(func)
278
279 immediately before a PIC function "func". The second is to add:
280
281 lui $25,%hi(func)
282 j func
283 addiu $25,$25,%lo(func)
284
285 to a separate trampoline section.
286
287 Stubs of the first kind go in a new section immediately before the
288 target function. Stubs of the second kind go in a single section
289 pointed to by the hash table's "strampoline" field. */
290struct mips_elf_la25_stub {
291 /* The generated section that contains this stub. */
292 asection *stub_section;
293
294 /* The offset of the stub from the start of STUB_SECTION. */
295 bfd_vma offset;
296
297 /* One symbol for the original function. Its location is available
298 in H->root.root.u.def. */
299 struct mips_elf_link_hash_entry *h;
300};
301
302/* Macros for populating a mips_elf_la25_stub. */
303
304#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
305#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
306#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
307
b49e97c9
TS
308/* This structure is passed to mips_elf_sort_hash_table_f when sorting
309 the dynamic symbols. */
310
311struct mips_elf_hash_sort_data
312{
313 /* The symbol in the global GOT with the lowest dynamic symbol table
314 index. */
315 struct elf_link_hash_entry *low;
0f20cc35
DJ
316 /* The least dynamic symbol table index corresponding to a non-TLS
317 symbol with a GOT entry. */
b49e97c9 318 long min_got_dynindx;
f4416af6
AO
319 /* The greatest dynamic symbol table index corresponding to a symbol
320 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 321 with dynamic relocations pointing to it from non-primary GOTs). */
f4416af6 322 long max_unref_got_dynindx;
b49e97c9
TS
323 /* The greatest dynamic symbol table index not corresponding to a
324 symbol without a GOT entry. */
325 long max_non_got_dynindx;
326};
327
328/* The MIPS ELF linker needs additional information for each symbol in
329 the global hash table. */
330
331struct mips_elf_link_hash_entry
332{
333 struct elf_link_hash_entry root;
334
335 /* External symbol information. */
336 EXTR esym;
337
861fb55a
DJ
338 /* The la25 stub we have created for ths symbol, if any. */
339 struct mips_elf_la25_stub *la25_stub;
340
b49e97c9
TS
341 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
342 this symbol. */
343 unsigned int possibly_dynamic_relocs;
344
b49e97c9
TS
345 /* If there is a stub that 32 bit functions should use to call this
346 16 bit function, this points to the section containing the stub. */
347 asection *fn_stub;
348
b49e97c9
TS
349 /* If there is a stub that 16 bit functions should use to call this
350 32 bit function, this points to the section containing the stub. */
351 asection *call_stub;
352
353 /* This is like the call_stub field, but it is used if the function
354 being called returns a floating point value. */
355 asection *call_fp_stub;
7c5fcef7 356
0f20cc35
DJ
357#define GOT_NORMAL 0
358#define GOT_TLS_GD 1
359#define GOT_TLS_LDM 2
360#define GOT_TLS_IE 4
361#define GOT_TLS_OFFSET_DONE 0x40
362#define GOT_TLS_DONE 0x80
363 unsigned char tls_type;
71782a75 364
0f20cc35
DJ
365 /* This is only used in single-GOT mode; in multi-GOT mode there
366 is one mips_got_entry per GOT entry, so the offset is stored
367 there. In single-GOT mode there may be many mips_got_entry
368 structures all referring to the same GOT slot. It might be
369 possible to use root.got.offset instead, but that field is
370 overloaded already. */
371 bfd_vma tls_got_offset;
71782a75 372
634835ae
RS
373 /* The highest GGA_* value that satisfies all references to this symbol. */
374 unsigned int global_got_area : 2;
375
71782a75
RS
376 /* True if one of the relocations described by possibly_dynamic_relocs
377 is against a readonly section. */
378 unsigned int readonly_reloc : 1;
379
861fb55a
DJ
380 /* True if there is a relocation against this symbol that must be
381 resolved by the static linker (in other words, if the relocation
382 cannot possibly be made dynamic). */
383 unsigned int has_static_relocs : 1;
384
71782a75
RS
385 /* True if we must not create a .MIPS.stubs entry for this symbol.
386 This is set, for example, if there are relocations related to
387 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
388 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
389 unsigned int no_fn_stub : 1;
390
391 /* Whether we need the fn_stub; this is true if this symbol appears
392 in any relocs other than a 16 bit call. */
393 unsigned int need_fn_stub : 1;
394
861fb55a
DJ
395 /* True if this symbol is referenced by branch relocations from
396 any non-PIC input file. This is used to determine whether an
397 la25 stub is required. */
398 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
399
400 /* Does this symbol need a traditional MIPS lazy-binding stub
401 (as opposed to a PLT entry)? */
402 unsigned int needs_lazy_stub : 1;
b49e97c9
TS
403};
404
405/* MIPS ELF linker hash table. */
406
407struct mips_elf_link_hash_table
408{
409 struct elf_link_hash_table root;
410#if 0
411 /* We no longer use this. */
412 /* String section indices for the dynamic section symbols. */
413 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
414#endif
861fb55a 415
b49e97c9
TS
416 /* The number of .rtproc entries. */
417 bfd_size_type procedure_count;
861fb55a 418
b49e97c9
TS
419 /* The size of the .compact_rel section (if SGI_COMPAT). */
420 bfd_size_type compact_rel_size;
861fb55a 421
b49e97c9 422 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
8dc1a139 423 entry is set to the address of __rld_obj_head as in IRIX5. */
b34976b6 424 bfd_boolean use_rld_obj_head;
861fb55a 425
b49e97c9
TS
426 /* This is the value of the __rld_map or __rld_obj_head symbol. */
427 bfd_vma rld_value;
861fb55a 428
b49e97c9 429 /* This is set if we see any mips16 stub sections. */
b34976b6 430 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
431
432 /* True if we can generate copy relocs and PLTs. */
433 bfd_boolean use_plts_and_copy_relocs;
434
0a44bf69
RS
435 /* True if we're generating code for VxWorks. */
436 bfd_boolean is_vxworks;
861fb55a 437
0e53d9da
AN
438 /* True if we already reported the small-data section overflow. */
439 bfd_boolean small_data_overflow_reported;
861fb55a 440
0a44bf69
RS
441 /* Shortcuts to some dynamic sections, or NULL if they are not
442 being used. */
443 asection *srelbss;
444 asection *sdynbss;
445 asection *srelplt;
446 asection *srelplt2;
447 asection *sgotplt;
448 asection *splt;
4e41d0d7 449 asection *sstubs;
a8028dd0 450 asection *sgot;
861fb55a 451
a8028dd0
RS
452 /* The master GOT information. */
453 struct mips_got_info *got_info;
861fb55a
DJ
454
455 /* The size of the PLT header in bytes. */
0a44bf69 456 bfd_vma plt_header_size;
861fb55a
DJ
457
458 /* The size of a PLT entry in bytes. */
0a44bf69 459 bfd_vma plt_entry_size;
861fb55a 460
33bb52fb
RS
461 /* The number of functions that need a lazy-binding stub. */
462 bfd_vma lazy_stub_count;
861fb55a 463
5108fc1b
RS
464 /* The size of a function stub entry in bytes. */
465 bfd_vma function_stub_size;
861fb55a
DJ
466
467 /* The number of reserved entries at the beginning of the GOT. */
468 unsigned int reserved_gotno;
469
470 /* The section used for mips_elf_la25_stub trampolines.
471 See the comment above that structure for details. */
472 asection *strampoline;
473
474 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
475 pairs. */
476 htab_t la25_stubs;
477
478 /* A function FN (NAME, IS, OS) that creates a new input section
479 called NAME and links it to output section OS. If IS is nonnull,
480 the new section should go immediately before it, otherwise it
481 should go at the (current) beginning of OS.
482
483 The function returns the new section on success, otherwise it
484 returns null. */
485 asection *(*add_stub_section) (const char *, asection *, asection *);
486};
487
488/* A structure used to communicate with htab_traverse callbacks. */
489struct mips_htab_traverse_info {
490 /* The usual link-wide information. */
491 struct bfd_link_info *info;
492 bfd *output_bfd;
493
494 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
495 bfd_boolean error;
b49e97c9
TS
496};
497
0f20cc35
DJ
498#define TLS_RELOC_P(r_type) \
499 (r_type == R_MIPS_TLS_DTPMOD32 \
500 || r_type == R_MIPS_TLS_DTPMOD64 \
501 || r_type == R_MIPS_TLS_DTPREL32 \
502 || r_type == R_MIPS_TLS_DTPREL64 \
503 || r_type == R_MIPS_TLS_GD \
504 || r_type == R_MIPS_TLS_LDM \
505 || r_type == R_MIPS_TLS_DTPREL_HI16 \
506 || r_type == R_MIPS_TLS_DTPREL_LO16 \
507 || r_type == R_MIPS_TLS_GOTTPREL \
508 || r_type == R_MIPS_TLS_TPREL32 \
509 || r_type == R_MIPS_TLS_TPREL64 \
510 || r_type == R_MIPS_TLS_TPREL_HI16 \
511 || r_type == R_MIPS_TLS_TPREL_LO16)
512
b49e97c9
TS
513/* Structure used to pass information to mips_elf_output_extsym. */
514
515struct extsym_info
516{
9e4aeb93
RS
517 bfd *abfd;
518 struct bfd_link_info *info;
b49e97c9
TS
519 struct ecoff_debug_info *debug;
520 const struct ecoff_debug_swap *swap;
b34976b6 521 bfd_boolean failed;
b49e97c9
TS
522};
523
8dc1a139 524/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
525
526static const char * const mips_elf_dynsym_rtproc_names[] =
527{
528 "_procedure_table",
529 "_procedure_string_table",
530 "_procedure_table_size",
531 NULL
532};
533
534/* These structures are used to generate the .compact_rel section on
8dc1a139 535 IRIX5. */
b49e97c9
TS
536
537typedef struct
538{
539 unsigned long id1; /* Always one? */
540 unsigned long num; /* Number of compact relocation entries. */
541 unsigned long id2; /* Always two? */
542 unsigned long offset; /* The file offset of the first relocation. */
543 unsigned long reserved0; /* Zero? */
544 unsigned long reserved1; /* Zero? */
545} Elf32_compact_rel;
546
547typedef struct
548{
549 bfd_byte id1[4];
550 bfd_byte num[4];
551 bfd_byte id2[4];
552 bfd_byte offset[4];
553 bfd_byte reserved0[4];
554 bfd_byte reserved1[4];
555} Elf32_External_compact_rel;
556
557typedef struct
558{
559 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
560 unsigned int rtype : 4; /* Relocation types. See below. */
561 unsigned int dist2to : 8;
562 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
563 unsigned long konst; /* KONST field. See below. */
564 unsigned long vaddr; /* VADDR to be relocated. */
565} Elf32_crinfo;
566
567typedef struct
568{
569 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
570 unsigned int rtype : 4; /* Relocation types. See below. */
571 unsigned int dist2to : 8;
572 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
573 unsigned long konst; /* KONST field. See below. */
574} Elf32_crinfo2;
575
576typedef struct
577{
578 bfd_byte info[4];
579 bfd_byte konst[4];
580 bfd_byte vaddr[4];
581} Elf32_External_crinfo;
582
583typedef struct
584{
585 bfd_byte info[4];
586 bfd_byte konst[4];
587} Elf32_External_crinfo2;
588
589/* These are the constants used to swap the bitfields in a crinfo. */
590
591#define CRINFO_CTYPE (0x1)
592#define CRINFO_CTYPE_SH (31)
593#define CRINFO_RTYPE (0xf)
594#define CRINFO_RTYPE_SH (27)
595#define CRINFO_DIST2TO (0xff)
596#define CRINFO_DIST2TO_SH (19)
597#define CRINFO_RELVADDR (0x7ffff)
598#define CRINFO_RELVADDR_SH (0)
599
600/* A compact relocation info has long (3 words) or short (2 words)
601 formats. A short format doesn't have VADDR field and relvaddr
602 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
603#define CRF_MIPS_LONG 1
604#define CRF_MIPS_SHORT 0
605
606/* There are 4 types of compact relocation at least. The value KONST
607 has different meaning for each type:
608
609 (type) (konst)
610 CT_MIPS_REL32 Address in data
611 CT_MIPS_WORD Address in word (XXX)
612 CT_MIPS_GPHI_LO GP - vaddr
613 CT_MIPS_JMPAD Address to jump
614 */
615
616#define CRT_MIPS_REL32 0xa
617#define CRT_MIPS_WORD 0xb
618#define CRT_MIPS_GPHI_LO 0xc
619#define CRT_MIPS_JMPAD 0xd
620
621#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
622#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
623#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
624#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
625\f
626/* The structure of the runtime procedure descriptor created by the
627 loader for use by the static exception system. */
628
629typedef struct runtime_pdr {
ae9a127f
NC
630 bfd_vma adr; /* Memory address of start of procedure. */
631 long regmask; /* Save register mask. */
632 long regoffset; /* Save register offset. */
633 long fregmask; /* Save floating point register mask. */
634 long fregoffset; /* Save floating point register offset. */
635 long frameoffset; /* Frame size. */
636 short framereg; /* Frame pointer register. */
637 short pcreg; /* Offset or reg of return pc. */
638 long irpss; /* Index into the runtime string table. */
b49e97c9 639 long reserved;
ae9a127f 640 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
641} RPDR, *pRPDR;
642#define cbRPDR sizeof (RPDR)
643#define rpdNil ((pRPDR) 0)
644\f
b15e6682 645static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
646 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
647 struct mips_elf_link_hash_entry *, int);
b34976b6 648static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 649 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
650static bfd_vma mips_elf_high
651 (bfd_vma);
b34976b6 652static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
653 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
654 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
655 bfd_vma *, asection *);
9719ad41
RS
656static hashval_t mips_elf_got_entry_hash
657 (const void *);
f4416af6 658static bfd_vma mips_elf_adjust_gp
9719ad41 659 (bfd *, struct mips_got_info *, bfd *);
f4416af6 660static struct mips_got_info *mips_elf_got_for_ibfd
9719ad41 661 (struct mips_got_info *, bfd *);
f4416af6 662
b49e97c9
TS
663/* This will be used when we sort the dynamic relocation records. */
664static bfd *reldyn_sorting_bfd;
665
6d30f5b2
NC
666/* True if ABFD is for CPUs with load interlocking that include
667 non-MIPS1 CPUs and R3900. */
668#define LOAD_INTERLOCKS_P(abfd) \
669 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
670 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
671
cd8d5a82
CF
672/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
673 This should be safe for all architectures. We enable this predicate
674 for RM9000 for now. */
675#define JAL_TO_BAL_P(abfd) \
676 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
677
678/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
679 This should be safe for all architectures. We enable this predicate for
680 all CPUs. */
681#define JALR_TO_BAL_P(abfd) 1
682
38a7df63
CF
683/* True if ABFD is for CPUs that are faster if JR is converted to B.
684 This should be safe for all architectures. We enable this predicate for
685 all CPUs. */
686#define JR_TO_B_P(abfd) 1
687
861fb55a
DJ
688/* True if ABFD is a PIC object. */
689#define PIC_OBJECT_P(abfd) \
690 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
691
b49e97c9 692/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
693#define ABI_N32_P(abfd) \
694 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
695
4a14403c 696/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 697#define ABI_64_P(abfd) \
141ff970 698 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 699
4a14403c
TS
700/* Nonzero if ABFD is using NewABI conventions. */
701#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
702
703/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
704#define IRIX_COMPAT(abfd) \
705 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
706
b49e97c9
TS
707/* Whether we are trying to be compatible with IRIX at all. */
708#define SGI_COMPAT(abfd) \
709 (IRIX_COMPAT (abfd) != ict_none)
710
711/* The name of the options section. */
712#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 713 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 714
cc2e31b9
RS
715/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
716 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
717#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
718 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
719
943284cc
DJ
720/* Whether the section is readonly. */
721#define MIPS_ELF_READONLY_SECTION(sec) \
722 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
723 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
724
b49e97c9 725/* The name of the stub section. */
ca07892d 726#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
727
728/* The size of an external REL relocation. */
729#define MIPS_ELF_REL_SIZE(abfd) \
730 (get_elf_backend_data (abfd)->s->sizeof_rel)
731
0a44bf69
RS
732/* The size of an external RELA relocation. */
733#define MIPS_ELF_RELA_SIZE(abfd) \
734 (get_elf_backend_data (abfd)->s->sizeof_rela)
735
b49e97c9
TS
736/* The size of an external dynamic table entry. */
737#define MIPS_ELF_DYN_SIZE(abfd) \
738 (get_elf_backend_data (abfd)->s->sizeof_dyn)
739
740/* The size of a GOT entry. */
741#define MIPS_ELF_GOT_SIZE(abfd) \
742 (get_elf_backend_data (abfd)->s->arch_size / 8)
743
744/* The size of a symbol-table entry. */
745#define MIPS_ELF_SYM_SIZE(abfd) \
746 (get_elf_backend_data (abfd)->s->sizeof_sym)
747
748/* The default alignment for sections, as a power of two. */
749#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 750 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
751
752/* Get word-sized data. */
753#define MIPS_ELF_GET_WORD(abfd, ptr) \
754 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
755
756/* Put out word-sized data. */
757#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
758 (ABI_64_P (abfd) \
759 ? bfd_put_64 (abfd, val, ptr) \
760 : bfd_put_32 (abfd, val, ptr))
761
861fb55a
DJ
762/* The opcode for word-sized loads (LW or LD). */
763#define MIPS_ELF_LOAD_WORD(abfd) \
764 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
765
b49e97c9 766/* Add a dynamic symbol table-entry. */
9719ad41 767#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 768 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
769
770#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
771 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
772
4ffba85c
AO
773/* Determine whether the internal relocation of index REL_IDX is REL
774 (zero) or RELA (non-zero). The assumption is that, if there are
775 two relocation sections for this section, one of them is REL and
776 the other is RELA. If the index of the relocation we're testing is
777 in range for the first relocation section, check that the external
778 relocation size is that for RELA. It is also assumed that, if
779 rel_idx is not in range for the first section, and this first
780 section contains REL relocs, then the relocation is in the second
781 section, that is RELA. */
782#define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
783 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
784 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
785 > (bfd_vma)(rel_idx)) \
786 == (elf_section_data (sec)->rel_hdr.sh_entsize \
787 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
788 : sizeof (Elf32_External_Rela))))
789
0a44bf69
RS
790/* The name of the dynamic relocation section. */
791#define MIPS_ELF_REL_DYN_NAME(INFO) \
792 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
793
b49e97c9
TS
794/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
795 from smaller values. Start with zero, widen, *then* decrement. */
796#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 797#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 798
51e38d68
RS
799/* The value to write into got[1] for SVR4 targets, to identify it is
800 a GNU object. The dynamic linker can then use got[1] to store the
801 module pointer. */
802#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
803 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
804
f4416af6 805/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
806#define ELF_MIPS_GP_OFFSET(INFO) \
807 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
808
809/* The maximum size of the GOT for it to be addressable using 16-bit
810 offsets from $gp. */
0a44bf69 811#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 812
6a691779 813/* Instructions which appear in a stub. */
3d6746ca
DD
814#define STUB_LW(abfd) \
815 ((ABI_64_P (abfd) \
816 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
817 : 0x8f998010)) /* lw t9,0x8010(gp) */
818#define STUB_MOVE(abfd) \
819 ((ABI_64_P (abfd) \
820 ? 0x03e0782d /* daddu t7,ra */ \
821 : 0x03e07821)) /* addu t7,ra */
822#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
823#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
824#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
825#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
826#define STUB_LI16S(abfd, VAL) \
827 ((ABI_64_P (abfd) \
828 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
829 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
830
5108fc1b
RS
831#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
832#define MIPS_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
833
834/* The name of the dynamic interpreter. This is put in the .interp
835 section. */
836
837#define ELF_DYNAMIC_INTERPRETER(abfd) \
838 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
839 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
840 : "/usr/lib/libc.so.1")
841
842#ifdef BFD64
ee6423ed
AO
843#define MNAME(bfd,pre,pos) \
844 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
845#define ELF_R_SYM(bfd, i) \
846 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
847#define ELF_R_TYPE(bfd, i) \
848 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
849#define ELF_R_INFO(bfd, s, t) \
850 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
851#else
ee6423ed 852#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
853#define ELF_R_SYM(bfd, i) \
854 (ELF32_R_SYM (i))
855#define ELF_R_TYPE(bfd, i) \
856 (ELF32_R_TYPE (i))
857#define ELF_R_INFO(bfd, s, t) \
858 (ELF32_R_INFO (s, t))
859#endif
860\f
861 /* The mips16 compiler uses a couple of special sections to handle
862 floating point arguments.
863
864 Section names that look like .mips16.fn.FNNAME contain stubs that
865 copy floating point arguments from the fp regs to the gp regs and
866 then jump to FNNAME. If any 32 bit function calls FNNAME, the
867 call should be redirected to the stub instead. If no 32 bit
868 function calls FNNAME, the stub should be discarded. We need to
869 consider any reference to the function, not just a call, because
870 if the address of the function is taken we will need the stub,
871 since the address might be passed to a 32 bit function.
872
873 Section names that look like .mips16.call.FNNAME contain stubs
874 that copy floating point arguments from the gp regs to the fp
875 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
876 then any 16 bit function that calls FNNAME should be redirected
877 to the stub instead. If FNNAME is not a 32 bit function, the
878 stub should be discarded.
879
880 .mips16.call.fp.FNNAME sections are similar, but contain stubs
881 which call FNNAME and then copy the return value from the fp regs
882 to the gp regs. These stubs store the return value in $18 while
883 calling FNNAME; any function which might call one of these stubs
884 must arrange to save $18 around the call. (This case is not
885 needed for 32 bit functions that call 16 bit functions, because
886 16 bit functions always return floating point values in both
887 $f0/$f1 and $2/$3.)
888
889 Note that in all cases FNNAME might be defined statically.
890 Therefore, FNNAME is not used literally. Instead, the relocation
891 information will indicate which symbol the section is for.
892
893 We record any stubs that we find in the symbol table. */
894
895#define FN_STUB ".mips16.fn."
896#define CALL_STUB ".mips16.call."
897#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
898
899#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
900#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
901#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 902\f
861fb55a 903/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
904static const bfd_vma mips_o32_exec_plt0_entry[] =
905{
861fb55a
DJ
906 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
907 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
908 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
909 0x031cc023, /* subu $24, $24, $28 */
910 0x03e07821, /* move $15, $31 */
911 0x0018c082, /* srl $24, $24, 2 */
912 0x0320f809, /* jalr $25 */
913 0x2718fffe /* subu $24, $24, 2 */
914};
915
916/* The format of the first PLT entry in an N32 executable. Different
917 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
918static const bfd_vma mips_n32_exec_plt0_entry[] =
919{
861fb55a
DJ
920 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
921 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
922 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
923 0x030ec023, /* subu $24, $24, $14 */
924 0x03e07821, /* move $15, $31 */
925 0x0018c082, /* srl $24, $24, 2 */
926 0x0320f809, /* jalr $25 */
927 0x2718fffe /* subu $24, $24, 2 */
928};
929
930/* The format of the first PLT entry in an N64 executable. Different
931 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
932static const bfd_vma mips_n64_exec_plt0_entry[] =
933{
861fb55a
DJ
934 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
935 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
936 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
937 0x030ec023, /* subu $24, $24, $14 */
938 0x03e07821, /* move $15, $31 */
939 0x0018c0c2, /* srl $24, $24, 3 */
940 0x0320f809, /* jalr $25 */
941 0x2718fffe /* subu $24, $24, 2 */
942};
943
944/* The format of subsequent PLT entries. */
6d30f5b2
NC
945static const bfd_vma mips_exec_plt_entry[] =
946{
861fb55a
DJ
947 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
948 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
949 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
950 0x03200008 /* jr $25 */
951};
952
0a44bf69 953/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
954static const bfd_vma mips_vxworks_exec_plt0_entry[] =
955{
0a44bf69
RS
956 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
957 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
958 0x8f390008, /* lw t9, 8(t9) */
959 0x00000000, /* nop */
960 0x03200008, /* jr t9 */
961 0x00000000 /* nop */
962};
963
964/* The format of subsequent PLT entries. */
6d30f5b2
NC
965static const bfd_vma mips_vxworks_exec_plt_entry[] =
966{
0a44bf69
RS
967 0x10000000, /* b .PLT_resolver */
968 0x24180000, /* li t8, <pltindex> */
969 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
970 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
971 0x8f390000, /* lw t9, 0(t9) */
972 0x00000000, /* nop */
973 0x03200008, /* jr t9 */
974 0x00000000 /* nop */
975};
976
977/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
978static const bfd_vma mips_vxworks_shared_plt0_entry[] =
979{
0a44bf69
RS
980 0x8f990008, /* lw t9, 8(gp) */
981 0x00000000, /* nop */
982 0x03200008, /* jr t9 */
983 0x00000000, /* nop */
984 0x00000000, /* nop */
985 0x00000000 /* nop */
986};
987
988/* The format of subsequent PLT entries. */
6d30f5b2
NC
989static const bfd_vma mips_vxworks_shared_plt_entry[] =
990{
0a44bf69
RS
991 0x10000000, /* b .PLT_resolver */
992 0x24180000 /* li t8, <pltindex> */
993};
994\f
b49e97c9
TS
995/* Look up an entry in a MIPS ELF linker hash table. */
996
997#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
998 ((struct mips_elf_link_hash_entry *) \
999 elf_link_hash_lookup (&(table)->root, (string), (create), \
1000 (copy), (follow)))
1001
1002/* Traverse a MIPS ELF linker hash table. */
1003
1004#define mips_elf_link_hash_traverse(table, func, info) \
1005 (elf_link_hash_traverse \
1006 (&(table)->root, \
9719ad41 1007 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1008 (info)))
1009
1010/* Get the MIPS ELF linker hash table from a link_info structure. */
1011
1012#define mips_elf_hash_table(p) \
1013 ((struct mips_elf_link_hash_table *) ((p)->hash))
1014
0f20cc35
DJ
1015/* Find the base offsets for thread-local storage in this object,
1016 for GD/LD and IE/LE respectively. */
1017
1018#define TP_OFFSET 0x7000
1019#define DTP_OFFSET 0x8000
1020
1021static bfd_vma
1022dtprel_base (struct bfd_link_info *info)
1023{
1024 /* If tls_sec is NULL, we should have signalled an error already. */
1025 if (elf_hash_table (info)->tls_sec == NULL)
1026 return 0;
1027 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1028}
1029
1030static bfd_vma
1031tprel_base (struct bfd_link_info *info)
1032{
1033 /* If tls_sec is NULL, we should have signalled an error already. */
1034 if (elf_hash_table (info)->tls_sec == NULL)
1035 return 0;
1036 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1037}
1038
b49e97c9
TS
1039/* Create an entry in a MIPS ELF linker hash table. */
1040
1041static struct bfd_hash_entry *
9719ad41
RS
1042mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1043 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1044{
1045 struct mips_elf_link_hash_entry *ret =
1046 (struct mips_elf_link_hash_entry *) entry;
1047
1048 /* Allocate the structure if it has not already been allocated by a
1049 subclass. */
9719ad41
RS
1050 if (ret == NULL)
1051 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1052 if (ret == NULL)
b49e97c9
TS
1053 return (struct bfd_hash_entry *) ret;
1054
1055 /* Call the allocation method of the superclass. */
1056 ret = ((struct mips_elf_link_hash_entry *)
1057 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1058 table, string));
9719ad41 1059 if (ret != NULL)
b49e97c9
TS
1060 {
1061 /* Set local fields. */
1062 memset (&ret->esym, 0, sizeof (EXTR));
1063 /* We use -2 as a marker to indicate that the information has
1064 not been set. -1 means there is no associated ifd. */
1065 ret->esym.ifd = -2;
861fb55a 1066 ret->la25_stub = 0;
b49e97c9 1067 ret->possibly_dynamic_relocs = 0;
b49e97c9 1068 ret->fn_stub = NULL;
b49e97c9
TS
1069 ret->call_stub = NULL;
1070 ret->call_fp_stub = NULL;
71782a75 1071 ret->tls_type = GOT_NORMAL;
634835ae 1072 ret->global_got_area = GGA_NONE;
71782a75 1073 ret->readonly_reloc = FALSE;
861fb55a 1074 ret->has_static_relocs = FALSE;
71782a75
RS
1075 ret->no_fn_stub = FALSE;
1076 ret->need_fn_stub = FALSE;
861fb55a 1077 ret->has_nonpic_branches = FALSE;
33bb52fb 1078 ret->needs_lazy_stub = FALSE;
b49e97c9
TS
1079 }
1080
1081 return (struct bfd_hash_entry *) ret;
1082}
f0abc2a1
AM
1083
1084bfd_boolean
9719ad41 1085_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1086{
f592407e
AM
1087 if (!sec->used_by_bfd)
1088 {
1089 struct _mips_elf_section_data *sdata;
1090 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1091
f592407e
AM
1092 sdata = bfd_zalloc (abfd, amt);
1093 if (sdata == NULL)
1094 return FALSE;
1095 sec->used_by_bfd = sdata;
1096 }
f0abc2a1
AM
1097
1098 return _bfd_elf_new_section_hook (abfd, sec);
1099}
b49e97c9
TS
1100\f
1101/* Read ECOFF debugging information from a .mdebug section into a
1102 ecoff_debug_info structure. */
1103
b34976b6 1104bfd_boolean
9719ad41
RS
1105_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1106 struct ecoff_debug_info *debug)
b49e97c9
TS
1107{
1108 HDRR *symhdr;
1109 const struct ecoff_debug_swap *swap;
9719ad41 1110 char *ext_hdr;
b49e97c9
TS
1111
1112 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1113 memset (debug, 0, sizeof (*debug));
1114
9719ad41 1115 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1116 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1117 goto error_return;
1118
9719ad41 1119 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1120 swap->external_hdr_size))
b49e97c9
TS
1121 goto error_return;
1122
1123 symhdr = &debug->symbolic_header;
1124 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1125
1126 /* The symbolic header contains absolute file offsets and sizes to
1127 read. */
1128#define READ(ptr, offset, count, size, type) \
1129 if (symhdr->count == 0) \
1130 debug->ptr = NULL; \
1131 else \
1132 { \
1133 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1134 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1135 if (debug->ptr == NULL) \
1136 goto error_return; \
9719ad41 1137 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1138 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1139 goto error_return; \
1140 }
1141
1142 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1143 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1144 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1145 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1146 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1147 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1148 union aux_ext *);
1149 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1150 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1151 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1152 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1153 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1154#undef READ
1155
1156 debug->fdr = NULL;
b49e97c9 1157
b34976b6 1158 return TRUE;
b49e97c9
TS
1159
1160 error_return:
1161 if (ext_hdr != NULL)
1162 free (ext_hdr);
1163 if (debug->line != NULL)
1164 free (debug->line);
1165 if (debug->external_dnr != NULL)
1166 free (debug->external_dnr);
1167 if (debug->external_pdr != NULL)
1168 free (debug->external_pdr);
1169 if (debug->external_sym != NULL)
1170 free (debug->external_sym);
1171 if (debug->external_opt != NULL)
1172 free (debug->external_opt);
1173 if (debug->external_aux != NULL)
1174 free (debug->external_aux);
1175 if (debug->ss != NULL)
1176 free (debug->ss);
1177 if (debug->ssext != NULL)
1178 free (debug->ssext);
1179 if (debug->external_fdr != NULL)
1180 free (debug->external_fdr);
1181 if (debug->external_rfd != NULL)
1182 free (debug->external_rfd);
1183 if (debug->external_ext != NULL)
1184 free (debug->external_ext);
b34976b6 1185 return FALSE;
b49e97c9
TS
1186}
1187\f
1188/* Swap RPDR (runtime procedure table entry) for output. */
1189
1190static void
9719ad41 1191ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1192{
1193 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1194 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1195 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1196 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1197 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1198 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1199
1200 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1201 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1202
1203 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1204}
1205
1206/* Create a runtime procedure table from the .mdebug section. */
1207
b34976b6 1208static bfd_boolean
9719ad41
RS
1209mips_elf_create_procedure_table (void *handle, bfd *abfd,
1210 struct bfd_link_info *info, asection *s,
1211 struct ecoff_debug_info *debug)
b49e97c9
TS
1212{
1213 const struct ecoff_debug_swap *swap;
1214 HDRR *hdr = &debug->symbolic_header;
1215 RPDR *rpdr, *rp;
1216 struct rpdr_ext *erp;
9719ad41 1217 void *rtproc;
b49e97c9
TS
1218 struct pdr_ext *epdr;
1219 struct sym_ext *esym;
1220 char *ss, **sv;
1221 char *str;
1222 bfd_size_type size;
1223 bfd_size_type count;
1224 unsigned long sindex;
1225 unsigned long i;
1226 PDR pdr;
1227 SYMR sym;
1228 const char *no_name_func = _("static procedure (no name)");
1229
1230 epdr = NULL;
1231 rpdr = NULL;
1232 esym = NULL;
1233 ss = NULL;
1234 sv = NULL;
1235
1236 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1237
1238 sindex = strlen (no_name_func) + 1;
1239 count = hdr->ipdMax;
1240 if (count > 0)
1241 {
1242 size = swap->external_pdr_size;
1243
9719ad41 1244 epdr = bfd_malloc (size * count);
b49e97c9
TS
1245 if (epdr == NULL)
1246 goto error_return;
1247
9719ad41 1248 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1249 goto error_return;
1250
1251 size = sizeof (RPDR);
9719ad41 1252 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1253 if (rpdr == NULL)
1254 goto error_return;
1255
1256 size = sizeof (char *);
9719ad41 1257 sv = bfd_malloc (size * count);
b49e97c9
TS
1258 if (sv == NULL)
1259 goto error_return;
1260
1261 count = hdr->isymMax;
1262 size = swap->external_sym_size;
9719ad41 1263 esym = bfd_malloc (size * count);
b49e97c9
TS
1264 if (esym == NULL)
1265 goto error_return;
1266
9719ad41 1267 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1268 goto error_return;
1269
1270 count = hdr->issMax;
9719ad41 1271 ss = bfd_malloc (count);
b49e97c9
TS
1272 if (ss == NULL)
1273 goto error_return;
f075ee0c 1274 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1275 goto error_return;
1276
1277 count = hdr->ipdMax;
1278 for (i = 0; i < (unsigned long) count; i++, rp++)
1279 {
9719ad41
RS
1280 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1281 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1282 rp->adr = sym.value;
1283 rp->regmask = pdr.regmask;
1284 rp->regoffset = pdr.regoffset;
1285 rp->fregmask = pdr.fregmask;
1286 rp->fregoffset = pdr.fregoffset;
1287 rp->frameoffset = pdr.frameoffset;
1288 rp->framereg = pdr.framereg;
1289 rp->pcreg = pdr.pcreg;
1290 rp->irpss = sindex;
1291 sv[i] = ss + sym.iss;
1292 sindex += strlen (sv[i]) + 1;
1293 }
1294 }
1295
1296 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1297 size = BFD_ALIGN (size, 16);
9719ad41 1298 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1299 if (rtproc == NULL)
1300 {
1301 mips_elf_hash_table (info)->procedure_count = 0;
1302 goto error_return;
1303 }
1304
1305 mips_elf_hash_table (info)->procedure_count = count + 2;
1306
9719ad41 1307 erp = rtproc;
b49e97c9
TS
1308 memset (erp, 0, sizeof (struct rpdr_ext));
1309 erp++;
1310 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1311 strcpy (str, no_name_func);
1312 str += strlen (no_name_func) + 1;
1313 for (i = 0; i < count; i++)
1314 {
1315 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1316 strcpy (str, sv[i]);
1317 str += strlen (sv[i]) + 1;
1318 }
1319 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1320
1321 /* Set the size and contents of .rtproc section. */
eea6121a 1322 s->size = size;
9719ad41 1323 s->contents = rtproc;
b49e97c9
TS
1324
1325 /* Skip this section later on (I don't think this currently
1326 matters, but someday it might). */
8423293d 1327 s->map_head.link_order = NULL;
b49e97c9
TS
1328
1329 if (epdr != NULL)
1330 free (epdr);
1331 if (rpdr != NULL)
1332 free (rpdr);
1333 if (esym != NULL)
1334 free (esym);
1335 if (ss != NULL)
1336 free (ss);
1337 if (sv != NULL)
1338 free (sv);
1339
b34976b6 1340 return TRUE;
b49e97c9
TS
1341
1342 error_return:
1343 if (epdr != NULL)
1344 free (epdr);
1345 if (rpdr != NULL)
1346 free (rpdr);
1347 if (esym != NULL)
1348 free (esym);
1349 if (ss != NULL)
1350 free (ss);
1351 if (sv != NULL)
1352 free (sv);
b34976b6 1353 return FALSE;
b49e97c9 1354}
738e5348 1355\f
861fb55a
DJ
1356/* We're going to create a stub for H. Create a symbol for the stub's
1357 value and size, to help make the disassembly easier to read. */
1358
1359static bfd_boolean
1360mips_elf_create_stub_symbol (struct bfd_link_info *info,
1361 struct mips_elf_link_hash_entry *h,
1362 const char *prefix, asection *s, bfd_vma value,
1363 bfd_vma size)
1364{
1365 struct bfd_link_hash_entry *bh;
1366 struct elf_link_hash_entry *elfh;
1367 const char *name;
1368
1369 /* Create a new symbol. */
1370 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1371 bh = NULL;
1372 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1373 BSF_LOCAL, s, value, NULL,
1374 TRUE, FALSE, &bh))
1375 return FALSE;
1376
1377 /* Make it a local function. */
1378 elfh = (struct elf_link_hash_entry *) bh;
1379 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1380 elfh->size = size;
1381 elfh->forced_local = 1;
1382 return TRUE;
1383}
1384
738e5348
RS
1385/* We're about to redefine H. Create a symbol to represent H's
1386 current value and size, to help make the disassembly easier
1387 to read. */
1388
1389static bfd_boolean
1390mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1391 struct mips_elf_link_hash_entry *h,
1392 const char *prefix)
1393{
1394 struct bfd_link_hash_entry *bh;
1395 struct elf_link_hash_entry *elfh;
1396 const char *name;
1397 asection *s;
1398 bfd_vma value;
1399
1400 /* Read the symbol's value. */
1401 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1402 || h->root.root.type == bfd_link_hash_defweak);
1403 s = h->root.root.u.def.section;
1404 value = h->root.root.u.def.value;
1405
1406 /* Create a new symbol. */
1407 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1408 bh = NULL;
1409 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1410 BSF_LOCAL, s, value, NULL,
1411 TRUE, FALSE, &bh))
1412 return FALSE;
1413
1414 /* Make it local and copy the other attributes from H. */
1415 elfh = (struct elf_link_hash_entry *) bh;
1416 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1417 elfh->other = h->root.other;
1418 elfh->size = h->root.size;
1419 elfh->forced_local = 1;
1420 return TRUE;
1421}
1422
1423/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1424 function rather than to a hard-float stub. */
1425
1426static bfd_boolean
1427section_allows_mips16_refs_p (asection *section)
1428{
1429 const char *name;
1430
1431 name = bfd_get_section_name (section->owner, section);
1432 return (FN_STUB_P (name)
1433 || CALL_STUB_P (name)
1434 || CALL_FP_STUB_P (name)
1435 || strcmp (name, ".pdr") == 0);
1436}
1437
1438/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1439 stub section of some kind. Return the R_SYMNDX of the target
1440 function, or 0 if we can't decide which function that is. */
1441
1442static unsigned long
502e814e
TT
1443mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1444 const Elf_Internal_Rela *relocs,
738e5348
RS
1445 const Elf_Internal_Rela *relend)
1446{
1447 const Elf_Internal_Rela *rel;
1448
1449 /* Trust the first R_MIPS_NONE relocation, if any. */
1450 for (rel = relocs; rel < relend; rel++)
1451 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1452 return ELF_R_SYM (sec->owner, rel->r_info);
1453
1454 /* Otherwise trust the first relocation, whatever its kind. This is
1455 the traditional behavior. */
1456 if (relocs < relend)
1457 return ELF_R_SYM (sec->owner, relocs->r_info);
1458
1459 return 0;
1460}
b49e97c9
TS
1461
1462/* Check the mips16 stubs for a particular symbol, and see if we can
1463 discard them. */
1464
861fb55a
DJ
1465static void
1466mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1467 struct mips_elf_link_hash_entry *h)
b49e97c9 1468{
738e5348
RS
1469 /* Dynamic symbols must use the standard call interface, in case other
1470 objects try to call them. */
1471 if (h->fn_stub != NULL
1472 && h->root.dynindx != -1)
1473 {
1474 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1475 h->need_fn_stub = TRUE;
1476 }
1477
b49e97c9
TS
1478 if (h->fn_stub != NULL
1479 && ! h->need_fn_stub)
1480 {
1481 /* We don't need the fn_stub; the only references to this symbol
1482 are 16 bit calls. Clobber the size to 0 to prevent it from
1483 being included in the link. */
eea6121a 1484 h->fn_stub->size = 0;
b49e97c9
TS
1485 h->fn_stub->flags &= ~SEC_RELOC;
1486 h->fn_stub->reloc_count = 0;
1487 h->fn_stub->flags |= SEC_EXCLUDE;
1488 }
1489
1490 if (h->call_stub != NULL
30c09090 1491 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1492 {
1493 /* We don't need the call_stub; this is a 16 bit function, so
1494 calls from other 16 bit functions are OK. Clobber the size
1495 to 0 to prevent it from being included in the link. */
eea6121a 1496 h->call_stub->size = 0;
b49e97c9
TS
1497 h->call_stub->flags &= ~SEC_RELOC;
1498 h->call_stub->reloc_count = 0;
1499 h->call_stub->flags |= SEC_EXCLUDE;
1500 }
1501
1502 if (h->call_fp_stub != NULL
30c09090 1503 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1504 {
1505 /* We don't need the call_stub; this is a 16 bit function, so
1506 calls from other 16 bit functions are OK. Clobber the size
1507 to 0 to prevent it from being included in the link. */
eea6121a 1508 h->call_fp_stub->size = 0;
b49e97c9
TS
1509 h->call_fp_stub->flags &= ~SEC_RELOC;
1510 h->call_fp_stub->reloc_count = 0;
1511 h->call_fp_stub->flags |= SEC_EXCLUDE;
1512 }
861fb55a
DJ
1513}
1514
1515/* Hashtable callbacks for mips_elf_la25_stubs. */
1516
1517static hashval_t
1518mips_elf_la25_stub_hash (const void *entry_)
1519{
1520 const struct mips_elf_la25_stub *entry;
1521
1522 entry = (struct mips_elf_la25_stub *) entry_;
1523 return entry->h->root.root.u.def.section->id
1524 + entry->h->root.root.u.def.value;
1525}
1526
1527static int
1528mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1529{
1530 const struct mips_elf_la25_stub *entry1, *entry2;
1531
1532 entry1 = (struct mips_elf_la25_stub *) entry1_;
1533 entry2 = (struct mips_elf_la25_stub *) entry2_;
1534 return ((entry1->h->root.root.u.def.section
1535 == entry2->h->root.root.u.def.section)
1536 && (entry1->h->root.root.u.def.value
1537 == entry2->h->root.root.u.def.value));
1538}
1539
1540/* Called by the linker to set up the la25 stub-creation code. FN is
1541 the linker's implementation of add_stub_function. Return true on
1542 success. */
1543
1544bfd_boolean
1545_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1546 asection *(*fn) (const char *, asection *,
1547 asection *))
1548{
1549 struct mips_elf_link_hash_table *htab;
1550
1551 htab = mips_elf_hash_table (info);
1552 htab->add_stub_section = fn;
1553 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1554 mips_elf_la25_stub_eq, NULL);
1555 if (htab->la25_stubs == NULL)
1556 return FALSE;
1557
1558 return TRUE;
1559}
1560
1561/* Return true if H is a locally-defined PIC function, in the sense
1562 that it might need $25 to be valid on entry. Note that MIPS16
1563 functions never need $25 to be valid on entry; they set up $gp
1564 using PC-relative instructions instead. */
1565
1566static bfd_boolean
1567mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1568{
1569 return ((h->root.root.type == bfd_link_hash_defined
1570 || h->root.root.type == bfd_link_hash_defweak)
1571 && h->root.def_regular
1572 && !bfd_is_abs_section (h->root.root.u.def.section)
1573 && !ELF_ST_IS_MIPS16 (h->root.other)
1574 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1575 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1576}
1577
1578/* STUB describes an la25 stub that we have decided to implement
1579 by inserting an LUI/ADDIU pair before the target function.
1580 Create the section and redirect the function symbol to it. */
1581
1582static bfd_boolean
1583mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1584 struct bfd_link_info *info)
1585{
1586 struct mips_elf_link_hash_table *htab;
1587 char *name;
1588 asection *s, *input_section;
1589 unsigned int align;
1590
1591 htab = mips_elf_hash_table (info);
1592
1593 /* Create a unique name for the new section. */
1594 name = bfd_malloc (11 + sizeof (".text.stub."));
1595 if (name == NULL)
1596 return FALSE;
1597 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1598
1599 /* Create the section. */
1600 input_section = stub->h->root.root.u.def.section;
1601 s = htab->add_stub_section (name, input_section,
1602 input_section->output_section);
1603 if (s == NULL)
1604 return FALSE;
1605
1606 /* Make sure that any padding goes before the stub. */
1607 align = input_section->alignment_power;
1608 if (!bfd_set_section_alignment (s->owner, s, align))
1609 return FALSE;
1610 if (align > 3)
1611 s->size = (1 << align) - 8;
1612
1613 /* Create a symbol for the stub. */
1614 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1615 stub->stub_section = s;
1616 stub->offset = s->size;
1617
1618 /* Allocate room for it. */
1619 s->size += 8;
1620 return TRUE;
1621}
1622
1623/* STUB describes an la25 stub that we have decided to implement
1624 with a separate trampoline. Allocate room for it and redirect
1625 the function symbol to it. */
1626
1627static bfd_boolean
1628mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1629 struct bfd_link_info *info)
1630{
1631 struct mips_elf_link_hash_table *htab;
1632 asection *s;
1633
1634 htab = mips_elf_hash_table (info);
1635
1636 /* Create a trampoline section, if we haven't already. */
1637 s = htab->strampoline;
1638 if (s == NULL)
1639 {
1640 asection *input_section = stub->h->root.root.u.def.section;
1641 s = htab->add_stub_section (".text", NULL,
1642 input_section->output_section);
1643 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1644 return FALSE;
1645 htab->strampoline = s;
1646 }
1647
1648 /* Create a symbol for the stub. */
1649 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1650 stub->stub_section = s;
1651 stub->offset = s->size;
1652
1653 /* Allocate room for it. */
1654 s->size += 16;
1655 return TRUE;
1656}
1657
1658/* H describes a symbol that needs an la25 stub. Make sure that an
1659 appropriate stub exists and point H at it. */
1660
1661static bfd_boolean
1662mips_elf_add_la25_stub (struct bfd_link_info *info,
1663 struct mips_elf_link_hash_entry *h)
1664{
1665 struct mips_elf_link_hash_table *htab;
1666 struct mips_elf_la25_stub search, *stub;
1667 bfd_boolean use_trampoline_p;
1668 asection *s;
1669 bfd_vma value;
1670 void **slot;
1671
1672 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1673 of the section and if we would need no more than 2 nops. */
1674 s = h->root.root.u.def.section;
1675 value = h->root.root.u.def.value;
1676 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1677
1678 /* Describe the stub we want. */
1679 search.stub_section = NULL;
1680 search.offset = 0;
1681 search.h = h;
1682
1683 /* See if we've already created an equivalent stub. */
1684 htab = mips_elf_hash_table (info);
1685 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1686 if (slot == NULL)
1687 return FALSE;
1688
1689 stub = (struct mips_elf_la25_stub *) *slot;
1690 if (stub != NULL)
1691 {
1692 /* We can reuse the existing stub. */
1693 h->la25_stub = stub;
1694 return TRUE;
1695 }
1696
1697 /* Create a permanent copy of ENTRY and add it to the hash table. */
1698 stub = bfd_malloc (sizeof (search));
1699 if (stub == NULL)
1700 return FALSE;
1701 *stub = search;
1702 *slot = stub;
1703
1704 h->la25_stub = stub;
1705 return (use_trampoline_p
1706 ? mips_elf_add_la25_trampoline (stub, info)
1707 : mips_elf_add_la25_intro (stub, info));
1708}
1709
1710/* A mips_elf_link_hash_traverse callback that is called before sizing
1711 sections. DATA points to a mips_htab_traverse_info structure. */
1712
1713static bfd_boolean
1714mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1715{
1716 struct mips_htab_traverse_info *hti;
1717
1718 hti = (struct mips_htab_traverse_info *) data;
1719 if (h->root.root.type == bfd_link_hash_warning)
1720 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1721
1722 if (!hti->info->relocatable)
1723 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1724
861fb55a
DJ
1725 if (mips_elf_local_pic_function_p (h))
1726 {
1727 /* H is a function that might need $25 to be valid on entry.
1728 If we're creating a non-PIC relocatable object, mark H as
1729 being PIC. If we're creating a non-relocatable object with
1730 non-PIC branches and jumps to H, make sure that H has an la25
1731 stub. */
1732 if (hti->info->relocatable)
1733 {
1734 if (!PIC_OBJECT_P (hti->output_bfd))
1735 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1736 }
1737 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1738 {
1739 hti->error = TRUE;
1740 return FALSE;
1741 }
1742 }
b34976b6 1743 return TRUE;
b49e97c9
TS
1744}
1745\f
d6f16593
MR
1746/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1747 Most mips16 instructions are 16 bits, but these instructions
1748 are 32 bits.
1749
1750 The format of these instructions is:
1751
1752 +--------------+--------------------------------+
1753 | JALX | X| Imm 20:16 | Imm 25:21 |
1754 +--------------+--------------------------------+
1755 | Immediate 15:0 |
1756 +-----------------------------------------------+
1757
1758 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1759 Note that the immediate value in the first word is swapped.
1760
1761 When producing a relocatable object file, R_MIPS16_26 is
1762 handled mostly like R_MIPS_26. In particular, the addend is
1763 stored as a straight 26-bit value in a 32-bit instruction.
1764 (gas makes life simpler for itself by never adjusting a
1765 R_MIPS16_26 reloc to be against a section, so the addend is
1766 always zero). However, the 32 bit instruction is stored as 2
1767 16-bit values, rather than a single 32-bit value. In a
1768 big-endian file, the result is the same; in a little-endian
1769 file, the two 16-bit halves of the 32 bit value are swapped.
1770 This is so that a disassembler can recognize the jal
1771 instruction.
1772
1773 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1774 instruction stored as two 16-bit values. The addend A is the
1775 contents of the targ26 field. The calculation is the same as
1776 R_MIPS_26. When storing the calculated value, reorder the
1777 immediate value as shown above, and don't forget to store the
1778 value as two 16-bit values.
1779
1780 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1781 defined as
1782
1783 big-endian:
1784 +--------+----------------------+
1785 | | |
1786 | | targ26-16 |
1787 |31 26|25 0|
1788 +--------+----------------------+
1789
1790 little-endian:
1791 +----------+------+-------------+
1792 | | | |
1793 | sub1 | | sub2 |
1794 |0 9|10 15|16 31|
1795 +----------+--------------------+
1796 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1797 ((sub1 << 16) | sub2)).
1798
1799 When producing a relocatable object file, the calculation is
1800 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1801 When producing a fully linked file, the calculation is
1802 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1803 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1804
738e5348
RS
1805 The table below lists the other MIPS16 instruction relocations.
1806 Each one is calculated in the same way as the non-MIPS16 relocation
1807 given on the right, but using the extended MIPS16 layout of 16-bit
1808 immediate fields:
1809
1810 R_MIPS16_GPREL R_MIPS_GPREL16
1811 R_MIPS16_GOT16 R_MIPS_GOT16
1812 R_MIPS16_CALL16 R_MIPS_CALL16
1813 R_MIPS16_HI16 R_MIPS_HI16
1814 R_MIPS16_LO16 R_MIPS_LO16
1815
1816 A typical instruction will have a format like this:
d6f16593
MR
1817
1818 +--------------+--------------------------------+
1819 | EXTEND | Imm 10:5 | Imm 15:11 |
1820 +--------------+--------------------------------+
1821 | Major | rx | ry | Imm 4:0 |
1822 +--------------+--------------------------------+
1823
1824 EXTEND is the five bit value 11110. Major is the instruction
1825 opcode.
1826
738e5348
RS
1827 All we need to do here is shuffle the bits appropriately.
1828 As above, the two 16-bit halves must be swapped on a
1829 little-endian system. */
1830
1831static inline bfd_boolean
1832mips16_reloc_p (int r_type)
1833{
1834 switch (r_type)
1835 {
1836 case R_MIPS16_26:
1837 case R_MIPS16_GPREL:
1838 case R_MIPS16_GOT16:
1839 case R_MIPS16_CALL16:
1840 case R_MIPS16_HI16:
1841 case R_MIPS16_LO16:
1842 return TRUE;
1843
1844 default:
1845 return FALSE;
1846 }
1847}
1848
1849static inline bfd_boolean
1850got16_reloc_p (int r_type)
1851{
1852 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1853}
1854
1855static inline bfd_boolean
1856call16_reloc_p (int r_type)
1857{
1858 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1859}
1860
1861static inline bfd_boolean
1862hi16_reloc_p (int r_type)
1863{
1864 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1865}
d6f16593 1866
738e5348
RS
1867static inline bfd_boolean
1868lo16_reloc_p (int r_type)
1869{
1870 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1871}
1872
1873static inline bfd_boolean
1874mips16_call_reloc_p (int r_type)
1875{
1876 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1877}
d6f16593 1878
38a7df63
CF
1879static inline bfd_boolean
1880jal_reloc_p (int r_type)
1881{
1882 return r_type == R_MIPS_26 || r_type == R_MIPS16_26;
1883}
1884
d6f16593
MR
1885void
1886_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1887 bfd_boolean jal_shuffle, bfd_byte *data)
1888{
1889 bfd_vma extend, insn, val;
1890
738e5348 1891 if (!mips16_reloc_p (r_type))
d6f16593
MR
1892 return;
1893
1894 /* Pick up the mips16 extend instruction and the real instruction. */
1895 extend = bfd_get_16 (abfd, data);
1896 insn = bfd_get_16 (abfd, data + 2);
1897 if (r_type == R_MIPS16_26)
1898 {
1899 if (jal_shuffle)
1900 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1901 | ((extend & 0x1f) << 21) | insn;
1902 else
1903 val = extend << 16 | insn;
1904 }
1905 else
1906 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1907 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1908 bfd_put_32 (abfd, val, data);
1909}
1910
1911void
1912_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1913 bfd_boolean jal_shuffle, bfd_byte *data)
1914{
1915 bfd_vma extend, insn, val;
1916
738e5348 1917 if (!mips16_reloc_p (r_type))
d6f16593
MR
1918 return;
1919
1920 val = bfd_get_32 (abfd, data);
1921 if (r_type == R_MIPS16_26)
1922 {
1923 if (jal_shuffle)
1924 {
1925 insn = val & 0xffff;
1926 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1927 | ((val >> 21) & 0x1f);
1928 }
1929 else
1930 {
1931 insn = val & 0xffff;
1932 extend = val >> 16;
1933 }
1934 }
1935 else
1936 {
1937 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1938 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1939 }
1940 bfd_put_16 (abfd, insn, data + 2);
1941 bfd_put_16 (abfd, extend, data);
1942}
1943
b49e97c9 1944bfd_reloc_status_type
9719ad41
RS
1945_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1946 arelent *reloc_entry, asection *input_section,
1947 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
1948{
1949 bfd_vma relocation;
a7ebbfdf 1950 bfd_signed_vma val;
30ac9238 1951 bfd_reloc_status_type status;
b49e97c9
TS
1952
1953 if (bfd_is_com_section (symbol->section))
1954 relocation = 0;
1955 else
1956 relocation = symbol->value;
1957
1958 relocation += symbol->section->output_section->vma;
1959 relocation += symbol->section->output_offset;
1960
07515404 1961 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
1962 return bfd_reloc_outofrange;
1963
b49e97c9 1964 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
1965 val = reloc_entry->addend;
1966
30ac9238 1967 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 1968
b49e97c9 1969 /* Adjust val for the final section location and GP value. If we
1049f94e 1970 are producing relocatable output, we don't want to do this for
b49e97c9 1971 an external symbol. */
1049f94e 1972 if (! relocatable
b49e97c9
TS
1973 || (symbol->flags & BSF_SECTION_SYM) != 0)
1974 val += relocation - gp;
1975
a7ebbfdf
TS
1976 if (reloc_entry->howto->partial_inplace)
1977 {
30ac9238
RS
1978 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1979 (bfd_byte *) data
1980 + reloc_entry->address);
1981 if (status != bfd_reloc_ok)
1982 return status;
a7ebbfdf
TS
1983 }
1984 else
1985 reloc_entry->addend = val;
b49e97c9 1986
1049f94e 1987 if (relocatable)
b49e97c9 1988 reloc_entry->address += input_section->output_offset;
30ac9238
RS
1989
1990 return bfd_reloc_ok;
1991}
1992
1993/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1994 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1995 that contains the relocation field and DATA points to the start of
1996 INPUT_SECTION. */
1997
1998struct mips_hi16
1999{
2000 struct mips_hi16 *next;
2001 bfd_byte *data;
2002 asection *input_section;
2003 arelent rel;
2004};
2005
2006/* FIXME: This should not be a static variable. */
2007
2008static struct mips_hi16 *mips_hi16_list;
2009
2010/* A howto special_function for REL *HI16 relocations. We can only
2011 calculate the correct value once we've seen the partnering
2012 *LO16 relocation, so just save the information for later.
2013
2014 The ABI requires that the *LO16 immediately follow the *HI16.
2015 However, as a GNU extension, we permit an arbitrary number of
2016 *HI16s to be associated with a single *LO16. This significantly
2017 simplies the relocation handling in gcc. */
2018
2019bfd_reloc_status_type
2020_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2021 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2022 asection *input_section, bfd *output_bfd,
2023 char **error_message ATTRIBUTE_UNUSED)
2024{
2025 struct mips_hi16 *n;
2026
07515404 2027 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2028 return bfd_reloc_outofrange;
2029
2030 n = bfd_malloc (sizeof *n);
2031 if (n == NULL)
2032 return bfd_reloc_outofrange;
2033
2034 n->next = mips_hi16_list;
2035 n->data = data;
2036 n->input_section = input_section;
2037 n->rel = *reloc_entry;
2038 mips_hi16_list = n;
2039
2040 if (output_bfd != NULL)
2041 reloc_entry->address += input_section->output_offset;
2042
2043 return bfd_reloc_ok;
2044}
2045
738e5348 2046/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2047 like any other 16-bit relocation when applied to global symbols, but is
2048 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2049
2050bfd_reloc_status_type
2051_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2052 void *data, asection *input_section,
2053 bfd *output_bfd, char **error_message)
2054{
2055 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2056 || bfd_is_und_section (bfd_get_section (symbol))
2057 || bfd_is_com_section (bfd_get_section (symbol)))
2058 /* The relocation is against a global symbol. */
2059 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2060 input_section, output_bfd,
2061 error_message);
2062
2063 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2064 input_section, output_bfd, error_message);
2065}
2066
2067/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2068 is a straightforward 16 bit inplace relocation, but we must deal with
2069 any partnering high-part relocations as well. */
2070
2071bfd_reloc_status_type
2072_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2073 void *data, asection *input_section,
2074 bfd *output_bfd, char **error_message)
2075{
2076 bfd_vma vallo;
d6f16593 2077 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2078
07515404 2079 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2080 return bfd_reloc_outofrange;
2081
d6f16593
MR
2082 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2083 location);
2084 vallo = bfd_get_32 (abfd, location);
2085 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2086 location);
2087
30ac9238
RS
2088 while (mips_hi16_list != NULL)
2089 {
2090 bfd_reloc_status_type ret;
2091 struct mips_hi16 *hi;
2092
2093 hi = mips_hi16_list;
2094
738e5348
RS
2095 /* R_MIPS*_GOT16 relocations are something of a special case. We
2096 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2097 relocation (with a rightshift of 16). However, since GOT16
2098 relocations can also be used with global symbols, their howto
2099 has a rightshift of 0. */
2100 if (hi->rel.howto->type == R_MIPS_GOT16)
2101 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2102 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2103 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
30ac9238
RS
2104
2105 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2106 carry or borrow will induce a change of +1 or -1 in the high part. */
2107 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2108
30ac9238
RS
2109 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2110 hi->input_section, output_bfd,
2111 error_message);
2112 if (ret != bfd_reloc_ok)
2113 return ret;
2114
2115 mips_hi16_list = hi->next;
2116 free (hi);
2117 }
2118
2119 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2120 input_section, output_bfd,
2121 error_message);
2122}
2123
2124/* A generic howto special_function. This calculates and installs the
2125 relocation itself, thus avoiding the oft-discussed problems in
2126 bfd_perform_relocation and bfd_install_relocation. */
2127
2128bfd_reloc_status_type
2129_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2130 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2131 asection *input_section, bfd *output_bfd,
2132 char **error_message ATTRIBUTE_UNUSED)
2133{
2134 bfd_signed_vma val;
2135 bfd_reloc_status_type status;
2136 bfd_boolean relocatable;
2137
2138 relocatable = (output_bfd != NULL);
2139
07515404 2140 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2141 return bfd_reloc_outofrange;
2142
2143 /* Build up the field adjustment in VAL. */
2144 val = 0;
2145 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2146 {
2147 /* Either we're calculating the final field value or we have a
2148 relocation against a section symbol. Add in the section's
2149 offset or address. */
2150 val += symbol->section->output_section->vma;
2151 val += symbol->section->output_offset;
2152 }
2153
2154 if (!relocatable)
2155 {
2156 /* We're calculating the final field value. Add in the symbol's value
2157 and, if pc-relative, subtract the address of the field itself. */
2158 val += symbol->value;
2159 if (reloc_entry->howto->pc_relative)
2160 {
2161 val -= input_section->output_section->vma;
2162 val -= input_section->output_offset;
2163 val -= reloc_entry->address;
2164 }
2165 }
2166
2167 /* VAL is now the final adjustment. If we're keeping this relocation
2168 in the output file, and if the relocation uses a separate addend,
2169 we just need to add VAL to that addend. Otherwise we need to add
2170 VAL to the relocation field itself. */
2171 if (relocatable && !reloc_entry->howto->partial_inplace)
2172 reloc_entry->addend += val;
2173 else
2174 {
d6f16593
MR
2175 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2176
30ac9238
RS
2177 /* Add in the separate addend, if any. */
2178 val += reloc_entry->addend;
2179
2180 /* Add VAL to the relocation field. */
d6f16593
MR
2181 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2182 location);
30ac9238 2183 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593
MR
2184 location);
2185 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2186 location);
2187
30ac9238
RS
2188 if (status != bfd_reloc_ok)
2189 return status;
2190 }
2191
2192 if (relocatable)
2193 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2194
2195 return bfd_reloc_ok;
2196}
2197\f
2198/* Swap an entry in a .gptab section. Note that these routines rely
2199 on the equivalence of the two elements of the union. */
2200
2201static void
9719ad41
RS
2202bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2203 Elf32_gptab *in)
b49e97c9
TS
2204{
2205 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2206 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2207}
2208
2209static void
9719ad41
RS
2210bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2211 Elf32_External_gptab *ex)
b49e97c9
TS
2212{
2213 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2214 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2215}
2216
2217static void
9719ad41
RS
2218bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2219 Elf32_External_compact_rel *ex)
b49e97c9
TS
2220{
2221 H_PUT_32 (abfd, in->id1, ex->id1);
2222 H_PUT_32 (abfd, in->num, ex->num);
2223 H_PUT_32 (abfd, in->id2, ex->id2);
2224 H_PUT_32 (abfd, in->offset, ex->offset);
2225 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2226 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2227}
2228
2229static void
9719ad41
RS
2230bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2231 Elf32_External_crinfo *ex)
b49e97c9
TS
2232{
2233 unsigned long l;
2234
2235 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2236 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2237 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2238 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2239 H_PUT_32 (abfd, l, ex->info);
2240 H_PUT_32 (abfd, in->konst, ex->konst);
2241 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2242}
b49e97c9
TS
2243\f
2244/* A .reginfo section holds a single Elf32_RegInfo structure. These
2245 routines swap this structure in and out. They are used outside of
2246 BFD, so they are globally visible. */
2247
2248void
9719ad41
RS
2249bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2250 Elf32_RegInfo *in)
b49e97c9
TS
2251{
2252 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2253 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2254 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2255 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2256 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2257 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2258}
2259
2260void
9719ad41
RS
2261bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2262 Elf32_External_RegInfo *ex)
b49e97c9
TS
2263{
2264 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2265 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2266 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2267 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2268 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2269 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2270}
2271
2272/* In the 64 bit ABI, the .MIPS.options section holds register
2273 information in an Elf64_Reginfo structure. These routines swap
2274 them in and out. They are globally visible because they are used
2275 outside of BFD. These routines are here so that gas can call them
2276 without worrying about whether the 64 bit ABI has been included. */
2277
2278void
9719ad41
RS
2279bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2280 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2281{
2282 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2283 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2284 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2285 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2286 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2287 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2288 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2289}
2290
2291void
9719ad41
RS
2292bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2293 Elf64_External_RegInfo *ex)
b49e97c9
TS
2294{
2295 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2296 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2297 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2298 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2299 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2300 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2301 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2302}
2303
2304/* Swap in an options header. */
2305
2306void
9719ad41
RS
2307bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2308 Elf_Internal_Options *in)
b49e97c9
TS
2309{
2310 in->kind = H_GET_8 (abfd, ex->kind);
2311 in->size = H_GET_8 (abfd, ex->size);
2312 in->section = H_GET_16 (abfd, ex->section);
2313 in->info = H_GET_32 (abfd, ex->info);
2314}
2315
2316/* Swap out an options header. */
2317
2318void
9719ad41
RS
2319bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2320 Elf_External_Options *ex)
b49e97c9
TS
2321{
2322 H_PUT_8 (abfd, in->kind, ex->kind);
2323 H_PUT_8 (abfd, in->size, ex->size);
2324 H_PUT_16 (abfd, in->section, ex->section);
2325 H_PUT_32 (abfd, in->info, ex->info);
2326}
2327\f
2328/* This function is called via qsort() to sort the dynamic relocation
2329 entries by increasing r_symndx value. */
2330
2331static int
9719ad41 2332sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2333{
947216bf
AM
2334 Elf_Internal_Rela int_reloc1;
2335 Elf_Internal_Rela int_reloc2;
6870500c 2336 int diff;
b49e97c9 2337
947216bf
AM
2338 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2339 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2340
6870500c
RS
2341 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2342 if (diff != 0)
2343 return diff;
2344
2345 if (int_reloc1.r_offset < int_reloc2.r_offset)
2346 return -1;
2347 if (int_reloc1.r_offset > int_reloc2.r_offset)
2348 return 1;
2349 return 0;
b49e97c9
TS
2350}
2351
f4416af6
AO
2352/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2353
2354static int
7e3102a7
AM
2355sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2356 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2357{
7e3102a7 2358#ifdef BFD64
f4416af6
AO
2359 Elf_Internal_Rela int_reloc1[3];
2360 Elf_Internal_Rela int_reloc2[3];
2361
2362 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2363 (reldyn_sorting_bfd, arg1, int_reloc1);
2364 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2365 (reldyn_sorting_bfd, arg2, int_reloc2);
2366
6870500c
RS
2367 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2368 return -1;
2369 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2370 return 1;
2371
2372 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2373 return -1;
2374 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2375 return 1;
2376 return 0;
7e3102a7
AM
2377#else
2378 abort ();
2379#endif
f4416af6
AO
2380}
2381
2382
b49e97c9
TS
2383/* This routine is used to write out ECOFF debugging external symbol
2384 information. It is called via mips_elf_link_hash_traverse. The
2385 ECOFF external symbol information must match the ELF external
2386 symbol information. Unfortunately, at this point we don't know
2387 whether a symbol is required by reloc information, so the two
2388 tables may wind up being different. We must sort out the external
2389 symbol information before we can set the final size of the .mdebug
2390 section, and we must set the size of the .mdebug section before we
2391 can relocate any sections, and we can't know which symbols are
2392 required by relocation until we relocate the sections.
2393 Fortunately, it is relatively unlikely that any symbol will be
2394 stripped but required by a reloc. In particular, it can not happen
2395 when generating a final executable. */
2396
b34976b6 2397static bfd_boolean
9719ad41 2398mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2399{
9719ad41 2400 struct extsym_info *einfo = data;
b34976b6 2401 bfd_boolean strip;
b49e97c9
TS
2402 asection *sec, *output_section;
2403
2404 if (h->root.root.type == bfd_link_hash_warning)
2405 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2406
2407 if (h->root.indx == -2)
b34976b6 2408 strip = FALSE;
f5385ebf 2409 else if ((h->root.def_dynamic
77cfaee6
AM
2410 || h->root.ref_dynamic
2411 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2412 && !h->root.def_regular
2413 && !h->root.ref_regular)
b34976b6 2414 strip = TRUE;
b49e97c9
TS
2415 else if (einfo->info->strip == strip_all
2416 || (einfo->info->strip == strip_some
2417 && bfd_hash_lookup (einfo->info->keep_hash,
2418 h->root.root.root.string,
b34976b6
AM
2419 FALSE, FALSE) == NULL))
2420 strip = TRUE;
b49e97c9 2421 else
b34976b6 2422 strip = FALSE;
b49e97c9
TS
2423
2424 if (strip)
b34976b6 2425 return TRUE;
b49e97c9
TS
2426
2427 if (h->esym.ifd == -2)
2428 {
2429 h->esym.jmptbl = 0;
2430 h->esym.cobol_main = 0;
2431 h->esym.weakext = 0;
2432 h->esym.reserved = 0;
2433 h->esym.ifd = ifdNil;
2434 h->esym.asym.value = 0;
2435 h->esym.asym.st = stGlobal;
2436
2437 if (h->root.root.type == bfd_link_hash_undefined
2438 || h->root.root.type == bfd_link_hash_undefweak)
2439 {
2440 const char *name;
2441
2442 /* Use undefined class. Also, set class and type for some
2443 special symbols. */
2444 name = h->root.root.root.string;
2445 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2446 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2447 {
2448 h->esym.asym.sc = scData;
2449 h->esym.asym.st = stLabel;
2450 h->esym.asym.value = 0;
2451 }
2452 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2453 {
2454 h->esym.asym.sc = scAbs;
2455 h->esym.asym.st = stLabel;
2456 h->esym.asym.value =
2457 mips_elf_hash_table (einfo->info)->procedure_count;
2458 }
4a14403c 2459 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2460 {
2461 h->esym.asym.sc = scAbs;
2462 h->esym.asym.st = stLabel;
2463 h->esym.asym.value = elf_gp (einfo->abfd);
2464 }
2465 else
2466 h->esym.asym.sc = scUndefined;
2467 }
2468 else if (h->root.root.type != bfd_link_hash_defined
2469 && h->root.root.type != bfd_link_hash_defweak)
2470 h->esym.asym.sc = scAbs;
2471 else
2472 {
2473 const char *name;
2474
2475 sec = h->root.root.u.def.section;
2476 output_section = sec->output_section;
2477
2478 /* When making a shared library and symbol h is the one from
2479 the another shared library, OUTPUT_SECTION may be null. */
2480 if (output_section == NULL)
2481 h->esym.asym.sc = scUndefined;
2482 else
2483 {
2484 name = bfd_section_name (output_section->owner, output_section);
2485
2486 if (strcmp (name, ".text") == 0)
2487 h->esym.asym.sc = scText;
2488 else if (strcmp (name, ".data") == 0)
2489 h->esym.asym.sc = scData;
2490 else if (strcmp (name, ".sdata") == 0)
2491 h->esym.asym.sc = scSData;
2492 else if (strcmp (name, ".rodata") == 0
2493 || strcmp (name, ".rdata") == 0)
2494 h->esym.asym.sc = scRData;
2495 else if (strcmp (name, ".bss") == 0)
2496 h->esym.asym.sc = scBss;
2497 else if (strcmp (name, ".sbss") == 0)
2498 h->esym.asym.sc = scSBss;
2499 else if (strcmp (name, ".init") == 0)
2500 h->esym.asym.sc = scInit;
2501 else if (strcmp (name, ".fini") == 0)
2502 h->esym.asym.sc = scFini;
2503 else
2504 h->esym.asym.sc = scAbs;
2505 }
2506 }
2507
2508 h->esym.asym.reserved = 0;
2509 h->esym.asym.index = indexNil;
2510 }
2511
2512 if (h->root.root.type == bfd_link_hash_common)
2513 h->esym.asym.value = h->root.root.u.c.size;
2514 else if (h->root.root.type == bfd_link_hash_defined
2515 || h->root.root.type == bfd_link_hash_defweak)
2516 {
2517 if (h->esym.asym.sc == scCommon)
2518 h->esym.asym.sc = scBss;
2519 else if (h->esym.asym.sc == scSCommon)
2520 h->esym.asym.sc = scSBss;
2521
2522 sec = h->root.root.u.def.section;
2523 output_section = sec->output_section;
2524 if (output_section != NULL)
2525 h->esym.asym.value = (h->root.root.u.def.value
2526 + sec->output_offset
2527 + output_section->vma);
2528 else
2529 h->esym.asym.value = 0;
2530 }
33bb52fb 2531 else
b49e97c9
TS
2532 {
2533 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2534
2535 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2536 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2537
33bb52fb 2538 if (hd->needs_lazy_stub)
b49e97c9
TS
2539 {
2540 /* Set type and value for a symbol with a function stub. */
2541 h->esym.asym.st = stProc;
2542 sec = hd->root.root.u.def.section;
2543 if (sec == NULL)
2544 h->esym.asym.value = 0;
2545 else
2546 {
2547 output_section = sec->output_section;
2548 if (output_section != NULL)
2549 h->esym.asym.value = (hd->root.plt.offset
2550 + sec->output_offset
2551 + output_section->vma);
2552 else
2553 h->esym.asym.value = 0;
2554 }
b49e97c9
TS
2555 }
2556 }
2557
2558 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2559 h->root.root.root.string,
2560 &h->esym))
2561 {
b34976b6
AM
2562 einfo->failed = TRUE;
2563 return FALSE;
b49e97c9
TS
2564 }
2565
b34976b6 2566 return TRUE;
b49e97c9
TS
2567}
2568
2569/* A comparison routine used to sort .gptab entries. */
2570
2571static int
9719ad41 2572gptab_compare (const void *p1, const void *p2)
b49e97c9 2573{
9719ad41
RS
2574 const Elf32_gptab *a1 = p1;
2575 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2576
2577 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2578}
2579\f
b15e6682 2580/* Functions to manage the got entry hash table. */
f4416af6
AO
2581
2582/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2583 hash number. */
2584
2585static INLINE hashval_t
9719ad41 2586mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2587{
2588#ifdef BFD64
2589 return addr + (addr >> 32);
2590#else
2591 return addr;
2592#endif
2593}
2594
2595/* got_entries only match if they're identical, except for gotidx, so
2596 use all fields to compute the hash, and compare the appropriate
2597 union members. */
2598
b15e6682 2599static hashval_t
9719ad41 2600mips_elf_got_entry_hash (const void *entry_)
b15e6682
AO
2601{
2602 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2603
38985a1c 2604 return entry->symndx
0f20cc35 2605 + ((entry->tls_type & GOT_TLS_LDM) << 17)
f4416af6 2606 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
38985a1c
AO
2607 : entry->abfd->id
2608 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2609 : entry->d.h->root.root.root.hash));
b15e6682
AO
2610}
2611
2612static int
9719ad41 2613mips_elf_got_entry_eq (const void *entry1, const void *entry2)
b15e6682
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 /* An LDM entry can only match another LDM entry. */
2619 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2620 return 0;
2621
b15e6682 2622 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
f4416af6
AO
2623 && (! e1->abfd ? e1->d.address == e2->d.address
2624 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2625 : e1->d.h == e2->d.h);
2626}
2627
2628/* multi_got_entries are still a match in the case of global objects,
2629 even if the input bfd in which they're referenced differs, so the
2630 hash computation and compare functions are adjusted
2631 accordingly. */
2632
2633static hashval_t
9719ad41 2634mips_elf_multi_got_entry_hash (const void *entry_)
f4416af6
AO
2635{
2636 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2637
2638 return entry->symndx
2639 + (! entry->abfd
2640 ? mips_elf_hash_bfd_vma (entry->d.address)
2641 : entry->symndx >= 0
0f20cc35
DJ
2642 ? ((entry->tls_type & GOT_TLS_LDM)
2643 ? (GOT_TLS_LDM << 17)
2644 : (entry->abfd->id
2645 + mips_elf_hash_bfd_vma (entry->d.addend)))
f4416af6
AO
2646 : entry->d.h->root.root.root.hash);
2647}
2648
2649static int
9719ad41 2650mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2651{
2652 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2653 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2654
0f20cc35
DJ
2655 /* Any two LDM entries match. */
2656 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2657 return 1;
2658
2659 /* Nothing else matches an LDM entry. */
2660 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2661 return 0;
2662
f4416af6
AO
2663 return e1->symndx == e2->symndx
2664 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2665 : e1->abfd == NULL || e2->abfd == NULL
2666 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2667 : e1->d.h == e2->d.h);
b15e6682 2668}
c224138d
RS
2669
2670static hashval_t
2671mips_got_page_entry_hash (const void *entry_)
2672{
2673 const struct mips_got_page_entry *entry;
2674
2675 entry = (const struct mips_got_page_entry *) entry_;
2676 return entry->abfd->id + entry->symndx;
2677}
2678
2679static int
2680mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2681{
2682 const struct mips_got_page_entry *entry1, *entry2;
2683
2684 entry1 = (const struct mips_got_page_entry *) entry1_;
2685 entry2 = (const struct mips_got_page_entry *) entry2_;
2686 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2687}
b15e6682 2688\f
0a44bf69
RS
2689/* Return the dynamic relocation section. If it doesn't exist, try to
2690 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2691 if creation fails. */
f4416af6
AO
2692
2693static asection *
0a44bf69 2694mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 2695{
0a44bf69 2696 const char *dname;
f4416af6 2697 asection *sreloc;
0a44bf69 2698 bfd *dynobj;
f4416af6 2699
0a44bf69
RS
2700 dname = MIPS_ELF_REL_DYN_NAME (info);
2701 dynobj = elf_hash_table (info)->dynobj;
f4416af6
AO
2702 sreloc = bfd_get_section_by_name (dynobj, dname);
2703 if (sreloc == NULL && create_p)
2704 {
3496cb2a
L
2705 sreloc = bfd_make_section_with_flags (dynobj, dname,
2706 (SEC_ALLOC
2707 | SEC_LOAD
2708 | SEC_HAS_CONTENTS
2709 | SEC_IN_MEMORY
2710 | SEC_LINKER_CREATED
2711 | SEC_READONLY));
f4416af6 2712 if (sreloc == NULL
f4416af6 2713 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 2714 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
2715 return NULL;
2716 }
2717 return sreloc;
2718}
2719
0f20cc35
DJ
2720/* Count the number of relocations needed for a TLS GOT entry, with
2721 access types from TLS_TYPE, and symbol H (or a local symbol if H
2722 is NULL). */
2723
2724static int
2725mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2726 struct elf_link_hash_entry *h)
2727{
2728 int indx = 0;
2729 int ret = 0;
2730 bfd_boolean need_relocs = FALSE;
2731 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2732
2733 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2734 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2735 indx = h->dynindx;
2736
2737 if ((info->shared || indx != 0)
2738 && (h == NULL
2739 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2740 || h->root.type != bfd_link_hash_undefweak))
2741 need_relocs = TRUE;
2742
2743 if (!need_relocs)
2744 return FALSE;
2745
2746 if (tls_type & GOT_TLS_GD)
2747 {
2748 ret++;
2749 if (indx != 0)
2750 ret++;
2751 }
2752
2753 if (tls_type & GOT_TLS_IE)
2754 ret++;
2755
2756 if ((tls_type & GOT_TLS_LDM) && info->shared)
2757 ret++;
2758
2759 return ret;
2760}
2761
2762/* Count the number of TLS relocations required for the GOT entry in
2763 ARG1, if it describes a local symbol. */
2764
2765static int
2766mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2767{
2768 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2769 struct mips_elf_count_tls_arg *arg = arg2;
2770
2771 if (entry->abfd != NULL && entry->symndx != -1)
2772 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2773
2774 return 1;
2775}
2776
2777/* Count the number of TLS GOT entries required for the global (or
2778 forced-local) symbol in ARG1. */
2779
2780static int
2781mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2782{
2783 struct mips_elf_link_hash_entry *hm
2784 = (struct mips_elf_link_hash_entry *) arg1;
2785 struct mips_elf_count_tls_arg *arg = arg2;
2786
2787 if (hm->tls_type & GOT_TLS_GD)
2788 arg->needed += 2;
2789 if (hm->tls_type & GOT_TLS_IE)
2790 arg->needed += 1;
2791
2792 return 1;
2793}
2794
2795/* Count the number of TLS relocations required for the global (or
2796 forced-local) symbol in ARG1. */
2797
2798static int
2799mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2800{
2801 struct mips_elf_link_hash_entry *hm
2802 = (struct mips_elf_link_hash_entry *) arg1;
2803 struct mips_elf_count_tls_arg *arg = arg2;
2804
2805 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2806
2807 return 1;
2808}
2809
2810/* Output a simple dynamic relocation into SRELOC. */
2811
2812static void
2813mips_elf_output_dynamic_relocation (bfd *output_bfd,
2814 asection *sreloc,
861fb55a 2815 unsigned long reloc_index,
0f20cc35
DJ
2816 unsigned long indx,
2817 int r_type,
2818 bfd_vma offset)
2819{
2820 Elf_Internal_Rela rel[3];
2821
2822 memset (rel, 0, sizeof (rel));
2823
2824 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2825 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2826
2827 if (ABI_64_P (output_bfd))
2828 {
2829 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2830 (output_bfd, &rel[0],
2831 (sreloc->contents
861fb55a 2832 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
2833 }
2834 else
2835 bfd_elf32_swap_reloc_out
2836 (output_bfd, &rel[0],
2837 (sreloc->contents
861fb55a 2838 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
2839}
2840
2841/* Initialize a set of TLS GOT entries for one symbol. */
2842
2843static void
2844mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2845 unsigned char *tls_type_p,
2846 struct bfd_link_info *info,
2847 struct mips_elf_link_hash_entry *h,
2848 bfd_vma value)
2849{
23cc69b6 2850 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
2851 int indx;
2852 asection *sreloc, *sgot;
2853 bfd_vma offset, offset2;
0f20cc35
DJ
2854 bfd_boolean need_relocs = FALSE;
2855
23cc69b6
RS
2856 htab = mips_elf_hash_table (info);
2857 sgot = htab->sgot;
0f20cc35
DJ
2858
2859 indx = 0;
2860 if (h != NULL)
2861 {
2862 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2863
2864 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2865 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2866 indx = h->root.dynindx;
2867 }
2868
2869 if (*tls_type_p & GOT_TLS_DONE)
2870 return;
2871
2872 if ((info->shared || indx != 0)
2873 && (h == NULL
2874 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2875 || h->root.type != bfd_link_hash_undefweak))
2876 need_relocs = TRUE;
2877
2878 /* MINUS_ONE means the symbol is not defined in this object. It may not
2879 be defined at all; assume that the value doesn't matter in that
2880 case. Otherwise complain if we would use the value. */
2881 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2882 || h->root.root.type == bfd_link_hash_undefweak);
2883
2884 /* Emit necessary relocations. */
0a44bf69 2885 sreloc = mips_elf_rel_dyn_section (info, FALSE);
0f20cc35
DJ
2886
2887 /* General Dynamic. */
2888 if (*tls_type_p & GOT_TLS_GD)
2889 {
2890 offset = got_offset;
2891 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2892
2893 if (need_relocs)
2894 {
2895 mips_elf_output_dynamic_relocation
861fb55a 2896 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2897 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2898 sgot->output_offset + sgot->output_section->vma + offset);
2899
2900 if (indx)
2901 mips_elf_output_dynamic_relocation
861fb55a 2902 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2903 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2904 sgot->output_offset + sgot->output_section->vma + offset2);
2905 else
2906 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2907 sgot->contents + offset2);
2908 }
2909 else
2910 {
2911 MIPS_ELF_PUT_WORD (abfd, 1,
2912 sgot->contents + offset);
2913 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2914 sgot->contents + offset2);
2915 }
2916
2917 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2918 }
2919
2920 /* Initial Exec model. */
2921 if (*tls_type_p & GOT_TLS_IE)
2922 {
2923 offset = got_offset;
2924
2925 if (need_relocs)
2926 {
2927 if (indx == 0)
2928 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2929 sgot->contents + offset);
2930 else
2931 MIPS_ELF_PUT_WORD (abfd, 0,
2932 sgot->contents + offset);
2933
2934 mips_elf_output_dynamic_relocation
861fb55a 2935 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2936 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2937 sgot->output_offset + sgot->output_section->vma + offset);
2938 }
2939 else
2940 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2941 sgot->contents + offset);
2942 }
2943
2944 if (*tls_type_p & GOT_TLS_LDM)
2945 {
2946 /* The initial offset is zero, and the LD offsets will include the
2947 bias by DTP_OFFSET. */
2948 MIPS_ELF_PUT_WORD (abfd, 0,
2949 sgot->contents + got_offset
2950 + MIPS_ELF_GOT_SIZE (abfd));
2951
2952 if (!info->shared)
2953 MIPS_ELF_PUT_WORD (abfd, 1,
2954 sgot->contents + got_offset);
2955 else
2956 mips_elf_output_dynamic_relocation
861fb55a 2957 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2958 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2959 sgot->output_offset + sgot->output_section->vma + got_offset);
2960 }
2961
2962 *tls_type_p |= GOT_TLS_DONE;
2963}
2964
2965/* Return the GOT index to use for a relocation of type R_TYPE against
2966 a symbol accessed using TLS_TYPE models. The GOT entries for this
2967 symbol in this GOT start at GOT_INDEX. This function initializes the
2968 GOT entries and corresponding relocations. */
2969
2970static bfd_vma
2971mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2972 int r_type, struct bfd_link_info *info,
2973 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2974{
2975 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2976 || r_type == R_MIPS_TLS_LDM);
2977
2978 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2979
2980 if (r_type == R_MIPS_TLS_GOTTPREL)
2981 {
2982 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2983 if (*tls_type & GOT_TLS_GD)
2984 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2985 else
2986 return got_index;
2987 }
2988
2989 if (r_type == R_MIPS_TLS_GD)
2990 {
2991 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2992 return got_index;
2993 }
2994
2995 if (r_type == R_MIPS_TLS_LDM)
2996 {
2997 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2998 return got_index;
2999 }
3000
3001 return got_index;
3002}
3003
0a44bf69
RS
3004/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3005 for global symbol H. .got.plt comes before the GOT, so the offset
3006 will be negative. */
3007
3008static bfd_vma
3009mips_elf_gotplt_index (struct bfd_link_info *info,
3010 struct elf_link_hash_entry *h)
3011{
3012 bfd_vma plt_index, got_address, got_value;
3013 struct mips_elf_link_hash_table *htab;
3014
3015 htab = mips_elf_hash_table (info);
3016 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3017
861fb55a
DJ
3018 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3019 section starts with reserved entries. */
3020 BFD_ASSERT (htab->is_vxworks);
3021
0a44bf69
RS
3022 /* Calculate the index of the symbol's PLT entry. */
3023 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3024
3025 /* Calculate the address of the associated .got.plt entry. */
3026 got_address = (htab->sgotplt->output_section->vma
3027 + htab->sgotplt->output_offset
3028 + plt_index * 4);
3029
3030 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3031 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3032 + htab->root.hgot->root.u.def.section->output_offset
3033 + htab->root.hgot->root.u.def.value);
3034
3035 return got_address - got_value;
3036}
3037
5c18022e 3038/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3039 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3040 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3041 offset can be found. */
b49e97c9
TS
3042
3043static bfd_vma
9719ad41 3044mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3045 bfd_vma value, unsigned long r_symndx,
0f20cc35 3046 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3047{
a8028dd0 3048 struct mips_elf_link_hash_table *htab;
b15e6682 3049 struct mips_got_entry *entry;
b49e97c9 3050
a8028dd0
RS
3051 htab = mips_elf_hash_table (info);
3052 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3053 r_symndx, h, r_type);
0f20cc35 3054 if (!entry)
b15e6682 3055 return MINUS_ONE;
0f20cc35
DJ
3056
3057 if (TLS_RELOC_P (r_type))
ead49a57 3058 {
a8028dd0 3059 if (entry->symndx == -1 && htab->got_info->next == NULL)
ead49a57
RS
3060 /* A type (3) entry in the single-GOT case. We use the symbol's
3061 hash table entry to track the index. */
3062 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3063 r_type, info, h, value);
3064 else
3065 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3066 r_type, info, h, value);
3067 }
0f20cc35
DJ
3068 else
3069 return entry->gotidx;
b49e97c9
TS
3070}
3071
3072/* Returns the GOT index for the global symbol indicated by H. */
3073
3074static bfd_vma
0f20cc35
DJ
3075mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3076 int r_type, struct bfd_link_info *info)
b49e97c9 3077{
a8028dd0 3078 struct mips_elf_link_hash_table *htab;
91d6fa6a 3079 bfd_vma got_index;
f4416af6 3080 struct mips_got_info *g, *gg;
d0c7ff07 3081 long global_got_dynindx = 0;
b49e97c9 3082
a8028dd0
RS
3083 htab = mips_elf_hash_table (info);
3084 gg = g = htab->got_info;
f4416af6
AO
3085 if (g->bfd2got && ibfd)
3086 {
3087 struct mips_got_entry e, *p;
143d77c5 3088
f4416af6
AO
3089 BFD_ASSERT (h->dynindx >= 0);
3090
3091 g = mips_elf_got_for_ibfd (g, ibfd);
0f20cc35 3092 if (g->next != gg || TLS_RELOC_P (r_type))
f4416af6
AO
3093 {
3094 e.abfd = ibfd;
3095 e.symndx = -1;
3096 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 3097 e.tls_type = 0;
f4416af6 3098
9719ad41 3099 p = htab_find (g->got_entries, &e);
f4416af6
AO
3100
3101 BFD_ASSERT (p->gotidx > 0);
0f20cc35
DJ
3102
3103 if (TLS_RELOC_P (r_type))
3104 {
3105 bfd_vma value = MINUS_ONE;
3106 if ((h->root.type == bfd_link_hash_defined
3107 || h->root.type == bfd_link_hash_defweak)
3108 && h->root.u.def.section->output_section)
3109 value = (h->root.u.def.value
3110 + h->root.u.def.section->output_offset
3111 + h->root.u.def.section->output_section->vma);
3112
3113 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3114 info, e.d.h, value);
3115 }
3116 else
3117 return p->gotidx;
f4416af6
AO
3118 }
3119 }
3120
3121 if (gg->global_gotsym != NULL)
3122 global_got_dynindx = gg->global_gotsym->dynindx;
b49e97c9 3123
0f20cc35
DJ
3124 if (TLS_RELOC_P (r_type))
3125 {
3126 struct mips_elf_link_hash_entry *hm
3127 = (struct mips_elf_link_hash_entry *) h;
3128 bfd_vma value = MINUS_ONE;
3129
3130 if ((h->root.type == bfd_link_hash_defined
3131 || h->root.type == bfd_link_hash_defweak)
3132 && h->root.u.def.section->output_section)
3133 value = (h->root.u.def.value
3134 + h->root.u.def.section->output_offset
3135 + h->root.u.def.section->output_section->vma);
3136
91d6fa6a
NC
3137 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3138 r_type, info, hm, value);
0f20cc35
DJ
3139 }
3140 else
3141 {
3142 /* Once we determine the global GOT entry with the lowest dynamic
3143 symbol table index, we must put all dynamic symbols with greater
3144 indices into the GOT. That makes it easy to calculate the GOT
3145 offset. */
3146 BFD_ASSERT (h->dynindx >= global_got_dynindx);
91d6fa6a
NC
3147 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3148 * MIPS_ELF_GOT_SIZE (abfd));
0f20cc35 3149 }
91d6fa6a 3150 BFD_ASSERT (got_index < htab->sgot->size);
b49e97c9 3151
91d6fa6a 3152 return got_index;
b49e97c9
TS
3153}
3154
5c18022e
RS
3155/* Find a GOT page entry that points to within 32KB of VALUE. These
3156 entries are supposed to be placed at small offsets in the GOT, i.e.,
3157 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3158 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3159 offset of the GOT entry from VALUE. */
b49e97c9
TS
3160
3161static bfd_vma
9719ad41 3162mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3163 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3164{
91d6fa6a 3165 bfd_vma page, got_index;
b15e6682 3166 struct mips_got_entry *entry;
b49e97c9 3167
0a44bf69 3168 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3169 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3170 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3171
b15e6682
AO
3172 if (!entry)
3173 return MINUS_ONE;
143d77c5 3174
91d6fa6a 3175 got_index = entry->gotidx;
b49e97c9
TS
3176
3177 if (offsetp)
f4416af6 3178 *offsetp = value - entry->d.address;
b49e97c9 3179
91d6fa6a 3180 return got_index;
b49e97c9
TS
3181}
3182
738e5348 3183/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
0a44bf69
RS
3184 EXTERNAL is true if the relocation was against a global symbol
3185 that has been forced local. */
b49e97c9
TS
3186
3187static bfd_vma
9719ad41 3188mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3189 bfd_vma value, bfd_boolean external)
b49e97c9 3190{
b15e6682 3191 struct mips_got_entry *entry;
b49e97c9 3192
0a44bf69
RS
3193 /* GOT16 relocations against local symbols are followed by a LO16
3194 relocation; those against global symbols are not. Thus if the
3195 symbol was originally local, the GOT16 relocation should load the
3196 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3197 if (! external)
0a44bf69 3198 value = mips_elf_high (value) << 16;
b49e97c9 3199
738e5348
RS
3200 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3201 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3202 same in all cases. */
a8028dd0
RS
3203 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3204 NULL, R_MIPS_GOT16);
b15e6682
AO
3205 if (entry)
3206 return entry->gotidx;
3207 else
3208 return MINUS_ONE;
b49e97c9
TS
3209}
3210
3211/* Returns the offset for the entry at the INDEXth position
3212 in the GOT. */
3213
3214static bfd_vma
a8028dd0 3215mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3216 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3217{
a8028dd0 3218 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3219 asection *sgot;
3220 bfd_vma gp;
3221
a8028dd0
RS
3222 htab = mips_elf_hash_table (info);
3223 sgot = htab->sgot;
f4416af6 3224 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3225 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3226
91d6fa6a 3227 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3228}
3229
0a44bf69
RS
3230/* Create and return a local GOT entry for VALUE, which was calculated
3231 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3232 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3233 instead. */
b49e97c9 3234
b15e6682 3235static struct mips_got_entry *
0a44bf69 3236mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3237 bfd *ibfd, bfd_vma value,
5c18022e 3238 unsigned long r_symndx,
0f20cc35
DJ
3239 struct mips_elf_link_hash_entry *h,
3240 int r_type)
b49e97c9 3241{
b15e6682 3242 struct mips_got_entry entry, **loc;
f4416af6 3243 struct mips_got_info *g;
0a44bf69
RS
3244 struct mips_elf_link_hash_table *htab;
3245
3246 htab = mips_elf_hash_table (info);
b15e6682 3247
f4416af6
AO
3248 entry.abfd = NULL;
3249 entry.symndx = -1;
3250 entry.d.address = value;
0f20cc35 3251 entry.tls_type = 0;
f4416af6 3252
a8028dd0 3253 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
f4416af6
AO
3254 if (g == NULL)
3255 {
a8028dd0 3256 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
f4416af6
AO
3257 BFD_ASSERT (g != NULL);
3258 }
b15e6682 3259
0f20cc35
DJ
3260 /* We might have a symbol, H, if it has been forced local. Use the
3261 global entry then. It doesn't matter whether an entry is local
3262 or global for TLS, since the dynamic linker does not
3263 automatically relocate TLS GOT entries. */
a008ac03 3264 BFD_ASSERT (h == NULL || h->root.forced_local);
0f20cc35
DJ
3265 if (TLS_RELOC_P (r_type))
3266 {
3267 struct mips_got_entry *p;
3268
3269 entry.abfd = ibfd;
3270 if (r_type == R_MIPS_TLS_LDM)
3271 {
3272 entry.tls_type = GOT_TLS_LDM;
3273 entry.symndx = 0;
3274 entry.d.addend = 0;
3275 }
3276 else if (h == NULL)
3277 {
3278 entry.symndx = r_symndx;
3279 entry.d.addend = 0;
3280 }
3281 else
3282 entry.d.h = h;
3283
3284 p = (struct mips_got_entry *)
3285 htab_find (g->got_entries, &entry);
3286
3287 BFD_ASSERT (p);
3288 return p;
3289 }
3290
b15e6682
AO
3291 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3292 INSERT);
3293 if (*loc)
3294 return *loc;
143d77c5 3295
b15e6682 3296 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
0f20cc35 3297 entry.tls_type = 0;
b15e6682
AO
3298
3299 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3300
3301 if (! *loc)
3302 return NULL;
143d77c5 3303
b15e6682
AO
3304 memcpy (*loc, &entry, sizeof entry);
3305
8275b357 3306 if (g->assigned_gotno > g->local_gotno)
b49e97c9 3307 {
f4416af6 3308 (*loc)->gotidx = -1;
b49e97c9
TS
3309 /* We didn't allocate enough space in the GOT. */
3310 (*_bfd_error_handler)
3311 (_("not enough GOT space for local GOT entries"));
3312 bfd_set_error (bfd_error_bad_value);
b15e6682 3313 return NULL;
b49e97c9
TS
3314 }
3315
3316 MIPS_ELF_PUT_WORD (abfd, value,
a8028dd0 3317 (htab->sgot->contents + entry.gotidx));
b15e6682 3318
5c18022e 3319 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3320 if (htab->is_vxworks)
3321 {
3322 Elf_Internal_Rela outrel;
5c18022e 3323 asection *s;
91d6fa6a 3324 bfd_byte *rloc;
0a44bf69 3325 bfd_vma got_address;
0a44bf69
RS
3326
3327 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3328 got_address = (htab->sgot->output_section->vma
3329 + htab->sgot->output_offset
0a44bf69
RS
3330 + entry.gotidx);
3331
91d6fa6a 3332 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3333 outrel.r_offset = got_address;
5c18022e
RS
3334 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3335 outrel.r_addend = value;
91d6fa6a 3336 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3337 }
3338
b15e6682 3339 return *loc;
b49e97c9
TS
3340}
3341
d4596a51
RS
3342/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3343 The number might be exact or a worst-case estimate, depending on how
3344 much information is available to elf_backend_omit_section_dynsym at
3345 the current linking stage. */
3346
3347static bfd_size_type
3348count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3349{
3350 bfd_size_type count;
3351
3352 count = 0;
3353 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3354 {
3355 asection *p;
3356 const struct elf_backend_data *bed;
3357
3358 bed = get_elf_backend_data (output_bfd);
3359 for (p = output_bfd->sections; p ; p = p->next)
3360 if ((p->flags & SEC_EXCLUDE) == 0
3361 && (p->flags & SEC_ALLOC) != 0
3362 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3363 ++count;
3364 }
3365 return count;
3366}
3367
b49e97c9 3368/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3369 appear towards the end. */
b49e97c9 3370
b34976b6 3371static bfd_boolean
d4596a51 3372mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3373{
a8028dd0 3374 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3375 struct mips_elf_hash_sort_data hsd;
3376 struct mips_got_info *g;
b49e97c9 3377
d4596a51
RS
3378 if (elf_hash_table (info)->dynsymcount == 0)
3379 return TRUE;
3380
a8028dd0
RS
3381 htab = mips_elf_hash_table (info);
3382 g = htab->got_info;
d4596a51
RS
3383 if (g == NULL)
3384 return TRUE;
f4416af6 3385
b49e97c9 3386 hsd.low = NULL;
23cc69b6
RS
3387 hsd.max_unref_got_dynindx
3388 = hsd.min_got_dynindx
3389 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3390 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3391 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3392 elf_hash_table (info)),
3393 mips_elf_sort_hash_table_f,
3394 &hsd);
3395
3396 /* There should have been enough room in the symbol table to
44c410de 3397 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3398 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3399 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3400 == elf_hash_table (info)->dynsymcount);
3401 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3402 == g->global_gotno);
b49e97c9
TS
3403
3404 /* Now we know which dynamic symbol has the lowest dynamic symbol
3405 table index in the GOT. */
b49e97c9
TS
3406 g->global_gotsym = hsd.low;
3407
b34976b6 3408 return TRUE;
b49e97c9
TS
3409}
3410
3411/* If H needs a GOT entry, assign it the highest available dynamic
3412 index. Otherwise, assign it the lowest available dynamic
3413 index. */
3414
b34976b6 3415static bfd_boolean
9719ad41 3416mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3417{
9719ad41 3418 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9
TS
3419
3420 if (h->root.root.type == bfd_link_hash_warning)
3421 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3422
3423 /* Symbols without dynamic symbol table entries aren't interesting
3424 at all. */
3425 if (h->root.dynindx == -1)
b34976b6 3426 return TRUE;
b49e97c9 3427
634835ae 3428 switch (h->global_got_area)
f4416af6 3429 {
634835ae
RS
3430 case GGA_NONE:
3431 h->root.dynindx = hsd->max_non_got_dynindx++;
3432 break;
0f20cc35 3433
634835ae 3434 case GGA_NORMAL:
0f20cc35
DJ
3435 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3436
b49e97c9
TS
3437 h->root.dynindx = --hsd->min_got_dynindx;
3438 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3439 break;
3440
3441 case GGA_RELOC_ONLY:
3442 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3443
3444 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3445 hsd->low = (struct elf_link_hash_entry *) h;
3446 h->root.dynindx = hsd->max_unref_got_dynindx++;
3447 break;
b49e97c9
TS
3448 }
3449
b34976b6 3450 return TRUE;
b49e97c9
TS
3451}
3452
3453/* If H is a symbol that needs a global GOT entry, but has a dynamic
3454 symbol table index lower than any we've seen to date, record it for
3455 posterity. */
3456
b34976b6 3457static bfd_boolean
9719ad41
RS
3458mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3459 bfd *abfd, struct bfd_link_info *info,
0f20cc35 3460 unsigned char tls_flag)
b49e97c9 3461{
a8028dd0 3462 struct mips_elf_link_hash_table *htab;
634835ae 3463 struct mips_elf_link_hash_entry *hmips;
f4416af6 3464 struct mips_got_entry entry, **loc;
a8028dd0
RS
3465 struct mips_got_info *g;
3466
3467 htab = mips_elf_hash_table (info);
634835ae 3468 hmips = (struct mips_elf_link_hash_entry *) h;
f4416af6 3469
b49e97c9
TS
3470 /* A global symbol in the GOT must also be in the dynamic symbol
3471 table. */
7c5fcef7
L
3472 if (h->dynindx == -1)
3473 {
3474 switch (ELF_ST_VISIBILITY (h->other))
3475 {
3476 case STV_INTERNAL:
3477 case STV_HIDDEN:
33bb52fb 3478 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3479 break;
3480 }
c152c796 3481 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3482 return FALSE;
7c5fcef7 3483 }
b49e97c9 3484
86324f90 3485 /* Make sure we have a GOT to put this entry into. */
a8028dd0 3486 g = htab->got_info;
86324f90
EC
3487 BFD_ASSERT (g != NULL);
3488
f4416af6
AO
3489 entry.abfd = abfd;
3490 entry.symndx = -1;
3491 entry.d.h = (struct mips_elf_link_hash_entry *) h;
0f20cc35 3492 entry.tls_type = 0;
f4416af6
AO
3493
3494 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3495 INSERT);
3496
b49e97c9
TS
3497 /* If we've already marked this entry as needing GOT space, we don't
3498 need to do it again. */
f4416af6 3499 if (*loc)
0f20cc35
DJ
3500 {
3501 (*loc)->tls_type |= tls_flag;
3502 return TRUE;
3503 }
f4416af6
AO
3504
3505 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3506
3507 if (! *loc)
3508 return FALSE;
143d77c5 3509
f4416af6 3510 entry.gotidx = -1;
0f20cc35
DJ
3511 entry.tls_type = tls_flag;
3512
f4416af6
AO
3513 memcpy (*loc, &entry, sizeof entry);
3514
0f20cc35 3515 if (tls_flag == 0)
634835ae 3516 hmips->global_got_area = GGA_NORMAL;
b49e97c9 3517
b34976b6 3518 return TRUE;
b49e97c9 3519}
f4416af6
AO
3520
3521/* Reserve space in G for a GOT entry containing the value of symbol
3522 SYMNDX in input bfd ABDF, plus ADDEND. */
3523
3524static bfd_boolean
9719ad41 3525mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
a8028dd0 3526 struct bfd_link_info *info,
0f20cc35 3527 unsigned char tls_flag)
f4416af6 3528{
a8028dd0
RS
3529 struct mips_elf_link_hash_table *htab;
3530 struct mips_got_info *g;
f4416af6
AO
3531 struct mips_got_entry entry, **loc;
3532
a8028dd0
RS
3533 htab = mips_elf_hash_table (info);
3534 g = htab->got_info;
3535 BFD_ASSERT (g != NULL);
3536
f4416af6
AO
3537 entry.abfd = abfd;
3538 entry.symndx = symndx;
3539 entry.d.addend = addend;
0f20cc35 3540 entry.tls_type = tls_flag;
f4416af6
AO
3541 loc = (struct mips_got_entry **)
3542 htab_find_slot (g->got_entries, &entry, INSERT);
3543
3544 if (*loc)
0f20cc35
DJ
3545 {
3546 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3547 {
3548 g->tls_gotno += 2;
3549 (*loc)->tls_type |= tls_flag;
3550 }
3551 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3552 {
3553 g->tls_gotno += 1;
3554 (*loc)->tls_type |= tls_flag;
3555 }
3556 return TRUE;
3557 }
f4416af6 3558
0f20cc35
DJ
3559 if (tls_flag != 0)
3560 {
3561 entry.gotidx = -1;
3562 entry.tls_type = tls_flag;
3563 if (tls_flag == GOT_TLS_IE)
3564 g->tls_gotno += 1;
3565 else if (tls_flag == GOT_TLS_GD)
3566 g->tls_gotno += 2;
3567 else if (g->tls_ldm_offset == MINUS_ONE)
3568 {
3569 g->tls_ldm_offset = MINUS_TWO;
3570 g->tls_gotno += 2;
3571 }
3572 }
3573 else
3574 {
3575 entry.gotidx = g->local_gotno++;
3576 entry.tls_type = 0;
3577 }
f4416af6
AO
3578
3579 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3580
3581 if (! *loc)
3582 return FALSE;
143d77c5 3583
f4416af6
AO
3584 memcpy (*loc, &entry, sizeof entry);
3585
3586 return TRUE;
3587}
c224138d
RS
3588
3589/* Return the maximum number of GOT page entries required for RANGE. */
3590
3591static bfd_vma
3592mips_elf_pages_for_range (const struct mips_got_page_range *range)
3593{
3594 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3595}
3596
3a3b6725 3597/* Record that ABFD has a page relocation against symbol SYMNDX and
a8028dd0
RS
3598 that ADDEND is the addend for that relocation.
3599
3600 This function creates an upper bound on the number of GOT slots
3601 required; no attempt is made to combine references to non-overridable
3602 global symbols across multiple input files. */
c224138d
RS
3603
3604static bfd_boolean
a8028dd0
RS
3605mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3606 long symndx, bfd_signed_vma addend)
c224138d 3607{
a8028dd0
RS
3608 struct mips_elf_link_hash_table *htab;
3609 struct mips_got_info *g;
c224138d
RS
3610 struct mips_got_page_entry lookup, *entry;
3611 struct mips_got_page_range **range_ptr, *range;
3612 bfd_vma old_pages, new_pages;
3613 void **loc;
3614
a8028dd0
RS
3615 htab = mips_elf_hash_table (info);
3616 g = htab->got_info;
3617 BFD_ASSERT (g != NULL);
3618
c224138d
RS
3619 /* Find the mips_got_page_entry hash table entry for this symbol. */
3620 lookup.abfd = abfd;
3621 lookup.symndx = symndx;
3622 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3623 if (loc == NULL)
3624 return FALSE;
3625
3626 /* Create a mips_got_page_entry if this is the first time we've
3627 seen the symbol. */
3628 entry = (struct mips_got_page_entry *) *loc;
3629 if (!entry)
3630 {
3631 entry = bfd_alloc (abfd, sizeof (*entry));
3632 if (!entry)
3633 return FALSE;
3634
3635 entry->abfd = abfd;
3636 entry->symndx = symndx;
3637 entry->ranges = NULL;
3638 entry->num_pages = 0;
3639 *loc = entry;
3640 }
3641
3642 /* Skip over ranges whose maximum extent cannot share a page entry
3643 with ADDEND. */
3644 range_ptr = &entry->ranges;
3645 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3646 range_ptr = &(*range_ptr)->next;
3647
3648 /* If we scanned to the end of the list, or found a range whose
3649 minimum extent cannot share a page entry with ADDEND, create
3650 a new singleton range. */
3651 range = *range_ptr;
3652 if (!range || addend < range->min_addend - 0xffff)
3653 {
3654 range = bfd_alloc (abfd, sizeof (*range));
3655 if (!range)
3656 return FALSE;
3657
3658 range->next = *range_ptr;
3659 range->min_addend = addend;
3660 range->max_addend = addend;
3661
3662 *range_ptr = range;
3663 entry->num_pages++;
3664 g->page_gotno++;
3665 return TRUE;
3666 }
3667
3668 /* Remember how many pages the old range contributed. */
3669 old_pages = mips_elf_pages_for_range (range);
3670
3671 /* Update the ranges. */
3672 if (addend < range->min_addend)
3673 range->min_addend = addend;
3674 else if (addend > range->max_addend)
3675 {
3676 if (range->next && addend >= range->next->min_addend - 0xffff)
3677 {
3678 old_pages += mips_elf_pages_for_range (range->next);
3679 range->max_addend = range->next->max_addend;
3680 range->next = range->next->next;
3681 }
3682 else
3683 range->max_addend = addend;
3684 }
3685
3686 /* Record any change in the total estimate. */
3687 new_pages = mips_elf_pages_for_range (range);
3688 if (old_pages != new_pages)
3689 {
3690 entry->num_pages += new_pages - old_pages;
3691 g->page_gotno += new_pages - old_pages;
3692 }
3693
3694 return TRUE;
3695}
33bb52fb
RS
3696
3697/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3698
3699static void
3700mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3701 unsigned int n)
3702{
3703 asection *s;
3704 struct mips_elf_link_hash_table *htab;
3705
3706 htab = mips_elf_hash_table (info);
3707 s = mips_elf_rel_dyn_section (info, FALSE);
3708 BFD_ASSERT (s != NULL);
3709
3710 if (htab->is_vxworks)
3711 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3712 else
3713 {
3714 if (s->size == 0)
3715 {
3716 /* Make room for a null element. */
3717 s->size += MIPS_ELF_REL_SIZE (abfd);
3718 ++s->reloc_count;
3719 }
3720 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3721 }
3722}
3723\f
3724/* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3725 if the GOT entry is for an indirect or warning symbol. */
3726
3727static int
3728mips_elf_check_recreate_got (void **entryp, void *data)
3729{
3730 struct mips_got_entry *entry;
3731 bfd_boolean *must_recreate;
3732
3733 entry = (struct mips_got_entry *) *entryp;
3734 must_recreate = (bfd_boolean *) data;
3735 if (entry->abfd != NULL && entry->symndx == -1)
3736 {
3737 struct mips_elf_link_hash_entry *h;
3738
3739 h = entry->d.h;
3740 if (h->root.root.type == bfd_link_hash_indirect
3741 || h->root.root.type == bfd_link_hash_warning)
3742 {
3743 *must_recreate = TRUE;
3744 return 0;
3745 }
3746 }
3747 return 1;
3748}
3749
3750/* A htab_traverse callback for GOT entries. Add all entries to
3751 hash table *DATA, converting entries for indirect and warning
3752 symbols into entries for the target symbol. Set *DATA to null
3753 on error. */
3754
3755static int
3756mips_elf_recreate_got (void **entryp, void *data)
3757{
3758 htab_t *new_got;
3759 struct mips_got_entry *entry;
3760 void **slot;
3761
3762 new_got = (htab_t *) data;
3763 entry = (struct mips_got_entry *) *entryp;
3764 if (entry->abfd != NULL && entry->symndx == -1)
3765 {
3766 struct mips_elf_link_hash_entry *h;
3767
3768 h = entry->d.h;
3769 while (h->root.root.type == bfd_link_hash_indirect
3770 || h->root.root.type == bfd_link_hash_warning)
634835ae
RS
3771 {
3772 BFD_ASSERT (h->global_got_area == GGA_NONE);
3773 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3774 }
33bb52fb
RS
3775 entry->d.h = h;
3776 }
3777 slot = htab_find_slot (*new_got, entry, INSERT);
3778 if (slot == NULL)
3779 {
3780 *new_got = NULL;
3781 return 0;
3782 }
3783 if (*slot == NULL)
3784 *slot = entry;
3785 else
3786 free (entry);
3787 return 1;
3788}
3789
3790/* If any entries in G->got_entries are for indirect or warning symbols,
3791 replace them with entries for the target symbol. */
3792
3793static bfd_boolean
3794mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3795{
3796 bfd_boolean must_recreate;
3797 htab_t new_got;
3798
3799 must_recreate = FALSE;
3800 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3801 if (must_recreate)
3802 {
3803 new_got = htab_create (htab_size (g->got_entries),
3804 mips_elf_got_entry_hash,
3805 mips_elf_got_entry_eq, NULL);
3806 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3807 if (new_got == NULL)
3808 return FALSE;
3809
3810 /* Each entry in g->got_entries has either been copied to new_got
3811 or freed. Now delete the hash table itself. */
3812 htab_delete (g->got_entries);
3813 g->got_entries = new_got;
3814 }
3815 return TRUE;
3816}
3817
634835ae 3818/* A mips_elf_link_hash_traverse callback for which DATA points
d4596a51 3819 to a mips_got_info. Count the number of type (3) entries. */
33bb52fb
RS
3820
3821static int
d4596a51 3822mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb
RS
3823{
3824 struct mips_got_info *g;
3825
3826 g = (struct mips_got_info *) data;
d4596a51 3827 if (h->global_got_area != GGA_NONE)
33bb52fb 3828 {
d4596a51
RS
3829 if (h->root.forced_local || h->root.dynindx == -1)
3830 {
3831 /* We no longer need this entry if it was only used for
3832 relocations; those relocations will be against the
3833 null or section symbol instead of H. */
3834 if (h->global_got_area != GGA_RELOC_ONLY)
3835 g->local_gotno++;
3836 h->global_got_area = GGA_NONE;
3837 }
3838 else
23cc69b6
RS
3839 {
3840 g->global_gotno++;
3841 if (h->global_got_area == GGA_RELOC_ONLY)
3842 g->reloc_only_gotno++;
3843 }
33bb52fb
RS
3844 }
3845 return 1;
3846}
f4416af6
AO
3847\f
3848/* Compute the hash value of the bfd in a bfd2got hash entry. */
3849
3850static hashval_t
9719ad41 3851mips_elf_bfd2got_entry_hash (const void *entry_)
f4416af6
AO
3852{
3853 const struct mips_elf_bfd2got_hash *entry
3854 = (struct mips_elf_bfd2got_hash *)entry_;
3855
3856 return entry->bfd->id;
3857}
3858
3859/* Check whether two hash entries have the same bfd. */
3860
3861static int
9719ad41 3862mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3863{
3864 const struct mips_elf_bfd2got_hash *e1
3865 = (const struct mips_elf_bfd2got_hash *)entry1;
3866 const struct mips_elf_bfd2got_hash *e2
3867 = (const struct mips_elf_bfd2got_hash *)entry2;
3868
3869 return e1->bfd == e2->bfd;
3870}
3871
bad36eac 3872/* In a multi-got link, determine the GOT to be used for IBFD. G must
f4416af6
AO
3873 be the master GOT data. */
3874
3875static struct mips_got_info *
9719ad41 3876mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
3877{
3878 struct mips_elf_bfd2got_hash e, *p;
3879
3880 if (! g->bfd2got)
3881 return g;
3882
3883 e.bfd = ibfd;
9719ad41 3884 p = htab_find (g->bfd2got, &e);
f4416af6
AO
3885 return p ? p->g : NULL;
3886}
3887
c224138d
RS
3888/* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3889 Return NULL if an error occured. */
f4416af6 3890
c224138d
RS
3891static struct mips_got_info *
3892mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3893 bfd *input_bfd)
f4416af6 3894{
f4416af6 3895 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
c224138d 3896 struct mips_got_info *g;
f4416af6 3897 void **bfdgotp;
143d77c5 3898
c224138d 3899 bfdgot_entry.bfd = input_bfd;
f4416af6 3900 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
c224138d 3901 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
f4416af6 3902
c224138d 3903 if (bfdgot == NULL)
f4416af6 3904 {
c224138d
RS
3905 bfdgot = ((struct mips_elf_bfd2got_hash *)
3906 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
f4416af6 3907 if (bfdgot == NULL)
c224138d 3908 return NULL;
f4416af6
AO
3909
3910 *bfdgotp = bfdgot;
3911
c224138d
RS
3912 g = ((struct mips_got_info *)
3913 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
f4416af6 3914 if (g == NULL)
c224138d
RS
3915 return NULL;
3916
3917 bfdgot->bfd = input_bfd;
3918 bfdgot->g = g;
f4416af6
AO
3919
3920 g->global_gotsym = NULL;
3921 g->global_gotno = 0;
23cc69b6 3922 g->reloc_only_gotno = 0;
f4416af6 3923 g->local_gotno = 0;
c224138d 3924 g->page_gotno = 0;
f4416af6 3925 g->assigned_gotno = -1;
0f20cc35
DJ
3926 g->tls_gotno = 0;
3927 g->tls_assigned_gotno = 0;
3928 g->tls_ldm_offset = MINUS_ONE;
f4416af6 3929 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
9719ad41 3930 mips_elf_multi_got_entry_eq, NULL);
f4416af6 3931 if (g->got_entries == NULL)
c224138d
RS
3932 return NULL;
3933
3934 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3935 mips_got_page_entry_eq, NULL);
3936 if (g->got_page_entries == NULL)
3937 return NULL;
f4416af6
AO
3938
3939 g->bfd2got = NULL;
3940 g->next = NULL;
3941 }
3942
c224138d
RS
3943 return bfdgot->g;
3944}
3945
3946/* A htab_traverse callback for the entries in the master got.
3947 Create one separate got for each bfd that has entries in the global
3948 got, such that we can tell how many local and global entries each
3949 bfd requires. */
3950
3951static int
3952mips_elf_make_got_per_bfd (void **entryp, void *p)
3953{
3954 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3955 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3956 struct mips_got_info *g;
3957
3958 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3959 if (g == NULL)
3960 {
3961 arg->obfd = NULL;
3962 return 0;
3963 }
3964
f4416af6
AO
3965 /* Insert the GOT entry in the bfd's got entry hash table. */
3966 entryp = htab_find_slot (g->got_entries, entry, INSERT);
3967 if (*entryp != NULL)
3968 return 1;
143d77c5 3969
f4416af6
AO
3970 *entryp = entry;
3971
0f20cc35
DJ
3972 if (entry->tls_type)
3973 {
3974 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3975 g->tls_gotno += 2;
3976 if (entry->tls_type & GOT_TLS_IE)
3977 g->tls_gotno += 1;
3978 }
33bb52fb 3979 else if (entry->symndx >= 0 || entry->d.h->root.forced_local)
f4416af6
AO
3980 ++g->local_gotno;
3981 else
3982 ++g->global_gotno;
3983
3984 return 1;
3985}
3986
c224138d
RS
3987/* A htab_traverse callback for the page entries in the master got.
3988 Associate each page entry with the bfd's got. */
3989
3990static int
3991mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
3992{
3993 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
3994 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
3995 struct mips_got_info *g;
3996
3997 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3998 if (g == NULL)
3999 {
4000 arg->obfd = NULL;
4001 return 0;
4002 }
4003
4004 /* Insert the GOT entry in the bfd's got entry hash table. */
4005 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4006 if (*entryp != NULL)
4007 return 1;
4008
4009 *entryp = entry;
4010 g->page_gotno += entry->num_pages;
4011 return 1;
4012}
4013
4014/* Consider merging the got described by BFD2GOT with TO, using the
4015 information given by ARG. Return -1 if this would lead to overflow,
4016 1 if they were merged successfully, and 0 if a merge failed due to
4017 lack of memory. (These values are chosen so that nonnegative return
4018 values can be returned by a htab_traverse callback.) */
4019
4020static int
4021mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4022 struct mips_got_info *to,
4023 struct mips_elf_got_per_bfd_arg *arg)
4024{
4025 struct mips_got_info *from = bfd2got->g;
4026 unsigned int estimate;
4027
4028 /* Work out how many page entries we would need for the combined GOT. */
4029 estimate = arg->max_pages;
4030 if (estimate >= from->page_gotno + to->page_gotno)
4031 estimate = from->page_gotno + to->page_gotno;
4032
4033 /* And conservatively estimate how many local, global and TLS entries
4034 would be needed. */
4035 estimate += (from->local_gotno
4036 + from->global_gotno
4037 + from->tls_gotno
4038 + to->local_gotno
4039 + to->global_gotno
4040 + to->tls_gotno);
4041
4042 /* Bail out if the combined GOT might be too big. */
4043 if (estimate > arg->max_count)
4044 return -1;
4045
4046 /* Commit to the merge. Record that TO is now the bfd for this got. */
4047 bfd2got->g = to;
4048
4049 /* Transfer the bfd's got information from FROM to TO. */
4050 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4051 if (arg->obfd == NULL)
4052 return 0;
4053
4054 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4055 if (arg->obfd == NULL)
4056 return 0;
4057
4058 /* We don't have to worry about releasing memory of the actual
4059 got entries, since they're all in the master got_entries hash
4060 table anyway. */
4061 htab_delete (from->got_entries);
4062 htab_delete (from->got_page_entries);
4063 return 1;
4064}
4065
f4416af6
AO
4066/* Attempt to merge gots of different input bfds. Try to use as much
4067 as possible of the primary got, since it doesn't require explicit
4068 dynamic relocations, but don't use bfds that would reference global
4069 symbols out of the addressable range. Failing the primary got,
4070 attempt to merge with the current got, or finish the current got
4071 and then make make the new got current. */
4072
4073static int
9719ad41 4074mips_elf_merge_gots (void **bfd2got_, void *p)
f4416af6
AO
4075{
4076 struct mips_elf_bfd2got_hash *bfd2got
4077 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4078 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
c224138d
RS
4079 struct mips_got_info *g;
4080 unsigned int estimate;
4081 int result;
4082
4083 g = bfd2got->g;
4084
4085 /* Work out the number of page, local and TLS entries. */
4086 estimate = arg->max_pages;
4087 if (estimate > g->page_gotno)
4088 estimate = g->page_gotno;
4089 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4090
4091 /* We place TLS GOT entries after both locals and globals. The globals
4092 for the primary GOT may overflow the normal GOT size limit, so be
4093 sure not to merge a GOT which requires TLS with the primary GOT in that
4094 case. This doesn't affect non-primary GOTs. */
c224138d 4095 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4096
c224138d 4097 if (estimate <= arg->max_count)
f4416af6 4098 {
c224138d
RS
4099 /* If we don't have a primary GOT, use it as
4100 a starting point for the primary GOT. */
4101 if (!arg->primary)
4102 {
4103 arg->primary = bfd2got->g;
4104 return 1;
4105 }
f4416af6 4106
c224138d
RS
4107 /* Try merging with the primary GOT. */
4108 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4109 if (result >= 0)
4110 return result;
f4416af6 4111 }
c224138d 4112
f4416af6 4113 /* If we can merge with the last-created got, do it. */
c224138d 4114 if (arg->current)
f4416af6 4115 {
c224138d
RS
4116 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4117 if (result >= 0)
4118 return result;
f4416af6 4119 }
c224138d 4120
f4416af6
AO
4121 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4122 fits; if it turns out that it doesn't, we'll get relocation
4123 overflows anyway. */
c224138d
RS
4124 g->next = arg->current;
4125 arg->current = g;
0f20cc35
DJ
4126
4127 return 1;
4128}
4129
ead49a57
RS
4130/* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4131 is null iff there is just a single GOT. */
0f20cc35
DJ
4132
4133static int
4134mips_elf_initialize_tls_index (void **entryp, void *p)
4135{
4136 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4137 struct mips_got_info *g = p;
ead49a57 4138 bfd_vma next_index;
cbf2cba4 4139 unsigned char tls_type;
0f20cc35
DJ
4140
4141 /* We're only interested in TLS symbols. */
4142 if (entry->tls_type == 0)
4143 return 1;
4144
ead49a57
RS
4145 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4146
4147 if (entry->symndx == -1 && g->next == NULL)
0f20cc35 4148 {
ead49a57
RS
4149 /* A type (3) got entry in the single-GOT case. We use the symbol's
4150 hash table entry to track its index. */
4151 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4152 return 1;
4153 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4154 entry->d.h->tls_got_offset = next_index;
cbf2cba4 4155 tls_type = entry->d.h->tls_type;
ead49a57
RS
4156 }
4157 else
4158 {
4159 if (entry->tls_type & GOT_TLS_LDM)
0f20cc35 4160 {
ead49a57
RS
4161 /* There are separate mips_got_entry objects for each input bfd
4162 that requires an LDM entry. Make sure that all LDM entries in
4163 a GOT resolve to the same index. */
4164 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4005427f 4165 {
ead49a57 4166 entry->gotidx = g->tls_ldm_offset;
4005427f
RS
4167 return 1;
4168 }
ead49a57 4169 g->tls_ldm_offset = next_index;
0f20cc35 4170 }
ead49a57 4171 entry->gotidx = next_index;
cbf2cba4 4172 tls_type = entry->tls_type;
f4416af6
AO
4173 }
4174
ead49a57 4175 /* Account for the entries we've just allocated. */
cbf2cba4 4176 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
0f20cc35 4177 g->tls_assigned_gotno += 2;
cbf2cba4 4178 if (tls_type & GOT_TLS_IE)
0f20cc35
DJ
4179 g->tls_assigned_gotno += 1;
4180
f4416af6
AO
4181 return 1;
4182}
4183
4184/* If passed a NULL mips_got_info in the argument, set the marker used
4185 to tell whether a global symbol needs a got entry (in the primary
4186 got) to the given VALUE.
4187
4188 If passed a pointer G to a mips_got_info in the argument (it must
4189 not be the primary GOT), compute the offset from the beginning of
4190 the (primary) GOT section to the entry in G corresponding to the
4191 global symbol. G's assigned_gotno must contain the index of the
4192 first available global GOT entry in G. VALUE must contain the size
4193 of a GOT entry in bytes. For each global GOT entry that requires a
4194 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4cc11e76 4195 marked as not eligible for lazy resolution through a function
f4416af6
AO
4196 stub. */
4197static int
9719ad41 4198mips_elf_set_global_got_offset (void **entryp, void *p)
f4416af6
AO
4199{
4200 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4201 struct mips_elf_set_global_got_offset_arg *arg
4202 = (struct mips_elf_set_global_got_offset_arg *)p;
4203 struct mips_got_info *g = arg->g;
4204
0f20cc35
DJ
4205 if (g && entry->tls_type != GOT_NORMAL)
4206 arg->needed_relocs +=
4207 mips_tls_got_relocs (arg->info, entry->tls_type,
4208 entry->symndx == -1 ? &entry->d.h->root : NULL);
4209
634835ae
RS
4210 if (entry->abfd != NULL
4211 && entry->symndx == -1
4212 && entry->d.h->global_got_area != GGA_NONE)
f4416af6
AO
4213 {
4214 if (g)
4215 {
4216 BFD_ASSERT (g->global_gotsym == NULL);
4217
4218 entry->gotidx = arg->value * (long) g->assigned_gotno++;
f4416af6
AO
4219 if (arg->info->shared
4220 || (elf_hash_table (arg->info)->dynamic_sections_created
f5385ebf
AM
4221 && entry->d.h->root.def_dynamic
4222 && !entry->d.h->root.def_regular))
f4416af6
AO
4223 ++arg->needed_relocs;
4224 }
4225 else
634835ae 4226 entry->d.h->global_got_area = arg->value;
f4416af6
AO
4227 }
4228
4229 return 1;
4230}
4231
33bb52fb
RS
4232/* A htab_traverse callback for GOT entries for which DATA is the
4233 bfd_link_info. Forbid any global symbols from having traditional
4234 lazy-binding stubs. */
4235
0626d451 4236static int
33bb52fb 4237mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4238{
33bb52fb
RS
4239 struct bfd_link_info *info;
4240 struct mips_elf_link_hash_table *htab;
4241 struct mips_got_entry *entry;
0626d451 4242
33bb52fb
RS
4243 entry = (struct mips_got_entry *) *entryp;
4244 info = (struct bfd_link_info *) data;
4245 htab = mips_elf_hash_table (info);
0626d451
RS
4246 if (entry->abfd != NULL
4247 && entry->symndx == -1
33bb52fb 4248 && entry->d.h->needs_lazy_stub)
f4416af6 4249 {
33bb52fb
RS
4250 entry->d.h->needs_lazy_stub = FALSE;
4251 htab->lazy_stub_count--;
f4416af6 4252 }
143d77c5 4253
f4416af6
AO
4254 return 1;
4255}
4256
f4416af6
AO
4257/* Return the offset of an input bfd IBFD's GOT from the beginning of
4258 the primary GOT. */
4259static bfd_vma
9719ad41 4260mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
4261{
4262 if (g->bfd2got == NULL)
4263 return 0;
4264
4265 g = mips_elf_got_for_ibfd (g, ibfd);
4266 if (! g)
4267 return 0;
4268
4269 BFD_ASSERT (g->next);
4270
4271 g = g->next;
143d77c5 4272
0f20cc35
DJ
4273 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4274 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4275}
4276
4277/* Turn a single GOT that is too big for 16-bit addressing into
4278 a sequence of GOTs, each one 16-bit addressable. */
4279
4280static bfd_boolean
9719ad41 4281mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4282 asection *got, bfd_size_type pages)
f4416af6 4283{
a8028dd0 4284 struct mips_elf_link_hash_table *htab;
f4416af6
AO
4285 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4286 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
a8028dd0 4287 struct mips_got_info *g, *gg;
33bb52fb
RS
4288 unsigned int assign, needed_relocs;
4289 bfd *dynobj;
f4416af6 4290
33bb52fb 4291 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
4292 htab = mips_elf_hash_table (info);
4293 g = htab->got_info;
f4416af6 4294 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
9719ad41 4295 mips_elf_bfd2got_entry_eq, NULL);
f4416af6
AO
4296 if (g->bfd2got == NULL)
4297 return FALSE;
4298
4299 got_per_bfd_arg.bfd2got = g->bfd2got;
4300 got_per_bfd_arg.obfd = abfd;
4301 got_per_bfd_arg.info = info;
4302
4303 /* Count how many GOT entries each input bfd requires, creating a
4304 map from bfd to got info while at that. */
f4416af6
AO
4305 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4306 if (got_per_bfd_arg.obfd == NULL)
4307 return FALSE;
4308
c224138d
RS
4309 /* Also count how many page entries each input bfd requires. */
4310 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4311 &got_per_bfd_arg);
4312 if (got_per_bfd_arg.obfd == NULL)
4313 return FALSE;
4314
f4416af6
AO
4315 got_per_bfd_arg.current = NULL;
4316 got_per_bfd_arg.primary = NULL;
0a44bf69 4317 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4318 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4319 - htab->reserved_gotno);
c224138d 4320 got_per_bfd_arg.max_pages = pages;
0f20cc35
DJ
4321 /* The number of globals that will be included in the primary GOT.
4322 See the calls to mips_elf_set_global_got_offset below for more
4323 information. */
4324 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4325
4326 /* Try to merge the GOTs of input bfds together, as long as they
4327 don't seem to exceed the maximum GOT size, choosing one of them
4328 to be the primary GOT. */
4329 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4330 if (got_per_bfd_arg.obfd == NULL)
4331 return FALSE;
4332
0f20cc35 4333 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6
AO
4334 if (got_per_bfd_arg.primary == NULL)
4335 {
4336 g->next = (struct mips_got_info *)
4337 bfd_alloc (abfd, sizeof (struct mips_got_info));
4338 if (g->next == NULL)
4339 return FALSE;
4340
4341 g->next->global_gotsym = NULL;
4342 g->next->global_gotno = 0;
23cc69b6 4343 g->next->reloc_only_gotno = 0;
f4416af6 4344 g->next->local_gotno = 0;
c224138d 4345 g->next->page_gotno = 0;
0f20cc35 4346 g->next->tls_gotno = 0;
f4416af6 4347 g->next->assigned_gotno = 0;
0f20cc35
DJ
4348 g->next->tls_assigned_gotno = 0;
4349 g->next->tls_ldm_offset = MINUS_ONE;
f4416af6
AO
4350 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4351 mips_elf_multi_got_entry_eq,
9719ad41 4352 NULL);
f4416af6
AO
4353 if (g->next->got_entries == NULL)
4354 return FALSE;
c224138d
RS
4355 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4356 mips_got_page_entry_eq,
4357 NULL);
4358 if (g->next->got_page_entries == NULL)
4359 return FALSE;
f4416af6
AO
4360 g->next->bfd2got = NULL;
4361 }
4362 else
4363 g->next = got_per_bfd_arg.primary;
4364 g->next->next = got_per_bfd_arg.current;
4365
4366 /* GG is now the master GOT, and G is the primary GOT. */
4367 gg = g;
4368 g = g->next;
4369
4370 /* Map the output bfd to the primary got. That's what we're going
4371 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4372 didn't mark in check_relocs, and we want a quick way to find it.
4373 We can't just use gg->next because we're going to reverse the
4374 list. */
4375 {
4376 struct mips_elf_bfd2got_hash *bfdgot;
4377 void **bfdgotp;
143d77c5 4378
f4416af6
AO
4379 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4380 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4381
4382 if (bfdgot == NULL)
4383 return FALSE;
4384
4385 bfdgot->bfd = abfd;
4386 bfdgot->g = g;
4387 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4388
4389 BFD_ASSERT (*bfdgotp == NULL);
4390 *bfdgotp = bfdgot;
4391 }
4392
634835ae
RS
4393 /* Every symbol that is referenced in a dynamic relocation must be
4394 present in the primary GOT, so arrange for them to appear after
4395 those that are actually referenced. */
23cc69b6 4396 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4397 g->global_gotno = gg->global_gotno;
f4416af6 4398
f4416af6 4399 set_got_offset_arg.g = NULL;
634835ae 4400 set_got_offset_arg.value = GGA_RELOC_ONLY;
f4416af6
AO
4401 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4402 &set_got_offset_arg);
634835ae 4403 set_got_offset_arg.value = GGA_NORMAL;
f4416af6
AO
4404 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4405 &set_got_offset_arg);
f4416af6
AO
4406
4407 /* Now go through the GOTs assigning them offset ranges.
4408 [assigned_gotno, local_gotno[ will be set to the range of local
4409 entries in each GOT. We can then compute the end of a GOT by
4410 adding local_gotno to global_gotno. We reverse the list and make
4411 it circular since then we'll be able to quickly compute the
4412 beginning of a GOT, by computing the end of its predecessor. To
4413 avoid special cases for the primary GOT, while still preserving
4414 assertions that are valid for both single- and multi-got links,
4415 we arrange for the main got struct to have the right number of
4416 global entries, but set its local_gotno such that the initial
4417 offset of the primary GOT is zero. Remember that the primary GOT
4418 will become the last item in the circular linked list, so it
4419 points back to the master GOT. */
4420 gg->local_gotno = -g->global_gotno;
4421 gg->global_gotno = g->global_gotno;
0f20cc35 4422 gg->tls_gotno = 0;
f4416af6
AO
4423 assign = 0;
4424 gg->next = gg;
4425
4426 do
4427 {
4428 struct mips_got_info *gn;
4429
861fb55a 4430 assign += htab->reserved_gotno;
f4416af6 4431 g->assigned_gotno = assign;
c224138d
RS
4432 g->local_gotno += assign;
4433 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4434 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4435
ead49a57
RS
4436 /* Take g out of the direct list, and push it onto the reversed
4437 list that gg points to. g->next is guaranteed to be nonnull after
4438 this operation, as required by mips_elf_initialize_tls_index. */
4439 gn = g->next;
4440 g->next = gg->next;
4441 gg->next = g;
4442
0f20cc35
DJ
4443 /* Set up any TLS entries. We always place the TLS entries after
4444 all non-TLS entries. */
4445 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4446 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
f4416af6 4447
ead49a57 4448 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4449 g = gn;
0626d451 4450
33bb52fb
RS
4451 /* Forbid global symbols in every non-primary GOT from having
4452 lazy-binding stubs. */
0626d451 4453 if (g)
33bb52fb 4454 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4455 }
4456 while (g);
4457
eea6121a 4458 got->size = (gg->next->local_gotno
33bb52fb
RS
4459 + gg->next->global_gotno
4460 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4461
4462 needed_relocs = 0;
4463 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4464 set_got_offset_arg.info = info;
4465 for (g = gg->next; g && g->next != gg; g = g->next)
4466 {
4467 unsigned int save_assign;
4468
4469 /* Assign offsets to global GOT entries. */
4470 save_assign = g->assigned_gotno;
4471 g->assigned_gotno = g->local_gotno;
4472 set_got_offset_arg.g = g;
4473 set_got_offset_arg.needed_relocs = 0;
4474 htab_traverse (g->got_entries,
4475 mips_elf_set_global_got_offset,
4476 &set_got_offset_arg);
4477 needed_relocs += set_got_offset_arg.needed_relocs;
4478 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4479
4480 g->assigned_gotno = save_assign;
4481 if (info->shared)
4482 {
4483 needed_relocs += g->local_gotno - g->assigned_gotno;
4484 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4485 + g->next->global_gotno
4486 + g->next->tls_gotno
861fb55a 4487 + htab->reserved_gotno);
33bb52fb
RS
4488 }
4489 }
4490
4491 if (needed_relocs)
4492 mips_elf_allocate_dynamic_relocations (dynobj, info,
4493 needed_relocs);
143d77c5 4494
f4416af6
AO
4495 return TRUE;
4496}
143d77c5 4497
b49e97c9
TS
4498\f
4499/* Returns the first relocation of type r_type found, beginning with
4500 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4501
4502static const Elf_Internal_Rela *
9719ad41
RS
4503mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4504 const Elf_Internal_Rela *relocation,
4505 const Elf_Internal_Rela *relend)
b49e97c9 4506{
c000e262
TS
4507 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4508
b49e97c9
TS
4509 while (relocation < relend)
4510 {
c000e262
TS
4511 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4512 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4513 return relocation;
4514
4515 ++relocation;
4516 }
4517
4518 /* We didn't find it. */
b49e97c9
TS
4519 return NULL;
4520}
4521
4522/* Return whether a relocation is against a local symbol. */
4523
b34976b6 4524static bfd_boolean
9719ad41
RS
4525mips_elf_local_relocation_p (bfd *input_bfd,
4526 const Elf_Internal_Rela *relocation,
4527 asection **local_sections,
4528 bfd_boolean check_forced)
b49e97c9
TS
4529{
4530 unsigned long r_symndx;
4531 Elf_Internal_Shdr *symtab_hdr;
4532 struct mips_elf_link_hash_entry *h;
4533 size_t extsymoff;
4534
4535 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4536 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4537 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4538
4539 if (r_symndx < extsymoff)
b34976b6 4540 return TRUE;
b49e97c9 4541 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4542 return TRUE;
b49e97c9
TS
4543
4544 if (check_forced)
4545 {
4546 /* Look up the hash table to check whether the symbol
4547 was forced local. */
4548 h = (struct mips_elf_link_hash_entry *)
4549 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
4550 /* Find the real hash-table entry for this symbol. */
4551 while (h->root.root.type == bfd_link_hash_indirect
4552 || h->root.root.type == bfd_link_hash_warning)
4553 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
f5385ebf 4554 if (h->root.forced_local)
b34976b6 4555 return TRUE;
b49e97c9
TS
4556 }
4557
b34976b6 4558 return FALSE;
b49e97c9
TS
4559}
4560\f
4561/* Sign-extend VALUE, which has the indicated number of BITS. */
4562
a7ebbfdf 4563bfd_vma
9719ad41 4564_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4565{
4566 if (value & ((bfd_vma) 1 << (bits - 1)))
4567 /* VALUE is negative. */
4568 value |= ((bfd_vma) - 1) << bits;
4569
4570 return value;
4571}
4572
4573/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4574 range expressible by a signed number with the indicated number of
b49e97c9
TS
4575 BITS. */
4576
b34976b6 4577static bfd_boolean
9719ad41 4578mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4579{
4580 bfd_signed_vma svalue = (bfd_signed_vma) value;
4581
4582 if (svalue > (1 << (bits - 1)) - 1)
4583 /* The value is too big. */
b34976b6 4584 return TRUE;
b49e97c9
TS
4585 else if (svalue < -(1 << (bits - 1)))
4586 /* The value is too small. */
b34976b6 4587 return TRUE;
b49e97c9
TS
4588
4589 /* All is well. */
b34976b6 4590 return FALSE;
b49e97c9
TS
4591}
4592
4593/* Calculate the %high function. */
4594
4595static bfd_vma
9719ad41 4596mips_elf_high (bfd_vma value)
b49e97c9
TS
4597{
4598 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4599}
4600
4601/* Calculate the %higher function. */
4602
4603static bfd_vma
9719ad41 4604mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4605{
4606#ifdef BFD64
4607 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4608#else
4609 abort ();
c5ae1840 4610 return MINUS_ONE;
b49e97c9
TS
4611#endif
4612}
4613
4614/* Calculate the %highest function. */
4615
4616static bfd_vma
9719ad41 4617mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4618{
4619#ifdef BFD64
b15e6682 4620 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4621#else
4622 abort ();
c5ae1840 4623 return MINUS_ONE;
b49e97c9
TS
4624#endif
4625}
4626\f
4627/* Create the .compact_rel section. */
4628
b34976b6 4629static bfd_boolean
9719ad41
RS
4630mips_elf_create_compact_rel_section
4631 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4632{
4633 flagword flags;
4634 register asection *s;
4635
4636 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4637 {
4638 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4639 | SEC_READONLY);
4640
3496cb2a 4641 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4642 if (s == NULL
b49e97c9
TS
4643 || ! bfd_set_section_alignment (abfd, s,
4644 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4645 return FALSE;
b49e97c9 4646
eea6121a 4647 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4648 }
4649
b34976b6 4650 return TRUE;
b49e97c9
TS
4651}
4652
4653/* Create the .got section to hold the global offset table. */
4654
b34976b6 4655static bfd_boolean
23cc69b6 4656mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
4657{
4658 flagword flags;
4659 register asection *s;
4660 struct elf_link_hash_entry *h;
14a793b2 4661 struct bfd_link_hash_entry *bh;
b49e97c9
TS
4662 struct mips_got_info *g;
4663 bfd_size_type amt;
0a44bf69
RS
4664 struct mips_elf_link_hash_table *htab;
4665
4666 htab = mips_elf_hash_table (info);
b49e97c9
TS
4667
4668 /* This function may be called more than once. */
23cc69b6
RS
4669 if (htab->sgot)
4670 return TRUE;
b49e97c9
TS
4671
4672 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4673 | SEC_LINKER_CREATED);
4674
72b4917c
TS
4675 /* We have to use an alignment of 2**4 here because this is hardcoded
4676 in the function stub generation and in the linker script. */
3496cb2a 4677 s = bfd_make_section_with_flags (abfd, ".got", flags);
b49e97c9 4678 if (s == NULL
72b4917c 4679 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 4680 return FALSE;
a8028dd0 4681 htab->sgot = s;
b49e97c9
TS
4682
4683 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4684 linker script because we don't want to define the symbol if we
4685 are not creating a global offset table. */
14a793b2 4686 bh = NULL;
b49e97c9
TS
4687 if (! (_bfd_generic_link_add_one_symbol
4688 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 4689 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 4690 return FALSE;
14a793b2
AM
4691
4692 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
4693 h->non_elf = 0;
4694 h->def_regular = 1;
b49e97c9 4695 h->type = STT_OBJECT;
d329bcd1 4696 elf_hash_table (info)->hgot = h;
b49e97c9
TS
4697
4698 if (info->shared
c152c796 4699 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4700 return FALSE;
b49e97c9 4701
b49e97c9 4702 amt = sizeof (struct mips_got_info);
9719ad41 4703 g = bfd_alloc (abfd, amt);
b49e97c9 4704 if (g == NULL)
b34976b6 4705 return FALSE;
b49e97c9 4706 g->global_gotsym = NULL;
e3d54347 4707 g->global_gotno = 0;
23cc69b6 4708 g->reloc_only_gotno = 0;
0f20cc35 4709 g->tls_gotno = 0;
861fb55a 4710 g->local_gotno = 0;
c224138d 4711 g->page_gotno = 0;
861fb55a 4712 g->assigned_gotno = 0;
f4416af6
AO
4713 g->bfd2got = NULL;
4714 g->next = NULL;
0f20cc35 4715 g->tls_ldm_offset = MINUS_ONE;
b15e6682 4716 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
9719ad41 4717 mips_elf_got_entry_eq, NULL);
b15e6682
AO
4718 if (g->got_entries == NULL)
4719 return FALSE;
c224138d
RS
4720 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4721 mips_got_page_entry_eq, NULL);
4722 if (g->got_page_entries == NULL)
4723 return FALSE;
a8028dd0 4724 htab->got_info = g;
f0abc2a1 4725 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
4726 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4727
861fb55a
DJ
4728 /* We also need a .got.plt section when generating PLTs. */
4729 s = bfd_make_section_with_flags (abfd, ".got.plt",
4730 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4731 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4732 if (s == NULL)
4733 return FALSE;
4734 htab->sgotplt = s;
0a44bf69 4735
b34976b6 4736 return TRUE;
b49e97c9 4737}
b49e97c9 4738\f
0a44bf69
RS
4739/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4740 __GOTT_INDEX__ symbols. These symbols are only special for
4741 shared objects; they are not used in executables. */
4742
4743static bfd_boolean
4744is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4745{
4746 return (mips_elf_hash_table (info)->is_vxworks
4747 && info->shared
4748 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4749 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4750}
861fb55a
DJ
4751
4752/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4753 require an la25 stub. See also mips_elf_local_pic_function_p,
4754 which determines whether the destination function ever requires a
4755 stub. */
4756
4757static bfd_boolean
4758mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4759{
4760 /* We specifically ignore branches and jumps from EF_PIC objects,
4761 where the onus is on the compiler or programmer to perform any
4762 necessary initialization of $25. Sometimes such initialization
4763 is unnecessary; for example, -mno-shared functions do not use
4764 the incoming value of $25, and may therefore be called directly. */
4765 if (PIC_OBJECT_P (input_bfd))
4766 return FALSE;
4767
4768 switch (r_type)
4769 {
4770 case R_MIPS_26:
4771 case R_MIPS_PC16:
4772 case R_MIPS16_26:
4773 return TRUE;
4774
4775 default:
4776 return FALSE;
4777 }
4778}
0a44bf69 4779\f
b49e97c9
TS
4780/* Calculate the value produced by the RELOCATION (which comes from
4781 the INPUT_BFD). The ADDEND is the addend to use for this
4782 RELOCATION; RELOCATION->R_ADDEND is ignored.
4783
4784 The result of the relocation calculation is stored in VALUEP.
38a7df63
CF
4785 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4786 is a MIPS16 jump to non-MIPS16 code, or vice versa.
b49e97c9
TS
4787
4788 This function returns bfd_reloc_continue if the caller need take no
4789 further action regarding this relocation, bfd_reloc_notsupported if
4790 something goes dramatically wrong, bfd_reloc_overflow if an
4791 overflow occurs, and bfd_reloc_ok to indicate success. */
4792
4793static bfd_reloc_status_type
9719ad41
RS
4794mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4795 asection *input_section,
4796 struct bfd_link_info *info,
4797 const Elf_Internal_Rela *relocation,
4798 bfd_vma addend, reloc_howto_type *howto,
4799 Elf_Internal_Sym *local_syms,
4800 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
4801 const char **namep,
4802 bfd_boolean *cross_mode_jump_p,
9719ad41 4803 bfd_boolean save_addend)
b49e97c9
TS
4804{
4805 /* The eventual value we will return. */
4806 bfd_vma value;
4807 /* The address of the symbol against which the relocation is
4808 occurring. */
4809 bfd_vma symbol = 0;
4810 /* The final GP value to be used for the relocatable, executable, or
4811 shared object file being produced. */
0a61c8c2 4812 bfd_vma gp;
b49e97c9
TS
4813 /* The place (section offset or address) of the storage unit being
4814 relocated. */
4815 bfd_vma p;
4816 /* The value of GP used to create the relocatable object. */
0a61c8c2 4817 bfd_vma gp0;
b49e97c9
TS
4818 /* The offset into the global offset table at which the address of
4819 the relocation entry symbol, adjusted by the addend, resides
4820 during execution. */
4821 bfd_vma g = MINUS_ONE;
4822 /* The section in which the symbol referenced by the relocation is
4823 located. */
4824 asection *sec = NULL;
4825 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 4826 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 4827 symbol. */
b34976b6
AM
4828 bfd_boolean local_p, was_local_p;
4829 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4830 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
4831 /* TRUE if the symbol referred to by this relocation is
4832 "__gnu_local_gp". */
4833 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
4834 Elf_Internal_Shdr *symtab_hdr;
4835 size_t extsymoff;
4836 unsigned long r_symndx;
4837 int r_type;
b34976b6 4838 /* TRUE if overflow occurred during the calculation of the
b49e97c9 4839 relocation value. */
b34976b6
AM
4840 bfd_boolean overflowed_p;
4841 /* TRUE if this relocation refers to a MIPS16 function. */
4842 bfd_boolean target_is_16_bit_code_p = FALSE;
0a44bf69
RS
4843 struct mips_elf_link_hash_table *htab;
4844 bfd *dynobj;
4845
4846 dynobj = elf_hash_table (info)->dynobj;
4847 htab = mips_elf_hash_table (info);
b49e97c9
TS
4848
4849 /* Parse the relocation. */
4850 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4851 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4852 p = (input_section->output_section->vma
4853 + input_section->output_offset
4854 + relocation->r_offset);
4855
4856 /* Assume that there will be no overflow. */
b34976b6 4857 overflowed_p = FALSE;
b49e97c9
TS
4858
4859 /* Figure out whether or not the symbol is local, and get the offset
4860 used in the array of hash table entries. */
4861 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4862 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 4863 local_sections, FALSE);
bce03d3d 4864 was_local_p = local_p;
b49e97c9
TS
4865 if (! elf_bad_symtab (input_bfd))
4866 extsymoff = symtab_hdr->sh_info;
4867 else
4868 {
4869 /* The symbol table does not follow the rule that local symbols
4870 must come before globals. */
4871 extsymoff = 0;
4872 }
4873
4874 /* Figure out the value of the symbol. */
4875 if (local_p)
4876 {
4877 Elf_Internal_Sym *sym;
4878
4879 sym = local_syms + r_symndx;
4880 sec = local_sections[r_symndx];
4881
4882 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
4883 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4884 || (sec->flags & SEC_MERGE))
b49e97c9 4885 symbol += sym->st_value;
d4df96e6
L
4886 if ((sec->flags & SEC_MERGE)
4887 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4888 {
4889 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4890 addend -= symbol;
4891 addend += sec->output_section->vma + sec->output_offset;
4892 }
b49e97c9
TS
4893
4894 /* MIPS16 text labels should be treated as odd. */
30c09090 4895 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
4896 ++symbol;
4897
4898 /* Record the name of this symbol, for our caller. */
4899 *namep = bfd_elf_string_from_elf_section (input_bfd,
4900 symtab_hdr->sh_link,
4901 sym->st_name);
4902 if (*namep == '\0')
4903 *namep = bfd_section_name (input_bfd, sec);
4904
30c09090 4905 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
b49e97c9
TS
4906 }
4907 else
4908 {
560e09e9
NC
4909 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4910
b49e97c9
TS
4911 /* For global symbols we look up the symbol in the hash-table. */
4912 h = ((struct mips_elf_link_hash_entry *)
4913 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4914 /* Find the real hash-table entry for this symbol. */
4915 while (h->root.root.type == bfd_link_hash_indirect
4916 || h->root.root.type == bfd_link_hash_warning)
4917 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4918
4919 /* Record the name of this symbol, for our caller. */
4920 *namep = h->root.root.root.string;
4921
4922 /* See if this is the special _gp_disp symbol. Note that such a
4923 symbol must always be a global symbol. */
560e09e9 4924 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
4925 && ! NEWABI_P (input_bfd))
4926 {
4927 /* Relocations against _gp_disp are permitted only with
4928 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 4929 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
4930 return bfd_reloc_notsupported;
4931
b34976b6 4932 gp_disp_p = TRUE;
b49e97c9 4933 }
bbe506e8
TS
4934 /* See if this is the special _gp symbol. Note that such a
4935 symbol must always be a global symbol. */
4936 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4937 gnu_local_gp_p = TRUE;
4938
4939
b49e97c9
TS
4940 /* If this symbol is defined, calculate its address. Note that
4941 _gp_disp is a magic symbol, always implicitly defined by the
4942 linker, so it's inappropriate to check to see whether or not
4943 its defined. */
4944 else if ((h->root.root.type == bfd_link_hash_defined
4945 || h->root.root.type == bfd_link_hash_defweak)
4946 && h->root.root.u.def.section)
4947 {
4948 sec = h->root.root.u.def.section;
4949 if (sec->output_section)
4950 symbol = (h->root.root.u.def.value
4951 + sec->output_section->vma
4952 + sec->output_offset);
4953 else
4954 symbol = h->root.root.u.def.value;
4955 }
4956 else if (h->root.root.type == bfd_link_hash_undefweak)
4957 /* We allow relocations against undefined weak symbols, giving
4958 it the value zero, so that you can undefined weak functions
4959 and check to see if they exist by looking at their
4960 addresses. */
4961 symbol = 0;
59c2e50f 4962 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
4963 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4964 symbol = 0;
a4d0f181
TS
4965 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4966 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
4967 {
4968 /* If this is a dynamic link, we should have created a
4969 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4970 in in _bfd_mips_elf_create_dynamic_sections.
4971 Otherwise, we should define the symbol with a value of 0.
4972 FIXME: It should probably get into the symbol table
4973 somehow as well. */
4974 BFD_ASSERT (! info->shared);
4975 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4976 symbol = 0;
4977 }
5e2b0d47
NC
4978 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4979 {
4980 /* This is an optional symbol - an Irix specific extension to the
4981 ELF spec. Ignore it for now.
4982 XXX - FIXME - there is more to the spec for OPTIONAL symbols
4983 than simply ignoring them, but we do not handle this for now.
4984 For information see the "64-bit ELF Object File Specification"
4985 which is available from here:
4986 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
4987 symbol = 0;
4988 }
e7e2196d
MR
4989 else if ((*info->callbacks->undefined_symbol)
4990 (info, h->root.root.root.string, input_bfd,
4991 input_section, relocation->r_offset,
4992 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4993 || ELF_ST_VISIBILITY (h->root.other)))
4994 {
4995 return bfd_reloc_undefined;
4996 }
b49e97c9
TS
4997 else
4998 {
e7e2196d 4999 return bfd_reloc_notsupported;
b49e97c9
TS
5000 }
5001
30c09090 5002 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
b49e97c9
TS
5003 }
5004
738e5348
RS
5005 /* If this is a reference to a 16-bit function with a stub, we need
5006 to redirect the relocation to the stub unless:
5007
5008 (a) the relocation is for a MIPS16 JAL;
5009
5010 (b) the relocation is for a MIPS16 PIC call, and there are no
5011 non-MIPS16 uses of the GOT slot; or
5012
5013 (c) the section allows direct references to MIPS16 functions. */
5014 if (r_type != R_MIPS16_26
5015 && !info->relocatable
5016 && ((h != NULL
5017 && h->fn_stub != NULL
5018 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71
TS
5019 || (local_p
5020 && elf_tdata (input_bfd)->local_stubs != NULL
b49e97c9 5021 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5022 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5023 {
5024 /* This is a 32- or 64-bit call to a 16-bit function. We should
5025 have already noticed that we were going to need the
5026 stub. */
5027 if (local_p)
5028 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5029 else
5030 {
5031 BFD_ASSERT (h->need_fn_stub);
5032 sec = h->fn_stub;
5033 }
5034
5035 symbol = sec->output_section->vma + sec->output_offset;
f38c2df5
TS
5036 /* The target is 16-bit, but the stub isn't. */
5037 target_is_16_bit_code_p = FALSE;
b49e97c9
TS
5038 }
5039 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
738e5348
RS
5040 need to redirect the call to the stub. Note that we specifically
5041 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5042 use an indirect stub instead. */
1049f94e 5043 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5044 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71
TS
5045 || (local_p
5046 && elf_tdata (input_bfd)->local_call_stubs != NULL
5047 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
b49e97c9
TS
5048 && !target_is_16_bit_code_p)
5049 {
b9d58d71
TS
5050 if (local_p)
5051 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5052 else
b49e97c9 5053 {
b9d58d71
TS
5054 /* If both call_stub and call_fp_stub are defined, we can figure
5055 out which one to use by checking which one appears in the input
5056 file. */
5057 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5058 {
b9d58d71
TS
5059 asection *o;
5060
5061 sec = NULL;
5062 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5063 {
b9d58d71
TS
5064 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5065 {
5066 sec = h->call_fp_stub;
5067 break;
5068 }
b49e97c9 5069 }
b9d58d71
TS
5070 if (sec == NULL)
5071 sec = h->call_stub;
b49e97c9 5072 }
b9d58d71 5073 else if (h->call_stub != NULL)
b49e97c9 5074 sec = h->call_stub;
b9d58d71
TS
5075 else
5076 sec = h->call_fp_stub;
5077 }
b49e97c9 5078
eea6121a 5079 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5080 symbol = sec->output_section->vma + sec->output_offset;
5081 }
861fb55a
DJ
5082 /* If this is a direct call to a PIC function, redirect to the
5083 non-PIC stub. */
5084 else if (h != NULL && h->la25_stub
5085 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5086 symbol = (h->la25_stub->stub_section->output_section->vma
5087 + h->la25_stub->stub_section->output_offset
5088 + h->la25_stub->offset);
b49e97c9
TS
5089
5090 /* Calls from 16-bit code to 32-bit code and vice versa require the
38a7df63
CF
5091 mode change. */
5092 *cross_mode_jump_p = !info->relocatable
5093 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5094 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5095 && target_is_16_bit_code_p));
b49e97c9
TS
5096
5097 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 5098 local_sections, TRUE);
b49e97c9 5099
0a61c8c2
RS
5100 gp0 = _bfd_get_gp_value (input_bfd);
5101 gp = _bfd_get_gp_value (abfd);
23cc69b6 5102 if (htab->got_info)
a8028dd0 5103 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5104
5105 if (gnu_local_gp_p)
5106 symbol = gp;
5107
5108 /* If we haven't already determined the GOT offset, oand we're going
5109 to need it, get it now. */
b49e97c9
TS
5110 switch (r_type)
5111 {
0fdc1bf1 5112 case R_MIPS_GOT_PAGE:
93a2b7ae 5113 case R_MIPS_GOT_OFST:
d25aed71
RS
5114 /* We need to decay to GOT_DISP/addend if the symbol doesn't
5115 bind locally. */
5116 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
93a2b7ae 5117 if (local_p || r_type == R_MIPS_GOT_OFST)
0fdc1bf1
AO
5118 break;
5119 /* Fall through. */
5120
738e5348
RS
5121 case R_MIPS16_CALL16:
5122 case R_MIPS16_GOT16:
b49e97c9
TS
5123 case R_MIPS_CALL16:
5124 case R_MIPS_GOT16:
5125 case R_MIPS_GOT_DISP:
5126 case R_MIPS_GOT_HI16:
5127 case R_MIPS_CALL_HI16:
5128 case R_MIPS_GOT_LO16:
5129 case R_MIPS_CALL_LO16:
0f20cc35
DJ
5130 case R_MIPS_TLS_GD:
5131 case R_MIPS_TLS_GOTTPREL:
5132 case R_MIPS_TLS_LDM:
b49e97c9 5133 /* Find the index into the GOT where this value is located. */
0f20cc35
DJ
5134 if (r_type == R_MIPS_TLS_LDM)
5135 {
0a44bf69 5136 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5137 0, 0, NULL, r_type);
0f20cc35
DJ
5138 if (g == MINUS_ONE)
5139 return bfd_reloc_outofrange;
5140 }
5141 else if (!local_p)
b49e97c9 5142 {
0a44bf69
RS
5143 /* On VxWorks, CALL relocations should refer to the .got.plt
5144 entry, which is initialized to point at the PLT stub. */
5145 if (htab->is_vxworks
5146 && (r_type == R_MIPS_CALL_HI16
5147 || r_type == R_MIPS_CALL_LO16
738e5348 5148 || call16_reloc_p (r_type)))
0a44bf69
RS
5149 {
5150 BFD_ASSERT (addend == 0);
5151 BFD_ASSERT (h->root.needs_plt);
5152 g = mips_elf_gotplt_index (info, &h->root);
5153 }
5154 else
b49e97c9 5155 {
0a44bf69
RS
5156 /* GOT_PAGE may take a non-zero addend, that is ignored in a
5157 GOT_PAGE relocation that decays to GOT_DISP because the
5158 symbol turns out to be global. The addend is then added
5159 as GOT_OFST. */
5160 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
5161 g = mips_elf_global_got_index (dynobj, input_bfd,
5162 &h->root, r_type, info);
5163 if (h->tls_type == GOT_NORMAL
5164 && (! elf_hash_table(info)->dynamic_sections_created
5165 || (info->shared
5166 && (info->symbolic || h->root.forced_local)
5167 && h->root.def_regular)))
a8028dd0
RS
5168 /* This is a static link or a -Bsymbolic link. The
5169 symbol is defined locally, or was forced to be local.
5170 We must initialize this entry in the GOT. */
5171 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5172 }
5173 }
0a44bf69 5174 else if (!htab->is_vxworks
738e5348 5175 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5176 /* The calculation below does not involve "g". */
b49e97c9
TS
5177 break;
5178 else
5179 {
5c18022e 5180 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5181 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5182 if (g == MINUS_ONE)
5183 return bfd_reloc_outofrange;
5184 }
5185
5186 /* Convert GOT indices to actual offsets. */
a8028dd0 5187 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5188 break;
b49e97c9
TS
5189 }
5190
0a44bf69
RS
5191 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5192 symbols are resolved by the loader. Add them to .rela.dyn. */
5193 if (h != NULL && is_gott_symbol (info, &h->root))
5194 {
5195 Elf_Internal_Rela outrel;
5196 bfd_byte *loc;
5197 asection *s;
5198
5199 s = mips_elf_rel_dyn_section (info, FALSE);
5200 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5201
5202 outrel.r_offset = (input_section->output_section->vma
5203 + input_section->output_offset
5204 + relocation->r_offset);
5205 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5206 outrel.r_addend = addend;
5207 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5208
5209 /* If we've written this relocation for a readonly section,
5210 we need to set DF_TEXTREL again, so that we do not delete the
5211 DT_TEXTREL tag. */
5212 if (MIPS_ELF_READONLY_SECTION (input_section))
5213 info->flags |= DF_TEXTREL;
5214
0a44bf69
RS
5215 *valuep = 0;
5216 return bfd_reloc_ok;
5217 }
5218
b49e97c9
TS
5219 /* Figure out what kind of relocation is being performed. */
5220 switch (r_type)
5221 {
5222 case R_MIPS_NONE:
5223 return bfd_reloc_continue;
5224
5225 case R_MIPS_16:
a7ebbfdf 5226 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5227 overflowed_p = mips_elf_overflow_p (value, 16);
5228 break;
5229
5230 case R_MIPS_32:
5231 case R_MIPS_REL32:
5232 case R_MIPS_64:
5233 if ((info->shared
861fb55a 5234 || (htab->root.dynamic_sections_created
b49e97c9 5235 && h != NULL
f5385ebf 5236 && h->root.def_dynamic
861fb55a
DJ
5237 && !h->root.def_regular
5238 && !h->has_static_relocs))
b49e97c9 5239 && r_symndx != 0
9a59ad6b
DJ
5240 && (h == NULL
5241 || h->root.root.type != bfd_link_hash_undefweak
5242 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5243 && (input_section->flags & SEC_ALLOC) != 0)
5244 {
861fb55a 5245 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5246 where the symbol will end up. So, we create a relocation
5247 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5248 linker. We must do the same for executable references to
5249 shared library symbols, unless we've decided to use copy
5250 relocs or PLTs instead. */
b49e97c9
TS
5251 value = addend;
5252 if (!mips_elf_create_dynamic_relocation (abfd,
5253 info,
5254 relocation,
5255 h,
5256 sec,
5257 symbol,
5258 &value,
5259 input_section))
5260 return bfd_reloc_undefined;
5261 }
5262 else
5263 {
5264 if (r_type != R_MIPS_REL32)
5265 value = symbol + addend;
5266 else
5267 value = addend;
5268 }
5269 value &= howto->dst_mask;
092dcd75
CD
5270 break;
5271
5272 case R_MIPS_PC32:
5273 value = symbol + addend - p;
5274 value &= howto->dst_mask;
b49e97c9
TS
5275 break;
5276
b49e97c9
TS
5277 case R_MIPS16_26:
5278 /* The calculation for R_MIPS16_26 is just the same as for an
5279 R_MIPS_26. It's only the storage of the relocated field into
5280 the output file that's different. That's handled in
5281 mips_elf_perform_relocation. So, we just fall through to the
5282 R_MIPS_26 case here. */
5283 case R_MIPS_26:
5284 if (local_p)
30ac9238 5285 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
b49e97c9 5286 else
728b2f21
ILT
5287 {
5288 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
c314987d
RS
5289 if (h->root.root.type != bfd_link_hash_undefweak)
5290 overflowed_p = (value >> 26) != ((p + 4) >> 28);
728b2f21 5291 }
b49e97c9
TS
5292 value &= howto->dst_mask;
5293 break;
5294
0f20cc35
DJ
5295 case R_MIPS_TLS_DTPREL_HI16:
5296 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5297 & howto->dst_mask);
5298 break;
5299
5300 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5301 case R_MIPS_TLS_DTPREL32:
5302 case R_MIPS_TLS_DTPREL64:
0f20cc35
DJ
5303 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5304 break;
5305
5306 case R_MIPS_TLS_TPREL_HI16:
5307 value = (mips_elf_high (addend + symbol - tprel_base (info))
5308 & howto->dst_mask);
5309 break;
5310
5311 case R_MIPS_TLS_TPREL_LO16:
5312 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5313 break;
5314
b49e97c9 5315 case R_MIPS_HI16:
d6f16593 5316 case R_MIPS16_HI16:
b49e97c9
TS
5317 if (!gp_disp_p)
5318 {
5319 value = mips_elf_high (addend + symbol);
5320 value &= howto->dst_mask;
5321 }
5322 else
5323 {
d6f16593
MR
5324 /* For MIPS16 ABI code we generate this sequence
5325 0: li $v0,%hi(_gp_disp)
5326 4: addiupc $v1,%lo(_gp_disp)
5327 8: sll $v0,16
5328 12: addu $v0,$v1
5329 14: move $gp,$v0
5330 So the offsets of hi and lo relocs are the same, but the
5331 $pc is four higher than $t9 would be, so reduce
5332 both reloc addends by 4. */
5333 if (r_type == R_MIPS16_HI16)
5334 value = mips_elf_high (addend + gp - p - 4);
5335 else
5336 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5337 overflowed_p = mips_elf_overflow_p (value, 16);
5338 }
5339 break;
5340
5341 case R_MIPS_LO16:
d6f16593 5342 case R_MIPS16_LO16:
b49e97c9
TS
5343 if (!gp_disp_p)
5344 value = (symbol + addend) & howto->dst_mask;
5345 else
5346 {
d6f16593
MR
5347 /* See the comment for R_MIPS16_HI16 above for the reason
5348 for this conditional. */
5349 if (r_type == R_MIPS16_LO16)
5350 value = addend + gp - p;
5351 else
5352 value = addend + gp - p + 4;
b49e97c9 5353 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5354 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5355 _gp_disp are normally generated from the .cpload
5356 pseudo-op. It generates code that normally looks like
5357 this:
5358
5359 lui $gp,%hi(_gp_disp)
5360 addiu $gp,$gp,%lo(_gp_disp)
5361 addu $gp,$gp,$t9
5362
5363 Here $t9 holds the address of the function being called,
5364 as required by the MIPS ELF ABI. The R_MIPS_LO16
5365 relocation can easily overflow in this situation, but the
5366 R_MIPS_HI16 relocation will handle the overflow.
5367 Therefore, we consider this a bug in the MIPS ABI, and do
5368 not check for overflow here. */
5369 }
5370 break;
5371
5372 case R_MIPS_LITERAL:
5373 /* Because we don't merge literal sections, we can handle this
5374 just like R_MIPS_GPREL16. In the long run, we should merge
5375 shared literals, and then we will need to additional work
5376 here. */
5377
5378 /* Fall through. */
5379
5380 case R_MIPS16_GPREL:
5381 /* The R_MIPS16_GPREL performs the same calculation as
5382 R_MIPS_GPREL16, but stores the relocated bits in a different
5383 order. We don't need to do anything special here; the
5384 differences are handled in mips_elf_perform_relocation. */
5385 case R_MIPS_GPREL16:
bce03d3d
AO
5386 /* Only sign-extend the addend if it was extracted from the
5387 instruction. If the addend was separate, leave it alone,
5388 otherwise we may lose significant bits. */
5389 if (howto->partial_inplace)
a7ebbfdf 5390 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5391 value = symbol + addend - gp;
5392 /* If the symbol was local, any earlier relocatable links will
5393 have adjusted its addend with the gp offset, so compensate
5394 for that now. Don't do it for symbols forced local in this
5395 link, though, since they won't have had the gp offset applied
5396 to them before. */
5397 if (was_local_p)
5398 value += gp0;
b49e97c9
TS
5399 overflowed_p = mips_elf_overflow_p (value, 16);
5400 break;
5401
738e5348
RS
5402 case R_MIPS16_GOT16:
5403 case R_MIPS16_CALL16:
b49e97c9
TS
5404 case R_MIPS_GOT16:
5405 case R_MIPS_CALL16:
0a44bf69 5406 /* VxWorks does not have separate local and global semantics for
738e5348 5407 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5408 if (!htab->is_vxworks && local_p)
b49e97c9 5409 {
b34976b6 5410 bfd_boolean forced;
b49e97c9 5411
b49e97c9 5412 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 5413 local_sections, FALSE);
5c18022e 5414 value = mips_elf_got16_entry (abfd, input_bfd, info,
f4416af6 5415 symbol + addend, forced);
b49e97c9
TS
5416 if (value == MINUS_ONE)
5417 return bfd_reloc_outofrange;
5418 value
a8028dd0 5419 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5420 overflowed_p = mips_elf_overflow_p (value, 16);
5421 break;
5422 }
5423
5424 /* Fall through. */
5425
0f20cc35
DJ
5426 case R_MIPS_TLS_GD:
5427 case R_MIPS_TLS_GOTTPREL:
5428 case R_MIPS_TLS_LDM:
b49e97c9 5429 case R_MIPS_GOT_DISP:
0fdc1bf1 5430 got_disp:
b49e97c9
TS
5431 value = g;
5432 overflowed_p = mips_elf_overflow_p (value, 16);
5433 break;
5434
5435 case R_MIPS_GPREL32:
bce03d3d
AO
5436 value = (addend + symbol + gp0 - gp);
5437 if (!save_addend)
5438 value &= howto->dst_mask;
b49e97c9
TS
5439 break;
5440
5441 case R_MIPS_PC16:
bad36eac
DJ
5442 case R_MIPS_GNU_REL16_S2:
5443 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5444 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5445 value >>= howto->rightshift;
5446 value &= howto->dst_mask;
b49e97c9
TS
5447 break;
5448
5449 case R_MIPS_GOT_HI16:
5450 case R_MIPS_CALL_HI16:
5451 /* We're allowed to handle these two relocations identically.
5452 The dynamic linker is allowed to handle the CALL relocations
5453 differently by creating a lazy evaluation stub. */
5454 value = g;
5455 value = mips_elf_high (value);
5456 value &= howto->dst_mask;
5457 break;
5458
5459 case R_MIPS_GOT_LO16:
5460 case R_MIPS_CALL_LO16:
5461 value = g & howto->dst_mask;
5462 break;
5463
5464 case R_MIPS_GOT_PAGE:
0fdc1bf1
AO
5465 /* GOT_PAGE relocations that reference non-local symbols decay
5466 to GOT_DISP. The corresponding GOT_OFST relocation decays to
5467 0. */
93a2b7ae 5468 if (! local_p)
0fdc1bf1 5469 goto got_disp;
5c18022e 5470 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5471 if (value == MINUS_ONE)
5472 return bfd_reloc_outofrange;
a8028dd0 5473 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5474 overflowed_p = mips_elf_overflow_p (value, 16);
5475 break;
5476
5477 case R_MIPS_GOT_OFST:
93a2b7ae 5478 if (local_p)
5c18022e 5479 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5480 else
5481 value = addend;
b49e97c9
TS
5482 overflowed_p = mips_elf_overflow_p (value, 16);
5483 break;
5484
5485 case R_MIPS_SUB:
5486 value = symbol - addend;
5487 value &= howto->dst_mask;
5488 break;
5489
5490 case R_MIPS_HIGHER:
5491 value = mips_elf_higher (addend + symbol);
5492 value &= howto->dst_mask;
5493 break;
5494
5495 case R_MIPS_HIGHEST:
5496 value = mips_elf_highest (addend + symbol);
5497 value &= howto->dst_mask;
5498 break;
5499
5500 case R_MIPS_SCN_DISP:
5501 value = symbol + addend - sec->output_offset;
5502 value &= howto->dst_mask;
5503 break;
5504
b49e97c9 5505 case R_MIPS_JALR:
1367d393
ILT
5506 /* This relocation is only a hint. In some cases, we optimize
5507 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5508 when the symbol does not resolve locally. */
5509 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5510 return bfd_reloc_continue;
5511 value = symbol + addend;
5512 break;
b49e97c9 5513
1367d393 5514 case R_MIPS_PJUMP:
b49e97c9
TS
5515 case R_MIPS_GNU_VTINHERIT:
5516 case R_MIPS_GNU_VTENTRY:
5517 /* We don't do anything with these at present. */
5518 return bfd_reloc_continue;
5519
5520 default:
5521 /* An unrecognized relocation type. */
5522 return bfd_reloc_notsupported;
5523 }
5524
5525 /* Store the VALUE for our caller. */
5526 *valuep = value;
5527 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5528}
5529
5530/* Obtain the field relocated by RELOCATION. */
5531
5532static bfd_vma
9719ad41
RS
5533mips_elf_obtain_contents (reloc_howto_type *howto,
5534 const Elf_Internal_Rela *relocation,
5535 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
5536{
5537 bfd_vma x;
5538 bfd_byte *location = contents + relocation->r_offset;
5539
5540 /* Obtain the bytes. */
5541 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5542
b49e97c9
TS
5543 return x;
5544}
5545
5546/* It has been determined that the result of the RELOCATION is the
5547 VALUE. Use HOWTO to place VALUE into the output file at the
5548 appropriate position. The SECTION is the section to which the
38a7df63
CF
5549 relocation applies.
5550 CROSS_MODE_JUMP_P is true if the relocation field
5551 is a MIPS16 jump to non-MIPS16 code, or vice versa.
b49e97c9 5552
b34976b6 5553 Returns FALSE if anything goes wrong. */
b49e97c9 5554
b34976b6 5555static bfd_boolean
9719ad41
RS
5556mips_elf_perform_relocation (struct bfd_link_info *info,
5557 reloc_howto_type *howto,
5558 const Elf_Internal_Rela *relocation,
5559 bfd_vma value, bfd *input_bfd,
5560 asection *input_section, bfd_byte *contents,
38a7df63 5561 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
5562{
5563 bfd_vma x;
5564 bfd_byte *location;
5565 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5566
5567 /* Figure out where the relocation is occurring. */
5568 location = contents + relocation->r_offset;
5569
d6f16593
MR
5570 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5571
b49e97c9
TS
5572 /* Obtain the current value. */
5573 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5574
5575 /* Clear the field we are setting. */
5576 x &= ~howto->dst_mask;
5577
b49e97c9
TS
5578 /* Set the field. */
5579 x |= (value & howto->dst_mask);
5580
5581 /* If required, turn JAL into JALX. */
38a7df63 5582 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 5583 {
b34976b6 5584 bfd_boolean ok;
b49e97c9
TS
5585 bfd_vma opcode = x >> 26;
5586 bfd_vma jalx_opcode;
5587
5588 /* Check to see if the opcode is already JAL or JALX. */
5589 if (r_type == R_MIPS16_26)
5590 {
5591 ok = ((opcode == 0x6) || (opcode == 0x7));
5592 jalx_opcode = 0x7;
5593 }
5594 else
5595 {
5596 ok = ((opcode == 0x3) || (opcode == 0x1d));
5597 jalx_opcode = 0x1d;
5598 }
5599
5600 /* If the opcode is not JAL or JALX, there's a problem. */
5601 if (!ok)
5602 {
5603 (*_bfd_error_handler)
d003868e
AM
5604 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5605 input_bfd,
5606 input_section,
b49e97c9
TS
5607 (unsigned long) relocation->r_offset);
5608 bfd_set_error (bfd_error_bad_value);
b34976b6 5609 return FALSE;
b49e97c9
TS
5610 }
5611
5612 /* Make this the JALX opcode. */
5613 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5614 }
5615
38a7df63
CF
5616 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5617 range. */
cd8d5a82 5618 if (!info->relocatable
38a7df63 5619 && !cross_mode_jump_p
cd8d5a82
CF
5620 && ((JAL_TO_BAL_P (input_bfd)
5621 && r_type == R_MIPS_26
5622 && (x >> 26) == 0x3) /* jal addr */
5623 || (JALR_TO_BAL_P (input_bfd)
5624 && r_type == R_MIPS_JALR
38a7df63
CF
5625 && x == 0x0320f809) /* jalr t9 */
5626 || (JR_TO_B_P (input_bfd)
5627 && r_type == R_MIPS_JALR
5628 && x == 0x03200008))) /* jr t9 */
1367d393
ILT
5629 {
5630 bfd_vma addr;
5631 bfd_vma dest;
5632 bfd_signed_vma off;
5633
5634 addr = (input_section->output_section->vma
5635 + input_section->output_offset
5636 + relocation->r_offset
5637 + 4);
5638 if (r_type == R_MIPS_26)
5639 dest = (value << 2) | ((addr >> 28) << 28);
5640 else
5641 dest = value;
5642 off = dest - addr;
5643 if (off <= 0x1ffff && off >= -0x20000)
38a7df63
CF
5644 {
5645 if (x == 0x03200008) /* jr t9 */
5646 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5647 else
5648 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5649 }
1367d393
ILT
5650 }
5651
b49e97c9
TS
5652 /* Put the value into the output. */
5653 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593
MR
5654
5655 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5656 location);
5657
b34976b6 5658 return TRUE;
b49e97c9 5659}
b49e97c9 5660\f
b49e97c9
TS
5661/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5662 is the original relocation, which is now being transformed into a
5663 dynamic relocation. The ADDENDP is adjusted if necessary; the
5664 caller should store the result in place of the original addend. */
5665
b34976b6 5666static bfd_boolean
9719ad41
RS
5667mips_elf_create_dynamic_relocation (bfd *output_bfd,
5668 struct bfd_link_info *info,
5669 const Elf_Internal_Rela *rel,
5670 struct mips_elf_link_hash_entry *h,
5671 asection *sec, bfd_vma symbol,
5672 bfd_vma *addendp, asection *input_section)
b49e97c9 5673{
947216bf 5674 Elf_Internal_Rela outrel[3];
b49e97c9
TS
5675 asection *sreloc;
5676 bfd *dynobj;
5677 int r_type;
5d41f0b6
RS
5678 long indx;
5679 bfd_boolean defined_p;
0a44bf69 5680 struct mips_elf_link_hash_table *htab;
b49e97c9 5681
0a44bf69 5682 htab = mips_elf_hash_table (info);
b49e97c9
TS
5683 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5684 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 5685 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
5686 BFD_ASSERT (sreloc != NULL);
5687 BFD_ASSERT (sreloc->contents != NULL);
5688 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 5689 < sreloc->size);
b49e97c9 5690
b49e97c9
TS
5691 outrel[0].r_offset =
5692 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
5693 if (ABI_64_P (output_bfd))
5694 {
5695 outrel[1].r_offset =
5696 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5697 outrel[2].r_offset =
5698 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5699 }
b49e97c9 5700
c5ae1840 5701 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 5702 /* The relocation field has been deleted. */
5d41f0b6
RS
5703 return TRUE;
5704
5705 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
5706 {
5707 /* The relocation field has been converted into a relative value of
5708 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5709 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 5710 *addendp += symbol;
5d41f0b6 5711 return TRUE;
0d591ff7 5712 }
b49e97c9 5713
5d41f0b6
RS
5714 /* We must now calculate the dynamic symbol table index to use
5715 in the relocation. */
5716 if (h != NULL
6ece8836
TS
5717 && (!h->root.def_regular
5718 || (info->shared && !info->symbolic && !h->root.forced_local)))
5d41f0b6
RS
5719 {
5720 indx = h->root.dynindx;
5721 if (SGI_COMPAT (output_bfd))
5722 defined_p = h->root.def_regular;
5723 else
5724 /* ??? glibc's ld.so just adds the final GOT entry to the
5725 relocation field. It therefore treats relocs against
5726 defined symbols in the same way as relocs against
5727 undefined symbols. */
5728 defined_p = FALSE;
5729 }
b49e97c9
TS
5730 else
5731 {
5d41f0b6
RS
5732 if (sec != NULL && bfd_is_abs_section (sec))
5733 indx = 0;
5734 else if (sec == NULL || sec->owner == NULL)
fdd07405 5735 {
5d41f0b6
RS
5736 bfd_set_error (bfd_error_bad_value);
5737 return FALSE;
b49e97c9
TS
5738 }
5739 else
5740 {
5d41f0b6 5741 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
5742 if (indx == 0)
5743 {
5744 asection *osec = htab->root.text_index_section;
5745 indx = elf_section_data (osec)->dynindx;
5746 }
5d41f0b6
RS
5747 if (indx == 0)
5748 abort ();
b49e97c9
TS
5749 }
5750
5d41f0b6
RS
5751 /* Instead of generating a relocation using the section
5752 symbol, we may as well make it a fully relative
5753 relocation. We want to avoid generating relocations to
5754 local symbols because we used to generate them
5755 incorrectly, without adding the original symbol value,
5756 which is mandated by the ABI for section symbols. In
5757 order to give dynamic loaders and applications time to
5758 phase out the incorrect use, we refrain from emitting
5759 section-relative relocations. It's not like they're
5760 useful, after all. This should be a bit more efficient
5761 as well. */
5762 /* ??? Although this behavior is compatible with glibc's ld.so,
5763 the ABI says that relocations against STN_UNDEF should have
5764 a symbol value of 0. Irix rld honors this, so relocations
5765 against STN_UNDEF have no effect. */
5766 if (!SGI_COMPAT (output_bfd))
5767 indx = 0;
5768 defined_p = TRUE;
b49e97c9
TS
5769 }
5770
5d41f0b6
RS
5771 /* If the relocation was previously an absolute relocation and
5772 this symbol will not be referred to by the relocation, we must
5773 adjust it by the value we give it in the dynamic symbol table.
5774 Otherwise leave the job up to the dynamic linker. */
5775 if (defined_p && r_type != R_MIPS_REL32)
5776 *addendp += symbol;
5777
0a44bf69
RS
5778 if (htab->is_vxworks)
5779 /* VxWorks uses non-relative relocations for this. */
5780 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5781 else
5782 /* The relocation is always an REL32 relocation because we don't
5783 know where the shared library will wind up at load-time. */
5784 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5785 R_MIPS_REL32);
5786
5d41f0b6
RS
5787 /* For strict adherence to the ABI specification, we should
5788 generate a R_MIPS_64 relocation record by itself before the
5789 _REL32/_64 record as well, such that the addend is read in as
5790 a 64-bit value (REL32 is a 32-bit relocation, after all).
5791 However, since none of the existing ELF64 MIPS dynamic
5792 loaders seems to care, we don't waste space with these
5793 artificial relocations. If this turns out to not be true,
5794 mips_elf_allocate_dynamic_relocation() should be tweaked so
5795 as to make room for a pair of dynamic relocations per
5796 invocation if ABI_64_P, and here we should generate an
5797 additional relocation record with R_MIPS_64 by itself for a
5798 NULL symbol before this relocation record. */
5799 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5800 ABI_64_P (output_bfd)
5801 ? R_MIPS_64
5802 : R_MIPS_NONE);
5803 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5804
5805 /* Adjust the output offset of the relocation to reference the
5806 correct location in the output file. */
5807 outrel[0].r_offset += (input_section->output_section->vma
5808 + input_section->output_offset);
5809 outrel[1].r_offset += (input_section->output_section->vma
5810 + input_section->output_offset);
5811 outrel[2].r_offset += (input_section->output_section->vma
5812 + input_section->output_offset);
5813
b49e97c9
TS
5814 /* Put the relocation back out. We have to use the special
5815 relocation outputter in the 64-bit case since the 64-bit
5816 relocation format is non-standard. */
5817 if (ABI_64_P (output_bfd))
5818 {
5819 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5820 (output_bfd, &outrel[0],
5821 (sreloc->contents
5822 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5823 }
0a44bf69
RS
5824 else if (htab->is_vxworks)
5825 {
5826 /* VxWorks uses RELA rather than REL dynamic relocations. */
5827 outrel[0].r_addend = *addendp;
5828 bfd_elf32_swap_reloca_out
5829 (output_bfd, &outrel[0],
5830 (sreloc->contents
5831 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5832 }
b49e97c9 5833 else
947216bf
AM
5834 bfd_elf32_swap_reloc_out
5835 (output_bfd, &outrel[0],
5836 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 5837
b49e97c9
TS
5838 /* We've now added another relocation. */
5839 ++sreloc->reloc_count;
5840
5841 /* Make sure the output section is writable. The dynamic linker
5842 will be writing to it. */
5843 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5844 |= SHF_WRITE;
5845
5846 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 5847 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9
TS
5848 {
5849 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5850 bfd_byte *cr;
5851
5852 if (scpt)
5853 {
5854 Elf32_crinfo cptrel;
5855
5856 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5857 cptrel.vaddr = (rel->r_offset
5858 + input_section->output_section->vma
5859 + input_section->output_offset);
5860 if (r_type == R_MIPS_REL32)
5861 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5862 else
5863 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5864 mips_elf_set_cr_dist2to (cptrel, 0);
5865 cptrel.konst = *addendp;
5866
5867 cr = (scpt->contents
5868 + sizeof (Elf32_External_compact_rel));
abc0f8d0 5869 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
5870 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5871 ((Elf32_External_crinfo *) cr
5872 + scpt->reloc_count));
5873 ++scpt->reloc_count;
5874 }
5875 }
5876
943284cc
DJ
5877 /* If we've written this relocation for a readonly section,
5878 we need to set DF_TEXTREL again, so that we do not delete the
5879 DT_TEXTREL tag. */
5880 if (MIPS_ELF_READONLY_SECTION (input_section))
5881 info->flags |= DF_TEXTREL;
5882
b34976b6 5883 return TRUE;
b49e97c9
TS
5884}
5885\f
b49e97c9
TS
5886/* Return the MACH for a MIPS e_flags value. */
5887
5888unsigned long
9719ad41 5889_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
5890{
5891 switch (flags & EF_MIPS_MACH)
5892 {
5893 case E_MIPS_MACH_3900:
5894 return bfd_mach_mips3900;
5895
5896 case E_MIPS_MACH_4010:
5897 return bfd_mach_mips4010;
5898
5899 case E_MIPS_MACH_4100:
5900 return bfd_mach_mips4100;
5901
5902 case E_MIPS_MACH_4111:
5903 return bfd_mach_mips4111;
5904
00707a0e
RS
5905 case E_MIPS_MACH_4120:
5906 return bfd_mach_mips4120;
5907
b49e97c9
TS
5908 case E_MIPS_MACH_4650:
5909 return bfd_mach_mips4650;
5910
00707a0e
RS
5911 case E_MIPS_MACH_5400:
5912 return bfd_mach_mips5400;
5913
5914 case E_MIPS_MACH_5500:
5915 return bfd_mach_mips5500;
5916
0d2e43ed
ILT
5917 case E_MIPS_MACH_9000:
5918 return bfd_mach_mips9000;
5919
b49e97c9
TS
5920 case E_MIPS_MACH_SB1:
5921 return bfd_mach_mips_sb1;
5922
350cc38d
MS
5923 case E_MIPS_MACH_LS2E:
5924 return bfd_mach_mips_loongson_2e;
5925
5926 case E_MIPS_MACH_LS2F:
5927 return bfd_mach_mips_loongson_2f;
5928
6f179bd0
AN
5929 case E_MIPS_MACH_OCTEON:
5930 return bfd_mach_mips_octeon;
5931
52b6b6b9
JM
5932 case E_MIPS_MACH_XLR:
5933 return bfd_mach_mips_xlr;
5934
b49e97c9
TS
5935 default:
5936 switch (flags & EF_MIPS_ARCH)
5937 {
5938 default:
5939 case E_MIPS_ARCH_1:
5940 return bfd_mach_mips3000;
b49e97c9
TS
5941
5942 case E_MIPS_ARCH_2:
5943 return bfd_mach_mips6000;
b49e97c9
TS
5944
5945 case E_MIPS_ARCH_3:
5946 return bfd_mach_mips4000;
b49e97c9
TS
5947
5948 case E_MIPS_ARCH_4:
5949 return bfd_mach_mips8000;
b49e97c9
TS
5950
5951 case E_MIPS_ARCH_5:
5952 return bfd_mach_mips5;
b49e97c9
TS
5953
5954 case E_MIPS_ARCH_32:
5955 return bfd_mach_mipsisa32;
b49e97c9
TS
5956
5957 case E_MIPS_ARCH_64:
5958 return bfd_mach_mipsisa64;
af7ee8bf
CD
5959
5960 case E_MIPS_ARCH_32R2:
5961 return bfd_mach_mipsisa32r2;
5f74bc13
CD
5962
5963 case E_MIPS_ARCH_64R2:
5964 return bfd_mach_mipsisa64r2;
b49e97c9
TS
5965 }
5966 }
5967
5968 return 0;
5969}
5970
5971/* Return printable name for ABI. */
5972
5973static INLINE char *
9719ad41 5974elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
5975{
5976 flagword flags;
5977
5978 flags = elf_elfheader (abfd)->e_flags;
5979 switch (flags & EF_MIPS_ABI)
5980 {
5981 case 0:
5982 if (ABI_N32_P (abfd))
5983 return "N32";
5984 else if (ABI_64_P (abfd))
5985 return "64";
5986 else
5987 return "none";
5988 case E_MIPS_ABI_O32:
5989 return "O32";
5990 case E_MIPS_ABI_O64:
5991 return "O64";
5992 case E_MIPS_ABI_EABI32:
5993 return "EABI32";
5994 case E_MIPS_ABI_EABI64:
5995 return "EABI64";
5996 default:
5997 return "unknown abi";
5998 }
5999}
6000\f
6001/* MIPS ELF uses two common sections. One is the usual one, and the
6002 other is for small objects. All the small objects are kept
6003 together, and then referenced via the gp pointer, which yields
6004 faster assembler code. This is what we use for the small common
6005 section. This approach is copied from ecoff.c. */
6006static asection mips_elf_scom_section;
6007static asymbol mips_elf_scom_symbol;
6008static asymbol *mips_elf_scom_symbol_ptr;
6009
6010/* MIPS ELF also uses an acommon section, which represents an
6011 allocated common symbol which may be overridden by a
6012 definition in a shared library. */
6013static asection mips_elf_acom_section;
6014static asymbol mips_elf_acom_symbol;
6015static asymbol *mips_elf_acom_symbol_ptr;
6016
738e5348 6017/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
6018
6019void
9719ad41 6020_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
6021{
6022 elf_symbol_type *elfsym;
6023
738e5348 6024 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6025 elfsym = (elf_symbol_type *) asym;
6026 switch (elfsym->internal_elf_sym.st_shndx)
6027 {
6028 case SHN_MIPS_ACOMMON:
6029 /* This section is used in a dynamically linked executable file.
6030 It is an allocated common section. The dynamic linker can
6031 either resolve these symbols to something in a shared
6032 library, or it can just leave them here. For our purposes,
6033 we can consider these symbols to be in a new section. */
6034 if (mips_elf_acom_section.name == NULL)
6035 {
6036 /* Initialize the acommon section. */
6037 mips_elf_acom_section.name = ".acommon";
6038 mips_elf_acom_section.flags = SEC_ALLOC;
6039 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6040 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6041 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6042 mips_elf_acom_symbol.name = ".acommon";
6043 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6044 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6045 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6046 }
6047 asym->section = &mips_elf_acom_section;
6048 break;
6049
6050 case SHN_COMMON:
6051 /* Common symbols less than the GP size are automatically
6052 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6053 if (asym->value > elf_gp_size (abfd)
b59eed79 6054 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6055 || IRIX_COMPAT (abfd) == ict_irix6)
6056 break;
6057 /* Fall through. */
6058 case SHN_MIPS_SCOMMON:
6059 if (mips_elf_scom_section.name == NULL)
6060 {
6061 /* Initialize the small common section. */
6062 mips_elf_scom_section.name = ".scommon";
6063 mips_elf_scom_section.flags = SEC_IS_COMMON;
6064 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6065 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6066 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6067 mips_elf_scom_symbol.name = ".scommon";
6068 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6069 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6070 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6071 }
6072 asym->section = &mips_elf_scom_section;
6073 asym->value = elfsym->internal_elf_sym.st_size;
6074 break;
6075
6076 case SHN_MIPS_SUNDEFINED:
6077 asym->section = bfd_und_section_ptr;
6078 break;
6079
b49e97c9 6080 case SHN_MIPS_TEXT:
00b4930b
TS
6081 {
6082 asection *section = bfd_get_section_by_name (abfd, ".text");
6083
6084 BFD_ASSERT (SGI_COMPAT (abfd));
6085 if (section != NULL)
6086 {
6087 asym->section = section;
6088 /* MIPS_TEXT is a bit special, the address is not an offset
6089 to the base of the .text section. So substract the section
6090 base address to make it an offset. */
6091 asym->value -= section->vma;
6092 }
6093 }
b49e97c9
TS
6094 break;
6095
6096 case SHN_MIPS_DATA:
00b4930b
TS
6097 {
6098 asection *section = bfd_get_section_by_name (abfd, ".data");
6099
6100 BFD_ASSERT (SGI_COMPAT (abfd));
6101 if (section != NULL)
6102 {
6103 asym->section = section;
6104 /* MIPS_DATA is a bit special, the address is not an offset
6105 to the base of the .data section. So substract the section
6106 base address to make it an offset. */
6107 asym->value -= section->vma;
6108 }
6109 }
b49e97c9 6110 break;
b49e97c9 6111 }
738e5348
RS
6112
6113 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6114 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6115 && (asym->value & 1) != 0)
6116 {
6117 asym->value--;
6118 elfsym->internal_elf_sym.st_other
6119 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6120 }
b49e97c9
TS
6121}
6122\f
8c946ed5
RS
6123/* Implement elf_backend_eh_frame_address_size. This differs from
6124 the default in the way it handles EABI64.
6125
6126 EABI64 was originally specified as an LP64 ABI, and that is what
6127 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6128 historically accepted the combination of -mabi=eabi and -mlong32,
6129 and this ILP32 variation has become semi-official over time.
6130 Both forms use elf32 and have pointer-sized FDE addresses.
6131
6132 If an EABI object was generated by GCC 4.0 or above, it will have
6133 an empty .gcc_compiled_longXX section, where XX is the size of longs
6134 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6135 have no special marking to distinguish them from LP64 objects.
6136
6137 We don't want users of the official LP64 ABI to be punished for the
6138 existence of the ILP32 variant, but at the same time, we don't want
6139 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6140 We therefore take the following approach:
6141
6142 - If ABFD contains a .gcc_compiled_longXX section, use it to
6143 determine the pointer size.
6144
6145 - Otherwise check the type of the first relocation. Assume that
6146 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6147
6148 - Otherwise punt.
6149
6150 The second check is enough to detect LP64 objects generated by pre-4.0
6151 compilers because, in the kind of output generated by those compilers,
6152 the first relocation will be associated with either a CIE personality
6153 routine or an FDE start address. Furthermore, the compilers never
6154 used a special (non-pointer) encoding for this ABI.
6155
6156 Checking the relocation type should also be safe because there is no
6157 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6158 did so. */
6159
6160unsigned int
6161_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6162{
6163 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6164 return 8;
6165 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6166 {
6167 bfd_boolean long32_p, long64_p;
6168
6169 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6170 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6171 if (long32_p && long64_p)
6172 return 0;
6173 if (long32_p)
6174 return 4;
6175 if (long64_p)
6176 return 8;
6177
6178 if (sec->reloc_count > 0
6179 && elf_section_data (sec)->relocs != NULL
6180 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6181 == R_MIPS_64))
6182 return 8;
6183
6184 return 0;
6185 }
6186 return 4;
6187}
6188\f
174fd7f9
RS
6189/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6190 relocations against two unnamed section symbols to resolve to the
6191 same address. For example, if we have code like:
6192
6193 lw $4,%got_disp(.data)($gp)
6194 lw $25,%got_disp(.text)($gp)
6195 jalr $25
6196
6197 then the linker will resolve both relocations to .data and the program
6198 will jump there rather than to .text.
6199
6200 We can work around this problem by giving names to local section symbols.
6201 This is also what the MIPSpro tools do. */
6202
6203bfd_boolean
6204_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6205{
6206 return SGI_COMPAT (abfd);
6207}
6208\f
b49e97c9
TS
6209/* Work over a section just before writing it out. This routine is
6210 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6211 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6212 a better way. */
6213
b34976b6 6214bfd_boolean
9719ad41 6215_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6216{
6217 if (hdr->sh_type == SHT_MIPS_REGINFO
6218 && hdr->sh_size > 0)
6219 {
6220 bfd_byte buf[4];
6221
6222 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6223 BFD_ASSERT (hdr->contents == NULL);
6224
6225 if (bfd_seek (abfd,
6226 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6227 SEEK_SET) != 0)
b34976b6 6228 return FALSE;
b49e97c9 6229 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6230 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6231 return FALSE;
b49e97c9
TS
6232 }
6233
6234 if (hdr->sh_type == SHT_MIPS_OPTIONS
6235 && hdr->bfd_section != NULL
f0abc2a1
AM
6236 && mips_elf_section_data (hdr->bfd_section) != NULL
6237 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6238 {
6239 bfd_byte *contents, *l, *lend;
6240
f0abc2a1
AM
6241 /* We stored the section contents in the tdata field in the
6242 set_section_contents routine. We save the section contents
6243 so that we don't have to read them again.
b49e97c9
TS
6244 At this point we know that elf_gp is set, so we can look
6245 through the section contents to see if there is an
6246 ODK_REGINFO structure. */
6247
f0abc2a1 6248 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6249 l = contents;
6250 lend = contents + hdr->sh_size;
6251 while (l + sizeof (Elf_External_Options) <= lend)
6252 {
6253 Elf_Internal_Options intopt;
6254
6255 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6256 &intopt);
1bc8074d
MR
6257 if (intopt.size < sizeof (Elf_External_Options))
6258 {
6259 (*_bfd_error_handler)
6260 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6261 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6262 break;
6263 }
b49e97c9
TS
6264 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6265 {
6266 bfd_byte buf[8];
6267
6268 if (bfd_seek (abfd,
6269 (hdr->sh_offset
6270 + (l - contents)
6271 + sizeof (Elf_External_Options)
6272 + (sizeof (Elf64_External_RegInfo) - 8)),
6273 SEEK_SET) != 0)
b34976b6 6274 return FALSE;
b49e97c9 6275 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6276 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6277 return FALSE;
b49e97c9
TS
6278 }
6279 else if (intopt.kind == ODK_REGINFO)
6280 {
6281 bfd_byte buf[4];
6282
6283 if (bfd_seek (abfd,
6284 (hdr->sh_offset
6285 + (l - contents)
6286 + sizeof (Elf_External_Options)
6287 + (sizeof (Elf32_External_RegInfo) - 4)),
6288 SEEK_SET) != 0)
b34976b6 6289 return FALSE;
b49e97c9 6290 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6291 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6292 return FALSE;
b49e97c9
TS
6293 }
6294 l += intopt.size;
6295 }
6296 }
6297
6298 if (hdr->bfd_section != NULL)
6299 {
6300 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6301
2d0f9ad9
JM
6302 /* .sbss is not handled specially here because the GNU/Linux
6303 prelinker can convert .sbss from NOBITS to PROGBITS and
6304 changing it back to NOBITS breaks the binary. The entry in
6305 _bfd_mips_elf_special_sections will ensure the correct flags
6306 are set on .sbss if BFD creates it without reading it from an
6307 input file, and without special handling here the flags set
6308 on it in an input file will be followed. */
b49e97c9
TS
6309 if (strcmp (name, ".sdata") == 0
6310 || strcmp (name, ".lit8") == 0
6311 || strcmp (name, ".lit4") == 0)
6312 {
6313 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6314 hdr->sh_type = SHT_PROGBITS;
6315 }
b49e97c9
TS
6316 else if (strcmp (name, ".srdata") == 0)
6317 {
6318 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6319 hdr->sh_type = SHT_PROGBITS;
6320 }
6321 else if (strcmp (name, ".compact_rel") == 0)
6322 {
6323 hdr->sh_flags = 0;
6324 hdr->sh_type = SHT_PROGBITS;
6325 }
6326 else if (strcmp (name, ".rtproc") == 0)
6327 {
6328 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6329 {
6330 unsigned int adjust;
6331
6332 adjust = hdr->sh_size % hdr->sh_addralign;
6333 if (adjust != 0)
6334 hdr->sh_size += hdr->sh_addralign - adjust;
6335 }
6336 }
6337 }
6338
b34976b6 6339 return TRUE;
b49e97c9
TS
6340}
6341
6342/* Handle a MIPS specific section when reading an object file. This
6343 is called when elfcode.h finds a section with an unknown type.
6344 This routine supports both the 32-bit and 64-bit ELF ABI.
6345
6346 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6347 how to. */
6348
b34976b6 6349bfd_boolean
6dc132d9
L
6350_bfd_mips_elf_section_from_shdr (bfd *abfd,
6351 Elf_Internal_Shdr *hdr,
6352 const char *name,
6353 int shindex)
b49e97c9
TS
6354{
6355 flagword flags = 0;
6356
6357 /* There ought to be a place to keep ELF backend specific flags, but
6358 at the moment there isn't one. We just keep track of the
6359 sections by their name, instead. Fortunately, the ABI gives
6360 suggested names for all the MIPS specific sections, so we will
6361 probably get away with this. */
6362 switch (hdr->sh_type)
6363 {
6364 case SHT_MIPS_LIBLIST:
6365 if (strcmp (name, ".liblist") != 0)
b34976b6 6366 return FALSE;
b49e97c9
TS
6367 break;
6368 case SHT_MIPS_MSYM:
6369 if (strcmp (name, ".msym") != 0)
b34976b6 6370 return FALSE;
b49e97c9
TS
6371 break;
6372 case SHT_MIPS_CONFLICT:
6373 if (strcmp (name, ".conflict") != 0)
b34976b6 6374 return FALSE;
b49e97c9
TS
6375 break;
6376 case SHT_MIPS_GPTAB:
0112cd26 6377 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6378 return FALSE;
b49e97c9
TS
6379 break;
6380 case SHT_MIPS_UCODE:
6381 if (strcmp (name, ".ucode") != 0)
b34976b6 6382 return FALSE;
b49e97c9
TS
6383 break;
6384 case SHT_MIPS_DEBUG:
6385 if (strcmp (name, ".mdebug") != 0)
b34976b6 6386 return FALSE;
b49e97c9
TS
6387 flags = SEC_DEBUGGING;
6388 break;
6389 case SHT_MIPS_REGINFO:
6390 if (strcmp (name, ".reginfo") != 0
6391 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6392 return FALSE;
b49e97c9
TS
6393 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6394 break;
6395 case SHT_MIPS_IFACE:
6396 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6397 return FALSE;
b49e97c9
TS
6398 break;
6399 case SHT_MIPS_CONTENT:
0112cd26 6400 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6401 return FALSE;
b49e97c9
TS
6402 break;
6403 case SHT_MIPS_OPTIONS:
cc2e31b9 6404 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6405 return FALSE;
b49e97c9
TS
6406 break;
6407 case SHT_MIPS_DWARF:
1b315056 6408 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6409 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6410 return FALSE;
b49e97c9
TS
6411 break;
6412 case SHT_MIPS_SYMBOL_LIB:
6413 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6414 return FALSE;
b49e97c9
TS
6415 break;
6416 case SHT_MIPS_EVENTS:
0112cd26
NC
6417 if (! CONST_STRNEQ (name, ".MIPS.events")
6418 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6419 return FALSE;
b49e97c9
TS
6420 break;
6421 default:
cc2e31b9 6422 break;
b49e97c9
TS
6423 }
6424
6dc132d9 6425 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6426 return FALSE;
b49e97c9
TS
6427
6428 if (flags)
6429 {
6430 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6431 (bfd_get_section_flags (abfd,
6432 hdr->bfd_section)
6433 | flags)))
b34976b6 6434 return FALSE;
b49e97c9
TS
6435 }
6436
6437 /* FIXME: We should record sh_info for a .gptab section. */
6438
6439 /* For a .reginfo section, set the gp value in the tdata information
6440 from the contents of this section. We need the gp value while
6441 processing relocs, so we just get it now. The .reginfo section
6442 is not used in the 64-bit MIPS ELF ABI. */
6443 if (hdr->sh_type == SHT_MIPS_REGINFO)
6444 {
6445 Elf32_External_RegInfo ext;
6446 Elf32_RegInfo s;
6447
9719ad41
RS
6448 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6449 &ext, 0, sizeof ext))
b34976b6 6450 return FALSE;
b49e97c9
TS
6451 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6452 elf_gp (abfd) = s.ri_gp_value;
6453 }
6454
6455 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6456 set the gp value based on what we find. We may see both
6457 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6458 they should agree. */
6459 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6460 {
6461 bfd_byte *contents, *l, *lend;
6462
9719ad41 6463 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6464 if (contents == NULL)
b34976b6 6465 return FALSE;
b49e97c9 6466 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6467 0, hdr->sh_size))
b49e97c9
TS
6468 {
6469 free (contents);
b34976b6 6470 return FALSE;
b49e97c9
TS
6471 }
6472 l = contents;
6473 lend = contents + hdr->sh_size;
6474 while (l + sizeof (Elf_External_Options) <= lend)
6475 {
6476 Elf_Internal_Options intopt;
6477
6478 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6479 &intopt);
1bc8074d
MR
6480 if (intopt.size < sizeof (Elf_External_Options))
6481 {
6482 (*_bfd_error_handler)
6483 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6484 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6485 break;
6486 }
b49e97c9
TS
6487 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6488 {
6489 Elf64_Internal_RegInfo intreg;
6490
6491 bfd_mips_elf64_swap_reginfo_in
6492 (abfd,
6493 ((Elf64_External_RegInfo *)
6494 (l + sizeof (Elf_External_Options))),
6495 &intreg);
6496 elf_gp (abfd) = intreg.ri_gp_value;
6497 }
6498 else if (intopt.kind == ODK_REGINFO)
6499 {
6500 Elf32_RegInfo intreg;
6501
6502 bfd_mips_elf32_swap_reginfo_in
6503 (abfd,
6504 ((Elf32_External_RegInfo *)
6505 (l + sizeof (Elf_External_Options))),
6506 &intreg);
6507 elf_gp (abfd) = intreg.ri_gp_value;
6508 }
6509 l += intopt.size;
6510 }
6511 free (contents);
6512 }
6513
b34976b6 6514 return TRUE;
b49e97c9
TS
6515}
6516
6517/* Set the correct type for a MIPS ELF section. We do this by the
6518 section name, which is a hack, but ought to work. This routine is
6519 used by both the 32-bit and the 64-bit ABI. */
6520
b34976b6 6521bfd_boolean
9719ad41 6522_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 6523{
0414f35b 6524 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
6525
6526 if (strcmp (name, ".liblist") == 0)
6527 {
6528 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 6529 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
6530 /* The sh_link field is set in final_write_processing. */
6531 }
6532 else if (strcmp (name, ".conflict") == 0)
6533 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 6534 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
6535 {
6536 hdr->sh_type = SHT_MIPS_GPTAB;
6537 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6538 /* The sh_info field is set in final_write_processing. */
6539 }
6540 else if (strcmp (name, ".ucode") == 0)
6541 hdr->sh_type = SHT_MIPS_UCODE;
6542 else if (strcmp (name, ".mdebug") == 0)
6543 {
6544 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 6545 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
6546 entsize of 0. FIXME: Does this matter? */
6547 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6548 hdr->sh_entsize = 0;
6549 else
6550 hdr->sh_entsize = 1;
6551 }
6552 else if (strcmp (name, ".reginfo") == 0)
6553 {
6554 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 6555 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
6556 entsize of 0x18. FIXME: Does this matter? */
6557 if (SGI_COMPAT (abfd))
6558 {
6559 if ((abfd->flags & DYNAMIC) != 0)
6560 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6561 else
6562 hdr->sh_entsize = 1;
6563 }
6564 else
6565 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6566 }
6567 else if (SGI_COMPAT (abfd)
6568 && (strcmp (name, ".hash") == 0
6569 || strcmp (name, ".dynamic") == 0
6570 || strcmp (name, ".dynstr") == 0))
6571 {
6572 if (SGI_COMPAT (abfd))
6573 hdr->sh_entsize = 0;
6574#if 0
8dc1a139 6575 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
6576 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6577#endif
6578 }
6579 else if (strcmp (name, ".got") == 0
6580 || strcmp (name, ".srdata") == 0
6581 || strcmp (name, ".sdata") == 0
6582 || strcmp (name, ".sbss") == 0
6583 || strcmp (name, ".lit4") == 0
6584 || strcmp (name, ".lit8") == 0)
6585 hdr->sh_flags |= SHF_MIPS_GPREL;
6586 else if (strcmp (name, ".MIPS.interfaces") == 0)
6587 {
6588 hdr->sh_type = SHT_MIPS_IFACE;
6589 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6590 }
0112cd26 6591 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
6592 {
6593 hdr->sh_type = SHT_MIPS_CONTENT;
6594 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6595 /* The sh_info field is set in final_write_processing. */
6596 }
cc2e31b9 6597 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
6598 {
6599 hdr->sh_type = SHT_MIPS_OPTIONS;
6600 hdr->sh_entsize = 1;
6601 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6602 }
1b315056
CS
6603 else if (CONST_STRNEQ (name, ".debug_")
6604 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
6605 {
6606 hdr->sh_type = SHT_MIPS_DWARF;
6607
6608 /* Irix facilities such as libexc expect a single .debug_frame
6609 per executable, the system ones have NOSTRIP set and the linker
6610 doesn't merge sections with different flags so ... */
6611 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6612 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6613 }
b49e97c9
TS
6614 else if (strcmp (name, ".MIPS.symlib") == 0)
6615 {
6616 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6617 /* The sh_link and sh_info fields are set in
6618 final_write_processing. */
6619 }
0112cd26
NC
6620 else if (CONST_STRNEQ (name, ".MIPS.events")
6621 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
6622 {
6623 hdr->sh_type = SHT_MIPS_EVENTS;
6624 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6625 /* The sh_link field is set in final_write_processing. */
6626 }
6627 else if (strcmp (name, ".msym") == 0)
6628 {
6629 hdr->sh_type = SHT_MIPS_MSYM;
6630 hdr->sh_flags |= SHF_ALLOC;
6631 hdr->sh_entsize = 8;
6632 }
6633
7a79a000
TS
6634 /* The generic elf_fake_sections will set up REL_HDR using the default
6635 kind of relocations. We used to set up a second header for the
6636 non-default kind of relocations here, but only NewABI would use
6637 these, and the IRIX ld doesn't like resulting empty RELA sections.
6638 Thus we create those header only on demand now. */
b49e97c9 6639
b34976b6 6640 return TRUE;
b49e97c9
TS
6641}
6642
6643/* Given a BFD section, try to locate the corresponding ELF section
6644 index. This is used by both the 32-bit and the 64-bit ABI.
6645 Actually, it's not clear to me that the 64-bit ABI supports these,
6646 but for non-PIC objects we will certainly want support for at least
6647 the .scommon section. */
6648
b34976b6 6649bfd_boolean
9719ad41
RS
6650_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6651 asection *sec, int *retval)
b49e97c9
TS
6652{
6653 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6654 {
6655 *retval = SHN_MIPS_SCOMMON;
b34976b6 6656 return TRUE;
b49e97c9
TS
6657 }
6658 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6659 {
6660 *retval = SHN_MIPS_ACOMMON;
b34976b6 6661 return TRUE;
b49e97c9 6662 }
b34976b6 6663 return FALSE;
b49e97c9
TS
6664}
6665\f
6666/* Hook called by the linker routine which adds symbols from an object
6667 file. We must handle the special MIPS section numbers here. */
6668
b34976b6 6669bfd_boolean
9719ad41 6670_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 6671 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
6672 flagword *flagsp ATTRIBUTE_UNUSED,
6673 asection **secp, bfd_vma *valp)
b49e97c9
TS
6674{
6675 if (SGI_COMPAT (abfd)
6676 && (abfd->flags & DYNAMIC) != 0
6677 && strcmp (*namep, "_rld_new_interface") == 0)
6678 {
8dc1a139 6679 /* Skip IRIX5 rld entry name. */
b49e97c9 6680 *namep = NULL;
b34976b6 6681 return TRUE;
b49e97c9
TS
6682 }
6683
eedecc07
DD
6684 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6685 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6686 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6687 a magic symbol resolved by the linker, we ignore this bogus definition
6688 of _gp_disp. New ABI objects do not suffer from this problem so this
6689 is not done for them. */
6690 if (!NEWABI_P(abfd)
6691 && (sym->st_shndx == SHN_ABS)
6692 && (strcmp (*namep, "_gp_disp") == 0))
6693 {
6694 *namep = NULL;
6695 return TRUE;
6696 }
6697
b49e97c9
TS
6698 switch (sym->st_shndx)
6699 {
6700 case SHN_COMMON:
6701 /* Common symbols less than the GP size are automatically
6702 treated as SHN_MIPS_SCOMMON symbols. */
6703 if (sym->st_size > elf_gp_size (abfd)
b59eed79 6704 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
6705 || IRIX_COMPAT (abfd) == ict_irix6)
6706 break;
6707 /* Fall through. */
6708 case SHN_MIPS_SCOMMON:
6709 *secp = bfd_make_section_old_way (abfd, ".scommon");
6710 (*secp)->flags |= SEC_IS_COMMON;
6711 *valp = sym->st_size;
6712 break;
6713
6714 case SHN_MIPS_TEXT:
6715 /* This section is used in a shared object. */
6716 if (elf_tdata (abfd)->elf_text_section == NULL)
6717 {
6718 asymbol *elf_text_symbol;
6719 asection *elf_text_section;
6720 bfd_size_type amt = sizeof (asection);
6721
6722 elf_text_section = bfd_zalloc (abfd, amt);
6723 if (elf_text_section == NULL)
b34976b6 6724 return FALSE;
b49e97c9
TS
6725
6726 amt = sizeof (asymbol);
6727 elf_text_symbol = bfd_zalloc (abfd, amt);
6728 if (elf_text_symbol == NULL)
b34976b6 6729 return FALSE;
b49e97c9
TS
6730
6731 /* Initialize the section. */
6732
6733 elf_tdata (abfd)->elf_text_section = elf_text_section;
6734 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6735
6736 elf_text_section->symbol = elf_text_symbol;
6737 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6738
6739 elf_text_section->name = ".text";
6740 elf_text_section->flags = SEC_NO_FLAGS;
6741 elf_text_section->output_section = NULL;
6742 elf_text_section->owner = abfd;
6743 elf_text_symbol->name = ".text";
6744 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6745 elf_text_symbol->section = elf_text_section;
6746 }
6747 /* This code used to do *secp = bfd_und_section_ptr if
6748 info->shared. I don't know why, and that doesn't make sense,
6749 so I took it out. */
6750 *secp = elf_tdata (abfd)->elf_text_section;
6751 break;
6752
6753 case SHN_MIPS_ACOMMON:
6754 /* Fall through. XXX Can we treat this as allocated data? */
6755 case SHN_MIPS_DATA:
6756 /* This section is used in a shared object. */
6757 if (elf_tdata (abfd)->elf_data_section == NULL)
6758 {
6759 asymbol *elf_data_symbol;
6760 asection *elf_data_section;
6761 bfd_size_type amt = sizeof (asection);
6762
6763 elf_data_section = bfd_zalloc (abfd, amt);
6764 if (elf_data_section == NULL)
b34976b6 6765 return FALSE;
b49e97c9
TS
6766
6767 amt = sizeof (asymbol);
6768 elf_data_symbol = bfd_zalloc (abfd, amt);
6769 if (elf_data_symbol == NULL)
b34976b6 6770 return FALSE;
b49e97c9
TS
6771
6772 /* Initialize the section. */
6773
6774 elf_tdata (abfd)->elf_data_section = elf_data_section;
6775 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6776
6777 elf_data_section->symbol = elf_data_symbol;
6778 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6779
6780 elf_data_section->name = ".data";
6781 elf_data_section->flags = SEC_NO_FLAGS;
6782 elf_data_section->output_section = NULL;
6783 elf_data_section->owner = abfd;
6784 elf_data_symbol->name = ".data";
6785 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6786 elf_data_symbol->section = elf_data_section;
6787 }
6788 /* This code used to do *secp = bfd_und_section_ptr if
6789 info->shared. I don't know why, and that doesn't make sense,
6790 so I took it out. */
6791 *secp = elf_tdata (abfd)->elf_data_section;
6792 break;
6793
6794 case SHN_MIPS_SUNDEFINED:
6795 *secp = bfd_und_section_ptr;
6796 break;
6797 }
6798
6799 if (SGI_COMPAT (abfd)
6800 && ! info->shared
f13a99db 6801 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
6802 && strcmp (*namep, "__rld_obj_head") == 0)
6803 {
6804 struct elf_link_hash_entry *h;
14a793b2 6805 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6806
6807 /* Mark __rld_obj_head as dynamic. */
14a793b2 6808 bh = NULL;
b49e97c9 6809 if (! (_bfd_generic_link_add_one_symbol
9719ad41 6810 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 6811 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6812 return FALSE;
14a793b2
AM
6813
6814 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6815 h->non_elf = 0;
6816 h->def_regular = 1;
b49e97c9
TS
6817 h->type = STT_OBJECT;
6818
c152c796 6819 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6820 return FALSE;
b49e97c9 6821
b34976b6 6822 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b49e97c9
TS
6823 }
6824
6825 /* If this is a mips16 text symbol, add 1 to the value to make it
6826 odd. This will cause something like .word SYM to come up with
6827 the right value when it is loaded into the PC. */
30c09090 6828 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
6829 ++*valp;
6830
b34976b6 6831 return TRUE;
b49e97c9
TS
6832}
6833
6834/* This hook function is called before the linker writes out a global
6835 symbol. We mark symbols as small common if appropriate. This is
6836 also where we undo the increment of the value for a mips16 symbol. */
6837
6e0b88f1 6838int
9719ad41
RS
6839_bfd_mips_elf_link_output_symbol_hook
6840 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6841 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6842 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
6843{
6844 /* If we see a common symbol, which implies a relocatable link, then
6845 if a symbol was small common in an input file, mark it as small
6846 common in the output file. */
6847 if (sym->st_shndx == SHN_COMMON
6848 && strcmp (input_sec->name, ".scommon") == 0)
6849 sym->st_shndx = SHN_MIPS_SCOMMON;
6850
30c09090 6851 if (ELF_ST_IS_MIPS16 (sym->st_other))
79cda7cf 6852 sym->st_value &= ~1;
b49e97c9 6853
6e0b88f1 6854 return 1;
b49e97c9
TS
6855}
6856\f
6857/* Functions for the dynamic linker. */
6858
6859/* Create dynamic sections when linking against a dynamic object. */
6860
b34976b6 6861bfd_boolean
9719ad41 6862_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
6863{
6864 struct elf_link_hash_entry *h;
14a793b2 6865 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6866 flagword flags;
6867 register asection *s;
6868 const char * const *namep;
0a44bf69 6869 struct mips_elf_link_hash_table *htab;
b49e97c9 6870
0a44bf69 6871 htab = mips_elf_hash_table (info);
b49e97c9
TS
6872 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6873 | SEC_LINKER_CREATED | SEC_READONLY);
6874
0a44bf69
RS
6875 /* The psABI requires a read-only .dynamic section, but the VxWorks
6876 EABI doesn't. */
6877 if (!htab->is_vxworks)
b49e97c9 6878 {
0a44bf69
RS
6879 s = bfd_get_section_by_name (abfd, ".dynamic");
6880 if (s != NULL)
6881 {
6882 if (! bfd_set_section_flags (abfd, s, flags))
6883 return FALSE;
6884 }
b49e97c9
TS
6885 }
6886
6887 /* We need to create .got section. */
23cc69b6 6888 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
6889 return FALSE;
6890
0a44bf69 6891 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 6892 return FALSE;
b49e97c9 6893
b49e97c9 6894 /* Create .stub section. */
4e41d0d7
RS
6895 s = bfd_make_section_with_flags (abfd,
6896 MIPS_ELF_STUB_SECTION_NAME (abfd),
6897 flags | SEC_CODE);
6898 if (s == NULL
6899 || ! bfd_set_section_alignment (abfd, s,
6900 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6901 return FALSE;
6902 htab->sstubs = s;
b49e97c9
TS
6903
6904 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6905 && !info->shared
6906 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6907 {
3496cb2a
L
6908 s = bfd_make_section_with_flags (abfd, ".rld_map",
6909 flags &~ (flagword) SEC_READONLY);
b49e97c9 6910 if (s == NULL
b49e97c9
TS
6911 || ! bfd_set_section_alignment (abfd, s,
6912 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 6913 return FALSE;
b49e97c9
TS
6914 }
6915
6916 /* On IRIX5, we adjust add some additional symbols and change the
6917 alignments of several sections. There is no ABI documentation
6918 indicating that this is necessary on IRIX6, nor any evidence that
6919 the linker takes such action. */
6920 if (IRIX_COMPAT (abfd) == ict_irix5)
6921 {
6922 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6923 {
14a793b2 6924 bh = NULL;
b49e97c9 6925 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
6926 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6927 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6928 return FALSE;
14a793b2
AM
6929
6930 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6931 h->non_elf = 0;
6932 h->def_regular = 1;
b49e97c9
TS
6933 h->type = STT_SECTION;
6934
c152c796 6935 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6936 return FALSE;
b49e97c9
TS
6937 }
6938
6939 /* We need to create a .compact_rel section. */
6940 if (SGI_COMPAT (abfd))
6941 {
6942 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 6943 return FALSE;
b49e97c9
TS
6944 }
6945
44c410de 6946 /* Change alignments of some sections. */
b49e97c9
TS
6947 s = bfd_get_section_by_name (abfd, ".hash");
6948 if (s != NULL)
d80dcc6a 6949 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6950 s = bfd_get_section_by_name (abfd, ".dynsym");
6951 if (s != NULL)
d80dcc6a 6952 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6953 s = bfd_get_section_by_name (abfd, ".dynstr");
6954 if (s != NULL)
d80dcc6a 6955 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6956 s = bfd_get_section_by_name (abfd, ".reginfo");
6957 if (s != NULL)
d80dcc6a 6958 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6959 s = bfd_get_section_by_name (abfd, ".dynamic");
6960 if (s != NULL)
d80dcc6a 6961 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6962 }
6963
6964 if (!info->shared)
6965 {
14a793b2
AM
6966 const char *name;
6967
6968 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6969 bh = NULL;
6970 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
6971 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6972 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6973 return FALSE;
14a793b2
AM
6974
6975 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6976 h->non_elf = 0;
6977 h->def_regular = 1;
b49e97c9
TS
6978 h->type = STT_SECTION;
6979
c152c796 6980 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6981 return FALSE;
b49e97c9
TS
6982
6983 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6984 {
6985 /* __rld_map is a four byte word located in the .data section
6986 and is filled in by the rtld to contain a pointer to
6987 the _r_debug structure. Its symbol value will be set in
6988 _bfd_mips_elf_finish_dynamic_symbol. */
6989 s = bfd_get_section_by_name (abfd, ".rld_map");
0abfb97a 6990 BFD_ASSERT (s != NULL);
14a793b2 6991
0abfb97a
L
6992 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6993 bh = NULL;
6994 if (!(_bfd_generic_link_add_one_symbol
6995 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6996 get_elf_backend_data (abfd)->collect, &bh)))
6997 return FALSE;
b49e97c9 6998
0abfb97a
L
6999 h = (struct elf_link_hash_entry *) bh;
7000 h->non_elf = 0;
7001 h->def_regular = 1;
7002 h->type = STT_OBJECT;
7003
7004 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7005 return FALSE;
b49e97c9
TS
7006 }
7007 }
7008
861fb55a
DJ
7009 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7010 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7011 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7012 return FALSE;
7013
7014 /* Cache the sections created above. */
7015 htab->splt = bfd_get_section_by_name (abfd, ".plt");
7016 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
0a44bf69
RS
7017 if (htab->is_vxworks)
7018 {
0a44bf69
RS
7019 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7020 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
861fb55a
DJ
7021 }
7022 else
7023 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7024 if (!htab->sdynbss
7025 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7026 || !htab->srelplt
7027 || !htab->splt)
7028 abort ();
0a44bf69 7029
861fb55a
DJ
7030 if (htab->is_vxworks)
7031 {
0a44bf69
RS
7032 /* Do the usual VxWorks handling. */
7033 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7034 return FALSE;
7035
7036 /* Work out the PLT sizes. */
7037 if (info->shared)
7038 {
7039 htab->plt_header_size
7040 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7041 htab->plt_entry_size
7042 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7043 }
7044 else
7045 {
7046 htab->plt_header_size
7047 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7048 htab->plt_entry_size
7049 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7050 }
7051 }
861fb55a
DJ
7052 else if (!info->shared)
7053 {
7054 /* All variants of the plt0 entry are the same size. */
7055 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7056 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7057 }
0a44bf69 7058
b34976b6 7059 return TRUE;
b49e97c9
TS
7060}
7061\f
c224138d
RS
7062/* Return true if relocation REL against section SEC is a REL rather than
7063 RELA relocation. RELOCS is the first relocation in the section and
7064 ABFD is the bfd that contains SEC. */
7065
7066static bfd_boolean
7067mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7068 const Elf_Internal_Rela *relocs,
7069 const Elf_Internal_Rela *rel)
7070{
7071 Elf_Internal_Shdr *rel_hdr;
7072 const struct elf_backend_data *bed;
7073
7074 /* To determine which flavor or relocation this is, we depend on the
7075 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
7076 rel_hdr = &elf_section_data (sec)->rel_hdr;
7077 bed = get_elf_backend_data (abfd);
7078 if ((size_t) (rel - relocs)
7079 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7080 rel_hdr = elf_section_data (sec)->rel_hdr2;
7081 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
7082}
7083
7084/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7085 HOWTO is the relocation's howto and CONTENTS points to the contents
7086 of the section that REL is against. */
7087
7088static bfd_vma
7089mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7090 reloc_howto_type *howto, bfd_byte *contents)
7091{
7092 bfd_byte *location;
7093 unsigned int r_type;
7094 bfd_vma addend;
7095
7096 r_type = ELF_R_TYPE (abfd, rel->r_info);
7097 location = contents + rel->r_offset;
7098
7099 /* Get the addend, which is stored in the input file. */
7100 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7101 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7102 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7103
7104 return addend & howto->src_mask;
7105}
7106
7107/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7108 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7109 and update *ADDEND with the final addend. Return true on success
7110 or false if the LO16 could not be found. RELEND is the exclusive
7111 upper bound on the relocations for REL's section. */
7112
7113static bfd_boolean
7114mips_elf_add_lo16_rel_addend (bfd *abfd,
7115 const Elf_Internal_Rela *rel,
7116 const Elf_Internal_Rela *relend,
7117 bfd_byte *contents, bfd_vma *addend)
7118{
7119 unsigned int r_type, lo16_type;
7120 const Elf_Internal_Rela *lo16_relocation;
7121 reloc_howto_type *lo16_howto;
7122 bfd_vma l;
7123
7124 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7125 if (mips16_reloc_p (r_type))
c224138d
RS
7126 lo16_type = R_MIPS16_LO16;
7127 else
7128 lo16_type = R_MIPS_LO16;
7129
7130 /* The combined value is the sum of the HI16 addend, left-shifted by
7131 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7132 code does a `lui' of the HI16 value, and then an `addiu' of the
7133 LO16 value.)
7134
7135 Scan ahead to find a matching LO16 relocation.
7136
7137 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7138 be immediately following. However, for the IRIX6 ABI, the next
7139 relocation may be a composed relocation consisting of several
7140 relocations for the same address. In that case, the R_MIPS_LO16
7141 relocation may occur as one of these. We permit a similar
7142 extension in general, as that is useful for GCC.
7143
7144 In some cases GCC dead code elimination removes the LO16 but keeps
7145 the corresponding HI16. This is strictly speaking a violation of
7146 the ABI but not immediately harmful. */
7147 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7148 if (lo16_relocation == NULL)
7149 return FALSE;
7150
7151 /* Obtain the addend kept there. */
7152 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7153 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7154
7155 l <<= lo16_howto->rightshift;
7156 l = _bfd_mips_elf_sign_extend (l, 16);
7157
7158 *addend <<= 16;
7159 *addend += l;
7160 return TRUE;
7161}
7162
7163/* Try to read the contents of section SEC in bfd ABFD. Return true and
7164 store the contents in *CONTENTS on success. Assume that *CONTENTS
7165 already holds the contents if it is nonull on entry. */
7166
7167static bfd_boolean
7168mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7169{
7170 if (*contents)
7171 return TRUE;
7172
7173 /* Get cached copy if it exists. */
7174 if (elf_section_data (sec)->this_hdr.contents != NULL)
7175 {
7176 *contents = elf_section_data (sec)->this_hdr.contents;
7177 return TRUE;
7178 }
7179
7180 return bfd_malloc_and_get_section (abfd, sec, contents);
7181}
7182
b49e97c9
TS
7183/* Look through the relocs for a section during the first phase, and
7184 allocate space in the global offset table. */
7185
b34976b6 7186bfd_boolean
9719ad41
RS
7187_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7188 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7189{
7190 const char *name;
7191 bfd *dynobj;
7192 Elf_Internal_Shdr *symtab_hdr;
7193 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7194 size_t extsymoff;
7195 const Elf_Internal_Rela *rel;
7196 const Elf_Internal_Rela *rel_end;
b49e97c9 7197 asection *sreloc;
9c5bfbb7 7198 const struct elf_backend_data *bed;
0a44bf69 7199 struct mips_elf_link_hash_table *htab;
c224138d
RS
7200 bfd_byte *contents;
7201 bfd_vma addend;
7202 reloc_howto_type *howto;
b49e97c9 7203
1049f94e 7204 if (info->relocatable)
b34976b6 7205 return TRUE;
b49e97c9 7206
0a44bf69 7207 htab = mips_elf_hash_table (info);
b49e97c9
TS
7208 dynobj = elf_hash_table (info)->dynobj;
7209 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7210 sym_hashes = elf_sym_hashes (abfd);
7211 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7212
738e5348
RS
7213 bed = get_elf_backend_data (abfd);
7214 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7215
b49e97c9
TS
7216 /* Check for the mips16 stub sections. */
7217
7218 name = bfd_get_section_name (abfd, sec);
b9d58d71 7219 if (FN_STUB_P (name))
b49e97c9
TS
7220 {
7221 unsigned long r_symndx;
7222
7223 /* Look at the relocation information to figure out which symbol
7224 this is for. */
7225
738e5348
RS
7226 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7227 if (r_symndx == 0)
7228 {
7229 (*_bfd_error_handler)
7230 (_("%B: Warning: cannot determine the target function for"
7231 " stub section `%s'"),
7232 abfd, name);
7233 bfd_set_error (bfd_error_bad_value);
7234 return FALSE;
7235 }
b49e97c9
TS
7236
7237 if (r_symndx < extsymoff
7238 || sym_hashes[r_symndx - extsymoff] == NULL)
7239 {
7240 asection *o;
7241
7242 /* This stub is for a local symbol. This stub will only be
7243 needed if there is some relocation in this BFD, other
7244 than a 16 bit function call, which refers to this symbol. */
7245 for (o = abfd->sections; o != NULL; o = o->next)
7246 {
7247 Elf_Internal_Rela *sec_relocs;
7248 const Elf_Internal_Rela *r, *rend;
7249
7250 /* We can ignore stub sections when looking for relocs. */
7251 if ((o->flags & SEC_RELOC) == 0
7252 || o->reloc_count == 0
738e5348 7253 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7254 continue;
7255
45d6a902 7256 sec_relocs
9719ad41 7257 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7258 info->keep_memory);
b49e97c9 7259 if (sec_relocs == NULL)
b34976b6 7260 return FALSE;
b49e97c9
TS
7261
7262 rend = sec_relocs + o->reloc_count;
7263 for (r = sec_relocs; r < rend; r++)
7264 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7265 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7266 break;
7267
6cdc0ccc 7268 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7269 free (sec_relocs);
7270
7271 if (r < rend)
7272 break;
7273 }
7274
7275 if (o == NULL)
7276 {
7277 /* There is no non-call reloc for this stub, so we do
7278 not need it. Since this function is called before
7279 the linker maps input sections to output sections, we
7280 can easily discard it by setting the SEC_EXCLUDE
7281 flag. */
7282 sec->flags |= SEC_EXCLUDE;
b34976b6 7283 return TRUE;
b49e97c9
TS
7284 }
7285
7286 /* Record this stub in an array of local symbol stubs for
7287 this BFD. */
7288 if (elf_tdata (abfd)->local_stubs == NULL)
7289 {
7290 unsigned long symcount;
7291 asection **n;
7292 bfd_size_type amt;
7293
7294 if (elf_bad_symtab (abfd))
7295 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7296 else
7297 symcount = symtab_hdr->sh_info;
7298 amt = symcount * sizeof (asection *);
9719ad41 7299 n = bfd_zalloc (abfd, amt);
b49e97c9 7300 if (n == NULL)
b34976b6 7301 return FALSE;
b49e97c9
TS
7302 elf_tdata (abfd)->local_stubs = n;
7303 }
7304
b9d58d71 7305 sec->flags |= SEC_KEEP;
b49e97c9
TS
7306 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7307
7308 /* We don't need to set mips16_stubs_seen in this case.
7309 That flag is used to see whether we need to look through
7310 the global symbol table for stubs. We don't need to set
7311 it here, because we just have a local stub. */
7312 }
7313 else
7314 {
7315 struct mips_elf_link_hash_entry *h;
7316
7317 h = ((struct mips_elf_link_hash_entry *)
7318 sym_hashes[r_symndx - extsymoff]);
7319
973a3492
L
7320 while (h->root.root.type == bfd_link_hash_indirect
7321 || h->root.root.type == bfd_link_hash_warning)
7322 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7323
b49e97c9
TS
7324 /* H is the symbol this stub is for. */
7325
b9d58d71
TS
7326 /* If we already have an appropriate stub for this function, we
7327 don't need another one, so we can discard this one. Since
7328 this function is called before the linker maps input sections
7329 to output sections, we can easily discard it by setting the
7330 SEC_EXCLUDE flag. */
7331 if (h->fn_stub != NULL)
7332 {
7333 sec->flags |= SEC_EXCLUDE;
7334 return TRUE;
7335 }
7336
7337 sec->flags |= SEC_KEEP;
b49e97c9 7338 h->fn_stub = sec;
b34976b6 7339 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7340 }
7341 }
b9d58d71 7342 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7343 {
7344 unsigned long r_symndx;
7345 struct mips_elf_link_hash_entry *h;
7346 asection **loc;
7347
7348 /* Look at the relocation information to figure out which symbol
7349 this is for. */
7350
738e5348
RS
7351 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7352 if (r_symndx == 0)
7353 {
7354 (*_bfd_error_handler)
7355 (_("%B: Warning: cannot determine the target function for"
7356 " stub section `%s'"),
7357 abfd, name);
7358 bfd_set_error (bfd_error_bad_value);
7359 return FALSE;
7360 }
b49e97c9
TS
7361
7362 if (r_symndx < extsymoff
7363 || sym_hashes[r_symndx - extsymoff] == NULL)
7364 {
b9d58d71 7365 asection *o;
b49e97c9 7366
b9d58d71
TS
7367 /* This stub is for a local symbol. This stub will only be
7368 needed if there is some relocation (R_MIPS16_26) in this BFD
7369 that refers to this symbol. */
7370 for (o = abfd->sections; o != NULL; o = o->next)
7371 {
7372 Elf_Internal_Rela *sec_relocs;
7373 const Elf_Internal_Rela *r, *rend;
7374
7375 /* We can ignore stub sections when looking for relocs. */
7376 if ((o->flags & SEC_RELOC) == 0
7377 || o->reloc_count == 0
738e5348 7378 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7379 continue;
7380
7381 sec_relocs
7382 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7383 info->keep_memory);
7384 if (sec_relocs == NULL)
7385 return FALSE;
7386
7387 rend = sec_relocs + o->reloc_count;
7388 for (r = sec_relocs; r < rend; r++)
7389 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7390 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7391 break;
7392
7393 if (elf_section_data (o)->relocs != sec_relocs)
7394 free (sec_relocs);
7395
7396 if (r < rend)
7397 break;
7398 }
7399
7400 if (o == NULL)
7401 {
7402 /* There is no non-call reloc for this stub, so we do
7403 not need it. Since this function is called before
7404 the linker maps input sections to output sections, we
7405 can easily discard it by setting the SEC_EXCLUDE
7406 flag. */
7407 sec->flags |= SEC_EXCLUDE;
7408 return TRUE;
7409 }
7410
7411 /* Record this stub in an array of local symbol call_stubs for
7412 this BFD. */
7413 if (elf_tdata (abfd)->local_call_stubs == NULL)
7414 {
7415 unsigned long symcount;
7416 asection **n;
7417 bfd_size_type amt;
7418
7419 if (elf_bad_symtab (abfd))
7420 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7421 else
7422 symcount = symtab_hdr->sh_info;
7423 amt = symcount * sizeof (asection *);
7424 n = bfd_zalloc (abfd, amt);
7425 if (n == NULL)
7426 return FALSE;
7427 elf_tdata (abfd)->local_call_stubs = n;
7428 }
b49e97c9 7429
b9d58d71
TS
7430 sec->flags |= SEC_KEEP;
7431 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7432
b9d58d71
TS
7433 /* We don't need to set mips16_stubs_seen in this case.
7434 That flag is used to see whether we need to look through
7435 the global symbol table for stubs. We don't need to set
7436 it here, because we just have a local stub. */
7437 }
b49e97c9 7438 else
b49e97c9 7439 {
b9d58d71
TS
7440 h = ((struct mips_elf_link_hash_entry *)
7441 sym_hashes[r_symndx - extsymoff]);
7442
7443 /* H is the symbol this stub is for. */
7444
7445 if (CALL_FP_STUB_P (name))
7446 loc = &h->call_fp_stub;
7447 else
7448 loc = &h->call_stub;
7449
7450 /* If we already have an appropriate stub for this function, we
7451 don't need another one, so we can discard this one. Since
7452 this function is called before the linker maps input sections
7453 to output sections, we can easily discard it by setting the
7454 SEC_EXCLUDE flag. */
7455 if (*loc != NULL)
7456 {
7457 sec->flags |= SEC_EXCLUDE;
7458 return TRUE;
7459 }
b49e97c9 7460
b9d58d71
TS
7461 sec->flags |= SEC_KEEP;
7462 *loc = sec;
7463 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7464 }
b49e97c9
TS
7465 }
7466
b49e97c9 7467 sreloc = NULL;
c224138d 7468 contents = NULL;
b49e97c9
TS
7469 for (rel = relocs; rel < rel_end; ++rel)
7470 {
7471 unsigned long r_symndx;
7472 unsigned int r_type;
7473 struct elf_link_hash_entry *h;
861fb55a 7474 bfd_boolean can_make_dynamic_p;
b49e97c9
TS
7475
7476 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7477 r_type = ELF_R_TYPE (abfd, rel->r_info);
7478
7479 if (r_symndx < extsymoff)
7480 h = NULL;
7481 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7482 {
7483 (*_bfd_error_handler)
d003868e
AM
7484 (_("%B: Malformed reloc detected for section %s"),
7485 abfd, name);
b49e97c9 7486 bfd_set_error (bfd_error_bad_value);
b34976b6 7487 return FALSE;
b49e97c9
TS
7488 }
7489 else
7490 {
7491 h = sym_hashes[r_symndx - extsymoff];
3e08fb72
NC
7492 while (h != NULL
7493 && (h->root.type == bfd_link_hash_indirect
7494 || h->root.type == bfd_link_hash_warning))
861fb55a
DJ
7495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7496 }
b49e97c9 7497
861fb55a
DJ
7498 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7499 relocation into a dynamic one. */
7500 can_make_dynamic_p = FALSE;
7501 switch (r_type)
7502 {
7503 case R_MIPS16_GOT16:
7504 case R_MIPS16_CALL16:
7505 case R_MIPS_GOT16:
7506 case R_MIPS_CALL16:
7507 case R_MIPS_CALL_HI16:
7508 case R_MIPS_CALL_LO16:
7509 case R_MIPS_GOT_HI16:
7510 case R_MIPS_GOT_LO16:
7511 case R_MIPS_GOT_PAGE:
7512 case R_MIPS_GOT_OFST:
7513 case R_MIPS_GOT_DISP:
7514 case R_MIPS_TLS_GOTTPREL:
7515 case R_MIPS_TLS_GD:
7516 case R_MIPS_TLS_LDM:
7517 if (dynobj == NULL)
7518 elf_hash_table (info)->dynobj = dynobj = abfd;
7519 if (!mips_elf_create_got_section (dynobj, info))
7520 return FALSE;
7521 if (htab->is_vxworks && !info->shared)
b49e97c9 7522 {
861fb55a
DJ
7523 (*_bfd_error_handler)
7524 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7525 abfd, (unsigned long) rel->r_offset);
7526 bfd_set_error (bfd_error_bad_value);
7527 return FALSE;
b49e97c9 7528 }
861fb55a 7529 break;
b49e97c9 7530
99da6b5f
AN
7531 /* This is just a hint; it can safely be ignored. Don't set
7532 has_static_relocs for the corresponding symbol. */
7533 case R_MIPS_JALR:
7534 break;
7535
861fb55a
DJ
7536 case R_MIPS_32:
7537 case R_MIPS_REL32:
7538 case R_MIPS_64:
7539 /* In VxWorks executables, references to external symbols
7540 must be handled using copy relocs or PLT entries; it is not
7541 possible to convert this relocation into a dynamic one.
7542
7543 For executables that use PLTs and copy-relocs, we have a
7544 choice between converting the relocation into a dynamic
7545 one or using copy relocations or PLT entries. It is
7546 usually better to do the former, unless the relocation is
7547 against a read-only section. */
7548 if ((info->shared
7549 || (h != NULL
7550 && !htab->is_vxworks
7551 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7552 && !(!info->nocopyreloc
7553 && !PIC_OBJECT_P (abfd)
7554 && MIPS_ELF_READONLY_SECTION (sec))))
7555 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 7556 {
861fb55a 7557 can_make_dynamic_p = TRUE;
b49e97c9
TS
7558 if (dynobj == NULL)
7559 elf_hash_table (info)->dynobj = dynobj = abfd;
b49e97c9 7560 break;
861fb55a
DJ
7561 }
7562 /* Fall through. */
b49e97c9 7563
861fb55a
DJ
7564 default:
7565 /* Most static relocations require pointer equality, except
7566 for branches. */
7567 if (h)
7568 h->pointer_equality_needed = TRUE;
7569 /* Fall through. */
b49e97c9 7570
861fb55a
DJ
7571 case R_MIPS_26:
7572 case R_MIPS_PC16:
7573 case R_MIPS16_26:
7574 if (h)
7575 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7576 break;
b49e97c9
TS
7577 }
7578
0a44bf69
RS
7579 if (h)
7580 {
0a44bf69
RS
7581 /* Relocations against the special VxWorks __GOTT_BASE__ and
7582 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7583 room for them in .rela.dyn. */
7584 if (is_gott_symbol (info, h))
7585 {
7586 if (sreloc == NULL)
7587 {
7588 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7589 if (sreloc == NULL)
7590 return FALSE;
7591 }
7592 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
7593 if (MIPS_ELF_READONLY_SECTION (sec))
7594 /* We tell the dynamic linker that there are
7595 relocations against the text segment. */
7596 info->flags |= DF_TEXTREL;
0a44bf69
RS
7597 }
7598 }
7599 else if (r_type == R_MIPS_CALL_LO16
7600 || r_type == R_MIPS_GOT_LO16
7601 || r_type == R_MIPS_GOT_DISP
738e5348 7602 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
7603 {
7604 /* We may need a local GOT entry for this relocation. We
7605 don't count R_MIPS_GOT_PAGE because we can estimate the
7606 maximum number of pages needed by looking at the size of
738e5348
RS
7607 the segment. Similar comments apply to R_MIPS*_GOT16 and
7608 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 7609 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 7610 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 7611 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0
RS
7612 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7613 rel->r_addend, info, 0))
f4416af6 7614 return FALSE;
b49e97c9
TS
7615 }
7616
861fb55a
DJ
7617 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7618 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7619
b49e97c9
TS
7620 switch (r_type)
7621 {
7622 case R_MIPS_CALL16:
738e5348 7623 case R_MIPS16_CALL16:
b49e97c9
TS
7624 if (h == NULL)
7625 {
7626 (*_bfd_error_handler)
d003868e
AM
7627 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7628 abfd, (unsigned long) rel->r_offset);
b49e97c9 7629 bfd_set_error (bfd_error_bad_value);
b34976b6 7630 return FALSE;
b49e97c9
TS
7631 }
7632 /* Fall through. */
7633
7634 case R_MIPS_CALL_HI16:
7635 case R_MIPS_CALL_LO16:
7636 if (h != NULL)
7637 {
d334575b 7638 /* VxWorks call relocations point at the function's .got.plt
0a44bf69
RS
7639 entry, which will be allocated by adjust_dynamic_symbol.
7640 Otherwise, this symbol requires a global GOT entry. */
8275b357 7641 if ((!htab->is_vxworks || h->forced_local)
a8028dd0 7642 && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
b34976b6 7643 return FALSE;
b49e97c9
TS
7644
7645 /* We need a stub, not a plt entry for the undefined
7646 function. But we record it as if it needs plt. See
c152c796 7647 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 7648 h->needs_plt = 1;
b49e97c9
TS
7649 h->type = STT_FUNC;
7650 }
7651 break;
7652
0fdc1bf1
AO
7653 case R_MIPS_GOT_PAGE:
7654 /* If this is a global, overridable symbol, GOT_PAGE will
7655 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d 7656 if (h)
0fdc1bf1
AO
7657 {
7658 struct mips_elf_link_hash_entry *hmips =
7659 (struct mips_elf_link_hash_entry *) h;
143d77c5 7660
3a3b6725 7661 /* This symbol is definitely not overridable. */
f5385ebf 7662 if (hmips->root.def_regular
0fdc1bf1 7663 && ! (info->shared && ! info->symbolic
f5385ebf 7664 && ! hmips->root.forced_local))
c224138d 7665 h = NULL;
0fdc1bf1
AO
7666 }
7667 /* Fall through. */
7668
738e5348 7669 case R_MIPS16_GOT16:
b49e97c9
TS
7670 case R_MIPS_GOT16:
7671 case R_MIPS_GOT_HI16:
7672 case R_MIPS_GOT_LO16:
3a3b6725 7673 if (!h || r_type == R_MIPS_GOT_PAGE)
c224138d 7674 {
3a3b6725
DJ
7675 /* This relocation needs (or may need, if h != NULL) a
7676 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7677 know for sure until we know whether the symbol is
7678 preemptible. */
c224138d
RS
7679 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7680 {
7681 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7682 return FALSE;
7683 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7684 addend = mips_elf_read_rel_addend (abfd, rel,
7685 howto, contents);
7686 if (r_type == R_MIPS_GOT16)
7687 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7688 contents, &addend);
7689 else
7690 addend <<= howto->rightshift;
7691 }
7692 else
7693 addend = rel->r_addend;
a8028dd0
RS
7694 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7695 addend))
c224138d
RS
7696 return FALSE;
7697 break;
7698 }
7699 /* Fall through. */
7700
b49e97c9 7701 case R_MIPS_GOT_DISP:
a8028dd0 7702 if (h && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
b34976b6 7703 return FALSE;
b49e97c9
TS
7704 break;
7705
0f20cc35
DJ
7706 case R_MIPS_TLS_GOTTPREL:
7707 if (info->shared)
7708 info->flags |= DF_STATIC_TLS;
7709 /* Fall through */
7710
7711 case R_MIPS_TLS_LDM:
7712 if (r_type == R_MIPS_TLS_LDM)
7713 {
7714 r_symndx = 0;
7715 h = NULL;
7716 }
7717 /* Fall through */
7718
7719 case R_MIPS_TLS_GD:
7720 /* This symbol requires a global offset table entry, or two
7721 for TLS GD relocations. */
7722 {
7723 unsigned char flag = (r_type == R_MIPS_TLS_GD
7724 ? GOT_TLS_GD
7725 : r_type == R_MIPS_TLS_LDM
7726 ? GOT_TLS_LDM
7727 : GOT_TLS_IE);
7728 if (h != NULL)
7729 {
7730 struct mips_elf_link_hash_entry *hmips =
7731 (struct mips_elf_link_hash_entry *) h;
7732 hmips->tls_type |= flag;
7733
a8028dd0
RS
7734 if (h && !mips_elf_record_global_got_symbol (h, abfd,
7735 info, flag))
0f20cc35
DJ
7736 return FALSE;
7737 }
7738 else
7739 {
7740 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7741
a8028dd0
RS
7742 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7743 rel->r_addend,
7744 info, flag))
0f20cc35
DJ
7745 return FALSE;
7746 }
7747 }
7748 break;
7749
b49e97c9
TS
7750 case R_MIPS_32:
7751 case R_MIPS_REL32:
7752 case R_MIPS_64:
0a44bf69
RS
7753 /* In VxWorks executables, references to external symbols
7754 are handled using copy relocs or PLT stubs, so there's
7755 no need to add a .rela.dyn entry for this relocation. */
861fb55a 7756 if (can_make_dynamic_p)
b49e97c9
TS
7757 {
7758 if (sreloc == NULL)
7759 {
0a44bf69 7760 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 7761 if (sreloc == NULL)
f4416af6 7762 return FALSE;
b49e97c9 7763 }
9a59ad6b 7764 if (info->shared && h == NULL)
82f0cfbd
EC
7765 {
7766 /* When creating a shared object, we must copy these
7767 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
7768 relocs. Make room for this reloc in .rel(a).dyn. */
7769 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 7770 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7771 /* We tell the dynamic linker that there are
7772 relocations against the text segment. */
7773 info->flags |= DF_TEXTREL;
7774 }
b49e97c9
TS
7775 else
7776 {
7777 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 7778
9a59ad6b
DJ
7779 /* For a shared object, we must copy this relocation
7780 unless the symbol turns out to be undefined and
7781 weak with non-default visibility, in which case
7782 it will be left as zero.
7783
7784 We could elide R_MIPS_REL32 for locally binding symbols
7785 in shared libraries, but do not yet do so.
7786
7787 For an executable, we only need to copy this
7788 reloc if the symbol is defined in a dynamic
7789 object. */
b49e97c9
TS
7790 hmips = (struct mips_elf_link_hash_entry *) h;
7791 ++hmips->possibly_dynamic_relocs;
943284cc 7792 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7793 /* We need it to tell the dynamic linker if there
7794 are relocations against the text segment. */
7795 hmips->readonly_reloc = TRUE;
b49e97c9 7796 }
b49e97c9
TS
7797 }
7798
7799 if (SGI_COMPAT (abfd))
7800 mips_elf_hash_table (info)->compact_rel_size +=
7801 sizeof (Elf32_External_crinfo);
7802 break;
7803
7804 case R_MIPS_26:
7805 case R_MIPS_GPREL16:
7806 case R_MIPS_LITERAL:
7807 case R_MIPS_GPREL32:
7808 if (SGI_COMPAT (abfd))
7809 mips_elf_hash_table (info)->compact_rel_size +=
7810 sizeof (Elf32_External_crinfo);
7811 break;
7812
7813 /* This relocation describes the C++ object vtable hierarchy.
7814 Reconstruct it for later use during GC. */
7815 case R_MIPS_GNU_VTINHERIT:
c152c796 7816 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 7817 return FALSE;
b49e97c9
TS
7818 break;
7819
7820 /* This relocation describes which C++ vtable entries are actually
7821 used. Record for later use during GC. */
7822 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
7823 BFD_ASSERT (h != NULL);
7824 if (h != NULL
7825 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 7826 return FALSE;
b49e97c9
TS
7827 break;
7828
7829 default:
7830 break;
7831 }
7832
7833 /* We must not create a stub for a symbol that has relocations
0a44bf69
RS
7834 related to taking the function's address. This doesn't apply to
7835 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7836 a normal .got entry. */
7837 if (!htab->is_vxworks && h != NULL)
7838 switch (r_type)
7839 {
7840 default:
7841 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7842 break;
738e5348 7843 case R_MIPS16_CALL16:
0a44bf69
RS
7844 case R_MIPS_CALL16:
7845 case R_MIPS_CALL_HI16:
7846 case R_MIPS_CALL_LO16:
7847 case R_MIPS_JALR:
7848 break;
7849 }
b49e97c9 7850
738e5348
RS
7851 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7852 if there is one. We only need to handle global symbols here;
7853 we decide whether to keep or delete stubs for local symbols
7854 when processing the stub's relocations. */
b49e97c9 7855 if (h != NULL
738e5348
RS
7856 && !mips16_call_reloc_p (r_type)
7857 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
7858 {
7859 struct mips_elf_link_hash_entry *mh;
7860
7861 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 7862 mh->need_fn_stub = TRUE;
b49e97c9 7863 }
861fb55a
DJ
7864
7865 /* Refuse some position-dependent relocations when creating a
7866 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7867 not PIC, but we can create dynamic relocations and the result
7868 will be fine. Also do not refuse R_MIPS_LO16, which can be
7869 combined with R_MIPS_GOT16. */
7870 if (info->shared)
7871 {
7872 switch (r_type)
7873 {
7874 case R_MIPS16_HI16:
7875 case R_MIPS_HI16:
7876 case R_MIPS_HIGHER:
7877 case R_MIPS_HIGHEST:
7878 /* Don't refuse a high part relocation if it's against
7879 no symbol (e.g. part of a compound relocation). */
7880 if (r_symndx == 0)
7881 break;
7882
7883 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7884 and has a special meaning. */
7885 if (!NEWABI_P (abfd) && h != NULL
7886 && strcmp (h->root.root.string, "_gp_disp") == 0)
7887 break;
7888
7889 /* FALLTHROUGH */
7890
7891 case R_MIPS16_26:
7892 case R_MIPS_26:
7893 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7894 (*_bfd_error_handler)
7895 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7896 abfd, howto->name,
7897 (h) ? h->root.root.string : "a local symbol");
7898 bfd_set_error (bfd_error_bad_value);
7899 return FALSE;
7900 default:
7901 break;
7902 }
7903 }
b49e97c9
TS
7904 }
7905
b34976b6 7906 return TRUE;
b49e97c9
TS
7907}
7908\f
d0647110 7909bfd_boolean
9719ad41
RS
7910_bfd_mips_relax_section (bfd *abfd, asection *sec,
7911 struct bfd_link_info *link_info,
7912 bfd_boolean *again)
d0647110
AO
7913{
7914 Elf_Internal_Rela *internal_relocs;
7915 Elf_Internal_Rela *irel, *irelend;
7916 Elf_Internal_Shdr *symtab_hdr;
7917 bfd_byte *contents = NULL;
d0647110
AO
7918 size_t extsymoff;
7919 bfd_boolean changed_contents = FALSE;
7920 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7921 Elf_Internal_Sym *isymbuf = NULL;
7922
7923 /* We are not currently changing any sizes, so only one pass. */
7924 *again = FALSE;
7925
1049f94e 7926 if (link_info->relocatable)
d0647110
AO
7927 return TRUE;
7928
9719ad41 7929 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 7930 link_info->keep_memory);
d0647110
AO
7931 if (internal_relocs == NULL)
7932 return TRUE;
7933
7934 irelend = internal_relocs + sec->reloc_count
7935 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7936 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7937 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7938
7939 for (irel = internal_relocs; irel < irelend; irel++)
7940 {
7941 bfd_vma symval;
7942 bfd_signed_vma sym_offset;
7943 unsigned int r_type;
7944 unsigned long r_symndx;
7945 asection *sym_sec;
7946 unsigned long instruction;
7947
7948 /* Turn jalr into bgezal, and jr into beq, if they're marked
7949 with a JALR relocation, that indicate where they jump to.
7950 This saves some pipeline bubbles. */
7951 r_type = ELF_R_TYPE (abfd, irel->r_info);
7952 if (r_type != R_MIPS_JALR)
7953 continue;
7954
7955 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7956 /* Compute the address of the jump target. */
7957 if (r_symndx >= extsymoff)
7958 {
7959 struct mips_elf_link_hash_entry *h
7960 = ((struct mips_elf_link_hash_entry *)
7961 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7962
7963 while (h->root.root.type == bfd_link_hash_indirect
7964 || h->root.root.type == bfd_link_hash_warning)
7965 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 7966
d0647110
AO
7967 /* If a symbol is undefined, or if it may be overridden,
7968 skip it. */
7969 if (! ((h->root.root.type == bfd_link_hash_defined
7970 || h->root.root.type == bfd_link_hash_defweak)
7971 && h->root.root.u.def.section)
7972 || (link_info->shared && ! link_info->symbolic
f5385ebf 7973 && !h->root.forced_local))
d0647110
AO
7974 continue;
7975
7976 sym_sec = h->root.root.u.def.section;
7977 if (sym_sec->output_section)
7978 symval = (h->root.root.u.def.value
7979 + sym_sec->output_section->vma
7980 + sym_sec->output_offset);
7981 else
7982 symval = h->root.root.u.def.value;
7983 }
7984 else
7985 {
7986 Elf_Internal_Sym *isym;
7987
7988 /* Read this BFD's symbols if we haven't done so already. */
7989 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7990 {
7991 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7992 if (isymbuf == NULL)
7993 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7994 symtab_hdr->sh_info, 0,
7995 NULL, NULL, NULL);
7996 if (isymbuf == NULL)
7997 goto relax_return;
7998 }
7999
8000 isym = isymbuf + r_symndx;
8001 if (isym->st_shndx == SHN_UNDEF)
8002 continue;
8003 else if (isym->st_shndx == SHN_ABS)
8004 sym_sec = bfd_abs_section_ptr;
8005 else if (isym->st_shndx == SHN_COMMON)
8006 sym_sec = bfd_com_section_ptr;
8007 else
8008 sym_sec
8009 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8010 symval = isym->st_value
8011 + sym_sec->output_section->vma
8012 + sym_sec->output_offset;
8013 }
8014
8015 /* Compute branch offset, from delay slot of the jump to the
8016 branch target. */
8017 sym_offset = (symval + irel->r_addend)
8018 - (sec_start + irel->r_offset + 4);
8019
8020 /* Branch offset must be properly aligned. */
8021 if ((sym_offset & 3) != 0)
8022 continue;
8023
8024 sym_offset >>= 2;
8025
8026 /* Check that it's in range. */
8027 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8028 continue;
143d77c5 8029
d0647110 8030 /* Get the section contents if we haven't done so already. */
c224138d
RS
8031 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8032 goto relax_return;
d0647110
AO
8033
8034 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8035
8036 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8037 if ((instruction & 0xfc1fffff) == 0x0000f809)
8038 instruction = 0x04110000;
8039 /* If it was jr <reg>, turn it into b <target>. */
8040 else if ((instruction & 0xfc1fffff) == 0x00000008)
8041 instruction = 0x10000000;
8042 else
8043 continue;
8044
8045 instruction |= (sym_offset & 0xffff);
8046 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8047 changed_contents = TRUE;
8048 }
8049
8050 if (contents != NULL
8051 && elf_section_data (sec)->this_hdr.contents != contents)
8052 {
8053 if (!changed_contents && !link_info->keep_memory)
8054 free (contents);
8055 else
8056 {
8057 /* Cache the section contents for elf_link_input_bfd. */
8058 elf_section_data (sec)->this_hdr.contents = contents;
8059 }
8060 }
8061 return TRUE;
8062
143d77c5 8063 relax_return:
eea6121a
AM
8064 if (contents != NULL
8065 && elf_section_data (sec)->this_hdr.contents != contents)
8066 free (contents);
d0647110
AO
8067 return FALSE;
8068}
8069\f
9a59ad6b
DJ
8070/* Allocate space for global sym dynamic relocs. */
8071
8072static bfd_boolean
8073allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8074{
8075 struct bfd_link_info *info = inf;
8076 bfd *dynobj;
8077 struct mips_elf_link_hash_entry *hmips;
8078 struct mips_elf_link_hash_table *htab;
8079
8080 htab = mips_elf_hash_table (info);
8081 dynobj = elf_hash_table (info)->dynobj;
8082 hmips = (struct mips_elf_link_hash_entry *) h;
8083
8084 /* VxWorks executables are handled elsewhere; we only need to
8085 allocate relocations in shared objects. */
8086 if (htab->is_vxworks && !info->shared)
8087 return TRUE;
8088
63897e2c
RS
8089 /* Ignore indirect and warning symbols. All relocations against
8090 such symbols will be redirected to the target symbol. */
8091 if (h->root.type == bfd_link_hash_indirect
8092 || h->root.type == bfd_link_hash_warning)
8093 return TRUE;
8094
9a59ad6b
DJ
8095 /* If this symbol is defined in a dynamic object, or we are creating
8096 a shared library, we will need to copy any R_MIPS_32 or
8097 R_MIPS_REL32 relocs against it into the output file. */
8098 if (! info->relocatable
8099 && hmips->possibly_dynamic_relocs != 0
8100 && (h->root.type == bfd_link_hash_defweak
8101 || !h->def_regular
8102 || info->shared))
8103 {
8104 bfd_boolean do_copy = TRUE;
8105
8106 if (h->root.type == bfd_link_hash_undefweak)
8107 {
8108 /* Do not copy relocations for undefined weak symbols with
8109 non-default visibility. */
8110 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8111 do_copy = FALSE;
8112
8113 /* Make sure undefined weak symbols are output as a dynamic
8114 symbol in PIEs. */
8115 else if (h->dynindx == -1 && !h->forced_local)
8116 {
8117 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8118 return FALSE;
8119 }
8120 }
8121
8122 if (do_copy)
8123 {
aff469fa
RS
8124 /* Even though we don't directly need a GOT entry for this symbol,
8125 a symbol must have a dynamic symbol table index greater that
8126 DT_MIPS_GOTSYM if there are dynamic relocations against it. */
8127 if (hmips->global_got_area > GGA_RELOC_ONLY)
8128 hmips->global_got_area = GGA_RELOC_ONLY;
8129
9a59ad6b
DJ
8130 mips_elf_allocate_dynamic_relocations
8131 (dynobj, info, hmips->possibly_dynamic_relocs);
8132 if (hmips->readonly_reloc)
8133 /* We tell the dynamic linker that there are relocations
8134 against the text segment. */
8135 info->flags |= DF_TEXTREL;
8136 }
8137 }
8138
8139 return TRUE;
8140}
8141
b49e97c9
TS
8142/* Adjust a symbol defined by a dynamic object and referenced by a
8143 regular object. The current definition is in some section of the
8144 dynamic object, but we're not including those sections. We have to
8145 change the definition to something the rest of the link can
8146 understand. */
8147
b34976b6 8148bfd_boolean
9719ad41
RS
8149_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8150 struct elf_link_hash_entry *h)
b49e97c9
TS
8151{
8152 bfd *dynobj;
8153 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8154 struct mips_elf_link_hash_table *htab;
b49e97c9 8155
5108fc1b 8156 htab = mips_elf_hash_table (info);
b49e97c9 8157 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8158 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8159
8160 /* Make sure we know what is going on here. */
8161 BFD_ASSERT (dynobj != NULL
f5385ebf 8162 && (h->needs_plt
f6e332e6 8163 || h->u.weakdef != NULL
f5385ebf
AM
8164 || (h->def_dynamic
8165 && h->ref_regular
8166 && !h->def_regular)));
b49e97c9 8167
b49e97c9 8168 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8169
861fb55a
DJ
8170 /* If there are call relocations against an externally-defined symbol,
8171 see whether we can create a MIPS lazy-binding stub for it. We can
8172 only do this if all references to the function are through call
8173 relocations, and in that case, the traditional lazy-binding stubs
8174 are much more efficient than PLT entries.
8175
8176 Traditional stubs are only available on SVR4 psABI-based systems;
8177 VxWorks always uses PLTs instead. */
8178 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8179 {
8180 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8181 return TRUE;
b49e97c9
TS
8182
8183 /* If this symbol is not defined in a regular file, then set
8184 the symbol to the stub location. This is required to make
8185 function pointers compare as equal between the normal
8186 executable and the shared library. */
f5385ebf 8187 if (!h->def_regular)
b49e97c9 8188 {
33bb52fb
RS
8189 hmips->needs_lazy_stub = TRUE;
8190 htab->lazy_stub_count++;
b34976b6 8191 return TRUE;
b49e97c9
TS
8192 }
8193 }
861fb55a
DJ
8194 /* As above, VxWorks requires PLT entries for externally-defined
8195 functions that are only accessed through call relocations.
b49e97c9 8196
861fb55a
DJ
8197 Both VxWorks and non-VxWorks targets also need PLT entries if there
8198 are static-only relocations against an externally-defined function.
8199 This can technically occur for shared libraries if there are
8200 branches to the symbol, although it is unlikely that this will be
8201 used in practice due to the short ranges involved. It can occur
8202 for any relative or absolute relocation in executables; in that
8203 case, the PLT entry becomes the function's canonical address. */
8204 else if (((h->needs_plt && !hmips->no_fn_stub)
8205 || (h->type == STT_FUNC && hmips->has_static_relocs))
8206 && htab->use_plts_and_copy_relocs
8207 && !SYMBOL_CALLS_LOCAL (info, h)
8208 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8209 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8210 {
861fb55a
DJ
8211 /* If this is the first symbol to need a PLT entry, allocate room
8212 for the header. */
8213 if (htab->splt->size == 0)
8214 {
8215 BFD_ASSERT (htab->sgotplt->size == 0);
0a44bf69 8216
861fb55a
DJ
8217 /* If we're using the PLT additions to the psABI, each PLT
8218 entry is 16 bytes and the PLT0 entry is 32 bytes.
8219 Encourage better cache usage by aligning. We do this
8220 lazily to avoid pessimizing traditional objects. */
8221 if (!htab->is_vxworks
8222 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8223 return FALSE;
0a44bf69 8224
861fb55a
DJ
8225 /* Make sure that .got.plt is word-aligned. We do this lazily
8226 for the same reason as above. */
8227 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8228 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8229 return FALSE;
0a44bf69 8230
861fb55a 8231 htab->splt->size += htab->plt_header_size;
0a44bf69 8232
861fb55a
DJ
8233 /* On non-VxWorks targets, the first two entries in .got.plt
8234 are reserved. */
8235 if (!htab->is_vxworks)
8236 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
0a44bf69 8237
861fb55a
DJ
8238 /* On VxWorks, also allocate room for the header's
8239 .rela.plt.unloaded entries. */
8240 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8241 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8242 }
8243
8244 /* Assign the next .plt entry to this symbol. */
8245 h->plt.offset = htab->splt->size;
8246 htab->splt->size += htab->plt_entry_size;
8247
8248 /* If the output file has no definition of the symbol, set the
861fb55a 8249 symbol's value to the address of the stub. */
131eb6b7 8250 if (!info->shared && !h->def_regular)
0a44bf69
RS
8251 {
8252 h->root.u.def.section = htab->splt;
8253 h->root.u.def.value = h->plt.offset;
861fb55a
DJ
8254 /* For VxWorks, point at the PLT load stub rather than the
8255 lazy resolution stub; this stub will become the canonical
8256 function address. */
8257 if (htab->is_vxworks)
8258 h->root.u.def.value += 8;
0a44bf69
RS
8259 }
8260
861fb55a
DJ
8261 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8262 relocation. */
8263 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8264 htab->srelplt->size += (htab->is_vxworks
8265 ? MIPS_ELF_RELA_SIZE (dynobj)
8266 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8267
8268 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8269 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8270 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8271
861fb55a
DJ
8272 /* All relocations against this symbol that could have been made
8273 dynamic will now refer to the PLT entry instead. */
8274 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8275
0a44bf69
RS
8276 return TRUE;
8277 }
8278
8279 /* If this is a weak symbol, and there is a real definition, the
8280 processor independent code will have arranged for us to see the
8281 real definition first, and we can just use the same value. */
8282 if (h->u.weakdef != NULL)
8283 {
8284 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8285 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8286 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8287 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8288 return TRUE;
8289 }
8290
861fb55a
DJ
8291 /* Otherwise, there is nothing further to do for symbols defined
8292 in regular objects. */
8293 if (h->def_regular)
0a44bf69
RS
8294 return TRUE;
8295
861fb55a
DJ
8296 /* There's also nothing more to do if we'll convert all relocations
8297 against this symbol into dynamic relocations. */
8298 if (!hmips->has_static_relocs)
8299 return TRUE;
8300
8301 /* We're now relying on copy relocations. Complain if we have
8302 some that we can't convert. */
8303 if (!htab->use_plts_and_copy_relocs || info->shared)
8304 {
8305 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8306 "dynamic symbol %s"),
8307 h->root.root.string);
8308 bfd_set_error (bfd_error_bad_value);
8309 return FALSE;
8310 }
8311
0a44bf69
RS
8312 /* We must allocate the symbol in our .dynbss section, which will
8313 become part of the .bss section of the executable. There will be
8314 an entry for this symbol in the .dynsym section. The dynamic
8315 object will contain position independent code, so all references
8316 from the dynamic object to this symbol will go through the global
8317 offset table. The dynamic linker will use the .dynsym entry to
8318 determine the address it must put in the global offset table, so
8319 both the dynamic object and the regular object will refer to the
8320 same memory location for the variable. */
8321
8322 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8323 {
861fb55a
DJ
8324 if (htab->is_vxworks)
8325 htab->srelbss->size += sizeof (Elf32_External_Rela);
8326 else
8327 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
8328 h->needs_copy = 1;
8329 }
8330
861fb55a
DJ
8331 /* All relocations against this symbol that could have been made
8332 dynamic will now refer to the local copy instead. */
8333 hmips->possibly_dynamic_relocs = 0;
8334
027297b7 8335 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 8336}
b49e97c9
TS
8337\f
8338/* This function is called after all the input files have been read,
8339 and the input sections have been assigned to output sections. We
8340 check for any mips16 stub sections that we can discard. */
8341
b34976b6 8342bfd_boolean
9719ad41
RS
8343_bfd_mips_elf_always_size_sections (bfd *output_bfd,
8344 struct bfd_link_info *info)
b49e97c9
TS
8345{
8346 asection *ri;
0a44bf69 8347 struct mips_elf_link_hash_table *htab;
861fb55a 8348 struct mips_htab_traverse_info hti;
0a44bf69
RS
8349
8350 htab = mips_elf_hash_table (info);
f4416af6 8351
b49e97c9
TS
8352 /* The .reginfo section has a fixed size. */
8353 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8354 if (ri != NULL)
9719ad41 8355 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 8356
861fb55a
DJ
8357 hti.info = info;
8358 hti.output_bfd = output_bfd;
8359 hti.error = FALSE;
8360 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8361 mips_elf_check_symbols, &hti);
8362 if (hti.error)
8363 return FALSE;
f4416af6 8364
33bb52fb
RS
8365 return TRUE;
8366}
8367
8368/* If the link uses a GOT, lay it out and work out its size. */
8369
8370static bfd_boolean
8371mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8372{
8373 bfd *dynobj;
8374 asection *s;
8375 struct mips_got_info *g;
33bb52fb
RS
8376 bfd_size_type loadable_size = 0;
8377 bfd_size_type page_gotno;
8378 bfd *sub;
8379 struct mips_elf_count_tls_arg count_tls_arg;
8380 struct mips_elf_link_hash_table *htab;
8381
8382 htab = mips_elf_hash_table (info);
a8028dd0 8383 s = htab->sgot;
f4416af6 8384 if (s == NULL)
b34976b6 8385 return TRUE;
b49e97c9 8386
33bb52fb 8387 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
8388 g = htab->got_info;
8389
861fb55a
DJ
8390 /* Allocate room for the reserved entries. VxWorks always reserves
8391 3 entries; other objects only reserve 2 entries. */
8392 BFD_ASSERT (g->assigned_gotno == 0);
8393 if (htab->is_vxworks)
8394 htab->reserved_gotno = 3;
8395 else
8396 htab->reserved_gotno = 2;
8397 g->local_gotno += htab->reserved_gotno;
8398 g->assigned_gotno = htab->reserved_gotno;
8399
33bb52fb
RS
8400 /* Replace entries for indirect and warning symbols with entries for
8401 the target symbol. */
8402 if (!mips_elf_resolve_final_got_entries (g))
8403 return FALSE;
f4416af6 8404
d4596a51
RS
8405 /* Count the number of GOT symbols. */
8406 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, g);
f4416af6 8407
33bb52fb
RS
8408 /* Calculate the total loadable size of the output. That
8409 will give us the maximum number of GOT_PAGE entries
8410 required. */
8411 for (sub = info->input_bfds; sub; sub = sub->link_next)
8412 {
8413 asection *subsection;
5108fc1b 8414
33bb52fb
RS
8415 for (subsection = sub->sections;
8416 subsection;
8417 subsection = subsection->next)
8418 {
8419 if ((subsection->flags & SEC_ALLOC) == 0)
8420 continue;
8421 loadable_size += ((subsection->size + 0xf)
8422 &~ (bfd_size_type) 0xf);
8423 }
8424 }
f4416af6 8425
0a44bf69 8426 if (htab->is_vxworks)
738e5348 8427 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
8428 relocations against local symbols evaluate to "G", and the EABI does
8429 not include R_MIPS_GOT_PAGE. */
c224138d 8430 page_gotno = 0;
0a44bf69
RS
8431 else
8432 /* Assume there are two loadable segments consisting of contiguous
8433 sections. Is 5 enough? */
c224138d
RS
8434 page_gotno = (loadable_size >> 16) + 5;
8435
8436 /* Choose the smaller of the two estimates; both are intended to be
8437 conservative. */
8438 if (page_gotno > g->page_gotno)
8439 page_gotno = g->page_gotno;
f4416af6 8440
c224138d 8441 g->local_gotno += page_gotno;
eea6121a 8442 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
d4596a51 8443 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6 8444
0f20cc35
DJ
8445 /* We need to calculate tls_gotno for global symbols at this point
8446 instead of building it up earlier, to avoid doublecounting
8447 entries for one global symbol from multiple input files. */
8448 count_tls_arg.info = info;
8449 count_tls_arg.needed = 0;
8450 elf_link_hash_traverse (elf_hash_table (info),
8451 mips_elf_count_global_tls_entries,
8452 &count_tls_arg);
8453 g->tls_gotno += count_tls_arg.needed;
8454 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8455
0a44bf69
RS
8456 /* VxWorks does not support multiple GOTs. It initializes $gp to
8457 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8458 dynamic loader. */
33bb52fb
RS
8459 if (htab->is_vxworks)
8460 {
8461 /* VxWorks executables do not need a GOT. */
8462 if (info->shared)
8463 {
8464 /* Each VxWorks GOT entry needs an explicit relocation. */
8465 unsigned int count;
8466
861fb55a 8467 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
33bb52fb
RS
8468 if (count)
8469 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8470 }
8471 }
8472 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 8473 {
a8028dd0 8474 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
8475 return FALSE;
8476 }
8477 else
8478 {
33bb52fb
RS
8479 struct mips_elf_count_tls_arg arg;
8480
8481 /* Set up TLS entries. */
0f20cc35
DJ
8482 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8483 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
33bb52fb
RS
8484
8485 /* Allocate room for the TLS relocations. */
8486 arg.info = info;
8487 arg.needed = 0;
8488 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8489 elf_link_hash_traverse (elf_hash_table (info),
8490 mips_elf_count_global_tls_relocs,
8491 &arg);
8492 if (arg.needed)
8493 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
0f20cc35 8494 }
b49e97c9 8495
b34976b6 8496 return TRUE;
b49e97c9
TS
8497}
8498
33bb52fb
RS
8499/* Estimate the size of the .MIPS.stubs section. */
8500
8501static void
8502mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8503{
8504 struct mips_elf_link_hash_table *htab;
8505 bfd_size_type dynsymcount;
8506
8507 htab = mips_elf_hash_table (info);
8508 if (htab->lazy_stub_count == 0)
8509 return;
8510
8511 /* IRIX rld assumes that a function stub isn't at the end of the .text
8512 section, so add a dummy entry to the end. */
8513 htab->lazy_stub_count++;
8514
8515 /* Get a worst-case estimate of the number of dynamic symbols needed.
8516 At this point, dynsymcount does not account for section symbols
8517 and count_section_dynsyms may overestimate the number that will
8518 be needed. */
8519 dynsymcount = (elf_hash_table (info)->dynsymcount
8520 + count_section_dynsyms (output_bfd, info));
8521
8522 /* Determine the size of one stub entry. */
8523 htab->function_stub_size = (dynsymcount > 0x10000
8524 ? MIPS_FUNCTION_STUB_BIG_SIZE
8525 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8526
8527 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8528}
8529
8530/* A mips_elf_link_hash_traverse callback for which DATA points to the
8531 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8532 allocate an entry in the stubs section. */
8533
8534static bfd_boolean
8535mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8536{
8537 struct mips_elf_link_hash_table *htab;
8538
8539 htab = (struct mips_elf_link_hash_table *) data;
8540 if (h->needs_lazy_stub)
8541 {
8542 h->root.root.u.def.section = htab->sstubs;
8543 h->root.root.u.def.value = htab->sstubs->size;
8544 h->root.plt.offset = htab->sstubs->size;
8545 htab->sstubs->size += htab->function_stub_size;
8546 }
8547 return TRUE;
8548}
8549
8550/* Allocate offsets in the stubs section to each symbol that needs one.
8551 Set the final size of the .MIPS.stub section. */
8552
8553static void
8554mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8555{
8556 struct mips_elf_link_hash_table *htab;
8557
8558 htab = mips_elf_hash_table (info);
8559 if (htab->lazy_stub_count == 0)
8560 return;
8561
8562 htab->sstubs->size = 0;
8563 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8564 mips_elf_allocate_lazy_stub, htab);
8565 htab->sstubs->size += htab->function_stub_size;
8566 BFD_ASSERT (htab->sstubs->size
8567 == htab->lazy_stub_count * htab->function_stub_size);
8568}
8569
b49e97c9
TS
8570/* Set the sizes of the dynamic sections. */
8571
b34976b6 8572bfd_boolean
9719ad41
RS
8573_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8574 struct bfd_link_info *info)
b49e97c9
TS
8575{
8576 bfd *dynobj;
861fb55a 8577 asection *s, *sreldyn;
b34976b6 8578 bfd_boolean reltext;
0a44bf69 8579 struct mips_elf_link_hash_table *htab;
b49e97c9 8580
0a44bf69 8581 htab = mips_elf_hash_table (info);
b49e97c9
TS
8582 dynobj = elf_hash_table (info)->dynobj;
8583 BFD_ASSERT (dynobj != NULL);
8584
8585 if (elf_hash_table (info)->dynamic_sections_created)
8586 {
8587 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 8588 if (info->executable)
b49e97c9
TS
8589 {
8590 s = bfd_get_section_by_name (dynobj, ".interp");
8591 BFD_ASSERT (s != NULL);
eea6121a 8592 s->size
b49e97c9
TS
8593 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8594 s->contents
8595 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8596 }
861fb55a
DJ
8597
8598 /* Create a symbol for the PLT, if we know that we are using it. */
8599 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8600 {
8601 struct elf_link_hash_entry *h;
8602
8603 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8604
8605 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8606 "_PROCEDURE_LINKAGE_TABLE_");
8607 htab->root.hplt = h;
8608 if (h == NULL)
8609 return FALSE;
8610 h->type = STT_FUNC;
8611 }
8612 }
4e41d0d7 8613
9a59ad6b
DJ
8614 /* Allocate space for global sym dynamic relocs. */
8615 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8616
33bb52fb
RS
8617 mips_elf_estimate_stub_size (output_bfd, info);
8618
8619 if (!mips_elf_lay_out_got (output_bfd, info))
8620 return FALSE;
8621
8622 mips_elf_lay_out_lazy_stubs (info);
8623
b49e97c9
TS
8624 /* The check_relocs and adjust_dynamic_symbol entry points have
8625 determined the sizes of the various dynamic sections. Allocate
8626 memory for them. */
b34976b6 8627 reltext = FALSE;
b49e97c9
TS
8628 for (s = dynobj->sections; s != NULL; s = s->next)
8629 {
8630 const char *name;
b49e97c9
TS
8631
8632 /* It's OK to base decisions on the section name, because none
8633 of the dynobj section names depend upon the input files. */
8634 name = bfd_get_section_name (dynobj, s);
8635
8636 if ((s->flags & SEC_LINKER_CREATED) == 0)
8637 continue;
8638
0112cd26 8639 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 8640 {
c456f082 8641 if (s->size != 0)
b49e97c9
TS
8642 {
8643 const char *outname;
8644 asection *target;
8645
8646 /* If this relocation section applies to a read only
8647 section, then we probably need a DT_TEXTREL entry.
0a44bf69 8648 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
8649 assert a DT_TEXTREL entry rather than testing whether
8650 there exists a relocation to a read only section or
8651 not. */
8652 outname = bfd_get_section_name (output_bfd,
8653 s->output_section);
8654 target = bfd_get_section_by_name (output_bfd, outname + 4);
8655 if ((target != NULL
8656 && (target->flags & SEC_READONLY) != 0
8657 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 8658 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 8659 reltext = TRUE;
b49e97c9
TS
8660
8661 /* We use the reloc_count field as a counter if we need
8662 to copy relocs into the output file. */
0a44bf69 8663 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 8664 s->reloc_count = 0;
f4416af6
AO
8665
8666 /* If combreloc is enabled, elf_link_sort_relocs() will
8667 sort relocations, but in a different way than we do,
8668 and before we're done creating relocations. Also, it
8669 will move them around between input sections'
8670 relocation's contents, so our sorting would be
8671 broken, so don't let it run. */
8672 info->combreloc = 0;
b49e97c9
TS
8673 }
8674 }
b49e97c9
TS
8675 else if (! info->shared
8676 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 8677 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 8678 {
5108fc1b 8679 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 8680 rtld to contain a pointer to the _r_debug structure. */
eea6121a 8681 s->size += 4;
b49e97c9
TS
8682 }
8683 else if (SGI_COMPAT (output_bfd)
0112cd26 8684 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 8685 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
8686 else if (s == htab->splt)
8687 {
8688 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
8689 room for an extra nop to fill the delay slot. This is
8690 for CPUs without load interlocking. */
8691 if (! LOAD_INTERLOCKS_P (output_bfd)
8692 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
8693 s->size += 4;
8694 }
0112cd26 8695 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 8696 && s != htab->sgot
0a44bf69 8697 && s != htab->sgotplt
861fb55a
DJ
8698 && s != htab->sstubs
8699 && s != htab->sdynbss)
b49e97c9
TS
8700 {
8701 /* It's not one of our sections, so don't allocate space. */
8702 continue;
8703 }
8704
c456f082 8705 if (s->size == 0)
b49e97c9 8706 {
8423293d 8707 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
8708 continue;
8709 }
8710
c456f082
AM
8711 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8712 continue;
8713
b49e97c9 8714 /* Allocate memory for the section contents. */
eea6121a 8715 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 8716 if (s->contents == NULL)
b49e97c9
TS
8717 {
8718 bfd_set_error (bfd_error_no_memory);
b34976b6 8719 return FALSE;
b49e97c9
TS
8720 }
8721 }
8722
8723 if (elf_hash_table (info)->dynamic_sections_created)
8724 {
8725 /* Add some entries to the .dynamic section. We fill in the
8726 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8727 must add the entries now so that we get the correct size for
5750dcec 8728 the .dynamic section. */
af5978fb
RS
8729
8730 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec
DJ
8731 DT_MIPS_RLD_MAP entry. This must come first because glibc
8732 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8733 looks at the first one it sees. */
af5978fb
RS
8734 if (!info->shared
8735 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8736 return FALSE;
b49e97c9 8737
5750dcec
DJ
8738 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8739 used by the debugger. */
8740 if (info->executable
8741 && !SGI_COMPAT (output_bfd)
8742 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8743 return FALSE;
8744
0a44bf69 8745 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
8746 info->flags |= DF_TEXTREL;
8747
8748 if ((info->flags & DF_TEXTREL) != 0)
8749 {
8750 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 8751 return FALSE;
943284cc
DJ
8752
8753 /* Clear the DF_TEXTREL flag. It will be set again if we
8754 write out an actual text relocation; we may not, because
8755 at this point we do not know whether e.g. any .eh_frame
8756 absolute relocations have been converted to PC-relative. */
8757 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
8758 }
8759
8760 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 8761 return FALSE;
b49e97c9 8762
861fb55a 8763 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 8764 if (htab->is_vxworks)
b49e97c9 8765 {
0a44bf69
RS
8766 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8767 use any of the DT_MIPS_* tags. */
861fb55a 8768 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8769 {
8770 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8771 return FALSE;
b49e97c9 8772
0a44bf69
RS
8773 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8774 return FALSE;
b49e97c9 8775
0a44bf69
RS
8776 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8777 return FALSE;
8778 }
b49e97c9 8779 }
0a44bf69
RS
8780 else
8781 {
861fb55a 8782 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8783 {
8784 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8785 return FALSE;
b49e97c9 8786
0a44bf69
RS
8787 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8788 return FALSE;
b49e97c9 8789
0a44bf69
RS
8790 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8791 return FALSE;
8792 }
b49e97c9 8793
0a44bf69
RS
8794 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8795 return FALSE;
b49e97c9 8796
0a44bf69
RS
8797 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8798 return FALSE;
b49e97c9 8799
0a44bf69
RS
8800 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8801 return FALSE;
b49e97c9 8802
0a44bf69
RS
8803 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8804 return FALSE;
b49e97c9 8805
0a44bf69
RS
8806 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8807 return FALSE;
b49e97c9 8808
0a44bf69
RS
8809 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8810 return FALSE;
b49e97c9 8811
0a44bf69
RS
8812 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8813 return FALSE;
8814
8815 if (IRIX_COMPAT (dynobj) == ict_irix5
8816 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8817 return FALSE;
8818
8819 if (IRIX_COMPAT (dynobj) == ict_irix6
8820 && (bfd_get_section_by_name
8821 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8822 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8823 return FALSE;
8824 }
861fb55a
DJ
8825 if (htab->splt->size > 0)
8826 {
8827 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8828 return FALSE;
8829
8830 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8831 return FALSE;
8832
8833 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8834 return FALSE;
8835
8836 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8837 return FALSE;
8838 }
7a2b07ff
NS
8839 if (htab->is_vxworks
8840 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8841 return FALSE;
b49e97c9
TS
8842 }
8843
b34976b6 8844 return TRUE;
b49e97c9
TS
8845}
8846\f
81d43bff
RS
8847/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8848 Adjust its R_ADDEND field so that it is correct for the output file.
8849 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8850 and sections respectively; both use symbol indexes. */
8851
8852static void
8853mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8854 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8855 asection **local_sections, Elf_Internal_Rela *rel)
8856{
8857 unsigned int r_type, r_symndx;
8858 Elf_Internal_Sym *sym;
8859 asection *sec;
8860
8861 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8862 {
8863 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8864 if (r_type == R_MIPS16_GPREL
8865 || r_type == R_MIPS_GPREL16
8866 || r_type == R_MIPS_GPREL32
8867 || r_type == R_MIPS_LITERAL)
8868 {
8869 rel->r_addend += _bfd_get_gp_value (input_bfd);
8870 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8871 }
8872
8873 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8874 sym = local_syms + r_symndx;
8875
8876 /* Adjust REL's addend to account for section merging. */
8877 if (!info->relocatable)
8878 {
8879 sec = local_sections[r_symndx];
8880 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8881 }
8882
8883 /* This would normally be done by the rela_normal code in elflink.c. */
8884 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8885 rel->r_addend += local_sections[r_symndx]->output_offset;
8886 }
8887}
8888
b49e97c9
TS
8889/* Relocate a MIPS ELF section. */
8890
b34976b6 8891bfd_boolean
9719ad41
RS
8892_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8893 bfd *input_bfd, asection *input_section,
8894 bfd_byte *contents, Elf_Internal_Rela *relocs,
8895 Elf_Internal_Sym *local_syms,
8896 asection **local_sections)
b49e97c9
TS
8897{
8898 Elf_Internal_Rela *rel;
8899 const Elf_Internal_Rela *relend;
8900 bfd_vma addend = 0;
b34976b6 8901 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 8902 const struct elf_backend_data *bed;
b49e97c9
TS
8903
8904 bed = get_elf_backend_data (output_bfd);
8905 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8906 for (rel = relocs; rel < relend; ++rel)
8907 {
8908 const char *name;
c9adbffe 8909 bfd_vma value = 0;
b49e97c9 8910 reloc_howto_type *howto;
38a7df63 8911 bfd_boolean cross_mode_jump_p;
b34976b6 8912 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 8913 REL relocation. */
b34976b6 8914 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 8915 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 8916 const char *msg;
ab96bf03
AM
8917 unsigned long r_symndx;
8918 asection *sec;
749b8d9d
L
8919 Elf_Internal_Shdr *symtab_hdr;
8920 struct elf_link_hash_entry *h;
b49e97c9
TS
8921
8922 /* Find the relocation howto for this relocation. */
ab96bf03
AM
8923 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8924 NEWABI_P (input_bfd)
8925 && (MIPS_RELOC_RELA_P
8926 (input_bfd, input_section,
8927 rel - relocs)));
8928
8929 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 8930 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
ab96bf03 8931 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
749b8d9d
L
8932 {
8933 sec = local_sections[r_symndx];
8934 h = NULL;
8935 }
ab96bf03
AM
8936 else
8937 {
ab96bf03 8938 unsigned long extsymoff;
ab96bf03 8939
ab96bf03
AM
8940 extsymoff = 0;
8941 if (!elf_bad_symtab (input_bfd))
8942 extsymoff = symtab_hdr->sh_info;
8943 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8944 while (h->root.type == bfd_link_hash_indirect
8945 || h->root.type == bfd_link_hash_warning)
8946 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8947
8948 sec = NULL;
8949 if (h->root.type == bfd_link_hash_defined
8950 || h->root.type == bfd_link_hash_defweak)
8951 sec = h->root.u.def.section;
8952 }
8953
8954 if (sec != NULL && elf_discarded_section (sec))
8955 {
8956 /* For relocs against symbols from removed linkonce sections,
8957 or sections discarded by a linker script, we just want the
8958 section contents zeroed. Avoid any special processing. */
8959 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8960 rel->r_info = 0;
8961 rel->r_addend = 0;
8962 continue;
8963 }
8964
4a14403c 8965 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
8966 {
8967 /* Some 32-bit code uses R_MIPS_64. In particular, people use
8968 64-bit code, but make sure all their addresses are in the
8969 lowermost or uppermost 32-bit section of the 64-bit address
8970 space. Thus, when they use an R_MIPS_64 they mean what is
8971 usually meant by R_MIPS_32, with the exception that the
8972 stored value is sign-extended to 64 bits. */
b34976b6 8973 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
8974
8975 /* On big-endian systems, we need to lie about the position
8976 of the reloc. */
8977 if (bfd_big_endian (input_bfd))
8978 rel->r_offset += 4;
8979 }
b49e97c9
TS
8980
8981 if (!use_saved_addend_p)
8982 {
b49e97c9
TS
8983 /* If these relocations were originally of the REL variety,
8984 we must pull the addend out of the field that will be
8985 relocated. Otherwise, we simply use the contents of the
c224138d
RS
8986 RELA relocation. */
8987 if (mips_elf_rel_relocation_p (input_bfd, input_section,
8988 relocs, rel))
b49e97c9 8989 {
b34976b6 8990 rela_relocation_p = FALSE;
c224138d
RS
8991 addend = mips_elf_read_rel_addend (input_bfd, rel,
8992 howto, contents);
738e5348
RS
8993 if (hi16_reloc_p (r_type)
8994 || (got16_reloc_p (r_type)
b49e97c9 8995 && mips_elf_local_relocation_p (input_bfd, rel,
b34976b6 8996 local_sections, FALSE)))
b49e97c9 8997 {
c224138d
RS
8998 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8999 contents, &addend))
749b8d9d 9000 {
749b8d9d
L
9001 if (h)
9002 name = h->root.root.string;
9003 else
9004 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9005 local_syms + r_symndx,
9006 sec);
9007 (*_bfd_error_handler)
9008 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9009 input_bfd, input_section, name, howto->name,
9010 rel->r_offset);
749b8d9d 9011 }
b49e97c9 9012 }
30ac9238
RS
9013 else
9014 addend <<= howto->rightshift;
b49e97c9
TS
9015 }
9016 else
9017 addend = rel->r_addend;
81d43bff
RS
9018 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9019 local_syms, local_sections, rel);
b49e97c9
TS
9020 }
9021
1049f94e 9022 if (info->relocatable)
b49e97c9 9023 {
4a14403c 9024 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9025 && bfd_big_endian (input_bfd))
9026 rel->r_offset -= 4;
9027
81d43bff 9028 if (!rela_relocation_p && rel->r_addend)
5a659663 9029 {
81d43bff 9030 addend += rel->r_addend;
738e5348 9031 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9032 addend = mips_elf_high (addend);
9033 else if (r_type == R_MIPS_HIGHER)
9034 addend = mips_elf_higher (addend);
9035 else if (r_type == R_MIPS_HIGHEST)
9036 addend = mips_elf_highest (addend);
30ac9238
RS
9037 else
9038 addend >>= howto->rightshift;
b49e97c9 9039
30ac9238
RS
9040 /* We use the source mask, rather than the destination
9041 mask because the place to which we are writing will be
9042 source of the addend in the final link. */
b49e97c9
TS
9043 addend &= howto->src_mask;
9044
5a659663 9045 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9046 /* See the comment above about using R_MIPS_64 in the 32-bit
9047 ABI. Here, we need to update the addend. It would be
9048 possible to get away with just using the R_MIPS_32 reloc
9049 but for endianness. */
9050 {
9051 bfd_vma sign_bits;
9052 bfd_vma low_bits;
9053 bfd_vma high_bits;
9054
9055 if (addend & ((bfd_vma) 1 << 31))
9056#ifdef BFD64
9057 sign_bits = ((bfd_vma) 1 << 32) - 1;
9058#else
9059 sign_bits = -1;
9060#endif
9061 else
9062 sign_bits = 0;
9063
9064 /* If we don't know that we have a 64-bit type,
9065 do two separate stores. */
9066 if (bfd_big_endian (input_bfd))
9067 {
9068 /* Store the sign-bits (which are most significant)
9069 first. */
9070 low_bits = sign_bits;
9071 high_bits = addend;
9072 }
9073 else
9074 {
9075 low_bits = addend;
9076 high_bits = sign_bits;
9077 }
9078 bfd_put_32 (input_bfd, low_bits,
9079 contents + rel->r_offset);
9080 bfd_put_32 (input_bfd, high_bits,
9081 contents + rel->r_offset + 4);
9082 continue;
9083 }
9084
9085 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9086 input_bfd, input_section,
b34976b6
AM
9087 contents, FALSE))
9088 return FALSE;
b49e97c9
TS
9089 }
9090
9091 /* Go on to the next relocation. */
9092 continue;
9093 }
9094
9095 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9096 relocations for the same offset. In that case we are
9097 supposed to treat the output of each relocation as the addend
9098 for the next. */
9099 if (rel + 1 < relend
9100 && rel->r_offset == rel[1].r_offset
9101 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9102 use_saved_addend_p = TRUE;
b49e97c9 9103 else
b34976b6 9104 use_saved_addend_p = FALSE;
b49e97c9
TS
9105
9106 /* Figure out what value we are supposed to relocate. */
9107 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9108 input_section, info, rel,
9109 addend, howto, local_syms,
9110 local_sections, &value,
38a7df63 9111 &name, &cross_mode_jump_p,
bce03d3d 9112 use_saved_addend_p))
b49e97c9
TS
9113 {
9114 case bfd_reloc_continue:
9115 /* There's nothing to do. */
9116 continue;
9117
9118 case bfd_reloc_undefined:
9119 /* mips_elf_calculate_relocation already called the
9120 undefined_symbol callback. There's no real point in
9121 trying to perform the relocation at this point, so we
9122 just skip ahead to the next relocation. */
9123 continue;
9124
9125 case bfd_reloc_notsupported:
9126 msg = _("internal error: unsupported relocation error");
9127 info->callbacks->warning
9128 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9129 return FALSE;
b49e97c9
TS
9130
9131 case bfd_reloc_overflow:
9132 if (use_saved_addend_p)
9133 /* Ignore overflow until we reach the last relocation for
9134 a given location. */
9135 ;
9136 else
9137 {
0e53d9da
AN
9138 struct mips_elf_link_hash_table *htab;
9139
9140 htab = mips_elf_hash_table (info);
b49e97c9 9141 BFD_ASSERT (name != NULL);
0e53d9da
AN
9142 if (!htab->small_data_overflow_reported
9143 && (howto->type == R_MIPS_GPREL16
9144 || howto->type == R_MIPS_LITERAL))
9145 {
91d6fa6a
NC
9146 msg = _("small-data section exceeds 64KB;"
9147 " lower small-data size limit (see option -G)");
0e53d9da
AN
9148
9149 htab->small_data_overflow_reported = TRUE;
9150 (*info->callbacks->einfo) ("%P: %s\n", msg);
9151 }
b49e97c9 9152 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 9153 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 9154 input_bfd, input_section, rel->r_offset)))
b34976b6 9155 return FALSE;
b49e97c9
TS
9156 }
9157 break;
9158
9159 case bfd_reloc_ok:
9160 break;
9161
9162 default:
9163 abort ();
9164 break;
9165 }
9166
9167 /* If we've got another relocation for the address, keep going
9168 until we reach the last one. */
9169 if (use_saved_addend_p)
9170 {
9171 addend = value;
9172 continue;
9173 }
9174
4a14403c 9175 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9176 /* See the comment above about using R_MIPS_64 in the 32-bit
9177 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9178 that calculated the right value. Now, however, we
9179 sign-extend the 32-bit result to 64-bits, and store it as a
9180 64-bit value. We are especially generous here in that we
9181 go to extreme lengths to support this usage on systems with
9182 only a 32-bit VMA. */
9183 {
9184 bfd_vma sign_bits;
9185 bfd_vma low_bits;
9186 bfd_vma high_bits;
9187
9188 if (value & ((bfd_vma) 1 << 31))
9189#ifdef BFD64
9190 sign_bits = ((bfd_vma) 1 << 32) - 1;
9191#else
9192 sign_bits = -1;
9193#endif
9194 else
9195 sign_bits = 0;
9196
9197 /* If we don't know that we have a 64-bit type,
9198 do two separate stores. */
9199 if (bfd_big_endian (input_bfd))
9200 {
9201 /* Undo what we did above. */
9202 rel->r_offset -= 4;
9203 /* Store the sign-bits (which are most significant)
9204 first. */
9205 low_bits = sign_bits;
9206 high_bits = value;
9207 }
9208 else
9209 {
9210 low_bits = value;
9211 high_bits = sign_bits;
9212 }
9213 bfd_put_32 (input_bfd, low_bits,
9214 contents + rel->r_offset);
9215 bfd_put_32 (input_bfd, high_bits,
9216 contents + rel->r_offset + 4);
9217 continue;
9218 }
9219
9220 /* Actually perform the relocation. */
9221 if (! mips_elf_perform_relocation (info, howto, rel, value,
9222 input_bfd, input_section,
38a7df63 9223 contents, cross_mode_jump_p))
b34976b6 9224 return FALSE;
b49e97c9
TS
9225 }
9226
b34976b6 9227 return TRUE;
b49e97c9
TS
9228}
9229\f
861fb55a
DJ
9230/* A function that iterates over each entry in la25_stubs and fills
9231 in the code for each one. DATA points to a mips_htab_traverse_info. */
9232
9233static int
9234mips_elf_create_la25_stub (void **slot, void *data)
9235{
9236 struct mips_htab_traverse_info *hti;
9237 struct mips_elf_link_hash_table *htab;
9238 struct mips_elf_la25_stub *stub;
9239 asection *s;
9240 bfd_byte *loc;
9241 bfd_vma offset, target, target_high, target_low;
9242
9243 stub = (struct mips_elf_la25_stub *) *slot;
9244 hti = (struct mips_htab_traverse_info *) data;
9245 htab = mips_elf_hash_table (hti->info);
9246
9247 /* Create the section contents, if we haven't already. */
9248 s = stub->stub_section;
9249 loc = s->contents;
9250 if (loc == NULL)
9251 {
9252 loc = bfd_malloc (s->size);
9253 if (loc == NULL)
9254 {
9255 hti->error = TRUE;
9256 return FALSE;
9257 }
9258 s->contents = loc;
9259 }
9260
9261 /* Work out where in the section this stub should go. */
9262 offset = stub->offset;
9263
9264 /* Work out the target address. */
9265 target = (stub->h->root.root.u.def.section->output_section->vma
9266 + stub->h->root.root.u.def.section->output_offset
9267 + stub->h->root.root.u.def.value);
9268 target_high = ((target + 0x8000) >> 16) & 0xffff;
9269 target_low = (target & 0xffff);
9270
9271 if (stub->stub_section != htab->strampoline)
9272 {
9273 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9274 of the section and write the two instructions at the end. */
9275 memset (loc, 0, offset);
9276 loc += offset;
9277 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9278 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9279 }
9280 else
9281 {
9282 /* This is trampoline. */
9283 loc += offset;
9284 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9285 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9286 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9287 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9288 }
9289 return TRUE;
9290}
9291
b49e97c9
TS
9292/* If NAME is one of the special IRIX6 symbols defined by the linker,
9293 adjust it appropriately now. */
9294
9295static void
9719ad41
RS
9296mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9297 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
9298{
9299 /* The linker script takes care of providing names and values for
9300 these, but we must place them into the right sections. */
9301 static const char* const text_section_symbols[] = {
9302 "_ftext",
9303 "_etext",
9304 "__dso_displacement",
9305 "__elf_header",
9306 "__program_header_table",
9307 NULL
9308 };
9309
9310 static const char* const data_section_symbols[] = {
9311 "_fdata",
9312 "_edata",
9313 "_end",
9314 "_fbss",
9315 NULL
9316 };
9317
9318 const char* const *p;
9319 int i;
9320
9321 for (i = 0; i < 2; ++i)
9322 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9323 *p;
9324 ++p)
9325 if (strcmp (*p, name) == 0)
9326 {
9327 /* All of these symbols are given type STT_SECTION by the
9328 IRIX6 linker. */
9329 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 9330 sym->st_other = STO_PROTECTED;
b49e97c9
TS
9331
9332 /* The IRIX linker puts these symbols in special sections. */
9333 if (i == 0)
9334 sym->st_shndx = SHN_MIPS_TEXT;
9335 else
9336 sym->st_shndx = SHN_MIPS_DATA;
9337
9338 break;
9339 }
9340}
9341
9342/* Finish up dynamic symbol handling. We set the contents of various
9343 dynamic sections here. */
9344
b34976b6 9345bfd_boolean
9719ad41
RS
9346_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9347 struct bfd_link_info *info,
9348 struct elf_link_hash_entry *h,
9349 Elf_Internal_Sym *sym)
b49e97c9
TS
9350{
9351 bfd *dynobj;
b49e97c9 9352 asection *sgot;
f4416af6 9353 struct mips_got_info *g, *gg;
b49e97c9 9354 const char *name;
3d6746ca 9355 int idx;
5108fc1b 9356 struct mips_elf_link_hash_table *htab;
738e5348 9357 struct mips_elf_link_hash_entry *hmips;
b49e97c9 9358
5108fc1b 9359 htab = mips_elf_hash_table (info);
b49e97c9 9360 dynobj = elf_hash_table (info)->dynobj;
738e5348 9361 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9362
861fb55a
DJ
9363 BFD_ASSERT (!htab->is_vxworks);
9364
9365 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9366 {
9367 /* We've decided to create a PLT entry for this symbol. */
9368 bfd_byte *loc;
9369 bfd_vma header_address, plt_index, got_address;
9370 bfd_vma got_address_high, got_address_low, load;
9371 const bfd_vma *plt_entry;
9372
9373 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9374 BFD_ASSERT (h->dynindx != -1);
9375 BFD_ASSERT (htab->splt != NULL);
9376 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9377 BFD_ASSERT (!h->def_regular);
9378
9379 /* Calculate the address of the PLT header. */
9380 header_address = (htab->splt->output_section->vma
9381 + htab->splt->output_offset);
9382
9383 /* Calculate the index of the entry. */
9384 plt_index = ((h->plt.offset - htab->plt_header_size)
9385 / htab->plt_entry_size);
9386
9387 /* Calculate the address of the .got.plt entry. */
9388 got_address = (htab->sgotplt->output_section->vma
9389 + htab->sgotplt->output_offset
9390 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9391 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9392 got_address_low = got_address & 0xffff;
9393
9394 /* Initially point the .got.plt entry at the PLT header. */
9395 loc = (htab->sgotplt->contents
9396 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9397 if (ABI_64_P (output_bfd))
9398 bfd_put_64 (output_bfd, header_address, loc);
9399 else
9400 bfd_put_32 (output_bfd, header_address, loc);
9401
9402 /* Find out where the .plt entry should go. */
9403 loc = htab->splt->contents + h->plt.offset;
9404
9405 /* Pick the load opcode. */
9406 load = MIPS_ELF_LOAD_WORD (output_bfd);
9407
9408 /* Fill in the PLT entry itself. */
9409 plt_entry = mips_exec_plt_entry;
9410 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9411 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
6d30f5b2
NC
9412
9413 if (! LOAD_INTERLOCKS_P (output_bfd))
9414 {
9415 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9416 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9417 }
9418 else
9419 {
9420 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9421 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9422 }
861fb55a
DJ
9423
9424 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9425 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9426 plt_index, h->dynindx,
9427 R_MIPS_JUMP_SLOT, got_address);
9428
9429 /* We distinguish between PLT entries and lazy-binding stubs by
9430 giving the former an st_other value of STO_MIPS_PLT. Set the
9431 flag and leave the value if there are any relocations in the
9432 binary where pointer equality matters. */
9433 sym->st_shndx = SHN_UNDEF;
9434 if (h->pointer_equality_needed)
9435 sym->st_other = STO_MIPS_PLT;
9436 else
9437 sym->st_value = 0;
9438 }
9439 else if (h->plt.offset != MINUS_ONE)
b49e97c9 9440 {
861fb55a 9441 /* We've decided to create a lazy-binding stub. */
5108fc1b 9442 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
b49e97c9
TS
9443
9444 /* This symbol has a stub. Set it up. */
9445
9446 BFD_ASSERT (h->dynindx != -1);
9447
5108fc1b
RS
9448 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9449 || (h->dynindx <= 0xffff));
3d6746ca
DD
9450
9451 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
9452 sign extension at runtime in the stub, resulting in a negative
9453 index value. */
9454 if (h->dynindx & ~0x7fffffff)
b34976b6 9455 return FALSE;
b49e97c9
TS
9456
9457 /* Fill the stub. */
3d6746ca
DD
9458 idx = 0;
9459 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9460 idx += 4;
9461 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9462 idx += 4;
5108fc1b 9463 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
3d6746ca 9464 {
5108fc1b 9465 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
3d6746ca
DD
9466 stub + idx);
9467 idx += 4;
9468 }
9469 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9470 idx += 4;
b49e97c9 9471
3d6746ca
DD
9472 /* If a large stub is not required and sign extension is not a
9473 problem, then use legacy code in the stub. */
5108fc1b
RS
9474 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9475 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9476 else if (h->dynindx & ~0x7fff)
3d6746ca
DD
9477 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9478 else
5108fc1b
RS
9479 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9480 stub + idx);
9481
4e41d0d7
RS
9482 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9483 memcpy (htab->sstubs->contents + h->plt.offset,
9484 stub, htab->function_stub_size);
b49e97c9
TS
9485
9486 /* Mark the symbol as undefined. plt.offset != -1 occurs
9487 only for the referenced symbol. */
9488 sym->st_shndx = SHN_UNDEF;
9489
9490 /* The run-time linker uses the st_value field of the symbol
9491 to reset the global offset table entry for this external
9492 to its stub address when unlinking a shared object. */
4e41d0d7
RS
9493 sym->st_value = (htab->sstubs->output_section->vma
9494 + htab->sstubs->output_offset
c5ae1840 9495 + h->plt.offset);
b49e97c9
TS
9496 }
9497
738e5348
RS
9498 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9499 refer to the stub, since only the stub uses the standard calling
9500 conventions. */
9501 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9502 {
9503 BFD_ASSERT (hmips->need_fn_stub);
9504 sym->st_value = (hmips->fn_stub->output_section->vma
9505 + hmips->fn_stub->output_offset);
9506 sym->st_size = hmips->fn_stub->size;
9507 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9508 }
9509
b49e97c9 9510 BFD_ASSERT (h->dynindx != -1
f5385ebf 9511 || h->forced_local);
b49e97c9 9512
23cc69b6 9513 sgot = htab->sgot;
a8028dd0 9514 g = htab->got_info;
b49e97c9
TS
9515 BFD_ASSERT (g != NULL);
9516
9517 /* Run through the global symbol table, creating GOT entries for all
9518 the symbols that need them. */
9519 if (g->global_gotsym != NULL
9520 && h->dynindx >= g->global_gotsym->dynindx)
9521 {
9522 bfd_vma offset;
9523 bfd_vma value;
9524
6eaa6adc 9525 value = sym->st_value;
738e5348
RS
9526 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9527 R_MIPS_GOT16, info);
b49e97c9
TS
9528 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9529 }
9530
0f20cc35 9531 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
f4416af6
AO
9532 {
9533 struct mips_got_entry e, *p;
0626d451 9534 bfd_vma entry;
f4416af6 9535 bfd_vma offset;
f4416af6
AO
9536
9537 gg = g;
9538
9539 e.abfd = output_bfd;
9540 e.symndx = -1;
738e5348 9541 e.d.h = hmips;
0f20cc35 9542 e.tls_type = 0;
143d77c5 9543
f4416af6
AO
9544 for (g = g->next; g->next != gg; g = g->next)
9545 {
9546 if (g->got_entries
9547 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9548 &e)))
9549 {
9550 offset = p->gotidx;
0626d451
RS
9551 if (info->shared
9552 || (elf_hash_table (info)->dynamic_sections_created
9553 && p->d.h != NULL
f5385ebf
AM
9554 && p->d.h->root.def_dynamic
9555 && !p->d.h->root.def_regular))
0626d451
RS
9556 {
9557 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9558 the various compatibility problems, it's easier to mock
9559 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9560 mips_elf_create_dynamic_relocation to calculate the
9561 appropriate addend. */
9562 Elf_Internal_Rela rel[3];
9563
9564 memset (rel, 0, sizeof (rel));
9565 if (ABI_64_P (output_bfd))
9566 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9567 else
9568 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9569 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9570
9571 entry = 0;
9572 if (! (mips_elf_create_dynamic_relocation
9573 (output_bfd, info, rel,
9574 e.d.h, NULL, sym->st_value, &entry, sgot)))
9575 return FALSE;
9576 }
9577 else
9578 entry = sym->st_value;
9579 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
9580 }
9581 }
9582 }
9583
b49e97c9
TS
9584 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9585 name = h->root.root.string;
9586 if (strcmp (name, "_DYNAMIC") == 0
22edb2f1 9587 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
9588 sym->st_shndx = SHN_ABS;
9589 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9590 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9591 {
9592 sym->st_shndx = SHN_ABS;
9593 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9594 sym->st_value = 1;
9595 }
4a14403c 9596 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9597 {
9598 sym->st_shndx = SHN_ABS;
9599 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9600 sym->st_value = elf_gp (output_bfd);
9601 }
9602 else if (SGI_COMPAT (output_bfd))
9603 {
9604 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9605 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9606 {
9607 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9608 sym->st_other = STO_PROTECTED;
9609 sym->st_value = 0;
9610 sym->st_shndx = SHN_MIPS_DATA;
9611 }
9612 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9613 {
9614 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9615 sym->st_other = STO_PROTECTED;
9616 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9617 sym->st_shndx = SHN_ABS;
9618 }
9619 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9620 {
9621 if (h->type == STT_FUNC)
9622 sym->st_shndx = SHN_MIPS_TEXT;
9623 else if (h->type == STT_OBJECT)
9624 sym->st_shndx = SHN_MIPS_DATA;
9625 }
9626 }
9627
861fb55a
DJ
9628 /* Emit a copy reloc, if needed. */
9629 if (h->needs_copy)
9630 {
9631 asection *s;
9632 bfd_vma symval;
9633
9634 BFD_ASSERT (h->dynindx != -1);
9635 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9636
9637 s = mips_elf_rel_dyn_section (info, FALSE);
9638 symval = (h->root.u.def.section->output_section->vma
9639 + h->root.u.def.section->output_offset
9640 + h->root.u.def.value);
9641 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9642 h->dynindx, R_MIPS_COPY, symval);
9643 }
9644
b49e97c9
TS
9645 /* Handle the IRIX6-specific symbols. */
9646 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9647 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9648
9649 if (! info->shared)
9650 {
9651 if (! mips_elf_hash_table (info)->use_rld_obj_head
9652 && (strcmp (name, "__rld_map") == 0
9653 || strcmp (name, "__RLD_MAP") == 0))
9654 {
9655 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9656 BFD_ASSERT (s != NULL);
9657 sym->st_value = s->output_section->vma + s->output_offset;
9719ad41 9658 bfd_put_32 (output_bfd, 0, s->contents);
b49e97c9
TS
9659 if (mips_elf_hash_table (info)->rld_value == 0)
9660 mips_elf_hash_table (info)->rld_value = sym->st_value;
9661 }
9662 else if (mips_elf_hash_table (info)->use_rld_obj_head
9663 && strcmp (name, "__rld_obj_head") == 0)
9664 {
9665 /* IRIX6 does not use a .rld_map section. */
9666 if (IRIX_COMPAT (output_bfd) == ict_irix5
9667 || IRIX_COMPAT (output_bfd) == ict_none)
9668 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9669 != NULL);
9670 mips_elf_hash_table (info)->rld_value = sym->st_value;
9671 }
9672 }
9673
738e5348
RS
9674 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9675 treat MIPS16 symbols like any other. */
30c09090 9676 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
9677 {
9678 BFD_ASSERT (sym->st_value & 1);
9679 sym->st_other -= STO_MIPS16;
9680 }
b49e97c9 9681
b34976b6 9682 return TRUE;
b49e97c9
TS
9683}
9684
0a44bf69
RS
9685/* Likewise, for VxWorks. */
9686
9687bfd_boolean
9688_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9689 struct bfd_link_info *info,
9690 struct elf_link_hash_entry *h,
9691 Elf_Internal_Sym *sym)
9692{
9693 bfd *dynobj;
9694 asection *sgot;
9695 struct mips_got_info *g;
9696 struct mips_elf_link_hash_table *htab;
9697
9698 htab = mips_elf_hash_table (info);
9699 dynobj = elf_hash_table (info)->dynobj;
9700
9701 if (h->plt.offset != (bfd_vma) -1)
9702 {
6d79d2ed 9703 bfd_byte *loc;
0a44bf69
RS
9704 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9705 Elf_Internal_Rela rel;
9706 static const bfd_vma *plt_entry;
9707
9708 BFD_ASSERT (h->dynindx != -1);
9709 BFD_ASSERT (htab->splt != NULL);
9710 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9711
9712 /* Calculate the address of the .plt entry. */
9713 plt_address = (htab->splt->output_section->vma
9714 + htab->splt->output_offset
9715 + h->plt.offset);
9716
9717 /* Calculate the index of the entry. */
9718 plt_index = ((h->plt.offset - htab->plt_header_size)
9719 / htab->plt_entry_size);
9720
9721 /* Calculate the address of the .got.plt entry. */
9722 got_address = (htab->sgotplt->output_section->vma
9723 + htab->sgotplt->output_offset
9724 + plt_index * 4);
9725
9726 /* Calculate the offset of the .got.plt entry from
9727 _GLOBAL_OFFSET_TABLE_. */
9728 got_offset = mips_elf_gotplt_index (info, h);
9729
9730 /* Calculate the offset for the branch at the start of the PLT
9731 entry. The branch jumps to the beginning of .plt. */
9732 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9733
9734 /* Fill in the initial value of the .got.plt entry. */
9735 bfd_put_32 (output_bfd, plt_address,
9736 htab->sgotplt->contents + plt_index * 4);
9737
9738 /* Find out where the .plt entry should go. */
9739 loc = htab->splt->contents + h->plt.offset;
9740
9741 if (info->shared)
9742 {
9743 plt_entry = mips_vxworks_shared_plt_entry;
9744 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9745 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9746 }
9747 else
9748 {
9749 bfd_vma got_address_high, got_address_low;
9750
9751 plt_entry = mips_vxworks_exec_plt_entry;
9752 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9753 got_address_low = got_address & 0xffff;
9754
9755 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9756 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9757 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9758 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9759 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9760 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9761 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9762 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9763
9764 loc = (htab->srelplt2->contents
9765 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9766
9767 /* Emit a relocation for the .got.plt entry. */
9768 rel.r_offset = got_address;
9769 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9770 rel.r_addend = h->plt.offset;
9771 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9772
9773 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9774 loc += sizeof (Elf32_External_Rela);
9775 rel.r_offset = plt_address + 8;
9776 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9777 rel.r_addend = got_offset;
9778 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9779
9780 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9781 loc += sizeof (Elf32_External_Rela);
9782 rel.r_offset += 4;
9783 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9784 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9785 }
9786
9787 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9788 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9789 rel.r_offset = got_address;
9790 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9791 rel.r_addend = 0;
9792 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9793
9794 if (!h->def_regular)
9795 sym->st_shndx = SHN_UNDEF;
9796 }
9797
9798 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9799
23cc69b6 9800 sgot = htab->sgot;
a8028dd0 9801 g = htab->got_info;
0a44bf69
RS
9802 BFD_ASSERT (g != NULL);
9803
9804 /* See if this symbol has an entry in the GOT. */
9805 if (g->global_gotsym != NULL
9806 && h->dynindx >= g->global_gotsym->dynindx)
9807 {
9808 bfd_vma offset;
9809 Elf_Internal_Rela outrel;
9810 bfd_byte *loc;
9811 asection *s;
9812
9813 /* Install the symbol value in the GOT. */
9814 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9815 R_MIPS_GOT16, info);
9816 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9817
9818 /* Add a dynamic relocation for it. */
9819 s = mips_elf_rel_dyn_section (info, FALSE);
9820 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9821 outrel.r_offset = (sgot->output_section->vma
9822 + sgot->output_offset
9823 + offset);
9824 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9825 outrel.r_addend = 0;
9826 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9827 }
9828
9829 /* Emit a copy reloc, if needed. */
9830 if (h->needs_copy)
9831 {
9832 Elf_Internal_Rela rel;
9833
9834 BFD_ASSERT (h->dynindx != -1);
9835
9836 rel.r_offset = (h->root.u.def.section->output_section->vma
9837 + h->root.u.def.section->output_offset
9838 + h->root.u.def.value);
9839 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9840 rel.r_addend = 0;
9841 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9842 htab->srelbss->contents
9843 + (htab->srelbss->reloc_count
9844 * sizeof (Elf32_External_Rela)));
9845 ++htab->srelbss->reloc_count;
9846 }
9847
9848 /* If this is a mips16 symbol, force the value to be even. */
30c09090 9849 if (ELF_ST_IS_MIPS16 (sym->st_other))
0a44bf69
RS
9850 sym->st_value &= ~1;
9851
9852 return TRUE;
9853}
9854
861fb55a
DJ
9855/* Write out a plt0 entry to the beginning of .plt. */
9856
9857static void
9858mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9859{
9860 bfd_byte *loc;
9861 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9862 static const bfd_vma *plt_entry;
9863 struct mips_elf_link_hash_table *htab;
9864
9865 htab = mips_elf_hash_table (info);
9866 if (ABI_64_P (output_bfd))
9867 plt_entry = mips_n64_exec_plt0_entry;
9868 else if (ABI_N32_P (output_bfd))
9869 plt_entry = mips_n32_exec_plt0_entry;
9870 else
9871 plt_entry = mips_o32_exec_plt0_entry;
9872
9873 /* Calculate the value of .got.plt. */
9874 gotplt_value = (htab->sgotplt->output_section->vma
9875 + htab->sgotplt->output_offset);
9876 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9877 gotplt_value_low = gotplt_value & 0xffff;
9878
9879 /* The PLT sequence is not safe for N64 if .got.plt's address can
9880 not be loaded in two instructions. */
9881 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9882 || ~(gotplt_value | 0x7fffffff) == 0);
9883
9884 /* Install the PLT header. */
9885 loc = htab->splt->contents;
9886 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9887 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9888 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9889 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9890 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9891 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9892 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9893 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9894}
9895
0a44bf69
RS
9896/* Install the PLT header for a VxWorks executable and finalize the
9897 contents of .rela.plt.unloaded. */
9898
9899static void
9900mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9901{
9902 Elf_Internal_Rela rela;
9903 bfd_byte *loc;
9904 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9905 static const bfd_vma *plt_entry;
9906 struct mips_elf_link_hash_table *htab;
9907
9908 htab = mips_elf_hash_table (info);
9909 plt_entry = mips_vxworks_exec_plt0_entry;
9910
9911 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9912 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9913 + htab->root.hgot->root.u.def.section->output_offset
9914 + htab->root.hgot->root.u.def.value);
9915
9916 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9917 got_value_low = got_value & 0xffff;
9918
9919 /* Calculate the address of the PLT header. */
9920 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9921
9922 /* Install the PLT header. */
9923 loc = htab->splt->contents;
9924 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9925 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9926 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9927 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9928 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9929 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9930
9931 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9932 loc = htab->srelplt2->contents;
9933 rela.r_offset = plt_address;
9934 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9935 rela.r_addend = 0;
9936 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9937 loc += sizeof (Elf32_External_Rela);
9938
9939 /* Output the relocation for the following addiu of
9940 %lo(_GLOBAL_OFFSET_TABLE_). */
9941 rela.r_offset += 4;
9942 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9943 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9944 loc += sizeof (Elf32_External_Rela);
9945
9946 /* Fix up the remaining relocations. They may have the wrong
9947 symbol index for _G_O_T_ or _P_L_T_ depending on the order
9948 in which symbols were output. */
9949 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9950 {
9951 Elf_Internal_Rela rel;
9952
9953 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9954 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9955 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9956 loc += sizeof (Elf32_External_Rela);
9957
9958 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9959 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9960 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9961 loc += sizeof (Elf32_External_Rela);
9962
9963 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9964 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9965 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9966 loc += sizeof (Elf32_External_Rela);
9967 }
9968}
9969
9970/* Install the PLT header for a VxWorks shared library. */
9971
9972static void
9973mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9974{
9975 unsigned int i;
9976 struct mips_elf_link_hash_table *htab;
9977
9978 htab = mips_elf_hash_table (info);
9979
9980 /* We just need to copy the entry byte-by-byte. */
9981 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9982 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9983 htab->splt->contents + i * 4);
9984}
9985
b49e97c9
TS
9986/* Finish up the dynamic sections. */
9987
b34976b6 9988bfd_boolean
9719ad41
RS
9989_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9990 struct bfd_link_info *info)
b49e97c9
TS
9991{
9992 bfd *dynobj;
9993 asection *sdyn;
9994 asection *sgot;
f4416af6 9995 struct mips_got_info *gg, *g;
0a44bf69 9996 struct mips_elf_link_hash_table *htab;
b49e97c9 9997
0a44bf69 9998 htab = mips_elf_hash_table (info);
b49e97c9
TS
9999 dynobj = elf_hash_table (info)->dynobj;
10000
10001 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10002
23cc69b6
RS
10003 sgot = htab->sgot;
10004 gg = htab->got_info;
b49e97c9
TS
10005
10006 if (elf_hash_table (info)->dynamic_sections_created)
10007 {
10008 bfd_byte *b;
943284cc 10009 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
10010
10011 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
10012 BFD_ASSERT (gg != NULL);
10013
10014 g = mips_elf_got_for_ibfd (gg, output_bfd);
b49e97c9
TS
10015 BFD_ASSERT (g != NULL);
10016
10017 for (b = sdyn->contents;
eea6121a 10018 b < sdyn->contents + sdyn->size;
b49e97c9
TS
10019 b += MIPS_ELF_DYN_SIZE (dynobj))
10020 {
10021 Elf_Internal_Dyn dyn;
10022 const char *name;
10023 size_t elemsize;
10024 asection *s;
b34976b6 10025 bfd_boolean swap_out_p;
b49e97c9
TS
10026
10027 /* Read in the current dynamic entry. */
10028 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10029
10030 /* Assume that we're going to modify it and write it out. */
b34976b6 10031 swap_out_p = TRUE;
b49e97c9
TS
10032
10033 switch (dyn.d_tag)
10034 {
10035 case DT_RELENT:
b49e97c9
TS
10036 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10037 break;
10038
0a44bf69
RS
10039 case DT_RELAENT:
10040 BFD_ASSERT (htab->is_vxworks);
10041 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10042 break;
10043
b49e97c9
TS
10044 case DT_STRSZ:
10045 /* Rewrite DT_STRSZ. */
10046 dyn.d_un.d_val =
10047 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10048 break;
10049
10050 case DT_PLTGOT:
861fb55a
DJ
10051 s = htab->sgot;
10052 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10053 break;
10054
10055 case DT_MIPS_PLTGOT:
10056 s = htab->sgotplt;
10057 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
10058 break;
10059
10060 case DT_MIPS_RLD_VERSION:
10061 dyn.d_un.d_val = 1; /* XXX */
10062 break;
10063
10064 case DT_MIPS_FLAGS:
10065 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10066 break;
10067
b49e97c9 10068 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
10069 {
10070 time_t t;
10071 time (&t);
10072 dyn.d_un.d_val = t;
10073 }
b49e97c9
TS
10074 break;
10075
10076 case DT_MIPS_ICHECKSUM:
10077 /* XXX FIXME: */
b34976b6 10078 swap_out_p = FALSE;
b49e97c9
TS
10079 break;
10080
10081 case DT_MIPS_IVERSION:
10082 /* XXX FIXME: */
b34976b6 10083 swap_out_p = FALSE;
b49e97c9
TS
10084 break;
10085
10086 case DT_MIPS_BASE_ADDRESS:
10087 s = output_bfd->sections;
10088 BFD_ASSERT (s != NULL);
10089 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10090 break;
10091
10092 case DT_MIPS_LOCAL_GOTNO:
10093 dyn.d_un.d_val = g->local_gotno;
10094 break;
10095
10096 case DT_MIPS_UNREFEXTNO:
10097 /* The index into the dynamic symbol table which is the
10098 entry of the first external symbol that is not
10099 referenced within the same object. */
10100 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10101 break;
10102
10103 case DT_MIPS_GOTSYM:
f4416af6 10104 if (gg->global_gotsym)
b49e97c9 10105 {
f4416af6 10106 dyn.d_un.d_val = gg->global_gotsym->dynindx;
b49e97c9
TS
10107 break;
10108 }
10109 /* In case if we don't have global got symbols we default
10110 to setting DT_MIPS_GOTSYM to the same value as
10111 DT_MIPS_SYMTABNO, so we just fall through. */
10112
10113 case DT_MIPS_SYMTABNO:
10114 name = ".dynsym";
10115 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10116 s = bfd_get_section_by_name (output_bfd, name);
10117 BFD_ASSERT (s != NULL);
10118
eea6121a 10119 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
10120 break;
10121
10122 case DT_MIPS_HIPAGENO:
861fb55a 10123 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
10124 break;
10125
10126 case DT_MIPS_RLD_MAP:
10127 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10128 break;
10129
10130 case DT_MIPS_OPTIONS:
10131 s = (bfd_get_section_by_name
10132 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10133 dyn.d_un.d_ptr = s->vma;
10134 break;
10135
0a44bf69
RS
10136 case DT_RELASZ:
10137 BFD_ASSERT (htab->is_vxworks);
10138 /* The count does not include the JUMP_SLOT relocations. */
10139 if (htab->srelplt)
10140 dyn.d_un.d_val -= htab->srelplt->size;
10141 break;
10142
10143 case DT_PLTREL:
861fb55a
DJ
10144 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10145 if (htab->is_vxworks)
10146 dyn.d_un.d_val = DT_RELA;
10147 else
10148 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
10149 break;
10150
10151 case DT_PLTRELSZ:
861fb55a 10152 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
10153 dyn.d_un.d_val = htab->srelplt->size;
10154 break;
10155
10156 case DT_JMPREL:
861fb55a
DJ
10157 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10158 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
10159 + htab->srelplt->output_offset);
10160 break;
10161
943284cc
DJ
10162 case DT_TEXTREL:
10163 /* If we didn't need any text relocations after all, delete
10164 the dynamic tag. */
10165 if (!(info->flags & DF_TEXTREL))
10166 {
10167 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10168 swap_out_p = FALSE;
10169 }
10170 break;
10171
10172 case DT_FLAGS:
10173 /* If we didn't need any text relocations after all, clear
10174 DF_TEXTREL from DT_FLAGS. */
10175 if (!(info->flags & DF_TEXTREL))
10176 dyn.d_un.d_val &= ~DF_TEXTREL;
10177 else
10178 swap_out_p = FALSE;
10179 break;
10180
b49e97c9 10181 default:
b34976b6 10182 swap_out_p = FALSE;
7a2b07ff
NS
10183 if (htab->is_vxworks
10184 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10185 swap_out_p = TRUE;
b49e97c9
TS
10186 break;
10187 }
10188
943284cc 10189 if (swap_out_p || dyn_skipped)
b49e97c9 10190 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
10191 (dynobj, &dyn, b - dyn_skipped);
10192
10193 if (dyn_to_skip)
10194 {
10195 dyn_skipped += dyn_to_skip;
10196 dyn_to_skip = 0;
10197 }
b49e97c9 10198 }
943284cc
DJ
10199
10200 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10201 if (dyn_skipped > 0)
10202 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
10203 }
10204
b55fd4d4
DJ
10205 if (sgot != NULL && sgot->size > 0
10206 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 10207 {
0a44bf69
RS
10208 if (htab->is_vxworks)
10209 {
10210 /* The first entry of the global offset table points to the
10211 ".dynamic" section. The second is initialized by the
10212 loader and contains the shared library identifier.
10213 The third is also initialized by the loader and points
10214 to the lazy resolution stub. */
10215 MIPS_ELF_PUT_WORD (output_bfd,
10216 sdyn->output_offset + sdyn->output_section->vma,
10217 sgot->contents);
10218 MIPS_ELF_PUT_WORD (output_bfd, 0,
10219 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10220 MIPS_ELF_PUT_WORD (output_bfd, 0,
10221 sgot->contents
10222 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10223 }
10224 else
10225 {
10226 /* The first entry of the global offset table will be filled at
10227 runtime. The second entry will be used by some runtime loaders.
10228 This isn't the case of IRIX rld. */
10229 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 10230 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
10231 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10232 }
b49e97c9 10233
54938e2a
TS
10234 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10235 = MIPS_ELF_GOT_SIZE (output_bfd);
10236 }
b49e97c9 10237
f4416af6
AO
10238 /* Generate dynamic relocations for the non-primary gots. */
10239 if (gg != NULL && gg->next)
10240 {
10241 Elf_Internal_Rela rel[3];
10242 bfd_vma addend = 0;
10243
10244 memset (rel, 0, sizeof (rel));
10245 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10246
10247 for (g = gg->next; g->next != gg; g = g->next)
10248 {
91d6fa6a 10249 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 10250 + g->next->tls_gotno;
f4416af6 10251
9719ad41 10252 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 10253 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
10254 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10255 sgot->contents
91d6fa6a 10256 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
10257
10258 if (! info->shared)
10259 continue;
10260
91d6fa6a 10261 while (got_index < g->assigned_gotno)
f4416af6
AO
10262 {
10263 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
91d6fa6a 10264 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
10265 if (!(mips_elf_create_dynamic_relocation
10266 (output_bfd, info, rel, NULL,
10267 bfd_abs_section_ptr,
10268 0, &addend, sgot)))
10269 return FALSE;
10270 BFD_ASSERT (addend == 0);
10271 }
10272 }
10273 }
10274
3133ddbf
DJ
10275 /* The generation of dynamic relocations for the non-primary gots
10276 adds more dynamic relocations. We cannot count them until
10277 here. */
10278
10279 if (elf_hash_table (info)->dynamic_sections_created)
10280 {
10281 bfd_byte *b;
10282 bfd_boolean swap_out_p;
10283
10284 BFD_ASSERT (sdyn != NULL);
10285
10286 for (b = sdyn->contents;
10287 b < sdyn->contents + sdyn->size;
10288 b += MIPS_ELF_DYN_SIZE (dynobj))
10289 {
10290 Elf_Internal_Dyn dyn;
10291 asection *s;
10292
10293 /* Read in the current dynamic entry. */
10294 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10295
10296 /* Assume that we're going to modify it and write it out. */
10297 swap_out_p = TRUE;
10298
10299 switch (dyn.d_tag)
10300 {
10301 case DT_RELSZ:
10302 /* Reduce DT_RELSZ to account for any relocations we
10303 decided not to make. This is for the n64 irix rld,
10304 which doesn't seem to apply any relocations if there
10305 are trailing null entries. */
0a44bf69 10306 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
10307 dyn.d_un.d_val = (s->reloc_count
10308 * (ABI_64_P (output_bfd)
10309 ? sizeof (Elf64_Mips_External_Rel)
10310 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
10311 /* Adjust the section size too. Tools like the prelinker
10312 can reasonably expect the values to the same. */
10313 elf_section_data (s->output_section)->this_hdr.sh_size
10314 = dyn.d_un.d_val;
3133ddbf
DJ
10315 break;
10316
10317 default:
10318 swap_out_p = FALSE;
10319 break;
10320 }
10321
10322 if (swap_out_p)
10323 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10324 (dynobj, &dyn, b);
10325 }
10326 }
10327
b49e97c9 10328 {
b49e97c9
TS
10329 asection *s;
10330 Elf32_compact_rel cpt;
10331
b49e97c9
TS
10332 if (SGI_COMPAT (output_bfd))
10333 {
10334 /* Write .compact_rel section out. */
10335 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10336 if (s != NULL)
10337 {
10338 cpt.id1 = 1;
10339 cpt.num = s->reloc_count;
10340 cpt.id2 = 2;
10341 cpt.offset = (s->output_section->filepos
10342 + sizeof (Elf32_External_compact_rel));
10343 cpt.reserved0 = 0;
10344 cpt.reserved1 = 0;
10345 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10346 ((Elf32_External_compact_rel *)
10347 s->contents));
10348
10349 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 10350 if (htab->sstubs != NULL)
b49e97c9
TS
10351 {
10352 file_ptr dummy_offset;
10353
4e41d0d7
RS
10354 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10355 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10356 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 10357 htab->function_stub_size);
b49e97c9
TS
10358 }
10359 }
10360 }
10361
0a44bf69
RS
10362 /* The psABI says that the dynamic relocations must be sorted in
10363 increasing order of r_symndx. The VxWorks EABI doesn't require
10364 this, and because the code below handles REL rather than RELA
10365 relocations, using it for VxWorks would be outright harmful. */
10366 if (!htab->is_vxworks)
b49e97c9 10367 {
0a44bf69
RS
10368 s = mips_elf_rel_dyn_section (info, FALSE);
10369 if (s != NULL
10370 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10371 {
10372 reldyn_sorting_bfd = output_bfd;
b49e97c9 10373
0a44bf69
RS
10374 if (ABI_64_P (output_bfd))
10375 qsort ((Elf64_External_Rel *) s->contents + 1,
10376 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10377 sort_dynamic_relocs_64);
10378 else
10379 qsort ((Elf32_External_Rel *) s->contents + 1,
10380 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10381 sort_dynamic_relocs);
10382 }
b49e97c9 10383 }
b49e97c9
TS
10384 }
10385
861fb55a 10386 if (htab->splt && htab->splt->size > 0)
0a44bf69 10387 {
861fb55a
DJ
10388 if (htab->is_vxworks)
10389 {
10390 if (info->shared)
10391 mips_vxworks_finish_shared_plt (output_bfd, info);
10392 else
10393 mips_vxworks_finish_exec_plt (output_bfd, info);
10394 }
0a44bf69 10395 else
861fb55a
DJ
10396 {
10397 BFD_ASSERT (!info->shared);
10398 mips_finish_exec_plt (output_bfd, info);
10399 }
0a44bf69 10400 }
b34976b6 10401 return TRUE;
b49e97c9
TS
10402}
10403
b49e97c9 10404
64543e1a
RS
10405/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10406
10407static void
9719ad41 10408mips_set_isa_flags (bfd *abfd)
b49e97c9 10409{
64543e1a 10410 flagword val;
b49e97c9
TS
10411
10412 switch (bfd_get_mach (abfd))
10413 {
10414 default:
10415 case bfd_mach_mips3000:
10416 val = E_MIPS_ARCH_1;
10417 break;
10418
10419 case bfd_mach_mips3900:
10420 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10421 break;
10422
10423 case bfd_mach_mips6000:
10424 val = E_MIPS_ARCH_2;
10425 break;
10426
10427 case bfd_mach_mips4000:
10428 case bfd_mach_mips4300:
10429 case bfd_mach_mips4400:
10430 case bfd_mach_mips4600:
10431 val = E_MIPS_ARCH_3;
10432 break;
10433
10434 case bfd_mach_mips4010:
10435 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10436 break;
10437
10438 case bfd_mach_mips4100:
10439 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10440 break;
10441
10442 case bfd_mach_mips4111:
10443 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10444 break;
10445
00707a0e
RS
10446 case bfd_mach_mips4120:
10447 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10448 break;
10449
b49e97c9
TS
10450 case bfd_mach_mips4650:
10451 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10452 break;
10453
00707a0e
RS
10454 case bfd_mach_mips5400:
10455 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10456 break;
10457
10458 case bfd_mach_mips5500:
10459 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10460 break;
10461
0d2e43ed
ILT
10462 case bfd_mach_mips9000:
10463 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10464 break;
10465
b49e97c9 10466 case bfd_mach_mips5000:
5a7ea749 10467 case bfd_mach_mips7000:
b49e97c9
TS
10468 case bfd_mach_mips8000:
10469 case bfd_mach_mips10000:
10470 case bfd_mach_mips12000:
3aa3176b
TS
10471 case bfd_mach_mips14000:
10472 case bfd_mach_mips16000:
b49e97c9
TS
10473 val = E_MIPS_ARCH_4;
10474 break;
10475
10476 case bfd_mach_mips5:
10477 val = E_MIPS_ARCH_5;
10478 break;
10479
350cc38d
MS
10480 case bfd_mach_mips_loongson_2e:
10481 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10482 break;
10483
10484 case bfd_mach_mips_loongson_2f:
10485 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10486 break;
10487
b49e97c9
TS
10488 case bfd_mach_mips_sb1:
10489 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10490 break;
10491
6f179bd0
AN
10492 case bfd_mach_mips_octeon:
10493 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10494 break;
10495
52b6b6b9
JM
10496 case bfd_mach_mips_xlr:
10497 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10498 break;
10499
b49e97c9
TS
10500 case bfd_mach_mipsisa32:
10501 val = E_MIPS_ARCH_32;
10502 break;
10503
10504 case bfd_mach_mipsisa64:
10505 val = E_MIPS_ARCH_64;
af7ee8bf
CD
10506 break;
10507
10508 case bfd_mach_mipsisa32r2:
10509 val = E_MIPS_ARCH_32R2;
10510 break;
5f74bc13
CD
10511
10512 case bfd_mach_mipsisa64r2:
10513 val = E_MIPS_ARCH_64R2;
10514 break;
b49e97c9 10515 }
b49e97c9
TS
10516 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10517 elf_elfheader (abfd)->e_flags |= val;
10518
64543e1a
RS
10519}
10520
10521
10522/* The final processing done just before writing out a MIPS ELF object
10523 file. This gets the MIPS architecture right based on the machine
10524 number. This is used by both the 32-bit and the 64-bit ABI. */
10525
10526void
9719ad41
RS
10527_bfd_mips_elf_final_write_processing (bfd *abfd,
10528 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
10529{
10530 unsigned int i;
10531 Elf_Internal_Shdr **hdrpp;
10532 const char *name;
10533 asection *sec;
10534
10535 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10536 is nonzero. This is for compatibility with old objects, which used
10537 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10538 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10539 mips_set_isa_flags (abfd);
10540
b49e97c9
TS
10541 /* Set the sh_info field for .gptab sections and other appropriate
10542 info for each special section. */
10543 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10544 i < elf_numsections (abfd);
10545 i++, hdrpp++)
10546 {
10547 switch ((*hdrpp)->sh_type)
10548 {
10549 case SHT_MIPS_MSYM:
10550 case SHT_MIPS_LIBLIST:
10551 sec = bfd_get_section_by_name (abfd, ".dynstr");
10552 if (sec != NULL)
10553 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10554 break;
10555
10556 case SHT_MIPS_GPTAB:
10557 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10558 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10559 BFD_ASSERT (name != NULL
0112cd26 10560 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
10561 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10562 BFD_ASSERT (sec != NULL);
10563 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10564 break;
10565
10566 case SHT_MIPS_CONTENT:
10567 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10568 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10569 BFD_ASSERT (name != NULL
0112cd26 10570 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
10571 sec = bfd_get_section_by_name (abfd,
10572 name + sizeof ".MIPS.content" - 1);
10573 BFD_ASSERT (sec != NULL);
10574 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10575 break;
10576
10577 case SHT_MIPS_SYMBOL_LIB:
10578 sec = bfd_get_section_by_name (abfd, ".dynsym");
10579 if (sec != NULL)
10580 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10581 sec = bfd_get_section_by_name (abfd, ".liblist");
10582 if (sec != NULL)
10583 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10584 break;
10585
10586 case SHT_MIPS_EVENTS:
10587 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10588 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10589 BFD_ASSERT (name != NULL);
0112cd26 10590 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
10591 sec = bfd_get_section_by_name (abfd,
10592 name + sizeof ".MIPS.events" - 1);
10593 else
10594 {
0112cd26 10595 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
10596 sec = bfd_get_section_by_name (abfd,
10597 (name
10598 + sizeof ".MIPS.post_rel" - 1));
10599 }
10600 BFD_ASSERT (sec != NULL);
10601 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10602 break;
10603
10604 }
10605 }
10606}
10607\f
8dc1a139 10608/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
10609 segments. */
10610
10611int
a6b96beb
AM
10612_bfd_mips_elf_additional_program_headers (bfd *abfd,
10613 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
10614{
10615 asection *s;
10616 int ret = 0;
10617
10618 /* See if we need a PT_MIPS_REGINFO segment. */
10619 s = bfd_get_section_by_name (abfd, ".reginfo");
10620 if (s && (s->flags & SEC_LOAD))
10621 ++ret;
10622
10623 /* See if we need a PT_MIPS_OPTIONS segment. */
10624 if (IRIX_COMPAT (abfd) == ict_irix6
10625 && bfd_get_section_by_name (abfd,
10626 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10627 ++ret;
10628
10629 /* See if we need a PT_MIPS_RTPROC segment. */
10630 if (IRIX_COMPAT (abfd) == ict_irix5
10631 && bfd_get_section_by_name (abfd, ".dynamic")
10632 && bfd_get_section_by_name (abfd, ".mdebug"))
10633 ++ret;
10634
98c904a8
RS
10635 /* Allocate a PT_NULL header in dynamic objects. See
10636 _bfd_mips_elf_modify_segment_map for details. */
10637 if (!SGI_COMPAT (abfd)
10638 && bfd_get_section_by_name (abfd, ".dynamic"))
10639 ++ret;
10640
b49e97c9
TS
10641 return ret;
10642}
10643
8dc1a139 10644/* Modify the segment map for an IRIX5 executable. */
b49e97c9 10645
b34976b6 10646bfd_boolean
9719ad41 10647_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 10648 struct bfd_link_info *info)
b49e97c9
TS
10649{
10650 asection *s;
10651 struct elf_segment_map *m, **pm;
10652 bfd_size_type amt;
10653
10654 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10655 segment. */
10656 s = bfd_get_section_by_name (abfd, ".reginfo");
10657 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10658 {
10659 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10660 if (m->p_type == PT_MIPS_REGINFO)
10661 break;
10662 if (m == NULL)
10663 {
10664 amt = sizeof *m;
9719ad41 10665 m = bfd_zalloc (abfd, amt);
b49e97c9 10666 if (m == NULL)
b34976b6 10667 return FALSE;
b49e97c9
TS
10668
10669 m->p_type = PT_MIPS_REGINFO;
10670 m->count = 1;
10671 m->sections[0] = s;
10672
10673 /* We want to put it after the PHDR and INTERP segments. */
10674 pm = &elf_tdata (abfd)->segment_map;
10675 while (*pm != NULL
10676 && ((*pm)->p_type == PT_PHDR
10677 || (*pm)->p_type == PT_INTERP))
10678 pm = &(*pm)->next;
10679
10680 m->next = *pm;
10681 *pm = m;
10682 }
10683 }
10684
10685 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10686 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 10687 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 10688 table. */
c1fd6598
AO
10689 if (NEWABI_P (abfd)
10690 /* On non-IRIX6 new abi, we'll have already created a segment
10691 for this section, so don't create another. I'm not sure this
10692 is not also the case for IRIX 6, but I can't test it right
10693 now. */
10694 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
10695 {
10696 for (s = abfd->sections; s; s = s->next)
10697 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10698 break;
10699
10700 if (s)
10701 {
10702 struct elf_segment_map *options_segment;
10703
98a8deaf
RS
10704 pm = &elf_tdata (abfd)->segment_map;
10705 while (*pm != NULL
10706 && ((*pm)->p_type == PT_PHDR
10707 || (*pm)->p_type == PT_INTERP))
10708 pm = &(*pm)->next;
b49e97c9 10709
8ded5a0f
AM
10710 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10711 {
10712 amt = sizeof (struct elf_segment_map);
10713 options_segment = bfd_zalloc (abfd, amt);
10714 options_segment->next = *pm;
10715 options_segment->p_type = PT_MIPS_OPTIONS;
10716 options_segment->p_flags = PF_R;
10717 options_segment->p_flags_valid = TRUE;
10718 options_segment->count = 1;
10719 options_segment->sections[0] = s;
10720 *pm = options_segment;
10721 }
b49e97c9
TS
10722 }
10723 }
10724 else
10725 {
10726 if (IRIX_COMPAT (abfd) == ict_irix5)
10727 {
10728 /* If there are .dynamic and .mdebug sections, we make a room
10729 for the RTPROC header. FIXME: Rewrite without section names. */
10730 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10731 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10732 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10733 {
10734 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10735 if (m->p_type == PT_MIPS_RTPROC)
10736 break;
10737 if (m == NULL)
10738 {
10739 amt = sizeof *m;
9719ad41 10740 m = bfd_zalloc (abfd, amt);
b49e97c9 10741 if (m == NULL)
b34976b6 10742 return FALSE;
b49e97c9
TS
10743
10744 m->p_type = PT_MIPS_RTPROC;
10745
10746 s = bfd_get_section_by_name (abfd, ".rtproc");
10747 if (s == NULL)
10748 {
10749 m->count = 0;
10750 m->p_flags = 0;
10751 m->p_flags_valid = 1;
10752 }
10753 else
10754 {
10755 m->count = 1;
10756 m->sections[0] = s;
10757 }
10758
10759 /* We want to put it after the DYNAMIC segment. */
10760 pm = &elf_tdata (abfd)->segment_map;
10761 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10762 pm = &(*pm)->next;
10763 if (*pm != NULL)
10764 pm = &(*pm)->next;
10765
10766 m->next = *pm;
10767 *pm = m;
10768 }
10769 }
10770 }
8dc1a139 10771 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
10772 .dynstr, .dynsym, and .hash sections, and everything in
10773 between. */
10774 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10775 pm = &(*pm)->next)
10776 if ((*pm)->p_type == PT_DYNAMIC)
10777 break;
10778 m = *pm;
10779 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10780 {
10781 /* For a normal mips executable the permissions for the PT_DYNAMIC
10782 segment are read, write and execute. We do that here since
10783 the code in elf.c sets only the read permission. This matters
10784 sometimes for the dynamic linker. */
10785 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10786 {
10787 m->p_flags = PF_R | PF_W | PF_X;
10788 m->p_flags_valid = 1;
10789 }
10790 }
f6f62d6f
RS
10791 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10792 glibc's dynamic linker has traditionally derived the number of
10793 tags from the p_filesz field, and sometimes allocates stack
10794 arrays of that size. An overly-big PT_DYNAMIC segment can
10795 be actively harmful in such cases. Making PT_DYNAMIC contain
10796 other sections can also make life hard for the prelinker,
10797 which might move one of the other sections to a different
10798 PT_LOAD segment. */
10799 if (SGI_COMPAT (abfd)
10800 && m != NULL
10801 && m->count == 1
10802 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
10803 {
10804 static const char *sec_names[] =
10805 {
10806 ".dynamic", ".dynstr", ".dynsym", ".hash"
10807 };
10808 bfd_vma low, high;
10809 unsigned int i, c;
10810 struct elf_segment_map *n;
10811
792b4a53 10812 low = ~(bfd_vma) 0;
b49e97c9
TS
10813 high = 0;
10814 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10815 {
10816 s = bfd_get_section_by_name (abfd, sec_names[i]);
10817 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10818 {
10819 bfd_size_type sz;
10820
10821 if (low > s->vma)
10822 low = s->vma;
eea6121a 10823 sz = s->size;
b49e97c9
TS
10824 if (high < s->vma + sz)
10825 high = s->vma + sz;
10826 }
10827 }
10828
10829 c = 0;
10830 for (s = abfd->sections; s != NULL; s = s->next)
10831 if ((s->flags & SEC_LOAD) != 0
10832 && s->vma >= low
eea6121a 10833 && s->vma + s->size <= high)
b49e97c9
TS
10834 ++c;
10835
10836 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 10837 n = bfd_zalloc (abfd, amt);
b49e97c9 10838 if (n == NULL)
b34976b6 10839 return FALSE;
b49e97c9
TS
10840 *n = *m;
10841 n->count = c;
10842
10843 i = 0;
10844 for (s = abfd->sections; s != NULL; s = s->next)
10845 {
10846 if ((s->flags & SEC_LOAD) != 0
10847 && s->vma >= low
eea6121a 10848 && s->vma + s->size <= high)
b49e97c9
TS
10849 {
10850 n->sections[i] = s;
10851 ++i;
10852 }
10853 }
10854
10855 *pm = n;
10856 }
10857 }
10858
98c904a8
RS
10859 /* Allocate a spare program header in dynamic objects so that tools
10860 like the prelinker can add an extra PT_LOAD entry.
10861
10862 If the prelinker needs to make room for a new PT_LOAD entry, its
10863 standard procedure is to move the first (read-only) sections into
10864 the new (writable) segment. However, the MIPS ABI requires
10865 .dynamic to be in a read-only segment, and the section will often
10866 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10867
10868 Although the prelinker could in principle move .dynamic to a
10869 writable segment, it seems better to allocate a spare program
10870 header instead, and avoid the need to move any sections.
10871 There is a long tradition of allocating spare dynamic tags,
10872 so allocating a spare program header seems like a natural
7c8b76cc
JM
10873 extension.
10874
10875 If INFO is NULL, we may be copying an already prelinked binary
10876 with objcopy or strip, so do not add this header. */
10877 if (info != NULL
10878 && !SGI_COMPAT (abfd)
98c904a8
RS
10879 && bfd_get_section_by_name (abfd, ".dynamic"))
10880 {
10881 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10882 if ((*pm)->p_type == PT_NULL)
10883 break;
10884 if (*pm == NULL)
10885 {
10886 m = bfd_zalloc (abfd, sizeof (*m));
10887 if (m == NULL)
10888 return FALSE;
10889
10890 m->p_type = PT_NULL;
10891 *pm = m;
10892 }
10893 }
10894
b34976b6 10895 return TRUE;
b49e97c9
TS
10896}
10897\f
10898/* Return the section that should be marked against GC for a given
10899 relocation. */
10900
10901asection *
9719ad41 10902_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 10903 struct bfd_link_info *info,
9719ad41
RS
10904 Elf_Internal_Rela *rel,
10905 struct elf_link_hash_entry *h,
10906 Elf_Internal_Sym *sym)
b49e97c9
TS
10907{
10908 /* ??? Do mips16 stub sections need to be handled special? */
10909
10910 if (h != NULL)
07adf181
AM
10911 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10912 {
10913 case R_MIPS_GNU_VTINHERIT:
10914 case R_MIPS_GNU_VTENTRY:
10915 return NULL;
10916 }
b49e97c9 10917
07adf181 10918 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
10919}
10920
10921/* Update the got entry reference counts for the section being removed. */
10922
b34976b6 10923bfd_boolean
9719ad41
RS
10924_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10925 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10926 asection *sec ATTRIBUTE_UNUSED,
10927 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
10928{
10929#if 0
10930 Elf_Internal_Shdr *symtab_hdr;
10931 struct elf_link_hash_entry **sym_hashes;
10932 bfd_signed_vma *local_got_refcounts;
10933 const Elf_Internal_Rela *rel, *relend;
10934 unsigned long r_symndx;
10935 struct elf_link_hash_entry *h;
10936
7dda2462
TG
10937 if (info->relocatable)
10938 return TRUE;
10939
b49e97c9
TS
10940 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10941 sym_hashes = elf_sym_hashes (abfd);
10942 local_got_refcounts = elf_local_got_refcounts (abfd);
10943
10944 relend = relocs + sec->reloc_count;
10945 for (rel = relocs; rel < relend; rel++)
10946 switch (ELF_R_TYPE (abfd, rel->r_info))
10947 {
738e5348
RS
10948 case R_MIPS16_GOT16:
10949 case R_MIPS16_CALL16:
b49e97c9
TS
10950 case R_MIPS_GOT16:
10951 case R_MIPS_CALL16:
10952 case R_MIPS_CALL_HI16:
10953 case R_MIPS_CALL_LO16:
10954 case R_MIPS_GOT_HI16:
10955 case R_MIPS_GOT_LO16:
4a14403c
TS
10956 case R_MIPS_GOT_DISP:
10957 case R_MIPS_GOT_PAGE:
10958 case R_MIPS_GOT_OFST:
b49e97c9
TS
10959 /* ??? It would seem that the existing MIPS code does no sort
10960 of reference counting or whatnot on its GOT and PLT entries,
10961 so it is not possible to garbage collect them at this time. */
10962 break;
10963
10964 default:
10965 break;
10966 }
10967#endif
10968
b34976b6 10969 return TRUE;
b49e97c9
TS
10970}
10971\f
10972/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10973 hiding the old indirect symbol. Process additional relocation
10974 information. Also called for weakdefs, in which case we just let
10975 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
10976
10977void
fcfa13d2 10978_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
10979 struct elf_link_hash_entry *dir,
10980 struct elf_link_hash_entry *ind)
b49e97c9
TS
10981{
10982 struct mips_elf_link_hash_entry *dirmips, *indmips;
10983
fcfa13d2 10984 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 10985
861fb55a
DJ
10986 dirmips = (struct mips_elf_link_hash_entry *) dir;
10987 indmips = (struct mips_elf_link_hash_entry *) ind;
10988 /* Any absolute non-dynamic relocations against an indirect or weak
10989 definition will be against the target symbol. */
10990 if (indmips->has_static_relocs)
10991 dirmips->has_static_relocs = TRUE;
10992
b49e97c9
TS
10993 if (ind->root.type != bfd_link_hash_indirect)
10994 return;
10995
b49e97c9
TS
10996 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10997 if (indmips->readonly_reloc)
b34976b6 10998 dirmips->readonly_reloc = TRUE;
b49e97c9 10999 if (indmips->no_fn_stub)
b34976b6 11000 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
11001 if (indmips->fn_stub)
11002 {
11003 dirmips->fn_stub = indmips->fn_stub;
11004 indmips->fn_stub = NULL;
11005 }
11006 if (indmips->need_fn_stub)
11007 {
11008 dirmips->need_fn_stub = TRUE;
11009 indmips->need_fn_stub = FALSE;
11010 }
11011 if (indmips->call_stub)
11012 {
11013 dirmips->call_stub = indmips->call_stub;
11014 indmips->call_stub = NULL;
11015 }
11016 if (indmips->call_fp_stub)
11017 {
11018 dirmips->call_fp_stub = indmips->call_fp_stub;
11019 indmips->call_fp_stub = NULL;
11020 }
634835ae
RS
11021 if (indmips->global_got_area < dirmips->global_got_area)
11022 dirmips->global_got_area = indmips->global_got_area;
11023 if (indmips->global_got_area < GGA_NONE)
11024 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
11025 if (indmips->has_nonpic_branches)
11026 dirmips->has_nonpic_branches = TRUE;
0f20cc35
DJ
11027
11028 if (dirmips->tls_type == 0)
11029 dirmips->tls_type = indmips->tls_type;
b49e97c9 11030}
b49e97c9 11031\f
d01414a5
TS
11032#define PDR_SIZE 32
11033
b34976b6 11034bfd_boolean
9719ad41
RS
11035_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11036 struct bfd_link_info *info)
d01414a5
TS
11037{
11038 asection *o;
b34976b6 11039 bfd_boolean ret = FALSE;
d01414a5
TS
11040 unsigned char *tdata;
11041 size_t i, skip;
11042
11043 o = bfd_get_section_by_name (abfd, ".pdr");
11044 if (! o)
b34976b6 11045 return FALSE;
eea6121a 11046 if (o->size == 0)
b34976b6 11047 return FALSE;
eea6121a 11048 if (o->size % PDR_SIZE != 0)
b34976b6 11049 return FALSE;
d01414a5
TS
11050 if (o->output_section != NULL
11051 && bfd_is_abs_section (o->output_section))
b34976b6 11052 return FALSE;
d01414a5 11053
eea6121a 11054 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 11055 if (! tdata)
b34976b6 11056 return FALSE;
d01414a5 11057
9719ad41 11058 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 11059 info->keep_memory);
d01414a5
TS
11060 if (!cookie->rels)
11061 {
11062 free (tdata);
b34976b6 11063 return FALSE;
d01414a5
TS
11064 }
11065
11066 cookie->rel = cookie->rels;
11067 cookie->relend = cookie->rels + o->reloc_count;
11068
eea6121a 11069 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 11070 {
c152c796 11071 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
11072 {
11073 tdata[i] = 1;
11074 skip ++;
11075 }
11076 }
11077
11078 if (skip != 0)
11079 {
f0abc2a1 11080 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 11081 o->size -= skip * PDR_SIZE;
b34976b6 11082 ret = TRUE;
d01414a5
TS
11083 }
11084 else
11085 free (tdata);
11086
11087 if (! info->keep_memory)
11088 free (cookie->rels);
11089
11090 return ret;
11091}
11092
b34976b6 11093bfd_boolean
9719ad41 11094_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
11095{
11096 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
11097 return TRUE;
11098 return FALSE;
53bfd6b4 11099}
d01414a5 11100
b34976b6 11101bfd_boolean
c7b8f16e
JB
11102_bfd_mips_elf_write_section (bfd *output_bfd,
11103 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11104 asection *sec, bfd_byte *contents)
d01414a5
TS
11105{
11106 bfd_byte *to, *from, *end;
11107 int i;
11108
11109 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 11110 return FALSE;
d01414a5 11111
f0abc2a1 11112 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 11113 return FALSE;
d01414a5
TS
11114
11115 to = contents;
eea6121a 11116 end = contents + sec->size;
d01414a5
TS
11117 for (from = contents, i = 0;
11118 from < end;
11119 from += PDR_SIZE, i++)
11120 {
f0abc2a1 11121 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
11122 continue;
11123 if (to != from)
11124 memcpy (to, from, PDR_SIZE);
11125 to += PDR_SIZE;
11126 }
11127 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 11128 sec->output_offset, sec->size);
b34976b6 11129 return TRUE;
d01414a5 11130}
53bfd6b4 11131\f
b49e97c9
TS
11132/* MIPS ELF uses a special find_nearest_line routine in order the
11133 handle the ECOFF debugging information. */
11134
11135struct mips_elf_find_line
11136{
11137 struct ecoff_debug_info d;
11138 struct ecoff_find_line i;
11139};
11140
b34976b6 11141bfd_boolean
9719ad41
RS
11142_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11143 asymbol **symbols, bfd_vma offset,
11144 const char **filename_ptr,
11145 const char **functionname_ptr,
11146 unsigned int *line_ptr)
b49e97c9
TS
11147{
11148 asection *msec;
11149
11150 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11151 filename_ptr, functionname_ptr,
11152 line_ptr))
b34976b6 11153 return TRUE;
b49e97c9
TS
11154
11155 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11156 filename_ptr, functionname_ptr,
9719ad41 11157 line_ptr, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 11158 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 11159 return TRUE;
b49e97c9
TS
11160
11161 msec = bfd_get_section_by_name (abfd, ".mdebug");
11162 if (msec != NULL)
11163 {
11164 flagword origflags;
11165 struct mips_elf_find_line *fi;
11166 const struct ecoff_debug_swap * const swap =
11167 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11168
11169 /* If we are called during a link, mips_elf_final_link may have
11170 cleared the SEC_HAS_CONTENTS field. We force it back on here
11171 if appropriate (which it normally will be). */
11172 origflags = msec->flags;
11173 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11174 msec->flags |= SEC_HAS_CONTENTS;
11175
11176 fi = elf_tdata (abfd)->find_line_info;
11177 if (fi == NULL)
11178 {
11179 bfd_size_type external_fdr_size;
11180 char *fraw_src;
11181 char *fraw_end;
11182 struct fdr *fdr_ptr;
11183 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11184
9719ad41 11185 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
11186 if (fi == NULL)
11187 {
11188 msec->flags = origflags;
b34976b6 11189 return FALSE;
b49e97c9
TS
11190 }
11191
11192 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11193 {
11194 msec->flags = origflags;
b34976b6 11195 return FALSE;
b49e97c9
TS
11196 }
11197
11198 /* Swap in the FDR information. */
11199 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 11200 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
11201 if (fi->d.fdr == NULL)
11202 {
11203 msec->flags = origflags;
b34976b6 11204 return FALSE;
b49e97c9
TS
11205 }
11206 external_fdr_size = swap->external_fdr_size;
11207 fdr_ptr = fi->d.fdr;
11208 fraw_src = (char *) fi->d.external_fdr;
11209 fraw_end = (fraw_src
11210 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11211 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 11212 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9
TS
11213
11214 elf_tdata (abfd)->find_line_info = fi;
11215
11216 /* Note that we don't bother to ever free this information.
11217 find_nearest_line is either called all the time, as in
11218 objdump -l, so the information should be saved, or it is
11219 rarely called, as in ld error messages, so the memory
11220 wasted is unimportant. Still, it would probably be a
11221 good idea for free_cached_info to throw it away. */
11222 }
11223
11224 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11225 &fi->i, filename_ptr, functionname_ptr,
11226 line_ptr))
11227 {
11228 msec->flags = origflags;
b34976b6 11229 return TRUE;
b49e97c9
TS
11230 }
11231
11232 msec->flags = origflags;
11233 }
11234
11235 /* Fall back on the generic ELF find_nearest_line routine. */
11236
11237 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11238 filename_ptr, functionname_ptr,
11239 line_ptr);
11240}
4ab527b0
FF
11241
11242bfd_boolean
11243_bfd_mips_elf_find_inliner_info (bfd *abfd,
11244 const char **filename_ptr,
11245 const char **functionname_ptr,
11246 unsigned int *line_ptr)
11247{
11248 bfd_boolean found;
11249 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11250 functionname_ptr, line_ptr,
11251 & elf_tdata (abfd)->dwarf2_find_line_info);
11252 return found;
11253}
11254
b49e97c9
TS
11255\f
11256/* When are writing out the .options or .MIPS.options section,
11257 remember the bytes we are writing out, so that we can install the
11258 GP value in the section_processing routine. */
11259
b34976b6 11260bfd_boolean
9719ad41
RS
11261_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11262 const void *location,
11263 file_ptr offset, bfd_size_type count)
b49e97c9 11264{
cc2e31b9 11265 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
11266 {
11267 bfd_byte *c;
11268
11269 if (elf_section_data (section) == NULL)
11270 {
11271 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 11272 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 11273 if (elf_section_data (section) == NULL)
b34976b6 11274 return FALSE;
b49e97c9 11275 }
f0abc2a1 11276 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
11277 if (c == NULL)
11278 {
eea6121a 11279 c = bfd_zalloc (abfd, section->size);
b49e97c9 11280 if (c == NULL)
b34976b6 11281 return FALSE;
f0abc2a1 11282 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
11283 }
11284
9719ad41 11285 memcpy (c + offset, location, count);
b49e97c9
TS
11286 }
11287
11288 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11289 count);
11290}
11291
11292/* This is almost identical to bfd_generic_get_... except that some
11293 MIPS relocations need to be handled specially. Sigh. */
11294
11295bfd_byte *
9719ad41
RS
11296_bfd_elf_mips_get_relocated_section_contents
11297 (bfd *abfd,
11298 struct bfd_link_info *link_info,
11299 struct bfd_link_order *link_order,
11300 bfd_byte *data,
11301 bfd_boolean relocatable,
11302 asymbol **symbols)
b49e97c9
TS
11303{
11304 /* Get enough memory to hold the stuff */
11305 bfd *input_bfd = link_order->u.indirect.section->owner;
11306 asection *input_section = link_order->u.indirect.section;
eea6121a 11307 bfd_size_type sz;
b49e97c9
TS
11308
11309 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11310 arelent **reloc_vector = NULL;
11311 long reloc_count;
11312
11313 if (reloc_size < 0)
11314 goto error_return;
11315
9719ad41 11316 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
11317 if (reloc_vector == NULL && reloc_size != 0)
11318 goto error_return;
11319
11320 /* read in the section */
eea6121a
AM
11321 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11322 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
11323 goto error_return;
11324
b49e97c9
TS
11325 reloc_count = bfd_canonicalize_reloc (input_bfd,
11326 input_section,
11327 reloc_vector,
11328 symbols);
11329 if (reloc_count < 0)
11330 goto error_return;
11331
11332 if (reloc_count > 0)
11333 {
11334 arelent **parent;
11335 /* for mips */
11336 int gp_found;
11337 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11338
11339 {
11340 struct bfd_hash_entry *h;
11341 struct bfd_link_hash_entry *lh;
11342 /* Skip all this stuff if we aren't mixing formats. */
11343 if (abfd && input_bfd
11344 && abfd->xvec == input_bfd->xvec)
11345 lh = 0;
11346 else
11347 {
b34976b6 11348 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
11349 lh = (struct bfd_link_hash_entry *) h;
11350 }
11351 lookup:
11352 if (lh)
11353 {
11354 switch (lh->type)
11355 {
11356 case bfd_link_hash_undefined:
11357 case bfd_link_hash_undefweak:
11358 case bfd_link_hash_common:
11359 gp_found = 0;
11360 break;
11361 case bfd_link_hash_defined:
11362 case bfd_link_hash_defweak:
11363 gp_found = 1;
11364 gp = lh->u.def.value;
11365 break;
11366 case bfd_link_hash_indirect:
11367 case bfd_link_hash_warning:
11368 lh = lh->u.i.link;
11369 /* @@FIXME ignoring warning for now */
11370 goto lookup;
11371 case bfd_link_hash_new:
11372 default:
11373 abort ();
11374 }
11375 }
11376 else
11377 gp_found = 0;
11378 }
11379 /* end mips */
9719ad41 11380 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 11381 {
9719ad41 11382 char *error_message = NULL;
b49e97c9
TS
11383 bfd_reloc_status_type r;
11384
11385 /* Specific to MIPS: Deal with relocation types that require
11386 knowing the gp of the output bfd. */
11387 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 11388
8236346f
EC
11389 /* If we've managed to find the gp and have a special
11390 function for the relocation then go ahead, else default
11391 to the generic handling. */
11392 if (gp_found
11393 && (*parent)->howto->special_function
11394 == _bfd_mips_elf32_gprel16_reloc)
11395 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11396 input_section, relocatable,
11397 data, gp);
11398 else
86324f90 11399 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
11400 input_section,
11401 relocatable ? abfd : NULL,
11402 &error_message);
b49e97c9 11403
1049f94e 11404 if (relocatable)
b49e97c9
TS
11405 {
11406 asection *os = input_section->output_section;
11407
11408 /* A partial link, so keep the relocs */
11409 os->orelocation[os->reloc_count] = *parent;
11410 os->reloc_count++;
11411 }
11412
11413 if (r != bfd_reloc_ok)
11414 {
11415 switch (r)
11416 {
11417 case bfd_reloc_undefined:
11418 if (!((*link_info->callbacks->undefined_symbol)
11419 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 11420 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
11421 goto error_return;
11422 break;
11423 case bfd_reloc_dangerous:
9719ad41 11424 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
11425 if (!((*link_info->callbacks->reloc_dangerous)
11426 (link_info, error_message, input_bfd, input_section,
11427 (*parent)->address)))
11428 goto error_return;
11429 break;
11430 case bfd_reloc_overflow:
11431 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
11432 (link_info, NULL,
11433 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
11434 (*parent)->howto->name, (*parent)->addend,
11435 input_bfd, input_section, (*parent)->address)))
11436 goto error_return;
11437 break;
11438 case bfd_reloc_outofrange:
11439 default:
11440 abort ();
11441 break;
11442 }
11443
11444 }
11445 }
11446 }
11447 if (reloc_vector != NULL)
11448 free (reloc_vector);
11449 return data;
11450
11451error_return:
11452 if (reloc_vector != NULL)
11453 free (reloc_vector);
11454 return NULL;
11455}
11456\f
d5eaccd7
RS
11457/* Allocate ABFD's target-dependent data. */
11458
11459bfd_boolean
11460_bfd_mips_elf_mkobject (bfd *abfd)
11461{
11462 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
11463 MIPS_ELF_TDATA);
11464}
11465
b49e97c9
TS
11466/* Create a MIPS ELF linker hash table. */
11467
11468struct bfd_link_hash_table *
9719ad41 11469_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
11470{
11471 struct mips_elf_link_hash_table *ret;
11472 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11473
9719ad41
RS
11474 ret = bfd_malloc (amt);
11475 if (ret == NULL)
b49e97c9
TS
11476 return NULL;
11477
66eb6687
AM
11478 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11479 mips_elf_link_hash_newfunc,
11480 sizeof (struct mips_elf_link_hash_entry)))
b49e97c9 11481 {
e2d34d7d 11482 free (ret);
b49e97c9
TS
11483 return NULL;
11484 }
11485
11486#if 0
11487 /* We no longer use this. */
11488 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11489 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11490#endif
11491 ret->procedure_count = 0;
11492 ret->compact_rel_size = 0;
b34976b6 11493 ret->use_rld_obj_head = FALSE;
b49e97c9 11494 ret->rld_value = 0;
b34976b6 11495 ret->mips16_stubs_seen = FALSE;
861fb55a 11496 ret->use_plts_and_copy_relocs = FALSE;
0a44bf69 11497 ret->is_vxworks = FALSE;
0e53d9da 11498 ret->small_data_overflow_reported = FALSE;
0a44bf69
RS
11499 ret->srelbss = NULL;
11500 ret->sdynbss = NULL;
11501 ret->srelplt = NULL;
11502 ret->srelplt2 = NULL;
11503 ret->sgotplt = NULL;
11504 ret->splt = NULL;
4e41d0d7 11505 ret->sstubs = NULL;
a8028dd0
RS
11506 ret->sgot = NULL;
11507 ret->got_info = NULL;
0a44bf69
RS
11508 ret->plt_header_size = 0;
11509 ret->plt_entry_size = 0;
33bb52fb 11510 ret->lazy_stub_count = 0;
5108fc1b 11511 ret->function_stub_size = 0;
861fb55a
DJ
11512 ret->strampoline = NULL;
11513 ret->la25_stubs = NULL;
11514 ret->add_stub_section = NULL;
b49e97c9
TS
11515
11516 return &ret->root.root;
11517}
0a44bf69
RS
11518
11519/* Likewise, but indicate that the target is VxWorks. */
11520
11521struct bfd_link_hash_table *
11522_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11523{
11524 struct bfd_link_hash_table *ret;
11525
11526 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11527 if (ret)
11528 {
11529 struct mips_elf_link_hash_table *htab;
11530
11531 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
11532 htab->use_plts_and_copy_relocs = TRUE;
11533 htab->is_vxworks = TRUE;
0a44bf69
RS
11534 }
11535 return ret;
11536}
861fb55a
DJ
11537
11538/* A function that the linker calls if we are allowed to use PLTs
11539 and copy relocs. */
11540
11541void
11542_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11543{
11544 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11545}
b49e97c9
TS
11546\f
11547/* We need to use a special link routine to handle the .reginfo and
11548 the .mdebug sections. We need to merge all instances of these
11549 sections together, not write them all out sequentially. */
11550
b34976b6 11551bfd_boolean
9719ad41 11552_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 11553{
b49e97c9
TS
11554 asection *o;
11555 struct bfd_link_order *p;
11556 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11557 asection *rtproc_sec;
11558 Elf32_RegInfo reginfo;
11559 struct ecoff_debug_info debug;
861fb55a 11560 struct mips_htab_traverse_info hti;
7a2a6943
NC
11561 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11562 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 11563 HDRR *symhdr = &debug.symbolic_header;
9719ad41 11564 void *mdebug_handle = NULL;
b49e97c9
TS
11565 asection *s;
11566 EXTR esym;
11567 unsigned int i;
11568 bfd_size_type amt;
0a44bf69 11569 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
11570
11571 static const char * const secname[] =
11572 {
11573 ".text", ".init", ".fini", ".data",
11574 ".rodata", ".sdata", ".sbss", ".bss"
11575 };
11576 static const int sc[] =
11577 {
11578 scText, scInit, scFini, scData,
11579 scRData, scSData, scSBss, scBss
11580 };
11581
d4596a51
RS
11582 /* Sort the dynamic symbols so that those with GOT entries come after
11583 those without. */
0a44bf69 11584 htab = mips_elf_hash_table (info);
d4596a51
RS
11585 if (!mips_elf_sort_hash_table (abfd, info))
11586 return FALSE;
b49e97c9 11587
861fb55a
DJ
11588 /* Create any scheduled LA25 stubs. */
11589 hti.info = info;
11590 hti.output_bfd = abfd;
11591 hti.error = FALSE;
11592 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11593 if (hti.error)
11594 return FALSE;
11595
b49e97c9
TS
11596 /* Get a value for the GP register. */
11597 if (elf_gp (abfd) == 0)
11598 {
11599 struct bfd_link_hash_entry *h;
11600
b34976b6 11601 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 11602 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
11603 elf_gp (abfd) = (h->u.def.value
11604 + h->u.def.section->output_section->vma
11605 + h->u.def.section->output_offset);
0a44bf69
RS
11606 else if (htab->is_vxworks
11607 && (h = bfd_link_hash_lookup (info->hash,
11608 "_GLOBAL_OFFSET_TABLE_",
11609 FALSE, FALSE, TRUE))
11610 && h->type == bfd_link_hash_defined)
11611 elf_gp (abfd) = (h->u.def.section->output_section->vma
11612 + h->u.def.section->output_offset
11613 + h->u.def.value);
1049f94e 11614 else if (info->relocatable)
b49e97c9
TS
11615 {
11616 bfd_vma lo = MINUS_ONE;
11617
11618 /* Find the GP-relative section with the lowest offset. */
9719ad41 11619 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11620 if (o->vma < lo
11621 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11622 lo = o->vma;
11623
11624 /* And calculate GP relative to that. */
0a44bf69 11625 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
11626 }
11627 else
11628 {
11629 /* If the relocate_section function needs to do a reloc
11630 involving the GP value, it should make a reloc_dangerous
11631 callback to warn that GP is not defined. */
11632 }
11633 }
11634
11635 /* Go through the sections and collect the .reginfo and .mdebug
11636 information. */
11637 reginfo_sec = NULL;
11638 mdebug_sec = NULL;
11639 gptab_data_sec = NULL;
11640 gptab_bss_sec = NULL;
9719ad41 11641 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11642 {
11643 if (strcmp (o->name, ".reginfo") == 0)
11644 {
11645 memset (&reginfo, 0, sizeof reginfo);
11646
11647 /* We have found the .reginfo section in the output file.
11648 Look through all the link_orders comprising it and merge
11649 the information together. */
8423293d 11650 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11651 {
11652 asection *input_section;
11653 bfd *input_bfd;
11654 Elf32_External_RegInfo ext;
11655 Elf32_RegInfo sub;
11656
11657 if (p->type != bfd_indirect_link_order)
11658 {
11659 if (p->type == bfd_data_link_order)
11660 continue;
11661 abort ();
11662 }
11663
11664 input_section = p->u.indirect.section;
11665 input_bfd = input_section->owner;
11666
b49e97c9 11667 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 11668 &ext, 0, sizeof ext))
b34976b6 11669 return FALSE;
b49e97c9
TS
11670
11671 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11672
11673 reginfo.ri_gprmask |= sub.ri_gprmask;
11674 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11675 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11676 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11677 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11678
11679 /* ri_gp_value is set by the function
11680 mips_elf32_section_processing when the section is
11681 finally written out. */
11682
11683 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11684 elf_link_input_bfd ignores this section. */
11685 input_section->flags &= ~SEC_HAS_CONTENTS;
11686 }
11687
11688 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 11689 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
11690
11691 /* Skip this section later on (I don't think this currently
11692 matters, but someday it might). */
8423293d 11693 o->map_head.link_order = NULL;
b49e97c9
TS
11694
11695 reginfo_sec = o;
11696 }
11697
11698 if (strcmp (o->name, ".mdebug") == 0)
11699 {
11700 struct extsym_info einfo;
11701 bfd_vma last;
11702
11703 /* We have found the .mdebug section in the output file.
11704 Look through all the link_orders comprising it and merge
11705 the information together. */
11706 symhdr->magic = swap->sym_magic;
11707 /* FIXME: What should the version stamp be? */
11708 symhdr->vstamp = 0;
11709 symhdr->ilineMax = 0;
11710 symhdr->cbLine = 0;
11711 symhdr->idnMax = 0;
11712 symhdr->ipdMax = 0;
11713 symhdr->isymMax = 0;
11714 symhdr->ioptMax = 0;
11715 symhdr->iauxMax = 0;
11716 symhdr->issMax = 0;
11717 symhdr->issExtMax = 0;
11718 symhdr->ifdMax = 0;
11719 symhdr->crfd = 0;
11720 symhdr->iextMax = 0;
11721
11722 /* We accumulate the debugging information itself in the
11723 debug_info structure. */
11724 debug.line = NULL;
11725 debug.external_dnr = NULL;
11726 debug.external_pdr = NULL;
11727 debug.external_sym = NULL;
11728 debug.external_opt = NULL;
11729 debug.external_aux = NULL;
11730 debug.ss = NULL;
11731 debug.ssext = debug.ssext_end = NULL;
11732 debug.external_fdr = NULL;
11733 debug.external_rfd = NULL;
11734 debug.external_ext = debug.external_ext_end = NULL;
11735
11736 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 11737 if (mdebug_handle == NULL)
b34976b6 11738 return FALSE;
b49e97c9
TS
11739
11740 esym.jmptbl = 0;
11741 esym.cobol_main = 0;
11742 esym.weakext = 0;
11743 esym.reserved = 0;
11744 esym.ifd = ifdNil;
11745 esym.asym.iss = issNil;
11746 esym.asym.st = stLocal;
11747 esym.asym.reserved = 0;
11748 esym.asym.index = indexNil;
11749 last = 0;
11750 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11751 {
11752 esym.asym.sc = sc[i];
11753 s = bfd_get_section_by_name (abfd, secname[i]);
11754 if (s != NULL)
11755 {
11756 esym.asym.value = s->vma;
eea6121a 11757 last = s->vma + s->size;
b49e97c9
TS
11758 }
11759 else
11760 esym.asym.value = last;
11761 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11762 secname[i], &esym))
b34976b6 11763 return FALSE;
b49e97c9
TS
11764 }
11765
8423293d 11766 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11767 {
11768 asection *input_section;
11769 bfd *input_bfd;
11770 const struct ecoff_debug_swap *input_swap;
11771 struct ecoff_debug_info input_debug;
11772 char *eraw_src;
11773 char *eraw_end;
11774
11775 if (p->type != bfd_indirect_link_order)
11776 {
11777 if (p->type == bfd_data_link_order)
11778 continue;
11779 abort ();
11780 }
11781
11782 input_section = p->u.indirect.section;
11783 input_bfd = input_section->owner;
11784
d5eaccd7 11785 if (!is_mips_elf (input_bfd))
b49e97c9
TS
11786 {
11787 /* I don't know what a non MIPS ELF bfd would be
11788 doing with a .mdebug section, but I don't really
11789 want to deal with it. */
11790 continue;
11791 }
11792
11793 input_swap = (get_elf_backend_data (input_bfd)
11794 ->elf_backend_ecoff_debug_swap);
11795
eea6121a 11796 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
11797
11798 /* The ECOFF linking code expects that we have already
11799 read in the debugging information and set up an
11800 ecoff_debug_info structure, so we do that now. */
11801 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11802 &input_debug))
b34976b6 11803 return FALSE;
b49e97c9
TS
11804
11805 if (! (bfd_ecoff_debug_accumulate
11806 (mdebug_handle, abfd, &debug, swap, input_bfd,
11807 &input_debug, input_swap, info)))
b34976b6 11808 return FALSE;
b49e97c9
TS
11809
11810 /* Loop through the external symbols. For each one with
11811 interesting information, try to find the symbol in
11812 the linker global hash table and save the information
11813 for the output external symbols. */
11814 eraw_src = input_debug.external_ext;
11815 eraw_end = (eraw_src
11816 + (input_debug.symbolic_header.iextMax
11817 * input_swap->external_ext_size));
11818 for (;
11819 eraw_src < eraw_end;
11820 eraw_src += input_swap->external_ext_size)
11821 {
11822 EXTR ext;
11823 const char *name;
11824 struct mips_elf_link_hash_entry *h;
11825
9719ad41 11826 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
11827 if (ext.asym.sc == scNil
11828 || ext.asym.sc == scUndefined
11829 || ext.asym.sc == scSUndefined)
11830 continue;
11831
11832 name = input_debug.ssext + ext.asym.iss;
11833 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 11834 name, FALSE, FALSE, TRUE);
b49e97c9
TS
11835 if (h == NULL || h->esym.ifd != -2)
11836 continue;
11837
11838 if (ext.ifd != -1)
11839 {
11840 BFD_ASSERT (ext.ifd
11841 < input_debug.symbolic_header.ifdMax);
11842 ext.ifd = input_debug.ifdmap[ext.ifd];
11843 }
11844
11845 h->esym = ext;
11846 }
11847
11848 /* Free up the information we just read. */
11849 free (input_debug.line);
11850 free (input_debug.external_dnr);
11851 free (input_debug.external_pdr);
11852 free (input_debug.external_sym);
11853 free (input_debug.external_opt);
11854 free (input_debug.external_aux);
11855 free (input_debug.ss);
11856 free (input_debug.ssext);
11857 free (input_debug.external_fdr);
11858 free (input_debug.external_rfd);
11859 free (input_debug.external_ext);
11860
11861 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11862 elf_link_input_bfd ignores this section. */
11863 input_section->flags &= ~SEC_HAS_CONTENTS;
11864 }
11865
11866 if (SGI_COMPAT (abfd) && info->shared)
11867 {
11868 /* Create .rtproc section. */
11869 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11870 if (rtproc_sec == NULL)
11871 {
11872 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11873 | SEC_LINKER_CREATED | SEC_READONLY);
11874
3496cb2a
L
11875 rtproc_sec = bfd_make_section_with_flags (abfd,
11876 ".rtproc",
11877 flags);
b49e97c9 11878 if (rtproc_sec == NULL
b49e97c9 11879 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 11880 return FALSE;
b49e97c9
TS
11881 }
11882
11883 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11884 info, rtproc_sec,
11885 &debug))
b34976b6 11886 return FALSE;
b49e97c9
TS
11887 }
11888
11889 /* Build the external symbol information. */
11890 einfo.abfd = abfd;
11891 einfo.info = info;
11892 einfo.debug = &debug;
11893 einfo.swap = swap;
b34976b6 11894 einfo.failed = FALSE;
b49e97c9 11895 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 11896 mips_elf_output_extsym, &einfo);
b49e97c9 11897 if (einfo.failed)
b34976b6 11898 return FALSE;
b49e97c9
TS
11899
11900 /* Set the size of the .mdebug section. */
eea6121a 11901 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
11902
11903 /* Skip this section later on (I don't think this currently
11904 matters, but someday it might). */
8423293d 11905 o->map_head.link_order = NULL;
b49e97c9
TS
11906
11907 mdebug_sec = o;
11908 }
11909
0112cd26 11910 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
11911 {
11912 const char *subname;
11913 unsigned int c;
11914 Elf32_gptab *tab;
11915 Elf32_External_gptab *ext_tab;
11916 unsigned int j;
11917
11918 /* The .gptab.sdata and .gptab.sbss sections hold
11919 information describing how the small data area would
11920 change depending upon the -G switch. These sections
11921 not used in executables files. */
1049f94e 11922 if (! info->relocatable)
b49e97c9 11923 {
8423293d 11924 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11925 {
11926 asection *input_section;
11927
11928 if (p->type != bfd_indirect_link_order)
11929 {
11930 if (p->type == bfd_data_link_order)
11931 continue;
11932 abort ();
11933 }
11934
11935 input_section = p->u.indirect.section;
11936
11937 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11938 elf_link_input_bfd ignores this section. */
11939 input_section->flags &= ~SEC_HAS_CONTENTS;
11940 }
11941
11942 /* Skip this section later on (I don't think this
11943 currently matters, but someday it might). */
8423293d 11944 o->map_head.link_order = NULL;
b49e97c9
TS
11945
11946 /* Really remove the section. */
5daa8fe7 11947 bfd_section_list_remove (abfd, o);
b49e97c9
TS
11948 --abfd->section_count;
11949
11950 continue;
11951 }
11952
11953 /* There is one gptab for initialized data, and one for
11954 uninitialized data. */
11955 if (strcmp (o->name, ".gptab.sdata") == 0)
11956 gptab_data_sec = o;
11957 else if (strcmp (o->name, ".gptab.sbss") == 0)
11958 gptab_bss_sec = o;
11959 else
11960 {
11961 (*_bfd_error_handler)
11962 (_("%s: illegal section name `%s'"),
11963 bfd_get_filename (abfd), o->name);
11964 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 11965 return FALSE;
b49e97c9
TS
11966 }
11967
11968 /* The linker script always combines .gptab.data and
11969 .gptab.sdata into .gptab.sdata, and likewise for
11970 .gptab.bss and .gptab.sbss. It is possible that there is
11971 no .sdata or .sbss section in the output file, in which
11972 case we must change the name of the output section. */
11973 subname = o->name + sizeof ".gptab" - 1;
11974 if (bfd_get_section_by_name (abfd, subname) == NULL)
11975 {
11976 if (o == gptab_data_sec)
11977 o->name = ".gptab.data";
11978 else
11979 o->name = ".gptab.bss";
11980 subname = o->name + sizeof ".gptab" - 1;
11981 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11982 }
11983
11984 /* Set up the first entry. */
11985 c = 1;
11986 amt = c * sizeof (Elf32_gptab);
9719ad41 11987 tab = bfd_malloc (amt);
b49e97c9 11988 if (tab == NULL)
b34976b6 11989 return FALSE;
b49e97c9
TS
11990 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11991 tab[0].gt_header.gt_unused = 0;
11992
11993 /* Combine the input sections. */
8423293d 11994 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11995 {
11996 asection *input_section;
11997 bfd *input_bfd;
11998 bfd_size_type size;
11999 unsigned long last;
12000 bfd_size_type gpentry;
12001
12002 if (p->type != bfd_indirect_link_order)
12003 {
12004 if (p->type == bfd_data_link_order)
12005 continue;
12006 abort ();
12007 }
12008
12009 input_section = p->u.indirect.section;
12010 input_bfd = input_section->owner;
12011
12012 /* Combine the gptab entries for this input section one
12013 by one. We know that the input gptab entries are
12014 sorted by ascending -G value. */
eea6121a 12015 size = input_section->size;
b49e97c9
TS
12016 last = 0;
12017 for (gpentry = sizeof (Elf32_External_gptab);
12018 gpentry < size;
12019 gpentry += sizeof (Elf32_External_gptab))
12020 {
12021 Elf32_External_gptab ext_gptab;
12022 Elf32_gptab int_gptab;
12023 unsigned long val;
12024 unsigned long add;
b34976b6 12025 bfd_boolean exact;
b49e97c9
TS
12026 unsigned int look;
12027
12028 if (! (bfd_get_section_contents
9719ad41
RS
12029 (input_bfd, input_section, &ext_gptab, gpentry,
12030 sizeof (Elf32_External_gptab))))
b49e97c9
TS
12031 {
12032 free (tab);
b34976b6 12033 return FALSE;
b49e97c9
TS
12034 }
12035
12036 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12037 &int_gptab);
12038 val = int_gptab.gt_entry.gt_g_value;
12039 add = int_gptab.gt_entry.gt_bytes - last;
12040
b34976b6 12041 exact = FALSE;
b49e97c9
TS
12042 for (look = 1; look < c; look++)
12043 {
12044 if (tab[look].gt_entry.gt_g_value >= val)
12045 tab[look].gt_entry.gt_bytes += add;
12046
12047 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 12048 exact = TRUE;
b49e97c9
TS
12049 }
12050
12051 if (! exact)
12052 {
12053 Elf32_gptab *new_tab;
12054 unsigned int max;
12055
12056 /* We need a new table entry. */
12057 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 12058 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
12059 if (new_tab == NULL)
12060 {
12061 free (tab);
b34976b6 12062 return FALSE;
b49e97c9
TS
12063 }
12064 tab = new_tab;
12065 tab[c].gt_entry.gt_g_value = val;
12066 tab[c].gt_entry.gt_bytes = add;
12067
12068 /* Merge in the size for the next smallest -G
12069 value, since that will be implied by this new
12070 value. */
12071 max = 0;
12072 for (look = 1; look < c; look++)
12073 {
12074 if (tab[look].gt_entry.gt_g_value < val
12075 && (max == 0
12076 || (tab[look].gt_entry.gt_g_value
12077 > tab[max].gt_entry.gt_g_value)))
12078 max = look;
12079 }
12080 if (max != 0)
12081 tab[c].gt_entry.gt_bytes +=
12082 tab[max].gt_entry.gt_bytes;
12083
12084 ++c;
12085 }
12086
12087 last = int_gptab.gt_entry.gt_bytes;
12088 }
12089
12090 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12091 elf_link_input_bfd ignores this section. */
12092 input_section->flags &= ~SEC_HAS_CONTENTS;
12093 }
12094
12095 /* The table must be sorted by -G value. */
12096 if (c > 2)
12097 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12098
12099 /* Swap out the table. */
12100 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 12101 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
12102 if (ext_tab == NULL)
12103 {
12104 free (tab);
b34976b6 12105 return FALSE;
b49e97c9
TS
12106 }
12107
12108 for (j = 0; j < c; j++)
12109 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12110 free (tab);
12111
eea6121a 12112 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
12113 o->contents = (bfd_byte *) ext_tab;
12114
12115 /* Skip this section later on (I don't think this currently
12116 matters, but someday it might). */
8423293d 12117 o->map_head.link_order = NULL;
b49e97c9
TS
12118 }
12119 }
12120
12121 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 12122 if (!bfd_elf_final_link (abfd, info))
b34976b6 12123 return FALSE;
b49e97c9
TS
12124
12125 /* Now write out the computed sections. */
12126
9719ad41 12127 if (reginfo_sec != NULL)
b49e97c9
TS
12128 {
12129 Elf32_External_RegInfo ext;
12130
12131 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 12132 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 12133 return FALSE;
b49e97c9
TS
12134 }
12135
9719ad41 12136 if (mdebug_sec != NULL)
b49e97c9
TS
12137 {
12138 BFD_ASSERT (abfd->output_has_begun);
12139 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12140 swap, info,
12141 mdebug_sec->filepos))
b34976b6 12142 return FALSE;
b49e97c9
TS
12143
12144 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12145 }
12146
9719ad41 12147 if (gptab_data_sec != NULL)
b49e97c9
TS
12148 {
12149 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12150 gptab_data_sec->contents,
eea6121a 12151 0, gptab_data_sec->size))
b34976b6 12152 return FALSE;
b49e97c9
TS
12153 }
12154
9719ad41 12155 if (gptab_bss_sec != NULL)
b49e97c9
TS
12156 {
12157 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12158 gptab_bss_sec->contents,
eea6121a 12159 0, gptab_bss_sec->size))
b34976b6 12160 return FALSE;
b49e97c9
TS
12161 }
12162
12163 if (SGI_COMPAT (abfd))
12164 {
12165 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12166 if (rtproc_sec != NULL)
12167 {
12168 if (! bfd_set_section_contents (abfd, rtproc_sec,
12169 rtproc_sec->contents,
eea6121a 12170 0, rtproc_sec->size))
b34976b6 12171 return FALSE;
b49e97c9
TS
12172 }
12173 }
12174
b34976b6 12175 return TRUE;
b49e97c9
TS
12176}
12177\f
64543e1a
RS
12178/* Structure for saying that BFD machine EXTENSION extends BASE. */
12179
12180struct mips_mach_extension {
12181 unsigned long extension, base;
12182};
12183
12184
12185/* An array describing how BFD machines relate to one another. The entries
12186 are ordered topologically with MIPS I extensions listed last. */
12187
12188static const struct mips_mach_extension mips_mach_extensions[] = {
6f179bd0
AN
12189 /* MIPS64r2 extensions. */
12190 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12191
64543e1a 12192 /* MIPS64 extensions. */
5f74bc13 12193 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 12194 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 12195 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
64543e1a
RS
12196
12197 /* MIPS V extensions. */
12198 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12199
12200 /* R10000 extensions. */
12201 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
12202 { bfd_mach_mips14000, bfd_mach_mips10000 },
12203 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
12204
12205 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12206 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12207 better to allow vr5400 and vr5500 code to be merged anyway, since
12208 many libraries will just use the core ISA. Perhaps we could add
12209 some sort of ASE flag if this ever proves a problem. */
12210 { bfd_mach_mips5500, bfd_mach_mips5400 },
12211 { bfd_mach_mips5400, bfd_mach_mips5000 },
12212
12213 /* MIPS IV extensions. */
12214 { bfd_mach_mips5, bfd_mach_mips8000 },
12215 { bfd_mach_mips10000, bfd_mach_mips8000 },
12216 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 12217 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 12218 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
12219
12220 /* VR4100 extensions. */
12221 { bfd_mach_mips4120, bfd_mach_mips4100 },
12222 { bfd_mach_mips4111, bfd_mach_mips4100 },
12223
12224 /* MIPS III extensions. */
350cc38d
MS
12225 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12226 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
12227 { bfd_mach_mips8000, bfd_mach_mips4000 },
12228 { bfd_mach_mips4650, bfd_mach_mips4000 },
12229 { bfd_mach_mips4600, bfd_mach_mips4000 },
12230 { bfd_mach_mips4400, bfd_mach_mips4000 },
12231 { bfd_mach_mips4300, bfd_mach_mips4000 },
12232 { bfd_mach_mips4100, bfd_mach_mips4000 },
12233 { bfd_mach_mips4010, bfd_mach_mips4000 },
12234
12235 /* MIPS32 extensions. */
12236 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12237
12238 /* MIPS II extensions. */
12239 { bfd_mach_mips4000, bfd_mach_mips6000 },
12240 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12241
12242 /* MIPS I extensions. */
12243 { bfd_mach_mips6000, bfd_mach_mips3000 },
12244 { bfd_mach_mips3900, bfd_mach_mips3000 }
12245};
12246
12247
12248/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12249
12250static bfd_boolean
9719ad41 12251mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
12252{
12253 size_t i;
12254
c5211a54
RS
12255 if (extension == base)
12256 return TRUE;
12257
12258 if (base == bfd_mach_mipsisa32
12259 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12260 return TRUE;
12261
12262 if (base == bfd_mach_mipsisa32r2
12263 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12264 return TRUE;
12265
12266 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 12267 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
12268 {
12269 extension = mips_mach_extensions[i].base;
12270 if (extension == base)
12271 return TRUE;
12272 }
64543e1a 12273
c5211a54 12274 return FALSE;
64543e1a
RS
12275}
12276
12277
12278/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 12279
b34976b6 12280static bfd_boolean
9719ad41 12281mips_32bit_flags_p (flagword flags)
00707a0e 12282{
64543e1a
RS
12283 return ((flags & EF_MIPS_32BITMODE) != 0
12284 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12285 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12286 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12287 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12288 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12289 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
12290}
12291
64543e1a 12292
2cf19d5c
JM
12293/* Merge object attributes from IBFD into OBFD. Raise an error if
12294 there are conflicting attributes. */
12295static bfd_boolean
12296mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12297{
12298 obj_attribute *in_attr;
12299 obj_attribute *out_attr;
12300
12301 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12302 {
12303 /* This is the first object. Copy the attributes. */
12304 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12305
12306 /* Use the Tag_null value to indicate the attributes have been
12307 initialized. */
12308 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12309
12310 return TRUE;
12311 }
12312
12313 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12314 non-conflicting ones. */
12315 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12316 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12317 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12318 {
12319 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12320 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12321 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12322 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12323 ;
42554f6a 12324 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12325 _bfd_error_handler
12326 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12327 in_attr[Tag_GNU_MIPS_ABI_FP].i);
42554f6a 12328 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12329 _bfd_error_handler
12330 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12331 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12332 else
12333 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12334 {
12335 case 1:
12336 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12337 {
12338 case 2:
12339 _bfd_error_handler
12340 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12341 obfd, ibfd);
51a0dd31 12342 break;
2cf19d5c
JM
12343
12344 case 3:
12345 _bfd_error_handler
12346 (_("Warning: %B uses hard float, %B uses soft float"),
12347 obfd, ibfd);
12348 break;
12349
42554f6a
TS
12350 case 4:
12351 _bfd_error_handler
12352 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12353 obfd, ibfd);
12354 break;
12355
2cf19d5c
JM
12356 default:
12357 abort ();
12358 }
12359 break;
12360
12361 case 2:
12362 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12363 {
12364 case 1:
12365 _bfd_error_handler
12366 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12367 ibfd, obfd);
51a0dd31 12368 break;
2cf19d5c
JM
12369
12370 case 3:
12371 _bfd_error_handler
12372 (_("Warning: %B uses hard float, %B uses soft float"),
12373 obfd, ibfd);
12374 break;
12375
42554f6a
TS
12376 case 4:
12377 _bfd_error_handler
12378 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12379 obfd, ibfd);
12380 break;
12381
2cf19d5c
JM
12382 default:
12383 abort ();
12384 }
12385 break;
12386
12387 case 3:
12388 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12389 {
12390 case 1:
12391 case 2:
42554f6a 12392 case 4:
2cf19d5c
JM
12393 _bfd_error_handler
12394 (_("Warning: %B uses hard float, %B uses soft float"),
12395 ibfd, obfd);
12396 break;
12397
12398 default:
12399 abort ();
12400 }
12401 break;
12402
42554f6a
TS
12403 case 4:
12404 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12405 {
12406 case 1:
12407 _bfd_error_handler
12408 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12409 ibfd, obfd);
12410 break;
12411
12412 case 2:
12413 _bfd_error_handler
12414 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12415 ibfd, obfd);
12416 break;
12417
12418 case 3:
12419 _bfd_error_handler
12420 (_("Warning: %B uses hard float, %B uses soft float"),
12421 obfd, ibfd);
12422 break;
12423
12424 default:
12425 abort ();
12426 }
12427 break;
12428
2cf19d5c
JM
12429 default:
12430 abort ();
12431 }
12432 }
12433
12434 /* Merge Tag_compatibility attributes and any common GNU ones. */
12435 _bfd_elf_merge_object_attributes (ibfd, obfd);
12436
12437 return TRUE;
12438}
12439
b49e97c9
TS
12440/* Merge backend specific data from an object file to the output
12441 object file when linking. */
12442
b34976b6 12443bfd_boolean
9719ad41 12444_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
12445{
12446 flagword old_flags;
12447 flagword new_flags;
b34976b6
AM
12448 bfd_boolean ok;
12449 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
12450 asection *sec;
12451
12452 /* Check if we have the same endianess */
82e51918 12453 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
12454 {
12455 (*_bfd_error_handler)
d003868e
AM
12456 (_("%B: endianness incompatible with that of the selected emulation"),
12457 ibfd);
aa701218
AO
12458 return FALSE;
12459 }
b49e97c9 12460
d5eaccd7 12461 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 12462 return TRUE;
b49e97c9 12463
aa701218
AO
12464 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12465 {
12466 (*_bfd_error_handler)
d003868e
AM
12467 (_("%B: ABI is incompatible with that of the selected emulation"),
12468 ibfd);
aa701218
AO
12469 return FALSE;
12470 }
12471
2cf19d5c
JM
12472 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12473 return FALSE;
12474
b49e97c9
TS
12475 new_flags = elf_elfheader (ibfd)->e_flags;
12476 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12477 old_flags = elf_elfheader (obfd)->e_flags;
12478
12479 if (! elf_flags_init (obfd))
12480 {
b34976b6 12481 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
12482 elf_elfheader (obfd)->e_flags = new_flags;
12483 elf_elfheader (obfd)->e_ident[EI_CLASS]
12484 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12485
12486 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861
TS
12487 && (bfd_get_arch_info (obfd)->the_default
12488 || mips_mach_extends_p (bfd_get_mach (obfd),
12489 bfd_get_mach (ibfd))))
b49e97c9
TS
12490 {
12491 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12492 bfd_get_mach (ibfd)))
b34976b6 12493 return FALSE;
b49e97c9
TS
12494 }
12495
b34976b6 12496 return TRUE;
b49e97c9
TS
12497 }
12498
12499 /* Check flag compatibility. */
12500
12501 new_flags &= ~EF_MIPS_NOREORDER;
12502 old_flags &= ~EF_MIPS_NOREORDER;
12503
f4416af6
AO
12504 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12505 doesn't seem to matter. */
12506 new_flags &= ~EF_MIPS_XGOT;
12507 old_flags &= ~EF_MIPS_XGOT;
12508
98a8deaf
RS
12509 /* MIPSpro generates ucode info in n64 objects. Again, we should
12510 just be able to ignore this. */
12511 new_flags &= ~EF_MIPS_UCODE;
12512 old_flags &= ~EF_MIPS_UCODE;
12513
861fb55a
DJ
12514 /* DSOs should only be linked with CPIC code. */
12515 if ((ibfd->flags & DYNAMIC) != 0)
12516 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 12517
b49e97c9 12518 if (new_flags == old_flags)
b34976b6 12519 return TRUE;
b49e97c9
TS
12520
12521 /* Check to see if the input BFD actually contains any sections.
12522 If not, its flags may not have been initialised either, but it cannot
12523 actually cause any incompatibility. */
12524 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12525 {
12526 /* Ignore synthetic sections and empty .text, .data and .bss sections
12527 which are automatically generated by gas. */
12528 if (strcmp (sec->name, ".reginfo")
12529 && strcmp (sec->name, ".mdebug")
eea6121a 12530 && (sec->size != 0
d13d89fa
NS
12531 || (strcmp (sec->name, ".text")
12532 && strcmp (sec->name, ".data")
12533 && strcmp (sec->name, ".bss"))))
b49e97c9 12534 {
b34976b6 12535 null_input_bfd = FALSE;
b49e97c9
TS
12536 break;
12537 }
12538 }
12539 if (null_input_bfd)
b34976b6 12540 return TRUE;
b49e97c9 12541
b34976b6 12542 ok = TRUE;
b49e97c9 12543
143d77c5
EC
12544 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12545 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 12546 {
b49e97c9 12547 (*_bfd_error_handler)
861fb55a 12548 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 12549 ibfd);
143d77c5 12550 ok = TRUE;
b49e97c9
TS
12551 }
12552
143d77c5
EC
12553 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12554 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12555 if (! (new_flags & EF_MIPS_PIC))
12556 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12557
12558 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12559 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 12560
64543e1a
RS
12561 /* Compare the ISAs. */
12562 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 12563 {
64543e1a 12564 (*_bfd_error_handler)
d003868e
AM
12565 (_("%B: linking 32-bit code with 64-bit code"),
12566 ibfd);
64543e1a
RS
12567 ok = FALSE;
12568 }
12569 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12570 {
12571 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12572 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 12573 {
64543e1a
RS
12574 /* Copy the architecture info from IBFD to OBFD. Also copy
12575 the 32-bit flag (if set) so that we continue to recognise
12576 OBFD as a 32-bit binary. */
12577 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12578 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12579 elf_elfheader (obfd)->e_flags
12580 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12581
12582 /* Copy across the ABI flags if OBFD doesn't use them
12583 and if that was what caused us to treat IBFD as 32-bit. */
12584 if ((old_flags & EF_MIPS_ABI) == 0
12585 && mips_32bit_flags_p (new_flags)
12586 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12587 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
12588 }
12589 else
12590 {
64543e1a 12591 /* The ISAs aren't compatible. */
b49e97c9 12592 (*_bfd_error_handler)
d003868e
AM
12593 (_("%B: linking %s module with previous %s modules"),
12594 ibfd,
64543e1a
RS
12595 bfd_printable_name (ibfd),
12596 bfd_printable_name (obfd));
b34976b6 12597 ok = FALSE;
b49e97c9 12598 }
b49e97c9
TS
12599 }
12600
64543e1a
RS
12601 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12602 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12603
12604 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
12605 does set EI_CLASS differently from any 32-bit ABI. */
12606 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12607 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12608 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12609 {
12610 /* Only error if both are set (to different values). */
12611 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12612 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12613 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12614 {
12615 (*_bfd_error_handler)
d003868e
AM
12616 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12617 ibfd,
b49e97c9
TS
12618 elf_mips_abi_name (ibfd),
12619 elf_mips_abi_name (obfd));
b34976b6 12620 ok = FALSE;
b49e97c9
TS
12621 }
12622 new_flags &= ~EF_MIPS_ABI;
12623 old_flags &= ~EF_MIPS_ABI;
12624 }
12625
fb39dac1
RS
12626 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12627 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12628 {
12629 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12630
12631 new_flags &= ~ EF_MIPS_ARCH_ASE;
12632 old_flags &= ~ EF_MIPS_ARCH_ASE;
12633 }
12634
b49e97c9
TS
12635 /* Warn about any other mismatches */
12636 if (new_flags != old_flags)
12637 {
12638 (*_bfd_error_handler)
d003868e
AM
12639 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12640 ibfd, (unsigned long) new_flags,
b49e97c9 12641 (unsigned long) old_flags);
b34976b6 12642 ok = FALSE;
b49e97c9
TS
12643 }
12644
12645 if (! ok)
12646 {
12647 bfd_set_error (bfd_error_bad_value);
b34976b6 12648 return FALSE;
b49e97c9
TS
12649 }
12650
b34976b6 12651 return TRUE;
b49e97c9
TS
12652}
12653
12654/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12655
b34976b6 12656bfd_boolean
9719ad41 12657_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
12658{
12659 BFD_ASSERT (!elf_flags_init (abfd)
12660 || elf_elfheader (abfd)->e_flags == flags);
12661
12662 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
12663 elf_flags_init (abfd) = TRUE;
12664 return TRUE;
b49e97c9
TS
12665}
12666
ad9563d6
CM
12667char *
12668_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12669{
12670 switch (dtag)
12671 {
12672 default: return "";
12673 case DT_MIPS_RLD_VERSION:
12674 return "MIPS_RLD_VERSION";
12675 case DT_MIPS_TIME_STAMP:
12676 return "MIPS_TIME_STAMP";
12677 case DT_MIPS_ICHECKSUM:
12678 return "MIPS_ICHECKSUM";
12679 case DT_MIPS_IVERSION:
12680 return "MIPS_IVERSION";
12681 case DT_MIPS_FLAGS:
12682 return "MIPS_FLAGS";
12683 case DT_MIPS_BASE_ADDRESS:
12684 return "MIPS_BASE_ADDRESS";
12685 case DT_MIPS_MSYM:
12686 return "MIPS_MSYM";
12687 case DT_MIPS_CONFLICT:
12688 return "MIPS_CONFLICT";
12689 case DT_MIPS_LIBLIST:
12690 return "MIPS_LIBLIST";
12691 case DT_MIPS_LOCAL_GOTNO:
12692 return "MIPS_LOCAL_GOTNO";
12693 case DT_MIPS_CONFLICTNO:
12694 return "MIPS_CONFLICTNO";
12695 case DT_MIPS_LIBLISTNO:
12696 return "MIPS_LIBLISTNO";
12697 case DT_MIPS_SYMTABNO:
12698 return "MIPS_SYMTABNO";
12699 case DT_MIPS_UNREFEXTNO:
12700 return "MIPS_UNREFEXTNO";
12701 case DT_MIPS_GOTSYM:
12702 return "MIPS_GOTSYM";
12703 case DT_MIPS_HIPAGENO:
12704 return "MIPS_HIPAGENO";
12705 case DT_MIPS_RLD_MAP:
12706 return "MIPS_RLD_MAP";
12707 case DT_MIPS_DELTA_CLASS:
12708 return "MIPS_DELTA_CLASS";
12709 case DT_MIPS_DELTA_CLASS_NO:
12710 return "MIPS_DELTA_CLASS_NO";
12711 case DT_MIPS_DELTA_INSTANCE:
12712 return "MIPS_DELTA_INSTANCE";
12713 case DT_MIPS_DELTA_INSTANCE_NO:
12714 return "MIPS_DELTA_INSTANCE_NO";
12715 case DT_MIPS_DELTA_RELOC:
12716 return "MIPS_DELTA_RELOC";
12717 case DT_MIPS_DELTA_RELOC_NO:
12718 return "MIPS_DELTA_RELOC_NO";
12719 case DT_MIPS_DELTA_SYM:
12720 return "MIPS_DELTA_SYM";
12721 case DT_MIPS_DELTA_SYM_NO:
12722 return "MIPS_DELTA_SYM_NO";
12723 case DT_MIPS_DELTA_CLASSSYM:
12724 return "MIPS_DELTA_CLASSSYM";
12725 case DT_MIPS_DELTA_CLASSSYM_NO:
12726 return "MIPS_DELTA_CLASSSYM_NO";
12727 case DT_MIPS_CXX_FLAGS:
12728 return "MIPS_CXX_FLAGS";
12729 case DT_MIPS_PIXIE_INIT:
12730 return "MIPS_PIXIE_INIT";
12731 case DT_MIPS_SYMBOL_LIB:
12732 return "MIPS_SYMBOL_LIB";
12733 case DT_MIPS_LOCALPAGE_GOTIDX:
12734 return "MIPS_LOCALPAGE_GOTIDX";
12735 case DT_MIPS_LOCAL_GOTIDX:
12736 return "MIPS_LOCAL_GOTIDX";
12737 case DT_MIPS_HIDDEN_GOTIDX:
12738 return "MIPS_HIDDEN_GOTIDX";
12739 case DT_MIPS_PROTECTED_GOTIDX:
12740 return "MIPS_PROTECTED_GOT_IDX";
12741 case DT_MIPS_OPTIONS:
12742 return "MIPS_OPTIONS";
12743 case DT_MIPS_INTERFACE:
12744 return "MIPS_INTERFACE";
12745 case DT_MIPS_DYNSTR_ALIGN:
12746 return "DT_MIPS_DYNSTR_ALIGN";
12747 case DT_MIPS_INTERFACE_SIZE:
12748 return "DT_MIPS_INTERFACE_SIZE";
12749 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12750 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12751 case DT_MIPS_PERF_SUFFIX:
12752 return "DT_MIPS_PERF_SUFFIX";
12753 case DT_MIPS_COMPACT_SIZE:
12754 return "DT_MIPS_COMPACT_SIZE";
12755 case DT_MIPS_GP_VALUE:
12756 return "DT_MIPS_GP_VALUE";
12757 case DT_MIPS_AUX_DYNAMIC:
12758 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
12759 case DT_MIPS_PLTGOT:
12760 return "DT_MIPS_PLTGOT";
12761 case DT_MIPS_RWPLT:
12762 return "DT_MIPS_RWPLT";
ad9563d6
CM
12763 }
12764}
12765
b34976b6 12766bfd_boolean
9719ad41 12767_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 12768{
9719ad41 12769 FILE *file = ptr;
b49e97c9
TS
12770
12771 BFD_ASSERT (abfd != NULL && ptr != NULL);
12772
12773 /* Print normal ELF private data. */
12774 _bfd_elf_print_private_bfd_data (abfd, ptr);
12775
12776 /* xgettext:c-format */
12777 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12778
12779 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12780 fprintf (file, _(" [abi=O32]"));
12781 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12782 fprintf (file, _(" [abi=O64]"));
12783 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12784 fprintf (file, _(" [abi=EABI32]"));
12785 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12786 fprintf (file, _(" [abi=EABI64]"));
12787 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12788 fprintf (file, _(" [abi unknown]"));
12789 else if (ABI_N32_P (abfd))
12790 fprintf (file, _(" [abi=N32]"));
12791 else if (ABI_64_P (abfd))
12792 fprintf (file, _(" [abi=64]"));
12793 else
12794 fprintf (file, _(" [no abi set]"));
12795
12796 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 12797 fprintf (file, " [mips1]");
b49e97c9 12798 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 12799 fprintf (file, " [mips2]");
b49e97c9 12800 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 12801 fprintf (file, " [mips3]");
b49e97c9 12802 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 12803 fprintf (file, " [mips4]");
b49e97c9 12804 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 12805 fprintf (file, " [mips5]");
b49e97c9 12806 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 12807 fprintf (file, " [mips32]");
b49e97c9 12808 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 12809 fprintf (file, " [mips64]");
af7ee8bf 12810 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 12811 fprintf (file, " [mips32r2]");
5f74bc13 12812 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 12813 fprintf (file, " [mips64r2]");
b49e97c9
TS
12814 else
12815 fprintf (file, _(" [unknown ISA]"));
12816
40d32fc6 12817 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 12818 fprintf (file, " [mdmx]");
40d32fc6
CD
12819
12820 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 12821 fprintf (file, " [mips16]");
40d32fc6 12822
b49e97c9 12823 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 12824 fprintf (file, " [32bitmode]");
b49e97c9
TS
12825 else
12826 fprintf (file, _(" [not 32bitmode]"));
12827
c0e3f241 12828 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 12829 fprintf (file, " [noreorder]");
c0e3f241
CD
12830
12831 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 12832 fprintf (file, " [PIC]");
c0e3f241
CD
12833
12834 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 12835 fprintf (file, " [CPIC]");
c0e3f241
CD
12836
12837 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 12838 fprintf (file, " [XGOT]");
c0e3f241
CD
12839
12840 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 12841 fprintf (file, " [UCODE]");
c0e3f241 12842
b49e97c9
TS
12843 fputc ('\n', file);
12844
b34976b6 12845 return TRUE;
b49e97c9 12846}
2f89ff8d 12847
b35d266b 12848const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 12849{
0112cd26
NC
12850 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12851 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12852 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12853 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12854 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12855 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12856 { NULL, 0, 0, 0, 0 }
2f89ff8d 12857};
5e2b0d47 12858
8992f0d7
TS
12859/* Merge non visibility st_other attributes. Ensure that the
12860 STO_OPTIONAL flag is copied into h->other, even if this is not a
12861 definiton of the symbol. */
5e2b0d47
NC
12862void
12863_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12864 const Elf_Internal_Sym *isym,
12865 bfd_boolean definition,
12866 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12867{
8992f0d7
TS
12868 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12869 {
12870 unsigned char other;
12871
12872 other = (definition ? isym->st_other : h->other);
12873 other &= ~ELF_ST_VISIBILITY (-1);
12874 h->other = other | ELF_ST_VISIBILITY (h->other);
12875 }
12876
12877 if (!definition
5e2b0d47
NC
12878 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12879 h->other |= STO_OPTIONAL;
12880}
12ac1cf5
NC
12881
12882/* Decide whether an undefined symbol is special and can be ignored.
12883 This is the case for OPTIONAL symbols on IRIX. */
12884bfd_boolean
12885_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12886{
12887 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12888}
e0764319
NC
12889
12890bfd_boolean
12891_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12892{
12893 return (sym->st_shndx == SHN_COMMON
12894 || sym->st_shndx == SHN_MIPS_ACOMMON
12895 || sym->st_shndx == SHN_MIPS_SCOMMON);
12896}
861fb55a
DJ
12897
12898/* Return address for Ith PLT stub in section PLT, for relocation REL
12899 or (bfd_vma) -1 if it should not be included. */
12900
12901bfd_vma
12902_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12903 const arelent *rel ATTRIBUTE_UNUSED)
12904{
12905 return (plt->vma
12906 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12907 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12908}
12909
12910void
12911_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12912{
12913 struct mips_elf_link_hash_table *htab;
12914 Elf_Internal_Ehdr *i_ehdrp;
12915
12916 i_ehdrp = elf_elfheader (abfd);
12917 if (link_info)
12918 {
12919 htab = mips_elf_hash_table (link_info);
12920 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12921 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
12922 }
12923}
This page took 1.28152 seconds and 4 git commands to generate.