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