* elf32-ppc.c (ppc_elf_relax_section): Correct conditions under
[binutils.git] / bfd / elfxx-mips.c
blob814ba90cdb3c56da7235d4129734a7c8c267f970
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 Free Software Foundation, Inc.
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>
12 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
30 /* This file handles functionality common to the different MIPS ABI's. */
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"
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"
47 #include "hashtab.h"
49 /* This structure is used to hold information about one GOT entry.
50 There are three types of entry:
52 (1) absolute addresses
53 (abfd == NULL)
54 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
55 (abfd != NULL, symndx >= 0)
56 (3) global and forced-local symbols
57 (abfd != NULL, symndx == -1)
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.
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.
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.
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
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
91 /* If abfd == NULL, an address that must be stored in the got. */
92 bfd_vma address;
93 /* If abfd != NULL && symndx != -1, the addend of the relocation
94 that should be added to the symbol value. */
95 bfd_vma addend;
96 /* If abfd != NULL && symndx == -1, the hash table entry
97 corresponding to a global symbol in the got (or, local, if
98 h->forced_local). */
99 struct mips_elf_link_hash_entry *h;
100 } d;
102 /* The TLS types included in this GOT entry (specifically, GD and
103 IE). The GD and IE flags can be added as we encounter new
104 relocations. LDM can also be set; it will always be alone, not
105 combined with any GD or IE flags. An LDM GOT entry will be
106 a local symbol entry with r_symndx == 0. */
107 unsigned char tls_type;
109 /* The offset from the beginning of the .got section to the entry
110 corresponding to this symbol+addend. If it's a global symbol
111 whose offset is yet to be decided, it's going to be -1. */
112 long gotidx;
115 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
116 The structures form a non-overlapping list that is sorted by increasing
117 MIN_ADDEND. */
118 struct mips_got_page_range
120 struct mips_got_page_range *next;
121 bfd_signed_vma min_addend;
122 bfd_signed_vma max_addend;
125 /* This structure describes the range of addends that are applied to page
126 relocations against a given symbol. */
127 struct mips_got_page_entry
129 /* The input bfd in which the symbol is defined. */
130 bfd *abfd;
131 /* The index of the symbol, as stored in the relocation r_info. */
132 long symndx;
133 /* The ranges for this page entry. */
134 struct mips_got_page_range *ranges;
135 /* The maximum number of page entries needed for RANGES. */
136 bfd_vma num_pages;
139 /* This structure is used to hold .got information when linking. */
141 struct mips_got_info
143 /* The global symbol in the GOT with the lowest index in the dynamic
144 symbol table. */
145 struct elf_link_hash_entry *global_gotsym;
146 /* The number of global .got entries. */
147 unsigned int global_gotno;
148 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
149 unsigned int reloc_only_gotno;
150 /* The number of .got slots used for TLS. */
151 unsigned int tls_gotno;
152 /* The first unused TLS .got entry. Used only during
153 mips_elf_initialize_tls_index. */
154 unsigned int tls_assigned_gotno;
155 /* The number of local .got entries, eventually including page entries. */
156 unsigned int local_gotno;
157 /* The maximum number of page entries needed. */
158 unsigned int page_gotno;
159 /* The number of local .got entries we have used. */
160 unsigned int assigned_gotno;
161 /* A hash table holding members of the got. */
162 struct htab *got_entries;
163 /* A hash table of mips_got_page_entry structures. */
164 struct htab *got_page_entries;
165 /* A hash table mapping input bfds to other mips_got_info. NULL
166 unless multi-got was necessary. */
167 struct htab *bfd2got;
168 /* In multi-got links, a pointer to the next got (err, rather, most
169 of the time, it points to the previous got). */
170 struct mips_got_info *next;
171 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
172 for none, or MINUS_TWO for not yet assigned. This is needed
173 because a single-GOT link may have multiple hash table entries
174 for the LDM. It does not get initialized in multi-GOT mode. */
175 bfd_vma tls_ldm_offset;
178 /* Map an input bfd to a got in a multi-got link. */
180 struct mips_elf_bfd2got_hash {
181 bfd *bfd;
182 struct mips_got_info *g;
185 /* Structure passed when traversing the bfd2got hash table, used to
186 create and merge bfd's gots. */
188 struct mips_elf_got_per_bfd_arg
190 /* A hashtable that maps bfds to gots. */
191 htab_t bfd2got;
192 /* The output bfd. */
193 bfd *obfd;
194 /* The link information. */
195 struct bfd_link_info *info;
196 /* A pointer to the primary got, i.e., the one that's going to get
197 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198 DT_MIPS_GOTSYM. */
199 struct mips_got_info *primary;
200 /* A non-primary got we're trying to merge with other input bfd's
201 gots. */
202 struct mips_got_info *current;
203 /* The maximum number of got entries that can be addressed with a
204 16-bit offset. */
205 unsigned int max_count;
206 /* The maximum number of page entries needed by each got. */
207 unsigned int max_pages;
208 /* The total number of global entries which will live in the
209 primary got and be automatically relocated. This includes
210 those not referenced by the primary GOT but included in
211 the "master" GOT. */
212 unsigned int global_count;
215 /* Another structure used to pass arguments for got entries traversal. */
217 struct mips_elf_set_global_got_offset_arg
219 struct mips_got_info *g;
220 int value;
221 unsigned int needed_relocs;
222 struct bfd_link_info *info;
225 /* A structure used to count TLS relocations or GOT entries, for GOT
226 entry or ELF symbol table traversal. */
228 struct mips_elf_count_tls_arg
230 struct bfd_link_info *info;
231 unsigned int needed;
234 struct _mips_elf_section_data
236 struct bfd_elf_section_data elf;
237 union
239 bfd_byte *tdata;
240 } u;
243 #define mips_elf_section_data(sec) \
244 ((struct _mips_elf_section_data *) elf_section_data (sec))
246 #define is_mips_elf(bfd) \
247 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
248 && elf_tdata (bfd) != NULL \
249 && elf_object_id (bfd) == MIPS_ELF_TDATA)
251 /* The ABI says that every symbol used by dynamic relocations must have
252 a global GOT entry. Among other things, this provides the dynamic
253 linker with a free, directly-indexed cache. The GOT can therefore
254 contain symbols that are not referenced by GOT relocations themselves
255 (in other words, it may have symbols that are not referenced by things
256 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
258 GOT relocations are less likely to overflow if we put the associated
259 GOT entries towards the beginning. We therefore divide the global
260 GOT entries into two areas: "normal" and "reloc-only". Entries in
261 the first area can be used for both dynamic relocations and GP-relative
262 accesses, while those in the "reloc-only" area are for dynamic
263 relocations only.
265 These GGA_* ("Global GOT Area") values are organised so that lower
266 values are more general than higher values. Also, non-GGA_NONE
267 values are ordered by the position of the area in the GOT. */
268 #define GGA_NORMAL 0
269 #define GGA_RELOC_ONLY 1
270 #define GGA_NONE 2
272 /* Information about a non-PIC interface to a PIC function. There are
273 two ways of creating these interfaces. The first is to add:
275 lui $25,%hi(func)
276 addiu $25,$25,%lo(func)
278 immediately before a PIC function "func". The second is to add:
280 lui $25,%hi(func)
281 j func
282 addiu $25,$25,%lo(func)
284 to a separate trampoline section.
286 Stubs of the first kind go in a new section immediately before the
287 target function. Stubs of the second kind go in a single section
288 pointed to by the hash table's "strampoline" field. */
289 struct mips_elf_la25_stub {
290 /* The generated section that contains this stub. */
291 asection *stub_section;
293 /* The offset of the stub from the start of STUB_SECTION. */
294 bfd_vma offset;
296 /* One symbol for the original function. Its location is available
297 in H->root.root.u.def. */
298 struct mips_elf_link_hash_entry *h;
301 /* Macros for populating a mips_elf_la25_stub. */
303 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
304 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
305 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
307 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
308 the dynamic symbols. */
310 struct mips_elf_hash_sort_data
312 /* The symbol in the global GOT with the lowest dynamic symbol table
313 index. */
314 struct elf_link_hash_entry *low;
315 /* The least dynamic symbol table index corresponding to a non-TLS
316 symbol with a GOT entry. */
317 long min_got_dynindx;
318 /* The greatest dynamic symbol table index corresponding to a symbol
319 with a GOT entry that is not referenced (e.g., a dynamic symbol
320 with dynamic relocations pointing to it from non-primary GOTs). */
321 long max_unref_got_dynindx;
322 /* The greatest dynamic symbol table index not corresponding to a
323 symbol without a GOT entry. */
324 long max_non_got_dynindx;
327 /* The MIPS ELF linker needs additional information for each symbol in
328 the global hash table. */
330 struct mips_elf_link_hash_entry
332 struct elf_link_hash_entry root;
334 /* External symbol information. */
335 EXTR esym;
337 /* The la25 stub we have created for ths symbol, if any. */
338 struct mips_elf_la25_stub *la25_stub;
340 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
341 this symbol. */
342 unsigned int possibly_dynamic_relocs;
344 /* If there is a stub that 32 bit functions should use to call this
345 16 bit function, this points to the section containing the stub. */
346 asection *fn_stub;
348 /* If there is a stub that 16 bit functions should use to call this
349 32 bit function, this points to the section containing the stub. */
350 asection *call_stub;
352 /* This is like the call_stub field, but it is used if the function
353 being called returns a floating point value. */
354 asection *call_fp_stub;
356 #define GOT_NORMAL 0
357 #define GOT_TLS_GD 1
358 #define GOT_TLS_LDM 2
359 #define GOT_TLS_IE 4
360 #define GOT_TLS_OFFSET_DONE 0x40
361 #define GOT_TLS_DONE 0x80
362 unsigned char tls_type;
364 /* This is only used in single-GOT mode; in multi-GOT mode there
365 is one mips_got_entry per GOT entry, so the offset is stored
366 there. In single-GOT mode there may be many mips_got_entry
367 structures all referring to the same GOT slot. It might be
368 possible to use root.got.offset instead, but that field is
369 overloaded already. */
370 bfd_vma tls_got_offset;
372 /* The highest GGA_* value that satisfies all references to this symbol. */
373 unsigned int global_got_area : 2;
375 /* True if one of the relocations described by possibly_dynamic_relocs
376 is against a readonly section. */
377 unsigned int readonly_reloc : 1;
379 /* True if there is a relocation against this symbol that must be
380 resolved by the static linker (in other words, if the relocation
381 cannot possibly be made dynamic). */
382 unsigned int has_static_relocs : 1;
384 /* True if we must not create a .MIPS.stubs entry for this symbol.
385 This is set, for example, if there are relocations related to
386 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
387 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
388 unsigned int no_fn_stub : 1;
390 /* Whether we need the fn_stub; this is true if this symbol appears
391 in any relocs other than a 16 bit call. */
392 unsigned int need_fn_stub : 1;
394 /* True if this symbol is referenced by branch relocations from
395 any non-PIC input file. This is used to determine whether an
396 la25 stub is required. */
397 unsigned int has_nonpic_branches : 1;
399 /* Does this symbol need a traditional MIPS lazy-binding stub
400 (as opposed to a PLT entry)? */
401 unsigned int needs_lazy_stub : 1;
404 /* MIPS ELF linker hash table. */
406 struct mips_elf_link_hash_table
408 struct elf_link_hash_table root;
409 #if 0
410 /* We no longer use this. */
411 /* String section indices for the dynamic section symbols. */
412 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
413 #endif
415 /* The number of .rtproc entries. */
416 bfd_size_type procedure_count;
418 /* The size of the .compact_rel section (if SGI_COMPAT). */
419 bfd_size_type compact_rel_size;
421 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
422 entry is set to the address of __rld_obj_head as in IRIX5. */
423 bfd_boolean use_rld_obj_head;
425 /* This is the value of the __rld_map or __rld_obj_head symbol. */
426 bfd_vma rld_value;
428 /* This is set if we see any mips16 stub sections. */
429 bfd_boolean mips16_stubs_seen;
431 /* True if we can generate copy relocs and PLTs. */
432 bfd_boolean use_plts_and_copy_relocs;
434 /* True if we're generating code for VxWorks. */
435 bfd_boolean is_vxworks;
437 /* True if we already reported the small-data section overflow. */
438 bfd_boolean small_data_overflow_reported;
440 /* Shortcuts to some dynamic sections, or NULL if they are not
441 being used. */
442 asection *srelbss;
443 asection *sdynbss;
444 asection *srelplt;
445 asection *srelplt2;
446 asection *sgotplt;
447 asection *splt;
448 asection *sstubs;
449 asection *sgot;
451 /* The master GOT information. */
452 struct mips_got_info *got_info;
454 /* The size of the PLT header in bytes. */
455 bfd_vma plt_header_size;
457 /* The size of a PLT entry in bytes. */
458 bfd_vma plt_entry_size;
460 /* The number of functions that need a lazy-binding stub. */
461 bfd_vma lazy_stub_count;
463 /* The size of a function stub entry in bytes. */
464 bfd_vma function_stub_size;
466 /* The number of reserved entries at the beginning of the GOT. */
467 unsigned int reserved_gotno;
469 /* The section used for mips_elf_la25_stub trampolines.
470 See the comment above that structure for details. */
471 asection *strampoline;
473 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
474 pairs. */
475 htab_t la25_stubs;
477 /* A function FN (NAME, IS, OS) that creates a new input section
478 called NAME and links it to output section OS. If IS is nonnull,
479 the new section should go immediately before it, otherwise it
480 should go at the (current) beginning of OS.
482 The function returns the new section on success, otherwise it
483 returns null. */
484 asection *(*add_stub_section) (const char *, asection *, asection *);
487 /* A structure used to communicate with htab_traverse callbacks. */
488 struct mips_htab_traverse_info {
489 /* The usual link-wide information. */
490 struct bfd_link_info *info;
491 bfd *output_bfd;
493 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
494 bfd_boolean error;
497 #define TLS_RELOC_P(r_type) \
498 (r_type == R_MIPS_TLS_DTPMOD32 \
499 || r_type == R_MIPS_TLS_DTPMOD64 \
500 || r_type == R_MIPS_TLS_DTPREL32 \
501 || r_type == R_MIPS_TLS_DTPREL64 \
502 || r_type == R_MIPS_TLS_GD \
503 || r_type == R_MIPS_TLS_LDM \
504 || r_type == R_MIPS_TLS_DTPREL_HI16 \
505 || r_type == R_MIPS_TLS_DTPREL_LO16 \
506 || r_type == R_MIPS_TLS_GOTTPREL \
507 || r_type == R_MIPS_TLS_TPREL32 \
508 || r_type == R_MIPS_TLS_TPREL64 \
509 || r_type == R_MIPS_TLS_TPREL_HI16 \
510 || r_type == R_MIPS_TLS_TPREL_LO16)
512 /* Structure used to pass information to mips_elf_output_extsym. */
514 struct extsym_info
516 bfd *abfd;
517 struct bfd_link_info *info;
518 struct ecoff_debug_info *debug;
519 const struct ecoff_debug_swap *swap;
520 bfd_boolean failed;
523 /* The names of the runtime procedure table symbols used on IRIX5. */
525 static const char * const mips_elf_dynsym_rtproc_names[] =
527 "_procedure_table",
528 "_procedure_string_table",
529 "_procedure_table_size",
530 NULL
533 /* These structures are used to generate the .compact_rel section on
534 IRIX5. */
536 typedef struct
538 unsigned long id1; /* Always one? */
539 unsigned long num; /* Number of compact relocation entries. */
540 unsigned long id2; /* Always two? */
541 unsigned long offset; /* The file offset of the first relocation. */
542 unsigned long reserved0; /* Zero? */
543 unsigned long reserved1; /* Zero? */
544 } Elf32_compact_rel;
546 typedef struct
548 bfd_byte id1[4];
549 bfd_byte num[4];
550 bfd_byte id2[4];
551 bfd_byte offset[4];
552 bfd_byte reserved0[4];
553 bfd_byte reserved1[4];
554 } Elf32_External_compact_rel;
556 typedef struct
558 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
559 unsigned int rtype : 4; /* Relocation types. See below. */
560 unsigned int dist2to : 8;
561 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
562 unsigned long konst; /* KONST field. See below. */
563 unsigned long vaddr; /* VADDR to be relocated. */
564 } Elf32_crinfo;
566 typedef struct
568 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
569 unsigned int rtype : 4; /* Relocation types. See below. */
570 unsigned int dist2to : 8;
571 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
572 unsigned long konst; /* KONST field. See below. */
573 } Elf32_crinfo2;
575 typedef struct
577 bfd_byte info[4];
578 bfd_byte konst[4];
579 bfd_byte vaddr[4];
580 } Elf32_External_crinfo;
582 typedef struct
584 bfd_byte info[4];
585 bfd_byte konst[4];
586 } Elf32_External_crinfo2;
588 /* These are the constants used to swap the bitfields in a crinfo. */
590 #define CRINFO_CTYPE (0x1)
591 #define CRINFO_CTYPE_SH (31)
592 #define CRINFO_RTYPE (0xf)
593 #define CRINFO_RTYPE_SH (27)
594 #define CRINFO_DIST2TO (0xff)
595 #define CRINFO_DIST2TO_SH (19)
596 #define CRINFO_RELVADDR (0x7ffff)
597 #define CRINFO_RELVADDR_SH (0)
599 /* A compact relocation info has long (3 words) or short (2 words)
600 formats. A short format doesn't have VADDR field and relvaddr
601 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
602 #define CRF_MIPS_LONG 1
603 #define CRF_MIPS_SHORT 0
605 /* There are 4 types of compact relocation at least. The value KONST
606 has different meaning for each type:
608 (type) (konst)
609 CT_MIPS_REL32 Address in data
610 CT_MIPS_WORD Address in word (XXX)
611 CT_MIPS_GPHI_LO GP - vaddr
612 CT_MIPS_JMPAD Address to jump
615 #define CRT_MIPS_REL32 0xa
616 #define CRT_MIPS_WORD 0xb
617 #define CRT_MIPS_GPHI_LO 0xc
618 #define CRT_MIPS_JMPAD 0xd
620 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
621 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
622 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
623 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
625 /* The structure of the runtime procedure descriptor created by the
626 loader for use by the static exception system. */
628 typedef struct runtime_pdr {
629 bfd_vma adr; /* Memory address of start of procedure. */
630 long regmask; /* Save register mask. */
631 long regoffset; /* Save register offset. */
632 long fregmask; /* Save floating point register mask. */
633 long fregoffset; /* Save floating point register offset. */
634 long frameoffset; /* Frame size. */
635 short framereg; /* Frame pointer register. */
636 short pcreg; /* Offset or reg of return pc. */
637 long irpss; /* Index into the runtime string table. */
638 long reserved;
639 struct exception_info *exception_info;/* Pointer to exception array. */
640 } RPDR, *pRPDR;
641 #define cbRPDR sizeof (RPDR)
642 #define rpdNil ((pRPDR) 0)
644 static struct mips_got_entry *mips_elf_create_local_got_entry
645 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
646 struct mips_elf_link_hash_entry *, int);
647 static bfd_boolean mips_elf_sort_hash_table_f
648 (struct mips_elf_link_hash_entry *, void *);
649 static bfd_vma mips_elf_high
650 (bfd_vma);
651 static bfd_boolean mips_elf_create_dynamic_relocation
652 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
653 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
654 bfd_vma *, asection *);
655 static hashval_t mips_elf_got_entry_hash
656 (const void *);
657 static bfd_vma mips_elf_adjust_gp
658 (bfd *, struct mips_got_info *, bfd *);
659 static struct mips_got_info *mips_elf_got_for_ibfd
660 (struct mips_got_info *, bfd *);
662 /* This will be used when we sort the dynamic relocation records. */
663 static bfd *reldyn_sorting_bfd;
665 /* True if ABFD is for CPUs with load interlocking that include
666 non-MIPS1 CPUs and R3900. */
667 #define LOAD_INTERLOCKS_P(abfd) \
668 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
669 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
671 /* True if ABFD is a PIC object. */
672 #define PIC_OBJECT_P(abfd) \
673 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
675 /* Nonzero if ABFD is using the N32 ABI. */
676 #define ABI_N32_P(abfd) \
677 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
679 /* Nonzero if ABFD is using the N64 ABI. */
680 #define ABI_64_P(abfd) \
681 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
683 /* Nonzero if ABFD is using NewABI conventions. */
684 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
686 /* The IRIX compatibility level we are striving for. */
687 #define IRIX_COMPAT(abfd) \
688 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
690 /* Whether we are trying to be compatible with IRIX at all. */
691 #define SGI_COMPAT(abfd) \
692 (IRIX_COMPAT (abfd) != ict_none)
694 /* The name of the options section. */
695 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
696 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
698 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
699 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
700 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
701 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
703 /* Whether the section is readonly. */
704 #define MIPS_ELF_READONLY_SECTION(sec) \
705 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
706 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
708 /* The name of the stub section. */
709 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
711 /* The size of an external REL relocation. */
712 #define MIPS_ELF_REL_SIZE(abfd) \
713 (get_elf_backend_data (abfd)->s->sizeof_rel)
715 /* The size of an external RELA relocation. */
716 #define MIPS_ELF_RELA_SIZE(abfd) \
717 (get_elf_backend_data (abfd)->s->sizeof_rela)
719 /* The size of an external dynamic table entry. */
720 #define MIPS_ELF_DYN_SIZE(abfd) \
721 (get_elf_backend_data (abfd)->s->sizeof_dyn)
723 /* The size of a GOT entry. */
724 #define MIPS_ELF_GOT_SIZE(abfd) \
725 (get_elf_backend_data (abfd)->s->arch_size / 8)
727 /* The size of a symbol-table entry. */
728 #define MIPS_ELF_SYM_SIZE(abfd) \
729 (get_elf_backend_data (abfd)->s->sizeof_sym)
731 /* The default alignment for sections, as a power of two. */
732 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
733 (get_elf_backend_data (abfd)->s->log_file_align)
735 /* Get word-sized data. */
736 #define MIPS_ELF_GET_WORD(abfd, ptr) \
737 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
739 /* Put out word-sized data. */
740 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
741 (ABI_64_P (abfd) \
742 ? bfd_put_64 (abfd, val, ptr) \
743 : bfd_put_32 (abfd, val, ptr))
745 /* The opcode for word-sized loads (LW or LD). */
746 #define MIPS_ELF_LOAD_WORD(abfd) \
747 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
749 /* Add a dynamic symbol table-entry. */
750 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
751 _bfd_elf_add_dynamic_entry (info, tag, val)
753 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
754 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
756 /* Determine whether the internal relocation of index REL_IDX is REL
757 (zero) or RELA (non-zero). The assumption is that, if there are
758 two relocation sections for this section, one of them is REL and
759 the other is RELA. If the index of the relocation we're testing is
760 in range for the first relocation section, check that the external
761 relocation size is that for RELA. It is also assumed that, if
762 rel_idx is not in range for the first section, and this first
763 section contains REL relocs, then the relocation is in the second
764 section, that is RELA. */
765 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
766 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
767 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
768 > (bfd_vma)(rel_idx)) \
769 == (elf_section_data (sec)->rel_hdr.sh_entsize \
770 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
771 : sizeof (Elf32_External_Rela))))
773 /* The name of the dynamic relocation section. */
774 #define MIPS_ELF_REL_DYN_NAME(INFO) \
775 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
777 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
778 from smaller values. Start with zero, widen, *then* decrement. */
779 #define MINUS_ONE (((bfd_vma)0) - 1)
780 #define MINUS_TWO (((bfd_vma)0) - 2)
782 /* The value to write into got[1] for SVR4 targets, to identify it is
783 a GNU object. The dynamic linker can then use got[1] to store the
784 module pointer. */
785 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
786 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
788 /* The offset of $gp from the beginning of the .got section. */
789 #define ELF_MIPS_GP_OFFSET(INFO) \
790 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
792 /* The maximum size of the GOT for it to be addressable using 16-bit
793 offsets from $gp. */
794 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
796 /* Instructions which appear in a stub. */
797 #define STUB_LW(abfd) \
798 ((ABI_64_P (abfd) \
799 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
800 : 0x8f998010)) /* lw t9,0x8010(gp) */
801 #define STUB_MOVE(abfd) \
802 ((ABI_64_P (abfd) \
803 ? 0x03e0782d /* daddu t7,ra */ \
804 : 0x03e07821)) /* addu t7,ra */
805 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
806 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
807 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
808 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
809 #define STUB_LI16S(abfd, VAL) \
810 ((ABI_64_P (abfd) \
811 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
812 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
814 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
815 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
817 /* The name of the dynamic interpreter. This is put in the .interp
818 section. */
820 #define ELF_DYNAMIC_INTERPRETER(abfd) \
821 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
822 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
823 : "/usr/lib/libc.so.1")
825 #ifdef BFD64
826 #define MNAME(bfd,pre,pos) \
827 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
828 #define ELF_R_SYM(bfd, i) \
829 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
830 #define ELF_R_TYPE(bfd, i) \
831 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
832 #define ELF_R_INFO(bfd, s, t) \
833 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
834 #else
835 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
836 #define ELF_R_SYM(bfd, i) \
837 (ELF32_R_SYM (i))
838 #define ELF_R_TYPE(bfd, i) \
839 (ELF32_R_TYPE (i))
840 #define ELF_R_INFO(bfd, s, t) \
841 (ELF32_R_INFO (s, t))
842 #endif
844 /* The mips16 compiler uses a couple of special sections to handle
845 floating point arguments.
847 Section names that look like .mips16.fn.FNNAME contain stubs that
848 copy floating point arguments from the fp regs to the gp regs and
849 then jump to FNNAME. If any 32 bit function calls FNNAME, the
850 call should be redirected to the stub instead. If no 32 bit
851 function calls FNNAME, the stub should be discarded. We need to
852 consider any reference to the function, not just a call, because
853 if the address of the function is taken we will need the stub,
854 since the address might be passed to a 32 bit function.
856 Section names that look like .mips16.call.FNNAME contain stubs
857 that copy floating point arguments from the gp regs to the fp
858 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
859 then any 16 bit function that calls FNNAME should be redirected
860 to the stub instead. If FNNAME is not a 32 bit function, the
861 stub should be discarded.
863 .mips16.call.fp.FNNAME sections are similar, but contain stubs
864 which call FNNAME and then copy the return value from the fp regs
865 to the gp regs. These stubs store the return value in $18 while
866 calling FNNAME; any function which might call one of these stubs
867 must arrange to save $18 around the call. (This case is not
868 needed for 32 bit functions that call 16 bit functions, because
869 16 bit functions always return floating point values in both
870 $f0/$f1 and $2/$3.)
872 Note that in all cases FNNAME might be defined statically.
873 Therefore, FNNAME is not used literally. Instead, the relocation
874 information will indicate which symbol the section is for.
876 We record any stubs that we find in the symbol table. */
878 #define FN_STUB ".mips16.fn."
879 #define CALL_STUB ".mips16.call."
880 #define CALL_FP_STUB ".mips16.call.fp."
882 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
883 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
884 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
886 /* The format of the first PLT entry in an O32 executable. */
887 static const bfd_vma mips_o32_exec_plt0_entry[] =
889 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
890 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
891 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
892 0x031cc023, /* subu $24, $24, $28 */
893 0x03e07821, /* move $15, $31 */
894 0x0018c082, /* srl $24, $24, 2 */
895 0x0320f809, /* jalr $25 */
896 0x2718fffe /* subu $24, $24, 2 */
899 /* The format of the first PLT entry in an N32 executable. Different
900 because gp ($28) is not available; we use t2 ($14) instead. */
901 static const bfd_vma mips_n32_exec_plt0_entry[] =
903 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
904 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
905 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
906 0x030ec023, /* subu $24, $24, $14 */
907 0x03e07821, /* move $15, $31 */
908 0x0018c082, /* srl $24, $24, 2 */
909 0x0320f809, /* jalr $25 */
910 0x2718fffe /* subu $24, $24, 2 */
913 /* The format of the first PLT entry in an N64 executable. Different
914 from N32 because of the increased size of GOT entries. */
915 static const bfd_vma mips_n64_exec_plt0_entry[] =
917 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
918 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
919 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
920 0x030ec023, /* subu $24, $24, $14 */
921 0x03e07821, /* move $15, $31 */
922 0x0018c0c2, /* srl $24, $24, 3 */
923 0x0320f809, /* jalr $25 */
924 0x2718fffe /* subu $24, $24, 2 */
927 /* The format of subsequent PLT entries. */
928 static const bfd_vma mips_exec_plt_entry[] =
930 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
931 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
932 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
933 0x03200008 /* jr $25 */
936 /* The format of the first PLT entry in a VxWorks executable. */
937 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
939 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
940 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
941 0x8f390008, /* lw t9, 8(t9) */
942 0x00000000, /* nop */
943 0x03200008, /* jr t9 */
944 0x00000000 /* nop */
947 /* The format of subsequent PLT entries. */
948 static const bfd_vma mips_vxworks_exec_plt_entry[] =
950 0x10000000, /* b .PLT_resolver */
951 0x24180000, /* li t8, <pltindex> */
952 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
953 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
954 0x8f390000, /* lw t9, 0(t9) */
955 0x00000000, /* nop */
956 0x03200008, /* jr t9 */
957 0x00000000 /* nop */
960 /* The format of the first PLT entry in a VxWorks shared object. */
961 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
963 0x8f990008, /* lw t9, 8(gp) */
964 0x00000000, /* nop */
965 0x03200008, /* jr t9 */
966 0x00000000, /* nop */
967 0x00000000, /* nop */
968 0x00000000 /* nop */
971 /* The format of subsequent PLT entries. */
972 static const bfd_vma mips_vxworks_shared_plt_entry[] =
974 0x10000000, /* b .PLT_resolver */
975 0x24180000 /* li t8, <pltindex> */
978 /* Look up an entry in a MIPS ELF linker hash table. */
980 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
981 ((struct mips_elf_link_hash_entry *) \
982 elf_link_hash_lookup (&(table)->root, (string), (create), \
983 (copy), (follow)))
985 /* Traverse a MIPS ELF linker hash table. */
987 #define mips_elf_link_hash_traverse(table, func, info) \
988 (elf_link_hash_traverse \
989 (&(table)->root, \
990 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
991 (info)))
993 /* Get the MIPS ELF linker hash table from a link_info structure. */
995 #define mips_elf_hash_table(p) \
996 ((struct mips_elf_link_hash_table *) ((p)->hash))
998 /* Find the base offsets for thread-local storage in this object,
999 for GD/LD and IE/LE respectively. */
1001 #define TP_OFFSET 0x7000
1002 #define DTP_OFFSET 0x8000
1004 static bfd_vma
1005 dtprel_base (struct bfd_link_info *info)
1007 /* If tls_sec is NULL, we should have signalled an error already. */
1008 if (elf_hash_table (info)->tls_sec == NULL)
1009 return 0;
1010 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1013 static bfd_vma
1014 tprel_base (struct bfd_link_info *info)
1016 /* If tls_sec is NULL, we should have signalled an error already. */
1017 if (elf_hash_table (info)->tls_sec == NULL)
1018 return 0;
1019 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1022 /* Create an entry in a MIPS ELF linker hash table. */
1024 static struct bfd_hash_entry *
1025 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1026 struct bfd_hash_table *table, const char *string)
1028 struct mips_elf_link_hash_entry *ret =
1029 (struct mips_elf_link_hash_entry *) entry;
1031 /* Allocate the structure if it has not already been allocated by a
1032 subclass. */
1033 if (ret == NULL)
1034 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1035 if (ret == NULL)
1036 return (struct bfd_hash_entry *) ret;
1038 /* Call the allocation method of the superclass. */
1039 ret = ((struct mips_elf_link_hash_entry *)
1040 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1041 table, string));
1042 if (ret != NULL)
1044 /* Set local fields. */
1045 memset (&ret->esym, 0, sizeof (EXTR));
1046 /* We use -2 as a marker to indicate that the information has
1047 not been set. -1 means there is no associated ifd. */
1048 ret->esym.ifd = -2;
1049 ret->la25_stub = 0;
1050 ret->possibly_dynamic_relocs = 0;
1051 ret->fn_stub = NULL;
1052 ret->call_stub = NULL;
1053 ret->call_fp_stub = NULL;
1054 ret->tls_type = GOT_NORMAL;
1055 ret->global_got_area = GGA_NONE;
1056 ret->readonly_reloc = FALSE;
1057 ret->has_static_relocs = FALSE;
1058 ret->no_fn_stub = FALSE;
1059 ret->need_fn_stub = FALSE;
1060 ret->has_nonpic_branches = FALSE;
1061 ret->needs_lazy_stub = FALSE;
1064 return (struct bfd_hash_entry *) ret;
1067 bfd_boolean
1068 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1070 if (!sec->used_by_bfd)
1072 struct _mips_elf_section_data *sdata;
1073 bfd_size_type amt = sizeof (*sdata);
1075 sdata = bfd_zalloc (abfd, amt);
1076 if (sdata == NULL)
1077 return FALSE;
1078 sec->used_by_bfd = sdata;
1081 return _bfd_elf_new_section_hook (abfd, sec);
1084 /* Read ECOFF debugging information from a .mdebug section into a
1085 ecoff_debug_info structure. */
1087 bfd_boolean
1088 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1089 struct ecoff_debug_info *debug)
1091 HDRR *symhdr;
1092 const struct ecoff_debug_swap *swap;
1093 char *ext_hdr;
1095 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1096 memset (debug, 0, sizeof (*debug));
1098 ext_hdr = bfd_malloc (swap->external_hdr_size);
1099 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1100 goto error_return;
1102 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1103 swap->external_hdr_size))
1104 goto error_return;
1106 symhdr = &debug->symbolic_header;
1107 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1109 /* The symbolic header contains absolute file offsets and sizes to
1110 read. */
1111 #define READ(ptr, offset, count, size, type) \
1112 if (symhdr->count == 0) \
1113 debug->ptr = NULL; \
1114 else \
1116 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1117 debug->ptr = bfd_malloc (amt); \
1118 if (debug->ptr == NULL) \
1119 goto error_return; \
1120 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1121 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1122 goto error_return; \
1125 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1126 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1127 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1128 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1129 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1130 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1131 union aux_ext *);
1132 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1133 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1134 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1135 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1136 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1137 #undef READ
1139 debug->fdr = NULL;
1141 return TRUE;
1143 error_return:
1144 if (ext_hdr != NULL)
1145 free (ext_hdr);
1146 if (debug->line != NULL)
1147 free (debug->line);
1148 if (debug->external_dnr != NULL)
1149 free (debug->external_dnr);
1150 if (debug->external_pdr != NULL)
1151 free (debug->external_pdr);
1152 if (debug->external_sym != NULL)
1153 free (debug->external_sym);
1154 if (debug->external_opt != NULL)
1155 free (debug->external_opt);
1156 if (debug->external_aux != NULL)
1157 free (debug->external_aux);
1158 if (debug->ss != NULL)
1159 free (debug->ss);
1160 if (debug->ssext != NULL)
1161 free (debug->ssext);
1162 if (debug->external_fdr != NULL)
1163 free (debug->external_fdr);
1164 if (debug->external_rfd != NULL)
1165 free (debug->external_rfd);
1166 if (debug->external_ext != NULL)
1167 free (debug->external_ext);
1168 return FALSE;
1171 /* Swap RPDR (runtime procedure table entry) for output. */
1173 static void
1174 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1176 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1177 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1178 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1179 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1180 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1181 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1183 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1184 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1186 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1189 /* Create a runtime procedure table from the .mdebug section. */
1191 static bfd_boolean
1192 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1193 struct bfd_link_info *info, asection *s,
1194 struct ecoff_debug_info *debug)
1196 const struct ecoff_debug_swap *swap;
1197 HDRR *hdr = &debug->symbolic_header;
1198 RPDR *rpdr, *rp;
1199 struct rpdr_ext *erp;
1200 void *rtproc;
1201 struct pdr_ext *epdr;
1202 struct sym_ext *esym;
1203 char *ss, **sv;
1204 char *str;
1205 bfd_size_type size;
1206 bfd_size_type count;
1207 unsigned long sindex;
1208 unsigned long i;
1209 PDR pdr;
1210 SYMR sym;
1211 const char *no_name_func = _("static procedure (no name)");
1213 epdr = NULL;
1214 rpdr = NULL;
1215 esym = NULL;
1216 ss = NULL;
1217 sv = NULL;
1219 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1221 sindex = strlen (no_name_func) + 1;
1222 count = hdr->ipdMax;
1223 if (count > 0)
1225 size = swap->external_pdr_size;
1227 epdr = bfd_malloc (size * count);
1228 if (epdr == NULL)
1229 goto error_return;
1231 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1232 goto error_return;
1234 size = sizeof (RPDR);
1235 rp = rpdr = bfd_malloc (size * count);
1236 if (rpdr == NULL)
1237 goto error_return;
1239 size = sizeof (char *);
1240 sv = bfd_malloc (size * count);
1241 if (sv == NULL)
1242 goto error_return;
1244 count = hdr->isymMax;
1245 size = swap->external_sym_size;
1246 esym = bfd_malloc (size * count);
1247 if (esym == NULL)
1248 goto error_return;
1250 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1251 goto error_return;
1253 count = hdr->issMax;
1254 ss = bfd_malloc (count);
1255 if (ss == NULL)
1256 goto error_return;
1257 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1258 goto error_return;
1260 count = hdr->ipdMax;
1261 for (i = 0; i < (unsigned long) count; i++, rp++)
1263 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1264 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1265 rp->adr = sym.value;
1266 rp->regmask = pdr.regmask;
1267 rp->regoffset = pdr.regoffset;
1268 rp->fregmask = pdr.fregmask;
1269 rp->fregoffset = pdr.fregoffset;
1270 rp->frameoffset = pdr.frameoffset;
1271 rp->framereg = pdr.framereg;
1272 rp->pcreg = pdr.pcreg;
1273 rp->irpss = sindex;
1274 sv[i] = ss + sym.iss;
1275 sindex += strlen (sv[i]) + 1;
1279 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1280 size = BFD_ALIGN (size, 16);
1281 rtproc = bfd_alloc (abfd, size);
1282 if (rtproc == NULL)
1284 mips_elf_hash_table (info)->procedure_count = 0;
1285 goto error_return;
1288 mips_elf_hash_table (info)->procedure_count = count + 2;
1290 erp = rtproc;
1291 memset (erp, 0, sizeof (struct rpdr_ext));
1292 erp++;
1293 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1294 strcpy (str, no_name_func);
1295 str += strlen (no_name_func) + 1;
1296 for (i = 0; i < count; i++)
1298 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1299 strcpy (str, sv[i]);
1300 str += strlen (sv[i]) + 1;
1302 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1304 /* Set the size and contents of .rtproc section. */
1305 s->size = size;
1306 s->contents = rtproc;
1308 /* Skip this section later on (I don't think this currently
1309 matters, but someday it might). */
1310 s->map_head.link_order = NULL;
1312 if (epdr != NULL)
1313 free (epdr);
1314 if (rpdr != NULL)
1315 free (rpdr);
1316 if (esym != NULL)
1317 free (esym);
1318 if (ss != NULL)
1319 free (ss);
1320 if (sv != NULL)
1321 free (sv);
1323 return TRUE;
1325 error_return:
1326 if (epdr != NULL)
1327 free (epdr);
1328 if (rpdr != NULL)
1329 free (rpdr);
1330 if (esym != NULL)
1331 free (esym);
1332 if (ss != NULL)
1333 free (ss);
1334 if (sv != NULL)
1335 free (sv);
1336 return FALSE;
1339 /* We're going to create a stub for H. Create a symbol for the stub's
1340 value and size, to help make the disassembly easier to read. */
1342 static bfd_boolean
1343 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1344 struct mips_elf_link_hash_entry *h,
1345 const char *prefix, asection *s, bfd_vma value,
1346 bfd_vma size)
1348 struct bfd_link_hash_entry *bh;
1349 struct elf_link_hash_entry *elfh;
1350 const char *name;
1352 /* Create a new symbol. */
1353 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1354 bh = NULL;
1355 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1356 BSF_LOCAL, s, value, NULL,
1357 TRUE, FALSE, &bh))
1358 return FALSE;
1360 /* Make it a local function. */
1361 elfh = (struct elf_link_hash_entry *) bh;
1362 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1363 elfh->size = size;
1364 elfh->forced_local = 1;
1365 return TRUE;
1368 /* We're about to redefine H. Create a symbol to represent H's
1369 current value and size, to help make the disassembly easier
1370 to read. */
1372 static bfd_boolean
1373 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1374 struct mips_elf_link_hash_entry *h,
1375 const char *prefix)
1377 struct bfd_link_hash_entry *bh;
1378 struct elf_link_hash_entry *elfh;
1379 const char *name;
1380 asection *s;
1381 bfd_vma value;
1383 /* Read the symbol's value. */
1384 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1385 || h->root.root.type == bfd_link_hash_defweak);
1386 s = h->root.root.u.def.section;
1387 value = h->root.root.u.def.value;
1389 /* Create a new symbol. */
1390 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1391 bh = NULL;
1392 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1393 BSF_LOCAL, s, value, NULL,
1394 TRUE, FALSE, &bh))
1395 return FALSE;
1397 /* Make it local and copy the other attributes from H. */
1398 elfh = (struct elf_link_hash_entry *) bh;
1399 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1400 elfh->other = h->root.other;
1401 elfh->size = h->root.size;
1402 elfh->forced_local = 1;
1403 return TRUE;
1406 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1407 function rather than to a hard-float stub. */
1409 static bfd_boolean
1410 section_allows_mips16_refs_p (asection *section)
1412 const char *name;
1414 name = bfd_get_section_name (section->owner, section);
1415 return (FN_STUB_P (name)
1416 || CALL_STUB_P (name)
1417 || CALL_FP_STUB_P (name)
1418 || strcmp (name, ".pdr") == 0);
1421 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1422 stub section of some kind. Return the R_SYMNDX of the target
1423 function, or 0 if we can't decide which function that is. */
1425 static unsigned long
1426 mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1427 const Elf_Internal_Rela *relocs,
1428 const Elf_Internal_Rela *relend)
1430 const Elf_Internal_Rela *rel;
1432 /* Trust the first R_MIPS_NONE relocation, if any. */
1433 for (rel = relocs; rel < relend; rel++)
1434 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1435 return ELF_R_SYM (sec->owner, rel->r_info);
1437 /* Otherwise trust the first relocation, whatever its kind. This is
1438 the traditional behavior. */
1439 if (relocs < relend)
1440 return ELF_R_SYM (sec->owner, relocs->r_info);
1442 return 0;
1445 /* Check the mips16 stubs for a particular symbol, and see if we can
1446 discard them. */
1448 static void
1449 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1450 struct mips_elf_link_hash_entry *h)
1452 /* Dynamic symbols must use the standard call interface, in case other
1453 objects try to call them. */
1454 if (h->fn_stub != NULL
1455 && h->root.dynindx != -1)
1457 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1458 h->need_fn_stub = TRUE;
1461 if (h->fn_stub != NULL
1462 && ! h->need_fn_stub)
1464 /* We don't need the fn_stub; the only references to this symbol
1465 are 16 bit calls. Clobber the size to 0 to prevent it from
1466 being included in the link. */
1467 h->fn_stub->size = 0;
1468 h->fn_stub->flags &= ~SEC_RELOC;
1469 h->fn_stub->reloc_count = 0;
1470 h->fn_stub->flags |= SEC_EXCLUDE;
1473 if (h->call_stub != NULL
1474 && ELF_ST_IS_MIPS16 (h->root.other))
1476 /* We don't need the call_stub; this is a 16 bit function, so
1477 calls from other 16 bit functions are OK. Clobber the size
1478 to 0 to prevent it from being included in the link. */
1479 h->call_stub->size = 0;
1480 h->call_stub->flags &= ~SEC_RELOC;
1481 h->call_stub->reloc_count = 0;
1482 h->call_stub->flags |= SEC_EXCLUDE;
1485 if (h->call_fp_stub != NULL
1486 && ELF_ST_IS_MIPS16 (h->root.other))
1488 /* We don't need the call_stub; this is a 16 bit function, so
1489 calls from other 16 bit functions are OK. Clobber the size
1490 to 0 to prevent it from being included in the link. */
1491 h->call_fp_stub->size = 0;
1492 h->call_fp_stub->flags &= ~SEC_RELOC;
1493 h->call_fp_stub->reloc_count = 0;
1494 h->call_fp_stub->flags |= SEC_EXCLUDE;
1498 /* Hashtable callbacks for mips_elf_la25_stubs. */
1500 static hashval_t
1501 mips_elf_la25_stub_hash (const void *entry_)
1503 const struct mips_elf_la25_stub *entry;
1505 entry = (struct mips_elf_la25_stub *) entry_;
1506 return entry->h->root.root.u.def.section->id
1507 + entry->h->root.root.u.def.value;
1510 static int
1511 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1513 const struct mips_elf_la25_stub *entry1, *entry2;
1515 entry1 = (struct mips_elf_la25_stub *) entry1_;
1516 entry2 = (struct mips_elf_la25_stub *) entry2_;
1517 return ((entry1->h->root.root.u.def.section
1518 == entry2->h->root.root.u.def.section)
1519 && (entry1->h->root.root.u.def.value
1520 == entry2->h->root.root.u.def.value));
1523 /* Called by the linker to set up the la25 stub-creation code. FN is
1524 the linker's implementation of add_stub_function. Return true on
1525 success. */
1527 bfd_boolean
1528 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1529 asection *(*fn) (const char *, asection *,
1530 asection *))
1532 struct mips_elf_link_hash_table *htab;
1534 htab = mips_elf_hash_table (info);
1535 htab->add_stub_section = fn;
1536 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1537 mips_elf_la25_stub_eq, NULL);
1538 if (htab->la25_stubs == NULL)
1539 return FALSE;
1541 return TRUE;
1544 /* Return true if H is a locally-defined PIC function, in the sense
1545 that it might need $25 to be valid on entry. Note that MIPS16
1546 functions never need $25 to be valid on entry; they set up $gp
1547 using PC-relative instructions instead. */
1549 static bfd_boolean
1550 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1552 return ((h->root.root.type == bfd_link_hash_defined
1553 || h->root.root.type == bfd_link_hash_defweak)
1554 && h->root.def_regular
1555 && !bfd_is_abs_section (h->root.root.u.def.section)
1556 && !ELF_ST_IS_MIPS16 (h->root.other)
1557 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1558 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1561 /* STUB describes an la25 stub that we have decided to implement
1562 by inserting an LUI/ADDIU pair before the target function.
1563 Create the section and redirect the function symbol to it. */
1565 static bfd_boolean
1566 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1567 struct bfd_link_info *info)
1569 struct mips_elf_link_hash_table *htab;
1570 char *name;
1571 asection *s, *input_section;
1572 unsigned int align;
1574 htab = mips_elf_hash_table (info);
1576 /* Create a unique name for the new section. */
1577 name = bfd_malloc (11 + sizeof (".text.stub."));
1578 if (name == NULL)
1579 return FALSE;
1580 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1582 /* Create the section. */
1583 input_section = stub->h->root.root.u.def.section;
1584 s = htab->add_stub_section (name, input_section,
1585 input_section->output_section);
1586 if (s == NULL)
1587 return FALSE;
1589 /* Make sure that any padding goes before the stub. */
1590 align = input_section->alignment_power;
1591 if (!bfd_set_section_alignment (s->owner, s, align))
1592 return FALSE;
1593 if (align > 3)
1594 s->size = (1 << align) - 8;
1596 /* Create a symbol for the stub. */
1597 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1598 stub->stub_section = s;
1599 stub->offset = s->size;
1601 /* Allocate room for it. */
1602 s->size += 8;
1603 return TRUE;
1606 /* STUB describes an la25 stub that we have decided to implement
1607 with a separate trampoline. Allocate room for it and redirect
1608 the function symbol to it. */
1610 static bfd_boolean
1611 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1612 struct bfd_link_info *info)
1614 struct mips_elf_link_hash_table *htab;
1615 asection *s;
1617 htab = mips_elf_hash_table (info);
1619 /* Create a trampoline section, if we haven't already. */
1620 s = htab->strampoline;
1621 if (s == NULL)
1623 asection *input_section = stub->h->root.root.u.def.section;
1624 s = htab->add_stub_section (".text", NULL,
1625 input_section->output_section);
1626 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1627 return FALSE;
1628 htab->strampoline = s;
1631 /* Create a symbol for the stub. */
1632 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1633 stub->stub_section = s;
1634 stub->offset = s->size;
1636 /* Allocate room for it. */
1637 s->size += 16;
1638 return TRUE;
1641 /* H describes a symbol that needs an la25 stub. Make sure that an
1642 appropriate stub exists and point H at it. */
1644 static bfd_boolean
1645 mips_elf_add_la25_stub (struct bfd_link_info *info,
1646 struct mips_elf_link_hash_entry *h)
1648 struct mips_elf_link_hash_table *htab;
1649 struct mips_elf_la25_stub search, *stub;
1650 bfd_boolean use_trampoline_p;
1651 asection *s;
1652 bfd_vma value;
1653 void **slot;
1655 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1656 of the section and if we would need no more than 2 nops. */
1657 s = h->root.root.u.def.section;
1658 value = h->root.root.u.def.value;
1659 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1661 /* Describe the stub we want. */
1662 search.stub_section = NULL;
1663 search.offset = 0;
1664 search.h = h;
1666 /* See if we've already created an equivalent stub. */
1667 htab = mips_elf_hash_table (info);
1668 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1669 if (slot == NULL)
1670 return FALSE;
1672 stub = (struct mips_elf_la25_stub *) *slot;
1673 if (stub != NULL)
1675 /* We can reuse the existing stub. */
1676 h->la25_stub = stub;
1677 return TRUE;
1680 /* Create a permanent copy of ENTRY and add it to the hash table. */
1681 stub = bfd_malloc (sizeof (search));
1682 if (stub == NULL)
1683 return FALSE;
1684 *stub = search;
1685 *slot = stub;
1687 h->la25_stub = stub;
1688 return (use_trampoline_p
1689 ? mips_elf_add_la25_trampoline (stub, info)
1690 : mips_elf_add_la25_intro (stub, info));
1693 /* A mips_elf_link_hash_traverse callback that is called before sizing
1694 sections. DATA points to a mips_htab_traverse_info structure. */
1696 static bfd_boolean
1697 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1699 struct mips_htab_traverse_info *hti;
1701 hti = (struct mips_htab_traverse_info *) data;
1702 if (h->root.root.type == bfd_link_hash_warning)
1703 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1705 if (!hti->info->relocatable)
1706 mips_elf_check_mips16_stubs (hti->info, h);
1708 if (mips_elf_local_pic_function_p (h))
1710 /* H is a function that might need $25 to be valid on entry.
1711 If we're creating a non-PIC relocatable object, mark H as
1712 being PIC. If we're creating a non-relocatable object with
1713 non-PIC branches and jumps to H, make sure that H has an la25
1714 stub. */
1715 if (hti->info->relocatable)
1717 if (!PIC_OBJECT_P (hti->output_bfd))
1718 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1720 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1722 hti->error = TRUE;
1723 return FALSE;
1726 return TRUE;
1729 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1730 Most mips16 instructions are 16 bits, but these instructions
1731 are 32 bits.
1733 The format of these instructions is:
1735 +--------------+--------------------------------+
1736 | JALX | X| Imm 20:16 | Imm 25:21 |
1737 +--------------+--------------------------------+
1738 | Immediate 15:0 |
1739 +-----------------------------------------------+
1741 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1742 Note that the immediate value in the first word is swapped.
1744 When producing a relocatable object file, R_MIPS16_26 is
1745 handled mostly like R_MIPS_26. In particular, the addend is
1746 stored as a straight 26-bit value in a 32-bit instruction.
1747 (gas makes life simpler for itself by never adjusting a
1748 R_MIPS16_26 reloc to be against a section, so the addend is
1749 always zero). However, the 32 bit instruction is stored as 2
1750 16-bit values, rather than a single 32-bit value. In a
1751 big-endian file, the result is the same; in a little-endian
1752 file, the two 16-bit halves of the 32 bit value are swapped.
1753 This is so that a disassembler can recognize the jal
1754 instruction.
1756 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1757 instruction stored as two 16-bit values. The addend A is the
1758 contents of the targ26 field. The calculation is the same as
1759 R_MIPS_26. When storing the calculated value, reorder the
1760 immediate value as shown above, and don't forget to store the
1761 value as two 16-bit values.
1763 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1764 defined as
1766 big-endian:
1767 +--------+----------------------+
1768 | | |
1769 | | targ26-16 |
1770 |31 26|25 0|
1771 +--------+----------------------+
1773 little-endian:
1774 +----------+------+-------------+
1775 | | | |
1776 | sub1 | | sub2 |
1777 |0 9|10 15|16 31|
1778 +----------+--------------------+
1779 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1780 ((sub1 << 16) | sub2)).
1782 When producing a relocatable object file, the calculation is
1783 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1784 When producing a fully linked file, the calculation is
1785 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1786 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1788 The table below lists the other MIPS16 instruction relocations.
1789 Each one is calculated in the same way as the non-MIPS16 relocation
1790 given on the right, but using the extended MIPS16 layout of 16-bit
1791 immediate fields:
1793 R_MIPS16_GPREL R_MIPS_GPREL16
1794 R_MIPS16_GOT16 R_MIPS_GOT16
1795 R_MIPS16_CALL16 R_MIPS_CALL16
1796 R_MIPS16_HI16 R_MIPS_HI16
1797 R_MIPS16_LO16 R_MIPS_LO16
1799 A typical instruction will have a format like this:
1801 +--------------+--------------------------------+
1802 | EXTEND | Imm 10:5 | Imm 15:11 |
1803 +--------------+--------------------------------+
1804 | Major | rx | ry | Imm 4:0 |
1805 +--------------+--------------------------------+
1807 EXTEND is the five bit value 11110. Major is the instruction
1808 opcode.
1810 All we need to do here is shuffle the bits appropriately.
1811 As above, the two 16-bit halves must be swapped on a
1812 little-endian system. */
1814 static inline bfd_boolean
1815 mips16_reloc_p (int r_type)
1817 switch (r_type)
1819 case R_MIPS16_26:
1820 case R_MIPS16_GPREL:
1821 case R_MIPS16_GOT16:
1822 case R_MIPS16_CALL16:
1823 case R_MIPS16_HI16:
1824 case R_MIPS16_LO16:
1825 return TRUE;
1827 default:
1828 return FALSE;
1832 static inline bfd_boolean
1833 got16_reloc_p (int r_type)
1835 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1838 static inline bfd_boolean
1839 call16_reloc_p (int r_type)
1841 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1844 static inline bfd_boolean
1845 hi16_reloc_p (int r_type)
1847 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1850 static inline bfd_boolean
1851 lo16_reloc_p (int r_type)
1853 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1856 static inline bfd_boolean
1857 mips16_call_reloc_p (int r_type)
1859 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1862 void
1863 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1864 bfd_boolean jal_shuffle, bfd_byte *data)
1866 bfd_vma extend, insn, val;
1868 if (!mips16_reloc_p (r_type))
1869 return;
1871 /* Pick up the mips16 extend instruction and the real instruction. */
1872 extend = bfd_get_16 (abfd, data);
1873 insn = bfd_get_16 (abfd, data + 2);
1874 if (r_type == R_MIPS16_26)
1876 if (jal_shuffle)
1877 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1878 | ((extend & 0x1f) << 21) | insn;
1879 else
1880 val = extend << 16 | insn;
1882 else
1883 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1884 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1885 bfd_put_32 (abfd, val, data);
1888 void
1889 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1890 bfd_boolean jal_shuffle, bfd_byte *data)
1892 bfd_vma extend, insn, val;
1894 if (!mips16_reloc_p (r_type))
1895 return;
1897 val = bfd_get_32 (abfd, data);
1898 if (r_type == R_MIPS16_26)
1900 if (jal_shuffle)
1902 insn = val & 0xffff;
1903 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1904 | ((val >> 21) & 0x1f);
1906 else
1908 insn = val & 0xffff;
1909 extend = val >> 16;
1912 else
1914 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1915 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1917 bfd_put_16 (abfd, insn, data + 2);
1918 bfd_put_16 (abfd, extend, data);
1921 bfd_reloc_status_type
1922 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1923 arelent *reloc_entry, asection *input_section,
1924 bfd_boolean relocatable, void *data, bfd_vma gp)
1926 bfd_vma relocation;
1927 bfd_signed_vma val;
1928 bfd_reloc_status_type status;
1930 if (bfd_is_com_section (symbol->section))
1931 relocation = 0;
1932 else
1933 relocation = symbol->value;
1935 relocation += symbol->section->output_section->vma;
1936 relocation += symbol->section->output_offset;
1938 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1939 return bfd_reloc_outofrange;
1941 /* Set val to the offset into the section or symbol. */
1942 val = reloc_entry->addend;
1944 _bfd_mips_elf_sign_extend (val, 16);
1946 /* Adjust val for the final section location and GP value. If we
1947 are producing relocatable output, we don't want to do this for
1948 an external symbol. */
1949 if (! relocatable
1950 || (symbol->flags & BSF_SECTION_SYM) != 0)
1951 val += relocation - gp;
1953 if (reloc_entry->howto->partial_inplace)
1955 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1956 (bfd_byte *) data
1957 + reloc_entry->address);
1958 if (status != bfd_reloc_ok)
1959 return status;
1961 else
1962 reloc_entry->addend = val;
1964 if (relocatable)
1965 reloc_entry->address += input_section->output_offset;
1967 return bfd_reloc_ok;
1970 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1971 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1972 that contains the relocation field and DATA points to the start of
1973 INPUT_SECTION. */
1975 struct mips_hi16
1977 struct mips_hi16 *next;
1978 bfd_byte *data;
1979 asection *input_section;
1980 arelent rel;
1983 /* FIXME: This should not be a static variable. */
1985 static struct mips_hi16 *mips_hi16_list;
1987 /* A howto special_function for REL *HI16 relocations. We can only
1988 calculate the correct value once we've seen the partnering
1989 *LO16 relocation, so just save the information for later.
1991 The ABI requires that the *LO16 immediately follow the *HI16.
1992 However, as a GNU extension, we permit an arbitrary number of
1993 *HI16s to be associated with a single *LO16. This significantly
1994 simplies the relocation handling in gcc. */
1996 bfd_reloc_status_type
1997 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1998 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1999 asection *input_section, bfd *output_bfd,
2000 char **error_message ATTRIBUTE_UNUSED)
2002 struct mips_hi16 *n;
2004 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2005 return bfd_reloc_outofrange;
2007 n = bfd_malloc (sizeof *n);
2008 if (n == NULL)
2009 return bfd_reloc_outofrange;
2011 n->next = mips_hi16_list;
2012 n->data = data;
2013 n->input_section = input_section;
2014 n->rel = *reloc_entry;
2015 mips_hi16_list = n;
2017 if (output_bfd != NULL)
2018 reloc_entry->address += input_section->output_offset;
2020 return bfd_reloc_ok;
2023 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2024 like any other 16-bit relocation when applied to global symbols, but is
2025 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2027 bfd_reloc_status_type
2028 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2029 void *data, asection *input_section,
2030 bfd *output_bfd, char **error_message)
2032 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2033 || bfd_is_und_section (bfd_get_section (symbol))
2034 || bfd_is_com_section (bfd_get_section (symbol)))
2035 /* The relocation is against a global symbol. */
2036 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2037 input_section, output_bfd,
2038 error_message);
2040 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2041 input_section, output_bfd, error_message);
2044 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2045 is a straightforward 16 bit inplace relocation, but we must deal with
2046 any partnering high-part relocations as well. */
2048 bfd_reloc_status_type
2049 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2050 void *data, asection *input_section,
2051 bfd *output_bfd, char **error_message)
2053 bfd_vma vallo;
2054 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2056 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2057 return bfd_reloc_outofrange;
2059 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2060 location);
2061 vallo = bfd_get_32 (abfd, location);
2062 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2063 location);
2065 while (mips_hi16_list != NULL)
2067 bfd_reloc_status_type ret;
2068 struct mips_hi16 *hi;
2070 hi = mips_hi16_list;
2072 /* R_MIPS*_GOT16 relocations are something of a special case. We
2073 want to install the addend in the same way as for a R_MIPS*_HI16
2074 relocation (with a rightshift of 16). However, since GOT16
2075 relocations can also be used with global symbols, their howto
2076 has a rightshift of 0. */
2077 if (hi->rel.howto->type == R_MIPS_GOT16)
2078 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2079 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2080 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2082 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2083 carry or borrow will induce a change of +1 or -1 in the high part. */
2084 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2086 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2087 hi->input_section, output_bfd,
2088 error_message);
2089 if (ret != bfd_reloc_ok)
2090 return ret;
2092 mips_hi16_list = hi->next;
2093 free (hi);
2096 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2097 input_section, output_bfd,
2098 error_message);
2101 /* A generic howto special_function. This calculates and installs the
2102 relocation itself, thus avoiding the oft-discussed problems in
2103 bfd_perform_relocation and bfd_install_relocation. */
2105 bfd_reloc_status_type
2106 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2107 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2108 asection *input_section, bfd *output_bfd,
2109 char **error_message ATTRIBUTE_UNUSED)
2111 bfd_signed_vma val;
2112 bfd_reloc_status_type status;
2113 bfd_boolean relocatable;
2115 relocatable = (output_bfd != NULL);
2117 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2118 return bfd_reloc_outofrange;
2120 /* Build up the field adjustment in VAL. */
2121 val = 0;
2122 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2124 /* Either we're calculating the final field value or we have a
2125 relocation against a section symbol. Add in the section's
2126 offset or address. */
2127 val += symbol->section->output_section->vma;
2128 val += symbol->section->output_offset;
2131 if (!relocatable)
2133 /* We're calculating the final field value. Add in the symbol's value
2134 and, if pc-relative, subtract the address of the field itself. */
2135 val += symbol->value;
2136 if (reloc_entry->howto->pc_relative)
2138 val -= input_section->output_section->vma;
2139 val -= input_section->output_offset;
2140 val -= reloc_entry->address;
2144 /* VAL is now the final adjustment. If we're keeping this relocation
2145 in the output file, and if the relocation uses a separate addend,
2146 we just need to add VAL to that addend. Otherwise we need to add
2147 VAL to the relocation field itself. */
2148 if (relocatable && !reloc_entry->howto->partial_inplace)
2149 reloc_entry->addend += val;
2150 else
2152 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2154 /* Add in the separate addend, if any. */
2155 val += reloc_entry->addend;
2157 /* Add VAL to the relocation field. */
2158 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2159 location);
2160 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2161 location);
2162 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2163 location);
2165 if (status != bfd_reloc_ok)
2166 return status;
2169 if (relocatable)
2170 reloc_entry->address += input_section->output_offset;
2172 return bfd_reloc_ok;
2175 /* Swap an entry in a .gptab section. Note that these routines rely
2176 on the equivalence of the two elements of the union. */
2178 static void
2179 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2180 Elf32_gptab *in)
2182 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2183 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2186 static void
2187 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2188 Elf32_External_gptab *ex)
2190 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2191 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2194 static void
2195 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2196 Elf32_External_compact_rel *ex)
2198 H_PUT_32 (abfd, in->id1, ex->id1);
2199 H_PUT_32 (abfd, in->num, ex->num);
2200 H_PUT_32 (abfd, in->id2, ex->id2);
2201 H_PUT_32 (abfd, in->offset, ex->offset);
2202 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2203 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2206 static void
2207 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2208 Elf32_External_crinfo *ex)
2210 unsigned long l;
2212 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2213 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2214 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2215 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2216 H_PUT_32 (abfd, l, ex->info);
2217 H_PUT_32 (abfd, in->konst, ex->konst);
2218 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2221 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2222 routines swap this structure in and out. They are used outside of
2223 BFD, so they are globally visible. */
2225 void
2226 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2227 Elf32_RegInfo *in)
2229 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2230 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2231 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2232 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2233 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2234 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2237 void
2238 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2239 Elf32_External_RegInfo *ex)
2241 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2242 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2243 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2244 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2245 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2246 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2249 /* In the 64 bit ABI, the .MIPS.options section holds register
2250 information in an Elf64_Reginfo structure. These routines swap
2251 them in and out. They are globally visible because they are used
2252 outside of BFD. These routines are here so that gas can call them
2253 without worrying about whether the 64 bit ABI has been included. */
2255 void
2256 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2257 Elf64_Internal_RegInfo *in)
2259 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2260 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2261 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2262 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2263 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2264 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2265 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2268 void
2269 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2270 Elf64_External_RegInfo *ex)
2272 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2273 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2274 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2275 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2276 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2277 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2278 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2281 /* Swap in an options header. */
2283 void
2284 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2285 Elf_Internal_Options *in)
2287 in->kind = H_GET_8 (abfd, ex->kind);
2288 in->size = H_GET_8 (abfd, ex->size);
2289 in->section = H_GET_16 (abfd, ex->section);
2290 in->info = H_GET_32 (abfd, ex->info);
2293 /* Swap out an options header. */
2295 void
2296 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2297 Elf_External_Options *ex)
2299 H_PUT_8 (abfd, in->kind, ex->kind);
2300 H_PUT_8 (abfd, in->size, ex->size);
2301 H_PUT_16 (abfd, in->section, ex->section);
2302 H_PUT_32 (abfd, in->info, ex->info);
2305 /* This function is called via qsort() to sort the dynamic relocation
2306 entries by increasing r_symndx value. */
2308 static int
2309 sort_dynamic_relocs (const void *arg1, const void *arg2)
2311 Elf_Internal_Rela int_reloc1;
2312 Elf_Internal_Rela int_reloc2;
2313 int diff;
2315 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2316 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2318 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2319 if (diff != 0)
2320 return diff;
2322 if (int_reloc1.r_offset < int_reloc2.r_offset)
2323 return -1;
2324 if (int_reloc1.r_offset > int_reloc2.r_offset)
2325 return 1;
2326 return 0;
2329 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2331 static int
2332 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2333 const void *arg2 ATTRIBUTE_UNUSED)
2335 #ifdef BFD64
2336 Elf_Internal_Rela int_reloc1[3];
2337 Elf_Internal_Rela int_reloc2[3];
2339 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2340 (reldyn_sorting_bfd, arg1, int_reloc1);
2341 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2342 (reldyn_sorting_bfd, arg2, int_reloc2);
2344 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2345 return -1;
2346 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2347 return 1;
2349 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2350 return -1;
2351 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2352 return 1;
2353 return 0;
2354 #else
2355 abort ();
2356 #endif
2360 /* This routine is used to write out ECOFF debugging external symbol
2361 information. It is called via mips_elf_link_hash_traverse. The
2362 ECOFF external symbol information must match the ELF external
2363 symbol information. Unfortunately, at this point we don't know
2364 whether a symbol is required by reloc information, so the two
2365 tables may wind up being different. We must sort out the external
2366 symbol information before we can set the final size of the .mdebug
2367 section, and we must set the size of the .mdebug section before we
2368 can relocate any sections, and we can't know which symbols are
2369 required by relocation until we relocate the sections.
2370 Fortunately, it is relatively unlikely that any symbol will be
2371 stripped but required by a reloc. In particular, it can not happen
2372 when generating a final executable. */
2374 static bfd_boolean
2375 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2377 struct extsym_info *einfo = data;
2378 bfd_boolean strip;
2379 asection *sec, *output_section;
2381 if (h->root.root.type == bfd_link_hash_warning)
2382 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2384 if (h->root.indx == -2)
2385 strip = FALSE;
2386 else if ((h->root.def_dynamic
2387 || h->root.ref_dynamic
2388 || h->root.type == bfd_link_hash_new)
2389 && !h->root.def_regular
2390 && !h->root.ref_regular)
2391 strip = TRUE;
2392 else if (einfo->info->strip == strip_all
2393 || (einfo->info->strip == strip_some
2394 && bfd_hash_lookup (einfo->info->keep_hash,
2395 h->root.root.root.string,
2396 FALSE, FALSE) == NULL))
2397 strip = TRUE;
2398 else
2399 strip = FALSE;
2401 if (strip)
2402 return TRUE;
2404 if (h->esym.ifd == -2)
2406 h->esym.jmptbl = 0;
2407 h->esym.cobol_main = 0;
2408 h->esym.weakext = 0;
2409 h->esym.reserved = 0;
2410 h->esym.ifd = ifdNil;
2411 h->esym.asym.value = 0;
2412 h->esym.asym.st = stGlobal;
2414 if (h->root.root.type == bfd_link_hash_undefined
2415 || h->root.root.type == bfd_link_hash_undefweak)
2417 const char *name;
2419 /* Use undefined class. Also, set class and type for some
2420 special symbols. */
2421 name = h->root.root.root.string;
2422 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2423 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2425 h->esym.asym.sc = scData;
2426 h->esym.asym.st = stLabel;
2427 h->esym.asym.value = 0;
2429 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2431 h->esym.asym.sc = scAbs;
2432 h->esym.asym.st = stLabel;
2433 h->esym.asym.value =
2434 mips_elf_hash_table (einfo->info)->procedure_count;
2436 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2438 h->esym.asym.sc = scAbs;
2439 h->esym.asym.st = stLabel;
2440 h->esym.asym.value = elf_gp (einfo->abfd);
2442 else
2443 h->esym.asym.sc = scUndefined;
2445 else if (h->root.root.type != bfd_link_hash_defined
2446 && h->root.root.type != bfd_link_hash_defweak)
2447 h->esym.asym.sc = scAbs;
2448 else
2450 const char *name;
2452 sec = h->root.root.u.def.section;
2453 output_section = sec->output_section;
2455 /* When making a shared library and symbol h is the one from
2456 the another shared library, OUTPUT_SECTION may be null. */
2457 if (output_section == NULL)
2458 h->esym.asym.sc = scUndefined;
2459 else
2461 name = bfd_section_name (output_section->owner, output_section);
2463 if (strcmp (name, ".text") == 0)
2464 h->esym.asym.sc = scText;
2465 else if (strcmp (name, ".data") == 0)
2466 h->esym.asym.sc = scData;
2467 else if (strcmp (name, ".sdata") == 0)
2468 h->esym.asym.sc = scSData;
2469 else if (strcmp (name, ".rodata") == 0
2470 || strcmp (name, ".rdata") == 0)
2471 h->esym.asym.sc = scRData;
2472 else if (strcmp (name, ".bss") == 0)
2473 h->esym.asym.sc = scBss;
2474 else if (strcmp (name, ".sbss") == 0)
2475 h->esym.asym.sc = scSBss;
2476 else if (strcmp (name, ".init") == 0)
2477 h->esym.asym.sc = scInit;
2478 else if (strcmp (name, ".fini") == 0)
2479 h->esym.asym.sc = scFini;
2480 else
2481 h->esym.asym.sc = scAbs;
2485 h->esym.asym.reserved = 0;
2486 h->esym.asym.index = indexNil;
2489 if (h->root.root.type == bfd_link_hash_common)
2490 h->esym.asym.value = h->root.root.u.c.size;
2491 else if (h->root.root.type == bfd_link_hash_defined
2492 || h->root.root.type == bfd_link_hash_defweak)
2494 if (h->esym.asym.sc == scCommon)
2495 h->esym.asym.sc = scBss;
2496 else if (h->esym.asym.sc == scSCommon)
2497 h->esym.asym.sc = scSBss;
2499 sec = h->root.root.u.def.section;
2500 output_section = sec->output_section;
2501 if (output_section != NULL)
2502 h->esym.asym.value = (h->root.root.u.def.value
2503 + sec->output_offset
2504 + output_section->vma);
2505 else
2506 h->esym.asym.value = 0;
2508 else
2510 struct mips_elf_link_hash_entry *hd = h;
2512 while (hd->root.root.type == bfd_link_hash_indirect)
2513 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2515 if (hd->needs_lazy_stub)
2517 /* Set type and value for a symbol with a function stub. */
2518 h->esym.asym.st = stProc;
2519 sec = hd->root.root.u.def.section;
2520 if (sec == NULL)
2521 h->esym.asym.value = 0;
2522 else
2524 output_section = sec->output_section;
2525 if (output_section != NULL)
2526 h->esym.asym.value = (hd->root.plt.offset
2527 + sec->output_offset
2528 + output_section->vma);
2529 else
2530 h->esym.asym.value = 0;
2535 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2536 h->root.root.root.string,
2537 &h->esym))
2539 einfo->failed = TRUE;
2540 return FALSE;
2543 return TRUE;
2546 /* A comparison routine used to sort .gptab entries. */
2548 static int
2549 gptab_compare (const void *p1, const void *p2)
2551 const Elf32_gptab *a1 = p1;
2552 const Elf32_gptab *a2 = p2;
2554 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2557 /* Functions to manage the got entry hash table. */
2559 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2560 hash number. */
2562 static INLINE hashval_t
2563 mips_elf_hash_bfd_vma (bfd_vma addr)
2565 #ifdef BFD64
2566 return addr + (addr >> 32);
2567 #else
2568 return addr;
2569 #endif
2572 /* got_entries only match if they're identical, except for gotidx, so
2573 use all fields to compute the hash, and compare the appropriate
2574 union members. */
2576 static hashval_t
2577 mips_elf_got_entry_hash (const void *entry_)
2579 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2581 return entry->symndx
2582 + ((entry->tls_type & GOT_TLS_LDM) << 17)
2583 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2584 : entry->abfd->id
2585 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2586 : entry->d.h->root.root.root.hash));
2589 static int
2590 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2592 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2593 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2595 /* An LDM entry can only match another LDM entry. */
2596 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2597 return 0;
2599 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2600 && (! e1->abfd ? e1->d.address == e2->d.address
2601 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2602 : e1->d.h == e2->d.h);
2605 /* multi_got_entries are still a match in the case of global objects,
2606 even if the input bfd in which they're referenced differs, so the
2607 hash computation and compare functions are adjusted
2608 accordingly. */
2610 static hashval_t
2611 mips_elf_multi_got_entry_hash (const void *entry_)
2613 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2615 return entry->symndx
2616 + (! entry->abfd
2617 ? mips_elf_hash_bfd_vma (entry->d.address)
2618 : entry->symndx >= 0
2619 ? ((entry->tls_type & GOT_TLS_LDM)
2620 ? (GOT_TLS_LDM << 17)
2621 : (entry->abfd->id
2622 + mips_elf_hash_bfd_vma (entry->d.addend)))
2623 : entry->d.h->root.root.root.hash);
2626 static int
2627 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2629 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2630 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2632 /* Any two LDM entries match. */
2633 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2634 return 1;
2636 /* Nothing else matches an LDM entry. */
2637 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2638 return 0;
2640 return e1->symndx == e2->symndx
2641 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2642 : e1->abfd == NULL || e2->abfd == NULL
2643 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2644 : e1->d.h == e2->d.h);
2647 static hashval_t
2648 mips_got_page_entry_hash (const void *entry_)
2650 const struct mips_got_page_entry *entry;
2652 entry = (const struct mips_got_page_entry *) entry_;
2653 return entry->abfd->id + entry->symndx;
2656 static int
2657 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2659 const struct mips_got_page_entry *entry1, *entry2;
2661 entry1 = (const struct mips_got_page_entry *) entry1_;
2662 entry2 = (const struct mips_got_page_entry *) entry2_;
2663 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2666 /* Return the dynamic relocation section. If it doesn't exist, try to
2667 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2668 if creation fails. */
2670 static asection *
2671 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2673 const char *dname;
2674 asection *sreloc;
2675 bfd *dynobj;
2677 dname = MIPS_ELF_REL_DYN_NAME (info);
2678 dynobj = elf_hash_table (info)->dynobj;
2679 sreloc = bfd_get_section_by_name (dynobj, dname);
2680 if (sreloc == NULL && create_p)
2682 sreloc = bfd_make_section_with_flags (dynobj, dname,
2683 (SEC_ALLOC
2684 | SEC_LOAD
2685 | SEC_HAS_CONTENTS
2686 | SEC_IN_MEMORY
2687 | SEC_LINKER_CREATED
2688 | SEC_READONLY));
2689 if (sreloc == NULL
2690 || ! bfd_set_section_alignment (dynobj, sreloc,
2691 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2692 return NULL;
2694 return sreloc;
2697 /* Count the number of relocations needed for a TLS GOT entry, with
2698 access types from TLS_TYPE, and symbol H (or a local symbol if H
2699 is NULL). */
2701 static int
2702 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2703 struct elf_link_hash_entry *h)
2705 int indx = 0;
2706 int ret = 0;
2707 bfd_boolean need_relocs = FALSE;
2708 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2710 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2711 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2712 indx = h->dynindx;
2714 if ((info->shared || indx != 0)
2715 && (h == NULL
2716 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2717 || h->root.type != bfd_link_hash_undefweak))
2718 need_relocs = TRUE;
2720 if (!need_relocs)
2721 return FALSE;
2723 if (tls_type & GOT_TLS_GD)
2725 ret++;
2726 if (indx != 0)
2727 ret++;
2730 if (tls_type & GOT_TLS_IE)
2731 ret++;
2733 if ((tls_type & GOT_TLS_LDM) && info->shared)
2734 ret++;
2736 return ret;
2739 /* Count the number of TLS relocations required for the GOT entry in
2740 ARG1, if it describes a local symbol. */
2742 static int
2743 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2745 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2746 struct mips_elf_count_tls_arg *arg = arg2;
2748 if (entry->abfd != NULL && entry->symndx != -1)
2749 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2751 return 1;
2754 /* Count the number of TLS GOT entries required for the global (or
2755 forced-local) symbol in ARG1. */
2757 static int
2758 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2760 struct mips_elf_link_hash_entry *hm
2761 = (struct mips_elf_link_hash_entry *) arg1;
2762 struct mips_elf_count_tls_arg *arg = arg2;
2764 if (hm->tls_type & GOT_TLS_GD)
2765 arg->needed += 2;
2766 if (hm->tls_type & GOT_TLS_IE)
2767 arg->needed += 1;
2769 return 1;
2772 /* Count the number of TLS relocations required for the global (or
2773 forced-local) symbol in ARG1. */
2775 static int
2776 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2778 struct mips_elf_link_hash_entry *hm
2779 = (struct mips_elf_link_hash_entry *) arg1;
2780 struct mips_elf_count_tls_arg *arg = arg2;
2782 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2784 return 1;
2787 /* Output a simple dynamic relocation into SRELOC. */
2789 static void
2790 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2791 asection *sreloc,
2792 unsigned long reloc_index,
2793 unsigned long indx,
2794 int r_type,
2795 bfd_vma offset)
2797 Elf_Internal_Rela rel[3];
2799 memset (rel, 0, sizeof (rel));
2801 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2802 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2804 if (ABI_64_P (output_bfd))
2806 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2807 (output_bfd, &rel[0],
2808 (sreloc->contents
2809 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
2811 else
2812 bfd_elf32_swap_reloc_out
2813 (output_bfd, &rel[0],
2814 (sreloc->contents
2815 + reloc_index * sizeof (Elf32_External_Rel)));
2818 /* Initialize a set of TLS GOT entries for one symbol. */
2820 static void
2821 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2822 unsigned char *tls_type_p,
2823 struct bfd_link_info *info,
2824 struct mips_elf_link_hash_entry *h,
2825 bfd_vma value)
2827 struct mips_elf_link_hash_table *htab;
2828 int indx;
2829 asection *sreloc, *sgot;
2830 bfd_vma offset, offset2;
2831 bfd_boolean need_relocs = FALSE;
2833 htab = mips_elf_hash_table (info);
2834 sgot = htab->sgot;
2836 indx = 0;
2837 if (h != NULL)
2839 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2841 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2842 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2843 indx = h->root.dynindx;
2846 if (*tls_type_p & GOT_TLS_DONE)
2847 return;
2849 if ((info->shared || indx != 0)
2850 && (h == NULL
2851 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2852 || h->root.type != bfd_link_hash_undefweak))
2853 need_relocs = TRUE;
2855 /* MINUS_ONE means the symbol is not defined in this object. It may not
2856 be defined at all; assume that the value doesn't matter in that
2857 case. Otherwise complain if we would use the value. */
2858 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2859 || h->root.root.type == bfd_link_hash_undefweak);
2861 /* Emit necessary relocations. */
2862 sreloc = mips_elf_rel_dyn_section (info, FALSE);
2864 /* General Dynamic. */
2865 if (*tls_type_p & GOT_TLS_GD)
2867 offset = got_offset;
2868 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2870 if (need_relocs)
2872 mips_elf_output_dynamic_relocation
2873 (abfd, sreloc, sreloc->reloc_count++, indx,
2874 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2875 sgot->output_offset + sgot->output_section->vma + offset);
2877 if (indx)
2878 mips_elf_output_dynamic_relocation
2879 (abfd, sreloc, sreloc->reloc_count++, indx,
2880 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2881 sgot->output_offset + sgot->output_section->vma + offset2);
2882 else
2883 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2884 sgot->contents + offset2);
2886 else
2888 MIPS_ELF_PUT_WORD (abfd, 1,
2889 sgot->contents + offset);
2890 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2891 sgot->contents + offset2);
2894 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2897 /* Initial Exec model. */
2898 if (*tls_type_p & GOT_TLS_IE)
2900 offset = got_offset;
2902 if (need_relocs)
2904 if (indx == 0)
2905 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2906 sgot->contents + offset);
2907 else
2908 MIPS_ELF_PUT_WORD (abfd, 0,
2909 sgot->contents + offset);
2911 mips_elf_output_dynamic_relocation
2912 (abfd, sreloc, sreloc->reloc_count++, indx,
2913 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2914 sgot->output_offset + sgot->output_section->vma + offset);
2916 else
2917 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2918 sgot->contents + offset);
2921 if (*tls_type_p & GOT_TLS_LDM)
2923 /* The initial offset is zero, and the LD offsets will include the
2924 bias by DTP_OFFSET. */
2925 MIPS_ELF_PUT_WORD (abfd, 0,
2926 sgot->contents + got_offset
2927 + MIPS_ELF_GOT_SIZE (abfd));
2929 if (!info->shared)
2930 MIPS_ELF_PUT_WORD (abfd, 1,
2931 sgot->contents + got_offset);
2932 else
2933 mips_elf_output_dynamic_relocation
2934 (abfd, sreloc, sreloc->reloc_count++, indx,
2935 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2936 sgot->output_offset + sgot->output_section->vma + got_offset);
2939 *tls_type_p |= GOT_TLS_DONE;
2942 /* Return the GOT index to use for a relocation of type R_TYPE against
2943 a symbol accessed using TLS_TYPE models. The GOT entries for this
2944 symbol in this GOT start at GOT_INDEX. This function initializes the
2945 GOT entries and corresponding relocations. */
2947 static bfd_vma
2948 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2949 int r_type, struct bfd_link_info *info,
2950 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2952 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2953 || r_type == R_MIPS_TLS_LDM);
2955 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2957 if (r_type == R_MIPS_TLS_GOTTPREL)
2959 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2960 if (*tls_type & GOT_TLS_GD)
2961 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2962 else
2963 return got_index;
2966 if (r_type == R_MIPS_TLS_GD)
2968 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2969 return got_index;
2972 if (r_type == R_MIPS_TLS_LDM)
2974 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2975 return got_index;
2978 return got_index;
2981 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2982 for global symbol H. .got.plt comes before the GOT, so the offset
2983 will be negative. */
2985 static bfd_vma
2986 mips_elf_gotplt_index (struct bfd_link_info *info,
2987 struct elf_link_hash_entry *h)
2989 bfd_vma plt_index, got_address, got_value;
2990 struct mips_elf_link_hash_table *htab;
2992 htab = mips_elf_hash_table (info);
2993 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2995 /* This function only works for VxWorks, because a non-VxWorks .got.plt
2996 section starts with reserved entries. */
2997 BFD_ASSERT (htab->is_vxworks);
2999 /* Calculate the index of the symbol's PLT entry. */
3000 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3002 /* Calculate the address of the associated .got.plt entry. */
3003 got_address = (htab->sgotplt->output_section->vma
3004 + htab->sgotplt->output_offset
3005 + plt_index * 4);
3007 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3008 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3009 + htab->root.hgot->root.u.def.section->output_offset
3010 + htab->root.hgot->root.u.def.value);
3012 return got_address - got_value;
3015 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3016 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3017 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3018 offset can be found. */
3020 static bfd_vma
3021 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3022 bfd_vma value, unsigned long r_symndx,
3023 struct mips_elf_link_hash_entry *h, int r_type)
3025 struct mips_elf_link_hash_table *htab;
3026 struct mips_got_entry *entry;
3028 htab = mips_elf_hash_table (info);
3029 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3030 r_symndx, h, r_type);
3031 if (!entry)
3032 return MINUS_ONE;
3034 if (TLS_RELOC_P (r_type))
3036 if (entry->symndx == -1 && htab->got_info->next == NULL)
3037 /* A type (3) entry in the single-GOT case. We use the symbol's
3038 hash table entry to track the index. */
3039 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3040 r_type, info, h, value);
3041 else
3042 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3043 r_type, info, h, value);
3045 else
3046 return entry->gotidx;
3049 /* Returns the GOT index for the global symbol indicated by H. */
3051 static bfd_vma
3052 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3053 int r_type, struct bfd_link_info *info)
3055 struct mips_elf_link_hash_table *htab;
3056 bfd_vma index;
3057 struct mips_got_info *g, *gg;
3058 long global_got_dynindx = 0;
3060 htab = mips_elf_hash_table (info);
3061 gg = g = htab->got_info;
3062 if (g->bfd2got && ibfd)
3064 struct mips_got_entry e, *p;
3066 BFD_ASSERT (h->dynindx >= 0);
3068 g = mips_elf_got_for_ibfd (g, ibfd);
3069 if (g->next != gg || TLS_RELOC_P (r_type))
3071 e.abfd = ibfd;
3072 e.symndx = -1;
3073 e.d.h = (struct mips_elf_link_hash_entry *)h;
3074 e.tls_type = 0;
3076 p = htab_find (g->got_entries, &e);
3078 BFD_ASSERT (p->gotidx > 0);
3080 if (TLS_RELOC_P (r_type))
3082 bfd_vma value = MINUS_ONE;
3083 if ((h->root.type == bfd_link_hash_defined
3084 || h->root.type == bfd_link_hash_defweak)
3085 && h->root.u.def.section->output_section)
3086 value = (h->root.u.def.value
3087 + h->root.u.def.section->output_offset
3088 + h->root.u.def.section->output_section->vma);
3090 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3091 info, e.d.h, value);
3093 else
3094 return p->gotidx;
3098 if (gg->global_gotsym != NULL)
3099 global_got_dynindx = gg->global_gotsym->dynindx;
3101 if (TLS_RELOC_P (r_type))
3103 struct mips_elf_link_hash_entry *hm
3104 = (struct mips_elf_link_hash_entry *) h;
3105 bfd_vma value = MINUS_ONE;
3107 if ((h->root.type == bfd_link_hash_defined
3108 || h->root.type == bfd_link_hash_defweak)
3109 && h->root.u.def.section->output_section)
3110 value = (h->root.u.def.value
3111 + h->root.u.def.section->output_offset
3112 + h->root.u.def.section->output_section->vma);
3114 index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3115 r_type, info, hm, value);
3117 else
3119 /* Once we determine the global GOT entry with the lowest dynamic
3120 symbol table index, we must put all dynamic symbols with greater
3121 indices into the GOT. That makes it easy to calculate the GOT
3122 offset. */
3123 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3124 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3125 * MIPS_ELF_GOT_SIZE (abfd));
3127 BFD_ASSERT (index < htab->sgot->size);
3129 return index;
3132 /* Find a GOT page entry that points to within 32KB of VALUE. These
3133 entries are supposed to be placed at small offsets in the GOT, i.e.,
3134 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3135 entry could be created. If OFFSETP is nonnull, use it to return the
3136 offset of the GOT entry from VALUE. */
3138 static bfd_vma
3139 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3140 bfd_vma value, bfd_vma *offsetp)
3142 bfd_vma page, index;
3143 struct mips_got_entry *entry;
3145 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3146 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3147 NULL, R_MIPS_GOT_PAGE);
3149 if (!entry)
3150 return MINUS_ONE;
3152 index = entry->gotidx;
3154 if (offsetp)
3155 *offsetp = value - entry->d.address;
3157 return index;
3160 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3161 EXTERNAL is true if the relocation was against a global symbol
3162 that has been forced local. */
3164 static bfd_vma
3165 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3166 bfd_vma value, bfd_boolean external)
3168 struct mips_got_entry *entry;
3170 /* GOT16 relocations against local symbols are followed by a LO16
3171 relocation; those against global symbols are not. Thus if the
3172 symbol was originally local, the GOT16 relocation should load the
3173 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3174 if (! external)
3175 value = mips_elf_high (value) << 16;
3177 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3178 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3179 same in all cases. */
3180 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3181 NULL, R_MIPS_GOT16);
3182 if (entry)
3183 return entry->gotidx;
3184 else
3185 return MINUS_ONE;
3188 /* Returns the offset for the entry at the INDEXth position
3189 in the GOT. */
3191 static bfd_vma
3192 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3193 bfd *input_bfd, bfd_vma index)
3195 struct mips_elf_link_hash_table *htab;
3196 asection *sgot;
3197 bfd_vma gp;
3199 htab = mips_elf_hash_table (info);
3200 sgot = htab->sgot;
3201 gp = _bfd_get_gp_value (output_bfd)
3202 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3204 return sgot->output_section->vma + sgot->output_offset + index - gp;
3207 /* Create and return a local GOT entry for VALUE, which was calculated
3208 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3209 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3210 instead. */
3212 static struct mips_got_entry *
3213 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3214 bfd *ibfd, bfd_vma value,
3215 unsigned long r_symndx,
3216 struct mips_elf_link_hash_entry *h,
3217 int r_type)
3219 struct mips_got_entry entry, **loc;
3220 struct mips_got_info *g;
3221 struct mips_elf_link_hash_table *htab;
3223 htab = mips_elf_hash_table (info);
3225 entry.abfd = NULL;
3226 entry.symndx = -1;
3227 entry.d.address = value;
3228 entry.tls_type = 0;
3230 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
3231 if (g == NULL)
3233 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
3234 BFD_ASSERT (g != NULL);
3237 /* We might have a symbol, H, if it has been forced local. Use the
3238 global entry then. It doesn't matter whether an entry is local
3239 or global for TLS, since the dynamic linker does not
3240 automatically relocate TLS GOT entries. */
3241 BFD_ASSERT (h == NULL || h->root.forced_local);
3242 if (TLS_RELOC_P (r_type))
3244 struct mips_got_entry *p;
3246 entry.abfd = ibfd;
3247 if (r_type == R_MIPS_TLS_LDM)
3249 entry.tls_type = GOT_TLS_LDM;
3250 entry.symndx = 0;
3251 entry.d.addend = 0;
3253 else if (h == NULL)
3255 entry.symndx = r_symndx;
3256 entry.d.addend = 0;
3258 else
3259 entry.d.h = h;
3261 p = (struct mips_got_entry *)
3262 htab_find (g->got_entries, &entry);
3264 BFD_ASSERT (p);
3265 return p;
3268 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3269 INSERT);
3270 if (*loc)
3271 return *loc;
3273 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3274 entry.tls_type = 0;
3276 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3278 if (! *loc)
3279 return NULL;
3281 memcpy (*loc, &entry, sizeof entry);
3283 if (g->assigned_gotno > g->local_gotno)
3285 (*loc)->gotidx = -1;
3286 /* We didn't allocate enough space in the GOT. */
3287 (*_bfd_error_handler)
3288 (_("not enough GOT space for local GOT entries"));
3289 bfd_set_error (bfd_error_bad_value);
3290 return NULL;
3293 MIPS_ELF_PUT_WORD (abfd, value,
3294 (htab->sgot->contents + entry.gotidx));
3296 /* These GOT entries need a dynamic relocation on VxWorks. */
3297 if (htab->is_vxworks)
3299 Elf_Internal_Rela outrel;
3300 asection *s;
3301 bfd_byte *loc;
3302 bfd_vma got_address;
3304 s = mips_elf_rel_dyn_section (info, FALSE);
3305 got_address = (htab->sgot->output_section->vma
3306 + htab->sgot->output_offset
3307 + entry.gotidx);
3309 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3310 outrel.r_offset = got_address;
3311 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3312 outrel.r_addend = value;
3313 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
3316 return *loc;
3319 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3320 The number might be exact or a worst-case estimate, depending on how
3321 much information is available to elf_backend_omit_section_dynsym at
3322 the current linking stage. */
3324 static bfd_size_type
3325 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3327 bfd_size_type count;
3329 count = 0;
3330 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3332 asection *p;
3333 const struct elf_backend_data *bed;
3335 bed = get_elf_backend_data (output_bfd);
3336 for (p = output_bfd->sections; p ; p = p->next)
3337 if ((p->flags & SEC_EXCLUDE) == 0
3338 && (p->flags & SEC_ALLOC) != 0
3339 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3340 ++count;
3342 return count;
3345 /* Sort the dynamic symbol table so that symbols that need GOT entries
3346 appear towards the end. */
3348 static bfd_boolean
3349 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3351 struct mips_elf_link_hash_table *htab;
3352 struct mips_elf_hash_sort_data hsd;
3353 struct mips_got_info *g;
3355 if (elf_hash_table (info)->dynsymcount == 0)
3356 return TRUE;
3358 htab = mips_elf_hash_table (info);
3359 g = htab->got_info;
3360 if (g == NULL)
3361 return TRUE;
3363 hsd.low = NULL;
3364 hsd.max_unref_got_dynindx
3365 = hsd.min_got_dynindx
3366 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3367 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3368 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3369 elf_hash_table (info)),
3370 mips_elf_sort_hash_table_f,
3371 &hsd);
3373 /* There should have been enough room in the symbol table to
3374 accommodate both the GOT and non-GOT symbols. */
3375 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3376 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3377 == elf_hash_table (info)->dynsymcount);
3378 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3379 == g->global_gotno);
3381 /* Now we know which dynamic symbol has the lowest dynamic symbol
3382 table index in the GOT. */
3383 g->global_gotsym = hsd.low;
3385 return TRUE;
3388 /* If H needs a GOT entry, assign it the highest available dynamic
3389 index. Otherwise, assign it the lowest available dynamic
3390 index. */
3392 static bfd_boolean
3393 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3395 struct mips_elf_hash_sort_data *hsd = data;
3397 if (h->root.root.type == bfd_link_hash_warning)
3398 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3400 /* Symbols without dynamic symbol table entries aren't interesting
3401 at all. */
3402 if (h->root.dynindx == -1)
3403 return TRUE;
3405 switch (h->global_got_area)
3407 case GGA_NONE:
3408 h->root.dynindx = hsd->max_non_got_dynindx++;
3409 break;
3411 case GGA_NORMAL:
3412 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3414 h->root.dynindx = --hsd->min_got_dynindx;
3415 hsd->low = (struct elf_link_hash_entry *) h;
3416 break;
3418 case GGA_RELOC_ONLY:
3419 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3421 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3422 hsd->low = (struct elf_link_hash_entry *) h;
3423 h->root.dynindx = hsd->max_unref_got_dynindx++;
3424 break;
3427 return TRUE;
3430 /* If H is a symbol that needs a global GOT entry, but has a dynamic
3431 symbol table index lower than any we've seen to date, record it for
3432 posterity. */
3434 static bfd_boolean
3435 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3436 bfd *abfd, struct bfd_link_info *info,
3437 unsigned char tls_flag)
3439 struct mips_elf_link_hash_table *htab;
3440 struct mips_elf_link_hash_entry *hmips;
3441 struct mips_got_entry entry, **loc;
3442 struct mips_got_info *g;
3444 htab = mips_elf_hash_table (info);
3445 hmips = (struct mips_elf_link_hash_entry *) h;
3447 /* A global symbol in the GOT must also be in the dynamic symbol
3448 table. */
3449 if (h->dynindx == -1)
3451 switch (ELF_ST_VISIBILITY (h->other))
3453 case STV_INTERNAL:
3454 case STV_HIDDEN:
3455 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3456 break;
3458 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3459 return FALSE;
3462 /* Make sure we have a GOT to put this entry into. */
3463 g = htab->got_info;
3464 BFD_ASSERT (g != NULL);
3466 entry.abfd = abfd;
3467 entry.symndx = -1;
3468 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3469 entry.tls_type = 0;
3471 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3472 INSERT);
3474 /* If we've already marked this entry as needing GOT space, we don't
3475 need to do it again. */
3476 if (*loc)
3478 (*loc)->tls_type |= tls_flag;
3479 return TRUE;
3482 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3484 if (! *loc)
3485 return FALSE;
3487 entry.gotidx = -1;
3488 entry.tls_type = tls_flag;
3490 memcpy (*loc, &entry, sizeof entry);
3492 if (tls_flag == 0)
3493 hmips->global_got_area = GGA_NORMAL;
3495 return TRUE;
3498 /* Reserve space in G for a GOT entry containing the value of symbol
3499 SYMNDX in input bfd ABDF, plus ADDEND. */
3501 static bfd_boolean
3502 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3503 struct bfd_link_info *info,
3504 unsigned char tls_flag)
3506 struct mips_elf_link_hash_table *htab;
3507 struct mips_got_info *g;
3508 struct mips_got_entry entry, **loc;
3510 htab = mips_elf_hash_table (info);
3511 g = htab->got_info;
3512 BFD_ASSERT (g != NULL);
3514 entry.abfd = abfd;
3515 entry.symndx = symndx;
3516 entry.d.addend = addend;
3517 entry.tls_type = tls_flag;
3518 loc = (struct mips_got_entry **)
3519 htab_find_slot (g->got_entries, &entry, INSERT);
3521 if (*loc)
3523 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3525 g->tls_gotno += 2;
3526 (*loc)->tls_type |= tls_flag;
3528 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3530 g->tls_gotno += 1;
3531 (*loc)->tls_type |= tls_flag;
3533 return TRUE;
3536 if (tls_flag != 0)
3538 entry.gotidx = -1;
3539 entry.tls_type = tls_flag;
3540 if (tls_flag == GOT_TLS_IE)
3541 g->tls_gotno += 1;
3542 else if (tls_flag == GOT_TLS_GD)
3543 g->tls_gotno += 2;
3544 else if (g->tls_ldm_offset == MINUS_ONE)
3546 g->tls_ldm_offset = MINUS_TWO;
3547 g->tls_gotno += 2;
3550 else
3552 entry.gotidx = g->local_gotno++;
3553 entry.tls_type = 0;
3556 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3558 if (! *loc)
3559 return FALSE;
3561 memcpy (*loc, &entry, sizeof entry);
3563 return TRUE;
3566 /* Return the maximum number of GOT page entries required for RANGE. */
3568 static bfd_vma
3569 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3571 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3574 /* Record that ABFD has a page relocation against symbol SYMNDX and
3575 that ADDEND is the addend for that relocation.
3577 This function creates an upper bound on the number of GOT slots
3578 required; no attempt is made to combine references to non-overridable
3579 global symbols across multiple input files. */
3581 static bfd_boolean
3582 mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3583 long symndx, bfd_signed_vma addend)
3585 struct mips_elf_link_hash_table *htab;
3586 struct mips_got_info *g;
3587 struct mips_got_page_entry lookup, *entry;
3588 struct mips_got_page_range **range_ptr, *range;
3589 bfd_vma old_pages, new_pages;
3590 void **loc;
3592 htab = mips_elf_hash_table (info);
3593 g = htab->got_info;
3594 BFD_ASSERT (g != NULL);
3596 /* Find the mips_got_page_entry hash table entry for this symbol. */
3597 lookup.abfd = abfd;
3598 lookup.symndx = symndx;
3599 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3600 if (loc == NULL)
3601 return FALSE;
3603 /* Create a mips_got_page_entry if this is the first time we've
3604 seen the symbol. */
3605 entry = (struct mips_got_page_entry *) *loc;
3606 if (!entry)
3608 entry = bfd_alloc (abfd, sizeof (*entry));
3609 if (!entry)
3610 return FALSE;
3612 entry->abfd = abfd;
3613 entry->symndx = symndx;
3614 entry->ranges = NULL;
3615 entry->num_pages = 0;
3616 *loc = entry;
3619 /* Skip over ranges whose maximum extent cannot share a page entry
3620 with ADDEND. */
3621 range_ptr = &entry->ranges;
3622 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3623 range_ptr = &(*range_ptr)->next;
3625 /* If we scanned to the end of the list, or found a range whose
3626 minimum extent cannot share a page entry with ADDEND, create
3627 a new singleton range. */
3628 range = *range_ptr;
3629 if (!range || addend < range->min_addend - 0xffff)
3631 range = bfd_alloc (abfd, sizeof (*range));
3632 if (!range)
3633 return FALSE;
3635 range->next = *range_ptr;
3636 range->min_addend = addend;
3637 range->max_addend = addend;
3639 *range_ptr = range;
3640 entry->num_pages++;
3641 g->page_gotno++;
3642 return TRUE;
3645 /* Remember how many pages the old range contributed. */
3646 old_pages = mips_elf_pages_for_range (range);
3648 /* Update the ranges. */
3649 if (addend < range->min_addend)
3650 range->min_addend = addend;
3651 else if (addend > range->max_addend)
3653 if (range->next && addend >= range->next->min_addend - 0xffff)
3655 old_pages += mips_elf_pages_for_range (range->next);
3656 range->max_addend = range->next->max_addend;
3657 range->next = range->next->next;
3659 else
3660 range->max_addend = addend;
3663 /* Record any change in the total estimate. */
3664 new_pages = mips_elf_pages_for_range (range);
3665 if (old_pages != new_pages)
3667 entry->num_pages += new_pages - old_pages;
3668 g->page_gotno += new_pages - old_pages;
3671 return TRUE;
3674 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3676 static void
3677 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3678 unsigned int n)
3680 asection *s;
3681 struct mips_elf_link_hash_table *htab;
3683 htab = mips_elf_hash_table (info);
3684 s = mips_elf_rel_dyn_section (info, FALSE);
3685 BFD_ASSERT (s != NULL);
3687 if (htab->is_vxworks)
3688 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3689 else
3691 if (s->size == 0)
3693 /* Make room for a null element. */
3694 s->size += MIPS_ELF_REL_SIZE (abfd);
3695 ++s->reloc_count;
3697 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3701 /* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3702 if the GOT entry is for an indirect or warning symbol. */
3704 static int
3705 mips_elf_check_recreate_got (void **entryp, void *data)
3707 struct mips_got_entry *entry;
3708 bfd_boolean *must_recreate;
3710 entry = (struct mips_got_entry *) *entryp;
3711 must_recreate = (bfd_boolean *) data;
3712 if (entry->abfd != NULL && entry->symndx == -1)
3714 struct mips_elf_link_hash_entry *h;
3716 h = entry->d.h;
3717 if (h->root.root.type == bfd_link_hash_indirect
3718 || h->root.root.type == bfd_link_hash_warning)
3720 *must_recreate = TRUE;
3721 return 0;
3724 return 1;
3727 /* A htab_traverse callback for GOT entries. Add all entries to
3728 hash table *DATA, converting entries for indirect and warning
3729 symbols into entries for the target symbol. Set *DATA to null
3730 on error. */
3732 static int
3733 mips_elf_recreate_got (void **entryp, void *data)
3735 htab_t *new_got;
3736 struct mips_got_entry *entry;
3737 void **slot;
3739 new_got = (htab_t *) data;
3740 entry = (struct mips_got_entry *) *entryp;
3741 if (entry->abfd != NULL && entry->symndx == -1)
3743 struct mips_elf_link_hash_entry *h;
3745 h = entry->d.h;
3746 while (h->root.root.type == bfd_link_hash_indirect
3747 || h->root.root.type == bfd_link_hash_warning)
3749 BFD_ASSERT (h->global_got_area == GGA_NONE);
3750 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3752 entry->d.h = h;
3754 slot = htab_find_slot (*new_got, entry, INSERT);
3755 if (slot == NULL)
3757 *new_got = NULL;
3758 return 0;
3760 if (*slot == NULL)
3761 *slot = entry;
3762 else
3763 free (entry);
3764 return 1;
3767 /* If any entries in G->got_entries are for indirect or warning symbols,
3768 replace them with entries for the target symbol. */
3770 static bfd_boolean
3771 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3773 bfd_boolean must_recreate;
3774 htab_t new_got;
3776 must_recreate = FALSE;
3777 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3778 if (must_recreate)
3780 new_got = htab_create (htab_size (g->got_entries),
3781 mips_elf_got_entry_hash,
3782 mips_elf_got_entry_eq, NULL);
3783 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3784 if (new_got == NULL)
3785 return FALSE;
3787 /* Each entry in g->got_entries has either been copied to new_got
3788 or freed. Now delete the hash table itself. */
3789 htab_delete (g->got_entries);
3790 g->got_entries = new_got;
3792 return TRUE;
3795 /* A mips_elf_link_hash_traverse callback for which DATA points
3796 to a mips_got_info. Count the number of type (3) entries. */
3798 static int
3799 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
3801 struct mips_got_info *g;
3803 g = (struct mips_got_info *) data;
3804 if (h->global_got_area != GGA_NONE)
3806 if (h->root.forced_local || h->root.dynindx == -1)
3808 /* We no longer need this entry if it was only used for
3809 relocations; those relocations will be against the
3810 null or section symbol instead of H. */
3811 if (h->global_got_area != GGA_RELOC_ONLY)
3812 g->local_gotno++;
3813 h->global_got_area = GGA_NONE;
3815 else
3817 g->global_gotno++;
3818 if (h->global_got_area == GGA_RELOC_ONLY)
3819 g->reloc_only_gotno++;
3822 return 1;
3825 /* Compute the hash value of the bfd in a bfd2got hash entry. */
3827 static hashval_t
3828 mips_elf_bfd2got_entry_hash (const void *entry_)
3830 const struct mips_elf_bfd2got_hash *entry
3831 = (struct mips_elf_bfd2got_hash *)entry_;
3833 return entry->bfd->id;
3836 /* Check whether two hash entries have the same bfd. */
3838 static int
3839 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
3841 const struct mips_elf_bfd2got_hash *e1
3842 = (const struct mips_elf_bfd2got_hash *)entry1;
3843 const struct mips_elf_bfd2got_hash *e2
3844 = (const struct mips_elf_bfd2got_hash *)entry2;
3846 return e1->bfd == e2->bfd;
3849 /* In a multi-got link, determine the GOT to be used for IBFD. G must
3850 be the master GOT data. */
3852 static struct mips_got_info *
3853 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
3855 struct mips_elf_bfd2got_hash e, *p;
3857 if (! g->bfd2got)
3858 return g;
3860 e.bfd = ibfd;
3861 p = htab_find (g->bfd2got, &e);
3862 return p ? p->g : NULL;
3865 /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3866 Return NULL if an error occured. */
3868 static struct mips_got_info *
3869 mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3870 bfd *input_bfd)
3872 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3873 struct mips_got_info *g;
3874 void **bfdgotp;
3876 bfdgot_entry.bfd = input_bfd;
3877 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3878 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
3880 if (bfdgot == NULL)
3882 bfdgot = ((struct mips_elf_bfd2got_hash *)
3883 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
3884 if (bfdgot == NULL)
3885 return NULL;
3887 *bfdgotp = bfdgot;
3889 g = ((struct mips_got_info *)
3890 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
3891 if (g == NULL)
3892 return NULL;
3894 bfdgot->bfd = input_bfd;
3895 bfdgot->g = g;
3897 g->global_gotsym = NULL;
3898 g->global_gotno = 0;
3899 g->reloc_only_gotno = 0;
3900 g->local_gotno = 0;
3901 g->page_gotno = 0;
3902 g->assigned_gotno = -1;
3903 g->tls_gotno = 0;
3904 g->tls_assigned_gotno = 0;
3905 g->tls_ldm_offset = MINUS_ONE;
3906 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3907 mips_elf_multi_got_entry_eq, NULL);
3908 if (g->got_entries == NULL)
3909 return NULL;
3911 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3912 mips_got_page_entry_eq, NULL);
3913 if (g->got_page_entries == NULL)
3914 return NULL;
3916 g->bfd2got = NULL;
3917 g->next = NULL;
3920 return bfdgot->g;
3923 /* A htab_traverse callback for the entries in the master got.
3924 Create one separate got for each bfd that has entries in the global
3925 got, such that we can tell how many local and global entries each
3926 bfd requires. */
3928 static int
3929 mips_elf_make_got_per_bfd (void **entryp, void *p)
3931 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3932 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3933 struct mips_got_info *g;
3935 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3936 if (g == NULL)
3938 arg->obfd = NULL;
3939 return 0;
3942 /* Insert the GOT entry in the bfd's got entry hash table. */
3943 entryp = htab_find_slot (g->got_entries, entry, INSERT);
3944 if (*entryp != NULL)
3945 return 1;
3947 *entryp = entry;
3949 if (entry->tls_type)
3951 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3952 g->tls_gotno += 2;
3953 if (entry->tls_type & GOT_TLS_IE)
3954 g->tls_gotno += 1;
3956 else if (entry->symndx >= 0 || entry->d.h->root.forced_local)
3957 ++g->local_gotno;
3958 else
3959 ++g->global_gotno;
3961 return 1;
3964 /* A htab_traverse callback for the page entries in the master got.
3965 Associate each page entry with the bfd's got. */
3967 static int
3968 mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
3970 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
3971 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
3972 struct mips_got_info *g;
3974 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3975 if (g == NULL)
3977 arg->obfd = NULL;
3978 return 0;
3981 /* Insert the GOT entry in the bfd's got entry hash table. */
3982 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
3983 if (*entryp != NULL)
3984 return 1;
3986 *entryp = entry;
3987 g->page_gotno += entry->num_pages;
3988 return 1;
3991 /* Consider merging the got described by BFD2GOT with TO, using the
3992 information given by ARG. Return -1 if this would lead to overflow,
3993 1 if they were merged successfully, and 0 if a merge failed due to
3994 lack of memory. (These values are chosen so that nonnegative return
3995 values can be returned by a htab_traverse callback.) */
3997 static int
3998 mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
3999 struct mips_got_info *to,
4000 struct mips_elf_got_per_bfd_arg *arg)
4002 struct mips_got_info *from = bfd2got->g;
4003 unsigned int estimate;
4005 /* Work out how many page entries we would need for the combined GOT. */
4006 estimate = arg->max_pages;
4007 if (estimate >= from->page_gotno + to->page_gotno)
4008 estimate = from->page_gotno + to->page_gotno;
4010 /* And conservatively estimate how many local, global and TLS entries
4011 would be needed. */
4012 estimate += (from->local_gotno
4013 + from->global_gotno
4014 + from->tls_gotno
4015 + to->local_gotno
4016 + to->global_gotno
4017 + to->tls_gotno);
4019 /* Bail out if the combined GOT might be too big. */
4020 if (estimate > arg->max_count)
4021 return -1;
4023 /* Commit to the merge. Record that TO is now the bfd for this got. */
4024 bfd2got->g = to;
4026 /* Transfer the bfd's got information from FROM to TO. */
4027 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4028 if (arg->obfd == NULL)
4029 return 0;
4031 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4032 if (arg->obfd == NULL)
4033 return 0;
4035 /* We don't have to worry about releasing memory of the actual
4036 got entries, since they're all in the master got_entries hash
4037 table anyway. */
4038 htab_delete (from->got_entries);
4039 htab_delete (from->got_page_entries);
4040 return 1;
4043 /* Attempt to merge gots of different input bfds. Try to use as much
4044 as possible of the primary got, since it doesn't require explicit
4045 dynamic relocations, but don't use bfds that would reference global
4046 symbols out of the addressable range. Failing the primary got,
4047 attempt to merge with the current got, or finish the current got
4048 and then make make the new got current. */
4050 static int
4051 mips_elf_merge_gots (void **bfd2got_, void *p)
4053 struct mips_elf_bfd2got_hash *bfd2got
4054 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4055 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4056 struct mips_got_info *g;
4057 unsigned int estimate;
4058 int result;
4060 g = bfd2got->g;
4062 /* Work out the number of page, local and TLS entries. */
4063 estimate = arg->max_pages;
4064 if (estimate > g->page_gotno)
4065 estimate = g->page_gotno;
4066 estimate += g->local_gotno + g->tls_gotno;
4068 /* We place TLS GOT entries after both locals and globals. The globals
4069 for the primary GOT may overflow the normal GOT size limit, so be
4070 sure not to merge a GOT which requires TLS with the primary GOT in that
4071 case. This doesn't affect non-primary GOTs. */
4072 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4074 if (estimate <= arg->max_count)
4076 /* If we don't have a primary GOT, use it as
4077 a starting point for the primary GOT. */
4078 if (!arg->primary)
4080 arg->primary = bfd2got->g;
4081 return 1;
4084 /* Try merging with the primary GOT. */
4085 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4086 if (result >= 0)
4087 return result;
4090 /* If we can merge with the last-created got, do it. */
4091 if (arg->current)
4093 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4094 if (result >= 0)
4095 return result;
4098 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4099 fits; if it turns out that it doesn't, we'll get relocation
4100 overflows anyway. */
4101 g->next = arg->current;
4102 arg->current = g;
4104 return 1;
4107 /* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4108 is null iff there is just a single GOT. */
4110 static int
4111 mips_elf_initialize_tls_index (void **entryp, void *p)
4113 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4114 struct mips_got_info *g = p;
4115 bfd_vma next_index;
4116 unsigned char tls_type;
4118 /* We're only interested in TLS symbols. */
4119 if (entry->tls_type == 0)
4120 return 1;
4122 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4124 if (entry->symndx == -1 && g->next == NULL)
4126 /* A type (3) got entry in the single-GOT case. We use the symbol's
4127 hash table entry to track its index. */
4128 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4129 return 1;
4130 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4131 entry->d.h->tls_got_offset = next_index;
4132 tls_type = entry->d.h->tls_type;
4134 else
4136 if (entry->tls_type & GOT_TLS_LDM)
4138 /* There are separate mips_got_entry objects for each input bfd
4139 that requires an LDM entry. Make sure that all LDM entries in
4140 a GOT resolve to the same index. */
4141 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4143 entry->gotidx = g->tls_ldm_offset;
4144 return 1;
4146 g->tls_ldm_offset = next_index;
4148 entry->gotidx = next_index;
4149 tls_type = entry->tls_type;
4152 /* Account for the entries we've just allocated. */
4153 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4154 g->tls_assigned_gotno += 2;
4155 if (tls_type & GOT_TLS_IE)
4156 g->tls_assigned_gotno += 1;
4158 return 1;
4161 /* If passed a NULL mips_got_info in the argument, set the marker used
4162 to tell whether a global symbol needs a got entry (in the primary
4163 got) to the given VALUE.
4165 If passed a pointer G to a mips_got_info in the argument (it must
4166 not be the primary GOT), compute the offset from the beginning of
4167 the (primary) GOT section to the entry in G corresponding to the
4168 global symbol. G's assigned_gotno must contain the index of the
4169 first available global GOT entry in G. VALUE must contain the size
4170 of a GOT entry in bytes. For each global GOT entry that requires a
4171 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4172 marked as not eligible for lazy resolution through a function
4173 stub. */
4174 static int
4175 mips_elf_set_global_got_offset (void **entryp, void *p)
4177 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4178 struct mips_elf_set_global_got_offset_arg *arg
4179 = (struct mips_elf_set_global_got_offset_arg *)p;
4180 struct mips_got_info *g = arg->g;
4182 if (g && entry->tls_type != GOT_NORMAL)
4183 arg->needed_relocs +=
4184 mips_tls_got_relocs (arg->info, entry->tls_type,
4185 entry->symndx == -1 ? &entry->d.h->root : NULL);
4187 if (entry->abfd != NULL
4188 && entry->symndx == -1
4189 && entry->d.h->global_got_area != GGA_NONE)
4191 if (g)
4193 BFD_ASSERT (g->global_gotsym == NULL);
4195 entry->gotidx = arg->value * (long) g->assigned_gotno++;
4196 if (arg->info->shared
4197 || (elf_hash_table (arg->info)->dynamic_sections_created
4198 && entry->d.h->root.def_dynamic
4199 && !entry->d.h->root.def_regular))
4200 ++arg->needed_relocs;
4202 else
4203 entry->d.h->global_got_area = arg->value;
4206 return 1;
4209 /* A htab_traverse callback for GOT entries for which DATA is the
4210 bfd_link_info. Forbid any global symbols from having traditional
4211 lazy-binding stubs. */
4213 static int
4214 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4216 struct bfd_link_info *info;
4217 struct mips_elf_link_hash_table *htab;
4218 struct mips_got_entry *entry;
4220 entry = (struct mips_got_entry *) *entryp;
4221 info = (struct bfd_link_info *) data;
4222 htab = mips_elf_hash_table (info);
4223 if (entry->abfd != NULL
4224 && entry->symndx == -1
4225 && entry->d.h->needs_lazy_stub)
4227 entry->d.h->needs_lazy_stub = FALSE;
4228 htab->lazy_stub_count--;
4231 return 1;
4234 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4235 the primary GOT. */
4236 static bfd_vma
4237 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4239 if (g->bfd2got == NULL)
4240 return 0;
4242 g = mips_elf_got_for_ibfd (g, ibfd);
4243 if (! g)
4244 return 0;
4246 BFD_ASSERT (g->next);
4248 g = g->next;
4250 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4251 * MIPS_ELF_GOT_SIZE (abfd);
4254 /* Turn a single GOT that is too big for 16-bit addressing into
4255 a sequence of GOTs, each one 16-bit addressable. */
4257 static bfd_boolean
4258 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4259 asection *got, bfd_size_type pages)
4261 struct mips_elf_link_hash_table *htab;
4262 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4263 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
4264 struct mips_got_info *g, *gg;
4265 unsigned int assign, needed_relocs;
4266 bfd *dynobj;
4268 dynobj = elf_hash_table (info)->dynobj;
4269 htab = mips_elf_hash_table (info);
4270 g = htab->got_info;
4271 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
4272 mips_elf_bfd2got_entry_eq, NULL);
4273 if (g->bfd2got == NULL)
4274 return FALSE;
4276 got_per_bfd_arg.bfd2got = g->bfd2got;
4277 got_per_bfd_arg.obfd = abfd;
4278 got_per_bfd_arg.info = info;
4280 /* Count how many GOT entries each input bfd requires, creating a
4281 map from bfd to got info while at that. */
4282 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4283 if (got_per_bfd_arg.obfd == NULL)
4284 return FALSE;
4286 /* Also count how many page entries each input bfd requires. */
4287 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4288 &got_per_bfd_arg);
4289 if (got_per_bfd_arg.obfd == NULL)
4290 return FALSE;
4292 got_per_bfd_arg.current = NULL;
4293 got_per_bfd_arg.primary = NULL;
4294 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4295 / MIPS_ELF_GOT_SIZE (abfd))
4296 - htab->reserved_gotno);
4297 got_per_bfd_arg.max_pages = pages;
4298 /* The number of globals that will be included in the primary GOT.
4299 See the calls to mips_elf_set_global_got_offset below for more
4300 information. */
4301 got_per_bfd_arg.global_count = g->global_gotno;
4303 /* Try to merge the GOTs of input bfds together, as long as they
4304 don't seem to exceed the maximum GOT size, choosing one of them
4305 to be the primary GOT. */
4306 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4307 if (got_per_bfd_arg.obfd == NULL)
4308 return FALSE;
4310 /* If we do not find any suitable primary GOT, create an empty one. */
4311 if (got_per_bfd_arg.primary == NULL)
4313 g->next = (struct mips_got_info *)
4314 bfd_alloc (abfd, sizeof (struct mips_got_info));
4315 if (g->next == NULL)
4316 return FALSE;
4318 g->next->global_gotsym = NULL;
4319 g->next->global_gotno = 0;
4320 g->next->reloc_only_gotno = 0;
4321 g->next->local_gotno = 0;
4322 g->next->page_gotno = 0;
4323 g->next->tls_gotno = 0;
4324 g->next->assigned_gotno = 0;
4325 g->next->tls_assigned_gotno = 0;
4326 g->next->tls_ldm_offset = MINUS_ONE;
4327 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4328 mips_elf_multi_got_entry_eq,
4329 NULL);
4330 if (g->next->got_entries == NULL)
4331 return FALSE;
4332 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4333 mips_got_page_entry_eq,
4334 NULL);
4335 if (g->next->got_page_entries == NULL)
4336 return FALSE;
4337 g->next->bfd2got = NULL;
4339 else
4340 g->next = got_per_bfd_arg.primary;
4341 g->next->next = got_per_bfd_arg.current;
4343 /* GG is now the master GOT, and G is the primary GOT. */
4344 gg = g;
4345 g = g->next;
4347 /* Map the output bfd to the primary got. That's what we're going
4348 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4349 didn't mark in check_relocs, and we want a quick way to find it.
4350 We can't just use gg->next because we're going to reverse the
4351 list. */
4353 struct mips_elf_bfd2got_hash *bfdgot;
4354 void **bfdgotp;
4356 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4357 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4359 if (bfdgot == NULL)
4360 return FALSE;
4362 bfdgot->bfd = abfd;
4363 bfdgot->g = g;
4364 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4366 BFD_ASSERT (*bfdgotp == NULL);
4367 *bfdgotp = bfdgot;
4370 /* Every symbol that is referenced in a dynamic relocation must be
4371 present in the primary GOT, so arrange for them to appear after
4372 those that are actually referenced. */
4373 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4374 g->global_gotno = gg->global_gotno;
4376 set_got_offset_arg.g = NULL;
4377 set_got_offset_arg.value = GGA_RELOC_ONLY;
4378 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4379 &set_got_offset_arg);
4380 set_got_offset_arg.value = GGA_NORMAL;
4381 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4382 &set_got_offset_arg);
4384 /* Now go through the GOTs assigning them offset ranges.
4385 [assigned_gotno, local_gotno[ will be set to the range of local
4386 entries in each GOT. We can then compute the end of a GOT by
4387 adding local_gotno to global_gotno. We reverse the list and make
4388 it circular since then we'll be able to quickly compute the
4389 beginning of a GOT, by computing the end of its predecessor. To
4390 avoid special cases for the primary GOT, while still preserving
4391 assertions that are valid for both single- and multi-got links,
4392 we arrange for the main got struct to have the right number of
4393 global entries, but set its local_gotno such that the initial
4394 offset of the primary GOT is zero. Remember that the primary GOT
4395 will become the last item in the circular linked list, so it
4396 points back to the master GOT. */
4397 gg->local_gotno = -g->global_gotno;
4398 gg->global_gotno = g->global_gotno;
4399 gg->tls_gotno = 0;
4400 assign = 0;
4401 gg->next = gg;
4405 struct mips_got_info *gn;
4407 assign += htab->reserved_gotno;
4408 g->assigned_gotno = assign;
4409 g->local_gotno += assign;
4410 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4411 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4413 /* Take g out of the direct list, and push it onto the reversed
4414 list that gg points to. g->next is guaranteed to be nonnull after
4415 this operation, as required by mips_elf_initialize_tls_index. */
4416 gn = g->next;
4417 g->next = gg->next;
4418 gg->next = g;
4420 /* Set up any TLS entries. We always place the TLS entries after
4421 all non-TLS entries. */
4422 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4423 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
4425 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4426 g = gn;
4428 /* Forbid global symbols in every non-primary GOT from having
4429 lazy-binding stubs. */
4430 if (g)
4431 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4433 while (g);
4435 got->size = (gg->next->local_gotno
4436 + gg->next->global_gotno
4437 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4439 needed_relocs = 0;
4440 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4441 set_got_offset_arg.info = info;
4442 for (g = gg->next; g && g->next != gg; g = g->next)
4444 unsigned int save_assign;
4446 /* Assign offsets to global GOT entries. */
4447 save_assign = g->assigned_gotno;
4448 g->assigned_gotno = g->local_gotno;
4449 set_got_offset_arg.g = g;
4450 set_got_offset_arg.needed_relocs = 0;
4451 htab_traverse (g->got_entries,
4452 mips_elf_set_global_got_offset,
4453 &set_got_offset_arg);
4454 needed_relocs += set_got_offset_arg.needed_relocs;
4455 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4457 g->assigned_gotno = save_assign;
4458 if (info->shared)
4460 needed_relocs += g->local_gotno - g->assigned_gotno;
4461 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4462 + g->next->global_gotno
4463 + g->next->tls_gotno
4464 + htab->reserved_gotno);
4468 if (needed_relocs)
4469 mips_elf_allocate_dynamic_relocations (dynobj, info,
4470 needed_relocs);
4472 return TRUE;
4476 /* Returns the first relocation of type r_type found, beginning with
4477 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4479 static const Elf_Internal_Rela *
4480 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4481 const Elf_Internal_Rela *relocation,
4482 const Elf_Internal_Rela *relend)
4484 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4486 while (relocation < relend)
4488 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4489 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4490 return relocation;
4492 ++relocation;
4495 /* We didn't find it. */
4496 return NULL;
4499 /* Return whether a relocation is against a local symbol. */
4501 static bfd_boolean
4502 mips_elf_local_relocation_p (bfd *input_bfd,
4503 const Elf_Internal_Rela *relocation,
4504 asection **local_sections,
4505 bfd_boolean check_forced)
4507 unsigned long r_symndx;
4508 Elf_Internal_Shdr *symtab_hdr;
4509 struct mips_elf_link_hash_entry *h;
4510 size_t extsymoff;
4512 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4513 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4514 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4516 if (r_symndx < extsymoff)
4517 return TRUE;
4518 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4519 return TRUE;
4521 if (check_forced)
4523 /* Look up the hash table to check whether the symbol
4524 was forced local. */
4525 h = (struct mips_elf_link_hash_entry *)
4526 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
4527 /* Find the real hash-table entry for this symbol. */
4528 while (h->root.root.type == bfd_link_hash_indirect
4529 || h->root.root.type == bfd_link_hash_warning)
4530 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4531 if (h->root.forced_local)
4532 return TRUE;
4535 return FALSE;
4538 /* Sign-extend VALUE, which has the indicated number of BITS. */
4540 bfd_vma
4541 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4543 if (value & ((bfd_vma) 1 << (bits - 1)))
4544 /* VALUE is negative. */
4545 value |= ((bfd_vma) - 1) << bits;
4547 return value;
4550 /* Return non-zero if the indicated VALUE has overflowed the maximum
4551 range expressible by a signed number with the indicated number of
4552 BITS. */
4554 static bfd_boolean
4555 mips_elf_overflow_p (bfd_vma value, int bits)
4557 bfd_signed_vma svalue = (bfd_signed_vma) value;
4559 if (svalue > (1 << (bits - 1)) - 1)
4560 /* The value is too big. */
4561 return TRUE;
4562 else if (svalue < -(1 << (bits - 1)))
4563 /* The value is too small. */
4564 return TRUE;
4566 /* All is well. */
4567 return FALSE;
4570 /* Calculate the %high function. */
4572 static bfd_vma
4573 mips_elf_high (bfd_vma value)
4575 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4578 /* Calculate the %higher function. */
4580 static bfd_vma
4581 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4583 #ifdef BFD64
4584 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4585 #else
4586 abort ();
4587 return MINUS_ONE;
4588 #endif
4591 /* Calculate the %highest function. */
4593 static bfd_vma
4594 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4596 #ifdef BFD64
4597 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4598 #else
4599 abort ();
4600 return MINUS_ONE;
4601 #endif
4604 /* Create the .compact_rel section. */
4606 static bfd_boolean
4607 mips_elf_create_compact_rel_section
4608 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4610 flagword flags;
4611 register asection *s;
4613 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4615 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4616 | SEC_READONLY);
4618 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4619 if (s == NULL
4620 || ! bfd_set_section_alignment (abfd, s,
4621 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4622 return FALSE;
4624 s->size = sizeof (Elf32_External_compact_rel);
4627 return TRUE;
4630 /* Create the .got section to hold the global offset table. */
4632 static bfd_boolean
4633 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
4635 flagword flags;
4636 register asection *s;
4637 struct elf_link_hash_entry *h;
4638 struct bfd_link_hash_entry *bh;
4639 struct mips_got_info *g;
4640 bfd_size_type amt;
4641 struct mips_elf_link_hash_table *htab;
4643 htab = mips_elf_hash_table (info);
4645 /* This function may be called more than once. */
4646 if (htab->sgot)
4647 return TRUE;
4649 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4650 | SEC_LINKER_CREATED);
4652 /* We have to use an alignment of 2**4 here because this is hardcoded
4653 in the function stub generation and in the linker script. */
4654 s = bfd_make_section_with_flags (abfd, ".got", flags);
4655 if (s == NULL
4656 || ! bfd_set_section_alignment (abfd, s, 4))
4657 return FALSE;
4658 htab->sgot = s;
4660 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4661 linker script because we don't want to define the symbol if we
4662 are not creating a global offset table. */
4663 bh = NULL;
4664 if (! (_bfd_generic_link_add_one_symbol
4665 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4666 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4667 return FALSE;
4669 h = (struct elf_link_hash_entry *) bh;
4670 h->non_elf = 0;
4671 h->def_regular = 1;
4672 h->type = STT_OBJECT;
4673 elf_hash_table (info)->hgot = h;
4675 if (info->shared
4676 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4677 return FALSE;
4679 amt = sizeof (struct mips_got_info);
4680 g = bfd_alloc (abfd, amt);
4681 if (g == NULL)
4682 return FALSE;
4683 g->global_gotsym = NULL;
4684 g->global_gotno = 0;
4685 g->reloc_only_gotno = 0;
4686 g->tls_gotno = 0;
4687 g->local_gotno = 0;
4688 g->page_gotno = 0;
4689 g->assigned_gotno = 0;
4690 g->bfd2got = NULL;
4691 g->next = NULL;
4692 g->tls_ldm_offset = MINUS_ONE;
4693 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4694 mips_elf_got_entry_eq, NULL);
4695 if (g->got_entries == NULL)
4696 return FALSE;
4697 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4698 mips_got_page_entry_eq, NULL);
4699 if (g->got_page_entries == NULL)
4700 return FALSE;
4701 htab->got_info = g;
4702 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4703 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4705 /* We also need a .got.plt section when generating PLTs. */
4706 s = bfd_make_section_with_flags (abfd, ".got.plt",
4707 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4708 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4709 if (s == NULL)
4710 return FALSE;
4711 htab->sgotplt = s;
4713 return TRUE;
4716 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4717 __GOTT_INDEX__ symbols. These symbols are only special for
4718 shared objects; they are not used in executables. */
4720 static bfd_boolean
4721 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4723 return (mips_elf_hash_table (info)->is_vxworks
4724 && info->shared
4725 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4726 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4729 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4730 require an la25 stub. See also mips_elf_local_pic_function_p,
4731 which determines whether the destination function ever requires a
4732 stub. */
4734 static bfd_boolean
4735 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4737 /* We specifically ignore branches and jumps from EF_PIC objects,
4738 where the onus is on the compiler or programmer to perform any
4739 necessary initialization of $25. Sometimes such initialization
4740 is unnecessary; for example, -mno-shared functions do not use
4741 the incoming value of $25, and may therefore be called directly. */
4742 if (PIC_OBJECT_P (input_bfd))
4743 return FALSE;
4745 switch (r_type)
4747 case R_MIPS_26:
4748 case R_MIPS_PC16:
4749 case R_MIPS16_26:
4750 return TRUE;
4752 default:
4753 return FALSE;
4757 /* Calculate the value produced by the RELOCATION (which comes from
4758 the INPUT_BFD). The ADDEND is the addend to use for this
4759 RELOCATION; RELOCATION->R_ADDEND is ignored.
4761 The result of the relocation calculation is stored in VALUEP.
4762 REQUIRE_JALXP indicates whether or not the opcode used with this
4763 relocation must be JALX.
4765 This function returns bfd_reloc_continue if the caller need take no
4766 further action regarding this relocation, bfd_reloc_notsupported if
4767 something goes dramatically wrong, bfd_reloc_overflow if an
4768 overflow occurs, and bfd_reloc_ok to indicate success. */
4770 static bfd_reloc_status_type
4771 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4772 asection *input_section,
4773 struct bfd_link_info *info,
4774 const Elf_Internal_Rela *relocation,
4775 bfd_vma addend, reloc_howto_type *howto,
4776 Elf_Internal_Sym *local_syms,
4777 asection **local_sections, bfd_vma *valuep,
4778 const char **namep, bfd_boolean *require_jalxp,
4779 bfd_boolean save_addend)
4781 /* The eventual value we will return. */
4782 bfd_vma value;
4783 /* The address of the symbol against which the relocation is
4784 occurring. */
4785 bfd_vma symbol = 0;
4786 /* The final GP value to be used for the relocatable, executable, or
4787 shared object file being produced. */
4788 bfd_vma gp;
4789 /* The place (section offset or address) of the storage unit being
4790 relocated. */
4791 bfd_vma p;
4792 /* The value of GP used to create the relocatable object. */
4793 bfd_vma gp0;
4794 /* The offset into the global offset table at which the address of
4795 the relocation entry symbol, adjusted by the addend, resides
4796 during execution. */
4797 bfd_vma g = MINUS_ONE;
4798 /* The section in which the symbol referenced by the relocation is
4799 located. */
4800 asection *sec = NULL;
4801 struct mips_elf_link_hash_entry *h = NULL;
4802 /* TRUE if the symbol referred to by this relocation is a local
4803 symbol. */
4804 bfd_boolean local_p, was_local_p;
4805 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4806 bfd_boolean gp_disp_p = FALSE;
4807 /* TRUE if the symbol referred to by this relocation is
4808 "__gnu_local_gp". */
4809 bfd_boolean gnu_local_gp_p = FALSE;
4810 Elf_Internal_Shdr *symtab_hdr;
4811 size_t extsymoff;
4812 unsigned long r_symndx;
4813 int r_type;
4814 /* TRUE if overflow occurred during the calculation of the
4815 relocation value. */
4816 bfd_boolean overflowed_p;
4817 /* TRUE if this relocation refers to a MIPS16 function. */
4818 bfd_boolean target_is_16_bit_code_p = FALSE;
4819 struct mips_elf_link_hash_table *htab;
4820 bfd *dynobj;
4822 dynobj = elf_hash_table (info)->dynobj;
4823 htab = mips_elf_hash_table (info);
4825 /* Parse the relocation. */
4826 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4827 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4828 p = (input_section->output_section->vma
4829 + input_section->output_offset
4830 + relocation->r_offset);
4832 /* Assume that there will be no overflow. */
4833 overflowed_p = FALSE;
4835 /* Figure out whether or not the symbol is local, and get the offset
4836 used in the array of hash table entries. */
4837 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4838 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4839 local_sections, FALSE);
4840 was_local_p = local_p;
4841 if (! elf_bad_symtab (input_bfd))
4842 extsymoff = symtab_hdr->sh_info;
4843 else
4845 /* The symbol table does not follow the rule that local symbols
4846 must come before globals. */
4847 extsymoff = 0;
4850 /* Figure out the value of the symbol. */
4851 if (local_p)
4853 Elf_Internal_Sym *sym;
4855 sym = local_syms + r_symndx;
4856 sec = local_sections[r_symndx];
4858 symbol = sec->output_section->vma + sec->output_offset;
4859 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4860 || (sec->flags & SEC_MERGE))
4861 symbol += sym->st_value;
4862 if ((sec->flags & SEC_MERGE)
4863 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4865 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4866 addend -= symbol;
4867 addend += sec->output_section->vma + sec->output_offset;
4870 /* MIPS16 text labels should be treated as odd. */
4871 if (ELF_ST_IS_MIPS16 (sym->st_other))
4872 ++symbol;
4874 /* Record the name of this symbol, for our caller. */
4875 *namep = bfd_elf_string_from_elf_section (input_bfd,
4876 symtab_hdr->sh_link,
4877 sym->st_name);
4878 if (*namep == '\0')
4879 *namep = bfd_section_name (input_bfd, sec);
4881 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
4883 else
4885 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4887 /* For global symbols we look up the symbol in the hash-table. */
4888 h = ((struct mips_elf_link_hash_entry *)
4889 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4890 /* Find the real hash-table entry for this symbol. */
4891 while (h->root.root.type == bfd_link_hash_indirect
4892 || h->root.root.type == bfd_link_hash_warning)
4893 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4895 /* Record the name of this symbol, for our caller. */
4896 *namep = h->root.root.root.string;
4898 /* See if this is the special _gp_disp symbol. Note that such a
4899 symbol must always be a global symbol. */
4900 if (strcmp (*namep, "_gp_disp") == 0
4901 && ! NEWABI_P (input_bfd))
4903 /* Relocations against _gp_disp are permitted only with
4904 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
4905 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
4906 return bfd_reloc_notsupported;
4908 gp_disp_p = TRUE;
4910 /* See if this is the special _gp symbol. Note that such a
4911 symbol must always be a global symbol. */
4912 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4913 gnu_local_gp_p = TRUE;
4916 /* If this symbol is defined, calculate its address. Note that
4917 _gp_disp is a magic symbol, always implicitly defined by the
4918 linker, so it's inappropriate to check to see whether or not
4919 its defined. */
4920 else if ((h->root.root.type == bfd_link_hash_defined
4921 || h->root.root.type == bfd_link_hash_defweak)
4922 && h->root.root.u.def.section)
4924 sec = h->root.root.u.def.section;
4925 if (sec->output_section)
4926 symbol = (h->root.root.u.def.value
4927 + sec->output_section->vma
4928 + sec->output_offset);
4929 else
4930 symbol = h->root.root.u.def.value;
4932 else if (h->root.root.type == bfd_link_hash_undefweak)
4933 /* We allow relocations against undefined weak symbols, giving
4934 it the value zero, so that you can undefined weak functions
4935 and check to see if they exist by looking at their
4936 addresses. */
4937 symbol = 0;
4938 else if (info->unresolved_syms_in_objects == RM_IGNORE
4939 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4940 symbol = 0;
4941 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4942 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
4944 /* If this is a dynamic link, we should have created a
4945 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4946 in in _bfd_mips_elf_create_dynamic_sections.
4947 Otherwise, we should define the symbol with a value of 0.
4948 FIXME: It should probably get into the symbol table
4949 somehow as well. */
4950 BFD_ASSERT (! info->shared);
4951 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4952 symbol = 0;
4954 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4956 /* This is an optional symbol - an Irix specific extension to the
4957 ELF spec. Ignore it for now.
4958 XXX - FIXME - there is more to the spec for OPTIONAL symbols
4959 than simply ignoring them, but we do not handle this for now.
4960 For information see the "64-bit ELF Object File Specification"
4961 which is available from here:
4962 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
4963 symbol = 0;
4965 else
4967 if (! ((*info->callbacks->undefined_symbol)
4968 (info, h->root.root.root.string, input_bfd,
4969 input_section, relocation->r_offset,
4970 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4971 || ELF_ST_VISIBILITY (h->root.other))))
4972 return bfd_reloc_undefined;
4973 symbol = 0;
4976 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
4979 /* If this is a reference to a 16-bit function with a stub, we need
4980 to redirect the relocation to the stub unless:
4982 (a) the relocation is for a MIPS16 JAL;
4984 (b) the relocation is for a MIPS16 PIC call, and there are no
4985 non-MIPS16 uses of the GOT slot; or
4987 (c) the section allows direct references to MIPS16 functions. */
4988 if (r_type != R_MIPS16_26
4989 && !info->relocatable
4990 && ((h != NULL
4991 && h->fn_stub != NULL
4992 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
4993 || (local_p
4994 && elf_tdata (input_bfd)->local_stubs != NULL
4995 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4996 && !section_allows_mips16_refs_p (input_section))
4998 /* This is a 32- or 64-bit call to a 16-bit function. We should
4999 have already noticed that we were going to need the
5000 stub. */
5001 if (local_p)
5002 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5003 else
5005 BFD_ASSERT (h->need_fn_stub);
5006 sec = h->fn_stub;
5009 symbol = sec->output_section->vma + sec->output_offset;
5010 /* The target is 16-bit, but the stub isn't. */
5011 target_is_16_bit_code_p = FALSE;
5013 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
5014 need to redirect the call to the stub. Note that we specifically
5015 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5016 use an indirect stub instead. */
5017 else if (r_type == R_MIPS16_26 && !info->relocatable
5018 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5019 || (local_p
5020 && elf_tdata (input_bfd)->local_call_stubs != NULL
5021 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5022 && !target_is_16_bit_code_p)
5024 if (local_p)
5025 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5026 else
5028 /* If both call_stub and call_fp_stub are defined, we can figure
5029 out which one to use by checking which one appears in the input
5030 file. */
5031 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5033 asection *o;
5035 sec = NULL;
5036 for (o = input_bfd->sections; o != NULL; o = o->next)
5038 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5040 sec = h->call_fp_stub;
5041 break;
5044 if (sec == NULL)
5045 sec = h->call_stub;
5047 else if (h->call_stub != NULL)
5048 sec = h->call_stub;
5049 else
5050 sec = h->call_fp_stub;
5053 BFD_ASSERT (sec->size > 0);
5054 symbol = sec->output_section->vma + sec->output_offset;
5056 /* If this is a direct call to a PIC function, redirect to the
5057 non-PIC stub. */
5058 else if (h != NULL && h->la25_stub
5059 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5060 symbol = (h->la25_stub->stub_section->output_section->vma
5061 + h->la25_stub->stub_section->output_offset
5062 + h->la25_stub->offset);
5064 /* Calls from 16-bit code to 32-bit code and vice versa require the
5065 special jalx instruction. */
5066 *require_jalxp = (!info->relocatable
5067 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
5068 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
5070 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5071 local_sections, TRUE);
5073 gp0 = _bfd_get_gp_value (input_bfd);
5074 gp = _bfd_get_gp_value (abfd);
5075 if (htab->got_info)
5076 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5078 if (gnu_local_gp_p)
5079 symbol = gp;
5081 /* If we haven't already determined the GOT offset, oand we're going
5082 to need it, get it now. */
5083 switch (r_type)
5085 case R_MIPS_GOT_PAGE:
5086 case R_MIPS_GOT_OFST:
5087 /* We need to decay to GOT_DISP/addend if the symbol doesn't
5088 bind locally. */
5089 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
5090 if (local_p || r_type == R_MIPS_GOT_OFST)
5091 break;
5092 /* Fall through. */
5094 case R_MIPS16_CALL16:
5095 case R_MIPS16_GOT16:
5096 case R_MIPS_CALL16:
5097 case R_MIPS_GOT16:
5098 case R_MIPS_GOT_DISP:
5099 case R_MIPS_GOT_HI16:
5100 case R_MIPS_CALL_HI16:
5101 case R_MIPS_GOT_LO16:
5102 case R_MIPS_CALL_LO16:
5103 case R_MIPS_TLS_GD:
5104 case R_MIPS_TLS_GOTTPREL:
5105 case R_MIPS_TLS_LDM:
5106 /* Find the index into the GOT where this value is located. */
5107 if (r_type == R_MIPS_TLS_LDM)
5109 g = mips_elf_local_got_index (abfd, input_bfd, info,
5110 0, 0, NULL, r_type);
5111 if (g == MINUS_ONE)
5112 return bfd_reloc_outofrange;
5114 else if (!local_p)
5116 /* On VxWorks, CALL relocations should refer to the .got.plt
5117 entry, which is initialized to point at the PLT stub. */
5118 if (htab->is_vxworks
5119 && (r_type == R_MIPS_CALL_HI16
5120 || r_type == R_MIPS_CALL_LO16
5121 || call16_reloc_p (r_type)))
5123 BFD_ASSERT (addend == 0);
5124 BFD_ASSERT (h->root.needs_plt);
5125 g = mips_elf_gotplt_index (info, &h->root);
5127 else
5129 /* GOT_PAGE may take a non-zero addend, that is ignored in a
5130 GOT_PAGE relocation that decays to GOT_DISP because the
5131 symbol turns out to be global. The addend is then added
5132 as GOT_OFST. */
5133 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
5134 g = mips_elf_global_got_index (dynobj, input_bfd,
5135 &h->root, r_type, info);
5136 if (h->tls_type == GOT_NORMAL
5137 && (! elf_hash_table(info)->dynamic_sections_created
5138 || (info->shared
5139 && (info->symbolic || h->root.forced_local)
5140 && h->root.def_regular)))
5141 /* This is a static link or a -Bsymbolic link. The
5142 symbol is defined locally, or was forced to be local.
5143 We must initialize this entry in the GOT. */
5144 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5147 else if (!htab->is_vxworks
5148 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5149 /* The calculation below does not involve "g". */
5150 break;
5151 else
5153 g = mips_elf_local_got_index (abfd, input_bfd, info,
5154 symbol + addend, r_symndx, h, r_type);
5155 if (g == MINUS_ONE)
5156 return bfd_reloc_outofrange;
5159 /* Convert GOT indices to actual offsets. */
5160 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5161 break;
5164 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5165 symbols are resolved by the loader. Add them to .rela.dyn. */
5166 if (h != NULL && is_gott_symbol (info, &h->root))
5168 Elf_Internal_Rela outrel;
5169 bfd_byte *loc;
5170 asection *s;
5172 s = mips_elf_rel_dyn_section (info, FALSE);
5173 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5175 outrel.r_offset = (input_section->output_section->vma
5176 + input_section->output_offset
5177 + relocation->r_offset);
5178 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5179 outrel.r_addend = addend;
5180 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5182 /* If we've written this relocation for a readonly section,
5183 we need to set DF_TEXTREL again, so that we do not delete the
5184 DT_TEXTREL tag. */
5185 if (MIPS_ELF_READONLY_SECTION (input_section))
5186 info->flags |= DF_TEXTREL;
5188 *valuep = 0;
5189 return bfd_reloc_ok;
5192 /* Figure out what kind of relocation is being performed. */
5193 switch (r_type)
5195 case R_MIPS_NONE:
5196 return bfd_reloc_continue;
5198 case R_MIPS_16:
5199 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5200 overflowed_p = mips_elf_overflow_p (value, 16);
5201 break;
5203 case R_MIPS_32:
5204 case R_MIPS_REL32:
5205 case R_MIPS_64:
5206 if ((info->shared
5207 || (htab->root.dynamic_sections_created
5208 && h != NULL
5209 && h->root.def_dynamic
5210 && !h->root.def_regular
5211 && !h->has_static_relocs))
5212 && r_symndx != 0
5213 && (h == NULL
5214 || h->root.root.type != bfd_link_hash_undefweak
5215 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5216 && (input_section->flags & SEC_ALLOC) != 0)
5218 /* If we're creating a shared library, then we can't know
5219 where the symbol will end up. So, we create a relocation
5220 record in the output, and leave the job up to the dynamic
5221 linker. We must do the same for executable references to
5222 shared library symbols, unless we've decided to use copy
5223 relocs or PLTs instead. */
5224 value = addend;
5225 if (!mips_elf_create_dynamic_relocation (abfd,
5226 info,
5227 relocation,
5229 sec,
5230 symbol,
5231 &value,
5232 input_section))
5233 return bfd_reloc_undefined;
5235 else
5237 if (r_type != R_MIPS_REL32)
5238 value = symbol + addend;
5239 else
5240 value = addend;
5242 value &= howto->dst_mask;
5243 break;
5245 case R_MIPS_PC32:
5246 value = symbol + addend - p;
5247 value &= howto->dst_mask;
5248 break;
5250 case R_MIPS16_26:
5251 /* The calculation for R_MIPS16_26 is just the same as for an
5252 R_MIPS_26. It's only the storage of the relocated field into
5253 the output file that's different. That's handled in
5254 mips_elf_perform_relocation. So, we just fall through to the
5255 R_MIPS_26 case here. */
5256 case R_MIPS_26:
5257 if (local_p)
5258 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
5259 else
5261 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
5262 if (h->root.root.type != bfd_link_hash_undefweak)
5263 overflowed_p = (value >> 26) != ((p + 4) >> 28);
5265 value &= howto->dst_mask;
5266 break;
5268 case R_MIPS_TLS_DTPREL_HI16:
5269 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5270 & howto->dst_mask);
5271 break;
5273 case R_MIPS_TLS_DTPREL_LO16:
5274 case R_MIPS_TLS_DTPREL32:
5275 case R_MIPS_TLS_DTPREL64:
5276 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5277 break;
5279 case R_MIPS_TLS_TPREL_HI16:
5280 value = (mips_elf_high (addend + symbol - tprel_base (info))
5281 & howto->dst_mask);
5282 break;
5284 case R_MIPS_TLS_TPREL_LO16:
5285 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5286 break;
5288 case R_MIPS_HI16:
5289 case R_MIPS16_HI16:
5290 if (!gp_disp_p)
5292 value = mips_elf_high (addend + symbol);
5293 value &= howto->dst_mask;
5295 else
5297 /* For MIPS16 ABI code we generate this sequence
5298 0: li $v0,%hi(_gp_disp)
5299 4: addiupc $v1,%lo(_gp_disp)
5300 8: sll $v0,16
5301 12: addu $v0,$v1
5302 14: move $gp,$v0
5303 So the offsets of hi and lo relocs are the same, but the
5304 $pc is four higher than $t9 would be, so reduce
5305 both reloc addends by 4. */
5306 if (r_type == R_MIPS16_HI16)
5307 value = mips_elf_high (addend + gp - p - 4);
5308 else
5309 value = mips_elf_high (addend + gp - p);
5310 overflowed_p = mips_elf_overflow_p (value, 16);
5312 break;
5314 case R_MIPS_LO16:
5315 case R_MIPS16_LO16:
5316 if (!gp_disp_p)
5317 value = (symbol + addend) & howto->dst_mask;
5318 else
5320 /* See the comment for R_MIPS16_HI16 above for the reason
5321 for this conditional. */
5322 if (r_type == R_MIPS16_LO16)
5323 value = addend + gp - p;
5324 else
5325 value = addend + gp - p + 4;
5326 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5327 for overflow. But, on, say, IRIX5, relocations against
5328 _gp_disp are normally generated from the .cpload
5329 pseudo-op. It generates code that normally looks like
5330 this:
5332 lui $gp,%hi(_gp_disp)
5333 addiu $gp,$gp,%lo(_gp_disp)
5334 addu $gp,$gp,$t9
5336 Here $t9 holds the address of the function being called,
5337 as required by the MIPS ELF ABI. The R_MIPS_LO16
5338 relocation can easily overflow in this situation, but the
5339 R_MIPS_HI16 relocation will handle the overflow.
5340 Therefore, we consider this a bug in the MIPS ABI, and do
5341 not check for overflow here. */
5343 break;
5345 case R_MIPS_LITERAL:
5346 /* Because we don't merge literal sections, we can handle this
5347 just like R_MIPS_GPREL16. In the long run, we should merge
5348 shared literals, and then we will need to additional work
5349 here. */
5351 /* Fall through. */
5353 case R_MIPS16_GPREL:
5354 /* The R_MIPS16_GPREL performs the same calculation as
5355 R_MIPS_GPREL16, but stores the relocated bits in a different
5356 order. We don't need to do anything special here; the
5357 differences are handled in mips_elf_perform_relocation. */
5358 case R_MIPS_GPREL16:
5359 /* Only sign-extend the addend if it was extracted from the
5360 instruction. If the addend was separate, leave it alone,
5361 otherwise we may lose significant bits. */
5362 if (howto->partial_inplace)
5363 addend = _bfd_mips_elf_sign_extend (addend, 16);
5364 value = symbol + addend - gp;
5365 /* If the symbol was local, any earlier relocatable links will
5366 have adjusted its addend with the gp offset, so compensate
5367 for that now. Don't do it for symbols forced local in this
5368 link, though, since they won't have had the gp offset applied
5369 to them before. */
5370 if (was_local_p)
5371 value += gp0;
5372 overflowed_p = mips_elf_overflow_p (value, 16);
5373 break;
5375 case R_MIPS16_GOT16:
5376 case R_MIPS16_CALL16:
5377 case R_MIPS_GOT16:
5378 case R_MIPS_CALL16:
5379 /* VxWorks does not have separate local and global semantics for
5380 R_MIPS*_GOT16; every relocation evaluates to "G". */
5381 if (!htab->is_vxworks && local_p)
5383 bfd_boolean forced;
5385 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
5386 local_sections, FALSE);
5387 value = mips_elf_got16_entry (abfd, input_bfd, info,
5388 symbol + addend, forced);
5389 if (value == MINUS_ONE)
5390 return bfd_reloc_outofrange;
5391 value
5392 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5393 overflowed_p = mips_elf_overflow_p (value, 16);
5394 break;
5397 /* Fall through. */
5399 case R_MIPS_TLS_GD:
5400 case R_MIPS_TLS_GOTTPREL:
5401 case R_MIPS_TLS_LDM:
5402 case R_MIPS_GOT_DISP:
5403 got_disp:
5404 value = g;
5405 overflowed_p = mips_elf_overflow_p (value, 16);
5406 break;
5408 case R_MIPS_GPREL32:
5409 value = (addend + symbol + gp0 - gp);
5410 if (!save_addend)
5411 value &= howto->dst_mask;
5412 break;
5414 case R_MIPS_PC16:
5415 case R_MIPS_GNU_REL16_S2:
5416 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5417 overflowed_p = mips_elf_overflow_p (value, 18);
5418 value >>= howto->rightshift;
5419 value &= howto->dst_mask;
5420 break;
5422 case R_MIPS_GOT_HI16:
5423 case R_MIPS_CALL_HI16:
5424 /* We're allowed to handle these two relocations identically.
5425 The dynamic linker is allowed to handle the CALL relocations
5426 differently by creating a lazy evaluation stub. */
5427 value = g;
5428 value = mips_elf_high (value);
5429 value &= howto->dst_mask;
5430 break;
5432 case R_MIPS_GOT_LO16:
5433 case R_MIPS_CALL_LO16:
5434 value = g & howto->dst_mask;
5435 break;
5437 case R_MIPS_GOT_PAGE:
5438 /* GOT_PAGE relocations that reference non-local symbols decay
5439 to GOT_DISP. The corresponding GOT_OFST relocation decays to
5440 0. */
5441 if (! local_p)
5442 goto got_disp;
5443 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5444 if (value == MINUS_ONE)
5445 return bfd_reloc_outofrange;
5446 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5447 overflowed_p = mips_elf_overflow_p (value, 16);
5448 break;
5450 case R_MIPS_GOT_OFST:
5451 if (local_p)
5452 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5453 else
5454 value = addend;
5455 overflowed_p = mips_elf_overflow_p (value, 16);
5456 break;
5458 case R_MIPS_SUB:
5459 value = symbol - addend;
5460 value &= howto->dst_mask;
5461 break;
5463 case R_MIPS_HIGHER:
5464 value = mips_elf_higher (addend + symbol);
5465 value &= howto->dst_mask;
5466 break;
5468 case R_MIPS_HIGHEST:
5469 value = mips_elf_highest (addend + symbol);
5470 value &= howto->dst_mask;
5471 break;
5473 case R_MIPS_SCN_DISP:
5474 value = symbol + addend - sec->output_offset;
5475 value &= howto->dst_mask;
5476 break;
5478 case R_MIPS_JALR:
5479 /* This relocation is only a hint. In some cases, we optimize
5480 it into a bal instruction. But we don't try to optimize
5481 branches to the PLT; that will wind up wasting time. */
5482 if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
5483 return bfd_reloc_continue;
5484 value = symbol + addend;
5485 break;
5487 case R_MIPS_PJUMP:
5488 case R_MIPS_GNU_VTINHERIT:
5489 case R_MIPS_GNU_VTENTRY:
5490 /* We don't do anything with these at present. */
5491 return bfd_reloc_continue;
5493 default:
5494 /* An unrecognized relocation type. */
5495 return bfd_reloc_notsupported;
5498 /* Store the VALUE for our caller. */
5499 *valuep = value;
5500 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5503 /* Obtain the field relocated by RELOCATION. */
5505 static bfd_vma
5506 mips_elf_obtain_contents (reloc_howto_type *howto,
5507 const Elf_Internal_Rela *relocation,
5508 bfd *input_bfd, bfd_byte *contents)
5510 bfd_vma x;
5511 bfd_byte *location = contents + relocation->r_offset;
5513 /* Obtain the bytes. */
5514 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5516 return x;
5519 /* It has been determined that the result of the RELOCATION is the
5520 VALUE. Use HOWTO to place VALUE into the output file at the
5521 appropriate position. The SECTION is the section to which the
5522 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
5523 for the relocation must be either JAL or JALX, and it is
5524 unconditionally converted to JALX.
5526 Returns FALSE if anything goes wrong. */
5528 static bfd_boolean
5529 mips_elf_perform_relocation (struct bfd_link_info *info,
5530 reloc_howto_type *howto,
5531 const Elf_Internal_Rela *relocation,
5532 bfd_vma value, bfd *input_bfd,
5533 asection *input_section, bfd_byte *contents,
5534 bfd_boolean require_jalx)
5536 bfd_vma x;
5537 bfd_byte *location;
5538 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5540 /* Figure out where the relocation is occurring. */
5541 location = contents + relocation->r_offset;
5543 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5545 /* Obtain the current value. */
5546 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5548 /* Clear the field we are setting. */
5549 x &= ~howto->dst_mask;
5551 /* Set the field. */
5552 x |= (value & howto->dst_mask);
5554 /* If required, turn JAL into JALX. */
5555 if (require_jalx)
5557 bfd_boolean ok;
5558 bfd_vma opcode = x >> 26;
5559 bfd_vma jalx_opcode;
5561 /* Check to see if the opcode is already JAL or JALX. */
5562 if (r_type == R_MIPS16_26)
5564 ok = ((opcode == 0x6) || (opcode == 0x7));
5565 jalx_opcode = 0x7;
5567 else
5569 ok = ((opcode == 0x3) || (opcode == 0x1d));
5570 jalx_opcode = 0x1d;
5573 /* If the opcode is not JAL or JALX, there's a problem. */
5574 if (!ok)
5576 (*_bfd_error_handler)
5577 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5578 input_bfd,
5579 input_section,
5580 (unsigned long) relocation->r_offset);
5581 bfd_set_error (bfd_error_bad_value);
5582 return FALSE;
5585 /* Make this the JALX opcode. */
5586 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5589 /* On the RM9000, bal is faster than jal, because bal uses branch
5590 prediction hardware. If we are linking for the RM9000, and we
5591 see jal, and bal fits, use it instead. Note that this
5592 transformation should be safe for all architectures. */
5593 if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
5594 && !info->relocatable
5595 && !require_jalx
5596 && ((r_type == R_MIPS_26 && (x >> 26) == 0x3) /* jal addr */
5597 || (r_type == R_MIPS_JALR && x == 0x0320f809))) /* jalr t9 */
5599 bfd_vma addr;
5600 bfd_vma dest;
5601 bfd_signed_vma off;
5603 addr = (input_section->output_section->vma
5604 + input_section->output_offset
5605 + relocation->r_offset
5606 + 4);
5607 if (r_type == R_MIPS_26)
5608 dest = (value << 2) | ((addr >> 28) << 28);
5609 else
5610 dest = value;
5611 off = dest - addr;
5612 if (off <= 0x1ffff && off >= -0x20000)
5613 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5616 /* Put the value into the output. */
5617 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5619 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5620 location);
5622 return TRUE;
5625 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5626 is the original relocation, which is now being transformed into a
5627 dynamic relocation. The ADDENDP is adjusted if necessary; the
5628 caller should store the result in place of the original addend. */
5630 static bfd_boolean
5631 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5632 struct bfd_link_info *info,
5633 const Elf_Internal_Rela *rel,
5634 struct mips_elf_link_hash_entry *h,
5635 asection *sec, bfd_vma symbol,
5636 bfd_vma *addendp, asection *input_section)
5638 Elf_Internal_Rela outrel[3];
5639 asection *sreloc;
5640 bfd *dynobj;
5641 int r_type;
5642 long indx;
5643 bfd_boolean defined_p;
5644 struct mips_elf_link_hash_table *htab;
5646 htab = mips_elf_hash_table (info);
5647 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5648 dynobj = elf_hash_table (info)->dynobj;
5649 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5650 BFD_ASSERT (sreloc != NULL);
5651 BFD_ASSERT (sreloc->contents != NULL);
5652 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5653 < sreloc->size);
5655 outrel[0].r_offset =
5656 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5657 if (ABI_64_P (output_bfd))
5659 outrel[1].r_offset =
5660 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5661 outrel[2].r_offset =
5662 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5665 if (outrel[0].r_offset == MINUS_ONE)
5666 /* The relocation field has been deleted. */
5667 return TRUE;
5669 if (outrel[0].r_offset == MINUS_TWO)
5671 /* The relocation field has been converted into a relative value of
5672 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5673 the field to be fully relocated, so add in the symbol's value. */
5674 *addendp += symbol;
5675 return TRUE;
5678 /* We must now calculate the dynamic symbol table index to use
5679 in the relocation. */
5680 if (h != NULL
5681 && (!h->root.def_regular
5682 || (info->shared && !info->symbolic && !h->root.forced_local)))
5684 indx = h->root.dynindx;
5685 if (SGI_COMPAT (output_bfd))
5686 defined_p = h->root.def_regular;
5687 else
5688 /* ??? glibc's ld.so just adds the final GOT entry to the
5689 relocation field. It therefore treats relocs against
5690 defined symbols in the same way as relocs against
5691 undefined symbols. */
5692 defined_p = FALSE;
5694 else
5696 if (sec != NULL && bfd_is_abs_section (sec))
5697 indx = 0;
5698 else if (sec == NULL || sec->owner == NULL)
5700 bfd_set_error (bfd_error_bad_value);
5701 return FALSE;
5703 else
5705 indx = elf_section_data (sec->output_section)->dynindx;
5706 if (indx == 0)
5708 asection *osec = htab->root.text_index_section;
5709 indx = elf_section_data (osec)->dynindx;
5711 if (indx == 0)
5712 abort ();
5715 /* Instead of generating a relocation using the section
5716 symbol, we may as well make it a fully relative
5717 relocation. We want to avoid generating relocations to
5718 local symbols because we used to generate them
5719 incorrectly, without adding the original symbol value,
5720 which is mandated by the ABI for section symbols. In
5721 order to give dynamic loaders and applications time to
5722 phase out the incorrect use, we refrain from emitting
5723 section-relative relocations. It's not like they're
5724 useful, after all. This should be a bit more efficient
5725 as well. */
5726 /* ??? Although this behavior is compatible with glibc's ld.so,
5727 the ABI says that relocations against STN_UNDEF should have
5728 a symbol value of 0. Irix rld honors this, so relocations
5729 against STN_UNDEF have no effect. */
5730 if (!SGI_COMPAT (output_bfd))
5731 indx = 0;
5732 defined_p = TRUE;
5735 /* If the relocation was previously an absolute relocation and
5736 this symbol will not be referred to by the relocation, we must
5737 adjust it by the value we give it in the dynamic symbol table.
5738 Otherwise leave the job up to the dynamic linker. */
5739 if (defined_p && r_type != R_MIPS_REL32)
5740 *addendp += symbol;
5742 if (htab->is_vxworks)
5743 /* VxWorks uses non-relative relocations for this. */
5744 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5745 else
5746 /* The relocation is always an REL32 relocation because we don't
5747 know where the shared library will wind up at load-time. */
5748 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5749 R_MIPS_REL32);
5751 /* For strict adherence to the ABI specification, we should
5752 generate a R_MIPS_64 relocation record by itself before the
5753 _REL32/_64 record as well, such that the addend is read in as
5754 a 64-bit value (REL32 is a 32-bit relocation, after all).
5755 However, since none of the existing ELF64 MIPS dynamic
5756 loaders seems to care, we don't waste space with these
5757 artificial relocations. If this turns out to not be true,
5758 mips_elf_allocate_dynamic_relocation() should be tweaked so
5759 as to make room for a pair of dynamic relocations per
5760 invocation if ABI_64_P, and here we should generate an
5761 additional relocation record with R_MIPS_64 by itself for a
5762 NULL symbol before this relocation record. */
5763 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5764 ABI_64_P (output_bfd)
5765 ? R_MIPS_64
5766 : R_MIPS_NONE);
5767 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5769 /* Adjust the output offset of the relocation to reference the
5770 correct location in the output file. */
5771 outrel[0].r_offset += (input_section->output_section->vma
5772 + input_section->output_offset);
5773 outrel[1].r_offset += (input_section->output_section->vma
5774 + input_section->output_offset);
5775 outrel[2].r_offset += (input_section->output_section->vma
5776 + input_section->output_offset);
5778 /* Put the relocation back out. We have to use the special
5779 relocation outputter in the 64-bit case since the 64-bit
5780 relocation format is non-standard. */
5781 if (ABI_64_P (output_bfd))
5783 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5784 (output_bfd, &outrel[0],
5785 (sreloc->contents
5786 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5788 else if (htab->is_vxworks)
5790 /* VxWorks uses RELA rather than REL dynamic relocations. */
5791 outrel[0].r_addend = *addendp;
5792 bfd_elf32_swap_reloca_out
5793 (output_bfd, &outrel[0],
5794 (sreloc->contents
5795 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5797 else
5798 bfd_elf32_swap_reloc_out
5799 (output_bfd, &outrel[0],
5800 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
5802 /* We've now added another relocation. */
5803 ++sreloc->reloc_count;
5805 /* Make sure the output section is writable. The dynamic linker
5806 will be writing to it. */
5807 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5808 |= SHF_WRITE;
5810 /* On IRIX5, make an entry of compact relocation info. */
5811 if (IRIX_COMPAT (output_bfd) == ict_irix5)
5813 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5814 bfd_byte *cr;
5816 if (scpt)
5818 Elf32_crinfo cptrel;
5820 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5821 cptrel.vaddr = (rel->r_offset
5822 + input_section->output_section->vma
5823 + input_section->output_offset);
5824 if (r_type == R_MIPS_REL32)
5825 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5826 else
5827 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5828 mips_elf_set_cr_dist2to (cptrel, 0);
5829 cptrel.konst = *addendp;
5831 cr = (scpt->contents
5832 + sizeof (Elf32_External_compact_rel));
5833 mips_elf_set_cr_relvaddr (cptrel, 0);
5834 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5835 ((Elf32_External_crinfo *) cr
5836 + scpt->reloc_count));
5837 ++scpt->reloc_count;
5841 /* If we've written this relocation for a readonly section,
5842 we need to set DF_TEXTREL again, so that we do not delete the
5843 DT_TEXTREL tag. */
5844 if (MIPS_ELF_READONLY_SECTION (input_section))
5845 info->flags |= DF_TEXTREL;
5847 return TRUE;
5850 /* Return the MACH for a MIPS e_flags value. */
5852 unsigned long
5853 _bfd_elf_mips_mach (flagword flags)
5855 switch (flags & EF_MIPS_MACH)
5857 case E_MIPS_MACH_3900:
5858 return bfd_mach_mips3900;
5860 case E_MIPS_MACH_4010:
5861 return bfd_mach_mips4010;
5863 case E_MIPS_MACH_4100:
5864 return bfd_mach_mips4100;
5866 case E_MIPS_MACH_4111:
5867 return bfd_mach_mips4111;
5869 case E_MIPS_MACH_4120:
5870 return bfd_mach_mips4120;
5872 case E_MIPS_MACH_4650:
5873 return bfd_mach_mips4650;
5875 case E_MIPS_MACH_5400:
5876 return bfd_mach_mips5400;
5878 case E_MIPS_MACH_5500:
5879 return bfd_mach_mips5500;
5881 case E_MIPS_MACH_9000:
5882 return bfd_mach_mips9000;
5884 case E_MIPS_MACH_SB1:
5885 return bfd_mach_mips_sb1;
5887 case E_MIPS_MACH_LS2E:
5888 return bfd_mach_mips_loongson_2e;
5890 case E_MIPS_MACH_LS2F:
5891 return bfd_mach_mips_loongson_2f;
5893 case E_MIPS_MACH_OCTEON:
5894 return bfd_mach_mips_octeon;
5896 case E_MIPS_MACH_XLR:
5897 return bfd_mach_mips_xlr;
5899 default:
5900 switch (flags & EF_MIPS_ARCH)
5902 default:
5903 case E_MIPS_ARCH_1:
5904 return bfd_mach_mips3000;
5906 case E_MIPS_ARCH_2:
5907 return bfd_mach_mips6000;
5909 case E_MIPS_ARCH_3:
5910 return bfd_mach_mips4000;
5912 case E_MIPS_ARCH_4:
5913 return bfd_mach_mips8000;
5915 case E_MIPS_ARCH_5:
5916 return bfd_mach_mips5;
5918 case E_MIPS_ARCH_32:
5919 return bfd_mach_mipsisa32;
5921 case E_MIPS_ARCH_64:
5922 return bfd_mach_mipsisa64;
5924 case E_MIPS_ARCH_32R2:
5925 return bfd_mach_mipsisa32r2;
5927 case E_MIPS_ARCH_64R2:
5928 return bfd_mach_mipsisa64r2;
5932 return 0;
5935 /* Return printable name for ABI. */
5937 static INLINE char *
5938 elf_mips_abi_name (bfd *abfd)
5940 flagword flags;
5942 flags = elf_elfheader (abfd)->e_flags;
5943 switch (flags & EF_MIPS_ABI)
5945 case 0:
5946 if (ABI_N32_P (abfd))
5947 return "N32";
5948 else if (ABI_64_P (abfd))
5949 return "64";
5950 else
5951 return "none";
5952 case E_MIPS_ABI_O32:
5953 return "O32";
5954 case E_MIPS_ABI_O64:
5955 return "O64";
5956 case E_MIPS_ABI_EABI32:
5957 return "EABI32";
5958 case E_MIPS_ABI_EABI64:
5959 return "EABI64";
5960 default:
5961 return "unknown abi";
5965 /* MIPS ELF uses two common sections. One is the usual one, and the
5966 other is for small objects. All the small objects are kept
5967 together, and then referenced via the gp pointer, which yields
5968 faster assembler code. This is what we use for the small common
5969 section. This approach is copied from ecoff.c. */
5970 static asection mips_elf_scom_section;
5971 static asymbol mips_elf_scom_symbol;
5972 static asymbol *mips_elf_scom_symbol_ptr;
5974 /* MIPS ELF also uses an acommon section, which represents an
5975 allocated common symbol which may be overridden by a
5976 definition in a shared library. */
5977 static asection mips_elf_acom_section;
5978 static asymbol mips_elf_acom_symbol;
5979 static asymbol *mips_elf_acom_symbol_ptr;
5981 /* This is used for both the 32-bit and the 64-bit ABI. */
5983 void
5984 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
5986 elf_symbol_type *elfsym;
5988 /* Handle the special MIPS section numbers that a symbol may use. */
5989 elfsym = (elf_symbol_type *) asym;
5990 switch (elfsym->internal_elf_sym.st_shndx)
5992 case SHN_MIPS_ACOMMON:
5993 /* This section is used in a dynamically linked executable file.
5994 It is an allocated common section. The dynamic linker can
5995 either resolve these symbols to something in a shared
5996 library, or it can just leave them here. For our purposes,
5997 we can consider these symbols to be in a new section. */
5998 if (mips_elf_acom_section.name == NULL)
6000 /* Initialize the acommon section. */
6001 mips_elf_acom_section.name = ".acommon";
6002 mips_elf_acom_section.flags = SEC_ALLOC;
6003 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6004 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6005 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6006 mips_elf_acom_symbol.name = ".acommon";
6007 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6008 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6009 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6011 asym->section = &mips_elf_acom_section;
6012 break;
6014 case SHN_COMMON:
6015 /* Common symbols less than the GP size are automatically
6016 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6017 if (asym->value > elf_gp_size (abfd)
6018 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6019 || IRIX_COMPAT (abfd) == ict_irix6)
6020 break;
6021 /* Fall through. */
6022 case SHN_MIPS_SCOMMON:
6023 if (mips_elf_scom_section.name == NULL)
6025 /* Initialize the small common section. */
6026 mips_elf_scom_section.name = ".scommon";
6027 mips_elf_scom_section.flags = SEC_IS_COMMON;
6028 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6029 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6030 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6031 mips_elf_scom_symbol.name = ".scommon";
6032 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6033 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6034 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6036 asym->section = &mips_elf_scom_section;
6037 asym->value = elfsym->internal_elf_sym.st_size;
6038 break;
6040 case SHN_MIPS_SUNDEFINED:
6041 asym->section = bfd_und_section_ptr;
6042 break;
6044 case SHN_MIPS_TEXT:
6046 asection *section = bfd_get_section_by_name (abfd, ".text");
6048 BFD_ASSERT (SGI_COMPAT (abfd));
6049 if (section != NULL)
6051 asym->section = section;
6052 /* MIPS_TEXT is a bit special, the address is not an offset
6053 to the base of the .text section. So substract the section
6054 base address to make it an offset. */
6055 asym->value -= section->vma;
6058 break;
6060 case SHN_MIPS_DATA:
6062 asection *section = bfd_get_section_by_name (abfd, ".data");
6064 BFD_ASSERT (SGI_COMPAT (abfd));
6065 if (section != NULL)
6067 asym->section = section;
6068 /* MIPS_DATA is a bit special, the address is not an offset
6069 to the base of the .data section. So substract the section
6070 base address to make it an offset. */
6071 asym->value -= section->vma;
6074 break;
6077 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6078 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6079 && (asym->value & 1) != 0)
6081 asym->value--;
6082 elfsym->internal_elf_sym.st_other
6083 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6087 /* Implement elf_backend_eh_frame_address_size. This differs from
6088 the default in the way it handles EABI64.
6090 EABI64 was originally specified as an LP64 ABI, and that is what
6091 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6092 historically accepted the combination of -mabi=eabi and -mlong32,
6093 and this ILP32 variation has become semi-official over time.
6094 Both forms use elf32 and have pointer-sized FDE addresses.
6096 If an EABI object was generated by GCC 4.0 or above, it will have
6097 an empty .gcc_compiled_longXX section, where XX is the size of longs
6098 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6099 have no special marking to distinguish them from LP64 objects.
6101 We don't want users of the official LP64 ABI to be punished for the
6102 existence of the ILP32 variant, but at the same time, we don't want
6103 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6104 We therefore take the following approach:
6106 - If ABFD contains a .gcc_compiled_longXX section, use it to
6107 determine the pointer size.
6109 - Otherwise check the type of the first relocation. Assume that
6110 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6112 - Otherwise punt.
6114 The second check is enough to detect LP64 objects generated by pre-4.0
6115 compilers because, in the kind of output generated by those compilers,
6116 the first relocation will be associated with either a CIE personality
6117 routine or an FDE start address. Furthermore, the compilers never
6118 used a special (non-pointer) encoding for this ABI.
6120 Checking the relocation type should also be safe because there is no
6121 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6122 did so. */
6124 unsigned int
6125 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6127 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6128 return 8;
6129 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6131 bfd_boolean long32_p, long64_p;
6133 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6134 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6135 if (long32_p && long64_p)
6136 return 0;
6137 if (long32_p)
6138 return 4;
6139 if (long64_p)
6140 return 8;
6142 if (sec->reloc_count > 0
6143 && elf_section_data (sec)->relocs != NULL
6144 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6145 == R_MIPS_64))
6146 return 8;
6148 return 0;
6150 return 4;
6153 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6154 relocations against two unnamed section symbols to resolve to the
6155 same address. For example, if we have code like:
6157 lw $4,%got_disp(.data)($gp)
6158 lw $25,%got_disp(.text)($gp)
6159 jalr $25
6161 then the linker will resolve both relocations to .data and the program
6162 will jump there rather than to .text.
6164 We can work around this problem by giving names to local section symbols.
6165 This is also what the MIPSpro tools do. */
6167 bfd_boolean
6168 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6170 return SGI_COMPAT (abfd);
6173 /* Work over a section just before writing it out. This routine is
6174 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6175 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6176 a better way. */
6178 bfd_boolean
6179 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6181 if (hdr->sh_type == SHT_MIPS_REGINFO
6182 && hdr->sh_size > 0)
6184 bfd_byte buf[4];
6186 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6187 BFD_ASSERT (hdr->contents == NULL);
6189 if (bfd_seek (abfd,
6190 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6191 SEEK_SET) != 0)
6192 return FALSE;
6193 H_PUT_32 (abfd, elf_gp (abfd), buf);
6194 if (bfd_bwrite (buf, 4, abfd) != 4)
6195 return FALSE;
6198 if (hdr->sh_type == SHT_MIPS_OPTIONS
6199 && hdr->bfd_section != NULL
6200 && mips_elf_section_data (hdr->bfd_section) != NULL
6201 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6203 bfd_byte *contents, *l, *lend;
6205 /* We stored the section contents in the tdata field in the
6206 set_section_contents routine. We save the section contents
6207 so that we don't have to read them again.
6208 At this point we know that elf_gp is set, so we can look
6209 through the section contents to see if there is an
6210 ODK_REGINFO structure. */
6212 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6213 l = contents;
6214 lend = contents + hdr->sh_size;
6215 while (l + sizeof (Elf_External_Options) <= lend)
6217 Elf_Internal_Options intopt;
6219 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6220 &intopt);
6221 if (intopt.size < sizeof (Elf_External_Options))
6223 (*_bfd_error_handler)
6224 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6225 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6226 break;
6228 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6230 bfd_byte buf[8];
6232 if (bfd_seek (abfd,
6233 (hdr->sh_offset
6234 + (l - contents)
6235 + sizeof (Elf_External_Options)
6236 + (sizeof (Elf64_External_RegInfo) - 8)),
6237 SEEK_SET) != 0)
6238 return FALSE;
6239 H_PUT_64 (abfd, elf_gp (abfd), buf);
6240 if (bfd_bwrite (buf, 8, abfd) != 8)
6241 return FALSE;
6243 else if (intopt.kind == ODK_REGINFO)
6245 bfd_byte buf[4];
6247 if (bfd_seek (abfd,
6248 (hdr->sh_offset
6249 + (l - contents)
6250 + sizeof (Elf_External_Options)
6251 + (sizeof (Elf32_External_RegInfo) - 4)),
6252 SEEK_SET) != 0)
6253 return FALSE;
6254 H_PUT_32 (abfd, elf_gp (abfd), buf);
6255 if (bfd_bwrite (buf, 4, abfd) != 4)
6256 return FALSE;
6258 l += intopt.size;
6262 if (hdr->bfd_section != NULL)
6264 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6266 /* .sbss is not handled specially here because the GNU/Linux
6267 prelinker can convert .sbss from NOBITS to PROGBITS and
6268 changing it back to NOBITS breaks the binary. The entry in
6269 _bfd_mips_elf_special_sections will ensure the correct flags
6270 are set on .sbss if BFD creates it without reading it from an
6271 input file, and without special handling here the flags set
6272 on it in an input file will be followed. */
6273 if (strcmp (name, ".sdata") == 0
6274 || strcmp (name, ".lit8") == 0
6275 || strcmp (name, ".lit4") == 0)
6277 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6278 hdr->sh_type = SHT_PROGBITS;
6280 else if (strcmp (name, ".srdata") == 0)
6282 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6283 hdr->sh_type = SHT_PROGBITS;
6285 else if (strcmp (name, ".compact_rel") == 0)
6287 hdr->sh_flags = 0;
6288 hdr->sh_type = SHT_PROGBITS;
6290 else if (strcmp (name, ".rtproc") == 0)
6292 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6294 unsigned int adjust;
6296 adjust = hdr->sh_size % hdr->sh_addralign;
6297 if (adjust != 0)
6298 hdr->sh_size += hdr->sh_addralign - adjust;
6303 return TRUE;
6306 /* Handle a MIPS specific section when reading an object file. This
6307 is called when elfcode.h finds a section with an unknown type.
6308 This routine supports both the 32-bit and 64-bit ELF ABI.
6310 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6311 how to. */
6313 bfd_boolean
6314 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6315 Elf_Internal_Shdr *hdr,
6316 const char *name,
6317 int shindex)
6319 flagword flags = 0;
6321 /* There ought to be a place to keep ELF backend specific flags, but
6322 at the moment there isn't one. We just keep track of the
6323 sections by their name, instead. Fortunately, the ABI gives
6324 suggested names for all the MIPS specific sections, so we will
6325 probably get away with this. */
6326 switch (hdr->sh_type)
6328 case SHT_MIPS_LIBLIST:
6329 if (strcmp (name, ".liblist") != 0)
6330 return FALSE;
6331 break;
6332 case SHT_MIPS_MSYM:
6333 if (strcmp (name, ".msym") != 0)
6334 return FALSE;
6335 break;
6336 case SHT_MIPS_CONFLICT:
6337 if (strcmp (name, ".conflict") != 0)
6338 return FALSE;
6339 break;
6340 case SHT_MIPS_GPTAB:
6341 if (! CONST_STRNEQ (name, ".gptab."))
6342 return FALSE;
6343 break;
6344 case SHT_MIPS_UCODE:
6345 if (strcmp (name, ".ucode") != 0)
6346 return FALSE;
6347 break;
6348 case SHT_MIPS_DEBUG:
6349 if (strcmp (name, ".mdebug") != 0)
6350 return FALSE;
6351 flags = SEC_DEBUGGING;
6352 break;
6353 case SHT_MIPS_REGINFO:
6354 if (strcmp (name, ".reginfo") != 0
6355 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6356 return FALSE;
6357 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6358 break;
6359 case SHT_MIPS_IFACE:
6360 if (strcmp (name, ".MIPS.interfaces") != 0)
6361 return FALSE;
6362 break;
6363 case SHT_MIPS_CONTENT:
6364 if (! CONST_STRNEQ (name, ".MIPS.content"))
6365 return FALSE;
6366 break;
6367 case SHT_MIPS_OPTIONS:
6368 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6369 return FALSE;
6370 break;
6371 case SHT_MIPS_DWARF:
6372 if (! CONST_STRNEQ (name, ".debug_")
6373 && ! CONST_STRNEQ (name, ".zdebug_"))
6374 return FALSE;
6375 break;
6376 case SHT_MIPS_SYMBOL_LIB:
6377 if (strcmp (name, ".MIPS.symlib") != 0)
6378 return FALSE;
6379 break;
6380 case SHT_MIPS_EVENTS:
6381 if (! CONST_STRNEQ (name, ".MIPS.events")
6382 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6383 return FALSE;
6384 break;
6385 default:
6386 break;
6389 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6390 return FALSE;
6392 if (flags)
6394 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6395 (bfd_get_section_flags (abfd,
6396 hdr->bfd_section)
6397 | flags)))
6398 return FALSE;
6401 /* FIXME: We should record sh_info for a .gptab section. */
6403 /* For a .reginfo section, set the gp value in the tdata information
6404 from the contents of this section. We need the gp value while
6405 processing relocs, so we just get it now. The .reginfo section
6406 is not used in the 64-bit MIPS ELF ABI. */
6407 if (hdr->sh_type == SHT_MIPS_REGINFO)
6409 Elf32_External_RegInfo ext;
6410 Elf32_RegInfo s;
6412 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6413 &ext, 0, sizeof ext))
6414 return FALSE;
6415 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6416 elf_gp (abfd) = s.ri_gp_value;
6419 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6420 set the gp value based on what we find. We may see both
6421 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6422 they should agree. */
6423 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6425 bfd_byte *contents, *l, *lend;
6427 contents = bfd_malloc (hdr->sh_size);
6428 if (contents == NULL)
6429 return FALSE;
6430 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6431 0, hdr->sh_size))
6433 free (contents);
6434 return FALSE;
6436 l = contents;
6437 lend = contents + hdr->sh_size;
6438 while (l + sizeof (Elf_External_Options) <= lend)
6440 Elf_Internal_Options intopt;
6442 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6443 &intopt);
6444 if (intopt.size < sizeof (Elf_External_Options))
6446 (*_bfd_error_handler)
6447 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6448 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6449 break;
6451 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6453 Elf64_Internal_RegInfo intreg;
6455 bfd_mips_elf64_swap_reginfo_in
6456 (abfd,
6457 ((Elf64_External_RegInfo *)
6458 (l + sizeof (Elf_External_Options))),
6459 &intreg);
6460 elf_gp (abfd) = intreg.ri_gp_value;
6462 else if (intopt.kind == ODK_REGINFO)
6464 Elf32_RegInfo intreg;
6466 bfd_mips_elf32_swap_reginfo_in
6467 (abfd,
6468 ((Elf32_External_RegInfo *)
6469 (l + sizeof (Elf_External_Options))),
6470 &intreg);
6471 elf_gp (abfd) = intreg.ri_gp_value;
6473 l += intopt.size;
6475 free (contents);
6478 return TRUE;
6481 /* Set the correct type for a MIPS ELF section. We do this by the
6482 section name, which is a hack, but ought to work. This routine is
6483 used by both the 32-bit and the 64-bit ABI. */
6485 bfd_boolean
6486 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6488 const char *name = bfd_get_section_name (abfd, sec);
6490 if (strcmp (name, ".liblist") == 0)
6492 hdr->sh_type = SHT_MIPS_LIBLIST;
6493 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6494 /* The sh_link field is set in final_write_processing. */
6496 else if (strcmp (name, ".conflict") == 0)
6497 hdr->sh_type = SHT_MIPS_CONFLICT;
6498 else if (CONST_STRNEQ (name, ".gptab."))
6500 hdr->sh_type = SHT_MIPS_GPTAB;
6501 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6502 /* The sh_info field is set in final_write_processing. */
6504 else if (strcmp (name, ".ucode") == 0)
6505 hdr->sh_type = SHT_MIPS_UCODE;
6506 else if (strcmp (name, ".mdebug") == 0)
6508 hdr->sh_type = SHT_MIPS_DEBUG;
6509 /* In a shared object on IRIX 5.3, the .mdebug section has an
6510 entsize of 0. FIXME: Does this matter? */
6511 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6512 hdr->sh_entsize = 0;
6513 else
6514 hdr->sh_entsize = 1;
6516 else if (strcmp (name, ".reginfo") == 0)
6518 hdr->sh_type = SHT_MIPS_REGINFO;
6519 /* In a shared object on IRIX 5.3, the .reginfo section has an
6520 entsize of 0x18. FIXME: Does this matter? */
6521 if (SGI_COMPAT (abfd))
6523 if ((abfd->flags & DYNAMIC) != 0)
6524 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6525 else
6526 hdr->sh_entsize = 1;
6528 else
6529 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6531 else if (SGI_COMPAT (abfd)
6532 && (strcmp (name, ".hash") == 0
6533 || strcmp (name, ".dynamic") == 0
6534 || strcmp (name, ".dynstr") == 0))
6536 if (SGI_COMPAT (abfd))
6537 hdr->sh_entsize = 0;
6538 #if 0
6539 /* This isn't how the IRIX6 linker behaves. */
6540 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6541 #endif
6543 else if (strcmp (name, ".got") == 0
6544 || strcmp (name, ".srdata") == 0
6545 || strcmp (name, ".sdata") == 0
6546 || strcmp (name, ".sbss") == 0
6547 || strcmp (name, ".lit4") == 0
6548 || strcmp (name, ".lit8") == 0)
6549 hdr->sh_flags |= SHF_MIPS_GPREL;
6550 else if (strcmp (name, ".MIPS.interfaces") == 0)
6552 hdr->sh_type = SHT_MIPS_IFACE;
6553 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6555 else if (CONST_STRNEQ (name, ".MIPS.content"))
6557 hdr->sh_type = SHT_MIPS_CONTENT;
6558 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6559 /* The sh_info field is set in final_write_processing. */
6561 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6563 hdr->sh_type = SHT_MIPS_OPTIONS;
6564 hdr->sh_entsize = 1;
6565 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6567 else if (CONST_STRNEQ (name, ".debug_")
6568 || CONST_STRNEQ (name, ".zdebug_"))
6570 hdr->sh_type = SHT_MIPS_DWARF;
6572 /* Irix facilities such as libexc expect a single .debug_frame
6573 per executable, the system ones have NOSTRIP set and the linker
6574 doesn't merge sections with different flags so ... */
6575 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6576 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6578 else if (strcmp (name, ".MIPS.symlib") == 0)
6580 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6581 /* The sh_link and sh_info fields are set in
6582 final_write_processing. */
6584 else if (CONST_STRNEQ (name, ".MIPS.events")
6585 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6587 hdr->sh_type = SHT_MIPS_EVENTS;
6588 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6589 /* The sh_link field is set in final_write_processing. */
6591 else if (strcmp (name, ".msym") == 0)
6593 hdr->sh_type = SHT_MIPS_MSYM;
6594 hdr->sh_flags |= SHF_ALLOC;
6595 hdr->sh_entsize = 8;
6598 /* The generic elf_fake_sections will set up REL_HDR using the default
6599 kind of relocations. We used to set up a second header for the
6600 non-default kind of relocations here, but only NewABI would use
6601 these, and the IRIX ld doesn't like resulting empty RELA sections.
6602 Thus we create those header only on demand now. */
6604 return TRUE;
6607 /* Given a BFD section, try to locate the corresponding ELF section
6608 index. This is used by both the 32-bit and the 64-bit ABI.
6609 Actually, it's not clear to me that the 64-bit ABI supports these,
6610 but for non-PIC objects we will certainly want support for at least
6611 the .scommon section. */
6613 bfd_boolean
6614 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6615 asection *sec, int *retval)
6617 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6619 *retval = SHN_MIPS_SCOMMON;
6620 return TRUE;
6622 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6624 *retval = SHN_MIPS_ACOMMON;
6625 return TRUE;
6627 return FALSE;
6630 /* Hook called by the linker routine which adds symbols from an object
6631 file. We must handle the special MIPS section numbers here. */
6633 bfd_boolean
6634 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6635 Elf_Internal_Sym *sym, const char **namep,
6636 flagword *flagsp ATTRIBUTE_UNUSED,
6637 asection **secp, bfd_vma *valp)
6639 if (SGI_COMPAT (abfd)
6640 && (abfd->flags & DYNAMIC) != 0
6641 && strcmp (*namep, "_rld_new_interface") == 0)
6643 /* Skip IRIX5 rld entry name. */
6644 *namep = NULL;
6645 return TRUE;
6648 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6649 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6650 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6651 a magic symbol resolved by the linker, we ignore this bogus definition
6652 of _gp_disp. New ABI objects do not suffer from this problem so this
6653 is not done for them. */
6654 if (!NEWABI_P(abfd)
6655 && (sym->st_shndx == SHN_ABS)
6656 && (strcmp (*namep, "_gp_disp") == 0))
6658 *namep = NULL;
6659 return TRUE;
6662 switch (sym->st_shndx)
6664 case SHN_COMMON:
6665 /* Common symbols less than the GP size are automatically
6666 treated as SHN_MIPS_SCOMMON symbols. */
6667 if (sym->st_size > elf_gp_size (abfd)
6668 || ELF_ST_TYPE (sym->st_info) == STT_TLS
6669 || IRIX_COMPAT (abfd) == ict_irix6)
6670 break;
6671 /* Fall through. */
6672 case SHN_MIPS_SCOMMON:
6673 *secp = bfd_make_section_old_way (abfd, ".scommon");
6674 (*secp)->flags |= SEC_IS_COMMON;
6675 *valp = sym->st_size;
6676 break;
6678 case SHN_MIPS_TEXT:
6679 /* This section is used in a shared object. */
6680 if (elf_tdata (abfd)->elf_text_section == NULL)
6682 asymbol *elf_text_symbol;
6683 asection *elf_text_section;
6684 bfd_size_type amt = sizeof (asection);
6686 elf_text_section = bfd_zalloc (abfd, amt);
6687 if (elf_text_section == NULL)
6688 return FALSE;
6690 amt = sizeof (asymbol);
6691 elf_text_symbol = bfd_zalloc (abfd, amt);
6692 if (elf_text_symbol == NULL)
6693 return FALSE;
6695 /* Initialize the section. */
6697 elf_tdata (abfd)->elf_text_section = elf_text_section;
6698 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6700 elf_text_section->symbol = elf_text_symbol;
6701 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6703 elf_text_section->name = ".text";
6704 elf_text_section->flags = SEC_NO_FLAGS;
6705 elf_text_section->output_section = NULL;
6706 elf_text_section->owner = abfd;
6707 elf_text_symbol->name = ".text";
6708 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6709 elf_text_symbol->section = elf_text_section;
6711 /* This code used to do *secp = bfd_und_section_ptr if
6712 info->shared. I don't know why, and that doesn't make sense,
6713 so I took it out. */
6714 *secp = elf_tdata (abfd)->elf_text_section;
6715 break;
6717 case SHN_MIPS_ACOMMON:
6718 /* Fall through. XXX Can we treat this as allocated data? */
6719 case SHN_MIPS_DATA:
6720 /* This section is used in a shared object. */
6721 if (elf_tdata (abfd)->elf_data_section == NULL)
6723 asymbol *elf_data_symbol;
6724 asection *elf_data_section;
6725 bfd_size_type amt = sizeof (asection);
6727 elf_data_section = bfd_zalloc (abfd, amt);
6728 if (elf_data_section == NULL)
6729 return FALSE;
6731 amt = sizeof (asymbol);
6732 elf_data_symbol = bfd_zalloc (abfd, amt);
6733 if (elf_data_symbol == NULL)
6734 return FALSE;
6736 /* Initialize the section. */
6738 elf_tdata (abfd)->elf_data_section = elf_data_section;
6739 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6741 elf_data_section->symbol = elf_data_symbol;
6742 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6744 elf_data_section->name = ".data";
6745 elf_data_section->flags = SEC_NO_FLAGS;
6746 elf_data_section->output_section = NULL;
6747 elf_data_section->owner = abfd;
6748 elf_data_symbol->name = ".data";
6749 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6750 elf_data_symbol->section = elf_data_section;
6752 /* This code used to do *secp = bfd_und_section_ptr if
6753 info->shared. I don't know why, and that doesn't make sense,
6754 so I took it out. */
6755 *secp = elf_tdata (abfd)->elf_data_section;
6756 break;
6758 case SHN_MIPS_SUNDEFINED:
6759 *secp = bfd_und_section_ptr;
6760 break;
6763 if (SGI_COMPAT (abfd)
6764 && ! info->shared
6765 && info->output_bfd->xvec == abfd->xvec
6766 && strcmp (*namep, "__rld_obj_head") == 0)
6768 struct elf_link_hash_entry *h;
6769 struct bfd_link_hash_entry *bh;
6771 /* Mark __rld_obj_head as dynamic. */
6772 bh = NULL;
6773 if (! (_bfd_generic_link_add_one_symbol
6774 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
6775 get_elf_backend_data (abfd)->collect, &bh)))
6776 return FALSE;
6778 h = (struct elf_link_hash_entry *) bh;
6779 h->non_elf = 0;
6780 h->def_regular = 1;
6781 h->type = STT_OBJECT;
6783 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6784 return FALSE;
6786 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
6789 /* If this is a mips16 text symbol, add 1 to the value to make it
6790 odd. This will cause something like .word SYM to come up with
6791 the right value when it is loaded into the PC. */
6792 if (ELF_ST_IS_MIPS16 (sym->st_other))
6793 ++*valp;
6795 return TRUE;
6798 /* This hook function is called before the linker writes out a global
6799 symbol. We mark symbols as small common if appropriate. This is
6800 also where we undo the increment of the value for a mips16 symbol. */
6803 _bfd_mips_elf_link_output_symbol_hook
6804 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6805 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6806 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6808 /* If we see a common symbol, which implies a relocatable link, then
6809 if a symbol was small common in an input file, mark it as small
6810 common in the output file. */
6811 if (sym->st_shndx == SHN_COMMON
6812 && strcmp (input_sec->name, ".scommon") == 0)
6813 sym->st_shndx = SHN_MIPS_SCOMMON;
6815 if (ELF_ST_IS_MIPS16 (sym->st_other))
6816 sym->st_value &= ~1;
6818 return 1;
6821 /* Functions for the dynamic linker. */
6823 /* Create dynamic sections when linking against a dynamic object. */
6825 bfd_boolean
6826 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
6828 struct elf_link_hash_entry *h;
6829 struct bfd_link_hash_entry *bh;
6830 flagword flags;
6831 register asection *s;
6832 const char * const *namep;
6833 struct mips_elf_link_hash_table *htab;
6835 htab = mips_elf_hash_table (info);
6836 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6837 | SEC_LINKER_CREATED | SEC_READONLY);
6839 /* The psABI requires a read-only .dynamic section, but the VxWorks
6840 EABI doesn't. */
6841 if (!htab->is_vxworks)
6843 s = bfd_get_section_by_name (abfd, ".dynamic");
6844 if (s != NULL)
6846 if (! bfd_set_section_flags (abfd, s, flags))
6847 return FALSE;
6851 /* We need to create .got section. */
6852 if (!mips_elf_create_got_section (abfd, info))
6853 return FALSE;
6855 if (! mips_elf_rel_dyn_section (info, TRUE))
6856 return FALSE;
6858 /* Create .stub section. */
6859 s = bfd_make_section_with_flags (abfd,
6860 MIPS_ELF_STUB_SECTION_NAME (abfd),
6861 flags | SEC_CODE);
6862 if (s == NULL
6863 || ! bfd_set_section_alignment (abfd, s,
6864 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6865 return FALSE;
6866 htab->sstubs = s;
6868 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6869 && !info->shared
6870 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6872 s = bfd_make_section_with_flags (abfd, ".rld_map",
6873 flags &~ (flagword) SEC_READONLY);
6874 if (s == NULL
6875 || ! bfd_set_section_alignment (abfd, s,
6876 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6877 return FALSE;
6880 /* On IRIX5, we adjust add some additional symbols and change the
6881 alignments of several sections. There is no ABI documentation
6882 indicating that this is necessary on IRIX6, nor any evidence that
6883 the linker takes such action. */
6884 if (IRIX_COMPAT (abfd) == ict_irix5)
6886 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6888 bh = NULL;
6889 if (! (_bfd_generic_link_add_one_symbol
6890 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6891 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6892 return FALSE;
6894 h = (struct elf_link_hash_entry *) bh;
6895 h->non_elf = 0;
6896 h->def_regular = 1;
6897 h->type = STT_SECTION;
6899 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6900 return FALSE;
6903 /* We need to create a .compact_rel section. */
6904 if (SGI_COMPAT (abfd))
6906 if (!mips_elf_create_compact_rel_section (abfd, info))
6907 return FALSE;
6910 /* Change alignments of some sections. */
6911 s = bfd_get_section_by_name (abfd, ".hash");
6912 if (s != NULL)
6913 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6914 s = bfd_get_section_by_name (abfd, ".dynsym");
6915 if (s != NULL)
6916 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6917 s = bfd_get_section_by_name (abfd, ".dynstr");
6918 if (s != NULL)
6919 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6920 s = bfd_get_section_by_name (abfd, ".reginfo");
6921 if (s != NULL)
6922 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6923 s = bfd_get_section_by_name (abfd, ".dynamic");
6924 if (s != NULL)
6925 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6928 if (!info->shared)
6930 const char *name;
6932 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6933 bh = NULL;
6934 if (!(_bfd_generic_link_add_one_symbol
6935 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6936 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6937 return FALSE;
6939 h = (struct elf_link_hash_entry *) bh;
6940 h->non_elf = 0;
6941 h->def_regular = 1;
6942 h->type = STT_SECTION;
6944 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6945 return FALSE;
6947 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6949 /* __rld_map is a four byte word located in the .data section
6950 and is filled in by the rtld to contain a pointer to
6951 the _r_debug structure. Its symbol value will be set in
6952 _bfd_mips_elf_finish_dynamic_symbol. */
6953 s = bfd_get_section_by_name (abfd, ".rld_map");
6954 BFD_ASSERT (s != NULL);
6956 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6957 bh = NULL;
6958 if (!(_bfd_generic_link_add_one_symbol
6959 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6960 get_elf_backend_data (abfd)->collect, &bh)))
6961 return FALSE;
6963 h = (struct elf_link_hash_entry *) bh;
6964 h->non_elf = 0;
6965 h->def_regular = 1;
6966 h->type = STT_OBJECT;
6968 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6969 return FALSE;
6973 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
6974 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
6975 if (!_bfd_elf_create_dynamic_sections (abfd, info))
6976 return FALSE;
6978 /* Cache the sections created above. */
6979 htab->splt = bfd_get_section_by_name (abfd, ".plt");
6980 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
6981 if (htab->is_vxworks)
6983 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
6984 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
6986 else
6987 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
6988 if (!htab->sdynbss
6989 || (htab->is_vxworks && !htab->srelbss && !info->shared)
6990 || !htab->srelplt
6991 || !htab->splt)
6992 abort ();
6994 if (htab->is_vxworks)
6996 /* Do the usual VxWorks handling. */
6997 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
6998 return FALSE;
7000 /* Work out the PLT sizes. */
7001 if (info->shared)
7003 htab->plt_header_size
7004 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7005 htab->plt_entry_size
7006 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7008 else
7010 htab->plt_header_size
7011 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7012 htab->plt_entry_size
7013 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7016 else if (!info->shared)
7018 /* All variants of the plt0 entry are the same size. */
7019 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7020 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7023 return TRUE;
7026 /* Return true if relocation REL against section SEC is a REL rather than
7027 RELA relocation. RELOCS is the first relocation in the section and
7028 ABFD is the bfd that contains SEC. */
7030 static bfd_boolean
7031 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7032 const Elf_Internal_Rela *relocs,
7033 const Elf_Internal_Rela *rel)
7035 Elf_Internal_Shdr *rel_hdr;
7036 const struct elf_backend_data *bed;
7038 /* To determine which flavor or relocation this is, we depend on the
7039 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
7040 rel_hdr = &elf_section_data (sec)->rel_hdr;
7041 bed = get_elf_backend_data (abfd);
7042 if ((size_t) (rel - relocs)
7043 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7044 rel_hdr = elf_section_data (sec)->rel_hdr2;
7045 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
7048 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7049 HOWTO is the relocation's howto and CONTENTS points to the contents
7050 of the section that REL is against. */
7052 static bfd_vma
7053 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7054 reloc_howto_type *howto, bfd_byte *contents)
7056 bfd_byte *location;
7057 unsigned int r_type;
7058 bfd_vma addend;
7060 r_type = ELF_R_TYPE (abfd, rel->r_info);
7061 location = contents + rel->r_offset;
7063 /* Get the addend, which is stored in the input file. */
7064 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7065 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7066 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7068 return addend & howto->src_mask;
7071 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7072 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7073 and update *ADDEND with the final addend. Return true on success
7074 or false if the LO16 could not be found. RELEND is the exclusive
7075 upper bound on the relocations for REL's section. */
7077 static bfd_boolean
7078 mips_elf_add_lo16_rel_addend (bfd *abfd,
7079 const Elf_Internal_Rela *rel,
7080 const Elf_Internal_Rela *relend,
7081 bfd_byte *contents, bfd_vma *addend)
7083 unsigned int r_type, lo16_type;
7084 const Elf_Internal_Rela *lo16_relocation;
7085 reloc_howto_type *lo16_howto;
7086 bfd_vma l;
7088 r_type = ELF_R_TYPE (abfd, rel->r_info);
7089 if (mips16_reloc_p (r_type))
7090 lo16_type = R_MIPS16_LO16;
7091 else
7092 lo16_type = R_MIPS_LO16;
7094 /* The combined value is the sum of the HI16 addend, left-shifted by
7095 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7096 code does a `lui' of the HI16 value, and then an `addiu' of the
7097 LO16 value.)
7099 Scan ahead to find a matching LO16 relocation.
7101 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7102 be immediately following. However, for the IRIX6 ABI, the next
7103 relocation may be a composed relocation consisting of several
7104 relocations for the same address. In that case, the R_MIPS_LO16
7105 relocation may occur as one of these. We permit a similar
7106 extension in general, as that is useful for GCC.
7108 In some cases GCC dead code elimination removes the LO16 but keeps
7109 the corresponding HI16. This is strictly speaking a violation of
7110 the ABI but not immediately harmful. */
7111 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7112 if (lo16_relocation == NULL)
7113 return FALSE;
7115 /* Obtain the addend kept there. */
7116 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7117 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7119 l <<= lo16_howto->rightshift;
7120 l = _bfd_mips_elf_sign_extend (l, 16);
7122 *addend <<= 16;
7123 *addend += l;
7124 return TRUE;
7127 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7128 store the contents in *CONTENTS on success. Assume that *CONTENTS
7129 already holds the contents if it is nonull on entry. */
7131 static bfd_boolean
7132 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7134 if (*contents)
7135 return TRUE;
7137 /* Get cached copy if it exists. */
7138 if (elf_section_data (sec)->this_hdr.contents != NULL)
7140 *contents = elf_section_data (sec)->this_hdr.contents;
7141 return TRUE;
7144 return bfd_malloc_and_get_section (abfd, sec, contents);
7147 /* Look through the relocs for a section during the first phase, and
7148 allocate space in the global offset table. */
7150 bfd_boolean
7151 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7152 asection *sec, const Elf_Internal_Rela *relocs)
7154 const char *name;
7155 bfd *dynobj;
7156 Elf_Internal_Shdr *symtab_hdr;
7157 struct elf_link_hash_entry **sym_hashes;
7158 size_t extsymoff;
7159 const Elf_Internal_Rela *rel;
7160 const Elf_Internal_Rela *rel_end;
7161 asection *sreloc;
7162 const struct elf_backend_data *bed;
7163 struct mips_elf_link_hash_table *htab;
7164 bfd_byte *contents;
7165 bfd_vma addend;
7166 reloc_howto_type *howto;
7168 if (info->relocatable)
7169 return TRUE;
7171 htab = mips_elf_hash_table (info);
7172 dynobj = elf_hash_table (info)->dynobj;
7173 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7174 sym_hashes = elf_sym_hashes (abfd);
7175 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7177 bed = get_elf_backend_data (abfd);
7178 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7180 /* Check for the mips16 stub sections. */
7182 name = bfd_get_section_name (abfd, sec);
7183 if (FN_STUB_P (name))
7185 unsigned long r_symndx;
7187 /* Look at the relocation information to figure out which symbol
7188 this is for. */
7190 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7191 if (r_symndx == 0)
7193 (*_bfd_error_handler)
7194 (_("%B: Warning: cannot determine the target function for"
7195 " stub section `%s'"),
7196 abfd, name);
7197 bfd_set_error (bfd_error_bad_value);
7198 return FALSE;
7201 if (r_symndx < extsymoff
7202 || sym_hashes[r_symndx - extsymoff] == NULL)
7204 asection *o;
7206 /* This stub is for a local symbol. This stub will only be
7207 needed if there is some relocation in this BFD, other
7208 than a 16 bit function call, which refers to this symbol. */
7209 for (o = abfd->sections; o != NULL; o = o->next)
7211 Elf_Internal_Rela *sec_relocs;
7212 const Elf_Internal_Rela *r, *rend;
7214 /* We can ignore stub sections when looking for relocs. */
7215 if ((o->flags & SEC_RELOC) == 0
7216 || o->reloc_count == 0
7217 || section_allows_mips16_refs_p (o))
7218 continue;
7220 sec_relocs
7221 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7222 info->keep_memory);
7223 if (sec_relocs == NULL)
7224 return FALSE;
7226 rend = sec_relocs + o->reloc_count;
7227 for (r = sec_relocs; r < rend; r++)
7228 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7229 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7230 break;
7232 if (elf_section_data (o)->relocs != sec_relocs)
7233 free (sec_relocs);
7235 if (r < rend)
7236 break;
7239 if (o == NULL)
7241 /* There is no non-call reloc for this stub, so we do
7242 not need it. Since this function is called before
7243 the linker maps input sections to output sections, we
7244 can easily discard it by setting the SEC_EXCLUDE
7245 flag. */
7246 sec->flags |= SEC_EXCLUDE;
7247 return TRUE;
7250 /* Record this stub in an array of local symbol stubs for
7251 this BFD. */
7252 if (elf_tdata (abfd)->local_stubs == NULL)
7254 unsigned long symcount;
7255 asection **n;
7256 bfd_size_type amt;
7258 if (elf_bad_symtab (abfd))
7259 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7260 else
7261 symcount = symtab_hdr->sh_info;
7262 amt = symcount * sizeof (asection *);
7263 n = bfd_zalloc (abfd, amt);
7264 if (n == NULL)
7265 return FALSE;
7266 elf_tdata (abfd)->local_stubs = n;
7269 sec->flags |= SEC_KEEP;
7270 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7272 /* We don't need to set mips16_stubs_seen in this case.
7273 That flag is used to see whether we need to look through
7274 the global symbol table for stubs. We don't need to set
7275 it here, because we just have a local stub. */
7277 else
7279 struct mips_elf_link_hash_entry *h;
7281 h = ((struct mips_elf_link_hash_entry *)
7282 sym_hashes[r_symndx - extsymoff]);
7284 while (h->root.root.type == bfd_link_hash_indirect
7285 || h->root.root.type == bfd_link_hash_warning)
7286 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7288 /* H is the symbol this stub is for. */
7290 /* If we already have an appropriate stub for this function, we
7291 don't need another one, so we can discard this one. Since
7292 this function is called before the linker maps input sections
7293 to output sections, we can easily discard it by setting the
7294 SEC_EXCLUDE flag. */
7295 if (h->fn_stub != NULL)
7297 sec->flags |= SEC_EXCLUDE;
7298 return TRUE;
7301 sec->flags |= SEC_KEEP;
7302 h->fn_stub = sec;
7303 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7306 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7308 unsigned long r_symndx;
7309 struct mips_elf_link_hash_entry *h;
7310 asection **loc;
7312 /* Look at the relocation information to figure out which symbol
7313 this is for. */
7315 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7316 if (r_symndx == 0)
7318 (*_bfd_error_handler)
7319 (_("%B: Warning: cannot determine the target function for"
7320 " stub section `%s'"),
7321 abfd, name);
7322 bfd_set_error (bfd_error_bad_value);
7323 return FALSE;
7326 if (r_symndx < extsymoff
7327 || sym_hashes[r_symndx - extsymoff] == NULL)
7329 asection *o;
7331 /* This stub is for a local symbol. This stub will only be
7332 needed if there is some relocation (R_MIPS16_26) in this BFD
7333 that refers to this symbol. */
7334 for (o = abfd->sections; o != NULL; o = o->next)
7336 Elf_Internal_Rela *sec_relocs;
7337 const Elf_Internal_Rela *r, *rend;
7339 /* We can ignore stub sections when looking for relocs. */
7340 if ((o->flags & SEC_RELOC) == 0
7341 || o->reloc_count == 0
7342 || section_allows_mips16_refs_p (o))
7343 continue;
7345 sec_relocs
7346 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7347 info->keep_memory);
7348 if (sec_relocs == NULL)
7349 return FALSE;
7351 rend = sec_relocs + o->reloc_count;
7352 for (r = sec_relocs; r < rend; r++)
7353 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7354 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7355 break;
7357 if (elf_section_data (o)->relocs != sec_relocs)
7358 free (sec_relocs);
7360 if (r < rend)
7361 break;
7364 if (o == NULL)
7366 /* There is no non-call reloc for this stub, so we do
7367 not need it. Since this function is called before
7368 the linker maps input sections to output sections, we
7369 can easily discard it by setting the SEC_EXCLUDE
7370 flag. */
7371 sec->flags |= SEC_EXCLUDE;
7372 return TRUE;
7375 /* Record this stub in an array of local symbol call_stubs for
7376 this BFD. */
7377 if (elf_tdata (abfd)->local_call_stubs == NULL)
7379 unsigned long symcount;
7380 asection **n;
7381 bfd_size_type amt;
7383 if (elf_bad_symtab (abfd))
7384 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7385 else
7386 symcount = symtab_hdr->sh_info;
7387 amt = symcount * sizeof (asection *);
7388 n = bfd_zalloc (abfd, amt);
7389 if (n == NULL)
7390 return FALSE;
7391 elf_tdata (abfd)->local_call_stubs = n;
7394 sec->flags |= SEC_KEEP;
7395 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7397 /* We don't need to set mips16_stubs_seen in this case.
7398 That flag is used to see whether we need to look through
7399 the global symbol table for stubs. We don't need to set
7400 it here, because we just have a local stub. */
7402 else
7404 h = ((struct mips_elf_link_hash_entry *)
7405 sym_hashes[r_symndx - extsymoff]);
7407 /* H is the symbol this stub is for. */
7409 if (CALL_FP_STUB_P (name))
7410 loc = &h->call_fp_stub;
7411 else
7412 loc = &h->call_stub;
7414 /* If we already have an appropriate stub for this function, we
7415 don't need another one, so we can discard this one. Since
7416 this function is called before the linker maps input sections
7417 to output sections, we can easily discard it by setting the
7418 SEC_EXCLUDE flag. */
7419 if (*loc != NULL)
7421 sec->flags |= SEC_EXCLUDE;
7422 return TRUE;
7425 sec->flags |= SEC_KEEP;
7426 *loc = sec;
7427 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7431 sreloc = NULL;
7432 contents = NULL;
7433 for (rel = relocs; rel < rel_end; ++rel)
7435 unsigned long r_symndx;
7436 unsigned int r_type;
7437 struct elf_link_hash_entry *h;
7438 bfd_boolean can_make_dynamic_p;
7440 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7441 r_type = ELF_R_TYPE (abfd, rel->r_info);
7443 if (r_symndx < extsymoff)
7444 h = NULL;
7445 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7447 (*_bfd_error_handler)
7448 (_("%B: Malformed reloc detected for section %s"),
7449 abfd, name);
7450 bfd_set_error (bfd_error_bad_value);
7451 return FALSE;
7453 else
7455 h = sym_hashes[r_symndx - extsymoff];
7456 while (h != NULL
7457 && (h->root.type == bfd_link_hash_indirect
7458 || h->root.type == bfd_link_hash_warning))
7459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7462 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7463 relocation into a dynamic one. */
7464 can_make_dynamic_p = FALSE;
7465 switch (r_type)
7467 case R_MIPS16_GOT16:
7468 case R_MIPS16_CALL16:
7469 case R_MIPS_GOT16:
7470 case R_MIPS_CALL16:
7471 case R_MIPS_CALL_HI16:
7472 case R_MIPS_CALL_LO16:
7473 case R_MIPS_GOT_HI16:
7474 case R_MIPS_GOT_LO16:
7475 case R_MIPS_GOT_PAGE:
7476 case R_MIPS_GOT_OFST:
7477 case R_MIPS_GOT_DISP:
7478 case R_MIPS_TLS_GOTTPREL:
7479 case R_MIPS_TLS_GD:
7480 case R_MIPS_TLS_LDM:
7481 if (dynobj == NULL)
7482 elf_hash_table (info)->dynobj = dynobj = abfd;
7483 if (!mips_elf_create_got_section (dynobj, info))
7484 return FALSE;
7485 if (htab->is_vxworks && !info->shared)
7487 (*_bfd_error_handler)
7488 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7489 abfd, (unsigned long) rel->r_offset);
7490 bfd_set_error (bfd_error_bad_value);
7491 return FALSE;
7493 break;
7495 case R_MIPS_32:
7496 case R_MIPS_REL32:
7497 case R_MIPS_64:
7498 /* In VxWorks executables, references to external symbols
7499 must be handled using copy relocs or PLT entries; it is not
7500 possible to convert this relocation into a dynamic one.
7502 For executables that use PLTs and copy-relocs, we have a
7503 choice between converting the relocation into a dynamic
7504 one or using copy relocations or PLT entries. It is
7505 usually better to do the former, unless the relocation is
7506 against a read-only section. */
7507 if ((info->shared
7508 || (h != NULL
7509 && !htab->is_vxworks
7510 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7511 && !(!info->nocopyreloc
7512 && !PIC_OBJECT_P (abfd)
7513 && MIPS_ELF_READONLY_SECTION (sec))))
7514 && (sec->flags & SEC_ALLOC) != 0)
7516 can_make_dynamic_p = TRUE;
7517 if (dynobj == NULL)
7518 elf_hash_table (info)->dynobj = dynobj = abfd;
7519 break;
7521 /* Fall through. */
7523 default:
7524 /* Most static relocations require pointer equality, except
7525 for branches. */
7526 if (h)
7527 h->pointer_equality_needed = TRUE;
7528 /* Fall through. */
7530 case R_MIPS_26:
7531 case R_MIPS_PC16:
7532 case R_MIPS16_26:
7533 if (h)
7534 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7535 break;
7538 if (h)
7540 /* Relocations against the special VxWorks __GOTT_BASE__ and
7541 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7542 room for them in .rela.dyn. */
7543 if (is_gott_symbol (info, h))
7545 if (sreloc == NULL)
7547 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7548 if (sreloc == NULL)
7549 return FALSE;
7551 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7552 if (MIPS_ELF_READONLY_SECTION (sec))
7553 /* We tell the dynamic linker that there are
7554 relocations against the text segment. */
7555 info->flags |= DF_TEXTREL;
7558 else if (r_type == R_MIPS_CALL_LO16
7559 || r_type == R_MIPS_GOT_LO16
7560 || r_type == R_MIPS_GOT_DISP
7561 || (got16_reloc_p (r_type) && htab->is_vxworks))
7563 /* We may need a local GOT entry for this relocation. We
7564 don't count R_MIPS_GOT_PAGE because we can estimate the
7565 maximum number of pages needed by looking at the size of
7566 the segment. Similar comments apply to R_MIPS*_GOT16 and
7567 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7568 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7569 R_MIPS_CALL_HI16 because these are always followed by an
7570 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7571 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7572 rel->r_addend, info, 0))
7573 return FALSE;
7576 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7577 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7579 switch (r_type)
7581 case R_MIPS_CALL16:
7582 case R_MIPS16_CALL16:
7583 if (h == NULL)
7585 (*_bfd_error_handler)
7586 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7587 abfd, (unsigned long) rel->r_offset);
7588 bfd_set_error (bfd_error_bad_value);
7589 return FALSE;
7591 /* Fall through. */
7593 case R_MIPS_CALL_HI16:
7594 case R_MIPS_CALL_LO16:
7595 if (h != NULL)
7597 /* VxWorks call relocations point at the function's .got.plt
7598 entry, which will be allocated by adjust_dynamic_symbol.
7599 Otherwise, this symbol requires a global GOT entry. */
7600 if ((!htab->is_vxworks || h->forced_local)
7601 && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
7602 return FALSE;
7604 /* We need a stub, not a plt entry for the undefined
7605 function. But we record it as if it needs plt. See
7606 _bfd_elf_adjust_dynamic_symbol. */
7607 h->needs_plt = 1;
7608 h->type = STT_FUNC;
7610 break;
7612 case R_MIPS_GOT_PAGE:
7613 /* If this is a global, overridable symbol, GOT_PAGE will
7614 decay to GOT_DISP, so we'll need a GOT entry for it. */
7615 if (h)
7617 struct mips_elf_link_hash_entry *hmips =
7618 (struct mips_elf_link_hash_entry *) h;
7620 /* This symbol is definitely not overridable. */
7621 if (hmips->root.def_regular
7622 && ! (info->shared && ! info->symbolic
7623 && ! hmips->root.forced_local))
7624 h = NULL;
7626 /* Fall through. */
7628 case R_MIPS16_GOT16:
7629 case R_MIPS_GOT16:
7630 case R_MIPS_GOT_HI16:
7631 case R_MIPS_GOT_LO16:
7632 if (!h || r_type == R_MIPS_GOT_PAGE)
7634 /* This relocation needs (or may need, if h != NULL) a
7635 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7636 know for sure until we know whether the symbol is
7637 preemptible. */
7638 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7640 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7641 return FALSE;
7642 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7643 addend = mips_elf_read_rel_addend (abfd, rel,
7644 howto, contents);
7645 if (r_type == R_MIPS_GOT16)
7646 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7647 contents, &addend);
7648 else
7649 addend <<= howto->rightshift;
7651 else
7652 addend = rel->r_addend;
7653 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7654 addend))
7655 return FALSE;
7656 break;
7658 /* Fall through. */
7660 case R_MIPS_GOT_DISP:
7661 if (h && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
7662 return FALSE;
7663 break;
7665 case R_MIPS_TLS_GOTTPREL:
7666 if (info->shared)
7667 info->flags |= DF_STATIC_TLS;
7668 /* Fall through */
7670 case R_MIPS_TLS_LDM:
7671 if (r_type == R_MIPS_TLS_LDM)
7673 r_symndx = 0;
7674 h = NULL;
7676 /* Fall through */
7678 case R_MIPS_TLS_GD:
7679 /* This symbol requires a global offset table entry, or two
7680 for TLS GD relocations. */
7682 unsigned char flag = (r_type == R_MIPS_TLS_GD
7683 ? GOT_TLS_GD
7684 : r_type == R_MIPS_TLS_LDM
7685 ? GOT_TLS_LDM
7686 : GOT_TLS_IE);
7687 if (h != NULL)
7689 struct mips_elf_link_hash_entry *hmips =
7690 (struct mips_elf_link_hash_entry *) h;
7691 hmips->tls_type |= flag;
7693 if (h && !mips_elf_record_global_got_symbol (h, abfd,
7694 info, flag))
7695 return FALSE;
7697 else
7699 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7701 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7702 rel->r_addend,
7703 info, flag))
7704 return FALSE;
7707 break;
7709 case R_MIPS_32:
7710 case R_MIPS_REL32:
7711 case R_MIPS_64:
7712 /* In VxWorks executables, references to external symbols
7713 are handled using copy relocs or PLT stubs, so there's
7714 no need to add a .rela.dyn entry for this relocation. */
7715 if (can_make_dynamic_p)
7717 if (sreloc == NULL)
7719 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7720 if (sreloc == NULL)
7721 return FALSE;
7723 if (info->shared && h == NULL)
7725 /* When creating a shared object, we must copy these
7726 reloc types into the output file as R_MIPS_REL32
7727 relocs. Make room for this reloc in .rel(a).dyn. */
7728 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7729 if (MIPS_ELF_READONLY_SECTION (sec))
7730 /* We tell the dynamic linker that there are
7731 relocations against the text segment. */
7732 info->flags |= DF_TEXTREL;
7734 else
7736 struct mips_elf_link_hash_entry *hmips;
7738 /* For a shared object, we must copy this relocation
7739 unless the symbol turns out to be undefined and
7740 weak with non-default visibility, in which case
7741 it will be left as zero.
7743 We could elide R_MIPS_REL32 for locally binding symbols
7744 in shared libraries, but do not yet do so.
7746 For an executable, we only need to copy this
7747 reloc if the symbol is defined in a dynamic
7748 object. */
7749 hmips = (struct mips_elf_link_hash_entry *) h;
7750 ++hmips->possibly_dynamic_relocs;
7751 if (MIPS_ELF_READONLY_SECTION (sec))
7752 /* We need it to tell the dynamic linker if there
7753 are relocations against the text segment. */
7754 hmips->readonly_reloc = TRUE;
7758 if (SGI_COMPAT (abfd))
7759 mips_elf_hash_table (info)->compact_rel_size +=
7760 sizeof (Elf32_External_crinfo);
7761 break;
7763 case R_MIPS_26:
7764 case R_MIPS_GPREL16:
7765 case R_MIPS_LITERAL:
7766 case R_MIPS_GPREL32:
7767 if (SGI_COMPAT (abfd))
7768 mips_elf_hash_table (info)->compact_rel_size +=
7769 sizeof (Elf32_External_crinfo);
7770 break;
7772 /* This relocation describes the C++ object vtable hierarchy.
7773 Reconstruct it for later use during GC. */
7774 case R_MIPS_GNU_VTINHERIT:
7775 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7776 return FALSE;
7777 break;
7779 /* This relocation describes which C++ vtable entries are actually
7780 used. Record for later use during GC. */
7781 case R_MIPS_GNU_VTENTRY:
7782 BFD_ASSERT (h != NULL);
7783 if (h != NULL
7784 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7785 return FALSE;
7786 break;
7788 default:
7789 break;
7792 /* We must not create a stub for a symbol that has relocations
7793 related to taking the function's address. This doesn't apply to
7794 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7795 a normal .got entry. */
7796 if (!htab->is_vxworks && h != NULL)
7797 switch (r_type)
7799 default:
7800 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7801 break;
7802 case R_MIPS16_CALL16:
7803 case R_MIPS_CALL16:
7804 case R_MIPS_CALL_HI16:
7805 case R_MIPS_CALL_LO16:
7806 case R_MIPS_JALR:
7807 break;
7810 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7811 if there is one. We only need to handle global symbols here;
7812 we decide whether to keep or delete stubs for local symbols
7813 when processing the stub's relocations. */
7814 if (h != NULL
7815 && !mips16_call_reloc_p (r_type)
7816 && !section_allows_mips16_refs_p (sec))
7818 struct mips_elf_link_hash_entry *mh;
7820 mh = (struct mips_elf_link_hash_entry *) h;
7821 mh->need_fn_stub = TRUE;
7824 /* Refuse some position-dependent relocations when creating a
7825 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7826 not PIC, but we can create dynamic relocations and the result
7827 will be fine. Also do not refuse R_MIPS_LO16, which can be
7828 combined with R_MIPS_GOT16. */
7829 if (info->shared)
7831 switch (r_type)
7833 case R_MIPS16_HI16:
7834 case R_MIPS_HI16:
7835 case R_MIPS_HIGHER:
7836 case R_MIPS_HIGHEST:
7837 /* Don't refuse a high part relocation if it's against
7838 no symbol (e.g. part of a compound relocation). */
7839 if (r_symndx == 0)
7840 break;
7842 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7843 and has a special meaning. */
7844 if (!NEWABI_P (abfd) && h != NULL
7845 && strcmp (h->root.root.string, "_gp_disp") == 0)
7846 break;
7848 /* FALLTHROUGH */
7850 case R_MIPS16_26:
7851 case R_MIPS_26:
7852 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7853 (*_bfd_error_handler)
7854 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7855 abfd, howto->name,
7856 (h) ? h->root.root.string : "a local symbol");
7857 bfd_set_error (bfd_error_bad_value);
7858 return FALSE;
7859 default:
7860 break;
7865 return TRUE;
7868 bfd_boolean
7869 _bfd_mips_relax_section (bfd *abfd, asection *sec,
7870 struct bfd_link_info *link_info,
7871 bfd_boolean *again)
7873 Elf_Internal_Rela *internal_relocs;
7874 Elf_Internal_Rela *irel, *irelend;
7875 Elf_Internal_Shdr *symtab_hdr;
7876 bfd_byte *contents = NULL;
7877 size_t extsymoff;
7878 bfd_boolean changed_contents = FALSE;
7879 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7880 Elf_Internal_Sym *isymbuf = NULL;
7882 /* We are not currently changing any sizes, so only one pass. */
7883 *again = FALSE;
7885 if (link_info->relocatable)
7886 return TRUE;
7888 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7889 link_info->keep_memory);
7890 if (internal_relocs == NULL)
7891 return TRUE;
7893 irelend = internal_relocs + sec->reloc_count
7894 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7895 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7896 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7898 for (irel = internal_relocs; irel < irelend; irel++)
7900 bfd_vma symval;
7901 bfd_signed_vma sym_offset;
7902 unsigned int r_type;
7903 unsigned long r_symndx;
7904 asection *sym_sec;
7905 unsigned long instruction;
7907 /* Turn jalr into bgezal, and jr into beq, if they're marked
7908 with a JALR relocation, that indicate where they jump to.
7909 This saves some pipeline bubbles. */
7910 r_type = ELF_R_TYPE (abfd, irel->r_info);
7911 if (r_type != R_MIPS_JALR)
7912 continue;
7914 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7915 /* Compute the address of the jump target. */
7916 if (r_symndx >= extsymoff)
7918 struct mips_elf_link_hash_entry *h
7919 = ((struct mips_elf_link_hash_entry *)
7920 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7922 while (h->root.root.type == bfd_link_hash_indirect
7923 || h->root.root.type == bfd_link_hash_warning)
7924 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7926 /* If a symbol is undefined, or if it may be overridden,
7927 skip it. */
7928 if (! ((h->root.root.type == bfd_link_hash_defined
7929 || h->root.root.type == bfd_link_hash_defweak)
7930 && h->root.root.u.def.section)
7931 || (link_info->shared && ! link_info->symbolic
7932 && !h->root.forced_local))
7933 continue;
7935 sym_sec = h->root.root.u.def.section;
7936 if (sym_sec->output_section)
7937 symval = (h->root.root.u.def.value
7938 + sym_sec->output_section->vma
7939 + sym_sec->output_offset);
7940 else
7941 symval = h->root.root.u.def.value;
7943 else
7945 Elf_Internal_Sym *isym;
7947 /* Read this BFD's symbols if we haven't done so already. */
7948 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7950 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7951 if (isymbuf == NULL)
7952 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7953 symtab_hdr->sh_info, 0,
7954 NULL, NULL, NULL);
7955 if (isymbuf == NULL)
7956 goto relax_return;
7959 isym = isymbuf + r_symndx;
7960 if (isym->st_shndx == SHN_UNDEF)
7961 continue;
7962 else if (isym->st_shndx == SHN_ABS)
7963 sym_sec = bfd_abs_section_ptr;
7964 else if (isym->st_shndx == SHN_COMMON)
7965 sym_sec = bfd_com_section_ptr;
7966 else
7967 sym_sec
7968 = bfd_section_from_elf_index (abfd, isym->st_shndx);
7969 symval = isym->st_value
7970 + sym_sec->output_section->vma
7971 + sym_sec->output_offset;
7974 /* Compute branch offset, from delay slot of the jump to the
7975 branch target. */
7976 sym_offset = (symval + irel->r_addend)
7977 - (sec_start + irel->r_offset + 4);
7979 /* Branch offset must be properly aligned. */
7980 if ((sym_offset & 3) != 0)
7981 continue;
7983 sym_offset >>= 2;
7985 /* Check that it's in range. */
7986 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
7987 continue;
7989 /* Get the section contents if we haven't done so already. */
7990 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7991 goto relax_return;
7993 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
7995 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
7996 if ((instruction & 0xfc1fffff) == 0x0000f809)
7997 instruction = 0x04110000;
7998 /* If it was jr <reg>, turn it into b <target>. */
7999 else if ((instruction & 0xfc1fffff) == 0x00000008)
8000 instruction = 0x10000000;
8001 else
8002 continue;
8004 instruction |= (sym_offset & 0xffff);
8005 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8006 changed_contents = TRUE;
8009 if (contents != NULL
8010 && elf_section_data (sec)->this_hdr.contents != contents)
8012 if (!changed_contents && !link_info->keep_memory)
8013 free (contents);
8014 else
8016 /* Cache the section contents for elf_link_input_bfd. */
8017 elf_section_data (sec)->this_hdr.contents = contents;
8020 return TRUE;
8022 relax_return:
8023 if (contents != NULL
8024 && elf_section_data (sec)->this_hdr.contents != contents)
8025 free (contents);
8026 return FALSE;
8029 /* Allocate space for global sym dynamic relocs. */
8031 static bfd_boolean
8032 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8034 struct bfd_link_info *info = inf;
8035 bfd *dynobj;
8036 struct mips_elf_link_hash_entry *hmips;
8037 struct mips_elf_link_hash_table *htab;
8039 htab = mips_elf_hash_table (info);
8040 dynobj = elf_hash_table (info)->dynobj;
8041 hmips = (struct mips_elf_link_hash_entry *) h;
8043 /* VxWorks executables are handled elsewhere; we only need to
8044 allocate relocations in shared objects. */
8045 if (htab->is_vxworks && !info->shared)
8046 return TRUE;
8048 /* Ignore indirect and warning symbols. All relocations against
8049 such symbols will be redirected to the target symbol. */
8050 if (h->root.type == bfd_link_hash_indirect
8051 || h->root.type == bfd_link_hash_warning)
8052 return TRUE;
8054 /* If this symbol is defined in a dynamic object, or we are creating
8055 a shared library, we will need to copy any R_MIPS_32 or
8056 R_MIPS_REL32 relocs against it into the output file. */
8057 if (! info->relocatable
8058 && hmips->possibly_dynamic_relocs != 0
8059 && (h->root.type == bfd_link_hash_defweak
8060 || !h->def_regular
8061 || info->shared))
8063 bfd_boolean do_copy = TRUE;
8065 if (h->root.type == bfd_link_hash_undefweak)
8067 /* Do not copy relocations for undefined weak symbols with
8068 non-default visibility. */
8069 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8070 do_copy = FALSE;
8072 /* Make sure undefined weak symbols are output as a dynamic
8073 symbol in PIEs. */
8074 else if (h->dynindx == -1 && !h->forced_local)
8076 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8077 return FALSE;
8081 if (do_copy)
8083 /* Even though we don't directly need a GOT entry for this symbol,
8084 a symbol must have a dynamic symbol table index greater that
8085 DT_MIPS_GOTSYM if there are dynamic relocations against it. */
8086 if (hmips->global_got_area > GGA_RELOC_ONLY)
8087 hmips->global_got_area = GGA_RELOC_ONLY;
8089 mips_elf_allocate_dynamic_relocations
8090 (dynobj, info, hmips->possibly_dynamic_relocs);
8091 if (hmips->readonly_reloc)
8092 /* We tell the dynamic linker that there are relocations
8093 against the text segment. */
8094 info->flags |= DF_TEXTREL;
8098 return TRUE;
8101 /* Adjust a symbol defined by a dynamic object and referenced by a
8102 regular object. The current definition is in some section of the
8103 dynamic object, but we're not including those sections. We have to
8104 change the definition to something the rest of the link can
8105 understand. */
8107 bfd_boolean
8108 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8109 struct elf_link_hash_entry *h)
8111 bfd *dynobj;
8112 struct mips_elf_link_hash_entry *hmips;
8113 struct mips_elf_link_hash_table *htab;
8115 htab = mips_elf_hash_table (info);
8116 dynobj = elf_hash_table (info)->dynobj;
8117 hmips = (struct mips_elf_link_hash_entry *) h;
8119 /* Make sure we know what is going on here. */
8120 BFD_ASSERT (dynobj != NULL
8121 && (h->needs_plt
8122 || h->u.weakdef != NULL
8123 || (h->def_dynamic
8124 && h->ref_regular
8125 && !h->def_regular)));
8127 hmips = (struct mips_elf_link_hash_entry *) h;
8129 /* If there are call relocations against an externally-defined symbol,
8130 see whether we can create a MIPS lazy-binding stub for it. We can
8131 only do this if all references to the function are through call
8132 relocations, and in that case, the traditional lazy-binding stubs
8133 are much more efficient than PLT entries.
8135 Traditional stubs are only available on SVR4 psABI-based systems;
8136 VxWorks always uses PLTs instead. */
8137 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8139 if (! elf_hash_table (info)->dynamic_sections_created)
8140 return TRUE;
8142 /* If this symbol is not defined in a regular file, then set
8143 the symbol to the stub location. This is required to make
8144 function pointers compare as equal between the normal
8145 executable and the shared library. */
8146 if (!h->def_regular)
8148 hmips->needs_lazy_stub = TRUE;
8149 htab->lazy_stub_count++;
8150 return TRUE;
8153 /* As above, VxWorks requires PLT entries for externally-defined
8154 functions that are only accessed through call relocations.
8156 Both VxWorks and non-VxWorks targets also need PLT entries if there
8157 are static-only relocations against an externally-defined function.
8158 This can technically occur for shared libraries if there are
8159 branches to the symbol, although it is unlikely that this will be
8160 used in practice due to the short ranges involved. It can occur
8161 for any relative or absolute relocation in executables; in that
8162 case, the PLT entry becomes the function's canonical address. */
8163 else if (((h->needs_plt && !hmips->no_fn_stub)
8164 || (h->type == STT_FUNC && hmips->has_static_relocs))
8165 && htab->use_plts_and_copy_relocs
8166 && !SYMBOL_CALLS_LOCAL (info, h)
8167 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8168 && h->root.type == bfd_link_hash_undefweak))
8170 /* If this is the first symbol to need a PLT entry, allocate room
8171 for the header. */
8172 if (htab->splt->size == 0)
8174 BFD_ASSERT (htab->sgotplt->size == 0);
8176 /* If we're using the PLT additions to the psABI, each PLT
8177 entry is 16 bytes and the PLT0 entry is 32 bytes.
8178 Encourage better cache usage by aligning. We do this
8179 lazily to avoid pessimizing traditional objects. */
8180 if (!htab->is_vxworks
8181 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8182 return FALSE;
8184 /* Make sure that .got.plt is word-aligned. We do this lazily
8185 for the same reason as above. */
8186 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8187 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8188 return FALSE;
8190 htab->splt->size += htab->plt_header_size;
8192 /* On non-VxWorks targets, the first two entries in .got.plt
8193 are reserved. */
8194 if (!htab->is_vxworks)
8195 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
8197 /* On VxWorks, also allocate room for the header's
8198 .rela.plt.unloaded entries. */
8199 if (htab->is_vxworks && !info->shared)
8200 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8203 /* Assign the next .plt entry to this symbol. */
8204 h->plt.offset = htab->splt->size;
8205 htab->splt->size += htab->plt_entry_size;
8207 /* If the output file has no definition of the symbol, set the
8208 symbol's value to the address of the stub. */
8209 if (!info->shared && !h->def_regular)
8211 h->root.u.def.section = htab->splt;
8212 h->root.u.def.value = h->plt.offset;
8213 /* For VxWorks, point at the PLT load stub rather than the
8214 lazy resolution stub; this stub will become the canonical
8215 function address. */
8216 if (htab->is_vxworks)
8217 h->root.u.def.value += 8;
8220 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8221 relocation. */
8222 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8223 htab->srelplt->size += (htab->is_vxworks
8224 ? MIPS_ELF_RELA_SIZE (dynobj)
8225 : MIPS_ELF_REL_SIZE (dynobj));
8227 /* Make room for the .rela.plt.unloaded relocations. */
8228 if (htab->is_vxworks && !info->shared)
8229 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8231 /* All relocations against this symbol that could have been made
8232 dynamic will now refer to the PLT entry instead. */
8233 hmips->possibly_dynamic_relocs = 0;
8235 return TRUE;
8238 /* If this is a weak symbol, and there is a real definition, the
8239 processor independent code will have arranged for us to see the
8240 real definition first, and we can just use the same value. */
8241 if (h->u.weakdef != NULL)
8243 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8244 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8245 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8246 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8247 return TRUE;
8250 /* Otherwise, there is nothing further to do for symbols defined
8251 in regular objects. */
8252 if (h->def_regular)
8253 return TRUE;
8255 /* There's also nothing more to do if we'll convert all relocations
8256 against this symbol into dynamic relocations. */
8257 if (!hmips->has_static_relocs)
8258 return TRUE;
8260 /* We're now relying on copy relocations. Complain if we have
8261 some that we can't convert. */
8262 if (!htab->use_plts_and_copy_relocs || info->shared)
8264 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8265 "dynamic symbol %s"),
8266 h->root.root.string);
8267 bfd_set_error (bfd_error_bad_value);
8268 return FALSE;
8271 /* We must allocate the symbol in our .dynbss section, which will
8272 become part of the .bss section of the executable. There will be
8273 an entry for this symbol in the .dynsym section. The dynamic
8274 object will contain position independent code, so all references
8275 from the dynamic object to this symbol will go through the global
8276 offset table. The dynamic linker will use the .dynsym entry to
8277 determine the address it must put in the global offset table, so
8278 both the dynamic object and the regular object will refer to the
8279 same memory location for the variable. */
8281 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8283 if (htab->is_vxworks)
8284 htab->srelbss->size += sizeof (Elf32_External_Rela);
8285 else
8286 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8287 h->needs_copy = 1;
8290 /* All relocations against this symbol that could have been made
8291 dynamic will now refer to the local copy instead. */
8292 hmips->possibly_dynamic_relocs = 0;
8294 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
8297 /* This function is called after all the input files have been read,
8298 and the input sections have been assigned to output sections. We
8299 check for any mips16 stub sections that we can discard. */
8301 bfd_boolean
8302 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
8303 struct bfd_link_info *info)
8305 asection *ri;
8306 struct mips_elf_link_hash_table *htab;
8307 struct mips_htab_traverse_info hti;
8309 htab = mips_elf_hash_table (info);
8311 /* The .reginfo section has a fixed size. */
8312 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8313 if (ri != NULL)
8314 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
8316 hti.info = info;
8317 hti.output_bfd = output_bfd;
8318 hti.error = FALSE;
8319 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8320 mips_elf_check_symbols, &hti);
8321 if (hti.error)
8322 return FALSE;
8324 return TRUE;
8327 /* If the link uses a GOT, lay it out and work out its size. */
8329 static bfd_boolean
8330 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8332 bfd *dynobj;
8333 asection *s;
8334 struct mips_got_info *g;
8335 bfd_size_type loadable_size = 0;
8336 bfd_size_type page_gotno;
8337 bfd *sub;
8338 struct mips_elf_count_tls_arg count_tls_arg;
8339 struct mips_elf_link_hash_table *htab;
8341 htab = mips_elf_hash_table (info);
8342 s = htab->sgot;
8343 if (s == NULL)
8344 return TRUE;
8346 dynobj = elf_hash_table (info)->dynobj;
8347 g = htab->got_info;
8349 /* Allocate room for the reserved entries. VxWorks always reserves
8350 3 entries; other objects only reserve 2 entries. */
8351 BFD_ASSERT (g->assigned_gotno == 0);
8352 if (htab->is_vxworks)
8353 htab->reserved_gotno = 3;
8354 else
8355 htab->reserved_gotno = 2;
8356 g->local_gotno += htab->reserved_gotno;
8357 g->assigned_gotno = htab->reserved_gotno;
8359 /* Replace entries for indirect and warning symbols with entries for
8360 the target symbol. */
8361 if (!mips_elf_resolve_final_got_entries (g))
8362 return FALSE;
8364 /* Count the number of GOT symbols. */
8365 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, g);
8367 /* Calculate the total loadable size of the output. That
8368 will give us the maximum number of GOT_PAGE entries
8369 required. */
8370 for (sub = info->input_bfds; sub; sub = sub->link_next)
8372 asection *subsection;
8374 for (subsection = sub->sections;
8375 subsection;
8376 subsection = subsection->next)
8378 if ((subsection->flags & SEC_ALLOC) == 0)
8379 continue;
8380 loadable_size += ((subsection->size + 0xf)
8381 &~ (bfd_size_type) 0xf);
8385 if (htab->is_vxworks)
8386 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
8387 relocations against local symbols evaluate to "G", and the EABI does
8388 not include R_MIPS_GOT_PAGE. */
8389 page_gotno = 0;
8390 else
8391 /* Assume there are two loadable segments consisting of contiguous
8392 sections. Is 5 enough? */
8393 page_gotno = (loadable_size >> 16) + 5;
8395 /* Choose the smaller of the two estimates; both are intended to be
8396 conservative. */
8397 if (page_gotno > g->page_gotno)
8398 page_gotno = g->page_gotno;
8400 g->local_gotno += page_gotno;
8401 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8402 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8404 /* We need to calculate tls_gotno for global symbols at this point
8405 instead of building it up earlier, to avoid doublecounting
8406 entries for one global symbol from multiple input files. */
8407 count_tls_arg.info = info;
8408 count_tls_arg.needed = 0;
8409 elf_link_hash_traverse (elf_hash_table (info),
8410 mips_elf_count_global_tls_entries,
8411 &count_tls_arg);
8412 g->tls_gotno += count_tls_arg.needed;
8413 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8415 /* VxWorks does not support multiple GOTs. It initializes $gp to
8416 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8417 dynamic loader. */
8418 if (htab->is_vxworks)
8420 /* VxWorks executables do not need a GOT. */
8421 if (info->shared)
8423 /* Each VxWorks GOT entry needs an explicit relocation. */
8424 unsigned int count;
8426 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
8427 if (count)
8428 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8431 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
8433 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
8434 return FALSE;
8436 else
8438 struct mips_elf_count_tls_arg arg;
8440 /* Set up TLS entries. */
8441 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8442 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
8444 /* Allocate room for the TLS relocations. */
8445 arg.info = info;
8446 arg.needed = 0;
8447 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8448 elf_link_hash_traverse (elf_hash_table (info),
8449 mips_elf_count_global_tls_relocs,
8450 &arg);
8451 if (arg.needed)
8452 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
8455 return TRUE;
8458 /* Estimate the size of the .MIPS.stubs section. */
8460 static void
8461 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8463 struct mips_elf_link_hash_table *htab;
8464 bfd_size_type dynsymcount;
8466 htab = mips_elf_hash_table (info);
8467 if (htab->lazy_stub_count == 0)
8468 return;
8470 /* IRIX rld assumes that a function stub isn't at the end of the .text
8471 section, so add a dummy entry to the end. */
8472 htab->lazy_stub_count++;
8474 /* Get a worst-case estimate of the number of dynamic symbols needed.
8475 At this point, dynsymcount does not account for section symbols
8476 and count_section_dynsyms may overestimate the number that will
8477 be needed. */
8478 dynsymcount = (elf_hash_table (info)->dynsymcount
8479 + count_section_dynsyms (output_bfd, info));
8481 /* Determine the size of one stub entry. */
8482 htab->function_stub_size = (dynsymcount > 0x10000
8483 ? MIPS_FUNCTION_STUB_BIG_SIZE
8484 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8486 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8489 /* A mips_elf_link_hash_traverse callback for which DATA points to the
8490 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8491 allocate an entry in the stubs section. */
8493 static bfd_boolean
8494 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8496 struct mips_elf_link_hash_table *htab;
8498 htab = (struct mips_elf_link_hash_table *) data;
8499 if (h->needs_lazy_stub)
8501 h->root.root.u.def.section = htab->sstubs;
8502 h->root.root.u.def.value = htab->sstubs->size;
8503 h->root.plt.offset = htab->sstubs->size;
8504 htab->sstubs->size += htab->function_stub_size;
8506 return TRUE;
8509 /* Allocate offsets in the stubs section to each symbol that needs one.
8510 Set the final size of the .MIPS.stub section. */
8512 static void
8513 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8515 struct mips_elf_link_hash_table *htab;
8517 htab = mips_elf_hash_table (info);
8518 if (htab->lazy_stub_count == 0)
8519 return;
8521 htab->sstubs->size = 0;
8522 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8523 mips_elf_allocate_lazy_stub, htab);
8524 htab->sstubs->size += htab->function_stub_size;
8525 BFD_ASSERT (htab->sstubs->size
8526 == htab->lazy_stub_count * htab->function_stub_size);
8529 /* Set the sizes of the dynamic sections. */
8531 bfd_boolean
8532 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8533 struct bfd_link_info *info)
8535 bfd *dynobj;
8536 asection *s, *sreldyn;
8537 bfd_boolean reltext;
8538 struct mips_elf_link_hash_table *htab;
8540 htab = mips_elf_hash_table (info);
8541 dynobj = elf_hash_table (info)->dynobj;
8542 BFD_ASSERT (dynobj != NULL);
8544 if (elf_hash_table (info)->dynamic_sections_created)
8546 /* Set the contents of the .interp section to the interpreter. */
8547 if (info->executable)
8549 s = bfd_get_section_by_name (dynobj, ".interp");
8550 BFD_ASSERT (s != NULL);
8551 s->size
8552 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8553 s->contents
8554 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8557 /* Create a symbol for the PLT, if we know that we are using it. */
8558 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8560 struct elf_link_hash_entry *h;
8562 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8564 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8565 "_PROCEDURE_LINKAGE_TABLE_");
8566 htab->root.hplt = h;
8567 if (h == NULL)
8568 return FALSE;
8569 h->type = STT_FUNC;
8573 /* Allocate space for global sym dynamic relocs. */
8574 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8576 mips_elf_estimate_stub_size (output_bfd, info);
8578 if (!mips_elf_lay_out_got (output_bfd, info))
8579 return FALSE;
8581 mips_elf_lay_out_lazy_stubs (info);
8583 /* The check_relocs and adjust_dynamic_symbol entry points have
8584 determined the sizes of the various dynamic sections. Allocate
8585 memory for them. */
8586 reltext = FALSE;
8587 for (s = dynobj->sections; s != NULL; s = s->next)
8589 const char *name;
8591 /* It's OK to base decisions on the section name, because none
8592 of the dynobj section names depend upon the input files. */
8593 name = bfd_get_section_name (dynobj, s);
8595 if ((s->flags & SEC_LINKER_CREATED) == 0)
8596 continue;
8598 if (CONST_STRNEQ (name, ".rel"))
8600 if (s->size != 0)
8602 const char *outname;
8603 asection *target;
8605 /* If this relocation section applies to a read only
8606 section, then we probably need a DT_TEXTREL entry.
8607 If the relocation section is .rel(a).dyn, we always
8608 assert a DT_TEXTREL entry rather than testing whether
8609 there exists a relocation to a read only section or
8610 not. */
8611 outname = bfd_get_section_name (output_bfd,
8612 s->output_section);
8613 target = bfd_get_section_by_name (output_bfd, outname + 4);
8614 if ((target != NULL
8615 && (target->flags & SEC_READONLY) != 0
8616 && (target->flags & SEC_ALLOC) != 0)
8617 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8618 reltext = TRUE;
8620 /* We use the reloc_count field as a counter if we need
8621 to copy relocs into the output file. */
8622 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8623 s->reloc_count = 0;
8625 /* If combreloc is enabled, elf_link_sort_relocs() will
8626 sort relocations, but in a different way than we do,
8627 and before we're done creating relocations. Also, it
8628 will move them around between input sections'
8629 relocation's contents, so our sorting would be
8630 broken, so don't let it run. */
8631 info->combreloc = 0;
8634 else if (! info->shared
8635 && ! mips_elf_hash_table (info)->use_rld_obj_head
8636 && CONST_STRNEQ (name, ".rld_map"))
8638 /* We add a room for __rld_map. It will be filled in by the
8639 rtld to contain a pointer to the _r_debug structure. */
8640 s->size += 4;
8642 else if (SGI_COMPAT (output_bfd)
8643 && CONST_STRNEQ (name, ".compact_rel"))
8644 s->size += mips_elf_hash_table (info)->compact_rel_size;
8645 else if (s == htab->splt)
8647 /* If the last PLT entry has a branch delay slot, allocate
8648 room for an extra nop to fill the delay slot. This is
8649 for CPUs without load interlocking. */
8650 if (! LOAD_INTERLOCKS_P (output_bfd)
8651 && ! htab->is_vxworks && s->size > 0)
8652 s->size += 4;
8654 else if (! CONST_STRNEQ (name, ".init")
8655 && s != htab->sgot
8656 && s != htab->sgotplt
8657 && s != htab->sstubs
8658 && s != htab->sdynbss)
8660 /* It's not one of our sections, so don't allocate space. */
8661 continue;
8664 if (s->size == 0)
8666 s->flags |= SEC_EXCLUDE;
8667 continue;
8670 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8671 continue;
8673 /* Allocate memory for the section contents. */
8674 s->contents = bfd_zalloc (dynobj, s->size);
8675 if (s->contents == NULL)
8677 bfd_set_error (bfd_error_no_memory);
8678 return FALSE;
8682 if (elf_hash_table (info)->dynamic_sections_created)
8684 /* Add some entries to the .dynamic section. We fill in the
8685 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8686 must add the entries now so that we get the correct size for
8687 the .dynamic section. */
8689 /* SGI object has the equivalence of DT_DEBUG in the
8690 DT_MIPS_RLD_MAP entry. This must come first because glibc
8691 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8692 looks at the first one it sees. */
8693 if (!info->shared
8694 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8695 return FALSE;
8697 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8698 used by the debugger. */
8699 if (info->executable
8700 && !SGI_COMPAT (output_bfd)
8701 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8702 return FALSE;
8704 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8705 info->flags |= DF_TEXTREL;
8707 if ((info->flags & DF_TEXTREL) != 0)
8709 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8710 return FALSE;
8712 /* Clear the DF_TEXTREL flag. It will be set again if we
8713 write out an actual text relocation; we may not, because
8714 at this point we do not know whether e.g. any .eh_frame
8715 absolute relocations have been converted to PC-relative. */
8716 info->flags &= ~DF_TEXTREL;
8719 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8720 return FALSE;
8722 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
8723 if (htab->is_vxworks)
8725 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8726 use any of the DT_MIPS_* tags. */
8727 if (sreldyn && sreldyn->size > 0)
8729 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8730 return FALSE;
8732 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8733 return FALSE;
8735 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8736 return FALSE;
8739 else
8741 if (sreldyn && sreldyn->size > 0)
8743 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8744 return FALSE;
8746 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8747 return FALSE;
8749 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8750 return FALSE;
8753 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8754 return FALSE;
8756 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8757 return FALSE;
8759 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8760 return FALSE;
8762 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8763 return FALSE;
8765 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8766 return FALSE;
8768 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8769 return FALSE;
8771 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8772 return FALSE;
8774 if (IRIX_COMPAT (dynobj) == ict_irix5
8775 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8776 return FALSE;
8778 if (IRIX_COMPAT (dynobj) == ict_irix6
8779 && (bfd_get_section_by_name
8780 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8781 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8782 return FALSE;
8784 if (htab->splt->size > 0)
8786 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8787 return FALSE;
8789 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8790 return FALSE;
8792 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8793 return FALSE;
8795 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8796 return FALSE;
8798 if (htab->is_vxworks
8799 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8800 return FALSE;
8803 return TRUE;
8806 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8807 Adjust its R_ADDEND field so that it is correct for the output file.
8808 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8809 and sections respectively; both use symbol indexes. */
8811 static void
8812 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8813 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8814 asection **local_sections, Elf_Internal_Rela *rel)
8816 unsigned int r_type, r_symndx;
8817 Elf_Internal_Sym *sym;
8818 asection *sec;
8820 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8822 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8823 if (r_type == R_MIPS16_GPREL
8824 || r_type == R_MIPS_GPREL16
8825 || r_type == R_MIPS_GPREL32
8826 || r_type == R_MIPS_LITERAL)
8828 rel->r_addend += _bfd_get_gp_value (input_bfd);
8829 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8832 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8833 sym = local_syms + r_symndx;
8835 /* Adjust REL's addend to account for section merging. */
8836 if (!info->relocatable)
8838 sec = local_sections[r_symndx];
8839 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8842 /* This would normally be done by the rela_normal code in elflink.c. */
8843 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8844 rel->r_addend += local_sections[r_symndx]->output_offset;
8848 /* Relocate a MIPS ELF section. */
8850 bfd_boolean
8851 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8852 bfd *input_bfd, asection *input_section,
8853 bfd_byte *contents, Elf_Internal_Rela *relocs,
8854 Elf_Internal_Sym *local_syms,
8855 asection **local_sections)
8857 Elf_Internal_Rela *rel;
8858 const Elf_Internal_Rela *relend;
8859 bfd_vma addend = 0;
8860 bfd_boolean use_saved_addend_p = FALSE;
8861 const struct elf_backend_data *bed;
8863 bed = get_elf_backend_data (output_bfd);
8864 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8865 for (rel = relocs; rel < relend; ++rel)
8867 const char *name;
8868 bfd_vma value = 0;
8869 reloc_howto_type *howto;
8870 bfd_boolean require_jalx;
8871 /* TRUE if the relocation is a RELA relocation, rather than a
8872 REL relocation. */
8873 bfd_boolean rela_relocation_p = TRUE;
8874 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8875 const char *msg;
8876 unsigned long r_symndx;
8877 asection *sec;
8878 Elf_Internal_Shdr *symtab_hdr;
8879 struct elf_link_hash_entry *h;
8881 /* Find the relocation howto for this relocation. */
8882 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8883 NEWABI_P (input_bfd)
8884 && (MIPS_RELOC_RELA_P
8885 (input_bfd, input_section,
8886 rel - relocs)));
8888 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
8889 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8890 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8892 sec = local_sections[r_symndx];
8893 h = NULL;
8895 else
8897 unsigned long extsymoff;
8899 extsymoff = 0;
8900 if (!elf_bad_symtab (input_bfd))
8901 extsymoff = symtab_hdr->sh_info;
8902 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8903 while (h->root.type == bfd_link_hash_indirect
8904 || h->root.type == bfd_link_hash_warning)
8905 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8907 sec = NULL;
8908 if (h->root.type == bfd_link_hash_defined
8909 || h->root.type == bfd_link_hash_defweak)
8910 sec = h->root.u.def.section;
8913 if (sec != NULL && elf_discarded_section (sec))
8915 /* For relocs against symbols from removed linkonce sections,
8916 or sections discarded by a linker script, we just want the
8917 section contents zeroed. Avoid any special processing. */
8918 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8919 rel->r_info = 0;
8920 rel->r_addend = 0;
8921 continue;
8924 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
8926 /* Some 32-bit code uses R_MIPS_64. In particular, people use
8927 64-bit code, but make sure all their addresses are in the
8928 lowermost or uppermost 32-bit section of the 64-bit address
8929 space. Thus, when they use an R_MIPS_64 they mean what is
8930 usually meant by R_MIPS_32, with the exception that the
8931 stored value is sign-extended to 64 bits. */
8932 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
8934 /* On big-endian systems, we need to lie about the position
8935 of the reloc. */
8936 if (bfd_big_endian (input_bfd))
8937 rel->r_offset += 4;
8940 if (!use_saved_addend_p)
8942 /* If these relocations were originally of the REL variety,
8943 we must pull the addend out of the field that will be
8944 relocated. Otherwise, we simply use the contents of the
8945 RELA relocation. */
8946 if (mips_elf_rel_relocation_p (input_bfd, input_section,
8947 relocs, rel))
8949 rela_relocation_p = FALSE;
8950 addend = mips_elf_read_rel_addend (input_bfd, rel,
8951 howto, contents);
8952 if (hi16_reloc_p (r_type)
8953 || (got16_reloc_p (r_type)
8954 && mips_elf_local_relocation_p (input_bfd, rel,
8955 local_sections, FALSE)))
8957 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8958 contents, &addend))
8960 const char *name;
8962 if (h)
8963 name = h->root.root.string;
8964 else
8965 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
8966 local_syms + r_symndx,
8967 sec);
8968 (*_bfd_error_handler)
8969 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
8970 input_bfd, input_section, name, howto->name,
8971 rel->r_offset);
8974 else
8975 addend <<= howto->rightshift;
8977 else
8978 addend = rel->r_addend;
8979 mips_elf_adjust_addend (output_bfd, info, input_bfd,
8980 local_syms, local_sections, rel);
8983 if (info->relocatable)
8985 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
8986 && bfd_big_endian (input_bfd))
8987 rel->r_offset -= 4;
8989 if (!rela_relocation_p && rel->r_addend)
8991 addend += rel->r_addend;
8992 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
8993 addend = mips_elf_high (addend);
8994 else if (r_type == R_MIPS_HIGHER)
8995 addend = mips_elf_higher (addend);
8996 else if (r_type == R_MIPS_HIGHEST)
8997 addend = mips_elf_highest (addend);
8998 else
8999 addend >>= howto->rightshift;
9001 /* We use the source mask, rather than the destination
9002 mask because the place to which we are writing will be
9003 source of the addend in the final link. */
9004 addend &= howto->src_mask;
9006 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9007 /* See the comment above about using R_MIPS_64 in the 32-bit
9008 ABI. Here, we need to update the addend. It would be
9009 possible to get away with just using the R_MIPS_32 reloc
9010 but for endianness. */
9012 bfd_vma sign_bits;
9013 bfd_vma low_bits;
9014 bfd_vma high_bits;
9016 if (addend & ((bfd_vma) 1 << 31))
9017 #ifdef BFD64
9018 sign_bits = ((bfd_vma) 1 << 32) - 1;
9019 #else
9020 sign_bits = -1;
9021 #endif
9022 else
9023 sign_bits = 0;
9025 /* If we don't know that we have a 64-bit type,
9026 do two separate stores. */
9027 if (bfd_big_endian (input_bfd))
9029 /* Store the sign-bits (which are most significant)
9030 first. */
9031 low_bits = sign_bits;
9032 high_bits = addend;
9034 else
9036 low_bits = addend;
9037 high_bits = sign_bits;
9039 bfd_put_32 (input_bfd, low_bits,
9040 contents + rel->r_offset);
9041 bfd_put_32 (input_bfd, high_bits,
9042 contents + rel->r_offset + 4);
9043 continue;
9046 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9047 input_bfd, input_section,
9048 contents, FALSE))
9049 return FALSE;
9052 /* Go on to the next relocation. */
9053 continue;
9056 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9057 relocations for the same offset. In that case we are
9058 supposed to treat the output of each relocation as the addend
9059 for the next. */
9060 if (rel + 1 < relend
9061 && rel->r_offset == rel[1].r_offset
9062 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9063 use_saved_addend_p = TRUE;
9064 else
9065 use_saved_addend_p = FALSE;
9067 /* Figure out what value we are supposed to relocate. */
9068 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9069 input_section, info, rel,
9070 addend, howto, local_syms,
9071 local_sections, &value,
9072 &name, &require_jalx,
9073 use_saved_addend_p))
9075 case bfd_reloc_continue:
9076 /* There's nothing to do. */
9077 continue;
9079 case bfd_reloc_undefined:
9080 /* mips_elf_calculate_relocation already called the
9081 undefined_symbol callback. There's no real point in
9082 trying to perform the relocation at this point, so we
9083 just skip ahead to the next relocation. */
9084 continue;
9086 case bfd_reloc_notsupported:
9087 msg = _("internal error: unsupported relocation error");
9088 info->callbacks->warning
9089 (info, msg, name, input_bfd, input_section, rel->r_offset);
9090 return FALSE;
9092 case bfd_reloc_overflow:
9093 if (use_saved_addend_p)
9094 /* Ignore overflow until we reach the last relocation for
9095 a given location. */
9097 else
9099 struct mips_elf_link_hash_table *htab;
9101 htab = mips_elf_hash_table (info);
9102 BFD_ASSERT (name != NULL);
9103 if (!htab->small_data_overflow_reported
9104 && (howto->type == R_MIPS_GPREL16
9105 || howto->type == R_MIPS_LITERAL))
9107 const char *msg =
9108 _("small-data section exceeds 64KB;"
9109 " lower small-data size limit (see option -G)");
9111 htab->small_data_overflow_reported = TRUE;
9112 (*info->callbacks->einfo) ("%P: %s\n", msg);
9114 if (! ((*info->callbacks->reloc_overflow)
9115 (info, NULL, name, howto->name, (bfd_vma) 0,
9116 input_bfd, input_section, rel->r_offset)))
9117 return FALSE;
9119 break;
9121 case bfd_reloc_ok:
9122 break;
9124 default:
9125 abort ();
9126 break;
9129 /* If we've got another relocation for the address, keep going
9130 until we reach the last one. */
9131 if (use_saved_addend_p)
9133 addend = value;
9134 continue;
9137 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9138 /* See the comment above about using R_MIPS_64 in the 32-bit
9139 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9140 that calculated the right value. Now, however, we
9141 sign-extend the 32-bit result to 64-bits, and store it as a
9142 64-bit value. We are especially generous here in that we
9143 go to extreme lengths to support this usage on systems with
9144 only a 32-bit VMA. */
9146 bfd_vma sign_bits;
9147 bfd_vma low_bits;
9148 bfd_vma high_bits;
9150 if (value & ((bfd_vma) 1 << 31))
9151 #ifdef BFD64
9152 sign_bits = ((bfd_vma) 1 << 32) - 1;
9153 #else
9154 sign_bits = -1;
9155 #endif
9156 else
9157 sign_bits = 0;
9159 /* If we don't know that we have a 64-bit type,
9160 do two separate stores. */
9161 if (bfd_big_endian (input_bfd))
9163 /* Undo what we did above. */
9164 rel->r_offset -= 4;
9165 /* Store the sign-bits (which are most significant)
9166 first. */
9167 low_bits = sign_bits;
9168 high_bits = value;
9170 else
9172 low_bits = value;
9173 high_bits = sign_bits;
9175 bfd_put_32 (input_bfd, low_bits,
9176 contents + rel->r_offset);
9177 bfd_put_32 (input_bfd, high_bits,
9178 contents + rel->r_offset + 4);
9179 continue;
9182 /* Actually perform the relocation. */
9183 if (! mips_elf_perform_relocation (info, howto, rel, value,
9184 input_bfd, input_section,
9185 contents, require_jalx))
9186 return FALSE;
9189 return TRUE;
9192 /* A function that iterates over each entry in la25_stubs and fills
9193 in the code for each one. DATA points to a mips_htab_traverse_info. */
9195 static int
9196 mips_elf_create_la25_stub (void **slot, void *data)
9198 struct mips_htab_traverse_info *hti;
9199 struct mips_elf_link_hash_table *htab;
9200 struct mips_elf_la25_stub *stub;
9201 asection *s;
9202 bfd_byte *loc;
9203 bfd_vma offset, target, target_high, target_low;
9205 stub = (struct mips_elf_la25_stub *) *slot;
9206 hti = (struct mips_htab_traverse_info *) data;
9207 htab = mips_elf_hash_table (hti->info);
9209 /* Create the section contents, if we haven't already. */
9210 s = stub->stub_section;
9211 loc = s->contents;
9212 if (loc == NULL)
9214 loc = bfd_malloc (s->size);
9215 if (loc == NULL)
9217 hti->error = TRUE;
9218 return FALSE;
9220 s->contents = loc;
9223 /* Work out where in the section this stub should go. */
9224 offset = stub->offset;
9226 /* Work out the target address. */
9227 target = (stub->h->root.root.u.def.section->output_section->vma
9228 + stub->h->root.root.u.def.section->output_offset
9229 + stub->h->root.root.u.def.value);
9230 target_high = ((target + 0x8000) >> 16) & 0xffff;
9231 target_low = (target & 0xffff);
9233 if (stub->stub_section != htab->strampoline)
9235 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9236 of the section and write the two instructions at the end. */
9237 memset (loc, 0, offset);
9238 loc += offset;
9239 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9240 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9242 else
9244 /* This is trampoline. */
9245 loc += offset;
9246 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9247 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9248 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9249 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9251 return TRUE;
9254 /* If NAME is one of the special IRIX6 symbols defined by the linker,
9255 adjust it appropriately now. */
9257 static void
9258 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9259 const char *name, Elf_Internal_Sym *sym)
9261 /* The linker script takes care of providing names and values for
9262 these, but we must place them into the right sections. */
9263 static const char* const text_section_symbols[] = {
9264 "_ftext",
9265 "_etext",
9266 "__dso_displacement",
9267 "__elf_header",
9268 "__program_header_table",
9269 NULL
9272 static const char* const data_section_symbols[] = {
9273 "_fdata",
9274 "_edata",
9275 "_end",
9276 "_fbss",
9277 NULL
9280 const char* const *p;
9281 int i;
9283 for (i = 0; i < 2; ++i)
9284 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9286 ++p)
9287 if (strcmp (*p, name) == 0)
9289 /* All of these symbols are given type STT_SECTION by the
9290 IRIX6 linker. */
9291 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9292 sym->st_other = STO_PROTECTED;
9294 /* The IRIX linker puts these symbols in special sections. */
9295 if (i == 0)
9296 sym->st_shndx = SHN_MIPS_TEXT;
9297 else
9298 sym->st_shndx = SHN_MIPS_DATA;
9300 break;
9304 /* Finish up dynamic symbol handling. We set the contents of various
9305 dynamic sections here. */
9307 bfd_boolean
9308 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9309 struct bfd_link_info *info,
9310 struct elf_link_hash_entry *h,
9311 Elf_Internal_Sym *sym)
9313 bfd *dynobj;
9314 asection *sgot;
9315 struct mips_got_info *g, *gg;
9316 const char *name;
9317 int idx;
9318 struct mips_elf_link_hash_table *htab;
9319 struct mips_elf_link_hash_entry *hmips;
9321 htab = mips_elf_hash_table (info);
9322 dynobj = elf_hash_table (info)->dynobj;
9323 hmips = (struct mips_elf_link_hash_entry *) h;
9325 BFD_ASSERT (!htab->is_vxworks);
9327 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9329 /* We've decided to create a PLT entry for this symbol. */
9330 bfd_byte *loc;
9331 bfd_vma header_address, plt_index, got_address;
9332 bfd_vma got_address_high, got_address_low, load;
9333 const bfd_vma *plt_entry;
9335 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9336 BFD_ASSERT (h->dynindx != -1);
9337 BFD_ASSERT (htab->splt != NULL);
9338 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9339 BFD_ASSERT (!h->def_regular);
9341 /* Calculate the address of the PLT header. */
9342 header_address = (htab->splt->output_section->vma
9343 + htab->splt->output_offset);
9345 /* Calculate the index of the entry. */
9346 plt_index = ((h->plt.offset - htab->plt_header_size)
9347 / htab->plt_entry_size);
9349 /* Calculate the address of the .got.plt entry. */
9350 got_address = (htab->sgotplt->output_section->vma
9351 + htab->sgotplt->output_offset
9352 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9353 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9354 got_address_low = got_address & 0xffff;
9356 /* Initially point the .got.plt entry at the PLT header. */
9357 loc = (htab->sgotplt->contents
9358 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9359 if (ABI_64_P (output_bfd))
9360 bfd_put_64 (output_bfd, header_address, loc);
9361 else
9362 bfd_put_32 (output_bfd, header_address, loc);
9364 /* Find out where the .plt entry should go. */
9365 loc = htab->splt->contents + h->plt.offset;
9367 /* Pick the load opcode. */
9368 load = MIPS_ELF_LOAD_WORD (output_bfd);
9370 /* Fill in the PLT entry itself. */
9371 plt_entry = mips_exec_plt_entry;
9372 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9373 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
9375 if (! LOAD_INTERLOCKS_P (output_bfd))
9377 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9378 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9380 else
9382 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9383 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9386 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9387 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9388 plt_index, h->dynindx,
9389 R_MIPS_JUMP_SLOT, got_address);
9391 /* We distinguish between PLT entries and lazy-binding stubs by
9392 giving the former an st_other value of STO_MIPS_PLT. Set the
9393 flag and leave the value if there are any relocations in the
9394 binary where pointer equality matters. */
9395 sym->st_shndx = SHN_UNDEF;
9396 if (h->pointer_equality_needed)
9397 sym->st_other = STO_MIPS_PLT;
9398 else
9399 sym->st_value = 0;
9401 else if (h->plt.offset != MINUS_ONE)
9403 /* We've decided to create a lazy-binding stub. */
9404 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
9406 /* This symbol has a stub. Set it up. */
9408 BFD_ASSERT (h->dynindx != -1);
9410 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9411 || (h->dynindx <= 0xffff));
9413 /* Values up to 2^31 - 1 are allowed. Larger values would cause
9414 sign extension at runtime in the stub, resulting in a negative
9415 index value. */
9416 if (h->dynindx & ~0x7fffffff)
9417 return FALSE;
9419 /* Fill the stub. */
9420 idx = 0;
9421 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9422 idx += 4;
9423 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9424 idx += 4;
9425 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9427 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
9428 stub + idx);
9429 idx += 4;
9431 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9432 idx += 4;
9434 /* If a large stub is not required and sign extension is not a
9435 problem, then use legacy code in the stub. */
9436 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9437 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9438 else if (h->dynindx & ~0x7fff)
9439 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9440 else
9441 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9442 stub + idx);
9444 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9445 memcpy (htab->sstubs->contents + h->plt.offset,
9446 stub, htab->function_stub_size);
9448 /* Mark the symbol as undefined. plt.offset != -1 occurs
9449 only for the referenced symbol. */
9450 sym->st_shndx = SHN_UNDEF;
9452 /* The run-time linker uses the st_value field of the symbol
9453 to reset the global offset table entry for this external
9454 to its stub address when unlinking a shared object. */
9455 sym->st_value = (htab->sstubs->output_section->vma
9456 + htab->sstubs->output_offset
9457 + h->plt.offset);
9460 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9461 refer to the stub, since only the stub uses the standard calling
9462 conventions. */
9463 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9465 BFD_ASSERT (hmips->need_fn_stub);
9466 sym->st_value = (hmips->fn_stub->output_section->vma
9467 + hmips->fn_stub->output_offset);
9468 sym->st_size = hmips->fn_stub->size;
9469 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9472 BFD_ASSERT (h->dynindx != -1
9473 || h->forced_local);
9475 sgot = htab->sgot;
9476 g = htab->got_info;
9477 BFD_ASSERT (g != NULL);
9479 /* Run through the global symbol table, creating GOT entries for all
9480 the symbols that need them. */
9481 if (g->global_gotsym != NULL
9482 && h->dynindx >= g->global_gotsym->dynindx)
9484 bfd_vma offset;
9485 bfd_vma value;
9487 value = sym->st_value;
9488 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9489 R_MIPS_GOT16, info);
9490 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9493 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
9495 struct mips_got_entry e, *p;
9496 bfd_vma entry;
9497 bfd_vma offset;
9499 gg = g;
9501 e.abfd = output_bfd;
9502 e.symndx = -1;
9503 e.d.h = hmips;
9504 e.tls_type = 0;
9506 for (g = g->next; g->next != gg; g = g->next)
9508 if (g->got_entries
9509 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9510 &e)))
9512 offset = p->gotidx;
9513 if (info->shared
9514 || (elf_hash_table (info)->dynamic_sections_created
9515 && p->d.h != NULL
9516 && p->d.h->root.def_dynamic
9517 && !p->d.h->root.def_regular))
9519 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9520 the various compatibility problems, it's easier to mock
9521 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9522 mips_elf_create_dynamic_relocation to calculate the
9523 appropriate addend. */
9524 Elf_Internal_Rela rel[3];
9526 memset (rel, 0, sizeof (rel));
9527 if (ABI_64_P (output_bfd))
9528 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9529 else
9530 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9531 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9533 entry = 0;
9534 if (! (mips_elf_create_dynamic_relocation
9535 (output_bfd, info, rel,
9536 e.d.h, NULL, sym->st_value, &entry, sgot)))
9537 return FALSE;
9539 else
9540 entry = sym->st_value;
9541 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
9546 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9547 name = h->root.root.string;
9548 if (strcmp (name, "_DYNAMIC") == 0
9549 || h == elf_hash_table (info)->hgot)
9550 sym->st_shndx = SHN_ABS;
9551 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9552 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9554 sym->st_shndx = SHN_ABS;
9555 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9556 sym->st_value = 1;
9558 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
9560 sym->st_shndx = SHN_ABS;
9561 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9562 sym->st_value = elf_gp (output_bfd);
9564 else if (SGI_COMPAT (output_bfd))
9566 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9567 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9569 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9570 sym->st_other = STO_PROTECTED;
9571 sym->st_value = 0;
9572 sym->st_shndx = SHN_MIPS_DATA;
9574 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9576 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9577 sym->st_other = STO_PROTECTED;
9578 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9579 sym->st_shndx = SHN_ABS;
9581 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9583 if (h->type == STT_FUNC)
9584 sym->st_shndx = SHN_MIPS_TEXT;
9585 else if (h->type == STT_OBJECT)
9586 sym->st_shndx = SHN_MIPS_DATA;
9590 /* Emit a copy reloc, if needed. */
9591 if (h->needs_copy)
9593 asection *s;
9594 bfd_vma symval;
9596 BFD_ASSERT (h->dynindx != -1);
9597 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9599 s = mips_elf_rel_dyn_section (info, FALSE);
9600 symval = (h->root.u.def.section->output_section->vma
9601 + h->root.u.def.section->output_offset
9602 + h->root.u.def.value);
9603 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9604 h->dynindx, R_MIPS_COPY, symval);
9607 /* Handle the IRIX6-specific symbols. */
9608 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9609 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9611 if (! info->shared)
9613 if (! mips_elf_hash_table (info)->use_rld_obj_head
9614 && (strcmp (name, "__rld_map") == 0
9615 || strcmp (name, "__RLD_MAP") == 0))
9617 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9618 BFD_ASSERT (s != NULL);
9619 sym->st_value = s->output_section->vma + s->output_offset;
9620 bfd_put_32 (output_bfd, 0, s->contents);
9621 if (mips_elf_hash_table (info)->rld_value == 0)
9622 mips_elf_hash_table (info)->rld_value = sym->st_value;
9624 else if (mips_elf_hash_table (info)->use_rld_obj_head
9625 && strcmp (name, "__rld_obj_head") == 0)
9627 /* IRIX6 does not use a .rld_map section. */
9628 if (IRIX_COMPAT (output_bfd) == ict_irix5
9629 || IRIX_COMPAT (output_bfd) == ict_none)
9630 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9631 != NULL);
9632 mips_elf_hash_table (info)->rld_value = sym->st_value;
9636 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9637 treat MIPS16 symbols like any other. */
9638 if (ELF_ST_IS_MIPS16 (sym->st_other))
9640 BFD_ASSERT (sym->st_value & 1);
9641 sym->st_other -= STO_MIPS16;
9644 return TRUE;
9647 /* Likewise, for VxWorks. */
9649 bfd_boolean
9650 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9651 struct bfd_link_info *info,
9652 struct elf_link_hash_entry *h,
9653 Elf_Internal_Sym *sym)
9655 bfd *dynobj;
9656 asection *sgot;
9657 struct mips_got_info *g;
9658 struct mips_elf_link_hash_table *htab;
9660 htab = mips_elf_hash_table (info);
9661 dynobj = elf_hash_table (info)->dynobj;
9663 if (h->plt.offset != (bfd_vma) -1)
9665 bfd_byte *loc;
9666 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9667 Elf_Internal_Rela rel;
9668 static const bfd_vma *plt_entry;
9670 BFD_ASSERT (h->dynindx != -1);
9671 BFD_ASSERT (htab->splt != NULL);
9672 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9674 /* Calculate the address of the .plt entry. */
9675 plt_address = (htab->splt->output_section->vma
9676 + htab->splt->output_offset
9677 + h->plt.offset);
9679 /* Calculate the index of the entry. */
9680 plt_index = ((h->plt.offset - htab->plt_header_size)
9681 / htab->plt_entry_size);
9683 /* Calculate the address of the .got.plt entry. */
9684 got_address = (htab->sgotplt->output_section->vma
9685 + htab->sgotplt->output_offset
9686 + plt_index * 4);
9688 /* Calculate the offset of the .got.plt entry from
9689 _GLOBAL_OFFSET_TABLE_. */
9690 got_offset = mips_elf_gotplt_index (info, h);
9692 /* Calculate the offset for the branch at the start of the PLT
9693 entry. The branch jumps to the beginning of .plt. */
9694 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9696 /* Fill in the initial value of the .got.plt entry. */
9697 bfd_put_32 (output_bfd, plt_address,
9698 htab->sgotplt->contents + plt_index * 4);
9700 /* Find out where the .plt entry should go. */
9701 loc = htab->splt->contents + h->plt.offset;
9703 if (info->shared)
9705 plt_entry = mips_vxworks_shared_plt_entry;
9706 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9707 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9709 else
9711 bfd_vma got_address_high, got_address_low;
9713 plt_entry = mips_vxworks_exec_plt_entry;
9714 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9715 got_address_low = got_address & 0xffff;
9717 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9718 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9719 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9720 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9721 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9722 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9723 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9724 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9726 loc = (htab->srelplt2->contents
9727 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9729 /* Emit a relocation for the .got.plt entry. */
9730 rel.r_offset = got_address;
9731 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9732 rel.r_addend = h->plt.offset;
9733 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9735 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9736 loc += sizeof (Elf32_External_Rela);
9737 rel.r_offset = plt_address + 8;
9738 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9739 rel.r_addend = got_offset;
9740 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9742 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9743 loc += sizeof (Elf32_External_Rela);
9744 rel.r_offset += 4;
9745 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9746 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9749 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9750 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9751 rel.r_offset = got_address;
9752 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9753 rel.r_addend = 0;
9754 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9756 if (!h->def_regular)
9757 sym->st_shndx = SHN_UNDEF;
9760 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9762 sgot = htab->sgot;
9763 g = htab->got_info;
9764 BFD_ASSERT (g != NULL);
9766 /* See if this symbol has an entry in the GOT. */
9767 if (g->global_gotsym != NULL
9768 && h->dynindx >= g->global_gotsym->dynindx)
9770 bfd_vma offset;
9771 Elf_Internal_Rela outrel;
9772 bfd_byte *loc;
9773 asection *s;
9775 /* Install the symbol value in the GOT. */
9776 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9777 R_MIPS_GOT16, info);
9778 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9780 /* Add a dynamic relocation for it. */
9781 s = mips_elf_rel_dyn_section (info, FALSE);
9782 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9783 outrel.r_offset = (sgot->output_section->vma
9784 + sgot->output_offset
9785 + offset);
9786 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9787 outrel.r_addend = 0;
9788 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9791 /* Emit a copy reloc, if needed. */
9792 if (h->needs_copy)
9794 Elf_Internal_Rela rel;
9796 BFD_ASSERT (h->dynindx != -1);
9798 rel.r_offset = (h->root.u.def.section->output_section->vma
9799 + h->root.u.def.section->output_offset
9800 + h->root.u.def.value);
9801 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9802 rel.r_addend = 0;
9803 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9804 htab->srelbss->contents
9805 + (htab->srelbss->reloc_count
9806 * sizeof (Elf32_External_Rela)));
9807 ++htab->srelbss->reloc_count;
9810 /* If this is a mips16 symbol, force the value to be even. */
9811 if (ELF_ST_IS_MIPS16 (sym->st_other))
9812 sym->st_value &= ~1;
9814 return TRUE;
9817 /* Write out a plt0 entry to the beginning of .plt. */
9819 static void
9820 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9822 bfd_byte *loc;
9823 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9824 static const bfd_vma *plt_entry;
9825 struct mips_elf_link_hash_table *htab;
9827 htab = mips_elf_hash_table (info);
9828 if (ABI_64_P (output_bfd))
9829 plt_entry = mips_n64_exec_plt0_entry;
9830 else if (ABI_N32_P (output_bfd))
9831 plt_entry = mips_n32_exec_plt0_entry;
9832 else
9833 plt_entry = mips_o32_exec_plt0_entry;
9835 /* Calculate the value of .got.plt. */
9836 gotplt_value = (htab->sgotplt->output_section->vma
9837 + htab->sgotplt->output_offset);
9838 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9839 gotplt_value_low = gotplt_value & 0xffff;
9841 /* The PLT sequence is not safe for N64 if .got.plt's address can
9842 not be loaded in two instructions. */
9843 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9844 || ~(gotplt_value | 0x7fffffff) == 0);
9846 /* Install the PLT header. */
9847 loc = htab->splt->contents;
9848 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9849 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9850 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9851 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9852 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9853 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9854 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9855 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9858 /* Install the PLT header for a VxWorks executable and finalize the
9859 contents of .rela.plt.unloaded. */
9861 static void
9862 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9864 Elf_Internal_Rela rela;
9865 bfd_byte *loc;
9866 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9867 static const bfd_vma *plt_entry;
9868 struct mips_elf_link_hash_table *htab;
9870 htab = mips_elf_hash_table (info);
9871 plt_entry = mips_vxworks_exec_plt0_entry;
9873 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9874 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9875 + htab->root.hgot->root.u.def.section->output_offset
9876 + htab->root.hgot->root.u.def.value);
9878 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9879 got_value_low = got_value & 0xffff;
9881 /* Calculate the address of the PLT header. */
9882 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9884 /* Install the PLT header. */
9885 loc = htab->splt->contents;
9886 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9887 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9888 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9889 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9890 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9891 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9893 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9894 loc = htab->srelplt2->contents;
9895 rela.r_offset = plt_address;
9896 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9897 rela.r_addend = 0;
9898 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9899 loc += sizeof (Elf32_External_Rela);
9901 /* Output the relocation for the following addiu of
9902 %lo(_GLOBAL_OFFSET_TABLE_). */
9903 rela.r_offset += 4;
9904 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9905 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9906 loc += sizeof (Elf32_External_Rela);
9908 /* Fix up the remaining relocations. They may have the wrong
9909 symbol index for _G_O_T_ or _P_L_T_ depending on the order
9910 in which symbols were output. */
9911 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9913 Elf_Internal_Rela rel;
9915 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9916 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9917 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9918 loc += sizeof (Elf32_External_Rela);
9920 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9921 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9922 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9923 loc += sizeof (Elf32_External_Rela);
9925 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9926 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9927 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9928 loc += sizeof (Elf32_External_Rela);
9932 /* Install the PLT header for a VxWorks shared library. */
9934 static void
9935 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9937 unsigned int i;
9938 struct mips_elf_link_hash_table *htab;
9940 htab = mips_elf_hash_table (info);
9942 /* We just need to copy the entry byte-by-byte. */
9943 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9944 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9945 htab->splt->contents + i * 4);
9948 /* Finish up the dynamic sections. */
9950 bfd_boolean
9951 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9952 struct bfd_link_info *info)
9954 bfd *dynobj;
9955 asection *sdyn;
9956 asection *sgot;
9957 struct mips_got_info *gg, *g;
9958 struct mips_elf_link_hash_table *htab;
9960 htab = mips_elf_hash_table (info);
9961 dynobj = elf_hash_table (info)->dynobj;
9963 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9965 sgot = htab->sgot;
9966 gg = htab->got_info;
9968 if (elf_hash_table (info)->dynamic_sections_created)
9970 bfd_byte *b;
9971 int dyn_to_skip = 0, dyn_skipped = 0;
9973 BFD_ASSERT (sdyn != NULL);
9974 BFD_ASSERT (gg != NULL);
9976 g = mips_elf_got_for_ibfd (gg, output_bfd);
9977 BFD_ASSERT (g != NULL);
9979 for (b = sdyn->contents;
9980 b < sdyn->contents + sdyn->size;
9981 b += MIPS_ELF_DYN_SIZE (dynobj))
9983 Elf_Internal_Dyn dyn;
9984 const char *name;
9985 size_t elemsize;
9986 asection *s;
9987 bfd_boolean swap_out_p;
9989 /* Read in the current dynamic entry. */
9990 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9992 /* Assume that we're going to modify it and write it out. */
9993 swap_out_p = TRUE;
9995 switch (dyn.d_tag)
9997 case DT_RELENT:
9998 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
9999 break;
10001 case DT_RELAENT:
10002 BFD_ASSERT (htab->is_vxworks);
10003 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10004 break;
10006 case DT_STRSZ:
10007 /* Rewrite DT_STRSZ. */
10008 dyn.d_un.d_val =
10009 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10010 break;
10012 case DT_PLTGOT:
10013 s = htab->sgot;
10014 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10015 break;
10017 case DT_MIPS_PLTGOT:
10018 s = htab->sgotplt;
10019 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10020 break;
10022 case DT_MIPS_RLD_VERSION:
10023 dyn.d_un.d_val = 1; /* XXX */
10024 break;
10026 case DT_MIPS_FLAGS:
10027 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10028 break;
10030 case DT_MIPS_TIME_STAMP:
10032 time_t t;
10033 time (&t);
10034 dyn.d_un.d_val = t;
10036 break;
10038 case DT_MIPS_ICHECKSUM:
10039 /* XXX FIXME: */
10040 swap_out_p = FALSE;
10041 break;
10043 case DT_MIPS_IVERSION:
10044 /* XXX FIXME: */
10045 swap_out_p = FALSE;
10046 break;
10048 case DT_MIPS_BASE_ADDRESS:
10049 s = output_bfd->sections;
10050 BFD_ASSERT (s != NULL);
10051 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10052 break;
10054 case DT_MIPS_LOCAL_GOTNO:
10055 dyn.d_un.d_val = g->local_gotno;
10056 break;
10058 case DT_MIPS_UNREFEXTNO:
10059 /* The index into the dynamic symbol table which is the
10060 entry of the first external symbol that is not
10061 referenced within the same object. */
10062 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10063 break;
10065 case DT_MIPS_GOTSYM:
10066 if (gg->global_gotsym)
10068 dyn.d_un.d_val = gg->global_gotsym->dynindx;
10069 break;
10071 /* In case if we don't have global got symbols we default
10072 to setting DT_MIPS_GOTSYM to the same value as
10073 DT_MIPS_SYMTABNO, so we just fall through. */
10075 case DT_MIPS_SYMTABNO:
10076 name = ".dynsym";
10077 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10078 s = bfd_get_section_by_name (output_bfd, name);
10079 BFD_ASSERT (s != NULL);
10081 dyn.d_un.d_val = s->size / elemsize;
10082 break;
10084 case DT_MIPS_HIPAGENO:
10085 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
10086 break;
10088 case DT_MIPS_RLD_MAP:
10089 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10090 break;
10092 case DT_MIPS_OPTIONS:
10093 s = (bfd_get_section_by_name
10094 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10095 dyn.d_un.d_ptr = s->vma;
10096 break;
10098 case DT_RELASZ:
10099 BFD_ASSERT (htab->is_vxworks);
10100 /* The count does not include the JUMP_SLOT relocations. */
10101 if (htab->srelplt)
10102 dyn.d_un.d_val -= htab->srelplt->size;
10103 break;
10105 case DT_PLTREL:
10106 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10107 if (htab->is_vxworks)
10108 dyn.d_un.d_val = DT_RELA;
10109 else
10110 dyn.d_un.d_val = DT_REL;
10111 break;
10113 case DT_PLTRELSZ:
10114 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10115 dyn.d_un.d_val = htab->srelplt->size;
10116 break;
10118 case DT_JMPREL:
10119 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10120 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
10121 + htab->srelplt->output_offset);
10122 break;
10124 case DT_TEXTREL:
10125 /* If we didn't need any text relocations after all, delete
10126 the dynamic tag. */
10127 if (!(info->flags & DF_TEXTREL))
10129 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10130 swap_out_p = FALSE;
10132 break;
10134 case DT_FLAGS:
10135 /* If we didn't need any text relocations after all, clear
10136 DF_TEXTREL from DT_FLAGS. */
10137 if (!(info->flags & DF_TEXTREL))
10138 dyn.d_un.d_val &= ~DF_TEXTREL;
10139 else
10140 swap_out_p = FALSE;
10141 break;
10143 default:
10144 swap_out_p = FALSE;
10145 if (htab->is_vxworks
10146 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10147 swap_out_p = TRUE;
10148 break;
10151 if (swap_out_p || dyn_skipped)
10152 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10153 (dynobj, &dyn, b - dyn_skipped);
10155 if (dyn_to_skip)
10157 dyn_skipped += dyn_to_skip;
10158 dyn_to_skip = 0;
10162 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10163 if (dyn_skipped > 0)
10164 memset (b - dyn_skipped, 0, dyn_skipped);
10167 if (sgot != NULL && sgot->size > 0
10168 && !bfd_is_abs_section (sgot->output_section))
10170 if (htab->is_vxworks)
10172 /* The first entry of the global offset table points to the
10173 ".dynamic" section. The second is initialized by the
10174 loader and contains the shared library identifier.
10175 The third is also initialized by the loader and points
10176 to the lazy resolution stub. */
10177 MIPS_ELF_PUT_WORD (output_bfd,
10178 sdyn->output_offset + sdyn->output_section->vma,
10179 sgot->contents);
10180 MIPS_ELF_PUT_WORD (output_bfd, 0,
10181 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10182 MIPS_ELF_PUT_WORD (output_bfd, 0,
10183 sgot->contents
10184 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10186 else
10188 /* The first entry of the global offset table will be filled at
10189 runtime. The second entry will be used by some runtime loaders.
10190 This isn't the case of IRIX rld. */
10191 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
10192 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10193 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10196 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10197 = MIPS_ELF_GOT_SIZE (output_bfd);
10200 /* Generate dynamic relocations for the non-primary gots. */
10201 if (gg != NULL && gg->next)
10203 Elf_Internal_Rela rel[3];
10204 bfd_vma addend = 0;
10206 memset (rel, 0, sizeof (rel));
10207 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10209 for (g = gg->next; g->next != gg; g = g->next)
10211 bfd_vma index = g->next->local_gotno + g->next->global_gotno
10212 + g->next->tls_gotno;
10214 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
10215 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10216 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10217 sgot->contents
10218 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10220 if (! info->shared)
10221 continue;
10223 while (index < g->assigned_gotno)
10225 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10226 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10227 if (!(mips_elf_create_dynamic_relocation
10228 (output_bfd, info, rel, NULL,
10229 bfd_abs_section_ptr,
10230 0, &addend, sgot)))
10231 return FALSE;
10232 BFD_ASSERT (addend == 0);
10237 /* The generation of dynamic relocations for the non-primary gots
10238 adds more dynamic relocations. We cannot count them until
10239 here. */
10241 if (elf_hash_table (info)->dynamic_sections_created)
10243 bfd_byte *b;
10244 bfd_boolean swap_out_p;
10246 BFD_ASSERT (sdyn != NULL);
10248 for (b = sdyn->contents;
10249 b < sdyn->contents + sdyn->size;
10250 b += MIPS_ELF_DYN_SIZE (dynobj))
10252 Elf_Internal_Dyn dyn;
10253 asection *s;
10255 /* Read in the current dynamic entry. */
10256 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10258 /* Assume that we're going to modify it and write it out. */
10259 swap_out_p = TRUE;
10261 switch (dyn.d_tag)
10263 case DT_RELSZ:
10264 /* Reduce DT_RELSZ to account for any relocations we
10265 decided not to make. This is for the n64 irix rld,
10266 which doesn't seem to apply any relocations if there
10267 are trailing null entries. */
10268 s = mips_elf_rel_dyn_section (info, FALSE);
10269 dyn.d_un.d_val = (s->reloc_count
10270 * (ABI_64_P (output_bfd)
10271 ? sizeof (Elf64_Mips_External_Rel)
10272 : sizeof (Elf32_External_Rel)));
10273 /* Adjust the section size too. Tools like the prelinker
10274 can reasonably expect the values to the same. */
10275 elf_section_data (s->output_section)->this_hdr.sh_size
10276 = dyn.d_un.d_val;
10277 break;
10279 default:
10280 swap_out_p = FALSE;
10281 break;
10284 if (swap_out_p)
10285 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10286 (dynobj, &dyn, b);
10291 asection *s;
10292 Elf32_compact_rel cpt;
10294 if (SGI_COMPAT (output_bfd))
10296 /* Write .compact_rel section out. */
10297 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10298 if (s != NULL)
10300 cpt.id1 = 1;
10301 cpt.num = s->reloc_count;
10302 cpt.id2 = 2;
10303 cpt.offset = (s->output_section->filepos
10304 + sizeof (Elf32_External_compact_rel));
10305 cpt.reserved0 = 0;
10306 cpt.reserved1 = 0;
10307 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10308 ((Elf32_External_compact_rel *)
10309 s->contents));
10311 /* Clean up a dummy stub function entry in .text. */
10312 if (htab->sstubs != NULL)
10314 file_ptr dummy_offset;
10316 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10317 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10318 memset (htab->sstubs->contents + dummy_offset, 0,
10319 htab->function_stub_size);
10324 /* The psABI says that the dynamic relocations must be sorted in
10325 increasing order of r_symndx. The VxWorks EABI doesn't require
10326 this, and because the code below handles REL rather than RELA
10327 relocations, using it for VxWorks would be outright harmful. */
10328 if (!htab->is_vxworks)
10330 s = mips_elf_rel_dyn_section (info, FALSE);
10331 if (s != NULL
10332 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10334 reldyn_sorting_bfd = output_bfd;
10336 if (ABI_64_P (output_bfd))
10337 qsort ((Elf64_External_Rel *) s->contents + 1,
10338 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10339 sort_dynamic_relocs_64);
10340 else
10341 qsort ((Elf32_External_Rel *) s->contents + 1,
10342 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10343 sort_dynamic_relocs);
10348 if (htab->splt && htab->splt->size > 0)
10350 if (htab->is_vxworks)
10352 if (info->shared)
10353 mips_vxworks_finish_shared_plt (output_bfd, info);
10354 else
10355 mips_vxworks_finish_exec_plt (output_bfd, info);
10357 else
10359 BFD_ASSERT (!info->shared);
10360 mips_finish_exec_plt (output_bfd, info);
10363 return TRUE;
10367 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10369 static void
10370 mips_set_isa_flags (bfd *abfd)
10372 flagword val;
10374 switch (bfd_get_mach (abfd))
10376 default:
10377 case bfd_mach_mips3000:
10378 val = E_MIPS_ARCH_1;
10379 break;
10381 case bfd_mach_mips3900:
10382 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10383 break;
10385 case bfd_mach_mips6000:
10386 val = E_MIPS_ARCH_2;
10387 break;
10389 case bfd_mach_mips4000:
10390 case bfd_mach_mips4300:
10391 case bfd_mach_mips4400:
10392 case bfd_mach_mips4600:
10393 val = E_MIPS_ARCH_3;
10394 break;
10396 case bfd_mach_mips4010:
10397 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10398 break;
10400 case bfd_mach_mips4100:
10401 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10402 break;
10404 case bfd_mach_mips4111:
10405 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10406 break;
10408 case bfd_mach_mips4120:
10409 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10410 break;
10412 case bfd_mach_mips4650:
10413 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10414 break;
10416 case bfd_mach_mips5400:
10417 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10418 break;
10420 case bfd_mach_mips5500:
10421 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10422 break;
10424 case bfd_mach_mips9000:
10425 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10426 break;
10428 case bfd_mach_mips5000:
10429 case bfd_mach_mips7000:
10430 case bfd_mach_mips8000:
10431 case bfd_mach_mips10000:
10432 case bfd_mach_mips12000:
10433 case bfd_mach_mips14000:
10434 case bfd_mach_mips16000:
10435 val = E_MIPS_ARCH_4;
10436 break;
10438 case bfd_mach_mips5:
10439 val = E_MIPS_ARCH_5;
10440 break;
10442 case bfd_mach_mips_loongson_2e:
10443 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10444 break;
10446 case bfd_mach_mips_loongson_2f:
10447 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10448 break;
10450 case bfd_mach_mips_sb1:
10451 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10452 break;
10454 case bfd_mach_mips_octeon:
10455 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10456 break;
10458 case bfd_mach_mips_xlr:
10459 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10460 break;
10462 case bfd_mach_mipsisa32:
10463 val = E_MIPS_ARCH_32;
10464 break;
10466 case bfd_mach_mipsisa64:
10467 val = E_MIPS_ARCH_64;
10468 break;
10470 case bfd_mach_mipsisa32r2:
10471 val = E_MIPS_ARCH_32R2;
10472 break;
10474 case bfd_mach_mipsisa64r2:
10475 val = E_MIPS_ARCH_64R2;
10476 break;
10478 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10479 elf_elfheader (abfd)->e_flags |= val;
10484 /* The final processing done just before writing out a MIPS ELF object
10485 file. This gets the MIPS architecture right based on the machine
10486 number. This is used by both the 32-bit and the 64-bit ABI. */
10488 void
10489 _bfd_mips_elf_final_write_processing (bfd *abfd,
10490 bfd_boolean linker ATTRIBUTE_UNUSED)
10492 unsigned int i;
10493 Elf_Internal_Shdr **hdrpp;
10494 const char *name;
10495 asection *sec;
10497 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10498 is nonzero. This is for compatibility with old objects, which used
10499 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10500 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10501 mips_set_isa_flags (abfd);
10503 /* Set the sh_info field for .gptab sections and other appropriate
10504 info for each special section. */
10505 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10506 i < elf_numsections (abfd);
10507 i++, hdrpp++)
10509 switch ((*hdrpp)->sh_type)
10511 case SHT_MIPS_MSYM:
10512 case SHT_MIPS_LIBLIST:
10513 sec = bfd_get_section_by_name (abfd, ".dynstr");
10514 if (sec != NULL)
10515 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10516 break;
10518 case SHT_MIPS_GPTAB:
10519 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10520 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10521 BFD_ASSERT (name != NULL
10522 && CONST_STRNEQ (name, ".gptab."));
10523 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10524 BFD_ASSERT (sec != NULL);
10525 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10526 break;
10528 case SHT_MIPS_CONTENT:
10529 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10530 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10531 BFD_ASSERT (name != NULL
10532 && CONST_STRNEQ (name, ".MIPS.content"));
10533 sec = bfd_get_section_by_name (abfd,
10534 name + sizeof ".MIPS.content" - 1);
10535 BFD_ASSERT (sec != NULL);
10536 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10537 break;
10539 case SHT_MIPS_SYMBOL_LIB:
10540 sec = bfd_get_section_by_name (abfd, ".dynsym");
10541 if (sec != NULL)
10542 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10543 sec = bfd_get_section_by_name (abfd, ".liblist");
10544 if (sec != NULL)
10545 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10546 break;
10548 case SHT_MIPS_EVENTS:
10549 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10550 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10551 BFD_ASSERT (name != NULL);
10552 if (CONST_STRNEQ (name, ".MIPS.events"))
10553 sec = bfd_get_section_by_name (abfd,
10554 name + sizeof ".MIPS.events" - 1);
10555 else
10557 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
10558 sec = bfd_get_section_by_name (abfd,
10559 (name
10560 + sizeof ".MIPS.post_rel" - 1));
10562 BFD_ASSERT (sec != NULL);
10563 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10564 break;
10570 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
10571 segments. */
10574 _bfd_mips_elf_additional_program_headers (bfd *abfd,
10575 struct bfd_link_info *info ATTRIBUTE_UNUSED)
10577 asection *s;
10578 int ret = 0;
10580 /* See if we need a PT_MIPS_REGINFO segment. */
10581 s = bfd_get_section_by_name (abfd, ".reginfo");
10582 if (s && (s->flags & SEC_LOAD))
10583 ++ret;
10585 /* See if we need a PT_MIPS_OPTIONS segment. */
10586 if (IRIX_COMPAT (abfd) == ict_irix6
10587 && bfd_get_section_by_name (abfd,
10588 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10589 ++ret;
10591 /* See if we need a PT_MIPS_RTPROC segment. */
10592 if (IRIX_COMPAT (abfd) == ict_irix5
10593 && bfd_get_section_by_name (abfd, ".dynamic")
10594 && bfd_get_section_by_name (abfd, ".mdebug"))
10595 ++ret;
10597 /* Allocate a PT_NULL header in dynamic objects. See
10598 _bfd_mips_elf_modify_segment_map for details. */
10599 if (!SGI_COMPAT (abfd)
10600 && bfd_get_section_by_name (abfd, ".dynamic"))
10601 ++ret;
10603 return ret;
10606 /* Modify the segment map for an IRIX5 executable. */
10608 bfd_boolean
10609 _bfd_mips_elf_modify_segment_map (bfd *abfd,
10610 struct bfd_link_info *info)
10612 asection *s;
10613 struct elf_segment_map *m, **pm;
10614 bfd_size_type amt;
10616 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10617 segment. */
10618 s = bfd_get_section_by_name (abfd, ".reginfo");
10619 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10621 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10622 if (m->p_type == PT_MIPS_REGINFO)
10623 break;
10624 if (m == NULL)
10626 amt = sizeof *m;
10627 m = bfd_zalloc (abfd, amt);
10628 if (m == NULL)
10629 return FALSE;
10631 m->p_type = PT_MIPS_REGINFO;
10632 m->count = 1;
10633 m->sections[0] = s;
10635 /* We want to put it after the PHDR and INTERP segments. */
10636 pm = &elf_tdata (abfd)->segment_map;
10637 while (*pm != NULL
10638 && ((*pm)->p_type == PT_PHDR
10639 || (*pm)->p_type == PT_INTERP))
10640 pm = &(*pm)->next;
10642 m->next = *pm;
10643 *pm = m;
10647 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10648 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
10649 PT_MIPS_OPTIONS segment immediately following the program header
10650 table. */
10651 if (NEWABI_P (abfd)
10652 /* On non-IRIX6 new abi, we'll have already created a segment
10653 for this section, so don't create another. I'm not sure this
10654 is not also the case for IRIX 6, but I can't test it right
10655 now. */
10656 && IRIX_COMPAT (abfd) == ict_irix6)
10658 for (s = abfd->sections; s; s = s->next)
10659 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10660 break;
10662 if (s)
10664 struct elf_segment_map *options_segment;
10666 pm = &elf_tdata (abfd)->segment_map;
10667 while (*pm != NULL
10668 && ((*pm)->p_type == PT_PHDR
10669 || (*pm)->p_type == PT_INTERP))
10670 pm = &(*pm)->next;
10672 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10674 amt = sizeof (struct elf_segment_map);
10675 options_segment = bfd_zalloc (abfd, amt);
10676 options_segment->next = *pm;
10677 options_segment->p_type = PT_MIPS_OPTIONS;
10678 options_segment->p_flags = PF_R;
10679 options_segment->p_flags_valid = TRUE;
10680 options_segment->count = 1;
10681 options_segment->sections[0] = s;
10682 *pm = options_segment;
10686 else
10688 if (IRIX_COMPAT (abfd) == ict_irix5)
10690 /* If there are .dynamic and .mdebug sections, we make a room
10691 for the RTPROC header. FIXME: Rewrite without section names. */
10692 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10693 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10694 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10696 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10697 if (m->p_type == PT_MIPS_RTPROC)
10698 break;
10699 if (m == NULL)
10701 amt = sizeof *m;
10702 m = bfd_zalloc (abfd, amt);
10703 if (m == NULL)
10704 return FALSE;
10706 m->p_type = PT_MIPS_RTPROC;
10708 s = bfd_get_section_by_name (abfd, ".rtproc");
10709 if (s == NULL)
10711 m->count = 0;
10712 m->p_flags = 0;
10713 m->p_flags_valid = 1;
10715 else
10717 m->count = 1;
10718 m->sections[0] = s;
10721 /* We want to put it after the DYNAMIC segment. */
10722 pm = &elf_tdata (abfd)->segment_map;
10723 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10724 pm = &(*pm)->next;
10725 if (*pm != NULL)
10726 pm = &(*pm)->next;
10728 m->next = *pm;
10729 *pm = m;
10733 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
10734 .dynstr, .dynsym, and .hash sections, and everything in
10735 between. */
10736 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10737 pm = &(*pm)->next)
10738 if ((*pm)->p_type == PT_DYNAMIC)
10739 break;
10740 m = *pm;
10741 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10743 /* For a normal mips executable the permissions for the PT_DYNAMIC
10744 segment are read, write and execute. We do that here since
10745 the code in elf.c sets only the read permission. This matters
10746 sometimes for the dynamic linker. */
10747 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10749 m->p_flags = PF_R | PF_W | PF_X;
10750 m->p_flags_valid = 1;
10753 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10754 glibc's dynamic linker has traditionally derived the number of
10755 tags from the p_filesz field, and sometimes allocates stack
10756 arrays of that size. An overly-big PT_DYNAMIC segment can
10757 be actively harmful in such cases. Making PT_DYNAMIC contain
10758 other sections can also make life hard for the prelinker,
10759 which might move one of the other sections to a different
10760 PT_LOAD segment. */
10761 if (SGI_COMPAT (abfd)
10762 && m != NULL
10763 && m->count == 1
10764 && strcmp (m->sections[0]->name, ".dynamic") == 0)
10766 static const char *sec_names[] =
10768 ".dynamic", ".dynstr", ".dynsym", ".hash"
10770 bfd_vma low, high;
10771 unsigned int i, c;
10772 struct elf_segment_map *n;
10774 low = ~(bfd_vma) 0;
10775 high = 0;
10776 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10778 s = bfd_get_section_by_name (abfd, sec_names[i]);
10779 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10781 bfd_size_type sz;
10783 if (low > s->vma)
10784 low = s->vma;
10785 sz = s->size;
10786 if (high < s->vma + sz)
10787 high = s->vma + sz;
10791 c = 0;
10792 for (s = abfd->sections; s != NULL; s = s->next)
10793 if ((s->flags & SEC_LOAD) != 0
10794 && s->vma >= low
10795 && s->vma + s->size <= high)
10796 ++c;
10798 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10799 n = bfd_zalloc (abfd, amt);
10800 if (n == NULL)
10801 return FALSE;
10802 *n = *m;
10803 n->count = c;
10805 i = 0;
10806 for (s = abfd->sections; s != NULL; s = s->next)
10808 if ((s->flags & SEC_LOAD) != 0
10809 && s->vma >= low
10810 && s->vma + s->size <= high)
10812 n->sections[i] = s;
10813 ++i;
10817 *pm = n;
10821 /* Allocate a spare program header in dynamic objects so that tools
10822 like the prelinker can add an extra PT_LOAD entry.
10824 If the prelinker needs to make room for a new PT_LOAD entry, its
10825 standard procedure is to move the first (read-only) sections into
10826 the new (writable) segment. However, the MIPS ABI requires
10827 .dynamic to be in a read-only segment, and the section will often
10828 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10830 Although the prelinker could in principle move .dynamic to a
10831 writable segment, it seems better to allocate a spare program
10832 header instead, and avoid the need to move any sections.
10833 There is a long tradition of allocating spare dynamic tags,
10834 so allocating a spare program header seems like a natural
10835 extension.
10837 If INFO is NULL, we may be copying an already prelinked binary
10838 with objcopy or strip, so do not add this header. */
10839 if (info != NULL
10840 && !SGI_COMPAT (abfd)
10841 && bfd_get_section_by_name (abfd, ".dynamic"))
10843 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10844 if ((*pm)->p_type == PT_NULL)
10845 break;
10846 if (*pm == NULL)
10848 m = bfd_zalloc (abfd, sizeof (*m));
10849 if (m == NULL)
10850 return FALSE;
10852 m->p_type = PT_NULL;
10853 *pm = m;
10857 return TRUE;
10860 /* Return the section that should be marked against GC for a given
10861 relocation. */
10863 asection *
10864 _bfd_mips_elf_gc_mark_hook (asection *sec,
10865 struct bfd_link_info *info,
10866 Elf_Internal_Rela *rel,
10867 struct elf_link_hash_entry *h,
10868 Elf_Internal_Sym *sym)
10870 /* ??? Do mips16 stub sections need to be handled special? */
10872 if (h != NULL)
10873 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10875 case R_MIPS_GNU_VTINHERIT:
10876 case R_MIPS_GNU_VTENTRY:
10877 return NULL;
10880 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
10883 /* Update the got entry reference counts for the section being removed. */
10885 bfd_boolean
10886 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10887 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10888 asection *sec ATTRIBUTE_UNUSED,
10889 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
10891 #if 0
10892 Elf_Internal_Shdr *symtab_hdr;
10893 struct elf_link_hash_entry **sym_hashes;
10894 bfd_signed_vma *local_got_refcounts;
10895 const Elf_Internal_Rela *rel, *relend;
10896 unsigned long r_symndx;
10897 struct elf_link_hash_entry *h;
10899 if (info->relocatable)
10900 return TRUE;
10902 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10903 sym_hashes = elf_sym_hashes (abfd);
10904 local_got_refcounts = elf_local_got_refcounts (abfd);
10906 relend = relocs + sec->reloc_count;
10907 for (rel = relocs; rel < relend; rel++)
10908 switch (ELF_R_TYPE (abfd, rel->r_info))
10910 case R_MIPS16_GOT16:
10911 case R_MIPS16_CALL16:
10912 case R_MIPS_GOT16:
10913 case R_MIPS_CALL16:
10914 case R_MIPS_CALL_HI16:
10915 case R_MIPS_CALL_LO16:
10916 case R_MIPS_GOT_HI16:
10917 case R_MIPS_GOT_LO16:
10918 case R_MIPS_GOT_DISP:
10919 case R_MIPS_GOT_PAGE:
10920 case R_MIPS_GOT_OFST:
10921 /* ??? It would seem that the existing MIPS code does no sort
10922 of reference counting or whatnot on its GOT and PLT entries,
10923 so it is not possible to garbage collect them at this time. */
10924 break;
10926 default:
10927 break;
10929 #endif
10931 return TRUE;
10934 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10935 hiding the old indirect symbol. Process additional relocation
10936 information. Also called for weakdefs, in which case we just let
10937 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
10939 void
10940 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
10941 struct elf_link_hash_entry *dir,
10942 struct elf_link_hash_entry *ind)
10944 struct mips_elf_link_hash_entry *dirmips, *indmips;
10946 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
10948 dirmips = (struct mips_elf_link_hash_entry *) dir;
10949 indmips = (struct mips_elf_link_hash_entry *) ind;
10950 /* Any absolute non-dynamic relocations against an indirect or weak
10951 definition will be against the target symbol. */
10952 if (indmips->has_static_relocs)
10953 dirmips->has_static_relocs = TRUE;
10955 if (ind->root.type != bfd_link_hash_indirect)
10956 return;
10958 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10959 if (indmips->readonly_reloc)
10960 dirmips->readonly_reloc = TRUE;
10961 if (indmips->no_fn_stub)
10962 dirmips->no_fn_stub = TRUE;
10963 if (indmips->fn_stub)
10965 dirmips->fn_stub = indmips->fn_stub;
10966 indmips->fn_stub = NULL;
10968 if (indmips->need_fn_stub)
10970 dirmips->need_fn_stub = TRUE;
10971 indmips->need_fn_stub = FALSE;
10973 if (indmips->call_stub)
10975 dirmips->call_stub = indmips->call_stub;
10976 indmips->call_stub = NULL;
10978 if (indmips->call_fp_stub)
10980 dirmips->call_fp_stub = indmips->call_fp_stub;
10981 indmips->call_fp_stub = NULL;
10983 if (indmips->global_got_area < dirmips->global_got_area)
10984 dirmips->global_got_area = indmips->global_got_area;
10985 if (indmips->global_got_area < GGA_NONE)
10986 indmips->global_got_area = GGA_NONE;
10987 if (indmips->has_nonpic_branches)
10988 dirmips->has_nonpic_branches = TRUE;
10990 if (dirmips->tls_type == 0)
10991 dirmips->tls_type = indmips->tls_type;
10994 #define PDR_SIZE 32
10996 bfd_boolean
10997 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
10998 struct bfd_link_info *info)
11000 asection *o;
11001 bfd_boolean ret = FALSE;
11002 unsigned char *tdata;
11003 size_t i, skip;
11005 o = bfd_get_section_by_name (abfd, ".pdr");
11006 if (! o)
11007 return FALSE;
11008 if (o->size == 0)
11009 return FALSE;
11010 if (o->size % PDR_SIZE != 0)
11011 return FALSE;
11012 if (o->output_section != NULL
11013 && bfd_is_abs_section (o->output_section))
11014 return FALSE;
11016 tdata = bfd_zmalloc (o->size / PDR_SIZE);
11017 if (! tdata)
11018 return FALSE;
11020 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
11021 info->keep_memory);
11022 if (!cookie->rels)
11024 free (tdata);
11025 return FALSE;
11028 cookie->rel = cookie->rels;
11029 cookie->relend = cookie->rels + o->reloc_count;
11031 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
11033 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
11035 tdata[i] = 1;
11036 skip ++;
11040 if (skip != 0)
11042 mips_elf_section_data (o)->u.tdata = tdata;
11043 o->size -= skip * PDR_SIZE;
11044 ret = TRUE;
11046 else
11047 free (tdata);
11049 if (! info->keep_memory)
11050 free (cookie->rels);
11052 return ret;
11055 bfd_boolean
11056 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
11058 if (strcmp (sec->name, ".pdr") == 0)
11059 return TRUE;
11060 return FALSE;
11063 bfd_boolean
11064 _bfd_mips_elf_write_section (bfd *output_bfd,
11065 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11066 asection *sec, bfd_byte *contents)
11068 bfd_byte *to, *from, *end;
11069 int i;
11071 if (strcmp (sec->name, ".pdr") != 0)
11072 return FALSE;
11074 if (mips_elf_section_data (sec)->u.tdata == NULL)
11075 return FALSE;
11077 to = contents;
11078 end = contents + sec->size;
11079 for (from = contents, i = 0;
11080 from < end;
11081 from += PDR_SIZE, i++)
11083 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
11084 continue;
11085 if (to != from)
11086 memcpy (to, from, PDR_SIZE);
11087 to += PDR_SIZE;
11089 bfd_set_section_contents (output_bfd, sec->output_section, contents,
11090 sec->output_offset, sec->size);
11091 return TRUE;
11094 /* MIPS ELF uses a special find_nearest_line routine in order the
11095 handle the ECOFF debugging information. */
11097 struct mips_elf_find_line
11099 struct ecoff_debug_info d;
11100 struct ecoff_find_line i;
11103 bfd_boolean
11104 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11105 asymbol **symbols, bfd_vma offset,
11106 const char **filename_ptr,
11107 const char **functionname_ptr,
11108 unsigned int *line_ptr)
11110 asection *msec;
11112 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11113 filename_ptr, functionname_ptr,
11114 line_ptr))
11115 return TRUE;
11117 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11118 filename_ptr, functionname_ptr,
11119 line_ptr, ABI_64_P (abfd) ? 8 : 0,
11120 &elf_tdata (abfd)->dwarf2_find_line_info))
11121 return TRUE;
11123 msec = bfd_get_section_by_name (abfd, ".mdebug");
11124 if (msec != NULL)
11126 flagword origflags;
11127 struct mips_elf_find_line *fi;
11128 const struct ecoff_debug_swap * const swap =
11129 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11131 /* If we are called during a link, mips_elf_final_link may have
11132 cleared the SEC_HAS_CONTENTS field. We force it back on here
11133 if appropriate (which it normally will be). */
11134 origflags = msec->flags;
11135 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11136 msec->flags |= SEC_HAS_CONTENTS;
11138 fi = elf_tdata (abfd)->find_line_info;
11139 if (fi == NULL)
11141 bfd_size_type external_fdr_size;
11142 char *fraw_src;
11143 char *fraw_end;
11144 struct fdr *fdr_ptr;
11145 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11147 fi = bfd_zalloc (abfd, amt);
11148 if (fi == NULL)
11150 msec->flags = origflags;
11151 return FALSE;
11154 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11156 msec->flags = origflags;
11157 return FALSE;
11160 /* Swap in the FDR information. */
11161 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
11162 fi->d.fdr = bfd_alloc (abfd, amt);
11163 if (fi->d.fdr == NULL)
11165 msec->flags = origflags;
11166 return FALSE;
11168 external_fdr_size = swap->external_fdr_size;
11169 fdr_ptr = fi->d.fdr;
11170 fraw_src = (char *) fi->d.external_fdr;
11171 fraw_end = (fraw_src
11172 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11173 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
11174 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
11176 elf_tdata (abfd)->find_line_info = fi;
11178 /* Note that we don't bother to ever free this information.
11179 find_nearest_line is either called all the time, as in
11180 objdump -l, so the information should be saved, or it is
11181 rarely called, as in ld error messages, so the memory
11182 wasted is unimportant. Still, it would probably be a
11183 good idea for free_cached_info to throw it away. */
11186 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11187 &fi->i, filename_ptr, functionname_ptr,
11188 line_ptr))
11190 msec->flags = origflags;
11191 return TRUE;
11194 msec->flags = origflags;
11197 /* Fall back on the generic ELF find_nearest_line routine. */
11199 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11200 filename_ptr, functionname_ptr,
11201 line_ptr);
11204 bfd_boolean
11205 _bfd_mips_elf_find_inliner_info (bfd *abfd,
11206 const char **filename_ptr,
11207 const char **functionname_ptr,
11208 unsigned int *line_ptr)
11210 bfd_boolean found;
11211 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11212 functionname_ptr, line_ptr,
11213 & elf_tdata (abfd)->dwarf2_find_line_info);
11214 return found;
11218 /* When are writing out the .options or .MIPS.options section,
11219 remember the bytes we are writing out, so that we can install the
11220 GP value in the section_processing routine. */
11222 bfd_boolean
11223 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11224 const void *location,
11225 file_ptr offset, bfd_size_type count)
11227 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
11229 bfd_byte *c;
11231 if (elf_section_data (section) == NULL)
11233 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
11234 section->used_by_bfd = bfd_zalloc (abfd, amt);
11235 if (elf_section_data (section) == NULL)
11236 return FALSE;
11238 c = mips_elf_section_data (section)->u.tdata;
11239 if (c == NULL)
11241 c = bfd_zalloc (abfd, section->size);
11242 if (c == NULL)
11243 return FALSE;
11244 mips_elf_section_data (section)->u.tdata = c;
11247 memcpy (c + offset, location, count);
11250 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11251 count);
11254 /* This is almost identical to bfd_generic_get_... except that some
11255 MIPS relocations need to be handled specially. Sigh. */
11257 bfd_byte *
11258 _bfd_elf_mips_get_relocated_section_contents
11259 (bfd *abfd,
11260 struct bfd_link_info *link_info,
11261 struct bfd_link_order *link_order,
11262 bfd_byte *data,
11263 bfd_boolean relocatable,
11264 asymbol **symbols)
11266 /* Get enough memory to hold the stuff */
11267 bfd *input_bfd = link_order->u.indirect.section->owner;
11268 asection *input_section = link_order->u.indirect.section;
11269 bfd_size_type sz;
11271 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11272 arelent **reloc_vector = NULL;
11273 long reloc_count;
11275 if (reloc_size < 0)
11276 goto error_return;
11278 reloc_vector = bfd_malloc (reloc_size);
11279 if (reloc_vector == NULL && reloc_size != 0)
11280 goto error_return;
11282 /* read in the section */
11283 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11284 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
11285 goto error_return;
11287 reloc_count = bfd_canonicalize_reloc (input_bfd,
11288 input_section,
11289 reloc_vector,
11290 symbols);
11291 if (reloc_count < 0)
11292 goto error_return;
11294 if (reloc_count > 0)
11296 arelent **parent;
11297 /* for mips */
11298 int gp_found;
11299 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11302 struct bfd_hash_entry *h;
11303 struct bfd_link_hash_entry *lh;
11304 /* Skip all this stuff if we aren't mixing formats. */
11305 if (abfd && input_bfd
11306 && abfd->xvec == input_bfd->xvec)
11307 lh = 0;
11308 else
11310 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
11311 lh = (struct bfd_link_hash_entry *) h;
11313 lookup:
11314 if (lh)
11316 switch (lh->type)
11318 case bfd_link_hash_undefined:
11319 case bfd_link_hash_undefweak:
11320 case bfd_link_hash_common:
11321 gp_found = 0;
11322 break;
11323 case bfd_link_hash_defined:
11324 case bfd_link_hash_defweak:
11325 gp_found = 1;
11326 gp = lh->u.def.value;
11327 break;
11328 case bfd_link_hash_indirect:
11329 case bfd_link_hash_warning:
11330 lh = lh->u.i.link;
11331 /* @@FIXME ignoring warning for now */
11332 goto lookup;
11333 case bfd_link_hash_new:
11334 default:
11335 abort ();
11338 else
11339 gp_found = 0;
11341 /* end mips */
11342 for (parent = reloc_vector; *parent != NULL; parent++)
11344 char *error_message = NULL;
11345 bfd_reloc_status_type r;
11347 /* Specific to MIPS: Deal with relocation types that require
11348 knowing the gp of the output bfd. */
11349 asymbol *sym = *(*parent)->sym_ptr_ptr;
11351 /* If we've managed to find the gp and have a special
11352 function for the relocation then go ahead, else default
11353 to the generic handling. */
11354 if (gp_found
11355 && (*parent)->howto->special_function
11356 == _bfd_mips_elf32_gprel16_reloc)
11357 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11358 input_section, relocatable,
11359 data, gp);
11360 else
11361 r = bfd_perform_relocation (input_bfd, *parent, data,
11362 input_section,
11363 relocatable ? abfd : NULL,
11364 &error_message);
11366 if (relocatable)
11368 asection *os = input_section->output_section;
11370 /* A partial link, so keep the relocs */
11371 os->orelocation[os->reloc_count] = *parent;
11372 os->reloc_count++;
11375 if (r != bfd_reloc_ok)
11377 switch (r)
11379 case bfd_reloc_undefined:
11380 if (!((*link_info->callbacks->undefined_symbol)
11381 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11382 input_bfd, input_section, (*parent)->address, TRUE)))
11383 goto error_return;
11384 break;
11385 case bfd_reloc_dangerous:
11386 BFD_ASSERT (error_message != NULL);
11387 if (!((*link_info->callbacks->reloc_dangerous)
11388 (link_info, error_message, input_bfd, input_section,
11389 (*parent)->address)))
11390 goto error_return;
11391 break;
11392 case bfd_reloc_overflow:
11393 if (!((*link_info->callbacks->reloc_overflow)
11394 (link_info, NULL,
11395 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11396 (*parent)->howto->name, (*parent)->addend,
11397 input_bfd, input_section, (*parent)->address)))
11398 goto error_return;
11399 break;
11400 case bfd_reloc_outofrange:
11401 default:
11402 abort ();
11403 break;
11409 if (reloc_vector != NULL)
11410 free (reloc_vector);
11411 return data;
11413 error_return:
11414 if (reloc_vector != NULL)
11415 free (reloc_vector);
11416 return NULL;
11419 /* Allocate ABFD's target-dependent data. */
11421 bfd_boolean
11422 _bfd_mips_elf_mkobject (bfd *abfd)
11424 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
11425 MIPS_ELF_TDATA);
11428 /* Create a MIPS ELF linker hash table. */
11430 struct bfd_link_hash_table *
11431 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
11433 struct mips_elf_link_hash_table *ret;
11434 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11436 ret = bfd_malloc (amt);
11437 if (ret == NULL)
11438 return NULL;
11440 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11441 mips_elf_link_hash_newfunc,
11442 sizeof (struct mips_elf_link_hash_entry)))
11444 free (ret);
11445 return NULL;
11448 #if 0
11449 /* We no longer use this. */
11450 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11451 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11452 #endif
11453 ret->procedure_count = 0;
11454 ret->compact_rel_size = 0;
11455 ret->use_rld_obj_head = FALSE;
11456 ret->rld_value = 0;
11457 ret->mips16_stubs_seen = FALSE;
11458 ret->use_plts_and_copy_relocs = FALSE;
11459 ret->is_vxworks = FALSE;
11460 ret->small_data_overflow_reported = FALSE;
11461 ret->srelbss = NULL;
11462 ret->sdynbss = NULL;
11463 ret->srelplt = NULL;
11464 ret->srelplt2 = NULL;
11465 ret->sgotplt = NULL;
11466 ret->splt = NULL;
11467 ret->sstubs = NULL;
11468 ret->sgot = NULL;
11469 ret->got_info = NULL;
11470 ret->plt_header_size = 0;
11471 ret->plt_entry_size = 0;
11472 ret->lazy_stub_count = 0;
11473 ret->function_stub_size = 0;
11474 ret->strampoline = NULL;
11475 ret->la25_stubs = NULL;
11476 ret->add_stub_section = NULL;
11478 return &ret->root.root;
11481 /* Likewise, but indicate that the target is VxWorks. */
11483 struct bfd_link_hash_table *
11484 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11486 struct bfd_link_hash_table *ret;
11488 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11489 if (ret)
11491 struct mips_elf_link_hash_table *htab;
11493 htab = (struct mips_elf_link_hash_table *) ret;
11494 htab->use_plts_and_copy_relocs = TRUE;
11495 htab->is_vxworks = TRUE;
11497 return ret;
11500 /* A function that the linker calls if we are allowed to use PLTs
11501 and copy relocs. */
11503 void
11504 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11506 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11509 /* We need to use a special link routine to handle the .reginfo and
11510 the .mdebug sections. We need to merge all instances of these
11511 sections together, not write them all out sequentially. */
11513 bfd_boolean
11514 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11516 asection *o;
11517 struct bfd_link_order *p;
11518 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11519 asection *rtproc_sec;
11520 Elf32_RegInfo reginfo;
11521 struct ecoff_debug_info debug;
11522 struct mips_htab_traverse_info hti;
11523 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11524 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
11525 HDRR *symhdr = &debug.symbolic_header;
11526 void *mdebug_handle = NULL;
11527 asection *s;
11528 EXTR esym;
11529 unsigned int i;
11530 bfd_size_type amt;
11531 struct mips_elf_link_hash_table *htab;
11533 static const char * const secname[] =
11535 ".text", ".init", ".fini", ".data",
11536 ".rodata", ".sdata", ".sbss", ".bss"
11538 static const int sc[] =
11540 scText, scInit, scFini, scData,
11541 scRData, scSData, scSBss, scBss
11544 /* Sort the dynamic symbols so that those with GOT entries come after
11545 those without. */
11546 htab = mips_elf_hash_table (info);
11547 if (!mips_elf_sort_hash_table (abfd, info))
11548 return FALSE;
11550 /* Create any scheduled LA25 stubs. */
11551 hti.info = info;
11552 hti.output_bfd = abfd;
11553 hti.error = FALSE;
11554 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11555 if (hti.error)
11556 return FALSE;
11558 /* Get a value for the GP register. */
11559 if (elf_gp (abfd) == 0)
11561 struct bfd_link_hash_entry *h;
11563 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
11564 if (h != NULL && h->type == bfd_link_hash_defined)
11565 elf_gp (abfd) = (h->u.def.value
11566 + h->u.def.section->output_section->vma
11567 + h->u.def.section->output_offset);
11568 else if (htab->is_vxworks
11569 && (h = bfd_link_hash_lookup (info->hash,
11570 "_GLOBAL_OFFSET_TABLE_",
11571 FALSE, FALSE, TRUE))
11572 && h->type == bfd_link_hash_defined)
11573 elf_gp (abfd) = (h->u.def.section->output_section->vma
11574 + h->u.def.section->output_offset
11575 + h->u.def.value);
11576 else if (info->relocatable)
11578 bfd_vma lo = MINUS_ONE;
11580 /* Find the GP-relative section with the lowest offset. */
11581 for (o = abfd->sections; o != NULL; o = o->next)
11582 if (o->vma < lo
11583 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11584 lo = o->vma;
11586 /* And calculate GP relative to that. */
11587 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
11589 else
11591 /* If the relocate_section function needs to do a reloc
11592 involving the GP value, it should make a reloc_dangerous
11593 callback to warn that GP is not defined. */
11597 /* Go through the sections and collect the .reginfo and .mdebug
11598 information. */
11599 reginfo_sec = NULL;
11600 mdebug_sec = NULL;
11601 gptab_data_sec = NULL;
11602 gptab_bss_sec = NULL;
11603 for (o = abfd->sections; o != NULL; o = o->next)
11605 if (strcmp (o->name, ".reginfo") == 0)
11607 memset (&reginfo, 0, sizeof reginfo);
11609 /* We have found the .reginfo section in the output file.
11610 Look through all the link_orders comprising it and merge
11611 the information together. */
11612 for (p = o->map_head.link_order; p != NULL; p = p->next)
11614 asection *input_section;
11615 bfd *input_bfd;
11616 Elf32_External_RegInfo ext;
11617 Elf32_RegInfo sub;
11619 if (p->type != bfd_indirect_link_order)
11621 if (p->type == bfd_data_link_order)
11622 continue;
11623 abort ();
11626 input_section = p->u.indirect.section;
11627 input_bfd = input_section->owner;
11629 if (! bfd_get_section_contents (input_bfd, input_section,
11630 &ext, 0, sizeof ext))
11631 return FALSE;
11633 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11635 reginfo.ri_gprmask |= sub.ri_gprmask;
11636 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11637 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11638 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11639 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11641 /* ri_gp_value is set by the function
11642 mips_elf32_section_processing when the section is
11643 finally written out. */
11645 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11646 elf_link_input_bfd ignores this section. */
11647 input_section->flags &= ~SEC_HAS_CONTENTS;
11650 /* Size has been set in _bfd_mips_elf_always_size_sections. */
11651 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
11653 /* Skip this section later on (I don't think this currently
11654 matters, but someday it might). */
11655 o->map_head.link_order = NULL;
11657 reginfo_sec = o;
11660 if (strcmp (o->name, ".mdebug") == 0)
11662 struct extsym_info einfo;
11663 bfd_vma last;
11665 /* We have found the .mdebug section in the output file.
11666 Look through all the link_orders comprising it and merge
11667 the information together. */
11668 symhdr->magic = swap->sym_magic;
11669 /* FIXME: What should the version stamp be? */
11670 symhdr->vstamp = 0;
11671 symhdr->ilineMax = 0;
11672 symhdr->cbLine = 0;
11673 symhdr->idnMax = 0;
11674 symhdr->ipdMax = 0;
11675 symhdr->isymMax = 0;
11676 symhdr->ioptMax = 0;
11677 symhdr->iauxMax = 0;
11678 symhdr->issMax = 0;
11679 symhdr->issExtMax = 0;
11680 symhdr->ifdMax = 0;
11681 symhdr->crfd = 0;
11682 symhdr->iextMax = 0;
11684 /* We accumulate the debugging information itself in the
11685 debug_info structure. */
11686 debug.line = NULL;
11687 debug.external_dnr = NULL;
11688 debug.external_pdr = NULL;
11689 debug.external_sym = NULL;
11690 debug.external_opt = NULL;
11691 debug.external_aux = NULL;
11692 debug.ss = NULL;
11693 debug.ssext = debug.ssext_end = NULL;
11694 debug.external_fdr = NULL;
11695 debug.external_rfd = NULL;
11696 debug.external_ext = debug.external_ext_end = NULL;
11698 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
11699 if (mdebug_handle == NULL)
11700 return FALSE;
11702 esym.jmptbl = 0;
11703 esym.cobol_main = 0;
11704 esym.weakext = 0;
11705 esym.reserved = 0;
11706 esym.ifd = ifdNil;
11707 esym.asym.iss = issNil;
11708 esym.asym.st = stLocal;
11709 esym.asym.reserved = 0;
11710 esym.asym.index = indexNil;
11711 last = 0;
11712 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11714 esym.asym.sc = sc[i];
11715 s = bfd_get_section_by_name (abfd, secname[i]);
11716 if (s != NULL)
11718 esym.asym.value = s->vma;
11719 last = s->vma + s->size;
11721 else
11722 esym.asym.value = last;
11723 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11724 secname[i], &esym))
11725 return FALSE;
11728 for (p = o->map_head.link_order; p != NULL; p = p->next)
11730 asection *input_section;
11731 bfd *input_bfd;
11732 const struct ecoff_debug_swap *input_swap;
11733 struct ecoff_debug_info input_debug;
11734 char *eraw_src;
11735 char *eraw_end;
11737 if (p->type != bfd_indirect_link_order)
11739 if (p->type == bfd_data_link_order)
11740 continue;
11741 abort ();
11744 input_section = p->u.indirect.section;
11745 input_bfd = input_section->owner;
11747 if (!is_mips_elf (input_bfd))
11749 /* I don't know what a non MIPS ELF bfd would be
11750 doing with a .mdebug section, but I don't really
11751 want to deal with it. */
11752 continue;
11755 input_swap = (get_elf_backend_data (input_bfd)
11756 ->elf_backend_ecoff_debug_swap);
11758 BFD_ASSERT (p->size == input_section->size);
11760 /* The ECOFF linking code expects that we have already
11761 read in the debugging information and set up an
11762 ecoff_debug_info structure, so we do that now. */
11763 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11764 &input_debug))
11765 return FALSE;
11767 if (! (bfd_ecoff_debug_accumulate
11768 (mdebug_handle, abfd, &debug, swap, input_bfd,
11769 &input_debug, input_swap, info)))
11770 return FALSE;
11772 /* Loop through the external symbols. For each one with
11773 interesting information, try to find the symbol in
11774 the linker global hash table and save the information
11775 for the output external symbols. */
11776 eraw_src = input_debug.external_ext;
11777 eraw_end = (eraw_src
11778 + (input_debug.symbolic_header.iextMax
11779 * input_swap->external_ext_size));
11780 for (;
11781 eraw_src < eraw_end;
11782 eraw_src += input_swap->external_ext_size)
11784 EXTR ext;
11785 const char *name;
11786 struct mips_elf_link_hash_entry *h;
11788 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
11789 if (ext.asym.sc == scNil
11790 || ext.asym.sc == scUndefined
11791 || ext.asym.sc == scSUndefined)
11792 continue;
11794 name = input_debug.ssext + ext.asym.iss;
11795 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
11796 name, FALSE, FALSE, TRUE);
11797 if (h == NULL || h->esym.ifd != -2)
11798 continue;
11800 if (ext.ifd != -1)
11802 BFD_ASSERT (ext.ifd
11803 < input_debug.symbolic_header.ifdMax);
11804 ext.ifd = input_debug.ifdmap[ext.ifd];
11807 h->esym = ext;
11810 /* Free up the information we just read. */
11811 free (input_debug.line);
11812 free (input_debug.external_dnr);
11813 free (input_debug.external_pdr);
11814 free (input_debug.external_sym);
11815 free (input_debug.external_opt);
11816 free (input_debug.external_aux);
11817 free (input_debug.ss);
11818 free (input_debug.ssext);
11819 free (input_debug.external_fdr);
11820 free (input_debug.external_rfd);
11821 free (input_debug.external_ext);
11823 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11824 elf_link_input_bfd ignores this section. */
11825 input_section->flags &= ~SEC_HAS_CONTENTS;
11828 if (SGI_COMPAT (abfd) && info->shared)
11830 /* Create .rtproc section. */
11831 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11832 if (rtproc_sec == NULL)
11834 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11835 | SEC_LINKER_CREATED | SEC_READONLY);
11837 rtproc_sec = bfd_make_section_with_flags (abfd,
11838 ".rtproc",
11839 flags);
11840 if (rtproc_sec == NULL
11841 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11842 return FALSE;
11845 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11846 info, rtproc_sec,
11847 &debug))
11848 return FALSE;
11851 /* Build the external symbol information. */
11852 einfo.abfd = abfd;
11853 einfo.info = info;
11854 einfo.debug = &debug;
11855 einfo.swap = swap;
11856 einfo.failed = FALSE;
11857 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11858 mips_elf_output_extsym, &einfo);
11859 if (einfo.failed)
11860 return FALSE;
11862 /* Set the size of the .mdebug section. */
11863 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11865 /* Skip this section later on (I don't think this currently
11866 matters, but someday it might). */
11867 o->map_head.link_order = NULL;
11869 mdebug_sec = o;
11872 if (CONST_STRNEQ (o->name, ".gptab."))
11874 const char *subname;
11875 unsigned int c;
11876 Elf32_gptab *tab;
11877 Elf32_External_gptab *ext_tab;
11878 unsigned int j;
11880 /* The .gptab.sdata and .gptab.sbss sections hold
11881 information describing how the small data area would
11882 change depending upon the -G switch. These sections
11883 not used in executables files. */
11884 if (! info->relocatable)
11886 for (p = o->map_head.link_order; p != NULL; p = p->next)
11888 asection *input_section;
11890 if (p->type != bfd_indirect_link_order)
11892 if (p->type == bfd_data_link_order)
11893 continue;
11894 abort ();
11897 input_section = p->u.indirect.section;
11899 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11900 elf_link_input_bfd ignores this section. */
11901 input_section->flags &= ~SEC_HAS_CONTENTS;
11904 /* Skip this section later on (I don't think this
11905 currently matters, but someday it might). */
11906 o->map_head.link_order = NULL;
11908 /* Really remove the section. */
11909 bfd_section_list_remove (abfd, o);
11910 --abfd->section_count;
11912 continue;
11915 /* There is one gptab for initialized data, and one for
11916 uninitialized data. */
11917 if (strcmp (o->name, ".gptab.sdata") == 0)
11918 gptab_data_sec = o;
11919 else if (strcmp (o->name, ".gptab.sbss") == 0)
11920 gptab_bss_sec = o;
11921 else
11923 (*_bfd_error_handler)
11924 (_("%s: illegal section name `%s'"),
11925 bfd_get_filename (abfd), o->name);
11926 bfd_set_error (bfd_error_nonrepresentable_section);
11927 return FALSE;
11930 /* The linker script always combines .gptab.data and
11931 .gptab.sdata into .gptab.sdata, and likewise for
11932 .gptab.bss and .gptab.sbss. It is possible that there is
11933 no .sdata or .sbss section in the output file, in which
11934 case we must change the name of the output section. */
11935 subname = o->name + sizeof ".gptab" - 1;
11936 if (bfd_get_section_by_name (abfd, subname) == NULL)
11938 if (o == gptab_data_sec)
11939 o->name = ".gptab.data";
11940 else
11941 o->name = ".gptab.bss";
11942 subname = o->name + sizeof ".gptab" - 1;
11943 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11946 /* Set up the first entry. */
11947 c = 1;
11948 amt = c * sizeof (Elf32_gptab);
11949 tab = bfd_malloc (amt);
11950 if (tab == NULL)
11951 return FALSE;
11952 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11953 tab[0].gt_header.gt_unused = 0;
11955 /* Combine the input sections. */
11956 for (p = o->map_head.link_order; p != NULL; p = p->next)
11958 asection *input_section;
11959 bfd *input_bfd;
11960 bfd_size_type size;
11961 unsigned long last;
11962 bfd_size_type gpentry;
11964 if (p->type != bfd_indirect_link_order)
11966 if (p->type == bfd_data_link_order)
11967 continue;
11968 abort ();
11971 input_section = p->u.indirect.section;
11972 input_bfd = input_section->owner;
11974 /* Combine the gptab entries for this input section one
11975 by one. We know that the input gptab entries are
11976 sorted by ascending -G value. */
11977 size = input_section->size;
11978 last = 0;
11979 for (gpentry = sizeof (Elf32_External_gptab);
11980 gpentry < size;
11981 gpentry += sizeof (Elf32_External_gptab))
11983 Elf32_External_gptab ext_gptab;
11984 Elf32_gptab int_gptab;
11985 unsigned long val;
11986 unsigned long add;
11987 bfd_boolean exact;
11988 unsigned int look;
11990 if (! (bfd_get_section_contents
11991 (input_bfd, input_section, &ext_gptab, gpentry,
11992 sizeof (Elf32_External_gptab))))
11994 free (tab);
11995 return FALSE;
11998 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
11999 &int_gptab);
12000 val = int_gptab.gt_entry.gt_g_value;
12001 add = int_gptab.gt_entry.gt_bytes - last;
12003 exact = FALSE;
12004 for (look = 1; look < c; look++)
12006 if (tab[look].gt_entry.gt_g_value >= val)
12007 tab[look].gt_entry.gt_bytes += add;
12009 if (tab[look].gt_entry.gt_g_value == val)
12010 exact = TRUE;
12013 if (! exact)
12015 Elf32_gptab *new_tab;
12016 unsigned int max;
12018 /* We need a new table entry. */
12019 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
12020 new_tab = bfd_realloc (tab, amt);
12021 if (new_tab == NULL)
12023 free (tab);
12024 return FALSE;
12026 tab = new_tab;
12027 tab[c].gt_entry.gt_g_value = val;
12028 tab[c].gt_entry.gt_bytes = add;
12030 /* Merge in the size for the next smallest -G
12031 value, since that will be implied by this new
12032 value. */
12033 max = 0;
12034 for (look = 1; look < c; look++)
12036 if (tab[look].gt_entry.gt_g_value < val
12037 && (max == 0
12038 || (tab[look].gt_entry.gt_g_value
12039 > tab[max].gt_entry.gt_g_value)))
12040 max = look;
12042 if (max != 0)
12043 tab[c].gt_entry.gt_bytes +=
12044 tab[max].gt_entry.gt_bytes;
12046 ++c;
12049 last = int_gptab.gt_entry.gt_bytes;
12052 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12053 elf_link_input_bfd ignores this section. */
12054 input_section->flags &= ~SEC_HAS_CONTENTS;
12057 /* The table must be sorted by -G value. */
12058 if (c > 2)
12059 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12061 /* Swap out the table. */
12062 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
12063 ext_tab = bfd_alloc (abfd, amt);
12064 if (ext_tab == NULL)
12066 free (tab);
12067 return FALSE;
12070 for (j = 0; j < c; j++)
12071 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12072 free (tab);
12074 o->size = c * sizeof (Elf32_External_gptab);
12075 o->contents = (bfd_byte *) ext_tab;
12077 /* Skip this section later on (I don't think this currently
12078 matters, but someday it might). */
12079 o->map_head.link_order = NULL;
12083 /* Invoke the regular ELF backend linker to do all the work. */
12084 if (!bfd_elf_final_link (abfd, info))
12085 return FALSE;
12087 /* Now write out the computed sections. */
12089 if (reginfo_sec != NULL)
12091 Elf32_External_RegInfo ext;
12093 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
12094 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
12095 return FALSE;
12098 if (mdebug_sec != NULL)
12100 BFD_ASSERT (abfd->output_has_begun);
12101 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12102 swap, info,
12103 mdebug_sec->filepos))
12104 return FALSE;
12106 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12109 if (gptab_data_sec != NULL)
12111 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12112 gptab_data_sec->contents,
12113 0, gptab_data_sec->size))
12114 return FALSE;
12117 if (gptab_bss_sec != NULL)
12119 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12120 gptab_bss_sec->contents,
12121 0, gptab_bss_sec->size))
12122 return FALSE;
12125 if (SGI_COMPAT (abfd))
12127 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12128 if (rtproc_sec != NULL)
12130 if (! bfd_set_section_contents (abfd, rtproc_sec,
12131 rtproc_sec->contents,
12132 0, rtproc_sec->size))
12133 return FALSE;
12137 return TRUE;
12140 /* Structure for saying that BFD machine EXTENSION extends BASE. */
12142 struct mips_mach_extension {
12143 unsigned long extension, base;
12147 /* An array describing how BFD machines relate to one another. The entries
12148 are ordered topologically with MIPS I extensions listed last. */
12150 static const struct mips_mach_extension mips_mach_extensions[] = {
12151 /* MIPS64r2 extensions. */
12152 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12154 /* MIPS64 extensions. */
12155 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
12156 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
12157 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
12159 /* MIPS V extensions. */
12160 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12162 /* R10000 extensions. */
12163 { bfd_mach_mips12000, bfd_mach_mips10000 },
12164 { bfd_mach_mips14000, bfd_mach_mips10000 },
12165 { bfd_mach_mips16000, bfd_mach_mips10000 },
12167 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12168 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12169 better to allow vr5400 and vr5500 code to be merged anyway, since
12170 many libraries will just use the core ISA. Perhaps we could add
12171 some sort of ASE flag if this ever proves a problem. */
12172 { bfd_mach_mips5500, bfd_mach_mips5400 },
12173 { bfd_mach_mips5400, bfd_mach_mips5000 },
12175 /* MIPS IV extensions. */
12176 { bfd_mach_mips5, bfd_mach_mips8000 },
12177 { bfd_mach_mips10000, bfd_mach_mips8000 },
12178 { bfd_mach_mips5000, bfd_mach_mips8000 },
12179 { bfd_mach_mips7000, bfd_mach_mips8000 },
12180 { bfd_mach_mips9000, bfd_mach_mips8000 },
12182 /* VR4100 extensions. */
12183 { bfd_mach_mips4120, bfd_mach_mips4100 },
12184 { bfd_mach_mips4111, bfd_mach_mips4100 },
12186 /* MIPS III extensions. */
12187 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12188 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
12189 { bfd_mach_mips8000, bfd_mach_mips4000 },
12190 { bfd_mach_mips4650, bfd_mach_mips4000 },
12191 { bfd_mach_mips4600, bfd_mach_mips4000 },
12192 { bfd_mach_mips4400, bfd_mach_mips4000 },
12193 { bfd_mach_mips4300, bfd_mach_mips4000 },
12194 { bfd_mach_mips4100, bfd_mach_mips4000 },
12195 { bfd_mach_mips4010, bfd_mach_mips4000 },
12197 /* MIPS32 extensions. */
12198 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12200 /* MIPS II extensions. */
12201 { bfd_mach_mips4000, bfd_mach_mips6000 },
12202 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12204 /* MIPS I extensions. */
12205 { bfd_mach_mips6000, bfd_mach_mips3000 },
12206 { bfd_mach_mips3900, bfd_mach_mips3000 }
12210 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12212 static bfd_boolean
12213 mips_mach_extends_p (unsigned long base, unsigned long extension)
12215 size_t i;
12217 if (extension == base)
12218 return TRUE;
12220 if (base == bfd_mach_mipsisa32
12221 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12222 return TRUE;
12224 if (base == bfd_mach_mipsisa32r2
12225 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12226 return TRUE;
12228 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
12229 if (extension == mips_mach_extensions[i].extension)
12231 extension = mips_mach_extensions[i].base;
12232 if (extension == base)
12233 return TRUE;
12236 return FALSE;
12240 /* Return true if the given ELF header flags describe a 32-bit binary. */
12242 static bfd_boolean
12243 mips_32bit_flags_p (flagword flags)
12245 return ((flags & EF_MIPS_32BITMODE) != 0
12246 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12247 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12248 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12249 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12250 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12251 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
12255 /* Merge object attributes from IBFD into OBFD. Raise an error if
12256 there are conflicting attributes. */
12257 static bfd_boolean
12258 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12260 obj_attribute *in_attr;
12261 obj_attribute *out_attr;
12263 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12265 /* This is the first object. Copy the attributes. */
12266 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12268 /* Use the Tag_null value to indicate the attributes have been
12269 initialized. */
12270 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12272 return TRUE;
12275 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12276 non-conflicting ones. */
12277 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12278 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12279 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12281 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12282 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12283 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12284 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12286 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12287 _bfd_error_handler
12288 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12289 in_attr[Tag_GNU_MIPS_ABI_FP].i);
12290 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12291 _bfd_error_handler
12292 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12293 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12294 else
12295 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12297 case 1:
12298 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12300 case 2:
12301 _bfd_error_handler
12302 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12303 obfd, ibfd);
12304 break;
12306 case 3:
12307 _bfd_error_handler
12308 (_("Warning: %B uses hard float, %B uses soft float"),
12309 obfd, ibfd);
12310 break;
12312 case 4:
12313 _bfd_error_handler
12314 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12315 obfd, ibfd);
12316 break;
12318 default:
12319 abort ();
12321 break;
12323 case 2:
12324 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12326 case 1:
12327 _bfd_error_handler
12328 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12329 ibfd, obfd);
12330 break;
12332 case 3:
12333 _bfd_error_handler
12334 (_("Warning: %B uses hard float, %B uses soft float"),
12335 obfd, ibfd);
12336 break;
12338 case 4:
12339 _bfd_error_handler
12340 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12341 obfd, ibfd);
12342 break;
12344 default:
12345 abort ();
12347 break;
12349 case 3:
12350 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12352 case 1:
12353 case 2:
12354 case 4:
12355 _bfd_error_handler
12356 (_("Warning: %B uses hard float, %B uses soft float"),
12357 ibfd, obfd);
12358 break;
12360 default:
12361 abort ();
12363 break;
12365 case 4:
12366 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12368 case 1:
12369 _bfd_error_handler
12370 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12371 ibfd, obfd);
12372 break;
12374 case 2:
12375 _bfd_error_handler
12376 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12377 ibfd, obfd);
12378 break;
12380 case 3:
12381 _bfd_error_handler
12382 (_("Warning: %B uses hard float, %B uses soft float"),
12383 obfd, ibfd);
12384 break;
12386 default:
12387 abort ();
12389 break;
12391 default:
12392 abort ();
12396 /* Merge Tag_compatibility attributes and any common GNU ones. */
12397 _bfd_elf_merge_object_attributes (ibfd, obfd);
12399 return TRUE;
12402 /* Merge backend specific data from an object file to the output
12403 object file when linking. */
12405 bfd_boolean
12406 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
12408 flagword old_flags;
12409 flagword new_flags;
12410 bfd_boolean ok;
12411 bfd_boolean null_input_bfd = TRUE;
12412 asection *sec;
12414 /* Check if we have the same endianess */
12415 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
12417 (*_bfd_error_handler)
12418 (_("%B: endianness incompatible with that of the selected emulation"),
12419 ibfd);
12420 return FALSE;
12423 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
12424 return TRUE;
12426 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12428 (*_bfd_error_handler)
12429 (_("%B: ABI is incompatible with that of the selected emulation"),
12430 ibfd);
12431 return FALSE;
12434 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12435 return FALSE;
12437 new_flags = elf_elfheader (ibfd)->e_flags;
12438 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12439 old_flags = elf_elfheader (obfd)->e_flags;
12441 if (! elf_flags_init (obfd))
12443 elf_flags_init (obfd) = TRUE;
12444 elf_elfheader (obfd)->e_flags = new_flags;
12445 elf_elfheader (obfd)->e_ident[EI_CLASS]
12446 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12448 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
12449 && (bfd_get_arch_info (obfd)->the_default
12450 || mips_mach_extends_p (bfd_get_mach (obfd),
12451 bfd_get_mach (ibfd))))
12453 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12454 bfd_get_mach (ibfd)))
12455 return FALSE;
12458 return TRUE;
12461 /* Check flag compatibility. */
12463 new_flags &= ~EF_MIPS_NOREORDER;
12464 old_flags &= ~EF_MIPS_NOREORDER;
12466 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12467 doesn't seem to matter. */
12468 new_flags &= ~EF_MIPS_XGOT;
12469 old_flags &= ~EF_MIPS_XGOT;
12471 /* MIPSpro generates ucode info in n64 objects. Again, we should
12472 just be able to ignore this. */
12473 new_flags &= ~EF_MIPS_UCODE;
12474 old_flags &= ~EF_MIPS_UCODE;
12476 /* DSOs should only be linked with CPIC code. */
12477 if ((ibfd->flags & DYNAMIC) != 0)
12478 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
12480 if (new_flags == old_flags)
12481 return TRUE;
12483 /* Check to see if the input BFD actually contains any sections.
12484 If not, its flags may not have been initialised either, but it cannot
12485 actually cause any incompatibility. */
12486 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12488 /* Ignore synthetic sections and empty .text, .data and .bss sections
12489 which are automatically generated by gas. */
12490 if (strcmp (sec->name, ".reginfo")
12491 && strcmp (sec->name, ".mdebug")
12492 && (sec->size != 0
12493 || (strcmp (sec->name, ".text")
12494 && strcmp (sec->name, ".data")
12495 && strcmp (sec->name, ".bss"))))
12497 null_input_bfd = FALSE;
12498 break;
12501 if (null_input_bfd)
12502 return TRUE;
12504 ok = TRUE;
12506 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12507 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
12509 (*_bfd_error_handler)
12510 (_("%B: warning: linking abicalls files with non-abicalls files"),
12511 ibfd);
12512 ok = TRUE;
12515 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12516 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12517 if (! (new_flags & EF_MIPS_PIC))
12518 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12520 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12521 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12523 /* Compare the ISAs. */
12524 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
12526 (*_bfd_error_handler)
12527 (_("%B: linking 32-bit code with 64-bit code"),
12528 ibfd);
12529 ok = FALSE;
12531 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12533 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12534 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
12536 /* Copy the architecture info from IBFD to OBFD. Also copy
12537 the 32-bit flag (if set) so that we continue to recognise
12538 OBFD as a 32-bit binary. */
12539 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12540 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12541 elf_elfheader (obfd)->e_flags
12542 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12544 /* Copy across the ABI flags if OBFD doesn't use them
12545 and if that was what caused us to treat IBFD as 32-bit. */
12546 if ((old_flags & EF_MIPS_ABI) == 0
12547 && mips_32bit_flags_p (new_flags)
12548 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12549 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
12551 else
12553 /* The ISAs aren't compatible. */
12554 (*_bfd_error_handler)
12555 (_("%B: linking %s module with previous %s modules"),
12556 ibfd,
12557 bfd_printable_name (ibfd),
12558 bfd_printable_name (obfd));
12559 ok = FALSE;
12563 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12564 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12566 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
12567 does set EI_CLASS differently from any 32-bit ABI. */
12568 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12569 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12570 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12572 /* Only error if both are set (to different values). */
12573 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12574 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12575 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12577 (*_bfd_error_handler)
12578 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12579 ibfd,
12580 elf_mips_abi_name (ibfd),
12581 elf_mips_abi_name (obfd));
12582 ok = FALSE;
12584 new_flags &= ~EF_MIPS_ABI;
12585 old_flags &= ~EF_MIPS_ABI;
12588 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12589 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12591 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12593 new_flags &= ~ EF_MIPS_ARCH_ASE;
12594 old_flags &= ~ EF_MIPS_ARCH_ASE;
12597 /* Warn about any other mismatches */
12598 if (new_flags != old_flags)
12600 (*_bfd_error_handler)
12601 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12602 ibfd, (unsigned long) new_flags,
12603 (unsigned long) old_flags);
12604 ok = FALSE;
12607 if (! ok)
12609 bfd_set_error (bfd_error_bad_value);
12610 return FALSE;
12613 return TRUE;
12616 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12618 bfd_boolean
12619 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
12621 BFD_ASSERT (!elf_flags_init (abfd)
12622 || elf_elfheader (abfd)->e_flags == flags);
12624 elf_elfheader (abfd)->e_flags = flags;
12625 elf_flags_init (abfd) = TRUE;
12626 return TRUE;
12629 char *
12630 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12632 switch (dtag)
12634 default: return "";
12635 case DT_MIPS_RLD_VERSION:
12636 return "MIPS_RLD_VERSION";
12637 case DT_MIPS_TIME_STAMP:
12638 return "MIPS_TIME_STAMP";
12639 case DT_MIPS_ICHECKSUM:
12640 return "MIPS_ICHECKSUM";
12641 case DT_MIPS_IVERSION:
12642 return "MIPS_IVERSION";
12643 case DT_MIPS_FLAGS:
12644 return "MIPS_FLAGS";
12645 case DT_MIPS_BASE_ADDRESS:
12646 return "MIPS_BASE_ADDRESS";
12647 case DT_MIPS_MSYM:
12648 return "MIPS_MSYM";
12649 case DT_MIPS_CONFLICT:
12650 return "MIPS_CONFLICT";
12651 case DT_MIPS_LIBLIST:
12652 return "MIPS_LIBLIST";
12653 case DT_MIPS_LOCAL_GOTNO:
12654 return "MIPS_LOCAL_GOTNO";
12655 case DT_MIPS_CONFLICTNO:
12656 return "MIPS_CONFLICTNO";
12657 case DT_MIPS_LIBLISTNO:
12658 return "MIPS_LIBLISTNO";
12659 case DT_MIPS_SYMTABNO:
12660 return "MIPS_SYMTABNO";
12661 case DT_MIPS_UNREFEXTNO:
12662 return "MIPS_UNREFEXTNO";
12663 case DT_MIPS_GOTSYM:
12664 return "MIPS_GOTSYM";
12665 case DT_MIPS_HIPAGENO:
12666 return "MIPS_HIPAGENO";
12667 case DT_MIPS_RLD_MAP:
12668 return "MIPS_RLD_MAP";
12669 case DT_MIPS_DELTA_CLASS:
12670 return "MIPS_DELTA_CLASS";
12671 case DT_MIPS_DELTA_CLASS_NO:
12672 return "MIPS_DELTA_CLASS_NO";
12673 case DT_MIPS_DELTA_INSTANCE:
12674 return "MIPS_DELTA_INSTANCE";
12675 case DT_MIPS_DELTA_INSTANCE_NO:
12676 return "MIPS_DELTA_INSTANCE_NO";
12677 case DT_MIPS_DELTA_RELOC:
12678 return "MIPS_DELTA_RELOC";
12679 case DT_MIPS_DELTA_RELOC_NO:
12680 return "MIPS_DELTA_RELOC_NO";
12681 case DT_MIPS_DELTA_SYM:
12682 return "MIPS_DELTA_SYM";
12683 case DT_MIPS_DELTA_SYM_NO:
12684 return "MIPS_DELTA_SYM_NO";
12685 case DT_MIPS_DELTA_CLASSSYM:
12686 return "MIPS_DELTA_CLASSSYM";
12687 case DT_MIPS_DELTA_CLASSSYM_NO:
12688 return "MIPS_DELTA_CLASSSYM_NO";
12689 case DT_MIPS_CXX_FLAGS:
12690 return "MIPS_CXX_FLAGS";
12691 case DT_MIPS_PIXIE_INIT:
12692 return "MIPS_PIXIE_INIT";
12693 case DT_MIPS_SYMBOL_LIB:
12694 return "MIPS_SYMBOL_LIB";
12695 case DT_MIPS_LOCALPAGE_GOTIDX:
12696 return "MIPS_LOCALPAGE_GOTIDX";
12697 case DT_MIPS_LOCAL_GOTIDX:
12698 return "MIPS_LOCAL_GOTIDX";
12699 case DT_MIPS_HIDDEN_GOTIDX:
12700 return "MIPS_HIDDEN_GOTIDX";
12701 case DT_MIPS_PROTECTED_GOTIDX:
12702 return "MIPS_PROTECTED_GOT_IDX";
12703 case DT_MIPS_OPTIONS:
12704 return "MIPS_OPTIONS";
12705 case DT_MIPS_INTERFACE:
12706 return "MIPS_INTERFACE";
12707 case DT_MIPS_DYNSTR_ALIGN:
12708 return "DT_MIPS_DYNSTR_ALIGN";
12709 case DT_MIPS_INTERFACE_SIZE:
12710 return "DT_MIPS_INTERFACE_SIZE";
12711 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12712 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12713 case DT_MIPS_PERF_SUFFIX:
12714 return "DT_MIPS_PERF_SUFFIX";
12715 case DT_MIPS_COMPACT_SIZE:
12716 return "DT_MIPS_COMPACT_SIZE";
12717 case DT_MIPS_GP_VALUE:
12718 return "DT_MIPS_GP_VALUE";
12719 case DT_MIPS_AUX_DYNAMIC:
12720 return "DT_MIPS_AUX_DYNAMIC";
12721 case DT_MIPS_PLTGOT:
12722 return "DT_MIPS_PLTGOT";
12723 case DT_MIPS_RWPLT:
12724 return "DT_MIPS_RWPLT";
12728 bfd_boolean
12729 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
12731 FILE *file = ptr;
12733 BFD_ASSERT (abfd != NULL && ptr != NULL);
12735 /* Print normal ELF private data. */
12736 _bfd_elf_print_private_bfd_data (abfd, ptr);
12738 /* xgettext:c-format */
12739 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12741 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12742 fprintf (file, _(" [abi=O32]"));
12743 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12744 fprintf (file, _(" [abi=O64]"));
12745 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12746 fprintf (file, _(" [abi=EABI32]"));
12747 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12748 fprintf (file, _(" [abi=EABI64]"));
12749 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12750 fprintf (file, _(" [abi unknown]"));
12751 else if (ABI_N32_P (abfd))
12752 fprintf (file, _(" [abi=N32]"));
12753 else if (ABI_64_P (abfd))
12754 fprintf (file, _(" [abi=64]"));
12755 else
12756 fprintf (file, _(" [no abi set]"));
12758 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
12759 fprintf (file, " [mips1]");
12760 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
12761 fprintf (file, " [mips2]");
12762 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
12763 fprintf (file, " [mips3]");
12764 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
12765 fprintf (file, " [mips4]");
12766 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
12767 fprintf (file, " [mips5]");
12768 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
12769 fprintf (file, " [mips32]");
12770 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
12771 fprintf (file, " [mips64]");
12772 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
12773 fprintf (file, " [mips32r2]");
12774 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
12775 fprintf (file, " [mips64r2]");
12776 else
12777 fprintf (file, _(" [unknown ISA]"));
12779 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
12780 fprintf (file, " [mdmx]");
12782 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
12783 fprintf (file, " [mips16]");
12785 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
12786 fprintf (file, " [32bitmode]");
12787 else
12788 fprintf (file, _(" [not 32bitmode]"));
12790 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
12791 fprintf (file, " [noreorder]");
12793 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
12794 fprintf (file, " [PIC]");
12796 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
12797 fprintf (file, " [CPIC]");
12799 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
12800 fprintf (file, " [XGOT]");
12802 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
12803 fprintf (file, " [UCODE]");
12805 fputc ('\n', file);
12807 return TRUE;
12810 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12812 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12813 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12814 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12815 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12816 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12817 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12818 { NULL, 0, 0, 0, 0 }
12821 /* Merge non visibility st_other attributes. Ensure that the
12822 STO_OPTIONAL flag is copied into h->other, even if this is not a
12823 definiton of the symbol. */
12824 void
12825 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12826 const Elf_Internal_Sym *isym,
12827 bfd_boolean definition,
12828 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12830 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12832 unsigned char other;
12834 other = (definition ? isym->st_other : h->other);
12835 other &= ~ELF_ST_VISIBILITY (-1);
12836 h->other = other | ELF_ST_VISIBILITY (h->other);
12839 if (!definition
12840 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12841 h->other |= STO_OPTIONAL;
12844 /* Decide whether an undefined symbol is special and can be ignored.
12845 This is the case for OPTIONAL symbols on IRIX. */
12846 bfd_boolean
12847 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12849 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12852 bfd_boolean
12853 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12855 return (sym->st_shndx == SHN_COMMON
12856 || sym->st_shndx == SHN_MIPS_ACOMMON
12857 || sym->st_shndx == SHN_MIPS_SCOMMON);
12860 /* Return address for Ith PLT stub in section PLT, for relocation REL
12861 or (bfd_vma) -1 if it should not be included. */
12863 bfd_vma
12864 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12865 const arelent *rel ATTRIBUTE_UNUSED)
12867 return (plt->vma
12868 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12869 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12872 void
12873 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12875 struct mips_elf_link_hash_table *htab;
12876 Elf_Internal_Ehdr *i_ehdrp;
12878 i_ehdrp = elf_elfheader (abfd);
12879 if (link_info)
12881 htab = mips_elf_hash_table (link_info);
12882 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
12883 i_ehdrp->e_ident[EI_ABIVERSION] = 1;