1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
29 different MIPS ELF from other targets. This matters when linking.
30 This file supports both, switching at runtime. */
40 /* Get the ECOFF swapping routines. */
42 #include "coff/symconst.h"
43 #include "coff/internal.h"
44 #include "coff/ecoff.h"
45 #include "coff/mips.h"
46 #define ECOFF_SIGNED_32
47 #include "ecoffswap.h"
49 /* This structure is used to hold .got information when linking. It
50 is stored in the tdata field of the bfd_elf_section_data structure. */
54 /* The global symbol in the GOT with the lowest index in the dynamic
56 struct elf_link_hash_entry *global_gotsym;
57 /* The number of global .got entries. */
58 unsigned int global_gotno;
59 /* The number of local .got entries. */
60 unsigned int local_gotno;
61 /* The number of local .got entries we have used. */
62 unsigned int assigned_gotno;
65 /* The MIPS ELF linker needs additional information for each symbol in
66 the global hash table. */
68 struct mips_elf_link_hash_entry
70 struct elf_link_hash_entry root;
72 /* External symbol information. */
75 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
77 unsigned int possibly_dynamic_relocs;
79 /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
80 a readonly section. */
81 boolean readonly_reloc;
83 /* The index of the first dynamic relocation (in the .rel.dyn
84 section) against this symbol. */
85 unsigned int min_dyn_reloc_index;
87 /* We must not create a stub for a symbol that has relocations
88 related to taking the function's address, i.e. any but
89 R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
93 /* If there is a stub that 32 bit functions should use to call this
94 16 bit function, this points to the section containing the stub. */
97 /* Whether we need the fn_stub; this is set if this symbol appears
98 in any relocs other than a 16 bit call. */
101 /* If there is a stub that 16 bit functions should use to call this
102 32 bit function, this points to the section containing the stub. */
105 /* This is like the call_stub field, but it is used if the function
106 being called returns a floating point value. */
107 asection *call_fp_stub;
110 static bfd_reloc_status_type mips32_64bit_reloc
111 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
112 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
113 PARAMS ((bfd *, bfd_reloc_code_real_type));
114 static reloc_howto_type *mips_rtype_to_howto
115 PARAMS ((unsigned int));
116 static void mips_info_to_howto_rel
117 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
118 static void mips_info_to_howto_rela
119 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
120 static void bfd_mips_elf32_swap_gptab_in
121 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
122 static void bfd_mips_elf32_swap_gptab_out
123 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
125 static void bfd_mips_elf_swap_msym_in
126 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
128 static void bfd_mips_elf_swap_msym_out
129 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
130 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
131 static boolean mips_elf_create_procedure_table
132 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
133 struct ecoff_debug_info *));
134 static INLINE int elf_mips_isa PARAMS ((flagword));
135 static INLINE unsigned long elf_mips_mach PARAMS ((flagword));
136 static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
137 static boolean mips_elf_is_local_label_name
138 PARAMS ((bfd *, const char *));
139 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
140 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
141 static int gptab_compare PARAMS ((const void *, const void *));
142 static bfd_reloc_status_type mips16_jump_reloc
143 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
144 static bfd_reloc_status_type mips16_gprel_reloc
145 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
146 static boolean mips_elf_create_compact_rel_section
147 PARAMS ((bfd *, struct bfd_link_info *));
148 static boolean mips_elf_create_got_section
149 PARAMS ((bfd *, struct bfd_link_info *));
150 static bfd_reloc_status_type mips_elf_final_gp
151 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
152 static bfd_byte *elf32_mips_get_relocated_section_contents
153 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
154 bfd_byte *, boolean, asymbol **));
155 static asection *mips_elf_create_msym_section
157 static void mips_elf_irix6_finish_dynamic_symbol
158 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
159 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
160 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
161 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
162 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
163 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
164 static bfd_vma mips_elf_global_got_index
165 PARAMS ((bfd *, struct elf_link_hash_entry *));
166 static bfd_vma mips_elf_local_got_index
167 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
168 static bfd_vma mips_elf_got_offset_from_index
169 PARAMS ((bfd *, bfd *, bfd_vma));
170 static boolean mips_elf_record_global_got_symbol
171 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
172 struct mips_got_info *));
173 static bfd_vma mips_elf_got_page
174 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
175 static const Elf_Internal_Rela *mips_elf_next_relocation
176 PARAMS ((unsigned int, const Elf_Internal_Rela *,
177 const Elf_Internal_Rela *));
178 static bfd_reloc_status_type mips_elf_calculate_relocation
179 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
180 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
181 Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
183 static bfd_vma mips_elf_obtain_contents
184 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
185 static boolean mips_elf_perform_relocation
186 PARAMS ((struct bfd_link_info *, reloc_howto_type *,
187 const Elf_Internal_Rela *, bfd_vma,
188 bfd *, asection *, bfd_byte *, boolean));
189 static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
190 static boolean mips_elf_sort_hash_table_f
191 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
192 static boolean mips_elf_sort_hash_table
193 PARAMS ((struct bfd_link_info *, unsigned long));
194 static asection * mips_elf_got_section PARAMS ((bfd *));
195 static struct mips_got_info *mips_elf_got_info
196 PARAMS ((bfd *, asection **));
197 static boolean mips_elf_local_relocation_p
198 PARAMS ((bfd *, const Elf_Internal_Rela *, asection **, boolean));
199 static bfd_vma mips_elf_create_local_got_entry
200 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
201 static bfd_vma mips_elf_got16_entry
202 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, boolean));
203 static boolean mips_elf_create_dynamic_relocation
204 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
205 struct mips_elf_link_hash_entry *, asection *,
206 bfd_vma, bfd_vma *, asection *));
207 static void mips_elf_allocate_dynamic_relocations
208 PARAMS ((bfd *, unsigned int));
209 static boolean mips_elf_stub_section_p
210 PARAMS ((bfd *, asection *));
211 static int sort_dynamic_relocs
212 PARAMS ((const void *, const void *));
213 static void _bfd_mips_elf_hide_symbol
214 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
215 static void _bfd_mips_elf_copy_indirect_symbol
216 PARAMS ((struct elf_link_hash_entry *,
217 struct elf_link_hash_entry *));
218 static boolean _bfd_elf32_mips_grok_prstatus
219 PARAMS ((bfd *, Elf_Internal_Note *));
220 static boolean _bfd_elf32_mips_grok_psinfo
221 PARAMS ((bfd *, Elf_Internal_Note *));
223 extern const bfd_target bfd_elf32_tradbigmips_vec;
224 extern const bfd_target bfd_elf32_tradlittlemips_vec;
226 extern const bfd_target bfd_elf64_tradbigmips_vec;
227 extern const bfd_target bfd_elf64_tradlittlemips_vec;
230 /* The level of IRIX compatibility we're striving for. */
238 /* This will be used when we sort the dynamic relocation records. */
239 static bfd *reldyn_sorting_bfd;
241 /* Nonzero if ABFD is using the N32 ABI. */
243 #define ABI_N32_P(abfd) \
244 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
246 /* Nonzero if ABFD is using the 64-bit ABI. */
247 #define ABI_64_P(abfd) \
248 ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0)
250 /* Depending on the target vector we generate some version of Irix
251 executables or "normal" MIPS ELF ABI executables. */
253 #define IRIX_COMPAT(abfd) \
254 (((abfd->xvec == &bfd_elf64_tradbigmips_vec) || \
255 (abfd->xvec == &bfd_elf64_tradlittlemips_vec) || \
256 (abfd->xvec == &bfd_elf32_tradbigmips_vec) || \
257 (abfd->xvec == &bfd_elf32_tradlittlemips_vec)) ? ict_none : \
258 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5))
260 #define IRIX_COMPAT(abfd) \
261 (((abfd->xvec == &bfd_elf32_tradbigmips_vec) || \
262 (abfd->xvec == &bfd_elf32_tradlittlemips_vec)) ? ict_none : \
263 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5))
266 /* Whether we are trying to be compatible with IRIX at all. */
267 #define SGI_COMPAT(abfd) \
268 (IRIX_COMPAT (abfd) != ict_none)
270 /* The name of the msym section. */
271 #define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym"
273 /* The name of the srdata section. */
274 #define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata"
276 /* The name of the options section. */
277 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
278 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options")
280 /* The name of the stub section. */
281 #define MIPS_ELF_STUB_SECTION_NAME(abfd) \
282 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub")
284 /* The name of the dynamic relocation section. */
285 #define MIPS_ELF_REL_DYN_SECTION_NAME(abfd) ".rel.dyn"
287 /* The size of an external REL relocation. */
288 #define MIPS_ELF_REL_SIZE(abfd) \
289 (get_elf_backend_data (abfd)->s->sizeof_rel)
291 /* The size of an external dynamic table entry. */
292 #define MIPS_ELF_DYN_SIZE(abfd) \
293 (get_elf_backend_data (abfd)->s->sizeof_dyn)
295 /* The size of a GOT entry. */
296 #define MIPS_ELF_GOT_SIZE(abfd) \
297 (get_elf_backend_data (abfd)->s->arch_size / 8)
299 /* The size of a symbol-table entry. */
300 #define MIPS_ELF_SYM_SIZE(abfd) \
301 (get_elf_backend_data (abfd)->s->sizeof_sym)
303 /* The default alignment for sections, as a power of two. */
304 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
305 (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2)
307 /* Get word-sized data. */
308 #define MIPS_ELF_GET_WORD(abfd, ptr) \
309 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
311 /* Put out word-sized data. */
312 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
314 ? bfd_put_64 (abfd, val, ptr) \
315 : bfd_put_32 (abfd, val, ptr))
317 /* Add a dynamic symbol table-entry. */
319 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
320 (ABI_64_P (elf_hash_table (info)->dynobj) \
321 ? bfd_elf64_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val) \
322 : bfd_elf32_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val))
324 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
325 (ABI_64_P (elf_hash_table (info)->dynobj) \
326 ? (abort (), false) \
327 : bfd_elf32_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val))
330 /* The number of local .got entries we reserve. */
331 #define MIPS_RESERVED_GOTNO (2)
333 /* Instructions which appear in a stub. For some reason the stub is
334 slightly different on an SGI system. */
335 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
336 #define STUB_LW(abfd) \
339 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
340 : 0x8f998010) /* lw t9,0x8010(gp) */ \
341 : 0x8f998010) /* lw t9,0x8000(gp) */
342 #define STUB_MOVE(abfd) \
343 (SGI_COMPAT (abfd) ? 0x03e07825 : 0x03e07821) /* move t7,ra */
344 #define STUB_JALR 0x0320f809 /* jal t9 */
345 #define STUB_LI16(abfd) \
346 (SGI_COMPAT (abfd) ? 0x34180000 : 0x24180000) /* ori t8,zero,0 */
347 #define MIPS_FUNCTION_STUB_SIZE (16)
350 /* We no longer try to identify particular sections for the .dynsym
351 section. When we do, we wind up crashing if there are other random
352 sections with relocations. */
354 /* Names of sections which appear in the .dynsym section in an Irix 5
357 static const char * const mips_elf_dynsym_sec_names[] =
370 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
371 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
373 /* The number of entries in mips_elf_dynsym_sec_names which go in the
376 #define MIPS_TEXT_DYNSYM_SECNO (3)
380 /* The names of the runtime procedure table symbols used on Irix 5. */
382 static const char * const mips_elf_dynsym_rtproc_names[] =
385 "_procedure_string_table",
386 "_procedure_table_size",
390 /* These structures are used to generate the .compact_rel section on
395 unsigned long id1; /* Always one? */
396 unsigned long num; /* Number of compact relocation entries. */
397 unsigned long id2; /* Always two? */
398 unsigned long offset; /* The file offset of the first relocation. */
399 unsigned long reserved0; /* Zero? */
400 unsigned long reserved1; /* Zero? */
409 bfd_byte reserved0[4];
410 bfd_byte reserved1[4];
411 } Elf32_External_compact_rel;
415 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
416 unsigned int rtype : 4; /* Relocation types. See below. */
417 unsigned int dist2to : 8;
418 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
419 unsigned long konst; /* KONST field. See below. */
420 unsigned long vaddr; /* VADDR to be relocated. */
425 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
426 unsigned int rtype : 4; /* Relocation types. See below. */
427 unsigned int dist2to : 8;
428 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
429 unsigned long konst; /* KONST field. See below. */
437 } Elf32_External_crinfo;
443 } Elf32_External_crinfo2;
445 /* These are the constants used to swap the bitfields in a crinfo. */
447 #define CRINFO_CTYPE (0x1)
448 #define CRINFO_CTYPE_SH (31)
449 #define CRINFO_RTYPE (0xf)
450 #define CRINFO_RTYPE_SH (27)
451 #define CRINFO_DIST2TO (0xff)
452 #define CRINFO_DIST2TO_SH (19)
453 #define CRINFO_RELVADDR (0x7ffff)
454 #define CRINFO_RELVADDR_SH (0)
456 /* A compact relocation info has long (3 words) or short (2 words)
457 formats. A short format doesn't have VADDR field and relvaddr
458 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
459 #define CRF_MIPS_LONG 1
460 #define CRF_MIPS_SHORT 0
462 /* There are 4 types of compact relocation at least. The value KONST
463 has different meaning for each type:
466 CT_MIPS_REL32 Address in data
467 CT_MIPS_WORD Address in word (XXX)
468 CT_MIPS_GPHI_LO GP - vaddr
469 CT_MIPS_JMPAD Address to jump
472 #define CRT_MIPS_REL32 0xa
473 #define CRT_MIPS_WORD 0xb
474 #define CRT_MIPS_GPHI_LO 0xc
475 #define CRT_MIPS_JMPAD 0xd
477 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
478 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
479 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
480 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
482 static void bfd_elf32_swap_compact_rel_out
483 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
484 static void bfd_elf32_swap_crinfo_out
485 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
487 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
489 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
490 from smaller values. Start with zero, widen, *then* decrement. */
491 #define MINUS_ONE (((bfd_vma)0) - 1)
493 static reloc_howto_type elf_mips_howto_table[] =
496 HOWTO (R_MIPS_NONE, /* type */
498 0, /* size (0 = byte, 1 = short, 2 = long) */
500 false, /* pc_relative */
502 complain_overflow_dont, /* complain_on_overflow */
503 bfd_elf_generic_reloc, /* special_function */
504 "R_MIPS_NONE", /* name */
505 false, /* partial_inplace */
508 false), /* pcrel_offset */
510 /* 16 bit relocation. */
511 HOWTO (R_MIPS_16, /* type */
513 1, /* size (0 = byte, 1 = short, 2 = long) */
515 false, /* pc_relative */
517 complain_overflow_bitfield, /* complain_on_overflow */
518 bfd_elf_generic_reloc, /* special_function */
519 "R_MIPS_16", /* name */
520 true, /* partial_inplace */
521 0xffff, /* src_mask */
522 0xffff, /* dst_mask */
523 false), /* pcrel_offset */
525 /* 32 bit relocation. */
526 HOWTO (R_MIPS_32, /* type */
528 2, /* size (0 = byte, 1 = short, 2 = long) */
530 false, /* pc_relative */
532 complain_overflow_bitfield, /* complain_on_overflow */
533 bfd_elf_generic_reloc, /* special_function */
534 "R_MIPS_32", /* name */
535 true, /* partial_inplace */
536 0xffffffff, /* src_mask */
537 0xffffffff, /* dst_mask */
538 false), /* pcrel_offset */
540 /* 32 bit symbol relative relocation. */
541 HOWTO (R_MIPS_REL32, /* type */
543 2, /* size (0 = byte, 1 = short, 2 = long) */
545 false, /* pc_relative */
547 complain_overflow_bitfield, /* complain_on_overflow */
548 bfd_elf_generic_reloc, /* special_function */
549 "R_MIPS_REL32", /* name */
550 true, /* partial_inplace */
551 0xffffffff, /* src_mask */
552 0xffffffff, /* dst_mask */
553 false), /* pcrel_offset */
555 /* 26 bit jump address. */
556 HOWTO (R_MIPS_26, /* type */
558 2, /* size (0 = byte, 1 = short, 2 = long) */
560 false, /* pc_relative */
562 complain_overflow_dont, /* complain_on_overflow */
563 /* This needs complex overflow
564 detection, because the upper four
565 bits must match the PC + 4. */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_MIPS_26", /* name */
568 true, /* partial_inplace */
569 0x3ffffff, /* src_mask */
570 0x3ffffff, /* dst_mask */
571 false), /* pcrel_offset */
573 /* High 16 bits of symbol value. */
574 HOWTO (R_MIPS_HI16, /* type */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
578 false, /* pc_relative */
580 complain_overflow_dont, /* complain_on_overflow */
581 _bfd_mips_elf_hi16_reloc, /* special_function */
582 "R_MIPS_HI16", /* name */
583 true, /* partial_inplace */
584 0xffff, /* src_mask */
585 0xffff, /* dst_mask */
586 false), /* pcrel_offset */
588 /* Low 16 bits of symbol value. */
589 HOWTO (R_MIPS_LO16, /* type */
591 2, /* size (0 = byte, 1 = short, 2 = long) */
593 false, /* pc_relative */
595 complain_overflow_dont, /* complain_on_overflow */
596 _bfd_mips_elf_lo16_reloc, /* special_function */
597 "R_MIPS_LO16", /* name */
598 true, /* partial_inplace */
599 0xffff, /* src_mask */
600 0xffff, /* dst_mask */
601 false), /* pcrel_offset */
603 /* GP relative reference. */
604 HOWTO (R_MIPS_GPREL16, /* type */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
608 false, /* pc_relative */
610 complain_overflow_signed, /* complain_on_overflow */
611 _bfd_mips_elf_gprel16_reloc, /* special_function */
612 "R_MIPS_GPREL16", /* name */
613 true, /* partial_inplace */
614 0xffff, /* src_mask */
615 0xffff, /* dst_mask */
616 false), /* pcrel_offset */
618 /* Reference to literal section. */
619 HOWTO (R_MIPS_LITERAL, /* type */
621 2, /* size (0 = byte, 1 = short, 2 = long) */
623 false, /* pc_relative */
625 complain_overflow_signed, /* complain_on_overflow */
626 _bfd_mips_elf_gprel16_reloc, /* special_function */
627 "R_MIPS_LITERAL", /* name */
628 true, /* partial_inplace */
629 0xffff, /* src_mask */
630 0xffff, /* dst_mask */
631 false), /* pcrel_offset */
633 /* Reference to global offset table. */
634 HOWTO (R_MIPS_GOT16, /* type */
636 2, /* size (0 = byte, 1 = short, 2 = long) */
638 false, /* pc_relative */
640 complain_overflow_signed, /* complain_on_overflow */
641 _bfd_mips_elf_got16_reloc, /* special_function */
642 "R_MIPS_GOT16", /* name */
643 false, /* partial_inplace */
644 0xffff, /* src_mask */
645 0xffff, /* dst_mask */
646 false), /* pcrel_offset */
648 /* 16 bit PC relative reference. */
649 HOWTO (R_MIPS_PC16, /* type */
651 2, /* size (0 = byte, 1 = short, 2 = long) */
653 true, /* pc_relative */
655 complain_overflow_signed, /* complain_on_overflow */
656 bfd_elf_generic_reloc, /* special_function */
657 "R_MIPS_PC16", /* name */
658 true, /* partial_inplace */
659 0xffff, /* src_mask */
660 0xffff, /* dst_mask */
661 true), /* pcrel_offset */
663 /* 16 bit call through global offset table. */
664 HOWTO (R_MIPS_CALL16, /* type */
666 2, /* size (0 = byte, 1 = short, 2 = long) */
668 false, /* pc_relative */
670 complain_overflow_signed, /* complain_on_overflow */
671 bfd_elf_generic_reloc, /* special_function */
672 "R_MIPS_CALL16", /* name */
673 false, /* partial_inplace */
674 0xffff, /* src_mask */
675 0xffff, /* dst_mask */
676 false), /* pcrel_offset */
678 /* 32 bit GP relative reference. */
679 HOWTO (R_MIPS_GPREL32, /* type */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
683 false, /* pc_relative */
685 complain_overflow_bitfield, /* complain_on_overflow */
686 _bfd_mips_elf_gprel32_reloc, /* special_function */
687 "R_MIPS_GPREL32", /* name */
688 true, /* partial_inplace */
689 0xffffffff, /* src_mask */
690 0xffffffff, /* dst_mask */
691 false), /* pcrel_offset */
693 /* The remaining relocs are defined on Irix 5, although they are
694 not defined by the ABI. */
699 /* A 5 bit shift field. */
700 HOWTO (R_MIPS_SHIFT5, /* type */
702 2, /* size (0 = byte, 1 = short, 2 = long) */
704 false, /* pc_relative */
706 complain_overflow_bitfield, /* complain_on_overflow */
707 bfd_elf_generic_reloc, /* special_function */
708 "R_MIPS_SHIFT5", /* name */
709 true, /* partial_inplace */
710 0x000007c0, /* src_mask */
711 0x000007c0, /* dst_mask */
712 false), /* pcrel_offset */
714 /* A 6 bit shift field. */
715 /* FIXME: This is not handled correctly; a special function is
716 needed to put the most significant bit in the right place. */
717 HOWTO (R_MIPS_SHIFT6, /* type */
719 2, /* size (0 = byte, 1 = short, 2 = long) */
721 false, /* pc_relative */
723 complain_overflow_bitfield, /* complain_on_overflow */
724 bfd_elf_generic_reloc, /* special_function */
725 "R_MIPS_SHIFT6", /* name */
726 true, /* partial_inplace */
727 0x000007c4, /* src_mask */
728 0x000007c4, /* dst_mask */
729 false), /* pcrel_offset */
731 /* A 64 bit relocation. */
732 HOWTO (R_MIPS_64, /* type */
734 4, /* size (0 = byte, 1 = short, 2 = long) */
736 false, /* pc_relative */
738 complain_overflow_bitfield, /* complain_on_overflow */
739 mips32_64bit_reloc, /* special_function */
740 "R_MIPS_64", /* name */
741 true, /* partial_inplace */
742 MINUS_ONE, /* src_mask */
743 MINUS_ONE, /* dst_mask */
744 false), /* pcrel_offset */
746 /* Displacement in the global offset table. */
747 HOWTO (R_MIPS_GOT_DISP, /* type */
749 2, /* size (0 = byte, 1 = short, 2 = long) */
751 false, /* pc_relative */
753 complain_overflow_bitfield, /* complain_on_overflow */
754 bfd_elf_generic_reloc, /* special_function */
755 "R_MIPS_GOT_DISP", /* name */
756 true, /* partial_inplace */
757 0x0000ffff, /* src_mask */
758 0x0000ffff, /* dst_mask */
759 false), /* pcrel_offset */
761 /* Displacement to page pointer in the global offset table. */
762 HOWTO (R_MIPS_GOT_PAGE, /* type */
764 2, /* size (0 = byte, 1 = short, 2 = long) */
766 false, /* pc_relative */
768 complain_overflow_bitfield, /* complain_on_overflow */
769 bfd_elf_generic_reloc, /* special_function */
770 "R_MIPS_GOT_PAGE", /* name */
771 true, /* partial_inplace */
772 0x0000ffff, /* src_mask */
773 0x0000ffff, /* dst_mask */
774 false), /* pcrel_offset */
776 /* Offset from page pointer in the global offset table. */
777 HOWTO (R_MIPS_GOT_OFST, /* type */
779 2, /* size (0 = byte, 1 = short, 2 = long) */
781 false, /* pc_relative */
783 complain_overflow_bitfield, /* complain_on_overflow */
784 bfd_elf_generic_reloc, /* special_function */
785 "R_MIPS_GOT_OFST", /* name */
786 true, /* partial_inplace */
787 0x0000ffff, /* src_mask */
788 0x0000ffff, /* dst_mask */
789 false), /* pcrel_offset */
791 /* High 16 bits of displacement in global offset table. */
792 HOWTO (R_MIPS_GOT_HI16, /* type */
794 2, /* size (0 = byte, 1 = short, 2 = long) */
796 false, /* pc_relative */
798 complain_overflow_dont, /* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_MIPS_GOT_HI16", /* name */
801 true, /* partial_inplace */
802 0x0000ffff, /* src_mask */
803 0x0000ffff, /* dst_mask */
804 false), /* pcrel_offset */
806 /* Low 16 bits of displacement in global offset table. */
807 HOWTO (R_MIPS_GOT_LO16, /* type */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
811 false, /* pc_relative */
813 complain_overflow_dont, /* complain_on_overflow */
814 bfd_elf_generic_reloc, /* special_function */
815 "R_MIPS_GOT_LO16", /* name */
816 true, /* partial_inplace */
817 0x0000ffff, /* src_mask */
818 0x0000ffff, /* dst_mask */
819 false), /* pcrel_offset */
821 /* 64 bit subtraction. Used in the N32 ABI. */
822 HOWTO (R_MIPS_SUB, /* type */
824 4, /* size (0 = byte, 1 = short, 2 = long) */
826 false, /* pc_relative */
828 complain_overflow_bitfield, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_MIPS_SUB", /* name */
831 true, /* partial_inplace */
832 MINUS_ONE, /* src_mask */
833 MINUS_ONE, /* dst_mask */
834 false), /* pcrel_offset */
836 /* Used to cause the linker to insert and delete instructions? */
837 EMPTY_HOWTO (R_MIPS_INSERT_A),
838 EMPTY_HOWTO (R_MIPS_INSERT_B),
839 EMPTY_HOWTO (R_MIPS_DELETE),
841 /* Get the higher value of a 64 bit addend. */
842 HOWTO (R_MIPS_HIGHER, /* type */
844 2, /* size (0 = byte, 1 = short, 2 = long) */
846 false, /* pc_relative */
848 complain_overflow_dont, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_MIPS_HIGHER", /* name */
851 true, /* partial_inplace */
853 0xffff, /* dst_mask */
854 false), /* pcrel_offset */
856 /* Get the highest value of a 64 bit addend. */
857 HOWTO (R_MIPS_HIGHEST, /* type */
859 2, /* size (0 = byte, 1 = short, 2 = long) */
861 false, /* pc_relative */
863 complain_overflow_dont, /* complain_on_overflow */
864 bfd_elf_generic_reloc, /* special_function */
865 "R_MIPS_HIGHEST", /* name */
866 true, /* partial_inplace */
868 0xffff, /* dst_mask */
869 false), /* pcrel_offset */
871 /* High 16 bits of displacement in global offset table. */
872 HOWTO (R_MIPS_CALL_HI16, /* type */
874 2, /* size (0 = byte, 1 = short, 2 = long) */
876 false, /* pc_relative */
878 complain_overflow_dont, /* complain_on_overflow */
879 bfd_elf_generic_reloc, /* special_function */
880 "R_MIPS_CALL_HI16", /* name */
881 true, /* partial_inplace */
882 0x0000ffff, /* src_mask */
883 0x0000ffff, /* dst_mask */
884 false), /* pcrel_offset */
886 /* Low 16 bits of displacement in global offset table. */
887 HOWTO (R_MIPS_CALL_LO16, /* type */
889 2, /* size (0 = byte, 1 = short, 2 = long) */
891 false, /* pc_relative */
893 complain_overflow_dont, /* complain_on_overflow */
894 bfd_elf_generic_reloc, /* special_function */
895 "R_MIPS_CALL_LO16", /* name */
896 true, /* partial_inplace */
897 0x0000ffff, /* src_mask */
898 0x0000ffff, /* dst_mask */
899 false), /* pcrel_offset */
901 /* Section displacement. */
902 HOWTO (R_MIPS_SCN_DISP, /* type */
904 2, /* size (0 = byte, 1 = short, 2 = long) */
906 false, /* pc_relative */
908 complain_overflow_dont, /* complain_on_overflow */
909 bfd_elf_generic_reloc, /* special_function */
910 "R_MIPS_SCN_DISP", /* name */
911 false, /* partial_inplace */
912 0xffffffff, /* src_mask */
913 0xffffffff, /* dst_mask */
914 false), /* pcrel_offset */
916 EMPTY_HOWTO (R_MIPS_REL16),
917 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
918 EMPTY_HOWTO (R_MIPS_PJUMP),
919 EMPTY_HOWTO (R_MIPS_RELGOT),
921 /* Protected jump conversion. This is an optimization hint. No
922 relocation is required for correctness. */
923 HOWTO (R_MIPS_JALR, /* type */
925 0, /* size (0 = byte, 1 = short, 2 = long) */
927 false, /* pc_relative */
929 complain_overflow_dont, /* complain_on_overflow */
930 bfd_elf_generic_reloc, /* special_function */
931 "R_MIPS_JALR", /* name */
932 false, /* partial_inplace */
933 0x00000000, /* src_mask */
934 0x00000000, /* dst_mask */
935 false), /* pcrel_offset */
938 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
939 is a hack to make the linker think that we need 64 bit values. */
940 static reloc_howto_type elf_mips_ctor64_howto =
941 HOWTO (R_MIPS_64, /* type */
943 4, /* size (0 = byte, 1 = short, 2 = long) */
945 false, /* pc_relative */
947 complain_overflow_signed, /* complain_on_overflow */
948 mips32_64bit_reloc, /* special_function */
949 "R_MIPS_64", /* name */
950 true, /* partial_inplace */
951 0xffffffff, /* src_mask */
952 0xffffffff, /* dst_mask */
953 false); /* pcrel_offset */
955 /* The reloc used for the mips16 jump instruction. */
956 static reloc_howto_type elf_mips16_jump_howto =
957 HOWTO (R_MIPS16_26, /* type */
959 2, /* size (0 = byte, 1 = short, 2 = long) */
961 false, /* pc_relative */
963 complain_overflow_dont, /* complain_on_overflow */
964 /* This needs complex overflow
965 detection, because the upper four
966 bits must match the PC. */
967 mips16_jump_reloc, /* special_function */
968 "R_MIPS16_26", /* name */
969 true, /* partial_inplace */
970 0x3ffffff, /* src_mask */
971 0x3ffffff, /* dst_mask */
972 false); /* pcrel_offset */
974 /* The reloc used for the mips16 gprel instruction. */
975 static reloc_howto_type elf_mips16_gprel_howto =
976 HOWTO (R_MIPS16_GPREL, /* type */
978 2, /* size (0 = byte, 1 = short, 2 = long) */
980 false, /* pc_relative */
982 complain_overflow_signed, /* complain_on_overflow */
983 mips16_gprel_reloc, /* special_function */
984 "R_MIPS16_GPREL", /* name */
985 true, /* partial_inplace */
986 0x07ff001f, /* src_mask */
987 0x07ff001f, /* dst_mask */
988 false); /* pcrel_offset */
990 /* GNU extensions for embedded-pic. */
991 /* High 16 bits of symbol value, pc-relative. */
992 static reloc_howto_type elf_mips_gnu_rel_hi16 =
993 HOWTO (R_MIPS_GNU_REL_HI16, /* type */
995 2, /* size (0 = byte, 1 = short, 2 = long) */
997 true, /* pc_relative */
999 complain_overflow_dont, /* complain_on_overflow */
1000 _bfd_mips_elf_hi16_reloc, /* special_function */
1001 "R_MIPS_GNU_REL_HI16", /* name */
1002 true, /* partial_inplace */
1003 0xffff, /* src_mask */
1004 0xffff, /* dst_mask */
1005 true); /* pcrel_offset */
1007 /* Low 16 bits of symbol value, pc-relative. */
1008 static reloc_howto_type elf_mips_gnu_rel_lo16 =
1009 HOWTO (R_MIPS_GNU_REL_LO16, /* type */
1011 2, /* size (0 = byte, 1 = short, 2 = long) */
1013 true, /* pc_relative */
1015 complain_overflow_dont, /* complain_on_overflow */
1016 _bfd_mips_elf_lo16_reloc, /* special_function */
1017 "R_MIPS_GNU_REL_LO16", /* name */
1018 true, /* partial_inplace */
1019 0xffff, /* src_mask */
1020 0xffff, /* dst_mask */
1021 true); /* pcrel_offset */
1023 /* 16 bit offset for pc-relative branches. */
1024 static reloc_howto_type elf_mips_gnu_rel16_s2 =
1025 HOWTO (R_MIPS_GNU_REL16_S2, /* type */
1027 2, /* size (0 = byte, 1 = short, 2 = long) */
1029 true, /* pc_relative */
1031 complain_overflow_signed, /* complain_on_overflow */
1032 bfd_elf_generic_reloc, /* special_function */
1033 "R_MIPS_GNU_REL16_S2", /* name */
1034 true, /* partial_inplace */
1035 0xffff, /* src_mask */
1036 0xffff, /* dst_mask */
1037 true); /* pcrel_offset */
1039 /* 64 bit pc-relative. */
1040 static reloc_howto_type elf_mips_gnu_pcrel64 =
1041 HOWTO (R_MIPS_PC64, /* type */
1043 4, /* size (0 = byte, 1 = short, 2 = long) */
1045 true, /* pc_relative */
1047 complain_overflow_signed, /* complain_on_overflow */
1048 bfd_elf_generic_reloc, /* special_function */
1049 "R_MIPS_PC64", /* name */
1050 true, /* partial_inplace */
1051 MINUS_ONE, /* src_mask */
1052 MINUS_ONE, /* dst_mask */
1053 true); /* pcrel_offset */
1055 /* 32 bit pc-relative. */
1056 static reloc_howto_type elf_mips_gnu_pcrel32 =
1057 HOWTO (R_MIPS_PC32, /* type */
1059 2, /* size (0 = byte, 1 = short, 2 = long) */
1061 true, /* pc_relative */
1063 complain_overflow_signed, /* complain_on_overflow */
1064 bfd_elf_generic_reloc, /* special_function */
1065 "R_MIPS_PC32", /* name */
1066 true, /* partial_inplace */
1067 0xffffffff, /* src_mask */
1068 0xffffffff, /* dst_mask */
1069 true); /* pcrel_offset */
1071 /* GNU extension to record C++ vtable hierarchy */
1072 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
1073 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
1075 2, /* size (0 = byte, 1 = short, 2 = long) */
1077 false, /* pc_relative */
1079 complain_overflow_dont, /* complain_on_overflow */
1080 NULL, /* special_function */
1081 "R_MIPS_GNU_VTINHERIT", /* name */
1082 false, /* partial_inplace */
1085 false); /* pcrel_offset */
1087 /* GNU extension to record C++ vtable member usage */
1088 static reloc_howto_type elf_mips_gnu_vtentry_howto =
1089 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
1091 2, /* size (0 = byte, 1 = short, 2 = long) */
1093 false, /* pc_relative */
1095 complain_overflow_dont, /* complain_on_overflow */
1096 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
1097 "R_MIPS_GNU_VTENTRY", /* name */
1098 false, /* partial_inplace */
1101 false); /* pcrel_offset */
1103 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
1104 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
1105 the HI16. Here we just save the information we need; we do the
1106 actual relocation when we see the LO16. MIPS ELF requires that the
1107 LO16 immediately follow the HI16. As a GNU extension, we permit an
1108 arbitrary number of HI16 relocs to be associated with a single LO16
1109 reloc. This extension permits gcc to output the HI and LO relocs
1114 struct mips_hi16 *next;
1119 /* FIXME: This should not be a static variable. */
1121 static struct mips_hi16 *mips_hi16_list;
1123 bfd_reloc_status_type
1124 _bfd_mips_elf_hi16_reloc (abfd,
1131 bfd *abfd ATTRIBUTE_UNUSED;
1132 arelent *reloc_entry;
1135 asection *input_section;
1137 char **error_message;
1139 bfd_reloc_status_type ret;
1141 struct mips_hi16 *n;
1143 /* If we're relocating, and this an external symbol, we don't want
1144 to change anything. */
1145 if (output_bfd != (bfd *) NULL
1146 && (symbol->flags & BSF_SECTION_SYM) == 0
1147 && reloc_entry->addend == 0)
1149 reloc_entry->address += input_section->output_offset;
1150 return bfd_reloc_ok;
1155 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1157 boolean relocateable;
1160 if (ret == bfd_reloc_undefined)
1163 if (output_bfd != NULL)
1164 relocateable = true;
1167 relocateable = false;
1168 output_bfd = symbol->section->output_section->owner;
1171 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1172 error_message, &gp);
1173 if (ret != bfd_reloc_ok)
1176 relocation = gp - reloc_entry->address;
1180 if (bfd_is_und_section (symbol->section)
1181 && output_bfd == (bfd *) NULL)
1182 ret = bfd_reloc_undefined;
1184 if (bfd_is_com_section (symbol->section))
1187 relocation = symbol->value;
1190 relocation += symbol->section->output_section->vma;
1191 relocation += symbol->section->output_offset;
1192 relocation += reloc_entry->addend;
1194 if (reloc_entry->address > input_section->_cooked_size)
1195 return bfd_reloc_outofrange;
1197 /* Save the information, and let LO16 do the actual relocation. */
1198 n = (struct mips_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
1200 return bfd_reloc_outofrange;
1201 n->addr = (bfd_byte *) data + reloc_entry->address;
1202 n->addend = relocation;
1203 n->next = mips_hi16_list;
1206 if (output_bfd != (bfd *) NULL)
1207 reloc_entry->address += input_section->output_offset;
1212 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
1213 inplace relocation; this function exists in order to do the
1214 R_MIPS_HI16 relocation described above. */
1216 bfd_reloc_status_type
1217 _bfd_mips_elf_lo16_reloc (abfd,
1225 arelent *reloc_entry;
1228 asection *input_section;
1230 char **error_message;
1232 arelent gp_disp_relent;
1234 if (mips_hi16_list != NULL)
1236 struct mips_hi16 *l;
1243 unsigned long vallo;
1244 struct mips_hi16 *next;
1246 /* Do the HI16 relocation. Note that we actually don't need
1247 to know anything about the LO16 itself, except where to
1248 find the low 16 bits of the addend needed by the LO16. */
1249 insn = bfd_get_32 (abfd, l->addr);
1250 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1252 val = ((insn & 0xffff) << 16) + vallo;
1255 /* The low order 16 bits are always treated as a signed
1256 value. Therefore, a negative value in the low order bits
1257 requires an adjustment in the high order bits. We need
1258 to make this adjustment in two ways: once for the bits we
1259 took from the data, and once for the bits we are putting
1260 back in to the data. */
1261 if ((vallo & 0x8000) != 0)
1263 if ((val & 0x8000) != 0)
1266 insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
1267 bfd_put_32 (abfd, (bfd_vma) insn, l->addr);
1269 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1271 gp_disp_relent = *reloc_entry;
1272 reloc_entry = &gp_disp_relent;
1273 reloc_entry->addend = l->addend;
1281 mips_hi16_list = NULL;
1283 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1285 bfd_reloc_status_type ret;
1286 bfd_vma gp, relocation;
1288 /* FIXME: Does this case ever occur? */
1290 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1291 if (ret != bfd_reloc_ok)
1294 relocation = gp - reloc_entry->address;
1295 relocation += symbol->section->output_section->vma;
1296 relocation += symbol->section->output_offset;
1297 relocation += reloc_entry->addend;
1299 if (reloc_entry->address > input_section->_cooked_size)
1300 return bfd_reloc_outofrange;
1302 gp_disp_relent = *reloc_entry;
1303 reloc_entry = &gp_disp_relent;
1304 reloc_entry->addend = relocation - 4;
1307 /* Now do the LO16 reloc in the usual way. */
1308 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1309 input_section, output_bfd, error_message);
1312 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1313 table used for PIC code. If the symbol is an external symbol, the
1314 instruction is modified to contain the offset of the appropriate
1315 entry in the global offset table. If the symbol is a section
1316 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1317 addends are combined to form the real addend against the section
1318 symbol; the GOT16 is modified to contain the offset of an entry in
1319 the global offset table, and the LO16 is modified to offset it
1320 appropriately. Thus an offset larger than 16 bits requires a
1321 modified value in the global offset table.
1323 This implementation suffices for the assembler, but the linker does
1324 not yet know how to create global offset tables. */
1326 bfd_reloc_status_type
1327 _bfd_mips_elf_got16_reloc (abfd,
1335 arelent *reloc_entry;
1338 asection *input_section;
1340 char **error_message;
1342 /* If we're relocating, and this an external symbol, we don't want
1343 to change anything. */
1344 if (output_bfd != (bfd *) NULL
1345 && (symbol->flags & BSF_SECTION_SYM) == 0
1346 && reloc_entry->addend == 0)
1348 reloc_entry->address += input_section->output_offset;
1349 return bfd_reloc_ok;
1352 /* If we're relocating, and this is a local symbol, we can handle it
1354 if (output_bfd != (bfd *) NULL
1355 && (symbol->flags & BSF_SECTION_SYM) != 0)
1356 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1357 input_section, output_bfd, error_message);
1362 /* Set the GP value for OUTPUT_BFD. Returns false if this is a
1363 dangerous relocation. */
1366 mips_elf_assign_gp (output_bfd, pgp)
1374 /* If we've already figured out what GP will be, just return it. */
1375 *pgp = _bfd_get_gp_value (output_bfd);
1379 count = bfd_get_symcount (output_bfd);
1380 sym = bfd_get_outsymbols (output_bfd);
1382 /* The linker script will have created a symbol named `_gp' with the
1383 appropriate value. */
1384 if (sym == (asymbol **) NULL)
1388 for (i = 0; i < count; i++, sym++)
1390 register const char *name;
1392 name = bfd_asymbol_name (*sym);
1393 if (*name == '_' && strcmp (name, "_gp") == 0)
1395 *pgp = bfd_asymbol_value (*sym);
1396 _bfd_set_gp_value (output_bfd, *pgp);
1404 /* Only get the error once. */
1406 _bfd_set_gp_value (output_bfd, *pgp);
1413 /* We have to figure out the gp value, so that we can adjust the
1414 symbol value correctly. We look up the symbol _gp in the output
1415 BFD. If we can't find it, we're stuck. We cache it in the ELF
1416 target data. We don't need to adjust the symbol value for an
1417 external symbol if we are producing relocateable output. */
1419 static bfd_reloc_status_type
1420 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1423 boolean relocateable;
1424 char **error_message;
1427 if (bfd_is_und_section (symbol->section)
1431 return bfd_reloc_undefined;
1434 *pgp = _bfd_get_gp_value (output_bfd);
1437 || (symbol->flags & BSF_SECTION_SYM) != 0))
1441 /* Make up a value. */
1442 *pgp = symbol->section->output_section->vma + 0x4000;
1443 _bfd_set_gp_value (output_bfd, *pgp);
1445 else if (!mips_elf_assign_gp (output_bfd, pgp))
1448 (char *) _("GP relative relocation when _gp not defined");
1449 return bfd_reloc_dangerous;
1453 return bfd_reloc_ok;
1456 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1457 become the offset from the gp register. This function also handles
1458 R_MIPS_LITERAL relocations, although those can be handled more
1459 cleverly because the entries in the .lit8 and .lit4 sections can be
1462 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1463 arelent *, asection *,
1464 boolean, PTR, bfd_vma));
1466 bfd_reloc_status_type
1467 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1468 output_bfd, error_message)
1470 arelent *reloc_entry;
1473 asection *input_section;
1475 char **error_message;
1477 boolean relocateable;
1478 bfd_reloc_status_type ret;
1481 /* If we're relocating, and this is an external symbol with no
1482 addend, we don't want to change anything. We will only have an
1483 addend if this is a newly created reloc, not read from an ELF
1485 if (output_bfd != (bfd *) NULL
1486 && (symbol->flags & BSF_SECTION_SYM) == 0
1487 && reloc_entry->addend == 0)
1489 reloc_entry->address += input_section->output_offset;
1490 return bfd_reloc_ok;
1493 if (output_bfd != (bfd *) NULL)
1494 relocateable = true;
1497 relocateable = false;
1498 output_bfd = symbol->section->output_section->owner;
1501 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1503 if (ret != bfd_reloc_ok)
1506 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1507 relocateable, data, gp);
1510 static bfd_reloc_status_type
1511 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1515 arelent *reloc_entry;
1516 asection *input_section;
1517 boolean relocateable;
1525 if (bfd_is_com_section (symbol->section))
1528 relocation = symbol->value;
1530 relocation += symbol->section->output_section->vma;
1531 relocation += symbol->section->output_offset;
1533 if (reloc_entry->address > input_section->_cooked_size)
1534 return bfd_reloc_outofrange;
1536 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1538 /* Set val to the offset into the section or symbol. */
1539 if (reloc_entry->howto->src_mask == 0)
1541 /* This case occurs with the 64-bit MIPS ELF ABI. */
1542 val = reloc_entry->addend;
1546 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1551 /* Adjust val for the final section location and GP value. If we
1552 are producing relocateable output, we don't want to do this for
1553 an external symbol. */
1555 || (symbol->flags & BSF_SECTION_SYM) != 0)
1556 val += relocation - gp;
1558 insn = (insn &~ (bfd_vma) 0xffff) | (val & 0xffff);
1559 bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + reloc_entry->address);
1562 reloc_entry->address += input_section->output_offset;
1564 /* Make sure it fit in 16 bits. */
1565 if ((long) val >= 0x8000 || (long) val < -0x8000)
1566 return bfd_reloc_overflow;
1568 return bfd_reloc_ok;
1571 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1572 from the gp register? XXX */
1574 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1575 arelent *, asection *,
1576 boolean, PTR, bfd_vma));
1578 bfd_reloc_status_type
1579 _bfd_mips_elf_gprel32_reloc (abfd,
1587 arelent *reloc_entry;
1590 asection *input_section;
1592 char **error_message;
1594 boolean relocateable;
1595 bfd_reloc_status_type ret;
1598 /* If we're relocating, and this is an external symbol with no
1599 addend, we don't want to change anything. We will only have an
1600 addend if this is a newly created reloc, not read from an ELF
1602 if (output_bfd != (bfd *) NULL
1603 && (symbol->flags & BSF_SECTION_SYM) == 0
1604 && reloc_entry->addend == 0)
1606 *error_message = (char *)
1607 _("32bits gp relative relocation occurs for an external symbol");
1608 return bfd_reloc_outofrange;
1611 if (output_bfd != (bfd *) NULL)
1613 relocateable = true;
1614 gp = _bfd_get_gp_value (output_bfd);
1618 relocateable = false;
1619 output_bfd = symbol->section->output_section->owner;
1621 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1622 error_message, &gp);
1623 if (ret != bfd_reloc_ok)
1627 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1628 relocateable, data, gp);
1631 static bfd_reloc_status_type
1632 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1636 arelent *reloc_entry;
1637 asection *input_section;
1638 boolean relocateable;
1645 if (bfd_is_com_section (symbol->section))
1648 relocation = symbol->value;
1650 relocation += symbol->section->output_section->vma;
1651 relocation += symbol->section->output_offset;
1653 if (reloc_entry->address > input_section->_cooked_size)
1654 return bfd_reloc_outofrange;
1656 if (reloc_entry->howto->src_mask == 0)
1658 /* This case arises with the 64-bit MIPS ELF ABI. */
1662 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1664 /* Set val to the offset into the section or symbol. */
1665 val += reloc_entry->addend;
1667 /* Adjust val for the final section location and GP value. If we
1668 are producing relocateable output, we don't want to do this for
1669 an external symbol. */
1671 || (symbol->flags & BSF_SECTION_SYM) != 0)
1672 val += relocation - gp;
1674 bfd_put_32 (abfd, (bfd_vma) val, (bfd_byte *) data + reloc_entry->address);
1677 reloc_entry->address += input_section->output_offset;
1679 return bfd_reloc_ok;
1682 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1683 generated when addresses are 64 bits. The upper 32 bits are a simple
1686 static bfd_reloc_status_type
1687 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1688 output_bfd, error_message)
1690 arelent *reloc_entry;
1693 asection *input_section;
1695 char **error_message;
1697 bfd_reloc_status_type r;
1702 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1703 input_section, output_bfd, error_message);
1704 if (r != bfd_reloc_continue)
1707 /* Do a normal 32 bit relocation on the lower 32 bits. */
1708 reloc32 = *reloc_entry;
1709 if (bfd_big_endian (abfd))
1710 reloc32.address += 4;
1711 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1712 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1713 output_bfd, error_message);
1715 /* Sign extend into the upper 32 bits. */
1716 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1717 if ((val & 0x80000000) != 0)
1721 addr = reloc_entry->address;
1722 if (bfd_little_endian (abfd))
1724 bfd_put_32 (abfd, (bfd_vma) val, (bfd_byte *) data + addr);
1729 /* Handle a mips16 jump. */
1731 static bfd_reloc_status_type
1732 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1733 output_bfd, error_message)
1734 bfd *abfd ATTRIBUTE_UNUSED;
1735 arelent *reloc_entry;
1737 PTR data ATTRIBUTE_UNUSED;
1738 asection *input_section;
1740 char **error_message ATTRIBUTE_UNUSED;
1742 if (output_bfd != (bfd *) NULL
1743 && (symbol->flags & BSF_SECTION_SYM) == 0
1744 && reloc_entry->addend == 0)
1746 reloc_entry->address += input_section->output_offset;
1747 return bfd_reloc_ok;
1752 static boolean warned;
1755 (*_bfd_error_handler)
1756 (_("Linking mips16 objects into %s format is not supported"),
1757 bfd_get_target (input_section->output_section->owner));
1761 return bfd_reloc_undefined;
1764 /* Handle a mips16 GP relative reloc. */
1766 static bfd_reloc_status_type
1767 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1768 output_bfd, error_message)
1770 arelent *reloc_entry;
1773 asection *input_section;
1775 char **error_message;
1777 boolean relocateable;
1778 bfd_reloc_status_type ret;
1780 unsigned short extend, insn;
1781 unsigned long final;
1783 /* If we're relocating, and this is an external symbol with no
1784 addend, we don't want to change anything. We will only have an
1785 addend if this is a newly created reloc, not read from an ELF
1787 if (output_bfd != NULL
1788 && (symbol->flags & BSF_SECTION_SYM) == 0
1789 && reloc_entry->addend == 0)
1791 reloc_entry->address += input_section->output_offset;
1792 return bfd_reloc_ok;
1795 if (output_bfd != NULL)
1796 relocateable = true;
1799 relocateable = false;
1800 output_bfd = symbol->section->output_section->owner;
1803 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1805 if (ret != bfd_reloc_ok)
1808 if (reloc_entry->address > input_section->_cooked_size)
1809 return bfd_reloc_outofrange;
1811 /* Pick up the mips16 extend instruction and the real instruction. */
1812 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1813 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1815 /* Stuff the current addend back as a 32 bit value, do the usual
1816 relocation, and then clean up. */
1818 (bfd_vma) (((extend & 0x1f) << 11)
1821 (bfd_byte *) data + reloc_entry->address);
1823 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1824 relocateable, data, gp);
1826 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1828 (bfd_vma) ((extend & 0xf800)
1829 | ((final >> 11) & 0x1f)
1831 (bfd_byte *) data + reloc_entry->address);
1833 (bfd_vma) ((insn & 0xffe0)
1835 (bfd_byte *) data + reloc_entry->address + 2);
1840 /* Return the ISA for a MIPS e_flags value. */
1843 elf_mips_isa (flags)
1846 switch (flags & EF_MIPS_ARCH)
1858 case E_MIPS_ARCH_32:
1860 case E_MIPS_ARCH_64:
1866 /* Return the MACH for a MIPS e_flags value. */
1868 static INLINE unsigned long
1869 elf_mips_mach (flags)
1872 switch (flags & EF_MIPS_MACH)
1874 case E_MIPS_MACH_3900:
1875 return bfd_mach_mips3900;
1877 case E_MIPS_MACH_4010:
1878 return bfd_mach_mips4010;
1880 case E_MIPS_MACH_4100:
1881 return bfd_mach_mips4100;
1883 case E_MIPS_MACH_4111:
1884 return bfd_mach_mips4111;
1886 case E_MIPS_MACH_4650:
1887 return bfd_mach_mips4650;
1889 case E_MIPS_MACH_SB1:
1890 return bfd_mach_mips_sb1;
1893 switch (flags & EF_MIPS_ARCH)
1897 return bfd_mach_mips3000;
1901 return bfd_mach_mips6000;
1905 return bfd_mach_mips4000;
1909 return bfd_mach_mips8000;
1913 return bfd_mach_mips5;
1916 case E_MIPS_ARCH_32:
1917 return bfd_mach_mipsisa32;
1920 case E_MIPS_ARCH_64:
1921 return bfd_mach_mipsisa64;
1929 /* Return printable name for ABI. */
1931 static INLINE char *
1932 elf_mips_abi_name (abfd)
1937 flags = elf_elfheader (abfd)->e_flags;
1938 switch (flags & EF_MIPS_ABI)
1941 if (ABI_N32_P (abfd))
1943 else if (ABI_64_P (abfd))
1947 case E_MIPS_ABI_O32:
1949 case E_MIPS_ABI_O64:
1951 case E_MIPS_ABI_EABI32:
1953 case E_MIPS_ABI_EABI64:
1956 return "unknown abi";
1960 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1962 struct elf_reloc_map {
1963 bfd_reloc_code_real_type bfd_reloc_val;
1964 enum elf_mips_reloc_type elf_reloc_val;
1967 static const struct elf_reloc_map mips_reloc_map[] =
1969 { BFD_RELOC_NONE, R_MIPS_NONE, },
1970 { BFD_RELOC_16, R_MIPS_16 },
1971 { BFD_RELOC_32, R_MIPS_32 },
1972 { BFD_RELOC_64, R_MIPS_64 },
1973 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1974 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1975 { BFD_RELOC_LO16, R_MIPS_LO16 },
1976 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1977 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1978 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1979 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1980 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1981 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1982 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1983 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1984 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1985 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1986 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1987 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1988 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1989 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1992 /* Given a BFD reloc type, return a howto structure. */
1994 static reloc_howto_type *
1995 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1997 bfd_reloc_code_real_type code;
2001 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
2003 if (mips_reloc_map[i].bfd_reloc_val == code)
2004 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
2010 bfd_set_error (bfd_error_bad_value);
2013 case BFD_RELOC_CTOR:
2014 /* We need to handle BFD_RELOC_CTOR specially.
2015 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
2016 size of addresses on this architecture. */
2017 if (bfd_arch_bits_per_address (abfd) == 32)
2018 return &elf_mips_howto_table[(int) R_MIPS_32];
2020 return &elf_mips_ctor64_howto;
2022 case BFD_RELOC_MIPS16_JMP:
2023 return &elf_mips16_jump_howto;
2024 case BFD_RELOC_MIPS16_GPREL:
2025 return &elf_mips16_gprel_howto;
2026 case BFD_RELOC_VTABLE_INHERIT:
2027 return &elf_mips_gnu_vtinherit_howto;
2028 case BFD_RELOC_VTABLE_ENTRY:
2029 return &elf_mips_gnu_vtentry_howto;
2030 case BFD_RELOC_PCREL_HI16_S:
2031 return &elf_mips_gnu_rel_hi16;
2032 case BFD_RELOC_PCREL_LO16:
2033 return &elf_mips_gnu_rel_lo16;
2034 case BFD_RELOC_16_PCREL_S2:
2035 return &elf_mips_gnu_rel16_s2;
2036 case BFD_RELOC_64_PCREL:
2037 return &elf_mips_gnu_pcrel64;
2038 case BFD_RELOC_32_PCREL:
2039 return &elf_mips_gnu_pcrel32;
2043 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
2045 static reloc_howto_type *
2046 mips_rtype_to_howto (r_type)
2047 unsigned int r_type;
2052 return &elf_mips16_jump_howto;
2054 case R_MIPS16_GPREL:
2055 return &elf_mips16_gprel_howto;
2057 case R_MIPS_GNU_VTINHERIT:
2058 return &elf_mips_gnu_vtinherit_howto;
2060 case R_MIPS_GNU_VTENTRY:
2061 return &elf_mips_gnu_vtentry_howto;
2063 case R_MIPS_GNU_REL_HI16:
2064 return &elf_mips_gnu_rel_hi16;
2066 case R_MIPS_GNU_REL_LO16:
2067 return &elf_mips_gnu_rel_lo16;
2069 case R_MIPS_GNU_REL16_S2:
2070 return &elf_mips_gnu_rel16_s2;
2073 return &elf_mips_gnu_pcrel64;
2076 return &elf_mips_gnu_pcrel32;
2080 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
2081 return &elf_mips_howto_table[r_type];
2086 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
2089 mips_info_to_howto_rel (abfd, cache_ptr, dst)
2092 Elf32_Internal_Rel *dst;
2094 unsigned int r_type;
2096 r_type = ELF32_R_TYPE (dst->r_info);
2097 cache_ptr->howto = mips_rtype_to_howto (r_type);
2099 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
2100 value for the object file. We get the addend now, rather than
2101 when we do the relocation, because the symbol manipulations done
2102 by the linker may cause us to lose track of the input BFD. */
2103 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
2104 && (r_type == (unsigned int) R_MIPS_GPREL16
2105 || r_type == (unsigned int) R_MIPS_LITERAL))
2106 cache_ptr->addend = elf_gp (abfd);
2109 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
2112 mips_info_to_howto_rela (abfd, cache_ptr, dst)
2115 Elf32_Internal_Rela *dst;
2117 /* Since an Elf32_Internal_Rel is an initial prefix of an
2118 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
2120 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
2122 /* If we ever need to do any extra processing with dst->r_addend
2123 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
2126 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2127 routines swap this structure in and out. They are used outside of
2128 BFD, so they are globally visible. */
2131 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
2133 const Elf32_External_RegInfo *ex;
2136 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2137 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2138 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2139 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2140 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2141 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2145 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
2147 const Elf32_RegInfo *in;
2148 Elf32_External_RegInfo *ex;
2150 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2151 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2152 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2153 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2154 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2155 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2158 /* In the 64 bit ABI, the .MIPS.options section holds register
2159 information in an Elf64_Reginfo structure. These routines swap
2160 them in and out. They are globally visible because they are used
2161 outside of BFD. These routines are here so that gas can call them
2162 without worrying about whether the 64 bit ABI has been included. */
2165 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
2167 const Elf64_External_RegInfo *ex;
2168 Elf64_Internal_RegInfo *in;
2170 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2171 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2172 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2173 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2174 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2175 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2176 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2180 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
2182 const Elf64_Internal_RegInfo *in;
2183 Elf64_External_RegInfo *ex;
2185 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2186 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2187 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2188 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2189 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2190 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2191 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2194 /* Swap an entry in a .gptab section. Note that these routines rely
2195 on the equivalence of the two elements of the union. */
2198 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
2200 const Elf32_External_gptab *ex;
2203 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2204 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2208 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
2210 const Elf32_gptab *in;
2211 Elf32_External_gptab *ex;
2213 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2214 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2218 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
2220 const Elf32_compact_rel *in;
2221 Elf32_External_compact_rel *ex;
2223 H_PUT_32 (abfd, in->id1, ex->id1);
2224 H_PUT_32 (abfd, in->num, ex->num);
2225 H_PUT_32 (abfd, in->id2, ex->id2);
2226 H_PUT_32 (abfd, in->offset, ex->offset);
2227 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2228 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2232 bfd_elf32_swap_crinfo_out (abfd, in, ex)
2234 const Elf32_crinfo *in;
2235 Elf32_External_crinfo *ex;
2239 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2240 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2241 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2242 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2243 H_PUT_32 (abfd, l, ex->info);
2244 H_PUT_32 (abfd, in->konst, ex->konst);
2245 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2248 /* Swap in an options header. */
2251 bfd_mips_elf_swap_options_in (abfd, ex, in)
2253 const Elf_External_Options *ex;
2254 Elf_Internal_Options *in;
2256 in->kind = H_GET_8 (abfd, ex->kind);
2257 in->size = H_GET_8 (abfd, ex->size);
2258 in->section = H_GET_16 (abfd, ex->section);
2259 in->info = H_GET_32 (abfd, ex->info);
2262 /* Swap out an options header. */
2265 bfd_mips_elf_swap_options_out (abfd, in, ex)
2267 const Elf_Internal_Options *in;
2268 Elf_External_Options *ex;
2270 H_PUT_8 (abfd, in->kind, ex->kind);
2271 H_PUT_8 (abfd, in->size, ex->size);
2272 H_PUT_16 (abfd, in->section, ex->section);
2273 H_PUT_32 (abfd, in->info, ex->info);
2276 /* Swap in an MSYM entry. */
2279 bfd_mips_elf_swap_msym_in (abfd, ex, in)
2281 const Elf32_External_Msym *ex;
2282 Elf32_Internal_Msym *in;
2284 in->ms_hash_value = H_GET_32 (abfd, ex->ms_hash_value);
2285 in->ms_info = H_GET_32 (abfd, ex->ms_info);
2288 /* Swap out an MSYM entry. */
2291 bfd_mips_elf_swap_msym_out (abfd, in, ex)
2293 const Elf32_Internal_Msym *in;
2294 Elf32_External_Msym *ex;
2296 H_PUT_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
2297 H_PUT_32 (abfd, in->ms_info, ex->ms_info);
2300 /* Determine whether a symbol is global for the purposes of splitting
2301 the symbol table into global symbols and local symbols. At least
2302 on Irix 5, this split must be between section symbols and all other
2303 symbols. On most ELF targets the split is between static symbols
2304 and externally visible symbols. */
2307 mips_elf_sym_is_global (abfd, sym)
2308 bfd *abfd ATTRIBUTE_UNUSED;
2311 if (SGI_COMPAT (abfd))
2312 return (sym->flags & BSF_SECTION_SYM) == 0;
2314 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2315 || bfd_is_und_section (bfd_get_section (sym))
2316 || bfd_is_com_section (bfd_get_section (sym)));
2319 /* Set the right machine number for a MIPS ELF file. This is used for
2320 both the 32-bit and the 64-bit ABI. */
2323 _bfd_mips_elf_object_p (abfd)
2326 /* Irix 5 and 6 is broken. Object file symbol tables are not always
2327 sorted correctly such that local symbols precede global symbols,
2328 and the sh_info field in the symbol table is not always right. */
2329 if (SGI_COMPAT(abfd))
2330 elf_bad_symtab (abfd) = true;
2332 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
2333 elf_mips_mach (elf_elfheader (abfd)->e_flags));
2337 /* The final processing done just before writing out a MIPS ELF object
2338 file. This gets the MIPS architecture right based on the machine
2339 number. This is used by both the 32-bit and the 64-bit ABI. */
2342 _bfd_mips_elf_final_write_processing (abfd, linker)
2344 boolean linker ATTRIBUTE_UNUSED;
2348 Elf_Internal_Shdr **hdrpp;
2352 switch (bfd_get_mach (abfd))
2355 case bfd_mach_mips3000:
2356 val = E_MIPS_ARCH_1;
2359 case bfd_mach_mips3900:
2360 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
2363 case bfd_mach_mips6000:
2364 val = E_MIPS_ARCH_2;
2367 case bfd_mach_mips4000:
2368 case bfd_mach_mips4300:
2369 case bfd_mach_mips4400:
2370 case bfd_mach_mips4600:
2371 val = E_MIPS_ARCH_3;
2374 case bfd_mach_mips4010:
2375 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
2378 case bfd_mach_mips4100:
2379 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2382 case bfd_mach_mips4111:
2383 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2386 case bfd_mach_mips4650:
2387 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2390 case bfd_mach_mips5000:
2391 case bfd_mach_mips8000:
2392 case bfd_mach_mips10000:
2393 case bfd_mach_mips12000:
2394 val = E_MIPS_ARCH_4;
2397 case bfd_mach_mips5:
2398 val = E_MIPS_ARCH_5;
2401 case bfd_mach_mips_sb1:
2402 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
2405 case bfd_mach_mipsisa32:
2406 val = E_MIPS_ARCH_32;
2409 case bfd_mach_mipsisa64:
2410 val = E_MIPS_ARCH_64;
2413 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
2414 elf_elfheader (abfd)->e_flags |= val;
2416 /* Set the sh_info field for .gptab sections and other appropriate
2417 info for each special section. */
2418 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2419 i < elf_elfheader (abfd)->e_shnum;
2422 switch ((*hdrpp)->sh_type)
2425 case SHT_MIPS_LIBLIST:
2426 sec = bfd_get_section_by_name (abfd, ".dynstr");
2428 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2431 case SHT_MIPS_GPTAB:
2432 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2433 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2434 BFD_ASSERT (name != NULL
2435 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2436 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2437 BFD_ASSERT (sec != NULL);
2438 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2441 case SHT_MIPS_CONTENT:
2442 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2443 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2444 BFD_ASSERT (name != NULL
2445 && strncmp (name, ".MIPS.content",
2446 sizeof ".MIPS.content" - 1) == 0);
2447 sec = bfd_get_section_by_name (abfd,
2448 name + sizeof ".MIPS.content" - 1);
2449 BFD_ASSERT (sec != NULL);
2450 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2453 case SHT_MIPS_SYMBOL_LIB:
2454 sec = bfd_get_section_by_name (abfd, ".dynsym");
2456 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2457 sec = bfd_get_section_by_name (abfd, ".liblist");
2459 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2462 case SHT_MIPS_EVENTS:
2463 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2464 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2465 BFD_ASSERT (name != NULL);
2466 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2467 sec = bfd_get_section_by_name (abfd,
2468 name + sizeof ".MIPS.events" - 1);
2471 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2472 sizeof ".MIPS.post_rel" - 1) == 0);
2473 sec = bfd_get_section_by_name (abfd,
2475 + sizeof ".MIPS.post_rel" - 1));
2477 BFD_ASSERT (sec != NULL);
2478 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2485 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2488 _bfd_mips_elf_set_private_flags (abfd, flags)
2492 BFD_ASSERT (!elf_flags_init (abfd)
2493 || elf_elfheader (abfd)->e_flags == flags);
2495 elf_elfheader (abfd)->e_flags = flags;
2496 elf_flags_init (abfd) = true;
2500 /* Copy backend specific data from one object module to another */
2503 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2507 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2508 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2511 BFD_ASSERT (!elf_flags_init (obfd)
2512 || (elf_elfheader (obfd)->e_flags
2513 == elf_elfheader (ibfd)->e_flags));
2515 elf_gp (obfd) = elf_gp (ibfd);
2516 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2517 elf_flags_init (obfd) = true;
2521 /* Merge backend specific data from an object file to the output
2522 object file when linking. */
2525 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2532 boolean null_input_bfd = true;
2535 /* Check if we have the same endianess */
2536 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
2539 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2540 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2543 new_flags = elf_elfheader (ibfd)->e_flags;
2544 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2545 old_flags = elf_elfheader (obfd)->e_flags;
2547 if (! elf_flags_init (obfd))
2549 elf_flags_init (obfd) = true;
2550 elf_elfheader (obfd)->e_flags = new_flags;
2551 elf_elfheader (obfd)->e_ident[EI_CLASS]
2552 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
2554 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2555 && bfd_get_arch_info (obfd)->the_default)
2557 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2558 bfd_get_mach (ibfd)))
2565 /* Check flag compatibility. */
2567 new_flags &= ~EF_MIPS_NOREORDER;
2568 old_flags &= ~EF_MIPS_NOREORDER;
2570 if (new_flags == old_flags)
2573 /* Check to see if the input BFD actually contains any sections.
2574 If not, its flags may not have been initialised either, but it cannot
2575 actually cause any incompatibility. */
2576 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2578 /* Ignore synthetic sections and empty .text, .data and .bss sections
2579 which are automatically generated by gas. */
2580 if (strcmp (sec->name, ".reginfo")
2581 && strcmp (sec->name, ".mdebug")
2582 && ((!strcmp (sec->name, ".text")
2583 || !strcmp (sec->name, ".data")
2584 || !strcmp (sec->name, ".bss"))
2585 && sec->_raw_size != 0))
2587 null_input_bfd = false;
2596 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2598 new_flags &= ~EF_MIPS_PIC;
2599 old_flags &= ~EF_MIPS_PIC;
2600 (*_bfd_error_handler)
2601 (_("%s: linking PIC files with non-PIC files"),
2602 bfd_get_filename (ibfd));
2606 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2608 new_flags &= ~EF_MIPS_CPIC;
2609 old_flags &= ~EF_MIPS_CPIC;
2610 (*_bfd_error_handler)
2611 (_("%s: linking abicalls files with non-abicalls files"),
2612 bfd_get_filename (ibfd));
2616 /* Compare the ISA's. */
2617 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2618 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2620 int new_mach = new_flags & EF_MIPS_MACH;
2621 int old_mach = old_flags & EF_MIPS_MACH;
2622 int new_isa = elf_mips_isa (new_flags);
2623 int old_isa = elf_mips_isa (old_flags);
2625 /* If either has no machine specified, just compare the general isa's.
2626 Some combinations of machines are ok, if the isa's match. */
2629 || new_mach == old_mach
2632 /* Don't warn about mixing code using 32-bit ISAs, or mixing code
2633 using 64-bit ISAs. They will normally use the same data sizes
2634 and calling conventions. */
2636 if (( (new_isa == 1 || new_isa == 2 || new_isa == 32)
2637 ^ (old_isa == 1 || old_isa == 2 || old_isa == 32)) != 0)
2639 (*_bfd_error_handler)
2640 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2641 bfd_get_filename (ibfd), new_isa, old_isa);
2648 (*_bfd_error_handler)
2649 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2650 bfd_get_filename (ibfd),
2651 elf_mips_mach (new_flags),
2652 elf_mips_mach (old_flags));
2656 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
2657 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
2660 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
2661 does set EI_CLASS differently from any 32-bit ABI. */
2662 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
2663 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2664 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2666 /* Only error if both are set (to different values). */
2667 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
2668 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2669 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2671 (*_bfd_error_handler)
2672 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2673 bfd_get_filename (ibfd),
2674 elf_mips_abi_name (ibfd),
2675 elf_mips_abi_name (obfd));
2678 new_flags &= ~EF_MIPS_ABI;
2679 old_flags &= ~EF_MIPS_ABI;
2682 /* Warn about any other mismatches */
2683 if (new_flags != old_flags)
2685 (*_bfd_error_handler)
2686 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2687 bfd_get_filename (ibfd), (unsigned long) new_flags,
2688 (unsigned long) old_flags);
2694 bfd_set_error (bfd_error_bad_value);
2702 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2706 FILE *file = (FILE *) ptr;
2708 BFD_ASSERT (abfd != NULL && ptr != NULL);
2710 /* Print normal ELF private data. */
2711 _bfd_elf_print_private_bfd_data (abfd, ptr);
2713 /* xgettext:c-format */
2714 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2716 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2717 fprintf (file, _(" [abi=O32]"));
2718 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2719 fprintf (file, _(" [abi=O64]"));
2720 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2721 fprintf (file, _(" [abi=EABI32]"));
2722 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2723 fprintf (file, _(" [abi=EABI64]"));
2724 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2725 fprintf (file, _(" [abi unknown]"));
2726 else if (ABI_N32_P (abfd))
2727 fprintf (file, _(" [abi=N32]"));
2728 else if (ABI_64_P (abfd))
2729 fprintf (file, _(" [abi=64]"));
2731 fprintf (file, _(" [no abi set]"));
2733 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2734 fprintf (file, _(" [mips1]"));
2735 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2736 fprintf (file, _(" [mips2]"));
2737 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2738 fprintf (file, _(" [mips3]"));
2739 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2740 fprintf (file, _(" [mips4]"));
2741 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
2742 fprintf (file, _ (" [mips5]"));
2743 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
2744 fprintf (file, _ (" [mips32]"));
2745 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
2746 fprintf (file, _ (" [mips64]"));
2748 fprintf (file, _(" [unknown ISA]"));
2750 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2751 fprintf (file, _(" [32bitmode]"));
2753 fprintf (file, _(" [not 32bitmode]"));
2760 /* Handle a MIPS specific section when reading an object file. This
2761 is called when elfcode.h finds a section with an unknown type.
2762 This routine supports both the 32-bit and 64-bit ELF ABI.
2764 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2768 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2770 Elf_Internal_Shdr *hdr;
2775 /* There ought to be a place to keep ELF backend specific flags, but
2776 at the moment there isn't one. We just keep track of the
2777 sections by their name, instead. Fortunately, the ABI gives
2778 suggested names for all the MIPS specific sections, so we will
2779 probably get away with this. */
2780 switch (hdr->sh_type)
2782 case SHT_MIPS_LIBLIST:
2783 if (strcmp (name, ".liblist") != 0)
2787 if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0)
2790 case SHT_MIPS_CONFLICT:
2791 if (strcmp (name, ".conflict") != 0)
2794 case SHT_MIPS_GPTAB:
2795 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2798 case SHT_MIPS_UCODE:
2799 if (strcmp (name, ".ucode") != 0)
2802 case SHT_MIPS_DEBUG:
2803 if (strcmp (name, ".mdebug") != 0)
2805 flags = SEC_DEBUGGING;
2807 case SHT_MIPS_REGINFO:
2808 if (strcmp (name, ".reginfo") != 0
2809 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2811 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2813 case SHT_MIPS_IFACE:
2814 if (strcmp (name, ".MIPS.interfaces") != 0)
2817 case SHT_MIPS_CONTENT:
2818 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2821 case SHT_MIPS_OPTIONS:
2822 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
2825 case SHT_MIPS_DWARF:
2826 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2829 case SHT_MIPS_SYMBOL_LIB:
2830 if (strcmp (name, ".MIPS.symlib") != 0)
2833 case SHT_MIPS_EVENTS:
2834 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2835 && strncmp (name, ".MIPS.post_rel",
2836 sizeof ".MIPS.post_rel" - 1) != 0)
2843 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2848 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2849 (bfd_get_section_flags (abfd,
2855 /* FIXME: We should record sh_info for a .gptab section. */
2857 /* For a .reginfo section, set the gp value in the tdata information
2858 from the contents of this section. We need the gp value while
2859 processing relocs, so we just get it now. The .reginfo section
2860 is not used in the 64-bit MIPS ELF ABI. */
2861 if (hdr->sh_type == SHT_MIPS_REGINFO)
2863 Elf32_External_RegInfo ext;
2866 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2868 (bfd_size_type) sizeof ext))
2870 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2871 elf_gp (abfd) = s.ri_gp_value;
2874 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2875 set the gp value based on what we find. We may see both
2876 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2877 they should agree. */
2878 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2880 bfd_byte *contents, *l, *lend;
2882 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2883 if (contents == NULL)
2885 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2886 (file_ptr) 0, hdr->sh_size))
2892 lend = contents + hdr->sh_size;
2893 while (l + sizeof (Elf_External_Options) <= lend)
2895 Elf_Internal_Options intopt;
2897 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2899 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2901 Elf64_Internal_RegInfo intreg;
2903 bfd_mips_elf64_swap_reginfo_in
2905 ((Elf64_External_RegInfo *)
2906 (l + sizeof (Elf_External_Options))),
2908 elf_gp (abfd) = intreg.ri_gp_value;
2910 else if (intopt.kind == ODK_REGINFO)
2912 Elf32_RegInfo intreg;
2914 bfd_mips_elf32_swap_reginfo_in
2916 ((Elf32_External_RegInfo *)
2917 (l + sizeof (Elf_External_Options))),
2919 elf_gp (abfd) = intreg.ri_gp_value;
2929 /* Set the correct type for a MIPS ELF section. We do this by the
2930 section name, which is a hack, but ought to work. This routine is
2931 used by both the 32-bit and the 64-bit ABI. */
2934 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2936 Elf32_Internal_Shdr *hdr;
2939 register const char *name;
2941 name = bfd_get_section_name (abfd, sec);
2943 if (strcmp (name, ".liblist") == 0)
2945 hdr->sh_type = SHT_MIPS_LIBLIST;
2946 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2947 /* The sh_link field is set in final_write_processing. */
2949 else if (strcmp (name, ".conflict") == 0)
2950 hdr->sh_type = SHT_MIPS_CONFLICT;
2951 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2953 hdr->sh_type = SHT_MIPS_GPTAB;
2954 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2955 /* The sh_info field is set in final_write_processing. */
2957 else if (strcmp (name, ".ucode") == 0)
2958 hdr->sh_type = SHT_MIPS_UCODE;
2959 else if (strcmp (name, ".mdebug") == 0)
2961 hdr->sh_type = SHT_MIPS_DEBUG;
2962 /* In a shared object on Irix 5.3, the .mdebug section has an
2963 entsize of 0. FIXME: Does this matter? */
2964 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2965 hdr->sh_entsize = 0;
2967 hdr->sh_entsize = 1;
2969 else if (strcmp (name, ".reginfo") == 0)
2971 hdr->sh_type = SHT_MIPS_REGINFO;
2972 /* In a shared object on Irix 5.3, the .reginfo section has an
2973 entsize of 0x18. FIXME: Does this matter? */
2974 if (SGI_COMPAT (abfd))
2976 if ((abfd->flags & DYNAMIC) != 0)
2977 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2979 hdr->sh_entsize = 1;
2982 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2984 else if (SGI_COMPAT (abfd)
2985 && (strcmp (name, ".hash") == 0
2986 || strcmp (name, ".dynamic") == 0
2987 || strcmp (name, ".dynstr") == 0))
2989 if (SGI_COMPAT (abfd))
2990 hdr->sh_entsize = 0;
2992 /* This isn't how the Irix 6 linker behaves. */
2993 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2996 else if (strcmp (name, ".got") == 0
2997 || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0
2998 || strcmp (name, ".sdata") == 0
2999 || strcmp (name, ".sbss") == 0
3000 || strcmp (name, ".lit4") == 0
3001 || strcmp (name, ".lit8") == 0)
3002 hdr->sh_flags |= SHF_MIPS_GPREL;
3003 else if (strcmp (name, ".MIPS.interfaces") == 0)
3005 hdr->sh_type = SHT_MIPS_IFACE;
3006 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3008 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
3010 hdr->sh_type = SHT_MIPS_CONTENT;
3011 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3012 /* The sh_info field is set in final_write_processing. */
3014 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
3016 hdr->sh_type = SHT_MIPS_OPTIONS;
3017 hdr->sh_entsize = 1;
3018 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3020 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
3021 hdr->sh_type = SHT_MIPS_DWARF;
3022 else if (strcmp (name, ".MIPS.symlib") == 0)
3024 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
3025 /* The sh_link and sh_info fields are set in
3026 final_write_processing. */
3028 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
3029 || strncmp (name, ".MIPS.post_rel",
3030 sizeof ".MIPS.post_rel" - 1) == 0)
3032 hdr->sh_type = SHT_MIPS_EVENTS;
3033 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3034 /* The sh_link field is set in final_write_processing. */
3036 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
3038 hdr->sh_type = SHT_MIPS_MSYM;
3039 hdr->sh_flags |= SHF_ALLOC;
3040 hdr->sh_entsize = 8;
3043 /* The generic elf_fake_sections will set up REL_HDR using the
3044 default kind of relocations. But, we may actually need both
3045 kinds of relocations, so we set up the second header here. */
3046 if ((sec->flags & SEC_RELOC) != 0)
3048 struct bfd_elf_section_data *esd;
3049 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
3051 esd = elf_section_data (sec);
3052 BFD_ASSERT (esd->rel_hdr2 == NULL);
3053 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
3056 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
3057 !elf_section_data (sec)->use_rela_p);
3063 /* Given a BFD section, try to locate the corresponding ELF section
3064 index. This is used by both the 32-bit and the 64-bit ABI.
3065 Actually, it's not clear to me that the 64-bit ABI supports these,
3066 but for non-PIC objects we will certainly want support for at least
3067 the .scommon section. */
3070 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
3071 bfd *abfd ATTRIBUTE_UNUSED;
3072 Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
3076 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
3078 *retval = SHN_MIPS_SCOMMON;
3081 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
3083 *retval = SHN_MIPS_ACOMMON;
3089 /* When are writing out the .options or .MIPS.options section,
3090 remember the bytes we are writing out, so that we can install the
3091 GP value in the section_processing routine. */
3094 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
3099 bfd_size_type count;
3101 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
3105 if (elf_section_data (section) == NULL)
3107 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
3108 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
3109 if (elf_section_data (section) == NULL)
3112 c = (bfd_byte *) elf_section_data (section)->tdata;
3117 if (section->_cooked_size != 0)
3118 size = section->_cooked_size;
3120 size = section->_raw_size;
3121 c = (bfd_byte *) bfd_zalloc (abfd, size);
3124 elf_section_data (section)->tdata = (PTR) c;
3127 memcpy (c + offset, location, (size_t) count);
3130 return _bfd_elf_set_section_contents (abfd, section, location, offset,
3134 /* Work over a section just before writing it out. This routine is
3135 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
3136 sections that need the SHF_MIPS_GPREL flag by name; there has to be
3140 _bfd_mips_elf_section_processing (abfd, hdr)
3142 Elf_Internal_Shdr *hdr;
3144 if (hdr->sh_type == SHT_MIPS_REGINFO
3145 && hdr->sh_size > 0)
3149 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
3150 BFD_ASSERT (hdr->contents == NULL);
3153 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
3156 H_PUT_32 (abfd, elf_gp (abfd), buf);
3157 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
3161 if (hdr->sh_type == SHT_MIPS_OPTIONS
3162 && hdr->bfd_section != NULL
3163 && elf_section_data (hdr->bfd_section) != NULL
3164 && elf_section_data (hdr->bfd_section)->tdata != NULL)
3166 bfd_byte *contents, *l, *lend;
3168 /* We stored the section contents in the elf_section_data tdata
3169 field in the set_section_contents routine. We save the
3170 section contents so that we don't have to read them again.
3171 At this point we know that elf_gp is set, so we can look
3172 through the section contents to see if there is an
3173 ODK_REGINFO structure. */
3175 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
3177 lend = contents + hdr->sh_size;
3178 while (l + sizeof (Elf_External_Options) <= lend)
3180 Elf_Internal_Options intopt;
3182 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3184 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3191 + sizeof (Elf_External_Options)
3192 + (sizeof (Elf64_External_RegInfo) - 8)),
3195 H_PUT_64 (abfd, elf_gp (abfd), buf);
3196 if (bfd_bwrite (buf, (bfd_size_type) 8, abfd) != 8)
3199 else if (intopt.kind == ODK_REGINFO)
3206 + sizeof (Elf_External_Options)
3207 + (sizeof (Elf32_External_RegInfo) - 4)),
3210 H_PUT_32 (abfd, elf_gp (abfd), buf);
3211 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
3218 if (hdr->bfd_section != NULL)
3220 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3222 if (strcmp (name, ".sdata") == 0
3223 || strcmp (name, ".lit8") == 0
3224 || strcmp (name, ".lit4") == 0)
3226 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3227 hdr->sh_type = SHT_PROGBITS;
3229 else if (strcmp (name, ".sbss") == 0)
3231 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3232 hdr->sh_type = SHT_NOBITS;
3234 else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0)
3236 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3237 hdr->sh_type = SHT_PROGBITS;
3239 else if (strcmp (name, ".compact_rel") == 0)
3242 hdr->sh_type = SHT_PROGBITS;
3244 else if (strcmp (name, ".rtproc") == 0)
3246 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3248 unsigned int adjust;
3250 adjust = hdr->sh_size % hdr->sh_addralign;
3252 hdr->sh_size += hdr->sh_addralign - adjust;
3260 /* MIPS ELF uses two common sections. One is the usual one, and the
3261 other is for small objects. All the small objects are kept
3262 together, and then referenced via the gp pointer, which yields
3263 faster assembler code. This is what we use for the small common
3264 section. This approach is copied from ecoff.c. */
3265 static asection mips_elf_scom_section;
3266 static asymbol mips_elf_scom_symbol;
3267 static asymbol *mips_elf_scom_symbol_ptr;
3269 /* MIPS ELF also uses an acommon section, which represents an
3270 allocated common symbol which may be overridden by a
3271 definition in a shared library. */
3272 static asection mips_elf_acom_section;
3273 static asymbol mips_elf_acom_symbol;
3274 static asymbol *mips_elf_acom_symbol_ptr;
3276 /* Handle the special MIPS section numbers that a symbol may use.
3277 This is used for both the 32-bit and the 64-bit ABI. */
3280 _bfd_mips_elf_symbol_processing (abfd, asym)
3284 elf_symbol_type *elfsym;
3286 elfsym = (elf_symbol_type *) asym;
3287 switch (elfsym->internal_elf_sym.st_shndx)
3289 case SHN_MIPS_ACOMMON:
3290 /* This section is used in a dynamically linked executable file.
3291 It is an allocated common section. The dynamic linker can
3292 either resolve these symbols to something in a shared
3293 library, or it can just leave them here. For our purposes,
3294 we can consider these symbols to be in a new section. */
3295 if (mips_elf_acom_section.name == NULL)
3297 /* Initialize the acommon section. */
3298 mips_elf_acom_section.name = ".acommon";
3299 mips_elf_acom_section.flags = SEC_ALLOC;
3300 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3301 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3302 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3303 mips_elf_acom_symbol.name = ".acommon";
3304 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3305 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3306 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3308 asym->section = &mips_elf_acom_section;
3312 /* Common symbols less than the GP size are automatically
3313 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3314 if (asym->value > elf_gp_size (abfd)
3315 || IRIX_COMPAT (abfd) == ict_irix6)
3318 case SHN_MIPS_SCOMMON:
3319 if (mips_elf_scom_section.name == NULL)
3321 /* Initialize the small common section. */
3322 mips_elf_scom_section.name = ".scommon";
3323 mips_elf_scom_section.flags = SEC_IS_COMMON;
3324 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3325 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3326 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3327 mips_elf_scom_symbol.name = ".scommon";
3328 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3329 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3330 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3332 asym->section = &mips_elf_scom_section;
3333 asym->value = elfsym->internal_elf_sym.st_size;
3336 case SHN_MIPS_SUNDEFINED:
3337 asym->section = bfd_und_section_ptr;
3340 #if 0 /* for SGI_COMPAT */
3342 asym->section = mips_elf_text_section_ptr;
3346 asym->section = mips_elf_data_section_ptr;
3352 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
3356 _bfd_mips_elf_additional_program_headers (abfd)
3362 /* See if we need a PT_MIPS_REGINFO segment. */
3363 s = bfd_get_section_by_name (abfd, ".reginfo");
3364 if (s && (s->flags & SEC_LOAD))
3367 /* See if we need a PT_MIPS_OPTIONS segment. */
3368 if (IRIX_COMPAT (abfd) == ict_irix6
3369 && bfd_get_section_by_name (abfd,
3370 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
3373 /* See if we need a PT_MIPS_RTPROC segment. */
3374 if (IRIX_COMPAT (abfd) == ict_irix5
3375 && bfd_get_section_by_name (abfd, ".dynamic")
3376 && bfd_get_section_by_name (abfd, ".mdebug"))
3382 /* Modify the segment map for an Irix 5 executable. */
3385 _bfd_mips_elf_modify_segment_map (abfd)
3389 struct elf_segment_map *m, **pm;
3392 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
3394 s = bfd_get_section_by_name (abfd, ".reginfo");
3395 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3397 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3398 if (m->p_type == PT_MIPS_REGINFO)
3403 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3407 m->p_type = PT_MIPS_REGINFO;
3411 /* We want to put it after the PHDR and INTERP segments. */
3412 pm = &elf_tdata (abfd)->segment_map;
3414 && ((*pm)->p_type == PT_PHDR
3415 || (*pm)->p_type == PT_INTERP))
3423 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
3424 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
3425 PT_OPTIONS segement immediately following the program header
3427 if (IRIX_COMPAT (abfd) == ict_irix6)
3429 for (s = abfd->sections; s; s = s->next)
3430 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
3435 struct elf_segment_map *options_segment;
3437 /* Usually, there's a program header table. But, sometimes
3438 there's not (like when running the `ld' testsuite). So,
3439 if there's no program header table, we just put the
3440 options segement at the end. */
3441 for (pm = &elf_tdata (abfd)->segment_map;
3444 if ((*pm)->p_type == PT_PHDR)
3447 amt = sizeof (struct elf_segment_map);
3448 options_segment = bfd_zalloc (abfd, amt);
3449 options_segment->next = *pm;
3450 options_segment->p_type = PT_MIPS_OPTIONS;
3451 options_segment->p_flags = PF_R;
3452 options_segment->p_flags_valid = true;
3453 options_segment->count = 1;
3454 options_segment->sections[0] = s;
3455 *pm = options_segment;
3460 if (IRIX_COMPAT (abfd) == ict_irix5)
3462 /* If there are .dynamic and .mdebug sections, we make a room
3463 for the RTPROC header. FIXME: Rewrite without section names. */
3464 if (bfd_get_section_by_name (abfd, ".interp") == NULL
3465 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
3466 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
3468 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3469 if (m->p_type == PT_MIPS_RTPROC)
3474 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3478 m->p_type = PT_MIPS_RTPROC;
3480 s = bfd_get_section_by_name (abfd, ".rtproc");
3485 m->p_flags_valid = 1;
3493 /* We want to put it after the DYNAMIC segment. */
3494 pm = &elf_tdata (abfd)->segment_map;
3495 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3505 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic,
3506 .dynstr, .dynsym, and .hash sections, and everything in
3508 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
3510 if ((*pm)->p_type == PT_DYNAMIC)
3513 if (IRIX_COMPAT (abfd) == ict_none)
3515 /* For a normal mips executable the permissions for the PT_DYNAMIC
3516 segment are read, write and execute. We do that here since
3517 the code in elf.c sets only the read permission. This matters
3518 sometimes for the dynamic linker. */
3519 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3521 m->p_flags = PF_R | PF_W | PF_X;
3522 m->p_flags_valid = 1;
3526 && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3528 static const char *sec_names[] =
3530 ".dynamic", ".dynstr", ".dynsym", ".hash"
3534 struct elf_segment_map *n;
3538 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3540 s = bfd_get_section_by_name (abfd, sec_names[i]);
3541 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3547 sz = s->_cooked_size;
3550 if (high < s->vma + sz)
3556 for (s = abfd->sections; s != NULL; s = s->next)
3557 if ((s->flags & SEC_LOAD) != 0
3560 + (s->_cooked_size !=
3561 0 ? s->_cooked_size : s->_raw_size)) <= high))
3564 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
3565 n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3572 for (s = abfd->sections; s != NULL; s = s->next)
3574 if ((s->flags & SEC_LOAD) != 0
3577 + (s->_cooked_size != 0 ?
3578 s->_cooked_size : s->_raw_size)) <= high))
3592 /* The structure of the runtime procedure descriptor created by the
3593 loader for use by the static exception system. */
3595 typedef struct runtime_pdr {
3596 bfd_vma adr; /* memory address of start of procedure */
3597 long regmask; /* save register mask */
3598 long regoffset; /* save register offset */
3599 long fregmask; /* save floating point register mask */
3600 long fregoffset; /* save floating point register offset */
3601 long frameoffset; /* frame size */
3602 short framereg; /* frame pointer register */
3603 short pcreg; /* offset or reg of return pc */
3604 long irpss; /* index into the runtime string table */
3606 struct exception_info *exception_info;/* pointer to exception array */
3608 #define cbRPDR sizeof (RPDR)
3609 #define rpdNil ((pRPDR) 0)
3611 /* Swap RPDR (runtime procedure table entry) for output. */
3613 static void ecoff_swap_rpdr_out
3614 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3617 ecoff_swap_rpdr_out (abfd, in, ex)
3620 struct rpdr_ext *ex;
3622 /* ECOFF_PUT_OFF was defined in ecoffswap.h. */
3623 ECOFF_PUT_OFF (abfd, in->adr, ex->p_adr);
3624 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
3625 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
3626 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
3627 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
3628 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
3630 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
3631 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
3633 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
3635 ECOFF_PUT_OFF (abfd, in->exception_info, ex->p_exception_info);
3639 /* Read ECOFF debugging information from a .mdebug section into a
3640 ecoff_debug_info structure. */
3643 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3646 struct ecoff_debug_info *debug;
3649 const struct ecoff_debug_swap *swap;
3650 char *ext_hdr = NULL;
3652 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3653 memset (debug, 0, sizeof (*debug));
3655 ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
3656 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3659 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3660 swap->external_hdr_size)
3664 symhdr = &debug->symbolic_header;
3665 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3667 /* The symbolic header contains absolute file offsets and sizes to
3669 #define READ(ptr, offset, count, size, type) \
3670 if (symhdr->count == 0) \
3671 debug->ptr = NULL; \
3674 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
3675 debug->ptr = (type) bfd_malloc (amt); \
3676 if (debug->ptr == NULL) \
3677 goto error_return; \
3678 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3679 || bfd_bread (debug->ptr, amt, abfd) != amt) \
3680 goto error_return; \
3683 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3684 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3685 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3686 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3687 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3688 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3690 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3691 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3692 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3693 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3694 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3698 debug->adjust = NULL;
3703 if (ext_hdr != NULL)
3705 if (debug->line != NULL)
3707 if (debug->external_dnr != NULL)
3708 free (debug->external_dnr);
3709 if (debug->external_pdr != NULL)
3710 free (debug->external_pdr);
3711 if (debug->external_sym != NULL)
3712 free (debug->external_sym);
3713 if (debug->external_opt != NULL)
3714 free (debug->external_opt);
3715 if (debug->external_aux != NULL)
3716 free (debug->external_aux);
3717 if (debug->ss != NULL)
3719 if (debug->ssext != NULL)
3720 free (debug->ssext);
3721 if (debug->external_fdr != NULL)
3722 free (debug->external_fdr);
3723 if (debug->external_rfd != NULL)
3724 free (debug->external_rfd);
3725 if (debug->external_ext != NULL)
3726 free (debug->external_ext);
3730 /* MIPS ELF local labels start with '$', not 'L'. */
3733 mips_elf_is_local_label_name (abfd, name)
3740 /* On Irix 6, the labels go back to starting with '.', so we accept
3741 the generic ELF local label syntax as well. */
3742 return _bfd_elf_is_local_label_name (abfd, name);
3745 /* MIPS ELF uses a special find_nearest_line routine in order the
3746 handle the ECOFF debugging information. */
3748 struct mips_elf_find_line
3750 struct ecoff_debug_info d;
3751 struct ecoff_find_line i;
3755 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3756 functionname_ptr, line_ptr)
3761 const char **filename_ptr;
3762 const char **functionname_ptr;
3763 unsigned int *line_ptr;
3767 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3768 filename_ptr, functionname_ptr,
3772 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3773 filename_ptr, functionname_ptr,
3775 (unsigned) (ABI_64_P (abfd) ? 8 : 0),
3776 &elf_tdata (abfd)->dwarf2_find_line_info))
3779 msec = bfd_get_section_by_name (abfd, ".mdebug");
3783 struct mips_elf_find_line *fi;
3784 const struct ecoff_debug_swap * const swap =
3785 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3787 /* If we are called during a link, mips_elf_final_link may have
3788 cleared the SEC_HAS_CONTENTS field. We force it back on here
3789 if appropriate (which it normally will be). */
3790 origflags = msec->flags;
3791 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3792 msec->flags |= SEC_HAS_CONTENTS;
3794 fi = elf_tdata (abfd)->find_line_info;
3797 bfd_size_type external_fdr_size;
3800 struct fdr *fdr_ptr;
3801 bfd_size_type amt = sizeof (struct mips_elf_find_line);
3803 fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
3806 msec->flags = origflags;
3810 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3812 msec->flags = origflags;
3816 /* Swap in the FDR information. */
3817 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
3818 fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
3819 if (fi->d.fdr == NULL)
3821 msec->flags = origflags;
3824 external_fdr_size = swap->external_fdr_size;
3825 fdr_ptr = fi->d.fdr;
3826 fraw_src = (char *) fi->d.external_fdr;
3827 fraw_end = (fraw_src
3828 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3829 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3830 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3832 elf_tdata (abfd)->find_line_info = fi;
3834 /* Note that we don't bother to ever free this information.
3835 find_nearest_line is either called all the time, as in
3836 objdump -l, so the information should be saved, or it is
3837 rarely called, as in ld error messages, so the memory
3838 wasted is unimportant. Still, it would probably be a
3839 good idea for free_cached_info to throw it away. */
3842 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3843 &fi->i, filename_ptr, functionname_ptr,
3846 msec->flags = origflags;
3850 msec->flags = origflags;
3853 /* Fall back on the generic ELF find_nearest_line routine. */
3855 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3856 filename_ptr, functionname_ptr,
3860 /* The mips16 compiler uses a couple of special sections to handle
3861 floating point arguments.
3863 Section names that look like .mips16.fn.FNNAME contain stubs that
3864 copy floating point arguments from the fp regs to the gp regs and
3865 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3866 call should be redirected to the stub instead. If no 32 bit
3867 function calls FNNAME, the stub should be discarded. We need to
3868 consider any reference to the function, not just a call, because
3869 if the address of the function is taken we will need the stub,
3870 since the address might be passed to a 32 bit function.
3872 Section names that look like .mips16.call.FNNAME contain stubs
3873 that copy floating point arguments from the gp regs to the fp
3874 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3875 then any 16 bit function that calls FNNAME should be redirected
3876 to the stub instead. If FNNAME is not a 32 bit function, the
3877 stub should be discarded.
3879 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3880 which call FNNAME and then copy the return value from the fp regs
3881 to the gp regs. These stubs store the return value in $18 while
3882 calling FNNAME; any function which might call one of these stubs
3883 must arrange to save $18 around the call. (This case is not
3884 needed for 32 bit functions that call 16 bit functions, because
3885 16 bit functions always return floating point values in both
3888 Note that in all cases FNNAME might be defined statically.
3889 Therefore, FNNAME is not used literally. Instead, the relocation
3890 information will indicate which symbol the section is for.
3892 We record any stubs that we find in the symbol table. */
3894 #define FN_STUB ".mips16.fn."
3895 #define CALL_STUB ".mips16.call."
3896 #define CALL_FP_STUB ".mips16.call.fp."
3898 /* MIPS ELF linker hash table. */
3900 struct mips_elf_link_hash_table
3902 struct elf_link_hash_table root;
3904 /* We no longer use this. */
3905 /* String section indices for the dynamic section symbols. */
3906 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3908 /* The number of .rtproc entries. */
3909 bfd_size_type procedure_count;
3910 /* The size of the .compact_rel section (if SGI_COMPAT). */
3911 bfd_size_type compact_rel_size;
3912 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3913 entry is set to the address of __rld_obj_head as in Irix 5. */
3914 boolean use_rld_obj_head;
3915 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3917 /* This is set if we see any mips16 stub sections. */
3918 boolean mips16_stubs_seen;
3921 /* Look up an entry in a MIPS ELF linker hash table. */
3923 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3924 ((struct mips_elf_link_hash_entry *) \
3925 elf_link_hash_lookup (&(table)->root, (string), (create), \
3928 /* Traverse a MIPS ELF linker hash table. */
3930 #define mips_elf_link_hash_traverse(table, func, info) \
3931 (elf_link_hash_traverse \
3933 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3936 /* Get the MIPS ELF linker hash table from a link_info structure. */
3938 #define mips_elf_hash_table(p) \
3939 ((struct mips_elf_link_hash_table *) ((p)->hash))
3941 static boolean mips_elf_output_extsym
3942 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3944 /* Create an entry in a MIPS ELF linker hash table. */
3946 static struct bfd_hash_entry *
3947 mips_elf_link_hash_newfunc (entry, table, string)
3948 struct bfd_hash_entry *entry;
3949 struct bfd_hash_table *table;
3952 struct mips_elf_link_hash_entry *ret =
3953 (struct mips_elf_link_hash_entry *) entry;
3955 /* Allocate the structure if it has not already been allocated by a
3957 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3958 ret = ((struct mips_elf_link_hash_entry *)
3959 bfd_hash_allocate (table,
3960 sizeof (struct mips_elf_link_hash_entry)));
3961 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3962 return (struct bfd_hash_entry *) ret;
3964 /* Call the allocation method of the superclass. */
3965 ret = ((struct mips_elf_link_hash_entry *)
3966 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3968 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3970 /* Set local fields. */
3971 memset (&ret->esym, 0, sizeof (EXTR));
3972 /* We use -2 as a marker to indicate that the information has
3973 not been set. -1 means there is no associated ifd. */
3975 ret->possibly_dynamic_relocs = 0;
3976 ret->readonly_reloc = false;
3977 ret->min_dyn_reloc_index = 0;
3978 ret->no_fn_stub = false;
3979 ret->fn_stub = NULL;
3980 ret->need_fn_stub = false;
3981 ret->call_stub = NULL;
3982 ret->call_fp_stub = NULL;
3985 return (struct bfd_hash_entry *) ret;
3989 _bfd_mips_elf_hide_symbol (info, entry)
3990 struct bfd_link_info *info;
3991 struct elf_link_hash_entry *entry;
3995 struct mips_got_info *g;
3996 struct mips_elf_link_hash_entry *h;
3997 h = (struct mips_elf_link_hash_entry *) entry;
3998 dynobj = elf_hash_table (info)->dynobj;
3999 got = bfd_get_section_by_name (dynobj, ".got");
4000 g = (struct mips_got_info *) elf_section_data (got)->tdata;
4002 h->root.elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4003 h->root.plt.offset = (bfd_vma) -1;
4004 if ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
4005 h->root.dynindx = -1;
4007 /* FIXME: Do we allocate too much GOT space here? */
4009 got->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
4012 /* Create a MIPS ELF linker hash table. */
4014 struct bfd_link_hash_table *
4015 _bfd_mips_elf_link_hash_table_create (abfd)
4018 struct mips_elf_link_hash_table *ret;
4019 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
4021 ret = (struct mips_elf_link_hash_table *) bfd_alloc (abfd, amt);
4022 if (ret == (struct mips_elf_link_hash_table *) NULL)
4025 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
4026 mips_elf_link_hash_newfunc))
4028 bfd_release (abfd, ret);
4033 /* We no longer use this. */
4034 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
4035 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
4037 ret->procedure_count = 0;
4038 ret->compact_rel_size = 0;
4039 ret->use_rld_obj_head = false;
4041 ret->mips16_stubs_seen = false;
4043 return &ret->root.root;
4046 /* Hook called by the linker routine which adds symbols from an object
4047 file. We must handle the special MIPS section numbers here. */
4050 _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
4052 struct bfd_link_info *info;
4053 const Elf_Internal_Sym *sym;
4055 flagword *flagsp ATTRIBUTE_UNUSED;
4059 if (SGI_COMPAT (abfd)
4060 && (abfd->flags & DYNAMIC) != 0
4061 && strcmp (*namep, "_rld_new_interface") == 0)
4063 /* Skip Irix 5 rld entry name. */
4068 switch (sym->st_shndx)
4071 /* Common symbols less than the GP size are automatically
4072 treated as SHN_MIPS_SCOMMON symbols. */
4073 if (sym->st_size > elf_gp_size (abfd)
4074 || IRIX_COMPAT (abfd) == ict_irix6)
4077 case SHN_MIPS_SCOMMON:
4078 *secp = bfd_make_section_old_way (abfd, ".scommon");
4079 (*secp)->flags |= SEC_IS_COMMON;
4080 *valp = sym->st_size;
4084 /* This section is used in a shared object. */
4085 if (elf_tdata (abfd)->elf_text_section == NULL)
4087 asymbol *elf_text_symbol;
4088 asection *elf_text_section;
4089 bfd_size_type amt = sizeof (asection);
4091 elf_text_section = bfd_zalloc (abfd, amt);
4092 if (elf_text_section == NULL)
4095 amt = sizeof (asymbol);
4096 elf_text_symbol = bfd_zalloc (abfd, amt);
4097 if (elf_text_symbol == NULL)
4100 /* Initialize the section. */
4102 elf_tdata (abfd)->elf_text_section = elf_text_section;
4103 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
4105 elf_text_section->symbol = elf_text_symbol;
4106 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
4108 elf_text_section->name = ".text";
4109 elf_text_section->flags = SEC_NO_FLAGS;
4110 elf_text_section->output_section = NULL;
4111 elf_text_section->owner = abfd;
4112 elf_text_symbol->name = ".text";
4113 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
4114 elf_text_symbol->section = elf_text_section;
4116 /* This code used to do *secp = bfd_und_section_ptr if
4117 info->shared. I don't know why, and that doesn't make sense,
4118 so I took it out. */
4119 *secp = elf_tdata (abfd)->elf_text_section;
4122 case SHN_MIPS_ACOMMON:
4123 /* Fall through. XXX Can we treat this as allocated data? */
4125 /* This section is used in a shared object. */
4126 if (elf_tdata (abfd)->elf_data_section == NULL)
4128 asymbol *elf_data_symbol;
4129 asection *elf_data_section;
4130 bfd_size_type amt = sizeof (asection);
4132 elf_data_section = bfd_zalloc (abfd, amt);
4133 if (elf_data_section == NULL)
4136 amt = sizeof (asymbol);
4137 elf_data_symbol = bfd_zalloc (abfd, amt);
4138 if (elf_data_symbol == NULL)
4141 /* Initialize the section. */
4143 elf_tdata (abfd)->elf_data_section = elf_data_section;
4144 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
4146 elf_data_section->symbol = elf_data_symbol;
4147 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
4149 elf_data_section->name = ".data";
4150 elf_data_section->flags = SEC_NO_FLAGS;
4151 elf_data_section->output_section = NULL;
4152 elf_data_section->owner = abfd;
4153 elf_data_symbol->name = ".data";
4154 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
4155 elf_data_symbol->section = elf_data_section;
4157 /* This code used to do *secp = bfd_und_section_ptr if
4158 info->shared. I don't know why, and that doesn't make sense,
4159 so I took it out. */
4160 *secp = elf_tdata (abfd)->elf_data_section;
4163 case SHN_MIPS_SUNDEFINED:
4164 *secp = bfd_und_section_ptr;
4168 if (SGI_COMPAT (abfd)
4170 && info->hash->creator == abfd->xvec
4171 && strcmp (*namep, "__rld_obj_head") == 0)
4173 struct elf_link_hash_entry *h;
4175 /* Mark __rld_obj_head as dynamic. */
4177 if (! (_bfd_generic_link_add_one_symbol
4178 (info, abfd, *namep, BSF_GLOBAL, *secp,
4179 (bfd_vma) *valp, (const char *) NULL, false,
4180 get_elf_backend_data (abfd)->collect,
4181 (struct bfd_link_hash_entry **) &h)))
4183 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4184 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4185 h->type = STT_OBJECT;
4187 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4190 mips_elf_hash_table (info)->use_rld_obj_head = true;
4193 /* If this is a mips16 text symbol, add 1 to the value to make it
4194 odd. This will cause something like .word SYM to come up with
4195 the right value when it is loaded into the PC. */
4196 if (sym->st_other == STO_MIPS16)
4202 /* Structure used to pass information to mips_elf_output_extsym. */
4207 struct bfd_link_info *info;
4208 struct ecoff_debug_info *debug;
4209 const struct ecoff_debug_swap *swap;
4213 /* This routine is used to write out ECOFF debugging external symbol
4214 information. It is called via mips_elf_link_hash_traverse. The
4215 ECOFF external symbol information must match the ELF external
4216 symbol information. Unfortunately, at this point we don't know
4217 whether a symbol is required by reloc information, so the two
4218 tables may wind up being different. We must sort out the external
4219 symbol information before we can set the final size of the .mdebug
4220 section, and we must set the size of the .mdebug section before we
4221 can relocate any sections, and we can't know which symbols are
4222 required by relocation until we relocate the sections.
4223 Fortunately, it is relatively unlikely that any symbol will be
4224 stripped but required by a reloc. In particular, it can not happen
4225 when generating a final executable. */
4228 mips_elf_output_extsym (h, data)
4229 struct mips_elf_link_hash_entry *h;
4232 struct extsym_info *einfo = (struct extsym_info *) data;
4234 asection *sec, *output_section;
4236 if (h->root.indx == -2)
4238 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4239 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
4240 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4241 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4243 else if (einfo->info->strip == strip_all
4244 || (einfo->info->strip == strip_some
4245 && bfd_hash_lookup (einfo->info->keep_hash,
4246 h->root.root.root.string,
4247 false, false) == NULL))
4255 if (h->esym.ifd == -2)
4258 h->esym.cobol_main = 0;
4259 h->esym.weakext = 0;
4260 h->esym.reserved = 0;
4261 h->esym.ifd = ifdNil;
4262 h->esym.asym.value = 0;
4263 h->esym.asym.st = stGlobal;
4265 if (h->root.root.type == bfd_link_hash_undefined
4266 || h->root.root.type == bfd_link_hash_undefweak)
4270 /* Use undefined class. Also, set class and type for some
4272 name = h->root.root.root.string;
4273 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
4274 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
4276 h->esym.asym.sc = scData;
4277 h->esym.asym.st = stLabel;
4278 h->esym.asym.value = 0;
4280 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
4282 h->esym.asym.sc = scAbs;
4283 h->esym.asym.st = stLabel;
4284 h->esym.asym.value =
4285 mips_elf_hash_table (einfo->info)->procedure_count;
4287 else if (strcmp (name, "_gp_disp") == 0)
4289 h->esym.asym.sc = scAbs;
4290 h->esym.asym.st = stLabel;
4291 h->esym.asym.value = elf_gp (einfo->abfd);
4294 h->esym.asym.sc = scUndefined;
4296 else if (h->root.root.type != bfd_link_hash_defined
4297 && h->root.root.type != bfd_link_hash_defweak)
4298 h->esym.asym.sc = scAbs;
4303 sec = h->root.root.u.def.section;
4304 output_section = sec->output_section;
4306 /* When making a shared library and symbol h is the one from
4307 the another shared library, OUTPUT_SECTION may be null. */
4308 if (output_section == NULL)
4309 h->esym.asym.sc = scUndefined;
4312 name = bfd_section_name (output_section->owner, output_section);
4314 if (strcmp (name, ".text") == 0)
4315 h->esym.asym.sc = scText;
4316 else if (strcmp (name, ".data") == 0)
4317 h->esym.asym.sc = scData;
4318 else if (strcmp (name, ".sdata") == 0)
4319 h->esym.asym.sc = scSData;
4320 else if (strcmp (name, ".rodata") == 0
4321 || strcmp (name, ".rdata") == 0)
4322 h->esym.asym.sc = scRData;
4323 else if (strcmp (name, ".bss") == 0)
4324 h->esym.asym.sc = scBss;
4325 else if (strcmp (name, ".sbss") == 0)
4326 h->esym.asym.sc = scSBss;
4327 else if (strcmp (name, ".init") == 0)
4328 h->esym.asym.sc = scInit;
4329 else if (strcmp (name, ".fini") == 0)
4330 h->esym.asym.sc = scFini;
4332 h->esym.asym.sc = scAbs;
4336 h->esym.asym.reserved = 0;
4337 h->esym.asym.index = indexNil;
4340 if (h->root.root.type == bfd_link_hash_common)
4341 h->esym.asym.value = h->root.root.u.c.size;
4342 else if (h->root.root.type == bfd_link_hash_defined
4343 || h->root.root.type == bfd_link_hash_defweak)
4345 if (h->esym.asym.sc == scCommon)
4346 h->esym.asym.sc = scBss;
4347 else if (h->esym.asym.sc == scSCommon)
4348 h->esym.asym.sc = scSBss;
4350 sec = h->root.root.u.def.section;
4351 output_section = sec->output_section;
4352 if (output_section != NULL)
4353 h->esym.asym.value = (h->root.root.u.def.value
4354 + sec->output_offset
4355 + output_section->vma);
4357 h->esym.asym.value = 0;
4359 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4361 struct mips_elf_link_hash_entry *hd = h;
4362 boolean no_fn_stub = h->no_fn_stub;
4364 while (hd->root.root.type == bfd_link_hash_indirect)
4366 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
4367 no_fn_stub = no_fn_stub || hd->no_fn_stub;
4372 /* Set type and value for a symbol with a function stub. */
4373 h->esym.asym.st = stProc;
4374 sec = hd->root.root.u.def.section;
4376 h->esym.asym.value = 0;
4379 output_section = sec->output_section;
4380 if (output_section != NULL)
4381 h->esym.asym.value = (hd->root.plt.offset
4382 + sec->output_offset
4383 + output_section->vma);
4385 h->esym.asym.value = 0;
4393 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
4394 h->root.root.root.string,
4397 einfo->failed = true;
4404 /* Create a runtime procedure table from the .mdebug section. */
4407 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
4410 struct bfd_link_info *info;
4412 struct ecoff_debug_info *debug;
4414 const struct ecoff_debug_swap *swap;
4415 HDRR *hdr = &debug->symbolic_header;
4417 struct rpdr_ext *erp;
4419 struct pdr_ext *epdr;
4420 struct sym_ext *esym;
4424 bfd_size_type count;
4425 unsigned long sindex;
4429 const char *no_name_func = _("static procedure (no name)");
4437 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4439 sindex = strlen (no_name_func) + 1;
4440 count = hdr->ipdMax;
4443 size = swap->external_pdr_size;
4445 epdr = (struct pdr_ext *) bfd_malloc (size * count);
4449 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
4452 size = sizeof (RPDR);
4453 rp = rpdr = (RPDR *) bfd_malloc (size * count);
4457 size = sizeof (char *);
4458 sv = (char **) bfd_malloc (size * count);
4462 count = hdr->isymMax;
4463 size = swap->external_sym_size;
4464 esym = (struct sym_ext *) bfd_malloc (size * count);
4468 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
4471 count = hdr->issMax;
4472 ss = (char *) bfd_malloc (count);
4475 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
4478 count = hdr->ipdMax;
4479 for (i = 0; i < (unsigned long) count; i++, rp++)
4481 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
4482 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
4483 rp->adr = sym.value;
4484 rp->regmask = pdr.regmask;
4485 rp->regoffset = pdr.regoffset;
4486 rp->fregmask = pdr.fregmask;
4487 rp->fregoffset = pdr.fregoffset;
4488 rp->frameoffset = pdr.frameoffset;
4489 rp->framereg = pdr.framereg;
4490 rp->pcreg = pdr.pcreg;
4492 sv[i] = ss + sym.iss;
4493 sindex += strlen (sv[i]) + 1;
4497 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
4498 size = BFD_ALIGN (size, 16);
4499 rtproc = (PTR) bfd_alloc (abfd, size);
4502 mips_elf_hash_table (info)->procedure_count = 0;
4506 mips_elf_hash_table (info)->procedure_count = count + 2;
4508 erp = (struct rpdr_ext *) rtproc;
4509 memset (erp, 0, sizeof (struct rpdr_ext));
4511 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
4512 strcpy (str, no_name_func);
4513 str += strlen (no_name_func) + 1;
4514 for (i = 0; i < count; i++)
4516 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
4517 strcpy (str, sv[i]);
4518 str += strlen (sv[i]) + 1;
4520 ECOFF_PUT_OFF (abfd, -1, (erp + count)->p_adr);
4522 /* Set the size and contents of .rtproc section. */
4523 s->_raw_size = size;
4524 s->contents = (bfd_byte *) rtproc;
4526 /* Skip this section later on (I don't think this currently
4527 matters, but someday it might). */
4528 s->link_order_head = (struct bfd_link_order *) NULL;
4557 /* A comparison routine used to sort .gptab entries. */
4560 gptab_compare (p1, p2)
4564 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4565 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4567 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4570 /* We need to use a special link routine to handle the .reginfo and
4571 the .mdebug sections. We need to merge all instances of these
4572 sections together, not write them all out sequentially. */
4575 _bfd_mips_elf_final_link (abfd, info)
4577 struct bfd_link_info *info;
4581 struct bfd_link_order *p;
4582 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4583 asection *rtproc_sec;
4584 Elf32_RegInfo reginfo;
4585 struct ecoff_debug_info debug;
4586 const struct ecoff_debug_swap *swap
4587 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4588 HDRR *symhdr = &debug.symbolic_header;
4589 PTR mdebug_handle = NULL;
4595 static const char * const secname[] =
4597 ".text", ".init", ".fini", ".data",
4598 ".rodata", ".sdata", ".sbss", ".bss"
4600 static const int sc[] =
4602 scText, scInit, scFini, scData,
4603 scRData, scSData, scSBss, scBss
4606 /* If all the things we linked together were PIC, but we're
4607 producing an executable (rather than a shared object), then the
4608 resulting file is CPIC (i.e., it calls PIC code.) */
4610 && !info->relocateable
4611 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
4613 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
4614 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
4617 /* We'd carefully arranged the dynamic symbol indices, and then the
4618 generic size_dynamic_sections renumbered them out from under us.
4619 Rather than trying somehow to prevent the renumbering, just do
4621 if (elf_hash_table (info)->dynamic_sections_created)
4625 struct mips_got_info *g;
4627 /* When we resort, we must tell mips_elf_sort_hash_table what
4628 the lowest index it may use is. That's the number of section
4629 symbols we're going to add. The generic ELF linker only
4630 adds these symbols when building a shared object. Note that
4631 we count the sections after (possibly) removing the .options
4633 if (!mips_elf_sort_hash_table (info, (info->shared
4634 ? bfd_count_sections (abfd) + 1
4638 /* Make sure we didn't grow the global .got region. */
4639 dynobj = elf_hash_table (info)->dynobj;
4640 got = bfd_get_section_by_name (dynobj, ".got");
4641 g = (struct mips_got_info *) elf_section_data (got)->tdata;
4643 if (g->global_gotsym != NULL)
4644 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
4645 - g->global_gotsym->dynindx)
4646 <= g->global_gotno);
4649 /* On IRIX5, we omit the .options section. On IRIX6, however, we
4650 include it, even though we don't process it quite right. (Some
4651 entries are supposed to be merged.) Empirically, we seem to be
4652 better off including it then not. */
4653 if (IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
4654 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4656 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4658 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4659 if (p->type == bfd_indirect_link_order)
4660 p->u.indirect.section->flags &= ~SEC_HAS_CONTENTS;
4661 (*secpp)->link_order_head = NULL;
4662 *secpp = (*secpp)->next;
4663 --abfd->section_count;
4669 /* Get a value for the GP register. */
4670 if (elf_gp (abfd) == 0)
4672 struct bfd_link_hash_entry *h;
4674 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4675 if (h != (struct bfd_link_hash_entry *) NULL
4676 && h->type == bfd_link_hash_defined)
4677 elf_gp (abfd) = (h->u.def.value
4678 + h->u.def.section->output_section->vma
4679 + h->u.def.section->output_offset);
4680 else if (info->relocateable)
4684 /* Find the GP-relative section with the lowest offset. */
4686 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4688 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
4691 /* And calculate GP relative to that. */
4692 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4696 /* If the relocate_section function needs to do a reloc
4697 involving the GP value, it should make a reloc_dangerous
4698 callback to warn that GP is not defined. */
4702 /* Go through the sections and collect the .reginfo and .mdebug
4706 gptab_data_sec = NULL;
4707 gptab_bss_sec = NULL;
4708 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4710 if (strcmp (o->name, ".reginfo") == 0)
4712 memset (®info, 0, sizeof reginfo);
4714 /* We have found the .reginfo section in the output file.
4715 Look through all the link_orders comprising it and merge
4716 the information together. */
4717 for (p = o->link_order_head;
4718 p != (struct bfd_link_order *) NULL;
4721 asection *input_section;
4723 Elf32_External_RegInfo ext;
4726 if (p->type != bfd_indirect_link_order)
4728 if (p->type == bfd_fill_link_order)
4733 input_section = p->u.indirect.section;
4734 input_bfd = input_section->owner;
4736 /* The linker emulation code has probably clobbered the
4737 size to be zero bytes. */
4738 if (input_section->_raw_size == 0)
4739 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4741 if (! bfd_get_section_contents (input_bfd, input_section,
4744 (bfd_size_type) sizeof ext))
4747 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4749 reginfo.ri_gprmask |= sub.ri_gprmask;
4750 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4751 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4752 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4753 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4755 /* ri_gp_value is set by the function
4756 mips_elf32_section_processing when the section is
4757 finally written out. */
4759 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4760 elf_link_input_bfd ignores this section. */
4761 input_section->flags &= ~SEC_HAS_CONTENTS;
4764 /* Size has been set in mips_elf_always_size_sections */
4765 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4767 /* Skip this section later on (I don't think this currently
4768 matters, but someday it might). */
4769 o->link_order_head = (struct bfd_link_order *) NULL;
4774 if (strcmp (o->name, ".mdebug") == 0)
4776 struct extsym_info einfo;
4779 /* We have found the .mdebug section in the output file.
4780 Look through all the link_orders comprising it and merge
4781 the information together. */
4782 symhdr->magic = swap->sym_magic;
4783 /* FIXME: What should the version stamp be? */
4785 symhdr->ilineMax = 0;
4789 symhdr->isymMax = 0;
4790 symhdr->ioptMax = 0;
4791 symhdr->iauxMax = 0;
4793 symhdr->issExtMax = 0;
4796 symhdr->iextMax = 0;
4798 /* We accumulate the debugging information itself in the
4799 debug_info structure. */
4801 debug.external_dnr = NULL;
4802 debug.external_pdr = NULL;
4803 debug.external_sym = NULL;
4804 debug.external_opt = NULL;
4805 debug.external_aux = NULL;
4807 debug.ssext = debug.ssext_end = NULL;
4808 debug.external_fdr = NULL;
4809 debug.external_rfd = NULL;
4810 debug.external_ext = debug.external_ext_end = NULL;
4812 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4813 if (mdebug_handle == (PTR) NULL)
4817 esym.cobol_main = 0;
4821 esym.asym.iss = issNil;
4822 esym.asym.st = stLocal;
4823 esym.asym.reserved = 0;
4824 esym.asym.index = indexNil;
4826 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
4828 esym.asym.sc = sc[i];
4829 s = bfd_get_section_by_name (abfd, secname[i]);
4832 esym.asym.value = s->vma;
4833 last = s->vma + s->_raw_size;
4836 esym.asym.value = last;
4837 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
4842 for (p = o->link_order_head;
4843 p != (struct bfd_link_order *) NULL;
4846 asection *input_section;
4848 const struct ecoff_debug_swap *input_swap;
4849 struct ecoff_debug_info input_debug;
4853 if (p->type != bfd_indirect_link_order)
4855 if (p->type == bfd_fill_link_order)
4860 input_section = p->u.indirect.section;
4861 input_bfd = input_section->owner;
4863 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4864 || (get_elf_backend_data (input_bfd)
4865 ->elf_backend_ecoff_debug_swap) == NULL)
4867 /* I don't know what a non MIPS ELF bfd would be
4868 doing with a .mdebug section, but I don't really
4869 want to deal with it. */
4873 input_swap = (get_elf_backend_data (input_bfd)
4874 ->elf_backend_ecoff_debug_swap);
4876 BFD_ASSERT (p->size == input_section->_raw_size);
4878 /* The ECOFF linking code expects that we have already
4879 read in the debugging information and set up an
4880 ecoff_debug_info structure, so we do that now. */
4881 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4885 if (! (bfd_ecoff_debug_accumulate
4886 (mdebug_handle, abfd, &debug, swap, input_bfd,
4887 &input_debug, input_swap, info)))
4890 /* Loop through the external symbols. For each one with
4891 interesting information, try to find the symbol in
4892 the linker global hash table and save the information
4893 for the output external symbols. */
4894 eraw_src = input_debug.external_ext;
4895 eraw_end = (eraw_src
4896 + (input_debug.symbolic_header.iextMax
4897 * input_swap->external_ext_size));
4899 eraw_src < eraw_end;
4900 eraw_src += input_swap->external_ext_size)
4904 struct mips_elf_link_hash_entry *h;
4906 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4907 if (ext.asym.sc == scNil
4908 || ext.asym.sc == scUndefined
4909 || ext.asym.sc == scSUndefined)
4912 name = input_debug.ssext + ext.asym.iss;
4913 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4914 name, false, false, true);
4915 if (h == NULL || h->esym.ifd != -2)
4921 < input_debug.symbolic_header.ifdMax);
4922 ext.ifd = input_debug.ifdmap[ext.ifd];
4928 /* Free up the information we just read. */
4929 free (input_debug.line);
4930 free (input_debug.external_dnr);
4931 free (input_debug.external_pdr);
4932 free (input_debug.external_sym);
4933 free (input_debug.external_opt);
4934 free (input_debug.external_aux);
4935 free (input_debug.ss);
4936 free (input_debug.ssext);
4937 free (input_debug.external_fdr);
4938 free (input_debug.external_rfd);
4939 free (input_debug.external_ext);
4941 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4942 elf_link_input_bfd ignores this section. */
4943 input_section->flags &= ~SEC_HAS_CONTENTS;
4946 if (SGI_COMPAT (abfd) && info->shared)
4948 /* Create .rtproc section. */
4949 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4950 if (rtproc_sec == NULL)
4952 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4953 | SEC_LINKER_CREATED | SEC_READONLY);
4955 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4956 if (rtproc_sec == NULL
4957 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4958 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4962 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4963 info, rtproc_sec, &debug))
4967 /* Build the external symbol information. */
4970 einfo.debug = &debug;
4972 einfo.failed = false;
4973 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4974 mips_elf_output_extsym,
4979 /* Set the size of the .mdebug section. */
4980 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4982 /* Skip this section later on (I don't think this currently
4983 matters, but someday it might). */
4984 o->link_order_head = (struct bfd_link_order *) NULL;
4989 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4991 const char *subname;
4994 Elf32_External_gptab *ext_tab;
4997 /* The .gptab.sdata and .gptab.sbss sections hold
4998 information describing how the small data area would
4999 change depending upon the -G switch. These sections
5000 not used in executables files. */
5001 if (! info->relocateable)
5003 for (p = o->link_order_head;
5004 p != (struct bfd_link_order *) NULL;
5007 asection *input_section;
5009 if (p->type != bfd_indirect_link_order)
5011 if (p->type == bfd_fill_link_order)
5016 input_section = p->u.indirect.section;
5018 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5019 elf_link_input_bfd ignores this section. */
5020 input_section->flags &= ~SEC_HAS_CONTENTS;
5023 /* Skip this section later on (I don't think this
5024 currently matters, but someday it might). */
5025 o->link_order_head = (struct bfd_link_order *) NULL;
5027 /* Really remove the section. */
5028 for (secpp = &abfd->sections;
5030 secpp = &(*secpp)->next)
5032 *secpp = (*secpp)->next;
5033 --abfd->section_count;
5038 /* There is one gptab for initialized data, and one for
5039 uninitialized data. */
5040 if (strcmp (o->name, ".gptab.sdata") == 0)
5042 else if (strcmp (o->name, ".gptab.sbss") == 0)
5046 (*_bfd_error_handler)
5047 (_("%s: illegal section name `%s'"),
5048 bfd_get_filename (abfd), o->name);
5049 bfd_set_error (bfd_error_nonrepresentable_section);
5053 /* The linker script always combines .gptab.data and
5054 .gptab.sdata into .gptab.sdata, and likewise for
5055 .gptab.bss and .gptab.sbss. It is possible that there is
5056 no .sdata or .sbss section in the output file, in which
5057 case we must change the name of the output section. */
5058 subname = o->name + sizeof ".gptab" - 1;
5059 if (bfd_get_section_by_name (abfd, subname) == NULL)
5061 if (o == gptab_data_sec)
5062 o->name = ".gptab.data";
5064 o->name = ".gptab.bss";
5065 subname = o->name + sizeof ".gptab" - 1;
5066 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
5069 /* Set up the first entry. */
5071 amt = c * sizeof (Elf32_gptab);
5072 tab = (Elf32_gptab *) bfd_malloc (amt);
5075 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
5076 tab[0].gt_header.gt_unused = 0;
5078 /* Combine the input sections. */
5079 for (p = o->link_order_head;
5080 p != (struct bfd_link_order *) NULL;
5083 asection *input_section;
5087 bfd_size_type gpentry;
5089 if (p->type != bfd_indirect_link_order)
5091 if (p->type == bfd_fill_link_order)
5096 input_section = p->u.indirect.section;
5097 input_bfd = input_section->owner;
5099 /* Combine the gptab entries for this input section one
5100 by one. We know that the input gptab entries are
5101 sorted by ascending -G value. */
5102 size = bfd_section_size (input_bfd, input_section);
5104 for (gpentry = sizeof (Elf32_External_gptab);
5106 gpentry += sizeof (Elf32_External_gptab))
5108 Elf32_External_gptab ext_gptab;
5109 Elf32_gptab int_gptab;
5115 if (! (bfd_get_section_contents
5116 (input_bfd, input_section, (PTR) &ext_gptab,
5118 (bfd_size_type) sizeof (Elf32_External_gptab))))
5124 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
5126 val = int_gptab.gt_entry.gt_g_value;
5127 add = int_gptab.gt_entry.gt_bytes - last;
5130 for (look = 1; look < c; look++)
5132 if (tab[look].gt_entry.gt_g_value >= val)
5133 tab[look].gt_entry.gt_bytes += add;
5135 if (tab[look].gt_entry.gt_g_value == val)
5141 Elf32_gptab *new_tab;
5144 /* We need a new table entry. */
5145 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
5146 new_tab = (Elf32_gptab *) bfd_realloc ((PTR) tab, amt);
5147 if (new_tab == NULL)
5153 tab[c].gt_entry.gt_g_value = val;
5154 tab[c].gt_entry.gt_bytes = add;
5156 /* Merge in the size for the next smallest -G
5157 value, since that will be implied by this new
5160 for (look = 1; look < c; look++)
5162 if (tab[look].gt_entry.gt_g_value < val
5164 || (tab[look].gt_entry.gt_g_value
5165 > tab[max].gt_entry.gt_g_value)))
5169 tab[c].gt_entry.gt_bytes +=
5170 tab[max].gt_entry.gt_bytes;
5175 last = int_gptab.gt_entry.gt_bytes;
5178 /* Hack: reset the SEC_HAS_CONTENTS flag so that
5179 elf_link_input_bfd ignores this section. */
5180 input_section->flags &= ~SEC_HAS_CONTENTS;
5183 /* The table must be sorted by -G value. */
5185 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
5187 /* Swap out the table. */
5188 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
5189 ext_tab = (Elf32_External_gptab *) bfd_alloc (abfd, amt);
5190 if (ext_tab == NULL)
5196 for (j = 0; j < c; j++)
5197 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
5200 o->_raw_size = c * sizeof (Elf32_External_gptab);
5201 o->contents = (bfd_byte *) ext_tab;
5203 /* Skip this section later on (I don't think this currently
5204 matters, but someday it might). */
5205 o->link_order_head = (struct bfd_link_order *) NULL;
5209 /* Invoke the regular ELF backend linker to do all the work. */
5210 if (ABI_64_P (abfd))
5213 if (!bfd_elf64_bfd_final_link (abfd, info))
5220 else if (!bfd_elf32_bfd_final_link (abfd, info))
5223 /* Now write out the computed sections. */
5225 if (reginfo_sec != (asection *) NULL)
5227 Elf32_External_RegInfo ext;
5229 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
5230 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
5231 (file_ptr) 0, (bfd_size_type) sizeof ext))
5235 if (mdebug_sec != (asection *) NULL)
5237 BFD_ASSERT (abfd->output_has_begun);
5238 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
5240 mdebug_sec->filepos))
5243 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
5246 if (gptab_data_sec != (asection *) NULL)
5248 if (! bfd_set_section_contents (abfd, gptab_data_sec,
5249 gptab_data_sec->contents,
5251 gptab_data_sec->_raw_size))
5255 if (gptab_bss_sec != (asection *) NULL)
5257 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
5258 gptab_bss_sec->contents,
5260 gptab_bss_sec->_raw_size))
5264 if (SGI_COMPAT (abfd))
5266 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
5267 if (rtproc_sec != NULL)
5269 if (! bfd_set_section_contents (abfd, rtproc_sec,
5270 rtproc_sec->contents,
5272 rtproc_sec->_raw_size))
5280 /* This function is called via qsort() to sort the dynamic relocation
5281 entries by increasing r_symndx value. */
5284 sort_dynamic_relocs (arg1, arg2)
5288 const Elf32_External_Rel *ext_reloc1 = (const Elf32_External_Rel *) arg1;
5289 const Elf32_External_Rel *ext_reloc2 = (const Elf32_External_Rel *) arg2;
5291 Elf_Internal_Rel int_reloc1;
5292 Elf_Internal_Rel int_reloc2;
5294 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, ext_reloc1, &int_reloc1);
5295 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, ext_reloc2, &int_reloc2);
5297 return (ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info));
5300 /* Returns the GOT section for ABFD. */
5303 mips_elf_got_section (abfd)
5306 return bfd_get_section_by_name (abfd, ".got");
5309 /* Returns the GOT information associated with the link indicated by
5310 INFO. If SGOTP is non-NULL, it is filled in with the GOT
5313 static struct mips_got_info *
5314 mips_elf_got_info (abfd, sgotp)
5319 struct mips_got_info *g;
5321 sgot = mips_elf_got_section (abfd);
5322 BFD_ASSERT (sgot != NULL);
5323 BFD_ASSERT (elf_section_data (sgot) != NULL);
5324 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5325 BFD_ASSERT (g != NULL);
5332 /* Return whether a relocation is against a local symbol. */
5335 mips_elf_local_relocation_p (input_bfd, relocation, local_sections,
5338 const Elf_Internal_Rela *relocation;
5339 asection **local_sections;
5340 boolean check_forced;
5342 unsigned long r_symndx;
5343 Elf_Internal_Shdr *symtab_hdr;
5344 struct mips_elf_link_hash_entry *h;
5347 r_symndx = ELF32_R_SYM (relocation->r_info);
5348 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5349 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5351 if (r_symndx < extsymoff)
5353 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5358 /* Look up the hash table to check whether the symbol
5359 was forced local. */
5360 h = (struct mips_elf_link_hash_entry *)
5361 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
5362 /* Find the real hash-table entry for this symbol. */
5363 while (h->root.root.type == bfd_link_hash_indirect
5364 || h->root.root.type == bfd_link_hash_warning)
5365 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5366 if ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
5373 /* Sign-extend VALUE, which has the indicated number of BITS. */
5376 mips_elf_sign_extend (value, bits)
5380 if (value & ((bfd_vma) 1 << (bits - 1)))
5381 /* VALUE is negative. */
5382 value |= ((bfd_vma) - 1) << bits;
5387 /* Return non-zero if the indicated VALUE has overflowed the maximum
5388 range expressable by a signed number with the indicated number of
5392 mips_elf_overflow_p (value, bits)
5396 bfd_signed_vma svalue = (bfd_signed_vma) value;
5398 if (svalue > (1 << (bits - 1)) - 1)
5399 /* The value is too big. */
5401 else if (svalue < -(1 << (bits - 1)))
5402 /* The value is too small. */
5409 /* Calculate the %high function. */
5412 mips_elf_high (value)
5415 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5418 /* Calculate the %higher function. */
5421 mips_elf_higher (value)
5422 bfd_vma value ATTRIBUTE_UNUSED;
5425 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5428 return (bfd_vma) -1;
5432 /* Calculate the %highest function. */
5435 mips_elf_highest (value)
5436 bfd_vma value ATTRIBUTE_UNUSED;
5439 return ((value + (bfd_vma) 0x800080008000) >> 48) & 0xffff;
5442 return (bfd_vma) -1;
5446 /* Returns the GOT index for the global symbol indicated by H. */
5449 mips_elf_global_got_index (abfd, h)
5451 struct elf_link_hash_entry *h;
5455 struct mips_got_info *g;
5457 g = mips_elf_got_info (abfd, &sgot);
5459 /* Once we determine the global GOT entry with the lowest dynamic
5460 symbol table index, we must put all dynamic symbols with greater
5461 indices into the GOT. That makes it easy to calculate the GOT
5463 BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
5464 index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno)
5465 * MIPS_ELF_GOT_SIZE (abfd));
5466 BFD_ASSERT (index < sgot->_raw_size);
5471 /* Returns the offset for the entry at the INDEXth position
5475 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5483 sgot = mips_elf_got_section (dynobj);
5484 gp = _bfd_get_gp_value (output_bfd);
5485 return (sgot->output_section->vma + sgot->output_offset + index -
5489 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5490 symbol table index lower than any we've seen to date, record it for
5494 mips_elf_record_global_got_symbol (h, info, g)
5495 struct elf_link_hash_entry *h;
5496 struct bfd_link_info *info;
5497 struct mips_got_info *g ATTRIBUTE_UNUSED;
5499 /* A global symbol in the GOT must also be in the dynamic symbol
5501 if (h->dynindx == -1
5502 && !bfd_elf32_link_record_dynamic_symbol (info, h))
5505 /* If we've already marked this entry as need GOT space, we don't
5506 need to do it again. */
5507 if (h->got.offset != (bfd_vma) - 1)
5510 /* By setting this to a value other than -1, we are indicating that
5511 there needs to be a GOT entry for H. */
5517 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
5518 the dynamic symbols. */
5520 struct mips_elf_hash_sort_data
5522 /* The symbol in the global GOT with the lowest dynamic symbol table
5524 struct elf_link_hash_entry *low;
5525 /* The least dynamic symbol table index corresponding to a symbol
5526 with a GOT entry. */
5527 long min_got_dynindx;
5528 /* The greatest dynamic symbol table index not corresponding to a
5529 symbol without a GOT entry. */
5530 long max_non_got_dynindx;
5533 /* If H needs a GOT entry, assign it the highest available dynamic
5534 index. Otherwise, assign it the lowest available dynamic
5538 mips_elf_sort_hash_table_f (h, data)
5539 struct mips_elf_link_hash_entry *h;
5542 struct mips_elf_hash_sort_data *hsd
5543 = (struct mips_elf_hash_sort_data *) data;
5545 /* Symbols without dynamic symbol table entries aren't interesting
5547 if (h->root.dynindx == -1)
5550 if (h->root.got.offset != 0)
5551 h->root.dynindx = hsd->max_non_got_dynindx++;
5554 h->root.dynindx = --hsd->min_got_dynindx;
5555 hsd->low = (struct elf_link_hash_entry *) h;
5561 /* Sort the dynamic symbol table so that symbols that need GOT entries
5562 appear towards the end. This reduces the amount of GOT space
5563 required. MAX_LOCAL is used to set the number of local symbols
5564 known to be in the dynamic symbol table. During
5565 mips_elf_size_dynamic_sections, this value is 1. Afterward, the
5566 section symbols are added and the count is higher. */
5569 mips_elf_sort_hash_table (info, max_local)
5570 struct bfd_link_info *info;
5571 unsigned long max_local;
5573 struct mips_elf_hash_sort_data hsd;
5574 struct mips_got_info *g;
5577 dynobj = elf_hash_table (info)->dynobj;
5580 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
5581 hsd.max_non_got_dynindx = max_local;
5582 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
5583 elf_hash_table (info)),
5584 mips_elf_sort_hash_table_f,
5587 /* There shoud have been enough room in the symbol table to
5588 accomodate both the GOT and non-GOT symbols. */
5589 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
5591 /* Now we know which dynamic symbol has the lowest dynamic symbol
5592 table index in the GOT. */
5593 g = mips_elf_got_info (dynobj, NULL);
5594 g->global_gotsym = hsd.low;
5599 /* Create a local GOT entry for VALUE. Return the index of the entry,
5600 or -1 if it could not be created. */
5603 mips_elf_create_local_got_entry (abfd, g, sgot, value)
5605 struct mips_got_info *g;
5609 if (g->assigned_gotno >= g->local_gotno)
5611 /* We didn't allocate enough space in the GOT. */
5612 (*_bfd_error_handler)
5613 (_("not enough GOT space for local GOT entries"));
5614 bfd_set_error (bfd_error_bad_value);
5615 return (bfd_vma) -1;
5618 MIPS_ELF_PUT_WORD (abfd, value,
5620 + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
5621 return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
5624 /* Returns the GOT offset at which the indicated address can be found.
5625 If there is not yet a GOT entry for this value, create one. Returns
5626 -1 if no satisfactory GOT offset can be found. */
5629 mips_elf_local_got_index (abfd, info, value)
5631 struct bfd_link_info *info;
5635 struct mips_got_info *g;
5638 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5640 /* Look to see if we already have an appropriate entry. */
5641 for (entry = (sgot->contents
5642 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5643 entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5644 entry += MIPS_ELF_GOT_SIZE (abfd))
5646 bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
5647 if (address == value)
5648 return entry - sgot->contents;
5651 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
5654 /* Find a GOT entry that is within 32KB of the VALUE. These entries
5655 are supposed to be placed at small offsets in the GOT, i.e.,
5656 within 32KB of GP. Return the index into the GOT for this page,
5657 and store the offset from this entry to the desired address in
5658 OFFSETP, if it is non-NULL. */
5661 mips_elf_got_page (abfd, info, value, offsetp)
5663 struct bfd_link_info *info;
5668 struct mips_got_info *g;
5670 bfd_byte *last_entry;
5674 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5676 /* Look to see if we aleady have an appropriate entry. */
5677 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5678 for (entry = (sgot->contents
5679 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5680 entry != last_entry;
5681 entry += MIPS_ELF_GOT_SIZE (abfd))
5683 address = MIPS_ELF_GET_WORD (abfd, entry);
5685 if (!mips_elf_overflow_p (value - address, 16))
5687 /* This entry will serve as the page pointer. We can add a
5688 16-bit number to it to get the actual address. */
5689 index = entry - sgot->contents;
5694 /* If we didn't have an appropriate entry, we create one now. */
5695 if (entry == last_entry)
5696 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5700 address = MIPS_ELF_GET_WORD (abfd, entry);
5701 *offsetp = value - address;
5707 /* Find a GOT entry whose higher-order 16 bits are the same as those
5708 for value. Return the index into the GOT for this entry. */
5711 mips_elf_got16_entry (abfd, info, value, external)
5713 struct bfd_link_info *info;
5718 struct mips_got_info *g;
5720 bfd_byte *last_entry;
5726 /* Although the ABI says that it is "the high-order 16 bits" that we
5727 want, it is really the %high value. The complete value is
5728 calculated with a `addiu' of a LO16 relocation, just as with a
5730 value = mips_elf_high (value) << 16;
5733 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5735 /* Look to see if we already have an appropriate entry. */
5736 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5737 for (entry = (sgot->contents
5738 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5739 entry != last_entry;
5740 entry += MIPS_ELF_GOT_SIZE (abfd))
5742 address = MIPS_ELF_GET_WORD (abfd, entry);
5743 if (address == value)
5745 /* This entry has the right high-order 16 bits, and the low-order
5746 16 bits are set to zero. */
5747 index = entry - sgot->contents;
5752 /* If we didn't have an appropriate entry, we create one now. */
5753 if (entry == last_entry)
5754 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5759 /* Returns the first relocation of type r_type found, beginning with
5760 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5762 static const Elf_Internal_Rela *
5763 mips_elf_next_relocation (r_type, relocation, relend)
5764 unsigned int r_type;
5765 const Elf_Internal_Rela *relocation;
5766 const Elf_Internal_Rela *relend;
5768 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
5769 immediately following. However, for the IRIX6 ABI, the next
5770 relocation may be a composed relocation consisting of several
5771 relocations for the same address. In that case, the R_MIPS_LO16
5772 relocation may occur as one of these. We permit a similar
5773 extension in general, as that is useful for GCC. */
5774 while (relocation < relend)
5776 if (ELF32_R_TYPE (relocation->r_info) == r_type)
5782 /* We didn't find it. */
5783 bfd_set_error (bfd_error_bad_value);
5787 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5788 is the original relocation, which is now being transformed into a
5789 dynamic relocation. The ADDENDP is adjusted if necessary; the
5790 caller should store the result in place of the original addend. */
5793 mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec,
5794 symbol, addendp, input_section)
5796 struct bfd_link_info *info;
5797 const Elf_Internal_Rela *rel;
5798 struct mips_elf_link_hash_entry *h;
5802 asection *input_section;
5804 Elf_Internal_Rel outrel;
5810 r_type = ELF32_R_TYPE (rel->r_info);
5811 dynobj = elf_hash_table (info)->dynobj;
5813 = bfd_get_section_by_name (dynobj,
5814 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd));
5815 BFD_ASSERT (sreloc != NULL);
5816 BFD_ASSERT (sreloc->contents != NULL);
5817 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5818 < sreloc->_raw_size);
5822 /* We begin by assuming that the offset for the dynamic relocation
5823 is the same as for the original relocation. We'll adjust this
5824 later to reflect the correct output offsets. */
5825 if (elf_section_data (input_section)->stab_info == NULL)
5826 outrel.r_offset = rel->r_offset;
5829 /* Except that in a stab section things are more complex.
5830 Because we compress stab information, the offset given in the
5831 relocation may not be the one we want; we must let the stabs
5832 machinery tell us the offset. */
5834 = (_bfd_stab_section_offset
5835 (output_bfd, &elf_hash_table (info)->stab_info,
5837 &elf_section_data (input_section)->stab_info,
5839 /* If we didn't need the relocation at all, this value will be
5841 if (outrel.r_offset == (bfd_vma) -1)
5845 /* If we've decided to skip this relocation, just output an empty
5846 record. Note that R_MIPS_NONE == 0, so that this call to memset
5847 is a way of setting R_TYPE to R_MIPS_NONE. */
5849 memset (&outrel, 0, sizeof (outrel));
5853 bfd_vma section_offset;
5855 /* We must now calculate the dynamic symbol table index to use
5856 in the relocation. */
5858 && (! info->symbolic || (h->root.elf_link_hash_flags
5859 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5861 indx = h->root.dynindx;
5862 /* h->root.dynindx may be -1 if this symbol was marked to
5869 if (sec != NULL && bfd_is_abs_section (sec))
5871 else if (sec == NULL || sec->owner == NULL)
5873 bfd_set_error (bfd_error_bad_value);
5878 indx = elf_section_data (sec->output_section)->dynindx;
5883 /* Figure out how far the target of the relocation is from
5884 the beginning of its section. */
5885 section_offset = symbol - sec->output_section->vma;
5886 /* The relocation we're building is section-relative.
5887 Therefore, the original addend must be adjusted by the
5889 *addendp += section_offset;
5890 /* Now, the relocation is just against the section. */
5891 symbol = sec->output_section->vma;
5894 /* If the relocation was previously an absolute relocation and
5895 this symbol will not be referred to by the relocation, we must
5896 adjust it by the value we give it in the dynamic symbol table.
5897 Otherwise leave the job up to the dynamic linker. */
5898 if (!indx && r_type != R_MIPS_REL32)
5901 /* The relocation is always an REL32 relocation because we don't
5902 know where the shared library will wind up at load-time. */
5903 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5905 /* Adjust the output offset of the relocation to reference the
5906 correct location in the output file. */
5907 outrel.r_offset += (input_section->output_section->vma
5908 + input_section->output_offset);
5911 /* Put the relocation back out. We have to use the special
5912 relocation outputter in the 64-bit case since the 64-bit
5913 relocation format is non-standard. */
5914 if (ABI_64_P (output_bfd))
5916 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5917 (output_bfd, &outrel,
5919 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5922 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5923 (((Elf32_External_Rel *)
5925 + sreloc->reloc_count));
5927 /* Record the index of the first relocation referencing H. This
5928 information is later emitted in the .msym section. */
5930 && (h->min_dyn_reloc_index == 0
5931 || sreloc->reloc_count < h->min_dyn_reloc_index))
5932 h->min_dyn_reloc_index = sreloc->reloc_count;
5934 /* We've now added another relocation. */
5935 ++sreloc->reloc_count;
5937 /* Make sure the output section is writable. The dynamic linker
5938 will be writing to it. */
5939 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5942 /* On IRIX5, make an entry of compact relocation info. */
5943 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
5945 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5950 Elf32_crinfo cptrel;
5952 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5953 cptrel.vaddr = (rel->r_offset
5954 + input_section->output_section->vma
5955 + input_section->output_offset);
5956 if (r_type == R_MIPS_REL32)
5957 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5959 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5960 mips_elf_set_cr_dist2to (cptrel, 0);
5961 cptrel.konst = *addendp;
5963 cr = (scpt->contents
5964 + sizeof (Elf32_External_compact_rel));
5965 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5966 ((Elf32_External_crinfo *) cr
5967 + scpt->reloc_count));
5968 ++scpt->reloc_count;
5975 /* Calculate the value produced by the RELOCATION (which comes from
5976 the INPUT_BFD). The ADDEND is the addend to use for this
5977 RELOCATION; RELOCATION->R_ADDEND is ignored.
5979 The result of the relocation calculation is stored in VALUEP.
5980 REQUIRE_JALXP indicates whether or not the opcode used with this
5981 relocation must be JALX.
5983 This function returns bfd_reloc_continue if the caller need take no
5984 further action regarding this relocation, bfd_reloc_notsupported if
5985 something goes dramatically wrong, bfd_reloc_overflow if an
5986 overflow occurs, and bfd_reloc_ok to indicate success. */
5988 static bfd_reloc_status_type
5989 mips_elf_calculate_relocation (abfd,
6003 asection *input_section;
6004 struct bfd_link_info *info;
6005 const Elf_Internal_Rela *relocation;
6007 reloc_howto_type *howto;
6008 Elf_Internal_Sym *local_syms;
6009 asection **local_sections;
6012 boolean *require_jalxp;
6014 /* The eventual value we will return. */
6016 /* The address of the symbol against which the relocation is
6019 /* The final GP value to be used for the relocatable, executable, or
6020 shared object file being produced. */
6021 bfd_vma gp = (bfd_vma) - 1;
6022 /* The place (section offset or address) of the storage unit being
6025 /* The value of GP used to create the relocatable object. */
6026 bfd_vma gp0 = (bfd_vma) - 1;
6027 /* The offset into the global offset table at which the address of
6028 the relocation entry symbol, adjusted by the addend, resides
6029 during execution. */
6030 bfd_vma g = (bfd_vma) - 1;
6031 /* The section in which the symbol referenced by the relocation is
6033 asection *sec = NULL;
6034 struct mips_elf_link_hash_entry *h = NULL;
6035 /* True if the symbol referred to by this relocation is a local
6038 /* True if the symbol referred to by this relocation is "_gp_disp". */
6039 boolean gp_disp_p = false;
6040 Elf_Internal_Shdr *symtab_hdr;
6042 unsigned long r_symndx;
6044 /* True if overflow occurred during the calculation of the
6045 relocation value. */
6046 boolean overflowed_p;
6047 /* True if this relocation refers to a MIPS16 function. */
6048 boolean target_is_16_bit_code_p = false;
6050 /* Parse the relocation. */
6051 r_symndx = ELF32_R_SYM (relocation->r_info);
6052 r_type = ELF32_R_TYPE (relocation->r_info);
6053 p = (input_section->output_section->vma
6054 + input_section->output_offset
6055 + relocation->r_offset);
6057 /* Assume that there will be no overflow. */
6058 overflowed_p = false;
6060 /* Figure out whether or not the symbol is local, and get the offset
6061 used in the array of hash table entries. */
6062 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6063 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
6064 local_sections, false);
6065 if (! elf_bad_symtab (input_bfd))
6066 extsymoff = symtab_hdr->sh_info;
6069 /* The symbol table does not follow the rule that local symbols
6070 must come before globals. */
6074 /* Figure out the value of the symbol. */
6077 Elf_Internal_Sym *sym;
6079 sym = local_syms + r_symndx;
6080 sec = local_sections[r_symndx];
6082 symbol = sec->output_section->vma + sec->output_offset;
6083 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
6084 symbol += sym->st_value;
6086 /* MIPS16 text labels should be treated as odd. */
6087 if (sym->st_other == STO_MIPS16)
6090 /* Record the name of this symbol, for our caller. */
6091 *namep = bfd_elf_string_from_elf_section (input_bfd,
6092 symtab_hdr->sh_link,
6095 *namep = bfd_section_name (input_bfd, sec);
6097 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
6101 /* For global symbols we look up the symbol in the hash-table. */
6102 h = ((struct mips_elf_link_hash_entry *)
6103 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
6104 /* Find the real hash-table entry for this symbol. */
6105 while (h->root.root.type == bfd_link_hash_indirect
6106 || h->root.root.type == bfd_link_hash_warning)
6107 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6109 /* Record the name of this symbol, for our caller. */
6110 *namep = h->root.root.root.string;
6112 /* See if this is the special _gp_disp symbol. Note that such a
6113 symbol must always be a global symbol. */
6114 if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
6116 /* Relocations against _gp_disp are permitted only with
6117 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
6118 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
6119 return bfd_reloc_notsupported;
6123 /* If this symbol is defined, calculate its address. Note that
6124 _gp_disp is a magic symbol, always implicitly defined by the
6125 linker, so it's inappropriate to check to see whether or not
6127 else if ((h->root.root.type == bfd_link_hash_defined
6128 || h->root.root.type == bfd_link_hash_defweak)
6129 && h->root.root.u.def.section)
6131 sec = h->root.root.u.def.section;
6132 if (sec->output_section)
6133 symbol = (h->root.root.u.def.value
6134 + sec->output_section->vma
6135 + sec->output_offset);
6137 symbol = h->root.root.u.def.value;
6139 else if (h->root.root.type == bfd_link_hash_undefweak)
6140 /* We allow relocations against undefined weak symbols, giving
6141 it the value zero, so that you can undefined weak functions
6142 and check to see if they exist by looking at their
6145 else if (info->shared && !info->symbolic && !info->no_undefined
6146 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
6148 else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0 ||
6149 strcmp (h->root.root.root.string, "_DYNAMIC_LINKING") == 0)
6151 /* If this is a dynamic link, we should have created a
6152 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
6153 in in mips_elf_create_dynamic_sections.
6154 Otherwise, we should define the symbol with a value of 0.
6155 FIXME: It should probably get into the symbol table
6157 BFD_ASSERT (! info->shared);
6158 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
6163 if (! ((*info->callbacks->undefined_symbol)
6164 (info, h->root.root.root.string, input_bfd,
6165 input_section, relocation->r_offset,
6166 (!info->shared || info->no_undefined
6167 || ELF_ST_VISIBILITY (h->root.other)))))
6168 return bfd_reloc_undefined;
6172 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
6175 /* If this is a 32-bit call to a 16-bit function with a stub, we
6176 need to redirect the call to the stub, unless we're already *in*
6178 if (r_type != R_MIPS16_26 && !info->relocateable
6179 && ((h != NULL && h->fn_stub != NULL)
6180 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
6181 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
6182 && !mips_elf_stub_section_p (input_bfd, input_section))
6184 /* This is a 32-bit call to a 16-bit function. We should
6185 have already noticed that we were going to need the
6188 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
6191 BFD_ASSERT (h->need_fn_stub);
6195 symbol = sec->output_section->vma + sec->output_offset;
6197 /* If this is a 16-bit call to a 32-bit function with a stub, we
6198 need to redirect the call to the stub. */
6199 else if (r_type == R_MIPS16_26 && !info->relocateable
6201 && (h->call_stub != NULL || h->call_fp_stub != NULL)
6202 && !target_is_16_bit_code_p)
6204 /* If both call_stub and call_fp_stub are defined, we can figure
6205 out which one to use by seeing which one appears in the input
6207 if (h->call_stub != NULL && h->call_fp_stub != NULL)
6212 for (o = input_bfd->sections; o != NULL; o = o->next)
6214 if (strncmp (bfd_get_section_name (input_bfd, o),
6215 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6217 sec = h->call_fp_stub;
6224 else if (h->call_stub != NULL)
6227 sec = h->call_fp_stub;
6229 BFD_ASSERT (sec->_raw_size > 0);
6230 symbol = sec->output_section->vma + sec->output_offset;
6233 /* Calls from 16-bit code to 32-bit code and vice versa require the
6234 special jalx instruction. */
6235 *require_jalxp = (!info->relocateable
6236 && ((r_type == R_MIPS16_26) != target_is_16_bit_code_p));
6238 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
6239 local_sections, true);
6241 /* If we haven't already determined the GOT offset, or the GP value,
6242 and we're going to need it, get it now. */
6247 case R_MIPS_GOT_DISP:
6248 case R_MIPS_GOT_HI16:
6249 case R_MIPS_CALL_HI16:
6250 case R_MIPS_GOT_LO16:
6251 case R_MIPS_CALL_LO16:
6252 /* Find the index into the GOT where this value is located. */
6255 BFD_ASSERT (addend == 0);
6256 g = mips_elf_global_got_index
6257 (elf_hash_table (info)->dynobj,
6258 (struct elf_link_hash_entry *) h);
6259 if (! elf_hash_table(info)->dynamic_sections_created
6261 && (info->symbolic || h->root.dynindx == -1)
6262 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
6264 /* This is a static link or a -Bsymbolic link. The
6265 symbol is defined locally, or was forced to be local.
6266 We must initialize this entry in the GOT. */
6267 asection *sgot = mips_elf_got_section(elf_hash_table
6269 MIPS_ELF_PUT_WORD (elf_hash_table (info)->dynobj,
6270 symbol + addend, sgot->contents + g);
6273 else if (r_type == R_MIPS_GOT16 || r_type == R_MIPS_CALL16)
6274 /* There's no need to create a local GOT entry here; the
6275 calculation for a local GOT16 entry does not involve G. */
6279 g = mips_elf_local_got_index (abfd, info, symbol + addend);
6280 if (g == (bfd_vma) -1)
6284 /* Convert GOT indices to actual offsets. */
6285 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6291 case R_MIPS_GPREL16:
6292 case R_MIPS_GPREL32:
6293 case R_MIPS_LITERAL:
6294 gp0 = _bfd_get_gp_value (input_bfd);
6295 gp = _bfd_get_gp_value (abfd);
6302 /* Figure out what kind of relocation is being performed. */
6306 return bfd_reloc_continue;
6309 value = symbol + mips_elf_sign_extend (addend, 16);
6310 overflowed_p = mips_elf_overflow_p (value, 16);
6317 || (elf_hash_table (info)->dynamic_sections_created
6319 && ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
6321 && (input_section->flags & SEC_ALLOC) != 0)
6323 /* If we're creating a shared library, or this relocation is
6324 against a symbol in a shared library, then we can't know
6325 where the symbol will end up. So, we create a relocation
6326 record in the output, and leave the job up to the dynamic
6329 if (!mips_elf_create_dynamic_relocation (abfd,
6341 if (r_type != R_MIPS_REL32)
6342 value = symbol + addend;
6346 value &= howto->dst_mask;
6351 case R_MIPS_GNU_REL_LO16:
6352 value = symbol + addend - p;
6353 value &= howto->dst_mask;
6356 case R_MIPS_GNU_REL16_S2:
6357 value = symbol + mips_elf_sign_extend (addend << 2, 18) - p;
6358 overflowed_p = mips_elf_overflow_p (value, 18);
6359 value = (value >> 2) & howto->dst_mask;
6362 case R_MIPS_GNU_REL_HI16:
6363 value = mips_elf_high (addend + symbol - p);
6364 value &= howto->dst_mask;
6368 /* The calculation for R_MIPS16_26 is just the same as for an
6369 R_MIPS_26. It's only the storage of the relocated field into
6370 the output file that's different. That's handled in
6371 mips_elf_perform_relocation. So, we just fall through to the
6372 R_MIPS_26 case here. */
6375 value = (((addend << 2) | ((p + 4) & 0xf0000000)) + symbol) >> 2;
6377 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
6378 value &= howto->dst_mask;
6384 value = mips_elf_high (addend + symbol);
6385 value &= howto->dst_mask;
6389 value = mips_elf_high (addend + gp - p);
6390 overflowed_p = mips_elf_overflow_p (value, 16);
6396 value = (symbol + addend) & howto->dst_mask;
6399 value = addend + gp - p + 4;
6400 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6401 for overflow. But, on, say, Irix 5, relocations against
6402 _gp_disp are normally generated from the .cpload
6403 pseudo-op. It generates code that normally looks like
6406 lui $gp,%hi(_gp_disp)
6407 addiu $gp,$gp,%lo(_gp_disp)
6410 Here $t9 holds the address of the function being called,
6411 as required by the MIPS ELF ABI. The R_MIPS_LO16
6412 relocation can easily overflow in this situation, but the
6413 R_MIPS_HI16 relocation will handle the overflow.
6414 Therefore, we consider this a bug in the MIPS ABI, and do
6415 not check for overflow here. */
6419 case R_MIPS_LITERAL:
6420 /* Because we don't merge literal sections, we can handle this
6421 just like R_MIPS_GPREL16. In the long run, we should merge
6422 shared literals, and then we will need to additional work
6427 case R_MIPS16_GPREL:
6428 /* The R_MIPS16_GPREL performs the same calculation as
6429 R_MIPS_GPREL16, but stores the relocated bits in a different
6430 order. We don't need to do anything special here; the
6431 differences are handled in mips_elf_perform_relocation. */
6432 case R_MIPS_GPREL16:
6434 value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
6436 value = mips_elf_sign_extend (addend, 16) + symbol - gp;
6437 overflowed_p = mips_elf_overflow_p (value, 16);
6446 /* The special case is when the symbol is forced to be local. We
6447 need the full address in the GOT since no R_MIPS_LO16 relocation
6449 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
6450 local_sections, false);
6451 value = mips_elf_got16_entry (abfd, info, symbol + addend, forced);
6452 if (value == (bfd_vma) -1)
6455 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6458 overflowed_p = mips_elf_overflow_p (value, 16);
6464 case R_MIPS_GOT_DISP:
6466 overflowed_p = mips_elf_overflow_p (value, 16);
6469 case R_MIPS_GPREL32:
6470 value = (addend + symbol + gp0 - gp) & howto->dst_mask;
6474 value = mips_elf_sign_extend (addend, 16) + symbol - p;
6475 overflowed_p = mips_elf_overflow_p (value, 16);
6476 value = (bfd_vma) ((bfd_signed_vma) value / 4);
6479 case R_MIPS_GOT_HI16:
6480 case R_MIPS_CALL_HI16:
6481 /* We're allowed to handle these two relocations identically.
6482 The dynamic linker is allowed to handle the CALL relocations
6483 differently by creating a lazy evaluation stub. */
6485 value = mips_elf_high (value);
6486 value &= howto->dst_mask;
6489 case R_MIPS_GOT_LO16:
6490 case R_MIPS_CALL_LO16:
6491 value = g & howto->dst_mask;
6494 case R_MIPS_GOT_PAGE:
6495 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
6496 if (value == (bfd_vma) -1)
6498 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6501 overflowed_p = mips_elf_overflow_p (value, 16);
6504 case R_MIPS_GOT_OFST:
6505 mips_elf_got_page (abfd, info, symbol + addend, &value);
6506 overflowed_p = mips_elf_overflow_p (value, 16);
6510 value = symbol - addend;
6511 value &= howto->dst_mask;
6515 value = mips_elf_higher (addend + symbol);
6516 value &= howto->dst_mask;
6519 case R_MIPS_HIGHEST:
6520 value = mips_elf_highest (addend + symbol);
6521 value &= howto->dst_mask;
6524 case R_MIPS_SCN_DISP:
6525 value = symbol + addend - sec->output_offset;
6526 value &= howto->dst_mask;
6531 /* Both of these may be ignored. R_MIPS_JALR is an optimization
6532 hint; we could improve performance by honoring that hint. */
6533 return bfd_reloc_continue;
6535 case R_MIPS_GNU_VTINHERIT:
6536 case R_MIPS_GNU_VTENTRY:
6537 /* We don't do anything with these at present. */
6538 return bfd_reloc_continue;
6541 /* An unrecognized relocation type. */
6542 return bfd_reloc_notsupported;
6545 /* Store the VALUE for our caller. */
6547 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6550 /* Obtain the field relocated by RELOCATION. */
6553 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
6554 reloc_howto_type *howto;
6555 const Elf_Internal_Rela *relocation;
6560 bfd_byte *location = contents + relocation->r_offset;
6562 /* Obtain the bytes. */
6563 x = bfd_get (8 * bfd_get_reloc_size (howto), input_bfd, location);
6565 if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26
6566 || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL)
6567 && bfd_little_endian (input_bfd))
6568 /* The two 16-bit words will be reversed on a little-endian
6569 system. See mips_elf_perform_relocation for more details. */
6570 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6575 /* It has been determined that the result of the RELOCATION is the
6576 VALUE. Use HOWTO to place VALUE into the output file at the
6577 appropriate position. The SECTION is the section to which the
6578 relocation applies. If REQUIRE_JALX is true, then the opcode used
6579 for the relocation must be either JAL or JALX, and it is
6580 unconditionally converted to JALX.
6582 Returns false if anything goes wrong. */
6585 mips_elf_perform_relocation (info, howto, relocation, value,
6586 input_bfd, input_section,
6587 contents, require_jalx)
6588 struct bfd_link_info *info;
6589 reloc_howto_type *howto;
6590 const Elf_Internal_Rela *relocation;
6593 asection *input_section;
6595 boolean require_jalx;
6599 int r_type = ELF32_R_TYPE (relocation->r_info);
6601 /* Figure out where the relocation is occurring. */
6602 location = contents + relocation->r_offset;
6604 /* Obtain the current value. */
6605 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6607 /* Clear the field we are setting. */
6608 x &= ~howto->dst_mask;
6610 /* If this is the R_MIPS16_26 relocation, we must store the
6611 value in a funny way. */
6612 if (r_type == R_MIPS16_26)
6614 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
6615 Most mips16 instructions are 16 bits, but these instructions
6618 The format of these instructions is:
6620 +--------------+--------------------------------+
6621 ! JALX ! X! Imm 20:16 ! Imm 25:21 !
6622 +--------------+--------------------------------+
6624 +-----------------------------------------------+
6626 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
6627 Note that the immediate value in the first word is swapped.
6629 When producing a relocateable object file, R_MIPS16_26 is
6630 handled mostly like R_MIPS_26. In particular, the addend is
6631 stored as a straight 26-bit value in a 32-bit instruction.
6632 (gas makes life simpler for itself by never adjusting a
6633 R_MIPS16_26 reloc to be against a section, so the addend is
6634 always zero). However, the 32 bit instruction is stored as 2
6635 16-bit values, rather than a single 32-bit value. In a
6636 big-endian file, the result is the same; in a little-endian
6637 file, the two 16-bit halves of the 32 bit value are swapped.
6638 This is so that a disassembler can recognize the jal
6641 When doing a final link, R_MIPS16_26 is treated as a 32 bit
6642 instruction stored as two 16-bit values. The addend A is the
6643 contents of the targ26 field. The calculation is the same as
6644 R_MIPS_26. When storing the calculated value, reorder the
6645 immediate value as shown above, and don't forget to store the
6646 value as two 16-bit values.
6648 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
6652 +--------+----------------------+
6656 +--------+----------------------+
6659 +----------+------+-------------+
6663 +----------+--------------------+
6664 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
6665 ((sub1 << 16) | sub2)).
6667 When producing a relocateable object file, the calculation is
6668 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
6669 When producing a fully linked file, the calculation is
6670 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
6671 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */
6673 if (!info->relocateable)
6674 /* Shuffle the bits according to the formula above. */
6675 value = (((value & 0x1f0000) << 5)
6676 | ((value & 0x3e00000) >> 5)
6677 | (value & 0xffff));
6679 else if (r_type == R_MIPS16_GPREL)
6681 /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
6682 mode. A typical instruction will have a format like this:
6684 +--------------+--------------------------------+
6685 ! EXTEND ! Imm 10:5 ! Imm 15:11 !
6686 +--------------+--------------------------------+
6687 ! Major ! rx ! ry ! Imm 4:0 !
6688 +--------------+--------------------------------+
6690 EXTEND is the five bit value 11110. Major is the instruction
6693 This is handled exactly like R_MIPS_GPREL16, except that the
6694 addend is retrieved and stored as shown in this diagram; that
6695 is, the Imm fields above replace the V-rel16 field.
6697 All we need to do here is shuffle the bits appropriately. As
6698 above, the two 16-bit halves must be swapped on a
6699 little-endian system. */
6700 value = (((value & 0x7e0) << 16)
6701 | ((value & 0xf800) << 5)
6705 /* Set the field. */
6706 x |= (value & howto->dst_mask);
6708 /* If required, turn JAL into JALX. */
6712 bfd_vma opcode = x >> 26;
6713 bfd_vma jalx_opcode;
6715 /* Check to see if the opcode is already JAL or JALX. */
6716 if (r_type == R_MIPS16_26)
6718 ok = ((opcode == 0x6) || (opcode == 0x7));
6723 ok = ((opcode == 0x3) || (opcode == 0x1d));
6727 /* If the opcode is not JAL or JALX, there's a problem. */
6730 (*_bfd_error_handler)
6731 (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
6732 bfd_get_filename (input_bfd),
6733 input_section->name,
6734 (unsigned long) relocation->r_offset);
6735 bfd_set_error (bfd_error_bad_value);
6739 /* Make this the JALX opcode. */
6740 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6743 /* Swap the high- and low-order 16 bits on little-endian systems
6744 when doing a MIPS16 relocation. */
6745 if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
6746 && bfd_little_endian (input_bfd))
6747 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6749 /* Put the value into the output. */
6750 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6754 /* Returns true if SECTION is a MIPS16 stub section. */
6757 mips_elf_stub_section_p (abfd, section)
6758 bfd *abfd ATTRIBUTE_UNUSED;
6761 const char *name = bfd_get_section_name (abfd, section);
6763 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
6764 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6765 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
6768 /* Relocate a MIPS ELF section. */
6771 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6772 contents, relocs, local_syms, local_sections)
6774 struct bfd_link_info *info;
6776 asection *input_section;
6778 Elf_Internal_Rela *relocs;
6779 Elf_Internal_Sym *local_syms;
6780 asection **local_sections;
6782 Elf_Internal_Rela *rel;
6783 const Elf_Internal_Rela *relend;
6785 boolean use_saved_addend_p = false;
6786 struct elf_backend_data *bed;
6788 bed = get_elf_backend_data (output_bfd);
6789 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6790 for (rel = relocs; rel < relend; ++rel)
6794 reloc_howto_type *howto;
6795 boolean require_jalx;
6796 /* True if the relocation is a RELA relocation, rather than a
6798 boolean rela_relocation_p = true;
6799 unsigned int r_type = ELF32_R_TYPE (rel->r_info);
6800 const char * msg = (const char *) NULL;
6802 /* Find the relocation howto for this relocation. */
6803 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6805 /* Some 32-bit code uses R_MIPS_64. In particular, people use
6806 64-bit code, but make sure all their addresses are in the
6807 lowermost or uppermost 32-bit section of the 64-bit address
6808 space. Thus, when they use an R_MIPS_64 they mean what is
6809 usually meant by R_MIPS_32, with the exception that the
6810 stored value is sign-extended to 64 bits. */
6811 howto = elf_mips_howto_table + R_MIPS_32;
6813 /* On big-endian systems, we need to lie about the position
6815 if (bfd_big_endian (input_bfd))
6819 howto = mips_rtype_to_howto (r_type);
6821 if (!use_saved_addend_p)
6823 Elf_Internal_Shdr *rel_hdr;
6825 /* If these relocations were originally of the REL variety,
6826 we must pull the addend out of the field that will be
6827 relocated. Otherwise, we simply use the contents of the
6828 RELA relocation. To determine which flavor or relocation
6829 this is, we depend on the fact that the INPUT_SECTION's
6830 REL_HDR is read before its REL_HDR2. */
6831 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6832 if ((size_t) (rel - relocs)
6833 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
6834 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6835 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6837 /* Note that this is a REL relocation. */
6838 rela_relocation_p = false;
6840 /* Get the addend, which is stored in the input file. */
6841 addend = mips_elf_obtain_contents (howto,
6845 addend &= howto->src_mask;
6847 /* For some kinds of relocations, the ADDEND is a
6848 combination of the addend stored in two different
6850 if (r_type == R_MIPS_HI16
6851 || r_type == R_MIPS_GNU_REL_HI16
6852 || (r_type == R_MIPS_GOT16
6853 && mips_elf_local_relocation_p (input_bfd, rel,
6854 local_sections, false)))
6857 const Elf_Internal_Rela *lo16_relocation;
6858 reloc_howto_type *lo16_howto;
6861 /* The combined value is the sum of the HI16 addend,
6862 left-shifted by sixteen bits, and the LO16
6863 addend, sign extended. (Usually, the code does
6864 a `lui' of the HI16 value, and then an `addiu' of
6867 Scan ahead to find a matching LO16 relocation. */
6868 if (r_type == R_MIPS_GNU_REL_HI16)
6869 lo = R_MIPS_GNU_REL_LO16;
6873 = mips_elf_next_relocation (lo, rel, relend);
6874 if (lo16_relocation == NULL)
6877 /* Obtain the addend kept there. */
6878 lo16_howto = mips_rtype_to_howto (lo);
6879 l = mips_elf_obtain_contents (lo16_howto,
6881 input_bfd, contents);
6882 l &= lo16_howto->src_mask;
6883 l = mips_elf_sign_extend (l, 16);
6887 /* Compute the combined addend. */
6890 else if (r_type == R_MIPS16_GPREL)
6892 /* The addend is scrambled in the object file. See
6893 mips_elf_perform_relocation for details on the
6895 addend = (((addend & 0x1f0000) >> 5)
6896 | ((addend & 0x7e00000) >> 16)
6901 addend = rel->r_addend;
6904 if (info->relocateable)
6906 Elf_Internal_Sym *sym;
6907 unsigned long r_symndx;
6909 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd)
6910 && bfd_big_endian (input_bfd))
6913 /* Since we're just relocating, all we need to do is copy
6914 the relocations back out to the object file, unless
6915 they're against a section symbol, in which case we need
6916 to adjust by the section offset, or unless they're GP
6917 relative in which case we need to adjust by the amount
6918 that we're adjusting GP in this relocateable object. */
6920 if (!mips_elf_local_relocation_p (input_bfd, rel, local_sections,
6922 /* There's nothing to do for non-local relocations. */
6925 if (r_type == R_MIPS16_GPREL
6926 || r_type == R_MIPS_GPREL16
6927 || r_type == R_MIPS_GPREL32
6928 || r_type == R_MIPS_LITERAL)
6929 addend -= (_bfd_get_gp_value (output_bfd)
6930 - _bfd_get_gp_value (input_bfd));
6931 else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26
6932 || r_type == R_MIPS_GNU_REL16_S2)
6933 /* The addend is stored without its two least
6934 significant bits (which are always zero.) In a
6935 non-relocateable link, calculate_relocation will do
6936 this shift; here, we must do it ourselves. */
6939 r_symndx = ELF32_R_SYM (rel->r_info);
6940 sym = local_syms + r_symndx;
6941 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6942 /* Adjust the addend appropriately. */
6943 addend += local_sections[r_symndx]->output_offset;
6945 /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
6946 then we only want to write out the high-order 16 bits.
6947 The subsequent R_MIPS_LO16 will handle the low-order bits. */
6948 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16
6949 || r_type == R_MIPS_GNU_REL_HI16)
6950 addend = mips_elf_high (addend);
6951 /* If the relocation is for an R_MIPS_26 relocation, then
6952 the two low-order bits are not stored in the object file;
6953 they are implicitly zero. */
6954 else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26
6955 || r_type == R_MIPS_GNU_REL16_S2)
6958 if (rela_relocation_p)
6959 /* If this is a RELA relocation, just update the addend.
6960 We have to cast away constness for REL. */
6961 rel->r_addend = addend;
6964 /* Otherwise, we have to write the value back out. Note
6965 that we use the source mask, rather than the
6966 destination mask because the place to which we are
6967 writing will be source of the addend in the final
6969 addend &= howto->src_mask;
6971 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6972 /* See the comment above about using R_MIPS_64 in the 32-bit
6973 ABI. Here, we need to update the addend. It would be
6974 possible to get away with just using the R_MIPS_32 reloc
6975 but for endianness. */
6981 if (addend & ((bfd_vma) 1 << 31))
6982 sign_bits = ((bfd_vma) 1 << 32) - 1;
6986 /* If we don't know that we have a 64-bit type,
6987 do two separate stores. */
6988 if (bfd_big_endian (input_bfd))
6990 /* Store the sign-bits (which are most significant)
6992 low_bits = sign_bits;
6998 high_bits = sign_bits;
7000 bfd_put_32 (input_bfd, low_bits,
7001 contents + rel->r_offset);
7002 bfd_put_32 (input_bfd, high_bits,
7003 contents + rel->r_offset + 4);
7007 if (!mips_elf_perform_relocation (info, howto, rel, addend,
7008 input_bfd, input_section,
7013 /* Go on to the next relocation. */
7017 /* In the N32 and 64-bit ABIs there may be multiple consecutive
7018 relocations for the same offset. In that case we are
7019 supposed to treat the output of each relocation as the addend
7021 if (rel + 1 < relend
7022 && rel->r_offset == rel[1].r_offset
7023 && ELF32_R_TYPE (rel[1].r_info) != R_MIPS_NONE)
7024 use_saved_addend_p = true;
7026 use_saved_addend_p = false;
7028 /* Figure out what value we are supposed to relocate. */
7029 switch (mips_elf_calculate_relocation (output_bfd,
7042 case bfd_reloc_continue:
7043 /* There's nothing to do. */
7046 case bfd_reloc_undefined:
7047 /* mips_elf_calculate_relocation already called the
7048 undefined_symbol callback. There's no real point in
7049 trying to perform the relocation at this point, so we
7050 just skip ahead to the next relocation. */
7053 case bfd_reloc_notsupported:
7054 msg = _("internal error: unsupported relocation error");
7055 info->callbacks->warning
7056 (info, msg, name, input_bfd, input_section, rel->r_offset);
7059 case bfd_reloc_overflow:
7060 if (use_saved_addend_p)
7061 /* Ignore overflow until we reach the last relocation for
7062 a given location. */
7066 BFD_ASSERT (name != NULL);
7067 if (! ((*info->callbacks->reloc_overflow)
7068 (info, name, howto->name, (bfd_vma) 0,
7069 input_bfd, input_section, rel->r_offset)))
7082 /* If we've got another relocation for the address, keep going
7083 until we reach the last one. */
7084 if (use_saved_addend_p)
7090 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
7091 /* See the comment above about using R_MIPS_64 in the 32-bit
7092 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
7093 that calculated the right value. Now, however, we
7094 sign-extend the 32-bit result to 64-bits, and store it as a
7095 64-bit value. We are especially generous here in that we
7096 go to extreme lengths to support this usage on systems with
7097 only a 32-bit VMA. */
7103 if (value & ((bfd_vma) 1 << 31))
7104 sign_bits = ((bfd_vma) 1 << 32) - 1;
7108 /* If we don't know that we have a 64-bit type,
7109 do two separate stores. */
7110 if (bfd_big_endian (input_bfd))
7112 /* Undo what we did above. */
7114 /* Store the sign-bits (which are most significant)
7116 low_bits = sign_bits;
7122 high_bits = sign_bits;
7124 bfd_put_32 (input_bfd, low_bits,
7125 contents + rel->r_offset);
7126 bfd_put_32 (input_bfd, high_bits,
7127 contents + rel->r_offset + 4);
7131 /* Actually perform the relocation. */
7132 if (!mips_elf_perform_relocation (info, howto, rel, value, input_bfd,
7133 input_section, contents,
7141 /* This hook function is called before the linker writes out a global
7142 symbol. We mark symbols as small common if appropriate. This is
7143 also where we undo the increment of the value for a mips16 symbol. */
7146 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
7147 bfd *abfd ATTRIBUTE_UNUSED;
7148 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7149 const char *name ATTRIBUTE_UNUSED;
7150 Elf_Internal_Sym *sym;
7151 asection *input_sec;
7153 /* If we see a common symbol, which implies a relocatable link, then
7154 if a symbol was small common in an input file, mark it as small
7155 common in the output file. */
7156 if (sym->st_shndx == SHN_COMMON
7157 && strcmp (input_sec->name, ".scommon") == 0)
7158 sym->st_shndx = SHN_MIPS_SCOMMON;
7160 if (sym->st_other == STO_MIPS16
7161 && (sym->st_value & 1) != 0)
7167 /* Functions for the dynamic linker. */
7169 /* The name of the dynamic interpreter. This is put in the .interp
7172 #define ELF_DYNAMIC_INTERPRETER(abfd) \
7173 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
7174 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
7175 : "/usr/lib/libc.so.1")
7177 /* Create dynamic sections when linking against a dynamic object. */
7180 _bfd_mips_elf_create_dynamic_sections (abfd, info)
7182 struct bfd_link_info *info;
7184 struct elf_link_hash_entry *h;
7186 register asection *s;
7187 const char * const *namep;
7189 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7190 | SEC_LINKER_CREATED | SEC_READONLY);
7192 /* Mips ABI requests the .dynamic section to be read only. */
7193 s = bfd_get_section_by_name (abfd, ".dynamic");
7196 if (! bfd_set_section_flags (abfd, s, flags))
7200 /* We need to create .got section. */
7201 if (! mips_elf_create_got_section (abfd, info))
7204 /* Create the .msym section on IRIX6. It is used by the dynamic
7205 linker to speed up dynamic relocations, and to avoid computing
7206 the ELF hash for symbols. */
7207 if (IRIX_COMPAT (abfd) == ict_irix6
7208 && !mips_elf_create_msym_section (abfd))
7211 /* Create .stub section. */
7212 if (bfd_get_section_by_name (abfd,
7213 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
7215 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
7217 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
7218 || ! bfd_set_section_alignment (abfd, s,
7219 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7223 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
7225 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
7227 s = bfd_make_section (abfd, ".rld_map");
7229 || ! bfd_set_section_flags (abfd, s, flags &~ (flagword) SEC_READONLY)
7230 || ! bfd_set_section_alignment (abfd, s,
7231 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7235 /* On IRIX5, we adjust add some additional symbols and change the
7236 alignments of several sections. There is no ABI documentation
7237 indicating that this is necessary on IRIX6, nor any evidence that
7238 the linker takes such action. */
7239 if (IRIX_COMPAT (abfd) == ict_irix5)
7241 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7244 if (! (_bfd_generic_link_add_one_symbol
7245 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
7246 (bfd_vma) 0, (const char *) NULL, false,
7247 get_elf_backend_data (abfd)->collect,
7248 (struct bfd_link_hash_entry **) &h)))
7250 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
7251 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7252 h->type = STT_SECTION;
7254 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7258 /* We need to create a .compact_rel section. */
7259 if (SGI_COMPAT (abfd))
7261 if (!mips_elf_create_compact_rel_section (abfd, info))
7265 /* Change aligments of some sections. */
7266 s = bfd_get_section_by_name (abfd, ".hash");
7268 bfd_set_section_alignment (abfd, s, 4);
7269 s = bfd_get_section_by_name (abfd, ".dynsym");
7271 bfd_set_section_alignment (abfd, s, 4);
7272 s = bfd_get_section_by_name (abfd, ".dynstr");
7274 bfd_set_section_alignment (abfd, s, 4);
7275 s = bfd_get_section_by_name (abfd, ".reginfo");
7277 bfd_set_section_alignment (abfd, s, 4);
7278 s = bfd_get_section_by_name (abfd, ".dynamic");
7280 bfd_set_section_alignment (abfd, s, 4);
7286 if (SGI_COMPAT (abfd))
7288 if (!(_bfd_generic_link_add_one_symbol
7289 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
7290 (bfd_vma) 0, (const char *) NULL, false,
7291 get_elf_backend_data (abfd)->collect,
7292 (struct bfd_link_hash_entry **) &h)))
7297 /* For normal mips it is _DYNAMIC_LINKING. */
7298 if (!(_bfd_generic_link_add_one_symbol
7299 (info, abfd, "_DYNAMIC_LINKING", BSF_GLOBAL,
7300 bfd_abs_section_ptr, (bfd_vma) 0, (const char *) NULL, false,
7301 get_elf_backend_data (abfd)->collect,
7302 (struct bfd_link_hash_entry **) &h)))
7305 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
7306 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7307 h->type = STT_SECTION;
7309 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7312 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7314 /* __rld_map is a four byte word located in the .data section
7315 and is filled in by the rtld to contain a pointer to
7316 the _r_debug structure. Its symbol value will be set in
7317 mips_elf_finish_dynamic_symbol. */
7318 s = bfd_get_section_by_name (abfd, ".rld_map");
7319 BFD_ASSERT (s != NULL);
7322 if (SGI_COMPAT (abfd))
7324 if (!(_bfd_generic_link_add_one_symbol
7325 (info, abfd, "__rld_map", BSF_GLOBAL, s,
7326 (bfd_vma) 0, (const char *) NULL, false,
7327 get_elf_backend_data (abfd)->collect,
7328 (struct bfd_link_hash_entry **) &h)))
7333 /* For normal mips the symbol is __RLD_MAP. */
7334 if (!(_bfd_generic_link_add_one_symbol
7335 (info, abfd, "__RLD_MAP", BSF_GLOBAL, s,
7336 (bfd_vma) 0, (const char *) NULL, false,
7337 get_elf_backend_data (abfd)->collect,
7338 (struct bfd_link_hash_entry **) &h)))
7341 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
7342 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7343 h->type = STT_OBJECT;
7345 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7353 /* Create the .compact_rel section. */
7356 mips_elf_create_compact_rel_section (abfd, info)
7358 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7361 register asection *s;
7363 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
7365 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
7368 s = bfd_make_section (abfd, ".compact_rel");
7370 || ! bfd_set_section_flags (abfd, s, flags)
7371 || ! bfd_set_section_alignment (abfd, s,
7372 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7375 s->_raw_size = sizeof (Elf32_External_compact_rel);
7381 /* Create the .got section to hold the global offset table. */
7384 mips_elf_create_got_section (abfd, info)
7386 struct bfd_link_info *info;
7389 register asection *s;
7390 struct elf_link_hash_entry *h;
7391 struct mips_got_info *g;
7394 /* This function may be called more than once. */
7395 if (mips_elf_got_section (abfd))
7398 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7399 | SEC_LINKER_CREATED);
7401 s = bfd_make_section (abfd, ".got");
7403 || ! bfd_set_section_flags (abfd, s, flags)
7404 || ! bfd_set_section_alignment (abfd, s, 4))
7407 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
7408 linker script because we don't want to define the symbol if we
7409 are not creating a global offset table. */
7411 if (! (_bfd_generic_link_add_one_symbol
7412 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
7413 (bfd_vma) 0, (const char *) NULL, false,
7414 get_elf_backend_data (abfd)->collect,
7415 (struct bfd_link_hash_entry **) &h)))
7417 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
7418 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7419 h->type = STT_OBJECT;
7422 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
7425 /* The first several global offset table entries are reserved. */
7426 s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
7428 amt = sizeof (struct mips_got_info);
7429 g = (struct mips_got_info *) bfd_alloc (abfd, amt);
7432 g->global_gotsym = NULL;
7433 g->local_gotno = MIPS_RESERVED_GOTNO;
7434 g->assigned_gotno = MIPS_RESERVED_GOTNO;
7435 if (elf_section_data (s) == NULL)
7437 amt = sizeof (struct bfd_elf_section_data);
7438 s->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
7439 if (elf_section_data (s) == NULL)
7442 elf_section_data (s)->tdata = (PTR) g;
7443 elf_section_data (s)->this_hdr.sh_flags
7444 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7449 /* Returns the .msym section for ABFD, creating it if it does not
7450 already exist. Returns NULL to indicate error. */
7453 mips_elf_create_msym_section (abfd)
7458 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7461 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7463 || !bfd_set_section_flags (abfd, s,
7467 | SEC_LINKER_CREATED
7469 || !bfd_set_section_alignment (abfd, s,
7470 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7477 /* Add room for N relocations to the .rel.dyn section in ABFD. */
7480 mips_elf_allocate_dynamic_relocations (abfd, n)
7486 s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd));
7487 BFD_ASSERT (s != NULL);
7489 if (s->_raw_size == 0)
7491 /* Make room for a null element. */
7492 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
7495 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
7498 /* Look through the relocs for a section during the first phase, and
7499 allocate space in the global offset table. */
7502 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
7504 struct bfd_link_info *info;
7506 const Elf_Internal_Rela *relocs;
7510 Elf_Internal_Shdr *symtab_hdr;
7511 struct elf_link_hash_entry **sym_hashes;
7512 struct mips_got_info *g;
7514 const Elf_Internal_Rela *rel;
7515 const Elf_Internal_Rela *rel_end;
7518 struct elf_backend_data *bed;
7520 if (info->relocateable)
7523 dynobj = elf_hash_table (info)->dynobj;
7524 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7525 sym_hashes = elf_sym_hashes (abfd);
7526 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7528 /* Check for the mips16 stub sections. */
7530 name = bfd_get_section_name (abfd, sec);
7531 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
7533 unsigned long r_symndx;
7535 /* Look at the relocation information to figure out which symbol
7538 r_symndx = ELF32_R_SYM (relocs->r_info);
7540 if (r_symndx < extsymoff
7541 || sym_hashes[r_symndx - extsymoff] == NULL)
7545 /* This stub is for a local symbol. This stub will only be
7546 needed if there is some relocation in this BFD, other
7547 than a 16 bit function call, which refers to this symbol. */
7548 for (o = abfd->sections; o != NULL; o = o->next)
7550 Elf_Internal_Rela *sec_relocs;
7551 const Elf_Internal_Rela *r, *rend;
7553 /* We can ignore stub sections when looking for relocs. */
7554 if ((o->flags & SEC_RELOC) == 0
7555 || o->reloc_count == 0
7556 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
7557 sizeof FN_STUB - 1) == 0
7558 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
7559 sizeof CALL_STUB - 1) == 0
7560 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
7561 sizeof CALL_FP_STUB - 1) == 0)
7564 sec_relocs = (_bfd_elf32_link_read_relocs
7565 (abfd, o, (PTR) NULL,
7566 (Elf_Internal_Rela *) NULL,
7567 info->keep_memory));
7568 if (sec_relocs == NULL)
7571 rend = sec_relocs + o->reloc_count;
7572 for (r = sec_relocs; r < rend; r++)
7573 if (ELF32_R_SYM (r->r_info) == r_symndx
7574 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
7577 if (! info->keep_memory)
7586 /* There is no non-call reloc for this stub, so we do
7587 not need it. Since this function is called before
7588 the linker maps input sections to output sections, we
7589 can easily discard it by setting the SEC_EXCLUDE
7591 sec->flags |= SEC_EXCLUDE;
7595 /* Record this stub in an array of local symbol stubs for
7597 if (elf_tdata (abfd)->local_stubs == NULL)
7599 unsigned long symcount;
7603 if (elf_bad_symtab (abfd))
7604 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7606 symcount = symtab_hdr->sh_info;
7607 amt = symcount * sizeof (asection *);
7608 n = (asection **) bfd_zalloc (abfd, amt);
7611 elf_tdata (abfd)->local_stubs = n;
7614 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7616 /* We don't need to set mips16_stubs_seen in this case.
7617 That flag is used to see whether we need to look through
7618 the global symbol table for stubs. We don't need to set
7619 it here, because we just have a local stub. */
7623 struct mips_elf_link_hash_entry *h;
7625 h = ((struct mips_elf_link_hash_entry *)
7626 sym_hashes[r_symndx - extsymoff]);
7628 /* H is the symbol this stub is for. */
7631 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7634 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
7635 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7637 unsigned long r_symndx;
7638 struct mips_elf_link_hash_entry *h;
7641 /* Look at the relocation information to figure out which symbol
7644 r_symndx = ELF32_R_SYM (relocs->r_info);
7646 if (r_symndx < extsymoff
7647 || sym_hashes[r_symndx - extsymoff] == NULL)
7649 /* This stub was actually built for a static symbol defined
7650 in the same file. We assume that all static symbols in
7651 mips16 code are themselves mips16, so we can simply
7652 discard this stub. Since this function is called before
7653 the linker maps input sections to output sections, we can
7654 easily discard it by setting the SEC_EXCLUDE flag. */
7655 sec->flags |= SEC_EXCLUDE;
7659 h = ((struct mips_elf_link_hash_entry *)
7660 sym_hashes[r_symndx - extsymoff]);
7662 /* H is the symbol this stub is for. */
7664 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7665 loc = &h->call_fp_stub;
7667 loc = &h->call_stub;
7669 /* If we already have an appropriate stub for this function, we
7670 don't need another one, so we can discard this one. Since
7671 this function is called before the linker maps input sections
7672 to output sections, we can easily discard it by setting the
7673 SEC_EXCLUDE flag. We can also discard this section if we
7674 happen to already know that this is a mips16 function; it is
7675 not necessary to check this here, as it is checked later, but
7676 it is slightly faster to check now. */
7677 if (*loc != NULL || h->root.other == STO_MIPS16)
7679 sec->flags |= SEC_EXCLUDE;
7684 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7694 sgot = mips_elf_got_section (dynobj);
7699 BFD_ASSERT (elf_section_data (sgot) != NULL);
7700 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7701 BFD_ASSERT (g != NULL);
7706 bed = get_elf_backend_data (abfd);
7707 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7708 for (rel = relocs; rel < rel_end; ++rel)
7710 unsigned long r_symndx;
7711 unsigned int r_type;
7712 struct elf_link_hash_entry *h;
7714 r_symndx = ELF32_R_SYM (rel->r_info);
7715 r_type = ELF32_R_TYPE (rel->r_info);
7717 if (r_symndx < extsymoff)
7719 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7721 if (abfd->my_archive)
7722 (*_bfd_error_handler)
7723 (_("%s(%s) Malformed reloc detected for section %s"),
7724 bfd_get_filename (abfd->my_archive),
7725 bfd_get_filename (abfd), name);
7727 (*_bfd_error_handler)
7728 (_("%s: Malformed reloc detected for section %s"),
7729 bfd_get_filename (abfd), name);
7730 bfd_set_error (bfd_error_bad_value);
7735 h = sym_hashes[r_symndx - extsymoff];
7737 /* This may be an indirect symbol created because of a version. */
7740 while (h->root.type == bfd_link_hash_indirect)
7741 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7745 /* Some relocs require a global offset table. */
7746 if (dynobj == NULL || sgot == NULL)
7752 case R_MIPS_CALL_HI16:
7753 case R_MIPS_CALL_LO16:
7754 case R_MIPS_GOT_HI16:
7755 case R_MIPS_GOT_LO16:
7756 case R_MIPS_GOT_PAGE:
7757 case R_MIPS_GOT_OFST:
7758 case R_MIPS_GOT_DISP:
7760 elf_hash_table (info)->dynobj = dynobj = abfd;
7761 if (! mips_elf_create_got_section (dynobj, info))
7763 g = mips_elf_got_info (dynobj, &sgot);
7770 && (info->shared || h != NULL)
7771 && (sec->flags & SEC_ALLOC) != 0)
7772 elf_hash_table (info)->dynobj = dynobj = abfd;
7780 if (!h && (r_type == R_MIPS_CALL_LO16
7781 || r_type == R_MIPS_GOT_LO16
7782 || r_type == R_MIPS_GOT_DISP))
7784 /* We may need a local GOT entry for this relocation. We
7785 don't count R_MIPS_GOT_PAGE because we can estimate the
7786 maximum number of pages needed by looking at the size of
7787 the segment. Similar comments apply to R_MIPS_GOT16 and
7788 R_MIPS_CALL16. We don't count R_MIPS_GOT_HI16, or
7789 R_MIPS_CALL_HI16 because these are always followed by an
7790 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
7792 This estimation is very conservative since we can merge
7793 duplicate entries in the GOT. In order to be less
7794 conservative, we could actually build the GOT here,
7795 rather than in relocate_section. */
7797 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
7805 (*_bfd_error_handler)
7806 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
7807 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
7808 bfd_set_error (bfd_error_bad_value);
7813 case R_MIPS_CALL_HI16:
7814 case R_MIPS_CALL_LO16:
7817 /* This symbol requires a global offset table entry. */
7818 if (!mips_elf_record_global_got_symbol (h, info, g))
7821 /* We need a stub, not a plt entry for the undefined
7822 function. But we record it as if it needs plt. See
7823 elf_adjust_dynamic_symbol in elflink.h. */
7824 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
7830 case R_MIPS_GOT_HI16:
7831 case R_MIPS_GOT_LO16:
7832 case R_MIPS_GOT_DISP:
7833 /* This symbol requires a global offset table entry. */
7834 if (h && !mips_elf_record_global_got_symbol (h, info, g))
7841 if ((info->shared || h != NULL)
7842 && (sec->flags & SEC_ALLOC) != 0)
7846 const char *dname = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj);
7848 sreloc = bfd_get_section_by_name (dynobj, dname);
7851 sreloc = bfd_make_section (dynobj, dname);
7853 || ! bfd_set_section_flags (dynobj, sreloc,
7858 | SEC_LINKER_CREATED
7860 || ! bfd_set_section_alignment (dynobj, sreloc,
7865 #define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
7868 /* When creating a shared object, we must copy these
7869 reloc types into the output file as R_MIPS_REL32
7870 relocs. We make room for this reloc in the
7871 .rel.dyn reloc section. */
7872 mips_elf_allocate_dynamic_relocations (dynobj, 1);
7873 if ((sec->flags & MIPS_READONLY_SECTION)
7874 == MIPS_READONLY_SECTION)
7875 /* We tell the dynamic linker that there are
7876 relocations against the text segment. */
7877 info->flags |= DF_TEXTREL;
7881 struct mips_elf_link_hash_entry *hmips;
7883 /* We only need to copy this reloc if the symbol is
7884 defined in a dynamic object. */
7885 hmips = (struct mips_elf_link_hash_entry *) h;
7886 ++hmips->possibly_dynamic_relocs;
7887 if ((sec->flags & MIPS_READONLY_SECTION)
7888 == MIPS_READONLY_SECTION)
7889 /* We need it to tell the dynamic linker if there
7890 are relocations against the text segment. */
7891 hmips->readonly_reloc = true;
7894 /* Even though we don't directly need a GOT entry for
7895 this symbol, a symbol must have a dynamic symbol
7896 table index greater that DT_MIPS_GOTSYM if there are
7897 dynamic relocations against it. */
7899 && !mips_elf_record_global_got_symbol (h, info, g))
7903 if (SGI_COMPAT (abfd))
7904 mips_elf_hash_table (info)->compact_rel_size +=
7905 sizeof (Elf32_External_crinfo);
7909 case R_MIPS_GPREL16:
7910 case R_MIPS_LITERAL:
7911 case R_MIPS_GPREL32:
7912 if (SGI_COMPAT (abfd))
7913 mips_elf_hash_table (info)->compact_rel_size +=
7914 sizeof (Elf32_External_crinfo);
7917 /* This relocation describes the C++ object vtable hierarchy.
7918 Reconstruct it for later use during GC. */
7919 case R_MIPS_GNU_VTINHERIT:
7920 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7924 /* This relocation describes which C++ vtable entries are actually
7925 used. Record for later use during GC. */
7926 case R_MIPS_GNU_VTENTRY:
7927 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7935 /* We must not create a stub for a symbol that has relocations
7936 related to taking the function's address. */
7942 struct mips_elf_link_hash_entry *mh;
7944 mh = (struct mips_elf_link_hash_entry *) h;
7945 mh->no_fn_stub = true;
7949 case R_MIPS_CALL_HI16:
7950 case R_MIPS_CALL_LO16:
7954 /* If this reloc is not a 16 bit call, and it has a global
7955 symbol, then we will need the fn_stub if there is one.
7956 References from a stub section do not count. */
7958 && r_type != R_MIPS16_26
7959 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
7960 sizeof FN_STUB - 1) != 0
7961 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
7962 sizeof CALL_STUB - 1) != 0
7963 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
7964 sizeof CALL_FP_STUB - 1) != 0)
7966 struct mips_elf_link_hash_entry *mh;
7968 mh = (struct mips_elf_link_hash_entry *) h;
7969 mh->need_fn_stub = true;
7976 /* Return the section that should be marked against GC for a given
7980 _bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
7982 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7983 Elf_Internal_Rela *rel;
7984 struct elf_link_hash_entry *h;
7985 Elf_Internal_Sym *sym;
7987 /* ??? Do mips16 stub sections need to be handled special? */
7991 switch (ELF32_R_TYPE (rel->r_info))
7993 case R_MIPS_GNU_VTINHERIT:
7994 case R_MIPS_GNU_VTENTRY:
7998 switch (h->root.type)
8000 case bfd_link_hash_defined:
8001 case bfd_link_hash_defweak:
8002 return h->root.u.def.section;
8004 case bfd_link_hash_common:
8005 return h->root.u.c.p->section;
8014 if (!(elf_bad_symtab (abfd)
8015 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8016 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
8017 && sym->st_shndx != SHN_COMMON))
8019 return bfd_section_from_elf_index (abfd, sym->st_shndx);
8026 /* Update the got entry reference counts for the section being removed. */
8029 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
8030 bfd *abfd ATTRIBUTE_UNUSED;
8031 struct bfd_link_info *info ATTRIBUTE_UNUSED;
8032 asection *sec ATTRIBUTE_UNUSED;
8033 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
8036 Elf_Internal_Shdr *symtab_hdr;
8037 struct elf_link_hash_entry **sym_hashes;
8038 bfd_signed_vma *local_got_refcounts;
8039 const Elf_Internal_Rela *rel, *relend;
8040 unsigned long r_symndx;
8041 struct elf_link_hash_entry *h;
8043 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8044 sym_hashes = elf_sym_hashes (abfd);
8045 local_got_refcounts = elf_local_got_refcounts (abfd);
8047 relend = relocs + sec->reloc_count;
8048 for (rel = relocs; rel < relend; rel++)
8049 switch (ELF32_R_TYPE (rel->r_info))
8053 case R_MIPS_CALL_HI16:
8054 case R_MIPS_CALL_LO16:
8055 case R_MIPS_GOT_HI16:
8056 case R_MIPS_GOT_LO16:
8057 /* ??? It would seem that the existing MIPS code does no sort
8058 of reference counting or whatnot on its GOT and PLT entries,
8059 so it is not possible to garbage collect them at this time. */
8070 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
8071 hiding the old indirect symbol. Process additional relocation
8075 _bfd_mips_elf_copy_indirect_symbol (dir, ind)
8076 struct elf_link_hash_entry *dir, *ind;
8078 struct mips_elf_link_hash_entry *dirmips, *indmips;
8080 _bfd_elf_link_hash_copy_indirect (dir, ind);
8082 dirmips = (struct mips_elf_link_hash_entry *) dir;
8083 indmips = (struct mips_elf_link_hash_entry *) ind;
8084 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
8085 if (indmips->readonly_reloc)
8086 dirmips->readonly_reloc = true;
8087 if (dirmips->min_dyn_reloc_index == 0
8088 || (indmips->min_dyn_reloc_index != 0
8089 && indmips->min_dyn_reloc_index < dirmips->min_dyn_reloc_index))
8090 dirmips->min_dyn_reloc_index = indmips->min_dyn_reloc_index;
8091 if (indmips->no_fn_stub)
8092 dirmips->no_fn_stub = true;
8095 /* Adjust a symbol defined by a dynamic object and referenced by a
8096 regular object. The current definition is in some section of the
8097 dynamic object, but we're not including those sections. We have to
8098 change the definition to something the rest of the link can
8102 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
8103 struct bfd_link_info *info;
8104 struct elf_link_hash_entry *h;
8107 struct mips_elf_link_hash_entry *hmips;
8110 dynobj = elf_hash_table (info)->dynobj;
8112 /* Make sure we know what is going on here. */
8113 BFD_ASSERT (dynobj != NULL
8114 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
8115 || h->weakdef != NULL
8116 || ((h->elf_link_hash_flags
8117 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
8118 && (h->elf_link_hash_flags
8119 & ELF_LINK_HASH_REF_REGULAR) != 0
8120 && (h->elf_link_hash_flags
8121 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
8123 /* If this symbol is defined in a dynamic object, we need to copy
8124 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
8126 hmips = (struct mips_elf_link_hash_entry *) h;
8127 if (! info->relocateable
8128 && hmips->possibly_dynamic_relocs != 0
8129 && (h->root.type == bfd_link_hash_defweak
8130 || (h->elf_link_hash_flags
8131 & ELF_LINK_HASH_DEF_REGULAR) == 0))
8133 mips_elf_allocate_dynamic_relocations (dynobj,
8134 hmips->possibly_dynamic_relocs);
8135 if (hmips->readonly_reloc)
8136 /* We tell the dynamic linker that there are relocations
8137 against the text segment. */
8138 info->flags |= DF_TEXTREL;
8141 /* For a function, create a stub, if allowed. */
8142 if (! hmips->no_fn_stub
8143 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
8145 if (! elf_hash_table (info)->dynamic_sections_created)
8148 /* If this symbol is not defined in a regular file, then set
8149 the symbol to the stub location. This is required to make
8150 function pointers compare as equal between the normal
8151 executable and the shared library. */
8152 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
8154 /* We need .stub section. */
8155 s = bfd_get_section_by_name (dynobj,
8156 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8157 BFD_ASSERT (s != NULL);
8159 h->root.u.def.section = s;
8160 h->root.u.def.value = s->_raw_size;
8162 /* XXX Write this stub address somewhere. */
8163 h->plt.offset = s->_raw_size;
8165 /* Make room for this stub code. */
8166 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
8168 /* The last half word of the stub will be filled with the index
8169 of this symbol in .dynsym section. */
8173 else if ((h->type == STT_FUNC)
8174 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
8176 /* This will set the entry for this symbol in the GOT to 0, and
8177 the dynamic linker will take care of this. */
8178 h->root.u.def.value = 0;
8182 /* If this is a weak symbol, and there is a real definition, the
8183 processor independent code will have arranged for us to see the
8184 real definition first, and we can just use the same value. */
8185 if (h->weakdef != NULL)
8187 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
8188 || h->weakdef->root.type == bfd_link_hash_defweak);
8189 h->root.u.def.section = h->weakdef->root.u.def.section;
8190 h->root.u.def.value = h->weakdef->root.u.def.value;
8194 /* This is a reference to a symbol defined by a dynamic object which
8195 is not a function. */
8200 /* This function is called after all the input files have been read,
8201 and the input sections have been assigned to output sections. We
8202 check for any mips16 stub sections that we can discard. */
8204 static boolean mips_elf_check_mips16_stubs
8205 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
8208 _bfd_mips_elf_always_size_sections (output_bfd, info)
8210 struct bfd_link_info *info;
8214 /* The .reginfo section has a fixed size. */
8215 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8217 bfd_set_section_size (output_bfd, ri,
8218 (bfd_size_type) sizeof (Elf32_External_RegInfo));
8220 if (info->relocateable
8221 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
8224 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8225 mips_elf_check_mips16_stubs,
8231 /* Check the mips16 stubs for a particular symbol, and see if we can
8235 mips_elf_check_mips16_stubs (h, data)
8236 struct mips_elf_link_hash_entry *h;
8237 PTR data ATTRIBUTE_UNUSED;
8239 if (h->fn_stub != NULL
8240 && ! h->need_fn_stub)
8242 /* We don't need the fn_stub; the only references to this symbol
8243 are 16 bit calls. Clobber the size to 0 to prevent it from
8244 being included in the link. */
8245 h->fn_stub->_raw_size = 0;
8246 h->fn_stub->_cooked_size = 0;
8247 h->fn_stub->flags &= ~SEC_RELOC;
8248 h->fn_stub->reloc_count = 0;
8249 h->fn_stub->flags |= SEC_EXCLUDE;
8252 if (h->call_stub != NULL
8253 && h->root.other == STO_MIPS16)
8255 /* We don't need the call_stub; this is a 16 bit function, so
8256 calls from other 16 bit functions are OK. Clobber the size
8257 to 0 to prevent it from being included in the link. */
8258 h->call_stub->_raw_size = 0;
8259 h->call_stub->_cooked_size = 0;
8260 h->call_stub->flags &= ~SEC_RELOC;
8261 h->call_stub->reloc_count = 0;
8262 h->call_stub->flags |= SEC_EXCLUDE;
8265 if (h->call_fp_stub != NULL
8266 && h->root.other == STO_MIPS16)
8268 /* We don't need the call_stub; this is a 16 bit function, so
8269 calls from other 16 bit functions are OK. Clobber the size
8270 to 0 to prevent it from being included in the link. */
8271 h->call_fp_stub->_raw_size = 0;
8272 h->call_fp_stub->_cooked_size = 0;
8273 h->call_fp_stub->flags &= ~SEC_RELOC;
8274 h->call_fp_stub->reloc_count = 0;
8275 h->call_fp_stub->flags |= SEC_EXCLUDE;
8281 /* Set the sizes of the dynamic sections. */
8284 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
8286 struct bfd_link_info *info;
8291 struct mips_got_info *g = NULL;
8293 dynobj = elf_hash_table (info)->dynobj;
8294 BFD_ASSERT (dynobj != NULL);
8296 if (elf_hash_table (info)->dynamic_sections_created)
8298 /* Set the contents of the .interp section to the interpreter. */
8301 s = bfd_get_section_by_name (dynobj, ".interp");
8302 BFD_ASSERT (s != NULL);
8304 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8306 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8310 /* The check_relocs and adjust_dynamic_symbol entry points have
8311 determined the sizes of the various dynamic sections. Allocate
8314 for (s = dynobj->sections; s != NULL; s = s->next)
8319 /* It's OK to base decisions on the section name, because none
8320 of the dynobj section names depend upon the input files. */
8321 name = bfd_get_section_name (dynobj, s);
8323 if ((s->flags & SEC_LINKER_CREATED) == 0)
8328 if (strncmp (name, ".rel", 4) == 0)
8330 if (s->_raw_size == 0)
8332 /* We only strip the section if the output section name
8333 has the same name. Otherwise, there might be several
8334 input sections for this output section. FIXME: This
8335 code is probably not needed these days anyhow, since
8336 the linker now does not create empty output sections. */
8337 if (s->output_section != NULL
8339 bfd_get_section_name (s->output_section->owner,
8340 s->output_section)) == 0)
8345 const char *outname;
8348 /* If this relocation section applies to a read only
8349 section, then we probably need a DT_TEXTREL entry.
8350 If the relocation section is .rel.dyn, we always
8351 assert a DT_TEXTREL entry rather than testing whether
8352 there exists a relocation to a read only section or
8354 outname = bfd_get_section_name (output_bfd,
8356 target = bfd_get_section_by_name (output_bfd, outname + 4);
8358 && (target->flags & SEC_READONLY) != 0
8359 && (target->flags & SEC_ALLOC) != 0)
8361 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0)
8364 /* We use the reloc_count field as a counter if we need
8365 to copy relocs into the output file. */
8367 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0)
8371 else if (strncmp (name, ".got", 4) == 0)
8374 bfd_size_type loadable_size = 0;
8375 bfd_size_type local_gotno;
8378 BFD_ASSERT (elf_section_data (s) != NULL);
8379 g = (struct mips_got_info *) elf_section_data (s)->tdata;
8380 BFD_ASSERT (g != NULL);
8382 /* Calculate the total loadable size of the output. That
8383 will give us the maximum number of GOT_PAGE entries
8385 for (sub = info->input_bfds; sub; sub = sub->link_next)
8387 asection *subsection;
8389 for (subsection = sub->sections;
8391 subsection = subsection->next)
8393 if ((subsection->flags & SEC_ALLOC) == 0)
8395 loadable_size += ((subsection->_raw_size + 0xf)
8396 &~ (bfd_size_type) 0xf);
8399 loadable_size += MIPS_FUNCTION_STUB_SIZE;
8401 /* Assume there are two loadable segments consisting of
8402 contiguous sections. Is 5 enough? */
8403 local_gotno = (loadable_size >> 16) + 5;
8404 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8405 /* It's possible we will need GOT_PAGE entries as well as
8406 GOT16 entries. Often, these will be able to share GOT
8407 entries, but not always. */
8410 g->local_gotno += local_gotno;
8411 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
8413 /* There has to be a global GOT entry for every symbol with
8414 a dynamic symbol table index of DT_MIPS_GOTSYM or
8415 higher. Therefore, it make sense to put those symbols
8416 that need GOT entries at the end of the symbol table. We
8418 if (!mips_elf_sort_hash_table (info, 1))
8421 if (g->global_gotsym != NULL)
8422 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
8424 /* If there are no global symbols, or none requiring
8425 relocations, then GLOBAL_GOTSYM will be NULL. */
8427 g->global_gotno = i;
8428 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
8430 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
8432 /* Irix rld assumes that the function stub isn't at the end
8433 of .text section. So put a dummy. XXX */
8434 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
8436 else if (! info->shared
8437 && ! mips_elf_hash_table (info)->use_rld_obj_head
8438 && strncmp (name, ".rld_map", 8) == 0)
8440 /* We add a room for __rld_map. It will be filled in by the
8441 rtld to contain a pointer to the _r_debug structure. */
8444 else if (SGI_COMPAT (output_bfd)
8445 && strncmp (name, ".compact_rel", 12) == 0)
8446 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
8447 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
8449 s->_raw_size = (sizeof (Elf32_External_Msym)
8450 * (elf_hash_table (info)->dynsymcount
8451 + bfd_count_sections (output_bfd)));
8452 else if (strncmp (name, ".init", 5) != 0)
8454 /* It's not one of our sections, so don't allocate space. */
8460 _bfd_strip_section_from_output (info, s);
8464 /* Allocate memory for the section contents. */
8465 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
8466 if (s->contents == NULL && s->_raw_size != 0)
8468 bfd_set_error (bfd_error_no_memory);
8473 if (elf_hash_table (info)->dynamic_sections_created)
8475 /* Add some entries to the .dynamic section. We fill in the
8476 values later, in elf_mips_finish_dynamic_sections, but we
8477 must add the entries now so that we get the correct size for
8478 the .dynamic section. The DT_DEBUG entry is filled in by the
8479 dynamic linker and used by the debugger. */
8482 /* SGI object has the equivalence of DT_DEBUG in the
8483 DT_MIPS_RLD_MAP entry. */
8484 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8486 if (!SGI_COMPAT (output_bfd))
8488 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8494 /* Shared libraries on traditional mips have DT_DEBUG. */
8495 if (!SGI_COMPAT (output_bfd))
8497 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8502 if (reltext && SGI_COMPAT (output_bfd))
8503 info->flags |= DF_TEXTREL;
8505 if ((info->flags & DF_TEXTREL) != 0)
8507 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8511 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8514 if (bfd_get_section_by_name (dynobj,
8515 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)))
8517 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8520 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8523 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8527 if (SGI_COMPAT (output_bfd))
8529 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
8533 if (SGI_COMPAT (output_bfd))
8535 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
8539 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
8541 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
8544 s = bfd_get_section_by_name (dynobj, ".liblist");
8545 BFD_ASSERT (s != NULL);
8547 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
8551 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8554 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8558 /* Time stamps in executable files are a bad idea. */
8559 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
8564 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
8569 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
8573 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8576 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8579 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8582 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8585 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8588 if (IRIX_COMPAT (dynobj) == ict_irix5
8589 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8592 if (IRIX_COMPAT (dynobj) == ict_irix6
8593 && (bfd_get_section_by_name
8594 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8595 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8598 if (bfd_get_section_by_name (dynobj,
8599 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
8600 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
8607 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8608 adjust it appropriately now. */
8611 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
8612 bfd *abfd ATTRIBUTE_UNUSED;
8614 Elf_Internal_Sym *sym;
8616 /* The linker script takes care of providing names and values for
8617 these, but we must place them into the right sections. */
8618 static const char* const text_section_symbols[] = {
8621 "__dso_displacement",
8623 "__program_header_table",
8627 static const char* const data_section_symbols[] = {
8635 const char* const *p;
8638 for (i = 0; i < 2; ++i)
8639 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8642 if (strcmp (*p, name) == 0)
8644 /* All of these symbols are given type STT_SECTION by the
8646 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8648 /* The IRIX linker puts these symbols in special sections. */
8650 sym->st_shndx = SHN_MIPS_TEXT;
8652 sym->st_shndx = SHN_MIPS_DATA;
8658 /* Finish up dynamic symbol handling. We set the contents of various
8659 dynamic sections here. */
8662 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8664 struct bfd_link_info *info;
8665 struct elf_link_hash_entry *h;
8666 Elf_Internal_Sym *sym;
8672 struct mips_got_info *g;
8674 struct mips_elf_link_hash_entry *mh;
8676 dynobj = elf_hash_table (info)->dynobj;
8677 gval = sym->st_value;
8678 mh = (struct mips_elf_link_hash_entry *) h;
8680 if (h->plt.offset != (bfd_vma) -1)
8684 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
8686 /* This symbol has a stub. Set it up. */
8688 BFD_ASSERT (h->dynindx != -1);
8690 s = bfd_get_section_by_name (dynobj,
8691 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8692 BFD_ASSERT (s != NULL);
8694 /* Fill the stub. */
8696 bfd_put_32 (output_bfd, (bfd_vma) STUB_LW (output_bfd), p);
8698 bfd_put_32 (output_bfd, (bfd_vma) STUB_MOVE (output_bfd), p);
8701 /* FIXME: Can h->dynindex be more than 64K? */
8702 if (h->dynindx & 0xffff0000)
8705 bfd_put_32 (output_bfd, (bfd_vma) STUB_JALR, p);
8707 bfd_put_32 (output_bfd, (bfd_vma) STUB_LI16 (output_bfd) + h->dynindx, p);
8709 BFD_ASSERT (h->plt.offset <= s->_raw_size);
8710 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
8712 /* Mark the symbol as undefined. plt.offset != -1 occurs
8713 only for the referenced symbol. */
8714 sym->st_shndx = SHN_UNDEF;
8716 /* The run-time linker uses the st_value field of the symbol
8717 to reset the global offset table entry for this external
8718 to its stub address when unlinking a shared object. */
8719 gval = s->output_section->vma + s->output_offset + h->plt.offset;
8720 sym->st_value = gval;
8723 BFD_ASSERT (h->dynindx != -1
8724 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0);
8726 sgot = mips_elf_got_section (dynobj);
8727 BFD_ASSERT (sgot != NULL);
8728 BFD_ASSERT (elf_section_data (sgot) != NULL);
8729 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8730 BFD_ASSERT (g != NULL);
8732 /* Run through the global symbol table, creating GOT entries for all
8733 the symbols that need them. */
8734 if (g->global_gotsym != NULL
8735 && h->dynindx >= g->global_gotsym->dynindx)
8741 value = sym->st_value;
8744 /* For an entity defined in a shared object, this will be
8745 NULL. (For functions in shared objects for
8746 which we have created stubs, ST_VALUE will be non-NULL.
8747 That's because such the functions are now no longer defined
8748 in a shared object.) */
8750 if (info->shared && h->root.type == bfd_link_hash_undefined)
8753 value = h->root.u.def.value;
8755 offset = mips_elf_global_got_index (dynobj, h);
8756 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8759 /* Create a .msym entry, if appropriate. */
8760 smsym = bfd_get_section_by_name (dynobj,
8761 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8764 Elf32_Internal_Msym msym;
8766 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
8767 /* It is undocumented what the `1' indicates, but IRIX6 uses
8769 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
8770 bfd_mips_elf_swap_msym_out
8772 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
8775 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8776 name = h->root.root.string;
8777 if (strcmp (name, "_DYNAMIC") == 0
8778 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
8779 sym->st_shndx = SHN_ABS;
8780 else if (strcmp (name, "_DYNAMIC_LINK") == 0
8781 || strcmp (name, "_DYNAMIC_LINKING") == 0)
8783 sym->st_shndx = SHN_ABS;
8784 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8787 else if (strcmp (name, "_gp_disp") == 0)
8789 sym->st_shndx = SHN_ABS;
8790 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8791 sym->st_value = elf_gp (output_bfd);
8793 else if (SGI_COMPAT (output_bfd))
8795 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8796 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8798 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8799 sym->st_other = STO_PROTECTED;
8801 sym->st_shndx = SHN_MIPS_DATA;
8803 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8805 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8806 sym->st_other = STO_PROTECTED;
8807 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8808 sym->st_shndx = SHN_ABS;
8810 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8812 if (h->type == STT_FUNC)
8813 sym->st_shndx = SHN_MIPS_TEXT;
8814 else if (h->type == STT_OBJECT)
8815 sym->st_shndx = SHN_MIPS_DATA;
8819 /* Handle the IRIX6-specific symbols. */
8820 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8821 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8825 if (! mips_elf_hash_table (info)->use_rld_obj_head
8826 && (strcmp (name, "__rld_map") == 0
8827 || strcmp (name, "__RLD_MAP") == 0))
8829 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8830 BFD_ASSERT (s != NULL);
8831 sym->st_value = s->output_section->vma + s->output_offset;
8832 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
8833 if (mips_elf_hash_table (info)->rld_value == 0)
8834 mips_elf_hash_table (info)->rld_value = sym->st_value;
8836 else if (mips_elf_hash_table (info)->use_rld_obj_head
8837 && strcmp (name, "__rld_obj_head") == 0)
8839 /* IRIX6 does not use a .rld_map section. */
8840 if (IRIX_COMPAT (output_bfd) == ict_irix5
8841 || IRIX_COMPAT (output_bfd) == ict_none)
8842 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8844 mips_elf_hash_table (info)->rld_value = sym->st_value;
8848 /* If this is a mips16 symbol, force the value to be even. */
8849 if (sym->st_other == STO_MIPS16
8850 && (sym->st_value & 1) != 0)
8856 /* Finish up the dynamic sections. */
8859 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
8861 struct bfd_link_info *info;
8866 struct mips_got_info *g;
8868 dynobj = elf_hash_table (info)->dynobj;
8870 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8872 sgot = mips_elf_got_section (dynobj);
8877 BFD_ASSERT (elf_section_data (sgot) != NULL);
8878 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8879 BFD_ASSERT (g != NULL);
8882 if (elf_hash_table (info)->dynamic_sections_created)
8886 BFD_ASSERT (sdyn != NULL);
8887 BFD_ASSERT (g != NULL);
8889 for (b = sdyn->contents;
8890 b < sdyn->contents + sdyn->_raw_size;
8891 b += MIPS_ELF_DYN_SIZE (dynobj))
8893 Elf_Internal_Dyn dyn;
8899 /* Read in the current dynamic entry. */
8900 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
8902 /* Assume that we're going to modify it and write it out. */
8908 s = (bfd_get_section_by_name
8910 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)));
8911 BFD_ASSERT (s != NULL);
8912 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
8916 /* Rewrite DT_STRSZ. */
8918 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
8924 case DT_MIPS_CONFLICT:
8927 case DT_MIPS_LIBLIST:
8930 s = bfd_get_section_by_name (output_bfd, name);
8931 BFD_ASSERT (s != NULL);
8932 dyn.d_un.d_ptr = s->vma;
8935 case DT_MIPS_RLD_VERSION:
8936 dyn.d_un.d_val = 1; /* XXX */
8940 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
8943 case DT_MIPS_CONFLICTNO:
8945 elemsize = sizeof (Elf32_Conflict);
8948 case DT_MIPS_LIBLISTNO:
8950 elemsize = sizeof (Elf32_Lib);
8952 s = bfd_get_section_by_name (output_bfd, name);
8955 if (s->_cooked_size != 0)
8956 dyn.d_un.d_val = s->_cooked_size / elemsize;
8958 dyn.d_un.d_val = s->_raw_size / elemsize;
8964 case DT_MIPS_TIME_STAMP:
8965 time ((time_t *) &dyn.d_un.d_val);
8968 case DT_MIPS_ICHECKSUM:
8973 case DT_MIPS_IVERSION:
8978 case DT_MIPS_BASE_ADDRESS:
8979 s = output_bfd->sections;
8980 BFD_ASSERT (s != NULL);
8981 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
8984 case DT_MIPS_LOCAL_GOTNO:
8985 dyn.d_un.d_val = g->local_gotno;
8988 case DT_MIPS_UNREFEXTNO:
8989 /* The index into the dynamic symbol table which is the
8990 entry of the first external symbol that is not
8991 referenced within the same object. */
8992 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8995 case DT_MIPS_GOTSYM:
8996 if (g->global_gotsym)
8998 dyn.d_un.d_val = g->global_gotsym->dynindx;
9001 /* In case if we don't have global got symbols we default
9002 to setting DT_MIPS_GOTSYM to the same value as
9003 DT_MIPS_SYMTABNO, so we just fall through. */
9005 case DT_MIPS_SYMTABNO:
9007 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
9008 s = bfd_get_section_by_name (output_bfd, name);
9009 BFD_ASSERT (s != NULL);
9011 if (s->_cooked_size != 0)
9012 dyn.d_un.d_val = s->_cooked_size / elemsize;
9014 dyn.d_un.d_val = s->_raw_size / elemsize;
9017 case DT_MIPS_HIPAGENO:
9018 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
9021 case DT_MIPS_RLD_MAP:
9022 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
9025 case DT_MIPS_OPTIONS:
9026 s = (bfd_get_section_by_name
9027 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
9028 dyn.d_un.d_ptr = s->vma;
9032 s = (bfd_get_section_by_name
9033 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
9034 dyn.d_un.d_ptr = s->vma;
9043 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
9048 /* The first entry of the global offset table will be filled at
9049 runtime. The second entry will be used by some runtime loaders.
9050 This isn't the case of Irix rld. */
9051 if (sgot != NULL && sgot->_raw_size > 0)
9053 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
9054 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
9055 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
9059 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
9060 = MIPS_ELF_GOT_SIZE (output_bfd);
9065 Elf32_compact_rel cpt;
9067 /* ??? The section symbols for the output sections were set up in
9068 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
9069 symbols. Should we do so? */
9071 smsym = bfd_get_section_by_name (dynobj,
9072 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
9075 Elf32_Internal_Msym msym;
9077 msym.ms_hash_value = 0;
9078 msym.ms_info = ELF32_MS_INFO (0, 1);
9080 for (s = output_bfd->sections; s != NULL; s = s->next)
9082 long dynindx = elf_section_data (s)->dynindx;
9084 bfd_mips_elf_swap_msym_out
9086 (((Elf32_External_Msym *) smsym->contents)
9091 if (SGI_COMPAT (output_bfd))
9093 /* Write .compact_rel section out. */
9094 s = bfd_get_section_by_name (dynobj, ".compact_rel");
9098 cpt.num = s->reloc_count;
9100 cpt.offset = (s->output_section->filepos
9101 + sizeof (Elf32_External_compact_rel));
9104 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
9105 ((Elf32_External_compact_rel *)
9108 /* Clean up a dummy stub function entry in .text. */
9109 s = bfd_get_section_by_name (dynobj,
9110 MIPS_ELF_STUB_SECTION_NAME (dynobj));
9113 file_ptr dummy_offset;
9115 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
9116 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
9117 memset (s->contents + dummy_offset, 0,
9118 MIPS_FUNCTION_STUB_SIZE);
9123 /* We need to sort the entries of the dynamic relocation section. */
9125 if (!ABI_64_P (output_bfd))
9129 reldyn = bfd_get_section_by_name (dynobj,
9130 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
9131 if (reldyn != NULL && reldyn->reloc_count > 2)
9133 reldyn_sorting_bfd = output_bfd;
9134 qsort ((Elf32_External_Rel *) reldyn->contents + 1,
9135 (size_t) reldyn->reloc_count - 1,
9136 sizeof (Elf32_External_Rel), sort_dynamic_relocs);
9140 /* Clean up a first relocation in .rel.dyn. */
9141 s = bfd_get_section_by_name (dynobj,
9142 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
9143 if (s != NULL && s->_raw_size > 0)
9144 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
9150 /* Support for core dump NOTE sections */
9152 _bfd_elf32_mips_grok_prstatus (abfd, note)
9154 Elf_Internal_Note *note;
9157 unsigned int raw_size;
9159 switch (note->descsz)
9164 case 256: /* Linux/MIPS */
9166 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
9169 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
9178 /* Make a ".reg/999" section. */
9179 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9180 raw_size, note->descpos + offset);
9184 _bfd_elf32_mips_grok_psinfo (abfd, note)
9186 Elf_Internal_Note *note;
9188 switch (note->descsz)
9193 case 128: /* Linux/MIPS elf_prpsinfo */
9194 elf_tdata (abfd)->core_program
9195 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
9196 elf_tdata (abfd)->core_command
9197 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
9200 /* Note that for some reason, a spurious space is tacked
9201 onto the end of the args in some (at least one anyway)
9202 implementations, so strip it off if it exists. */
9205 char *command = elf_tdata (abfd)->core_command;
9206 int n = strlen (command);
9208 if (0 < n && command[n - 1] == ' ')
9209 command[n - 1] = '\0';
9215 /* This is almost identical to bfd_generic_get_... except that some
9216 MIPS relocations need to be handled specially. Sigh. */
9219 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
9220 relocateable, symbols)
9222 struct bfd_link_info *link_info;
9223 struct bfd_link_order *link_order;
9225 boolean relocateable;
9228 /* Get enough memory to hold the stuff */
9229 bfd *input_bfd = link_order->u.indirect.section->owner;
9230 asection *input_section = link_order->u.indirect.section;
9232 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
9233 arelent **reloc_vector = NULL;
9239 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
9240 if (reloc_vector == NULL && reloc_size != 0)
9243 /* read in the section */
9244 if (!bfd_get_section_contents (input_bfd,
9248 input_section->_raw_size))
9251 /* We're not relaxing the section, so just copy the size info */
9252 input_section->_cooked_size = input_section->_raw_size;
9253 input_section->reloc_done = true;
9255 reloc_count = bfd_canonicalize_reloc (input_bfd,
9259 if (reloc_count < 0)
9262 if (reloc_count > 0)
9267 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
9270 struct bfd_hash_entry *h;
9271 struct bfd_link_hash_entry *lh;
9272 /* Skip all this stuff if we aren't mixing formats. */
9273 if (abfd && input_bfd
9274 && abfd->xvec == input_bfd->xvec)
9278 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
9279 lh = (struct bfd_link_hash_entry *) h;
9286 case bfd_link_hash_undefined:
9287 case bfd_link_hash_undefweak:
9288 case bfd_link_hash_common:
9291 case bfd_link_hash_defined:
9292 case bfd_link_hash_defweak:
9294 gp = lh->u.def.value;
9296 case bfd_link_hash_indirect:
9297 case bfd_link_hash_warning:
9299 /* @@FIXME ignoring warning for now */
9301 case bfd_link_hash_new:
9310 for (parent = reloc_vector; *parent != (arelent *) NULL;
9313 char *error_message = (char *) NULL;
9314 bfd_reloc_status_type r;
9316 /* Specific to MIPS: Deal with relocation types that require
9317 knowing the gp of the output bfd. */
9318 asymbol *sym = *(*parent)->sym_ptr_ptr;
9319 if (bfd_is_abs_section (sym->section) && abfd)
9321 /* The special_function wouldn't get called anyways. */
9325 /* The gp isn't there; let the special function code
9326 fall over on its own. */
9328 else if ((*parent)->howto->special_function
9329 == _bfd_mips_elf_gprel16_reloc)
9331 /* bypass special_function call */
9332 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
9333 relocateable, (PTR) data, gp);
9334 goto skip_bfd_perform_relocation;
9336 /* end mips specific stuff */
9338 r = bfd_perform_relocation (input_bfd,
9342 relocateable ? abfd : (bfd *) NULL,
9344 skip_bfd_perform_relocation:
9348 asection *os = input_section->output_section;
9350 /* A partial link, so keep the relocs */
9351 os->orelocation[os->reloc_count] = *parent;
9355 if (r != bfd_reloc_ok)
9359 case bfd_reloc_undefined:
9360 if (!((*link_info->callbacks->undefined_symbol)
9361 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
9362 input_bfd, input_section, (*parent)->address,
9366 case bfd_reloc_dangerous:
9367 BFD_ASSERT (error_message != (char *) NULL);
9368 if (!((*link_info->callbacks->reloc_dangerous)
9369 (link_info, error_message, input_bfd, input_section,
9370 (*parent)->address)))
9373 case bfd_reloc_overflow:
9374 if (!((*link_info->callbacks->reloc_overflow)
9375 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
9376 (*parent)->howto->name, (*parent)->addend,
9377 input_bfd, input_section, (*parent)->address)))
9380 case bfd_reloc_outofrange:
9389 if (reloc_vector != NULL)
9390 free (reloc_vector);
9394 if (reloc_vector != NULL)
9395 free (reloc_vector);
9399 #define bfd_elf32_bfd_get_relocated_section_contents \
9400 elf32_mips_get_relocated_section_contents
9402 /* ECOFF swapping routines. These are used when dealing with the
9403 .mdebug section, which is in the ECOFF debugging format. */
9404 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
9405 /* Symbol table magic number. */
9407 /* Alignment of debugging information. E.g., 4. */
9409 /* Sizes of external symbolic information. */
9410 sizeof (struct hdr_ext),
9411 sizeof (struct dnr_ext),
9412 sizeof (struct pdr_ext),
9413 sizeof (struct sym_ext),
9414 sizeof (struct opt_ext),
9415 sizeof (struct fdr_ext),
9416 sizeof (struct rfd_ext),
9417 sizeof (struct ext_ext),
9418 /* Functions to swap in external symbolic data. */
9427 _bfd_ecoff_swap_tir_in,
9428 _bfd_ecoff_swap_rndx_in,
9429 /* Functions to swap out external symbolic data. */
9438 _bfd_ecoff_swap_tir_out,
9439 _bfd_ecoff_swap_rndx_out,
9440 /* Function to read in symbolic data. */
9441 _bfd_mips_elf_read_ecoff_info
9444 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
9445 #define TARGET_LITTLE_NAME "elf32-littlemips"
9446 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
9447 #define TARGET_BIG_NAME "elf32-bigmips"
9448 #define ELF_ARCH bfd_arch_mips
9449 #define ELF_MACHINE_CODE EM_MIPS
9451 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
9452 a value of 0x1000, and we are compatible. */
9453 #define ELF_MAXPAGESIZE 0x1000
9455 #define elf_backend_collect true
9456 #define elf_backend_type_change_ok true
9457 #define elf_backend_can_gc_sections true
9458 #define elf_backend_sign_extend_vma true
9459 #define elf_info_to_howto mips_info_to_howto_rela
9460 #define elf_info_to_howto_rel mips_info_to_howto_rel
9461 #define elf_backend_sym_is_global mips_elf_sym_is_global
9462 #define elf_backend_object_p _bfd_mips_elf_object_p
9463 #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
9464 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
9465 #define elf_backend_section_from_bfd_section \
9466 _bfd_mips_elf_section_from_bfd_section
9467 #define elf_backend_section_processing _bfd_mips_elf_section_processing
9468 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
9469 #define elf_backend_additional_program_headers \
9470 _bfd_mips_elf_additional_program_headers
9471 #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
9472 #define elf_backend_final_write_processing \
9473 _bfd_mips_elf_final_write_processing
9474 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
9475 #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
9476 #define elf_backend_create_dynamic_sections \
9477 _bfd_mips_elf_create_dynamic_sections
9478 #define elf_backend_check_relocs _bfd_mips_elf_check_relocs
9479 #define elf_backend_adjust_dynamic_symbol \
9480 _bfd_mips_elf_adjust_dynamic_symbol
9481 #define elf_backend_always_size_sections \
9482 _bfd_mips_elf_always_size_sections
9483 #define elf_backend_size_dynamic_sections \
9484 _bfd_mips_elf_size_dynamic_sections
9485 #define elf_backend_relocate_section _bfd_mips_elf_relocate_section
9486 #define elf_backend_link_output_symbol_hook \
9487 _bfd_mips_elf_link_output_symbol_hook
9488 #define elf_backend_finish_dynamic_symbol \
9489 _bfd_mips_elf_finish_dynamic_symbol
9490 #define elf_backend_finish_dynamic_sections \
9491 _bfd_mips_elf_finish_dynamic_sections
9492 #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
9493 #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
9495 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
9496 #define elf_backend_plt_header_size 0
9498 #define elf_backend_copy_indirect_symbol \
9499 _bfd_mips_elf_copy_indirect_symbol
9501 #define elf_backend_hide_symbol _bfd_mips_elf_hide_symbol
9502 #define elf_backend_grok_prstatus _bfd_elf32_mips_grok_prstatus
9503 #define elf_backend_grok_psinfo _bfd_elf32_mips_grok_psinfo
9505 #define bfd_elf32_bfd_is_local_label_name \
9506 mips_elf_is_local_label_name
9507 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
9508 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
9509 #define bfd_elf32_bfd_link_hash_table_create \
9510 _bfd_mips_elf_link_hash_table_create
9511 #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
9512 #define bfd_elf32_bfd_copy_private_bfd_data \
9513 _bfd_mips_elf_copy_private_bfd_data
9514 #define bfd_elf32_bfd_merge_private_bfd_data \
9515 _bfd_mips_elf_merge_private_bfd_data
9516 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
9517 #define bfd_elf32_bfd_print_private_bfd_data \
9518 _bfd_mips_elf_print_private_bfd_data
9519 #include "elf32-target.h"
9521 /* Support for traditional mips targets */
9523 #define INCLUDED_TARGET_FILE /* More a type of flag */
9525 #undef TARGET_LITTLE_SYM
9526 #undef TARGET_LITTLE_NAME
9527 #undef TARGET_BIG_SYM
9528 #undef TARGET_BIG_NAME
9530 #define TARGET_LITTLE_SYM bfd_elf32_tradlittlemips_vec
9531 #define TARGET_LITTLE_NAME "elf32-tradlittlemips"
9532 #define TARGET_BIG_SYM bfd_elf32_tradbigmips_vec
9533 #define TARGET_BIG_NAME "elf32-tradbigmips"
9535 /* Include the target file again for this target */
9536 #include "elf32-target.h"