* ld/testsuite/ld-arm/group-relocs.s: Mark code sections as executable.
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
64543e1a 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4dfe6ac6 3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
b49e97c9
TS
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
ae9a127f 12 This file is part of BFD, the Binary File Descriptor library.
b49e97c9 13
ae9a127f
NC
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
cd123cb7 16 the Free Software Foundation; either version 3 of the License, or
ae9a127f 17 (at your option) any later version.
b49e97c9 18
ae9a127f
NC
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
b49e97c9 23
ae9a127f
NC
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
cd123cb7
NC
26 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
27 MA 02110-1301, USA. */
28
b49e97c9
TS
29
30/* This file handles functionality common to the different MIPS ABI's. */
31
b49e97c9 32#include "sysdep.h"
3db64b00 33#include "bfd.h"
b49e97c9 34#include "libbfd.h"
64543e1a 35#include "libiberty.h"
b49e97c9
TS
36#include "elf-bfd.h"
37#include "elfxx-mips.h"
38#include "elf/mips.h"
0a44bf69 39#include "elf-vxworks.h"
b49e97c9
TS
40
41/* Get the ECOFF swapping routines. */
42#include "coff/sym.h"
43#include "coff/symconst.h"
44#include "coff/ecoff.h"
45#include "coff/mips.h"
46
b15e6682
AO
47#include "hashtab.h"
48
ead49a57
RS
49/* This structure is used to hold information about one GOT entry.
50 There are three types of entry:
51
52 (1) absolute addresses
53 (abfd == NULL)
54 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
55 (abfd != NULL, symndx >= 0)
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 \
4dfe6ac6 250 && elf_object_id (bfd) == MIPS_ELF_DATA)
d5eaccd7 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
4dfe6ac6
NC
488/* Get the MIPS ELF linker hash table from a link_info structure. */
489
490#define mips_elf_hash_table(p) \
491 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
492 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
493
861fb55a 494/* A structure used to communicate with htab_traverse callbacks. */
4dfe6ac6
NC
495struct mips_htab_traverse_info
496{
861fb55a
DJ
497 /* The usual link-wide information. */
498 struct bfd_link_info *info;
499 bfd *output_bfd;
500
501 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
502 bfd_boolean error;
b49e97c9
TS
503};
504
0f20cc35
DJ
505#define TLS_RELOC_P(r_type) \
506 (r_type == R_MIPS_TLS_DTPMOD32 \
507 || r_type == R_MIPS_TLS_DTPMOD64 \
508 || r_type == R_MIPS_TLS_DTPREL32 \
509 || r_type == R_MIPS_TLS_DTPREL64 \
510 || r_type == R_MIPS_TLS_GD \
511 || r_type == R_MIPS_TLS_LDM \
512 || r_type == R_MIPS_TLS_DTPREL_HI16 \
513 || r_type == R_MIPS_TLS_DTPREL_LO16 \
514 || r_type == R_MIPS_TLS_GOTTPREL \
515 || r_type == R_MIPS_TLS_TPREL32 \
516 || r_type == R_MIPS_TLS_TPREL64 \
517 || r_type == R_MIPS_TLS_TPREL_HI16 \
518 || r_type == R_MIPS_TLS_TPREL_LO16)
519
b49e97c9
TS
520/* Structure used to pass information to mips_elf_output_extsym. */
521
522struct extsym_info
523{
9e4aeb93
RS
524 bfd *abfd;
525 struct bfd_link_info *info;
b49e97c9
TS
526 struct ecoff_debug_info *debug;
527 const struct ecoff_debug_swap *swap;
b34976b6 528 bfd_boolean failed;
b49e97c9
TS
529};
530
8dc1a139 531/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
532
533static const char * const mips_elf_dynsym_rtproc_names[] =
534{
535 "_procedure_table",
536 "_procedure_string_table",
537 "_procedure_table_size",
538 NULL
539};
540
541/* These structures are used to generate the .compact_rel section on
8dc1a139 542 IRIX5. */
b49e97c9
TS
543
544typedef struct
545{
546 unsigned long id1; /* Always one? */
547 unsigned long num; /* Number of compact relocation entries. */
548 unsigned long id2; /* Always two? */
549 unsigned long offset; /* The file offset of the first relocation. */
550 unsigned long reserved0; /* Zero? */
551 unsigned long reserved1; /* Zero? */
552} Elf32_compact_rel;
553
554typedef struct
555{
556 bfd_byte id1[4];
557 bfd_byte num[4];
558 bfd_byte id2[4];
559 bfd_byte offset[4];
560 bfd_byte reserved0[4];
561 bfd_byte reserved1[4];
562} Elf32_External_compact_rel;
563
564typedef struct
565{
566 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
567 unsigned int rtype : 4; /* Relocation types. See below. */
568 unsigned int dist2to : 8;
569 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
570 unsigned long konst; /* KONST field. See below. */
571 unsigned long vaddr; /* VADDR to be relocated. */
572} Elf32_crinfo;
573
574typedef struct
575{
576 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
577 unsigned int rtype : 4; /* Relocation types. See below. */
578 unsigned int dist2to : 8;
579 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
580 unsigned long konst; /* KONST field. See below. */
581} Elf32_crinfo2;
582
583typedef struct
584{
585 bfd_byte info[4];
586 bfd_byte konst[4];
587 bfd_byte vaddr[4];
588} Elf32_External_crinfo;
589
590typedef struct
591{
592 bfd_byte info[4];
593 bfd_byte konst[4];
594} Elf32_External_crinfo2;
595
596/* These are the constants used to swap the bitfields in a crinfo. */
597
598#define CRINFO_CTYPE (0x1)
599#define CRINFO_CTYPE_SH (31)
600#define CRINFO_RTYPE (0xf)
601#define CRINFO_RTYPE_SH (27)
602#define CRINFO_DIST2TO (0xff)
603#define CRINFO_DIST2TO_SH (19)
604#define CRINFO_RELVADDR (0x7ffff)
605#define CRINFO_RELVADDR_SH (0)
606
607/* A compact relocation info has long (3 words) or short (2 words)
608 formats. A short format doesn't have VADDR field and relvaddr
609 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
610#define CRF_MIPS_LONG 1
611#define CRF_MIPS_SHORT 0
612
613/* There are 4 types of compact relocation at least. The value KONST
614 has different meaning for each type:
615
616 (type) (konst)
617 CT_MIPS_REL32 Address in data
618 CT_MIPS_WORD Address in word (XXX)
619 CT_MIPS_GPHI_LO GP - vaddr
620 CT_MIPS_JMPAD Address to jump
621 */
622
623#define CRT_MIPS_REL32 0xa
624#define CRT_MIPS_WORD 0xb
625#define CRT_MIPS_GPHI_LO 0xc
626#define CRT_MIPS_JMPAD 0xd
627
628#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
629#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
630#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
631#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
632\f
633/* The structure of the runtime procedure descriptor created by the
634 loader for use by the static exception system. */
635
636typedef struct runtime_pdr {
ae9a127f
NC
637 bfd_vma adr; /* Memory address of start of procedure. */
638 long regmask; /* Save register mask. */
639 long regoffset; /* Save register offset. */
640 long fregmask; /* Save floating point register mask. */
641 long fregoffset; /* Save floating point register offset. */
642 long frameoffset; /* Frame size. */
643 short framereg; /* Frame pointer register. */
644 short pcreg; /* Offset or reg of return pc. */
645 long irpss; /* Index into the runtime string table. */
b49e97c9 646 long reserved;
ae9a127f 647 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
648} RPDR, *pRPDR;
649#define cbRPDR sizeof (RPDR)
650#define rpdNil ((pRPDR) 0)
651\f
b15e6682 652static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
653 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
654 struct mips_elf_link_hash_entry *, int);
b34976b6 655static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 656 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
657static bfd_vma mips_elf_high
658 (bfd_vma);
b34976b6 659static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
660 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
661 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
662 bfd_vma *, asection *);
9719ad41
RS
663static hashval_t mips_elf_got_entry_hash
664 (const void *);
f4416af6 665static bfd_vma mips_elf_adjust_gp
9719ad41 666 (bfd *, struct mips_got_info *, bfd *);
f4416af6 667static struct mips_got_info *mips_elf_got_for_ibfd
9719ad41 668 (struct mips_got_info *, bfd *);
f4416af6 669
b49e97c9
TS
670/* This will be used when we sort the dynamic relocation records. */
671static bfd *reldyn_sorting_bfd;
672
6d30f5b2
NC
673/* True if ABFD is for CPUs with load interlocking that include
674 non-MIPS1 CPUs and R3900. */
675#define LOAD_INTERLOCKS_P(abfd) \
676 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
677 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
678
cd8d5a82
CF
679/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
680 This should be safe for all architectures. We enable this predicate
681 for RM9000 for now. */
682#define JAL_TO_BAL_P(abfd) \
683 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
684
685/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
686 This should be safe for all architectures. We enable this predicate for
687 all CPUs. */
688#define JALR_TO_BAL_P(abfd) 1
689
38a7df63
CF
690/* True if ABFD is for CPUs that are faster if JR is converted to B.
691 This should be safe for all architectures. We enable this predicate for
692 all CPUs. */
693#define JR_TO_B_P(abfd) 1
694
861fb55a
DJ
695/* True if ABFD is a PIC object. */
696#define PIC_OBJECT_P(abfd) \
697 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
698
b49e97c9 699/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
700#define ABI_N32_P(abfd) \
701 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
702
4a14403c 703/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 704#define ABI_64_P(abfd) \
141ff970 705 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 706
4a14403c
TS
707/* Nonzero if ABFD is using NewABI conventions. */
708#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
709
710/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
711#define IRIX_COMPAT(abfd) \
712 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
713
b49e97c9
TS
714/* Whether we are trying to be compatible with IRIX at all. */
715#define SGI_COMPAT(abfd) \
716 (IRIX_COMPAT (abfd) != ict_none)
717
718/* The name of the options section. */
719#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 720 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 721
cc2e31b9
RS
722/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
723 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
724#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
725 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
726
943284cc
DJ
727/* Whether the section is readonly. */
728#define MIPS_ELF_READONLY_SECTION(sec) \
729 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
730 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
731
b49e97c9 732/* The name of the stub section. */
ca07892d 733#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
734
735/* The size of an external REL relocation. */
736#define MIPS_ELF_REL_SIZE(abfd) \
737 (get_elf_backend_data (abfd)->s->sizeof_rel)
738
0a44bf69
RS
739/* The size of an external RELA relocation. */
740#define MIPS_ELF_RELA_SIZE(abfd) \
741 (get_elf_backend_data (abfd)->s->sizeof_rela)
742
b49e97c9
TS
743/* The size of an external dynamic table entry. */
744#define MIPS_ELF_DYN_SIZE(abfd) \
745 (get_elf_backend_data (abfd)->s->sizeof_dyn)
746
747/* The size of a GOT entry. */
748#define MIPS_ELF_GOT_SIZE(abfd) \
749 (get_elf_backend_data (abfd)->s->arch_size / 8)
750
751/* The size of a symbol-table entry. */
752#define MIPS_ELF_SYM_SIZE(abfd) \
753 (get_elf_backend_data (abfd)->s->sizeof_sym)
754
755/* The default alignment for sections, as a power of two. */
756#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 757 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
758
759/* Get word-sized data. */
760#define MIPS_ELF_GET_WORD(abfd, ptr) \
761 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
762
763/* Put out word-sized data. */
764#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
765 (ABI_64_P (abfd) \
766 ? bfd_put_64 (abfd, val, ptr) \
767 : bfd_put_32 (abfd, val, ptr))
768
861fb55a
DJ
769/* The opcode for word-sized loads (LW or LD). */
770#define MIPS_ELF_LOAD_WORD(abfd) \
771 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
772
b49e97c9 773/* Add a dynamic symbol table-entry. */
9719ad41 774#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 775 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
776
777#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
778 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
779
4ffba85c
AO
780/* Determine whether the internal relocation of index REL_IDX is REL
781 (zero) or RELA (non-zero). The assumption is that, if there are
782 two relocation sections for this section, one of them is REL and
783 the other is RELA. If the index of the relocation we're testing is
784 in range for the first relocation section, check that the external
785 relocation size is that for RELA. It is also assumed that, if
786 rel_idx is not in range for the first section, and this first
787 section contains REL relocs, then the relocation is in the second
788 section, that is RELA. */
789#define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
790 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
791 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
792 > (bfd_vma)(rel_idx)) \
793 == (elf_section_data (sec)->rel_hdr.sh_entsize \
794 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
795 : sizeof (Elf32_External_Rela))))
796
0a44bf69
RS
797/* The name of the dynamic relocation section. */
798#define MIPS_ELF_REL_DYN_NAME(INFO) \
799 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
800
b49e97c9
TS
801/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
802 from smaller values. Start with zero, widen, *then* decrement. */
803#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 804#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 805
51e38d68
RS
806/* The value to write into got[1] for SVR4 targets, to identify it is
807 a GNU object. The dynamic linker can then use got[1] to store the
808 module pointer. */
809#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
810 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
811
f4416af6 812/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
813#define ELF_MIPS_GP_OFFSET(INFO) \
814 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
815
816/* The maximum size of the GOT for it to be addressable using 16-bit
817 offsets from $gp. */
0a44bf69 818#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 819
6a691779 820/* Instructions which appear in a stub. */
3d6746ca
DD
821#define STUB_LW(abfd) \
822 ((ABI_64_P (abfd) \
823 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
824 : 0x8f998010)) /* lw t9,0x8010(gp) */
825#define STUB_MOVE(abfd) \
826 ((ABI_64_P (abfd) \
827 ? 0x03e0782d /* daddu t7,ra */ \
828 : 0x03e07821)) /* addu t7,ra */
829#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
830#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
831#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
832#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
833#define STUB_LI16S(abfd, VAL) \
834 ((ABI_64_P (abfd) \
835 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
836 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
837
5108fc1b
RS
838#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
839#define MIPS_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
840
841/* The name of the dynamic interpreter. This is put in the .interp
842 section. */
843
844#define ELF_DYNAMIC_INTERPRETER(abfd) \
845 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
846 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
847 : "/usr/lib/libc.so.1")
848
849#ifdef BFD64
ee6423ed
AO
850#define MNAME(bfd,pre,pos) \
851 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
852#define ELF_R_SYM(bfd, i) \
853 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
854#define ELF_R_TYPE(bfd, i) \
855 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
856#define ELF_R_INFO(bfd, s, t) \
857 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
858#else
ee6423ed 859#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
860#define ELF_R_SYM(bfd, i) \
861 (ELF32_R_SYM (i))
862#define ELF_R_TYPE(bfd, i) \
863 (ELF32_R_TYPE (i))
864#define ELF_R_INFO(bfd, s, t) \
865 (ELF32_R_INFO (s, t))
866#endif
867\f
868 /* The mips16 compiler uses a couple of special sections to handle
869 floating point arguments.
870
871 Section names that look like .mips16.fn.FNNAME contain stubs that
872 copy floating point arguments from the fp regs to the gp regs and
873 then jump to FNNAME. If any 32 bit function calls FNNAME, the
874 call should be redirected to the stub instead. If no 32 bit
875 function calls FNNAME, the stub should be discarded. We need to
876 consider any reference to the function, not just a call, because
877 if the address of the function is taken we will need the stub,
878 since the address might be passed to a 32 bit function.
879
880 Section names that look like .mips16.call.FNNAME contain stubs
881 that copy floating point arguments from the gp regs to the fp
882 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
883 then any 16 bit function that calls FNNAME should be redirected
884 to the stub instead. If FNNAME is not a 32 bit function, the
885 stub should be discarded.
886
887 .mips16.call.fp.FNNAME sections are similar, but contain stubs
888 which call FNNAME and then copy the return value from the fp regs
889 to the gp regs. These stubs store the return value in $18 while
890 calling FNNAME; any function which might call one of these stubs
891 must arrange to save $18 around the call. (This case is not
892 needed for 32 bit functions that call 16 bit functions, because
893 16 bit functions always return floating point values in both
894 $f0/$f1 and $2/$3.)
895
896 Note that in all cases FNNAME might be defined statically.
897 Therefore, FNNAME is not used literally. Instead, the relocation
898 information will indicate which symbol the section is for.
899
900 We record any stubs that we find in the symbol table. */
901
902#define FN_STUB ".mips16.fn."
903#define CALL_STUB ".mips16.call."
904#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
905
906#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
907#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
908#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 909\f
861fb55a 910/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
911static const bfd_vma mips_o32_exec_plt0_entry[] =
912{
861fb55a
DJ
913 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
914 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
915 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
916 0x031cc023, /* subu $24, $24, $28 */
917 0x03e07821, /* move $15, $31 */
918 0x0018c082, /* srl $24, $24, 2 */
919 0x0320f809, /* jalr $25 */
920 0x2718fffe /* subu $24, $24, 2 */
921};
922
923/* The format of the first PLT entry in an N32 executable. Different
924 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
925static const bfd_vma mips_n32_exec_plt0_entry[] =
926{
861fb55a
DJ
927 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
928 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
929 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
930 0x030ec023, /* subu $24, $24, $14 */
931 0x03e07821, /* move $15, $31 */
932 0x0018c082, /* srl $24, $24, 2 */
933 0x0320f809, /* jalr $25 */
934 0x2718fffe /* subu $24, $24, 2 */
935};
936
937/* The format of the first PLT entry in an N64 executable. Different
938 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
939static const bfd_vma mips_n64_exec_plt0_entry[] =
940{
861fb55a
DJ
941 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
942 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
943 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
944 0x030ec023, /* subu $24, $24, $14 */
945 0x03e07821, /* move $15, $31 */
946 0x0018c0c2, /* srl $24, $24, 3 */
947 0x0320f809, /* jalr $25 */
948 0x2718fffe /* subu $24, $24, 2 */
949};
950
951/* The format of subsequent PLT entries. */
6d30f5b2
NC
952static const bfd_vma mips_exec_plt_entry[] =
953{
861fb55a
DJ
954 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
955 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
956 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
957 0x03200008 /* jr $25 */
958};
959
0a44bf69 960/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
961static const bfd_vma mips_vxworks_exec_plt0_entry[] =
962{
0a44bf69
RS
963 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
964 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
965 0x8f390008, /* lw t9, 8(t9) */
966 0x00000000, /* nop */
967 0x03200008, /* jr t9 */
968 0x00000000 /* nop */
969};
970
971/* The format of subsequent PLT entries. */
6d30f5b2
NC
972static const bfd_vma mips_vxworks_exec_plt_entry[] =
973{
0a44bf69
RS
974 0x10000000, /* b .PLT_resolver */
975 0x24180000, /* li t8, <pltindex> */
976 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
977 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
978 0x8f390000, /* lw t9, 0(t9) */
979 0x00000000, /* nop */
980 0x03200008, /* jr t9 */
981 0x00000000 /* nop */
982};
983
984/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
985static const bfd_vma mips_vxworks_shared_plt0_entry[] =
986{
0a44bf69
RS
987 0x8f990008, /* lw t9, 8(gp) */
988 0x00000000, /* nop */
989 0x03200008, /* jr t9 */
990 0x00000000, /* nop */
991 0x00000000, /* nop */
992 0x00000000 /* nop */
993};
994
995/* The format of subsequent PLT entries. */
6d30f5b2
NC
996static const bfd_vma mips_vxworks_shared_plt_entry[] =
997{
0a44bf69
RS
998 0x10000000, /* b .PLT_resolver */
999 0x24180000 /* li t8, <pltindex> */
1000};
1001\f
b49e97c9
TS
1002/* Look up an entry in a MIPS ELF linker hash table. */
1003
1004#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1005 ((struct mips_elf_link_hash_entry *) \
1006 elf_link_hash_lookup (&(table)->root, (string), (create), \
1007 (copy), (follow)))
1008
1009/* Traverse a MIPS ELF linker hash table. */
1010
1011#define mips_elf_link_hash_traverse(table, func, info) \
1012 (elf_link_hash_traverse \
1013 (&(table)->root, \
9719ad41 1014 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1015 (info)))
1016
0f20cc35
DJ
1017/* Find the base offsets for thread-local storage in this object,
1018 for GD/LD and IE/LE respectively. */
1019
1020#define TP_OFFSET 0x7000
1021#define DTP_OFFSET 0x8000
1022
1023static bfd_vma
1024dtprel_base (struct bfd_link_info *info)
1025{
1026 /* If tls_sec is NULL, we should have signalled an error already. */
1027 if (elf_hash_table (info)->tls_sec == NULL)
1028 return 0;
1029 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1030}
1031
1032static bfd_vma
1033tprel_base (struct bfd_link_info *info)
1034{
1035 /* If tls_sec is NULL, we should have signalled an error already. */
1036 if (elf_hash_table (info)->tls_sec == NULL)
1037 return 0;
1038 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1039}
1040
b49e97c9
TS
1041/* Create an entry in a MIPS ELF linker hash table. */
1042
1043static struct bfd_hash_entry *
9719ad41
RS
1044mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1045 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1046{
1047 struct mips_elf_link_hash_entry *ret =
1048 (struct mips_elf_link_hash_entry *) entry;
1049
1050 /* Allocate the structure if it has not already been allocated by a
1051 subclass. */
9719ad41
RS
1052 if (ret == NULL)
1053 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1054 if (ret == NULL)
b49e97c9
TS
1055 return (struct bfd_hash_entry *) ret;
1056
1057 /* Call the allocation method of the superclass. */
1058 ret = ((struct mips_elf_link_hash_entry *)
1059 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1060 table, string));
9719ad41 1061 if (ret != NULL)
b49e97c9
TS
1062 {
1063 /* Set local fields. */
1064 memset (&ret->esym, 0, sizeof (EXTR));
1065 /* We use -2 as a marker to indicate that the information has
1066 not been set. -1 means there is no associated ifd. */
1067 ret->esym.ifd = -2;
861fb55a 1068 ret->la25_stub = 0;
b49e97c9 1069 ret->possibly_dynamic_relocs = 0;
b49e97c9 1070 ret->fn_stub = NULL;
b49e97c9
TS
1071 ret->call_stub = NULL;
1072 ret->call_fp_stub = NULL;
71782a75 1073 ret->tls_type = GOT_NORMAL;
634835ae 1074 ret->global_got_area = GGA_NONE;
71782a75 1075 ret->readonly_reloc = FALSE;
861fb55a 1076 ret->has_static_relocs = FALSE;
71782a75
RS
1077 ret->no_fn_stub = FALSE;
1078 ret->need_fn_stub = FALSE;
861fb55a 1079 ret->has_nonpic_branches = FALSE;
33bb52fb 1080 ret->needs_lazy_stub = FALSE;
b49e97c9
TS
1081 }
1082
1083 return (struct bfd_hash_entry *) ret;
1084}
f0abc2a1
AM
1085
1086bfd_boolean
9719ad41 1087_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1088{
f592407e
AM
1089 if (!sec->used_by_bfd)
1090 {
1091 struct _mips_elf_section_data *sdata;
1092 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1093
f592407e
AM
1094 sdata = bfd_zalloc (abfd, amt);
1095 if (sdata == NULL)
1096 return FALSE;
1097 sec->used_by_bfd = sdata;
1098 }
f0abc2a1
AM
1099
1100 return _bfd_elf_new_section_hook (abfd, sec);
1101}
b49e97c9
TS
1102\f
1103/* Read ECOFF debugging information from a .mdebug section into a
1104 ecoff_debug_info structure. */
1105
b34976b6 1106bfd_boolean
9719ad41
RS
1107_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1108 struct ecoff_debug_info *debug)
b49e97c9
TS
1109{
1110 HDRR *symhdr;
1111 const struct ecoff_debug_swap *swap;
9719ad41 1112 char *ext_hdr;
b49e97c9
TS
1113
1114 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1115 memset (debug, 0, sizeof (*debug));
1116
9719ad41 1117 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1118 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1119 goto error_return;
1120
9719ad41 1121 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1122 swap->external_hdr_size))
b49e97c9
TS
1123 goto error_return;
1124
1125 symhdr = &debug->symbolic_header;
1126 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1127
1128 /* The symbolic header contains absolute file offsets and sizes to
1129 read. */
1130#define READ(ptr, offset, count, size, type) \
1131 if (symhdr->count == 0) \
1132 debug->ptr = NULL; \
1133 else \
1134 { \
1135 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1136 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1137 if (debug->ptr == NULL) \
1138 goto error_return; \
9719ad41 1139 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1140 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1141 goto error_return; \
1142 }
1143
1144 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1145 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1146 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1147 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1148 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1149 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1150 union aux_ext *);
1151 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1152 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1153 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1154 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1155 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1156#undef READ
1157
1158 debug->fdr = NULL;
b49e97c9 1159
b34976b6 1160 return TRUE;
b49e97c9
TS
1161
1162 error_return:
1163 if (ext_hdr != NULL)
1164 free (ext_hdr);
1165 if (debug->line != NULL)
1166 free (debug->line);
1167 if (debug->external_dnr != NULL)
1168 free (debug->external_dnr);
1169 if (debug->external_pdr != NULL)
1170 free (debug->external_pdr);
1171 if (debug->external_sym != NULL)
1172 free (debug->external_sym);
1173 if (debug->external_opt != NULL)
1174 free (debug->external_opt);
1175 if (debug->external_aux != NULL)
1176 free (debug->external_aux);
1177 if (debug->ss != NULL)
1178 free (debug->ss);
1179 if (debug->ssext != NULL)
1180 free (debug->ssext);
1181 if (debug->external_fdr != NULL)
1182 free (debug->external_fdr);
1183 if (debug->external_rfd != NULL)
1184 free (debug->external_rfd);
1185 if (debug->external_ext != NULL)
1186 free (debug->external_ext);
b34976b6 1187 return FALSE;
b49e97c9
TS
1188}
1189\f
1190/* Swap RPDR (runtime procedure table entry) for output. */
1191
1192static void
9719ad41 1193ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1194{
1195 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1196 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1197 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1198 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1199 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1200 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1201
1202 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1203 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1204
1205 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1206}
1207
1208/* Create a runtime procedure table from the .mdebug section. */
1209
b34976b6 1210static bfd_boolean
9719ad41
RS
1211mips_elf_create_procedure_table (void *handle, bfd *abfd,
1212 struct bfd_link_info *info, asection *s,
1213 struct ecoff_debug_info *debug)
b49e97c9
TS
1214{
1215 const struct ecoff_debug_swap *swap;
1216 HDRR *hdr = &debug->symbolic_header;
1217 RPDR *rpdr, *rp;
1218 struct rpdr_ext *erp;
9719ad41 1219 void *rtproc;
b49e97c9
TS
1220 struct pdr_ext *epdr;
1221 struct sym_ext *esym;
1222 char *ss, **sv;
1223 char *str;
1224 bfd_size_type size;
1225 bfd_size_type count;
1226 unsigned long sindex;
1227 unsigned long i;
1228 PDR pdr;
1229 SYMR sym;
1230 const char *no_name_func = _("static procedure (no name)");
1231
1232 epdr = NULL;
1233 rpdr = NULL;
1234 esym = NULL;
1235 ss = NULL;
1236 sv = NULL;
1237
1238 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1239
1240 sindex = strlen (no_name_func) + 1;
1241 count = hdr->ipdMax;
1242 if (count > 0)
1243 {
1244 size = swap->external_pdr_size;
1245
9719ad41 1246 epdr = bfd_malloc (size * count);
b49e97c9
TS
1247 if (epdr == NULL)
1248 goto error_return;
1249
9719ad41 1250 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1251 goto error_return;
1252
1253 size = sizeof (RPDR);
9719ad41 1254 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1255 if (rpdr == NULL)
1256 goto error_return;
1257
1258 size = sizeof (char *);
9719ad41 1259 sv = bfd_malloc (size * count);
b49e97c9
TS
1260 if (sv == NULL)
1261 goto error_return;
1262
1263 count = hdr->isymMax;
1264 size = swap->external_sym_size;
9719ad41 1265 esym = bfd_malloc (size * count);
b49e97c9
TS
1266 if (esym == NULL)
1267 goto error_return;
1268
9719ad41 1269 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1270 goto error_return;
1271
1272 count = hdr->issMax;
9719ad41 1273 ss = bfd_malloc (count);
b49e97c9
TS
1274 if (ss == NULL)
1275 goto error_return;
f075ee0c 1276 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1277 goto error_return;
1278
1279 count = hdr->ipdMax;
1280 for (i = 0; i < (unsigned long) count; i++, rp++)
1281 {
9719ad41
RS
1282 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1283 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1284 rp->adr = sym.value;
1285 rp->regmask = pdr.regmask;
1286 rp->regoffset = pdr.regoffset;
1287 rp->fregmask = pdr.fregmask;
1288 rp->fregoffset = pdr.fregoffset;
1289 rp->frameoffset = pdr.frameoffset;
1290 rp->framereg = pdr.framereg;
1291 rp->pcreg = pdr.pcreg;
1292 rp->irpss = sindex;
1293 sv[i] = ss + sym.iss;
1294 sindex += strlen (sv[i]) + 1;
1295 }
1296 }
1297
1298 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1299 size = BFD_ALIGN (size, 16);
9719ad41 1300 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1301 if (rtproc == NULL)
1302 {
1303 mips_elf_hash_table (info)->procedure_count = 0;
1304 goto error_return;
1305 }
1306
1307 mips_elf_hash_table (info)->procedure_count = count + 2;
1308
9719ad41 1309 erp = rtproc;
b49e97c9
TS
1310 memset (erp, 0, sizeof (struct rpdr_ext));
1311 erp++;
1312 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1313 strcpy (str, no_name_func);
1314 str += strlen (no_name_func) + 1;
1315 for (i = 0; i < count; i++)
1316 {
1317 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1318 strcpy (str, sv[i]);
1319 str += strlen (sv[i]) + 1;
1320 }
1321 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1322
1323 /* Set the size and contents of .rtproc section. */
eea6121a 1324 s->size = size;
9719ad41 1325 s->contents = rtproc;
b49e97c9
TS
1326
1327 /* Skip this section later on (I don't think this currently
1328 matters, but someday it might). */
8423293d 1329 s->map_head.link_order = NULL;
b49e97c9
TS
1330
1331 if (epdr != NULL)
1332 free (epdr);
1333 if (rpdr != NULL)
1334 free (rpdr);
1335 if (esym != NULL)
1336 free (esym);
1337 if (ss != NULL)
1338 free (ss);
1339 if (sv != NULL)
1340 free (sv);
1341
b34976b6 1342 return TRUE;
b49e97c9
TS
1343
1344 error_return:
1345 if (epdr != NULL)
1346 free (epdr);
1347 if (rpdr != NULL)
1348 free (rpdr);
1349 if (esym != NULL)
1350 free (esym);
1351 if (ss != NULL)
1352 free (ss);
1353 if (sv != NULL)
1354 free (sv);
b34976b6 1355 return FALSE;
b49e97c9 1356}
738e5348 1357\f
861fb55a
DJ
1358/* We're going to create a stub for H. Create a symbol for the stub's
1359 value and size, to help make the disassembly easier to read. */
1360
1361static bfd_boolean
1362mips_elf_create_stub_symbol (struct bfd_link_info *info,
1363 struct mips_elf_link_hash_entry *h,
1364 const char *prefix, asection *s, bfd_vma value,
1365 bfd_vma size)
1366{
1367 struct bfd_link_hash_entry *bh;
1368 struct elf_link_hash_entry *elfh;
1369 const char *name;
1370
1371 /* Create a new symbol. */
1372 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1373 bh = NULL;
1374 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1375 BSF_LOCAL, s, value, NULL,
1376 TRUE, FALSE, &bh))
1377 return FALSE;
1378
1379 /* Make it a local function. */
1380 elfh = (struct elf_link_hash_entry *) bh;
1381 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1382 elfh->size = size;
1383 elfh->forced_local = 1;
1384 return TRUE;
1385}
1386
738e5348
RS
1387/* We're about to redefine H. Create a symbol to represent H's
1388 current value and size, to help make the disassembly easier
1389 to read. */
1390
1391static bfd_boolean
1392mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1393 struct mips_elf_link_hash_entry *h,
1394 const char *prefix)
1395{
1396 struct bfd_link_hash_entry *bh;
1397 struct elf_link_hash_entry *elfh;
1398 const char *name;
1399 asection *s;
1400 bfd_vma value;
1401
1402 /* Read the symbol's value. */
1403 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1404 || h->root.root.type == bfd_link_hash_defweak);
1405 s = h->root.root.u.def.section;
1406 value = h->root.root.u.def.value;
1407
1408 /* Create a new symbol. */
1409 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1410 bh = NULL;
1411 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1412 BSF_LOCAL, s, value, NULL,
1413 TRUE, FALSE, &bh))
1414 return FALSE;
1415
1416 /* Make it local and copy the other attributes from H. */
1417 elfh = (struct elf_link_hash_entry *) bh;
1418 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1419 elfh->other = h->root.other;
1420 elfh->size = h->root.size;
1421 elfh->forced_local = 1;
1422 return TRUE;
1423}
1424
1425/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1426 function rather than to a hard-float stub. */
1427
1428static bfd_boolean
1429section_allows_mips16_refs_p (asection *section)
1430{
1431 const char *name;
1432
1433 name = bfd_get_section_name (section->owner, section);
1434 return (FN_STUB_P (name)
1435 || CALL_STUB_P (name)
1436 || CALL_FP_STUB_P (name)
1437 || strcmp (name, ".pdr") == 0);
1438}
1439
1440/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1441 stub section of some kind. Return the R_SYMNDX of the target
1442 function, or 0 if we can't decide which function that is. */
1443
1444static unsigned long
502e814e
TT
1445mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1446 const Elf_Internal_Rela *relocs,
738e5348
RS
1447 const Elf_Internal_Rela *relend)
1448{
1449 const Elf_Internal_Rela *rel;
1450
1451 /* Trust the first R_MIPS_NONE relocation, if any. */
1452 for (rel = relocs; rel < relend; rel++)
1453 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1454 return ELF_R_SYM (sec->owner, rel->r_info);
1455
1456 /* Otherwise trust the first relocation, whatever its kind. This is
1457 the traditional behavior. */
1458 if (relocs < relend)
1459 return ELF_R_SYM (sec->owner, relocs->r_info);
1460
1461 return 0;
1462}
b49e97c9
TS
1463
1464/* Check the mips16 stubs for a particular symbol, and see if we can
1465 discard them. */
1466
861fb55a
DJ
1467static void
1468mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1469 struct mips_elf_link_hash_entry *h)
b49e97c9 1470{
738e5348
RS
1471 /* Dynamic symbols must use the standard call interface, in case other
1472 objects try to call them. */
1473 if (h->fn_stub != NULL
1474 && h->root.dynindx != -1)
1475 {
1476 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1477 h->need_fn_stub = TRUE;
1478 }
1479
b49e97c9
TS
1480 if (h->fn_stub != NULL
1481 && ! h->need_fn_stub)
1482 {
1483 /* We don't need the fn_stub; the only references to this symbol
1484 are 16 bit calls. Clobber the size to 0 to prevent it from
1485 being included in the link. */
eea6121a 1486 h->fn_stub->size = 0;
b49e97c9
TS
1487 h->fn_stub->flags &= ~SEC_RELOC;
1488 h->fn_stub->reloc_count = 0;
1489 h->fn_stub->flags |= SEC_EXCLUDE;
1490 }
1491
1492 if (h->call_stub != NULL
30c09090 1493 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1494 {
1495 /* We don't need the call_stub; this is a 16 bit function, so
1496 calls from other 16 bit functions are OK. Clobber the size
1497 to 0 to prevent it from being included in the link. */
eea6121a 1498 h->call_stub->size = 0;
b49e97c9
TS
1499 h->call_stub->flags &= ~SEC_RELOC;
1500 h->call_stub->reloc_count = 0;
1501 h->call_stub->flags |= SEC_EXCLUDE;
1502 }
1503
1504 if (h->call_fp_stub != NULL
30c09090 1505 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1506 {
1507 /* We don't need the call_stub; this is a 16 bit function, so
1508 calls from other 16 bit functions are OK. Clobber the size
1509 to 0 to prevent it from being included in the link. */
eea6121a 1510 h->call_fp_stub->size = 0;
b49e97c9
TS
1511 h->call_fp_stub->flags &= ~SEC_RELOC;
1512 h->call_fp_stub->reloc_count = 0;
1513 h->call_fp_stub->flags |= SEC_EXCLUDE;
1514 }
861fb55a
DJ
1515}
1516
1517/* Hashtable callbacks for mips_elf_la25_stubs. */
1518
1519static hashval_t
1520mips_elf_la25_stub_hash (const void *entry_)
1521{
1522 const struct mips_elf_la25_stub *entry;
1523
1524 entry = (struct mips_elf_la25_stub *) entry_;
1525 return entry->h->root.root.u.def.section->id
1526 + entry->h->root.root.u.def.value;
1527}
1528
1529static int
1530mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1531{
1532 const struct mips_elf_la25_stub *entry1, *entry2;
1533
1534 entry1 = (struct mips_elf_la25_stub *) entry1_;
1535 entry2 = (struct mips_elf_la25_stub *) entry2_;
1536 return ((entry1->h->root.root.u.def.section
1537 == entry2->h->root.root.u.def.section)
1538 && (entry1->h->root.root.u.def.value
1539 == entry2->h->root.root.u.def.value));
1540}
1541
1542/* Called by the linker to set up the la25 stub-creation code. FN is
1543 the linker's implementation of add_stub_function. Return true on
1544 success. */
1545
1546bfd_boolean
1547_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1548 asection *(*fn) (const char *, asection *,
1549 asection *))
1550{
1551 struct mips_elf_link_hash_table *htab;
1552
1553 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1554 if (htab == NULL)
1555 return FALSE;
1556
861fb55a
DJ
1557 htab->add_stub_section = fn;
1558 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1559 mips_elf_la25_stub_eq, NULL);
1560 if (htab->la25_stubs == NULL)
1561 return FALSE;
1562
1563 return TRUE;
1564}
1565
1566/* Return true if H is a locally-defined PIC function, in the sense
1567 that it might need $25 to be valid on entry. Note that MIPS16
1568 functions never need $25 to be valid on entry; they set up $gp
1569 using PC-relative instructions instead. */
1570
1571static bfd_boolean
1572mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1573{
1574 return ((h->root.root.type == bfd_link_hash_defined
1575 || h->root.root.type == bfd_link_hash_defweak)
1576 && h->root.def_regular
1577 && !bfd_is_abs_section (h->root.root.u.def.section)
1578 && !ELF_ST_IS_MIPS16 (h->root.other)
1579 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1580 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1581}
1582
1583/* STUB describes an la25 stub that we have decided to implement
1584 by inserting an LUI/ADDIU pair before the target function.
1585 Create the section and redirect the function symbol to it. */
1586
1587static bfd_boolean
1588mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1589 struct bfd_link_info *info)
1590{
1591 struct mips_elf_link_hash_table *htab;
1592 char *name;
1593 asection *s, *input_section;
1594 unsigned int align;
1595
1596 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1597 if (htab == NULL)
1598 return FALSE;
861fb55a
DJ
1599
1600 /* Create a unique name for the new section. */
1601 name = bfd_malloc (11 + sizeof (".text.stub."));
1602 if (name == NULL)
1603 return FALSE;
1604 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1605
1606 /* Create the section. */
1607 input_section = stub->h->root.root.u.def.section;
1608 s = htab->add_stub_section (name, input_section,
1609 input_section->output_section);
1610 if (s == NULL)
1611 return FALSE;
1612
1613 /* Make sure that any padding goes before the stub. */
1614 align = input_section->alignment_power;
1615 if (!bfd_set_section_alignment (s->owner, s, align))
1616 return FALSE;
1617 if (align > 3)
1618 s->size = (1 << align) - 8;
1619
1620 /* Create a symbol for the stub. */
1621 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1622 stub->stub_section = s;
1623 stub->offset = s->size;
1624
1625 /* Allocate room for it. */
1626 s->size += 8;
1627 return TRUE;
1628}
1629
1630/* STUB describes an la25 stub that we have decided to implement
1631 with a separate trampoline. Allocate room for it and redirect
1632 the function symbol to it. */
1633
1634static bfd_boolean
1635mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1636 struct bfd_link_info *info)
1637{
1638 struct mips_elf_link_hash_table *htab;
1639 asection *s;
1640
1641 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1642 if (htab == NULL)
1643 return FALSE;
861fb55a
DJ
1644
1645 /* Create a trampoline section, if we haven't already. */
1646 s = htab->strampoline;
1647 if (s == NULL)
1648 {
1649 asection *input_section = stub->h->root.root.u.def.section;
1650 s = htab->add_stub_section (".text", NULL,
1651 input_section->output_section);
1652 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1653 return FALSE;
1654 htab->strampoline = s;
1655 }
1656
1657 /* Create a symbol for the stub. */
1658 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1659 stub->stub_section = s;
1660 stub->offset = s->size;
1661
1662 /* Allocate room for it. */
1663 s->size += 16;
1664 return TRUE;
1665}
1666
1667/* H describes a symbol that needs an la25 stub. Make sure that an
1668 appropriate stub exists and point H at it. */
1669
1670static bfd_boolean
1671mips_elf_add_la25_stub (struct bfd_link_info *info,
1672 struct mips_elf_link_hash_entry *h)
1673{
1674 struct mips_elf_link_hash_table *htab;
1675 struct mips_elf_la25_stub search, *stub;
1676 bfd_boolean use_trampoline_p;
1677 asection *s;
1678 bfd_vma value;
1679 void **slot;
1680
1681 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1682 of the section and if we would need no more than 2 nops. */
1683 s = h->root.root.u.def.section;
1684 value = h->root.root.u.def.value;
1685 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1686
1687 /* Describe the stub we want. */
1688 search.stub_section = NULL;
1689 search.offset = 0;
1690 search.h = h;
1691
1692 /* See if we've already created an equivalent stub. */
1693 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1694 if (htab == NULL)
1695 return FALSE;
1696
861fb55a
DJ
1697 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1698 if (slot == NULL)
1699 return FALSE;
1700
1701 stub = (struct mips_elf_la25_stub *) *slot;
1702 if (stub != NULL)
1703 {
1704 /* We can reuse the existing stub. */
1705 h->la25_stub = stub;
1706 return TRUE;
1707 }
1708
1709 /* Create a permanent copy of ENTRY and add it to the hash table. */
1710 stub = bfd_malloc (sizeof (search));
1711 if (stub == NULL)
1712 return FALSE;
1713 *stub = search;
1714 *slot = stub;
1715
1716 h->la25_stub = stub;
1717 return (use_trampoline_p
1718 ? mips_elf_add_la25_trampoline (stub, info)
1719 : mips_elf_add_la25_intro (stub, info));
1720}
1721
1722/* A mips_elf_link_hash_traverse callback that is called before sizing
1723 sections. DATA points to a mips_htab_traverse_info structure. */
1724
1725static bfd_boolean
1726mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1727{
1728 struct mips_htab_traverse_info *hti;
1729
1730 hti = (struct mips_htab_traverse_info *) data;
1731 if (h->root.root.type == bfd_link_hash_warning)
1732 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1733
1734 if (!hti->info->relocatable)
1735 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1736
861fb55a
DJ
1737 if (mips_elf_local_pic_function_p (h))
1738 {
1739 /* H is a function that might need $25 to be valid on entry.
1740 If we're creating a non-PIC relocatable object, mark H as
1741 being PIC. If we're creating a non-relocatable object with
1742 non-PIC branches and jumps to H, make sure that H has an la25
1743 stub. */
1744 if (hti->info->relocatable)
1745 {
1746 if (!PIC_OBJECT_P (hti->output_bfd))
1747 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1748 }
1749 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1750 {
1751 hti->error = TRUE;
1752 return FALSE;
1753 }
1754 }
b34976b6 1755 return TRUE;
b49e97c9
TS
1756}
1757\f
d6f16593
MR
1758/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1759 Most mips16 instructions are 16 bits, but these instructions
1760 are 32 bits.
1761
1762 The format of these instructions is:
1763
1764 +--------------+--------------------------------+
1765 | JALX | X| Imm 20:16 | Imm 25:21 |
1766 +--------------+--------------------------------+
1767 | Immediate 15:0 |
1768 +-----------------------------------------------+
1769
1770 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1771 Note that the immediate value in the first word is swapped.
1772
1773 When producing a relocatable object file, R_MIPS16_26 is
1774 handled mostly like R_MIPS_26. In particular, the addend is
1775 stored as a straight 26-bit value in a 32-bit instruction.
1776 (gas makes life simpler for itself by never adjusting a
1777 R_MIPS16_26 reloc to be against a section, so the addend is
1778 always zero). However, the 32 bit instruction is stored as 2
1779 16-bit values, rather than a single 32-bit value. In a
1780 big-endian file, the result is the same; in a little-endian
1781 file, the two 16-bit halves of the 32 bit value are swapped.
1782 This is so that a disassembler can recognize the jal
1783 instruction.
1784
1785 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1786 instruction stored as two 16-bit values. The addend A is the
1787 contents of the targ26 field. The calculation is the same as
1788 R_MIPS_26. When storing the calculated value, reorder the
1789 immediate value as shown above, and don't forget to store the
1790 value as two 16-bit values.
1791
1792 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1793 defined as
1794
1795 big-endian:
1796 +--------+----------------------+
1797 | | |
1798 | | targ26-16 |
1799 |31 26|25 0|
1800 +--------+----------------------+
1801
1802 little-endian:
1803 +----------+------+-------------+
1804 | | | |
1805 | sub1 | | sub2 |
1806 |0 9|10 15|16 31|
1807 +----------+--------------------+
1808 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1809 ((sub1 << 16) | sub2)).
1810
1811 When producing a relocatable object file, the calculation is
1812 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1813 When producing a fully linked file, the calculation is
1814 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1815 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1816
738e5348
RS
1817 The table below lists the other MIPS16 instruction relocations.
1818 Each one is calculated in the same way as the non-MIPS16 relocation
1819 given on the right, but using the extended MIPS16 layout of 16-bit
1820 immediate fields:
1821
1822 R_MIPS16_GPREL R_MIPS_GPREL16
1823 R_MIPS16_GOT16 R_MIPS_GOT16
1824 R_MIPS16_CALL16 R_MIPS_CALL16
1825 R_MIPS16_HI16 R_MIPS_HI16
1826 R_MIPS16_LO16 R_MIPS_LO16
1827
1828 A typical instruction will have a format like this:
d6f16593
MR
1829
1830 +--------------+--------------------------------+
1831 | EXTEND | Imm 10:5 | Imm 15:11 |
1832 +--------------+--------------------------------+
1833 | Major | rx | ry | Imm 4:0 |
1834 +--------------+--------------------------------+
1835
1836 EXTEND is the five bit value 11110. Major is the instruction
1837 opcode.
1838
738e5348
RS
1839 All we need to do here is shuffle the bits appropriately.
1840 As above, the two 16-bit halves must be swapped on a
1841 little-endian system. */
1842
1843static inline bfd_boolean
1844mips16_reloc_p (int r_type)
1845{
1846 switch (r_type)
1847 {
1848 case R_MIPS16_26:
1849 case R_MIPS16_GPREL:
1850 case R_MIPS16_GOT16:
1851 case R_MIPS16_CALL16:
1852 case R_MIPS16_HI16:
1853 case R_MIPS16_LO16:
1854 return TRUE;
1855
1856 default:
1857 return FALSE;
1858 }
1859}
1860
1861static inline bfd_boolean
1862got16_reloc_p (int r_type)
1863{
1864 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1865}
1866
1867static inline bfd_boolean
1868call16_reloc_p (int r_type)
1869{
1870 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1871}
1872
1873static inline bfd_boolean
1874hi16_reloc_p (int r_type)
1875{
1876 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1877}
d6f16593 1878
738e5348
RS
1879static inline bfd_boolean
1880lo16_reloc_p (int r_type)
1881{
1882 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1883}
1884
1885static inline bfd_boolean
1886mips16_call_reloc_p (int r_type)
1887{
1888 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1889}
d6f16593 1890
38a7df63
CF
1891static inline bfd_boolean
1892jal_reloc_p (int r_type)
1893{
1894 return r_type == R_MIPS_26 || r_type == R_MIPS16_26;
1895}
1896
d6f16593
MR
1897void
1898_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1899 bfd_boolean jal_shuffle, bfd_byte *data)
1900{
1901 bfd_vma extend, insn, val;
1902
738e5348 1903 if (!mips16_reloc_p (r_type))
d6f16593
MR
1904 return;
1905
1906 /* Pick up the mips16 extend instruction and the real instruction. */
1907 extend = bfd_get_16 (abfd, data);
1908 insn = bfd_get_16 (abfd, data + 2);
1909 if (r_type == R_MIPS16_26)
1910 {
1911 if (jal_shuffle)
1912 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1913 | ((extend & 0x1f) << 21) | insn;
1914 else
1915 val = extend << 16 | insn;
1916 }
1917 else
1918 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1919 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1920 bfd_put_32 (abfd, val, data);
1921}
1922
1923void
1924_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1925 bfd_boolean jal_shuffle, bfd_byte *data)
1926{
1927 bfd_vma extend, insn, val;
1928
738e5348 1929 if (!mips16_reloc_p (r_type))
d6f16593
MR
1930 return;
1931
1932 val = bfd_get_32 (abfd, data);
1933 if (r_type == R_MIPS16_26)
1934 {
1935 if (jal_shuffle)
1936 {
1937 insn = val & 0xffff;
1938 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1939 | ((val >> 21) & 0x1f);
1940 }
1941 else
1942 {
1943 insn = val & 0xffff;
1944 extend = val >> 16;
1945 }
1946 }
1947 else
1948 {
1949 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1950 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1951 }
1952 bfd_put_16 (abfd, insn, data + 2);
1953 bfd_put_16 (abfd, extend, data);
1954}
1955
b49e97c9 1956bfd_reloc_status_type
9719ad41
RS
1957_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1958 arelent *reloc_entry, asection *input_section,
1959 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
1960{
1961 bfd_vma relocation;
a7ebbfdf 1962 bfd_signed_vma val;
30ac9238 1963 bfd_reloc_status_type status;
b49e97c9
TS
1964
1965 if (bfd_is_com_section (symbol->section))
1966 relocation = 0;
1967 else
1968 relocation = symbol->value;
1969
1970 relocation += symbol->section->output_section->vma;
1971 relocation += symbol->section->output_offset;
1972
07515404 1973 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
1974 return bfd_reloc_outofrange;
1975
b49e97c9 1976 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
1977 val = reloc_entry->addend;
1978
30ac9238 1979 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 1980
b49e97c9 1981 /* Adjust val for the final section location and GP value. If we
1049f94e 1982 are producing relocatable output, we don't want to do this for
b49e97c9 1983 an external symbol. */
1049f94e 1984 if (! relocatable
b49e97c9
TS
1985 || (symbol->flags & BSF_SECTION_SYM) != 0)
1986 val += relocation - gp;
1987
a7ebbfdf
TS
1988 if (reloc_entry->howto->partial_inplace)
1989 {
30ac9238
RS
1990 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1991 (bfd_byte *) data
1992 + reloc_entry->address);
1993 if (status != bfd_reloc_ok)
1994 return status;
a7ebbfdf
TS
1995 }
1996 else
1997 reloc_entry->addend = val;
b49e97c9 1998
1049f94e 1999 if (relocatable)
b49e97c9 2000 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2001
2002 return bfd_reloc_ok;
2003}
2004
2005/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2006 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2007 that contains the relocation field and DATA points to the start of
2008 INPUT_SECTION. */
2009
2010struct mips_hi16
2011{
2012 struct mips_hi16 *next;
2013 bfd_byte *data;
2014 asection *input_section;
2015 arelent rel;
2016};
2017
2018/* FIXME: This should not be a static variable. */
2019
2020static struct mips_hi16 *mips_hi16_list;
2021
2022/* A howto special_function for REL *HI16 relocations. We can only
2023 calculate the correct value once we've seen the partnering
2024 *LO16 relocation, so just save the information for later.
2025
2026 The ABI requires that the *LO16 immediately follow the *HI16.
2027 However, as a GNU extension, we permit an arbitrary number of
2028 *HI16s to be associated with a single *LO16. This significantly
2029 simplies the relocation handling in gcc. */
2030
2031bfd_reloc_status_type
2032_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2033 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2034 asection *input_section, bfd *output_bfd,
2035 char **error_message ATTRIBUTE_UNUSED)
2036{
2037 struct mips_hi16 *n;
2038
07515404 2039 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2040 return bfd_reloc_outofrange;
2041
2042 n = bfd_malloc (sizeof *n);
2043 if (n == NULL)
2044 return bfd_reloc_outofrange;
2045
2046 n->next = mips_hi16_list;
2047 n->data = data;
2048 n->input_section = input_section;
2049 n->rel = *reloc_entry;
2050 mips_hi16_list = n;
2051
2052 if (output_bfd != NULL)
2053 reloc_entry->address += input_section->output_offset;
2054
2055 return bfd_reloc_ok;
2056}
2057
738e5348 2058/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2059 like any other 16-bit relocation when applied to global symbols, but is
2060 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2061
2062bfd_reloc_status_type
2063_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2064 void *data, asection *input_section,
2065 bfd *output_bfd, char **error_message)
2066{
2067 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2068 || bfd_is_und_section (bfd_get_section (symbol))
2069 || bfd_is_com_section (bfd_get_section (symbol)))
2070 /* The relocation is against a global symbol. */
2071 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2072 input_section, output_bfd,
2073 error_message);
2074
2075 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2076 input_section, output_bfd, error_message);
2077}
2078
2079/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2080 is a straightforward 16 bit inplace relocation, but we must deal with
2081 any partnering high-part relocations as well. */
2082
2083bfd_reloc_status_type
2084_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2085 void *data, asection *input_section,
2086 bfd *output_bfd, char **error_message)
2087{
2088 bfd_vma vallo;
d6f16593 2089 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2090
07515404 2091 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2092 return bfd_reloc_outofrange;
2093
d6f16593
MR
2094 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2095 location);
2096 vallo = bfd_get_32 (abfd, location);
2097 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2098 location);
2099
30ac9238
RS
2100 while (mips_hi16_list != NULL)
2101 {
2102 bfd_reloc_status_type ret;
2103 struct mips_hi16 *hi;
2104
2105 hi = mips_hi16_list;
2106
738e5348
RS
2107 /* R_MIPS*_GOT16 relocations are something of a special case. We
2108 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2109 relocation (with a rightshift of 16). However, since GOT16
2110 relocations can also be used with global symbols, their howto
2111 has a rightshift of 0. */
2112 if (hi->rel.howto->type == R_MIPS_GOT16)
2113 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2114 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2115 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
30ac9238
RS
2116
2117 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2118 carry or borrow will induce a change of +1 or -1 in the high part. */
2119 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2120
30ac9238
RS
2121 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2122 hi->input_section, output_bfd,
2123 error_message);
2124 if (ret != bfd_reloc_ok)
2125 return ret;
2126
2127 mips_hi16_list = hi->next;
2128 free (hi);
2129 }
2130
2131 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2132 input_section, output_bfd,
2133 error_message);
2134}
2135
2136/* A generic howto special_function. This calculates and installs the
2137 relocation itself, thus avoiding the oft-discussed problems in
2138 bfd_perform_relocation and bfd_install_relocation. */
2139
2140bfd_reloc_status_type
2141_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2142 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2143 asection *input_section, bfd *output_bfd,
2144 char **error_message ATTRIBUTE_UNUSED)
2145{
2146 bfd_signed_vma val;
2147 bfd_reloc_status_type status;
2148 bfd_boolean relocatable;
2149
2150 relocatable = (output_bfd != NULL);
2151
07515404 2152 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2153 return bfd_reloc_outofrange;
2154
2155 /* Build up the field adjustment in VAL. */
2156 val = 0;
2157 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2158 {
2159 /* Either we're calculating the final field value or we have a
2160 relocation against a section symbol. Add in the section's
2161 offset or address. */
2162 val += symbol->section->output_section->vma;
2163 val += symbol->section->output_offset;
2164 }
2165
2166 if (!relocatable)
2167 {
2168 /* We're calculating the final field value. Add in the symbol's value
2169 and, if pc-relative, subtract the address of the field itself. */
2170 val += symbol->value;
2171 if (reloc_entry->howto->pc_relative)
2172 {
2173 val -= input_section->output_section->vma;
2174 val -= input_section->output_offset;
2175 val -= reloc_entry->address;
2176 }
2177 }
2178
2179 /* VAL is now the final adjustment. If we're keeping this relocation
2180 in the output file, and if the relocation uses a separate addend,
2181 we just need to add VAL to that addend. Otherwise we need to add
2182 VAL to the relocation field itself. */
2183 if (relocatable && !reloc_entry->howto->partial_inplace)
2184 reloc_entry->addend += val;
2185 else
2186 {
d6f16593
MR
2187 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2188
30ac9238
RS
2189 /* Add in the separate addend, if any. */
2190 val += reloc_entry->addend;
2191
2192 /* Add VAL to the relocation field. */
d6f16593
MR
2193 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2194 location);
30ac9238 2195 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593
MR
2196 location);
2197 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2198 location);
2199
30ac9238
RS
2200 if (status != bfd_reloc_ok)
2201 return status;
2202 }
2203
2204 if (relocatable)
2205 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2206
2207 return bfd_reloc_ok;
2208}
2209\f
2210/* Swap an entry in a .gptab section. Note that these routines rely
2211 on the equivalence of the two elements of the union. */
2212
2213static void
9719ad41
RS
2214bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2215 Elf32_gptab *in)
b49e97c9
TS
2216{
2217 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2218 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2219}
2220
2221static void
9719ad41
RS
2222bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2223 Elf32_External_gptab *ex)
b49e97c9
TS
2224{
2225 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2226 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2227}
2228
2229static void
9719ad41
RS
2230bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2231 Elf32_External_compact_rel *ex)
b49e97c9
TS
2232{
2233 H_PUT_32 (abfd, in->id1, ex->id1);
2234 H_PUT_32 (abfd, in->num, ex->num);
2235 H_PUT_32 (abfd, in->id2, ex->id2);
2236 H_PUT_32 (abfd, in->offset, ex->offset);
2237 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2238 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2239}
2240
2241static void
9719ad41
RS
2242bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2243 Elf32_External_crinfo *ex)
b49e97c9
TS
2244{
2245 unsigned long l;
2246
2247 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2248 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2249 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2250 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2251 H_PUT_32 (abfd, l, ex->info);
2252 H_PUT_32 (abfd, in->konst, ex->konst);
2253 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2254}
b49e97c9
TS
2255\f
2256/* A .reginfo section holds a single Elf32_RegInfo structure. These
2257 routines swap this structure in and out. They are used outside of
2258 BFD, so they are globally visible. */
2259
2260void
9719ad41
RS
2261bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2262 Elf32_RegInfo *in)
b49e97c9
TS
2263{
2264 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2265 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2266 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2267 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2268 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2269 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2270}
2271
2272void
9719ad41
RS
2273bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2274 Elf32_External_RegInfo *ex)
b49e97c9
TS
2275{
2276 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2277 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2278 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2279 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2280 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2281 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2282}
2283
2284/* In the 64 bit ABI, the .MIPS.options section holds register
2285 information in an Elf64_Reginfo structure. These routines swap
2286 them in and out. They are globally visible because they are used
2287 outside of BFD. These routines are here so that gas can call them
2288 without worrying about whether the 64 bit ABI has been included. */
2289
2290void
9719ad41
RS
2291bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2292 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2293{
2294 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2295 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2296 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2297 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2298 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2299 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2300 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2301}
2302
2303void
9719ad41
RS
2304bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2305 Elf64_External_RegInfo *ex)
b49e97c9
TS
2306{
2307 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2308 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2309 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2310 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2311 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2312 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2313 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2314}
2315
2316/* Swap in an options header. */
2317
2318void
9719ad41
RS
2319bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2320 Elf_Internal_Options *in)
b49e97c9
TS
2321{
2322 in->kind = H_GET_8 (abfd, ex->kind);
2323 in->size = H_GET_8 (abfd, ex->size);
2324 in->section = H_GET_16 (abfd, ex->section);
2325 in->info = H_GET_32 (abfd, ex->info);
2326}
2327
2328/* Swap out an options header. */
2329
2330void
9719ad41
RS
2331bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2332 Elf_External_Options *ex)
b49e97c9
TS
2333{
2334 H_PUT_8 (abfd, in->kind, ex->kind);
2335 H_PUT_8 (abfd, in->size, ex->size);
2336 H_PUT_16 (abfd, in->section, ex->section);
2337 H_PUT_32 (abfd, in->info, ex->info);
2338}
2339\f
2340/* This function is called via qsort() to sort the dynamic relocation
2341 entries by increasing r_symndx value. */
2342
2343static int
9719ad41 2344sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2345{
947216bf
AM
2346 Elf_Internal_Rela int_reloc1;
2347 Elf_Internal_Rela int_reloc2;
6870500c 2348 int diff;
b49e97c9 2349
947216bf
AM
2350 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2351 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2352
6870500c
RS
2353 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2354 if (diff != 0)
2355 return diff;
2356
2357 if (int_reloc1.r_offset < int_reloc2.r_offset)
2358 return -1;
2359 if (int_reloc1.r_offset > int_reloc2.r_offset)
2360 return 1;
2361 return 0;
b49e97c9
TS
2362}
2363
f4416af6
AO
2364/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2365
2366static int
7e3102a7
AM
2367sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2368 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2369{
7e3102a7 2370#ifdef BFD64
f4416af6
AO
2371 Elf_Internal_Rela int_reloc1[3];
2372 Elf_Internal_Rela int_reloc2[3];
2373
2374 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2375 (reldyn_sorting_bfd, arg1, int_reloc1);
2376 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2377 (reldyn_sorting_bfd, arg2, int_reloc2);
2378
6870500c
RS
2379 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2380 return -1;
2381 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2382 return 1;
2383
2384 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2385 return -1;
2386 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2387 return 1;
2388 return 0;
7e3102a7
AM
2389#else
2390 abort ();
2391#endif
f4416af6
AO
2392}
2393
2394
b49e97c9
TS
2395/* This routine is used to write out ECOFF debugging external symbol
2396 information. It is called via mips_elf_link_hash_traverse. The
2397 ECOFF external symbol information must match the ELF external
2398 symbol information. Unfortunately, at this point we don't know
2399 whether a symbol is required by reloc information, so the two
2400 tables may wind up being different. We must sort out the external
2401 symbol information before we can set the final size of the .mdebug
2402 section, and we must set the size of the .mdebug section before we
2403 can relocate any sections, and we can't know which symbols are
2404 required by relocation until we relocate the sections.
2405 Fortunately, it is relatively unlikely that any symbol will be
2406 stripped but required by a reloc. In particular, it can not happen
2407 when generating a final executable. */
2408
b34976b6 2409static bfd_boolean
9719ad41 2410mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2411{
9719ad41 2412 struct extsym_info *einfo = data;
b34976b6 2413 bfd_boolean strip;
b49e97c9
TS
2414 asection *sec, *output_section;
2415
2416 if (h->root.root.type == bfd_link_hash_warning)
2417 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2418
2419 if (h->root.indx == -2)
b34976b6 2420 strip = FALSE;
f5385ebf 2421 else if ((h->root.def_dynamic
77cfaee6
AM
2422 || h->root.ref_dynamic
2423 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2424 && !h->root.def_regular
2425 && !h->root.ref_regular)
b34976b6 2426 strip = TRUE;
b49e97c9
TS
2427 else if (einfo->info->strip == strip_all
2428 || (einfo->info->strip == strip_some
2429 && bfd_hash_lookup (einfo->info->keep_hash,
2430 h->root.root.root.string,
b34976b6
AM
2431 FALSE, FALSE) == NULL))
2432 strip = TRUE;
b49e97c9 2433 else
b34976b6 2434 strip = FALSE;
b49e97c9
TS
2435
2436 if (strip)
b34976b6 2437 return TRUE;
b49e97c9
TS
2438
2439 if (h->esym.ifd == -2)
2440 {
2441 h->esym.jmptbl = 0;
2442 h->esym.cobol_main = 0;
2443 h->esym.weakext = 0;
2444 h->esym.reserved = 0;
2445 h->esym.ifd = ifdNil;
2446 h->esym.asym.value = 0;
2447 h->esym.asym.st = stGlobal;
2448
2449 if (h->root.root.type == bfd_link_hash_undefined
2450 || h->root.root.type == bfd_link_hash_undefweak)
2451 {
2452 const char *name;
2453
2454 /* Use undefined class. Also, set class and type for some
2455 special symbols. */
2456 name = h->root.root.root.string;
2457 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2458 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2459 {
2460 h->esym.asym.sc = scData;
2461 h->esym.asym.st = stLabel;
2462 h->esym.asym.value = 0;
2463 }
2464 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2465 {
2466 h->esym.asym.sc = scAbs;
2467 h->esym.asym.st = stLabel;
2468 h->esym.asym.value =
2469 mips_elf_hash_table (einfo->info)->procedure_count;
2470 }
4a14403c 2471 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2472 {
2473 h->esym.asym.sc = scAbs;
2474 h->esym.asym.st = stLabel;
2475 h->esym.asym.value = elf_gp (einfo->abfd);
2476 }
2477 else
2478 h->esym.asym.sc = scUndefined;
2479 }
2480 else if (h->root.root.type != bfd_link_hash_defined
2481 && h->root.root.type != bfd_link_hash_defweak)
2482 h->esym.asym.sc = scAbs;
2483 else
2484 {
2485 const char *name;
2486
2487 sec = h->root.root.u.def.section;
2488 output_section = sec->output_section;
2489
2490 /* When making a shared library and symbol h is the one from
2491 the another shared library, OUTPUT_SECTION may be null. */
2492 if (output_section == NULL)
2493 h->esym.asym.sc = scUndefined;
2494 else
2495 {
2496 name = bfd_section_name (output_section->owner, output_section);
2497
2498 if (strcmp (name, ".text") == 0)
2499 h->esym.asym.sc = scText;
2500 else if (strcmp (name, ".data") == 0)
2501 h->esym.asym.sc = scData;
2502 else if (strcmp (name, ".sdata") == 0)
2503 h->esym.asym.sc = scSData;
2504 else if (strcmp (name, ".rodata") == 0
2505 || strcmp (name, ".rdata") == 0)
2506 h->esym.asym.sc = scRData;
2507 else if (strcmp (name, ".bss") == 0)
2508 h->esym.asym.sc = scBss;
2509 else if (strcmp (name, ".sbss") == 0)
2510 h->esym.asym.sc = scSBss;
2511 else if (strcmp (name, ".init") == 0)
2512 h->esym.asym.sc = scInit;
2513 else if (strcmp (name, ".fini") == 0)
2514 h->esym.asym.sc = scFini;
2515 else
2516 h->esym.asym.sc = scAbs;
2517 }
2518 }
2519
2520 h->esym.asym.reserved = 0;
2521 h->esym.asym.index = indexNil;
2522 }
2523
2524 if (h->root.root.type == bfd_link_hash_common)
2525 h->esym.asym.value = h->root.root.u.c.size;
2526 else if (h->root.root.type == bfd_link_hash_defined
2527 || h->root.root.type == bfd_link_hash_defweak)
2528 {
2529 if (h->esym.asym.sc == scCommon)
2530 h->esym.asym.sc = scBss;
2531 else if (h->esym.asym.sc == scSCommon)
2532 h->esym.asym.sc = scSBss;
2533
2534 sec = h->root.root.u.def.section;
2535 output_section = sec->output_section;
2536 if (output_section != NULL)
2537 h->esym.asym.value = (h->root.root.u.def.value
2538 + sec->output_offset
2539 + output_section->vma);
2540 else
2541 h->esym.asym.value = 0;
2542 }
33bb52fb 2543 else
b49e97c9
TS
2544 {
2545 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2546
2547 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2548 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2549
33bb52fb 2550 if (hd->needs_lazy_stub)
b49e97c9
TS
2551 {
2552 /* Set type and value for a symbol with a function stub. */
2553 h->esym.asym.st = stProc;
2554 sec = hd->root.root.u.def.section;
2555 if (sec == NULL)
2556 h->esym.asym.value = 0;
2557 else
2558 {
2559 output_section = sec->output_section;
2560 if (output_section != NULL)
2561 h->esym.asym.value = (hd->root.plt.offset
2562 + sec->output_offset
2563 + output_section->vma);
2564 else
2565 h->esym.asym.value = 0;
2566 }
b49e97c9
TS
2567 }
2568 }
2569
2570 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2571 h->root.root.root.string,
2572 &h->esym))
2573 {
b34976b6
AM
2574 einfo->failed = TRUE;
2575 return FALSE;
b49e97c9
TS
2576 }
2577
b34976b6 2578 return TRUE;
b49e97c9
TS
2579}
2580
2581/* A comparison routine used to sort .gptab entries. */
2582
2583static int
9719ad41 2584gptab_compare (const void *p1, const void *p2)
b49e97c9 2585{
9719ad41
RS
2586 const Elf32_gptab *a1 = p1;
2587 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2588
2589 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2590}
2591\f
b15e6682 2592/* Functions to manage the got entry hash table. */
f4416af6
AO
2593
2594/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2595 hash number. */
2596
2597static INLINE hashval_t
9719ad41 2598mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2599{
2600#ifdef BFD64
2601 return addr + (addr >> 32);
2602#else
2603 return addr;
2604#endif
2605}
2606
2607/* got_entries only match if they're identical, except for gotidx, so
2608 use all fields to compute the hash, and compare the appropriate
2609 union members. */
2610
b15e6682 2611static hashval_t
9719ad41 2612mips_elf_got_entry_hash (const void *entry_)
b15e6682
AO
2613{
2614 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2615
38985a1c 2616 return entry->symndx
0f20cc35 2617 + ((entry->tls_type & GOT_TLS_LDM) << 17)
f4416af6 2618 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
38985a1c
AO
2619 : entry->abfd->id
2620 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2621 : entry->d.h->root.root.root.hash));
b15e6682
AO
2622}
2623
2624static int
9719ad41 2625mips_elf_got_entry_eq (const void *entry1, const void *entry2)
b15e6682
AO
2626{
2627 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2628 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2629
0f20cc35
DJ
2630 /* An LDM entry can only match another LDM entry. */
2631 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2632 return 0;
2633
b15e6682 2634 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
f4416af6
AO
2635 && (! e1->abfd ? e1->d.address == e2->d.address
2636 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2637 : e1->d.h == e2->d.h);
2638}
2639
2640/* multi_got_entries are still a match in the case of global objects,
2641 even if the input bfd in which they're referenced differs, so the
2642 hash computation and compare functions are adjusted
2643 accordingly. */
2644
2645static hashval_t
9719ad41 2646mips_elf_multi_got_entry_hash (const void *entry_)
f4416af6
AO
2647{
2648 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2649
2650 return entry->symndx
2651 + (! entry->abfd
2652 ? mips_elf_hash_bfd_vma (entry->d.address)
2653 : entry->symndx >= 0
0f20cc35
DJ
2654 ? ((entry->tls_type & GOT_TLS_LDM)
2655 ? (GOT_TLS_LDM << 17)
2656 : (entry->abfd->id
2657 + mips_elf_hash_bfd_vma (entry->d.addend)))
f4416af6
AO
2658 : entry->d.h->root.root.root.hash);
2659}
2660
2661static int
9719ad41 2662mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2663{
2664 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2665 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2666
0f20cc35
DJ
2667 /* Any two LDM entries match. */
2668 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2669 return 1;
2670
2671 /* Nothing else matches an LDM entry. */
2672 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2673 return 0;
2674
f4416af6
AO
2675 return e1->symndx == e2->symndx
2676 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2677 : e1->abfd == NULL || e2->abfd == NULL
2678 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2679 : e1->d.h == e2->d.h);
b15e6682 2680}
c224138d
RS
2681
2682static hashval_t
2683mips_got_page_entry_hash (const void *entry_)
2684{
2685 const struct mips_got_page_entry *entry;
2686
2687 entry = (const struct mips_got_page_entry *) entry_;
2688 return entry->abfd->id + entry->symndx;
2689}
2690
2691static int
2692mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2693{
2694 const struct mips_got_page_entry *entry1, *entry2;
2695
2696 entry1 = (const struct mips_got_page_entry *) entry1_;
2697 entry2 = (const struct mips_got_page_entry *) entry2_;
2698 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2699}
b15e6682 2700\f
0a44bf69
RS
2701/* Return the dynamic relocation section. If it doesn't exist, try to
2702 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2703 if creation fails. */
f4416af6
AO
2704
2705static asection *
0a44bf69 2706mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 2707{
0a44bf69 2708 const char *dname;
f4416af6 2709 asection *sreloc;
0a44bf69 2710 bfd *dynobj;
f4416af6 2711
0a44bf69
RS
2712 dname = MIPS_ELF_REL_DYN_NAME (info);
2713 dynobj = elf_hash_table (info)->dynobj;
f4416af6
AO
2714 sreloc = bfd_get_section_by_name (dynobj, dname);
2715 if (sreloc == NULL && create_p)
2716 {
3496cb2a
L
2717 sreloc = bfd_make_section_with_flags (dynobj, dname,
2718 (SEC_ALLOC
2719 | SEC_LOAD
2720 | SEC_HAS_CONTENTS
2721 | SEC_IN_MEMORY
2722 | SEC_LINKER_CREATED
2723 | SEC_READONLY));
f4416af6 2724 if (sreloc == NULL
f4416af6 2725 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 2726 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
2727 return NULL;
2728 }
2729 return sreloc;
2730}
2731
0f20cc35
DJ
2732/* Count the number of relocations needed for a TLS GOT entry, with
2733 access types from TLS_TYPE, and symbol H (or a local symbol if H
2734 is NULL). */
2735
2736static int
2737mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2738 struct elf_link_hash_entry *h)
2739{
2740 int indx = 0;
2741 int ret = 0;
2742 bfd_boolean need_relocs = FALSE;
2743 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2744
2745 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2746 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2747 indx = h->dynindx;
2748
2749 if ((info->shared || indx != 0)
2750 && (h == NULL
2751 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2752 || h->root.type != bfd_link_hash_undefweak))
2753 need_relocs = TRUE;
2754
2755 if (!need_relocs)
2756 return FALSE;
2757
2758 if (tls_type & GOT_TLS_GD)
2759 {
2760 ret++;
2761 if (indx != 0)
2762 ret++;
2763 }
2764
2765 if (tls_type & GOT_TLS_IE)
2766 ret++;
2767
2768 if ((tls_type & GOT_TLS_LDM) && info->shared)
2769 ret++;
2770
2771 return ret;
2772}
2773
2774/* Count the number of TLS relocations required for the GOT entry in
2775 ARG1, if it describes a local symbol. */
2776
2777static int
2778mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2779{
2780 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2781 struct mips_elf_count_tls_arg *arg = arg2;
2782
2783 if (entry->abfd != NULL && entry->symndx != -1)
2784 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2785
2786 return 1;
2787}
2788
2789/* Count the number of TLS GOT entries required for the global (or
2790 forced-local) symbol in ARG1. */
2791
2792static int
2793mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2794{
2795 struct mips_elf_link_hash_entry *hm
2796 = (struct mips_elf_link_hash_entry *) arg1;
2797 struct mips_elf_count_tls_arg *arg = arg2;
2798
2799 if (hm->tls_type & GOT_TLS_GD)
2800 arg->needed += 2;
2801 if (hm->tls_type & GOT_TLS_IE)
2802 arg->needed += 1;
2803
2804 return 1;
2805}
2806
2807/* Count the number of TLS relocations required for the global (or
2808 forced-local) symbol in ARG1. */
2809
2810static int
2811mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2812{
2813 struct mips_elf_link_hash_entry *hm
2814 = (struct mips_elf_link_hash_entry *) arg1;
2815 struct mips_elf_count_tls_arg *arg = arg2;
2816
2817 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2818
2819 return 1;
2820}
2821
2822/* Output a simple dynamic relocation into SRELOC. */
2823
2824static void
2825mips_elf_output_dynamic_relocation (bfd *output_bfd,
2826 asection *sreloc,
861fb55a 2827 unsigned long reloc_index,
0f20cc35
DJ
2828 unsigned long indx,
2829 int r_type,
2830 bfd_vma offset)
2831{
2832 Elf_Internal_Rela rel[3];
2833
2834 memset (rel, 0, sizeof (rel));
2835
2836 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2837 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2838
2839 if (ABI_64_P (output_bfd))
2840 {
2841 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2842 (output_bfd, &rel[0],
2843 (sreloc->contents
861fb55a 2844 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
2845 }
2846 else
2847 bfd_elf32_swap_reloc_out
2848 (output_bfd, &rel[0],
2849 (sreloc->contents
861fb55a 2850 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
2851}
2852
2853/* Initialize a set of TLS GOT entries for one symbol. */
2854
2855static void
2856mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2857 unsigned char *tls_type_p,
2858 struct bfd_link_info *info,
2859 struct mips_elf_link_hash_entry *h,
2860 bfd_vma value)
2861{
23cc69b6 2862 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
2863 int indx;
2864 asection *sreloc, *sgot;
2865 bfd_vma offset, offset2;
0f20cc35
DJ
2866 bfd_boolean need_relocs = FALSE;
2867
23cc69b6 2868 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
2869 if (htab == NULL)
2870 return;
2871
23cc69b6 2872 sgot = htab->sgot;
0f20cc35
DJ
2873
2874 indx = 0;
2875 if (h != NULL)
2876 {
2877 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2878
2879 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2880 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2881 indx = h->root.dynindx;
2882 }
2883
2884 if (*tls_type_p & GOT_TLS_DONE)
2885 return;
2886
2887 if ((info->shared || indx != 0)
2888 && (h == NULL
2889 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2890 || h->root.type != bfd_link_hash_undefweak))
2891 need_relocs = TRUE;
2892
2893 /* MINUS_ONE means the symbol is not defined in this object. It may not
2894 be defined at all; assume that the value doesn't matter in that
2895 case. Otherwise complain if we would use the value. */
2896 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2897 || h->root.root.type == bfd_link_hash_undefweak);
2898
2899 /* Emit necessary relocations. */
0a44bf69 2900 sreloc = mips_elf_rel_dyn_section (info, FALSE);
0f20cc35
DJ
2901
2902 /* General Dynamic. */
2903 if (*tls_type_p & GOT_TLS_GD)
2904 {
2905 offset = got_offset;
2906 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2907
2908 if (need_relocs)
2909 {
2910 mips_elf_output_dynamic_relocation
861fb55a 2911 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2912 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2913 sgot->output_offset + sgot->output_section->vma + offset);
2914
2915 if (indx)
2916 mips_elf_output_dynamic_relocation
861fb55a 2917 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2918 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2919 sgot->output_offset + sgot->output_section->vma + offset2);
2920 else
2921 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2922 sgot->contents + offset2);
2923 }
2924 else
2925 {
2926 MIPS_ELF_PUT_WORD (abfd, 1,
2927 sgot->contents + offset);
2928 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2929 sgot->contents + offset2);
2930 }
2931
2932 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2933 }
2934
2935 /* Initial Exec model. */
2936 if (*tls_type_p & GOT_TLS_IE)
2937 {
2938 offset = got_offset;
2939
2940 if (need_relocs)
2941 {
2942 if (indx == 0)
2943 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2944 sgot->contents + offset);
2945 else
2946 MIPS_ELF_PUT_WORD (abfd, 0,
2947 sgot->contents + offset);
2948
2949 mips_elf_output_dynamic_relocation
861fb55a 2950 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2951 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2952 sgot->output_offset + sgot->output_section->vma + offset);
2953 }
2954 else
2955 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2956 sgot->contents + offset);
2957 }
2958
2959 if (*tls_type_p & GOT_TLS_LDM)
2960 {
2961 /* The initial offset is zero, and the LD offsets will include the
2962 bias by DTP_OFFSET. */
2963 MIPS_ELF_PUT_WORD (abfd, 0,
2964 sgot->contents + got_offset
2965 + MIPS_ELF_GOT_SIZE (abfd));
2966
2967 if (!info->shared)
2968 MIPS_ELF_PUT_WORD (abfd, 1,
2969 sgot->contents + got_offset);
2970 else
2971 mips_elf_output_dynamic_relocation
861fb55a 2972 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2973 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2974 sgot->output_offset + sgot->output_section->vma + got_offset);
2975 }
2976
2977 *tls_type_p |= GOT_TLS_DONE;
2978}
2979
2980/* Return the GOT index to use for a relocation of type R_TYPE against
2981 a symbol accessed using TLS_TYPE models. The GOT entries for this
2982 symbol in this GOT start at GOT_INDEX. This function initializes the
2983 GOT entries and corresponding relocations. */
2984
2985static bfd_vma
2986mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2987 int r_type, struct bfd_link_info *info,
2988 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2989{
2990 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2991 || r_type == R_MIPS_TLS_LDM);
2992
2993 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2994
2995 if (r_type == R_MIPS_TLS_GOTTPREL)
2996 {
2997 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2998 if (*tls_type & GOT_TLS_GD)
2999 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
3000 else
3001 return got_index;
3002 }
3003
3004 if (r_type == R_MIPS_TLS_GD)
3005 {
3006 BFD_ASSERT (*tls_type & GOT_TLS_GD);
3007 return got_index;
3008 }
3009
3010 if (r_type == R_MIPS_TLS_LDM)
3011 {
3012 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
3013 return got_index;
3014 }
3015
3016 return got_index;
3017}
3018
0a44bf69
RS
3019/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3020 for global symbol H. .got.plt comes before the GOT, so the offset
3021 will be negative. */
3022
3023static bfd_vma
3024mips_elf_gotplt_index (struct bfd_link_info *info,
3025 struct elf_link_hash_entry *h)
3026{
3027 bfd_vma plt_index, got_address, got_value;
3028 struct mips_elf_link_hash_table *htab;
3029
3030 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3031 BFD_ASSERT (htab != NULL);
3032
0a44bf69
RS
3033 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3034
861fb55a
DJ
3035 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3036 section starts with reserved entries. */
3037 BFD_ASSERT (htab->is_vxworks);
3038
0a44bf69
RS
3039 /* Calculate the index of the symbol's PLT entry. */
3040 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3041
3042 /* Calculate the address of the associated .got.plt entry. */
3043 got_address = (htab->sgotplt->output_section->vma
3044 + htab->sgotplt->output_offset
3045 + plt_index * 4);
3046
3047 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3048 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3049 + htab->root.hgot->root.u.def.section->output_offset
3050 + htab->root.hgot->root.u.def.value);
3051
3052 return got_address - got_value;
3053}
3054
5c18022e 3055/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3056 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3057 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3058 offset can be found. */
b49e97c9
TS
3059
3060static bfd_vma
9719ad41 3061mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3062 bfd_vma value, unsigned long r_symndx,
0f20cc35 3063 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3064{
a8028dd0 3065 struct mips_elf_link_hash_table *htab;
b15e6682 3066 struct mips_got_entry *entry;
b49e97c9 3067
a8028dd0 3068 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3069 BFD_ASSERT (htab != NULL);
3070
a8028dd0
RS
3071 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3072 r_symndx, h, r_type);
0f20cc35 3073 if (!entry)
b15e6682 3074 return MINUS_ONE;
0f20cc35
DJ
3075
3076 if (TLS_RELOC_P (r_type))
ead49a57 3077 {
a8028dd0 3078 if (entry->symndx == -1 && htab->got_info->next == NULL)
ead49a57
RS
3079 /* A type (3) entry in the single-GOT case. We use the symbol's
3080 hash table entry to track the index. */
3081 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3082 r_type, info, h, value);
3083 else
3084 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3085 r_type, info, h, value);
3086 }
0f20cc35
DJ
3087 else
3088 return entry->gotidx;
b49e97c9
TS
3089}
3090
3091/* Returns the GOT index for the global symbol indicated by H. */
3092
3093static bfd_vma
0f20cc35
DJ
3094mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3095 int r_type, struct bfd_link_info *info)
b49e97c9 3096{
a8028dd0 3097 struct mips_elf_link_hash_table *htab;
91d6fa6a 3098 bfd_vma got_index;
f4416af6 3099 struct mips_got_info *g, *gg;
d0c7ff07 3100 long global_got_dynindx = 0;
b49e97c9 3101
a8028dd0 3102 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3103 BFD_ASSERT (htab != NULL);
3104
a8028dd0 3105 gg = g = htab->got_info;
f4416af6
AO
3106 if (g->bfd2got && ibfd)
3107 {
3108 struct mips_got_entry e, *p;
143d77c5 3109
f4416af6
AO
3110 BFD_ASSERT (h->dynindx >= 0);
3111
3112 g = mips_elf_got_for_ibfd (g, ibfd);
0f20cc35 3113 if (g->next != gg || TLS_RELOC_P (r_type))
f4416af6
AO
3114 {
3115 e.abfd = ibfd;
3116 e.symndx = -1;
3117 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 3118 e.tls_type = 0;
f4416af6 3119
9719ad41 3120 p = htab_find (g->got_entries, &e);
f4416af6
AO
3121
3122 BFD_ASSERT (p->gotidx > 0);
0f20cc35
DJ
3123
3124 if (TLS_RELOC_P (r_type))
3125 {
3126 bfd_vma value = MINUS_ONE;
3127 if ((h->root.type == bfd_link_hash_defined
3128 || h->root.type == bfd_link_hash_defweak)
3129 && h->root.u.def.section->output_section)
3130 value = (h->root.u.def.value
3131 + h->root.u.def.section->output_offset
3132 + h->root.u.def.section->output_section->vma);
3133
3134 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3135 info, e.d.h, value);
3136 }
3137 else
3138 return p->gotidx;
f4416af6
AO
3139 }
3140 }
3141
3142 if (gg->global_gotsym != NULL)
3143 global_got_dynindx = gg->global_gotsym->dynindx;
b49e97c9 3144
0f20cc35
DJ
3145 if (TLS_RELOC_P (r_type))
3146 {
3147 struct mips_elf_link_hash_entry *hm
3148 = (struct mips_elf_link_hash_entry *) h;
3149 bfd_vma value = MINUS_ONE;
3150
3151 if ((h->root.type == bfd_link_hash_defined
3152 || h->root.type == bfd_link_hash_defweak)
3153 && h->root.u.def.section->output_section)
3154 value = (h->root.u.def.value
3155 + h->root.u.def.section->output_offset
3156 + h->root.u.def.section->output_section->vma);
3157
91d6fa6a
NC
3158 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3159 r_type, info, hm, value);
0f20cc35
DJ
3160 }
3161 else
3162 {
3163 /* Once we determine the global GOT entry with the lowest dynamic
3164 symbol table index, we must put all dynamic symbols with greater
3165 indices into the GOT. That makes it easy to calculate the GOT
3166 offset. */
3167 BFD_ASSERT (h->dynindx >= global_got_dynindx);
91d6fa6a
NC
3168 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3169 * MIPS_ELF_GOT_SIZE (abfd));
0f20cc35 3170 }
91d6fa6a 3171 BFD_ASSERT (got_index < htab->sgot->size);
b49e97c9 3172
91d6fa6a 3173 return got_index;
b49e97c9
TS
3174}
3175
5c18022e
RS
3176/* Find a GOT page entry that points to within 32KB of VALUE. These
3177 entries are supposed to be placed at small offsets in the GOT, i.e.,
3178 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3179 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3180 offset of the GOT entry from VALUE. */
b49e97c9
TS
3181
3182static bfd_vma
9719ad41 3183mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3184 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3185{
91d6fa6a 3186 bfd_vma page, got_index;
b15e6682 3187 struct mips_got_entry *entry;
b49e97c9 3188
0a44bf69 3189 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3190 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3191 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3192
b15e6682
AO
3193 if (!entry)
3194 return MINUS_ONE;
143d77c5 3195
91d6fa6a 3196 got_index = entry->gotidx;
b49e97c9
TS
3197
3198 if (offsetp)
f4416af6 3199 *offsetp = value - entry->d.address;
b49e97c9 3200
91d6fa6a 3201 return got_index;
b49e97c9
TS
3202}
3203
738e5348 3204/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
0a44bf69
RS
3205 EXTERNAL is true if the relocation was against a global symbol
3206 that has been forced local. */
b49e97c9
TS
3207
3208static bfd_vma
9719ad41 3209mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3210 bfd_vma value, bfd_boolean external)
b49e97c9 3211{
b15e6682 3212 struct mips_got_entry *entry;
b49e97c9 3213
0a44bf69
RS
3214 /* GOT16 relocations against local symbols are followed by a LO16
3215 relocation; those against global symbols are not. Thus if the
3216 symbol was originally local, the GOT16 relocation should load the
3217 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3218 if (! external)
0a44bf69 3219 value = mips_elf_high (value) << 16;
b49e97c9 3220
738e5348
RS
3221 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3222 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3223 same in all cases. */
a8028dd0
RS
3224 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3225 NULL, R_MIPS_GOT16);
b15e6682
AO
3226 if (entry)
3227 return entry->gotidx;
3228 else
3229 return MINUS_ONE;
b49e97c9
TS
3230}
3231
3232/* Returns the offset for the entry at the INDEXth position
3233 in the GOT. */
3234
3235static bfd_vma
a8028dd0 3236mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3237 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3238{
a8028dd0 3239 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3240 asection *sgot;
3241 bfd_vma gp;
3242
a8028dd0 3243 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3244 BFD_ASSERT (htab != NULL);
3245
a8028dd0 3246 sgot = htab->sgot;
f4416af6 3247 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3248 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3249
91d6fa6a 3250 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3251}
3252
0a44bf69
RS
3253/* Create and return a local GOT entry for VALUE, which was calculated
3254 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3255 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3256 instead. */
b49e97c9 3257
b15e6682 3258static struct mips_got_entry *
0a44bf69 3259mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3260 bfd *ibfd, bfd_vma value,
5c18022e 3261 unsigned long r_symndx,
0f20cc35
DJ
3262 struct mips_elf_link_hash_entry *h,
3263 int r_type)
b49e97c9 3264{
b15e6682 3265 struct mips_got_entry entry, **loc;
f4416af6 3266 struct mips_got_info *g;
0a44bf69
RS
3267 struct mips_elf_link_hash_table *htab;
3268
3269 htab = mips_elf_hash_table (info);
4dfe6ac6 3270 BFD_ASSERT (htab != NULL);
b15e6682 3271
f4416af6
AO
3272 entry.abfd = NULL;
3273 entry.symndx = -1;
3274 entry.d.address = value;
0f20cc35 3275 entry.tls_type = 0;
f4416af6 3276
a8028dd0 3277 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
f4416af6
AO
3278 if (g == NULL)
3279 {
a8028dd0 3280 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
f4416af6
AO
3281 BFD_ASSERT (g != NULL);
3282 }
b15e6682 3283
0f20cc35
DJ
3284 /* We might have a symbol, H, if it has been forced local. Use the
3285 global entry then. It doesn't matter whether an entry is local
3286 or global for TLS, since the dynamic linker does not
3287 automatically relocate TLS GOT entries. */
a008ac03 3288 BFD_ASSERT (h == NULL || h->root.forced_local);
0f20cc35
DJ
3289 if (TLS_RELOC_P (r_type))
3290 {
3291 struct mips_got_entry *p;
3292
3293 entry.abfd = ibfd;
3294 if (r_type == R_MIPS_TLS_LDM)
3295 {
3296 entry.tls_type = GOT_TLS_LDM;
3297 entry.symndx = 0;
3298 entry.d.addend = 0;
3299 }
3300 else if (h == NULL)
3301 {
3302 entry.symndx = r_symndx;
3303 entry.d.addend = 0;
3304 }
3305 else
3306 entry.d.h = h;
3307
3308 p = (struct mips_got_entry *)
3309 htab_find (g->got_entries, &entry);
3310
3311 BFD_ASSERT (p);
3312 return p;
3313 }
3314
b15e6682
AO
3315 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3316 INSERT);
3317 if (*loc)
3318 return *loc;
143d77c5 3319
b15e6682 3320 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
0f20cc35 3321 entry.tls_type = 0;
b15e6682
AO
3322
3323 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3324
3325 if (! *loc)
3326 return NULL;
143d77c5 3327
b15e6682
AO
3328 memcpy (*loc, &entry, sizeof entry);
3329
8275b357 3330 if (g->assigned_gotno > g->local_gotno)
b49e97c9 3331 {
f4416af6 3332 (*loc)->gotidx = -1;
b49e97c9
TS
3333 /* We didn't allocate enough space in the GOT. */
3334 (*_bfd_error_handler)
3335 (_("not enough GOT space for local GOT entries"));
3336 bfd_set_error (bfd_error_bad_value);
b15e6682 3337 return NULL;
b49e97c9
TS
3338 }
3339
3340 MIPS_ELF_PUT_WORD (abfd, value,
a8028dd0 3341 (htab->sgot->contents + entry.gotidx));
b15e6682 3342
5c18022e 3343 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3344 if (htab->is_vxworks)
3345 {
3346 Elf_Internal_Rela outrel;
5c18022e 3347 asection *s;
91d6fa6a 3348 bfd_byte *rloc;
0a44bf69 3349 bfd_vma got_address;
0a44bf69
RS
3350
3351 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3352 got_address = (htab->sgot->output_section->vma
3353 + htab->sgot->output_offset
0a44bf69
RS
3354 + entry.gotidx);
3355
91d6fa6a 3356 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3357 outrel.r_offset = got_address;
5c18022e
RS
3358 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3359 outrel.r_addend = value;
91d6fa6a 3360 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3361 }
3362
b15e6682 3363 return *loc;
b49e97c9
TS
3364}
3365
d4596a51
RS
3366/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3367 The number might be exact or a worst-case estimate, depending on how
3368 much information is available to elf_backend_omit_section_dynsym at
3369 the current linking stage. */
3370
3371static bfd_size_type
3372count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3373{
3374 bfd_size_type count;
3375
3376 count = 0;
3377 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3378 {
3379 asection *p;
3380 const struct elf_backend_data *bed;
3381
3382 bed = get_elf_backend_data (output_bfd);
3383 for (p = output_bfd->sections; p ; p = p->next)
3384 if ((p->flags & SEC_EXCLUDE) == 0
3385 && (p->flags & SEC_ALLOC) != 0
3386 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3387 ++count;
3388 }
3389 return count;
3390}
3391
b49e97c9 3392/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3393 appear towards the end. */
b49e97c9 3394
b34976b6 3395static bfd_boolean
d4596a51 3396mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3397{
a8028dd0 3398 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3399 struct mips_elf_hash_sort_data hsd;
3400 struct mips_got_info *g;
b49e97c9 3401
d4596a51
RS
3402 if (elf_hash_table (info)->dynsymcount == 0)
3403 return TRUE;
3404
a8028dd0 3405 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3406 BFD_ASSERT (htab != NULL);
3407
a8028dd0 3408 g = htab->got_info;
d4596a51
RS
3409 if (g == NULL)
3410 return TRUE;
f4416af6 3411
b49e97c9 3412 hsd.low = NULL;
23cc69b6
RS
3413 hsd.max_unref_got_dynindx
3414 = hsd.min_got_dynindx
3415 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3416 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3417 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3418 elf_hash_table (info)),
3419 mips_elf_sort_hash_table_f,
3420 &hsd);
3421
3422 /* There should have been enough room in the symbol table to
44c410de 3423 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3424 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3425 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3426 == elf_hash_table (info)->dynsymcount);
3427 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3428 == g->global_gotno);
b49e97c9
TS
3429
3430 /* Now we know which dynamic symbol has the lowest dynamic symbol
3431 table index in the GOT. */
b49e97c9
TS
3432 g->global_gotsym = hsd.low;
3433
b34976b6 3434 return TRUE;
b49e97c9
TS
3435}
3436
3437/* If H needs a GOT entry, assign it the highest available dynamic
3438 index. Otherwise, assign it the lowest available dynamic
3439 index. */
3440
b34976b6 3441static bfd_boolean
9719ad41 3442mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3443{
9719ad41 3444 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9
TS
3445
3446 if (h->root.root.type == bfd_link_hash_warning)
3447 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3448
3449 /* Symbols without dynamic symbol table entries aren't interesting
3450 at all. */
3451 if (h->root.dynindx == -1)
b34976b6 3452 return TRUE;
b49e97c9 3453
634835ae 3454 switch (h->global_got_area)
f4416af6 3455 {
634835ae
RS
3456 case GGA_NONE:
3457 h->root.dynindx = hsd->max_non_got_dynindx++;
3458 break;
0f20cc35 3459
634835ae 3460 case GGA_NORMAL:
0f20cc35
DJ
3461 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3462
b49e97c9
TS
3463 h->root.dynindx = --hsd->min_got_dynindx;
3464 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3465 break;
3466
3467 case GGA_RELOC_ONLY:
3468 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3469
3470 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3471 hsd->low = (struct elf_link_hash_entry *) h;
3472 h->root.dynindx = hsd->max_unref_got_dynindx++;
3473 break;
b49e97c9
TS
3474 }
3475
b34976b6 3476 return TRUE;
b49e97c9
TS
3477}
3478
3479/* If H is a symbol that needs a global GOT entry, but has a dynamic
3480 symbol table index lower than any we've seen to date, record it for
3481 posterity. */
3482
b34976b6 3483static bfd_boolean
9719ad41
RS
3484mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3485 bfd *abfd, struct bfd_link_info *info,
0f20cc35 3486 unsigned char tls_flag)
b49e97c9 3487{
a8028dd0 3488 struct mips_elf_link_hash_table *htab;
634835ae 3489 struct mips_elf_link_hash_entry *hmips;
f4416af6 3490 struct mips_got_entry entry, **loc;
a8028dd0
RS
3491 struct mips_got_info *g;
3492
3493 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3494 BFD_ASSERT (htab != NULL);
3495
634835ae 3496 hmips = (struct mips_elf_link_hash_entry *) h;
f4416af6 3497
b49e97c9
TS
3498 /* A global symbol in the GOT must also be in the dynamic symbol
3499 table. */
7c5fcef7
L
3500 if (h->dynindx == -1)
3501 {
3502 switch (ELF_ST_VISIBILITY (h->other))
3503 {
3504 case STV_INTERNAL:
3505 case STV_HIDDEN:
33bb52fb 3506 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3507 break;
3508 }
c152c796 3509 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3510 return FALSE;
7c5fcef7 3511 }
b49e97c9 3512
86324f90 3513 /* Make sure we have a GOT to put this entry into. */
a8028dd0 3514 g = htab->got_info;
86324f90
EC
3515 BFD_ASSERT (g != NULL);
3516
f4416af6
AO
3517 entry.abfd = abfd;
3518 entry.symndx = -1;
3519 entry.d.h = (struct mips_elf_link_hash_entry *) h;
0f20cc35 3520 entry.tls_type = 0;
f4416af6
AO
3521
3522 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3523 INSERT);
3524
b49e97c9
TS
3525 /* If we've already marked this entry as needing GOT space, we don't
3526 need to do it again. */
f4416af6 3527 if (*loc)
0f20cc35
DJ
3528 {
3529 (*loc)->tls_type |= tls_flag;
3530 return TRUE;
3531 }
f4416af6
AO
3532
3533 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3534
3535 if (! *loc)
3536 return FALSE;
143d77c5 3537
f4416af6 3538 entry.gotidx = -1;
0f20cc35
DJ
3539 entry.tls_type = tls_flag;
3540
f4416af6
AO
3541 memcpy (*loc, &entry, sizeof entry);
3542
0f20cc35 3543 if (tls_flag == 0)
634835ae 3544 hmips->global_got_area = GGA_NORMAL;
b49e97c9 3545
b34976b6 3546 return TRUE;
b49e97c9 3547}
f4416af6
AO
3548
3549/* Reserve space in G for a GOT entry containing the value of symbol
3550 SYMNDX in input bfd ABDF, plus ADDEND. */
3551
3552static bfd_boolean
9719ad41 3553mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
a8028dd0 3554 struct bfd_link_info *info,
0f20cc35 3555 unsigned char tls_flag)
f4416af6 3556{
a8028dd0
RS
3557 struct mips_elf_link_hash_table *htab;
3558 struct mips_got_info *g;
f4416af6
AO
3559 struct mips_got_entry entry, **loc;
3560
a8028dd0 3561 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3562 BFD_ASSERT (htab != NULL);
3563
a8028dd0
RS
3564 g = htab->got_info;
3565 BFD_ASSERT (g != NULL);
3566
f4416af6
AO
3567 entry.abfd = abfd;
3568 entry.symndx = symndx;
3569 entry.d.addend = addend;
0f20cc35 3570 entry.tls_type = tls_flag;
f4416af6
AO
3571 loc = (struct mips_got_entry **)
3572 htab_find_slot (g->got_entries, &entry, INSERT);
3573
3574 if (*loc)
0f20cc35
DJ
3575 {
3576 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3577 {
3578 g->tls_gotno += 2;
3579 (*loc)->tls_type |= tls_flag;
3580 }
3581 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3582 {
3583 g->tls_gotno += 1;
3584 (*loc)->tls_type |= tls_flag;
3585 }
3586 return TRUE;
3587 }
f4416af6 3588
0f20cc35
DJ
3589 if (tls_flag != 0)
3590 {
3591 entry.gotidx = -1;
3592 entry.tls_type = tls_flag;
3593 if (tls_flag == GOT_TLS_IE)
3594 g->tls_gotno += 1;
3595 else if (tls_flag == GOT_TLS_GD)
3596 g->tls_gotno += 2;
3597 else if (g->tls_ldm_offset == MINUS_ONE)
3598 {
3599 g->tls_ldm_offset = MINUS_TWO;
3600 g->tls_gotno += 2;
3601 }
3602 }
3603 else
3604 {
3605 entry.gotidx = g->local_gotno++;
3606 entry.tls_type = 0;
3607 }
f4416af6
AO
3608
3609 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3610
3611 if (! *loc)
3612 return FALSE;
143d77c5 3613
f4416af6
AO
3614 memcpy (*loc, &entry, sizeof entry);
3615
3616 return TRUE;
3617}
c224138d
RS
3618
3619/* Return the maximum number of GOT page entries required for RANGE. */
3620
3621static bfd_vma
3622mips_elf_pages_for_range (const struct mips_got_page_range *range)
3623{
3624 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3625}
3626
3a3b6725 3627/* Record that ABFD has a page relocation against symbol SYMNDX and
a8028dd0
RS
3628 that ADDEND is the addend for that relocation.
3629
3630 This function creates an upper bound on the number of GOT slots
3631 required; no attempt is made to combine references to non-overridable
3632 global symbols across multiple input files. */
c224138d
RS
3633
3634static bfd_boolean
a8028dd0
RS
3635mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3636 long symndx, bfd_signed_vma addend)
c224138d 3637{
a8028dd0
RS
3638 struct mips_elf_link_hash_table *htab;
3639 struct mips_got_info *g;
c224138d
RS
3640 struct mips_got_page_entry lookup, *entry;
3641 struct mips_got_page_range **range_ptr, *range;
3642 bfd_vma old_pages, new_pages;
3643 void **loc;
3644
a8028dd0 3645 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3646 BFD_ASSERT (htab != NULL);
3647
a8028dd0
RS
3648 g = htab->got_info;
3649 BFD_ASSERT (g != NULL);
3650
c224138d
RS
3651 /* Find the mips_got_page_entry hash table entry for this symbol. */
3652 lookup.abfd = abfd;
3653 lookup.symndx = symndx;
3654 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3655 if (loc == NULL)
3656 return FALSE;
3657
3658 /* Create a mips_got_page_entry if this is the first time we've
3659 seen the symbol. */
3660 entry = (struct mips_got_page_entry *) *loc;
3661 if (!entry)
3662 {
3663 entry = bfd_alloc (abfd, sizeof (*entry));
3664 if (!entry)
3665 return FALSE;
3666
3667 entry->abfd = abfd;
3668 entry->symndx = symndx;
3669 entry->ranges = NULL;
3670 entry->num_pages = 0;
3671 *loc = entry;
3672 }
3673
3674 /* Skip over ranges whose maximum extent cannot share a page entry
3675 with ADDEND. */
3676 range_ptr = &entry->ranges;
3677 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3678 range_ptr = &(*range_ptr)->next;
3679
3680 /* If we scanned to the end of the list, or found a range whose
3681 minimum extent cannot share a page entry with ADDEND, create
3682 a new singleton range. */
3683 range = *range_ptr;
3684 if (!range || addend < range->min_addend - 0xffff)
3685 {
3686 range = bfd_alloc (abfd, sizeof (*range));
3687 if (!range)
3688 return FALSE;
3689
3690 range->next = *range_ptr;
3691 range->min_addend = addend;
3692 range->max_addend = addend;
3693
3694 *range_ptr = range;
3695 entry->num_pages++;
3696 g->page_gotno++;
3697 return TRUE;
3698 }
3699
3700 /* Remember how many pages the old range contributed. */
3701 old_pages = mips_elf_pages_for_range (range);
3702
3703 /* Update the ranges. */
3704 if (addend < range->min_addend)
3705 range->min_addend = addend;
3706 else if (addend > range->max_addend)
3707 {
3708 if (range->next && addend >= range->next->min_addend - 0xffff)
3709 {
3710 old_pages += mips_elf_pages_for_range (range->next);
3711 range->max_addend = range->next->max_addend;
3712 range->next = range->next->next;
3713 }
3714 else
3715 range->max_addend = addend;
3716 }
3717
3718 /* Record any change in the total estimate. */
3719 new_pages = mips_elf_pages_for_range (range);
3720 if (old_pages != new_pages)
3721 {
3722 entry->num_pages += new_pages - old_pages;
3723 g->page_gotno += new_pages - old_pages;
3724 }
3725
3726 return TRUE;
3727}
33bb52fb
RS
3728
3729/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3730
3731static void
3732mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3733 unsigned int n)
3734{
3735 asection *s;
3736 struct mips_elf_link_hash_table *htab;
3737
3738 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3739 BFD_ASSERT (htab != NULL);
3740
33bb52fb
RS
3741 s = mips_elf_rel_dyn_section (info, FALSE);
3742 BFD_ASSERT (s != NULL);
3743
3744 if (htab->is_vxworks)
3745 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3746 else
3747 {
3748 if (s->size == 0)
3749 {
3750 /* Make room for a null element. */
3751 s->size += MIPS_ELF_REL_SIZE (abfd);
3752 ++s->reloc_count;
3753 }
3754 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3755 }
3756}
3757\f
3758/* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3759 if the GOT entry is for an indirect or warning symbol. */
3760
3761static int
3762mips_elf_check_recreate_got (void **entryp, void *data)
3763{
3764 struct mips_got_entry *entry;
3765 bfd_boolean *must_recreate;
3766
3767 entry = (struct mips_got_entry *) *entryp;
3768 must_recreate = (bfd_boolean *) data;
3769 if (entry->abfd != NULL && entry->symndx == -1)
3770 {
3771 struct mips_elf_link_hash_entry *h;
3772
3773 h = entry->d.h;
3774 if (h->root.root.type == bfd_link_hash_indirect
3775 || h->root.root.type == bfd_link_hash_warning)
3776 {
3777 *must_recreate = TRUE;
3778 return 0;
3779 }
3780 }
3781 return 1;
3782}
3783
3784/* A htab_traverse callback for GOT entries. Add all entries to
3785 hash table *DATA, converting entries for indirect and warning
3786 symbols into entries for the target symbol. Set *DATA to null
3787 on error. */
3788
3789static int
3790mips_elf_recreate_got (void **entryp, void *data)
3791{
3792 htab_t *new_got;
3793 struct mips_got_entry *entry;
3794 void **slot;
3795
3796 new_got = (htab_t *) data;
3797 entry = (struct mips_got_entry *) *entryp;
3798 if (entry->abfd != NULL && entry->symndx == -1)
3799 {
3800 struct mips_elf_link_hash_entry *h;
3801
3802 h = entry->d.h;
3803 while (h->root.root.type == bfd_link_hash_indirect
3804 || h->root.root.type == bfd_link_hash_warning)
634835ae
RS
3805 {
3806 BFD_ASSERT (h->global_got_area == GGA_NONE);
3807 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3808 }
33bb52fb
RS
3809 entry->d.h = h;
3810 }
3811 slot = htab_find_slot (*new_got, entry, INSERT);
3812 if (slot == NULL)
3813 {
3814 *new_got = NULL;
3815 return 0;
3816 }
3817 if (*slot == NULL)
3818 *slot = entry;
3819 else
3820 free (entry);
3821 return 1;
3822}
3823
3824/* If any entries in G->got_entries are for indirect or warning symbols,
3825 replace them with entries for the target symbol. */
3826
3827static bfd_boolean
3828mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3829{
3830 bfd_boolean must_recreate;
3831 htab_t new_got;
3832
3833 must_recreate = FALSE;
3834 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3835 if (must_recreate)
3836 {
3837 new_got = htab_create (htab_size (g->got_entries),
3838 mips_elf_got_entry_hash,
3839 mips_elf_got_entry_eq, NULL);
3840 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3841 if (new_got == NULL)
3842 return FALSE;
3843
3844 /* Each entry in g->got_entries has either been copied to new_got
3845 or freed. Now delete the hash table itself. */
3846 htab_delete (g->got_entries);
3847 g->got_entries = new_got;
3848 }
3849 return TRUE;
3850}
3851
634835ae 3852/* A mips_elf_link_hash_traverse callback for which DATA points
d4596a51 3853 to a mips_got_info. Count the number of type (3) entries. */
33bb52fb
RS
3854
3855static int
d4596a51 3856mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb
RS
3857{
3858 struct mips_got_info *g;
3859
3860 g = (struct mips_got_info *) data;
d4596a51 3861 if (h->global_got_area != GGA_NONE)
33bb52fb 3862 {
d4596a51
RS
3863 if (h->root.forced_local || h->root.dynindx == -1)
3864 {
3865 /* We no longer need this entry if it was only used for
3866 relocations; those relocations will be against the
3867 null or section symbol instead of H. */
3868 if (h->global_got_area != GGA_RELOC_ONLY)
3869 g->local_gotno++;
3870 h->global_got_area = GGA_NONE;
3871 }
3872 else
23cc69b6
RS
3873 {
3874 g->global_gotno++;
3875 if (h->global_got_area == GGA_RELOC_ONLY)
3876 g->reloc_only_gotno++;
3877 }
33bb52fb
RS
3878 }
3879 return 1;
3880}
f4416af6
AO
3881\f
3882/* Compute the hash value of the bfd in a bfd2got hash entry. */
3883
3884static hashval_t
9719ad41 3885mips_elf_bfd2got_entry_hash (const void *entry_)
f4416af6
AO
3886{
3887 const struct mips_elf_bfd2got_hash *entry
3888 = (struct mips_elf_bfd2got_hash *)entry_;
3889
3890 return entry->bfd->id;
3891}
3892
3893/* Check whether two hash entries have the same bfd. */
3894
3895static int
9719ad41 3896mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3897{
3898 const struct mips_elf_bfd2got_hash *e1
3899 = (const struct mips_elf_bfd2got_hash *)entry1;
3900 const struct mips_elf_bfd2got_hash *e2
3901 = (const struct mips_elf_bfd2got_hash *)entry2;
3902
3903 return e1->bfd == e2->bfd;
3904}
3905
bad36eac 3906/* In a multi-got link, determine the GOT to be used for IBFD. G must
f4416af6
AO
3907 be the master GOT data. */
3908
3909static struct mips_got_info *
9719ad41 3910mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
3911{
3912 struct mips_elf_bfd2got_hash e, *p;
3913
3914 if (! g->bfd2got)
3915 return g;
3916
3917 e.bfd = ibfd;
9719ad41 3918 p = htab_find (g->bfd2got, &e);
f4416af6
AO
3919 return p ? p->g : NULL;
3920}
3921
c224138d
RS
3922/* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3923 Return NULL if an error occured. */
f4416af6 3924
c224138d
RS
3925static struct mips_got_info *
3926mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3927 bfd *input_bfd)
f4416af6 3928{
f4416af6 3929 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
c224138d 3930 struct mips_got_info *g;
f4416af6 3931 void **bfdgotp;
143d77c5 3932
c224138d 3933 bfdgot_entry.bfd = input_bfd;
f4416af6 3934 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
c224138d 3935 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
f4416af6 3936
c224138d 3937 if (bfdgot == NULL)
f4416af6 3938 {
c224138d
RS
3939 bfdgot = ((struct mips_elf_bfd2got_hash *)
3940 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
f4416af6 3941 if (bfdgot == NULL)
c224138d 3942 return NULL;
f4416af6
AO
3943
3944 *bfdgotp = bfdgot;
3945
c224138d
RS
3946 g = ((struct mips_got_info *)
3947 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
f4416af6 3948 if (g == NULL)
c224138d
RS
3949 return NULL;
3950
3951 bfdgot->bfd = input_bfd;
3952 bfdgot->g = g;
f4416af6
AO
3953
3954 g->global_gotsym = NULL;
3955 g->global_gotno = 0;
23cc69b6 3956 g->reloc_only_gotno = 0;
f4416af6 3957 g->local_gotno = 0;
c224138d 3958 g->page_gotno = 0;
f4416af6 3959 g->assigned_gotno = -1;
0f20cc35
DJ
3960 g->tls_gotno = 0;
3961 g->tls_assigned_gotno = 0;
3962 g->tls_ldm_offset = MINUS_ONE;
f4416af6 3963 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
9719ad41 3964 mips_elf_multi_got_entry_eq, NULL);
f4416af6 3965 if (g->got_entries == NULL)
c224138d
RS
3966 return NULL;
3967
3968 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3969 mips_got_page_entry_eq, NULL);
3970 if (g->got_page_entries == NULL)
3971 return NULL;
f4416af6
AO
3972
3973 g->bfd2got = NULL;
3974 g->next = NULL;
3975 }
3976
c224138d
RS
3977 return bfdgot->g;
3978}
3979
3980/* A htab_traverse callback for the entries in the master got.
3981 Create one separate got for each bfd that has entries in the global
3982 got, such that we can tell how many local and global entries each
3983 bfd requires. */
3984
3985static int
3986mips_elf_make_got_per_bfd (void **entryp, void *p)
3987{
3988 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3989 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3990 struct mips_got_info *g;
3991
3992 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3993 if (g == NULL)
3994 {
3995 arg->obfd = NULL;
3996 return 0;
3997 }
3998
f4416af6
AO
3999 /* Insert the GOT entry in the bfd's got entry hash table. */
4000 entryp = htab_find_slot (g->got_entries, entry, INSERT);
4001 if (*entryp != NULL)
4002 return 1;
143d77c5 4003
f4416af6
AO
4004 *entryp = entry;
4005
0f20cc35
DJ
4006 if (entry->tls_type)
4007 {
4008 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4009 g->tls_gotno += 2;
4010 if (entry->tls_type & GOT_TLS_IE)
4011 g->tls_gotno += 1;
4012 }
33bb52fb 4013 else if (entry->symndx >= 0 || entry->d.h->root.forced_local)
f4416af6
AO
4014 ++g->local_gotno;
4015 else
4016 ++g->global_gotno;
4017
4018 return 1;
4019}
4020
c224138d
RS
4021/* A htab_traverse callback for the page entries in the master got.
4022 Associate each page entry with the bfd's got. */
4023
4024static int
4025mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
4026{
4027 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
4028 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
4029 struct mips_got_info *g;
4030
4031 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4032 if (g == NULL)
4033 {
4034 arg->obfd = NULL;
4035 return 0;
4036 }
4037
4038 /* Insert the GOT entry in the bfd's got entry hash table. */
4039 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4040 if (*entryp != NULL)
4041 return 1;
4042
4043 *entryp = entry;
4044 g->page_gotno += entry->num_pages;
4045 return 1;
4046}
4047
4048/* Consider merging the got described by BFD2GOT with TO, using the
4049 information given by ARG. Return -1 if this would lead to overflow,
4050 1 if they were merged successfully, and 0 if a merge failed due to
4051 lack of memory. (These values are chosen so that nonnegative return
4052 values can be returned by a htab_traverse callback.) */
4053
4054static int
4055mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4056 struct mips_got_info *to,
4057 struct mips_elf_got_per_bfd_arg *arg)
4058{
4059 struct mips_got_info *from = bfd2got->g;
4060 unsigned int estimate;
4061
4062 /* Work out how many page entries we would need for the combined GOT. */
4063 estimate = arg->max_pages;
4064 if (estimate >= from->page_gotno + to->page_gotno)
4065 estimate = from->page_gotno + to->page_gotno;
4066
4067 /* And conservatively estimate how many local, global and TLS entries
4068 would be needed. */
4069 estimate += (from->local_gotno
4070 + from->global_gotno
4071 + from->tls_gotno
4072 + to->local_gotno
4073 + to->global_gotno
4074 + to->tls_gotno);
4075
4076 /* Bail out if the combined GOT might be too big. */
4077 if (estimate > arg->max_count)
4078 return -1;
4079
4080 /* Commit to the merge. Record that TO is now the bfd for this got. */
4081 bfd2got->g = to;
4082
4083 /* Transfer the bfd's got information from FROM to TO. */
4084 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4085 if (arg->obfd == NULL)
4086 return 0;
4087
4088 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4089 if (arg->obfd == NULL)
4090 return 0;
4091
4092 /* We don't have to worry about releasing memory of the actual
4093 got entries, since they're all in the master got_entries hash
4094 table anyway. */
4095 htab_delete (from->got_entries);
4096 htab_delete (from->got_page_entries);
4097 return 1;
4098}
4099
f4416af6
AO
4100/* Attempt to merge gots of different input bfds. Try to use as much
4101 as possible of the primary got, since it doesn't require explicit
4102 dynamic relocations, but don't use bfds that would reference global
4103 symbols out of the addressable range. Failing the primary got,
4104 attempt to merge with the current got, or finish the current got
4105 and then make make the new got current. */
4106
4107static int
9719ad41 4108mips_elf_merge_gots (void **bfd2got_, void *p)
f4416af6
AO
4109{
4110 struct mips_elf_bfd2got_hash *bfd2got
4111 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4112 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
c224138d
RS
4113 struct mips_got_info *g;
4114 unsigned int estimate;
4115 int result;
4116
4117 g = bfd2got->g;
4118
4119 /* Work out the number of page, local and TLS entries. */
4120 estimate = arg->max_pages;
4121 if (estimate > g->page_gotno)
4122 estimate = g->page_gotno;
4123 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4124
4125 /* We place TLS GOT entries after both locals and globals. The globals
4126 for the primary GOT may overflow the normal GOT size limit, so be
4127 sure not to merge a GOT which requires TLS with the primary GOT in that
4128 case. This doesn't affect non-primary GOTs. */
c224138d 4129 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4130
c224138d 4131 if (estimate <= arg->max_count)
f4416af6 4132 {
c224138d
RS
4133 /* If we don't have a primary GOT, use it as
4134 a starting point for the primary GOT. */
4135 if (!arg->primary)
4136 {
4137 arg->primary = bfd2got->g;
4138 return 1;
4139 }
f4416af6 4140
c224138d
RS
4141 /* Try merging with the primary GOT. */
4142 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4143 if (result >= 0)
4144 return result;
f4416af6 4145 }
c224138d 4146
f4416af6 4147 /* If we can merge with the last-created got, do it. */
c224138d 4148 if (arg->current)
f4416af6 4149 {
c224138d
RS
4150 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4151 if (result >= 0)
4152 return result;
f4416af6 4153 }
c224138d 4154
f4416af6
AO
4155 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4156 fits; if it turns out that it doesn't, we'll get relocation
4157 overflows anyway. */
c224138d
RS
4158 g->next = arg->current;
4159 arg->current = g;
0f20cc35
DJ
4160
4161 return 1;
4162}
4163
ead49a57
RS
4164/* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4165 is null iff there is just a single GOT. */
0f20cc35
DJ
4166
4167static int
4168mips_elf_initialize_tls_index (void **entryp, void *p)
4169{
4170 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4171 struct mips_got_info *g = p;
ead49a57 4172 bfd_vma next_index;
cbf2cba4 4173 unsigned char tls_type;
0f20cc35
DJ
4174
4175 /* We're only interested in TLS symbols. */
4176 if (entry->tls_type == 0)
4177 return 1;
4178
ead49a57
RS
4179 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4180
4181 if (entry->symndx == -1 && g->next == NULL)
0f20cc35 4182 {
ead49a57
RS
4183 /* A type (3) got entry in the single-GOT case. We use the symbol's
4184 hash table entry to track its index. */
4185 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4186 return 1;
4187 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4188 entry->d.h->tls_got_offset = next_index;
cbf2cba4 4189 tls_type = entry->d.h->tls_type;
ead49a57
RS
4190 }
4191 else
4192 {
4193 if (entry->tls_type & GOT_TLS_LDM)
0f20cc35 4194 {
ead49a57
RS
4195 /* There are separate mips_got_entry objects for each input bfd
4196 that requires an LDM entry. Make sure that all LDM entries in
4197 a GOT resolve to the same index. */
4198 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4005427f 4199 {
ead49a57 4200 entry->gotidx = g->tls_ldm_offset;
4005427f
RS
4201 return 1;
4202 }
ead49a57 4203 g->tls_ldm_offset = next_index;
0f20cc35 4204 }
ead49a57 4205 entry->gotidx = next_index;
cbf2cba4 4206 tls_type = entry->tls_type;
f4416af6
AO
4207 }
4208
ead49a57 4209 /* Account for the entries we've just allocated. */
cbf2cba4 4210 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
0f20cc35 4211 g->tls_assigned_gotno += 2;
cbf2cba4 4212 if (tls_type & GOT_TLS_IE)
0f20cc35
DJ
4213 g->tls_assigned_gotno += 1;
4214
f4416af6
AO
4215 return 1;
4216}
4217
4218/* If passed a NULL mips_got_info in the argument, set the marker used
4219 to tell whether a global symbol needs a got entry (in the primary
4220 got) to the given VALUE.
4221
4222 If passed a pointer G to a mips_got_info in the argument (it must
4223 not be the primary GOT), compute the offset from the beginning of
4224 the (primary) GOT section to the entry in G corresponding to the
4225 global symbol. G's assigned_gotno must contain the index of the
4226 first available global GOT entry in G. VALUE must contain the size
4227 of a GOT entry in bytes. For each global GOT entry that requires a
4228 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4cc11e76 4229 marked as not eligible for lazy resolution through a function
f4416af6
AO
4230 stub. */
4231static int
9719ad41 4232mips_elf_set_global_got_offset (void **entryp, void *p)
f4416af6
AO
4233{
4234 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4235 struct mips_elf_set_global_got_offset_arg *arg
4236 = (struct mips_elf_set_global_got_offset_arg *)p;
4237 struct mips_got_info *g = arg->g;
4238
0f20cc35
DJ
4239 if (g && entry->tls_type != GOT_NORMAL)
4240 arg->needed_relocs +=
4241 mips_tls_got_relocs (arg->info, entry->tls_type,
4242 entry->symndx == -1 ? &entry->d.h->root : NULL);
4243
634835ae
RS
4244 if (entry->abfd != NULL
4245 && entry->symndx == -1
4246 && entry->d.h->global_got_area != GGA_NONE)
f4416af6
AO
4247 {
4248 if (g)
4249 {
4250 BFD_ASSERT (g->global_gotsym == NULL);
4251
4252 entry->gotidx = arg->value * (long) g->assigned_gotno++;
f4416af6
AO
4253 if (arg->info->shared
4254 || (elf_hash_table (arg->info)->dynamic_sections_created
f5385ebf
AM
4255 && entry->d.h->root.def_dynamic
4256 && !entry->d.h->root.def_regular))
f4416af6
AO
4257 ++arg->needed_relocs;
4258 }
4259 else
634835ae 4260 entry->d.h->global_got_area = arg->value;
f4416af6
AO
4261 }
4262
4263 return 1;
4264}
4265
33bb52fb
RS
4266/* A htab_traverse callback for GOT entries for which DATA is the
4267 bfd_link_info. Forbid any global symbols from having traditional
4268 lazy-binding stubs. */
4269
0626d451 4270static int
33bb52fb 4271mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4272{
33bb52fb
RS
4273 struct bfd_link_info *info;
4274 struct mips_elf_link_hash_table *htab;
4275 struct mips_got_entry *entry;
0626d451 4276
33bb52fb
RS
4277 entry = (struct mips_got_entry *) *entryp;
4278 info = (struct bfd_link_info *) data;
4279 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4280 BFD_ASSERT (htab != NULL);
4281
0626d451
RS
4282 if (entry->abfd != NULL
4283 && entry->symndx == -1
33bb52fb 4284 && entry->d.h->needs_lazy_stub)
f4416af6 4285 {
33bb52fb
RS
4286 entry->d.h->needs_lazy_stub = FALSE;
4287 htab->lazy_stub_count--;
f4416af6 4288 }
143d77c5 4289
f4416af6
AO
4290 return 1;
4291}
4292
f4416af6
AO
4293/* Return the offset of an input bfd IBFD's GOT from the beginning of
4294 the primary GOT. */
4295static bfd_vma
9719ad41 4296mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
4297{
4298 if (g->bfd2got == NULL)
4299 return 0;
4300
4301 g = mips_elf_got_for_ibfd (g, ibfd);
4302 if (! g)
4303 return 0;
4304
4305 BFD_ASSERT (g->next);
4306
4307 g = g->next;
143d77c5 4308
0f20cc35
DJ
4309 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4310 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4311}
4312
4313/* Turn a single GOT that is too big for 16-bit addressing into
4314 a sequence of GOTs, each one 16-bit addressable. */
4315
4316static bfd_boolean
9719ad41 4317mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4318 asection *got, bfd_size_type pages)
f4416af6 4319{
a8028dd0 4320 struct mips_elf_link_hash_table *htab;
f4416af6
AO
4321 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4322 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
a8028dd0 4323 struct mips_got_info *g, *gg;
33bb52fb
RS
4324 unsigned int assign, needed_relocs;
4325 bfd *dynobj;
f4416af6 4326
33bb52fb 4327 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4328 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4329 BFD_ASSERT (htab != NULL);
4330
a8028dd0 4331 g = htab->got_info;
f4416af6 4332 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
9719ad41 4333 mips_elf_bfd2got_entry_eq, NULL);
f4416af6
AO
4334 if (g->bfd2got == NULL)
4335 return FALSE;
4336
4337 got_per_bfd_arg.bfd2got = g->bfd2got;
4338 got_per_bfd_arg.obfd = abfd;
4339 got_per_bfd_arg.info = info;
4340
4341 /* Count how many GOT entries each input bfd requires, creating a
4342 map from bfd to got info while at that. */
f4416af6
AO
4343 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4344 if (got_per_bfd_arg.obfd == NULL)
4345 return FALSE;
4346
c224138d
RS
4347 /* Also count how many page entries each input bfd requires. */
4348 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4349 &got_per_bfd_arg);
4350 if (got_per_bfd_arg.obfd == NULL)
4351 return FALSE;
4352
f4416af6
AO
4353 got_per_bfd_arg.current = NULL;
4354 got_per_bfd_arg.primary = NULL;
0a44bf69 4355 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4356 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4357 - htab->reserved_gotno);
c224138d 4358 got_per_bfd_arg.max_pages = pages;
0f20cc35
DJ
4359 /* The number of globals that will be included in the primary GOT.
4360 See the calls to mips_elf_set_global_got_offset below for more
4361 information. */
4362 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4363
4364 /* Try to merge the GOTs of input bfds together, as long as they
4365 don't seem to exceed the maximum GOT size, choosing one of them
4366 to be the primary GOT. */
4367 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4368 if (got_per_bfd_arg.obfd == NULL)
4369 return FALSE;
4370
0f20cc35 4371 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6
AO
4372 if (got_per_bfd_arg.primary == NULL)
4373 {
4374 g->next = (struct mips_got_info *)
4375 bfd_alloc (abfd, sizeof (struct mips_got_info));
4376 if (g->next == NULL)
4377 return FALSE;
4378
4379 g->next->global_gotsym = NULL;
4380 g->next->global_gotno = 0;
23cc69b6 4381 g->next->reloc_only_gotno = 0;
f4416af6 4382 g->next->local_gotno = 0;
c224138d 4383 g->next->page_gotno = 0;
0f20cc35 4384 g->next->tls_gotno = 0;
f4416af6 4385 g->next->assigned_gotno = 0;
0f20cc35
DJ
4386 g->next->tls_assigned_gotno = 0;
4387 g->next->tls_ldm_offset = MINUS_ONE;
f4416af6
AO
4388 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4389 mips_elf_multi_got_entry_eq,
9719ad41 4390 NULL);
f4416af6
AO
4391 if (g->next->got_entries == NULL)
4392 return FALSE;
c224138d
RS
4393 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4394 mips_got_page_entry_eq,
4395 NULL);
4396 if (g->next->got_page_entries == NULL)
4397 return FALSE;
f4416af6
AO
4398 g->next->bfd2got = NULL;
4399 }
4400 else
4401 g->next = got_per_bfd_arg.primary;
4402 g->next->next = got_per_bfd_arg.current;
4403
4404 /* GG is now the master GOT, and G is the primary GOT. */
4405 gg = g;
4406 g = g->next;
4407
4408 /* Map the output bfd to the primary got. That's what we're going
4409 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4410 didn't mark in check_relocs, and we want a quick way to find it.
4411 We can't just use gg->next because we're going to reverse the
4412 list. */
4413 {
4414 struct mips_elf_bfd2got_hash *bfdgot;
4415 void **bfdgotp;
143d77c5 4416
f4416af6
AO
4417 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4418 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4419
4420 if (bfdgot == NULL)
4421 return FALSE;
4422
4423 bfdgot->bfd = abfd;
4424 bfdgot->g = g;
4425 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4426
4427 BFD_ASSERT (*bfdgotp == NULL);
4428 *bfdgotp = bfdgot;
4429 }
4430
634835ae
RS
4431 /* Every symbol that is referenced in a dynamic relocation must be
4432 present in the primary GOT, so arrange for them to appear after
4433 those that are actually referenced. */
23cc69b6 4434 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4435 g->global_gotno = gg->global_gotno;
f4416af6 4436
f4416af6 4437 set_got_offset_arg.g = NULL;
634835ae 4438 set_got_offset_arg.value = GGA_RELOC_ONLY;
f4416af6
AO
4439 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4440 &set_got_offset_arg);
634835ae 4441 set_got_offset_arg.value = GGA_NORMAL;
f4416af6
AO
4442 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4443 &set_got_offset_arg);
f4416af6
AO
4444
4445 /* Now go through the GOTs assigning them offset ranges.
4446 [assigned_gotno, local_gotno[ will be set to the range of local
4447 entries in each GOT. We can then compute the end of a GOT by
4448 adding local_gotno to global_gotno. We reverse the list and make
4449 it circular since then we'll be able to quickly compute the
4450 beginning of a GOT, by computing the end of its predecessor. To
4451 avoid special cases for the primary GOT, while still preserving
4452 assertions that are valid for both single- and multi-got links,
4453 we arrange for the main got struct to have the right number of
4454 global entries, but set its local_gotno such that the initial
4455 offset of the primary GOT is zero. Remember that the primary GOT
4456 will become the last item in the circular linked list, so it
4457 points back to the master GOT. */
4458 gg->local_gotno = -g->global_gotno;
4459 gg->global_gotno = g->global_gotno;
0f20cc35 4460 gg->tls_gotno = 0;
f4416af6
AO
4461 assign = 0;
4462 gg->next = gg;
4463
4464 do
4465 {
4466 struct mips_got_info *gn;
4467
861fb55a 4468 assign += htab->reserved_gotno;
f4416af6 4469 g->assigned_gotno = assign;
c224138d
RS
4470 g->local_gotno += assign;
4471 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4472 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4473
ead49a57
RS
4474 /* Take g out of the direct list, and push it onto the reversed
4475 list that gg points to. g->next is guaranteed to be nonnull after
4476 this operation, as required by mips_elf_initialize_tls_index. */
4477 gn = g->next;
4478 g->next = gg->next;
4479 gg->next = g;
4480
0f20cc35
DJ
4481 /* Set up any TLS entries. We always place the TLS entries after
4482 all non-TLS entries. */
4483 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4484 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
f4416af6 4485
ead49a57 4486 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4487 g = gn;
0626d451 4488
33bb52fb
RS
4489 /* Forbid global symbols in every non-primary GOT from having
4490 lazy-binding stubs. */
0626d451 4491 if (g)
33bb52fb 4492 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4493 }
4494 while (g);
4495
eea6121a 4496 got->size = (gg->next->local_gotno
33bb52fb
RS
4497 + gg->next->global_gotno
4498 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4499
4500 needed_relocs = 0;
4501 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4502 set_got_offset_arg.info = info;
4503 for (g = gg->next; g && g->next != gg; g = g->next)
4504 {
4505 unsigned int save_assign;
4506
4507 /* Assign offsets to global GOT entries. */
4508 save_assign = g->assigned_gotno;
4509 g->assigned_gotno = g->local_gotno;
4510 set_got_offset_arg.g = g;
4511 set_got_offset_arg.needed_relocs = 0;
4512 htab_traverse (g->got_entries,
4513 mips_elf_set_global_got_offset,
4514 &set_got_offset_arg);
4515 needed_relocs += set_got_offset_arg.needed_relocs;
4516 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4517
4518 g->assigned_gotno = save_assign;
4519 if (info->shared)
4520 {
4521 needed_relocs += g->local_gotno - g->assigned_gotno;
4522 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4523 + g->next->global_gotno
4524 + g->next->tls_gotno
861fb55a 4525 + htab->reserved_gotno);
33bb52fb
RS
4526 }
4527 }
4528
4529 if (needed_relocs)
4530 mips_elf_allocate_dynamic_relocations (dynobj, info,
4531 needed_relocs);
143d77c5 4532
f4416af6
AO
4533 return TRUE;
4534}
143d77c5 4535
b49e97c9
TS
4536\f
4537/* Returns the first relocation of type r_type found, beginning with
4538 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4539
4540static const Elf_Internal_Rela *
9719ad41
RS
4541mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4542 const Elf_Internal_Rela *relocation,
4543 const Elf_Internal_Rela *relend)
b49e97c9 4544{
c000e262
TS
4545 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4546
b49e97c9
TS
4547 while (relocation < relend)
4548 {
c000e262
TS
4549 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4550 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4551 return relocation;
4552
4553 ++relocation;
4554 }
4555
4556 /* We didn't find it. */
b49e97c9
TS
4557 return NULL;
4558}
4559
4560/* Return whether a relocation is against a local symbol. */
4561
b34976b6 4562static bfd_boolean
9719ad41
RS
4563mips_elf_local_relocation_p (bfd *input_bfd,
4564 const Elf_Internal_Rela *relocation,
4565 asection **local_sections,
4566 bfd_boolean check_forced)
b49e97c9
TS
4567{
4568 unsigned long r_symndx;
4569 Elf_Internal_Shdr *symtab_hdr;
4570 struct mips_elf_link_hash_entry *h;
4571 size_t extsymoff;
4572
4573 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4574 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4575 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4576
4577 if (r_symndx < extsymoff)
b34976b6 4578 return TRUE;
b49e97c9 4579 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4580 return TRUE;
b49e97c9
TS
4581
4582 if (check_forced)
4583 {
4584 /* Look up the hash table to check whether the symbol
4585 was forced local. */
4586 h = (struct mips_elf_link_hash_entry *)
4587 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
4588 /* Find the real hash-table entry for this symbol. */
4589 while (h->root.root.type == bfd_link_hash_indirect
4590 || h->root.root.type == bfd_link_hash_warning)
4591 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
f5385ebf 4592 if (h->root.forced_local)
b34976b6 4593 return TRUE;
b49e97c9
TS
4594 }
4595
b34976b6 4596 return FALSE;
b49e97c9
TS
4597}
4598\f
4599/* Sign-extend VALUE, which has the indicated number of BITS. */
4600
a7ebbfdf 4601bfd_vma
9719ad41 4602_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4603{
4604 if (value & ((bfd_vma) 1 << (bits - 1)))
4605 /* VALUE is negative. */
4606 value |= ((bfd_vma) - 1) << bits;
4607
4608 return value;
4609}
4610
4611/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4612 range expressible by a signed number with the indicated number of
b49e97c9
TS
4613 BITS. */
4614
b34976b6 4615static bfd_boolean
9719ad41 4616mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4617{
4618 bfd_signed_vma svalue = (bfd_signed_vma) value;
4619
4620 if (svalue > (1 << (bits - 1)) - 1)
4621 /* The value is too big. */
b34976b6 4622 return TRUE;
b49e97c9
TS
4623 else if (svalue < -(1 << (bits - 1)))
4624 /* The value is too small. */
b34976b6 4625 return TRUE;
b49e97c9
TS
4626
4627 /* All is well. */
b34976b6 4628 return FALSE;
b49e97c9
TS
4629}
4630
4631/* Calculate the %high function. */
4632
4633static bfd_vma
9719ad41 4634mips_elf_high (bfd_vma value)
b49e97c9
TS
4635{
4636 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4637}
4638
4639/* Calculate the %higher function. */
4640
4641static bfd_vma
9719ad41 4642mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4643{
4644#ifdef BFD64
4645 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4646#else
4647 abort ();
c5ae1840 4648 return MINUS_ONE;
b49e97c9
TS
4649#endif
4650}
4651
4652/* Calculate the %highest function. */
4653
4654static bfd_vma
9719ad41 4655mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4656{
4657#ifdef BFD64
b15e6682 4658 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4659#else
4660 abort ();
c5ae1840 4661 return MINUS_ONE;
b49e97c9
TS
4662#endif
4663}
4664\f
4665/* Create the .compact_rel section. */
4666
b34976b6 4667static bfd_boolean
9719ad41
RS
4668mips_elf_create_compact_rel_section
4669 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4670{
4671 flagword flags;
4672 register asection *s;
4673
4674 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4675 {
4676 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4677 | SEC_READONLY);
4678
3496cb2a 4679 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4680 if (s == NULL
b49e97c9
TS
4681 || ! bfd_set_section_alignment (abfd, s,
4682 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4683 return FALSE;
b49e97c9 4684
eea6121a 4685 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4686 }
4687
b34976b6 4688 return TRUE;
b49e97c9
TS
4689}
4690
4691/* Create the .got section to hold the global offset table. */
4692
b34976b6 4693static bfd_boolean
23cc69b6 4694mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
4695{
4696 flagword flags;
4697 register asection *s;
4698 struct elf_link_hash_entry *h;
14a793b2 4699 struct bfd_link_hash_entry *bh;
b49e97c9
TS
4700 struct mips_got_info *g;
4701 bfd_size_type amt;
0a44bf69
RS
4702 struct mips_elf_link_hash_table *htab;
4703
4704 htab = mips_elf_hash_table (info);
4dfe6ac6 4705 BFD_ASSERT (htab != NULL);
b49e97c9
TS
4706
4707 /* This function may be called more than once. */
23cc69b6
RS
4708 if (htab->sgot)
4709 return TRUE;
b49e97c9
TS
4710
4711 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4712 | SEC_LINKER_CREATED);
4713
72b4917c
TS
4714 /* We have to use an alignment of 2**4 here because this is hardcoded
4715 in the function stub generation and in the linker script. */
3496cb2a 4716 s = bfd_make_section_with_flags (abfd, ".got", flags);
b49e97c9 4717 if (s == NULL
72b4917c 4718 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 4719 return FALSE;
a8028dd0 4720 htab->sgot = s;
b49e97c9
TS
4721
4722 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4723 linker script because we don't want to define the symbol if we
4724 are not creating a global offset table. */
14a793b2 4725 bh = NULL;
b49e97c9
TS
4726 if (! (_bfd_generic_link_add_one_symbol
4727 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 4728 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 4729 return FALSE;
14a793b2
AM
4730
4731 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
4732 h->non_elf = 0;
4733 h->def_regular = 1;
b49e97c9 4734 h->type = STT_OBJECT;
d329bcd1 4735 elf_hash_table (info)->hgot = h;
b49e97c9
TS
4736
4737 if (info->shared
c152c796 4738 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4739 return FALSE;
b49e97c9 4740
b49e97c9 4741 amt = sizeof (struct mips_got_info);
9719ad41 4742 g = bfd_alloc (abfd, amt);
b49e97c9 4743 if (g == NULL)
b34976b6 4744 return FALSE;
b49e97c9 4745 g->global_gotsym = NULL;
e3d54347 4746 g->global_gotno = 0;
23cc69b6 4747 g->reloc_only_gotno = 0;
0f20cc35 4748 g->tls_gotno = 0;
861fb55a 4749 g->local_gotno = 0;
c224138d 4750 g->page_gotno = 0;
861fb55a 4751 g->assigned_gotno = 0;
f4416af6
AO
4752 g->bfd2got = NULL;
4753 g->next = NULL;
0f20cc35 4754 g->tls_ldm_offset = MINUS_ONE;
b15e6682 4755 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
9719ad41 4756 mips_elf_got_entry_eq, NULL);
b15e6682
AO
4757 if (g->got_entries == NULL)
4758 return FALSE;
c224138d
RS
4759 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4760 mips_got_page_entry_eq, NULL);
4761 if (g->got_page_entries == NULL)
4762 return FALSE;
a8028dd0 4763 htab->got_info = g;
f0abc2a1 4764 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
4765 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4766
861fb55a
DJ
4767 /* We also need a .got.plt section when generating PLTs. */
4768 s = bfd_make_section_with_flags (abfd, ".got.plt",
4769 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4770 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4771 if (s == NULL)
4772 return FALSE;
4773 htab->sgotplt = s;
0a44bf69 4774
b34976b6 4775 return TRUE;
b49e97c9 4776}
b49e97c9 4777\f
0a44bf69
RS
4778/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4779 __GOTT_INDEX__ symbols. These symbols are only special for
4780 shared objects; they are not used in executables. */
4781
4782static bfd_boolean
4783is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4784{
4785 return (mips_elf_hash_table (info)->is_vxworks
4786 && info->shared
4787 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4788 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4789}
861fb55a
DJ
4790
4791/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4792 require an la25 stub. See also mips_elf_local_pic_function_p,
4793 which determines whether the destination function ever requires a
4794 stub. */
4795
4796static bfd_boolean
4797mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4798{
4799 /* We specifically ignore branches and jumps from EF_PIC objects,
4800 where the onus is on the compiler or programmer to perform any
4801 necessary initialization of $25. Sometimes such initialization
4802 is unnecessary; for example, -mno-shared functions do not use
4803 the incoming value of $25, and may therefore be called directly. */
4804 if (PIC_OBJECT_P (input_bfd))
4805 return FALSE;
4806
4807 switch (r_type)
4808 {
4809 case R_MIPS_26:
4810 case R_MIPS_PC16:
4811 case R_MIPS16_26:
4812 return TRUE;
4813
4814 default:
4815 return FALSE;
4816 }
4817}
0a44bf69 4818\f
b49e97c9
TS
4819/* Calculate the value produced by the RELOCATION (which comes from
4820 the INPUT_BFD). The ADDEND is the addend to use for this
4821 RELOCATION; RELOCATION->R_ADDEND is ignored.
4822
4823 The result of the relocation calculation is stored in VALUEP.
38a7df63
CF
4824 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4825 is a MIPS16 jump to non-MIPS16 code, or vice versa.
b49e97c9
TS
4826
4827 This function returns bfd_reloc_continue if the caller need take no
4828 further action regarding this relocation, bfd_reloc_notsupported if
4829 something goes dramatically wrong, bfd_reloc_overflow if an
4830 overflow occurs, and bfd_reloc_ok to indicate success. */
4831
4832static bfd_reloc_status_type
9719ad41
RS
4833mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4834 asection *input_section,
4835 struct bfd_link_info *info,
4836 const Elf_Internal_Rela *relocation,
4837 bfd_vma addend, reloc_howto_type *howto,
4838 Elf_Internal_Sym *local_syms,
4839 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
4840 const char **namep,
4841 bfd_boolean *cross_mode_jump_p,
9719ad41 4842 bfd_boolean save_addend)
b49e97c9
TS
4843{
4844 /* The eventual value we will return. */
4845 bfd_vma value;
4846 /* The address of the symbol against which the relocation is
4847 occurring. */
4848 bfd_vma symbol = 0;
4849 /* The final GP value to be used for the relocatable, executable, or
4850 shared object file being produced. */
0a61c8c2 4851 bfd_vma gp;
b49e97c9
TS
4852 /* The place (section offset or address) of the storage unit being
4853 relocated. */
4854 bfd_vma p;
4855 /* The value of GP used to create the relocatable object. */
0a61c8c2 4856 bfd_vma gp0;
b49e97c9
TS
4857 /* The offset into the global offset table at which the address of
4858 the relocation entry symbol, adjusted by the addend, resides
4859 during execution. */
4860 bfd_vma g = MINUS_ONE;
4861 /* The section in which the symbol referenced by the relocation is
4862 located. */
4863 asection *sec = NULL;
4864 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 4865 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 4866 symbol. */
b34976b6
AM
4867 bfd_boolean local_p, was_local_p;
4868 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4869 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
4870 /* TRUE if the symbol referred to by this relocation is
4871 "__gnu_local_gp". */
4872 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
4873 Elf_Internal_Shdr *symtab_hdr;
4874 size_t extsymoff;
4875 unsigned long r_symndx;
4876 int r_type;
b34976b6 4877 /* TRUE if overflow occurred during the calculation of the
b49e97c9 4878 relocation value. */
b34976b6
AM
4879 bfd_boolean overflowed_p;
4880 /* TRUE if this relocation refers to a MIPS16 function. */
4881 bfd_boolean target_is_16_bit_code_p = FALSE;
0a44bf69
RS
4882 struct mips_elf_link_hash_table *htab;
4883 bfd *dynobj;
4884
4885 dynobj = elf_hash_table (info)->dynobj;
4886 htab = mips_elf_hash_table (info);
4dfe6ac6 4887 BFD_ASSERT (htab != NULL);
b49e97c9
TS
4888
4889 /* Parse the relocation. */
4890 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4891 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4892 p = (input_section->output_section->vma
4893 + input_section->output_offset
4894 + relocation->r_offset);
4895
4896 /* Assume that there will be no overflow. */
b34976b6 4897 overflowed_p = FALSE;
b49e97c9
TS
4898
4899 /* Figure out whether or not the symbol is local, and get the offset
4900 used in the array of hash table entries. */
4901 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4902 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 4903 local_sections, FALSE);
bce03d3d 4904 was_local_p = local_p;
b49e97c9
TS
4905 if (! elf_bad_symtab (input_bfd))
4906 extsymoff = symtab_hdr->sh_info;
4907 else
4908 {
4909 /* The symbol table does not follow the rule that local symbols
4910 must come before globals. */
4911 extsymoff = 0;
4912 }
4913
4914 /* Figure out the value of the symbol. */
4915 if (local_p)
4916 {
4917 Elf_Internal_Sym *sym;
4918
4919 sym = local_syms + r_symndx;
4920 sec = local_sections[r_symndx];
4921
4922 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
4923 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4924 || (sec->flags & SEC_MERGE))
b49e97c9 4925 symbol += sym->st_value;
d4df96e6
L
4926 if ((sec->flags & SEC_MERGE)
4927 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4928 {
4929 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4930 addend -= symbol;
4931 addend += sec->output_section->vma + sec->output_offset;
4932 }
b49e97c9
TS
4933
4934 /* MIPS16 text labels should be treated as odd. */
30c09090 4935 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
4936 ++symbol;
4937
4938 /* Record the name of this symbol, for our caller. */
4939 *namep = bfd_elf_string_from_elf_section (input_bfd,
4940 symtab_hdr->sh_link,
4941 sym->st_name);
4942 if (*namep == '\0')
4943 *namep = bfd_section_name (input_bfd, sec);
4944
30c09090 4945 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
b49e97c9
TS
4946 }
4947 else
4948 {
560e09e9
NC
4949 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4950
b49e97c9
TS
4951 /* For global symbols we look up the symbol in the hash-table. */
4952 h = ((struct mips_elf_link_hash_entry *)
4953 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4954 /* Find the real hash-table entry for this symbol. */
4955 while (h->root.root.type == bfd_link_hash_indirect
4956 || h->root.root.type == bfd_link_hash_warning)
4957 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4958
4959 /* Record the name of this symbol, for our caller. */
4960 *namep = h->root.root.root.string;
4961
4962 /* See if this is the special _gp_disp symbol. Note that such a
4963 symbol must always be a global symbol. */
560e09e9 4964 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
4965 && ! NEWABI_P (input_bfd))
4966 {
4967 /* Relocations against _gp_disp are permitted only with
4968 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 4969 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
4970 return bfd_reloc_notsupported;
4971
b34976b6 4972 gp_disp_p = TRUE;
b49e97c9 4973 }
bbe506e8
TS
4974 /* See if this is the special _gp symbol. Note that such a
4975 symbol must always be a global symbol. */
4976 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4977 gnu_local_gp_p = TRUE;
4978
4979
b49e97c9
TS
4980 /* If this symbol is defined, calculate its address. Note that
4981 _gp_disp is a magic symbol, always implicitly defined by the
4982 linker, so it's inappropriate to check to see whether or not
4983 its defined. */
4984 else if ((h->root.root.type == bfd_link_hash_defined
4985 || h->root.root.type == bfd_link_hash_defweak)
4986 && h->root.root.u.def.section)
4987 {
4988 sec = h->root.root.u.def.section;
4989 if (sec->output_section)
4990 symbol = (h->root.root.u.def.value
4991 + sec->output_section->vma
4992 + sec->output_offset);
4993 else
4994 symbol = h->root.root.u.def.value;
4995 }
4996 else if (h->root.root.type == bfd_link_hash_undefweak)
4997 /* We allow relocations against undefined weak symbols, giving
4998 it the value zero, so that you can undefined weak functions
4999 and check to see if they exist by looking at their
5000 addresses. */
5001 symbol = 0;
59c2e50f 5002 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5003 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5004 symbol = 0;
a4d0f181
TS
5005 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5006 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5007 {
5008 /* If this is a dynamic link, we should have created a
5009 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5010 in in _bfd_mips_elf_create_dynamic_sections.
5011 Otherwise, we should define the symbol with a value of 0.
5012 FIXME: It should probably get into the symbol table
5013 somehow as well. */
5014 BFD_ASSERT (! info->shared);
5015 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5016 symbol = 0;
5017 }
5e2b0d47
NC
5018 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5019 {
5020 /* This is an optional symbol - an Irix specific extension to the
5021 ELF spec. Ignore it for now.
5022 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5023 than simply ignoring them, but we do not handle this for now.
5024 For information see the "64-bit ELF Object File Specification"
5025 which is available from here:
5026 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5027 symbol = 0;
5028 }
e7e2196d
MR
5029 else if ((*info->callbacks->undefined_symbol)
5030 (info, h->root.root.root.string, input_bfd,
5031 input_section, relocation->r_offset,
5032 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5033 || ELF_ST_VISIBILITY (h->root.other)))
5034 {
5035 return bfd_reloc_undefined;
5036 }
b49e97c9
TS
5037 else
5038 {
e7e2196d 5039 return bfd_reloc_notsupported;
b49e97c9
TS
5040 }
5041
30c09090 5042 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
b49e97c9
TS
5043 }
5044
738e5348
RS
5045 /* If this is a reference to a 16-bit function with a stub, we need
5046 to redirect the relocation to the stub unless:
5047
5048 (a) the relocation is for a MIPS16 JAL;
5049
5050 (b) the relocation is for a MIPS16 PIC call, and there are no
5051 non-MIPS16 uses of the GOT slot; or
5052
5053 (c) the section allows direct references to MIPS16 functions. */
5054 if (r_type != R_MIPS16_26
5055 && !info->relocatable
5056 && ((h != NULL
5057 && h->fn_stub != NULL
5058 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71
TS
5059 || (local_p
5060 && elf_tdata (input_bfd)->local_stubs != NULL
b49e97c9 5061 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5062 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5063 {
5064 /* This is a 32- or 64-bit call to a 16-bit function. We should
5065 have already noticed that we were going to need the
5066 stub. */
5067 if (local_p)
5068 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5069 else
5070 {
5071 BFD_ASSERT (h->need_fn_stub);
5072 sec = h->fn_stub;
5073 }
5074
5075 symbol = sec->output_section->vma + sec->output_offset;
f38c2df5
TS
5076 /* The target is 16-bit, but the stub isn't. */
5077 target_is_16_bit_code_p = FALSE;
b49e97c9
TS
5078 }
5079 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
738e5348
RS
5080 need to redirect the call to the stub. Note that we specifically
5081 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5082 use an indirect stub instead. */
1049f94e 5083 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5084 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71
TS
5085 || (local_p
5086 && elf_tdata (input_bfd)->local_call_stubs != NULL
5087 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
b49e97c9
TS
5088 && !target_is_16_bit_code_p)
5089 {
b9d58d71
TS
5090 if (local_p)
5091 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5092 else
b49e97c9 5093 {
b9d58d71
TS
5094 /* If both call_stub and call_fp_stub are defined, we can figure
5095 out which one to use by checking which one appears in the input
5096 file. */
5097 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5098 {
b9d58d71
TS
5099 asection *o;
5100
5101 sec = NULL;
5102 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5103 {
b9d58d71
TS
5104 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5105 {
5106 sec = h->call_fp_stub;
5107 break;
5108 }
b49e97c9 5109 }
b9d58d71
TS
5110 if (sec == NULL)
5111 sec = h->call_stub;
b49e97c9 5112 }
b9d58d71 5113 else if (h->call_stub != NULL)
b49e97c9 5114 sec = h->call_stub;
b9d58d71
TS
5115 else
5116 sec = h->call_fp_stub;
5117 }
b49e97c9 5118
eea6121a 5119 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5120 symbol = sec->output_section->vma + sec->output_offset;
5121 }
861fb55a
DJ
5122 /* If this is a direct call to a PIC function, redirect to the
5123 non-PIC stub. */
5124 else if (h != NULL && h->la25_stub
5125 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5126 symbol = (h->la25_stub->stub_section->output_section->vma
5127 + h->la25_stub->stub_section->output_offset
5128 + h->la25_stub->offset);
b49e97c9
TS
5129
5130 /* Calls from 16-bit code to 32-bit code and vice versa require the
38a7df63
CF
5131 mode change. */
5132 *cross_mode_jump_p = !info->relocatable
5133 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5134 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5135 && target_is_16_bit_code_p));
b49e97c9
TS
5136
5137 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 5138 local_sections, TRUE);
b49e97c9 5139
0a61c8c2
RS
5140 gp0 = _bfd_get_gp_value (input_bfd);
5141 gp = _bfd_get_gp_value (abfd);
23cc69b6 5142 if (htab->got_info)
a8028dd0 5143 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5144
5145 if (gnu_local_gp_p)
5146 symbol = gp;
5147
5148 /* If we haven't already determined the GOT offset, oand we're going
5149 to need it, get it now. */
b49e97c9
TS
5150 switch (r_type)
5151 {
0fdc1bf1 5152 case R_MIPS_GOT_PAGE:
93a2b7ae 5153 case R_MIPS_GOT_OFST:
d25aed71
RS
5154 /* We need to decay to GOT_DISP/addend if the symbol doesn't
5155 bind locally. */
5156 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
93a2b7ae 5157 if (local_p || r_type == R_MIPS_GOT_OFST)
0fdc1bf1
AO
5158 break;
5159 /* Fall through. */
5160
738e5348
RS
5161 case R_MIPS16_CALL16:
5162 case R_MIPS16_GOT16:
b49e97c9
TS
5163 case R_MIPS_CALL16:
5164 case R_MIPS_GOT16:
5165 case R_MIPS_GOT_DISP:
5166 case R_MIPS_GOT_HI16:
5167 case R_MIPS_CALL_HI16:
5168 case R_MIPS_GOT_LO16:
5169 case R_MIPS_CALL_LO16:
0f20cc35
DJ
5170 case R_MIPS_TLS_GD:
5171 case R_MIPS_TLS_GOTTPREL:
5172 case R_MIPS_TLS_LDM:
b49e97c9 5173 /* Find the index into the GOT where this value is located. */
0f20cc35
DJ
5174 if (r_type == R_MIPS_TLS_LDM)
5175 {
0a44bf69 5176 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5177 0, 0, NULL, r_type);
0f20cc35
DJ
5178 if (g == MINUS_ONE)
5179 return bfd_reloc_outofrange;
5180 }
5181 else if (!local_p)
b49e97c9 5182 {
0a44bf69
RS
5183 /* On VxWorks, CALL relocations should refer to the .got.plt
5184 entry, which is initialized to point at the PLT stub. */
5185 if (htab->is_vxworks
5186 && (r_type == R_MIPS_CALL_HI16
5187 || r_type == R_MIPS_CALL_LO16
738e5348 5188 || call16_reloc_p (r_type)))
0a44bf69
RS
5189 {
5190 BFD_ASSERT (addend == 0);
5191 BFD_ASSERT (h->root.needs_plt);
5192 g = mips_elf_gotplt_index (info, &h->root);
5193 }
5194 else
b49e97c9 5195 {
0a44bf69
RS
5196 /* GOT_PAGE may take a non-zero addend, that is ignored in a
5197 GOT_PAGE relocation that decays to GOT_DISP because the
5198 symbol turns out to be global. The addend is then added
5199 as GOT_OFST. */
5200 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
5201 g = mips_elf_global_got_index (dynobj, input_bfd,
5202 &h->root, r_type, info);
5203 if (h->tls_type == GOT_NORMAL
5204 && (! elf_hash_table(info)->dynamic_sections_created
5205 || (info->shared
5206 && (info->symbolic || h->root.forced_local)
5207 && h->root.def_regular)))
a8028dd0
RS
5208 /* This is a static link or a -Bsymbolic link. The
5209 symbol is defined locally, or was forced to be local.
5210 We must initialize this entry in the GOT. */
5211 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5212 }
5213 }
0a44bf69 5214 else if (!htab->is_vxworks
738e5348 5215 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5216 /* The calculation below does not involve "g". */
b49e97c9
TS
5217 break;
5218 else
5219 {
5c18022e 5220 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5221 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5222 if (g == MINUS_ONE)
5223 return bfd_reloc_outofrange;
5224 }
5225
5226 /* Convert GOT indices to actual offsets. */
a8028dd0 5227 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5228 break;
b49e97c9
TS
5229 }
5230
0a44bf69
RS
5231 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5232 symbols are resolved by the loader. Add them to .rela.dyn. */
5233 if (h != NULL && is_gott_symbol (info, &h->root))
5234 {
5235 Elf_Internal_Rela outrel;
5236 bfd_byte *loc;
5237 asection *s;
5238
5239 s = mips_elf_rel_dyn_section (info, FALSE);
5240 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5241
5242 outrel.r_offset = (input_section->output_section->vma
5243 + input_section->output_offset
5244 + relocation->r_offset);
5245 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5246 outrel.r_addend = addend;
5247 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5248
5249 /* If we've written this relocation for a readonly section,
5250 we need to set DF_TEXTREL again, so that we do not delete the
5251 DT_TEXTREL tag. */
5252 if (MIPS_ELF_READONLY_SECTION (input_section))
5253 info->flags |= DF_TEXTREL;
5254
0a44bf69
RS
5255 *valuep = 0;
5256 return bfd_reloc_ok;
5257 }
5258
b49e97c9
TS
5259 /* Figure out what kind of relocation is being performed. */
5260 switch (r_type)
5261 {
5262 case R_MIPS_NONE:
5263 return bfd_reloc_continue;
5264
5265 case R_MIPS_16:
a7ebbfdf 5266 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5267 overflowed_p = mips_elf_overflow_p (value, 16);
5268 break;
5269
5270 case R_MIPS_32:
5271 case R_MIPS_REL32:
5272 case R_MIPS_64:
5273 if ((info->shared
861fb55a 5274 || (htab->root.dynamic_sections_created
b49e97c9 5275 && h != NULL
f5385ebf 5276 && h->root.def_dynamic
861fb55a
DJ
5277 && !h->root.def_regular
5278 && !h->has_static_relocs))
b49e97c9 5279 && r_symndx != 0
9a59ad6b
DJ
5280 && (h == NULL
5281 || h->root.root.type != bfd_link_hash_undefweak
5282 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5283 && (input_section->flags & SEC_ALLOC) != 0)
5284 {
861fb55a 5285 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5286 where the symbol will end up. So, we create a relocation
5287 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5288 linker. We must do the same for executable references to
5289 shared library symbols, unless we've decided to use copy
5290 relocs or PLTs instead. */
b49e97c9
TS
5291 value = addend;
5292 if (!mips_elf_create_dynamic_relocation (abfd,
5293 info,
5294 relocation,
5295 h,
5296 sec,
5297 symbol,
5298 &value,
5299 input_section))
5300 return bfd_reloc_undefined;
5301 }
5302 else
5303 {
5304 if (r_type != R_MIPS_REL32)
5305 value = symbol + addend;
5306 else
5307 value = addend;
5308 }
5309 value &= howto->dst_mask;
092dcd75
CD
5310 break;
5311
5312 case R_MIPS_PC32:
5313 value = symbol + addend - p;
5314 value &= howto->dst_mask;
b49e97c9
TS
5315 break;
5316
b49e97c9
TS
5317 case R_MIPS16_26:
5318 /* The calculation for R_MIPS16_26 is just the same as for an
5319 R_MIPS_26. It's only the storage of the relocated field into
5320 the output file that's different. That's handled in
5321 mips_elf_perform_relocation. So, we just fall through to the
5322 R_MIPS_26 case here. */
5323 case R_MIPS_26:
5324 if (local_p)
30ac9238 5325 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
b49e97c9 5326 else
728b2f21
ILT
5327 {
5328 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
c314987d
RS
5329 if (h->root.root.type != bfd_link_hash_undefweak)
5330 overflowed_p = (value >> 26) != ((p + 4) >> 28);
728b2f21 5331 }
b49e97c9
TS
5332 value &= howto->dst_mask;
5333 break;
5334
0f20cc35
DJ
5335 case R_MIPS_TLS_DTPREL_HI16:
5336 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5337 & howto->dst_mask);
5338 break;
5339
5340 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5341 case R_MIPS_TLS_DTPREL32:
5342 case R_MIPS_TLS_DTPREL64:
0f20cc35
DJ
5343 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5344 break;
5345
5346 case R_MIPS_TLS_TPREL_HI16:
5347 value = (mips_elf_high (addend + symbol - tprel_base (info))
5348 & howto->dst_mask);
5349 break;
5350
5351 case R_MIPS_TLS_TPREL_LO16:
5352 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5353 break;
5354
b49e97c9 5355 case R_MIPS_HI16:
d6f16593 5356 case R_MIPS16_HI16:
b49e97c9
TS
5357 if (!gp_disp_p)
5358 {
5359 value = mips_elf_high (addend + symbol);
5360 value &= howto->dst_mask;
5361 }
5362 else
5363 {
d6f16593
MR
5364 /* For MIPS16 ABI code we generate this sequence
5365 0: li $v0,%hi(_gp_disp)
5366 4: addiupc $v1,%lo(_gp_disp)
5367 8: sll $v0,16
5368 12: addu $v0,$v1
5369 14: move $gp,$v0
5370 So the offsets of hi and lo relocs are the same, but the
5371 $pc is four higher than $t9 would be, so reduce
5372 both reloc addends by 4. */
5373 if (r_type == R_MIPS16_HI16)
5374 value = mips_elf_high (addend + gp - p - 4);
5375 else
5376 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5377 overflowed_p = mips_elf_overflow_p (value, 16);
5378 }
5379 break;
5380
5381 case R_MIPS_LO16:
d6f16593 5382 case R_MIPS16_LO16:
b49e97c9
TS
5383 if (!gp_disp_p)
5384 value = (symbol + addend) & howto->dst_mask;
5385 else
5386 {
d6f16593
MR
5387 /* See the comment for R_MIPS16_HI16 above for the reason
5388 for this conditional. */
5389 if (r_type == R_MIPS16_LO16)
5390 value = addend + gp - p;
5391 else
5392 value = addend + gp - p + 4;
b49e97c9 5393 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5394 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5395 _gp_disp are normally generated from the .cpload
5396 pseudo-op. It generates code that normally looks like
5397 this:
5398
5399 lui $gp,%hi(_gp_disp)
5400 addiu $gp,$gp,%lo(_gp_disp)
5401 addu $gp,$gp,$t9
5402
5403 Here $t9 holds the address of the function being called,
5404 as required by the MIPS ELF ABI. The R_MIPS_LO16
5405 relocation can easily overflow in this situation, but the
5406 R_MIPS_HI16 relocation will handle the overflow.
5407 Therefore, we consider this a bug in the MIPS ABI, and do
5408 not check for overflow here. */
5409 }
5410 break;
5411
5412 case R_MIPS_LITERAL:
5413 /* Because we don't merge literal sections, we can handle this
5414 just like R_MIPS_GPREL16. In the long run, we should merge
5415 shared literals, and then we will need to additional work
5416 here. */
5417
5418 /* Fall through. */
5419
5420 case R_MIPS16_GPREL:
5421 /* The R_MIPS16_GPREL performs the same calculation as
5422 R_MIPS_GPREL16, but stores the relocated bits in a different
5423 order. We don't need to do anything special here; the
5424 differences are handled in mips_elf_perform_relocation. */
5425 case R_MIPS_GPREL16:
bce03d3d
AO
5426 /* Only sign-extend the addend if it was extracted from the
5427 instruction. If the addend was separate, leave it alone,
5428 otherwise we may lose significant bits. */
5429 if (howto->partial_inplace)
a7ebbfdf 5430 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5431 value = symbol + addend - gp;
5432 /* If the symbol was local, any earlier relocatable links will
5433 have adjusted its addend with the gp offset, so compensate
5434 for that now. Don't do it for symbols forced local in this
5435 link, though, since they won't have had the gp offset applied
5436 to them before. */
5437 if (was_local_p)
5438 value += gp0;
b49e97c9
TS
5439 overflowed_p = mips_elf_overflow_p (value, 16);
5440 break;
5441
738e5348
RS
5442 case R_MIPS16_GOT16:
5443 case R_MIPS16_CALL16:
b49e97c9
TS
5444 case R_MIPS_GOT16:
5445 case R_MIPS_CALL16:
0a44bf69 5446 /* VxWorks does not have separate local and global semantics for
738e5348 5447 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5448 if (!htab->is_vxworks && local_p)
b49e97c9 5449 {
b34976b6 5450 bfd_boolean forced;
b49e97c9 5451
b49e97c9 5452 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 5453 local_sections, FALSE);
5c18022e 5454 value = mips_elf_got16_entry (abfd, input_bfd, info,
f4416af6 5455 symbol + addend, forced);
b49e97c9
TS
5456 if (value == MINUS_ONE)
5457 return bfd_reloc_outofrange;
5458 value
a8028dd0 5459 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5460 overflowed_p = mips_elf_overflow_p (value, 16);
5461 break;
5462 }
5463
5464 /* Fall through. */
5465
0f20cc35
DJ
5466 case R_MIPS_TLS_GD:
5467 case R_MIPS_TLS_GOTTPREL:
5468 case R_MIPS_TLS_LDM:
b49e97c9 5469 case R_MIPS_GOT_DISP:
0fdc1bf1 5470 got_disp:
b49e97c9
TS
5471 value = g;
5472 overflowed_p = mips_elf_overflow_p (value, 16);
5473 break;
5474
5475 case R_MIPS_GPREL32:
bce03d3d
AO
5476 value = (addend + symbol + gp0 - gp);
5477 if (!save_addend)
5478 value &= howto->dst_mask;
b49e97c9
TS
5479 break;
5480
5481 case R_MIPS_PC16:
bad36eac
DJ
5482 case R_MIPS_GNU_REL16_S2:
5483 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5484 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5485 value >>= howto->rightshift;
5486 value &= howto->dst_mask;
b49e97c9
TS
5487 break;
5488
5489 case R_MIPS_GOT_HI16:
5490 case R_MIPS_CALL_HI16:
5491 /* We're allowed to handle these two relocations identically.
5492 The dynamic linker is allowed to handle the CALL relocations
5493 differently by creating a lazy evaluation stub. */
5494 value = g;
5495 value = mips_elf_high (value);
5496 value &= howto->dst_mask;
5497 break;
5498
5499 case R_MIPS_GOT_LO16:
5500 case R_MIPS_CALL_LO16:
5501 value = g & howto->dst_mask;
5502 break;
5503
5504 case R_MIPS_GOT_PAGE:
0fdc1bf1
AO
5505 /* GOT_PAGE relocations that reference non-local symbols decay
5506 to GOT_DISP. The corresponding GOT_OFST relocation decays to
5507 0. */
93a2b7ae 5508 if (! local_p)
0fdc1bf1 5509 goto got_disp;
5c18022e 5510 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5511 if (value == MINUS_ONE)
5512 return bfd_reloc_outofrange;
a8028dd0 5513 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5514 overflowed_p = mips_elf_overflow_p (value, 16);
5515 break;
5516
5517 case R_MIPS_GOT_OFST:
93a2b7ae 5518 if (local_p)
5c18022e 5519 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5520 else
5521 value = addend;
b49e97c9
TS
5522 overflowed_p = mips_elf_overflow_p (value, 16);
5523 break;
5524
5525 case R_MIPS_SUB:
5526 value = symbol - addend;
5527 value &= howto->dst_mask;
5528 break;
5529
5530 case R_MIPS_HIGHER:
5531 value = mips_elf_higher (addend + symbol);
5532 value &= howto->dst_mask;
5533 break;
5534
5535 case R_MIPS_HIGHEST:
5536 value = mips_elf_highest (addend + symbol);
5537 value &= howto->dst_mask;
5538 break;
5539
5540 case R_MIPS_SCN_DISP:
5541 value = symbol + addend - sec->output_offset;
5542 value &= howto->dst_mask;
5543 break;
5544
b49e97c9 5545 case R_MIPS_JALR:
1367d393
ILT
5546 /* This relocation is only a hint. In some cases, we optimize
5547 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5548 when the symbol does not resolve locally. */
5549 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5550 return bfd_reloc_continue;
5551 value = symbol + addend;
5552 break;
b49e97c9 5553
1367d393 5554 case R_MIPS_PJUMP:
b49e97c9
TS
5555 case R_MIPS_GNU_VTINHERIT:
5556 case R_MIPS_GNU_VTENTRY:
5557 /* We don't do anything with these at present. */
5558 return bfd_reloc_continue;
5559
5560 default:
5561 /* An unrecognized relocation type. */
5562 return bfd_reloc_notsupported;
5563 }
5564
5565 /* Store the VALUE for our caller. */
5566 *valuep = value;
5567 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5568}
5569
5570/* Obtain the field relocated by RELOCATION. */
5571
5572static bfd_vma
9719ad41
RS
5573mips_elf_obtain_contents (reloc_howto_type *howto,
5574 const Elf_Internal_Rela *relocation,
5575 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
5576{
5577 bfd_vma x;
5578 bfd_byte *location = contents + relocation->r_offset;
5579
5580 /* Obtain the bytes. */
5581 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5582
b49e97c9
TS
5583 return x;
5584}
5585
5586/* It has been determined that the result of the RELOCATION is the
5587 VALUE. Use HOWTO to place VALUE into the output file at the
5588 appropriate position. The SECTION is the section to which the
38a7df63
CF
5589 relocation applies.
5590 CROSS_MODE_JUMP_P is true if the relocation field
5591 is a MIPS16 jump to non-MIPS16 code, or vice versa.
b49e97c9 5592
b34976b6 5593 Returns FALSE if anything goes wrong. */
b49e97c9 5594
b34976b6 5595static bfd_boolean
9719ad41
RS
5596mips_elf_perform_relocation (struct bfd_link_info *info,
5597 reloc_howto_type *howto,
5598 const Elf_Internal_Rela *relocation,
5599 bfd_vma value, bfd *input_bfd,
5600 asection *input_section, bfd_byte *contents,
38a7df63 5601 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
5602{
5603 bfd_vma x;
5604 bfd_byte *location;
5605 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5606
5607 /* Figure out where the relocation is occurring. */
5608 location = contents + relocation->r_offset;
5609
d6f16593
MR
5610 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5611
b49e97c9
TS
5612 /* Obtain the current value. */
5613 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5614
5615 /* Clear the field we are setting. */
5616 x &= ~howto->dst_mask;
5617
b49e97c9
TS
5618 /* Set the field. */
5619 x |= (value & howto->dst_mask);
5620
5621 /* If required, turn JAL into JALX. */
38a7df63 5622 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 5623 {
b34976b6 5624 bfd_boolean ok;
b49e97c9
TS
5625 bfd_vma opcode = x >> 26;
5626 bfd_vma jalx_opcode;
5627
5628 /* Check to see if the opcode is already JAL or JALX. */
5629 if (r_type == R_MIPS16_26)
5630 {
5631 ok = ((opcode == 0x6) || (opcode == 0x7));
5632 jalx_opcode = 0x7;
5633 }
5634 else
5635 {
5636 ok = ((opcode == 0x3) || (opcode == 0x1d));
5637 jalx_opcode = 0x1d;
5638 }
5639
5640 /* If the opcode is not JAL or JALX, there's a problem. */
5641 if (!ok)
5642 {
5643 (*_bfd_error_handler)
d003868e
AM
5644 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5645 input_bfd,
5646 input_section,
b49e97c9
TS
5647 (unsigned long) relocation->r_offset);
5648 bfd_set_error (bfd_error_bad_value);
b34976b6 5649 return FALSE;
b49e97c9
TS
5650 }
5651
5652 /* Make this the JALX opcode. */
5653 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5654 }
5655
38a7df63
CF
5656 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5657 range. */
cd8d5a82 5658 if (!info->relocatable
38a7df63 5659 && !cross_mode_jump_p
cd8d5a82
CF
5660 && ((JAL_TO_BAL_P (input_bfd)
5661 && r_type == R_MIPS_26
5662 && (x >> 26) == 0x3) /* jal addr */
5663 || (JALR_TO_BAL_P (input_bfd)
5664 && r_type == R_MIPS_JALR
38a7df63
CF
5665 && x == 0x0320f809) /* jalr t9 */
5666 || (JR_TO_B_P (input_bfd)
5667 && r_type == R_MIPS_JALR
5668 && x == 0x03200008))) /* jr t9 */
1367d393
ILT
5669 {
5670 bfd_vma addr;
5671 bfd_vma dest;
5672 bfd_signed_vma off;
5673
5674 addr = (input_section->output_section->vma
5675 + input_section->output_offset
5676 + relocation->r_offset
5677 + 4);
5678 if (r_type == R_MIPS_26)
5679 dest = (value << 2) | ((addr >> 28) << 28);
5680 else
5681 dest = value;
5682 off = dest - addr;
5683 if (off <= 0x1ffff && off >= -0x20000)
38a7df63
CF
5684 {
5685 if (x == 0x03200008) /* jr t9 */
5686 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5687 else
5688 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5689 }
1367d393
ILT
5690 }
5691
b49e97c9
TS
5692 /* Put the value into the output. */
5693 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593
MR
5694
5695 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5696 location);
5697
b34976b6 5698 return TRUE;
b49e97c9 5699}
b49e97c9 5700\f
b49e97c9
TS
5701/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5702 is the original relocation, which is now being transformed into a
5703 dynamic relocation. The ADDENDP is adjusted if necessary; the
5704 caller should store the result in place of the original addend. */
5705
b34976b6 5706static bfd_boolean
9719ad41
RS
5707mips_elf_create_dynamic_relocation (bfd *output_bfd,
5708 struct bfd_link_info *info,
5709 const Elf_Internal_Rela *rel,
5710 struct mips_elf_link_hash_entry *h,
5711 asection *sec, bfd_vma symbol,
5712 bfd_vma *addendp, asection *input_section)
b49e97c9 5713{
947216bf 5714 Elf_Internal_Rela outrel[3];
b49e97c9
TS
5715 asection *sreloc;
5716 bfd *dynobj;
5717 int r_type;
5d41f0b6
RS
5718 long indx;
5719 bfd_boolean defined_p;
0a44bf69 5720 struct mips_elf_link_hash_table *htab;
b49e97c9 5721
0a44bf69 5722 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
5723 BFD_ASSERT (htab != NULL);
5724
b49e97c9
TS
5725 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5726 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 5727 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
5728 BFD_ASSERT (sreloc != NULL);
5729 BFD_ASSERT (sreloc->contents != NULL);
5730 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 5731 < sreloc->size);
b49e97c9 5732
b49e97c9
TS
5733 outrel[0].r_offset =
5734 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
5735 if (ABI_64_P (output_bfd))
5736 {
5737 outrel[1].r_offset =
5738 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5739 outrel[2].r_offset =
5740 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5741 }
b49e97c9 5742
c5ae1840 5743 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 5744 /* The relocation field has been deleted. */
5d41f0b6
RS
5745 return TRUE;
5746
5747 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
5748 {
5749 /* The relocation field has been converted into a relative value of
5750 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5751 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 5752 *addendp += symbol;
5d41f0b6 5753 return TRUE;
0d591ff7 5754 }
b49e97c9 5755
5d41f0b6
RS
5756 /* We must now calculate the dynamic symbol table index to use
5757 in the relocation. */
5758 if (h != NULL
6ece8836
TS
5759 && (!h->root.def_regular
5760 || (info->shared && !info->symbolic && !h->root.forced_local)))
5d41f0b6
RS
5761 {
5762 indx = h->root.dynindx;
5763 if (SGI_COMPAT (output_bfd))
5764 defined_p = h->root.def_regular;
5765 else
5766 /* ??? glibc's ld.so just adds the final GOT entry to the
5767 relocation field. It therefore treats relocs against
5768 defined symbols in the same way as relocs against
5769 undefined symbols. */
5770 defined_p = FALSE;
5771 }
b49e97c9
TS
5772 else
5773 {
5d41f0b6
RS
5774 if (sec != NULL && bfd_is_abs_section (sec))
5775 indx = 0;
5776 else if (sec == NULL || sec->owner == NULL)
fdd07405 5777 {
5d41f0b6
RS
5778 bfd_set_error (bfd_error_bad_value);
5779 return FALSE;
b49e97c9
TS
5780 }
5781 else
5782 {
5d41f0b6 5783 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
5784 if (indx == 0)
5785 {
5786 asection *osec = htab->root.text_index_section;
5787 indx = elf_section_data (osec)->dynindx;
5788 }
5d41f0b6
RS
5789 if (indx == 0)
5790 abort ();
b49e97c9
TS
5791 }
5792
5d41f0b6
RS
5793 /* Instead of generating a relocation using the section
5794 symbol, we may as well make it a fully relative
5795 relocation. We want to avoid generating relocations to
5796 local symbols because we used to generate them
5797 incorrectly, without adding the original symbol value,
5798 which is mandated by the ABI for section symbols. In
5799 order to give dynamic loaders and applications time to
5800 phase out the incorrect use, we refrain from emitting
5801 section-relative relocations. It's not like they're
5802 useful, after all. This should be a bit more efficient
5803 as well. */
5804 /* ??? Although this behavior is compatible with glibc's ld.so,
5805 the ABI says that relocations against STN_UNDEF should have
5806 a symbol value of 0. Irix rld honors this, so relocations
5807 against STN_UNDEF have no effect. */
5808 if (!SGI_COMPAT (output_bfd))
5809 indx = 0;
5810 defined_p = TRUE;
b49e97c9
TS
5811 }
5812
5d41f0b6
RS
5813 /* If the relocation was previously an absolute relocation and
5814 this symbol will not be referred to by the relocation, we must
5815 adjust it by the value we give it in the dynamic symbol table.
5816 Otherwise leave the job up to the dynamic linker. */
5817 if (defined_p && r_type != R_MIPS_REL32)
5818 *addendp += symbol;
5819
0a44bf69
RS
5820 if (htab->is_vxworks)
5821 /* VxWorks uses non-relative relocations for this. */
5822 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5823 else
5824 /* The relocation is always an REL32 relocation because we don't
5825 know where the shared library will wind up at load-time. */
5826 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5827 R_MIPS_REL32);
5828
5d41f0b6
RS
5829 /* For strict adherence to the ABI specification, we should
5830 generate a R_MIPS_64 relocation record by itself before the
5831 _REL32/_64 record as well, such that the addend is read in as
5832 a 64-bit value (REL32 is a 32-bit relocation, after all).
5833 However, since none of the existing ELF64 MIPS dynamic
5834 loaders seems to care, we don't waste space with these
5835 artificial relocations. If this turns out to not be true,
5836 mips_elf_allocate_dynamic_relocation() should be tweaked so
5837 as to make room for a pair of dynamic relocations per
5838 invocation if ABI_64_P, and here we should generate an
5839 additional relocation record with R_MIPS_64 by itself for a
5840 NULL symbol before this relocation record. */
5841 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5842 ABI_64_P (output_bfd)
5843 ? R_MIPS_64
5844 : R_MIPS_NONE);
5845 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5846
5847 /* Adjust the output offset of the relocation to reference the
5848 correct location in the output file. */
5849 outrel[0].r_offset += (input_section->output_section->vma
5850 + input_section->output_offset);
5851 outrel[1].r_offset += (input_section->output_section->vma
5852 + input_section->output_offset);
5853 outrel[2].r_offset += (input_section->output_section->vma
5854 + input_section->output_offset);
5855
b49e97c9
TS
5856 /* Put the relocation back out. We have to use the special
5857 relocation outputter in the 64-bit case since the 64-bit
5858 relocation format is non-standard. */
5859 if (ABI_64_P (output_bfd))
5860 {
5861 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5862 (output_bfd, &outrel[0],
5863 (sreloc->contents
5864 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5865 }
0a44bf69
RS
5866 else if (htab->is_vxworks)
5867 {
5868 /* VxWorks uses RELA rather than REL dynamic relocations. */
5869 outrel[0].r_addend = *addendp;
5870 bfd_elf32_swap_reloca_out
5871 (output_bfd, &outrel[0],
5872 (sreloc->contents
5873 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5874 }
b49e97c9 5875 else
947216bf
AM
5876 bfd_elf32_swap_reloc_out
5877 (output_bfd, &outrel[0],
5878 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 5879
b49e97c9
TS
5880 /* We've now added another relocation. */
5881 ++sreloc->reloc_count;
5882
5883 /* Make sure the output section is writable. The dynamic linker
5884 will be writing to it. */
5885 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5886 |= SHF_WRITE;
5887
5888 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 5889 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9
TS
5890 {
5891 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5892 bfd_byte *cr;
5893
5894 if (scpt)
5895 {
5896 Elf32_crinfo cptrel;
5897
5898 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5899 cptrel.vaddr = (rel->r_offset
5900 + input_section->output_section->vma
5901 + input_section->output_offset);
5902 if (r_type == R_MIPS_REL32)
5903 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5904 else
5905 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5906 mips_elf_set_cr_dist2to (cptrel, 0);
5907 cptrel.konst = *addendp;
5908
5909 cr = (scpt->contents
5910 + sizeof (Elf32_External_compact_rel));
abc0f8d0 5911 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
5912 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5913 ((Elf32_External_crinfo *) cr
5914 + scpt->reloc_count));
5915 ++scpt->reloc_count;
5916 }
5917 }
5918
943284cc
DJ
5919 /* If we've written this relocation for a readonly section,
5920 we need to set DF_TEXTREL again, so that we do not delete the
5921 DT_TEXTREL tag. */
5922 if (MIPS_ELF_READONLY_SECTION (input_section))
5923 info->flags |= DF_TEXTREL;
5924
b34976b6 5925 return TRUE;
b49e97c9
TS
5926}
5927\f
b49e97c9
TS
5928/* Return the MACH for a MIPS e_flags value. */
5929
5930unsigned long
9719ad41 5931_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
5932{
5933 switch (flags & EF_MIPS_MACH)
5934 {
5935 case E_MIPS_MACH_3900:
5936 return bfd_mach_mips3900;
5937
5938 case E_MIPS_MACH_4010:
5939 return bfd_mach_mips4010;
5940
5941 case E_MIPS_MACH_4100:
5942 return bfd_mach_mips4100;
5943
5944 case E_MIPS_MACH_4111:
5945 return bfd_mach_mips4111;
5946
00707a0e
RS
5947 case E_MIPS_MACH_4120:
5948 return bfd_mach_mips4120;
5949
b49e97c9
TS
5950 case E_MIPS_MACH_4650:
5951 return bfd_mach_mips4650;
5952
00707a0e
RS
5953 case E_MIPS_MACH_5400:
5954 return bfd_mach_mips5400;
5955
5956 case E_MIPS_MACH_5500:
5957 return bfd_mach_mips5500;
5958
0d2e43ed
ILT
5959 case E_MIPS_MACH_9000:
5960 return bfd_mach_mips9000;
5961
b49e97c9
TS
5962 case E_MIPS_MACH_SB1:
5963 return bfd_mach_mips_sb1;
5964
350cc38d
MS
5965 case E_MIPS_MACH_LS2E:
5966 return bfd_mach_mips_loongson_2e;
5967
5968 case E_MIPS_MACH_LS2F:
5969 return bfd_mach_mips_loongson_2f;
5970
6f179bd0
AN
5971 case E_MIPS_MACH_OCTEON:
5972 return bfd_mach_mips_octeon;
5973
52b6b6b9
JM
5974 case E_MIPS_MACH_XLR:
5975 return bfd_mach_mips_xlr;
5976
b49e97c9
TS
5977 default:
5978 switch (flags & EF_MIPS_ARCH)
5979 {
5980 default:
5981 case E_MIPS_ARCH_1:
5982 return bfd_mach_mips3000;
b49e97c9
TS
5983
5984 case E_MIPS_ARCH_2:
5985 return bfd_mach_mips6000;
b49e97c9
TS
5986
5987 case E_MIPS_ARCH_3:
5988 return bfd_mach_mips4000;
b49e97c9
TS
5989
5990 case E_MIPS_ARCH_4:
5991 return bfd_mach_mips8000;
b49e97c9
TS
5992
5993 case E_MIPS_ARCH_5:
5994 return bfd_mach_mips5;
b49e97c9
TS
5995
5996 case E_MIPS_ARCH_32:
5997 return bfd_mach_mipsisa32;
b49e97c9
TS
5998
5999 case E_MIPS_ARCH_64:
6000 return bfd_mach_mipsisa64;
af7ee8bf
CD
6001
6002 case E_MIPS_ARCH_32R2:
6003 return bfd_mach_mipsisa32r2;
5f74bc13
CD
6004
6005 case E_MIPS_ARCH_64R2:
6006 return bfd_mach_mipsisa64r2;
b49e97c9
TS
6007 }
6008 }
6009
6010 return 0;
6011}
6012
6013/* Return printable name for ABI. */
6014
6015static INLINE char *
9719ad41 6016elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
6017{
6018 flagword flags;
6019
6020 flags = elf_elfheader (abfd)->e_flags;
6021 switch (flags & EF_MIPS_ABI)
6022 {
6023 case 0:
6024 if (ABI_N32_P (abfd))
6025 return "N32";
6026 else if (ABI_64_P (abfd))
6027 return "64";
6028 else
6029 return "none";
6030 case E_MIPS_ABI_O32:
6031 return "O32";
6032 case E_MIPS_ABI_O64:
6033 return "O64";
6034 case E_MIPS_ABI_EABI32:
6035 return "EABI32";
6036 case E_MIPS_ABI_EABI64:
6037 return "EABI64";
6038 default:
6039 return "unknown abi";
6040 }
6041}
6042\f
6043/* MIPS ELF uses two common sections. One is the usual one, and the
6044 other is for small objects. All the small objects are kept
6045 together, and then referenced via the gp pointer, which yields
6046 faster assembler code. This is what we use for the small common
6047 section. This approach is copied from ecoff.c. */
6048static asection mips_elf_scom_section;
6049static asymbol mips_elf_scom_symbol;
6050static asymbol *mips_elf_scom_symbol_ptr;
6051
6052/* MIPS ELF also uses an acommon section, which represents an
6053 allocated common symbol which may be overridden by a
6054 definition in a shared library. */
6055static asection mips_elf_acom_section;
6056static asymbol mips_elf_acom_symbol;
6057static asymbol *mips_elf_acom_symbol_ptr;
6058
738e5348 6059/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
6060
6061void
9719ad41 6062_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
6063{
6064 elf_symbol_type *elfsym;
6065
738e5348 6066 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6067 elfsym = (elf_symbol_type *) asym;
6068 switch (elfsym->internal_elf_sym.st_shndx)
6069 {
6070 case SHN_MIPS_ACOMMON:
6071 /* This section is used in a dynamically linked executable file.
6072 It is an allocated common section. The dynamic linker can
6073 either resolve these symbols to something in a shared
6074 library, or it can just leave them here. For our purposes,
6075 we can consider these symbols to be in a new section. */
6076 if (mips_elf_acom_section.name == NULL)
6077 {
6078 /* Initialize the acommon section. */
6079 mips_elf_acom_section.name = ".acommon";
6080 mips_elf_acom_section.flags = SEC_ALLOC;
6081 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6082 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6083 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6084 mips_elf_acom_symbol.name = ".acommon";
6085 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6086 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6087 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6088 }
6089 asym->section = &mips_elf_acom_section;
6090 break;
6091
6092 case SHN_COMMON:
6093 /* Common symbols less than the GP size are automatically
6094 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6095 if (asym->value > elf_gp_size (abfd)
b59eed79 6096 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6097 || IRIX_COMPAT (abfd) == ict_irix6)
6098 break;
6099 /* Fall through. */
6100 case SHN_MIPS_SCOMMON:
6101 if (mips_elf_scom_section.name == NULL)
6102 {
6103 /* Initialize the small common section. */
6104 mips_elf_scom_section.name = ".scommon";
6105 mips_elf_scom_section.flags = SEC_IS_COMMON;
6106 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6107 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6108 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6109 mips_elf_scom_symbol.name = ".scommon";
6110 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6111 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6112 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6113 }
6114 asym->section = &mips_elf_scom_section;
6115 asym->value = elfsym->internal_elf_sym.st_size;
6116 break;
6117
6118 case SHN_MIPS_SUNDEFINED:
6119 asym->section = bfd_und_section_ptr;
6120 break;
6121
b49e97c9 6122 case SHN_MIPS_TEXT:
00b4930b
TS
6123 {
6124 asection *section = bfd_get_section_by_name (abfd, ".text");
6125
6126 BFD_ASSERT (SGI_COMPAT (abfd));
6127 if (section != NULL)
6128 {
6129 asym->section = section;
6130 /* MIPS_TEXT is a bit special, the address is not an offset
6131 to the base of the .text section. So substract the section
6132 base address to make it an offset. */
6133 asym->value -= section->vma;
6134 }
6135 }
b49e97c9
TS
6136 break;
6137
6138 case SHN_MIPS_DATA:
00b4930b
TS
6139 {
6140 asection *section = bfd_get_section_by_name (abfd, ".data");
6141
6142 BFD_ASSERT (SGI_COMPAT (abfd));
6143 if (section != NULL)
6144 {
6145 asym->section = section;
6146 /* MIPS_DATA is a bit special, the address is not an offset
6147 to the base of the .data section. So substract the section
6148 base address to make it an offset. */
6149 asym->value -= section->vma;
6150 }
6151 }
b49e97c9 6152 break;
b49e97c9 6153 }
738e5348
RS
6154
6155 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6156 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6157 && (asym->value & 1) != 0)
6158 {
6159 asym->value--;
6160 elfsym->internal_elf_sym.st_other
6161 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6162 }
b49e97c9
TS
6163}
6164\f
8c946ed5
RS
6165/* Implement elf_backend_eh_frame_address_size. This differs from
6166 the default in the way it handles EABI64.
6167
6168 EABI64 was originally specified as an LP64 ABI, and that is what
6169 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6170 historically accepted the combination of -mabi=eabi and -mlong32,
6171 and this ILP32 variation has become semi-official over time.
6172 Both forms use elf32 and have pointer-sized FDE addresses.
6173
6174 If an EABI object was generated by GCC 4.0 or above, it will have
6175 an empty .gcc_compiled_longXX section, where XX is the size of longs
6176 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6177 have no special marking to distinguish them from LP64 objects.
6178
6179 We don't want users of the official LP64 ABI to be punished for the
6180 existence of the ILP32 variant, but at the same time, we don't want
6181 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6182 We therefore take the following approach:
6183
6184 - If ABFD contains a .gcc_compiled_longXX section, use it to
6185 determine the pointer size.
6186
6187 - Otherwise check the type of the first relocation. Assume that
6188 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6189
6190 - Otherwise punt.
6191
6192 The second check is enough to detect LP64 objects generated by pre-4.0
6193 compilers because, in the kind of output generated by those compilers,
6194 the first relocation will be associated with either a CIE personality
6195 routine or an FDE start address. Furthermore, the compilers never
6196 used a special (non-pointer) encoding for this ABI.
6197
6198 Checking the relocation type should also be safe because there is no
6199 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6200 did so. */
6201
6202unsigned int
6203_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6204{
6205 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6206 return 8;
6207 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6208 {
6209 bfd_boolean long32_p, long64_p;
6210
6211 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6212 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6213 if (long32_p && long64_p)
6214 return 0;
6215 if (long32_p)
6216 return 4;
6217 if (long64_p)
6218 return 8;
6219
6220 if (sec->reloc_count > 0
6221 && elf_section_data (sec)->relocs != NULL
6222 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6223 == R_MIPS_64))
6224 return 8;
6225
6226 return 0;
6227 }
6228 return 4;
6229}
6230\f
174fd7f9
RS
6231/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6232 relocations against two unnamed section symbols to resolve to the
6233 same address. For example, if we have code like:
6234
6235 lw $4,%got_disp(.data)($gp)
6236 lw $25,%got_disp(.text)($gp)
6237 jalr $25
6238
6239 then the linker will resolve both relocations to .data and the program
6240 will jump there rather than to .text.
6241
6242 We can work around this problem by giving names to local section symbols.
6243 This is also what the MIPSpro tools do. */
6244
6245bfd_boolean
6246_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6247{
6248 return SGI_COMPAT (abfd);
6249}
6250\f
b49e97c9
TS
6251/* Work over a section just before writing it out. This routine is
6252 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6253 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6254 a better way. */
6255
b34976b6 6256bfd_boolean
9719ad41 6257_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6258{
6259 if (hdr->sh_type == SHT_MIPS_REGINFO
6260 && hdr->sh_size > 0)
6261 {
6262 bfd_byte buf[4];
6263
6264 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6265 BFD_ASSERT (hdr->contents == NULL);
6266
6267 if (bfd_seek (abfd,
6268 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6269 SEEK_SET) != 0)
b34976b6 6270 return FALSE;
b49e97c9 6271 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6272 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6273 return FALSE;
b49e97c9
TS
6274 }
6275
6276 if (hdr->sh_type == SHT_MIPS_OPTIONS
6277 && hdr->bfd_section != NULL
f0abc2a1
AM
6278 && mips_elf_section_data (hdr->bfd_section) != NULL
6279 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6280 {
6281 bfd_byte *contents, *l, *lend;
6282
f0abc2a1
AM
6283 /* We stored the section contents in the tdata field in the
6284 set_section_contents routine. We save the section contents
6285 so that we don't have to read them again.
b49e97c9
TS
6286 At this point we know that elf_gp is set, so we can look
6287 through the section contents to see if there is an
6288 ODK_REGINFO structure. */
6289
f0abc2a1 6290 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6291 l = contents;
6292 lend = contents + hdr->sh_size;
6293 while (l + sizeof (Elf_External_Options) <= lend)
6294 {
6295 Elf_Internal_Options intopt;
6296
6297 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6298 &intopt);
1bc8074d
MR
6299 if (intopt.size < sizeof (Elf_External_Options))
6300 {
6301 (*_bfd_error_handler)
6302 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6303 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6304 break;
6305 }
b49e97c9
TS
6306 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6307 {
6308 bfd_byte buf[8];
6309
6310 if (bfd_seek (abfd,
6311 (hdr->sh_offset
6312 + (l - contents)
6313 + sizeof (Elf_External_Options)
6314 + (sizeof (Elf64_External_RegInfo) - 8)),
6315 SEEK_SET) != 0)
b34976b6 6316 return FALSE;
b49e97c9 6317 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6318 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6319 return FALSE;
b49e97c9
TS
6320 }
6321 else if (intopt.kind == ODK_REGINFO)
6322 {
6323 bfd_byte buf[4];
6324
6325 if (bfd_seek (abfd,
6326 (hdr->sh_offset
6327 + (l - contents)
6328 + sizeof (Elf_External_Options)
6329 + (sizeof (Elf32_External_RegInfo) - 4)),
6330 SEEK_SET) != 0)
b34976b6 6331 return FALSE;
b49e97c9 6332 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6333 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6334 return FALSE;
b49e97c9
TS
6335 }
6336 l += intopt.size;
6337 }
6338 }
6339
6340 if (hdr->bfd_section != NULL)
6341 {
6342 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6343
2d0f9ad9
JM
6344 /* .sbss is not handled specially here because the GNU/Linux
6345 prelinker can convert .sbss from NOBITS to PROGBITS and
6346 changing it back to NOBITS breaks the binary. The entry in
6347 _bfd_mips_elf_special_sections will ensure the correct flags
6348 are set on .sbss if BFD creates it without reading it from an
6349 input file, and without special handling here the flags set
6350 on it in an input file will be followed. */
b49e97c9
TS
6351 if (strcmp (name, ".sdata") == 0
6352 || strcmp (name, ".lit8") == 0
6353 || strcmp (name, ".lit4") == 0)
6354 {
6355 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6356 hdr->sh_type = SHT_PROGBITS;
6357 }
b49e97c9
TS
6358 else if (strcmp (name, ".srdata") == 0)
6359 {
6360 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6361 hdr->sh_type = SHT_PROGBITS;
6362 }
6363 else if (strcmp (name, ".compact_rel") == 0)
6364 {
6365 hdr->sh_flags = 0;
6366 hdr->sh_type = SHT_PROGBITS;
6367 }
6368 else if (strcmp (name, ".rtproc") == 0)
6369 {
6370 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6371 {
6372 unsigned int adjust;
6373
6374 adjust = hdr->sh_size % hdr->sh_addralign;
6375 if (adjust != 0)
6376 hdr->sh_size += hdr->sh_addralign - adjust;
6377 }
6378 }
6379 }
6380
b34976b6 6381 return TRUE;
b49e97c9
TS
6382}
6383
6384/* Handle a MIPS specific section when reading an object file. This
6385 is called when elfcode.h finds a section with an unknown type.
6386 This routine supports both the 32-bit and 64-bit ELF ABI.
6387
6388 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6389 how to. */
6390
b34976b6 6391bfd_boolean
6dc132d9
L
6392_bfd_mips_elf_section_from_shdr (bfd *abfd,
6393 Elf_Internal_Shdr *hdr,
6394 const char *name,
6395 int shindex)
b49e97c9
TS
6396{
6397 flagword flags = 0;
6398
6399 /* There ought to be a place to keep ELF backend specific flags, but
6400 at the moment there isn't one. We just keep track of the
6401 sections by their name, instead. Fortunately, the ABI gives
6402 suggested names for all the MIPS specific sections, so we will
6403 probably get away with this. */
6404 switch (hdr->sh_type)
6405 {
6406 case SHT_MIPS_LIBLIST:
6407 if (strcmp (name, ".liblist") != 0)
b34976b6 6408 return FALSE;
b49e97c9
TS
6409 break;
6410 case SHT_MIPS_MSYM:
6411 if (strcmp (name, ".msym") != 0)
b34976b6 6412 return FALSE;
b49e97c9
TS
6413 break;
6414 case SHT_MIPS_CONFLICT:
6415 if (strcmp (name, ".conflict") != 0)
b34976b6 6416 return FALSE;
b49e97c9
TS
6417 break;
6418 case SHT_MIPS_GPTAB:
0112cd26 6419 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6420 return FALSE;
b49e97c9
TS
6421 break;
6422 case SHT_MIPS_UCODE:
6423 if (strcmp (name, ".ucode") != 0)
b34976b6 6424 return FALSE;
b49e97c9
TS
6425 break;
6426 case SHT_MIPS_DEBUG:
6427 if (strcmp (name, ".mdebug") != 0)
b34976b6 6428 return FALSE;
b49e97c9
TS
6429 flags = SEC_DEBUGGING;
6430 break;
6431 case SHT_MIPS_REGINFO:
6432 if (strcmp (name, ".reginfo") != 0
6433 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6434 return FALSE;
b49e97c9
TS
6435 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6436 break;
6437 case SHT_MIPS_IFACE:
6438 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6439 return FALSE;
b49e97c9
TS
6440 break;
6441 case SHT_MIPS_CONTENT:
0112cd26 6442 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6443 return FALSE;
b49e97c9
TS
6444 break;
6445 case SHT_MIPS_OPTIONS:
cc2e31b9 6446 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6447 return FALSE;
b49e97c9
TS
6448 break;
6449 case SHT_MIPS_DWARF:
1b315056 6450 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6451 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6452 return FALSE;
b49e97c9
TS
6453 break;
6454 case SHT_MIPS_SYMBOL_LIB:
6455 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6456 return FALSE;
b49e97c9
TS
6457 break;
6458 case SHT_MIPS_EVENTS:
0112cd26
NC
6459 if (! CONST_STRNEQ (name, ".MIPS.events")
6460 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6461 return FALSE;
b49e97c9
TS
6462 break;
6463 default:
cc2e31b9 6464 break;
b49e97c9
TS
6465 }
6466
6dc132d9 6467 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6468 return FALSE;
b49e97c9
TS
6469
6470 if (flags)
6471 {
6472 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6473 (bfd_get_section_flags (abfd,
6474 hdr->bfd_section)
6475 | flags)))
b34976b6 6476 return FALSE;
b49e97c9
TS
6477 }
6478
6479 /* FIXME: We should record sh_info for a .gptab section. */
6480
6481 /* For a .reginfo section, set the gp value in the tdata information
6482 from the contents of this section. We need the gp value while
6483 processing relocs, so we just get it now. The .reginfo section
6484 is not used in the 64-bit MIPS ELF ABI. */
6485 if (hdr->sh_type == SHT_MIPS_REGINFO)
6486 {
6487 Elf32_External_RegInfo ext;
6488 Elf32_RegInfo s;
6489
9719ad41
RS
6490 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6491 &ext, 0, sizeof ext))
b34976b6 6492 return FALSE;
b49e97c9
TS
6493 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6494 elf_gp (abfd) = s.ri_gp_value;
6495 }
6496
6497 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6498 set the gp value based on what we find. We may see both
6499 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6500 they should agree. */
6501 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6502 {
6503 bfd_byte *contents, *l, *lend;
6504
9719ad41 6505 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6506 if (contents == NULL)
b34976b6 6507 return FALSE;
b49e97c9 6508 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6509 0, hdr->sh_size))
b49e97c9
TS
6510 {
6511 free (contents);
b34976b6 6512 return FALSE;
b49e97c9
TS
6513 }
6514 l = contents;
6515 lend = contents + hdr->sh_size;
6516 while (l + sizeof (Elf_External_Options) <= lend)
6517 {
6518 Elf_Internal_Options intopt;
6519
6520 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6521 &intopt);
1bc8074d
MR
6522 if (intopt.size < sizeof (Elf_External_Options))
6523 {
6524 (*_bfd_error_handler)
6525 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6526 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6527 break;
6528 }
b49e97c9
TS
6529 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6530 {
6531 Elf64_Internal_RegInfo intreg;
6532
6533 bfd_mips_elf64_swap_reginfo_in
6534 (abfd,
6535 ((Elf64_External_RegInfo *)
6536 (l + sizeof (Elf_External_Options))),
6537 &intreg);
6538 elf_gp (abfd) = intreg.ri_gp_value;
6539 }
6540 else if (intopt.kind == ODK_REGINFO)
6541 {
6542 Elf32_RegInfo intreg;
6543
6544 bfd_mips_elf32_swap_reginfo_in
6545 (abfd,
6546 ((Elf32_External_RegInfo *)
6547 (l + sizeof (Elf_External_Options))),
6548 &intreg);
6549 elf_gp (abfd) = intreg.ri_gp_value;
6550 }
6551 l += intopt.size;
6552 }
6553 free (contents);
6554 }
6555
b34976b6 6556 return TRUE;
b49e97c9
TS
6557}
6558
6559/* Set the correct type for a MIPS ELF section. We do this by the
6560 section name, which is a hack, but ought to work. This routine is
6561 used by both the 32-bit and the 64-bit ABI. */
6562
b34976b6 6563bfd_boolean
9719ad41 6564_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 6565{
0414f35b 6566 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
6567
6568 if (strcmp (name, ".liblist") == 0)
6569 {
6570 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 6571 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
6572 /* The sh_link field is set in final_write_processing. */
6573 }
6574 else if (strcmp (name, ".conflict") == 0)
6575 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 6576 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
6577 {
6578 hdr->sh_type = SHT_MIPS_GPTAB;
6579 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6580 /* The sh_info field is set in final_write_processing. */
6581 }
6582 else if (strcmp (name, ".ucode") == 0)
6583 hdr->sh_type = SHT_MIPS_UCODE;
6584 else if (strcmp (name, ".mdebug") == 0)
6585 {
6586 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 6587 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
6588 entsize of 0. FIXME: Does this matter? */
6589 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6590 hdr->sh_entsize = 0;
6591 else
6592 hdr->sh_entsize = 1;
6593 }
6594 else if (strcmp (name, ".reginfo") == 0)
6595 {
6596 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 6597 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
6598 entsize of 0x18. FIXME: Does this matter? */
6599 if (SGI_COMPAT (abfd))
6600 {
6601 if ((abfd->flags & DYNAMIC) != 0)
6602 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6603 else
6604 hdr->sh_entsize = 1;
6605 }
6606 else
6607 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6608 }
6609 else if (SGI_COMPAT (abfd)
6610 && (strcmp (name, ".hash") == 0
6611 || strcmp (name, ".dynamic") == 0
6612 || strcmp (name, ".dynstr") == 0))
6613 {
6614 if (SGI_COMPAT (abfd))
6615 hdr->sh_entsize = 0;
6616#if 0
8dc1a139 6617 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
6618 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6619#endif
6620 }
6621 else if (strcmp (name, ".got") == 0
6622 || strcmp (name, ".srdata") == 0
6623 || strcmp (name, ".sdata") == 0
6624 || strcmp (name, ".sbss") == 0
6625 || strcmp (name, ".lit4") == 0
6626 || strcmp (name, ".lit8") == 0)
6627 hdr->sh_flags |= SHF_MIPS_GPREL;
6628 else if (strcmp (name, ".MIPS.interfaces") == 0)
6629 {
6630 hdr->sh_type = SHT_MIPS_IFACE;
6631 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6632 }
0112cd26 6633 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
6634 {
6635 hdr->sh_type = SHT_MIPS_CONTENT;
6636 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6637 /* The sh_info field is set in final_write_processing. */
6638 }
cc2e31b9 6639 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
6640 {
6641 hdr->sh_type = SHT_MIPS_OPTIONS;
6642 hdr->sh_entsize = 1;
6643 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6644 }
1b315056
CS
6645 else if (CONST_STRNEQ (name, ".debug_")
6646 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
6647 {
6648 hdr->sh_type = SHT_MIPS_DWARF;
6649
6650 /* Irix facilities such as libexc expect a single .debug_frame
6651 per executable, the system ones have NOSTRIP set and the linker
6652 doesn't merge sections with different flags so ... */
6653 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6654 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6655 }
b49e97c9
TS
6656 else if (strcmp (name, ".MIPS.symlib") == 0)
6657 {
6658 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6659 /* The sh_link and sh_info fields are set in
6660 final_write_processing. */
6661 }
0112cd26
NC
6662 else if (CONST_STRNEQ (name, ".MIPS.events")
6663 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
6664 {
6665 hdr->sh_type = SHT_MIPS_EVENTS;
6666 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6667 /* The sh_link field is set in final_write_processing. */
6668 }
6669 else if (strcmp (name, ".msym") == 0)
6670 {
6671 hdr->sh_type = SHT_MIPS_MSYM;
6672 hdr->sh_flags |= SHF_ALLOC;
6673 hdr->sh_entsize = 8;
6674 }
6675
7a79a000
TS
6676 /* The generic elf_fake_sections will set up REL_HDR using the default
6677 kind of relocations. We used to set up a second header for the
6678 non-default kind of relocations here, but only NewABI would use
6679 these, and the IRIX ld doesn't like resulting empty RELA sections.
6680 Thus we create those header only on demand now. */
b49e97c9 6681
b34976b6 6682 return TRUE;
b49e97c9
TS
6683}
6684
6685/* Given a BFD section, try to locate the corresponding ELF section
6686 index. This is used by both the 32-bit and the 64-bit ABI.
6687 Actually, it's not clear to me that the 64-bit ABI supports these,
6688 but for non-PIC objects we will certainly want support for at least
6689 the .scommon section. */
6690
b34976b6 6691bfd_boolean
9719ad41
RS
6692_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6693 asection *sec, int *retval)
b49e97c9
TS
6694{
6695 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6696 {
6697 *retval = SHN_MIPS_SCOMMON;
b34976b6 6698 return TRUE;
b49e97c9
TS
6699 }
6700 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6701 {
6702 *retval = SHN_MIPS_ACOMMON;
b34976b6 6703 return TRUE;
b49e97c9 6704 }
b34976b6 6705 return FALSE;
b49e97c9
TS
6706}
6707\f
6708/* Hook called by the linker routine which adds symbols from an object
6709 file. We must handle the special MIPS section numbers here. */
6710
b34976b6 6711bfd_boolean
9719ad41 6712_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 6713 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
6714 flagword *flagsp ATTRIBUTE_UNUSED,
6715 asection **secp, bfd_vma *valp)
b49e97c9
TS
6716{
6717 if (SGI_COMPAT (abfd)
6718 && (abfd->flags & DYNAMIC) != 0
6719 && strcmp (*namep, "_rld_new_interface") == 0)
6720 {
8dc1a139 6721 /* Skip IRIX5 rld entry name. */
b49e97c9 6722 *namep = NULL;
b34976b6 6723 return TRUE;
b49e97c9
TS
6724 }
6725
eedecc07
DD
6726 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6727 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6728 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6729 a magic symbol resolved by the linker, we ignore this bogus definition
6730 of _gp_disp. New ABI objects do not suffer from this problem so this
6731 is not done for them. */
6732 if (!NEWABI_P(abfd)
6733 && (sym->st_shndx == SHN_ABS)
6734 && (strcmp (*namep, "_gp_disp") == 0))
6735 {
6736 *namep = NULL;
6737 return TRUE;
6738 }
6739
b49e97c9
TS
6740 switch (sym->st_shndx)
6741 {
6742 case SHN_COMMON:
6743 /* Common symbols less than the GP size are automatically
6744 treated as SHN_MIPS_SCOMMON symbols. */
6745 if (sym->st_size > elf_gp_size (abfd)
b59eed79 6746 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
6747 || IRIX_COMPAT (abfd) == ict_irix6)
6748 break;
6749 /* Fall through. */
6750 case SHN_MIPS_SCOMMON:
6751 *secp = bfd_make_section_old_way (abfd, ".scommon");
6752 (*secp)->flags |= SEC_IS_COMMON;
6753 *valp = sym->st_size;
6754 break;
6755
6756 case SHN_MIPS_TEXT:
6757 /* This section is used in a shared object. */
6758 if (elf_tdata (abfd)->elf_text_section == NULL)
6759 {
6760 asymbol *elf_text_symbol;
6761 asection *elf_text_section;
6762 bfd_size_type amt = sizeof (asection);
6763
6764 elf_text_section = bfd_zalloc (abfd, amt);
6765 if (elf_text_section == NULL)
b34976b6 6766 return FALSE;
b49e97c9
TS
6767
6768 amt = sizeof (asymbol);
6769 elf_text_symbol = bfd_zalloc (abfd, amt);
6770 if (elf_text_symbol == NULL)
b34976b6 6771 return FALSE;
b49e97c9
TS
6772
6773 /* Initialize the section. */
6774
6775 elf_tdata (abfd)->elf_text_section = elf_text_section;
6776 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6777
6778 elf_text_section->symbol = elf_text_symbol;
6779 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6780
6781 elf_text_section->name = ".text";
6782 elf_text_section->flags = SEC_NO_FLAGS;
6783 elf_text_section->output_section = NULL;
6784 elf_text_section->owner = abfd;
6785 elf_text_symbol->name = ".text";
6786 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6787 elf_text_symbol->section = elf_text_section;
6788 }
6789 /* This code used to do *secp = bfd_und_section_ptr if
6790 info->shared. I don't know why, and that doesn't make sense,
6791 so I took it out. */
6792 *secp = elf_tdata (abfd)->elf_text_section;
6793 break;
6794
6795 case SHN_MIPS_ACOMMON:
6796 /* Fall through. XXX Can we treat this as allocated data? */
6797 case SHN_MIPS_DATA:
6798 /* This section is used in a shared object. */
6799 if (elf_tdata (abfd)->elf_data_section == NULL)
6800 {
6801 asymbol *elf_data_symbol;
6802 asection *elf_data_section;
6803 bfd_size_type amt = sizeof (asection);
6804
6805 elf_data_section = bfd_zalloc (abfd, amt);
6806 if (elf_data_section == NULL)
b34976b6 6807 return FALSE;
b49e97c9
TS
6808
6809 amt = sizeof (asymbol);
6810 elf_data_symbol = bfd_zalloc (abfd, amt);
6811 if (elf_data_symbol == NULL)
b34976b6 6812 return FALSE;
b49e97c9
TS
6813
6814 /* Initialize the section. */
6815
6816 elf_tdata (abfd)->elf_data_section = elf_data_section;
6817 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6818
6819 elf_data_section->symbol = elf_data_symbol;
6820 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6821
6822 elf_data_section->name = ".data";
6823 elf_data_section->flags = SEC_NO_FLAGS;
6824 elf_data_section->output_section = NULL;
6825 elf_data_section->owner = abfd;
6826 elf_data_symbol->name = ".data";
6827 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6828 elf_data_symbol->section = elf_data_section;
6829 }
6830 /* This code used to do *secp = bfd_und_section_ptr if
6831 info->shared. I don't know why, and that doesn't make sense,
6832 so I took it out. */
6833 *secp = elf_tdata (abfd)->elf_data_section;
6834 break;
6835
6836 case SHN_MIPS_SUNDEFINED:
6837 *secp = bfd_und_section_ptr;
6838 break;
6839 }
6840
6841 if (SGI_COMPAT (abfd)
6842 && ! info->shared
f13a99db 6843 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
6844 && strcmp (*namep, "__rld_obj_head") == 0)
6845 {
6846 struct elf_link_hash_entry *h;
14a793b2 6847 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6848
6849 /* Mark __rld_obj_head as dynamic. */
14a793b2 6850 bh = NULL;
b49e97c9 6851 if (! (_bfd_generic_link_add_one_symbol
9719ad41 6852 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 6853 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6854 return FALSE;
14a793b2
AM
6855
6856 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6857 h->non_elf = 0;
6858 h->def_regular = 1;
b49e97c9
TS
6859 h->type = STT_OBJECT;
6860
c152c796 6861 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6862 return FALSE;
b49e97c9 6863
b34976b6 6864 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b49e97c9
TS
6865 }
6866
6867 /* If this is a mips16 text symbol, add 1 to the value to make it
6868 odd. This will cause something like .word SYM to come up with
6869 the right value when it is loaded into the PC. */
30c09090 6870 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
6871 ++*valp;
6872
b34976b6 6873 return TRUE;
b49e97c9
TS
6874}
6875
6876/* This hook function is called before the linker writes out a global
6877 symbol. We mark symbols as small common if appropriate. This is
6878 also where we undo the increment of the value for a mips16 symbol. */
6879
6e0b88f1 6880int
9719ad41
RS
6881_bfd_mips_elf_link_output_symbol_hook
6882 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6883 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6884 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
6885{
6886 /* If we see a common symbol, which implies a relocatable link, then
6887 if a symbol was small common in an input file, mark it as small
6888 common in the output file. */
6889 if (sym->st_shndx == SHN_COMMON
6890 && strcmp (input_sec->name, ".scommon") == 0)
6891 sym->st_shndx = SHN_MIPS_SCOMMON;
6892
30c09090 6893 if (ELF_ST_IS_MIPS16 (sym->st_other))
79cda7cf 6894 sym->st_value &= ~1;
b49e97c9 6895
6e0b88f1 6896 return 1;
b49e97c9
TS
6897}
6898\f
6899/* Functions for the dynamic linker. */
6900
6901/* Create dynamic sections when linking against a dynamic object. */
6902
b34976b6 6903bfd_boolean
9719ad41 6904_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
6905{
6906 struct elf_link_hash_entry *h;
14a793b2 6907 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6908 flagword flags;
6909 register asection *s;
6910 const char * const *namep;
0a44bf69 6911 struct mips_elf_link_hash_table *htab;
b49e97c9 6912
0a44bf69 6913 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6914 BFD_ASSERT (htab != NULL);
6915
b49e97c9
TS
6916 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6917 | SEC_LINKER_CREATED | SEC_READONLY);
6918
0a44bf69
RS
6919 /* The psABI requires a read-only .dynamic section, but the VxWorks
6920 EABI doesn't. */
6921 if (!htab->is_vxworks)
b49e97c9 6922 {
0a44bf69
RS
6923 s = bfd_get_section_by_name (abfd, ".dynamic");
6924 if (s != NULL)
6925 {
6926 if (! bfd_set_section_flags (abfd, s, flags))
6927 return FALSE;
6928 }
b49e97c9
TS
6929 }
6930
6931 /* We need to create .got section. */
23cc69b6 6932 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
6933 return FALSE;
6934
0a44bf69 6935 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 6936 return FALSE;
b49e97c9 6937
b49e97c9 6938 /* Create .stub section. */
4e41d0d7
RS
6939 s = bfd_make_section_with_flags (abfd,
6940 MIPS_ELF_STUB_SECTION_NAME (abfd),
6941 flags | SEC_CODE);
6942 if (s == NULL
6943 || ! bfd_set_section_alignment (abfd, s,
6944 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6945 return FALSE;
6946 htab->sstubs = s;
b49e97c9
TS
6947
6948 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6949 && !info->shared
6950 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6951 {
3496cb2a
L
6952 s = bfd_make_section_with_flags (abfd, ".rld_map",
6953 flags &~ (flagword) SEC_READONLY);
b49e97c9 6954 if (s == NULL
b49e97c9
TS
6955 || ! bfd_set_section_alignment (abfd, s,
6956 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 6957 return FALSE;
b49e97c9
TS
6958 }
6959
6960 /* On IRIX5, we adjust add some additional symbols and change the
6961 alignments of several sections. There is no ABI documentation
6962 indicating that this is necessary on IRIX6, nor any evidence that
6963 the linker takes such action. */
6964 if (IRIX_COMPAT (abfd) == ict_irix5)
6965 {
6966 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6967 {
14a793b2 6968 bh = NULL;
b49e97c9 6969 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
6970 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6971 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6972 return FALSE;
14a793b2
AM
6973
6974 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6975 h->non_elf = 0;
6976 h->def_regular = 1;
b49e97c9
TS
6977 h->type = STT_SECTION;
6978
c152c796 6979 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6980 return FALSE;
b49e97c9
TS
6981 }
6982
6983 /* We need to create a .compact_rel section. */
6984 if (SGI_COMPAT (abfd))
6985 {
6986 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 6987 return FALSE;
b49e97c9
TS
6988 }
6989
44c410de 6990 /* Change alignments of some sections. */
b49e97c9
TS
6991 s = bfd_get_section_by_name (abfd, ".hash");
6992 if (s != NULL)
d80dcc6a 6993 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6994 s = bfd_get_section_by_name (abfd, ".dynsym");
6995 if (s != NULL)
d80dcc6a 6996 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6997 s = bfd_get_section_by_name (abfd, ".dynstr");
6998 if (s != NULL)
d80dcc6a 6999 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7000 s = bfd_get_section_by_name (abfd, ".reginfo");
7001 if (s != NULL)
d80dcc6a 7002 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7003 s = bfd_get_section_by_name (abfd, ".dynamic");
7004 if (s != NULL)
d80dcc6a 7005 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7006 }
7007
7008 if (!info->shared)
7009 {
14a793b2
AM
7010 const char *name;
7011
7012 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7013 bh = NULL;
7014 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
7015 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7016 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7017 return FALSE;
14a793b2
AM
7018
7019 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7020 h->non_elf = 0;
7021 h->def_regular = 1;
b49e97c9
TS
7022 h->type = STT_SECTION;
7023
c152c796 7024 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7025 return FALSE;
b49e97c9
TS
7026
7027 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7028 {
7029 /* __rld_map is a four byte word located in the .data section
7030 and is filled in by the rtld to contain a pointer to
7031 the _r_debug structure. Its symbol value will be set in
7032 _bfd_mips_elf_finish_dynamic_symbol. */
7033 s = bfd_get_section_by_name (abfd, ".rld_map");
0abfb97a 7034 BFD_ASSERT (s != NULL);
14a793b2 7035
0abfb97a
L
7036 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7037 bh = NULL;
7038 if (!(_bfd_generic_link_add_one_symbol
7039 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7040 get_elf_backend_data (abfd)->collect, &bh)))
7041 return FALSE;
b49e97c9 7042
0abfb97a
L
7043 h = (struct elf_link_hash_entry *) bh;
7044 h->non_elf = 0;
7045 h->def_regular = 1;
7046 h->type = STT_OBJECT;
7047
7048 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7049 return FALSE;
b49e97c9
TS
7050 }
7051 }
7052
861fb55a
DJ
7053 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7054 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7055 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7056 return FALSE;
7057
7058 /* Cache the sections created above. */
7059 htab->splt = bfd_get_section_by_name (abfd, ".plt");
7060 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
0a44bf69
RS
7061 if (htab->is_vxworks)
7062 {
0a44bf69
RS
7063 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7064 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
861fb55a
DJ
7065 }
7066 else
7067 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7068 if (!htab->sdynbss
7069 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7070 || !htab->srelplt
7071 || !htab->splt)
7072 abort ();
0a44bf69 7073
861fb55a
DJ
7074 if (htab->is_vxworks)
7075 {
0a44bf69
RS
7076 /* Do the usual VxWorks handling. */
7077 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7078 return FALSE;
7079
7080 /* Work out the PLT sizes. */
7081 if (info->shared)
7082 {
7083 htab->plt_header_size
7084 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7085 htab->plt_entry_size
7086 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7087 }
7088 else
7089 {
7090 htab->plt_header_size
7091 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7092 htab->plt_entry_size
7093 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7094 }
7095 }
861fb55a
DJ
7096 else if (!info->shared)
7097 {
7098 /* All variants of the plt0 entry are the same size. */
7099 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7100 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7101 }
0a44bf69 7102
b34976b6 7103 return TRUE;
b49e97c9
TS
7104}
7105\f
c224138d
RS
7106/* Return true if relocation REL against section SEC is a REL rather than
7107 RELA relocation. RELOCS is the first relocation in the section and
7108 ABFD is the bfd that contains SEC. */
7109
7110static bfd_boolean
7111mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7112 const Elf_Internal_Rela *relocs,
7113 const Elf_Internal_Rela *rel)
7114{
7115 Elf_Internal_Shdr *rel_hdr;
7116 const struct elf_backend_data *bed;
7117
7118 /* To determine which flavor or relocation this is, we depend on the
7119 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
7120 rel_hdr = &elf_section_data (sec)->rel_hdr;
7121 bed = get_elf_backend_data (abfd);
7122 if ((size_t) (rel - relocs)
7123 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7124 rel_hdr = elf_section_data (sec)->rel_hdr2;
7125 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
7126}
7127
7128/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7129 HOWTO is the relocation's howto and CONTENTS points to the contents
7130 of the section that REL is against. */
7131
7132static bfd_vma
7133mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7134 reloc_howto_type *howto, bfd_byte *contents)
7135{
7136 bfd_byte *location;
7137 unsigned int r_type;
7138 bfd_vma addend;
7139
7140 r_type = ELF_R_TYPE (abfd, rel->r_info);
7141 location = contents + rel->r_offset;
7142
7143 /* Get the addend, which is stored in the input file. */
7144 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7145 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7146 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7147
7148 return addend & howto->src_mask;
7149}
7150
7151/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7152 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7153 and update *ADDEND with the final addend. Return true on success
7154 or false if the LO16 could not be found. RELEND is the exclusive
7155 upper bound on the relocations for REL's section. */
7156
7157static bfd_boolean
7158mips_elf_add_lo16_rel_addend (bfd *abfd,
7159 const Elf_Internal_Rela *rel,
7160 const Elf_Internal_Rela *relend,
7161 bfd_byte *contents, bfd_vma *addend)
7162{
7163 unsigned int r_type, lo16_type;
7164 const Elf_Internal_Rela *lo16_relocation;
7165 reloc_howto_type *lo16_howto;
7166 bfd_vma l;
7167
7168 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7169 if (mips16_reloc_p (r_type))
c224138d
RS
7170 lo16_type = R_MIPS16_LO16;
7171 else
7172 lo16_type = R_MIPS_LO16;
7173
7174 /* The combined value is the sum of the HI16 addend, left-shifted by
7175 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7176 code does a `lui' of the HI16 value, and then an `addiu' of the
7177 LO16 value.)
7178
7179 Scan ahead to find a matching LO16 relocation.
7180
7181 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7182 be immediately following. However, for the IRIX6 ABI, the next
7183 relocation may be a composed relocation consisting of several
7184 relocations for the same address. In that case, the R_MIPS_LO16
7185 relocation may occur as one of these. We permit a similar
7186 extension in general, as that is useful for GCC.
7187
7188 In some cases GCC dead code elimination removes the LO16 but keeps
7189 the corresponding HI16. This is strictly speaking a violation of
7190 the ABI but not immediately harmful. */
7191 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7192 if (lo16_relocation == NULL)
7193 return FALSE;
7194
7195 /* Obtain the addend kept there. */
7196 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7197 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7198
7199 l <<= lo16_howto->rightshift;
7200 l = _bfd_mips_elf_sign_extend (l, 16);
7201
7202 *addend <<= 16;
7203 *addend += l;
7204 return TRUE;
7205}
7206
7207/* Try to read the contents of section SEC in bfd ABFD. Return true and
7208 store the contents in *CONTENTS on success. Assume that *CONTENTS
7209 already holds the contents if it is nonull on entry. */
7210
7211static bfd_boolean
7212mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7213{
7214 if (*contents)
7215 return TRUE;
7216
7217 /* Get cached copy if it exists. */
7218 if (elf_section_data (sec)->this_hdr.contents != NULL)
7219 {
7220 *contents = elf_section_data (sec)->this_hdr.contents;
7221 return TRUE;
7222 }
7223
7224 return bfd_malloc_and_get_section (abfd, sec, contents);
7225}
7226
b49e97c9
TS
7227/* Look through the relocs for a section during the first phase, and
7228 allocate space in the global offset table. */
7229
b34976b6 7230bfd_boolean
9719ad41
RS
7231_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7232 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7233{
7234 const char *name;
7235 bfd *dynobj;
7236 Elf_Internal_Shdr *symtab_hdr;
7237 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7238 size_t extsymoff;
7239 const Elf_Internal_Rela *rel;
7240 const Elf_Internal_Rela *rel_end;
b49e97c9 7241 asection *sreloc;
9c5bfbb7 7242 const struct elf_backend_data *bed;
0a44bf69 7243 struct mips_elf_link_hash_table *htab;
c224138d
RS
7244 bfd_byte *contents;
7245 bfd_vma addend;
7246 reloc_howto_type *howto;
b49e97c9 7247
1049f94e 7248 if (info->relocatable)
b34976b6 7249 return TRUE;
b49e97c9 7250
0a44bf69 7251 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7252 BFD_ASSERT (htab != NULL);
7253
b49e97c9
TS
7254 dynobj = elf_hash_table (info)->dynobj;
7255 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7256 sym_hashes = elf_sym_hashes (abfd);
7257 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7258
738e5348
RS
7259 bed = get_elf_backend_data (abfd);
7260 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7261
b49e97c9
TS
7262 /* Check for the mips16 stub sections. */
7263
7264 name = bfd_get_section_name (abfd, sec);
b9d58d71 7265 if (FN_STUB_P (name))
b49e97c9
TS
7266 {
7267 unsigned long r_symndx;
7268
7269 /* Look at the relocation information to figure out which symbol
7270 this is for. */
7271
738e5348
RS
7272 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7273 if (r_symndx == 0)
7274 {
7275 (*_bfd_error_handler)
7276 (_("%B: Warning: cannot determine the target function for"
7277 " stub section `%s'"),
7278 abfd, name);
7279 bfd_set_error (bfd_error_bad_value);
7280 return FALSE;
7281 }
b49e97c9
TS
7282
7283 if (r_symndx < extsymoff
7284 || sym_hashes[r_symndx - extsymoff] == NULL)
7285 {
7286 asection *o;
7287
7288 /* This stub is for a local symbol. This stub will only be
7289 needed if there is some relocation in this BFD, other
7290 than a 16 bit function call, which refers to this symbol. */
7291 for (o = abfd->sections; o != NULL; o = o->next)
7292 {
7293 Elf_Internal_Rela *sec_relocs;
7294 const Elf_Internal_Rela *r, *rend;
7295
7296 /* We can ignore stub sections when looking for relocs. */
7297 if ((o->flags & SEC_RELOC) == 0
7298 || o->reloc_count == 0
738e5348 7299 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7300 continue;
7301
45d6a902 7302 sec_relocs
9719ad41 7303 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7304 info->keep_memory);
b49e97c9 7305 if (sec_relocs == NULL)
b34976b6 7306 return FALSE;
b49e97c9
TS
7307
7308 rend = sec_relocs + o->reloc_count;
7309 for (r = sec_relocs; r < rend; r++)
7310 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7311 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7312 break;
7313
6cdc0ccc 7314 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7315 free (sec_relocs);
7316
7317 if (r < rend)
7318 break;
7319 }
7320
7321 if (o == NULL)
7322 {
7323 /* There is no non-call reloc for this stub, so we do
7324 not need it. Since this function is called before
7325 the linker maps input sections to output sections, we
7326 can easily discard it by setting the SEC_EXCLUDE
7327 flag. */
7328 sec->flags |= SEC_EXCLUDE;
b34976b6 7329 return TRUE;
b49e97c9
TS
7330 }
7331
7332 /* Record this stub in an array of local symbol stubs for
7333 this BFD. */
7334 if (elf_tdata (abfd)->local_stubs == NULL)
7335 {
7336 unsigned long symcount;
7337 asection **n;
7338 bfd_size_type amt;
7339
7340 if (elf_bad_symtab (abfd))
7341 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7342 else
7343 symcount = symtab_hdr->sh_info;
7344 amt = symcount * sizeof (asection *);
9719ad41 7345 n = bfd_zalloc (abfd, amt);
b49e97c9 7346 if (n == NULL)
b34976b6 7347 return FALSE;
b49e97c9
TS
7348 elf_tdata (abfd)->local_stubs = n;
7349 }
7350
b9d58d71 7351 sec->flags |= SEC_KEEP;
b49e97c9
TS
7352 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7353
7354 /* We don't need to set mips16_stubs_seen in this case.
7355 That flag is used to see whether we need to look through
7356 the global symbol table for stubs. We don't need to set
7357 it here, because we just have a local stub. */
7358 }
7359 else
7360 {
7361 struct mips_elf_link_hash_entry *h;
7362
7363 h = ((struct mips_elf_link_hash_entry *)
7364 sym_hashes[r_symndx - extsymoff]);
7365
973a3492
L
7366 while (h->root.root.type == bfd_link_hash_indirect
7367 || h->root.root.type == bfd_link_hash_warning)
7368 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7369
b49e97c9
TS
7370 /* H is the symbol this stub is for. */
7371
b9d58d71
TS
7372 /* If we already have an appropriate stub for this function, we
7373 don't need another one, so we can discard this one. Since
7374 this function is called before the linker maps input sections
7375 to output sections, we can easily discard it by setting the
7376 SEC_EXCLUDE flag. */
7377 if (h->fn_stub != NULL)
7378 {
7379 sec->flags |= SEC_EXCLUDE;
7380 return TRUE;
7381 }
7382
7383 sec->flags |= SEC_KEEP;
b49e97c9 7384 h->fn_stub = sec;
b34976b6 7385 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7386 }
7387 }
b9d58d71 7388 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7389 {
7390 unsigned long r_symndx;
7391 struct mips_elf_link_hash_entry *h;
7392 asection **loc;
7393
7394 /* Look at the relocation information to figure out which symbol
7395 this is for. */
7396
738e5348
RS
7397 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7398 if (r_symndx == 0)
7399 {
7400 (*_bfd_error_handler)
7401 (_("%B: Warning: cannot determine the target function for"
7402 " stub section `%s'"),
7403 abfd, name);
7404 bfd_set_error (bfd_error_bad_value);
7405 return FALSE;
7406 }
b49e97c9
TS
7407
7408 if (r_symndx < extsymoff
7409 || sym_hashes[r_symndx - extsymoff] == NULL)
7410 {
b9d58d71 7411 asection *o;
b49e97c9 7412
b9d58d71
TS
7413 /* This stub is for a local symbol. This stub will only be
7414 needed if there is some relocation (R_MIPS16_26) in this BFD
7415 that refers to this symbol. */
7416 for (o = abfd->sections; o != NULL; o = o->next)
7417 {
7418 Elf_Internal_Rela *sec_relocs;
7419 const Elf_Internal_Rela *r, *rend;
7420
7421 /* We can ignore stub sections when looking for relocs. */
7422 if ((o->flags & SEC_RELOC) == 0
7423 || o->reloc_count == 0
738e5348 7424 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7425 continue;
7426
7427 sec_relocs
7428 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7429 info->keep_memory);
7430 if (sec_relocs == NULL)
7431 return FALSE;
7432
7433 rend = sec_relocs + o->reloc_count;
7434 for (r = sec_relocs; r < rend; r++)
7435 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7436 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7437 break;
7438
7439 if (elf_section_data (o)->relocs != sec_relocs)
7440 free (sec_relocs);
7441
7442 if (r < rend)
7443 break;
7444 }
7445
7446 if (o == NULL)
7447 {
7448 /* There is no non-call reloc for this stub, so we do
7449 not need it. Since this function is called before
7450 the linker maps input sections to output sections, we
7451 can easily discard it by setting the SEC_EXCLUDE
7452 flag. */
7453 sec->flags |= SEC_EXCLUDE;
7454 return TRUE;
7455 }
7456
7457 /* Record this stub in an array of local symbol call_stubs for
7458 this BFD. */
7459 if (elf_tdata (abfd)->local_call_stubs == NULL)
7460 {
7461 unsigned long symcount;
7462 asection **n;
7463 bfd_size_type amt;
7464
7465 if (elf_bad_symtab (abfd))
7466 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7467 else
7468 symcount = symtab_hdr->sh_info;
7469 amt = symcount * sizeof (asection *);
7470 n = bfd_zalloc (abfd, amt);
7471 if (n == NULL)
7472 return FALSE;
7473 elf_tdata (abfd)->local_call_stubs = n;
7474 }
b49e97c9 7475
b9d58d71
TS
7476 sec->flags |= SEC_KEEP;
7477 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7478
b9d58d71
TS
7479 /* We don't need to set mips16_stubs_seen in this case.
7480 That flag is used to see whether we need to look through
7481 the global symbol table for stubs. We don't need to set
7482 it here, because we just have a local stub. */
7483 }
b49e97c9 7484 else
b49e97c9 7485 {
b9d58d71
TS
7486 h = ((struct mips_elf_link_hash_entry *)
7487 sym_hashes[r_symndx - extsymoff]);
7488
7489 /* H is the symbol this stub is for. */
7490
7491 if (CALL_FP_STUB_P (name))
7492 loc = &h->call_fp_stub;
7493 else
7494 loc = &h->call_stub;
7495
7496 /* If we already have an appropriate stub for this function, we
7497 don't need another one, so we can discard this one. Since
7498 this function is called before the linker maps input sections
7499 to output sections, we can easily discard it by setting the
7500 SEC_EXCLUDE flag. */
7501 if (*loc != NULL)
7502 {
7503 sec->flags |= SEC_EXCLUDE;
7504 return TRUE;
7505 }
b49e97c9 7506
b9d58d71
TS
7507 sec->flags |= SEC_KEEP;
7508 *loc = sec;
7509 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7510 }
b49e97c9
TS
7511 }
7512
b49e97c9 7513 sreloc = NULL;
c224138d 7514 contents = NULL;
b49e97c9
TS
7515 for (rel = relocs; rel < rel_end; ++rel)
7516 {
7517 unsigned long r_symndx;
7518 unsigned int r_type;
7519 struct elf_link_hash_entry *h;
861fb55a 7520 bfd_boolean can_make_dynamic_p;
b49e97c9
TS
7521
7522 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7523 r_type = ELF_R_TYPE (abfd, rel->r_info);
7524
7525 if (r_symndx < extsymoff)
7526 h = NULL;
7527 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7528 {
7529 (*_bfd_error_handler)
d003868e
AM
7530 (_("%B: Malformed reloc detected for section %s"),
7531 abfd, name);
b49e97c9 7532 bfd_set_error (bfd_error_bad_value);
b34976b6 7533 return FALSE;
b49e97c9
TS
7534 }
7535 else
7536 {
7537 h = sym_hashes[r_symndx - extsymoff];
3e08fb72
NC
7538 while (h != NULL
7539 && (h->root.type == bfd_link_hash_indirect
7540 || h->root.type == bfd_link_hash_warning))
861fb55a
DJ
7541 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7542 }
b49e97c9 7543
861fb55a
DJ
7544 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7545 relocation into a dynamic one. */
7546 can_make_dynamic_p = FALSE;
7547 switch (r_type)
7548 {
7549 case R_MIPS16_GOT16:
7550 case R_MIPS16_CALL16:
7551 case R_MIPS_GOT16:
7552 case R_MIPS_CALL16:
7553 case R_MIPS_CALL_HI16:
7554 case R_MIPS_CALL_LO16:
7555 case R_MIPS_GOT_HI16:
7556 case R_MIPS_GOT_LO16:
7557 case R_MIPS_GOT_PAGE:
7558 case R_MIPS_GOT_OFST:
7559 case R_MIPS_GOT_DISP:
7560 case R_MIPS_TLS_GOTTPREL:
7561 case R_MIPS_TLS_GD:
7562 case R_MIPS_TLS_LDM:
7563 if (dynobj == NULL)
7564 elf_hash_table (info)->dynobj = dynobj = abfd;
7565 if (!mips_elf_create_got_section (dynobj, info))
7566 return FALSE;
7567 if (htab->is_vxworks && !info->shared)
b49e97c9 7568 {
861fb55a
DJ
7569 (*_bfd_error_handler)
7570 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7571 abfd, (unsigned long) rel->r_offset);
7572 bfd_set_error (bfd_error_bad_value);
7573 return FALSE;
b49e97c9 7574 }
861fb55a 7575 break;
b49e97c9 7576
99da6b5f
AN
7577 /* This is just a hint; it can safely be ignored. Don't set
7578 has_static_relocs for the corresponding symbol. */
7579 case R_MIPS_JALR:
7580 break;
7581
861fb55a
DJ
7582 case R_MIPS_32:
7583 case R_MIPS_REL32:
7584 case R_MIPS_64:
7585 /* In VxWorks executables, references to external symbols
7586 must be handled using copy relocs or PLT entries; it is not
7587 possible to convert this relocation into a dynamic one.
7588
7589 For executables that use PLTs and copy-relocs, we have a
7590 choice between converting the relocation into a dynamic
7591 one or using copy relocations or PLT entries. It is
7592 usually better to do the former, unless the relocation is
7593 against a read-only section. */
7594 if ((info->shared
7595 || (h != NULL
7596 && !htab->is_vxworks
7597 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7598 && !(!info->nocopyreloc
7599 && !PIC_OBJECT_P (abfd)
7600 && MIPS_ELF_READONLY_SECTION (sec))))
7601 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 7602 {
861fb55a 7603 can_make_dynamic_p = TRUE;
b49e97c9
TS
7604 if (dynobj == NULL)
7605 elf_hash_table (info)->dynobj = dynobj = abfd;
b49e97c9 7606 break;
861fb55a
DJ
7607 }
7608 /* Fall through. */
b49e97c9 7609
861fb55a
DJ
7610 default:
7611 /* Most static relocations require pointer equality, except
7612 for branches. */
7613 if (h)
7614 h->pointer_equality_needed = TRUE;
7615 /* Fall through. */
b49e97c9 7616
861fb55a
DJ
7617 case R_MIPS_26:
7618 case R_MIPS_PC16:
7619 case R_MIPS16_26:
7620 if (h)
7621 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7622 break;
b49e97c9
TS
7623 }
7624
0a44bf69
RS
7625 if (h)
7626 {
0a44bf69
RS
7627 /* Relocations against the special VxWorks __GOTT_BASE__ and
7628 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7629 room for them in .rela.dyn. */
7630 if (is_gott_symbol (info, h))
7631 {
7632 if (sreloc == NULL)
7633 {
7634 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7635 if (sreloc == NULL)
7636 return FALSE;
7637 }
7638 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
7639 if (MIPS_ELF_READONLY_SECTION (sec))
7640 /* We tell the dynamic linker that there are
7641 relocations against the text segment. */
7642 info->flags |= DF_TEXTREL;
0a44bf69
RS
7643 }
7644 }
7645 else if (r_type == R_MIPS_CALL_LO16
7646 || r_type == R_MIPS_GOT_LO16
7647 || r_type == R_MIPS_GOT_DISP
738e5348 7648 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
7649 {
7650 /* We may need a local GOT entry for this relocation. We
7651 don't count R_MIPS_GOT_PAGE because we can estimate the
7652 maximum number of pages needed by looking at the size of
738e5348
RS
7653 the segment. Similar comments apply to R_MIPS*_GOT16 and
7654 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 7655 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 7656 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 7657 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0
RS
7658 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7659 rel->r_addend, info, 0))
f4416af6 7660 return FALSE;
b49e97c9
TS
7661 }
7662
861fb55a
DJ
7663 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7664 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7665
b49e97c9
TS
7666 switch (r_type)
7667 {
7668 case R_MIPS_CALL16:
738e5348 7669 case R_MIPS16_CALL16:
b49e97c9
TS
7670 if (h == NULL)
7671 {
7672 (*_bfd_error_handler)
d003868e
AM
7673 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7674 abfd, (unsigned long) rel->r_offset);
b49e97c9 7675 bfd_set_error (bfd_error_bad_value);
b34976b6 7676 return FALSE;
b49e97c9
TS
7677 }
7678 /* Fall through. */
7679
7680 case R_MIPS_CALL_HI16:
7681 case R_MIPS_CALL_LO16:
7682 if (h != NULL)
7683 {
d334575b 7684 /* VxWorks call relocations point at the function's .got.plt
0a44bf69
RS
7685 entry, which will be allocated by adjust_dynamic_symbol.
7686 Otherwise, this symbol requires a global GOT entry. */
8275b357 7687 if ((!htab->is_vxworks || h->forced_local)
a8028dd0 7688 && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
b34976b6 7689 return FALSE;
b49e97c9
TS
7690
7691 /* We need a stub, not a plt entry for the undefined
7692 function. But we record it as if it needs plt. See
c152c796 7693 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 7694 h->needs_plt = 1;
b49e97c9
TS
7695 h->type = STT_FUNC;
7696 }
7697 break;
7698
0fdc1bf1
AO
7699 case R_MIPS_GOT_PAGE:
7700 /* If this is a global, overridable symbol, GOT_PAGE will
7701 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d 7702 if (h)
0fdc1bf1
AO
7703 {
7704 struct mips_elf_link_hash_entry *hmips =
7705 (struct mips_elf_link_hash_entry *) h;
143d77c5 7706
3a3b6725 7707 /* This symbol is definitely not overridable. */
f5385ebf 7708 if (hmips->root.def_regular
0fdc1bf1 7709 && ! (info->shared && ! info->symbolic
f5385ebf 7710 && ! hmips->root.forced_local))
c224138d 7711 h = NULL;
0fdc1bf1
AO
7712 }
7713 /* Fall through. */
7714
738e5348 7715 case R_MIPS16_GOT16:
b49e97c9
TS
7716 case R_MIPS_GOT16:
7717 case R_MIPS_GOT_HI16:
7718 case R_MIPS_GOT_LO16:
3a3b6725 7719 if (!h || r_type == R_MIPS_GOT_PAGE)
c224138d 7720 {
3a3b6725
DJ
7721 /* This relocation needs (or may need, if h != NULL) a
7722 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7723 know for sure until we know whether the symbol is
7724 preemptible. */
c224138d
RS
7725 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7726 {
7727 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7728 return FALSE;
7729 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7730 addend = mips_elf_read_rel_addend (abfd, rel,
7731 howto, contents);
7732 if (r_type == R_MIPS_GOT16)
7733 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7734 contents, &addend);
7735 else
7736 addend <<= howto->rightshift;
7737 }
7738 else
7739 addend = rel->r_addend;
a8028dd0
RS
7740 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7741 addend))
c224138d
RS
7742 return FALSE;
7743 break;
7744 }
7745 /* Fall through. */
7746
b49e97c9 7747 case R_MIPS_GOT_DISP:
a8028dd0 7748 if (h && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
b34976b6 7749 return FALSE;
b49e97c9
TS
7750 break;
7751
0f20cc35
DJ
7752 case R_MIPS_TLS_GOTTPREL:
7753 if (info->shared)
7754 info->flags |= DF_STATIC_TLS;
7755 /* Fall through */
7756
7757 case R_MIPS_TLS_LDM:
7758 if (r_type == R_MIPS_TLS_LDM)
7759 {
7760 r_symndx = 0;
7761 h = NULL;
7762 }
7763 /* Fall through */
7764
7765 case R_MIPS_TLS_GD:
7766 /* This symbol requires a global offset table entry, or two
7767 for TLS GD relocations. */
7768 {
7769 unsigned char flag = (r_type == R_MIPS_TLS_GD
7770 ? GOT_TLS_GD
7771 : r_type == R_MIPS_TLS_LDM
7772 ? GOT_TLS_LDM
7773 : GOT_TLS_IE);
7774 if (h != NULL)
7775 {
7776 struct mips_elf_link_hash_entry *hmips =
7777 (struct mips_elf_link_hash_entry *) h;
7778 hmips->tls_type |= flag;
7779
a8028dd0
RS
7780 if (h && !mips_elf_record_global_got_symbol (h, abfd,
7781 info, flag))
0f20cc35
DJ
7782 return FALSE;
7783 }
7784 else
7785 {
7786 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7787
a8028dd0
RS
7788 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7789 rel->r_addend,
7790 info, flag))
0f20cc35
DJ
7791 return FALSE;
7792 }
7793 }
7794 break;
7795
b49e97c9
TS
7796 case R_MIPS_32:
7797 case R_MIPS_REL32:
7798 case R_MIPS_64:
0a44bf69
RS
7799 /* In VxWorks executables, references to external symbols
7800 are handled using copy relocs or PLT stubs, so there's
7801 no need to add a .rela.dyn entry for this relocation. */
861fb55a 7802 if (can_make_dynamic_p)
b49e97c9
TS
7803 {
7804 if (sreloc == NULL)
7805 {
0a44bf69 7806 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 7807 if (sreloc == NULL)
f4416af6 7808 return FALSE;
b49e97c9 7809 }
9a59ad6b 7810 if (info->shared && h == NULL)
82f0cfbd
EC
7811 {
7812 /* When creating a shared object, we must copy these
7813 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
7814 relocs. Make room for this reloc in .rel(a).dyn. */
7815 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 7816 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7817 /* We tell the dynamic linker that there are
7818 relocations against the text segment. */
7819 info->flags |= DF_TEXTREL;
7820 }
b49e97c9
TS
7821 else
7822 {
7823 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 7824
9a59ad6b
DJ
7825 /* For a shared object, we must copy this relocation
7826 unless the symbol turns out to be undefined and
7827 weak with non-default visibility, in which case
7828 it will be left as zero.
7829
7830 We could elide R_MIPS_REL32 for locally binding symbols
7831 in shared libraries, but do not yet do so.
7832
7833 For an executable, we only need to copy this
7834 reloc if the symbol is defined in a dynamic
7835 object. */
b49e97c9
TS
7836 hmips = (struct mips_elf_link_hash_entry *) h;
7837 ++hmips->possibly_dynamic_relocs;
943284cc 7838 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7839 /* We need it to tell the dynamic linker if there
7840 are relocations against the text segment. */
7841 hmips->readonly_reloc = TRUE;
b49e97c9 7842 }
b49e97c9
TS
7843 }
7844
7845 if (SGI_COMPAT (abfd))
7846 mips_elf_hash_table (info)->compact_rel_size +=
7847 sizeof (Elf32_External_crinfo);
7848 break;
7849
7850 case R_MIPS_26:
7851 case R_MIPS_GPREL16:
7852 case R_MIPS_LITERAL:
7853 case R_MIPS_GPREL32:
7854 if (SGI_COMPAT (abfd))
7855 mips_elf_hash_table (info)->compact_rel_size +=
7856 sizeof (Elf32_External_crinfo);
7857 break;
7858
7859 /* This relocation describes the C++ object vtable hierarchy.
7860 Reconstruct it for later use during GC. */
7861 case R_MIPS_GNU_VTINHERIT:
c152c796 7862 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 7863 return FALSE;
b49e97c9
TS
7864 break;
7865
7866 /* This relocation describes which C++ vtable entries are actually
7867 used. Record for later use during GC. */
7868 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
7869 BFD_ASSERT (h != NULL);
7870 if (h != NULL
7871 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 7872 return FALSE;
b49e97c9
TS
7873 break;
7874
7875 default:
7876 break;
7877 }
7878
7879 /* We must not create a stub for a symbol that has relocations
0a44bf69
RS
7880 related to taking the function's address. This doesn't apply to
7881 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7882 a normal .got entry. */
7883 if (!htab->is_vxworks && h != NULL)
7884 switch (r_type)
7885 {
7886 default:
7887 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7888 break;
738e5348 7889 case R_MIPS16_CALL16:
0a44bf69
RS
7890 case R_MIPS_CALL16:
7891 case R_MIPS_CALL_HI16:
7892 case R_MIPS_CALL_LO16:
7893 case R_MIPS_JALR:
7894 break;
7895 }
b49e97c9 7896
738e5348
RS
7897 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7898 if there is one. We only need to handle global symbols here;
7899 we decide whether to keep or delete stubs for local symbols
7900 when processing the stub's relocations. */
b49e97c9 7901 if (h != NULL
738e5348
RS
7902 && !mips16_call_reloc_p (r_type)
7903 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
7904 {
7905 struct mips_elf_link_hash_entry *mh;
7906
7907 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 7908 mh->need_fn_stub = TRUE;
b49e97c9 7909 }
861fb55a
DJ
7910
7911 /* Refuse some position-dependent relocations when creating a
7912 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7913 not PIC, but we can create dynamic relocations and the result
7914 will be fine. Also do not refuse R_MIPS_LO16, which can be
7915 combined with R_MIPS_GOT16. */
7916 if (info->shared)
7917 {
7918 switch (r_type)
7919 {
7920 case R_MIPS16_HI16:
7921 case R_MIPS_HI16:
7922 case R_MIPS_HIGHER:
7923 case R_MIPS_HIGHEST:
7924 /* Don't refuse a high part relocation if it's against
7925 no symbol (e.g. part of a compound relocation). */
7926 if (r_symndx == 0)
7927 break;
7928
7929 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7930 and has a special meaning. */
7931 if (!NEWABI_P (abfd) && h != NULL
7932 && strcmp (h->root.root.string, "_gp_disp") == 0)
7933 break;
7934
7935 /* FALLTHROUGH */
7936
7937 case R_MIPS16_26:
7938 case R_MIPS_26:
7939 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7940 (*_bfd_error_handler)
7941 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7942 abfd, howto->name,
7943 (h) ? h->root.root.string : "a local symbol");
7944 bfd_set_error (bfd_error_bad_value);
7945 return FALSE;
7946 default:
7947 break;
7948 }
7949 }
b49e97c9
TS
7950 }
7951
b34976b6 7952 return TRUE;
b49e97c9
TS
7953}
7954\f
d0647110 7955bfd_boolean
9719ad41
RS
7956_bfd_mips_relax_section (bfd *abfd, asection *sec,
7957 struct bfd_link_info *link_info,
7958 bfd_boolean *again)
d0647110
AO
7959{
7960 Elf_Internal_Rela *internal_relocs;
7961 Elf_Internal_Rela *irel, *irelend;
7962 Elf_Internal_Shdr *symtab_hdr;
7963 bfd_byte *contents = NULL;
d0647110
AO
7964 size_t extsymoff;
7965 bfd_boolean changed_contents = FALSE;
7966 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7967 Elf_Internal_Sym *isymbuf = NULL;
7968
7969 /* We are not currently changing any sizes, so only one pass. */
7970 *again = FALSE;
7971
1049f94e 7972 if (link_info->relocatable)
d0647110
AO
7973 return TRUE;
7974
9719ad41 7975 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 7976 link_info->keep_memory);
d0647110
AO
7977 if (internal_relocs == NULL)
7978 return TRUE;
7979
7980 irelend = internal_relocs + sec->reloc_count
7981 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7982 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7983 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7984
7985 for (irel = internal_relocs; irel < irelend; irel++)
7986 {
7987 bfd_vma symval;
7988 bfd_signed_vma sym_offset;
7989 unsigned int r_type;
7990 unsigned long r_symndx;
7991 asection *sym_sec;
7992 unsigned long instruction;
7993
7994 /* Turn jalr into bgezal, and jr into beq, if they're marked
7995 with a JALR relocation, that indicate where they jump to.
7996 This saves some pipeline bubbles. */
7997 r_type = ELF_R_TYPE (abfd, irel->r_info);
7998 if (r_type != R_MIPS_JALR)
7999 continue;
8000
8001 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8002 /* Compute the address of the jump target. */
8003 if (r_symndx >= extsymoff)
8004 {
8005 struct mips_elf_link_hash_entry *h
8006 = ((struct mips_elf_link_hash_entry *)
8007 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8008
8009 while (h->root.root.type == bfd_link_hash_indirect
8010 || h->root.root.type == bfd_link_hash_warning)
8011 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 8012
d0647110
AO
8013 /* If a symbol is undefined, or if it may be overridden,
8014 skip it. */
8015 if (! ((h->root.root.type == bfd_link_hash_defined
8016 || h->root.root.type == bfd_link_hash_defweak)
8017 && h->root.root.u.def.section)
8018 || (link_info->shared && ! link_info->symbolic
f5385ebf 8019 && !h->root.forced_local))
d0647110
AO
8020 continue;
8021
8022 sym_sec = h->root.root.u.def.section;
8023 if (sym_sec->output_section)
8024 symval = (h->root.root.u.def.value
8025 + sym_sec->output_section->vma
8026 + sym_sec->output_offset);
8027 else
8028 symval = h->root.root.u.def.value;
8029 }
8030 else
8031 {
8032 Elf_Internal_Sym *isym;
8033
8034 /* Read this BFD's symbols if we haven't done so already. */
8035 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8036 {
8037 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8038 if (isymbuf == NULL)
8039 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8040 symtab_hdr->sh_info, 0,
8041 NULL, NULL, NULL);
8042 if (isymbuf == NULL)
8043 goto relax_return;
8044 }
8045
8046 isym = isymbuf + r_symndx;
8047 if (isym->st_shndx == SHN_UNDEF)
8048 continue;
8049 else if (isym->st_shndx == SHN_ABS)
8050 sym_sec = bfd_abs_section_ptr;
8051 else if (isym->st_shndx == SHN_COMMON)
8052 sym_sec = bfd_com_section_ptr;
8053 else
8054 sym_sec
8055 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8056 symval = isym->st_value
8057 + sym_sec->output_section->vma
8058 + sym_sec->output_offset;
8059 }
8060
8061 /* Compute branch offset, from delay slot of the jump to the
8062 branch target. */
8063 sym_offset = (symval + irel->r_addend)
8064 - (sec_start + irel->r_offset + 4);
8065
8066 /* Branch offset must be properly aligned. */
8067 if ((sym_offset & 3) != 0)
8068 continue;
8069
8070 sym_offset >>= 2;
8071
8072 /* Check that it's in range. */
8073 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8074 continue;
143d77c5 8075
d0647110 8076 /* Get the section contents if we haven't done so already. */
c224138d
RS
8077 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8078 goto relax_return;
d0647110
AO
8079
8080 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8081
8082 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8083 if ((instruction & 0xfc1fffff) == 0x0000f809)
8084 instruction = 0x04110000;
8085 /* If it was jr <reg>, turn it into b <target>. */
8086 else if ((instruction & 0xfc1fffff) == 0x00000008)
8087 instruction = 0x10000000;
8088 else
8089 continue;
8090
8091 instruction |= (sym_offset & 0xffff);
8092 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8093 changed_contents = TRUE;
8094 }
8095
8096 if (contents != NULL
8097 && elf_section_data (sec)->this_hdr.contents != contents)
8098 {
8099 if (!changed_contents && !link_info->keep_memory)
8100 free (contents);
8101 else
8102 {
8103 /* Cache the section contents for elf_link_input_bfd. */
8104 elf_section_data (sec)->this_hdr.contents = contents;
8105 }
8106 }
8107 return TRUE;
8108
143d77c5 8109 relax_return:
eea6121a
AM
8110 if (contents != NULL
8111 && elf_section_data (sec)->this_hdr.contents != contents)
8112 free (contents);
d0647110
AO
8113 return FALSE;
8114}
8115\f
9a59ad6b
DJ
8116/* Allocate space for global sym dynamic relocs. */
8117
8118static bfd_boolean
8119allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8120{
8121 struct bfd_link_info *info = inf;
8122 bfd *dynobj;
8123 struct mips_elf_link_hash_entry *hmips;
8124 struct mips_elf_link_hash_table *htab;
8125
8126 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8127 BFD_ASSERT (htab != NULL);
8128
9a59ad6b
DJ
8129 dynobj = elf_hash_table (info)->dynobj;
8130 hmips = (struct mips_elf_link_hash_entry *) h;
8131
8132 /* VxWorks executables are handled elsewhere; we only need to
8133 allocate relocations in shared objects. */
8134 if (htab->is_vxworks && !info->shared)
8135 return TRUE;
8136
63897e2c
RS
8137 /* Ignore indirect and warning symbols. All relocations against
8138 such symbols will be redirected to the target symbol. */
8139 if (h->root.type == bfd_link_hash_indirect
8140 || h->root.type == bfd_link_hash_warning)
8141 return TRUE;
8142
9a59ad6b
DJ
8143 /* If this symbol is defined in a dynamic object, or we are creating
8144 a shared library, we will need to copy any R_MIPS_32 or
8145 R_MIPS_REL32 relocs against it into the output file. */
8146 if (! info->relocatable
8147 && hmips->possibly_dynamic_relocs != 0
8148 && (h->root.type == bfd_link_hash_defweak
8149 || !h->def_regular
8150 || info->shared))
8151 {
8152 bfd_boolean do_copy = TRUE;
8153
8154 if (h->root.type == bfd_link_hash_undefweak)
8155 {
8156 /* Do not copy relocations for undefined weak symbols with
8157 non-default visibility. */
8158 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8159 do_copy = FALSE;
8160
8161 /* Make sure undefined weak symbols are output as a dynamic
8162 symbol in PIEs. */
8163 else if (h->dynindx == -1 && !h->forced_local)
8164 {
8165 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8166 return FALSE;
8167 }
8168 }
8169
8170 if (do_copy)
8171 {
aff469fa
RS
8172 /* Even though we don't directly need a GOT entry for this symbol,
8173 a symbol must have a dynamic symbol table index greater that
8174 DT_MIPS_GOTSYM if there are dynamic relocations against it. */
8175 if (hmips->global_got_area > GGA_RELOC_ONLY)
8176 hmips->global_got_area = GGA_RELOC_ONLY;
8177
9a59ad6b
DJ
8178 mips_elf_allocate_dynamic_relocations
8179 (dynobj, info, hmips->possibly_dynamic_relocs);
8180 if (hmips->readonly_reloc)
8181 /* We tell the dynamic linker that there are relocations
8182 against the text segment. */
8183 info->flags |= DF_TEXTREL;
8184 }
8185 }
8186
8187 return TRUE;
8188}
8189
b49e97c9
TS
8190/* Adjust a symbol defined by a dynamic object and referenced by a
8191 regular object. The current definition is in some section of the
8192 dynamic object, but we're not including those sections. We have to
8193 change the definition to something the rest of the link can
8194 understand. */
8195
b34976b6 8196bfd_boolean
9719ad41
RS
8197_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8198 struct elf_link_hash_entry *h)
b49e97c9
TS
8199{
8200 bfd *dynobj;
8201 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8202 struct mips_elf_link_hash_table *htab;
b49e97c9 8203
5108fc1b 8204 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8205 BFD_ASSERT (htab != NULL);
8206
b49e97c9 8207 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8208 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8209
8210 /* Make sure we know what is going on here. */
8211 BFD_ASSERT (dynobj != NULL
f5385ebf 8212 && (h->needs_plt
f6e332e6 8213 || h->u.weakdef != NULL
f5385ebf
AM
8214 || (h->def_dynamic
8215 && h->ref_regular
8216 && !h->def_regular)));
b49e97c9 8217
b49e97c9 8218 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8219
861fb55a
DJ
8220 /* If there are call relocations against an externally-defined symbol,
8221 see whether we can create a MIPS lazy-binding stub for it. We can
8222 only do this if all references to the function are through call
8223 relocations, and in that case, the traditional lazy-binding stubs
8224 are much more efficient than PLT entries.
8225
8226 Traditional stubs are only available on SVR4 psABI-based systems;
8227 VxWorks always uses PLTs instead. */
8228 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8229 {
8230 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8231 return TRUE;
b49e97c9
TS
8232
8233 /* If this symbol is not defined in a regular file, then set
8234 the symbol to the stub location. This is required to make
8235 function pointers compare as equal between the normal
8236 executable and the shared library. */
f5385ebf 8237 if (!h->def_regular)
b49e97c9 8238 {
33bb52fb
RS
8239 hmips->needs_lazy_stub = TRUE;
8240 htab->lazy_stub_count++;
b34976b6 8241 return TRUE;
b49e97c9
TS
8242 }
8243 }
861fb55a
DJ
8244 /* As above, VxWorks requires PLT entries for externally-defined
8245 functions that are only accessed through call relocations.
b49e97c9 8246
861fb55a
DJ
8247 Both VxWorks and non-VxWorks targets also need PLT entries if there
8248 are static-only relocations against an externally-defined function.
8249 This can technically occur for shared libraries if there are
8250 branches to the symbol, although it is unlikely that this will be
8251 used in practice due to the short ranges involved. It can occur
8252 for any relative or absolute relocation in executables; in that
8253 case, the PLT entry becomes the function's canonical address. */
8254 else if (((h->needs_plt && !hmips->no_fn_stub)
8255 || (h->type == STT_FUNC && hmips->has_static_relocs))
8256 && htab->use_plts_and_copy_relocs
8257 && !SYMBOL_CALLS_LOCAL (info, h)
8258 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8259 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8260 {
861fb55a
DJ
8261 /* If this is the first symbol to need a PLT entry, allocate room
8262 for the header. */
8263 if (htab->splt->size == 0)
8264 {
8265 BFD_ASSERT (htab->sgotplt->size == 0);
0a44bf69 8266
861fb55a
DJ
8267 /* If we're using the PLT additions to the psABI, each PLT
8268 entry is 16 bytes and the PLT0 entry is 32 bytes.
8269 Encourage better cache usage by aligning. We do this
8270 lazily to avoid pessimizing traditional objects. */
8271 if (!htab->is_vxworks
8272 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8273 return FALSE;
0a44bf69 8274
861fb55a
DJ
8275 /* Make sure that .got.plt is word-aligned. We do this lazily
8276 for the same reason as above. */
8277 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8278 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8279 return FALSE;
0a44bf69 8280
861fb55a 8281 htab->splt->size += htab->plt_header_size;
0a44bf69 8282
861fb55a
DJ
8283 /* On non-VxWorks targets, the first two entries in .got.plt
8284 are reserved. */
8285 if (!htab->is_vxworks)
8286 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
0a44bf69 8287
861fb55a
DJ
8288 /* On VxWorks, also allocate room for the header's
8289 .rela.plt.unloaded entries. */
8290 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8291 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8292 }
8293
8294 /* Assign the next .plt entry to this symbol. */
8295 h->plt.offset = htab->splt->size;
8296 htab->splt->size += htab->plt_entry_size;
8297
8298 /* If the output file has no definition of the symbol, set the
861fb55a 8299 symbol's value to the address of the stub. */
131eb6b7 8300 if (!info->shared && !h->def_regular)
0a44bf69
RS
8301 {
8302 h->root.u.def.section = htab->splt;
8303 h->root.u.def.value = h->plt.offset;
861fb55a
DJ
8304 /* For VxWorks, point at the PLT load stub rather than the
8305 lazy resolution stub; this stub will become the canonical
8306 function address. */
8307 if (htab->is_vxworks)
8308 h->root.u.def.value += 8;
0a44bf69
RS
8309 }
8310
861fb55a
DJ
8311 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8312 relocation. */
8313 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8314 htab->srelplt->size += (htab->is_vxworks
8315 ? MIPS_ELF_RELA_SIZE (dynobj)
8316 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8317
8318 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8319 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8320 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8321
861fb55a
DJ
8322 /* All relocations against this symbol that could have been made
8323 dynamic will now refer to the PLT entry instead. */
8324 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8325
0a44bf69
RS
8326 return TRUE;
8327 }
8328
8329 /* If this is a weak symbol, and there is a real definition, the
8330 processor independent code will have arranged for us to see the
8331 real definition first, and we can just use the same value. */
8332 if (h->u.weakdef != NULL)
8333 {
8334 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8335 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8336 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8337 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8338 return TRUE;
8339 }
8340
861fb55a
DJ
8341 /* Otherwise, there is nothing further to do for symbols defined
8342 in regular objects. */
8343 if (h->def_regular)
0a44bf69
RS
8344 return TRUE;
8345
861fb55a
DJ
8346 /* There's also nothing more to do if we'll convert all relocations
8347 against this symbol into dynamic relocations. */
8348 if (!hmips->has_static_relocs)
8349 return TRUE;
8350
8351 /* We're now relying on copy relocations. Complain if we have
8352 some that we can't convert. */
8353 if (!htab->use_plts_and_copy_relocs || info->shared)
8354 {
8355 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8356 "dynamic symbol %s"),
8357 h->root.root.string);
8358 bfd_set_error (bfd_error_bad_value);
8359 return FALSE;
8360 }
8361
0a44bf69
RS
8362 /* We must allocate the symbol in our .dynbss section, which will
8363 become part of the .bss section of the executable. There will be
8364 an entry for this symbol in the .dynsym section. The dynamic
8365 object will contain position independent code, so all references
8366 from the dynamic object to this symbol will go through the global
8367 offset table. The dynamic linker will use the .dynsym entry to
8368 determine the address it must put in the global offset table, so
8369 both the dynamic object and the regular object will refer to the
8370 same memory location for the variable. */
8371
8372 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8373 {
861fb55a
DJ
8374 if (htab->is_vxworks)
8375 htab->srelbss->size += sizeof (Elf32_External_Rela);
8376 else
8377 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
8378 h->needs_copy = 1;
8379 }
8380
861fb55a
DJ
8381 /* All relocations against this symbol that could have been made
8382 dynamic will now refer to the local copy instead. */
8383 hmips->possibly_dynamic_relocs = 0;
8384
027297b7 8385 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 8386}
b49e97c9
TS
8387\f
8388/* This function is called after all the input files have been read,
8389 and the input sections have been assigned to output sections. We
8390 check for any mips16 stub sections that we can discard. */
8391
b34976b6 8392bfd_boolean
9719ad41
RS
8393_bfd_mips_elf_always_size_sections (bfd *output_bfd,
8394 struct bfd_link_info *info)
b49e97c9
TS
8395{
8396 asection *ri;
0a44bf69 8397 struct mips_elf_link_hash_table *htab;
861fb55a 8398 struct mips_htab_traverse_info hti;
0a44bf69
RS
8399
8400 htab = mips_elf_hash_table (info);
4dfe6ac6 8401 BFD_ASSERT (htab != NULL);
f4416af6 8402
b49e97c9
TS
8403 /* The .reginfo section has a fixed size. */
8404 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8405 if (ri != NULL)
9719ad41 8406 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 8407
861fb55a
DJ
8408 hti.info = info;
8409 hti.output_bfd = output_bfd;
8410 hti.error = FALSE;
8411 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8412 mips_elf_check_symbols, &hti);
8413 if (hti.error)
8414 return FALSE;
f4416af6 8415
33bb52fb
RS
8416 return TRUE;
8417}
8418
8419/* If the link uses a GOT, lay it out and work out its size. */
8420
8421static bfd_boolean
8422mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8423{
8424 bfd *dynobj;
8425 asection *s;
8426 struct mips_got_info *g;
33bb52fb
RS
8427 bfd_size_type loadable_size = 0;
8428 bfd_size_type page_gotno;
8429 bfd *sub;
8430 struct mips_elf_count_tls_arg count_tls_arg;
8431 struct mips_elf_link_hash_table *htab;
8432
8433 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8434 BFD_ASSERT (htab != NULL);
8435
a8028dd0 8436 s = htab->sgot;
f4416af6 8437 if (s == NULL)
b34976b6 8438 return TRUE;
b49e97c9 8439
33bb52fb 8440 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
8441 g = htab->got_info;
8442
861fb55a
DJ
8443 /* Allocate room for the reserved entries. VxWorks always reserves
8444 3 entries; other objects only reserve 2 entries. */
8445 BFD_ASSERT (g->assigned_gotno == 0);
8446 if (htab->is_vxworks)
8447 htab->reserved_gotno = 3;
8448 else
8449 htab->reserved_gotno = 2;
8450 g->local_gotno += htab->reserved_gotno;
8451 g->assigned_gotno = htab->reserved_gotno;
8452
33bb52fb
RS
8453 /* Replace entries for indirect and warning symbols with entries for
8454 the target symbol. */
8455 if (!mips_elf_resolve_final_got_entries (g))
8456 return FALSE;
f4416af6 8457
d4596a51
RS
8458 /* Count the number of GOT symbols. */
8459 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, g);
f4416af6 8460
33bb52fb
RS
8461 /* Calculate the total loadable size of the output. That
8462 will give us the maximum number of GOT_PAGE entries
8463 required. */
8464 for (sub = info->input_bfds; sub; sub = sub->link_next)
8465 {
8466 asection *subsection;
5108fc1b 8467
33bb52fb
RS
8468 for (subsection = sub->sections;
8469 subsection;
8470 subsection = subsection->next)
8471 {
8472 if ((subsection->flags & SEC_ALLOC) == 0)
8473 continue;
8474 loadable_size += ((subsection->size + 0xf)
8475 &~ (bfd_size_type) 0xf);
8476 }
8477 }
f4416af6 8478
0a44bf69 8479 if (htab->is_vxworks)
738e5348 8480 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
8481 relocations against local symbols evaluate to "G", and the EABI does
8482 not include R_MIPS_GOT_PAGE. */
c224138d 8483 page_gotno = 0;
0a44bf69
RS
8484 else
8485 /* Assume there are two loadable segments consisting of contiguous
8486 sections. Is 5 enough? */
c224138d
RS
8487 page_gotno = (loadable_size >> 16) + 5;
8488
8489 /* Choose the smaller of the two estimates; both are intended to be
8490 conservative. */
8491 if (page_gotno > g->page_gotno)
8492 page_gotno = g->page_gotno;
f4416af6 8493
c224138d 8494 g->local_gotno += page_gotno;
eea6121a 8495 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
d4596a51 8496 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6 8497
0f20cc35
DJ
8498 /* We need to calculate tls_gotno for global symbols at this point
8499 instead of building it up earlier, to avoid doublecounting
8500 entries for one global symbol from multiple input files. */
8501 count_tls_arg.info = info;
8502 count_tls_arg.needed = 0;
8503 elf_link_hash_traverse (elf_hash_table (info),
8504 mips_elf_count_global_tls_entries,
8505 &count_tls_arg);
8506 g->tls_gotno += count_tls_arg.needed;
8507 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8508
0a44bf69
RS
8509 /* VxWorks does not support multiple GOTs. It initializes $gp to
8510 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8511 dynamic loader. */
33bb52fb
RS
8512 if (htab->is_vxworks)
8513 {
8514 /* VxWorks executables do not need a GOT. */
8515 if (info->shared)
8516 {
8517 /* Each VxWorks GOT entry needs an explicit relocation. */
8518 unsigned int count;
8519
861fb55a 8520 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
33bb52fb
RS
8521 if (count)
8522 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8523 }
8524 }
8525 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 8526 {
a8028dd0 8527 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
8528 return FALSE;
8529 }
8530 else
8531 {
33bb52fb
RS
8532 struct mips_elf_count_tls_arg arg;
8533
8534 /* Set up TLS entries. */
0f20cc35
DJ
8535 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8536 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
33bb52fb
RS
8537
8538 /* Allocate room for the TLS relocations. */
8539 arg.info = info;
8540 arg.needed = 0;
8541 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8542 elf_link_hash_traverse (elf_hash_table (info),
8543 mips_elf_count_global_tls_relocs,
8544 &arg);
8545 if (arg.needed)
8546 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
0f20cc35 8547 }
b49e97c9 8548
b34976b6 8549 return TRUE;
b49e97c9
TS
8550}
8551
33bb52fb
RS
8552/* Estimate the size of the .MIPS.stubs section. */
8553
8554static void
8555mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8556{
8557 struct mips_elf_link_hash_table *htab;
8558 bfd_size_type dynsymcount;
8559
8560 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8561 BFD_ASSERT (htab != NULL);
8562
33bb52fb
RS
8563 if (htab->lazy_stub_count == 0)
8564 return;
8565
8566 /* IRIX rld assumes that a function stub isn't at the end of the .text
8567 section, so add a dummy entry to the end. */
8568 htab->lazy_stub_count++;
8569
8570 /* Get a worst-case estimate of the number of dynamic symbols needed.
8571 At this point, dynsymcount does not account for section symbols
8572 and count_section_dynsyms may overestimate the number that will
8573 be needed. */
8574 dynsymcount = (elf_hash_table (info)->dynsymcount
8575 + count_section_dynsyms (output_bfd, info));
8576
8577 /* Determine the size of one stub entry. */
8578 htab->function_stub_size = (dynsymcount > 0x10000
8579 ? MIPS_FUNCTION_STUB_BIG_SIZE
8580 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8581
8582 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8583}
8584
8585/* A mips_elf_link_hash_traverse callback for which DATA points to the
8586 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8587 allocate an entry in the stubs section. */
8588
8589static bfd_boolean
8590mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8591{
8592 struct mips_elf_link_hash_table *htab;
8593
8594 htab = (struct mips_elf_link_hash_table *) data;
8595 if (h->needs_lazy_stub)
8596 {
8597 h->root.root.u.def.section = htab->sstubs;
8598 h->root.root.u.def.value = htab->sstubs->size;
8599 h->root.plt.offset = htab->sstubs->size;
8600 htab->sstubs->size += htab->function_stub_size;
8601 }
8602 return TRUE;
8603}
8604
8605/* Allocate offsets in the stubs section to each symbol that needs one.
8606 Set the final size of the .MIPS.stub section. */
8607
8608static void
8609mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8610{
8611 struct mips_elf_link_hash_table *htab;
8612
8613 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8614 BFD_ASSERT (htab != NULL);
8615
33bb52fb
RS
8616 if (htab->lazy_stub_count == 0)
8617 return;
8618
8619 htab->sstubs->size = 0;
4dfe6ac6 8620 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
33bb52fb
RS
8621 htab->sstubs->size += htab->function_stub_size;
8622 BFD_ASSERT (htab->sstubs->size
8623 == htab->lazy_stub_count * htab->function_stub_size);
8624}
8625
b49e97c9
TS
8626/* Set the sizes of the dynamic sections. */
8627
b34976b6 8628bfd_boolean
9719ad41
RS
8629_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8630 struct bfd_link_info *info)
b49e97c9
TS
8631{
8632 bfd *dynobj;
861fb55a 8633 asection *s, *sreldyn;
b34976b6 8634 bfd_boolean reltext;
0a44bf69 8635 struct mips_elf_link_hash_table *htab;
b49e97c9 8636
0a44bf69 8637 htab = mips_elf_hash_table (info);
4dfe6ac6 8638 BFD_ASSERT (htab != NULL);
b49e97c9
TS
8639 dynobj = elf_hash_table (info)->dynobj;
8640 BFD_ASSERT (dynobj != NULL);
8641
8642 if (elf_hash_table (info)->dynamic_sections_created)
8643 {
8644 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 8645 if (info->executable)
b49e97c9
TS
8646 {
8647 s = bfd_get_section_by_name (dynobj, ".interp");
8648 BFD_ASSERT (s != NULL);
eea6121a 8649 s->size
b49e97c9
TS
8650 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8651 s->contents
8652 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8653 }
861fb55a
DJ
8654
8655 /* Create a symbol for the PLT, if we know that we are using it. */
8656 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8657 {
8658 struct elf_link_hash_entry *h;
8659
8660 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8661
8662 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8663 "_PROCEDURE_LINKAGE_TABLE_");
8664 htab->root.hplt = h;
8665 if (h == NULL)
8666 return FALSE;
8667 h->type = STT_FUNC;
8668 }
8669 }
4e41d0d7 8670
9a59ad6b
DJ
8671 /* Allocate space for global sym dynamic relocs. */
8672 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8673
33bb52fb
RS
8674 mips_elf_estimate_stub_size (output_bfd, info);
8675
8676 if (!mips_elf_lay_out_got (output_bfd, info))
8677 return FALSE;
8678
8679 mips_elf_lay_out_lazy_stubs (info);
8680
b49e97c9
TS
8681 /* The check_relocs and adjust_dynamic_symbol entry points have
8682 determined the sizes of the various dynamic sections. Allocate
8683 memory for them. */
b34976b6 8684 reltext = FALSE;
b49e97c9
TS
8685 for (s = dynobj->sections; s != NULL; s = s->next)
8686 {
8687 const char *name;
b49e97c9
TS
8688
8689 /* It's OK to base decisions on the section name, because none
8690 of the dynobj section names depend upon the input files. */
8691 name = bfd_get_section_name (dynobj, s);
8692
8693 if ((s->flags & SEC_LINKER_CREATED) == 0)
8694 continue;
8695
0112cd26 8696 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 8697 {
c456f082 8698 if (s->size != 0)
b49e97c9
TS
8699 {
8700 const char *outname;
8701 asection *target;
8702
8703 /* If this relocation section applies to a read only
8704 section, then we probably need a DT_TEXTREL entry.
0a44bf69 8705 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
8706 assert a DT_TEXTREL entry rather than testing whether
8707 there exists a relocation to a read only section or
8708 not. */
8709 outname = bfd_get_section_name (output_bfd,
8710 s->output_section);
8711 target = bfd_get_section_by_name (output_bfd, outname + 4);
8712 if ((target != NULL
8713 && (target->flags & SEC_READONLY) != 0
8714 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 8715 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 8716 reltext = TRUE;
b49e97c9
TS
8717
8718 /* We use the reloc_count field as a counter if we need
8719 to copy relocs into the output file. */
0a44bf69 8720 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 8721 s->reloc_count = 0;
f4416af6
AO
8722
8723 /* If combreloc is enabled, elf_link_sort_relocs() will
8724 sort relocations, but in a different way than we do,
8725 and before we're done creating relocations. Also, it
8726 will move them around between input sections'
8727 relocation's contents, so our sorting would be
8728 broken, so don't let it run. */
8729 info->combreloc = 0;
b49e97c9
TS
8730 }
8731 }
b49e97c9
TS
8732 else if (! info->shared
8733 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 8734 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 8735 {
5108fc1b 8736 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 8737 rtld to contain a pointer to the _r_debug structure. */
eea6121a 8738 s->size += 4;
b49e97c9
TS
8739 }
8740 else if (SGI_COMPAT (output_bfd)
0112cd26 8741 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 8742 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
8743 else if (s == htab->splt)
8744 {
8745 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
8746 room for an extra nop to fill the delay slot. This is
8747 for CPUs without load interlocking. */
8748 if (! LOAD_INTERLOCKS_P (output_bfd)
8749 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
8750 s->size += 4;
8751 }
0112cd26 8752 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 8753 && s != htab->sgot
0a44bf69 8754 && s != htab->sgotplt
861fb55a
DJ
8755 && s != htab->sstubs
8756 && s != htab->sdynbss)
b49e97c9
TS
8757 {
8758 /* It's not one of our sections, so don't allocate space. */
8759 continue;
8760 }
8761
c456f082 8762 if (s->size == 0)
b49e97c9 8763 {
8423293d 8764 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
8765 continue;
8766 }
8767
c456f082
AM
8768 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8769 continue;
8770
b49e97c9 8771 /* Allocate memory for the section contents. */
eea6121a 8772 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 8773 if (s->contents == NULL)
b49e97c9
TS
8774 {
8775 bfd_set_error (bfd_error_no_memory);
b34976b6 8776 return FALSE;
b49e97c9
TS
8777 }
8778 }
8779
8780 if (elf_hash_table (info)->dynamic_sections_created)
8781 {
8782 /* Add some entries to the .dynamic section. We fill in the
8783 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8784 must add the entries now so that we get the correct size for
5750dcec 8785 the .dynamic section. */
af5978fb
RS
8786
8787 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec
DJ
8788 DT_MIPS_RLD_MAP entry. This must come first because glibc
8789 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8790 looks at the first one it sees. */
af5978fb
RS
8791 if (!info->shared
8792 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8793 return FALSE;
b49e97c9 8794
5750dcec
DJ
8795 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8796 used by the debugger. */
8797 if (info->executable
8798 && !SGI_COMPAT (output_bfd)
8799 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8800 return FALSE;
8801
0a44bf69 8802 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
8803 info->flags |= DF_TEXTREL;
8804
8805 if ((info->flags & DF_TEXTREL) != 0)
8806 {
8807 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 8808 return FALSE;
943284cc
DJ
8809
8810 /* Clear the DF_TEXTREL flag. It will be set again if we
8811 write out an actual text relocation; we may not, because
8812 at this point we do not know whether e.g. any .eh_frame
8813 absolute relocations have been converted to PC-relative. */
8814 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
8815 }
8816
8817 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 8818 return FALSE;
b49e97c9 8819
861fb55a 8820 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 8821 if (htab->is_vxworks)
b49e97c9 8822 {
0a44bf69
RS
8823 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8824 use any of the DT_MIPS_* tags. */
861fb55a 8825 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8826 {
8827 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8828 return FALSE;
b49e97c9 8829
0a44bf69
RS
8830 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8831 return FALSE;
b49e97c9 8832
0a44bf69
RS
8833 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8834 return FALSE;
8835 }
b49e97c9 8836 }
0a44bf69
RS
8837 else
8838 {
861fb55a 8839 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8840 {
8841 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8842 return FALSE;
b49e97c9 8843
0a44bf69
RS
8844 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8845 return FALSE;
b49e97c9 8846
0a44bf69
RS
8847 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8848 return FALSE;
8849 }
b49e97c9 8850
0a44bf69
RS
8851 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8852 return FALSE;
b49e97c9 8853
0a44bf69
RS
8854 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8855 return FALSE;
b49e97c9 8856
0a44bf69
RS
8857 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8858 return FALSE;
b49e97c9 8859
0a44bf69
RS
8860 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8861 return FALSE;
b49e97c9 8862
0a44bf69
RS
8863 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8864 return FALSE;
b49e97c9 8865
0a44bf69
RS
8866 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8867 return FALSE;
b49e97c9 8868
0a44bf69
RS
8869 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8870 return FALSE;
8871
8872 if (IRIX_COMPAT (dynobj) == ict_irix5
8873 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8874 return FALSE;
8875
8876 if (IRIX_COMPAT (dynobj) == ict_irix6
8877 && (bfd_get_section_by_name
8878 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8879 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8880 return FALSE;
8881 }
861fb55a
DJ
8882 if (htab->splt->size > 0)
8883 {
8884 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8885 return FALSE;
8886
8887 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8888 return FALSE;
8889
8890 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8891 return FALSE;
8892
8893 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8894 return FALSE;
8895 }
7a2b07ff
NS
8896 if (htab->is_vxworks
8897 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8898 return FALSE;
b49e97c9
TS
8899 }
8900
b34976b6 8901 return TRUE;
b49e97c9
TS
8902}
8903\f
81d43bff
RS
8904/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8905 Adjust its R_ADDEND field so that it is correct for the output file.
8906 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8907 and sections respectively; both use symbol indexes. */
8908
8909static void
8910mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8911 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8912 asection **local_sections, Elf_Internal_Rela *rel)
8913{
8914 unsigned int r_type, r_symndx;
8915 Elf_Internal_Sym *sym;
8916 asection *sec;
8917
8918 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8919 {
8920 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8921 if (r_type == R_MIPS16_GPREL
8922 || r_type == R_MIPS_GPREL16
8923 || r_type == R_MIPS_GPREL32
8924 || r_type == R_MIPS_LITERAL)
8925 {
8926 rel->r_addend += _bfd_get_gp_value (input_bfd);
8927 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8928 }
8929
8930 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8931 sym = local_syms + r_symndx;
8932
8933 /* Adjust REL's addend to account for section merging. */
8934 if (!info->relocatable)
8935 {
8936 sec = local_sections[r_symndx];
8937 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8938 }
8939
8940 /* This would normally be done by the rela_normal code in elflink.c. */
8941 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8942 rel->r_addend += local_sections[r_symndx]->output_offset;
8943 }
8944}
8945
b49e97c9
TS
8946/* Relocate a MIPS ELF section. */
8947
b34976b6 8948bfd_boolean
9719ad41
RS
8949_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8950 bfd *input_bfd, asection *input_section,
8951 bfd_byte *contents, Elf_Internal_Rela *relocs,
8952 Elf_Internal_Sym *local_syms,
8953 asection **local_sections)
b49e97c9
TS
8954{
8955 Elf_Internal_Rela *rel;
8956 const Elf_Internal_Rela *relend;
8957 bfd_vma addend = 0;
b34976b6 8958 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 8959 const struct elf_backend_data *bed;
b49e97c9
TS
8960
8961 bed = get_elf_backend_data (output_bfd);
8962 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8963 for (rel = relocs; rel < relend; ++rel)
8964 {
8965 const char *name;
c9adbffe 8966 bfd_vma value = 0;
b49e97c9 8967 reloc_howto_type *howto;
38a7df63 8968 bfd_boolean cross_mode_jump_p;
b34976b6 8969 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 8970 REL relocation. */
b34976b6 8971 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 8972 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 8973 const char *msg;
ab96bf03
AM
8974 unsigned long r_symndx;
8975 asection *sec;
749b8d9d
L
8976 Elf_Internal_Shdr *symtab_hdr;
8977 struct elf_link_hash_entry *h;
b49e97c9
TS
8978
8979 /* Find the relocation howto for this relocation. */
ab96bf03
AM
8980 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8981 NEWABI_P (input_bfd)
8982 && (MIPS_RELOC_RELA_P
8983 (input_bfd, input_section,
8984 rel - relocs)));
8985
8986 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 8987 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
ab96bf03 8988 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
749b8d9d
L
8989 {
8990 sec = local_sections[r_symndx];
8991 h = NULL;
8992 }
ab96bf03
AM
8993 else
8994 {
ab96bf03 8995 unsigned long extsymoff;
ab96bf03 8996
ab96bf03
AM
8997 extsymoff = 0;
8998 if (!elf_bad_symtab (input_bfd))
8999 extsymoff = symtab_hdr->sh_info;
9000 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9001 while (h->root.type == bfd_link_hash_indirect
9002 || h->root.type == bfd_link_hash_warning)
9003 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9004
9005 sec = NULL;
9006 if (h->root.type == bfd_link_hash_defined
9007 || h->root.type == bfd_link_hash_defweak)
9008 sec = h->root.u.def.section;
9009 }
9010
9011 if (sec != NULL && elf_discarded_section (sec))
9012 {
9013 /* For relocs against symbols from removed linkonce sections,
9014 or sections discarded by a linker script, we just want the
9015 section contents zeroed. Avoid any special processing. */
9016 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
9017 rel->r_info = 0;
9018 rel->r_addend = 0;
9019 continue;
9020 }
9021
4a14403c 9022 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
9023 {
9024 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9025 64-bit code, but make sure all their addresses are in the
9026 lowermost or uppermost 32-bit section of the 64-bit address
9027 space. Thus, when they use an R_MIPS_64 they mean what is
9028 usually meant by R_MIPS_32, with the exception that the
9029 stored value is sign-extended to 64 bits. */
b34976b6 9030 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
9031
9032 /* On big-endian systems, we need to lie about the position
9033 of the reloc. */
9034 if (bfd_big_endian (input_bfd))
9035 rel->r_offset += 4;
9036 }
b49e97c9
TS
9037
9038 if (!use_saved_addend_p)
9039 {
b49e97c9
TS
9040 /* If these relocations were originally of the REL variety,
9041 we must pull the addend out of the field that will be
9042 relocated. Otherwise, we simply use the contents of the
c224138d
RS
9043 RELA relocation. */
9044 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9045 relocs, rel))
b49e97c9 9046 {
b34976b6 9047 rela_relocation_p = FALSE;
c224138d
RS
9048 addend = mips_elf_read_rel_addend (input_bfd, rel,
9049 howto, contents);
738e5348
RS
9050 if (hi16_reloc_p (r_type)
9051 || (got16_reloc_p (r_type)
b49e97c9 9052 && mips_elf_local_relocation_p (input_bfd, rel,
b34976b6 9053 local_sections, FALSE)))
b49e97c9 9054 {
c224138d
RS
9055 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9056 contents, &addend))
749b8d9d 9057 {
749b8d9d
L
9058 if (h)
9059 name = h->root.root.string;
9060 else
9061 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9062 local_syms + r_symndx,
9063 sec);
9064 (*_bfd_error_handler)
9065 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9066 input_bfd, input_section, name, howto->name,
9067 rel->r_offset);
749b8d9d 9068 }
b49e97c9 9069 }
30ac9238
RS
9070 else
9071 addend <<= howto->rightshift;
b49e97c9
TS
9072 }
9073 else
9074 addend = rel->r_addend;
81d43bff
RS
9075 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9076 local_syms, local_sections, rel);
b49e97c9
TS
9077 }
9078
1049f94e 9079 if (info->relocatable)
b49e97c9 9080 {
4a14403c 9081 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9082 && bfd_big_endian (input_bfd))
9083 rel->r_offset -= 4;
9084
81d43bff 9085 if (!rela_relocation_p && rel->r_addend)
5a659663 9086 {
81d43bff 9087 addend += rel->r_addend;
738e5348 9088 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9089 addend = mips_elf_high (addend);
9090 else if (r_type == R_MIPS_HIGHER)
9091 addend = mips_elf_higher (addend);
9092 else if (r_type == R_MIPS_HIGHEST)
9093 addend = mips_elf_highest (addend);
30ac9238
RS
9094 else
9095 addend >>= howto->rightshift;
b49e97c9 9096
30ac9238
RS
9097 /* We use the source mask, rather than the destination
9098 mask because the place to which we are writing will be
9099 source of the addend in the final link. */
b49e97c9
TS
9100 addend &= howto->src_mask;
9101
5a659663 9102 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9103 /* See the comment above about using R_MIPS_64 in the 32-bit
9104 ABI. Here, we need to update the addend. It would be
9105 possible to get away with just using the R_MIPS_32 reloc
9106 but for endianness. */
9107 {
9108 bfd_vma sign_bits;
9109 bfd_vma low_bits;
9110 bfd_vma high_bits;
9111
9112 if (addend & ((bfd_vma) 1 << 31))
9113#ifdef BFD64
9114 sign_bits = ((bfd_vma) 1 << 32) - 1;
9115#else
9116 sign_bits = -1;
9117#endif
9118 else
9119 sign_bits = 0;
9120
9121 /* If we don't know that we have a 64-bit type,
9122 do two separate stores. */
9123 if (bfd_big_endian (input_bfd))
9124 {
9125 /* Store the sign-bits (which are most significant)
9126 first. */
9127 low_bits = sign_bits;
9128 high_bits = addend;
9129 }
9130 else
9131 {
9132 low_bits = addend;
9133 high_bits = sign_bits;
9134 }
9135 bfd_put_32 (input_bfd, low_bits,
9136 contents + rel->r_offset);
9137 bfd_put_32 (input_bfd, high_bits,
9138 contents + rel->r_offset + 4);
9139 continue;
9140 }
9141
9142 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9143 input_bfd, input_section,
b34976b6
AM
9144 contents, FALSE))
9145 return FALSE;
b49e97c9
TS
9146 }
9147
9148 /* Go on to the next relocation. */
9149 continue;
9150 }
9151
9152 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9153 relocations for the same offset. In that case we are
9154 supposed to treat the output of each relocation as the addend
9155 for the next. */
9156 if (rel + 1 < relend
9157 && rel->r_offset == rel[1].r_offset
9158 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9159 use_saved_addend_p = TRUE;
b49e97c9 9160 else
b34976b6 9161 use_saved_addend_p = FALSE;
b49e97c9
TS
9162
9163 /* Figure out what value we are supposed to relocate. */
9164 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9165 input_section, info, rel,
9166 addend, howto, local_syms,
9167 local_sections, &value,
38a7df63 9168 &name, &cross_mode_jump_p,
bce03d3d 9169 use_saved_addend_p))
b49e97c9
TS
9170 {
9171 case bfd_reloc_continue:
9172 /* There's nothing to do. */
9173 continue;
9174
9175 case bfd_reloc_undefined:
9176 /* mips_elf_calculate_relocation already called the
9177 undefined_symbol callback. There's no real point in
9178 trying to perform the relocation at this point, so we
9179 just skip ahead to the next relocation. */
9180 continue;
9181
9182 case bfd_reloc_notsupported:
9183 msg = _("internal error: unsupported relocation error");
9184 info->callbacks->warning
9185 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9186 return FALSE;
b49e97c9
TS
9187
9188 case bfd_reloc_overflow:
9189 if (use_saved_addend_p)
9190 /* Ignore overflow until we reach the last relocation for
9191 a given location. */
9192 ;
9193 else
9194 {
0e53d9da
AN
9195 struct mips_elf_link_hash_table *htab;
9196
9197 htab = mips_elf_hash_table (info);
4dfe6ac6 9198 BFD_ASSERT (htab != NULL);
b49e97c9 9199 BFD_ASSERT (name != NULL);
0e53d9da
AN
9200 if (!htab->small_data_overflow_reported
9201 && (howto->type == R_MIPS_GPREL16
9202 || howto->type == R_MIPS_LITERAL))
9203 {
91d6fa6a
NC
9204 msg = _("small-data section exceeds 64KB;"
9205 " lower small-data size limit (see option -G)");
0e53d9da
AN
9206
9207 htab->small_data_overflow_reported = TRUE;
9208 (*info->callbacks->einfo) ("%P: %s\n", msg);
9209 }
b49e97c9 9210 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 9211 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 9212 input_bfd, input_section, rel->r_offset)))
b34976b6 9213 return FALSE;
b49e97c9
TS
9214 }
9215 break;
9216
9217 case bfd_reloc_ok:
9218 break;
9219
9220 default:
9221 abort ();
9222 break;
9223 }
9224
9225 /* If we've got another relocation for the address, keep going
9226 until we reach the last one. */
9227 if (use_saved_addend_p)
9228 {
9229 addend = value;
9230 continue;
9231 }
9232
4a14403c 9233 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9234 /* See the comment above about using R_MIPS_64 in the 32-bit
9235 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9236 that calculated the right value. Now, however, we
9237 sign-extend the 32-bit result to 64-bits, and store it as a
9238 64-bit value. We are especially generous here in that we
9239 go to extreme lengths to support this usage on systems with
9240 only a 32-bit VMA. */
9241 {
9242 bfd_vma sign_bits;
9243 bfd_vma low_bits;
9244 bfd_vma high_bits;
9245
9246 if (value & ((bfd_vma) 1 << 31))
9247#ifdef BFD64
9248 sign_bits = ((bfd_vma) 1 << 32) - 1;
9249#else
9250 sign_bits = -1;
9251#endif
9252 else
9253 sign_bits = 0;
9254
9255 /* If we don't know that we have a 64-bit type,
9256 do two separate stores. */
9257 if (bfd_big_endian (input_bfd))
9258 {
9259 /* Undo what we did above. */
9260 rel->r_offset -= 4;
9261 /* Store the sign-bits (which are most significant)
9262 first. */
9263 low_bits = sign_bits;
9264 high_bits = value;
9265 }
9266 else
9267 {
9268 low_bits = value;
9269 high_bits = sign_bits;
9270 }
9271 bfd_put_32 (input_bfd, low_bits,
9272 contents + rel->r_offset);
9273 bfd_put_32 (input_bfd, high_bits,
9274 contents + rel->r_offset + 4);
9275 continue;
9276 }
9277
9278 /* Actually perform the relocation. */
9279 if (! mips_elf_perform_relocation (info, howto, rel, value,
9280 input_bfd, input_section,
38a7df63 9281 contents, cross_mode_jump_p))
b34976b6 9282 return FALSE;
b49e97c9
TS
9283 }
9284
b34976b6 9285 return TRUE;
b49e97c9
TS
9286}
9287\f
861fb55a
DJ
9288/* A function that iterates over each entry in la25_stubs and fills
9289 in the code for each one. DATA points to a mips_htab_traverse_info. */
9290
9291static int
9292mips_elf_create_la25_stub (void **slot, void *data)
9293{
9294 struct mips_htab_traverse_info *hti;
9295 struct mips_elf_link_hash_table *htab;
9296 struct mips_elf_la25_stub *stub;
9297 asection *s;
9298 bfd_byte *loc;
9299 bfd_vma offset, target, target_high, target_low;
9300
9301 stub = (struct mips_elf_la25_stub *) *slot;
9302 hti = (struct mips_htab_traverse_info *) data;
9303 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 9304 BFD_ASSERT (htab != NULL);
861fb55a
DJ
9305
9306 /* Create the section contents, if we haven't already. */
9307 s = stub->stub_section;
9308 loc = s->contents;
9309 if (loc == NULL)
9310 {
9311 loc = bfd_malloc (s->size);
9312 if (loc == NULL)
9313 {
9314 hti->error = TRUE;
9315 return FALSE;
9316 }
9317 s->contents = loc;
9318 }
9319
9320 /* Work out where in the section this stub should go. */
9321 offset = stub->offset;
9322
9323 /* Work out the target address. */
9324 target = (stub->h->root.root.u.def.section->output_section->vma
9325 + stub->h->root.root.u.def.section->output_offset
9326 + stub->h->root.root.u.def.value);
9327 target_high = ((target + 0x8000) >> 16) & 0xffff;
9328 target_low = (target & 0xffff);
9329
9330 if (stub->stub_section != htab->strampoline)
9331 {
9332 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9333 of the section and write the two instructions at the end. */
9334 memset (loc, 0, offset);
9335 loc += offset;
9336 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9337 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9338 }
9339 else
9340 {
9341 /* This is trampoline. */
9342 loc += offset;
9343 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9344 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9345 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9346 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9347 }
9348 return TRUE;
9349}
9350
b49e97c9
TS
9351/* If NAME is one of the special IRIX6 symbols defined by the linker,
9352 adjust it appropriately now. */
9353
9354static void
9719ad41
RS
9355mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9356 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
9357{
9358 /* The linker script takes care of providing names and values for
9359 these, but we must place them into the right sections. */
9360 static const char* const text_section_symbols[] = {
9361 "_ftext",
9362 "_etext",
9363 "__dso_displacement",
9364 "__elf_header",
9365 "__program_header_table",
9366 NULL
9367 };
9368
9369 static const char* const data_section_symbols[] = {
9370 "_fdata",
9371 "_edata",
9372 "_end",
9373 "_fbss",
9374 NULL
9375 };
9376
9377 const char* const *p;
9378 int i;
9379
9380 for (i = 0; i < 2; ++i)
9381 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9382 *p;
9383 ++p)
9384 if (strcmp (*p, name) == 0)
9385 {
9386 /* All of these symbols are given type STT_SECTION by the
9387 IRIX6 linker. */
9388 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 9389 sym->st_other = STO_PROTECTED;
b49e97c9
TS
9390
9391 /* The IRIX linker puts these symbols in special sections. */
9392 if (i == 0)
9393 sym->st_shndx = SHN_MIPS_TEXT;
9394 else
9395 sym->st_shndx = SHN_MIPS_DATA;
9396
9397 break;
9398 }
9399}
9400
9401/* Finish up dynamic symbol handling. We set the contents of various
9402 dynamic sections here. */
9403
b34976b6 9404bfd_boolean
9719ad41
RS
9405_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9406 struct bfd_link_info *info,
9407 struct elf_link_hash_entry *h,
9408 Elf_Internal_Sym *sym)
b49e97c9
TS
9409{
9410 bfd *dynobj;
b49e97c9 9411 asection *sgot;
f4416af6 9412 struct mips_got_info *g, *gg;
b49e97c9 9413 const char *name;
3d6746ca 9414 int idx;
5108fc1b 9415 struct mips_elf_link_hash_table *htab;
738e5348 9416 struct mips_elf_link_hash_entry *hmips;
b49e97c9 9417
5108fc1b 9418 htab = mips_elf_hash_table (info);
4dfe6ac6 9419 BFD_ASSERT (htab != NULL);
b49e97c9 9420 dynobj = elf_hash_table (info)->dynobj;
738e5348 9421 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9422
861fb55a
DJ
9423 BFD_ASSERT (!htab->is_vxworks);
9424
9425 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9426 {
9427 /* We've decided to create a PLT entry for this symbol. */
9428 bfd_byte *loc;
9429 bfd_vma header_address, plt_index, got_address;
9430 bfd_vma got_address_high, got_address_low, load;
9431 const bfd_vma *plt_entry;
9432
9433 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9434 BFD_ASSERT (h->dynindx != -1);
9435 BFD_ASSERT (htab->splt != NULL);
9436 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9437 BFD_ASSERT (!h->def_regular);
9438
9439 /* Calculate the address of the PLT header. */
9440 header_address = (htab->splt->output_section->vma
9441 + htab->splt->output_offset);
9442
9443 /* Calculate the index of the entry. */
9444 plt_index = ((h->plt.offset - htab->plt_header_size)
9445 / htab->plt_entry_size);
9446
9447 /* Calculate the address of the .got.plt entry. */
9448 got_address = (htab->sgotplt->output_section->vma
9449 + htab->sgotplt->output_offset
9450 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9451 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9452 got_address_low = got_address & 0xffff;
9453
9454 /* Initially point the .got.plt entry at the PLT header. */
9455 loc = (htab->sgotplt->contents
9456 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9457 if (ABI_64_P (output_bfd))
9458 bfd_put_64 (output_bfd, header_address, loc);
9459 else
9460 bfd_put_32 (output_bfd, header_address, loc);
9461
9462 /* Find out where the .plt entry should go. */
9463 loc = htab->splt->contents + h->plt.offset;
9464
9465 /* Pick the load opcode. */
9466 load = MIPS_ELF_LOAD_WORD (output_bfd);
9467
9468 /* Fill in the PLT entry itself. */
9469 plt_entry = mips_exec_plt_entry;
9470 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9471 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
6d30f5b2
NC
9472
9473 if (! LOAD_INTERLOCKS_P (output_bfd))
9474 {
9475 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9476 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9477 }
9478 else
9479 {
9480 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9481 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9482 }
861fb55a
DJ
9483
9484 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9485 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9486 plt_index, h->dynindx,
9487 R_MIPS_JUMP_SLOT, got_address);
9488
9489 /* We distinguish between PLT entries and lazy-binding stubs by
9490 giving the former an st_other value of STO_MIPS_PLT. Set the
9491 flag and leave the value if there are any relocations in the
9492 binary where pointer equality matters. */
9493 sym->st_shndx = SHN_UNDEF;
9494 if (h->pointer_equality_needed)
9495 sym->st_other = STO_MIPS_PLT;
9496 else
9497 sym->st_value = 0;
9498 }
9499 else if (h->plt.offset != MINUS_ONE)
b49e97c9 9500 {
861fb55a 9501 /* We've decided to create a lazy-binding stub. */
5108fc1b 9502 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
b49e97c9
TS
9503
9504 /* This symbol has a stub. Set it up. */
9505
9506 BFD_ASSERT (h->dynindx != -1);
9507
5108fc1b
RS
9508 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9509 || (h->dynindx <= 0xffff));
3d6746ca
DD
9510
9511 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
9512 sign extension at runtime in the stub, resulting in a negative
9513 index value. */
9514 if (h->dynindx & ~0x7fffffff)
b34976b6 9515 return FALSE;
b49e97c9
TS
9516
9517 /* Fill the stub. */
3d6746ca
DD
9518 idx = 0;
9519 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9520 idx += 4;
9521 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9522 idx += 4;
5108fc1b 9523 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
3d6746ca 9524 {
5108fc1b 9525 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
3d6746ca
DD
9526 stub + idx);
9527 idx += 4;
9528 }
9529 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9530 idx += 4;
b49e97c9 9531
3d6746ca
DD
9532 /* If a large stub is not required and sign extension is not a
9533 problem, then use legacy code in the stub. */
5108fc1b
RS
9534 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9535 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9536 else if (h->dynindx & ~0x7fff)
3d6746ca
DD
9537 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9538 else
5108fc1b
RS
9539 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9540 stub + idx);
9541
4e41d0d7
RS
9542 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9543 memcpy (htab->sstubs->contents + h->plt.offset,
9544 stub, htab->function_stub_size);
b49e97c9
TS
9545
9546 /* Mark the symbol as undefined. plt.offset != -1 occurs
9547 only for the referenced symbol. */
9548 sym->st_shndx = SHN_UNDEF;
9549
9550 /* The run-time linker uses the st_value field of the symbol
9551 to reset the global offset table entry for this external
9552 to its stub address when unlinking a shared object. */
4e41d0d7
RS
9553 sym->st_value = (htab->sstubs->output_section->vma
9554 + htab->sstubs->output_offset
c5ae1840 9555 + h->plt.offset);
b49e97c9
TS
9556 }
9557
738e5348
RS
9558 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9559 refer to the stub, since only the stub uses the standard calling
9560 conventions. */
9561 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9562 {
9563 BFD_ASSERT (hmips->need_fn_stub);
9564 sym->st_value = (hmips->fn_stub->output_section->vma
9565 + hmips->fn_stub->output_offset);
9566 sym->st_size = hmips->fn_stub->size;
9567 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9568 }
9569
b49e97c9 9570 BFD_ASSERT (h->dynindx != -1
f5385ebf 9571 || h->forced_local);
b49e97c9 9572
23cc69b6 9573 sgot = htab->sgot;
a8028dd0 9574 g = htab->got_info;
b49e97c9
TS
9575 BFD_ASSERT (g != NULL);
9576
9577 /* Run through the global symbol table, creating GOT entries for all
9578 the symbols that need them. */
9579 if (g->global_gotsym != NULL
9580 && h->dynindx >= g->global_gotsym->dynindx)
9581 {
9582 bfd_vma offset;
9583 bfd_vma value;
9584
6eaa6adc 9585 value = sym->st_value;
738e5348
RS
9586 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9587 R_MIPS_GOT16, info);
b49e97c9
TS
9588 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9589 }
9590
0f20cc35 9591 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
f4416af6
AO
9592 {
9593 struct mips_got_entry e, *p;
0626d451 9594 bfd_vma entry;
f4416af6 9595 bfd_vma offset;
f4416af6
AO
9596
9597 gg = g;
9598
9599 e.abfd = output_bfd;
9600 e.symndx = -1;
738e5348 9601 e.d.h = hmips;
0f20cc35 9602 e.tls_type = 0;
143d77c5 9603
f4416af6
AO
9604 for (g = g->next; g->next != gg; g = g->next)
9605 {
9606 if (g->got_entries
9607 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9608 &e)))
9609 {
9610 offset = p->gotidx;
0626d451
RS
9611 if (info->shared
9612 || (elf_hash_table (info)->dynamic_sections_created
9613 && p->d.h != NULL
f5385ebf
AM
9614 && p->d.h->root.def_dynamic
9615 && !p->d.h->root.def_regular))
0626d451
RS
9616 {
9617 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9618 the various compatibility problems, it's easier to mock
9619 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9620 mips_elf_create_dynamic_relocation to calculate the
9621 appropriate addend. */
9622 Elf_Internal_Rela rel[3];
9623
9624 memset (rel, 0, sizeof (rel));
9625 if (ABI_64_P (output_bfd))
9626 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9627 else
9628 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9629 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9630
9631 entry = 0;
9632 if (! (mips_elf_create_dynamic_relocation
9633 (output_bfd, info, rel,
9634 e.d.h, NULL, sym->st_value, &entry, sgot)))
9635 return FALSE;
9636 }
9637 else
9638 entry = sym->st_value;
9639 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
9640 }
9641 }
9642 }
9643
b49e97c9
TS
9644 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9645 name = h->root.root.string;
9646 if (strcmp (name, "_DYNAMIC") == 0
22edb2f1 9647 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
9648 sym->st_shndx = SHN_ABS;
9649 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9650 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9651 {
9652 sym->st_shndx = SHN_ABS;
9653 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9654 sym->st_value = 1;
9655 }
4a14403c 9656 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9657 {
9658 sym->st_shndx = SHN_ABS;
9659 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9660 sym->st_value = elf_gp (output_bfd);
9661 }
9662 else if (SGI_COMPAT (output_bfd))
9663 {
9664 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9665 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9666 {
9667 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9668 sym->st_other = STO_PROTECTED;
9669 sym->st_value = 0;
9670 sym->st_shndx = SHN_MIPS_DATA;
9671 }
9672 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9673 {
9674 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9675 sym->st_other = STO_PROTECTED;
9676 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9677 sym->st_shndx = SHN_ABS;
9678 }
9679 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9680 {
9681 if (h->type == STT_FUNC)
9682 sym->st_shndx = SHN_MIPS_TEXT;
9683 else if (h->type == STT_OBJECT)
9684 sym->st_shndx = SHN_MIPS_DATA;
9685 }
9686 }
9687
861fb55a
DJ
9688 /* Emit a copy reloc, if needed. */
9689 if (h->needs_copy)
9690 {
9691 asection *s;
9692 bfd_vma symval;
9693
9694 BFD_ASSERT (h->dynindx != -1);
9695 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9696
9697 s = mips_elf_rel_dyn_section (info, FALSE);
9698 symval = (h->root.u.def.section->output_section->vma
9699 + h->root.u.def.section->output_offset
9700 + h->root.u.def.value);
9701 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9702 h->dynindx, R_MIPS_COPY, symval);
9703 }
9704
b49e97c9
TS
9705 /* Handle the IRIX6-specific symbols. */
9706 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9707 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9708
9709 if (! info->shared)
9710 {
9711 if (! mips_elf_hash_table (info)->use_rld_obj_head
9712 && (strcmp (name, "__rld_map") == 0
9713 || strcmp (name, "__RLD_MAP") == 0))
9714 {
9715 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9716 BFD_ASSERT (s != NULL);
9717 sym->st_value = s->output_section->vma + s->output_offset;
9719ad41 9718 bfd_put_32 (output_bfd, 0, s->contents);
b49e97c9
TS
9719 if (mips_elf_hash_table (info)->rld_value == 0)
9720 mips_elf_hash_table (info)->rld_value = sym->st_value;
9721 }
9722 else if (mips_elf_hash_table (info)->use_rld_obj_head
9723 && strcmp (name, "__rld_obj_head") == 0)
9724 {
9725 /* IRIX6 does not use a .rld_map section. */
9726 if (IRIX_COMPAT (output_bfd) == ict_irix5
9727 || IRIX_COMPAT (output_bfd) == ict_none)
9728 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9729 != NULL);
9730 mips_elf_hash_table (info)->rld_value = sym->st_value;
9731 }
9732 }
9733
738e5348
RS
9734 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9735 treat MIPS16 symbols like any other. */
30c09090 9736 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
9737 {
9738 BFD_ASSERT (sym->st_value & 1);
9739 sym->st_other -= STO_MIPS16;
9740 }
b49e97c9 9741
b34976b6 9742 return TRUE;
b49e97c9
TS
9743}
9744
0a44bf69
RS
9745/* Likewise, for VxWorks. */
9746
9747bfd_boolean
9748_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9749 struct bfd_link_info *info,
9750 struct elf_link_hash_entry *h,
9751 Elf_Internal_Sym *sym)
9752{
9753 bfd *dynobj;
9754 asection *sgot;
9755 struct mips_got_info *g;
9756 struct mips_elf_link_hash_table *htab;
9757
9758 htab = mips_elf_hash_table (info);
4dfe6ac6 9759 BFD_ASSERT (htab != NULL);
0a44bf69
RS
9760 dynobj = elf_hash_table (info)->dynobj;
9761
9762 if (h->plt.offset != (bfd_vma) -1)
9763 {
6d79d2ed 9764 bfd_byte *loc;
0a44bf69
RS
9765 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9766 Elf_Internal_Rela rel;
9767 static const bfd_vma *plt_entry;
9768
9769 BFD_ASSERT (h->dynindx != -1);
9770 BFD_ASSERT (htab->splt != NULL);
9771 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9772
9773 /* Calculate the address of the .plt entry. */
9774 plt_address = (htab->splt->output_section->vma
9775 + htab->splt->output_offset
9776 + h->plt.offset);
9777
9778 /* Calculate the index of the entry. */
9779 plt_index = ((h->plt.offset - htab->plt_header_size)
9780 / htab->plt_entry_size);
9781
9782 /* Calculate the address of the .got.plt entry. */
9783 got_address = (htab->sgotplt->output_section->vma
9784 + htab->sgotplt->output_offset
9785 + plt_index * 4);
9786
9787 /* Calculate the offset of the .got.plt entry from
9788 _GLOBAL_OFFSET_TABLE_. */
9789 got_offset = mips_elf_gotplt_index (info, h);
9790
9791 /* Calculate the offset for the branch at the start of the PLT
9792 entry. The branch jumps to the beginning of .plt. */
9793 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9794
9795 /* Fill in the initial value of the .got.plt entry. */
9796 bfd_put_32 (output_bfd, plt_address,
9797 htab->sgotplt->contents + plt_index * 4);
9798
9799 /* Find out where the .plt entry should go. */
9800 loc = htab->splt->contents + h->plt.offset;
9801
9802 if (info->shared)
9803 {
9804 plt_entry = mips_vxworks_shared_plt_entry;
9805 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9806 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9807 }
9808 else
9809 {
9810 bfd_vma got_address_high, got_address_low;
9811
9812 plt_entry = mips_vxworks_exec_plt_entry;
9813 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9814 got_address_low = got_address & 0xffff;
9815
9816 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9817 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9818 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9819 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9820 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9821 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9822 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9823 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9824
9825 loc = (htab->srelplt2->contents
9826 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9827
9828 /* Emit a relocation for the .got.plt entry. */
9829 rel.r_offset = got_address;
9830 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9831 rel.r_addend = h->plt.offset;
9832 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9833
9834 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9835 loc += sizeof (Elf32_External_Rela);
9836 rel.r_offset = plt_address + 8;
9837 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9838 rel.r_addend = got_offset;
9839 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9840
9841 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9842 loc += sizeof (Elf32_External_Rela);
9843 rel.r_offset += 4;
9844 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9845 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9846 }
9847
9848 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9849 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9850 rel.r_offset = got_address;
9851 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9852 rel.r_addend = 0;
9853 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9854
9855 if (!h->def_regular)
9856 sym->st_shndx = SHN_UNDEF;
9857 }
9858
9859 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9860
23cc69b6 9861 sgot = htab->sgot;
a8028dd0 9862 g = htab->got_info;
0a44bf69
RS
9863 BFD_ASSERT (g != NULL);
9864
9865 /* See if this symbol has an entry in the GOT. */
9866 if (g->global_gotsym != NULL
9867 && h->dynindx >= g->global_gotsym->dynindx)
9868 {
9869 bfd_vma offset;
9870 Elf_Internal_Rela outrel;
9871 bfd_byte *loc;
9872 asection *s;
9873
9874 /* Install the symbol value in the GOT. */
9875 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9876 R_MIPS_GOT16, info);
9877 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9878
9879 /* Add a dynamic relocation for it. */
9880 s = mips_elf_rel_dyn_section (info, FALSE);
9881 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9882 outrel.r_offset = (sgot->output_section->vma
9883 + sgot->output_offset
9884 + offset);
9885 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9886 outrel.r_addend = 0;
9887 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9888 }
9889
9890 /* Emit a copy reloc, if needed. */
9891 if (h->needs_copy)
9892 {
9893 Elf_Internal_Rela rel;
9894
9895 BFD_ASSERT (h->dynindx != -1);
9896
9897 rel.r_offset = (h->root.u.def.section->output_section->vma
9898 + h->root.u.def.section->output_offset
9899 + h->root.u.def.value);
9900 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9901 rel.r_addend = 0;
9902 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9903 htab->srelbss->contents
9904 + (htab->srelbss->reloc_count
9905 * sizeof (Elf32_External_Rela)));
9906 ++htab->srelbss->reloc_count;
9907 }
9908
9909 /* If this is a mips16 symbol, force the value to be even. */
30c09090 9910 if (ELF_ST_IS_MIPS16 (sym->st_other))
0a44bf69
RS
9911 sym->st_value &= ~1;
9912
9913 return TRUE;
9914}
9915
861fb55a
DJ
9916/* Write out a plt0 entry to the beginning of .plt. */
9917
9918static void
9919mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9920{
9921 bfd_byte *loc;
9922 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9923 static const bfd_vma *plt_entry;
9924 struct mips_elf_link_hash_table *htab;
9925
9926 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9927 BFD_ASSERT (htab != NULL);
9928
861fb55a
DJ
9929 if (ABI_64_P (output_bfd))
9930 plt_entry = mips_n64_exec_plt0_entry;
9931 else if (ABI_N32_P (output_bfd))
9932 plt_entry = mips_n32_exec_plt0_entry;
9933 else
9934 plt_entry = mips_o32_exec_plt0_entry;
9935
9936 /* Calculate the value of .got.plt. */
9937 gotplt_value = (htab->sgotplt->output_section->vma
9938 + htab->sgotplt->output_offset);
9939 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9940 gotplt_value_low = gotplt_value & 0xffff;
9941
9942 /* The PLT sequence is not safe for N64 if .got.plt's address can
9943 not be loaded in two instructions. */
9944 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9945 || ~(gotplt_value | 0x7fffffff) == 0);
9946
9947 /* Install the PLT header. */
9948 loc = htab->splt->contents;
9949 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9950 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9951 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9952 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9953 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9954 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9955 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9956 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9957}
9958
0a44bf69
RS
9959/* Install the PLT header for a VxWorks executable and finalize the
9960 contents of .rela.plt.unloaded. */
9961
9962static void
9963mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9964{
9965 Elf_Internal_Rela rela;
9966 bfd_byte *loc;
9967 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9968 static const bfd_vma *plt_entry;
9969 struct mips_elf_link_hash_table *htab;
9970
9971 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9972 BFD_ASSERT (htab != NULL);
9973
0a44bf69
RS
9974 plt_entry = mips_vxworks_exec_plt0_entry;
9975
9976 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9977 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9978 + htab->root.hgot->root.u.def.section->output_offset
9979 + htab->root.hgot->root.u.def.value);
9980
9981 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9982 got_value_low = got_value & 0xffff;
9983
9984 /* Calculate the address of the PLT header. */
9985 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9986
9987 /* Install the PLT header. */
9988 loc = htab->splt->contents;
9989 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9990 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9991 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9992 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9993 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9994 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9995
9996 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9997 loc = htab->srelplt2->contents;
9998 rela.r_offset = plt_address;
9999 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10000 rela.r_addend = 0;
10001 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10002 loc += sizeof (Elf32_External_Rela);
10003
10004 /* Output the relocation for the following addiu of
10005 %lo(_GLOBAL_OFFSET_TABLE_). */
10006 rela.r_offset += 4;
10007 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10008 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10009 loc += sizeof (Elf32_External_Rela);
10010
10011 /* Fix up the remaining relocations. They may have the wrong
10012 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10013 in which symbols were output. */
10014 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10015 {
10016 Elf_Internal_Rela rel;
10017
10018 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10019 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10020 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10021 loc += sizeof (Elf32_External_Rela);
10022
10023 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10024 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10025 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10026 loc += sizeof (Elf32_External_Rela);
10027
10028 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10029 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10030 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10031 loc += sizeof (Elf32_External_Rela);
10032 }
10033}
10034
10035/* Install the PLT header for a VxWorks shared library. */
10036
10037static void
10038mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10039{
10040 unsigned int i;
10041 struct mips_elf_link_hash_table *htab;
10042
10043 htab = mips_elf_hash_table (info);
4dfe6ac6 10044 BFD_ASSERT (htab != NULL);
0a44bf69
RS
10045
10046 /* We just need to copy the entry byte-by-byte. */
10047 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10048 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10049 htab->splt->contents + i * 4);
10050}
10051
b49e97c9
TS
10052/* Finish up the dynamic sections. */
10053
b34976b6 10054bfd_boolean
9719ad41
RS
10055_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10056 struct bfd_link_info *info)
b49e97c9
TS
10057{
10058 bfd *dynobj;
10059 asection *sdyn;
10060 asection *sgot;
f4416af6 10061 struct mips_got_info *gg, *g;
0a44bf69 10062 struct mips_elf_link_hash_table *htab;
b49e97c9 10063
0a44bf69 10064 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
10065 BFD_ASSERT (htab != NULL);
10066
b49e97c9
TS
10067 dynobj = elf_hash_table (info)->dynobj;
10068
10069 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10070
23cc69b6
RS
10071 sgot = htab->sgot;
10072 gg = htab->got_info;
b49e97c9
TS
10073
10074 if (elf_hash_table (info)->dynamic_sections_created)
10075 {
10076 bfd_byte *b;
943284cc 10077 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
10078
10079 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
10080 BFD_ASSERT (gg != NULL);
10081
10082 g = mips_elf_got_for_ibfd (gg, output_bfd);
b49e97c9
TS
10083 BFD_ASSERT (g != NULL);
10084
10085 for (b = sdyn->contents;
eea6121a 10086 b < sdyn->contents + sdyn->size;
b49e97c9
TS
10087 b += MIPS_ELF_DYN_SIZE (dynobj))
10088 {
10089 Elf_Internal_Dyn dyn;
10090 const char *name;
10091 size_t elemsize;
10092 asection *s;
b34976b6 10093 bfd_boolean swap_out_p;
b49e97c9
TS
10094
10095 /* Read in the current dynamic entry. */
10096 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10097
10098 /* Assume that we're going to modify it and write it out. */
b34976b6 10099 swap_out_p = TRUE;
b49e97c9
TS
10100
10101 switch (dyn.d_tag)
10102 {
10103 case DT_RELENT:
b49e97c9
TS
10104 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10105 break;
10106
0a44bf69
RS
10107 case DT_RELAENT:
10108 BFD_ASSERT (htab->is_vxworks);
10109 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10110 break;
10111
b49e97c9
TS
10112 case DT_STRSZ:
10113 /* Rewrite DT_STRSZ. */
10114 dyn.d_un.d_val =
10115 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10116 break;
10117
10118 case DT_PLTGOT:
861fb55a
DJ
10119 s = htab->sgot;
10120 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10121 break;
10122
10123 case DT_MIPS_PLTGOT:
10124 s = htab->sgotplt;
10125 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
10126 break;
10127
10128 case DT_MIPS_RLD_VERSION:
10129 dyn.d_un.d_val = 1; /* XXX */
10130 break;
10131
10132 case DT_MIPS_FLAGS:
10133 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10134 break;
10135
b49e97c9 10136 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
10137 {
10138 time_t t;
10139 time (&t);
10140 dyn.d_un.d_val = t;
10141 }
b49e97c9
TS
10142 break;
10143
10144 case DT_MIPS_ICHECKSUM:
10145 /* XXX FIXME: */
b34976b6 10146 swap_out_p = FALSE;
b49e97c9
TS
10147 break;
10148
10149 case DT_MIPS_IVERSION:
10150 /* XXX FIXME: */
b34976b6 10151 swap_out_p = FALSE;
b49e97c9
TS
10152 break;
10153
10154 case DT_MIPS_BASE_ADDRESS:
10155 s = output_bfd->sections;
10156 BFD_ASSERT (s != NULL);
10157 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10158 break;
10159
10160 case DT_MIPS_LOCAL_GOTNO:
10161 dyn.d_un.d_val = g->local_gotno;
10162 break;
10163
10164 case DT_MIPS_UNREFEXTNO:
10165 /* The index into the dynamic symbol table which is the
10166 entry of the first external symbol that is not
10167 referenced within the same object. */
10168 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10169 break;
10170
10171 case DT_MIPS_GOTSYM:
f4416af6 10172 if (gg->global_gotsym)
b49e97c9 10173 {
f4416af6 10174 dyn.d_un.d_val = gg->global_gotsym->dynindx;
b49e97c9
TS
10175 break;
10176 }
10177 /* In case if we don't have global got symbols we default
10178 to setting DT_MIPS_GOTSYM to the same value as
10179 DT_MIPS_SYMTABNO, so we just fall through. */
10180
10181 case DT_MIPS_SYMTABNO:
10182 name = ".dynsym";
10183 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10184 s = bfd_get_section_by_name (output_bfd, name);
10185 BFD_ASSERT (s != NULL);
10186
eea6121a 10187 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
10188 break;
10189
10190 case DT_MIPS_HIPAGENO:
861fb55a 10191 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
10192 break;
10193
10194 case DT_MIPS_RLD_MAP:
10195 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10196 break;
10197
10198 case DT_MIPS_OPTIONS:
10199 s = (bfd_get_section_by_name
10200 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10201 dyn.d_un.d_ptr = s->vma;
10202 break;
10203
0a44bf69
RS
10204 case DT_RELASZ:
10205 BFD_ASSERT (htab->is_vxworks);
10206 /* The count does not include the JUMP_SLOT relocations. */
10207 if (htab->srelplt)
10208 dyn.d_un.d_val -= htab->srelplt->size;
10209 break;
10210
10211 case DT_PLTREL:
861fb55a
DJ
10212 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10213 if (htab->is_vxworks)
10214 dyn.d_un.d_val = DT_RELA;
10215 else
10216 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
10217 break;
10218
10219 case DT_PLTRELSZ:
861fb55a 10220 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
10221 dyn.d_un.d_val = htab->srelplt->size;
10222 break;
10223
10224 case DT_JMPREL:
861fb55a
DJ
10225 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10226 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
10227 + htab->srelplt->output_offset);
10228 break;
10229
943284cc
DJ
10230 case DT_TEXTREL:
10231 /* If we didn't need any text relocations after all, delete
10232 the dynamic tag. */
10233 if (!(info->flags & DF_TEXTREL))
10234 {
10235 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10236 swap_out_p = FALSE;
10237 }
10238 break;
10239
10240 case DT_FLAGS:
10241 /* If we didn't need any text relocations after all, clear
10242 DF_TEXTREL from DT_FLAGS. */
10243 if (!(info->flags & DF_TEXTREL))
10244 dyn.d_un.d_val &= ~DF_TEXTREL;
10245 else
10246 swap_out_p = FALSE;
10247 break;
10248
b49e97c9 10249 default:
b34976b6 10250 swap_out_p = FALSE;
7a2b07ff
NS
10251 if (htab->is_vxworks
10252 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10253 swap_out_p = TRUE;
b49e97c9
TS
10254 break;
10255 }
10256
943284cc 10257 if (swap_out_p || dyn_skipped)
b49e97c9 10258 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
10259 (dynobj, &dyn, b - dyn_skipped);
10260
10261 if (dyn_to_skip)
10262 {
10263 dyn_skipped += dyn_to_skip;
10264 dyn_to_skip = 0;
10265 }
b49e97c9 10266 }
943284cc
DJ
10267
10268 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10269 if (dyn_skipped > 0)
10270 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
10271 }
10272
b55fd4d4
DJ
10273 if (sgot != NULL && sgot->size > 0
10274 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 10275 {
0a44bf69
RS
10276 if (htab->is_vxworks)
10277 {
10278 /* The first entry of the global offset table points to the
10279 ".dynamic" section. The second is initialized by the
10280 loader and contains the shared library identifier.
10281 The third is also initialized by the loader and points
10282 to the lazy resolution stub. */
10283 MIPS_ELF_PUT_WORD (output_bfd,
10284 sdyn->output_offset + sdyn->output_section->vma,
10285 sgot->contents);
10286 MIPS_ELF_PUT_WORD (output_bfd, 0,
10287 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10288 MIPS_ELF_PUT_WORD (output_bfd, 0,
10289 sgot->contents
10290 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10291 }
10292 else
10293 {
10294 /* The first entry of the global offset table will be filled at
10295 runtime. The second entry will be used by some runtime loaders.
10296 This isn't the case of IRIX rld. */
10297 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 10298 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
10299 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10300 }
b49e97c9 10301
54938e2a
TS
10302 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10303 = MIPS_ELF_GOT_SIZE (output_bfd);
10304 }
b49e97c9 10305
f4416af6
AO
10306 /* Generate dynamic relocations for the non-primary gots. */
10307 if (gg != NULL && gg->next)
10308 {
10309 Elf_Internal_Rela rel[3];
10310 bfd_vma addend = 0;
10311
10312 memset (rel, 0, sizeof (rel));
10313 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10314
10315 for (g = gg->next; g->next != gg; g = g->next)
10316 {
91d6fa6a 10317 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 10318 + g->next->tls_gotno;
f4416af6 10319
9719ad41 10320 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 10321 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
10322 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10323 sgot->contents
91d6fa6a 10324 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
10325
10326 if (! info->shared)
10327 continue;
10328
91d6fa6a 10329 while (got_index < g->assigned_gotno)
f4416af6
AO
10330 {
10331 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
91d6fa6a 10332 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
10333 if (!(mips_elf_create_dynamic_relocation
10334 (output_bfd, info, rel, NULL,
10335 bfd_abs_section_ptr,
10336 0, &addend, sgot)))
10337 return FALSE;
10338 BFD_ASSERT (addend == 0);
10339 }
10340 }
10341 }
10342
3133ddbf
DJ
10343 /* The generation of dynamic relocations for the non-primary gots
10344 adds more dynamic relocations. We cannot count them until
10345 here. */
10346
10347 if (elf_hash_table (info)->dynamic_sections_created)
10348 {
10349 bfd_byte *b;
10350 bfd_boolean swap_out_p;
10351
10352 BFD_ASSERT (sdyn != NULL);
10353
10354 for (b = sdyn->contents;
10355 b < sdyn->contents + sdyn->size;
10356 b += MIPS_ELF_DYN_SIZE (dynobj))
10357 {
10358 Elf_Internal_Dyn dyn;
10359 asection *s;
10360
10361 /* Read in the current dynamic entry. */
10362 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10363
10364 /* Assume that we're going to modify it and write it out. */
10365 swap_out_p = TRUE;
10366
10367 switch (dyn.d_tag)
10368 {
10369 case DT_RELSZ:
10370 /* Reduce DT_RELSZ to account for any relocations we
10371 decided not to make. This is for the n64 irix rld,
10372 which doesn't seem to apply any relocations if there
10373 are trailing null entries. */
0a44bf69 10374 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
10375 dyn.d_un.d_val = (s->reloc_count
10376 * (ABI_64_P (output_bfd)
10377 ? sizeof (Elf64_Mips_External_Rel)
10378 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
10379 /* Adjust the section size too. Tools like the prelinker
10380 can reasonably expect the values to the same. */
10381 elf_section_data (s->output_section)->this_hdr.sh_size
10382 = dyn.d_un.d_val;
3133ddbf
DJ
10383 break;
10384
10385 default:
10386 swap_out_p = FALSE;
10387 break;
10388 }
10389
10390 if (swap_out_p)
10391 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10392 (dynobj, &dyn, b);
10393 }
10394 }
10395
b49e97c9 10396 {
b49e97c9
TS
10397 asection *s;
10398 Elf32_compact_rel cpt;
10399
b49e97c9
TS
10400 if (SGI_COMPAT (output_bfd))
10401 {
10402 /* Write .compact_rel section out. */
10403 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10404 if (s != NULL)
10405 {
10406 cpt.id1 = 1;
10407 cpt.num = s->reloc_count;
10408 cpt.id2 = 2;
10409 cpt.offset = (s->output_section->filepos
10410 + sizeof (Elf32_External_compact_rel));
10411 cpt.reserved0 = 0;
10412 cpt.reserved1 = 0;
10413 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10414 ((Elf32_External_compact_rel *)
10415 s->contents));
10416
10417 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 10418 if (htab->sstubs != NULL)
b49e97c9
TS
10419 {
10420 file_ptr dummy_offset;
10421
4e41d0d7
RS
10422 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10423 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10424 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 10425 htab->function_stub_size);
b49e97c9
TS
10426 }
10427 }
10428 }
10429
0a44bf69
RS
10430 /* The psABI says that the dynamic relocations must be sorted in
10431 increasing order of r_symndx. The VxWorks EABI doesn't require
10432 this, and because the code below handles REL rather than RELA
10433 relocations, using it for VxWorks would be outright harmful. */
10434 if (!htab->is_vxworks)
b49e97c9 10435 {
0a44bf69
RS
10436 s = mips_elf_rel_dyn_section (info, FALSE);
10437 if (s != NULL
10438 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10439 {
10440 reldyn_sorting_bfd = output_bfd;
b49e97c9 10441
0a44bf69
RS
10442 if (ABI_64_P (output_bfd))
10443 qsort ((Elf64_External_Rel *) s->contents + 1,
10444 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10445 sort_dynamic_relocs_64);
10446 else
10447 qsort ((Elf32_External_Rel *) s->contents + 1,
10448 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10449 sort_dynamic_relocs);
10450 }
b49e97c9 10451 }
b49e97c9
TS
10452 }
10453
861fb55a 10454 if (htab->splt && htab->splt->size > 0)
0a44bf69 10455 {
861fb55a
DJ
10456 if (htab->is_vxworks)
10457 {
10458 if (info->shared)
10459 mips_vxworks_finish_shared_plt (output_bfd, info);
10460 else
10461 mips_vxworks_finish_exec_plt (output_bfd, info);
10462 }
0a44bf69 10463 else
861fb55a
DJ
10464 {
10465 BFD_ASSERT (!info->shared);
10466 mips_finish_exec_plt (output_bfd, info);
10467 }
0a44bf69 10468 }
b34976b6 10469 return TRUE;
b49e97c9
TS
10470}
10471
b49e97c9 10472
64543e1a
RS
10473/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10474
10475static void
9719ad41 10476mips_set_isa_flags (bfd *abfd)
b49e97c9 10477{
64543e1a 10478 flagword val;
b49e97c9
TS
10479
10480 switch (bfd_get_mach (abfd))
10481 {
10482 default:
10483 case bfd_mach_mips3000:
10484 val = E_MIPS_ARCH_1;
10485 break;
10486
10487 case bfd_mach_mips3900:
10488 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10489 break;
10490
10491 case bfd_mach_mips6000:
10492 val = E_MIPS_ARCH_2;
10493 break;
10494
10495 case bfd_mach_mips4000:
10496 case bfd_mach_mips4300:
10497 case bfd_mach_mips4400:
10498 case bfd_mach_mips4600:
10499 val = E_MIPS_ARCH_3;
10500 break;
10501
10502 case bfd_mach_mips4010:
10503 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10504 break;
10505
10506 case bfd_mach_mips4100:
10507 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10508 break;
10509
10510 case bfd_mach_mips4111:
10511 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10512 break;
10513
00707a0e
RS
10514 case bfd_mach_mips4120:
10515 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10516 break;
10517
b49e97c9
TS
10518 case bfd_mach_mips4650:
10519 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10520 break;
10521
00707a0e
RS
10522 case bfd_mach_mips5400:
10523 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10524 break;
10525
10526 case bfd_mach_mips5500:
10527 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10528 break;
10529
0d2e43ed
ILT
10530 case bfd_mach_mips9000:
10531 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10532 break;
10533
b49e97c9 10534 case bfd_mach_mips5000:
5a7ea749 10535 case bfd_mach_mips7000:
b49e97c9
TS
10536 case bfd_mach_mips8000:
10537 case bfd_mach_mips10000:
10538 case bfd_mach_mips12000:
3aa3176b
TS
10539 case bfd_mach_mips14000:
10540 case bfd_mach_mips16000:
b49e97c9
TS
10541 val = E_MIPS_ARCH_4;
10542 break;
10543
10544 case bfd_mach_mips5:
10545 val = E_MIPS_ARCH_5;
10546 break;
10547
350cc38d
MS
10548 case bfd_mach_mips_loongson_2e:
10549 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10550 break;
10551
10552 case bfd_mach_mips_loongson_2f:
10553 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10554 break;
10555
b49e97c9
TS
10556 case bfd_mach_mips_sb1:
10557 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10558 break;
10559
6f179bd0
AN
10560 case bfd_mach_mips_octeon:
10561 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10562 break;
10563
52b6b6b9
JM
10564 case bfd_mach_mips_xlr:
10565 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10566 break;
10567
b49e97c9
TS
10568 case bfd_mach_mipsisa32:
10569 val = E_MIPS_ARCH_32;
10570 break;
10571
10572 case bfd_mach_mipsisa64:
10573 val = E_MIPS_ARCH_64;
af7ee8bf
CD
10574 break;
10575
10576 case bfd_mach_mipsisa32r2:
10577 val = E_MIPS_ARCH_32R2;
10578 break;
5f74bc13
CD
10579
10580 case bfd_mach_mipsisa64r2:
10581 val = E_MIPS_ARCH_64R2;
10582 break;
b49e97c9 10583 }
b49e97c9
TS
10584 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10585 elf_elfheader (abfd)->e_flags |= val;
10586
64543e1a
RS
10587}
10588
10589
10590/* The final processing done just before writing out a MIPS ELF object
10591 file. This gets the MIPS architecture right based on the machine
10592 number. This is used by both the 32-bit and the 64-bit ABI. */
10593
10594void
9719ad41
RS
10595_bfd_mips_elf_final_write_processing (bfd *abfd,
10596 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
10597{
10598 unsigned int i;
10599 Elf_Internal_Shdr **hdrpp;
10600 const char *name;
10601 asection *sec;
10602
10603 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10604 is nonzero. This is for compatibility with old objects, which used
10605 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10606 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10607 mips_set_isa_flags (abfd);
10608
b49e97c9
TS
10609 /* Set the sh_info field for .gptab sections and other appropriate
10610 info for each special section. */
10611 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10612 i < elf_numsections (abfd);
10613 i++, hdrpp++)
10614 {
10615 switch ((*hdrpp)->sh_type)
10616 {
10617 case SHT_MIPS_MSYM:
10618 case SHT_MIPS_LIBLIST:
10619 sec = bfd_get_section_by_name (abfd, ".dynstr");
10620 if (sec != NULL)
10621 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10622 break;
10623
10624 case SHT_MIPS_GPTAB:
10625 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10626 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10627 BFD_ASSERT (name != NULL
0112cd26 10628 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
10629 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10630 BFD_ASSERT (sec != NULL);
10631 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10632 break;
10633
10634 case SHT_MIPS_CONTENT:
10635 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10636 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10637 BFD_ASSERT (name != NULL
0112cd26 10638 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
10639 sec = bfd_get_section_by_name (abfd,
10640 name + sizeof ".MIPS.content" - 1);
10641 BFD_ASSERT (sec != NULL);
10642 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10643 break;
10644
10645 case SHT_MIPS_SYMBOL_LIB:
10646 sec = bfd_get_section_by_name (abfd, ".dynsym");
10647 if (sec != NULL)
10648 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10649 sec = bfd_get_section_by_name (abfd, ".liblist");
10650 if (sec != NULL)
10651 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10652 break;
10653
10654 case SHT_MIPS_EVENTS:
10655 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10656 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10657 BFD_ASSERT (name != NULL);
0112cd26 10658 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
10659 sec = bfd_get_section_by_name (abfd,
10660 name + sizeof ".MIPS.events" - 1);
10661 else
10662 {
0112cd26 10663 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
10664 sec = bfd_get_section_by_name (abfd,
10665 (name
10666 + sizeof ".MIPS.post_rel" - 1));
10667 }
10668 BFD_ASSERT (sec != NULL);
10669 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10670 break;
10671
10672 }
10673 }
10674}
10675\f
8dc1a139 10676/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
10677 segments. */
10678
10679int
a6b96beb
AM
10680_bfd_mips_elf_additional_program_headers (bfd *abfd,
10681 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
10682{
10683 asection *s;
10684 int ret = 0;
10685
10686 /* See if we need a PT_MIPS_REGINFO segment. */
10687 s = bfd_get_section_by_name (abfd, ".reginfo");
10688 if (s && (s->flags & SEC_LOAD))
10689 ++ret;
10690
10691 /* See if we need a PT_MIPS_OPTIONS segment. */
10692 if (IRIX_COMPAT (abfd) == ict_irix6
10693 && bfd_get_section_by_name (abfd,
10694 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10695 ++ret;
10696
10697 /* See if we need a PT_MIPS_RTPROC segment. */
10698 if (IRIX_COMPAT (abfd) == ict_irix5
10699 && bfd_get_section_by_name (abfd, ".dynamic")
10700 && bfd_get_section_by_name (abfd, ".mdebug"))
10701 ++ret;
10702
98c904a8
RS
10703 /* Allocate a PT_NULL header in dynamic objects. See
10704 _bfd_mips_elf_modify_segment_map for details. */
10705 if (!SGI_COMPAT (abfd)
10706 && bfd_get_section_by_name (abfd, ".dynamic"))
10707 ++ret;
10708
b49e97c9
TS
10709 return ret;
10710}
10711
8dc1a139 10712/* Modify the segment map for an IRIX5 executable. */
b49e97c9 10713
b34976b6 10714bfd_boolean
9719ad41 10715_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 10716 struct bfd_link_info *info)
b49e97c9
TS
10717{
10718 asection *s;
10719 struct elf_segment_map *m, **pm;
10720 bfd_size_type amt;
10721
10722 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10723 segment. */
10724 s = bfd_get_section_by_name (abfd, ".reginfo");
10725 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10726 {
10727 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10728 if (m->p_type == PT_MIPS_REGINFO)
10729 break;
10730 if (m == NULL)
10731 {
10732 amt = sizeof *m;
9719ad41 10733 m = bfd_zalloc (abfd, amt);
b49e97c9 10734 if (m == NULL)
b34976b6 10735 return FALSE;
b49e97c9
TS
10736
10737 m->p_type = PT_MIPS_REGINFO;
10738 m->count = 1;
10739 m->sections[0] = s;
10740
10741 /* We want to put it after the PHDR and INTERP segments. */
10742 pm = &elf_tdata (abfd)->segment_map;
10743 while (*pm != NULL
10744 && ((*pm)->p_type == PT_PHDR
10745 || (*pm)->p_type == PT_INTERP))
10746 pm = &(*pm)->next;
10747
10748 m->next = *pm;
10749 *pm = m;
10750 }
10751 }
10752
10753 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10754 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 10755 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 10756 table. */
c1fd6598
AO
10757 if (NEWABI_P (abfd)
10758 /* On non-IRIX6 new abi, we'll have already created a segment
10759 for this section, so don't create another. I'm not sure this
10760 is not also the case for IRIX 6, but I can't test it right
10761 now. */
10762 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
10763 {
10764 for (s = abfd->sections; s; s = s->next)
10765 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10766 break;
10767
10768 if (s)
10769 {
10770 struct elf_segment_map *options_segment;
10771
98a8deaf
RS
10772 pm = &elf_tdata (abfd)->segment_map;
10773 while (*pm != NULL
10774 && ((*pm)->p_type == PT_PHDR
10775 || (*pm)->p_type == PT_INTERP))
10776 pm = &(*pm)->next;
b49e97c9 10777
8ded5a0f
AM
10778 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10779 {
10780 amt = sizeof (struct elf_segment_map);
10781 options_segment = bfd_zalloc (abfd, amt);
10782 options_segment->next = *pm;
10783 options_segment->p_type = PT_MIPS_OPTIONS;
10784 options_segment->p_flags = PF_R;
10785 options_segment->p_flags_valid = TRUE;
10786 options_segment->count = 1;
10787 options_segment->sections[0] = s;
10788 *pm = options_segment;
10789 }
b49e97c9
TS
10790 }
10791 }
10792 else
10793 {
10794 if (IRIX_COMPAT (abfd) == ict_irix5)
10795 {
10796 /* If there are .dynamic and .mdebug sections, we make a room
10797 for the RTPROC header. FIXME: Rewrite without section names. */
10798 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10799 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10800 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10801 {
10802 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10803 if (m->p_type == PT_MIPS_RTPROC)
10804 break;
10805 if (m == NULL)
10806 {
10807 amt = sizeof *m;
9719ad41 10808 m = bfd_zalloc (abfd, amt);
b49e97c9 10809 if (m == NULL)
b34976b6 10810 return FALSE;
b49e97c9
TS
10811
10812 m->p_type = PT_MIPS_RTPROC;
10813
10814 s = bfd_get_section_by_name (abfd, ".rtproc");
10815 if (s == NULL)
10816 {
10817 m->count = 0;
10818 m->p_flags = 0;
10819 m->p_flags_valid = 1;
10820 }
10821 else
10822 {
10823 m->count = 1;
10824 m->sections[0] = s;
10825 }
10826
10827 /* We want to put it after the DYNAMIC segment. */
10828 pm = &elf_tdata (abfd)->segment_map;
10829 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10830 pm = &(*pm)->next;
10831 if (*pm != NULL)
10832 pm = &(*pm)->next;
10833
10834 m->next = *pm;
10835 *pm = m;
10836 }
10837 }
10838 }
8dc1a139 10839 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
10840 .dynstr, .dynsym, and .hash sections, and everything in
10841 between. */
10842 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10843 pm = &(*pm)->next)
10844 if ((*pm)->p_type == PT_DYNAMIC)
10845 break;
10846 m = *pm;
10847 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10848 {
10849 /* For a normal mips executable the permissions for the PT_DYNAMIC
10850 segment are read, write and execute. We do that here since
10851 the code in elf.c sets only the read permission. This matters
10852 sometimes for the dynamic linker. */
10853 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10854 {
10855 m->p_flags = PF_R | PF_W | PF_X;
10856 m->p_flags_valid = 1;
10857 }
10858 }
f6f62d6f
RS
10859 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10860 glibc's dynamic linker has traditionally derived the number of
10861 tags from the p_filesz field, and sometimes allocates stack
10862 arrays of that size. An overly-big PT_DYNAMIC segment can
10863 be actively harmful in such cases. Making PT_DYNAMIC contain
10864 other sections can also make life hard for the prelinker,
10865 which might move one of the other sections to a different
10866 PT_LOAD segment. */
10867 if (SGI_COMPAT (abfd)
10868 && m != NULL
10869 && m->count == 1
10870 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
10871 {
10872 static const char *sec_names[] =
10873 {
10874 ".dynamic", ".dynstr", ".dynsym", ".hash"
10875 };
10876 bfd_vma low, high;
10877 unsigned int i, c;
10878 struct elf_segment_map *n;
10879
792b4a53 10880 low = ~(bfd_vma) 0;
b49e97c9
TS
10881 high = 0;
10882 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10883 {
10884 s = bfd_get_section_by_name (abfd, sec_names[i]);
10885 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10886 {
10887 bfd_size_type sz;
10888
10889 if (low > s->vma)
10890 low = s->vma;
eea6121a 10891 sz = s->size;
b49e97c9
TS
10892 if (high < s->vma + sz)
10893 high = s->vma + sz;
10894 }
10895 }
10896
10897 c = 0;
10898 for (s = abfd->sections; s != NULL; s = s->next)
10899 if ((s->flags & SEC_LOAD) != 0
10900 && s->vma >= low
eea6121a 10901 && s->vma + s->size <= high)
b49e97c9
TS
10902 ++c;
10903
10904 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 10905 n = bfd_zalloc (abfd, amt);
b49e97c9 10906 if (n == NULL)
b34976b6 10907 return FALSE;
b49e97c9
TS
10908 *n = *m;
10909 n->count = c;
10910
10911 i = 0;
10912 for (s = abfd->sections; s != NULL; s = s->next)
10913 {
10914 if ((s->flags & SEC_LOAD) != 0
10915 && s->vma >= low
eea6121a 10916 && s->vma + s->size <= high)
b49e97c9
TS
10917 {
10918 n->sections[i] = s;
10919 ++i;
10920 }
10921 }
10922
10923 *pm = n;
10924 }
10925 }
10926
98c904a8
RS
10927 /* Allocate a spare program header in dynamic objects so that tools
10928 like the prelinker can add an extra PT_LOAD entry.
10929
10930 If the prelinker needs to make room for a new PT_LOAD entry, its
10931 standard procedure is to move the first (read-only) sections into
10932 the new (writable) segment. However, the MIPS ABI requires
10933 .dynamic to be in a read-only segment, and the section will often
10934 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10935
10936 Although the prelinker could in principle move .dynamic to a
10937 writable segment, it seems better to allocate a spare program
10938 header instead, and avoid the need to move any sections.
10939 There is a long tradition of allocating spare dynamic tags,
10940 so allocating a spare program header seems like a natural
7c8b76cc
JM
10941 extension.
10942
10943 If INFO is NULL, we may be copying an already prelinked binary
10944 with objcopy or strip, so do not add this header. */
10945 if (info != NULL
10946 && !SGI_COMPAT (abfd)
98c904a8
RS
10947 && bfd_get_section_by_name (abfd, ".dynamic"))
10948 {
10949 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10950 if ((*pm)->p_type == PT_NULL)
10951 break;
10952 if (*pm == NULL)
10953 {
10954 m = bfd_zalloc (abfd, sizeof (*m));
10955 if (m == NULL)
10956 return FALSE;
10957
10958 m->p_type = PT_NULL;
10959 *pm = m;
10960 }
10961 }
10962
b34976b6 10963 return TRUE;
b49e97c9
TS
10964}
10965\f
10966/* Return the section that should be marked against GC for a given
10967 relocation. */
10968
10969asection *
9719ad41 10970_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 10971 struct bfd_link_info *info,
9719ad41
RS
10972 Elf_Internal_Rela *rel,
10973 struct elf_link_hash_entry *h,
10974 Elf_Internal_Sym *sym)
b49e97c9
TS
10975{
10976 /* ??? Do mips16 stub sections need to be handled special? */
10977
10978 if (h != NULL)
07adf181
AM
10979 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10980 {
10981 case R_MIPS_GNU_VTINHERIT:
10982 case R_MIPS_GNU_VTENTRY:
10983 return NULL;
10984 }
b49e97c9 10985
07adf181 10986 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
10987}
10988
10989/* Update the got entry reference counts for the section being removed. */
10990
b34976b6 10991bfd_boolean
9719ad41
RS
10992_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10993 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10994 asection *sec ATTRIBUTE_UNUSED,
10995 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
10996{
10997#if 0
10998 Elf_Internal_Shdr *symtab_hdr;
10999 struct elf_link_hash_entry **sym_hashes;
11000 bfd_signed_vma *local_got_refcounts;
11001 const Elf_Internal_Rela *rel, *relend;
11002 unsigned long r_symndx;
11003 struct elf_link_hash_entry *h;
11004
7dda2462
TG
11005 if (info->relocatable)
11006 return TRUE;
11007
b49e97c9
TS
11008 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11009 sym_hashes = elf_sym_hashes (abfd);
11010 local_got_refcounts = elf_local_got_refcounts (abfd);
11011
11012 relend = relocs + sec->reloc_count;
11013 for (rel = relocs; rel < relend; rel++)
11014 switch (ELF_R_TYPE (abfd, rel->r_info))
11015 {
738e5348
RS
11016 case R_MIPS16_GOT16:
11017 case R_MIPS16_CALL16:
b49e97c9
TS
11018 case R_MIPS_GOT16:
11019 case R_MIPS_CALL16:
11020 case R_MIPS_CALL_HI16:
11021 case R_MIPS_CALL_LO16:
11022 case R_MIPS_GOT_HI16:
11023 case R_MIPS_GOT_LO16:
4a14403c
TS
11024 case R_MIPS_GOT_DISP:
11025 case R_MIPS_GOT_PAGE:
11026 case R_MIPS_GOT_OFST:
b49e97c9
TS
11027 /* ??? It would seem that the existing MIPS code does no sort
11028 of reference counting or whatnot on its GOT and PLT entries,
11029 so it is not possible to garbage collect them at this time. */
11030 break;
11031
11032 default:
11033 break;
11034 }
11035#endif
11036
b34976b6 11037 return TRUE;
b49e97c9
TS
11038}
11039\f
11040/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11041 hiding the old indirect symbol. Process additional relocation
11042 information. Also called for weakdefs, in which case we just let
11043 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11044
11045void
fcfa13d2 11046_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
11047 struct elf_link_hash_entry *dir,
11048 struct elf_link_hash_entry *ind)
b49e97c9
TS
11049{
11050 struct mips_elf_link_hash_entry *dirmips, *indmips;
11051
fcfa13d2 11052 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 11053
861fb55a
DJ
11054 dirmips = (struct mips_elf_link_hash_entry *) dir;
11055 indmips = (struct mips_elf_link_hash_entry *) ind;
11056 /* Any absolute non-dynamic relocations against an indirect or weak
11057 definition will be against the target symbol. */
11058 if (indmips->has_static_relocs)
11059 dirmips->has_static_relocs = TRUE;
11060
b49e97c9
TS
11061 if (ind->root.type != bfd_link_hash_indirect)
11062 return;
11063
b49e97c9
TS
11064 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11065 if (indmips->readonly_reloc)
b34976b6 11066 dirmips->readonly_reloc = TRUE;
b49e97c9 11067 if (indmips->no_fn_stub)
b34976b6 11068 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
11069 if (indmips->fn_stub)
11070 {
11071 dirmips->fn_stub = indmips->fn_stub;
11072 indmips->fn_stub = NULL;
11073 }
11074 if (indmips->need_fn_stub)
11075 {
11076 dirmips->need_fn_stub = TRUE;
11077 indmips->need_fn_stub = FALSE;
11078 }
11079 if (indmips->call_stub)
11080 {
11081 dirmips->call_stub = indmips->call_stub;
11082 indmips->call_stub = NULL;
11083 }
11084 if (indmips->call_fp_stub)
11085 {
11086 dirmips->call_fp_stub = indmips->call_fp_stub;
11087 indmips->call_fp_stub = NULL;
11088 }
634835ae
RS
11089 if (indmips->global_got_area < dirmips->global_got_area)
11090 dirmips->global_got_area = indmips->global_got_area;
11091 if (indmips->global_got_area < GGA_NONE)
11092 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
11093 if (indmips->has_nonpic_branches)
11094 dirmips->has_nonpic_branches = TRUE;
0f20cc35
DJ
11095
11096 if (dirmips->tls_type == 0)
11097 dirmips->tls_type = indmips->tls_type;
b49e97c9 11098}
b49e97c9 11099\f
d01414a5
TS
11100#define PDR_SIZE 32
11101
b34976b6 11102bfd_boolean
9719ad41
RS
11103_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11104 struct bfd_link_info *info)
d01414a5
TS
11105{
11106 asection *o;
b34976b6 11107 bfd_boolean ret = FALSE;
d01414a5
TS
11108 unsigned char *tdata;
11109 size_t i, skip;
11110
11111 o = bfd_get_section_by_name (abfd, ".pdr");
11112 if (! o)
b34976b6 11113 return FALSE;
eea6121a 11114 if (o->size == 0)
b34976b6 11115 return FALSE;
eea6121a 11116 if (o->size % PDR_SIZE != 0)
b34976b6 11117 return FALSE;
d01414a5
TS
11118 if (o->output_section != NULL
11119 && bfd_is_abs_section (o->output_section))
b34976b6 11120 return FALSE;
d01414a5 11121
eea6121a 11122 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 11123 if (! tdata)
b34976b6 11124 return FALSE;
d01414a5 11125
9719ad41 11126 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 11127 info->keep_memory);
d01414a5
TS
11128 if (!cookie->rels)
11129 {
11130 free (tdata);
b34976b6 11131 return FALSE;
d01414a5
TS
11132 }
11133
11134 cookie->rel = cookie->rels;
11135 cookie->relend = cookie->rels + o->reloc_count;
11136
eea6121a 11137 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 11138 {
c152c796 11139 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
11140 {
11141 tdata[i] = 1;
11142 skip ++;
11143 }
11144 }
11145
11146 if (skip != 0)
11147 {
f0abc2a1 11148 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 11149 o->size -= skip * PDR_SIZE;
b34976b6 11150 ret = TRUE;
d01414a5
TS
11151 }
11152 else
11153 free (tdata);
11154
11155 if (! info->keep_memory)
11156 free (cookie->rels);
11157
11158 return ret;
11159}
11160
b34976b6 11161bfd_boolean
9719ad41 11162_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
11163{
11164 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
11165 return TRUE;
11166 return FALSE;
53bfd6b4 11167}
d01414a5 11168
b34976b6 11169bfd_boolean
c7b8f16e
JB
11170_bfd_mips_elf_write_section (bfd *output_bfd,
11171 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11172 asection *sec, bfd_byte *contents)
d01414a5
TS
11173{
11174 bfd_byte *to, *from, *end;
11175 int i;
11176
11177 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 11178 return FALSE;
d01414a5 11179
f0abc2a1 11180 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 11181 return FALSE;
d01414a5
TS
11182
11183 to = contents;
eea6121a 11184 end = contents + sec->size;
d01414a5
TS
11185 for (from = contents, i = 0;
11186 from < end;
11187 from += PDR_SIZE, i++)
11188 {
f0abc2a1 11189 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
11190 continue;
11191 if (to != from)
11192 memcpy (to, from, PDR_SIZE);
11193 to += PDR_SIZE;
11194 }
11195 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 11196 sec->output_offset, sec->size);
b34976b6 11197 return TRUE;
d01414a5 11198}
53bfd6b4 11199\f
b49e97c9
TS
11200/* MIPS ELF uses a special find_nearest_line routine in order the
11201 handle the ECOFF debugging information. */
11202
11203struct mips_elf_find_line
11204{
11205 struct ecoff_debug_info d;
11206 struct ecoff_find_line i;
11207};
11208
b34976b6 11209bfd_boolean
9719ad41
RS
11210_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11211 asymbol **symbols, bfd_vma offset,
11212 const char **filename_ptr,
11213 const char **functionname_ptr,
11214 unsigned int *line_ptr)
b49e97c9
TS
11215{
11216 asection *msec;
11217
11218 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11219 filename_ptr, functionname_ptr,
11220 line_ptr))
b34976b6 11221 return TRUE;
b49e97c9
TS
11222
11223 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11224 filename_ptr, functionname_ptr,
9719ad41 11225 line_ptr, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 11226 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 11227 return TRUE;
b49e97c9
TS
11228
11229 msec = bfd_get_section_by_name (abfd, ".mdebug");
11230 if (msec != NULL)
11231 {
11232 flagword origflags;
11233 struct mips_elf_find_line *fi;
11234 const struct ecoff_debug_swap * const swap =
11235 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11236
11237 /* If we are called during a link, mips_elf_final_link may have
11238 cleared the SEC_HAS_CONTENTS field. We force it back on here
11239 if appropriate (which it normally will be). */
11240 origflags = msec->flags;
11241 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11242 msec->flags |= SEC_HAS_CONTENTS;
11243
11244 fi = elf_tdata (abfd)->find_line_info;
11245 if (fi == NULL)
11246 {
11247 bfd_size_type external_fdr_size;
11248 char *fraw_src;
11249 char *fraw_end;
11250 struct fdr *fdr_ptr;
11251 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11252
9719ad41 11253 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
11254 if (fi == NULL)
11255 {
11256 msec->flags = origflags;
b34976b6 11257 return FALSE;
b49e97c9
TS
11258 }
11259
11260 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11261 {
11262 msec->flags = origflags;
b34976b6 11263 return FALSE;
b49e97c9
TS
11264 }
11265
11266 /* Swap in the FDR information. */
11267 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 11268 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
11269 if (fi->d.fdr == NULL)
11270 {
11271 msec->flags = origflags;
b34976b6 11272 return FALSE;
b49e97c9
TS
11273 }
11274 external_fdr_size = swap->external_fdr_size;
11275 fdr_ptr = fi->d.fdr;
11276 fraw_src = (char *) fi->d.external_fdr;
11277 fraw_end = (fraw_src
11278 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11279 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 11280 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9
TS
11281
11282 elf_tdata (abfd)->find_line_info = fi;
11283
11284 /* Note that we don't bother to ever free this information.
11285 find_nearest_line is either called all the time, as in
11286 objdump -l, so the information should be saved, or it is
11287 rarely called, as in ld error messages, so the memory
11288 wasted is unimportant. Still, it would probably be a
11289 good idea for free_cached_info to throw it away. */
11290 }
11291
11292 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11293 &fi->i, filename_ptr, functionname_ptr,
11294 line_ptr))
11295 {
11296 msec->flags = origflags;
b34976b6 11297 return TRUE;
b49e97c9
TS
11298 }
11299
11300 msec->flags = origflags;
11301 }
11302
11303 /* Fall back on the generic ELF find_nearest_line routine. */
11304
11305 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11306 filename_ptr, functionname_ptr,
11307 line_ptr);
11308}
4ab527b0
FF
11309
11310bfd_boolean
11311_bfd_mips_elf_find_inliner_info (bfd *abfd,
11312 const char **filename_ptr,
11313 const char **functionname_ptr,
11314 unsigned int *line_ptr)
11315{
11316 bfd_boolean found;
11317 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11318 functionname_ptr, line_ptr,
11319 & elf_tdata (abfd)->dwarf2_find_line_info);
11320 return found;
11321}
11322
b49e97c9
TS
11323\f
11324/* When are writing out the .options or .MIPS.options section,
11325 remember the bytes we are writing out, so that we can install the
11326 GP value in the section_processing routine. */
11327
b34976b6 11328bfd_boolean
9719ad41
RS
11329_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11330 const void *location,
11331 file_ptr offset, bfd_size_type count)
b49e97c9 11332{
cc2e31b9 11333 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
11334 {
11335 bfd_byte *c;
11336
11337 if (elf_section_data (section) == NULL)
11338 {
11339 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 11340 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 11341 if (elf_section_data (section) == NULL)
b34976b6 11342 return FALSE;
b49e97c9 11343 }
f0abc2a1 11344 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
11345 if (c == NULL)
11346 {
eea6121a 11347 c = bfd_zalloc (abfd, section->size);
b49e97c9 11348 if (c == NULL)
b34976b6 11349 return FALSE;
f0abc2a1 11350 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
11351 }
11352
9719ad41 11353 memcpy (c + offset, location, count);
b49e97c9
TS
11354 }
11355
11356 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11357 count);
11358}
11359
11360/* This is almost identical to bfd_generic_get_... except that some
11361 MIPS relocations need to be handled specially. Sigh. */
11362
11363bfd_byte *
9719ad41
RS
11364_bfd_elf_mips_get_relocated_section_contents
11365 (bfd *abfd,
11366 struct bfd_link_info *link_info,
11367 struct bfd_link_order *link_order,
11368 bfd_byte *data,
11369 bfd_boolean relocatable,
11370 asymbol **symbols)
b49e97c9
TS
11371{
11372 /* Get enough memory to hold the stuff */
11373 bfd *input_bfd = link_order->u.indirect.section->owner;
11374 asection *input_section = link_order->u.indirect.section;
eea6121a 11375 bfd_size_type sz;
b49e97c9
TS
11376
11377 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11378 arelent **reloc_vector = NULL;
11379 long reloc_count;
11380
11381 if (reloc_size < 0)
11382 goto error_return;
11383
9719ad41 11384 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
11385 if (reloc_vector == NULL && reloc_size != 0)
11386 goto error_return;
11387
11388 /* read in the section */
eea6121a
AM
11389 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11390 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
11391 goto error_return;
11392
b49e97c9
TS
11393 reloc_count = bfd_canonicalize_reloc (input_bfd,
11394 input_section,
11395 reloc_vector,
11396 symbols);
11397 if (reloc_count < 0)
11398 goto error_return;
11399
11400 if (reloc_count > 0)
11401 {
11402 arelent **parent;
11403 /* for mips */
11404 int gp_found;
11405 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11406
11407 {
11408 struct bfd_hash_entry *h;
11409 struct bfd_link_hash_entry *lh;
11410 /* Skip all this stuff if we aren't mixing formats. */
11411 if (abfd && input_bfd
11412 && abfd->xvec == input_bfd->xvec)
11413 lh = 0;
11414 else
11415 {
b34976b6 11416 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
11417 lh = (struct bfd_link_hash_entry *) h;
11418 }
11419 lookup:
11420 if (lh)
11421 {
11422 switch (lh->type)
11423 {
11424 case bfd_link_hash_undefined:
11425 case bfd_link_hash_undefweak:
11426 case bfd_link_hash_common:
11427 gp_found = 0;
11428 break;
11429 case bfd_link_hash_defined:
11430 case bfd_link_hash_defweak:
11431 gp_found = 1;
11432 gp = lh->u.def.value;
11433 break;
11434 case bfd_link_hash_indirect:
11435 case bfd_link_hash_warning:
11436 lh = lh->u.i.link;
11437 /* @@FIXME ignoring warning for now */
11438 goto lookup;
11439 case bfd_link_hash_new:
11440 default:
11441 abort ();
11442 }
11443 }
11444 else
11445 gp_found = 0;
11446 }
11447 /* end mips */
9719ad41 11448 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 11449 {
9719ad41 11450 char *error_message = NULL;
b49e97c9
TS
11451 bfd_reloc_status_type r;
11452
11453 /* Specific to MIPS: Deal with relocation types that require
11454 knowing the gp of the output bfd. */
11455 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 11456
8236346f
EC
11457 /* If we've managed to find the gp and have a special
11458 function for the relocation then go ahead, else default
11459 to the generic handling. */
11460 if (gp_found
11461 && (*parent)->howto->special_function
11462 == _bfd_mips_elf32_gprel16_reloc)
11463 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11464 input_section, relocatable,
11465 data, gp);
11466 else
86324f90 11467 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
11468 input_section,
11469 relocatable ? abfd : NULL,
11470 &error_message);
b49e97c9 11471
1049f94e 11472 if (relocatable)
b49e97c9
TS
11473 {
11474 asection *os = input_section->output_section;
11475
11476 /* A partial link, so keep the relocs */
11477 os->orelocation[os->reloc_count] = *parent;
11478 os->reloc_count++;
11479 }
11480
11481 if (r != bfd_reloc_ok)
11482 {
11483 switch (r)
11484 {
11485 case bfd_reloc_undefined:
11486 if (!((*link_info->callbacks->undefined_symbol)
11487 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 11488 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
11489 goto error_return;
11490 break;
11491 case bfd_reloc_dangerous:
9719ad41 11492 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
11493 if (!((*link_info->callbacks->reloc_dangerous)
11494 (link_info, error_message, input_bfd, input_section,
11495 (*parent)->address)))
11496 goto error_return;
11497 break;
11498 case bfd_reloc_overflow:
11499 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
11500 (link_info, NULL,
11501 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
11502 (*parent)->howto->name, (*parent)->addend,
11503 input_bfd, input_section, (*parent)->address)))
11504 goto error_return;
11505 break;
11506 case bfd_reloc_outofrange:
11507 default:
11508 abort ();
11509 break;
11510 }
11511
11512 }
11513 }
11514 }
11515 if (reloc_vector != NULL)
11516 free (reloc_vector);
11517 return data;
11518
11519error_return:
11520 if (reloc_vector != NULL)
11521 free (reloc_vector);
11522 return NULL;
11523}
11524\f
d5eaccd7
RS
11525/* Allocate ABFD's target-dependent data. */
11526
11527bfd_boolean
11528_bfd_mips_elf_mkobject (bfd *abfd)
11529{
11530 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
4dfe6ac6 11531 MIPS_ELF_DATA);
d5eaccd7
RS
11532}
11533
b49e97c9
TS
11534/* Create a MIPS ELF linker hash table. */
11535
11536struct bfd_link_hash_table *
9719ad41 11537_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
11538{
11539 struct mips_elf_link_hash_table *ret;
11540 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11541
9719ad41
RS
11542 ret = bfd_malloc (amt);
11543 if (ret == NULL)
b49e97c9
TS
11544 return NULL;
11545
66eb6687
AM
11546 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11547 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
11548 sizeof (struct mips_elf_link_hash_entry),
11549 MIPS_ELF_DATA))
b49e97c9 11550 {
e2d34d7d 11551 free (ret);
b49e97c9
TS
11552 return NULL;
11553 }
11554
11555#if 0
11556 /* We no longer use this. */
11557 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11558 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11559#endif
11560 ret->procedure_count = 0;
11561 ret->compact_rel_size = 0;
b34976b6 11562 ret->use_rld_obj_head = FALSE;
b49e97c9 11563 ret->rld_value = 0;
b34976b6 11564 ret->mips16_stubs_seen = FALSE;
861fb55a 11565 ret->use_plts_and_copy_relocs = FALSE;
0a44bf69 11566 ret->is_vxworks = FALSE;
0e53d9da 11567 ret->small_data_overflow_reported = FALSE;
0a44bf69
RS
11568 ret->srelbss = NULL;
11569 ret->sdynbss = NULL;
11570 ret->srelplt = NULL;
11571 ret->srelplt2 = NULL;
11572 ret->sgotplt = NULL;
11573 ret->splt = NULL;
4e41d0d7 11574 ret->sstubs = NULL;
a8028dd0
RS
11575 ret->sgot = NULL;
11576 ret->got_info = NULL;
0a44bf69
RS
11577 ret->plt_header_size = 0;
11578 ret->plt_entry_size = 0;
33bb52fb 11579 ret->lazy_stub_count = 0;
5108fc1b 11580 ret->function_stub_size = 0;
861fb55a
DJ
11581 ret->strampoline = NULL;
11582 ret->la25_stubs = NULL;
11583 ret->add_stub_section = NULL;
b49e97c9
TS
11584
11585 return &ret->root.root;
11586}
0a44bf69
RS
11587
11588/* Likewise, but indicate that the target is VxWorks. */
11589
11590struct bfd_link_hash_table *
11591_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11592{
11593 struct bfd_link_hash_table *ret;
11594
11595 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11596 if (ret)
11597 {
11598 struct mips_elf_link_hash_table *htab;
11599
11600 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
11601 htab->use_plts_and_copy_relocs = TRUE;
11602 htab->is_vxworks = TRUE;
0a44bf69
RS
11603 }
11604 return ret;
11605}
861fb55a
DJ
11606
11607/* A function that the linker calls if we are allowed to use PLTs
11608 and copy relocs. */
11609
11610void
11611_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11612{
11613 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11614}
b49e97c9
TS
11615\f
11616/* We need to use a special link routine to handle the .reginfo and
11617 the .mdebug sections. We need to merge all instances of these
11618 sections together, not write them all out sequentially. */
11619
b34976b6 11620bfd_boolean
9719ad41 11621_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 11622{
b49e97c9
TS
11623 asection *o;
11624 struct bfd_link_order *p;
11625 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11626 asection *rtproc_sec;
11627 Elf32_RegInfo reginfo;
11628 struct ecoff_debug_info debug;
861fb55a 11629 struct mips_htab_traverse_info hti;
7a2a6943
NC
11630 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11631 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 11632 HDRR *symhdr = &debug.symbolic_header;
9719ad41 11633 void *mdebug_handle = NULL;
b49e97c9
TS
11634 asection *s;
11635 EXTR esym;
11636 unsigned int i;
11637 bfd_size_type amt;
0a44bf69 11638 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
11639
11640 static const char * const secname[] =
11641 {
11642 ".text", ".init", ".fini", ".data",
11643 ".rodata", ".sdata", ".sbss", ".bss"
11644 };
11645 static const int sc[] =
11646 {
11647 scText, scInit, scFini, scData,
11648 scRData, scSData, scSBss, scBss
11649 };
11650
d4596a51
RS
11651 /* Sort the dynamic symbols so that those with GOT entries come after
11652 those without. */
0a44bf69 11653 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11654 BFD_ASSERT (htab != NULL);
11655
d4596a51
RS
11656 if (!mips_elf_sort_hash_table (abfd, info))
11657 return FALSE;
b49e97c9 11658
861fb55a
DJ
11659 /* Create any scheduled LA25 stubs. */
11660 hti.info = info;
11661 hti.output_bfd = abfd;
11662 hti.error = FALSE;
11663 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11664 if (hti.error)
11665 return FALSE;
11666
b49e97c9
TS
11667 /* Get a value for the GP register. */
11668 if (elf_gp (abfd) == 0)
11669 {
11670 struct bfd_link_hash_entry *h;
11671
b34976b6 11672 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 11673 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
11674 elf_gp (abfd) = (h->u.def.value
11675 + h->u.def.section->output_section->vma
11676 + h->u.def.section->output_offset);
0a44bf69
RS
11677 else if (htab->is_vxworks
11678 && (h = bfd_link_hash_lookup (info->hash,
11679 "_GLOBAL_OFFSET_TABLE_",
11680 FALSE, FALSE, TRUE))
11681 && h->type == bfd_link_hash_defined)
11682 elf_gp (abfd) = (h->u.def.section->output_section->vma
11683 + h->u.def.section->output_offset
11684 + h->u.def.value);
1049f94e 11685 else if (info->relocatable)
b49e97c9
TS
11686 {
11687 bfd_vma lo = MINUS_ONE;
11688
11689 /* Find the GP-relative section with the lowest offset. */
9719ad41 11690 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11691 if (o->vma < lo
11692 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11693 lo = o->vma;
11694
11695 /* And calculate GP relative to that. */
0a44bf69 11696 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
11697 }
11698 else
11699 {
11700 /* If the relocate_section function needs to do a reloc
11701 involving the GP value, it should make a reloc_dangerous
11702 callback to warn that GP is not defined. */
11703 }
11704 }
11705
11706 /* Go through the sections and collect the .reginfo and .mdebug
11707 information. */
11708 reginfo_sec = NULL;
11709 mdebug_sec = NULL;
11710 gptab_data_sec = NULL;
11711 gptab_bss_sec = NULL;
9719ad41 11712 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11713 {
11714 if (strcmp (o->name, ".reginfo") == 0)
11715 {
11716 memset (&reginfo, 0, sizeof reginfo);
11717
11718 /* We have found the .reginfo section in the output file.
11719 Look through all the link_orders comprising it and merge
11720 the information together. */
8423293d 11721 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11722 {
11723 asection *input_section;
11724 bfd *input_bfd;
11725 Elf32_External_RegInfo ext;
11726 Elf32_RegInfo sub;
11727
11728 if (p->type != bfd_indirect_link_order)
11729 {
11730 if (p->type == bfd_data_link_order)
11731 continue;
11732 abort ();
11733 }
11734
11735 input_section = p->u.indirect.section;
11736 input_bfd = input_section->owner;
11737
b49e97c9 11738 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 11739 &ext, 0, sizeof ext))
b34976b6 11740 return FALSE;
b49e97c9
TS
11741
11742 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11743
11744 reginfo.ri_gprmask |= sub.ri_gprmask;
11745 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11746 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11747 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11748 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11749
11750 /* ri_gp_value is set by the function
11751 mips_elf32_section_processing when the section is
11752 finally written out. */
11753
11754 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11755 elf_link_input_bfd ignores this section. */
11756 input_section->flags &= ~SEC_HAS_CONTENTS;
11757 }
11758
11759 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 11760 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
11761
11762 /* Skip this section later on (I don't think this currently
11763 matters, but someday it might). */
8423293d 11764 o->map_head.link_order = NULL;
b49e97c9
TS
11765
11766 reginfo_sec = o;
11767 }
11768
11769 if (strcmp (o->name, ".mdebug") == 0)
11770 {
11771 struct extsym_info einfo;
11772 bfd_vma last;
11773
11774 /* We have found the .mdebug section in the output file.
11775 Look through all the link_orders comprising it and merge
11776 the information together. */
11777 symhdr->magic = swap->sym_magic;
11778 /* FIXME: What should the version stamp be? */
11779 symhdr->vstamp = 0;
11780 symhdr->ilineMax = 0;
11781 symhdr->cbLine = 0;
11782 symhdr->idnMax = 0;
11783 symhdr->ipdMax = 0;
11784 symhdr->isymMax = 0;
11785 symhdr->ioptMax = 0;
11786 symhdr->iauxMax = 0;
11787 symhdr->issMax = 0;
11788 symhdr->issExtMax = 0;
11789 symhdr->ifdMax = 0;
11790 symhdr->crfd = 0;
11791 symhdr->iextMax = 0;
11792
11793 /* We accumulate the debugging information itself in the
11794 debug_info structure. */
11795 debug.line = NULL;
11796 debug.external_dnr = NULL;
11797 debug.external_pdr = NULL;
11798 debug.external_sym = NULL;
11799 debug.external_opt = NULL;
11800 debug.external_aux = NULL;
11801 debug.ss = NULL;
11802 debug.ssext = debug.ssext_end = NULL;
11803 debug.external_fdr = NULL;
11804 debug.external_rfd = NULL;
11805 debug.external_ext = debug.external_ext_end = NULL;
11806
11807 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 11808 if (mdebug_handle == NULL)
b34976b6 11809 return FALSE;
b49e97c9
TS
11810
11811 esym.jmptbl = 0;
11812 esym.cobol_main = 0;
11813 esym.weakext = 0;
11814 esym.reserved = 0;
11815 esym.ifd = ifdNil;
11816 esym.asym.iss = issNil;
11817 esym.asym.st = stLocal;
11818 esym.asym.reserved = 0;
11819 esym.asym.index = indexNil;
11820 last = 0;
11821 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11822 {
11823 esym.asym.sc = sc[i];
11824 s = bfd_get_section_by_name (abfd, secname[i]);
11825 if (s != NULL)
11826 {
11827 esym.asym.value = s->vma;
eea6121a 11828 last = s->vma + s->size;
b49e97c9
TS
11829 }
11830 else
11831 esym.asym.value = last;
11832 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11833 secname[i], &esym))
b34976b6 11834 return FALSE;
b49e97c9
TS
11835 }
11836
8423293d 11837 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11838 {
11839 asection *input_section;
11840 bfd *input_bfd;
11841 const struct ecoff_debug_swap *input_swap;
11842 struct ecoff_debug_info input_debug;
11843 char *eraw_src;
11844 char *eraw_end;
11845
11846 if (p->type != bfd_indirect_link_order)
11847 {
11848 if (p->type == bfd_data_link_order)
11849 continue;
11850 abort ();
11851 }
11852
11853 input_section = p->u.indirect.section;
11854 input_bfd = input_section->owner;
11855
d5eaccd7 11856 if (!is_mips_elf (input_bfd))
b49e97c9
TS
11857 {
11858 /* I don't know what a non MIPS ELF bfd would be
11859 doing with a .mdebug section, but I don't really
11860 want to deal with it. */
11861 continue;
11862 }
11863
11864 input_swap = (get_elf_backend_data (input_bfd)
11865 ->elf_backend_ecoff_debug_swap);
11866
eea6121a 11867 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
11868
11869 /* The ECOFF linking code expects that we have already
11870 read in the debugging information and set up an
11871 ecoff_debug_info structure, so we do that now. */
11872 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11873 &input_debug))
b34976b6 11874 return FALSE;
b49e97c9
TS
11875
11876 if (! (bfd_ecoff_debug_accumulate
11877 (mdebug_handle, abfd, &debug, swap, input_bfd,
11878 &input_debug, input_swap, info)))
b34976b6 11879 return FALSE;
b49e97c9
TS
11880
11881 /* Loop through the external symbols. For each one with
11882 interesting information, try to find the symbol in
11883 the linker global hash table and save the information
11884 for the output external symbols. */
11885 eraw_src = input_debug.external_ext;
11886 eraw_end = (eraw_src
11887 + (input_debug.symbolic_header.iextMax
11888 * input_swap->external_ext_size));
11889 for (;
11890 eraw_src < eraw_end;
11891 eraw_src += input_swap->external_ext_size)
11892 {
11893 EXTR ext;
11894 const char *name;
11895 struct mips_elf_link_hash_entry *h;
11896
9719ad41 11897 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
11898 if (ext.asym.sc == scNil
11899 || ext.asym.sc == scUndefined
11900 || ext.asym.sc == scSUndefined)
11901 continue;
11902
11903 name = input_debug.ssext + ext.asym.iss;
11904 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 11905 name, FALSE, FALSE, TRUE);
b49e97c9
TS
11906 if (h == NULL || h->esym.ifd != -2)
11907 continue;
11908
11909 if (ext.ifd != -1)
11910 {
11911 BFD_ASSERT (ext.ifd
11912 < input_debug.symbolic_header.ifdMax);
11913 ext.ifd = input_debug.ifdmap[ext.ifd];
11914 }
11915
11916 h->esym = ext;
11917 }
11918
11919 /* Free up the information we just read. */
11920 free (input_debug.line);
11921 free (input_debug.external_dnr);
11922 free (input_debug.external_pdr);
11923 free (input_debug.external_sym);
11924 free (input_debug.external_opt);
11925 free (input_debug.external_aux);
11926 free (input_debug.ss);
11927 free (input_debug.ssext);
11928 free (input_debug.external_fdr);
11929 free (input_debug.external_rfd);
11930 free (input_debug.external_ext);
11931
11932 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11933 elf_link_input_bfd ignores this section. */
11934 input_section->flags &= ~SEC_HAS_CONTENTS;
11935 }
11936
11937 if (SGI_COMPAT (abfd) && info->shared)
11938 {
11939 /* Create .rtproc section. */
11940 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11941 if (rtproc_sec == NULL)
11942 {
11943 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11944 | SEC_LINKER_CREATED | SEC_READONLY);
11945
3496cb2a
L
11946 rtproc_sec = bfd_make_section_with_flags (abfd,
11947 ".rtproc",
11948 flags);
b49e97c9 11949 if (rtproc_sec == NULL
b49e97c9 11950 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 11951 return FALSE;
b49e97c9
TS
11952 }
11953
11954 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11955 info, rtproc_sec,
11956 &debug))
b34976b6 11957 return FALSE;
b49e97c9
TS
11958 }
11959
11960 /* Build the external symbol information. */
11961 einfo.abfd = abfd;
11962 einfo.info = info;
11963 einfo.debug = &debug;
11964 einfo.swap = swap;
b34976b6 11965 einfo.failed = FALSE;
b49e97c9 11966 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 11967 mips_elf_output_extsym, &einfo);
b49e97c9 11968 if (einfo.failed)
b34976b6 11969 return FALSE;
b49e97c9
TS
11970
11971 /* Set the size of the .mdebug section. */
eea6121a 11972 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
11973
11974 /* Skip this section later on (I don't think this currently
11975 matters, but someday it might). */
8423293d 11976 o->map_head.link_order = NULL;
b49e97c9
TS
11977
11978 mdebug_sec = o;
11979 }
11980
0112cd26 11981 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
11982 {
11983 const char *subname;
11984 unsigned int c;
11985 Elf32_gptab *tab;
11986 Elf32_External_gptab *ext_tab;
11987 unsigned int j;
11988
11989 /* The .gptab.sdata and .gptab.sbss sections hold
11990 information describing how the small data area would
11991 change depending upon the -G switch. These sections
11992 not used in executables files. */
1049f94e 11993 if (! info->relocatable)
b49e97c9 11994 {
8423293d 11995 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11996 {
11997 asection *input_section;
11998
11999 if (p->type != bfd_indirect_link_order)
12000 {
12001 if (p->type == bfd_data_link_order)
12002 continue;
12003 abort ();
12004 }
12005
12006 input_section = p->u.indirect.section;
12007
12008 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12009 elf_link_input_bfd ignores this section. */
12010 input_section->flags &= ~SEC_HAS_CONTENTS;
12011 }
12012
12013 /* Skip this section later on (I don't think this
12014 currently matters, but someday it might). */
8423293d 12015 o->map_head.link_order = NULL;
b49e97c9
TS
12016
12017 /* Really remove the section. */
5daa8fe7 12018 bfd_section_list_remove (abfd, o);
b49e97c9
TS
12019 --abfd->section_count;
12020
12021 continue;
12022 }
12023
12024 /* There is one gptab for initialized data, and one for
12025 uninitialized data. */
12026 if (strcmp (o->name, ".gptab.sdata") == 0)
12027 gptab_data_sec = o;
12028 else if (strcmp (o->name, ".gptab.sbss") == 0)
12029 gptab_bss_sec = o;
12030 else
12031 {
12032 (*_bfd_error_handler)
12033 (_("%s: illegal section name `%s'"),
12034 bfd_get_filename (abfd), o->name);
12035 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 12036 return FALSE;
b49e97c9
TS
12037 }
12038
12039 /* The linker script always combines .gptab.data and
12040 .gptab.sdata into .gptab.sdata, and likewise for
12041 .gptab.bss and .gptab.sbss. It is possible that there is
12042 no .sdata or .sbss section in the output file, in which
12043 case we must change the name of the output section. */
12044 subname = o->name + sizeof ".gptab" - 1;
12045 if (bfd_get_section_by_name (abfd, subname) == NULL)
12046 {
12047 if (o == gptab_data_sec)
12048 o->name = ".gptab.data";
12049 else
12050 o->name = ".gptab.bss";
12051 subname = o->name + sizeof ".gptab" - 1;
12052 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
12053 }
12054
12055 /* Set up the first entry. */
12056 c = 1;
12057 amt = c * sizeof (Elf32_gptab);
9719ad41 12058 tab = bfd_malloc (amt);
b49e97c9 12059 if (tab == NULL)
b34976b6 12060 return FALSE;
b49e97c9
TS
12061 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
12062 tab[0].gt_header.gt_unused = 0;
12063
12064 /* Combine the input sections. */
8423293d 12065 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
12066 {
12067 asection *input_section;
12068 bfd *input_bfd;
12069 bfd_size_type size;
12070 unsigned long last;
12071 bfd_size_type gpentry;
12072
12073 if (p->type != bfd_indirect_link_order)
12074 {
12075 if (p->type == bfd_data_link_order)
12076 continue;
12077 abort ();
12078 }
12079
12080 input_section = p->u.indirect.section;
12081 input_bfd = input_section->owner;
12082
12083 /* Combine the gptab entries for this input section one
12084 by one. We know that the input gptab entries are
12085 sorted by ascending -G value. */
eea6121a 12086 size = input_section->size;
b49e97c9
TS
12087 last = 0;
12088 for (gpentry = sizeof (Elf32_External_gptab);
12089 gpentry < size;
12090 gpentry += sizeof (Elf32_External_gptab))
12091 {
12092 Elf32_External_gptab ext_gptab;
12093 Elf32_gptab int_gptab;
12094 unsigned long val;
12095 unsigned long add;
b34976b6 12096 bfd_boolean exact;
b49e97c9
TS
12097 unsigned int look;
12098
12099 if (! (bfd_get_section_contents
9719ad41
RS
12100 (input_bfd, input_section, &ext_gptab, gpentry,
12101 sizeof (Elf32_External_gptab))))
b49e97c9
TS
12102 {
12103 free (tab);
b34976b6 12104 return FALSE;
b49e97c9
TS
12105 }
12106
12107 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12108 &int_gptab);
12109 val = int_gptab.gt_entry.gt_g_value;
12110 add = int_gptab.gt_entry.gt_bytes - last;
12111
b34976b6 12112 exact = FALSE;
b49e97c9
TS
12113 for (look = 1; look < c; look++)
12114 {
12115 if (tab[look].gt_entry.gt_g_value >= val)
12116 tab[look].gt_entry.gt_bytes += add;
12117
12118 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 12119 exact = TRUE;
b49e97c9
TS
12120 }
12121
12122 if (! exact)
12123 {
12124 Elf32_gptab *new_tab;
12125 unsigned int max;
12126
12127 /* We need a new table entry. */
12128 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 12129 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
12130 if (new_tab == NULL)
12131 {
12132 free (tab);
b34976b6 12133 return FALSE;
b49e97c9
TS
12134 }
12135 tab = new_tab;
12136 tab[c].gt_entry.gt_g_value = val;
12137 tab[c].gt_entry.gt_bytes = add;
12138
12139 /* Merge in the size for the next smallest -G
12140 value, since that will be implied by this new
12141 value. */
12142 max = 0;
12143 for (look = 1; look < c; look++)
12144 {
12145 if (tab[look].gt_entry.gt_g_value < val
12146 && (max == 0
12147 || (tab[look].gt_entry.gt_g_value
12148 > tab[max].gt_entry.gt_g_value)))
12149 max = look;
12150 }
12151 if (max != 0)
12152 tab[c].gt_entry.gt_bytes +=
12153 tab[max].gt_entry.gt_bytes;
12154
12155 ++c;
12156 }
12157
12158 last = int_gptab.gt_entry.gt_bytes;
12159 }
12160
12161 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12162 elf_link_input_bfd ignores this section. */
12163 input_section->flags &= ~SEC_HAS_CONTENTS;
12164 }
12165
12166 /* The table must be sorted by -G value. */
12167 if (c > 2)
12168 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12169
12170 /* Swap out the table. */
12171 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 12172 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
12173 if (ext_tab == NULL)
12174 {
12175 free (tab);
b34976b6 12176 return FALSE;
b49e97c9
TS
12177 }
12178
12179 for (j = 0; j < c; j++)
12180 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12181 free (tab);
12182
eea6121a 12183 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
12184 o->contents = (bfd_byte *) ext_tab;
12185
12186 /* Skip this section later on (I don't think this currently
12187 matters, but someday it might). */
8423293d 12188 o->map_head.link_order = NULL;
b49e97c9
TS
12189 }
12190 }
12191
12192 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 12193 if (!bfd_elf_final_link (abfd, info))
b34976b6 12194 return FALSE;
b49e97c9
TS
12195
12196 /* Now write out the computed sections. */
12197
9719ad41 12198 if (reginfo_sec != NULL)
b49e97c9
TS
12199 {
12200 Elf32_External_RegInfo ext;
12201
12202 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 12203 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 12204 return FALSE;
b49e97c9
TS
12205 }
12206
9719ad41 12207 if (mdebug_sec != NULL)
b49e97c9
TS
12208 {
12209 BFD_ASSERT (abfd->output_has_begun);
12210 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12211 swap, info,
12212 mdebug_sec->filepos))
b34976b6 12213 return FALSE;
b49e97c9
TS
12214
12215 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12216 }
12217
9719ad41 12218 if (gptab_data_sec != NULL)
b49e97c9
TS
12219 {
12220 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12221 gptab_data_sec->contents,
eea6121a 12222 0, gptab_data_sec->size))
b34976b6 12223 return FALSE;
b49e97c9
TS
12224 }
12225
9719ad41 12226 if (gptab_bss_sec != NULL)
b49e97c9
TS
12227 {
12228 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12229 gptab_bss_sec->contents,
eea6121a 12230 0, gptab_bss_sec->size))
b34976b6 12231 return FALSE;
b49e97c9
TS
12232 }
12233
12234 if (SGI_COMPAT (abfd))
12235 {
12236 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12237 if (rtproc_sec != NULL)
12238 {
12239 if (! bfd_set_section_contents (abfd, rtproc_sec,
12240 rtproc_sec->contents,
eea6121a 12241 0, rtproc_sec->size))
b34976b6 12242 return FALSE;
b49e97c9
TS
12243 }
12244 }
12245
b34976b6 12246 return TRUE;
b49e97c9
TS
12247}
12248\f
64543e1a
RS
12249/* Structure for saying that BFD machine EXTENSION extends BASE. */
12250
12251struct mips_mach_extension {
12252 unsigned long extension, base;
12253};
12254
12255
12256/* An array describing how BFD machines relate to one another. The entries
12257 are ordered topologically with MIPS I extensions listed last. */
12258
12259static const struct mips_mach_extension mips_mach_extensions[] = {
6f179bd0
AN
12260 /* MIPS64r2 extensions. */
12261 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12262
64543e1a 12263 /* MIPS64 extensions. */
5f74bc13 12264 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 12265 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 12266 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
64543e1a
RS
12267
12268 /* MIPS V extensions. */
12269 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12270
12271 /* R10000 extensions. */
12272 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
12273 { bfd_mach_mips14000, bfd_mach_mips10000 },
12274 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
12275
12276 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12277 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12278 better to allow vr5400 and vr5500 code to be merged anyway, since
12279 many libraries will just use the core ISA. Perhaps we could add
12280 some sort of ASE flag if this ever proves a problem. */
12281 { bfd_mach_mips5500, bfd_mach_mips5400 },
12282 { bfd_mach_mips5400, bfd_mach_mips5000 },
12283
12284 /* MIPS IV extensions. */
12285 { bfd_mach_mips5, bfd_mach_mips8000 },
12286 { bfd_mach_mips10000, bfd_mach_mips8000 },
12287 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 12288 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 12289 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
12290
12291 /* VR4100 extensions. */
12292 { bfd_mach_mips4120, bfd_mach_mips4100 },
12293 { bfd_mach_mips4111, bfd_mach_mips4100 },
12294
12295 /* MIPS III extensions. */
350cc38d
MS
12296 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12297 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
12298 { bfd_mach_mips8000, bfd_mach_mips4000 },
12299 { bfd_mach_mips4650, bfd_mach_mips4000 },
12300 { bfd_mach_mips4600, bfd_mach_mips4000 },
12301 { bfd_mach_mips4400, bfd_mach_mips4000 },
12302 { bfd_mach_mips4300, bfd_mach_mips4000 },
12303 { bfd_mach_mips4100, bfd_mach_mips4000 },
12304 { bfd_mach_mips4010, bfd_mach_mips4000 },
12305
12306 /* MIPS32 extensions. */
12307 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12308
12309 /* MIPS II extensions. */
12310 { bfd_mach_mips4000, bfd_mach_mips6000 },
12311 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12312
12313 /* MIPS I extensions. */
12314 { bfd_mach_mips6000, bfd_mach_mips3000 },
12315 { bfd_mach_mips3900, bfd_mach_mips3000 }
12316};
12317
12318
12319/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12320
12321static bfd_boolean
9719ad41 12322mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
12323{
12324 size_t i;
12325
c5211a54
RS
12326 if (extension == base)
12327 return TRUE;
12328
12329 if (base == bfd_mach_mipsisa32
12330 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12331 return TRUE;
12332
12333 if (base == bfd_mach_mipsisa32r2
12334 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12335 return TRUE;
12336
12337 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 12338 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
12339 {
12340 extension = mips_mach_extensions[i].base;
12341 if (extension == base)
12342 return TRUE;
12343 }
64543e1a 12344
c5211a54 12345 return FALSE;
64543e1a
RS
12346}
12347
12348
12349/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 12350
b34976b6 12351static bfd_boolean
9719ad41 12352mips_32bit_flags_p (flagword flags)
00707a0e 12353{
64543e1a
RS
12354 return ((flags & EF_MIPS_32BITMODE) != 0
12355 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12356 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12357 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12358 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12359 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12360 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
12361}
12362
64543e1a 12363
2cf19d5c
JM
12364/* Merge object attributes from IBFD into OBFD. Raise an error if
12365 there are conflicting attributes. */
12366static bfd_boolean
12367mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12368{
12369 obj_attribute *in_attr;
12370 obj_attribute *out_attr;
12371
12372 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12373 {
12374 /* This is the first object. Copy the attributes. */
12375 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12376
12377 /* Use the Tag_null value to indicate the attributes have been
12378 initialized. */
12379 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12380
12381 return TRUE;
12382 }
12383
12384 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12385 non-conflicting ones. */
12386 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12387 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12388 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12389 {
12390 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12391 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12392 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12393 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12394 ;
42554f6a 12395 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12396 _bfd_error_handler
12397 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12398 in_attr[Tag_GNU_MIPS_ABI_FP].i);
42554f6a 12399 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12400 _bfd_error_handler
12401 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12402 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12403 else
12404 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12405 {
12406 case 1:
12407 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12408 {
12409 case 2:
12410 _bfd_error_handler
12411 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12412 obfd, ibfd);
51a0dd31 12413 break;
2cf19d5c
JM
12414
12415 case 3:
12416 _bfd_error_handler
12417 (_("Warning: %B uses hard float, %B uses soft float"),
12418 obfd, ibfd);
12419 break;
12420
42554f6a
TS
12421 case 4:
12422 _bfd_error_handler
12423 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12424 obfd, ibfd);
12425 break;
12426
2cf19d5c
JM
12427 default:
12428 abort ();
12429 }
12430 break;
12431
12432 case 2:
12433 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12434 {
12435 case 1:
12436 _bfd_error_handler
12437 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12438 ibfd, obfd);
51a0dd31 12439 break;
2cf19d5c
JM
12440
12441 case 3:
12442 _bfd_error_handler
12443 (_("Warning: %B uses hard float, %B uses soft float"),
12444 obfd, ibfd);
12445 break;
12446
42554f6a
TS
12447 case 4:
12448 _bfd_error_handler
12449 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12450 obfd, ibfd);
12451 break;
12452
2cf19d5c
JM
12453 default:
12454 abort ();
12455 }
12456 break;
12457
12458 case 3:
12459 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12460 {
12461 case 1:
12462 case 2:
42554f6a 12463 case 4:
2cf19d5c
JM
12464 _bfd_error_handler
12465 (_("Warning: %B uses hard float, %B uses soft float"),
12466 ibfd, obfd);
12467 break;
12468
12469 default:
12470 abort ();
12471 }
12472 break;
12473
42554f6a
TS
12474 case 4:
12475 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12476 {
12477 case 1:
12478 _bfd_error_handler
12479 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12480 ibfd, obfd);
12481 break;
12482
12483 case 2:
12484 _bfd_error_handler
12485 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12486 ibfd, obfd);
12487 break;
12488
12489 case 3:
12490 _bfd_error_handler
12491 (_("Warning: %B uses hard float, %B uses soft float"),
12492 obfd, ibfd);
12493 break;
12494
12495 default:
12496 abort ();
12497 }
12498 break;
12499
2cf19d5c
JM
12500 default:
12501 abort ();
12502 }
12503 }
12504
12505 /* Merge Tag_compatibility attributes and any common GNU ones. */
12506 _bfd_elf_merge_object_attributes (ibfd, obfd);
12507
12508 return TRUE;
12509}
12510
b49e97c9
TS
12511/* Merge backend specific data from an object file to the output
12512 object file when linking. */
12513
b34976b6 12514bfd_boolean
9719ad41 12515_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
12516{
12517 flagword old_flags;
12518 flagword new_flags;
b34976b6
AM
12519 bfd_boolean ok;
12520 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
12521 asection *sec;
12522
12523 /* Check if we have the same endianess */
82e51918 12524 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
12525 {
12526 (*_bfd_error_handler)
d003868e
AM
12527 (_("%B: endianness incompatible with that of the selected emulation"),
12528 ibfd);
aa701218
AO
12529 return FALSE;
12530 }
b49e97c9 12531
d5eaccd7 12532 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 12533 return TRUE;
b49e97c9 12534
aa701218
AO
12535 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12536 {
12537 (*_bfd_error_handler)
d003868e
AM
12538 (_("%B: ABI is incompatible with that of the selected emulation"),
12539 ibfd);
aa701218
AO
12540 return FALSE;
12541 }
12542
2cf19d5c
JM
12543 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12544 return FALSE;
12545
b49e97c9
TS
12546 new_flags = elf_elfheader (ibfd)->e_flags;
12547 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12548 old_flags = elf_elfheader (obfd)->e_flags;
12549
12550 if (! elf_flags_init (obfd))
12551 {
b34976b6 12552 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
12553 elf_elfheader (obfd)->e_flags = new_flags;
12554 elf_elfheader (obfd)->e_ident[EI_CLASS]
12555 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12556
12557 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861
TS
12558 && (bfd_get_arch_info (obfd)->the_default
12559 || mips_mach_extends_p (bfd_get_mach (obfd),
12560 bfd_get_mach (ibfd))))
b49e97c9
TS
12561 {
12562 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12563 bfd_get_mach (ibfd)))
b34976b6 12564 return FALSE;
b49e97c9
TS
12565 }
12566
b34976b6 12567 return TRUE;
b49e97c9
TS
12568 }
12569
12570 /* Check flag compatibility. */
12571
12572 new_flags &= ~EF_MIPS_NOREORDER;
12573 old_flags &= ~EF_MIPS_NOREORDER;
12574
f4416af6
AO
12575 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12576 doesn't seem to matter. */
12577 new_flags &= ~EF_MIPS_XGOT;
12578 old_flags &= ~EF_MIPS_XGOT;
12579
98a8deaf
RS
12580 /* MIPSpro generates ucode info in n64 objects. Again, we should
12581 just be able to ignore this. */
12582 new_flags &= ~EF_MIPS_UCODE;
12583 old_flags &= ~EF_MIPS_UCODE;
12584
861fb55a
DJ
12585 /* DSOs should only be linked with CPIC code. */
12586 if ((ibfd->flags & DYNAMIC) != 0)
12587 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 12588
b49e97c9 12589 if (new_flags == old_flags)
b34976b6 12590 return TRUE;
b49e97c9
TS
12591
12592 /* Check to see if the input BFD actually contains any sections.
12593 If not, its flags may not have been initialised either, but it cannot
12594 actually cause any incompatibility. */
12595 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12596 {
12597 /* Ignore synthetic sections and empty .text, .data and .bss sections
12598 which are automatically generated by gas. */
12599 if (strcmp (sec->name, ".reginfo")
12600 && strcmp (sec->name, ".mdebug")
eea6121a 12601 && (sec->size != 0
d13d89fa
NS
12602 || (strcmp (sec->name, ".text")
12603 && strcmp (sec->name, ".data")
12604 && strcmp (sec->name, ".bss"))))
b49e97c9 12605 {
b34976b6 12606 null_input_bfd = FALSE;
b49e97c9
TS
12607 break;
12608 }
12609 }
12610 if (null_input_bfd)
b34976b6 12611 return TRUE;
b49e97c9 12612
b34976b6 12613 ok = TRUE;
b49e97c9 12614
143d77c5
EC
12615 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12616 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 12617 {
b49e97c9 12618 (*_bfd_error_handler)
861fb55a 12619 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 12620 ibfd);
143d77c5 12621 ok = TRUE;
b49e97c9
TS
12622 }
12623
143d77c5
EC
12624 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12625 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12626 if (! (new_flags & EF_MIPS_PIC))
12627 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12628
12629 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12630 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 12631
64543e1a
RS
12632 /* Compare the ISAs. */
12633 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 12634 {
64543e1a 12635 (*_bfd_error_handler)
d003868e
AM
12636 (_("%B: linking 32-bit code with 64-bit code"),
12637 ibfd);
64543e1a
RS
12638 ok = FALSE;
12639 }
12640 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12641 {
12642 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12643 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 12644 {
64543e1a
RS
12645 /* Copy the architecture info from IBFD to OBFD. Also copy
12646 the 32-bit flag (if set) so that we continue to recognise
12647 OBFD as a 32-bit binary. */
12648 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12649 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12650 elf_elfheader (obfd)->e_flags
12651 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12652
12653 /* Copy across the ABI flags if OBFD doesn't use them
12654 and if that was what caused us to treat IBFD as 32-bit. */
12655 if ((old_flags & EF_MIPS_ABI) == 0
12656 && mips_32bit_flags_p (new_flags)
12657 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12658 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
12659 }
12660 else
12661 {
64543e1a 12662 /* The ISAs aren't compatible. */
b49e97c9 12663 (*_bfd_error_handler)
d003868e
AM
12664 (_("%B: linking %s module with previous %s modules"),
12665 ibfd,
64543e1a
RS
12666 bfd_printable_name (ibfd),
12667 bfd_printable_name (obfd));
b34976b6 12668 ok = FALSE;
b49e97c9 12669 }
b49e97c9
TS
12670 }
12671
64543e1a
RS
12672 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12673 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12674
12675 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
12676 does set EI_CLASS differently from any 32-bit ABI. */
12677 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12678 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12679 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12680 {
12681 /* Only error if both are set (to different values). */
12682 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12683 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12684 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12685 {
12686 (*_bfd_error_handler)
d003868e
AM
12687 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12688 ibfd,
b49e97c9
TS
12689 elf_mips_abi_name (ibfd),
12690 elf_mips_abi_name (obfd));
b34976b6 12691 ok = FALSE;
b49e97c9
TS
12692 }
12693 new_flags &= ~EF_MIPS_ABI;
12694 old_flags &= ~EF_MIPS_ABI;
12695 }
12696
fb39dac1
RS
12697 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12698 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12699 {
12700 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12701
12702 new_flags &= ~ EF_MIPS_ARCH_ASE;
12703 old_flags &= ~ EF_MIPS_ARCH_ASE;
12704 }
12705
b49e97c9
TS
12706 /* Warn about any other mismatches */
12707 if (new_flags != old_flags)
12708 {
12709 (*_bfd_error_handler)
d003868e
AM
12710 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12711 ibfd, (unsigned long) new_flags,
b49e97c9 12712 (unsigned long) old_flags);
b34976b6 12713 ok = FALSE;
b49e97c9
TS
12714 }
12715
12716 if (! ok)
12717 {
12718 bfd_set_error (bfd_error_bad_value);
b34976b6 12719 return FALSE;
b49e97c9
TS
12720 }
12721
b34976b6 12722 return TRUE;
b49e97c9
TS
12723}
12724
12725/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12726
b34976b6 12727bfd_boolean
9719ad41 12728_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
12729{
12730 BFD_ASSERT (!elf_flags_init (abfd)
12731 || elf_elfheader (abfd)->e_flags == flags);
12732
12733 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
12734 elf_flags_init (abfd) = TRUE;
12735 return TRUE;
b49e97c9
TS
12736}
12737
ad9563d6
CM
12738char *
12739_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12740{
12741 switch (dtag)
12742 {
12743 default: return "";
12744 case DT_MIPS_RLD_VERSION:
12745 return "MIPS_RLD_VERSION";
12746 case DT_MIPS_TIME_STAMP:
12747 return "MIPS_TIME_STAMP";
12748 case DT_MIPS_ICHECKSUM:
12749 return "MIPS_ICHECKSUM";
12750 case DT_MIPS_IVERSION:
12751 return "MIPS_IVERSION";
12752 case DT_MIPS_FLAGS:
12753 return "MIPS_FLAGS";
12754 case DT_MIPS_BASE_ADDRESS:
12755 return "MIPS_BASE_ADDRESS";
12756 case DT_MIPS_MSYM:
12757 return "MIPS_MSYM";
12758 case DT_MIPS_CONFLICT:
12759 return "MIPS_CONFLICT";
12760 case DT_MIPS_LIBLIST:
12761 return "MIPS_LIBLIST";
12762 case DT_MIPS_LOCAL_GOTNO:
12763 return "MIPS_LOCAL_GOTNO";
12764 case DT_MIPS_CONFLICTNO:
12765 return "MIPS_CONFLICTNO";
12766 case DT_MIPS_LIBLISTNO:
12767 return "MIPS_LIBLISTNO";
12768 case DT_MIPS_SYMTABNO:
12769 return "MIPS_SYMTABNO";
12770 case DT_MIPS_UNREFEXTNO:
12771 return "MIPS_UNREFEXTNO";
12772 case DT_MIPS_GOTSYM:
12773 return "MIPS_GOTSYM";
12774 case DT_MIPS_HIPAGENO:
12775 return "MIPS_HIPAGENO";
12776 case DT_MIPS_RLD_MAP:
12777 return "MIPS_RLD_MAP";
12778 case DT_MIPS_DELTA_CLASS:
12779 return "MIPS_DELTA_CLASS";
12780 case DT_MIPS_DELTA_CLASS_NO:
12781 return "MIPS_DELTA_CLASS_NO";
12782 case DT_MIPS_DELTA_INSTANCE:
12783 return "MIPS_DELTA_INSTANCE";
12784 case DT_MIPS_DELTA_INSTANCE_NO:
12785 return "MIPS_DELTA_INSTANCE_NO";
12786 case DT_MIPS_DELTA_RELOC:
12787 return "MIPS_DELTA_RELOC";
12788 case DT_MIPS_DELTA_RELOC_NO:
12789 return "MIPS_DELTA_RELOC_NO";
12790 case DT_MIPS_DELTA_SYM:
12791 return "MIPS_DELTA_SYM";
12792 case DT_MIPS_DELTA_SYM_NO:
12793 return "MIPS_DELTA_SYM_NO";
12794 case DT_MIPS_DELTA_CLASSSYM:
12795 return "MIPS_DELTA_CLASSSYM";
12796 case DT_MIPS_DELTA_CLASSSYM_NO:
12797 return "MIPS_DELTA_CLASSSYM_NO";
12798 case DT_MIPS_CXX_FLAGS:
12799 return "MIPS_CXX_FLAGS";
12800 case DT_MIPS_PIXIE_INIT:
12801 return "MIPS_PIXIE_INIT";
12802 case DT_MIPS_SYMBOL_LIB:
12803 return "MIPS_SYMBOL_LIB";
12804 case DT_MIPS_LOCALPAGE_GOTIDX:
12805 return "MIPS_LOCALPAGE_GOTIDX";
12806 case DT_MIPS_LOCAL_GOTIDX:
12807 return "MIPS_LOCAL_GOTIDX";
12808 case DT_MIPS_HIDDEN_GOTIDX:
12809 return "MIPS_HIDDEN_GOTIDX";
12810 case DT_MIPS_PROTECTED_GOTIDX:
12811 return "MIPS_PROTECTED_GOT_IDX";
12812 case DT_MIPS_OPTIONS:
12813 return "MIPS_OPTIONS";
12814 case DT_MIPS_INTERFACE:
12815 return "MIPS_INTERFACE";
12816 case DT_MIPS_DYNSTR_ALIGN:
12817 return "DT_MIPS_DYNSTR_ALIGN";
12818 case DT_MIPS_INTERFACE_SIZE:
12819 return "DT_MIPS_INTERFACE_SIZE";
12820 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12821 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12822 case DT_MIPS_PERF_SUFFIX:
12823 return "DT_MIPS_PERF_SUFFIX";
12824 case DT_MIPS_COMPACT_SIZE:
12825 return "DT_MIPS_COMPACT_SIZE";
12826 case DT_MIPS_GP_VALUE:
12827 return "DT_MIPS_GP_VALUE";
12828 case DT_MIPS_AUX_DYNAMIC:
12829 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
12830 case DT_MIPS_PLTGOT:
12831 return "DT_MIPS_PLTGOT";
12832 case DT_MIPS_RWPLT:
12833 return "DT_MIPS_RWPLT";
ad9563d6
CM
12834 }
12835}
12836
b34976b6 12837bfd_boolean
9719ad41 12838_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 12839{
9719ad41 12840 FILE *file = ptr;
b49e97c9
TS
12841
12842 BFD_ASSERT (abfd != NULL && ptr != NULL);
12843
12844 /* Print normal ELF private data. */
12845 _bfd_elf_print_private_bfd_data (abfd, ptr);
12846
12847 /* xgettext:c-format */
12848 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12849
12850 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12851 fprintf (file, _(" [abi=O32]"));
12852 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12853 fprintf (file, _(" [abi=O64]"));
12854 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12855 fprintf (file, _(" [abi=EABI32]"));
12856 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12857 fprintf (file, _(" [abi=EABI64]"));
12858 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12859 fprintf (file, _(" [abi unknown]"));
12860 else if (ABI_N32_P (abfd))
12861 fprintf (file, _(" [abi=N32]"));
12862 else if (ABI_64_P (abfd))
12863 fprintf (file, _(" [abi=64]"));
12864 else
12865 fprintf (file, _(" [no abi set]"));
12866
12867 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 12868 fprintf (file, " [mips1]");
b49e97c9 12869 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 12870 fprintf (file, " [mips2]");
b49e97c9 12871 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 12872 fprintf (file, " [mips3]");
b49e97c9 12873 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 12874 fprintf (file, " [mips4]");
b49e97c9 12875 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 12876 fprintf (file, " [mips5]");
b49e97c9 12877 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 12878 fprintf (file, " [mips32]");
b49e97c9 12879 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 12880 fprintf (file, " [mips64]");
af7ee8bf 12881 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 12882 fprintf (file, " [mips32r2]");
5f74bc13 12883 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 12884 fprintf (file, " [mips64r2]");
b49e97c9
TS
12885 else
12886 fprintf (file, _(" [unknown ISA]"));
12887
40d32fc6 12888 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 12889 fprintf (file, " [mdmx]");
40d32fc6
CD
12890
12891 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 12892 fprintf (file, " [mips16]");
40d32fc6 12893
b49e97c9 12894 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 12895 fprintf (file, " [32bitmode]");
b49e97c9
TS
12896 else
12897 fprintf (file, _(" [not 32bitmode]"));
12898
c0e3f241 12899 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 12900 fprintf (file, " [noreorder]");
c0e3f241
CD
12901
12902 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 12903 fprintf (file, " [PIC]");
c0e3f241
CD
12904
12905 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 12906 fprintf (file, " [CPIC]");
c0e3f241
CD
12907
12908 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 12909 fprintf (file, " [XGOT]");
c0e3f241
CD
12910
12911 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 12912 fprintf (file, " [UCODE]");
c0e3f241 12913
b49e97c9
TS
12914 fputc ('\n', file);
12915
b34976b6 12916 return TRUE;
b49e97c9 12917}
2f89ff8d 12918
b35d266b 12919const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 12920{
0112cd26
NC
12921 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12922 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12923 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12924 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12925 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12926 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12927 { NULL, 0, 0, 0, 0 }
2f89ff8d 12928};
5e2b0d47 12929
8992f0d7
TS
12930/* Merge non visibility st_other attributes. Ensure that the
12931 STO_OPTIONAL flag is copied into h->other, even if this is not a
12932 definiton of the symbol. */
5e2b0d47
NC
12933void
12934_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12935 const Elf_Internal_Sym *isym,
12936 bfd_boolean definition,
12937 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12938{
8992f0d7
TS
12939 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12940 {
12941 unsigned char other;
12942
12943 other = (definition ? isym->st_other : h->other);
12944 other &= ~ELF_ST_VISIBILITY (-1);
12945 h->other = other | ELF_ST_VISIBILITY (h->other);
12946 }
12947
12948 if (!definition
5e2b0d47
NC
12949 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12950 h->other |= STO_OPTIONAL;
12951}
12ac1cf5
NC
12952
12953/* Decide whether an undefined symbol is special and can be ignored.
12954 This is the case for OPTIONAL symbols on IRIX. */
12955bfd_boolean
12956_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12957{
12958 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12959}
e0764319
NC
12960
12961bfd_boolean
12962_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12963{
12964 return (sym->st_shndx == SHN_COMMON
12965 || sym->st_shndx == SHN_MIPS_ACOMMON
12966 || sym->st_shndx == SHN_MIPS_SCOMMON);
12967}
861fb55a
DJ
12968
12969/* Return address for Ith PLT stub in section PLT, for relocation REL
12970 or (bfd_vma) -1 if it should not be included. */
12971
12972bfd_vma
12973_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12974 const arelent *rel ATTRIBUTE_UNUSED)
12975{
12976 return (plt->vma
12977 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12978 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12979}
12980
12981void
12982_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12983{
12984 struct mips_elf_link_hash_table *htab;
12985 Elf_Internal_Ehdr *i_ehdrp;
12986
12987 i_ehdrp = elf_elfheader (abfd);
12988 if (link_info)
12989 {
12990 htab = mips_elf_hash_table (link_info);
4dfe6ac6
NC
12991 BFD_ASSERT (htab != NULL);
12992
861fb55a
DJ
12993 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12994 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
12995 }
12996}
This page took 1.742274 seconds and 4 git commands to generate.