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