* elf32-mips.c (_bfd_mips_elf_check_relocs): Don't allocate local
[binutils.git] / bfd / elf32-mips.c
blob3d4ddf5ffac49a8da7f329fa11eaf941c500ba4a
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark@codesourcery.com>
9 This file is part of BFD, the Binary File Descriptor library.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
26 different MIPS ELF from other targets. This matters when linking.
27 This file supports both, switching at runtime. */
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "libbfd.h"
32 #include "bfdlink.h"
33 #include "genlink.h"
34 #include "elf-bfd.h"
35 #include "elf/mips.h"
37 /* Get the ECOFF swapping routines. */
38 #include "coff/sym.h"
39 #include "coff/symconst.h"
40 #include "coff/internal.h"
41 #include "coff/ecoff.h"
42 #include "coff/mips.h"
43 #define ECOFF_32
44 #include "ecoffswap.h"
46 /* This structure is used to hold .got information when linking. It
47 is stored in the tdata field of the bfd_elf_section_data structure. */
49 struct mips_got_info
51 /* The global symbol in the GOT with the lowest index in the dynamic
52 symbol table. */
53 struct elf_link_hash_entry *global_gotsym;
54 /* The number of global .got entries. */
55 unsigned int global_gotno;
56 /* The number of local .got entries. */
57 unsigned int local_gotno;
58 /* The number of local .got entries we have used. */
59 unsigned int assigned_gotno;
62 /* The MIPS ELF linker needs additional information for each symbol in
63 the global hash table. */
65 struct mips_elf_link_hash_entry
67 struct elf_link_hash_entry root;
69 /* External symbol information. */
70 EXTR esym;
72 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
73 this symbol. */
74 unsigned int possibly_dynamic_relocs;
76 /* The index of the first dynamic relocation (in the .rel.dyn
77 section) against this symbol. */
78 unsigned int min_dyn_reloc_index;
80 /* If there is a stub that 32 bit functions should use to call this
81 16 bit function, this points to the section containing the stub. */
82 asection *fn_stub;
84 /* Whether we need the fn_stub; this is set if this symbol appears
85 in any relocs other than a 16 bit call. */
86 boolean need_fn_stub;
88 /* If there is a stub that 16 bit functions should use to call this
89 32 bit function, this points to the section containing the stub. */
90 asection *call_stub;
92 /* This is like the call_stub field, but it is used if the function
93 being called returns a floating point value. */
94 asection *call_fp_stub;
97 static bfd_reloc_status_type mips32_64bit_reloc
98 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
99 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
100 PARAMS ((bfd *, bfd_reloc_code_real_type));
101 static reloc_howto_type *mips_rtype_to_howto
102 PARAMS ((unsigned int));
103 static void mips_info_to_howto_rel
104 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
105 static void mips_info_to_howto_rela
106 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
107 static void bfd_mips_elf32_swap_gptab_in
108 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
109 static void bfd_mips_elf32_swap_gptab_out
110 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
111 static void bfd_mips_elf_swap_msym_in
112 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
113 static void bfd_mips_elf_swap_msym_out
114 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
115 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
116 static boolean mips_elf_create_procedure_table
117 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
118 struct ecoff_debug_info *));
119 static INLINE int elf_mips_isa PARAMS ((flagword));
120 static INLINE int elf_mips_mach PARAMS ((flagword));
121 static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
122 static boolean mips_elf_is_local_label_name
123 PARAMS ((bfd *, const char *));
124 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
125 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
126 static int gptab_compare PARAMS ((const void *, const void *));
127 static void mips_elf_relocate_hi16
128 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
129 bfd_vma));
130 static boolean mips_elf_relocate_got_local
131 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
132 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
133 static void mips_elf_relocate_global_got
134 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
135 static bfd_reloc_status_type mips16_jump_reloc
136 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
137 static bfd_reloc_status_type mips16_gprel_reloc
138 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
139 static boolean mips_elf_create_compact_rel_section
140 PARAMS ((bfd *, struct bfd_link_info *));
141 static boolean mips_elf_create_got_section
142 PARAMS ((bfd *, struct bfd_link_info *));
143 static bfd_reloc_status_type mips_elf_final_gp
144 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
145 static bfd_byte *elf32_mips_get_relocated_section_contents
146 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
147 bfd_byte *, boolean, asymbol **));
148 static asection *mips_elf_create_msym_section
149 PARAMS ((bfd *));
150 static void mips_elf_irix6_finish_dynamic_symbol
151 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
152 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
153 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
154 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
155 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
156 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
157 static bfd_vma mips_elf_global_got_index
158 PARAMS ((bfd *, struct elf_link_hash_entry *));
159 static bfd_vma mips_elf_local_got_index
160 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
161 static bfd_vma mips_elf_got_offset_from_index
162 PARAMS ((bfd *, bfd *, bfd_vma));
163 static boolean mips_elf_record_global_got_symbol
164 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
165 struct mips_got_info *));
166 static bfd_vma mips_elf_got_page
167 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
168 static const Elf_Internal_Rela *mips_elf_next_lo16_relocation
169 PARAMS ((const Elf_Internal_Rela *, const Elf_Internal_Rela *));
170 static bfd_reloc_status_type mips_elf_calculate_relocation
171 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
172 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
173 Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
174 boolean *));
175 static bfd_vma mips_elf_obtain_contents
176 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
177 static boolean mips_elf_perform_relocation
178 PARAMS ((struct bfd_link_info *, reloc_howto_type *,
179 const Elf_Internal_Rela *, bfd_vma,
180 bfd *, asection *, bfd_byte *, boolean));
181 static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
182 static boolean mips_elf_sort_hash_table_f
183 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
184 static boolean mips_elf_sort_hash_table
185 PARAMS ((struct bfd_link_info *, unsigned long));
186 static asection * mips_elf_got_section PARAMS ((bfd *));
187 static struct mips_got_info *mips_elf_got_info
188 PARAMS ((bfd *, asection **));
189 static boolean mips_elf_local_relocation_p
190 PARAMS ((bfd *, const Elf_Internal_Rela *, asection **));
191 static bfd_vma mips_elf_create_local_got_entry
192 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
193 static bfd_vma mips_elf_got16_entry
194 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
195 static boolean mips_elf_create_dynamic_relocation
196 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
197 struct mips_elf_link_hash_entry *, asection *,
198 bfd_vma, bfd_vma *, asection *));
199 static void mips_elf_allocate_dynamic_relocations
200 PARAMS ((bfd *, unsigned int));
201 static boolean mips_elf_stub_section_p
202 PARAMS ((bfd *, asection *));
204 /* The level of IRIX compatibility we're striving for. */
206 typedef enum {
207 ict_none,
208 ict_irix5,
209 ict_irix6
210 } irix_compat_t;
212 /* Nonzero if ABFD is using the N32 ABI. */
214 #define ABI_N32_P(abfd) \
215 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
217 /* Nonzero if ABFD is using the 64-bit ABI. FIXME: This is never
218 true, yet. */
219 #define ABI_64_P(abfd) \
220 ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0)
222 /* What version of Irix we are trying to be compatible with. FIXME:
223 At the moment, we never generate "normal" MIPS ELF ABI executables;
224 we always use some version of Irix. */
226 #define IRIX_COMPAT(abfd) \
227 ((ABI_N32_P (abfd) || ABI_64_P (abfd)) ? ict_irix6 : ict_irix5)
229 /* Whether we are trying to be compatible with IRIX at all. */
231 #define SGI_COMPAT(abfd) \
232 (IRIX_COMPAT (abfd) != ict_none)
234 /* The name of the msym section. */
235 #define MIPS_ELF_MSYM_SECTION_NAME(abfd) ".msym"
237 /* The name of the srdata section. */
238 #define MIPS_ELF_SRDATA_SECTION_NAME(abfd) ".srdata"
240 /* The name of the options section. */
241 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
242 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.options" : ".options")
244 /* The name of the stub section. */
245 #define MIPS_ELF_STUB_SECTION_NAME(abfd) \
246 (IRIX_COMPAT (abfd) == ict_irix6 ? ".MIPS.stubs" : ".stub")
248 /* The name of the dynamic relocation section. */
249 #define MIPS_ELF_REL_DYN_SECTION_NAME(abfd) ".rel.dyn"
251 /* The size of an external REL relocation. */
252 #define MIPS_ELF_REL_SIZE(abfd) \
253 (get_elf_backend_data (abfd)->s->sizeof_rel)
255 /* The size of an external dynamic table entry. */
256 #define MIPS_ELF_DYN_SIZE(abfd) \
257 (get_elf_backend_data (abfd)->s->sizeof_dyn)
259 /* The size of a GOT entry. */
260 #define MIPS_ELF_GOT_SIZE(abfd) \
261 (get_elf_backend_data (abfd)->s->arch_size / 8)
263 /* The size of a symbol-table entry. */
264 #define MIPS_ELF_SYM_SIZE(abfd) \
265 (get_elf_backend_data (abfd)->s->sizeof_sym)
267 /* The default alignment for sections, as a power of two. */
268 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
269 (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2)
271 /* Get word-sized data. */
272 #define MIPS_ELF_GET_WORD(abfd, ptr) \
273 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
275 /* Put out word-sized data. */
276 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
277 (ABI_64_P (abfd) \
278 ? bfd_put_64 (abfd, val, ptr) \
279 : bfd_put_32 (abfd, val, ptr))
281 /* Add a dynamic symbol table-entry. */
282 #ifdef BFD64
283 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
284 (ABI_64_P (elf_hash_table (info)->dynobj) \
285 ? bfd_elf64_add_dynamic_entry (info, tag, val) \
286 : bfd_elf32_add_dynamic_entry (info, tag, val))
287 #else
288 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
289 (ABI_64_P (elf_hash_table (info)->dynobj) \
290 ? (abort (), false) \
291 : bfd_elf32_add_dynamic_entry (info, tag, val))
292 #endif
294 /* The number of local .got entries we reserve. */
295 #define MIPS_RESERVED_GOTNO (2)
297 /* Instructions which appear in a stub. For some reason the stub is
298 slightly different on an SGI system. */
299 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
300 #define STUB_LW(abfd) \
301 (SGI_COMPAT (abfd) \
302 ? (ABI_64_P (abfd) \
303 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
304 : 0x8f998010) /* lw t9,0x8010(gp) */ \
305 : 0x8f998000) /* lw t9,0x8000(gp) */
306 #define STUB_MOVE 0x03e07825 /* move t7,ra */
307 #define STUB_JALR 0x0320f809 /* jal t9 */
308 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
309 #define MIPS_FUNCTION_STUB_SIZE (16)
311 #if 0
312 /* We no longer try to identify particular sections for the .dynsym
313 section. When we do, we wind up crashing if there are other random
314 sections with relocations. */
316 /* Names of sections which appear in the .dynsym section in an Irix 5
317 executable. */
319 static const char * const mips_elf_dynsym_sec_names[] =
321 ".text",
322 ".init",
323 ".fini",
324 ".data",
325 ".rodata",
326 ".sdata",
327 ".sbss",
328 ".bss",
329 NULL
332 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
333 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
335 /* The number of entries in mips_elf_dynsym_sec_names which go in the
336 text segment. */
338 #define MIPS_TEXT_DYNSYM_SECNO (3)
340 #endif /* 0 */
342 /* The names of the runtime procedure table symbols used on Irix 5. */
344 static const char * const mips_elf_dynsym_rtproc_names[] =
346 "_procedure_table",
347 "_procedure_string_table",
348 "_procedure_table_size",
349 NULL
352 /* These structures are used to generate the .compact_rel section on
353 Irix 5. */
355 typedef struct
357 unsigned long id1; /* Always one? */
358 unsigned long num; /* Number of compact relocation entries. */
359 unsigned long id2; /* Always two? */
360 unsigned long offset; /* The file offset of the first relocation. */
361 unsigned long reserved0; /* Zero? */
362 unsigned long reserved1; /* Zero? */
363 } Elf32_compact_rel;
365 typedef struct
367 bfd_byte id1[4];
368 bfd_byte num[4];
369 bfd_byte id2[4];
370 bfd_byte offset[4];
371 bfd_byte reserved0[4];
372 bfd_byte reserved1[4];
373 } Elf32_External_compact_rel;
375 typedef struct
377 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
378 unsigned int rtype : 4; /* Relocation types. See below. */
379 unsigned int dist2to : 8;
380 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
381 unsigned long konst; /* KONST field. See below. */
382 unsigned long vaddr; /* VADDR to be relocated. */
383 } Elf32_crinfo;
385 typedef struct
387 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
388 unsigned int rtype : 4; /* Relocation types. See below. */
389 unsigned int dist2to : 8;
390 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
391 unsigned long konst; /* KONST field. See below. */
392 } Elf32_crinfo2;
394 typedef struct
396 bfd_byte info[4];
397 bfd_byte konst[4];
398 bfd_byte vaddr[4];
399 } Elf32_External_crinfo;
401 typedef struct
403 bfd_byte info[4];
404 bfd_byte konst[4];
405 } Elf32_External_crinfo2;
407 /* These are the constants used to swap the bitfields in a crinfo. */
409 #define CRINFO_CTYPE (0x1)
410 #define CRINFO_CTYPE_SH (31)
411 #define CRINFO_RTYPE (0xf)
412 #define CRINFO_RTYPE_SH (27)
413 #define CRINFO_DIST2TO (0xff)
414 #define CRINFO_DIST2TO_SH (19)
415 #define CRINFO_RELVADDR (0x7ffff)
416 #define CRINFO_RELVADDR_SH (0)
418 /* A compact relocation info has long (3 words) or short (2 words)
419 formats. A short format doesn't have VADDR field and relvaddr
420 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
421 #define CRF_MIPS_LONG 1
422 #define CRF_MIPS_SHORT 0
424 /* There are 4 types of compact relocation at least. The value KONST
425 has different meaning for each type:
427 (type) (konst)
428 CT_MIPS_REL32 Address in data
429 CT_MIPS_WORD Address in word (XXX)
430 CT_MIPS_GPHI_LO GP - vaddr
431 CT_MIPS_JMPAD Address to jump
434 #define CRT_MIPS_REL32 0xa
435 #define CRT_MIPS_WORD 0xb
436 #define CRT_MIPS_GPHI_LO 0xc
437 #define CRT_MIPS_JMPAD 0xd
439 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
440 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
441 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
442 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
444 static void bfd_elf32_swap_compact_rel_out
445 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
446 static void bfd_elf32_swap_crinfo_out
447 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
449 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
451 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
452 from smaller values. Start with zero, widen, *then* decrement. */
453 #define MINUS_ONE (((bfd_vma)0) - 1)
455 static reloc_howto_type elf_mips_howto_table[] =
457 /* No relocation. */
458 HOWTO (R_MIPS_NONE, /* type */
459 0, /* rightshift */
460 0, /* size (0 = byte, 1 = short, 2 = long) */
461 0, /* bitsize */
462 false, /* pc_relative */
463 0, /* bitpos */
464 complain_overflow_dont, /* complain_on_overflow */
465 bfd_elf_generic_reloc, /* special_function */
466 "R_MIPS_NONE", /* name */
467 false, /* partial_inplace */
468 0, /* src_mask */
469 0, /* dst_mask */
470 false), /* pcrel_offset */
472 /* 16 bit relocation. */
473 HOWTO (R_MIPS_16, /* type */
474 0, /* rightshift */
475 1, /* size (0 = byte, 1 = short, 2 = long) */
476 16, /* bitsize */
477 false, /* pc_relative */
478 0, /* bitpos */
479 complain_overflow_bitfield, /* complain_on_overflow */
480 bfd_elf_generic_reloc, /* special_function */
481 "R_MIPS_16", /* name */
482 true, /* partial_inplace */
483 0xffff, /* src_mask */
484 0xffff, /* dst_mask */
485 false), /* pcrel_offset */
487 /* 32 bit relocation. */
488 HOWTO (R_MIPS_32, /* type */
489 0, /* rightshift */
490 2, /* size (0 = byte, 1 = short, 2 = long) */
491 32, /* bitsize */
492 false, /* pc_relative */
493 0, /* bitpos */
494 complain_overflow_bitfield, /* complain_on_overflow */
495 bfd_elf_generic_reloc, /* special_function */
496 "R_MIPS_32", /* name */
497 true, /* partial_inplace */
498 0xffffffff, /* src_mask */
499 0xffffffff, /* dst_mask */
500 false), /* pcrel_offset */
502 /* 32 bit symbol relative relocation. */
503 HOWTO (R_MIPS_REL32, /* type */
504 0, /* rightshift */
505 2, /* size (0 = byte, 1 = short, 2 = long) */
506 32, /* bitsize */
507 false, /* pc_relative */
508 0, /* bitpos */
509 complain_overflow_bitfield, /* complain_on_overflow */
510 bfd_elf_generic_reloc, /* special_function */
511 "R_MIPS_REL32", /* name */
512 true, /* partial_inplace */
513 0xffffffff, /* src_mask */
514 0xffffffff, /* dst_mask */
515 false), /* pcrel_offset */
517 /* 26 bit branch address. */
518 HOWTO (R_MIPS_26, /* type */
519 2, /* rightshift */
520 2, /* size (0 = byte, 1 = short, 2 = long) */
521 26, /* bitsize */
522 false, /* pc_relative */
523 0, /* bitpos */
524 complain_overflow_dont, /* complain_on_overflow */
525 /* This needs complex overflow
526 detection, because the upper four
527 bits must match the PC. */
528 bfd_elf_generic_reloc, /* special_function */
529 "R_MIPS_26", /* name */
530 true, /* partial_inplace */
531 0x3ffffff, /* src_mask */
532 0x3ffffff, /* dst_mask */
533 false), /* pcrel_offset */
535 /* High 16 bits of symbol value. */
536 HOWTO (R_MIPS_HI16, /* type */
537 0, /* rightshift */
538 2, /* size (0 = byte, 1 = short, 2 = long) */
539 16, /* bitsize */
540 false, /* pc_relative */
541 0, /* bitpos */
542 complain_overflow_dont, /* complain_on_overflow */
543 _bfd_mips_elf_hi16_reloc, /* special_function */
544 "R_MIPS_HI16", /* name */
545 true, /* partial_inplace */
546 0xffff, /* src_mask */
547 0xffff, /* dst_mask */
548 false), /* pcrel_offset */
550 /* Low 16 bits of symbol value. */
551 HOWTO (R_MIPS_LO16, /* type */
552 0, /* rightshift */
553 2, /* size (0 = byte, 1 = short, 2 = long) */
554 16, /* bitsize */
555 false, /* pc_relative */
556 0, /* bitpos */
557 complain_overflow_dont, /* complain_on_overflow */
558 _bfd_mips_elf_lo16_reloc, /* special_function */
559 "R_MIPS_LO16", /* name */
560 true, /* partial_inplace */
561 0xffff, /* src_mask */
562 0xffff, /* dst_mask */
563 false), /* pcrel_offset */
565 /* GP relative reference. */
566 HOWTO (R_MIPS_GPREL16, /* type */
567 0, /* rightshift */
568 2, /* size (0 = byte, 1 = short, 2 = long) */
569 16, /* bitsize */
570 false, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_signed, /* complain_on_overflow */
573 _bfd_mips_elf_gprel16_reloc, /* special_function */
574 "R_MIPS_GPREL16", /* name */
575 true, /* partial_inplace */
576 0xffff, /* src_mask */
577 0xffff, /* dst_mask */
578 false), /* pcrel_offset */
580 /* Reference to literal section. */
581 HOWTO (R_MIPS_LITERAL, /* type */
582 0, /* rightshift */
583 2, /* size (0 = byte, 1 = short, 2 = long) */
584 16, /* bitsize */
585 false, /* pc_relative */
586 0, /* bitpos */
587 complain_overflow_signed, /* complain_on_overflow */
588 _bfd_mips_elf_gprel16_reloc, /* special_function */
589 "R_MIPS_LITERAL", /* name */
590 true, /* partial_inplace */
591 0xffff, /* src_mask */
592 0xffff, /* dst_mask */
593 false), /* pcrel_offset */
595 /* Reference to global offset table. */
596 HOWTO (R_MIPS_GOT16, /* type */
597 0, /* rightshift */
598 2, /* size (0 = byte, 1 = short, 2 = long) */
599 16, /* bitsize */
600 false, /* pc_relative */
601 0, /* bitpos */
602 complain_overflow_signed, /* complain_on_overflow */
603 _bfd_mips_elf_got16_reloc, /* special_function */
604 "R_MIPS_GOT16", /* name */
605 false, /* partial_inplace */
606 0xffff, /* src_mask */
607 0xffff, /* dst_mask */
608 false), /* pcrel_offset */
610 /* 16 bit PC relative reference. */
611 HOWTO (R_MIPS_PC16, /* type */
612 0, /* rightshift */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
614 16, /* bitsize */
615 true, /* pc_relative */
616 0, /* bitpos */
617 complain_overflow_signed, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_MIPS_PC16", /* name */
620 true, /* partial_inplace */
621 0xffff, /* src_mask */
622 0xffff, /* dst_mask */
623 false), /* pcrel_offset */
625 /* 16 bit call through global offset table. */
626 HOWTO (R_MIPS_CALL16, /* type */
627 0, /* rightshift */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
629 16, /* bitsize */
630 false, /* pc_relative */
631 0, /* bitpos */
632 complain_overflow_signed, /* complain_on_overflow */
633 bfd_elf_generic_reloc, /* special_function */
634 "R_MIPS_CALL16", /* name */
635 false, /* partial_inplace */
636 0xffff, /* src_mask */
637 0xffff, /* dst_mask */
638 false), /* pcrel_offset */
640 /* 32 bit GP relative reference. */
641 HOWTO (R_MIPS_GPREL32, /* type */
642 0, /* rightshift */
643 2, /* size (0 = byte, 1 = short, 2 = long) */
644 32, /* bitsize */
645 false, /* pc_relative */
646 0, /* bitpos */
647 complain_overflow_bitfield, /* complain_on_overflow */
648 _bfd_mips_elf_gprel32_reloc, /* special_function */
649 "R_MIPS_GPREL32", /* name */
650 true, /* partial_inplace */
651 0xffffffff, /* src_mask */
652 0xffffffff, /* dst_mask */
653 false), /* pcrel_offset */
655 /* The remaining relocs are defined on Irix 5, although they are
656 not defined by the ABI. */
657 EMPTY_HOWTO (13),
658 EMPTY_HOWTO (14),
659 EMPTY_HOWTO (15),
661 /* A 5 bit shift field. */
662 HOWTO (R_MIPS_SHIFT5, /* type */
663 0, /* rightshift */
664 2, /* size (0 = byte, 1 = short, 2 = long) */
665 5, /* bitsize */
666 false, /* pc_relative */
667 6, /* bitpos */
668 complain_overflow_bitfield, /* complain_on_overflow */
669 bfd_elf_generic_reloc, /* special_function */
670 "R_MIPS_SHIFT5", /* name */
671 true, /* partial_inplace */
672 0x000007c0, /* src_mask */
673 0x000007c0, /* dst_mask */
674 false), /* pcrel_offset */
676 /* A 6 bit shift field. */
677 /* FIXME: This is not handled correctly; a special function is
678 needed to put the most significant bit in the right place. */
679 HOWTO (R_MIPS_SHIFT6, /* type */
680 0, /* rightshift */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
682 6, /* bitsize */
683 false, /* pc_relative */
684 6, /* bitpos */
685 complain_overflow_bitfield, /* complain_on_overflow */
686 bfd_elf_generic_reloc, /* special_function */
687 "R_MIPS_SHIFT6", /* name */
688 true, /* partial_inplace */
689 0x000007c4, /* src_mask */
690 0x000007c4, /* dst_mask */
691 false), /* pcrel_offset */
693 /* A 64 bit relocation. */
694 HOWTO (R_MIPS_64, /* type */
695 0, /* rightshift */
696 4, /* size (0 = byte, 1 = short, 2 = long) */
697 64, /* bitsize */
698 false, /* pc_relative */
699 0, /* bitpos */
700 complain_overflow_bitfield, /* complain_on_overflow */
701 mips32_64bit_reloc, /* special_function */
702 "R_MIPS_64", /* name */
703 true, /* partial_inplace */
704 MINUS_ONE, /* src_mask */
705 MINUS_ONE, /* dst_mask */
706 false), /* pcrel_offset */
708 /* Displacement in the global offset table. */
709 HOWTO (R_MIPS_GOT_DISP, /* type */
710 0, /* rightshift */
711 2, /* size (0 = byte, 1 = short, 2 = long) */
712 16, /* bitsize */
713 false, /* pc_relative */
714 0, /* bitpos */
715 complain_overflow_bitfield, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* special_function */
717 "R_MIPS_GOT_DISP", /* name */
718 true, /* partial_inplace */
719 0x0000ffff, /* src_mask */
720 0x0000ffff, /* dst_mask */
721 false), /* pcrel_offset */
723 /* Displacement to page pointer in the global offset table. */
724 HOWTO (R_MIPS_GOT_PAGE, /* type */
725 0, /* rightshift */
726 2, /* size (0 = byte, 1 = short, 2 = long) */
727 16, /* bitsize */
728 false, /* pc_relative */
729 0, /* bitpos */
730 complain_overflow_bitfield, /* complain_on_overflow */
731 bfd_elf_generic_reloc, /* special_function */
732 "R_MIPS_GOT_PAGE", /* name */
733 true, /* partial_inplace */
734 0x0000ffff, /* src_mask */
735 0x0000ffff, /* dst_mask */
736 false), /* pcrel_offset */
738 /* Offset from page pointer in the global offset table. */
739 HOWTO (R_MIPS_GOT_OFST, /* type */
740 0, /* rightshift */
741 2, /* size (0 = byte, 1 = short, 2 = long) */
742 16, /* bitsize */
743 false, /* pc_relative */
744 0, /* bitpos */
745 complain_overflow_bitfield, /* complain_on_overflow */
746 bfd_elf_generic_reloc, /* special_function */
747 "R_MIPS_GOT_OFST", /* name */
748 true, /* partial_inplace */
749 0x0000ffff, /* src_mask */
750 0x0000ffff, /* dst_mask */
751 false), /* pcrel_offset */
753 /* High 16 bits of displacement in global offset table. */
754 HOWTO (R_MIPS_GOT_HI16, /* type */
755 0, /* rightshift */
756 2, /* size (0 = byte, 1 = short, 2 = long) */
757 16, /* bitsize */
758 false, /* pc_relative */
759 0, /* bitpos */
760 complain_overflow_dont, /* complain_on_overflow */
761 bfd_elf_generic_reloc, /* special_function */
762 "R_MIPS_GOT_HI16", /* name */
763 true, /* partial_inplace */
764 0x0000ffff, /* src_mask */
765 0x0000ffff, /* dst_mask */
766 false), /* pcrel_offset */
768 /* Low 16 bits of displacement in global offset table. */
769 HOWTO (R_MIPS_GOT_LO16, /* type */
770 0, /* rightshift */
771 2, /* size (0 = byte, 1 = short, 2 = long) */
772 16, /* bitsize */
773 false, /* pc_relative */
774 0, /* bitpos */
775 complain_overflow_dont, /* complain_on_overflow */
776 bfd_elf_generic_reloc, /* special_function */
777 "R_MIPS_GOT_LO16", /* name */
778 true, /* partial_inplace */
779 0x0000ffff, /* src_mask */
780 0x0000ffff, /* dst_mask */
781 false), /* pcrel_offset */
783 /* 64 bit subtraction. Used in the N32 ABI. */
784 HOWTO (R_MIPS_SUB, /* type */
785 0, /* rightshift */
786 4, /* size (0 = byte, 1 = short, 2 = long) */
787 64, /* bitsize */
788 false, /* pc_relative */
789 0, /* bitpos */
790 complain_overflow_bitfield, /* complain_on_overflow */
791 bfd_elf_generic_reloc, /* special_function */
792 "R_MIPS_SUB", /* name */
793 true, /* partial_inplace */
794 MINUS_ONE, /* src_mask */
795 MINUS_ONE, /* dst_mask */
796 false), /* pcrel_offset */
798 /* Used to cause the linker to insert and delete instructions? */
799 EMPTY_HOWTO (R_MIPS_INSERT_A),
800 EMPTY_HOWTO (R_MIPS_INSERT_B),
801 EMPTY_HOWTO (R_MIPS_DELETE),
803 /* Get the higher value of a 64 bit addend. */
804 HOWTO (R_MIPS_HIGHER, /* type */
805 0, /* rightshift */
806 2, /* size (0 = byte, 1 = short, 2 = long) */
807 16, /* bitsize */
808 false, /* pc_relative */
809 0, /* bitpos */
810 complain_overflow_dont, /* complain_on_overflow */
811 bfd_elf_generic_reloc, /* special_function */
812 "R_MIPS_HIGHER", /* name */
813 true, /* partial_inplace */
814 0, /* src_mask */
815 0xffff, /* dst_mask */
816 false), /* pcrel_offset */
818 /* Get the highest value of a 64 bit addend. */
819 HOWTO (R_MIPS_HIGHEST, /* type */
820 0, /* rightshift */
821 2, /* size (0 = byte, 1 = short, 2 = long) */
822 16, /* bitsize */
823 false, /* pc_relative */
824 0, /* bitpos */
825 complain_overflow_dont, /* complain_on_overflow */
826 bfd_elf_generic_reloc, /* special_function */
827 "R_MIPS_HIGHEST", /* name */
828 true, /* partial_inplace */
829 0, /* src_mask */
830 0xffff, /* dst_mask */
831 false), /* pcrel_offset */
833 /* High 16 bits of displacement in global offset table. */
834 HOWTO (R_MIPS_CALL_HI16, /* type */
835 0, /* rightshift */
836 2, /* size (0 = byte, 1 = short, 2 = long) */
837 16, /* bitsize */
838 false, /* pc_relative */
839 0, /* bitpos */
840 complain_overflow_dont, /* complain_on_overflow */
841 bfd_elf_generic_reloc, /* special_function */
842 "R_MIPS_CALL_HI16", /* name */
843 true, /* partial_inplace */
844 0x0000ffff, /* src_mask */
845 0x0000ffff, /* dst_mask */
846 false), /* pcrel_offset */
848 /* Low 16 bits of displacement in global offset table. */
849 HOWTO (R_MIPS_CALL_LO16, /* type */
850 0, /* rightshift */
851 2, /* size (0 = byte, 1 = short, 2 = long) */
852 16, /* bitsize */
853 false, /* pc_relative */
854 0, /* bitpos */
855 complain_overflow_dont, /* complain_on_overflow */
856 bfd_elf_generic_reloc, /* special_function */
857 "R_MIPS_CALL_LO16", /* name */
858 true, /* partial_inplace */
859 0x0000ffff, /* src_mask */
860 0x0000ffff, /* dst_mask */
861 false), /* pcrel_offset */
863 /* Section displacement. */
864 HOWTO (R_MIPS_SCN_DISP, /* type */
865 0, /* rightshift */
866 2, /* size (0 = byte, 1 = short, 2 = long) */
867 32, /* bitsize */
868 false, /* pc_relative */
869 0, /* bitpos */
870 complain_overflow_dont, /* complain_on_overflow */
871 bfd_elf_generic_reloc, /* special_function */
872 "R_MIPS_SCN_DISP", /* name */
873 false, /* partial_inplace */
874 0xffffffff, /* src_mask */
875 0xffffffff, /* dst_mask */
876 false), /* pcrel_offset */
878 EMPTY_HOWTO (R_MIPS_REL16),
879 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
880 EMPTY_HOWTO (R_MIPS_PJUMP),
881 EMPTY_HOWTO (R_MIPS_RELGOT),
883 /* Protected jump conversion. This is an optimization hint. No
884 relocation is required for correctness. */
885 HOWTO (R_MIPS_JALR, /* type */
886 0, /* rightshift */
887 0, /* size (0 = byte, 1 = short, 2 = long) */
888 0, /* bitsize */
889 false, /* pc_relative */
890 0, /* bitpos */
891 complain_overflow_dont, /* complain_on_overflow */
892 bfd_elf_generic_reloc, /* special_function */
893 "R_MIPS_JALR", /* name */
894 false, /* partial_inplace */
895 0x00000000, /* src_mask */
896 0x00000000, /* dst_mask */
897 false), /* pcrel_offset */
900 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
901 is a hack to make the linker think that we need 64 bit values. */
902 static reloc_howto_type elf_mips_ctor64_howto =
903 HOWTO (R_MIPS_64, /* type */
904 0, /* rightshift */
905 4, /* size (0 = byte, 1 = short, 2 = long) */
906 32, /* bitsize */
907 false, /* pc_relative */
908 0, /* bitpos */
909 complain_overflow_signed, /* complain_on_overflow */
910 mips32_64bit_reloc, /* special_function */
911 "R_MIPS_64", /* name */
912 true, /* partial_inplace */
913 0xffffffff, /* src_mask */
914 0xffffffff, /* dst_mask */
915 false); /* pcrel_offset */
917 /* The reloc used for the mips16 jump instruction. */
918 static reloc_howto_type elf_mips16_jump_howto =
919 HOWTO (R_MIPS16_26, /* type */
920 2, /* rightshift */
921 2, /* size (0 = byte, 1 = short, 2 = long) */
922 26, /* bitsize */
923 false, /* pc_relative */
924 0, /* bitpos */
925 complain_overflow_dont, /* complain_on_overflow */
926 /* This needs complex overflow
927 detection, because the upper four
928 bits must match the PC. */
929 mips16_jump_reloc, /* special_function */
930 "R_MIPS16_26", /* name */
931 true, /* partial_inplace */
932 0x3ffffff, /* src_mask */
933 0x3ffffff, /* dst_mask */
934 false); /* pcrel_offset */
936 /* The reloc used for the mips16 gprel instruction. */
937 static reloc_howto_type elf_mips16_gprel_howto =
938 HOWTO (R_MIPS16_GPREL, /* type */
939 0, /* rightshift */
940 2, /* size (0 = byte, 1 = short, 2 = long) */
941 16, /* bitsize */
942 false, /* pc_relative */
943 0, /* bitpos */
944 complain_overflow_signed, /* complain_on_overflow */
945 mips16_gprel_reloc, /* special_function */
946 "R_MIPS16_GPREL", /* name */
947 true, /* partial_inplace */
948 0x07ff001f, /* src_mask */
949 0x07ff001f, /* dst_mask */
950 false); /* pcrel_offset */
953 /* GNU extension to record C++ vtable hierarchy */
954 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
955 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
956 0, /* rightshift */
957 2, /* size (0 = byte, 1 = short, 2 = long) */
958 0, /* bitsize */
959 false, /* pc_relative */
960 0, /* bitpos */
961 complain_overflow_dont, /* complain_on_overflow */
962 NULL, /* special_function */
963 "R_MIPS_GNU_VTINHERIT", /* name */
964 false, /* partial_inplace */
965 0, /* src_mask */
966 0, /* dst_mask */
967 false); /* pcrel_offset */
969 /* GNU extension to record C++ vtable member usage */
970 static reloc_howto_type elf_mips_gnu_vtentry_howto =
971 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
972 0, /* rightshift */
973 2, /* size (0 = byte, 1 = short, 2 = long) */
974 0, /* bitsize */
975 false, /* pc_relative */
976 0, /* bitpos */
977 complain_overflow_dont, /* complain_on_overflow */
978 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
979 "R_MIPS_GNU_VTENTRY", /* name */
980 false, /* partial_inplace */
981 0, /* src_mask */
982 0, /* dst_mask */
983 false); /* pcrel_offset */
985 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
986 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
987 the HI16. Here we just save the information we need; we do the
988 actual relocation when we see the LO16. MIPS ELF requires that the
989 LO16 immediately follow the HI16. As a GNU extension, we permit an
990 arbitrary number of HI16 relocs to be associated with a single LO16
991 reloc. This extension permits gcc to output the HI and LO relocs
992 itself. */
994 struct mips_hi16
996 struct mips_hi16 *next;
997 bfd_byte *addr;
998 bfd_vma addend;
1001 /* FIXME: This should not be a static variable. */
1003 static struct mips_hi16 *mips_hi16_list;
1005 bfd_reloc_status_type
1006 _bfd_mips_elf_hi16_reloc (abfd,
1007 reloc_entry,
1008 symbol,
1009 data,
1010 input_section,
1011 output_bfd,
1012 error_message)
1013 bfd *abfd ATTRIBUTE_UNUSED;
1014 arelent *reloc_entry;
1015 asymbol *symbol;
1016 PTR data;
1017 asection *input_section;
1018 bfd *output_bfd;
1019 char **error_message;
1021 bfd_reloc_status_type ret;
1022 bfd_vma relocation;
1023 struct mips_hi16 *n;
1025 /* If we're relocating, and this an external symbol, we don't want
1026 to change anything. */
1027 if (output_bfd != (bfd *) NULL
1028 && (symbol->flags & BSF_SECTION_SYM) == 0
1029 && reloc_entry->addend == 0)
1031 reloc_entry->address += input_section->output_offset;
1032 return bfd_reloc_ok;
1035 ret = bfd_reloc_ok;
1037 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1039 boolean relocateable;
1040 bfd_vma gp;
1042 if (ret == bfd_reloc_undefined)
1043 abort ();
1045 if (output_bfd != NULL)
1046 relocateable = true;
1047 else
1049 relocateable = false;
1050 output_bfd = symbol->section->output_section->owner;
1053 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1054 error_message, &gp);
1055 if (ret != bfd_reloc_ok)
1056 return ret;
1058 relocation = gp - reloc_entry->address;
1060 else
1062 if (bfd_is_und_section (symbol->section)
1063 && output_bfd == (bfd *) NULL)
1064 ret = bfd_reloc_undefined;
1066 if (bfd_is_com_section (symbol->section))
1067 relocation = 0;
1068 else
1069 relocation = symbol->value;
1072 relocation += symbol->section->output_section->vma;
1073 relocation += symbol->section->output_offset;
1074 relocation += reloc_entry->addend;
1076 if (reloc_entry->address > input_section->_cooked_size)
1077 return bfd_reloc_outofrange;
1079 /* Save the information, and let LO16 do the actual relocation. */
1080 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
1081 if (n == NULL)
1082 return bfd_reloc_outofrange;
1083 n->addr = (bfd_byte *) data + reloc_entry->address;
1084 n->addend = relocation;
1085 n->next = mips_hi16_list;
1086 mips_hi16_list = n;
1088 if (output_bfd != (bfd *) NULL)
1089 reloc_entry->address += input_section->output_offset;
1091 return ret;
1094 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
1095 inplace relocation; this function exists in order to do the
1096 R_MIPS_HI16 relocation described above. */
1098 bfd_reloc_status_type
1099 _bfd_mips_elf_lo16_reloc (abfd,
1100 reloc_entry,
1101 symbol,
1102 data,
1103 input_section,
1104 output_bfd,
1105 error_message)
1106 bfd *abfd;
1107 arelent *reloc_entry;
1108 asymbol *symbol;
1109 PTR data;
1110 asection *input_section;
1111 bfd *output_bfd;
1112 char **error_message;
1114 arelent gp_disp_relent;
1116 if (mips_hi16_list != NULL)
1118 struct mips_hi16 *l;
1120 l = mips_hi16_list;
1121 while (l != NULL)
1123 unsigned long insn;
1124 unsigned long val;
1125 unsigned long vallo;
1126 struct mips_hi16 *next;
1128 /* Do the HI16 relocation. Note that we actually don't need
1129 to know anything about the LO16 itself, except where to
1130 find the low 16 bits of the addend needed by the LO16. */
1131 insn = bfd_get_32 (abfd, l->addr);
1132 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1133 & 0xffff);
1134 val = ((insn & 0xffff) << 16) + vallo;
1135 val += l->addend;
1137 /* The low order 16 bits are always treated as a signed
1138 value. Therefore, a negative value in the low order bits
1139 requires an adjustment in the high order bits. We need
1140 to make this adjustment in two ways: once for the bits we
1141 took from the data, and once for the bits we are putting
1142 back in to the data. */
1143 if ((vallo & 0x8000) != 0)
1144 val -= 0x10000;
1145 if ((val & 0x8000) != 0)
1146 val += 0x10000;
1148 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1149 bfd_put_32 (abfd, insn, l->addr);
1151 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1153 gp_disp_relent = *reloc_entry;
1154 reloc_entry = &gp_disp_relent;
1155 reloc_entry->addend = l->addend;
1158 next = l->next;
1159 free (l);
1160 l = next;
1163 mips_hi16_list = NULL;
1165 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1167 bfd_reloc_status_type ret;
1168 bfd_vma gp, relocation;
1170 /* FIXME: Does this case ever occur? */
1172 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1173 if (ret != bfd_reloc_ok)
1174 return ret;
1176 relocation = gp - reloc_entry->address;
1177 relocation += symbol->section->output_section->vma;
1178 relocation += symbol->section->output_offset;
1179 relocation += reloc_entry->addend;
1181 if (reloc_entry->address > input_section->_cooked_size)
1182 return bfd_reloc_outofrange;
1184 gp_disp_relent = *reloc_entry;
1185 reloc_entry = &gp_disp_relent;
1186 reloc_entry->addend = relocation - 4;
1189 /* Now do the LO16 reloc in the usual way. */
1190 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1191 input_section, output_bfd, error_message);
1194 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1195 table used for PIC code. If the symbol is an external symbol, the
1196 instruction is modified to contain the offset of the appropriate
1197 entry in the global offset table. If the symbol is a section
1198 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1199 addends are combined to form the real addend against the section
1200 symbol; the GOT16 is modified to contain the offset of an entry in
1201 the global offset table, and the LO16 is modified to offset it
1202 appropriately. Thus an offset larger than 16 bits requires a
1203 modified value in the global offset table.
1205 This implementation suffices for the assembler, but the linker does
1206 not yet know how to create global offset tables. */
1208 bfd_reloc_status_type
1209 _bfd_mips_elf_got16_reloc (abfd,
1210 reloc_entry,
1211 symbol,
1212 data,
1213 input_section,
1214 output_bfd,
1215 error_message)
1216 bfd *abfd;
1217 arelent *reloc_entry;
1218 asymbol *symbol;
1219 PTR data;
1220 asection *input_section;
1221 bfd *output_bfd;
1222 char **error_message;
1224 /* If we're relocating, and this an external symbol, we don't want
1225 to change anything. */
1226 if (output_bfd != (bfd *) NULL
1227 && (symbol->flags & BSF_SECTION_SYM) == 0
1228 && reloc_entry->addend == 0)
1230 reloc_entry->address += input_section->output_offset;
1231 return bfd_reloc_ok;
1234 /* If we're relocating, and this is a local symbol, we can handle it
1235 just like HI16. */
1236 if (output_bfd != (bfd *) NULL
1237 && (symbol->flags & BSF_SECTION_SYM) != 0)
1238 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1239 input_section, output_bfd, error_message);
1241 abort ();
1244 /* Set the GP value for OUTPUT_BFD. Returns false if this is a
1245 dangerous relocation. */
1247 static boolean
1248 mips_elf_assign_gp (output_bfd, pgp)
1249 bfd *output_bfd;
1250 bfd_vma *pgp;
1252 unsigned int count;
1253 asymbol **sym;
1254 unsigned int i;
1256 /* If we've already figured out what GP will be, just return it. */
1257 *pgp = _bfd_get_gp_value (output_bfd);
1258 if (*pgp)
1259 return true;
1261 count = bfd_get_symcount (output_bfd);
1262 sym = bfd_get_outsymbols (output_bfd);
1264 /* The linker script will have created a symbol named `_gp' with the
1265 appropriate value. */
1266 if (sym == (asymbol **) NULL)
1267 i = count;
1268 else
1270 for (i = 0; i < count; i++, sym++)
1272 register CONST char *name;
1274 name = bfd_asymbol_name (*sym);
1275 if (*name == '_' && strcmp (name, "_gp") == 0)
1277 *pgp = bfd_asymbol_value (*sym);
1278 _bfd_set_gp_value (output_bfd, *pgp);
1279 break;
1284 if (i >= count)
1286 /* Only get the error once. */
1287 *pgp = 4;
1288 _bfd_set_gp_value (output_bfd, *pgp);
1289 return false;
1292 return true;
1295 /* We have to figure out the gp value, so that we can adjust the
1296 symbol value correctly. We look up the symbol _gp in the output
1297 BFD. If we can't find it, we're stuck. We cache it in the ELF
1298 target data. We don't need to adjust the symbol value for an
1299 external symbol if we are producing relocateable output. */
1301 static bfd_reloc_status_type
1302 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1303 bfd *output_bfd;
1304 asymbol *symbol;
1305 boolean relocateable;
1306 char **error_message;
1307 bfd_vma *pgp;
1309 if (bfd_is_und_section (symbol->section)
1310 && ! relocateable)
1312 *pgp = 0;
1313 return bfd_reloc_undefined;
1316 *pgp = _bfd_get_gp_value (output_bfd);
1317 if (*pgp == 0
1318 && (! relocateable
1319 || (symbol->flags & BSF_SECTION_SYM) != 0))
1321 if (relocateable)
1323 /* Make up a value. */
1324 *pgp = symbol->section->output_section->vma + 0x4000;
1325 _bfd_set_gp_value (output_bfd, *pgp);
1327 else if (!mips_elf_assign_gp (output_bfd, pgp))
1329 *error_message =
1330 (char *) _("GP relative relocation when _gp not defined");
1331 return bfd_reloc_dangerous;
1335 return bfd_reloc_ok;
1338 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1339 become the offset from the gp register. This function also handles
1340 R_MIPS_LITERAL relocations, although those can be handled more
1341 cleverly because the entries in the .lit8 and .lit4 sections can be
1342 merged. */
1344 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1345 arelent *, asection *,
1346 boolean, PTR, bfd_vma));
1348 bfd_reloc_status_type
1349 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1350 output_bfd, error_message)
1351 bfd *abfd;
1352 arelent *reloc_entry;
1353 asymbol *symbol;
1354 PTR data;
1355 asection *input_section;
1356 bfd *output_bfd;
1357 char **error_message;
1359 boolean relocateable;
1360 bfd_reloc_status_type ret;
1361 bfd_vma gp;
1363 /* If we're relocating, and this is an external symbol with no
1364 addend, we don't want to change anything. We will only have an
1365 addend if this is a newly created reloc, not read from an ELF
1366 file. */
1367 if (output_bfd != (bfd *) NULL
1368 && (symbol->flags & BSF_SECTION_SYM) == 0
1369 && reloc_entry->addend == 0)
1371 reloc_entry->address += input_section->output_offset;
1372 return bfd_reloc_ok;
1375 if (output_bfd != (bfd *) NULL)
1376 relocateable = true;
1377 else
1379 relocateable = false;
1380 output_bfd = symbol->section->output_section->owner;
1383 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1384 &gp);
1385 if (ret != bfd_reloc_ok)
1386 return ret;
1388 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1389 relocateable, data, gp);
1392 static bfd_reloc_status_type
1393 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1395 bfd *abfd;
1396 asymbol *symbol;
1397 arelent *reloc_entry;
1398 asection *input_section;
1399 boolean relocateable;
1400 PTR data;
1401 bfd_vma gp;
1403 bfd_vma relocation;
1404 unsigned long insn;
1405 unsigned long val;
1407 if (bfd_is_com_section (symbol->section))
1408 relocation = 0;
1409 else
1410 relocation = symbol->value;
1412 relocation += symbol->section->output_section->vma;
1413 relocation += symbol->section->output_offset;
1415 if (reloc_entry->address > input_section->_cooked_size)
1416 return bfd_reloc_outofrange;
1418 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1420 /* Set val to the offset into the section or symbol. */
1421 if (reloc_entry->howto->src_mask == 0)
1423 /* This case occurs with the 64-bit MIPS ELF ABI. */
1424 val = reloc_entry->addend;
1426 else
1428 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1429 if (val & 0x8000)
1430 val -= 0x10000;
1433 /* Adjust val for the final section location and GP value. If we
1434 are producing relocateable output, we don't want to do this for
1435 an external symbol. */
1436 if (! relocateable
1437 || (symbol->flags & BSF_SECTION_SYM) != 0)
1438 val += relocation - gp;
1440 insn = (insn &~ 0xffff) | (val & 0xffff);
1441 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1443 if (relocateable)
1444 reloc_entry->address += input_section->output_offset;
1446 /* Make sure it fit in 16 bits. */
1447 if (val >= 0x8000 && val < 0xffff8000)
1448 return bfd_reloc_overflow;
1450 return bfd_reloc_ok;
1453 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1454 from the gp register? XXX */
1456 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1457 arelent *, asection *,
1458 boolean, PTR, bfd_vma));
1460 bfd_reloc_status_type
1461 _bfd_mips_elf_gprel32_reloc (abfd,
1462 reloc_entry,
1463 symbol,
1464 data,
1465 input_section,
1466 output_bfd,
1467 error_message)
1468 bfd *abfd;
1469 arelent *reloc_entry;
1470 asymbol *symbol;
1471 PTR data;
1472 asection *input_section;
1473 bfd *output_bfd;
1474 char **error_message;
1476 boolean relocateable;
1477 bfd_reloc_status_type ret;
1478 bfd_vma gp;
1480 /* If we're relocating, and this is an external symbol with no
1481 addend, we don't want to change anything. We will only have an
1482 addend if this is a newly created reloc, not read from an ELF
1483 file. */
1484 if (output_bfd != (bfd *) NULL
1485 && (symbol->flags & BSF_SECTION_SYM) == 0
1486 && reloc_entry->addend == 0)
1488 *error_message = (char *)
1489 _("32bits gp relative relocation occurs for an external symbol");
1490 return bfd_reloc_outofrange;
1493 if (output_bfd != (bfd *) NULL)
1495 relocateable = true;
1496 gp = _bfd_get_gp_value (output_bfd);
1498 else
1500 relocateable = false;
1501 output_bfd = symbol->section->output_section->owner;
1503 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1504 error_message, &gp);
1505 if (ret != bfd_reloc_ok)
1506 return ret;
1509 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1510 relocateable, data, gp);
1513 static bfd_reloc_status_type
1514 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1516 bfd *abfd;
1517 asymbol *symbol;
1518 arelent *reloc_entry;
1519 asection *input_section;
1520 boolean relocateable;
1521 PTR data;
1522 bfd_vma gp;
1524 bfd_vma relocation;
1525 unsigned long val;
1527 if (bfd_is_com_section (symbol->section))
1528 relocation = 0;
1529 else
1530 relocation = symbol->value;
1532 relocation += symbol->section->output_section->vma;
1533 relocation += symbol->section->output_offset;
1535 if (reloc_entry->address > input_section->_cooked_size)
1536 return bfd_reloc_outofrange;
1538 if (reloc_entry->howto->src_mask == 0)
1540 /* This case arises with the 64-bit MIPS ELF ABI. */
1541 val = 0;
1543 else
1544 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1546 /* Set val to the offset into the section or symbol. */
1547 val += reloc_entry->addend;
1549 /* Adjust val for the final section location and GP value. If we
1550 are producing relocateable output, we don't want to do this for
1551 an external symbol. */
1552 if (! relocateable
1553 || (symbol->flags & BSF_SECTION_SYM) != 0)
1554 val += relocation - gp;
1556 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1558 if (relocateable)
1559 reloc_entry->address += input_section->output_offset;
1561 return bfd_reloc_ok;
1564 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1565 generated when addreses are 64 bits. The upper 32 bits are a simle
1566 sign extension. */
1568 static bfd_reloc_status_type
1569 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1570 output_bfd, error_message)
1571 bfd *abfd;
1572 arelent *reloc_entry;
1573 asymbol *symbol;
1574 PTR data;
1575 asection *input_section;
1576 bfd *output_bfd;
1577 char **error_message;
1579 bfd_reloc_status_type r;
1580 arelent reloc32;
1581 unsigned long val;
1582 bfd_size_type addr;
1584 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1585 input_section, output_bfd, error_message);
1586 if (r != bfd_reloc_continue)
1587 return r;
1589 /* Do a normal 32 bit relocation on the lower 32 bits. */
1590 reloc32 = *reloc_entry;
1591 if (bfd_big_endian (abfd))
1592 reloc32.address += 4;
1593 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1594 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1595 output_bfd, error_message);
1597 /* Sign extend into the upper 32 bits. */
1598 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1599 if ((val & 0x80000000) != 0)
1600 val = 0xffffffff;
1601 else
1602 val = 0;
1603 addr = reloc_entry->address;
1604 if (bfd_little_endian (abfd))
1605 addr += 4;
1606 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1608 return r;
1611 /* Handle a mips16 jump. */
1613 static bfd_reloc_status_type
1614 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1615 output_bfd, error_message)
1616 bfd *abfd ATTRIBUTE_UNUSED;
1617 arelent *reloc_entry;
1618 asymbol *symbol;
1619 PTR data ATTRIBUTE_UNUSED;
1620 asection *input_section;
1621 bfd *output_bfd;
1622 char **error_message ATTRIBUTE_UNUSED;
1624 if (output_bfd != (bfd *) NULL
1625 && (symbol->flags & BSF_SECTION_SYM) == 0
1626 && reloc_entry->addend == 0)
1628 reloc_entry->address += input_section->output_offset;
1629 return bfd_reloc_ok;
1632 /* FIXME. */
1634 static boolean warned;
1636 if (! warned)
1637 (*_bfd_error_handler)
1638 (_("Linking mips16 objects into %s format is not supported"),
1639 bfd_get_target (input_section->output_section->owner));
1640 warned = true;
1643 return bfd_reloc_undefined;
1646 /* Handle a mips16 GP relative reloc. */
1648 static bfd_reloc_status_type
1649 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1650 output_bfd, error_message)
1651 bfd *abfd;
1652 arelent *reloc_entry;
1653 asymbol *symbol;
1654 PTR data;
1655 asection *input_section;
1656 bfd *output_bfd;
1657 char **error_message;
1659 boolean relocateable;
1660 bfd_reloc_status_type ret;
1661 bfd_vma gp;
1662 unsigned short extend, insn;
1663 unsigned long final;
1665 /* If we're relocating, and this is an external symbol with no
1666 addend, we don't want to change anything. We will only have an
1667 addend if this is a newly created reloc, not read from an ELF
1668 file. */
1669 if (output_bfd != NULL
1670 && (symbol->flags & BSF_SECTION_SYM) == 0
1671 && reloc_entry->addend == 0)
1673 reloc_entry->address += input_section->output_offset;
1674 return bfd_reloc_ok;
1677 if (output_bfd != NULL)
1678 relocateable = true;
1679 else
1681 relocateable = false;
1682 output_bfd = symbol->section->output_section->owner;
1685 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1686 &gp);
1687 if (ret != bfd_reloc_ok)
1688 return ret;
1690 if (reloc_entry->address > input_section->_cooked_size)
1691 return bfd_reloc_outofrange;
1693 /* Pick up the mips16 extend instruction and the real instruction. */
1694 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1695 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1697 /* Stuff the current addend back as a 32 bit value, do the usual
1698 relocation, and then clean up. */
1699 bfd_put_32 (abfd,
1700 (((extend & 0x1f) << 11)
1701 | (extend & 0x7e0)
1702 | (insn & 0x1f)),
1703 (bfd_byte *) data + reloc_entry->address);
1705 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1706 relocateable, data, gp);
1708 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1709 bfd_put_16 (abfd,
1710 ((extend & 0xf800)
1711 | ((final >> 11) & 0x1f)
1712 | (final & 0x7e0)),
1713 (bfd_byte *) data + reloc_entry->address);
1714 bfd_put_16 (abfd,
1715 ((insn & 0xffe0)
1716 | (final & 0x1f)),
1717 (bfd_byte *) data + reloc_entry->address + 2);
1719 return ret;
1722 /* Return the ISA for a MIPS e_flags value. */
1724 static INLINE int
1725 elf_mips_isa (flags)
1726 flagword flags;
1728 switch (flags & EF_MIPS_ARCH)
1730 case E_MIPS_ARCH_1:
1731 return 1;
1732 case E_MIPS_ARCH_2:
1733 return 2;
1734 case E_MIPS_ARCH_3:
1735 return 3;
1736 case E_MIPS_ARCH_4:
1737 return 4;
1739 return 4;
1742 /* Return the MACH for a MIPS e_flags value. */
1744 static INLINE int
1745 elf_mips_mach (flags)
1746 flagword flags;
1748 switch (flags & EF_MIPS_MACH)
1750 case E_MIPS_MACH_3900:
1751 return bfd_mach_mips3900;
1753 case E_MIPS_MACH_4010:
1754 return bfd_mach_mips4010;
1756 case E_MIPS_MACH_4100:
1757 return bfd_mach_mips4100;
1759 case E_MIPS_MACH_4111:
1760 return bfd_mach_mips4111;
1762 case E_MIPS_MACH_4650:
1763 return bfd_mach_mips4650;
1765 default:
1766 switch (flags & EF_MIPS_ARCH)
1768 default:
1769 case E_MIPS_ARCH_1:
1770 return bfd_mach_mips3000;
1771 break;
1773 case E_MIPS_ARCH_2:
1774 return bfd_mach_mips6000;
1775 break;
1777 case E_MIPS_ARCH_3:
1778 return bfd_mach_mips4000;
1779 break;
1781 case E_MIPS_ARCH_4:
1782 return bfd_mach_mips8000;
1783 break;
1787 return 0;
1790 /* Return printable name for ABI. */
1792 static INLINE char*
1793 elf_mips_abi_name (abfd)
1794 bfd *abfd;
1796 flagword flags;
1798 if (ABI_N32_P (abfd))
1799 return "N32";
1800 else if (ABI_64_P (abfd))
1801 return "64";
1803 flags = elf_elfheader (abfd)->e_flags;
1804 switch (flags & EF_MIPS_ABI)
1806 case 0:
1807 return "none";
1808 case E_MIPS_ABI_O32:
1809 return "O32";
1810 case E_MIPS_ABI_O64:
1811 return "O64";
1812 case E_MIPS_ABI_EABI32:
1813 return "EABI32";
1814 case E_MIPS_ABI_EABI64:
1815 return "EABI64";
1816 default:
1817 return "unknown abi";
1821 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1823 struct elf_reloc_map {
1824 bfd_reloc_code_real_type bfd_reloc_val;
1825 enum elf_mips_reloc_type elf_reloc_val;
1828 static CONST struct elf_reloc_map mips_reloc_map[] =
1830 { BFD_RELOC_NONE, R_MIPS_NONE, },
1831 { BFD_RELOC_16, R_MIPS_16 },
1832 { BFD_RELOC_32, R_MIPS_32 },
1833 { BFD_RELOC_64, R_MIPS_64 },
1834 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1835 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1836 { BFD_RELOC_LO16, R_MIPS_LO16 },
1837 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1838 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1839 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1840 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1841 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1842 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1843 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1844 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1845 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1846 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1847 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1848 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1849 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1850 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1853 /* Given a BFD reloc type, return a howto structure. */
1855 static reloc_howto_type *
1856 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1857 bfd *abfd;
1858 bfd_reloc_code_real_type code;
1860 unsigned int i;
1862 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1864 if (mips_reloc_map[i].bfd_reloc_val == code)
1865 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1868 switch (code)
1870 default:
1871 bfd_set_error (bfd_error_bad_value);
1872 return NULL;
1874 case BFD_RELOC_CTOR:
1875 /* We need to handle BFD_RELOC_CTOR specially.
1876 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1877 size of addresses on this architecture. */
1878 if (bfd_arch_bits_per_address (abfd) == 32)
1879 return &elf_mips_howto_table[(int) R_MIPS_32];
1880 else
1881 return &elf_mips_ctor64_howto;
1883 case BFD_RELOC_MIPS16_JMP:
1884 return &elf_mips16_jump_howto;
1885 case BFD_RELOC_MIPS16_GPREL:
1886 return &elf_mips16_gprel_howto;
1887 case BFD_RELOC_VTABLE_INHERIT:
1888 return &elf_mips_gnu_vtinherit_howto;
1889 case BFD_RELOC_VTABLE_ENTRY:
1890 return &elf_mips_gnu_vtentry_howto;
1894 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1896 static reloc_howto_type *
1897 mips_rtype_to_howto (r_type)
1898 unsigned int r_type;
1900 switch (r_type)
1902 case R_MIPS16_26:
1903 return &elf_mips16_jump_howto;
1904 break;
1905 case R_MIPS16_GPREL:
1906 return &elf_mips16_gprel_howto;
1907 break;
1908 case R_MIPS_GNU_VTINHERIT:
1909 return &elf_mips_gnu_vtinherit_howto;
1910 break;
1911 case R_MIPS_GNU_VTENTRY:
1912 return &elf_mips_gnu_vtentry_howto;
1913 break;
1915 default:
1916 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1917 return &elf_mips_howto_table[r_type];
1918 break;
1922 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1924 static void
1925 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1926 bfd *abfd;
1927 arelent *cache_ptr;
1928 Elf32_Internal_Rel *dst;
1930 unsigned int r_type;
1932 r_type = ELF32_R_TYPE (dst->r_info);
1933 cache_ptr->howto = mips_rtype_to_howto (r_type);
1935 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1936 value for the object file. We get the addend now, rather than
1937 when we do the relocation, because the symbol manipulations done
1938 by the linker may cause us to lose track of the input BFD. */
1939 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1940 && (r_type == (unsigned int) R_MIPS_GPREL16
1941 || r_type == (unsigned int) R_MIPS_LITERAL))
1942 cache_ptr->addend = elf_gp (abfd);
1945 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1947 static void
1948 mips_info_to_howto_rela (abfd, cache_ptr, dst)
1949 bfd *abfd;
1950 arelent *cache_ptr;
1951 Elf32_Internal_Rela *dst;
1953 /* Since an Elf32_Internal_Rel is an initial prefix of an
1954 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1955 above. */
1956 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1958 /* If we ever need to do any extra processing with dst->r_addend
1959 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1962 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1963 routines swap this structure in and out. They are used outside of
1964 BFD, so they are globally visible. */
1966 void
1967 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1968 bfd *abfd;
1969 const Elf32_External_RegInfo *ex;
1970 Elf32_RegInfo *in;
1972 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1973 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1974 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1975 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1976 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1977 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1980 void
1981 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1982 bfd *abfd;
1983 const Elf32_RegInfo *in;
1984 Elf32_External_RegInfo *ex;
1986 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1987 (bfd_byte *) ex->ri_gprmask);
1988 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1989 (bfd_byte *) ex->ri_cprmask[0]);
1990 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1991 (bfd_byte *) ex->ri_cprmask[1]);
1992 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1993 (bfd_byte *) ex->ri_cprmask[2]);
1994 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1995 (bfd_byte *) ex->ri_cprmask[3]);
1996 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1997 (bfd_byte *) ex->ri_gp_value);
2000 /* In the 64 bit ABI, the .MIPS.options section holds register
2001 information in an Elf64_Reginfo structure. These routines swap
2002 them in and out. They are globally visible because they are used
2003 outside of BFD. These routines are here so that gas can call them
2004 without worrying about whether the 64 bit ABI has been included. */
2006 void
2007 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
2008 bfd *abfd;
2009 const Elf64_External_RegInfo *ex;
2010 Elf64_Internal_RegInfo *in;
2012 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
2013 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
2014 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
2015 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
2016 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
2017 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
2018 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
2021 void
2022 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
2023 bfd *abfd;
2024 const Elf64_Internal_RegInfo *in;
2025 Elf64_External_RegInfo *ex;
2027 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
2028 (bfd_byte *) ex->ri_gprmask);
2029 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
2030 (bfd_byte *) ex->ri_pad);
2031 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
2032 (bfd_byte *) ex->ri_cprmask[0]);
2033 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
2034 (bfd_byte *) ex->ri_cprmask[1]);
2035 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
2036 (bfd_byte *) ex->ri_cprmask[2]);
2037 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
2038 (bfd_byte *) ex->ri_cprmask[3]);
2039 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
2040 (bfd_byte *) ex->ri_gp_value);
2043 /* Swap an entry in a .gptab section. Note that these routines rely
2044 on the equivalence of the two elements of the union. */
2046 static void
2047 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
2048 bfd *abfd;
2049 const Elf32_External_gptab *ex;
2050 Elf32_gptab *in;
2052 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
2053 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
2056 static void
2057 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
2058 bfd *abfd;
2059 const Elf32_gptab *in;
2060 Elf32_External_gptab *ex;
2062 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
2063 ex->gt_entry.gt_g_value);
2064 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
2065 ex->gt_entry.gt_bytes);
2068 static void
2069 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
2070 bfd *abfd;
2071 const Elf32_compact_rel *in;
2072 Elf32_External_compact_rel *ex;
2074 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
2075 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
2076 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
2077 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
2078 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
2079 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
2082 static void
2083 bfd_elf32_swap_crinfo_out (abfd, in, ex)
2084 bfd *abfd;
2085 const Elf32_crinfo *in;
2086 Elf32_External_crinfo *ex;
2088 unsigned long l;
2090 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2091 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2092 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2093 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2094 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
2095 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
2096 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
2099 /* Swap in an options header. */
2101 void
2102 bfd_mips_elf_swap_options_in (abfd, ex, in)
2103 bfd *abfd;
2104 const Elf_External_Options *ex;
2105 Elf_Internal_Options *in;
2107 in->kind = bfd_h_get_8 (abfd, ex->kind);
2108 in->size = bfd_h_get_8 (abfd, ex->size);
2109 in->section = bfd_h_get_16 (abfd, ex->section);
2110 in->info = bfd_h_get_32 (abfd, ex->info);
2113 /* Swap out an options header. */
2115 void
2116 bfd_mips_elf_swap_options_out (abfd, in, ex)
2117 bfd *abfd;
2118 const Elf_Internal_Options *in;
2119 Elf_External_Options *ex;
2121 bfd_h_put_8 (abfd, in->kind, ex->kind);
2122 bfd_h_put_8 (abfd, in->size, ex->size);
2123 bfd_h_put_16 (abfd, in->section, ex->section);
2124 bfd_h_put_32 (abfd, in->info, ex->info);
2127 /* Swap in an MSYM entry. */
2129 static void
2130 bfd_mips_elf_swap_msym_in (abfd, ex, in)
2131 bfd *abfd;
2132 const Elf32_External_Msym *ex;
2133 Elf32_Internal_Msym *in;
2135 in->ms_hash_value = bfd_h_get_32 (abfd, ex->ms_hash_value);
2136 in->ms_info = bfd_h_get_32 (abfd, ex->ms_info);
2139 /* Swap out an MSYM entry. */
2141 static void
2142 bfd_mips_elf_swap_msym_out (abfd, in, ex)
2143 bfd *abfd;
2144 const Elf32_Internal_Msym *in;
2145 Elf32_External_Msym *ex;
2147 bfd_h_put_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
2148 bfd_h_put_32 (abfd, in->ms_info, ex->ms_info);
2152 /* Determine whether a symbol is global for the purposes of splitting
2153 the symbol table into global symbols and local symbols. At least
2154 on Irix 5, this split must be between section symbols and all other
2155 symbols. On most ELF targets the split is between static symbols
2156 and externally visible symbols. */
2158 /*ARGSUSED*/
2159 static boolean
2160 mips_elf_sym_is_global (abfd, sym)
2161 bfd *abfd ATTRIBUTE_UNUSED;
2162 asymbol *sym;
2164 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
2167 /* Set the right machine number for a MIPS ELF file. This is used for
2168 both the 32-bit and the 64-bit ABI. */
2170 boolean
2171 _bfd_mips_elf_object_p (abfd)
2172 bfd *abfd;
2174 /* Irix 5 and 6 is broken. Object file symbol tables are not always
2175 sorted correctly such that local symbols precede global symbols,
2176 and the sh_info field in the symbol table is not always right. */
2177 elf_bad_symtab (abfd) = true;
2179 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
2180 elf_mips_mach (elf_elfheader (abfd)->e_flags));
2181 return true;
2184 /* The final processing done just before writing out a MIPS ELF object
2185 file. This gets the MIPS architecture right based on the machine
2186 number. This is used by both the 32-bit and the 64-bit ABI. */
2188 /*ARGSUSED*/
2189 void
2190 _bfd_mips_elf_final_write_processing (abfd, linker)
2191 bfd *abfd;
2192 boolean linker ATTRIBUTE_UNUSED;
2194 unsigned long val;
2195 unsigned int i;
2196 Elf_Internal_Shdr **hdrpp;
2197 const char *name;
2198 asection *sec;
2200 switch (bfd_get_mach (abfd))
2202 default:
2203 case bfd_mach_mips3000:
2204 val = E_MIPS_ARCH_1;
2205 break;
2207 case bfd_mach_mips3900:
2208 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
2209 break;
2211 case bfd_mach_mips6000:
2212 val = E_MIPS_ARCH_2;
2213 break;
2215 case bfd_mach_mips4000:
2216 case bfd_mach_mips4300:
2217 val = E_MIPS_ARCH_3;
2218 break;
2220 case bfd_mach_mips4010:
2221 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
2222 break;
2224 case bfd_mach_mips4100:
2225 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2226 break;
2228 case bfd_mach_mips4111:
2229 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2230 break;
2232 case bfd_mach_mips4650:
2233 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2234 break;
2236 case bfd_mach_mips8000:
2237 val = E_MIPS_ARCH_4;
2238 break;
2241 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2242 elf_elfheader (abfd)->e_flags |= val;
2244 /* Set the sh_info field for .gptab sections and other appropriate
2245 info for each special section. */
2246 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2247 i < elf_elfheader (abfd)->e_shnum;
2248 i++, hdrpp++)
2250 switch ((*hdrpp)->sh_type)
2252 case SHT_MIPS_MSYM:
2253 case SHT_MIPS_LIBLIST:
2254 sec = bfd_get_section_by_name (abfd, ".dynstr");
2255 if (sec != NULL)
2256 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2257 break;
2259 case SHT_MIPS_GPTAB:
2260 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2261 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2262 BFD_ASSERT (name != NULL
2263 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2264 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2265 BFD_ASSERT (sec != NULL);
2266 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2267 break;
2269 case SHT_MIPS_CONTENT:
2270 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2271 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2272 BFD_ASSERT (name != NULL
2273 && strncmp (name, ".MIPS.content",
2274 sizeof ".MIPS.content" - 1) == 0);
2275 sec = bfd_get_section_by_name (abfd,
2276 name + sizeof ".MIPS.content" - 1);
2277 BFD_ASSERT (sec != NULL);
2278 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2279 break;
2281 case SHT_MIPS_SYMBOL_LIB:
2282 sec = bfd_get_section_by_name (abfd, ".dynsym");
2283 if (sec != NULL)
2284 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2285 sec = bfd_get_section_by_name (abfd, ".liblist");
2286 if (sec != NULL)
2287 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2288 break;
2290 case SHT_MIPS_EVENTS:
2291 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2292 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2293 BFD_ASSERT (name != NULL);
2294 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2295 sec = bfd_get_section_by_name (abfd,
2296 name + sizeof ".MIPS.events" - 1);
2297 else
2299 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2300 sizeof ".MIPS.post_rel" - 1) == 0);
2301 sec = bfd_get_section_by_name (abfd,
2302 (name
2303 + sizeof ".MIPS.post_rel" - 1));
2305 BFD_ASSERT (sec != NULL);
2306 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2307 break;
2313 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2315 boolean
2316 _bfd_mips_elf_set_private_flags (abfd, flags)
2317 bfd *abfd;
2318 flagword flags;
2320 BFD_ASSERT (!elf_flags_init (abfd)
2321 || elf_elfheader (abfd)->e_flags == flags);
2323 elf_elfheader (abfd)->e_flags = flags;
2324 elf_flags_init (abfd) = true;
2325 return true;
2328 /* Copy backend specific data from one object module to another */
2330 boolean
2331 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2332 bfd *ibfd;
2333 bfd *obfd;
2335 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2336 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2337 return true;
2339 BFD_ASSERT (!elf_flags_init (obfd)
2340 || (elf_elfheader (obfd)->e_flags
2341 == elf_elfheader (ibfd)->e_flags));
2343 elf_gp (obfd) = elf_gp (ibfd);
2344 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2345 elf_flags_init (obfd) = true;
2346 return true;
2349 /* Merge backend specific data from an object file to the output
2350 object file when linking. */
2352 boolean
2353 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2354 bfd *ibfd;
2355 bfd *obfd;
2357 flagword old_flags;
2358 flagword new_flags;
2359 boolean ok;
2361 /* Check if we have the same endianess */
2362 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2363 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2365 const char *msg;
2367 if (bfd_big_endian (ibfd))
2368 msg = _("%s: compiled for a big endian system and target is little endian");
2369 else
2370 msg = _("%s: compiled for a little endian system and target is big endian");
2372 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2374 bfd_set_error (bfd_error_wrong_format);
2375 return false;
2378 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2379 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2380 return true;
2382 new_flags = elf_elfheader (ibfd)->e_flags;
2383 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2384 old_flags = elf_elfheader (obfd)->e_flags;
2386 if (! elf_flags_init (obfd))
2388 elf_flags_init (obfd) = true;
2389 elf_elfheader (obfd)->e_flags = new_flags;
2390 elf_elfheader (obfd)->e_ident[EI_CLASS]
2391 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
2393 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2394 && bfd_get_arch_info (obfd)->the_default)
2396 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2397 bfd_get_mach (ibfd)))
2398 return false;
2401 return true;
2404 /* Check flag compatibility. */
2406 new_flags &= ~EF_MIPS_NOREORDER;
2407 old_flags &= ~EF_MIPS_NOREORDER;
2409 if (new_flags == old_flags)
2410 return true;
2412 ok = true;
2414 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2416 new_flags &= ~EF_MIPS_PIC;
2417 old_flags &= ~EF_MIPS_PIC;
2418 (*_bfd_error_handler)
2419 (_("%s: linking PIC files with non-PIC files"),
2420 bfd_get_filename (ibfd));
2421 ok = false;
2424 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2426 new_flags &= ~EF_MIPS_CPIC;
2427 old_flags &= ~EF_MIPS_CPIC;
2428 (*_bfd_error_handler)
2429 (_("%s: linking abicalls files with non-abicalls files"),
2430 bfd_get_filename (ibfd));
2431 ok = false;
2434 /* Compare the ISA's. */
2435 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2436 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2438 int new_mach = new_flags & EF_MIPS_MACH;
2439 int old_mach = old_flags & EF_MIPS_MACH;
2440 int new_isa = elf_mips_isa (new_flags);
2441 int old_isa = elf_mips_isa (old_flags);
2443 /* If either has no machine specified, just compare the general isa's.
2444 Some combinations of machines are ok, if the isa's match. */
2445 if (! new_mach
2446 || ! old_mach
2447 || new_mach == old_mach
2450 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2451 and -mips4 code. They will normally use the same data sizes and
2452 calling conventions. */
2454 if ((new_isa == 1 || new_isa == 2)
2455 ? (old_isa != 1 && old_isa != 2)
2456 : (old_isa == 1 || old_isa == 2))
2458 (*_bfd_error_handler)
2459 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2460 bfd_get_filename (ibfd), new_isa, old_isa);
2461 ok = false;
2465 else
2467 (*_bfd_error_handler)
2468 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2469 bfd_get_filename (ibfd),
2470 elf_mips_mach (new_flags),
2471 elf_mips_mach (old_flags));
2472 ok = false;
2475 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2476 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2479 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
2480 does set EI_CLASS differently from any 32-bit ABI. */
2481 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
2482 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2483 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2485 /* Only error if both are set (to different values). */
2486 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
2487 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
2488 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
2490 (*_bfd_error_handler)
2491 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2492 bfd_get_filename (ibfd),
2493 elf_mips_abi_name (ibfd),
2494 elf_mips_abi_name (obfd));
2495 ok = false;
2497 new_flags &= ~EF_MIPS_ABI;
2498 old_flags &= ~EF_MIPS_ABI;
2501 /* Warn about any other mismatches */
2502 if (new_flags != old_flags)
2504 (*_bfd_error_handler)
2505 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2506 bfd_get_filename (ibfd), (unsigned long) new_flags,
2507 (unsigned long) old_flags);
2508 ok = false;
2511 if (! ok)
2513 bfd_set_error (bfd_error_bad_value);
2514 return false;
2517 return true;
2520 boolean
2521 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2522 bfd *abfd;
2523 PTR ptr;
2525 FILE *file = (FILE *) ptr;
2527 BFD_ASSERT (abfd != NULL && ptr != NULL);
2529 /* Print normal ELF private data. */
2530 _bfd_elf_print_private_bfd_data (abfd, ptr);
2532 /* xgettext:c-format */
2533 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2535 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2536 fprintf (file, _ (" [abi=O32]"));
2537 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2538 fprintf (file, _ (" [abi=O64]"));
2539 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2540 fprintf (file, _ (" [abi=EABI32]"));
2541 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2542 fprintf (file, _ (" [abi=EABI64]"));
2543 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2544 fprintf (file, _ (" [abi unknown]"));
2545 else if (ABI_N32_P (abfd))
2546 fprintf (file, _ (" [abi=N32]"));
2547 else if (ABI_64_P (abfd))
2548 fprintf (file, _ (" [abi=64]"));
2549 else
2550 fprintf (file, _ (" [no abi set]"));
2552 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2553 fprintf (file, _ (" [mips1]"));
2554 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2555 fprintf (file, _ (" [mips2]"));
2556 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2557 fprintf (file, _ (" [mips3]"));
2558 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2559 fprintf (file, _ (" [mips4]"));
2560 else
2561 fprintf (file, _ (" [unknown ISA]"));
2563 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2564 fprintf (file, _ (" [32bitmode]"));
2565 else
2566 fprintf (file, _ (" [not 32bitmode]"));
2568 fputc ('\n', file);
2570 return true;
2573 /* Handle a MIPS specific section when reading an object file. This
2574 is called when elfcode.h finds a section with an unknown type.
2575 This routine supports both the 32-bit and 64-bit ELF ABI.
2577 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2578 how to. */
2580 boolean
2581 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2582 bfd *abfd;
2583 Elf_Internal_Shdr *hdr;
2584 char *name;
2586 flagword flags = 0;
2588 /* There ought to be a place to keep ELF backend specific flags, but
2589 at the moment there isn't one. We just keep track of the
2590 sections by their name, instead. Fortunately, the ABI gives
2591 suggested names for all the MIPS specific sections, so we will
2592 probably get away with this. */
2593 switch (hdr->sh_type)
2595 case SHT_MIPS_LIBLIST:
2596 if (strcmp (name, ".liblist") != 0)
2597 return false;
2598 break;
2599 case SHT_MIPS_MSYM:
2600 if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) != 0)
2601 return false;
2602 break;
2603 case SHT_MIPS_CONFLICT:
2604 if (strcmp (name, ".conflict") != 0)
2605 return false;
2606 break;
2607 case SHT_MIPS_GPTAB:
2608 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2609 return false;
2610 break;
2611 case SHT_MIPS_UCODE:
2612 if (strcmp (name, ".ucode") != 0)
2613 return false;
2614 break;
2615 case SHT_MIPS_DEBUG:
2616 if (strcmp (name, ".mdebug") != 0)
2617 return false;
2618 flags = SEC_DEBUGGING;
2619 break;
2620 case SHT_MIPS_REGINFO:
2621 if (strcmp (name, ".reginfo") != 0
2622 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2623 return false;
2624 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2625 break;
2626 case SHT_MIPS_IFACE:
2627 if (strcmp (name, ".MIPS.interfaces") != 0)
2628 return false;
2629 break;
2630 case SHT_MIPS_CONTENT:
2631 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2632 return false;
2633 break;
2634 case SHT_MIPS_OPTIONS:
2635 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
2636 return false;
2637 break;
2638 case SHT_MIPS_DWARF:
2639 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2640 return false;
2641 break;
2642 case SHT_MIPS_SYMBOL_LIB:
2643 if (strcmp (name, ".MIPS.symlib") != 0)
2644 return false;
2645 break;
2646 case SHT_MIPS_EVENTS:
2647 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2648 && strncmp (name, ".MIPS.post_rel",
2649 sizeof ".MIPS.post_rel" - 1) != 0)
2650 return false;
2651 break;
2652 default:
2653 return false;
2656 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2657 return false;
2659 if (flags)
2661 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2662 (bfd_get_section_flags (abfd,
2663 hdr->bfd_section)
2664 | flags)))
2665 return false;
2668 /* FIXME: We should record sh_info for a .gptab section. */
2670 /* For a .reginfo section, set the gp value in the tdata information
2671 from the contents of this section. We need the gp value while
2672 processing relocs, so we just get it now. The .reginfo section
2673 is not used in the 64-bit MIPS ELF ABI. */
2674 if (hdr->sh_type == SHT_MIPS_REGINFO)
2676 Elf32_External_RegInfo ext;
2677 Elf32_RegInfo s;
2679 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2680 (file_ptr) 0, sizeof ext))
2681 return false;
2682 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2683 elf_gp (abfd) = s.ri_gp_value;
2686 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2687 set the gp value based on what we find. We may see both
2688 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2689 they should agree. */
2690 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2692 bfd_byte *contents, *l, *lend;
2694 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2695 if (contents == NULL)
2696 return false;
2697 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2698 (file_ptr) 0, hdr->sh_size))
2700 free (contents);
2701 return false;
2703 l = contents;
2704 lend = contents + hdr->sh_size;
2705 while (l + sizeof (Elf_External_Options) <= lend)
2707 Elf_Internal_Options intopt;
2709 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2710 &intopt);
2711 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2713 Elf64_Internal_RegInfo intreg;
2715 bfd_mips_elf64_swap_reginfo_in
2716 (abfd,
2717 ((Elf64_External_RegInfo *)
2718 (l + sizeof (Elf_External_Options))),
2719 &intreg);
2720 elf_gp (abfd) = intreg.ri_gp_value;
2722 else if (intopt.kind == ODK_REGINFO)
2724 Elf32_RegInfo intreg;
2726 bfd_mips_elf32_swap_reginfo_in
2727 (abfd,
2728 ((Elf32_External_RegInfo *)
2729 (l + sizeof (Elf_External_Options))),
2730 &intreg);
2731 elf_gp (abfd) = intreg.ri_gp_value;
2733 l += intopt.size;
2735 free (contents);
2738 return true;
2741 /* Set the correct type for a MIPS ELF section. We do this by the
2742 section name, which is a hack, but ought to work. This routine is
2743 used by both the 32-bit and the 64-bit ABI. */
2745 boolean
2746 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2747 bfd *abfd;
2748 Elf32_Internal_Shdr *hdr;
2749 asection *sec;
2751 register const char *name;
2753 name = bfd_get_section_name (abfd, sec);
2755 if (strcmp (name, ".liblist") == 0)
2757 hdr->sh_type = SHT_MIPS_LIBLIST;
2758 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2759 /* The sh_link field is set in final_write_processing. */
2761 else if (strcmp (name, ".conflict") == 0)
2762 hdr->sh_type = SHT_MIPS_CONFLICT;
2763 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2765 hdr->sh_type = SHT_MIPS_GPTAB;
2766 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2767 /* The sh_info field is set in final_write_processing. */
2769 else if (strcmp (name, ".ucode") == 0)
2770 hdr->sh_type = SHT_MIPS_UCODE;
2771 else if (strcmp (name, ".mdebug") == 0)
2773 hdr->sh_type = SHT_MIPS_DEBUG;
2774 /* In a shared object on Irix 5.3, the .mdebug section has an
2775 entsize of 0. FIXME: Does this matter? */
2776 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2777 hdr->sh_entsize = 0;
2778 else
2779 hdr->sh_entsize = 1;
2781 else if (strcmp (name, ".reginfo") == 0)
2783 hdr->sh_type = SHT_MIPS_REGINFO;
2784 /* In a shared object on Irix 5.3, the .reginfo section has an
2785 entsize of 0x18. FIXME: Does this matter? */
2786 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2787 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2788 else
2789 hdr->sh_entsize = 1;
2791 else if (SGI_COMPAT (abfd)
2792 && (strcmp (name, ".hash") == 0
2793 || strcmp (name, ".dynamic") == 0
2794 || strcmp (name, ".dynstr") == 0))
2796 hdr->sh_entsize = 0;
2797 #if 0
2798 /* This isn't how the Irix 6 linker behaves. */
2799 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2800 #endif
2802 else if (strcmp (name, ".got") == 0
2803 || strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0
2804 || strcmp (name, ".sdata") == 0
2805 || strcmp (name, ".sbss") == 0
2806 || strcmp (name, ".lit4") == 0
2807 || strcmp (name, ".lit8") == 0)
2808 hdr->sh_flags |= SHF_MIPS_GPREL;
2809 else if (strcmp (name, ".MIPS.interfaces") == 0)
2811 hdr->sh_type = SHT_MIPS_IFACE;
2812 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2814 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
2816 hdr->sh_type = SHT_MIPS_CONTENT;
2817 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2818 /* The sh_info field is set in final_write_processing. */
2820 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2822 hdr->sh_type = SHT_MIPS_OPTIONS;
2823 hdr->sh_entsize = 1;
2824 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2826 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2827 hdr->sh_type = SHT_MIPS_DWARF;
2828 else if (strcmp (name, ".MIPS.symlib") == 0)
2830 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2831 /* The sh_link and sh_info fields are set in
2832 final_write_processing. */
2834 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2835 || strncmp (name, ".MIPS.post_rel",
2836 sizeof ".MIPS.post_rel" - 1) == 0)
2838 hdr->sh_type = SHT_MIPS_EVENTS;
2839 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2840 /* The sh_link field is set in final_write_processing. */
2842 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (abfd)) == 0)
2844 hdr->sh_type = SHT_MIPS_MSYM;
2845 hdr->sh_flags |= SHF_ALLOC;
2846 hdr->sh_entsize = 8;
2849 /* The generic elf_fake_sections will set up REL_HDR using the
2850 default kind of relocations. But, we may actually need both
2851 kinds of relocations, so we set up the second header here. */
2852 if ((sec->flags & SEC_RELOC) != 0)
2854 struct bfd_elf_section_data *esd;
2856 esd = elf_section_data (sec);
2857 BFD_ASSERT (esd->rel_hdr2 == NULL);
2858 esd->rel_hdr2
2859 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2860 if (!esd->rel_hdr2)
2861 return false;
2862 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
2863 !elf_section_data (sec)->use_rela_p);
2866 return true;
2869 /* Given a BFD section, try to locate the corresponding ELF section
2870 index. This is used by both the 32-bit and the 64-bit ABI.
2871 Actually, it's not clear to me that the 64-bit ABI supports these,
2872 but for non-PIC objects we will certainly want support for at least
2873 the .scommon section. */
2875 boolean
2876 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2877 bfd *abfd ATTRIBUTE_UNUSED;
2878 Elf32_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
2879 asection *sec;
2880 int *retval;
2882 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2884 *retval = SHN_MIPS_SCOMMON;
2885 return true;
2887 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2889 *retval = SHN_MIPS_ACOMMON;
2890 return true;
2892 return false;
2895 /* When are writing out the .options or .MIPS.options section,
2896 remember the bytes we are writing out, so that we can install the
2897 GP value in the section_processing routine. */
2899 boolean
2900 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2901 bfd *abfd;
2902 sec_ptr section;
2903 PTR location;
2904 file_ptr offset;
2905 bfd_size_type count;
2907 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
2909 bfd_byte *c;
2911 if (elf_section_data (section) == NULL)
2913 section->used_by_bfd =
2914 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2915 if (elf_section_data (section) == NULL)
2916 return false;
2918 c = (bfd_byte *) elf_section_data (section)->tdata;
2919 if (c == NULL)
2921 bfd_size_type size;
2923 if (section->_cooked_size != 0)
2924 size = section->_cooked_size;
2925 else
2926 size = section->_raw_size;
2927 c = (bfd_byte *) bfd_zalloc (abfd, size);
2928 if (c == NULL)
2929 return false;
2930 elf_section_data (section)->tdata = (PTR) c;
2933 memcpy (c + offset, location, count);
2936 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2937 count);
2940 /* Work over a section just before writing it out. This routine is
2941 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2942 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2943 a better way. */
2945 boolean
2946 _bfd_mips_elf_section_processing (abfd, hdr)
2947 bfd *abfd;
2948 Elf_Internal_Shdr *hdr;
2950 if (hdr->sh_type == SHT_MIPS_REGINFO
2951 && hdr->sh_size > 0)
2953 bfd_byte buf[4];
2955 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2956 BFD_ASSERT (hdr->contents == NULL);
2958 if (bfd_seek (abfd,
2959 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2960 SEEK_SET) == -1)
2961 return false;
2962 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2963 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2964 return false;
2967 if (hdr->sh_type == SHT_MIPS_OPTIONS
2968 && hdr->bfd_section != NULL
2969 && elf_section_data (hdr->bfd_section) != NULL
2970 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2972 bfd_byte *contents, *l, *lend;
2974 /* We stored the section contents in the elf_section_data tdata
2975 field in the set_section_contents routine. We save the
2976 section contents so that we don't have to read them again.
2977 At this point we know that elf_gp is set, so we can look
2978 through the section contents to see if there is an
2979 ODK_REGINFO structure. */
2981 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2982 l = contents;
2983 lend = contents + hdr->sh_size;
2984 while (l + sizeof (Elf_External_Options) <= lend)
2986 Elf_Internal_Options intopt;
2988 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2989 &intopt);
2990 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
2992 bfd_byte buf[8];
2994 if (bfd_seek (abfd,
2995 (hdr->sh_offset
2996 + (l - contents)
2997 + sizeof (Elf_External_Options)
2998 + (sizeof (Elf64_External_RegInfo) - 8)),
2999 SEEK_SET) == -1)
3000 return false;
3001 bfd_h_put_64 (abfd, elf_gp (abfd), buf);
3002 if (bfd_write (buf, 1, 8, abfd) != 8)
3003 return false;
3005 else if (intopt.kind == ODK_REGINFO)
3007 bfd_byte buf[4];
3009 if (bfd_seek (abfd,
3010 (hdr->sh_offset
3011 + (l - contents)
3012 + sizeof (Elf_External_Options)
3013 + (sizeof (Elf32_External_RegInfo) - 4)),
3014 SEEK_SET) == -1)
3015 return false;
3016 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
3017 if (bfd_write (buf, 1, 4, abfd) != 4)
3018 return false;
3020 l += intopt.size;
3024 if (hdr->bfd_section != NULL)
3026 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3028 if (strcmp (name, ".sdata") == 0
3029 || strcmp (name, ".lit8") == 0
3030 || strcmp (name, ".lit4") == 0)
3032 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3033 hdr->sh_type = SHT_PROGBITS;
3035 else if (strcmp (name, ".sbss") == 0)
3037 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3038 hdr->sh_type = SHT_NOBITS;
3040 else if (strcmp (name, MIPS_ELF_SRDATA_SECTION_NAME (abfd)) == 0)
3042 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3043 hdr->sh_type = SHT_PROGBITS;
3045 else if (strcmp (name, ".compact_rel") == 0)
3047 hdr->sh_flags = 0;
3048 hdr->sh_type = SHT_PROGBITS;
3050 else if (strcmp (name, ".rtproc") == 0)
3052 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3054 unsigned int adjust;
3056 adjust = hdr->sh_size % hdr->sh_addralign;
3057 if (adjust != 0)
3058 hdr->sh_size += hdr->sh_addralign - adjust;
3063 return true;
3067 /* MIPS ELF uses two common sections. One is the usual one, and the
3068 other is for small objects. All the small objects are kept
3069 together, and then referenced via the gp pointer, which yields
3070 faster assembler code. This is what we use for the small common
3071 section. This approach is copied from ecoff.c. */
3072 static asection mips_elf_scom_section;
3073 static asymbol mips_elf_scom_symbol;
3074 static asymbol *mips_elf_scom_symbol_ptr;
3076 /* MIPS ELF also uses an acommon section, which represents an
3077 allocated common symbol which may be overridden by a
3078 definition in a shared library. */
3079 static asection mips_elf_acom_section;
3080 static asymbol mips_elf_acom_symbol;
3081 static asymbol *mips_elf_acom_symbol_ptr;
3083 /* The Irix 5 support uses two virtual sections, which represent
3084 text/data symbols defined in dynamic objects. */
3085 static asection mips_elf_text_section;
3086 static asection *mips_elf_text_section_ptr;
3087 static asymbol mips_elf_text_symbol;
3088 static asymbol *mips_elf_text_symbol_ptr;
3090 static asection mips_elf_data_section;
3091 static asection *mips_elf_data_section_ptr;
3092 static asymbol mips_elf_data_symbol;
3093 static asymbol *mips_elf_data_symbol_ptr;
3095 /* Handle the special MIPS section numbers that a symbol may use.
3096 This is used for both the 32-bit and the 64-bit ABI. */
3098 void
3099 _bfd_mips_elf_symbol_processing (abfd, asym)
3100 bfd *abfd;
3101 asymbol *asym;
3103 elf_symbol_type *elfsym;
3105 elfsym = (elf_symbol_type *) asym;
3106 switch (elfsym->internal_elf_sym.st_shndx)
3108 case SHN_MIPS_ACOMMON:
3109 /* This section is used in a dynamically linked executable file.
3110 It is an allocated common section. The dynamic linker can
3111 either resolve these symbols to something in a shared
3112 library, or it can just leave them here. For our purposes,
3113 we can consider these symbols to be in a new section. */
3114 if (mips_elf_acom_section.name == NULL)
3116 /* Initialize the acommon section. */
3117 mips_elf_acom_section.name = ".acommon";
3118 mips_elf_acom_section.flags = SEC_ALLOC;
3119 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3120 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3121 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3122 mips_elf_acom_symbol.name = ".acommon";
3123 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3124 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3125 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3127 asym->section = &mips_elf_acom_section;
3128 break;
3130 case SHN_COMMON:
3131 /* Common symbols less than the GP size are automatically
3132 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3133 if (asym->value > elf_gp_size (abfd)
3134 || IRIX_COMPAT (abfd) == ict_irix6)
3135 break;
3136 /* Fall through. */
3137 case SHN_MIPS_SCOMMON:
3138 if (mips_elf_scom_section.name == NULL)
3140 /* Initialize the small common section. */
3141 mips_elf_scom_section.name = ".scommon";
3142 mips_elf_scom_section.flags = SEC_IS_COMMON;
3143 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3144 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3145 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3146 mips_elf_scom_symbol.name = ".scommon";
3147 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3148 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3149 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3151 asym->section = &mips_elf_scom_section;
3152 asym->value = elfsym->internal_elf_sym.st_size;
3153 break;
3155 case SHN_MIPS_SUNDEFINED:
3156 asym->section = bfd_und_section_ptr;
3157 break;
3159 #if 0 /* for SGI_COMPAT */
3160 case SHN_MIPS_TEXT:
3161 asym->section = mips_elf_text_section_ptr;
3162 break;
3164 case SHN_MIPS_DATA:
3165 asym->section = mips_elf_data_section_ptr;
3166 break;
3167 #endif
3171 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
3172 segments. */
3175 _bfd_mips_elf_additional_program_headers (abfd)
3176 bfd *abfd;
3178 asection *s;
3179 int ret = 0;
3181 if (!SGI_COMPAT (abfd))
3182 return 0;
3184 /* See if we need a PT_MIPS_REGINFO segment. */
3185 s = bfd_get_section_by_name (abfd, ".reginfo");
3186 if (s && (s->flags & SEC_LOAD))
3187 ++ret;
3189 /* See if we need a PT_MIPS_OPTIONS segment. */
3190 if (IRIX_COMPAT (abfd) == ict_irix6
3191 && bfd_get_section_by_name (abfd,
3192 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
3193 ++ret;
3195 /* See if we need a PT_MIPS_RTPROC segment. */
3196 if (IRIX_COMPAT (abfd) == ict_irix5
3197 && bfd_get_section_by_name (abfd, ".dynamic")
3198 && bfd_get_section_by_name (abfd, ".mdebug"))
3199 ++ret;
3201 return ret;
3204 /* Modify the segment map for an Irix 5 executable. */
3206 boolean
3207 _bfd_mips_elf_modify_segment_map (abfd)
3208 bfd *abfd;
3210 asection *s;
3211 struct elf_segment_map *m, **pm;
3213 if (! SGI_COMPAT (abfd))
3214 return true;
3216 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
3217 segment. */
3218 s = bfd_get_section_by_name (abfd, ".reginfo");
3219 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3221 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3222 if (m->p_type == PT_MIPS_REGINFO)
3223 break;
3224 if (m == NULL)
3226 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3227 if (m == NULL)
3228 return false;
3230 m->p_type = PT_MIPS_REGINFO;
3231 m->count = 1;
3232 m->sections[0] = s;
3234 /* We want to put it after the PHDR and INTERP segments. */
3235 pm = &elf_tdata (abfd)->segment_map;
3236 while (*pm != NULL
3237 && ((*pm)->p_type == PT_PHDR
3238 || (*pm)->p_type == PT_INTERP))
3239 pm = &(*pm)->next;
3241 m->next = *pm;
3242 *pm = m;
3246 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
3247 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
3248 PT_OPTIONS segement immediately following the program header
3249 table. */
3250 if (IRIX_COMPAT (abfd) == ict_irix6)
3252 asection *s;
3254 for (s = abfd->sections; s; s = s->next)
3255 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
3256 break;
3258 if (s)
3260 struct elf_segment_map *options_segment;
3262 /* Usually, there's a program header table. But, sometimes
3263 there's not (like when running the `ld' testsuite). So,
3264 if there's no program header table, we just put the
3265 options segement at the end. */
3266 for (pm = &elf_tdata (abfd)->segment_map;
3267 *pm != NULL;
3268 pm = &(*pm)->next)
3269 if ((*pm)->p_type == PT_PHDR)
3270 break;
3272 options_segment = bfd_zalloc (abfd,
3273 sizeof (struct elf_segment_map));
3274 options_segment->next = *pm;
3275 options_segment->p_type = PT_MIPS_OPTIONS;
3276 options_segment->p_flags = PF_R;
3277 options_segment->p_flags_valid = true;
3278 options_segment->count = 1;
3279 options_segment->sections[0] = s;
3280 *pm = options_segment;
3283 else
3285 /* If there are .dynamic and .mdebug sections, we make a room
3286 for the RTPROC header. FIXME: Rewrite without section names. */
3287 if (bfd_get_section_by_name (abfd, ".interp") == NULL
3288 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
3289 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
3291 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3292 if (m->p_type == PT_MIPS_RTPROC)
3293 break;
3294 if (m == NULL)
3296 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3297 if (m == NULL)
3298 return false;
3300 m->p_type = PT_MIPS_RTPROC;
3302 s = bfd_get_section_by_name (abfd, ".rtproc");
3303 if (s == NULL)
3305 m->count = 0;
3306 m->p_flags = 0;
3307 m->p_flags_valid = 1;
3309 else
3311 m->count = 1;
3312 m->sections[0] = s;
3315 /* We want to put it after the DYNAMIC segment. */
3316 pm = &elf_tdata (abfd)->segment_map;
3317 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3318 pm = &(*pm)->next;
3319 if (*pm != NULL)
3320 pm = &(*pm)->next;
3322 m->next = *pm;
3323 *pm = m;
3327 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic,
3328 .dynstr, .dynsym, and .hash sections, and everything in
3329 between. */
3330 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3331 if ((*pm)->p_type == PT_DYNAMIC)
3332 break;
3333 m = *pm;
3334 if (m != NULL
3335 && m->count == 1
3336 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3338 static const char *sec_names[] =
3339 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3340 bfd_vma low, high;
3341 unsigned int i, c;
3342 struct elf_segment_map *n;
3344 low = 0xffffffff;
3345 high = 0;
3346 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3348 s = bfd_get_section_by_name (abfd, sec_names[i]);
3349 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3351 bfd_size_type sz;
3353 if (low > s->vma)
3354 low = s->vma;
3355 sz = s->_cooked_size;
3356 if (sz == 0)
3357 sz = s->_raw_size;
3358 if (high < s->vma + sz)
3359 high = s->vma + sz;
3363 c = 0;
3364 for (s = abfd->sections; s != NULL; s = s->next)
3365 if ((s->flags & SEC_LOAD) != 0
3366 && s->vma >= low
3367 && ((s->vma
3368 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3369 <= high))
3370 ++c;
3372 n = ((struct elf_segment_map *)
3373 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3374 if (n == NULL)
3375 return false;
3376 *n = *m;
3377 n->count = c;
3379 i = 0;
3380 for (s = abfd->sections; s != NULL; s = s->next)
3382 if ((s->flags & SEC_LOAD) != 0
3383 && s->vma >= low
3384 && ((s->vma
3385 + (s->_cooked_size != 0 ?
3386 s->_cooked_size : s->_raw_size))
3387 <= high))
3389 n->sections[i] = s;
3390 ++i;
3394 *pm = n;
3398 return true;
3401 /* The structure of the runtime procedure descriptor created by the
3402 loader for use by the static exception system. */
3404 typedef struct runtime_pdr {
3405 bfd_vma adr; /* memory address of start of procedure */
3406 long regmask; /* save register mask */
3407 long regoffset; /* save register offset */
3408 long fregmask; /* save floating point register mask */
3409 long fregoffset; /* save floating point register offset */
3410 long frameoffset; /* frame size */
3411 short framereg; /* frame pointer register */
3412 short pcreg; /* offset or reg of return pc */
3413 long irpss; /* index into the runtime string table */
3414 long reserved;
3415 struct exception_info *exception_info;/* pointer to exception array */
3416 } RPDR, *pRPDR;
3417 #define cbRPDR sizeof(RPDR)
3418 #define rpdNil ((pRPDR) 0)
3420 /* Swap RPDR (runtime procedure table entry) for output. */
3422 static void ecoff_swap_rpdr_out
3423 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3425 static void
3426 ecoff_swap_rpdr_out (abfd, in, ex)
3427 bfd *abfd;
3428 const RPDR *in;
3429 struct rpdr_ext *ex;
3431 /* ecoff_put_off was defined in ecoffswap.h. */
3432 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3433 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3434 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3435 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3436 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3437 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3439 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3440 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3442 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3443 #if 0 /* FIXME */
3444 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3445 #endif
3448 /* Read ECOFF debugging information from a .mdebug section into a
3449 ecoff_debug_info structure. */
3451 boolean
3452 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3453 bfd *abfd;
3454 asection *section;
3455 struct ecoff_debug_info *debug;
3457 HDRR *symhdr;
3458 const struct ecoff_debug_swap *swap;
3459 char *ext_hdr = NULL;
3461 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3462 memset (debug, 0, sizeof(*debug));
3464 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3465 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3466 goto error_return;
3468 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3469 swap->external_hdr_size)
3470 == false)
3471 goto error_return;
3473 symhdr = &debug->symbolic_header;
3474 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3476 /* The symbolic header contains absolute file offsets and sizes to
3477 read. */
3478 #define READ(ptr, offset, count, size, type) \
3479 if (symhdr->count == 0) \
3480 debug->ptr = NULL; \
3481 else \
3483 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3484 if (debug->ptr == NULL) \
3485 goto error_return; \
3486 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3487 || (bfd_read (debug->ptr, size, symhdr->count, \
3488 abfd) != size * symhdr->count)) \
3489 goto error_return; \
3492 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3493 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3494 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3495 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3496 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3497 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3498 union aux_ext *);
3499 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3500 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3501 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3502 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3503 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3504 #undef READ
3506 debug->fdr = NULL;
3507 debug->adjust = NULL;
3509 return true;
3511 error_return:
3512 if (ext_hdr != NULL)
3513 free (ext_hdr);
3514 if (debug->line != NULL)
3515 free (debug->line);
3516 if (debug->external_dnr != NULL)
3517 free (debug->external_dnr);
3518 if (debug->external_pdr != NULL)
3519 free (debug->external_pdr);
3520 if (debug->external_sym != NULL)
3521 free (debug->external_sym);
3522 if (debug->external_opt != NULL)
3523 free (debug->external_opt);
3524 if (debug->external_aux != NULL)
3525 free (debug->external_aux);
3526 if (debug->ss != NULL)
3527 free (debug->ss);
3528 if (debug->ssext != NULL)
3529 free (debug->ssext);
3530 if (debug->external_fdr != NULL)
3531 free (debug->external_fdr);
3532 if (debug->external_rfd != NULL)
3533 free (debug->external_rfd);
3534 if (debug->external_ext != NULL)
3535 free (debug->external_ext);
3536 return false;
3539 /* MIPS ELF local labels start with '$', not 'L'. */
3541 /*ARGSUSED*/
3542 static boolean
3543 mips_elf_is_local_label_name (abfd, name)
3544 bfd *abfd;
3545 const char *name;
3547 if (name[0] == '$')
3548 return true;
3550 /* On Irix 6, the labels go back to starting with '.', so we accept
3551 the generic ELF local label syntax as well. */
3552 return _bfd_elf_is_local_label_name (abfd, name);
3555 /* MIPS ELF uses a special find_nearest_line routine in order the
3556 handle the ECOFF debugging information. */
3558 struct mips_elf_find_line
3560 struct ecoff_debug_info d;
3561 struct ecoff_find_line i;
3564 boolean
3565 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3566 functionname_ptr, line_ptr)
3567 bfd *abfd;
3568 asection *section;
3569 asymbol **symbols;
3570 bfd_vma offset;
3571 const char **filename_ptr;
3572 const char **functionname_ptr;
3573 unsigned int *line_ptr;
3575 asection *msec;
3577 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3578 filename_ptr, functionname_ptr,
3579 line_ptr))
3580 return true;
3582 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3583 filename_ptr, functionname_ptr,
3584 line_ptr,
3585 ABI_64_P (abfd) ? 8 : 0))
3586 return true;
3588 msec = bfd_get_section_by_name (abfd, ".mdebug");
3589 if (msec != NULL)
3591 flagword origflags;
3592 struct mips_elf_find_line *fi;
3593 const struct ecoff_debug_swap * const swap =
3594 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3596 /* If we are called during a link, mips_elf_final_link may have
3597 cleared the SEC_HAS_CONTENTS field. We force it back on here
3598 if appropriate (which it normally will be). */
3599 origflags = msec->flags;
3600 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3601 msec->flags |= SEC_HAS_CONTENTS;
3603 fi = elf_tdata (abfd)->find_line_info;
3604 if (fi == NULL)
3606 bfd_size_type external_fdr_size;
3607 char *fraw_src;
3608 char *fraw_end;
3609 struct fdr *fdr_ptr;
3611 fi = ((struct mips_elf_find_line *)
3612 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3613 if (fi == NULL)
3615 msec->flags = origflags;
3616 return false;
3619 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3621 msec->flags = origflags;
3622 return false;
3625 /* Swap in the FDR information. */
3626 fi->d.fdr = ((struct fdr *)
3627 bfd_alloc (abfd,
3628 (fi->d.symbolic_header.ifdMax *
3629 sizeof (struct fdr))));
3630 if (fi->d.fdr == NULL)
3632 msec->flags = origflags;
3633 return false;
3635 external_fdr_size = swap->external_fdr_size;
3636 fdr_ptr = fi->d.fdr;
3637 fraw_src = (char *) fi->d.external_fdr;
3638 fraw_end = (fraw_src
3639 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3640 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3641 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3643 elf_tdata (abfd)->find_line_info = fi;
3645 /* Note that we don't bother to ever free this information.
3646 find_nearest_line is either called all the time, as in
3647 objdump -l, so the information should be saved, or it is
3648 rarely called, as in ld error messages, so the memory
3649 wasted is unimportant. Still, it would probably be a
3650 good idea for free_cached_info to throw it away. */
3653 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3654 &fi->i, filename_ptr, functionname_ptr,
3655 line_ptr))
3657 msec->flags = origflags;
3658 return true;
3661 msec->flags = origflags;
3664 /* Fall back on the generic ELF find_nearest_line routine. */
3666 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3667 filename_ptr, functionname_ptr,
3668 line_ptr);
3671 /* The mips16 compiler uses a couple of special sections to handle
3672 floating point arguments.
3674 Section names that look like .mips16.fn.FNNAME contain stubs that
3675 copy floating point arguments from the fp regs to the gp regs and
3676 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3677 call should be redirected to the stub instead. If no 32 bit
3678 function calls FNNAME, the stub should be discarded. We need to
3679 consider any reference to the function, not just a call, because
3680 if the address of the function is taken we will need the stub,
3681 since the address might be passed to a 32 bit function.
3683 Section names that look like .mips16.call.FNNAME contain stubs
3684 that copy floating point arguments from the gp regs to the fp
3685 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3686 then any 16 bit function that calls FNNAME should be redirected
3687 to the stub instead. If FNNAME is not a 32 bit function, the
3688 stub should be discarded.
3690 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3691 which call FNNAME and then copy the return value from the fp regs
3692 to the gp regs. These stubs store the return value in $18 while
3693 calling FNNAME; any function which might call one of these stubs
3694 must arrange to save $18 around the call. (This case is not
3695 needed for 32 bit functions that call 16 bit functions, because
3696 16 bit functions always return floating point values in both
3697 $f0/$f1 and $2/$3.)
3699 Note that in all cases FNNAME might be defined statically.
3700 Therefore, FNNAME is not used literally. Instead, the relocation
3701 information will indicate which symbol the section is for.
3703 We record any stubs that we find in the symbol table. */
3705 #define FN_STUB ".mips16.fn."
3706 #define CALL_STUB ".mips16.call."
3707 #define CALL_FP_STUB ".mips16.call.fp."
3709 /* MIPS ELF linker hash table. */
3711 struct mips_elf_link_hash_table
3713 struct elf_link_hash_table root;
3714 #if 0
3715 /* We no longer use this. */
3716 /* String section indices for the dynamic section symbols. */
3717 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3718 #endif
3719 /* The number of .rtproc entries. */
3720 bfd_size_type procedure_count;
3721 /* The size of the .compact_rel section (if SGI_COMPAT). */
3722 bfd_size_type compact_rel_size;
3723 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3724 entry is set to the address of __rld_obj_head as in Irix 5. */
3725 boolean use_rld_obj_head;
3726 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3727 bfd_vma rld_value;
3728 /* This is set if we see any mips16 stub sections. */
3729 boolean mips16_stubs_seen;
3732 /* Look up an entry in a MIPS ELF linker hash table. */
3734 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3735 ((struct mips_elf_link_hash_entry *) \
3736 elf_link_hash_lookup (&(table)->root, (string), (create), \
3737 (copy), (follow)))
3739 /* Traverse a MIPS ELF linker hash table. */
3741 #define mips_elf_link_hash_traverse(table, func, info) \
3742 (elf_link_hash_traverse \
3743 (&(table)->root, \
3744 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3745 (info)))
3747 /* Get the MIPS ELF linker hash table from a link_info structure. */
3749 #define mips_elf_hash_table(p) \
3750 ((struct mips_elf_link_hash_table *) ((p)->hash))
3752 static boolean mips_elf_output_extsym
3753 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3755 /* Create an entry in a MIPS ELF linker hash table. */
3757 static struct bfd_hash_entry *
3758 mips_elf_link_hash_newfunc (entry, table, string)
3759 struct bfd_hash_entry *entry;
3760 struct bfd_hash_table *table;
3761 const char *string;
3763 struct mips_elf_link_hash_entry *ret =
3764 (struct mips_elf_link_hash_entry *) entry;
3766 /* Allocate the structure if it has not already been allocated by a
3767 subclass. */
3768 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3769 ret = ((struct mips_elf_link_hash_entry *)
3770 bfd_hash_allocate (table,
3771 sizeof (struct mips_elf_link_hash_entry)));
3772 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3773 return (struct bfd_hash_entry *) ret;
3775 /* Call the allocation method of the superclass. */
3776 ret = ((struct mips_elf_link_hash_entry *)
3777 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3778 table, string));
3779 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3781 /* Set local fields. */
3782 memset (&ret->esym, 0, sizeof (EXTR));
3783 /* We use -2 as a marker to indicate that the information has
3784 not been set. -1 means there is no associated ifd. */
3785 ret->esym.ifd = -2;
3786 ret->possibly_dynamic_relocs = 0;
3787 ret->min_dyn_reloc_index = 0;
3788 ret->fn_stub = NULL;
3789 ret->need_fn_stub = false;
3790 ret->call_stub = NULL;
3791 ret->call_fp_stub = NULL;
3794 return (struct bfd_hash_entry *) ret;
3797 /* Create a MIPS ELF linker hash table. */
3799 struct bfd_link_hash_table *
3800 _bfd_mips_elf_link_hash_table_create (abfd)
3801 bfd *abfd;
3803 struct mips_elf_link_hash_table *ret;
3805 ret = ((struct mips_elf_link_hash_table *)
3806 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3807 if (ret == (struct mips_elf_link_hash_table *) NULL)
3808 return NULL;
3810 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3811 mips_elf_link_hash_newfunc))
3813 bfd_release (abfd, ret);
3814 return NULL;
3817 #if 0
3818 /* We no longer use this. */
3819 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3820 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3821 #endif
3822 ret->procedure_count = 0;
3823 ret->compact_rel_size = 0;
3824 ret->use_rld_obj_head = false;
3825 ret->rld_value = 0;
3826 ret->mips16_stubs_seen = false;
3828 return &ret->root.root;
3831 /* Hook called by the linker routine which adds symbols from an object
3832 file. We must handle the special MIPS section numbers here. */
3834 /*ARGSUSED*/
3835 boolean
3836 _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3837 bfd *abfd;
3838 struct bfd_link_info *info;
3839 const Elf_Internal_Sym *sym;
3840 const char **namep;
3841 flagword *flagsp ATTRIBUTE_UNUSED;
3842 asection **secp;
3843 bfd_vma *valp;
3845 if (SGI_COMPAT (abfd)
3846 && (abfd->flags & DYNAMIC) != 0
3847 && strcmp (*namep, "_rld_new_interface") == 0)
3849 /* Skip Irix 5 rld entry name. */
3850 *namep = NULL;
3851 return true;
3854 switch (sym->st_shndx)
3856 case SHN_COMMON:
3857 /* Common symbols less than the GP size are automatically
3858 treated as SHN_MIPS_SCOMMON symbols. */
3859 if (sym->st_size > elf_gp_size (abfd)
3860 || IRIX_COMPAT (abfd) == ict_irix6)
3861 break;
3862 /* Fall through. */
3863 case SHN_MIPS_SCOMMON:
3864 *secp = bfd_make_section_old_way (abfd, ".scommon");
3865 (*secp)->flags |= SEC_IS_COMMON;
3866 *valp = sym->st_size;
3867 break;
3869 case SHN_MIPS_TEXT:
3870 /* This section is used in a shared object. */
3871 if (mips_elf_text_section_ptr == NULL)
3873 /* Initialize the section. */
3874 mips_elf_text_section.name = ".text";
3875 mips_elf_text_section.flags = SEC_NO_FLAGS;
3876 mips_elf_text_section.output_section = NULL;
3877 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3878 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3879 mips_elf_text_symbol.name = ".text";
3880 mips_elf_text_symbol.flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3881 mips_elf_text_symbol.section = &mips_elf_text_section;
3882 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3883 mips_elf_text_section_ptr = &mips_elf_text_section;
3885 /* This code used to do *secp = bfd_und_section_ptr if
3886 info->shared. I don't know why, and that doesn't make sense,
3887 so I took it out. */
3888 *secp = mips_elf_text_section_ptr;
3889 break;
3891 case SHN_MIPS_ACOMMON:
3892 /* Fall through. XXX Can we treat this as allocated data? */
3893 case SHN_MIPS_DATA:
3894 /* This section is used in a shared object. */
3895 if (mips_elf_data_section_ptr == NULL)
3897 /* Initialize the section. */
3898 mips_elf_data_section.name = ".data";
3899 mips_elf_data_section.flags = SEC_NO_FLAGS;
3900 mips_elf_data_section.output_section = NULL;
3901 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3902 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3903 mips_elf_data_symbol.name = ".data";
3904 mips_elf_data_symbol.flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3905 mips_elf_data_symbol.section = &mips_elf_data_section;
3906 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3907 mips_elf_data_section_ptr = &mips_elf_data_section;
3909 /* This code used to do *secp = bfd_und_section_ptr if
3910 info->shared. I don't know why, and that doesn't make sense,
3911 so I took it out. */
3912 *secp = mips_elf_data_section_ptr;
3913 break;
3915 case SHN_MIPS_SUNDEFINED:
3916 *secp = bfd_und_section_ptr;
3917 break;
3920 if (SGI_COMPAT (abfd)
3921 && ! info->shared
3922 && info->hash->creator == abfd->xvec
3923 && strcmp (*namep, "__rld_obj_head") == 0)
3925 struct elf_link_hash_entry *h;
3927 /* Mark __rld_obj_head as dynamic. */
3928 h = NULL;
3929 if (! (_bfd_generic_link_add_one_symbol
3930 (info, abfd, *namep, BSF_GLOBAL, *secp,
3931 (bfd_vma) *valp, (const char *) NULL, false,
3932 get_elf_backend_data (abfd)->collect,
3933 (struct bfd_link_hash_entry **) &h)))
3934 return false;
3935 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3936 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3937 h->type = STT_OBJECT;
3939 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3940 return false;
3942 mips_elf_hash_table (info)->use_rld_obj_head = true;
3945 /* If this is a mips16 text symbol, add 1 to the value to make it
3946 odd. This will cause something like .word SYM to come up with
3947 the right value when it is loaded into the PC. */
3948 if (sym->st_other == STO_MIPS16)
3949 ++*valp;
3951 return true;
3954 /* Structure used to pass information to mips_elf_output_extsym. */
3956 struct extsym_info
3958 bfd *abfd;
3959 struct bfd_link_info *info;
3960 struct ecoff_debug_info *debug;
3961 const struct ecoff_debug_swap *swap;
3962 boolean failed;
3965 /* This routine is used to write out ECOFF debugging external symbol
3966 information. It is called via mips_elf_link_hash_traverse. The
3967 ECOFF external symbol information must match the ELF external
3968 symbol information. Unfortunately, at this point we don't know
3969 whether a symbol is required by reloc information, so the two
3970 tables may wind up being different. We must sort out the external
3971 symbol information before we can set the final size of the .mdebug
3972 section, and we must set the size of the .mdebug section before we
3973 can relocate any sections, and we can't know which symbols are
3974 required by relocation until we relocate the sections.
3975 Fortunately, it is relatively unlikely that any symbol will be
3976 stripped but required by a reloc. In particular, it can not happen
3977 when generating a final executable. */
3979 static boolean
3980 mips_elf_output_extsym (h, data)
3981 struct mips_elf_link_hash_entry *h;
3982 PTR data;
3984 struct extsym_info *einfo = (struct extsym_info *) data;
3985 boolean strip;
3986 asection *sec, *output_section;
3988 if (h->root.indx == -2)
3989 strip = false;
3990 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3991 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3992 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3993 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3994 strip = true;
3995 else if (einfo->info->strip == strip_all
3996 || (einfo->info->strip == strip_some
3997 && bfd_hash_lookup (einfo->info->keep_hash,
3998 h->root.root.root.string,
3999 false, false) == NULL))
4000 strip = true;
4001 else
4002 strip = false;
4004 if (strip)
4005 return true;
4007 if (h->esym.ifd == -2)
4009 h->esym.jmptbl = 0;
4010 h->esym.cobol_main = 0;
4011 h->esym.weakext = 0;
4012 h->esym.reserved = 0;
4013 h->esym.ifd = ifdNil;
4014 h->esym.asym.value = 0;
4015 h->esym.asym.st = stGlobal;
4017 if (SGI_COMPAT (einfo->abfd)
4018 && (h->root.root.type == bfd_link_hash_undefined
4019 || h->root.root.type == bfd_link_hash_undefweak))
4021 const char *name;
4023 /* Use undefined class. Also, set class and type for some
4024 special symbols. */
4025 name = h->root.root.root.string;
4026 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
4027 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
4029 h->esym.asym.sc = scData;
4030 h->esym.asym.st = stLabel;
4031 h->esym.asym.value = 0;
4033 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
4035 h->esym.asym.sc = scAbs;
4036 h->esym.asym.st = stLabel;
4037 h->esym.asym.value =
4038 mips_elf_hash_table (einfo->info)->procedure_count;
4040 else if (strcmp (name, "_gp_disp") == 0)
4042 h->esym.asym.sc = scAbs;
4043 h->esym.asym.st = stLabel;
4044 h->esym.asym.value = elf_gp (einfo->abfd);
4046 else
4047 h->esym.asym.sc = scUndefined;
4049 else if (h->root.root.type != bfd_link_hash_defined
4050 && h->root.root.type != bfd_link_hash_defweak)
4051 h->esym.asym.sc = scAbs;
4052 else
4054 const char *name;
4056 sec = h->root.root.u.def.section;
4057 output_section = sec->output_section;
4059 /* When making a shared library and symbol h is the one from
4060 the another shared library, OUTPUT_SECTION may be null. */
4061 if (output_section == NULL)
4062 h->esym.asym.sc = scUndefined;
4063 else
4065 name = bfd_section_name (output_section->owner, output_section);
4067 if (strcmp (name, ".text") == 0)
4068 h->esym.asym.sc = scText;
4069 else if (strcmp (name, ".data") == 0)
4070 h->esym.asym.sc = scData;
4071 else if (strcmp (name, ".sdata") == 0)
4072 h->esym.asym.sc = scSData;
4073 else if (strcmp (name, ".rodata") == 0
4074 || strcmp (name, ".rdata") == 0)
4075 h->esym.asym.sc = scRData;
4076 else if (strcmp (name, ".bss") == 0)
4077 h->esym.asym.sc = scBss;
4078 else if (strcmp (name, ".sbss") == 0)
4079 h->esym.asym.sc = scSBss;
4080 else if (strcmp (name, ".init") == 0)
4081 h->esym.asym.sc = scInit;
4082 else if (strcmp (name, ".fini") == 0)
4083 h->esym.asym.sc = scFini;
4084 else
4085 h->esym.asym.sc = scAbs;
4089 h->esym.asym.reserved = 0;
4090 h->esym.asym.index = indexNil;
4093 if (h->root.root.type == bfd_link_hash_common)
4094 h->esym.asym.value = h->root.root.u.c.size;
4095 else if (h->root.root.type == bfd_link_hash_defined
4096 || h->root.root.type == bfd_link_hash_defweak)
4098 if (h->esym.asym.sc == scCommon)
4099 h->esym.asym.sc = scBss;
4100 else if (h->esym.asym.sc == scSCommon)
4101 h->esym.asym.sc = scSBss;
4103 sec = h->root.root.u.def.section;
4104 output_section = sec->output_section;
4105 if (output_section != NULL)
4106 h->esym.asym.value = (h->root.root.u.def.value
4107 + sec->output_offset
4108 + output_section->vma);
4109 else
4110 h->esym.asym.value = 0;
4112 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4114 /* Set type and value for a symbol with a function stub. */
4115 h->esym.asym.st = stProc;
4116 sec = h->root.root.u.def.section;
4117 if (sec == NULL)
4118 h->esym.asym.value = 0;
4119 else
4121 output_section = sec->output_section;
4122 if (output_section != NULL)
4123 h->esym.asym.value = (h->root.plt.offset
4124 + sec->output_offset
4125 + output_section->vma);
4126 else
4127 h->esym.asym.value = 0;
4129 #if 0 /* FIXME? */
4130 h->esym.ifd = 0;
4131 #endif
4134 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
4135 h->root.root.root.string,
4136 &h->esym))
4138 einfo->failed = true;
4139 return false;
4142 return true;
4145 /* Create a runtime procedure table from the .mdebug section. */
4147 static boolean
4148 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
4149 PTR handle;
4150 bfd *abfd;
4151 struct bfd_link_info *info;
4152 asection *s;
4153 struct ecoff_debug_info *debug;
4155 const struct ecoff_debug_swap *swap;
4156 HDRR *hdr = &debug->symbolic_header;
4157 RPDR *rpdr, *rp;
4158 struct rpdr_ext *erp;
4159 PTR rtproc;
4160 struct pdr_ext *epdr;
4161 struct sym_ext *esym;
4162 char *ss, **sv;
4163 char *str;
4164 unsigned long size, count;
4165 unsigned long sindex;
4166 unsigned long i;
4167 PDR pdr;
4168 SYMR sym;
4169 const char *no_name_func = _("static procedure (no name)");
4171 epdr = NULL;
4172 rpdr = NULL;
4173 esym = NULL;
4174 ss = NULL;
4175 sv = NULL;
4177 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4179 sindex = strlen (no_name_func) + 1;
4180 count = hdr->ipdMax;
4181 if (count > 0)
4183 size = swap->external_pdr_size;
4185 epdr = (struct pdr_ext *) bfd_malloc (size * count);
4186 if (epdr == NULL)
4187 goto error_return;
4189 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
4190 goto error_return;
4192 size = sizeof (RPDR);
4193 rp = rpdr = (RPDR *) bfd_malloc (size * count);
4194 if (rpdr == NULL)
4195 goto error_return;
4197 sv = (char **) bfd_malloc (sizeof (char *) * count);
4198 if (sv == NULL)
4199 goto error_return;
4201 count = hdr->isymMax;
4202 size = swap->external_sym_size;
4203 esym = (struct sym_ext *) bfd_malloc (size * count);
4204 if (esym == NULL)
4205 goto error_return;
4207 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
4208 goto error_return;
4210 count = hdr->issMax;
4211 ss = (char *) bfd_malloc (count);
4212 if (ss == NULL)
4213 goto error_return;
4214 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
4215 goto error_return;
4217 count = hdr->ipdMax;
4218 for (i = 0; i < count; i++, rp++)
4220 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
4221 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
4222 rp->adr = sym.value;
4223 rp->regmask = pdr.regmask;
4224 rp->regoffset = pdr.regoffset;
4225 rp->fregmask = pdr.fregmask;
4226 rp->fregoffset = pdr.fregoffset;
4227 rp->frameoffset = pdr.frameoffset;
4228 rp->framereg = pdr.framereg;
4229 rp->pcreg = pdr.pcreg;
4230 rp->irpss = sindex;
4231 sv[i] = ss + sym.iss;
4232 sindex += strlen (sv[i]) + 1;
4236 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
4237 size = BFD_ALIGN (size, 16);
4238 rtproc = (PTR) bfd_alloc (abfd, size);
4239 if (rtproc == NULL)
4241 mips_elf_hash_table (info)->procedure_count = 0;
4242 goto error_return;
4245 mips_elf_hash_table (info)->procedure_count = count + 2;
4247 erp = (struct rpdr_ext *) rtproc;
4248 memset (erp, 0, sizeof (struct rpdr_ext));
4249 erp++;
4250 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
4251 strcpy (str, no_name_func);
4252 str += strlen (no_name_func) + 1;
4253 for (i = 0; i < count; i++)
4255 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
4256 strcpy (str, sv[i]);
4257 str += strlen (sv[i]) + 1;
4259 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
4261 /* Set the size and contents of .rtproc section. */
4262 s->_raw_size = size;
4263 s->contents = (bfd_byte *) rtproc;
4265 /* Skip this section later on (I don't think this currently
4266 matters, but someday it might). */
4267 s->link_order_head = (struct bfd_link_order *) NULL;
4269 if (epdr != NULL)
4270 free (epdr);
4271 if (rpdr != NULL)
4272 free (rpdr);
4273 if (esym != NULL)
4274 free (esym);
4275 if (ss != NULL)
4276 free (ss);
4277 if (sv != NULL)
4278 free (sv);
4280 return true;
4282 error_return:
4283 if (epdr != NULL)
4284 free (epdr);
4285 if (rpdr != NULL)
4286 free (rpdr);
4287 if (esym != NULL)
4288 free (esym);
4289 if (ss != NULL)
4290 free (ss);
4291 if (sv != NULL)
4292 free (sv);
4293 return false;
4296 /* A comparison routine used to sort .gptab entries. */
4298 static int
4299 gptab_compare (p1, p2)
4300 const PTR p1;
4301 const PTR p2;
4303 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4304 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4306 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4309 /* We need to use a special link routine to handle the .reginfo and
4310 the .mdebug sections. We need to merge all instances of these
4311 sections together, not write them all out sequentially. */
4313 boolean
4314 _bfd_mips_elf_final_link (abfd, info)
4315 bfd *abfd;
4316 struct bfd_link_info *info;
4318 asection **secpp;
4319 asection *o;
4320 struct bfd_link_order *p;
4321 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4322 asection *rtproc_sec;
4323 Elf32_RegInfo reginfo;
4324 struct ecoff_debug_info debug;
4325 const struct ecoff_debug_swap *swap
4326 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4327 HDRR *symhdr = &debug.symbolic_header;
4328 PTR mdebug_handle = NULL;
4330 /* If all the things we linked together were PIC, but we're
4331 producing an executable (rather than a shared object), then the
4332 resulting file is CPIC (i.e., it calls PIC code.) */
4333 if (!info->shared
4334 && !info->relocateable
4335 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
4337 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
4338 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
4341 /* We'd carefully arranged the dynamic symbol indices, and then the
4342 generic size_dynamic_sections renumbered them out from under us.
4343 Rather than trying somehow to prevent the renumbering, just do
4344 the sort again. */
4345 if (elf_hash_table (info)->dynamic_sections_created)
4347 bfd *dynobj;
4348 asection *got;
4349 struct mips_got_info *g;
4351 /* When we resort, we must tell mips_elf_sort_hash_table what
4352 the lowest index it may use is. That's the number of section
4353 symbols we're going to add. The generic ELF linker only
4354 adds these symbols when building a shared object. Note that
4355 we count the sections after (possibly) removing the .options
4356 section above. */
4357 if (!mips_elf_sort_hash_table (info, (info->shared
4358 ? bfd_count_sections (abfd) + 1
4359 : 1)))
4360 return false;
4362 /* Make sure we didn't grow the global .got region. */
4363 dynobj = elf_hash_table (info)->dynobj;
4364 got = bfd_get_section_by_name (dynobj, ".got");
4365 g = (struct mips_got_info *) elf_section_data (got)->tdata;
4367 if (g->global_gotsym != NULL)
4368 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
4369 - g->global_gotsym->dynindx)
4370 <= g->global_gotno);
4373 /* On IRIX5, we omit the .options section. On IRIX6, however, we
4374 include it, even though we don't process it quite right. (Some
4375 entries are supposed to be merged.) Empirically, we seem to be
4376 better off including it then not. */
4377 if (IRIX_COMPAT (abfd) == ict_irix5)
4378 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4380 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4382 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4383 if (p->type == bfd_indirect_link_order)
4384 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4385 (*secpp)->link_order_head = NULL;
4386 *secpp = (*secpp)->next;
4387 --abfd->section_count;
4389 break;
4393 /* Get a value for the GP register. */
4394 if (elf_gp (abfd) == 0)
4396 struct bfd_link_hash_entry *h;
4398 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4399 if (h != (struct bfd_link_hash_entry *) NULL
4400 && h->type == bfd_link_hash_defined)
4401 elf_gp (abfd) = (h->u.def.value
4402 + h->u.def.section->output_section->vma
4403 + h->u.def.section->output_offset);
4404 else if (info->relocateable)
4406 bfd_vma lo;
4408 /* Find the GP-relative section with the lowest offset. */
4409 lo = (bfd_vma) -1;
4410 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4411 if (o->vma < lo
4412 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
4413 lo = o->vma;
4415 /* And calculate GP relative to that. */
4416 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4418 else
4420 /* If the relocate_section function needs to do a reloc
4421 involving the GP value, it should make a reloc_dangerous
4422 callback to warn that GP is not defined. */
4426 /* Go through the sections and collect the .reginfo and .mdebug
4427 information. */
4428 reginfo_sec = NULL;
4429 mdebug_sec = NULL;
4430 gptab_data_sec = NULL;
4431 gptab_bss_sec = NULL;
4432 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4434 if (strcmp (o->name, ".reginfo") == 0)
4436 memset (&reginfo, 0, sizeof reginfo);
4438 /* We have found the .reginfo section in the output file.
4439 Look through all the link_orders comprising it and merge
4440 the information together. */
4441 for (p = o->link_order_head;
4442 p != (struct bfd_link_order *) NULL;
4443 p = p->next)
4445 asection *input_section;
4446 bfd *input_bfd;
4447 Elf32_External_RegInfo ext;
4448 Elf32_RegInfo sub;
4450 if (p->type != bfd_indirect_link_order)
4452 if (p->type == bfd_fill_link_order)
4453 continue;
4454 abort ();
4457 input_section = p->u.indirect.section;
4458 input_bfd = input_section->owner;
4460 /* The linker emulation code has probably clobbered the
4461 size to be zero bytes. */
4462 if (input_section->_raw_size == 0)
4463 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4465 if (! bfd_get_section_contents (input_bfd, input_section,
4466 (PTR) &ext,
4467 (file_ptr) 0,
4468 sizeof ext))
4469 return false;
4471 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4473 reginfo.ri_gprmask |= sub.ri_gprmask;
4474 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4475 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4476 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4477 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4479 /* ri_gp_value is set by the function
4480 mips_elf32_section_processing when the section is
4481 finally written out. */
4483 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4484 elf_link_input_bfd ignores this section. */
4485 input_section->flags &=~ SEC_HAS_CONTENTS;
4488 /* Size has been set in mips_elf_always_size_sections */
4489 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4491 /* Skip this section later on (I don't think this currently
4492 matters, but someday it might). */
4493 o->link_order_head = (struct bfd_link_order *) NULL;
4495 reginfo_sec = o;
4498 if (strcmp (o->name, ".mdebug") == 0)
4500 struct extsym_info einfo;
4502 /* We have found the .mdebug section in the output file.
4503 Look through all the link_orders comprising it and merge
4504 the information together. */
4505 symhdr->magic = swap->sym_magic;
4506 /* FIXME: What should the version stamp be? */
4507 symhdr->vstamp = 0;
4508 symhdr->ilineMax = 0;
4509 symhdr->cbLine = 0;
4510 symhdr->idnMax = 0;
4511 symhdr->ipdMax = 0;
4512 symhdr->isymMax = 0;
4513 symhdr->ioptMax = 0;
4514 symhdr->iauxMax = 0;
4515 symhdr->issMax = 0;
4516 symhdr->issExtMax = 0;
4517 symhdr->ifdMax = 0;
4518 symhdr->crfd = 0;
4519 symhdr->iextMax = 0;
4521 /* We accumulate the debugging information itself in the
4522 debug_info structure. */
4523 debug.line = NULL;
4524 debug.external_dnr = NULL;
4525 debug.external_pdr = NULL;
4526 debug.external_sym = NULL;
4527 debug.external_opt = NULL;
4528 debug.external_aux = NULL;
4529 debug.ss = NULL;
4530 debug.ssext = debug.ssext_end = NULL;
4531 debug.external_fdr = NULL;
4532 debug.external_rfd = NULL;
4533 debug.external_ext = debug.external_ext_end = NULL;
4535 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4536 if (mdebug_handle == (PTR) NULL)
4537 return false;
4539 if (SGI_COMPAT (abfd))
4541 asection *s;
4542 EXTR esym;
4543 bfd_vma last;
4544 unsigned int i;
4545 static const char * const name[] =
4546 { ".text", ".init", ".fini", ".data",
4547 ".rodata", ".sdata", ".sbss", ".bss" };
4548 static const int sc[] = { scText, scInit, scFini, scData,
4549 scRData, scSData, scSBss, scBss };
4551 esym.jmptbl = 0;
4552 esym.cobol_main = 0;
4553 esym.weakext = 0;
4554 esym.reserved = 0;
4555 esym.ifd = ifdNil;
4556 esym.asym.iss = issNil;
4557 esym.asym.st = stLocal;
4558 esym.asym.reserved = 0;
4559 esym.asym.index = indexNil;
4560 last = 0;
4561 for (i = 0; i < 8; i++)
4563 esym.asym.sc = sc[i];
4564 s = bfd_get_section_by_name (abfd, name[i]);
4565 if (s != NULL)
4567 esym.asym.value = s->vma;
4568 last = s->vma + s->_raw_size;
4570 else
4571 esym.asym.value = last;
4573 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4574 name[i], &esym))
4575 return false;
4579 for (p = o->link_order_head;
4580 p != (struct bfd_link_order *) NULL;
4581 p = p->next)
4583 asection *input_section;
4584 bfd *input_bfd;
4585 const struct ecoff_debug_swap *input_swap;
4586 struct ecoff_debug_info input_debug;
4587 char *eraw_src;
4588 char *eraw_end;
4590 if (p->type != bfd_indirect_link_order)
4592 if (p->type == bfd_fill_link_order)
4593 continue;
4594 abort ();
4597 input_section = p->u.indirect.section;
4598 input_bfd = input_section->owner;
4600 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4601 || (get_elf_backend_data (input_bfd)
4602 ->elf_backend_ecoff_debug_swap) == NULL)
4604 /* I don't know what a non MIPS ELF bfd would be
4605 doing with a .mdebug section, but I don't really
4606 want to deal with it. */
4607 continue;
4610 input_swap = (get_elf_backend_data (input_bfd)
4611 ->elf_backend_ecoff_debug_swap);
4613 BFD_ASSERT (p->size == input_section->_raw_size);
4615 /* The ECOFF linking code expects that we have already
4616 read in the debugging information and set up an
4617 ecoff_debug_info structure, so we do that now. */
4618 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4619 &input_debug))
4620 return false;
4622 if (! (bfd_ecoff_debug_accumulate
4623 (mdebug_handle, abfd, &debug, swap, input_bfd,
4624 &input_debug, input_swap, info)))
4625 return false;
4627 /* Loop through the external symbols. For each one with
4628 interesting information, try to find the symbol in
4629 the linker global hash table and save the information
4630 for the output external symbols. */
4631 eraw_src = input_debug.external_ext;
4632 eraw_end = (eraw_src
4633 + (input_debug.symbolic_header.iextMax
4634 * input_swap->external_ext_size));
4635 for (;
4636 eraw_src < eraw_end;
4637 eraw_src += input_swap->external_ext_size)
4639 EXTR ext;
4640 const char *name;
4641 struct mips_elf_link_hash_entry *h;
4643 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4644 if (ext.asym.sc == scNil
4645 || ext.asym.sc == scUndefined
4646 || ext.asym.sc == scSUndefined)
4647 continue;
4649 name = input_debug.ssext + ext.asym.iss;
4650 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4651 name, false, false, true);
4652 if (h == NULL || h->esym.ifd != -2)
4653 continue;
4655 if (ext.ifd != -1)
4657 BFD_ASSERT (ext.ifd
4658 < input_debug.symbolic_header.ifdMax);
4659 ext.ifd = input_debug.ifdmap[ext.ifd];
4662 h->esym = ext;
4665 /* Free up the information we just read. */
4666 free (input_debug.line);
4667 free (input_debug.external_dnr);
4668 free (input_debug.external_pdr);
4669 free (input_debug.external_sym);
4670 free (input_debug.external_opt);
4671 free (input_debug.external_aux);
4672 free (input_debug.ss);
4673 free (input_debug.ssext);
4674 free (input_debug.external_fdr);
4675 free (input_debug.external_rfd);
4676 free (input_debug.external_ext);
4678 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4679 elf_link_input_bfd ignores this section. */
4680 input_section->flags &=~ SEC_HAS_CONTENTS;
4683 if (SGI_COMPAT (abfd) && info->shared)
4685 /* Create .rtproc section. */
4686 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4687 if (rtproc_sec == NULL)
4689 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4690 | SEC_LINKER_CREATED | SEC_READONLY);
4692 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4693 if (rtproc_sec == NULL
4694 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4695 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4696 return false;
4699 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4700 info, rtproc_sec, &debug))
4701 return false;
4704 /* Build the external symbol information. */
4705 einfo.abfd = abfd;
4706 einfo.info = info;
4707 einfo.debug = &debug;
4708 einfo.swap = swap;
4709 einfo.failed = false;
4710 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4711 mips_elf_output_extsym,
4712 (PTR) &einfo);
4713 if (einfo.failed)
4714 return false;
4716 /* Set the size of the .mdebug section. */
4717 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4719 /* Skip this section later on (I don't think this currently
4720 matters, but someday it might). */
4721 o->link_order_head = (struct bfd_link_order *) NULL;
4723 mdebug_sec = o;
4726 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4728 const char *subname;
4729 unsigned int c;
4730 Elf32_gptab *tab;
4731 Elf32_External_gptab *ext_tab;
4732 unsigned int i;
4734 /* The .gptab.sdata and .gptab.sbss sections hold
4735 information describing how the small data area would
4736 change depending upon the -G switch. These sections
4737 not used in executables files. */
4738 if (! info->relocateable)
4740 asection **secpp;
4742 for (p = o->link_order_head;
4743 p != (struct bfd_link_order *) NULL;
4744 p = p->next)
4746 asection *input_section;
4748 if (p->type != bfd_indirect_link_order)
4750 if (p->type == bfd_fill_link_order)
4751 continue;
4752 abort ();
4755 input_section = p->u.indirect.section;
4757 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4758 elf_link_input_bfd ignores this section. */
4759 input_section->flags &=~ SEC_HAS_CONTENTS;
4762 /* Skip this section later on (I don't think this
4763 currently matters, but someday it might). */
4764 o->link_order_head = (struct bfd_link_order *) NULL;
4766 /* Really remove the section. */
4767 for (secpp = &abfd->sections;
4768 *secpp != o;
4769 secpp = &(*secpp)->next)
4771 *secpp = (*secpp)->next;
4772 --abfd->section_count;
4774 continue;
4777 /* There is one gptab for initialized data, and one for
4778 uninitialized data. */
4779 if (strcmp (o->name, ".gptab.sdata") == 0)
4780 gptab_data_sec = o;
4781 else if (strcmp (o->name, ".gptab.sbss") == 0)
4782 gptab_bss_sec = o;
4783 else
4785 (*_bfd_error_handler)
4786 (_("%s: illegal section name `%s'"),
4787 bfd_get_filename (abfd), o->name);
4788 bfd_set_error (bfd_error_nonrepresentable_section);
4789 return false;
4792 /* The linker script always combines .gptab.data and
4793 .gptab.sdata into .gptab.sdata, and likewise for
4794 .gptab.bss and .gptab.sbss. It is possible that there is
4795 no .sdata or .sbss section in the output file, in which
4796 case we must change the name of the output section. */
4797 subname = o->name + sizeof ".gptab" - 1;
4798 if (bfd_get_section_by_name (abfd, subname) == NULL)
4800 if (o == gptab_data_sec)
4801 o->name = ".gptab.data";
4802 else
4803 o->name = ".gptab.bss";
4804 subname = o->name + sizeof ".gptab" - 1;
4805 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4808 /* Set up the first entry. */
4809 c = 1;
4810 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4811 if (tab == NULL)
4812 return false;
4813 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4814 tab[0].gt_header.gt_unused = 0;
4816 /* Combine the input sections. */
4817 for (p = o->link_order_head;
4818 p != (struct bfd_link_order *) NULL;
4819 p = p->next)
4821 asection *input_section;
4822 bfd *input_bfd;
4823 bfd_size_type size;
4824 unsigned long last;
4825 bfd_size_type gpentry;
4827 if (p->type != bfd_indirect_link_order)
4829 if (p->type == bfd_fill_link_order)
4830 continue;
4831 abort ();
4834 input_section = p->u.indirect.section;
4835 input_bfd = input_section->owner;
4837 /* Combine the gptab entries for this input section one
4838 by one. We know that the input gptab entries are
4839 sorted by ascending -G value. */
4840 size = bfd_section_size (input_bfd, input_section);
4841 last = 0;
4842 for (gpentry = sizeof (Elf32_External_gptab);
4843 gpentry < size;
4844 gpentry += sizeof (Elf32_External_gptab))
4846 Elf32_External_gptab ext_gptab;
4847 Elf32_gptab int_gptab;
4848 unsigned long val;
4849 unsigned long add;
4850 boolean exact;
4851 unsigned int look;
4853 if (! (bfd_get_section_contents
4854 (input_bfd, input_section, (PTR) &ext_gptab,
4855 gpentry, sizeof (Elf32_External_gptab))))
4857 free (tab);
4858 return false;
4861 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4862 &int_gptab);
4863 val = int_gptab.gt_entry.gt_g_value;
4864 add = int_gptab.gt_entry.gt_bytes - last;
4866 exact = false;
4867 for (look = 1; look < c; look++)
4869 if (tab[look].gt_entry.gt_g_value >= val)
4870 tab[look].gt_entry.gt_bytes += add;
4872 if (tab[look].gt_entry.gt_g_value == val)
4873 exact = true;
4876 if (! exact)
4878 Elf32_gptab *new_tab;
4879 unsigned int max;
4881 /* We need a new table entry. */
4882 new_tab = ((Elf32_gptab *)
4883 bfd_realloc ((PTR) tab,
4884 (c + 1) * sizeof (Elf32_gptab)));
4885 if (new_tab == NULL)
4887 free (tab);
4888 return false;
4890 tab = new_tab;
4891 tab[c].gt_entry.gt_g_value = val;
4892 tab[c].gt_entry.gt_bytes = add;
4894 /* Merge in the size for the next smallest -G
4895 value, since that will be implied by this new
4896 value. */
4897 max = 0;
4898 for (look = 1; look < c; look++)
4900 if (tab[look].gt_entry.gt_g_value < val
4901 && (max == 0
4902 || (tab[look].gt_entry.gt_g_value
4903 > tab[max].gt_entry.gt_g_value)))
4904 max = look;
4906 if (max != 0)
4907 tab[c].gt_entry.gt_bytes +=
4908 tab[max].gt_entry.gt_bytes;
4910 ++c;
4913 last = int_gptab.gt_entry.gt_bytes;
4916 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4917 elf_link_input_bfd ignores this section. */
4918 input_section->flags &=~ SEC_HAS_CONTENTS;
4921 /* The table must be sorted by -G value. */
4922 if (c > 2)
4923 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4925 /* Swap out the table. */
4926 ext_tab = ((Elf32_External_gptab *)
4927 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4928 if (ext_tab == NULL)
4930 free (tab);
4931 return false;
4934 for (i = 0; i < c; i++)
4935 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4936 free (tab);
4938 o->_raw_size = c * sizeof (Elf32_External_gptab);
4939 o->contents = (bfd_byte *) ext_tab;
4941 /* Skip this section later on (I don't think this currently
4942 matters, but someday it might). */
4943 o->link_order_head = (struct bfd_link_order *) NULL;
4947 /* Invoke the regular ELF backend linker to do all the work. */
4948 if (ABI_64_P (abfd))
4950 #ifdef BFD64
4951 if (!bfd_elf64_bfd_final_link (abfd, info))
4952 return false;
4953 #else
4954 abort ();
4955 return false;
4956 #endif /* BFD64 */
4958 else if (!bfd_elf32_bfd_final_link (abfd, info))
4959 return false;
4961 /* Now write out the computed sections. */
4963 if (reginfo_sec != (asection *) NULL)
4965 Elf32_External_RegInfo ext;
4967 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4968 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4969 (file_ptr) 0, sizeof ext))
4970 return false;
4973 if (mdebug_sec != (asection *) NULL)
4975 BFD_ASSERT (abfd->output_has_begun);
4976 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4977 swap, info,
4978 mdebug_sec->filepos))
4979 return false;
4981 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4984 if (gptab_data_sec != (asection *) NULL)
4986 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4987 gptab_data_sec->contents,
4988 (file_ptr) 0,
4989 gptab_data_sec->_raw_size))
4990 return false;
4993 if (gptab_bss_sec != (asection *) NULL)
4995 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4996 gptab_bss_sec->contents,
4997 (file_ptr) 0,
4998 gptab_bss_sec->_raw_size))
4999 return false;
5002 if (SGI_COMPAT (abfd))
5004 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
5005 if (rtproc_sec != NULL)
5007 if (! bfd_set_section_contents (abfd, rtproc_sec,
5008 rtproc_sec->contents,
5009 (file_ptr) 0,
5010 rtproc_sec->_raw_size))
5011 return false;
5015 return true;
5018 /* Handle a MIPS ELF HI16 reloc. */
5020 static void
5021 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
5022 bfd *input_bfd;
5023 Elf_Internal_Rela *relhi;
5024 Elf_Internal_Rela *rello;
5025 bfd_byte *contents;
5026 bfd_vma addend;
5028 bfd_vma insn;
5029 bfd_vma addlo;
5031 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5033 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5034 addlo &= 0xffff;
5036 addend += ((insn & 0xffff) << 16) + addlo;
5038 if ((addlo & 0x8000) != 0)
5039 addend -= 0x10000;
5040 if ((addend & 0x8000) != 0)
5041 addend += 0x10000;
5043 bfd_put_32 (input_bfd,
5044 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
5045 contents + relhi->r_offset);
5048 /* Handle a MIPS ELF local GOT16 reloc. */
5050 static boolean
5051 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
5052 contents, addend)
5053 bfd *output_bfd;
5054 bfd *input_bfd;
5055 asection *sgot;
5056 Elf_Internal_Rela *relhi;
5057 Elf_Internal_Rela *rello;
5058 bfd_byte *contents;
5059 bfd_vma addend;
5061 unsigned int assigned_gotno;
5062 unsigned int i;
5063 bfd_vma insn;
5064 bfd_vma addlo;
5065 bfd_vma address;
5066 bfd_vma hipage;
5067 bfd_byte *got_contents;
5068 struct mips_got_info *g;
5070 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
5072 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
5073 addlo &= 0xffff;
5075 addend += ((insn & 0xffff) << 16) + addlo;
5077 if ((addlo & 0x8000) != 0)
5078 addend -= 0x10000;
5079 if ((addend & 0x8000) != 0)
5080 addend += 0x10000;
5082 /* Get a got entry representing requested hipage. */
5083 BFD_ASSERT (elf_section_data (sgot) != NULL);
5084 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5085 BFD_ASSERT (g != NULL);
5087 assigned_gotno = g->assigned_gotno;
5088 got_contents = sgot->contents;
5089 hipage = addend & 0xffff0000;
5091 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
5093 address = bfd_get_32 (input_bfd, got_contents + i * 4);
5094 if (hipage == (address & 0xffff0000))
5095 break;
5098 if (i == assigned_gotno)
5100 if (assigned_gotno >= g->local_gotno)
5102 (*_bfd_error_handler)
5103 (_("more got entries are needed for hipage relocations"));
5104 bfd_set_error (bfd_error_bad_value);
5105 return false;
5108 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
5109 ++g->assigned_gotno;
5112 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
5113 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
5114 contents + relhi->r_offset);
5116 return true;
5119 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
5121 static void
5122 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
5123 bfd *input_bfd;
5124 Elf_Internal_Rela *rel;
5125 bfd_byte *contents;
5126 bfd_vma offset;
5128 bfd_vma insn;
5130 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5131 bfd_put_32 (input_bfd,
5132 (insn & 0xffff0000) | (offset & 0xffff),
5133 contents + rel->r_offset);
5136 /* Returns the GOT section for ABFD. */
5138 static asection *
5139 mips_elf_got_section (abfd)
5140 bfd *abfd;
5142 return bfd_get_section_by_name (abfd, ".got");
5145 /* Returns the GOT information associated with the link indicated by
5146 INFO. If SGOTP is non-NULL, it is filled in with the GOT
5147 section. */
5149 static struct mips_got_info *
5150 mips_elf_got_info (abfd, sgotp)
5151 bfd *abfd;
5152 asection **sgotp;
5154 asection *sgot;
5155 struct mips_got_info *g;
5157 sgot = mips_elf_got_section (abfd);
5158 BFD_ASSERT (sgot != NULL);
5159 BFD_ASSERT (elf_section_data (sgot) != NULL);
5160 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5161 BFD_ASSERT (g != NULL);
5163 if (sgotp)
5164 *sgotp = sgot;
5165 return g;
5168 /* Return whether a relocation is against a local symbol. */
5170 static boolean
5171 mips_elf_local_relocation_p (input_bfd, relocation, local_sections)
5172 bfd *input_bfd;
5173 const Elf_Internal_Rela *relocation;
5174 asection **local_sections;
5176 unsigned long r_symndx;
5177 Elf_Internal_Shdr *symtab_hdr;
5179 r_symndx = ELF32_R_SYM (relocation->r_info);
5180 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5181 if (! elf_bad_symtab (input_bfd))
5182 return r_symndx < symtab_hdr->sh_info;
5183 else
5185 /* The symbol table does not follow the rule that local symbols
5186 must come before globals. */
5187 return local_sections[r_symndx] != NULL;
5191 /* Sign-extend VALUE, which has the indicated number of BITS. */
5193 static bfd_vma
5194 mips_elf_sign_extend (value, bits)
5195 bfd_vma value;
5196 int bits;
5198 if (value & (1 << (bits - 1)))
5199 /* VALUE is negative. */
5200 value |= ((bfd_vma) - 1) << bits;
5202 return value;
5205 /* Return non-zero if the indicated VALUE has overflowed the maximum
5206 range expressable by a signed number with the indicated number of
5207 BITS. */
5209 static boolean
5210 mips_elf_overflow_p (value, bits)
5211 bfd_vma value;
5212 int bits;
5214 bfd_signed_vma svalue = (bfd_signed_vma) value;
5216 if (svalue > (1 << (bits - 1)) - 1)
5217 /* The value is too big. */
5218 return true;
5219 else if (svalue < -(1 << (bits - 1)))
5220 /* The value is too small. */
5221 return true;
5223 /* All is well. */
5224 return false;
5227 /* Calculate the %high function. */
5229 static bfd_vma
5230 mips_elf_high (value)
5231 bfd_vma value;
5233 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5236 /* Calculate the %higher function. */
5238 static bfd_vma
5239 mips_elf_higher (value)
5240 bfd_vma value ATTRIBUTE_UNUSED;
5242 #ifdef BFD64
5243 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5244 #else
5245 abort ();
5246 return (bfd_vma) -1;
5247 #endif
5250 /* Calculate the %highest function. */
5252 static bfd_vma
5253 mips_elf_highest (value)
5254 bfd_vma value ATTRIBUTE_UNUSED;
5256 #ifdef BFD64
5257 return ((value + (bfd_vma) 0x800080008000) > 48) & 0xffff;
5258 #else
5259 abort ();
5260 return (bfd_vma) -1;
5261 #endif
5264 /* Returns the GOT index for the global symbol indicated by H. */
5266 static bfd_vma
5267 mips_elf_global_got_index (abfd, h)
5268 bfd *abfd;
5269 struct elf_link_hash_entry *h;
5271 bfd_vma index;
5272 asection *sgot;
5273 struct mips_got_info *g;
5275 g = mips_elf_got_info (abfd, &sgot);
5277 /* Once we determine the global GOT entry with the lowest dynamic
5278 symbol table index, we must put all dynamic symbols with greater
5279 indices into the GOT. That makes it easy to calculate the GOT
5280 offset. */
5281 BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
5282 index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno)
5283 * MIPS_ELF_GOT_SIZE (abfd));
5284 BFD_ASSERT (index < sgot->_raw_size);
5286 return index;
5289 /* Returns the offset for the entry at the INDEXth position
5290 in the GOT. */
5292 static bfd_vma
5293 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
5294 bfd *dynobj;
5295 bfd *output_bfd;
5296 bfd_vma index;
5298 asection *sgot;
5299 bfd_vma gp;
5301 sgot = mips_elf_got_section (dynobj);
5302 gp = _bfd_get_gp_value (output_bfd);
5303 return (sgot->output_section->vma + sgot->output_offset + index -
5304 gp);
5307 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5308 symbol table index lower than any we've seen to date, record it for
5309 posterity. */
5311 static boolean
5312 mips_elf_record_global_got_symbol (h, info, g)
5313 struct elf_link_hash_entry *h;
5314 struct bfd_link_info *info;
5315 struct mips_got_info *g ATTRIBUTE_UNUSED;
5317 /* A global symbol in the GOT must also be in the dynamic symbol
5318 table. */
5319 if (h->dynindx == -1
5320 && !bfd_elf32_link_record_dynamic_symbol (info, h))
5321 return false;
5323 /* If we've already marked this entry as need GOT space, we don't
5324 need to do it again. */
5325 if (h->got.offset != (bfd_vma) - 1)
5326 return true;
5328 /* By setting this to a value other than -1, we are indicating that
5329 there needs to be a GOT entry for H. */
5330 h->got.offset = 0;
5332 return true;
5335 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
5336 the dynamic symbols. */
5338 struct mips_elf_hash_sort_data
5340 /* The symbol in the global GOT with the lowest dynamic symbol table
5341 index. */
5342 struct elf_link_hash_entry *low;
5343 /* The least dynamic symbol table index corresponding to a symbol
5344 with a GOT entry. */
5345 long min_got_dynindx;
5346 /* The greatest dynamic symbol table index not corresponding to a
5347 symbol without a GOT entry. */
5348 long max_non_got_dynindx;
5351 /* If H needs a GOT entry, assign it the highest available dynamic
5352 index. Otherwise, assign it the lowest available dynamic
5353 index. */
5355 static boolean
5356 mips_elf_sort_hash_table_f (h, data)
5357 struct mips_elf_link_hash_entry *h;
5358 PTR data;
5360 struct mips_elf_hash_sort_data *hsd
5361 = (struct mips_elf_hash_sort_data *) data;
5363 /* Symbols without dynamic symbol table entries aren't interesting
5364 at all. */
5365 if (h->root.dynindx == -1)
5366 return true;
5368 if (h->root.got.offset != 0)
5369 h->root.dynindx = hsd->max_non_got_dynindx++;
5370 else
5372 h->root.dynindx = --hsd->min_got_dynindx;
5373 hsd->low = (struct elf_link_hash_entry *) h;
5376 return true;
5379 /* Sort the dynamic symbol table so that symbols that need GOT entries
5380 appear towards the end. This reduces the amount of GOT space
5381 required. MAX_LOCAL is used to set the number of local symbols
5382 known to be in the dynamic symbol table. During
5383 mips_elf_size_dynamic_sections, this value is 1. Afterward, the
5384 section symbols are added and the count is higher. */
5386 static boolean
5387 mips_elf_sort_hash_table (info, max_local)
5388 struct bfd_link_info *info;
5389 unsigned long max_local;
5391 struct mips_elf_hash_sort_data hsd;
5392 struct mips_got_info *g;
5393 bfd *dynobj;
5395 dynobj = elf_hash_table (info)->dynobj;
5397 hsd.low = NULL;
5398 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
5399 hsd.max_non_got_dynindx = max_local;
5400 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
5401 elf_hash_table (info)),
5402 mips_elf_sort_hash_table_f,
5403 &hsd);
5405 /* There shoud have been enough room in the symbol table to
5406 accomodate both the GOT and non-GOT symbols. */
5407 BFD_ASSERT (hsd.min_got_dynindx == hsd.max_non_got_dynindx);
5409 /* Now we know which dynamic symbol has the lowest dynamic symbol
5410 table index in the GOT. */
5411 g = mips_elf_got_info (dynobj, NULL);
5412 g->global_gotsym = hsd.low;
5414 return true;
5417 /* Create a local GOT entry for VALUE. Return the index of the entry,
5418 or -1 if it could not be created. */
5420 static bfd_vma
5421 mips_elf_create_local_got_entry (abfd, g, sgot, value)
5422 bfd *abfd;
5423 struct mips_got_info *g;
5424 asection *sgot;
5425 bfd_vma value;
5427 if (g->assigned_gotno >= g->local_gotno)
5429 /* We didn't allocate enough space in the GOT. */
5430 (*_bfd_error_handler)
5431 (_("not enough GOT space for local GOT entries"));
5432 bfd_set_error (bfd_error_bad_value);
5433 return (bfd_vma) -1;
5436 MIPS_ELF_PUT_WORD (abfd, value,
5437 (sgot->contents
5438 + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
5439 return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
5442 /* Returns the GOT offset at which the indicated address can be found.
5443 If there is not yet a GOT entry for this value, create one. Returns
5444 -1 if no satisfactory GOT offset can be found. */
5446 static bfd_vma
5447 mips_elf_local_got_index (abfd, info, value)
5448 bfd *abfd;
5449 struct bfd_link_info *info;
5450 bfd_vma value;
5452 asection *sgot;
5453 struct mips_got_info *g;
5454 bfd_byte *entry;
5456 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5458 /* Look to see if we already have an appropriate entry. */
5459 for (entry = (sgot->contents
5460 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5461 entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5462 entry += MIPS_ELF_GOT_SIZE (abfd))
5464 bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
5465 if (address == value)
5466 return entry - sgot->contents;
5469 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
5472 /* Find a GOT entry that is within 32KB of the VALUE. These entries
5473 are supposed to be placed at small offsets in the GOT, i.e.,
5474 within 32KB of GP. Return the index into the GOT for this page,
5475 and store the offset from this entry to the desired address in
5476 OFFSETP, if it is non-NULL. */
5478 static bfd_vma
5479 mips_elf_got_page (abfd, info, value, offsetp)
5480 bfd *abfd;
5481 struct bfd_link_info *info;
5482 bfd_vma value;
5483 bfd_vma *offsetp;
5485 asection *sgot;
5486 struct mips_got_info *g;
5487 bfd_byte *entry;
5488 bfd_byte *last_entry;
5489 bfd_vma index;
5490 bfd_vma address;
5492 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5494 /* Look to see if we aleady have an appropriate entry. */
5495 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5496 for (entry = (sgot->contents
5497 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5498 entry != last_entry;
5499 entry += MIPS_ELF_GOT_SIZE (abfd))
5501 address = MIPS_ELF_GET_WORD (abfd, entry);
5503 if (!mips_elf_overflow_p (value - address, 16))
5505 /* This entry will serve as the page pointer. We can add a
5506 16-bit number to it to get the actual address. */
5507 index = entry - sgot->contents;
5508 break;
5512 /* If we didn't have an appropriate entry, we create one now. */
5513 if (entry == last_entry)
5514 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5516 if (offsetp)
5518 address = MIPS_ELF_GET_WORD (abfd, entry);
5519 *offsetp = value - address;
5522 return index;
5525 /* Find a GOT entry whose higher-order 16 bits are the same as those
5526 for value. Return the index into the GOT for this entry. */
5528 static bfd_vma
5529 mips_elf_got16_entry (abfd, info, value)
5530 bfd *abfd;
5531 struct bfd_link_info *info;
5532 bfd_vma value;
5534 asection *sgot;
5535 struct mips_got_info *g;
5536 bfd_byte *entry;
5537 bfd_byte *last_entry;
5538 bfd_vma index;
5539 bfd_vma address;
5541 /* Although the ABI says that it is "the high-order 16 bits" that we
5542 want, it is really the %high value. The complete value is
5543 calculated with a `addiu' of a LO16 relocation, just as with a
5544 HI16/LO16 pair. */
5545 value = mips_elf_high (value) << 16;
5546 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
5548 /* Look to see if we already have an appropriate entry. */
5549 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
5550 for (entry = (sgot->contents
5551 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
5552 entry != last_entry;
5553 entry += MIPS_ELF_GOT_SIZE (abfd))
5555 address = MIPS_ELF_GET_WORD (abfd, entry);
5556 if ((address & 0xffff0000) == value)
5558 /* This entry has the right high-order 16 bits. */
5559 index = entry - sgot->contents;
5560 break;
5564 /* If we didn't have an appropriate entry, we create one now. */
5565 if (entry == last_entry)
5566 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
5568 return index;
5571 /* Returns the first R_MIPS_LO16 relocation found, beginning with
5572 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5574 static const Elf_Internal_Rela *
5575 mips_elf_next_lo16_relocation (relocation, relend)
5576 const Elf_Internal_Rela *relocation;
5577 const Elf_Internal_Rela *relend;
5579 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
5580 immediately following. However, for the IRIX6 ABI, the next
5581 relocation may be a composed relocation consisting of several
5582 relocations for the same address. In that case, the R_MIPS_LO16
5583 relocation may occur as one of these. We permit a similar
5584 extension in general, as that is useful for GCC. */
5585 while (relocation < relend)
5587 if (ELF32_R_TYPE (relocation->r_info) == R_MIPS_LO16)
5588 return relocation;
5590 ++relocation;
5593 /* We didn't find it. */
5594 bfd_set_error (bfd_error_bad_value);
5595 return NULL;
5598 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5599 is the original relocation, which is now being transformed into a
5600 dyanmic relocation. The ADDENDP is adjusted if necessary; the
5601 caller should store the result in place of the original addend. */
5603 static boolean
5604 mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec,
5605 symbol, addendp, input_section)
5606 bfd *output_bfd;
5607 struct bfd_link_info *info;
5608 const Elf_Internal_Rela *rel;
5609 struct mips_elf_link_hash_entry *h;
5610 asection *sec;
5611 bfd_vma symbol;
5612 bfd_vma *addendp;
5613 asection *input_section;
5615 Elf_Internal_Rel outrel;
5616 boolean skip;
5617 asection *sreloc;
5618 bfd *dynobj;
5619 int r_type;
5621 r_type = ELF32_R_TYPE (rel->r_info);
5622 dynobj = elf_hash_table (info)->dynobj;
5623 sreloc
5624 = bfd_get_section_by_name (dynobj,
5625 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd));
5626 BFD_ASSERT (sreloc != NULL);
5628 skip = false;
5630 /* We begin by assuming that the offset for the dynamic relocation
5631 is the same as for the original relocation. We'll adjust this
5632 later to reflect the correct output offsets. */
5633 if (elf_section_data (input_section)->stab_info == NULL)
5634 outrel.r_offset = rel->r_offset;
5635 else
5637 /* Except that in a stab section things are more complex.
5638 Because we compress stab information, the offset given in the
5639 relocation may not be the one we want; we must let the stabs
5640 machinery tell us the offset. */
5641 outrel.r_offset
5642 = (_bfd_stab_section_offset
5643 (output_bfd, &elf_hash_table (info)->stab_info,
5644 input_section,
5645 &elf_section_data (input_section)->stab_info,
5646 rel->r_offset));
5647 /* If we didn't need the relocation at all, this value will be
5648 -1. */
5649 if (outrel.r_offset == (bfd_vma) -1)
5650 skip = true;
5653 /* If we've decided to skip this relocation, just output an emtpy
5654 record. Note that R_MIPS_NONE == 0, so that this call to memset
5655 is a way of setting R_TYPE to R_MIPS_NONE. */
5656 if (skip)
5657 memset (&outrel, 0, sizeof (outrel));
5658 else
5660 long indx;
5661 bfd_vma section_offset;
5663 /* We must now calculate the dynamic symbol table index to use
5664 in the relocation. */
5665 if (h != NULL
5666 && (! info->symbolic || (h->root.elf_link_hash_flags
5667 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5669 indx = h->root.dynindx;
5670 BFD_ASSERT (indx != -1);
5672 else
5674 if (sec != NULL && bfd_is_abs_section (sec))
5675 indx = 0;
5676 else if (sec == NULL || sec->owner == NULL)
5678 bfd_set_error (bfd_error_bad_value);
5679 return false;
5681 else
5683 indx = elf_section_data (sec->output_section)->dynindx;
5684 if (indx == 0)
5685 abort ();
5688 /* Figure out how far the target of the relocation is from
5689 the beginning of its section. */
5690 section_offset = symbol - sec->output_section->vma;
5691 /* The relocation we're building is section-relative.
5692 Therefore, the original addend must be adjusted by the
5693 section offset. */
5694 *addendp += symbol - sec->output_section->vma;
5695 /* Now, the relocation is just against the section. */
5696 symbol = sec->output_section->vma;
5699 /* If the relocation was previously an absolute relocation, we
5700 must adjust it by the value we give it in the dynamic symbol
5701 table. */
5702 if (r_type != R_MIPS_REL32)
5703 *addendp += symbol;
5705 /* The relocation is always an REL32 relocation because we don't
5706 know where the shared library will wind up at load-time. */
5707 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5709 /* Adjust the output offset of the relocation to reference the
5710 correct location in the output file. */
5711 outrel.r_offset += (input_section->output_section->vma
5712 + input_section->output_offset);
5715 /* Put the relocation back out. We have to use the special
5716 relocation outputter in the 64-bit case since the 64-bit
5717 relocation format is non-standard. */
5718 if (ABI_64_P (output_bfd))
5720 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5721 (output_bfd, &outrel,
5722 (sreloc->contents
5723 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5725 else
5726 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5727 (((Elf32_External_Rel *)
5728 sreloc->contents)
5729 + sreloc->reloc_count));
5731 /* Record the index of the first relocation referencing H. This
5732 information is later emitted in the .msym section. */
5733 if (h != NULL
5734 && (h->min_dyn_reloc_index == 0
5735 || sreloc->reloc_count < h->min_dyn_reloc_index))
5736 h->min_dyn_reloc_index = sreloc->reloc_count;
5738 /* We've now added another relocation. */
5739 ++sreloc->reloc_count;
5741 /* Make sure the output section is writable. The dynamic linker
5742 will be writing to it. */
5743 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5744 |= SHF_WRITE;
5746 /* On IRIX5, make an entry of compact relocation info. */
5747 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
5749 asection* scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5750 bfd_byte *cr;
5752 if (scpt)
5754 Elf32_crinfo cptrel;
5756 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5757 cptrel.vaddr = (rel->r_offset
5758 + input_section->output_section->vma
5759 + input_section->output_offset);
5760 if (r_type == R_MIPS_REL32)
5761 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5762 else
5763 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5764 mips_elf_set_cr_dist2to (cptrel, 0);
5765 cptrel.konst = *addendp;
5767 cr = (scpt->contents
5768 + sizeof (Elf32_External_compact_rel));
5769 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5770 ((Elf32_External_crinfo *) cr
5771 + scpt->reloc_count));
5772 ++scpt->reloc_count;
5776 return true;
5779 /* Calculate the value produced by the RELOCATION (which comes from
5780 the INPUT_BFD). The ADDEND is the addend to use for this
5781 RELOCATION; RELOCATION->R_ADDEND is ignored.
5783 The result of the relocation calculation is stored in VALUEP.
5784 REQUIRE_JALXP indicates whether or not the opcode used with this
5785 relocation must be JALX.
5787 This function returns bfd_reloc_continue if the caller need take no
5788 further action regarding this relocation, bfd_reloc_notsupported if
5789 something goes dramatically wrong, bfd_reloc_overflow if an
5790 overflow occurs, and bfd_reloc_ok to indicate success. */
5792 static bfd_reloc_status_type
5793 mips_elf_calculate_relocation (abfd,
5794 input_bfd,
5795 input_section,
5796 info,
5797 relocation,
5798 addend,
5799 howto,
5800 local_syms,
5801 local_sections,
5802 valuep,
5803 namep,
5804 require_jalxp)
5805 bfd *abfd;
5806 bfd *input_bfd;
5807 asection *input_section;
5808 struct bfd_link_info *info;
5809 const Elf_Internal_Rela *relocation;
5810 bfd_vma addend;
5811 reloc_howto_type *howto;
5812 Elf_Internal_Sym *local_syms;
5813 asection **local_sections;
5814 bfd_vma *valuep;
5815 const char **namep;
5816 boolean *require_jalxp;
5818 /* The eventual value we will return. */
5819 bfd_vma value;
5820 /* The address of the symbol against which the relocation is
5821 occurring. */
5822 bfd_vma symbol = 0;
5823 /* The final GP value to be used for the relocatable, executable, or
5824 shared object file being produced. */
5825 bfd_vma gp = (bfd_vma) - 1;
5826 /* The place (section offset or address) of the storage unit being
5827 relocated. */
5828 bfd_vma p;
5829 /* The value of GP used to create the relocatable object. */
5830 bfd_vma gp0 = (bfd_vma) - 1;
5831 /* The offset into the global offset table at which the address of
5832 the relocation entry symbol, adjusted by the addend, resides
5833 during execution. */
5834 bfd_vma g = (bfd_vma) - 1;
5835 /* The section in which the symbol referenced by the relocation is
5836 located. */
5837 asection *sec = NULL;
5838 struct mips_elf_link_hash_entry* h = NULL;
5839 /* True if the symbol referred to by this relocation is a local
5840 symbol. */
5841 boolean local_p;
5842 /* True if the symbol referred to by this relocation is "_gp_disp". */
5843 boolean gp_disp_p = false;
5844 Elf_Internal_Shdr *symtab_hdr;
5845 size_t extsymoff;
5846 unsigned long r_symndx;
5847 int r_type;
5848 /* True if overflow occurred during the calculation of the
5849 relocation value. */
5850 boolean overflowed_p;
5851 /* True if this relocation refers to a MIPS16 function. */
5852 boolean target_is_16_bit_code_p = false;
5854 /* Parse the relocation. */
5855 r_symndx = ELF32_R_SYM (relocation->r_info);
5856 r_type = ELF32_R_TYPE (relocation->r_info);
5857 p = (input_section->output_section->vma
5858 + input_section->output_offset
5859 + relocation->r_offset);
5861 /* Assume that there will be no overflow. */
5862 overflowed_p = false;
5864 /* Figure out whether or not the symbol is local, and get the offset
5865 used in the array of hash table entries. */
5866 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5867 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5868 local_sections);
5869 if (! elf_bad_symtab (input_bfd))
5870 extsymoff = symtab_hdr->sh_info;
5871 else
5873 /* The symbol table does not follow the rule that local symbols
5874 must come before globals. */
5875 extsymoff = 0;
5878 /* Figure out the value of the symbol. */
5879 if (local_p)
5881 Elf_Internal_Sym *sym;
5883 sym = local_syms + r_symndx;
5884 sec = local_sections[r_symndx];
5886 symbol = sec->output_section->vma + sec->output_offset;
5887 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5888 symbol += sym->st_value;
5890 /* MIPS16 text labels should be treated as odd. */
5891 if (sym->st_other == STO_MIPS16)
5892 ++symbol;
5894 /* Record the name of this symbol, for our caller. */
5895 *namep = bfd_elf_string_from_elf_section (input_bfd,
5896 symtab_hdr->sh_link,
5897 sym->st_name);
5898 if (*namep == '\0')
5899 *namep = bfd_section_name (input_bfd, sec);
5901 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
5903 else
5905 /* For global symbols we look up the symbol in the hash-table. */
5906 h = ((struct mips_elf_link_hash_entry *)
5907 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5908 /* Find the real hash-table entry for this symbol. */
5909 while (h->root.type == bfd_link_hash_indirect
5910 || h->root.type == bfd_link_hash_warning)
5911 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5913 /* Record the name of this symbol, for our caller. */
5914 *namep = h->root.root.root.string;
5916 /* See if this is the special _gp_disp symbol. Note that such a
5917 symbol must always be a global symbol. */
5918 if (strcmp (h->root.root.root.string, "_gp_disp") == 0)
5920 /* Relocations against _gp_disp are permitted only with
5921 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5922 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
5923 return bfd_reloc_notsupported;
5925 gp_disp_p = true;
5927 /* If this symbol is defined, calculate its address. Note that
5928 _gp_disp is a magic symbol, always implicitly defined by the
5929 linker, so it's inappropriate to check to see whether or not
5930 its defined. */
5931 else if ((h->root.root.type == bfd_link_hash_defined
5932 || h->root.root.type == bfd_link_hash_defweak)
5933 && h->root.root.u.def.section)
5935 sec = h->root.root.u.def.section;
5936 if (sec->output_section)
5937 symbol = (h->root.root.u.def.value
5938 + sec->output_section->vma
5939 + sec->output_offset);
5940 else
5941 symbol = h->root.root.u.def.value;
5943 else if (h->root.root.type == bfd_link_hash_undefweak)
5944 /* We allow relocations against undefined weak symbols, giving
5945 it the value zero, so that you can undefined weak functions
5946 and check to see if they exist by looking at their
5947 addresses. */
5948 symbol = 0;
5949 else if (info->shared && !info->symbolic && !info->no_undefined)
5950 symbol = 0;
5951 else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0)
5953 /* If this is a dynamic link, we should have created a
5954 _DYNAMIC_LINK symbol in mips_elf_create_dynamic_sections.
5955 Otherwise, we should define the symbol with a value of 0.
5956 FIXME: It should probably get into the symbol table
5957 somehow as well. */
5958 BFD_ASSERT (! info->shared);
5959 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5960 symbol = 0;
5962 else
5964 (*info->callbacks->undefined_symbol)
5965 (info, h->root.root.root.string, input_bfd,
5966 input_section, relocation->r_offset);
5967 return bfd_reloc_undefined;
5970 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
5973 /* If this is a 32-bit call to a 16-bit function with a stub, we
5974 need to redirect the call to the stub, unless we're already *in*
5975 a stub. */
5976 if (r_type != R_MIPS16_26 && !info->relocateable
5977 && ((h != NULL && h->fn_stub != NULL)
5978 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
5979 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5980 && !mips_elf_stub_section_p (input_bfd, input_section))
5982 /* This is a 32-bit call to a 16-bit function. We should
5983 have already noticed that we were going to need the
5984 stub. */
5985 if (local_p)
5986 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5987 else
5989 BFD_ASSERT (h->need_fn_stub);
5990 sec = h->fn_stub;
5993 symbol = sec->output_section->vma + sec->output_offset;
5995 /* If this is a 16-bit call to a 32-bit function with a stub, we
5996 need to redirect the call to the stub. */
5997 else if (r_type == R_MIPS16_26 && !info->relocateable
5998 && h != NULL
5999 && (h->call_stub != NULL || h->call_fp_stub != NULL)
6000 && !target_is_16_bit_code_p)
6002 /* If both call_stub and call_fp_stub are defined, we can figure
6003 out which one to use by seeing which one appears in the input
6004 file. */
6005 if (h->call_stub != NULL && h->call_fp_stub != NULL)
6007 asection *o;
6009 sec = NULL;
6010 for (o = input_bfd->sections; o != NULL; o = o->next)
6012 if (strncmp (bfd_get_section_name (input_bfd, o),
6013 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6015 sec = h->call_fp_stub;
6016 break;
6019 if (sec == NULL)
6020 sec = h->call_stub;
6022 else if (h->call_stub != NULL)
6023 sec = h->call_stub;
6024 else
6025 sec = h->call_fp_stub;
6027 BFD_ASSERT (sec->_raw_size > 0);
6028 symbol = sec->output_section->vma + sec->output_offset;
6031 /* Calls from 16-bit code to 32-bit code and vice versa require the
6032 special jalx instruction. */
6033 *require_jalxp = (!info->relocateable
6034 && ((r_type == R_MIPS16_26) != target_is_16_bit_code_p));
6036 /* If we haven't already determined the GOT offset, or the GP value,
6037 and we're going to need it, get it now. */
6038 switch (r_type)
6040 case R_MIPS_CALL16:
6041 case R_MIPS_GOT16:
6042 case R_MIPS_GOT_DISP:
6043 case R_MIPS_GOT_HI16:
6044 case R_MIPS_CALL_HI16:
6045 case R_MIPS_GOT_LO16:
6046 case R_MIPS_CALL_LO16:
6047 /* Find the index into the GOT where this value is located. */
6048 if (!local_p)
6050 BFD_ASSERT (addend == 0);
6051 g = mips_elf_global_got_index
6052 (elf_hash_table (info)->dynobj,
6053 (struct elf_link_hash_entry*) h);
6055 else if (r_type == R_MIPS_GOT16)
6056 /* There's no need to create a local GOT entry here; the
6057 calculation for a local GOT16 entry does not involve G. */
6058 break;
6059 else
6061 g = mips_elf_local_got_index (abfd, info, symbol + addend);
6062 if (g == (bfd_vma) -1)
6063 return false;
6066 /* Convert GOT indices to actual offsets. */
6067 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6068 abfd, g);
6069 break;
6071 case R_MIPS_HI16:
6072 case R_MIPS_LO16:
6073 case R_MIPS_GPREL16:
6074 case R_MIPS_GPREL32:
6075 gp0 = _bfd_get_gp_value (input_bfd);
6076 gp = _bfd_get_gp_value (abfd);
6077 break;
6079 default:
6080 break;
6083 /* Figure out what kind of relocation is being performed. */
6084 switch (r_type)
6086 case R_MIPS_NONE:
6087 return bfd_reloc_continue;
6089 case R_MIPS_16:
6090 value = symbol + mips_elf_sign_extend (addend, 16);
6091 overflowed_p = mips_elf_overflow_p (value, 16);
6092 break;
6094 case R_MIPS_32:
6095 case R_MIPS_REL32:
6096 case R_MIPS_64:
6097 if ((info->shared
6098 || (elf_hash_table (info)->dynamic_sections_created
6099 && h != NULL
6100 && ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
6101 == 0)))
6102 && (input_section->flags & SEC_ALLOC) != 0)
6104 /* If we're creating a shared library, or this relocation is
6105 against a symbol in a shared library, then we can't know
6106 where the symbol will end up. So, we create a relocation
6107 record in the output, and leave the job up to the dynamic
6108 linker. */
6109 value = addend;
6110 if (!mips_elf_create_dynamic_relocation (abfd,
6111 info,
6112 relocation,
6114 sec,
6115 symbol,
6116 &value,
6117 input_section))
6118 return false;
6120 else
6122 if (r_type != R_MIPS_REL32)
6123 value = symbol + addend;
6124 else
6125 value = addend;
6127 value &= howto->dst_mask;
6128 break;
6130 case R_MIPS16_26:
6131 /* The calculation for R_MIPS_26 is just the same as for an
6132 R_MIPS_26. It's only the storage of the relocated field into
6133 the output file that's different. That's handled in
6134 mips_elf_perform_relocation. So, we just fall through to the
6135 R_MIPS_26 case here. */
6136 case R_MIPS_26:
6137 if (local_p)
6138 value = (((addend << 2) | (p & 0xf0000000)) + symbol) >> 2;
6139 else
6140 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
6141 value &= howto->dst_mask;
6142 break;
6144 case R_MIPS_HI16:
6145 if (!gp_disp_p)
6147 value = mips_elf_high (addend + symbol);
6148 value &= howto->dst_mask;
6150 else
6152 value = mips_elf_high (addend + gp - p);
6153 overflowed_p = mips_elf_overflow_p (value, 16);
6155 break;
6157 case R_MIPS_LO16:
6158 if (!gp_disp_p)
6159 value = (symbol + addend) & howto->dst_mask;
6160 else
6162 value = addend + gp - p + 4;
6163 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6164 for overflow. But, on, say, Irix 5, relocations against
6165 _gp_disp are normally generated from the .cpload
6166 pseudo-op. It generates code that normally looks like
6167 this:
6169 lui $gp,%hi(_gp_disp)
6170 addiu $gp,$gp,%lo(_gp_disp)
6171 addu $gp,$gp,$t9
6173 Here $t9 holds the address of the function being called,
6174 as required by the MIPS ELF ABI. The R_MIPS_LO16
6175 relocation can easily overflow in this situation, but the
6176 R_MIPS_HI16 relocation will handle the overflow.
6177 Therefore, we consider this a bug in the MIPS ABI, and do
6178 not check for overflow here. */
6180 break;
6182 case R_MIPS_LITERAL:
6183 /* Because we don't merge literal sections, we can handle this
6184 just like R_MIPS_GPREL16. In the long run, we should merge
6185 shared literals, and then we will need to additional work
6186 here. */
6188 /* Fall through. */
6190 case R_MIPS16_GPREL:
6191 /* The R_MIPS16_GPREL performs the same calculation as
6192 R_MIPS_GPREL16, but stores the relocated bits in a different
6193 order. We don't need to do anything special here; the
6194 differences are handled in mips_elf_perform_relocation. */
6195 case R_MIPS_GPREL16:
6196 if (local_p)
6197 value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
6198 else
6199 value = mips_elf_sign_extend (addend, 16) + symbol - gp;
6200 overflowed_p = mips_elf_overflow_p (value, 16);
6201 break;
6203 case R_MIPS_GOT16:
6204 if (local_p)
6206 value = mips_elf_got16_entry (abfd, info, symbol + addend);
6207 if (value == (bfd_vma) -1)
6208 return false;
6209 value
6210 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6211 abfd,
6212 value);
6213 overflowed_p = mips_elf_overflow_p (value, 16);
6214 break;
6217 /* Fall through. */
6219 case R_MIPS_CALL16:
6220 case R_MIPS_GOT_DISP:
6221 value = g;
6222 overflowed_p = mips_elf_overflow_p (value, 16);
6223 break;
6225 case R_MIPS_GPREL32:
6226 value = (addend + symbol + gp0 - gp) & howto->dst_mask;
6227 break;
6229 case R_MIPS_PC16:
6230 value = mips_elf_sign_extend (addend, 16) + symbol - p;
6231 overflowed_p = mips_elf_overflow_p (value, 16);
6232 break;
6234 case R_MIPS_GOT_HI16:
6235 case R_MIPS_CALL_HI16:
6236 /* We're allowed to handle these two relocations identically.
6237 The dynamic linker is allowed to handle the CALL relocations
6238 differently by creating a lazy evaluation stub. */
6239 value = g;
6240 value = mips_elf_high (value);
6241 value &= howto->dst_mask;
6242 break;
6244 case R_MIPS_GOT_LO16:
6245 case R_MIPS_CALL_LO16:
6246 value = g & howto->dst_mask;
6247 break;
6249 case R_MIPS_GOT_PAGE:
6250 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
6251 if (value == (bfd_vma) -1)
6252 return false;
6253 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
6254 abfd,
6255 value);
6256 overflowed_p = mips_elf_overflow_p (value, 16);
6257 break;
6259 case R_MIPS_GOT_OFST:
6260 mips_elf_got_page (abfd, info, symbol + addend, &value);
6261 overflowed_p = mips_elf_overflow_p (value, 16);
6262 break;
6264 case R_MIPS_SUB:
6265 value = symbol - addend;
6266 value &= howto->dst_mask;
6267 break;
6269 case R_MIPS_HIGHER:
6270 value = mips_elf_higher (addend + symbol);
6271 value &= howto->dst_mask;
6272 break;
6274 case R_MIPS_HIGHEST:
6275 value = mips_elf_highest (addend + symbol);
6276 value &= howto->dst_mask;
6277 break;
6279 case R_MIPS_SCN_DISP:
6280 value = symbol + addend - sec->output_offset;
6281 value &= howto->dst_mask;
6282 break;
6284 case R_MIPS_PJUMP:
6285 case R_MIPS_JALR:
6286 /* Both of these may be ignored. R_MIPS_JALR is an optimization
6287 hint; we could improve performance by honoring that hint. */
6288 return bfd_reloc_continue;
6290 case R_MIPS_GNU_VTINHERIT:
6291 case R_MIPS_GNU_VTENTRY:
6292 /* We don't do anything with these at present. */
6293 return bfd_reloc_continue;
6295 default:
6296 /* An unrecognized relocation type. */
6297 return bfd_reloc_notsupported;
6300 /* Store the VALUE for our caller. */
6301 *valuep = value;
6302 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6305 /* Obtain the field relocated by RELOCATION. */
6307 static bfd_vma
6308 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
6309 reloc_howto_type *howto;
6310 const Elf_Internal_Rela *relocation;
6311 bfd *input_bfd;
6312 bfd_byte *contents;
6314 bfd_vma x;
6315 bfd_byte *location = contents + relocation->r_offset;
6317 /* Obtain the bytes. */
6318 x = bfd_get (8 * bfd_get_reloc_size (howto), input_bfd, location);
6320 if ((ELF32_R_TYPE (relocation->r_info) == R_MIPS16_26
6321 || ELF32_R_TYPE (relocation->r_info) == R_MIPS16_GPREL)
6322 && bfd_little_endian (input_bfd))
6323 /* The two 16-bit words will be reversed on a little-endian
6324 system. See mips_elf_perform_relocation for more details. */
6325 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6327 return x;
6330 /* It has been determined that the result of the RELOCATION is the
6331 VALUE. Use HOWTO to place VALUE into the output file at the
6332 appropriate position. The SECTION is the section to which the
6333 relocation applies. If REQUIRE_JALX is true, then the opcode used
6334 for the relocation must be either JAL or JALX, and it is
6335 unconditionally converted to JALX.
6337 Returns false if anything goes wrong. */
6339 static boolean
6340 mips_elf_perform_relocation (info, howto, relocation, value,
6341 input_bfd, input_section,
6342 contents, require_jalx)
6343 struct bfd_link_info *info;
6344 reloc_howto_type *howto;
6345 const Elf_Internal_Rela *relocation;
6346 bfd_vma value;
6347 bfd *input_bfd;
6348 asection *input_section;
6349 bfd_byte *contents;
6350 boolean require_jalx;
6352 bfd_vma x;
6353 bfd_byte *location;
6354 int r_type = ELF32_R_TYPE (relocation->r_info);
6356 /* Figure out where the relocation is occurring. */
6357 location = contents + relocation->r_offset;
6359 /* Obtain the current value. */
6360 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6362 /* Clear the field we are setting. */
6363 x &= ~howto->dst_mask;
6365 /* If this is the R_MIPS16_26 relocation, we must store the
6366 value in a funny way. */
6367 if (r_type == R_MIPS16_26)
6369 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
6370 Most mips16 instructions are 16 bits, but these instructions
6371 are 32 bits.
6373 The format of these instructions is:
6375 +--------------+--------------------------------+
6376 ! JALX ! X! Imm 20:16 ! Imm 25:21 !
6377 +--------------+--------------------------------+
6378 ! Immediate 15:0 !
6379 +-----------------------------------------------+
6381 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
6382 Note that the immediate value in the first word is swapped.
6384 When producing a relocateable object file, R_MIPS16_26 is
6385 handled mostly like R_MIPS_26. In particular, the addend is
6386 stored as a straight 26-bit value in a 32-bit instruction.
6387 (gas makes life simpler for itself by never adjusting a
6388 R_MIPS16_26 reloc to be against a section, so the addend is
6389 always zero). However, the 32 bit instruction is stored as 2
6390 16-bit values, rather than a single 32-bit value. In a
6391 big-endian file, the result is the same; in a little-endian
6392 file, the two 16-bit halves of the 32 bit value are swapped.
6393 This is so that a disassembler can recognize the jal
6394 instruction.
6396 When doing a final link, R_MIPS16_26 is treated as a 32 bit
6397 instruction stored as two 16-bit values. The addend A is the
6398 contents of the targ26 field. The calculation is the same as
6399 R_MIPS_26. When storing the calculated value, reorder the
6400 immediate value as shown above, and don't forget to store the
6401 value as two 16-bit values.
6403 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
6404 defined as
6406 big-endian:
6407 +--------+----------------------+
6408 | | |
6409 | | targ26-16 |
6410 |31 26|25 0|
6411 +--------+----------------------+
6413 little-endian:
6414 +----------+------+-------------+
6415 | | | |
6416 | sub1 | | sub2 |
6417 |0 9|10 15|16 31|
6418 +----------+--------------------+
6419 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
6420 ((sub1 << 16) | sub2)).
6422 When producing a relocateable object file, the calculation is
6423 (((A < 2) | (P & 0xf0000000) + S) >> 2)
6424 When producing a fully linked file, the calculation is
6425 let R = (((A < 2) | (P & 0xf0000000) + S) >> 2)
6426 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */
6428 if (!info->relocateable)
6429 /* Shuffle the bits according to the formula above. */
6430 value = (((value & 0x1f0000) << 5)
6431 | ((value & 0x3e00000) >> 5)
6432 | (value & 0xffff));
6435 else if (r_type == R_MIPS16_GPREL)
6437 /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
6438 mode. A typical instruction will have a format like this:
6440 +--------------+--------------------------------+
6441 ! EXTEND ! Imm 10:5 ! Imm 15:11 !
6442 +--------------+--------------------------------+
6443 ! Major ! rx ! ry ! Imm 4:0 !
6444 +--------------+--------------------------------+
6446 EXTEND is the five bit value 11110. Major is the instruction
6447 opcode.
6449 This is handled exactly like R_MIPS_GPREL16, except that the
6450 addend is retrieved and stored as shown in this diagram; that
6451 is, the Imm fields above replace the V-rel16 field.
6453 All we need to do here is shuffle the bits appropriately. As
6454 above, the two 16-bit halves must be swapped on a
6455 little-endian system. */
6456 value = (((value & 0x7e0) << 16)
6457 | ((value & 0xf800) << 5)
6458 | (value & 0x1f));
6461 /* Set the field. */
6462 x |= (value & howto->dst_mask);
6464 /* If required, turn JAL into JALX. */
6465 if (require_jalx)
6467 boolean ok;
6468 bfd_vma opcode = x >> 26;
6469 bfd_vma jalx_opcode;
6471 /* Check to see if the opcode is already JAL or JALX. */
6472 if (r_type == R_MIPS16_26)
6474 ok = ((opcode == 0x6) || (opcode == 0x7));
6475 jalx_opcode = 0x7;
6477 else
6479 ok = ((opcode == 0x3) || (opcode == 0x1d));
6480 jalx_opcode = 0x1d;
6483 /* If the opcode is not JAL or JALX, there's a problem. */
6484 if (!ok)
6486 (*_bfd_error_handler)
6487 (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
6488 bfd_get_filename (input_bfd),
6489 input_section->name,
6490 (unsigned long) relocation->r_offset);
6491 bfd_set_error (bfd_error_bad_value);
6492 return false;
6495 /* Make this the JALX opcode. */
6496 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6499 /* Swap the high- and low-order 16 bits on little-endian systems
6500 when doing a MIPS16 relocation. */
6501 if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
6502 && bfd_little_endian (input_bfd))
6503 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
6505 /* Put the value into the output. */
6506 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6507 return true;
6510 /* Returns true if SECTION is a MIPS16 stub section. */
6512 static boolean
6513 mips_elf_stub_section_p (abfd, section)
6514 bfd *abfd ATTRIBUTE_UNUSED;
6515 asection *section;
6517 const char *name = bfd_get_section_name (abfd, section);
6519 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
6520 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6521 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
6524 /* Relocate a MIPS ELF section. */
6526 boolean
6527 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6528 contents, relocs, local_syms, local_sections)
6529 bfd *output_bfd;
6530 struct bfd_link_info *info;
6531 bfd *input_bfd;
6532 asection *input_section;
6533 bfd_byte *contents;
6534 Elf_Internal_Rela *relocs;
6535 Elf_Internal_Sym *local_syms;
6536 asection **local_sections;
6538 Elf_Internal_Rela *rel;
6539 const Elf_Internal_Rela *relend;
6540 bfd_vma addend;
6541 bfd_vma last_hi16_addend;
6542 boolean use_saved_addend_p = false;
6543 boolean last_hi16_addend_valid_p = false;
6544 struct elf_backend_data *bed;
6546 bed = get_elf_backend_data (output_bfd);
6547 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
6548 for (rel = relocs; rel < relend; ++rel)
6550 const char *name;
6551 bfd_vma value;
6552 reloc_howto_type *howto;
6553 boolean require_jalx;
6554 /* True if the relocation is a RELA relocation, rather than a
6555 REL relocation. */
6556 boolean rela_relocation_p = true;
6557 int r_type = ELF32_R_TYPE (rel->r_info);
6559 /* Find the relocation howto for this relocation. */
6560 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6561 /* Some 32-bit code uses R_MIPS_64. In particular, people use
6562 64-bit code, but make sure all their addresses are in the
6563 lowermost or uppermost 32-bit section of the 64-bit address
6564 space. Thus, when they use an R_MIPS_64 they mean what is
6565 usually meant by R_MIPS_32, with the exception that the
6566 stored value is sign-extended to 64 bits. */
6567 howto = elf_mips_howto_table + R_MIPS_32;
6568 else
6569 howto = mips_rtype_to_howto (r_type);
6571 if (!use_saved_addend_p)
6573 Elf_Internal_Shdr *rel_hdr;
6575 /* If these relocations were originally of the REL variety,
6576 we must pull the addend out of the field that will be
6577 relocated. Otherwise, we simply use the contents of the
6578 RELA relocation. To determine which flavor or relocation
6579 this is, we depend on the fact that the INPUT_SECTION's
6580 REL_HDR is read before its REL_HDR2. */
6581 rel_hdr = &elf_section_data (input_section)->rel_hdr;
6582 if ((size_t) (rel - relocs)
6583 >= (rel_hdr->sh_size / rel_hdr->sh_entsize
6584 * bed->s->int_rels_per_ext_rel))
6585 rel_hdr = elf_section_data (input_section)->rel_hdr2;
6586 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
6588 /* Note that this is a REL relocation. */
6589 rela_relocation_p = false;
6591 /* Get the addend, which is stored in the input file. */
6592 addend = mips_elf_obtain_contents (howto,
6593 rel,
6594 input_bfd,
6595 contents);
6596 addend &= howto->src_mask;
6598 /* For some kinds of relocations, the ADDEND is a
6599 combination of the addend stored in two different
6600 relocations. */
6601 if (r_type == R_MIPS_HI16
6602 || (r_type == R_MIPS_GOT16
6603 && mips_elf_local_relocation_p (input_bfd, rel,
6604 local_sections)))
6606 bfd_vma l;
6607 const Elf_Internal_Rela *lo16_relocation;
6608 reloc_howto_type *lo16_howto;
6610 /* The combined value is the sum of the HI16 addend,
6611 left-shifted by sixteen bits, and the LO16
6612 addend, sign extended. (Usually, the code does
6613 a `lui' of the HI16 value, and then an `addiu' of
6614 the LO16 value.)
6616 Scan ahead to find a matching R_MIPS_LO16
6617 relocation. */
6618 lo16_relocation
6619 = mips_elf_next_lo16_relocation (rel, relend);
6620 if (lo16_relocation == NULL)
6621 return false;
6623 /* Obtain the addend kept there. */
6624 lo16_howto = mips_rtype_to_howto (R_MIPS_LO16);
6625 l = mips_elf_obtain_contents (lo16_howto,
6626 lo16_relocation,
6627 input_bfd, contents);
6628 l &= lo16_howto->src_mask;
6629 l = mips_elf_sign_extend (l, 16);
6631 /* Save the high-order bit for later. When we
6632 encounter the R_MIPS_LO16 relocation we will need
6633 them again. */
6634 addend <<= 16;
6635 last_hi16_addend = addend;
6636 last_hi16_addend_valid_p = true;
6638 /* Compute the combined addend. */
6639 addend += l;
6641 else if (r_type == R_MIPS_LO16)
6643 /* Used the saved HI16 addend. */
6644 if (!last_hi16_addend_valid_p)
6646 bfd_set_error (bfd_error_bad_value);
6647 return false;
6649 addend |= last_hi16_addend;
6651 else if (r_type == R_MIPS16_GPREL)
6653 /* The addend is scrambled in the object file. See
6654 mips_elf_perform_relocation for details on the
6655 format. */
6656 addend = (((addend & 0x1f0000) >> 5)
6657 | ((addend & 0x7e00000) >> 16)
6658 | (addend & 0x1f));
6661 else
6662 addend = rel->r_addend;
6665 if (info->relocateable)
6667 Elf_Internal_Sym *sym;
6668 unsigned long r_symndx;
6670 /* Since we're just relocating, all we need to do is copy
6671 the relocations back out to the object file, unless
6672 they're against a section symbol, in which case we need
6673 to adjust by the section offset, or unless they're GP
6674 relative in which case we need to adjust by the amount
6675 that we're adjusting GP in this relocateable object. */
6677 if (!mips_elf_local_relocation_p (input_bfd, rel, local_sections))
6678 /* There's nothing to do for non-local relocations. */
6679 continue;
6681 if (r_type == R_MIPS16_GPREL
6682 || r_type == R_MIPS_GPREL16
6683 || r_type == R_MIPS_GPREL32)
6684 addend -= (_bfd_get_gp_value (output_bfd)
6685 - _bfd_get_gp_value (input_bfd));
6686 else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26)
6687 /* The addend is stored without its two least
6688 significant bits (which are always zero.) In a
6689 non-relocateable link, calculate_relocation will do
6690 this shift; here, we must do it ourselves. */
6691 addend <<= 2;
6693 r_symndx = ELF32_R_SYM (rel->r_info);
6694 sym = local_syms + r_symndx;
6695 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6696 /* Adjust the addend appropriately. */
6697 addend += local_sections[r_symndx]->output_offset;
6699 /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
6700 then we only want to write out the high-order 16 bits.
6701 The subsequent R_MIPS_LO16 will handle the low-order bits. */
6702 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16)
6703 addend = mips_elf_high (addend);
6704 /* If the relocation is for an R_MIPS_26 relocation, then
6705 the two low-order bits are not stored in the object file;
6706 they are implicitly zero. */
6707 else if (r_type == R_MIPS_26 || r_type == R_MIPS16_26)
6708 addend >>= 2;
6710 if (rela_relocation_p)
6711 /* If this is a RELA relocation, just update the addend.
6712 We have to cast away constness for REL. */
6713 rel->r_addend = addend;
6714 else
6716 /* Otherwise, we have to write the value back out. Note
6717 that we use the source mask, rather than the
6718 destination mask because the place to which we are
6719 writing will be source of the addend in the final
6720 link. */
6721 addend &= howto->src_mask;
6722 if (!mips_elf_perform_relocation (info, howto, rel, addend,
6723 input_bfd, input_section,
6724 contents, false))
6725 return false;
6728 /* Go on to the next relocation. */
6729 continue;
6732 /* In the N32 and 64-bit ABIs there may be multiple consecutive
6733 relocations for the same offset. In that case we are
6734 supposed to treat the output of each relocation as the addend
6735 for the next. */
6736 if (rel + 1 < relend
6737 && rel->r_offset == rel[1].r_offset
6738 && ELF32_R_TYPE (rel[1].r_info) != R_MIPS_NONE)
6739 use_saved_addend_p = true;
6740 else
6741 use_saved_addend_p = false;
6743 /* Figure out what value we are supposed to relocate. */
6744 switch (mips_elf_calculate_relocation (output_bfd,
6745 input_bfd,
6746 input_section,
6747 info,
6748 rel,
6749 addend,
6750 howto,
6751 local_syms,
6752 local_sections,
6753 &value,
6754 &name,
6755 &require_jalx))
6757 case bfd_reloc_continue:
6758 /* There's nothing to do. */
6759 continue;
6761 case bfd_reloc_undefined:
6762 /* mips_elf_calculate_relocation already called the
6763 undefined_symbol callback. There's no real point in
6764 trying to perform the relocation at this point, so we
6765 just skip ahead to the next relocation. */
6766 continue;
6768 case bfd_reloc_notsupported:
6769 abort ();
6770 break;
6772 case bfd_reloc_overflow:
6773 if (use_saved_addend_p)
6774 /* Ignore overflow until we reach the last relocation for
6775 a given location. */
6777 else
6779 BFD_ASSERT (name != NULL);
6780 if (! ((*info->callbacks->reloc_overflow)
6781 (info, name, howto->name, (bfd_vma) 0,
6782 input_bfd, input_section, rel->r_offset)))
6783 return false;
6785 break;
6787 case bfd_reloc_ok:
6788 break;
6790 default:
6791 abort ();
6792 break;
6795 /* If we've got another relocation for the address, keep going
6796 until we reach the last one. */
6797 if (use_saved_addend_p)
6799 addend = value;
6800 continue;
6803 if (r_type == R_MIPS_64 && !ABI_64_P (output_bfd))
6804 /* See the comment above about using R_MIPS_64 in the 32-bit
6805 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
6806 that calculated the right value. Now, however, we
6807 sign-extend the 32-bit result to 64-bits, and store it as a
6808 64-bit value. We are especially generous here in that we
6809 go to extreme lengths to support this usage on systems with
6810 only a 32-bit VMA. */
6812 #ifdef BFD64
6813 /* Just sign-extend the value, and then fall through to the
6814 normal case, using the R_MIPS_64 howto. That will store
6815 the 64-bit value into a 64-bit area. */
6816 value = mips_elf_sign_extend (value, 64);
6817 howto = elf_mips_howto_table + R_MIPS_64;
6818 #else /* !BFD64 */
6819 /* In the 32-bit VMA case, we must handle sign-extension and
6820 endianness manually. */
6821 bfd_vma sign_bits;
6822 bfd_vma low_bits;
6823 bfd_vma high_bits;
6825 if (value & 0x80000000)
6826 sign_bits = 0xffffffff;
6827 else
6828 sign_bits = 0;
6830 /* If only a 32-bit VMA is available do two separate
6831 stores. */
6832 if (bfd_big_endian (input_bfd))
6834 /* Store the sign-bits (which are most significant)
6835 first. */
6836 low_bits = sign_bits;
6837 high_bits = value;
6839 else
6841 low_bits = value;
6842 high_bits = sign_bits;
6844 bfd_put_32 (input_bfd, low_bits,
6845 contents + rel->r_offset);
6846 bfd_put_32 (input_bfd, high_bits,
6847 contents + rel->r_offset + 4);
6848 continue;
6849 #endif /* !BFD64 */
6852 /* Actually perform the relocation. */
6853 if (!mips_elf_perform_relocation (info, howto, rel, value, input_bfd,
6854 input_section, contents,
6855 require_jalx))
6856 return false;
6859 return true;
6862 /* This hook function is called before the linker writes out a global
6863 symbol. We mark symbols as small common if appropriate. This is
6864 also where we undo the increment of the value for a mips16 symbol. */
6866 /*ARGSIGNORED*/
6867 boolean
6868 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
6869 bfd *abfd ATTRIBUTE_UNUSED;
6870 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6871 const char *name ATTRIBUTE_UNUSED;
6872 Elf_Internal_Sym *sym;
6873 asection *input_sec;
6875 /* If we see a common symbol, which implies a relocatable link, then
6876 if a symbol was small common in an input file, mark it as small
6877 common in the output file. */
6878 if (sym->st_shndx == SHN_COMMON
6879 && strcmp (input_sec->name, ".scommon") == 0)
6880 sym->st_shndx = SHN_MIPS_SCOMMON;
6882 if (sym->st_other == STO_MIPS16
6883 && (sym->st_value & 1) != 0)
6884 --sym->st_value;
6886 return true;
6889 /* Functions for the dynamic linker. */
6891 /* The name of the dynamic interpreter. This is put in the .interp
6892 section. */
6894 #define ELF_DYNAMIC_INTERPRETER(abfd) \
6895 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
6896 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
6897 : "/usr/lib/libc.so.1")
6899 /* Create dynamic sections when linking against a dynamic object. */
6901 boolean
6902 _bfd_mips_elf_create_dynamic_sections (abfd, info)
6903 bfd *abfd;
6904 struct bfd_link_info *info;
6906 struct elf_link_hash_entry *h;
6907 flagword flags;
6908 register asection *s;
6909 const char * const *namep;
6911 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6912 | SEC_LINKER_CREATED | SEC_READONLY);
6914 /* Mips ABI requests the .dynamic section to be read only. */
6915 s = bfd_get_section_by_name (abfd, ".dynamic");
6916 if (s != NULL)
6918 if (! bfd_set_section_flags (abfd, s, flags))
6919 return false;
6922 /* We need to create .got section. */
6923 if (! mips_elf_create_got_section (abfd, info))
6924 return false;
6926 /* Create the .msym section on IRIX6. It is used by the dynamic
6927 linker to speed up dynamic relocations, and to avoid computing
6928 the ELF hash for symbols. */
6929 if (IRIX_COMPAT (abfd) == ict_irix6
6930 && !mips_elf_create_msym_section (abfd))
6931 return false;
6933 /* Create .stub section. */
6934 if (bfd_get_section_by_name (abfd,
6935 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
6937 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
6938 if (s == NULL
6939 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
6940 || ! bfd_set_section_alignment (abfd, s,
6941 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6942 return false;
6945 if (IRIX_COMPAT (abfd) == ict_irix5
6946 && !info->shared
6947 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6949 s = bfd_make_section (abfd, ".rld_map");
6950 if (s == NULL
6951 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
6952 || ! bfd_set_section_alignment (abfd, s,
6953 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6954 return false;
6957 /* On IRIX5, we adjust add some additional symbols and change the
6958 alignments of several sections. There is no ABI documentation
6959 indicating that this is necessary on IRIX6, nor any evidence that
6960 the linker takes such action. */
6961 if (IRIX_COMPAT (abfd) == ict_irix5)
6963 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6965 h = NULL;
6966 if (! (_bfd_generic_link_add_one_symbol
6967 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
6968 (bfd_vma) 0, (const char *) NULL, false,
6969 get_elf_backend_data (abfd)->collect,
6970 (struct bfd_link_hash_entry **) &h)))
6971 return false;
6972 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6973 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6974 h->type = STT_SECTION;
6976 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6977 return false;
6980 /* We need to create a .compact_rel section. */
6981 if (! mips_elf_create_compact_rel_section (abfd, info))
6982 return false;
6984 /* Change aligments of some sections. */
6985 s = bfd_get_section_by_name (abfd, ".hash");
6986 if (s != NULL)
6987 bfd_set_section_alignment (abfd, s, 4);
6988 s = bfd_get_section_by_name (abfd, ".dynsym");
6989 if (s != NULL)
6990 bfd_set_section_alignment (abfd, s, 4);
6991 s = bfd_get_section_by_name (abfd, ".dynstr");
6992 if (s != NULL)
6993 bfd_set_section_alignment (abfd, s, 4);
6994 s = bfd_get_section_by_name (abfd, ".reginfo");
6995 if (s != NULL)
6996 bfd_set_section_alignment (abfd, s, 4);
6997 s = bfd_get_section_by_name (abfd, ".dynamic");
6998 if (s != NULL)
6999 bfd_set_section_alignment (abfd, s, 4);
7002 if (!info->shared)
7004 h = NULL;
7005 if (! (_bfd_generic_link_add_one_symbol
7006 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
7007 (bfd_vma) 0, (const char *) NULL, false,
7008 get_elf_backend_data (abfd)->collect,
7009 (struct bfd_link_hash_entry **) &h)))
7010 return false;
7011 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7012 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7013 h->type = STT_SECTION;
7015 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7016 return false;
7018 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7020 /* __rld_map is a four byte word located in the .data section
7021 and is filled in by the rtld to contain a pointer to
7022 the _r_debug structure. Its symbol value will be set in
7023 mips_elf_finish_dynamic_symbol. */
7024 s = bfd_get_section_by_name (abfd, ".rld_map");
7025 BFD_ASSERT (s != NULL);
7027 h = NULL;
7028 if (! (_bfd_generic_link_add_one_symbol
7029 (info, abfd, "__rld_map", BSF_GLOBAL, s,
7030 (bfd_vma) 0, (const char *) NULL, false,
7031 get_elf_backend_data (abfd)->collect,
7032 (struct bfd_link_hash_entry **) &h)))
7033 return false;
7034 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7035 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7036 h->type = STT_OBJECT;
7038 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
7039 return false;
7043 return true;
7046 /* Create the .compact_rel section. */
7048 static boolean
7049 mips_elf_create_compact_rel_section (abfd, info)
7050 bfd *abfd;
7051 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7053 flagword flags;
7054 register asection *s;
7056 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
7058 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
7059 | SEC_READONLY);
7061 s = bfd_make_section (abfd, ".compact_rel");
7062 if (s == NULL
7063 || ! bfd_set_section_flags (abfd, s, flags)
7064 || ! bfd_set_section_alignment (abfd, s,
7065 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7066 return false;
7068 s->_raw_size = sizeof (Elf32_External_compact_rel);
7071 return true;
7074 /* Create the .got section to hold the global offset table. */
7076 static boolean
7077 mips_elf_create_got_section (abfd, info)
7078 bfd *abfd;
7079 struct bfd_link_info *info;
7081 flagword flags;
7082 register asection *s;
7083 struct elf_link_hash_entry *h;
7084 struct mips_got_info *g;
7086 /* This function may be called more than once. */
7087 if (mips_elf_got_section (abfd))
7088 return true;
7090 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7091 | SEC_LINKER_CREATED);
7093 s = bfd_make_section (abfd, ".got");
7094 if (s == NULL
7095 || ! bfd_set_section_flags (abfd, s, flags)
7096 || ! bfd_set_section_alignment (abfd, s, 4))
7097 return false;
7099 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
7100 linker script because we don't want to define the symbol if we
7101 are not creating a global offset table. */
7102 h = NULL;
7103 if (! (_bfd_generic_link_add_one_symbol
7104 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
7105 (bfd_vma) 0, (const char *) NULL, false,
7106 get_elf_backend_data (abfd)->collect,
7107 (struct bfd_link_hash_entry **) &h)))
7108 return false;
7109 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
7110 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
7111 h->type = STT_OBJECT;
7113 if (info->shared
7114 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
7115 return false;
7117 /* The first several global offset table entries are reserved. */
7118 s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
7120 g = (struct mips_got_info *) bfd_alloc (abfd,
7121 sizeof (struct mips_got_info));
7122 if (g == NULL)
7123 return false;
7124 g->global_gotsym = NULL;
7125 g->local_gotno = MIPS_RESERVED_GOTNO;
7126 g->assigned_gotno = MIPS_RESERVED_GOTNO;
7127 if (elf_section_data (s) == NULL)
7129 s->used_by_bfd =
7130 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
7131 if (elf_section_data (s) == NULL)
7132 return false;
7134 elf_section_data (s)->tdata = (PTR) g;
7135 elf_section_data (s)->this_hdr.sh_flags
7136 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7138 return true;
7141 /* Returns the .msym section for ABFD, creating it if it does not
7142 already exist. Returns NULL to indicate error. */
7144 static asection *
7145 mips_elf_create_msym_section (abfd)
7146 bfd *abfd;
7148 asection *s;
7150 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7151 if (!s)
7153 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
7154 if (!s
7155 || !bfd_set_section_flags (abfd, s,
7156 SEC_ALLOC
7157 | SEC_LOAD
7158 | SEC_HAS_CONTENTS
7159 | SEC_LINKER_CREATED
7160 | SEC_READONLY)
7161 || !bfd_set_section_alignment (abfd, s,
7162 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7163 return NULL;
7166 return s;
7169 /* Add room for N relocations to the .rel.dyn section in ABFD. */
7171 static void
7172 mips_elf_allocate_dynamic_relocations (abfd, n)
7173 bfd *abfd;
7174 unsigned int n;
7176 asection *s;
7178 s = bfd_get_section_by_name (abfd, MIPS_ELF_REL_DYN_SECTION_NAME (abfd));
7179 BFD_ASSERT (s != NULL);
7181 if (s->_raw_size == 0)
7183 /* Make room for a null element. */
7184 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
7185 ++s->reloc_count;
7187 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
7190 /* Look through the relocs for a section during the first phase, and
7191 allocate space in the global offset table. */
7193 boolean
7194 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
7195 bfd *abfd;
7196 struct bfd_link_info *info;
7197 asection *sec;
7198 const Elf_Internal_Rela *relocs;
7200 const char *name;
7201 bfd *dynobj;
7202 Elf_Internal_Shdr *symtab_hdr;
7203 struct elf_link_hash_entry **sym_hashes;
7204 struct mips_got_info *g;
7205 size_t extsymoff;
7206 const Elf_Internal_Rela *rel;
7207 const Elf_Internal_Rela *rel_end;
7208 asection *sgot;
7209 asection *sreloc;
7210 struct elf_backend_data *bed;
7212 if (info->relocateable)
7213 return true;
7215 dynobj = elf_hash_table (info)->dynobj;
7216 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7217 sym_hashes = elf_sym_hashes (abfd);
7218 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7220 /* Check for the mips16 stub sections. */
7222 name = bfd_get_section_name (abfd, sec);
7223 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
7225 unsigned long r_symndx;
7227 /* Look at the relocation information to figure out which symbol
7228 this is for. */
7230 r_symndx = ELF32_R_SYM (relocs->r_info);
7232 if (r_symndx < extsymoff
7233 || sym_hashes[r_symndx - extsymoff] == NULL)
7235 asection *o;
7237 /* This stub is for a local symbol. This stub will only be
7238 needed if there is some relocation in this BFD, other
7239 than a 16 bit function call, which refers to this symbol. */
7240 for (o = abfd->sections; o != NULL; o = o->next)
7242 Elf_Internal_Rela *sec_relocs;
7243 const Elf_Internal_Rela *r, *rend;
7245 /* We can ignore stub sections when looking for relocs. */
7246 if ((o->flags & SEC_RELOC) == 0
7247 || o->reloc_count == 0
7248 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
7249 sizeof FN_STUB - 1) == 0
7250 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
7251 sizeof CALL_STUB - 1) == 0
7252 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
7253 sizeof CALL_FP_STUB - 1) == 0)
7254 continue;
7256 sec_relocs = (_bfd_elf32_link_read_relocs
7257 (abfd, o, (PTR) NULL,
7258 (Elf_Internal_Rela *) NULL,
7259 info->keep_memory));
7260 if (sec_relocs == NULL)
7261 return false;
7263 rend = sec_relocs + o->reloc_count;
7264 for (r = sec_relocs; r < rend; r++)
7265 if (ELF32_R_SYM (r->r_info) == r_symndx
7266 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
7267 break;
7269 if (! info->keep_memory)
7270 free (sec_relocs);
7272 if (r < rend)
7273 break;
7276 if (o == NULL)
7278 /* There is no non-call reloc for this stub, so we do
7279 not need it. Since this function is called before
7280 the linker maps input sections to output sections, we
7281 can easily discard it by setting the SEC_EXCLUDE
7282 flag. */
7283 sec->flags |= SEC_EXCLUDE;
7284 return true;
7287 /* Record this stub in an array of local symbol stubs for
7288 this BFD. */
7289 if (elf_tdata (abfd)->local_stubs == NULL)
7291 unsigned long symcount;
7292 asection **n;
7294 if (elf_bad_symtab (abfd))
7295 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
7296 else
7297 symcount = symtab_hdr->sh_info;
7298 n = (asection **) bfd_zalloc (abfd,
7299 symcount * sizeof (asection *));
7300 if (n == NULL)
7301 return false;
7302 elf_tdata (abfd)->local_stubs = n;
7305 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7307 /* We don't need to set mips16_stubs_seen in this case.
7308 That flag is used to see whether we need to look through
7309 the global symbol table for stubs. We don't need to set
7310 it here, because we just have a local stub. */
7312 else
7314 struct mips_elf_link_hash_entry *h;
7316 h = ((struct mips_elf_link_hash_entry *)
7317 sym_hashes[r_symndx - extsymoff]);
7319 /* H is the symbol this stub is for. */
7321 h->fn_stub = sec;
7322 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7325 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
7326 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7328 unsigned long r_symndx;
7329 struct mips_elf_link_hash_entry *h;
7330 asection **loc;
7332 /* Look at the relocation information to figure out which symbol
7333 this is for. */
7335 r_symndx = ELF32_R_SYM (relocs->r_info);
7337 if (r_symndx < extsymoff
7338 || sym_hashes[r_symndx - extsymoff] == NULL)
7340 /* This stub was actually built for a static symbol defined
7341 in the same file. We assume that all static symbols in
7342 mips16 code are themselves mips16, so we can simply
7343 discard this stub. Since this function is called before
7344 the linker maps input sections to output sections, we can
7345 easily discard it by setting the SEC_EXCLUDE flag. */
7346 sec->flags |= SEC_EXCLUDE;
7347 return true;
7350 h = ((struct mips_elf_link_hash_entry *)
7351 sym_hashes[r_symndx - extsymoff]);
7353 /* H is the symbol this stub is for. */
7355 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
7356 loc = &h->call_fp_stub;
7357 else
7358 loc = &h->call_stub;
7360 /* If we already have an appropriate stub for this function, we
7361 don't need another one, so we can discard this one. Since
7362 this function is called before the linker maps input sections
7363 to output sections, we can easily discard it by setting the
7364 SEC_EXCLUDE flag. We can also discard this section if we
7365 happen to already know that this is a mips16 function; it is
7366 not necessary to check this here, as it is checked later, but
7367 it is slightly faster to check now. */
7368 if (*loc != NULL || h->root.other == STO_MIPS16)
7370 sec->flags |= SEC_EXCLUDE;
7371 return true;
7374 *loc = sec;
7375 mips_elf_hash_table (info)->mips16_stubs_seen = true;
7378 if (dynobj == NULL)
7380 sgot = NULL;
7381 g = NULL;
7383 else
7385 sgot = mips_elf_got_section (dynobj);
7386 if (sgot == NULL)
7387 g = NULL;
7388 else
7390 BFD_ASSERT (elf_section_data (sgot) != NULL);
7391 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7392 BFD_ASSERT (g != NULL);
7396 sreloc = NULL;
7397 bed = get_elf_backend_data (abfd);
7398 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7399 for (rel = relocs; rel < rel_end; ++rel)
7401 unsigned long r_symndx;
7402 int r_type;
7403 struct elf_link_hash_entry *h;
7405 r_symndx = ELF32_R_SYM (rel->r_info);
7406 r_type = ELF32_R_TYPE (rel->r_info);
7408 if (r_symndx < extsymoff)
7409 h = NULL;
7410 else
7412 h = sym_hashes[r_symndx - extsymoff];
7414 /* This may be an indirect symbol created because of a version. */
7415 if (h != NULL)
7417 while (h->root.type == bfd_link_hash_indirect)
7418 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7422 /* Some relocs require a global offset table. */
7423 if (dynobj == NULL || sgot == NULL)
7425 switch (r_type)
7427 case R_MIPS_GOT16:
7428 case R_MIPS_CALL16:
7429 case R_MIPS_CALL_HI16:
7430 case R_MIPS_CALL_LO16:
7431 case R_MIPS_GOT_HI16:
7432 case R_MIPS_GOT_LO16:
7433 case R_MIPS_GOT_PAGE:
7434 case R_MIPS_GOT_OFST:
7435 case R_MIPS_GOT_DISP:
7436 if (dynobj == NULL)
7437 elf_hash_table (info)->dynobj = dynobj = abfd;
7438 if (! mips_elf_create_got_section (dynobj, info))
7439 return false;
7440 g = mips_elf_got_info (dynobj, &sgot);
7441 break;
7443 case R_MIPS_32:
7444 case R_MIPS_REL32:
7445 case R_MIPS_64:
7446 if (dynobj == NULL
7447 && (info->shared || h != NULL)
7448 && (sec->flags & SEC_ALLOC) != 0)
7449 elf_hash_table (info)->dynobj = dynobj = abfd;
7450 break;
7452 default:
7453 break;
7457 if (!h && (r_type == R_MIPS_CALL_LO16
7458 || r_type == R_MIPS_GOT_LO16
7459 || r_type == R_MIPS_GOT_DISP))
7461 /* We may need a local GOT entry for this relocation. We
7462 don't count R_MIPS_GOT_PAGE because we can estimate the
7463 maximum number of pages needed by looking at the size of
7464 the segment. Similar comments apply to R_MIPS_GOT16. We
7465 don't count R_MIPS_GOT_HI16, or R_MIPS_CALL_HI16 because
7466 these are always followed by an R_MIPS_GOT_LO16 or
7467 R_MIPS_CALL_LO16.
7469 This estimation is very conservative since we can merge
7470 duplicate entries in the GOT. In order to be less
7471 conservative, we could actually build the GOT here,
7472 rather than in relocate_section. */
7473 g->local_gotno++;
7474 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
7477 switch (r_type)
7479 case R_MIPS_CALL16:
7480 if (h == NULL)
7482 (*_bfd_error_handler)
7483 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
7484 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
7485 bfd_set_error (bfd_error_bad_value);
7486 return false;
7488 /* Fall through. */
7490 case R_MIPS_CALL_HI16:
7491 case R_MIPS_CALL_LO16:
7492 if (h != NULL)
7494 /* This symbol requires a global offset table entry. */
7495 if (!mips_elf_record_global_got_symbol (h, info, g))
7496 return false;
7498 /* We need a stub, not a plt entry for the undefined
7499 function. But we record it as if it needs plt. See
7500 elf_adjust_dynamic_symbol in elflink.h. */
7501 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
7502 h->type = STT_FUNC;
7504 break;
7506 case R_MIPS_GOT16:
7507 case R_MIPS_GOT_HI16:
7508 case R_MIPS_GOT_LO16:
7509 case R_MIPS_GOT_DISP:
7510 /* This symbol requires a global offset table entry. */
7511 if (h && !mips_elf_record_global_got_symbol (h, info, g))
7512 return false;
7513 break;
7515 case R_MIPS_32:
7516 case R_MIPS_REL32:
7517 case R_MIPS_64:
7518 if ((info->shared || h != NULL)
7519 && (sec->flags & SEC_ALLOC) != 0)
7521 if (sreloc == NULL)
7523 const char *name = MIPS_ELF_REL_DYN_SECTION_NAME (dynobj);
7525 sreloc = bfd_get_section_by_name (dynobj, name);
7526 if (sreloc == NULL)
7528 sreloc = bfd_make_section (dynobj, name);
7529 if (sreloc == NULL
7530 || ! bfd_set_section_flags (dynobj, sreloc,
7531 (SEC_ALLOC
7532 | SEC_LOAD
7533 | SEC_HAS_CONTENTS
7534 | SEC_IN_MEMORY
7535 | SEC_LINKER_CREATED
7536 | SEC_READONLY))
7537 || ! bfd_set_section_alignment (dynobj, sreloc,
7539 return false;
7542 if (info->shared)
7543 /* When creating a shared object, we must copy these
7544 reloc types into the output file as R_MIPS_REL32
7545 relocs. We make room for this reloc in the
7546 .rel.dyn reloc section. */
7547 mips_elf_allocate_dynamic_relocations (dynobj, 1);
7548 else
7550 struct mips_elf_link_hash_entry *hmips;
7552 /* We only need to copy this reloc if the symbol is
7553 defined in a dynamic object. */
7554 hmips = (struct mips_elf_link_hash_entry *) h;
7555 ++hmips->possibly_dynamic_relocs;
7558 /* Even though we don't directly need a GOT entry for
7559 this symbol, a symbol must have a dynamic symbol
7560 table index greater that DT_MIPS_GOTSYM if there are
7561 dynamic relocations against it. */
7562 if (h != NULL
7563 && !mips_elf_record_global_got_symbol (h, info, g))
7564 return false;
7567 if (SGI_COMPAT (dynobj))
7568 mips_elf_hash_table (info)->compact_rel_size +=
7569 sizeof (Elf32_External_crinfo);
7570 break;
7572 case R_MIPS_26:
7573 case R_MIPS_GPREL16:
7574 case R_MIPS_LITERAL:
7575 case R_MIPS_GPREL32:
7576 if (SGI_COMPAT (dynobj))
7577 mips_elf_hash_table (info)->compact_rel_size +=
7578 sizeof (Elf32_External_crinfo);
7579 break;
7581 /* This relocation describes the C++ object vtable hierarchy.
7582 Reconstruct it for later use during GC. */
7583 case R_MIPS_GNU_VTINHERIT:
7584 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7585 return false;
7586 break;
7588 /* This relocation describes which C++ vtable entries are actually
7589 used. Record for later use during GC. */
7590 case R_MIPS_GNU_VTENTRY:
7591 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7592 return false;
7593 break;
7595 default:
7596 break;
7599 /* If this reloc is not a 16 bit call, and it has a global
7600 symbol, then we will need the fn_stub if there is one.
7601 References from a stub section do not count. */
7602 if (h != NULL
7603 && r_type != R_MIPS16_26
7604 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
7605 sizeof FN_STUB - 1) != 0
7606 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
7607 sizeof CALL_STUB - 1) != 0
7608 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
7609 sizeof CALL_FP_STUB - 1) != 0)
7611 struct mips_elf_link_hash_entry *mh;
7613 mh = (struct mips_elf_link_hash_entry *) h;
7614 mh->need_fn_stub = true;
7618 return true;
7621 /* Return the section that should be marked against GC for a given
7622 relocation. */
7624 asection *
7625 _bfd_mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
7626 bfd *abfd;
7627 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7628 Elf_Internal_Rela *rel;
7629 struct elf_link_hash_entry *h;
7630 Elf_Internal_Sym *sym;
7632 /* ??? Do mips16 stub sections need to be handled special? */
7634 if (h != NULL)
7636 switch (ELF32_R_TYPE (rel->r_info))
7638 case R_MIPS_GNU_VTINHERIT:
7639 case R_MIPS_GNU_VTENTRY:
7640 break;
7642 default:
7643 switch (h->root.type)
7645 case bfd_link_hash_defined:
7646 case bfd_link_hash_defweak:
7647 return h->root.u.def.section;
7649 case bfd_link_hash_common:
7650 return h->root.u.c.p->section;
7652 default:
7653 break;
7657 else
7659 if (!(elf_bad_symtab (abfd)
7660 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7661 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
7662 && sym->st_shndx != SHN_COMMON))
7664 return bfd_section_from_elf_index (abfd, sym->st_shndx);
7668 return NULL;
7671 /* Update the got entry reference counts for the section being removed. */
7673 boolean
7674 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
7675 bfd *abfd ATTRIBUTE_UNUSED;
7676 struct bfd_link_info *info ATTRIBUTE_UNUSED;
7677 asection *sec ATTRIBUTE_UNUSED;
7678 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
7680 #if 0
7681 Elf_Internal_Shdr *symtab_hdr;
7682 struct elf_link_hash_entry **sym_hashes;
7683 bfd_signed_vma *local_got_refcounts;
7684 const Elf_Internal_Rela *rel, *relend;
7685 unsigned long r_symndx;
7686 struct elf_link_hash_entry *h;
7688 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7689 sym_hashes = elf_sym_hashes (abfd);
7690 local_got_refcounts = elf_local_got_refcounts (abfd);
7692 relend = relocs + sec->reloc_count;
7693 for (rel = relocs; rel < relend; rel++)
7694 switch (ELF32_R_TYPE (rel->r_info))
7696 case R_MIPS_GOT16:
7697 case R_MIPS_CALL16:
7698 case R_MIPS_CALL_HI16:
7699 case R_MIPS_CALL_LO16:
7700 case R_MIPS_GOT_HI16:
7701 case R_MIPS_GOT_LO16:
7702 /* ??? It would seem that the existing MIPS code does no sort
7703 of reference counting or whatnot on its GOT and PLT entries,
7704 so it is not possible to garbage collect them at this time. */
7705 break;
7707 default:
7708 break;
7710 #endif
7712 return true;
7716 /* Adjust a symbol defined by a dynamic object and referenced by a
7717 regular object. The current definition is in some section of the
7718 dynamic object, but we're not including those sections. We have to
7719 change the definition to something the rest of the link can
7720 understand. */
7722 boolean
7723 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
7724 struct bfd_link_info *info;
7725 struct elf_link_hash_entry *h;
7727 bfd *dynobj;
7728 struct mips_elf_link_hash_entry *hmips;
7729 asection *s;
7731 dynobj = elf_hash_table (info)->dynobj;
7733 /* Make sure we know what is going on here. */
7734 BFD_ASSERT (dynobj != NULL
7735 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
7736 || h->weakdef != NULL
7737 || ((h->elf_link_hash_flags
7738 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7739 && (h->elf_link_hash_flags
7740 & ELF_LINK_HASH_REF_REGULAR) != 0
7741 && (h->elf_link_hash_flags
7742 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
7744 /* If this symbol is defined in a dynamic object, we need to copy
7745 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
7746 file. */
7747 hmips = (struct mips_elf_link_hash_entry *) h;
7748 if (! info->relocateable
7749 && hmips->possibly_dynamic_relocs != 0
7750 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7751 mips_elf_allocate_dynamic_relocations (dynobj,
7752 hmips->possibly_dynamic_relocs);
7754 /* For a function, create a stub, if needed. */
7755 if (h->type == STT_FUNC
7756 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
7758 if (! elf_hash_table (info)->dynamic_sections_created)
7759 return true;
7761 /* If this symbol is not defined in a regular file, then set
7762 the symbol to the stub location. This is required to make
7763 function pointers compare as equal between the normal
7764 executable and the shared library. */
7765 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
7767 /* We need .stub section. */
7768 s = bfd_get_section_by_name (dynobj,
7769 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7770 BFD_ASSERT (s != NULL);
7772 h->root.u.def.section = s;
7773 h->root.u.def.value = s->_raw_size;
7775 /* XXX Write this stub address somewhere. */
7776 h->plt.offset = s->_raw_size;
7778 /* Make room for this stub code. */
7779 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7781 /* The last half word of the stub will be filled with the index
7782 of this symbol in .dynsym section. */
7783 return true;
7787 /* If this is a weak symbol, and there is a real definition, the
7788 processor independent code will have arranged for us to see the
7789 real definition first, and we can just use the same value. */
7790 if (h->weakdef != NULL)
7792 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
7793 || h->weakdef->root.type == bfd_link_hash_defweak);
7794 h->root.u.def.section = h->weakdef->root.u.def.section;
7795 h->root.u.def.value = h->weakdef->root.u.def.value;
7796 return true;
7799 /* This is a reference to a symbol defined by a dynamic object which
7800 is not a function. */
7802 return true;
7805 /* This function is called after all the input files have been read,
7806 and the input sections have been assigned to output sections. We
7807 check for any mips16 stub sections that we can discard. */
7809 static boolean mips_elf_check_mips16_stubs
7810 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
7812 boolean
7813 _bfd_mips_elf_always_size_sections (output_bfd, info)
7814 bfd *output_bfd;
7815 struct bfd_link_info *info;
7817 asection *ri;
7819 /* The .reginfo section has a fixed size. */
7820 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7821 if (ri != NULL)
7822 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
7824 if (info->relocateable
7825 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
7826 return true;
7828 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7829 mips_elf_check_mips16_stubs,
7830 (PTR) NULL);
7832 return true;
7835 /* Check the mips16 stubs for a particular symbol, and see if we can
7836 discard them. */
7838 /*ARGSUSED*/
7839 static boolean
7840 mips_elf_check_mips16_stubs (h, data)
7841 struct mips_elf_link_hash_entry *h;
7842 PTR data ATTRIBUTE_UNUSED;
7844 if (h->fn_stub != NULL
7845 && ! h->need_fn_stub)
7847 /* We don't need the fn_stub; the only references to this symbol
7848 are 16 bit calls. Clobber the size to 0 to prevent it from
7849 being included in the link. */
7850 h->fn_stub->_raw_size = 0;
7851 h->fn_stub->_cooked_size = 0;
7852 h->fn_stub->flags &= ~ SEC_RELOC;
7853 h->fn_stub->reloc_count = 0;
7854 h->fn_stub->flags |= SEC_EXCLUDE;
7857 if (h->call_stub != NULL
7858 && h->root.other == STO_MIPS16)
7860 /* We don't need the call_stub; this is a 16 bit function, so
7861 calls from other 16 bit functions are OK. Clobber the size
7862 to 0 to prevent it from being included in the link. */
7863 h->call_stub->_raw_size = 0;
7864 h->call_stub->_cooked_size = 0;
7865 h->call_stub->flags &= ~ SEC_RELOC;
7866 h->call_stub->reloc_count = 0;
7867 h->call_stub->flags |= SEC_EXCLUDE;
7870 if (h->call_fp_stub != NULL
7871 && h->root.other == STO_MIPS16)
7873 /* We don't need the call_stub; this is a 16 bit function, so
7874 calls from other 16 bit functions are OK. Clobber the size
7875 to 0 to prevent it from being included in the link. */
7876 h->call_fp_stub->_raw_size = 0;
7877 h->call_fp_stub->_cooked_size = 0;
7878 h->call_fp_stub->flags &= ~ SEC_RELOC;
7879 h->call_fp_stub->reloc_count = 0;
7880 h->call_fp_stub->flags |= SEC_EXCLUDE;
7883 return true;
7886 /* Set the sizes of the dynamic sections. */
7888 boolean
7889 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
7890 bfd *output_bfd;
7891 struct bfd_link_info *info;
7893 bfd *dynobj;
7894 asection *s;
7895 boolean reltext;
7896 struct mips_got_info *g = NULL;
7898 dynobj = elf_hash_table (info)->dynobj;
7899 BFD_ASSERT (dynobj != NULL);
7901 if (elf_hash_table (info)->dynamic_sections_created)
7903 /* Set the contents of the .interp section to the interpreter. */
7904 if (! info->shared)
7906 s = bfd_get_section_by_name (dynobj, ".interp");
7907 BFD_ASSERT (s != NULL);
7908 s->_raw_size
7909 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7910 s->contents
7911 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7915 /* The check_relocs and adjust_dynamic_symbol entry points have
7916 determined the sizes of the various dynamic sections. Allocate
7917 memory for them. */
7918 reltext = false;
7919 for (s = dynobj->sections; s != NULL; s = s->next)
7921 const char *name;
7922 boolean strip;
7924 /* It's OK to base decisions on the section name, because none
7925 of the dynobj section names depend upon the input files. */
7926 name = bfd_get_section_name (dynobj, s);
7928 if ((s->flags & SEC_LINKER_CREATED) == 0)
7929 continue;
7931 strip = false;
7933 if (strncmp (name, ".rel", 4) == 0)
7935 if (s->_raw_size == 0)
7937 /* We only strip the section if the output section name
7938 has the same name. Otherwise, there might be several
7939 input sections for this output section. FIXME: This
7940 code is probably not needed these days anyhow, since
7941 the linker now does not create empty output sections. */
7942 if (s->output_section != NULL
7943 && strcmp (name,
7944 bfd_get_section_name (s->output_section->owner,
7945 s->output_section)) == 0)
7946 strip = true;
7948 else
7950 const char *outname;
7951 asection *target;
7953 /* If this relocation section applies to a read only
7954 section, then we probably need a DT_TEXTREL entry.
7955 If the relocation section is .rel.dyn, we always
7956 assert a DT_TEXTREL entry rather than testing whether
7957 there exists a relocation to a read only section or
7958 not. */
7959 outname = bfd_get_section_name (output_bfd,
7960 s->output_section);
7961 target = bfd_get_section_by_name (output_bfd, outname + 4);
7962 if ((target != NULL
7963 && (target->flags & SEC_READONLY) != 0
7964 && (target->flags & SEC_ALLOC) != 0)
7965 || strcmp (outname,
7966 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) == 0)
7967 reltext = true;
7969 /* We use the reloc_count field as a counter if we need
7970 to copy relocs into the output file. */
7971 if (strcmp (name,
7972 MIPS_ELF_REL_DYN_SECTION_NAME (output_bfd)) != 0)
7973 s->reloc_count = 0;
7976 else if (strncmp (name, ".got", 4) == 0)
7978 int i;
7979 bfd_size_type loadable_size = 0;
7980 bfd_size_type local_gotno;
7981 struct _bfd *sub;
7983 BFD_ASSERT (elf_section_data (s) != NULL);
7984 g = (struct mips_got_info *) elf_section_data (s)->tdata;
7985 BFD_ASSERT (g != NULL);
7987 /* Calculate the total loadable size of the output. That
7988 will give us the maximum number of GOT_PAGE entries
7989 required. */
7990 for (sub = info->input_bfds; sub; sub = sub->link_next)
7992 asection *subsection;
7994 for (subsection = sub->sections;
7995 subsection;
7996 subsection = subsection->next)
7998 if ((subsection->flags & SEC_ALLOC) == 0)
7999 continue;
8000 loadable_size += (subsection->_raw_size + 0xf) & ~0xf;
8003 loadable_size += MIPS_FUNCTION_STUB_SIZE;
8005 /* Assume there are two loadable segments consisting of
8006 contiguous sections. Is 5 enough? */
8007 local_gotno = (loadable_size >> 16) + 5;
8008 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8009 /* It's possible we will need GOT_PAGE entries as well as
8010 GOT16 entries. Often, these will be able to share GOT
8011 entries, but not always. */
8012 local_gotno *= 2;
8014 g->local_gotno += local_gotno;
8015 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
8017 /* There has to be a global GOT entry for every symbol with
8018 a dynamic symbol table index of DT_MIPS_GOTSYM or
8019 higher. Therefore, it make sense to put those symbols
8020 that need GOT entries at the end of the symbol table. We
8021 do that here. */
8022 if (!mips_elf_sort_hash_table (info, 1))
8023 return false;
8025 if (g->global_gotsym != NULL)
8026 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
8027 else
8028 /* If there are no global symbols, or none requiring
8029 relocations, then GLOBAL_GOTSYM will be NULL. */
8030 i = 0;
8031 g->global_gotno = i;
8032 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
8034 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
8036 /* Irix rld assumes that the function stub isn't at the end
8037 of .text section. So put a dummy. XXX */
8038 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
8040 else if (! info->shared
8041 && ! mips_elf_hash_table (info)->use_rld_obj_head
8042 && strncmp (name, ".rld_map", 8) == 0)
8044 /* We add a room for __rld_map. It will be filled in by the
8045 rtld to contain a pointer to the _r_debug structure. */
8046 s->_raw_size += 4;
8048 else if (SGI_COMPAT (output_bfd)
8049 && strncmp (name, ".compact_rel", 12) == 0)
8050 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
8051 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
8052 == 0)
8053 s->_raw_size = (sizeof (Elf32_External_Msym)
8054 * (elf_hash_table (info)->dynsymcount
8055 + bfd_count_sections (output_bfd)));
8056 else if (strncmp (name, ".init", 5) != 0)
8058 /* It's not one of our sections, so don't allocate space. */
8059 continue;
8062 if (strip)
8064 _bfd_strip_section_from_output (s);
8065 continue;
8068 /* Allocate memory for the section contents. */
8069 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
8070 if (s->contents == NULL && s->_raw_size != 0)
8072 bfd_set_error (bfd_error_no_memory);
8073 return false;
8077 if (elf_hash_table (info)->dynamic_sections_created)
8079 /* Add some entries to the .dynamic section. We fill in the
8080 values later, in elf_mips_finish_dynamic_sections, but we
8081 must add the entries now so that we get the correct size for
8082 the .dynamic section. The DT_DEBUG entry is filled in by the
8083 dynamic linker and used by the debugger. */
8084 if (! info->shared)
8086 if (SGI_COMPAT (output_bfd))
8088 /* SGI object has the equivalence of DT_DEBUG in the
8089 DT_MIPS_RLD_MAP entry. */
8090 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8091 return false;
8093 else
8094 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8095 return false;
8098 if (reltext)
8100 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8101 return false;
8104 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8105 return false;
8107 if (bfd_get_section_by_name (dynobj,
8108 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)))
8110 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8111 return false;
8113 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8114 return false;
8116 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8117 return false;
8120 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
8121 return false;
8123 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
8124 return false;
8126 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
8128 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
8129 return false;
8131 s = bfd_get_section_by_name (dynobj, ".liblist");
8132 BFD_ASSERT (s != NULL);
8134 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
8135 return false;
8138 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8139 return false;
8141 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8142 return false;
8144 #if 0
8145 /* Time stamps in executable files are a bad idea. */
8146 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
8147 return false;
8148 #endif
8150 #if 0 /* FIXME */
8151 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
8152 return false;
8153 #endif
8155 #if 0 /* FIXME */
8156 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
8157 return false;
8158 #endif
8160 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8161 return false;
8163 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8164 return false;
8166 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8167 return false;
8169 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8170 return false;
8172 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8173 return false;
8175 if (IRIX_COMPAT (dynobj) == ict_irix5
8176 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8177 return false;
8179 if (IRIX_COMPAT (dynobj) == ict_irix6
8180 && (bfd_get_section_by_name
8181 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8182 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8183 return false;
8185 if (bfd_get_section_by_name (dynobj,
8186 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
8187 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
8188 return false;
8191 return true;
8194 /* If NAME is one of the special IRIX6 symbols defined by the linker,
8195 adjust it appropriately now. */
8197 static void
8198 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
8199 bfd *abfd ATTRIBUTE_UNUSED;
8200 const char *name;
8201 Elf_Internal_Sym *sym;
8203 /* The linker script takes care of providing names and values for
8204 these, but we must place them into the right sections. */
8205 static const char* const text_section_symbols[] = {
8206 "_ftext",
8207 "_etext",
8208 "__dso_displacement",
8209 "__elf_header",
8210 "__program_header_table",
8211 NULL
8214 static const char* const data_section_symbols[] = {
8215 "_fdata",
8216 "_edata",
8217 "_end",
8218 "_fbss",
8219 NULL
8222 const char* const *p;
8223 int i;
8225 for (i = 0; i < 2; ++i)
8226 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
8228 ++p)
8229 if (strcmp (*p, name) == 0)
8231 /* All of these symbols are given type STT_SECTION by the
8232 IRIX6 linker. */
8233 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8235 /* The IRIX linker puts these symbols in special sections. */
8236 if (i == 0)
8237 sym->st_shndx = SHN_MIPS_TEXT;
8238 else
8239 sym->st_shndx = SHN_MIPS_DATA;
8241 break;
8245 /* Finish up dynamic symbol handling. We set the contents of various
8246 dynamic sections here. */
8248 boolean
8249 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8250 bfd *output_bfd;
8251 struct bfd_link_info *info;
8252 struct elf_link_hash_entry *h;
8253 Elf_Internal_Sym *sym;
8255 bfd *dynobj;
8256 bfd_vma gval;
8257 asection *sgot;
8258 asection *smsym;
8259 struct mips_got_info *g;
8260 const char *name;
8261 struct mips_elf_link_hash_entry *mh;
8263 dynobj = elf_hash_table (info)->dynobj;
8264 gval = sym->st_value;
8265 mh = (struct mips_elf_link_hash_entry *) h;
8267 if (h->plt.offset != (bfd_vma) -1)
8269 asection *s;
8270 bfd_byte *p;
8271 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
8273 /* This symbol has a stub. Set it up. */
8275 BFD_ASSERT (h->dynindx != -1);
8277 s = bfd_get_section_by_name (dynobj,
8278 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8279 BFD_ASSERT (s != NULL);
8281 /* Fill the stub. */
8282 p = stub;
8283 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
8284 p += 4;
8285 bfd_put_32 (output_bfd, STUB_MOVE, p);
8286 p += 4;
8288 /* FIXME: Can h->dynindex be more than 64K? */
8289 if (h->dynindx & 0xffff0000)
8290 return false;
8292 bfd_put_32 (output_bfd, STUB_JALR, p);
8293 p += 4;
8294 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
8296 BFD_ASSERT (h->plt.offset <= s->_raw_size);
8297 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
8299 /* Mark the symbol as undefined. plt.offset != -1 occurs
8300 only for the referenced symbol. */
8301 sym->st_shndx = SHN_UNDEF;
8303 /* The run-time linker uses the st_value field of the symbol
8304 to reset the global offset table entry for this external
8305 to its stub address when unlinking a shared object. */
8306 gval = s->output_section->vma + s->output_offset + h->plt.offset;
8307 sym->st_value = gval;
8310 BFD_ASSERT (h->dynindx != -1);
8312 sgot = mips_elf_got_section (dynobj);
8313 BFD_ASSERT (sgot != NULL);
8314 BFD_ASSERT (elf_section_data (sgot) != NULL);
8315 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8316 BFD_ASSERT (g != NULL);
8318 /* Run through the global symbol table, creating GOT entries for all
8319 the symbols that need them. */
8320 if (g->global_gotsym != NULL
8321 && h->dynindx >= g->global_gotsym->dynindx)
8323 bfd_vma offset;
8324 bfd_vma value;
8326 if (sym->st_value)
8327 value = sym->st_value;
8328 else
8329 /* For an entity defined in a shared object, this will be
8330 NULL. (For functions in shared objects for
8331 which we have created stubs, ST_VALUE will be non-NULL.
8332 That's because such the functions are now no longer defined
8333 in a shared object.) */
8334 value = h->root.u.def.value;
8336 offset = mips_elf_global_got_index (dynobj, h);
8337 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8340 /* Create a .msym entry, if appropriate. */
8341 smsym = bfd_get_section_by_name (dynobj,
8342 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8343 if (smsym)
8345 Elf32_Internal_Msym msym;
8347 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
8348 /* It is undocumented what the `1' indicates, but IRIX6 uses
8349 this value. */
8350 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
8351 bfd_mips_elf_swap_msym_out
8352 (dynobj, &msym,
8353 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
8356 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8357 name = h->root.root.string;
8358 if (strcmp (name, "_DYNAMIC") == 0
8359 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
8360 sym->st_shndx = SHN_ABS;
8361 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
8363 sym->st_shndx = SHN_ABS;
8364 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8365 sym->st_value = 1;
8367 else if (SGI_COMPAT (output_bfd))
8369 if (strcmp (name, "_gp_disp") == 0)
8371 sym->st_shndx = SHN_ABS;
8372 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8373 sym->st_value = elf_gp (output_bfd);
8375 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8376 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8378 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8379 sym->st_other = STO_PROTECTED;
8380 sym->st_value = 0;
8381 sym->st_shndx = SHN_MIPS_DATA;
8383 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8385 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8386 sym->st_other = STO_PROTECTED;
8387 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8388 sym->st_shndx = SHN_ABS;
8390 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8392 if (h->type == STT_FUNC)
8393 sym->st_shndx = SHN_MIPS_TEXT;
8394 else if (h->type == STT_OBJECT)
8395 sym->st_shndx = SHN_MIPS_DATA;
8399 /* Handle the IRIX6-specific symbols. */
8400 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8401 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8403 if (SGI_COMPAT (output_bfd)
8404 && ! info->shared)
8406 if (! mips_elf_hash_table (info)->use_rld_obj_head
8407 && strcmp (name, "__rld_map") == 0)
8409 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8410 BFD_ASSERT (s != NULL);
8411 sym->st_value = s->output_section->vma + s->output_offset;
8412 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
8413 if (mips_elf_hash_table (info)->rld_value == 0)
8414 mips_elf_hash_table (info)->rld_value = sym->st_value;
8416 else if (mips_elf_hash_table (info)->use_rld_obj_head
8417 && strcmp (name, "__rld_obj_head") == 0)
8419 /* IRIX6 does not use a .rld_map section. */
8420 if (IRIX_COMPAT (output_bfd) == ict_irix5)
8421 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8422 != NULL);
8423 mips_elf_hash_table (info)->rld_value = sym->st_value;
8427 /* If this is a mips16 symbol, force the value to be even. */
8428 if (sym->st_other == STO_MIPS16
8429 && (sym->st_value & 1) != 0)
8430 --sym->st_value;
8432 return true;
8435 /* Finish up the dynamic sections. */
8437 boolean
8438 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
8439 bfd *output_bfd;
8440 struct bfd_link_info *info;
8442 bfd *dynobj;
8443 asection *sdyn;
8444 asection *sgot;
8445 struct mips_got_info *g;
8447 dynobj = elf_hash_table (info)->dynobj;
8449 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8451 sgot = mips_elf_got_section (dynobj);
8452 if (sgot == NULL)
8453 g = NULL;
8454 else
8456 BFD_ASSERT (elf_section_data (sgot) != NULL);
8457 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
8458 BFD_ASSERT (g != NULL);
8461 if (elf_hash_table (info)->dynamic_sections_created)
8463 bfd_byte *b;
8465 BFD_ASSERT (sdyn != NULL);
8466 BFD_ASSERT (g != NULL);
8468 for (b = sdyn->contents;
8469 b < sdyn->contents + sdyn->_raw_size;
8470 b += MIPS_ELF_DYN_SIZE (dynobj))
8472 Elf_Internal_Dyn dyn;
8473 const char *name;
8474 size_t elemsize;
8475 asection *s;
8476 boolean swap_out_p;
8478 /* Read in the current dynamic entry. */
8479 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
8481 /* Assume that we're going to modify it and write it out. */
8482 swap_out_p = true;
8484 switch (dyn.d_tag)
8486 case DT_RELENT:
8487 s = (bfd_get_section_by_name
8488 (dynobj,
8489 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj)));
8490 BFD_ASSERT (s != NULL);
8491 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
8492 break;
8494 case DT_STRSZ:
8495 /* Rewrite DT_STRSZ. */
8496 dyn.d_un.d_val =
8497 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
8498 break;
8500 case DT_PLTGOT:
8501 name = ".got";
8502 goto get_vma;
8503 case DT_MIPS_CONFLICT:
8504 name = ".conflict";
8505 goto get_vma;
8506 case DT_MIPS_LIBLIST:
8507 name = ".liblist";
8508 get_vma:
8509 s = bfd_get_section_by_name (output_bfd, name);
8510 BFD_ASSERT (s != NULL);
8511 dyn.d_un.d_ptr = s->vma;
8512 break;
8514 case DT_MIPS_RLD_VERSION:
8515 dyn.d_un.d_val = 1; /* XXX */
8516 break;
8518 case DT_MIPS_FLAGS:
8519 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
8520 break;
8522 case DT_MIPS_CONFLICTNO:
8523 name = ".conflict";
8524 elemsize = sizeof (Elf32_Conflict);
8525 goto set_elemno;
8527 case DT_MIPS_LIBLISTNO:
8528 name = ".liblist";
8529 elemsize = sizeof (Elf32_Lib);
8530 set_elemno:
8531 s = bfd_get_section_by_name (output_bfd, name);
8532 if (s != NULL)
8534 if (s->_cooked_size != 0)
8535 dyn.d_un.d_val = s->_cooked_size / elemsize;
8536 else
8537 dyn.d_un.d_val = s->_raw_size / elemsize;
8539 else
8540 dyn.d_un.d_val = 0;
8541 break;
8543 case DT_MIPS_TIME_STAMP:
8544 time ((time_t *) &dyn.d_un.d_val);
8545 break;
8547 case DT_MIPS_ICHECKSUM:
8548 /* XXX FIXME: */
8549 swap_out_p = false;
8550 break;
8552 case DT_MIPS_IVERSION:
8553 /* XXX FIXME: */
8554 swap_out_p = false;
8555 break;
8557 case DT_MIPS_BASE_ADDRESS:
8558 s = output_bfd->sections;
8559 BFD_ASSERT (s != NULL);
8560 dyn.d_un.d_ptr = s->vma & ~(0xffff);
8561 break;
8563 case DT_MIPS_LOCAL_GOTNO:
8564 dyn.d_un.d_val = g->local_gotno;
8565 break;
8567 case DT_MIPS_UNREFEXTNO:
8568 /* The index into the dynamic symbol table which is the
8569 entry of the first external symbol that is not
8570 referenced within the same object. */
8571 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8572 break;
8574 case DT_MIPS_GOTSYM:
8575 if (g->global_gotsym)
8577 dyn.d_un.d_val = g->global_gotsym->dynindx;
8578 break;
8580 /* In case if we don't have global got symbols we default
8581 to setting DT_MIPS_GOTSYM to the same value as
8582 DT_MIPS_SYMTABNO, so we just fall through. */
8584 case DT_MIPS_SYMTABNO:
8585 name = ".dynsym";
8586 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
8587 s = bfd_get_section_by_name (output_bfd, name);
8588 BFD_ASSERT (s != NULL);
8590 if (s->_cooked_size != 0)
8591 dyn.d_un.d_val = s->_cooked_size / elemsize;
8592 else
8593 dyn.d_un.d_val = s->_raw_size / elemsize;
8594 break;
8596 case DT_MIPS_HIPAGENO:
8597 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
8598 break;
8600 case DT_MIPS_RLD_MAP:
8601 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
8602 break;
8604 case DT_MIPS_OPTIONS:
8605 s = (bfd_get_section_by_name
8606 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
8607 dyn.d_un.d_ptr = s->vma;
8608 break;
8610 case DT_MIPS_MSYM:
8611 s = (bfd_get_section_by_name
8612 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
8613 dyn.d_un.d_ptr = s->vma;
8614 break;
8616 default:
8617 swap_out_p = false;
8618 break;
8621 if (swap_out_p)
8622 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
8623 (dynobj, &dyn, b);
8627 /* The first entry of the global offset table will be filled at
8628 runtime. The second entry will be used by some runtime loaders.
8629 This isn't the case of Irix rld. */
8630 if (sgot != NULL && sgot->_raw_size > 0)
8632 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
8633 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
8634 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
8637 if (sgot != NULL)
8638 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
8639 = MIPS_ELF_GOT_SIZE (output_bfd);
8642 asection *smsym;
8643 asection *s;
8644 Elf32_compact_rel cpt;
8646 /* ??? The section symbols for the output sections were set up in
8647 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
8648 symbols. Should we do so? */
8650 smsym = bfd_get_section_by_name (dynobj,
8651 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
8652 if (smsym != NULL)
8654 Elf32_Internal_Msym msym;
8656 msym.ms_hash_value = 0;
8657 msym.ms_info = ELF32_MS_INFO (0, 1);
8659 for (s = output_bfd->sections; s != NULL; s = s->next)
8661 long dynindx = elf_section_data (s)->dynindx;
8663 bfd_mips_elf_swap_msym_out
8664 (output_bfd, &msym,
8665 (((Elf32_External_Msym *) smsym->contents)
8666 + dynindx));
8670 if (SGI_COMPAT (output_bfd))
8672 /* Write .compact_rel section out. */
8673 s = bfd_get_section_by_name (dynobj, ".compact_rel");
8674 if (s != NULL)
8676 cpt.id1 = 1;
8677 cpt.num = s->reloc_count;
8678 cpt.id2 = 2;
8679 cpt.offset = (s->output_section->filepos
8680 + sizeof (Elf32_External_compact_rel));
8681 cpt.reserved0 = 0;
8682 cpt.reserved1 = 0;
8683 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
8684 ((Elf32_External_compact_rel *)
8685 s->contents));
8687 /* Clean up a dummy stub function entry in .text. */
8688 s = bfd_get_section_by_name (dynobj,
8689 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8690 if (s != NULL)
8692 file_ptr dummy_offset;
8694 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
8695 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
8696 memset (s->contents + dummy_offset, 0,
8697 MIPS_FUNCTION_STUB_SIZE);
8702 /* Clean up a first relocation in .rel.dyn. */
8703 s = bfd_get_section_by_name (dynobj,
8704 MIPS_ELF_REL_DYN_SECTION_NAME (dynobj));
8705 if (s != NULL && s->_raw_size > 0)
8706 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
8709 return true;
8712 /* This is almost identical to bfd_generic_get_... except that some
8713 MIPS relocations need to be handled specially. Sigh. */
8715 static bfd_byte *
8716 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
8717 relocateable, symbols)
8718 bfd *abfd;
8719 struct bfd_link_info *link_info;
8720 struct bfd_link_order *link_order;
8721 bfd_byte *data;
8722 boolean relocateable;
8723 asymbol **symbols;
8725 /* Get enough memory to hold the stuff */
8726 bfd *input_bfd = link_order->u.indirect.section->owner;
8727 asection *input_section = link_order->u.indirect.section;
8729 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
8730 arelent **reloc_vector = NULL;
8731 long reloc_count;
8733 if (reloc_size < 0)
8734 goto error_return;
8736 reloc_vector = (arelent **) bfd_malloc (reloc_size);
8737 if (reloc_vector == NULL && reloc_size != 0)
8738 goto error_return;
8740 /* read in the section */
8741 if (!bfd_get_section_contents (input_bfd,
8742 input_section,
8743 (PTR) data,
8745 input_section->_raw_size))
8746 goto error_return;
8748 /* We're not relaxing the section, so just copy the size info */
8749 input_section->_cooked_size = input_section->_raw_size;
8750 input_section->reloc_done = true;
8752 reloc_count = bfd_canonicalize_reloc (input_bfd,
8753 input_section,
8754 reloc_vector,
8755 symbols);
8756 if (reloc_count < 0)
8757 goto error_return;
8759 if (reloc_count > 0)
8761 arelent **parent;
8762 /* for mips */
8763 int gp_found;
8764 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
8767 struct bfd_hash_entry *h;
8768 struct bfd_link_hash_entry *lh;
8769 /* Skip all this stuff if we aren't mixing formats. */
8770 if (abfd && input_bfd
8771 && abfd->xvec == input_bfd->xvec)
8772 lh = 0;
8773 else
8775 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
8776 lh = (struct bfd_link_hash_entry *) h;
8778 lookup:
8779 if (lh)
8781 switch (lh->type)
8783 case bfd_link_hash_undefined:
8784 case bfd_link_hash_undefweak:
8785 case bfd_link_hash_common:
8786 gp_found = 0;
8787 break;
8788 case bfd_link_hash_defined:
8789 case bfd_link_hash_defweak:
8790 gp_found = 1;
8791 gp = lh->u.def.value;
8792 break;
8793 case bfd_link_hash_indirect:
8794 case bfd_link_hash_warning:
8795 lh = lh->u.i.link;
8796 /* @@FIXME ignoring warning for now */
8797 goto lookup;
8798 case bfd_link_hash_new:
8799 default:
8800 abort ();
8803 else
8804 gp_found = 0;
8806 /* end mips */
8807 for (parent = reloc_vector; *parent != (arelent *) NULL;
8808 parent++)
8810 char *error_message = (char *) NULL;
8811 bfd_reloc_status_type r;
8813 /* Specific to MIPS: Deal with relocation types that require
8814 knowing the gp of the output bfd. */
8815 asymbol *sym = *(*parent)->sym_ptr_ptr;
8816 if (bfd_is_abs_section (sym->section) && abfd)
8818 /* The special_function wouldn't get called anyways. */
8820 else if (!gp_found)
8822 /* The gp isn't there; let the special function code
8823 fall over on its own. */
8825 else if ((*parent)->howto->special_function
8826 == _bfd_mips_elf_gprel16_reloc)
8828 /* bypass special_function call */
8829 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
8830 relocateable, (PTR) data, gp);
8831 goto skip_bfd_perform_relocation;
8833 /* end mips specific stuff */
8835 r = bfd_perform_relocation (input_bfd,
8836 *parent,
8837 (PTR) data,
8838 input_section,
8839 relocateable ? abfd : (bfd *) NULL,
8840 &error_message);
8841 skip_bfd_perform_relocation:
8843 if (relocateable)
8845 asection *os = input_section->output_section;
8847 /* A partial link, so keep the relocs */
8848 os->orelocation[os->reloc_count] = *parent;
8849 os->reloc_count++;
8852 if (r != bfd_reloc_ok)
8854 switch (r)
8856 case bfd_reloc_undefined:
8857 if (!((*link_info->callbacks->undefined_symbol)
8858 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8859 input_bfd, input_section, (*parent)->address)))
8860 goto error_return;
8861 break;
8862 case bfd_reloc_dangerous:
8863 BFD_ASSERT (error_message != (char *) NULL);
8864 if (!((*link_info->callbacks->reloc_dangerous)
8865 (link_info, error_message, input_bfd, input_section,
8866 (*parent)->address)))
8867 goto error_return;
8868 break;
8869 case bfd_reloc_overflow:
8870 if (!((*link_info->callbacks->reloc_overflow)
8871 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
8872 (*parent)->howto->name, (*parent)->addend,
8873 input_bfd, input_section, (*parent)->address)))
8874 goto error_return;
8875 break;
8876 case bfd_reloc_outofrange:
8877 default:
8878 abort ();
8879 break;
8885 if (reloc_vector != NULL)
8886 free (reloc_vector);
8887 return data;
8889 error_return:
8890 if (reloc_vector != NULL)
8891 free (reloc_vector);
8892 return NULL;
8894 #define bfd_elf32_bfd_get_relocated_section_contents \
8895 elf32_mips_get_relocated_section_contents
8897 /* ECOFF swapping routines. These are used when dealing with the
8898 .mdebug section, which is in the ECOFF debugging format. */
8899 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
8901 /* Symbol table magic number. */
8902 magicSym,
8903 /* Alignment of debugging information. E.g., 4. */
8905 /* Sizes of external symbolic information. */
8906 sizeof (struct hdr_ext),
8907 sizeof (struct dnr_ext),
8908 sizeof (struct pdr_ext),
8909 sizeof (struct sym_ext),
8910 sizeof (struct opt_ext),
8911 sizeof (struct fdr_ext),
8912 sizeof (struct rfd_ext),
8913 sizeof (struct ext_ext),
8914 /* Functions to swap in external symbolic data. */
8915 ecoff_swap_hdr_in,
8916 ecoff_swap_dnr_in,
8917 ecoff_swap_pdr_in,
8918 ecoff_swap_sym_in,
8919 ecoff_swap_opt_in,
8920 ecoff_swap_fdr_in,
8921 ecoff_swap_rfd_in,
8922 ecoff_swap_ext_in,
8923 _bfd_ecoff_swap_tir_in,
8924 _bfd_ecoff_swap_rndx_in,
8925 /* Functions to swap out external symbolic data. */
8926 ecoff_swap_hdr_out,
8927 ecoff_swap_dnr_out,
8928 ecoff_swap_pdr_out,
8929 ecoff_swap_sym_out,
8930 ecoff_swap_opt_out,
8931 ecoff_swap_fdr_out,
8932 ecoff_swap_rfd_out,
8933 ecoff_swap_ext_out,
8934 _bfd_ecoff_swap_tir_out,
8935 _bfd_ecoff_swap_rndx_out,
8936 /* Function to read in symbolic data. */
8937 _bfd_mips_elf_read_ecoff_info
8940 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
8941 #define TARGET_LITTLE_NAME "elf32-littlemips"
8942 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
8943 #define TARGET_BIG_NAME "elf32-bigmips"
8944 #define ELF_ARCH bfd_arch_mips
8945 #define ELF_MACHINE_CODE EM_MIPS
8947 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
8948 a value of 0x1000, and we are compatible. */
8949 #define ELF_MAXPAGESIZE 0x1000
8951 #define elf_backend_collect true
8952 #define elf_backend_type_change_ok true
8953 #define elf_backend_can_gc_sections true
8954 #define elf_info_to_howto mips_info_to_howto_rela
8955 #define elf_info_to_howto_rel mips_info_to_howto_rel
8956 #define elf_backend_sym_is_global mips_elf_sym_is_global
8957 #define elf_backend_object_p _bfd_mips_elf_object_p
8958 #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
8959 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
8960 #define elf_backend_section_from_bfd_section \
8961 _bfd_mips_elf_section_from_bfd_section
8962 #define elf_backend_section_processing _bfd_mips_elf_section_processing
8963 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
8964 #define elf_backend_additional_program_headers \
8965 _bfd_mips_elf_additional_program_headers
8966 #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
8967 #define elf_backend_final_write_processing \
8968 _bfd_mips_elf_final_write_processing
8969 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
8970 #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
8971 #define elf_backend_create_dynamic_sections \
8972 _bfd_mips_elf_create_dynamic_sections
8973 #define elf_backend_check_relocs _bfd_mips_elf_check_relocs
8974 #define elf_backend_adjust_dynamic_symbol \
8975 _bfd_mips_elf_adjust_dynamic_symbol
8976 #define elf_backend_always_size_sections \
8977 _bfd_mips_elf_always_size_sections
8978 #define elf_backend_size_dynamic_sections \
8979 _bfd_mips_elf_size_dynamic_sections
8980 #define elf_backend_relocate_section _bfd_mips_elf_relocate_section
8981 #define elf_backend_link_output_symbol_hook \
8982 _bfd_mips_elf_link_output_symbol_hook
8983 #define elf_backend_finish_dynamic_symbol \
8984 _bfd_mips_elf_finish_dynamic_symbol
8985 #define elf_backend_finish_dynamic_sections \
8986 _bfd_mips_elf_finish_dynamic_sections
8987 #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
8988 #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
8990 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
8991 #define elf_backend_plt_header_size 0
8993 #define bfd_elf32_bfd_is_local_label_name \
8994 mips_elf_is_local_label_name
8995 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
8996 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
8997 #define bfd_elf32_bfd_link_hash_table_create \
8998 _bfd_mips_elf_link_hash_table_create
8999 #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
9000 #define bfd_elf32_bfd_copy_private_bfd_data \
9001 _bfd_mips_elf_copy_private_bfd_data
9002 #define bfd_elf32_bfd_merge_private_bfd_data \
9003 _bfd_mips_elf_merge_private_bfd_data
9004 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
9005 #define bfd_elf32_bfd_print_private_bfd_data \
9006 _bfd_mips_elf_print_private_bfd_data
9007 #include "elf32-target.h"