bfd:
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5
6 Most of the information added by Ian Lance Taylor, Cygnus Support,
7 <ian@cygnus.com>.
8 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
9 <mark@codesourcery.com>
10 Traditional MIPS targets support added by Koundinya.K, Dansk Data
11 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
12
13 This file is part of BFD, the Binary File Descriptor library.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
28 MA 02110-1301, USA. */
29
30
31 /* This file handles functionality common to the different MIPS ABI's. */
32
33 #include "sysdep.h"
34 #include "bfd.h"
35 #include "libbfd.h"
36 #include "libiberty.h"
37 #include "elf-bfd.h"
38 #include "elfxx-mips.h"
39 #include "elf/mips.h"
40 #include "elf-vxworks.h"
41
42 /* Get the ECOFF swapping routines. */
43 #include "coff/sym.h"
44 #include "coff/symconst.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
47
48 #include "hashtab.h"
49
50 /* This structure is used to hold information about one GOT entry.
51 There are three types of entry:
52
53 (1) absolute addresses
54 (abfd == NULL)
55 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
56 (abfd != NULL, symndx >= 0)
57 (3) SYMBOL addresses, where SYMBOL is not local to an input bfd
58 (abfd != NULL, symndx == -1)
59
60 Type (3) entries are treated differently for different types of GOT.
61 In the "master" GOT -- i.e. the one that describes every GOT
62 reference needed in the link -- the mips_got_entry is keyed on both
63 the symbol and the input bfd that references it. If it turns out
64 that we need multiple GOTs, we can then use this information to
65 create separate GOTs for each input bfd.
66
67 However, we want each of these separate GOTs to have at most one
68 entry for a given symbol, so their type (3) entries are keyed only
69 on the symbol. The input bfd given by the "abfd" field is somewhat
70 arbitrary in this case.
71
72 This means that when there are multiple GOTs, each GOT has a unique
73 mips_got_entry for every symbol within it. We can therefore use the
74 mips_got_entry fields (tls_type and gotidx) to track the symbol's
75 GOT index.
76
77 However, if it turns out that we need only a single GOT, we continue
78 to use the master GOT to describe it. There may therefore be several
79 mips_got_entries for the same symbol, each with a different input bfd.
80 We want to make sure that each symbol gets a unique GOT entry, so when
81 there's a single GOT, we use the symbol's hash entry, not the
82 mips_got_entry fields, to track a symbol's GOT index. */
83 struct mips_got_entry
84 {
85 /* The input bfd in which the symbol is defined. */
86 bfd *abfd;
87 /* The index of the symbol, as stored in the relocation r_info, if
88 we have a local symbol; -1 otherwise. */
89 long symndx;
90 union
91 {
92 /* If abfd == NULL, an address that must be stored in the got. */
93 bfd_vma address;
94 /* If abfd != NULL && symndx != -1, the addend of the relocation
95 that should be added to the symbol value. */
96 bfd_vma addend;
97 /* If abfd != NULL && symndx == -1, the hash table entry
98 corresponding to symbol in the GOT. The symbol's entry
99 is in the local area if h->global_got_area is GGA_NONE,
100 otherwise it is in the global area. */
101 struct mips_elf_link_hash_entry *h;
102 } d;
103
104 /* The TLS types included in this GOT entry (specifically, GD and
105 IE). The GD and IE flags can be added as we encounter new
106 relocations. LDM can also be set; it will always be alone, not
107 combined with any GD or IE flags. An LDM GOT entry will be
108 a local symbol entry with r_symndx == 0. */
109 unsigned char tls_type;
110
111 /* The offset from the beginning of the .got section to the entry
112 corresponding to this symbol+addend. If it's a global symbol
113 whose offset is yet to be decided, it's going to be -1. */
114 long gotidx;
115 };
116
117 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
118 The structures form a non-overlapping list that is sorted by increasing
119 MIN_ADDEND. */
120 struct mips_got_page_range
121 {
122 struct mips_got_page_range *next;
123 bfd_signed_vma min_addend;
124 bfd_signed_vma max_addend;
125 };
126
127 /* This structure describes the range of addends that are applied to page
128 relocations against a given symbol. */
129 struct mips_got_page_entry
130 {
131 /* The input bfd in which the symbol is defined. */
132 bfd *abfd;
133 /* The index of the symbol, as stored in the relocation r_info. */
134 long symndx;
135 /* The ranges for this page entry. */
136 struct mips_got_page_range *ranges;
137 /* The maximum number of page entries needed for RANGES. */
138 bfd_vma num_pages;
139 };
140
141 /* This structure is used to hold .got information when linking. */
142
143 struct mips_got_info
144 {
145 /* The global symbol in the GOT with the lowest index in the dynamic
146 symbol table. */
147 struct elf_link_hash_entry *global_gotsym;
148 /* The number of global .got entries. */
149 unsigned int global_gotno;
150 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
151 unsigned int reloc_only_gotno;
152 /* The number of .got slots used for TLS. */
153 unsigned int tls_gotno;
154 /* The first unused TLS .got entry. Used only during
155 mips_elf_initialize_tls_index. */
156 unsigned int tls_assigned_gotno;
157 /* The number of local .got entries, eventually including page entries. */
158 unsigned int local_gotno;
159 /* The maximum number of page entries needed. */
160 unsigned int page_gotno;
161 /* The number of local .got entries we have used. */
162 unsigned int assigned_gotno;
163 /* A hash table holding members of the got. */
164 struct htab *got_entries;
165 /* A hash table of mips_got_page_entry structures. */
166 struct htab *got_page_entries;
167 /* A hash table mapping input bfds to other mips_got_info. NULL
168 unless multi-got was necessary. */
169 struct htab *bfd2got;
170 /* In multi-got links, a pointer to the next got (err, rather, most
171 of the time, it points to the previous got). */
172 struct mips_got_info *next;
173 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
174 for none, or MINUS_TWO for not yet assigned. This is needed
175 because a single-GOT link may have multiple hash table entries
176 for the LDM. It does not get initialized in multi-GOT mode. */
177 bfd_vma tls_ldm_offset;
178 };
179
180 /* Map an input bfd to a got in a multi-got link. */
181
182 struct mips_elf_bfd2got_hash
183 {
184 bfd *bfd;
185 struct mips_got_info *g;
186 };
187
188 /* Structure passed when traversing the bfd2got hash table, used to
189 create and merge bfd's gots. */
190
191 struct mips_elf_got_per_bfd_arg
192 {
193 /* A hashtable that maps bfds to gots. */
194 htab_t bfd2got;
195 /* The output bfd. */
196 bfd *obfd;
197 /* The link information. */
198 struct bfd_link_info *info;
199 /* A pointer to the primary got, i.e., the one that's going to get
200 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
201 DT_MIPS_GOTSYM. */
202 struct mips_got_info *primary;
203 /* A non-primary got we're trying to merge with other input bfd's
204 gots. */
205 struct mips_got_info *current;
206 /* The maximum number of got entries that can be addressed with a
207 16-bit offset. */
208 unsigned int max_count;
209 /* The maximum number of page entries needed by each got. */
210 unsigned int max_pages;
211 /* The total number of global entries which will live in the
212 primary got and be automatically relocated. This includes
213 those not referenced by the primary GOT but included in
214 the "master" GOT. */
215 unsigned int global_count;
216 };
217
218 /* Another structure used to pass arguments for got entries traversal. */
219
220 struct mips_elf_set_global_got_offset_arg
221 {
222 struct mips_got_info *g;
223 int value;
224 unsigned int needed_relocs;
225 struct bfd_link_info *info;
226 };
227
228 /* A structure used to count TLS relocations or GOT entries, for GOT
229 entry or ELF symbol table traversal. */
230
231 struct mips_elf_count_tls_arg
232 {
233 struct bfd_link_info *info;
234 unsigned int needed;
235 };
236
237 struct _mips_elf_section_data
238 {
239 struct bfd_elf_section_data elf;
240 union
241 {
242 bfd_byte *tdata;
243 } u;
244 };
245
246 #define mips_elf_section_data(sec) \
247 ((struct _mips_elf_section_data *) elf_section_data (sec))
248
249 #define is_mips_elf(bfd) \
250 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
251 && elf_tdata (bfd) != NULL \
252 && elf_object_id (bfd) == MIPS_ELF_DATA)
253
254 /* The ABI says that every symbol used by dynamic relocations must have
255 a global GOT entry. Among other things, this provides the dynamic
256 linker with a free, directly-indexed cache. The GOT can therefore
257 contain symbols that are not referenced by GOT relocations themselves
258 (in other words, it may have symbols that are not referenced by things
259 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
260
261 GOT relocations are less likely to overflow if we put the associated
262 GOT entries towards the beginning. We therefore divide the global
263 GOT entries into two areas: "normal" and "reloc-only". Entries in
264 the first area can be used for both dynamic relocations and GP-relative
265 accesses, while those in the "reloc-only" area are for dynamic
266 relocations only.
267
268 These GGA_* ("Global GOT Area") values are organised so that lower
269 values are more general than higher values. Also, non-GGA_NONE
270 values are ordered by the position of the area in the GOT. */
271 #define GGA_NORMAL 0
272 #define GGA_RELOC_ONLY 1
273 #define GGA_NONE 2
274
275 /* Information about a non-PIC interface to a PIC function. There are
276 two ways of creating these interfaces. The first is to add:
277
278 lui $25,%hi(func)
279 addiu $25,$25,%lo(func)
280
281 immediately before a PIC function "func". The second is to add:
282
283 lui $25,%hi(func)
284 j func
285 addiu $25,$25,%lo(func)
286
287 to a separate trampoline section.
288
289 Stubs of the first kind go in a new section immediately before the
290 target function. Stubs of the second kind go in a single section
291 pointed to by the hash table's "strampoline" field. */
292 struct mips_elf_la25_stub {
293 /* The generated section that contains this stub. */
294 asection *stub_section;
295
296 /* The offset of the stub from the start of STUB_SECTION. */
297 bfd_vma offset;
298
299 /* One symbol for the original function. Its location is available
300 in H->root.root.u.def. */
301 struct mips_elf_link_hash_entry *h;
302 };
303
304 /* Macros for populating a mips_elf_la25_stub. */
305
306 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
307 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
308 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
309 #define LA25_LUI_MICROMIPS_1(VAL) (0x41b9) /* lui t9,VAL */
310 #define LA25_LUI_MICROMIPS_2(VAL) (VAL)
311 #define LA25_J_MICROMIPS_1(VAL) (0xd400 | (((VAL) >> 17) & 0x3ff)) /* j VAL */
312 #define LA25_J_MICROMIPS_2(VAL) ((VAL) >> 1)
313 #define LA25_ADDIU_MICROMIPS_1(VAL) (0x3339) /* addiu t9,t9,VAL */
314 #define LA25_ADDIU_MICROMIPS_2(VAL) (VAL)
315
316 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
317 the dynamic symbols. */
318
319 struct mips_elf_hash_sort_data
320 {
321 /* The symbol in the global GOT with the lowest dynamic symbol table
322 index. */
323 struct elf_link_hash_entry *low;
324 /* The least dynamic symbol table index corresponding to a non-TLS
325 symbol with a GOT entry. */
326 long min_got_dynindx;
327 /* The greatest dynamic symbol table index corresponding to a symbol
328 with a GOT entry that is not referenced (e.g., a dynamic symbol
329 with dynamic relocations pointing to it from non-primary GOTs). */
330 long max_unref_got_dynindx;
331 /* The greatest dynamic symbol table index not corresponding to a
332 symbol without a GOT entry. */
333 long max_non_got_dynindx;
334 };
335
336 /* The MIPS ELF linker needs additional information for each symbol in
337 the global hash table. */
338
339 struct mips_elf_link_hash_entry
340 {
341 struct elf_link_hash_entry root;
342
343 /* External symbol information. */
344 EXTR esym;
345
346 /* The la25 stub we have created for ths symbol, if any. */
347 struct mips_elf_la25_stub *la25_stub;
348
349 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
350 this symbol. */
351 unsigned int possibly_dynamic_relocs;
352
353 /* If there is a stub that 32 bit functions should use to call this
354 16 bit function, this points to the section containing the stub. */
355 asection *fn_stub;
356
357 /* If there is a stub that 16 bit functions should use to call this
358 32 bit function, this points to the section containing the stub. */
359 asection *call_stub;
360
361 /* This is like the call_stub field, but it is used if the function
362 being called returns a floating point value. */
363 asection *call_fp_stub;
364
365 #define GOT_NORMAL 0
366 #define GOT_TLS_GD 1
367 #define GOT_TLS_LDM 2
368 #define GOT_TLS_IE 4
369 #define GOT_TLS_OFFSET_DONE 0x40
370 #define GOT_TLS_DONE 0x80
371 unsigned char tls_type;
372
373 /* This is only used in single-GOT mode; in multi-GOT mode there
374 is one mips_got_entry per GOT entry, so the offset is stored
375 there. In single-GOT mode there may be many mips_got_entry
376 structures all referring to the same GOT slot. It might be
377 possible to use root.got.offset instead, but that field is
378 overloaded already. */
379 bfd_vma tls_got_offset;
380
381 /* The highest GGA_* value that satisfies all references to this symbol. */
382 unsigned int global_got_area : 2;
383
384 /* True if all GOT relocations against this symbol are for calls. This is
385 a looser condition than no_fn_stub below, because there may be other
386 non-call non-GOT relocations against the symbol. */
387 unsigned int got_only_for_calls : 1;
388
389 /* True if one of the relocations described by possibly_dynamic_relocs
390 is against a readonly section. */
391 unsigned int readonly_reloc : 1;
392
393 /* True if there is a relocation against this symbol that must be
394 resolved by the static linker (in other words, if the relocation
395 cannot possibly be made dynamic). */
396 unsigned int has_static_relocs : 1;
397
398 /* True if we must not create a .MIPS.stubs entry for this symbol.
399 This is set, for example, if there are relocations related to
400 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
401 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
402 unsigned int no_fn_stub : 1;
403
404 /* Whether we need the fn_stub; this is true if this symbol appears
405 in any relocs other than a 16 bit call. */
406 unsigned int need_fn_stub : 1;
407
408 /* True if this symbol is referenced by branch relocations from
409 any non-PIC input file. This is used to determine whether an
410 la25 stub is required. */
411 unsigned int has_nonpic_branches : 1;
412
413 /* Does this symbol need a traditional MIPS lazy-binding stub
414 (as opposed to a PLT entry)? */
415 unsigned int needs_lazy_stub : 1;
416 };
417
418 /* MIPS ELF linker hash table. */
419
420 struct mips_elf_link_hash_table
421 {
422 struct elf_link_hash_table root;
423 #if 0
424 /* We no longer use this. */
425 /* String section indices for the dynamic section symbols. */
426 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
427 #endif
428
429 /* The number of .rtproc entries. */
430 bfd_size_type procedure_count;
431
432 /* The size of the .compact_rel section (if SGI_COMPAT). */
433 bfd_size_type compact_rel_size;
434
435 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
436 entry is set to the address of __rld_obj_head as in IRIX5. */
437 bfd_boolean use_rld_obj_head;
438
439 /* This is the value of the __rld_map or __rld_obj_head symbol. */
440 bfd_vma rld_value;
441
442 /* This is set if we see any mips16 stub sections. */
443 bfd_boolean mips16_stubs_seen;
444
445 /* True if we can generate copy relocs and PLTs. */
446 bfd_boolean use_plts_and_copy_relocs;
447
448 /* True if we're generating code for VxWorks. */
449 bfd_boolean is_vxworks;
450
451 /* True if we already reported the small-data section overflow. */
452 bfd_boolean small_data_overflow_reported;
453
454 /* Shortcuts to some dynamic sections, or NULL if they are not
455 being used. */
456 asection *srelbss;
457 asection *sdynbss;
458 asection *srelplt;
459 asection *srelplt2;
460 asection *sgotplt;
461 asection *splt;
462 asection *sstubs;
463 asection *sgot;
464
465 /* The master GOT information. */
466 struct mips_got_info *got_info;
467
468 /* The size of the PLT header in bytes. */
469 bfd_vma plt_header_size;
470
471 /* The size of a PLT entry in bytes. */
472 bfd_vma plt_entry_size;
473
474 /* The number of functions that need a lazy-binding stub. */
475 bfd_vma lazy_stub_count;
476
477 /* The size of a function stub entry in bytes. */
478 bfd_vma function_stub_size;
479
480 /* The number of reserved entries at the beginning of the GOT. */
481 unsigned int reserved_gotno;
482
483 /* The section used for mips_elf_la25_stub trampolines.
484 See the comment above that structure for details. */
485 asection *strampoline;
486
487 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
488 pairs. */
489 htab_t la25_stubs;
490
491 /* A function FN (NAME, IS, OS) that creates a new input section
492 called NAME and links it to output section OS. If IS is nonnull,
493 the new section should go immediately before it, otherwise it
494 should go at the (current) beginning of OS.
495
496 The function returns the new section on success, otherwise it
497 returns null. */
498 asection *(*add_stub_section) (const char *, asection *, asection *);
499 };
500
501 /* Get the MIPS ELF linker hash table from a link_info structure. */
502
503 #define mips_elf_hash_table(p) \
504 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
505 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
506
507 /* A structure used to communicate with htab_traverse callbacks. */
508 struct mips_htab_traverse_info
509 {
510 /* The usual link-wide information. */
511 struct bfd_link_info *info;
512 bfd *output_bfd;
513
514 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
515 bfd_boolean error;
516 };
517
518 #define TLS_RELOC_P(r_type) \
519 (r_type == R_MIPS_TLS_DTPMOD32 \
520 || r_type == R_MIPS_TLS_DTPMOD64 \
521 || r_type == R_MIPS_TLS_DTPREL32 \
522 || r_type == R_MIPS_TLS_DTPREL64 \
523 || r_type == R_MIPS_TLS_GD \
524 || r_type == R_MIPS_TLS_LDM \
525 || r_type == R_MIPS_TLS_DTPREL_HI16 \
526 || r_type == R_MIPS_TLS_DTPREL_LO16 \
527 || r_type == R_MIPS_TLS_GOTTPREL \
528 || r_type == R_MIPS_TLS_TPREL32 \
529 || r_type == R_MIPS_TLS_TPREL64 \
530 || r_type == R_MIPS_TLS_TPREL_HI16 \
531 || r_type == R_MIPS_TLS_TPREL_LO16 \
532 || r_type == R_MICROMIPS_TLS_GD \
533 || r_type == R_MICROMIPS_TLS_LDM \
534 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
535 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
536 || r_type == R_MICROMIPS_TLS_GOTTPREL \
537 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
538 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
539
540 /* Structure used to pass information to mips_elf_output_extsym. */
541
542 struct extsym_info
543 {
544 bfd *abfd;
545 struct bfd_link_info *info;
546 struct ecoff_debug_info *debug;
547 const struct ecoff_debug_swap *swap;
548 bfd_boolean failed;
549 };
550
551 /* The names of the runtime procedure table symbols used on IRIX5. */
552
553 static const char * const mips_elf_dynsym_rtproc_names[] =
554 {
555 "_procedure_table",
556 "_procedure_string_table",
557 "_procedure_table_size",
558 NULL
559 };
560
561 /* These structures are used to generate the .compact_rel section on
562 IRIX5. */
563
564 typedef struct
565 {
566 unsigned long id1; /* Always one? */
567 unsigned long num; /* Number of compact relocation entries. */
568 unsigned long id2; /* Always two? */
569 unsigned long offset; /* The file offset of the first relocation. */
570 unsigned long reserved0; /* Zero? */
571 unsigned long reserved1; /* Zero? */
572 } Elf32_compact_rel;
573
574 typedef struct
575 {
576 bfd_byte id1[4];
577 bfd_byte num[4];
578 bfd_byte id2[4];
579 bfd_byte offset[4];
580 bfd_byte reserved0[4];
581 bfd_byte reserved1[4];
582 } Elf32_External_compact_rel;
583
584 typedef struct
585 {
586 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
587 unsigned int rtype : 4; /* Relocation types. See below. */
588 unsigned int dist2to : 8;
589 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
590 unsigned long konst; /* KONST field. See below. */
591 unsigned long vaddr; /* VADDR to be relocated. */
592 } Elf32_crinfo;
593
594 typedef struct
595 {
596 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
597 unsigned int rtype : 4; /* Relocation types. See below. */
598 unsigned int dist2to : 8;
599 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
600 unsigned long konst; /* KONST field. See below. */
601 } Elf32_crinfo2;
602
603 typedef struct
604 {
605 bfd_byte info[4];
606 bfd_byte konst[4];
607 bfd_byte vaddr[4];
608 } Elf32_External_crinfo;
609
610 typedef struct
611 {
612 bfd_byte info[4];
613 bfd_byte konst[4];
614 } Elf32_External_crinfo2;
615
616 /* These are the constants used to swap the bitfields in a crinfo. */
617
618 #define CRINFO_CTYPE (0x1)
619 #define CRINFO_CTYPE_SH (31)
620 #define CRINFO_RTYPE (0xf)
621 #define CRINFO_RTYPE_SH (27)
622 #define CRINFO_DIST2TO (0xff)
623 #define CRINFO_DIST2TO_SH (19)
624 #define CRINFO_RELVADDR (0x7ffff)
625 #define CRINFO_RELVADDR_SH (0)
626
627 /* A compact relocation info has long (3 words) or short (2 words)
628 formats. A short format doesn't have VADDR field and relvaddr
629 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
630 #define CRF_MIPS_LONG 1
631 #define CRF_MIPS_SHORT 0
632
633 /* There are 4 types of compact relocation at least. The value KONST
634 has different meaning for each type:
635
636 (type) (konst)
637 CT_MIPS_REL32 Address in data
638 CT_MIPS_WORD Address in word (XXX)
639 CT_MIPS_GPHI_LO GP - vaddr
640 CT_MIPS_JMPAD Address to jump
641 */
642
643 #define CRT_MIPS_REL32 0xa
644 #define CRT_MIPS_WORD 0xb
645 #define CRT_MIPS_GPHI_LO 0xc
646 #define CRT_MIPS_JMPAD 0xd
647
648 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
649 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
650 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
651 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
652 \f
653 /* The structure of the runtime procedure descriptor created by the
654 loader for use by the static exception system. */
655
656 typedef struct runtime_pdr {
657 bfd_vma adr; /* Memory address of start of procedure. */
658 long regmask; /* Save register mask. */
659 long regoffset; /* Save register offset. */
660 long fregmask; /* Save floating point register mask. */
661 long fregoffset; /* Save floating point register offset. */
662 long frameoffset; /* Frame size. */
663 short framereg; /* Frame pointer register. */
664 short pcreg; /* Offset or reg of return pc. */
665 long irpss; /* Index into the runtime string table. */
666 long reserved;
667 struct exception_info *exception_info;/* Pointer to exception array. */
668 } RPDR, *pRPDR;
669 #define cbRPDR sizeof (RPDR)
670 #define rpdNil ((pRPDR) 0)
671 \f
672 static struct mips_got_entry *mips_elf_create_local_got_entry
673 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
674 struct mips_elf_link_hash_entry *, int);
675 static bfd_boolean mips_elf_sort_hash_table_f
676 (struct mips_elf_link_hash_entry *, void *);
677 static bfd_vma mips_elf_high
678 (bfd_vma);
679 static bfd_boolean mips_elf_create_dynamic_relocation
680 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
681 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
682 bfd_vma *, asection *);
683 static hashval_t mips_elf_got_entry_hash
684 (const void *);
685 static bfd_vma mips_elf_adjust_gp
686 (bfd *, struct mips_got_info *, bfd *);
687 static struct mips_got_info *mips_elf_got_for_ibfd
688 (struct mips_got_info *, bfd *);
689
690 /* This will be used when we sort the dynamic relocation records. */
691 static bfd *reldyn_sorting_bfd;
692
693 /* True if ABFD is for CPUs with load interlocking that include
694 non-MIPS1 CPUs and R3900. */
695 #define LOAD_INTERLOCKS_P(abfd) \
696 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
697 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
698
699 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
700 This should be safe for all architectures. We enable this predicate
701 for RM9000 for now. */
702 #define JAL_TO_BAL_P(abfd) \
703 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
704
705 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
706 This should be safe for all architectures. We enable this predicate for
707 all CPUs. */
708 #define JALR_TO_BAL_P(abfd) 1
709
710 /* True if ABFD is for CPUs that are faster if JR is converted to B.
711 This should be safe for all architectures. We enable this predicate for
712 all CPUs. */
713 #define JR_TO_B_P(abfd) 1
714
715 /* True if ABFD is a PIC object. */
716 #define PIC_OBJECT_P(abfd) \
717 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
718
719 /* Nonzero if ABFD is using the N32 ABI. */
720 #define ABI_N32_P(abfd) \
721 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
722
723 /* Nonzero if ABFD is using the N64 ABI. */
724 #define ABI_64_P(abfd) \
725 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
726
727 /* Nonzero if ABFD is using NewABI conventions. */
728 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
729
730 /* The IRIX compatibility level we are striving for. */
731 #define IRIX_COMPAT(abfd) \
732 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
733
734 /* Whether we are trying to be compatible with IRIX at all. */
735 #define SGI_COMPAT(abfd) \
736 (IRIX_COMPAT (abfd) != ict_none)
737
738 /* The name of the options section. */
739 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
740 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
741
742 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
743 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
744 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
745 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
746
747 /* Whether the section is readonly. */
748 #define MIPS_ELF_READONLY_SECTION(sec) \
749 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
750 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
751
752 /* The name of the stub section. */
753 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
754
755 /* The size of an external REL relocation. */
756 #define MIPS_ELF_REL_SIZE(abfd) \
757 (get_elf_backend_data (abfd)->s->sizeof_rel)
758
759 /* The size of an external RELA relocation. */
760 #define MIPS_ELF_RELA_SIZE(abfd) \
761 (get_elf_backend_data (abfd)->s->sizeof_rela)
762
763 /* The size of an external dynamic table entry. */
764 #define MIPS_ELF_DYN_SIZE(abfd) \
765 (get_elf_backend_data (abfd)->s->sizeof_dyn)
766
767 /* The size of a GOT entry. */
768 #define MIPS_ELF_GOT_SIZE(abfd) \
769 (get_elf_backend_data (abfd)->s->arch_size / 8)
770
771 /* The size of a symbol-table entry. */
772 #define MIPS_ELF_SYM_SIZE(abfd) \
773 (get_elf_backend_data (abfd)->s->sizeof_sym)
774
775 /* The default alignment for sections, as a power of two. */
776 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
777 (get_elf_backend_data (abfd)->s->log_file_align)
778
779 /* Get word-sized data. */
780 #define MIPS_ELF_GET_WORD(abfd, ptr) \
781 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
782
783 /* Put out word-sized data. */
784 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
785 (ABI_64_P (abfd) \
786 ? bfd_put_64 (abfd, val, ptr) \
787 : bfd_put_32 (abfd, val, ptr))
788
789 /* The opcode for word-sized loads (LW or LD). */
790 #define MIPS_ELF_LOAD_WORD(abfd) \
791 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
792
793 /* Add a dynamic symbol table-entry. */
794 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
795 _bfd_elf_add_dynamic_entry (info, tag, val)
796
797 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
798 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
799
800 /* The name of the dynamic relocation section. */
801 #define MIPS_ELF_REL_DYN_NAME(INFO) \
802 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
803
804 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
805 from smaller values. Start with zero, widen, *then* decrement. */
806 #define MINUS_ONE (((bfd_vma)0) - 1)
807 #define MINUS_TWO (((bfd_vma)0) - 2)
808
809 /* The value to write into got[1] for SVR4 targets, to identify it is
810 a GNU object. The dynamic linker can then use got[1] to store the
811 module pointer. */
812 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
813 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
814
815 /* The offset of $gp from the beginning of the .got section. */
816 #define ELF_MIPS_GP_OFFSET(INFO) \
817 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
818
819 /* The maximum size of the GOT for it to be addressable using 16-bit
820 offsets from $gp. */
821 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
822
823 /* Instructions which appear in a stub. */
824 #define STUB_LW(abfd) \
825 ((ABI_64_P (abfd) \
826 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
827 : 0x8f998010)) /* lw t9,0x8010(gp) */
828 #define STUB_MOVE(abfd) \
829 ((ABI_64_P (abfd) \
830 ? 0x03e0782d /* daddu t7,ra */ \
831 : 0x03e07821)) /* addu t7,ra */
832 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
833 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
834 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
835 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
836 #define STUB_LI16S(abfd, VAL) \
837 ((ABI_64_P (abfd) \
838 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
839 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
840
841 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
842 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
843
844 /* The name of the dynamic interpreter. This is put in the .interp
845 section. */
846
847 #define ELF_DYNAMIC_INTERPRETER(abfd) \
848 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
849 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
850 : "/usr/lib/libc.so.1")
851
852 #ifdef BFD64
853 #define MNAME(bfd,pre,pos) \
854 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
855 #define ELF_R_SYM(bfd, i) \
856 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
857 #define ELF_R_TYPE(bfd, i) \
858 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
859 #define ELF_R_INFO(bfd, s, t) \
860 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
861 #else
862 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
863 #define ELF_R_SYM(bfd, i) \
864 (ELF32_R_SYM (i))
865 #define ELF_R_TYPE(bfd, i) \
866 (ELF32_R_TYPE (i))
867 #define ELF_R_INFO(bfd, s, t) \
868 (ELF32_R_INFO (s, t))
869 #endif
870 \f
871 /* The mips16 compiler uses a couple of special sections to handle
872 floating point arguments.
873
874 Section names that look like .mips16.fn.FNNAME contain stubs that
875 copy floating point arguments from the fp regs to the gp regs and
876 then jump to FNNAME. If any 32 bit function calls FNNAME, the
877 call should be redirected to the stub instead. If no 32 bit
878 function calls FNNAME, the stub should be discarded. We need to
879 consider any reference to the function, not just a call, because
880 if the address of the function is taken we will need the stub,
881 since the address might be passed to a 32 bit function.
882
883 Section names that look like .mips16.call.FNNAME contain stubs
884 that copy floating point arguments from the gp regs to the fp
885 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
886 then any 16 bit function that calls FNNAME should be redirected
887 to the stub instead. If FNNAME is not a 32 bit function, the
888 stub should be discarded.
889
890 .mips16.call.fp.FNNAME sections are similar, but contain stubs
891 which call FNNAME and then copy the return value from the fp regs
892 to the gp regs. These stubs store the return value in $18 while
893 calling FNNAME; any function which might call one of these stubs
894 must arrange to save $18 around the call. (This case is not
895 needed for 32 bit functions that call 16 bit functions, because
896 16 bit functions always return floating point values in both
897 $f0/$f1 and $2/$3.)
898
899 Note that in all cases FNNAME might be defined statically.
900 Therefore, FNNAME is not used literally. Instead, the relocation
901 information will indicate which symbol the section is for.
902
903 We record any stubs that we find in the symbol table. */
904
905 #define FN_STUB ".mips16.fn."
906 #define CALL_STUB ".mips16.call."
907 #define CALL_FP_STUB ".mips16.call.fp."
908
909 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
910 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
911 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
912 \f
913 /* The format of the first PLT entry in an O32 executable. */
914 static const bfd_vma mips_o32_exec_plt0_entry[] =
915 {
916 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
917 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
918 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
919 0x031cc023, /* subu $24, $24, $28 */
920 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
921 0x0018c082, /* srl $24, $24, 2 */
922 0x0320f809, /* jalr $25 */
923 0x2718fffe /* subu $24, $24, 2 */
924 };
925
926 /* The format of the first PLT entry in an N32 executable. Different
927 because gp ($28) is not available; we use t2 ($14) instead. */
928 static const bfd_vma mips_n32_exec_plt0_entry[] =
929 {
930 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
931 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
932 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
933 0x030ec023, /* subu $24, $24, $14 */
934 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
935 0x0018c082, /* srl $24, $24, 2 */
936 0x0320f809, /* jalr $25 */
937 0x2718fffe /* subu $24, $24, 2 */
938 };
939
940 /* The format of the first PLT entry in an N64 executable. Different
941 from N32 because of the increased size of GOT entries. */
942 static const bfd_vma mips_n64_exec_plt0_entry[] =
943 {
944 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
945 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
946 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
947 0x030ec023, /* subu $24, $24, $14 */
948 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
949 0x0018c0c2, /* srl $24, $24, 3 */
950 0x0320f809, /* jalr $25 */
951 0x2718fffe /* subu $24, $24, 2 */
952 };
953
954 /* The format of subsequent PLT entries. */
955 static const bfd_vma mips_exec_plt_entry[] =
956 {
957 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
958 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
959 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
960 0x03200008 /* jr $25 */
961 };
962
963 /* The format of the first PLT entry in a VxWorks executable. */
964 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
965 {
966 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
967 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
968 0x8f390008, /* lw t9, 8(t9) */
969 0x00000000, /* nop */
970 0x03200008, /* jr t9 */
971 0x00000000 /* nop */
972 };
973
974 /* The format of subsequent PLT entries. */
975 static const bfd_vma mips_vxworks_exec_plt_entry[] =
976 {
977 0x10000000, /* b .PLT_resolver */
978 0x24180000, /* li t8, <pltindex> */
979 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
980 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
981 0x8f390000, /* lw t9, 0(t9) */
982 0x00000000, /* nop */
983 0x03200008, /* jr t9 */
984 0x00000000 /* nop */
985 };
986
987 /* The format of the first PLT entry in a VxWorks shared object. */
988 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
989 {
990 0x8f990008, /* lw t9, 8(gp) */
991 0x00000000, /* nop */
992 0x03200008, /* jr t9 */
993 0x00000000, /* nop */
994 0x00000000, /* nop */
995 0x00000000 /* nop */
996 };
997
998 /* The format of subsequent PLT entries. */
999 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1000 {
1001 0x10000000, /* b .PLT_resolver */
1002 0x24180000 /* li t8, <pltindex> */
1003 };
1004 \f
1005 /* Look up an entry in a MIPS ELF linker hash table. */
1006
1007 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1008 ((struct mips_elf_link_hash_entry *) \
1009 elf_link_hash_lookup (&(table)->root, (string), (create), \
1010 (copy), (follow)))
1011
1012 /* Traverse a MIPS ELF linker hash table. */
1013
1014 #define mips_elf_link_hash_traverse(table, func, info) \
1015 (elf_link_hash_traverse \
1016 (&(table)->root, \
1017 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1018 (info)))
1019
1020 /* Find the base offsets for thread-local storage in this object,
1021 for GD/LD and IE/LE respectively. */
1022
1023 #define TP_OFFSET 0x7000
1024 #define DTP_OFFSET 0x8000
1025
1026 static bfd_vma
1027 dtprel_base (struct bfd_link_info *info)
1028 {
1029 /* If tls_sec is NULL, we should have signalled an error already. */
1030 if (elf_hash_table (info)->tls_sec == NULL)
1031 return 0;
1032 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1033 }
1034
1035 static bfd_vma
1036 tprel_base (struct bfd_link_info *info)
1037 {
1038 /* If tls_sec is NULL, we should have signalled an error already. */
1039 if (elf_hash_table (info)->tls_sec == NULL)
1040 return 0;
1041 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1042 }
1043
1044 /* Create an entry in a MIPS ELF linker hash table. */
1045
1046 static struct bfd_hash_entry *
1047 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1048 struct bfd_hash_table *table, const char *string)
1049 {
1050 struct mips_elf_link_hash_entry *ret =
1051 (struct mips_elf_link_hash_entry *) entry;
1052
1053 /* Allocate the structure if it has not already been allocated by a
1054 subclass. */
1055 if (ret == NULL)
1056 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1057 if (ret == NULL)
1058 return (struct bfd_hash_entry *) ret;
1059
1060 /* Call the allocation method of the superclass. */
1061 ret = ((struct mips_elf_link_hash_entry *)
1062 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1063 table, string));
1064 if (ret != NULL)
1065 {
1066 /* Set local fields. */
1067 memset (&ret->esym, 0, sizeof (EXTR));
1068 /* We use -2 as a marker to indicate that the information has
1069 not been set. -1 means there is no associated ifd. */
1070 ret->esym.ifd = -2;
1071 ret->la25_stub = 0;
1072 ret->possibly_dynamic_relocs = 0;
1073 ret->fn_stub = NULL;
1074 ret->call_stub = NULL;
1075 ret->call_fp_stub = NULL;
1076 ret->tls_type = GOT_NORMAL;
1077 ret->global_got_area = GGA_NONE;
1078 ret->got_only_for_calls = TRUE;
1079 ret->readonly_reloc = FALSE;
1080 ret->has_static_relocs = FALSE;
1081 ret->no_fn_stub = FALSE;
1082 ret->need_fn_stub = FALSE;
1083 ret->has_nonpic_branches = FALSE;
1084 ret->needs_lazy_stub = FALSE;
1085 }
1086
1087 return (struct bfd_hash_entry *) ret;
1088 }
1089
1090 bfd_boolean
1091 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1092 {
1093 if (!sec->used_by_bfd)
1094 {
1095 struct _mips_elf_section_data *sdata;
1096 bfd_size_type amt = sizeof (*sdata);
1097
1098 sdata = bfd_zalloc (abfd, amt);
1099 if (sdata == NULL)
1100 return FALSE;
1101 sec->used_by_bfd = sdata;
1102 }
1103
1104 return _bfd_elf_new_section_hook (abfd, sec);
1105 }
1106 \f
1107 /* Read ECOFF debugging information from a .mdebug section into a
1108 ecoff_debug_info structure. */
1109
1110 bfd_boolean
1111 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1112 struct ecoff_debug_info *debug)
1113 {
1114 HDRR *symhdr;
1115 const struct ecoff_debug_swap *swap;
1116 char *ext_hdr;
1117
1118 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1119 memset (debug, 0, sizeof (*debug));
1120
1121 ext_hdr = bfd_malloc (swap->external_hdr_size);
1122 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1123 goto error_return;
1124
1125 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1126 swap->external_hdr_size))
1127 goto error_return;
1128
1129 symhdr = &debug->symbolic_header;
1130 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1131
1132 /* The symbolic header contains absolute file offsets and sizes to
1133 read. */
1134 #define READ(ptr, offset, count, size, type) \
1135 if (symhdr->count == 0) \
1136 debug->ptr = NULL; \
1137 else \
1138 { \
1139 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1140 debug->ptr = bfd_malloc (amt); \
1141 if (debug->ptr == NULL) \
1142 goto error_return; \
1143 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1144 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1145 goto error_return; \
1146 }
1147
1148 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1149 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1150 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1151 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1152 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1153 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1154 union aux_ext *);
1155 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1156 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1157 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1158 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1159 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1160 #undef READ
1161
1162 debug->fdr = NULL;
1163
1164 return TRUE;
1165
1166 error_return:
1167 if (ext_hdr != NULL)
1168 free (ext_hdr);
1169 if (debug->line != NULL)
1170 free (debug->line);
1171 if (debug->external_dnr != NULL)
1172 free (debug->external_dnr);
1173 if (debug->external_pdr != NULL)
1174 free (debug->external_pdr);
1175 if (debug->external_sym != NULL)
1176 free (debug->external_sym);
1177 if (debug->external_opt != NULL)
1178 free (debug->external_opt);
1179 if (debug->external_aux != NULL)
1180 free (debug->external_aux);
1181 if (debug->ss != NULL)
1182 free (debug->ss);
1183 if (debug->ssext != NULL)
1184 free (debug->ssext);
1185 if (debug->external_fdr != NULL)
1186 free (debug->external_fdr);
1187 if (debug->external_rfd != NULL)
1188 free (debug->external_rfd);
1189 if (debug->external_ext != NULL)
1190 free (debug->external_ext);
1191 return FALSE;
1192 }
1193 \f
1194 /* Swap RPDR (runtime procedure table entry) for output. */
1195
1196 static void
1197 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1198 {
1199 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1200 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1201 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1202 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1203 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1204 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1205
1206 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1207 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1208
1209 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1210 }
1211
1212 /* Create a runtime procedure table from the .mdebug section. */
1213
1214 static bfd_boolean
1215 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1216 struct bfd_link_info *info, asection *s,
1217 struct ecoff_debug_info *debug)
1218 {
1219 const struct ecoff_debug_swap *swap;
1220 HDRR *hdr = &debug->symbolic_header;
1221 RPDR *rpdr, *rp;
1222 struct rpdr_ext *erp;
1223 void *rtproc;
1224 struct pdr_ext *epdr;
1225 struct sym_ext *esym;
1226 char *ss, **sv;
1227 char *str;
1228 bfd_size_type size;
1229 bfd_size_type count;
1230 unsigned long sindex;
1231 unsigned long i;
1232 PDR pdr;
1233 SYMR sym;
1234 const char *no_name_func = _("static procedure (no name)");
1235
1236 epdr = NULL;
1237 rpdr = NULL;
1238 esym = NULL;
1239 ss = NULL;
1240 sv = NULL;
1241
1242 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1243
1244 sindex = strlen (no_name_func) + 1;
1245 count = hdr->ipdMax;
1246 if (count > 0)
1247 {
1248 size = swap->external_pdr_size;
1249
1250 epdr = bfd_malloc (size * count);
1251 if (epdr == NULL)
1252 goto error_return;
1253
1254 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1255 goto error_return;
1256
1257 size = sizeof (RPDR);
1258 rp = rpdr = bfd_malloc (size * count);
1259 if (rpdr == NULL)
1260 goto error_return;
1261
1262 size = sizeof (char *);
1263 sv = bfd_malloc (size * count);
1264 if (sv == NULL)
1265 goto error_return;
1266
1267 count = hdr->isymMax;
1268 size = swap->external_sym_size;
1269 esym = bfd_malloc (size * count);
1270 if (esym == NULL)
1271 goto error_return;
1272
1273 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1274 goto error_return;
1275
1276 count = hdr->issMax;
1277 ss = bfd_malloc (count);
1278 if (ss == NULL)
1279 goto error_return;
1280 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1281 goto error_return;
1282
1283 count = hdr->ipdMax;
1284 for (i = 0; i < (unsigned long) count; i++, rp++)
1285 {
1286 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1287 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1288 rp->adr = sym.value;
1289 rp->regmask = pdr.regmask;
1290 rp->regoffset = pdr.regoffset;
1291 rp->fregmask = pdr.fregmask;
1292 rp->fregoffset = pdr.fregoffset;
1293 rp->frameoffset = pdr.frameoffset;
1294 rp->framereg = pdr.framereg;
1295 rp->pcreg = pdr.pcreg;
1296 rp->irpss = sindex;
1297 sv[i] = ss + sym.iss;
1298 sindex += strlen (sv[i]) + 1;
1299 }
1300 }
1301
1302 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1303 size = BFD_ALIGN (size, 16);
1304 rtproc = bfd_alloc (abfd, size);
1305 if (rtproc == NULL)
1306 {
1307 mips_elf_hash_table (info)->procedure_count = 0;
1308 goto error_return;
1309 }
1310
1311 mips_elf_hash_table (info)->procedure_count = count + 2;
1312
1313 erp = rtproc;
1314 memset (erp, 0, sizeof (struct rpdr_ext));
1315 erp++;
1316 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1317 strcpy (str, no_name_func);
1318 str += strlen (no_name_func) + 1;
1319 for (i = 0; i < count; i++)
1320 {
1321 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1322 strcpy (str, sv[i]);
1323 str += strlen (sv[i]) + 1;
1324 }
1325 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1326
1327 /* Set the size and contents of .rtproc section. */
1328 s->size = size;
1329 s->contents = rtproc;
1330
1331 /* Skip this section later on (I don't think this currently
1332 matters, but someday it might). */
1333 s->map_head.link_order = NULL;
1334
1335 if (epdr != NULL)
1336 free (epdr);
1337 if (rpdr != NULL)
1338 free (rpdr);
1339 if (esym != NULL)
1340 free (esym);
1341 if (ss != NULL)
1342 free (ss);
1343 if (sv != NULL)
1344 free (sv);
1345
1346 return TRUE;
1347
1348 error_return:
1349 if (epdr != NULL)
1350 free (epdr);
1351 if (rpdr != NULL)
1352 free (rpdr);
1353 if (esym != NULL)
1354 free (esym);
1355 if (ss != NULL)
1356 free (ss);
1357 if (sv != NULL)
1358 free (sv);
1359 return FALSE;
1360 }
1361 \f
1362 /* We're going to create a stub for H. Create a symbol for the stub's
1363 value and size, to help make the disassembly easier to read. */
1364
1365 static bfd_boolean
1366 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1367 struct mips_elf_link_hash_entry *h,
1368 const char *prefix, asection *s, bfd_vma value,
1369 bfd_vma size)
1370 {
1371 struct bfd_link_hash_entry *bh;
1372 struct elf_link_hash_entry *elfh;
1373 const char *name;
1374
1375 if (ELF_ST_IS_MICROMIPS (h->root.other))
1376 value |= 1;
1377
1378 /* Create a new symbol. */
1379 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1380 bh = NULL;
1381 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1382 BSF_LOCAL, s, value, NULL,
1383 TRUE, FALSE, &bh))
1384 return FALSE;
1385
1386 /* Make it a local function. */
1387 elfh = (struct elf_link_hash_entry *) bh;
1388 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1389 elfh->size = size;
1390 elfh->forced_local = 1;
1391 return TRUE;
1392 }
1393
1394 /* We're about to redefine H. Create a symbol to represent H's
1395 current value and size, to help make the disassembly easier
1396 to read. */
1397
1398 static bfd_boolean
1399 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1400 struct mips_elf_link_hash_entry *h,
1401 const char *prefix)
1402 {
1403 struct bfd_link_hash_entry *bh;
1404 struct elf_link_hash_entry *elfh;
1405 const char *name;
1406 asection *s;
1407 bfd_vma value;
1408
1409 /* Read the symbol's value. */
1410 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1411 || h->root.root.type == bfd_link_hash_defweak);
1412 s = h->root.root.u.def.section;
1413 value = h->root.root.u.def.value;
1414
1415 /* Create a new symbol. */
1416 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1417 bh = NULL;
1418 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1419 BSF_LOCAL, s, value, NULL,
1420 TRUE, FALSE, &bh))
1421 return FALSE;
1422
1423 /* Make it local and copy the other attributes from H. */
1424 elfh = (struct elf_link_hash_entry *) bh;
1425 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1426 elfh->other = h->root.other;
1427 elfh->size = h->root.size;
1428 elfh->forced_local = 1;
1429 return TRUE;
1430 }
1431
1432 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1433 function rather than to a hard-float stub. */
1434
1435 static bfd_boolean
1436 section_allows_mips16_refs_p (asection *section)
1437 {
1438 const char *name;
1439
1440 name = bfd_get_section_name (section->owner, section);
1441 return (FN_STUB_P (name)
1442 || CALL_STUB_P (name)
1443 || CALL_FP_STUB_P (name)
1444 || strcmp (name, ".pdr") == 0);
1445 }
1446
1447 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1448 stub section of some kind. Return the R_SYMNDX of the target
1449 function, or 0 if we can't decide which function that is. */
1450
1451 static unsigned long
1452 mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1453 const Elf_Internal_Rela *relocs,
1454 const Elf_Internal_Rela *relend)
1455 {
1456 const Elf_Internal_Rela *rel;
1457
1458 /* Trust the first R_MIPS_NONE relocation, if any. */
1459 for (rel = relocs; rel < relend; rel++)
1460 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1461 return ELF_R_SYM (sec->owner, rel->r_info);
1462
1463 /* Otherwise trust the first relocation, whatever its kind. This is
1464 the traditional behavior. */
1465 if (relocs < relend)
1466 return ELF_R_SYM (sec->owner, relocs->r_info);
1467
1468 return 0;
1469 }
1470
1471 /* Check the mips16 stubs for a particular symbol, and see if we can
1472 discard them. */
1473
1474 static void
1475 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1476 struct mips_elf_link_hash_entry *h)
1477 {
1478 /* Dynamic symbols must use the standard call interface, in case other
1479 objects try to call them. */
1480 if (h->fn_stub != NULL
1481 && h->root.dynindx != -1)
1482 {
1483 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1484 h->need_fn_stub = TRUE;
1485 }
1486
1487 if (h->fn_stub != NULL
1488 && ! h->need_fn_stub)
1489 {
1490 /* We don't need the fn_stub; the only references to this symbol
1491 are 16 bit calls. Clobber the size to 0 to prevent it from
1492 being included in the link. */
1493 h->fn_stub->size = 0;
1494 h->fn_stub->flags &= ~SEC_RELOC;
1495 h->fn_stub->reloc_count = 0;
1496 h->fn_stub->flags |= SEC_EXCLUDE;
1497 }
1498
1499 if (h->call_stub != NULL
1500 && ELF_ST_IS_MIPS16 (h->root.other))
1501 {
1502 /* We don't need the call_stub; this is a 16 bit function, so
1503 calls from other 16 bit functions are OK. Clobber the size
1504 to 0 to prevent it from being included in the link. */
1505 h->call_stub->size = 0;
1506 h->call_stub->flags &= ~SEC_RELOC;
1507 h->call_stub->reloc_count = 0;
1508 h->call_stub->flags |= SEC_EXCLUDE;
1509 }
1510
1511 if (h->call_fp_stub != NULL
1512 && ELF_ST_IS_MIPS16 (h->root.other))
1513 {
1514 /* We don't need the call_stub; this is a 16 bit function, so
1515 calls from other 16 bit functions are OK. Clobber the size
1516 to 0 to prevent it from being included in the link. */
1517 h->call_fp_stub->size = 0;
1518 h->call_fp_stub->flags &= ~SEC_RELOC;
1519 h->call_fp_stub->reloc_count = 0;
1520 h->call_fp_stub->flags |= SEC_EXCLUDE;
1521 }
1522 }
1523
1524 /* Hashtable callbacks for mips_elf_la25_stubs. */
1525
1526 static hashval_t
1527 mips_elf_la25_stub_hash (const void *entry_)
1528 {
1529 const struct mips_elf_la25_stub *entry;
1530
1531 entry = (struct mips_elf_la25_stub *) entry_;
1532 return entry->h->root.root.u.def.section->id
1533 + entry->h->root.root.u.def.value;
1534 }
1535
1536 static int
1537 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1538 {
1539 const struct mips_elf_la25_stub *entry1, *entry2;
1540
1541 entry1 = (struct mips_elf_la25_stub *) entry1_;
1542 entry2 = (struct mips_elf_la25_stub *) entry2_;
1543 return ((entry1->h->root.root.u.def.section
1544 == entry2->h->root.root.u.def.section)
1545 && (entry1->h->root.root.u.def.value
1546 == entry2->h->root.root.u.def.value));
1547 }
1548
1549 /* Called by the linker to set up the la25 stub-creation code. FN is
1550 the linker's implementation of add_stub_function. Return true on
1551 success. */
1552
1553 bfd_boolean
1554 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1555 asection *(*fn) (const char *, asection *,
1556 asection *))
1557 {
1558 struct mips_elf_link_hash_table *htab;
1559
1560 htab = mips_elf_hash_table (info);
1561 if (htab == NULL)
1562 return FALSE;
1563
1564 htab->add_stub_section = fn;
1565 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1566 mips_elf_la25_stub_eq, NULL);
1567 if (htab->la25_stubs == NULL)
1568 return FALSE;
1569
1570 return TRUE;
1571 }
1572
1573 /* Return true if H is a locally-defined PIC function, in the sense
1574 that it might need $25 to be valid on entry. Note that MIPS16
1575 functions never need $25 to be valid on entry; they set up $gp
1576 using PC-relative instructions instead. */
1577
1578 static bfd_boolean
1579 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1580 {
1581 return ((h->root.root.type == bfd_link_hash_defined
1582 || h->root.root.type == bfd_link_hash_defweak)
1583 && h->root.def_regular
1584 && !bfd_is_abs_section (h->root.root.u.def.section)
1585 && !ELF_ST_IS_MIPS16 (h->root.other)
1586 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1587 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1588 }
1589
1590 /* STUB describes an la25 stub that we have decided to implement
1591 by inserting an LUI/ADDIU pair before the target function.
1592 Create the section and redirect the function symbol to it. */
1593
1594 static bfd_boolean
1595 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1596 struct bfd_link_info *info)
1597 {
1598 struct mips_elf_link_hash_table *htab;
1599 char *name;
1600 asection *s, *input_section;
1601 unsigned int align;
1602
1603 htab = mips_elf_hash_table (info);
1604 if (htab == NULL)
1605 return FALSE;
1606
1607 /* Create a unique name for the new section. */
1608 name = bfd_malloc (11 + sizeof (".text.stub."));
1609 if (name == NULL)
1610 return FALSE;
1611 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1612
1613 /* Create the section. */
1614 input_section = stub->h->root.root.u.def.section;
1615 s = htab->add_stub_section (name, input_section,
1616 input_section->output_section);
1617 if (s == NULL)
1618 return FALSE;
1619
1620 /* Make sure that any padding goes before the stub. */
1621 align = input_section->alignment_power;
1622 if (!bfd_set_section_alignment (s->owner, s, align))
1623 return FALSE;
1624 if (align > 3)
1625 s->size = (1 << align) - 8;
1626
1627 /* Create a symbol for the stub. */
1628 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1629 stub->stub_section = s;
1630 stub->offset = s->size;
1631
1632 /* Allocate room for it. */
1633 s->size += 8;
1634 return TRUE;
1635 }
1636
1637 /* STUB describes an la25 stub that we have decided to implement
1638 with a separate trampoline. Allocate room for it and redirect
1639 the function symbol to it. */
1640
1641 static bfd_boolean
1642 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1643 struct bfd_link_info *info)
1644 {
1645 struct mips_elf_link_hash_table *htab;
1646 asection *s;
1647
1648 htab = mips_elf_hash_table (info);
1649 if (htab == NULL)
1650 return FALSE;
1651
1652 /* Create a trampoline section, if we haven't already. */
1653 s = htab->strampoline;
1654 if (s == NULL)
1655 {
1656 asection *input_section = stub->h->root.root.u.def.section;
1657 s = htab->add_stub_section (".text", NULL,
1658 input_section->output_section);
1659 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1660 return FALSE;
1661 htab->strampoline = s;
1662 }
1663
1664 /* Create a symbol for the stub. */
1665 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1666 stub->stub_section = s;
1667 stub->offset = s->size;
1668
1669 /* Allocate room for it. */
1670 s->size += 16;
1671 return TRUE;
1672 }
1673
1674 /* H describes a symbol that needs an la25 stub. Make sure that an
1675 appropriate stub exists and point H at it. */
1676
1677 static bfd_boolean
1678 mips_elf_add_la25_stub (struct bfd_link_info *info,
1679 struct mips_elf_link_hash_entry *h)
1680 {
1681 struct mips_elf_link_hash_table *htab;
1682 struct mips_elf_la25_stub search, *stub;
1683 bfd_boolean use_trampoline_p;
1684 asection *s;
1685 bfd_vma value;
1686 void **slot;
1687
1688 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1689 of the section and if we would need no more than 2 nops. */
1690 s = h->root.root.u.def.section;
1691 value = h->root.root.u.def.value;
1692 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1693
1694 /* Describe the stub we want. */
1695 search.stub_section = NULL;
1696 search.offset = 0;
1697 search.h = h;
1698
1699 /* See if we've already created an equivalent stub. */
1700 htab = mips_elf_hash_table (info);
1701 if (htab == NULL)
1702 return FALSE;
1703
1704 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1705 if (slot == NULL)
1706 return FALSE;
1707
1708 stub = (struct mips_elf_la25_stub *) *slot;
1709 if (stub != NULL)
1710 {
1711 /* We can reuse the existing stub. */
1712 h->la25_stub = stub;
1713 return TRUE;
1714 }
1715
1716 /* Create a permanent copy of ENTRY and add it to the hash table. */
1717 stub = bfd_malloc (sizeof (search));
1718 if (stub == NULL)
1719 return FALSE;
1720 *stub = search;
1721 *slot = stub;
1722
1723 h->la25_stub = stub;
1724 return (use_trampoline_p
1725 ? mips_elf_add_la25_trampoline (stub, info)
1726 : mips_elf_add_la25_intro (stub, info));
1727 }
1728
1729 /* A mips_elf_link_hash_traverse callback that is called before sizing
1730 sections. DATA points to a mips_htab_traverse_info structure. */
1731
1732 static bfd_boolean
1733 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1734 {
1735 struct mips_htab_traverse_info *hti;
1736
1737 hti = (struct mips_htab_traverse_info *) data;
1738 if (!hti->info->relocatable)
1739 mips_elf_check_mips16_stubs (hti->info, h);
1740
1741 if (mips_elf_local_pic_function_p (h))
1742 {
1743 /* PR 12845: If H is in a section that has been garbage
1744 collected it will have its output section set to *ABS*. */
1745 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1746 return TRUE;
1747
1748 /* H is a function that might need $25 to be valid on entry.
1749 If we're creating a non-PIC relocatable object, mark H as
1750 being PIC. If we're creating a non-relocatable object with
1751 non-PIC branches and jumps to H, make sure that H has an la25
1752 stub. */
1753 if (hti->info->relocatable)
1754 {
1755 if (!PIC_OBJECT_P (hti->output_bfd))
1756 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1757 }
1758 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1759 {
1760 hti->error = TRUE;
1761 return FALSE;
1762 }
1763 }
1764 return TRUE;
1765 }
1766 \f
1767 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1768 Most mips16 instructions are 16 bits, but these instructions
1769 are 32 bits.
1770
1771 The format of these instructions is:
1772
1773 +--------------+--------------------------------+
1774 | JALX | X| Imm 20:16 | Imm 25:21 |
1775 +--------------+--------------------------------+
1776 | Immediate 15:0 |
1777 +-----------------------------------------------+
1778
1779 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1780 Note that the immediate value in the first word is swapped.
1781
1782 When producing a relocatable object file, R_MIPS16_26 is
1783 handled mostly like R_MIPS_26. In particular, the addend is
1784 stored as a straight 26-bit value in a 32-bit instruction.
1785 (gas makes life simpler for itself by never adjusting a
1786 R_MIPS16_26 reloc to be against a section, so the addend is
1787 always zero). However, the 32 bit instruction is stored as 2
1788 16-bit values, rather than a single 32-bit value. In a
1789 big-endian file, the result is the same; in a little-endian
1790 file, the two 16-bit halves of the 32 bit value are swapped.
1791 This is so that a disassembler can recognize the jal
1792 instruction.
1793
1794 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1795 instruction stored as two 16-bit values. The addend A is the
1796 contents of the targ26 field. The calculation is the same as
1797 R_MIPS_26. When storing the calculated value, reorder the
1798 immediate value as shown above, and don't forget to store the
1799 value as two 16-bit values.
1800
1801 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1802 defined as
1803
1804 big-endian:
1805 +--------+----------------------+
1806 | | |
1807 | | targ26-16 |
1808 |31 26|25 0|
1809 +--------+----------------------+
1810
1811 little-endian:
1812 +----------+------+-------------+
1813 | | | |
1814 | sub1 | | sub2 |
1815 |0 9|10 15|16 31|
1816 +----------+--------------------+
1817 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1818 ((sub1 << 16) | sub2)).
1819
1820 When producing a relocatable object file, the calculation is
1821 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1822 When producing a fully linked file, the calculation is
1823 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1824 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1825
1826 The table below lists the other MIPS16 instruction relocations.
1827 Each one is calculated in the same way as the non-MIPS16 relocation
1828 given on the right, but using the extended MIPS16 layout of 16-bit
1829 immediate fields:
1830
1831 R_MIPS16_GPREL R_MIPS_GPREL16
1832 R_MIPS16_GOT16 R_MIPS_GOT16
1833 R_MIPS16_CALL16 R_MIPS_CALL16
1834 R_MIPS16_HI16 R_MIPS_HI16
1835 R_MIPS16_LO16 R_MIPS_LO16
1836
1837 A typical instruction will have a format like this:
1838
1839 +--------------+--------------------------------+
1840 | EXTEND | Imm 10:5 | Imm 15:11 |
1841 +--------------+--------------------------------+
1842 | Major | rx | ry | Imm 4:0 |
1843 +--------------+--------------------------------+
1844
1845 EXTEND is the five bit value 11110. Major is the instruction
1846 opcode.
1847
1848 All we need to do here is shuffle the bits appropriately.
1849 As above, the two 16-bit halves must be swapped on a
1850 little-endian system. */
1851
1852 static inline bfd_boolean
1853 mips16_reloc_p (int r_type)
1854 {
1855 switch (r_type)
1856 {
1857 case R_MIPS16_26:
1858 case R_MIPS16_GPREL:
1859 case R_MIPS16_GOT16:
1860 case R_MIPS16_CALL16:
1861 case R_MIPS16_HI16:
1862 case R_MIPS16_LO16:
1863 return TRUE;
1864
1865 default:
1866 return FALSE;
1867 }
1868 }
1869
1870 /* Check if a microMIPS reloc. */
1871
1872 static inline bfd_boolean
1873 micromips_reloc_p (unsigned int r_type)
1874 {
1875 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
1876 }
1877
1878 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
1879 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
1880 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
1881
1882 static inline bfd_boolean
1883 micromips_reloc_shuffle_p (unsigned int r_type)
1884 {
1885 return (micromips_reloc_p (r_type)
1886 && r_type != R_MICROMIPS_PC7_S1
1887 && r_type != R_MICROMIPS_PC10_S1);
1888 }
1889
1890 static inline bfd_boolean
1891 got16_reloc_p (int r_type)
1892 {
1893 return (r_type == R_MIPS_GOT16
1894 || r_type == R_MIPS16_GOT16
1895 || r_type == R_MICROMIPS_GOT16);
1896 }
1897
1898 static inline bfd_boolean
1899 call16_reloc_p (int r_type)
1900 {
1901 return (r_type == R_MIPS_CALL16
1902 || r_type == R_MIPS16_CALL16
1903 || r_type == R_MICROMIPS_CALL16);
1904 }
1905
1906 static inline bfd_boolean
1907 got_disp_reloc_p (unsigned int r_type)
1908 {
1909 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
1910 }
1911
1912 static inline bfd_boolean
1913 got_page_reloc_p (unsigned int r_type)
1914 {
1915 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
1916 }
1917
1918 static inline bfd_boolean
1919 got_ofst_reloc_p (unsigned int r_type)
1920 {
1921 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
1922 }
1923
1924 static inline bfd_boolean
1925 got_hi16_reloc_p (unsigned int r_type)
1926 {
1927 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
1928 }
1929
1930 static inline bfd_boolean
1931 got_lo16_reloc_p (unsigned int r_type)
1932 {
1933 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
1934 }
1935
1936 static inline bfd_boolean
1937 call_hi16_reloc_p (unsigned int r_type)
1938 {
1939 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
1940 }
1941
1942 static inline bfd_boolean
1943 call_lo16_reloc_p (unsigned int r_type)
1944 {
1945 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
1946 }
1947
1948 static inline bfd_boolean
1949 hi16_reloc_p (int r_type)
1950 {
1951 return (r_type == R_MIPS_HI16
1952 || r_type == R_MIPS16_HI16
1953 || r_type == R_MICROMIPS_HI16);
1954 }
1955
1956 static inline bfd_boolean
1957 lo16_reloc_p (int r_type)
1958 {
1959 return (r_type == R_MIPS_LO16
1960 || r_type == R_MIPS16_LO16
1961 || r_type == R_MICROMIPS_LO16);
1962 }
1963
1964 static inline bfd_boolean
1965 mips16_call_reloc_p (int r_type)
1966 {
1967 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1968 }
1969
1970 static inline bfd_boolean
1971 jal_reloc_p (int r_type)
1972 {
1973 return (r_type == R_MIPS_26
1974 || r_type == R_MIPS16_26
1975 || r_type == R_MICROMIPS_26_S1);
1976 }
1977
1978 static inline bfd_boolean
1979 micromips_branch_reloc_p (int r_type)
1980 {
1981 return (r_type == R_MICROMIPS_26_S1
1982 || r_type == R_MICROMIPS_PC16_S1
1983 || r_type == R_MICROMIPS_PC10_S1
1984 || r_type == R_MICROMIPS_PC7_S1);
1985 }
1986
1987 static inline bfd_boolean
1988 tls_gd_reloc_p (unsigned int r_type)
1989 {
1990 return r_type == R_MIPS_TLS_GD || r_type == R_MICROMIPS_TLS_GD;
1991 }
1992
1993 static inline bfd_boolean
1994 tls_ldm_reloc_p (unsigned int r_type)
1995 {
1996 return r_type == R_MIPS_TLS_LDM || r_type == R_MICROMIPS_TLS_LDM;
1997 }
1998
1999 static inline bfd_boolean
2000 tls_gottprel_reloc_p (unsigned int r_type)
2001 {
2002 return r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MICROMIPS_TLS_GOTTPREL;
2003 }
2004
2005 void
2006 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2007 bfd_boolean jal_shuffle, bfd_byte *data)
2008 {
2009 bfd_vma first, second, val;
2010
2011 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2012 return;
2013
2014 /* Pick up the first and second halfwords of the instruction. */
2015 first = bfd_get_16 (abfd, data);
2016 second = bfd_get_16 (abfd, data + 2);
2017 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2018 val = first << 16 | second;
2019 else if (r_type != R_MIPS16_26)
2020 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2021 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2022 else
2023 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2024 | ((first & 0x1f) << 21) | second);
2025 bfd_put_32 (abfd, val, data);
2026 }
2027
2028 void
2029 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2030 bfd_boolean jal_shuffle, bfd_byte *data)
2031 {
2032 bfd_vma first, second, val;
2033
2034 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2035 return;
2036
2037 val = bfd_get_32 (abfd, data);
2038 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2039 {
2040 second = val & 0xffff;
2041 first = val >> 16;
2042 }
2043 else if (r_type != R_MIPS16_26)
2044 {
2045 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2046 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2047 }
2048 else
2049 {
2050 second = val & 0xffff;
2051 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2052 | ((val >> 21) & 0x1f);
2053 }
2054 bfd_put_16 (abfd, second, data + 2);
2055 bfd_put_16 (abfd, first, data);
2056 }
2057
2058 bfd_reloc_status_type
2059 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2060 arelent *reloc_entry, asection *input_section,
2061 bfd_boolean relocatable, void *data, bfd_vma gp)
2062 {
2063 bfd_vma relocation;
2064 bfd_signed_vma val;
2065 bfd_reloc_status_type status;
2066
2067 if (bfd_is_com_section (symbol->section))
2068 relocation = 0;
2069 else
2070 relocation = symbol->value;
2071
2072 relocation += symbol->section->output_section->vma;
2073 relocation += symbol->section->output_offset;
2074
2075 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2076 return bfd_reloc_outofrange;
2077
2078 /* Set val to the offset into the section or symbol. */
2079 val = reloc_entry->addend;
2080
2081 _bfd_mips_elf_sign_extend (val, 16);
2082
2083 /* Adjust val for the final section location and GP value. If we
2084 are producing relocatable output, we don't want to do this for
2085 an external symbol. */
2086 if (! relocatable
2087 || (symbol->flags & BSF_SECTION_SYM) != 0)
2088 val += relocation - gp;
2089
2090 if (reloc_entry->howto->partial_inplace)
2091 {
2092 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2093 (bfd_byte *) data
2094 + reloc_entry->address);
2095 if (status != bfd_reloc_ok)
2096 return status;
2097 }
2098 else
2099 reloc_entry->addend = val;
2100
2101 if (relocatable)
2102 reloc_entry->address += input_section->output_offset;
2103
2104 return bfd_reloc_ok;
2105 }
2106
2107 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2108 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2109 that contains the relocation field and DATA points to the start of
2110 INPUT_SECTION. */
2111
2112 struct mips_hi16
2113 {
2114 struct mips_hi16 *next;
2115 bfd_byte *data;
2116 asection *input_section;
2117 arelent rel;
2118 };
2119
2120 /* FIXME: This should not be a static variable. */
2121
2122 static struct mips_hi16 *mips_hi16_list;
2123
2124 /* A howto special_function for REL *HI16 relocations. We can only
2125 calculate the correct value once we've seen the partnering
2126 *LO16 relocation, so just save the information for later.
2127
2128 The ABI requires that the *LO16 immediately follow the *HI16.
2129 However, as a GNU extension, we permit an arbitrary number of
2130 *HI16s to be associated with a single *LO16. This significantly
2131 simplies the relocation handling in gcc. */
2132
2133 bfd_reloc_status_type
2134 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2135 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2136 asection *input_section, bfd *output_bfd,
2137 char **error_message ATTRIBUTE_UNUSED)
2138 {
2139 struct mips_hi16 *n;
2140
2141 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2142 return bfd_reloc_outofrange;
2143
2144 n = bfd_malloc (sizeof *n);
2145 if (n == NULL)
2146 return bfd_reloc_outofrange;
2147
2148 n->next = mips_hi16_list;
2149 n->data = data;
2150 n->input_section = input_section;
2151 n->rel = *reloc_entry;
2152 mips_hi16_list = n;
2153
2154 if (output_bfd != NULL)
2155 reloc_entry->address += input_section->output_offset;
2156
2157 return bfd_reloc_ok;
2158 }
2159
2160 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2161 like any other 16-bit relocation when applied to global symbols, but is
2162 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2163
2164 bfd_reloc_status_type
2165 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2166 void *data, asection *input_section,
2167 bfd *output_bfd, char **error_message)
2168 {
2169 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2170 || bfd_is_und_section (bfd_get_section (symbol))
2171 || bfd_is_com_section (bfd_get_section (symbol)))
2172 /* The relocation is against a global symbol. */
2173 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2174 input_section, output_bfd,
2175 error_message);
2176
2177 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2178 input_section, output_bfd, error_message);
2179 }
2180
2181 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2182 is a straightforward 16 bit inplace relocation, but we must deal with
2183 any partnering high-part relocations as well. */
2184
2185 bfd_reloc_status_type
2186 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2187 void *data, asection *input_section,
2188 bfd *output_bfd, char **error_message)
2189 {
2190 bfd_vma vallo;
2191 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2192
2193 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2194 return bfd_reloc_outofrange;
2195
2196 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2197 location);
2198 vallo = bfd_get_32 (abfd, location);
2199 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2200 location);
2201
2202 while (mips_hi16_list != NULL)
2203 {
2204 bfd_reloc_status_type ret;
2205 struct mips_hi16 *hi;
2206
2207 hi = mips_hi16_list;
2208
2209 /* R_MIPS*_GOT16 relocations are something of a special case. We
2210 want to install the addend in the same way as for a R_MIPS*_HI16
2211 relocation (with a rightshift of 16). However, since GOT16
2212 relocations can also be used with global symbols, their howto
2213 has a rightshift of 0. */
2214 if (hi->rel.howto->type == R_MIPS_GOT16)
2215 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2216 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2217 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2218 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2219 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2220
2221 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2222 carry or borrow will induce a change of +1 or -1 in the high part. */
2223 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2224
2225 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2226 hi->input_section, output_bfd,
2227 error_message);
2228 if (ret != bfd_reloc_ok)
2229 return ret;
2230
2231 mips_hi16_list = hi->next;
2232 free (hi);
2233 }
2234
2235 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2236 input_section, output_bfd,
2237 error_message);
2238 }
2239
2240 /* A generic howto special_function. This calculates and installs the
2241 relocation itself, thus avoiding the oft-discussed problems in
2242 bfd_perform_relocation and bfd_install_relocation. */
2243
2244 bfd_reloc_status_type
2245 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2246 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2247 asection *input_section, bfd *output_bfd,
2248 char **error_message ATTRIBUTE_UNUSED)
2249 {
2250 bfd_signed_vma val;
2251 bfd_reloc_status_type status;
2252 bfd_boolean relocatable;
2253
2254 relocatable = (output_bfd != NULL);
2255
2256 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2257 return bfd_reloc_outofrange;
2258
2259 /* Build up the field adjustment in VAL. */
2260 val = 0;
2261 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2262 {
2263 /* Either we're calculating the final field value or we have a
2264 relocation against a section symbol. Add in the section's
2265 offset or address. */
2266 val += symbol->section->output_section->vma;
2267 val += symbol->section->output_offset;
2268 }
2269
2270 if (!relocatable)
2271 {
2272 /* We're calculating the final field value. Add in the symbol's value
2273 and, if pc-relative, subtract the address of the field itself. */
2274 val += symbol->value;
2275 if (reloc_entry->howto->pc_relative)
2276 {
2277 val -= input_section->output_section->vma;
2278 val -= input_section->output_offset;
2279 val -= reloc_entry->address;
2280 }
2281 }
2282
2283 /* VAL is now the final adjustment. If we're keeping this relocation
2284 in the output file, and if the relocation uses a separate addend,
2285 we just need to add VAL to that addend. Otherwise we need to add
2286 VAL to the relocation field itself. */
2287 if (relocatable && !reloc_entry->howto->partial_inplace)
2288 reloc_entry->addend += val;
2289 else
2290 {
2291 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2292
2293 /* Add in the separate addend, if any. */
2294 val += reloc_entry->addend;
2295
2296 /* Add VAL to the relocation field. */
2297 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2298 location);
2299 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2300 location);
2301 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2302 location);
2303
2304 if (status != bfd_reloc_ok)
2305 return status;
2306 }
2307
2308 if (relocatable)
2309 reloc_entry->address += input_section->output_offset;
2310
2311 return bfd_reloc_ok;
2312 }
2313 \f
2314 /* Swap an entry in a .gptab section. Note that these routines rely
2315 on the equivalence of the two elements of the union. */
2316
2317 static void
2318 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2319 Elf32_gptab *in)
2320 {
2321 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2322 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2323 }
2324
2325 static void
2326 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2327 Elf32_External_gptab *ex)
2328 {
2329 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2330 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2331 }
2332
2333 static void
2334 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2335 Elf32_External_compact_rel *ex)
2336 {
2337 H_PUT_32 (abfd, in->id1, ex->id1);
2338 H_PUT_32 (abfd, in->num, ex->num);
2339 H_PUT_32 (abfd, in->id2, ex->id2);
2340 H_PUT_32 (abfd, in->offset, ex->offset);
2341 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2342 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2343 }
2344
2345 static void
2346 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2347 Elf32_External_crinfo *ex)
2348 {
2349 unsigned long l;
2350
2351 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2352 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2353 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2354 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2355 H_PUT_32 (abfd, l, ex->info);
2356 H_PUT_32 (abfd, in->konst, ex->konst);
2357 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2358 }
2359 \f
2360 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2361 routines swap this structure in and out. They are used outside of
2362 BFD, so they are globally visible. */
2363
2364 void
2365 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2366 Elf32_RegInfo *in)
2367 {
2368 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2369 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2370 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2371 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2372 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2373 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2374 }
2375
2376 void
2377 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2378 Elf32_External_RegInfo *ex)
2379 {
2380 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2381 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2382 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2383 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2384 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2385 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2386 }
2387
2388 /* In the 64 bit ABI, the .MIPS.options section holds register
2389 information in an Elf64_Reginfo structure. These routines swap
2390 them in and out. They are globally visible because they are used
2391 outside of BFD. These routines are here so that gas can call them
2392 without worrying about whether the 64 bit ABI has been included. */
2393
2394 void
2395 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2396 Elf64_Internal_RegInfo *in)
2397 {
2398 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2399 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2400 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2401 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2402 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2403 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2404 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2405 }
2406
2407 void
2408 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2409 Elf64_External_RegInfo *ex)
2410 {
2411 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2412 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2413 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2414 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2415 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2416 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2417 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2418 }
2419
2420 /* Swap in an options header. */
2421
2422 void
2423 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2424 Elf_Internal_Options *in)
2425 {
2426 in->kind = H_GET_8 (abfd, ex->kind);
2427 in->size = H_GET_8 (abfd, ex->size);
2428 in->section = H_GET_16 (abfd, ex->section);
2429 in->info = H_GET_32 (abfd, ex->info);
2430 }
2431
2432 /* Swap out an options header. */
2433
2434 void
2435 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2436 Elf_External_Options *ex)
2437 {
2438 H_PUT_8 (abfd, in->kind, ex->kind);
2439 H_PUT_8 (abfd, in->size, ex->size);
2440 H_PUT_16 (abfd, in->section, ex->section);
2441 H_PUT_32 (abfd, in->info, ex->info);
2442 }
2443 \f
2444 /* This function is called via qsort() to sort the dynamic relocation
2445 entries by increasing r_symndx value. */
2446
2447 static int
2448 sort_dynamic_relocs (const void *arg1, const void *arg2)
2449 {
2450 Elf_Internal_Rela int_reloc1;
2451 Elf_Internal_Rela int_reloc2;
2452 int diff;
2453
2454 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2455 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2456
2457 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2458 if (diff != 0)
2459 return diff;
2460
2461 if (int_reloc1.r_offset < int_reloc2.r_offset)
2462 return -1;
2463 if (int_reloc1.r_offset > int_reloc2.r_offset)
2464 return 1;
2465 return 0;
2466 }
2467
2468 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2469
2470 static int
2471 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2472 const void *arg2 ATTRIBUTE_UNUSED)
2473 {
2474 #ifdef BFD64
2475 Elf_Internal_Rela int_reloc1[3];
2476 Elf_Internal_Rela int_reloc2[3];
2477
2478 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2479 (reldyn_sorting_bfd, arg1, int_reloc1);
2480 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2481 (reldyn_sorting_bfd, arg2, int_reloc2);
2482
2483 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2484 return -1;
2485 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2486 return 1;
2487
2488 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2489 return -1;
2490 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2491 return 1;
2492 return 0;
2493 #else
2494 abort ();
2495 #endif
2496 }
2497
2498
2499 /* This routine is used to write out ECOFF debugging external symbol
2500 information. It is called via mips_elf_link_hash_traverse. The
2501 ECOFF external symbol information must match the ELF external
2502 symbol information. Unfortunately, at this point we don't know
2503 whether a symbol is required by reloc information, so the two
2504 tables may wind up being different. We must sort out the external
2505 symbol information before we can set the final size of the .mdebug
2506 section, and we must set the size of the .mdebug section before we
2507 can relocate any sections, and we can't know which symbols are
2508 required by relocation until we relocate the sections.
2509 Fortunately, it is relatively unlikely that any symbol will be
2510 stripped but required by a reloc. In particular, it can not happen
2511 when generating a final executable. */
2512
2513 static bfd_boolean
2514 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2515 {
2516 struct extsym_info *einfo = data;
2517 bfd_boolean strip;
2518 asection *sec, *output_section;
2519
2520 if (h->root.indx == -2)
2521 strip = FALSE;
2522 else if ((h->root.def_dynamic
2523 || h->root.ref_dynamic
2524 || h->root.type == bfd_link_hash_new)
2525 && !h->root.def_regular
2526 && !h->root.ref_regular)
2527 strip = TRUE;
2528 else if (einfo->info->strip == strip_all
2529 || (einfo->info->strip == strip_some
2530 && bfd_hash_lookup (einfo->info->keep_hash,
2531 h->root.root.root.string,
2532 FALSE, FALSE) == NULL))
2533 strip = TRUE;
2534 else
2535 strip = FALSE;
2536
2537 if (strip)
2538 return TRUE;
2539
2540 if (h->esym.ifd == -2)
2541 {
2542 h->esym.jmptbl = 0;
2543 h->esym.cobol_main = 0;
2544 h->esym.weakext = 0;
2545 h->esym.reserved = 0;
2546 h->esym.ifd = ifdNil;
2547 h->esym.asym.value = 0;
2548 h->esym.asym.st = stGlobal;
2549
2550 if (h->root.root.type == bfd_link_hash_undefined
2551 || h->root.root.type == bfd_link_hash_undefweak)
2552 {
2553 const char *name;
2554
2555 /* Use undefined class. Also, set class and type for some
2556 special symbols. */
2557 name = h->root.root.root.string;
2558 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2559 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2560 {
2561 h->esym.asym.sc = scData;
2562 h->esym.asym.st = stLabel;
2563 h->esym.asym.value = 0;
2564 }
2565 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2566 {
2567 h->esym.asym.sc = scAbs;
2568 h->esym.asym.st = stLabel;
2569 h->esym.asym.value =
2570 mips_elf_hash_table (einfo->info)->procedure_count;
2571 }
2572 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2573 {
2574 h->esym.asym.sc = scAbs;
2575 h->esym.asym.st = stLabel;
2576 h->esym.asym.value = elf_gp (einfo->abfd);
2577 }
2578 else
2579 h->esym.asym.sc = scUndefined;
2580 }
2581 else if (h->root.root.type != bfd_link_hash_defined
2582 && h->root.root.type != bfd_link_hash_defweak)
2583 h->esym.asym.sc = scAbs;
2584 else
2585 {
2586 const char *name;
2587
2588 sec = h->root.root.u.def.section;
2589 output_section = sec->output_section;
2590
2591 /* When making a shared library and symbol h is the one from
2592 the another shared library, OUTPUT_SECTION may be null. */
2593 if (output_section == NULL)
2594 h->esym.asym.sc = scUndefined;
2595 else
2596 {
2597 name = bfd_section_name (output_section->owner, output_section);
2598
2599 if (strcmp (name, ".text") == 0)
2600 h->esym.asym.sc = scText;
2601 else if (strcmp (name, ".data") == 0)
2602 h->esym.asym.sc = scData;
2603 else if (strcmp (name, ".sdata") == 0)
2604 h->esym.asym.sc = scSData;
2605 else if (strcmp (name, ".rodata") == 0
2606 || strcmp (name, ".rdata") == 0)
2607 h->esym.asym.sc = scRData;
2608 else if (strcmp (name, ".bss") == 0)
2609 h->esym.asym.sc = scBss;
2610 else if (strcmp (name, ".sbss") == 0)
2611 h->esym.asym.sc = scSBss;
2612 else if (strcmp (name, ".init") == 0)
2613 h->esym.asym.sc = scInit;
2614 else if (strcmp (name, ".fini") == 0)
2615 h->esym.asym.sc = scFini;
2616 else
2617 h->esym.asym.sc = scAbs;
2618 }
2619 }
2620
2621 h->esym.asym.reserved = 0;
2622 h->esym.asym.index = indexNil;
2623 }
2624
2625 if (h->root.root.type == bfd_link_hash_common)
2626 h->esym.asym.value = h->root.root.u.c.size;
2627 else if (h->root.root.type == bfd_link_hash_defined
2628 || h->root.root.type == bfd_link_hash_defweak)
2629 {
2630 if (h->esym.asym.sc == scCommon)
2631 h->esym.asym.sc = scBss;
2632 else if (h->esym.asym.sc == scSCommon)
2633 h->esym.asym.sc = scSBss;
2634
2635 sec = h->root.root.u.def.section;
2636 output_section = sec->output_section;
2637 if (output_section != NULL)
2638 h->esym.asym.value = (h->root.root.u.def.value
2639 + sec->output_offset
2640 + output_section->vma);
2641 else
2642 h->esym.asym.value = 0;
2643 }
2644 else
2645 {
2646 struct mips_elf_link_hash_entry *hd = h;
2647
2648 while (hd->root.root.type == bfd_link_hash_indirect)
2649 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2650
2651 if (hd->needs_lazy_stub)
2652 {
2653 /* Set type and value for a symbol with a function stub. */
2654 h->esym.asym.st = stProc;
2655 sec = hd->root.root.u.def.section;
2656 if (sec == NULL)
2657 h->esym.asym.value = 0;
2658 else
2659 {
2660 output_section = sec->output_section;
2661 if (output_section != NULL)
2662 h->esym.asym.value = (hd->root.plt.offset
2663 + sec->output_offset
2664 + output_section->vma);
2665 else
2666 h->esym.asym.value = 0;
2667 }
2668 }
2669 }
2670
2671 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2672 h->root.root.root.string,
2673 &h->esym))
2674 {
2675 einfo->failed = TRUE;
2676 return FALSE;
2677 }
2678
2679 return TRUE;
2680 }
2681
2682 /* A comparison routine used to sort .gptab entries. */
2683
2684 static int
2685 gptab_compare (const void *p1, const void *p2)
2686 {
2687 const Elf32_gptab *a1 = p1;
2688 const Elf32_gptab *a2 = p2;
2689
2690 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2691 }
2692 \f
2693 /* Functions to manage the got entry hash table. */
2694
2695 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2696 hash number. */
2697
2698 static INLINE hashval_t
2699 mips_elf_hash_bfd_vma (bfd_vma addr)
2700 {
2701 #ifdef BFD64
2702 return addr + (addr >> 32);
2703 #else
2704 return addr;
2705 #endif
2706 }
2707
2708 /* got_entries only match if they're identical, except for gotidx, so
2709 use all fields to compute the hash, and compare the appropriate
2710 union members. */
2711
2712 static hashval_t
2713 mips_elf_got_entry_hash (const void *entry_)
2714 {
2715 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2716
2717 return entry->symndx
2718 + ((entry->tls_type & GOT_TLS_LDM) << 17)
2719 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2720 : entry->abfd->id
2721 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2722 : entry->d.h->root.root.root.hash));
2723 }
2724
2725 static int
2726 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2727 {
2728 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2729 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2730
2731 /* An LDM entry can only match another LDM entry. */
2732 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2733 return 0;
2734
2735 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2736 && (! e1->abfd ? e1->d.address == e2->d.address
2737 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2738 : e1->d.h == e2->d.h);
2739 }
2740
2741 /* multi_got_entries are still a match in the case of global objects,
2742 even if the input bfd in which they're referenced differs, so the
2743 hash computation and compare functions are adjusted
2744 accordingly. */
2745
2746 static hashval_t
2747 mips_elf_multi_got_entry_hash (const void *entry_)
2748 {
2749 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2750
2751 return entry->symndx
2752 + (! entry->abfd
2753 ? mips_elf_hash_bfd_vma (entry->d.address)
2754 : entry->symndx >= 0
2755 ? ((entry->tls_type & GOT_TLS_LDM)
2756 ? (GOT_TLS_LDM << 17)
2757 : (entry->abfd->id
2758 + mips_elf_hash_bfd_vma (entry->d.addend)))
2759 : entry->d.h->root.root.root.hash);
2760 }
2761
2762 static int
2763 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2764 {
2765 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2766 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2767
2768 /* Any two LDM entries match. */
2769 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2770 return 1;
2771
2772 /* Nothing else matches an LDM entry. */
2773 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2774 return 0;
2775
2776 return e1->symndx == e2->symndx
2777 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2778 : e1->abfd == NULL || e2->abfd == NULL
2779 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2780 : e1->d.h == e2->d.h);
2781 }
2782
2783 static hashval_t
2784 mips_got_page_entry_hash (const void *entry_)
2785 {
2786 const struct mips_got_page_entry *entry;
2787
2788 entry = (const struct mips_got_page_entry *) entry_;
2789 return entry->abfd->id + entry->symndx;
2790 }
2791
2792 static int
2793 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2794 {
2795 const struct mips_got_page_entry *entry1, *entry2;
2796
2797 entry1 = (const struct mips_got_page_entry *) entry1_;
2798 entry2 = (const struct mips_got_page_entry *) entry2_;
2799 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2800 }
2801 \f
2802 /* Return the dynamic relocation section. If it doesn't exist, try to
2803 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2804 if creation fails. */
2805
2806 static asection *
2807 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2808 {
2809 const char *dname;
2810 asection *sreloc;
2811 bfd *dynobj;
2812
2813 dname = MIPS_ELF_REL_DYN_NAME (info);
2814 dynobj = elf_hash_table (info)->dynobj;
2815 sreloc = bfd_get_section_by_name (dynobj, dname);
2816 if (sreloc == NULL && create_p)
2817 {
2818 sreloc = bfd_make_section_with_flags (dynobj, dname,
2819 (SEC_ALLOC
2820 | SEC_LOAD
2821 | SEC_HAS_CONTENTS
2822 | SEC_IN_MEMORY
2823 | SEC_LINKER_CREATED
2824 | SEC_READONLY));
2825 if (sreloc == NULL
2826 || ! bfd_set_section_alignment (dynobj, sreloc,
2827 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2828 return NULL;
2829 }
2830 return sreloc;
2831 }
2832
2833 /* Count the number of relocations needed for a TLS GOT entry, with
2834 access types from TLS_TYPE, and symbol H (or a local symbol if H
2835 is NULL). */
2836
2837 static int
2838 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2839 struct elf_link_hash_entry *h)
2840 {
2841 int indx = 0;
2842 int ret = 0;
2843 bfd_boolean need_relocs = FALSE;
2844 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2845
2846 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2847 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2848 indx = h->dynindx;
2849
2850 if ((info->shared || indx != 0)
2851 && (h == NULL
2852 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2853 || h->root.type != bfd_link_hash_undefweak))
2854 need_relocs = TRUE;
2855
2856 if (!need_relocs)
2857 return FALSE;
2858
2859 if (tls_type & GOT_TLS_GD)
2860 {
2861 ret++;
2862 if (indx != 0)
2863 ret++;
2864 }
2865
2866 if (tls_type & GOT_TLS_IE)
2867 ret++;
2868
2869 if ((tls_type & GOT_TLS_LDM) && info->shared)
2870 ret++;
2871
2872 return ret;
2873 }
2874
2875 /* Count the number of TLS relocations required for the GOT entry in
2876 ARG1, if it describes a local symbol. */
2877
2878 static int
2879 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2880 {
2881 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2882 struct mips_elf_count_tls_arg *arg = arg2;
2883
2884 if (entry->abfd != NULL && entry->symndx != -1)
2885 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2886
2887 return 1;
2888 }
2889
2890 /* Count the number of TLS GOT entries required for the global (or
2891 forced-local) symbol in ARG1. */
2892
2893 static int
2894 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2895 {
2896 struct mips_elf_link_hash_entry *hm
2897 = (struct mips_elf_link_hash_entry *) arg1;
2898 struct mips_elf_count_tls_arg *arg = arg2;
2899
2900 if (hm->tls_type & GOT_TLS_GD)
2901 arg->needed += 2;
2902 if (hm->tls_type & GOT_TLS_IE)
2903 arg->needed += 1;
2904
2905 return 1;
2906 }
2907
2908 /* Count the number of TLS relocations required for the global (or
2909 forced-local) symbol in ARG1. */
2910
2911 static int
2912 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2913 {
2914 struct mips_elf_link_hash_entry *hm
2915 = (struct mips_elf_link_hash_entry *) arg1;
2916 struct mips_elf_count_tls_arg *arg = arg2;
2917
2918 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2919
2920 return 1;
2921 }
2922
2923 /* Output a simple dynamic relocation into SRELOC. */
2924
2925 static void
2926 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2927 asection *sreloc,
2928 unsigned long reloc_index,
2929 unsigned long indx,
2930 int r_type,
2931 bfd_vma offset)
2932 {
2933 Elf_Internal_Rela rel[3];
2934
2935 memset (rel, 0, sizeof (rel));
2936
2937 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2938 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2939
2940 if (ABI_64_P (output_bfd))
2941 {
2942 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2943 (output_bfd, &rel[0],
2944 (sreloc->contents
2945 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
2946 }
2947 else
2948 bfd_elf32_swap_reloc_out
2949 (output_bfd, &rel[0],
2950 (sreloc->contents
2951 + reloc_index * sizeof (Elf32_External_Rel)));
2952 }
2953
2954 /* Initialize a set of TLS GOT entries for one symbol. */
2955
2956 static void
2957 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2958 unsigned char *tls_type_p,
2959 struct bfd_link_info *info,
2960 struct mips_elf_link_hash_entry *h,
2961 bfd_vma value)
2962 {
2963 struct mips_elf_link_hash_table *htab;
2964 int indx;
2965 asection *sreloc, *sgot;
2966 bfd_vma offset, offset2;
2967 bfd_boolean need_relocs = FALSE;
2968
2969 htab = mips_elf_hash_table (info);
2970 if (htab == NULL)
2971 return;
2972
2973 sgot = htab->sgot;
2974
2975 indx = 0;
2976 if (h != NULL)
2977 {
2978 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2979
2980 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2981 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2982 indx = h->root.dynindx;
2983 }
2984
2985 if (*tls_type_p & GOT_TLS_DONE)
2986 return;
2987
2988 if ((info->shared || indx != 0)
2989 && (h == NULL
2990 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2991 || h->root.type != bfd_link_hash_undefweak))
2992 need_relocs = TRUE;
2993
2994 /* MINUS_ONE means the symbol is not defined in this object. It may not
2995 be defined at all; assume that the value doesn't matter in that
2996 case. Otherwise complain if we would use the value. */
2997 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2998 || h->root.root.type == bfd_link_hash_undefweak);
2999
3000 /* Emit necessary relocations. */
3001 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3002
3003 /* General Dynamic. */
3004 if (*tls_type_p & GOT_TLS_GD)
3005 {
3006 offset = got_offset;
3007 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
3008
3009 if (need_relocs)
3010 {
3011 mips_elf_output_dynamic_relocation
3012 (abfd, sreloc, sreloc->reloc_count++, indx,
3013 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3014 sgot->output_offset + sgot->output_section->vma + offset);
3015
3016 if (indx)
3017 mips_elf_output_dynamic_relocation
3018 (abfd, sreloc, sreloc->reloc_count++, indx,
3019 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3020 sgot->output_offset + sgot->output_section->vma + offset2);
3021 else
3022 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3023 sgot->contents + offset2);
3024 }
3025 else
3026 {
3027 MIPS_ELF_PUT_WORD (abfd, 1,
3028 sgot->contents + offset);
3029 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3030 sgot->contents + offset2);
3031 }
3032
3033 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
3034 }
3035
3036 /* Initial Exec model. */
3037 if (*tls_type_p & GOT_TLS_IE)
3038 {
3039 offset = got_offset;
3040
3041 if (need_relocs)
3042 {
3043 if (indx == 0)
3044 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3045 sgot->contents + offset);
3046 else
3047 MIPS_ELF_PUT_WORD (abfd, 0,
3048 sgot->contents + offset);
3049
3050 mips_elf_output_dynamic_relocation
3051 (abfd, sreloc, sreloc->reloc_count++, indx,
3052 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3053 sgot->output_offset + sgot->output_section->vma + offset);
3054 }
3055 else
3056 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3057 sgot->contents + offset);
3058 }
3059
3060 if (*tls_type_p & GOT_TLS_LDM)
3061 {
3062 /* The initial offset is zero, and the LD offsets will include the
3063 bias by DTP_OFFSET. */
3064 MIPS_ELF_PUT_WORD (abfd, 0,
3065 sgot->contents + got_offset
3066 + MIPS_ELF_GOT_SIZE (abfd));
3067
3068 if (!info->shared)
3069 MIPS_ELF_PUT_WORD (abfd, 1,
3070 sgot->contents + got_offset);
3071 else
3072 mips_elf_output_dynamic_relocation
3073 (abfd, sreloc, sreloc->reloc_count++, indx,
3074 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3075 sgot->output_offset + sgot->output_section->vma + got_offset);
3076 }
3077
3078 *tls_type_p |= GOT_TLS_DONE;
3079 }
3080
3081 /* Return the GOT index to use for a relocation of type R_TYPE against
3082 a symbol accessed using TLS_TYPE models. The GOT entries for this
3083 symbol in this GOT start at GOT_INDEX. This function initializes the
3084 GOT entries and corresponding relocations. */
3085
3086 static bfd_vma
3087 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
3088 int r_type, struct bfd_link_info *info,
3089 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
3090 {
3091 BFD_ASSERT (tls_gottprel_reloc_p (r_type)
3092 || tls_gd_reloc_p (r_type)
3093 || tls_ldm_reloc_p (r_type));
3094
3095 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
3096
3097 if (tls_gottprel_reloc_p (r_type))
3098 {
3099 BFD_ASSERT (*tls_type & GOT_TLS_IE);
3100 if (*tls_type & GOT_TLS_GD)
3101 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
3102 else
3103 return got_index;
3104 }
3105
3106 if (tls_gd_reloc_p (r_type))
3107 {
3108 BFD_ASSERT (*tls_type & GOT_TLS_GD);
3109 return got_index;
3110 }
3111
3112 if (tls_ldm_reloc_p (r_type))
3113 {
3114 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
3115 return got_index;
3116 }
3117
3118 return got_index;
3119 }
3120
3121 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3122 for global symbol H. .got.plt comes before the GOT, so the offset
3123 will be negative. */
3124
3125 static bfd_vma
3126 mips_elf_gotplt_index (struct bfd_link_info *info,
3127 struct elf_link_hash_entry *h)
3128 {
3129 bfd_vma plt_index, got_address, got_value;
3130 struct mips_elf_link_hash_table *htab;
3131
3132 htab = mips_elf_hash_table (info);
3133 BFD_ASSERT (htab != NULL);
3134
3135 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3136
3137 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3138 section starts with reserved entries. */
3139 BFD_ASSERT (htab->is_vxworks);
3140
3141 /* Calculate the index of the symbol's PLT entry. */
3142 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3143
3144 /* Calculate the address of the associated .got.plt entry. */
3145 got_address = (htab->sgotplt->output_section->vma
3146 + htab->sgotplt->output_offset
3147 + plt_index * 4);
3148
3149 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3150 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3151 + htab->root.hgot->root.u.def.section->output_offset
3152 + htab->root.hgot->root.u.def.value);
3153
3154 return got_address - got_value;
3155 }
3156
3157 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3158 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3159 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3160 offset can be found. */
3161
3162 static bfd_vma
3163 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3164 bfd_vma value, unsigned long r_symndx,
3165 struct mips_elf_link_hash_entry *h, int r_type)
3166 {
3167 struct mips_elf_link_hash_table *htab;
3168 struct mips_got_entry *entry;
3169
3170 htab = mips_elf_hash_table (info);
3171 BFD_ASSERT (htab != NULL);
3172
3173 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3174 r_symndx, h, r_type);
3175 if (!entry)
3176 return MINUS_ONE;
3177
3178 if (TLS_RELOC_P (r_type))
3179 {
3180 if (entry->symndx == -1 && htab->got_info->next == NULL)
3181 /* A type (3) entry in the single-GOT case. We use the symbol's
3182 hash table entry to track the index. */
3183 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3184 r_type, info, h, value);
3185 else
3186 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3187 r_type, info, h, value);
3188 }
3189 else
3190 return entry->gotidx;
3191 }
3192
3193 /* Returns the GOT index for the global symbol indicated by H. */
3194
3195 static bfd_vma
3196 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3197 int r_type, struct bfd_link_info *info)
3198 {
3199 struct mips_elf_link_hash_table *htab;
3200 bfd_vma got_index;
3201 struct mips_got_info *g, *gg;
3202 long global_got_dynindx = 0;
3203
3204 htab = mips_elf_hash_table (info);
3205 BFD_ASSERT (htab != NULL);
3206
3207 gg = g = htab->got_info;
3208 if (g->bfd2got && ibfd)
3209 {
3210 struct mips_got_entry e, *p;
3211
3212 BFD_ASSERT (h->dynindx >= 0);
3213
3214 g = mips_elf_got_for_ibfd (g, ibfd);
3215 if (g->next != gg || TLS_RELOC_P (r_type))
3216 {
3217 e.abfd = ibfd;
3218 e.symndx = -1;
3219 e.d.h = (struct mips_elf_link_hash_entry *)h;
3220 e.tls_type = 0;
3221
3222 p = htab_find (g->got_entries, &e);
3223
3224 BFD_ASSERT (p->gotidx > 0);
3225
3226 if (TLS_RELOC_P (r_type))
3227 {
3228 bfd_vma value = MINUS_ONE;
3229 if ((h->root.type == bfd_link_hash_defined
3230 || h->root.type == bfd_link_hash_defweak)
3231 && h->root.u.def.section->output_section)
3232 value = (h->root.u.def.value
3233 + h->root.u.def.section->output_offset
3234 + h->root.u.def.section->output_section->vma);
3235
3236 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3237 info, e.d.h, value);
3238 }
3239 else
3240 return p->gotidx;
3241 }
3242 }
3243
3244 if (gg->global_gotsym != NULL)
3245 global_got_dynindx = gg->global_gotsym->dynindx;
3246
3247 if (TLS_RELOC_P (r_type))
3248 {
3249 struct mips_elf_link_hash_entry *hm
3250 = (struct mips_elf_link_hash_entry *) h;
3251 bfd_vma value = MINUS_ONE;
3252
3253 if ((h->root.type == bfd_link_hash_defined
3254 || h->root.type == bfd_link_hash_defweak)
3255 && h->root.u.def.section->output_section)
3256 value = (h->root.u.def.value
3257 + h->root.u.def.section->output_offset
3258 + h->root.u.def.section->output_section->vma);
3259
3260 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3261 r_type, info, hm, value);
3262 }
3263 else
3264 {
3265 /* Once we determine the global GOT entry with the lowest dynamic
3266 symbol table index, we must put all dynamic symbols with greater
3267 indices into the GOT. That makes it easy to calculate the GOT
3268 offset. */
3269 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3270 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3271 * MIPS_ELF_GOT_SIZE (abfd));
3272 }
3273 BFD_ASSERT (got_index < htab->sgot->size);
3274
3275 return got_index;
3276 }
3277
3278 /* Find a GOT page entry that points to within 32KB of VALUE. These
3279 entries are supposed to be placed at small offsets in the GOT, i.e.,
3280 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3281 entry could be created. If OFFSETP is nonnull, use it to return the
3282 offset of the GOT entry from VALUE. */
3283
3284 static bfd_vma
3285 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3286 bfd_vma value, bfd_vma *offsetp)
3287 {
3288 bfd_vma page, got_index;
3289 struct mips_got_entry *entry;
3290
3291 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3292 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3293 NULL, R_MIPS_GOT_PAGE);
3294
3295 if (!entry)
3296 return MINUS_ONE;
3297
3298 got_index = entry->gotidx;
3299
3300 if (offsetp)
3301 *offsetp = value - entry->d.address;
3302
3303 return got_index;
3304 }
3305
3306 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3307 EXTERNAL is true if the relocation was originally against a global
3308 symbol that binds locally. */
3309
3310 static bfd_vma
3311 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3312 bfd_vma value, bfd_boolean external)
3313 {
3314 struct mips_got_entry *entry;
3315
3316 /* GOT16 relocations against local symbols are followed by a LO16
3317 relocation; those against global symbols are not. Thus if the
3318 symbol was originally local, the GOT16 relocation should load the
3319 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3320 if (! external)
3321 value = mips_elf_high (value) << 16;
3322
3323 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3324 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3325 same in all cases. */
3326 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3327 NULL, R_MIPS_GOT16);
3328 if (entry)
3329 return entry->gotidx;
3330 else
3331 return MINUS_ONE;
3332 }
3333
3334 /* Returns the offset for the entry at the INDEXth position
3335 in the GOT. */
3336
3337 static bfd_vma
3338 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3339 bfd *input_bfd, bfd_vma got_index)
3340 {
3341 struct mips_elf_link_hash_table *htab;
3342 asection *sgot;
3343 bfd_vma gp;
3344
3345 htab = mips_elf_hash_table (info);
3346 BFD_ASSERT (htab != NULL);
3347
3348 sgot = htab->sgot;
3349 gp = _bfd_get_gp_value (output_bfd)
3350 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3351
3352 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3353 }
3354
3355 /* Create and return a local GOT entry for VALUE, which was calculated
3356 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3357 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3358 instead. */
3359
3360 static struct mips_got_entry *
3361 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3362 bfd *ibfd, bfd_vma value,
3363 unsigned long r_symndx,
3364 struct mips_elf_link_hash_entry *h,
3365 int r_type)
3366 {
3367 struct mips_got_entry entry, **loc;
3368 struct mips_got_info *g;
3369 struct mips_elf_link_hash_table *htab;
3370
3371 htab = mips_elf_hash_table (info);
3372 BFD_ASSERT (htab != NULL);
3373
3374 entry.abfd = NULL;
3375 entry.symndx = -1;
3376 entry.d.address = value;
3377 entry.tls_type = 0;
3378
3379 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
3380 if (g == NULL)
3381 {
3382 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
3383 BFD_ASSERT (g != NULL);
3384 }
3385
3386 /* This function shouldn't be called for symbols that live in the global
3387 area of the GOT. */
3388 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3389 if (TLS_RELOC_P (r_type))
3390 {
3391 struct mips_got_entry *p;
3392
3393 entry.abfd = ibfd;
3394 if (tls_ldm_reloc_p (r_type))
3395 {
3396 entry.tls_type = GOT_TLS_LDM;
3397 entry.symndx = 0;
3398 entry.d.addend = 0;
3399 }
3400 else if (h == NULL)
3401 {
3402 entry.symndx = r_symndx;
3403 entry.d.addend = 0;
3404 }
3405 else
3406 entry.d.h = h;
3407
3408 p = (struct mips_got_entry *)
3409 htab_find (g->got_entries, &entry);
3410
3411 BFD_ASSERT (p);
3412 return p;
3413 }
3414
3415 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3416 INSERT);
3417 if (*loc)
3418 return *loc;
3419
3420 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3421 entry.tls_type = 0;
3422
3423 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3424
3425 if (! *loc)
3426 return NULL;
3427
3428 memcpy (*loc, &entry, sizeof entry);
3429
3430 if (g->assigned_gotno > g->local_gotno)
3431 {
3432 (*loc)->gotidx = -1;
3433 /* We didn't allocate enough space in the GOT. */
3434 (*_bfd_error_handler)
3435 (_("not enough GOT space for local GOT entries"));
3436 bfd_set_error (bfd_error_bad_value);
3437 return NULL;
3438 }
3439
3440 MIPS_ELF_PUT_WORD (abfd, value,
3441 (htab->sgot->contents + entry.gotidx));
3442
3443 /* These GOT entries need a dynamic relocation on VxWorks. */
3444 if (htab->is_vxworks)
3445 {
3446 Elf_Internal_Rela outrel;
3447 asection *s;
3448 bfd_byte *rloc;
3449 bfd_vma got_address;
3450
3451 s = mips_elf_rel_dyn_section (info, FALSE);
3452 got_address = (htab->sgot->output_section->vma
3453 + htab->sgot->output_offset
3454 + entry.gotidx);
3455
3456 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3457 outrel.r_offset = got_address;
3458 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3459 outrel.r_addend = value;
3460 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3461 }
3462
3463 return *loc;
3464 }
3465
3466 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3467 The number might be exact or a worst-case estimate, depending on how
3468 much information is available to elf_backend_omit_section_dynsym at
3469 the current linking stage. */
3470
3471 static bfd_size_type
3472 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3473 {
3474 bfd_size_type count;
3475
3476 count = 0;
3477 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3478 {
3479 asection *p;
3480 const struct elf_backend_data *bed;
3481
3482 bed = get_elf_backend_data (output_bfd);
3483 for (p = output_bfd->sections; p ; p = p->next)
3484 if ((p->flags & SEC_EXCLUDE) == 0
3485 && (p->flags & SEC_ALLOC) != 0
3486 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3487 ++count;
3488 }
3489 return count;
3490 }
3491
3492 /* Sort the dynamic symbol table so that symbols that need GOT entries
3493 appear towards the end. */
3494
3495 static bfd_boolean
3496 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3497 {
3498 struct mips_elf_link_hash_table *htab;
3499 struct mips_elf_hash_sort_data hsd;
3500 struct mips_got_info *g;
3501
3502 if (elf_hash_table (info)->dynsymcount == 0)
3503 return TRUE;
3504
3505 htab = mips_elf_hash_table (info);
3506 BFD_ASSERT (htab != NULL);
3507
3508 g = htab->got_info;
3509 if (g == NULL)
3510 return TRUE;
3511
3512 hsd.low = NULL;
3513 hsd.max_unref_got_dynindx
3514 = hsd.min_got_dynindx
3515 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3516 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3517 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3518 elf_hash_table (info)),
3519 mips_elf_sort_hash_table_f,
3520 &hsd);
3521
3522 /* There should have been enough room in the symbol table to
3523 accommodate both the GOT and non-GOT symbols. */
3524 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3525 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3526 == elf_hash_table (info)->dynsymcount);
3527 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3528 == g->global_gotno);
3529
3530 /* Now we know which dynamic symbol has the lowest dynamic symbol
3531 table index in the GOT. */
3532 g->global_gotsym = hsd.low;
3533
3534 return TRUE;
3535 }
3536
3537 /* If H needs a GOT entry, assign it the highest available dynamic
3538 index. Otherwise, assign it the lowest available dynamic
3539 index. */
3540
3541 static bfd_boolean
3542 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3543 {
3544 struct mips_elf_hash_sort_data *hsd = data;
3545
3546 /* Symbols without dynamic symbol table entries aren't interesting
3547 at all. */
3548 if (h->root.dynindx == -1)
3549 return TRUE;
3550
3551 switch (h->global_got_area)
3552 {
3553 case GGA_NONE:
3554 h->root.dynindx = hsd->max_non_got_dynindx++;
3555 break;
3556
3557 case GGA_NORMAL:
3558 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3559
3560 h->root.dynindx = --hsd->min_got_dynindx;
3561 hsd->low = (struct elf_link_hash_entry *) h;
3562 break;
3563
3564 case GGA_RELOC_ONLY:
3565 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3566
3567 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3568 hsd->low = (struct elf_link_hash_entry *) h;
3569 h->root.dynindx = hsd->max_unref_got_dynindx++;
3570 break;
3571 }
3572
3573 return TRUE;
3574 }
3575
3576 /* If H is a symbol that needs a global GOT entry, but has a dynamic
3577 symbol table index lower than any we've seen to date, record it for
3578 posterity. FOR_CALL is true if the caller is only interested in
3579 using the GOT entry for calls. */
3580
3581 static bfd_boolean
3582 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3583 bfd *abfd, struct bfd_link_info *info,
3584 bfd_boolean for_call,
3585 unsigned char tls_flag)
3586 {
3587 struct mips_elf_link_hash_table *htab;
3588 struct mips_elf_link_hash_entry *hmips;
3589 struct mips_got_entry entry, **loc;
3590 struct mips_got_info *g;
3591
3592 htab = mips_elf_hash_table (info);
3593 BFD_ASSERT (htab != NULL);
3594
3595 hmips = (struct mips_elf_link_hash_entry *) h;
3596 if (!for_call)
3597 hmips->got_only_for_calls = FALSE;
3598
3599 /* A global symbol in the GOT must also be in the dynamic symbol
3600 table. */
3601 if (h->dynindx == -1)
3602 {
3603 switch (ELF_ST_VISIBILITY (h->other))
3604 {
3605 case STV_INTERNAL:
3606 case STV_HIDDEN:
3607 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3608 break;
3609 }
3610 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3611 return FALSE;
3612 }
3613
3614 /* Make sure we have a GOT to put this entry into. */
3615 g = htab->got_info;
3616 BFD_ASSERT (g != NULL);
3617
3618 entry.abfd = abfd;
3619 entry.symndx = -1;
3620 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3621 entry.tls_type = 0;
3622
3623 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3624 INSERT);
3625
3626 /* If we've already marked this entry as needing GOT space, we don't
3627 need to do it again. */
3628 if (*loc)
3629 {
3630 (*loc)->tls_type |= tls_flag;
3631 return TRUE;
3632 }
3633
3634 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3635
3636 if (! *loc)
3637 return FALSE;
3638
3639 entry.gotidx = -1;
3640 entry.tls_type = tls_flag;
3641
3642 memcpy (*loc, &entry, sizeof entry);
3643
3644 if (tls_flag == 0)
3645 hmips->global_got_area = GGA_NORMAL;
3646
3647 return TRUE;
3648 }
3649
3650 /* Reserve space in G for a GOT entry containing the value of symbol
3651 SYMNDX in input bfd ABDF, plus ADDEND. */
3652
3653 static bfd_boolean
3654 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3655 struct bfd_link_info *info,
3656 unsigned char tls_flag)
3657 {
3658 struct mips_elf_link_hash_table *htab;
3659 struct mips_got_info *g;
3660 struct mips_got_entry entry, **loc;
3661
3662 htab = mips_elf_hash_table (info);
3663 BFD_ASSERT (htab != NULL);
3664
3665 g = htab->got_info;
3666 BFD_ASSERT (g != NULL);
3667
3668 entry.abfd = abfd;
3669 entry.symndx = symndx;
3670 entry.d.addend = addend;
3671 entry.tls_type = tls_flag;
3672 loc = (struct mips_got_entry **)
3673 htab_find_slot (g->got_entries, &entry, INSERT);
3674
3675 if (*loc)
3676 {
3677 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3678 {
3679 g->tls_gotno += 2;
3680 (*loc)->tls_type |= tls_flag;
3681 }
3682 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3683 {
3684 g->tls_gotno += 1;
3685 (*loc)->tls_type |= tls_flag;
3686 }
3687 return TRUE;
3688 }
3689
3690 if (tls_flag != 0)
3691 {
3692 entry.gotidx = -1;
3693 entry.tls_type = tls_flag;
3694 if (tls_flag == GOT_TLS_IE)
3695 g->tls_gotno += 1;
3696 else if (tls_flag == GOT_TLS_GD)
3697 g->tls_gotno += 2;
3698 else if (g->tls_ldm_offset == MINUS_ONE)
3699 {
3700 g->tls_ldm_offset = MINUS_TWO;
3701 g->tls_gotno += 2;
3702 }
3703 }
3704 else
3705 {
3706 entry.gotidx = g->local_gotno++;
3707 entry.tls_type = 0;
3708 }
3709
3710 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3711
3712 if (! *loc)
3713 return FALSE;
3714
3715 memcpy (*loc, &entry, sizeof entry);
3716
3717 return TRUE;
3718 }
3719
3720 /* Return the maximum number of GOT page entries required for RANGE. */
3721
3722 static bfd_vma
3723 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3724 {
3725 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3726 }
3727
3728 /* Record that ABFD has a page relocation against symbol SYMNDX and
3729 that ADDEND is the addend for that relocation.
3730
3731 This function creates an upper bound on the number of GOT slots
3732 required; no attempt is made to combine references to non-overridable
3733 global symbols across multiple input files. */
3734
3735 static bfd_boolean
3736 mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3737 long symndx, bfd_signed_vma addend)
3738 {
3739 struct mips_elf_link_hash_table *htab;
3740 struct mips_got_info *g;
3741 struct mips_got_page_entry lookup, *entry;
3742 struct mips_got_page_range **range_ptr, *range;
3743 bfd_vma old_pages, new_pages;
3744 void **loc;
3745
3746 htab = mips_elf_hash_table (info);
3747 BFD_ASSERT (htab != NULL);
3748
3749 g = htab->got_info;
3750 BFD_ASSERT (g != NULL);
3751
3752 /* Find the mips_got_page_entry hash table entry for this symbol. */
3753 lookup.abfd = abfd;
3754 lookup.symndx = symndx;
3755 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3756 if (loc == NULL)
3757 return FALSE;
3758
3759 /* Create a mips_got_page_entry if this is the first time we've
3760 seen the symbol. */
3761 entry = (struct mips_got_page_entry *) *loc;
3762 if (!entry)
3763 {
3764 entry = bfd_alloc (abfd, sizeof (*entry));
3765 if (!entry)
3766 return FALSE;
3767
3768 entry->abfd = abfd;
3769 entry->symndx = symndx;
3770 entry->ranges = NULL;
3771 entry->num_pages = 0;
3772 *loc = entry;
3773 }
3774
3775 /* Skip over ranges whose maximum extent cannot share a page entry
3776 with ADDEND. */
3777 range_ptr = &entry->ranges;
3778 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3779 range_ptr = &(*range_ptr)->next;
3780
3781 /* If we scanned to the end of the list, or found a range whose
3782 minimum extent cannot share a page entry with ADDEND, create
3783 a new singleton range. */
3784 range = *range_ptr;
3785 if (!range || addend < range->min_addend - 0xffff)
3786 {
3787 range = bfd_alloc (abfd, sizeof (*range));
3788 if (!range)
3789 return FALSE;
3790
3791 range->next = *range_ptr;
3792 range->min_addend = addend;
3793 range->max_addend = addend;
3794
3795 *range_ptr = range;
3796 entry->num_pages++;
3797 g->page_gotno++;
3798 return TRUE;
3799 }
3800
3801 /* Remember how many pages the old range contributed. */
3802 old_pages = mips_elf_pages_for_range (range);
3803
3804 /* Update the ranges. */
3805 if (addend < range->min_addend)
3806 range->min_addend = addend;
3807 else if (addend > range->max_addend)
3808 {
3809 if (range->next && addend >= range->next->min_addend - 0xffff)
3810 {
3811 old_pages += mips_elf_pages_for_range (range->next);
3812 range->max_addend = range->next->max_addend;
3813 range->next = range->next->next;
3814 }
3815 else
3816 range->max_addend = addend;
3817 }
3818
3819 /* Record any change in the total estimate. */
3820 new_pages = mips_elf_pages_for_range (range);
3821 if (old_pages != new_pages)
3822 {
3823 entry->num_pages += new_pages - old_pages;
3824 g->page_gotno += new_pages - old_pages;
3825 }
3826
3827 return TRUE;
3828 }
3829
3830 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3831
3832 static void
3833 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3834 unsigned int n)
3835 {
3836 asection *s;
3837 struct mips_elf_link_hash_table *htab;
3838
3839 htab = mips_elf_hash_table (info);
3840 BFD_ASSERT (htab != NULL);
3841
3842 s = mips_elf_rel_dyn_section (info, FALSE);
3843 BFD_ASSERT (s != NULL);
3844
3845 if (htab->is_vxworks)
3846 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3847 else
3848 {
3849 if (s->size == 0)
3850 {
3851 /* Make room for a null element. */
3852 s->size += MIPS_ELF_REL_SIZE (abfd);
3853 ++s->reloc_count;
3854 }
3855 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3856 }
3857 }
3858 \f
3859 /* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3860 if the GOT entry is for an indirect or warning symbol. */
3861
3862 static int
3863 mips_elf_check_recreate_got (void **entryp, void *data)
3864 {
3865 struct mips_got_entry *entry;
3866 bfd_boolean *must_recreate;
3867
3868 entry = (struct mips_got_entry *) *entryp;
3869 must_recreate = (bfd_boolean *) data;
3870 if (entry->abfd != NULL && entry->symndx == -1)
3871 {
3872 struct mips_elf_link_hash_entry *h;
3873
3874 h = entry->d.h;
3875 if (h->root.root.type == bfd_link_hash_indirect
3876 || h->root.root.type == bfd_link_hash_warning)
3877 {
3878 *must_recreate = TRUE;
3879 return 0;
3880 }
3881 }
3882 return 1;
3883 }
3884
3885 /* A htab_traverse callback for GOT entries. Add all entries to
3886 hash table *DATA, converting entries for indirect and warning
3887 symbols into entries for the target symbol. Set *DATA to null
3888 on error. */
3889
3890 static int
3891 mips_elf_recreate_got (void **entryp, void *data)
3892 {
3893 htab_t *new_got;
3894 struct mips_got_entry *entry;
3895 void **slot;
3896
3897 new_got = (htab_t *) data;
3898 entry = (struct mips_got_entry *) *entryp;
3899 if (entry->abfd != NULL && entry->symndx == -1)
3900 {
3901 struct mips_elf_link_hash_entry *h;
3902
3903 h = entry->d.h;
3904 while (h->root.root.type == bfd_link_hash_indirect
3905 || h->root.root.type == bfd_link_hash_warning)
3906 {
3907 BFD_ASSERT (h->global_got_area == GGA_NONE);
3908 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3909 }
3910 entry->d.h = h;
3911 }
3912 slot = htab_find_slot (*new_got, entry, INSERT);
3913 if (slot == NULL)
3914 {
3915 *new_got = NULL;
3916 return 0;
3917 }
3918 if (*slot == NULL)
3919 *slot = entry;
3920 else
3921 free (entry);
3922 return 1;
3923 }
3924
3925 /* If any entries in G->got_entries are for indirect or warning symbols,
3926 replace them with entries for the target symbol. */
3927
3928 static bfd_boolean
3929 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3930 {
3931 bfd_boolean must_recreate;
3932 htab_t new_got;
3933
3934 must_recreate = FALSE;
3935 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3936 if (must_recreate)
3937 {
3938 new_got = htab_create (htab_size (g->got_entries),
3939 mips_elf_got_entry_hash,
3940 mips_elf_got_entry_eq, NULL);
3941 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3942 if (new_got == NULL)
3943 return FALSE;
3944
3945 /* Each entry in g->got_entries has either been copied to new_got
3946 or freed. Now delete the hash table itself. */
3947 htab_delete (g->got_entries);
3948 g->got_entries = new_got;
3949 }
3950 return TRUE;
3951 }
3952
3953 /* A mips_elf_link_hash_traverse callback for which DATA points
3954 to the link_info structure. Count the number of type (3) entries
3955 in the master GOT. */
3956
3957 static int
3958 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
3959 {
3960 struct bfd_link_info *info;
3961 struct mips_elf_link_hash_table *htab;
3962 struct mips_got_info *g;
3963
3964 info = (struct bfd_link_info *) data;
3965 htab = mips_elf_hash_table (info);
3966 g = htab->got_info;
3967 if (h->global_got_area != GGA_NONE)
3968 {
3969 /* Make a final decision about whether the symbol belongs in the
3970 local or global GOT. Symbols that bind locally can (and in the
3971 case of forced-local symbols, must) live in the local GOT.
3972 Those that are aren't in the dynamic symbol table must also
3973 live in the local GOT.
3974
3975 Note that the former condition does not always imply the
3976 latter: symbols do not bind locally if they are completely
3977 undefined. We'll report undefined symbols later if appropriate. */
3978 if (h->root.dynindx == -1
3979 || (h->got_only_for_calls
3980 ? SYMBOL_CALLS_LOCAL (info, &h->root)
3981 : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3982 {
3983 /* The symbol belongs in the local GOT. We no longer need this
3984 entry if it was only used for relocations; those relocations
3985 will be against the null or section symbol instead of H. */
3986 if (h->global_got_area != GGA_RELOC_ONLY)
3987 g->local_gotno++;
3988 h->global_got_area = GGA_NONE;
3989 }
3990 else if (htab->is_vxworks
3991 && h->got_only_for_calls
3992 && h->root.plt.offset != MINUS_ONE)
3993 /* On VxWorks, calls can refer directly to the .got.plt entry;
3994 they don't need entries in the regular GOT. .got.plt entries
3995 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
3996 h->global_got_area = GGA_NONE;
3997 else
3998 {
3999 g->global_gotno++;
4000 if (h->global_got_area == GGA_RELOC_ONLY)
4001 g->reloc_only_gotno++;
4002 }
4003 }
4004 return 1;
4005 }
4006 \f
4007 /* Compute the hash value of the bfd in a bfd2got hash entry. */
4008
4009 static hashval_t
4010 mips_elf_bfd2got_entry_hash (const void *entry_)
4011 {
4012 const struct mips_elf_bfd2got_hash *entry
4013 = (struct mips_elf_bfd2got_hash *)entry_;
4014
4015 return entry->bfd->id;
4016 }
4017
4018 /* Check whether two hash entries have the same bfd. */
4019
4020 static int
4021 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
4022 {
4023 const struct mips_elf_bfd2got_hash *e1
4024 = (const struct mips_elf_bfd2got_hash *)entry1;
4025 const struct mips_elf_bfd2got_hash *e2
4026 = (const struct mips_elf_bfd2got_hash *)entry2;
4027
4028 return e1->bfd == e2->bfd;
4029 }
4030
4031 /* In a multi-got link, determine the GOT to be used for IBFD. G must
4032 be the master GOT data. */
4033
4034 static struct mips_got_info *
4035 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
4036 {
4037 struct mips_elf_bfd2got_hash e, *p;
4038
4039 if (! g->bfd2got)
4040 return g;
4041
4042 e.bfd = ibfd;
4043 p = htab_find (g->bfd2got, &e);
4044 return p ? p->g : NULL;
4045 }
4046
4047 /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
4048 Return NULL if an error occured. */
4049
4050 static struct mips_got_info *
4051 mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
4052 bfd *input_bfd)
4053 {
4054 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
4055 struct mips_got_info *g;
4056 void **bfdgotp;
4057
4058 bfdgot_entry.bfd = input_bfd;
4059 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
4060 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
4061
4062 if (bfdgot == NULL)
4063 {
4064 bfdgot = ((struct mips_elf_bfd2got_hash *)
4065 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
4066 if (bfdgot == NULL)
4067 return NULL;
4068
4069 *bfdgotp = bfdgot;
4070
4071 g = ((struct mips_got_info *)
4072 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
4073 if (g == NULL)
4074 return NULL;
4075
4076 bfdgot->bfd = input_bfd;
4077 bfdgot->g = g;
4078
4079 g->global_gotsym = NULL;
4080 g->global_gotno = 0;
4081 g->reloc_only_gotno = 0;
4082 g->local_gotno = 0;
4083 g->page_gotno = 0;
4084 g->assigned_gotno = -1;
4085 g->tls_gotno = 0;
4086 g->tls_assigned_gotno = 0;
4087 g->tls_ldm_offset = MINUS_ONE;
4088 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4089 mips_elf_multi_got_entry_eq, NULL);
4090 if (g->got_entries == NULL)
4091 return NULL;
4092
4093 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4094 mips_got_page_entry_eq, NULL);
4095 if (g->got_page_entries == NULL)
4096 return NULL;
4097
4098 g->bfd2got = NULL;
4099 g->next = NULL;
4100 }
4101
4102 return bfdgot->g;
4103 }
4104
4105 /* A htab_traverse callback for the entries in the master got.
4106 Create one separate got for each bfd that has entries in the global
4107 got, such that we can tell how many local and global entries each
4108 bfd requires. */
4109
4110 static int
4111 mips_elf_make_got_per_bfd (void **entryp, void *p)
4112 {
4113 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4114 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4115 struct mips_got_info *g;
4116
4117 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4118 if (g == NULL)
4119 {
4120 arg->obfd = NULL;
4121 return 0;
4122 }
4123
4124 /* Insert the GOT entry in the bfd's got entry hash table. */
4125 entryp = htab_find_slot (g->got_entries, entry, INSERT);
4126 if (*entryp != NULL)
4127 return 1;
4128
4129 *entryp = entry;
4130
4131 if (entry->tls_type)
4132 {
4133 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4134 g->tls_gotno += 2;
4135 if (entry->tls_type & GOT_TLS_IE)
4136 g->tls_gotno += 1;
4137 }
4138 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
4139 ++g->local_gotno;
4140 else
4141 ++g->global_gotno;
4142
4143 return 1;
4144 }
4145
4146 /* A htab_traverse callback for the page entries in the master got.
4147 Associate each page entry with the bfd's got. */
4148
4149 static int
4150 mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
4151 {
4152 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
4153 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
4154 struct mips_got_info *g;
4155
4156 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4157 if (g == NULL)
4158 {
4159 arg->obfd = NULL;
4160 return 0;
4161 }
4162
4163 /* Insert the GOT entry in the bfd's got entry hash table. */
4164 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4165 if (*entryp != NULL)
4166 return 1;
4167
4168 *entryp = entry;
4169 g->page_gotno += entry->num_pages;
4170 return 1;
4171 }
4172
4173 /* Consider merging the got described by BFD2GOT with TO, using the
4174 information given by ARG. Return -1 if this would lead to overflow,
4175 1 if they were merged successfully, and 0 if a merge failed due to
4176 lack of memory. (These values are chosen so that nonnegative return
4177 values can be returned by a htab_traverse callback.) */
4178
4179 static int
4180 mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4181 struct mips_got_info *to,
4182 struct mips_elf_got_per_bfd_arg *arg)
4183 {
4184 struct mips_got_info *from = bfd2got->g;
4185 unsigned int estimate;
4186
4187 /* Work out how many page entries we would need for the combined GOT. */
4188 estimate = arg->max_pages;
4189 if (estimate >= from->page_gotno + to->page_gotno)
4190 estimate = from->page_gotno + to->page_gotno;
4191
4192 /* And conservatively estimate how many local and TLS entries
4193 would be needed. */
4194 estimate += from->local_gotno + to->local_gotno;
4195 estimate += from->tls_gotno + to->tls_gotno;
4196
4197 /* If we're merging with the primary got, we will always have
4198 the full set of global entries. Otherwise estimate those
4199 conservatively as well. */
4200 if (to == arg->primary)
4201 estimate += arg->global_count;
4202 else
4203 estimate += from->global_gotno + to->global_gotno;
4204
4205 /* Bail out if the combined GOT might be too big. */
4206 if (estimate > arg->max_count)
4207 return -1;
4208
4209 /* Commit to the merge. Record that TO is now the bfd for this got. */
4210 bfd2got->g = to;
4211
4212 /* Transfer the bfd's got information from FROM to TO. */
4213 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4214 if (arg->obfd == NULL)
4215 return 0;
4216
4217 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4218 if (arg->obfd == NULL)
4219 return 0;
4220
4221 /* We don't have to worry about releasing memory of the actual
4222 got entries, since they're all in the master got_entries hash
4223 table anyway. */
4224 htab_delete (from->got_entries);
4225 htab_delete (from->got_page_entries);
4226 return 1;
4227 }
4228
4229 /* Attempt to merge gots of different input bfds. Try to use as much
4230 as possible of the primary got, since it doesn't require explicit
4231 dynamic relocations, but don't use bfds that would reference global
4232 symbols out of the addressable range. Failing the primary got,
4233 attempt to merge with the current got, or finish the current got
4234 and then make make the new got current. */
4235
4236 static int
4237 mips_elf_merge_gots (void **bfd2got_, void *p)
4238 {
4239 struct mips_elf_bfd2got_hash *bfd2got
4240 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4241 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4242 struct mips_got_info *g;
4243 unsigned int estimate;
4244 int result;
4245
4246 g = bfd2got->g;
4247
4248 /* Work out the number of page, local and TLS entries. */
4249 estimate = arg->max_pages;
4250 if (estimate > g->page_gotno)
4251 estimate = g->page_gotno;
4252 estimate += g->local_gotno + g->tls_gotno;
4253
4254 /* We place TLS GOT entries after both locals and globals. The globals
4255 for the primary GOT may overflow the normal GOT size limit, so be
4256 sure not to merge a GOT which requires TLS with the primary GOT in that
4257 case. This doesn't affect non-primary GOTs. */
4258 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4259
4260 if (estimate <= arg->max_count)
4261 {
4262 /* If we don't have a primary GOT, use it as
4263 a starting point for the primary GOT. */
4264 if (!arg->primary)
4265 {
4266 arg->primary = bfd2got->g;
4267 return 1;
4268 }
4269
4270 /* Try merging with the primary GOT. */
4271 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4272 if (result >= 0)
4273 return result;
4274 }
4275
4276 /* If we can merge with the last-created got, do it. */
4277 if (arg->current)
4278 {
4279 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4280 if (result >= 0)
4281 return result;
4282 }
4283
4284 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4285 fits; if it turns out that it doesn't, we'll get relocation
4286 overflows anyway. */
4287 g->next = arg->current;
4288 arg->current = g;
4289
4290 return 1;
4291 }
4292
4293 /* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4294 is null iff there is just a single GOT. */
4295
4296 static int
4297 mips_elf_initialize_tls_index (void **entryp, void *p)
4298 {
4299 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4300 struct mips_got_info *g = p;
4301 bfd_vma next_index;
4302 unsigned char tls_type;
4303
4304 /* We're only interested in TLS symbols. */
4305 if (entry->tls_type == 0)
4306 return 1;
4307
4308 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4309
4310 if (entry->symndx == -1 && g->next == NULL)
4311 {
4312 /* A type (3) got entry in the single-GOT case. We use the symbol's
4313 hash table entry to track its index. */
4314 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4315 return 1;
4316 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4317 entry->d.h->tls_got_offset = next_index;
4318 tls_type = entry->d.h->tls_type;
4319 }
4320 else
4321 {
4322 if (entry->tls_type & GOT_TLS_LDM)
4323 {
4324 /* There are separate mips_got_entry objects for each input bfd
4325 that requires an LDM entry. Make sure that all LDM entries in
4326 a GOT resolve to the same index. */
4327 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4328 {
4329 entry->gotidx = g->tls_ldm_offset;
4330 return 1;
4331 }
4332 g->tls_ldm_offset = next_index;
4333 }
4334 entry->gotidx = next_index;
4335 tls_type = entry->tls_type;
4336 }
4337
4338 /* Account for the entries we've just allocated. */
4339 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4340 g->tls_assigned_gotno += 2;
4341 if (tls_type & GOT_TLS_IE)
4342 g->tls_assigned_gotno += 1;
4343
4344 return 1;
4345 }
4346
4347 /* If passed a NULL mips_got_info in the argument, set the marker used
4348 to tell whether a global symbol needs a got entry (in the primary
4349 got) to the given VALUE.
4350
4351 If passed a pointer G to a mips_got_info in the argument (it must
4352 not be the primary GOT), compute the offset from the beginning of
4353 the (primary) GOT section to the entry in G corresponding to the
4354 global symbol. G's assigned_gotno must contain the index of the
4355 first available global GOT entry in G. VALUE must contain the size
4356 of a GOT entry in bytes. For each global GOT entry that requires a
4357 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4358 marked as not eligible for lazy resolution through a function
4359 stub. */
4360 static int
4361 mips_elf_set_global_got_offset (void **entryp, void *p)
4362 {
4363 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4364 struct mips_elf_set_global_got_offset_arg *arg
4365 = (struct mips_elf_set_global_got_offset_arg *)p;
4366 struct mips_got_info *g = arg->g;
4367
4368 if (g && entry->tls_type != GOT_NORMAL)
4369 arg->needed_relocs +=
4370 mips_tls_got_relocs (arg->info, entry->tls_type,
4371 entry->symndx == -1 ? &entry->d.h->root : NULL);
4372
4373 if (entry->abfd != NULL
4374 && entry->symndx == -1
4375 && entry->d.h->global_got_area != GGA_NONE)
4376 {
4377 if (g)
4378 {
4379 BFD_ASSERT (g->global_gotsym == NULL);
4380
4381 entry->gotidx = arg->value * (long) g->assigned_gotno++;
4382 if (arg->info->shared
4383 || (elf_hash_table (arg->info)->dynamic_sections_created
4384 && entry->d.h->root.def_dynamic
4385 && !entry->d.h->root.def_regular))
4386 ++arg->needed_relocs;
4387 }
4388 else
4389 entry->d.h->global_got_area = arg->value;
4390 }
4391
4392 return 1;
4393 }
4394
4395 /* A htab_traverse callback for GOT entries for which DATA is the
4396 bfd_link_info. Forbid any global symbols from having traditional
4397 lazy-binding stubs. */
4398
4399 static int
4400 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4401 {
4402 struct bfd_link_info *info;
4403 struct mips_elf_link_hash_table *htab;
4404 struct mips_got_entry *entry;
4405
4406 entry = (struct mips_got_entry *) *entryp;
4407 info = (struct bfd_link_info *) data;
4408 htab = mips_elf_hash_table (info);
4409 BFD_ASSERT (htab != NULL);
4410
4411 if (entry->abfd != NULL
4412 && entry->symndx == -1
4413 && entry->d.h->needs_lazy_stub)
4414 {
4415 entry->d.h->needs_lazy_stub = FALSE;
4416 htab->lazy_stub_count--;
4417 }
4418
4419 return 1;
4420 }
4421
4422 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4423 the primary GOT. */
4424 static bfd_vma
4425 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4426 {
4427 if (g->bfd2got == NULL)
4428 return 0;
4429
4430 g = mips_elf_got_for_ibfd (g, ibfd);
4431 if (! g)
4432 return 0;
4433
4434 BFD_ASSERT (g->next);
4435
4436 g = g->next;
4437
4438 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4439 * MIPS_ELF_GOT_SIZE (abfd);
4440 }
4441
4442 /* Turn a single GOT that is too big for 16-bit addressing into
4443 a sequence of GOTs, each one 16-bit addressable. */
4444
4445 static bfd_boolean
4446 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4447 asection *got, bfd_size_type pages)
4448 {
4449 struct mips_elf_link_hash_table *htab;
4450 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4451 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
4452 struct mips_got_info *g, *gg;
4453 unsigned int assign, needed_relocs;
4454 bfd *dynobj;
4455
4456 dynobj = elf_hash_table (info)->dynobj;
4457 htab = mips_elf_hash_table (info);
4458 BFD_ASSERT (htab != NULL);
4459
4460 g = htab->got_info;
4461 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
4462 mips_elf_bfd2got_entry_eq, NULL);
4463 if (g->bfd2got == NULL)
4464 return FALSE;
4465
4466 got_per_bfd_arg.bfd2got = g->bfd2got;
4467 got_per_bfd_arg.obfd = abfd;
4468 got_per_bfd_arg.info = info;
4469
4470 /* Count how many GOT entries each input bfd requires, creating a
4471 map from bfd to got info while at that. */
4472 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4473 if (got_per_bfd_arg.obfd == NULL)
4474 return FALSE;
4475
4476 /* Also count how many page entries each input bfd requires. */
4477 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4478 &got_per_bfd_arg);
4479 if (got_per_bfd_arg.obfd == NULL)
4480 return FALSE;
4481
4482 got_per_bfd_arg.current = NULL;
4483 got_per_bfd_arg.primary = NULL;
4484 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4485 / MIPS_ELF_GOT_SIZE (abfd))
4486 - htab->reserved_gotno);
4487 got_per_bfd_arg.max_pages = pages;
4488 /* The number of globals that will be included in the primary GOT.
4489 See the calls to mips_elf_set_global_got_offset below for more
4490 information. */
4491 got_per_bfd_arg.global_count = g->global_gotno;
4492
4493 /* Try to merge the GOTs of input bfds together, as long as they
4494 don't seem to exceed the maximum GOT size, choosing one of them
4495 to be the primary GOT. */
4496 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4497 if (got_per_bfd_arg.obfd == NULL)
4498 return FALSE;
4499
4500 /* If we do not find any suitable primary GOT, create an empty one. */
4501 if (got_per_bfd_arg.primary == NULL)
4502 {
4503 g->next = (struct mips_got_info *)
4504 bfd_alloc (abfd, sizeof (struct mips_got_info));
4505 if (g->next == NULL)
4506 return FALSE;
4507
4508 g->next->global_gotsym = NULL;
4509 g->next->global_gotno = 0;
4510 g->next->reloc_only_gotno = 0;
4511 g->next->local_gotno = 0;
4512 g->next->page_gotno = 0;
4513 g->next->tls_gotno = 0;
4514 g->next->assigned_gotno = 0;
4515 g->next->tls_assigned_gotno = 0;
4516 g->next->tls_ldm_offset = MINUS_ONE;
4517 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4518 mips_elf_multi_got_entry_eq,
4519 NULL);
4520 if (g->next->got_entries == NULL)
4521 return FALSE;
4522 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4523 mips_got_page_entry_eq,
4524 NULL);
4525 if (g->next->got_page_entries == NULL)
4526 return FALSE;
4527 g->next->bfd2got = NULL;
4528 }
4529 else
4530 g->next = got_per_bfd_arg.primary;
4531 g->next->next = got_per_bfd_arg.current;
4532
4533 /* GG is now the master GOT, and G is the primary GOT. */
4534 gg = g;
4535 g = g->next;
4536
4537 /* Map the output bfd to the primary got. That's what we're going
4538 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4539 didn't mark in check_relocs, and we want a quick way to find it.
4540 We can't just use gg->next because we're going to reverse the
4541 list. */
4542 {
4543 struct mips_elf_bfd2got_hash *bfdgot;
4544 void **bfdgotp;
4545
4546 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4547 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4548
4549 if (bfdgot == NULL)
4550 return FALSE;
4551
4552 bfdgot->bfd = abfd;
4553 bfdgot->g = g;
4554 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4555
4556 BFD_ASSERT (*bfdgotp == NULL);
4557 *bfdgotp = bfdgot;
4558 }
4559
4560 /* Every symbol that is referenced in a dynamic relocation must be
4561 present in the primary GOT, so arrange for them to appear after
4562 those that are actually referenced. */
4563 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4564 g->global_gotno = gg->global_gotno;
4565
4566 set_got_offset_arg.g = NULL;
4567 set_got_offset_arg.value = GGA_RELOC_ONLY;
4568 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4569 &set_got_offset_arg);
4570 set_got_offset_arg.value = GGA_NORMAL;
4571 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4572 &set_got_offset_arg);
4573
4574 /* Now go through the GOTs assigning them offset ranges.
4575 [assigned_gotno, local_gotno[ will be set to the range of local
4576 entries in each GOT. We can then compute the end of a GOT by
4577 adding local_gotno to global_gotno. We reverse the list and make
4578 it circular since then we'll be able to quickly compute the
4579 beginning of a GOT, by computing the end of its predecessor. To
4580 avoid special cases for the primary GOT, while still preserving
4581 assertions that are valid for both single- and multi-got links,
4582 we arrange for the main got struct to have the right number of
4583 global entries, but set its local_gotno such that the initial
4584 offset of the primary GOT is zero. Remember that the primary GOT
4585 will become the last item in the circular linked list, so it
4586 points back to the master GOT. */
4587 gg->local_gotno = -g->global_gotno;
4588 gg->global_gotno = g->global_gotno;
4589 gg->tls_gotno = 0;
4590 assign = 0;
4591 gg->next = gg;
4592
4593 do
4594 {
4595 struct mips_got_info *gn;
4596
4597 assign += htab->reserved_gotno;
4598 g->assigned_gotno = assign;
4599 g->local_gotno += assign;
4600 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4601 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4602
4603 /* Take g out of the direct list, and push it onto the reversed
4604 list that gg points to. g->next is guaranteed to be nonnull after
4605 this operation, as required by mips_elf_initialize_tls_index. */
4606 gn = g->next;
4607 g->next = gg->next;
4608 gg->next = g;
4609
4610 /* Set up any TLS entries. We always place the TLS entries after
4611 all non-TLS entries. */
4612 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4613 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
4614
4615 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4616 g = gn;
4617
4618 /* Forbid global symbols in every non-primary GOT from having
4619 lazy-binding stubs. */
4620 if (g)
4621 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4622 }
4623 while (g);
4624
4625 got->size = (gg->next->local_gotno
4626 + gg->next->global_gotno
4627 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4628
4629 needed_relocs = 0;
4630 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4631 set_got_offset_arg.info = info;
4632 for (g = gg->next; g && g->next != gg; g = g->next)
4633 {
4634 unsigned int save_assign;
4635
4636 /* Assign offsets to global GOT entries. */
4637 save_assign = g->assigned_gotno;
4638 g->assigned_gotno = g->local_gotno;
4639 set_got_offset_arg.g = g;
4640 set_got_offset_arg.needed_relocs = 0;
4641 htab_traverse (g->got_entries,
4642 mips_elf_set_global_got_offset,
4643 &set_got_offset_arg);
4644 needed_relocs += set_got_offset_arg.needed_relocs;
4645 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4646
4647 g->assigned_gotno = save_assign;
4648 if (info->shared)
4649 {
4650 needed_relocs += g->local_gotno - g->assigned_gotno;
4651 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4652 + g->next->global_gotno
4653 + g->next->tls_gotno
4654 + htab->reserved_gotno);
4655 }
4656 }
4657
4658 if (needed_relocs)
4659 mips_elf_allocate_dynamic_relocations (dynobj, info,
4660 needed_relocs);
4661
4662 return TRUE;
4663 }
4664
4665 \f
4666 /* Returns the first relocation of type r_type found, beginning with
4667 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4668
4669 static const Elf_Internal_Rela *
4670 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4671 const Elf_Internal_Rela *relocation,
4672 const Elf_Internal_Rela *relend)
4673 {
4674 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4675
4676 while (relocation < relend)
4677 {
4678 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4679 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4680 return relocation;
4681
4682 ++relocation;
4683 }
4684
4685 /* We didn't find it. */
4686 return NULL;
4687 }
4688
4689 /* Return whether an input relocation is against a local symbol. */
4690
4691 static bfd_boolean
4692 mips_elf_local_relocation_p (bfd *input_bfd,
4693 const Elf_Internal_Rela *relocation,
4694 asection **local_sections)
4695 {
4696 unsigned long r_symndx;
4697 Elf_Internal_Shdr *symtab_hdr;
4698 size_t extsymoff;
4699
4700 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4701 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4702 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4703
4704 if (r_symndx < extsymoff)
4705 return TRUE;
4706 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4707 return TRUE;
4708
4709 return FALSE;
4710 }
4711 \f
4712 /* Sign-extend VALUE, which has the indicated number of BITS. */
4713
4714 bfd_vma
4715 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4716 {
4717 if (value & ((bfd_vma) 1 << (bits - 1)))
4718 /* VALUE is negative. */
4719 value |= ((bfd_vma) - 1) << bits;
4720
4721 return value;
4722 }
4723
4724 /* Return non-zero if the indicated VALUE has overflowed the maximum
4725 range expressible by a signed number with the indicated number of
4726 BITS. */
4727
4728 static bfd_boolean
4729 mips_elf_overflow_p (bfd_vma value, int bits)
4730 {
4731 bfd_signed_vma svalue = (bfd_signed_vma) value;
4732
4733 if (svalue > (1 << (bits - 1)) - 1)
4734 /* The value is too big. */
4735 return TRUE;
4736 else if (svalue < -(1 << (bits - 1)))
4737 /* The value is too small. */
4738 return TRUE;
4739
4740 /* All is well. */
4741 return FALSE;
4742 }
4743
4744 /* Calculate the %high function. */
4745
4746 static bfd_vma
4747 mips_elf_high (bfd_vma value)
4748 {
4749 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4750 }
4751
4752 /* Calculate the %higher function. */
4753
4754 static bfd_vma
4755 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4756 {
4757 #ifdef BFD64
4758 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4759 #else
4760 abort ();
4761 return MINUS_ONE;
4762 #endif
4763 }
4764
4765 /* Calculate the %highest function. */
4766
4767 static bfd_vma
4768 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4769 {
4770 #ifdef BFD64
4771 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4772 #else
4773 abort ();
4774 return MINUS_ONE;
4775 #endif
4776 }
4777 \f
4778 /* Create the .compact_rel section. */
4779
4780 static bfd_boolean
4781 mips_elf_create_compact_rel_section
4782 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4783 {
4784 flagword flags;
4785 register asection *s;
4786
4787 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4788 {
4789 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4790 | SEC_READONLY);
4791
4792 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4793 if (s == NULL
4794 || ! bfd_set_section_alignment (abfd, s,
4795 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4796 return FALSE;
4797
4798 s->size = sizeof (Elf32_External_compact_rel);
4799 }
4800
4801 return TRUE;
4802 }
4803
4804 /* Create the .got section to hold the global offset table. */
4805
4806 static bfd_boolean
4807 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
4808 {
4809 flagword flags;
4810 register asection *s;
4811 struct elf_link_hash_entry *h;
4812 struct bfd_link_hash_entry *bh;
4813 struct mips_got_info *g;
4814 bfd_size_type amt;
4815 struct mips_elf_link_hash_table *htab;
4816
4817 htab = mips_elf_hash_table (info);
4818 BFD_ASSERT (htab != NULL);
4819
4820 /* This function may be called more than once. */
4821 if (htab->sgot)
4822 return TRUE;
4823
4824 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4825 | SEC_LINKER_CREATED);
4826
4827 /* We have to use an alignment of 2**4 here because this is hardcoded
4828 in the function stub generation and in the linker script. */
4829 s = bfd_make_section_with_flags (abfd, ".got", flags);
4830 if (s == NULL
4831 || ! bfd_set_section_alignment (abfd, s, 4))
4832 return FALSE;
4833 htab->sgot = s;
4834
4835 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4836 linker script because we don't want to define the symbol if we
4837 are not creating a global offset table. */
4838 bh = NULL;
4839 if (! (_bfd_generic_link_add_one_symbol
4840 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4841 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4842 return FALSE;
4843
4844 h = (struct elf_link_hash_entry *) bh;
4845 h->non_elf = 0;
4846 h->def_regular = 1;
4847 h->type = STT_OBJECT;
4848 elf_hash_table (info)->hgot = h;
4849
4850 if (info->shared
4851 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4852 return FALSE;
4853
4854 amt = sizeof (struct mips_got_info);
4855 g = bfd_alloc (abfd, amt);
4856 if (g == NULL)
4857 return FALSE;
4858 g->global_gotsym = NULL;
4859 g->global_gotno = 0;
4860 g->reloc_only_gotno = 0;
4861 g->tls_gotno = 0;
4862 g->local_gotno = 0;
4863 g->page_gotno = 0;
4864 g->assigned_gotno = 0;
4865 g->bfd2got = NULL;
4866 g->next = NULL;
4867 g->tls_ldm_offset = MINUS_ONE;
4868 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4869 mips_elf_got_entry_eq, NULL);
4870 if (g->got_entries == NULL)
4871 return FALSE;
4872 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4873 mips_got_page_entry_eq, NULL);
4874 if (g->got_page_entries == NULL)
4875 return FALSE;
4876 htab->got_info = g;
4877 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4878 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4879
4880 /* We also need a .got.plt section when generating PLTs. */
4881 s = bfd_make_section_with_flags (abfd, ".got.plt",
4882 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4883 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4884 if (s == NULL)
4885 return FALSE;
4886 htab->sgotplt = s;
4887
4888 return TRUE;
4889 }
4890 \f
4891 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4892 __GOTT_INDEX__ symbols. These symbols are only special for
4893 shared objects; they are not used in executables. */
4894
4895 static bfd_boolean
4896 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4897 {
4898 return (mips_elf_hash_table (info)->is_vxworks
4899 && info->shared
4900 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4901 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4902 }
4903
4904 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4905 require an la25 stub. See also mips_elf_local_pic_function_p,
4906 which determines whether the destination function ever requires a
4907 stub. */
4908
4909 static bfd_boolean
4910 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4911 {
4912 /* We specifically ignore branches and jumps from EF_PIC objects,
4913 where the onus is on the compiler or programmer to perform any
4914 necessary initialization of $25. Sometimes such initialization
4915 is unnecessary; for example, -mno-shared functions do not use
4916 the incoming value of $25, and may therefore be called directly. */
4917 if (PIC_OBJECT_P (input_bfd))
4918 return FALSE;
4919
4920 switch (r_type)
4921 {
4922 case R_MIPS_26:
4923 case R_MIPS_PC16:
4924 case R_MIPS16_26:
4925 case R_MICROMIPS_26_S1:
4926 case R_MICROMIPS_PC7_S1:
4927 case R_MICROMIPS_PC10_S1:
4928 case R_MICROMIPS_PC16_S1:
4929 case R_MICROMIPS_PC23_S2:
4930 return TRUE;
4931
4932 default:
4933 return FALSE;
4934 }
4935 }
4936 \f
4937 /* Calculate the value produced by the RELOCATION (which comes from
4938 the INPUT_BFD). The ADDEND is the addend to use for this
4939 RELOCATION; RELOCATION->R_ADDEND is ignored.
4940
4941 The result of the relocation calculation is stored in VALUEP.
4942 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4943 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
4944
4945 This function returns bfd_reloc_continue if the caller need take no
4946 further action regarding this relocation, bfd_reloc_notsupported if
4947 something goes dramatically wrong, bfd_reloc_overflow if an
4948 overflow occurs, and bfd_reloc_ok to indicate success. */
4949
4950 static bfd_reloc_status_type
4951 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4952 asection *input_section,
4953 struct bfd_link_info *info,
4954 const Elf_Internal_Rela *relocation,
4955 bfd_vma addend, reloc_howto_type *howto,
4956 Elf_Internal_Sym *local_syms,
4957 asection **local_sections, bfd_vma *valuep,
4958 const char **namep,
4959 bfd_boolean *cross_mode_jump_p,
4960 bfd_boolean save_addend)
4961 {
4962 /* The eventual value we will return. */
4963 bfd_vma value;
4964 /* The address of the symbol against which the relocation is
4965 occurring. */
4966 bfd_vma symbol = 0;
4967 /* The final GP value to be used for the relocatable, executable, or
4968 shared object file being produced. */
4969 bfd_vma gp;
4970 /* The place (section offset or address) of the storage unit being
4971 relocated. */
4972 bfd_vma p;
4973 /* The value of GP used to create the relocatable object. */
4974 bfd_vma gp0;
4975 /* The offset into the global offset table at which the address of
4976 the relocation entry symbol, adjusted by the addend, resides
4977 during execution. */
4978 bfd_vma g = MINUS_ONE;
4979 /* The section in which the symbol referenced by the relocation is
4980 located. */
4981 asection *sec = NULL;
4982 struct mips_elf_link_hash_entry *h = NULL;
4983 /* TRUE if the symbol referred to by this relocation is a local
4984 symbol. */
4985 bfd_boolean local_p, was_local_p;
4986 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4987 bfd_boolean gp_disp_p = FALSE;
4988 /* TRUE if the symbol referred to by this relocation is
4989 "__gnu_local_gp". */
4990 bfd_boolean gnu_local_gp_p = FALSE;
4991 Elf_Internal_Shdr *symtab_hdr;
4992 size_t extsymoff;
4993 unsigned long r_symndx;
4994 int r_type;
4995 /* TRUE if overflow occurred during the calculation of the
4996 relocation value. */
4997 bfd_boolean overflowed_p;
4998 /* TRUE if this relocation refers to a MIPS16 function. */
4999 bfd_boolean target_is_16_bit_code_p = FALSE;
5000 bfd_boolean target_is_micromips_code_p = FALSE;
5001 struct mips_elf_link_hash_table *htab;
5002 bfd *dynobj;
5003
5004 dynobj = elf_hash_table (info)->dynobj;
5005 htab = mips_elf_hash_table (info);
5006 BFD_ASSERT (htab != NULL);
5007
5008 /* Parse the relocation. */
5009 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5010 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5011 p = (input_section->output_section->vma
5012 + input_section->output_offset
5013 + relocation->r_offset);
5014
5015 /* Assume that there will be no overflow. */
5016 overflowed_p = FALSE;
5017
5018 /* Figure out whether or not the symbol is local, and get the offset
5019 used in the array of hash table entries. */
5020 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5021 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5022 local_sections);
5023 was_local_p = local_p;
5024 if (! elf_bad_symtab (input_bfd))
5025 extsymoff = symtab_hdr->sh_info;
5026 else
5027 {
5028 /* The symbol table does not follow the rule that local symbols
5029 must come before globals. */
5030 extsymoff = 0;
5031 }
5032
5033 /* Figure out the value of the symbol. */
5034 if (local_p)
5035 {
5036 Elf_Internal_Sym *sym;
5037
5038 sym = local_syms + r_symndx;
5039 sec = local_sections[r_symndx];
5040
5041 symbol = sec->output_section->vma + sec->output_offset;
5042 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5043 || (sec->flags & SEC_MERGE))
5044 symbol += sym->st_value;
5045 if ((sec->flags & SEC_MERGE)
5046 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5047 {
5048 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5049 addend -= symbol;
5050 addend += sec->output_section->vma + sec->output_offset;
5051 }
5052
5053 /* MIPS16/microMIPS text labels should be treated as odd. */
5054 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5055 ++symbol;
5056
5057 /* Record the name of this symbol, for our caller. */
5058 *namep = bfd_elf_string_from_elf_section (input_bfd,
5059 symtab_hdr->sh_link,
5060 sym->st_name);
5061 if (*namep == '\0')
5062 *namep = bfd_section_name (input_bfd, sec);
5063
5064 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5065 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5066 }
5067 else
5068 {
5069 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5070
5071 /* For global symbols we look up the symbol in the hash-table. */
5072 h = ((struct mips_elf_link_hash_entry *)
5073 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5074 /* Find the real hash-table entry for this symbol. */
5075 while (h->root.root.type == bfd_link_hash_indirect
5076 || h->root.root.type == bfd_link_hash_warning)
5077 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5078
5079 /* Record the name of this symbol, for our caller. */
5080 *namep = h->root.root.root.string;
5081
5082 /* See if this is the special _gp_disp symbol. Note that such a
5083 symbol must always be a global symbol. */
5084 if (strcmp (*namep, "_gp_disp") == 0
5085 && ! NEWABI_P (input_bfd))
5086 {
5087 /* Relocations against _gp_disp are permitted only with
5088 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5089 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5090 return bfd_reloc_notsupported;
5091
5092 gp_disp_p = TRUE;
5093 }
5094 /* See if this is the special _gp symbol. Note that such a
5095 symbol must always be a global symbol. */
5096 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5097 gnu_local_gp_p = TRUE;
5098
5099
5100 /* If this symbol is defined, calculate its address. Note that
5101 _gp_disp is a magic symbol, always implicitly defined by the
5102 linker, so it's inappropriate to check to see whether or not
5103 its defined. */
5104 else if ((h->root.root.type == bfd_link_hash_defined
5105 || h->root.root.type == bfd_link_hash_defweak)
5106 && h->root.root.u.def.section)
5107 {
5108 sec = h->root.root.u.def.section;
5109 if (sec->output_section)
5110 symbol = (h->root.root.u.def.value
5111 + sec->output_section->vma
5112 + sec->output_offset);
5113 else
5114 symbol = h->root.root.u.def.value;
5115 }
5116 else if (h->root.root.type == bfd_link_hash_undefweak)
5117 /* We allow relocations against undefined weak symbols, giving
5118 it the value zero, so that you can undefined weak functions
5119 and check to see if they exist by looking at their
5120 addresses. */
5121 symbol = 0;
5122 else if (info->unresolved_syms_in_objects == RM_IGNORE
5123 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5124 symbol = 0;
5125 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5126 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5127 {
5128 /* If this is a dynamic link, we should have created a
5129 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5130 in in _bfd_mips_elf_create_dynamic_sections.
5131 Otherwise, we should define the symbol with a value of 0.
5132 FIXME: It should probably get into the symbol table
5133 somehow as well. */
5134 BFD_ASSERT (! info->shared);
5135 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5136 symbol = 0;
5137 }
5138 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5139 {
5140 /* This is an optional symbol - an Irix specific extension to the
5141 ELF spec. Ignore it for now.
5142 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5143 than simply ignoring them, but we do not handle this for now.
5144 For information see the "64-bit ELF Object File Specification"
5145 which is available from here:
5146 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5147 symbol = 0;
5148 }
5149 else if ((*info->callbacks->undefined_symbol)
5150 (info, h->root.root.root.string, input_bfd,
5151 input_section, relocation->r_offset,
5152 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5153 || ELF_ST_VISIBILITY (h->root.other)))
5154 {
5155 return bfd_reloc_undefined;
5156 }
5157 else
5158 {
5159 return bfd_reloc_notsupported;
5160 }
5161
5162 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5163 /* If the output section is the PLT section,
5164 then the target is not microMIPS. */
5165 target_is_micromips_code_p = (htab->splt != sec
5166 && ELF_ST_IS_MICROMIPS (h->root.other));
5167 }
5168
5169 /* If this is a reference to a 16-bit function with a stub, we need
5170 to redirect the relocation to the stub unless:
5171
5172 (a) the relocation is for a MIPS16 JAL;
5173
5174 (b) the relocation is for a MIPS16 PIC call, and there are no
5175 non-MIPS16 uses of the GOT slot; or
5176
5177 (c) the section allows direct references to MIPS16 functions. */
5178 if (r_type != R_MIPS16_26
5179 && !info->relocatable
5180 && ((h != NULL
5181 && h->fn_stub != NULL
5182 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5183 || (local_p
5184 && elf_tdata (input_bfd)->local_stubs != NULL
5185 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5186 && !section_allows_mips16_refs_p (input_section))
5187 {
5188 /* This is a 32- or 64-bit call to a 16-bit function. We should
5189 have already noticed that we were going to need the
5190 stub. */
5191 if (local_p)
5192 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5193 else
5194 {
5195 BFD_ASSERT (h->need_fn_stub);
5196 sec = h->fn_stub;
5197 }
5198
5199 symbol = sec->output_section->vma + sec->output_offset;
5200 /* The target is 16-bit, but the stub isn't. */
5201 target_is_16_bit_code_p = FALSE;
5202 }
5203 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
5204 need to redirect the call to the stub. Note that we specifically
5205 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5206 use an indirect stub instead. */
5207 else if (r_type == R_MIPS16_26 && !info->relocatable
5208 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5209 || (local_p
5210 && elf_tdata (input_bfd)->local_call_stubs != NULL
5211 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5212 && !target_is_16_bit_code_p)
5213 {
5214 if (local_p)
5215 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5216 else
5217 {
5218 /* If both call_stub and call_fp_stub are defined, we can figure
5219 out which one to use by checking which one appears in the input
5220 file. */
5221 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5222 {
5223 asection *o;
5224
5225 sec = NULL;
5226 for (o = input_bfd->sections; o != NULL; o = o->next)
5227 {
5228 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5229 {
5230 sec = h->call_fp_stub;
5231 break;
5232 }
5233 }
5234 if (sec == NULL)
5235 sec = h->call_stub;
5236 }
5237 else if (h->call_stub != NULL)
5238 sec = h->call_stub;
5239 else
5240 sec = h->call_fp_stub;
5241 }
5242
5243 BFD_ASSERT (sec->size > 0);
5244 symbol = sec->output_section->vma + sec->output_offset;
5245 }
5246 /* If this is a direct call to a PIC function, redirect to the
5247 non-PIC stub. */
5248 else if (h != NULL && h->la25_stub
5249 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5250 symbol = (h->la25_stub->stub_section->output_section->vma
5251 + h->la25_stub->stub_section->output_offset
5252 + h->la25_stub->offset);
5253
5254 /* Make sure MIPS16 and microMIPS are not used together. */
5255 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5256 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5257 {
5258 (*_bfd_error_handler)
5259 (_("MIPS16 and microMIPS functions cannot call each other"));
5260 return bfd_reloc_notsupported;
5261 }
5262
5263 /* Calls from 16-bit code to 32-bit code and vice versa require the
5264 mode change. However, we can ignore calls to undefined weak symbols,
5265 which should never be executed at runtime. This exception is important
5266 because the assembly writer may have "known" that any definition of the
5267 symbol would be 16-bit code, and that direct jumps were therefore
5268 acceptable. */
5269 *cross_mode_jump_p = (!info->relocatable
5270 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5271 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5272 || (r_type == R_MICROMIPS_26_S1
5273 && !target_is_micromips_code_p)
5274 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5275 && (target_is_16_bit_code_p
5276 || target_is_micromips_code_p))));
5277
5278 local_p = h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->root);
5279
5280 gp0 = _bfd_get_gp_value (input_bfd);
5281 gp = _bfd_get_gp_value (abfd);
5282 if (htab->got_info)
5283 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5284
5285 if (gnu_local_gp_p)
5286 symbol = gp;
5287
5288 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5289 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5290 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5291 if (got_page_reloc_p (r_type) && !local_p)
5292 {
5293 r_type = (micromips_reloc_p (r_type)
5294 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5295 addend = 0;
5296 }
5297
5298 /* If we haven't already determined the GOT offset, and we're going
5299 to need it, get it now. */
5300 switch (r_type)
5301 {
5302 case R_MIPS16_CALL16:
5303 case R_MIPS16_GOT16:
5304 case R_MIPS_CALL16:
5305 case R_MIPS_GOT16:
5306 case R_MIPS_GOT_DISP:
5307 case R_MIPS_GOT_HI16:
5308 case R_MIPS_CALL_HI16:
5309 case R_MIPS_GOT_LO16:
5310 case R_MIPS_CALL_LO16:
5311 case R_MICROMIPS_CALL16:
5312 case R_MICROMIPS_GOT16:
5313 case R_MICROMIPS_GOT_DISP:
5314 case R_MICROMIPS_GOT_HI16:
5315 case R_MICROMIPS_CALL_HI16:
5316 case R_MICROMIPS_GOT_LO16:
5317 case R_MICROMIPS_CALL_LO16:
5318 case R_MIPS_TLS_GD:
5319 case R_MIPS_TLS_GOTTPREL:
5320 case R_MIPS_TLS_LDM:
5321 case R_MICROMIPS_TLS_GD:
5322 case R_MICROMIPS_TLS_GOTTPREL:
5323 case R_MICROMIPS_TLS_LDM:
5324 /* Find the index into the GOT where this value is located. */
5325 if (tls_ldm_reloc_p (r_type))
5326 {
5327 g = mips_elf_local_got_index (abfd, input_bfd, info,
5328 0, 0, NULL, r_type);
5329 if (g == MINUS_ONE)
5330 return bfd_reloc_outofrange;
5331 }
5332 else if (!local_p)
5333 {
5334 /* On VxWorks, CALL relocations should refer to the .got.plt
5335 entry, which is initialized to point at the PLT stub. */
5336 if (htab->is_vxworks
5337 && (call_hi16_reloc_p (r_type)
5338 || call_lo16_reloc_p (r_type)
5339 || call16_reloc_p (r_type)))
5340 {
5341 BFD_ASSERT (addend == 0);
5342 BFD_ASSERT (h->root.needs_plt);
5343 g = mips_elf_gotplt_index (info, &h->root);
5344 }
5345 else
5346 {
5347 BFD_ASSERT (addend == 0);
5348 g = mips_elf_global_got_index (dynobj, input_bfd,
5349 &h->root, r_type, info);
5350 if (h->tls_type == GOT_NORMAL
5351 && !elf_hash_table (info)->dynamic_sections_created)
5352 /* This is a static link. We must initialize the GOT entry. */
5353 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5354 }
5355 }
5356 else if (!htab->is_vxworks
5357 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5358 /* The calculation below does not involve "g". */
5359 break;
5360 else
5361 {
5362 g = mips_elf_local_got_index (abfd, input_bfd, info,
5363 symbol + addend, r_symndx, h, r_type);
5364 if (g == MINUS_ONE)
5365 return bfd_reloc_outofrange;
5366 }
5367
5368 /* Convert GOT indices to actual offsets. */
5369 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5370 break;
5371 }
5372
5373 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5374 symbols are resolved by the loader. Add them to .rela.dyn. */
5375 if (h != NULL && is_gott_symbol (info, &h->root))
5376 {
5377 Elf_Internal_Rela outrel;
5378 bfd_byte *loc;
5379 asection *s;
5380
5381 s = mips_elf_rel_dyn_section (info, FALSE);
5382 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5383
5384 outrel.r_offset = (input_section->output_section->vma
5385 + input_section->output_offset
5386 + relocation->r_offset);
5387 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5388 outrel.r_addend = addend;
5389 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5390
5391 /* If we've written this relocation for a readonly section,
5392 we need to set DF_TEXTREL again, so that we do not delete the
5393 DT_TEXTREL tag. */
5394 if (MIPS_ELF_READONLY_SECTION (input_section))
5395 info->flags |= DF_TEXTREL;
5396
5397 *valuep = 0;
5398 return bfd_reloc_ok;
5399 }
5400
5401 /* Figure out what kind of relocation is being performed. */
5402 switch (r_type)
5403 {
5404 case R_MIPS_NONE:
5405 return bfd_reloc_continue;
5406
5407 case R_MIPS_16:
5408 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5409 overflowed_p = mips_elf_overflow_p (value, 16);
5410 break;
5411
5412 case R_MIPS_32:
5413 case R_MIPS_REL32:
5414 case R_MIPS_64:
5415 if ((info->shared
5416 || (htab->root.dynamic_sections_created
5417 && h != NULL
5418 && h->root.def_dynamic
5419 && !h->root.def_regular
5420 && !h->has_static_relocs))
5421 && r_symndx != STN_UNDEF
5422 && (h == NULL
5423 || h->root.root.type != bfd_link_hash_undefweak
5424 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5425 && (input_section->flags & SEC_ALLOC) != 0)
5426 {
5427 /* If we're creating a shared library, then we can't know
5428 where the symbol will end up. So, we create a relocation
5429 record in the output, and leave the job up to the dynamic
5430 linker. We must do the same for executable references to
5431 shared library symbols, unless we've decided to use copy
5432 relocs or PLTs instead. */
5433 value = addend;
5434 if (!mips_elf_create_dynamic_relocation (abfd,
5435 info,
5436 relocation,
5437 h,
5438 sec,
5439 symbol,
5440 &value,
5441 input_section))
5442 return bfd_reloc_undefined;
5443 }
5444 else
5445 {
5446 if (r_type != R_MIPS_REL32)
5447 value = symbol + addend;
5448 else
5449 value = addend;
5450 }
5451 value &= howto->dst_mask;
5452 break;
5453
5454 case R_MIPS_PC32:
5455 value = symbol + addend - p;
5456 value &= howto->dst_mask;
5457 break;
5458
5459 case R_MIPS16_26:
5460 /* The calculation for R_MIPS16_26 is just the same as for an
5461 R_MIPS_26. It's only the storage of the relocated field into
5462 the output file that's different. That's handled in
5463 mips_elf_perform_relocation. So, we just fall through to the
5464 R_MIPS_26 case here. */
5465 case R_MIPS_26:
5466 case R_MICROMIPS_26_S1:
5467 {
5468 unsigned int shift;
5469
5470 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5471 the correct ISA mode selector and bit 1 must be 0. */
5472 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5473 return bfd_reloc_outofrange;
5474
5475 /* Shift is 2, unusually, for microMIPS JALX. */
5476 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5477
5478 if (was_local_p)
5479 value = addend | ((p + 4) & (0xfc000000 << shift));
5480 else
5481 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5482 value = (value + symbol) >> shift;
5483 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5484 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5485 value &= howto->dst_mask;
5486 }
5487 break;
5488
5489 case R_MIPS_TLS_DTPREL_HI16:
5490 case R_MICROMIPS_TLS_DTPREL_HI16:
5491 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5492 & howto->dst_mask);
5493 break;
5494
5495 case R_MIPS_TLS_DTPREL_LO16:
5496 case R_MIPS_TLS_DTPREL32:
5497 case R_MIPS_TLS_DTPREL64:
5498 case R_MICROMIPS_TLS_DTPREL_LO16:
5499 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5500 break;
5501
5502 case R_MIPS_TLS_TPREL_HI16:
5503 case R_MICROMIPS_TLS_TPREL_HI16:
5504 value = (mips_elf_high (addend + symbol - tprel_base (info))
5505 & howto->dst_mask);
5506 break;
5507
5508 case R_MIPS_TLS_TPREL_LO16:
5509 case R_MICROMIPS_TLS_TPREL_LO16:
5510 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5511 break;
5512
5513 case R_MIPS_HI16:
5514 case R_MIPS16_HI16:
5515 case R_MICROMIPS_HI16:
5516 if (!gp_disp_p)
5517 {
5518 value = mips_elf_high (addend + symbol);
5519 value &= howto->dst_mask;
5520 }
5521 else
5522 {
5523 /* For MIPS16 ABI code we generate this sequence
5524 0: li $v0,%hi(_gp_disp)
5525 4: addiupc $v1,%lo(_gp_disp)
5526 8: sll $v0,16
5527 12: addu $v0,$v1
5528 14: move $gp,$v0
5529 So the offsets of hi and lo relocs are the same, but the
5530 $pc is four higher than $t9 would be, so reduce
5531 both reloc addends by 4. */
5532 if (r_type == R_MIPS16_HI16)
5533 value = mips_elf_high (addend + gp - p - 4);
5534 /* The microMIPS .cpload sequence uses the same assembly
5535 instructions as the traditional psABI version, but the
5536 incoming $t9 has the low bit set. */
5537 else if (r_type == R_MICROMIPS_HI16)
5538 value = mips_elf_high (addend + gp - p - 1);
5539 else
5540 value = mips_elf_high (addend + gp - p);
5541 overflowed_p = mips_elf_overflow_p (value, 16);
5542 }
5543 break;
5544
5545 case R_MIPS_LO16:
5546 case R_MIPS16_LO16:
5547 case R_MICROMIPS_LO16:
5548 case R_MICROMIPS_HI0_LO16:
5549 if (!gp_disp_p)
5550 value = (symbol + addend) & howto->dst_mask;
5551 else
5552 {
5553 /* See the comment for R_MIPS16_HI16 above for the reason
5554 for this conditional. */
5555 if (r_type == R_MIPS16_LO16)
5556 value = addend + gp - p;
5557 else if (r_type == R_MICROMIPS_LO16
5558 || r_type == R_MICROMIPS_HI0_LO16)
5559 value = addend + gp - p + 3;
5560 else
5561 value = addend + gp - p + 4;
5562 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5563 for overflow. But, on, say, IRIX5, relocations against
5564 _gp_disp are normally generated from the .cpload
5565 pseudo-op. It generates code that normally looks like
5566 this:
5567
5568 lui $gp,%hi(_gp_disp)
5569 addiu $gp,$gp,%lo(_gp_disp)
5570 addu $gp,$gp,$t9
5571
5572 Here $t9 holds the address of the function being called,
5573 as required by the MIPS ELF ABI. The R_MIPS_LO16
5574 relocation can easily overflow in this situation, but the
5575 R_MIPS_HI16 relocation will handle the overflow.
5576 Therefore, we consider this a bug in the MIPS ABI, and do
5577 not check for overflow here. */
5578 }
5579 break;
5580
5581 case R_MIPS_LITERAL:
5582 case R_MICROMIPS_LITERAL:
5583 /* Because we don't merge literal sections, we can handle this
5584 just like R_MIPS_GPREL16. In the long run, we should merge
5585 shared literals, and then we will need to additional work
5586 here. */
5587
5588 /* Fall through. */
5589
5590 case R_MIPS16_GPREL:
5591 /* The R_MIPS16_GPREL performs the same calculation as
5592 R_MIPS_GPREL16, but stores the relocated bits in a different
5593 order. We don't need to do anything special here; the
5594 differences are handled in mips_elf_perform_relocation. */
5595 case R_MIPS_GPREL16:
5596 case R_MICROMIPS_GPREL7_S2:
5597 case R_MICROMIPS_GPREL16:
5598 /* Only sign-extend the addend if it was extracted from the
5599 instruction. If the addend was separate, leave it alone,
5600 otherwise we may lose significant bits. */
5601 if (howto->partial_inplace)
5602 addend = _bfd_mips_elf_sign_extend (addend, 16);
5603 value = symbol + addend - gp;
5604 /* If the symbol was local, any earlier relocatable links will
5605 have adjusted its addend with the gp offset, so compensate
5606 for that now. Don't do it for symbols forced local in this
5607 link, though, since they won't have had the gp offset applied
5608 to them before. */
5609 if (was_local_p)
5610 value += gp0;
5611 overflowed_p = mips_elf_overflow_p (value, 16);
5612 break;
5613
5614 case R_MIPS16_GOT16:
5615 case R_MIPS16_CALL16:
5616 case R_MIPS_GOT16:
5617 case R_MIPS_CALL16:
5618 case R_MICROMIPS_GOT16:
5619 case R_MICROMIPS_CALL16:
5620 /* VxWorks does not have separate local and global semantics for
5621 R_MIPS*_GOT16; every relocation evaluates to "G". */
5622 if (!htab->is_vxworks && local_p)
5623 {
5624 value = mips_elf_got16_entry (abfd, input_bfd, info,
5625 symbol + addend, !was_local_p);
5626 if (value == MINUS_ONE)
5627 return bfd_reloc_outofrange;
5628 value
5629 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5630 overflowed_p = mips_elf_overflow_p (value, 16);
5631 break;
5632 }
5633
5634 /* Fall through. */
5635
5636 case R_MIPS_TLS_GD:
5637 case R_MIPS_TLS_GOTTPREL:
5638 case R_MIPS_TLS_LDM:
5639 case R_MIPS_GOT_DISP:
5640 case R_MICROMIPS_TLS_GD:
5641 case R_MICROMIPS_TLS_GOTTPREL:
5642 case R_MICROMIPS_TLS_LDM:
5643 case R_MICROMIPS_GOT_DISP:
5644 value = g;
5645 overflowed_p = mips_elf_overflow_p (value, 16);
5646 break;
5647
5648 case R_MIPS_GPREL32:
5649 value = (addend + symbol + gp0 - gp);
5650 if (!save_addend)
5651 value &= howto->dst_mask;
5652 break;
5653
5654 case R_MIPS_PC16:
5655 case R_MIPS_GNU_REL16_S2:
5656 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5657 overflowed_p = mips_elf_overflow_p (value, 18);
5658 value >>= howto->rightshift;
5659 value &= howto->dst_mask;
5660 break;
5661
5662 case R_MICROMIPS_PC7_S1:
5663 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5664 overflowed_p = mips_elf_overflow_p (value, 8);
5665 value >>= howto->rightshift;
5666 value &= howto->dst_mask;
5667 break;
5668
5669 case R_MICROMIPS_PC10_S1:
5670 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5671 overflowed_p = mips_elf_overflow_p (value, 11);
5672 value >>= howto->rightshift;
5673 value &= howto->dst_mask;
5674 break;
5675
5676 case R_MICROMIPS_PC16_S1:
5677 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5678 overflowed_p = mips_elf_overflow_p (value, 17);
5679 value >>= howto->rightshift;
5680 value &= howto->dst_mask;
5681 break;
5682
5683 case R_MICROMIPS_PC23_S2:
5684 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5685 overflowed_p = mips_elf_overflow_p (value, 25);
5686 value >>= howto->rightshift;
5687 value &= howto->dst_mask;
5688 break;
5689
5690 case R_MIPS_GOT_HI16:
5691 case R_MIPS_CALL_HI16:
5692 case R_MICROMIPS_GOT_HI16:
5693 case R_MICROMIPS_CALL_HI16:
5694 /* We're allowed to handle these two relocations identically.
5695 The dynamic linker is allowed to handle the CALL relocations
5696 differently by creating a lazy evaluation stub. */
5697 value = g;
5698 value = mips_elf_high (value);
5699 value &= howto->dst_mask;
5700 break;
5701
5702 case R_MIPS_GOT_LO16:
5703 case R_MIPS_CALL_LO16:
5704 case R_MICROMIPS_GOT_LO16:
5705 case R_MICROMIPS_CALL_LO16:
5706 value = g & howto->dst_mask;
5707 break;
5708
5709 case R_MIPS_GOT_PAGE:
5710 case R_MICROMIPS_GOT_PAGE:
5711 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5712 if (value == MINUS_ONE)
5713 return bfd_reloc_outofrange;
5714 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5715 overflowed_p = mips_elf_overflow_p (value, 16);
5716 break;
5717
5718 case R_MIPS_GOT_OFST:
5719 case R_MICROMIPS_GOT_OFST:
5720 if (local_p)
5721 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5722 else
5723 value = addend;
5724 overflowed_p = mips_elf_overflow_p (value, 16);
5725 break;
5726
5727 case R_MIPS_SUB:
5728 case R_MICROMIPS_SUB:
5729 value = symbol - addend;
5730 value &= howto->dst_mask;
5731 break;
5732
5733 case R_MIPS_HIGHER:
5734 case R_MICROMIPS_HIGHER:
5735 value = mips_elf_higher (addend + symbol);
5736 value &= howto->dst_mask;
5737 break;
5738
5739 case R_MIPS_HIGHEST:
5740 case R_MICROMIPS_HIGHEST:
5741 value = mips_elf_highest (addend + symbol);
5742 value &= howto->dst_mask;
5743 break;
5744
5745 case R_MIPS_SCN_DISP:
5746 case R_MICROMIPS_SCN_DISP:
5747 value = symbol + addend - sec->output_offset;
5748 value &= howto->dst_mask;
5749 break;
5750
5751 case R_MIPS_JALR:
5752 case R_MICROMIPS_JALR:
5753 /* This relocation is only a hint. In some cases, we optimize
5754 it into a bal instruction. But we don't try to optimize
5755 when the symbol does not resolve locally. */
5756 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5757 return bfd_reloc_continue;
5758 value = symbol + addend;
5759 break;
5760
5761 case R_MIPS_PJUMP:
5762 case R_MIPS_GNU_VTINHERIT:
5763 case R_MIPS_GNU_VTENTRY:
5764 /* We don't do anything with these at present. */
5765 return bfd_reloc_continue;
5766
5767 default:
5768 /* An unrecognized relocation type. */
5769 return bfd_reloc_notsupported;
5770 }
5771
5772 /* Store the VALUE for our caller. */
5773 *valuep = value;
5774 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5775 }
5776
5777 /* Obtain the field relocated by RELOCATION. */
5778
5779 static bfd_vma
5780 mips_elf_obtain_contents (reloc_howto_type *howto,
5781 const Elf_Internal_Rela *relocation,
5782 bfd *input_bfd, bfd_byte *contents)
5783 {
5784 bfd_vma x;
5785 bfd_byte *location = contents + relocation->r_offset;
5786
5787 /* Obtain the bytes. */
5788 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5789
5790 return x;
5791 }
5792
5793 /* It has been determined that the result of the RELOCATION is the
5794 VALUE. Use HOWTO to place VALUE into the output file at the
5795 appropriate position. The SECTION is the section to which the
5796 relocation applies.
5797 CROSS_MODE_JUMP_P is true if the relocation field
5798 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5799
5800 Returns FALSE if anything goes wrong. */
5801
5802 static bfd_boolean
5803 mips_elf_perform_relocation (struct bfd_link_info *info,
5804 reloc_howto_type *howto,
5805 const Elf_Internal_Rela *relocation,
5806 bfd_vma value, bfd *input_bfd,
5807 asection *input_section, bfd_byte *contents,
5808 bfd_boolean cross_mode_jump_p)
5809 {
5810 bfd_vma x;
5811 bfd_byte *location;
5812 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5813
5814 /* Figure out where the relocation is occurring. */
5815 location = contents + relocation->r_offset;
5816
5817 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5818
5819 /* Obtain the current value. */
5820 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5821
5822 /* Clear the field we are setting. */
5823 x &= ~howto->dst_mask;
5824
5825 /* Set the field. */
5826 x |= (value & howto->dst_mask);
5827
5828 /* If required, turn JAL into JALX. */
5829 if (cross_mode_jump_p && jal_reloc_p (r_type))
5830 {
5831 bfd_boolean ok;
5832 bfd_vma opcode = x >> 26;
5833 bfd_vma jalx_opcode;
5834
5835 /* Check to see if the opcode is already JAL or JALX. */
5836 if (r_type == R_MIPS16_26)
5837 {
5838 ok = ((opcode == 0x6) || (opcode == 0x7));
5839 jalx_opcode = 0x7;
5840 }
5841 else if (r_type == R_MICROMIPS_26_S1)
5842 {
5843 ok = ((opcode == 0x3d) || (opcode == 0x3c));
5844 jalx_opcode = 0x3c;
5845 }
5846 else
5847 {
5848 ok = ((opcode == 0x3) || (opcode == 0x1d));
5849 jalx_opcode = 0x1d;
5850 }
5851
5852 /* If the opcode is not JAL or JALX, there's a problem. */
5853 if (!ok)
5854 {
5855 (*_bfd_error_handler)
5856 (_("%B: %A+0x%lx: Direct jumps between ISA modes are not allowed; consider recompiling with interlinking enabled."),
5857 input_bfd,
5858 input_section,
5859 (unsigned long) relocation->r_offset);
5860 bfd_set_error (bfd_error_bad_value);
5861 return FALSE;
5862 }
5863
5864 /* Make this the JALX opcode. */
5865 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5866 }
5867
5868 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5869 range. */
5870 if (!info->relocatable
5871 && !cross_mode_jump_p
5872 && ((JAL_TO_BAL_P (input_bfd)
5873 && r_type == R_MIPS_26
5874 && (x >> 26) == 0x3) /* jal addr */
5875 || (JALR_TO_BAL_P (input_bfd)
5876 && r_type == R_MIPS_JALR
5877 && x == 0x0320f809) /* jalr t9 */
5878 || (JR_TO_B_P (input_bfd)
5879 && r_type == R_MIPS_JALR
5880 && x == 0x03200008))) /* jr t9 */
5881 {
5882 bfd_vma addr;
5883 bfd_vma dest;
5884 bfd_signed_vma off;
5885
5886 addr = (input_section->output_section->vma
5887 + input_section->output_offset
5888 + relocation->r_offset
5889 + 4);
5890 if (r_type == R_MIPS_26)
5891 dest = (value << 2) | ((addr >> 28) << 28);
5892 else
5893 dest = value;
5894 off = dest - addr;
5895 if (off <= 0x1ffff && off >= -0x20000)
5896 {
5897 if (x == 0x03200008) /* jr t9 */
5898 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5899 else
5900 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5901 }
5902 }
5903
5904 /* Put the value into the output. */
5905 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5906
5907 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
5908 location);
5909
5910 return TRUE;
5911 }
5912 \f
5913 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5914 is the original relocation, which is now being transformed into a
5915 dynamic relocation. The ADDENDP is adjusted if necessary; the
5916 caller should store the result in place of the original addend. */
5917
5918 static bfd_boolean
5919 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5920 struct bfd_link_info *info,
5921 const Elf_Internal_Rela *rel,
5922 struct mips_elf_link_hash_entry *h,
5923 asection *sec, bfd_vma symbol,
5924 bfd_vma *addendp, asection *input_section)
5925 {
5926 Elf_Internal_Rela outrel[3];
5927 asection *sreloc;
5928 bfd *dynobj;
5929 int r_type;
5930 long indx;
5931 bfd_boolean defined_p;
5932 struct mips_elf_link_hash_table *htab;
5933
5934 htab = mips_elf_hash_table (info);
5935 BFD_ASSERT (htab != NULL);
5936
5937 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5938 dynobj = elf_hash_table (info)->dynobj;
5939 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5940 BFD_ASSERT (sreloc != NULL);
5941 BFD_ASSERT (sreloc->contents != NULL);
5942 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5943 < sreloc->size);
5944
5945 outrel[0].r_offset =
5946 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5947 if (ABI_64_P (output_bfd))
5948 {
5949 outrel[1].r_offset =
5950 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5951 outrel[2].r_offset =
5952 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5953 }
5954
5955 if (outrel[0].r_offset == MINUS_ONE)
5956 /* The relocation field has been deleted. */
5957 return TRUE;
5958
5959 if (outrel[0].r_offset == MINUS_TWO)
5960 {
5961 /* The relocation field has been converted into a relative value of
5962 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5963 the field to be fully relocated, so add in the symbol's value. */
5964 *addendp += symbol;
5965 return TRUE;
5966 }
5967
5968 /* We must now calculate the dynamic symbol table index to use
5969 in the relocation. */
5970 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5971 {
5972 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5973 indx = h->root.dynindx;
5974 if (SGI_COMPAT (output_bfd))
5975 defined_p = h->root.def_regular;
5976 else
5977 /* ??? glibc's ld.so just adds the final GOT entry to the
5978 relocation field. It therefore treats relocs against
5979 defined symbols in the same way as relocs against
5980 undefined symbols. */
5981 defined_p = FALSE;
5982 }
5983 else
5984 {
5985 if (sec != NULL && bfd_is_abs_section (sec))
5986 indx = 0;
5987 else if (sec == NULL || sec->owner == NULL)
5988 {
5989 bfd_set_error (bfd_error_bad_value);
5990 return FALSE;
5991 }
5992 else
5993 {
5994 indx = elf_section_data (sec->output_section)->dynindx;
5995 if (indx == 0)
5996 {
5997 asection *osec = htab->root.text_index_section;
5998 indx = elf_section_data (osec)->dynindx;
5999 }
6000 if (indx == 0)
6001 abort ();
6002 }
6003
6004 /* Instead of generating a relocation using the section
6005 symbol, we may as well make it a fully relative
6006 relocation. We want to avoid generating relocations to
6007 local symbols because we used to generate them
6008 incorrectly, without adding the original symbol value,
6009 which is mandated by the ABI for section symbols. In
6010 order to give dynamic loaders and applications time to
6011 phase out the incorrect use, we refrain from emitting
6012 section-relative relocations. It's not like they're
6013 useful, after all. This should be a bit more efficient
6014 as well. */
6015 /* ??? Although this behavior is compatible with glibc's ld.so,
6016 the ABI says that relocations against STN_UNDEF should have
6017 a symbol value of 0. Irix rld honors this, so relocations
6018 against STN_UNDEF have no effect. */
6019 if (!SGI_COMPAT (output_bfd))
6020 indx = 0;
6021 defined_p = TRUE;
6022 }
6023
6024 /* If the relocation was previously an absolute relocation and
6025 this symbol will not be referred to by the relocation, we must
6026 adjust it by the value we give it in the dynamic symbol table.
6027 Otherwise leave the job up to the dynamic linker. */
6028 if (defined_p && r_type != R_MIPS_REL32)
6029 *addendp += symbol;
6030
6031 if (htab->is_vxworks)
6032 /* VxWorks uses non-relative relocations for this. */
6033 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6034 else
6035 /* The relocation is always an REL32 relocation because we don't
6036 know where the shared library will wind up at load-time. */
6037 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6038 R_MIPS_REL32);
6039
6040 /* For strict adherence to the ABI specification, we should
6041 generate a R_MIPS_64 relocation record by itself before the
6042 _REL32/_64 record as well, such that the addend is read in as
6043 a 64-bit value (REL32 is a 32-bit relocation, after all).
6044 However, since none of the existing ELF64 MIPS dynamic
6045 loaders seems to care, we don't waste space with these
6046 artificial relocations. If this turns out to not be true,
6047 mips_elf_allocate_dynamic_relocation() should be tweaked so
6048 as to make room for a pair of dynamic relocations per
6049 invocation if ABI_64_P, and here we should generate an
6050 additional relocation record with R_MIPS_64 by itself for a
6051 NULL symbol before this relocation record. */
6052 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6053 ABI_64_P (output_bfd)
6054 ? R_MIPS_64
6055 : R_MIPS_NONE);
6056 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6057
6058 /* Adjust the output offset of the relocation to reference the
6059 correct location in the output file. */
6060 outrel[0].r_offset += (input_section->output_section->vma
6061 + input_section->output_offset);
6062 outrel[1].r_offset += (input_section->output_section->vma
6063 + input_section->output_offset);
6064 outrel[2].r_offset += (input_section->output_section->vma
6065 + input_section->output_offset);
6066
6067 /* Put the relocation back out. We have to use the special
6068 relocation outputter in the 64-bit case since the 64-bit
6069 relocation format is non-standard. */
6070 if (ABI_64_P (output_bfd))
6071 {
6072 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6073 (output_bfd, &outrel[0],
6074 (sreloc->contents
6075 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6076 }
6077 else if (htab->is_vxworks)
6078 {
6079 /* VxWorks uses RELA rather than REL dynamic relocations. */
6080 outrel[0].r_addend = *addendp;
6081 bfd_elf32_swap_reloca_out
6082 (output_bfd, &outrel[0],
6083 (sreloc->contents
6084 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6085 }
6086 else
6087 bfd_elf32_swap_reloc_out
6088 (output_bfd, &outrel[0],
6089 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6090
6091 /* We've now added another relocation. */
6092 ++sreloc->reloc_count;
6093
6094 /* Make sure the output section is writable. The dynamic linker
6095 will be writing to it. */
6096 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6097 |= SHF_WRITE;
6098
6099 /* On IRIX5, make an entry of compact relocation info. */
6100 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6101 {
6102 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
6103 bfd_byte *cr;
6104
6105 if (scpt)
6106 {
6107 Elf32_crinfo cptrel;
6108
6109 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6110 cptrel.vaddr = (rel->r_offset
6111 + input_section->output_section->vma
6112 + input_section->output_offset);
6113 if (r_type == R_MIPS_REL32)
6114 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6115 else
6116 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6117 mips_elf_set_cr_dist2to (cptrel, 0);
6118 cptrel.konst = *addendp;
6119
6120 cr = (scpt->contents
6121 + sizeof (Elf32_External_compact_rel));
6122 mips_elf_set_cr_relvaddr (cptrel, 0);
6123 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6124 ((Elf32_External_crinfo *) cr
6125 + scpt->reloc_count));
6126 ++scpt->reloc_count;
6127 }
6128 }
6129
6130 /* If we've written this relocation for a readonly section,
6131 we need to set DF_TEXTREL again, so that we do not delete the
6132 DT_TEXTREL tag. */
6133 if (MIPS_ELF_READONLY_SECTION (input_section))
6134 info->flags |= DF_TEXTREL;
6135
6136 return TRUE;
6137 }
6138 \f
6139 /* Return the MACH for a MIPS e_flags value. */
6140
6141 unsigned long
6142 _bfd_elf_mips_mach (flagword flags)
6143 {
6144 switch (flags & EF_MIPS_MACH)
6145 {
6146 case E_MIPS_MACH_3900:
6147 return bfd_mach_mips3900;
6148
6149 case E_MIPS_MACH_4010:
6150 return bfd_mach_mips4010;
6151
6152 case E_MIPS_MACH_4100:
6153 return bfd_mach_mips4100;
6154
6155 case E_MIPS_MACH_4111:
6156 return bfd_mach_mips4111;
6157
6158 case E_MIPS_MACH_4120:
6159 return bfd_mach_mips4120;
6160
6161 case E_MIPS_MACH_4650:
6162 return bfd_mach_mips4650;
6163
6164 case E_MIPS_MACH_5400:
6165 return bfd_mach_mips5400;
6166
6167 case E_MIPS_MACH_5500:
6168 return bfd_mach_mips5500;
6169
6170 case E_MIPS_MACH_9000:
6171 return bfd_mach_mips9000;
6172
6173 case E_MIPS_MACH_SB1:
6174 return bfd_mach_mips_sb1;
6175
6176 case E_MIPS_MACH_LS2E:
6177 return bfd_mach_mips_loongson_2e;
6178
6179 case E_MIPS_MACH_LS2F:
6180 return bfd_mach_mips_loongson_2f;
6181
6182 case E_MIPS_MACH_LS3A:
6183 return bfd_mach_mips_loongson_3a;
6184
6185 case E_MIPS_MACH_OCTEON2:
6186 return bfd_mach_mips_octeon2;
6187
6188 case E_MIPS_MACH_OCTEON:
6189 return bfd_mach_mips_octeon;
6190
6191 case E_MIPS_MACH_XLR:
6192 return bfd_mach_mips_xlr;
6193
6194 default:
6195 switch (flags & EF_MIPS_ARCH)
6196 {
6197 default:
6198 case E_MIPS_ARCH_1:
6199 return bfd_mach_mips3000;
6200
6201 case E_MIPS_ARCH_2:
6202 return bfd_mach_mips6000;
6203
6204 case E_MIPS_ARCH_3:
6205 return bfd_mach_mips4000;
6206
6207 case E_MIPS_ARCH_4:
6208 return bfd_mach_mips8000;
6209
6210 case E_MIPS_ARCH_5:
6211 return bfd_mach_mips5;
6212
6213 case E_MIPS_ARCH_32:
6214 return bfd_mach_mipsisa32;
6215
6216 case E_MIPS_ARCH_64:
6217 return bfd_mach_mipsisa64;
6218
6219 case E_MIPS_ARCH_32R2:
6220 return bfd_mach_mipsisa32r2;
6221
6222 case E_MIPS_ARCH_64R2:
6223 return bfd_mach_mipsisa64r2;
6224 }
6225 }
6226
6227 return 0;
6228 }
6229
6230 /* Return printable name for ABI. */
6231
6232 static INLINE char *
6233 elf_mips_abi_name (bfd *abfd)
6234 {
6235 flagword flags;
6236
6237 flags = elf_elfheader (abfd)->e_flags;
6238 switch (flags & EF_MIPS_ABI)
6239 {
6240 case 0:
6241 if (ABI_N32_P (abfd))
6242 return "N32";
6243 else if (ABI_64_P (abfd))
6244 return "64";
6245 else
6246 return "none";
6247 case E_MIPS_ABI_O32:
6248 return "O32";
6249 case E_MIPS_ABI_O64:
6250 return "O64";
6251 case E_MIPS_ABI_EABI32:
6252 return "EABI32";
6253 case E_MIPS_ABI_EABI64:
6254 return "EABI64";
6255 default:
6256 return "unknown abi";
6257 }
6258 }
6259 \f
6260 /* MIPS ELF uses two common sections. One is the usual one, and the
6261 other is for small objects. All the small objects are kept
6262 together, and then referenced via the gp pointer, which yields
6263 faster assembler code. This is what we use for the small common
6264 section. This approach is copied from ecoff.c. */
6265 static asection mips_elf_scom_section;
6266 static asymbol mips_elf_scom_symbol;
6267 static asymbol *mips_elf_scom_symbol_ptr;
6268
6269 /* MIPS ELF also uses an acommon section, which represents an
6270 allocated common symbol which may be overridden by a
6271 definition in a shared library. */
6272 static asection mips_elf_acom_section;
6273 static asymbol mips_elf_acom_symbol;
6274 static asymbol *mips_elf_acom_symbol_ptr;
6275
6276 /* This is used for both the 32-bit and the 64-bit ABI. */
6277
6278 void
6279 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6280 {
6281 elf_symbol_type *elfsym;
6282
6283 /* Handle the special MIPS section numbers that a symbol may use. */
6284 elfsym = (elf_symbol_type *) asym;
6285 switch (elfsym->internal_elf_sym.st_shndx)
6286 {
6287 case SHN_MIPS_ACOMMON:
6288 /* This section is used in a dynamically linked executable file.
6289 It is an allocated common section. The dynamic linker can
6290 either resolve these symbols to something in a shared
6291 library, or it can just leave them here. For our purposes,
6292 we can consider these symbols to be in a new section. */
6293 if (mips_elf_acom_section.name == NULL)
6294 {
6295 /* Initialize the acommon section. */
6296 mips_elf_acom_section.name = ".acommon";
6297 mips_elf_acom_section.flags = SEC_ALLOC;
6298 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6299 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6300 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6301 mips_elf_acom_symbol.name = ".acommon";
6302 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6303 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6304 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6305 }
6306 asym->section = &mips_elf_acom_section;
6307 break;
6308
6309 case SHN_COMMON:
6310 /* Common symbols less than the GP size are automatically
6311 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6312 if (asym->value > elf_gp_size (abfd)
6313 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6314 || IRIX_COMPAT (abfd) == ict_irix6)
6315 break;
6316 /* Fall through. */
6317 case SHN_MIPS_SCOMMON:
6318 if (mips_elf_scom_section.name == NULL)
6319 {
6320 /* Initialize the small common section. */
6321 mips_elf_scom_section.name = ".scommon";
6322 mips_elf_scom_section.flags = SEC_IS_COMMON;
6323 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6324 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6325 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6326 mips_elf_scom_symbol.name = ".scommon";
6327 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6328 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6329 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6330 }
6331 asym->section = &mips_elf_scom_section;
6332 asym->value = elfsym->internal_elf_sym.st_size;
6333 break;
6334
6335 case SHN_MIPS_SUNDEFINED:
6336 asym->section = bfd_und_section_ptr;
6337 break;
6338
6339 case SHN_MIPS_TEXT:
6340 {
6341 asection *section = bfd_get_section_by_name (abfd, ".text");
6342
6343 if (section != NULL)
6344 {
6345 asym->section = section;
6346 /* MIPS_TEXT is a bit special, the address is not an offset
6347 to the base of the .text section. So substract the section
6348 base address to make it an offset. */
6349 asym->value -= section->vma;
6350 }
6351 }
6352 break;
6353
6354 case SHN_MIPS_DATA:
6355 {
6356 asection *section = bfd_get_section_by_name (abfd, ".data");
6357
6358 if (section != NULL)
6359 {
6360 asym->section = section;
6361 /* MIPS_DATA is a bit special, the address is not an offset
6362 to the base of the .data section. So substract the section
6363 base address to make it an offset. */
6364 asym->value -= section->vma;
6365 }
6366 }
6367 break;
6368 }
6369
6370 /* If this is an odd-valued function symbol, assume it's a MIPS16
6371 or microMIPS one. */
6372 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6373 && (asym->value & 1) != 0)
6374 {
6375 asym->value--;
6376 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
6377 elfsym->internal_elf_sym.st_other
6378 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6379 else
6380 elfsym->internal_elf_sym.st_other
6381 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6382 }
6383 }
6384 \f
6385 /* Implement elf_backend_eh_frame_address_size. This differs from
6386 the default in the way it handles EABI64.
6387
6388 EABI64 was originally specified as an LP64 ABI, and that is what
6389 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6390 historically accepted the combination of -mabi=eabi and -mlong32,
6391 and this ILP32 variation has become semi-official over time.
6392 Both forms use elf32 and have pointer-sized FDE addresses.
6393
6394 If an EABI object was generated by GCC 4.0 or above, it will have
6395 an empty .gcc_compiled_longXX section, where XX is the size of longs
6396 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6397 have no special marking to distinguish them from LP64 objects.
6398
6399 We don't want users of the official LP64 ABI to be punished for the
6400 existence of the ILP32 variant, but at the same time, we don't want
6401 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6402 We therefore take the following approach:
6403
6404 - If ABFD contains a .gcc_compiled_longXX section, use it to
6405 determine the pointer size.
6406
6407 - Otherwise check the type of the first relocation. Assume that
6408 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6409
6410 - Otherwise punt.
6411
6412 The second check is enough to detect LP64 objects generated by pre-4.0
6413 compilers because, in the kind of output generated by those compilers,
6414 the first relocation will be associated with either a CIE personality
6415 routine or an FDE start address. Furthermore, the compilers never
6416 used a special (non-pointer) encoding for this ABI.
6417
6418 Checking the relocation type should also be safe because there is no
6419 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6420 did so. */
6421
6422 unsigned int
6423 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6424 {
6425 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6426 return 8;
6427 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6428 {
6429 bfd_boolean long32_p, long64_p;
6430
6431 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6432 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6433 if (long32_p && long64_p)
6434 return 0;
6435 if (long32_p)
6436 return 4;
6437 if (long64_p)
6438 return 8;
6439
6440 if (sec->reloc_count > 0
6441 && elf_section_data (sec)->relocs != NULL
6442 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6443 == R_MIPS_64))
6444 return 8;
6445
6446 return 0;
6447 }
6448 return 4;
6449 }
6450 \f
6451 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6452 relocations against two unnamed section symbols to resolve to the
6453 same address. For example, if we have code like:
6454
6455 lw $4,%got_disp(.data)($gp)
6456 lw $25,%got_disp(.text)($gp)
6457 jalr $25
6458
6459 then the linker will resolve both relocations to .data and the program
6460 will jump there rather than to .text.
6461
6462 We can work around this problem by giving names to local section symbols.
6463 This is also what the MIPSpro tools do. */
6464
6465 bfd_boolean
6466 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6467 {
6468 return SGI_COMPAT (abfd);
6469 }
6470 \f
6471 /* Work over a section just before writing it out. This routine is
6472 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6473 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6474 a better way. */
6475
6476 bfd_boolean
6477 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6478 {
6479 if (hdr->sh_type == SHT_MIPS_REGINFO
6480 && hdr->sh_size > 0)
6481 {
6482 bfd_byte buf[4];
6483
6484 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6485 BFD_ASSERT (hdr->contents == NULL);
6486
6487 if (bfd_seek (abfd,
6488 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6489 SEEK_SET) != 0)
6490 return FALSE;
6491 H_PUT_32 (abfd, elf_gp (abfd), buf);
6492 if (bfd_bwrite (buf, 4, abfd) != 4)
6493 return FALSE;
6494 }
6495
6496 if (hdr->sh_type == SHT_MIPS_OPTIONS
6497 && hdr->bfd_section != NULL
6498 && mips_elf_section_data (hdr->bfd_section) != NULL
6499 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6500 {
6501 bfd_byte *contents, *l, *lend;
6502
6503 /* We stored the section contents in the tdata field in the
6504 set_section_contents routine. We save the section contents
6505 so that we don't have to read them again.
6506 At this point we know that elf_gp is set, so we can look
6507 through the section contents to see if there is an
6508 ODK_REGINFO structure. */
6509
6510 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6511 l = contents;
6512 lend = contents + hdr->sh_size;
6513 while (l + sizeof (Elf_External_Options) <= lend)
6514 {
6515 Elf_Internal_Options intopt;
6516
6517 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6518 &intopt);
6519 if (intopt.size < sizeof (Elf_External_Options))
6520 {
6521 (*_bfd_error_handler)
6522 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6523 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6524 break;
6525 }
6526 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6527 {
6528 bfd_byte buf[8];
6529
6530 if (bfd_seek (abfd,
6531 (hdr->sh_offset
6532 + (l - contents)
6533 + sizeof (Elf_External_Options)
6534 + (sizeof (Elf64_External_RegInfo) - 8)),
6535 SEEK_SET) != 0)
6536 return FALSE;
6537 H_PUT_64 (abfd, elf_gp (abfd), buf);
6538 if (bfd_bwrite (buf, 8, abfd) != 8)
6539 return FALSE;
6540 }
6541 else if (intopt.kind == ODK_REGINFO)
6542 {
6543 bfd_byte buf[4];
6544
6545 if (bfd_seek (abfd,
6546 (hdr->sh_offset
6547 + (l - contents)
6548 + sizeof (Elf_External_Options)
6549 + (sizeof (Elf32_External_RegInfo) - 4)),
6550 SEEK_SET) != 0)
6551 return FALSE;
6552 H_PUT_32 (abfd, elf_gp (abfd), buf);
6553 if (bfd_bwrite (buf, 4, abfd) != 4)
6554 return FALSE;
6555 }
6556 l += intopt.size;
6557 }
6558 }
6559
6560 if (hdr->bfd_section != NULL)
6561 {
6562 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6563
6564 /* .sbss is not handled specially here because the GNU/Linux
6565 prelinker can convert .sbss from NOBITS to PROGBITS and
6566 changing it back to NOBITS breaks the binary. The entry in
6567 _bfd_mips_elf_special_sections will ensure the correct flags
6568 are set on .sbss if BFD creates it without reading it from an
6569 input file, and without special handling here the flags set
6570 on it in an input file will be followed. */
6571 if (strcmp (name, ".sdata") == 0
6572 || strcmp (name, ".lit8") == 0
6573 || strcmp (name, ".lit4") == 0)
6574 {
6575 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6576 hdr->sh_type = SHT_PROGBITS;
6577 }
6578 else if (strcmp (name, ".srdata") == 0)
6579 {
6580 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6581 hdr->sh_type = SHT_PROGBITS;
6582 }
6583 else if (strcmp (name, ".compact_rel") == 0)
6584 {
6585 hdr->sh_flags = 0;
6586 hdr->sh_type = SHT_PROGBITS;
6587 }
6588 else if (strcmp (name, ".rtproc") == 0)
6589 {
6590 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6591 {
6592 unsigned int adjust;
6593
6594 adjust = hdr->sh_size % hdr->sh_addralign;
6595 if (adjust != 0)
6596 hdr->sh_size += hdr->sh_addralign - adjust;
6597 }
6598 }
6599 }
6600
6601 return TRUE;
6602 }
6603
6604 /* Handle a MIPS specific section when reading an object file. This
6605 is called when elfcode.h finds a section with an unknown type.
6606 This routine supports both the 32-bit and 64-bit ELF ABI.
6607
6608 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6609 how to. */
6610
6611 bfd_boolean
6612 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6613 Elf_Internal_Shdr *hdr,
6614 const char *name,
6615 int shindex)
6616 {
6617 flagword flags = 0;
6618
6619 /* There ought to be a place to keep ELF backend specific flags, but
6620 at the moment there isn't one. We just keep track of the
6621 sections by their name, instead. Fortunately, the ABI gives
6622 suggested names for all the MIPS specific sections, so we will
6623 probably get away with this. */
6624 switch (hdr->sh_type)
6625 {
6626 case SHT_MIPS_LIBLIST:
6627 if (strcmp (name, ".liblist") != 0)
6628 return FALSE;
6629 break;
6630 case SHT_MIPS_MSYM:
6631 if (strcmp (name, ".msym") != 0)
6632 return FALSE;
6633 break;
6634 case SHT_MIPS_CONFLICT:
6635 if (strcmp (name, ".conflict") != 0)
6636 return FALSE;
6637 break;
6638 case SHT_MIPS_GPTAB:
6639 if (! CONST_STRNEQ (name, ".gptab."))
6640 return FALSE;
6641 break;
6642 case SHT_MIPS_UCODE:
6643 if (strcmp (name, ".ucode") != 0)
6644 return FALSE;
6645 break;
6646 case SHT_MIPS_DEBUG:
6647 if (strcmp (name, ".mdebug") != 0)
6648 return FALSE;
6649 flags = SEC_DEBUGGING;
6650 break;
6651 case SHT_MIPS_REGINFO:
6652 if (strcmp (name, ".reginfo") != 0
6653 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6654 return FALSE;
6655 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6656 break;
6657 case SHT_MIPS_IFACE:
6658 if (strcmp (name, ".MIPS.interfaces") != 0)
6659 return FALSE;
6660 break;
6661 case SHT_MIPS_CONTENT:
6662 if (! CONST_STRNEQ (name, ".MIPS.content"))
6663 return FALSE;
6664 break;
6665 case SHT_MIPS_OPTIONS:
6666 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6667 return FALSE;
6668 break;
6669 case SHT_MIPS_DWARF:
6670 if (! CONST_STRNEQ (name, ".debug_")
6671 && ! CONST_STRNEQ (name, ".zdebug_"))
6672 return FALSE;
6673 break;
6674 case SHT_MIPS_SYMBOL_LIB:
6675 if (strcmp (name, ".MIPS.symlib") != 0)
6676 return FALSE;
6677 break;
6678 case SHT_MIPS_EVENTS:
6679 if (! CONST_STRNEQ (name, ".MIPS.events")
6680 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6681 return FALSE;
6682 break;
6683 default:
6684 break;
6685 }
6686
6687 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6688 return FALSE;
6689
6690 if (flags)
6691 {
6692 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6693 (bfd_get_section_flags (abfd,
6694 hdr->bfd_section)
6695 | flags)))
6696 return FALSE;
6697 }
6698
6699 /* FIXME: We should record sh_info for a .gptab section. */
6700
6701 /* For a .reginfo section, set the gp value in the tdata information
6702 from the contents of this section. We need the gp value while
6703 processing relocs, so we just get it now. The .reginfo section
6704 is not used in the 64-bit MIPS ELF ABI. */
6705 if (hdr->sh_type == SHT_MIPS_REGINFO)
6706 {
6707 Elf32_External_RegInfo ext;
6708 Elf32_RegInfo s;
6709
6710 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6711 &ext, 0, sizeof ext))
6712 return FALSE;
6713 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6714 elf_gp (abfd) = s.ri_gp_value;
6715 }
6716
6717 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6718 set the gp value based on what we find. We may see both
6719 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6720 they should agree. */
6721 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6722 {
6723 bfd_byte *contents, *l, *lend;
6724
6725 contents = bfd_malloc (hdr->sh_size);
6726 if (contents == NULL)
6727 return FALSE;
6728 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6729 0, hdr->sh_size))
6730 {
6731 free (contents);
6732 return FALSE;
6733 }
6734 l = contents;
6735 lend = contents + hdr->sh_size;
6736 while (l + sizeof (Elf_External_Options) <= lend)
6737 {
6738 Elf_Internal_Options intopt;
6739
6740 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6741 &intopt);
6742 if (intopt.size < sizeof (Elf_External_Options))
6743 {
6744 (*_bfd_error_handler)
6745 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6746 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6747 break;
6748 }
6749 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6750 {
6751 Elf64_Internal_RegInfo intreg;
6752
6753 bfd_mips_elf64_swap_reginfo_in
6754 (abfd,
6755 ((Elf64_External_RegInfo *)
6756 (l + sizeof (Elf_External_Options))),
6757 &intreg);
6758 elf_gp (abfd) = intreg.ri_gp_value;
6759 }
6760 else if (intopt.kind == ODK_REGINFO)
6761 {
6762 Elf32_RegInfo intreg;
6763
6764 bfd_mips_elf32_swap_reginfo_in
6765 (abfd,
6766 ((Elf32_External_RegInfo *)
6767 (l + sizeof (Elf_External_Options))),
6768 &intreg);
6769 elf_gp (abfd) = intreg.ri_gp_value;
6770 }
6771 l += intopt.size;
6772 }
6773 free (contents);
6774 }
6775
6776 return TRUE;
6777 }
6778
6779 /* Set the correct type for a MIPS ELF section. We do this by the
6780 section name, which is a hack, but ought to work. This routine is
6781 used by both the 32-bit and the 64-bit ABI. */
6782
6783 bfd_boolean
6784 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6785 {
6786 const char *name = bfd_get_section_name (abfd, sec);
6787
6788 if (strcmp (name, ".liblist") == 0)
6789 {
6790 hdr->sh_type = SHT_MIPS_LIBLIST;
6791 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6792 /* The sh_link field is set in final_write_processing. */
6793 }
6794 else if (strcmp (name, ".conflict") == 0)
6795 hdr->sh_type = SHT_MIPS_CONFLICT;
6796 else if (CONST_STRNEQ (name, ".gptab."))
6797 {
6798 hdr->sh_type = SHT_MIPS_GPTAB;
6799 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6800 /* The sh_info field is set in final_write_processing. */
6801 }
6802 else if (strcmp (name, ".ucode") == 0)
6803 hdr->sh_type = SHT_MIPS_UCODE;
6804 else if (strcmp (name, ".mdebug") == 0)
6805 {
6806 hdr->sh_type = SHT_MIPS_DEBUG;
6807 /* In a shared object on IRIX 5.3, the .mdebug section has an
6808 entsize of 0. FIXME: Does this matter? */
6809 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6810 hdr->sh_entsize = 0;
6811 else
6812 hdr->sh_entsize = 1;
6813 }
6814 else if (strcmp (name, ".reginfo") == 0)
6815 {
6816 hdr->sh_type = SHT_MIPS_REGINFO;
6817 /* In a shared object on IRIX 5.3, the .reginfo section has an
6818 entsize of 0x18. FIXME: Does this matter? */
6819 if (SGI_COMPAT (abfd))
6820 {
6821 if ((abfd->flags & DYNAMIC) != 0)
6822 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6823 else
6824 hdr->sh_entsize = 1;
6825 }
6826 else
6827 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6828 }
6829 else if (SGI_COMPAT (abfd)
6830 && (strcmp (name, ".hash") == 0
6831 || strcmp (name, ".dynamic") == 0
6832 || strcmp (name, ".dynstr") == 0))
6833 {
6834 if (SGI_COMPAT (abfd))
6835 hdr->sh_entsize = 0;
6836 #if 0
6837 /* This isn't how the IRIX6 linker behaves. */
6838 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6839 #endif
6840 }
6841 else if (strcmp (name, ".got") == 0
6842 || strcmp (name, ".srdata") == 0
6843 || strcmp (name, ".sdata") == 0
6844 || strcmp (name, ".sbss") == 0
6845 || strcmp (name, ".lit4") == 0
6846 || strcmp (name, ".lit8") == 0)
6847 hdr->sh_flags |= SHF_MIPS_GPREL;
6848 else if (strcmp (name, ".MIPS.interfaces") == 0)
6849 {
6850 hdr->sh_type = SHT_MIPS_IFACE;
6851 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6852 }
6853 else if (CONST_STRNEQ (name, ".MIPS.content"))
6854 {
6855 hdr->sh_type = SHT_MIPS_CONTENT;
6856 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6857 /* The sh_info field is set in final_write_processing. */
6858 }
6859 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6860 {
6861 hdr->sh_type = SHT_MIPS_OPTIONS;
6862 hdr->sh_entsize = 1;
6863 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6864 }
6865 else if (CONST_STRNEQ (name, ".debug_")
6866 || CONST_STRNEQ (name, ".zdebug_"))
6867 {
6868 hdr->sh_type = SHT_MIPS_DWARF;
6869
6870 /* Irix facilities such as libexc expect a single .debug_frame
6871 per executable, the system ones have NOSTRIP set and the linker
6872 doesn't merge sections with different flags so ... */
6873 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6874 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6875 }
6876 else if (strcmp (name, ".MIPS.symlib") == 0)
6877 {
6878 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6879 /* The sh_link and sh_info fields are set in
6880 final_write_processing. */
6881 }
6882 else if (CONST_STRNEQ (name, ".MIPS.events")
6883 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6884 {
6885 hdr->sh_type = SHT_MIPS_EVENTS;
6886 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6887 /* The sh_link field is set in final_write_processing. */
6888 }
6889 else if (strcmp (name, ".msym") == 0)
6890 {
6891 hdr->sh_type = SHT_MIPS_MSYM;
6892 hdr->sh_flags |= SHF_ALLOC;
6893 hdr->sh_entsize = 8;
6894 }
6895
6896 /* The generic elf_fake_sections will set up REL_HDR using the default
6897 kind of relocations. We used to set up a second header for the
6898 non-default kind of relocations here, but only NewABI would use
6899 these, and the IRIX ld doesn't like resulting empty RELA sections.
6900 Thus we create those header only on demand now. */
6901
6902 return TRUE;
6903 }
6904
6905 /* Given a BFD section, try to locate the corresponding ELF section
6906 index. This is used by both the 32-bit and the 64-bit ABI.
6907 Actually, it's not clear to me that the 64-bit ABI supports these,
6908 but for non-PIC objects we will certainly want support for at least
6909 the .scommon section. */
6910
6911 bfd_boolean
6912 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6913 asection *sec, int *retval)
6914 {
6915 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6916 {
6917 *retval = SHN_MIPS_SCOMMON;
6918 return TRUE;
6919 }
6920 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6921 {
6922 *retval = SHN_MIPS_ACOMMON;
6923 return TRUE;
6924 }
6925 return FALSE;
6926 }
6927 \f
6928 /* Hook called by the linker routine which adds symbols from an object
6929 file. We must handle the special MIPS section numbers here. */
6930
6931 bfd_boolean
6932 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6933 Elf_Internal_Sym *sym, const char **namep,
6934 flagword *flagsp ATTRIBUTE_UNUSED,
6935 asection **secp, bfd_vma *valp)
6936 {
6937 if (SGI_COMPAT (abfd)
6938 && (abfd->flags & DYNAMIC) != 0
6939 && strcmp (*namep, "_rld_new_interface") == 0)
6940 {
6941 /* Skip IRIX5 rld entry name. */
6942 *namep = NULL;
6943 return TRUE;
6944 }
6945
6946 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6947 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6948 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6949 a magic symbol resolved by the linker, we ignore this bogus definition
6950 of _gp_disp. New ABI objects do not suffer from this problem so this
6951 is not done for them. */
6952 if (!NEWABI_P(abfd)
6953 && (sym->st_shndx == SHN_ABS)
6954 && (strcmp (*namep, "_gp_disp") == 0))
6955 {
6956 *namep = NULL;
6957 return TRUE;
6958 }
6959
6960 switch (sym->st_shndx)
6961 {
6962 case SHN_COMMON:
6963 /* Common symbols less than the GP size are automatically
6964 treated as SHN_MIPS_SCOMMON symbols. */
6965 if (sym->st_size > elf_gp_size (abfd)
6966 || ELF_ST_TYPE (sym->st_info) == STT_TLS
6967 || IRIX_COMPAT (abfd) == ict_irix6)
6968 break;
6969 /* Fall through. */
6970 case SHN_MIPS_SCOMMON:
6971 *secp = bfd_make_section_old_way (abfd, ".scommon");
6972 (*secp)->flags |= SEC_IS_COMMON;
6973 *valp = sym->st_size;
6974 break;
6975
6976 case SHN_MIPS_TEXT:
6977 /* This section is used in a shared object. */
6978 if (elf_tdata (abfd)->elf_text_section == NULL)
6979 {
6980 asymbol *elf_text_symbol;
6981 asection *elf_text_section;
6982 bfd_size_type amt = sizeof (asection);
6983
6984 elf_text_section = bfd_zalloc (abfd, amt);
6985 if (elf_text_section == NULL)
6986 return FALSE;
6987
6988 amt = sizeof (asymbol);
6989 elf_text_symbol = bfd_zalloc (abfd, amt);
6990 if (elf_text_symbol == NULL)
6991 return FALSE;
6992
6993 /* Initialize the section. */
6994
6995 elf_tdata (abfd)->elf_text_section = elf_text_section;
6996 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6997
6998 elf_text_section->symbol = elf_text_symbol;
6999 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
7000
7001 elf_text_section->name = ".text";
7002 elf_text_section->flags = SEC_NO_FLAGS;
7003 elf_text_section->output_section = NULL;
7004 elf_text_section->owner = abfd;
7005 elf_text_symbol->name = ".text";
7006 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7007 elf_text_symbol->section = elf_text_section;
7008 }
7009 /* This code used to do *secp = bfd_und_section_ptr if
7010 info->shared. I don't know why, and that doesn't make sense,
7011 so I took it out. */
7012 *secp = elf_tdata (abfd)->elf_text_section;
7013 break;
7014
7015 case SHN_MIPS_ACOMMON:
7016 /* Fall through. XXX Can we treat this as allocated data? */
7017 case SHN_MIPS_DATA:
7018 /* This section is used in a shared object. */
7019 if (elf_tdata (abfd)->elf_data_section == NULL)
7020 {
7021 asymbol *elf_data_symbol;
7022 asection *elf_data_section;
7023 bfd_size_type amt = sizeof (asection);
7024
7025 elf_data_section = bfd_zalloc (abfd, amt);
7026 if (elf_data_section == NULL)
7027 return FALSE;
7028
7029 amt = sizeof (asymbol);
7030 elf_data_symbol = bfd_zalloc (abfd, amt);
7031 if (elf_data_symbol == NULL)
7032 return FALSE;
7033
7034 /* Initialize the section. */
7035
7036 elf_tdata (abfd)->elf_data_section = elf_data_section;
7037 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7038
7039 elf_data_section->symbol = elf_data_symbol;
7040 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
7041
7042 elf_data_section->name = ".data";
7043 elf_data_section->flags = SEC_NO_FLAGS;
7044 elf_data_section->output_section = NULL;
7045 elf_data_section->owner = abfd;
7046 elf_data_symbol->name = ".data";
7047 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7048 elf_data_symbol->section = elf_data_section;
7049 }
7050 /* This code used to do *secp = bfd_und_section_ptr if
7051 info->shared. I don't know why, and that doesn't make sense,
7052 so I took it out. */
7053 *secp = elf_tdata (abfd)->elf_data_section;
7054 break;
7055
7056 case SHN_MIPS_SUNDEFINED:
7057 *secp = bfd_und_section_ptr;
7058 break;
7059 }
7060
7061 if (SGI_COMPAT (abfd)
7062 && ! info->shared
7063 && info->output_bfd->xvec == abfd->xvec
7064 && strcmp (*namep, "__rld_obj_head") == 0)
7065 {
7066 struct elf_link_hash_entry *h;
7067 struct bfd_link_hash_entry *bh;
7068
7069 /* Mark __rld_obj_head as dynamic. */
7070 bh = NULL;
7071 if (! (_bfd_generic_link_add_one_symbol
7072 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7073 get_elf_backend_data (abfd)->collect, &bh)))
7074 return FALSE;
7075
7076 h = (struct elf_link_hash_entry *) bh;
7077 h->non_elf = 0;
7078 h->def_regular = 1;
7079 h->type = STT_OBJECT;
7080
7081 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7082 return FALSE;
7083
7084 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7085 }
7086
7087 /* If this is a mips16 text symbol, add 1 to the value to make it
7088 odd. This will cause something like .word SYM to come up with
7089 the right value when it is loaded into the PC. */
7090 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7091 ++*valp;
7092
7093 return TRUE;
7094 }
7095
7096 /* This hook function is called before the linker writes out a global
7097 symbol. We mark symbols as small common if appropriate. This is
7098 also where we undo the increment of the value for a mips16 symbol. */
7099
7100 int
7101 _bfd_mips_elf_link_output_symbol_hook
7102 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7103 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7104 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7105 {
7106 /* If we see a common symbol, which implies a relocatable link, then
7107 if a symbol was small common in an input file, mark it as small
7108 common in the output file. */
7109 if (sym->st_shndx == SHN_COMMON
7110 && strcmp (input_sec->name, ".scommon") == 0)
7111 sym->st_shndx = SHN_MIPS_SCOMMON;
7112
7113 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7114 sym->st_value &= ~1;
7115
7116 return 1;
7117 }
7118 \f
7119 /* Functions for the dynamic linker. */
7120
7121 /* Create dynamic sections when linking against a dynamic object. */
7122
7123 bfd_boolean
7124 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7125 {
7126 struct elf_link_hash_entry *h;
7127 struct bfd_link_hash_entry *bh;
7128 flagword flags;
7129 register asection *s;
7130 const char * const *namep;
7131 struct mips_elf_link_hash_table *htab;
7132
7133 htab = mips_elf_hash_table (info);
7134 BFD_ASSERT (htab != NULL);
7135
7136 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7137 | SEC_LINKER_CREATED | SEC_READONLY);
7138
7139 /* The psABI requires a read-only .dynamic section, but the VxWorks
7140 EABI doesn't. */
7141 if (!htab->is_vxworks)
7142 {
7143 s = bfd_get_section_by_name (abfd, ".dynamic");
7144 if (s != NULL)
7145 {
7146 if (! bfd_set_section_flags (abfd, s, flags))
7147 return FALSE;
7148 }
7149 }
7150
7151 /* We need to create .got section. */
7152 if (!mips_elf_create_got_section (abfd, info))
7153 return FALSE;
7154
7155 if (! mips_elf_rel_dyn_section (info, TRUE))
7156 return FALSE;
7157
7158 /* Create .stub section. */
7159 s = bfd_make_section_with_flags (abfd,
7160 MIPS_ELF_STUB_SECTION_NAME (abfd),
7161 flags | SEC_CODE);
7162 if (s == NULL
7163 || ! bfd_set_section_alignment (abfd, s,
7164 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7165 return FALSE;
7166 htab->sstubs = s;
7167
7168 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
7169 && !info->shared
7170 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
7171 {
7172 s = bfd_make_section_with_flags (abfd, ".rld_map",
7173 flags &~ (flagword) SEC_READONLY);
7174 if (s == NULL
7175 || ! bfd_set_section_alignment (abfd, s,
7176 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7177 return FALSE;
7178 }
7179
7180 /* On IRIX5, we adjust add some additional symbols and change the
7181 alignments of several sections. There is no ABI documentation
7182 indicating that this is necessary on IRIX6, nor any evidence that
7183 the linker takes such action. */
7184 if (IRIX_COMPAT (abfd) == ict_irix5)
7185 {
7186 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7187 {
7188 bh = NULL;
7189 if (! (_bfd_generic_link_add_one_symbol
7190 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7191 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7192 return FALSE;
7193
7194 h = (struct elf_link_hash_entry *) bh;
7195 h->non_elf = 0;
7196 h->def_regular = 1;
7197 h->type = STT_SECTION;
7198
7199 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7200 return FALSE;
7201 }
7202
7203 /* We need to create a .compact_rel section. */
7204 if (SGI_COMPAT (abfd))
7205 {
7206 if (!mips_elf_create_compact_rel_section (abfd, info))
7207 return FALSE;
7208 }
7209
7210 /* Change alignments of some sections. */
7211 s = bfd_get_section_by_name (abfd, ".hash");
7212 if (s != NULL)
7213 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7214 s = bfd_get_section_by_name (abfd, ".dynsym");
7215 if (s != NULL)
7216 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7217 s = bfd_get_section_by_name (abfd, ".dynstr");
7218 if (s != NULL)
7219 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7220 s = bfd_get_section_by_name (abfd, ".reginfo");
7221 if (s != NULL)
7222 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7223 s = bfd_get_section_by_name (abfd, ".dynamic");
7224 if (s != NULL)
7225 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7226 }
7227
7228 if (!info->shared)
7229 {
7230 const char *name;
7231
7232 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7233 bh = NULL;
7234 if (!(_bfd_generic_link_add_one_symbol
7235 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7236 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7237 return FALSE;
7238
7239 h = (struct elf_link_hash_entry *) bh;
7240 h->non_elf = 0;
7241 h->def_regular = 1;
7242 h->type = STT_SECTION;
7243
7244 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7245 return FALSE;
7246
7247 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7248 {
7249 /* __rld_map is a four byte word located in the .data section
7250 and is filled in by the rtld to contain a pointer to
7251 the _r_debug structure. Its symbol value will be set in
7252 _bfd_mips_elf_finish_dynamic_symbol. */
7253 s = bfd_get_section_by_name (abfd, ".rld_map");
7254 BFD_ASSERT (s != NULL);
7255
7256 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7257 bh = NULL;
7258 if (!(_bfd_generic_link_add_one_symbol
7259 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7260 get_elf_backend_data (abfd)->collect, &bh)))
7261 return FALSE;
7262
7263 h = (struct elf_link_hash_entry *) bh;
7264 h->non_elf = 0;
7265 h->def_regular = 1;
7266 h->type = STT_OBJECT;
7267
7268 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7269 return FALSE;
7270 }
7271 }
7272
7273 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7274 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7275 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7276 return FALSE;
7277
7278 /* Cache the sections created above. */
7279 htab->splt = bfd_get_section_by_name (abfd, ".plt");
7280 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
7281 if (htab->is_vxworks)
7282 {
7283 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7284 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
7285 }
7286 else
7287 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7288 if (!htab->sdynbss
7289 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7290 || !htab->srelplt
7291 || !htab->splt)
7292 abort ();
7293
7294 if (htab->is_vxworks)
7295 {
7296 /* Do the usual VxWorks handling. */
7297 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7298 return FALSE;
7299
7300 /* Work out the PLT sizes. */
7301 if (info->shared)
7302 {
7303 htab->plt_header_size
7304 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7305 htab->plt_entry_size
7306 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7307 }
7308 else
7309 {
7310 htab->plt_header_size
7311 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7312 htab->plt_entry_size
7313 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7314 }
7315 }
7316 else if (!info->shared)
7317 {
7318 /* All variants of the plt0 entry are the same size. */
7319 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7320 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7321 }
7322
7323 return TRUE;
7324 }
7325 \f
7326 /* Return true if relocation REL against section SEC is a REL rather than
7327 RELA relocation. RELOCS is the first relocation in the section and
7328 ABFD is the bfd that contains SEC. */
7329
7330 static bfd_boolean
7331 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7332 const Elf_Internal_Rela *relocs,
7333 const Elf_Internal_Rela *rel)
7334 {
7335 Elf_Internal_Shdr *rel_hdr;
7336 const struct elf_backend_data *bed;
7337
7338 /* To determine which flavor of relocation this is, we depend on the
7339 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7340 rel_hdr = elf_section_data (sec)->rel.hdr;
7341 if (rel_hdr == NULL)
7342 return FALSE;
7343 bed = get_elf_backend_data (abfd);
7344 return ((size_t) (rel - relocs)
7345 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7346 }
7347
7348 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7349 HOWTO is the relocation's howto and CONTENTS points to the contents
7350 of the section that REL is against. */
7351
7352 static bfd_vma
7353 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7354 reloc_howto_type *howto, bfd_byte *contents)
7355 {
7356 bfd_byte *location;
7357 unsigned int r_type;
7358 bfd_vma addend;
7359
7360 r_type = ELF_R_TYPE (abfd, rel->r_info);
7361 location = contents + rel->r_offset;
7362
7363 /* Get the addend, which is stored in the input file. */
7364 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7365 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7366 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7367
7368 return addend & howto->src_mask;
7369 }
7370
7371 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7372 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7373 and update *ADDEND with the final addend. Return true on success
7374 or false if the LO16 could not be found. RELEND is the exclusive
7375 upper bound on the relocations for REL's section. */
7376
7377 static bfd_boolean
7378 mips_elf_add_lo16_rel_addend (bfd *abfd,
7379 const Elf_Internal_Rela *rel,
7380 const Elf_Internal_Rela *relend,
7381 bfd_byte *contents, bfd_vma *addend)
7382 {
7383 unsigned int r_type, lo16_type;
7384 const Elf_Internal_Rela *lo16_relocation;
7385 reloc_howto_type *lo16_howto;
7386 bfd_vma l;
7387
7388 r_type = ELF_R_TYPE (abfd, rel->r_info);
7389 if (mips16_reloc_p (r_type))
7390 lo16_type = R_MIPS16_LO16;
7391 else if (micromips_reloc_p (r_type))
7392 lo16_type = R_MICROMIPS_LO16;
7393 else
7394 lo16_type = R_MIPS_LO16;
7395
7396 /* The combined value is the sum of the HI16 addend, left-shifted by
7397 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7398 code does a `lui' of the HI16 value, and then an `addiu' of the
7399 LO16 value.)
7400
7401 Scan ahead to find a matching LO16 relocation.
7402
7403 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7404 be immediately following. However, for the IRIX6 ABI, the next
7405 relocation may be a composed relocation consisting of several
7406 relocations for the same address. In that case, the R_MIPS_LO16
7407 relocation may occur as one of these. We permit a similar
7408 extension in general, as that is useful for GCC.
7409
7410 In some cases GCC dead code elimination removes the LO16 but keeps
7411 the corresponding HI16. This is strictly speaking a violation of
7412 the ABI but not immediately harmful. */
7413 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7414 if (lo16_relocation == NULL)
7415 return FALSE;
7416
7417 /* Obtain the addend kept there. */
7418 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7419 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7420
7421 l <<= lo16_howto->rightshift;
7422 l = _bfd_mips_elf_sign_extend (l, 16);
7423
7424 *addend <<= 16;
7425 *addend += l;
7426 return TRUE;
7427 }
7428
7429 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7430 store the contents in *CONTENTS on success. Assume that *CONTENTS
7431 already holds the contents if it is nonull on entry. */
7432
7433 static bfd_boolean
7434 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7435 {
7436 if (*contents)
7437 return TRUE;
7438
7439 /* Get cached copy if it exists. */
7440 if (elf_section_data (sec)->this_hdr.contents != NULL)
7441 {
7442 *contents = elf_section_data (sec)->this_hdr.contents;
7443 return TRUE;
7444 }
7445
7446 return bfd_malloc_and_get_section (abfd, sec, contents);
7447 }
7448
7449 /* Look through the relocs for a section during the first phase, and
7450 allocate space in the global offset table. */
7451
7452 bfd_boolean
7453 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7454 asection *sec, const Elf_Internal_Rela *relocs)
7455 {
7456 const char *name;
7457 bfd *dynobj;
7458 Elf_Internal_Shdr *symtab_hdr;
7459 struct elf_link_hash_entry **sym_hashes;
7460 size_t extsymoff;
7461 const Elf_Internal_Rela *rel;
7462 const Elf_Internal_Rela *rel_end;
7463 asection *sreloc;
7464 const struct elf_backend_data *bed;
7465 struct mips_elf_link_hash_table *htab;
7466 bfd_byte *contents;
7467 bfd_vma addend;
7468 reloc_howto_type *howto;
7469
7470 if (info->relocatable)
7471 return TRUE;
7472
7473 htab = mips_elf_hash_table (info);
7474 BFD_ASSERT (htab != NULL);
7475
7476 dynobj = elf_hash_table (info)->dynobj;
7477 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7478 sym_hashes = elf_sym_hashes (abfd);
7479 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7480
7481 bed = get_elf_backend_data (abfd);
7482 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7483
7484 /* Check for the mips16 stub sections. */
7485
7486 name = bfd_get_section_name (abfd, sec);
7487 if (FN_STUB_P (name))
7488 {
7489 unsigned long r_symndx;
7490
7491 /* Look at the relocation information to figure out which symbol
7492 this is for. */
7493
7494 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7495 if (r_symndx == 0)
7496 {
7497 (*_bfd_error_handler)
7498 (_("%B: Warning: cannot determine the target function for"
7499 " stub section `%s'"),
7500 abfd, name);
7501 bfd_set_error (bfd_error_bad_value);
7502 return FALSE;
7503 }
7504
7505 if (r_symndx < extsymoff
7506 || sym_hashes[r_symndx - extsymoff] == NULL)
7507 {
7508 asection *o;
7509
7510 /* This stub is for a local symbol. This stub will only be
7511 needed if there is some relocation in this BFD, other
7512 than a 16 bit function call, which refers to this symbol. */
7513 for (o = abfd->sections; o != NULL; o = o->next)
7514 {
7515 Elf_Internal_Rela *sec_relocs;
7516 const Elf_Internal_Rela *r, *rend;
7517
7518 /* We can ignore stub sections when looking for relocs. */
7519 if ((o->flags & SEC_RELOC) == 0
7520 || o->reloc_count == 0
7521 || section_allows_mips16_refs_p (o))
7522 continue;
7523
7524 sec_relocs
7525 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7526 info->keep_memory);
7527 if (sec_relocs == NULL)
7528 return FALSE;
7529
7530 rend = sec_relocs + o->reloc_count;
7531 for (r = sec_relocs; r < rend; r++)
7532 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7533 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7534 break;
7535
7536 if (elf_section_data (o)->relocs != sec_relocs)
7537 free (sec_relocs);
7538
7539 if (r < rend)
7540 break;
7541 }
7542
7543 if (o == NULL)
7544 {
7545 /* There is no non-call reloc for this stub, so we do
7546 not need it. Since this function is called before
7547 the linker maps input sections to output sections, we
7548 can easily discard it by setting the SEC_EXCLUDE
7549 flag. */
7550 sec->flags |= SEC_EXCLUDE;
7551 return TRUE;
7552 }
7553
7554 /* Record this stub in an array of local symbol stubs for
7555 this BFD. */
7556 if (elf_tdata (abfd)->local_stubs == NULL)
7557 {
7558 unsigned long symcount;
7559 asection **n;
7560 bfd_size_type amt;
7561
7562 if (elf_bad_symtab (abfd))
7563 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7564 else
7565 symcount = symtab_hdr->sh_info;
7566 amt = symcount * sizeof (asection *);
7567 n = bfd_zalloc (abfd, amt);
7568 if (n == NULL)
7569 return FALSE;
7570 elf_tdata (abfd)->local_stubs = n;
7571 }
7572
7573 sec->flags |= SEC_KEEP;
7574 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7575
7576 /* We don't need to set mips16_stubs_seen in this case.
7577 That flag is used to see whether we need to look through
7578 the global symbol table for stubs. We don't need to set
7579 it here, because we just have a local stub. */
7580 }
7581 else
7582 {
7583 struct mips_elf_link_hash_entry *h;
7584
7585 h = ((struct mips_elf_link_hash_entry *)
7586 sym_hashes[r_symndx - extsymoff]);
7587
7588 while (h->root.root.type == bfd_link_hash_indirect
7589 || h->root.root.type == bfd_link_hash_warning)
7590 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7591
7592 /* H is the symbol this stub is for. */
7593
7594 /* If we already have an appropriate stub for this function, we
7595 don't need another one, so we can discard this one. Since
7596 this function is called before the linker maps input sections
7597 to output sections, we can easily discard it by setting the
7598 SEC_EXCLUDE flag. */
7599 if (h->fn_stub != NULL)
7600 {
7601 sec->flags |= SEC_EXCLUDE;
7602 return TRUE;
7603 }
7604
7605 sec->flags |= SEC_KEEP;
7606 h->fn_stub = sec;
7607 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7608 }
7609 }
7610 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7611 {
7612 unsigned long r_symndx;
7613 struct mips_elf_link_hash_entry *h;
7614 asection **loc;
7615
7616 /* Look at the relocation information to figure out which symbol
7617 this is for. */
7618
7619 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7620 if (r_symndx == 0)
7621 {
7622 (*_bfd_error_handler)
7623 (_("%B: Warning: cannot determine the target function for"
7624 " stub section `%s'"),
7625 abfd, name);
7626 bfd_set_error (bfd_error_bad_value);
7627 return FALSE;
7628 }
7629
7630 if (r_symndx < extsymoff
7631 || sym_hashes[r_symndx - extsymoff] == NULL)
7632 {
7633 asection *o;
7634
7635 /* This stub is for a local symbol. This stub will only be
7636 needed if there is some relocation (R_MIPS16_26) in this BFD
7637 that refers to this symbol. */
7638 for (o = abfd->sections; o != NULL; o = o->next)
7639 {
7640 Elf_Internal_Rela *sec_relocs;
7641 const Elf_Internal_Rela *r, *rend;
7642
7643 /* We can ignore stub sections when looking for relocs. */
7644 if ((o->flags & SEC_RELOC) == 0
7645 || o->reloc_count == 0
7646 || section_allows_mips16_refs_p (o))
7647 continue;
7648
7649 sec_relocs
7650 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7651 info->keep_memory);
7652 if (sec_relocs == NULL)
7653 return FALSE;
7654
7655 rend = sec_relocs + o->reloc_count;
7656 for (r = sec_relocs; r < rend; r++)
7657 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7658 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7659 break;
7660
7661 if (elf_section_data (o)->relocs != sec_relocs)
7662 free (sec_relocs);
7663
7664 if (r < rend)
7665 break;
7666 }
7667
7668 if (o == NULL)
7669 {
7670 /* There is no non-call reloc for this stub, so we do
7671 not need it. Since this function is called before
7672 the linker maps input sections to output sections, we
7673 can easily discard it by setting the SEC_EXCLUDE
7674 flag. */
7675 sec->flags |= SEC_EXCLUDE;
7676 return TRUE;
7677 }
7678
7679 /* Record this stub in an array of local symbol call_stubs for
7680 this BFD. */
7681 if (elf_tdata (abfd)->local_call_stubs == NULL)
7682 {
7683 unsigned long symcount;
7684 asection **n;
7685 bfd_size_type amt;
7686
7687 if (elf_bad_symtab (abfd))
7688 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7689 else
7690 symcount = symtab_hdr->sh_info;
7691 amt = symcount * sizeof (asection *);
7692 n = bfd_zalloc (abfd, amt);
7693 if (n == NULL)
7694 return FALSE;
7695 elf_tdata (abfd)->local_call_stubs = n;
7696 }
7697
7698 sec->flags |= SEC_KEEP;
7699 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7700
7701 /* We don't need to set mips16_stubs_seen in this case.
7702 That flag is used to see whether we need to look through
7703 the global symbol table for stubs. We don't need to set
7704 it here, because we just have a local stub. */
7705 }
7706 else
7707 {
7708 h = ((struct mips_elf_link_hash_entry *)
7709 sym_hashes[r_symndx - extsymoff]);
7710
7711 /* H is the symbol this stub is for. */
7712
7713 if (CALL_FP_STUB_P (name))
7714 loc = &h->call_fp_stub;
7715 else
7716 loc = &h->call_stub;
7717
7718 /* If we already have an appropriate stub for this function, we
7719 don't need another one, so we can discard this one. Since
7720 this function is called before the linker maps input sections
7721 to output sections, we can easily discard it by setting the
7722 SEC_EXCLUDE flag. */
7723 if (*loc != NULL)
7724 {
7725 sec->flags |= SEC_EXCLUDE;
7726 return TRUE;
7727 }
7728
7729 sec->flags |= SEC_KEEP;
7730 *loc = sec;
7731 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7732 }
7733 }
7734
7735 sreloc = NULL;
7736 contents = NULL;
7737 for (rel = relocs; rel < rel_end; ++rel)
7738 {
7739 unsigned long r_symndx;
7740 unsigned int r_type;
7741 struct elf_link_hash_entry *h;
7742 bfd_boolean can_make_dynamic_p;
7743
7744 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7745 r_type = ELF_R_TYPE (abfd, rel->r_info);
7746
7747 if (r_symndx < extsymoff)
7748 h = NULL;
7749 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7750 {
7751 (*_bfd_error_handler)
7752 (_("%B: Malformed reloc detected for section %s"),
7753 abfd, name);
7754 bfd_set_error (bfd_error_bad_value);
7755 return FALSE;
7756 }
7757 else
7758 {
7759 h = sym_hashes[r_symndx - extsymoff];
7760 while (h != NULL
7761 && (h->root.type == bfd_link_hash_indirect
7762 || h->root.type == bfd_link_hash_warning))
7763 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7764 }
7765
7766 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7767 relocation into a dynamic one. */
7768 can_make_dynamic_p = FALSE;
7769 switch (r_type)
7770 {
7771 case R_MIPS16_GOT16:
7772 case R_MIPS16_CALL16:
7773 case R_MIPS_GOT16:
7774 case R_MIPS_CALL16:
7775 case R_MIPS_CALL_HI16:
7776 case R_MIPS_CALL_LO16:
7777 case R_MIPS_GOT_HI16:
7778 case R_MIPS_GOT_LO16:
7779 case R_MIPS_GOT_PAGE:
7780 case R_MIPS_GOT_OFST:
7781 case R_MIPS_GOT_DISP:
7782 case R_MIPS_TLS_GOTTPREL:
7783 case R_MIPS_TLS_GD:
7784 case R_MIPS_TLS_LDM:
7785 case R_MICROMIPS_GOT16:
7786 case R_MICROMIPS_CALL16:
7787 case R_MICROMIPS_CALL_HI16:
7788 case R_MICROMIPS_CALL_LO16:
7789 case R_MICROMIPS_GOT_HI16:
7790 case R_MICROMIPS_GOT_LO16:
7791 case R_MICROMIPS_GOT_PAGE:
7792 case R_MICROMIPS_GOT_OFST:
7793 case R_MICROMIPS_GOT_DISP:
7794 case R_MICROMIPS_TLS_GOTTPREL:
7795 case R_MICROMIPS_TLS_GD:
7796 case R_MICROMIPS_TLS_LDM:
7797 if (dynobj == NULL)
7798 elf_hash_table (info)->dynobj = dynobj = abfd;
7799 if (!mips_elf_create_got_section (dynobj, info))
7800 return FALSE;
7801 if (htab->is_vxworks && !info->shared)
7802 {
7803 (*_bfd_error_handler)
7804 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7805 abfd, (unsigned long) rel->r_offset);
7806 bfd_set_error (bfd_error_bad_value);
7807 return FALSE;
7808 }
7809 break;
7810
7811 /* This is just a hint; it can safely be ignored. Don't set
7812 has_static_relocs for the corresponding symbol. */
7813 case R_MIPS_JALR:
7814 case R_MICROMIPS_JALR:
7815 break;
7816
7817 case R_MIPS_32:
7818 case R_MIPS_REL32:
7819 case R_MIPS_64:
7820 /* In VxWorks executables, references to external symbols
7821 must be handled using copy relocs or PLT entries; it is not
7822 possible to convert this relocation into a dynamic one.
7823
7824 For executables that use PLTs and copy-relocs, we have a
7825 choice between converting the relocation into a dynamic
7826 one or using copy relocations or PLT entries. It is
7827 usually better to do the former, unless the relocation is
7828 against a read-only section. */
7829 if ((info->shared
7830 || (h != NULL
7831 && !htab->is_vxworks
7832 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7833 && !(!info->nocopyreloc
7834 && !PIC_OBJECT_P (abfd)
7835 && MIPS_ELF_READONLY_SECTION (sec))))
7836 && (sec->flags & SEC_ALLOC) != 0)
7837 {
7838 can_make_dynamic_p = TRUE;
7839 if (dynobj == NULL)
7840 elf_hash_table (info)->dynobj = dynobj = abfd;
7841 break;
7842 }
7843 /* For sections that are not SEC_ALLOC a copy reloc would be
7844 output if possible (implying questionable semantics for
7845 read-only data objects) or otherwise the final link would
7846 fail as ld.so will not process them and could not therefore
7847 handle any outstanding dynamic relocations.
7848
7849 For such sections that are also SEC_DEBUGGING, we can avoid
7850 these problems by simply ignoring any relocs as these
7851 sections have a predefined use and we know it is safe to do
7852 so.
7853
7854 This is needed in cases such as a global symbol definition
7855 in a shared library causing a common symbol from an object
7856 file to be converted to an undefined reference. If that
7857 happens, then all the relocations against this symbol from
7858 SEC_DEBUGGING sections in the object file will resolve to
7859 nil. */
7860 if ((sec->flags & SEC_DEBUGGING) != 0)
7861 break;
7862 /* Fall through. */
7863
7864 default:
7865 /* Most static relocations require pointer equality, except
7866 for branches. */
7867 if (h)
7868 h->pointer_equality_needed = TRUE;
7869 /* Fall through. */
7870
7871 case R_MIPS_26:
7872 case R_MIPS_PC16:
7873 case R_MIPS16_26:
7874 case R_MICROMIPS_26_S1:
7875 case R_MICROMIPS_PC7_S1:
7876 case R_MICROMIPS_PC10_S1:
7877 case R_MICROMIPS_PC16_S1:
7878 case R_MICROMIPS_PC23_S2:
7879 if (h)
7880 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7881 break;
7882 }
7883
7884 if (h)
7885 {
7886 /* Relocations against the special VxWorks __GOTT_BASE__ and
7887 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7888 room for them in .rela.dyn. */
7889 if (is_gott_symbol (info, h))
7890 {
7891 if (sreloc == NULL)
7892 {
7893 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7894 if (sreloc == NULL)
7895 return FALSE;
7896 }
7897 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7898 if (MIPS_ELF_READONLY_SECTION (sec))
7899 /* We tell the dynamic linker that there are
7900 relocations against the text segment. */
7901 info->flags |= DF_TEXTREL;
7902 }
7903 }
7904 else if (call_lo16_reloc_p (r_type)
7905 || got_lo16_reloc_p (r_type)
7906 || got_disp_reloc_p (r_type)
7907 || (got16_reloc_p (r_type) && htab->is_vxworks))
7908 {
7909 /* We may need a local GOT entry for this relocation. We
7910 don't count R_MIPS_GOT_PAGE because we can estimate the
7911 maximum number of pages needed by looking at the size of
7912 the segment. Similar comments apply to R_MIPS*_GOT16 and
7913 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7914 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7915 R_MIPS_CALL_HI16 because these are always followed by an
7916 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7917 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7918 rel->r_addend, info, 0))
7919 return FALSE;
7920 }
7921
7922 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7923 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7924
7925 switch (r_type)
7926 {
7927 case R_MIPS_CALL16:
7928 case R_MIPS16_CALL16:
7929 case R_MICROMIPS_CALL16:
7930 if (h == NULL)
7931 {
7932 (*_bfd_error_handler)
7933 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7934 abfd, (unsigned long) rel->r_offset);
7935 bfd_set_error (bfd_error_bad_value);
7936 return FALSE;
7937 }
7938 /* Fall through. */
7939
7940 case R_MIPS_CALL_HI16:
7941 case R_MIPS_CALL_LO16:
7942 case R_MICROMIPS_CALL_HI16:
7943 case R_MICROMIPS_CALL_LO16:
7944 if (h != NULL)
7945 {
7946 /* Make sure there is room in the regular GOT to hold the
7947 function's address. We may eliminate it in favour of
7948 a .got.plt entry later; see mips_elf_count_got_symbols. */
7949 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE, 0))
7950 return FALSE;
7951
7952 /* We need a stub, not a plt entry for the undefined
7953 function. But we record it as if it needs plt. See
7954 _bfd_elf_adjust_dynamic_symbol. */
7955 h->needs_plt = 1;
7956 h->type = STT_FUNC;
7957 }
7958 break;
7959
7960 case R_MIPS_GOT_PAGE:
7961 case R_MICROMIPS_GOT_PAGE:
7962 /* If this is a global, overridable symbol, GOT_PAGE will
7963 decay to GOT_DISP, so we'll need a GOT entry for it. */
7964 if (h)
7965 {
7966 struct mips_elf_link_hash_entry *hmips =
7967 (struct mips_elf_link_hash_entry *) h;
7968
7969 /* This symbol is definitely not overridable. */
7970 if (hmips->root.def_regular
7971 && ! (info->shared && ! info->symbolic
7972 && ! hmips->root.forced_local))
7973 h = NULL;
7974 }
7975 /* Fall through. */
7976
7977 case R_MIPS16_GOT16:
7978 case R_MIPS_GOT16:
7979 case R_MIPS_GOT_HI16:
7980 case R_MIPS_GOT_LO16:
7981 case R_MICROMIPS_GOT16:
7982 case R_MICROMIPS_GOT_HI16:
7983 case R_MICROMIPS_GOT_LO16:
7984 if (!h || got_page_reloc_p (r_type))
7985 {
7986 /* This relocation needs (or may need, if h != NULL) a
7987 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7988 know for sure until we know whether the symbol is
7989 preemptible. */
7990 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7991 {
7992 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7993 return FALSE;
7994 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7995 addend = mips_elf_read_rel_addend (abfd, rel,
7996 howto, contents);
7997 if (got16_reloc_p (r_type))
7998 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7999 contents, &addend);
8000 else
8001 addend <<= howto->rightshift;
8002 }
8003 else
8004 addend = rel->r_addend;
8005 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
8006 addend))
8007 return FALSE;
8008 }
8009 /* Fall through. */
8010
8011 case R_MIPS_GOT_DISP:
8012 case R_MICROMIPS_GOT_DISP:
8013 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8014 FALSE, 0))
8015 return FALSE;
8016 break;
8017
8018 case R_MIPS_TLS_GOTTPREL:
8019 case R_MICROMIPS_TLS_GOTTPREL:
8020 if (info->shared)
8021 info->flags |= DF_STATIC_TLS;
8022 /* Fall through */
8023
8024 case R_MIPS_TLS_LDM:
8025 case R_MICROMIPS_TLS_LDM:
8026 if (tls_ldm_reloc_p (r_type))
8027 {
8028 r_symndx = STN_UNDEF;
8029 h = NULL;
8030 }
8031 /* Fall through */
8032
8033 case R_MIPS_TLS_GD:
8034 case R_MICROMIPS_TLS_GD:
8035 /* This symbol requires a global offset table entry, or two
8036 for TLS GD relocations. */
8037 {
8038 unsigned char flag;
8039
8040 flag = (tls_gd_reloc_p (r_type)
8041 ? GOT_TLS_GD
8042 : tls_ldm_reloc_p (r_type) ? GOT_TLS_LDM : GOT_TLS_IE);
8043 if (h != NULL)
8044 {
8045 struct mips_elf_link_hash_entry *hmips =
8046 (struct mips_elf_link_hash_entry *) h;
8047 hmips->tls_type |= flag;
8048
8049 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8050 FALSE, flag))
8051 return FALSE;
8052 }
8053 else
8054 {
8055 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != STN_UNDEF);
8056
8057 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8058 rel->r_addend,
8059 info, flag))
8060 return FALSE;
8061 }
8062 }
8063 break;
8064
8065 case R_MIPS_32:
8066 case R_MIPS_REL32:
8067 case R_MIPS_64:
8068 /* In VxWorks executables, references to external symbols
8069 are handled using copy relocs or PLT stubs, so there's
8070 no need to add a .rela.dyn entry for this relocation. */
8071 if (can_make_dynamic_p)
8072 {
8073 if (sreloc == NULL)
8074 {
8075 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8076 if (sreloc == NULL)
8077 return FALSE;
8078 }
8079 if (info->shared && h == NULL)
8080 {
8081 /* When creating a shared object, we must copy these
8082 reloc types into the output file as R_MIPS_REL32
8083 relocs. Make room for this reloc in .rel(a).dyn. */
8084 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8085 if (MIPS_ELF_READONLY_SECTION (sec))
8086 /* We tell the dynamic linker that there are
8087 relocations against the text segment. */
8088 info->flags |= DF_TEXTREL;
8089 }
8090 else
8091 {
8092 struct mips_elf_link_hash_entry *hmips;
8093
8094 /* For a shared object, we must copy this relocation
8095 unless the symbol turns out to be undefined and
8096 weak with non-default visibility, in which case
8097 it will be left as zero.
8098
8099 We could elide R_MIPS_REL32 for locally binding symbols
8100 in shared libraries, but do not yet do so.
8101
8102 For an executable, we only need to copy this
8103 reloc if the symbol is defined in a dynamic
8104 object. */
8105 hmips = (struct mips_elf_link_hash_entry *) h;
8106 ++hmips->possibly_dynamic_relocs;
8107 if (MIPS_ELF_READONLY_SECTION (sec))
8108 /* We need it to tell the dynamic linker if there
8109 are relocations against the text segment. */
8110 hmips->readonly_reloc = TRUE;
8111 }
8112 }
8113
8114 if (SGI_COMPAT (abfd))
8115 mips_elf_hash_table (info)->compact_rel_size +=
8116 sizeof (Elf32_External_crinfo);
8117 break;
8118
8119 case R_MIPS_26:
8120 case R_MIPS_GPREL16:
8121 case R_MIPS_LITERAL:
8122 case R_MIPS_GPREL32:
8123 case R_MICROMIPS_26_S1:
8124 case R_MICROMIPS_GPREL16:
8125 case R_MICROMIPS_LITERAL:
8126 case R_MICROMIPS_GPREL7_S2:
8127 if (SGI_COMPAT (abfd))
8128 mips_elf_hash_table (info)->compact_rel_size +=
8129 sizeof (Elf32_External_crinfo);
8130 break;
8131
8132 /* This relocation describes the C++ object vtable hierarchy.
8133 Reconstruct it for later use during GC. */
8134 case R_MIPS_GNU_VTINHERIT:
8135 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8136 return FALSE;
8137 break;
8138
8139 /* This relocation describes which C++ vtable entries are actually
8140 used. Record for later use during GC. */
8141 case R_MIPS_GNU_VTENTRY:
8142 BFD_ASSERT (h != NULL);
8143 if (h != NULL
8144 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8145 return FALSE;
8146 break;
8147
8148 default:
8149 break;
8150 }
8151
8152 /* We must not create a stub for a symbol that has relocations
8153 related to taking the function's address. This doesn't apply to
8154 VxWorks, where CALL relocs refer to a .got.plt entry instead of
8155 a normal .got entry. */
8156 if (!htab->is_vxworks && h != NULL)
8157 switch (r_type)
8158 {
8159 default:
8160 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8161 break;
8162 case R_MIPS16_CALL16:
8163 case R_MIPS_CALL16:
8164 case R_MIPS_CALL_HI16:
8165 case R_MIPS_CALL_LO16:
8166 case R_MIPS_JALR:
8167 case R_MICROMIPS_CALL16:
8168 case R_MICROMIPS_CALL_HI16:
8169 case R_MICROMIPS_CALL_LO16:
8170 case R_MICROMIPS_JALR:
8171 break;
8172 }
8173
8174 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8175 if there is one. We only need to handle global symbols here;
8176 we decide whether to keep or delete stubs for local symbols
8177 when processing the stub's relocations. */
8178 if (h != NULL
8179 && !mips16_call_reloc_p (r_type)
8180 && !section_allows_mips16_refs_p (sec))
8181 {
8182 struct mips_elf_link_hash_entry *mh;
8183
8184 mh = (struct mips_elf_link_hash_entry *) h;
8185 mh->need_fn_stub = TRUE;
8186 }
8187
8188 /* Refuse some position-dependent relocations when creating a
8189 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8190 not PIC, but we can create dynamic relocations and the result
8191 will be fine. Also do not refuse R_MIPS_LO16, which can be
8192 combined with R_MIPS_GOT16. */
8193 if (info->shared)
8194 {
8195 switch (r_type)
8196 {
8197 case R_MIPS16_HI16:
8198 case R_MIPS_HI16:
8199 case R_MIPS_HIGHER:
8200 case R_MIPS_HIGHEST:
8201 case R_MICROMIPS_HI16:
8202 case R_MICROMIPS_HIGHER:
8203 case R_MICROMIPS_HIGHEST:
8204 /* Don't refuse a high part relocation if it's against
8205 no symbol (e.g. part of a compound relocation). */
8206 if (r_symndx == STN_UNDEF)
8207 break;
8208
8209 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8210 and has a special meaning. */
8211 if (!NEWABI_P (abfd) && h != NULL
8212 && strcmp (h->root.root.string, "_gp_disp") == 0)
8213 break;
8214
8215 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8216 if (is_gott_symbol (info, h))
8217 break;
8218
8219 /* FALLTHROUGH */
8220
8221 case R_MIPS16_26:
8222 case R_MIPS_26:
8223 case R_MICROMIPS_26_S1:
8224 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8225 (*_bfd_error_handler)
8226 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8227 abfd, howto->name,
8228 (h) ? h->root.root.string : "a local symbol");
8229 bfd_set_error (bfd_error_bad_value);
8230 return FALSE;
8231 default:
8232 break;
8233 }
8234 }
8235 }
8236
8237 return TRUE;
8238 }
8239 \f
8240 bfd_boolean
8241 _bfd_mips_relax_section (bfd *abfd, asection *sec,
8242 struct bfd_link_info *link_info,
8243 bfd_boolean *again)
8244 {
8245 Elf_Internal_Rela *internal_relocs;
8246 Elf_Internal_Rela *irel, *irelend;
8247 Elf_Internal_Shdr *symtab_hdr;
8248 bfd_byte *contents = NULL;
8249 size_t extsymoff;
8250 bfd_boolean changed_contents = FALSE;
8251 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8252 Elf_Internal_Sym *isymbuf = NULL;
8253
8254 /* We are not currently changing any sizes, so only one pass. */
8255 *again = FALSE;
8256
8257 if (link_info->relocatable)
8258 return TRUE;
8259
8260 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8261 link_info->keep_memory);
8262 if (internal_relocs == NULL)
8263 return TRUE;
8264
8265 irelend = internal_relocs + sec->reloc_count
8266 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8267 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8268 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8269
8270 for (irel = internal_relocs; irel < irelend; irel++)
8271 {
8272 bfd_vma symval;
8273 bfd_signed_vma sym_offset;
8274 unsigned int r_type;
8275 unsigned long r_symndx;
8276 asection *sym_sec;
8277 unsigned long instruction;
8278
8279 /* Turn jalr into bgezal, and jr into beq, if they're marked
8280 with a JALR relocation, that indicate where they jump to.
8281 This saves some pipeline bubbles. */
8282 r_type = ELF_R_TYPE (abfd, irel->r_info);
8283 if (r_type != R_MIPS_JALR)
8284 continue;
8285
8286 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8287 /* Compute the address of the jump target. */
8288 if (r_symndx >= extsymoff)
8289 {
8290 struct mips_elf_link_hash_entry *h
8291 = ((struct mips_elf_link_hash_entry *)
8292 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8293
8294 while (h->root.root.type == bfd_link_hash_indirect
8295 || h->root.root.type == bfd_link_hash_warning)
8296 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8297
8298 /* If a symbol is undefined, or if it may be overridden,
8299 skip it. */
8300 if (! ((h->root.root.type == bfd_link_hash_defined
8301 || h->root.root.type == bfd_link_hash_defweak)
8302 && h->root.root.u.def.section)
8303 || (link_info->shared && ! link_info->symbolic
8304 && !h->root.forced_local))
8305 continue;
8306
8307 sym_sec = h->root.root.u.def.section;
8308 if (sym_sec->output_section)
8309 symval = (h->root.root.u.def.value
8310 + sym_sec->output_section->vma
8311 + sym_sec->output_offset);
8312 else
8313 symval = h->root.root.u.def.value;
8314 }
8315 else
8316 {
8317 Elf_Internal_Sym *isym;
8318
8319 /* Read this BFD's symbols if we haven't done so already. */
8320 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8321 {
8322 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8323 if (isymbuf == NULL)
8324 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8325 symtab_hdr->sh_info, 0,
8326 NULL, NULL, NULL);
8327 if (isymbuf == NULL)
8328 goto relax_return;
8329 }
8330
8331 isym = isymbuf + r_symndx;
8332 if (isym->st_shndx == SHN_UNDEF)
8333 continue;
8334 else if (isym->st_shndx == SHN_ABS)
8335 sym_sec = bfd_abs_section_ptr;
8336 else if (isym->st_shndx == SHN_COMMON)
8337 sym_sec = bfd_com_section_ptr;
8338 else
8339 sym_sec
8340 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8341 symval = isym->st_value
8342 + sym_sec->output_section->vma
8343 + sym_sec->output_offset;
8344 }
8345
8346 /* Compute branch offset, from delay slot of the jump to the
8347 branch target. */
8348 sym_offset = (symval + irel->r_addend)
8349 - (sec_start + irel->r_offset + 4);
8350
8351 /* Branch offset must be properly aligned. */
8352 if ((sym_offset & 3) != 0)
8353 continue;
8354
8355 sym_offset >>= 2;
8356
8357 /* Check that it's in range. */
8358 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8359 continue;
8360
8361 /* Get the section contents if we haven't done so already. */
8362 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8363 goto relax_return;
8364
8365 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8366
8367 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8368 if ((instruction & 0xfc1fffff) == 0x0000f809)
8369 instruction = 0x04110000;
8370 /* If it was jr <reg>, turn it into b <target>. */
8371 else if ((instruction & 0xfc1fffff) == 0x00000008)
8372 instruction = 0x10000000;
8373 else
8374 continue;
8375
8376 instruction |= (sym_offset & 0xffff);
8377 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8378 changed_contents = TRUE;
8379 }
8380
8381 if (contents != NULL
8382 && elf_section_data (sec)->this_hdr.contents != contents)
8383 {
8384 if (!changed_contents && !link_info->keep_memory)
8385 free (contents);
8386 else
8387 {
8388 /* Cache the section contents for elf_link_input_bfd. */
8389 elf_section_data (sec)->this_hdr.contents = contents;
8390 }
8391 }
8392 return TRUE;
8393
8394 relax_return:
8395 if (contents != NULL
8396 && elf_section_data (sec)->this_hdr.contents != contents)
8397 free (contents);
8398 return FALSE;
8399 }
8400 \f
8401 /* Allocate space for global sym dynamic relocs. */
8402
8403 static bfd_boolean
8404 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8405 {
8406 struct bfd_link_info *info = inf;
8407 bfd *dynobj;
8408 struct mips_elf_link_hash_entry *hmips;
8409 struct mips_elf_link_hash_table *htab;
8410
8411 htab = mips_elf_hash_table (info);
8412 BFD_ASSERT (htab != NULL);
8413
8414 dynobj = elf_hash_table (info)->dynobj;
8415 hmips = (struct mips_elf_link_hash_entry *) h;
8416
8417 /* VxWorks executables are handled elsewhere; we only need to
8418 allocate relocations in shared objects. */
8419 if (htab->is_vxworks && !info->shared)
8420 return TRUE;
8421
8422 /* Ignore indirect symbols. All relocations against such symbols
8423 will be redirected to the target symbol. */
8424 if (h->root.type == bfd_link_hash_indirect)
8425 return TRUE;
8426
8427 /* If this symbol is defined in a dynamic object, or we are creating
8428 a shared library, we will need to copy any R_MIPS_32 or
8429 R_MIPS_REL32 relocs against it into the output file. */
8430 if (! info->relocatable
8431 && hmips->possibly_dynamic_relocs != 0
8432 && (h->root.type == bfd_link_hash_defweak
8433 || !h->def_regular
8434 || info->shared))
8435 {
8436 bfd_boolean do_copy = TRUE;
8437
8438 if (h->root.type == bfd_link_hash_undefweak)
8439 {
8440 /* Do not copy relocations for undefined weak symbols with
8441 non-default visibility. */
8442 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8443 do_copy = FALSE;
8444
8445 /* Make sure undefined weak symbols are output as a dynamic
8446 symbol in PIEs. */
8447 else if (h->dynindx == -1 && !h->forced_local)
8448 {
8449 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8450 return FALSE;
8451 }
8452 }
8453
8454 if (do_copy)
8455 {
8456 /* Even though we don't directly need a GOT entry for this symbol,
8457 the SVR4 psABI requires it to have a dynamic symbol table
8458 index greater that DT_MIPS_GOTSYM if there are dynamic
8459 relocations against it.
8460
8461 VxWorks does not enforce the same mapping between the GOT
8462 and the symbol table, so the same requirement does not
8463 apply there. */
8464 if (!htab->is_vxworks)
8465 {
8466 if (hmips->global_got_area > GGA_RELOC_ONLY)
8467 hmips->global_got_area = GGA_RELOC_ONLY;
8468 hmips->got_only_for_calls = FALSE;
8469 }
8470
8471 mips_elf_allocate_dynamic_relocations
8472 (dynobj, info, hmips->possibly_dynamic_relocs);
8473 if (hmips->readonly_reloc)
8474 /* We tell the dynamic linker that there are relocations
8475 against the text segment. */
8476 info->flags |= DF_TEXTREL;
8477 }
8478 }
8479
8480 return TRUE;
8481 }
8482
8483 /* Adjust a symbol defined by a dynamic object and referenced by a
8484 regular object. The current definition is in some section of the
8485 dynamic object, but we're not including those sections. We have to
8486 change the definition to something the rest of the link can
8487 understand. */
8488
8489 bfd_boolean
8490 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8491 struct elf_link_hash_entry *h)
8492 {
8493 bfd *dynobj;
8494 struct mips_elf_link_hash_entry *hmips;
8495 struct mips_elf_link_hash_table *htab;
8496
8497 htab = mips_elf_hash_table (info);
8498 BFD_ASSERT (htab != NULL);
8499
8500 dynobj = elf_hash_table (info)->dynobj;
8501 hmips = (struct mips_elf_link_hash_entry *) h;
8502
8503 /* Make sure we know what is going on here. */
8504 BFD_ASSERT (dynobj != NULL
8505 && (h->needs_plt
8506 || h->u.weakdef != NULL
8507 || (h->def_dynamic
8508 && h->ref_regular
8509 && !h->def_regular)));
8510
8511 hmips = (struct mips_elf_link_hash_entry *) h;
8512
8513 /* If there are call relocations against an externally-defined symbol,
8514 see whether we can create a MIPS lazy-binding stub for it. We can
8515 only do this if all references to the function are through call
8516 relocations, and in that case, the traditional lazy-binding stubs
8517 are much more efficient than PLT entries.
8518
8519 Traditional stubs are only available on SVR4 psABI-based systems;
8520 VxWorks always uses PLTs instead. */
8521 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8522 {
8523 if (! elf_hash_table (info)->dynamic_sections_created)
8524 return TRUE;
8525
8526 /* If this symbol is not defined in a regular file, then set
8527 the symbol to the stub location. This is required to make
8528 function pointers compare as equal between the normal
8529 executable and the shared library. */
8530 if (!h->def_regular)
8531 {
8532 hmips->needs_lazy_stub = TRUE;
8533 htab->lazy_stub_count++;
8534 return TRUE;
8535 }
8536 }
8537 /* As above, VxWorks requires PLT entries for externally-defined
8538 functions that are only accessed through call relocations.
8539
8540 Both VxWorks and non-VxWorks targets also need PLT entries if there
8541 are static-only relocations against an externally-defined function.
8542 This can technically occur for shared libraries if there are
8543 branches to the symbol, although it is unlikely that this will be
8544 used in practice due to the short ranges involved. It can occur
8545 for any relative or absolute relocation in executables; in that
8546 case, the PLT entry becomes the function's canonical address. */
8547 else if (((h->needs_plt && !hmips->no_fn_stub)
8548 || (h->type == STT_FUNC && hmips->has_static_relocs))
8549 && htab->use_plts_and_copy_relocs
8550 && !SYMBOL_CALLS_LOCAL (info, h)
8551 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8552 && h->root.type == bfd_link_hash_undefweak))
8553 {
8554 /* If this is the first symbol to need a PLT entry, allocate room
8555 for the header. */
8556 if (htab->splt->size == 0)
8557 {
8558 BFD_ASSERT (htab->sgotplt->size == 0);
8559
8560 /* If we're using the PLT additions to the psABI, each PLT
8561 entry is 16 bytes and the PLT0 entry is 32 bytes.
8562 Encourage better cache usage by aligning. We do this
8563 lazily to avoid pessimizing traditional objects. */
8564 if (!htab->is_vxworks
8565 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8566 return FALSE;
8567
8568 /* Make sure that .got.plt is word-aligned. We do this lazily
8569 for the same reason as above. */
8570 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8571 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8572 return FALSE;
8573
8574 htab->splt->size += htab->plt_header_size;
8575
8576 /* On non-VxWorks targets, the first two entries in .got.plt
8577 are reserved. */
8578 if (!htab->is_vxworks)
8579 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
8580
8581 /* On VxWorks, also allocate room for the header's
8582 .rela.plt.unloaded entries. */
8583 if (htab->is_vxworks && !info->shared)
8584 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8585 }
8586
8587 /* Assign the next .plt entry to this symbol. */
8588 h->plt.offset = htab->splt->size;
8589 htab->splt->size += htab->plt_entry_size;
8590
8591 /* If the output file has no definition of the symbol, set the
8592 symbol's value to the address of the stub. */
8593 if (!info->shared && !h->def_regular)
8594 {
8595 h->root.u.def.section = htab->splt;
8596 h->root.u.def.value = h->plt.offset;
8597 /* For VxWorks, point at the PLT load stub rather than the
8598 lazy resolution stub; this stub will become the canonical
8599 function address. */
8600 if (htab->is_vxworks)
8601 h->root.u.def.value += 8;
8602 }
8603
8604 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8605 relocation. */
8606 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8607 htab->srelplt->size += (htab->is_vxworks
8608 ? MIPS_ELF_RELA_SIZE (dynobj)
8609 : MIPS_ELF_REL_SIZE (dynobj));
8610
8611 /* Make room for the .rela.plt.unloaded relocations. */
8612 if (htab->is_vxworks && !info->shared)
8613 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8614
8615 /* All relocations against this symbol that could have been made
8616 dynamic will now refer to the PLT entry instead. */
8617 hmips->possibly_dynamic_relocs = 0;
8618
8619 return TRUE;
8620 }
8621
8622 /* If this is a weak symbol, and there is a real definition, the
8623 processor independent code will have arranged for us to see the
8624 real definition first, and we can just use the same value. */
8625 if (h->u.weakdef != NULL)
8626 {
8627 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8628 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8629 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8630 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8631 return TRUE;
8632 }
8633
8634 /* Otherwise, there is nothing further to do for symbols defined
8635 in regular objects. */
8636 if (h->def_regular)
8637 return TRUE;
8638
8639 /* There's also nothing more to do if we'll convert all relocations
8640 against this symbol into dynamic relocations. */
8641 if (!hmips->has_static_relocs)
8642 return TRUE;
8643
8644 /* We're now relying on copy relocations. Complain if we have
8645 some that we can't convert. */
8646 if (!htab->use_plts_and_copy_relocs || info->shared)
8647 {
8648 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8649 "dynamic symbol %s"),
8650 h->root.root.string);
8651 bfd_set_error (bfd_error_bad_value);
8652 return FALSE;
8653 }
8654
8655 /* We must allocate the symbol in our .dynbss section, which will
8656 become part of the .bss section of the executable. There will be
8657 an entry for this symbol in the .dynsym section. The dynamic
8658 object will contain position independent code, so all references
8659 from the dynamic object to this symbol will go through the global
8660 offset table. The dynamic linker will use the .dynsym entry to
8661 determine the address it must put in the global offset table, so
8662 both the dynamic object and the regular object will refer to the
8663 same memory location for the variable. */
8664
8665 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8666 {
8667 if (htab->is_vxworks)
8668 htab->srelbss->size += sizeof (Elf32_External_Rela);
8669 else
8670 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8671 h->needs_copy = 1;
8672 }
8673
8674 /* All relocations against this symbol that could have been made
8675 dynamic will now refer to the local copy instead. */
8676 hmips->possibly_dynamic_relocs = 0;
8677
8678 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
8679 }
8680 \f
8681 /* This function is called after all the input files have been read,
8682 and the input sections have been assigned to output sections. We
8683 check for any mips16 stub sections that we can discard. */
8684
8685 bfd_boolean
8686 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
8687 struct bfd_link_info *info)
8688 {
8689 asection *ri;
8690 struct mips_elf_link_hash_table *htab;
8691 struct mips_htab_traverse_info hti;
8692
8693 htab = mips_elf_hash_table (info);
8694 BFD_ASSERT (htab != NULL);
8695
8696 /* The .reginfo section has a fixed size. */
8697 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8698 if (ri != NULL)
8699 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
8700
8701 hti.info = info;
8702 hti.output_bfd = output_bfd;
8703 hti.error = FALSE;
8704 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8705 mips_elf_check_symbols, &hti);
8706 if (hti.error)
8707 return FALSE;
8708
8709 return TRUE;
8710 }
8711
8712 /* If the link uses a GOT, lay it out and work out its size. */
8713
8714 static bfd_boolean
8715 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8716 {
8717 bfd *dynobj;
8718 asection *s;
8719 struct mips_got_info *g;
8720 bfd_size_type loadable_size = 0;
8721 bfd_size_type page_gotno;
8722 bfd *sub;
8723 struct mips_elf_count_tls_arg count_tls_arg;
8724 struct mips_elf_link_hash_table *htab;
8725
8726 htab = mips_elf_hash_table (info);
8727 BFD_ASSERT (htab != NULL);
8728
8729 s = htab->sgot;
8730 if (s == NULL)
8731 return TRUE;
8732
8733 dynobj = elf_hash_table (info)->dynobj;
8734 g = htab->got_info;
8735
8736 /* Allocate room for the reserved entries. VxWorks always reserves
8737 3 entries; other objects only reserve 2 entries. */
8738 BFD_ASSERT (g->assigned_gotno == 0);
8739 if (htab->is_vxworks)
8740 htab->reserved_gotno = 3;
8741 else
8742 htab->reserved_gotno = 2;
8743 g->local_gotno += htab->reserved_gotno;
8744 g->assigned_gotno = htab->reserved_gotno;
8745
8746 /* Replace entries for indirect and warning symbols with entries for
8747 the target symbol. */
8748 if (!mips_elf_resolve_final_got_entries (g))
8749 return FALSE;
8750
8751 /* Count the number of GOT symbols. */
8752 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
8753
8754 /* Calculate the total loadable size of the output. That
8755 will give us the maximum number of GOT_PAGE entries
8756 required. */
8757 for (sub = info->input_bfds; sub; sub = sub->link_next)
8758 {
8759 asection *subsection;
8760
8761 for (subsection = sub->sections;
8762 subsection;
8763 subsection = subsection->next)
8764 {
8765 if ((subsection->flags & SEC_ALLOC) == 0)
8766 continue;
8767 loadable_size += ((subsection->size + 0xf)
8768 &~ (bfd_size_type) 0xf);
8769 }
8770 }
8771
8772 if (htab->is_vxworks)
8773 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
8774 relocations against local symbols evaluate to "G", and the EABI does
8775 not include R_MIPS_GOT_PAGE. */
8776 page_gotno = 0;
8777 else
8778 /* Assume there are two loadable segments consisting of contiguous
8779 sections. Is 5 enough? */
8780 page_gotno = (loadable_size >> 16) + 5;
8781
8782 /* Choose the smaller of the two estimates; both are intended to be
8783 conservative. */
8784 if (page_gotno > g->page_gotno)
8785 page_gotno = g->page_gotno;
8786
8787 g->local_gotno += page_gotno;
8788 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8789 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8790
8791 /* We need to calculate tls_gotno for global symbols at this point
8792 instead of building it up earlier, to avoid doublecounting
8793 entries for one global symbol from multiple input files. */
8794 count_tls_arg.info = info;
8795 count_tls_arg.needed = 0;
8796 elf_link_hash_traverse (elf_hash_table (info),
8797 mips_elf_count_global_tls_entries,
8798 &count_tls_arg);
8799 g->tls_gotno += count_tls_arg.needed;
8800 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8801
8802 /* VxWorks does not support multiple GOTs. It initializes $gp to
8803 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8804 dynamic loader. */
8805 if (htab->is_vxworks)
8806 {
8807 /* VxWorks executables do not need a GOT. */
8808 if (info->shared)
8809 {
8810 /* Each VxWorks GOT entry needs an explicit relocation. */
8811 unsigned int count;
8812
8813 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
8814 if (count)
8815 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8816 }
8817 }
8818 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
8819 {
8820 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
8821 return FALSE;
8822 }
8823 else
8824 {
8825 struct mips_elf_count_tls_arg arg;
8826
8827 /* Set up TLS entries. */
8828 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8829 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
8830
8831 /* Allocate room for the TLS relocations. */
8832 arg.info = info;
8833 arg.needed = 0;
8834 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8835 elf_link_hash_traverse (elf_hash_table (info),
8836 mips_elf_count_global_tls_relocs,
8837 &arg);
8838 if (arg.needed)
8839 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
8840 }
8841
8842 return TRUE;
8843 }
8844
8845 /* Estimate the size of the .MIPS.stubs section. */
8846
8847 static void
8848 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8849 {
8850 struct mips_elf_link_hash_table *htab;
8851 bfd_size_type dynsymcount;
8852
8853 htab = mips_elf_hash_table (info);
8854 BFD_ASSERT (htab != NULL);
8855
8856 if (htab->lazy_stub_count == 0)
8857 return;
8858
8859 /* IRIX rld assumes that a function stub isn't at the end of the .text
8860 section, so add a dummy entry to the end. */
8861 htab->lazy_stub_count++;
8862
8863 /* Get a worst-case estimate of the number of dynamic symbols needed.
8864 At this point, dynsymcount does not account for section symbols
8865 and count_section_dynsyms may overestimate the number that will
8866 be needed. */
8867 dynsymcount = (elf_hash_table (info)->dynsymcount
8868 + count_section_dynsyms (output_bfd, info));
8869
8870 /* Determine the size of one stub entry. */
8871 htab->function_stub_size = (dynsymcount > 0x10000
8872 ? MIPS_FUNCTION_STUB_BIG_SIZE
8873 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8874
8875 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8876 }
8877
8878 /* A mips_elf_link_hash_traverse callback for which DATA points to the
8879 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8880 allocate an entry in the stubs section. */
8881
8882 static bfd_boolean
8883 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8884 {
8885 struct mips_elf_link_hash_table *htab;
8886
8887 htab = (struct mips_elf_link_hash_table *) data;
8888 if (h->needs_lazy_stub)
8889 {
8890 h->root.root.u.def.section = htab->sstubs;
8891 h->root.root.u.def.value = htab->sstubs->size;
8892 h->root.plt.offset = htab->sstubs->size;
8893 htab->sstubs->size += htab->function_stub_size;
8894 }
8895 return TRUE;
8896 }
8897
8898 /* Allocate offsets in the stubs section to each symbol that needs one.
8899 Set the final size of the .MIPS.stub section. */
8900
8901 static void
8902 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8903 {
8904 struct mips_elf_link_hash_table *htab;
8905
8906 htab = mips_elf_hash_table (info);
8907 BFD_ASSERT (htab != NULL);
8908
8909 if (htab->lazy_stub_count == 0)
8910 return;
8911
8912 htab->sstubs->size = 0;
8913 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
8914 htab->sstubs->size += htab->function_stub_size;
8915 BFD_ASSERT (htab->sstubs->size
8916 == htab->lazy_stub_count * htab->function_stub_size);
8917 }
8918
8919 /* Set the sizes of the dynamic sections. */
8920
8921 bfd_boolean
8922 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8923 struct bfd_link_info *info)
8924 {
8925 bfd *dynobj;
8926 asection *s, *sreldyn;
8927 bfd_boolean reltext;
8928 struct mips_elf_link_hash_table *htab;
8929
8930 htab = mips_elf_hash_table (info);
8931 BFD_ASSERT (htab != NULL);
8932 dynobj = elf_hash_table (info)->dynobj;
8933 BFD_ASSERT (dynobj != NULL);
8934
8935 if (elf_hash_table (info)->dynamic_sections_created)
8936 {
8937 /* Set the contents of the .interp section to the interpreter. */
8938 if (info->executable)
8939 {
8940 s = bfd_get_section_by_name (dynobj, ".interp");
8941 BFD_ASSERT (s != NULL);
8942 s->size
8943 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8944 s->contents
8945 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8946 }
8947
8948 /* Create a symbol for the PLT, if we know that we are using it. */
8949 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8950 {
8951 struct elf_link_hash_entry *h;
8952
8953 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8954
8955 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8956 "_PROCEDURE_LINKAGE_TABLE_");
8957 htab->root.hplt = h;
8958 if (h == NULL)
8959 return FALSE;
8960 h->type = STT_FUNC;
8961 }
8962 }
8963
8964 /* Allocate space for global sym dynamic relocs. */
8965 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8966
8967 mips_elf_estimate_stub_size (output_bfd, info);
8968
8969 if (!mips_elf_lay_out_got (output_bfd, info))
8970 return FALSE;
8971
8972 mips_elf_lay_out_lazy_stubs (info);
8973
8974 /* The check_relocs and adjust_dynamic_symbol entry points have
8975 determined the sizes of the various dynamic sections. Allocate
8976 memory for them. */
8977 reltext = FALSE;
8978 for (s = dynobj->sections; s != NULL; s = s->next)
8979 {
8980 const char *name;
8981
8982 /* It's OK to base decisions on the section name, because none
8983 of the dynobj section names depend upon the input files. */
8984 name = bfd_get_section_name (dynobj, s);
8985
8986 if ((s->flags & SEC_LINKER_CREATED) == 0)
8987 continue;
8988
8989 if (CONST_STRNEQ (name, ".rel"))
8990 {
8991 if (s->size != 0)
8992 {
8993 const char *outname;
8994 asection *target;
8995
8996 /* If this relocation section applies to a read only
8997 section, then we probably need a DT_TEXTREL entry.
8998 If the relocation section is .rel(a).dyn, we always
8999 assert a DT_TEXTREL entry rather than testing whether
9000 there exists a relocation to a read only section or
9001 not. */
9002 outname = bfd_get_section_name (output_bfd,
9003 s->output_section);
9004 target = bfd_get_section_by_name (output_bfd, outname + 4);
9005 if ((target != NULL
9006 && (target->flags & SEC_READONLY) != 0
9007 && (target->flags & SEC_ALLOC) != 0)
9008 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9009 reltext = TRUE;
9010
9011 /* We use the reloc_count field as a counter if we need
9012 to copy relocs into the output file. */
9013 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9014 s->reloc_count = 0;
9015
9016 /* If combreloc is enabled, elf_link_sort_relocs() will
9017 sort relocations, but in a different way than we do,
9018 and before we're done creating relocations. Also, it
9019 will move them around between input sections'
9020 relocation's contents, so our sorting would be
9021 broken, so don't let it run. */
9022 info->combreloc = 0;
9023 }
9024 }
9025 else if (! info->shared
9026 && ! mips_elf_hash_table (info)->use_rld_obj_head
9027 && CONST_STRNEQ (name, ".rld_map"))
9028 {
9029 /* We add a room for __rld_map. It will be filled in by the
9030 rtld to contain a pointer to the _r_debug structure. */
9031 s->size += 4;
9032 }
9033 else if (SGI_COMPAT (output_bfd)
9034 && CONST_STRNEQ (name, ".compact_rel"))
9035 s->size += mips_elf_hash_table (info)->compact_rel_size;
9036 else if (s == htab->splt)
9037 {
9038 /* If the last PLT entry has a branch delay slot, allocate
9039 room for an extra nop to fill the delay slot. This is
9040 for CPUs without load interlocking. */
9041 if (! LOAD_INTERLOCKS_P (output_bfd)
9042 && ! htab->is_vxworks && s->size > 0)
9043 s->size += 4;
9044 }
9045 else if (! CONST_STRNEQ (name, ".init")
9046 && s != htab->sgot
9047 && s != htab->sgotplt
9048 && s != htab->sstubs
9049 && s != htab->sdynbss)
9050 {
9051 /* It's not one of our sections, so don't allocate space. */
9052 continue;
9053 }
9054
9055 if (s->size == 0)
9056 {
9057 s->flags |= SEC_EXCLUDE;
9058 continue;
9059 }
9060
9061 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9062 continue;
9063
9064 /* Allocate memory for the section contents. */
9065 s->contents = bfd_zalloc (dynobj, s->size);
9066 if (s->contents == NULL)
9067 {
9068 bfd_set_error (bfd_error_no_memory);
9069 return FALSE;
9070 }
9071 }
9072
9073 if (elf_hash_table (info)->dynamic_sections_created)
9074 {
9075 /* Add some entries to the .dynamic section. We fill in the
9076 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9077 must add the entries now so that we get the correct size for
9078 the .dynamic section. */
9079
9080 /* SGI object has the equivalence of DT_DEBUG in the
9081 DT_MIPS_RLD_MAP entry. This must come first because glibc
9082 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
9083 looks at the first one it sees. */
9084 if (!info->shared
9085 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9086 return FALSE;
9087
9088 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9089 used by the debugger. */
9090 if (info->executable
9091 && !SGI_COMPAT (output_bfd)
9092 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9093 return FALSE;
9094
9095 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
9096 info->flags |= DF_TEXTREL;
9097
9098 if ((info->flags & DF_TEXTREL) != 0)
9099 {
9100 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
9101 return FALSE;
9102
9103 /* Clear the DF_TEXTREL flag. It will be set again if we
9104 write out an actual text relocation; we may not, because
9105 at this point we do not know whether e.g. any .eh_frame
9106 absolute relocations have been converted to PC-relative. */
9107 info->flags &= ~DF_TEXTREL;
9108 }
9109
9110 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
9111 return FALSE;
9112
9113 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
9114 if (htab->is_vxworks)
9115 {
9116 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9117 use any of the DT_MIPS_* tags. */
9118 if (sreldyn && sreldyn->size > 0)
9119 {
9120 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9121 return FALSE;
9122
9123 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9124 return FALSE;
9125
9126 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9127 return FALSE;
9128 }
9129 }
9130 else
9131 {
9132 if (sreldyn && sreldyn->size > 0)
9133 {
9134 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9135 return FALSE;
9136
9137 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9138 return FALSE;
9139
9140 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9141 return FALSE;
9142 }
9143
9144 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9145 return FALSE;
9146
9147 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9148 return FALSE;
9149
9150 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9151 return FALSE;
9152
9153 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9154 return FALSE;
9155
9156 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9157 return FALSE;
9158
9159 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9160 return FALSE;
9161
9162 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9163 return FALSE;
9164
9165 if (IRIX_COMPAT (dynobj) == ict_irix5
9166 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9167 return FALSE;
9168
9169 if (IRIX_COMPAT (dynobj) == ict_irix6
9170 && (bfd_get_section_by_name
9171 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9172 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9173 return FALSE;
9174 }
9175 if (htab->splt->size > 0)
9176 {
9177 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9178 return FALSE;
9179
9180 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9181 return FALSE;
9182
9183 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9184 return FALSE;
9185
9186 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9187 return FALSE;
9188 }
9189 if (htab->is_vxworks
9190 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9191 return FALSE;
9192 }
9193
9194 return TRUE;
9195 }
9196 \f
9197 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9198 Adjust its R_ADDEND field so that it is correct for the output file.
9199 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9200 and sections respectively; both use symbol indexes. */
9201
9202 static void
9203 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9204 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9205 asection **local_sections, Elf_Internal_Rela *rel)
9206 {
9207 unsigned int r_type, r_symndx;
9208 Elf_Internal_Sym *sym;
9209 asection *sec;
9210
9211 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9212 {
9213 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9214 if (gprel16_reloc_p (r_type)
9215 || r_type == R_MIPS_GPREL32
9216 || literal_reloc_p (r_type))
9217 {
9218 rel->r_addend += _bfd_get_gp_value (input_bfd);
9219 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9220 }
9221
9222 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9223 sym = local_syms + r_symndx;
9224
9225 /* Adjust REL's addend to account for section merging. */
9226 if (!info->relocatable)
9227 {
9228 sec = local_sections[r_symndx];
9229 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9230 }
9231
9232 /* This would normally be done by the rela_normal code in elflink.c. */
9233 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9234 rel->r_addend += local_sections[r_symndx]->output_offset;
9235 }
9236 }
9237
9238 /* Relocate a MIPS ELF section. */
9239
9240 bfd_boolean
9241 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9242 bfd *input_bfd, asection *input_section,
9243 bfd_byte *contents, Elf_Internal_Rela *relocs,
9244 Elf_Internal_Sym *local_syms,
9245 asection **local_sections)
9246 {
9247 Elf_Internal_Rela *rel;
9248 const Elf_Internal_Rela *relend;
9249 bfd_vma addend = 0;
9250 bfd_boolean use_saved_addend_p = FALSE;
9251 const struct elf_backend_data *bed;
9252
9253 bed = get_elf_backend_data (output_bfd);
9254 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9255 for (rel = relocs; rel < relend; ++rel)
9256 {
9257 const char *name;
9258 bfd_vma value = 0;
9259 reloc_howto_type *howto;
9260 bfd_boolean cross_mode_jump_p;
9261 /* TRUE if the relocation is a RELA relocation, rather than a
9262 REL relocation. */
9263 bfd_boolean rela_relocation_p = TRUE;
9264 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9265 const char *msg;
9266 unsigned long r_symndx;
9267 asection *sec;
9268 Elf_Internal_Shdr *symtab_hdr;
9269 struct elf_link_hash_entry *h;
9270 bfd_boolean rel_reloc;
9271
9272 rel_reloc = (NEWABI_P (input_bfd)
9273 && mips_elf_rel_relocation_p (input_bfd, input_section,
9274 relocs, rel));
9275 /* Find the relocation howto for this relocation. */
9276 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9277
9278 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9279 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9280 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9281 {
9282 sec = local_sections[r_symndx];
9283 h = NULL;
9284 }
9285 else
9286 {
9287 unsigned long extsymoff;
9288
9289 extsymoff = 0;
9290 if (!elf_bad_symtab (input_bfd))
9291 extsymoff = symtab_hdr->sh_info;
9292 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9293 while (h->root.type == bfd_link_hash_indirect
9294 || h->root.type == bfd_link_hash_warning)
9295 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9296
9297 sec = NULL;
9298 if (h->root.type == bfd_link_hash_defined
9299 || h->root.type == bfd_link_hash_defweak)
9300 sec = h->root.u.def.section;
9301 }
9302
9303 if (sec != NULL && elf_discarded_section (sec))
9304 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9305 rel, relend, howto, contents);
9306
9307 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9308 {
9309 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9310 64-bit code, but make sure all their addresses are in the
9311 lowermost or uppermost 32-bit section of the 64-bit address
9312 space. Thus, when they use an R_MIPS_64 they mean what is
9313 usually meant by R_MIPS_32, with the exception that the
9314 stored value is sign-extended to 64 bits. */
9315 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9316
9317 /* On big-endian systems, we need to lie about the position
9318 of the reloc. */
9319 if (bfd_big_endian (input_bfd))
9320 rel->r_offset += 4;
9321 }
9322
9323 if (!use_saved_addend_p)
9324 {
9325 /* If these relocations were originally of the REL variety,
9326 we must pull the addend out of the field that will be
9327 relocated. Otherwise, we simply use the contents of the
9328 RELA relocation. */
9329 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9330 relocs, rel))
9331 {
9332 rela_relocation_p = FALSE;
9333 addend = mips_elf_read_rel_addend (input_bfd, rel,
9334 howto, contents);
9335 if (hi16_reloc_p (r_type)
9336 || (got16_reloc_p (r_type)
9337 && mips_elf_local_relocation_p (input_bfd, rel,
9338 local_sections)))
9339 {
9340 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9341 contents, &addend))
9342 {
9343 if (h)
9344 name = h->root.root.string;
9345 else
9346 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9347 local_syms + r_symndx,
9348 sec);
9349 (*_bfd_error_handler)
9350 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9351 input_bfd, input_section, name, howto->name,
9352 rel->r_offset);
9353 }
9354 }
9355 else
9356 addend <<= howto->rightshift;
9357 }
9358 else
9359 addend = rel->r_addend;
9360 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9361 local_syms, local_sections, rel);
9362 }
9363
9364 if (info->relocatable)
9365 {
9366 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9367 && bfd_big_endian (input_bfd))
9368 rel->r_offset -= 4;
9369
9370 if (!rela_relocation_p && rel->r_addend)
9371 {
9372 addend += rel->r_addend;
9373 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9374 addend = mips_elf_high (addend);
9375 else if (r_type == R_MIPS_HIGHER)
9376 addend = mips_elf_higher (addend);
9377 else if (r_type == R_MIPS_HIGHEST)
9378 addend = mips_elf_highest (addend);
9379 else
9380 addend >>= howto->rightshift;
9381
9382 /* We use the source mask, rather than the destination
9383 mask because the place to which we are writing will be
9384 source of the addend in the final link. */
9385 addend &= howto->src_mask;
9386
9387 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9388 /* See the comment above about using R_MIPS_64 in the 32-bit
9389 ABI. Here, we need to update the addend. It would be
9390 possible to get away with just using the R_MIPS_32 reloc
9391 but for endianness. */
9392 {
9393 bfd_vma sign_bits;
9394 bfd_vma low_bits;
9395 bfd_vma high_bits;
9396
9397 if (addend & ((bfd_vma) 1 << 31))
9398 #ifdef BFD64
9399 sign_bits = ((bfd_vma) 1 << 32) - 1;
9400 #else
9401 sign_bits = -1;
9402 #endif
9403 else
9404 sign_bits = 0;
9405
9406 /* If we don't know that we have a 64-bit type,
9407 do two separate stores. */
9408 if (bfd_big_endian (input_bfd))
9409 {
9410 /* Store the sign-bits (which are most significant)
9411 first. */
9412 low_bits = sign_bits;
9413 high_bits = addend;
9414 }
9415 else
9416 {
9417 low_bits = addend;
9418 high_bits = sign_bits;
9419 }
9420 bfd_put_32 (input_bfd, low_bits,
9421 contents + rel->r_offset);
9422 bfd_put_32 (input_bfd, high_bits,
9423 contents + rel->r_offset + 4);
9424 continue;
9425 }
9426
9427 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9428 input_bfd, input_section,
9429 contents, FALSE))
9430 return FALSE;
9431 }
9432
9433 /* Go on to the next relocation. */
9434 continue;
9435 }
9436
9437 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9438 relocations for the same offset. In that case we are
9439 supposed to treat the output of each relocation as the addend
9440 for the next. */
9441 if (rel + 1 < relend
9442 && rel->r_offset == rel[1].r_offset
9443 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9444 use_saved_addend_p = TRUE;
9445 else
9446 use_saved_addend_p = FALSE;
9447
9448 /* Figure out what value we are supposed to relocate. */
9449 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9450 input_section, info, rel,
9451 addend, howto, local_syms,
9452 local_sections, &value,
9453 &name, &cross_mode_jump_p,
9454 use_saved_addend_p))
9455 {
9456 case bfd_reloc_continue:
9457 /* There's nothing to do. */
9458 continue;
9459
9460 case bfd_reloc_undefined:
9461 /* mips_elf_calculate_relocation already called the
9462 undefined_symbol callback. There's no real point in
9463 trying to perform the relocation at this point, so we
9464 just skip ahead to the next relocation. */
9465 continue;
9466
9467 case bfd_reloc_notsupported:
9468 msg = _("internal error: unsupported relocation error");
9469 info->callbacks->warning
9470 (info, msg, name, input_bfd, input_section, rel->r_offset);
9471 return FALSE;
9472
9473 case bfd_reloc_overflow:
9474 if (use_saved_addend_p)
9475 /* Ignore overflow until we reach the last relocation for
9476 a given location. */
9477 ;
9478 else
9479 {
9480 struct mips_elf_link_hash_table *htab;
9481
9482 htab = mips_elf_hash_table (info);
9483 BFD_ASSERT (htab != NULL);
9484 BFD_ASSERT (name != NULL);
9485 if (!htab->small_data_overflow_reported
9486 && (gprel16_reloc_p (howto->type)
9487 || literal_reloc_p (howto->type)))
9488 {
9489 msg = _("small-data section exceeds 64KB;"
9490 " lower small-data size limit (see option -G)");
9491
9492 htab->small_data_overflow_reported = TRUE;
9493 (*info->callbacks->einfo) ("%P: %s\n", msg);
9494 }
9495 if (! ((*info->callbacks->reloc_overflow)
9496 (info, NULL, name, howto->name, (bfd_vma) 0,
9497 input_bfd, input_section, rel->r_offset)))
9498 return FALSE;
9499 }
9500 break;
9501
9502 case bfd_reloc_ok:
9503 break;
9504
9505 case bfd_reloc_outofrange:
9506 if (jal_reloc_p (howto->type))
9507 {
9508 msg = _("JALX to a non-word-aligned address");
9509 info->callbacks->warning
9510 (info, msg, name, input_bfd, input_section, rel->r_offset);
9511 return FALSE;
9512 }
9513 /* Fall through. */
9514
9515 default:
9516 abort ();
9517 break;
9518 }
9519
9520 /* If we've got another relocation for the address, keep going
9521 until we reach the last one. */
9522 if (use_saved_addend_p)
9523 {
9524 addend = value;
9525 continue;
9526 }
9527
9528 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9529 /* See the comment above about using R_MIPS_64 in the 32-bit
9530 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9531 that calculated the right value. Now, however, we
9532 sign-extend the 32-bit result to 64-bits, and store it as a
9533 64-bit value. We are especially generous here in that we
9534 go to extreme lengths to support this usage on systems with
9535 only a 32-bit VMA. */
9536 {
9537 bfd_vma sign_bits;
9538 bfd_vma low_bits;
9539 bfd_vma high_bits;
9540
9541 if (value & ((bfd_vma) 1 << 31))
9542 #ifdef BFD64
9543 sign_bits = ((bfd_vma) 1 << 32) - 1;
9544 #else
9545 sign_bits = -1;
9546 #endif
9547 else
9548 sign_bits = 0;
9549
9550 /* If we don't know that we have a 64-bit type,
9551 do two separate stores. */
9552 if (bfd_big_endian (input_bfd))
9553 {
9554 /* Undo what we did above. */
9555 rel->r_offset -= 4;
9556 /* Store the sign-bits (which are most significant)
9557 first. */
9558 low_bits = sign_bits;
9559 high_bits = value;
9560 }
9561 else
9562 {
9563 low_bits = value;
9564 high_bits = sign_bits;
9565 }
9566 bfd_put_32 (input_bfd, low_bits,
9567 contents + rel->r_offset);
9568 bfd_put_32 (input_bfd, high_bits,
9569 contents + rel->r_offset + 4);
9570 continue;
9571 }
9572
9573 /* Actually perform the relocation. */
9574 if (! mips_elf_perform_relocation (info, howto, rel, value,
9575 input_bfd, input_section,
9576 contents, cross_mode_jump_p))
9577 return FALSE;
9578 }
9579
9580 return TRUE;
9581 }
9582 \f
9583 /* A function that iterates over each entry in la25_stubs and fills
9584 in the code for each one. DATA points to a mips_htab_traverse_info. */
9585
9586 static int
9587 mips_elf_create_la25_stub (void **slot, void *data)
9588 {
9589 struct mips_htab_traverse_info *hti;
9590 struct mips_elf_link_hash_table *htab;
9591 struct mips_elf_la25_stub *stub;
9592 asection *s;
9593 bfd_byte *loc;
9594 bfd_vma offset, target, target_high, target_low;
9595
9596 stub = (struct mips_elf_la25_stub *) *slot;
9597 hti = (struct mips_htab_traverse_info *) data;
9598 htab = mips_elf_hash_table (hti->info);
9599 BFD_ASSERT (htab != NULL);
9600
9601 /* Create the section contents, if we haven't already. */
9602 s = stub->stub_section;
9603 loc = s->contents;
9604 if (loc == NULL)
9605 {
9606 loc = bfd_malloc (s->size);
9607 if (loc == NULL)
9608 {
9609 hti->error = TRUE;
9610 return FALSE;
9611 }
9612 s->contents = loc;
9613 }
9614
9615 /* Work out where in the section this stub should go. */
9616 offset = stub->offset;
9617
9618 /* Work out the target address. */
9619 target = (stub->h->root.root.u.def.section->output_section->vma
9620 + stub->h->root.root.u.def.section->output_offset
9621 + stub->h->root.root.u.def.value);
9622 target_high = ((target + 0x8000) >> 16) & 0xffff;
9623 target_low = (target & 0xffff);
9624
9625 if (stub->stub_section != htab->strampoline)
9626 {
9627 /* This is a simple LUI/ADDIU stub. Zero out the beginning
9628 of the section and write the two instructions at the end. */
9629 memset (loc, 0, offset);
9630 loc += offset;
9631 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
9632 {
9633 bfd_put_16 (hti->output_bfd, LA25_LUI_MICROMIPS_1 (target_high),
9634 loc);
9635 bfd_put_16 (hti->output_bfd, LA25_LUI_MICROMIPS_2 (target_high),
9636 loc + 2);
9637 bfd_put_16 (hti->output_bfd, LA25_ADDIU_MICROMIPS_1 (target_low),
9638 loc + 4);
9639 bfd_put_16 (hti->output_bfd, LA25_ADDIU_MICROMIPS_2 (target_low),
9640 loc + 6);
9641 }
9642 else
9643 {
9644 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9645 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9646 }
9647 }
9648 else
9649 {
9650 /* This is trampoline. */
9651 loc += offset;
9652 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
9653 {
9654 bfd_put_16 (hti->output_bfd, LA25_LUI_MICROMIPS_1 (target_high),
9655 loc);
9656 bfd_put_16 (hti->output_bfd, LA25_LUI_MICROMIPS_2 (target_high),
9657 loc + 2);
9658 bfd_put_16 (hti->output_bfd, LA25_J_MICROMIPS_1 (target), loc + 4);
9659 bfd_put_16 (hti->output_bfd, LA25_J_MICROMIPS_2 (target), loc + 6);
9660 bfd_put_16 (hti->output_bfd, LA25_ADDIU_MICROMIPS_1 (target_low),
9661 loc + 8);
9662 bfd_put_16 (hti->output_bfd, LA25_ADDIU_MICROMIPS_2 (target_low),
9663 loc + 10);
9664 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9665 }
9666 else
9667 {
9668 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9669 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9670 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9671 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9672 }
9673 }
9674 return TRUE;
9675 }
9676
9677 /* If NAME is one of the special IRIX6 symbols defined by the linker,
9678 adjust it appropriately now. */
9679
9680 static void
9681 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9682 const char *name, Elf_Internal_Sym *sym)
9683 {
9684 /* The linker script takes care of providing names and values for
9685 these, but we must place them into the right sections. */
9686 static const char* const text_section_symbols[] = {
9687 "_ftext",
9688 "_etext",
9689 "__dso_displacement",
9690 "__elf_header",
9691 "__program_header_table",
9692 NULL
9693 };
9694
9695 static const char* const data_section_symbols[] = {
9696 "_fdata",
9697 "_edata",
9698 "_end",
9699 "_fbss",
9700 NULL
9701 };
9702
9703 const char* const *p;
9704 int i;
9705
9706 for (i = 0; i < 2; ++i)
9707 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9708 *p;
9709 ++p)
9710 if (strcmp (*p, name) == 0)
9711 {
9712 /* All of these symbols are given type STT_SECTION by the
9713 IRIX6 linker. */
9714 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9715 sym->st_other = STO_PROTECTED;
9716
9717 /* The IRIX linker puts these symbols in special sections. */
9718 if (i == 0)
9719 sym->st_shndx = SHN_MIPS_TEXT;
9720 else
9721 sym->st_shndx = SHN_MIPS_DATA;
9722
9723 break;
9724 }
9725 }
9726
9727 /* Finish up dynamic symbol handling. We set the contents of various
9728 dynamic sections here. */
9729
9730 bfd_boolean
9731 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9732 struct bfd_link_info *info,
9733 struct elf_link_hash_entry *h,
9734 Elf_Internal_Sym *sym)
9735 {
9736 bfd *dynobj;
9737 asection *sgot;
9738 struct mips_got_info *g, *gg;
9739 const char *name;
9740 int idx;
9741 struct mips_elf_link_hash_table *htab;
9742 struct mips_elf_link_hash_entry *hmips;
9743
9744 htab = mips_elf_hash_table (info);
9745 BFD_ASSERT (htab != NULL);
9746 dynobj = elf_hash_table (info)->dynobj;
9747 hmips = (struct mips_elf_link_hash_entry *) h;
9748
9749 BFD_ASSERT (!htab->is_vxworks);
9750
9751 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9752 {
9753 /* We've decided to create a PLT entry for this symbol. */
9754 bfd_byte *loc;
9755 bfd_vma header_address, plt_index, got_address;
9756 bfd_vma got_address_high, got_address_low, load;
9757 const bfd_vma *plt_entry;
9758
9759 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9760 BFD_ASSERT (h->dynindx != -1);
9761 BFD_ASSERT (htab->splt != NULL);
9762 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9763 BFD_ASSERT (!h->def_regular);
9764
9765 /* Calculate the address of the PLT header. */
9766 header_address = (htab->splt->output_section->vma
9767 + htab->splt->output_offset);
9768
9769 /* Calculate the index of the entry. */
9770 plt_index = ((h->plt.offset - htab->plt_header_size)
9771 / htab->plt_entry_size);
9772
9773 /* Calculate the address of the .got.plt entry. */
9774 got_address = (htab->sgotplt->output_section->vma
9775 + htab->sgotplt->output_offset
9776 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9777 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9778 got_address_low = got_address & 0xffff;
9779
9780 /* Initially point the .got.plt entry at the PLT header. */
9781 loc = (htab->sgotplt->contents
9782 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9783 if (ABI_64_P (output_bfd))
9784 bfd_put_64 (output_bfd, header_address, loc);
9785 else
9786 bfd_put_32 (output_bfd, header_address, loc);
9787
9788 /* Find out where the .plt entry should go. */
9789 loc = htab->splt->contents + h->plt.offset;
9790
9791 /* Pick the load opcode. */
9792 load = MIPS_ELF_LOAD_WORD (output_bfd);
9793
9794 /* Fill in the PLT entry itself. */
9795 plt_entry = mips_exec_plt_entry;
9796 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9797 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
9798
9799 if (! LOAD_INTERLOCKS_P (output_bfd))
9800 {
9801 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9802 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9803 }
9804 else
9805 {
9806 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9807 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9808 }
9809
9810 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9811 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9812 plt_index, h->dynindx,
9813 R_MIPS_JUMP_SLOT, got_address);
9814
9815 /* We distinguish between PLT entries and lazy-binding stubs by
9816 giving the former an st_other value of STO_MIPS_PLT. Set the
9817 flag and leave the value if there are any relocations in the
9818 binary where pointer equality matters. */
9819 sym->st_shndx = SHN_UNDEF;
9820 if (h->pointer_equality_needed)
9821 sym->st_other = STO_MIPS_PLT;
9822 else
9823 sym->st_value = 0;
9824 }
9825 else if (h->plt.offset != MINUS_ONE)
9826 {
9827 /* We've decided to create a lazy-binding stub. */
9828 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
9829
9830 /* This symbol has a stub. Set it up. */
9831
9832 BFD_ASSERT (h->dynindx != -1);
9833
9834 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9835 || (h->dynindx <= 0xffff));
9836
9837 /* Values up to 2^31 - 1 are allowed. Larger values would cause
9838 sign extension at runtime in the stub, resulting in a negative
9839 index value. */
9840 if (h->dynindx & ~0x7fffffff)
9841 return FALSE;
9842
9843 /* Fill the stub. */
9844 idx = 0;
9845 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9846 idx += 4;
9847 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9848 idx += 4;
9849 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9850 {
9851 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
9852 stub + idx);
9853 idx += 4;
9854 }
9855 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9856 idx += 4;
9857
9858 /* If a large stub is not required and sign extension is not a
9859 problem, then use legacy code in the stub. */
9860 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9861 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9862 else if (h->dynindx & ~0x7fff)
9863 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9864 else
9865 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9866 stub + idx);
9867
9868 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9869 memcpy (htab->sstubs->contents + h->plt.offset,
9870 stub, htab->function_stub_size);
9871
9872 /* Mark the symbol as undefined. plt.offset != -1 occurs
9873 only for the referenced symbol. */
9874 sym->st_shndx = SHN_UNDEF;
9875
9876 /* The run-time linker uses the st_value field of the symbol
9877 to reset the global offset table entry for this external
9878 to its stub address when unlinking a shared object. */
9879 sym->st_value = (htab->sstubs->output_section->vma
9880 + htab->sstubs->output_offset
9881 + h->plt.offset);
9882 }
9883
9884 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9885 refer to the stub, since only the stub uses the standard calling
9886 conventions. */
9887 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9888 {
9889 BFD_ASSERT (hmips->need_fn_stub);
9890 sym->st_value = (hmips->fn_stub->output_section->vma
9891 + hmips->fn_stub->output_offset);
9892 sym->st_size = hmips->fn_stub->size;
9893 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9894 }
9895
9896 BFD_ASSERT (h->dynindx != -1
9897 || h->forced_local);
9898
9899 sgot = htab->sgot;
9900 g = htab->got_info;
9901 BFD_ASSERT (g != NULL);
9902
9903 /* Run through the global symbol table, creating GOT entries for all
9904 the symbols that need them. */
9905 if (hmips->global_got_area != GGA_NONE)
9906 {
9907 bfd_vma offset;
9908 bfd_vma value;
9909
9910 value = sym->st_value;
9911 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9912 R_MIPS_GOT16, info);
9913 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9914 }
9915
9916 if (hmips->global_got_area != GGA_NONE && g->next && h->type != STT_TLS)
9917 {
9918 struct mips_got_entry e, *p;
9919 bfd_vma entry;
9920 bfd_vma offset;
9921
9922 gg = g;
9923
9924 e.abfd = output_bfd;
9925 e.symndx = -1;
9926 e.d.h = hmips;
9927 e.tls_type = 0;
9928
9929 for (g = g->next; g->next != gg; g = g->next)
9930 {
9931 if (g->got_entries
9932 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9933 &e)))
9934 {
9935 offset = p->gotidx;
9936 if (info->shared
9937 || (elf_hash_table (info)->dynamic_sections_created
9938 && p->d.h != NULL
9939 && p->d.h->root.def_dynamic
9940 && !p->d.h->root.def_regular))
9941 {
9942 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9943 the various compatibility problems, it's easier to mock
9944 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9945 mips_elf_create_dynamic_relocation to calculate the
9946 appropriate addend. */
9947 Elf_Internal_Rela rel[3];
9948
9949 memset (rel, 0, sizeof (rel));
9950 if (ABI_64_P (output_bfd))
9951 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9952 else
9953 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9954 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9955
9956 entry = 0;
9957 if (! (mips_elf_create_dynamic_relocation
9958 (output_bfd, info, rel,
9959 e.d.h, NULL, sym->st_value, &entry, sgot)))
9960 return FALSE;
9961 }
9962 else
9963 entry = sym->st_value;
9964 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
9965 }
9966 }
9967 }
9968
9969 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9970 name = h->root.root.string;
9971 if (strcmp (name, "_DYNAMIC") == 0
9972 || h == elf_hash_table (info)->hgot)
9973 sym->st_shndx = SHN_ABS;
9974 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9975 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9976 {
9977 sym->st_shndx = SHN_ABS;
9978 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9979 sym->st_value = 1;
9980 }
9981 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
9982 {
9983 sym->st_shndx = SHN_ABS;
9984 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9985 sym->st_value = elf_gp (output_bfd);
9986 }
9987 else if (SGI_COMPAT (output_bfd))
9988 {
9989 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9990 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9991 {
9992 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9993 sym->st_other = STO_PROTECTED;
9994 sym->st_value = 0;
9995 sym->st_shndx = SHN_MIPS_DATA;
9996 }
9997 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9998 {
9999 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10000 sym->st_other = STO_PROTECTED;
10001 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10002 sym->st_shndx = SHN_ABS;
10003 }
10004 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10005 {
10006 if (h->type == STT_FUNC)
10007 sym->st_shndx = SHN_MIPS_TEXT;
10008 else if (h->type == STT_OBJECT)
10009 sym->st_shndx = SHN_MIPS_DATA;
10010 }
10011 }
10012
10013 /* Emit a copy reloc, if needed. */
10014 if (h->needs_copy)
10015 {
10016 asection *s;
10017 bfd_vma symval;
10018
10019 BFD_ASSERT (h->dynindx != -1);
10020 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10021
10022 s = mips_elf_rel_dyn_section (info, FALSE);
10023 symval = (h->root.u.def.section->output_section->vma
10024 + h->root.u.def.section->output_offset
10025 + h->root.u.def.value);
10026 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10027 h->dynindx, R_MIPS_COPY, symval);
10028 }
10029
10030 /* Handle the IRIX6-specific symbols. */
10031 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10032 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10033
10034 if (! info->shared)
10035 {
10036 if (! mips_elf_hash_table (info)->use_rld_obj_head
10037 && (strcmp (name, "__rld_map") == 0
10038 || strcmp (name, "__RLD_MAP") == 0))
10039 {
10040 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
10041 BFD_ASSERT (s != NULL);
10042 sym->st_value = s->output_section->vma + s->output_offset;
10043 bfd_put_32 (output_bfd, 0, s->contents);
10044 if (mips_elf_hash_table (info)->rld_value == 0)
10045 mips_elf_hash_table (info)->rld_value = sym->st_value;
10046 }
10047 else if (mips_elf_hash_table (info)->use_rld_obj_head
10048 && strcmp (name, "__rld_obj_head") == 0)
10049 {
10050 /* IRIX6 does not use a .rld_map section. */
10051 if (IRIX_COMPAT (output_bfd) == ict_irix5
10052 || IRIX_COMPAT (output_bfd) == ict_none)
10053 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
10054 != NULL);
10055 mips_elf_hash_table (info)->rld_value = sym->st_value;
10056 }
10057 }
10058
10059 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
10060 treat MIPS16 symbols like any other. */
10061 if (ELF_ST_IS_MIPS16 (sym->st_other))
10062 {
10063 BFD_ASSERT (sym->st_value & 1);
10064 sym->st_other -= STO_MIPS16;
10065 }
10066
10067 return TRUE;
10068 }
10069
10070 /* Likewise, for VxWorks. */
10071
10072 bfd_boolean
10073 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10074 struct bfd_link_info *info,
10075 struct elf_link_hash_entry *h,
10076 Elf_Internal_Sym *sym)
10077 {
10078 bfd *dynobj;
10079 asection *sgot;
10080 struct mips_got_info *g;
10081 struct mips_elf_link_hash_table *htab;
10082 struct mips_elf_link_hash_entry *hmips;
10083
10084 htab = mips_elf_hash_table (info);
10085 BFD_ASSERT (htab != NULL);
10086 dynobj = elf_hash_table (info)->dynobj;
10087 hmips = (struct mips_elf_link_hash_entry *) h;
10088
10089 if (h->plt.offset != (bfd_vma) -1)
10090 {
10091 bfd_byte *loc;
10092 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
10093 Elf_Internal_Rela rel;
10094 static const bfd_vma *plt_entry;
10095
10096 BFD_ASSERT (h->dynindx != -1);
10097 BFD_ASSERT (htab->splt != NULL);
10098 BFD_ASSERT (h->plt.offset <= htab->splt->size);
10099
10100 /* Calculate the address of the .plt entry. */
10101 plt_address = (htab->splt->output_section->vma
10102 + htab->splt->output_offset
10103 + h->plt.offset);
10104
10105 /* Calculate the index of the entry. */
10106 plt_index = ((h->plt.offset - htab->plt_header_size)
10107 / htab->plt_entry_size);
10108
10109 /* Calculate the address of the .got.plt entry. */
10110 got_address = (htab->sgotplt->output_section->vma
10111 + htab->sgotplt->output_offset
10112 + plt_index * 4);
10113
10114 /* Calculate the offset of the .got.plt entry from
10115 _GLOBAL_OFFSET_TABLE_. */
10116 got_offset = mips_elf_gotplt_index (info, h);
10117
10118 /* Calculate the offset for the branch at the start of the PLT
10119 entry. The branch jumps to the beginning of .plt. */
10120 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
10121
10122 /* Fill in the initial value of the .got.plt entry. */
10123 bfd_put_32 (output_bfd, plt_address,
10124 htab->sgotplt->contents + plt_index * 4);
10125
10126 /* Find out where the .plt entry should go. */
10127 loc = htab->splt->contents + h->plt.offset;
10128
10129 if (info->shared)
10130 {
10131 plt_entry = mips_vxworks_shared_plt_entry;
10132 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10133 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
10134 }
10135 else
10136 {
10137 bfd_vma got_address_high, got_address_low;
10138
10139 plt_entry = mips_vxworks_exec_plt_entry;
10140 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10141 got_address_low = got_address & 0xffff;
10142
10143 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10144 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
10145 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10146 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10147 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10148 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10149 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10150 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10151
10152 loc = (htab->srelplt2->contents
10153 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
10154
10155 /* Emit a relocation for the .got.plt entry. */
10156 rel.r_offset = got_address;
10157 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10158 rel.r_addend = h->plt.offset;
10159 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10160
10161 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10162 loc += sizeof (Elf32_External_Rela);
10163 rel.r_offset = plt_address + 8;
10164 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10165 rel.r_addend = got_offset;
10166 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10167
10168 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10169 loc += sizeof (Elf32_External_Rela);
10170 rel.r_offset += 4;
10171 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10172 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10173 }
10174
10175 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10176 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
10177 rel.r_offset = got_address;
10178 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10179 rel.r_addend = 0;
10180 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10181
10182 if (!h->def_regular)
10183 sym->st_shndx = SHN_UNDEF;
10184 }
10185
10186 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10187
10188 sgot = htab->sgot;
10189 g = htab->got_info;
10190 BFD_ASSERT (g != NULL);
10191
10192 /* See if this symbol has an entry in the GOT. */
10193 if (hmips->global_got_area != GGA_NONE)
10194 {
10195 bfd_vma offset;
10196 Elf_Internal_Rela outrel;
10197 bfd_byte *loc;
10198 asection *s;
10199
10200 /* Install the symbol value in the GOT. */
10201 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
10202 R_MIPS_GOT16, info);
10203 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10204
10205 /* Add a dynamic relocation for it. */
10206 s = mips_elf_rel_dyn_section (info, FALSE);
10207 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10208 outrel.r_offset = (sgot->output_section->vma
10209 + sgot->output_offset
10210 + offset);
10211 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10212 outrel.r_addend = 0;
10213 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10214 }
10215
10216 /* Emit a copy reloc, if needed. */
10217 if (h->needs_copy)
10218 {
10219 Elf_Internal_Rela rel;
10220
10221 BFD_ASSERT (h->dynindx != -1);
10222
10223 rel.r_offset = (h->root.u.def.section->output_section->vma
10224 + h->root.u.def.section->output_offset
10225 + h->root.u.def.value);
10226 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10227 rel.r_addend = 0;
10228 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10229 htab->srelbss->contents
10230 + (htab->srelbss->reloc_count
10231 * sizeof (Elf32_External_Rela)));
10232 ++htab->srelbss->reloc_count;
10233 }
10234
10235 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10236 if (ELF_ST_IS_COMPRESSED (sym->st_other))
10237 sym->st_value &= ~1;
10238
10239 return TRUE;
10240 }
10241
10242 /* Write out a plt0 entry to the beginning of .plt. */
10243
10244 static void
10245 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10246 {
10247 bfd_byte *loc;
10248 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10249 static const bfd_vma *plt_entry;
10250 struct mips_elf_link_hash_table *htab;
10251
10252 htab = mips_elf_hash_table (info);
10253 BFD_ASSERT (htab != NULL);
10254
10255 if (ABI_64_P (output_bfd))
10256 plt_entry = mips_n64_exec_plt0_entry;
10257 else if (ABI_N32_P (output_bfd))
10258 plt_entry = mips_n32_exec_plt0_entry;
10259 else
10260 plt_entry = mips_o32_exec_plt0_entry;
10261
10262 /* Calculate the value of .got.plt. */
10263 gotplt_value = (htab->sgotplt->output_section->vma
10264 + htab->sgotplt->output_offset);
10265 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10266 gotplt_value_low = gotplt_value & 0xffff;
10267
10268 /* The PLT sequence is not safe for N64 if .got.plt's address can
10269 not be loaded in two instructions. */
10270 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10271 || ~(gotplt_value | 0x7fffffff) == 0);
10272
10273 /* Install the PLT header. */
10274 loc = htab->splt->contents;
10275 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
10276 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
10277 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
10278 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10279 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10280 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10281 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10282 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10283 }
10284
10285 /* Install the PLT header for a VxWorks executable and finalize the
10286 contents of .rela.plt.unloaded. */
10287
10288 static void
10289 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10290 {
10291 Elf_Internal_Rela rela;
10292 bfd_byte *loc;
10293 bfd_vma got_value, got_value_high, got_value_low, plt_address;
10294 static const bfd_vma *plt_entry;
10295 struct mips_elf_link_hash_table *htab;
10296
10297 htab = mips_elf_hash_table (info);
10298 BFD_ASSERT (htab != NULL);
10299
10300 plt_entry = mips_vxworks_exec_plt0_entry;
10301
10302 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
10303 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
10304 + htab->root.hgot->root.u.def.section->output_offset
10305 + htab->root.hgot->root.u.def.value);
10306
10307 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
10308 got_value_low = got_value & 0xffff;
10309
10310 /* Calculate the address of the PLT header. */
10311 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
10312
10313 /* Install the PLT header. */
10314 loc = htab->splt->contents;
10315 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
10316 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
10317 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
10318 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10319 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10320 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10321
10322 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
10323 loc = htab->srelplt2->contents;
10324 rela.r_offset = plt_address;
10325 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10326 rela.r_addend = 0;
10327 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10328 loc += sizeof (Elf32_External_Rela);
10329
10330 /* Output the relocation for the following addiu of
10331 %lo(_GLOBAL_OFFSET_TABLE_). */
10332 rela.r_offset += 4;
10333 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10334 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10335 loc += sizeof (Elf32_External_Rela);
10336
10337 /* Fix up the remaining relocations. They may have the wrong
10338 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10339 in which symbols were output. */
10340 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10341 {
10342 Elf_Internal_Rela rel;
10343
10344 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10345 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10346 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10347 loc += sizeof (Elf32_External_Rela);
10348
10349 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10350 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10351 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10352 loc += sizeof (Elf32_External_Rela);
10353
10354 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10355 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10356 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10357 loc += sizeof (Elf32_External_Rela);
10358 }
10359 }
10360
10361 /* Install the PLT header for a VxWorks shared library. */
10362
10363 static void
10364 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10365 {
10366 unsigned int i;
10367 struct mips_elf_link_hash_table *htab;
10368
10369 htab = mips_elf_hash_table (info);
10370 BFD_ASSERT (htab != NULL);
10371
10372 /* We just need to copy the entry byte-by-byte. */
10373 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10374 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10375 htab->splt->contents + i * 4);
10376 }
10377
10378 /* Finish up the dynamic sections. */
10379
10380 bfd_boolean
10381 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10382 struct bfd_link_info *info)
10383 {
10384 bfd *dynobj;
10385 asection *sdyn;
10386 asection *sgot;
10387 struct mips_got_info *gg, *g;
10388 struct mips_elf_link_hash_table *htab;
10389
10390 htab = mips_elf_hash_table (info);
10391 BFD_ASSERT (htab != NULL);
10392
10393 dynobj = elf_hash_table (info)->dynobj;
10394
10395 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10396
10397 sgot = htab->sgot;
10398 gg = htab->got_info;
10399
10400 if (elf_hash_table (info)->dynamic_sections_created)
10401 {
10402 bfd_byte *b;
10403 int dyn_to_skip = 0, dyn_skipped = 0;
10404
10405 BFD_ASSERT (sdyn != NULL);
10406 BFD_ASSERT (gg != NULL);
10407
10408 g = mips_elf_got_for_ibfd (gg, output_bfd);
10409 BFD_ASSERT (g != NULL);
10410
10411 for (b = sdyn->contents;
10412 b < sdyn->contents + sdyn->size;
10413 b += MIPS_ELF_DYN_SIZE (dynobj))
10414 {
10415 Elf_Internal_Dyn dyn;
10416 const char *name;
10417 size_t elemsize;
10418 asection *s;
10419 bfd_boolean swap_out_p;
10420
10421 /* Read in the current dynamic entry. */
10422 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10423
10424 /* Assume that we're going to modify it and write it out. */
10425 swap_out_p = TRUE;
10426
10427 switch (dyn.d_tag)
10428 {
10429 case DT_RELENT:
10430 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10431 break;
10432
10433 case DT_RELAENT:
10434 BFD_ASSERT (htab->is_vxworks);
10435 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10436 break;
10437
10438 case DT_STRSZ:
10439 /* Rewrite DT_STRSZ. */
10440 dyn.d_un.d_val =
10441 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10442 break;
10443
10444 case DT_PLTGOT:
10445 s = htab->sgot;
10446 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10447 break;
10448
10449 case DT_MIPS_PLTGOT:
10450 s = htab->sgotplt;
10451 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10452 break;
10453
10454 case DT_MIPS_RLD_VERSION:
10455 dyn.d_un.d_val = 1; /* XXX */
10456 break;
10457
10458 case DT_MIPS_FLAGS:
10459 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10460 break;
10461
10462 case DT_MIPS_TIME_STAMP:
10463 {
10464 time_t t;
10465 time (&t);
10466 dyn.d_un.d_val = t;
10467 }
10468 break;
10469
10470 case DT_MIPS_ICHECKSUM:
10471 /* XXX FIXME: */
10472 swap_out_p = FALSE;
10473 break;
10474
10475 case DT_MIPS_IVERSION:
10476 /* XXX FIXME: */
10477 swap_out_p = FALSE;
10478 break;
10479
10480 case DT_MIPS_BASE_ADDRESS:
10481 s = output_bfd->sections;
10482 BFD_ASSERT (s != NULL);
10483 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10484 break;
10485
10486 case DT_MIPS_LOCAL_GOTNO:
10487 dyn.d_un.d_val = g->local_gotno;
10488 break;
10489
10490 case DT_MIPS_UNREFEXTNO:
10491 /* The index into the dynamic symbol table which is the
10492 entry of the first external symbol that is not
10493 referenced within the same object. */
10494 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10495 break;
10496
10497 case DT_MIPS_GOTSYM:
10498 if (gg->global_gotsym)
10499 {
10500 dyn.d_un.d_val = gg->global_gotsym->dynindx;
10501 break;
10502 }
10503 /* In case if we don't have global got symbols we default
10504 to setting DT_MIPS_GOTSYM to the same value as
10505 DT_MIPS_SYMTABNO, so we just fall through. */
10506
10507 case DT_MIPS_SYMTABNO:
10508 name = ".dynsym";
10509 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10510 s = bfd_get_section_by_name (output_bfd, name);
10511 BFD_ASSERT (s != NULL);
10512
10513 dyn.d_un.d_val = s->size / elemsize;
10514 break;
10515
10516 case DT_MIPS_HIPAGENO:
10517 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
10518 break;
10519
10520 case DT_MIPS_RLD_MAP:
10521 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10522 break;
10523
10524 case DT_MIPS_OPTIONS:
10525 s = (bfd_get_section_by_name
10526 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10527 dyn.d_un.d_ptr = s->vma;
10528 break;
10529
10530 case DT_RELASZ:
10531 BFD_ASSERT (htab->is_vxworks);
10532 /* The count does not include the JUMP_SLOT relocations. */
10533 if (htab->srelplt)
10534 dyn.d_un.d_val -= htab->srelplt->size;
10535 break;
10536
10537 case DT_PLTREL:
10538 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10539 if (htab->is_vxworks)
10540 dyn.d_un.d_val = DT_RELA;
10541 else
10542 dyn.d_un.d_val = DT_REL;
10543 break;
10544
10545 case DT_PLTRELSZ:
10546 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10547 dyn.d_un.d_val = htab->srelplt->size;
10548 break;
10549
10550 case DT_JMPREL:
10551 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10552 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
10553 + htab->srelplt->output_offset);
10554 break;
10555
10556 case DT_TEXTREL:
10557 /* If we didn't need any text relocations after all, delete
10558 the dynamic tag. */
10559 if (!(info->flags & DF_TEXTREL))
10560 {
10561 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10562 swap_out_p = FALSE;
10563 }
10564 break;
10565
10566 case DT_FLAGS:
10567 /* If we didn't need any text relocations after all, clear
10568 DF_TEXTREL from DT_FLAGS. */
10569 if (!(info->flags & DF_TEXTREL))
10570 dyn.d_un.d_val &= ~DF_TEXTREL;
10571 else
10572 swap_out_p = FALSE;
10573 break;
10574
10575 default:
10576 swap_out_p = FALSE;
10577 if (htab->is_vxworks
10578 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10579 swap_out_p = TRUE;
10580 break;
10581 }
10582
10583 if (swap_out_p || dyn_skipped)
10584 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10585 (dynobj, &dyn, b - dyn_skipped);
10586
10587 if (dyn_to_skip)
10588 {
10589 dyn_skipped += dyn_to_skip;
10590 dyn_to_skip = 0;
10591 }
10592 }
10593
10594 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10595 if (dyn_skipped > 0)
10596 memset (b - dyn_skipped, 0, dyn_skipped);
10597 }
10598
10599 if (sgot != NULL && sgot->size > 0
10600 && !bfd_is_abs_section (sgot->output_section))
10601 {
10602 if (htab->is_vxworks)
10603 {
10604 /* The first entry of the global offset table points to the
10605 ".dynamic" section. The second is initialized by the
10606 loader and contains the shared library identifier.
10607 The third is also initialized by the loader and points
10608 to the lazy resolution stub. */
10609 MIPS_ELF_PUT_WORD (output_bfd,
10610 sdyn->output_offset + sdyn->output_section->vma,
10611 sgot->contents);
10612 MIPS_ELF_PUT_WORD (output_bfd, 0,
10613 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10614 MIPS_ELF_PUT_WORD (output_bfd, 0,
10615 sgot->contents
10616 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10617 }
10618 else
10619 {
10620 /* The first entry of the global offset table will be filled at
10621 runtime. The second entry will be used by some runtime loaders.
10622 This isn't the case of IRIX rld. */
10623 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
10624 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10625 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10626 }
10627
10628 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10629 = MIPS_ELF_GOT_SIZE (output_bfd);
10630 }
10631
10632 /* Generate dynamic relocations for the non-primary gots. */
10633 if (gg != NULL && gg->next)
10634 {
10635 Elf_Internal_Rela rel[3];
10636 bfd_vma addend = 0;
10637
10638 memset (rel, 0, sizeof (rel));
10639 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10640
10641 for (g = gg->next; g->next != gg; g = g->next)
10642 {
10643 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
10644 + g->next->tls_gotno;
10645
10646 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
10647 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10648 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10649 sgot->contents
10650 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10651
10652 if (! info->shared)
10653 continue;
10654
10655 while (got_index < g->assigned_gotno)
10656 {
10657 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10658 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10659 if (!(mips_elf_create_dynamic_relocation
10660 (output_bfd, info, rel, NULL,
10661 bfd_abs_section_ptr,
10662 0, &addend, sgot)))
10663 return FALSE;
10664 BFD_ASSERT (addend == 0);
10665 }
10666 }
10667 }
10668
10669 /* The generation of dynamic relocations for the non-primary gots
10670 adds more dynamic relocations. We cannot count them until
10671 here. */
10672
10673 if (elf_hash_table (info)->dynamic_sections_created)
10674 {
10675 bfd_byte *b;
10676 bfd_boolean swap_out_p;
10677
10678 BFD_ASSERT (sdyn != NULL);
10679
10680 for (b = sdyn->contents;
10681 b < sdyn->contents + sdyn->size;
10682 b += MIPS_ELF_DYN_SIZE (dynobj))
10683 {
10684 Elf_Internal_Dyn dyn;
10685 asection *s;
10686
10687 /* Read in the current dynamic entry. */
10688 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10689
10690 /* Assume that we're going to modify it and write it out. */
10691 swap_out_p = TRUE;
10692
10693 switch (dyn.d_tag)
10694 {
10695 case DT_RELSZ:
10696 /* Reduce DT_RELSZ to account for any relocations we
10697 decided not to make. This is for the n64 irix rld,
10698 which doesn't seem to apply any relocations if there
10699 are trailing null entries. */
10700 s = mips_elf_rel_dyn_section (info, FALSE);
10701 dyn.d_un.d_val = (s->reloc_count
10702 * (ABI_64_P (output_bfd)
10703 ? sizeof (Elf64_Mips_External_Rel)
10704 : sizeof (Elf32_External_Rel)));
10705 /* Adjust the section size too. Tools like the prelinker
10706 can reasonably expect the values to the same. */
10707 elf_section_data (s->output_section)->this_hdr.sh_size
10708 = dyn.d_un.d_val;
10709 break;
10710
10711 default:
10712 swap_out_p = FALSE;
10713 break;
10714 }
10715
10716 if (swap_out_p)
10717 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10718 (dynobj, &dyn, b);
10719 }
10720 }
10721
10722 {
10723 asection *s;
10724 Elf32_compact_rel cpt;
10725
10726 if (SGI_COMPAT (output_bfd))
10727 {
10728 /* Write .compact_rel section out. */
10729 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10730 if (s != NULL)
10731 {
10732 cpt.id1 = 1;
10733 cpt.num = s->reloc_count;
10734 cpt.id2 = 2;
10735 cpt.offset = (s->output_section->filepos
10736 + sizeof (Elf32_External_compact_rel));
10737 cpt.reserved0 = 0;
10738 cpt.reserved1 = 0;
10739 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10740 ((Elf32_External_compact_rel *)
10741 s->contents));
10742
10743 /* Clean up a dummy stub function entry in .text. */
10744 if (htab->sstubs != NULL)
10745 {
10746 file_ptr dummy_offset;
10747
10748 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10749 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10750 memset (htab->sstubs->contents + dummy_offset, 0,
10751 htab->function_stub_size);
10752 }
10753 }
10754 }
10755
10756 /* The psABI says that the dynamic relocations must be sorted in
10757 increasing order of r_symndx. The VxWorks EABI doesn't require
10758 this, and because the code below handles REL rather than RELA
10759 relocations, using it for VxWorks would be outright harmful. */
10760 if (!htab->is_vxworks)
10761 {
10762 s = mips_elf_rel_dyn_section (info, FALSE);
10763 if (s != NULL
10764 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10765 {
10766 reldyn_sorting_bfd = output_bfd;
10767
10768 if (ABI_64_P (output_bfd))
10769 qsort ((Elf64_External_Rel *) s->contents + 1,
10770 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10771 sort_dynamic_relocs_64);
10772 else
10773 qsort ((Elf32_External_Rel *) s->contents + 1,
10774 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10775 sort_dynamic_relocs);
10776 }
10777 }
10778 }
10779
10780 if (htab->splt && htab->splt->size > 0)
10781 {
10782 if (htab->is_vxworks)
10783 {
10784 if (info->shared)
10785 mips_vxworks_finish_shared_plt (output_bfd, info);
10786 else
10787 mips_vxworks_finish_exec_plt (output_bfd, info);
10788 }
10789 else
10790 {
10791 BFD_ASSERT (!info->shared);
10792 mips_finish_exec_plt (output_bfd, info);
10793 }
10794 }
10795 return TRUE;
10796 }
10797
10798
10799 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10800
10801 static void
10802 mips_set_isa_flags (bfd *abfd)
10803 {
10804 flagword val;
10805
10806 switch (bfd_get_mach (abfd))
10807 {
10808 default:
10809 case bfd_mach_mips3000:
10810 val = E_MIPS_ARCH_1;
10811 break;
10812
10813 case bfd_mach_mips3900:
10814 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10815 break;
10816
10817 case bfd_mach_mips6000:
10818 val = E_MIPS_ARCH_2;
10819 break;
10820
10821 case bfd_mach_mips4000:
10822 case bfd_mach_mips4300:
10823 case bfd_mach_mips4400:
10824 case bfd_mach_mips4600:
10825 val = E_MIPS_ARCH_3;
10826 break;
10827
10828 case bfd_mach_mips4010:
10829 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10830 break;
10831
10832 case bfd_mach_mips4100:
10833 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10834 break;
10835
10836 case bfd_mach_mips4111:
10837 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10838 break;
10839
10840 case bfd_mach_mips4120:
10841 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10842 break;
10843
10844 case bfd_mach_mips4650:
10845 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10846 break;
10847
10848 case bfd_mach_mips5400:
10849 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10850 break;
10851
10852 case bfd_mach_mips5500:
10853 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10854 break;
10855
10856 case bfd_mach_mips9000:
10857 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10858 break;
10859
10860 case bfd_mach_mips5000:
10861 case bfd_mach_mips7000:
10862 case bfd_mach_mips8000:
10863 case bfd_mach_mips10000:
10864 case bfd_mach_mips12000:
10865 case bfd_mach_mips14000:
10866 case bfd_mach_mips16000:
10867 val = E_MIPS_ARCH_4;
10868 break;
10869
10870 case bfd_mach_mips5:
10871 val = E_MIPS_ARCH_5;
10872 break;
10873
10874 case bfd_mach_mips_loongson_2e:
10875 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10876 break;
10877
10878 case bfd_mach_mips_loongson_2f:
10879 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10880 break;
10881
10882 case bfd_mach_mips_sb1:
10883 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10884 break;
10885
10886 case bfd_mach_mips_loongson_3a:
10887 val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
10888 break;
10889
10890 case bfd_mach_mips_octeon:
10891 case bfd_mach_mips_octeonp:
10892 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10893 break;
10894
10895 case bfd_mach_mips_xlr:
10896 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10897 break;
10898
10899 case bfd_mach_mips_octeon2:
10900 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
10901 break;
10902
10903 case bfd_mach_mipsisa32:
10904 val = E_MIPS_ARCH_32;
10905 break;
10906
10907 case bfd_mach_mipsisa64:
10908 val = E_MIPS_ARCH_64;
10909 break;
10910
10911 case bfd_mach_mipsisa32r2:
10912 val = E_MIPS_ARCH_32R2;
10913 break;
10914
10915 case bfd_mach_mipsisa64r2:
10916 val = E_MIPS_ARCH_64R2;
10917 break;
10918 }
10919 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10920 elf_elfheader (abfd)->e_flags |= val;
10921
10922 }
10923
10924
10925 /* The final processing done just before writing out a MIPS ELF object
10926 file. This gets the MIPS architecture right based on the machine
10927 number. This is used by both the 32-bit and the 64-bit ABI. */
10928
10929 void
10930 _bfd_mips_elf_final_write_processing (bfd *abfd,
10931 bfd_boolean linker ATTRIBUTE_UNUSED)
10932 {
10933 unsigned int i;
10934 Elf_Internal_Shdr **hdrpp;
10935 const char *name;
10936 asection *sec;
10937
10938 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10939 is nonzero. This is for compatibility with old objects, which used
10940 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10941 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10942 mips_set_isa_flags (abfd);
10943
10944 /* Set the sh_info field for .gptab sections and other appropriate
10945 info for each special section. */
10946 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10947 i < elf_numsections (abfd);
10948 i++, hdrpp++)
10949 {
10950 switch ((*hdrpp)->sh_type)
10951 {
10952 case SHT_MIPS_MSYM:
10953 case SHT_MIPS_LIBLIST:
10954 sec = bfd_get_section_by_name (abfd, ".dynstr");
10955 if (sec != NULL)
10956 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10957 break;
10958
10959 case SHT_MIPS_GPTAB:
10960 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10961 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10962 BFD_ASSERT (name != NULL
10963 && CONST_STRNEQ (name, ".gptab."));
10964 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10965 BFD_ASSERT (sec != NULL);
10966 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10967 break;
10968
10969 case SHT_MIPS_CONTENT:
10970 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10971 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10972 BFD_ASSERT (name != NULL
10973 && CONST_STRNEQ (name, ".MIPS.content"));
10974 sec = bfd_get_section_by_name (abfd,
10975 name + sizeof ".MIPS.content" - 1);
10976 BFD_ASSERT (sec != NULL);
10977 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10978 break;
10979
10980 case SHT_MIPS_SYMBOL_LIB:
10981 sec = bfd_get_section_by_name (abfd, ".dynsym");
10982 if (sec != NULL)
10983 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10984 sec = bfd_get_section_by_name (abfd, ".liblist");
10985 if (sec != NULL)
10986 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10987 break;
10988
10989 case SHT_MIPS_EVENTS:
10990 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10991 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10992 BFD_ASSERT (name != NULL);
10993 if (CONST_STRNEQ (name, ".MIPS.events"))
10994 sec = bfd_get_section_by_name (abfd,
10995 name + sizeof ".MIPS.events" - 1);
10996 else
10997 {
10998 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
10999 sec = bfd_get_section_by_name (abfd,
11000 (name
11001 + sizeof ".MIPS.post_rel" - 1));
11002 }
11003 BFD_ASSERT (sec != NULL);
11004 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11005 break;
11006
11007 }
11008 }
11009 }
11010 \f
11011 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
11012 segments. */
11013
11014 int
11015 _bfd_mips_elf_additional_program_headers (bfd *abfd,
11016 struct bfd_link_info *info ATTRIBUTE_UNUSED)
11017 {
11018 asection *s;
11019 int ret = 0;
11020
11021 /* See if we need a PT_MIPS_REGINFO segment. */
11022 s = bfd_get_section_by_name (abfd, ".reginfo");
11023 if (s && (s->flags & SEC_LOAD))
11024 ++ret;
11025
11026 /* See if we need a PT_MIPS_OPTIONS segment. */
11027 if (IRIX_COMPAT (abfd) == ict_irix6
11028 && bfd_get_section_by_name (abfd,
11029 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11030 ++ret;
11031
11032 /* See if we need a PT_MIPS_RTPROC segment. */
11033 if (IRIX_COMPAT (abfd) == ict_irix5
11034 && bfd_get_section_by_name (abfd, ".dynamic")
11035 && bfd_get_section_by_name (abfd, ".mdebug"))
11036 ++ret;
11037
11038 /* Allocate a PT_NULL header in dynamic objects. See
11039 _bfd_mips_elf_modify_segment_map for details. */
11040 if (!SGI_COMPAT (abfd)
11041 && bfd_get_section_by_name (abfd, ".dynamic"))
11042 ++ret;
11043
11044 return ret;
11045 }
11046
11047 /* Modify the segment map for an IRIX5 executable. */
11048
11049 bfd_boolean
11050 _bfd_mips_elf_modify_segment_map (bfd *abfd,
11051 struct bfd_link_info *info)
11052 {
11053 asection *s;
11054 struct elf_segment_map *m, **pm;
11055 bfd_size_type amt;
11056
11057 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11058 segment. */
11059 s = bfd_get_section_by_name (abfd, ".reginfo");
11060 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11061 {
11062 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11063 if (m->p_type == PT_MIPS_REGINFO)
11064 break;
11065 if (m == NULL)
11066 {
11067 amt = sizeof *m;
11068 m = bfd_zalloc (abfd, amt);
11069 if (m == NULL)
11070 return FALSE;
11071
11072 m->p_type = PT_MIPS_REGINFO;
11073 m->count = 1;
11074 m->sections[0] = s;
11075
11076 /* We want to put it after the PHDR and INTERP segments. */
11077 pm = &elf_tdata (abfd)->segment_map;
11078 while (*pm != NULL
11079 && ((*pm)->p_type == PT_PHDR
11080 || (*pm)->p_type == PT_INTERP))
11081 pm = &(*pm)->next;
11082
11083 m->next = *pm;
11084 *pm = m;
11085 }
11086 }
11087
11088 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11089 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
11090 PT_MIPS_OPTIONS segment immediately following the program header
11091 table. */
11092 if (NEWABI_P (abfd)
11093 /* On non-IRIX6 new abi, we'll have already created a segment
11094 for this section, so don't create another. I'm not sure this
11095 is not also the case for IRIX 6, but I can't test it right
11096 now. */
11097 && IRIX_COMPAT (abfd) == ict_irix6)
11098 {
11099 for (s = abfd->sections; s; s = s->next)
11100 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11101 break;
11102
11103 if (s)
11104 {
11105 struct elf_segment_map *options_segment;
11106
11107 pm = &elf_tdata (abfd)->segment_map;
11108 while (*pm != NULL
11109 && ((*pm)->p_type == PT_PHDR
11110 || (*pm)->p_type == PT_INTERP))
11111 pm = &(*pm)->next;
11112
11113 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11114 {
11115 amt = sizeof (struct elf_segment_map);
11116 options_segment = bfd_zalloc (abfd, amt);
11117 options_segment->next = *pm;
11118 options_segment->p_type = PT_MIPS_OPTIONS;
11119 options_segment->p_flags = PF_R;
11120 options_segment->p_flags_valid = TRUE;
11121 options_segment->count = 1;
11122 options_segment->sections[0] = s;
11123 *pm = options_segment;
11124 }
11125 }
11126 }
11127 else
11128 {
11129 if (IRIX_COMPAT (abfd) == ict_irix5)
11130 {
11131 /* If there are .dynamic and .mdebug sections, we make a room
11132 for the RTPROC header. FIXME: Rewrite without section names. */
11133 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11134 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11135 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11136 {
11137 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
11138 if (m->p_type == PT_MIPS_RTPROC)
11139 break;
11140 if (m == NULL)
11141 {
11142 amt = sizeof *m;
11143 m = bfd_zalloc (abfd, amt);
11144 if (m == NULL)
11145 return FALSE;
11146
11147 m->p_type = PT_MIPS_RTPROC;
11148
11149 s = bfd_get_section_by_name (abfd, ".rtproc");
11150 if (s == NULL)
11151 {
11152 m->count = 0;
11153 m->p_flags = 0;
11154 m->p_flags_valid = 1;
11155 }
11156 else
11157 {
11158 m->count = 1;
11159 m->sections[0] = s;
11160 }
11161
11162 /* We want to put it after the DYNAMIC segment. */
11163 pm = &elf_tdata (abfd)->segment_map;
11164 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11165 pm = &(*pm)->next;
11166 if (*pm != NULL)
11167 pm = &(*pm)->next;
11168
11169 m->next = *pm;
11170 *pm = m;
11171 }
11172 }
11173 }
11174 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
11175 .dynstr, .dynsym, and .hash sections, and everything in
11176 between. */
11177 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
11178 pm = &(*pm)->next)
11179 if ((*pm)->p_type == PT_DYNAMIC)
11180 break;
11181 m = *pm;
11182 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
11183 {
11184 /* For a normal mips executable the permissions for the PT_DYNAMIC
11185 segment are read, write and execute. We do that here since
11186 the code in elf.c sets only the read permission. This matters
11187 sometimes for the dynamic linker. */
11188 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
11189 {
11190 m->p_flags = PF_R | PF_W | PF_X;
11191 m->p_flags_valid = 1;
11192 }
11193 }
11194 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11195 glibc's dynamic linker has traditionally derived the number of
11196 tags from the p_filesz field, and sometimes allocates stack
11197 arrays of that size. An overly-big PT_DYNAMIC segment can
11198 be actively harmful in such cases. Making PT_DYNAMIC contain
11199 other sections can also make life hard for the prelinker,
11200 which might move one of the other sections to a different
11201 PT_LOAD segment. */
11202 if (SGI_COMPAT (abfd)
11203 && m != NULL
11204 && m->count == 1
11205 && strcmp (m->sections[0]->name, ".dynamic") == 0)
11206 {
11207 static const char *sec_names[] =
11208 {
11209 ".dynamic", ".dynstr", ".dynsym", ".hash"
11210 };
11211 bfd_vma low, high;
11212 unsigned int i, c;
11213 struct elf_segment_map *n;
11214
11215 low = ~(bfd_vma) 0;
11216 high = 0;
11217 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11218 {
11219 s = bfd_get_section_by_name (abfd, sec_names[i]);
11220 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11221 {
11222 bfd_size_type sz;
11223
11224 if (low > s->vma)
11225 low = s->vma;
11226 sz = s->size;
11227 if (high < s->vma + sz)
11228 high = s->vma + sz;
11229 }
11230 }
11231
11232 c = 0;
11233 for (s = abfd->sections; s != NULL; s = s->next)
11234 if ((s->flags & SEC_LOAD) != 0
11235 && s->vma >= low
11236 && s->vma + s->size <= high)
11237 ++c;
11238
11239 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
11240 n = bfd_zalloc (abfd, amt);
11241 if (n == NULL)
11242 return FALSE;
11243 *n = *m;
11244 n->count = c;
11245
11246 i = 0;
11247 for (s = abfd->sections; s != NULL; s = s->next)
11248 {
11249 if ((s->flags & SEC_LOAD) != 0
11250 && s->vma >= low
11251 && s->vma + s->size <= high)
11252 {
11253 n->sections[i] = s;
11254 ++i;
11255 }
11256 }
11257
11258 *pm = n;
11259 }
11260 }
11261
11262 /* Allocate a spare program header in dynamic objects so that tools
11263 like the prelinker can add an extra PT_LOAD entry.
11264
11265 If the prelinker needs to make room for a new PT_LOAD entry, its
11266 standard procedure is to move the first (read-only) sections into
11267 the new (writable) segment. However, the MIPS ABI requires
11268 .dynamic to be in a read-only segment, and the section will often
11269 start within sizeof (ElfNN_Phdr) bytes of the last program header.
11270
11271 Although the prelinker could in principle move .dynamic to a
11272 writable segment, it seems better to allocate a spare program
11273 header instead, and avoid the need to move any sections.
11274 There is a long tradition of allocating spare dynamic tags,
11275 so allocating a spare program header seems like a natural
11276 extension.
11277
11278 If INFO is NULL, we may be copying an already prelinked binary
11279 with objcopy or strip, so do not add this header. */
11280 if (info != NULL
11281 && !SGI_COMPAT (abfd)
11282 && bfd_get_section_by_name (abfd, ".dynamic"))
11283 {
11284 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
11285 if ((*pm)->p_type == PT_NULL)
11286 break;
11287 if (*pm == NULL)
11288 {
11289 m = bfd_zalloc (abfd, sizeof (*m));
11290 if (m == NULL)
11291 return FALSE;
11292
11293 m->p_type = PT_NULL;
11294 *pm = m;
11295 }
11296 }
11297
11298 return TRUE;
11299 }
11300 \f
11301 /* Return the section that should be marked against GC for a given
11302 relocation. */
11303
11304 asection *
11305 _bfd_mips_elf_gc_mark_hook (asection *sec,
11306 struct bfd_link_info *info,
11307 Elf_Internal_Rela *rel,
11308 struct elf_link_hash_entry *h,
11309 Elf_Internal_Sym *sym)
11310 {
11311 /* ??? Do mips16 stub sections need to be handled special? */
11312
11313 if (h != NULL)
11314 switch (ELF_R_TYPE (sec->owner, rel->r_info))
11315 {
11316 case R_MIPS_GNU_VTINHERIT:
11317 case R_MIPS_GNU_VTENTRY:
11318 return NULL;
11319 }
11320
11321 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
11322 }
11323
11324 /* Update the got entry reference counts for the section being removed. */
11325
11326 bfd_boolean
11327 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
11328 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11329 asection *sec ATTRIBUTE_UNUSED,
11330 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
11331 {
11332 #if 0
11333 Elf_Internal_Shdr *symtab_hdr;
11334 struct elf_link_hash_entry **sym_hashes;
11335 bfd_signed_vma *local_got_refcounts;
11336 const Elf_Internal_Rela *rel, *relend;
11337 unsigned long r_symndx;
11338 struct elf_link_hash_entry *h;
11339
11340 if (info->relocatable)
11341 return TRUE;
11342
11343 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11344 sym_hashes = elf_sym_hashes (abfd);
11345 local_got_refcounts = elf_local_got_refcounts (abfd);
11346
11347 relend = relocs + sec->reloc_count;
11348 for (rel = relocs; rel < relend; rel++)
11349 switch (ELF_R_TYPE (abfd, rel->r_info))
11350 {
11351 case R_MIPS16_GOT16:
11352 case R_MIPS16_CALL16:
11353 case R_MIPS_GOT16:
11354 case R_MIPS_CALL16:
11355 case R_MIPS_CALL_HI16:
11356 case R_MIPS_CALL_LO16:
11357 case R_MIPS_GOT_HI16:
11358 case R_MIPS_GOT_LO16:
11359 case R_MIPS_GOT_DISP:
11360 case R_MIPS_GOT_PAGE:
11361 case R_MIPS_GOT_OFST:
11362 case R_MICROMIPS_GOT16:
11363 case R_MICROMIPS_CALL16:
11364 case R_MICROMIPS_CALL_HI16:
11365 case R_MICROMIPS_CALL_LO16:
11366 case R_MICROMIPS_GOT_HI16:
11367 case R_MICROMIPS_GOT_LO16:
11368 case R_MICROMIPS_GOT_DISP:
11369 case R_MICROMIPS_GOT_PAGE:
11370 case R_MICROMIPS_GOT_OFST:
11371 /* ??? It would seem that the existing MIPS code does no sort
11372 of reference counting or whatnot on its GOT and PLT entries,
11373 so it is not possible to garbage collect them at this time. */
11374 break;
11375
11376 default:
11377 break;
11378 }
11379 #endif
11380
11381 return TRUE;
11382 }
11383 \f
11384 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11385 hiding the old indirect symbol. Process additional relocation
11386 information. Also called for weakdefs, in which case we just let
11387 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11388
11389 void
11390 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
11391 struct elf_link_hash_entry *dir,
11392 struct elf_link_hash_entry *ind)
11393 {
11394 struct mips_elf_link_hash_entry *dirmips, *indmips;
11395
11396 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
11397
11398 dirmips = (struct mips_elf_link_hash_entry *) dir;
11399 indmips = (struct mips_elf_link_hash_entry *) ind;
11400 /* Any absolute non-dynamic relocations against an indirect or weak
11401 definition will be against the target symbol. */
11402 if (indmips->has_static_relocs)
11403 dirmips->has_static_relocs = TRUE;
11404
11405 if (ind->root.type != bfd_link_hash_indirect)
11406 return;
11407
11408 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11409 if (indmips->readonly_reloc)
11410 dirmips->readonly_reloc = TRUE;
11411 if (indmips->no_fn_stub)
11412 dirmips->no_fn_stub = TRUE;
11413 if (indmips->fn_stub)
11414 {
11415 dirmips->fn_stub = indmips->fn_stub;
11416 indmips->fn_stub = NULL;
11417 }
11418 if (indmips->need_fn_stub)
11419 {
11420 dirmips->need_fn_stub = TRUE;
11421 indmips->need_fn_stub = FALSE;
11422 }
11423 if (indmips->call_stub)
11424 {
11425 dirmips->call_stub = indmips->call_stub;
11426 indmips->call_stub = NULL;
11427 }
11428 if (indmips->call_fp_stub)
11429 {
11430 dirmips->call_fp_stub = indmips->call_fp_stub;
11431 indmips->call_fp_stub = NULL;
11432 }
11433 if (indmips->global_got_area < dirmips->global_got_area)
11434 dirmips->global_got_area = indmips->global_got_area;
11435 if (indmips->global_got_area < GGA_NONE)
11436 indmips->global_got_area = GGA_NONE;
11437 if (indmips->has_nonpic_branches)
11438 dirmips->has_nonpic_branches = TRUE;
11439
11440 if (dirmips->tls_type == 0)
11441 dirmips->tls_type = indmips->tls_type;
11442 }
11443 \f
11444 #define PDR_SIZE 32
11445
11446 bfd_boolean
11447 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11448 struct bfd_link_info *info)
11449 {
11450 asection *o;
11451 bfd_boolean ret = FALSE;
11452 unsigned char *tdata;
11453 size_t i, skip;
11454
11455 o = bfd_get_section_by_name (abfd, ".pdr");
11456 if (! o)
11457 return FALSE;
11458 if (o->size == 0)
11459 return FALSE;
11460 if (o->size % PDR_SIZE != 0)
11461 return FALSE;
11462 if (o->output_section != NULL
11463 && bfd_is_abs_section (o->output_section))
11464 return FALSE;
11465
11466 tdata = bfd_zmalloc (o->size / PDR_SIZE);
11467 if (! tdata)
11468 return FALSE;
11469
11470 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
11471 info->keep_memory);
11472 if (!cookie->rels)
11473 {
11474 free (tdata);
11475 return FALSE;
11476 }
11477
11478 cookie->rel = cookie->rels;
11479 cookie->relend = cookie->rels + o->reloc_count;
11480
11481 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
11482 {
11483 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
11484 {
11485 tdata[i] = 1;
11486 skip ++;
11487 }
11488 }
11489
11490 if (skip != 0)
11491 {
11492 mips_elf_section_data (o)->u.tdata = tdata;
11493 o->size -= skip * PDR_SIZE;
11494 ret = TRUE;
11495 }
11496 else
11497 free (tdata);
11498
11499 if (! info->keep_memory)
11500 free (cookie->rels);
11501
11502 return ret;
11503 }
11504
11505 bfd_boolean
11506 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
11507 {
11508 if (strcmp (sec->name, ".pdr") == 0)
11509 return TRUE;
11510 return FALSE;
11511 }
11512
11513 bfd_boolean
11514 _bfd_mips_elf_write_section (bfd *output_bfd,
11515 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11516 asection *sec, bfd_byte *contents)
11517 {
11518 bfd_byte *to, *from, *end;
11519 int i;
11520
11521 if (strcmp (sec->name, ".pdr") != 0)
11522 return FALSE;
11523
11524 if (mips_elf_section_data (sec)->u.tdata == NULL)
11525 return FALSE;
11526
11527 to = contents;
11528 end = contents + sec->size;
11529 for (from = contents, i = 0;
11530 from < end;
11531 from += PDR_SIZE, i++)
11532 {
11533 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
11534 continue;
11535 if (to != from)
11536 memcpy (to, from, PDR_SIZE);
11537 to += PDR_SIZE;
11538 }
11539 bfd_set_section_contents (output_bfd, sec->output_section, contents,
11540 sec->output_offset, sec->size);
11541 return TRUE;
11542 }
11543 \f
11544 /* microMIPS code retains local labels for linker relaxation. Omit them
11545 from output by default for clarity. */
11546
11547 bfd_boolean
11548 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
11549 {
11550 return _bfd_elf_is_local_label_name (abfd, sym->name);
11551 }
11552
11553 /* MIPS ELF uses a special find_nearest_line routine in order the
11554 handle the ECOFF debugging information. */
11555
11556 struct mips_elf_find_line
11557 {
11558 struct ecoff_debug_info d;
11559 struct ecoff_find_line i;
11560 };
11561
11562 bfd_boolean
11563 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11564 asymbol **symbols, bfd_vma offset,
11565 const char **filename_ptr,
11566 const char **functionname_ptr,
11567 unsigned int *line_ptr)
11568 {
11569 asection *msec;
11570
11571 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11572 filename_ptr, functionname_ptr,
11573 line_ptr))
11574 return TRUE;
11575
11576 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
11577 section, symbols, offset,
11578 filename_ptr, functionname_ptr,
11579 line_ptr, ABI_64_P (abfd) ? 8 : 0,
11580 &elf_tdata (abfd)->dwarf2_find_line_info))
11581 return TRUE;
11582
11583 msec = bfd_get_section_by_name (abfd, ".mdebug");
11584 if (msec != NULL)
11585 {
11586 flagword origflags;
11587 struct mips_elf_find_line *fi;
11588 const struct ecoff_debug_swap * const swap =
11589 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11590
11591 /* If we are called during a link, mips_elf_final_link may have
11592 cleared the SEC_HAS_CONTENTS field. We force it back on here
11593 if appropriate (which it normally will be). */
11594 origflags = msec->flags;
11595 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11596 msec->flags |= SEC_HAS_CONTENTS;
11597
11598 fi = elf_tdata (abfd)->find_line_info;
11599 if (fi == NULL)
11600 {
11601 bfd_size_type external_fdr_size;
11602 char *fraw_src;
11603 char *fraw_end;
11604 struct fdr *fdr_ptr;
11605 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11606
11607 fi = bfd_zalloc (abfd, amt);
11608 if (fi == NULL)
11609 {
11610 msec->flags = origflags;
11611 return FALSE;
11612 }
11613
11614 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11615 {
11616 msec->flags = origflags;
11617 return FALSE;
11618 }
11619
11620 /* Swap in the FDR information. */
11621 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
11622 fi->d.fdr = bfd_alloc (abfd, amt);
11623 if (fi->d.fdr == NULL)
11624 {
11625 msec->flags = origflags;
11626 return FALSE;
11627 }
11628 external_fdr_size = swap->external_fdr_size;
11629 fdr_ptr = fi->d.fdr;
11630 fraw_src = (char *) fi->d.external_fdr;
11631 fraw_end = (fraw_src
11632 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11633 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
11634 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
11635
11636 elf_tdata (abfd)->find_line_info = fi;
11637
11638 /* Note that we don't bother to ever free this information.
11639 find_nearest_line is either called all the time, as in
11640 objdump -l, so the information should be saved, or it is
11641 rarely called, as in ld error messages, so the memory
11642 wasted is unimportant. Still, it would probably be a
11643 good idea for free_cached_info to throw it away. */
11644 }
11645
11646 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11647 &fi->i, filename_ptr, functionname_ptr,
11648 line_ptr))
11649 {
11650 msec->flags = origflags;
11651 return TRUE;
11652 }
11653
11654 msec->flags = origflags;
11655 }
11656
11657 /* Fall back on the generic ELF find_nearest_line routine. */
11658
11659 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11660 filename_ptr, functionname_ptr,
11661 line_ptr);
11662 }
11663
11664 bfd_boolean
11665 _bfd_mips_elf_find_inliner_info (bfd *abfd,
11666 const char **filename_ptr,
11667 const char **functionname_ptr,
11668 unsigned int *line_ptr)
11669 {
11670 bfd_boolean found;
11671 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11672 functionname_ptr, line_ptr,
11673 & elf_tdata (abfd)->dwarf2_find_line_info);
11674 return found;
11675 }
11676
11677 \f
11678 /* When are writing out the .options or .MIPS.options section,
11679 remember the bytes we are writing out, so that we can install the
11680 GP value in the section_processing routine. */
11681
11682 bfd_boolean
11683 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11684 const void *location,
11685 file_ptr offset, bfd_size_type count)
11686 {
11687 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
11688 {
11689 bfd_byte *c;
11690
11691 if (elf_section_data (section) == NULL)
11692 {
11693 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
11694 section->used_by_bfd = bfd_zalloc (abfd, amt);
11695 if (elf_section_data (section) == NULL)
11696 return FALSE;
11697 }
11698 c = mips_elf_section_data (section)->u.tdata;
11699 if (c == NULL)
11700 {
11701 c = bfd_zalloc (abfd, section->size);
11702 if (c == NULL)
11703 return FALSE;
11704 mips_elf_section_data (section)->u.tdata = c;
11705 }
11706
11707 memcpy (c + offset, location, count);
11708 }
11709
11710 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11711 count);
11712 }
11713
11714 /* This is almost identical to bfd_generic_get_... except that some
11715 MIPS relocations need to be handled specially. Sigh. */
11716
11717 bfd_byte *
11718 _bfd_elf_mips_get_relocated_section_contents
11719 (bfd *abfd,
11720 struct bfd_link_info *link_info,
11721 struct bfd_link_order *link_order,
11722 bfd_byte *data,
11723 bfd_boolean relocatable,
11724 asymbol **symbols)
11725 {
11726 /* Get enough memory to hold the stuff */
11727 bfd *input_bfd = link_order->u.indirect.section->owner;
11728 asection *input_section = link_order->u.indirect.section;
11729 bfd_size_type sz;
11730
11731 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11732 arelent **reloc_vector = NULL;
11733 long reloc_count;
11734
11735 if (reloc_size < 0)
11736 goto error_return;
11737
11738 reloc_vector = bfd_malloc (reloc_size);
11739 if (reloc_vector == NULL && reloc_size != 0)
11740 goto error_return;
11741
11742 /* read in the section */
11743 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11744 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
11745 goto error_return;
11746
11747 reloc_count = bfd_canonicalize_reloc (input_bfd,
11748 input_section,
11749 reloc_vector,
11750 symbols);
11751 if (reloc_count < 0)
11752 goto error_return;
11753
11754 if (reloc_count > 0)
11755 {
11756 arelent **parent;
11757 /* for mips */
11758 int gp_found;
11759 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11760
11761 {
11762 struct bfd_hash_entry *h;
11763 struct bfd_link_hash_entry *lh;
11764 /* Skip all this stuff if we aren't mixing formats. */
11765 if (abfd && input_bfd
11766 && abfd->xvec == input_bfd->xvec)
11767 lh = 0;
11768 else
11769 {
11770 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
11771 lh = (struct bfd_link_hash_entry *) h;
11772 }
11773 lookup:
11774 if (lh)
11775 {
11776 switch (lh->type)
11777 {
11778 case bfd_link_hash_undefined:
11779 case bfd_link_hash_undefweak:
11780 case bfd_link_hash_common:
11781 gp_found = 0;
11782 break;
11783 case bfd_link_hash_defined:
11784 case bfd_link_hash_defweak:
11785 gp_found = 1;
11786 gp = lh->u.def.value;
11787 break;
11788 case bfd_link_hash_indirect:
11789 case bfd_link_hash_warning:
11790 lh = lh->u.i.link;
11791 /* @@FIXME ignoring warning for now */
11792 goto lookup;
11793 case bfd_link_hash_new:
11794 default:
11795 abort ();
11796 }
11797 }
11798 else
11799 gp_found = 0;
11800 }
11801 /* end mips */
11802 for (parent = reloc_vector; *parent != NULL; parent++)
11803 {
11804 char *error_message = NULL;
11805 bfd_reloc_status_type r;
11806
11807 /* Specific to MIPS: Deal with relocation types that require
11808 knowing the gp of the output bfd. */
11809 asymbol *sym = *(*parent)->sym_ptr_ptr;
11810
11811 /* If we've managed to find the gp and have a special
11812 function for the relocation then go ahead, else default
11813 to the generic handling. */
11814 if (gp_found
11815 && (*parent)->howto->special_function
11816 == _bfd_mips_elf32_gprel16_reloc)
11817 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11818 input_section, relocatable,
11819 data, gp);
11820 else
11821 r = bfd_perform_relocation (input_bfd, *parent, data,
11822 input_section,
11823 relocatable ? abfd : NULL,
11824 &error_message);
11825
11826 if (relocatable)
11827 {
11828 asection *os = input_section->output_section;
11829
11830 /* A partial link, so keep the relocs */
11831 os->orelocation[os->reloc_count] = *parent;
11832 os->reloc_count++;
11833 }
11834
11835 if (r != bfd_reloc_ok)
11836 {
11837 switch (r)
11838 {
11839 case bfd_reloc_undefined:
11840 if (!((*link_info->callbacks->undefined_symbol)
11841 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11842 input_bfd, input_section, (*parent)->address, TRUE)))
11843 goto error_return;
11844 break;
11845 case bfd_reloc_dangerous:
11846 BFD_ASSERT (error_message != NULL);
11847 if (!((*link_info->callbacks->reloc_dangerous)
11848 (link_info, error_message, input_bfd, input_section,
11849 (*parent)->address)))
11850 goto error_return;
11851 break;
11852 case bfd_reloc_overflow:
11853 if (!((*link_info->callbacks->reloc_overflow)
11854 (link_info, NULL,
11855 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11856 (*parent)->howto->name, (*parent)->addend,
11857 input_bfd, input_section, (*parent)->address)))
11858 goto error_return;
11859 break;
11860 case bfd_reloc_outofrange:
11861 default:
11862 abort ();
11863 break;
11864 }
11865
11866 }
11867 }
11868 }
11869 if (reloc_vector != NULL)
11870 free (reloc_vector);
11871 return data;
11872
11873 error_return:
11874 if (reloc_vector != NULL)
11875 free (reloc_vector);
11876 return NULL;
11877 }
11878 \f
11879 static bfd_boolean
11880 mips_elf_relax_delete_bytes (bfd *abfd,
11881 asection *sec, bfd_vma addr, int count)
11882 {
11883 Elf_Internal_Shdr *symtab_hdr;
11884 unsigned int sec_shndx;
11885 bfd_byte *contents;
11886 Elf_Internal_Rela *irel, *irelend;
11887 Elf_Internal_Sym *isym;
11888 Elf_Internal_Sym *isymend;
11889 struct elf_link_hash_entry **sym_hashes;
11890 struct elf_link_hash_entry **end_hashes;
11891 struct elf_link_hash_entry **start_hashes;
11892 unsigned int symcount;
11893
11894 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
11895 contents = elf_section_data (sec)->this_hdr.contents;
11896
11897 irel = elf_section_data (sec)->relocs;
11898 irelend = irel + sec->reloc_count;
11899
11900 /* Actually delete the bytes. */
11901 memmove (contents + addr, contents + addr + count,
11902 (size_t) (sec->size - addr - count));
11903 sec->size -= count;
11904
11905 /* Adjust all the relocs. */
11906 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
11907 {
11908 /* Get the new reloc address. */
11909 if (irel->r_offset > addr)
11910 irel->r_offset -= count;
11911 }
11912
11913 BFD_ASSERT (addr % 2 == 0);
11914 BFD_ASSERT (count % 2 == 0);
11915
11916 /* Adjust the local symbols defined in this section. */
11917 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11918 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
11919 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
11920 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
11921 isym->st_value -= count;
11922
11923 /* Now adjust the global symbols defined in this section. */
11924 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
11925 - symtab_hdr->sh_info);
11926 sym_hashes = start_hashes = elf_sym_hashes (abfd);
11927 end_hashes = sym_hashes + symcount;
11928
11929 for (; sym_hashes < end_hashes; sym_hashes++)
11930 {
11931 struct elf_link_hash_entry *sym_hash = *sym_hashes;
11932
11933 if ((sym_hash->root.type == bfd_link_hash_defined
11934 || sym_hash->root.type == bfd_link_hash_defweak)
11935 && sym_hash->root.u.def.section == sec)
11936 {
11937 bfd_vma value = sym_hash->root.u.def.value;
11938
11939 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
11940 value &= MINUS_TWO;
11941 if (value > addr)
11942 sym_hash->root.u.def.value -= count;
11943 }
11944 }
11945
11946 return TRUE;
11947 }
11948
11949
11950 /* Opcodes needed for microMIPS relaxation as found in
11951 opcodes/micromips-opc.c. */
11952
11953 struct opcode_descriptor {
11954 unsigned long match;
11955 unsigned long mask;
11956 };
11957
11958 /* The $ra register aka $31. */
11959
11960 #define RA 31
11961
11962 /* 32-bit instruction format register fields. */
11963
11964 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
11965 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
11966
11967 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
11968
11969 #define OP16_VALID_REG(r) \
11970 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
11971
11972
11973 /* 32-bit and 16-bit branches. */
11974
11975 static const struct opcode_descriptor b_insns_32[] = {
11976 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
11977 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
11978 { 0, 0 } /* End marker for find_match(). */
11979 };
11980
11981 static const struct opcode_descriptor bc_insn_32 =
11982 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
11983
11984 static const struct opcode_descriptor bz_insn_32 =
11985 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
11986
11987 static const struct opcode_descriptor bzal_insn_32 =
11988 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
11989
11990 static const struct opcode_descriptor beq_insn_32 =
11991 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
11992
11993 static const struct opcode_descriptor b_insn_16 =
11994 { /* "b", "mD", */ 0xcc00, 0xfc00 };
11995
11996 static const struct opcode_descriptor bz_insn_16 =
11997 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
11998
11999
12000 /* 32-bit and 16-bit branch EQ and NE zero. */
12001
12002 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12003 eq and second the ne. This convention is used when replacing a
12004 32-bit BEQ/BNE with the 16-bit version. */
12005
12006 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12007
12008 static const struct opcode_descriptor bz_rs_insns_32[] = {
12009 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12010 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12011 { 0, 0 } /* End marker for find_match(). */
12012 };
12013
12014 static const struct opcode_descriptor bz_rt_insns_32[] = {
12015 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12016 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12017 { 0, 0 } /* End marker for find_match(). */
12018 };
12019
12020 static const struct opcode_descriptor bzc_insns_32[] = {
12021 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12022 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12023 { 0, 0 } /* End marker for find_match(). */
12024 };
12025
12026 static const struct opcode_descriptor bz_insns_16[] = {
12027 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12028 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12029 { 0, 0 } /* End marker for find_match(). */
12030 };
12031
12032 /* Switch between a 5-bit register index and its 3-bit shorthand. */
12033
12034 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12035 #define BZ16_REG_FIELD(r) \
12036 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12037
12038
12039 /* 32-bit instructions with a delay slot. */
12040
12041 static const struct opcode_descriptor jal_insn_32_bd16 =
12042 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12043
12044 static const struct opcode_descriptor jal_insn_32_bd32 =
12045 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12046
12047 static const struct opcode_descriptor jal_x_insn_32_bd32 =
12048 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12049
12050 static const struct opcode_descriptor j_insn_32 =
12051 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12052
12053 static const struct opcode_descriptor jalr_insn_32 =
12054 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12055
12056 /* This table can be compacted, because no opcode replacement is made. */
12057
12058 static const struct opcode_descriptor ds_insns_32_bd16[] = {
12059 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12060
12061 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12062 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12063
12064 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12065 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12066 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12067 { 0, 0 } /* End marker for find_match(). */
12068 };
12069
12070 /* This table can be compacted, because no opcode replacement is made. */
12071
12072 static const struct opcode_descriptor ds_insns_32_bd32[] = {
12073 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12074
12075 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12076 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12077 { 0, 0 } /* End marker for find_match(). */
12078 };
12079
12080
12081 /* 16-bit instructions with a delay slot. */
12082
12083 static const struct opcode_descriptor jalr_insn_16_bd16 =
12084 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12085
12086 static const struct opcode_descriptor jalr_insn_16_bd32 =
12087 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12088
12089 static const struct opcode_descriptor jr_insn_16 =
12090 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12091
12092 #define JR16_REG(opcode) ((opcode) & 0x1f)
12093
12094 /* This table can be compacted, because no opcode replacement is made. */
12095
12096 static const struct opcode_descriptor ds_insns_16_bd16[] = {
12097 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12098
12099 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12100 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12101 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12102 { 0, 0 } /* End marker for find_match(). */
12103 };
12104
12105
12106 /* LUI instruction. */
12107
12108 static const struct opcode_descriptor lui_insn =
12109 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12110
12111
12112 /* ADDIU instruction. */
12113
12114 static const struct opcode_descriptor addiu_insn =
12115 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12116
12117 static const struct opcode_descriptor addiupc_insn =
12118 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12119
12120 #define ADDIUPC_REG_FIELD(r) \
12121 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12122
12123
12124 /* Relaxable instructions in a JAL delay slot: MOVE. */
12125
12126 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12127 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12128 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12129 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12130
12131 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12132 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12133
12134 static const struct opcode_descriptor move_insns_32[] = {
12135 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12136 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12137 { 0, 0 } /* End marker for find_match(). */
12138 };
12139
12140 static const struct opcode_descriptor move_insn_16 =
12141 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12142
12143
12144 /* NOP instructions. */
12145
12146 static const struct opcode_descriptor nop_insn_32 =
12147 { /* "nop", "", */ 0x00000000, 0xffffffff };
12148
12149 static const struct opcode_descriptor nop_insn_16 =
12150 { /* "nop", "", */ 0x0c00, 0xffff };
12151
12152
12153 /* Instruction match support. */
12154
12155 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12156
12157 static int
12158 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12159 {
12160 unsigned long indx;
12161
12162 for (indx = 0; insn[indx].mask != 0; indx++)
12163 if (MATCH (opcode, insn[indx]))
12164 return indx;
12165
12166 return -1;
12167 }
12168
12169
12170 /* Branch and delay slot decoding support. */
12171
12172 /* If PTR points to what *might* be a 16-bit branch or jump, then
12173 return the minimum length of its delay slot, otherwise return 0.
12174 Non-zero results are not definitive as we might be checking against
12175 the second half of another instruction. */
12176
12177 static int
12178 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12179 {
12180 unsigned long opcode;
12181 int bdsize;
12182
12183 opcode = bfd_get_16 (abfd, ptr);
12184 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12185 /* 16-bit branch/jump with a 32-bit delay slot. */
12186 bdsize = 4;
12187 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12188 || find_match (opcode, ds_insns_16_bd16) >= 0)
12189 /* 16-bit branch/jump with a 16-bit delay slot. */
12190 bdsize = 2;
12191 else
12192 /* No delay slot. */
12193 bdsize = 0;
12194
12195 return bdsize;
12196 }
12197
12198 /* If PTR points to what *might* be a 32-bit branch or jump, then
12199 return the minimum length of its delay slot, otherwise return 0.
12200 Non-zero results are not definitive as we might be checking against
12201 the second half of another instruction. */
12202
12203 static int
12204 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12205 {
12206 unsigned long opcode;
12207 int bdsize;
12208
12209 opcode = (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
12210 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12211 /* 32-bit branch/jump with a 32-bit delay slot. */
12212 bdsize = 4;
12213 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12214 /* 32-bit branch/jump with a 16-bit delay slot. */
12215 bdsize = 2;
12216 else
12217 /* No delay slot. */
12218 bdsize = 0;
12219
12220 return bdsize;
12221 }
12222
12223 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12224 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12225
12226 static bfd_boolean
12227 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12228 {
12229 unsigned long opcode;
12230
12231 opcode = bfd_get_16 (abfd, ptr);
12232 if (MATCH (opcode, b_insn_16)
12233 /* B16 */
12234 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12235 /* JR16 */
12236 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12237 /* BEQZ16, BNEZ16 */
12238 || (MATCH (opcode, jalr_insn_16_bd32)
12239 /* JALR16 */
12240 && reg != JR16_REG (opcode) && reg != RA))
12241 return TRUE;
12242
12243 return FALSE;
12244 }
12245
12246 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12247 then return TRUE, otherwise FALSE. */
12248
12249 static bfd_boolean
12250 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12251 {
12252 unsigned long opcode;
12253
12254 opcode = (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
12255 if (MATCH (opcode, j_insn_32)
12256 /* J */
12257 || MATCH (opcode, bc_insn_32)
12258 /* BC1F, BC1T, BC2F, BC2T */
12259 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
12260 /* JAL, JALX */
12261 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
12262 /* BGEZ, BGTZ, BLEZ, BLTZ */
12263 || (MATCH (opcode, bzal_insn_32)
12264 /* BGEZAL, BLTZAL */
12265 && reg != OP32_SREG (opcode) && reg != RA)
12266 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
12267 /* JALR, JALR.HB, BEQ, BNE */
12268 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
12269 return TRUE;
12270
12271 return FALSE;
12272 }
12273
12274 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
12275 IRELEND) at OFFSET indicate that there must be a compact branch there,
12276 then return TRUE, otherwise FALSE. */
12277
12278 static bfd_boolean
12279 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
12280 const Elf_Internal_Rela *internal_relocs,
12281 const Elf_Internal_Rela *irelend)
12282 {
12283 const Elf_Internal_Rela *irel;
12284 unsigned long opcode;
12285
12286 opcode = bfd_get_16 (abfd, ptr);
12287 opcode <<= 16;
12288 opcode |= bfd_get_16 (abfd, ptr + 2);
12289 if (find_match (opcode, bzc_insns_32) < 0)
12290 return FALSE;
12291
12292 for (irel = internal_relocs; irel < irelend; irel++)
12293 if (irel->r_offset == offset
12294 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
12295 return TRUE;
12296
12297 return FALSE;
12298 }
12299
12300 /* Bitsize checking. */
12301 #define IS_BITSIZE(val, N) \
12302 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
12303 - (1ULL << ((N) - 1))) == (val))
12304
12305 \f
12306 bfd_boolean
12307 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
12308 struct bfd_link_info *link_info,
12309 bfd_boolean *again)
12310 {
12311 Elf_Internal_Shdr *symtab_hdr;
12312 Elf_Internal_Rela *internal_relocs;
12313 Elf_Internal_Rela *irel, *irelend;
12314 bfd_byte *contents = NULL;
12315 Elf_Internal_Sym *isymbuf = NULL;
12316
12317 /* Assume nothing changes. */
12318 *again = FALSE;
12319
12320 /* We don't have to do anything for a relocatable link, if
12321 this section does not have relocs, or if this is not a
12322 code section. */
12323
12324 if (link_info->relocatable
12325 || (sec->flags & SEC_RELOC) == 0
12326 || sec->reloc_count == 0
12327 || (sec->flags & SEC_CODE) == 0)
12328 return TRUE;
12329
12330 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12331
12332 /* Get a copy of the native relocations. */
12333 internal_relocs = (_bfd_elf_link_read_relocs
12334 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
12335 link_info->keep_memory));
12336 if (internal_relocs == NULL)
12337 goto error_return;
12338
12339 /* Walk through them looking for relaxing opportunities. */
12340 irelend = internal_relocs + sec->reloc_count;
12341 for (irel = internal_relocs; irel < irelend; irel++)
12342 {
12343 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
12344 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
12345 bfd_boolean target_is_micromips_code_p;
12346 unsigned long opcode;
12347 bfd_vma symval;
12348 bfd_vma pcrval;
12349 bfd_byte *ptr;
12350 int fndopc;
12351
12352 /* The number of bytes to delete for relaxation and from where
12353 to delete these bytes starting at irel->r_offset. */
12354 int delcnt = 0;
12355 int deloff = 0;
12356
12357 /* If this isn't something that can be relaxed, then ignore
12358 this reloc. */
12359 if (r_type != R_MICROMIPS_HI16
12360 && r_type != R_MICROMIPS_PC16_S1
12361 && r_type != R_MICROMIPS_26_S1)
12362 continue;
12363
12364 /* Get the section contents if we haven't done so already. */
12365 if (contents == NULL)
12366 {
12367 /* Get cached copy if it exists. */
12368 if (elf_section_data (sec)->this_hdr.contents != NULL)
12369 contents = elf_section_data (sec)->this_hdr.contents;
12370 /* Go get them off disk. */
12371 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
12372 goto error_return;
12373 }
12374 ptr = contents + irel->r_offset;
12375
12376 /* Read this BFD's local symbols if we haven't done so already. */
12377 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
12378 {
12379 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
12380 if (isymbuf == NULL)
12381 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12382 symtab_hdr->sh_info, 0,
12383 NULL, NULL, NULL);
12384 if (isymbuf == NULL)
12385 goto error_return;
12386 }
12387
12388 /* Get the value of the symbol referred to by the reloc. */
12389 if (r_symndx < symtab_hdr->sh_info)
12390 {
12391 /* A local symbol. */
12392 Elf_Internal_Sym *isym;
12393 asection *sym_sec;
12394
12395 isym = isymbuf + r_symndx;
12396 if (isym->st_shndx == SHN_UNDEF)
12397 sym_sec = bfd_und_section_ptr;
12398 else if (isym->st_shndx == SHN_ABS)
12399 sym_sec = bfd_abs_section_ptr;
12400 else if (isym->st_shndx == SHN_COMMON)
12401 sym_sec = bfd_com_section_ptr;
12402 else
12403 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
12404 symval = (isym->st_value
12405 + sym_sec->output_section->vma
12406 + sym_sec->output_offset);
12407 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
12408 }
12409 else
12410 {
12411 unsigned long indx;
12412 struct elf_link_hash_entry *h;
12413
12414 /* An external symbol. */
12415 indx = r_symndx - symtab_hdr->sh_info;
12416 h = elf_sym_hashes (abfd)[indx];
12417 BFD_ASSERT (h != NULL);
12418
12419 if (h->root.type != bfd_link_hash_defined
12420 && h->root.type != bfd_link_hash_defweak)
12421 /* This appears to be a reference to an undefined
12422 symbol. Just ignore it -- it will be caught by the
12423 regular reloc processing. */
12424 continue;
12425
12426 symval = (h->root.u.def.value
12427 + h->root.u.def.section->output_section->vma
12428 + h->root.u.def.section->output_offset);
12429 target_is_micromips_code_p = (!h->needs_plt
12430 && ELF_ST_IS_MICROMIPS (h->other));
12431 }
12432
12433
12434 /* For simplicity of coding, we are going to modify the
12435 section contents, the section relocs, and the BFD symbol
12436 table. We must tell the rest of the code not to free up this
12437 information. It would be possible to instead create a table
12438 of changes which have to be made, as is done in coff-mips.c;
12439 that would be more work, but would require less memory when
12440 the linker is run. */
12441
12442 /* Only 32-bit instructions relaxed. */
12443 if (irel->r_offset + 4 > sec->size)
12444 continue;
12445
12446 opcode = bfd_get_16 (abfd, ptr ) << 16;
12447 opcode |= bfd_get_16 (abfd, ptr + 2);
12448
12449 /* This is the pc-relative distance from the instruction the
12450 relocation is applied to, to the symbol referred. */
12451 pcrval = (symval
12452 - (sec->output_section->vma + sec->output_offset)
12453 - irel->r_offset);
12454
12455 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
12456 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
12457 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
12458
12459 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
12460
12461 where pcrval has first to be adjusted to apply against the LO16
12462 location (we make the adjustment later on, when we have figured
12463 out the offset). */
12464 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
12465 {
12466 bfd_boolean bzc = FALSE;
12467 unsigned long nextopc;
12468 unsigned long reg;
12469 bfd_vma offset;
12470
12471 /* Give up if the previous reloc was a HI16 against this symbol
12472 too. */
12473 if (irel > internal_relocs
12474 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
12475 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
12476 continue;
12477
12478 /* Or if the next reloc is not a LO16 against this symbol. */
12479 if (irel + 1 >= irelend
12480 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
12481 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
12482 continue;
12483
12484 /* Or if the second next reloc is a LO16 against this symbol too. */
12485 if (irel + 2 >= irelend
12486 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
12487 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
12488 continue;
12489
12490 /* See if the LUI instruction *might* be in a branch delay slot.
12491 We check whether what looks like a 16-bit branch or jump is
12492 actually an immediate argument to a compact branch, and let
12493 it through if so. */
12494 if (irel->r_offset >= 2
12495 && check_br16_dslot (abfd, ptr - 2)
12496 && !(irel->r_offset >= 4
12497 && (bzc = check_relocated_bzc (abfd,
12498 ptr - 4, irel->r_offset - 4,
12499 internal_relocs, irelend))))
12500 continue;
12501 if (irel->r_offset >= 4
12502 && !bzc
12503 && check_br32_dslot (abfd, ptr - 4))
12504 continue;
12505
12506 reg = OP32_SREG (opcode);
12507
12508 /* We only relax adjacent instructions or ones separated with
12509 a branch or jump that has a delay slot. The branch or jump
12510 must not fiddle with the register used to hold the address.
12511 Subtract 4 for the LUI itself. */
12512 offset = irel[1].r_offset - irel[0].r_offset;
12513 switch (offset - 4)
12514 {
12515 case 0:
12516 break;
12517 case 2:
12518 if (check_br16 (abfd, ptr + 4, reg))
12519 break;
12520 continue;
12521 case 4:
12522 if (check_br32 (abfd, ptr + 4, reg))
12523 break;
12524 continue;
12525 default:
12526 continue;
12527 }
12528
12529 nextopc = bfd_get_16 (abfd, contents + irel[1].r_offset ) << 16;
12530 nextopc |= bfd_get_16 (abfd, contents + irel[1].r_offset + 2);
12531
12532 /* Give up unless the same register is used with both
12533 relocations. */
12534 if (OP32_SREG (nextopc) != reg)
12535 continue;
12536
12537 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
12538 and rounding up to take masking of the two LSBs into account. */
12539 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
12540
12541 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
12542 if (IS_BITSIZE (symval, 16))
12543 {
12544 /* Fix the relocation's type. */
12545 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
12546
12547 /* Instructions using R_MICROMIPS_LO16 have the base or
12548 source register in bits 20:16. This register becomes $0
12549 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
12550 nextopc &= ~0x001f0000;
12551 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
12552 contents + irel[1].r_offset);
12553 }
12554
12555 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
12556 We add 4 to take LUI deletion into account while checking
12557 the PC-relative distance. */
12558 else if (symval % 4 == 0
12559 && IS_BITSIZE (pcrval + 4, 25)
12560 && MATCH (nextopc, addiu_insn)
12561 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
12562 && OP16_VALID_REG (OP32_TREG (nextopc)))
12563 {
12564 /* Fix the relocation's type. */
12565 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
12566
12567 /* Replace ADDIU with the ADDIUPC version. */
12568 nextopc = (addiupc_insn.match
12569 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
12570
12571 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
12572 contents + irel[1].r_offset);
12573 bfd_put_16 (abfd, nextopc & 0xffff,
12574 contents + irel[1].r_offset + 2);
12575 }
12576
12577 /* Can't do anything, give up, sigh... */
12578 else
12579 continue;
12580
12581 /* Fix the relocation's type. */
12582 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
12583
12584 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
12585 delcnt = 4;
12586 deloff = 0;
12587 }
12588
12589 /* Compact branch relaxation -- due to the multitude of macros
12590 employed by the compiler/assembler, compact branches are not
12591 always generated. Obviously, this can/will be fixed elsewhere,
12592 but there is no drawback in double checking it here. */
12593 else if (r_type == R_MICROMIPS_PC16_S1
12594 && irel->r_offset + 5 < sec->size
12595 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
12596 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
12597 && MATCH (bfd_get_16 (abfd, ptr + 4), nop_insn_16))
12598 {
12599 unsigned long reg;
12600
12601 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
12602
12603 /* Replace BEQZ/BNEZ with the compact version. */
12604 opcode = (bzc_insns_32[fndopc].match
12605 | BZC32_REG_FIELD (reg)
12606 | (opcode & 0xffff)); /* Addend value. */
12607
12608 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
12609 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
12610
12611 /* Delete the 16-bit delay slot NOP: two bytes from
12612 irel->offset + 4. */
12613 delcnt = 2;
12614 deloff = 4;
12615 }
12616
12617 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
12618 to check the distance from the next instruction, so subtract 2. */
12619 else if (r_type == R_MICROMIPS_PC16_S1
12620 && IS_BITSIZE (pcrval - 2, 11)
12621 && find_match (opcode, b_insns_32) >= 0)
12622 {
12623 /* Fix the relocation's type. */
12624 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
12625
12626 /* Replace the the 32-bit opcode with a 16-bit opcode. */
12627 bfd_put_16 (abfd,
12628 (b_insn_16.match
12629 | (opcode & 0x3ff)), /* Addend value. */
12630 ptr);
12631
12632 /* Delete 2 bytes from irel->r_offset + 2. */
12633 delcnt = 2;
12634 deloff = 2;
12635 }
12636
12637 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
12638 to check the distance from the next instruction, so subtract 2. */
12639 else if (r_type == R_MICROMIPS_PC16_S1
12640 && IS_BITSIZE (pcrval - 2, 8)
12641 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
12642 && OP16_VALID_REG (OP32_SREG (opcode)))
12643 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
12644 && OP16_VALID_REG (OP32_TREG (opcode)))))
12645 {
12646 unsigned long reg;
12647
12648 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
12649
12650 /* Fix the relocation's type. */
12651 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
12652
12653 /* Replace the the 32-bit opcode with a 16-bit opcode. */
12654 bfd_put_16 (abfd,
12655 (bz_insns_16[fndopc].match
12656 | BZ16_REG_FIELD (reg)
12657 | (opcode & 0x7f)), /* Addend value. */
12658 ptr);
12659
12660 /* Delete 2 bytes from irel->r_offset + 2. */
12661 delcnt = 2;
12662 deloff = 2;
12663 }
12664
12665 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
12666 else if (r_type == R_MICROMIPS_26_S1
12667 && target_is_micromips_code_p
12668 && irel->r_offset + 7 < sec->size
12669 && MATCH (opcode, jal_insn_32_bd32))
12670 {
12671 unsigned long n32opc;
12672 bfd_boolean relaxed = FALSE;
12673
12674 n32opc = bfd_get_16 (abfd, ptr + 4) << 16;
12675 n32opc |= bfd_get_16 (abfd, ptr + 6);
12676
12677 if (MATCH (n32opc, nop_insn_32))
12678 {
12679 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
12680 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
12681
12682 relaxed = TRUE;
12683 }
12684 else if (find_match (n32opc, move_insns_32) >= 0)
12685 {
12686 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
12687 bfd_put_16 (abfd,
12688 (move_insn_16.match
12689 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
12690 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
12691 ptr + 4);
12692
12693 relaxed = TRUE;
12694 }
12695 /* Other 32-bit instructions relaxable to 16-bit
12696 instructions will be handled here later. */
12697
12698 if (relaxed)
12699 {
12700 /* JAL with 32-bit delay slot that is changed to a JALS
12701 with 16-bit delay slot. */
12702 bfd_put_16 (abfd, (jal_insn_32_bd16.match >> 16) & 0xffff,
12703 ptr);
12704 bfd_put_16 (abfd, jal_insn_32_bd16.match & 0xffff,
12705 ptr + 2);
12706
12707 /* Delete 2 bytes from irel->r_offset + 6. */
12708 delcnt = 2;
12709 deloff = 6;
12710 }
12711 }
12712
12713 if (delcnt != 0)
12714 {
12715 /* Note that we've changed the relocs, section contents, etc. */
12716 elf_section_data (sec)->relocs = internal_relocs;
12717 elf_section_data (sec)->this_hdr.contents = contents;
12718 symtab_hdr->contents = (unsigned char *) isymbuf;
12719
12720 /* Delete bytes depending on the delcnt and deloff. */
12721 if (!mips_elf_relax_delete_bytes (abfd, sec,
12722 irel->r_offset + deloff, delcnt))
12723 goto error_return;
12724
12725 /* That will change things, so we should relax again.
12726 Note that this is not required, and it may be slow. */
12727 *again = TRUE;
12728 }
12729 }
12730
12731 if (isymbuf != NULL
12732 && symtab_hdr->contents != (unsigned char *) isymbuf)
12733 {
12734 if (! link_info->keep_memory)
12735 free (isymbuf);
12736 else
12737 {
12738 /* Cache the symbols for elf_link_input_bfd. */
12739 symtab_hdr->contents = (unsigned char *) isymbuf;
12740 }
12741 }
12742
12743 if (contents != NULL
12744 && elf_section_data (sec)->this_hdr.contents != contents)
12745 {
12746 if (! link_info->keep_memory)
12747 free (contents);
12748 else
12749 {
12750 /* Cache the section contents for elf_link_input_bfd. */
12751 elf_section_data (sec)->this_hdr.contents = contents;
12752 }
12753 }
12754
12755 if (internal_relocs != NULL
12756 && elf_section_data (sec)->relocs != internal_relocs)
12757 free (internal_relocs);
12758
12759 return TRUE;
12760
12761 error_return:
12762 if (isymbuf != NULL
12763 && symtab_hdr->contents != (unsigned char *) isymbuf)
12764 free (isymbuf);
12765 if (contents != NULL
12766 && elf_section_data (sec)->this_hdr.contents != contents)
12767 free (contents);
12768 if (internal_relocs != NULL
12769 && elf_section_data (sec)->relocs != internal_relocs)
12770 free (internal_relocs);
12771
12772 return FALSE;
12773 }
12774 \f
12775 /* Create a MIPS ELF linker hash table. */
12776
12777 struct bfd_link_hash_table *
12778 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
12779 {
12780 struct mips_elf_link_hash_table *ret;
12781 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
12782
12783 ret = bfd_malloc (amt);
12784 if (ret == NULL)
12785 return NULL;
12786
12787 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
12788 mips_elf_link_hash_newfunc,
12789 sizeof (struct mips_elf_link_hash_entry),
12790 MIPS_ELF_DATA))
12791 {
12792 free (ret);
12793 return NULL;
12794 }
12795
12796 #if 0
12797 /* We no longer use this. */
12798 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
12799 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
12800 #endif
12801 ret->procedure_count = 0;
12802 ret->compact_rel_size = 0;
12803 ret->use_rld_obj_head = FALSE;
12804 ret->rld_value = 0;
12805 ret->mips16_stubs_seen = FALSE;
12806 ret->use_plts_and_copy_relocs = FALSE;
12807 ret->is_vxworks = FALSE;
12808 ret->small_data_overflow_reported = FALSE;
12809 ret->srelbss = NULL;
12810 ret->sdynbss = NULL;
12811 ret->srelplt = NULL;
12812 ret->srelplt2 = NULL;
12813 ret->sgotplt = NULL;
12814 ret->splt = NULL;
12815 ret->sstubs = NULL;
12816 ret->sgot = NULL;
12817 ret->got_info = NULL;
12818 ret->plt_header_size = 0;
12819 ret->plt_entry_size = 0;
12820 ret->lazy_stub_count = 0;
12821 ret->function_stub_size = 0;
12822 ret->strampoline = NULL;
12823 ret->la25_stubs = NULL;
12824 ret->add_stub_section = NULL;
12825
12826 return &ret->root.root;
12827 }
12828
12829 /* Likewise, but indicate that the target is VxWorks. */
12830
12831 struct bfd_link_hash_table *
12832 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
12833 {
12834 struct bfd_link_hash_table *ret;
12835
12836 ret = _bfd_mips_elf_link_hash_table_create (abfd);
12837 if (ret)
12838 {
12839 struct mips_elf_link_hash_table *htab;
12840
12841 htab = (struct mips_elf_link_hash_table *) ret;
12842 htab->use_plts_and_copy_relocs = TRUE;
12843 htab->is_vxworks = TRUE;
12844 }
12845 return ret;
12846 }
12847
12848 /* A function that the linker calls if we are allowed to use PLTs
12849 and copy relocs. */
12850
12851 void
12852 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
12853 {
12854 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
12855 }
12856 \f
12857 /* We need to use a special link routine to handle the .reginfo and
12858 the .mdebug sections. We need to merge all instances of these
12859 sections together, not write them all out sequentially. */
12860
12861 bfd_boolean
12862 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
12863 {
12864 asection *o;
12865 struct bfd_link_order *p;
12866 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
12867 asection *rtproc_sec;
12868 Elf32_RegInfo reginfo;
12869 struct ecoff_debug_info debug;
12870 struct mips_htab_traverse_info hti;
12871 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12872 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
12873 HDRR *symhdr = &debug.symbolic_header;
12874 void *mdebug_handle = NULL;
12875 asection *s;
12876 EXTR esym;
12877 unsigned int i;
12878 bfd_size_type amt;
12879 struct mips_elf_link_hash_table *htab;
12880
12881 static const char * const secname[] =
12882 {
12883 ".text", ".init", ".fini", ".data",
12884 ".rodata", ".sdata", ".sbss", ".bss"
12885 };
12886 static const int sc[] =
12887 {
12888 scText, scInit, scFini, scData,
12889 scRData, scSData, scSBss, scBss
12890 };
12891
12892 /* Sort the dynamic symbols so that those with GOT entries come after
12893 those without. */
12894 htab = mips_elf_hash_table (info);
12895 BFD_ASSERT (htab != NULL);
12896
12897 if (!mips_elf_sort_hash_table (abfd, info))
12898 return FALSE;
12899
12900 /* Create any scheduled LA25 stubs. */
12901 hti.info = info;
12902 hti.output_bfd = abfd;
12903 hti.error = FALSE;
12904 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
12905 if (hti.error)
12906 return FALSE;
12907
12908 /* Get a value for the GP register. */
12909 if (elf_gp (abfd) == 0)
12910 {
12911 struct bfd_link_hash_entry *h;
12912
12913 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
12914 if (h != NULL && h->type == bfd_link_hash_defined)
12915 elf_gp (abfd) = (h->u.def.value
12916 + h->u.def.section->output_section->vma
12917 + h->u.def.section->output_offset);
12918 else if (htab->is_vxworks
12919 && (h = bfd_link_hash_lookup (info->hash,
12920 "_GLOBAL_OFFSET_TABLE_",
12921 FALSE, FALSE, TRUE))
12922 && h->type == bfd_link_hash_defined)
12923 elf_gp (abfd) = (h->u.def.section->output_section->vma
12924 + h->u.def.section->output_offset
12925 + h->u.def.value);
12926 else if (info->relocatable)
12927 {
12928 bfd_vma lo = MINUS_ONE;
12929
12930 /* Find the GP-relative section with the lowest offset. */
12931 for (o = abfd->sections; o != NULL; o = o->next)
12932 if (o->vma < lo
12933 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
12934 lo = o->vma;
12935
12936 /* And calculate GP relative to that. */
12937 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
12938 }
12939 else
12940 {
12941 /* If the relocate_section function needs to do a reloc
12942 involving the GP value, it should make a reloc_dangerous
12943 callback to warn that GP is not defined. */
12944 }
12945 }
12946
12947 /* Go through the sections and collect the .reginfo and .mdebug
12948 information. */
12949 reginfo_sec = NULL;
12950 mdebug_sec = NULL;
12951 gptab_data_sec = NULL;
12952 gptab_bss_sec = NULL;
12953 for (o = abfd->sections; o != NULL; o = o->next)
12954 {
12955 if (strcmp (o->name, ".reginfo") == 0)
12956 {
12957 memset (&reginfo, 0, sizeof reginfo);
12958
12959 /* We have found the .reginfo section in the output file.
12960 Look through all the link_orders comprising it and merge
12961 the information together. */
12962 for (p = o->map_head.link_order; p != NULL; p = p->next)
12963 {
12964 asection *input_section;
12965 bfd *input_bfd;
12966 Elf32_External_RegInfo ext;
12967 Elf32_RegInfo sub;
12968
12969 if (p->type != bfd_indirect_link_order)
12970 {
12971 if (p->type == bfd_data_link_order)
12972 continue;
12973 abort ();
12974 }
12975
12976 input_section = p->u.indirect.section;
12977 input_bfd = input_section->owner;
12978
12979 if (! bfd_get_section_contents (input_bfd, input_section,
12980 &ext, 0, sizeof ext))
12981 return FALSE;
12982
12983 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
12984
12985 reginfo.ri_gprmask |= sub.ri_gprmask;
12986 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
12987 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
12988 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
12989 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
12990
12991 /* ri_gp_value is set by the function
12992 mips_elf32_section_processing when the section is
12993 finally written out. */
12994
12995 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12996 elf_link_input_bfd ignores this section. */
12997 input_section->flags &= ~SEC_HAS_CONTENTS;
12998 }
12999
13000 /* Size has been set in _bfd_mips_elf_always_size_sections. */
13001 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
13002
13003 /* Skip this section later on (I don't think this currently
13004 matters, but someday it might). */
13005 o->map_head.link_order = NULL;
13006
13007 reginfo_sec = o;
13008 }
13009
13010 if (strcmp (o->name, ".mdebug") == 0)
13011 {
13012 struct extsym_info einfo;
13013 bfd_vma last;
13014
13015 /* We have found the .mdebug section in the output file.
13016 Look through all the link_orders comprising it and merge
13017 the information together. */
13018 symhdr->magic = swap->sym_magic;
13019 /* FIXME: What should the version stamp be? */
13020 symhdr->vstamp = 0;
13021 symhdr->ilineMax = 0;
13022 symhdr->cbLine = 0;
13023 symhdr->idnMax = 0;
13024 symhdr->ipdMax = 0;
13025 symhdr->isymMax = 0;
13026 symhdr->ioptMax = 0;
13027 symhdr->iauxMax = 0;
13028 symhdr->issMax = 0;
13029 symhdr->issExtMax = 0;
13030 symhdr->ifdMax = 0;
13031 symhdr->crfd = 0;
13032 symhdr->iextMax = 0;
13033
13034 /* We accumulate the debugging information itself in the
13035 debug_info structure. */
13036 debug.line = NULL;
13037 debug.external_dnr = NULL;
13038 debug.external_pdr = NULL;
13039 debug.external_sym = NULL;
13040 debug.external_opt = NULL;
13041 debug.external_aux = NULL;
13042 debug.ss = NULL;
13043 debug.ssext = debug.ssext_end = NULL;
13044 debug.external_fdr = NULL;
13045 debug.external_rfd = NULL;
13046 debug.external_ext = debug.external_ext_end = NULL;
13047
13048 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
13049 if (mdebug_handle == NULL)
13050 return FALSE;
13051
13052 esym.jmptbl = 0;
13053 esym.cobol_main = 0;
13054 esym.weakext = 0;
13055 esym.reserved = 0;
13056 esym.ifd = ifdNil;
13057 esym.asym.iss = issNil;
13058 esym.asym.st = stLocal;
13059 esym.asym.reserved = 0;
13060 esym.asym.index = indexNil;
13061 last = 0;
13062 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13063 {
13064 esym.asym.sc = sc[i];
13065 s = bfd_get_section_by_name (abfd, secname[i]);
13066 if (s != NULL)
13067 {
13068 esym.asym.value = s->vma;
13069 last = s->vma + s->size;
13070 }
13071 else
13072 esym.asym.value = last;
13073 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13074 secname[i], &esym))
13075 return FALSE;
13076 }
13077
13078 for (p = o->map_head.link_order; p != NULL; p = p->next)
13079 {
13080 asection *input_section;
13081 bfd *input_bfd;
13082 const struct ecoff_debug_swap *input_swap;
13083 struct ecoff_debug_info input_debug;
13084 char *eraw_src;
13085 char *eraw_end;
13086
13087 if (p->type != bfd_indirect_link_order)
13088 {
13089 if (p->type == bfd_data_link_order)
13090 continue;
13091 abort ();
13092 }
13093
13094 input_section = p->u.indirect.section;
13095 input_bfd = input_section->owner;
13096
13097 if (!is_mips_elf (input_bfd))
13098 {
13099 /* I don't know what a non MIPS ELF bfd would be
13100 doing with a .mdebug section, but I don't really
13101 want to deal with it. */
13102 continue;
13103 }
13104
13105 input_swap = (get_elf_backend_data (input_bfd)
13106 ->elf_backend_ecoff_debug_swap);
13107
13108 BFD_ASSERT (p->size == input_section->size);
13109
13110 /* The ECOFF linking code expects that we have already
13111 read in the debugging information and set up an
13112 ecoff_debug_info structure, so we do that now. */
13113 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13114 &input_debug))
13115 return FALSE;
13116
13117 if (! (bfd_ecoff_debug_accumulate
13118 (mdebug_handle, abfd, &debug, swap, input_bfd,
13119 &input_debug, input_swap, info)))
13120 return FALSE;
13121
13122 /* Loop through the external symbols. For each one with
13123 interesting information, try to find the symbol in
13124 the linker global hash table and save the information
13125 for the output external symbols. */
13126 eraw_src = input_debug.external_ext;
13127 eraw_end = (eraw_src
13128 + (input_debug.symbolic_header.iextMax
13129 * input_swap->external_ext_size));
13130 for (;
13131 eraw_src < eraw_end;
13132 eraw_src += input_swap->external_ext_size)
13133 {
13134 EXTR ext;
13135 const char *name;
13136 struct mips_elf_link_hash_entry *h;
13137
13138 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
13139 if (ext.asym.sc == scNil
13140 || ext.asym.sc == scUndefined
13141 || ext.asym.sc == scSUndefined)
13142 continue;
13143
13144 name = input_debug.ssext + ext.asym.iss;
13145 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
13146 name, FALSE, FALSE, TRUE);
13147 if (h == NULL || h->esym.ifd != -2)
13148 continue;
13149
13150 if (ext.ifd != -1)
13151 {
13152 BFD_ASSERT (ext.ifd
13153 < input_debug.symbolic_header.ifdMax);
13154 ext.ifd = input_debug.ifdmap[ext.ifd];
13155 }
13156
13157 h->esym = ext;
13158 }
13159
13160 /* Free up the information we just read. */
13161 free (input_debug.line);
13162 free (input_debug.external_dnr);
13163 free (input_debug.external_pdr);
13164 free (input_debug.external_sym);
13165 free (input_debug.external_opt);
13166 free (input_debug.external_aux);
13167 free (input_debug.ss);
13168 free (input_debug.ssext);
13169 free (input_debug.external_fdr);
13170 free (input_debug.external_rfd);
13171 free (input_debug.external_ext);
13172
13173 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13174 elf_link_input_bfd ignores this section. */
13175 input_section->flags &= ~SEC_HAS_CONTENTS;
13176 }
13177
13178 if (SGI_COMPAT (abfd) && info->shared)
13179 {
13180 /* Create .rtproc section. */
13181 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
13182 if (rtproc_sec == NULL)
13183 {
13184 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13185 | SEC_LINKER_CREATED | SEC_READONLY);
13186
13187 rtproc_sec = bfd_make_section_with_flags (abfd,
13188 ".rtproc",
13189 flags);
13190 if (rtproc_sec == NULL
13191 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
13192 return FALSE;
13193 }
13194
13195 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13196 info, rtproc_sec,
13197 &debug))
13198 return FALSE;
13199 }
13200
13201 /* Build the external symbol information. */
13202 einfo.abfd = abfd;
13203 einfo.info = info;
13204 einfo.debug = &debug;
13205 einfo.swap = swap;
13206 einfo.failed = FALSE;
13207 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
13208 mips_elf_output_extsym, &einfo);
13209 if (einfo.failed)
13210 return FALSE;
13211
13212 /* Set the size of the .mdebug section. */
13213 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
13214
13215 /* Skip this section later on (I don't think this currently
13216 matters, but someday it might). */
13217 o->map_head.link_order = NULL;
13218
13219 mdebug_sec = o;
13220 }
13221
13222 if (CONST_STRNEQ (o->name, ".gptab."))
13223 {
13224 const char *subname;
13225 unsigned int c;
13226 Elf32_gptab *tab;
13227 Elf32_External_gptab *ext_tab;
13228 unsigned int j;
13229
13230 /* The .gptab.sdata and .gptab.sbss sections hold
13231 information describing how the small data area would
13232 change depending upon the -G switch. These sections
13233 not used in executables files. */
13234 if (! info->relocatable)
13235 {
13236 for (p = o->map_head.link_order; p != NULL; p = p->next)
13237 {
13238 asection *input_section;
13239
13240 if (p->type != bfd_indirect_link_order)
13241 {
13242 if (p->type == bfd_data_link_order)
13243 continue;
13244 abort ();
13245 }
13246
13247 input_section = p->u.indirect.section;
13248
13249 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13250 elf_link_input_bfd ignores this section. */
13251 input_section->flags &= ~SEC_HAS_CONTENTS;
13252 }
13253
13254 /* Skip this section later on (I don't think this
13255 currently matters, but someday it might). */
13256 o->map_head.link_order = NULL;
13257
13258 /* Really remove the section. */
13259 bfd_section_list_remove (abfd, o);
13260 --abfd->section_count;
13261
13262 continue;
13263 }
13264
13265 /* There is one gptab for initialized data, and one for
13266 uninitialized data. */
13267 if (strcmp (o->name, ".gptab.sdata") == 0)
13268 gptab_data_sec = o;
13269 else if (strcmp (o->name, ".gptab.sbss") == 0)
13270 gptab_bss_sec = o;
13271 else
13272 {
13273 (*_bfd_error_handler)
13274 (_("%s: illegal section name `%s'"),
13275 bfd_get_filename (abfd), o->name);
13276 bfd_set_error (bfd_error_nonrepresentable_section);
13277 return FALSE;
13278 }
13279
13280 /* The linker script always combines .gptab.data and
13281 .gptab.sdata into .gptab.sdata, and likewise for
13282 .gptab.bss and .gptab.sbss. It is possible that there is
13283 no .sdata or .sbss section in the output file, in which
13284 case we must change the name of the output section. */
13285 subname = o->name + sizeof ".gptab" - 1;
13286 if (bfd_get_section_by_name (abfd, subname) == NULL)
13287 {
13288 if (o == gptab_data_sec)
13289 o->name = ".gptab.data";
13290 else
13291 o->name = ".gptab.bss";
13292 subname = o->name + sizeof ".gptab" - 1;
13293 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
13294 }
13295
13296 /* Set up the first entry. */
13297 c = 1;
13298 amt = c * sizeof (Elf32_gptab);
13299 tab = bfd_malloc (amt);
13300 if (tab == NULL)
13301 return FALSE;
13302 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
13303 tab[0].gt_header.gt_unused = 0;
13304
13305 /* Combine the input sections. */
13306 for (p = o->map_head.link_order; p != NULL; p = p->next)
13307 {
13308 asection *input_section;
13309 bfd *input_bfd;
13310 bfd_size_type size;
13311 unsigned long last;
13312 bfd_size_type gpentry;
13313
13314 if (p->type != bfd_indirect_link_order)
13315 {
13316 if (p->type == bfd_data_link_order)
13317 continue;
13318 abort ();
13319 }
13320
13321 input_section = p->u.indirect.section;
13322 input_bfd = input_section->owner;
13323
13324 /* Combine the gptab entries for this input section one
13325 by one. We know that the input gptab entries are
13326 sorted by ascending -G value. */
13327 size = input_section->size;
13328 last = 0;
13329 for (gpentry = sizeof (Elf32_External_gptab);
13330 gpentry < size;
13331 gpentry += sizeof (Elf32_External_gptab))
13332 {
13333 Elf32_External_gptab ext_gptab;
13334 Elf32_gptab int_gptab;
13335 unsigned long val;
13336 unsigned long add;
13337 bfd_boolean exact;
13338 unsigned int look;
13339
13340 if (! (bfd_get_section_contents
13341 (input_bfd, input_section, &ext_gptab, gpentry,
13342 sizeof (Elf32_External_gptab))))
13343 {
13344 free (tab);
13345 return FALSE;
13346 }
13347
13348 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
13349 &int_gptab);
13350 val = int_gptab.gt_entry.gt_g_value;
13351 add = int_gptab.gt_entry.gt_bytes - last;
13352
13353 exact = FALSE;
13354 for (look = 1; look < c; look++)
13355 {
13356 if (tab[look].gt_entry.gt_g_value >= val)
13357 tab[look].gt_entry.gt_bytes += add;
13358
13359 if (tab[look].gt_entry.gt_g_value == val)
13360 exact = TRUE;
13361 }
13362
13363 if (! exact)
13364 {
13365 Elf32_gptab *new_tab;
13366 unsigned int max;
13367
13368 /* We need a new table entry. */
13369 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
13370 new_tab = bfd_realloc (tab, amt);
13371 if (new_tab == NULL)
13372 {
13373 free (tab);
13374 return FALSE;
13375 }
13376 tab = new_tab;
13377 tab[c].gt_entry.gt_g_value = val;
13378 tab[c].gt_entry.gt_bytes = add;
13379
13380 /* Merge in the size for the next smallest -G
13381 value, since that will be implied by this new
13382 value. */
13383 max = 0;
13384 for (look = 1; look < c; look++)
13385 {
13386 if (tab[look].gt_entry.gt_g_value < val
13387 && (max == 0
13388 || (tab[look].gt_entry.gt_g_value
13389 > tab[max].gt_entry.gt_g_value)))
13390 max = look;
13391 }
13392 if (max != 0)
13393 tab[c].gt_entry.gt_bytes +=
13394 tab[max].gt_entry.gt_bytes;
13395
13396 ++c;
13397 }
13398
13399 last = int_gptab.gt_entry.gt_bytes;
13400 }
13401
13402 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13403 elf_link_input_bfd ignores this section. */
13404 input_section->flags &= ~SEC_HAS_CONTENTS;
13405 }
13406
13407 /* The table must be sorted by -G value. */
13408 if (c > 2)
13409 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
13410
13411 /* Swap out the table. */
13412 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
13413 ext_tab = bfd_alloc (abfd, amt);
13414 if (ext_tab == NULL)
13415 {
13416 free (tab);
13417 return FALSE;
13418 }
13419
13420 for (j = 0; j < c; j++)
13421 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
13422 free (tab);
13423
13424 o->size = c * sizeof (Elf32_External_gptab);
13425 o->contents = (bfd_byte *) ext_tab;
13426
13427 /* Skip this section later on (I don't think this currently
13428 matters, but someday it might). */
13429 o->map_head.link_order = NULL;
13430 }
13431 }
13432
13433 /* Invoke the regular ELF backend linker to do all the work. */
13434 if (!bfd_elf_final_link (abfd, info))
13435 return FALSE;
13436
13437 /* Now write out the computed sections. */
13438
13439 if (reginfo_sec != NULL)
13440 {
13441 Elf32_External_RegInfo ext;
13442
13443 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
13444 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
13445 return FALSE;
13446 }
13447
13448 if (mdebug_sec != NULL)
13449 {
13450 BFD_ASSERT (abfd->output_has_begun);
13451 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
13452 swap, info,
13453 mdebug_sec->filepos))
13454 return FALSE;
13455
13456 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
13457 }
13458
13459 if (gptab_data_sec != NULL)
13460 {
13461 if (! bfd_set_section_contents (abfd, gptab_data_sec,
13462 gptab_data_sec->contents,
13463 0, gptab_data_sec->size))
13464 return FALSE;
13465 }
13466
13467 if (gptab_bss_sec != NULL)
13468 {
13469 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
13470 gptab_bss_sec->contents,
13471 0, gptab_bss_sec->size))
13472 return FALSE;
13473 }
13474
13475 if (SGI_COMPAT (abfd))
13476 {
13477 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
13478 if (rtproc_sec != NULL)
13479 {
13480 if (! bfd_set_section_contents (abfd, rtproc_sec,
13481 rtproc_sec->contents,
13482 0, rtproc_sec->size))
13483 return FALSE;
13484 }
13485 }
13486
13487 return TRUE;
13488 }
13489 \f
13490 /* Structure for saying that BFD machine EXTENSION extends BASE. */
13491
13492 struct mips_mach_extension {
13493 unsigned long extension, base;
13494 };
13495
13496
13497 /* An array describing how BFD machines relate to one another. The entries
13498 are ordered topologically with MIPS I extensions listed last. */
13499
13500 static const struct mips_mach_extension mips_mach_extensions[] = {
13501 /* MIPS64r2 extensions. */
13502 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
13503 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
13504 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
13505
13506 /* MIPS64 extensions. */
13507 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
13508 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
13509 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
13510 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
13511
13512 /* MIPS V extensions. */
13513 { bfd_mach_mipsisa64, bfd_mach_mips5 },
13514
13515 /* R10000 extensions. */
13516 { bfd_mach_mips12000, bfd_mach_mips10000 },
13517 { bfd_mach_mips14000, bfd_mach_mips10000 },
13518 { bfd_mach_mips16000, bfd_mach_mips10000 },
13519
13520 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
13521 vr5400 ISA, but doesn't include the multimedia stuff. It seems
13522 better to allow vr5400 and vr5500 code to be merged anyway, since
13523 many libraries will just use the core ISA. Perhaps we could add
13524 some sort of ASE flag if this ever proves a problem. */
13525 { bfd_mach_mips5500, bfd_mach_mips5400 },
13526 { bfd_mach_mips5400, bfd_mach_mips5000 },
13527
13528 /* MIPS IV extensions. */
13529 { bfd_mach_mips5, bfd_mach_mips8000 },
13530 { bfd_mach_mips10000, bfd_mach_mips8000 },
13531 { bfd_mach_mips5000, bfd_mach_mips8000 },
13532 { bfd_mach_mips7000, bfd_mach_mips8000 },
13533 { bfd_mach_mips9000, bfd_mach_mips8000 },
13534
13535 /* VR4100 extensions. */
13536 { bfd_mach_mips4120, bfd_mach_mips4100 },
13537 { bfd_mach_mips4111, bfd_mach_mips4100 },
13538
13539 /* MIPS III extensions. */
13540 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
13541 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
13542 { bfd_mach_mips8000, bfd_mach_mips4000 },
13543 { bfd_mach_mips4650, bfd_mach_mips4000 },
13544 { bfd_mach_mips4600, bfd_mach_mips4000 },
13545 { bfd_mach_mips4400, bfd_mach_mips4000 },
13546 { bfd_mach_mips4300, bfd_mach_mips4000 },
13547 { bfd_mach_mips4100, bfd_mach_mips4000 },
13548 { bfd_mach_mips4010, bfd_mach_mips4000 },
13549
13550 /* MIPS32 extensions. */
13551 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
13552
13553 /* MIPS II extensions. */
13554 { bfd_mach_mips4000, bfd_mach_mips6000 },
13555 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
13556
13557 /* MIPS I extensions. */
13558 { bfd_mach_mips6000, bfd_mach_mips3000 },
13559 { bfd_mach_mips3900, bfd_mach_mips3000 }
13560 };
13561
13562
13563 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
13564
13565 static bfd_boolean
13566 mips_mach_extends_p (unsigned long base, unsigned long extension)
13567 {
13568 size_t i;
13569
13570 if (extension == base)
13571 return TRUE;
13572
13573 if (base == bfd_mach_mipsisa32
13574 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
13575 return TRUE;
13576
13577 if (base == bfd_mach_mipsisa32r2
13578 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
13579 return TRUE;
13580
13581 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
13582 if (extension == mips_mach_extensions[i].extension)
13583 {
13584 extension = mips_mach_extensions[i].base;
13585 if (extension == base)
13586 return TRUE;
13587 }
13588
13589 return FALSE;
13590 }
13591
13592
13593 /* Return true if the given ELF header flags describe a 32-bit binary. */
13594
13595 static bfd_boolean
13596 mips_32bit_flags_p (flagword flags)
13597 {
13598 return ((flags & EF_MIPS_32BITMODE) != 0
13599 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
13600 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
13601 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
13602 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
13603 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
13604 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
13605 }
13606
13607
13608 /* Merge object attributes from IBFD into OBFD. Raise an error if
13609 there are conflicting attributes. */
13610 static bfd_boolean
13611 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
13612 {
13613 obj_attribute *in_attr;
13614 obj_attribute *out_attr;
13615
13616 if (!elf_known_obj_attributes_proc (obfd)[0].i)
13617 {
13618 /* This is the first object. Copy the attributes. */
13619 _bfd_elf_copy_obj_attributes (ibfd, obfd);
13620
13621 /* Use the Tag_null value to indicate the attributes have been
13622 initialized. */
13623 elf_known_obj_attributes_proc (obfd)[0].i = 1;
13624
13625 return TRUE;
13626 }
13627
13628 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
13629 non-conflicting ones. */
13630 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
13631 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
13632 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
13633 {
13634 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
13635 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
13636 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
13637 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
13638 ;
13639 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
13640 _bfd_error_handler
13641 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
13642 in_attr[Tag_GNU_MIPS_ABI_FP].i);
13643 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
13644 _bfd_error_handler
13645 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
13646 out_attr[Tag_GNU_MIPS_ABI_FP].i);
13647 else
13648 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
13649 {
13650 case 1:
13651 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13652 {
13653 case 2:
13654 _bfd_error_handler
13655 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
13656 obfd, ibfd);
13657 break;
13658
13659 case 3:
13660 _bfd_error_handler
13661 (_("Warning: %B uses hard float, %B uses soft float"),
13662 obfd, ibfd);
13663 break;
13664
13665 case 4:
13666 _bfd_error_handler
13667 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
13668 obfd, ibfd);
13669 break;
13670
13671 default:
13672 abort ();
13673 }
13674 break;
13675
13676 case 2:
13677 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13678 {
13679 case 1:
13680 _bfd_error_handler
13681 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
13682 ibfd, obfd);
13683 break;
13684
13685 case 3:
13686 _bfd_error_handler
13687 (_("Warning: %B uses hard float, %B uses soft float"),
13688 obfd, ibfd);
13689 break;
13690
13691 case 4:
13692 _bfd_error_handler
13693 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
13694 obfd, ibfd);
13695 break;
13696
13697 default:
13698 abort ();
13699 }
13700 break;
13701
13702 case 3:
13703 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13704 {
13705 case 1:
13706 case 2:
13707 case 4:
13708 _bfd_error_handler
13709 (_("Warning: %B uses hard float, %B uses soft float"),
13710 ibfd, obfd);
13711 break;
13712
13713 default:
13714 abort ();
13715 }
13716 break;
13717
13718 case 4:
13719 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
13720 {
13721 case 1:
13722 _bfd_error_handler
13723 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
13724 ibfd, obfd);
13725 break;
13726
13727 case 2:
13728 _bfd_error_handler
13729 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
13730 ibfd, obfd);
13731 break;
13732
13733 case 3:
13734 _bfd_error_handler
13735 (_("Warning: %B uses hard float, %B uses soft float"),
13736 obfd, ibfd);
13737 break;
13738
13739 default:
13740 abort ();
13741 }
13742 break;
13743
13744 default:
13745 abort ();
13746 }
13747 }
13748
13749 /* Merge Tag_compatibility attributes and any common GNU ones. */
13750 _bfd_elf_merge_object_attributes (ibfd, obfd);
13751
13752 return TRUE;
13753 }
13754
13755 /* Merge backend specific data from an object file to the output
13756 object file when linking. */
13757
13758 bfd_boolean
13759 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
13760 {
13761 flagword old_flags;
13762 flagword new_flags;
13763 bfd_boolean ok;
13764 bfd_boolean null_input_bfd = TRUE;
13765 asection *sec;
13766
13767 /* Check if we have the same endianness. */
13768 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
13769 {
13770 (*_bfd_error_handler)
13771 (_("%B: endianness incompatible with that of the selected emulation"),
13772 ibfd);
13773 return FALSE;
13774 }
13775
13776 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
13777 return TRUE;
13778
13779 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
13780 {
13781 (*_bfd_error_handler)
13782 (_("%B: ABI is incompatible with that of the selected emulation"),
13783 ibfd);
13784 return FALSE;
13785 }
13786
13787 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
13788 return FALSE;
13789
13790 new_flags = elf_elfheader (ibfd)->e_flags;
13791 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
13792 old_flags = elf_elfheader (obfd)->e_flags;
13793
13794 if (! elf_flags_init (obfd))
13795 {
13796 elf_flags_init (obfd) = TRUE;
13797 elf_elfheader (obfd)->e_flags = new_flags;
13798 elf_elfheader (obfd)->e_ident[EI_CLASS]
13799 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
13800
13801 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
13802 && (bfd_get_arch_info (obfd)->the_default
13803 || mips_mach_extends_p (bfd_get_mach (obfd),
13804 bfd_get_mach (ibfd))))
13805 {
13806 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
13807 bfd_get_mach (ibfd)))
13808 return FALSE;
13809 }
13810
13811 return TRUE;
13812 }
13813
13814 /* Check flag compatibility. */
13815
13816 new_flags &= ~EF_MIPS_NOREORDER;
13817 old_flags &= ~EF_MIPS_NOREORDER;
13818
13819 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
13820 doesn't seem to matter. */
13821 new_flags &= ~EF_MIPS_XGOT;
13822 old_flags &= ~EF_MIPS_XGOT;
13823
13824 /* MIPSpro generates ucode info in n64 objects. Again, we should
13825 just be able to ignore this. */
13826 new_flags &= ~EF_MIPS_UCODE;
13827 old_flags &= ~EF_MIPS_UCODE;
13828
13829 /* DSOs should only be linked with CPIC code. */
13830 if ((ibfd->flags & DYNAMIC) != 0)
13831 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
13832
13833 if (new_flags == old_flags)
13834 return TRUE;
13835
13836 /* Check to see if the input BFD actually contains any sections.
13837 If not, its flags may not have been initialised either, but it cannot
13838 actually cause any incompatibility. */
13839 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
13840 {
13841 /* Ignore synthetic sections and empty .text, .data and .bss sections
13842 which are automatically generated by gas. Also ignore fake
13843 (s)common sections, since merely defining a common symbol does
13844 not affect compatibility. */
13845 if ((sec->flags & SEC_IS_COMMON) == 0
13846 && strcmp (sec->name, ".reginfo")
13847 && strcmp (sec->name, ".mdebug")
13848 && (sec->size != 0
13849 || (strcmp (sec->name, ".text")
13850 && strcmp (sec->name, ".data")
13851 && strcmp (sec->name, ".bss"))))
13852 {
13853 null_input_bfd = FALSE;
13854 break;
13855 }
13856 }
13857 if (null_input_bfd)
13858 return TRUE;
13859
13860 ok = TRUE;
13861
13862 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
13863 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
13864 {
13865 (*_bfd_error_handler)
13866 (_("%B: warning: linking abicalls files with non-abicalls files"),
13867 ibfd);
13868 ok = TRUE;
13869 }
13870
13871 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
13872 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
13873 if (! (new_flags & EF_MIPS_PIC))
13874 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
13875
13876 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
13877 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
13878
13879 /* Compare the ISAs. */
13880 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
13881 {
13882 (*_bfd_error_handler)
13883 (_("%B: linking 32-bit code with 64-bit code"),
13884 ibfd);
13885 ok = FALSE;
13886 }
13887 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
13888 {
13889 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
13890 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
13891 {
13892 /* Copy the architecture info from IBFD to OBFD. Also copy
13893 the 32-bit flag (if set) so that we continue to recognise
13894 OBFD as a 32-bit binary. */
13895 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
13896 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
13897 elf_elfheader (obfd)->e_flags
13898 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13899
13900 /* Copy across the ABI flags if OBFD doesn't use them
13901 and if that was what caused us to treat IBFD as 32-bit. */
13902 if ((old_flags & EF_MIPS_ABI) == 0
13903 && mips_32bit_flags_p (new_flags)
13904 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
13905 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
13906 }
13907 else
13908 {
13909 /* The ISAs aren't compatible. */
13910 (*_bfd_error_handler)
13911 (_("%B: linking %s module with previous %s modules"),
13912 ibfd,
13913 bfd_printable_name (ibfd),
13914 bfd_printable_name (obfd));
13915 ok = FALSE;
13916 }
13917 }
13918
13919 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13920 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
13921
13922 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
13923 does set EI_CLASS differently from any 32-bit ABI. */
13924 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
13925 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
13926 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
13927 {
13928 /* Only error if both are set (to different values). */
13929 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
13930 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
13931 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
13932 {
13933 (*_bfd_error_handler)
13934 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
13935 ibfd,
13936 elf_mips_abi_name (ibfd),
13937 elf_mips_abi_name (obfd));
13938 ok = FALSE;
13939 }
13940 new_flags &= ~EF_MIPS_ABI;
13941 old_flags &= ~EF_MIPS_ABI;
13942 }
13943
13944 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
13945 and allow arbitrary mixing of the remaining ASEs (retain the union). */
13946 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
13947 {
13948 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
13949 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
13950 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
13951 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
13952 int micro_mis = old_m16 && new_micro;
13953 int m16_mis = old_micro && new_m16;
13954
13955 if (m16_mis || micro_mis)
13956 {
13957 (*_bfd_error_handler)
13958 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
13959 ibfd,
13960 m16_mis ? "MIPS16" : "microMIPS",
13961 m16_mis ? "microMIPS" : "MIPS16");
13962 ok = FALSE;
13963 }
13964
13965 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
13966
13967 new_flags &= ~ EF_MIPS_ARCH_ASE;
13968 old_flags &= ~ EF_MIPS_ARCH_ASE;
13969 }
13970
13971 /* Warn about any other mismatches */
13972 if (new_flags != old_flags)
13973 {
13974 (*_bfd_error_handler)
13975 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
13976 ibfd, (unsigned long) new_flags,
13977 (unsigned long) old_flags);
13978 ok = FALSE;
13979 }
13980
13981 if (! ok)
13982 {
13983 bfd_set_error (bfd_error_bad_value);
13984 return FALSE;
13985 }
13986
13987 return TRUE;
13988 }
13989
13990 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
13991
13992 bfd_boolean
13993 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
13994 {
13995 BFD_ASSERT (!elf_flags_init (abfd)
13996 || elf_elfheader (abfd)->e_flags == flags);
13997
13998 elf_elfheader (abfd)->e_flags = flags;
13999 elf_flags_init (abfd) = TRUE;
14000 return TRUE;
14001 }
14002
14003 char *
14004 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14005 {
14006 switch (dtag)
14007 {
14008 default: return "";
14009 case DT_MIPS_RLD_VERSION:
14010 return "MIPS_RLD_VERSION";
14011 case DT_MIPS_TIME_STAMP:
14012 return "MIPS_TIME_STAMP";
14013 case DT_MIPS_ICHECKSUM:
14014 return "MIPS_ICHECKSUM";
14015 case DT_MIPS_IVERSION:
14016 return "MIPS_IVERSION";
14017 case DT_MIPS_FLAGS:
14018 return "MIPS_FLAGS";
14019 case DT_MIPS_BASE_ADDRESS:
14020 return "MIPS_BASE_ADDRESS";
14021 case DT_MIPS_MSYM:
14022 return "MIPS_MSYM";
14023 case DT_MIPS_CONFLICT:
14024 return "MIPS_CONFLICT";
14025 case DT_MIPS_LIBLIST:
14026 return "MIPS_LIBLIST";
14027 case DT_MIPS_LOCAL_GOTNO:
14028 return "MIPS_LOCAL_GOTNO";
14029 case DT_MIPS_CONFLICTNO:
14030 return "MIPS_CONFLICTNO";
14031 case DT_MIPS_LIBLISTNO:
14032 return "MIPS_LIBLISTNO";
14033 case DT_MIPS_SYMTABNO:
14034 return "MIPS_SYMTABNO";
14035 case DT_MIPS_UNREFEXTNO:
14036 return "MIPS_UNREFEXTNO";
14037 case DT_MIPS_GOTSYM:
14038 return "MIPS_GOTSYM";
14039 case DT_MIPS_HIPAGENO:
14040 return "MIPS_HIPAGENO";
14041 case DT_MIPS_RLD_MAP:
14042 return "MIPS_RLD_MAP";
14043 case DT_MIPS_DELTA_CLASS:
14044 return "MIPS_DELTA_CLASS";
14045 case DT_MIPS_DELTA_CLASS_NO:
14046 return "MIPS_DELTA_CLASS_NO";
14047 case DT_MIPS_DELTA_INSTANCE:
14048 return "MIPS_DELTA_INSTANCE";
14049 case DT_MIPS_DELTA_INSTANCE_NO:
14050 return "MIPS_DELTA_INSTANCE_NO";
14051 case DT_MIPS_DELTA_RELOC:
14052 return "MIPS_DELTA_RELOC";
14053 case DT_MIPS_DELTA_RELOC_NO:
14054 return "MIPS_DELTA_RELOC_NO";
14055 case DT_MIPS_DELTA_SYM:
14056 return "MIPS_DELTA_SYM";
14057 case DT_MIPS_DELTA_SYM_NO:
14058 return "MIPS_DELTA_SYM_NO";
14059 case DT_MIPS_DELTA_CLASSSYM:
14060 return "MIPS_DELTA_CLASSSYM";
14061 case DT_MIPS_DELTA_CLASSSYM_NO:
14062 return "MIPS_DELTA_CLASSSYM_NO";
14063 case DT_MIPS_CXX_FLAGS:
14064 return "MIPS_CXX_FLAGS";
14065 case DT_MIPS_PIXIE_INIT:
14066 return "MIPS_PIXIE_INIT";
14067 case DT_MIPS_SYMBOL_LIB:
14068 return "MIPS_SYMBOL_LIB";
14069 case DT_MIPS_LOCALPAGE_GOTIDX:
14070 return "MIPS_LOCALPAGE_GOTIDX";
14071 case DT_MIPS_LOCAL_GOTIDX:
14072 return "MIPS_LOCAL_GOTIDX";
14073 case DT_MIPS_HIDDEN_GOTIDX:
14074 return "MIPS_HIDDEN_GOTIDX";
14075 case DT_MIPS_PROTECTED_GOTIDX:
14076 return "MIPS_PROTECTED_GOT_IDX";
14077 case DT_MIPS_OPTIONS:
14078 return "MIPS_OPTIONS";
14079 case DT_MIPS_INTERFACE:
14080 return "MIPS_INTERFACE";
14081 case DT_MIPS_DYNSTR_ALIGN:
14082 return "DT_MIPS_DYNSTR_ALIGN";
14083 case DT_MIPS_INTERFACE_SIZE:
14084 return "DT_MIPS_INTERFACE_SIZE";
14085 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14086 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14087 case DT_MIPS_PERF_SUFFIX:
14088 return "DT_MIPS_PERF_SUFFIX";
14089 case DT_MIPS_COMPACT_SIZE:
14090 return "DT_MIPS_COMPACT_SIZE";
14091 case DT_MIPS_GP_VALUE:
14092 return "DT_MIPS_GP_VALUE";
14093 case DT_MIPS_AUX_DYNAMIC:
14094 return "DT_MIPS_AUX_DYNAMIC";
14095 case DT_MIPS_PLTGOT:
14096 return "DT_MIPS_PLTGOT";
14097 case DT_MIPS_RWPLT:
14098 return "DT_MIPS_RWPLT";
14099 }
14100 }
14101
14102 bfd_boolean
14103 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
14104 {
14105 FILE *file = ptr;
14106
14107 BFD_ASSERT (abfd != NULL && ptr != NULL);
14108
14109 /* Print normal ELF private data. */
14110 _bfd_elf_print_private_bfd_data (abfd, ptr);
14111
14112 /* xgettext:c-format */
14113 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14114
14115 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14116 fprintf (file, _(" [abi=O32]"));
14117 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14118 fprintf (file, _(" [abi=O64]"));
14119 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14120 fprintf (file, _(" [abi=EABI32]"));
14121 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14122 fprintf (file, _(" [abi=EABI64]"));
14123 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14124 fprintf (file, _(" [abi unknown]"));
14125 else if (ABI_N32_P (abfd))
14126 fprintf (file, _(" [abi=N32]"));
14127 else if (ABI_64_P (abfd))
14128 fprintf (file, _(" [abi=64]"));
14129 else
14130 fprintf (file, _(" [no abi set]"));
14131
14132 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
14133 fprintf (file, " [mips1]");
14134 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
14135 fprintf (file, " [mips2]");
14136 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
14137 fprintf (file, " [mips3]");
14138 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
14139 fprintf (file, " [mips4]");
14140 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
14141 fprintf (file, " [mips5]");
14142 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
14143 fprintf (file, " [mips32]");
14144 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
14145 fprintf (file, " [mips64]");
14146 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
14147 fprintf (file, " [mips32r2]");
14148 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
14149 fprintf (file, " [mips64r2]");
14150 else
14151 fprintf (file, _(" [unknown ISA]"));
14152
14153 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14154 fprintf (file, " [mdmx]");
14155
14156 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14157 fprintf (file, " [mips16]");
14158
14159 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14160 fprintf (file, " [micromips]");
14161
14162 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
14163 fprintf (file, " [32bitmode]");
14164 else
14165 fprintf (file, _(" [not 32bitmode]"));
14166
14167 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
14168 fprintf (file, " [noreorder]");
14169
14170 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
14171 fprintf (file, " [PIC]");
14172
14173 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
14174 fprintf (file, " [CPIC]");
14175
14176 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
14177 fprintf (file, " [XGOT]");
14178
14179 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
14180 fprintf (file, " [UCODE]");
14181
14182 fputc ('\n', file);
14183
14184 return TRUE;
14185 }
14186
14187 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
14188 {
14189 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14190 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14191 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
14192 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14193 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
14194 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
14195 { NULL, 0, 0, 0, 0 }
14196 };
14197
14198 /* Merge non visibility st_other attributes. Ensure that the
14199 STO_OPTIONAL flag is copied into h->other, even if this is not a
14200 definiton of the symbol. */
14201 void
14202 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
14203 const Elf_Internal_Sym *isym,
14204 bfd_boolean definition,
14205 bfd_boolean dynamic ATTRIBUTE_UNUSED)
14206 {
14207 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
14208 {
14209 unsigned char other;
14210
14211 other = (definition ? isym->st_other : h->other);
14212 other &= ~ELF_ST_VISIBILITY (-1);
14213 h->other = other | ELF_ST_VISIBILITY (h->other);
14214 }
14215
14216 if (!definition
14217 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
14218 h->other |= STO_OPTIONAL;
14219 }
14220
14221 /* Decide whether an undefined symbol is special and can be ignored.
14222 This is the case for OPTIONAL symbols on IRIX. */
14223 bfd_boolean
14224 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
14225 {
14226 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
14227 }
14228
14229 bfd_boolean
14230 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
14231 {
14232 return (sym->st_shndx == SHN_COMMON
14233 || sym->st_shndx == SHN_MIPS_ACOMMON
14234 || sym->st_shndx == SHN_MIPS_SCOMMON);
14235 }
14236
14237 /* Return address for Ith PLT stub in section PLT, for relocation REL
14238 or (bfd_vma) -1 if it should not be included. */
14239
14240 bfd_vma
14241 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
14242 const arelent *rel ATTRIBUTE_UNUSED)
14243 {
14244 return (plt->vma
14245 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
14246 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
14247 }
14248
14249 void
14250 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
14251 {
14252 struct mips_elf_link_hash_table *htab;
14253 Elf_Internal_Ehdr *i_ehdrp;
14254
14255 i_ehdrp = elf_elfheader (abfd);
14256 if (link_info)
14257 {
14258 htab = mips_elf_hash_table (link_info);
14259 BFD_ASSERT (htab != NULL);
14260
14261 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
14262 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
14263 }
14264 }
This page took 0.451242 seconds and 5 git commands to generate.