bfd/
[binutils.git] / bfd / elfxx-mips.c
blob8fc8b7bd6fae07b0cd197085300b8993b6ac46ef
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 .got slots used for TLS. */
149 unsigned int tls_gotno;
150 /* The first unused TLS .got entry. Used only during
151 mips_elf_initialize_tls_index. */
152 unsigned int tls_assigned_gotno;
153 /* The number of local .got entries, eventually including page entries. */
154 unsigned int local_gotno;
155 /* The maximum number of page entries needed. */
156 unsigned int page_gotno;
157 /* The number of local .got entries we have used. */
158 unsigned int assigned_gotno;
159 /* A hash table holding members of the got. */
160 struct htab *got_entries;
161 /* A hash table of mips_got_page_entry structures. */
162 struct htab *got_page_entries;
163 /* A hash table mapping input bfds to other mips_got_info. NULL
164 unless multi-got was necessary. */
165 struct htab *bfd2got;
166 /* In multi-got links, a pointer to the next got (err, rather, most
167 of the time, it points to the previous got). */
168 struct mips_got_info *next;
169 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
170 for none, or MINUS_TWO for not yet assigned. This is needed
171 because a single-GOT link may have multiple hash table entries
172 for the LDM. It does not get initialized in multi-GOT mode. */
173 bfd_vma tls_ldm_offset;
176 /* Map an input bfd to a got in a multi-got link. */
178 struct mips_elf_bfd2got_hash {
179 bfd *bfd;
180 struct mips_got_info *g;
183 /* Structure passed when traversing the bfd2got hash table, used to
184 create and merge bfd's gots. */
186 struct mips_elf_got_per_bfd_arg
188 /* A hashtable that maps bfds to gots. */
189 htab_t bfd2got;
190 /* The output bfd. */
191 bfd *obfd;
192 /* The link information. */
193 struct bfd_link_info *info;
194 /* A pointer to the primary got, i.e., the one that's going to get
195 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
196 DT_MIPS_GOTSYM. */
197 struct mips_got_info *primary;
198 /* A non-primary got we're trying to merge with other input bfd's
199 gots. */
200 struct mips_got_info *current;
201 /* The maximum number of got entries that can be addressed with a
202 16-bit offset. */
203 unsigned int max_count;
204 /* The maximum number of page entries needed by each got. */
205 unsigned int max_pages;
206 /* The total number of global entries which will live in the
207 primary got and be automatically relocated. This includes
208 those not referenced by the primary GOT but included in
209 the "master" GOT. */
210 unsigned int global_count;
213 /* Another structure used to pass arguments for got entries traversal. */
215 struct mips_elf_set_global_got_offset_arg
217 struct mips_got_info *g;
218 int value;
219 unsigned int needed_relocs;
220 struct bfd_link_info *info;
223 /* A structure used to count TLS relocations or GOT entries, for GOT
224 entry or ELF symbol table traversal. */
226 struct mips_elf_count_tls_arg
228 struct bfd_link_info *info;
229 unsigned int needed;
232 struct _mips_elf_section_data
234 struct bfd_elf_section_data elf;
235 union
237 bfd_byte *tdata;
238 } u;
241 #define mips_elf_section_data(sec) \
242 ((struct _mips_elf_section_data *) elf_section_data (sec))
244 /* The ABI says that every symbol used by dynamic relocations must have
245 a global GOT entry. Among other things, this provides the dynamic
246 linker with a free, directly-indexed cache. The GOT can therefore
247 contain symbols that are not referenced by GOT relocations themselves
248 (in other words, it may have symbols that are not referenced by things
249 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
251 GOT relocations are less likely to overflow if we put the associated
252 GOT entries towards the beginning. We therefore divide the global
253 GOT entries into two areas: "normal" and "reloc-only". Entries in
254 the first area can be used for both dynamic relocations and GP-relative
255 accesses, while those in the "reloc-only" area are for dynamic
256 relocations only.
258 These GGA_* ("Global GOT Area") values are organised so that lower
259 values are more general than higher values. Also, non-GGA_NONE
260 values are ordered by the position of the area in the GOT. */
261 #define GGA_NORMAL 0
262 #define GGA_RELOC_ONLY 1
263 #define GGA_NONE 2
265 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
266 the dynamic symbols. */
268 struct mips_elf_hash_sort_data
270 /* The symbol in the global GOT with the lowest dynamic symbol table
271 index. */
272 struct elf_link_hash_entry *low;
273 /* The least dynamic symbol table index corresponding to a non-TLS
274 symbol with a GOT entry. */
275 long min_got_dynindx;
276 /* The greatest dynamic symbol table index corresponding to a symbol
277 with a GOT entry that is not referenced (e.g., a dynamic symbol
278 with dynamic relocations pointing to it from non-primary GOTs). */
279 long max_unref_got_dynindx;
280 /* The greatest dynamic symbol table index not corresponding to a
281 symbol without a GOT entry. */
282 long max_non_got_dynindx;
285 /* The MIPS ELF linker needs additional information for each symbol in
286 the global hash table. */
288 struct mips_elf_link_hash_entry
290 struct elf_link_hash_entry root;
292 /* External symbol information. */
293 EXTR esym;
295 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
296 this symbol. */
297 unsigned int possibly_dynamic_relocs;
299 /* If there is a stub that 32 bit functions should use to call this
300 16 bit function, this points to the section containing the stub. */
301 asection *fn_stub;
303 /* If there is a stub that 16 bit functions should use to call this
304 32 bit function, this points to the section containing the stub. */
305 asection *call_stub;
307 /* This is like the call_stub field, but it is used if the function
308 being called returns a floating point value. */
309 asection *call_fp_stub;
311 #define GOT_NORMAL 0
312 #define GOT_TLS_GD 1
313 #define GOT_TLS_LDM 2
314 #define GOT_TLS_IE 4
315 #define GOT_TLS_OFFSET_DONE 0x40
316 #define GOT_TLS_DONE 0x80
317 unsigned char tls_type;
319 /* This is only used in single-GOT mode; in multi-GOT mode there
320 is one mips_got_entry per GOT entry, so the offset is stored
321 there. In single-GOT mode there may be many mips_got_entry
322 structures all referring to the same GOT slot. It might be
323 possible to use root.got.offset instead, but that field is
324 overloaded already. */
325 bfd_vma tls_got_offset;
327 /* The highest GGA_* value that satisfies all references to this symbol. */
328 unsigned int global_got_area : 2;
330 /* True if one of the relocations described by possibly_dynamic_relocs
331 is against a readonly section. */
332 unsigned int readonly_reloc : 1;
334 /* True if we must not create a .MIPS.stubs entry for this symbol.
335 This is set, for example, if there are relocations related to
336 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
337 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
338 unsigned int no_fn_stub : 1;
340 /* Whether we need the fn_stub; this is true if this symbol appears
341 in any relocs other than a 16 bit call. */
342 unsigned int need_fn_stub : 1;
344 /* Are we referenced by some kind of relocation? */
345 unsigned int is_relocation_target : 1;
347 /* Are we referenced by branch relocations? */
348 unsigned int is_branch_target : 1;
350 /* Does this symbol need a traditional MIPS lazy-binding stub
351 (as opposed to a PLT entry)? */
352 unsigned int needs_lazy_stub : 1;
355 /* MIPS ELF linker hash table. */
357 struct mips_elf_link_hash_table
359 struct elf_link_hash_table root;
360 #if 0
361 /* We no longer use this. */
362 /* String section indices for the dynamic section symbols. */
363 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
364 #endif
365 /* The number of .rtproc entries. */
366 bfd_size_type procedure_count;
367 /* The size of the .compact_rel section (if SGI_COMPAT). */
368 bfd_size_type compact_rel_size;
369 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
370 entry is set to the address of __rld_obj_head as in IRIX5. */
371 bfd_boolean use_rld_obj_head;
372 /* This is the value of the __rld_map or __rld_obj_head symbol. */
373 bfd_vma rld_value;
374 /* This is set if we see any mips16 stub sections. */
375 bfd_boolean mips16_stubs_seen;
376 /* True if we're generating code for VxWorks. */
377 bfd_boolean is_vxworks;
378 /* True if we already reported the small-data section overflow. */
379 bfd_boolean small_data_overflow_reported;
380 /* Shortcuts to some dynamic sections, or NULL if they are not
381 being used. */
382 asection *srelbss;
383 asection *sdynbss;
384 asection *srelplt;
385 asection *srelplt2;
386 asection *sgotplt;
387 asection *splt;
388 asection *sstubs;
389 asection *sgot;
390 /* The master GOT information. */
391 struct mips_got_info *got_info;
392 /* The size of the PLT header in bytes (VxWorks only). */
393 bfd_vma plt_header_size;
394 /* The size of a PLT entry in bytes (VxWorks only). */
395 bfd_vma plt_entry_size;
396 /* The number of functions that need a lazy-binding stub. */
397 bfd_vma lazy_stub_count;
398 /* The size of a function stub entry in bytes. */
399 bfd_vma function_stub_size;
402 #define TLS_RELOC_P(r_type) \
403 (r_type == R_MIPS_TLS_DTPMOD32 \
404 || r_type == R_MIPS_TLS_DTPMOD64 \
405 || r_type == R_MIPS_TLS_DTPREL32 \
406 || r_type == R_MIPS_TLS_DTPREL64 \
407 || r_type == R_MIPS_TLS_GD \
408 || r_type == R_MIPS_TLS_LDM \
409 || r_type == R_MIPS_TLS_DTPREL_HI16 \
410 || r_type == R_MIPS_TLS_DTPREL_LO16 \
411 || r_type == R_MIPS_TLS_GOTTPREL \
412 || r_type == R_MIPS_TLS_TPREL32 \
413 || r_type == R_MIPS_TLS_TPREL64 \
414 || r_type == R_MIPS_TLS_TPREL_HI16 \
415 || r_type == R_MIPS_TLS_TPREL_LO16)
417 /* Structure used to pass information to mips_elf_output_extsym. */
419 struct extsym_info
421 bfd *abfd;
422 struct bfd_link_info *info;
423 struct ecoff_debug_info *debug;
424 const struct ecoff_debug_swap *swap;
425 bfd_boolean failed;
428 /* The names of the runtime procedure table symbols used on IRIX5. */
430 static const char * const mips_elf_dynsym_rtproc_names[] =
432 "_procedure_table",
433 "_procedure_string_table",
434 "_procedure_table_size",
435 NULL
438 /* These structures are used to generate the .compact_rel section on
439 IRIX5. */
441 typedef struct
443 unsigned long id1; /* Always one? */
444 unsigned long num; /* Number of compact relocation entries. */
445 unsigned long id2; /* Always two? */
446 unsigned long offset; /* The file offset of the first relocation. */
447 unsigned long reserved0; /* Zero? */
448 unsigned long reserved1; /* Zero? */
449 } Elf32_compact_rel;
451 typedef struct
453 bfd_byte id1[4];
454 bfd_byte num[4];
455 bfd_byte id2[4];
456 bfd_byte offset[4];
457 bfd_byte reserved0[4];
458 bfd_byte reserved1[4];
459 } Elf32_External_compact_rel;
461 typedef struct
463 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
464 unsigned int rtype : 4; /* Relocation types. See below. */
465 unsigned int dist2to : 8;
466 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
467 unsigned long konst; /* KONST field. See below. */
468 unsigned long vaddr; /* VADDR to be relocated. */
469 } Elf32_crinfo;
471 typedef struct
473 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
474 unsigned int rtype : 4; /* Relocation types. See below. */
475 unsigned int dist2to : 8;
476 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
477 unsigned long konst; /* KONST field. See below. */
478 } Elf32_crinfo2;
480 typedef struct
482 bfd_byte info[4];
483 bfd_byte konst[4];
484 bfd_byte vaddr[4];
485 } Elf32_External_crinfo;
487 typedef struct
489 bfd_byte info[4];
490 bfd_byte konst[4];
491 } Elf32_External_crinfo2;
493 /* These are the constants used to swap the bitfields in a crinfo. */
495 #define CRINFO_CTYPE (0x1)
496 #define CRINFO_CTYPE_SH (31)
497 #define CRINFO_RTYPE (0xf)
498 #define CRINFO_RTYPE_SH (27)
499 #define CRINFO_DIST2TO (0xff)
500 #define CRINFO_DIST2TO_SH (19)
501 #define CRINFO_RELVADDR (0x7ffff)
502 #define CRINFO_RELVADDR_SH (0)
504 /* A compact relocation info has long (3 words) or short (2 words)
505 formats. A short format doesn't have VADDR field and relvaddr
506 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
507 #define CRF_MIPS_LONG 1
508 #define CRF_MIPS_SHORT 0
510 /* There are 4 types of compact relocation at least. The value KONST
511 has different meaning for each type:
513 (type) (konst)
514 CT_MIPS_REL32 Address in data
515 CT_MIPS_WORD Address in word (XXX)
516 CT_MIPS_GPHI_LO GP - vaddr
517 CT_MIPS_JMPAD Address to jump
520 #define CRT_MIPS_REL32 0xa
521 #define CRT_MIPS_WORD 0xb
522 #define CRT_MIPS_GPHI_LO 0xc
523 #define CRT_MIPS_JMPAD 0xd
525 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
526 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
527 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
528 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
530 /* The structure of the runtime procedure descriptor created by the
531 loader for use by the static exception system. */
533 typedef struct runtime_pdr {
534 bfd_vma adr; /* Memory address of start of procedure. */
535 long regmask; /* Save register mask. */
536 long regoffset; /* Save register offset. */
537 long fregmask; /* Save floating point register mask. */
538 long fregoffset; /* Save floating point register offset. */
539 long frameoffset; /* Frame size. */
540 short framereg; /* Frame pointer register. */
541 short pcreg; /* Offset or reg of return pc. */
542 long irpss; /* Index into the runtime string table. */
543 long reserved;
544 struct exception_info *exception_info;/* Pointer to exception array. */
545 } RPDR, *pRPDR;
546 #define cbRPDR sizeof (RPDR)
547 #define rpdNil ((pRPDR) 0)
549 static struct mips_got_entry *mips_elf_create_local_got_entry
550 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
551 struct mips_elf_link_hash_entry *, int);
552 static bfd_boolean mips_elf_sort_hash_table_f
553 (struct mips_elf_link_hash_entry *, void *);
554 static bfd_vma mips_elf_high
555 (bfd_vma);
556 static bfd_boolean mips_elf_create_dynamic_relocation
557 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
558 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
559 bfd_vma *, asection *);
560 static hashval_t mips_elf_got_entry_hash
561 (const void *);
562 static bfd_vma mips_elf_adjust_gp
563 (bfd *, struct mips_got_info *, bfd *);
564 static struct mips_got_info *mips_elf_got_for_ibfd
565 (struct mips_got_info *, bfd *);
567 /* This will be used when we sort the dynamic relocation records. */
568 static bfd *reldyn_sorting_bfd;
570 /* Nonzero if ABFD is using the N32 ABI. */
571 #define ABI_N32_P(abfd) \
572 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
574 /* Nonzero if ABFD is using the N64 ABI. */
575 #define ABI_64_P(abfd) \
576 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
578 /* Nonzero if ABFD is using NewABI conventions. */
579 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
581 /* The IRIX compatibility level we are striving for. */
582 #define IRIX_COMPAT(abfd) \
583 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
585 /* Whether we are trying to be compatible with IRIX at all. */
586 #define SGI_COMPAT(abfd) \
587 (IRIX_COMPAT (abfd) != ict_none)
589 /* The name of the options section. */
590 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
591 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
593 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
594 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
595 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
596 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
598 /* Whether the section is readonly. */
599 #define MIPS_ELF_READONLY_SECTION(sec) \
600 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
601 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
603 /* The name of the stub section. */
604 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
606 /* The size of an external REL relocation. */
607 #define MIPS_ELF_REL_SIZE(abfd) \
608 (get_elf_backend_data (abfd)->s->sizeof_rel)
610 /* The size of an external RELA relocation. */
611 #define MIPS_ELF_RELA_SIZE(abfd) \
612 (get_elf_backend_data (abfd)->s->sizeof_rela)
614 /* The size of an external dynamic table entry. */
615 #define MIPS_ELF_DYN_SIZE(abfd) \
616 (get_elf_backend_data (abfd)->s->sizeof_dyn)
618 /* The size of a GOT entry. */
619 #define MIPS_ELF_GOT_SIZE(abfd) \
620 (get_elf_backend_data (abfd)->s->arch_size / 8)
622 /* The size of a symbol-table entry. */
623 #define MIPS_ELF_SYM_SIZE(abfd) \
624 (get_elf_backend_data (abfd)->s->sizeof_sym)
626 /* The default alignment for sections, as a power of two. */
627 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
628 (get_elf_backend_data (abfd)->s->log_file_align)
630 /* Get word-sized data. */
631 #define MIPS_ELF_GET_WORD(abfd, ptr) \
632 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
634 /* Put out word-sized data. */
635 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
636 (ABI_64_P (abfd) \
637 ? bfd_put_64 (abfd, val, ptr) \
638 : bfd_put_32 (abfd, val, ptr))
640 /* Add a dynamic symbol table-entry. */
641 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
642 _bfd_elf_add_dynamic_entry (info, tag, val)
644 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
645 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
647 /* Determine whether the internal relocation of index REL_IDX is REL
648 (zero) or RELA (non-zero). The assumption is that, if there are
649 two relocation sections for this section, one of them is REL and
650 the other is RELA. If the index of the relocation we're testing is
651 in range for the first relocation section, check that the external
652 relocation size is that for RELA. It is also assumed that, if
653 rel_idx is not in range for the first section, and this first
654 section contains REL relocs, then the relocation is in the second
655 section, that is RELA. */
656 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
657 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
658 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
659 > (bfd_vma)(rel_idx)) \
660 == (elf_section_data (sec)->rel_hdr.sh_entsize \
661 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
662 : sizeof (Elf32_External_Rela))))
664 /* The name of the dynamic relocation section. */
665 #define MIPS_ELF_REL_DYN_NAME(INFO) \
666 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
668 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
669 from smaller values. Start with zero, widen, *then* decrement. */
670 #define MINUS_ONE (((bfd_vma)0) - 1)
671 #define MINUS_TWO (((bfd_vma)0) - 2)
673 /* The number of local .got entries we reserve. */
674 #define MIPS_RESERVED_GOTNO(INFO) \
675 (mips_elf_hash_table (INFO)->is_vxworks ? 3 : 2)
677 /* The value to write into got[1] for SVR4 targets, to identify it is
678 a GNU object. The dynamic linker can then use got[1] to store the
679 module pointer. */
680 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
681 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
683 /* The offset of $gp from the beginning of the .got section. */
684 #define ELF_MIPS_GP_OFFSET(INFO) \
685 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
687 /* The maximum size of the GOT for it to be addressable using 16-bit
688 offsets from $gp. */
689 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
691 /* Instructions which appear in a stub. */
692 #define STUB_LW(abfd) \
693 ((ABI_64_P (abfd) \
694 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
695 : 0x8f998010)) /* lw t9,0x8010(gp) */
696 #define STUB_MOVE(abfd) \
697 ((ABI_64_P (abfd) \
698 ? 0x03e0782d /* daddu t7,ra */ \
699 : 0x03e07821)) /* addu t7,ra */
700 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
701 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
702 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
703 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
704 #define STUB_LI16S(abfd, VAL) \
705 ((ABI_64_P (abfd) \
706 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
707 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
709 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
710 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
712 /* The name of the dynamic interpreter. This is put in the .interp
713 section. */
715 #define ELF_DYNAMIC_INTERPRETER(abfd) \
716 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
717 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
718 : "/usr/lib/libc.so.1")
720 #ifdef BFD64
721 #define MNAME(bfd,pre,pos) \
722 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
723 #define ELF_R_SYM(bfd, i) \
724 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
725 #define ELF_R_TYPE(bfd, i) \
726 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
727 #define ELF_R_INFO(bfd, s, t) \
728 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
729 #else
730 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
731 #define ELF_R_SYM(bfd, i) \
732 (ELF32_R_SYM (i))
733 #define ELF_R_TYPE(bfd, i) \
734 (ELF32_R_TYPE (i))
735 #define ELF_R_INFO(bfd, s, t) \
736 (ELF32_R_INFO (s, t))
737 #endif
739 /* The mips16 compiler uses a couple of special sections to handle
740 floating point arguments.
742 Section names that look like .mips16.fn.FNNAME contain stubs that
743 copy floating point arguments from the fp regs to the gp regs and
744 then jump to FNNAME. If any 32 bit function calls FNNAME, the
745 call should be redirected to the stub instead. If no 32 bit
746 function calls FNNAME, the stub should be discarded. We need to
747 consider any reference to the function, not just a call, because
748 if the address of the function is taken we will need the stub,
749 since the address might be passed to a 32 bit function.
751 Section names that look like .mips16.call.FNNAME contain stubs
752 that copy floating point arguments from the gp regs to the fp
753 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
754 then any 16 bit function that calls FNNAME should be redirected
755 to the stub instead. If FNNAME is not a 32 bit function, the
756 stub should be discarded.
758 .mips16.call.fp.FNNAME sections are similar, but contain stubs
759 which call FNNAME and then copy the return value from the fp regs
760 to the gp regs. These stubs store the return value in $18 while
761 calling FNNAME; any function which might call one of these stubs
762 must arrange to save $18 around the call. (This case is not
763 needed for 32 bit functions that call 16 bit functions, because
764 16 bit functions always return floating point values in both
765 $f0/$f1 and $2/$3.)
767 Note that in all cases FNNAME might be defined statically.
768 Therefore, FNNAME is not used literally. Instead, the relocation
769 information will indicate which symbol the section is for.
771 We record any stubs that we find in the symbol table. */
773 #define FN_STUB ".mips16.fn."
774 #define CALL_STUB ".mips16.call."
775 #define CALL_FP_STUB ".mips16.call.fp."
777 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
778 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
779 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
781 /* The format of the first PLT entry in a VxWorks executable. */
782 static const bfd_vma mips_vxworks_exec_plt0_entry[] = {
783 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
784 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
785 0x8f390008, /* lw t9, 8(t9) */
786 0x00000000, /* nop */
787 0x03200008, /* jr t9 */
788 0x00000000 /* nop */
791 /* The format of subsequent PLT entries. */
792 static const bfd_vma mips_vxworks_exec_plt_entry[] = {
793 0x10000000, /* b .PLT_resolver */
794 0x24180000, /* li t8, <pltindex> */
795 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
796 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
797 0x8f390000, /* lw t9, 0(t9) */
798 0x00000000, /* nop */
799 0x03200008, /* jr t9 */
800 0x00000000 /* nop */
803 /* The format of the first PLT entry in a VxWorks shared object. */
804 static const bfd_vma mips_vxworks_shared_plt0_entry[] = {
805 0x8f990008, /* lw t9, 8(gp) */
806 0x00000000, /* nop */
807 0x03200008, /* jr t9 */
808 0x00000000, /* nop */
809 0x00000000, /* nop */
810 0x00000000 /* nop */
813 /* The format of subsequent PLT entries. */
814 static const bfd_vma mips_vxworks_shared_plt_entry[] = {
815 0x10000000, /* b .PLT_resolver */
816 0x24180000 /* li t8, <pltindex> */
819 /* Look up an entry in a MIPS ELF linker hash table. */
821 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
822 ((struct mips_elf_link_hash_entry *) \
823 elf_link_hash_lookup (&(table)->root, (string), (create), \
824 (copy), (follow)))
826 /* Traverse a MIPS ELF linker hash table. */
828 #define mips_elf_link_hash_traverse(table, func, info) \
829 (elf_link_hash_traverse \
830 (&(table)->root, \
831 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
832 (info)))
834 /* Get the MIPS ELF linker hash table from a link_info structure. */
836 #define mips_elf_hash_table(p) \
837 ((struct mips_elf_link_hash_table *) ((p)->hash))
839 /* Find the base offsets for thread-local storage in this object,
840 for GD/LD and IE/LE respectively. */
842 #define TP_OFFSET 0x7000
843 #define DTP_OFFSET 0x8000
845 static bfd_vma
846 dtprel_base (struct bfd_link_info *info)
848 /* If tls_sec is NULL, we should have signalled an error already. */
849 if (elf_hash_table (info)->tls_sec == NULL)
850 return 0;
851 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
854 static bfd_vma
855 tprel_base (struct bfd_link_info *info)
857 /* If tls_sec is NULL, we should have signalled an error already. */
858 if (elf_hash_table (info)->tls_sec == NULL)
859 return 0;
860 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
863 /* Create an entry in a MIPS ELF linker hash table. */
865 static struct bfd_hash_entry *
866 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
867 struct bfd_hash_table *table, const char *string)
869 struct mips_elf_link_hash_entry *ret =
870 (struct mips_elf_link_hash_entry *) entry;
872 /* Allocate the structure if it has not already been allocated by a
873 subclass. */
874 if (ret == NULL)
875 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
876 if (ret == NULL)
877 return (struct bfd_hash_entry *) ret;
879 /* Call the allocation method of the superclass. */
880 ret = ((struct mips_elf_link_hash_entry *)
881 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
882 table, string));
883 if (ret != NULL)
885 /* Set local fields. */
886 memset (&ret->esym, 0, sizeof (EXTR));
887 /* We use -2 as a marker to indicate that the information has
888 not been set. -1 means there is no associated ifd. */
889 ret->esym.ifd = -2;
890 ret->possibly_dynamic_relocs = 0;
891 ret->fn_stub = NULL;
892 ret->call_stub = NULL;
893 ret->call_fp_stub = NULL;
894 ret->tls_type = GOT_NORMAL;
895 ret->global_got_area = GGA_NONE;
896 ret->readonly_reloc = FALSE;
897 ret->no_fn_stub = FALSE;
898 ret->need_fn_stub = FALSE;
899 ret->is_relocation_target = FALSE;
900 ret->is_branch_target = FALSE;
901 ret->needs_lazy_stub = FALSE;
904 return (struct bfd_hash_entry *) ret;
907 bfd_boolean
908 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
910 if (!sec->used_by_bfd)
912 struct _mips_elf_section_data *sdata;
913 bfd_size_type amt = sizeof (*sdata);
915 sdata = bfd_zalloc (abfd, amt);
916 if (sdata == NULL)
917 return FALSE;
918 sec->used_by_bfd = sdata;
921 return _bfd_elf_new_section_hook (abfd, sec);
924 /* Read ECOFF debugging information from a .mdebug section into a
925 ecoff_debug_info structure. */
927 bfd_boolean
928 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
929 struct ecoff_debug_info *debug)
931 HDRR *symhdr;
932 const struct ecoff_debug_swap *swap;
933 char *ext_hdr;
935 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
936 memset (debug, 0, sizeof (*debug));
938 ext_hdr = bfd_malloc (swap->external_hdr_size);
939 if (ext_hdr == NULL && swap->external_hdr_size != 0)
940 goto error_return;
942 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
943 swap->external_hdr_size))
944 goto error_return;
946 symhdr = &debug->symbolic_header;
947 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
949 /* The symbolic header contains absolute file offsets and sizes to
950 read. */
951 #define READ(ptr, offset, count, size, type) \
952 if (symhdr->count == 0) \
953 debug->ptr = NULL; \
954 else \
956 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
957 debug->ptr = bfd_malloc (amt); \
958 if (debug->ptr == NULL) \
959 goto error_return; \
960 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
961 || bfd_bread (debug->ptr, amt, abfd) != amt) \
962 goto error_return; \
965 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
966 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
967 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
968 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
969 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
970 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
971 union aux_ext *);
972 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
973 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
974 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
975 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
976 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
977 #undef READ
979 debug->fdr = NULL;
981 return TRUE;
983 error_return:
984 if (ext_hdr != NULL)
985 free (ext_hdr);
986 if (debug->line != NULL)
987 free (debug->line);
988 if (debug->external_dnr != NULL)
989 free (debug->external_dnr);
990 if (debug->external_pdr != NULL)
991 free (debug->external_pdr);
992 if (debug->external_sym != NULL)
993 free (debug->external_sym);
994 if (debug->external_opt != NULL)
995 free (debug->external_opt);
996 if (debug->external_aux != NULL)
997 free (debug->external_aux);
998 if (debug->ss != NULL)
999 free (debug->ss);
1000 if (debug->ssext != NULL)
1001 free (debug->ssext);
1002 if (debug->external_fdr != NULL)
1003 free (debug->external_fdr);
1004 if (debug->external_rfd != NULL)
1005 free (debug->external_rfd);
1006 if (debug->external_ext != NULL)
1007 free (debug->external_ext);
1008 return FALSE;
1011 /* Swap RPDR (runtime procedure table entry) for output. */
1013 static void
1014 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1016 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1017 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1018 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1019 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1020 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1021 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1023 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1024 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1026 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1029 /* Create a runtime procedure table from the .mdebug section. */
1031 static bfd_boolean
1032 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1033 struct bfd_link_info *info, asection *s,
1034 struct ecoff_debug_info *debug)
1036 const struct ecoff_debug_swap *swap;
1037 HDRR *hdr = &debug->symbolic_header;
1038 RPDR *rpdr, *rp;
1039 struct rpdr_ext *erp;
1040 void *rtproc;
1041 struct pdr_ext *epdr;
1042 struct sym_ext *esym;
1043 char *ss, **sv;
1044 char *str;
1045 bfd_size_type size;
1046 bfd_size_type count;
1047 unsigned long sindex;
1048 unsigned long i;
1049 PDR pdr;
1050 SYMR sym;
1051 const char *no_name_func = _("static procedure (no name)");
1053 epdr = NULL;
1054 rpdr = NULL;
1055 esym = NULL;
1056 ss = NULL;
1057 sv = NULL;
1059 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1061 sindex = strlen (no_name_func) + 1;
1062 count = hdr->ipdMax;
1063 if (count > 0)
1065 size = swap->external_pdr_size;
1067 epdr = bfd_malloc (size * count);
1068 if (epdr == NULL)
1069 goto error_return;
1071 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1072 goto error_return;
1074 size = sizeof (RPDR);
1075 rp = rpdr = bfd_malloc (size * count);
1076 if (rpdr == NULL)
1077 goto error_return;
1079 size = sizeof (char *);
1080 sv = bfd_malloc (size * count);
1081 if (sv == NULL)
1082 goto error_return;
1084 count = hdr->isymMax;
1085 size = swap->external_sym_size;
1086 esym = bfd_malloc (size * count);
1087 if (esym == NULL)
1088 goto error_return;
1090 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1091 goto error_return;
1093 count = hdr->issMax;
1094 ss = bfd_malloc (count);
1095 if (ss == NULL)
1096 goto error_return;
1097 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1098 goto error_return;
1100 count = hdr->ipdMax;
1101 for (i = 0; i < (unsigned long) count; i++, rp++)
1103 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1104 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1105 rp->adr = sym.value;
1106 rp->regmask = pdr.regmask;
1107 rp->regoffset = pdr.regoffset;
1108 rp->fregmask = pdr.fregmask;
1109 rp->fregoffset = pdr.fregoffset;
1110 rp->frameoffset = pdr.frameoffset;
1111 rp->framereg = pdr.framereg;
1112 rp->pcreg = pdr.pcreg;
1113 rp->irpss = sindex;
1114 sv[i] = ss + sym.iss;
1115 sindex += strlen (sv[i]) + 1;
1119 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1120 size = BFD_ALIGN (size, 16);
1121 rtproc = bfd_alloc (abfd, size);
1122 if (rtproc == NULL)
1124 mips_elf_hash_table (info)->procedure_count = 0;
1125 goto error_return;
1128 mips_elf_hash_table (info)->procedure_count = count + 2;
1130 erp = rtproc;
1131 memset (erp, 0, sizeof (struct rpdr_ext));
1132 erp++;
1133 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1134 strcpy (str, no_name_func);
1135 str += strlen (no_name_func) + 1;
1136 for (i = 0; i < count; i++)
1138 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1139 strcpy (str, sv[i]);
1140 str += strlen (sv[i]) + 1;
1142 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1144 /* Set the size and contents of .rtproc section. */
1145 s->size = size;
1146 s->contents = rtproc;
1148 /* Skip this section later on (I don't think this currently
1149 matters, but someday it might). */
1150 s->map_head.link_order = NULL;
1152 if (epdr != NULL)
1153 free (epdr);
1154 if (rpdr != NULL)
1155 free (rpdr);
1156 if (esym != NULL)
1157 free (esym);
1158 if (ss != NULL)
1159 free (ss);
1160 if (sv != NULL)
1161 free (sv);
1163 return TRUE;
1165 error_return:
1166 if (epdr != NULL)
1167 free (epdr);
1168 if (rpdr != NULL)
1169 free (rpdr);
1170 if (esym != NULL)
1171 free (esym);
1172 if (ss != NULL)
1173 free (ss);
1174 if (sv != NULL)
1175 free (sv);
1176 return FALSE;
1179 /* We're about to redefine H. Create a symbol to represent H's
1180 current value and size, to help make the disassembly easier
1181 to read. */
1183 static bfd_boolean
1184 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1185 struct mips_elf_link_hash_entry *h,
1186 const char *prefix)
1188 struct bfd_link_hash_entry *bh;
1189 struct elf_link_hash_entry *elfh;
1190 const char *name;
1191 asection *s;
1192 bfd_vma value;
1194 /* Read the symbol's value. */
1195 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1196 || h->root.root.type == bfd_link_hash_defweak);
1197 s = h->root.root.u.def.section;
1198 value = h->root.root.u.def.value;
1200 /* Create a new symbol. */
1201 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1202 bh = NULL;
1203 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1204 BSF_LOCAL, s, value, NULL,
1205 TRUE, FALSE, &bh))
1206 return FALSE;
1208 /* Make it local and copy the other attributes from H. */
1209 elfh = (struct elf_link_hash_entry *) bh;
1210 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1211 elfh->other = h->root.other;
1212 elfh->size = h->root.size;
1213 elfh->forced_local = 1;
1214 return TRUE;
1217 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1218 function rather than to a hard-float stub. */
1220 static bfd_boolean
1221 section_allows_mips16_refs_p (asection *section)
1223 const char *name;
1225 name = bfd_get_section_name (section->owner, section);
1226 return (FN_STUB_P (name)
1227 || CALL_STUB_P (name)
1228 || CALL_FP_STUB_P (name)
1229 || strcmp (name, ".pdr") == 0);
1232 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1233 stub section of some kind. Return the R_SYMNDX of the target
1234 function, or 0 if we can't decide which function that is. */
1236 static unsigned long
1237 mips16_stub_symndx (asection *sec, const Elf_Internal_Rela *relocs,
1238 const Elf_Internal_Rela *relend)
1240 const Elf_Internal_Rela *rel;
1242 /* Trust the first R_MIPS_NONE relocation, if any. */
1243 for (rel = relocs; rel < relend; rel++)
1244 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1245 return ELF_R_SYM (sec->owner, rel->r_info);
1247 /* Otherwise trust the first relocation, whatever its kind. This is
1248 the traditional behavior. */
1249 if (relocs < relend)
1250 return ELF_R_SYM (sec->owner, relocs->r_info);
1252 return 0;
1255 /* Check the mips16 stubs for a particular symbol, and see if we can
1256 discard them. */
1258 static bfd_boolean
1259 mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h, void *data)
1261 struct bfd_link_info *info;
1263 info = (struct bfd_link_info *) data;
1264 if (h->root.root.type == bfd_link_hash_warning)
1265 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1267 /* Dynamic symbols must use the standard call interface, in case other
1268 objects try to call them. */
1269 if (h->fn_stub != NULL
1270 && h->root.dynindx != -1)
1272 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1273 h->need_fn_stub = TRUE;
1276 if (h->fn_stub != NULL
1277 && ! h->need_fn_stub)
1279 /* We don't need the fn_stub; the only references to this symbol
1280 are 16 bit calls. Clobber the size to 0 to prevent it from
1281 being included in the link. */
1282 h->fn_stub->size = 0;
1283 h->fn_stub->flags &= ~SEC_RELOC;
1284 h->fn_stub->reloc_count = 0;
1285 h->fn_stub->flags |= SEC_EXCLUDE;
1288 if (h->call_stub != NULL
1289 && ELF_ST_IS_MIPS16 (h->root.other))
1291 /* We don't need the call_stub; this is a 16 bit function, so
1292 calls from other 16 bit functions are OK. Clobber the size
1293 to 0 to prevent it from being included in the link. */
1294 h->call_stub->size = 0;
1295 h->call_stub->flags &= ~SEC_RELOC;
1296 h->call_stub->reloc_count = 0;
1297 h->call_stub->flags |= SEC_EXCLUDE;
1300 if (h->call_fp_stub != NULL
1301 && ELF_ST_IS_MIPS16 (h->root.other))
1303 /* We don't need the call_stub; this is a 16 bit function, so
1304 calls from other 16 bit functions are OK. Clobber the size
1305 to 0 to prevent it from being included in the link. */
1306 h->call_fp_stub->size = 0;
1307 h->call_fp_stub->flags &= ~SEC_RELOC;
1308 h->call_fp_stub->reloc_count = 0;
1309 h->call_fp_stub->flags |= SEC_EXCLUDE;
1312 return TRUE;
1315 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1316 Most mips16 instructions are 16 bits, but these instructions
1317 are 32 bits.
1319 The format of these instructions is:
1321 +--------------+--------------------------------+
1322 | JALX | X| Imm 20:16 | Imm 25:21 |
1323 +--------------+--------------------------------+
1324 | Immediate 15:0 |
1325 +-----------------------------------------------+
1327 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1328 Note that the immediate value in the first word is swapped.
1330 When producing a relocatable object file, R_MIPS16_26 is
1331 handled mostly like R_MIPS_26. In particular, the addend is
1332 stored as a straight 26-bit value in a 32-bit instruction.
1333 (gas makes life simpler for itself by never adjusting a
1334 R_MIPS16_26 reloc to be against a section, so the addend is
1335 always zero). However, the 32 bit instruction is stored as 2
1336 16-bit values, rather than a single 32-bit value. In a
1337 big-endian file, the result is the same; in a little-endian
1338 file, the two 16-bit halves of the 32 bit value are swapped.
1339 This is so that a disassembler can recognize the jal
1340 instruction.
1342 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1343 instruction stored as two 16-bit values. The addend A is the
1344 contents of the targ26 field. The calculation is the same as
1345 R_MIPS_26. When storing the calculated value, reorder the
1346 immediate value as shown above, and don't forget to store the
1347 value as two 16-bit values.
1349 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1350 defined as
1352 big-endian:
1353 +--------+----------------------+
1354 | | |
1355 | | targ26-16 |
1356 |31 26|25 0|
1357 +--------+----------------------+
1359 little-endian:
1360 +----------+------+-------------+
1361 | | | |
1362 | sub1 | | sub2 |
1363 |0 9|10 15|16 31|
1364 +----------+--------------------+
1365 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1366 ((sub1 << 16) | sub2)).
1368 When producing a relocatable object file, the calculation is
1369 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1370 When producing a fully linked file, the calculation is
1371 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1372 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1374 The table below lists the other MIPS16 instruction relocations.
1375 Each one is calculated in the same way as the non-MIPS16 relocation
1376 given on the right, but using the extended MIPS16 layout of 16-bit
1377 immediate fields:
1379 R_MIPS16_GPREL R_MIPS_GPREL16
1380 R_MIPS16_GOT16 R_MIPS_GOT16
1381 R_MIPS16_CALL16 R_MIPS_CALL16
1382 R_MIPS16_HI16 R_MIPS_HI16
1383 R_MIPS16_LO16 R_MIPS_LO16
1385 A typical instruction will have a format like this:
1387 +--------------+--------------------------------+
1388 | EXTEND | Imm 10:5 | Imm 15:11 |
1389 +--------------+--------------------------------+
1390 | Major | rx | ry | Imm 4:0 |
1391 +--------------+--------------------------------+
1393 EXTEND is the five bit value 11110. Major is the instruction
1394 opcode.
1396 All we need to do here is shuffle the bits appropriately.
1397 As above, the two 16-bit halves must be swapped on a
1398 little-endian system. */
1400 static inline bfd_boolean
1401 mips16_reloc_p (int r_type)
1403 switch (r_type)
1405 case R_MIPS16_26:
1406 case R_MIPS16_GPREL:
1407 case R_MIPS16_GOT16:
1408 case R_MIPS16_CALL16:
1409 case R_MIPS16_HI16:
1410 case R_MIPS16_LO16:
1411 return TRUE;
1413 default:
1414 return FALSE;
1418 static inline bfd_boolean
1419 got16_reloc_p (int r_type)
1421 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1424 static inline bfd_boolean
1425 call16_reloc_p (int r_type)
1427 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1430 static inline bfd_boolean
1431 hi16_reloc_p (int r_type)
1433 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1436 static inline bfd_boolean
1437 lo16_reloc_p (int r_type)
1439 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1442 static inline bfd_boolean
1443 mips16_call_reloc_p (int r_type)
1445 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1448 void
1449 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1450 bfd_boolean jal_shuffle, bfd_byte *data)
1452 bfd_vma extend, insn, val;
1454 if (!mips16_reloc_p (r_type))
1455 return;
1457 /* Pick up the mips16 extend instruction and the real instruction. */
1458 extend = bfd_get_16 (abfd, data);
1459 insn = bfd_get_16 (abfd, data + 2);
1460 if (r_type == R_MIPS16_26)
1462 if (jal_shuffle)
1463 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1464 | ((extend & 0x1f) << 21) | insn;
1465 else
1466 val = extend << 16 | insn;
1468 else
1469 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1470 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1471 bfd_put_32 (abfd, val, data);
1474 void
1475 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1476 bfd_boolean jal_shuffle, bfd_byte *data)
1478 bfd_vma extend, insn, val;
1480 if (!mips16_reloc_p (r_type))
1481 return;
1483 val = bfd_get_32 (abfd, data);
1484 if (r_type == R_MIPS16_26)
1486 if (jal_shuffle)
1488 insn = val & 0xffff;
1489 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1490 | ((val >> 21) & 0x1f);
1492 else
1494 insn = val & 0xffff;
1495 extend = val >> 16;
1498 else
1500 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1501 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1503 bfd_put_16 (abfd, insn, data + 2);
1504 bfd_put_16 (abfd, extend, data);
1507 bfd_reloc_status_type
1508 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1509 arelent *reloc_entry, asection *input_section,
1510 bfd_boolean relocatable, void *data, bfd_vma gp)
1512 bfd_vma relocation;
1513 bfd_signed_vma val;
1514 bfd_reloc_status_type status;
1516 if (bfd_is_com_section (symbol->section))
1517 relocation = 0;
1518 else
1519 relocation = symbol->value;
1521 relocation += symbol->section->output_section->vma;
1522 relocation += symbol->section->output_offset;
1524 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1525 return bfd_reloc_outofrange;
1527 /* Set val to the offset into the section or symbol. */
1528 val = reloc_entry->addend;
1530 _bfd_mips_elf_sign_extend (val, 16);
1532 /* Adjust val for the final section location and GP value. If we
1533 are producing relocatable output, we don't want to do this for
1534 an external symbol. */
1535 if (! relocatable
1536 || (symbol->flags & BSF_SECTION_SYM) != 0)
1537 val += relocation - gp;
1539 if (reloc_entry->howto->partial_inplace)
1541 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1542 (bfd_byte *) data
1543 + reloc_entry->address);
1544 if (status != bfd_reloc_ok)
1545 return status;
1547 else
1548 reloc_entry->addend = val;
1550 if (relocatable)
1551 reloc_entry->address += input_section->output_offset;
1553 return bfd_reloc_ok;
1556 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1557 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1558 that contains the relocation field and DATA points to the start of
1559 INPUT_SECTION. */
1561 struct mips_hi16
1563 struct mips_hi16 *next;
1564 bfd_byte *data;
1565 asection *input_section;
1566 arelent rel;
1569 /* FIXME: This should not be a static variable. */
1571 static struct mips_hi16 *mips_hi16_list;
1573 /* A howto special_function for REL *HI16 relocations. We can only
1574 calculate the correct value once we've seen the partnering
1575 *LO16 relocation, so just save the information for later.
1577 The ABI requires that the *LO16 immediately follow the *HI16.
1578 However, as a GNU extension, we permit an arbitrary number of
1579 *HI16s to be associated with a single *LO16. This significantly
1580 simplies the relocation handling in gcc. */
1582 bfd_reloc_status_type
1583 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1584 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1585 asection *input_section, bfd *output_bfd,
1586 char **error_message ATTRIBUTE_UNUSED)
1588 struct mips_hi16 *n;
1590 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1591 return bfd_reloc_outofrange;
1593 n = bfd_malloc (sizeof *n);
1594 if (n == NULL)
1595 return bfd_reloc_outofrange;
1597 n->next = mips_hi16_list;
1598 n->data = data;
1599 n->input_section = input_section;
1600 n->rel = *reloc_entry;
1601 mips_hi16_list = n;
1603 if (output_bfd != NULL)
1604 reloc_entry->address += input_section->output_offset;
1606 return bfd_reloc_ok;
1609 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
1610 like any other 16-bit relocation when applied to global symbols, but is
1611 treated in the same as R_MIPS_HI16 when applied to local symbols. */
1613 bfd_reloc_status_type
1614 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1615 void *data, asection *input_section,
1616 bfd *output_bfd, char **error_message)
1618 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1619 || bfd_is_und_section (bfd_get_section (symbol))
1620 || bfd_is_com_section (bfd_get_section (symbol)))
1621 /* The relocation is against a global symbol. */
1622 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1623 input_section, output_bfd,
1624 error_message);
1626 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1627 input_section, output_bfd, error_message);
1630 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
1631 is a straightforward 16 bit inplace relocation, but we must deal with
1632 any partnering high-part relocations as well. */
1634 bfd_reloc_status_type
1635 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1636 void *data, asection *input_section,
1637 bfd *output_bfd, char **error_message)
1639 bfd_vma vallo;
1640 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1642 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1643 return bfd_reloc_outofrange;
1645 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1646 location);
1647 vallo = bfd_get_32 (abfd, location);
1648 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1649 location);
1651 while (mips_hi16_list != NULL)
1653 bfd_reloc_status_type ret;
1654 struct mips_hi16 *hi;
1656 hi = mips_hi16_list;
1658 /* R_MIPS*_GOT16 relocations are something of a special case. We
1659 want to install the addend in the same way as for a R_MIPS*_HI16
1660 relocation (with a rightshift of 16). However, since GOT16
1661 relocations can also be used with global symbols, their howto
1662 has a rightshift of 0. */
1663 if (hi->rel.howto->type == R_MIPS_GOT16)
1664 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
1665 else if (hi->rel.howto->type == R_MIPS16_GOT16)
1666 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
1668 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
1669 carry or borrow will induce a change of +1 or -1 in the high part. */
1670 hi->rel.addend += (vallo + 0x8000) & 0xffff;
1672 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
1673 hi->input_section, output_bfd,
1674 error_message);
1675 if (ret != bfd_reloc_ok)
1676 return ret;
1678 mips_hi16_list = hi->next;
1679 free (hi);
1682 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1683 input_section, output_bfd,
1684 error_message);
1687 /* A generic howto special_function. This calculates and installs the
1688 relocation itself, thus avoiding the oft-discussed problems in
1689 bfd_perform_relocation and bfd_install_relocation. */
1691 bfd_reloc_status_type
1692 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1693 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1694 asection *input_section, bfd *output_bfd,
1695 char **error_message ATTRIBUTE_UNUSED)
1697 bfd_signed_vma val;
1698 bfd_reloc_status_type status;
1699 bfd_boolean relocatable;
1701 relocatable = (output_bfd != NULL);
1703 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1704 return bfd_reloc_outofrange;
1706 /* Build up the field adjustment in VAL. */
1707 val = 0;
1708 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
1710 /* Either we're calculating the final field value or we have a
1711 relocation against a section symbol. Add in the section's
1712 offset or address. */
1713 val += symbol->section->output_section->vma;
1714 val += symbol->section->output_offset;
1717 if (!relocatable)
1719 /* We're calculating the final field value. Add in the symbol's value
1720 and, if pc-relative, subtract the address of the field itself. */
1721 val += symbol->value;
1722 if (reloc_entry->howto->pc_relative)
1724 val -= input_section->output_section->vma;
1725 val -= input_section->output_offset;
1726 val -= reloc_entry->address;
1730 /* VAL is now the final adjustment. If we're keeping this relocation
1731 in the output file, and if the relocation uses a separate addend,
1732 we just need to add VAL to that addend. Otherwise we need to add
1733 VAL to the relocation field itself. */
1734 if (relocatable && !reloc_entry->howto->partial_inplace)
1735 reloc_entry->addend += val;
1736 else
1738 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1740 /* Add in the separate addend, if any. */
1741 val += reloc_entry->addend;
1743 /* Add VAL to the relocation field. */
1744 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1745 location);
1746 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1747 location);
1748 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1749 location);
1751 if (status != bfd_reloc_ok)
1752 return status;
1755 if (relocatable)
1756 reloc_entry->address += input_section->output_offset;
1758 return bfd_reloc_ok;
1761 /* Swap an entry in a .gptab section. Note that these routines rely
1762 on the equivalence of the two elements of the union. */
1764 static void
1765 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
1766 Elf32_gptab *in)
1768 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1769 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1772 static void
1773 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
1774 Elf32_External_gptab *ex)
1776 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1777 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1780 static void
1781 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
1782 Elf32_External_compact_rel *ex)
1784 H_PUT_32 (abfd, in->id1, ex->id1);
1785 H_PUT_32 (abfd, in->num, ex->num);
1786 H_PUT_32 (abfd, in->id2, ex->id2);
1787 H_PUT_32 (abfd, in->offset, ex->offset);
1788 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1789 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1792 static void
1793 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
1794 Elf32_External_crinfo *ex)
1796 unsigned long l;
1798 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1799 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1800 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1801 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1802 H_PUT_32 (abfd, l, ex->info);
1803 H_PUT_32 (abfd, in->konst, ex->konst);
1804 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1807 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1808 routines swap this structure in and out. They are used outside of
1809 BFD, so they are globally visible. */
1811 void
1812 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
1813 Elf32_RegInfo *in)
1815 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1816 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1817 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1818 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1819 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1820 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1823 void
1824 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
1825 Elf32_External_RegInfo *ex)
1827 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1828 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1829 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1830 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1831 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1832 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1835 /* In the 64 bit ABI, the .MIPS.options section holds register
1836 information in an Elf64_Reginfo structure. These routines swap
1837 them in and out. They are globally visible because they are used
1838 outside of BFD. These routines are here so that gas can call them
1839 without worrying about whether the 64 bit ABI has been included. */
1841 void
1842 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
1843 Elf64_Internal_RegInfo *in)
1845 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1846 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1847 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1848 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1849 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1850 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1851 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1854 void
1855 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
1856 Elf64_External_RegInfo *ex)
1858 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1859 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1860 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1861 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1862 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1863 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1864 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1867 /* Swap in an options header. */
1869 void
1870 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
1871 Elf_Internal_Options *in)
1873 in->kind = H_GET_8 (abfd, ex->kind);
1874 in->size = H_GET_8 (abfd, ex->size);
1875 in->section = H_GET_16 (abfd, ex->section);
1876 in->info = H_GET_32 (abfd, ex->info);
1879 /* Swap out an options header. */
1881 void
1882 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
1883 Elf_External_Options *ex)
1885 H_PUT_8 (abfd, in->kind, ex->kind);
1886 H_PUT_8 (abfd, in->size, ex->size);
1887 H_PUT_16 (abfd, in->section, ex->section);
1888 H_PUT_32 (abfd, in->info, ex->info);
1891 /* This function is called via qsort() to sort the dynamic relocation
1892 entries by increasing r_symndx value. */
1894 static int
1895 sort_dynamic_relocs (const void *arg1, const void *arg2)
1897 Elf_Internal_Rela int_reloc1;
1898 Elf_Internal_Rela int_reloc2;
1899 int diff;
1901 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1902 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
1904 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
1905 if (diff != 0)
1906 return diff;
1908 if (int_reloc1.r_offset < int_reloc2.r_offset)
1909 return -1;
1910 if (int_reloc1.r_offset > int_reloc2.r_offset)
1911 return 1;
1912 return 0;
1915 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
1917 static int
1918 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
1919 const void *arg2 ATTRIBUTE_UNUSED)
1921 #ifdef BFD64
1922 Elf_Internal_Rela int_reloc1[3];
1923 Elf_Internal_Rela int_reloc2[3];
1925 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1926 (reldyn_sorting_bfd, arg1, int_reloc1);
1927 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1928 (reldyn_sorting_bfd, arg2, int_reloc2);
1930 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
1931 return -1;
1932 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
1933 return 1;
1935 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
1936 return -1;
1937 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
1938 return 1;
1939 return 0;
1940 #else
1941 abort ();
1942 #endif
1946 /* This routine is used to write out ECOFF debugging external symbol
1947 information. It is called via mips_elf_link_hash_traverse. The
1948 ECOFF external symbol information must match the ELF external
1949 symbol information. Unfortunately, at this point we don't know
1950 whether a symbol is required by reloc information, so the two
1951 tables may wind up being different. We must sort out the external
1952 symbol information before we can set the final size of the .mdebug
1953 section, and we must set the size of the .mdebug section before we
1954 can relocate any sections, and we can't know which symbols are
1955 required by relocation until we relocate the sections.
1956 Fortunately, it is relatively unlikely that any symbol will be
1957 stripped but required by a reloc. In particular, it can not happen
1958 when generating a final executable. */
1960 static bfd_boolean
1961 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
1963 struct extsym_info *einfo = data;
1964 bfd_boolean strip;
1965 asection *sec, *output_section;
1967 if (h->root.root.type == bfd_link_hash_warning)
1968 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1970 if (h->root.indx == -2)
1971 strip = FALSE;
1972 else if ((h->root.def_dynamic
1973 || h->root.ref_dynamic
1974 || h->root.type == bfd_link_hash_new)
1975 && !h->root.def_regular
1976 && !h->root.ref_regular)
1977 strip = TRUE;
1978 else if (einfo->info->strip == strip_all
1979 || (einfo->info->strip == strip_some
1980 && bfd_hash_lookup (einfo->info->keep_hash,
1981 h->root.root.root.string,
1982 FALSE, FALSE) == NULL))
1983 strip = TRUE;
1984 else
1985 strip = FALSE;
1987 if (strip)
1988 return TRUE;
1990 if (h->esym.ifd == -2)
1992 h->esym.jmptbl = 0;
1993 h->esym.cobol_main = 0;
1994 h->esym.weakext = 0;
1995 h->esym.reserved = 0;
1996 h->esym.ifd = ifdNil;
1997 h->esym.asym.value = 0;
1998 h->esym.asym.st = stGlobal;
2000 if (h->root.root.type == bfd_link_hash_undefined
2001 || h->root.root.type == bfd_link_hash_undefweak)
2003 const char *name;
2005 /* Use undefined class. Also, set class and type for some
2006 special symbols. */
2007 name = h->root.root.root.string;
2008 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2009 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2011 h->esym.asym.sc = scData;
2012 h->esym.asym.st = stLabel;
2013 h->esym.asym.value = 0;
2015 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2017 h->esym.asym.sc = scAbs;
2018 h->esym.asym.st = stLabel;
2019 h->esym.asym.value =
2020 mips_elf_hash_table (einfo->info)->procedure_count;
2022 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2024 h->esym.asym.sc = scAbs;
2025 h->esym.asym.st = stLabel;
2026 h->esym.asym.value = elf_gp (einfo->abfd);
2028 else
2029 h->esym.asym.sc = scUndefined;
2031 else if (h->root.root.type != bfd_link_hash_defined
2032 && h->root.root.type != bfd_link_hash_defweak)
2033 h->esym.asym.sc = scAbs;
2034 else
2036 const char *name;
2038 sec = h->root.root.u.def.section;
2039 output_section = sec->output_section;
2041 /* When making a shared library and symbol h is the one from
2042 the another shared library, OUTPUT_SECTION may be null. */
2043 if (output_section == NULL)
2044 h->esym.asym.sc = scUndefined;
2045 else
2047 name = bfd_section_name (output_section->owner, output_section);
2049 if (strcmp (name, ".text") == 0)
2050 h->esym.asym.sc = scText;
2051 else if (strcmp (name, ".data") == 0)
2052 h->esym.asym.sc = scData;
2053 else if (strcmp (name, ".sdata") == 0)
2054 h->esym.asym.sc = scSData;
2055 else if (strcmp (name, ".rodata") == 0
2056 || strcmp (name, ".rdata") == 0)
2057 h->esym.asym.sc = scRData;
2058 else if (strcmp (name, ".bss") == 0)
2059 h->esym.asym.sc = scBss;
2060 else if (strcmp (name, ".sbss") == 0)
2061 h->esym.asym.sc = scSBss;
2062 else if (strcmp (name, ".init") == 0)
2063 h->esym.asym.sc = scInit;
2064 else if (strcmp (name, ".fini") == 0)
2065 h->esym.asym.sc = scFini;
2066 else
2067 h->esym.asym.sc = scAbs;
2071 h->esym.asym.reserved = 0;
2072 h->esym.asym.index = indexNil;
2075 if (h->root.root.type == bfd_link_hash_common)
2076 h->esym.asym.value = h->root.root.u.c.size;
2077 else if (h->root.root.type == bfd_link_hash_defined
2078 || h->root.root.type == bfd_link_hash_defweak)
2080 if (h->esym.asym.sc == scCommon)
2081 h->esym.asym.sc = scBss;
2082 else if (h->esym.asym.sc == scSCommon)
2083 h->esym.asym.sc = scSBss;
2085 sec = h->root.root.u.def.section;
2086 output_section = sec->output_section;
2087 if (output_section != NULL)
2088 h->esym.asym.value = (h->root.root.u.def.value
2089 + sec->output_offset
2090 + output_section->vma);
2091 else
2092 h->esym.asym.value = 0;
2094 else
2096 struct mips_elf_link_hash_entry *hd = h;
2098 while (hd->root.root.type == bfd_link_hash_indirect)
2099 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2101 if (hd->needs_lazy_stub)
2103 /* Set type and value for a symbol with a function stub. */
2104 h->esym.asym.st = stProc;
2105 sec = hd->root.root.u.def.section;
2106 if (sec == NULL)
2107 h->esym.asym.value = 0;
2108 else
2110 output_section = sec->output_section;
2111 if (output_section != NULL)
2112 h->esym.asym.value = (hd->root.plt.offset
2113 + sec->output_offset
2114 + output_section->vma);
2115 else
2116 h->esym.asym.value = 0;
2121 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2122 h->root.root.root.string,
2123 &h->esym))
2125 einfo->failed = TRUE;
2126 return FALSE;
2129 return TRUE;
2132 /* A comparison routine used to sort .gptab entries. */
2134 static int
2135 gptab_compare (const void *p1, const void *p2)
2137 const Elf32_gptab *a1 = p1;
2138 const Elf32_gptab *a2 = p2;
2140 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2143 /* Functions to manage the got entry hash table. */
2145 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2146 hash number. */
2148 static INLINE hashval_t
2149 mips_elf_hash_bfd_vma (bfd_vma addr)
2151 #ifdef BFD64
2152 return addr + (addr >> 32);
2153 #else
2154 return addr;
2155 #endif
2158 /* got_entries only match if they're identical, except for gotidx, so
2159 use all fields to compute the hash, and compare the appropriate
2160 union members. */
2162 static hashval_t
2163 mips_elf_got_entry_hash (const void *entry_)
2165 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2167 return entry->symndx
2168 + ((entry->tls_type & GOT_TLS_LDM) << 17)
2169 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2170 : entry->abfd->id
2171 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2172 : entry->d.h->root.root.root.hash));
2175 static int
2176 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2178 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2179 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2181 /* An LDM entry can only match another LDM entry. */
2182 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2183 return 0;
2185 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2186 && (! e1->abfd ? e1->d.address == e2->d.address
2187 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2188 : e1->d.h == e2->d.h);
2191 /* multi_got_entries are still a match in the case of global objects,
2192 even if the input bfd in which they're referenced differs, so the
2193 hash computation and compare functions are adjusted
2194 accordingly. */
2196 static hashval_t
2197 mips_elf_multi_got_entry_hash (const void *entry_)
2199 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2201 return entry->symndx
2202 + (! entry->abfd
2203 ? mips_elf_hash_bfd_vma (entry->d.address)
2204 : entry->symndx >= 0
2205 ? ((entry->tls_type & GOT_TLS_LDM)
2206 ? (GOT_TLS_LDM << 17)
2207 : (entry->abfd->id
2208 + mips_elf_hash_bfd_vma (entry->d.addend)))
2209 : entry->d.h->root.root.root.hash);
2212 static int
2213 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2215 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2216 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2218 /* Any two LDM entries match. */
2219 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2220 return 1;
2222 /* Nothing else matches an LDM entry. */
2223 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2224 return 0;
2226 return e1->symndx == e2->symndx
2227 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2228 : e1->abfd == NULL || e2->abfd == NULL
2229 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2230 : e1->d.h == e2->d.h);
2233 static hashval_t
2234 mips_got_page_entry_hash (const void *entry_)
2236 const struct mips_got_page_entry *entry;
2238 entry = (const struct mips_got_page_entry *) entry_;
2239 return entry->abfd->id + entry->symndx;
2242 static int
2243 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2245 const struct mips_got_page_entry *entry1, *entry2;
2247 entry1 = (const struct mips_got_page_entry *) entry1_;
2248 entry2 = (const struct mips_got_page_entry *) entry2_;
2249 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2252 /* Return the dynamic relocation section. If it doesn't exist, try to
2253 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2254 if creation fails. */
2256 static asection *
2257 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2259 const char *dname;
2260 asection *sreloc;
2261 bfd *dynobj;
2263 dname = MIPS_ELF_REL_DYN_NAME (info);
2264 dynobj = elf_hash_table (info)->dynobj;
2265 sreloc = bfd_get_section_by_name (dynobj, dname);
2266 if (sreloc == NULL && create_p)
2268 sreloc = bfd_make_section_with_flags (dynobj, dname,
2269 (SEC_ALLOC
2270 | SEC_LOAD
2271 | SEC_HAS_CONTENTS
2272 | SEC_IN_MEMORY
2273 | SEC_LINKER_CREATED
2274 | SEC_READONLY));
2275 if (sreloc == NULL
2276 || ! bfd_set_section_alignment (dynobj, sreloc,
2277 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2278 return NULL;
2280 return sreloc;
2283 /* Returns the GOT section, if it hasn't been excluded. */
2285 static asection *
2286 mips_elf_got_section (struct bfd_link_info *info)
2288 struct mips_elf_link_hash_table *htab;
2290 htab = mips_elf_hash_table (info);
2291 if (htab->sgot == NULL || (htab->sgot->flags & SEC_EXCLUDE) != 0)
2292 return NULL;
2293 return htab->sgot;
2296 /* Count the number of relocations needed for a TLS GOT entry, with
2297 access types from TLS_TYPE, and symbol H (or a local symbol if H
2298 is NULL). */
2300 static int
2301 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2302 struct elf_link_hash_entry *h)
2304 int indx = 0;
2305 int ret = 0;
2306 bfd_boolean need_relocs = FALSE;
2307 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2309 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2310 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2311 indx = h->dynindx;
2313 if ((info->shared || indx != 0)
2314 && (h == NULL
2315 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2316 || h->root.type != bfd_link_hash_undefweak))
2317 need_relocs = TRUE;
2319 if (!need_relocs)
2320 return FALSE;
2322 if (tls_type & GOT_TLS_GD)
2324 ret++;
2325 if (indx != 0)
2326 ret++;
2329 if (tls_type & GOT_TLS_IE)
2330 ret++;
2332 if ((tls_type & GOT_TLS_LDM) && info->shared)
2333 ret++;
2335 return ret;
2338 /* Count the number of TLS relocations required for the GOT entry in
2339 ARG1, if it describes a local symbol. */
2341 static int
2342 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2344 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2345 struct mips_elf_count_tls_arg *arg = arg2;
2347 if (entry->abfd != NULL && entry->symndx != -1)
2348 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2350 return 1;
2353 /* Count the number of TLS GOT entries required for the global (or
2354 forced-local) symbol in ARG1. */
2356 static int
2357 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2359 struct mips_elf_link_hash_entry *hm
2360 = (struct mips_elf_link_hash_entry *) arg1;
2361 struct mips_elf_count_tls_arg *arg = arg2;
2363 if (hm->tls_type & GOT_TLS_GD)
2364 arg->needed += 2;
2365 if (hm->tls_type & GOT_TLS_IE)
2366 arg->needed += 1;
2368 return 1;
2371 /* Count the number of TLS relocations required for the global (or
2372 forced-local) symbol in ARG1. */
2374 static int
2375 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2377 struct mips_elf_link_hash_entry *hm
2378 = (struct mips_elf_link_hash_entry *) arg1;
2379 struct mips_elf_count_tls_arg *arg = arg2;
2381 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2383 return 1;
2386 /* Output a simple dynamic relocation into SRELOC. */
2388 static void
2389 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2390 asection *sreloc,
2391 unsigned long indx,
2392 int r_type,
2393 bfd_vma offset)
2395 Elf_Internal_Rela rel[3];
2397 memset (rel, 0, sizeof (rel));
2399 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2400 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2402 if (ABI_64_P (output_bfd))
2404 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2405 (output_bfd, &rel[0],
2406 (sreloc->contents
2407 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2409 else
2410 bfd_elf32_swap_reloc_out
2411 (output_bfd, &rel[0],
2412 (sreloc->contents
2413 + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
2414 ++sreloc->reloc_count;
2417 /* Initialize a set of TLS GOT entries for one symbol. */
2419 static void
2420 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2421 unsigned char *tls_type_p,
2422 struct bfd_link_info *info,
2423 struct mips_elf_link_hash_entry *h,
2424 bfd_vma value)
2426 int indx;
2427 asection *sreloc, *sgot;
2428 bfd_vma offset, offset2;
2429 bfd_boolean need_relocs = FALSE;
2431 sgot = mips_elf_got_section (info);
2433 indx = 0;
2434 if (h != NULL)
2436 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2438 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2439 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2440 indx = h->root.dynindx;
2443 if (*tls_type_p & GOT_TLS_DONE)
2444 return;
2446 if ((info->shared || indx != 0)
2447 && (h == NULL
2448 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2449 || h->root.type != bfd_link_hash_undefweak))
2450 need_relocs = TRUE;
2452 /* MINUS_ONE means the symbol is not defined in this object. It may not
2453 be defined at all; assume that the value doesn't matter in that
2454 case. Otherwise complain if we would use the value. */
2455 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2456 || h->root.root.type == bfd_link_hash_undefweak);
2458 /* Emit necessary relocations. */
2459 sreloc = mips_elf_rel_dyn_section (info, FALSE);
2461 /* General Dynamic. */
2462 if (*tls_type_p & GOT_TLS_GD)
2464 offset = got_offset;
2465 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2467 if (need_relocs)
2469 mips_elf_output_dynamic_relocation
2470 (abfd, sreloc, indx,
2471 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2472 sgot->output_offset + sgot->output_section->vma + offset);
2474 if (indx)
2475 mips_elf_output_dynamic_relocation
2476 (abfd, sreloc, indx,
2477 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2478 sgot->output_offset + sgot->output_section->vma + offset2);
2479 else
2480 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2481 sgot->contents + offset2);
2483 else
2485 MIPS_ELF_PUT_WORD (abfd, 1,
2486 sgot->contents + offset);
2487 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2488 sgot->contents + offset2);
2491 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2494 /* Initial Exec model. */
2495 if (*tls_type_p & GOT_TLS_IE)
2497 offset = got_offset;
2499 if (need_relocs)
2501 if (indx == 0)
2502 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2503 sgot->contents + offset);
2504 else
2505 MIPS_ELF_PUT_WORD (abfd, 0,
2506 sgot->contents + offset);
2508 mips_elf_output_dynamic_relocation
2509 (abfd, sreloc, indx,
2510 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2511 sgot->output_offset + sgot->output_section->vma + offset);
2513 else
2514 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2515 sgot->contents + offset);
2518 if (*tls_type_p & GOT_TLS_LDM)
2520 /* The initial offset is zero, and the LD offsets will include the
2521 bias by DTP_OFFSET. */
2522 MIPS_ELF_PUT_WORD (abfd, 0,
2523 sgot->contents + got_offset
2524 + MIPS_ELF_GOT_SIZE (abfd));
2526 if (!info->shared)
2527 MIPS_ELF_PUT_WORD (abfd, 1,
2528 sgot->contents + got_offset);
2529 else
2530 mips_elf_output_dynamic_relocation
2531 (abfd, sreloc, indx,
2532 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2533 sgot->output_offset + sgot->output_section->vma + got_offset);
2536 *tls_type_p |= GOT_TLS_DONE;
2539 /* Return the GOT index to use for a relocation of type R_TYPE against
2540 a symbol accessed using TLS_TYPE models. The GOT entries for this
2541 symbol in this GOT start at GOT_INDEX. This function initializes the
2542 GOT entries and corresponding relocations. */
2544 static bfd_vma
2545 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2546 int r_type, struct bfd_link_info *info,
2547 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2549 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2550 || r_type == R_MIPS_TLS_LDM);
2552 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2554 if (r_type == R_MIPS_TLS_GOTTPREL)
2556 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2557 if (*tls_type & GOT_TLS_GD)
2558 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2559 else
2560 return got_index;
2563 if (r_type == R_MIPS_TLS_GD)
2565 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2566 return got_index;
2569 if (r_type == R_MIPS_TLS_LDM)
2571 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2572 return got_index;
2575 return got_index;
2578 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2579 for global symbol H. .got.plt comes before the GOT, so the offset
2580 will be negative. */
2582 static bfd_vma
2583 mips_elf_gotplt_index (struct bfd_link_info *info,
2584 struct elf_link_hash_entry *h)
2586 bfd_vma plt_index, got_address, got_value;
2587 struct mips_elf_link_hash_table *htab;
2589 htab = mips_elf_hash_table (info);
2590 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2592 /* Calculate the index of the symbol's PLT entry. */
2593 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
2595 /* Calculate the address of the associated .got.plt entry. */
2596 got_address = (htab->sgotplt->output_section->vma
2597 + htab->sgotplt->output_offset
2598 + plt_index * 4);
2600 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
2601 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
2602 + htab->root.hgot->root.u.def.section->output_offset
2603 + htab->root.hgot->root.u.def.value);
2605 return got_address - got_value;
2608 /* Return the GOT offset for address VALUE. If there is not yet a GOT
2609 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
2610 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
2611 offset can be found. */
2613 static bfd_vma
2614 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2615 bfd_vma value, unsigned long r_symndx,
2616 struct mips_elf_link_hash_entry *h, int r_type)
2618 struct mips_elf_link_hash_table *htab;
2619 struct mips_got_entry *entry;
2621 htab = mips_elf_hash_table (info);
2622 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
2623 r_symndx, h, r_type);
2624 if (!entry)
2625 return MINUS_ONE;
2627 if (TLS_RELOC_P (r_type))
2629 if (entry->symndx == -1 && htab->got_info->next == NULL)
2630 /* A type (3) entry in the single-GOT case. We use the symbol's
2631 hash table entry to track the index. */
2632 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
2633 r_type, info, h, value);
2634 else
2635 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
2636 r_type, info, h, value);
2638 else
2639 return entry->gotidx;
2642 /* Returns the GOT index for the global symbol indicated by H. */
2644 static bfd_vma
2645 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
2646 int r_type, struct bfd_link_info *info)
2648 struct mips_elf_link_hash_table *htab;
2649 bfd_vma index;
2650 struct mips_got_info *g, *gg;
2651 long global_got_dynindx = 0;
2653 htab = mips_elf_hash_table (info);
2654 gg = g = htab->got_info;
2655 if (g->bfd2got && ibfd)
2657 struct mips_got_entry e, *p;
2659 BFD_ASSERT (h->dynindx >= 0);
2661 g = mips_elf_got_for_ibfd (g, ibfd);
2662 if (g->next != gg || TLS_RELOC_P (r_type))
2664 e.abfd = ibfd;
2665 e.symndx = -1;
2666 e.d.h = (struct mips_elf_link_hash_entry *)h;
2667 e.tls_type = 0;
2669 p = htab_find (g->got_entries, &e);
2671 BFD_ASSERT (p->gotidx > 0);
2673 if (TLS_RELOC_P (r_type))
2675 bfd_vma value = MINUS_ONE;
2676 if ((h->root.type == bfd_link_hash_defined
2677 || h->root.type == bfd_link_hash_defweak)
2678 && h->root.u.def.section->output_section)
2679 value = (h->root.u.def.value
2680 + h->root.u.def.section->output_offset
2681 + h->root.u.def.section->output_section->vma);
2683 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
2684 info, e.d.h, value);
2686 else
2687 return p->gotidx;
2691 if (gg->global_gotsym != NULL)
2692 global_got_dynindx = gg->global_gotsym->dynindx;
2694 if (TLS_RELOC_P (r_type))
2696 struct mips_elf_link_hash_entry *hm
2697 = (struct mips_elf_link_hash_entry *) h;
2698 bfd_vma value = MINUS_ONE;
2700 if ((h->root.type == bfd_link_hash_defined
2701 || h->root.type == bfd_link_hash_defweak)
2702 && h->root.u.def.section->output_section)
2703 value = (h->root.u.def.value
2704 + h->root.u.def.section->output_offset
2705 + h->root.u.def.section->output_section->vma);
2707 index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
2708 r_type, info, hm, value);
2710 else
2712 /* Once we determine the global GOT entry with the lowest dynamic
2713 symbol table index, we must put all dynamic symbols with greater
2714 indices into the GOT. That makes it easy to calculate the GOT
2715 offset. */
2716 BFD_ASSERT (h->dynindx >= global_got_dynindx);
2717 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
2718 * MIPS_ELF_GOT_SIZE (abfd));
2720 BFD_ASSERT (index < htab->sgot->size);
2722 return index;
2725 /* Find a GOT page entry that points to within 32KB of VALUE. These
2726 entries are supposed to be placed at small offsets in the GOT, i.e.,
2727 within 32KB of GP. Return the index of the GOT entry, or -1 if no
2728 entry could be created. If OFFSETP is nonnull, use it to return the
2729 offset of the GOT entry from VALUE. */
2731 static bfd_vma
2732 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2733 bfd_vma value, bfd_vma *offsetp)
2735 bfd_vma page, index;
2736 struct mips_got_entry *entry;
2738 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
2739 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
2740 NULL, R_MIPS_GOT_PAGE);
2742 if (!entry)
2743 return MINUS_ONE;
2745 index = entry->gotidx;
2747 if (offsetp)
2748 *offsetp = value - entry->d.address;
2750 return index;
2753 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
2754 EXTERNAL is true if the relocation was against a global symbol
2755 that has been forced local. */
2757 static bfd_vma
2758 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
2759 bfd_vma value, bfd_boolean external)
2761 struct mips_got_entry *entry;
2763 /* GOT16 relocations against local symbols are followed by a LO16
2764 relocation; those against global symbols are not. Thus if the
2765 symbol was originally local, the GOT16 relocation should load the
2766 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
2767 if (! external)
2768 value = mips_elf_high (value) << 16;
2770 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
2771 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
2772 same in all cases. */
2773 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
2774 NULL, R_MIPS_GOT16);
2775 if (entry)
2776 return entry->gotidx;
2777 else
2778 return MINUS_ONE;
2781 /* Returns the offset for the entry at the INDEXth position
2782 in the GOT. */
2784 static bfd_vma
2785 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
2786 bfd *input_bfd, bfd_vma index)
2788 struct mips_elf_link_hash_table *htab;
2789 asection *sgot;
2790 bfd_vma gp;
2792 htab = mips_elf_hash_table (info);
2793 sgot = htab->sgot;
2794 gp = _bfd_get_gp_value (output_bfd)
2795 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
2797 return sgot->output_section->vma + sgot->output_offset + index - gp;
2800 /* Create and return a local GOT entry for VALUE, which was calculated
2801 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
2802 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
2803 instead. */
2805 static struct mips_got_entry *
2806 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
2807 bfd *ibfd, bfd_vma value,
2808 unsigned long r_symndx,
2809 struct mips_elf_link_hash_entry *h,
2810 int r_type)
2812 struct mips_got_entry entry, **loc;
2813 struct mips_got_info *g;
2814 struct mips_elf_link_hash_table *htab;
2816 htab = mips_elf_hash_table (info);
2818 entry.abfd = NULL;
2819 entry.symndx = -1;
2820 entry.d.address = value;
2821 entry.tls_type = 0;
2823 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
2824 if (g == NULL)
2826 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
2827 BFD_ASSERT (g != NULL);
2830 /* We might have a symbol, H, if it has been forced local. Use the
2831 global entry then. It doesn't matter whether an entry is local
2832 or global for TLS, since the dynamic linker does not
2833 automatically relocate TLS GOT entries. */
2834 BFD_ASSERT (h == NULL || h->root.forced_local);
2835 if (TLS_RELOC_P (r_type))
2837 struct mips_got_entry *p;
2839 entry.abfd = ibfd;
2840 if (r_type == R_MIPS_TLS_LDM)
2842 entry.tls_type = GOT_TLS_LDM;
2843 entry.symndx = 0;
2844 entry.d.addend = 0;
2846 else if (h == NULL)
2848 entry.symndx = r_symndx;
2849 entry.d.addend = 0;
2851 else
2852 entry.d.h = h;
2854 p = (struct mips_got_entry *)
2855 htab_find (g->got_entries, &entry);
2857 BFD_ASSERT (p);
2858 return p;
2861 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2862 INSERT);
2863 if (*loc)
2864 return *loc;
2866 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
2867 entry.tls_type = 0;
2869 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2871 if (! *loc)
2872 return NULL;
2874 memcpy (*loc, &entry, sizeof entry);
2876 if (g->assigned_gotno > g->local_gotno)
2878 (*loc)->gotidx = -1;
2879 /* We didn't allocate enough space in the GOT. */
2880 (*_bfd_error_handler)
2881 (_("not enough GOT space for local GOT entries"));
2882 bfd_set_error (bfd_error_bad_value);
2883 return NULL;
2886 MIPS_ELF_PUT_WORD (abfd, value,
2887 (htab->sgot->contents + entry.gotidx));
2889 /* These GOT entries need a dynamic relocation on VxWorks. */
2890 if (htab->is_vxworks)
2892 Elf_Internal_Rela outrel;
2893 asection *s;
2894 bfd_byte *loc;
2895 bfd_vma got_address;
2897 s = mips_elf_rel_dyn_section (info, FALSE);
2898 got_address = (htab->sgot->output_section->vma
2899 + htab->sgot->output_offset
2900 + entry.gotidx);
2902 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
2903 outrel.r_offset = got_address;
2904 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
2905 outrel.r_addend = value;
2906 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
2909 return *loc;
2912 /* Sort the dynamic symbol table so that symbols that need GOT entries
2913 appear towards the end. This reduces the amount of GOT space
2914 required. MAX_LOCAL is used to set the number of local symbols
2915 known to be in the dynamic symbol table. During
2916 _bfd_mips_elf_size_dynamic_sections, this value is 1. Afterward, the
2917 section symbols are added and the count is higher. */
2919 static bfd_boolean
2920 mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
2922 struct mips_elf_link_hash_table *htab;
2923 struct mips_elf_hash_sort_data hsd;
2924 struct mips_got_info *g;
2926 htab = mips_elf_hash_table (info);
2927 g = htab->got_info;
2929 hsd.low = NULL;
2930 hsd.max_unref_got_dynindx =
2931 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
2932 /* In the multi-got case, assigned_gotno of the master got_info
2933 indicate the number of entries that aren't referenced in the
2934 primary GOT, but that must have entries because there are
2935 dynamic relocations that reference it. Since they aren't
2936 referenced, we move them to the end of the GOT, so that they
2937 don't prevent other entries that are referenced from getting
2938 too large offsets. */
2939 - (g->next ? g->assigned_gotno : 0);
2940 hsd.max_non_got_dynindx = max_local;
2941 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
2942 elf_hash_table (info)),
2943 mips_elf_sort_hash_table_f,
2944 &hsd);
2946 /* There should have been enough room in the symbol table to
2947 accommodate both the GOT and non-GOT symbols. */
2948 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
2949 BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
2950 <= elf_hash_table (info)->dynsymcount);
2952 /* Now we know which dynamic symbol has the lowest dynamic symbol
2953 table index in the GOT. */
2954 g->global_gotsym = hsd.low;
2956 return TRUE;
2959 /* If H needs a GOT entry, assign it the highest available dynamic
2960 index. Otherwise, assign it the lowest available dynamic
2961 index. */
2963 static bfd_boolean
2964 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
2966 struct mips_elf_hash_sort_data *hsd = data;
2968 if (h->root.root.type == bfd_link_hash_warning)
2969 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2971 /* Symbols without dynamic symbol table entries aren't interesting
2972 at all. */
2973 if (h->root.dynindx == -1)
2974 return TRUE;
2976 switch (h->global_got_area)
2978 case GGA_NONE:
2979 h->root.dynindx = hsd->max_non_got_dynindx++;
2980 break;
2982 case GGA_NORMAL:
2983 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2985 h->root.dynindx = --hsd->min_got_dynindx;
2986 hsd->low = (struct elf_link_hash_entry *) h;
2987 break;
2989 case GGA_RELOC_ONLY:
2990 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2992 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
2993 hsd->low = (struct elf_link_hash_entry *) h;
2994 h->root.dynindx = hsd->max_unref_got_dynindx++;
2995 break;
2998 return TRUE;
3001 /* If H is a symbol that needs a global GOT entry, but has a dynamic
3002 symbol table index lower than any we've seen to date, record it for
3003 posterity. */
3005 static bfd_boolean
3006 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3007 bfd *abfd, struct bfd_link_info *info,
3008 unsigned char tls_flag)
3010 struct mips_elf_link_hash_table *htab;
3011 struct mips_elf_link_hash_entry *hmips;
3012 struct mips_got_entry entry, **loc;
3013 struct mips_got_info *g;
3015 htab = mips_elf_hash_table (info);
3016 hmips = (struct mips_elf_link_hash_entry *) h;
3018 /* A global symbol in the GOT must also be in the dynamic symbol
3019 table. */
3020 if (h->dynindx == -1)
3022 switch (ELF_ST_VISIBILITY (h->other))
3024 case STV_INTERNAL:
3025 case STV_HIDDEN:
3026 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3027 break;
3029 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3030 return FALSE;
3033 /* Make sure we have a GOT to put this entry into. */
3034 g = htab->got_info;
3035 BFD_ASSERT (g != NULL);
3037 entry.abfd = abfd;
3038 entry.symndx = -1;
3039 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3040 entry.tls_type = 0;
3042 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3043 INSERT);
3045 /* If we've already marked this entry as needing GOT space, we don't
3046 need to do it again. */
3047 if (*loc)
3049 (*loc)->tls_type |= tls_flag;
3050 return TRUE;
3053 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3055 if (! *loc)
3056 return FALSE;
3058 entry.gotidx = -1;
3059 entry.tls_type = tls_flag;
3061 memcpy (*loc, &entry, sizeof entry);
3063 if (tls_flag == 0)
3064 hmips->global_got_area = GGA_NORMAL;
3066 return TRUE;
3069 /* Reserve space in G for a GOT entry containing the value of symbol
3070 SYMNDX in input bfd ABDF, plus ADDEND. */
3072 static bfd_boolean
3073 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3074 struct bfd_link_info *info,
3075 unsigned char tls_flag)
3077 struct mips_elf_link_hash_table *htab;
3078 struct mips_got_info *g;
3079 struct mips_got_entry entry, **loc;
3081 htab = mips_elf_hash_table (info);
3082 g = htab->got_info;
3083 BFD_ASSERT (g != NULL);
3085 entry.abfd = abfd;
3086 entry.symndx = symndx;
3087 entry.d.addend = addend;
3088 entry.tls_type = tls_flag;
3089 loc = (struct mips_got_entry **)
3090 htab_find_slot (g->got_entries, &entry, INSERT);
3092 if (*loc)
3094 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3096 g->tls_gotno += 2;
3097 (*loc)->tls_type |= tls_flag;
3099 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3101 g->tls_gotno += 1;
3102 (*loc)->tls_type |= tls_flag;
3104 return TRUE;
3107 if (tls_flag != 0)
3109 entry.gotidx = -1;
3110 entry.tls_type = tls_flag;
3111 if (tls_flag == GOT_TLS_IE)
3112 g->tls_gotno += 1;
3113 else if (tls_flag == GOT_TLS_GD)
3114 g->tls_gotno += 2;
3115 else if (g->tls_ldm_offset == MINUS_ONE)
3117 g->tls_ldm_offset = MINUS_TWO;
3118 g->tls_gotno += 2;
3121 else
3123 entry.gotidx = g->local_gotno++;
3124 entry.tls_type = 0;
3127 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3129 if (! *loc)
3130 return FALSE;
3132 memcpy (*loc, &entry, sizeof entry);
3134 return TRUE;
3137 /* Return the maximum number of GOT page entries required for RANGE. */
3139 static bfd_vma
3140 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3142 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3145 /* Record that ABFD has a page relocation against symbol SYMNDX and
3146 that ADDEND is the addend for that relocation.
3148 This function creates an upper bound on the number of GOT slots
3149 required; no attempt is made to combine references to non-overridable
3150 global symbols across multiple input files. */
3152 static bfd_boolean
3153 mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3154 long symndx, bfd_signed_vma addend)
3156 struct mips_elf_link_hash_table *htab;
3157 struct mips_got_info *g;
3158 struct mips_got_page_entry lookup, *entry;
3159 struct mips_got_page_range **range_ptr, *range;
3160 bfd_vma old_pages, new_pages;
3161 void **loc;
3163 htab = mips_elf_hash_table (info);
3164 g = htab->got_info;
3165 BFD_ASSERT (g != NULL);
3167 /* Find the mips_got_page_entry hash table entry for this symbol. */
3168 lookup.abfd = abfd;
3169 lookup.symndx = symndx;
3170 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3171 if (loc == NULL)
3172 return FALSE;
3174 /* Create a mips_got_page_entry if this is the first time we've
3175 seen the symbol. */
3176 entry = (struct mips_got_page_entry *) *loc;
3177 if (!entry)
3179 entry = bfd_alloc (abfd, sizeof (*entry));
3180 if (!entry)
3181 return FALSE;
3183 entry->abfd = abfd;
3184 entry->symndx = symndx;
3185 entry->ranges = NULL;
3186 entry->num_pages = 0;
3187 *loc = entry;
3190 /* Skip over ranges whose maximum extent cannot share a page entry
3191 with ADDEND. */
3192 range_ptr = &entry->ranges;
3193 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3194 range_ptr = &(*range_ptr)->next;
3196 /* If we scanned to the end of the list, or found a range whose
3197 minimum extent cannot share a page entry with ADDEND, create
3198 a new singleton range. */
3199 range = *range_ptr;
3200 if (!range || addend < range->min_addend - 0xffff)
3202 range = bfd_alloc (abfd, sizeof (*range));
3203 if (!range)
3204 return FALSE;
3206 range->next = *range_ptr;
3207 range->min_addend = addend;
3208 range->max_addend = addend;
3210 *range_ptr = range;
3211 entry->num_pages++;
3212 g->page_gotno++;
3213 return TRUE;
3216 /* Remember how many pages the old range contributed. */
3217 old_pages = mips_elf_pages_for_range (range);
3219 /* Update the ranges. */
3220 if (addend < range->min_addend)
3221 range->min_addend = addend;
3222 else if (addend > range->max_addend)
3224 if (range->next && addend >= range->next->min_addend - 0xffff)
3226 old_pages += mips_elf_pages_for_range (range->next);
3227 range->max_addend = range->next->max_addend;
3228 range->next = range->next->next;
3230 else
3231 range->max_addend = addend;
3234 /* Record any change in the total estimate. */
3235 new_pages = mips_elf_pages_for_range (range);
3236 if (old_pages != new_pages)
3238 entry->num_pages += new_pages - old_pages;
3239 g->page_gotno += new_pages - old_pages;
3242 return TRUE;
3245 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3247 static void
3248 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3249 unsigned int n)
3251 asection *s;
3252 struct mips_elf_link_hash_table *htab;
3254 htab = mips_elf_hash_table (info);
3255 s = mips_elf_rel_dyn_section (info, FALSE);
3256 BFD_ASSERT (s != NULL);
3258 if (htab->is_vxworks)
3259 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3260 else
3262 if (s->size == 0)
3264 /* Make room for a null element. */
3265 s->size += MIPS_ELF_REL_SIZE (abfd);
3266 ++s->reloc_count;
3268 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3272 /* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3273 if the GOT entry is for an indirect or warning symbol. */
3275 static int
3276 mips_elf_check_recreate_got (void **entryp, void *data)
3278 struct mips_got_entry *entry;
3279 bfd_boolean *must_recreate;
3281 entry = (struct mips_got_entry *) *entryp;
3282 must_recreate = (bfd_boolean *) data;
3283 if (entry->abfd != NULL && entry->symndx == -1)
3285 struct mips_elf_link_hash_entry *h;
3287 h = entry->d.h;
3288 if (h->root.root.type == bfd_link_hash_indirect
3289 || h->root.root.type == bfd_link_hash_warning)
3291 *must_recreate = TRUE;
3292 return 0;
3295 return 1;
3298 /* A htab_traverse callback for GOT entries. Add all entries to
3299 hash table *DATA, converting entries for indirect and warning
3300 symbols into entries for the target symbol. Set *DATA to null
3301 on error. */
3303 static int
3304 mips_elf_recreate_got (void **entryp, void *data)
3306 htab_t *new_got;
3307 struct mips_got_entry *entry;
3308 void **slot;
3310 new_got = (htab_t *) data;
3311 entry = (struct mips_got_entry *) *entryp;
3312 if (entry->abfd != NULL && entry->symndx == -1)
3314 struct mips_elf_link_hash_entry *h;
3316 h = entry->d.h;
3317 while (h->root.root.type == bfd_link_hash_indirect
3318 || h->root.root.type == bfd_link_hash_warning)
3320 BFD_ASSERT (h->global_got_area == GGA_NONE);
3321 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3323 entry->d.h = h;
3325 slot = htab_find_slot (*new_got, entry, INSERT);
3326 if (slot == NULL)
3328 *new_got = NULL;
3329 return 0;
3331 if (*slot == NULL)
3332 *slot = entry;
3333 else
3334 free (entry);
3335 return 1;
3338 /* If any entries in G->got_entries are for indirect or warning symbols,
3339 replace them with entries for the target symbol. */
3341 static bfd_boolean
3342 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3344 bfd_boolean must_recreate;
3345 htab_t new_got;
3347 must_recreate = FALSE;
3348 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3349 if (must_recreate)
3351 new_got = htab_create (htab_size (g->got_entries),
3352 mips_elf_got_entry_hash,
3353 mips_elf_got_entry_eq, NULL);
3354 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3355 if (new_got == NULL)
3356 return FALSE;
3358 /* Each entry in g->got_entries has either been copied to new_got
3359 or freed. Now delete the hash table itself. */
3360 htab_delete (g->got_entries);
3361 g->got_entries = new_got;
3363 return TRUE;
3366 /* A mips_elf_link_hash_traverse callback for which DATA points
3367 to a mips_got_info. Add each forced-local GOT symbol to DATA's
3368 local_gotno field. */
3370 static int
3371 mips_elf_count_forced_local_got_symbols (struct mips_elf_link_hash_entry *h,
3372 void *data)
3374 struct mips_got_info *g;
3376 g = (struct mips_got_info *) data;
3377 if (h->global_got_area != GGA_NONE
3378 && (h->root.forced_local || h->root.dynindx == -1))
3380 /* We no longer need this entry if it was only used for
3381 relocations; those relocations will be against the
3382 null or section symbol instead of H. */
3383 if (h->global_got_area != GGA_RELOC_ONLY)
3384 g->local_gotno++;
3385 h->global_got_area = GGA_NONE;
3387 return 1;
3390 /* Compute the hash value of the bfd in a bfd2got hash entry. */
3392 static hashval_t
3393 mips_elf_bfd2got_entry_hash (const void *entry_)
3395 const struct mips_elf_bfd2got_hash *entry
3396 = (struct mips_elf_bfd2got_hash *)entry_;
3398 return entry->bfd->id;
3401 /* Check whether two hash entries have the same bfd. */
3403 static int
3404 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
3406 const struct mips_elf_bfd2got_hash *e1
3407 = (const struct mips_elf_bfd2got_hash *)entry1;
3408 const struct mips_elf_bfd2got_hash *e2
3409 = (const struct mips_elf_bfd2got_hash *)entry2;
3411 return e1->bfd == e2->bfd;
3414 /* In a multi-got link, determine the GOT to be used for IBFD. G must
3415 be the master GOT data. */
3417 static struct mips_got_info *
3418 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
3420 struct mips_elf_bfd2got_hash e, *p;
3422 if (! g->bfd2got)
3423 return g;
3425 e.bfd = ibfd;
3426 p = htab_find (g->bfd2got, &e);
3427 return p ? p->g : NULL;
3430 /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3431 Return NULL if an error occured. */
3433 static struct mips_got_info *
3434 mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3435 bfd *input_bfd)
3437 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3438 struct mips_got_info *g;
3439 void **bfdgotp;
3441 bfdgot_entry.bfd = input_bfd;
3442 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3443 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
3445 if (bfdgot == NULL)
3447 bfdgot = ((struct mips_elf_bfd2got_hash *)
3448 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
3449 if (bfdgot == NULL)
3450 return NULL;
3452 *bfdgotp = bfdgot;
3454 g = ((struct mips_got_info *)
3455 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
3456 if (g == NULL)
3457 return NULL;
3459 bfdgot->bfd = input_bfd;
3460 bfdgot->g = g;
3462 g->global_gotsym = NULL;
3463 g->global_gotno = 0;
3464 g->local_gotno = 0;
3465 g->page_gotno = 0;
3466 g->assigned_gotno = -1;
3467 g->tls_gotno = 0;
3468 g->tls_assigned_gotno = 0;
3469 g->tls_ldm_offset = MINUS_ONE;
3470 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3471 mips_elf_multi_got_entry_eq, NULL);
3472 if (g->got_entries == NULL)
3473 return NULL;
3475 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3476 mips_got_page_entry_eq, NULL);
3477 if (g->got_page_entries == NULL)
3478 return NULL;
3480 g->bfd2got = NULL;
3481 g->next = NULL;
3484 return bfdgot->g;
3487 /* A htab_traverse callback for the entries in the master got.
3488 Create one separate got for each bfd that has entries in the global
3489 got, such that we can tell how many local and global entries each
3490 bfd requires. */
3492 static int
3493 mips_elf_make_got_per_bfd (void **entryp, void *p)
3495 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3496 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3497 struct mips_got_info *g;
3499 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3500 if (g == NULL)
3502 arg->obfd = NULL;
3503 return 0;
3506 /* Insert the GOT entry in the bfd's got entry hash table. */
3507 entryp = htab_find_slot (g->got_entries, entry, INSERT);
3508 if (*entryp != NULL)
3509 return 1;
3511 *entryp = entry;
3513 if (entry->tls_type)
3515 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3516 g->tls_gotno += 2;
3517 if (entry->tls_type & GOT_TLS_IE)
3518 g->tls_gotno += 1;
3520 else if (entry->symndx >= 0 || entry->d.h->root.forced_local)
3521 ++g->local_gotno;
3522 else
3523 ++g->global_gotno;
3525 return 1;
3528 /* A htab_traverse callback for the page entries in the master got.
3529 Associate each page entry with the bfd's got. */
3531 static int
3532 mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
3534 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
3535 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
3536 struct mips_got_info *g;
3538 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
3539 if (g == NULL)
3541 arg->obfd = NULL;
3542 return 0;
3545 /* Insert the GOT entry in the bfd's got entry hash table. */
3546 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
3547 if (*entryp != NULL)
3548 return 1;
3550 *entryp = entry;
3551 g->page_gotno += entry->num_pages;
3552 return 1;
3555 /* Consider merging the got described by BFD2GOT with TO, using the
3556 information given by ARG. Return -1 if this would lead to overflow,
3557 1 if they were merged successfully, and 0 if a merge failed due to
3558 lack of memory. (These values are chosen so that nonnegative return
3559 values can be returned by a htab_traverse callback.) */
3561 static int
3562 mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
3563 struct mips_got_info *to,
3564 struct mips_elf_got_per_bfd_arg *arg)
3566 struct mips_got_info *from = bfd2got->g;
3567 unsigned int estimate;
3569 /* Work out how many page entries we would need for the combined GOT. */
3570 estimate = arg->max_pages;
3571 if (estimate >= from->page_gotno + to->page_gotno)
3572 estimate = from->page_gotno + to->page_gotno;
3574 /* And conservatively estimate how many local, global and TLS entries
3575 would be needed. */
3576 estimate += (from->local_gotno
3577 + from->global_gotno
3578 + from->tls_gotno
3579 + to->local_gotno
3580 + to->global_gotno
3581 + to->tls_gotno);
3583 /* Bail out if the combined GOT might be too big. */
3584 if (estimate > arg->max_count)
3585 return -1;
3587 /* Commit to the merge. Record that TO is now the bfd for this got. */
3588 bfd2got->g = to;
3590 /* Transfer the bfd's got information from FROM to TO. */
3591 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
3592 if (arg->obfd == NULL)
3593 return 0;
3595 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
3596 if (arg->obfd == NULL)
3597 return 0;
3599 /* We don't have to worry about releasing memory of the actual
3600 got entries, since they're all in the master got_entries hash
3601 table anyway. */
3602 htab_delete (from->got_entries);
3603 htab_delete (from->got_page_entries);
3604 return 1;
3607 /* Attempt to merge gots of different input bfds. Try to use as much
3608 as possible of the primary got, since it doesn't require explicit
3609 dynamic relocations, but don't use bfds that would reference global
3610 symbols out of the addressable range. Failing the primary got,
3611 attempt to merge with the current got, or finish the current got
3612 and then make make the new got current. */
3614 static int
3615 mips_elf_merge_gots (void **bfd2got_, void *p)
3617 struct mips_elf_bfd2got_hash *bfd2got
3618 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
3619 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3620 struct mips_got_info *g;
3621 unsigned int estimate;
3622 int result;
3624 g = bfd2got->g;
3626 /* Work out the number of page, local and TLS entries. */
3627 estimate = arg->max_pages;
3628 if (estimate > g->page_gotno)
3629 estimate = g->page_gotno;
3630 estimate += g->local_gotno + g->tls_gotno;
3632 /* We place TLS GOT entries after both locals and globals. The globals
3633 for the primary GOT may overflow the normal GOT size limit, so be
3634 sure not to merge a GOT which requires TLS with the primary GOT in that
3635 case. This doesn't affect non-primary GOTs. */
3636 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
3638 if (estimate <= arg->max_count)
3640 /* If we don't have a primary GOT, use it as
3641 a starting point for the primary GOT. */
3642 if (!arg->primary)
3644 arg->primary = bfd2got->g;
3645 return 1;
3648 /* Try merging with the primary GOT. */
3649 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
3650 if (result >= 0)
3651 return result;
3654 /* If we can merge with the last-created got, do it. */
3655 if (arg->current)
3657 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
3658 if (result >= 0)
3659 return result;
3662 /* Well, we couldn't merge, so create a new GOT. Don't check if it
3663 fits; if it turns out that it doesn't, we'll get relocation
3664 overflows anyway. */
3665 g->next = arg->current;
3666 arg->current = g;
3668 return 1;
3671 /* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
3672 is null iff there is just a single GOT. */
3674 static int
3675 mips_elf_initialize_tls_index (void **entryp, void *p)
3677 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3678 struct mips_got_info *g = p;
3679 bfd_vma next_index;
3680 unsigned char tls_type;
3682 /* We're only interested in TLS symbols. */
3683 if (entry->tls_type == 0)
3684 return 1;
3686 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
3688 if (entry->symndx == -1 && g->next == NULL)
3690 /* A type (3) got entry in the single-GOT case. We use the symbol's
3691 hash table entry to track its index. */
3692 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
3693 return 1;
3694 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
3695 entry->d.h->tls_got_offset = next_index;
3696 tls_type = entry->d.h->tls_type;
3698 else
3700 if (entry->tls_type & GOT_TLS_LDM)
3702 /* There are separate mips_got_entry objects for each input bfd
3703 that requires an LDM entry. Make sure that all LDM entries in
3704 a GOT resolve to the same index. */
3705 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
3707 entry->gotidx = g->tls_ldm_offset;
3708 return 1;
3710 g->tls_ldm_offset = next_index;
3712 entry->gotidx = next_index;
3713 tls_type = entry->tls_type;
3716 /* Account for the entries we've just allocated. */
3717 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3718 g->tls_assigned_gotno += 2;
3719 if (tls_type & GOT_TLS_IE)
3720 g->tls_assigned_gotno += 1;
3722 return 1;
3725 /* If passed a NULL mips_got_info in the argument, set the marker used
3726 to tell whether a global symbol needs a got entry (in the primary
3727 got) to the given VALUE.
3729 If passed a pointer G to a mips_got_info in the argument (it must
3730 not be the primary GOT), compute the offset from the beginning of
3731 the (primary) GOT section to the entry in G corresponding to the
3732 global symbol. G's assigned_gotno must contain the index of the
3733 first available global GOT entry in G. VALUE must contain the size
3734 of a GOT entry in bytes. For each global GOT entry that requires a
3735 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
3736 marked as not eligible for lazy resolution through a function
3737 stub. */
3738 static int
3739 mips_elf_set_global_got_offset (void **entryp, void *p)
3741 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3742 struct mips_elf_set_global_got_offset_arg *arg
3743 = (struct mips_elf_set_global_got_offset_arg *)p;
3744 struct mips_got_info *g = arg->g;
3746 if (g && entry->tls_type != GOT_NORMAL)
3747 arg->needed_relocs +=
3748 mips_tls_got_relocs (arg->info, entry->tls_type,
3749 entry->symndx == -1 ? &entry->d.h->root : NULL);
3751 if (entry->abfd != NULL
3752 && entry->symndx == -1
3753 && entry->d.h->global_got_area != GGA_NONE)
3755 if (g)
3757 BFD_ASSERT (g->global_gotsym == NULL);
3759 entry->gotidx = arg->value * (long) g->assigned_gotno++;
3760 if (arg->info->shared
3761 || (elf_hash_table (arg->info)->dynamic_sections_created
3762 && entry->d.h->root.def_dynamic
3763 && !entry->d.h->root.def_regular))
3764 ++arg->needed_relocs;
3766 else
3767 entry->d.h->global_got_area = arg->value;
3770 return 1;
3773 /* A htab_traverse callback for GOT entries for which DATA is the
3774 bfd_link_info. Forbid any global symbols from having traditional
3775 lazy-binding stubs. */
3777 static int
3778 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
3780 struct bfd_link_info *info;
3781 struct mips_elf_link_hash_table *htab;
3782 struct mips_got_entry *entry;
3784 entry = (struct mips_got_entry *) *entryp;
3785 info = (struct bfd_link_info *) data;
3786 htab = mips_elf_hash_table (info);
3787 if (entry->abfd != NULL
3788 && entry->symndx == -1
3789 && entry->d.h->needs_lazy_stub)
3791 entry->d.h->needs_lazy_stub = FALSE;
3792 htab->lazy_stub_count--;
3795 return 1;
3798 /* Return the offset of an input bfd IBFD's GOT from the beginning of
3799 the primary GOT. */
3800 static bfd_vma
3801 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
3803 if (g->bfd2got == NULL)
3804 return 0;
3806 g = mips_elf_got_for_ibfd (g, ibfd);
3807 if (! g)
3808 return 0;
3810 BFD_ASSERT (g->next);
3812 g = g->next;
3814 return (g->local_gotno + g->global_gotno + g->tls_gotno)
3815 * MIPS_ELF_GOT_SIZE (abfd);
3818 /* Turn a single GOT that is too big for 16-bit addressing into
3819 a sequence of GOTs, each one 16-bit addressable. */
3821 static bfd_boolean
3822 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
3823 asection *got, bfd_size_type pages)
3825 struct mips_elf_link_hash_table *htab;
3826 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
3827 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
3828 struct mips_got_info *g, *gg;
3829 unsigned int assign, needed_relocs;
3830 bfd *dynobj;
3832 dynobj = elf_hash_table (info)->dynobj;
3833 htab = mips_elf_hash_table (info);
3834 g = htab->got_info;
3835 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
3836 mips_elf_bfd2got_entry_eq, NULL);
3837 if (g->bfd2got == NULL)
3838 return FALSE;
3840 got_per_bfd_arg.bfd2got = g->bfd2got;
3841 got_per_bfd_arg.obfd = abfd;
3842 got_per_bfd_arg.info = info;
3844 /* Count how many GOT entries each input bfd requires, creating a
3845 map from bfd to got info while at that. */
3846 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
3847 if (got_per_bfd_arg.obfd == NULL)
3848 return FALSE;
3850 /* Also count how many page entries each input bfd requires. */
3851 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
3852 &got_per_bfd_arg);
3853 if (got_per_bfd_arg.obfd == NULL)
3854 return FALSE;
3856 got_per_bfd_arg.current = NULL;
3857 got_per_bfd_arg.primary = NULL;
3858 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
3859 / MIPS_ELF_GOT_SIZE (abfd))
3860 - MIPS_RESERVED_GOTNO (info));
3861 got_per_bfd_arg.max_pages = pages;
3862 /* The number of globals that will be included in the primary GOT.
3863 See the calls to mips_elf_set_global_got_offset below for more
3864 information. */
3865 got_per_bfd_arg.global_count = g->global_gotno;
3867 /* Try to merge the GOTs of input bfds together, as long as they
3868 don't seem to exceed the maximum GOT size, choosing one of them
3869 to be the primary GOT. */
3870 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
3871 if (got_per_bfd_arg.obfd == NULL)
3872 return FALSE;
3874 /* If we do not find any suitable primary GOT, create an empty one. */
3875 if (got_per_bfd_arg.primary == NULL)
3877 g->next = (struct mips_got_info *)
3878 bfd_alloc (abfd, sizeof (struct mips_got_info));
3879 if (g->next == NULL)
3880 return FALSE;
3882 g->next->global_gotsym = NULL;
3883 g->next->global_gotno = 0;
3884 g->next->local_gotno = 0;
3885 g->next->page_gotno = 0;
3886 g->next->tls_gotno = 0;
3887 g->next->assigned_gotno = 0;
3888 g->next->tls_assigned_gotno = 0;
3889 g->next->tls_ldm_offset = MINUS_ONE;
3890 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3891 mips_elf_multi_got_entry_eq,
3892 NULL);
3893 if (g->next->got_entries == NULL)
3894 return FALSE;
3895 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3896 mips_got_page_entry_eq,
3897 NULL);
3898 if (g->next->got_page_entries == NULL)
3899 return FALSE;
3900 g->next->bfd2got = NULL;
3902 else
3903 g->next = got_per_bfd_arg.primary;
3904 g->next->next = got_per_bfd_arg.current;
3906 /* GG is now the master GOT, and G is the primary GOT. */
3907 gg = g;
3908 g = g->next;
3910 /* Map the output bfd to the primary got. That's what we're going
3911 to use for bfds that use GOT16 or GOT_PAGE relocations that we
3912 didn't mark in check_relocs, and we want a quick way to find it.
3913 We can't just use gg->next because we're going to reverse the
3914 list. */
3916 struct mips_elf_bfd2got_hash *bfdgot;
3917 void **bfdgotp;
3919 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3920 (abfd, sizeof (struct mips_elf_bfd2got_hash));
3922 if (bfdgot == NULL)
3923 return FALSE;
3925 bfdgot->bfd = abfd;
3926 bfdgot->g = g;
3927 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
3929 BFD_ASSERT (*bfdgotp == NULL);
3930 *bfdgotp = bfdgot;
3933 /* Every symbol that is referenced in a dynamic relocation must be
3934 present in the primary GOT, so arrange for them to appear after
3935 those that are actually referenced. */
3936 gg->assigned_gotno = gg->global_gotno - g->global_gotno;
3937 g->global_gotno = gg->global_gotno;
3939 set_got_offset_arg.g = NULL;
3940 set_got_offset_arg.value = GGA_RELOC_ONLY;
3941 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
3942 &set_got_offset_arg);
3943 set_got_offset_arg.value = GGA_NORMAL;
3944 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
3945 &set_got_offset_arg);
3946 if (! mips_elf_sort_hash_table (info, 1))
3947 return FALSE;
3949 /* Now go through the GOTs assigning them offset ranges.
3950 [assigned_gotno, local_gotno[ will be set to the range of local
3951 entries in each GOT. We can then compute the end of a GOT by
3952 adding local_gotno to global_gotno. We reverse the list and make
3953 it circular since then we'll be able to quickly compute the
3954 beginning of a GOT, by computing the end of its predecessor. To
3955 avoid special cases for the primary GOT, while still preserving
3956 assertions that are valid for both single- and multi-got links,
3957 we arrange for the main got struct to have the right number of
3958 global entries, but set its local_gotno such that the initial
3959 offset of the primary GOT is zero. Remember that the primary GOT
3960 will become the last item in the circular linked list, so it
3961 points back to the master GOT. */
3962 gg->local_gotno = -g->global_gotno;
3963 gg->global_gotno = g->global_gotno;
3964 gg->tls_gotno = 0;
3965 assign = 0;
3966 gg->next = gg;
3970 struct mips_got_info *gn;
3972 assign += MIPS_RESERVED_GOTNO (info);
3973 g->assigned_gotno = assign;
3974 g->local_gotno += assign;
3975 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
3976 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
3978 /* Take g out of the direct list, and push it onto the reversed
3979 list that gg points to. g->next is guaranteed to be nonnull after
3980 this operation, as required by mips_elf_initialize_tls_index. */
3981 gn = g->next;
3982 g->next = gg->next;
3983 gg->next = g;
3985 /* Set up any TLS entries. We always place the TLS entries after
3986 all non-TLS entries. */
3987 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
3988 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
3990 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
3991 g = gn;
3993 /* Forbid global symbols in every non-primary GOT from having
3994 lazy-binding stubs. */
3995 if (g)
3996 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
3998 while (g);
4000 got->size = (gg->next->local_gotno
4001 + gg->next->global_gotno
4002 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4004 needed_relocs = 0;
4005 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4006 set_got_offset_arg.info = info;
4007 for (g = gg->next; g && g->next != gg; g = g->next)
4009 unsigned int save_assign;
4011 /* Assign offsets to global GOT entries. */
4012 save_assign = g->assigned_gotno;
4013 g->assigned_gotno = g->local_gotno;
4014 set_got_offset_arg.g = g;
4015 set_got_offset_arg.needed_relocs = 0;
4016 htab_traverse (g->got_entries,
4017 mips_elf_set_global_got_offset,
4018 &set_got_offset_arg);
4019 needed_relocs += set_got_offset_arg.needed_relocs;
4020 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4022 g->assigned_gotno = save_assign;
4023 if (info->shared)
4025 needed_relocs += g->local_gotno - g->assigned_gotno;
4026 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4027 + g->next->global_gotno
4028 + g->next->tls_gotno
4029 + MIPS_RESERVED_GOTNO (info));
4033 if (needed_relocs)
4034 mips_elf_allocate_dynamic_relocations (dynobj, info,
4035 needed_relocs);
4037 return TRUE;
4041 /* Returns the first relocation of type r_type found, beginning with
4042 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4044 static const Elf_Internal_Rela *
4045 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4046 const Elf_Internal_Rela *relocation,
4047 const Elf_Internal_Rela *relend)
4049 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4051 while (relocation < relend)
4053 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4054 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4055 return relocation;
4057 ++relocation;
4060 /* We didn't find it. */
4061 return NULL;
4064 /* Return whether a relocation is against a local symbol. */
4066 static bfd_boolean
4067 mips_elf_local_relocation_p (bfd *input_bfd,
4068 const Elf_Internal_Rela *relocation,
4069 asection **local_sections,
4070 bfd_boolean check_forced)
4072 unsigned long r_symndx;
4073 Elf_Internal_Shdr *symtab_hdr;
4074 struct mips_elf_link_hash_entry *h;
4075 size_t extsymoff;
4077 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4078 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4079 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4081 if (r_symndx < extsymoff)
4082 return TRUE;
4083 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4084 return TRUE;
4086 if (check_forced)
4088 /* Look up the hash table to check whether the symbol
4089 was forced local. */
4090 h = (struct mips_elf_link_hash_entry *)
4091 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
4092 /* Find the real hash-table entry for this symbol. */
4093 while (h->root.root.type == bfd_link_hash_indirect
4094 || h->root.root.type == bfd_link_hash_warning)
4095 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4096 if (h->root.forced_local)
4097 return TRUE;
4100 return FALSE;
4103 /* Sign-extend VALUE, which has the indicated number of BITS. */
4105 bfd_vma
4106 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4108 if (value & ((bfd_vma) 1 << (bits - 1)))
4109 /* VALUE is negative. */
4110 value |= ((bfd_vma) - 1) << bits;
4112 return value;
4115 /* Return non-zero if the indicated VALUE has overflowed the maximum
4116 range expressible by a signed number with the indicated number of
4117 BITS. */
4119 static bfd_boolean
4120 mips_elf_overflow_p (bfd_vma value, int bits)
4122 bfd_signed_vma svalue = (bfd_signed_vma) value;
4124 if (svalue > (1 << (bits - 1)) - 1)
4125 /* The value is too big. */
4126 return TRUE;
4127 else if (svalue < -(1 << (bits - 1)))
4128 /* The value is too small. */
4129 return TRUE;
4131 /* All is well. */
4132 return FALSE;
4135 /* Calculate the %high function. */
4137 static bfd_vma
4138 mips_elf_high (bfd_vma value)
4140 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4143 /* Calculate the %higher function. */
4145 static bfd_vma
4146 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4148 #ifdef BFD64
4149 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4150 #else
4151 abort ();
4152 return MINUS_ONE;
4153 #endif
4156 /* Calculate the %highest function. */
4158 static bfd_vma
4159 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4161 #ifdef BFD64
4162 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4163 #else
4164 abort ();
4165 return MINUS_ONE;
4166 #endif
4169 /* Create the .compact_rel section. */
4171 static bfd_boolean
4172 mips_elf_create_compact_rel_section
4173 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4175 flagword flags;
4176 register asection *s;
4178 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4180 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4181 | SEC_READONLY);
4183 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4184 if (s == NULL
4185 || ! bfd_set_section_alignment (abfd, s,
4186 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4187 return FALSE;
4189 s->size = sizeof (Elf32_External_compact_rel);
4192 return TRUE;
4195 /* Create the .got section to hold the global offset table. */
4197 static bfd_boolean
4198 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
4199 bfd_boolean maybe_exclude)
4201 flagword flags;
4202 register asection *s;
4203 struct elf_link_hash_entry *h;
4204 struct bfd_link_hash_entry *bh;
4205 struct mips_got_info *g;
4206 bfd_size_type amt;
4207 struct mips_elf_link_hash_table *htab;
4209 htab = mips_elf_hash_table (info);
4211 /* This function may be called more than once. */
4212 s = htab->sgot;
4213 if (s)
4215 if (! maybe_exclude)
4216 s->flags &= ~SEC_EXCLUDE;
4217 return TRUE;
4220 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4221 | SEC_LINKER_CREATED);
4223 if (maybe_exclude)
4224 flags |= SEC_EXCLUDE;
4226 /* We have to use an alignment of 2**4 here because this is hardcoded
4227 in the function stub generation and in the linker script. */
4228 s = bfd_make_section_with_flags (abfd, ".got", flags);
4229 if (s == NULL
4230 || ! bfd_set_section_alignment (abfd, s, 4))
4231 return FALSE;
4232 htab->sgot = s;
4234 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4235 linker script because we don't want to define the symbol if we
4236 are not creating a global offset table. */
4237 bh = NULL;
4238 if (! (_bfd_generic_link_add_one_symbol
4239 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4240 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4241 return FALSE;
4243 h = (struct elf_link_hash_entry *) bh;
4244 h->non_elf = 0;
4245 h->def_regular = 1;
4246 h->type = STT_OBJECT;
4247 elf_hash_table (info)->hgot = h;
4249 if (info->shared
4250 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4251 return FALSE;
4253 amt = sizeof (struct mips_got_info);
4254 g = bfd_alloc (abfd, amt);
4255 if (g == NULL)
4256 return FALSE;
4257 g->global_gotsym = NULL;
4258 g->global_gotno = 0;
4259 g->tls_gotno = 0;
4260 g->local_gotno = MIPS_RESERVED_GOTNO (info);
4261 g->page_gotno = 0;
4262 g->assigned_gotno = MIPS_RESERVED_GOTNO (info);
4263 g->bfd2got = NULL;
4264 g->next = NULL;
4265 g->tls_ldm_offset = MINUS_ONE;
4266 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4267 mips_elf_got_entry_eq, NULL);
4268 if (g->got_entries == NULL)
4269 return FALSE;
4270 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4271 mips_got_page_entry_eq, NULL);
4272 if (g->got_page_entries == NULL)
4273 return FALSE;
4274 htab->got_info = g;
4275 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4276 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4278 /* VxWorks also needs a .got.plt section. */
4279 if (htab->is_vxworks)
4281 s = bfd_make_section_with_flags (abfd, ".got.plt",
4282 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4283 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4284 if (s == NULL || !bfd_set_section_alignment (abfd, s, 4))
4285 return FALSE;
4287 htab->sgotplt = s;
4289 return TRUE;
4292 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4293 __GOTT_INDEX__ symbols. These symbols are only special for
4294 shared objects; they are not used in executables. */
4296 static bfd_boolean
4297 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4299 return (mips_elf_hash_table (info)->is_vxworks
4300 && info->shared
4301 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4302 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4305 /* Calculate the value produced by the RELOCATION (which comes from
4306 the INPUT_BFD). The ADDEND is the addend to use for this
4307 RELOCATION; RELOCATION->R_ADDEND is ignored.
4309 The result of the relocation calculation is stored in VALUEP.
4310 REQUIRE_JALXP indicates whether or not the opcode used with this
4311 relocation must be JALX.
4313 This function returns bfd_reloc_continue if the caller need take no
4314 further action regarding this relocation, bfd_reloc_notsupported if
4315 something goes dramatically wrong, bfd_reloc_overflow if an
4316 overflow occurs, and bfd_reloc_ok to indicate success. */
4318 static bfd_reloc_status_type
4319 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4320 asection *input_section,
4321 struct bfd_link_info *info,
4322 const Elf_Internal_Rela *relocation,
4323 bfd_vma addend, reloc_howto_type *howto,
4324 Elf_Internal_Sym *local_syms,
4325 asection **local_sections, bfd_vma *valuep,
4326 const char **namep, bfd_boolean *require_jalxp,
4327 bfd_boolean save_addend)
4329 /* The eventual value we will return. */
4330 bfd_vma value;
4331 /* The address of the symbol against which the relocation is
4332 occurring. */
4333 bfd_vma symbol = 0;
4334 /* The final GP value to be used for the relocatable, executable, or
4335 shared object file being produced. */
4336 bfd_vma gp;
4337 /* The place (section offset or address) of the storage unit being
4338 relocated. */
4339 bfd_vma p;
4340 /* The value of GP used to create the relocatable object. */
4341 bfd_vma gp0;
4342 /* The offset into the global offset table at which the address of
4343 the relocation entry symbol, adjusted by the addend, resides
4344 during execution. */
4345 bfd_vma g = MINUS_ONE;
4346 /* The section in which the symbol referenced by the relocation is
4347 located. */
4348 asection *sec = NULL;
4349 struct mips_elf_link_hash_entry *h = NULL;
4350 /* TRUE if the symbol referred to by this relocation is a local
4351 symbol. */
4352 bfd_boolean local_p, was_local_p;
4353 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4354 bfd_boolean gp_disp_p = FALSE;
4355 /* TRUE if the symbol referred to by this relocation is
4356 "__gnu_local_gp". */
4357 bfd_boolean gnu_local_gp_p = FALSE;
4358 Elf_Internal_Shdr *symtab_hdr;
4359 size_t extsymoff;
4360 unsigned long r_symndx;
4361 int r_type;
4362 /* TRUE if overflow occurred during the calculation of the
4363 relocation value. */
4364 bfd_boolean overflowed_p;
4365 /* TRUE if this relocation refers to a MIPS16 function. */
4366 bfd_boolean target_is_16_bit_code_p = FALSE;
4367 struct mips_elf_link_hash_table *htab;
4368 bfd *dynobj;
4370 dynobj = elf_hash_table (info)->dynobj;
4371 htab = mips_elf_hash_table (info);
4373 /* Parse the relocation. */
4374 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4375 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4376 p = (input_section->output_section->vma
4377 + input_section->output_offset
4378 + relocation->r_offset);
4380 /* Assume that there will be no overflow. */
4381 overflowed_p = FALSE;
4383 /* Figure out whether or not the symbol is local, and get the offset
4384 used in the array of hash table entries. */
4385 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4386 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4387 local_sections, FALSE);
4388 was_local_p = local_p;
4389 if (! elf_bad_symtab (input_bfd))
4390 extsymoff = symtab_hdr->sh_info;
4391 else
4393 /* The symbol table does not follow the rule that local symbols
4394 must come before globals. */
4395 extsymoff = 0;
4398 /* Figure out the value of the symbol. */
4399 if (local_p)
4401 Elf_Internal_Sym *sym;
4403 sym = local_syms + r_symndx;
4404 sec = local_sections[r_symndx];
4406 symbol = sec->output_section->vma + sec->output_offset;
4407 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4408 || (sec->flags & SEC_MERGE))
4409 symbol += sym->st_value;
4410 if ((sec->flags & SEC_MERGE)
4411 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4413 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4414 addend -= symbol;
4415 addend += sec->output_section->vma + sec->output_offset;
4418 /* MIPS16 text labels should be treated as odd. */
4419 if (ELF_ST_IS_MIPS16 (sym->st_other))
4420 ++symbol;
4422 /* Record the name of this symbol, for our caller. */
4423 *namep = bfd_elf_string_from_elf_section (input_bfd,
4424 symtab_hdr->sh_link,
4425 sym->st_name);
4426 if (*namep == '\0')
4427 *namep = bfd_section_name (input_bfd, sec);
4429 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
4431 else
4433 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4435 /* For global symbols we look up the symbol in the hash-table. */
4436 h = ((struct mips_elf_link_hash_entry *)
4437 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4438 /* Find the real hash-table entry for this symbol. */
4439 while (h->root.root.type == bfd_link_hash_indirect
4440 || h->root.root.type == bfd_link_hash_warning)
4441 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4443 /* Record the name of this symbol, for our caller. */
4444 *namep = h->root.root.root.string;
4446 /* See if this is the special _gp_disp symbol. Note that such a
4447 symbol must always be a global symbol. */
4448 if (strcmp (*namep, "_gp_disp") == 0
4449 && ! NEWABI_P (input_bfd))
4451 /* Relocations against _gp_disp are permitted only with
4452 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
4453 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
4454 return bfd_reloc_notsupported;
4456 gp_disp_p = TRUE;
4458 /* See if this is the special _gp symbol. Note that such a
4459 symbol must always be a global symbol. */
4460 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4461 gnu_local_gp_p = TRUE;
4464 /* If this symbol is defined, calculate its address. Note that
4465 _gp_disp is a magic symbol, always implicitly defined by the
4466 linker, so it's inappropriate to check to see whether or not
4467 its defined. */
4468 else if ((h->root.root.type == bfd_link_hash_defined
4469 || h->root.root.type == bfd_link_hash_defweak)
4470 && h->root.root.u.def.section)
4472 sec = h->root.root.u.def.section;
4473 if (sec->output_section)
4474 symbol = (h->root.root.u.def.value
4475 + sec->output_section->vma
4476 + sec->output_offset);
4477 else
4478 symbol = h->root.root.u.def.value;
4480 else if (h->root.root.type == bfd_link_hash_undefweak)
4481 /* We allow relocations against undefined weak symbols, giving
4482 it the value zero, so that you can undefined weak functions
4483 and check to see if they exist by looking at their
4484 addresses. */
4485 symbol = 0;
4486 else if (info->unresolved_syms_in_objects == RM_IGNORE
4487 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4488 symbol = 0;
4489 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
4490 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
4492 /* If this is a dynamic link, we should have created a
4493 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
4494 in in _bfd_mips_elf_create_dynamic_sections.
4495 Otherwise, we should define the symbol with a value of 0.
4496 FIXME: It should probably get into the symbol table
4497 somehow as well. */
4498 BFD_ASSERT (! info->shared);
4499 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
4500 symbol = 0;
4502 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
4504 /* This is an optional symbol - an Irix specific extension to the
4505 ELF spec. Ignore it for now.
4506 XXX - FIXME - there is more to the spec for OPTIONAL symbols
4507 than simply ignoring them, but we do not handle this for now.
4508 For information see the "64-bit ELF Object File Specification"
4509 which is available from here:
4510 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
4511 symbol = 0;
4513 else
4515 if (! ((*info->callbacks->undefined_symbol)
4516 (info, h->root.root.root.string, input_bfd,
4517 input_section, relocation->r_offset,
4518 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
4519 || ELF_ST_VISIBILITY (h->root.other))))
4520 return bfd_reloc_undefined;
4521 symbol = 0;
4524 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
4527 /* If this is a reference to a 16-bit function with a stub, we need
4528 to redirect the relocation to the stub unless:
4530 (a) the relocation is for a MIPS16 JAL;
4532 (b) the relocation is for a MIPS16 PIC call, and there are no
4533 non-MIPS16 uses of the GOT slot; or
4535 (c) the section allows direct references to MIPS16 functions. */
4536 if (r_type != R_MIPS16_26
4537 && !info->relocatable
4538 && ((h != NULL
4539 && h->fn_stub != NULL
4540 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
4541 || (local_p
4542 && elf_tdata (input_bfd)->local_stubs != NULL
4543 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4544 && !section_allows_mips16_refs_p (input_section))
4546 /* This is a 32- or 64-bit call to a 16-bit function. We should
4547 have already noticed that we were going to need the
4548 stub. */
4549 if (local_p)
4550 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
4551 else
4553 BFD_ASSERT (h->need_fn_stub);
4554 sec = h->fn_stub;
4557 symbol = sec->output_section->vma + sec->output_offset;
4558 /* The target is 16-bit, but the stub isn't. */
4559 target_is_16_bit_code_p = FALSE;
4561 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
4562 need to redirect the call to the stub. Note that we specifically
4563 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
4564 use an indirect stub instead. */
4565 else if (r_type == R_MIPS16_26 && !info->relocatable
4566 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
4567 || (local_p
4568 && elf_tdata (input_bfd)->local_call_stubs != NULL
4569 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
4570 && !target_is_16_bit_code_p)
4572 if (local_p)
4573 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
4574 else
4576 /* If both call_stub and call_fp_stub are defined, we can figure
4577 out which one to use by checking which one appears in the input
4578 file. */
4579 if (h->call_stub != NULL && h->call_fp_stub != NULL)
4581 asection *o;
4583 sec = NULL;
4584 for (o = input_bfd->sections; o != NULL; o = o->next)
4586 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
4588 sec = h->call_fp_stub;
4589 break;
4592 if (sec == NULL)
4593 sec = h->call_stub;
4595 else if (h->call_stub != NULL)
4596 sec = h->call_stub;
4597 else
4598 sec = h->call_fp_stub;
4601 BFD_ASSERT (sec->size > 0);
4602 symbol = sec->output_section->vma + sec->output_offset;
4605 /* Calls from 16-bit code to 32-bit code and vice versa require the
4606 special jalx instruction. */
4607 *require_jalxp = (!info->relocatable
4608 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
4609 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
4611 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4612 local_sections, TRUE);
4614 gp0 = _bfd_get_gp_value (input_bfd);
4615 gp = _bfd_get_gp_value (abfd);
4616 if (dynobj)
4617 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
4619 if (gnu_local_gp_p)
4620 symbol = gp;
4622 /* If we haven't already determined the GOT offset, oand we're going
4623 to need it, get it now. */
4624 switch (r_type)
4626 case R_MIPS_GOT_PAGE:
4627 case R_MIPS_GOT_OFST:
4628 /* We need to decay to GOT_DISP/addend if the symbol doesn't
4629 bind locally. */
4630 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
4631 if (local_p || r_type == R_MIPS_GOT_OFST)
4632 break;
4633 /* Fall through. */
4635 case R_MIPS16_CALL16:
4636 case R_MIPS16_GOT16:
4637 case R_MIPS_CALL16:
4638 case R_MIPS_GOT16:
4639 case R_MIPS_GOT_DISP:
4640 case R_MIPS_GOT_HI16:
4641 case R_MIPS_CALL_HI16:
4642 case R_MIPS_GOT_LO16:
4643 case R_MIPS_CALL_LO16:
4644 case R_MIPS_TLS_GD:
4645 case R_MIPS_TLS_GOTTPREL:
4646 case R_MIPS_TLS_LDM:
4647 /* Find the index into the GOT where this value is located. */
4648 if (r_type == R_MIPS_TLS_LDM)
4650 g = mips_elf_local_got_index (abfd, input_bfd, info,
4651 0, 0, NULL, r_type);
4652 if (g == MINUS_ONE)
4653 return bfd_reloc_outofrange;
4655 else if (!local_p)
4657 /* On VxWorks, CALL relocations should refer to the .got.plt
4658 entry, which is initialized to point at the PLT stub. */
4659 if (htab->is_vxworks
4660 && (r_type == R_MIPS_CALL_HI16
4661 || r_type == R_MIPS_CALL_LO16
4662 || call16_reloc_p (r_type)))
4664 BFD_ASSERT (addend == 0);
4665 BFD_ASSERT (h->root.needs_plt);
4666 g = mips_elf_gotplt_index (info, &h->root);
4668 else
4670 /* GOT_PAGE may take a non-zero addend, that is ignored in a
4671 GOT_PAGE relocation that decays to GOT_DISP because the
4672 symbol turns out to be global. The addend is then added
4673 as GOT_OFST. */
4674 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
4675 g = mips_elf_global_got_index (dynobj, input_bfd,
4676 &h->root, r_type, info);
4677 if (h->tls_type == GOT_NORMAL
4678 && (! elf_hash_table(info)->dynamic_sections_created
4679 || (info->shared
4680 && (info->symbolic || h->root.forced_local)
4681 && h->root.def_regular)))
4682 /* This is a static link or a -Bsymbolic link. The
4683 symbol is defined locally, or was forced to be local.
4684 We must initialize this entry in the GOT. */
4685 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
4688 else if (!htab->is_vxworks
4689 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
4690 /* The calculation below does not involve "g". */
4691 break;
4692 else
4694 g = mips_elf_local_got_index (abfd, input_bfd, info,
4695 symbol + addend, r_symndx, h, r_type);
4696 if (g == MINUS_ONE)
4697 return bfd_reloc_outofrange;
4700 /* Convert GOT indices to actual offsets. */
4701 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
4702 break;
4705 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
4706 symbols are resolved by the loader. Add them to .rela.dyn. */
4707 if (h != NULL && is_gott_symbol (info, &h->root))
4709 Elf_Internal_Rela outrel;
4710 bfd_byte *loc;
4711 asection *s;
4713 s = mips_elf_rel_dyn_section (info, FALSE);
4714 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4716 outrel.r_offset = (input_section->output_section->vma
4717 + input_section->output_offset
4718 + relocation->r_offset);
4719 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
4720 outrel.r_addend = addend;
4721 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
4723 /* If we've written this relocation for a readonly section,
4724 we need to set DF_TEXTREL again, so that we do not delete the
4725 DT_TEXTREL tag. */
4726 if (MIPS_ELF_READONLY_SECTION (input_section))
4727 info->flags |= DF_TEXTREL;
4729 *valuep = 0;
4730 return bfd_reloc_ok;
4733 /* Figure out what kind of relocation is being performed. */
4734 switch (r_type)
4736 case R_MIPS_NONE:
4737 return bfd_reloc_continue;
4739 case R_MIPS_16:
4740 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
4741 overflowed_p = mips_elf_overflow_p (value, 16);
4742 break;
4744 case R_MIPS_32:
4745 case R_MIPS_REL32:
4746 case R_MIPS_64:
4747 if ((info->shared
4748 || (!htab->is_vxworks
4749 && htab->root.dynamic_sections_created
4750 && h != NULL
4751 && h->root.def_dynamic
4752 && !h->root.def_regular))
4753 && r_symndx != 0
4754 && (h == NULL
4755 || h->root.root.type != bfd_link_hash_undefweak
4756 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
4757 && (input_section->flags & SEC_ALLOC) != 0)
4759 /* If we're creating a shared library, or this relocation is
4760 against a symbol in a shared library, then we can't know
4761 where the symbol will end up. So, we create a relocation
4762 record in the output, and leave the job up to the dynamic
4763 linker.
4765 In VxWorks executables, references to external symbols
4766 are handled using copy relocs or PLT stubs, so there's
4767 no need to add a dynamic relocation here. */
4768 value = addend;
4769 if (!mips_elf_create_dynamic_relocation (abfd,
4770 info,
4771 relocation,
4773 sec,
4774 symbol,
4775 &value,
4776 input_section))
4777 return bfd_reloc_undefined;
4779 else
4781 if (r_type != R_MIPS_REL32)
4782 value = symbol + addend;
4783 else
4784 value = addend;
4786 value &= howto->dst_mask;
4787 break;
4789 case R_MIPS_PC32:
4790 value = symbol + addend - p;
4791 value &= howto->dst_mask;
4792 break;
4794 case R_MIPS16_26:
4795 /* The calculation for R_MIPS16_26 is just the same as for an
4796 R_MIPS_26. It's only the storage of the relocated field into
4797 the output file that's different. That's handled in
4798 mips_elf_perform_relocation. So, we just fall through to the
4799 R_MIPS_26 case here. */
4800 case R_MIPS_26:
4801 if (local_p)
4802 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
4803 else
4805 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
4806 if (h->root.root.type != bfd_link_hash_undefweak)
4807 overflowed_p = (value >> 26) != ((p + 4) >> 28);
4809 value &= howto->dst_mask;
4810 break;
4812 case R_MIPS_TLS_DTPREL_HI16:
4813 value = (mips_elf_high (addend + symbol - dtprel_base (info))
4814 & howto->dst_mask);
4815 break;
4817 case R_MIPS_TLS_DTPREL_LO16:
4818 case R_MIPS_TLS_DTPREL32:
4819 case R_MIPS_TLS_DTPREL64:
4820 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
4821 break;
4823 case R_MIPS_TLS_TPREL_HI16:
4824 value = (mips_elf_high (addend + symbol - tprel_base (info))
4825 & howto->dst_mask);
4826 break;
4828 case R_MIPS_TLS_TPREL_LO16:
4829 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
4830 break;
4832 case R_MIPS_HI16:
4833 case R_MIPS16_HI16:
4834 if (!gp_disp_p)
4836 value = mips_elf_high (addend + symbol);
4837 value &= howto->dst_mask;
4839 else
4841 /* For MIPS16 ABI code we generate this sequence
4842 0: li $v0,%hi(_gp_disp)
4843 4: addiupc $v1,%lo(_gp_disp)
4844 8: sll $v0,16
4845 12: addu $v0,$v1
4846 14: move $gp,$v0
4847 So the offsets of hi and lo relocs are the same, but the
4848 $pc is four higher than $t9 would be, so reduce
4849 both reloc addends by 4. */
4850 if (r_type == R_MIPS16_HI16)
4851 value = mips_elf_high (addend + gp - p - 4);
4852 else
4853 value = mips_elf_high (addend + gp - p);
4854 overflowed_p = mips_elf_overflow_p (value, 16);
4856 break;
4858 case R_MIPS_LO16:
4859 case R_MIPS16_LO16:
4860 if (!gp_disp_p)
4861 value = (symbol + addend) & howto->dst_mask;
4862 else
4864 /* See the comment for R_MIPS16_HI16 above for the reason
4865 for this conditional. */
4866 if (r_type == R_MIPS16_LO16)
4867 value = addend + gp - p;
4868 else
4869 value = addend + gp - p + 4;
4870 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
4871 for overflow. But, on, say, IRIX5, relocations against
4872 _gp_disp are normally generated from the .cpload
4873 pseudo-op. It generates code that normally looks like
4874 this:
4876 lui $gp,%hi(_gp_disp)
4877 addiu $gp,$gp,%lo(_gp_disp)
4878 addu $gp,$gp,$t9
4880 Here $t9 holds the address of the function being called,
4881 as required by the MIPS ELF ABI. The R_MIPS_LO16
4882 relocation can easily overflow in this situation, but the
4883 R_MIPS_HI16 relocation will handle the overflow.
4884 Therefore, we consider this a bug in the MIPS ABI, and do
4885 not check for overflow here. */
4887 break;
4889 case R_MIPS_LITERAL:
4890 /* Because we don't merge literal sections, we can handle this
4891 just like R_MIPS_GPREL16. In the long run, we should merge
4892 shared literals, and then we will need to additional work
4893 here. */
4895 /* Fall through. */
4897 case R_MIPS16_GPREL:
4898 /* The R_MIPS16_GPREL performs the same calculation as
4899 R_MIPS_GPREL16, but stores the relocated bits in a different
4900 order. We don't need to do anything special here; the
4901 differences are handled in mips_elf_perform_relocation. */
4902 case R_MIPS_GPREL16:
4903 /* Only sign-extend the addend if it was extracted from the
4904 instruction. If the addend was separate, leave it alone,
4905 otherwise we may lose significant bits. */
4906 if (howto->partial_inplace)
4907 addend = _bfd_mips_elf_sign_extend (addend, 16);
4908 value = symbol + addend - gp;
4909 /* If the symbol was local, any earlier relocatable links will
4910 have adjusted its addend with the gp offset, so compensate
4911 for that now. Don't do it for symbols forced local in this
4912 link, though, since they won't have had the gp offset applied
4913 to them before. */
4914 if (was_local_p)
4915 value += gp0;
4916 overflowed_p = mips_elf_overflow_p (value, 16);
4917 break;
4919 case R_MIPS16_GOT16:
4920 case R_MIPS16_CALL16:
4921 case R_MIPS_GOT16:
4922 case R_MIPS_CALL16:
4923 /* VxWorks does not have separate local and global semantics for
4924 R_MIPS*_GOT16; every relocation evaluates to "G". */
4925 if (!htab->is_vxworks && local_p)
4927 bfd_boolean forced;
4929 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
4930 local_sections, FALSE);
4931 value = mips_elf_got16_entry (abfd, input_bfd, info,
4932 symbol + addend, forced);
4933 if (value == MINUS_ONE)
4934 return bfd_reloc_outofrange;
4935 value
4936 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
4937 overflowed_p = mips_elf_overflow_p (value, 16);
4938 break;
4941 /* Fall through. */
4943 case R_MIPS_TLS_GD:
4944 case R_MIPS_TLS_GOTTPREL:
4945 case R_MIPS_TLS_LDM:
4946 case R_MIPS_GOT_DISP:
4947 got_disp:
4948 value = g;
4949 overflowed_p = mips_elf_overflow_p (value, 16);
4950 break;
4952 case R_MIPS_GPREL32:
4953 value = (addend + symbol + gp0 - gp);
4954 if (!save_addend)
4955 value &= howto->dst_mask;
4956 break;
4958 case R_MIPS_PC16:
4959 case R_MIPS_GNU_REL16_S2:
4960 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
4961 overflowed_p = mips_elf_overflow_p (value, 18);
4962 value >>= howto->rightshift;
4963 value &= howto->dst_mask;
4964 break;
4966 case R_MIPS_GOT_HI16:
4967 case R_MIPS_CALL_HI16:
4968 /* We're allowed to handle these two relocations identically.
4969 The dynamic linker is allowed to handle the CALL relocations
4970 differently by creating a lazy evaluation stub. */
4971 value = g;
4972 value = mips_elf_high (value);
4973 value &= howto->dst_mask;
4974 break;
4976 case R_MIPS_GOT_LO16:
4977 case R_MIPS_CALL_LO16:
4978 value = g & howto->dst_mask;
4979 break;
4981 case R_MIPS_GOT_PAGE:
4982 /* GOT_PAGE relocations that reference non-local symbols decay
4983 to GOT_DISP. The corresponding GOT_OFST relocation decays to
4984 0. */
4985 if (! local_p)
4986 goto got_disp;
4987 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
4988 if (value == MINUS_ONE)
4989 return bfd_reloc_outofrange;
4990 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
4991 overflowed_p = mips_elf_overflow_p (value, 16);
4992 break;
4994 case R_MIPS_GOT_OFST:
4995 if (local_p)
4996 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
4997 else
4998 value = addend;
4999 overflowed_p = mips_elf_overflow_p (value, 16);
5000 break;
5002 case R_MIPS_SUB:
5003 value = symbol - addend;
5004 value &= howto->dst_mask;
5005 break;
5007 case R_MIPS_HIGHER:
5008 value = mips_elf_higher (addend + symbol);
5009 value &= howto->dst_mask;
5010 break;
5012 case R_MIPS_HIGHEST:
5013 value = mips_elf_highest (addend + symbol);
5014 value &= howto->dst_mask;
5015 break;
5017 case R_MIPS_SCN_DISP:
5018 value = symbol + addend - sec->output_offset;
5019 value &= howto->dst_mask;
5020 break;
5022 case R_MIPS_JALR:
5023 /* This relocation is only a hint. In some cases, we optimize
5024 it into a bal instruction. But we don't try to optimize
5025 branches to the PLT; that will wind up wasting time. */
5026 if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
5027 return bfd_reloc_continue;
5028 value = symbol + addend;
5029 break;
5031 case R_MIPS_PJUMP:
5032 case R_MIPS_GNU_VTINHERIT:
5033 case R_MIPS_GNU_VTENTRY:
5034 /* We don't do anything with these at present. */
5035 return bfd_reloc_continue;
5037 default:
5038 /* An unrecognized relocation type. */
5039 return bfd_reloc_notsupported;
5042 /* Store the VALUE for our caller. */
5043 *valuep = value;
5044 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5047 /* Obtain the field relocated by RELOCATION. */
5049 static bfd_vma
5050 mips_elf_obtain_contents (reloc_howto_type *howto,
5051 const Elf_Internal_Rela *relocation,
5052 bfd *input_bfd, bfd_byte *contents)
5054 bfd_vma x;
5055 bfd_byte *location = contents + relocation->r_offset;
5057 /* Obtain the bytes. */
5058 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5060 return x;
5063 /* It has been determined that the result of the RELOCATION is the
5064 VALUE. Use HOWTO to place VALUE into the output file at the
5065 appropriate position. The SECTION is the section to which the
5066 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
5067 for the relocation must be either JAL or JALX, and it is
5068 unconditionally converted to JALX.
5070 Returns FALSE if anything goes wrong. */
5072 static bfd_boolean
5073 mips_elf_perform_relocation (struct bfd_link_info *info,
5074 reloc_howto_type *howto,
5075 const Elf_Internal_Rela *relocation,
5076 bfd_vma value, bfd *input_bfd,
5077 asection *input_section, bfd_byte *contents,
5078 bfd_boolean require_jalx)
5080 bfd_vma x;
5081 bfd_byte *location;
5082 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5084 /* Figure out where the relocation is occurring. */
5085 location = contents + relocation->r_offset;
5087 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5089 /* Obtain the current value. */
5090 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5092 /* Clear the field we are setting. */
5093 x &= ~howto->dst_mask;
5095 /* Set the field. */
5096 x |= (value & howto->dst_mask);
5098 /* If required, turn JAL into JALX. */
5099 if (require_jalx)
5101 bfd_boolean ok;
5102 bfd_vma opcode = x >> 26;
5103 bfd_vma jalx_opcode;
5105 /* Check to see if the opcode is already JAL or JALX. */
5106 if (r_type == R_MIPS16_26)
5108 ok = ((opcode == 0x6) || (opcode == 0x7));
5109 jalx_opcode = 0x7;
5111 else
5113 ok = ((opcode == 0x3) || (opcode == 0x1d));
5114 jalx_opcode = 0x1d;
5117 /* If the opcode is not JAL or JALX, there's a problem. */
5118 if (!ok)
5120 (*_bfd_error_handler)
5121 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
5122 input_bfd,
5123 input_section,
5124 (unsigned long) relocation->r_offset);
5125 bfd_set_error (bfd_error_bad_value);
5126 return FALSE;
5129 /* Make this the JALX opcode. */
5130 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5133 /* On the RM9000, bal is faster than jal, because bal uses branch
5134 prediction hardware. If we are linking for the RM9000, and we
5135 see jal, and bal fits, use it instead. Note that this
5136 transformation should be safe for all architectures. */
5137 if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
5138 && !info->relocatable
5139 && !require_jalx
5140 && ((r_type == R_MIPS_26 && (x >> 26) == 0x3) /* jal addr */
5141 || (r_type == R_MIPS_JALR && x == 0x0320f809))) /* jalr t9 */
5143 bfd_vma addr;
5144 bfd_vma dest;
5145 bfd_signed_vma off;
5147 addr = (input_section->output_section->vma
5148 + input_section->output_offset
5149 + relocation->r_offset
5150 + 4);
5151 if (r_type == R_MIPS_26)
5152 dest = (value << 2) | ((addr >> 28) << 28);
5153 else
5154 dest = value;
5155 off = dest - addr;
5156 if (off <= 0x1ffff && off >= -0x20000)
5157 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5160 /* Put the value into the output. */
5161 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5163 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5164 location);
5166 return TRUE;
5169 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5170 is the original relocation, which is now being transformed into a
5171 dynamic relocation. The ADDENDP is adjusted if necessary; the
5172 caller should store the result in place of the original addend. */
5174 static bfd_boolean
5175 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5176 struct bfd_link_info *info,
5177 const Elf_Internal_Rela *rel,
5178 struct mips_elf_link_hash_entry *h,
5179 asection *sec, bfd_vma symbol,
5180 bfd_vma *addendp, asection *input_section)
5182 Elf_Internal_Rela outrel[3];
5183 asection *sreloc;
5184 bfd *dynobj;
5185 int r_type;
5186 long indx;
5187 bfd_boolean defined_p;
5188 struct mips_elf_link_hash_table *htab;
5190 htab = mips_elf_hash_table (info);
5191 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5192 dynobj = elf_hash_table (info)->dynobj;
5193 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5194 BFD_ASSERT (sreloc != NULL);
5195 BFD_ASSERT (sreloc->contents != NULL);
5196 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5197 < sreloc->size);
5199 outrel[0].r_offset =
5200 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5201 if (ABI_64_P (output_bfd))
5203 outrel[1].r_offset =
5204 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5205 outrel[2].r_offset =
5206 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5209 if (outrel[0].r_offset == MINUS_ONE)
5210 /* The relocation field has been deleted. */
5211 return TRUE;
5213 if (outrel[0].r_offset == MINUS_TWO)
5215 /* The relocation field has been converted into a relative value of
5216 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5217 the field to be fully relocated, so add in the symbol's value. */
5218 *addendp += symbol;
5219 return TRUE;
5222 /* We must now calculate the dynamic symbol table index to use
5223 in the relocation. */
5224 if (h != NULL
5225 && (!h->root.def_regular
5226 || (info->shared && !info->symbolic && !h->root.forced_local)))
5228 indx = h->root.dynindx;
5229 if (SGI_COMPAT (output_bfd))
5230 defined_p = h->root.def_regular;
5231 else
5232 /* ??? glibc's ld.so just adds the final GOT entry to the
5233 relocation field. It therefore treats relocs against
5234 defined symbols in the same way as relocs against
5235 undefined symbols. */
5236 defined_p = FALSE;
5238 else
5240 if (sec != NULL && bfd_is_abs_section (sec))
5241 indx = 0;
5242 else if (sec == NULL || sec->owner == NULL)
5244 bfd_set_error (bfd_error_bad_value);
5245 return FALSE;
5247 else
5249 indx = elf_section_data (sec->output_section)->dynindx;
5250 if (indx == 0)
5252 asection *osec = htab->root.text_index_section;
5253 indx = elf_section_data (osec)->dynindx;
5255 if (indx == 0)
5256 abort ();
5259 /* Instead of generating a relocation using the section
5260 symbol, we may as well make it a fully relative
5261 relocation. We want to avoid generating relocations to
5262 local symbols because we used to generate them
5263 incorrectly, without adding the original symbol value,
5264 which is mandated by the ABI for section symbols. In
5265 order to give dynamic loaders and applications time to
5266 phase out the incorrect use, we refrain from emitting
5267 section-relative relocations. It's not like they're
5268 useful, after all. This should be a bit more efficient
5269 as well. */
5270 /* ??? Although this behavior is compatible with glibc's ld.so,
5271 the ABI says that relocations against STN_UNDEF should have
5272 a symbol value of 0. Irix rld honors this, so relocations
5273 against STN_UNDEF have no effect. */
5274 if (!SGI_COMPAT (output_bfd))
5275 indx = 0;
5276 defined_p = TRUE;
5279 /* If the relocation was previously an absolute relocation and
5280 this symbol will not be referred to by the relocation, we must
5281 adjust it by the value we give it in the dynamic symbol table.
5282 Otherwise leave the job up to the dynamic linker. */
5283 if (defined_p && r_type != R_MIPS_REL32)
5284 *addendp += symbol;
5286 if (htab->is_vxworks)
5287 /* VxWorks uses non-relative relocations for this. */
5288 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5289 else
5290 /* The relocation is always an REL32 relocation because we don't
5291 know where the shared library will wind up at load-time. */
5292 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5293 R_MIPS_REL32);
5295 /* For strict adherence to the ABI specification, we should
5296 generate a R_MIPS_64 relocation record by itself before the
5297 _REL32/_64 record as well, such that the addend is read in as
5298 a 64-bit value (REL32 is a 32-bit relocation, after all).
5299 However, since none of the existing ELF64 MIPS dynamic
5300 loaders seems to care, we don't waste space with these
5301 artificial relocations. If this turns out to not be true,
5302 mips_elf_allocate_dynamic_relocation() should be tweaked so
5303 as to make room for a pair of dynamic relocations per
5304 invocation if ABI_64_P, and here we should generate an
5305 additional relocation record with R_MIPS_64 by itself for a
5306 NULL symbol before this relocation record. */
5307 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5308 ABI_64_P (output_bfd)
5309 ? R_MIPS_64
5310 : R_MIPS_NONE);
5311 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5313 /* Adjust the output offset of the relocation to reference the
5314 correct location in the output file. */
5315 outrel[0].r_offset += (input_section->output_section->vma
5316 + input_section->output_offset);
5317 outrel[1].r_offset += (input_section->output_section->vma
5318 + input_section->output_offset);
5319 outrel[2].r_offset += (input_section->output_section->vma
5320 + input_section->output_offset);
5322 /* Put the relocation back out. We have to use the special
5323 relocation outputter in the 64-bit case since the 64-bit
5324 relocation format is non-standard. */
5325 if (ABI_64_P (output_bfd))
5327 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5328 (output_bfd, &outrel[0],
5329 (sreloc->contents
5330 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5332 else if (htab->is_vxworks)
5334 /* VxWorks uses RELA rather than REL dynamic relocations. */
5335 outrel[0].r_addend = *addendp;
5336 bfd_elf32_swap_reloca_out
5337 (output_bfd, &outrel[0],
5338 (sreloc->contents
5339 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5341 else
5342 bfd_elf32_swap_reloc_out
5343 (output_bfd, &outrel[0],
5344 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
5346 /* We've now added another relocation. */
5347 ++sreloc->reloc_count;
5349 /* Make sure the output section is writable. The dynamic linker
5350 will be writing to it. */
5351 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5352 |= SHF_WRITE;
5354 /* On IRIX5, make an entry of compact relocation info. */
5355 if (IRIX_COMPAT (output_bfd) == ict_irix5)
5357 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5358 bfd_byte *cr;
5360 if (scpt)
5362 Elf32_crinfo cptrel;
5364 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5365 cptrel.vaddr = (rel->r_offset
5366 + input_section->output_section->vma
5367 + input_section->output_offset);
5368 if (r_type == R_MIPS_REL32)
5369 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5370 else
5371 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5372 mips_elf_set_cr_dist2to (cptrel, 0);
5373 cptrel.konst = *addendp;
5375 cr = (scpt->contents
5376 + sizeof (Elf32_External_compact_rel));
5377 mips_elf_set_cr_relvaddr (cptrel, 0);
5378 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5379 ((Elf32_External_crinfo *) cr
5380 + scpt->reloc_count));
5381 ++scpt->reloc_count;
5385 /* If we've written this relocation for a readonly section,
5386 we need to set DF_TEXTREL again, so that we do not delete the
5387 DT_TEXTREL tag. */
5388 if (MIPS_ELF_READONLY_SECTION (input_section))
5389 info->flags |= DF_TEXTREL;
5391 return TRUE;
5394 /* Return the MACH for a MIPS e_flags value. */
5396 unsigned long
5397 _bfd_elf_mips_mach (flagword flags)
5399 switch (flags & EF_MIPS_MACH)
5401 case E_MIPS_MACH_3900:
5402 return bfd_mach_mips3900;
5404 case E_MIPS_MACH_4010:
5405 return bfd_mach_mips4010;
5407 case E_MIPS_MACH_4100:
5408 return bfd_mach_mips4100;
5410 case E_MIPS_MACH_4111:
5411 return bfd_mach_mips4111;
5413 case E_MIPS_MACH_4120:
5414 return bfd_mach_mips4120;
5416 case E_MIPS_MACH_4650:
5417 return bfd_mach_mips4650;
5419 case E_MIPS_MACH_5400:
5420 return bfd_mach_mips5400;
5422 case E_MIPS_MACH_5500:
5423 return bfd_mach_mips5500;
5425 case E_MIPS_MACH_9000:
5426 return bfd_mach_mips9000;
5428 case E_MIPS_MACH_SB1:
5429 return bfd_mach_mips_sb1;
5431 case E_MIPS_MACH_LS2E:
5432 return bfd_mach_mips_loongson_2e;
5434 case E_MIPS_MACH_LS2F:
5435 return bfd_mach_mips_loongson_2f;
5437 case E_MIPS_MACH_OCTEON:
5438 return bfd_mach_mips_octeon;
5440 default:
5441 switch (flags & EF_MIPS_ARCH)
5443 default:
5444 case E_MIPS_ARCH_1:
5445 return bfd_mach_mips3000;
5447 case E_MIPS_ARCH_2:
5448 return bfd_mach_mips6000;
5450 case E_MIPS_ARCH_3:
5451 return bfd_mach_mips4000;
5453 case E_MIPS_ARCH_4:
5454 return bfd_mach_mips8000;
5456 case E_MIPS_ARCH_5:
5457 return bfd_mach_mips5;
5459 case E_MIPS_ARCH_32:
5460 return bfd_mach_mipsisa32;
5462 case E_MIPS_ARCH_64:
5463 return bfd_mach_mipsisa64;
5465 case E_MIPS_ARCH_32R2:
5466 return bfd_mach_mipsisa32r2;
5468 case E_MIPS_ARCH_64R2:
5469 return bfd_mach_mipsisa64r2;
5473 return 0;
5476 /* Return printable name for ABI. */
5478 static INLINE char *
5479 elf_mips_abi_name (bfd *abfd)
5481 flagword flags;
5483 flags = elf_elfheader (abfd)->e_flags;
5484 switch (flags & EF_MIPS_ABI)
5486 case 0:
5487 if (ABI_N32_P (abfd))
5488 return "N32";
5489 else if (ABI_64_P (abfd))
5490 return "64";
5491 else
5492 return "none";
5493 case E_MIPS_ABI_O32:
5494 return "O32";
5495 case E_MIPS_ABI_O64:
5496 return "O64";
5497 case E_MIPS_ABI_EABI32:
5498 return "EABI32";
5499 case E_MIPS_ABI_EABI64:
5500 return "EABI64";
5501 default:
5502 return "unknown abi";
5506 /* MIPS ELF uses two common sections. One is the usual one, and the
5507 other is for small objects. All the small objects are kept
5508 together, and then referenced via the gp pointer, which yields
5509 faster assembler code. This is what we use for the small common
5510 section. This approach is copied from ecoff.c. */
5511 static asection mips_elf_scom_section;
5512 static asymbol mips_elf_scom_symbol;
5513 static asymbol *mips_elf_scom_symbol_ptr;
5515 /* MIPS ELF also uses an acommon section, which represents an
5516 allocated common symbol which may be overridden by a
5517 definition in a shared library. */
5518 static asection mips_elf_acom_section;
5519 static asymbol mips_elf_acom_symbol;
5520 static asymbol *mips_elf_acom_symbol_ptr;
5522 /* This is used for both the 32-bit and the 64-bit ABI. */
5524 void
5525 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
5527 elf_symbol_type *elfsym;
5529 /* Handle the special MIPS section numbers that a symbol may use. */
5530 elfsym = (elf_symbol_type *) asym;
5531 switch (elfsym->internal_elf_sym.st_shndx)
5533 case SHN_MIPS_ACOMMON:
5534 /* This section is used in a dynamically linked executable file.
5535 It is an allocated common section. The dynamic linker can
5536 either resolve these symbols to something in a shared
5537 library, or it can just leave them here. For our purposes,
5538 we can consider these symbols to be in a new section. */
5539 if (mips_elf_acom_section.name == NULL)
5541 /* Initialize the acommon section. */
5542 mips_elf_acom_section.name = ".acommon";
5543 mips_elf_acom_section.flags = SEC_ALLOC;
5544 mips_elf_acom_section.output_section = &mips_elf_acom_section;
5545 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
5546 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
5547 mips_elf_acom_symbol.name = ".acommon";
5548 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
5549 mips_elf_acom_symbol.section = &mips_elf_acom_section;
5550 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
5552 asym->section = &mips_elf_acom_section;
5553 break;
5555 case SHN_COMMON:
5556 /* Common symbols less than the GP size are automatically
5557 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
5558 if (asym->value > elf_gp_size (abfd)
5559 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
5560 || IRIX_COMPAT (abfd) == ict_irix6)
5561 break;
5562 /* Fall through. */
5563 case SHN_MIPS_SCOMMON:
5564 if (mips_elf_scom_section.name == NULL)
5566 /* Initialize the small common section. */
5567 mips_elf_scom_section.name = ".scommon";
5568 mips_elf_scom_section.flags = SEC_IS_COMMON;
5569 mips_elf_scom_section.output_section = &mips_elf_scom_section;
5570 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
5571 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
5572 mips_elf_scom_symbol.name = ".scommon";
5573 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
5574 mips_elf_scom_symbol.section = &mips_elf_scom_section;
5575 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
5577 asym->section = &mips_elf_scom_section;
5578 asym->value = elfsym->internal_elf_sym.st_size;
5579 break;
5581 case SHN_MIPS_SUNDEFINED:
5582 asym->section = bfd_und_section_ptr;
5583 break;
5585 case SHN_MIPS_TEXT:
5587 asection *section = bfd_get_section_by_name (abfd, ".text");
5589 BFD_ASSERT (SGI_COMPAT (abfd));
5590 if (section != NULL)
5592 asym->section = section;
5593 /* MIPS_TEXT is a bit special, the address is not an offset
5594 to the base of the .text section. So substract the section
5595 base address to make it an offset. */
5596 asym->value -= section->vma;
5599 break;
5601 case SHN_MIPS_DATA:
5603 asection *section = bfd_get_section_by_name (abfd, ".data");
5605 BFD_ASSERT (SGI_COMPAT (abfd));
5606 if (section != NULL)
5608 asym->section = section;
5609 /* MIPS_DATA is a bit special, the address is not an offset
5610 to the base of the .data section. So substract the section
5611 base address to make it an offset. */
5612 asym->value -= section->vma;
5615 break;
5618 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
5619 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
5620 && (asym->value & 1) != 0)
5622 asym->value--;
5623 elfsym->internal_elf_sym.st_other
5624 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
5628 /* Implement elf_backend_eh_frame_address_size. This differs from
5629 the default in the way it handles EABI64.
5631 EABI64 was originally specified as an LP64 ABI, and that is what
5632 -mabi=eabi normally gives on a 64-bit target. However, gcc has
5633 historically accepted the combination of -mabi=eabi and -mlong32,
5634 and this ILP32 variation has become semi-official over time.
5635 Both forms use elf32 and have pointer-sized FDE addresses.
5637 If an EABI object was generated by GCC 4.0 or above, it will have
5638 an empty .gcc_compiled_longXX section, where XX is the size of longs
5639 in bits. Unfortunately, ILP32 objects generated by earlier compilers
5640 have no special marking to distinguish them from LP64 objects.
5642 We don't want users of the official LP64 ABI to be punished for the
5643 existence of the ILP32 variant, but at the same time, we don't want
5644 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
5645 We therefore take the following approach:
5647 - If ABFD contains a .gcc_compiled_longXX section, use it to
5648 determine the pointer size.
5650 - Otherwise check the type of the first relocation. Assume that
5651 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
5653 - Otherwise punt.
5655 The second check is enough to detect LP64 objects generated by pre-4.0
5656 compilers because, in the kind of output generated by those compilers,
5657 the first relocation will be associated with either a CIE personality
5658 routine or an FDE start address. Furthermore, the compilers never
5659 used a special (non-pointer) encoding for this ABI.
5661 Checking the relocation type should also be safe because there is no
5662 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
5663 did so. */
5665 unsigned int
5666 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
5668 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5669 return 8;
5670 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
5672 bfd_boolean long32_p, long64_p;
5674 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
5675 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
5676 if (long32_p && long64_p)
5677 return 0;
5678 if (long32_p)
5679 return 4;
5680 if (long64_p)
5681 return 8;
5683 if (sec->reloc_count > 0
5684 && elf_section_data (sec)->relocs != NULL
5685 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
5686 == R_MIPS_64))
5687 return 8;
5689 return 0;
5691 return 4;
5694 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
5695 relocations against two unnamed section symbols to resolve to the
5696 same address. For example, if we have code like:
5698 lw $4,%got_disp(.data)($gp)
5699 lw $25,%got_disp(.text)($gp)
5700 jalr $25
5702 then the linker will resolve both relocations to .data and the program
5703 will jump there rather than to .text.
5705 We can work around this problem by giving names to local section symbols.
5706 This is also what the MIPSpro tools do. */
5708 bfd_boolean
5709 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
5711 return SGI_COMPAT (abfd);
5714 /* Work over a section just before writing it out. This routine is
5715 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
5716 sections that need the SHF_MIPS_GPREL flag by name; there has to be
5717 a better way. */
5719 bfd_boolean
5720 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
5722 if (hdr->sh_type == SHT_MIPS_REGINFO
5723 && hdr->sh_size > 0)
5725 bfd_byte buf[4];
5727 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
5728 BFD_ASSERT (hdr->contents == NULL);
5730 if (bfd_seek (abfd,
5731 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
5732 SEEK_SET) != 0)
5733 return FALSE;
5734 H_PUT_32 (abfd, elf_gp (abfd), buf);
5735 if (bfd_bwrite (buf, 4, abfd) != 4)
5736 return FALSE;
5739 if (hdr->sh_type == SHT_MIPS_OPTIONS
5740 && hdr->bfd_section != NULL
5741 && mips_elf_section_data (hdr->bfd_section) != NULL
5742 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
5744 bfd_byte *contents, *l, *lend;
5746 /* We stored the section contents in the tdata field in the
5747 set_section_contents routine. We save the section contents
5748 so that we don't have to read them again.
5749 At this point we know that elf_gp is set, so we can look
5750 through the section contents to see if there is an
5751 ODK_REGINFO structure. */
5753 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
5754 l = contents;
5755 lend = contents + hdr->sh_size;
5756 while (l + sizeof (Elf_External_Options) <= lend)
5758 Elf_Internal_Options intopt;
5760 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5761 &intopt);
5762 if (intopt.size < sizeof (Elf_External_Options))
5764 (*_bfd_error_handler)
5765 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5766 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5767 break;
5769 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5771 bfd_byte buf[8];
5773 if (bfd_seek (abfd,
5774 (hdr->sh_offset
5775 + (l - contents)
5776 + sizeof (Elf_External_Options)
5777 + (sizeof (Elf64_External_RegInfo) - 8)),
5778 SEEK_SET) != 0)
5779 return FALSE;
5780 H_PUT_64 (abfd, elf_gp (abfd), buf);
5781 if (bfd_bwrite (buf, 8, abfd) != 8)
5782 return FALSE;
5784 else if (intopt.kind == ODK_REGINFO)
5786 bfd_byte buf[4];
5788 if (bfd_seek (abfd,
5789 (hdr->sh_offset
5790 + (l - contents)
5791 + sizeof (Elf_External_Options)
5792 + (sizeof (Elf32_External_RegInfo) - 4)),
5793 SEEK_SET) != 0)
5794 return FALSE;
5795 H_PUT_32 (abfd, elf_gp (abfd), buf);
5796 if (bfd_bwrite (buf, 4, abfd) != 4)
5797 return FALSE;
5799 l += intopt.size;
5803 if (hdr->bfd_section != NULL)
5805 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5807 if (strcmp (name, ".sdata") == 0
5808 || strcmp (name, ".lit8") == 0
5809 || strcmp (name, ".lit4") == 0)
5811 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5812 hdr->sh_type = SHT_PROGBITS;
5814 else if (strcmp (name, ".sbss") == 0)
5816 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5817 hdr->sh_type = SHT_NOBITS;
5819 else if (strcmp (name, ".srdata") == 0)
5821 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
5822 hdr->sh_type = SHT_PROGBITS;
5824 else if (strcmp (name, ".compact_rel") == 0)
5826 hdr->sh_flags = 0;
5827 hdr->sh_type = SHT_PROGBITS;
5829 else if (strcmp (name, ".rtproc") == 0)
5831 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
5833 unsigned int adjust;
5835 adjust = hdr->sh_size % hdr->sh_addralign;
5836 if (adjust != 0)
5837 hdr->sh_size += hdr->sh_addralign - adjust;
5842 return TRUE;
5845 /* Handle a MIPS specific section when reading an object file. This
5846 is called when elfcode.h finds a section with an unknown type.
5847 This routine supports both the 32-bit and 64-bit ELF ABI.
5849 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
5850 how to. */
5852 bfd_boolean
5853 _bfd_mips_elf_section_from_shdr (bfd *abfd,
5854 Elf_Internal_Shdr *hdr,
5855 const char *name,
5856 int shindex)
5858 flagword flags = 0;
5860 /* There ought to be a place to keep ELF backend specific flags, but
5861 at the moment there isn't one. We just keep track of the
5862 sections by their name, instead. Fortunately, the ABI gives
5863 suggested names for all the MIPS specific sections, so we will
5864 probably get away with this. */
5865 switch (hdr->sh_type)
5867 case SHT_MIPS_LIBLIST:
5868 if (strcmp (name, ".liblist") != 0)
5869 return FALSE;
5870 break;
5871 case SHT_MIPS_MSYM:
5872 if (strcmp (name, ".msym") != 0)
5873 return FALSE;
5874 break;
5875 case SHT_MIPS_CONFLICT:
5876 if (strcmp (name, ".conflict") != 0)
5877 return FALSE;
5878 break;
5879 case SHT_MIPS_GPTAB:
5880 if (! CONST_STRNEQ (name, ".gptab."))
5881 return FALSE;
5882 break;
5883 case SHT_MIPS_UCODE:
5884 if (strcmp (name, ".ucode") != 0)
5885 return FALSE;
5886 break;
5887 case SHT_MIPS_DEBUG:
5888 if (strcmp (name, ".mdebug") != 0)
5889 return FALSE;
5890 flags = SEC_DEBUGGING;
5891 break;
5892 case SHT_MIPS_REGINFO:
5893 if (strcmp (name, ".reginfo") != 0
5894 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
5895 return FALSE;
5896 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
5897 break;
5898 case SHT_MIPS_IFACE:
5899 if (strcmp (name, ".MIPS.interfaces") != 0)
5900 return FALSE;
5901 break;
5902 case SHT_MIPS_CONTENT:
5903 if (! CONST_STRNEQ (name, ".MIPS.content"))
5904 return FALSE;
5905 break;
5906 case SHT_MIPS_OPTIONS:
5907 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
5908 return FALSE;
5909 break;
5910 case SHT_MIPS_DWARF:
5911 if (! CONST_STRNEQ (name, ".debug_")
5912 && ! CONST_STRNEQ (name, ".zdebug_"))
5913 return FALSE;
5914 break;
5915 case SHT_MIPS_SYMBOL_LIB:
5916 if (strcmp (name, ".MIPS.symlib") != 0)
5917 return FALSE;
5918 break;
5919 case SHT_MIPS_EVENTS:
5920 if (! CONST_STRNEQ (name, ".MIPS.events")
5921 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
5922 return FALSE;
5923 break;
5924 default:
5925 break;
5928 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5929 return FALSE;
5931 if (flags)
5933 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
5934 (bfd_get_section_flags (abfd,
5935 hdr->bfd_section)
5936 | flags)))
5937 return FALSE;
5940 /* FIXME: We should record sh_info for a .gptab section. */
5942 /* For a .reginfo section, set the gp value in the tdata information
5943 from the contents of this section. We need the gp value while
5944 processing relocs, so we just get it now. The .reginfo section
5945 is not used in the 64-bit MIPS ELF ABI. */
5946 if (hdr->sh_type == SHT_MIPS_REGINFO)
5948 Elf32_External_RegInfo ext;
5949 Elf32_RegInfo s;
5951 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
5952 &ext, 0, sizeof ext))
5953 return FALSE;
5954 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
5955 elf_gp (abfd) = s.ri_gp_value;
5958 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
5959 set the gp value based on what we find. We may see both
5960 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
5961 they should agree. */
5962 if (hdr->sh_type == SHT_MIPS_OPTIONS)
5964 bfd_byte *contents, *l, *lend;
5966 contents = bfd_malloc (hdr->sh_size);
5967 if (contents == NULL)
5968 return FALSE;
5969 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
5970 0, hdr->sh_size))
5972 free (contents);
5973 return FALSE;
5975 l = contents;
5976 lend = contents + hdr->sh_size;
5977 while (l + sizeof (Elf_External_Options) <= lend)
5979 Elf_Internal_Options intopt;
5981 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5982 &intopt);
5983 if (intopt.size < sizeof (Elf_External_Options))
5985 (*_bfd_error_handler)
5986 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5987 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5988 break;
5990 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5992 Elf64_Internal_RegInfo intreg;
5994 bfd_mips_elf64_swap_reginfo_in
5995 (abfd,
5996 ((Elf64_External_RegInfo *)
5997 (l + sizeof (Elf_External_Options))),
5998 &intreg);
5999 elf_gp (abfd) = intreg.ri_gp_value;
6001 else if (intopt.kind == ODK_REGINFO)
6003 Elf32_RegInfo intreg;
6005 bfd_mips_elf32_swap_reginfo_in
6006 (abfd,
6007 ((Elf32_External_RegInfo *)
6008 (l + sizeof (Elf_External_Options))),
6009 &intreg);
6010 elf_gp (abfd) = intreg.ri_gp_value;
6012 l += intopt.size;
6014 free (contents);
6017 return TRUE;
6020 /* Set the correct type for a MIPS ELF section. We do this by the
6021 section name, which is a hack, but ought to work. This routine is
6022 used by both the 32-bit and the 64-bit ABI. */
6024 bfd_boolean
6025 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6027 const char *name = bfd_get_section_name (abfd, sec);
6029 if (strcmp (name, ".liblist") == 0)
6031 hdr->sh_type = SHT_MIPS_LIBLIST;
6032 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6033 /* The sh_link field is set in final_write_processing. */
6035 else if (strcmp (name, ".conflict") == 0)
6036 hdr->sh_type = SHT_MIPS_CONFLICT;
6037 else if (CONST_STRNEQ (name, ".gptab."))
6039 hdr->sh_type = SHT_MIPS_GPTAB;
6040 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6041 /* The sh_info field is set in final_write_processing. */
6043 else if (strcmp (name, ".ucode") == 0)
6044 hdr->sh_type = SHT_MIPS_UCODE;
6045 else if (strcmp (name, ".mdebug") == 0)
6047 hdr->sh_type = SHT_MIPS_DEBUG;
6048 /* In a shared object on IRIX 5.3, the .mdebug section has an
6049 entsize of 0. FIXME: Does this matter? */
6050 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6051 hdr->sh_entsize = 0;
6052 else
6053 hdr->sh_entsize = 1;
6055 else if (strcmp (name, ".reginfo") == 0)
6057 hdr->sh_type = SHT_MIPS_REGINFO;
6058 /* In a shared object on IRIX 5.3, the .reginfo section has an
6059 entsize of 0x18. FIXME: Does this matter? */
6060 if (SGI_COMPAT (abfd))
6062 if ((abfd->flags & DYNAMIC) != 0)
6063 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6064 else
6065 hdr->sh_entsize = 1;
6067 else
6068 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6070 else if (SGI_COMPAT (abfd)
6071 && (strcmp (name, ".hash") == 0
6072 || strcmp (name, ".dynamic") == 0
6073 || strcmp (name, ".dynstr") == 0))
6075 if (SGI_COMPAT (abfd))
6076 hdr->sh_entsize = 0;
6077 #if 0
6078 /* This isn't how the IRIX6 linker behaves. */
6079 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6080 #endif
6082 else if (strcmp (name, ".got") == 0
6083 || strcmp (name, ".srdata") == 0
6084 || strcmp (name, ".sdata") == 0
6085 || strcmp (name, ".sbss") == 0
6086 || strcmp (name, ".lit4") == 0
6087 || strcmp (name, ".lit8") == 0)
6088 hdr->sh_flags |= SHF_MIPS_GPREL;
6089 else if (strcmp (name, ".MIPS.interfaces") == 0)
6091 hdr->sh_type = SHT_MIPS_IFACE;
6092 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6094 else if (CONST_STRNEQ (name, ".MIPS.content"))
6096 hdr->sh_type = SHT_MIPS_CONTENT;
6097 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6098 /* The sh_info field is set in final_write_processing. */
6100 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6102 hdr->sh_type = SHT_MIPS_OPTIONS;
6103 hdr->sh_entsize = 1;
6104 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6106 else if (CONST_STRNEQ (name, ".debug_")
6107 || CONST_STRNEQ (name, ".zdebug_"))
6109 hdr->sh_type = SHT_MIPS_DWARF;
6111 /* Irix facilities such as libexc expect a single .debug_frame
6112 per executable, the system ones have NOSTRIP set and the linker
6113 doesn't merge sections with different flags so ... */
6114 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6115 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6117 else if (strcmp (name, ".MIPS.symlib") == 0)
6119 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6120 /* The sh_link and sh_info fields are set in
6121 final_write_processing. */
6123 else if (CONST_STRNEQ (name, ".MIPS.events")
6124 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6126 hdr->sh_type = SHT_MIPS_EVENTS;
6127 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6128 /* The sh_link field is set in final_write_processing. */
6130 else if (strcmp (name, ".msym") == 0)
6132 hdr->sh_type = SHT_MIPS_MSYM;
6133 hdr->sh_flags |= SHF_ALLOC;
6134 hdr->sh_entsize = 8;
6137 /* The generic elf_fake_sections will set up REL_HDR using the default
6138 kind of relocations. We used to set up a second header for the
6139 non-default kind of relocations here, but only NewABI would use
6140 these, and the IRIX ld doesn't like resulting empty RELA sections.
6141 Thus we create those header only on demand now. */
6143 return TRUE;
6146 /* Given a BFD section, try to locate the corresponding ELF section
6147 index. This is used by both the 32-bit and the 64-bit ABI.
6148 Actually, it's not clear to me that the 64-bit ABI supports these,
6149 but for non-PIC objects we will certainly want support for at least
6150 the .scommon section. */
6152 bfd_boolean
6153 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6154 asection *sec, int *retval)
6156 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6158 *retval = SHN_MIPS_SCOMMON;
6159 return TRUE;
6161 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6163 *retval = SHN_MIPS_ACOMMON;
6164 return TRUE;
6166 return FALSE;
6169 /* Hook called by the linker routine which adds symbols from an object
6170 file. We must handle the special MIPS section numbers here. */
6172 bfd_boolean
6173 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6174 Elf_Internal_Sym *sym, const char **namep,
6175 flagword *flagsp ATTRIBUTE_UNUSED,
6176 asection **secp, bfd_vma *valp)
6178 if (SGI_COMPAT (abfd)
6179 && (abfd->flags & DYNAMIC) != 0
6180 && strcmp (*namep, "_rld_new_interface") == 0)
6182 /* Skip IRIX5 rld entry name. */
6183 *namep = NULL;
6184 return TRUE;
6187 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6188 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6189 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6190 a magic symbol resolved by the linker, we ignore this bogus definition
6191 of _gp_disp. New ABI objects do not suffer from this problem so this
6192 is not done for them. */
6193 if (!NEWABI_P(abfd)
6194 && (sym->st_shndx == SHN_ABS)
6195 && (strcmp (*namep, "_gp_disp") == 0))
6197 *namep = NULL;
6198 return TRUE;
6201 switch (sym->st_shndx)
6203 case SHN_COMMON:
6204 /* Common symbols less than the GP size are automatically
6205 treated as SHN_MIPS_SCOMMON symbols. */
6206 if (sym->st_size > elf_gp_size (abfd)
6207 || ELF_ST_TYPE (sym->st_info) == STT_TLS
6208 || IRIX_COMPAT (abfd) == ict_irix6)
6209 break;
6210 /* Fall through. */
6211 case SHN_MIPS_SCOMMON:
6212 *secp = bfd_make_section_old_way (abfd, ".scommon");
6213 (*secp)->flags |= SEC_IS_COMMON;
6214 *valp = sym->st_size;
6215 break;
6217 case SHN_MIPS_TEXT:
6218 /* This section is used in a shared object. */
6219 if (elf_tdata (abfd)->elf_text_section == NULL)
6221 asymbol *elf_text_symbol;
6222 asection *elf_text_section;
6223 bfd_size_type amt = sizeof (asection);
6225 elf_text_section = bfd_zalloc (abfd, amt);
6226 if (elf_text_section == NULL)
6227 return FALSE;
6229 amt = sizeof (asymbol);
6230 elf_text_symbol = bfd_zalloc (abfd, amt);
6231 if (elf_text_symbol == NULL)
6232 return FALSE;
6234 /* Initialize the section. */
6236 elf_tdata (abfd)->elf_text_section = elf_text_section;
6237 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6239 elf_text_section->symbol = elf_text_symbol;
6240 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6242 elf_text_section->name = ".text";
6243 elf_text_section->flags = SEC_NO_FLAGS;
6244 elf_text_section->output_section = NULL;
6245 elf_text_section->owner = abfd;
6246 elf_text_symbol->name = ".text";
6247 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6248 elf_text_symbol->section = elf_text_section;
6250 /* This code used to do *secp = bfd_und_section_ptr if
6251 info->shared. I don't know why, and that doesn't make sense,
6252 so I took it out. */
6253 *secp = elf_tdata (abfd)->elf_text_section;
6254 break;
6256 case SHN_MIPS_ACOMMON:
6257 /* Fall through. XXX Can we treat this as allocated data? */
6258 case SHN_MIPS_DATA:
6259 /* This section is used in a shared object. */
6260 if (elf_tdata (abfd)->elf_data_section == NULL)
6262 asymbol *elf_data_symbol;
6263 asection *elf_data_section;
6264 bfd_size_type amt = sizeof (asection);
6266 elf_data_section = bfd_zalloc (abfd, amt);
6267 if (elf_data_section == NULL)
6268 return FALSE;
6270 amt = sizeof (asymbol);
6271 elf_data_symbol = bfd_zalloc (abfd, amt);
6272 if (elf_data_symbol == NULL)
6273 return FALSE;
6275 /* Initialize the section. */
6277 elf_tdata (abfd)->elf_data_section = elf_data_section;
6278 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6280 elf_data_section->symbol = elf_data_symbol;
6281 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6283 elf_data_section->name = ".data";
6284 elf_data_section->flags = SEC_NO_FLAGS;
6285 elf_data_section->output_section = NULL;
6286 elf_data_section->owner = abfd;
6287 elf_data_symbol->name = ".data";
6288 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6289 elf_data_symbol->section = elf_data_section;
6291 /* This code used to do *secp = bfd_und_section_ptr if
6292 info->shared. I don't know why, and that doesn't make sense,
6293 so I took it out. */
6294 *secp = elf_tdata (abfd)->elf_data_section;
6295 break;
6297 case SHN_MIPS_SUNDEFINED:
6298 *secp = bfd_und_section_ptr;
6299 break;
6302 if (SGI_COMPAT (abfd)
6303 && ! info->shared
6304 && info->output_bfd->xvec == abfd->xvec
6305 && strcmp (*namep, "__rld_obj_head") == 0)
6307 struct elf_link_hash_entry *h;
6308 struct bfd_link_hash_entry *bh;
6310 /* Mark __rld_obj_head as dynamic. */
6311 bh = NULL;
6312 if (! (_bfd_generic_link_add_one_symbol
6313 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
6314 get_elf_backend_data (abfd)->collect, &bh)))
6315 return FALSE;
6317 h = (struct elf_link_hash_entry *) bh;
6318 h->non_elf = 0;
6319 h->def_regular = 1;
6320 h->type = STT_OBJECT;
6322 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6323 return FALSE;
6325 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
6328 /* If this is a mips16 text symbol, add 1 to the value to make it
6329 odd. This will cause something like .word SYM to come up with
6330 the right value when it is loaded into the PC. */
6331 if (ELF_ST_IS_MIPS16 (sym->st_other))
6332 ++*valp;
6334 return TRUE;
6337 /* This hook function is called before the linker writes out a global
6338 symbol. We mark symbols as small common if appropriate. This is
6339 also where we undo the increment of the value for a mips16 symbol. */
6341 bfd_boolean
6342 _bfd_mips_elf_link_output_symbol_hook
6343 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6344 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6345 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6347 /* If we see a common symbol, which implies a relocatable link, then
6348 if a symbol was small common in an input file, mark it as small
6349 common in the output file. */
6350 if (sym->st_shndx == SHN_COMMON
6351 && strcmp (input_sec->name, ".scommon") == 0)
6352 sym->st_shndx = SHN_MIPS_SCOMMON;
6354 if (ELF_ST_IS_MIPS16 (sym->st_other))
6355 sym->st_value &= ~1;
6357 return TRUE;
6360 /* Functions for the dynamic linker. */
6362 /* Create dynamic sections when linking against a dynamic object. */
6364 bfd_boolean
6365 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
6367 struct elf_link_hash_entry *h;
6368 struct bfd_link_hash_entry *bh;
6369 flagword flags;
6370 register asection *s;
6371 const char * const *namep;
6372 struct mips_elf_link_hash_table *htab;
6374 htab = mips_elf_hash_table (info);
6375 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6376 | SEC_LINKER_CREATED | SEC_READONLY);
6378 /* The psABI requires a read-only .dynamic section, but the VxWorks
6379 EABI doesn't. */
6380 if (!htab->is_vxworks)
6382 s = bfd_get_section_by_name (abfd, ".dynamic");
6383 if (s != NULL)
6385 if (! bfd_set_section_flags (abfd, s, flags))
6386 return FALSE;
6390 /* We need to create .got section. */
6391 if (! mips_elf_create_got_section (abfd, info, FALSE))
6392 return FALSE;
6394 if (! mips_elf_rel_dyn_section (info, TRUE))
6395 return FALSE;
6397 /* Create .stub section. */
6398 s = bfd_make_section_with_flags (abfd,
6399 MIPS_ELF_STUB_SECTION_NAME (abfd),
6400 flags | SEC_CODE);
6401 if (s == NULL
6402 || ! bfd_set_section_alignment (abfd, s,
6403 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6404 return FALSE;
6405 htab->sstubs = s;
6407 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6408 && !info->shared
6409 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6411 s = bfd_make_section_with_flags (abfd, ".rld_map",
6412 flags &~ (flagword) SEC_READONLY);
6413 if (s == NULL
6414 || ! bfd_set_section_alignment (abfd, s,
6415 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6416 return FALSE;
6419 /* On IRIX5, we adjust add some additional symbols and change the
6420 alignments of several sections. There is no ABI documentation
6421 indicating that this is necessary on IRIX6, nor any evidence that
6422 the linker takes such action. */
6423 if (IRIX_COMPAT (abfd) == ict_irix5)
6425 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6427 bh = NULL;
6428 if (! (_bfd_generic_link_add_one_symbol
6429 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6430 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6431 return FALSE;
6433 h = (struct elf_link_hash_entry *) bh;
6434 h->non_elf = 0;
6435 h->def_regular = 1;
6436 h->type = STT_SECTION;
6438 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6439 return FALSE;
6442 /* We need to create a .compact_rel section. */
6443 if (SGI_COMPAT (abfd))
6445 if (!mips_elf_create_compact_rel_section (abfd, info))
6446 return FALSE;
6449 /* Change alignments of some sections. */
6450 s = bfd_get_section_by_name (abfd, ".hash");
6451 if (s != NULL)
6452 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6453 s = bfd_get_section_by_name (abfd, ".dynsym");
6454 if (s != NULL)
6455 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6456 s = bfd_get_section_by_name (abfd, ".dynstr");
6457 if (s != NULL)
6458 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6459 s = bfd_get_section_by_name (abfd, ".reginfo");
6460 if (s != NULL)
6461 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6462 s = bfd_get_section_by_name (abfd, ".dynamic");
6463 if (s != NULL)
6464 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6467 if (!info->shared)
6469 const char *name;
6471 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
6472 bh = NULL;
6473 if (!(_bfd_generic_link_add_one_symbol
6474 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
6475 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6476 return FALSE;
6478 h = (struct elf_link_hash_entry *) bh;
6479 h->non_elf = 0;
6480 h->def_regular = 1;
6481 h->type = STT_SECTION;
6483 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6484 return FALSE;
6486 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6488 /* __rld_map is a four byte word located in the .data section
6489 and is filled in by the rtld to contain a pointer to
6490 the _r_debug structure. Its symbol value will be set in
6491 _bfd_mips_elf_finish_dynamic_symbol. */
6492 s = bfd_get_section_by_name (abfd, ".rld_map");
6493 BFD_ASSERT (s != NULL);
6495 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
6496 bh = NULL;
6497 if (!(_bfd_generic_link_add_one_symbol
6498 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
6499 get_elf_backend_data (abfd)->collect, &bh)))
6500 return FALSE;
6502 h = (struct elf_link_hash_entry *) bh;
6503 h->non_elf = 0;
6504 h->def_regular = 1;
6505 h->type = STT_OBJECT;
6507 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6508 return FALSE;
6512 if (htab->is_vxworks)
6514 /* Create the .plt, .rela.plt, .dynbss and .rela.bss sections.
6515 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
6516 if (!_bfd_elf_create_dynamic_sections (abfd, info))
6517 return FALSE;
6519 /* Cache the sections created above. */
6520 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
6521 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
6522 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
6523 htab->splt = bfd_get_section_by_name (abfd, ".plt");
6524 if (!htab->sdynbss
6525 || (!htab->srelbss && !info->shared)
6526 || !htab->srelplt
6527 || !htab->splt)
6528 abort ();
6530 /* Do the usual VxWorks handling. */
6531 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
6532 return FALSE;
6534 /* Work out the PLT sizes. */
6535 if (info->shared)
6537 htab->plt_header_size
6538 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
6539 htab->plt_entry_size
6540 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
6542 else
6544 htab->plt_header_size
6545 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
6546 htab->plt_entry_size
6547 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
6551 return TRUE;
6554 /* Return true if relocation REL against section SEC is a REL rather than
6555 RELA relocation. RELOCS is the first relocation in the section and
6556 ABFD is the bfd that contains SEC. */
6558 static bfd_boolean
6559 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
6560 const Elf_Internal_Rela *relocs,
6561 const Elf_Internal_Rela *rel)
6563 Elf_Internal_Shdr *rel_hdr;
6564 const struct elf_backend_data *bed;
6566 /* To determine which flavor or relocation this is, we depend on the
6567 fact that the INPUT_SECTION's REL_HDR is read before its REL_HDR2. */
6568 rel_hdr = &elf_section_data (sec)->rel_hdr;
6569 bed = get_elf_backend_data (abfd);
6570 if ((size_t) (rel - relocs)
6571 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
6572 rel_hdr = elf_section_data (sec)->rel_hdr2;
6573 return rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (abfd);
6576 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
6577 HOWTO is the relocation's howto and CONTENTS points to the contents
6578 of the section that REL is against. */
6580 static bfd_vma
6581 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
6582 reloc_howto_type *howto, bfd_byte *contents)
6584 bfd_byte *location;
6585 unsigned int r_type;
6586 bfd_vma addend;
6588 r_type = ELF_R_TYPE (abfd, rel->r_info);
6589 location = contents + rel->r_offset;
6591 /* Get the addend, which is stored in the input file. */
6592 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
6593 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
6594 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
6596 return addend & howto->src_mask;
6599 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
6600 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
6601 and update *ADDEND with the final addend. Return true on success
6602 or false if the LO16 could not be found. RELEND is the exclusive
6603 upper bound on the relocations for REL's section. */
6605 static bfd_boolean
6606 mips_elf_add_lo16_rel_addend (bfd *abfd,
6607 const Elf_Internal_Rela *rel,
6608 const Elf_Internal_Rela *relend,
6609 bfd_byte *contents, bfd_vma *addend)
6611 unsigned int r_type, lo16_type;
6612 const Elf_Internal_Rela *lo16_relocation;
6613 reloc_howto_type *lo16_howto;
6614 bfd_vma l;
6616 r_type = ELF_R_TYPE (abfd, rel->r_info);
6617 if (mips16_reloc_p (r_type))
6618 lo16_type = R_MIPS16_LO16;
6619 else
6620 lo16_type = R_MIPS_LO16;
6622 /* The combined value is the sum of the HI16 addend, left-shifted by
6623 sixteen bits, and the LO16 addend, sign extended. (Usually, the
6624 code does a `lui' of the HI16 value, and then an `addiu' of the
6625 LO16 value.)
6627 Scan ahead to find a matching LO16 relocation.
6629 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
6630 be immediately following. However, for the IRIX6 ABI, the next
6631 relocation may be a composed relocation consisting of several
6632 relocations for the same address. In that case, the R_MIPS_LO16
6633 relocation may occur as one of these. We permit a similar
6634 extension in general, as that is useful for GCC.
6636 In some cases GCC dead code elimination removes the LO16 but keeps
6637 the corresponding HI16. This is strictly speaking a violation of
6638 the ABI but not immediately harmful. */
6639 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
6640 if (lo16_relocation == NULL)
6641 return FALSE;
6643 /* Obtain the addend kept there. */
6644 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
6645 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
6647 l <<= lo16_howto->rightshift;
6648 l = _bfd_mips_elf_sign_extend (l, 16);
6650 *addend <<= 16;
6651 *addend += l;
6652 return TRUE;
6655 /* Try to read the contents of section SEC in bfd ABFD. Return true and
6656 store the contents in *CONTENTS on success. Assume that *CONTENTS
6657 already holds the contents if it is nonull on entry. */
6659 static bfd_boolean
6660 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
6662 if (*contents)
6663 return TRUE;
6665 /* Get cached copy if it exists. */
6666 if (elf_section_data (sec)->this_hdr.contents != NULL)
6668 *contents = elf_section_data (sec)->this_hdr.contents;
6669 return TRUE;
6672 return bfd_malloc_and_get_section (abfd, sec, contents);
6675 /* Look through the relocs for a section during the first phase, and
6676 allocate space in the global offset table. */
6678 bfd_boolean
6679 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6680 asection *sec, const Elf_Internal_Rela *relocs)
6682 const char *name;
6683 bfd *dynobj;
6684 Elf_Internal_Shdr *symtab_hdr;
6685 struct elf_link_hash_entry **sym_hashes;
6686 size_t extsymoff;
6687 const Elf_Internal_Rela *rel;
6688 const Elf_Internal_Rela *rel_end;
6689 asection *sreloc;
6690 const struct elf_backend_data *bed;
6691 struct mips_elf_link_hash_table *htab;
6692 bfd_byte *contents;
6693 bfd_vma addend;
6694 reloc_howto_type *howto;
6696 if (info->relocatable)
6697 return TRUE;
6699 htab = mips_elf_hash_table (info);
6700 dynobj = elf_hash_table (info)->dynobj;
6701 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6702 sym_hashes = elf_sym_hashes (abfd);
6703 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6705 bed = get_elf_backend_data (abfd);
6706 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6708 /* Check for the mips16 stub sections. */
6710 name = bfd_get_section_name (abfd, sec);
6711 if (FN_STUB_P (name))
6713 unsigned long r_symndx;
6715 /* Look at the relocation information to figure out which symbol
6716 this is for. */
6718 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
6719 if (r_symndx == 0)
6721 (*_bfd_error_handler)
6722 (_("%B: Warning: cannot determine the target function for"
6723 " stub section `%s'"),
6724 abfd, name);
6725 bfd_set_error (bfd_error_bad_value);
6726 return FALSE;
6729 if (r_symndx < extsymoff
6730 || sym_hashes[r_symndx - extsymoff] == NULL)
6732 asection *o;
6734 /* This stub is for a local symbol. This stub will only be
6735 needed if there is some relocation in this BFD, other
6736 than a 16 bit function call, which refers to this symbol. */
6737 for (o = abfd->sections; o != NULL; o = o->next)
6739 Elf_Internal_Rela *sec_relocs;
6740 const Elf_Internal_Rela *r, *rend;
6742 /* We can ignore stub sections when looking for relocs. */
6743 if ((o->flags & SEC_RELOC) == 0
6744 || o->reloc_count == 0
6745 || section_allows_mips16_refs_p (o))
6746 continue;
6748 sec_relocs
6749 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
6750 info->keep_memory);
6751 if (sec_relocs == NULL)
6752 return FALSE;
6754 rend = sec_relocs + o->reloc_count;
6755 for (r = sec_relocs; r < rend; r++)
6756 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
6757 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
6758 break;
6760 if (elf_section_data (o)->relocs != sec_relocs)
6761 free (sec_relocs);
6763 if (r < rend)
6764 break;
6767 if (o == NULL)
6769 /* There is no non-call reloc for this stub, so we do
6770 not need it. Since this function is called before
6771 the linker maps input sections to output sections, we
6772 can easily discard it by setting the SEC_EXCLUDE
6773 flag. */
6774 sec->flags |= SEC_EXCLUDE;
6775 return TRUE;
6778 /* Record this stub in an array of local symbol stubs for
6779 this BFD. */
6780 if (elf_tdata (abfd)->local_stubs == NULL)
6782 unsigned long symcount;
6783 asection **n;
6784 bfd_size_type amt;
6786 if (elf_bad_symtab (abfd))
6787 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
6788 else
6789 symcount = symtab_hdr->sh_info;
6790 amt = symcount * sizeof (asection *);
6791 n = bfd_zalloc (abfd, amt);
6792 if (n == NULL)
6793 return FALSE;
6794 elf_tdata (abfd)->local_stubs = n;
6797 sec->flags |= SEC_KEEP;
6798 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6800 /* We don't need to set mips16_stubs_seen in this case.
6801 That flag is used to see whether we need to look through
6802 the global symbol table for stubs. We don't need to set
6803 it here, because we just have a local stub. */
6805 else
6807 struct mips_elf_link_hash_entry *h;
6809 h = ((struct mips_elf_link_hash_entry *)
6810 sym_hashes[r_symndx - extsymoff]);
6812 while (h->root.root.type == bfd_link_hash_indirect
6813 || h->root.root.type == bfd_link_hash_warning)
6814 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6816 /* H is the symbol this stub is for. */
6818 /* If we already have an appropriate stub for this function, we
6819 don't need another one, so we can discard this one. Since
6820 this function is called before the linker maps input sections
6821 to output sections, we can easily discard it by setting the
6822 SEC_EXCLUDE flag. */
6823 if (h->fn_stub != NULL)
6825 sec->flags |= SEC_EXCLUDE;
6826 return TRUE;
6829 sec->flags |= SEC_KEEP;
6830 h->fn_stub = sec;
6831 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
6834 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
6836 unsigned long r_symndx;
6837 struct mips_elf_link_hash_entry *h;
6838 asection **loc;
6840 /* Look at the relocation information to figure out which symbol
6841 this is for. */
6843 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
6844 if (r_symndx == 0)
6846 (*_bfd_error_handler)
6847 (_("%B: Warning: cannot determine the target function for"
6848 " stub section `%s'"),
6849 abfd, name);
6850 bfd_set_error (bfd_error_bad_value);
6851 return FALSE;
6854 if (r_symndx < extsymoff
6855 || sym_hashes[r_symndx - extsymoff] == NULL)
6857 asection *o;
6859 /* This stub is for a local symbol. This stub will only be
6860 needed if there is some relocation (R_MIPS16_26) in this BFD
6861 that refers to this symbol. */
6862 for (o = abfd->sections; o != NULL; o = o->next)
6864 Elf_Internal_Rela *sec_relocs;
6865 const Elf_Internal_Rela *r, *rend;
6867 /* We can ignore stub sections when looking for relocs. */
6868 if ((o->flags & SEC_RELOC) == 0
6869 || o->reloc_count == 0
6870 || section_allows_mips16_refs_p (o))
6871 continue;
6873 sec_relocs
6874 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
6875 info->keep_memory);
6876 if (sec_relocs == NULL)
6877 return FALSE;
6879 rend = sec_relocs + o->reloc_count;
6880 for (r = sec_relocs; r < rend; r++)
6881 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
6882 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
6883 break;
6885 if (elf_section_data (o)->relocs != sec_relocs)
6886 free (sec_relocs);
6888 if (r < rend)
6889 break;
6892 if (o == NULL)
6894 /* There is no non-call reloc for this stub, so we do
6895 not need it. Since this function is called before
6896 the linker maps input sections to output sections, we
6897 can easily discard it by setting the SEC_EXCLUDE
6898 flag. */
6899 sec->flags |= SEC_EXCLUDE;
6900 return TRUE;
6903 /* Record this stub in an array of local symbol call_stubs for
6904 this BFD. */
6905 if (elf_tdata (abfd)->local_call_stubs == NULL)
6907 unsigned long symcount;
6908 asection **n;
6909 bfd_size_type amt;
6911 if (elf_bad_symtab (abfd))
6912 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
6913 else
6914 symcount = symtab_hdr->sh_info;
6915 amt = symcount * sizeof (asection *);
6916 n = bfd_zalloc (abfd, amt);
6917 if (n == NULL)
6918 return FALSE;
6919 elf_tdata (abfd)->local_call_stubs = n;
6922 sec->flags |= SEC_KEEP;
6923 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
6925 /* We don't need to set mips16_stubs_seen in this case.
6926 That flag is used to see whether we need to look through
6927 the global symbol table for stubs. We don't need to set
6928 it here, because we just have a local stub. */
6930 else
6932 h = ((struct mips_elf_link_hash_entry *)
6933 sym_hashes[r_symndx - extsymoff]);
6935 /* H is the symbol this stub is for. */
6937 if (CALL_FP_STUB_P (name))
6938 loc = &h->call_fp_stub;
6939 else
6940 loc = &h->call_stub;
6942 /* If we already have an appropriate stub for this function, we
6943 don't need another one, so we can discard this one. Since
6944 this function is called before the linker maps input sections
6945 to output sections, we can easily discard it by setting the
6946 SEC_EXCLUDE flag. */
6947 if (*loc != NULL)
6949 sec->flags |= SEC_EXCLUDE;
6950 return TRUE;
6953 sec->flags |= SEC_KEEP;
6954 *loc = sec;
6955 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
6959 sreloc = NULL;
6960 contents = NULL;
6961 for (rel = relocs; rel < rel_end; ++rel)
6963 unsigned long r_symndx;
6964 unsigned int r_type;
6965 struct elf_link_hash_entry *h;
6967 r_symndx = ELF_R_SYM (abfd, rel->r_info);
6968 r_type = ELF_R_TYPE (abfd, rel->r_info);
6970 if (r_symndx < extsymoff)
6971 h = NULL;
6972 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
6974 (*_bfd_error_handler)
6975 (_("%B: Malformed reloc detected for section %s"),
6976 abfd, name);
6977 bfd_set_error (bfd_error_bad_value);
6978 return FALSE;
6980 else
6982 h = sym_hashes[r_symndx - extsymoff];
6984 /* This may be an indirect symbol created because of a version. */
6985 if (h != NULL)
6987 while (h->root.type == bfd_link_hash_indirect)
6988 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6992 /* Some relocs require a global offset table. */
6993 if (dynobj == NULL || htab->sgot == NULL)
6995 switch (r_type)
6997 case R_MIPS16_GOT16:
6998 case R_MIPS16_CALL16:
6999 case R_MIPS_GOT16:
7000 case R_MIPS_CALL16:
7001 case R_MIPS_CALL_HI16:
7002 case R_MIPS_CALL_LO16:
7003 case R_MIPS_GOT_HI16:
7004 case R_MIPS_GOT_LO16:
7005 case R_MIPS_GOT_PAGE:
7006 case R_MIPS_GOT_OFST:
7007 case R_MIPS_GOT_DISP:
7008 case R_MIPS_TLS_GOTTPREL:
7009 case R_MIPS_TLS_GD:
7010 case R_MIPS_TLS_LDM:
7011 if (dynobj == NULL)
7012 elf_hash_table (info)->dynobj = dynobj = abfd;
7013 if (! mips_elf_create_got_section (dynobj, info, FALSE))
7014 return FALSE;
7015 if (htab->is_vxworks && !info->shared)
7017 (*_bfd_error_handler)
7018 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7019 abfd, (unsigned long) rel->r_offset);
7020 bfd_set_error (bfd_error_bad_value);
7021 return FALSE;
7023 break;
7025 case R_MIPS_32:
7026 case R_MIPS_REL32:
7027 case R_MIPS_64:
7028 /* In VxWorks executables, references to external symbols
7029 are handled using copy relocs or PLT stubs, so there's
7030 no need to add a dynamic relocation here. */
7031 if (dynobj == NULL
7032 && (info->shared || (h != NULL && !htab->is_vxworks))
7033 && (sec->flags & SEC_ALLOC) != 0)
7034 elf_hash_table (info)->dynobj = dynobj = abfd;
7035 break;
7037 default:
7038 break;
7042 if (h)
7044 ((struct mips_elf_link_hash_entry *) h)->is_relocation_target = TRUE;
7046 /* Relocations against the special VxWorks __GOTT_BASE__ and
7047 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7048 room for them in .rela.dyn. */
7049 if (is_gott_symbol (info, h))
7051 if (sreloc == NULL)
7053 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7054 if (sreloc == NULL)
7055 return FALSE;
7057 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7058 if (MIPS_ELF_READONLY_SECTION (sec))
7059 /* We tell the dynamic linker that there are
7060 relocations against the text segment. */
7061 info->flags |= DF_TEXTREL;
7064 else if (r_type == R_MIPS_CALL_LO16
7065 || r_type == R_MIPS_GOT_LO16
7066 || r_type == R_MIPS_GOT_DISP
7067 || (got16_reloc_p (r_type) && htab->is_vxworks))
7069 /* We may need a local GOT entry for this relocation. We
7070 don't count R_MIPS_GOT_PAGE because we can estimate the
7071 maximum number of pages needed by looking at the size of
7072 the segment. Similar comments apply to R_MIPS*_GOT16 and
7073 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7074 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7075 R_MIPS_CALL_HI16 because these are always followed by an
7076 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7077 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7078 rel->r_addend, info, 0))
7079 return FALSE;
7082 switch (r_type)
7084 case R_MIPS_CALL16:
7085 case R_MIPS16_CALL16:
7086 if (h == NULL)
7088 (*_bfd_error_handler)
7089 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7090 abfd, (unsigned long) rel->r_offset);
7091 bfd_set_error (bfd_error_bad_value);
7092 return FALSE;
7094 /* Fall through. */
7096 case R_MIPS_CALL_HI16:
7097 case R_MIPS_CALL_LO16:
7098 if (h != NULL)
7100 /* VxWorks call relocations point the function's .got.plt
7101 entry, which will be allocated by adjust_dynamic_symbol.
7102 Otherwise, this symbol requires a global GOT entry. */
7103 if ((!htab->is_vxworks || h->forced_local)
7104 && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
7105 return FALSE;
7107 /* We need a stub, not a plt entry for the undefined
7108 function. But we record it as if it needs plt. See
7109 _bfd_elf_adjust_dynamic_symbol. */
7110 h->needs_plt = 1;
7111 h->type = STT_FUNC;
7113 break;
7115 case R_MIPS_GOT_PAGE:
7116 /* If this is a global, overridable symbol, GOT_PAGE will
7117 decay to GOT_DISP, so we'll need a GOT entry for it. */
7118 if (h)
7120 struct mips_elf_link_hash_entry *hmips =
7121 (struct mips_elf_link_hash_entry *) h;
7123 while (hmips->root.root.type == bfd_link_hash_indirect
7124 || hmips->root.root.type == bfd_link_hash_warning)
7125 hmips = (struct mips_elf_link_hash_entry *)
7126 hmips->root.root.u.i.link;
7128 /* This symbol is definitely not overridable. */
7129 if (hmips->root.def_regular
7130 && ! (info->shared && ! info->symbolic
7131 && ! hmips->root.forced_local))
7132 h = NULL;
7134 /* Fall through. */
7136 case R_MIPS16_GOT16:
7137 case R_MIPS_GOT16:
7138 case R_MIPS_GOT_HI16:
7139 case R_MIPS_GOT_LO16:
7140 if (!h || r_type == R_MIPS_GOT_PAGE)
7142 /* This relocation needs (or may need, if h != NULL) a
7143 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7144 know for sure until we know whether the symbol is
7145 preemptible. */
7146 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7148 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7149 return FALSE;
7150 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7151 addend = mips_elf_read_rel_addend (abfd, rel,
7152 howto, contents);
7153 if (r_type == R_MIPS_GOT16)
7154 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7155 contents, &addend);
7156 else
7157 addend <<= howto->rightshift;
7159 else
7160 addend = rel->r_addend;
7161 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7162 addend))
7163 return FALSE;
7164 break;
7166 /* Fall through. */
7168 case R_MIPS_GOT_DISP:
7169 if (h && !mips_elf_record_global_got_symbol (h, abfd, info, 0))
7170 return FALSE;
7171 break;
7173 case R_MIPS_TLS_GOTTPREL:
7174 if (info->shared)
7175 info->flags |= DF_STATIC_TLS;
7176 /* Fall through */
7178 case R_MIPS_TLS_LDM:
7179 if (r_type == R_MIPS_TLS_LDM)
7181 r_symndx = 0;
7182 h = NULL;
7184 /* Fall through */
7186 case R_MIPS_TLS_GD:
7187 /* This symbol requires a global offset table entry, or two
7188 for TLS GD relocations. */
7190 unsigned char flag = (r_type == R_MIPS_TLS_GD
7191 ? GOT_TLS_GD
7192 : r_type == R_MIPS_TLS_LDM
7193 ? GOT_TLS_LDM
7194 : GOT_TLS_IE);
7195 if (h != NULL)
7197 struct mips_elf_link_hash_entry *hmips =
7198 (struct mips_elf_link_hash_entry *) h;
7199 hmips->tls_type |= flag;
7201 if (h && !mips_elf_record_global_got_symbol (h, abfd,
7202 info, flag))
7203 return FALSE;
7205 else
7207 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
7209 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7210 rel->r_addend,
7211 info, flag))
7212 return FALSE;
7215 break;
7217 case R_MIPS_32:
7218 case R_MIPS_REL32:
7219 case R_MIPS_64:
7220 /* In VxWorks executables, references to external symbols
7221 are handled using copy relocs or PLT stubs, so there's
7222 no need to add a .rela.dyn entry for this relocation. */
7223 if ((info->shared || (h != NULL && !htab->is_vxworks))
7224 && !(h && strcmp (h->root.root.string, "__gnu_local_gp") == 0)
7225 && (sec->flags & SEC_ALLOC) != 0)
7227 if (sreloc == NULL)
7229 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7230 if (sreloc == NULL)
7231 return FALSE;
7233 if (info->shared && h == NULL)
7235 /* When creating a shared object, we must copy these
7236 reloc types into the output file as R_MIPS_REL32
7237 relocs. Make room for this reloc in .rel(a).dyn. */
7238 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7239 if (MIPS_ELF_READONLY_SECTION (sec))
7240 /* We tell the dynamic linker that there are
7241 relocations against the text segment. */
7242 info->flags |= DF_TEXTREL;
7244 else
7246 struct mips_elf_link_hash_entry *hmips;
7248 /* For a shared object, we must copy this relocation
7249 unless the symbol turns out to be undefined and
7250 weak with non-default visibility, in which case
7251 it will be left as zero.
7253 We could elide R_MIPS_REL32 for locally binding symbols
7254 in shared libraries, but do not yet do so.
7256 For an executable, we only need to copy this
7257 reloc if the symbol is defined in a dynamic
7258 object. */
7259 hmips = (struct mips_elf_link_hash_entry *) h;
7260 ++hmips->possibly_dynamic_relocs;
7261 if (MIPS_ELF_READONLY_SECTION (sec))
7262 /* We need it to tell the dynamic linker if there
7263 are relocations against the text segment. */
7264 hmips->readonly_reloc = TRUE;
7267 /* Even though we don't directly need a GOT entry for
7268 this symbol, a symbol must have a dynamic symbol
7269 table index greater that DT_MIPS_GOTSYM if there are
7270 dynamic relocations against it. This does not apply
7271 to VxWorks, which does not have the usual coupling
7272 between global GOT entries and .dynsym entries. */
7273 if (h != NULL && !htab->is_vxworks)
7275 if (dynobj == NULL)
7276 elf_hash_table (info)->dynobj = dynobj = abfd;
7277 if (! mips_elf_create_got_section (dynobj, info, TRUE))
7278 return FALSE;
7279 if (!mips_elf_record_global_got_symbol (h, abfd, info, 0))
7280 return FALSE;
7284 if (SGI_COMPAT (abfd))
7285 mips_elf_hash_table (info)->compact_rel_size +=
7286 sizeof (Elf32_External_crinfo);
7287 break;
7289 case R_MIPS_PC16:
7290 if (h)
7291 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
7292 break;
7294 case R_MIPS_26:
7295 if (h)
7296 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
7297 /* Fall through. */
7299 case R_MIPS_GPREL16:
7300 case R_MIPS_LITERAL:
7301 case R_MIPS_GPREL32:
7302 if (SGI_COMPAT (abfd))
7303 mips_elf_hash_table (info)->compact_rel_size +=
7304 sizeof (Elf32_External_crinfo);
7305 break;
7307 /* This relocation describes the C++ object vtable hierarchy.
7308 Reconstruct it for later use during GC. */
7309 case R_MIPS_GNU_VTINHERIT:
7310 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7311 return FALSE;
7312 break;
7314 /* This relocation describes which C++ vtable entries are actually
7315 used. Record for later use during GC. */
7316 case R_MIPS_GNU_VTENTRY:
7317 BFD_ASSERT (h != NULL);
7318 if (h != NULL
7319 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7320 return FALSE;
7321 break;
7323 default:
7324 break;
7327 /* We must not create a stub for a symbol that has relocations
7328 related to taking the function's address. This doesn't apply to
7329 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7330 a normal .got entry. */
7331 if (!htab->is_vxworks && h != NULL)
7332 switch (r_type)
7334 default:
7335 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7336 break;
7337 case R_MIPS16_CALL16:
7338 case R_MIPS_CALL16:
7339 case R_MIPS_CALL_HI16:
7340 case R_MIPS_CALL_LO16:
7341 case R_MIPS_JALR:
7342 break;
7345 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7346 if there is one. We only need to handle global symbols here;
7347 we decide whether to keep or delete stubs for local symbols
7348 when processing the stub's relocations. */
7349 if (h != NULL
7350 && !mips16_call_reloc_p (r_type)
7351 && !section_allows_mips16_refs_p (sec))
7353 struct mips_elf_link_hash_entry *mh;
7355 mh = (struct mips_elf_link_hash_entry *) h;
7356 mh->need_fn_stub = TRUE;
7360 return TRUE;
7363 bfd_boolean
7364 _bfd_mips_relax_section (bfd *abfd, asection *sec,
7365 struct bfd_link_info *link_info,
7366 bfd_boolean *again)
7368 Elf_Internal_Rela *internal_relocs;
7369 Elf_Internal_Rela *irel, *irelend;
7370 Elf_Internal_Shdr *symtab_hdr;
7371 bfd_byte *contents = NULL;
7372 size_t extsymoff;
7373 bfd_boolean changed_contents = FALSE;
7374 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7375 Elf_Internal_Sym *isymbuf = NULL;
7377 /* We are not currently changing any sizes, so only one pass. */
7378 *again = FALSE;
7380 if (link_info->relocatable)
7381 return TRUE;
7383 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7384 link_info->keep_memory);
7385 if (internal_relocs == NULL)
7386 return TRUE;
7388 irelend = internal_relocs + sec->reloc_count
7389 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7390 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7391 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7393 for (irel = internal_relocs; irel < irelend; irel++)
7395 bfd_vma symval;
7396 bfd_signed_vma sym_offset;
7397 unsigned int r_type;
7398 unsigned long r_symndx;
7399 asection *sym_sec;
7400 unsigned long instruction;
7402 /* Turn jalr into bgezal, and jr into beq, if they're marked
7403 with a JALR relocation, that indicate where they jump to.
7404 This saves some pipeline bubbles. */
7405 r_type = ELF_R_TYPE (abfd, irel->r_info);
7406 if (r_type != R_MIPS_JALR)
7407 continue;
7409 r_symndx = ELF_R_SYM (abfd, irel->r_info);
7410 /* Compute the address of the jump target. */
7411 if (r_symndx >= extsymoff)
7413 struct mips_elf_link_hash_entry *h
7414 = ((struct mips_elf_link_hash_entry *)
7415 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
7417 while (h->root.root.type == bfd_link_hash_indirect
7418 || h->root.root.type == bfd_link_hash_warning)
7419 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7421 /* If a symbol is undefined, or if it may be overridden,
7422 skip it. */
7423 if (! ((h->root.root.type == bfd_link_hash_defined
7424 || h->root.root.type == bfd_link_hash_defweak)
7425 && h->root.root.u.def.section)
7426 || (link_info->shared && ! link_info->symbolic
7427 && !h->root.forced_local))
7428 continue;
7430 sym_sec = h->root.root.u.def.section;
7431 if (sym_sec->output_section)
7432 symval = (h->root.root.u.def.value
7433 + sym_sec->output_section->vma
7434 + sym_sec->output_offset);
7435 else
7436 symval = h->root.root.u.def.value;
7438 else
7440 Elf_Internal_Sym *isym;
7442 /* Read this BFD's symbols if we haven't done so already. */
7443 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
7445 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
7446 if (isymbuf == NULL)
7447 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
7448 symtab_hdr->sh_info, 0,
7449 NULL, NULL, NULL);
7450 if (isymbuf == NULL)
7451 goto relax_return;
7454 isym = isymbuf + r_symndx;
7455 if (isym->st_shndx == SHN_UNDEF)
7456 continue;
7457 else if (isym->st_shndx == SHN_ABS)
7458 sym_sec = bfd_abs_section_ptr;
7459 else if (isym->st_shndx == SHN_COMMON)
7460 sym_sec = bfd_com_section_ptr;
7461 else
7462 sym_sec
7463 = bfd_section_from_elf_index (abfd, isym->st_shndx);
7464 symval = isym->st_value
7465 + sym_sec->output_section->vma
7466 + sym_sec->output_offset;
7469 /* Compute branch offset, from delay slot of the jump to the
7470 branch target. */
7471 sym_offset = (symval + irel->r_addend)
7472 - (sec_start + irel->r_offset + 4);
7474 /* Branch offset must be properly aligned. */
7475 if ((sym_offset & 3) != 0)
7476 continue;
7478 sym_offset >>= 2;
7480 /* Check that it's in range. */
7481 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
7482 continue;
7484 /* Get the section contents if we haven't done so already. */
7485 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7486 goto relax_return;
7488 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
7490 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
7491 if ((instruction & 0xfc1fffff) == 0x0000f809)
7492 instruction = 0x04110000;
7493 /* If it was jr <reg>, turn it into b <target>. */
7494 else if ((instruction & 0xfc1fffff) == 0x00000008)
7495 instruction = 0x10000000;
7496 else
7497 continue;
7499 instruction |= (sym_offset & 0xffff);
7500 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
7501 changed_contents = TRUE;
7504 if (contents != NULL
7505 && elf_section_data (sec)->this_hdr.contents != contents)
7507 if (!changed_contents && !link_info->keep_memory)
7508 free (contents);
7509 else
7511 /* Cache the section contents for elf_link_input_bfd. */
7512 elf_section_data (sec)->this_hdr.contents = contents;
7515 return TRUE;
7517 relax_return:
7518 if (contents != NULL
7519 && elf_section_data (sec)->this_hdr.contents != contents)
7520 free (contents);
7521 return FALSE;
7524 /* Allocate space for global sym dynamic relocs. */
7526 static bfd_boolean
7527 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7529 struct bfd_link_info *info = inf;
7530 bfd *dynobj;
7531 struct mips_elf_link_hash_entry *hmips;
7532 struct mips_elf_link_hash_table *htab;
7534 htab = mips_elf_hash_table (info);
7535 dynobj = elf_hash_table (info)->dynobj;
7536 hmips = (struct mips_elf_link_hash_entry *) h;
7538 /* VxWorks executables are handled elsewhere; we only need to
7539 allocate relocations in shared objects. */
7540 if (htab->is_vxworks && !info->shared)
7541 return TRUE;
7543 /* Ignore indirect and warning symbols. All relocations against
7544 such symbols will be redirected to the target symbol. */
7545 if (h->root.type == bfd_link_hash_indirect
7546 || h->root.type == bfd_link_hash_warning)
7547 return TRUE;
7549 /* If this symbol is defined in a dynamic object, or we are creating
7550 a shared library, we will need to copy any R_MIPS_32 or
7551 R_MIPS_REL32 relocs against it into the output file. */
7552 if (! info->relocatable
7553 && hmips->possibly_dynamic_relocs != 0
7554 && (h->root.type == bfd_link_hash_defweak
7555 || !h->def_regular
7556 || info->shared))
7558 bfd_boolean do_copy = TRUE;
7560 if (h->root.type == bfd_link_hash_undefweak)
7562 /* Do not copy relocations for undefined weak symbols with
7563 non-default visibility. */
7564 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7565 do_copy = FALSE;
7567 /* Make sure undefined weak symbols are output as a dynamic
7568 symbol in PIEs. */
7569 else if (h->dynindx == -1 && !h->forced_local)
7571 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7572 return FALSE;
7576 if (do_copy)
7578 mips_elf_allocate_dynamic_relocations
7579 (dynobj, info, hmips->possibly_dynamic_relocs);
7580 if (hmips->readonly_reloc)
7581 /* We tell the dynamic linker that there are relocations
7582 against the text segment. */
7583 info->flags |= DF_TEXTREL;
7587 return TRUE;
7590 /* Adjust a symbol defined by a dynamic object and referenced by a
7591 regular object. The current definition is in some section of the
7592 dynamic object, but we're not including those sections. We have to
7593 change the definition to something the rest of the link can
7594 understand. */
7596 bfd_boolean
7597 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7598 struct elf_link_hash_entry *h)
7600 bfd *dynobj;
7601 struct mips_elf_link_hash_entry *hmips;
7602 struct mips_elf_link_hash_table *htab;
7604 htab = mips_elf_hash_table (info);
7605 dynobj = elf_hash_table (info)->dynobj;
7607 /* Make sure we know what is going on here. */
7608 BFD_ASSERT (dynobj != NULL
7609 && (h->needs_plt
7610 || h->u.weakdef != NULL
7611 || (h->def_dynamic
7612 && h->ref_regular
7613 && !h->def_regular)));
7615 hmips = (struct mips_elf_link_hash_entry *) h;
7617 /* For a function, create a stub, if allowed. */
7618 if (! hmips->no_fn_stub
7619 && h->needs_plt)
7621 if (! elf_hash_table (info)->dynamic_sections_created)
7622 return TRUE;
7624 /* If this symbol is not defined in a regular file, then set
7625 the symbol to the stub location. This is required to make
7626 function pointers compare as equal between the normal
7627 executable and the shared library. */
7628 if (!h->def_regular)
7630 hmips->needs_lazy_stub = TRUE;
7631 htab->lazy_stub_count++;
7632 return TRUE;
7635 else if ((h->type == STT_FUNC)
7636 && !h->needs_plt)
7638 /* This will set the entry for this symbol in the GOT to 0, and
7639 the dynamic linker will take care of this. */
7640 h->root.u.def.value = 0;
7641 return TRUE;
7644 /* If this is a weak symbol, and there is a real definition, the
7645 processor independent code will have arranged for us to see the
7646 real definition first, and we can just use the same value. */
7647 if (h->u.weakdef != NULL)
7649 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7650 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7651 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7652 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7653 return TRUE;
7656 /* This is a reference to a symbol defined by a dynamic object which
7657 is not a function. */
7659 return TRUE;
7662 /* Likewise, for VxWorks. */
7664 bfd_boolean
7665 _bfd_mips_vxworks_adjust_dynamic_symbol (struct bfd_link_info *info,
7666 struct elf_link_hash_entry *h)
7668 bfd *dynobj;
7669 struct mips_elf_link_hash_entry *hmips;
7670 struct mips_elf_link_hash_table *htab;
7672 htab = mips_elf_hash_table (info);
7673 dynobj = elf_hash_table (info)->dynobj;
7674 hmips = (struct mips_elf_link_hash_entry *) h;
7676 /* Make sure we know what is going on here. */
7677 BFD_ASSERT (dynobj != NULL
7678 && (h->needs_plt
7679 || h->needs_copy
7680 || h->u.weakdef != NULL
7681 || (h->def_dynamic
7682 && h->ref_regular
7683 && !h->def_regular)));
7685 /* If the symbol is defined by a dynamic object, we need a PLT stub if
7686 either (a) we want to branch to the symbol or (b) we're linking an
7687 executable that needs a canonical function address. In the latter
7688 case, the canonical address will be the address of the executable's
7689 load stub. */
7690 if ((hmips->is_branch_target
7691 || (!info->shared
7692 && h->type == STT_FUNC
7693 && hmips->is_relocation_target))
7694 && h->def_dynamic
7695 && h->ref_regular
7696 && !h->def_regular
7697 && !h->forced_local)
7698 h->needs_plt = 1;
7700 /* Locally-binding symbols do not need a PLT stub; we can refer to
7701 the functions directly. */
7702 else if (h->needs_plt
7703 && (SYMBOL_CALLS_LOCAL (info, h)
7704 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7705 && h->root.type == bfd_link_hash_undefweak)))
7707 h->needs_plt = 0;
7708 return TRUE;
7711 if (h->needs_plt)
7713 /* If this is the first symbol to need a PLT entry, allocate room
7714 for the header, and for the header's .rela.plt.unloaded entries. */
7715 if (htab->splt->size == 0)
7717 htab->splt->size += htab->plt_header_size;
7718 if (!info->shared)
7719 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
7722 /* Assign the next .plt entry to this symbol. */
7723 h->plt.offset = htab->splt->size;
7724 htab->splt->size += htab->plt_entry_size;
7726 /* If the output file has no definition of the symbol, set the
7727 symbol's value to the address of the stub. Point at the PLT
7728 load stub rather than the lazy resolution stub; this stub
7729 will become the canonical function address. */
7730 if (!info->shared && !h->def_regular)
7732 h->root.u.def.section = htab->splt;
7733 h->root.u.def.value = h->plt.offset;
7734 h->root.u.def.value += 8;
7737 /* Make room for the .got.plt entry and the R_JUMP_SLOT relocation. */
7738 htab->sgotplt->size += 4;
7739 htab->srelplt->size += sizeof (Elf32_External_Rela);
7741 /* Make room for the .rela.plt.unloaded relocations. */
7742 if (!info->shared)
7743 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
7745 return TRUE;
7748 /* If a function symbol is defined by a dynamic object, and we do not
7749 need a PLT stub for it, the symbol's value should be zero. */
7750 if (h->type == STT_FUNC
7751 && h->def_dynamic
7752 && h->ref_regular
7753 && !h->def_regular)
7755 h->root.u.def.value = 0;
7756 return TRUE;
7759 /* If this is a weak symbol, and there is a real definition, the
7760 processor independent code will have arranged for us to see the
7761 real definition first, and we can just use the same value. */
7762 if (h->u.weakdef != NULL)
7764 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7765 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7766 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7767 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7768 return TRUE;
7771 /* This is a reference to a symbol defined by a dynamic object which
7772 is not a function. */
7773 if (info->shared)
7774 return TRUE;
7776 /* We must allocate the symbol in our .dynbss section, which will
7777 become part of the .bss section of the executable. There will be
7778 an entry for this symbol in the .dynsym section. The dynamic
7779 object will contain position independent code, so all references
7780 from the dynamic object to this symbol will go through the global
7781 offset table. The dynamic linker will use the .dynsym entry to
7782 determine the address it must put in the global offset table, so
7783 both the dynamic object and the regular object will refer to the
7784 same memory location for the variable. */
7786 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
7788 htab->srelbss->size += sizeof (Elf32_External_Rela);
7789 h->needs_copy = 1;
7792 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
7795 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
7796 The number might be exact or a worst-case estimate, depending on how
7797 much information is available to elf_backend_omit_section_dynsym at
7798 the current linking stage. */
7800 static bfd_size_type
7801 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
7803 bfd_size_type count;
7805 count = 0;
7806 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
7808 asection *p;
7809 const struct elf_backend_data *bed;
7811 bed = get_elf_backend_data (output_bfd);
7812 for (p = output_bfd->sections; p ; p = p->next)
7813 if ((p->flags & SEC_EXCLUDE) == 0
7814 && (p->flags & SEC_ALLOC) != 0
7815 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
7816 ++count;
7818 return count;
7821 /* This function is called after all the input files have been read,
7822 and the input sections have been assigned to output sections. We
7823 check for any mips16 stub sections that we can discard. */
7825 bfd_boolean
7826 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
7827 struct bfd_link_info *info)
7829 asection *ri;
7830 struct mips_elf_link_hash_table *htab;
7832 htab = mips_elf_hash_table (info);
7834 /* The .reginfo section has a fixed size. */
7835 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7836 if (ri != NULL)
7837 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7839 if (! (info->relocatable
7840 || ! mips_elf_hash_table (info)->mips16_stubs_seen))
7841 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7842 mips_elf_check_mips16_stubs, info);
7844 return TRUE;
7847 /* If the link uses a GOT, lay it out and work out its size. */
7849 static bfd_boolean
7850 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
7852 bfd *dynobj;
7853 asection *s;
7854 struct mips_got_info *g;
7855 int i;
7856 bfd_size_type loadable_size = 0;
7857 bfd_size_type page_gotno;
7858 bfd *sub;
7859 struct mips_elf_count_tls_arg count_tls_arg;
7860 struct mips_elf_link_hash_table *htab;
7862 htab = mips_elf_hash_table (info);
7863 s = htab->sgot;
7864 if (s == NULL)
7865 return TRUE;
7867 dynobj = elf_hash_table (info)->dynobj;
7868 g = htab->got_info;
7870 /* Replace entries for indirect and warning symbols with entries for
7871 the target symbol. */
7872 if (!mips_elf_resolve_final_got_entries (g))
7873 return FALSE;
7875 /* Count the number of forced-local entries. */
7876 mips_elf_link_hash_traverse (htab,
7877 mips_elf_count_forced_local_got_symbols, g);
7879 /* There has to be a global GOT entry for every symbol with
7880 a dynamic symbol table index of DT_MIPS_GOTSYM or
7881 higher. Therefore, it make sense to put those symbols
7882 that need GOT entries at the end of the symbol table. We
7883 do that here. */
7884 if (! mips_elf_sort_hash_table (info, 1))
7885 return FALSE;
7887 if (g->global_gotsym != NULL)
7888 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
7889 else
7890 /* If there are no global symbols, or none requiring
7891 relocations, then GLOBAL_GOTSYM will be NULL. */
7892 i = 0;
7894 /* Calculate the total loadable size of the output. That
7895 will give us the maximum number of GOT_PAGE entries
7896 required. */
7897 for (sub = info->input_bfds; sub; sub = sub->link_next)
7899 asection *subsection;
7901 for (subsection = sub->sections;
7902 subsection;
7903 subsection = subsection->next)
7905 if ((subsection->flags & SEC_ALLOC) == 0)
7906 continue;
7907 loadable_size += ((subsection->size + 0xf)
7908 &~ (bfd_size_type) 0xf);
7912 if (htab->is_vxworks)
7913 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
7914 relocations against local symbols evaluate to "G", and the EABI does
7915 not include R_MIPS_GOT_PAGE. */
7916 page_gotno = 0;
7917 else
7918 /* Assume there are two loadable segments consisting of contiguous
7919 sections. Is 5 enough? */
7920 page_gotno = (loadable_size >> 16) + 5;
7922 /* Choose the smaller of the two estimates; both are intended to be
7923 conservative. */
7924 if (page_gotno > g->page_gotno)
7925 page_gotno = g->page_gotno;
7927 g->local_gotno += page_gotno;
7928 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7930 g->global_gotno = i;
7931 s->size += i * MIPS_ELF_GOT_SIZE (output_bfd);
7933 /* We need to calculate tls_gotno for global symbols at this point
7934 instead of building it up earlier, to avoid doublecounting
7935 entries for one global symbol from multiple input files. */
7936 count_tls_arg.info = info;
7937 count_tls_arg.needed = 0;
7938 elf_link_hash_traverse (elf_hash_table (info),
7939 mips_elf_count_global_tls_entries,
7940 &count_tls_arg);
7941 g->tls_gotno += count_tls_arg.needed;
7942 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7944 /* VxWorks does not support multiple GOTs. It initializes $gp to
7945 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
7946 dynamic loader. */
7947 if (htab->is_vxworks)
7949 /* VxWorks executables do not need a GOT. */
7950 if (info->shared)
7952 /* Each VxWorks GOT entry needs an explicit relocation. */
7953 unsigned int count;
7955 count = g->global_gotno + g->local_gotno - MIPS_RESERVED_GOTNO (info);
7956 if (count)
7957 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
7960 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
7962 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
7963 return FALSE;
7965 else
7967 struct mips_elf_count_tls_arg arg;
7969 /* Set up TLS entries. */
7970 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
7971 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
7973 /* Allocate room for the TLS relocations. */
7974 arg.info = info;
7975 arg.needed = 0;
7976 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
7977 elf_link_hash_traverse (elf_hash_table (info),
7978 mips_elf_count_global_tls_relocs,
7979 &arg);
7980 if (arg.needed)
7981 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
7984 return TRUE;
7987 /* Estimate the size of the .MIPS.stubs section. */
7989 static void
7990 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
7992 struct mips_elf_link_hash_table *htab;
7993 bfd_size_type dynsymcount;
7995 htab = mips_elf_hash_table (info);
7996 if (htab->lazy_stub_count == 0)
7997 return;
7999 /* IRIX rld assumes that a function stub isn't at the end of the .text
8000 section, so add a dummy entry to the end. */
8001 htab->lazy_stub_count++;
8003 /* Get a worst-case estimate of the number of dynamic symbols needed.
8004 At this point, dynsymcount does not account for section symbols
8005 and count_section_dynsyms may overestimate the number that will
8006 be needed. */
8007 dynsymcount = (elf_hash_table (info)->dynsymcount
8008 + count_section_dynsyms (output_bfd, info));
8010 /* Determine the size of one stub entry. */
8011 htab->function_stub_size = (dynsymcount > 0x10000
8012 ? MIPS_FUNCTION_STUB_BIG_SIZE
8013 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8015 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8018 /* A mips_elf_link_hash_traverse callback for which DATA points to the
8019 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8020 allocate an entry in the stubs section. */
8022 static bfd_boolean
8023 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8025 struct mips_elf_link_hash_table *htab;
8027 htab = (struct mips_elf_link_hash_table *) data;
8028 if (h->needs_lazy_stub)
8030 h->root.root.u.def.section = htab->sstubs;
8031 h->root.root.u.def.value = htab->sstubs->size;
8032 h->root.plt.offset = htab->sstubs->size;
8033 htab->sstubs->size += htab->function_stub_size;
8035 return TRUE;
8038 /* Allocate offsets in the stubs section to each symbol that needs one.
8039 Set the final size of the .MIPS.stub section. */
8041 static void
8042 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8044 struct mips_elf_link_hash_table *htab;
8046 htab = mips_elf_hash_table (info);
8047 if (htab->lazy_stub_count == 0)
8048 return;
8050 htab->sstubs->size = 0;
8051 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8052 mips_elf_allocate_lazy_stub, htab);
8053 htab->sstubs->size += htab->function_stub_size;
8054 BFD_ASSERT (htab->sstubs->size
8055 == htab->lazy_stub_count * htab->function_stub_size);
8058 /* Set the sizes of the dynamic sections. */
8060 bfd_boolean
8061 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8062 struct bfd_link_info *info)
8064 bfd *dynobj;
8065 asection *s;
8066 bfd_boolean reltext;
8067 struct mips_elf_link_hash_table *htab;
8069 htab = mips_elf_hash_table (info);
8070 dynobj = elf_hash_table (info)->dynobj;
8071 BFD_ASSERT (dynobj != NULL);
8073 if (elf_hash_table (info)->dynamic_sections_created)
8075 /* Set the contents of the .interp section to the interpreter. */
8076 if (info->executable)
8078 s = bfd_get_section_by_name (dynobj, ".interp");
8079 BFD_ASSERT (s != NULL);
8080 s->size
8081 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8082 s->contents
8083 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8087 /* Allocate space for global sym dynamic relocs. */
8088 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8090 mips_elf_estimate_stub_size (output_bfd, info);
8092 if (!mips_elf_lay_out_got (output_bfd, info))
8093 return FALSE;
8095 mips_elf_lay_out_lazy_stubs (info);
8097 /* The check_relocs and adjust_dynamic_symbol entry points have
8098 determined the sizes of the various dynamic sections. Allocate
8099 memory for them. */
8100 reltext = FALSE;
8101 for (s = dynobj->sections; s != NULL; s = s->next)
8103 const char *name;
8105 /* It's OK to base decisions on the section name, because none
8106 of the dynobj section names depend upon the input files. */
8107 name = bfd_get_section_name (dynobj, s);
8109 if ((s->flags & SEC_LINKER_CREATED) == 0)
8110 continue;
8112 if (CONST_STRNEQ (name, ".rel"))
8114 if (s->size != 0)
8116 const char *outname;
8117 asection *target;
8119 /* If this relocation section applies to a read only
8120 section, then we probably need a DT_TEXTREL entry.
8121 If the relocation section is .rel(a).dyn, we always
8122 assert a DT_TEXTREL entry rather than testing whether
8123 there exists a relocation to a read only section or
8124 not. */
8125 outname = bfd_get_section_name (output_bfd,
8126 s->output_section);
8127 target = bfd_get_section_by_name (output_bfd, outname + 4);
8128 if ((target != NULL
8129 && (target->flags & SEC_READONLY) != 0
8130 && (target->flags & SEC_ALLOC) != 0)
8131 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8132 reltext = TRUE;
8134 /* We use the reloc_count field as a counter if we need
8135 to copy relocs into the output file. */
8136 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8137 s->reloc_count = 0;
8139 /* If combreloc is enabled, elf_link_sort_relocs() will
8140 sort relocations, but in a different way than we do,
8141 and before we're done creating relocations. Also, it
8142 will move them around between input sections'
8143 relocation's contents, so our sorting would be
8144 broken, so don't let it run. */
8145 info->combreloc = 0;
8148 else if (! info->shared
8149 && ! mips_elf_hash_table (info)->use_rld_obj_head
8150 && CONST_STRNEQ (name, ".rld_map"))
8152 /* We add a room for __rld_map. It will be filled in by the
8153 rtld to contain a pointer to the _r_debug structure. */
8154 s->size += 4;
8156 else if (SGI_COMPAT (output_bfd)
8157 && CONST_STRNEQ (name, ".compact_rel"))
8158 s->size += mips_elf_hash_table (info)->compact_rel_size;
8159 else if (! CONST_STRNEQ (name, ".init")
8160 && s != htab->sgot
8161 && s != htab->sgotplt
8162 && s != htab->splt
8163 && s != htab->sstubs)
8165 /* It's not one of our sections, so don't allocate space. */
8166 continue;
8169 if (s->size == 0)
8171 s->flags |= SEC_EXCLUDE;
8172 continue;
8175 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8176 continue;
8178 /* Allocate memory for the section contents. */
8179 s->contents = bfd_zalloc (dynobj, s->size);
8180 if (s->contents == NULL)
8182 bfd_set_error (bfd_error_no_memory);
8183 return FALSE;
8187 if (elf_hash_table (info)->dynamic_sections_created)
8189 /* Add some entries to the .dynamic section. We fill in the
8190 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8191 must add the entries now so that we get the correct size for
8192 the .dynamic section. */
8194 /* SGI object has the equivalence of DT_DEBUG in the
8195 DT_MIPS_RLD_MAP entry. This must come first because glibc
8196 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8197 looks at the first one it sees. */
8198 if (!info->shared
8199 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8200 return FALSE;
8202 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8203 used by the debugger. */
8204 if (info->executable
8205 && !SGI_COMPAT (output_bfd)
8206 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8207 return FALSE;
8209 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8210 info->flags |= DF_TEXTREL;
8212 if ((info->flags & DF_TEXTREL) != 0)
8214 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8215 return FALSE;
8217 /* Clear the DF_TEXTREL flag. It will be set again if we
8218 write out an actual text relocation; we may not, because
8219 at this point we do not know whether e.g. any .eh_frame
8220 absolute relocations have been converted to PC-relative. */
8221 info->flags &= ~DF_TEXTREL;
8224 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8225 return FALSE;
8227 if (htab->is_vxworks)
8229 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8230 use any of the DT_MIPS_* tags. */
8231 if (mips_elf_rel_dyn_section (info, FALSE))
8233 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8234 return FALSE;
8236 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8237 return FALSE;
8239 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8240 return FALSE;
8242 if (htab->splt->size > 0)
8244 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8245 return FALSE;
8247 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8248 return FALSE;
8250 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8251 return FALSE;
8254 else
8256 if (mips_elf_rel_dyn_section (info, FALSE))
8258 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8259 return FALSE;
8261 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8262 return FALSE;
8264 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8265 return FALSE;
8268 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8269 return FALSE;
8271 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8272 return FALSE;
8274 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8275 return FALSE;
8277 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8278 return FALSE;
8280 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8281 return FALSE;
8283 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8284 return FALSE;
8286 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8287 return FALSE;
8289 if (IRIX_COMPAT (dynobj) == ict_irix5
8290 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8291 return FALSE;
8293 if (IRIX_COMPAT (dynobj) == ict_irix6
8294 && (bfd_get_section_by_name
8295 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8296 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8297 return FALSE;
8299 if (htab->is_vxworks
8300 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8301 return FALSE;
8304 return TRUE;
8307 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8308 Adjust its R_ADDEND field so that it is correct for the output file.
8309 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8310 and sections respectively; both use symbol indexes. */
8312 static void
8313 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8314 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8315 asection **local_sections, Elf_Internal_Rela *rel)
8317 unsigned int r_type, r_symndx;
8318 Elf_Internal_Sym *sym;
8319 asection *sec;
8321 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8323 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8324 if (r_type == R_MIPS16_GPREL
8325 || r_type == R_MIPS_GPREL16
8326 || r_type == R_MIPS_GPREL32
8327 || r_type == R_MIPS_LITERAL)
8329 rel->r_addend += _bfd_get_gp_value (input_bfd);
8330 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8333 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8334 sym = local_syms + r_symndx;
8336 /* Adjust REL's addend to account for section merging. */
8337 if (!info->relocatable)
8339 sec = local_sections[r_symndx];
8340 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8343 /* This would normally be done by the rela_normal code in elflink.c. */
8344 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8345 rel->r_addend += local_sections[r_symndx]->output_offset;
8349 /* Relocate a MIPS ELF section. */
8351 bfd_boolean
8352 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8353 bfd *input_bfd, asection *input_section,
8354 bfd_byte *contents, Elf_Internal_Rela *relocs,
8355 Elf_Internal_Sym *local_syms,
8356 asection **local_sections)
8358 Elf_Internal_Rela *rel;
8359 const Elf_Internal_Rela *relend;
8360 bfd_vma addend = 0;
8361 bfd_boolean use_saved_addend_p = FALSE;
8362 const struct elf_backend_data *bed;
8364 bed = get_elf_backend_data (output_bfd);
8365 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8366 for (rel = relocs; rel < relend; ++rel)
8368 const char *name;
8369 bfd_vma value = 0;
8370 reloc_howto_type *howto;
8371 bfd_boolean require_jalx;
8372 /* TRUE if the relocation is a RELA relocation, rather than a
8373 REL relocation. */
8374 bfd_boolean rela_relocation_p = TRUE;
8375 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8376 const char *msg;
8377 unsigned long r_symndx;
8378 asection *sec;
8379 Elf_Internal_Shdr *symtab_hdr;
8380 struct elf_link_hash_entry *h;
8382 /* Find the relocation howto for this relocation. */
8383 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
8384 NEWABI_P (input_bfd)
8385 && (MIPS_RELOC_RELA_P
8386 (input_bfd, input_section,
8387 rel - relocs)));
8389 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
8390 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8391 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
8393 sec = local_sections[r_symndx];
8394 h = NULL;
8396 else
8398 unsigned long extsymoff;
8400 extsymoff = 0;
8401 if (!elf_bad_symtab (input_bfd))
8402 extsymoff = symtab_hdr->sh_info;
8403 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
8404 while (h->root.type == bfd_link_hash_indirect
8405 || h->root.type == bfd_link_hash_warning)
8406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8408 sec = NULL;
8409 if (h->root.type == bfd_link_hash_defined
8410 || h->root.type == bfd_link_hash_defweak)
8411 sec = h->root.u.def.section;
8414 if (sec != NULL && elf_discarded_section (sec))
8416 /* For relocs against symbols from removed linkonce sections,
8417 or sections discarded by a linker script, we just want the
8418 section contents zeroed. Avoid any special processing. */
8419 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
8420 rel->r_info = 0;
8421 rel->r_addend = 0;
8422 continue;
8425 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
8427 /* Some 32-bit code uses R_MIPS_64. In particular, people use
8428 64-bit code, but make sure all their addresses are in the
8429 lowermost or uppermost 32-bit section of the 64-bit address
8430 space. Thus, when they use an R_MIPS_64 they mean what is
8431 usually meant by R_MIPS_32, with the exception that the
8432 stored value is sign-extended to 64 bits. */
8433 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
8435 /* On big-endian systems, we need to lie about the position
8436 of the reloc. */
8437 if (bfd_big_endian (input_bfd))
8438 rel->r_offset += 4;
8441 if (!use_saved_addend_p)
8443 /* If these relocations were originally of the REL variety,
8444 we must pull the addend out of the field that will be
8445 relocated. Otherwise, we simply use the contents of the
8446 RELA relocation. */
8447 if (mips_elf_rel_relocation_p (input_bfd, input_section,
8448 relocs, rel))
8450 rela_relocation_p = FALSE;
8451 addend = mips_elf_read_rel_addend (input_bfd, rel,
8452 howto, contents);
8453 if (hi16_reloc_p (r_type)
8454 || (got16_reloc_p (r_type)
8455 && mips_elf_local_relocation_p (input_bfd, rel,
8456 local_sections, FALSE)))
8458 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
8459 contents, &addend))
8461 const char *name;
8463 if (h)
8464 name = h->root.root.string;
8465 else
8466 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
8467 local_syms + r_symndx,
8468 sec);
8469 (*_bfd_error_handler)
8470 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
8471 input_bfd, input_section, name, howto->name,
8472 rel->r_offset);
8475 else
8476 addend <<= howto->rightshift;
8478 else
8479 addend = rel->r_addend;
8480 mips_elf_adjust_addend (output_bfd, info, input_bfd,
8481 local_syms, local_sections, rel);
8484 if (info->relocatable)
8486 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
8487 && bfd_big_endian (input_bfd))
8488 rel->r_offset -= 4;
8490 if (!rela_relocation_p && rel->r_addend)
8492 addend += rel->r_addend;
8493 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
8494 addend = mips_elf_high (addend);
8495 else if (r_type == R_MIPS_HIGHER)
8496 addend = mips_elf_higher (addend);
8497 else if (r_type == R_MIPS_HIGHEST)
8498 addend = mips_elf_highest (addend);
8499 else
8500 addend >>= howto->rightshift;
8502 /* We use the source mask, rather than the destination
8503 mask because the place to which we are writing will be
8504 source of the addend in the final link. */
8505 addend &= howto->src_mask;
8507 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
8508 /* See the comment above about using R_MIPS_64 in the 32-bit
8509 ABI. Here, we need to update the addend. It would be
8510 possible to get away with just using the R_MIPS_32 reloc
8511 but for endianness. */
8513 bfd_vma sign_bits;
8514 bfd_vma low_bits;
8515 bfd_vma high_bits;
8517 if (addend & ((bfd_vma) 1 << 31))
8518 #ifdef BFD64
8519 sign_bits = ((bfd_vma) 1 << 32) - 1;
8520 #else
8521 sign_bits = -1;
8522 #endif
8523 else
8524 sign_bits = 0;
8526 /* If we don't know that we have a 64-bit type,
8527 do two separate stores. */
8528 if (bfd_big_endian (input_bfd))
8530 /* Store the sign-bits (which are most significant)
8531 first. */
8532 low_bits = sign_bits;
8533 high_bits = addend;
8535 else
8537 low_bits = addend;
8538 high_bits = sign_bits;
8540 bfd_put_32 (input_bfd, low_bits,
8541 contents + rel->r_offset);
8542 bfd_put_32 (input_bfd, high_bits,
8543 contents + rel->r_offset + 4);
8544 continue;
8547 if (! mips_elf_perform_relocation (info, howto, rel, addend,
8548 input_bfd, input_section,
8549 contents, FALSE))
8550 return FALSE;
8553 /* Go on to the next relocation. */
8554 continue;
8557 /* In the N32 and 64-bit ABIs there may be multiple consecutive
8558 relocations for the same offset. In that case we are
8559 supposed to treat the output of each relocation as the addend
8560 for the next. */
8561 if (rel + 1 < relend
8562 && rel->r_offset == rel[1].r_offset
8563 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
8564 use_saved_addend_p = TRUE;
8565 else
8566 use_saved_addend_p = FALSE;
8568 /* Figure out what value we are supposed to relocate. */
8569 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
8570 input_section, info, rel,
8571 addend, howto, local_syms,
8572 local_sections, &value,
8573 &name, &require_jalx,
8574 use_saved_addend_p))
8576 case bfd_reloc_continue:
8577 /* There's nothing to do. */
8578 continue;
8580 case bfd_reloc_undefined:
8581 /* mips_elf_calculate_relocation already called the
8582 undefined_symbol callback. There's no real point in
8583 trying to perform the relocation at this point, so we
8584 just skip ahead to the next relocation. */
8585 continue;
8587 case bfd_reloc_notsupported:
8588 msg = _("internal error: unsupported relocation error");
8589 info->callbacks->warning
8590 (info, msg, name, input_bfd, input_section, rel->r_offset);
8591 return FALSE;
8593 case bfd_reloc_overflow:
8594 if (use_saved_addend_p)
8595 /* Ignore overflow until we reach the last relocation for
8596 a given location. */
8598 else
8600 struct mips_elf_link_hash_table *htab;
8602 htab = mips_elf_hash_table (info);
8603 BFD_ASSERT (name != NULL);
8604 if (!htab->small_data_overflow_reported
8605 && (howto->type == R_MIPS_GPREL16
8606 || howto->type == R_MIPS_LITERAL))
8608 const char *msg =
8609 _("small-data section exceeds 64KB;"
8610 " lower small-data size limit (see option -G)");
8612 htab->small_data_overflow_reported = TRUE;
8613 (*info->callbacks->einfo) ("%P: %s\n", msg);
8615 if (! ((*info->callbacks->reloc_overflow)
8616 (info, NULL, name, howto->name, (bfd_vma) 0,
8617 input_bfd, input_section, rel->r_offset)))
8618 return FALSE;
8620 break;
8622 case bfd_reloc_ok:
8623 break;
8625 default:
8626 abort ();
8627 break;
8630 /* If we've got another relocation for the address, keep going
8631 until we reach the last one. */
8632 if (use_saved_addend_p)
8634 addend = value;
8635 continue;
8638 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
8639 /* See the comment above about using R_MIPS_64 in the 32-bit
8640 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
8641 that calculated the right value. Now, however, we
8642 sign-extend the 32-bit result to 64-bits, and store it as a
8643 64-bit value. We are especially generous here in that we
8644 go to extreme lengths to support this usage on systems with
8645 only a 32-bit VMA. */
8647 bfd_vma sign_bits;
8648 bfd_vma low_bits;
8649 bfd_vma high_bits;
8651 if (value & ((bfd_vma) 1 << 31))
8652 #ifdef BFD64
8653 sign_bits = ((bfd_vma) 1 << 32) - 1;
8654 #else
8655 sign_bits = -1;
8656 #endif
8657 else
8658 sign_bits = 0;
8660 /* If we don't know that we have a 64-bit type,
8661 do two separate stores. */
8662 if (bfd_big_endian (input_bfd))
8664 /* Undo what we did above. */
8665 rel->r_offset -= 4;
8666 /* Store the sign-bits (which are most significant)
8667 first. */
8668 low_bits = sign_bits;
8669 high_bits = value;
8671 else
8673 low_bits = value;
8674 high_bits = sign_bits;
8676 bfd_put_32 (input_bfd, low_bits,
8677 contents + rel->r_offset);
8678 bfd_put_32 (input_bfd, high_bits,
8679 contents + rel->r_offset + 4);
8680 continue;
8683 /* Actually perform the relocation. */
8684 if (! mips_elf_perform_relocation (info, howto, rel, value,
8685 input_bfd, input_section,
8686 contents, require_jalx))
8687 return FALSE;
8690 return TRUE;
8693 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8694 adjust it appropriately now. */
8696 static void
8697 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
8698 const char *name, Elf_Internal_Sym *sym)
8700 /* The linker script takes care of providing names and values for
8701 these, but we must place them into the right sections. */
8702 static const char* const text_section_symbols[] = {
8703 "_ftext",
8704 "_etext",
8705 "__dso_displacement",
8706 "__elf_header",
8707 "__program_header_table",
8708 NULL
8711 static const char* const data_section_symbols[] = {
8712 "_fdata",
8713 "_edata",
8714 "_end",
8715 "_fbss",
8716 NULL
8719 const char* const *p;
8720 int i;
8722 for (i = 0; i < 2; ++i)
8723 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8725 ++p)
8726 if (strcmp (*p, name) == 0)
8728 /* All of these symbols are given type STT_SECTION by the
8729 IRIX6 linker. */
8730 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8731 sym->st_other = STO_PROTECTED;
8733 /* The IRIX linker puts these symbols in special sections. */
8734 if (i == 0)
8735 sym->st_shndx = SHN_MIPS_TEXT;
8736 else
8737 sym->st_shndx = SHN_MIPS_DATA;
8739 break;
8743 /* Finish up dynamic symbol handling. We set the contents of various
8744 dynamic sections here. */
8746 bfd_boolean
8747 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
8748 struct bfd_link_info *info,
8749 struct elf_link_hash_entry *h,
8750 Elf_Internal_Sym *sym)
8752 bfd *dynobj;
8753 asection *sgot;
8754 struct mips_got_info *g, *gg;
8755 const char *name;
8756 int idx;
8757 struct mips_elf_link_hash_table *htab;
8758 struct mips_elf_link_hash_entry *hmips;
8760 htab = mips_elf_hash_table (info);
8761 dynobj = elf_hash_table (info)->dynobj;
8762 hmips = (struct mips_elf_link_hash_entry *) h;
8764 if (h->plt.offset != MINUS_ONE)
8766 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
8768 /* This symbol has a stub. Set it up. */
8770 BFD_ASSERT (h->dynindx != -1);
8772 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8773 || (h->dynindx <= 0xffff));
8775 /* Values up to 2^31 - 1 are allowed. Larger values would cause
8776 sign extension at runtime in the stub, resulting in a negative
8777 index value. */
8778 if (h->dynindx & ~0x7fffffff)
8779 return FALSE;
8781 /* Fill the stub. */
8782 idx = 0;
8783 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
8784 idx += 4;
8785 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
8786 idx += 4;
8787 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8789 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
8790 stub + idx);
8791 idx += 4;
8793 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
8794 idx += 4;
8796 /* If a large stub is not required and sign extension is not a
8797 problem, then use legacy code in the stub. */
8798 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
8799 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
8800 else if (h->dynindx & ~0x7fff)
8801 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
8802 else
8803 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
8804 stub + idx);
8806 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
8807 memcpy (htab->sstubs->contents + h->plt.offset,
8808 stub, htab->function_stub_size);
8810 /* Mark the symbol as undefined. plt.offset != -1 occurs
8811 only for the referenced symbol. */
8812 sym->st_shndx = SHN_UNDEF;
8814 /* The run-time linker uses the st_value field of the symbol
8815 to reset the global offset table entry for this external
8816 to its stub address when unlinking a shared object. */
8817 sym->st_value = (htab->sstubs->output_section->vma
8818 + htab->sstubs->output_offset
8819 + h->plt.offset);
8822 /* If we have a MIPS16 function with a stub, the dynamic symbol must
8823 refer to the stub, since only the stub uses the standard calling
8824 conventions. */
8825 if (h->dynindx != -1 && hmips->fn_stub != NULL)
8827 BFD_ASSERT (hmips->need_fn_stub);
8828 sym->st_value = (hmips->fn_stub->output_section->vma
8829 + hmips->fn_stub->output_offset);
8830 sym->st_size = hmips->fn_stub->size;
8831 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
8834 BFD_ASSERT (h->dynindx != -1
8835 || h->forced_local);
8837 sgot = mips_elf_got_section (info);
8838 BFD_ASSERT (sgot != NULL);
8839 g = htab->got_info;
8840 BFD_ASSERT (g != NULL);
8842 /* Run through the global symbol table, creating GOT entries for all
8843 the symbols that need them. */
8844 if (g->global_gotsym != NULL
8845 && h->dynindx >= g->global_gotsym->dynindx)
8847 bfd_vma offset;
8848 bfd_vma value;
8850 value = sym->st_value;
8851 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
8852 R_MIPS_GOT16, info);
8853 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8856 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
8858 struct mips_got_entry e, *p;
8859 bfd_vma entry;
8860 bfd_vma offset;
8862 gg = g;
8864 e.abfd = output_bfd;
8865 e.symndx = -1;
8866 e.d.h = hmips;
8867 e.tls_type = 0;
8869 for (g = g->next; g->next != gg; g = g->next)
8871 if (g->got_entries
8872 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
8873 &e)))
8875 offset = p->gotidx;
8876 if (info->shared
8877 || (elf_hash_table (info)->dynamic_sections_created
8878 && p->d.h != NULL
8879 && p->d.h->root.def_dynamic
8880 && !p->d.h->root.def_regular))
8882 /* Create an R_MIPS_REL32 relocation for this entry. Due to
8883 the various compatibility problems, it's easier to mock
8884 up an R_MIPS_32 or R_MIPS_64 relocation and leave
8885 mips_elf_create_dynamic_relocation to calculate the
8886 appropriate addend. */
8887 Elf_Internal_Rela rel[3];
8889 memset (rel, 0, sizeof (rel));
8890 if (ABI_64_P (output_bfd))
8891 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
8892 else
8893 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
8894 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
8896 entry = 0;
8897 if (! (mips_elf_create_dynamic_relocation
8898 (output_bfd, info, rel,
8899 e.d.h, NULL, sym->st_value, &entry, sgot)))
8900 return FALSE;
8902 else
8903 entry = sym->st_value;
8904 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
8909 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8910 name = h->root.root.string;
8911 if (strcmp (name, "_DYNAMIC") == 0
8912 || h == elf_hash_table (info)->hgot)
8913 sym->st_shndx = SHN_ABS;
8914 else if (strcmp (name, "_DYNAMIC_LINK") == 0
8915 || strcmp (name, "_DYNAMIC_LINKING") == 0)
8917 sym->st_shndx = SHN_ABS;
8918 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8919 sym->st_value = 1;
8921 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
8923 sym->st_shndx = SHN_ABS;
8924 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8925 sym->st_value = elf_gp (output_bfd);
8927 else if (SGI_COMPAT (output_bfd))
8929 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8930 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8932 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8933 sym->st_other = STO_PROTECTED;
8934 sym->st_value = 0;
8935 sym->st_shndx = SHN_MIPS_DATA;
8937 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8939 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8940 sym->st_other = STO_PROTECTED;
8941 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8942 sym->st_shndx = SHN_ABS;
8944 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8946 if (h->type == STT_FUNC)
8947 sym->st_shndx = SHN_MIPS_TEXT;
8948 else if (h->type == STT_OBJECT)
8949 sym->st_shndx = SHN_MIPS_DATA;
8953 /* Handle the IRIX6-specific symbols. */
8954 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8955 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8957 if (! info->shared)
8959 if (! mips_elf_hash_table (info)->use_rld_obj_head
8960 && (strcmp (name, "__rld_map") == 0
8961 || strcmp (name, "__RLD_MAP") == 0))
8963 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8964 BFD_ASSERT (s != NULL);
8965 sym->st_value = s->output_section->vma + s->output_offset;
8966 bfd_put_32 (output_bfd, 0, s->contents);
8967 if (mips_elf_hash_table (info)->rld_value == 0)
8968 mips_elf_hash_table (info)->rld_value = sym->st_value;
8970 else if (mips_elf_hash_table (info)->use_rld_obj_head
8971 && strcmp (name, "__rld_obj_head") == 0)
8973 /* IRIX6 does not use a .rld_map section. */
8974 if (IRIX_COMPAT (output_bfd) == ict_irix5
8975 || IRIX_COMPAT (output_bfd) == ict_none)
8976 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8977 != NULL);
8978 mips_elf_hash_table (info)->rld_value = sym->st_value;
8982 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
8983 treat MIPS16 symbols like any other. */
8984 if (ELF_ST_IS_MIPS16 (sym->st_other))
8986 BFD_ASSERT (sym->st_value & 1);
8987 sym->st_other -= STO_MIPS16;
8990 return TRUE;
8993 /* Likewise, for VxWorks. */
8995 bfd_boolean
8996 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
8997 struct bfd_link_info *info,
8998 struct elf_link_hash_entry *h,
8999 Elf_Internal_Sym *sym)
9001 bfd *dynobj;
9002 asection *sgot;
9003 struct mips_got_info *g;
9004 struct mips_elf_link_hash_table *htab;
9006 htab = mips_elf_hash_table (info);
9007 dynobj = elf_hash_table (info)->dynobj;
9009 if (h->plt.offset != (bfd_vma) -1)
9011 bfd_byte *loc;
9012 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9013 Elf_Internal_Rela rel;
9014 static const bfd_vma *plt_entry;
9016 BFD_ASSERT (h->dynindx != -1);
9017 BFD_ASSERT (htab->splt != NULL);
9018 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9020 /* Calculate the address of the .plt entry. */
9021 plt_address = (htab->splt->output_section->vma
9022 + htab->splt->output_offset
9023 + h->plt.offset);
9025 /* Calculate the index of the entry. */
9026 plt_index = ((h->plt.offset - htab->plt_header_size)
9027 / htab->plt_entry_size);
9029 /* Calculate the address of the .got.plt entry. */
9030 got_address = (htab->sgotplt->output_section->vma
9031 + htab->sgotplt->output_offset
9032 + plt_index * 4);
9034 /* Calculate the offset of the .got.plt entry from
9035 _GLOBAL_OFFSET_TABLE_. */
9036 got_offset = mips_elf_gotplt_index (info, h);
9038 /* Calculate the offset for the branch at the start of the PLT
9039 entry. The branch jumps to the beginning of .plt. */
9040 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9042 /* Fill in the initial value of the .got.plt entry. */
9043 bfd_put_32 (output_bfd, plt_address,
9044 htab->sgotplt->contents + plt_index * 4);
9046 /* Find out where the .plt entry should go. */
9047 loc = htab->splt->contents + h->plt.offset;
9049 if (info->shared)
9051 plt_entry = mips_vxworks_shared_plt_entry;
9052 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9053 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9055 else
9057 bfd_vma got_address_high, got_address_low;
9059 plt_entry = mips_vxworks_exec_plt_entry;
9060 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9061 got_address_low = got_address & 0xffff;
9063 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9064 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9065 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9066 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9067 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9068 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9069 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9070 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9072 loc = (htab->srelplt2->contents
9073 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9075 /* Emit a relocation for the .got.plt entry. */
9076 rel.r_offset = got_address;
9077 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9078 rel.r_addend = h->plt.offset;
9079 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9081 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9082 loc += sizeof (Elf32_External_Rela);
9083 rel.r_offset = plt_address + 8;
9084 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9085 rel.r_addend = got_offset;
9086 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9088 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9089 loc += sizeof (Elf32_External_Rela);
9090 rel.r_offset += 4;
9091 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9092 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9095 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9096 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9097 rel.r_offset = got_address;
9098 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9099 rel.r_addend = 0;
9100 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9102 if (!h->def_regular)
9103 sym->st_shndx = SHN_UNDEF;
9106 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9108 sgot = mips_elf_got_section (info);
9109 BFD_ASSERT (sgot != NULL);
9110 g = htab->got_info;
9111 BFD_ASSERT (g != NULL);
9113 /* See if this symbol has an entry in the GOT. */
9114 if (g->global_gotsym != NULL
9115 && h->dynindx >= g->global_gotsym->dynindx)
9117 bfd_vma offset;
9118 Elf_Internal_Rela outrel;
9119 bfd_byte *loc;
9120 asection *s;
9122 /* Install the symbol value in the GOT. */
9123 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9124 R_MIPS_GOT16, info);
9125 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9127 /* Add a dynamic relocation for it. */
9128 s = mips_elf_rel_dyn_section (info, FALSE);
9129 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9130 outrel.r_offset = (sgot->output_section->vma
9131 + sgot->output_offset
9132 + offset);
9133 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9134 outrel.r_addend = 0;
9135 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9138 /* Emit a copy reloc, if needed. */
9139 if (h->needs_copy)
9141 Elf_Internal_Rela rel;
9143 BFD_ASSERT (h->dynindx != -1);
9145 rel.r_offset = (h->root.u.def.section->output_section->vma
9146 + h->root.u.def.section->output_offset
9147 + h->root.u.def.value);
9148 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9149 rel.r_addend = 0;
9150 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9151 htab->srelbss->contents
9152 + (htab->srelbss->reloc_count
9153 * sizeof (Elf32_External_Rela)));
9154 ++htab->srelbss->reloc_count;
9157 /* If this is a mips16 symbol, force the value to be even. */
9158 if (ELF_ST_IS_MIPS16 (sym->st_other))
9159 sym->st_value &= ~1;
9161 return TRUE;
9164 /* Install the PLT header for a VxWorks executable and finalize the
9165 contents of .rela.plt.unloaded. */
9167 static void
9168 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9170 Elf_Internal_Rela rela;
9171 bfd_byte *loc;
9172 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9173 static const bfd_vma *plt_entry;
9174 struct mips_elf_link_hash_table *htab;
9176 htab = mips_elf_hash_table (info);
9177 plt_entry = mips_vxworks_exec_plt0_entry;
9179 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9180 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9181 + htab->root.hgot->root.u.def.section->output_offset
9182 + htab->root.hgot->root.u.def.value);
9184 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9185 got_value_low = got_value & 0xffff;
9187 /* Calculate the address of the PLT header. */
9188 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
9190 /* Install the PLT header. */
9191 loc = htab->splt->contents;
9192 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
9193 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
9194 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
9195 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9196 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9197 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9199 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
9200 loc = htab->srelplt2->contents;
9201 rela.r_offset = plt_address;
9202 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9203 rela.r_addend = 0;
9204 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9205 loc += sizeof (Elf32_External_Rela);
9207 /* Output the relocation for the following addiu of
9208 %lo(_GLOBAL_OFFSET_TABLE_). */
9209 rela.r_offset += 4;
9210 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9211 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
9212 loc += sizeof (Elf32_External_Rela);
9214 /* Fix up the remaining relocations. They may have the wrong
9215 symbol index for _G_O_T_ or _P_L_T_ depending on the order
9216 in which symbols were output. */
9217 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
9219 Elf_Internal_Rela rel;
9221 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9222 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9223 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9224 loc += sizeof (Elf32_External_Rela);
9226 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9227 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9228 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9229 loc += sizeof (Elf32_External_Rela);
9231 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
9232 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9233 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9234 loc += sizeof (Elf32_External_Rela);
9238 /* Install the PLT header for a VxWorks shared library. */
9240 static void
9241 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
9243 unsigned int i;
9244 struct mips_elf_link_hash_table *htab;
9246 htab = mips_elf_hash_table (info);
9248 /* We just need to copy the entry byte-by-byte. */
9249 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
9250 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
9251 htab->splt->contents + i * 4);
9254 /* Finish up the dynamic sections. */
9256 bfd_boolean
9257 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
9258 struct bfd_link_info *info)
9260 bfd *dynobj;
9261 asection *sdyn;
9262 asection *sgot;
9263 struct mips_got_info *gg, *g;
9264 struct mips_elf_link_hash_table *htab;
9266 htab = mips_elf_hash_table (info);
9267 dynobj = elf_hash_table (info)->dynobj;
9269 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
9271 sgot = mips_elf_got_section (info);
9272 if (sgot == NULL)
9273 gg = g = NULL;
9274 else
9276 gg = htab->got_info;
9277 g = mips_elf_got_for_ibfd (gg, output_bfd);
9278 BFD_ASSERT (g != NULL);
9281 if (elf_hash_table (info)->dynamic_sections_created)
9283 bfd_byte *b;
9284 int dyn_to_skip = 0, dyn_skipped = 0;
9286 BFD_ASSERT (sdyn != NULL);
9287 BFD_ASSERT (g != NULL);
9289 for (b = sdyn->contents;
9290 b < sdyn->contents + sdyn->size;
9291 b += MIPS_ELF_DYN_SIZE (dynobj))
9293 Elf_Internal_Dyn dyn;
9294 const char *name;
9295 size_t elemsize;
9296 asection *s;
9297 bfd_boolean swap_out_p;
9299 /* Read in the current dynamic entry. */
9300 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9302 /* Assume that we're going to modify it and write it out. */
9303 swap_out_p = TRUE;
9305 switch (dyn.d_tag)
9307 case DT_RELENT:
9308 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
9309 break;
9311 case DT_RELAENT:
9312 BFD_ASSERT (htab->is_vxworks);
9313 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
9314 break;
9316 case DT_STRSZ:
9317 /* Rewrite DT_STRSZ. */
9318 dyn.d_un.d_val =
9319 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
9320 break;
9322 case DT_PLTGOT:
9323 name = ".got";
9324 if (htab->is_vxworks)
9326 /* _GLOBAL_OFFSET_TABLE_ is defined to be the beginning
9327 of the ".got" section in DYNOBJ. */
9328 s = bfd_get_section_by_name (dynobj, name);
9329 BFD_ASSERT (s != NULL);
9330 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
9332 else
9334 s = bfd_get_section_by_name (output_bfd, name);
9335 BFD_ASSERT (s != NULL);
9336 dyn.d_un.d_ptr = s->vma;
9338 break;
9340 case DT_MIPS_RLD_VERSION:
9341 dyn.d_un.d_val = 1; /* XXX */
9342 break;
9344 case DT_MIPS_FLAGS:
9345 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
9346 break;
9348 case DT_MIPS_TIME_STAMP:
9350 time_t t;
9351 time (&t);
9352 dyn.d_un.d_val = t;
9354 break;
9356 case DT_MIPS_ICHECKSUM:
9357 /* XXX FIXME: */
9358 swap_out_p = FALSE;
9359 break;
9361 case DT_MIPS_IVERSION:
9362 /* XXX FIXME: */
9363 swap_out_p = FALSE;
9364 break;
9366 case DT_MIPS_BASE_ADDRESS:
9367 s = output_bfd->sections;
9368 BFD_ASSERT (s != NULL);
9369 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
9370 break;
9372 case DT_MIPS_LOCAL_GOTNO:
9373 dyn.d_un.d_val = g->local_gotno;
9374 break;
9376 case DT_MIPS_UNREFEXTNO:
9377 /* The index into the dynamic symbol table which is the
9378 entry of the first external symbol that is not
9379 referenced within the same object. */
9380 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
9381 break;
9383 case DT_MIPS_GOTSYM:
9384 if (gg->global_gotsym)
9386 dyn.d_un.d_val = gg->global_gotsym->dynindx;
9387 break;
9389 /* In case if we don't have global got symbols we default
9390 to setting DT_MIPS_GOTSYM to the same value as
9391 DT_MIPS_SYMTABNO, so we just fall through. */
9393 case DT_MIPS_SYMTABNO:
9394 name = ".dynsym";
9395 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
9396 s = bfd_get_section_by_name (output_bfd, name);
9397 BFD_ASSERT (s != NULL);
9399 dyn.d_un.d_val = s->size / elemsize;
9400 break;
9402 case DT_MIPS_HIPAGENO:
9403 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO (info);
9404 break;
9406 case DT_MIPS_RLD_MAP:
9407 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
9408 break;
9410 case DT_MIPS_OPTIONS:
9411 s = (bfd_get_section_by_name
9412 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
9413 dyn.d_un.d_ptr = s->vma;
9414 break;
9416 case DT_RELASZ:
9417 BFD_ASSERT (htab->is_vxworks);
9418 /* The count does not include the JUMP_SLOT relocations. */
9419 if (htab->srelplt)
9420 dyn.d_un.d_val -= htab->srelplt->size;
9421 break;
9423 case DT_PLTREL:
9424 BFD_ASSERT (htab->is_vxworks);
9425 dyn.d_un.d_val = DT_RELA;
9426 break;
9428 case DT_PLTRELSZ:
9429 BFD_ASSERT (htab->is_vxworks);
9430 dyn.d_un.d_val = htab->srelplt->size;
9431 break;
9433 case DT_JMPREL:
9434 BFD_ASSERT (htab->is_vxworks);
9435 dyn.d_un.d_val = (htab->srelplt->output_section->vma
9436 + htab->srelplt->output_offset);
9437 break;
9439 case DT_TEXTREL:
9440 /* If we didn't need any text relocations after all, delete
9441 the dynamic tag. */
9442 if (!(info->flags & DF_TEXTREL))
9444 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
9445 swap_out_p = FALSE;
9447 break;
9449 case DT_FLAGS:
9450 /* If we didn't need any text relocations after all, clear
9451 DF_TEXTREL from DT_FLAGS. */
9452 if (!(info->flags & DF_TEXTREL))
9453 dyn.d_un.d_val &= ~DF_TEXTREL;
9454 else
9455 swap_out_p = FALSE;
9456 break;
9458 default:
9459 swap_out_p = FALSE;
9460 if (htab->is_vxworks
9461 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
9462 swap_out_p = TRUE;
9463 break;
9466 if (swap_out_p || dyn_skipped)
9467 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9468 (dynobj, &dyn, b - dyn_skipped);
9470 if (dyn_to_skip)
9472 dyn_skipped += dyn_to_skip;
9473 dyn_to_skip = 0;
9477 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
9478 if (dyn_skipped > 0)
9479 memset (b - dyn_skipped, 0, dyn_skipped);
9482 if (sgot != NULL && sgot->size > 0
9483 && !bfd_is_abs_section (sgot->output_section))
9485 if (htab->is_vxworks)
9487 /* The first entry of the global offset table points to the
9488 ".dynamic" section. The second is initialized by the
9489 loader and contains the shared library identifier.
9490 The third is also initialized by the loader and points
9491 to the lazy resolution stub. */
9492 MIPS_ELF_PUT_WORD (output_bfd,
9493 sdyn->output_offset + sdyn->output_section->vma,
9494 sgot->contents);
9495 MIPS_ELF_PUT_WORD (output_bfd, 0,
9496 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9497 MIPS_ELF_PUT_WORD (output_bfd, 0,
9498 sgot->contents
9499 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
9501 else
9503 /* The first entry of the global offset table will be filled at
9504 runtime. The second entry will be used by some runtime loaders.
9505 This isn't the case of IRIX rld. */
9506 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
9507 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
9508 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9511 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
9512 = MIPS_ELF_GOT_SIZE (output_bfd);
9515 /* Generate dynamic relocations for the non-primary gots. */
9516 if (gg != NULL && gg->next)
9518 Elf_Internal_Rela rel[3];
9519 bfd_vma addend = 0;
9521 memset (rel, 0, sizeof (rel));
9522 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
9524 for (g = gg->next; g->next != gg; g = g->next)
9526 bfd_vma index = g->next->local_gotno + g->next->global_gotno
9527 + g->next->tls_gotno;
9529 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
9530 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9531 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
9532 sgot->contents
9533 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9535 if (! info->shared)
9536 continue;
9538 while (index < g->assigned_gotno)
9540 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
9541 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
9542 if (!(mips_elf_create_dynamic_relocation
9543 (output_bfd, info, rel, NULL,
9544 bfd_abs_section_ptr,
9545 0, &addend, sgot)))
9546 return FALSE;
9547 BFD_ASSERT (addend == 0);
9552 /* The generation of dynamic relocations for the non-primary gots
9553 adds more dynamic relocations. We cannot count them until
9554 here. */
9556 if (elf_hash_table (info)->dynamic_sections_created)
9558 bfd_byte *b;
9559 bfd_boolean swap_out_p;
9561 BFD_ASSERT (sdyn != NULL);
9563 for (b = sdyn->contents;
9564 b < sdyn->contents + sdyn->size;
9565 b += MIPS_ELF_DYN_SIZE (dynobj))
9567 Elf_Internal_Dyn dyn;
9568 asection *s;
9570 /* Read in the current dynamic entry. */
9571 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
9573 /* Assume that we're going to modify it and write it out. */
9574 swap_out_p = TRUE;
9576 switch (dyn.d_tag)
9578 case DT_RELSZ:
9579 /* Reduce DT_RELSZ to account for any relocations we
9580 decided not to make. This is for the n64 irix rld,
9581 which doesn't seem to apply any relocations if there
9582 are trailing null entries. */
9583 s = mips_elf_rel_dyn_section (info, FALSE);
9584 dyn.d_un.d_val = (s->reloc_count
9585 * (ABI_64_P (output_bfd)
9586 ? sizeof (Elf64_Mips_External_Rel)
9587 : sizeof (Elf32_External_Rel)));
9588 /* Adjust the section size too. Tools like the prelinker
9589 can reasonably expect the values to the same. */
9590 elf_section_data (s->output_section)->this_hdr.sh_size
9591 = dyn.d_un.d_val;
9592 break;
9594 default:
9595 swap_out_p = FALSE;
9596 break;
9599 if (swap_out_p)
9600 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9601 (dynobj, &dyn, b);
9606 asection *s;
9607 Elf32_compact_rel cpt;
9609 if (SGI_COMPAT (output_bfd))
9611 /* Write .compact_rel section out. */
9612 s = bfd_get_section_by_name (dynobj, ".compact_rel");
9613 if (s != NULL)
9615 cpt.id1 = 1;
9616 cpt.num = s->reloc_count;
9617 cpt.id2 = 2;
9618 cpt.offset = (s->output_section->filepos
9619 + sizeof (Elf32_External_compact_rel));
9620 cpt.reserved0 = 0;
9621 cpt.reserved1 = 0;
9622 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
9623 ((Elf32_External_compact_rel *)
9624 s->contents));
9626 /* Clean up a dummy stub function entry in .text. */
9627 if (htab->sstubs != NULL)
9629 file_ptr dummy_offset;
9631 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
9632 dummy_offset = htab->sstubs->size - htab->function_stub_size;
9633 memset (htab->sstubs->contents + dummy_offset, 0,
9634 htab->function_stub_size);
9639 /* The psABI says that the dynamic relocations must be sorted in
9640 increasing order of r_symndx. The VxWorks EABI doesn't require
9641 this, and because the code below handles REL rather than RELA
9642 relocations, using it for VxWorks would be outright harmful. */
9643 if (!htab->is_vxworks)
9645 s = mips_elf_rel_dyn_section (info, FALSE);
9646 if (s != NULL
9647 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
9649 reldyn_sorting_bfd = output_bfd;
9651 if (ABI_64_P (output_bfd))
9652 qsort ((Elf64_External_Rel *) s->contents + 1,
9653 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
9654 sort_dynamic_relocs_64);
9655 else
9656 qsort ((Elf32_External_Rel *) s->contents + 1,
9657 s->reloc_count - 1, sizeof (Elf32_External_Rel),
9658 sort_dynamic_relocs);
9663 if (htab->is_vxworks && htab->splt->size > 0)
9665 if (info->shared)
9666 mips_vxworks_finish_shared_plt (output_bfd, info);
9667 else
9668 mips_vxworks_finish_exec_plt (output_bfd, info);
9670 return TRUE;
9674 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
9676 static void
9677 mips_set_isa_flags (bfd *abfd)
9679 flagword val;
9681 switch (bfd_get_mach (abfd))
9683 default:
9684 case bfd_mach_mips3000:
9685 val = E_MIPS_ARCH_1;
9686 break;
9688 case bfd_mach_mips3900:
9689 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
9690 break;
9692 case bfd_mach_mips6000:
9693 val = E_MIPS_ARCH_2;
9694 break;
9696 case bfd_mach_mips4000:
9697 case bfd_mach_mips4300:
9698 case bfd_mach_mips4400:
9699 case bfd_mach_mips4600:
9700 val = E_MIPS_ARCH_3;
9701 break;
9703 case bfd_mach_mips4010:
9704 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
9705 break;
9707 case bfd_mach_mips4100:
9708 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
9709 break;
9711 case bfd_mach_mips4111:
9712 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
9713 break;
9715 case bfd_mach_mips4120:
9716 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
9717 break;
9719 case bfd_mach_mips4650:
9720 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
9721 break;
9723 case bfd_mach_mips5400:
9724 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
9725 break;
9727 case bfd_mach_mips5500:
9728 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
9729 break;
9731 case bfd_mach_mips9000:
9732 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
9733 break;
9735 case bfd_mach_mips5000:
9736 case bfd_mach_mips7000:
9737 case bfd_mach_mips8000:
9738 case bfd_mach_mips10000:
9739 case bfd_mach_mips12000:
9740 val = E_MIPS_ARCH_4;
9741 break;
9743 case bfd_mach_mips5:
9744 val = E_MIPS_ARCH_5;
9745 break;
9747 case bfd_mach_mips_loongson_2e:
9748 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
9749 break;
9751 case bfd_mach_mips_loongson_2f:
9752 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
9753 break;
9755 case bfd_mach_mips_sb1:
9756 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
9757 break;
9759 case bfd_mach_mips_octeon:
9760 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
9761 break;
9763 case bfd_mach_mipsisa32:
9764 val = E_MIPS_ARCH_32;
9765 break;
9767 case bfd_mach_mipsisa64:
9768 val = E_MIPS_ARCH_64;
9769 break;
9771 case bfd_mach_mipsisa32r2:
9772 val = E_MIPS_ARCH_32R2;
9773 break;
9775 case bfd_mach_mipsisa64r2:
9776 val = E_MIPS_ARCH_64R2;
9777 break;
9779 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
9780 elf_elfheader (abfd)->e_flags |= val;
9785 /* The final processing done just before writing out a MIPS ELF object
9786 file. This gets the MIPS architecture right based on the machine
9787 number. This is used by both the 32-bit and the 64-bit ABI. */
9789 void
9790 _bfd_mips_elf_final_write_processing (bfd *abfd,
9791 bfd_boolean linker ATTRIBUTE_UNUSED)
9793 unsigned int i;
9794 Elf_Internal_Shdr **hdrpp;
9795 const char *name;
9796 asection *sec;
9798 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
9799 is nonzero. This is for compatibility with old objects, which used
9800 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
9801 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
9802 mips_set_isa_flags (abfd);
9804 /* Set the sh_info field for .gptab sections and other appropriate
9805 info for each special section. */
9806 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
9807 i < elf_numsections (abfd);
9808 i++, hdrpp++)
9810 switch ((*hdrpp)->sh_type)
9812 case SHT_MIPS_MSYM:
9813 case SHT_MIPS_LIBLIST:
9814 sec = bfd_get_section_by_name (abfd, ".dynstr");
9815 if (sec != NULL)
9816 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9817 break;
9819 case SHT_MIPS_GPTAB:
9820 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9821 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9822 BFD_ASSERT (name != NULL
9823 && CONST_STRNEQ (name, ".gptab."));
9824 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
9825 BFD_ASSERT (sec != NULL);
9826 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
9827 break;
9829 case SHT_MIPS_CONTENT:
9830 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9831 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9832 BFD_ASSERT (name != NULL
9833 && CONST_STRNEQ (name, ".MIPS.content"));
9834 sec = bfd_get_section_by_name (abfd,
9835 name + sizeof ".MIPS.content" - 1);
9836 BFD_ASSERT (sec != NULL);
9837 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9838 break;
9840 case SHT_MIPS_SYMBOL_LIB:
9841 sec = bfd_get_section_by_name (abfd, ".dynsym");
9842 if (sec != NULL)
9843 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9844 sec = bfd_get_section_by_name (abfd, ".liblist");
9845 if (sec != NULL)
9846 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
9847 break;
9849 case SHT_MIPS_EVENTS:
9850 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
9851 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
9852 BFD_ASSERT (name != NULL);
9853 if (CONST_STRNEQ (name, ".MIPS.events"))
9854 sec = bfd_get_section_by_name (abfd,
9855 name + sizeof ".MIPS.events" - 1);
9856 else
9858 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
9859 sec = bfd_get_section_by_name (abfd,
9860 (name
9861 + sizeof ".MIPS.post_rel" - 1));
9863 BFD_ASSERT (sec != NULL);
9864 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
9865 break;
9871 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
9872 segments. */
9875 _bfd_mips_elf_additional_program_headers (bfd *abfd,
9876 struct bfd_link_info *info ATTRIBUTE_UNUSED)
9878 asection *s;
9879 int ret = 0;
9881 /* See if we need a PT_MIPS_REGINFO segment. */
9882 s = bfd_get_section_by_name (abfd, ".reginfo");
9883 if (s && (s->flags & SEC_LOAD))
9884 ++ret;
9886 /* See if we need a PT_MIPS_OPTIONS segment. */
9887 if (IRIX_COMPAT (abfd) == ict_irix6
9888 && bfd_get_section_by_name (abfd,
9889 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
9890 ++ret;
9892 /* See if we need a PT_MIPS_RTPROC segment. */
9893 if (IRIX_COMPAT (abfd) == ict_irix5
9894 && bfd_get_section_by_name (abfd, ".dynamic")
9895 && bfd_get_section_by_name (abfd, ".mdebug"))
9896 ++ret;
9898 /* Allocate a PT_NULL header in dynamic objects. See
9899 _bfd_mips_elf_modify_segment_map for details. */
9900 if (!SGI_COMPAT (abfd)
9901 && bfd_get_section_by_name (abfd, ".dynamic"))
9902 ++ret;
9904 return ret;
9907 /* Modify the segment map for an IRIX5 executable. */
9909 bfd_boolean
9910 _bfd_mips_elf_modify_segment_map (bfd *abfd,
9911 struct bfd_link_info *info)
9913 asection *s;
9914 struct elf_segment_map *m, **pm;
9915 bfd_size_type amt;
9917 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
9918 segment. */
9919 s = bfd_get_section_by_name (abfd, ".reginfo");
9920 if (s != NULL && (s->flags & SEC_LOAD) != 0)
9922 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9923 if (m->p_type == PT_MIPS_REGINFO)
9924 break;
9925 if (m == NULL)
9927 amt = sizeof *m;
9928 m = bfd_zalloc (abfd, amt);
9929 if (m == NULL)
9930 return FALSE;
9932 m->p_type = PT_MIPS_REGINFO;
9933 m->count = 1;
9934 m->sections[0] = s;
9936 /* We want to put it after the PHDR and INTERP segments. */
9937 pm = &elf_tdata (abfd)->segment_map;
9938 while (*pm != NULL
9939 && ((*pm)->p_type == PT_PHDR
9940 || (*pm)->p_type == PT_INTERP))
9941 pm = &(*pm)->next;
9943 m->next = *pm;
9944 *pm = m;
9948 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
9949 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
9950 PT_MIPS_OPTIONS segment immediately following the program header
9951 table. */
9952 if (NEWABI_P (abfd)
9953 /* On non-IRIX6 new abi, we'll have already created a segment
9954 for this section, so don't create another. I'm not sure this
9955 is not also the case for IRIX 6, but I can't test it right
9956 now. */
9957 && IRIX_COMPAT (abfd) == ict_irix6)
9959 for (s = abfd->sections; s; s = s->next)
9960 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
9961 break;
9963 if (s)
9965 struct elf_segment_map *options_segment;
9967 pm = &elf_tdata (abfd)->segment_map;
9968 while (*pm != NULL
9969 && ((*pm)->p_type == PT_PHDR
9970 || (*pm)->p_type == PT_INTERP))
9971 pm = &(*pm)->next;
9973 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
9975 amt = sizeof (struct elf_segment_map);
9976 options_segment = bfd_zalloc (abfd, amt);
9977 options_segment->next = *pm;
9978 options_segment->p_type = PT_MIPS_OPTIONS;
9979 options_segment->p_flags = PF_R;
9980 options_segment->p_flags_valid = TRUE;
9981 options_segment->count = 1;
9982 options_segment->sections[0] = s;
9983 *pm = options_segment;
9987 else
9989 if (IRIX_COMPAT (abfd) == ict_irix5)
9991 /* If there are .dynamic and .mdebug sections, we make a room
9992 for the RTPROC header. FIXME: Rewrite without section names. */
9993 if (bfd_get_section_by_name (abfd, ".interp") == NULL
9994 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
9995 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
9997 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9998 if (m->p_type == PT_MIPS_RTPROC)
9999 break;
10000 if (m == NULL)
10002 amt = sizeof *m;
10003 m = bfd_zalloc (abfd, amt);
10004 if (m == NULL)
10005 return FALSE;
10007 m->p_type = PT_MIPS_RTPROC;
10009 s = bfd_get_section_by_name (abfd, ".rtproc");
10010 if (s == NULL)
10012 m->count = 0;
10013 m->p_flags = 0;
10014 m->p_flags_valid = 1;
10016 else
10018 m->count = 1;
10019 m->sections[0] = s;
10022 /* We want to put it after the DYNAMIC segment. */
10023 pm = &elf_tdata (abfd)->segment_map;
10024 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10025 pm = &(*pm)->next;
10026 if (*pm != NULL)
10027 pm = &(*pm)->next;
10029 m->next = *pm;
10030 *pm = m;
10034 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
10035 .dynstr, .dynsym, and .hash sections, and everything in
10036 between. */
10037 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10038 pm = &(*pm)->next)
10039 if ((*pm)->p_type == PT_DYNAMIC)
10040 break;
10041 m = *pm;
10042 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10044 /* For a normal mips executable the permissions for the PT_DYNAMIC
10045 segment are read, write and execute. We do that here since
10046 the code in elf.c sets only the read permission. This matters
10047 sometimes for the dynamic linker. */
10048 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10050 m->p_flags = PF_R | PF_W | PF_X;
10051 m->p_flags_valid = 1;
10054 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10055 glibc's dynamic linker has traditionally derived the number of
10056 tags from the p_filesz field, and sometimes allocates stack
10057 arrays of that size. An overly-big PT_DYNAMIC segment can
10058 be actively harmful in such cases. Making PT_DYNAMIC contain
10059 other sections can also make life hard for the prelinker,
10060 which might move one of the other sections to a different
10061 PT_LOAD segment. */
10062 if (SGI_COMPAT (abfd)
10063 && m != NULL
10064 && m->count == 1
10065 && strcmp (m->sections[0]->name, ".dynamic") == 0)
10067 static const char *sec_names[] =
10069 ".dynamic", ".dynstr", ".dynsym", ".hash"
10071 bfd_vma low, high;
10072 unsigned int i, c;
10073 struct elf_segment_map *n;
10075 low = ~(bfd_vma) 0;
10076 high = 0;
10077 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10079 s = bfd_get_section_by_name (abfd, sec_names[i]);
10080 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10082 bfd_size_type sz;
10084 if (low > s->vma)
10085 low = s->vma;
10086 sz = s->size;
10087 if (high < s->vma + sz)
10088 high = s->vma + sz;
10092 c = 0;
10093 for (s = abfd->sections; s != NULL; s = s->next)
10094 if ((s->flags & SEC_LOAD) != 0
10095 && s->vma >= low
10096 && s->vma + s->size <= high)
10097 ++c;
10099 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10100 n = bfd_zalloc (abfd, amt);
10101 if (n == NULL)
10102 return FALSE;
10103 *n = *m;
10104 n->count = c;
10106 i = 0;
10107 for (s = abfd->sections; s != NULL; s = s->next)
10109 if ((s->flags & SEC_LOAD) != 0
10110 && s->vma >= low
10111 && s->vma + s->size <= high)
10113 n->sections[i] = s;
10114 ++i;
10118 *pm = n;
10122 /* Allocate a spare program header in dynamic objects so that tools
10123 like the prelinker can add an extra PT_LOAD entry.
10125 If the prelinker needs to make room for a new PT_LOAD entry, its
10126 standard procedure is to move the first (read-only) sections into
10127 the new (writable) segment. However, the MIPS ABI requires
10128 .dynamic to be in a read-only segment, and the section will often
10129 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10131 Although the prelinker could in principle move .dynamic to a
10132 writable segment, it seems better to allocate a spare program
10133 header instead, and avoid the need to move any sections.
10134 There is a long tradition of allocating spare dynamic tags,
10135 so allocating a spare program header seems like a natural
10136 extension.
10138 If INFO is NULL, we may be copying an already prelinked binary
10139 with objcopy or strip, so do not add this header. */
10140 if (info != NULL
10141 && !SGI_COMPAT (abfd)
10142 && bfd_get_section_by_name (abfd, ".dynamic"))
10144 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10145 if ((*pm)->p_type == PT_NULL)
10146 break;
10147 if (*pm == NULL)
10149 m = bfd_zalloc (abfd, sizeof (*m));
10150 if (m == NULL)
10151 return FALSE;
10153 m->p_type = PT_NULL;
10154 *pm = m;
10158 return TRUE;
10161 /* Return the section that should be marked against GC for a given
10162 relocation. */
10164 asection *
10165 _bfd_mips_elf_gc_mark_hook (asection *sec,
10166 struct bfd_link_info *info,
10167 Elf_Internal_Rela *rel,
10168 struct elf_link_hash_entry *h,
10169 Elf_Internal_Sym *sym)
10171 /* ??? Do mips16 stub sections need to be handled special? */
10173 if (h != NULL)
10174 switch (ELF_R_TYPE (sec->owner, rel->r_info))
10176 case R_MIPS_GNU_VTINHERIT:
10177 case R_MIPS_GNU_VTENTRY:
10178 return NULL;
10181 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
10184 /* Update the got entry reference counts for the section being removed. */
10186 bfd_boolean
10187 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
10188 struct bfd_link_info *info ATTRIBUTE_UNUSED,
10189 asection *sec ATTRIBUTE_UNUSED,
10190 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
10192 #if 0
10193 Elf_Internal_Shdr *symtab_hdr;
10194 struct elf_link_hash_entry **sym_hashes;
10195 bfd_signed_vma *local_got_refcounts;
10196 const Elf_Internal_Rela *rel, *relend;
10197 unsigned long r_symndx;
10198 struct elf_link_hash_entry *h;
10200 if (info->relocatable)
10201 return TRUE;
10203 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10204 sym_hashes = elf_sym_hashes (abfd);
10205 local_got_refcounts = elf_local_got_refcounts (abfd);
10207 relend = relocs + sec->reloc_count;
10208 for (rel = relocs; rel < relend; rel++)
10209 switch (ELF_R_TYPE (abfd, rel->r_info))
10211 case R_MIPS16_GOT16:
10212 case R_MIPS16_CALL16:
10213 case R_MIPS_GOT16:
10214 case R_MIPS_CALL16:
10215 case R_MIPS_CALL_HI16:
10216 case R_MIPS_CALL_LO16:
10217 case R_MIPS_GOT_HI16:
10218 case R_MIPS_GOT_LO16:
10219 case R_MIPS_GOT_DISP:
10220 case R_MIPS_GOT_PAGE:
10221 case R_MIPS_GOT_OFST:
10222 /* ??? It would seem that the existing MIPS code does no sort
10223 of reference counting or whatnot on its GOT and PLT entries,
10224 so it is not possible to garbage collect them at this time. */
10225 break;
10227 default:
10228 break;
10230 #endif
10232 return TRUE;
10235 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
10236 hiding the old indirect symbol. Process additional relocation
10237 information. Also called for weakdefs, in which case we just let
10238 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
10240 void
10241 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
10242 struct elf_link_hash_entry *dir,
10243 struct elf_link_hash_entry *ind)
10245 struct mips_elf_link_hash_entry *dirmips, *indmips;
10247 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
10249 if (ind->root.type != bfd_link_hash_indirect)
10250 return;
10252 dirmips = (struct mips_elf_link_hash_entry *) dir;
10253 indmips = (struct mips_elf_link_hash_entry *) ind;
10254 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
10255 if (indmips->readonly_reloc)
10256 dirmips->readonly_reloc = TRUE;
10257 if (indmips->no_fn_stub)
10258 dirmips->no_fn_stub = TRUE;
10259 if (indmips->global_got_area < dirmips->global_got_area)
10260 dirmips->global_got_area = indmips->global_got_area;
10261 if (indmips->global_got_area < GGA_NONE)
10262 indmips->global_got_area = GGA_NONE;
10264 if (dirmips->tls_type == 0)
10265 dirmips->tls_type = indmips->tls_type;
10268 #define PDR_SIZE 32
10270 bfd_boolean
10271 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
10272 struct bfd_link_info *info)
10274 asection *o;
10275 bfd_boolean ret = FALSE;
10276 unsigned char *tdata;
10277 size_t i, skip;
10279 o = bfd_get_section_by_name (abfd, ".pdr");
10280 if (! o)
10281 return FALSE;
10282 if (o->size == 0)
10283 return FALSE;
10284 if (o->size % PDR_SIZE != 0)
10285 return FALSE;
10286 if (o->output_section != NULL
10287 && bfd_is_abs_section (o->output_section))
10288 return FALSE;
10290 tdata = bfd_zmalloc (o->size / PDR_SIZE);
10291 if (! tdata)
10292 return FALSE;
10294 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
10295 info->keep_memory);
10296 if (!cookie->rels)
10298 free (tdata);
10299 return FALSE;
10302 cookie->rel = cookie->rels;
10303 cookie->relend = cookie->rels + o->reloc_count;
10305 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
10307 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
10309 tdata[i] = 1;
10310 skip ++;
10314 if (skip != 0)
10316 mips_elf_section_data (o)->u.tdata = tdata;
10317 o->size -= skip * PDR_SIZE;
10318 ret = TRUE;
10320 else
10321 free (tdata);
10323 if (! info->keep_memory)
10324 free (cookie->rels);
10326 return ret;
10329 bfd_boolean
10330 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
10332 if (strcmp (sec->name, ".pdr") == 0)
10333 return TRUE;
10334 return FALSE;
10337 bfd_boolean
10338 _bfd_mips_elf_write_section (bfd *output_bfd,
10339 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
10340 asection *sec, bfd_byte *contents)
10342 bfd_byte *to, *from, *end;
10343 int i;
10345 if (strcmp (sec->name, ".pdr") != 0)
10346 return FALSE;
10348 if (mips_elf_section_data (sec)->u.tdata == NULL)
10349 return FALSE;
10351 to = contents;
10352 end = contents + sec->size;
10353 for (from = contents, i = 0;
10354 from < end;
10355 from += PDR_SIZE, i++)
10357 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
10358 continue;
10359 if (to != from)
10360 memcpy (to, from, PDR_SIZE);
10361 to += PDR_SIZE;
10363 bfd_set_section_contents (output_bfd, sec->output_section, contents,
10364 sec->output_offset, sec->size);
10365 return TRUE;
10368 /* MIPS ELF uses a special find_nearest_line routine in order the
10369 handle the ECOFF debugging information. */
10371 struct mips_elf_find_line
10373 struct ecoff_debug_info d;
10374 struct ecoff_find_line i;
10377 bfd_boolean
10378 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
10379 asymbol **symbols, bfd_vma offset,
10380 const char **filename_ptr,
10381 const char **functionname_ptr,
10382 unsigned int *line_ptr)
10384 asection *msec;
10386 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
10387 filename_ptr, functionname_ptr,
10388 line_ptr))
10389 return TRUE;
10391 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
10392 filename_ptr, functionname_ptr,
10393 line_ptr, ABI_64_P (abfd) ? 8 : 0,
10394 &elf_tdata (abfd)->dwarf2_find_line_info))
10395 return TRUE;
10397 msec = bfd_get_section_by_name (abfd, ".mdebug");
10398 if (msec != NULL)
10400 flagword origflags;
10401 struct mips_elf_find_line *fi;
10402 const struct ecoff_debug_swap * const swap =
10403 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
10405 /* If we are called during a link, mips_elf_final_link may have
10406 cleared the SEC_HAS_CONTENTS field. We force it back on here
10407 if appropriate (which it normally will be). */
10408 origflags = msec->flags;
10409 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
10410 msec->flags |= SEC_HAS_CONTENTS;
10412 fi = elf_tdata (abfd)->find_line_info;
10413 if (fi == NULL)
10415 bfd_size_type external_fdr_size;
10416 char *fraw_src;
10417 char *fraw_end;
10418 struct fdr *fdr_ptr;
10419 bfd_size_type amt = sizeof (struct mips_elf_find_line);
10421 fi = bfd_zalloc (abfd, amt);
10422 if (fi == NULL)
10424 msec->flags = origflags;
10425 return FALSE;
10428 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
10430 msec->flags = origflags;
10431 return FALSE;
10434 /* Swap in the FDR information. */
10435 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
10436 fi->d.fdr = bfd_alloc (abfd, amt);
10437 if (fi->d.fdr == NULL)
10439 msec->flags = origflags;
10440 return FALSE;
10442 external_fdr_size = swap->external_fdr_size;
10443 fdr_ptr = fi->d.fdr;
10444 fraw_src = (char *) fi->d.external_fdr;
10445 fraw_end = (fraw_src
10446 + fi->d.symbolic_header.ifdMax * external_fdr_size);
10447 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
10448 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
10450 elf_tdata (abfd)->find_line_info = fi;
10452 /* Note that we don't bother to ever free this information.
10453 find_nearest_line is either called all the time, as in
10454 objdump -l, so the information should be saved, or it is
10455 rarely called, as in ld error messages, so the memory
10456 wasted is unimportant. Still, it would probably be a
10457 good idea for free_cached_info to throw it away. */
10460 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
10461 &fi->i, filename_ptr, functionname_ptr,
10462 line_ptr))
10464 msec->flags = origflags;
10465 return TRUE;
10468 msec->flags = origflags;
10471 /* Fall back on the generic ELF find_nearest_line routine. */
10473 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
10474 filename_ptr, functionname_ptr,
10475 line_ptr);
10478 bfd_boolean
10479 _bfd_mips_elf_find_inliner_info (bfd *abfd,
10480 const char **filename_ptr,
10481 const char **functionname_ptr,
10482 unsigned int *line_ptr)
10484 bfd_boolean found;
10485 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10486 functionname_ptr, line_ptr,
10487 & elf_tdata (abfd)->dwarf2_find_line_info);
10488 return found;
10492 /* When are writing out the .options or .MIPS.options section,
10493 remember the bytes we are writing out, so that we can install the
10494 GP value in the section_processing routine. */
10496 bfd_boolean
10497 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
10498 const void *location,
10499 file_ptr offset, bfd_size_type count)
10501 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
10503 bfd_byte *c;
10505 if (elf_section_data (section) == NULL)
10507 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
10508 section->used_by_bfd = bfd_zalloc (abfd, amt);
10509 if (elf_section_data (section) == NULL)
10510 return FALSE;
10512 c = mips_elf_section_data (section)->u.tdata;
10513 if (c == NULL)
10515 c = bfd_zalloc (abfd, section->size);
10516 if (c == NULL)
10517 return FALSE;
10518 mips_elf_section_data (section)->u.tdata = c;
10521 memcpy (c + offset, location, count);
10524 return _bfd_elf_set_section_contents (abfd, section, location, offset,
10525 count);
10528 /* This is almost identical to bfd_generic_get_... except that some
10529 MIPS relocations need to be handled specially. Sigh. */
10531 bfd_byte *
10532 _bfd_elf_mips_get_relocated_section_contents
10533 (bfd *abfd,
10534 struct bfd_link_info *link_info,
10535 struct bfd_link_order *link_order,
10536 bfd_byte *data,
10537 bfd_boolean relocatable,
10538 asymbol **symbols)
10540 /* Get enough memory to hold the stuff */
10541 bfd *input_bfd = link_order->u.indirect.section->owner;
10542 asection *input_section = link_order->u.indirect.section;
10543 bfd_size_type sz;
10545 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
10546 arelent **reloc_vector = NULL;
10547 long reloc_count;
10549 if (reloc_size < 0)
10550 goto error_return;
10552 reloc_vector = bfd_malloc (reloc_size);
10553 if (reloc_vector == NULL && reloc_size != 0)
10554 goto error_return;
10556 /* read in the section */
10557 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
10558 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
10559 goto error_return;
10561 reloc_count = bfd_canonicalize_reloc (input_bfd,
10562 input_section,
10563 reloc_vector,
10564 symbols);
10565 if (reloc_count < 0)
10566 goto error_return;
10568 if (reloc_count > 0)
10570 arelent **parent;
10571 /* for mips */
10572 int gp_found;
10573 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
10576 struct bfd_hash_entry *h;
10577 struct bfd_link_hash_entry *lh;
10578 /* Skip all this stuff if we aren't mixing formats. */
10579 if (abfd && input_bfd
10580 && abfd->xvec == input_bfd->xvec)
10581 lh = 0;
10582 else
10584 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
10585 lh = (struct bfd_link_hash_entry *) h;
10587 lookup:
10588 if (lh)
10590 switch (lh->type)
10592 case bfd_link_hash_undefined:
10593 case bfd_link_hash_undefweak:
10594 case bfd_link_hash_common:
10595 gp_found = 0;
10596 break;
10597 case bfd_link_hash_defined:
10598 case bfd_link_hash_defweak:
10599 gp_found = 1;
10600 gp = lh->u.def.value;
10601 break;
10602 case bfd_link_hash_indirect:
10603 case bfd_link_hash_warning:
10604 lh = lh->u.i.link;
10605 /* @@FIXME ignoring warning for now */
10606 goto lookup;
10607 case bfd_link_hash_new:
10608 default:
10609 abort ();
10612 else
10613 gp_found = 0;
10615 /* end mips */
10616 for (parent = reloc_vector; *parent != NULL; parent++)
10618 char *error_message = NULL;
10619 bfd_reloc_status_type r;
10621 /* Specific to MIPS: Deal with relocation types that require
10622 knowing the gp of the output bfd. */
10623 asymbol *sym = *(*parent)->sym_ptr_ptr;
10625 /* If we've managed to find the gp and have a special
10626 function for the relocation then go ahead, else default
10627 to the generic handling. */
10628 if (gp_found
10629 && (*parent)->howto->special_function
10630 == _bfd_mips_elf32_gprel16_reloc)
10631 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
10632 input_section, relocatable,
10633 data, gp);
10634 else
10635 r = bfd_perform_relocation (input_bfd, *parent, data,
10636 input_section,
10637 relocatable ? abfd : NULL,
10638 &error_message);
10640 if (relocatable)
10642 asection *os = input_section->output_section;
10644 /* A partial link, so keep the relocs */
10645 os->orelocation[os->reloc_count] = *parent;
10646 os->reloc_count++;
10649 if (r != bfd_reloc_ok)
10651 switch (r)
10653 case bfd_reloc_undefined:
10654 if (!((*link_info->callbacks->undefined_symbol)
10655 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
10656 input_bfd, input_section, (*parent)->address, TRUE)))
10657 goto error_return;
10658 break;
10659 case bfd_reloc_dangerous:
10660 BFD_ASSERT (error_message != NULL);
10661 if (!((*link_info->callbacks->reloc_dangerous)
10662 (link_info, error_message, input_bfd, input_section,
10663 (*parent)->address)))
10664 goto error_return;
10665 break;
10666 case bfd_reloc_overflow:
10667 if (!((*link_info->callbacks->reloc_overflow)
10668 (link_info, NULL,
10669 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
10670 (*parent)->howto->name, (*parent)->addend,
10671 input_bfd, input_section, (*parent)->address)))
10672 goto error_return;
10673 break;
10674 case bfd_reloc_outofrange:
10675 default:
10676 abort ();
10677 break;
10683 if (reloc_vector != NULL)
10684 free (reloc_vector);
10685 return data;
10687 error_return:
10688 if (reloc_vector != NULL)
10689 free (reloc_vector);
10690 return NULL;
10693 /* Create a MIPS ELF linker hash table. */
10695 struct bfd_link_hash_table *
10696 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
10698 struct mips_elf_link_hash_table *ret;
10699 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
10701 ret = bfd_malloc (amt);
10702 if (ret == NULL)
10703 return NULL;
10705 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
10706 mips_elf_link_hash_newfunc,
10707 sizeof (struct mips_elf_link_hash_entry)))
10709 free (ret);
10710 return NULL;
10713 #if 0
10714 /* We no longer use this. */
10715 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
10716 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
10717 #endif
10718 ret->procedure_count = 0;
10719 ret->compact_rel_size = 0;
10720 ret->use_rld_obj_head = FALSE;
10721 ret->rld_value = 0;
10722 ret->mips16_stubs_seen = FALSE;
10723 ret->is_vxworks = FALSE;
10724 ret->small_data_overflow_reported = FALSE;
10725 ret->srelbss = NULL;
10726 ret->sdynbss = NULL;
10727 ret->srelplt = NULL;
10728 ret->srelplt2 = NULL;
10729 ret->sgotplt = NULL;
10730 ret->splt = NULL;
10731 ret->sstubs = NULL;
10732 ret->sgot = NULL;
10733 ret->got_info = NULL;
10734 ret->plt_header_size = 0;
10735 ret->plt_entry_size = 0;
10736 ret->lazy_stub_count = 0;
10737 ret->function_stub_size = 0;
10739 return &ret->root.root;
10742 /* Likewise, but indicate that the target is VxWorks. */
10744 struct bfd_link_hash_table *
10745 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
10747 struct bfd_link_hash_table *ret;
10749 ret = _bfd_mips_elf_link_hash_table_create (abfd);
10750 if (ret)
10752 struct mips_elf_link_hash_table *htab;
10754 htab = (struct mips_elf_link_hash_table *) ret;
10755 htab->is_vxworks = 1;
10757 return ret;
10760 /* We need to use a special link routine to handle the .reginfo and
10761 the .mdebug sections. We need to merge all instances of these
10762 sections together, not write them all out sequentially. */
10764 bfd_boolean
10765 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10767 asection *o;
10768 struct bfd_link_order *p;
10769 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
10770 asection *rtproc_sec;
10771 Elf32_RegInfo reginfo;
10772 struct ecoff_debug_info debug;
10773 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10774 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
10775 HDRR *symhdr = &debug.symbolic_header;
10776 void *mdebug_handle = NULL;
10777 asection *s;
10778 EXTR esym;
10779 unsigned int i;
10780 bfd_size_type amt;
10781 struct mips_elf_link_hash_table *htab;
10783 static const char * const secname[] =
10785 ".text", ".init", ".fini", ".data",
10786 ".rodata", ".sdata", ".sbss", ".bss"
10788 static const int sc[] =
10790 scText, scInit, scFini, scData,
10791 scRData, scSData, scSBss, scBss
10794 /* We'd carefully arranged the dynamic symbol indices, and then the
10795 generic size_dynamic_sections renumbered them out from under us.
10796 Rather than trying somehow to prevent the renumbering, just do
10797 the sort again. */
10798 htab = mips_elf_hash_table (info);
10799 if (elf_hash_table (info)->dynamic_sections_created)
10801 struct mips_got_info *g;
10802 bfd_size_type dynsecsymcount;
10804 /* When we resort, we must tell mips_elf_sort_hash_table what
10805 the lowest index it may use is. That's the number of section
10806 symbols we're going to add. The generic ELF linker only
10807 adds these symbols when building a shared object. Note that
10808 we count the sections after (possibly) removing the .options
10809 section above. */
10811 dynsecsymcount = count_section_dynsyms (abfd, info);
10812 if (! mips_elf_sort_hash_table (info, dynsecsymcount + 1))
10813 return FALSE;
10815 /* Make sure we didn't grow the global .got region. */
10816 g = htab->got_info;
10817 if (g->global_gotsym != NULL)
10818 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
10819 - g->global_gotsym->dynindx)
10820 <= g->global_gotno);
10823 /* Get a value for the GP register. */
10824 if (elf_gp (abfd) == 0)
10826 struct bfd_link_hash_entry *h;
10828 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
10829 if (h != NULL && h->type == bfd_link_hash_defined)
10830 elf_gp (abfd) = (h->u.def.value
10831 + h->u.def.section->output_section->vma
10832 + h->u.def.section->output_offset);
10833 else if (htab->is_vxworks
10834 && (h = bfd_link_hash_lookup (info->hash,
10835 "_GLOBAL_OFFSET_TABLE_",
10836 FALSE, FALSE, TRUE))
10837 && h->type == bfd_link_hash_defined)
10838 elf_gp (abfd) = (h->u.def.section->output_section->vma
10839 + h->u.def.section->output_offset
10840 + h->u.def.value);
10841 else if (info->relocatable)
10843 bfd_vma lo = MINUS_ONE;
10845 /* Find the GP-relative section with the lowest offset. */
10846 for (o = abfd->sections; o != NULL; o = o->next)
10847 if (o->vma < lo
10848 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
10849 lo = o->vma;
10851 /* And calculate GP relative to that. */
10852 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
10854 else
10856 /* If the relocate_section function needs to do a reloc
10857 involving the GP value, it should make a reloc_dangerous
10858 callback to warn that GP is not defined. */
10862 /* Go through the sections and collect the .reginfo and .mdebug
10863 information. */
10864 reginfo_sec = NULL;
10865 mdebug_sec = NULL;
10866 gptab_data_sec = NULL;
10867 gptab_bss_sec = NULL;
10868 for (o = abfd->sections; o != NULL; o = o->next)
10870 if (strcmp (o->name, ".reginfo") == 0)
10872 memset (&reginfo, 0, sizeof reginfo);
10874 /* We have found the .reginfo section in the output file.
10875 Look through all the link_orders comprising it and merge
10876 the information together. */
10877 for (p = o->map_head.link_order; p != NULL; p = p->next)
10879 asection *input_section;
10880 bfd *input_bfd;
10881 Elf32_External_RegInfo ext;
10882 Elf32_RegInfo sub;
10884 if (p->type != bfd_indirect_link_order)
10886 if (p->type == bfd_data_link_order)
10887 continue;
10888 abort ();
10891 input_section = p->u.indirect.section;
10892 input_bfd = input_section->owner;
10894 if (! bfd_get_section_contents (input_bfd, input_section,
10895 &ext, 0, sizeof ext))
10896 return FALSE;
10898 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
10900 reginfo.ri_gprmask |= sub.ri_gprmask;
10901 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
10902 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
10903 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
10904 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
10906 /* ri_gp_value is set by the function
10907 mips_elf32_section_processing when the section is
10908 finally written out. */
10910 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10911 elf_link_input_bfd ignores this section. */
10912 input_section->flags &= ~SEC_HAS_CONTENTS;
10915 /* Size has been set in _bfd_mips_elf_always_size_sections. */
10916 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
10918 /* Skip this section later on (I don't think this currently
10919 matters, but someday it might). */
10920 o->map_head.link_order = NULL;
10922 reginfo_sec = o;
10925 if (strcmp (o->name, ".mdebug") == 0)
10927 struct extsym_info einfo;
10928 bfd_vma last;
10930 /* We have found the .mdebug section in the output file.
10931 Look through all the link_orders comprising it and merge
10932 the information together. */
10933 symhdr->magic = swap->sym_magic;
10934 /* FIXME: What should the version stamp be? */
10935 symhdr->vstamp = 0;
10936 symhdr->ilineMax = 0;
10937 symhdr->cbLine = 0;
10938 symhdr->idnMax = 0;
10939 symhdr->ipdMax = 0;
10940 symhdr->isymMax = 0;
10941 symhdr->ioptMax = 0;
10942 symhdr->iauxMax = 0;
10943 symhdr->issMax = 0;
10944 symhdr->issExtMax = 0;
10945 symhdr->ifdMax = 0;
10946 symhdr->crfd = 0;
10947 symhdr->iextMax = 0;
10949 /* We accumulate the debugging information itself in the
10950 debug_info structure. */
10951 debug.line = NULL;
10952 debug.external_dnr = NULL;
10953 debug.external_pdr = NULL;
10954 debug.external_sym = NULL;
10955 debug.external_opt = NULL;
10956 debug.external_aux = NULL;
10957 debug.ss = NULL;
10958 debug.ssext = debug.ssext_end = NULL;
10959 debug.external_fdr = NULL;
10960 debug.external_rfd = NULL;
10961 debug.external_ext = debug.external_ext_end = NULL;
10963 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
10964 if (mdebug_handle == NULL)
10965 return FALSE;
10967 esym.jmptbl = 0;
10968 esym.cobol_main = 0;
10969 esym.weakext = 0;
10970 esym.reserved = 0;
10971 esym.ifd = ifdNil;
10972 esym.asym.iss = issNil;
10973 esym.asym.st = stLocal;
10974 esym.asym.reserved = 0;
10975 esym.asym.index = indexNil;
10976 last = 0;
10977 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
10979 esym.asym.sc = sc[i];
10980 s = bfd_get_section_by_name (abfd, secname[i]);
10981 if (s != NULL)
10983 esym.asym.value = s->vma;
10984 last = s->vma + s->size;
10986 else
10987 esym.asym.value = last;
10988 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
10989 secname[i], &esym))
10990 return FALSE;
10993 for (p = o->map_head.link_order; p != NULL; p = p->next)
10995 asection *input_section;
10996 bfd *input_bfd;
10997 const struct ecoff_debug_swap *input_swap;
10998 struct ecoff_debug_info input_debug;
10999 char *eraw_src;
11000 char *eraw_end;
11002 if (p->type != bfd_indirect_link_order)
11004 if (p->type == bfd_data_link_order)
11005 continue;
11006 abort ();
11009 input_section = p->u.indirect.section;
11010 input_bfd = input_section->owner;
11012 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
11013 || (get_elf_backend_data (input_bfd)
11014 ->elf_backend_ecoff_debug_swap) == NULL)
11016 /* I don't know what a non MIPS ELF bfd would be
11017 doing with a .mdebug section, but I don't really
11018 want to deal with it. */
11019 continue;
11022 input_swap = (get_elf_backend_data (input_bfd)
11023 ->elf_backend_ecoff_debug_swap);
11025 BFD_ASSERT (p->size == input_section->size);
11027 /* The ECOFF linking code expects that we have already
11028 read in the debugging information and set up an
11029 ecoff_debug_info structure, so we do that now. */
11030 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11031 &input_debug))
11032 return FALSE;
11034 if (! (bfd_ecoff_debug_accumulate
11035 (mdebug_handle, abfd, &debug, swap, input_bfd,
11036 &input_debug, input_swap, info)))
11037 return FALSE;
11039 /* Loop through the external symbols. For each one with
11040 interesting information, try to find the symbol in
11041 the linker global hash table and save the information
11042 for the output external symbols. */
11043 eraw_src = input_debug.external_ext;
11044 eraw_end = (eraw_src
11045 + (input_debug.symbolic_header.iextMax
11046 * input_swap->external_ext_size));
11047 for (;
11048 eraw_src < eraw_end;
11049 eraw_src += input_swap->external_ext_size)
11051 EXTR ext;
11052 const char *name;
11053 struct mips_elf_link_hash_entry *h;
11055 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
11056 if (ext.asym.sc == scNil
11057 || ext.asym.sc == scUndefined
11058 || ext.asym.sc == scSUndefined)
11059 continue;
11061 name = input_debug.ssext + ext.asym.iss;
11062 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
11063 name, FALSE, FALSE, TRUE);
11064 if (h == NULL || h->esym.ifd != -2)
11065 continue;
11067 if (ext.ifd != -1)
11069 BFD_ASSERT (ext.ifd
11070 < input_debug.symbolic_header.ifdMax);
11071 ext.ifd = input_debug.ifdmap[ext.ifd];
11074 h->esym = ext;
11077 /* Free up the information we just read. */
11078 free (input_debug.line);
11079 free (input_debug.external_dnr);
11080 free (input_debug.external_pdr);
11081 free (input_debug.external_sym);
11082 free (input_debug.external_opt);
11083 free (input_debug.external_aux);
11084 free (input_debug.ss);
11085 free (input_debug.ssext);
11086 free (input_debug.external_fdr);
11087 free (input_debug.external_rfd);
11088 free (input_debug.external_ext);
11090 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11091 elf_link_input_bfd ignores this section. */
11092 input_section->flags &= ~SEC_HAS_CONTENTS;
11095 if (SGI_COMPAT (abfd) && info->shared)
11097 /* Create .rtproc section. */
11098 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11099 if (rtproc_sec == NULL)
11101 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11102 | SEC_LINKER_CREATED | SEC_READONLY);
11104 rtproc_sec = bfd_make_section_with_flags (abfd,
11105 ".rtproc",
11106 flags);
11107 if (rtproc_sec == NULL
11108 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11109 return FALSE;
11112 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11113 info, rtproc_sec,
11114 &debug))
11115 return FALSE;
11118 /* Build the external symbol information. */
11119 einfo.abfd = abfd;
11120 einfo.info = info;
11121 einfo.debug = &debug;
11122 einfo.swap = swap;
11123 einfo.failed = FALSE;
11124 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11125 mips_elf_output_extsym, &einfo);
11126 if (einfo.failed)
11127 return FALSE;
11129 /* Set the size of the .mdebug section. */
11130 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11132 /* Skip this section later on (I don't think this currently
11133 matters, but someday it might). */
11134 o->map_head.link_order = NULL;
11136 mdebug_sec = o;
11139 if (CONST_STRNEQ (o->name, ".gptab."))
11141 const char *subname;
11142 unsigned int c;
11143 Elf32_gptab *tab;
11144 Elf32_External_gptab *ext_tab;
11145 unsigned int j;
11147 /* The .gptab.sdata and .gptab.sbss sections hold
11148 information describing how the small data area would
11149 change depending upon the -G switch. These sections
11150 not used in executables files. */
11151 if (! info->relocatable)
11153 for (p = o->map_head.link_order; p != NULL; p = p->next)
11155 asection *input_section;
11157 if (p->type != bfd_indirect_link_order)
11159 if (p->type == bfd_data_link_order)
11160 continue;
11161 abort ();
11164 input_section = p->u.indirect.section;
11166 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11167 elf_link_input_bfd ignores this section. */
11168 input_section->flags &= ~SEC_HAS_CONTENTS;
11171 /* Skip this section later on (I don't think this
11172 currently matters, but someday it might). */
11173 o->map_head.link_order = NULL;
11175 /* Really remove the section. */
11176 bfd_section_list_remove (abfd, o);
11177 --abfd->section_count;
11179 continue;
11182 /* There is one gptab for initialized data, and one for
11183 uninitialized data. */
11184 if (strcmp (o->name, ".gptab.sdata") == 0)
11185 gptab_data_sec = o;
11186 else if (strcmp (o->name, ".gptab.sbss") == 0)
11187 gptab_bss_sec = o;
11188 else
11190 (*_bfd_error_handler)
11191 (_("%s: illegal section name `%s'"),
11192 bfd_get_filename (abfd), o->name);
11193 bfd_set_error (bfd_error_nonrepresentable_section);
11194 return FALSE;
11197 /* The linker script always combines .gptab.data and
11198 .gptab.sdata into .gptab.sdata, and likewise for
11199 .gptab.bss and .gptab.sbss. It is possible that there is
11200 no .sdata or .sbss section in the output file, in which
11201 case we must change the name of the output section. */
11202 subname = o->name + sizeof ".gptab" - 1;
11203 if (bfd_get_section_by_name (abfd, subname) == NULL)
11205 if (o == gptab_data_sec)
11206 o->name = ".gptab.data";
11207 else
11208 o->name = ".gptab.bss";
11209 subname = o->name + sizeof ".gptab" - 1;
11210 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
11213 /* Set up the first entry. */
11214 c = 1;
11215 amt = c * sizeof (Elf32_gptab);
11216 tab = bfd_malloc (amt);
11217 if (tab == NULL)
11218 return FALSE;
11219 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
11220 tab[0].gt_header.gt_unused = 0;
11222 /* Combine the input sections. */
11223 for (p = o->map_head.link_order; p != NULL; p = p->next)
11225 asection *input_section;
11226 bfd *input_bfd;
11227 bfd_size_type size;
11228 unsigned long last;
11229 bfd_size_type gpentry;
11231 if (p->type != bfd_indirect_link_order)
11233 if (p->type == bfd_data_link_order)
11234 continue;
11235 abort ();
11238 input_section = p->u.indirect.section;
11239 input_bfd = input_section->owner;
11241 /* Combine the gptab entries for this input section one
11242 by one. We know that the input gptab entries are
11243 sorted by ascending -G value. */
11244 size = input_section->size;
11245 last = 0;
11246 for (gpentry = sizeof (Elf32_External_gptab);
11247 gpentry < size;
11248 gpentry += sizeof (Elf32_External_gptab))
11250 Elf32_External_gptab ext_gptab;
11251 Elf32_gptab int_gptab;
11252 unsigned long val;
11253 unsigned long add;
11254 bfd_boolean exact;
11255 unsigned int look;
11257 if (! (bfd_get_section_contents
11258 (input_bfd, input_section, &ext_gptab, gpentry,
11259 sizeof (Elf32_External_gptab))))
11261 free (tab);
11262 return FALSE;
11265 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
11266 &int_gptab);
11267 val = int_gptab.gt_entry.gt_g_value;
11268 add = int_gptab.gt_entry.gt_bytes - last;
11270 exact = FALSE;
11271 for (look = 1; look < c; look++)
11273 if (tab[look].gt_entry.gt_g_value >= val)
11274 tab[look].gt_entry.gt_bytes += add;
11276 if (tab[look].gt_entry.gt_g_value == val)
11277 exact = TRUE;
11280 if (! exact)
11282 Elf32_gptab *new_tab;
11283 unsigned int max;
11285 /* We need a new table entry. */
11286 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
11287 new_tab = bfd_realloc (tab, amt);
11288 if (new_tab == NULL)
11290 free (tab);
11291 return FALSE;
11293 tab = new_tab;
11294 tab[c].gt_entry.gt_g_value = val;
11295 tab[c].gt_entry.gt_bytes = add;
11297 /* Merge in the size for the next smallest -G
11298 value, since that will be implied by this new
11299 value. */
11300 max = 0;
11301 for (look = 1; look < c; look++)
11303 if (tab[look].gt_entry.gt_g_value < val
11304 && (max == 0
11305 || (tab[look].gt_entry.gt_g_value
11306 > tab[max].gt_entry.gt_g_value)))
11307 max = look;
11309 if (max != 0)
11310 tab[c].gt_entry.gt_bytes +=
11311 tab[max].gt_entry.gt_bytes;
11313 ++c;
11316 last = int_gptab.gt_entry.gt_bytes;
11319 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11320 elf_link_input_bfd ignores this section. */
11321 input_section->flags &= ~SEC_HAS_CONTENTS;
11324 /* The table must be sorted by -G value. */
11325 if (c > 2)
11326 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
11328 /* Swap out the table. */
11329 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
11330 ext_tab = bfd_alloc (abfd, amt);
11331 if (ext_tab == NULL)
11333 free (tab);
11334 return FALSE;
11337 for (j = 0; j < c; j++)
11338 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
11339 free (tab);
11341 o->size = c * sizeof (Elf32_External_gptab);
11342 o->contents = (bfd_byte *) ext_tab;
11344 /* Skip this section later on (I don't think this currently
11345 matters, but someday it might). */
11346 o->map_head.link_order = NULL;
11350 /* Invoke the regular ELF backend linker to do all the work. */
11351 if (!bfd_elf_final_link (abfd, info))
11352 return FALSE;
11354 /* Now write out the computed sections. */
11356 if (reginfo_sec != NULL)
11358 Elf32_External_RegInfo ext;
11360 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
11361 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
11362 return FALSE;
11365 if (mdebug_sec != NULL)
11367 BFD_ASSERT (abfd->output_has_begun);
11368 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
11369 swap, info,
11370 mdebug_sec->filepos))
11371 return FALSE;
11373 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
11376 if (gptab_data_sec != NULL)
11378 if (! bfd_set_section_contents (abfd, gptab_data_sec,
11379 gptab_data_sec->contents,
11380 0, gptab_data_sec->size))
11381 return FALSE;
11384 if (gptab_bss_sec != NULL)
11386 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
11387 gptab_bss_sec->contents,
11388 0, gptab_bss_sec->size))
11389 return FALSE;
11392 if (SGI_COMPAT (abfd))
11394 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11395 if (rtproc_sec != NULL)
11397 if (! bfd_set_section_contents (abfd, rtproc_sec,
11398 rtproc_sec->contents,
11399 0, rtproc_sec->size))
11400 return FALSE;
11404 return TRUE;
11407 /* Structure for saying that BFD machine EXTENSION extends BASE. */
11409 struct mips_mach_extension {
11410 unsigned long extension, base;
11414 /* An array describing how BFD machines relate to one another. The entries
11415 are ordered topologically with MIPS I extensions listed last. */
11417 static const struct mips_mach_extension mips_mach_extensions[] = {
11418 /* MIPS64r2 extensions. */
11419 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
11421 /* MIPS64 extensions. */
11422 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
11423 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
11425 /* MIPS V extensions. */
11426 { bfd_mach_mipsisa64, bfd_mach_mips5 },
11428 /* R10000 extensions. */
11429 { bfd_mach_mips12000, bfd_mach_mips10000 },
11431 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
11432 vr5400 ISA, but doesn't include the multimedia stuff. It seems
11433 better to allow vr5400 and vr5500 code to be merged anyway, since
11434 many libraries will just use the core ISA. Perhaps we could add
11435 some sort of ASE flag if this ever proves a problem. */
11436 { bfd_mach_mips5500, bfd_mach_mips5400 },
11437 { bfd_mach_mips5400, bfd_mach_mips5000 },
11439 /* MIPS IV extensions. */
11440 { bfd_mach_mips5, bfd_mach_mips8000 },
11441 { bfd_mach_mips10000, bfd_mach_mips8000 },
11442 { bfd_mach_mips5000, bfd_mach_mips8000 },
11443 { bfd_mach_mips7000, bfd_mach_mips8000 },
11444 { bfd_mach_mips9000, bfd_mach_mips8000 },
11446 /* VR4100 extensions. */
11447 { bfd_mach_mips4120, bfd_mach_mips4100 },
11448 { bfd_mach_mips4111, bfd_mach_mips4100 },
11450 /* MIPS III extensions. */
11451 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
11452 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
11453 { bfd_mach_mips8000, bfd_mach_mips4000 },
11454 { bfd_mach_mips4650, bfd_mach_mips4000 },
11455 { bfd_mach_mips4600, bfd_mach_mips4000 },
11456 { bfd_mach_mips4400, bfd_mach_mips4000 },
11457 { bfd_mach_mips4300, bfd_mach_mips4000 },
11458 { bfd_mach_mips4100, bfd_mach_mips4000 },
11459 { bfd_mach_mips4010, bfd_mach_mips4000 },
11461 /* MIPS32 extensions. */
11462 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
11464 /* MIPS II extensions. */
11465 { bfd_mach_mips4000, bfd_mach_mips6000 },
11466 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
11468 /* MIPS I extensions. */
11469 { bfd_mach_mips6000, bfd_mach_mips3000 },
11470 { bfd_mach_mips3900, bfd_mach_mips3000 }
11474 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
11476 static bfd_boolean
11477 mips_mach_extends_p (unsigned long base, unsigned long extension)
11479 size_t i;
11481 if (extension == base)
11482 return TRUE;
11484 if (base == bfd_mach_mipsisa32
11485 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
11486 return TRUE;
11488 if (base == bfd_mach_mipsisa32r2
11489 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
11490 return TRUE;
11492 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
11493 if (extension == mips_mach_extensions[i].extension)
11495 extension = mips_mach_extensions[i].base;
11496 if (extension == base)
11497 return TRUE;
11500 return FALSE;
11504 /* Return true if the given ELF header flags describe a 32-bit binary. */
11506 static bfd_boolean
11507 mips_32bit_flags_p (flagword flags)
11509 return ((flags & EF_MIPS_32BITMODE) != 0
11510 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
11511 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
11512 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
11513 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
11514 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
11515 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
11519 /* Merge object attributes from IBFD into OBFD. Raise an error if
11520 there are conflicting attributes. */
11521 static bfd_boolean
11522 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
11524 obj_attribute *in_attr;
11525 obj_attribute *out_attr;
11527 if (!elf_known_obj_attributes_proc (obfd)[0].i)
11529 /* This is the first object. Copy the attributes. */
11530 _bfd_elf_copy_obj_attributes (ibfd, obfd);
11532 /* Use the Tag_null value to indicate the attributes have been
11533 initialized. */
11534 elf_known_obj_attributes_proc (obfd)[0].i = 1;
11536 return TRUE;
11539 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
11540 non-conflicting ones. */
11541 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
11542 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
11543 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
11545 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
11546 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
11547 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
11548 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
11550 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
11551 _bfd_error_handler
11552 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
11553 in_attr[Tag_GNU_MIPS_ABI_FP].i);
11554 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
11555 _bfd_error_handler
11556 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
11557 out_attr[Tag_GNU_MIPS_ABI_FP].i);
11558 else
11559 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
11561 case 1:
11562 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11564 case 2:
11565 _bfd_error_handler
11566 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
11567 obfd, ibfd);
11568 break;
11570 case 3:
11571 _bfd_error_handler
11572 (_("Warning: %B uses hard float, %B uses soft float"),
11573 obfd, ibfd);
11574 break;
11576 case 4:
11577 _bfd_error_handler
11578 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
11579 obfd, ibfd);
11580 break;
11582 default:
11583 abort ();
11585 break;
11587 case 2:
11588 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11590 case 1:
11591 _bfd_error_handler
11592 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
11593 ibfd, obfd);
11594 break;
11596 case 3:
11597 _bfd_error_handler
11598 (_("Warning: %B uses hard float, %B uses soft float"),
11599 obfd, ibfd);
11600 break;
11602 case 4:
11603 _bfd_error_handler
11604 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
11605 obfd, ibfd);
11606 break;
11608 default:
11609 abort ();
11611 break;
11613 case 3:
11614 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11616 case 1:
11617 case 2:
11618 case 4:
11619 _bfd_error_handler
11620 (_("Warning: %B uses hard float, %B uses soft float"),
11621 ibfd, obfd);
11622 break;
11624 default:
11625 abort ();
11627 break;
11629 case 4:
11630 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
11632 case 1:
11633 _bfd_error_handler
11634 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
11635 ibfd, obfd);
11636 break;
11638 case 2:
11639 _bfd_error_handler
11640 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
11641 ibfd, obfd);
11642 break;
11644 case 3:
11645 _bfd_error_handler
11646 (_("Warning: %B uses hard float, %B uses soft float"),
11647 obfd, ibfd);
11648 break;
11650 default:
11651 abort ();
11653 break;
11655 default:
11656 abort ();
11660 /* Merge Tag_compatibility attributes and any common GNU ones. */
11661 _bfd_elf_merge_object_attributes (ibfd, obfd);
11663 return TRUE;
11666 /* Merge backend specific data from an object file to the output
11667 object file when linking. */
11669 bfd_boolean
11670 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
11672 flagword old_flags;
11673 flagword new_flags;
11674 bfd_boolean ok;
11675 bfd_boolean null_input_bfd = TRUE;
11676 asection *sec;
11678 /* Check if we have the same endianess */
11679 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
11681 (*_bfd_error_handler)
11682 (_("%B: endianness incompatible with that of the selected emulation"),
11683 ibfd);
11684 return FALSE;
11687 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
11688 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
11689 return TRUE;
11691 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
11693 (*_bfd_error_handler)
11694 (_("%B: ABI is incompatible with that of the selected emulation"),
11695 ibfd);
11696 return FALSE;
11699 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
11700 return FALSE;
11702 new_flags = elf_elfheader (ibfd)->e_flags;
11703 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
11704 old_flags = elf_elfheader (obfd)->e_flags;
11706 if (! elf_flags_init (obfd))
11708 elf_flags_init (obfd) = TRUE;
11709 elf_elfheader (obfd)->e_flags = new_flags;
11710 elf_elfheader (obfd)->e_ident[EI_CLASS]
11711 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
11713 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
11714 && (bfd_get_arch_info (obfd)->the_default
11715 || mips_mach_extends_p (bfd_get_mach (obfd),
11716 bfd_get_mach (ibfd))))
11718 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
11719 bfd_get_mach (ibfd)))
11720 return FALSE;
11723 return TRUE;
11726 /* Check flag compatibility. */
11728 new_flags &= ~EF_MIPS_NOREORDER;
11729 old_flags &= ~EF_MIPS_NOREORDER;
11731 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
11732 doesn't seem to matter. */
11733 new_flags &= ~EF_MIPS_XGOT;
11734 old_flags &= ~EF_MIPS_XGOT;
11736 /* MIPSpro generates ucode info in n64 objects. Again, we should
11737 just be able to ignore this. */
11738 new_flags &= ~EF_MIPS_UCODE;
11739 old_flags &= ~EF_MIPS_UCODE;
11741 /* Don't care about the PIC flags from dynamic objects; they are
11742 PIC by design. */
11743 if ((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0
11744 && (ibfd->flags & DYNAMIC) != 0)
11745 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11747 if (new_flags == old_flags)
11748 return TRUE;
11750 /* Check to see if the input BFD actually contains any sections.
11751 If not, its flags may not have been initialised either, but it cannot
11752 actually cause any incompatibility. */
11753 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
11755 /* Ignore synthetic sections and empty .text, .data and .bss sections
11756 which are automatically generated by gas. */
11757 if (strcmp (sec->name, ".reginfo")
11758 && strcmp (sec->name, ".mdebug")
11759 && (sec->size != 0
11760 || (strcmp (sec->name, ".text")
11761 && strcmp (sec->name, ".data")
11762 && strcmp (sec->name, ".bss"))))
11764 null_input_bfd = FALSE;
11765 break;
11768 if (null_input_bfd)
11769 return TRUE;
11771 ok = TRUE;
11773 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
11774 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
11776 (*_bfd_error_handler)
11777 (_("%B: warning: linking PIC files with non-PIC files"),
11778 ibfd);
11779 ok = TRUE;
11782 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
11783 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
11784 if (! (new_flags & EF_MIPS_PIC))
11785 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
11787 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11788 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
11790 /* Compare the ISAs. */
11791 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
11793 (*_bfd_error_handler)
11794 (_("%B: linking 32-bit code with 64-bit code"),
11795 ibfd);
11796 ok = FALSE;
11798 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
11800 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
11801 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
11803 /* Copy the architecture info from IBFD to OBFD. Also copy
11804 the 32-bit flag (if set) so that we continue to recognise
11805 OBFD as a 32-bit binary. */
11806 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
11807 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11808 elf_elfheader (obfd)->e_flags
11809 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11811 /* Copy across the ABI flags if OBFD doesn't use them
11812 and if that was what caused us to treat IBFD as 32-bit. */
11813 if ((old_flags & EF_MIPS_ABI) == 0
11814 && mips_32bit_flags_p (new_flags)
11815 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
11816 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
11818 else
11820 /* The ISAs aren't compatible. */
11821 (*_bfd_error_handler)
11822 (_("%B: linking %s module with previous %s modules"),
11823 ibfd,
11824 bfd_printable_name (ibfd),
11825 bfd_printable_name (obfd));
11826 ok = FALSE;
11830 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11831 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
11833 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
11834 does set EI_CLASS differently from any 32-bit ABI. */
11835 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
11836 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
11837 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
11839 /* Only error if both are set (to different values). */
11840 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
11841 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
11842 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
11844 (*_bfd_error_handler)
11845 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
11846 ibfd,
11847 elf_mips_abi_name (ibfd),
11848 elf_mips_abi_name (obfd));
11849 ok = FALSE;
11851 new_flags &= ~EF_MIPS_ABI;
11852 old_flags &= ~EF_MIPS_ABI;
11855 /* For now, allow arbitrary mixing of ASEs (retain the union). */
11856 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
11858 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
11860 new_flags &= ~ EF_MIPS_ARCH_ASE;
11861 old_flags &= ~ EF_MIPS_ARCH_ASE;
11864 /* Warn about any other mismatches */
11865 if (new_flags != old_flags)
11867 (*_bfd_error_handler)
11868 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
11869 ibfd, (unsigned long) new_flags,
11870 (unsigned long) old_flags);
11871 ok = FALSE;
11874 if (! ok)
11876 bfd_set_error (bfd_error_bad_value);
11877 return FALSE;
11880 return TRUE;
11883 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
11885 bfd_boolean
11886 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
11888 BFD_ASSERT (!elf_flags_init (abfd)
11889 || elf_elfheader (abfd)->e_flags == flags);
11891 elf_elfheader (abfd)->e_flags = flags;
11892 elf_flags_init (abfd) = TRUE;
11893 return TRUE;
11896 char *
11897 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
11899 switch (dtag)
11901 default: return "";
11902 case DT_MIPS_RLD_VERSION:
11903 return "MIPS_RLD_VERSION";
11904 case DT_MIPS_TIME_STAMP:
11905 return "MIPS_TIME_STAMP";
11906 case DT_MIPS_ICHECKSUM:
11907 return "MIPS_ICHECKSUM";
11908 case DT_MIPS_IVERSION:
11909 return "MIPS_IVERSION";
11910 case DT_MIPS_FLAGS:
11911 return "MIPS_FLAGS";
11912 case DT_MIPS_BASE_ADDRESS:
11913 return "MIPS_BASE_ADDRESS";
11914 case DT_MIPS_MSYM:
11915 return "MIPS_MSYM";
11916 case DT_MIPS_CONFLICT:
11917 return "MIPS_CONFLICT";
11918 case DT_MIPS_LIBLIST:
11919 return "MIPS_LIBLIST";
11920 case DT_MIPS_LOCAL_GOTNO:
11921 return "MIPS_LOCAL_GOTNO";
11922 case DT_MIPS_CONFLICTNO:
11923 return "MIPS_CONFLICTNO";
11924 case DT_MIPS_LIBLISTNO:
11925 return "MIPS_LIBLISTNO";
11926 case DT_MIPS_SYMTABNO:
11927 return "MIPS_SYMTABNO";
11928 case DT_MIPS_UNREFEXTNO:
11929 return "MIPS_UNREFEXTNO";
11930 case DT_MIPS_GOTSYM:
11931 return "MIPS_GOTSYM";
11932 case DT_MIPS_HIPAGENO:
11933 return "MIPS_HIPAGENO";
11934 case DT_MIPS_RLD_MAP:
11935 return "MIPS_RLD_MAP";
11936 case DT_MIPS_DELTA_CLASS:
11937 return "MIPS_DELTA_CLASS";
11938 case DT_MIPS_DELTA_CLASS_NO:
11939 return "MIPS_DELTA_CLASS_NO";
11940 case DT_MIPS_DELTA_INSTANCE:
11941 return "MIPS_DELTA_INSTANCE";
11942 case DT_MIPS_DELTA_INSTANCE_NO:
11943 return "MIPS_DELTA_INSTANCE_NO";
11944 case DT_MIPS_DELTA_RELOC:
11945 return "MIPS_DELTA_RELOC";
11946 case DT_MIPS_DELTA_RELOC_NO:
11947 return "MIPS_DELTA_RELOC_NO";
11948 case DT_MIPS_DELTA_SYM:
11949 return "MIPS_DELTA_SYM";
11950 case DT_MIPS_DELTA_SYM_NO:
11951 return "MIPS_DELTA_SYM_NO";
11952 case DT_MIPS_DELTA_CLASSSYM:
11953 return "MIPS_DELTA_CLASSSYM";
11954 case DT_MIPS_DELTA_CLASSSYM_NO:
11955 return "MIPS_DELTA_CLASSSYM_NO";
11956 case DT_MIPS_CXX_FLAGS:
11957 return "MIPS_CXX_FLAGS";
11958 case DT_MIPS_PIXIE_INIT:
11959 return "MIPS_PIXIE_INIT";
11960 case DT_MIPS_SYMBOL_LIB:
11961 return "MIPS_SYMBOL_LIB";
11962 case DT_MIPS_LOCALPAGE_GOTIDX:
11963 return "MIPS_LOCALPAGE_GOTIDX";
11964 case DT_MIPS_LOCAL_GOTIDX:
11965 return "MIPS_LOCAL_GOTIDX";
11966 case DT_MIPS_HIDDEN_GOTIDX:
11967 return "MIPS_HIDDEN_GOTIDX";
11968 case DT_MIPS_PROTECTED_GOTIDX:
11969 return "MIPS_PROTECTED_GOT_IDX";
11970 case DT_MIPS_OPTIONS:
11971 return "MIPS_OPTIONS";
11972 case DT_MIPS_INTERFACE:
11973 return "MIPS_INTERFACE";
11974 case DT_MIPS_DYNSTR_ALIGN:
11975 return "DT_MIPS_DYNSTR_ALIGN";
11976 case DT_MIPS_INTERFACE_SIZE:
11977 return "DT_MIPS_INTERFACE_SIZE";
11978 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
11979 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
11980 case DT_MIPS_PERF_SUFFIX:
11981 return "DT_MIPS_PERF_SUFFIX";
11982 case DT_MIPS_COMPACT_SIZE:
11983 return "DT_MIPS_COMPACT_SIZE";
11984 case DT_MIPS_GP_VALUE:
11985 return "DT_MIPS_GP_VALUE";
11986 case DT_MIPS_AUX_DYNAMIC:
11987 return "DT_MIPS_AUX_DYNAMIC";
11991 bfd_boolean
11992 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
11994 FILE *file = ptr;
11996 BFD_ASSERT (abfd != NULL && ptr != NULL);
11998 /* Print normal ELF private data. */
11999 _bfd_elf_print_private_bfd_data (abfd, ptr);
12001 /* xgettext:c-format */
12002 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12004 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12005 fprintf (file, _(" [abi=O32]"));
12006 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12007 fprintf (file, _(" [abi=O64]"));
12008 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12009 fprintf (file, _(" [abi=EABI32]"));
12010 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12011 fprintf (file, _(" [abi=EABI64]"));
12012 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12013 fprintf (file, _(" [abi unknown]"));
12014 else if (ABI_N32_P (abfd))
12015 fprintf (file, _(" [abi=N32]"));
12016 else if (ABI_64_P (abfd))
12017 fprintf (file, _(" [abi=64]"));
12018 else
12019 fprintf (file, _(" [no abi set]"));
12021 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
12022 fprintf (file, " [mips1]");
12023 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
12024 fprintf (file, " [mips2]");
12025 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
12026 fprintf (file, " [mips3]");
12027 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
12028 fprintf (file, " [mips4]");
12029 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
12030 fprintf (file, " [mips5]");
12031 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
12032 fprintf (file, " [mips32]");
12033 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
12034 fprintf (file, " [mips64]");
12035 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
12036 fprintf (file, " [mips32r2]");
12037 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
12038 fprintf (file, " [mips64r2]");
12039 else
12040 fprintf (file, _(" [unknown ISA]"));
12042 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
12043 fprintf (file, " [mdmx]");
12045 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
12046 fprintf (file, " [mips16]");
12048 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
12049 fprintf (file, " [32bitmode]");
12050 else
12051 fprintf (file, _(" [not 32bitmode]"));
12053 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
12054 fprintf (file, " [noreorder]");
12056 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
12057 fprintf (file, " [PIC]");
12059 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
12060 fprintf (file, " [CPIC]");
12062 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
12063 fprintf (file, " [XGOT]");
12065 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
12066 fprintf (file, " [UCODE]");
12068 fputc ('\n', file);
12070 return TRUE;
12073 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12075 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12076 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12077 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12078 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12079 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12080 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12081 { NULL, 0, 0, 0, 0 }
12084 /* Merge non visibility st_other attributes. Ensure that the
12085 STO_OPTIONAL flag is copied into h->other, even if this is not a
12086 definiton of the symbol. */
12087 void
12088 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12089 const Elf_Internal_Sym *isym,
12090 bfd_boolean definition,
12091 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12093 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12095 unsigned char other;
12097 other = (definition ? isym->st_other : h->other);
12098 other &= ~ELF_ST_VISIBILITY (-1);
12099 h->other = other | ELF_ST_VISIBILITY (h->other);
12102 if (!definition
12103 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12104 h->other |= STO_OPTIONAL;
12107 /* Decide whether an undefined symbol is special and can be ignored.
12108 This is the case for OPTIONAL symbols on IRIX. */
12109 bfd_boolean
12110 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12112 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12115 bfd_boolean
12116 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12118 return (sym->st_shndx == SHN_COMMON
12119 || sym->st_shndx == SHN_MIPS_ACOMMON
12120 || sym->st_shndx == SHN_MIPS_SCOMMON);