]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elfxx-mips.c
2010-01-13 Doug Kwan <dougkwan@google.com>
[thirdparty/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
64543e1a 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
aa820537 3 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
b49e97c9
TS
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
ae9a127f 12 This file is part of BFD, the Binary File Descriptor library.
b49e97c9 13
ae9a127f
NC
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
cd123cb7 16 the Free Software Foundation; either version 3 of the License, or
ae9a127f 17 (at your option) any later version.
b49e97c9 18
ae9a127f
NC
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
b49e97c9 23
ae9a127f
NC
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
cd123cb7
NC
26 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
27 MA 02110-1301, USA. */
28
b49e97c9
TS
29
30/* This file handles functionality common to the different MIPS ABI's. */
31
b49e97c9 32#include "sysdep.h"
3db64b00 33#include "bfd.h"
b49e97c9 34#include "libbfd.h"
64543e1a 35#include "libiberty.h"
b49e97c9
TS
36#include "elf-bfd.h"
37#include "elfxx-mips.h"
38#include "elf/mips.h"
0a44bf69 39#include "elf-vxworks.h"
b49e97c9
TS
40
41/* Get the ECOFF swapping routines. */
42#include "coff/sym.h"
43#include "coff/symconst.h"
44#include "coff/ecoff.h"
45#include "coff/mips.h"
46
b15e6682
AO
47#include "hashtab.h"
48
ead49a57
RS
49/* This structure is used to hold information about one GOT entry.
50 There are three types of entry:
51
52 (1) absolute addresses
53 (abfd == NULL)
54 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
55 (abfd != NULL, symndx >= 0)
56 (3) global and forced-local symbols
57 (abfd != NULL, symndx == -1)
58
59 Type (3) entries are treated differently for different types of GOT.
60 In the "master" GOT -- i.e. the one that describes every GOT
61 reference needed in the link -- the mips_got_entry is keyed on both
62 the symbol and the input bfd that references it. If it turns out
63 that we need multiple GOTs, we can then use this information to
64 create separate GOTs for each input bfd.
65
66 However, we want each of these separate GOTs to have at most one
67 entry for a given symbol, so their type (3) entries are keyed only
68 on the symbol. The input bfd given by the "abfd" field is somewhat
69 arbitrary in this case.
70
71 This means that when there are multiple GOTs, each GOT has a unique
72 mips_got_entry for every symbol within it. We can therefore use the
73 mips_got_entry fields (tls_type and gotidx) to track the symbol's
74 GOT index.
75
76 However, if it turns out that we need only a single GOT, we continue
77 to use the master GOT to describe it. There may therefore be several
78 mips_got_entries for the same symbol, each with a different input bfd.
79 We want to make sure that each symbol gets a unique GOT entry, so when
80 there's a single GOT, we use the symbol's hash entry, not the
81 mips_got_entry fields, to track a symbol's GOT index. */
b15e6682
AO
82struct mips_got_entry
83{
84 /* The input bfd in which the symbol is defined. */
85 bfd *abfd;
f4416af6
AO
86 /* The index of the symbol, as stored in the relocation r_info, if
87 we have a local symbol; -1 otherwise. */
88 long symndx;
89 union
90 {
91 /* If abfd == NULL, an address that must be stored in the got. */
92 bfd_vma address;
93 /* If abfd != NULL && symndx != -1, the addend of the relocation
94 that should be added to the symbol value. */
95 bfd_vma addend;
96 /* If abfd != NULL && symndx == -1, the hash table entry
97 corresponding to a global symbol in the got (or, local, if
98 h->forced_local). */
99 struct mips_elf_link_hash_entry *h;
100 } d;
0f20cc35
DJ
101
102 /* The TLS types included in this GOT entry (specifically, GD and
103 IE). The GD and IE flags can be added as we encounter new
104 relocations. LDM can also be set; it will always be alone, not
105 combined with any GD or IE flags. An LDM GOT entry will be
106 a local symbol entry with r_symndx == 0. */
107 unsigned char tls_type;
108
b15e6682 109 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
110 corresponding to this symbol+addend. If it's a global symbol
111 whose offset is yet to be decided, it's going to be -1. */
112 long gotidx;
b15e6682
AO
113};
114
c224138d
RS
115/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
116 The structures form a non-overlapping list that is sorted by increasing
117 MIN_ADDEND. */
118struct mips_got_page_range
119{
120 struct mips_got_page_range *next;
121 bfd_signed_vma min_addend;
122 bfd_signed_vma max_addend;
123};
124
125/* This structure describes the range of addends that are applied to page
126 relocations against a given symbol. */
127struct mips_got_page_entry
128{
129 /* The input bfd in which the symbol is defined. */
130 bfd *abfd;
131 /* The index of the symbol, as stored in the relocation r_info. */
132 long symndx;
133 /* The ranges for this page entry. */
134 struct mips_got_page_range *ranges;
135 /* The maximum number of page entries needed for RANGES. */
136 bfd_vma num_pages;
137};
138
f0abc2a1 139/* This structure is used to hold .got information when linking. */
b49e97c9
TS
140
141struct mips_got_info
142{
143 /* The global symbol in the GOT with the lowest index in the dynamic
144 symbol table. */
145 struct elf_link_hash_entry *global_gotsym;
146 /* The number of global .got entries. */
147 unsigned int global_gotno;
23cc69b6
RS
148 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
149 unsigned int reloc_only_gotno;
0f20cc35
DJ
150 /* The number of .got slots used for TLS. */
151 unsigned int tls_gotno;
152 /* The first unused TLS .got entry. Used only during
153 mips_elf_initialize_tls_index. */
154 unsigned int tls_assigned_gotno;
c224138d 155 /* The number of local .got entries, eventually including page entries. */
b49e97c9 156 unsigned int local_gotno;
c224138d
RS
157 /* The maximum number of page entries needed. */
158 unsigned int page_gotno;
b49e97c9
TS
159 /* The number of local .got entries we have used. */
160 unsigned int assigned_gotno;
b15e6682
AO
161 /* A hash table holding members of the got. */
162 struct htab *got_entries;
c224138d
RS
163 /* A hash table of mips_got_page_entry structures. */
164 struct htab *got_page_entries;
f4416af6
AO
165 /* A hash table mapping input bfds to other mips_got_info. NULL
166 unless multi-got was necessary. */
167 struct htab *bfd2got;
168 /* In multi-got links, a pointer to the next got (err, rather, most
169 of the time, it points to the previous got). */
170 struct mips_got_info *next;
0f20cc35
DJ
171 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
172 for none, or MINUS_TWO for not yet assigned. This is needed
173 because a single-GOT link may have multiple hash table entries
174 for the LDM. It does not get initialized in multi-GOT mode. */
175 bfd_vma tls_ldm_offset;
f4416af6
AO
176};
177
178/* Map an input bfd to a got in a multi-got link. */
179
91d6fa6a
NC
180struct mips_elf_bfd2got_hash
181{
f4416af6
AO
182 bfd *bfd;
183 struct mips_got_info *g;
184};
185
186/* Structure passed when traversing the bfd2got hash table, used to
187 create and merge bfd's gots. */
188
189struct mips_elf_got_per_bfd_arg
190{
191 /* A hashtable that maps bfds to gots. */
192 htab_t bfd2got;
193 /* The output bfd. */
194 bfd *obfd;
195 /* The link information. */
196 struct bfd_link_info *info;
197 /* A pointer to the primary got, i.e., the one that's going to get
198 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
199 DT_MIPS_GOTSYM. */
200 struct mips_got_info *primary;
201 /* A non-primary got we're trying to merge with other input bfd's
202 gots. */
203 struct mips_got_info *current;
204 /* The maximum number of got entries that can be addressed with a
205 16-bit offset. */
206 unsigned int max_count;
c224138d
RS
207 /* The maximum number of page entries needed by each got. */
208 unsigned int max_pages;
0f20cc35
DJ
209 /* The total number of global entries which will live in the
210 primary got and be automatically relocated. This includes
211 those not referenced by the primary GOT but included in
212 the "master" GOT. */
213 unsigned int global_count;
f4416af6
AO
214};
215
216/* Another structure used to pass arguments for got entries traversal. */
217
218struct mips_elf_set_global_got_offset_arg
219{
220 struct mips_got_info *g;
221 int value;
222 unsigned int needed_relocs;
223 struct bfd_link_info *info;
b49e97c9
TS
224};
225
0f20cc35
DJ
226/* A structure used to count TLS relocations or GOT entries, for GOT
227 entry or ELF symbol table traversal. */
228
229struct mips_elf_count_tls_arg
230{
231 struct bfd_link_info *info;
232 unsigned int needed;
233};
234
f0abc2a1
AM
235struct _mips_elf_section_data
236{
237 struct bfd_elf_section_data elf;
238 union
239 {
f0abc2a1
AM
240 bfd_byte *tdata;
241 } u;
242};
243
244#define mips_elf_section_data(sec) \
68bfbfcc 245 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 246
d5eaccd7
RS
247#define is_mips_elf(bfd) \
248 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
249 && elf_tdata (bfd) != NULL \
250 && elf_object_id (bfd) == MIPS_ELF_TDATA)
251
634835ae
RS
252/* The ABI says that every symbol used by dynamic relocations must have
253 a global GOT entry. Among other things, this provides the dynamic
254 linker with a free, directly-indexed cache. The GOT can therefore
255 contain symbols that are not referenced by GOT relocations themselves
256 (in other words, it may have symbols that are not referenced by things
257 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
258
259 GOT relocations are less likely to overflow if we put the associated
260 GOT entries towards the beginning. We therefore divide the global
261 GOT entries into two areas: "normal" and "reloc-only". Entries in
262 the first area can be used for both dynamic relocations and GP-relative
263 accesses, while those in the "reloc-only" area are for dynamic
264 relocations only.
265
266 These GGA_* ("Global GOT Area") values are organised so that lower
267 values are more general than higher values. Also, non-GGA_NONE
268 values are ordered by the position of the area in the GOT. */
269#define GGA_NORMAL 0
270#define GGA_RELOC_ONLY 1
271#define GGA_NONE 2
272
861fb55a
DJ
273/* Information about a non-PIC interface to a PIC function. There are
274 two ways of creating these interfaces. The first is to add:
275
276 lui $25,%hi(func)
277 addiu $25,$25,%lo(func)
278
279 immediately before a PIC function "func". The second is to add:
280
281 lui $25,%hi(func)
282 j func
283 addiu $25,$25,%lo(func)
284
285 to a separate trampoline section.
286
287 Stubs of the first kind go in a new section immediately before the
288 target function. Stubs of the second kind go in a single section
289 pointed to by the hash table's "strampoline" field. */
290struct mips_elf_la25_stub {
291 /* The generated section that contains this stub. */
292 asection *stub_section;
293
294 /* The offset of the stub from the start of STUB_SECTION. */
295 bfd_vma offset;
296
297 /* One symbol for the original function. Its location is available
298 in H->root.root.u.def. */
299 struct mips_elf_link_hash_entry *h;
300};
301
302/* Macros for populating a mips_elf_la25_stub. */
303
304#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
305#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
306#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
307
b49e97c9
TS
308/* This structure is passed to mips_elf_sort_hash_table_f when sorting
309 the dynamic symbols. */
310
311struct mips_elf_hash_sort_data
312{
313 /* The symbol in the global GOT with the lowest dynamic symbol table
314 index. */
315 struct elf_link_hash_entry *low;
0f20cc35
DJ
316 /* The least dynamic symbol table index corresponding to a non-TLS
317 symbol with a GOT entry. */
b49e97c9 318 long min_got_dynindx;
f4416af6
AO
319 /* The greatest dynamic symbol table index corresponding to a symbol
320 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 321 with dynamic relocations pointing to it from non-primary GOTs). */
f4416af6 322 long max_unref_got_dynindx;
b49e97c9
TS
323 /* The greatest dynamic symbol table index not corresponding to a
324 symbol without a GOT entry. */
325 long max_non_got_dynindx;
326};
327
328/* The MIPS ELF linker needs additional information for each symbol in
329 the global hash table. */
330
331struct mips_elf_link_hash_entry
332{
333 struct elf_link_hash_entry root;
334
335 /* External symbol information. */
336 EXTR esym;
337
861fb55a
DJ
338 /* The la25 stub we have created for ths symbol, if any. */
339 struct mips_elf_la25_stub *la25_stub;
340
b49e97c9
TS
341 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
342 this symbol. */
343 unsigned int possibly_dynamic_relocs;
344
b49e97c9
TS
345 /* If there is a stub that 32 bit functions should use to call this
346 16 bit function, this points to the section containing the stub. */
347 asection *fn_stub;
348
b49e97c9
TS
349 /* If there is a stub that 16 bit functions should use to call this
350 32 bit function, this points to the section containing the stub. */
351 asection *call_stub;
352
353 /* This is like the call_stub field, but it is used if the function
354 being called returns a floating point value. */
355 asection *call_fp_stub;
7c5fcef7 356
0f20cc35
DJ
357#define GOT_NORMAL 0
358#define GOT_TLS_GD 1
359#define GOT_TLS_LDM 2
360#define GOT_TLS_IE 4
361#define GOT_TLS_OFFSET_DONE 0x40
362#define GOT_TLS_DONE 0x80
363 unsigned char tls_type;
71782a75 364
0f20cc35
DJ
365 /* This is only used in single-GOT mode; in multi-GOT mode there
366 is one mips_got_entry per GOT entry, so the offset is stored
367 there. In single-GOT mode there may be many mips_got_entry
368 structures all referring to the same GOT slot. It might be
369 possible to use root.got.offset instead, but that field is
370 overloaded already. */
371 bfd_vma tls_got_offset;
71782a75 372
634835ae
RS
373 /* The highest GGA_* value that satisfies all references to this symbol. */
374 unsigned int global_got_area : 2;
375
71782a75
RS
376 /* True if one of the relocations described by possibly_dynamic_relocs
377 is against a readonly section. */
378 unsigned int readonly_reloc : 1;
379
861fb55a
DJ
380 /* True if there is a relocation against this symbol that must be
381 resolved by the static linker (in other words, if the relocation
382 cannot possibly be made dynamic). */
383 unsigned int has_static_relocs : 1;
384
71782a75
RS
385 /* True if we must not create a .MIPS.stubs entry for this symbol.
386 This is set, for example, if there are relocations related to
387 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
388 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
389 unsigned int no_fn_stub : 1;
390
391 /* Whether we need the fn_stub; this is true if this symbol appears
392 in any relocs other than a 16 bit call. */
393 unsigned int need_fn_stub : 1;
394
861fb55a
DJ
395 /* True if this symbol is referenced by branch relocations from
396 any non-PIC input file. This is used to determine whether an
397 la25 stub is required. */
398 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
399
400 /* Does this symbol need a traditional MIPS lazy-binding stub
401 (as opposed to a PLT entry)? */
402 unsigned int needs_lazy_stub : 1;
b49e97c9
TS
403};
404
405/* MIPS ELF linker hash table. */
406
407struct mips_elf_link_hash_table
408{
409 struct elf_link_hash_table root;
410#if 0
411 /* We no longer use this. */
412 /* String section indices for the dynamic section symbols. */
413 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
414#endif
861fb55a 415
b49e97c9
TS
416 /* The number of .rtproc entries. */
417 bfd_size_type procedure_count;
861fb55a 418
b49e97c9
TS
419 /* The size of the .compact_rel section (if SGI_COMPAT). */
420 bfd_size_type compact_rel_size;
861fb55a 421
b49e97c9 422 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
8dc1a139 423 entry is set to the address of __rld_obj_head as in IRIX5. */
b34976b6 424 bfd_boolean use_rld_obj_head;
861fb55a 425
b49e97c9
TS
426 /* This is the value of the __rld_map or __rld_obj_head symbol. */
427 bfd_vma rld_value;
861fb55a 428
b49e97c9 429 /* This is set if we see any mips16 stub sections. */
b34976b6 430 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
431
432 /* True if we can generate copy relocs and PLTs. */
433 bfd_boolean use_plts_and_copy_relocs;
434
0a44bf69
RS
435 /* True if we're generating code for VxWorks. */
436 bfd_boolean is_vxworks;
861fb55a 437
0e53d9da
AN
438 /* True if we already reported the small-data section overflow. */
439 bfd_boolean small_data_overflow_reported;
861fb55a 440
0a44bf69
RS
441 /* Shortcuts to some dynamic sections, or NULL if they are not
442 being used. */
443 asection *srelbss;
444 asection *sdynbss;
445 asection *srelplt;
446 asection *srelplt2;
447 asection *sgotplt;
448 asection *splt;
4e41d0d7 449 asection *sstubs;
a8028dd0 450 asection *sgot;
861fb55a 451
a8028dd0
RS
452 /* The master GOT information. */
453 struct mips_got_info *got_info;
861fb55a
DJ
454
455 /* The size of the PLT header in bytes. */
0a44bf69 456 bfd_vma plt_header_size;
861fb55a
DJ
457
458 /* The size of a PLT entry in bytes. */
0a44bf69 459 bfd_vma plt_entry_size;
861fb55a 460
33bb52fb
RS
461 /* The number of functions that need a lazy-binding stub. */
462 bfd_vma lazy_stub_count;
861fb55a 463
5108fc1b
RS
464 /* The size of a function stub entry in bytes. */
465 bfd_vma function_stub_size;
861fb55a
DJ
466
467 /* The number of reserved entries at the beginning of the GOT. */
468 unsigned int reserved_gotno;
469
470 /* The section used for mips_elf_la25_stub trampolines.
471 See the comment above that structure for details. */
472 asection *strampoline;
473
474 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
475 pairs. */
476 htab_t la25_stubs;
477
478 /* A function FN (NAME, IS, OS) that creates a new input section
479 called NAME and links it to output section OS. If IS is nonnull,
480 the new section should go immediately before it, otherwise it
481 should go at the (current) beginning of OS.
482
483 The function returns the new section on success, otherwise it
484 returns null. */
485 asection *(*add_stub_section) (const char *, asection *, asection *);
486};
487
488/* A structure used to communicate with htab_traverse callbacks. */
489struct mips_htab_traverse_info {
490 /* The usual link-wide information. */
491 struct bfd_link_info *info;
492 bfd *output_bfd;
493
494 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
495 bfd_boolean error;
b49e97c9
TS
496};
497
0f20cc35
DJ
498#define TLS_RELOC_P(r_type) \
499 (r_type == R_MIPS_TLS_DTPMOD32 \
500 || r_type == R_MIPS_TLS_DTPMOD64 \
501 || r_type == R_MIPS_TLS_DTPREL32 \
502 || r_type == R_MIPS_TLS_DTPREL64 \
503 || r_type == R_MIPS_TLS_GD \
504 || r_type == R_MIPS_TLS_LDM \
505 || r_type == R_MIPS_TLS_DTPREL_HI16 \
506 || r_type == R_MIPS_TLS_DTPREL_LO16 \
507 || r_type == R_MIPS_TLS_GOTTPREL \
508 || r_type == R_MIPS_TLS_TPREL32 \
509 || r_type == R_MIPS_TLS_TPREL64 \
510 || r_type == R_MIPS_TLS_TPREL_HI16 \
511 || r_type == R_MIPS_TLS_TPREL_LO16)
512
b49e97c9
TS
513/* Structure used to pass information to mips_elf_output_extsym. */
514
515struct extsym_info
516{
9e4aeb93
RS
517 bfd *abfd;
518 struct bfd_link_info *info;
b49e97c9
TS
519 struct ecoff_debug_info *debug;
520 const struct ecoff_debug_swap *swap;
b34976b6 521 bfd_boolean failed;
b49e97c9
TS
522};
523
8dc1a139 524/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
525
526static const char * const mips_elf_dynsym_rtproc_names[] =
527{
528 "_procedure_table",
529 "_procedure_string_table",
530 "_procedure_table_size",
531 NULL
532};
533
534/* These structures are used to generate the .compact_rel section on
8dc1a139 535 IRIX5. */
b49e97c9
TS
536
537typedef struct
538{
539 unsigned long id1; /* Always one? */
540 unsigned long num; /* Number of compact relocation entries. */
541 unsigned long id2; /* Always two? */
542 unsigned long offset; /* The file offset of the first relocation. */
543 unsigned long reserved0; /* Zero? */
544 unsigned long reserved1; /* Zero? */
545} Elf32_compact_rel;
546
547typedef struct
548{
549 bfd_byte id1[4];
550 bfd_byte num[4];
551 bfd_byte id2[4];
552 bfd_byte offset[4];
553 bfd_byte reserved0[4];
554 bfd_byte reserved1[4];
555} Elf32_External_compact_rel;
556
557typedef struct
558{
559 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
560 unsigned int rtype : 4; /* Relocation types. See below. */
561 unsigned int dist2to : 8;
562 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
563 unsigned long konst; /* KONST field. See below. */
564 unsigned long vaddr; /* VADDR to be relocated. */
565} Elf32_crinfo;
566
567typedef struct
568{
569 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
570 unsigned int rtype : 4; /* Relocation types. See below. */
571 unsigned int dist2to : 8;
572 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
573 unsigned long konst; /* KONST field. See below. */
574} Elf32_crinfo2;
575
576typedef struct
577{
578 bfd_byte info[4];
579 bfd_byte konst[4];
580 bfd_byte vaddr[4];
581} Elf32_External_crinfo;
582
583typedef struct
584{
585 bfd_byte info[4];
586 bfd_byte konst[4];
587} Elf32_External_crinfo2;
588
589/* These are the constants used to swap the bitfields in a crinfo. */
590
591#define CRINFO_CTYPE (0x1)
592#define CRINFO_CTYPE_SH (31)
593#define CRINFO_RTYPE (0xf)
594#define CRINFO_RTYPE_SH (27)
595#define CRINFO_DIST2TO (0xff)
596#define CRINFO_DIST2TO_SH (19)
597#define CRINFO_RELVADDR (0x7ffff)
598#define CRINFO_RELVADDR_SH (0)
599
600/* A compact relocation info has long (3 words) or short (2 words)
601 formats. A short format doesn't have VADDR field and relvaddr
602 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
603#define CRF_MIPS_LONG 1
604#define CRF_MIPS_SHORT 0
605
606/* There are 4 types of compact relocation at least. The value KONST
607 has different meaning for each type:
608
609 (type) (konst)
610 CT_MIPS_REL32 Address in data
611 CT_MIPS_WORD Address in word (XXX)
612 CT_MIPS_GPHI_LO GP - vaddr
613 CT_MIPS_JMPAD Address to jump
614 */
615
616#define CRT_MIPS_REL32 0xa
617#define CRT_MIPS_WORD 0xb
618#define CRT_MIPS_GPHI_LO 0xc
619#define CRT_MIPS_JMPAD 0xd
620
621#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
622#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
623#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
624#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
625\f
626/* The structure of the runtime procedure descriptor created by the
627 loader for use by the static exception system. */
628
629typedef struct runtime_pdr {
ae9a127f
NC
630 bfd_vma adr; /* Memory address of start of procedure. */
631 long regmask; /* Save register mask. */
632 long regoffset; /* Save register offset. */
633 long fregmask; /* Save floating point register mask. */
634 long fregoffset; /* Save floating point register offset. */
635 long frameoffset; /* Frame size. */
636 short framereg; /* Frame pointer register. */
637 short pcreg; /* Offset or reg of return pc. */
638 long irpss; /* Index into the runtime string table. */
b49e97c9 639 long reserved;
ae9a127f 640 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
641} RPDR, *pRPDR;
642#define cbRPDR sizeof (RPDR)
643#define rpdNil ((pRPDR) 0)
644\f
b15e6682 645static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
646 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
647 struct mips_elf_link_hash_entry *, int);
b34976b6 648static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 649 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
650static bfd_vma mips_elf_high
651 (bfd_vma);
b34976b6 652static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
653 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
654 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
655 bfd_vma *, asection *);
9719ad41
RS
656static hashval_t mips_elf_got_entry_hash
657 (const void *);
f4416af6 658static bfd_vma mips_elf_adjust_gp
9719ad41 659 (bfd *, struct mips_got_info *, bfd *);
f4416af6 660static struct mips_got_info *mips_elf_got_for_ibfd
9719ad41 661 (struct mips_got_info *, bfd *);
f4416af6 662
b49e97c9
TS
663/* This will be used when we sort the dynamic relocation records. */
664static bfd *reldyn_sorting_bfd;
665
6d30f5b2
NC
666/* True if ABFD is for CPUs with load interlocking that include
667 non-MIPS1 CPUs and R3900. */
668#define LOAD_INTERLOCKS_P(abfd) \
669 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
670 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
671
cd8d5a82
CF
672/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
673 This should be safe for all architectures. We enable this predicate
674 for RM9000 for now. */
675#define JAL_TO_BAL_P(abfd) \
676 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
677
678/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
679 This should be safe for all architectures. We enable this predicate for
680 all CPUs. */
681#define JALR_TO_BAL_P(abfd) 1
682
861fb55a
DJ
683/* True if ABFD is a PIC object. */
684#define PIC_OBJECT_P(abfd) \
685 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
686
b49e97c9 687/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
688#define ABI_N32_P(abfd) \
689 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
690
4a14403c 691/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 692#define ABI_64_P(abfd) \
141ff970 693 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 694
4a14403c
TS
695/* Nonzero if ABFD is using NewABI conventions. */
696#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
697
698/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
699#define IRIX_COMPAT(abfd) \
700 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
701
b49e97c9
TS
702/* Whether we are trying to be compatible with IRIX at all. */
703#define SGI_COMPAT(abfd) \
704 (IRIX_COMPAT (abfd) != ict_none)
705
706/* The name of the options section. */
707#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 708 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 709
cc2e31b9
RS
710/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
711 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
712#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
713 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
714
943284cc
DJ
715/* Whether the section is readonly. */
716#define MIPS_ELF_READONLY_SECTION(sec) \
717 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
718 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
719
b49e97c9 720/* The name of the stub section. */
ca07892d 721#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
722
723/* The size of an external REL relocation. */
724#define MIPS_ELF_REL_SIZE(abfd) \
725 (get_elf_backend_data (abfd)->s->sizeof_rel)
726
0a44bf69
RS
727/* The size of an external RELA relocation. */
728#define MIPS_ELF_RELA_SIZE(abfd) \
729 (get_elf_backend_data (abfd)->s->sizeof_rela)
730
b49e97c9
TS
731/* The size of an external dynamic table entry. */
732#define MIPS_ELF_DYN_SIZE(abfd) \
733 (get_elf_backend_data (abfd)->s->sizeof_dyn)
734
735/* The size of a GOT entry. */
736#define MIPS_ELF_GOT_SIZE(abfd) \
737 (get_elf_backend_data (abfd)->s->arch_size / 8)
738
739/* The size of a symbol-table entry. */
740#define MIPS_ELF_SYM_SIZE(abfd) \
741 (get_elf_backend_data (abfd)->s->sizeof_sym)
742
743/* The default alignment for sections, as a power of two. */
744#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 745 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
746
747/* Get word-sized data. */
748#define MIPS_ELF_GET_WORD(abfd, ptr) \
749 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
750
751/* Put out word-sized data. */
752#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
753 (ABI_64_P (abfd) \
754 ? bfd_put_64 (abfd, val, ptr) \
755 : bfd_put_32 (abfd, val, ptr))
756
861fb55a
DJ
757/* The opcode for word-sized loads (LW or LD). */
758#define MIPS_ELF_LOAD_WORD(abfd) \
759 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
760
b49e97c9 761/* Add a dynamic symbol table-entry. */
9719ad41 762#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 763 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
764
765#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
766 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
767
4ffba85c
AO
768/* Determine whether the internal relocation of index REL_IDX is REL
769 (zero) or RELA (non-zero). The assumption is that, if there are
770 two relocation sections for this section, one of them is REL and
771 the other is RELA. If the index of the relocation we're testing is
772 in range for the first relocation section, check that the external
773 relocation size is that for RELA. It is also assumed that, if
774 rel_idx is not in range for the first section, and this first
775 section contains REL relocs, then the relocation is in the second
776 section, that is RELA. */
777#define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
778 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
779 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
780 > (bfd_vma)(rel_idx)) \
781 == (elf_section_data (sec)->rel_hdr.sh_entsize \
782 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
783 : sizeof (Elf32_External_Rela))))
784
0a44bf69
RS
785/* The name of the dynamic relocation section. */
786#define MIPS_ELF_REL_DYN_NAME(INFO) \
787 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
788
b49e97c9
TS
789/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
790 from smaller values. Start with zero, widen, *then* decrement. */
791#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 792#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 793
51e38d68
RS
794/* The value to write into got[1] for SVR4 targets, to identify it is
795 a GNU object. The dynamic linker can then use got[1] to store the
796 module pointer. */
797#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
798 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
799
f4416af6 800/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
801#define ELF_MIPS_GP_OFFSET(INFO) \
802 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
803
804/* The maximum size of the GOT for it to be addressable using 16-bit
805 offsets from $gp. */
0a44bf69 806#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 807
6a691779 808/* Instructions which appear in a stub. */
3d6746ca
DD
809#define STUB_LW(abfd) \
810 ((ABI_64_P (abfd) \
811 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
812 : 0x8f998010)) /* lw t9,0x8010(gp) */
813#define STUB_MOVE(abfd) \
814 ((ABI_64_P (abfd) \
815 ? 0x03e0782d /* daddu t7,ra */ \
816 : 0x03e07821)) /* addu t7,ra */
817#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
818#define STUB_JALR 0x0320f809 /* jalr t9,ra */
5108fc1b
RS
819#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
820#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
821#define STUB_LI16S(abfd, VAL) \
822 ((ABI_64_P (abfd) \
823 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
824 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
825
5108fc1b
RS
826#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
827#define MIPS_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
828
829/* The name of the dynamic interpreter. This is put in the .interp
830 section. */
831
832#define ELF_DYNAMIC_INTERPRETER(abfd) \
833 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
834 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
835 : "/usr/lib/libc.so.1")
836
837#ifdef BFD64
ee6423ed
AO
838#define MNAME(bfd,pre,pos) \
839 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
840#define ELF_R_SYM(bfd, i) \
841 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
842#define ELF_R_TYPE(bfd, i) \
843 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
844#define ELF_R_INFO(bfd, s, t) \
845 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
846#else
ee6423ed 847#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
848#define ELF_R_SYM(bfd, i) \
849 (ELF32_R_SYM (i))
850#define ELF_R_TYPE(bfd, i) \
851 (ELF32_R_TYPE (i))
852#define ELF_R_INFO(bfd, s, t) \
853 (ELF32_R_INFO (s, t))
854#endif
855\f
856 /* The mips16 compiler uses a couple of special sections to handle
857 floating point arguments.
858
859 Section names that look like .mips16.fn.FNNAME contain stubs that
860 copy floating point arguments from the fp regs to the gp regs and
861 then jump to FNNAME. If any 32 bit function calls FNNAME, the
862 call should be redirected to the stub instead. If no 32 bit
863 function calls FNNAME, the stub should be discarded. We need to
864 consider any reference to the function, not just a call, because
865 if the address of the function is taken we will need the stub,
866 since the address might be passed to a 32 bit function.
867
868 Section names that look like .mips16.call.FNNAME contain stubs
869 that copy floating point arguments from the gp regs to the fp
870 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
871 then any 16 bit function that calls FNNAME should be redirected
872 to the stub instead. If FNNAME is not a 32 bit function, the
873 stub should be discarded.
874
875 .mips16.call.fp.FNNAME sections are similar, but contain stubs
876 which call FNNAME and then copy the return value from the fp regs
877 to the gp regs. These stubs store the return value in $18 while
878 calling FNNAME; any function which might call one of these stubs
879 must arrange to save $18 around the call. (This case is not
880 needed for 32 bit functions that call 16 bit functions, because
881 16 bit functions always return floating point values in both
882 $f0/$f1 and $2/$3.)
883
884 Note that in all cases FNNAME might be defined statically.
885 Therefore, FNNAME is not used literally. Instead, the relocation
886 information will indicate which symbol the section is for.
887
888 We record any stubs that we find in the symbol table. */
889
890#define FN_STUB ".mips16.fn."
891#define CALL_STUB ".mips16.call."
892#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
893
894#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
895#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
896#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 897\f
861fb55a 898/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
899static const bfd_vma mips_o32_exec_plt0_entry[] =
900{
861fb55a
DJ
901 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
902 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
903 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
904 0x031cc023, /* subu $24, $24, $28 */
905 0x03e07821, /* move $15, $31 */
906 0x0018c082, /* srl $24, $24, 2 */
907 0x0320f809, /* jalr $25 */
908 0x2718fffe /* subu $24, $24, 2 */
909};
910
911/* The format of the first PLT entry in an N32 executable. Different
912 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
913static const bfd_vma mips_n32_exec_plt0_entry[] =
914{
861fb55a
DJ
915 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
916 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
917 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
918 0x030ec023, /* subu $24, $24, $14 */
919 0x03e07821, /* move $15, $31 */
920 0x0018c082, /* srl $24, $24, 2 */
921 0x0320f809, /* jalr $25 */
922 0x2718fffe /* subu $24, $24, 2 */
923};
924
925/* The format of the first PLT entry in an N64 executable. Different
926 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
927static const bfd_vma mips_n64_exec_plt0_entry[] =
928{
861fb55a
DJ
929 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
930 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
931 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
932 0x030ec023, /* subu $24, $24, $14 */
933 0x03e07821, /* move $15, $31 */
934 0x0018c0c2, /* srl $24, $24, 3 */
935 0x0320f809, /* jalr $25 */
936 0x2718fffe /* subu $24, $24, 2 */
937};
938
939/* The format of subsequent PLT entries. */
6d30f5b2
NC
940static const bfd_vma mips_exec_plt_entry[] =
941{
861fb55a
DJ
942 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
943 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
944 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
945 0x03200008 /* jr $25 */
946};
947
0a44bf69 948/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
949static const bfd_vma mips_vxworks_exec_plt0_entry[] =
950{
0a44bf69
RS
951 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
952 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
953 0x8f390008, /* lw t9, 8(t9) */
954 0x00000000, /* nop */
955 0x03200008, /* jr t9 */
956 0x00000000 /* nop */
957};
958
959/* The format of subsequent PLT entries. */
6d30f5b2
NC
960static const bfd_vma mips_vxworks_exec_plt_entry[] =
961{
0a44bf69
RS
962 0x10000000, /* b .PLT_resolver */
963 0x24180000, /* li t8, <pltindex> */
964 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
965 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
966 0x8f390000, /* lw t9, 0(t9) */
967 0x00000000, /* nop */
968 0x03200008, /* jr t9 */
969 0x00000000 /* nop */
970};
971
972/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
973static const bfd_vma mips_vxworks_shared_plt0_entry[] =
974{
0a44bf69
RS
975 0x8f990008, /* lw t9, 8(gp) */
976 0x00000000, /* nop */
977 0x03200008, /* jr t9 */
978 0x00000000, /* nop */
979 0x00000000, /* nop */
980 0x00000000 /* nop */
981};
982
983/* The format of subsequent PLT entries. */
6d30f5b2
NC
984static const bfd_vma mips_vxworks_shared_plt_entry[] =
985{
0a44bf69
RS
986 0x10000000, /* b .PLT_resolver */
987 0x24180000 /* li t8, <pltindex> */
988};
989\f
b49e97c9
TS
990/* Look up an entry in a MIPS ELF linker hash table. */
991
992#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
993 ((struct mips_elf_link_hash_entry *) \
994 elf_link_hash_lookup (&(table)->root, (string), (create), \
995 (copy), (follow)))
996
997/* Traverse a MIPS ELF linker hash table. */
998
999#define mips_elf_link_hash_traverse(table, func, info) \
1000 (elf_link_hash_traverse \
1001 (&(table)->root, \
9719ad41 1002 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1003 (info)))
1004
1005/* Get the MIPS ELF linker hash table from a link_info structure. */
1006
1007#define mips_elf_hash_table(p) \
1008 ((struct mips_elf_link_hash_table *) ((p)->hash))
1009
0f20cc35
DJ
1010/* Find the base offsets for thread-local storage in this object,
1011 for GD/LD and IE/LE respectively. */
1012
1013#define TP_OFFSET 0x7000
1014#define DTP_OFFSET 0x8000
1015
1016static bfd_vma
1017dtprel_base (struct bfd_link_info *info)
1018{
1019 /* If tls_sec is NULL, we should have signalled an error already. */
1020 if (elf_hash_table (info)->tls_sec == NULL)
1021 return 0;
1022 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1023}
1024
1025static bfd_vma
1026tprel_base (struct bfd_link_info *info)
1027{
1028 /* If tls_sec is NULL, we should have signalled an error already. */
1029 if (elf_hash_table (info)->tls_sec == NULL)
1030 return 0;
1031 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1032}
1033
b49e97c9
TS
1034/* Create an entry in a MIPS ELF linker hash table. */
1035
1036static struct bfd_hash_entry *
9719ad41
RS
1037mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1038 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1039{
1040 struct mips_elf_link_hash_entry *ret =
1041 (struct mips_elf_link_hash_entry *) entry;
1042
1043 /* Allocate the structure if it has not already been allocated by a
1044 subclass. */
9719ad41
RS
1045 if (ret == NULL)
1046 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1047 if (ret == NULL)
b49e97c9
TS
1048 return (struct bfd_hash_entry *) ret;
1049
1050 /* Call the allocation method of the superclass. */
1051 ret = ((struct mips_elf_link_hash_entry *)
1052 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1053 table, string));
9719ad41 1054 if (ret != NULL)
b49e97c9
TS
1055 {
1056 /* Set local fields. */
1057 memset (&ret->esym, 0, sizeof (EXTR));
1058 /* We use -2 as a marker to indicate that the information has
1059 not been set. -1 means there is no associated ifd. */
1060 ret->esym.ifd = -2;
861fb55a 1061 ret->la25_stub = 0;
b49e97c9 1062 ret->possibly_dynamic_relocs = 0;
b49e97c9 1063 ret->fn_stub = NULL;
b49e97c9
TS
1064 ret->call_stub = NULL;
1065 ret->call_fp_stub = NULL;
71782a75 1066 ret->tls_type = GOT_NORMAL;
634835ae 1067 ret->global_got_area = GGA_NONE;
71782a75 1068 ret->readonly_reloc = FALSE;
861fb55a 1069 ret->has_static_relocs = FALSE;
71782a75
RS
1070 ret->no_fn_stub = FALSE;
1071 ret->need_fn_stub = FALSE;
861fb55a 1072 ret->has_nonpic_branches = FALSE;
33bb52fb 1073 ret->needs_lazy_stub = FALSE;
b49e97c9
TS
1074 }
1075
1076 return (struct bfd_hash_entry *) ret;
1077}
f0abc2a1
AM
1078
1079bfd_boolean
9719ad41 1080_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1081{
f592407e
AM
1082 if (!sec->used_by_bfd)
1083 {
1084 struct _mips_elf_section_data *sdata;
1085 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1086
f592407e
AM
1087 sdata = bfd_zalloc (abfd, amt);
1088 if (sdata == NULL)
1089 return FALSE;
1090 sec->used_by_bfd = sdata;
1091 }
f0abc2a1
AM
1092
1093 return _bfd_elf_new_section_hook (abfd, sec);
1094}
b49e97c9
TS
1095\f
1096/* Read ECOFF debugging information from a .mdebug section into a
1097 ecoff_debug_info structure. */
1098
b34976b6 1099bfd_boolean
9719ad41
RS
1100_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1101 struct ecoff_debug_info *debug)
b49e97c9
TS
1102{
1103 HDRR *symhdr;
1104 const struct ecoff_debug_swap *swap;
9719ad41 1105 char *ext_hdr;
b49e97c9
TS
1106
1107 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1108 memset (debug, 0, sizeof (*debug));
1109
9719ad41 1110 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1111 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1112 goto error_return;
1113
9719ad41 1114 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1115 swap->external_hdr_size))
b49e97c9
TS
1116 goto error_return;
1117
1118 symhdr = &debug->symbolic_header;
1119 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1120
1121 /* The symbolic header contains absolute file offsets and sizes to
1122 read. */
1123#define READ(ptr, offset, count, size, type) \
1124 if (symhdr->count == 0) \
1125 debug->ptr = NULL; \
1126 else \
1127 { \
1128 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1129 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1130 if (debug->ptr == NULL) \
1131 goto error_return; \
9719ad41 1132 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1133 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1134 goto error_return; \
1135 }
1136
1137 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1138 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1139 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1140 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1141 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1142 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1143 union aux_ext *);
1144 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1145 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1146 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1147 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1148 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1149#undef READ
1150
1151 debug->fdr = NULL;
b49e97c9 1152
b34976b6 1153 return TRUE;
b49e97c9
TS
1154
1155 error_return:
1156 if (ext_hdr != NULL)
1157 free (ext_hdr);
1158 if (debug->line != NULL)
1159 free (debug->line);
1160 if (debug->external_dnr != NULL)
1161 free (debug->external_dnr);
1162 if (debug->external_pdr != NULL)
1163 free (debug->external_pdr);
1164 if (debug->external_sym != NULL)
1165 free (debug->external_sym);
1166 if (debug->external_opt != NULL)
1167 free (debug->external_opt);
1168 if (debug->external_aux != NULL)
1169 free (debug->external_aux);
1170 if (debug->ss != NULL)
1171 free (debug->ss);
1172 if (debug->ssext != NULL)
1173 free (debug->ssext);
1174 if (debug->external_fdr != NULL)
1175 free (debug->external_fdr);
1176 if (debug->external_rfd != NULL)
1177 free (debug->external_rfd);
1178 if (debug->external_ext != NULL)
1179 free (debug->external_ext);
b34976b6 1180 return FALSE;
b49e97c9
TS
1181}
1182\f
1183/* Swap RPDR (runtime procedure table entry) for output. */
1184
1185static void
9719ad41 1186ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1187{
1188 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1189 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1190 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1191 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1192 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1193 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1194
1195 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1196 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1197
1198 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1199}
1200
1201/* Create a runtime procedure table from the .mdebug section. */
1202
b34976b6 1203static bfd_boolean
9719ad41
RS
1204mips_elf_create_procedure_table (void *handle, bfd *abfd,
1205 struct bfd_link_info *info, asection *s,
1206 struct ecoff_debug_info *debug)
b49e97c9
TS
1207{
1208 const struct ecoff_debug_swap *swap;
1209 HDRR *hdr = &debug->symbolic_header;
1210 RPDR *rpdr, *rp;
1211 struct rpdr_ext *erp;
9719ad41 1212 void *rtproc;
b49e97c9
TS
1213 struct pdr_ext *epdr;
1214 struct sym_ext *esym;
1215 char *ss, **sv;
1216 char *str;
1217 bfd_size_type size;
1218 bfd_size_type count;
1219 unsigned long sindex;
1220 unsigned long i;
1221 PDR pdr;
1222 SYMR sym;
1223 const char *no_name_func = _("static procedure (no name)");
1224
1225 epdr = NULL;
1226 rpdr = NULL;
1227 esym = NULL;
1228 ss = NULL;
1229 sv = NULL;
1230
1231 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1232
1233 sindex = strlen (no_name_func) + 1;
1234 count = hdr->ipdMax;
1235 if (count > 0)
1236 {
1237 size = swap->external_pdr_size;
1238
9719ad41 1239 epdr = bfd_malloc (size * count);
b49e97c9
TS
1240 if (epdr == NULL)
1241 goto error_return;
1242
9719ad41 1243 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1244 goto error_return;
1245
1246 size = sizeof (RPDR);
9719ad41 1247 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1248 if (rpdr == NULL)
1249 goto error_return;
1250
1251 size = sizeof (char *);
9719ad41 1252 sv = bfd_malloc (size * count);
b49e97c9
TS
1253 if (sv == NULL)
1254 goto error_return;
1255
1256 count = hdr->isymMax;
1257 size = swap->external_sym_size;
9719ad41 1258 esym = bfd_malloc (size * count);
b49e97c9
TS
1259 if (esym == NULL)
1260 goto error_return;
1261
9719ad41 1262 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1263 goto error_return;
1264
1265 count = hdr->issMax;
9719ad41 1266 ss = bfd_malloc (count);
b49e97c9
TS
1267 if (ss == NULL)
1268 goto error_return;
f075ee0c 1269 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1270 goto error_return;
1271
1272 count = hdr->ipdMax;
1273 for (i = 0; i < (unsigned long) count; i++, rp++)
1274 {
9719ad41
RS
1275 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1276 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1277 rp->adr = sym.value;
1278 rp->regmask = pdr.regmask;
1279 rp->regoffset = pdr.regoffset;
1280 rp->fregmask = pdr.fregmask;
1281 rp->fregoffset = pdr.fregoffset;
1282 rp->frameoffset = pdr.frameoffset;
1283 rp->framereg = pdr.framereg;
1284 rp->pcreg = pdr.pcreg;
1285 rp->irpss = sindex;
1286 sv[i] = ss + sym.iss;
1287 sindex += strlen (sv[i]) + 1;
1288 }
1289 }
1290
1291 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1292 size = BFD_ALIGN (size, 16);
9719ad41 1293 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1294 if (rtproc == NULL)
1295 {
1296 mips_elf_hash_table (info)->procedure_count = 0;
1297 goto error_return;
1298 }
1299
1300 mips_elf_hash_table (info)->procedure_count = count + 2;
1301
9719ad41 1302 erp = rtproc;
b49e97c9
TS
1303 memset (erp, 0, sizeof (struct rpdr_ext));
1304 erp++;
1305 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1306 strcpy (str, no_name_func);
1307 str += strlen (no_name_func) + 1;
1308 for (i = 0; i < count; i++)
1309 {
1310 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1311 strcpy (str, sv[i]);
1312 str += strlen (sv[i]) + 1;
1313 }
1314 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1315
1316 /* Set the size and contents of .rtproc section. */
eea6121a 1317 s->size = size;
9719ad41 1318 s->contents = rtproc;
b49e97c9
TS
1319
1320 /* Skip this section later on (I don't think this currently
1321 matters, but someday it might). */
8423293d 1322 s->map_head.link_order = NULL;
b49e97c9
TS
1323
1324 if (epdr != NULL)
1325 free (epdr);
1326 if (rpdr != NULL)
1327 free (rpdr);
1328 if (esym != NULL)
1329 free (esym);
1330 if (ss != NULL)
1331 free (ss);
1332 if (sv != NULL)
1333 free (sv);
1334
b34976b6 1335 return TRUE;
b49e97c9
TS
1336
1337 error_return:
1338 if (epdr != NULL)
1339 free (epdr);
1340 if (rpdr != NULL)
1341 free (rpdr);
1342 if (esym != NULL)
1343 free (esym);
1344 if (ss != NULL)
1345 free (ss);
1346 if (sv != NULL)
1347 free (sv);
b34976b6 1348 return FALSE;
b49e97c9 1349}
738e5348 1350\f
861fb55a
DJ
1351/* We're going to create a stub for H. Create a symbol for the stub's
1352 value and size, to help make the disassembly easier to read. */
1353
1354static bfd_boolean
1355mips_elf_create_stub_symbol (struct bfd_link_info *info,
1356 struct mips_elf_link_hash_entry *h,
1357 const char *prefix, asection *s, bfd_vma value,
1358 bfd_vma size)
1359{
1360 struct bfd_link_hash_entry *bh;
1361 struct elf_link_hash_entry *elfh;
1362 const char *name;
1363
1364 /* Create a new symbol. */
1365 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1366 bh = NULL;
1367 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1368 BSF_LOCAL, s, value, NULL,
1369 TRUE, FALSE, &bh))
1370 return FALSE;
1371
1372 /* Make it a local function. */
1373 elfh = (struct elf_link_hash_entry *) bh;
1374 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1375 elfh->size = size;
1376 elfh->forced_local = 1;
1377 return TRUE;
1378}
1379
738e5348
RS
1380/* We're about to redefine H. Create a symbol to represent H's
1381 current value and size, to help make the disassembly easier
1382 to read. */
1383
1384static bfd_boolean
1385mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1386 struct mips_elf_link_hash_entry *h,
1387 const char *prefix)
1388{
1389 struct bfd_link_hash_entry *bh;
1390 struct elf_link_hash_entry *elfh;
1391 const char *name;
1392 asection *s;
1393 bfd_vma value;
1394
1395 /* Read the symbol's value. */
1396 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1397 || h->root.root.type == bfd_link_hash_defweak);
1398 s = h->root.root.u.def.section;
1399 value = h->root.root.u.def.value;
1400
1401 /* Create a new symbol. */
1402 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1403 bh = NULL;
1404 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1405 BSF_LOCAL, s, value, NULL,
1406 TRUE, FALSE, &bh))
1407 return FALSE;
1408
1409 /* Make it local and copy the other attributes from H. */
1410 elfh = (struct elf_link_hash_entry *) bh;
1411 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1412 elfh->other = h->root.other;
1413 elfh->size = h->root.size;
1414 elfh->forced_local = 1;
1415 return TRUE;
1416}
1417
1418/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1419 function rather than to a hard-float stub. */
1420
1421static bfd_boolean
1422section_allows_mips16_refs_p (asection *section)
1423{
1424 const char *name;
1425
1426 name = bfd_get_section_name (section->owner, section);
1427 return (FN_STUB_P (name)
1428 || CALL_STUB_P (name)
1429 || CALL_FP_STUB_P (name)
1430 || strcmp (name, ".pdr") == 0);
1431}
1432
1433/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1434 stub section of some kind. Return the R_SYMNDX of the target
1435 function, or 0 if we can't decide which function that is. */
1436
1437static unsigned long
502e814e
TT
1438mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1439 const Elf_Internal_Rela *relocs,
738e5348
RS
1440 const Elf_Internal_Rela *relend)
1441{
1442 const Elf_Internal_Rela *rel;
1443
1444 /* Trust the first R_MIPS_NONE relocation, if any. */
1445 for (rel = relocs; rel < relend; rel++)
1446 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1447 return ELF_R_SYM (sec->owner, rel->r_info);
1448
1449 /* Otherwise trust the first relocation, whatever its kind. This is
1450 the traditional behavior. */
1451 if (relocs < relend)
1452 return ELF_R_SYM (sec->owner, relocs->r_info);
1453
1454 return 0;
1455}
b49e97c9
TS
1456
1457/* Check the mips16 stubs for a particular symbol, and see if we can
1458 discard them. */
1459
861fb55a
DJ
1460static void
1461mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1462 struct mips_elf_link_hash_entry *h)
b49e97c9 1463{
738e5348
RS
1464 /* Dynamic symbols must use the standard call interface, in case other
1465 objects try to call them. */
1466 if (h->fn_stub != NULL
1467 && h->root.dynindx != -1)
1468 {
1469 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1470 h->need_fn_stub = TRUE;
1471 }
1472
b49e97c9
TS
1473 if (h->fn_stub != NULL
1474 && ! h->need_fn_stub)
1475 {
1476 /* We don't need the fn_stub; the only references to this symbol
1477 are 16 bit calls. Clobber the size to 0 to prevent it from
1478 being included in the link. */
eea6121a 1479 h->fn_stub->size = 0;
b49e97c9
TS
1480 h->fn_stub->flags &= ~SEC_RELOC;
1481 h->fn_stub->reloc_count = 0;
1482 h->fn_stub->flags |= SEC_EXCLUDE;
1483 }
1484
1485 if (h->call_stub != NULL
30c09090 1486 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1487 {
1488 /* We don't need the call_stub; this is a 16 bit function, so
1489 calls from other 16 bit functions are OK. Clobber the size
1490 to 0 to prevent it from being included in the link. */
eea6121a 1491 h->call_stub->size = 0;
b49e97c9
TS
1492 h->call_stub->flags &= ~SEC_RELOC;
1493 h->call_stub->reloc_count = 0;
1494 h->call_stub->flags |= SEC_EXCLUDE;
1495 }
1496
1497 if (h->call_fp_stub != NULL
30c09090 1498 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1499 {
1500 /* We don't need the call_stub; this is a 16 bit function, so
1501 calls from other 16 bit functions are OK. Clobber the size
1502 to 0 to prevent it from being included in the link. */
eea6121a 1503 h->call_fp_stub->size = 0;
b49e97c9
TS
1504 h->call_fp_stub->flags &= ~SEC_RELOC;
1505 h->call_fp_stub->reloc_count = 0;
1506 h->call_fp_stub->flags |= SEC_EXCLUDE;
1507 }
861fb55a
DJ
1508}
1509
1510/* Hashtable callbacks for mips_elf_la25_stubs. */
1511
1512static hashval_t
1513mips_elf_la25_stub_hash (const void *entry_)
1514{
1515 const struct mips_elf_la25_stub *entry;
1516
1517 entry = (struct mips_elf_la25_stub *) entry_;
1518 return entry->h->root.root.u.def.section->id
1519 + entry->h->root.root.u.def.value;
1520}
1521
1522static int
1523mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1524{
1525 const struct mips_elf_la25_stub *entry1, *entry2;
1526
1527 entry1 = (struct mips_elf_la25_stub *) entry1_;
1528 entry2 = (struct mips_elf_la25_stub *) entry2_;
1529 return ((entry1->h->root.root.u.def.section
1530 == entry2->h->root.root.u.def.section)
1531 && (entry1->h->root.root.u.def.value
1532 == entry2->h->root.root.u.def.value));
1533}
1534
1535/* Called by the linker to set up the la25 stub-creation code. FN is
1536 the linker's implementation of add_stub_function. Return true on
1537 success. */
1538
1539bfd_boolean
1540_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1541 asection *(*fn) (const char *, asection *,
1542 asection *))
1543{
1544 struct mips_elf_link_hash_table *htab;
1545
1546 htab = mips_elf_hash_table (info);
1547 htab->add_stub_section = fn;
1548 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1549 mips_elf_la25_stub_eq, NULL);
1550 if (htab->la25_stubs == NULL)
1551 return FALSE;
1552
1553 return TRUE;
1554}
1555
1556/* Return true if H is a locally-defined PIC function, in the sense
1557 that it might need $25 to be valid on entry. Note that MIPS16
1558 functions never need $25 to be valid on entry; they set up $gp
1559 using PC-relative instructions instead. */
1560
1561static bfd_boolean
1562mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1563{
1564 return ((h->root.root.type == bfd_link_hash_defined
1565 || h->root.root.type == bfd_link_hash_defweak)
1566 && h->root.def_regular
1567 && !bfd_is_abs_section (h->root.root.u.def.section)
1568 && !ELF_ST_IS_MIPS16 (h->root.other)
1569 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1570 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1571}
1572
1573/* STUB describes an la25 stub that we have decided to implement
1574 by inserting an LUI/ADDIU pair before the target function.
1575 Create the section and redirect the function symbol to it. */
1576
1577static bfd_boolean
1578mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1579 struct bfd_link_info *info)
1580{
1581 struct mips_elf_link_hash_table *htab;
1582 char *name;
1583 asection *s, *input_section;
1584 unsigned int align;
1585
1586 htab = mips_elf_hash_table (info);
1587
1588 /* Create a unique name for the new section. */
1589 name = bfd_malloc (11 + sizeof (".text.stub."));
1590 if (name == NULL)
1591 return FALSE;
1592 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1593
1594 /* Create the section. */
1595 input_section = stub->h->root.root.u.def.section;
1596 s = htab->add_stub_section (name, input_section,
1597 input_section->output_section);
1598 if (s == NULL)
1599 return FALSE;
1600
1601 /* Make sure that any padding goes before the stub. */
1602 align = input_section->alignment_power;
1603 if (!bfd_set_section_alignment (s->owner, s, align))
1604 return FALSE;
1605 if (align > 3)
1606 s->size = (1 << align) - 8;
1607
1608 /* Create a symbol for the stub. */
1609 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1610 stub->stub_section = s;
1611 stub->offset = s->size;
1612
1613 /* Allocate room for it. */
1614 s->size += 8;
1615 return TRUE;
1616}
1617
1618/* STUB describes an la25 stub that we have decided to implement
1619 with a separate trampoline. Allocate room for it and redirect
1620 the function symbol to it. */
1621
1622static bfd_boolean
1623mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1624 struct bfd_link_info *info)
1625{
1626 struct mips_elf_link_hash_table *htab;
1627 asection *s;
1628
1629 htab = mips_elf_hash_table (info);
1630
1631 /* Create a trampoline section, if we haven't already. */
1632 s = htab->strampoline;
1633 if (s == NULL)
1634 {
1635 asection *input_section = stub->h->root.root.u.def.section;
1636 s = htab->add_stub_section (".text", NULL,
1637 input_section->output_section);
1638 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1639 return FALSE;
1640 htab->strampoline = s;
1641 }
1642
1643 /* Create a symbol for the stub. */
1644 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1645 stub->stub_section = s;
1646 stub->offset = s->size;
1647
1648 /* Allocate room for it. */
1649 s->size += 16;
1650 return TRUE;
1651}
1652
1653/* H describes a symbol that needs an la25 stub. Make sure that an
1654 appropriate stub exists and point H at it. */
1655
1656static bfd_boolean
1657mips_elf_add_la25_stub (struct bfd_link_info *info,
1658 struct mips_elf_link_hash_entry *h)
1659{
1660 struct mips_elf_link_hash_table *htab;
1661 struct mips_elf_la25_stub search, *stub;
1662 bfd_boolean use_trampoline_p;
1663 asection *s;
1664 bfd_vma value;
1665 void **slot;
1666
1667 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1668 of the section and if we would need no more than 2 nops. */
1669 s = h->root.root.u.def.section;
1670 value = h->root.root.u.def.value;
1671 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1672
1673 /* Describe the stub we want. */
1674 search.stub_section = NULL;
1675 search.offset = 0;
1676 search.h = h;
1677
1678 /* See if we've already created an equivalent stub. */
1679 htab = mips_elf_hash_table (info);
1680 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1681 if (slot == NULL)
1682 return FALSE;
1683
1684 stub = (struct mips_elf_la25_stub *) *slot;
1685 if (stub != NULL)
1686 {
1687 /* We can reuse the existing stub. */
1688 h->la25_stub = stub;
1689 return TRUE;
1690 }
1691
1692 /* Create a permanent copy of ENTRY and add it to the hash table. */
1693 stub = bfd_malloc (sizeof (search));
1694 if (stub == NULL)
1695 return FALSE;
1696 *stub = search;
1697 *slot = stub;
1698
1699 h->la25_stub = stub;
1700 return (use_trampoline_p
1701 ? mips_elf_add_la25_trampoline (stub, info)
1702 : mips_elf_add_la25_intro (stub, info));
1703}
1704
1705/* A mips_elf_link_hash_traverse callback that is called before sizing
1706 sections. DATA points to a mips_htab_traverse_info structure. */
1707
1708static bfd_boolean
1709mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1710{
1711 struct mips_htab_traverse_info *hti;
1712
1713 hti = (struct mips_htab_traverse_info *) data;
1714 if (h->root.root.type == bfd_link_hash_warning)
1715 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1716
1717 if (!hti->info->relocatable)
1718 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1719
861fb55a
DJ
1720 if (mips_elf_local_pic_function_p (h))
1721 {
1722 /* H is a function that might need $25 to be valid on entry.
1723 If we're creating a non-PIC relocatable object, mark H as
1724 being PIC. If we're creating a non-relocatable object with
1725 non-PIC branches and jumps to H, make sure that H has an la25
1726 stub. */
1727 if (hti->info->relocatable)
1728 {
1729 if (!PIC_OBJECT_P (hti->output_bfd))
1730 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1731 }
1732 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1733 {
1734 hti->error = TRUE;
1735 return FALSE;
1736 }
1737 }
b34976b6 1738 return TRUE;
b49e97c9
TS
1739}
1740\f
d6f16593
MR
1741/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1742 Most mips16 instructions are 16 bits, but these instructions
1743 are 32 bits.
1744
1745 The format of these instructions is:
1746
1747 +--------------+--------------------------------+
1748 | JALX | X| Imm 20:16 | Imm 25:21 |
1749 +--------------+--------------------------------+
1750 | Immediate 15:0 |
1751 +-----------------------------------------------+
1752
1753 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1754 Note that the immediate value in the first word is swapped.
1755
1756 When producing a relocatable object file, R_MIPS16_26 is
1757 handled mostly like R_MIPS_26. In particular, the addend is
1758 stored as a straight 26-bit value in a 32-bit instruction.
1759 (gas makes life simpler for itself by never adjusting a
1760 R_MIPS16_26 reloc to be against a section, so the addend is
1761 always zero). However, the 32 bit instruction is stored as 2
1762 16-bit values, rather than a single 32-bit value. In a
1763 big-endian file, the result is the same; in a little-endian
1764 file, the two 16-bit halves of the 32 bit value are swapped.
1765 This is so that a disassembler can recognize the jal
1766 instruction.
1767
1768 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1769 instruction stored as two 16-bit values. The addend A is the
1770 contents of the targ26 field. The calculation is the same as
1771 R_MIPS_26. When storing the calculated value, reorder the
1772 immediate value as shown above, and don't forget to store the
1773 value as two 16-bit values.
1774
1775 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1776 defined as
1777
1778 big-endian:
1779 +--------+----------------------+
1780 | | |
1781 | | targ26-16 |
1782 |31 26|25 0|
1783 +--------+----------------------+
1784
1785 little-endian:
1786 +----------+------+-------------+
1787 | | | |
1788 | sub1 | | sub2 |
1789 |0 9|10 15|16 31|
1790 +----------+--------------------+
1791 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1792 ((sub1 << 16) | sub2)).
1793
1794 When producing a relocatable object file, the calculation is
1795 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1796 When producing a fully linked file, the calculation is
1797 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1798 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1799
738e5348
RS
1800 The table below lists the other MIPS16 instruction relocations.
1801 Each one is calculated in the same way as the non-MIPS16 relocation
1802 given on the right, but using the extended MIPS16 layout of 16-bit
1803 immediate fields:
1804
1805 R_MIPS16_GPREL R_MIPS_GPREL16
1806 R_MIPS16_GOT16 R_MIPS_GOT16
1807 R_MIPS16_CALL16 R_MIPS_CALL16
1808 R_MIPS16_HI16 R_MIPS_HI16
1809 R_MIPS16_LO16 R_MIPS_LO16
1810
1811 A typical instruction will have a format like this:
d6f16593
MR
1812
1813 +--------------+--------------------------------+
1814 | EXTEND | Imm 10:5 | Imm 15:11 |
1815 +--------------+--------------------------------+
1816 | Major | rx | ry | Imm 4:0 |
1817 +--------------+--------------------------------+
1818
1819 EXTEND is the five bit value 11110. Major is the instruction
1820 opcode.
1821
738e5348
RS
1822 All we need to do here is shuffle the bits appropriately.
1823 As above, the two 16-bit halves must be swapped on a
1824 little-endian system. */
1825
1826static inline bfd_boolean
1827mips16_reloc_p (int r_type)
1828{
1829 switch (r_type)
1830 {
1831 case R_MIPS16_26:
1832 case R_MIPS16_GPREL:
1833 case R_MIPS16_GOT16:
1834 case R_MIPS16_CALL16:
1835 case R_MIPS16_HI16:
1836 case R_MIPS16_LO16:
1837 return TRUE;
1838
1839 default:
1840 return FALSE;
1841 }
1842}
1843
1844static inline bfd_boolean
1845got16_reloc_p (int r_type)
1846{
1847 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1848}
1849
1850static inline bfd_boolean
1851call16_reloc_p (int r_type)
1852{
1853 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1854}
1855
1856static inline bfd_boolean
1857hi16_reloc_p (int r_type)
1858{
1859 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1860}
d6f16593 1861
738e5348
RS
1862static inline bfd_boolean
1863lo16_reloc_p (int r_type)
1864{
1865 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1866}
1867
1868static inline bfd_boolean
1869mips16_call_reloc_p (int r_type)
1870{
1871 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1872}
d6f16593 1873
d6f16593
MR
1874void
1875_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1876 bfd_boolean jal_shuffle, bfd_byte *data)
1877{
1878 bfd_vma extend, insn, val;
1879
738e5348 1880 if (!mips16_reloc_p (r_type))
d6f16593
MR
1881 return;
1882
1883 /* Pick up the mips16 extend instruction and the real instruction. */
1884 extend = bfd_get_16 (abfd, data);
1885 insn = bfd_get_16 (abfd, data + 2);
1886 if (r_type == R_MIPS16_26)
1887 {
1888 if (jal_shuffle)
1889 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1890 | ((extend & 0x1f) << 21) | insn;
1891 else
1892 val = extend << 16 | insn;
1893 }
1894 else
1895 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1896 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1897 bfd_put_32 (abfd, val, data);
1898}
1899
1900void
1901_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1902 bfd_boolean jal_shuffle, bfd_byte *data)
1903{
1904 bfd_vma extend, insn, val;
1905
738e5348 1906 if (!mips16_reloc_p (r_type))
d6f16593
MR
1907 return;
1908
1909 val = bfd_get_32 (abfd, data);
1910 if (r_type == R_MIPS16_26)
1911 {
1912 if (jal_shuffle)
1913 {
1914 insn = val & 0xffff;
1915 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1916 | ((val >> 21) & 0x1f);
1917 }
1918 else
1919 {
1920 insn = val & 0xffff;
1921 extend = val >> 16;
1922 }
1923 }
1924 else
1925 {
1926 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1927 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1928 }
1929 bfd_put_16 (abfd, insn, data + 2);
1930 bfd_put_16 (abfd, extend, data);
1931}
1932
b49e97c9 1933bfd_reloc_status_type
9719ad41
RS
1934_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1935 arelent *reloc_entry, asection *input_section,
1936 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
1937{
1938 bfd_vma relocation;
a7ebbfdf 1939 bfd_signed_vma val;
30ac9238 1940 bfd_reloc_status_type status;
b49e97c9
TS
1941
1942 if (bfd_is_com_section (symbol->section))
1943 relocation = 0;
1944 else
1945 relocation = symbol->value;
1946
1947 relocation += symbol->section->output_section->vma;
1948 relocation += symbol->section->output_offset;
1949
07515404 1950 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
1951 return bfd_reloc_outofrange;
1952
b49e97c9 1953 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
1954 val = reloc_entry->addend;
1955
30ac9238 1956 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 1957
b49e97c9 1958 /* Adjust val for the final section location and GP value. If we
1049f94e 1959 are producing relocatable output, we don't want to do this for
b49e97c9 1960 an external symbol. */
1049f94e 1961 if (! relocatable
b49e97c9
TS
1962 || (symbol->flags & BSF_SECTION_SYM) != 0)
1963 val += relocation - gp;
1964
a7ebbfdf
TS
1965 if (reloc_entry->howto->partial_inplace)
1966 {
30ac9238
RS
1967 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1968 (bfd_byte *) data
1969 + reloc_entry->address);
1970 if (status != bfd_reloc_ok)
1971 return status;
a7ebbfdf
TS
1972 }
1973 else
1974 reloc_entry->addend = val;
b49e97c9 1975
1049f94e 1976 if (relocatable)
b49e97c9 1977 reloc_entry->address += input_section->output_offset;
30ac9238
RS
1978
1979 return bfd_reloc_ok;
1980}
1981
1982/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1983 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1984 that contains the relocation field and DATA points to the start of
1985 INPUT_SECTION. */
1986
1987struct mips_hi16
1988{
1989 struct mips_hi16 *next;
1990 bfd_byte *data;
1991 asection *input_section;
1992 arelent rel;
1993};
1994
1995/* FIXME: This should not be a static variable. */
1996
1997static struct mips_hi16 *mips_hi16_list;
1998
1999/* A howto special_function for REL *HI16 relocations. We can only
2000 calculate the correct value once we've seen the partnering
2001 *LO16 relocation, so just save the information for later.
2002
2003 The ABI requires that the *LO16 immediately follow the *HI16.
2004 However, as a GNU extension, we permit an arbitrary number of
2005 *HI16s to be associated with a single *LO16. This significantly
2006 simplies the relocation handling in gcc. */
2007
2008bfd_reloc_status_type
2009_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2010 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2011 asection *input_section, bfd *output_bfd,
2012 char **error_message ATTRIBUTE_UNUSED)
2013{
2014 struct mips_hi16 *n;
2015
07515404 2016 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2017 return bfd_reloc_outofrange;
2018
2019 n = bfd_malloc (sizeof *n);
2020 if (n == NULL)
2021 return bfd_reloc_outofrange;
2022
2023 n->next = mips_hi16_list;
2024 n->data = data;
2025 n->input_section = input_section;
2026 n->rel = *reloc_entry;
2027 mips_hi16_list = n;
2028
2029 if (output_bfd != NULL)
2030 reloc_entry->address += input_section->output_offset;
2031
2032 return bfd_reloc_ok;
2033}
2034
738e5348 2035/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2036 like any other 16-bit relocation when applied to global symbols, but is
2037 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2038
2039bfd_reloc_status_type
2040_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2041 void *data, asection *input_section,
2042 bfd *output_bfd, char **error_message)
2043{
2044 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2045 || bfd_is_und_section (bfd_get_section (symbol))
2046 || bfd_is_com_section (bfd_get_section (symbol)))
2047 /* The relocation is against a global symbol. */
2048 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2049 input_section, output_bfd,
2050 error_message);
2051
2052 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2053 input_section, output_bfd, error_message);
2054}
2055
2056/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2057 is a straightforward 16 bit inplace relocation, but we must deal with
2058 any partnering high-part relocations as well. */
2059
2060bfd_reloc_status_type
2061_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2062 void *data, asection *input_section,
2063 bfd *output_bfd, char **error_message)
2064{
2065 bfd_vma vallo;
d6f16593 2066 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2067
07515404 2068 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2069 return bfd_reloc_outofrange;
2070
d6f16593
MR
2071 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2072 location);
2073 vallo = bfd_get_32 (abfd, location);
2074 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2075 location);
2076
30ac9238
RS
2077 while (mips_hi16_list != NULL)
2078 {
2079 bfd_reloc_status_type ret;
2080 struct mips_hi16 *hi;
2081
2082 hi = mips_hi16_list;
2083
738e5348
RS
2084 /* R_MIPS*_GOT16 relocations are something of a special case. We
2085 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2086 relocation (with a rightshift of 16). However, since GOT16
2087 relocations can also be used with global symbols, their howto
2088 has a rightshift of 0. */
2089 if (hi->rel.howto->type == R_MIPS_GOT16)
2090 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2091 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2092 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
30ac9238
RS
2093
2094 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2095 carry or borrow will induce a change of +1 or -1 in the high part. */
2096 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2097
30ac9238
RS
2098 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2099 hi->input_section, output_bfd,
2100 error_message);
2101 if (ret != bfd_reloc_ok)
2102 return ret;
2103
2104 mips_hi16_list = hi->next;
2105 free (hi);
2106 }
2107
2108 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2109 input_section, output_bfd,
2110 error_message);
2111}
2112
2113/* A generic howto special_function. This calculates and installs the
2114 relocation itself, thus avoiding the oft-discussed problems in
2115 bfd_perform_relocation and bfd_install_relocation. */
2116
2117bfd_reloc_status_type
2118_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2119 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2120 asection *input_section, bfd *output_bfd,
2121 char **error_message ATTRIBUTE_UNUSED)
2122{
2123 bfd_signed_vma val;
2124 bfd_reloc_status_type status;
2125 bfd_boolean relocatable;
2126
2127 relocatable = (output_bfd != NULL);
2128
07515404 2129 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2130 return bfd_reloc_outofrange;
2131
2132 /* Build up the field adjustment in VAL. */
2133 val = 0;
2134 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2135 {
2136 /* Either we're calculating the final field value or we have a
2137 relocation against a section symbol. Add in the section's
2138 offset or address. */
2139 val += symbol->section->output_section->vma;
2140 val += symbol->section->output_offset;
2141 }
2142
2143 if (!relocatable)
2144 {
2145 /* We're calculating the final field value. Add in the symbol's value
2146 and, if pc-relative, subtract the address of the field itself. */
2147 val += symbol->value;
2148 if (reloc_entry->howto->pc_relative)
2149 {
2150 val -= input_section->output_section->vma;
2151 val -= input_section->output_offset;
2152 val -= reloc_entry->address;
2153 }
2154 }
2155
2156 /* VAL is now the final adjustment. If we're keeping this relocation
2157 in the output file, and if the relocation uses a separate addend,
2158 we just need to add VAL to that addend. Otherwise we need to add
2159 VAL to the relocation field itself. */
2160 if (relocatable && !reloc_entry->howto->partial_inplace)
2161 reloc_entry->addend += val;
2162 else
2163 {
d6f16593
MR
2164 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2165
30ac9238
RS
2166 /* Add in the separate addend, if any. */
2167 val += reloc_entry->addend;
2168
2169 /* Add VAL to the relocation field. */
d6f16593
MR
2170 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2171 location);
30ac9238 2172 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593
MR
2173 location);
2174 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2175 location);
2176
30ac9238
RS
2177 if (status != bfd_reloc_ok)
2178 return status;
2179 }
2180
2181 if (relocatable)
2182 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2183
2184 return bfd_reloc_ok;
2185}
2186\f
2187/* Swap an entry in a .gptab section. Note that these routines rely
2188 on the equivalence of the two elements of the union. */
2189
2190static void
9719ad41
RS
2191bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2192 Elf32_gptab *in)
b49e97c9
TS
2193{
2194 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2195 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2196}
2197
2198static void
9719ad41
RS
2199bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2200 Elf32_External_gptab *ex)
b49e97c9
TS
2201{
2202 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2203 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2204}
2205
2206static void
9719ad41
RS
2207bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2208 Elf32_External_compact_rel *ex)
b49e97c9
TS
2209{
2210 H_PUT_32 (abfd, in->id1, ex->id1);
2211 H_PUT_32 (abfd, in->num, ex->num);
2212 H_PUT_32 (abfd, in->id2, ex->id2);
2213 H_PUT_32 (abfd, in->offset, ex->offset);
2214 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2215 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2216}
2217
2218static void
9719ad41
RS
2219bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2220 Elf32_External_crinfo *ex)
b49e97c9
TS
2221{
2222 unsigned long l;
2223
2224 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2225 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2226 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2227 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2228 H_PUT_32 (abfd, l, ex->info);
2229 H_PUT_32 (abfd, in->konst, ex->konst);
2230 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2231}
b49e97c9
TS
2232\f
2233/* A .reginfo section holds a single Elf32_RegInfo structure. These
2234 routines swap this structure in and out. They are used outside of
2235 BFD, so they are globally visible. */
2236
2237void
9719ad41
RS
2238bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2239 Elf32_RegInfo *in)
b49e97c9
TS
2240{
2241 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2242 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2243 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2244 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2245 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2246 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2247}
2248
2249void
9719ad41
RS
2250bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2251 Elf32_External_RegInfo *ex)
b49e97c9
TS
2252{
2253 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2254 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2255 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2256 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2257 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2258 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2259}
2260
2261/* In the 64 bit ABI, the .MIPS.options section holds register
2262 information in an Elf64_Reginfo structure. These routines swap
2263 them in and out. They are globally visible because they are used
2264 outside of BFD. These routines are here so that gas can call them
2265 without worrying about whether the 64 bit ABI has been included. */
2266
2267void
9719ad41
RS
2268bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2269 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2270{
2271 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2272 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2273 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2274 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2275 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2276 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2277 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2278}
2279
2280void
9719ad41
RS
2281bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2282 Elf64_External_RegInfo *ex)
b49e97c9
TS
2283{
2284 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2285 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2286 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2287 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2288 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2289 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2290 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2291}
2292
2293/* Swap in an options header. */
2294
2295void
9719ad41
RS
2296bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2297 Elf_Internal_Options *in)
b49e97c9
TS
2298{
2299 in->kind = H_GET_8 (abfd, ex->kind);
2300 in->size = H_GET_8 (abfd, ex->size);
2301 in->section = H_GET_16 (abfd, ex->section);
2302 in->info = H_GET_32 (abfd, ex->info);
2303}
2304
2305/* Swap out an options header. */
2306
2307void
9719ad41
RS
2308bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2309 Elf_External_Options *ex)
b49e97c9
TS
2310{
2311 H_PUT_8 (abfd, in->kind, ex->kind);
2312 H_PUT_8 (abfd, in->size, ex->size);
2313 H_PUT_16 (abfd, in->section, ex->section);
2314 H_PUT_32 (abfd, in->info, ex->info);
2315}
2316\f
2317/* This function is called via qsort() to sort the dynamic relocation
2318 entries by increasing r_symndx value. */
2319
2320static int
9719ad41 2321sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2322{
947216bf
AM
2323 Elf_Internal_Rela int_reloc1;
2324 Elf_Internal_Rela int_reloc2;
6870500c 2325 int diff;
b49e97c9 2326
947216bf
AM
2327 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2328 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2329
6870500c
RS
2330 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2331 if (diff != 0)
2332 return diff;
2333
2334 if (int_reloc1.r_offset < int_reloc2.r_offset)
2335 return -1;
2336 if (int_reloc1.r_offset > int_reloc2.r_offset)
2337 return 1;
2338 return 0;
b49e97c9
TS
2339}
2340
f4416af6
AO
2341/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2342
2343static int
7e3102a7
AM
2344sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2345 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2346{
7e3102a7 2347#ifdef BFD64
f4416af6
AO
2348 Elf_Internal_Rela int_reloc1[3];
2349 Elf_Internal_Rela int_reloc2[3];
2350
2351 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2352 (reldyn_sorting_bfd, arg1, int_reloc1);
2353 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2354 (reldyn_sorting_bfd, arg2, int_reloc2);
2355
6870500c
RS
2356 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2357 return -1;
2358 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2359 return 1;
2360
2361 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2362 return -1;
2363 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2364 return 1;
2365 return 0;
7e3102a7
AM
2366#else
2367 abort ();
2368#endif
f4416af6
AO
2369}
2370
2371
b49e97c9
TS
2372/* This routine is used to write out ECOFF debugging external symbol
2373 information. It is called via mips_elf_link_hash_traverse. The
2374 ECOFF external symbol information must match the ELF external
2375 symbol information. Unfortunately, at this point we don't know
2376 whether a symbol is required by reloc information, so the two
2377 tables may wind up being different. We must sort out the external
2378 symbol information before we can set the final size of the .mdebug
2379 section, and we must set the size of the .mdebug section before we
2380 can relocate any sections, and we can't know which symbols are
2381 required by relocation until we relocate the sections.
2382 Fortunately, it is relatively unlikely that any symbol will be
2383 stripped but required by a reloc. In particular, it can not happen
2384 when generating a final executable. */
2385
b34976b6 2386static bfd_boolean
9719ad41 2387mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2388{
9719ad41 2389 struct extsym_info *einfo = data;
b34976b6 2390 bfd_boolean strip;
b49e97c9
TS
2391 asection *sec, *output_section;
2392
2393 if (h->root.root.type == bfd_link_hash_warning)
2394 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2395
2396 if (h->root.indx == -2)
b34976b6 2397 strip = FALSE;
f5385ebf 2398 else if ((h->root.def_dynamic
77cfaee6
AM
2399 || h->root.ref_dynamic
2400 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2401 && !h->root.def_regular
2402 && !h->root.ref_regular)
b34976b6 2403 strip = TRUE;
b49e97c9
TS
2404 else if (einfo->info->strip == strip_all
2405 || (einfo->info->strip == strip_some
2406 && bfd_hash_lookup (einfo->info->keep_hash,
2407 h->root.root.root.string,
b34976b6
AM
2408 FALSE, FALSE) == NULL))
2409 strip = TRUE;
b49e97c9 2410 else
b34976b6 2411 strip = FALSE;
b49e97c9
TS
2412
2413 if (strip)
b34976b6 2414 return TRUE;
b49e97c9
TS
2415
2416 if (h->esym.ifd == -2)
2417 {
2418 h->esym.jmptbl = 0;
2419 h->esym.cobol_main = 0;
2420 h->esym.weakext = 0;
2421 h->esym.reserved = 0;
2422 h->esym.ifd = ifdNil;
2423 h->esym.asym.value = 0;
2424 h->esym.asym.st = stGlobal;
2425
2426 if (h->root.root.type == bfd_link_hash_undefined
2427 || h->root.root.type == bfd_link_hash_undefweak)
2428 {
2429 const char *name;
2430
2431 /* Use undefined class. Also, set class and type for some
2432 special symbols. */
2433 name = h->root.root.root.string;
2434 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2435 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2436 {
2437 h->esym.asym.sc = scData;
2438 h->esym.asym.st = stLabel;
2439 h->esym.asym.value = 0;
2440 }
2441 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2442 {
2443 h->esym.asym.sc = scAbs;
2444 h->esym.asym.st = stLabel;
2445 h->esym.asym.value =
2446 mips_elf_hash_table (einfo->info)->procedure_count;
2447 }
4a14403c 2448 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
2449 {
2450 h->esym.asym.sc = scAbs;
2451 h->esym.asym.st = stLabel;
2452 h->esym.asym.value = elf_gp (einfo->abfd);
2453 }
2454 else
2455 h->esym.asym.sc = scUndefined;
2456 }
2457 else if (h->root.root.type != bfd_link_hash_defined
2458 && h->root.root.type != bfd_link_hash_defweak)
2459 h->esym.asym.sc = scAbs;
2460 else
2461 {
2462 const char *name;
2463
2464 sec = h->root.root.u.def.section;
2465 output_section = sec->output_section;
2466
2467 /* When making a shared library and symbol h is the one from
2468 the another shared library, OUTPUT_SECTION may be null. */
2469 if (output_section == NULL)
2470 h->esym.asym.sc = scUndefined;
2471 else
2472 {
2473 name = bfd_section_name (output_section->owner, output_section);
2474
2475 if (strcmp (name, ".text") == 0)
2476 h->esym.asym.sc = scText;
2477 else if (strcmp (name, ".data") == 0)
2478 h->esym.asym.sc = scData;
2479 else if (strcmp (name, ".sdata") == 0)
2480 h->esym.asym.sc = scSData;
2481 else if (strcmp (name, ".rodata") == 0
2482 || strcmp (name, ".rdata") == 0)
2483 h->esym.asym.sc = scRData;
2484 else if (strcmp (name, ".bss") == 0)
2485 h->esym.asym.sc = scBss;
2486 else if (strcmp (name, ".sbss") == 0)
2487 h->esym.asym.sc = scSBss;
2488 else if (strcmp (name, ".init") == 0)
2489 h->esym.asym.sc = scInit;
2490 else if (strcmp (name, ".fini") == 0)
2491 h->esym.asym.sc = scFini;
2492 else
2493 h->esym.asym.sc = scAbs;
2494 }
2495 }
2496
2497 h->esym.asym.reserved = 0;
2498 h->esym.asym.index = indexNil;
2499 }
2500
2501 if (h->root.root.type == bfd_link_hash_common)
2502 h->esym.asym.value = h->root.root.u.c.size;
2503 else if (h->root.root.type == bfd_link_hash_defined
2504 || h->root.root.type == bfd_link_hash_defweak)
2505 {
2506 if (h->esym.asym.sc == scCommon)
2507 h->esym.asym.sc = scBss;
2508 else if (h->esym.asym.sc == scSCommon)
2509 h->esym.asym.sc = scSBss;
2510
2511 sec = h->root.root.u.def.section;
2512 output_section = sec->output_section;
2513 if (output_section != NULL)
2514 h->esym.asym.value = (h->root.root.u.def.value
2515 + sec->output_offset
2516 + output_section->vma);
2517 else
2518 h->esym.asym.value = 0;
2519 }
33bb52fb 2520 else
b49e97c9
TS
2521 {
2522 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2523
2524 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2525 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2526
33bb52fb 2527 if (hd->needs_lazy_stub)
b49e97c9
TS
2528 {
2529 /* Set type and value for a symbol with a function stub. */
2530 h->esym.asym.st = stProc;
2531 sec = hd->root.root.u.def.section;
2532 if (sec == NULL)
2533 h->esym.asym.value = 0;
2534 else
2535 {
2536 output_section = sec->output_section;
2537 if (output_section != NULL)
2538 h->esym.asym.value = (hd->root.plt.offset
2539 + sec->output_offset
2540 + output_section->vma);
2541 else
2542 h->esym.asym.value = 0;
2543 }
b49e97c9
TS
2544 }
2545 }
2546
2547 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2548 h->root.root.root.string,
2549 &h->esym))
2550 {
b34976b6
AM
2551 einfo->failed = TRUE;
2552 return FALSE;
b49e97c9
TS
2553 }
2554
b34976b6 2555 return TRUE;
b49e97c9
TS
2556}
2557
2558/* A comparison routine used to sort .gptab entries. */
2559
2560static int
9719ad41 2561gptab_compare (const void *p1, const void *p2)
b49e97c9 2562{
9719ad41
RS
2563 const Elf32_gptab *a1 = p1;
2564 const Elf32_gptab *a2 = p2;
b49e97c9
TS
2565
2566 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2567}
2568\f
b15e6682 2569/* Functions to manage the got entry hash table. */
f4416af6
AO
2570
2571/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2572 hash number. */
2573
2574static INLINE hashval_t
9719ad41 2575mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
2576{
2577#ifdef BFD64
2578 return addr + (addr >> 32);
2579#else
2580 return addr;
2581#endif
2582}
2583
2584/* got_entries only match if they're identical, except for gotidx, so
2585 use all fields to compute the hash, and compare the appropriate
2586 union members. */
2587
b15e6682 2588static hashval_t
9719ad41 2589mips_elf_got_entry_hash (const void *entry_)
b15e6682
AO
2590{
2591 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2592
38985a1c 2593 return entry->symndx
0f20cc35 2594 + ((entry->tls_type & GOT_TLS_LDM) << 17)
f4416af6 2595 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
38985a1c
AO
2596 : entry->abfd->id
2597 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2598 : entry->d.h->root.root.root.hash));
b15e6682
AO
2599}
2600
2601static int
9719ad41 2602mips_elf_got_entry_eq (const void *entry1, const void *entry2)
b15e6682
AO
2603{
2604 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2605 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2606
0f20cc35
DJ
2607 /* An LDM entry can only match another LDM entry. */
2608 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2609 return 0;
2610
b15e6682 2611 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
f4416af6
AO
2612 && (! e1->abfd ? e1->d.address == e2->d.address
2613 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2614 : e1->d.h == e2->d.h);
2615}
2616
2617/* multi_got_entries are still a match in the case of global objects,
2618 even if the input bfd in which they're referenced differs, so the
2619 hash computation and compare functions are adjusted
2620 accordingly. */
2621
2622static hashval_t
9719ad41 2623mips_elf_multi_got_entry_hash (const void *entry_)
f4416af6
AO
2624{
2625 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2626
2627 return entry->symndx
2628 + (! entry->abfd
2629 ? mips_elf_hash_bfd_vma (entry->d.address)
2630 : entry->symndx >= 0
0f20cc35
DJ
2631 ? ((entry->tls_type & GOT_TLS_LDM)
2632 ? (GOT_TLS_LDM << 17)
2633 : (entry->abfd->id
2634 + mips_elf_hash_bfd_vma (entry->d.addend)))
f4416af6
AO
2635 : entry->d.h->root.root.root.hash);
2636}
2637
2638static int
9719ad41 2639mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2640{
2641 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2642 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2643
0f20cc35
DJ
2644 /* Any two LDM entries match. */
2645 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2646 return 1;
2647
2648 /* Nothing else matches an LDM entry. */
2649 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2650 return 0;
2651
f4416af6
AO
2652 return e1->symndx == e2->symndx
2653 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2654 : e1->abfd == NULL || e2->abfd == NULL
2655 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2656 : e1->d.h == e2->d.h);
b15e6682 2657}
c224138d
RS
2658
2659static hashval_t
2660mips_got_page_entry_hash (const void *entry_)
2661{
2662 const struct mips_got_page_entry *entry;
2663
2664 entry = (const struct mips_got_page_entry *) entry_;
2665 return entry->abfd->id + entry->symndx;
2666}
2667
2668static int
2669mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2670{
2671 const struct mips_got_page_entry *entry1, *entry2;
2672
2673 entry1 = (const struct mips_got_page_entry *) entry1_;
2674 entry2 = (const struct mips_got_page_entry *) entry2_;
2675 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2676}
b15e6682 2677\f
0a44bf69
RS
2678/* Return the dynamic relocation section. If it doesn't exist, try to
2679 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2680 if creation fails. */
f4416af6
AO
2681
2682static asection *
0a44bf69 2683mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 2684{
0a44bf69 2685 const char *dname;
f4416af6 2686 asection *sreloc;
0a44bf69 2687 bfd *dynobj;
f4416af6 2688
0a44bf69
RS
2689 dname = MIPS_ELF_REL_DYN_NAME (info);
2690 dynobj = elf_hash_table (info)->dynobj;
f4416af6
AO
2691 sreloc = bfd_get_section_by_name (dynobj, dname);
2692 if (sreloc == NULL && create_p)
2693 {
3496cb2a
L
2694 sreloc = bfd_make_section_with_flags (dynobj, dname,
2695 (SEC_ALLOC
2696 | SEC_LOAD
2697 | SEC_HAS_CONTENTS
2698 | SEC_IN_MEMORY
2699 | SEC_LINKER_CREATED
2700 | SEC_READONLY));
f4416af6 2701 if (sreloc == NULL
f4416af6 2702 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 2703 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
2704 return NULL;
2705 }
2706 return sreloc;
2707}
2708
0f20cc35
DJ
2709/* Count the number of relocations needed for a TLS GOT entry, with
2710 access types from TLS_TYPE, and symbol H (or a local symbol if H
2711 is NULL). */
2712
2713static int
2714mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2715 struct elf_link_hash_entry *h)
2716{
2717 int indx = 0;
2718 int ret = 0;
2719 bfd_boolean need_relocs = FALSE;
2720 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2721
2722 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2723 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2724 indx = h->dynindx;
2725
2726 if ((info->shared || indx != 0)
2727 && (h == NULL
2728 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2729 || h->root.type != bfd_link_hash_undefweak))
2730 need_relocs = TRUE;
2731
2732 if (!need_relocs)
2733 return FALSE;
2734
2735 if (tls_type & GOT_TLS_GD)
2736 {
2737 ret++;
2738 if (indx != 0)
2739 ret++;
2740 }
2741
2742 if (tls_type & GOT_TLS_IE)
2743 ret++;
2744
2745 if ((tls_type & GOT_TLS_LDM) && info->shared)
2746 ret++;
2747
2748 return ret;
2749}
2750
2751/* Count the number of TLS relocations required for the GOT entry in
2752 ARG1, if it describes a local symbol. */
2753
2754static int
2755mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2756{
2757 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2758 struct mips_elf_count_tls_arg *arg = arg2;
2759
2760 if (entry->abfd != NULL && entry->symndx != -1)
2761 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2762
2763 return 1;
2764}
2765
2766/* Count the number of TLS GOT entries required for the global (or
2767 forced-local) symbol in ARG1. */
2768
2769static int
2770mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2771{
2772 struct mips_elf_link_hash_entry *hm
2773 = (struct mips_elf_link_hash_entry *) arg1;
2774 struct mips_elf_count_tls_arg *arg = arg2;
2775
2776 if (hm->tls_type & GOT_TLS_GD)
2777 arg->needed += 2;
2778 if (hm->tls_type & GOT_TLS_IE)
2779 arg->needed += 1;
2780
2781 return 1;
2782}
2783
2784/* Count the number of TLS relocations required for the global (or
2785 forced-local) symbol in ARG1. */
2786
2787static int
2788mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2789{
2790 struct mips_elf_link_hash_entry *hm
2791 = (struct mips_elf_link_hash_entry *) arg1;
2792 struct mips_elf_count_tls_arg *arg = arg2;
2793
2794 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2795
2796 return 1;
2797}
2798
2799/* Output a simple dynamic relocation into SRELOC. */
2800
2801static void
2802mips_elf_output_dynamic_relocation (bfd *output_bfd,
2803 asection *sreloc,
861fb55a 2804 unsigned long reloc_index,
0f20cc35
DJ
2805 unsigned long indx,
2806 int r_type,
2807 bfd_vma offset)
2808{
2809 Elf_Internal_Rela rel[3];
2810
2811 memset (rel, 0, sizeof (rel));
2812
2813 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2814 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2815
2816 if (ABI_64_P (output_bfd))
2817 {
2818 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2819 (output_bfd, &rel[0],
2820 (sreloc->contents
861fb55a 2821 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
2822 }
2823 else
2824 bfd_elf32_swap_reloc_out
2825 (output_bfd, &rel[0],
2826 (sreloc->contents
861fb55a 2827 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
2828}
2829
2830/* Initialize a set of TLS GOT entries for one symbol. */
2831
2832static void
2833mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2834 unsigned char *tls_type_p,
2835 struct bfd_link_info *info,
2836 struct mips_elf_link_hash_entry *h,
2837 bfd_vma value)
2838{
23cc69b6 2839 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
2840 int indx;
2841 asection *sreloc, *sgot;
2842 bfd_vma offset, offset2;
0f20cc35
DJ
2843 bfd_boolean need_relocs = FALSE;
2844
23cc69b6
RS
2845 htab = mips_elf_hash_table (info);
2846 sgot = htab->sgot;
0f20cc35
DJ
2847
2848 indx = 0;
2849 if (h != NULL)
2850 {
2851 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2852
2853 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2854 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2855 indx = h->root.dynindx;
2856 }
2857
2858 if (*tls_type_p & GOT_TLS_DONE)
2859 return;
2860
2861 if ((info->shared || indx != 0)
2862 && (h == NULL
2863 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2864 || h->root.type != bfd_link_hash_undefweak))
2865 need_relocs = TRUE;
2866
2867 /* MINUS_ONE means the symbol is not defined in this object. It may not
2868 be defined at all; assume that the value doesn't matter in that
2869 case. Otherwise complain if we would use the value. */
2870 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2871 || h->root.root.type == bfd_link_hash_undefweak);
2872
2873 /* Emit necessary relocations. */
0a44bf69 2874 sreloc = mips_elf_rel_dyn_section (info, FALSE);
0f20cc35
DJ
2875
2876 /* General Dynamic. */
2877 if (*tls_type_p & GOT_TLS_GD)
2878 {
2879 offset = got_offset;
2880 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2881
2882 if (need_relocs)
2883 {
2884 mips_elf_output_dynamic_relocation
861fb55a 2885 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2886 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2887 sgot->output_offset + sgot->output_section->vma + offset);
2888
2889 if (indx)
2890 mips_elf_output_dynamic_relocation
861fb55a 2891 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2892 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2893 sgot->output_offset + sgot->output_section->vma + offset2);
2894 else
2895 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2896 sgot->contents + offset2);
2897 }
2898 else
2899 {
2900 MIPS_ELF_PUT_WORD (abfd, 1,
2901 sgot->contents + offset);
2902 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2903 sgot->contents + offset2);
2904 }
2905
2906 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2907 }
2908
2909 /* Initial Exec model. */
2910 if (*tls_type_p & GOT_TLS_IE)
2911 {
2912 offset = got_offset;
2913
2914 if (need_relocs)
2915 {
2916 if (indx == 0)
2917 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2918 sgot->contents + offset);
2919 else
2920 MIPS_ELF_PUT_WORD (abfd, 0,
2921 sgot->contents + offset);
2922
2923 mips_elf_output_dynamic_relocation
861fb55a 2924 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2925 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2926 sgot->output_offset + sgot->output_section->vma + offset);
2927 }
2928 else
2929 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2930 sgot->contents + offset);
2931 }
2932
2933 if (*tls_type_p & GOT_TLS_LDM)
2934 {
2935 /* The initial offset is zero, and the LD offsets will include the
2936 bias by DTP_OFFSET. */
2937 MIPS_ELF_PUT_WORD (abfd, 0,
2938 sgot->contents + got_offset
2939 + MIPS_ELF_GOT_SIZE (abfd));
2940
2941 if (!info->shared)
2942 MIPS_ELF_PUT_WORD (abfd, 1,
2943 sgot->contents + got_offset);
2944 else
2945 mips_elf_output_dynamic_relocation
861fb55a 2946 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
2947 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2948 sgot->output_offset + sgot->output_section->vma + got_offset);
2949 }
2950
2951 *tls_type_p |= GOT_TLS_DONE;
2952}
2953
2954/* Return the GOT index to use for a relocation of type R_TYPE against
2955 a symbol accessed using TLS_TYPE models. The GOT entries for this
2956 symbol in this GOT start at GOT_INDEX. This function initializes the
2957 GOT entries and corresponding relocations. */
2958
2959static bfd_vma
2960mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2961 int r_type, struct bfd_link_info *info,
2962 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2963{
2964 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2965 || r_type == R_MIPS_TLS_LDM);
2966
2967 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2968
2969 if (r_type == R_MIPS_TLS_GOTTPREL)
2970 {
2971 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2972 if (*tls_type & GOT_TLS_GD)
2973 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2974 else
2975 return got_index;
2976 }
2977
2978 if (r_type == R_MIPS_TLS_GD)
2979 {
2980 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2981 return got_index;
2982 }
2983
2984 if (r_type == R_MIPS_TLS_LDM)
2985 {
2986 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2987 return got_index;
2988 }
2989
2990 return got_index;
2991}
2992
0a44bf69
RS
2993/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2994 for global symbol H. .got.plt comes before the GOT, so the offset
2995 will be negative. */
2996
2997static bfd_vma
2998mips_elf_gotplt_index (struct bfd_link_info *info,
2999 struct elf_link_hash_entry *h)
3000{
3001 bfd_vma plt_index, got_address, got_value;
3002 struct mips_elf_link_hash_table *htab;
3003
3004 htab = mips_elf_hash_table (info);
3005 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3006
861fb55a
DJ
3007 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3008 section starts with reserved entries. */
3009 BFD_ASSERT (htab->is_vxworks);
3010
0a44bf69
RS
3011 /* Calculate the index of the symbol's PLT entry. */
3012 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3013
3014 /* Calculate the address of the associated .got.plt entry. */
3015 got_address = (htab->sgotplt->output_section->vma
3016 + htab->sgotplt->output_offset
3017 + plt_index * 4);
3018
3019 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3020 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3021 + htab->root.hgot->root.u.def.section->output_offset
3022 + htab->root.hgot->root.u.def.value);
3023
3024 return got_address - got_value;
3025}
3026
5c18022e 3027/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3028 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3029 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3030 offset can be found. */
b49e97c9
TS
3031
3032static bfd_vma
9719ad41 3033mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3034 bfd_vma value, unsigned long r_symndx,
0f20cc35 3035 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3036{
a8028dd0 3037 struct mips_elf_link_hash_table *htab;
b15e6682 3038 struct mips_got_entry *entry;
b49e97c9 3039
a8028dd0
RS
3040 htab = mips_elf_hash_table (info);
3041 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3042 r_symndx, h, r_type);
0f20cc35 3043 if (!entry)
b15e6682 3044 return MINUS_ONE;
0f20cc35
DJ
3045
3046 if (TLS_RELOC_P (r_type))
ead49a57 3047 {
a8028dd0 3048 if (entry->symndx == -1 && htab->got_info->next == NULL)
ead49a57
RS
3049 /* A type (3) entry in the single-GOT case. We use the symbol's
3050 hash table entry to track the index. */
3051 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3052 r_type, info, h, value);
3053 else
3054 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3055 r_type, info, h, value);
3056 }
0f20cc35
DJ
3057 else
3058 return entry->gotidx;
b49e97c9
TS
3059}
3060
3061/* Returns the GOT index for the global symbol indicated by H. */
3062
3063static bfd_vma
0f20cc35
DJ
3064mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3065 int r_type, struct bfd_link_info *info)
b49e97c9 3066{
a8028dd0 3067 struct mips_elf_link_hash_table *htab;
91d6fa6a 3068 bfd_vma got_index;
f4416af6 3069 struct mips_got_info *g, *gg;
d0c7ff07 3070 long global_got_dynindx = 0;
b49e97c9 3071
a8028dd0
RS
3072 htab = mips_elf_hash_table (info);
3073 gg = g = htab->got_info;
f4416af6
AO
3074 if (g->bfd2got && ibfd)
3075 {
3076 struct mips_got_entry e, *p;
143d77c5 3077
f4416af6
AO
3078 BFD_ASSERT (h->dynindx >= 0);
3079
3080 g = mips_elf_got_for_ibfd (g, ibfd);
0f20cc35 3081 if (g->next != gg || TLS_RELOC_P (r_type))
f4416af6
AO
3082 {
3083 e.abfd = ibfd;
3084 e.symndx = -1;
3085 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 3086 e.tls_type = 0;
f4416af6 3087
9719ad41 3088 p = htab_find (g->got_entries, &e);
f4416af6
AO
3089
3090 BFD_ASSERT (p->gotidx > 0);
0f20cc35
DJ
3091
3092 if (TLS_RELOC_P (r_type))
3093 {
3094 bfd_vma value = MINUS_ONE;
3095 if ((h->root.type == bfd_link_hash_defined
3096 || h->root.type == bfd_link_hash_defweak)
3097 && h->root.u.def.section->output_section)
3098 value = (h->root.u.def.value
3099 + h->root.u.def.section->output_offset
3100 + h->root.u.def.section->output_section->vma);
3101
3102 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3103 info, e.d.h, value);
3104 }
3105 else
3106 return p->gotidx;
f4416af6
AO
3107 }
3108 }
3109
3110 if (gg->global_gotsym != NULL)
3111 global_got_dynindx = gg->global_gotsym->dynindx;
b49e97c9 3112
0f20cc35
DJ
3113 if (TLS_RELOC_P (r_type))
3114 {
3115 struct mips_elf_link_hash_entry *hm
3116 = (struct mips_elf_link_hash_entry *) h;
3117 bfd_vma value = MINUS_ONE;
3118
3119 if ((h->root.type == bfd_link_hash_defined
3120 || h->root.type == bfd_link_hash_defweak)
3121 && h->root.u.def.section->output_section)
3122 value = (h->root.u.def.value
3123 + h->root.u.def.section->output_offset
3124 + h->root.u.def.section->output_section->vma);
3125
91d6fa6a
NC
3126 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3127 r_type, info, hm, value);
0f20cc35
DJ
3128 }
3129 else
3130 {
3131 /* Once we determine the global GOT entry with the lowest dynamic
3132 symbol table index, we must put all dynamic symbols with greater
3133 indices into the GOT. That makes it easy to calculate the GOT
3134 offset. */
3135 BFD_ASSERT (h->dynindx >= global_got_dynindx);
91d6fa6a
NC
3136 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3137 * MIPS_ELF_GOT_SIZE (abfd));
0f20cc35 3138 }
91d6fa6a 3139 BFD_ASSERT (got_index < htab->sgot->size);
b49e97c9 3140
91d6fa6a 3141 return got_index;
b49e97c9
TS
3142}
3143
5c18022e
RS
3144/* Find a GOT page entry that points to within 32KB of VALUE. These
3145 entries are supposed to be placed at small offsets in the GOT, i.e.,
3146 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3147 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3148 offset of the GOT entry from VALUE. */
b49e97c9
TS
3149
3150static bfd_vma
9719ad41 3151mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3152 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3153{
91d6fa6a 3154 bfd_vma page, got_index;
b15e6682 3155 struct mips_got_entry *entry;
b49e97c9 3156
0a44bf69 3157 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3158 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3159 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3160
b15e6682
AO
3161 if (!entry)
3162 return MINUS_ONE;
143d77c5 3163
91d6fa6a 3164 got_index = entry->gotidx;
b49e97c9
TS
3165
3166 if (offsetp)
f4416af6 3167 *offsetp = value - entry->d.address;
b49e97c9 3168
91d6fa6a 3169 return got_index;
b49e97c9
TS
3170}
3171
738e5348 3172/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
0a44bf69
RS
3173 EXTERNAL is true if the relocation was against a global symbol
3174 that has been forced local. */
b49e97c9
TS
3175
3176static bfd_vma
9719ad41 3177mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3178 bfd_vma value, bfd_boolean external)
b49e97c9 3179{
b15e6682 3180 struct mips_got_entry *entry;
b49e97c9 3181
0a44bf69
RS
3182 /* GOT16 relocations against local symbols are followed by a LO16
3183 relocation; those against global symbols are not. Thus if the
3184 symbol was originally local, the GOT16 relocation should load the
3185 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3186 if (! external)
0a44bf69 3187 value = mips_elf_high (value) << 16;
b49e97c9 3188
738e5348
RS
3189 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3190 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3191 same in all cases. */
a8028dd0
RS
3192 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3193 NULL, R_MIPS_GOT16);
b15e6682
AO
3194 if (entry)
3195 return entry->gotidx;
3196 else
3197 return MINUS_ONE;
b49e97c9
TS
3198}
3199
3200/* Returns the offset for the entry at the INDEXth position
3201 in the GOT. */
3202
3203static bfd_vma
a8028dd0 3204mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3205 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3206{
a8028dd0 3207 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3208 asection *sgot;
3209 bfd_vma gp;
3210
a8028dd0
RS
3211 htab = mips_elf_hash_table (info);
3212 sgot = htab->sgot;
f4416af6 3213 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3214 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3215
91d6fa6a 3216 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3217}
3218
0a44bf69
RS
3219/* Create and return a local GOT entry for VALUE, which was calculated
3220 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3221 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3222 instead. */
b49e97c9 3223
b15e6682 3224static struct mips_got_entry *
0a44bf69 3225mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3226 bfd *ibfd, bfd_vma value,
5c18022e 3227 unsigned long r_symndx,
0f20cc35
DJ
3228 struct mips_elf_link_hash_entry *h,
3229 int r_type)
b49e97c9 3230{
b15e6682 3231 struct mips_got_entry entry, **loc;
f4416af6 3232 struct mips_got_info *g;
0a44bf69
RS
3233 struct mips_elf_link_hash_table *htab;
3234
3235 htab = mips_elf_hash_table (info);
b15e6682 3236
f4416af6
AO
3237 entry.abfd = NULL;
3238 entry.symndx = -1;
3239 entry.d.address = value;
0f20cc35 3240 entry.tls_type = 0;
f4416af6 3241
a8028dd0 3242 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
f4416af6
AO
3243 if (g == NULL)
3244 {
a8028dd0 3245 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
f4416af6
AO
3246 BFD_ASSERT (g != NULL);
3247 }
b15e6682 3248
0f20cc35
DJ
3249 /* We might have a symbol, H, if it has been forced local. Use the
3250 global entry then. It doesn't matter whether an entry is local
3251 or global for TLS, since the dynamic linker does not
3252 automatically relocate TLS GOT entries. */
a008ac03 3253 BFD_ASSERT (h == NULL || h->root.forced_local);
0f20cc35
DJ
3254 if (TLS_RELOC_P (r_type))
3255 {
3256 struct mips_got_entry *p;
3257
3258 entry.abfd = ibfd;
3259 if (r_type == R_MIPS_TLS_LDM)
3260 {
3261 entry.tls_type = GOT_TLS_LDM;
3262 entry.symndx = 0;
3263 entry.d.addend = 0;
3264 }
3265 else if (h == NULL)
3266 {
3267 entry.symndx = r_symndx;
3268 entry.d.addend = 0;
3269 }
3270 else
3271 entry.d.h = h;
3272
3273 p = (struct mips_got_entry *)
3274 htab_find (g->got_entries, &entry);
3275
3276 BFD_ASSERT (p);
3277 return p;
3278 }
3279
b15e6682
AO
3280 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3281 INSERT);
3282 if (*loc)
3283 return *loc;
143d77c5 3284
b15e6682 3285 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
0f20cc35 3286 entry.tls_type = 0;
b15e6682
AO
3287
3288 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3289
3290 if (! *loc)
3291 return NULL;
143d77c5 3292
b15e6682
AO
3293 memcpy (*loc, &entry, sizeof entry);
3294
8275b357 3295 if (g->assigned_gotno > g->local_gotno)
b49e97c9 3296 {
f4416af6 3297 (*loc)->gotidx = -1;
b49e97c9
TS
3298 /* We didn't allocate enough space in the GOT. */
3299 (*_bfd_error_handler)
3300 (_("not enough GOT space for local GOT entries"));
3301 bfd_set_error (bfd_error_bad_value);
b15e6682 3302 return NULL;
b49e97c9
TS
3303 }
3304
3305 MIPS_ELF_PUT_WORD (abfd, value,
a8028dd0 3306 (htab->sgot->contents + entry.gotidx));
b15e6682 3307
5c18022e 3308 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3309 if (htab->is_vxworks)
3310 {
3311 Elf_Internal_Rela outrel;
5c18022e 3312 asection *s;
91d6fa6a 3313 bfd_byte *rloc;
0a44bf69 3314 bfd_vma got_address;
0a44bf69
RS
3315
3316 s = mips_elf_rel_dyn_section (info, FALSE);
a8028dd0
RS
3317 got_address = (htab->sgot->output_section->vma
3318 + htab->sgot->output_offset
0a44bf69
RS
3319 + entry.gotidx);
3320
91d6fa6a 3321 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3322 outrel.r_offset = got_address;
5c18022e
RS
3323 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3324 outrel.r_addend = value;
91d6fa6a 3325 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3326 }
3327
b15e6682 3328 return *loc;
b49e97c9
TS
3329}
3330
d4596a51
RS
3331/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3332 The number might be exact or a worst-case estimate, depending on how
3333 much information is available to elf_backend_omit_section_dynsym at
3334 the current linking stage. */
3335
3336static bfd_size_type
3337count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3338{
3339 bfd_size_type count;
3340
3341 count = 0;
3342 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3343 {
3344 asection *p;
3345 const struct elf_backend_data *bed;
3346
3347 bed = get_elf_backend_data (output_bfd);
3348 for (p = output_bfd->sections; p ; p = p->next)
3349 if ((p->flags & SEC_EXCLUDE) == 0
3350 && (p->flags & SEC_ALLOC) != 0
3351 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3352 ++count;
3353 }
3354 return count;
3355}
3356
b49e97c9 3357/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3358 appear towards the end. */
b49e97c9 3359
b34976b6 3360static bfd_boolean
d4596a51 3361mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3362{
a8028dd0 3363 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3364 struct mips_elf_hash_sort_data hsd;
3365 struct mips_got_info *g;
b49e97c9 3366
d4596a51
RS
3367 if (elf_hash_table (info)->dynsymcount == 0)
3368 return TRUE;
3369
a8028dd0
RS
3370 htab = mips_elf_hash_table (info);
3371 g = htab->got_info;
d4596a51
RS
3372 if (g == NULL)
3373 return TRUE;
f4416af6 3374
b49e97c9 3375 hsd.low = NULL;
23cc69b6
RS
3376 hsd.max_unref_got_dynindx
3377 = hsd.min_got_dynindx
3378 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
d4596a51 3379 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
b49e97c9
TS
3380 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3381 elf_hash_table (info)),
3382 mips_elf_sort_hash_table_f,
3383 &hsd);
3384
3385 /* There should have been enough room in the symbol table to
44c410de 3386 accommodate both the GOT and non-GOT symbols. */
b49e97c9 3387 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
d4596a51
RS
3388 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3389 == elf_hash_table (info)->dynsymcount);
3390 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3391 == g->global_gotno);
b49e97c9
TS
3392
3393 /* Now we know which dynamic symbol has the lowest dynamic symbol
3394 table index in the GOT. */
b49e97c9
TS
3395 g->global_gotsym = hsd.low;
3396
b34976b6 3397 return TRUE;
b49e97c9
TS
3398}
3399
3400/* If H needs a GOT entry, assign it the highest available dynamic
3401 index. Otherwise, assign it the lowest available dynamic
3402 index. */
3403
b34976b6 3404static bfd_boolean
9719ad41 3405mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3406{
9719ad41 3407 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9
TS
3408
3409 if (h->root.root.type == bfd_link_hash_warning)
3410 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3411
3412 /* Symbols without dynamic symbol table entries aren't interesting
3413 at all. */
3414 if (h->root.dynindx == -1)
b34976b6 3415 return TRUE;
b49e97c9 3416
634835ae 3417 switch (h->global_got_area)
f4416af6 3418 {
634835ae
RS
3419 case GGA_NONE:
3420 h->root.dynindx = hsd->max_non_got_dynindx++;
3421 break;
0f20cc35 3422
634835ae 3423 case GGA_NORMAL:
0f20cc35
DJ
3424 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3425
b49e97c9
TS
3426 h->root.dynindx = --hsd->min_got_dynindx;
3427 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3428 break;
3429
3430 case GGA_RELOC_ONLY:
3431 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3432
3433 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3434 hsd->low = (struct elf_link_hash_entry *) h;
3435 h->root.dynindx = hsd->max_unref_got_dynindx++;
3436 break;
b49e97c9
TS
3437 }
3438
b34976b6 3439 return TRUE;
b49e97c9
TS
3440}
3441
3442/* If H is a symbol that needs a global GOT entry, but has a dynamic
3443 symbol table index lower than any we've seen to date, record it for
3444 posterity. */
3445
b34976b6 3446static bfd_boolean
9719ad41
RS
3447mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3448 bfd *abfd, struct bfd_link_info *info,
0f20cc35 3449 unsigned char tls_flag)
b49e97c9 3450{
a8028dd0 3451 struct mips_elf_link_hash_table *htab;
634835ae 3452 struct mips_elf_link_hash_entry *hmips;
f4416af6 3453 struct mips_got_entry entry, **loc;
a8028dd0
RS
3454 struct mips_got_info *g;
3455
3456 htab = mips_elf_hash_table (info);
634835ae 3457 hmips = (struct mips_elf_link_hash_entry *) h;
f4416af6 3458
b49e97c9
TS
3459 /* A global symbol in the GOT must also be in the dynamic symbol
3460 table. */
7c5fcef7
L
3461 if (h->dynindx == -1)
3462 {
3463 switch (ELF_ST_VISIBILITY (h->other))
3464 {
3465 case STV_INTERNAL:
3466 case STV_HIDDEN:
33bb52fb 3467 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
7c5fcef7
L
3468 break;
3469 }
c152c796 3470 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3471 return FALSE;
7c5fcef7 3472 }
b49e97c9 3473
86324f90 3474 /* Make sure we have a GOT to put this entry into. */
a8028dd0 3475 g = htab->got_info;
86324f90
EC
3476 BFD_ASSERT (g != NULL);
3477
f4416af6
AO
3478 entry.abfd = abfd;
3479 entry.symndx = -1;
3480 entry.d.h = (struct mips_elf_link_hash_entry *) h;
0f20cc35 3481 entry.tls_type = 0;
f4416af6
AO
3482
3483 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3484 INSERT);
3485
b49e97c9
TS
3486 /* If we've already marked this entry as needing GOT space, we don't
3487 need to do it again. */
f4416af6 3488 if (*loc)
0f20cc35
DJ
3489 {
3490 (*loc)->tls_type |= tls_flag;
3491 return TRUE;
3492 }
f4416af6
AO
3493
3494 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3495
3496 if (! *loc)
3497 return FALSE;
143d77c5 3498
f4416af6 3499 entry.gotidx = -1;
0f20cc35
DJ
3500 entry.tls_type = tls_flag;
3501
f4416af6
AO
3502 memcpy (*loc, &entry, sizeof entry);
3503
0f20cc35 3504 if (tls_flag == 0)
634835ae 3505 hmips->global_got_area = GGA_NORMAL;
b49e97c9 3506
b34976b6 3507 return TRUE;
b49e97c9 3508}
f4416af6
AO
3509
3510/* Reserve space in G for a GOT entry containing the value of symbol
3511 SYMNDX in input bfd ABDF, plus ADDEND. */
3512
3513static bfd_boolean
9719ad41 3514mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
a8028dd0 3515 struct bfd_link_info *info,
0f20cc35 3516 unsigned char tls_flag)
f4416af6 3517{
a8028dd0
RS
3518 struct mips_elf_link_hash_table *htab;
3519 struct mips_got_info *g;
f4416af6
AO
3520 struct mips_got_entry entry, **loc;
3521
a8028dd0
RS
3522 htab = mips_elf_hash_table (info);
3523 g = htab->got_info;
3524 BFD_ASSERT (g != NULL);
3525
f4416af6
AO
3526 entry.abfd = abfd;
3527 entry.symndx = symndx;
3528 entry.d.addend = addend;
0f20cc35 3529 entry.tls_type = tls_flag;
f4416af6
AO
3530 loc = (struct mips_got_entry **)
3531 htab_find_slot (g->got_entries, &entry, INSERT);
3532
3533 if (*loc)
0f20cc35
DJ
3534 {
3535 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3536 {
3537 g->tls_gotno += 2;
3538 (*loc)->tls_type |= tls_flag;
3539 }
3540 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3541 {
3542 g->tls_gotno += 1;
3543 (*loc)->tls_type |= tls_flag;
3544 }
3545 return TRUE;
3546 }
f4416af6 3547
0f20cc35
DJ
3548 if (tls_flag != 0)
3549 {
3550 entry.gotidx = -1;
3551 entry.tls_type = tls_flag;
3552 if (tls_flag == GOT_TLS_IE)
3553 g->tls_gotno += 1;
3554 else if (tls_flag == GOT_TLS_GD)
3555 g->tls_gotno += 2;
3556 else if (g->tls_ldm_offset == MINUS_ONE)
3557 {
3558 g->tls_ldm_offset = MINUS_TWO;
3559 g->tls_gotno += 2;
3560 }
3561 }
3562 else
3563 {
3564 entry.gotidx = g->local_gotno++;
3565 entry.tls_type = 0;
3566 }
f4416af6
AO
3567
3568 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3569
3570 if (! *loc)
3571 return FALSE;
143d77c5 3572
f4416af6
AO
3573 memcpy (*loc, &entry, sizeof entry);
3574
3575 return TRUE;
3576}
c224138d
RS
3577
3578/* Return the maximum number of GOT page entries required for RANGE. */
3579
3580static bfd_vma
3581mips_elf_pages_for_range (const struct mips_got_page_range *range)
3582{
3583 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3584}
3585
3a3b6725 3586/* Record that ABFD has a page relocation against symbol SYMNDX and
a8028dd0
RS
3587 that ADDEND is the addend for that relocation.
3588
3589 This function creates an upper bound on the number of GOT slots
3590 required; no attempt is made to combine references to non-overridable
3591 global symbols across multiple input files. */
c224138d
RS
3592
3593static bfd_boolean
a8028dd0
RS
3594mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3595 long symndx, bfd_signed_vma addend)
c224138d 3596{
a8028dd0
RS
3597 struct mips_elf_link_hash_table *htab;
3598 struct mips_got_info *g;
c224138d
RS
3599 struct mips_got_page_entry lookup, *entry;
3600 struct mips_got_page_range **range_ptr, *range;
3601 bfd_vma old_pages, new_pages;
3602 void **loc;
3603
a8028dd0
RS
3604 htab = mips_elf_hash_table (info);
3605 g = htab->got_info;
3606 BFD_ASSERT (g != NULL);
3607
c224138d
RS
3608 /* Find the mips_got_page_entry hash table entry for this symbol. */
3609 lookup.abfd = abfd;
3610 lookup.symndx = symndx;
3611 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3612 if (loc == NULL)
3613 return FALSE;
3614
3615 /* Create a mips_got_page_entry if this is the first time we've
3616 seen the symbol. */
3617 entry = (struct mips_got_page_entry *) *loc;
3618 if (!entry)
3619 {
3620 entry = bfd_alloc (abfd, sizeof (*entry));
3621 if (!entry)
3622 return FALSE;
3623
3624 entry->abfd = abfd;
3625 entry->symndx = symndx;
3626 entry->ranges = NULL;
3627 entry->num_pages = 0;
3628 *loc = entry;
3629 }
3630
3631 /* Skip over ranges whose maximum extent cannot share a page entry
3632 with ADDEND. */
3633 range_ptr = &entry->ranges;
3634 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3635 range_ptr = &(*range_ptr)->next;
3636
3637 /* If we scanned to the end of the list, or found a range whose
3638 minimum extent cannot share a page entry with ADDEND, create
3639 a new singleton range. */
3640 range = *range_ptr;
3641 if (!range || addend < range->min_addend - 0xffff)
3642 {
3643 range = bfd_alloc (abfd, sizeof (*range));
3644 if (!range)
3645 return FALSE;
3646
3647 range->next = *range_ptr;
3648 range->min_addend = addend;
3649 range->max_addend = addend;
3650
3651 *range_ptr = range;
3652 entry->num_pages++;
3653 g->page_gotno++;
3654 return TRUE;
3655 }
3656
3657 /* Remember how many pages the old range contributed. */
3658 old_pages = mips_elf_pages_for_range (range);
3659
3660 /* Update the ranges. */
3661 if (addend < range->min_addend)
3662 range->min_addend = addend;
3663 else if (addend > range->max_addend)
3664 {
3665 if (range->next && addend >= range->next->min_addend - 0xffff)
3666 {
3667 old_pages += mips_elf_pages_for_range (range->next);
3668 range->max_addend = range->next->max_addend;
3669 range->next = range->next->next;
3670 }
3671 else
3672 range->max_addend = addend;
3673 }
3674
3675 /* Record any change in the total estimate. */
3676 new_pages = mips_elf_pages_for_range (range);
3677 if (old_pages != new_pages)
3678 {
3679 entry->num_pages += new_pages - old_pages;
3680 g->page_gotno += new_pages - old_pages;
3681 }
3682
3683 return TRUE;
3684}
33bb52fb
RS
3685
3686/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3687
3688static void
3689mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3690 unsigned int n)
3691{
3692 asection *s;
3693 struct mips_elf_link_hash_table *htab;
3694
3695 htab = mips_elf_hash_table (info);
3696 s = mips_elf_rel_dyn_section (info, FALSE);
3697 BFD_ASSERT (s != NULL);
3698
3699 if (htab->is_vxworks)
3700 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3701 else
3702 {
3703 if (s->size == 0)
3704 {
3705 /* Make room for a null element. */
3706 s->size += MIPS_ELF_REL_SIZE (abfd);
3707 ++s->reloc_count;
3708 }
3709 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3710 }
3711}
3712\f
3713/* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3714 if the GOT entry is for an indirect or warning symbol. */
3715
3716static int
3717mips_elf_check_recreate_got (void **entryp, void *data)
3718{
3719 struct mips_got_entry *entry;
3720 bfd_boolean *must_recreate;
3721
3722 entry = (struct mips_got_entry *) *entryp;
3723 must_recreate = (bfd_boolean *) data;
3724 if (entry->abfd != NULL && entry->symndx == -1)
3725 {
3726 struct mips_elf_link_hash_entry *h;
3727
3728 h = entry->d.h;
3729 if (h->root.root.type == bfd_link_hash_indirect
3730 || h->root.root.type == bfd_link_hash_warning)
3731 {
3732 *must_recreate = TRUE;
3733 return 0;
3734 }
3735 }
3736 return 1;
3737}
3738
3739/* A htab_traverse callback for GOT entries. Add all entries to
3740 hash table *DATA, converting entries for indirect and warning
3741 symbols into entries for the target symbol. Set *DATA to null
3742 on error. */
3743
3744static int
3745mips_elf_recreate_got (void **entryp, void *data)
3746{
3747 htab_t *new_got;
3748 struct mips_got_entry *entry;
3749 void **slot;
3750
3751 new_got = (htab_t *) data;
3752 entry = (struct mips_got_entry *) *entryp;
3753 if (entry->abfd != NULL && entry->symndx == -1)
3754 {
3755 struct mips_elf_link_hash_entry *h;
3756
3757 h = entry->d.h;
3758 while (h->root.root.type == bfd_link_hash_indirect
3759 || h->root.root.type == bfd_link_hash_warning)
634835ae
RS
3760 {
3761 BFD_ASSERT (h->global_got_area == GGA_NONE);
3762 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3763 }
33bb52fb
RS
3764 entry->d.h = h;
3765 }
3766 slot = htab_find_slot (*new_got, entry, INSERT);
3767 if (slot == NULL)
3768 {
3769 *new_got = NULL;
3770 return 0;
3771 }
3772 if (*slot == NULL)
3773 *slot = entry;
3774 else
3775 free (entry);
3776 return 1;
3777}
3778
3779/* If any entries in G->got_entries are for indirect or warning symbols,
3780 replace them with entries for the target symbol. */
3781
3782static bfd_boolean
3783mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3784{
3785 bfd_boolean must_recreate;
3786 htab_t new_got;
3787
3788 must_recreate = FALSE;
3789 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3790 if (must_recreate)
3791 {
3792 new_got = htab_create (htab_size (g->got_entries),
3793 mips_elf_got_entry_hash,
3794 mips_elf_got_entry_eq, NULL);
3795 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3796 if (new_got == NULL)
3797 return FALSE;
3798
3799 /* Each entry in g->got_entries has either been copied to new_got
3800 or freed. Now delete the hash table itself. */
3801 htab_delete (g->got_entries);
3802 g->got_entries = new_got;
3803 }
3804 return TRUE;
3805}
3806
634835ae 3807/* A mips_elf_link_hash_traverse callback for which DATA points
d4596a51 3808 to a mips_got_info. Count the number of type (3) entries. */
33bb52fb
RS
3809
3810static int
d4596a51 3811mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb
RS
3812{
3813 struct mips_got_info *g;
3814
3815 g = (struct mips_got_info *) data;
d4596a51 3816 if (h->global_got_area != GGA_NONE)
33bb52fb 3817 {
d4596a51
RS
3818 if (h->root.forced_local || h->root.dynindx == -1)
3819 {
3820 /* We no longer need this entry if it was only used for
3821 relocations; those relocations will be against the
3822 null or section symbol instead of H. */
3823 if (h->global_got_area != GGA_RELOC_ONLY)
3824 g->local_gotno++;
3825 h->global_got_area = GGA_NONE;
3826 }
3827 else
23cc69b6
RS
3828 {
3829 g->global_gotno++;
3830 if (h->global_got_area == GGA_RELOC_ONLY)
3831 g->reloc_only_gotno++;
3832 }
33bb52fb
RS
3833 }
3834 return 1;
3835}
f4416af6
AO
3836\f
3837/* Compute the hash value of the bfd in a bfd2got hash entry. */
3838
3839static hashval_t
9719ad41 3840mips_elf_bfd2got_entry_hash (const void *entry_)
f4416af6
AO
3841{
3842 const struct mips_elf_bfd2got_hash *entry
3843 = (struct mips_elf_bfd2got_hash *)entry_;
3844
3845 return entry->bfd->id;
3846}
3847
3848/* Check whether two hash entries have the same bfd. */
3849
3850static int
9719ad41 3851mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3852{
3853 const struct mips_elf_bfd2got_hash *e1
3854 = (const struct mips_elf_bfd2got_hash *)entry1;
3855 const struct mips_elf_bfd2got_hash *e2
3856 = (const struct mips_elf_bfd2got_hash *)entry2;
3857
3858 return e1->bfd == e2->bfd;
3859}
3860
bad36eac 3861/* In a multi-got link, determine the GOT to be used for IBFD. G must
f4416af6
AO
3862 be the master GOT data. */
3863
3864static struct mips_got_info *
9719ad41 3865mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
3866{
3867 struct mips_elf_bfd2got_hash e, *p;
3868
3869 if (! g->bfd2got)
3870 return g;
3871
3872 e.bfd = ibfd;
9719ad41 3873 p = htab_find (g->bfd2got, &e);
f4416af6
AO
3874 return p ? p->g : NULL;
3875}
3876
c224138d
RS
3877/* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3878 Return NULL if an error occured. */
f4416af6 3879
c224138d
RS
3880static struct mips_got_info *
3881mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3882 bfd *input_bfd)
f4416af6 3883{
f4416af6 3884 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
c224138d 3885 struct mips_got_info *g;
f4416af6 3886 void **bfdgotp;
143d77c5 3887
c224138d 3888 bfdgot_entry.bfd = input_bfd;
f4416af6 3889 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
c224138d 3890 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
f4416af6 3891
c224138d 3892 if (bfdgot == NULL)
f4416af6 3893 {
c224138d
RS
3894 bfdgot = ((struct mips_elf_bfd2got_hash *)
3895 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
f4416af6 3896 if (bfdgot == NULL)
c224138d 3897 return NULL;
f4416af6
AO
3898
3899 *bfdgotp = bfdgot;
3900
c224138d
RS
3901 g = ((struct mips_got_info *)
3902 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
f4416af6 3903 if (g == NULL)
c224138d
RS
3904 return NULL;
3905
3906 bfdgot->bfd = input_bfd;
3907 bfdgot->g = g;
f4416af6
AO
3908
3909 g->global_gotsym = NULL;
3910 g->global_gotno = 0;
23cc69b6 3911 g->reloc_only_gotno = 0;
f4416af6 3912 g->local_gotno = 0;
c224138d 3913 g->page_gotno = 0;
f4416af6 3914 g->assigned_gotno = -1;
0f20cc35
DJ
3915 g->tls_gotno = 0;
3916 g->tls_assigned_gotno = 0;
3917 g->tls_ldm_offset = MINUS_ONE;
f4416af6 3918 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
9719ad41 3919 mips_elf_multi_got_entry_eq, NULL);
f4416af6 3920 if (g->got_entries == NULL)
c224138d
RS
3921 return NULL;
3922
3923 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3924 mips_got_page_entry_eq, NULL);
3925 if (g->got_page_entries == NULL)
3926 return NULL;
f4416af6
AO
3927
3928 g->bfd2got = NULL;
3929 g->next = NULL;
3930 }
3931
c224138d
RS
3932 return bfdgot->g;
3933}
3934
3935/* A htab_traverse callback for the entries in the master got.
3936 Create one separate got for each bfd that has entries in the global
3937 got, such that we can tell how many local and global entries each
3938 bfd requires. */
3939
3940static int
3941mips_elf_make_got_per_bfd (void **entryp, void *p)
3942{
3943 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3944 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3945 struct mips_got_info *g;
3946
3947 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3948 if (g == NULL)
3949 {
3950 arg->obfd = NULL;
3951 return 0;
3952 }
3953
f4416af6
AO
3954 /* Insert the GOT entry in the bfd's got entry hash table. */
3955 entryp = htab_find_slot (g->got_entries, entry, INSERT);
3956 if (*entryp != NULL)
3957 return 1;
143d77c5 3958
f4416af6
AO
3959 *entryp = entry;
3960
0f20cc35
DJ
3961 if (entry->tls_type)
3962 {
3963 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3964 g->tls_gotno += 2;
3965 if (entry->tls_type & GOT_TLS_IE)
3966 g->tls_gotno += 1;
3967 }
33bb52fb 3968 else if (entry->symndx >= 0 || entry->d.h->root.forced_local)
f4416af6
AO
3969 ++g->local_gotno;
3970 else
3971 ++g->global_gotno;
3972
3973 return 1;
3974}
3975
c224138d
RS
3976/* A htab_traverse callback for the page entries in the master got.
3977 Associate each page entry with the bfd's got. */
3978
3979static int
3980mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
3981{
3982 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
3983 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
3984 struct mips_got_info *g;
3985
3986 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3987 if (g == NULL)
3988 {
3989 arg->obfd = NULL;
3990 return 0;
3991 }
3992
3993 /* Insert the GOT entry in the bfd's got entry hash table. */
3994 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
3995 if (*entryp != NULL)
3996 return 1;
3997
3998 *entryp = entry;
3999 g->page_gotno += entry->num_pages;
4000 return 1;
4001}
4002
4003/* Consider merging the got described by BFD2GOT with TO, using the
4004 information given by ARG. Return -1 if this would lead to overflow,
4005 1 if they were merged successfully, and 0 if a merge failed due to
4006 lack of memory. (These values are chosen so that nonnegative return
4007 values can be returned by a htab_traverse callback.) */
4008
4009static int
4010mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4011 struct mips_got_info *to,
4012 struct mips_elf_got_per_bfd_arg *arg)
4013{
4014 struct mips_got_info *from = bfd2got->g;
4015 unsigned int estimate;
4016
4017 /* Work out how many page entries we would need for the combined GOT. */
4018 estimate = arg->max_pages;
4019 if (estimate >= from->page_gotno + to->page_gotno)
4020 estimate = from->page_gotno + to->page_gotno;
4021
4022 /* And conservatively estimate how many local, global and TLS entries
4023 would be needed. */
4024 estimate += (from->local_gotno
4025 + from->global_gotno
4026 + from->tls_gotno
4027 + to->local_gotno
4028 + to->global_gotno
4029 + to->tls_gotno);
4030
4031 /* Bail out if the combined GOT might be too big. */
4032 if (estimate > arg->max_count)
4033 return -1;
4034
4035 /* Commit to the merge. Record that TO is now the bfd for this got. */
4036 bfd2got->g = to;
4037
4038 /* Transfer the bfd's got information from FROM to TO. */
4039 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4040 if (arg->obfd == NULL)
4041 return 0;
4042
4043 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4044 if (arg->obfd == NULL)
4045 return 0;
4046
4047 /* We don't have to worry about releasing memory of the actual
4048 got entries, since they're all in the master got_entries hash
4049 table anyway. */
4050 htab_delete (from->got_entries);
4051 htab_delete (from->got_page_entries);
4052 return 1;
4053}
4054
f4416af6
AO
4055/* Attempt to merge gots of different input bfds. Try to use as much
4056 as possible of the primary got, since it doesn't require explicit
4057 dynamic relocations, but don't use bfds that would reference global
4058 symbols out of the addressable range. Failing the primary got,
4059 attempt to merge with the current got, or finish the current got
4060 and then make make the new got current. */
4061
4062static int
9719ad41 4063mips_elf_merge_gots (void **bfd2got_, void *p)
f4416af6
AO
4064{
4065 struct mips_elf_bfd2got_hash *bfd2got
4066 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4067 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
c224138d
RS
4068 struct mips_got_info *g;
4069 unsigned int estimate;
4070 int result;
4071
4072 g = bfd2got->g;
4073
4074 /* Work out the number of page, local and TLS entries. */
4075 estimate = arg->max_pages;
4076 if (estimate > g->page_gotno)
4077 estimate = g->page_gotno;
4078 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4079
4080 /* We place TLS GOT entries after both locals and globals. The globals
4081 for the primary GOT may overflow the normal GOT size limit, so be
4082 sure not to merge a GOT which requires TLS with the primary GOT in that
4083 case. This doesn't affect non-primary GOTs. */
c224138d 4084 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4085
c224138d 4086 if (estimate <= arg->max_count)
f4416af6 4087 {
c224138d
RS
4088 /* If we don't have a primary GOT, use it as
4089 a starting point for the primary GOT. */
4090 if (!arg->primary)
4091 {
4092 arg->primary = bfd2got->g;
4093 return 1;
4094 }
f4416af6 4095
c224138d
RS
4096 /* Try merging with the primary GOT. */
4097 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4098 if (result >= 0)
4099 return result;
f4416af6 4100 }
c224138d 4101
f4416af6 4102 /* If we can merge with the last-created got, do it. */
c224138d 4103 if (arg->current)
f4416af6 4104 {
c224138d
RS
4105 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4106 if (result >= 0)
4107 return result;
f4416af6 4108 }
c224138d 4109
f4416af6
AO
4110 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4111 fits; if it turns out that it doesn't, we'll get relocation
4112 overflows anyway. */
c224138d
RS
4113 g->next = arg->current;
4114 arg->current = g;
0f20cc35
DJ
4115
4116 return 1;
4117}
4118
ead49a57
RS
4119/* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4120 is null iff there is just a single GOT. */
0f20cc35
DJ
4121
4122static int
4123mips_elf_initialize_tls_index (void **entryp, void *p)
4124{
4125 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4126 struct mips_got_info *g = p;
ead49a57 4127 bfd_vma next_index;
cbf2cba4 4128 unsigned char tls_type;
0f20cc35
DJ
4129
4130 /* We're only interested in TLS symbols. */
4131 if (entry->tls_type == 0)
4132 return 1;
4133
ead49a57
RS
4134 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4135
4136 if (entry->symndx == -1 && g->next == NULL)
0f20cc35 4137 {
ead49a57
RS
4138 /* A type (3) got entry in the single-GOT case. We use the symbol's
4139 hash table entry to track its index. */
4140 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4141 return 1;
4142 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4143 entry->d.h->tls_got_offset = next_index;
cbf2cba4 4144 tls_type = entry->d.h->tls_type;
ead49a57
RS
4145 }
4146 else
4147 {
4148 if (entry->tls_type & GOT_TLS_LDM)
0f20cc35 4149 {
ead49a57
RS
4150 /* There are separate mips_got_entry objects for each input bfd
4151 that requires an LDM entry. Make sure that all LDM entries in
4152 a GOT resolve to the same index. */
4153 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4005427f 4154 {
ead49a57 4155 entry->gotidx = g->tls_ldm_offset;
4005427f
RS
4156 return 1;
4157 }
ead49a57 4158 g->tls_ldm_offset = next_index;
0f20cc35 4159 }
ead49a57 4160 entry->gotidx = next_index;
cbf2cba4 4161 tls_type = entry->tls_type;
f4416af6
AO
4162 }
4163
ead49a57 4164 /* Account for the entries we've just allocated. */
cbf2cba4 4165 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
0f20cc35 4166 g->tls_assigned_gotno += 2;
cbf2cba4 4167 if (tls_type & GOT_TLS_IE)
0f20cc35
DJ
4168 g->tls_assigned_gotno += 1;
4169
f4416af6
AO
4170 return 1;
4171}
4172
4173/* If passed a NULL mips_got_info in the argument, set the marker used
4174 to tell whether a global symbol needs a got entry (in the primary
4175 got) to the given VALUE.
4176
4177 If passed a pointer G to a mips_got_info in the argument (it must
4178 not be the primary GOT), compute the offset from the beginning of
4179 the (primary) GOT section to the entry in G corresponding to the
4180 global symbol. G's assigned_gotno must contain the index of the
4181 first available global GOT entry in G. VALUE must contain the size
4182 of a GOT entry in bytes. For each global GOT entry that requires a
4183 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4cc11e76 4184 marked as not eligible for lazy resolution through a function
f4416af6
AO
4185 stub. */
4186static int
9719ad41 4187mips_elf_set_global_got_offset (void **entryp, void *p)
f4416af6
AO
4188{
4189 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4190 struct mips_elf_set_global_got_offset_arg *arg
4191 = (struct mips_elf_set_global_got_offset_arg *)p;
4192 struct mips_got_info *g = arg->g;
4193
0f20cc35
DJ
4194 if (g && entry->tls_type != GOT_NORMAL)
4195 arg->needed_relocs +=
4196 mips_tls_got_relocs (arg->info, entry->tls_type,
4197 entry->symndx == -1 ? &entry->d.h->root : NULL);
4198
634835ae
RS
4199 if (entry->abfd != NULL
4200 && entry->symndx == -1
4201 && entry->d.h->global_got_area != GGA_NONE)
f4416af6
AO
4202 {
4203 if (g)
4204 {
4205 BFD_ASSERT (g->global_gotsym == NULL);
4206
4207 entry->gotidx = arg->value * (long) g->assigned_gotno++;
f4416af6
AO
4208 if (arg->info->shared
4209 || (elf_hash_table (arg->info)->dynamic_sections_created
f5385ebf
AM
4210 && entry->d.h->root.def_dynamic
4211 && !entry->d.h->root.def_regular))
f4416af6
AO
4212 ++arg->needed_relocs;
4213 }
4214 else
634835ae 4215 entry->d.h->global_got_area = arg->value;
f4416af6
AO
4216 }
4217
4218 return 1;
4219}
4220
33bb52fb
RS
4221/* A htab_traverse callback for GOT entries for which DATA is the
4222 bfd_link_info. Forbid any global symbols from having traditional
4223 lazy-binding stubs. */
4224
0626d451 4225static int
33bb52fb 4226mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4227{
33bb52fb
RS
4228 struct bfd_link_info *info;
4229 struct mips_elf_link_hash_table *htab;
4230 struct mips_got_entry *entry;
0626d451 4231
33bb52fb
RS
4232 entry = (struct mips_got_entry *) *entryp;
4233 info = (struct bfd_link_info *) data;
4234 htab = mips_elf_hash_table (info);
0626d451
RS
4235 if (entry->abfd != NULL
4236 && entry->symndx == -1
33bb52fb 4237 && entry->d.h->needs_lazy_stub)
f4416af6 4238 {
33bb52fb
RS
4239 entry->d.h->needs_lazy_stub = FALSE;
4240 htab->lazy_stub_count--;
f4416af6 4241 }
143d77c5 4242
f4416af6
AO
4243 return 1;
4244}
4245
f4416af6
AO
4246/* Return the offset of an input bfd IBFD's GOT from the beginning of
4247 the primary GOT. */
4248static bfd_vma
9719ad41 4249mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
4250{
4251 if (g->bfd2got == NULL)
4252 return 0;
4253
4254 g = mips_elf_got_for_ibfd (g, ibfd);
4255 if (! g)
4256 return 0;
4257
4258 BFD_ASSERT (g->next);
4259
4260 g = g->next;
143d77c5 4261
0f20cc35
DJ
4262 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4263 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4264}
4265
4266/* Turn a single GOT that is too big for 16-bit addressing into
4267 a sequence of GOTs, each one 16-bit addressable. */
4268
4269static bfd_boolean
9719ad41 4270mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4271 asection *got, bfd_size_type pages)
f4416af6 4272{
a8028dd0 4273 struct mips_elf_link_hash_table *htab;
f4416af6
AO
4274 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4275 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
a8028dd0 4276 struct mips_got_info *g, *gg;
33bb52fb
RS
4277 unsigned int assign, needed_relocs;
4278 bfd *dynobj;
f4416af6 4279
33bb52fb 4280 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
4281 htab = mips_elf_hash_table (info);
4282 g = htab->got_info;
f4416af6 4283 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
9719ad41 4284 mips_elf_bfd2got_entry_eq, NULL);
f4416af6
AO
4285 if (g->bfd2got == NULL)
4286 return FALSE;
4287
4288 got_per_bfd_arg.bfd2got = g->bfd2got;
4289 got_per_bfd_arg.obfd = abfd;
4290 got_per_bfd_arg.info = info;
4291
4292 /* Count how many GOT entries each input bfd requires, creating a
4293 map from bfd to got info while at that. */
f4416af6
AO
4294 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4295 if (got_per_bfd_arg.obfd == NULL)
4296 return FALSE;
4297
c224138d
RS
4298 /* Also count how many page entries each input bfd requires. */
4299 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4300 &got_per_bfd_arg);
4301 if (got_per_bfd_arg.obfd == NULL)
4302 return FALSE;
4303
f4416af6
AO
4304 got_per_bfd_arg.current = NULL;
4305 got_per_bfd_arg.primary = NULL;
0a44bf69 4306 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4307 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4308 - htab->reserved_gotno);
c224138d 4309 got_per_bfd_arg.max_pages = pages;
0f20cc35
DJ
4310 /* The number of globals that will be included in the primary GOT.
4311 See the calls to mips_elf_set_global_got_offset below for more
4312 information. */
4313 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4314
4315 /* Try to merge the GOTs of input bfds together, as long as they
4316 don't seem to exceed the maximum GOT size, choosing one of them
4317 to be the primary GOT. */
4318 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4319 if (got_per_bfd_arg.obfd == NULL)
4320 return FALSE;
4321
0f20cc35 4322 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6
AO
4323 if (got_per_bfd_arg.primary == NULL)
4324 {
4325 g->next = (struct mips_got_info *)
4326 bfd_alloc (abfd, sizeof (struct mips_got_info));
4327 if (g->next == NULL)
4328 return FALSE;
4329
4330 g->next->global_gotsym = NULL;
4331 g->next->global_gotno = 0;
23cc69b6 4332 g->next->reloc_only_gotno = 0;
f4416af6 4333 g->next->local_gotno = 0;
c224138d 4334 g->next->page_gotno = 0;
0f20cc35 4335 g->next->tls_gotno = 0;
f4416af6 4336 g->next->assigned_gotno = 0;
0f20cc35
DJ
4337 g->next->tls_assigned_gotno = 0;
4338 g->next->tls_ldm_offset = MINUS_ONE;
f4416af6
AO
4339 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4340 mips_elf_multi_got_entry_eq,
9719ad41 4341 NULL);
f4416af6
AO
4342 if (g->next->got_entries == NULL)
4343 return FALSE;
c224138d
RS
4344 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4345 mips_got_page_entry_eq,
4346 NULL);
4347 if (g->next->got_page_entries == NULL)
4348 return FALSE;
f4416af6
AO
4349 g->next->bfd2got = NULL;
4350 }
4351 else
4352 g->next = got_per_bfd_arg.primary;
4353 g->next->next = got_per_bfd_arg.current;
4354
4355 /* GG is now the master GOT, and G is the primary GOT. */
4356 gg = g;
4357 g = g->next;
4358
4359 /* Map the output bfd to the primary got. That's what we're going
4360 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4361 didn't mark in check_relocs, and we want a quick way to find it.
4362 We can't just use gg->next because we're going to reverse the
4363 list. */
4364 {
4365 struct mips_elf_bfd2got_hash *bfdgot;
4366 void **bfdgotp;
143d77c5 4367
f4416af6
AO
4368 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4369 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4370
4371 if (bfdgot == NULL)
4372 return FALSE;
4373
4374 bfdgot->bfd = abfd;
4375 bfdgot->g = g;
4376 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4377
4378 BFD_ASSERT (*bfdgotp == NULL);
4379 *bfdgotp = bfdgot;
4380 }
4381
634835ae
RS
4382 /* Every symbol that is referenced in a dynamic relocation must be
4383 present in the primary GOT, so arrange for them to appear after
4384 those that are actually referenced. */
23cc69b6 4385 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4386 g->global_gotno = gg->global_gotno;
f4416af6 4387
f4416af6 4388 set_got_offset_arg.g = NULL;
634835ae 4389 set_got_offset_arg.value = GGA_RELOC_ONLY;
f4416af6
AO
4390 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4391 &set_got_offset_arg);
634835ae 4392 set_got_offset_arg.value = GGA_NORMAL;
f4416af6
AO
4393 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4394 &set_got_offset_arg);
f4416af6
AO
4395
4396 /* Now go through the GOTs assigning them offset ranges.
4397 [assigned_gotno, local_gotno[ will be set to the range of local
4398 entries in each GOT. We can then compute the end of a GOT by
4399 adding local_gotno to global_gotno. We reverse the list and make
4400 it circular since then we'll be able to quickly compute the
4401 beginning of a GOT, by computing the end of its predecessor. To
4402 avoid special cases for the primary GOT, while still preserving
4403 assertions that are valid for both single- and multi-got links,
4404 we arrange for the main got struct to have the right number of
4405 global entries, but set its local_gotno such that the initial
4406 offset of the primary GOT is zero. Remember that the primary GOT
4407 will become the last item in the circular linked list, so it
4408 points back to the master GOT. */
4409 gg->local_gotno = -g->global_gotno;
4410 gg->global_gotno = g->global_gotno;
0f20cc35 4411 gg->tls_gotno = 0;
f4416af6
AO
4412 assign = 0;
4413 gg->next = gg;
4414
4415 do
4416 {
4417 struct mips_got_info *gn;
4418
861fb55a 4419 assign += htab->reserved_gotno;
f4416af6 4420 g->assigned_gotno = assign;
c224138d
RS
4421 g->local_gotno += assign;
4422 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
0f20cc35
DJ
4423 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4424
ead49a57
RS
4425 /* Take g out of the direct list, and push it onto the reversed
4426 list that gg points to. g->next is guaranteed to be nonnull after
4427 this operation, as required by mips_elf_initialize_tls_index. */
4428 gn = g->next;
4429 g->next = gg->next;
4430 gg->next = g;
4431
0f20cc35
DJ
4432 /* Set up any TLS entries. We always place the TLS entries after
4433 all non-TLS entries. */
4434 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4435 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
f4416af6 4436
ead49a57 4437 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4438 g = gn;
0626d451 4439
33bb52fb
RS
4440 /* Forbid global symbols in every non-primary GOT from having
4441 lazy-binding stubs. */
0626d451 4442 if (g)
33bb52fb 4443 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4444 }
4445 while (g);
4446
eea6121a 4447 got->size = (gg->next->local_gotno
33bb52fb
RS
4448 + gg->next->global_gotno
4449 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4450
4451 needed_relocs = 0;
4452 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4453 set_got_offset_arg.info = info;
4454 for (g = gg->next; g && g->next != gg; g = g->next)
4455 {
4456 unsigned int save_assign;
4457
4458 /* Assign offsets to global GOT entries. */
4459 save_assign = g->assigned_gotno;
4460 g->assigned_gotno = g->local_gotno;
4461 set_got_offset_arg.g = g;
4462 set_got_offset_arg.needed_relocs = 0;
4463 htab_traverse (g->got_entries,
4464 mips_elf_set_global_got_offset,
4465 &set_got_offset_arg);
4466 needed_relocs += set_got_offset_arg.needed_relocs;
4467 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4468
4469 g->assigned_gotno = save_assign;
4470 if (info->shared)
4471 {
4472 needed_relocs += g->local_gotno - g->assigned_gotno;
4473 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4474 + g->next->global_gotno
4475 + g->next->tls_gotno
861fb55a 4476 + htab->reserved_gotno);
33bb52fb
RS
4477 }
4478 }
4479
4480 if (needed_relocs)
4481 mips_elf_allocate_dynamic_relocations (dynobj, info,
4482 needed_relocs);
143d77c5 4483
f4416af6
AO
4484 return TRUE;
4485}
143d77c5 4486
b49e97c9
TS
4487\f
4488/* Returns the first relocation of type r_type found, beginning with
4489 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4490
4491static const Elf_Internal_Rela *
9719ad41
RS
4492mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4493 const Elf_Internal_Rela *relocation,
4494 const Elf_Internal_Rela *relend)
b49e97c9 4495{
c000e262
TS
4496 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4497
b49e97c9
TS
4498 while (relocation < relend)
4499 {
c000e262
TS
4500 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4501 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
4502 return relocation;
4503
4504 ++relocation;
4505 }
4506
4507 /* We didn't find it. */
b49e97c9
TS
4508 return NULL;
4509}
4510
4511/* Return whether a relocation is against a local symbol. */
4512
b34976b6 4513static bfd_boolean
9719ad41
RS
4514mips_elf_local_relocation_p (bfd *input_bfd,
4515 const Elf_Internal_Rela *relocation,
4516 asection **local_sections,
4517 bfd_boolean check_forced)
b49e97c9
TS
4518{
4519 unsigned long r_symndx;
4520 Elf_Internal_Shdr *symtab_hdr;
4521 struct mips_elf_link_hash_entry *h;
4522 size_t extsymoff;
4523
4524 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4525 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4526 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4527
4528 if (r_symndx < extsymoff)
b34976b6 4529 return TRUE;
b49e97c9 4530 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 4531 return TRUE;
b49e97c9
TS
4532
4533 if (check_forced)
4534 {
4535 /* Look up the hash table to check whether the symbol
4536 was forced local. */
4537 h = (struct mips_elf_link_hash_entry *)
4538 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
4539 /* Find the real hash-table entry for this symbol. */
4540 while (h->root.root.type == bfd_link_hash_indirect
4541 || h->root.root.type == bfd_link_hash_warning)
4542 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
f5385ebf 4543 if (h->root.forced_local)
b34976b6 4544 return TRUE;
b49e97c9
TS
4545 }
4546
b34976b6 4547 return FALSE;
b49e97c9
TS
4548}
4549\f
4550/* Sign-extend VALUE, which has the indicated number of BITS. */
4551
a7ebbfdf 4552bfd_vma
9719ad41 4553_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
4554{
4555 if (value & ((bfd_vma) 1 << (bits - 1)))
4556 /* VALUE is negative. */
4557 value |= ((bfd_vma) - 1) << bits;
4558
4559 return value;
4560}
4561
4562/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 4563 range expressible by a signed number with the indicated number of
b49e97c9
TS
4564 BITS. */
4565
b34976b6 4566static bfd_boolean
9719ad41 4567mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
4568{
4569 bfd_signed_vma svalue = (bfd_signed_vma) value;
4570
4571 if (svalue > (1 << (bits - 1)) - 1)
4572 /* The value is too big. */
b34976b6 4573 return TRUE;
b49e97c9
TS
4574 else if (svalue < -(1 << (bits - 1)))
4575 /* The value is too small. */
b34976b6 4576 return TRUE;
b49e97c9
TS
4577
4578 /* All is well. */
b34976b6 4579 return FALSE;
b49e97c9
TS
4580}
4581
4582/* Calculate the %high function. */
4583
4584static bfd_vma
9719ad41 4585mips_elf_high (bfd_vma value)
b49e97c9
TS
4586{
4587 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4588}
4589
4590/* Calculate the %higher function. */
4591
4592static bfd_vma
9719ad41 4593mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4594{
4595#ifdef BFD64
4596 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4597#else
4598 abort ();
c5ae1840 4599 return MINUS_ONE;
b49e97c9
TS
4600#endif
4601}
4602
4603/* Calculate the %highest function. */
4604
4605static bfd_vma
9719ad41 4606mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
4607{
4608#ifdef BFD64
b15e6682 4609 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
4610#else
4611 abort ();
c5ae1840 4612 return MINUS_ONE;
b49e97c9
TS
4613#endif
4614}
4615\f
4616/* Create the .compact_rel section. */
4617
b34976b6 4618static bfd_boolean
9719ad41
RS
4619mips_elf_create_compact_rel_section
4620 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
4621{
4622 flagword flags;
4623 register asection *s;
4624
4625 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4626 {
4627 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4628 | SEC_READONLY);
4629
3496cb2a 4630 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
b49e97c9 4631 if (s == NULL
b49e97c9
TS
4632 || ! bfd_set_section_alignment (abfd, s,
4633 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 4634 return FALSE;
b49e97c9 4635
eea6121a 4636 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
4637 }
4638
b34976b6 4639 return TRUE;
b49e97c9
TS
4640}
4641
4642/* Create the .got section to hold the global offset table. */
4643
b34976b6 4644static bfd_boolean
23cc69b6 4645mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
4646{
4647 flagword flags;
4648 register asection *s;
4649 struct elf_link_hash_entry *h;
14a793b2 4650 struct bfd_link_hash_entry *bh;
b49e97c9
TS
4651 struct mips_got_info *g;
4652 bfd_size_type amt;
0a44bf69
RS
4653 struct mips_elf_link_hash_table *htab;
4654
4655 htab = mips_elf_hash_table (info);
b49e97c9
TS
4656
4657 /* This function may be called more than once. */
23cc69b6
RS
4658 if (htab->sgot)
4659 return TRUE;
b49e97c9
TS
4660
4661 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4662 | SEC_LINKER_CREATED);
4663
72b4917c
TS
4664 /* We have to use an alignment of 2**4 here because this is hardcoded
4665 in the function stub generation and in the linker script. */
3496cb2a 4666 s = bfd_make_section_with_flags (abfd, ".got", flags);
b49e97c9 4667 if (s == NULL
72b4917c 4668 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 4669 return FALSE;
a8028dd0 4670 htab->sgot = s;
b49e97c9
TS
4671
4672 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4673 linker script because we don't want to define the symbol if we
4674 are not creating a global offset table. */
14a793b2 4675 bh = NULL;
b49e97c9
TS
4676 if (! (_bfd_generic_link_add_one_symbol
4677 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 4678 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 4679 return FALSE;
14a793b2
AM
4680
4681 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
4682 h->non_elf = 0;
4683 h->def_regular = 1;
b49e97c9 4684 h->type = STT_OBJECT;
d329bcd1 4685 elf_hash_table (info)->hgot = h;
b49e97c9
TS
4686
4687 if (info->shared
c152c796 4688 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4689 return FALSE;
b49e97c9 4690
b49e97c9 4691 amt = sizeof (struct mips_got_info);
9719ad41 4692 g = bfd_alloc (abfd, amt);
b49e97c9 4693 if (g == NULL)
b34976b6 4694 return FALSE;
b49e97c9 4695 g->global_gotsym = NULL;
e3d54347 4696 g->global_gotno = 0;
23cc69b6 4697 g->reloc_only_gotno = 0;
0f20cc35 4698 g->tls_gotno = 0;
861fb55a 4699 g->local_gotno = 0;
c224138d 4700 g->page_gotno = 0;
861fb55a 4701 g->assigned_gotno = 0;
f4416af6
AO
4702 g->bfd2got = NULL;
4703 g->next = NULL;
0f20cc35 4704 g->tls_ldm_offset = MINUS_ONE;
b15e6682 4705 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
9719ad41 4706 mips_elf_got_entry_eq, NULL);
b15e6682
AO
4707 if (g->got_entries == NULL)
4708 return FALSE;
c224138d
RS
4709 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4710 mips_got_page_entry_eq, NULL);
4711 if (g->got_page_entries == NULL)
4712 return FALSE;
a8028dd0 4713 htab->got_info = g;
f0abc2a1 4714 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
4715 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4716
861fb55a
DJ
4717 /* We also need a .got.plt section when generating PLTs. */
4718 s = bfd_make_section_with_flags (abfd, ".got.plt",
4719 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4720 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4721 if (s == NULL)
4722 return FALSE;
4723 htab->sgotplt = s;
0a44bf69 4724
b34976b6 4725 return TRUE;
b49e97c9 4726}
b49e97c9 4727\f
0a44bf69
RS
4728/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4729 __GOTT_INDEX__ symbols. These symbols are only special for
4730 shared objects; they are not used in executables. */
4731
4732static bfd_boolean
4733is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4734{
4735 return (mips_elf_hash_table (info)->is_vxworks
4736 && info->shared
4737 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4738 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4739}
861fb55a
DJ
4740
4741/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4742 require an la25 stub. See also mips_elf_local_pic_function_p,
4743 which determines whether the destination function ever requires a
4744 stub. */
4745
4746static bfd_boolean
4747mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4748{
4749 /* We specifically ignore branches and jumps from EF_PIC objects,
4750 where the onus is on the compiler or programmer to perform any
4751 necessary initialization of $25. Sometimes such initialization
4752 is unnecessary; for example, -mno-shared functions do not use
4753 the incoming value of $25, and may therefore be called directly. */
4754 if (PIC_OBJECT_P (input_bfd))
4755 return FALSE;
4756
4757 switch (r_type)
4758 {
4759 case R_MIPS_26:
4760 case R_MIPS_PC16:
4761 case R_MIPS16_26:
4762 return TRUE;
4763
4764 default:
4765 return FALSE;
4766 }
4767}
0a44bf69 4768\f
b49e97c9
TS
4769/* Calculate the value produced by the RELOCATION (which comes from
4770 the INPUT_BFD). The ADDEND is the addend to use for this
4771 RELOCATION; RELOCATION->R_ADDEND is ignored.
4772
4773 The result of the relocation calculation is stored in VALUEP.
4774 REQUIRE_JALXP indicates whether or not the opcode used with this
4775 relocation must be JALX.
4776
4777 This function returns bfd_reloc_continue if the caller need take no
4778 further action regarding this relocation, bfd_reloc_notsupported if
4779 something goes dramatically wrong, bfd_reloc_overflow if an
4780 overflow occurs, and bfd_reloc_ok to indicate success. */
4781
4782static bfd_reloc_status_type
9719ad41
RS
4783mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4784 asection *input_section,
4785 struct bfd_link_info *info,
4786 const Elf_Internal_Rela *relocation,
4787 bfd_vma addend, reloc_howto_type *howto,
4788 Elf_Internal_Sym *local_syms,
4789 asection **local_sections, bfd_vma *valuep,
4790 const char **namep, bfd_boolean *require_jalxp,
4791 bfd_boolean save_addend)
b49e97c9
TS
4792{
4793 /* The eventual value we will return. */
4794 bfd_vma value;
4795 /* The address of the symbol against which the relocation is
4796 occurring. */
4797 bfd_vma symbol = 0;
4798 /* The final GP value to be used for the relocatable, executable, or
4799 shared object file being produced. */
0a61c8c2 4800 bfd_vma gp;
b49e97c9
TS
4801 /* The place (section offset or address) of the storage unit being
4802 relocated. */
4803 bfd_vma p;
4804 /* The value of GP used to create the relocatable object. */
0a61c8c2 4805 bfd_vma gp0;
b49e97c9
TS
4806 /* The offset into the global offset table at which the address of
4807 the relocation entry symbol, adjusted by the addend, resides
4808 during execution. */
4809 bfd_vma g = MINUS_ONE;
4810 /* The section in which the symbol referenced by the relocation is
4811 located. */
4812 asection *sec = NULL;
4813 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 4814 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 4815 symbol. */
b34976b6
AM
4816 bfd_boolean local_p, was_local_p;
4817 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4818 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
4819 /* TRUE if the symbol referred to by this relocation is
4820 "__gnu_local_gp". */
4821 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
4822 Elf_Internal_Shdr *symtab_hdr;
4823 size_t extsymoff;
4824 unsigned long r_symndx;
4825 int r_type;
b34976b6 4826 /* TRUE if overflow occurred during the calculation of the
b49e97c9 4827 relocation value. */
b34976b6
AM
4828 bfd_boolean overflowed_p;
4829 /* TRUE if this relocation refers to a MIPS16 function. */
4830 bfd_boolean target_is_16_bit_code_p = FALSE;
0a44bf69
RS
4831 struct mips_elf_link_hash_table *htab;
4832 bfd *dynobj;
4833
4834 dynobj = elf_hash_table (info)->dynobj;
4835 htab = mips_elf_hash_table (info);
b49e97c9
TS
4836
4837 /* Parse the relocation. */
4838 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4839 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4840 p = (input_section->output_section->vma
4841 + input_section->output_offset
4842 + relocation->r_offset);
4843
4844 /* Assume that there will be no overflow. */
b34976b6 4845 overflowed_p = FALSE;
b49e97c9
TS
4846
4847 /* Figure out whether or not the symbol is local, and get the offset
4848 used in the array of hash table entries. */
4849 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4850 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 4851 local_sections, FALSE);
bce03d3d 4852 was_local_p = local_p;
b49e97c9
TS
4853 if (! elf_bad_symtab (input_bfd))
4854 extsymoff = symtab_hdr->sh_info;
4855 else
4856 {
4857 /* The symbol table does not follow the rule that local symbols
4858 must come before globals. */
4859 extsymoff = 0;
4860 }
4861
4862 /* Figure out the value of the symbol. */
4863 if (local_p)
4864 {
4865 Elf_Internal_Sym *sym;
4866
4867 sym = local_syms + r_symndx;
4868 sec = local_sections[r_symndx];
4869
4870 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
4871 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4872 || (sec->flags & SEC_MERGE))
b49e97c9 4873 symbol += sym->st_value;
d4df96e6
L
4874 if ((sec->flags & SEC_MERGE)
4875 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4876 {
4877 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4878 addend -= symbol;
4879 addend += sec->output_section->vma + sec->output_offset;
4880 }
b49e97c9
TS
4881
4882 /* MIPS16 text labels should be treated as odd. */
30c09090 4883 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
4884 ++symbol;
4885
4886 /* Record the name of this symbol, for our caller. */
4887 *namep = bfd_elf_string_from_elf_section (input_bfd,
4888 symtab_hdr->sh_link,
4889 sym->st_name);
4890 if (*namep == '\0')
4891 *namep = bfd_section_name (input_bfd, sec);
4892
30c09090 4893 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
b49e97c9
TS
4894 }
4895 else
4896 {
560e09e9
NC
4897 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4898
b49e97c9
TS
4899 /* For global symbols we look up the symbol in the hash-table. */
4900 h = ((struct mips_elf_link_hash_entry *)
4901 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4902 /* Find the real hash-table entry for this symbol. */
4903 while (h->root.root.type == bfd_link_hash_indirect
4904 || h->root.root.type == bfd_link_hash_warning)
4905 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4906
4907 /* Record the name of this symbol, for our caller. */
4908 *namep = h->root.root.root.string;
4909
4910 /* See if this is the special _gp_disp symbol. Note that such a
4911 symbol must always be a global symbol. */
560e09e9 4912 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
4913 && ! NEWABI_P (input_bfd))
4914 {
4915 /* Relocations against _gp_disp are permitted only with
4916 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 4917 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
4918 return bfd_reloc_notsupported;
4919
b34976b6 4920 gp_disp_p = TRUE;
b49e97c9 4921 }
bbe506e8
TS
4922 /* See if this is the special _gp symbol. Note that such a
4923 symbol must always be a global symbol. */
4924 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4925 gnu_local_gp_p = TRUE;
4926
4927
b49e97c9
TS
4928 /* If this symbol is defined, calculate its address. Note that
4929 _gp_disp is a magic symbol, always implicitly defined by the
4930 linker, so it's inappropriate to check to see whether or not
4931 its defined. */
4932 else if ((h->root.root.type == bfd_link_hash_defined
4933 || h->root.root.type == bfd_link_hash_defweak)
4934 && h->root.root.u.def.section)
4935 {
4936 sec = h->root.root.u.def.section;
4937 if (sec->output_section)
4938 symbol = (h->root.root.u.def.value
4939 + sec->output_section->vma
4940 + sec->output_offset);
4941 else
4942 symbol = h->root.root.u.def.value;
4943 }
4944 else if (h->root.root.type == bfd_link_hash_undefweak)
4945 /* We allow relocations against undefined weak symbols, giving
4946 it the value zero, so that you can undefined weak functions
4947 and check to see if they exist by looking at their
4948 addresses. */
4949 symbol = 0;
59c2e50f 4950 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
4951 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4952 symbol = 0;
a4d0f181
TS
4953 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4954 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
4955 {
4956 /* If this is a dynamic link, we should have created a
4957 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4958 in in _bfd_mips_elf_create_dynamic_sections.
4959 Otherwise, we should define the symbol with a value of 0.
4960 FIXME: It should probably get into the symbol table
4961 somehow as well. */
4962 BFD_ASSERT (! info->shared);
4963 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4964 symbol = 0;
4965 }
5e2b0d47
NC
4966 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4967 {
4968 /* This is an optional symbol - an Irix specific extension to the
4969 ELF spec. Ignore it for now.
4970 XXX - FIXME - there is more to the spec for OPTIONAL symbols
4971 than simply ignoring them, but we do not handle this for now.
4972 For information see the "64-bit ELF Object File Specification"
4973 which is available from here:
4974 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
4975 symbol = 0;
4976 }
e7e2196d
MR
4977 else if ((*info->callbacks->undefined_symbol)
4978 (info, h->root.root.root.string, input_bfd,
4979 input_section, relocation->r_offset,
4980 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4981 || ELF_ST_VISIBILITY (h->root.other)))
4982 {
4983 return bfd_reloc_undefined;
4984 }
b49e97c9
TS
4985 else
4986 {
e7e2196d 4987 return bfd_reloc_notsupported;
b49e97c9
TS
4988 }
4989
30c09090 4990 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
b49e97c9
TS
4991 }
4992
738e5348
RS
4993 /* If this is a reference to a 16-bit function with a stub, we need
4994 to redirect the relocation to the stub unless:
4995
4996 (a) the relocation is for a MIPS16 JAL;
4997
4998 (b) the relocation is for a MIPS16 PIC call, and there are no
4999 non-MIPS16 uses of the GOT slot; or
5000
5001 (c) the section allows direct references to MIPS16 functions. */
5002 if (r_type != R_MIPS16_26
5003 && !info->relocatable
5004 && ((h != NULL
5005 && h->fn_stub != NULL
5006 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71
TS
5007 || (local_p
5008 && elf_tdata (input_bfd)->local_stubs != NULL
b49e97c9 5009 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5010 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5011 {
5012 /* This is a 32- or 64-bit call to a 16-bit function. We should
5013 have already noticed that we were going to need the
5014 stub. */
5015 if (local_p)
5016 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5017 else
5018 {
5019 BFD_ASSERT (h->need_fn_stub);
5020 sec = h->fn_stub;
5021 }
5022
5023 symbol = sec->output_section->vma + sec->output_offset;
f38c2df5
TS
5024 /* The target is 16-bit, but the stub isn't. */
5025 target_is_16_bit_code_p = FALSE;
b49e97c9
TS
5026 }
5027 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
738e5348
RS
5028 need to redirect the call to the stub. Note that we specifically
5029 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5030 use an indirect stub instead. */
1049f94e 5031 else if (r_type == R_MIPS16_26 && !info->relocatable
b314ec0e 5032 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71
TS
5033 || (local_p
5034 && elf_tdata (input_bfd)->local_call_stubs != NULL
5035 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
b49e97c9
TS
5036 && !target_is_16_bit_code_p)
5037 {
b9d58d71
TS
5038 if (local_p)
5039 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5040 else
b49e97c9 5041 {
b9d58d71
TS
5042 /* If both call_stub and call_fp_stub are defined, we can figure
5043 out which one to use by checking which one appears in the input
5044 file. */
5045 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5046 {
b9d58d71
TS
5047 asection *o;
5048
5049 sec = NULL;
5050 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5051 {
b9d58d71
TS
5052 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5053 {
5054 sec = h->call_fp_stub;
5055 break;
5056 }
b49e97c9 5057 }
b9d58d71
TS
5058 if (sec == NULL)
5059 sec = h->call_stub;
b49e97c9 5060 }
b9d58d71 5061 else if (h->call_stub != NULL)
b49e97c9 5062 sec = h->call_stub;
b9d58d71
TS
5063 else
5064 sec = h->call_fp_stub;
5065 }
b49e97c9 5066
eea6121a 5067 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5068 symbol = sec->output_section->vma + sec->output_offset;
5069 }
861fb55a
DJ
5070 /* If this is a direct call to a PIC function, redirect to the
5071 non-PIC stub. */
5072 else if (h != NULL && h->la25_stub
5073 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5074 symbol = (h->la25_stub->stub_section->output_section->vma
5075 + h->la25_stub->stub_section->output_offset
5076 + h->la25_stub->offset);
b49e97c9
TS
5077
5078 /* Calls from 16-bit code to 32-bit code and vice versa require the
5079 special jalx instruction. */
1049f94e 5080 *require_jalxp = (!info->relocatable
b49e97c9
TS
5081 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
5082 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
5083
5084 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 5085 local_sections, TRUE);
b49e97c9 5086
0a61c8c2
RS
5087 gp0 = _bfd_get_gp_value (input_bfd);
5088 gp = _bfd_get_gp_value (abfd);
23cc69b6 5089 if (htab->got_info)
a8028dd0 5090 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5091
5092 if (gnu_local_gp_p)
5093 symbol = gp;
5094
5095 /* If we haven't already determined the GOT offset, oand we're going
5096 to need it, get it now. */
b49e97c9
TS
5097 switch (r_type)
5098 {
0fdc1bf1 5099 case R_MIPS_GOT_PAGE:
93a2b7ae 5100 case R_MIPS_GOT_OFST:
d25aed71
RS
5101 /* We need to decay to GOT_DISP/addend if the symbol doesn't
5102 bind locally. */
5103 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
93a2b7ae 5104 if (local_p || r_type == R_MIPS_GOT_OFST)
0fdc1bf1
AO
5105 break;
5106 /* Fall through. */
5107
738e5348
RS
5108 case R_MIPS16_CALL16:
5109 case R_MIPS16_GOT16:
b49e97c9
TS
5110 case R_MIPS_CALL16:
5111 case R_MIPS_GOT16:
5112 case R_MIPS_GOT_DISP:
5113 case R_MIPS_GOT_HI16:
5114 case R_MIPS_CALL_HI16:
5115 case R_MIPS_GOT_LO16:
5116 case R_MIPS_CALL_LO16:
0f20cc35
DJ
5117 case R_MIPS_TLS_GD:
5118 case R_MIPS_TLS_GOTTPREL:
5119 case R_MIPS_TLS_LDM:
b49e97c9 5120 /* Find the index into the GOT where this value is located. */
0f20cc35
DJ
5121 if (r_type == R_MIPS_TLS_LDM)
5122 {
0a44bf69 5123 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5124 0, 0, NULL, r_type);
0f20cc35
DJ
5125 if (g == MINUS_ONE)
5126 return bfd_reloc_outofrange;
5127 }
5128 else if (!local_p)
b49e97c9 5129 {
0a44bf69
RS
5130 /* On VxWorks, CALL relocations should refer to the .got.plt
5131 entry, which is initialized to point at the PLT stub. */
5132 if (htab->is_vxworks
5133 && (r_type == R_MIPS_CALL_HI16
5134 || r_type == R_MIPS_CALL_LO16
738e5348 5135 || call16_reloc_p (r_type)))
0a44bf69
RS
5136 {
5137 BFD_ASSERT (addend == 0);
5138 BFD_ASSERT (h->root.needs_plt);
5139 g = mips_elf_gotplt_index (info, &h->root);
5140 }
5141 else
b49e97c9 5142 {
0a44bf69
RS
5143 /* GOT_PAGE may take a non-zero addend, that is ignored in a
5144 GOT_PAGE relocation that decays to GOT_DISP because the
5145 symbol turns out to be global. The addend is then added
5146 as GOT_OFST. */
5147 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
5148 g = mips_elf_global_got_index (dynobj, input_bfd,
5149 &h->root, r_type, info);
5150 if (h->tls_type == GOT_NORMAL
5151 && (! elf_hash_table(info)->dynamic_sections_created
5152 || (info->shared
5153 && (info->symbolic || h->root.forced_local)
5154 && h->root.def_regular)))
a8028dd0
RS
5155 /* This is a static link or a -Bsymbolic link. The
5156 symbol is defined locally, or was forced to be local.
5157 We must initialize this entry in the GOT. */
5158 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
b49e97c9
TS
5159 }
5160 }
0a44bf69 5161 else if (!htab->is_vxworks
738e5348 5162 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5163 /* The calculation below does not involve "g". */
b49e97c9
TS
5164 break;
5165 else
5166 {
5c18022e 5167 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5168 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5169 if (g == MINUS_ONE)
5170 return bfd_reloc_outofrange;
5171 }
5172
5173 /* Convert GOT indices to actual offsets. */
a8028dd0 5174 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5175 break;
b49e97c9
TS
5176 }
5177
0a44bf69
RS
5178 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5179 symbols are resolved by the loader. Add them to .rela.dyn. */
5180 if (h != NULL && is_gott_symbol (info, &h->root))
5181 {
5182 Elf_Internal_Rela outrel;
5183 bfd_byte *loc;
5184 asection *s;
5185
5186 s = mips_elf_rel_dyn_section (info, FALSE);
5187 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5188
5189 outrel.r_offset = (input_section->output_section->vma
5190 + input_section->output_offset
5191 + relocation->r_offset);
5192 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5193 outrel.r_addend = addend;
5194 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5195
5196 /* If we've written this relocation for a readonly section,
5197 we need to set DF_TEXTREL again, so that we do not delete the
5198 DT_TEXTREL tag. */
5199 if (MIPS_ELF_READONLY_SECTION (input_section))
5200 info->flags |= DF_TEXTREL;
5201
0a44bf69
RS
5202 *valuep = 0;
5203 return bfd_reloc_ok;
5204 }
5205
b49e97c9
TS
5206 /* Figure out what kind of relocation is being performed. */
5207 switch (r_type)
5208 {
5209 case R_MIPS_NONE:
5210 return bfd_reloc_continue;
5211
5212 case R_MIPS_16:
a7ebbfdf 5213 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
5214 overflowed_p = mips_elf_overflow_p (value, 16);
5215 break;
5216
5217 case R_MIPS_32:
5218 case R_MIPS_REL32:
5219 case R_MIPS_64:
5220 if ((info->shared
861fb55a 5221 || (htab->root.dynamic_sections_created
b49e97c9 5222 && h != NULL
f5385ebf 5223 && h->root.def_dynamic
861fb55a
DJ
5224 && !h->root.def_regular
5225 && !h->has_static_relocs))
b49e97c9 5226 && r_symndx != 0
9a59ad6b
DJ
5227 && (h == NULL
5228 || h->root.root.type != bfd_link_hash_undefweak
5229 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
b49e97c9
TS
5230 && (input_section->flags & SEC_ALLOC) != 0)
5231 {
861fb55a 5232 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5233 where the symbol will end up. So, we create a relocation
5234 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5235 linker. We must do the same for executable references to
5236 shared library symbols, unless we've decided to use copy
5237 relocs or PLTs instead. */
b49e97c9
TS
5238 value = addend;
5239 if (!mips_elf_create_dynamic_relocation (abfd,
5240 info,
5241 relocation,
5242 h,
5243 sec,
5244 symbol,
5245 &value,
5246 input_section))
5247 return bfd_reloc_undefined;
5248 }
5249 else
5250 {
5251 if (r_type != R_MIPS_REL32)
5252 value = symbol + addend;
5253 else
5254 value = addend;
5255 }
5256 value &= howto->dst_mask;
092dcd75
CD
5257 break;
5258
5259 case R_MIPS_PC32:
5260 value = symbol + addend - p;
5261 value &= howto->dst_mask;
b49e97c9
TS
5262 break;
5263
b49e97c9
TS
5264 case R_MIPS16_26:
5265 /* The calculation for R_MIPS16_26 is just the same as for an
5266 R_MIPS_26. It's only the storage of the relocated field into
5267 the output file that's different. That's handled in
5268 mips_elf_perform_relocation. So, we just fall through to the
5269 R_MIPS_26 case here. */
5270 case R_MIPS_26:
5271 if (local_p)
30ac9238 5272 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
b49e97c9 5273 else
728b2f21
ILT
5274 {
5275 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
c314987d
RS
5276 if (h->root.root.type != bfd_link_hash_undefweak)
5277 overflowed_p = (value >> 26) != ((p + 4) >> 28);
728b2f21 5278 }
b49e97c9
TS
5279 value &= howto->dst_mask;
5280 break;
5281
0f20cc35
DJ
5282 case R_MIPS_TLS_DTPREL_HI16:
5283 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5284 & howto->dst_mask);
5285 break;
5286
5287 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
5288 case R_MIPS_TLS_DTPREL32:
5289 case R_MIPS_TLS_DTPREL64:
0f20cc35
DJ
5290 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5291 break;
5292
5293 case R_MIPS_TLS_TPREL_HI16:
5294 value = (mips_elf_high (addend + symbol - tprel_base (info))
5295 & howto->dst_mask);
5296 break;
5297
5298 case R_MIPS_TLS_TPREL_LO16:
5299 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5300 break;
5301
b49e97c9 5302 case R_MIPS_HI16:
d6f16593 5303 case R_MIPS16_HI16:
b49e97c9
TS
5304 if (!gp_disp_p)
5305 {
5306 value = mips_elf_high (addend + symbol);
5307 value &= howto->dst_mask;
5308 }
5309 else
5310 {
d6f16593
MR
5311 /* For MIPS16 ABI code we generate this sequence
5312 0: li $v0,%hi(_gp_disp)
5313 4: addiupc $v1,%lo(_gp_disp)
5314 8: sll $v0,16
5315 12: addu $v0,$v1
5316 14: move $gp,$v0
5317 So the offsets of hi and lo relocs are the same, but the
5318 $pc is four higher than $t9 would be, so reduce
5319 both reloc addends by 4. */
5320 if (r_type == R_MIPS16_HI16)
5321 value = mips_elf_high (addend + gp - p - 4);
5322 else
5323 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
5324 overflowed_p = mips_elf_overflow_p (value, 16);
5325 }
5326 break;
5327
5328 case R_MIPS_LO16:
d6f16593 5329 case R_MIPS16_LO16:
b49e97c9
TS
5330 if (!gp_disp_p)
5331 value = (symbol + addend) & howto->dst_mask;
5332 else
5333 {
d6f16593
MR
5334 /* See the comment for R_MIPS16_HI16 above for the reason
5335 for this conditional. */
5336 if (r_type == R_MIPS16_LO16)
5337 value = addend + gp - p;
5338 else
5339 value = addend + gp - p + 4;
b49e97c9 5340 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 5341 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
5342 _gp_disp are normally generated from the .cpload
5343 pseudo-op. It generates code that normally looks like
5344 this:
5345
5346 lui $gp,%hi(_gp_disp)
5347 addiu $gp,$gp,%lo(_gp_disp)
5348 addu $gp,$gp,$t9
5349
5350 Here $t9 holds the address of the function being called,
5351 as required by the MIPS ELF ABI. The R_MIPS_LO16
5352 relocation can easily overflow in this situation, but the
5353 R_MIPS_HI16 relocation will handle the overflow.
5354 Therefore, we consider this a bug in the MIPS ABI, and do
5355 not check for overflow here. */
5356 }
5357 break;
5358
5359 case R_MIPS_LITERAL:
5360 /* Because we don't merge literal sections, we can handle this
5361 just like R_MIPS_GPREL16. In the long run, we should merge
5362 shared literals, and then we will need to additional work
5363 here. */
5364
5365 /* Fall through. */
5366
5367 case R_MIPS16_GPREL:
5368 /* The R_MIPS16_GPREL performs the same calculation as
5369 R_MIPS_GPREL16, but stores the relocated bits in a different
5370 order. We don't need to do anything special here; the
5371 differences are handled in mips_elf_perform_relocation. */
5372 case R_MIPS_GPREL16:
bce03d3d
AO
5373 /* Only sign-extend the addend if it was extracted from the
5374 instruction. If the addend was separate, leave it alone,
5375 otherwise we may lose significant bits. */
5376 if (howto->partial_inplace)
a7ebbfdf 5377 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
5378 value = symbol + addend - gp;
5379 /* If the symbol was local, any earlier relocatable links will
5380 have adjusted its addend with the gp offset, so compensate
5381 for that now. Don't do it for symbols forced local in this
5382 link, though, since they won't have had the gp offset applied
5383 to them before. */
5384 if (was_local_p)
5385 value += gp0;
b49e97c9
TS
5386 overflowed_p = mips_elf_overflow_p (value, 16);
5387 break;
5388
738e5348
RS
5389 case R_MIPS16_GOT16:
5390 case R_MIPS16_CALL16:
b49e97c9
TS
5391 case R_MIPS_GOT16:
5392 case R_MIPS_CALL16:
0a44bf69 5393 /* VxWorks does not have separate local and global semantics for
738e5348 5394 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 5395 if (!htab->is_vxworks && local_p)
b49e97c9 5396 {
b34976b6 5397 bfd_boolean forced;
b49e97c9 5398
b49e97c9 5399 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 5400 local_sections, FALSE);
5c18022e 5401 value = mips_elf_got16_entry (abfd, input_bfd, info,
f4416af6 5402 symbol + addend, forced);
b49e97c9
TS
5403 if (value == MINUS_ONE)
5404 return bfd_reloc_outofrange;
5405 value
a8028dd0 5406 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5407 overflowed_p = mips_elf_overflow_p (value, 16);
5408 break;
5409 }
5410
5411 /* Fall through. */
5412
0f20cc35
DJ
5413 case R_MIPS_TLS_GD:
5414 case R_MIPS_TLS_GOTTPREL:
5415 case R_MIPS_TLS_LDM:
b49e97c9 5416 case R_MIPS_GOT_DISP:
0fdc1bf1 5417 got_disp:
b49e97c9
TS
5418 value = g;
5419 overflowed_p = mips_elf_overflow_p (value, 16);
5420 break;
5421
5422 case R_MIPS_GPREL32:
bce03d3d
AO
5423 value = (addend + symbol + gp0 - gp);
5424 if (!save_addend)
5425 value &= howto->dst_mask;
b49e97c9
TS
5426 break;
5427
5428 case R_MIPS_PC16:
bad36eac
DJ
5429 case R_MIPS_GNU_REL16_S2:
5430 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5431 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
5432 value >>= howto->rightshift;
5433 value &= howto->dst_mask;
b49e97c9
TS
5434 break;
5435
5436 case R_MIPS_GOT_HI16:
5437 case R_MIPS_CALL_HI16:
5438 /* We're allowed to handle these two relocations identically.
5439 The dynamic linker is allowed to handle the CALL relocations
5440 differently by creating a lazy evaluation stub. */
5441 value = g;
5442 value = mips_elf_high (value);
5443 value &= howto->dst_mask;
5444 break;
5445
5446 case R_MIPS_GOT_LO16:
5447 case R_MIPS_CALL_LO16:
5448 value = g & howto->dst_mask;
5449 break;
5450
5451 case R_MIPS_GOT_PAGE:
0fdc1bf1
AO
5452 /* GOT_PAGE relocations that reference non-local symbols decay
5453 to GOT_DISP. The corresponding GOT_OFST relocation decays to
5454 0. */
93a2b7ae 5455 if (! local_p)
0fdc1bf1 5456 goto got_disp;
5c18022e 5457 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
5458 if (value == MINUS_ONE)
5459 return bfd_reloc_outofrange;
a8028dd0 5460 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
5461 overflowed_p = mips_elf_overflow_p (value, 16);
5462 break;
5463
5464 case R_MIPS_GOT_OFST:
93a2b7ae 5465 if (local_p)
5c18022e 5466 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
5467 else
5468 value = addend;
b49e97c9
TS
5469 overflowed_p = mips_elf_overflow_p (value, 16);
5470 break;
5471
5472 case R_MIPS_SUB:
5473 value = symbol - addend;
5474 value &= howto->dst_mask;
5475 break;
5476
5477 case R_MIPS_HIGHER:
5478 value = mips_elf_higher (addend + symbol);
5479 value &= howto->dst_mask;
5480 break;
5481
5482 case R_MIPS_HIGHEST:
5483 value = mips_elf_highest (addend + symbol);
5484 value &= howto->dst_mask;
5485 break;
5486
5487 case R_MIPS_SCN_DISP:
5488 value = symbol + addend - sec->output_offset;
5489 value &= howto->dst_mask;
5490 break;
5491
b49e97c9 5492 case R_MIPS_JALR:
1367d393
ILT
5493 /* This relocation is only a hint. In some cases, we optimize
5494 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
5495 when the symbol does not resolve locally. */
5496 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393
ILT
5497 return bfd_reloc_continue;
5498 value = symbol + addend;
5499 break;
b49e97c9 5500
1367d393 5501 case R_MIPS_PJUMP:
b49e97c9
TS
5502 case R_MIPS_GNU_VTINHERIT:
5503 case R_MIPS_GNU_VTENTRY:
5504 /* We don't do anything with these at present. */
5505 return bfd_reloc_continue;
5506
5507 default:
5508 /* An unrecognized relocation type. */
5509 return bfd_reloc_notsupported;
5510 }
5511
5512 /* Store the VALUE for our caller. */
5513 *valuep = value;
5514 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5515}
5516
5517/* Obtain the field relocated by RELOCATION. */
5518
5519static bfd_vma
9719ad41
RS
5520mips_elf_obtain_contents (reloc_howto_type *howto,
5521 const Elf_Internal_Rela *relocation,
5522 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
5523{
5524 bfd_vma x;
5525 bfd_byte *location = contents + relocation->r_offset;
5526
5527 /* Obtain the bytes. */
5528 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5529
b49e97c9
TS
5530 return x;
5531}
5532
5533/* It has been determined that the result of the RELOCATION is the
5534 VALUE. Use HOWTO to place VALUE into the output file at the
5535 appropriate position. The SECTION is the section to which the
b34976b6 5536 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
b49e97c9
TS
5537 for the relocation must be either JAL or JALX, and it is
5538 unconditionally converted to JALX.
5539
b34976b6 5540 Returns FALSE if anything goes wrong. */
b49e97c9 5541
b34976b6 5542static bfd_boolean
9719ad41
RS
5543mips_elf_perform_relocation (struct bfd_link_info *info,
5544 reloc_howto_type *howto,
5545 const Elf_Internal_Rela *relocation,
5546 bfd_vma value, bfd *input_bfd,
5547 asection *input_section, bfd_byte *contents,
5548 bfd_boolean require_jalx)
b49e97c9
TS
5549{
5550 bfd_vma x;
5551 bfd_byte *location;
5552 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5553
5554 /* Figure out where the relocation is occurring. */
5555 location = contents + relocation->r_offset;
5556
d6f16593
MR
5557 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5558
b49e97c9
TS
5559 /* Obtain the current value. */
5560 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5561
5562 /* Clear the field we are setting. */
5563 x &= ~howto->dst_mask;
5564
b49e97c9
TS
5565 /* Set the field. */
5566 x |= (value & howto->dst_mask);
5567
5568 /* If required, turn JAL into JALX. */
5569 if (require_jalx)
5570 {
b34976b6 5571 bfd_boolean ok;
b49e97c9
TS
5572 bfd_vma opcode = x >> 26;
5573 bfd_vma jalx_opcode;
5574
5575 /* Check to see if the opcode is already JAL or JALX. */
5576 if (r_type == R_MIPS16_26)
5577 {
5578 ok = ((opcode == 0x6) || (opcode == 0x7));
5579 jalx_opcode = 0x7;
5580 }
5581 else
5582 {
5583 ok = ((opcode == 0x3) || (opcode == 0x1d));
5584 jalx_opcode = 0x1d;
5585 }
5586
5587 /* If the opcode is not JAL or JALX, there's a problem. */
5588 if (!ok)
5589 {
5590 (*_bfd_error_handler)
d003868e
AM
5591 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5592 input_bfd,
5593 input_section,
b49e97c9
TS
5594 (unsigned long) relocation->r_offset);
5595 bfd_set_error (bfd_error_bad_value);
b34976b6 5596 return FALSE;
b49e97c9
TS
5597 }
5598
5599 /* Make this the JALX opcode. */
5600 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5601 }
5602
cd8d5a82
CF
5603 /* Try converting JAL and JALR to BAL, if the target is in range. */
5604 if (!info->relocatable
1367d393 5605 && !require_jalx
cd8d5a82
CF
5606 && ((JAL_TO_BAL_P (input_bfd)
5607 && r_type == R_MIPS_26
5608 && (x >> 26) == 0x3) /* jal addr */
5609 || (JALR_TO_BAL_P (input_bfd)
5610 && r_type == R_MIPS_JALR
5611 && x == 0x0320f809))) /* jalr t9 */
1367d393
ILT
5612 {
5613 bfd_vma addr;
5614 bfd_vma dest;
5615 bfd_signed_vma off;
5616
5617 addr = (input_section->output_section->vma
5618 + input_section->output_offset
5619 + relocation->r_offset
5620 + 4);
5621 if (r_type == R_MIPS_26)
5622 dest = (value << 2) | ((addr >> 28) << 28);
5623 else
5624 dest = value;
5625 off = dest - addr;
5626 if (off <= 0x1ffff && off >= -0x20000)
5627 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5628 }
5629
b49e97c9
TS
5630 /* Put the value into the output. */
5631 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593
MR
5632
5633 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5634 location);
5635
b34976b6 5636 return TRUE;
b49e97c9 5637}
b49e97c9 5638\f
b49e97c9
TS
5639/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5640 is the original relocation, which is now being transformed into a
5641 dynamic relocation. The ADDENDP is adjusted if necessary; the
5642 caller should store the result in place of the original addend. */
5643
b34976b6 5644static bfd_boolean
9719ad41
RS
5645mips_elf_create_dynamic_relocation (bfd *output_bfd,
5646 struct bfd_link_info *info,
5647 const Elf_Internal_Rela *rel,
5648 struct mips_elf_link_hash_entry *h,
5649 asection *sec, bfd_vma symbol,
5650 bfd_vma *addendp, asection *input_section)
b49e97c9 5651{
947216bf 5652 Elf_Internal_Rela outrel[3];
b49e97c9
TS
5653 asection *sreloc;
5654 bfd *dynobj;
5655 int r_type;
5d41f0b6
RS
5656 long indx;
5657 bfd_boolean defined_p;
0a44bf69 5658 struct mips_elf_link_hash_table *htab;
b49e97c9 5659
0a44bf69 5660 htab = mips_elf_hash_table (info);
b49e97c9
TS
5661 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5662 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 5663 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
5664 BFD_ASSERT (sreloc != NULL);
5665 BFD_ASSERT (sreloc->contents != NULL);
5666 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 5667 < sreloc->size);
b49e97c9 5668
b49e97c9
TS
5669 outrel[0].r_offset =
5670 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
5671 if (ABI_64_P (output_bfd))
5672 {
5673 outrel[1].r_offset =
5674 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5675 outrel[2].r_offset =
5676 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5677 }
b49e97c9 5678
c5ae1840 5679 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 5680 /* The relocation field has been deleted. */
5d41f0b6
RS
5681 return TRUE;
5682
5683 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
5684 {
5685 /* The relocation field has been converted into a relative value of
5686 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5687 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 5688 *addendp += symbol;
5d41f0b6 5689 return TRUE;
0d591ff7 5690 }
b49e97c9 5691
5d41f0b6
RS
5692 /* We must now calculate the dynamic symbol table index to use
5693 in the relocation. */
5694 if (h != NULL
6ece8836
TS
5695 && (!h->root.def_regular
5696 || (info->shared && !info->symbolic && !h->root.forced_local)))
5d41f0b6
RS
5697 {
5698 indx = h->root.dynindx;
5699 if (SGI_COMPAT (output_bfd))
5700 defined_p = h->root.def_regular;
5701 else
5702 /* ??? glibc's ld.so just adds the final GOT entry to the
5703 relocation field. It therefore treats relocs against
5704 defined symbols in the same way as relocs against
5705 undefined symbols. */
5706 defined_p = FALSE;
5707 }
b49e97c9
TS
5708 else
5709 {
5d41f0b6
RS
5710 if (sec != NULL && bfd_is_abs_section (sec))
5711 indx = 0;
5712 else if (sec == NULL || sec->owner == NULL)
fdd07405 5713 {
5d41f0b6
RS
5714 bfd_set_error (bfd_error_bad_value);
5715 return FALSE;
b49e97c9
TS
5716 }
5717 else
5718 {
5d41f0b6 5719 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
5720 if (indx == 0)
5721 {
5722 asection *osec = htab->root.text_index_section;
5723 indx = elf_section_data (osec)->dynindx;
5724 }
5d41f0b6
RS
5725 if (indx == 0)
5726 abort ();
b49e97c9
TS
5727 }
5728
5d41f0b6
RS
5729 /* Instead of generating a relocation using the section
5730 symbol, we may as well make it a fully relative
5731 relocation. We want to avoid generating relocations to
5732 local symbols because we used to generate them
5733 incorrectly, without adding the original symbol value,
5734 which is mandated by the ABI for section symbols. In
5735 order to give dynamic loaders and applications time to
5736 phase out the incorrect use, we refrain from emitting
5737 section-relative relocations. It's not like they're
5738 useful, after all. This should be a bit more efficient
5739 as well. */
5740 /* ??? Although this behavior is compatible with glibc's ld.so,
5741 the ABI says that relocations against STN_UNDEF should have
5742 a symbol value of 0. Irix rld honors this, so relocations
5743 against STN_UNDEF have no effect. */
5744 if (!SGI_COMPAT (output_bfd))
5745 indx = 0;
5746 defined_p = TRUE;
b49e97c9
TS
5747 }
5748
5d41f0b6
RS
5749 /* If the relocation was previously an absolute relocation and
5750 this symbol will not be referred to by the relocation, we must
5751 adjust it by the value we give it in the dynamic symbol table.
5752 Otherwise leave the job up to the dynamic linker. */
5753 if (defined_p && r_type != R_MIPS_REL32)
5754 *addendp += symbol;
5755
0a44bf69
RS
5756 if (htab->is_vxworks)
5757 /* VxWorks uses non-relative relocations for this. */
5758 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5759 else
5760 /* The relocation is always an REL32 relocation because we don't
5761 know where the shared library will wind up at load-time. */
5762 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5763 R_MIPS_REL32);
5764
5d41f0b6
RS
5765 /* For strict adherence to the ABI specification, we should
5766 generate a R_MIPS_64 relocation record by itself before the
5767 _REL32/_64 record as well, such that the addend is read in as
5768 a 64-bit value (REL32 is a 32-bit relocation, after all).
5769 However, since none of the existing ELF64 MIPS dynamic
5770 loaders seems to care, we don't waste space with these
5771 artificial relocations. If this turns out to not be true,
5772 mips_elf_allocate_dynamic_relocation() should be tweaked so
5773 as to make room for a pair of dynamic relocations per
5774 invocation if ABI_64_P, and here we should generate an
5775 additional relocation record with R_MIPS_64 by itself for a
5776 NULL symbol before this relocation record. */
5777 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5778 ABI_64_P (output_bfd)
5779 ? R_MIPS_64
5780 : R_MIPS_NONE);
5781 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5782
5783 /* Adjust the output offset of the relocation to reference the
5784 correct location in the output file. */
5785 outrel[0].r_offset += (input_section->output_section->vma
5786 + input_section->output_offset);
5787 outrel[1].r_offset += (input_section->output_section->vma
5788 + input_section->output_offset);
5789 outrel[2].r_offset += (input_section->output_section->vma
5790 + input_section->output_offset);
5791
b49e97c9
TS
5792 /* Put the relocation back out. We have to use the special
5793 relocation outputter in the 64-bit case since the 64-bit
5794 relocation format is non-standard. */
5795 if (ABI_64_P (output_bfd))
5796 {
5797 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5798 (output_bfd, &outrel[0],
5799 (sreloc->contents
5800 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5801 }
0a44bf69
RS
5802 else if (htab->is_vxworks)
5803 {
5804 /* VxWorks uses RELA rather than REL dynamic relocations. */
5805 outrel[0].r_addend = *addendp;
5806 bfd_elf32_swap_reloca_out
5807 (output_bfd, &outrel[0],
5808 (sreloc->contents
5809 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5810 }
b49e97c9 5811 else
947216bf
AM
5812 bfd_elf32_swap_reloc_out
5813 (output_bfd, &outrel[0],
5814 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 5815
b49e97c9
TS
5816 /* We've now added another relocation. */
5817 ++sreloc->reloc_count;
5818
5819 /* Make sure the output section is writable. The dynamic linker
5820 will be writing to it. */
5821 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5822 |= SHF_WRITE;
5823
5824 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 5825 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9
TS
5826 {
5827 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5828 bfd_byte *cr;
5829
5830 if (scpt)
5831 {
5832 Elf32_crinfo cptrel;
5833
5834 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5835 cptrel.vaddr = (rel->r_offset
5836 + input_section->output_section->vma
5837 + input_section->output_offset);
5838 if (r_type == R_MIPS_REL32)
5839 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5840 else
5841 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5842 mips_elf_set_cr_dist2to (cptrel, 0);
5843 cptrel.konst = *addendp;
5844
5845 cr = (scpt->contents
5846 + sizeof (Elf32_External_compact_rel));
abc0f8d0 5847 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
5848 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5849 ((Elf32_External_crinfo *) cr
5850 + scpt->reloc_count));
5851 ++scpt->reloc_count;
5852 }
5853 }
5854
943284cc
DJ
5855 /* If we've written this relocation for a readonly section,
5856 we need to set DF_TEXTREL again, so that we do not delete the
5857 DT_TEXTREL tag. */
5858 if (MIPS_ELF_READONLY_SECTION (input_section))
5859 info->flags |= DF_TEXTREL;
5860
b34976b6 5861 return TRUE;
b49e97c9
TS
5862}
5863\f
b49e97c9
TS
5864/* Return the MACH for a MIPS e_flags value. */
5865
5866unsigned long
9719ad41 5867_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
5868{
5869 switch (flags & EF_MIPS_MACH)
5870 {
5871 case E_MIPS_MACH_3900:
5872 return bfd_mach_mips3900;
5873
5874 case E_MIPS_MACH_4010:
5875 return bfd_mach_mips4010;
5876
5877 case E_MIPS_MACH_4100:
5878 return bfd_mach_mips4100;
5879
5880 case E_MIPS_MACH_4111:
5881 return bfd_mach_mips4111;
5882
00707a0e
RS
5883 case E_MIPS_MACH_4120:
5884 return bfd_mach_mips4120;
5885
b49e97c9
TS
5886 case E_MIPS_MACH_4650:
5887 return bfd_mach_mips4650;
5888
00707a0e
RS
5889 case E_MIPS_MACH_5400:
5890 return bfd_mach_mips5400;
5891
5892 case E_MIPS_MACH_5500:
5893 return bfd_mach_mips5500;
5894
0d2e43ed
ILT
5895 case E_MIPS_MACH_9000:
5896 return bfd_mach_mips9000;
5897
b49e97c9
TS
5898 case E_MIPS_MACH_SB1:
5899 return bfd_mach_mips_sb1;
5900
350cc38d
MS
5901 case E_MIPS_MACH_LS2E:
5902 return bfd_mach_mips_loongson_2e;
5903
5904 case E_MIPS_MACH_LS2F:
5905 return bfd_mach_mips_loongson_2f;
5906
6f179bd0
AN
5907 case E_MIPS_MACH_OCTEON:
5908 return bfd_mach_mips_octeon;
5909
52b6b6b9
JM
5910 case E_MIPS_MACH_XLR:
5911 return bfd_mach_mips_xlr;
5912
b49e97c9
TS
5913 default:
5914 switch (flags & EF_MIPS_ARCH)
5915 {
5916 default:
5917 case E_MIPS_ARCH_1:
5918 return bfd_mach_mips3000;
b49e97c9
TS
5919
5920 case E_MIPS_ARCH_2:
5921 return bfd_mach_mips6000;
b49e97c9
TS
5922
5923 case E_MIPS_ARCH_3:
5924 return bfd_mach_mips4000;
b49e97c9
TS
5925
5926 case E_MIPS_ARCH_4:
5927 return bfd_mach_mips8000;
b49e97c9
TS
5928
5929 case E_MIPS_ARCH_5:
5930 return bfd_mach_mips5;
b49e97c9
TS
5931
5932 case E_MIPS_ARCH_32:
5933 return bfd_mach_mipsisa32;
b49e97c9
TS
5934
5935 case E_MIPS_ARCH_64:
5936 return bfd_mach_mipsisa64;
af7ee8bf
CD
5937
5938 case E_MIPS_ARCH_32R2:
5939 return bfd_mach_mipsisa32r2;
5f74bc13
CD
5940
5941 case E_MIPS_ARCH_64R2:
5942 return bfd_mach_mipsisa64r2;
b49e97c9
TS
5943 }
5944 }
5945
5946 return 0;
5947}
5948
5949/* Return printable name for ABI. */
5950
5951static INLINE char *
9719ad41 5952elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
5953{
5954 flagword flags;
5955
5956 flags = elf_elfheader (abfd)->e_flags;
5957 switch (flags & EF_MIPS_ABI)
5958 {
5959 case 0:
5960 if (ABI_N32_P (abfd))
5961 return "N32";
5962 else if (ABI_64_P (abfd))
5963 return "64";
5964 else
5965 return "none";
5966 case E_MIPS_ABI_O32:
5967 return "O32";
5968 case E_MIPS_ABI_O64:
5969 return "O64";
5970 case E_MIPS_ABI_EABI32:
5971 return "EABI32";
5972 case E_MIPS_ABI_EABI64:
5973 return "EABI64";
5974 default:
5975 return "unknown abi";
5976 }
5977}
5978\f
5979/* MIPS ELF uses two common sections. One is the usual one, and the
5980 other is for small objects. All the small objects are kept
5981 together, and then referenced via the gp pointer, which yields
5982 faster assembler code. This is what we use for the small common
5983 section. This approach is copied from ecoff.c. */
5984static asection mips_elf_scom_section;
5985static asymbol mips_elf_scom_symbol;
5986static asymbol *mips_elf_scom_symbol_ptr;
5987
5988/* MIPS ELF also uses an acommon section, which represents an
5989 allocated common symbol which may be overridden by a
5990 definition in a shared library. */
5991static asection mips_elf_acom_section;
5992static asymbol mips_elf_acom_symbol;
5993static asymbol *mips_elf_acom_symbol_ptr;
5994
738e5348 5995/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
5996
5997void
9719ad41 5998_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
5999{
6000 elf_symbol_type *elfsym;
6001
738e5348 6002 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
6003 elfsym = (elf_symbol_type *) asym;
6004 switch (elfsym->internal_elf_sym.st_shndx)
6005 {
6006 case SHN_MIPS_ACOMMON:
6007 /* This section is used in a dynamically linked executable file.
6008 It is an allocated common section. The dynamic linker can
6009 either resolve these symbols to something in a shared
6010 library, or it can just leave them here. For our purposes,
6011 we can consider these symbols to be in a new section. */
6012 if (mips_elf_acom_section.name == NULL)
6013 {
6014 /* Initialize the acommon section. */
6015 mips_elf_acom_section.name = ".acommon";
6016 mips_elf_acom_section.flags = SEC_ALLOC;
6017 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6018 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6019 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6020 mips_elf_acom_symbol.name = ".acommon";
6021 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6022 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6023 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6024 }
6025 asym->section = &mips_elf_acom_section;
6026 break;
6027
6028 case SHN_COMMON:
6029 /* Common symbols less than the GP size are automatically
6030 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6031 if (asym->value > elf_gp_size (abfd)
b59eed79 6032 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
6033 || IRIX_COMPAT (abfd) == ict_irix6)
6034 break;
6035 /* Fall through. */
6036 case SHN_MIPS_SCOMMON:
6037 if (mips_elf_scom_section.name == NULL)
6038 {
6039 /* Initialize the small common section. */
6040 mips_elf_scom_section.name = ".scommon";
6041 mips_elf_scom_section.flags = SEC_IS_COMMON;
6042 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6043 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6044 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6045 mips_elf_scom_symbol.name = ".scommon";
6046 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6047 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6048 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6049 }
6050 asym->section = &mips_elf_scom_section;
6051 asym->value = elfsym->internal_elf_sym.st_size;
6052 break;
6053
6054 case SHN_MIPS_SUNDEFINED:
6055 asym->section = bfd_und_section_ptr;
6056 break;
6057
b49e97c9 6058 case SHN_MIPS_TEXT:
00b4930b
TS
6059 {
6060 asection *section = bfd_get_section_by_name (abfd, ".text");
6061
6062 BFD_ASSERT (SGI_COMPAT (abfd));
6063 if (section != NULL)
6064 {
6065 asym->section = section;
6066 /* MIPS_TEXT is a bit special, the address is not an offset
6067 to the base of the .text section. So substract the section
6068 base address to make it an offset. */
6069 asym->value -= section->vma;
6070 }
6071 }
b49e97c9
TS
6072 break;
6073
6074 case SHN_MIPS_DATA:
00b4930b
TS
6075 {
6076 asection *section = bfd_get_section_by_name (abfd, ".data");
6077
6078 BFD_ASSERT (SGI_COMPAT (abfd));
6079 if (section != NULL)
6080 {
6081 asym->section = section;
6082 /* MIPS_DATA is a bit special, the address is not an offset
6083 to the base of the .data section. So substract the section
6084 base address to make it an offset. */
6085 asym->value -= section->vma;
6086 }
6087 }
b49e97c9 6088 break;
b49e97c9 6089 }
738e5348
RS
6090
6091 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6092 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6093 && (asym->value & 1) != 0)
6094 {
6095 asym->value--;
6096 elfsym->internal_elf_sym.st_other
6097 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6098 }
b49e97c9
TS
6099}
6100\f
8c946ed5
RS
6101/* Implement elf_backend_eh_frame_address_size. This differs from
6102 the default in the way it handles EABI64.
6103
6104 EABI64 was originally specified as an LP64 ABI, and that is what
6105 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6106 historically accepted the combination of -mabi=eabi and -mlong32,
6107 and this ILP32 variation has become semi-official over time.
6108 Both forms use elf32 and have pointer-sized FDE addresses.
6109
6110 If an EABI object was generated by GCC 4.0 or above, it will have
6111 an empty .gcc_compiled_longXX section, where XX is the size of longs
6112 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6113 have no special marking to distinguish them from LP64 objects.
6114
6115 We don't want users of the official LP64 ABI to be punished for the
6116 existence of the ILP32 variant, but at the same time, we don't want
6117 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6118 We therefore take the following approach:
6119
6120 - If ABFD contains a .gcc_compiled_longXX section, use it to
6121 determine the pointer size.
6122
6123 - Otherwise check the type of the first relocation. Assume that
6124 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6125
6126 - Otherwise punt.
6127
6128 The second check is enough to detect LP64 objects generated by pre-4.0
6129 compilers because, in the kind of output generated by those compilers,
6130 the first relocation will be associated with either a CIE personality
6131 routine or an FDE start address. Furthermore, the compilers never
6132 used a special (non-pointer) encoding for this ABI.
6133
6134 Checking the relocation type should also be safe because there is no
6135 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6136 did so. */
6137
6138unsigned int
6139_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6140{
6141 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6142 return 8;
6143 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6144 {
6145 bfd_boolean long32_p, long64_p;
6146
6147 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6148 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6149 if (long32_p && long64_p)
6150 return 0;
6151 if (long32_p)
6152 return 4;
6153 if (long64_p)
6154 return 8;
6155
6156 if (sec->reloc_count > 0
6157 && elf_section_data (sec)->relocs != NULL
6158 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6159 == R_MIPS_64))
6160 return 8;
6161
6162 return 0;
6163 }
6164 return 4;
6165}
6166\f
174fd7f9
RS
6167/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6168 relocations against two unnamed section symbols to resolve to the
6169 same address. For example, if we have code like:
6170
6171 lw $4,%got_disp(.data)($gp)
6172 lw $25,%got_disp(.text)($gp)
6173 jalr $25
6174
6175 then the linker will resolve both relocations to .data and the program
6176 will jump there rather than to .text.
6177
6178 We can work around this problem by giving names to local section symbols.
6179 This is also what the MIPSpro tools do. */
6180
6181bfd_boolean
6182_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6183{
6184 return SGI_COMPAT (abfd);
6185}
6186\f
b49e97c9
TS
6187/* Work over a section just before writing it out. This routine is
6188 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6189 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6190 a better way. */
6191
b34976b6 6192bfd_boolean
9719ad41 6193_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
6194{
6195 if (hdr->sh_type == SHT_MIPS_REGINFO
6196 && hdr->sh_size > 0)
6197 {
6198 bfd_byte buf[4];
6199
6200 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6201 BFD_ASSERT (hdr->contents == NULL);
6202
6203 if (bfd_seek (abfd,
6204 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6205 SEEK_SET) != 0)
b34976b6 6206 return FALSE;
b49e97c9 6207 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6208 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6209 return FALSE;
b49e97c9
TS
6210 }
6211
6212 if (hdr->sh_type == SHT_MIPS_OPTIONS
6213 && hdr->bfd_section != NULL
f0abc2a1
AM
6214 && mips_elf_section_data (hdr->bfd_section) != NULL
6215 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
6216 {
6217 bfd_byte *contents, *l, *lend;
6218
f0abc2a1
AM
6219 /* We stored the section contents in the tdata field in the
6220 set_section_contents routine. We save the section contents
6221 so that we don't have to read them again.
b49e97c9
TS
6222 At this point we know that elf_gp is set, so we can look
6223 through the section contents to see if there is an
6224 ODK_REGINFO structure. */
6225
f0abc2a1 6226 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
6227 l = contents;
6228 lend = contents + hdr->sh_size;
6229 while (l + sizeof (Elf_External_Options) <= lend)
6230 {
6231 Elf_Internal_Options intopt;
6232
6233 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6234 &intopt);
1bc8074d
MR
6235 if (intopt.size < sizeof (Elf_External_Options))
6236 {
6237 (*_bfd_error_handler)
6238 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6239 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6240 break;
6241 }
b49e97c9
TS
6242 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6243 {
6244 bfd_byte buf[8];
6245
6246 if (bfd_seek (abfd,
6247 (hdr->sh_offset
6248 + (l - contents)
6249 + sizeof (Elf_External_Options)
6250 + (sizeof (Elf64_External_RegInfo) - 8)),
6251 SEEK_SET) != 0)
b34976b6 6252 return FALSE;
b49e97c9 6253 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 6254 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 6255 return FALSE;
b49e97c9
TS
6256 }
6257 else if (intopt.kind == ODK_REGINFO)
6258 {
6259 bfd_byte buf[4];
6260
6261 if (bfd_seek (abfd,
6262 (hdr->sh_offset
6263 + (l - contents)
6264 + sizeof (Elf_External_Options)
6265 + (sizeof (Elf32_External_RegInfo) - 4)),
6266 SEEK_SET) != 0)
b34976b6 6267 return FALSE;
b49e97c9 6268 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 6269 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 6270 return FALSE;
b49e97c9
TS
6271 }
6272 l += intopt.size;
6273 }
6274 }
6275
6276 if (hdr->bfd_section != NULL)
6277 {
6278 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6279
2d0f9ad9
JM
6280 /* .sbss is not handled specially here because the GNU/Linux
6281 prelinker can convert .sbss from NOBITS to PROGBITS and
6282 changing it back to NOBITS breaks the binary. The entry in
6283 _bfd_mips_elf_special_sections will ensure the correct flags
6284 are set on .sbss if BFD creates it without reading it from an
6285 input file, and without special handling here the flags set
6286 on it in an input file will be followed. */
b49e97c9
TS
6287 if (strcmp (name, ".sdata") == 0
6288 || strcmp (name, ".lit8") == 0
6289 || strcmp (name, ".lit4") == 0)
6290 {
6291 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6292 hdr->sh_type = SHT_PROGBITS;
6293 }
b49e97c9
TS
6294 else if (strcmp (name, ".srdata") == 0)
6295 {
6296 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6297 hdr->sh_type = SHT_PROGBITS;
6298 }
6299 else if (strcmp (name, ".compact_rel") == 0)
6300 {
6301 hdr->sh_flags = 0;
6302 hdr->sh_type = SHT_PROGBITS;
6303 }
6304 else if (strcmp (name, ".rtproc") == 0)
6305 {
6306 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6307 {
6308 unsigned int adjust;
6309
6310 adjust = hdr->sh_size % hdr->sh_addralign;
6311 if (adjust != 0)
6312 hdr->sh_size += hdr->sh_addralign - adjust;
6313 }
6314 }
6315 }
6316
b34976b6 6317 return TRUE;
b49e97c9
TS
6318}
6319
6320/* Handle a MIPS specific section when reading an object file. This
6321 is called when elfcode.h finds a section with an unknown type.
6322 This routine supports both the 32-bit and 64-bit ELF ABI.
6323
6324 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6325 how to. */
6326
b34976b6 6327bfd_boolean
6dc132d9
L
6328_bfd_mips_elf_section_from_shdr (bfd *abfd,
6329 Elf_Internal_Shdr *hdr,
6330 const char *name,
6331 int shindex)
b49e97c9
TS
6332{
6333 flagword flags = 0;
6334
6335 /* There ought to be a place to keep ELF backend specific flags, but
6336 at the moment there isn't one. We just keep track of the
6337 sections by their name, instead. Fortunately, the ABI gives
6338 suggested names for all the MIPS specific sections, so we will
6339 probably get away with this. */
6340 switch (hdr->sh_type)
6341 {
6342 case SHT_MIPS_LIBLIST:
6343 if (strcmp (name, ".liblist") != 0)
b34976b6 6344 return FALSE;
b49e97c9
TS
6345 break;
6346 case SHT_MIPS_MSYM:
6347 if (strcmp (name, ".msym") != 0)
b34976b6 6348 return FALSE;
b49e97c9
TS
6349 break;
6350 case SHT_MIPS_CONFLICT:
6351 if (strcmp (name, ".conflict") != 0)
b34976b6 6352 return FALSE;
b49e97c9
TS
6353 break;
6354 case SHT_MIPS_GPTAB:
0112cd26 6355 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 6356 return FALSE;
b49e97c9
TS
6357 break;
6358 case SHT_MIPS_UCODE:
6359 if (strcmp (name, ".ucode") != 0)
b34976b6 6360 return FALSE;
b49e97c9
TS
6361 break;
6362 case SHT_MIPS_DEBUG:
6363 if (strcmp (name, ".mdebug") != 0)
b34976b6 6364 return FALSE;
b49e97c9
TS
6365 flags = SEC_DEBUGGING;
6366 break;
6367 case SHT_MIPS_REGINFO:
6368 if (strcmp (name, ".reginfo") != 0
6369 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 6370 return FALSE;
b49e97c9
TS
6371 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6372 break;
6373 case SHT_MIPS_IFACE:
6374 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 6375 return FALSE;
b49e97c9
TS
6376 break;
6377 case SHT_MIPS_CONTENT:
0112cd26 6378 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 6379 return FALSE;
b49e97c9
TS
6380 break;
6381 case SHT_MIPS_OPTIONS:
cc2e31b9 6382 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 6383 return FALSE;
b49e97c9
TS
6384 break;
6385 case SHT_MIPS_DWARF:
1b315056 6386 if (! CONST_STRNEQ (name, ".debug_")
355d10dc 6387 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 6388 return FALSE;
b49e97c9
TS
6389 break;
6390 case SHT_MIPS_SYMBOL_LIB:
6391 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 6392 return FALSE;
b49e97c9
TS
6393 break;
6394 case SHT_MIPS_EVENTS:
0112cd26
NC
6395 if (! CONST_STRNEQ (name, ".MIPS.events")
6396 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 6397 return FALSE;
b49e97c9
TS
6398 break;
6399 default:
cc2e31b9 6400 break;
b49e97c9
TS
6401 }
6402
6dc132d9 6403 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 6404 return FALSE;
b49e97c9
TS
6405
6406 if (flags)
6407 {
6408 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6409 (bfd_get_section_flags (abfd,
6410 hdr->bfd_section)
6411 | flags)))
b34976b6 6412 return FALSE;
b49e97c9
TS
6413 }
6414
6415 /* FIXME: We should record sh_info for a .gptab section. */
6416
6417 /* For a .reginfo section, set the gp value in the tdata information
6418 from the contents of this section. We need the gp value while
6419 processing relocs, so we just get it now. The .reginfo section
6420 is not used in the 64-bit MIPS ELF ABI. */
6421 if (hdr->sh_type == SHT_MIPS_REGINFO)
6422 {
6423 Elf32_External_RegInfo ext;
6424 Elf32_RegInfo s;
6425
9719ad41
RS
6426 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6427 &ext, 0, sizeof ext))
b34976b6 6428 return FALSE;
b49e97c9
TS
6429 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6430 elf_gp (abfd) = s.ri_gp_value;
6431 }
6432
6433 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6434 set the gp value based on what we find. We may see both
6435 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6436 they should agree. */
6437 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6438 {
6439 bfd_byte *contents, *l, *lend;
6440
9719ad41 6441 contents = bfd_malloc (hdr->sh_size);
b49e97c9 6442 if (contents == NULL)
b34976b6 6443 return FALSE;
b49e97c9 6444 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 6445 0, hdr->sh_size))
b49e97c9
TS
6446 {
6447 free (contents);
b34976b6 6448 return FALSE;
b49e97c9
TS
6449 }
6450 l = contents;
6451 lend = contents + hdr->sh_size;
6452 while (l + sizeof (Elf_External_Options) <= lend)
6453 {
6454 Elf_Internal_Options intopt;
6455
6456 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6457 &intopt);
1bc8074d
MR
6458 if (intopt.size < sizeof (Elf_External_Options))
6459 {
6460 (*_bfd_error_handler)
6461 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6462 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6463 break;
6464 }
b49e97c9
TS
6465 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6466 {
6467 Elf64_Internal_RegInfo intreg;
6468
6469 bfd_mips_elf64_swap_reginfo_in
6470 (abfd,
6471 ((Elf64_External_RegInfo *)
6472 (l + sizeof (Elf_External_Options))),
6473 &intreg);
6474 elf_gp (abfd) = intreg.ri_gp_value;
6475 }
6476 else if (intopt.kind == ODK_REGINFO)
6477 {
6478 Elf32_RegInfo intreg;
6479
6480 bfd_mips_elf32_swap_reginfo_in
6481 (abfd,
6482 ((Elf32_External_RegInfo *)
6483 (l + sizeof (Elf_External_Options))),
6484 &intreg);
6485 elf_gp (abfd) = intreg.ri_gp_value;
6486 }
6487 l += intopt.size;
6488 }
6489 free (contents);
6490 }
6491
b34976b6 6492 return TRUE;
b49e97c9
TS
6493}
6494
6495/* Set the correct type for a MIPS ELF section. We do this by the
6496 section name, which is a hack, but ought to work. This routine is
6497 used by both the 32-bit and the 64-bit ABI. */
6498
b34976b6 6499bfd_boolean
9719ad41 6500_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 6501{
0414f35b 6502 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
6503
6504 if (strcmp (name, ".liblist") == 0)
6505 {
6506 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 6507 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
6508 /* The sh_link field is set in final_write_processing. */
6509 }
6510 else if (strcmp (name, ".conflict") == 0)
6511 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 6512 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
6513 {
6514 hdr->sh_type = SHT_MIPS_GPTAB;
6515 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6516 /* The sh_info field is set in final_write_processing. */
6517 }
6518 else if (strcmp (name, ".ucode") == 0)
6519 hdr->sh_type = SHT_MIPS_UCODE;
6520 else if (strcmp (name, ".mdebug") == 0)
6521 {
6522 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 6523 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
6524 entsize of 0. FIXME: Does this matter? */
6525 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6526 hdr->sh_entsize = 0;
6527 else
6528 hdr->sh_entsize = 1;
6529 }
6530 else if (strcmp (name, ".reginfo") == 0)
6531 {
6532 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 6533 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
6534 entsize of 0x18. FIXME: Does this matter? */
6535 if (SGI_COMPAT (abfd))
6536 {
6537 if ((abfd->flags & DYNAMIC) != 0)
6538 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6539 else
6540 hdr->sh_entsize = 1;
6541 }
6542 else
6543 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6544 }
6545 else if (SGI_COMPAT (abfd)
6546 && (strcmp (name, ".hash") == 0
6547 || strcmp (name, ".dynamic") == 0
6548 || strcmp (name, ".dynstr") == 0))
6549 {
6550 if (SGI_COMPAT (abfd))
6551 hdr->sh_entsize = 0;
6552#if 0
8dc1a139 6553 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
6554 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6555#endif
6556 }
6557 else if (strcmp (name, ".got") == 0
6558 || strcmp (name, ".srdata") == 0
6559 || strcmp (name, ".sdata") == 0
6560 || strcmp (name, ".sbss") == 0
6561 || strcmp (name, ".lit4") == 0
6562 || strcmp (name, ".lit8") == 0)
6563 hdr->sh_flags |= SHF_MIPS_GPREL;
6564 else if (strcmp (name, ".MIPS.interfaces") == 0)
6565 {
6566 hdr->sh_type = SHT_MIPS_IFACE;
6567 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6568 }
0112cd26 6569 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
6570 {
6571 hdr->sh_type = SHT_MIPS_CONTENT;
6572 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6573 /* The sh_info field is set in final_write_processing. */
6574 }
cc2e31b9 6575 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
6576 {
6577 hdr->sh_type = SHT_MIPS_OPTIONS;
6578 hdr->sh_entsize = 1;
6579 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6580 }
1b315056
CS
6581 else if (CONST_STRNEQ (name, ".debug_")
6582 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
6583 {
6584 hdr->sh_type = SHT_MIPS_DWARF;
6585
6586 /* Irix facilities such as libexc expect a single .debug_frame
6587 per executable, the system ones have NOSTRIP set and the linker
6588 doesn't merge sections with different flags so ... */
6589 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6590 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6591 }
b49e97c9
TS
6592 else if (strcmp (name, ".MIPS.symlib") == 0)
6593 {
6594 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6595 /* The sh_link and sh_info fields are set in
6596 final_write_processing. */
6597 }
0112cd26
NC
6598 else if (CONST_STRNEQ (name, ".MIPS.events")
6599 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
6600 {
6601 hdr->sh_type = SHT_MIPS_EVENTS;
6602 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6603 /* The sh_link field is set in final_write_processing. */
6604 }
6605 else if (strcmp (name, ".msym") == 0)
6606 {
6607 hdr->sh_type = SHT_MIPS_MSYM;
6608 hdr->sh_flags |= SHF_ALLOC;
6609 hdr->sh_entsize = 8;
6610 }
6611
7a79a000
TS
6612 /* The generic elf_fake_sections will set up REL_HDR using the default
6613 kind of relocations. We used to set up a second header for the
6614 non-default kind of relocations here, but only NewABI would use
6615 these, and the IRIX ld doesn't like resulting empty RELA sections.
6616 Thus we create those header only on demand now. */
b49e97c9 6617
b34976b6 6618 return TRUE;
b49e97c9
TS
6619}
6620
6621/* Given a BFD section, try to locate the corresponding ELF section
6622 index. This is used by both the 32-bit and the 64-bit ABI.
6623 Actually, it's not clear to me that the 64-bit ABI supports these,
6624 but for non-PIC objects we will certainly want support for at least
6625 the .scommon section. */
6626
b34976b6 6627bfd_boolean
9719ad41
RS
6628_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6629 asection *sec, int *retval)
b49e97c9
TS
6630{
6631 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6632 {
6633 *retval = SHN_MIPS_SCOMMON;
b34976b6 6634 return TRUE;
b49e97c9
TS
6635 }
6636 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6637 {
6638 *retval = SHN_MIPS_ACOMMON;
b34976b6 6639 return TRUE;
b49e97c9 6640 }
b34976b6 6641 return FALSE;
b49e97c9
TS
6642}
6643\f
6644/* Hook called by the linker routine which adds symbols from an object
6645 file. We must handle the special MIPS section numbers here. */
6646
b34976b6 6647bfd_boolean
9719ad41 6648_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 6649 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
6650 flagword *flagsp ATTRIBUTE_UNUSED,
6651 asection **secp, bfd_vma *valp)
b49e97c9
TS
6652{
6653 if (SGI_COMPAT (abfd)
6654 && (abfd->flags & DYNAMIC) != 0
6655 && strcmp (*namep, "_rld_new_interface") == 0)
6656 {
8dc1a139 6657 /* Skip IRIX5 rld entry name. */
b49e97c9 6658 *namep = NULL;
b34976b6 6659 return TRUE;
b49e97c9
TS
6660 }
6661
eedecc07
DD
6662 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6663 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6664 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6665 a magic symbol resolved by the linker, we ignore this bogus definition
6666 of _gp_disp. New ABI objects do not suffer from this problem so this
6667 is not done for them. */
6668 if (!NEWABI_P(abfd)
6669 && (sym->st_shndx == SHN_ABS)
6670 && (strcmp (*namep, "_gp_disp") == 0))
6671 {
6672 *namep = NULL;
6673 return TRUE;
6674 }
6675
b49e97c9
TS
6676 switch (sym->st_shndx)
6677 {
6678 case SHN_COMMON:
6679 /* Common symbols less than the GP size are automatically
6680 treated as SHN_MIPS_SCOMMON symbols. */
6681 if (sym->st_size > elf_gp_size (abfd)
b59eed79 6682 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
6683 || IRIX_COMPAT (abfd) == ict_irix6)
6684 break;
6685 /* Fall through. */
6686 case SHN_MIPS_SCOMMON:
6687 *secp = bfd_make_section_old_way (abfd, ".scommon");
6688 (*secp)->flags |= SEC_IS_COMMON;
6689 *valp = sym->st_size;
6690 break;
6691
6692 case SHN_MIPS_TEXT:
6693 /* This section is used in a shared object. */
6694 if (elf_tdata (abfd)->elf_text_section == NULL)
6695 {
6696 asymbol *elf_text_symbol;
6697 asection *elf_text_section;
6698 bfd_size_type amt = sizeof (asection);
6699
6700 elf_text_section = bfd_zalloc (abfd, amt);
6701 if (elf_text_section == NULL)
b34976b6 6702 return FALSE;
b49e97c9
TS
6703
6704 amt = sizeof (asymbol);
6705 elf_text_symbol = bfd_zalloc (abfd, amt);
6706 if (elf_text_symbol == NULL)
b34976b6 6707 return FALSE;
b49e97c9
TS
6708
6709 /* Initialize the section. */
6710
6711 elf_tdata (abfd)->elf_text_section = elf_text_section;
6712 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6713
6714 elf_text_section->symbol = elf_text_symbol;
6715 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6716
6717 elf_text_section->name = ".text";
6718 elf_text_section->flags = SEC_NO_FLAGS;
6719 elf_text_section->output_section = NULL;
6720 elf_text_section->owner = abfd;
6721 elf_text_symbol->name = ".text";
6722 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6723 elf_text_symbol->section = elf_text_section;
6724 }
6725 /* This code used to do *secp = bfd_und_section_ptr if
6726 info->shared. I don't know why, and that doesn't make sense,
6727 so I took it out. */
6728 *secp = elf_tdata (abfd)->elf_text_section;
6729 break;
6730
6731 case SHN_MIPS_ACOMMON:
6732 /* Fall through. XXX Can we treat this as allocated data? */
6733 case SHN_MIPS_DATA:
6734 /* This section is used in a shared object. */
6735 if (elf_tdata (abfd)->elf_data_section == NULL)
6736 {
6737 asymbol *elf_data_symbol;
6738 asection *elf_data_section;
6739 bfd_size_type amt = sizeof (asection);
6740
6741 elf_data_section = bfd_zalloc (abfd, amt);
6742 if (elf_data_section == NULL)
b34976b6 6743 return FALSE;
b49e97c9
TS
6744
6745 amt = sizeof (asymbol);
6746 elf_data_symbol = bfd_zalloc (abfd, amt);
6747 if (elf_data_symbol == NULL)
b34976b6 6748 return FALSE;
b49e97c9
TS
6749
6750 /* Initialize the section. */
6751
6752 elf_tdata (abfd)->elf_data_section = elf_data_section;
6753 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6754
6755 elf_data_section->symbol = elf_data_symbol;
6756 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6757
6758 elf_data_section->name = ".data";
6759 elf_data_section->flags = SEC_NO_FLAGS;
6760 elf_data_section->output_section = NULL;
6761 elf_data_section->owner = abfd;
6762 elf_data_symbol->name = ".data";
6763 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6764 elf_data_symbol->section = elf_data_section;
6765 }
6766 /* This code used to do *secp = bfd_und_section_ptr if
6767 info->shared. I don't know why, and that doesn't make sense,
6768 so I took it out. */
6769 *secp = elf_tdata (abfd)->elf_data_section;
6770 break;
6771
6772 case SHN_MIPS_SUNDEFINED:
6773 *secp = bfd_und_section_ptr;
6774 break;
6775 }
6776
6777 if (SGI_COMPAT (abfd)
6778 && ! info->shared
f13a99db 6779 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
6780 && strcmp (*namep, "__rld_obj_head") == 0)
6781 {
6782 struct elf_link_hash_entry *h;
14a793b2 6783 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6784
6785 /* Mark __rld_obj_head as dynamic. */
14a793b2 6786 bh = NULL;
b49e97c9 6787 if (! (_bfd_generic_link_add_one_symbol
9719ad41 6788 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 6789 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6790 return FALSE;
14a793b2
AM
6791
6792 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6793 h->non_elf = 0;
6794 h->def_regular = 1;
b49e97c9
TS
6795 h->type = STT_OBJECT;
6796
c152c796 6797 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6798 return FALSE;
b49e97c9 6799
b34976b6 6800 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b49e97c9
TS
6801 }
6802
6803 /* If this is a mips16 text symbol, add 1 to the value to make it
6804 odd. This will cause something like .word SYM to come up with
6805 the right value when it is loaded into the PC. */
30c09090 6806 if (ELF_ST_IS_MIPS16 (sym->st_other))
b49e97c9
TS
6807 ++*valp;
6808
b34976b6 6809 return TRUE;
b49e97c9
TS
6810}
6811
6812/* This hook function is called before the linker writes out a global
6813 symbol. We mark symbols as small common if appropriate. This is
6814 also where we undo the increment of the value for a mips16 symbol. */
6815
6e0b88f1 6816int
9719ad41
RS
6817_bfd_mips_elf_link_output_symbol_hook
6818 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6819 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6820 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
6821{
6822 /* If we see a common symbol, which implies a relocatable link, then
6823 if a symbol was small common in an input file, mark it as small
6824 common in the output file. */
6825 if (sym->st_shndx == SHN_COMMON
6826 && strcmp (input_sec->name, ".scommon") == 0)
6827 sym->st_shndx = SHN_MIPS_SCOMMON;
6828
30c09090 6829 if (ELF_ST_IS_MIPS16 (sym->st_other))
79cda7cf 6830 sym->st_value &= ~1;
b49e97c9 6831
6e0b88f1 6832 return 1;
b49e97c9
TS
6833}
6834\f
6835/* Functions for the dynamic linker. */
6836
6837/* Create dynamic sections when linking against a dynamic object. */
6838
b34976b6 6839bfd_boolean
9719ad41 6840_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
6841{
6842 struct elf_link_hash_entry *h;
14a793b2 6843 struct bfd_link_hash_entry *bh;
b49e97c9
TS
6844 flagword flags;
6845 register asection *s;
6846 const char * const *namep;
0a44bf69 6847 struct mips_elf_link_hash_table *htab;
b49e97c9 6848
0a44bf69 6849 htab = mips_elf_hash_table (info);
b49e97c9
TS
6850 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6851 | SEC_LINKER_CREATED | SEC_READONLY);
6852
0a44bf69
RS
6853 /* The psABI requires a read-only .dynamic section, but the VxWorks
6854 EABI doesn't. */
6855 if (!htab->is_vxworks)
b49e97c9 6856 {
0a44bf69
RS
6857 s = bfd_get_section_by_name (abfd, ".dynamic");
6858 if (s != NULL)
6859 {
6860 if (! bfd_set_section_flags (abfd, s, flags))
6861 return FALSE;
6862 }
b49e97c9
TS
6863 }
6864
6865 /* We need to create .got section. */
23cc69b6 6866 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
6867 return FALSE;
6868
0a44bf69 6869 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 6870 return FALSE;
b49e97c9 6871
b49e97c9 6872 /* Create .stub section. */
4e41d0d7
RS
6873 s = bfd_make_section_with_flags (abfd,
6874 MIPS_ELF_STUB_SECTION_NAME (abfd),
6875 flags | SEC_CODE);
6876 if (s == NULL
6877 || ! bfd_set_section_alignment (abfd, s,
6878 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6879 return FALSE;
6880 htab->sstubs = s;
b49e97c9
TS
6881
6882 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6883 && !info->shared
6884 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6885 {
3496cb2a
L
6886 s = bfd_make_section_with_flags (abfd, ".rld_map",
6887 flags &~ (flagword) SEC_READONLY);
b49e97c9 6888 if (s == NULL
b49e97c9
TS
6889 || ! bfd_set_section_alignment (abfd, s,
6890 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 6891 return FALSE;
b49e97c9
TS
6892 }
6893
6894 /* On IRIX5, we adjust add some additional symbols and change the
6895 alignments of several sections. There is no ABI documentation
6896 indicating that this is necessary on IRIX6, nor any evidence that
6897 the linker takes such action. */
6898 if (IRIX_COMPAT (abfd) == ict_irix5)
6899 {
6900 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6901 {
14a793b2 6902 bh = NULL;
b49e97c9 6903 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
6904 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6905 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6906 return FALSE;
14a793b2
AM
6907
6908 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6909 h->non_elf = 0;
6910 h->def_regular = 1;
b49e97c9
TS
6911 h->type = STT_SECTION;
6912
c152c796 6913 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6914 return FALSE;
b49e97c9
TS
6915 }
6916
6917 /* We need to create a .compact_rel section. */
6918 if (SGI_COMPAT (abfd))
6919 {
6920 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 6921 return FALSE;
b49e97c9
TS
6922 }
6923
44c410de 6924 /* Change alignments of some sections. */
b49e97c9
TS
6925 s = bfd_get_section_by_name (abfd, ".hash");
6926 if (s != NULL)
d80dcc6a 6927 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6928 s = bfd_get_section_by_name (abfd, ".dynsym");
6929 if (s != NULL)
d80dcc6a 6930 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6931 s = bfd_get_section_by_name (abfd, ".dynstr");
6932 if (s != NULL)
d80dcc6a 6933 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6934 s = bfd_get_section_by_name (abfd, ".reginfo");
6935 if (s != NULL)
d80dcc6a 6936 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6937 s = bfd_get_section_by_name (abfd, ".dynamic");
6938 if (s != NULL)
d80dcc6a 6939 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
6940 }
6941
6942 if (!info->shared)
6943 {
14a793b2
AM
6944 const char *name;
6945
6946 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6947 bh = NULL;
6948 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
6949 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6950 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 6951 return FALSE;
14a793b2
AM
6952
6953 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
6954 h->non_elf = 0;
6955 h->def_regular = 1;
b49e97c9
TS
6956 h->type = STT_SECTION;
6957
c152c796 6958 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 6959 return FALSE;
b49e97c9
TS
6960
6961 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6962 {
6963 /* __rld_map is a four byte word located in the .data section
6964 and is filled in by the rtld to contain a pointer to
6965 the _r_debug structure. Its symbol value will be set in
6966 _bfd_mips_elf_finish_dynamic_symbol. */
6967 s = bfd_get_section_by_name (abfd, ".rld_map");
0abfb97a 6968 BFD_ASSERT (s != NULL);
14a793b2 6969
0abfb97a
L
6970 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6971 bh = NULL;
6972 if (!(_bfd_generic_link_add_one_symbol
6973 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6974 get_elf_backend_data (abfd)->collect, &bh)))
6975 return FALSE;
b49e97c9 6976
0abfb97a
L
6977 h = (struct elf_link_hash_entry *) bh;
6978 h->non_elf = 0;
6979 h->def_regular = 1;
6980 h->type = STT_OBJECT;
6981
6982 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6983 return FALSE;
b49e97c9
TS
6984 }
6985 }
6986
861fb55a
DJ
6987 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
6988 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
6989 if (!_bfd_elf_create_dynamic_sections (abfd, info))
6990 return FALSE;
6991
6992 /* Cache the sections created above. */
6993 htab->splt = bfd_get_section_by_name (abfd, ".plt");
6994 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
0a44bf69
RS
6995 if (htab->is_vxworks)
6996 {
0a44bf69
RS
6997 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
6998 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
861fb55a
DJ
6999 }
7000 else
7001 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7002 if (!htab->sdynbss
7003 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7004 || !htab->srelplt
7005 || !htab->splt)
7006 abort ();
0a44bf69 7007
861fb55a
DJ
7008 if (htab->is_vxworks)
7009 {
0a44bf69
RS
7010 /* Do the usual VxWorks handling. */
7011 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7012 return FALSE;
7013
7014 /* Work out the PLT sizes. */
7015 if (info->shared)
7016 {
7017 htab->plt_header_size
7018 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7019 htab->plt_entry_size
7020 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7021 }
7022 else
7023 {
7024 htab->plt_header_size
7025 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7026 htab->plt_entry_size
7027 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7028 }
7029 }
861fb55a
DJ
7030 else if (!info->shared)
7031 {
7032 /* All variants of the plt0 entry are the same size. */
7033 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7034 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7035 }
0a44bf69 7036
b34976b6 7037 return TRUE;
b49e97c9
TS
7038}
7039\f
c224138d
RS
7040/* Return true if relocation REL against section SEC is a REL rather than
7041 RELA relocation. RELOCS is the first relocation in the section and
7042 ABFD is the bfd that contains SEC. */
7043
7044static bfd_boolean
7045mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7046 const Elf_Internal_Rela *relocs,
7047 const Elf_Internal_Rela *rel)
7048{
7049 Elf_Internal_Shdr *rel_hdr;
7050 const struct elf_backend_data *bed;
7051
7052 /* To determine which flavor or relocation this is, we depend on the
7053 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
7054 rel_hdr = &elf_section_data (sec)->rel_hdr;
7055 bed = get_elf_backend_data (abfd);
7056 if ((size_t) (rel - relocs)
7057 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7058 rel_hdr = elf_section_data (sec)->rel_hdr2;
7059 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
7060}
7061
7062/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7063 HOWTO is the relocation's howto and CONTENTS points to the contents
7064 of the section that REL is against. */
7065
7066static bfd_vma
7067mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7068 reloc_howto_type *howto, bfd_byte *contents)
7069{
7070 bfd_byte *location;
7071 unsigned int r_type;
7072 bfd_vma addend;
7073
7074 r_type = ELF_R_TYPE (abfd, rel->r_info);
7075 location = contents + rel->r_offset;
7076
7077 /* Get the addend, which is stored in the input file. */
7078 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7079 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7080 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7081
7082 return addend & howto->src_mask;
7083}
7084
7085/* REL is a relocation in ABFD that needs a partnering LO16 relocation
7086 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7087 and update *ADDEND with the final addend. Return true on success
7088 or false if the LO16 could not be found. RELEND is the exclusive
7089 upper bound on the relocations for REL's section. */
7090
7091static bfd_boolean
7092mips_elf_add_lo16_rel_addend (bfd *abfd,
7093 const Elf_Internal_Rela *rel,
7094 const Elf_Internal_Rela *relend,
7095 bfd_byte *contents, bfd_vma *addend)
7096{
7097 unsigned int r_type, lo16_type;
7098 const Elf_Internal_Rela *lo16_relocation;
7099 reloc_howto_type *lo16_howto;
7100 bfd_vma l;
7101
7102 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 7103 if (mips16_reloc_p (r_type))
c224138d
RS
7104 lo16_type = R_MIPS16_LO16;
7105 else
7106 lo16_type = R_MIPS_LO16;
7107
7108 /* The combined value is the sum of the HI16 addend, left-shifted by
7109 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7110 code does a `lui' of the HI16 value, and then an `addiu' of the
7111 LO16 value.)
7112
7113 Scan ahead to find a matching LO16 relocation.
7114
7115 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7116 be immediately following. However, for the IRIX6 ABI, the next
7117 relocation may be a composed relocation consisting of several
7118 relocations for the same address. In that case, the R_MIPS_LO16
7119 relocation may occur as one of these. We permit a similar
7120 extension in general, as that is useful for GCC.
7121
7122 In some cases GCC dead code elimination removes the LO16 but keeps
7123 the corresponding HI16. This is strictly speaking a violation of
7124 the ABI but not immediately harmful. */
7125 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7126 if (lo16_relocation == NULL)
7127 return FALSE;
7128
7129 /* Obtain the addend kept there. */
7130 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7131 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7132
7133 l <<= lo16_howto->rightshift;
7134 l = _bfd_mips_elf_sign_extend (l, 16);
7135
7136 *addend <<= 16;
7137 *addend += l;
7138 return TRUE;
7139}
7140
7141/* Try to read the contents of section SEC in bfd ABFD. Return true and
7142 store the contents in *CONTENTS on success. Assume that *CONTENTS
7143 already holds the contents if it is nonull on entry. */
7144
7145static bfd_boolean
7146mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7147{
7148 if (*contents)
7149 return TRUE;
7150
7151 /* Get cached copy if it exists. */
7152 if (elf_section_data (sec)->this_hdr.contents != NULL)
7153 {
7154 *contents = elf_section_data (sec)->this_hdr.contents;
7155 return TRUE;
7156 }
7157
7158 return bfd_malloc_and_get_section (abfd, sec, contents);
7159}
7160
b49e97c9
TS
7161/* Look through the relocs for a section during the first phase, and
7162 allocate space in the global offset table. */
7163
b34976b6 7164bfd_boolean
9719ad41
RS
7165_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7166 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
7167{
7168 const char *name;
7169 bfd *dynobj;
7170 Elf_Internal_Shdr *symtab_hdr;
7171 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
7172 size_t extsymoff;
7173 const Elf_Internal_Rela *rel;
7174 const Elf_Internal_Rela *rel_end;
b49e97c9 7175 asection *sreloc;
9c5bfbb7 7176 const struct elf_backend_data *bed;
0a44bf69 7177 struct mips_elf_link_hash_table *htab;
c224138d
RS
7178 bfd_byte *contents;
7179 bfd_vma addend;
7180 reloc_howto_type *howto;
b49e97c9 7181
1049f94e 7182 if (info->relocatable)
b34976b6 7183 return TRUE;
b49e97c9 7184
0a44bf69 7185 htab = mips_elf_hash_table (info);
b49e97c9
TS
7186 dynobj = elf_hash_table (info)->dynobj;
7187 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7188 sym_hashes = elf_sym_hashes (abfd);
7189 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7190
738e5348
RS
7191 bed = get_elf_backend_data (abfd);
7192 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7193
b49e97c9
TS
7194 /* Check for the mips16 stub sections. */
7195
7196 name = bfd_get_section_name (abfd, sec);
b9d58d71 7197 if (FN_STUB_P (name))
b49e97c9
TS
7198 {
7199 unsigned long r_symndx;
7200
7201 /* Look at the relocation information to figure out which symbol
7202 this is for. */
7203
738e5348
RS
7204 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7205 if (r_symndx == 0)
7206 {
7207 (*_bfd_error_handler)
7208 (_("%B: Warning: cannot determine the target function for"
7209 " stub section `%s'"),
7210 abfd, name);
7211 bfd_set_error (bfd_error_bad_value);
7212 return FALSE;
7213 }
b49e97c9
TS
7214
7215 if (r_symndx < extsymoff
7216 || sym_hashes[r_symndx - extsymoff] == NULL)
7217 {
7218 asection *o;
7219
7220 /* This stub is for a local symbol. This stub will only be
7221 needed if there is some relocation in this BFD, other
7222 than a 16 bit function call, which refers to this symbol. */
7223 for (o = abfd->sections; o != NULL; o = o->next)
7224 {
7225 Elf_Internal_Rela *sec_relocs;
7226 const Elf_Internal_Rela *r, *rend;
7227
7228 /* We can ignore stub sections when looking for relocs. */
7229 if ((o->flags & SEC_RELOC) == 0
7230 || o->reloc_count == 0
738e5348 7231 || section_allows_mips16_refs_p (o))
b49e97c9
TS
7232 continue;
7233
45d6a902 7234 sec_relocs
9719ad41 7235 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 7236 info->keep_memory);
b49e97c9 7237 if (sec_relocs == NULL)
b34976b6 7238 return FALSE;
b49e97c9
TS
7239
7240 rend = sec_relocs + o->reloc_count;
7241 for (r = sec_relocs; r < rend; r++)
7242 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 7243 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
7244 break;
7245
6cdc0ccc 7246 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
7247 free (sec_relocs);
7248
7249 if (r < rend)
7250 break;
7251 }
7252
7253 if (o == NULL)
7254 {
7255 /* There is no non-call reloc for this stub, so we do
7256 not need it. Since this function is called before
7257 the linker maps input sections to output sections, we
7258 can easily discard it by setting the SEC_EXCLUDE
7259 flag. */
7260 sec->flags |= SEC_EXCLUDE;
b34976b6 7261 return TRUE;
b49e97c9
TS
7262 }
7263
7264 /* Record this stub in an array of local symbol stubs for
7265 this BFD. */
7266 if (elf_tdata (abfd)->local_stubs == NULL)
7267 {
7268 unsigned long symcount;
7269 asection **n;
7270 bfd_size_type amt;
7271
7272 if (elf_bad_symtab (abfd))
7273 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7274 else
7275 symcount = symtab_hdr->sh_info;
7276 amt = symcount * sizeof (asection *);
9719ad41 7277 n = bfd_zalloc (abfd, amt);
b49e97c9 7278 if (n == NULL)
b34976b6 7279 return FALSE;
b49e97c9
TS
7280 elf_tdata (abfd)->local_stubs = n;
7281 }
7282
b9d58d71 7283 sec->flags |= SEC_KEEP;
b49e97c9
TS
7284 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7285
7286 /* We don't need to set mips16_stubs_seen in this case.
7287 That flag is used to see whether we need to look through
7288 the global symbol table for stubs. We don't need to set
7289 it here, because we just have a local stub. */
7290 }
7291 else
7292 {
7293 struct mips_elf_link_hash_entry *h;
7294
7295 h = ((struct mips_elf_link_hash_entry *)
7296 sym_hashes[r_symndx - extsymoff]);
7297
973a3492
L
7298 while (h->root.root.type == bfd_link_hash_indirect
7299 || h->root.root.type == bfd_link_hash_warning)
7300 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7301
b49e97c9
TS
7302 /* H is the symbol this stub is for. */
7303
b9d58d71
TS
7304 /* If we already have an appropriate stub for this function, we
7305 don't need another one, so we can discard this one. Since
7306 this function is called before the linker maps input sections
7307 to output sections, we can easily discard it by setting the
7308 SEC_EXCLUDE flag. */
7309 if (h->fn_stub != NULL)
7310 {
7311 sec->flags |= SEC_EXCLUDE;
7312 return TRUE;
7313 }
7314
7315 sec->flags |= SEC_KEEP;
b49e97c9 7316 h->fn_stub = sec;
b34976b6 7317 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
7318 }
7319 }
b9d58d71 7320 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
7321 {
7322 unsigned long r_symndx;
7323 struct mips_elf_link_hash_entry *h;
7324 asection **loc;
7325
7326 /* Look at the relocation information to figure out which symbol
7327 this is for. */
7328
738e5348
RS
7329 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7330 if (r_symndx == 0)
7331 {
7332 (*_bfd_error_handler)
7333 (_("%B: Warning: cannot determine the target function for"
7334 " stub section `%s'"),
7335 abfd, name);
7336 bfd_set_error (bfd_error_bad_value);
7337 return FALSE;
7338 }
b49e97c9
TS
7339
7340 if (r_symndx < extsymoff
7341 || sym_hashes[r_symndx - extsymoff] == NULL)
7342 {
b9d58d71 7343 asection *o;
b49e97c9 7344
b9d58d71
TS
7345 /* This stub is for a local symbol. This stub will only be
7346 needed if there is some relocation (R_MIPS16_26) in this BFD
7347 that refers to this symbol. */
7348 for (o = abfd->sections; o != NULL; o = o->next)
7349 {
7350 Elf_Internal_Rela *sec_relocs;
7351 const Elf_Internal_Rela *r, *rend;
7352
7353 /* We can ignore stub sections when looking for relocs. */
7354 if ((o->flags & SEC_RELOC) == 0
7355 || o->reloc_count == 0
738e5348 7356 || section_allows_mips16_refs_p (o))
b9d58d71
TS
7357 continue;
7358
7359 sec_relocs
7360 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7361 info->keep_memory);
7362 if (sec_relocs == NULL)
7363 return FALSE;
7364
7365 rend = sec_relocs + o->reloc_count;
7366 for (r = sec_relocs; r < rend; r++)
7367 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7368 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7369 break;
7370
7371 if (elf_section_data (o)->relocs != sec_relocs)
7372 free (sec_relocs);
7373
7374 if (r < rend)
7375 break;
7376 }
7377
7378 if (o == NULL)
7379 {
7380 /* There is no non-call reloc for this stub, so we do
7381 not need it. Since this function is called before
7382 the linker maps input sections to output sections, we
7383 can easily discard it by setting the SEC_EXCLUDE
7384 flag. */
7385 sec->flags |= SEC_EXCLUDE;
7386 return TRUE;
7387 }
7388
7389 /* Record this stub in an array of local symbol call_stubs for
7390 this BFD. */
7391 if (elf_tdata (abfd)->local_call_stubs == NULL)
7392 {
7393 unsigned long symcount;
7394 asection **n;
7395 bfd_size_type amt;
7396
7397 if (elf_bad_symtab (abfd))
7398 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7399 else
7400 symcount = symtab_hdr->sh_info;
7401 amt = symcount * sizeof (asection *);
7402 n = bfd_zalloc (abfd, amt);
7403 if (n == NULL)
7404 return FALSE;
7405 elf_tdata (abfd)->local_call_stubs = n;
7406 }
b49e97c9 7407
b9d58d71
TS
7408 sec->flags |= SEC_KEEP;
7409 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 7410
b9d58d71
TS
7411 /* We don't need to set mips16_stubs_seen in this case.
7412 That flag is used to see whether we need to look through
7413 the global symbol table for stubs. We don't need to set
7414 it here, because we just have a local stub. */
7415 }
b49e97c9 7416 else
b49e97c9 7417 {
b9d58d71
TS
7418 h = ((struct mips_elf_link_hash_entry *)
7419 sym_hashes[r_symndx - extsymoff]);
7420
7421 /* H is the symbol this stub is for. */
7422
7423 if (CALL_FP_STUB_P (name))
7424 loc = &h->call_fp_stub;
7425 else
7426 loc = &h->call_stub;
7427
7428 /* If we already have an appropriate stub for this function, we
7429 don't need another one, so we can discard this one. Since
7430 this function is called before the linker maps input sections
7431 to output sections, we can easily discard it by setting the
7432 SEC_EXCLUDE flag. */
7433 if (*loc != NULL)
7434 {
7435 sec->flags |= SEC_EXCLUDE;
7436 return TRUE;
7437 }
b49e97c9 7438
b9d58d71
TS
7439 sec->flags |= SEC_KEEP;
7440 *loc = sec;
7441 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7442 }
b49e97c9
TS
7443 }
7444
b49e97c9 7445 sreloc = NULL;
c224138d 7446 contents = NULL;
b49e97c9
TS
7447 for (rel = relocs; rel < rel_end; ++rel)
7448 {
7449 unsigned long r_symndx;
7450 unsigned int r_type;
7451 struct elf_link_hash_entry *h;
861fb55a 7452 bfd_boolean can_make_dynamic_p;
b49e97c9
TS
7453
7454 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7455 r_type = ELF_R_TYPE (abfd, rel->r_info);
7456
7457 if (r_symndx < extsymoff)
7458 h = NULL;
7459 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7460 {
7461 (*_bfd_error_handler)
d003868e
AM
7462 (_("%B: Malformed reloc detected for section %s"),
7463 abfd, name);
b49e97c9 7464 bfd_set_error (bfd_error_bad_value);
b34976b6 7465 return FALSE;
b49e97c9
TS
7466 }
7467 else
7468 {
7469 h = sym_hashes[r_symndx - extsymoff];
3e08fb72
NC
7470 while (h != NULL
7471 && (h->root.type == bfd_link_hash_indirect
7472 || h->root.type == bfd_link_hash_warning))
861fb55a
DJ
7473 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7474 }
b49e97c9 7475
861fb55a
DJ
7476 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7477 relocation into a dynamic one. */
7478 can_make_dynamic_p = FALSE;
7479 switch (r_type)
7480 {
7481 case R_MIPS16_GOT16:
7482 case R_MIPS16_CALL16:
7483 case R_MIPS_GOT16:
7484 case R_MIPS_CALL16:
7485 case R_MIPS_CALL_HI16:
7486 case R_MIPS_CALL_LO16:
7487 case R_MIPS_GOT_HI16:
7488 case R_MIPS_GOT_LO16:
7489 case R_MIPS_GOT_PAGE:
7490 case R_MIPS_GOT_OFST:
7491 case R_MIPS_GOT_DISP:
7492 case R_MIPS_TLS_GOTTPREL:
7493 case R_MIPS_TLS_GD:
7494 case R_MIPS_TLS_LDM:
7495 if (dynobj == NULL)
7496 elf_hash_table (info)->dynobj = dynobj = abfd;
7497 if (!mips_elf_create_got_section (dynobj, info))
7498 return FALSE;
7499 if (htab->is_vxworks && !info->shared)
b49e97c9 7500 {
861fb55a
DJ
7501 (*_bfd_error_handler)
7502 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7503 abfd, (unsigned long) rel->r_offset);
7504 bfd_set_error (bfd_error_bad_value);
7505 return FALSE;
b49e97c9 7506 }
861fb55a 7507 break;
b49e97c9 7508
99da6b5f
AN
7509 /* This is just a hint; it can safely be ignored. Don't set
7510 has_static_relocs for the corresponding symbol. */
7511 case R_MIPS_JALR:
7512 break;
7513
861fb55a
DJ
7514 case R_MIPS_32:
7515 case R_MIPS_REL32:
7516 case R_MIPS_64:
7517 /* In VxWorks executables, references to external symbols
7518 must be handled using copy relocs or PLT entries; it is not
7519 possible to convert this relocation into a dynamic one.
7520
7521 For executables that use PLTs and copy-relocs, we have a
7522 choice between converting the relocation into a dynamic
7523 one or using copy relocations or PLT entries. It is
7524 usually better to do the former, unless the relocation is
7525 against a read-only section. */
7526 if ((info->shared
7527 || (h != NULL
7528 && !htab->is_vxworks
7529 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7530 && !(!info->nocopyreloc
7531 && !PIC_OBJECT_P (abfd)
7532 && MIPS_ELF_READONLY_SECTION (sec))))
7533 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 7534 {
861fb55a 7535 can_make_dynamic_p = TRUE;
b49e97c9
TS
7536 if (dynobj == NULL)
7537 elf_hash_table (info)->dynobj = dynobj = abfd;
b49e97c9 7538 break;
861fb55a
DJ
7539 }
7540 /* Fall through. */
b49e97c9 7541
861fb55a
DJ
7542 default:
7543 /* Most static relocations require pointer equality, except
7544 for branches. */
7545 if (h)
7546 h->pointer_equality_needed = TRUE;
7547 /* Fall through. */
b49e97c9 7548
861fb55a
DJ
7549 case R_MIPS_26:
7550 case R_MIPS_PC16:
7551 case R_MIPS16_26:
7552 if (h)
7553 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7554 break;
b49e97c9
TS
7555 }
7556
0a44bf69
RS
7557 if (h)
7558 {
0a44bf69
RS
7559 /* Relocations against the special VxWorks __GOTT_BASE__ and
7560 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7561 room for them in .rela.dyn. */
7562 if (is_gott_symbol (info, h))
7563 {
7564 if (sreloc == NULL)
7565 {
7566 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7567 if (sreloc == NULL)
7568 return FALSE;
7569 }
7570 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
7571 if (MIPS_ELF_READONLY_SECTION (sec))
7572 /* We tell the dynamic linker that there are
7573 relocations against the text segment. */
7574 info->flags |= DF_TEXTREL;
0a44bf69
RS
7575 }
7576 }
7577 else if (r_type == R_MIPS_CALL_LO16
7578 || r_type == R_MIPS_GOT_LO16
7579 || r_type == R_MIPS_GOT_DISP
738e5348 7580 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
7581 {
7582 /* We may need a local GOT entry for this relocation. We
7583 don't count R_MIPS_GOT_PAGE because we can estimate the
7584 maximum number of pages needed by looking at the size of
738e5348
RS
7585 the segment. Similar comments apply to R_MIPS*_GOT16 and
7586 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 7587 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 7588 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 7589 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0
RS
7590 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7591 rel->r_addend, info, 0))
f4416af6 7592 return FALSE;
b49e97c9
TS
7593 }
7594
861fb55a
DJ
7595 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7596 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7597
b49e97c9
TS
7598 switch (r_type)
7599 {
7600 case R_MIPS_CALL16:
738e5348 7601 case R_MIPS16_CALL16:
b49e97c9
TS
7602 if (h == NULL)
7603 {
7604 (*_bfd_error_handler)
d003868e
AM
7605 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7606 abfd, (unsigned long) rel->r_offset);
b49e97c9 7607 bfd_set_error (bfd_error_bad_value);
b34976b6 7608 return FALSE;
b49e97c9
TS
7609 }
7610 /* Fall through. */
7611
7612 case R_MIPS_CALL_HI16:
7613 case R_MIPS_CALL_LO16:
7614 if (h != NULL)
7615 {
d334575b 7616 /* VxWorks call relocations point at the function's .got.plt
0a44bf69
RS
7617 entry, which will be allocated by adjust_dynamic_symbol.
7618 Otherwise, this symbol requires a global GOT entry. */
8275b357 7619 if ((!htab->is_vxworks || h->forced_local)
a8028dd0 7620 && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
b34976b6 7621 return FALSE;
b49e97c9
TS
7622
7623 /* We need a stub, not a plt entry for the undefined
7624 function. But we record it as if it needs plt. See
c152c796 7625 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 7626 h->needs_plt = 1;
b49e97c9
TS
7627 h->type = STT_FUNC;
7628 }
7629 break;
7630
0fdc1bf1
AO
7631 case R_MIPS_GOT_PAGE:
7632 /* If this is a global, overridable symbol, GOT_PAGE will
7633 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d 7634 if (h)
0fdc1bf1
AO
7635 {
7636 struct mips_elf_link_hash_entry *hmips =
7637 (struct mips_elf_link_hash_entry *) h;
143d77c5 7638
3a3b6725 7639 /* This symbol is definitely not overridable. */
f5385ebf 7640 if (hmips->root.def_regular
0fdc1bf1 7641 && ! (info->shared && ! info->symbolic
f5385ebf 7642 && ! hmips->root.forced_local))
c224138d 7643 h = NULL;
0fdc1bf1
AO
7644 }
7645 /* Fall through. */
7646
738e5348 7647 case R_MIPS16_GOT16:
b49e97c9
TS
7648 case R_MIPS_GOT16:
7649 case R_MIPS_GOT_HI16:
7650 case R_MIPS_GOT_LO16:
3a3b6725 7651 if (!h || r_type == R_MIPS_GOT_PAGE)
c224138d 7652 {
3a3b6725
DJ
7653 /* This relocation needs (or may need, if h != NULL) a
7654 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7655 know for sure until we know whether the symbol is
7656 preemptible. */
c224138d
RS
7657 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7658 {
7659 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7660 return FALSE;
7661 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7662 addend = mips_elf_read_rel_addend (abfd, rel,
7663 howto, contents);
7664 if (r_type == R_MIPS_GOT16)
7665 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7666 contents, &addend);
7667 else
7668 addend <<= howto->rightshift;
7669 }
7670 else
7671 addend = rel->r_addend;
a8028dd0
RS
7672 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7673 addend))
c224138d
RS
7674 return FALSE;
7675 break;
7676 }
7677 /* Fall through. */
7678
b49e97c9 7679 case R_MIPS_GOT_DISP:
a8028dd0 7680 if (h && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
b34976b6 7681 return FALSE;
b49e97c9
TS
7682 break;
7683
0f20cc35
DJ
7684 case R_MIPS_TLS_GOTTPREL:
7685 if (info->shared)
7686 info->flags |= DF_STATIC_TLS;
7687 /* Fall through */
7688
7689 case R_MIPS_TLS_LDM:
7690 if (r_type == R_MIPS_TLS_LDM)
7691 {
7692 r_symndx = 0;
7693 h = NULL;
7694 }
7695 /* Fall through */
7696
7697 case R_MIPS_TLS_GD:
7698 /* This symbol requires a global offset table entry, or two
7699 for TLS GD relocations. */
7700 {
7701 unsigned char flag = (r_type == R_MIPS_TLS_GD
7702 ? GOT_TLS_GD
7703 : r_type == R_MIPS_TLS_LDM
7704 ? GOT_TLS_LDM
7705 : GOT_TLS_IE);
7706 if (h != NULL)
7707 {
7708 struct mips_elf_link_hash_entry *hmips =
7709 (struct mips_elf_link_hash_entry *) h;
7710 hmips->tls_type |= flag;
7711
a8028dd0
RS
7712 if (h && !mips_elf_record_global_got_symbol (h, abfd,
7713 info, flag))
0f20cc35
DJ
7714 return FALSE;
7715 }
7716 else
7717 {
7718 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7719
a8028dd0
RS
7720 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7721 rel->r_addend,
7722 info, flag))
0f20cc35
DJ
7723 return FALSE;
7724 }
7725 }
7726 break;
7727
b49e97c9
TS
7728 case R_MIPS_32:
7729 case R_MIPS_REL32:
7730 case R_MIPS_64:
0a44bf69
RS
7731 /* In VxWorks executables, references to external symbols
7732 are handled using copy relocs or PLT stubs, so there's
7733 no need to add a .rela.dyn entry for this relocation. */
861fb55a 7734 if (can_make_dynamic_p)
b49e97c9
TS
7735 {
7736 if (sreloc == NULL)
7737 {
0a44bf69 7738 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 7739 if (sreloc == NULL)
f4416af6 7740 return FALSE;
b49e97c9 7741 }
9a59ad6b 7742 if (info->shared && h == NULL)
82f0cfbd
EC
7743 {
7744 /* When creating a shared object, we must copy these
7745 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
7746 relocs. Make room for this reloc in .rel(a).dyn. */
7747 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 7748 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7749 /* We tell the dynamic linker that there are
7750 relocations against the text segment. */
7751 info->flags |= DF_TEXTREL;
7752 }
b49e97c9
TS
7753 else
7754 {
7755 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 7756
9a59ad6b
DJ
7757 /* For a shared object, we must copy this relocation
7758 unless the symbol turns out to be undefined and
7759 weak with non-default visibility, in which case
7760 it will be left as zero.
7761
7762 We could elide R_MIPS_REL32 for locally binding symbols
7763 in shared libraries, but do not yet do so.
7764
7765 For an executable, we only need to copy this
7766 reloc if the symbol is defined in a dynamic
7767 object. */
b49e97c9
TS
7768 hmips = (struct mips_elf_link_hash_entry *) h;
7769 ++hmips->possibly_dynamic_relocs;
943284cc 7770 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
7771 /* We need it to tell the dynamic linker if there
7772 are relocations against the text segment. */
7773 hmips->readonly_reloc = TRUE;
b49e97c9 7774 }
b49e97c9
TS
7775 }
7776
7777 if (SGI_COMPAT (abfd))
7778 mips_elf_hash_table (info)->compact_rel_size +=
7779 sizeof (Elf32_External_crinfo);
7780 break;
7781
7782 case R_MIPS_26:
7783 case R_MIPS_GPREL16:
7784 case R_MIPS_LITERAL:
7785 case R_MIPS_GPREL32:
7786 if (SGI_COMPAT (abfd))
7787 mips_elf_hash_table (info)->compact_rel_size +=
7788 sizeof (Elf32_External_crinfo);
7789 break;
7790
7791 /* This relocation describes the C++ object vtable hierarchy.
7792 Reconstruct it for later use during GC. */
7793 case R_MIPS_GNU_VTINHERIT:
c152c796 7794 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 7795 return FALSE;
b49e97c9
TS
7796 break;
7797
7798 /* This relocation describes which C++ vtable entries are actually
7799 used. Record for later use during GC. */
7800 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
7801 BFD_ASSERT (h != NULL);
7802 if (h != NULL
7803 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 7804 return FALSE;
b49e97c9
TS
7805 break;
7806
7807 default:
7808 break;
7809 }
7810
7811 /* We must not create a stub for a symbol that has relocations
0a44bf69
RS
7812 related to taking the function's address. This doesn't apply to
7813 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7814 a normal .got entry. */
7815 if (!htab->is_vxworks && h != NULL)
7816 switch (r_type)
7817 {
7818 default:
7819 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7820 break;
738e5348 7821 case R_MIPS16_CALL16:
0a44bf69
RS
7822 case R_MIPS_CALL16:
7823 case R_MIPS_CALL_HI16:
7824 case R_MIPS_CALL_LO16:
7825 case R_MIPS_JALR:
7826 break;
7827 }
b49e97c9 7828
738e5348
RS
7829 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7830 if there is one. We only need to handle global symbols here;
7831 we decide whether to keep or delete stubs for local symbols
7832 when processing the stub's relocations. */
b49e97c9 7833 if (h != NULL
738e5348
RS
7834 && !mips16_call_reloc_p (r_type)
7835 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
7836 {
7837 struct mips_elf_link_hash_entry *mh;
7838
7839 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 7840 mh->need_fn_stub = TRUE;
b49e97c9 7841 }
861fb55a
DJ
7842
7843 /* Refuse some position-dependent relocations when creating a
7844 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7845 not PIC, but we can create dynamic relocations and the result
7846 will be fine. Also do not refuse R_MIPS_LO16, which can be
7847 combined with R_MIPS_GOT16. */
7848 if (info->shared)
7849 {
7850 switch (r_type)
7851 {
7852 case R_MIPS16_HI16:
7853 case R_MIPS_HI16:
7854 case R_MIPS_HIGHER:
7855 case R_MIPS_HIGHEST:
7856 /* Don't refuse a high part relocation if it's against
7857 no symbol (e.g. part of a compound relocation). */
7858 if (r_symndx == 0)
7859 break;
7860
7861 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7862 and has a special meaning. */
7863 if (!NEWABI_P (abfd) && h != NULL
7864 && strcmp (h->root.root.string, "_gp_disp") == 0)
7865 break;
7866
7867 /* FALLTHROUGH */
7868
7869 case R_MIPS16_26:
7870 case R_MIPS_26:
7871 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7872 (*_bfd_error_handler)
7873 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7874 abfd, howto->name,
7875 (h) ? h->root.root.string : "a local symbol");
7876 bfd_set_error (bfd_error_bad_value);
7877 return FALSE;
7878 default:
7879 break;
7880 }
7881 }
b49e97c9
TS
7882 }
7883
b34976b6 7884 return TRUE;
b49e97c9
TS
7885}
7886\f
d0647110 7887bfd_boolean
9719ad41
RS
7888_bfd_mips_relax_section (bfd *abfd, asection *sec,
7889 struct bfd_link_info *link_info,
7890 bfd_boolean *again)
d0647110
AO
7891{
7892 Elf_Internal_Rela *internal_relocs;
7893 Elf_Internal_Rela *irel, *irelend;
7894 Elf_Internal_Shdr *symtab_hdr;
7895 bfd_byte *contents = NULL;
d0647110
AO
7896 size_t extsymoff;
7897 bfd_boolean changed_contents = FALSE;
7898 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7899 Elf_Internal_Sym *isymbuf = NULL;
7900
7901 /* We are not currently changing any sizes, so only one pass. */
7902 *again = FALSE;
7903
1049f94e 7904 if (link_info->relocatable)
d0647110
AO
7905 return TRUE;
7906
9719ad41 7907 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 7908 link_info->keep_memory);
d0647110
AO
7909 if (internal_relocs == NULL)
7910 return TRUE;
7911
7912 irelend = internal_relocs + sec->reloc_count
7913 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7914 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7915 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7916
7917 for (irel = internal_relocs; irel < irelend; irel++)
7918 {
7919 bfd_vma symval;
7920 bfd_signed_vma sym_offset;
7921 unsigned int r_type;
7922 unsigned long r_symndx;
7923 asection *sym_sec;
7924 unsigned long instruction;
7925
7926 /* Turn jalr into bgezal, and jr into beq, if they're marked
7927 with a JALR relocation, that indicate where they jump to.
7928 This saves some pipeline bubbles. */
7929 r_type = ELF_R_TYPE (abfd, irel->r_info);
7930 if (r_type != R_MIPS_JALR)
7931 continue;
7932
7933 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7934 /* Compute the address of the jump target. */
7935 if (r_symndx >= extsymoff)
7936 {
7937 struct mips_elf_link_hash_entry *h
7938 = ((struct mips_elf_link_hash_entry *)
7939 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7940
7941 while (h->root.root.type == bfd_link_hash_indirect
7942 || h->root.root.type == bfd_link_hash_warning)
7943 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 7944
d0647110
AO
7945 /* If a symbol is undefined, or if it may be overridden,
7946 skip it. */
7947 if (! ((h->root.root.type == bfd_link_hash_defined
7948 || h->root.root.type == bfd_link_hash_defweak)
7949 && h->root.root.u.def.section)
7950 || (link_info->shared && ! link_info->symbolic
f5385ebf 7951 && !h->root.forced_local))
d0647110
AO
7952 continue;
7953
7954 sym_sec = h->root.root.u.def.section;
7955 if (sym_sec->output_section)
7956 symval = (h->root.root.u.def.value
7957 + sym_sec->output_section->vma
7958 + sym_sec->output_offset);
7959 else
7960 symval = h->root.root.u.def.value;
7961 }
7962 else
7963 {
7964 Elf_Internal_Sym *isym;
7965
7966 /* Read this BFD's symbols if we haven't done so already. */
7967 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7968 {
7969 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7970 if (isymbuf == NULL)
7971 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7972 symtab_hdr->sh_info, 0,
7973 NULL, NULL, NULL);
7974 if (isymbuf == NULL)
7975 goto relax_return;
7976 }
7977
7978 isym = isymbuf + r_symndx;
7979 if (isym->st_shndx == SHN_UNDEF)
7980 continue;
7981 else if (isym->st_shndx == SHN_ABS)
7982 sym_sec = bfd_abs_section_ptr;
7983 else if (isym->st_shndx == SHN_COMMON)
7984 sym_sec = bfd_com_section_ptr;
7985 else
7986 sym_sec
7987 = bfd_section_from_elf_index (abfd, isym->st_shndx);
7988 symval = isym->st_value
7989 + sym_sec->output_section->vma
7990 + sym_sec->output_offset;
7991 }
7992
7993 /* Compute branch offset, from delay slot of the jump to the
7994 branch target. */
7995 sym_offset = (symval + irel->r_addend)
7996 - (sec_start + irel->r_offset + 4);
7997
7998 /* Branch offset must be properly aligned. */
7999 if ((sym_offset & 3) != 0)
8000 continue;
8001
8002 sym_offset >>= 2;
8003
8004 /* Check that it's in range. */
8005 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8006 continue;
143d77c5 8007
d0647110 8008 /* Get the section contents if we haven't done so already. */
c224138d
RS
8009 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8010 goto relax_return;
d0647110
AO
8011
8012 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8013
8014 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8015 if ((instruction & 0xfc1fffff) == 0x0000f809)
8016 instruction = 0x04110000;
8017 /* If it was jr <reg>, turn it into b <target>. */
8018 else if ((instruction & 0xfc1fffff) == 0x00000008)
8019 instruction = 0x10000000;
8020 else
8021 continue;
8022
8023 instruction |= (sym_offset & 0xffff);
8024 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8025 changed_contents = TRUE;
8026 }
8027
8028 if (contents != NULL
8029 && elf_section_data (sec)->this_hdr.contents != contents)
8030 {
8031 if (!changed_contents && !link_info->keep_memory)
8032 free (contents);
8033 else
8034 {
8035 /* Cache the section contents for elf_link_input_bfd. */
8036 elf_section_data (sec)->this_hdr.contents = contents;
8037 }
8038 }
8039 return TRUE;
8040
143d77c5 8041 relax_return:
eea6121a
AM
8042 if (contents != NULL
8043 && elf_section_data (sec)->this_hdr.contents != contents)
8044 free (contents);
d0647110
AO
8045 return FALSE;
8046}
8047\f
9a59ad6b
DJ
8048/* Allocate space for global sym dynamic relocs. */
8049
8050static bfd_boolean
8051allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8052{
8053 struct bfd_link_info *info = inf;
8054 bfd *dynobj;
8055 struct mips_elf_link_hash_entry *hmips;
8056 struct mips_elf_link_hash_table *htab;
8057
8058 htab = mips_elf_hash_table (info);
8059 dynobj = elf_hash_table (info)->dynobj;
8060 hmips = (struct mips_elf_link_hash_entry *) h;
8061
8062 /* VxWorks executables are handled elsewhere; we only need to
8063 allocate relocations in shared objects. */
8064 if (htab->is_vxworks && !info->shared)
8065 return TRUE;
8066
63897e2c
RS
8067 /* Ignore indirect and warning symbols. All relocations against
8068 such symbols will be redirected to the target symbol. */
8069 if (h->root.type == bfd_link_hash_indirect
8070 || h->root.type == bfd_link_hash_warning)
8071 return TRUE;
8072
9a59ad6b
DJ
8073 /* If this symbol is defined in a dynamic object, or we are creating
8074 a shared library, we will need to copy any R_MIPS_32 or
8075 R_MIPS_REL32 relocs against it into the output file. */
8076 if (! info->relocatable
8077 && hmips->possibly_dynamic_relocs != 0
8078 && (h->root.type == bfd_link_hash_defweak
8079 || !h->def_regular
8080 || info->shared))
8081 {
8082 bfd_boolean do_copy = TRUE;
8083
8084 if (h->root.type == bfd_link_hash_undefweak)
8085 {
8086 /* Do not copy relocations for undefined weak symbols with
8087 non-default visibility. */
8088 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8089 do_copy = FALSE;
8090
8091 /* Make sure undefined weak symbols are output as a dynamic
8092 symbol in PIEs. */
8093 else if (h->dynindx == -1 && !h->forced_local)
8094 {
8095 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8096 return FALSE;
8097 }
8098 }
8099
8100 if (do_copy)
8101 {
aff469fa
RS
8102 /* Even though we don't directly need a GOT entry for this symbol,
8103 a symbol must have a dynamic symbol table index greater that
8104 DT_MIPS_GOTSYM if there are dynamic relocations against it. */
8105 if (hmips->global_got_area > GGA_RELOC_ONLY)
8106 hmips->global_got_area = GGA_RELOC_ONLY;
8107
9a59ad6b
DJ
8108 mips_elf_allocate_dynamic_relocations
8109 (dynobj, info, hmips->possibly_dynamic_relocs);
8110 if (hmips->readonly_reloc)
8111 /* We tell the dynamic linker that there are relocations
8112 against the text segment. */
8113 info->flags |= DF_TEXTREL;
8114 }
8115 }
8116
8117 return TRUE;
8118}
8119
b49e97c9
TS
8120/* Adjust a symbol defined by a dynamic object and referenced by a
8121 regular object. The current definition is in some section of the
8122 dynamic object, but we're not including those sections. We have to
8123 change the definition to something the rest of the link can
8124 understand. */
8125
b34976b6 8126bfd_boolean
9719ad41
RS
8127_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8128 struct elf_link_hash_entry *h)
b49e97c9
TS
8129{
8130 bfd *dynobj;
8131 struct mips_elf_link_hash_entry *hmips;
5108fc1b 8132 struct mips_elf_link_hash_table *htab;
b49e97c9 8133
5108fc1b 8134 htab = mips_elf_hash_table (info);
b49e97c9 8135 dynobj = elf_hash_table (info)->dynobj;
861fb55a 8136 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
8137
8138 /* Make sure we know what is going on here. */
8139 BFD_ASSERT (dynobj != NULL
f5385ebf 8140 && (h->needs_plt
f6e332e6 8141 || h->u.weakdef != NULL
f5385ebf
AM
8142 || (h->def_dynamic
8143 && h->ref_regular
8144 && !h->def_regular)));
b49e97c9 8145
b49e97c9 8146 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 8147
861fb55a
DJ
8148 /* If there are call relocations against an externally-defined symbol,
8149 see whether we can create a MIPS lazy-binding stub for it. We can
8150 only do this if all references to the function are through call
8151 relocations, and in that case, the traditional lazy-binding stubs
8152 are much more efficient than PLT entries.
8153
8154 Traditional stubs are only available on SVR4 psABI-based systems;
8155 VxWorks always uses PLTs instead. */
8156 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
8157 {
8158 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 8159 return TRUE;
b49e97c9
TS
8160
8161 /* If this symbol is not defined in a regular file, then set
8162 the symbol to the stub location. This is required to make
8163 function pointers compare as equal between the normal
8164 executable and the shared library. */
f5385ebf 8165 if (!h->def_regular)
b49e97c9 8166 {
33bb52fb
RS
8167 hmips->needs_lazy_stub = TRUE;
8168 htab->lazy_stub_count++;
b34976b6 8169 return TRUE;
b49e97c9
TS
8170 }
8171 }
861fb55a
DJ
8172 /* As above, VxWorks requires PLT entries for externally-defined
8173 functions that are only accessed through call relocations.
b49e97c9 8174
861fb55a
DJ
8175 Both VxWorks and non-VxWorks targets also need PLT entries if there
8176 are static-only relocations against an externally-defined function.
8177 This can technically occur for shared libraries if there are
8178 branches to the symbol, although it is unlikely that this will be
8179 used in practice due to the short ranges involved. It can occur
8180 for any relative or absolute relocation in executables; in that
8181 case, the PLT entry becomes the function's canonical address. */
8182 else if (((h->needs_plt && !hmips->no_fn_stub)
8183 || (h->type == STT_FUNC && hmips->has_static_relocs))
8184 && htab->use_plts_and_copy_relocs
8185 && !SYMBOL_CALLS_LOCAL (info, h)
8186 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8187 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 8188 {
861fb55a
DJ
8189 /* If this is the first symbol to need a PLT entry, allocate room
8190 for the header. */
8191 if (htab->splt->size == 0)
8192 {
8193 BFD_ASSERT (htab->sgotplt->size == 0);
0a44bf69 8194
861fb55a
DJ
8195 /* If we're using the PLT additions to the psABI, each PLT
8196 entry is 16 bytes and the PLT0 entry is 32 bytes.
8197 Encourage better cache usage by aligning. We do this
8198 lazily to avoid pessimizing traditional objects. */
8199 if (!htab->is_vxworks
8200 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8201 return FALSE;
0a44bf69 8202
861fb55a
DJ
8203 /* Make sure that .got.plt is word-aligned. We do this lazily
8204 for the same reason as above. */
8205 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8206 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8207 return FALSE;
0a44bf69 8208
861fb55a 8209 htab->splt->size += htab->plt_header_size;
0a44bf69 8210
861fb55a
DJ
8211 /* On non-VxWorks targets, the first two entries in .got.plt
8212 are reserved. */
8213 if (!htab->is_vxworks)
8214 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
0a44bf69 8215
861fb55a
DJ
8216 /* On VxWorks, also allocate room for the header's
8217 .rela.plt.unloaded entries. */
8218 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8219 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8220 }
8221
8222 /* Assign the next .plt entry to this symbol. */
8223 h->plt.offset = htab->splt->size;
8224 htab->splt->size += htab->plt_entry_size;
8225
8226 /* If the output file has no definition of the symbol, set the
861fb55a 8227 symbol's value to the address of the stub. */
131eb6b7 8228 if (!info->shared && !h->def_regular)
0a44bf69
RS
8229 {
8230 h->root.u.def.section = htab->splt;
8231 h->root.u.def.value = h->plt.offset;
861fb55a
DJ
8232 /* For VxWorks, point at the PLT load stub rather than the
8233 lazy resolution stub; this stub will become the canonical
8234 function address. */
8235 if (htab->is_vxworks)
8236 h->root.u.def.value += 8;
0a44bf69
RS
8237 }
8238
861fb55a
DJ
8239 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8240 relocation. */
8241 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8242 htab->srelplt->size += (htab->is_vxworks
8243 ? MIPS_ELF_RELA_SIZE (dynobj)
8244 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
8245
8246 /* Make room for the .rela.plt.unloaded relocations. */
861fb55a 8247 if (htab->is_vxworks && !info->shared)
0a44bf69
RS
8248 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8249
861fb55a
DJ
8250 /* All relocations against this symbol that could have been made
8251 dynamic will now refer to the PLT entry instead. */
8252 hmips->possibly_dynamic_relocs = 0;
0a44bf69 8253
0a44bf69
RS
8254 return TRUE;
8255 }
8256
8257 /* If this is a weak symbol, and there is a real definition, the
8258 processor independent code will have arranged for us to see the
8259 real definition first, and we can just use the same value. */
8260 if (h->u.weakdef != NULL)
8261 {
8262 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8263 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8264 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8265 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8266 return TRUE;
8267 }
8268
861fb55a
DJ
8269 /* Otherwise, there is nothing further to do for symbols defined
8270 in regular objects. */
8271 if (h->def_regular)
0a44bf69
RS
8272 return TRUE;
8273
861fb55a
DJ
8274 /* There's also nothing more to do if we'll convert all relocations
8275 against this symbol into dynamic relocations. */
8276 if (!hmips->has_static_relocs)
8277 return TRUE;
8278
8279 /* We're now relying on copy relocations. Complain if we have
8280 some that we can't convert. */
8281 if (!htab->use_plts_and_copy_relocs || info->shared)
8282 {
8283 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8284 "dynamic symbol %s"),
8285 h->root.root.string);
8286 bfd_set_error (bfd_error_bad_value);
8287 return FALSE;
8288 }
8289
0a44bf69
RS
8290 /* We must allocate the symbol in our .dynbss section, which will
8291 become part of the .bss section of the executable. There will be
8292 an entry for this symbol in the .dynsym section. The dynamic
8293 object will contain position independent code, so all references
8294 from the dynamic object to this symbol will go through the global
8295 offset table. The dynamic linker will use the .dynsym entry to
8296 determine the address it must put in the global offset table, so
8297 both the dynamic object and the regular object will refer to the
8298 same memory location for the variable. */
8299
8300 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8301 {
861fb55a
DJ
8302 if (htab->is_vxworks)
8303 htab->srelbss->size += sizeof (Elf32_External_Rela);
8304 else
8305 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
8306 h->needs_copy = 1;
8307 }
8308
861fb55a
DJ
8309 /* All relocations against this symbol that could have been made
8310 dynamic will now refer to the local copy instead. */
8311 hmips->possibly_dynamic_relocs = 0;
8312
027297b7 8313 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
0a44bf69 8314}
b49e97c9
TS
8315\f
8316/* This function is called after all the input files have been read,
8317 and the input sections have been assigned to output sections. We
8318 check for any mips16 stub sections that we can discard. */
8319
b34976b6 8320bfd_boolean
9719ad41
RS
8321_bfd_mips_elf_always_size_sections (bfd *output_bfd,
8322 struct bfd_link_info *info)
b49e97c9
TS
8323{
8324 asection *ri;
0a44bf69 8325 struct mips_elf_link_hash_table *htab;
861fb55a 8326 struct mips_htab_traverse_info hti;
0a44bf69
RS
8327
8328 htab = mips_elf_hash_table (info);
f4416af6 8329
b49e97c9
TS
8330 /* The .reginfo section has a fixed size. */
8331 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8332 if (ri != NULL)
9719ad41 8333 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 8334
861fb55a
DJ
8335 hti.info = info;
8336 hti.output_bfd = output_bfd;
8337 hti.error = FALSE;
8338 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8339 mips_elf_check_symbols, &hti);
8340 if (hti.error)
8341 return FALSE;
f4416af6 8342
33bb52fb
RS
8343 return TRUE;
8344}
8345
8346/* If the link uses a GOT, lay it out and work out its size. */
8347
8348static bfd_boolean
8349mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8350{
8351 bfd *dynobj;
8352 asection *s;
8353 struct mips_got_info *g;
33bb52fb
RS
8354 bfd_size_type loadable_size = 0;
8355 bfd_size_type page_gotno;
8356 bfd *sub;
8357 struct mips_elf_count_tls_arg count_tls_arg;
8358 struct mips_elf_link_hash_table *htab;
8359
8360 htab = mips_elf_hash_table (info);
a8028dd0 8361 s = htab->sgot;
f4416af6 8362 if (s == NULL)
b34976b6 8363 return TRUE;
b49e97c9 8364
33bb52fb 8365 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
8366 g = htab->got_info;
8367
861fb55a
DJ
8368 /* Allocate room for the reserved entries. VxWorks always reserves
8369 3 entries; other objects only reserve 2 entries. */
8370 BFD_ASSERT (g->assigned_gotno == 0);
8371 if (htab->is_vxworks)
8372 htab->reserved_gotno = 3;
8373 else
8374 htab->reserved_gotno = 2;
8375 g->local_gotno += htab->reserved_gotno;
8376 g->assigned_gotno = htab->reserved_gotno;
8377
33bb52fb
RS
8378 /* Replace entries for indirect and warning symbols with entries for
8379 the target symbol. */
8380 if (!mips_elf_resolve_final_got_entries (g))
8381 return FALSE;
f4416af6 8382
d4596a51
RS
8383 /* Count the number of GOT symbols. */
8384 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, g);
f4416af6 8385
33bb52fb
RS
8386 /* Calculate the total loadable size of the output. That
8387 will give us the maximum number of GOT_PAGE entries
8388 required. */
8389 for (sub = info->input_bfds; sub; sub = sub->link_next)
8390 {
8391 asection *subsection;
5108fc1b 8392
33bb52fb
RS
8393 for (subsection = sub->sections;
8394 subsection;
8395 subsection = subsection->next)
8396 {
8397 if ((subsection->flags & SEC_ALLOC) == 0)
8398 continue;
8399 loadable_size += ((subsection->size + 0xf)
8400 &~ (bfd_size_type) 0xf);
8401 }
8402 }
f4416af6 8403
0a44bf69 8404 if (htab->is_vxworks)
738e5348 8405 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
8406 relocations against local symbols evaluate to "G", and the EABI does
8407 not include R_MIPS_GOT_PAGE. */
c224138d 8408 page_gotno = 0;
0a44bf69
RS
8409 else
8410 /* Assume there are two loadable segments consisting of contiguous
8411 sections. Is 5 enough? */
c224138d
RS
8412 page_gotno = (loadable_size >> 16) + 5;
8413
8414 /* Choose the smaller of the two estimates; both are intended to be
8415 conservative. */
8416 if (page_gotno > g->page_gotno)
8417 page_gotno = g->page_gotno;
f4416af6 8418
c224138d 8419 g->local_gotno += page_gotno;
eea6121a 8420 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
d4596a51 8421 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6 8422
0f20cc35
DJ
8423 /* We need to calculate tls_gotno for global symbols at this point
8424 instead of building it up earlier, to avoid doublecounting
8425 entries for one global symbol from multiple input files. */
8426 count_tls_arg.info = info;
8427 count_tls_arg.needed = 0;
8428 elf_link_hash_traverse (elf_hash_table (info),
8429 mips_elf_count_global_tls_entries,
8430 &count_tls_arg);
8431 g->tls_gotno += count_tls_arg.needed;
8432 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8433
0a44bf69
RS
8434 /* VxWorks does not support multiple GOTs. It initializes $gp to
8435 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8436 dynamic loader. */
33bb52fb
RS
8437 if (htab->is_vxworks)
8438 {
8439 /* VxWorks executables do not need a GOT. */
8440 if (info->shared)
8441 {
8442 /* Each VxWorks GOT entry needs an explicit relocation. */
8443 unsigned int count;
8444
861fb55a 8445 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
33bb52fb
RS
8446 if (count)
8447 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8448 }
8449 }
8450 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 8451 {
a8028dd0 8452 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
8453 return FALSE;
8454 }
8455 else
8456 {
33bb52fb
RS
8457 struct mips_elf_count_tls_arg arg;
8458
8459 /* Set up TLS entries. */
0f20cc35
DJ
8460 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8461 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
33bb52fb
RS
8462
8463 /* Allocate room for the TLS relocations. */
8464 arg.info = info;
8465 arg.needed = 0;
8466 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8467 elf_link_hash_traverse (elf_hash_table (info),
8468 mips_elf_count_global_tls_relocs,
8469 &arg);
8470 if (arg.needed)
8471 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
0f20cc35 8472 }
b49e97c9 8473
b34976b6 8474 return TRUE;
b49e97c9
TS
8475}
8476
33bb52fb
RS
8477/* Estimate the size of the .MIPS.stubs section. */
8478
8479static void
8480mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8481{
8482 struct mips_elf_link_hash_table *htab;
8483 bfd_size_type dynsymcount;
8484
8485 htab = mips_elf_hash_table (info);
8486 if (htab->lazy_stub_count == 0)
8487 return;
8488
8489 /* IRIX rld assumes that a function stub isn't at the end of the .text
8490 section, so add a dummy entry to the end. */
8491 htab->lazy_stub_count++;
8492
8493 /* Get a worst-case estimate of the number of dynamic symbols needed.
8494 At this point, dynsymcount does not account for section symbols
8495 and count_section_dynsyms may overestimate the number that will
8496 be needed. */
8497 dynsymcount = (elf_hash_table (info)->dynsymcount
8498 + count_section_dynsyms (output_bfd, info));
8499
8500 /* Determine the size of one stub entry. */
8501 htab->function_stub_size = (dynsymcount > 0x10000
8502 ? MIPS_FUNCTION_STUB_BIG_SIZE
8503 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8504
8505 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8506}
8507
8508/* A mips_elf_link_hash_traverse callback for which DATA points to the
8509 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8510 allocate an entry in the stubs section. */
8511
8512static bfd_boolean
8513mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8514{
8515 struct mips_elf_link_hash_table *htab;
8516
8517 htab = (struct mips_elf_link_hash_table *) data;
8518 if (h->needs_lazy_stub)
8519 {
8520 h->root.root.u.def.section = htab->sstubs;
8521 h->root.root.u.def.value = htab->sstubs->size;
8522 h->root.plt.offset = htab->sstubs->size;
8523 htab->sstubs->size += htab->function_stub_size;
8524 }
8525 return TRUE;
8526}
8527
8528/* Allocate offsets in the stubs section to each symbol that needs one.
8529 Set the final size of the .MIPS.stub section. */
8530
8531static void
8532mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8533{
8534 struct mips_elf_link_hash_table *htab;
8535
8536 htab = mips_elf_hash_table (info);
8537 if (htab->lazy_stub_count == 0)
8538 return;
8539
8540 htab->sstubs->size = 0;
8541 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8542 mips_elf_allocate_lazy_stub, htab);
8543 htab->sstubs->size += htab->function_stub_size;
8544 BFD_ASSERT (htab->sstubs->size
8545 == htab->lazy_stub_count * htab->function_stub_size);
8546}
8547
b49e97c9
TS
8548/* Set the sizes of the dynamic sections. */
8549
b34976b6 8550bfd_boolean
9719ad41
RS
8551_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8552 struct bfd_link_info *info)
b49e97c9
TS
8553{
8554 bfd *dynobj;
861fb55a 8555 asection *s, *sreldyn;
b34976b6 8556 bfd_boolean reltext;
0a44bf69 8557 struct mips_elf_link_hash_table *htab;
b49e97c9 8558
0a44bf69 8559 htab = mips_elf_hash_table (info);
b49e97c9
TS
8560 dynobj = elf_hash_table (info)->dynobj;
8561 BFD_ASSERT (dynobj != NULL);
8562
8563 if (elf_hash_table (info)->dynamic_sections_created)
8564 {
8565 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 8566 if (info->executable)
b49e97c9
TS
8567 {
8568 s = bfd_get_section_by_name (dynobj, ".interp");
8569 BFD_ASSERT (s != NULL);
eea6121a 8570 s->size
b49e97c9
TS
8571 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8572 s->contents
8573 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8574 }
861fb55a
DJ
8575
8576 /* Create a symbol for the PLT, if we know that we are using it. */
8577 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8578 {
8579 struct elf_link_hash_entry *h;
8580
8581 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8582
8583 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8584 "_PROCEDURE_LINKAGE_TABLE_");
8585 htab->root.hplt = h;
8586 if (h == NULL)
8587 return FALSE;
8588 h->type = STT_FUNC;
8589 }
8590 }
4e41d0d7 8591
9a59ad6b
DJ
8592 /* Allocate space for global sym dynamic relocs. */
8593 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8594
33bb52fb
RS
8595 mips_elf_estimate_stub_size (output_bfd, info);
8596
8597 if (!mips_elf_lay_out_got (output_bfd, info))
8598 return FALSE;
8599
8600 mips_elf_lay_out_lazy_stubs (info);
8601
b49e97c9
TS
8602 /* The check_relocs and adjust_dynamic_symbol entry points have
8603 determined the sizes of the various dynamic sections. Allocate
8604 memory for them. */
b34976b6 8605 reltext = FALSE;
b49e97c9
TS
8606 for (s = dynobj->sections; s != NULL; s = s->next)
8607 {
8608 const char *name;
b49e97c9
TS
8609
8610 /* It's OK to base decisions on the section name, because none
8611 of the dynobj section names depend upon the input files. */
8612 name = bfd_get_section_name (dynobj, s);
8613
8614 if ((s->flags & SEC_LINKER_CREATED) == 0)
8615 continue;
8616
0112cd26 8617 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 8618 {
c456f082 8619 if (s->size != 0)
b49e97c9
TS
8620 {
8621 const char *outname;
8622 asection *target;
8623
8624 /* If this relocation section applies to a read only
8625 section, then we probably need a DT_TEXTREL entry.
0a44bf69 8626 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
8627 assert a DT_TEXTREL entry rather than testing whether
8628 there exists a relocation to a read only section or
8629 not. */
8630 outname = bfd_get_section_name (output_bfd,
8631 s->output_section);
8632 target = bfd_get_section_by_name (output_bfd, outname + 4);
8633 if ((target != NULL
8634 && (target->flags & SEC_READONLY) != 0
8635 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 8636 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 8637 reltext = TRUE;
b49e97c9
TS
8638
8639 /* We use the reloc_count field as a counter if we need
8640 to copy relocs into the output file. */
0a44bf69 8641 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 8642 s->reloc_count = 0;
f4416af6
AO
8643
8644 /* If combreloc is enabled, elf_link_sort_relocs() will
8645 sort relocations, but in a different way than we do,
8646 and before we're done creating relocations. Also, it
8647 will move them around between input sections'
8648 relocation's contents, so our sorting would be
8649 broken, so don't let it run. */
8650 info->combreloc = 0;
b49e97c9
TS
8651 }
8652 }
b49e97c9
TS
8653 else if (! info->shared
8654 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 8655 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 8656 {
5108fc1b 8657 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 8658 rtld to contain a pointer to the _r_debug structure. */
eea6121a 8659 s->size += 4;
b49e97c9
TS
8660 }
8661 else if (SGI_COMPAT (output_bfd)
0112cd26 8662 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 8663 s->size += mips_elf_hash_table (info)->compact_rel_size;
861fb55a
DJ
8664 else if (s == htab->splt)
8665 {
8666 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
8667 room for an extra nop to fill the delay slot. This is
8668 for CPUs without load interlocking. */
8669 if (! LOAD_INTERLOCKS_P (output_bfd)
8670 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
8671 s->size += 4;
8672 }
0112cd26 8673 else if (! CONST_STRNEQ (name, ".init")
33bb52fb 8674 && s != htab->sgot
0a44bf69 8675 && s != htab->sgotplt
861fb55a
DJ
8676 && s != htab->sstubs
8677 && s != htab->sdynbss)
b49e97c9
TS
8678 {
8679 /* It's not one of our sections, so don't allocate space. */
8680 continue;
8681 }
8682
c456f082 8683 if (s->size == 0)
b49e97c9 8684 {
8423293d 8685 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
8686 continue;
8687 }
8688
c456f082
AM
8689 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8690 continue;
8691
b49e97c9 8692 /* Allocate memory for the section contents. */
eea6121a 8693 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 8694 if (s->contents == NULL)
b49e97c9
TS
8695 {
8696 bfd_set_error (bfd_error_no_memory);
b34976b6 8697 return FALSE;
b49e97c9
TS
8698 }
8699 }
8700
8701 if (elf_hash_table (info)->dynamic_sections_created)
8702 {
8703 /* Add some entries to the .dynamic section. We fill in the
8704 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8705 must add the entries now so that we get the correct size for
5750dcec 8706 the .dynamic section. */
af5978fb
RS
8707
8708 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec
DJ
8709 DT_MIPS_RLD_MAP entry. This must come first because glibc
8710 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8711 looks at the first one it sees. */
af5978fb
RS
8712 if (!info->shared
8713 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8714 return FALSE;
b49e97c9 8715
5750dcec
DJ
8716 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8717 used by the debugger. */
8718 if (info->executable
8719 && !SGI_COMPAT (output_bfd)
8720 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8721 return FALSE;
8722
0a44bf69 8723 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
8724 info->flags |= DF_TEXTREL;
8725
8726 if ((info->flags & DF_TEXTREL) != 0)
8727 {
8728 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 8729 return FALSE;
943284cc
DJ
8730
8731 /* Clear the DF_TEXTREL flag. It will be set again if we
8732 write out an actual text relocation; we may not, because
8733 at this point we do not know whether e.g. any .eh_frame
8734 absolute relocations have been converted to PC-relative. */
8735 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
8736 }
8737
8738 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 8739 return FALSE;
b49e97c9 8740
861fb55a 8741 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 8742 if (htab->is_vxworks)
b49e97c9 8743 {
0a44bf69
RS
8744 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8745 use any of the DT_MIPS_* tags. */
861fb55a 8746 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8747 {
8748 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8749 return FALSE;
b49e97c9 8750
0a44bf69
RS
8751 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8752 return FALSE;
b49e97c9 8753
0a44bf69
RS
8754 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8755 return FALSE;
8756 }
b49e97c9 8757 }
0a44bf69
RS
8758 else
8759 {
861fb55a 8760 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
8761 {
8762 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8763 return FALSE;
b49e97c9 8764
0a44bf69
RS
8765 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8766 return FALSE;
b49e97c9 8767
0a44bf69
RS
8768 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8769 return FALSE;
8770 }
b49e97c9 8771
0a44bf69
RS
8772 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8773 return FALSE;
b49e97c9 8774
0a44bf69
RS
8775 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8776 return FALSE;
b49e97c9 8777
0a44bf69
RS
8778 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8779 return FALSE;
b49e97c9 8780
0a44bf69
RS
8781 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8782 return FALSE;
b49e97c9 8783
0a44bf69
RS
8784 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8785 return FALSE;
b49e97c9 8786
0a44bf69
RS
8787 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8788 return FALSE;
b49e97c9 8789
0a44bf69
RS
8790 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8791 return FALSE;
8792
8793 if (IRIX_COMPAT (dynobj) == ict_irix5
8794 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8795 return FALSE;
8796
8797 if (IRIX_COMPAT (dynobj) == ict_irix6
8798 && (bfd_get_section_by_name
8799 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8800 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8801 return FALSE;
8802 }
861fb55a
DJ
8803 if (htab->splt->size > 0)
8804 {
8805 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8806 return FALSE;
8807
8808 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8809 return FALSE;
8810
8811 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8812 return FALSE;
8813
8814 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8815 return FALSE;
8816 }
7a2b07ff
NS
8817 if (htab->is_vxworks
8818 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8819 return FALSE;
b49e97c9
TS
8820 }
8821
b34976b6 8822 return TRUE;
b49e97c9
TS
8823}
8824\f
81d43bff
RS
8825/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8826 Adjust its R_ADDEND field so that it is correct for the output file.
8827 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8828 and sections respectively; both use symbol indexes. */
8829
8830static void
8831mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8832 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8833 asection **local_sections, Elf_Internal_Rela *rel)
8834{
8835 unsigned int r_type, r_symndx;
8836 Elf_Internal_Sym *sym;
8837 asection *sec;
8838
8839 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8840 {
8841 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8842 if (r_type == R_MIPS16_GPREL
8843 || r_type == R_MIPS_GPREL16
8844 || r_type == R_MIPS_GPREL32
8845 || r_type == R_MIPS_LITERAL)
8846 {
8847 rel->r_addend += _bfd_get_gp_value (input_bfd);
8848 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8849 }
8850
8851 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8852 sym = local_syms + r_symndx;
8853
8854 /* Adjust REL's addend to account for section merging. */
8855 if (!info->relocatable)
8856 {
8857 sec = local_sections[r_symndx];
8858 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8859 }
8860
8861 /* This would normally be done by the rela_normal code in elflink.c. */
8862 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8863 rel->r_addend += local_sections[r_symndx]->output_offset;
8864 }
8865}
8866
b49e97c9
TS
8867/* Relocate a MIPS ELF section. */
8868
b34976b6 8869bfd_boolean
9719ad41
RS
8870_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8871 bfd *input_bfd, asection *input_section,
8872 bfd_byte *contents, Elf_Internal_Rela *relocs,
8873 Elf_Internal_Sym *local_syms,
8874 asection **local_sections)
b49e97c9
TS
8875{
8876 Elf_Internal_Rela *rel;
8877 const Elf_Internal_Rela *relend;
8878 bfd_vma addend = 0;
b34976b6 8879 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 8880 const struct elf_backend_data *bed;
b49e97c9
TS
8881
8882 bed = get_elf_backend_data (output_bfd);
8883 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8884 for (rel = relocs; rel < relend; ++rel)
8885 {
8886 const char *name;
c9adbffe 8887 bfd_vma value = 0;
b49e97c9 8888 reloc_howto_type *howto;
b34976b6
AM
8889 bfd_boolean require_jalx;
8890 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 8891 REL relocation. */
b34976b6 8892 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 8893 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 8894 const char *msg;
ab96bf03
AM
8895 unsigned long r_symndx;
8896 asection *sec;
749b8d9d
L
8897 Elf_Internal_Shdr *symtab_hdr;
8898 struct elf_link_hash_entry *h;
b49e97c9
TS
8899
8900 /* Find the relocation howto for this relocation. */
ab96bf03
AM
8901 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8902 NEWABI_P (input_bfd)
8903 && (MIPS_RELOC_RELA_P
8904 (input_bfd, input_section,
8905 rel - relocs)));
8906
8907 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 8908 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
ab96bf03 8909 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
749b8d9d
L
8910 {
8911 sec = local_sections[r_symndx];
8912 h = NULL;
8913 }
ab96bf03
AM
8914 else
8915 {
ab96bf03 8916 unsigned long extsymoff;
ab96bf03 8917
ab96bf03
AM
8918 extsymoff = 0;
8919 if (!elf_bad_symtab (input_bfd))
8920 extsymoff = symtab_hdr->sh_info;
8921 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8922 while (h->root.type == bfd_link_hash_indirect
8923 || h->root.type == bfd_link_hash_warning)
8924 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8925
8926 sec = NULL;
8927 if (h->root.type == bfd_link_hash_defined
8928 || h->root.type == bfd_link_hash_defweak)
8929 sec = h->root.u.def.section;
8930 }
8931
8932 if (sec != NULL && elf_discarded_section (sec))
8933 {
8934 /* For relocs against symbols from removed linkonce sections,
8935 or sections discarded by a linker script, we just want the
8936 section contents zeroed. Avoid any special processing. */
8937 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8938 rel->r_info = 0;
8939 rel->r_addend = 0;
8940 continue;
8941 }
8942
4a14403c 8943 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
8944 {
8945 /* Some 32-bit code uses R_MIPS_64. In particular, people use
8946 64-bit code, but make sure all their addresses are in the
8947 lowermost or uppermost 32-bit section of the 64-bit address
8948 space. Thus, when they use an R_MIPS_64 they mean what is
8949 usually meant by R_MIPS_32, with the exception that the
8950 stored value is sign-extended to 64 bits. */
b34976b6 8951 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
8952
8953 /* On big-endian systems, we need to lie about the position
8954 of the reloc. */
8955 if (bfd_big_endian (input_bfd))
8956 rel->r_offset += 4;
8957 }
b49e97c9
TS
8958
8959 if (!use_saved_addend_p)
8960 {
b49e97c9
TS
8961 /* If these relocations were originally of the REL variety,
8962 we must pull the addend out of the field that will be
8963 relocated. Otherwise, we simply use the contents of the
c224138d
RS
8964 RELA relocation. */
8965 if (mips_elf_rel_relocation_p (input_bfd, input_section,
8966 relocs, rel))
b49e97c9 8967 {
b34976b6 8968 rela_relocation_p = FALSE;
c224138d
RS
8969 addend = mips_elf_read_rel_addend (input_bfd, rel,
8970 howto, contents);
738e5348
RS
8971 if (hi16_reloc_p (r_type)
8972 || (got16_reloc_p (r_type)
b49e97c9 8973 && mips_elf_local_relocation_p (input_bfd, rel,
b34976b6 8974 local_sections, FALSE)))
b49e97c9 8975 {
c224138d
RS
8976 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8977 contents, &addend))
749b8d9d 8978 {
749b8d9d
L
8979 if (h)
8980 name = h->root.root.string;
8981 else
8982 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
8983 local_syms + r_symndx,
8984 sec);
8985 (*_bfd_error_handler)
8986 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
8987 input_bfd, input_section, name, howto->name,
8988 rel->r_offset);
749b8d9d 8989 }
b49e97c9 8990 }
30ac9238
RS
8991 else
8992 addend <<= howto->rightshift;
b49e97c9
TS
8993 }
8994 else
8995 addend = rel->r_addend;
81d43bff
RS
8996 mips_elf_adjust_addend (output_bfd, info, input_bfd,
8997 local_syms, local_sections, rel);
b49e97c9
TS
8998 }
8999
1049f94e 9000 if (info->relocatable)
b49e97c9 9001 {
4a14403c 9002 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
9003 && bfd_big_endian (input_bfd))
9004 rel->r_offset -= 4;
9005
81d43bff 9006 if (!rela_relocation_p && rel->r_addend)
5a659663 9007 {
81d43bff 9008 addend += rel->r_addend;
738e5348 9009 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
9010 addend = mips_elf_high (addend);
9011 else if (r_type == R_MIPS_HIGHER)
9012 addend = mips_elf_higher (addend);
9013 else if (r_type == R_MIPS_HIGHEST)
9014 addend = mips_elf_highest (addend);
30ac9238
RS
9015 else
9016 addend >>= howto->rightshift;
b49e97c9 9017
30ac9238
RS
9018 /* We use the source mask, rather than the destination
9019 mask because the place to which we are writing will be
9020 source of the addend in the final link. */
b49e97c9
TS
9021 addend &= howto->src_mask;
9022
5a659663 9023 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9024 /* See the comment above about using R_MIPS_64 in the 32-bit
9025 ABI. Here, we need to update the addend. It would be
9026 possible to get away with just using the R_MIPS_32 reloc
9027 but for endianness. */
9028 {
9029 bfd_vma sign_bits;
9030 bfd_vma low_bits;
9031 bfd_vma high_bits;
9032
9033 if (addend & ((bfd_vma) 1 << 31))
9034#ifdef BFD64
9035 sign_bits = ((bfd_vma) 1 << 32) - 1;
9036#else
9037 sign_bits = -1;
9038#endif
9039 else
9040 sign_bits = 0;
9041
9042 /* If we don't know that we have a 64-bit type,
9043 do two separate stores. */
9044 if (bfd_big_endian (input_bfd))
9045 {
9046 /* Store the sign-bits (which are most significant)
9047 first. */
9048 low_bits = sign_bits;
9049 high_bits = addend;
9050 }
9051 else
9052 {
9053 low_bits = addend;
9054 high_bits = sign_bits;
9055 }
9056 bfd_put_32 (input_bfd, low_bits,
9057 contents + rel->r_offset);
9058 bfd_put_32 (input_bfd, high_bits,
9059 contents + rel->r_offset + 4);
9060 continue;
9061 }
9062
9063 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9064 input_bfd, input_section,
b34976b6
AM
9065 contents, FALSE))
9066 return FALSE;
b49e97c9
TS
9067 }
9068
9069 /* Go on to the next relocation. */
9070 continue;
9071 }
9072
9073 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9074 relocations for the same offset. In that case we are
9075 supposed to treat the output of each relocation as the addend
9076 for the next. */
9077 if (rel + 1 < relend
9078 && rel->r_offset == rel[1].r_offset
9079 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 9080 use_saved_addend_p = TRUE;
b49e97c9 9081 else
b34976b6 9082 use_saved_addend_p = FALSE;
b49e97c9
TS
9083
9084 /* Figure out what value we are supposed to relocate. */
9085 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9086 input_section, info, rel,
9087 addend, howto, local_syms,
9088 local_sections, &value,
bce03d3d
AO
9089 &name, &require_jalx,
9090 use_saved_addend_p))
b49e97c9
TS
9091 {
9092 case bfd_reloc_continue:
9093 /* There's nothing to do. */
9094 continue;
9095
9096 case bfd_reloc_undefined:
9097 /* mips_elf_calculate_relocation already called the
9098 undefined_symbol callback. There's no real point in
9099 trying to perform the relocation at this point, so we
9100 just skip ahead to the next relocation. */
9101 continue;
9102
9103 case bfd_reloc_notsupported:
9104 msg = _("internal error: unsupported relocation error");
9105 info->callbacks->warning
9106 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 9107 return FALSE;
b49e97c9
TS
9108
9109 case bfd_reloc_overflow:
9110 if (use_saved_addend_p)
9111 /* Ignore overflow until we reach the last relocation for
9112 a given location. */
9113 ;
9114 else
9115 {
0e53d9da
AN
9116 struct mips_elf_link_hash_table *htab;
9117
9118 htab = mips_elf_hash_table (info);
b49e97c9 9119 BFD_ASSERT (name != NULL);
0e53d9da
AN
9120 if (!htab->small_data_overflow_reported
9121 && (howto->type == R_MIPS_GPREL16
9122 || howto->type == R_MIPS_LITERAL))
9123 {
91d6fa6a
NC
9124 msg = _("small-data section exceeds 64KB;"
9125 " lower small-data size limit (see option -G)");
0e53d9da
AN
9126
9127 htab->small_data_overflow_reported = TRUE;
9128 (*info->callbacks->einfo) ("%P: %s\n", msg);
9129 }
b49e97c9 9130 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 9131 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 9132 input_bfd, input_section, rel->r_offset)))
b34976b6 9133 return FALSE;
b49e97c9
TS
9134 }
9135 break;
9136
9137 case bfd_reloc_ok:
9138 break;
9139
9140 default:
9141 abort ();
9142 break;
9143 }
9144
9145 /* If we've got another relocation for the address, keep going
9146 until we reach the last one. */
9147 if (use_saved_addend_p)
9148 {
9149 addend = value;
9150 continue;
9151 }
9152
4a14403c 9153 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9154 /* See the comment above about using R_MIPS_64 in the 32-bit
9155 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9156 that calculated the right value. Now, however, we
9157 sign-extend the 32-bit result to 64-bits, and store it as a
9158 64-bit value. We are especially generous here in that we
9159 go to extreme lengths to support this usage on systems with
9160 only a 32-bit VMA. */
9161 {
9162 bfd_vma sign_bits;
9163 bfd_vma low_bits;
9164 bfd_vma high_bits;
9165
9166 if (value & ((bfd_vma) 1 << 31))
9167#ifdef BFD64
9168 sign_bits = ((bfd_vma) 1 << 32) - 1;
9169#else
9170 sign_bits = -1;
9171#endif
9172 else
9173 sign_bits = 0;
9174
9175 /* If we don't know that we have a 64-bit type,
9176 do two separate stores. */
9177 if (bfd_big_endian (input_bfd))
9178 {
9179 /* Undo what we did above. */
9180 rel->r_offset -= 4;
9181 /* Store the sign-bits (which are most significant)
9182 first. */
9183 low_bits = sign_bits;
9184 high_bits = value;
9185 }
9186 else
9187 {
9188 low_bits = value;
9189 high_bits = sign_bits;
9190 }
9191 bfd_put_32 (input_bfd, low_bits,
9192 contents + rel->r_offset);
9193 bfd_put_32 (input_bfd, high_bits,
9194 contents + rel->r_offset + 4);
9195 continue;
9196 }
9197
9198 /* Actually perform the relocation. */
9199 if (! mips_elf_perform_relocation (info, howto, rel, value,
9200 input_bfd, input_section,
9201 contents, require_jalx))
b34976b6 9202 return FALSE;
b49e97c9
TS
9203 }
9204
b34976b6 9205 return TRUE;
b49e97c9
TS
9206}
9207\f
861fb55a
DJ
9208/* A function that iterates over each entry in la25_stubs and fills
9209 in the code for each one. DATA points to a mips_htab_traverse_info. */
9210
9211static int
9212mips_elf_create_la25_stub (void **slot, void *data)
9213{
9214 struct mips_htab_traverse_info *hti;
9215 struct mips_elf_link_hash_table *htab;
9216 struct mips_elf_la25_stub *stub;
9217 asection *s;
9218 bfd_byte *loc;
9219 bfd_vma offset, target, target_high, target_low;
9220
9221 stub = (struct mips_elf_la25_stub *) *slot;
9222 hti = (struct mips_htab_traverse_info *) data;
9223 htab = mips_elf_hash_table (hti->info);
9224
9225 /* Create the section contents, if we haven't already. */
9226 s = stub->stub_section;
9227 loc = s->contents;
9228 if (loc == NULL)
9229 {
9230 loc = bfd_malloc (s->size);
9231 if (loc == NULL)
9232 {
9233 hti->error = TRUE;
9234 return FALSE;
9235 }
9236 s->contents = loc;
9237 }
9238
9239 /* Work out where in the section this stub should go. */
9240 offset = stub->offset;
9241
9242 /* Work out the target address. */
9243 target = (stub->h->root.root.u.def.section->output_section->vma
9244 + stub->h->root.root.u.def.section->output_offset
9245 + stub->h->root.root.u.def.value);
9246 target_high = ((target + 0x8000) >> 16) & 0xffff;
9247 target_low = (target & 0xffff);
9248
9249 if (stub->stub_section != htab->strampoline)
9250 {
9251 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9252 of the section and write the two instructions at the end. */
9253 memset (loc, 0, offset);
9254 loc += offset;
9255 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9256 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9257 }
9258 else
9259 {
9260 /* This is trampoline. */
9261 loc += offset;
9262 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9263 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9264 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9265 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9266 }
9267 return TRUE;
9268}
9269
b49e97c9
TS
9270/* If NAME is one of the special IRIX6 symbols defined by the linker,
9271 adjust it appropriately now. */
9272
9273static void
9719ad41
RS
9274mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9275 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
9276{
9277 /* The linker script takes care of providing names and values for
9278 these, but we must place them into the right sections. */
9279 static const char* const text_section_symbols[] = {
9280 "_ftext",
9281 "_etext",
9282 "__dso_displacement",
9283 "__elf_header",
9284 "__program_header_table",
9285 NULL
9286 };
9287
9288 static const char* const data_section_symbols[] = {
9289 "_fdata",
9290 "_edata",
9291 "_end",
9292 "_fbss",
9293 NULL
9294 };
9295
9296 const char* const *p;
9297 int i;
9298
9299 for (i = 0; i < 2; ++i)
9300 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9301 *p;
9302 ++p)
9303 if (strcmp (*p, name) == 0)
9304 {
9305 /* All of these symbols are given type STT_SECTION by the
9306 IRIX6 linker. */
9307 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 9308 sym->st_other = STO_PROTECTED;
b49e97c9
TS
9309
9310 /* The IRIX linker puts these symbols in special sections. */
9311 if (i == 0)
9312 sym->st_shndx = SHN_MIPS_TEXT;
9313 else
9314 sym->st_shndx = SHN_MIPS_DATA;
9315
9316 break;
9317 }
9318}
9319
9320/* Finish up dynamic symbol handling. We set the contents of various
9321 dynamic sections here. */
9322
b34976b6 9323bfd_boolean
9719ad41
RS
9324_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9325 struct bfd_link_info *info,
9326 struct elf_link_hash_entry *h,
9327 Elf_Internal_Sym *sym)
b49e97c9
TS
9328{
9329 bfd *dynobj;
b49e97c9 9330 asection *sgot;
f4416af6 9331 struct mips_got_info *g, *gg;
b49e97c9 9332 const char *name;
3d6746ca 9333 int idx;
5108fc1b 9334 struct mips_elf_link_hash_table *htab;
738e5348 9335 struct mips_elf_link_hash_entry *hmips;
b49e97c9 9336
5108fc1b 9337 htab = mips_elf_hash_table (info);
b49e97c9 9338 dynobj = elf_hash_table (info)->dynobj;
738e5348 9339 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9340
861fb55a
DJ
9341 BFD_ASSERT (!htab->is_vxworks);
9342
9343 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9344 {
9345 /* We've decided to create a PLT entry for this symbol. */
9346 bfd_byte *loc;
9347 bfd_vma header_address, plt_index, got_address;
9348 bfd_vma got_address_high, got_address_low, load;
9349 const bfd_vma *plt_entry;
9350
9351 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9352 BFD_ASSERT (h->dynindx != -1);
9353 BFD_ASSERT (htab->splt != NULL);
9354 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9355 BFD_ASSERT (!h->def_regular);
9356
9357 /* Calculate the address of the PLT header. */
9358 header_address = (htab->splt->output_section->vma
9359 + htab->splt->output_offset);
9360
9361 /* Calculate the index of the entry. */
9362 plt_index = ((h->plt.offset - htab->plt_header_size)
9363 / htab->plt_entry_size);
9364
9365 /* Calculate the address of the .got.plt entry. */
9366 got_address = (htab->sgotplt->output_section->vma
9367 + htab->sgotplt->output_offset
9368 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9369 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9370 got_address_low = got_address & 0xffff;
9371
9372 /* Initially point the .got.plt entry at the PLT header. */
9373 loc = (htab->sgotplt->contents
9374 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9375 if (ABI_64_P (output_bfd))
9376 bfd_put_64 (output_bfd, header_address, loc);
9377 else
9378 bfd_put_32 (output_bfd, header_address, loc);
9379
9380 /* Find out where the .plt entry should go. */
9381 loc = htab->splt->contents + h->plt.offset;
9382
9383 /* Pick the load opcode. */
9384 load = MIPS_ELF_LOAD_WORD (output_bfd);
9385
9386 /* Fill in the PLT entry itself. */
9387 plt_entry = mips_exec_plt_entry;
9388 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9389 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
6d30f5b2
NC
9390
9391 if (! LOAD_INTERLOCKS_P (output_bfd))
9392 {
9393 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9394 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9395 }
9396 else
9397 {
9398 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9399 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9400 }
861fb55a
DJ
9401
9402 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9403 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9404 plt_index, h->dynindx,
9405 R_MIPS_JUMP_SLOT, got_address);
9406
9407 /* We distinguish between PLT entries and lazy-binding stubs by
9408 giving the former an st_other value of STO_MIPS_PLT. Set the
9409 flag and leave the value if there are any relocations in the
9410 binary where pointer equality matters. */
9411 sym->st_shndx = SHN_UNDEF;
9412 if (h->pointer_equality_needed)
9413 sym->st_other = STO_MIPS_PLT;
9414 else
9415 sym->st_value = 0;
9416 }
9417 else if (h->plt.offset != MINUS_ONE)
b49e97c9 9418 {
861fb55a 9419 /* We've decided to create a lazy-binding stub. */
5108fc1b 9420 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
b49e97c9
TS
9421
9422 /* This symbol has a stub. Set it up. */
9423
9424 BFD_ASSERT (h->dynindx != -1);
9425
5108fc1b
RS
9426 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9427 || (h->dynindx <= 0xffff));
3d6746ca
DD
9428
9429 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
9430 sign extension at runtime in the stub, resulting in a negative
9431 index value. */
9432 if (h->dynindx & ~0x7fffffff)
b34976b6 9433 return FALSE;
b49e97c9
TS
9434
9435 /* Fill the stub. */
3d6746ca
DD
9436 idx = 0;
9437 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9438 idx += 4;
9439 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9440 idx += 4;
5108fc1b 9441 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
3d6746ca 9442 {
5108fc1b 9443 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
3d6746ca
DD
9444 stub + idx);
9445 idx += 4;
9446 }
9447 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9448 idx += 4;
b49e97c9 9449
3d6746ca
DD
9450 /* If a large stub is not required and sign extension is not a
9451 problem, then use legacy code in the stub. */
5108fc1b
RS
9452 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9453 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9454 else if (h->dynindx & ~0x7fff)
3d6746ca
DD
9455 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9456 else
5108fc1b
RS
9457 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9458 stub + idx);
9459
4e41d0d7
RS
9460 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9461 memcpy (htab->sstubs->contents + h->plt.offset,
9462 stub, htab->function_stub_size);
b49e97c9
TS
9463
9464 /* Mark the symbol as undefined. plt.offset != -1 occurs
9465 only for the referenced symbol. */
9466 sym->st_shndx = SHN_UNDEF;
9467
9468 /* The run-time linker uses the st_value field of the symbol
9469 to reset the global offset table entry for this external
9470 to its stub address when unlinking a shared object. */
4e41d0d7
RS
9471 sym->st_value = (htab->sstubs->output_section->vma
9472 + htab->sstubs->output_offset
c5ae1840 9473 + h->plt.offset);
b49e97c9
TS
9474 }
9475
738e5348
RS
9476 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9477 refer to the stub, since only the stub uses the standard calling
9478 conventions. */
9479 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9480 {
9481 BFD_ASSERT (hmips->need_fn_stub);
9482 sym->st_value = (hmips->fn_stub->output_section->vma
9483 + hmips->fn_stub->output_offset);
9484 sym->st_size = hmips->fn_stub->size;
9485 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9486 }
9487
b49e97c9 9488 BFD_ASSERT (h->dynindx != -1
f5385ebf 9489 || h->forced_local);
b49e97c9 9490
23cc69b6 9491 sgot = htab->sgot;
a8028dd0 9492 g = htab->got_info;
b49e97c9
TS
9493 BFD_ASSERT (g != NULL);
9494
9495 /* Run through the global symbol table, creating GOT entries for all
9496 the symbols that need them. */
9497 if (g->global_gotsym != NULL
9498 && h->dynindx >= g->global_gotsym->dynindx)
9499 {
9500 bfd_vma offset;
9501 bfd_vma value;
9502
6eaa6adc 9503 value = sym->st_value;
738e5348
RS
9504 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9505 R_MIPS_GOT16, info);
b49e97c9
TS
9506 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9507 }
9508
0f20cc35 9509 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
f4416af6
AO
9510 {
9511 struct mips_got_entry e, *p;
0626d451 9512 bfd_vma entry;
f4416af6 9513 bfd_vma offset;
f4416af6
AO
9514
9515 gg = g;
9516
9517 e.abfd = output_bfd;
9518 e.symndx = -1;
738e5348 9519 e.d.h = hmips;
0f20cc35 9520 e.tls_type = 0;
143d77c5 9521
f4416af6
AO
9522 for (g = g->next; g->next != gg; g = g->next)
9523 {
9524 if (g->got_entries
9525 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9526 &e)))
9527 {
9528 offset = p->gotidx;
0626d451
RS
9529 if (info->shared
9530 || (elf_hash_table (info)->dynamic_sections_created
9531 && p->d.h != NULL
f5385ebf
AM
9532 && p->d.h->root.def_dynamic
9533 && !p->d.h->root.def_regular))
0626d451
RS
9534 {
9535 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9536 the various compatibility problems, it's easier to mock
9537 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9538 mips_elf_create_dynamic_relocation to calculate the
9539 appropriate addend. */
9540 Elf_Internal_Rela rel[3];
9541
9542 memset (rel, 0, sizeof (rel));
9543 if (ABI_64_P (output_bfd))
9544 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9545 else
9546 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9547 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9548
9549 entry = 0;
9550 if (! (mips_elf_create_dynamic_relocation
9551 (output_bfd, info, rel,
9552 e.d.h, NULL, sym->st_value, &entry, sgot)))
9553 return FALSE;
9554 }
9555 else
9556 entry = sym->st_value;
9557 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
9558 }
9559 }
9560 }
9561
b49e97c9
TS
9562 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9563 name = h->root.root.string;
9564 if (strcmp (name, "_DYNAMIC") == 0
22edb2f1 9565 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
9566 sym->st_shndx = SHN_ABS;
9567 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9568 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9569 {
9570 sym->st_shndx = SHN_ABS;
9571 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9572 sym->st_value = 1;
9573 }
4a14403c 9574 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
9575 {
9576 sym->st_shndx = SHN_ABS;
9577 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9578 sym->st_value = elf_gp (output_bfd);
9579 }
9580 else if (SGI_COMPAT (output_bfd))
9581 {
9582 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9583 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9584 {
9585 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9586 sym->st_other = STO_PROTECTED;
9587 sym->st_value = 0;
9588 sym->st_shndx = SHN_MIPS_DATA;
9589 }
9590 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9591 {
9592 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9593 sym->st_other = STO_PROTECTED;
9594 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9595 sym->st_shndx = SHN_ABS;
9596 }
9597 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9598 {
9599 if (h->type == STT_FUNC)
9600 sym->st_shndx = SHN_MIPS_TEXT;
9601 else if (h->type == STT_OBJECT)
9602 sym->st_shndx = SHN_MIPS_DATA;
9603 }
9604 }
9605
861fb55a
DJ
9606 /* Emit a copy reloc, if needed. */
9607 if (h->needs_copy)
9608 {
9609 asection *s;
9610 bfd_vma symval;
9611
9612 BFD_ASSERT (h->dynindx != -1);
9613 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9614
9615 s = mips_elf_rel_dyn_section (info, FALSE);
9616 symval = (h->root.u.def.section->output_section->vma
9617 + h->root.u.def.section->output_offset
9618 + h->root.u.def.value);
9619 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9620 h->dynindx, R_MIPS_COPY, symval);
9621 }
9622
b49e97c9
TS
9623 /* Handle the IRIX6-specific symbols. */
9624 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9625 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9626
9627 if (! info->shared)
9628 {
9629 if (! mips_elf_hash_table (info)->use_rld_obj_head
9630 && (strcmp (name, "__rld_map") == 0
9631 || strcmp (name, "__RLD_MAP") == 0))
9632 {
9633 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9634 BFD_ASSERT (s != NULL);
9635 sym->st_value = s->output_section->vma + s->output_offset;
9719ad41 9636 bfd_put_32 (output_bfd, 0, s->contents);
b49e97c9
TS
9637 if (mips_elf_hash_table (info)->rld_value == 0)
9638 mips_elf_hash_table (info)->rld_value = sym->st_value;
9639 }
9640 else if (mips_elf_hash_table (info)->use_rld_obj_head
9641 && strcmp (name, "__rld_obj_head") == 0)
9642 {
9643 /* IRIX6 does not use a .rld_map section. */
9644 if (IRIX_COMPAT (output_bfd) == ict_irix5
9645 || IRIX_COMPAT (output_bfd) == ict_none)
9646 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9647 != NULL);
9648 mips_elf_hash_table (info)->rld_value = sym->st_value;
9649 }
9650 }
9651
738e5348
RS
9652 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9653 treat MIPS16 symbols like any other. */
30c09090 9654 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
9655 {
9656 BFD_ASSERT (sym->st_value & 1);
9657 sym->st_other -= STO_MIPS16;
9658 }
b49e97c9 9659
b34976b6 9660 return TRUE;
b49e97c9
TS
9661}
9662
0a44bf69
RS
9663/* Likewise, for VxWorks. */
9664
9665bfd_boolean
9666_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9667 struct bfd_link_info *info,
9668 struct elf_link_hash_entry *h,
9669 Elf_Internal_Sym *sym)
9670{
9671 bfd *dynobj;
9672 asection *sgot;
9673 struct mips_got_info *g;
9674 struct mips_elf_link_hash_table *htab;
9675
9676 htab = mips_elf_hash_table (info);
9677 dynobj = elf_hash_table (info)->dynobj;
9678
9679 if (h->plt.offset != (bfd_vma) -1)
9680 {
6d79d2ed 9681 bfd_byte *loc;
0a44bf69
RS
9682 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9683 Elf_Internal_Rela rel;
9684 static const bfd_vma *plt_entry;
9685
9686 BFD_ASSERT (h->dynindx != -1);
9687 BFD_ASSERT (htab->splt != NULL);
9688 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9689
9690 /* Calculate the address of the .plt entry. */
9691 plt_address = (htab->splt->output_section->vma
9692 + htab->splt->output_offset
9693 + h->plt.offset);
9694
9695 /* Calculate the index of the entry. */
9696 plt_index = ((h->plt.offset - htab->plt_header_size)
9697 / htab->plt_entry_size);
9698
9699 /* Calculate the address of the .got.plt entry. */
9700 got_address = (htab->sgotplt->output_section->vma
9701 + htab->sgotplt->output_offset
9702 + plt_index * 4);
9703
9704 /* Calculate the offset of the .got.plt entry from
9705 _GLOBAL_OFFSET_TABLE_. */
9706 got_offset = mips_elf_gotplt_index (info, h);
9707
9708 /* Calculate the offset for the branch at the start of the PLT
9709 entry. The branch jumps to the beginning of .plt. */
9710 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9711
9712 /* Fill in the initial value of the .got.plt entry. */
9713 bfd_put_32 (output_bfd, plt_address,
9714 htab->sgotplt->contents + plt_index * 4);
9715
9716 /* Find out where the .plt entry should go. */
9717 loc = htab->splt->contents + h->plt.offset;
9718
9719 if (info->shared)
9720 {
9721 plt_entry = mips_vxworks_shared_plt_entry;
9722 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9723 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9724 }
9725 else
9726 {
9727 bfd_vma got_address_high, got_address_low;
9728
9729 plt_entry = mips_vxworks_exec_plt_entry;
9730 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9731 got_address_low = got_address & 0xffff;
9732
9733 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9734 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9735 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9736 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9737 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9738 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9739 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9740 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9741
9742 loc = (htab->srelplt2->contents
9743 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9744
9745 /* Emit a relocation for the .got.plt entry. */
9746 rel.r_offset = got_address;
9747 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9748 rel.r_addend = h->plt.offset;
9749 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9750
9751 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9752 loc += sizeof (Elf32_External_Rela);
9753 rel.r_offset = plt_address + 8;
9754 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9755 rel.r_addend = got_offset;
9756 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9757
9758 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9759 loc += sizeof (Elf32_External_Rela);
9760 rel.r_offset += 4;
9761 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9762 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9763 }
9764
9765 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9766 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9767 rel.r_offset = got_address;
9768 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9769 rel.r_addend = 0;
9770 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9771
9772 if (!h->def_regular)
9773 sym->st_shndx = SHN_UNDEF;
9774 }
9775
9776 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9777
23cc69b6 9778 sgot = htab->sgot;
a8028dd0 9779 g = htab->got_info;
0a44bf69
RS
9780 BFD_ASSERT (g != NULL);
9781
9782 /* See if this symbol has an entry in the GOT. */
9783 if (g->global_gotsym != NULL
9784 && h->dynindx >= g->global_gotsym->dynindx)
9785 {
9786 bfd_vma offset;
9787 Elf_Internal_Rela outrel;
9788 bfd_byte *loc;
9789 asection *s;
9790
9791 /* Install the symbol value in the GOT. */
9792 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9793 R_MIPS_GOT16, info);
9794 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9795
9796 /* Add a dynamic relocation for it. */
9797 s = mips_elf_rel_dyn_section (info, FALSE);
9798 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9799 outrel.r_offset = (sgot->output_section->vma
9800 + sgot->output_offset
9801 + offset);
9802 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9803 outrel.r_addend = 0;
9804 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9805 }
9806
9807 /* Emit a copy reloc, if needed. */
9808 if (h->needs_copy)
9809 {
9810 Elf_Internal_Rela rel;
9811
9812 BFD_ASSERT (h->dynindx != -1);
9813
9814 rel.r_offset = (h->root.u.def.section->output_section->vma
9815 + h->root.u.def.section->output_offset
9816 + h->root.u.def.value);
9817 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9818 rel.r_addend = 0;
9819 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9820 htab->srelbss->contents
9821 + (htab->srelbss->reloc_count
9822 * sizeof (Elf32_External_Rela)));
9823 ++htab->srelbss->reloc_count;
9824 }
9825
9826 /* If this is a mips16 symbol, force the value to be even. */
30c09090 9827 if (ELF_ST_IS_MIPS16 (sym->st_other))
0a44bf69
RS
9828 sym->st_value &= ~1;
9829
9830 return TRUE;
9831}
9832
861fb55a
DJ
9833/* Write out a plt0 entry to the beginning of .plt. */
9834
9835static void
9836mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9837{
9838 bfd_byte *loc;
9839 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9840 static const bfd_vma *plt_entry;
9841 struct mips_elf_link_hash_table *htab;
9842
9843 htab = mips_elf_hash_table (info);
9844 if (ABI_64_P (output_bfd))
9845 plt_entry = mips_n64_exec_plt0_entry;
9846 else if (ABI_N32_P (output_bfd))
9847 plt_entry = mips_n32_exec_plt0_entry;
9848 else
9849 plt_entry = mips_o32_exec_plt0_entry;
9850
9851 /* Calculate the value of .got.plt. */
9852 gotplt_value = (htab->sgotplt->output_section->vma
9853 + htab->sgotplt->output_offset);
9854 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9855 gotplt_value_low = gotplt_value & 0xffff;
9856
9857 /* The PLT sequence is not safe for N64 if .got.plt's address can
9858 not be loaded in two instructions. */
9859 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9860 || ~(gotplt_value | 0x7fffffff) == 0);
9861
9862 /* Install the PLT header. */
9863 loc = htab->splt->contents;
9864 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9865 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9866 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9867 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9868 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9869 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9870 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9871 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9872}
9873
0a44bf69
RS
9874/* Install the PLT header for a VxWorks executable and finalize the
9875 contents of .rela.plt.unloaded. */
9876
9877static void
9878mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9879{
9880 Elf_Internal_Rela rela;
9881 bfd_byte *loc;
9882 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9883 static const bfd_vma *plt_entry;
9884 struct mips_elf_link_hash_table *htab;
9885
9886 htab = mips_elf_hash_table (info);
9887 plt_entry = mips_vxworks_exec_plt0_entry;
9888
9889 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9890 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9891 + htab->root.hgot->root.u.def.section->output_offset
9892 + htab->root.hgot->root.u.def.value);
9893
9894 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9895 got_value_low = got_value & 0xffff;
9896
9897 /* Calculate the address of the PLT header. */
9898 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9899
9900 /* Install the PLT header. */
9901 loc = htab->splt->contents;
9902 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9903 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9904 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9905 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9906 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9907 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9908
9909 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9910 loc = htab->srelplt2->contents;
9911 rela.r_offset = plt_address;
9912 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9913 rela.r_addend = 0;
9914 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9915 loc += sizeof (Elf32_External_Rela);
9916
9917 /* Output the relocation for the following addiu of
9918 %lo(_GLOBAL_OFFSET_TABLE_). */
9919 rela.r_offset += 4;
9920 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9921 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9922 loc += sizeof (Elf32_External_Rela);
9923
9924 /* Fix up the remaining relocations. They may have the wrong
9925 symbol index for _G_O_T_ or _P_L_T_ depending on the order
9926 in which symbols were output. */
9927 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9928 {
9929 Elf_Internal_Rela rel;
9930
9931 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9932 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9933 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9934 loc += sizeof (Elf32_External_Rela);
9935
9936 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9937 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9938 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9939 loc += sizeof (Elf32_External_Rela);
9940
9941 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9942 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9943 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9944 loc += sizeof (Elf32_External_Rela);
9945 }
9946}
9947
9948/* Install the PLT header for a VxWorks shared library. */
9949
9950static void
9951mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9952{
9953 unsigned int i;
9954 struct mips_elf_link_hash_table *htab;
9955
9956 htab = mips_elf_hash_table (info);
9957
9958 /* We just need to copy the entry byte-by-byte. */
9959 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9960 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9961 htab->splt->contents + i * 4);
9962}
9963
b49e97c9
TS
9964/* Finish up the dynamic sections. */
9965
b34976b6 9966bfd_boolean
9719ad41
RS
9967_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9968 struct bfd_link_info *info)
b49e97c9
TS
9969{
9970 bfd *dynobj;
9971 asection *sdyn;
9972 asection *sgot;
f4416af6 9973 struct mips_got_info *gg, *g;
0a44bf69 9974 struct mips_elf_link_hash_table *htab;
b49e97c9 9975
0a44bf69 9976 htab = mips_elf_hash_table (info);
b49e97c9
TS
9977 dynobj = elf_hash_table (info)->dynobj;
9978
9979 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9980
23cc69b6
RS
9981 sgot = htab->sgot;
9982 gg = htab->got_info;
b49e97c9
TS
9983
9984 if (elf_hash_table (info)->dynamic_sections_created)
9985 {
9986 bfd_byte *b;
943284cc 9987 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
9988
9989 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
9990 BFD_ASSERT (gg != NULL);
9991
9992 g = mips_elf_got_for_ibfd (gg, output_bfd);
b49e97c9
TS
9993 BFD_ASSERT (g != NULL);
9994
9995 for (b = sdyn->contents;
eea6121a 9996 b < sdyn->contents + sdyn->size;
b49e97c9
TS
9997 b += MIPS_ELF_DYN_SIZE (dynobj))
9998 {
9999 Elf_Internal_Dyn dyn;
10000 const char *name;
10001 size_t elemsize;
10002 asection *s;
b34976b6 10003 bfd_boolean swap_out_p;
b49e97c9
TS
10004
10005 /* Read in the current dynamic entry. */
10006 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10007
10008 /* Assume that we're going to modify it and write it out. */
b34976b6 10009 swap_out_p = TRUE;
b49e97c9
TS
10010
10011 switch (dyn.d_tag)
10012 {
10013 case DT_RELENT:
b49e97c9
TS
10014 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10015 break;
10016
0a44bf69
RS
10017 case DT_RELAENT:
10018 BFD_ASSERT (htab->is_vxworks);
10019 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10020 break;
10021
b49e97c9
TS
10022 case DT_STRSZ:
10023 /* Rewrite DT_STRSZ. */
10024 dyn.d_un.d_val =
10025 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10026 break;
10027
10028 case DT_PLTGOT:
861fb55a
DJ
10029 s = htab->sgot;
10030 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10031 break;
10032
10033 case DT_MIPS_PLTGOT:
10034 s = htab->sgotplt;
10035 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
10036 break;
10037
10038 case DT_MIPS_RLD_VERSION:
10039 dyn.d_un.d_val = 1; /* XXX */
10040 break;
10041
10042 case DT_MIPS_FLAGS:
10043 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10044 break;
10045
b49e97c9 10046 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
10047 {
10048 time_t t;
10049 time (&t);
10050 dyn.d_un.d_val = t;
10051 }
b49e97c9
TS
10052 break;
10053
10054 case DT_MIPS_ICHECKSUM:
10055 /* XXX FIXME: */
b34976b6 10056 swap_out_p = FALSE;
b49e97c9
TS
10057 break;
10058
10059 case DT_MIPS_IVERSION:
10060 /* XXX FIXME: */
b34976b6 10061 swap_out_p = FALSE;
b49e97c9
TS
10062 break;
10063
10064 case DT_MIPS_BASE_ADDRESS:
10065 s = output_bfd->sections;
10066 BFD_ASSERT (s != NULL);
10067 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10068 break;
10069
10070 case DT_MIPS_LOCAL_GOTNO:
10071 dyn.d_un.d_val = g->local_gotno;
10072 break;
10073
10074 case DT_MIPS_UNREFEXTNO:
10075 /* The index into the dynamic symbol table which is the
10076 entry of the first external symbol that is not
10077 referenced within the same object. */
10078 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10079 break;
10080
10081 case DT_MIPS_GOTSYM:
f4416af6 10082 if (gg->global_gotsym)
b49e97c9 10083 {
f4416af6 10084 dyn.d_un.d_val = gg->global_gotsym->dynindx;
b49e97c9
TS
10085 break;
10086 }
10087 /* In case if we don't have global got symbols we default
10088 to setting DT_MIPS_GOTSYM to the same value as
10089 DT_MIPS_SYMTABNO, so we just fall through. */
10090
10091 case DT_MIPS_SYMTABNO:
10092 name = ".dynsym";
10093 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10094 s = bfd_get_section_by_name (output_bfd, name);
10095 BFD_ASSERT (s != NULL);
10096
eea6121a 10097 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
10098 break;
10099
10100 case DT_MIPS_HIPAGENO:
861fb55a 10101 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
10102 break;
10103
10104 case DT_MIPS_RLD_MAP:
10105 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10106 break;
10107
10108 case DT_MIPS_OPTIONS:
10109 s = (bfd_get_section_by_name
10110 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10111 dyn.d_un.d_ptr = s->vma;
10112 break;
10113
0a44bf69
RS
10114 case DT_RELASZ:
10115 BFD_ASSERT (htab->is_vxworks);
10116 /* The count does not include the JUMP_SLOT relocations. */
10117 if (htab->srelplt)
10118 dyn.d_un.d_val -= htab->srelplt->size;
10119 break;
10120
10121 case DT_PLTREL:
861fb55a
DJ
10122 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10123 if (htab->is_vxworks)
10124 dyn.d_un.d_val = DT_RELA;
10125 else
10126 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
10127 break;
10128
10129 case DT_PLTRELSZ:
861fb55a 10130 BFD_ASSERT (htab->use_plts_and_copy_relocs);
0a44bf69
RS
10131 dyn.d_un.d_val = htab->srelplt->size;
10132 break;
10133
10134 case DT_JMPREL:
861fb55a
DJ
10135 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10136 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
0a44bf69
RS
10137 + htab->srelplt->output_offset);
10138 break;
10139
943284cc
DJ
10140 case DT_TEXTREL:
10141 /* If we didn't need any text relocations after all, delete
10142 the dynamic tag. */
10143 if (!(info->flags & DF_TEXTREL))
10144 {
10145 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10146 swap_out_p = FALSE;
10147 }
10148 break;
10149
10150 case DT_FLAGS:
10151 /* If we didn't need any text relocations after all, clear
10152 DF_TEXTREL from DT_FLAGS. */
10153 if (!(info->flags & DF_TEXTREL))
10154 dyn.d_un.d_val &= ~DF_TEXTREL;
10155 else
10156 swap_out_p = FALSE;
10157 break;
10158
b49e97c9 10159 default:
b34976b6 10160 swap_out_p = FALSE;
7a2b07ff
NS
10161 if (htab->is_vxworks
10162 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10163 swap_out_p = TRUE;
b49e97c9
TS
10164 break;
10165 }
10166
943284cc 10167 if (swap_out_p || dyn_skipped)
b49e97c9 10168 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
10169 (dynobj, &dyn, b - dyn_skipped);
10170
10171 if (dyn_to_skip)
10172 {
10173 dyn_skipped += dyn_to_skip;
10174 dyn_to_skip = 0;
10175 }
b49e97c9 10176 }
943284cc
DJ
10177
10178 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10179 if (dyn_skipped > 0)
10180 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
10181 }
10182
b55fd4d4
DJ
10183 if (sgot != NULL && sgot->size > 0
10184 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 10185 {
0a44bf69
RS
10186 if (htab->is_vxworks)
10187 {
10188 /* The first entry of the global offset table points to the
10189 ".dynamic" section. The second is initialized by the
10190 loader and contains the shared library identifier.
10191 The third is also initialized by the loader and points
10192 to the lazy resolution stub. */
10193 MIPS_ELF_PUT_WORD (output_bfd,
10194 sdyn->output_offset + sdyn->output_section->vma,
10195 sgot->contents);
10196 MIPS_ELF_PUT_WORD (output_bfd, 0,
10197 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10198 MIPS_ELF_PUT_WORD (output_bfd, 0,
10199 sgot->contents
10200 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10201 }
10202 else
10203 {
10204 /* The first entry of the global offset table will be filled at
10205 runtime. The second entry will be used by some runtime loaders.
10206 This isn't the case of IRIX rld. */
10207 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 10208 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
10209 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10210 }
b49e97c9 10211
54938e2a
TS
10212 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10213 = MIPS_ELF_GOT_SIZE (output_bfd);
10214 }
b49e97c9 10215
f4416af6
AO
10216 /* Generate dynamic relocations for the non-primary gots. */
10217 if (gg != NULL && gg->next)
10218 {
10219 Elf_Internal_Rela rel[3];
10220 bfd_vma addend = 0;
10221
10222 memset (rel, 0, sizeof (rel));
10223 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10224
10225 for (g = gg->next; g->next != gg; g = g->next)
10226 {
91d6fa6a 10227 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 10228 + g->next->tls_gotno;
f4416af6 10229
9719ad41 10230 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 10231 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
10232 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10233 sgot->contents
91d6fa6a 10234 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6
AO
10235
10236 if (! info->shared)
10237 continue;
10238
91d6fa6a 10239 while (got_index < g->assigned_gotno)
f4416af6
AO
10240 {
10241 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
91d6fa6a 10242 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
10243 if (!(mips_elf_create_dynamic_relocation
10244 (output_bfd, info, rel, NULL,
10245 bfd_abs_section_ptr,
10246 0, &addend, sgot)))
10247 return FALSE;
10248 BFD_ASSERT (addend == 0);
10249 }
10250 }
10251 }
10252
3133ddbf
DJ
10253 /* The generation of dynamic relocations for the non-primary gots
10254 adds more dynamic relocations. We cannot count them until
10255 here. */
10256
10257 if (elf_hash_table (info)->dynamic_sections_created)
10258 {
10259 bfd_byte *b;
10260 bfd_boolean swap_out_p;
10261
10262 BFD_ASSERT (sdyn != NULL);
10263
10264 for (b = sdyn->contents;
10265 b < sdyn->contents + sdyn->size;
10266 b += MIPS_ELF_DYN_SIZE (dynobj))
10267 {
10268 Elf_Internal_Dyn dyn;
10269 asection *s;
10270
10271 /* Read in the current dynamic entry. */
10272 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10273
10274 /* Assume that we're going to modify it and write it out. */
10275 swap_out_p = TRUE;
10276
10277 switch (dyn.d_tag)
10278 {
10279 case DT_RELSZ:
10280 /* Reduce DT_RELSZ to account for any relocations we
10281 decided not to make. This is for the n64 irix rld,
10282 which doesn't seem to apply any relocations if there
10283 are trailing null entries. */
0a44bf69 10284 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
10285 dyn.d_un.d_val = (s->reloc_count
10286 * (ABI_64_P (output_bfd)
10287 ? sizeof (Elf64_Mips_External_Rel)
10288 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
10289 /* Adjust the section size too. Tools like the prelinker
10290 can reasonably expect the values to the same. */
10291 elf_section_data (s->output_section)->this_hdr.sh_size
10292 = dyn.d_un.d_val;
3133ddbf
DJ
10293 break;
10294
10295 default:
10296 swap_out_p = FALSE;
10297 break;
10298 }
10299
10300 if (swap_out_p)
10301 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10302 (dynobj, &dyn, b);
10303 }
10304 }
10305
b49e97c9 10306 {
b49e97c9
TS
10307 asection *s;
10308 Elf32_compact_rel cpt;
10309
b49e97c9
TS
10310 if (SGI_COMPAT (output_bfd))
10311 {
10312 /* Write .compact_rel section out. */
10313 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10314 if (s != NULL)
10315 {
10316 cpt.id1 = 1;
10317 cpt.num = s->reloc_count;
10318 cpt.id2 = 2;
10319 cpt.offset = (s->output_section->filepos
10320 + sizeof (Elf32_External_compact_rel));
10321 cpt.reserved0 = 0;
10322 cpt.reserved1 = 0;
10323 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10324 ((Elf32_External_compact_rel *)
10325 s->contents));
10326
10327 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 10328 if (htab->sstubs != NULL)
b49e97c9
TS
10329 {
10330 file_ptr dummy_offset;
10331
4e41d0d7
RS
10332 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10333 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10334 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 10335 htab->function_stub_size);
b49e97c9
TS
10336 }
10337 }
10338 }
10339
0a44bf69
RS
10340 /* The psABI says that the dynamic relocations must be sorted in
10341 increasing order of r_symndx. The VxWorks EABI doesn't require
10342 this, and because the code below handles REL rather than RELA
10343 relocations, using it for VxWorks would be outright harmful. */
10344 if (!htab->is_vxworks)
b49e97c9 10345 {
0a44bf69
RS
10346 s = mips_elf_rel_dyn_section (info, FALSE);
10347 if (s != NULL
10348 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10349 {
10350 reldyn_sorting_bfd = output_bfd;
b49e97c9 10351
0a44bf69
RS
10352 if (ABI_64_P (output_bfd))
10353 qsort ((Elf64_External_Rel *) s->contents + 1,
10354 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10355 sort_dynamic_relocs_64);
10356 else
10357 qsort ((Elf32_External_Rel *) s->contents + 1,
10358 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10359 sort_dynamic_relocs);
10360 }
b49e97c9 10361 }
b49e97c9
TS
10362 }
10363
861fb55a 10364 if (htab->splt && htab->splt->size > 0)
0a44bf69 10365 {
861fb55a
DJ
10366 if (htab->is_vxworks)
10367 {
10368 if (info->shared)
10369 mips_vxworks_finish_shared_plt (output_bfd, info);
10370 else
10371 mips_vxworks_finish_exec_plt (output_bfd, info);
10372 }
0a44bf69 10373 else
861fb55a
DJ
10374 {
10375 BFD_ASSERT (!info->shared);
10376 mips_finish_exec_plt (output_bfd, info);
10377 }
0a44bf69 10378 }
b34976b6 10379 return TRUE;
b49e97c9
TS
10380}
10381
b49e97c9 10382
64543e1a
RS
10383/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10384
10385static void
9719ad41 10386mips_set_isa_flags (bfd *abfd)
b49e97c9 10387{
64543e1a 10388 flagword val;
b49e97c9
TS
10389
10390 switch (bfd_get_mach (abfd))
10391 {
10392 default:
10393 case bfd_mach_mips3000:
10394 val = E_MIPS_ARCH_1;
10395 break;
10396
10397 case bfd_mach_mips3900:
10398 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10399 break;
10400
10401 case bfd_mach_mips6000:
10402 val = E_MIPS_ARCH_2;
10403 break;
10404
10405 case bfd_mach_mips4000:
10406 case bfd_mach_mips4300:
10407 case bfd_mach_mips4400:
10408 case bfd_mach_mips4600:
10409 val = E_MIPS_ARCH_3;
10410 break;
10411
10412 case bfd_mach_mips4010:
10413 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10414 break;
10415
10416 case bfd_mach_mips4100:
10417 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10418 break;
10419
10420 case bfd_mach_mips4111:
10421 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10422 break;
10423
00707a0e
RS
10424 case bfd_mach_mips4120:
10425 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10426 break;
10427
b49e97c9
TS
10428 case bfd_mach_mips4650:
10429 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10430 break;
10431
00707a0e
RS
10432 case bfd_mach_mips5400:
10433 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10434 break;
10435
10436 case bfd_mach_mips5500:
10437 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10438 break;
10439
0d2e43ed
ILT
10440 case bfd_mach_mips9000:
10441 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10442 break;
10443
b49e97c9 10444 case bfd_mach_mips5000:
5a7ea749 10445 case bfd_mach_mips7000:
b49e97c9
TS
10446 case bfd_mach_mips8000:
10447 case bfd_mach_mips10000:
10448 case bfd_mach_mips12000:
3aa3176b
TS
10449 case bfd_mach_mips14000:
10450 case bfd_mach_mips16000:
b49e97c9
TS
10451 val = E_MIPS_ARCH_4;
10452 break;
10453
10454 case bfd_mach_mips5:
10455 val = E_MIPS_ARCH_5;
10456 break;
10457
350cc38d
MS
10458 case bfd_mach_mips_loongson_2e:
10459 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10460 break;
10461
10462 case bfd_mach_mips_loongson_2f:
10463 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10464 break;
10465
b49e97c9
TS
10466 case bfd_mach_mips_sb1:
10467 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10468 break;
10469
6f179bd0
AN
10470 case bfd_mach_mips_octeon:
10471 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10472 break;
10473
52b6b6b9
JM
10474 case bfd_mach_mips_xlr:
10475 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10476 break;
10477
b49e97c9
TS
10478 case bfd_mach_mipsisa32:
10479 val = E_MIPS_ARCH_32;
10480 break;
10481
10482 case bfd_mach_mipsisa64:
10483 val = E_MIPS_ARCH_64;
af7ee8bf
CD
10484 break;
10485
10486 case bfd_mach_mipsisa32r2:
10487 val = E_MIPS_ARCH_32R2;
10488 break;
5f74bc13
CD
10489
10490 case bfd_mach_mipsisa64r2:
10491 val = E_MIPS_ARCH_64R2;
10492 break;
b49e97c9 10493 }
b49e97c9
TS
10494 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10495 elf_elfheader (abfd)->e_flags |= val;
10496
64543e1a
RS
10497}
10498
10499
10500/* The final processing done just before writing out a MIPS ELF object
10501 file. This gets the MIPS architecture right based on the machine
10502 number. This is used by both the 32-bit and the 64-bit ABI. */
10503
10504void
9719ad41
RS
10505_bfd_mips_elf_final_write_processing (bfd *abfd,
10506 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
10507{
10508 unsigned int i;
10509 Elf_Internal_Shdr **hdrpp;
10510 const char *name;
10511 asection *sec;
10512
10513 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10514 is nonzero. This is for compatibility with old objects, which used
10515 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10516 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10517 mips_set_isa_flags (abfd);
10518
b49e97c9
TS
10519 /* Set the sh_info field for .gptab sections and other appropriate
10520 info for each special section. */
10521 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10522 i < elf_numsections (abfd);
10523 i++, hdrpp++)
10524 {
10525 switch ((*hdrpp)->sh_type)
10526 {
10527 case SHT_MIPS_MSYM:
10528 case SHT_MIPS_LIBLIST:
10529 sec = bfd_get_section_by_name (abfd, ".dynstr");
10530 if (sec != NULL)
10531 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10532 break;
10533
10534 case SHT_MIPS_GPTAB:
10535 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10536 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10537 BFD_ASSERT (name != NULL
0112cd26 10538 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
10539 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10540 BFD_ASSERT (sec != NULL);
10541 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10542 break;
10543
10544 case SHT_MIPS_CONTENT:
10545 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10546 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10547 BFD_ASSERT (name != NULL
0112cd26 10548 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
10549 sec = bfd_get_section_by_name (abfd,
10550 name + sizeof ".MIPS.content" - 1);
10551 BFD_ASSERT (sec != NULL);
10552 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10553 break;
10554
10555 case SHT_MIPS_SYMBOL_LIB:
10556 sec = bfd_get_section_by_name (abfd, ".dynsym");
10557 if (sec != NULL)
10558 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10559 sec = bfd_get_section_by_name (abfd, ".liblist");
10560 if (sec != NULL)
10561 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10562 break;
10563
10564 case SHT_MIPS_EVENTS:
10565 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10566 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10567 BFD_ASSERT (name != NULL);
0112cd26 10568 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
10569 sec = bfd_get_section_by_name (abfd,
10570 name + sizeof ".MIPS.events" - 1);
10571 else
10572 {
0112cd26 10573 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
10574 sec = bfd_get_section_by_name (abfd,
10575 (name
10576 + sizeof ".MIPS.post_rel" - 1));
10577 }
10578 BFD_ASSERT (sec != NULL);
10579 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10580 break;
10581
10582 }
10583 }
10584}
10585\f
8dc1a139 10586/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
10587 segments. */
10588
10589int
a6b96beb
AM
10590_bfd_mips_elf_additional_program_headers (bfd *abfd,
10591 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
10592{
10593 asection *s;
10594 int ret = 0;
10595
10596 /* See if we need a PT_MIPS_REGINFO segment. */
10597 s = bfd_get_section_by_name (abfd, ".reginfo");
10598 if (s && (s->flags & SEC_LOAD))
10599 ++ret;
10600
10601 /* See if we need a PT_MIPS_OPTIONS segment. */
10602 if (IRIX_COMPAT (abfd) == ict_irix6
10603 && bfd_get_section_by_name (abfd,
10604 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10605 ++ret;
10606
10607 /* See if we need a PT_MIPS_RTPROC segment. */
10608 if (IRIX_COMPAT (abfd) == ict_irix5
10609 && bfd_get_section_by_name (abfd, ".dynamic")
10610 && bfd_get_section_by_name (abfd, ".mdebug"))
10611 ++ret;
10612
98c904a8
RS
10613 /* Allocate a PT_NULL header in dynamic objects. See
10614 _bfd_mips_elf_modify_segment_map for details. */
10615 if (!SGI_COMPAT (abfd)
10616 && bfd_get_section_by_name (abfd, ".dynamic"))
10617 ++ret;
10618
b49e97c9
TS
10619 return ret;
10620}
10621
8dc1a139 10622/* Modify the segment map for an IRIX5 executable. */
b49e97c9 10623
b34976b6 10624bfd_boolean
9719ad41 10625_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 10626 struct bfd_link_info *info)
b49e97c9
TS
10627{
10628 asection *s;
10629 struct elf_segment_map *m, **pm;
10630 bfd_size_type amt;
10631
10632 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10633 segment. */
10634 s = bfd_get_section_by_name (abfd, ".reginfo");
10635 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10636 {
10637 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10638 if (m->p_type == PT_MIPS_REGINFO)
10639 break;
10640 if (m == NULL)
10641 {
10642 amt = sizeof *m;
9719ad41 10643 m = bfd_zalloc (abfd, amt);
b49e97c9 10644 if (m == NULL)
b34976b6 10645 return FALSE;
b49e97c9
TS
10646
10647 m->p_type = PT_MIPS_REGINFO;
10648 m->count = 1;
10649 m->sections[0] = s;
10650
10651 /* We want to put it after the PHDR and INTERP segments. */
10652 pm = &elf_tdata (abfd)->segment_map;
10653 while (*pm != NULL
10654 && ((*pm)->p_type == PT_PHDR
10655 || (*pm)->p_type == PT_INTERP))
10656 pm = &(*pm)->next;
10657
10658 m->next = *pm;
10659 *pm = m;
10660 }
10661 }
10662
10663 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10664 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 10665 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 10666 table. */
c1fd6598
AO
10667 if (NEWABI_P (abfd)
10668 /* On non-IRIX6 new abi, we'll have already created a segment
10669 for this section, so don't create another. I'm not sure this
10670 is not also the case for IRIX 6, but I can't test it right
10671 now. */
10672 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
10673 {
10674 for (s = abfd->sections; s; s = s->next)
10675 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10676 break;
10677
10678 if (s)
10679 {
10680 struct elf_segment_map *options_segment;
10681
98a8deaf
RS
10682 pm = &elf_tdata (abfd)->segment_map;
10683 while (*pm != NULL
10684 && ((*pm)->p_type == PT_PHDR
10685 || (*pm)->p_type == PT_INTERP))
10686 pm = &(*pm)->next;
b49e97c9 10687
8ded5a0f
AM
10688 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10689 {
10690 amt = sizeof (struct elf_segment_map);
10691 options_segment = bfd_zalloc (abfd, amt);
10692 options_segment->next = *pm;
10693 options_segment->p_type = PT_MIPS_OPTIONS;
10694 options_segment->p_flags = PF_R;
10695 options_segment->p_flags_valid = TRUE;
10696 options_segment->count = 1;
10697 options_segment->sections[0] = s;
10698 *pm = options_segment;
10699 }
b49e97c9
TS
10700 }
10701 }
10702 else
10703 {
10704 if (IRIX_COMPAT (abfd) == ict_irix5)
10705 {
10706 /* If there are .dynamic and .mdebug sections, we make a room
10707 for the RTPROC header. FIXME: Rewrite without section names. */
10708 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10709 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10710 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10711 {
10712 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10713 if (m->p_type == PT_MIPS_RTPROC)
10714 break;
10715 if (m == NULL)
10716 {
10717 amt = sizeof *m;
9719ad41 10718 m = bfd_zalloc (abfd, amt);
b49e97c9 10719 if (m == NULL)
b34976b6 10720 return FALSE;
b49e97c9
TS
10721
10722 m->p_type = PT_MIPS_RTPROC;
10723
10724 s = bfd_get_section_by_name (abfd, ".rtproc");
10725 if (s == NULL)
10726 {
10727 m->count = 0;
10728 m->p_flags = 0;
10729 m->p_flags_valid = 1;
10730 }
10731 else
10732 {
10733 m->count = 1;
10734 m->sections[0] = s;
10735 }
10736
10737 /* We want to put it after the DYNAMIC segment. */
10738 pm = &elf_tdata (abfd)->segment_map;
10739 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10740 pm = &(*pm)->next;
10741 if (*pm != NULL)
10742 pm = &(*pm)->next;
10743
10744 m->next = *pm;
10745 *pm = m;
10746 }
10747 }
10748 }
8dc1a139 10749 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
10750 .dynstr, .dynsym, and .hash sections, and everything in
10751 between. */
10752 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10753 pm = &(*pm)->next)
10754 if ((*pm)->p_type == PT_DYNAMIC)
10755 break;
10756 m = *pm;
10757 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10758 {
10759 /* For a normal mips executable the permissions for the PT_DYNAMIC
10760 segment are read, write and execute. We do that here since
10761 the code in elf.c sets only the read permission. This matters
10762 sometimes for the dynamic linker. */
10763 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10764 {
10765 m->p_flags = PF_R | PF_W | PF_X;
10766 m->p_flags_valid = 1;
10767 }
10768 }
f6f62d6f
RS
10769 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10770 glibc's dynamic linker has traditionally derived the number of
10771 tags from the p_filesz field, and sometimes allocates stack
10772 arrays of that size. An overly-big PT_DYNAMIC segment can
10773 be actively harmful in such cases. Making PT_DYNAMIC contain
10774 other sections can also make life hard for the prelinker,
10775 which might move one of the other sections to a different
10776 PT_LOAD segment. */
10777 if (SGI_COMPAT (abfd)
10778 && m != NULL
10779 && m->count == 1
10780 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
10781 {
10782 static const char *sec_names[] =
10783 {
10784 ".dynamic", ".dynstr", ".dynsym", ".hash"
10785 };
10786 bfd_vma low, high;
10787 unsigned int i, c;
10788 struct elf_segment_map *n;
10789
792b4a53 10790 low = ~(bfd_vma) 0;
b49e97c9
TS
10791 high = 0;
10792 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10793 {
10794 s = bfd_get_section_by_name (abfd, sec_names[i]);
10795 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10796 {
10797 bfd_size_type sz;
10798
10799 if (low > s->vma)
10800 low = s->vma;
eea6121a 10801 sz = s->size;
b49e97c9
TS
10802 if (high < s->vma + sz)
10803 high = s->vma + sz;
10804 }
10805 }
10806
10807 c = 0;
10808 for (s = abfd->sections; s != NULL; s = s->next)
10809 if ((s->flags & SEC_LOAD) != 0
10810 && s->vma >= low
eea6121a 10811 && s->vma + s->size <= high)
b49e97c9
TS
10812 ++c;
10813
10814 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 10815 n = bfd_zalloc (abfd, amt);
b49e97c9 10816 if (n == NULL)
b34976b6 10817 return FALSE;
b49e97c9
TS
10818 *n = *m;
10819 n->count = c;
10820
10821 i = 0;
10822 for (s = abfd->sections; s != NULL; s = s->next)
10823 {
10824 if ((s->flags & SEC_LOAD) != 0
10825 && s->vma >= low
eea6121a 10826 && s->vma + s->size <= high)
b49e97c9
TS
10827 {
10828 n->sections[i] = s;
10829 ++i;
10830 }
10831 }
10832
10833 *pm = n;
10834 }
10835 }
10836
98c904a8
RS
10837 /* Allocate a spare program header in dynamic objects so that tools
10838 like the prelinker can add an extra PT_LOAD entry.
10839
10840 If the prelinker needs to make room for a new PT_LOAD entry, its
10841 standard procedure is to move the first (read-only) sections into
10842 the new (writable) segment. However, the MIPS ABI requires
10843 .dynamic to be in a read-only segment, and the section will often
10844 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10845
10846 Although the prelinker could in principle move .dynamic to a
10847 writable segment, it seems better to allocate a spare program
10848 header instead, and avoid the need to move any sections.
10849 There is a long tradition of allocating spare dynamic tags,
10850 so allocating a spare program header seems like a natural
7c8b76cc
JM
10851 extension.
10852
10853 If INFO is NULL, we may be copying an already prelinked binary
10854 with objcopy or strip, so do not add this header. */
10855 if (info != NULL
10856 && !SGI_COMPAT (abfd)
98c904a8
RS
10857 && bfd_get_section_by_name (abfd, ".dynamic"))
10858 {
10859 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10860 if ((*pm)->p_type == PT_NULL)
10861 break;
10862 if (*pm == NULL)
10863 {
10864 m = bfd_zalloc (abfd, sizeof (*m));
10865 if (m == NULL)
10866 return FALSE;
10867
10868 m->p_type = PT_NULL;
10869 *pm = m;
10870 }
10871 }
10872
b34976b6 10873 return TRUE;
b49e97c9
TS
10874}
10875\f
10876/* Return the section that should be marked against GC for a given
10877 relocation. */
10878
10879asection *
9719ad41 10880_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 10881 struct bfd_link_info *info,
9719ad41
RS
10882 Elf_Internal_Rela *rel,
10883 struct elf_link_hash_entry *h,
10884 Elf_Internal_Sym *sym)
b49e97c9
TS
10885{
10886 /* ??? Do mips16 stub sections need to be handled special? */
10887
10888 if (h != NULL)
07adf181
AM
10889 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10890 {
10891 case R_MIPS_GNU_VTINHERIT:
10892 case R_MIPS_GNU_VTENTRY:
10893 return NULL;
10894 }
b49e97c9 10895
07adf181 10896 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
10897}
10898
10899/* Update the got entry reference counts for the section being removed. */
10900
b34976b6 10901bfd_boolean
9719ad41
RS
10902_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10903 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10904 asection *sec ATTRIBUTE_UNUSED,
10905 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
10906{
10907#if 0
10908 Elf_Internal_Shdr *symtab_hdr;
10909 struct elf_link_hash_entry **sym_hashes;
10910 bfd_signed_vma *local_got_refcounts;
10911 const Elf_Internal_Rela *rel, *relend;
10912 unsigned long r_symndx;
10913 struct elf_link_hash_entry *h;
10914
7dda2462
TG
10915 if (info->relocatable)
10916 return TRUE;
10917
b49e97c9
TS
10918 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10919 sym_hashes = elf_sym_hashes (abfd);
10920 local_got_refcounts = elf_local_got_refcounts (abfd);
10921
10922 relend = relocs + sec->reloc_count;
10923 for (rel = relocs; rel < relend; rel++)
10924 switch (ELF_R_TYPE (abfd, rel->r_info))
10925 {
738e5348
RS
10926 case R_MIPS16_GOT16:
10927 case R_MIPS16_CALL16:
b49e97c9
TS
10928 case R_MIPS_GOT16:
10929 case R_MIPS_CALL16:
10930 case R_MIPS_CALL_HI16:
10931 case R_MIPS_CALL_LO16:
10932 case R_MIPS_GOT_HI16:
10933 case R_MIPS_GOT_LO16:
4a14403c
TS
10934 case R_MIPS_GOT_DISP:
10935 case R_MIPS_GOT_PAGE:
10936 case R_MIPS_GOT_OFST:
b49e97c9
TS
10937 /* ??? It would seem that the existing MIPS code does no sort
10938 of reference counting or whatnot on its GOT and PLT entries,
10939 so it is not possible to garbage collect them at this time. */
10940 break;
10941
10942 default:
10943 break;
10944 }
10945#endif
10946
b34976b6 10947 return TRUE;
b49e97c9
TS
10948}
10949\f
10950/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10951 hiding the old indirect symbol. Process additional relocation
10952 information. Also called for weakdefs, in which case we just let
10953 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
10954
10955void
fcfa13d2 10956_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
10957 struct elf_link_hash_entry *dir,
10958 struct elf_link_hash_entry *ind)
b49e97c9
TS
10959{
10960 struct mips_elf_link_hash_entry *dirmips, *indmips;
10961
fcfa13d2 10962 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 10963
861fb55a
DJ
10964 dirmips = (struct mips_elf_link_hash_entry *) dir;
10965 indmips = (struct mips_elf_link_hash_entry *) ind;
10966 /* Any absolute non-dynamic relocations against an indirect or weak
10967 definition will be against the target symbol. */
10968 if (indmips->has_static_relocs)
10969 dirmips->has_static_relocs = TRUE;
10970
b49e97c9
TS
10971 if (ind->root.type != bfd_link_hash_indirect)
10972 return;
10973
b49e97c9
TS
10974 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10975 if (indmips->readonly_reloc)
b34976b6 10976 dirmips->readonly_reloc = TRUE;
b49e97c9 10977 if (indmips->no_fn_stub)
b34976b6 10978 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
10979 if (indmips->fn_stub)
10980 {
10981 dirmips->fn_stub = indmips->fn_stub;
10982 indmips->fn_stub = NULL;
10983 }
10984 if (indmips->need_fn_stub)
10985 {
10986 dirmips->need_fn_stub = TRUE;
10987 indmips->need_fn_stub = FALSE;
10988 }
10989 if (indmips->call_stub)
10990 {
10991 dirmips->call_stub = indmips->call_stub;
10992 indmips->call_stub = NULL;
10993 }
10994 if (indmips->call_fp_stub)
10995 {
10996 dirmips->call_fp_stub = indmips->call_fp_stub;
10997 indmips->call_fp_stub = NULL;
10998 }
634835ae
RS
10999 if (indmips->global_got_area < dirmips->global_got_area)
11000 dirmips->global_got_area = indmips->global_got_area;
11001 if (indmips->global_got_area < GGA_NONE)
11002 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
11003 if (indmips->has_nonpic_branches)
11004 dirmips->has_nonpic_branches = TRUE;
0f20cc35
DJ
11005
11006 if (dirmips->tls_type == 0)
11007 dirmips->tls_type = indmips->tls_type;
b49e97c9 11008}
b49e97c9 11009\f
d01414a5
TS
11010#define PDR_SIZE 32
11011
b34976b6 11012bfd_boolean
9719ad41
RS
11013_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11014 struct bfd_link_info *info)
d01414a5
TS
11015{
11016 asection *o;
b34976b6 11017 bfd_boolean ret = FALSE;
d01414a5
TS
11018 unsigned char *tdata;
11019 size_t i, skip;
11020
11021 o = bfd_get_section_by_name (abfd, ".pdr");
11022 if (! o)
b34976b6 11023 return FALSE;
eea6121a 11024 if (o->size == 0)
b34976b6 11025 return FALSE;
eea6121a 11026 if (o->size % PDR_SIZE != 0)
b34976b6 11027 return FALSE;
d01414a5
TS
11028 if (o->output_section != NULL
11029 && bfd_is_abs_section (o->output_section))
b34976b6 11030 return FALSE;
d01414a5 11031
eea6121a 11032 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 11033 if (! tdata)
b34976b6 11034 return FALSE;
d01414a5 11035
9719ad41 11036 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 11037 info->keep_memory);
d01414a5
TS
11038 if (!cookie->rels)
11039 {
11040 free (tdata);
b34976b6 11041 return FALSE;
d01414a5
TS
11042 }
11043
11044 cookie->rel = cookie->rels;
11045 cookie->relend = cookie->rels + o->reloc_count;
11046
eea6121a 11047 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 11048 {
c152c796 11049 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
11050 {
11051 tdata[i] = 1;
11052 skip ++;
11053 }
11054 }
11055
11056 if (skip != 0)
11057 {
f0abc2a1 11058 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 11059 o->size -= skip * PDR_SIZE;
b34976b6 11060 ret = TRUE;
d01414a5
TS
11061 }
11062 else
11063 free (tdata);
11064
11065 if (! info->keep_memory)
11066 free (cookie->rels);
11067
11068 return ret;
11069}
11070
b34976b6 11071bfd_boolean
9719ad41 11072_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
11073{
11074 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
11075 return TRUE;
11076 return FALSE;
53bfd6b4 11077}
d01414a5 11078
b34976b6 11079bfd_boolean
c7b8f16e
JB
11080_bfd_mips_elf_write_section (bfd *output_bfd,
11081 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11082 asection *sec, bfd_byte *contents)
d01414a5
TS
11083{
11084 bfd_byte *to, *from, *end;
11085 int i;
11086
11087 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 11088 return FALSE;
d01414a5 11089
f0abc2a1 11090 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 11091 return FALSE;
d01414a5
TS
11092
11093 to = contents;
eea6121a 11094 end = contents + sec->size;
d01414a5
TS
11095 for (from = contents, i = 0;
11096 from < end;
11097 from += PDR_SIZE, i++)
11098 {
f0abc2a1 11099 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
11100 continue;
11101 if (to != from)
11102 memcpy (to, from, PDR_SIZE);
11103 to += PDR_SIZE;
11104 }
11105 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 11106 sec->output_offset, sec->size);
b34976b6 11107 return TRUE;
d01414a5 11108}
53bfd6b4 11109\f
b49e97c9
TS
11110/* MIPS ELF uses a special find_nearest_line routine in order the
11111 handle the ECOFF debugging information. */
11112
11113struct mips_elf_find_line
11114{
11115 struct ecoff_debug_info d;
11116 struct ecoff_find_line i;
11117};
11118
b34976b6 11119bfd_boolean
9719ad41
RS
11120_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11121 asymbol **symbols, bfd_vma offset,
11122 const char **filename_ptr,
11123 const char **functionname_ptr,
11124 unsigned int *line_ptr)
b49e97c9
TS
11125{
11126 asection *msec;
11127
11128 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11129 filename_ptr, functionname_ptr,
11130 line_ptr))
b34976b6 11131 return TRUE;
b49e97c9
TS
11132
11133 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11134 filename_ptr, functionname_ptr,
9719ad41 11135 line_ptr, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 11136 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 11137 return TRUE;
b49e97c9
TS
11138
11139 msec = bfd_get_section_by_name (abfd, ".mdebug");
11140 if (msec != NULL)
11141 {
11142 flagword origflags;
11143 struct mips_elf_find_line *fi;
11144 const struct ecoff_debug_swap * const swap =
11145 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11146
11147 /* If we are called during a link, mips_elf_final_link may have
11148 cleared the SEC_HAS_CONTENTS field. We force it back on here
11149 if appropriate (which it normally will be). */
11150 origflags = msec->flags;
11151 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11152 msec->flags |= SEC_HAS_CONTENTS;
11153
11154 fi = elf_tdata (abfd)->find_line_info;
11155 if (fi == NULL)
11156 {
11157 bfd_size_type external_fdr_size;
11158 char *fraw_src;
11159 char *fraw_end;
11160 struct fdr *fdr_ptr;
11161 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11162
9719ad41 11163 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
11164 if (fi == NULL)
11165 {
11166 msec->flags = origflags;
b34976b6 11167 return FALSE;
b49e97c9
TS
11168 }
11169
11170 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11171 {
11172 msec->flags = origflags;
b34976b6 11173 return FALSE;
b49e97c9
TS
11174 }
11175
11176 /* Swap in the FDR information. */
11177 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 11178 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
11179 if (fi->d.fdr == NULL)
11180 {
11181 msec->flags = origflags;
b34976b6 11182 return FALSE;
b49e97c9
TS
11183 }
11184 external_fdr_size = swap->external_fdr_size;
11185 fdr_ptr = fi->d.fdr;
11186 fraw_src = (char *) fi->d.external_fdr;
11187 fraw_end = (fraw_src
11188 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11189 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 11190 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9
TS
11191
11192 elf_tdata (abfd)->find_line_info = fi;
11193
11194 /* Note that we don't bother to ever free this information.
11195 find_nearest_line is either called all the time, as in
11196 objdump -l, so the information should be saved, or it is
11197 rarely called, as in ld error messages, so the memory
11198 wasted is unimportant. Still, it would probably be a
11199 good idea for free_cached_info to throw it away. */
11200 }
11201
11202 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11203 &fi->i, filename_ptr, functionname_ptr,
11204 line_ptr))
11205 {
11206 msec->flags = origflags;
b34976b6 11207 return TRUE;
b49e97c9
TS
11208 }
11209
11210 msec->flags = origflags;
11211 }
11212
11213 /* Fall back on the generic ELF find_nearest_line routine. */
11214
11215 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11216 filename_ptr, functionname_ptr,
11217 line_ptr);
11218}
4ab527b0
FF
11219
11220bfd_boolean
11221_bfd_mips_elf_find_inliner_info (bfd *abfd,
11222 const char **filename_ptr,
11223 const char **functionname_ptr,
11224 unsigned int *line_ptr)
11225{
11226 bfd_boolean found;
11227 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11228 functionname_ptr, line_ptr,
11229 & elf_tdata (abfd)->dwarf2_find_line_info);
11230 return found;
11231}
11232
b49e97c9
TS
11233\f
11234/* When are writing out the .options or .MIPS.options section,
11235 remember the bytes we are writing out, so that we can install the
11236 GP value in the section_processing routine. */
11237
b34976b6 11238bfd_boolean
9719ad41
RS
11239_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11240 const void *location,
11241 file_ptr offset, bfd_size_type count)
b49e97c9 11242{
cc2e31b9 11243 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
11244 {
11245 bfd_byte *c;
11246
11247 if (elf_section_data (section) == NULL)
11248 {
11249 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 11250 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 11251 if (elf_section_data (section) == NULL)
b34976b6 11252 return FALSE;
b49e97c9 11253 }
f0abc2a1 11254 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
11255 if (c == NULL)
11256 {
eea6121a 11257 c = bfd_zalloc (abfd, section->size);
b49e97c9 11258 if (c == NULL)
b34976b6 11259 return FALSE;
f0abc2a1 11260 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
11261 }
11262
9719ad41 11263 memcpy (c + offset, location, count);
b49e97c9
TS
11264 }
11265
11266 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11267 count);
11268}
11269
11270/* This is almost identical to bfd_generic_get_... except that some
11271 MIPS relocations need to be handled specially. Sigh. */
11272
11273bfd_byte *
9719ad41
RS
11274_bfd_elf_mips_get_relocated_section_contents
11275 (bfd *abfd,
11276 struct bfd_link_info *link_info,
11277 struct bfd_link_order *link_order,
11278 bfd_byte *data,
11279 bfd_boolean relocatable,
11280 asymbol **symbols)
b49e97c9
TS
11281{
11282 /* Get enough memory to hold the stuff */
11283 bfd *input_bfd = link_order->u.indirect.section->owner;
11284 asection *input_section = link_order->u.indirect.section;
eea6121a 11285 bfd_size_type sz;
b49e97c9
TS
11286
11287 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11288 arelent **reloc_vector = NULL;
11289 long reloc_count;
11290
11291 if (reloc_size < 0)
11292 goto error_return;
11293
9719ad41 11294 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
11295 if (reloc_vector == NULL && reloc_size != 0)
11296 goto error_return;
11297
11298 /* read in the section */
eea6121a
AM
11299 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11300 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
11301 goto error_return;
11302
b49e97c9
TS
11303 reloc_count = bfd_canonicalize_reloc (input_bfd,
11304 input_section,
11305 reloc_vector,
11306 symbols);
11307 if (reloc_count < 0)
11308 goto error_return;
11309
11310 if (reloc_count > 0)
11311 {
11312 arelent **parent;
11313 /* for mips */
11314 int gp_found;
11315 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11316
11317 {
11318 struct bfd_hash_entry *h;
11319 struct bfd_link_hash_entry *lh;
11320 /* Skip all this stuff if we aren't mixing formats. */
11321 if (abfd && input_bfd
11322 && abfd->xvec == input_bfd->xvec)
11323 lh = 0;
11324 else
11325 {
b34976b6 11326 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
11327 lh = (struct bfd_link_hash_entry *) h;
11328 }
11329 lookup:
11330 if (lh)
11331 {
11332 switch (lh->type)
11333 {
11334 case bfd_link_hash_undefined:
11335 case bfd_link_hash_undefweak:
11336 case bfd_link_hash_common:
11337 gp_found = 0;
11338 break;
11339 case bfd_link_hash_defined:
11340 case bfd_link_hash_defweak:
11341 gp_found = 1;
11342 gp = lh->u.def.value;
11343 break;
11344 case bfd_link_hash_indirect:
11345 case bfd_link_hash_warning:
11346 lh = lh->u.i.link;
11347 /* @@FIXME ignoring warning for now */
11348 goto lookup;
11349 case bfd_link_hash_new:
11350 default:
11351 abort ();
11352 }
11353 }
11354 else
11355 gp_found = 0;
11356 }
11357 /* end mips */
9719ad41 11358 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 11359 {
9719ad41 11360 char *error_message = NULL;
b49e97c9
TS
11361 bfd_reloc_status_type r;
11362
11363 /* Specific to MIPS: Deal with relocation types that require
11364 knowing the gp of the output bfd. */
11365 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 11366
8236346f
EC
11367 /* If we've managed to find the gp and have a special
11368 function for the relocation then go ahead, else default
11369 to the generic handling. */
11370 if (gp_found
11371 && (*parent)->howto->special_function
11372 == _bfd_mips_elf32_gprel16_reloc)
11373 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11374 input_section, relocatable,
11375 data, gp);
11376 else
86324f90 11377 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
11378 input_section,
11379 relocatable ? abfd : NULL,
11380 &error_message);
b49e97c9 11381
1049f94e 11382 if (relocatable)
b49e97c9
TS
11383 {
11384 asection *os = input_section->output_section;
11385
11386 /* A partial link, so keep the relocs */
11387 os->orelocation[os->reloc_count] = *parent;
11388 os->reloc_count++;
11389 }
11390
11391 if (r != bfd_reloc_ok)
11392 {
11393 switch (r)
11394 {
11395 case bfd_reloc_undefined:
11396 if (!((*link_info->callbacks->undefined_symbol)
11397 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 11398 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
11399 goto error_return;
11400 break;
11401 case bfd_reloc_dangerous:
9719ad41 11402 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
11403 if (!((*link_info->callbacks->reloc_dangerous)
11404 (link_info, error_message, input_bfd, input_section,
11405 (*parent)->address)))
11406 goto error_return;
11407 break;
11408 case bfd_reloc_overflow:
11409 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
11410 (link_info, NULL,
11411 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
11412 (*parent)->howto->name, (*parent)->addend,
11413 input_bfd, input_section, (*parent)->address)))
11414 goto error_return;
11415 break;
11416 case bfd_reloc_outofrange:
11417 default:
11418 abort ();
11419 break;
11420 }
11421
11422 }
11423 }
11424 }
11425 if (reloc_vector != NULL)
11426 free (reloc_vector);
11427 return data;
11428
11429error_return:
11430 if (reloc_vector != NULL)
11431 free (reloc_vector);
11432 return NULL;
11433}
11434\f
d5eaccd7
RS
11435/* Allocate ABFD's target-dependent data. */
11436
11437bfd_boolean
11438_bfd_mips_elf_mkobject (bfd *abfd)
11439{
11440 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
11441 MIPS_ELF_TDATA);
11442}
11443
b49e97c9
TS
11444/* Create a MIPS ELF linker hash table. */
11445
11446struct bfd_link_hash_table *
9719ad41 11447_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
11448{
11449 struct mips_elf_link_hash_table *ret;
11450 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11451
9719ad41
RS
11452 ret = bfd_malloc (amt);
11453 if (ret == NULL)
b49e97c9
TS
11454 return NULL;
11455
66eb6687
AM
11456 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11457 mips_elf_link_hash_newfunc,
11458 sizeof (struct mips_elf_link_hash_entry)))
b49e97c9 11459 {
e2d34d7d 11460 free (ret);
b49e97c9
TS
11461 return NULL;
11462 }
11463
11464#if 0
11465 /* We no longer use this. */
11466 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11467 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11468#endif
11469 ret->procedure_count = 0;
11470 ret->compact_rel_size = 0;
b34976b6 11471 ret->use_rld_obj_head = FALSE;
b49e97c9 11472 ret->rld_value = 0;
b34976b6 11473 ret->mips16_stubs_seen = FALSE;
861fb55a 11474 ret->use_plts_and_copy_relocs = FALSE;
0a44bf69 11475 ret->is_vxworks = FALSE;
0e53d9da 11476 ret->small_data_overflow_reported = FALSE;
0a44bf69
RS
11477 ret->srelbss = NULL;
11478 ret->sdynbss = NULL;
11479 ret->srelplt = NULL;
11480 ret->srelplt2 = NULL;
11481 ret->sgotplt = NULL;
11482 ret->splt = NULL;
4e41d0d7 11483 ret->sstubs = NULL;
a8028dd0
RS
11484 ret->sgot = NULL;
11485 ret->got_info = NULL;
0a44bf69
RS
11486 ret->plt_header_size = 0;
11487 ret->plt_entry_size = 0;
33bb52fb 11488 ret->lazy_stub_count = 0;
5108fc1b 11489 ret->function_stub_size = 0;
861fb55a
DJ
11490 ret->strampoline = NULL;
11491 ret->la25_stubs = NULL;
11492 ret->add_stub_section = NULL;
b49e97c9
TS
11493
11494 return &ret->root.root;
11495}
0a44bf69
RS
11496
11497/* Likewise, but indicate that the target is VxWorks. */
11498
11499struct bfd_link_hash_table *
11500_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11501{
11502 struct bfd_link_hash_table *ret;
11503
11504 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11505 if (ret)
11506 {
11507 struct mips_elf_link_hash_table *htab;
11508
11509 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
11510 htab->use_plts_and_copy_relocs = TRUE;
11511 htab->is_vxworks = TRUE;
0a44bf69
RS
11512 }
11513 return ret;
11514}
861fb55a
DJ
11515
11516/* A function that the linker calls if we are allowed to use PLTs
11517 and copy relocs. */
11518
11519void
11520_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11521{
11522 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11523}
b49e97c9
TS
11524\f
11525/* We need to use a special link routine to handle the .reginfo and
11526 the .mdebug sections. We need to merge all instances of these
11527 sections together, not write them all out sequentially. */
11528
b34976b6 11529bfd_boolean
9719ad41 11530_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 11531{
b49e97c9
TS
11532 asection *o;
11533 struct bfd_link_order *p;
11534 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11535 asection *rtproc_sec;
11536 Elf32_RegInfo reginfo;
11537 struct ecoff_debug_info debug;
861fb55a 11538 struct mips_htab_traverse_info hti;
7a2a6943
NC
11539 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11540 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 11541 HDRR *symhdr = &debug.symbolic_header;
9719ad41 11542 void *mdebug_handle = NULL;
b49e97c9
TS
11543 asection *s;
11544 EXTR esym;
11545 unsigned int i;
11546 bfd_size_type amt;
0a44bf69 11547 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
11548
11549 static const char * const secname[] =
11550 {
11551 ".text", ".init", ".fini", ".data",
11552 ".rodata", ".sdata", ".sbss", ".bss"
11553 };
11554 static const int sc[] =
11555 {
11556 scText, scInit, scFini, scData,
11557 scRData, scSData, scSBss, scBss
11558 };
11559
d4596a51
RS
11560 /* Sort the dynamic symbols so that those with GOT entries come after
11561 those without. */
0a44bf69 11562 htab = mips_elf_hash_table (info);
d4596a51
RS
11563 if (!mips_elf_sort_hash_table (abfd, info))
11564 return FALSE;
b49e97c9 11565
861fb55a
DJ
11566 /* Create any scheduled LA25 stubs. */
11567 hti.info = info;
11568 hti.output_bfd = abfd;
11569 hti.error = FALSE;
11570 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11571 if (hti.error)
11572 return FALSE;
11573
b49e97c9
TS
11574 /* Get a value for the GP register. */
11575 if (elf_gp (abfd) == 0)
11576 {
11577 struct bfd_link_hash_entry *h;
11578
b34976b6 11579 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 11580 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
11581 elf_gp (abfd) = (h->u.def.value
11582 + h->u.def.section->output_section->vma
11583 + h->u.def.section->output_offset);
0a44bf69
RS
11584 else if (htab->is_vxworks
11585 && (h = bfd_link_hash_lookup (info->hash,
11586 "_GLOBAL_OFFSET_TABLE_",
11587 FALSE, FALSE, TRUE))
11588 && h->type == bfd_link_hash_defined)
11589 elf_gp (abfd) = (h->u.def.section->output_section->vma
11590 + h->u.def.section->output_offset
11591 + h->u.def.value);
1049f94e 11592 else if (info->relocatable)
b49e97c9
TS
11593 {
11594 bfd_vma lo = MINUS_ONE;
11595
11596 /* Find the GP-relative section with the lowest offset. */
9719ad41 11597 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11598 if (o->vma < lo
11599 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11600 lo = o->vma;
11601
11602 /* And calculate GP relative to that. */
0a44bf69 11603 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
11604 }
11605 else
11606 {
11607 /* If the relocate_section function needs to do a reloc
11608 involving the GP value, it should make a reloc_dangerous
11609 callback to warn that GP is not defined. */
11610 }
11611 }
11612
11613 /* Go through the sections and collect the .reginfo and .mdebug
11614 information. */
11615 reginfo_sec = NULL;
11616 mdebug_sec = NULL;
11617 gptab_data_sec = NULL;
11618 gptab_bss_sec = NULL;
9719ad41 11619 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
11620 {
11621 if (strcmp (o->name, ".reginfo") == 0)
11622 {
11623 memset (&reginfo, 0, sizeof reginfo);
11624
11625 /* We have found the .reginfo section in the output file.
11626 Look through all the link_orders comprising it and merge
11627 the information together. */
8423293d 11628 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11629 {
11630 asection *input_section;
11631 bfd *input_bfd;
11632 Elf32_External_RegInfo ext;
11633 Elf32_RegInfo sub;
11634
11635 if (p->type != bfd_indirect_link_order)
11636 {
11637 if (p->type == bfd_data_link_order)
11638 continue;
11639 abort ();
11640 }
11641
11642 input_section = p->u.indirect.section;
11643 input_bfd = input_section->owner;
11644
b49e97c9 11645 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 11646 &ext, 0, sizeof ext))
b34976b6 11647 return FALSE;
b49e97c9
TS
11648
11649 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11650
11651 reginfo.ri_gprmask |= sub.ri_gprmask;
11652 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11653 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11654 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11655 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11656
11657 /* ri_gp_value is set by the function
11658 mips_elf32_section_processing when the section is
11659 finally written out. */
11660
11661 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11662 elf_link_input_bfd ignores this section. */
11663 input_section->flags &= ~SEC_HAS_CONTENTS;
11664 }
11665
11666 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 11667 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
11668
11669 /* Skip this section later on (I don't think this currently
11670 matters, but someday it might). */
8423293d 11671 o->map_head.link_order = NULL;
b49e97c9
TS
11672
11673 reginfo_sec = o;
11674 }
11675
11676 if (strcmp (o->name, ".mdebug") == 0)
11677 {
11678 struct extsym_info einfo;
11679 bfd_vma last;
11680
11681 /* We have found the .mdebug section in the output file.
11682 Look through all the link_orders comprising it and merge
11683 the information together. */
11684 symhdr->magic = swap->sym_magic;
11685 /* FIXME: What should the version stamp be? */
11686 symhdr->vstamp = 0;
11687 symhdr->ilineMax = 0;
11688 symhdr->cbLine = 0;
11689 symhdr->idnMax = 0;
11690 symhdr->ipdMax = 0;
11691 symhdr->isymMax = 0;
11692 symhdr->ioptMax = 0;
11693 symhdr->iauxMax = 0;
11694 symhdr->issMax = 0;
11695 symhdr->issExtMax = 0;
11696 symhdr->ifdMax = 0;
11697 symhdr->crfd = 0;
11698 symhdr->iextMax = 0;
11699
11700 /* We accumulate the debugging information itself in the
11701 debug_info structure. */
11702 debug.line = NULL;
11703 debug.external_dnr = NULL;
11704 debug.external_pdr = NULL;
11705 debug.external_sym = NULL;
11706 debug.external_opt = NULL;
11707 debug.external_aux = NULL;
11708 debug.ss = NULL;
11709 debug.ssext = debug.ssext_end = NULL;
11710 debug.external_fdr = NULL;
11711 debug.external_rfd = NULL;
11712 debug.external_ext = debug.external_ext_end = NULL;
11713
11714 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 11715 if (mdebug_handle == NULL)
b34976b6 11716 return FALSE;
b49e97c9
TS
11717
11718 esym.jmptbl = 0;
11719 esym.cobol_main = 0;
11720 esym.weakext = 0;
11721 esym.reserved = 0;
11722 esym.ifd = ifdNil;
11723 esym.asym.iss = issNil;
11724 esym.asym.st = stLocal;
11725 esym.asym.reserved = 0;
11726 esym.asym.index = indexNil;
11727 last = 0;
11728 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11729 {
11730 esym.asym.sc = sc[i];
11731 s = bfd_get_section_by_name (abfd, secname[i]);
11732 if (s != NULL)
11733 {
11734 esym.asym.value = s->vma;
eea6121a 11735 last = s->vma + s->size;
b49e97c9
TS
11736 }
11737 else
11738 esym.asym.value = last;
11739 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11740 secname[i], &esym))
b34976b6 11741 return FALSE;
b49e97c9
TS
11742 }
11743
8423293d 11744 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11745 {
11746 asection *input_section;
11747 bfd *input_bfd;
11748 const struct ecoff_debug_swap *input_swap;
11749 struct ecoff_debug_info input_debug;
11750 char *eraw_src;
11751 char *eraw_end;
11752
11753 if (p->type != bfd_indirect_link_order)
11754 {
11755 if (p->type == bfd_data_link_order)
11756 continue;
11757 abort ();
11758 }
11759
11760 input_section = p->u.indirect.section;
11761 input_bfd = input_section->owner;
11762
d5eaccd7 11763 if (!is_mips_elf (input_bfd))
b49e97c9
TS
11764 {
11765 /* I don't know what a non MIPS ELF bfd would be
11766 doing with a .mdebug section, but I don't really
11767 want to deal with it. */
11768 continue;
11769 }
11770
11771 input_swap = (get_elf_backend_data (input_bfd)
11772 ->elf_backend_ecoff_debug_swap);
11773
eea6121a 11774 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
11775
11776 /* The ECOFF linking code expects that we have already
11777 read in the debugging information and set up an
11778 ecoff_debug_info structure, so we do that now. */
11779 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11780 &input_debug))
b34976b6 11781 return FALSE;
b49e97c9
TS
11782
11783 if (! (bfd_ecoff_debug_accumulate
11784 (mdebug_handle, abfd, &debug, swap, input_bfd,
11785 &input_debug, input_swap, info)))
b34976b6 11786 return FALSE;
b49e97c9
TS
11787
11788 /* Loop through the external symbols. For each one with
11789 interesting information, try to find the symbol in
11790 the linker global hash table and save the information
11791 for the output external symbols. */
11792 eraw_src = input_debug.external_ext;
11793 eraw_end = (eraw_src
11794 + (input_debug.symbolic_header.iextMax
11795 * input_swap->external_ext_size));
11796 for (;
11797 eraw_src < eraw_end;
11798 eraw_src += input_swap->external_ext_size)
11799 {
11800 EXTR ext;
11801 const char *name;
11802 struct mips_elf_link_hash_entry *h;
11803
9719ad41 11804 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
11805 if (ext.asym.sc == scNil
11806 || ext.asym.sc == scUndefined
11807 || ext.asym.sc == scSUndefined)
11808 continue;
11809
11810 name = input_debug.ssext + ext.asym.iss;
11811 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 11812 name, FALSE, FALSE, TRUE);
b49e97c9
TS
11813 if (h == NULL || h->esym.ifd != -2)
11814 continue;
11815
11816 if (ext.ifd != -1)
11817 {
11818 BFD_ASSERT (ext.ifd
11819 < input_debug.symbolic_header.ifdMax);
11820 ext.ifd = input_debug.ifdmap[ext.ifd];
11821 }
11822
11823 h->esym = ext;
11824 }
11825
11826 /* Free up the information we just read. */
11827 free (input_debug.line);
11828 free (input_debug.external_dnr);
11829 free (input_debug.external_pdr);
11830 free (input_debug.external_sym);
11831 free (input_debug.external_opt);
11832 free (input_debug.external_aux);
11833 free (input_debug.ss);
11834 free (input_debug.ssext);
11835 free (input_debug.external_fdr);
11836 free (input_debug.external_rfd);
11837 free (input_debug.external_ext);
11838
11839 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11840 elf_link_input_bfd ignores this section. */
11841 input_section->flags &= ~SEC_HAS_CONTENTS;
11842 }
11843
11844 if (SGI_COMPAT (abfd) && info->shared)
11845 {
11846 /* Create .rtproc section. */
11847 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11848 if (rtproc_sec == NULL)
11849 {
11850 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11851 | SEC_LINKER_CREATED | SEC_READONLY);
11852
3496cb2a
L
11853 rtproc_sec = bfd_make_section_with_flags (abfd,
11854 ".rtproc",
11855 flags);
b49e97c9 11856 if (rtproc_sec == NULL
b49e97c9 11857 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 11858 return FALSE;
b49e97c9
TS
11859 }
11860
11861 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11862 info, rtproc_sec,
11863 &debug))
b34976b6 11864 return FALSE;
b49e97c9
TS
11865 }
11866
11867 /* Build the external symbol information. */
11868 einfo.abfd = abfd;
11869 einfo.info = info;
11870 einfo.debug = &debug;
11871 einfo.swap = swap;
b34976b6 11872 einfo.failed = FALSE;
b49e97c9 11873 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 11874 mips_elf_output_extsym, &einfo);
b49e97c9 11875 if (einfo.failed)
b34976b6 11876 return FALSE;
b49e97c9
TS
11877
11878 /* Set the size of the .mdebug section. */
eea6121a 11879 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
11880
11881 /* Skip this section later on (I don't think this currently
11882 matters, but someday it might). */
8423293d 11883 o->map_head.link_order = NULL;
b49e97c9
TS
11884
11885 mdebug_sec = o;
11886 }
11887
0112cd26 11888 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
11889 {
11890 const char *subname;
11891 unsigned int c;
11892 Elf32_gptab *tab;
11893 Elf32_External_gptab *ext_tab;
11894 unsigned int j;
11895
11896 /* The .gptab.sdata and .gptab.sbss sections hold
11897 information describing how the small data area would
11898 change depending upon the -G switch. These sections
11899 not used in executables files. */
1049f94e 11900 if (! info->relocatable)
b49e97c9 11901 {
8423293d 11902 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11903 {
11904 asection *input_section;
11905
11906 if (p->type != bfd_indirect_link_order)
11907 {
11908 if (p->type == bfd_data_link_order)
11909 continue;
11910 abort ();
11911 }
11912
11913 input_section = p->u.indirect.section;
11914
11915 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11916 elf_link_input_bfd ignores this section. */
11917 input_section->flags &= ~SEC_HAS_CONTENTS;
11918 }
11919
11920 /* Skip this section later on (I don't think this
11921 currently matters, but someday it might). */
8423293d 11922 o->map_head.link_order = NULL;
b49e97c9
TS
11923
11924 /* Really remove the section. */
5daa8fe7 11925 bfd_section_list_remove (abfd, o);
b49e97c9
TS
11926 --abfd->section_count;
11927
11928 continue;
11929 }
11930
11931 /* There is one gptab for initialized data, and one for
11932 uninitialized data. */
11933 if (strcmp (o->name, ".gptab.sdata") == 0)
11934 gptab_data_sec = o;
11935 else if (strcmp (o->name, ".gptab.sbss") == 0)
11936 gptab_bss_sec = o;
11937 else
11938 {
11939 (*_bfd_error_handler)
11940 (_("%s: illegal section name `%s'"),
11941 bfd_get_filename (abfd), o->name);
11942 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 11943 return FALSE;
b49e97c9
TS
11944 }
11945
11946 /* The linker script always combines .gptab.data and
11947 .gptab.sdata into .gptab.sdata, and likewise for
11948 .gptab.bss and .gptab.sbss. It is possible that there is
11949 no .sdata or .sbss section in the output file, in which
11950 case we must change the name of the output section. */
11951 subname = o->name + sizeof ".gptab" - 1;
11952 if (bfd_get_section_by_name (abfd, subname) == NULL)
11953 {
11954 if (o == gptab_data_sec)
11955 o->name = ".gptab.data";
11956 else
11957 o->name = ".gptab.bss";
11958 subname = o->name + sizeof ".gptab" - 1;
11959 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11960 }
11961
11962 /* Set up the first entry. */
11963 c = 1;
11964 amt = c * sizeof (Elf32_gptab);
9719ad41 11965 tab = bfd_malloc (amt);
b49e97c9 11966 if (tab == NULL)
b34976b6 11967 return FALSE;
b49e97c9
TS
11968 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11969 tab[0].gt_header.gt_unused = 0;
11970
11971 /* Combine the input sections. */
8423293d 11972 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
11973 {
11974 asection *input_section;
11975 bfd *input_bfd;
11976 bfd_size_type size;
11977 unsigned long last;
11978 bfd_size_type gpentry;
11979
11980 if (p->type != bfd_indirect_link_order)
11981 {
11982 if (p->type == bfd_data_link_order)
11983 continue;
11984 abort ();
11985 }
11986
11987 input_section = p->u.indirect.section;
11988 input_bfd = input_section->owner;
11989
11990 /* Combine the gptab entries for this input section one
11991 by one. We know that the input gptab entries are
11992 sorted by ascending -G value. */
eea6121a 11993 size = input_section->size;
b49e97c9
TS
11994 last = 0;
11995 for (gpentry = sizeof (Elf32_External_gptab);
11996 gpentry < size;
11997 gpentry += sizeof (Elf32_External_gptab))
11998 {
11999 Elf32_External_gptab ext_gptab;
12000 Elf32_gptab int_gptab;
12001 unsigned long val;
12002 unsigned long add;
b34976b6 12003 bfd_boolean exact;
b49e97c9
TS
12004 unsigned int look;
12005
12006 if (! (bfd_get_section_contents
9719ad41
RS
12007 (input_bfd, input_section, &ext_gptab, gpentry,
12008 sizeof (Elf32_External_gptab))))
b49e97c9
TS
12009 {
12010 free (tab);
b34976b6 12011 return FALSE;
b49e97c9
TS
12012 }
12013
12014 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12015 &int_gptab);
12016 val = int_gptab.gt_entry.gt_g_value;
12017 add = int_gptab.gt_entry.gt_bytes - last;
12018
b34976b6 12019 exact = FALSE;
b49e97c9
TS
12020 for (look = 1; look < c; look++)
12021 {
12022 if (tab[look].gt_entry.gt_g_value >= val)
12023 tab[look].gt_entry.gt_bytes += add;
12024
12025 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 12026 exact = TRUE;
b49e97c9
TS
12027 }
12028
12029 if (! exact)
12030 {
12031 Elf32_gptab *new_tab;
12032 unsigned int max;
12033
12034 /* We need a new table entry. */
12035 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 12036 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
12037 if (new_tab == NULL)
12038 {
12039 free (tab);
b34976b6 12040 return FALSE;
b49e97c9
TS
12041 }
12042 tab = new_tab;
12043 tab[c].gt_entry.gt_g_value = val;
12044 tab[c].gt_entry.gt_bytes = add;
12045
12046 /* Merge in the size for the next smallest -G
12047 value, since that will be implied by this new
12048 value. */
12049 max = 0;
12050 for (look = 1; look < c; look++)
12051 {
12052 if (tab[look].gt_entry.gt_g_value < val
12053 && (max == 0
12054 || (tab[look].gt_entry.gt_g_value
12055 > tab[max].gt_entry.gt_g_value)))
12056 max = look;
12057 }
12058 if (max != 0)
12059 tab[c].gt_entry.gt_bytes +=
12060 tab[max].gt_entry.gt_bytes;
12061
12062 ++c;
12063 }
12064
12065 last = int_gptab.gt_entry.gt_bytes;
12066 }
12067
12068 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12069 elf_link_input_bfd ignores this section. */
12070 input_section->flags &= ~SEC_HAS_CONTENTS;
12071 }
12072
12073 /* The table must be sorted by -G value. */
12074 if (c > 2)
12075 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12076
12077 /* Swap out the table. */
12078 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 12079 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
12080 if (ext_tab == NULL)
12081 {
12082 free (tab);
b34976b6 12083 return FALSE;
b49e97c9
TS
12084 }
12085
12086 for (j = 0; j < c; j++)
12087 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12088 free (tab);
12089
eea6121a 12090 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
12091 o->contents = (bfd_byte *) ext_tab;
12092
12093 /* Skip this section later on (I don't think this currently
12094 matters, but someday it might). */
8423293d 12095 o->map_head.link_order = NULL;
b49e97c9
TS
12096 }
12097 }
12098
12099 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 12100 if (!bfd_elf_final_link (abfd, info))
b34976b6 12101 return FALSE;
b49e97c9
TS
12102
12103 /* Now write out the computed sections. */
12104
9719ad41 12105 if (reginfo_sec != NULL)
b49e97c9
TS
12106 {
12107 Elf32_External_RegInfo ext;
12108
12109 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 12110 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 12111 return FALSE;
b49e97c9
TS
12112 }
12113
9719ad41 12114 if (mdebug_sec != NULL)
b49e97c9
TS
12115 {
12116 BFD_ASSERT (abfd->output_has_begun);
12117 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12118 swap, info,
12119 mdebug_sec->filepos))
b34976b6 12120 return FALSE;
b49e97c9
TS
12121
12122 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12123 }
12124
9719ad41 12125 if (gptab_data_sec != NULL)
b49e97c9
TS
12126 {
12127 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12128 gptab_data_sec->contents,
eea6121a 12129 0, gptab_data_sec->size))
b34976b6 12130 return FALSE;
b49e97c9
TS
12131 }
12132
9719ad41 12133 if (gptab_bss_sec != NULL)
b49e97c9
TS
12134 {
12135 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12136 gptab_bss_sec->contents,
eea6121a 12137 0, gptab_bss_sec->size))
b34976b6 12138 return FALSE;
b49e97c9
TS
12139 }
12140
12141 if (SGI_COMPAT (abfd))
12142 {
12143 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12144 if (rtproc_sec != NULL)
12145 {
12146 if (! bfd_set_section_contents (abfd, rtproc_sec,
12147 rtproc_sec->contents,
eea6121a 12148 0, rtproc_sec->size))
b34976b6 12149 return FALSE;
b49e97c9
TS
12150 }
12151 }
12152
b34976b6 12153 return TRUE;
b49e97c9
TS
12154}
12155\f
64543e1a
RS
12156/* Structure for saying that BFD machine EXTENSION extends BASE. */
12157
12158struct mips_mach_extension {
12159 unsigned long extension, base;
12160};
12161
12162
12163/* An array describing how BFD machines relate to one another. The entries
12164 are ordered topologically with MIPS I extensions listed last. */
12165
12166static const struct mips_mach_extension mips_mach_extensions[] = {
6f179bd0
AN
12167 /* MIPS64r2 extensions. */
12168 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12169
64543e1a 12170 /* MIPS64 extensions. */
5f74bc13 12171 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a 12172 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
52b6b6b9 12173 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
64543e1a
RS
12174
12175 /* MIPS V extensions. */
12176 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12177
12178 /* R10000 extensions. */
12179 { bfd_mach_mips12000, bfd_mach_mips10000 },
3aa3176b
TS
12180 { bfd_mach_mips14000, bfd_mach_mips10000 },
12181 { bfd_mach_mips16000, bfd_mach_mips10000 },
64543e1a
RS
12182
12183 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12184 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12185 better to allow vr5400 and vr5500 code to be merged anyway, since
12186 many libraries will just use the core ISA. Perhaps we could add
12187 some sort of ASE flag if this ever proves a problem. */
12188 { bfd_mach_mips5500, bfd_mach_mips5400 },
12189 { bfd_mach_mips5400, bfd_mach_mips5000 },
12190
12191 /* MIPS IV extensions. */
12192 { bfd_mach_mips5, bfd_mach_mips8000 },
12193 { bfd_mach_mips10000, bfd_mach_mips8000 },
12194 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 12195 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 12196 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
12197
12198 /* VR4100 extensions. */
12199 { bfd_mach_mips4120, bfd_mach_mips4100 },
12200 { bfd_mach_mips4111, bfd_mach_mips4100 },
12201
12202 /* MIPS III extensions. */
350cc38d
MS
12203 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12204 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
64543e1a
RS
12205 { bfd_mach_mips8000, bfd_mach_mips4000 },
12206 { bfd_mach_mips4650, bfd_mach_mips4000 },
12207 { bfd_mach_mips4600, bfd_mach_mips4000 },
12208 { bfd_mach_mips4400, bfd_mach_mips4000 },
12209 { bfd_mach_mips4300, bfd_mach_mips4000 },
12210 { bfd_mach_mips4100, bfd_mach_mips4000 },
12211 { bfd_mach_mips4010, bfd_mach_mips4000 },
12212
12213 /* MIPS32 extensions. */
12214 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12215
12216 /* MIPS II extensions. */
12217 { bfd_mach_mips4000, bfd_mach_mips6000 },
12218 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12219
12220 /* MIPS I extensions. */
12221 { bfd_mach_mips6000, bfd_mach_mips3000 },
12222 { bfd_mach_mips3900, bfd_mach_mips3000 }
12223};
12224
12225
12226/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12227
12228static bfd_boolean
9719ad41 12229mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
12230{
12231 size_t i;
12232
c5211a54
RS
12233 if (extension == base)
12234 return TRUE;
12235
12236 if (base == bfd_mach_mipsisa32
12237 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12238 return TRUE;
12239
12240 if (base == bfd_mach_mipsisa32r2
12241 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12242 return TRUE;
12243
12244 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 12245 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
12246 {
12247 extension = mips_mach_extensions[i].base;
12248 if (extension == base)
12249 return TRUE;
12250 }
64543e1a 12251
c5211a54 12252 return FALSE;
64543e1a
RS
12253}
12254
12255
12256/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 12257
b34976b6 12258static bfd_boolean
9719ad41 12259mips_32bit_flags_p (flagword flags)
00707a0e 12260{
64543e1a
RS
12261 return ((flags & EF_MIPS_32BITMODE) != 0
12262 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12263 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12264 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12265 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12266 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12267 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
12268}
12269
64543e1a 12270
2cf19d5c
JM
12271/* Merge object attributes from IBFD into OBFD. Raise an error if
12272 there are conflicting attributes. */
12273static bfd_boolean
12274mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12275{
12276 obj_attribute *in_attr;
12277 obj_attribute *out_attr;
12278
12279 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12280 {
12281 /* This is the first object. Copy the attributes. */
12282 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12283
12284 /* Use the Tag_null value to indicate the attributes have been
12285 initialized. */
12286 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12287
12288 return TRUE;
12289 }
12290
12291 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12292 non-conflicting ones. */
12293 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12294 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12295 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12296 {
12297 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12298 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12299 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12300 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12301 ;
42554f6a 12302 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12303 _bfd_error_handler
12304 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12305 in_attr[Tag_GNU_MIPS_ABI_FP].i);
42554f6a 12306 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
2cf19d5c
JM
12307 _bfd_error_handler
12308 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12309 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12310 else
12311 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12312 {
12313 case 1:
12314 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12315 {
12316 case 2:
12317 _bfd_error_handler
12318 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12319 obfd, ibfd);
51a0dd31 12320 break;
2cf19d5c
JM
12321
12322 case 3:
12323 _bfd_error_handler
12324 (_("Warning: %B uses hard float, %B uses soft float"),
12325 obfd, ibfd);
12326 break;
12327
42554f6a
TS
12328 case 4:
12329 _bfd_error_handler
12330 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12331 obfd, ibfd);
12332 break;
12333
2cf19d5c
JM
12334 default:
12335 abort ();
12336 }
12337 break;
12338
12339 case 2:
12340 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12341 {
12342 case 1:
12343 _bfd_error_handler
12344 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12345 ibfd, obfd);
51a0dd31 12346 break;
2cf19d5c
JM
12347
12348 case 3:
12349 _bfd_error_handler
12350 (_("Warning: %B uses hard float, %B uses soft float"),
12351 obfd, ibfd);
12352 break;
12353
42554f6a
TS
12354 case 4:
12355 _bfd_error_handler
12356 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12357 obfd, ibfd);
12358 break;
12359
2cf19d5c
JM
12360 default:
12361 abort ();
12362 }
12363 break;
12364
12365 case 3:
12366 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12367 {
12368 case 1:
12369 case 2:
42554f6a 12370 case 4:
2cf19d5c
JM
12371 _bfd_error_handler
12372 (_("Warning: %B uses hard float, %B uses soft float"),
12373 ibfd, obfd);
12374 break;
12375
12376 default:
12377 abort ();
12378 }
12379 break;
12380
42554f6a
TS
12381 case 4:
12382 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12383 {
12384 case 1:
12385 _bfd_error_handler
12386 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12387 ibfd, obfd);
12388 break;
12389
12390 case 2:
12391 _bfd_error_handler
12392 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12393 ibfd, obfd);
12394 break;
12395
12396 case 3:
12397 _bfd_error_handler
12398 (_("Warning: %B uses hard float, %B uses soft float"),
12399 obfd, ibfd);
12400 break;
12401
12402 default:
12403 abort ();
12404 }
12405 break;
12406
2cf19d5c
JM
12407 default:
12408 abort ();
12409 }
12410 }
12411
12412 /* Merge Tag_compatibility attributes and any common GNU ones. */
12413 _bfd_elf_merge_object_attributes (ibfd, obfd);
12414
12415 return TRUE;
12416}
12417
b49e97c9
TS
12418/* Merge backend specific data from an object file to the output
12419 object file when linking. */
12420
b34976b6 12421bfd_boolean
9719ad41 12422_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
12423{
12424 flagword old_flags;
12425 flagword new_flags;
b34976b6
AM
12426 bfd_boolean ok;
12427 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
12428 asection *sec;
12429
12430 /* Check if we have the same endianess */
82e51918 12431 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
12432 {
12433 (*_bfd_error_handler)
d003868e
AM
12434 (_("%B: endianness incompatible with that of the selected emulation"),
12435 ibfd);
aa701218
AO
12436 return FALSE;
12437 }
b49e97c9 12438
d5eaccd7 12439 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 12440 return TRUE;
b49e97c9 12441
aa701218
AO
12442 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12443 {
12444 (*_bfd_error_handler)
d003868e
AM
12445 (_("%B: ABI is incompatible with that of the selected emulation"),
12446 ibfd);
aa701218
AO
12447 return FALSE;
12448 }
12449
2cf19d5c
JM
12450 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12451 return FALSE;
12452
b49e97c9
TS
12453 new_flags = elf_elfheader (ibfd)->e_flags;
12454 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12455 old_flags = elf_elfheader (obfd)->e_flags;
12456
12457 if (! elf_flags_init (obfd))
12458 {
b34976b6 12459 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
12460 elf_elfheader (obfd)->e_flags = new_flags;
12461 elf_elfheader (obfd)->e_ident[EI_CLASS]
12462 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12463
12464 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861
TS
12465 && (bfd_get_arch_info (obfd)->the_default
12466 || mips_mach_extends_p (bfd_get_mach (obfd),
12467 bfd_get_mach (ibfd))))
b49e97c9
TS
12468 {
12469 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12470 bfd_get_mach (ibfd)))
b34976b6 12471 return FALSE;
b49e97c9
TS
12472 }
12473
b34976b6 12474 return TRUE;
b49e97c9
TS
12475 }
12476
12477 /* Check flag compatibility. */
12478
12479 new_flags &= ~EF_MIPS_NOREORDER;
12480 old_flags &= ~EF_MIPS_NOREORDER;
12481
f4416af6
AO
12482 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12483 doesn't seem to matter. */
12484 new_flags &= ~EF_MIPS_XGOT;
12485 old_flags &= ~EF_MIPS_XGOT;
12486
98a8deaf
RS
12487 /* MIPSpro generates ucode info in n64 objects. Again, we should
12488 just be able to ignore this. */
12489 new_flags &= ~EF_MIPS_UCODE;
12490 old_flags &= ~EF_MIPS_UCODE;
12491
861fb55a
DJ
12492 /* DSOs should only be linked with CPIC code. */
12493 if ((ibfd->flags & DYNAMIC) != 0)
12494 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
0a44bf69 12495
b49e97c9 12496 if (new_flags == old_flags)
b34976b6 12497 return TRUE;
b49e97c9
TS
12498
12499 /* Check to see if the input BFD actually contains any sections.
12500 If not, its flags may not have been initialised either, but it cannot
12501 actually cause any incompatibility. */
12502 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12503 {
12504 /* Ignore synthetic sections and empty .text, .data and .bss sections
12505 which are automatically generated by gas. */
12506 if (strcmp (sec->name, ".reginfo")
12507 && strcmp (sec->name, ".mdebug")
eea6121a 12508 && (sec->size != 0
d13d89fa
NS
12509 || (strcmp (sec->name, ".text")
12510 && strcmp (sec->name, ".data")
12511 && strcmp (sec->name, ".bss"))))
b49e97c9 12512 {
b34976b6 12513 null_input_bfd = FALSE;
b49e97c9
TS
12514 break;
12515 }
12516 }
12517 if (null_input_bfd)
b34976b6 12518 return TRUE;
b49e97c9 12519
b34976b6 12520 ok = TRUE;
b49e97c9 12521
143d77c5
EC
12522 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12523 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 12524 {
b49e97c9 12525 (*_bfd_error_handler)
861fb55a 12526 (_("%B: warning: linking abicalls files with non-abicalls files"),
d003868e 12527 ibfd);
143d77c5 12528 ok = TRUE;
b49e97c9
TS
12529 }
12530
143d77c5
EC
12531 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12532 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12533 if (! (new_flags & EF_MIPS_PIC))
12534 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12535
12536 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12537 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 12538
64543e1a
RS
12539 /* Compare the ISAs. */
12540 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 12541 {
64543e1a 12542 (*_bfd_error_handler)
d003868e
AM
12543 (_("%B: linking 32-bit code with 64-bit code"),
12544 ibfd);
64543e1a
RS
12545 ok = FALSE;
12546 }
12547 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12548 {
12549 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12550 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 12551 {
64543e1a
RS
12552 /* Copy the architecture info from IBFD to OBFD. Also copy
12553 the 32-bit flag (if set) so that we continue to recognise
12554 OBFD as a 32-bit binary. */
12555 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12556 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12557 elf_elfheader (obfd)->e_flags
12558 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12559
12560 /* Copy across the ABI flags if OBFD doesn't use them
12561 and if that was what caused us to treat IBFD as 32-bit. */
12562 if ((old_flags & EF_MIPS_ABI) == 0
12563 && mips_32bit_flags_p (new_flags)
12564 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12565 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
12566 }
12567 else
12568 {
64543e1a 12569 /* The ISAs aren't compatible. */
b49e97c9 12570 (*_bfd_error_handler)
d003868e
AM
12571 (_("%B: linking %s module with previous %s modules"),
12572 ibfd,
64543e1a
RS
12573 bfd_printable_name (ibfd),
12574 bfd_printable_name (obfd));
b34976b6 12575 ok = FALSE;
b49e97c9 12576 }
b49e97c9
TS
12577 }
12578
64543e1a
RS
12579 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12580 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12581
12582 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
12583 does set EI_CLASS differently from any 32-bit ABI. */
12584 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12585 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12586 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12587 {
12588 /* Only error if both are set (to different values). */
12589 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12590 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12591 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12592 {
12593 (*_bfd_error_handler)
d003868e
AM
12594 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12595 ibfd,
b49e97c9
TS
12596 elf_mips_abi_name (ibfd),
12597 elf_mips_abi_name (obfd));
b34976b6 12598 ok = FALSE;
b49e97c9
TS
12599 }
12600 new_flags &= ~EF_MIPS_ABI;
12601 old_flags &= ~EF_MIPS_ABI;
12602 }
12603
fb39dac1
RS
12604 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12605 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12606 {
12607 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12608
12609 new_flags &= ~ EF_MIPS_ARCH_ASE;
12610 old_flags &= ~ EF_MIPS_ARCH_ASE;
12611 }
12612
b49e97c9
TS
12613 /* Warn about any other mismatches */
12614 if (new_flags != old_flags)
12615 {
12616 (*_bfd_error_handler)
d003868e
AM
12617 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12618 ibfd, (unsigned long) new_flags,
b49e97c9 12619 (unsigned long) old_flags);
b34976b6 12620 ok = FALSE;
b49e97c9
TS
12621 }
12622
12623 if (! ok)
12624 {
12625 bfd_set_error (bfd_error_bad_value);
b34976b6 12626 return FALSE;
b49e97c9
TS
12627 }
12628
b34976b6 12629 return TRUE;
b49e97c9
TS
12630}
12631
12632/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12633
b34976b6 12634bfd_boolean
9719ad41 12635_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
12636{
12637 BFD_ASSERT (!elf_flags_init (abfd)
12638 || elf_elfheader (abfd)->e_flags == flags);
12639
12640 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
12641 elf_flags_init (abfd) = TRUE;
12642 return TRUE;
b49e97c9
TS
12643}
12644
ad9563d6
CM
12645char *
12646_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12647{
12648 switch (dtag)
12649 {
12650 default: return "";
12651 case DT_MIPS_RLD_VERSION:
12652 return "MIPS_RLD_VERSION";
12653 case DT_MIPS_TIME_STAMP:
12654 return "MIPS_TIME_STAMP";
12655 case DT_MIPS_ICHECKSUM:
12656 return "MIPS_ICHECKSUM";
12657 case DT_MIPS_IVERSION:
12658 return "MIPS_IVERSION";
12659 case DT_MIPS_FLAGS:
12660 return "MIPS_FLAGS";
12661 case DT_MIPS_BASE_ADDRESS:
12662 return "MIPS_BASE_ADDRESS";
12663 case DT_MIPS_MSYM:
12664 return "MIPS_MSYM";
12665 case DT_MIPS_CONFLICT:
12666 return "MIPS_CONFLICT";
12667 case DT_MIPS_LIBLIST:
12668 return "MIPS_LIBLIST";
12669 case DT_MIPS_LOCAL_GOTNO:
12670 return "MIPS_LOCAL_GOTNO";
12671 case DT_MIPS_CONFLICTNO:
12672 return "MIPS_CONFLICTNO";
12673 case DT_MIPS_LIBLISTNO:
12674 return "MIPS_LIBLISTNO";
12675 case DT_MIPS_SYMTABNO:
12676 return "MIPS_SYMTABNO";
12677 case DT_MIPS_UNREFEXTNO:
12678 return "MIPS_UNREFEXTNO";
12679 case DT_MIPS_GOTSYM:
12680 return "MIPS_GOTSYM";
12681 case DT_MIPS_HIPAGENO:
12682 return "MIPS_HIPAGENO";
12683 case DT_MIPS_RLD_MAP:
12684 return "MIPS_RLD_MAP";
12685 case DT_MIPS_DELTA_CLASS:
12686 return "MIPS_DELTA_CLASS";
12687 case DT_MIPS_DELTA_CLASS_NO:
12688 return "MIPS_DELTA_CLASS_NO";
12689 case DT_MIPS_DELTA_INSTANCE:
12690 return "MIPS_DELTA_INSTANCE";
12691 case DT_MIPS_DELTA_INSTANCE_NO:
12692 return "MIPS_DELTA_INSTANCE_NO";
12693 case DT_MIPS_DELTA_RELOC:
12694 return "MIPS_DELTA_RELOC";
12695 case DT_MIPS_DELTA_RELOC_NO:
12696 return "MIPS_DELTA_RELOC_NO";
12697 case DT_MIPS_DELTA_SYM:
12698 return "MIPS_DELTA_SYM";
12699 case DT_MIPS_DELTA_SYM_NO:
12700 return "MIPS_DELTA_SYM_NO";
12701 case DT_MIPS_DELTA_CLASSSYM:
12702 return "MIPS_DELTA_CLASSSYM";
12703 case DT_MIPS_DELTA_CLASSSYM_NO:
12704 return "MIPS_DELTA_CLASSSYM_NO";
12705 case DT_MIPS_CXX_FLAGS:
12706 return "MIPS_CXX_FLAGS";
12707 case DT_MIPS_PIXIE_INIT:
12708 return "MIPS_PIXIE_INIT";
12709 case DT_MIPS_SYMBOL_LIB:
12710 return "MIPS_SYMBOL_LIB";
12711 case DT_MIPS_LOCALPAGE_GOTIDX:
12712 return "MIPS_LOCALPAGE_GOTIDX";
12713 case DT_MIPS_LOCAL_GOTIDX:
12714 return "MIPS_LOCAL_GOTIDX";
12715 case DT_MIPS_HIDDEN_GOTIDX:
12716 return "MIPS_HIDDEN_GOTIDX";
12717 case DT_MIPS_PROTECTED_GOTIDX:
12718 return "MIPS_PROTECTED_GOT_IDX";
12719 case DT_MIPS_OPTIONS:
12720 return "MIPS_OPTIONS";
12721 case DT_MIPS_INTERFACE:
12722 return "MIPS_INTERFACE";
12723 case DT_MIPS_DYNSTR_ALIGN:
12724 return "DT_MIPS_DYNSTR_ALIGN";
12725 case DT_MIPS_INTERFACE_SIZE:
12726 return "DT_MIPS_INTERFACE_SIZE";
12727 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12728 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12729 case DT_MIPS_PERF_SUFFIX:
12730 return "DT_MIPS_PERF_SUFFIX";
12731 case DT_MIPS_COMPACT_SIZE:
12732 return "DT_MIPS_COMPACT_SIZE";
12733 case DT_MIPS_GP_VALUE:
12734 return "DT_MIPS_GP_VALUE";
12735 case DT_MIPS_AUX_DYNAMIC:
12736 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
12737 case DT_MIPS_PLTGOT:
12738 return "DT_MIPS_PLTGOT";
12739 case DT_MIPS_RWPLT:
12740 return "DT_MIPS_RWPLT";
ad9563d6
CM
12741 }
12742}
12743
b34976b6 12744bfd_boolean
9719ad41 12745_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 12746{
9719ad41 12747 FILE *file = ptr;
b49e97c9
TS
12748
12749 BFD_ASSERT (abfd != NULL && ptr != NULL);
12750
12751 /* Print normal ELF private data. */
12752 _bfd_elf_print_private_bfd_data (abfd, ptr);
12753
12754 /* xgettext:c-format */
12755 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12756
12757 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12758 fprintf (file, _(" [abi=O32]"));
12759 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12760 fprintf (file, _(" [abi=O64]"));
12761 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12762 fprintf (file, _(" [abi=EABI32]"));
12763 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12764 fprintf (file, _(" [abi=EABI64]"));
12765 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12766 fprintf (file, _(" [abi unknown]"));
12767 else if (ABI_N32_P (abfd))
12768 fprintf (file, _(" [abi=N32]"));
12769 else if (ABI_64_P (abfd))
12770 fprintf (file, _(" [abi=64]"));
12771 else
12772 fprintf (file, _(" [no abi set]"));
12773
12774 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 12775 fprintf (file, " [mips1]");
b49e97c9 12776 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 12777 fprintf (file, " [mips2]");
b49e97c9 12778 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 12779 fprintf (file, " [mips3]");
b49e97c9 12780 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 12781 fprintf (file, " [mips4]");
b49e97c9 12782 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 12783 fprintf (file, " [mips5]");
b49e97c9 12784 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 12785 fprintf (file, " [mips32]");
b49e97c9 12786 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 12787 fprintf (file, " [mips64]");
af7ee8bf 12788 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 12789 fprintf (file, " [mips32r2]");
5f74bc13 12790 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 12791 fprintf (file, " [mips64r2]");
b49e97c9
TS
12792 else
12793 fprintf (file, _(" [unknown ISA]"));
12794
40d32fc6 12795 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 12796 fprintf (file, " [mdmx]");
40d32fc6
CD
12797
12798 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 12799 fprintf (file, " [mips16]");
40d32fc6 12800
b49e97c9 12801 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 12802 fprintf (file, " [32bitmode]");
b49e97c9
TS
12803 else
12804 fprintf (file, _(" [not 32bitmode]"));
12805
c0e3f241 12806 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 12807 fprintf (file, " [noreorder]");
c0e3f241
CD
12808
12809 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 12810 fprintf (file, " [PIC]");
c0e3f241
CD
12811
12812 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 12813 fprintf (file, " [CPIC]");
c0e3f241
CD
12814
12815 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 12816 fprintf (file, " [XGOT]");
c0e3f241
CD
12817
12818 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 12819 fprintf (file, " [UCODE]");
c0e3f241 12820
b49e97c9
TS
12821 fputc ('\n', file);
12822
b34976b6 12823 return TRUE;
b49e97c9 12824}
2f89ff8d 12825
b35d266b 12826const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 12827{
0112cd26
NC
12828 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12829 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12830 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12831 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12832 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12833 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12834 { NULL, 0, 0, 0, 0 }
2f89ff8d 12835};
5e2b0d47 12836
8992f0d7
TS
12837/* Merge non visibility st_other attributes. Ensure that the
12838 STO_OPTIONAL flag is copied into h->other, even if this is not a
12839 definiton of the symbol. */
5e2b0d47
NC
12840void
12841_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12842 const Elf_Internal_Sym *isym,
12843 bfd_boolean definition,
12844 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12845{
8992f0d7
TS
12846 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12847 {
12848 unsigned char other;
12849
12850 other = (definition ? isym->st_other : h->other);
12851 other &= ~ELF_ST_VISIBILITY (-1);
12852 h->other = other | ELF_ST_VISIBILITY (h->other);
12853 }
12854
12855 if (!definition
5e2b0d47
NC
12856 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12857 h->other |= STO_OPTIONAL;
12858}
12ac1cf5
NC
12859
12860/* Decide whether an undefined symbol is special and can be ignored.
12861 This is the case for OPTIONAL symbols on IRIX. */
12862bfd_boolean
12863_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12864{
12865 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12866}
e0764319
NC
12867
12868bfd_boolean
12869_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12870{
12871 return (sym->st_shndx == SHN_COMMON
12872 || sym->st_shndx == SHN_MIPS_ACOMMON
12873 || sym->st_shndx == SHN_MIPS_SCOMMON);
12874}
861fb55a
DJ
12875
12876/* Return address for Ith PLT stub in section PLT, for relocation REL
12877 or (bfd_vma) -1 if it should not be included. */
12878
12879bfd_vma
12880_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12881 const arelent *rel ATTRIBUTE_UNUSED)
12882{
12883 return (plt->vma
12884 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12885 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12886}
12887
12888void
12889_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12890{
12891 struct mips_elf_link_hash_table *htab;
12892 Elf_Internal_Ehdr *i_ehdrp;
12893
12894 i_ehdrp = elf_elfheader (abfd);
12895 if (link_info)
12896 {
12897 htab = mips_elf_hash_table (link_info);
12898 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12899 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
12900 }
12901}