1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
12 This file is part of BFD, the Binary File Descriptor library.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 /* This file handles functionality common to the different MIPS ABI's. */
34 #include "elfxx-mips.h"
37 /* Get the ECOFF swapping routines. */
39 #include "coff/symconst.h"
40 #include "coff/ecoff.h"
41 #include "coff/mips.h"
43 /* This structure is used to hold .got information when linking. It
44 is stored in the tdata field of the bfd_elf_section_data structure. */
48 /* The global symbol in the GOT with the lowest index in the dynamic
50 struct elf_link_hash_entry *global_gotsym;
51 /* The number of global .got entries. */
52 unsigned int global_gotno;
53 /* The number of local .got entries. */
54 unsigned int local_gotno;
55 /* The number of local .got entries we have used. */
56 unsigned int assigned_gotno;
59 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
60 the dynamic symbols. */
62 struct mips_elf_hash_sort_data
64 /* The symbol in the global GOT with the lowest dynamic symbol table
66 struct elf_link_hash_entry *low;
67 /* The least dynamic symbol table index corresponding to a symbol
70 /* The greatest dynamic symbol table index not corresponding to a
71 symbol without a GOT entry. */
72 long max_non_got_dynindx;
75 /* The MIPS ELF linker needs additional information for each symbol in
76 the global hash table. */
78 struct mips_elf_link_hash_entry
80 struct elf_link_hash_entry root;
82 /* External symbol information. */
85 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
87 unsigned int possibly_dynamic_relocs;
89 /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
90 a readonly section. */
91 boolean readonly_reloc;
93 /* The index of the first dynamic relocation (in the .rel.dyn
94 section) against this symbol. */
95 unsigned int min_dyn_reloc_index;
97 /* We must not create a stub for a symbol that has relocations
98 related to taking the function's address, i.e. any but
99 R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
103 /* If there is a stub that 32 bit functions should use to call this
104 16 bit function, this points to the section containing the stub. */
107 /* Whether we need the fn_stub; this is set if this symbol appears
108 in any relocs other than a 16 bit call. */
109 boolean need_fn_stub;
111 /* If there is a stub that 16 bit functions should use to call this
112 32 bit function, this points to the section containing the stub. */
115 /* This is like the call_stub field, but it is used if the function
116 being called returns a floating point value. */
117 asection *call_fp_stub;
119 /* Are we forced local? .*/
120 boolean forced_local;
123 /* MIPS ELF linker hash table. */
125 struct mips_elf_link_hash_table
127 struct elf_link_hash_table root;
129 /* We no longer use this. */
130 /* String section indices for the dynamic section symbols. */
131 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
133 /* The number of .rtproc entries. */
134 bfd_size_type procedure_count;
135 /* The size of the .compact_rel section (if SGI_COMPAT). */
136 bfd_size_type compact_rel_size;
137 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
138 entry is set to the address of __rld_obj_head as in IRIX5. */
139 boolean use_rld_obj_head;
140 /* This is the value of the __rld_map or __rld_obj_head symbol. */
142 /* This is set if we see any mips16 stub sections. */
143 boolean mips16_stubs_seen;
146 /* Structure used to pass information to mips_elf_output_extsym. */
151 struct bfd_link_info *info;
152 struct ecoff_debug_info *debug;
153 const struct ecoff_debug_swap *swap;
157 /* The names of the runtime procedure table symbols used on IRIX5. */
159 static const char * const mips_elf_dynsym_rtproc_names[] =
162 "_procedure_string_table",
163 "_procedure_table_size",
167 /* These structures are used to generate the .compact_rel section on
172 unsigned long id1; /* Always one? */
173 unsigned long num; /* Number of compact relocation entries. */
174 unsigned long id2; /* Always two? */
175 unsigned long offset; /* The file offset of the first relocation. */
176 unsigned long reserved0; /* Zero? */
177 unsigned long reserved1; /* Zero? */
186 bfd_byte reserved0[4];
187 bfd_byte reserved1[4];
188 } Elf32_External_compact_rel;
192 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
193 unsigned int rtype : 4; /* Relocation types. See below. */
194 unsigned int dist2to : 8;
195 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
196 unsigned long konst; /* KONST field. See below. */
197 unsigned long vaddr; /* VADDR to be relocated. */
202 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
203 unsigned int rtype : 4; /* Relocation types. See below. */
204 unsigned int dist2to : 8;
205 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
206 unsigned long konst; /* KONST field. See below. */
214 } Elf32_External_crinfo;
220 } Elf32_External_crinfo2;
222 /* These are the constants used to swap the bitfields in a crinfo. */
224 #define CRINFO_CTYPE (0x1)
225 #define CRINFO_CTYPE_SH (31)
226 #define CRINFO_RTYPE (0xf)
227 #define CRINFO_RTYPE_SH (27)
228 #define CRINFO_DIST2TO (0xff)
229 #define CRINFO_DIST2TO_SH (19)
230 #define CRINFO_RELVADDR (0x7ffff)
231 #define CRINFO_RELVADDR_SH (0)
233 /* A compact relocation info has long (3 words) or short (2 words)
234 formats. A short format doesn't have VADDR field and relvaddr
235 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
236 #define CRF_MIPS_LONG 1
237 #define CRF_MIPS_SHORT 0
239 /* There are 4 types of compact relocation at least. The value KONST
240 has different meaning for each type:
243 CT_MIPS_REL32 Address in data
244 CT_MIPS_WORD Address in word (XXX)
245 CT_MIPS_GPHI_LO GP - vaddr
246 CT_MIPS_JMPAD Address to jump
249 #define CRT_MIPS_REL32 0xa
250 #define CRT_MIPS_WORD 0xb
251 #define CRT_MIPS_GPHI_LO 0xc
252 #define CRT_MIPS_JMPAD 0xd
254 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
255 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
256 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
257 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
259 /* The structure of the runtime procedure descriptor created by the
260 loader for use by the static exception system. */
262 typedef struct runtime_pdr {
263 bfd_vma adr; /* memory address of start of procedure */
264 long regmask; /* save register mask */
265 long regoffset; /* save register offset */
266 long fregmask; /* save floating point register mask */
267 long fregoffset; /* save floating point register offset */
268 long frameoffset; /* frame size */
269 short framereg; /* frame pointer register */
270 short pcreg; /* offset or reg of return pc */
271 long irpss; /* index into the runtime string table */
273 struct exception_info *exception_info;/* pointer to exception array */
275 #define cbRPDR sizeof (RPDR)
276 #define rpdNil ((pRPDR) 0)
278 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
279 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
280 static void ecoff_swap_rpdr_out
281 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
282 static boolean mips_elf_create_procedure_table
283 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
284 struct ecoff_debug_info *));
285 static boolean mips_elf_check_mips16_stubs
286 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
287 static void bfd_mips_elf32_swap_gptab_in
288 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
289 static void bfd_mips_elf32_swap_gptab_out
290 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
291 static void bfd_elf32_swap_compact_rel_out
292 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
293 static void bfd_elf32_swap_crinfo_out
294 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
296 static void bfd_mips_elf_swap_msym_in
297 PARAMS ((bfd *, const Elf32_External_Msym *, Elf32_Internal_Msym *));
299 static void bfd_mips_elf_swap_msym_out
300 PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
301 static int sort_dynamic_relocs
302 PARAMS ((const void *, const void *));
303 static boolean mips_elf_output_extsym
304 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
305 static int gptab_compare PARAMS ((const void *, const void *));
306 static asection * mips_elf_got_section PARAMS ((bfd *));
307 static struct mips_got_info *mips_elf_got_info
308 PARAMS ((bfd *, asection **));
309 static bfd_vma mips_elf_local_got_index
310 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
311 static bfd_vma mips_elf_global_got_index
312 PARAMS ((bfd *, struct elf_link_hash_entry *));
313 static bfd_vma mips_elf_got_page
314 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
315 static bfd_vma mips_elf_got16_entry
316 PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, boolean));
317 static bfd_vma mips_elf_got_offset_from_index
318 PARAMS ((bfd *, bfd *, bfd_vma));
319 static bfd_vma mips_elf_create_local_got_entry
320 PARAMS ((bfd *, struct mips_got_info *, asection *, bfd_vma));
321 static boolean mips_elf_sort_hash_table
322 PARAMS ((struct bfd_link_info *, unsigned long));
323 static boolean mips_elf_sort_hash_table_f
324 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
325 static boolean mips_elf_record_global_got_symbol
326 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
327 struct mips_got_info *));
328 static const Elf_Internal_Rela *mips_elf_next_relocation
329 PARAMS ((bfd *, unsigned int, const Elf_Internal_Rela *,
330 const Elf_Internal_Rela *));
331 static boolean mips_elf_local_relocation_p
332 PARAMS ((bfd *, const Elf_Internal_Rela *, asection **, boolean));
333 static bfd_vma mips_elf_sign_extend PARAMS ((bfd_vma, int));
334 static boolean mips_elf_overflow_p PARAMS ((bfd_vma, int));
335 static bfd_vma mips_elf_high PARAMS ((bfd_vma));
336 static bfd_vma mips_elf_higher PARAMS ((bfd_vma));
337 static bfd_vma mips_elf_highest PARAMS ((bfd_vma));
338 static boolean mips_elf_create_compact_rel_section
339 PARAMS ((bfd *, struct bfd_link_info *));
340 static boolean mips_elf_create_got_section
341 PARAMS ((bfd *, struct bfd_link_info *));
342 static asection *mips_elf_create_msym_section
344 static bfd_reloc_status_type mips_elf_calculate_relocation
345 PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
346 const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
347 Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
348 boolean *, boolean));
349 static bfd_vma mips_elf_obtain_contents
350 PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
351 static boolean mips_elf_perform_relocation
352 PARAMS ((struct bfd_link_info *, reloc_howto_type *,
353 const Elf_Internal_Rela *, bfd_vma, bfd *, asection *, bfd_byte *,
355 static boolean mips_elf_stub_section_p
356 PARAMS ((bfd *, asection *));
357 static void mips_elf_allocate_dynamic_relocations
358 PARAMS ((bfd *, unsigned int));
359 static boolean mips_elf_create_dynamic_relocation
360 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
361 struct mips_elf_link_hash_entry *, asection *,
362 bfd_vma, bfd_vma *, asection *));
363 static INLINE int elf_mips_isa PARAMS ((flagword));
364 static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
365 static void mips_elf_irix6_finish_dynamic_symbol
366 PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
367 static boolean _bfd_mips_elf_mach_extends_p PARAMS ((flagword, flagword));
369 /* This will be used when we sort the dynamic relocation records. */
370 static bfd *reldyn_sorting_bfd;
372 /* Nonzero if ABFD is using the N32 ABI. */
374 #define ABI_N32_P(abfd) \
375 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
377 /* Nonzero if ABFD is using the N64 ABI. */
378 #define ABI_64_P(abfd) \
379 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
381 /* Nonzero if ABFD is using NewABI conventions. */
382 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
384 /* The IRIX compatibility level we are striving for. */
385 #define IRIX_COMPAT(abfd) \
386 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
388 /* Whether we are trying to be compatible with IRIX at all. */
389 #define SGI_COMPAT(abfd) \
390 (IRIX_COMPAT (abfd) != ict_none)
392 /* The name of the options section. */
393 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
394 (ABI_64_P (abfd) ? ".MIPS.options" : ".options")
396 /* The name of the stub section. */
397 #define MIPS_ELF_STUB_SECTION_NAME(abfd) \
398 (ABI_64_P (abfd) ? ".MIPS.stubs" : ".stub")
400 /* The size of an external REL relocation. */
401 #define MIPS_ELF_REL_SIZE(abfd) \
402 (get_elf_backend_data (abfd)->s->sizeof_rel)
404 /* The size of an external dynamic table entry. */
405 #define MIPS_ELF_DYN_SIZE(abfd) \
406 (get_elf_backend_data (abfd)->s->sizeof_dyn)
408 /* The size of a GOT entry. */
409 #define MIPS_ELF_GOT_SIZE(abfd) \
410 (get_elf_backend_data (abfd)->s->arch_size / 8)
412 /* The size of a symbol-table entry. */
413 #define MIPS_ELF_SYM_SIZE(abfd) \
414 (get_elf_backend_data (abfd)->s->sizeof_sym)
416 /* The default alignment for sections, as a power of two. */
417 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
418 (get_elf_backend_data (abfd)->s->file_align == 8 ? 3 : 2)
420 /* Get word-sized data. */
421 #define MIPS_ELF_GET_WORD(abfd, ptr) \
422 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
424 /* Put out word-sized data. */
425 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
427 ? bfd_put_64 (abfd, val, ptr) \
428 : bfd_put_32 (abfd, val, ptr))
430 /* Add a dynamic symbol table-entry. */
432 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
433 (ABI_64_P (elf_hash_table (info)->dynobj) \
434 ? bfd_elf64_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val) \
435 : bfd_elf32_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val))
437 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
438 (ABI_64_P (elf_hash_table (info)->dynobj) \
439 ? (boolean) (abort (), false) \
440 : bfd_elf32_add_dynamic_entry (info, (bfd_vma) tag, (bfd_vma) val))
443 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
444 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
446 /* Determine whether the internal relocation of index REL_IDX is REL
447 (zero) or RELA (non-zero). The assumption is that, if there are
448 two relocation sections for this section, one of them is REL and
449 the other is RELA. If the index of the relocation we're testing is
450 in range for the first relocation section, check that the external
451 relocation size is that for RELA. It is also assumed that, if
452 rel_idx is not in range for the first section, and this first
453 section contains REL relocs, then the relocation is in the second
454 section, that is RELA. */
455 #define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
456 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
457 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
458 > (bfd_vma)(rel_idx)) \
459 == (elf_section_data (sec)->rel_hdr.sh_entsize \
460 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
461 : sizeof (Elf32_External_Rela))))
463 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
464 from smaller values. Start with zero, widen, *then* decrement. */
465 #define MINUS_ONE (((bfd_vma)0) - 1)
467 /* The number of local .got entries we reserve. */
468 #define MIPS_RESERVED_GOTNO (2)
470 /* Instructions which appear in a stub. For some reason the stub is
471 slightly different on an SGI system. */
472 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
473 #define STUB_LW(abfd) \
476 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
477 : 0x8f998010) /* lw t9,0x8010(gp) */ \
478 : 0x8f998010) /* lw t9,0x8000(gp) */
479 #define STUB_MOVE(abfd) \
480 (SGI_COMPAT (abfd) ? 0x03e07825 : 0x03e07821) /* move t7,ra */
481 #define STUB_JALR 0x0320f809 /* jal t9 */
482 #define STUB_LI16(abfd) \
483 (SGI_COMPAT (abfd) ? 0x34180000 : 0x24180000) /* ori t8,zero,0 */
484 #define MIPS_FUNCTION_STUB_SIZE (16)
486 /* The name of the dynamic interpreter. This is put in the .interp
489 #define ELF_DYNAMIC_INTERPRETER(abfd) \
490 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
491 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
492 : "/usr/lib/libc.so.1")
495 #define MNAME(bfd,pre,pos) \
496 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
497 #define ELF_R_SYM(bfd, i) \
498 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
499 #define ELF_R_TYPE(bfd, i) \
500 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
501 #define ELF_R_INFO(bfd, s, t) \
502 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
504 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
505 #define ELF_R_SYM(bfd, i) \
507 #define ELF_R_TYPE(bfd, i) \
509 #define ELF_R_INFO(bfd, s, t) \
510 (ELF32_R_INFO (s, t))
513 /* The mips16 compiler uses a couple of special sections to handle
514 floating point arguments.
516 Section names that look like .mips16.fn.FNNAME contain stubs that
517 copy floating point arguments from the fp regs to the gp regs and
518 then jump to FNNAME. If any 32 bit function calls FNNAME, the
519 call should be redirected to the stub instead. If no 32 bit
520 function calls FNNAME, the stub should be discarded. We need to
521 consider any reference to the function, not just a call, because
522 if the address of the function is taken we will need the stub,
523 since the address might be passed to a 32 bit function.
525 Section names that look like .mips16.call.FNNAME contain stubs
526 that copy floating point arguments from the gp regs to the fp
527 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
528 then any 16 bit function that calls FNNAME should be redirected
529 to the stub instead. If FNNAME is not a 32 bit function, the
530 stub should be discarded.
532 .mips16.call.fp.FNNAME sections are similar, but contain stubs
533 which call FNNAME and then copy the return value from the fp regs
534 to the gp regs. These stubs store the return value in $18 while
535 calling FNNAME; any function which might call one of these stubs
536 must arrange to save $18 around the call. (This case is not
537 needed for 32 bit functions that call 16 bit functions, because
538 16 bit functions always return floating point values in both
541 Note that in all cases FNNAME might be defined statically.
542 Therefore, FNNAME is not used literally. Instead, the relocation
543 information will indicate which symbol the section is for.
545 We record any stubs that we find in the symbol table. */
547 #define FN_STUB ".mips16.fn."
548 #define CALL_STUB ".mips16.call."
549 #define CALL_FP_STUB ".mips16.call.fp."
551 /* Look up an entry in a MIPS ELF linker hash table. */
553 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
554 ((struct mips_elf_link_hash_entry *) \
555 elf_link_hash_lookup (&(table)->root, (string), (create), \
558 /* Traverse a MIPS ELF linker hash table. */
560 #define mips_elf_link_hash_traverse(table, func, info) \
561 (elf_link_hash_traverse \
563 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
566 /* Get the MIPS ELF linker hash table from a link_info structure. */
568 #define mips_elf_hash_table(p) \
569 ((struct mips_elf_link_hash_table *) ((p)->hash))
571 /* Create an entry in a MIPS ELF linker hash table. */
573 static struct bfd_hash_entry *
574 mips_elf_link_hash_newfunc (entry, table, string)
575 struct bfd_hash_entry *entry;
576 struct bfd_hash_table *table;
579 struct mips_elf_link_hash_entry *ret =
580 (struct mips_elf_link_hash_entry *) entry;
582 /* Allocate the structure if it has not already been allocated by a
584 if (ret == (struct mips_elf_link_hash_entry *) NULL)
585 ret = ((struct mips_elf_link_hash_entry *)
586 bfd_hash_allocate (table,
587 sizeof (struct mips_elf_link_hash_entry)));
588 if (ret == (struct mips_elf_link_hash_entry *) NULL)
589 return (struct bfd_hash_entry *) ret;
591 /* Call the allocation method of the superclass. */
592 ret = ((struct mips_elf_link_hash_entry *)
593 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
595 if (ret != (struct mips_elf_link_hash_entry *) NULL)
597 /* Set local fields. */
598 memset (&ret->esym, 0, sizeof (EXTR));
599 /* We use -2 as a marker to indicate that the information has
600 not been set. -1 means there is no associated ifd. */
602 ret->possibly_dynamic_relocs = 0;
603 ret->readonly_reloc = false;
604 ret->min_dyn_reloc_index = 0;
605 ret->no_fn_stub = false;
607 ret->need_fn_stub = false;
608 ret->call_stub = NULL;
609 ret->call_fp_stub = NULL;
610 ret->forced_local = false;
613 return (struct bfd_hash_entry *) ret;
616 /* Read ECOFF debugging information from a .mdebug section into a
617 ecoff_debug_info structure. */
620 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
623 struct ecoff_debug_info *debug;
626 const struct ecoff_debug_swap *swap;
627 char *ext_hdr = NULL;
629 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
630 memset (debug, 0, sizeof (*debug));
632 ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
633 if (ext_hdr == NULL && swap->external_hdr_size != 0)
636 if (! bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
637 swap->external_hdr_size))
640 symhdr = &debug->symbolic_header;
641 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
643 /* The symbolic header contains absolute file offsets and sizes to
645 #define READ(ptr, offset, count, size, type) \
646 if (symhdr->count == 0) \
650 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
651 debug->ptr = (type) bfd_malloc (amt); \
652 if (debug->ptr == NULL) \
654 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
655 || bfd_bread (debug->ptr, amt, abfd) != amt) \
659 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
660 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
661 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
662 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
663 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
664 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
666 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
667 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
668 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
669 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
670 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
674 debug->adjust = NULL;
681 if (debug->line != NULL)
683 if (debug->external_dnr != NULL)
684 free (debug->external_dnr);
685 if (debug->external_pdr != NULL)
686 free (debug->external_pdr);
687 if (debug->external_sym != NULL)
688 free (debug->external_sym);
689 if (debug->external_opt != NULL)
690 free (debug->external_opt);
691 if (debug->external_aux != NULL)
692 free (debug->external_aux);
693 if (debug->ss != NULL)
695 if (debug->ssext != NULL)
697 if (debug->external_fdr != NULL)
698 free (debug->external_fdr);
699 if (debug->external_rfd != NULL)
700 free (debug->external_rfd);
701 if (debug->external_ext != NULL)
702 free (debug->external_ext);
706 /* Swap RPDR (runtime procedure table entry) for output. */
709 ecoff_swap_rpdr_out (abfd, in, ex)
714 H_PUT_S32 (abfd, in->adr, ex->p_adr);
715 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
716 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
717 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
718 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
719 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
721 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
722 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
724 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
726 H_PUT_S32 (abfd, in->exception_info, ex->p_exception_info);
730 /* Create a runtime procedure table from the .mdebug section. */
733 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
736 struct bfd_link_info *info;
738 struct ecoff_debug_info *debug;
740 const struct ecoff_debug_swap *swap;
741 HDRR *hdr = &debug->symbolic_header;
743 struct rpdr_ext *erp;
745 struct pdr_ext *epdr;
746 struct sym_ext *esym;
751 unsigned long sindex;
755 const char *no_name_func = _("static procedure (no name)");
763 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
765 sindex = strlen (no_name_func) + 1;
769 size = swap->external_pdr_size;
771 epdr = (struct pdr_ext *) bfd_malloc (size * count);
775 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
778 size = sizeof (RPDR);
779 rp = rpdr = (RPDR *) bfd_malloc (size * count);
783 size = sizeof (char *);
784 sv = (char **) bfd_malloc (size * count);
788 count = hdr->isymMax;
789 size = swap->external_sym_size;
790 esym = (struct sym_ext *) bfd_malloc (size * count);
794 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
798 ss = (char *) bfd_malloc (count);
801 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
805 for (i = 0; i < (unsigned long) count; i++, rp++)
807 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
808 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
810 rp->regmask = pdr.regmask;
811 rp->regoffset = pdr.regoffset;
812 rp->fregmask = pdr.fregmask;
813 rp->fregoffset = pdr.fregoffset;
814 rp->frameoffset = pdr.frameoffset;
815 rp->framereg = pdr.framereg;
816 rp->pcreg = pdr.pcreg;
818 sv[i] = ss + sym.iss;
819 sindex += strlen (sv[i]) + 1;
823 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
824 size = BFD_ALIGN (size, 16);
825 rtproc = (PTR) bfd_alloc (abfd, size);
828 mips_elf_hash_table (info)->procedure_count = 0;
832 mips_elf_hash_table (info)->procedure_count = count + 2;
834 erp = (struct rpdr_ext *) rtproc;
835 memset (erp, 0, sizeof (struct rpdr_ext));
837 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
838 strcpy (str, no_name_func);
839 str += strlen (no_name_func) + 1;
840 for (i = 0; i < count; i++)
842 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
844 str += strlen (sv[i]) + 1;
846 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
848 /* Set the size and contents of .rtproc section. */
850 s->contents = (bfd_byte *) rtproc;
852 /* Skip this section later on (I don't think this currently
853 matters, but someday it might). */
854 s->link_order_head = (struct bfd_link_order *) NULL;
883 /* Check the mips16 stubs for a particular symbol, and see if we can
887 mips_elf_check_mips16_stubs (h, data)
888 struct mips_elf_link_hash_entry *h;
889 PTR data ATTRIBUTE_UNUSED;
891 if (h->root.root.type == bfd_link_hash_warning)
892 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
894 if (h->fn_stub != NULL
895 && ! h->need_fn_stub)
897 /* We don't need the fn_stub; the only references to this symbol
898 are 16 bit calls. Clobber the size to 0 to prevent it from
899 being included in the link. */
900 h->fn_stub->_raw_size = 0;
901 h->fn_stub->_cooked_size = 0;
902 h->fn_stub->flags &= ~SEC_RELOC;
903 h->fn_stub->reloc_count = 0;
904 h->fn_stub->flags |= SEC_EXCLUDE;
907 if (h->call_stub != NULL
908 && h->root.other == STO_MIPS16)
910 /* We don't need the call_stub; this is a 16 bit function, so
911 calls from other 16 bit functions are OK. Clobber the size
912 to 0 to prevent it from being included in the link. */
913 h->call_stub->_raw_size = 0;
914 h->call_stub->_cooked_size = 0;
915 h->call_stub->flags &= ~SEC_RELOC;
916 h->call_stub->reloc_count = 0;
917 h->call_stub->flags |= SEC_EXCLUDE;
920 if (h->call_fp_stub != NULL
921 && h->root.other == STO_MIPS16)
923 /* We don't need the call_stub; this is a 16 bit function, so
924 calls from other 16 bit functions are OK. Clobber the size
925 to 0 to prevent it from being included in the link. */
926 h->call_fp_stub->_raw_size = 0;
927 h->call_fp_stub->_cooked_size = 0;
928 h->call_fp_stub->flags &= ~SEC_RELOC;
929 h->call_fp_stub->reloc_count = 0;
930 h->call_fp_stub->flags |= SEC_EXCLUDE;
936 bfd_reloc_status_type
937 _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
938 relocateable, data, gp)
941 arelent *reloc_entry;
942 asection *input_section;
943 boolean relocateable;
951 if (bfd_is_com_section (symbol->section))
954 relocation = symbol->value;
956 relocation += symbol->section->output_section->vma;
957 relocation += symbol->section->output_offset;
959 if (reloc_entry->address > input_section->_cooked_size)
960 return bfd_reloc_outofrange;
962 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
964 /* Set val to the offset into the section or symbol. */
965 if (reloc_entry->howto->src_mask == 0)
967 /* This case occurs with the 64-bit MIPS ELF ABI. */
968 val = reloc_entry->addend;
972 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
977 /* Adjust val for the final section location and GP value. If we
978 are producing relocateable output, we don't want to do this for
979 an external symbol. */
981 || (symbol->flags & BSF_SECTION_SYM) != 0)
982 val += relocation - gp;
984 insn = (insn & ~0xffff) | (val & 0xffff);
985 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
988 reloc_entry->address += input_section->output_offset;
990 else if ((long) val >= 0x8000 || (long) val < -0x8000)
991 return bfd_reloc_overflow;
996 /* Swap an entry in a .gptab section. Note that these routines rely
997 on the equivalence of the two elements of the union. */
1000 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1002 const Elf32_External_gptab *ex;
1005 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1006 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1010 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1012 const Elf32_gptab *in;
1013 Elf32_External_gptab *ex;
1015 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1016 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1020 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1022 const Elf32_compact_rel *in;
1023 Elf32_External_compact_rel *ex;
1025 H_PUT_32 (abfd, in->id1, ex->id1);
1026 H_PUT_32 (abfd, in->num, ex->num);
1027 H_PUT_32 (abfd, in->id2, ex->id2);
1028 H_PUT_32 (abfd, in->offset, ex->offset);
1029 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1030 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1034 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1036 const Elf32_crinfo *in;
1037 Elf32_External_crinfo *ex;
1041 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1042 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1043 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1044 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1045 H_PUT_32 (abfd, l, ex->info);
1046 H_PUT_32 (abfd, in->konst, ex->konst);
1047 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1051 /* Swap in an MSYM entry. */
1054 bfd_mips_elf_swap_msym_in (abfd, ex, in)
1056 const Elf32_External_Msym *ex;
1057 Elf32_Internal_Msym *in;
1059 in->ms_hash_value = H_GET_32 (abfd, ex->ms_hash_value);
1060 in->ms_info = H_GET_32 (abfd, ex->ms_info);
1063 /* Swap out an MSYM entry. */
1066 bfd_mips_elf_swap_msym_out (abfd, in, ex)
1068 const Elf32_Internal_Msym *in;
1069 Elf32_External_Msym *ex;
1071 H_PUT_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
1072 H_PUT_32 (abfd, in->ms_info, ex->ms_info);
1075 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1076 routines swap this structure in and out. They are used outside of
1077 BFD, so they are globally visible. */
1080 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1082 const Elf32_External_RegInfo *ex;
1085 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1086 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1087 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1088 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1089 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1090 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1094 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1096 const Elf32_RegInfo *in;
1097 Elf32_External_RegInfo *ex;
1099 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1100 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1101 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1102 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1103 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1104 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1107 /* In the 64 bit ABI, the .MIPS.options section holds register
1108 information in an Elf64_Reginfo structure. These routines swap
1109 them in and out. They are globally visible because they are used
1110 outside of BFD. These routines are here so that gas can call them
1111 without worrying about whether the 64 bit ABI has been included. */
1114 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1116 const Elf64_External_RegInfo *ex;
1117 Elf64_Internal_RegInfo *in;
1119 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1120 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1121 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1122 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1123 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1124 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1125 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1129 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1131 const Elf64_Internal_RegInfo *in;
1132 Elf64_External_RegInfo *ex;
1134 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1135 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1136 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1137 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1138 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1139 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1140 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1143 /* Swap in an options header. */
1146 bfd_mips_elf_swap_options_in (abfd, ex, in)
1148 const Elf_External_Options *ex;
1149 Elf_Internal_Options *in;
1151 in->kind = H_GET_8 (abfd, ex->kind);
1152 in->size = H_GET_8 (abfd, ex->size);
1153 in->section = H_GET_16 (abfd, ex->section);
1154 in->info = H_GET_32 (abfd, ex->info);
1157 /* Swap out an options header. */
1160 bfd_mips_elf_swap_options_out (abfd, in, ex)
1162 const Elf_Internal_Options *in;
1163 Elf_External_Options *ex;
1165 H_PUT_8 (abfd, in->kind, ex->kind);
1166 H_PUT_8 (abfd, in->size, ex->size);
1167 H_PUT_16 (abfd, in->section, ex->section);
1168 H_PUT_32 (abfd, in->info, ex->info);
1171 /* This function is called via qsort() to sort the dynamic relocation
1172 entries by increasing r_symndx value. */
1175 sort_dynamic_relocs (arg1, arg2)
1179 const Elf32_External_Rel *ext_reloc1 = (const Elf32_External_Rel *) arg1;
1180 const Elf32_External_Rel *ext_reloc2 = (const Elf32_External_Rel *) arg2;
1182 Elf_Internal_Rel int_reloc1;
1183 Elf_Internal_Rel int_reloc2;
1185 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, ext_reloc1, &int_reloc1);
1186 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, ext_reloc2, &int_reloc2);
1188 return (ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info));
1191 /* This routine is used to write out ECOFF debugging external symbol
1192 information. It is called via mips_elf_link_hash_traverse. The
1193 ECOFF external symbol information must match the ELF external
1194 symbol information. Unfortunately, at this point we don't know
1195 whether a symbol is required by reloc information, so the two
1196 tables may wind up being different. We must sort out the external
1197 symbol information before we can set the final size of the .mdebug
1198 section, and we must set the size of the .mdebug section before we
1199 can relocate any sections, and we can't know which symbols are
1200 required by relocation until we relocate the sections.
1201 Fortunately, it is relatively unlikely that any symbol will be
1202 stripped but required by a reloc. In particular, it can not happen
1203 when generating a final executable. */
1206 mips_elf_output_extsym (h, data)
1207 struct mips_elf_link_hash_entry *h;
1210 struct extsym_info *einfo = (struct extsym_info *) data;
1212 asection *sec, *output_section;
1214 if (h->root.root.type == bfd_link_hash_warning)
1215 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1217 if (h->root.indx == -2)
1219 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1220 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
1221 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1222 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
1224 else if (einfo->info->strip == strip_all
1225 || (einfo->info->strip == strip_some
1226 && bfd_hash_lookup (einfo->info->keep_hash,
1227 h->root.root.root.string,
1228 false, false) == NULL))
1236 if (h->esym.ifd == -2)
1239 h->esym.cobol_main = 0;
1240 h->esym.weakext = 0;
1241 h->esym.reserved = 0;
1242 h->esym.ifd = ifdNil;
1243 h->esym.asym.value = 0;
1244 h->esym.asym.st = stGlobal;
1246 if (h->root.root.type == bfd_link_hash_undefined
1247 || h->root.root.type == bfd_link_hash_undefweak)
1251 /* Use undefined class. Also, set class and type for some
1253 name = h->root.root.root.string;
1254 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
1255 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
1257 h->esym.asym.sc = scData;
1258 h->esym.asym.st = stLabel;
1259 h->esym.asym.value = 0;
1261 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
1263 h->esym.asym.sc = scAbs;
1264 h->esym.asym.st = stLabel;
1265 h->esym.asym.value =
1266 mips_elf_hash_table (einfo->info)->procedure_count;
1268 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
1270 h->esym.asym.sc = scAbs;
1271 h->esym.asym.st = stLabel;
1272 h->esym.asym.value = elf_gp (einfo->abfd);
1275 h->esym.asym.sc = scUndefined;
1277 else if (h->root.root.type != bfd_link_hash_defined
1278 && h->root.root.type != bfd_link_hash_defweak)
1279 h->esym.asym.sc = scAbs;
1284 sec = h->root.root.u.def.section;
1285 output_section = sec->output_section;
1287 /* When making a shared library and symbol h is the one from
1288 the another shared library, OUTPUT_SECTION may be null. */
1289 if (output_section == NULL)
1290 h->esym.asym.sc = scUndefined;
1293 name = bfd_section_name (output_section->owner, output_section);
1295 if (strcmp (name, ".text") == 0)
1296 h->esym.asym.sc = scText;
1297 else if (strcmp (name, ".data") == 0)
1298 h->esym.asym.sc = scData;
1299 else if (strcmp (name, ".sdata") == 0)
1300 h->esym.asym.sc = scSData;
1301 else if (strcmp (name, ".rodata") == 0
1302 || strcmp (name, ".rdata") == 0)
1303 h->esym.asym.sc = scRData;
1304 else if (strcmp (name, ".bss") == 0)
1305 h->esym.asym.sc = scBss;
1306 else if (strcmp (name, ".sbss") == 0)
1307 h->esym.asym.sc = scSBss;
1308 else if (strcmp (name, ".init") == 0)
1309 h->esym.asym.sc = scInit;
1310 else if (strcmp (name, ".fini") == 0)
1311 h->esym.asym.sc = scFini;
1313 h->esym.asym.sc = scAbs;
1317 h->esym.asym.reserved = 0;
1318 h->esym.asym.index = indexNil;
1321 if (h->root.root.type == bfd_link_hash_common)
1322 h->esym.asym.value = h->root.root.u.c.size;
1323 else if (h->root.root.type == bfd_link_hash_defined
1324 || h->root.root.type == bfd_link_hash_defweak)
1326 if (h->esym.asym.sc == scCommon)
1327 h->esym.asym.sc = scBss;
1328 else if (h->esym.asym.sc == scSCommon)
1329 h->esym.asym.sc = scSBss;
1331 sec = h->root.root.u.def.section;
1332 output_section = sec->output_section;
1333 if (output_section != NULL)
1334 h->esym.asym.value = (h->root.root.u.def.value
1335 + sec->output_offset
1336 + output_section->vma);
1338 h->esym.asym.value = 0;
1340 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1342 struct mips_elf_link_hash_entry *hd = h;
1343 boolean no_fn_stub = h->no_fn_stub;
1345 while (hd->root.root.type == bfd_link_hash_indirect)
1347 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
1348 no_fn_stub = no_fn_stub || hd->no_fn_stub;
1353 /* Set type and value for a symbol with a function stub. */
1354 h->esym.asym.st = stProc;
1355 sec = hd->root.root.u.def.section;
1357 h->esym.asym.value = 0;
1360 output_section = sec->output_section;
1361 if (output_section != NULL)
1362 h->esym.asym.value = (hd->root.plt.offset
1363 + sec->output_offset
1364 + output_section->vma);
1366 h->esym.asym.value = 0;
1374 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1375 h->root.root.root.string,
1378 einfo->failed = true;
1385 /* A comparison routine used to sort .gptab entries. */
1388 gptab_compare (p1, p2)
1392 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
1393 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
1395 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1398 /* Returns the GOT section for ABFD. */
1401 mips_elf_got_section (abfd)
1404 return bfd_get_section_by_name (abfd, ".got");
1407 /* Returns the GOT information associated with the link indicated by
1408 INFO. If SGOTP is non-NULL, it is filled in with the GOT
1411 static struct mips_got_info *
1412 mips_elf_got_info (abfd, sgotp)
1417 struct mips_got_info *g;
1419 sgot = mips_elf_got_section (abfd);
1420 BFD_ASSERT (sgot != NULL);
1421 BFD_ASSERT (elf_section_data (sgot) != NULL);
1422 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
1423 BFD_ASSERT (g != NULL);
1430 /* Returns the GOT offset at which the indicated address can be found.
1431 If there is not yet a GOT entry for this value, create one. Returns
1432 -1 if no satisfactory GOT offset can be found. */
1435 mips_elf_local_got_index (abfd, info, value)
1437 struct bfd_link_info *info;
1441 struct mips_got_info *g;
1444 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
1446 /* Look to see if we already have an appropriate entry. */
1447 for (entry = (sgot->contents
1448 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
1449 entry != sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
1450 entry += MIPS_ELF_GOT_SIZE (abfd))
1452 bfd_vma address = MIPS_ELF_GET_WORD (abfd, entry);
1453 if (address == value)
1454 return entry - sgot->contents;
1457 return mips_elf_create_local_got_entry (abfd, g, sgot, value);
1460 /* Returns the GOT index for the global symbol indicated by H. */
1463 mips_elf_global_got_index (abfd, h)
1465 struct elf_link_hash_entry *h;
1469 struct mips_got_info *g;
1470 long global_got_dynindx = 0;
1472 g = mips_elf_got_info (abfd, &sgot);
1473 if (g->global_gotsym != NULL)
1474 global_got_dynindx = g->global_gotsym->dynindx;
1476 /* Once we determine the global GOT entry with the lowest dynamic
1477 symbol table index, we must put all dynamic symbols with greater
1478 indices into the GOT. That makes it easy to calculate the GOT
1480 BFD_ASSERT (h->dynindx >= global_got_dynindx);
1481 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
1482 * MIPS_ELF_GOT_SIZE (abfd));
1483 BFD_ASSERT (index < sgot->_raw_size);
1488 /* Find a GOT entry that is within 32KB of the VALUE. These entries
1489 are supposed to be placed at small offsets in the GOT, i.e.,
1490 within 32KB of GP. Return the index into the GOT for this page,
1491 and store the offset from this entry to the desired address in
1492 OFFSETP, if it is non-NULL. */
1495 mips_elf_got_page (abfd, info, value, offsetp)
1497 struct bfd_link_info *info;
1502 struct mips_got_info *g;
1504 bfd_byte *last_entry;
1508 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
1510 /* Look to see if we already have an appropriate entry. */
1511 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
1512 for (entry = (sgot->contents
1513 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
1514 entry != last_entry;
1515 entry += MIPS_ELF_GOT_SIZE (abfd))
1517 address = MIPS_ELF_GET_WORD (abfd, entry);
1519 if (!mips_elf_overflow_p (value - address, 16))
1521 /* This entry will serve as the page pointer. We can add a
1522 16-bit number to it to get the actual address. */
1523 index = entry - sgot->contents;
1528 /* If we didn't have an appropriate entry, we create one now. */
1529 if (entry == last_entry)
1530 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
1534 address = MIPS_ELF_GET_WORD (abfd, entry);
1535 *offsetp = value - address;
1541 /* Find a GOT entry whose higher-order 16 bits are the same as those
1542 for value. Return the index into the GOT for this entry. */
1545 mips_elf_got16_entry (abfd, info, value, external)
1547 struct bfd_link_info *info;
1552 struct mips_got_info *g;
1554 bfd_byte *last_entry;
1560 /* Although the ABI says that it is "the high-order 16 bits" that we
1561 want, it is really the %high value. The complete value is
1562 calculated with a `addiu' of a LO16 relocation, just as with a
1564 value = mips_elf_high (value) << 16;
1567 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
1569 /* Look to see if we already have an appropriate entry. */
1570 last_entry = sgot->contents + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno;
1571 for (entry = (sgot->contents
1572 + MIPS_ELF_GOT_SIZE (abfd) * MIPS_RESERVED_GOTNO);
1573 entry != last_entry;
1574 entry += MIPS_ELF_GOT_SIZE (abfd))
1576 address = MIPS_ELF_GET_WORD (abfd, entry);
1577 if (address == value)
1579 /* This entry has the right high-order 16 bits, and the low-order
1580 16 bits are set to zero. */
1581 index = entry - sgot->contents;
1586 /* If we didn't have an appropriate entry, we create one now. */
1587 if (entry == last_entry)
1588 index = mips_elf_create_local_got_entry (abfd, g, sgot, value);
1593 /* Returns the offset for the entry at the INDEXth position
1597 mips_elf_got_offset_from_index (dynobj, output_bfd, index)
1605 sgot = mips_elf_got_section (dynobj);
1606 gp = _bfd_get_gp_value (output_bfd);
1607 return (sgot->output_section->vma + sgot->output_offset + index -
1611 /* Create a local GOT entry for VALUE. Return the index of the entry,
1612 or -1 if it could not be created. */
1615 mips_elf_create_local_got_entry (abfd, g, sgot, value)
1617 struct mips_got_info *g;
1621 if (g->assigned_gotno >= g->local_gotno)
1623 /* We didn't allocate enough space in the GOT. */
1624 (*_bfd_error_handler)
1625 (_("not enough GOT space for local GOT entries"));
1626 bfd_set_error (bfd_error_bad_value);
1627 return (bfd_vma) -1;
1630 MIPS_ELF_PUT_WORD (abfd, value,
1632 + MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno));
1633 return MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
1636 /* Sort the dynamic symbol table so that symbols that need GOT entries
1637 appear towards the end. This reduces the amount of GOT space
1638 required. MAX_LOCAL is used to set the number of local symbols
1639 known to be in the dynamic symbol table. During
1640 _bfd_mips_elf_size_dynamic_sections, this value is 1. Afterward, the
1641 section symbols are added and the count is higher. */
1644 mips_elf_sort_hash_table (info, max_local)
1645 struct bfd_link_info *info;
1646 unsigned long max_local;
1648 struct mips_elf_hash_sort_data hsd;
1649 struct mips_got_info *g;
1652 dynobj = elf_hash_table (info)->dynobj;
1655 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
1656 hsd.max_non_got_dynindx = max_local;
1657 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
1658 elf_hash_table (info)),
1659 mips_elf_sort_hash_table_f,
1662 /* There should have been enough room in the symbol table to
1663 accommodate both the GOT and non-GOT symbols. */
1664 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
1666 /* Now we know which dynamic symbol has the lowest dynamic symbol
1667 table index in the GOT. */
1668 g = mips_elf_got_info (dynobj, NULL);
1669 g->global_gotsym = hsd.low;
1674 /* If H needs a GOT entry, assign it the highest available dynamic
1675 index. Otherwise, assign it the lowest available dynamic
1679 mips_elf_sort_hash_table_f (h, data)
1680 struct mips_elf_link_hash_entry *h;
1683 struct mips_elf_hash_sort_data *hsd
1684 = (struct mips_elf_hash_sort_data *) data;
1686 if (h->root.root.type == bfd_link_hash_warning)
1687 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1689 /* Symbols without dynamic symbol table entries aren't interesting
1691 if (h->root.dynindx == -1)
1694 if (h->root.got.offset != 1)
1695 h->root.dynindx = hsd->max_non_got_dynindx++;
1698 h->root.dynindx = --hsd->min_got_dynindx;
1699 hsd->low = (struct elf_link_hash_entry *) h;
1705 /* If H is a symbol that needs a global GOT entry, but has a dynamic
1706 symbol table index lower than any we've seen to date, record it for
1710 mips_elf_record_global_got_symbol (h, info, g)
1711 struct elf_link_hash_entry *h;
1712 struct bfd_link_info *info;
1713 struct mips_got_info *g ATTRIBUTE_UNUSED;
1715 /* A global symbol in the GOT must also be in the dynamic symbol
1717 if (h->dynindx == -1)
1719 switch (ELF_ST_VISIBILITY (h->other))
1723 _bfd_mips_elf_hide_symbol (info, h, true);
1726 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
1730 /* If we've already marked this entry as needing GOT space, we don't
1731 need to do it again. */
1732 if (h->got.offset != MINUS_ONE)
1735 /* By setting this to a value other than -1, we are indicating that
1736 there needs to be a GOT entry for H. Avoid using zero, as the
1737 generic ELF copy_indirect_symbol tests for <= 0. */
1743 /* Returns the first relocation of type r_type found, beginning with
1744 RELOCATION. RELEND is one-past-the-end of the relocation table. */
1746 static const Elf_Internal_Rela *
1747 mips_elf_next_relocation (abfd, r_type, relocation, relend)
1748 bfd *abfd ATTRIBUTE_UNUSED;
1749 unsigned int r_type;
1750 const Elf_Internal_Rela *relocation;
1751 const Elf_Internal_Rela *relend;
1753 /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
1754 immediately following. However, for the IRIX6 ABI, the next
1755 relocation may be a composed relocation consisting of several
1756 relocations for the same address. In that case, the R_MIPS_LO16
1757 relocation may occur as one of these. We permit a similar
1758 extension in general, as that is useful for GCC. */
1759 while (relocation < relend)
1761 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type)
1767 /* We didn't find it. */
1768 bfd_set_error (bfd_error_bad_value);
1772 /* Return whether a relocation is against a local symbol. */
1775 mips_elf_local_relocation_p (input_bfd, relocation, local_sections,
1778 const Elf_Internal_Rela *relocation;
1779 asection **local_sections;
1780 boolean check_forced;
1782 unsigned long r_symndx;
1783 Elf_Internal_Shdr *symtab_hdr;
1784 struct mips_elf_link_hash_entry *h;
1787 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
1788 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1789 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
1791 if (r_symndx < extsymoff)
1793 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
1798 /* Look up the hash table to check whether the symbol
1799 was forced local. */
1800 h = (struct mips_elf_link_hash_entry *)
1801 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
1802 /* Find the real hash-table entry for this symbol. */
1803 while (h->root.root.type == bfd_link_hash_indirect
1804 || h->root.root.type == bfd_link_hash_warning)
1805 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1806 if ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1813 /* Sign-extend VALUE, which has the indicated number of BITS. */
1816 mips_elf_sign_extend (value, bits)
1820 if (value & ((bfd_vma) 1 << (bits - 1)))
1821 /* VALUE is negative. */
1822 value |= ((bfd_vma) - 1) << bits;
1827 /* Return non-zero if the indicated VALUE has overflowed the maximum
1828 range expressable by a signed number with the indicated number of
1832 mips_elf_overflow_p (value, bits)
1836 bfd_signed_vma svalue = (bfd_signed_vma) value;
1838 if (svalue > (1 << (bits - 1)) - 1)
1839 /* The value is too big. */
1841 else if (svalue < -(1 << (bits - 1)))
1842 /* The value is too small. */
1849 /* Calculate the %high function. */
1852 mips_elf_high (value)
1855 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
1858 /* Calculate the %higher function. */
1861 mips_elf_higher (value)
1862 bfd_vma value ATTRIBUTE_UNUSED;
1865 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
1868 return (bfd_vma) -1;
1872 /* Calculate the %highest function. */
1875 mips_elf_highest (value)
1876 bfd_vma value ATTRIBUTE_UNUSED;
1879 return ((value + (bfd_vma) 0x800080008000) >> 48) & 0xffff;
1882 return (bfd_vma) -1;
1886 /* Create the .compact_rel section. */
1889 mips_elf_create_compact_rel_section (abfd, info)
1891 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1894 register asection *s;
1896 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
1898 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
1901 s = bfd_make_section (abfd, ".compact_rel");
1903 || ! bfd_set_section_flags (abfd, s, flags)
1904 || ! bfd_set_section_alignment (abfd, s,
1905 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
1908 s->_raw_size = sizeof (Elf32_External_compact_rel);
1914 /* Create the .got section to hold the global offset table. */
1917 mips_elf_create_got_section (abfd, info)
1919 struct bfd_link_info *info;
1922 register asection *s;
1923 struct elf_link_hash_entry *h;
1924 struct bfd_link_hash_entry *bh;
1925 struct mips_got_info *g;
1928 /* This function may be called more than once. */
1929 if (mips_elf_got_section (abfd))
1932 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1933 | SEC_LINKER_CREATED);
1935 s = bfd_make_section (abfd, ".got");
1937 || ! bfd_set_section_flags (abfd, s, flags)
1938 || ! bfd_set_section_alignment (abfd, s, 4))
1941 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
1942 linker script because we don't want to define the symbol if we
1943 are not creating a global offset table. */
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
1947 (bfd_vma) 0, (const char *) NULL, false,
1948 get_elf_backend_data (abfd)->collect, &bh)))
1951 h = (struct elf_link_hash_entry *) bh;
1952 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
1953 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
1954 h->type = STT_OBJECT;
1957 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
1960 /* The first several global offset table entries are reserved. */
1961 s->_raw_size = MIPS_RESERVED_GOTNO * MIPS_ELF_GOT_SIZE (abfd);
1963 amt = sizeof (struct mips_got_info);
1964 g = (struct mips_got_info *) bfd_alloc (abfd, amt);
1967 g->global_gotsym = NULL;
1968 g->local_gotno = MIPS_RESERVED_GOTNO;
1969 g->assigned_gotno = MIPS_RESERVED_GOTNO;
1970 if (elf_section_data (s) == NULL)
1972 amt = sizeof (struct bfd_elf_section_data);
1973 s->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1974 if (elf_section_data (s) == NULL)
1977 elf_section_data (s)->tdata = (PTR) g;
1978 elf_section_data (s)->this_hdr.sh_flags
1979 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1984 /* Returns the .msym section for ABFD, creating it if it does not
1985 already exist. Returns NULL to indicate error. */
1988 mips_elf_create_msym_section (abfd)
1993 s = bfd_get_section_by_name (abfd, ".msym");
1996 s = bfd_make_section (abfd, ".msym");
1998 || !bfd_set_section_flags (abfd, s,
2002 | SEC_LINKER_CREATED
2004 || !bfd_set_section_alignment (abfd, s,
2005 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
2012 /* Calculate the value produced by the RELOCATION (which comes from
2013 the INPUT_BFD). The ADDEND is the addend to use for this
2014 RELOCATION; RELOCATION->R_ADDEND is ignored.
2016 The result of the relocation calculation is stored in VALUEP.
2017 REQUIRE_JALXP indicates whether or not the opcode used with this
2018 relocation must be JALX.
2020 This function returns bfd_reloc_continue if the caller need take no
2021 further action regarding this relocation, bfd_reloc_notsupported if
2022 something goes dramatically wrong, bfd_reloc_overflow if an
2023 overflow occurs, and bfd_reloc_ok to indicate success. */
2025 static bfd_reloc_status_type
2026 mips_elf_calculate_relocation (abfd, input_bfd, input_section, info,
2027 relocation, addend, howto, local_syms,
2028 local_sections, valuep, namep,
2029 require_jalxp, save_addend)
2032 asection *input_section;
2033 struct bfd_link_info *info;
2034 const Elf_Internal_Rela *relocation;
2036 reloc_howto_type *howto;
2037 Elf_Internal_Sym *local_syms;
2038 asection **local_sections;
2041 boolean *require_jalxp;
2042 boolean save_addend;
2044 /* The eventual value we will return. */
2046 /* The address of the symbol against which the relocation is
2049 /* The final GP value to be used for the relocatable, executable, or
2050 shared object file being produced. */
2051 bfd_vma gp = MINUS_ONE;
2052 /* The place (section offset or address) of the storage unit being
2055 /* The value of GP used to create the relocatable object. */
2056 bfd_vma gp0 = MINUS_ONE;
2057 /* The offset into the global offset table at which the address of
2058 the relocation entry symbol, adjusted by the addend, resides
2059 during execution. */
2060 bfd_vma g = MINUS_ONE;
2061 /* The section in which the symbol referenced by the relocation is
2063 asection *sec = NULL;
2064 struct mips_elf_link_hash_entry *h = NULL;
2065 /* True if the symbol referred to by this relocation is a local
2067 boolean local_p, was_local_p;
2068 /* True if the symbol referred to by this relocation is "_gp_disp". */
2069 boolean gp_disp_p = false;
2070 Elf_Internal_Shdr *symtab_hdr;
2072 unsigned long r_symndx;
2074 /* True if overflow occurred during the calculation of the
2075 relocation value. */
2076 boolean overflowed_p;
2077 /* True if this relocation refers to a MIPS16 function. */
2078 boolean target_is_16_bit_code_p = false;
2080 /* Parse the relocation. */
2081 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
2082 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
2083 p = (input_section->output_section->vma
2084 + input_section->output_offset
2085 + relocation->r_offset);
2087 /* Assume that there will be no overflow. */
2088 overflowed_p = false;
2090 /* Figure out whether or not the symbol is local, and get the offset
2091 used in the array of hash table entries. */
2092 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2093 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
2094 local_sections, false);
2095 was_local_p = local_p;
2096 if (! elf_bad_symtab (input_bfd))
2097 extsymoff = symtab_hdr->sh_info;
2100 /* The symbol table does not follow the rule that local symbols
2101 must come before globals. */
2105 /* Figure out the value of the symbol. */
2108 Elf_Internal_Sym *sym;
2110 sym = local_syms + r_symndx;
2111 sec = local_sections[r_symndx];
2113 symbol = sec->output_section->vma + sec->output_offset;
2114 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
2115 || (sec->flags & SEC_MERGE))
2116 symbol += sym->st_value;
2117 if ((sec->flags & SEC_MERGE)
2118 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2120 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
2122 addend += sec->output_section->vma + sec->output_offset;
2125 /* MIPS16 text labels should be treated as odd. */
2126 if (sym->st_other == STO_MIPS16)
2129 /* Record the name of this symbol, for our caller. */
2130 *namep = bfd_elf_string_from_elf_section (input_bfd,
2131 symtab_hdr->sh_link,
2134 *namep = bfd_section_name (input_bfd, sec);
2136 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
2140 /* For global symbols we look up the symbol in the hash-table. */
2141 h = ((struct mips_elf_link_hash_entry *)
2142 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
2143 /* Find the real hash-table entry for this symbol. */
2144 while (h->root.root.type == bfd_link_hash_indirect
2145 || h->root.root.type == bfd_link_hash_warning)
2146 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2148 /* Record the name of this symbol, for our caller. */
2149 *namep = h->root.root.root.string;
2151 /* See if this is the special _gp_disp symbol. Note that such a
2152 symbol must always be a global symbol. */
2153 if (strcmp (h->root.root.root.string, "_gp_disp") == 0
2154 && ! NEWABI_P (input_bfd))
2156 /* Relocations against _gp_disp are permitted only with
2157 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
2158 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16)
2159 return bfd_reloc_notsupported;
2163 /* If this symbol is defined, calculate its address. Note that
2164 _gp_disp is a magic symbol, always implicitly defined by the
2165 linker, so it's inappropriate to check to see whether or not
2167 else if ((h->root.root.type == bfd_link_hash_defined
2168 || h->root.root.type == bfd_link_hash_defweak)
2169 && h->root.root.u.def.section)
2171 sec = h->root.root.u.def.section;
2172 if (sec->output_section)
2173 symbol = (h->root.root.u.def.value
2174 + sec->output_section->vma
2175 + sec->output_offset);
2177 symbol = h->root.root.u.def.value;
2179 else if (h->root.root.type == bfd_link_hash_undefweak)
2180 /* We allow relocations against undefined weak symbols, giving
2181 it the value zero, so that you can undefined weak functions
2182 and check to see if they exist by looking at their
2185 else if (info->shared
2186 && (!info->symbolic || info->allow_shlib_undefined)
2187 && !info->no_undefined
2188 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
2190 else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0 ||
2191 strcmp (h->root.root.root.string, "_DYNAMIC_LINKING") == 0)
2193 /* If this is a dynamic link, we should have created a
2194 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
2195 in in _bfd_mips_elf_create_dynamic_sections.
2196 Otherwise, we should define the symbol with a value of 0.
2197 FIXME: It should probably get into the symbol table
2199 BFD_ASSERT (! info->shared);
2200 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
2205 if (! ((*info->callbacks->undefined_symbol)
2206 (info, h->root.root.root.string, input_bfd,
2207 input_section, relocation->r_offset,
2208 (!info->shared || info->no_undefined
2209 || ELF_ST_VISIBILITY (h->root.other)))))
2210 return bfd_reloc_undefined;
2214 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
2217 /* If this is a 32- or 64-bit call to a 16-bit function with a stub, we
2218 need to redirect the call to the stub, unless we're already *in*
2220 if (r_type != R_MIPS16_26 && !info->relocateable
2221 && ((h != NULL && h->fn_stub != NULL)
2222 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
2223 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
2224 && !mips_elf_stub_section_p (input_bfd, input_section))
2226 /* This is a 32- or 64-bit call to a 16-bit function. We should
2227 have already noticed that we were going to need the
2230 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
2233 BFD_ASSERT (h->need_fn_stub);
2237 symbol = sec->output_section->vma + sec->output_offset;
2239 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
2240 need to redirect the call to the stub. */
2241 else if (r_type == R_MIPS16_26 && !info->relocateable
2243 && (h->call_stub != NULL || h->call_fp_stub != NULL)
2244 && !target_is_16_bit_code_p)
2246 /* If both call_stub and call_fp_stub are defined, we can figure
2247 out which one to use by seeing which one appears in the input
2249 if (h->call_stub != NULL && h->call_fp_stub != NULL)
2254 for (o = input_bfd->sections; o != NULL; o = o->next)
2256 if (strncmp (bfd_get_section_name (input_bfd, o),
2257 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
2259 sec = h->call_fp_stub;
2266 else if (h->call_stub != NULL)
2269 sec = h->call_fp_stub;
2271 BFD_ASSERT (sec->_raw_size > 0);
2272 symbol = sec->output_section->vma + sec->output_offset;
2275 /* Calls from 16-bit code to 32-bit code and vice versa require the
2276 special jalx instruction. */
2277 *require_jalxp = (!info->relocateable
2278 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
2279 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
2281 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
2282 local_sections, true);
2284 /* If we haven't already determined the GOT offset, or the GP value,
2285 and we're going to need it, get it now. */
2290 case R_MIPS_GOT_DISP:
2291 case R_MIPS_GOT_HI16:
2292 case R_MIPS_CALL_HI16:
2293 case R_MIPS_GOT_LO16:
2294 case R_MIPS_CALL_LO16:
2295 /* Find the index into the GOT where this value is located. */
2298 BFD_ASSERT (addend == 0);
2299 g = mips_elf_global_got_index (elf_hash_table (info)->dynobj,
2300 (struct elf_link_hash_entry *) h);
2301 if (! elf_hash_table(info)->dynamic_sections_created
2303 && (info->symbolic || h->root.dynindx == -1)
2304 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2306 /* This is a static link or a -Bsymbolic link. The
2307 symbol is defined locally, or was forced to be local.
2308 We must initialize this entry in the GOT. */
2309 bfd *tmpbfd = elf_hash_table (info)->dynobj;
2310 asection *sgot = mips_elf_got_section(tmpbfd);
2311 MIPS_ELF_PUT_WORD (tmpbfd, symbol + addend, sgot->contents + g);
2314 else if (r_type == R_MIPS_GOT16 || r_type == R_MIPS_CALL16)
2315 /* There's no need to create a local GOT entry here; the
2316 calculation for a local GOT16 entry does not involve G. */
2320 g = mips_elf_local_got_index (abfd, info, symbol + addend);
2322 return bfd_reloc_outofrange;
2325 /* Convert GOT indices to actual offsets. */
2326 g = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
2332 case R_MIPS16_GPREL:
2333 case R_MIPS_GPREL16:
2334 case R_MIPS_GPREL32:
2335 case R_MIPS_LITERAL:
2336 gp0 = _bfd_get_gp_value (input_bfd);
2337 gp = _bfd_get_gp_value (abfd);
2344 /* Figure out what kind of relocation is being performed. */
2348 return bfd_reloc_continue;
2351 value = symbol + mips_elf_sign_extend (addend, 16);
2352 overflowed_p = mips_elf_overflow_p (value, 16);
2359 || (elf_hash_table (info)->dynamic_sections_created
2361 && ((h->root.elf_link_hash_flags
2362 & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
2363 && ((h->root.elf_link_hash_flags
2364 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
2366 && (input_section->flags & SEC_ALLOC) != 0)
2368 /* If we're creating a shared library, or this relocation is
2369 against a symbol in a shared library, then we can't know
2370 where the symbol will end up. So, we create a relocation
2371 record in the output, and leave the job up to the dynamic
2374 if (!mips_elf_create_dynamic_relocation (abfd,
2382 return bfd_reloc_undefined;
2386 if (r_type != R_MIPS_REL32)
2387 value = symbol + addend;
2391 value &= howto->dst_mask;
2396 case R_MIPS_GNU_REL_LO16:
2397 value = symbol + addend - p;
2398 value &= howto->dst_mask;
2401 case R_MIPS_GNU_REL16_S2:
2402 value = symbol + mips_elf_sign_extend (addend << 2, 18) - p;
2403 overflowed_p = mips_elf_overflow_p (value, 18);
2404 value = (value >> 2) & howto->dst_mask;
2407 case R_MIPS_GNU_REL_HI16:
2408 /* Instead of subtracting 'p' here, we should be subtracting the
2409 equivalent value for the LO part of the reloc, since the value
2410 here is relative to that address. Because that's not easy to do,
2411 we adjust 'addend' in _bfd_mips_elf_relocate_section(). See also
2412 the comment there for more information. */
2413 value = mips_elf_high (addend + symbol - p);
2414 value &= howto->dst_mask;
2418 /* The calculation for R_MIPS16_26 is just the same as for an
2419 R_MIPS_26. It's only the storage of the relocated field into
2420 the output file that's different. That's handled in
2421 mips_elf_perform_relocation. So, we just fall through to the
2422 R_MIPS_26 case here. */
2425 value = (((addend << 2) | ((p + 4) & 0xf0000000)) + symbol) >> 2;
2427 value = (mips_elf_sign_extend (addend << 2, 28) + symbol) >> 2;
2428 value &= howto->dst_mask;
2434 value = mips_elf_high (addend + symbol);
2435 value &= howto->dst_mask;
2439 value = mips_elf_high (addend + gp - p);
2440 overflowed_p = mips_elf_overflow_p (value, 16);
2446 value = (symbol + addend) & howto->dst_mask;
2449 value = addend + gp - p + 4;
2450 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
2451 for overflow. But, on, say, IRIX5, relocations against
2452 _gp_disp are normally generated from the .cpload
2453 pseudo-op. It generates code that normally looks like
2456 lui $gp,%hi(_gp_disp)
2457 addiu $gp,$gp,%lo(_gp_disp)
2460 Here $t9 holds the address of the function being called,
2461 as required by the MIPS ELF ABI. The R_MIPS_LO16
2462 relocation can easily overflow in this situation, but the
2463 R_MIPS_HI16 relocation will handle the overflow.
2464 Therefore, we consider this a bug in the MIPS ABI, and do
2465 not check for overflow here. */
2469 case R_MIPS_LITERAL:
2470 /* Because we don't merge literal sections, we can handle this
2471 just like R_MIPS_GPREL16. In the long run, we should merge
2472 shared literals, and then we will need to additional work
2477 case R_MIPS16_GPREL:
2478 /* The R_MIPS16_GPREL performs the same calculation as
2479 R_MIPS_GPREL16, but stores the relocated bits in a different
2480 order. We don't need to do anything special here; the
2481 differences are handled in mips_elf_perform_relocation. */
2482 case R_MIPS_GPREL16:
2483 /* Only sign-extend the addend if it was extracted from the
2484 instruction. If the addend was separate, leave it alone,
2485 otherwise we may lose significant bits. */
2486 if (howto->partial_inplace)
2487 addend = mips_elf_sign_extend (addend, 16);
2488 value = symbol + addend - gp;
2489 /* If the symbol was local, any earlier relocatable links will
2490 have adjusted its addend with the gp offset, so compensate
2491 for that now. Don't do it for symbols forced local in this
2492 link, though, since they won't have had the gp offset applied
2496 overflowed_p = mips_elf_overflow_p (value, 16);
2505 /* The special case is when the symbol is forced to be local. We
2506 need the full address in the GOT since no R_MIPS_LO16 relocation
2508 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
2509 local_sections, false);
2510 value = mips_elf_got16_entry (abfd, info, symbol + addend, forced);
2511 if (value == MINUS_ONE)
2512 return bfd_reloc_outofrange;
2514 = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
2516 overflowed_p = mips_elf_overflow_p (value, 16);
2522 case R_MIPS_GOT_DISP:
2524 overflowed_p = mips_elf_overflow_p (value, 16);
2527 case R_MIPS_GPREL32:
2528 value = (addend + symbol + gp0 - gp);
2530 value &= howto->dst_mask;
2534 value = mips_elf_sign_extend (addend, 16) + symbol - p;
2535 overflowed_p = mips_elf_overflow_p (value, 16);
2536 value = (bfd_vma) ((bfd_signed_vma) value / 4);
2539 case R_MIPS_GOT_HI16:
2540 case R_MIPS_CALL_HI16:
2541 /* We're allowed to handle these two relocations identically.
2542 The dynamic linker is allowed to handle the CALL relocations
2543 differently by creating a lazy evaluation stub. */
2545 value = mips_elf_high (value);
2546 value &= howto->dst_mask;
2549 case R_MIPS_GOT_LO16:
2550 case R_MIPS_CALL_LO16:
2551 value = g & howto->dst_mask;
2554 case R_MIPS_GOT_PAGE:
2555 value = mips_elf_got_page (abfd, info, symbol + addend, NULL);
2556 if (value == MINUS_ONE)
2557 return bfd_reloc_outofrange;
2558 value = mips_elf_got_offset_from_index (elf_hash_table (info)->dynobj,
2560 overflowed_p = mips_elf_overflow_p (value, 16);
2563 case R_MIPS_GOT_OFST:
2564 mips_elf_got_page (abfd, info, symbol + addend, &value);
2565 overflowed_p = mips_elf_overflow_p (value, 16);
2569 value = symbol - addend;
2570 value &= howto->dst_mask;
2574 value = mips_elf_higher (addend + symbol);
2575 value &= howto->dst_mask;
2578 case R_MIPS_HIGHEST:
2579 value = mips_elf_highest (addend + symbol);
2580 value &= howto->dst_mask;
2583 case R_MIPS_SCN_DISP:
2584 value = symbol + addend - sec->output_offset;
2585 value &= howto->dst_mask;
2590 /* Both of these may be ignored. R_MIPS_JALR is an optimization
2591 hint; we could improve performance by honoring that hint. */
2592 return bfd_reloc_continue;
2594 case R_MIPS_GNU_VTINHERIT:
2595 case R_MIPS_GNU_VTENTRY:
2596 /* We don't do anything with these at present. */
2597 return bfd_reloc_continue;
2600 /* An unrecognized relocation type. */
2601 return bfd_reloc_notsupported;
2604 /* Store the VALUE for our caller. */
2606 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
2609 /* Obtain the field relocated by RELOCATION. */
2612 mips_elf_obtain_contents (howto, relocation, input_bfd, contents)
2613 reloc_howto_type *howto;
2614 const Elf_Internal_Rela *relocation;
2619 bfd_byte *location = contents + relocation->r_offset;
2621 /* Obtain the bytes. */
2622 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
2624 if ((ELF_R_TYPE (input_bfd, relocation->r_info) == R_MIPS16_26
2625 || ELF_R_TYPE (input_bfd, relocation->r_info) == R_MIPS16_GPREL)
2626 && bfd_little_endian (input_bfd))
2627 /* The two 16-bit words will be reversed on a little-endian system.
2628 See mips_elf_perform_relocation for more details. */
2629 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
2634 /* It has been determined that the result of the RELOCATION is the
2635 VALUE. Use HOWTO to place VALUE into the output file at the
2636 appropriate position. The SECTION is the section to which the
2637 relocation applies. If REQUIRE_JALX is true, then the opcode used
2638 for the relocation must be either JAL or JALX, and it is
2639 unconditionally converted to JALX.
2641 Returns false if anything goes wrong. */
2644 mips_elf_perform_relocation (info, howto, relocation, value, input_bfd,
2645 input_section, contents, require_jalx)
2646 struct bfd_link_info *info;
2647 reloc_howto_type *howto;
2648 const Elf_Internal_Rela *relocation;
2651 asection *input_section;
2653 boolean require_jalx;
2657 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
2659 /* Figure out where the relocation is occurring. */
2660 location = contents + relocation->r_offset;
2662 /* Obtain the current value. */
2663 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
2665 /* Clear the field we are setting. */
2666 x &= ~howto->dst_mask;
2668 /* If this is the R_MIPS16_26 relocation, we must store the
2669 value in a funny way. */
2670 if (r_type == R_MIPS16_26)
2672 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2673 Most mips16 instructions are 16 bits, but these instructions
2676 The format of these instructions is:
2678 +--------------+--------------------------------+
2679 ! JALX ! X! Imm 20:16 ! Imm 25:21 !
2680 +--------------+--------------------------------+
2682 +-----------------------------------------------+
2684 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2685 Note that the immediate value in the first word is swapped.
2687 When producing a relocateable object file, R_MIPS16_26 is
2688 handled mostly like R_MIPS_26. In particular, the addend is
2689 stored as a straight 26-bit value in a 32-bit instruction.
2690 (gas makes life simpler for itself by never adjusting a
2691 R_MIPS16_26 reloc to be against a section, so the addend is
2692 always zero). However, the 32 bit instruction is stored as 2
2693 16-bit values, rather than a single 32-bit value. In a
2694 big-endian file, the result is the same; in a little-endian
2695 file, the two 16-bit halves of the 32 bit value are swapped.
2696 This is so that a disassembler can recognize the jal
2699 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2700 instruction stored as two 16-bit values. The addend A is the
2701 contents of the targ26 field. The calculation is the same as
2702 R_MIPS_26. When storing the calculated value, reorder the
2703 immediate value as shown above, and don't forget to store the
2704 value as two 16-bit values.
2706 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2710 +--------+----------------------+
2714 +--------+----------------------+
2717 +----------+------+-------------+
2721 +----------+--------------------+
2722 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2723 ((sub1 << 16) | sub2)).
2725 When producing a relocateable object file, the calculation is
2726 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2727 When producing a fully linked file, the calculation is
2728 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2729 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff) */
2731 if (!info->relocateable)
2732 /* Shuffle the bits according to the formula above. */
2733 value = (((value & 0x1f0000) << 5)
2734 | ((value & 0x3e00000) >> 5)
2735 | (value & 0xffff));
2737 else if (r_type == R_MIPS16_GPREL)
2739 /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
2740 mode. A typical instruction will have a format like this:
2742 +--------------+--------------------------------+
2743 ! EXTEND ! Imm 10:5 ! Imm 15:11 !
2744 +--------------+--------------------------------+
2745 ! Major ! rx ! ry ! Imm 4:0 !
2746 +--------------+--------------------------------+
2748 EXTEND is the five bit value 11110. Major is the instruction
2751 This is handled exactly like R_MIPS_GPREL16, except that the
2752 addend is retrieved and stored as shown in this diagram; that
2753 is, the Imm fields above replace the V-rel16 field.
2755 All we need to do here is shuffle the bits appropriately. As
2756 above, the two 16-bit halves must be swapped on a
2757 little-endian system. */
2758 value = (((value & 0x7e0) << 16)
2759 | ((value & 0xf800) << 5)
2763 /* Set the field. */
2764 x |= (value & howto->dst_mask);
2766 /* If required, turn JAL into JALX. */
2770 bfd_vma opcode = x >> 26;
2771 bfd_vma jalx_opcode;
2773 /* Check to see if the opcode is already JAL or JALX. */
2774 if (r_type == R_MIPS16_26)
2776 ok = ((opcode == 0x6) || (opcode == 0x7));
2781 ok = ((opcode == 0x3) || (opcode == 0x1d));
2785 /* If the opcode is not JAL or JALX, there's a problem. */
2788 (*_bfd_error_handler)
2789 (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
2790 bfd_archive_filename (input_bfd),
2791 input_section->name,
2792 (unsigned long) relocation->r_offset);
2793 bfd_set_error (bfd_error_bad_value);
2797 /* Make this the JALX opcode. */
2798 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
2801 /* Swap the high- and low-order 16 bits on little-endian systems
2802 when doing a MIPS16 relocation. */
2803 if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
2804 && bfd_little_endian (input_bfd))
2805 x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
2807 /* Put the value into the output. */
2808 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
2812 /* Returns true if SECTION is a MIPS16 stub section. */
2815 mips_elf_stub_section_p (abfd, section)
2816 bfd *abfd ATTRIBUTE_UNUSED;
2819 const char *name = bfd_get_section_name (abfd, section);
2821 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
2822 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
2823 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
2826 /* Add room for N relocations to the .rel.dyn section in ABFD. */
2829 mips_elf_allocate_dynamic_relocations (abfd, n)
2835 s = bfd_get_section_by_name (abfd, ".rel.dyn");
2836 BFD_ASSERT (s != NULL);
2838 if (s->_raw_size == 0)
2840 /* Make room for a null element. */
2841 s->_raw_size += MIPS_ELF_REL_SIZE (abfd);
2844 s->_raw_size += n * MIPS_ELF_REL_SIZE (abfd);
2847 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
2848 is the original relocation, which is now being transformed into a
2849 dynamic relocation. The ADDENDP is adjusted if necessary; the
2850 caller should store the result in place of the original addend. */
2853 mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec,
2854 symbol, addendp, input_section)
2856 struct bfd_link_info *info;
2857 const Elf_Internal_Rela *rel;
2858 struct mips_elf_link_hash_entry *h;
2862 asection *input_section;
2864 Elf_Internal_Rel outrel[3];
2870 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
2871 dynobj = elf_hash_table (info)->dynobj;
2872 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
2873 BFD_ASSERT (sreloc != NULL);
2874 BFD_ASSERT (sreloc->contents != NULL);
2875 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
2876 < sreloc->_raw_size);
2879 outrel[0].r_offset =
2880 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
2881 outrel[1].r_offset =
2882 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
2883 outrel[2].r_offset =
2884 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
2887 /* We begin by assuming that the offset for the dynamic relocation
2888 is the same as for the original relocation. We'll adjust this
2889 later to reflect the correct output offsets. */
2890 if (elf_section_data (input_section)->sec_info_type != ELF_INFO_TYPE_STABS)
2892 outrel[1].r_offset = rel[1].r_offset;
2893 outrel[2].r_offset = rel[2].r_offset;
2897 /* Except that in a stab section things are more complex.
2898 Because we compress stab information, the offset given in the
2899 relocation may not be the one we want; we must let the stabs
2900 machinery tell us the offset. */
2901 outrel[1].r_offset = outrel[0].r_offset;
2902 outrel[2].r_offset = outrel[0].r_offset;
2903 /* If we didn't need the relocation at all, this value will be
2905 if (outrel[0].r_offset == (bfd_vma) -1)
2910 if (outrel[0].r_offset == (bfd_vma) -1)
2912 /* FIXME: For -2 runtime relocation needs to be skipped, but
2913 properly resolved statically and installed. */
2914 BFD_ASSERT (outrel[0].r_offset != (bfd_vma) -2);
2916 /* If we've decided to skip this relocation, just output an empty
2917 record. Note that R_MIPS_NONE == 0, so that this call to memset
2918 is a way of setting R_TYPE to R_MIPS_NONE. */
2920 memset (outrel, 0, sizeof (Elf_Internal_Rel) * 3);
2924 bfd_vma section_offset;
2926 /* We must now calculate the dynamic symbol table index to use
2927 in the relocation. */
2929 && (! info->symbolic || (h->root.elf_link_hash_flags
2930 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2932 indx = h->root.dynindx;
2933 /* h->root.dynindx may be -1 if this symbol was marked to
2940 if (sec != NULL && bfd_is_abs_section (sec))
2942 else if (sec == NULL || sec->owner == NULL)
2944 bfd_set_error (bfd_error_bad_value);
2949 indx = elf_section_data (sec->output_section)->dynindx;
2954 /* Figure out how far the target of the relocation is from
2955 the beginning of its section. */
2956 section_offset = symbol - sec->output_section->vma;
2957 /* The relocation we're building is section-relative.
2958 Therefore, the original addend must be adjusted by the
2960 *addendp += section_offset;
2961 /* Now, the relocation is just against the section. */
2962 symbol = sec->output_section->vma;
2965 /* If the relocation was previously an absolute relocation and
2966 this symbol will not be referred to by the relocation, we must
2967 adjust it by the value we give it in the dynamic symbol table.
2968 Otherwise leave the job up to the dynamic linker. */
2969 if (!indx && r_type != R_MIPS_REL32)
2972 /* The relocation is always an REL32 relocation because we don't
2973 know where the shared library will wind up at load-time. */
2974 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
2976 outrel[1].r_info = ELF_R_INFO (output_bfd, (unsigned long) 0,
2977 ABI_64_P (output_bfd)
2980 outrel[2].r_info = ELF_R_INFO (output_bfd, (unsigned long) 0,
2983 /* Adjust the output offset of the relocation to reference the
2984 correct location in the output file. */
2985 outrel[0].r_offset += (input_section->output_section->vma
2986 + input_section->output_offset);
2987 outrel[1].r_offset += (input_section->output_section->vma
2988 + input_section->output_offset);
2989 outrel[2].r_offset += (input_section->output_section->vma
2990 + input_section->output_offset);
2993 /* Put the relocation back out. We have to use the special
2994 relocation outputter in the 64-bit case since the 64-bit
2995 relocation format is non-standard. */
2996 if (ABI_64_P (output_bfd))
2998 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2999 (output_bfd, &outrel[0],
3001 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
3004 bfd_elf32_swap_reloc_out (output_bfd, &outrel[0],
3005 (((Elf32_External_Rel *)
3007 + sreloc->reloc_count));
3009 /* Record the index of the first relocation referencing H. This
3010 information is later emitted in the .msym section. */
3012 && (h->min_dyn_reloc_index == 0
3013 || sreloc->reloc_count < h->min_dyn_reloc_index))
3014 h->min_dyn_reloc_index = sreloc->reloc_count;
3016 /* We've now added another relocation. */
3017 ++sreloc->reloc_count;
3019 /* Make sure the output section is writable. The dynamic linker
3020 will be writing to it. */
3021 elf_section_data (input_section->output_section)->this_hdr.sh_flags
3024 /* On IRIX5, make an entry of compact relocation info. */
3025 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
3027 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
3032 Elf32_crinfo cptrel;
3034 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
3035 cptrel.vaddr = (rel->r_offset
3036 + input_section->output_section->vma
3037 + input_section->output_offset);
3038 if (r_type == R_MIPS_REL32)
3039 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
3041 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
3042 mips_elf_set_cr_dist2to (cptrel, 0);
3043 cptrel.konst = *addendp;
3045 cr = (scpt->contents
3046 + sizeof (Elf32_External_compact_rel));
3047 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
3048 ((Elf32_External_crinfo *) cr
3049 + scpt->reloc_count));
3050 ++scpt->reloc_count;
3057 /* Return the ISA for a MIPS e_flags value. */
3060 elf_mips_isa (flags)
3063 switch (flags & EF_MIPS_ARCH)
3075 case E_MIPS_ARCH_32:
3077 case E_MIPS_ARCH_64:
3083 /* Return the MACH for a MIPS e_flags value. */
3086 _bfd_elf_mips_mach (flags)
3089 switch (flags & EF_MIPS_MACH)
3091 case E_MIPS_MACH_3900:
3092 return bfd_mach_mips3900;
3094 case E_MIPS_MACH_4010:
3095 return bfd_mach_mips4010;
3097 case E_MIPS_MACH_4100:
3098 return bfd_mach_mips4100;
3100 case E_MIPS_MACH_4111:
3101 return bfd_mach_mips4111;
3103 case E_MIPS_MACH_4120:
3104 return bfd_mach_mips4120;
3106 case E_MIPS_MACH_4650:
3107 return bfd_mach_mips4650;
3109 case E_MIPS_MACH_5400:
3110 return bfd_mach_mips5400;
3112 case E_MIPS_MACH_5500:
3113 return bfd_mach_mips5500;
3115 case E_MIPS_MACH_SB1:
3116 return bfd_mach_mips_sb1;
3119 switch (flags & EF_MIPS_ARCH)
3123 return bfd_mach_mips3000;
3127 return bfd_mach_mips6000;
3131 return bfd_mach_mips4000;
3135 return bfd_mach_mips8000;
3139 return bfd_mach_mips5;
3142 case E_MIPS_ARCH_32:
3143 return bfd_mach_mipsisa32;
3146 case E_MIPS_ARCH_64:
3147 return bfd_mach_mipsisa64;
3155 /* Return printable name for ABI. */
3157 static INLINE char *
3158 elf_mips_abi_name (abfd)
3163 flags = elf_elfheader (abfd)->e_flags;
3164 switch (flags & EF_MIPS_ABI)
3167 if (ABI_N32_P (abfd))
3169 else if (ABI_64_P (abfd))
3173 case E_MIPS_ABI_O32:
3175 case E_MIPS_ABI_O64:
3177 case E_MIPS_ABI_EABI32:
3179 case E_MIPS_ABI_EABI64:
3182 return "unknown abi";
3186 /* MIPS ELF uses two common sections. One is the usual one, and the
3187 other is for small objects. All the small objects are kept
3188 together, and then referenced via the gp pointer, which yields
3189 faster assembler code. This is what we use for the small common
3190 section. This approach is copied from ecoff.c. */
3191 static asection mips_elf_scom_section;
3192 static asymbol mips_elf_scom_symbol;
3193 static asymbol *mips_elf_scom_symbol_ptr;
3195 /* MIPS ELF also uses an acommon section, which represents an
3196 allocated common symbol which may be overridden by a
3197 definition in a shared library. */
3198 static asection mips_elf_acom_section;
3199 static asymbol mips_elf_acom_symbol;
3200 static asymbol *mips_elf_acom_symbol_ptr;
3202 /* Handle the special MIPS section numbers that a symbol may use.
3203 This is used for both the 32-bit and the 64-bit ABI. */
3206 _bfd_mips_elf_symbol_processing (abfd, asym)
3210 elf_symbol_type *elfsym;
3212 elfsym = (elf_symbol_type *) asym;
3213 switch (elfsym->internal_elf_sym.st_shndx)
3215 case SHN_MIPS_ACOMMON:
3216 /* This section is used in a dynamically linked executable file.
3217 It is an allocated common section. The dynamic linker can
3218 either resolve these symbols to something in a shared
3219 library, or it can just leave them here. For our purposes,
3220 we can consider these symbols to be in a new section. */
3221 if (mips_elf_acom_section.name == NULL)
3223 /* Initialize the acommon section. */
3224 mips_elf_acom_section.name = ".acommon";
3225 mips_elf_acom_section.flags = SEC_ALLOC;
3226 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3227 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3228 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3229 mips_elf_acom_symbol.name = ".acommon";
3230 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3231 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3232 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3234 asym->section = &mips_elf_acom_section;
3238 /* Common symbols less than the GP size are automatically
3239 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3240 if (asym->value > elf_gp_size (abfd)
3241 || IRIX_COMPAT (abfd) == ict_irix6)
3244 case SHN_MIPS_SCOMMON:
3245 if (mips_elf_scom_section.name == NULL)
3247 /* Initialize the small common section. */
3248 mips_elf_scom_section.name = ".scommon";
3249 mips_elf_scom_section.flags = SEC_IS_COMMON;
3250 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3251 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3252 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3253 mips_elf_scom_symbol.name = ".scommon";
3254 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3255 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3256 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3258 asym->section = &mips_elf_scom_section;
3259 asym->value = elfsym->internal_elf_sym.st_size;
3262 case SHN_MIPS_SUNDEFINED:
3263 asym->section = bfd_und_section_ptr;
3266 #if 0 /* for SGI_COMPAT */
3268 asym->section = mips_elf_text_section_ptr;
3272 asym->section = mips_elf_data_section_ptr;
3278 /* Work over a section just before writing it out. This routine is
3279 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
3280 sections that need the SHF_MIPS_GPREL flag by name; there has to be
3284 _bfd_mips_elf_section_processing (abfd, hdr)
3286 Elf_Internal_Shdr *hdr;
3288 if (hdr->sh_type == SHT_MIPS_REGINFO
3289 && hdr->sh_size > 0)
3293 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
3294 BFD_ASSERT (hdr->contents == NULL);
3297 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
3300 H_PUT_32 (abfd, elf_gp (abfd), buf);
3301 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
3305 if (hdr->sh_type == SHT_MIPS_OPTIONS
3306 && hdr->bfd_section != NULL
3307 && elf_section_data (hdr->bfd_section) != NULL
3308 && elf_section_data (hdr->bfd_section)->tdata != NULL)
3310 bfd_byte *contents, *l, *lend;
3312 /* We stored the section contents in the elf_section_data tdata
3313 field in the set_section_contents routine. We save the
3314 section contents so that we don't have to read them again.
3315 At this point we know that elf_gp is set, so we can look
3316 through the section contents to see if there is an
3317 ODK_REGINFO structure. */
3319 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
3321 lend = contents + hdr->sh_size;
3322 while (l + sizeof (Elf_External_Options) <= lend)
3324 Elf_Internal_Options intopt;
3326 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3328 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3335 + sizeof (Elf_External_Options)
3336 + (sizeof (Elf64_External_RegInfo) - 8)),
3339 H_PUT_64 (abfd, elf_gp (abfd), buf);
3340 if (bfd_bwrite (buf, (bfd_size_type) 8, abfd) != 8)
3343 else if (intopt.kind == ODK_REGINFO)
3350 + sizeof (Elf_External_Options)
3351 + (sizeof (Elf32_External_RegInfo) - 4)),
3354 H_PUT_32 (abfd, elf_gp (abfd), buf);
3355 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
3362 if (hdr->bfd_section != NULL)
3364 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3366 if (strcmp (name, ".sdata") == 0
3367 || strcmp (name, ".lit8") == 0
3368 || strcmp (name, ".lit4") == 0)
3370 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3371 hdr->sh_type = SHT_PROGBITS;
3373 else if (strcmp (name, ".sbss") == 0)
3375 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3376 hdr->sh_type = SHT_NOBITS;
3378 else if (strcmp (name, ".srdata") == 0)
3380 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3381 hdr->sh_type = SHT_PROGBITS;
3383 else if (strcmp (name, ".compact_rel") == 0)
3386 hdr->sh_type = SHT_PROGBITS;
3388 else if (strcmp (name, ".rtproc") == 0)
3390 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3392 unsigned int adjust;
3394 adjust = hdr->sh_size % hdr->sh_addralign;
3396 hdr->sh_size += hdr->sh_addralign - adjust;
3404 /* Handle a MIPS specific section when reading an object file. This
3405 is called when elfcode.h finds a section with an unknown type.
3406 This routine supports both the 32-bit and 64-bit ELF ABI.
3408 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
3412 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
3414 Elf_Internal_Shdr *hdr;
3419 /* There ought to be a place to keep ELF backend specific flags, but
3420 at the moment there isn't one. We just keep track of the
3421 sections by their name, instead. Fortunately, the ABI gives
3422 suggested names for all the MIPS specific sections, so we will
3423 probably get away with this. */
3424 switch (hdr->sh_type)
3426 case SHT_MIPS_LIBLIST:
3427 if (strcmp (name, ".liblist") != 0)
3431 if (strcmp (name, ".msym") != 0)
3434 case SHT_MIPS_CONFLICT:
3435 if (strcmp (name, ".conflict") != 0)
3438 case SHT_MIPS_GPTAB:
3439 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
3442 case SHT_MIPS_UCODE:
3443 if (strcmp (name, ".ucode") != 0)
3446 case SHT_MIPS_DEBUG:
3447 if (strcmp (name, ".mdebug") != 0)
3449 flags = SEC_DEBUGGING;
3451 case SHT_MIPS_REGINFO:
3452 if (strcmp (name, ".reginfo") != 0
3453 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
3455 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
3457 case SHT_MIPS_IFACE:
3458 if (strcmp (name, ".MIPS.interfaces") != 0)
3461 case SHT_MIPS_CONTENT:
3462 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
3465 case SHT_MIPS_OPTIONS:
3466 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
3469 case SHT_MIPS_DWARF:
3470 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
3473 case SHT_MIPS_SYMBOL_LIB:
3474 if (strcmp (name, ".MIPS.symlib") != 0)
3477 case SHT_MIPS_EVENTS:
3478 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
3479 && strncmp (name, ".MIPS.post_rel",
3480 sizeof ".MIPS.post_rel" - 1) != 0)
3487 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
3492 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
3493 (bfd_get_section_flags (abfd,
3499 /* FIXME: We should record sh_info for a .gptab section. */
3501 /* For a .reginfo section, set the gp value in the tdata information
3502 from the contents of this section. We need the gp value while
3503 processing relocs, so we just get it now. The .reginfo section
3504 is not used in the 64-bit MIPS ELF ABI. */
3505 if (hdr->sh_type == SHT_MIPS_REGINFO)
3507 Elf32_External_RegInfo ext;
3510 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
3512 (bfd_size_type) sizeof ext))
3514 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
3515 elf_gp (abfd) = s.ri_gp_value;
3518 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
3519 set the gp value based on what we find. We may see both
3520 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
3521 they should agree. */
3522 if (hdr->sh_type == SHT_MIPS_OPTIONS)
3524 bfd_byte *contents, *l, *lend;
3526 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3527 if (contents == NULL)
3529 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
3530 (file_ptr) 0, hdr->sh_size))
3536 lend = contents + hdr->sh_size;
3537 while (l + sizeof (Elf_External_Options) <= lend)
3539 Elf_Internal_Options intopt;
3541 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3543 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3545 Elf64_Internal_RegInfo intreg;
3547 bfd_mips_elf64_swap_reginfo_in
3549 ((Elf64_External_RegInfo *)
3550 (l + sizeof (Elf_External_Options))),
3552 elf_gp (abfd) = intreg.ri_gp_value;
3554 else if (intopt.kind == ODK_REGINFO)
3556 Elf32_RegInfo intreg;
3558 bfd_mips_elf32_swap_reginfo_in
3560 ((Elf32_External_RegInfo *)
3561 (l + sizeof (Elf_External_Options))),
3563 elf_gp (abfd) = intreg.ri_gp_value;
3573 /* Set the correct type for a MIPS ELF section. We do this by the
3574 section name, which is a hack, but ought to work. This routine is
3575 used by both the 32-bit and the 64-bit ABI. */
3578 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
3580 Elf32_Internal_Shdr *hdr;
3583 register const char *name;
3585 name = bfd_get_section_name (abfd, sec);
3587 if (strcmp (name, ".liblist") == 0)
3589 hdr->sh_type = SHT_MIPS_LIBLIST;
3590 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
3591 /* The sh_link field is set in final_write_processing. */
3593 else if (strcmp (name, ".conflict") == 0)
3594 hdr->sh_type = SHT_MIPS_CONFLICT;
3595 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
3597 hdr->sh_type = SHT_MIPS_GPTAB;
3598 hdr->sh_entsize = sizeof (Elf32_External_gptab);
3599 /* The sh_info field is set in final_write_processing. */
3601 else if (strcmp (name, ".ucode") == 0)
3602 hdr->sh_type = SHT_MIPS_UCODE;
3603 else if (strcmp (name, ".mdebug") == 0)
3605 hdr->sh_type = SHT_MIPS_DEBUG;
3606 /* In a shared object on IRIX 5.3, the .mdebug section has an
3607 entsize of 0. FIXME: Does this matter? */
3608 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
3609 hdr->sh_entsize = 0;
3611 hdr->sh_entsize = 1;
3613 else if (strcmp (name, ".reginfo") == 0)
3615 hdr->sh_type = SHT_MIPS_REGINFO;
3616 /* In a shared object on IRIX 5.3, the .reginfo section has an
3617 entsize of 0x18. FIXME: Does this matter? */
3618 if (SGI_COMPAT (abfd))
3620 if ((abfd->flags & DYNAMIC) != 0)
3621 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
3623 hdr->sh_entsize = 1;
3626 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
3628 else if (SGI_COMPAT (abfd)
3629 && (strcmp (name, ".hash") == 0
3630 || strcmp (name, ".dynamic") == 0
3631 || strcmp (name, ".dynstr") == 0))
3633 if (SGI_COMPAT (abfd))
3634 hdr->sh_entsize = 0;
3636 /* This isn't how the IRIX6 linker behaves. */
3637 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
3640 else if (strcmp (name, ".got") == 0
3641 || strcmp (name, ".srdata") == 0
3642 || strcmp (name, ".sdata") == 0
3643 || strcmp (name, ".sbss") == 0
3644 || strcmp (name, ".lit4") == 0
3645 || strcmp (name, ".lit8") == 0)
3646 hdr->sh_flags |= SHF_MIPS_GPREL;
3647 else if (strcmp (name, ".MIPS.interfaces") == 0)
3649 hdr->sh_type = SHT_MIPS_IFACE;
3650 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3652 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
3654 hdr->sh_type = SHT_MIPS_CONTENT;
3655 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3656 /* The sh_info field is set in final_write_processing. */
3658 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
3660 hdr->sh_type = SHT_MIPS_OPTIONS;
3661 hdr->sh_entsize = 1;
3662 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3664 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
3665 hdr->sh_type = SHT_MIPS_DWARF;
3666 else if (strcmp (name, ".MIPS.symlib") == 0)
3668 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
3669 /* The sh_link and sh_info fields are set in
3670 final_write_processing. */
3672 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
3673 || strncmp (name, ".MIPS.post_rel",
3674 sizeof ".MIPS.post_rel" - 1) == 0)
3676 hdr->sh_type = SHT_MIPS_EVENTS;
3677 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3678 /* The sh_link field is set in final_write_processing. */
3680 else if (strcmp (name, ".msym") == 0)
3682 hdr->sh_type = SHT_MIPS_MSYM;
3683 hdr->sh_flags |= SHF_ALLOC;
3684 hdr->sh_entsize = 8;
3687 /* The generic elf_fake_sections will set up REL_HDR using the
3688 default kind of relocations. But, we may actually need both
3689 kinds of relocations, so we set up the second header here.
3691 This is not necessary for the O32 ABI since that only uses Elf32_Rel
3692 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
3693 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
3694 of the resulting empty .rela.<section> sections starts with
3695 sh_offset == object size, and ld doesn't allow that. While the check
3696 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
3697 avoided by not emitting those useless sections in the first place. */
3698 if (! SGI_COMPAT (abfd) && ! NEWABI_P(abfd)
3699 && (sec->flags & SEC_RELOC) != 0)
3701 struct bfd_elf_section_data *esd;
3702 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
3704 esd = elf_section_data (sec);
3705 BFD_ASSERT (esd->rel_hdr2 == NULL);
3706 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
3709 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
3710 !elf_section_data (sec)->use_rela_p);
3716 /* Given a BFD section, try to locate the corresponding ELF section
3717 index. This is used by both the 32-bit and the 64-bit ABI.
3718 Actually, it's not clear to me that the 64-bit ABI supports these,
3719 but for non-PIC objects we will certainly want support for at least
3720 the .scommon section. */
3723 _bfd_mips_elf_section_from_bfd_section (abfd, sec, retval)
3724 bfd *abfd ATTRIBUTE_UNUSED;
3728 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
3730 *retval = SHN_MIPS_SCOMMON;
3733 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
3735 *retval = SHN_MIPS_ACOMMON;
3741 /* Hook called by the linker routine which adds symbols from an object
3742 file. We must handle the special MIPS section numbers here. */
3745 _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3747 struct bfd_link_info *info;
3748 const Elf_Internal_Sym *sym;
3750 flagword *flagsp ATTRIBUTE_UNUSED;
3754 if (SGI_COMPAT (abfd)
3755 && (abfd->flags & DYNAMIC) != 0
3756 && strcmp (*namep, "_rld_new_interface") == 0)
3758 /* Skip IRIX5 rld entry name. */
3763 switch (sym->st_shndx)
3766 /* Common symbols less than the GP size are automatically
3767 treated as SHN_MIPS_SCOMMON symbols. */
3768 if (sym->st_size > elf_gp_size (abfd)
3769 || IRIX_COMPAT (abfd) == ict_irix6)
3772 case SHN_MIPS_SCOMMON:
3773 *secp = bfd_make_section_old_way (abfd, ".scommon");
3774 (*secp)->flags |= SEC_IS_COMMON;
3775 *valp = sym->st_size;
3779 /* This section is used in a shared object. */
3780 if (elf_tdata (abfd)->elf_text_section == NULL)
3782 asymbol *elf_text_symbol;
3783 asection *elf_text_section;
3784 bfd_size_type amt = sizeof (asection);
3786 elf_text_section = bfd_zalloc (abfd, amt);
3787 if (elf_text_section == NULL)
3790 amt = sizeof (asymbol);
3791 elf_text_symbol = bfd_zalloc (abfd, amt);
3792 if (elf_text_symbol == NULL)
3795 /* Initialize the section. */
3797 elf_tdata (abfd)->elf_text_section = elf_text_section;
3798 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
3800 elf_text_section->symbol = elf_text_symbol;
3801 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
3803 elf_text_section->name = ".text";
3804 elf_text_section->flags = SEC_NO_FLAGS;
3805 elf_text_section->output_section = NULL;
3806 elf_text_section->owner = abfd;
3807 elf_text_symbol->name = ".text";
3808 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3809 elf_text_symbol->section = elf_text_section;
3811 /* This code used to do *secp = bfd_und_section_ptr if
3812 info->shared. I don't know why, and that doesn't make sense,
3813 so I took it out. */
3814 *secp = elf_tdata (abfd)->elf_text_section;
3817 case SHN_MIPS_ACOMMON:
3818 /* Fall through. XXX Can we treat this as allocated data? */
3820 /* This section is used in a shared object. */
3821 if (elf_tdata (abfd)->elf_data_section == NULL)
3823 asymbol *elf_data_symbol;
3824 asection *elf_data_section;
3825 bfd_size_type amt = sizeof (asection);
3827 elf_data_section = bfd_zalloc (abfd, amt);
3828 if (elf_data_section == NULL)
3831 amt = sizeof (asymbol);
3832 elf_data_symbol = bfd_zalloc (abfd, amt);
3833 if (elf_data_symbol == NULL)
3836 /* Initialize the section. */
3838 elf_tdata (abfd)->elf_data_section = elf_data_section;
3839 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
3841 elf_data_section->symbol = elf_data_symbol;
3842 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
3844 elf_data_section->name = ".data";
3845 elf_data_section->flags = SEC_NO_FLAGS;
3846 elf_data_section->output_section = NULL;
3847 elf_data_section->owner = abfd;
3848 elf_data_symbol->name = ".data";
3849 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3850 elf_data_symbol->section = elf_data_section;
3852 /* This code used to do *secp = bfd_und_section_ptr if
3853 info->shared. I don't know why, and that doesn't make sense,
3854 so I took it out. */
3855 *secp = elf_tdata (abfd)->elf_data_section;
3858 case SHN_MIPS_SUNDEFINED:
3859 *secp = bfd_und_section_ptr;
3863 if (SGI_COMPAT (abfd)
3865 && info->hash->creator == abfd->xvec
3866 && strcmp (*namep, "__rld_obj_head") == 0)
3868 struct elf_link_hash_entry *h;
3869 struct bfd_link_hash_entry *bh;
3871 /* Mark __rld_obj_head as dynamic. */
3873 if (! (_bfd_generic_link_add_one_symbol
3874 (info, abfd, *namep, BSF_GLOBAL, *secp,
3875 (bfd_vma) *valp, (const char *) NULL, false,
3876 get_elf_backend_data (abfd)->collect, &bh)))
3879 h = (struct elf_link_hash_entry *) bh;
3880 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3881 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3882 h->type = STT_OBJECT;
3884 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3887 mips_elf_hash_table (info)->use_rld_obj_head = true;
3890 /* If this is a mips16 text symbol, add 1 to the value to make it
3891 odd. This will cause something like .word SYM to come up with
3892 the right value when it is loaded into the PC. */
3893 if (sym->st_other == STO_MIPS16)
3899 /* This hook function is called before the linker writes out a global
3900 symbol. We mark symbols as small common if appropriate. This is
3901 also where we undo the increment of the value for a mips16 symbol. */
3904 _bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
3905 bfd *abfd ATTRIBUTE_UNUSED;
3906 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3907 const char *name ATTRIBUTE_UNUSED;
3908 Elf_Internal_Sym *sym;
3909 asection *input_sec;
3911 /* If we see a common symbol, which implies a relocatable link, then
3912 if a symbol was small common in an input file, mark it as small
3913 common in the output file. */
3914 if (sym->st_shndx == SHN_COMMON
3915 && strcmp (input_sec->name, ".scommon") == 0)
3916 sym->st_shndx = SHN_MIPS_SCOMMON;
3918 if (sym->st_other == STO_MIPS16
3919 && (sym->st_value & 1) != 0)
3925 /* Functions for the dynamic linker. */
3927 /* Create dynamic sections when linking against a dynamic object. */
3930 _bfd_mips_elf_create_dynamic_sections (abfd, info)
3932 struct bfd_link_info *info;
3934 struct elf_link_hash_entry *h;
3935 struct bfd_link_hash_entry *bh;
3937 register asection *s;
3938 const char * const *namep;
3940 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3941 | SEC_LINKER_CREATED | SEC_READONLY);
3943 /* Mips ABI requests the .dynamic section to be read only. */
3944 s = bfd_get_section_by_name (abfd, ".dynamic");
3947 if (! bfd_set_section_flags (abfd, s, flags))
3951 /* We need to create .got section. */
3952 if (! mips_elf_create_got_section (abfd, info))
3955 /* Create the .msym section on IRIX6. It is used by the dynamic
3956 linker to speed up dynamic relocations, and to avoid computing
3957 the ELF hash for symbols. */
3958 if (IRIX_COMPAT (abfd) == ict_irix6
3959 && !mips_elf_create_msym_section (abfd))
3962 /* Create .stub section. */
3963 if (bfd_get_section_by_name (abfd,
3964 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
3966 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
3968 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
3969 || ! bfd_set_section_alignment (abfd, s,
3970 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3974 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
3976 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
3978 s = bfd_make_section (abfd, ".rld_map");
3980 || ! bfd_set_section_flags (abfd, s, flags &~ (flagword) SEC_READONLY)
3981 || ! bfd_set_section_alignment (abfd, s,
3982 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3986 /* On IRIX5, we adjust add some additional symbols and change the
3987 alignments of several sections. There is no ABI documentation
3988 indicating that this is necessary on IRIX6, nor any evidence that
3989 the linker takes such action. */
3990 if (IRIX_COMPAT (abfd) == ict_irix5)
3992 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
3995 if (! (_bfd_generic_link_add_one_symbol
3996 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
3997 (bfd_vma) 0, (const char *) NULL, false,
3998 get_elf_backend_data (abfd)->collect, &bh)))
4001 h = (struct elf_link_hash_entry *) bh;
4002 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4003 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4004 h->type = STT_SECTION;
4006 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4010 /* We need to create a .compact_rel section. */
4011 if (SGI_COMPAT (abfd))
4013 if (!mips_elf_create_compact_rel_section (abfd, info))
4017 /* Change alignments of some sections. */
4018 s = bfd_get_section_by_name (abfd, ".hash");
4020 bfd_set_section_alignment (abfd, s, 4);
4021 s = bfd_get_section_by_name (abfd, ".dynsym");
4023 bfd_set_section_alignment (abfd, s, 4);
4024 s = bfd_get_section_by_name (abfd, ".dynstr");
4026 bfd_set_section_alignment (abfd, s, 4);
4027 s = bfd_get_section_by_name (abfd, ".reginfo");
4029 bfd_set_section_alignment (abfd, s, 4);
4030 s = bfd_get_section_by_name (abfd, ".dynamic");
4032 bfd_set_section_alignment (abfd, s, 4);
4039 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
4041 if (!(_bfd_generic_link_add_one_symbol
4042 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr,
4043 (bfd_vma) 0, (const char *) NULL, false,
4044 get_elf_backend_data (abfd)->collect, &bh)))
4047 h = (struct elf_link_hash_entry *) bh;
4048 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4049 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4050 h->type = STT_SECTION;
4052 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4055 if (! mips_elf_hash_table (info)->use_rld_obj_head)
4057 /* __rld_map is a four byte word located in the .data section
4058 and is filled in by the rtld to contain a pointer to
4059 the _r_debug structure. Its symbol value will be set in
4060 _bfd_mips_elf_finish_dynamic_symbol. */
4061 s = bfd_get_section_by_name (abfd, ".rld_map");
4062 BFD_ASSERT (s != NULL);
4064 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
4066 if (!(_bfd_generic_link_add_one_symbol
4067 (info, abfd, name, BSF_GLOBAL, s,
4068 (bfd_vma) 0, (const char *) NULL, false,
4069 get_elf_backend_data (abfd)->collect, &bh)))
4072 h = (struct elf_link_hash_entry *) bh;
4073 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4074 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4075 h->type = STT_OBJECT;
4077 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4085 /* Look through the relocs for a section during the first phase, and
4086 allocate space in the global offset table. */
4089 _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
4091 struct bfd_link_info *info;
4093 const Elf_Internal_Rela *relocs;
4097 Elf_Internal_Shdr *symtab_hdr;
4098 struct elf_link_hash_entry **sym_hashes;
4099 struct mips_got_info *g;
4101 const Elf_Internal_Rela *rel;
4102 const Elf_Internal_Rela *rel_end;
4105 struct elf_backend_data *bed;
4107 if (info->relocateable)
4110 dynobj = elf_hash_table (info)->dynobj;
4111 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4112 sym_hashes = elf_sym_hashes (abfd);
4113 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
4115 /* Check for the mips16 stub sections. */
4117 name = bfd_get_section_name (abfd, sec);
4118 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
4120 unsigned long r_symndx;
4122 /* Look at the relocation information to figure out which symbol
4125 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
4127 if (r_symndx < extsymoff
4128 || sym_hashes[r_symndx - extsymoff] == NULL)
4132 /* This stub is for a local symbol. This stub will only be
4133 needed if there is some relocation in this BFD, other
4134 than a 16 bit function call, which refers to this symbol. */
4135 for (o = abfd->sections; o != NULL; o = o->next)
4137 Elf_Internal_Rela *sec_relocs;
4138 const Elf_Internal_Rela *r, *rend;
4140 /* We can ignore stub sections when looking for relocs. */
4141 if ((o->flags & SEC_RELOC) == 0
4142 || o->reloc_count == 0
4143 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
4144 sizeof FN_STUB - 1) == 0
4145 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
4146 sizeof CALL_STUB - 1) == 0
4147 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
4148 sizeof CALL_FP_STUB - 1) == 0)
4151 sec_relocs = (MNAME(abfd,_bfd_elf,link_read_relocs)
4152 (abfd, o, (PTR) NULL,
4153 (Elf_Internal_Rela *) NULL,
4154 info->keep_memory));
4155 if (sec_relocs == NULL)
4158 rend = sec_relocs + o->reloc_count;
4159 for (r = sec_relocs; r < rend; r++)
4160 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
4161 && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
4164 if (elf_section_data (o)->relocs != sec_relocs)
4173 /* There is no non-call reloc for this stub, so we do
4174 not need it. Since this function is called before
4175 the linker maps input sections to output sections, we
4176 can easily discard it by setting the SEC_EXCLUDE
4178 sec->flags |= SEC_EXCLUDE;
4182 /* Record this stub in an array of local symbol stubs for
4184 if (elf_tdata (abfd)->local_stubs == NULL)
4186 unsigned long symcount;
4190 if (elf_bad_symtab (abfd))
4191 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
4193 symcount = symtab_hdr->sh_info;
4194 amt = symcount * sizeof (asection *);
4195 n = (asection **) bfd_zalloc (abfd, amt);
4198 elf_tdata (abfd)->local_stubs = n;
4201 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
4203 /* We don't need to set mips16_stubs_seen in this case.
4204 That flag is used to see whether we need to look through
4205 the global symbol table for stubs. We don't need to set
4206 it here, because we just have a local stub. */
4210 struct mips_elf_link_hash_entry *h;
4212 h = ((struct mips_elf_link_hash_entry *)
4213 sym_hashes[r_symndx - extsymoff]);
4215 /* H is the symbol this stub is for. */
4218 mips_elf_hash_table (info)->mips16_stubs_seen = true;
4221 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
4222 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4224 unsigned long r_symndx;
4225 struct mips_elf_link_hash_entry *h;
4228 /* Look at the relocation information to figure out which symbol
4231 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
4233 if (r_symndx < extsymoff
4234 || sym_hashes[r_symndx - extsymoff] == NULL)
4236 /* This stub was actually built for a static symbol defined
4237 in the same file. We assume that all static symbols in
4238 mips16 code are themselves mips16, so we can simply
4239 discard this stub. Since this function is called before
4240 the linker maps input sections to output sections, we can
4241 easily discard it by setting the SEC_EXCLUDE flag. */
4242 sec->flags |= SEC_EXCLUDE;
4246 h = ((struct mips_elf_link_hash_entry *)
4247 sym_hashes[r_symndx - extsymoff]);
4249 /* H is the symbol this stub is for. */
4251 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4252 loc = &h->call_fp_stub;
4254 loc = &h->call_stub;
4256 /* If we already have an appropriate stub for this function, we
4257 don't need another one, so we can discard this one. Since
4258 this function is called before the linker maps input sections
4259 to output sections, we can easily discard it by setting the
4260 SEC_EXCLUDE flag. We can also discard this section if we
4261 happen to already know that this is a mips16 function; it is
4262 not necessary to check this here, as it is checked later, but
4263 it is slightly faster to check now. */
4264 if (*loc != NULL || h->root.other == STO_MIPS16)
4266 sec->flags |= SEC_EXCLUDE;
4271 mips_elf_hash_table (info)->mips16_stubs_seen = true;
4281 sgot = mips_elf_got_section (dynobj);
4286 BFD_ASSERT (elf_section_data (sgot) != NULL);
4287 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4288 BFD_ASSERT (g != NULL);
4293 bed = get_elf_backend_data (abfd);
4294 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
4295 for (rel = relocs; rel < rel_end; ++rel)
4297 unsigned long r_symndx;
4298 unsigned int r_type;
4299 struct elf_link_hash_entry *h;
4301 r_symndx = ELF_R_SYM (abfd, rel->r_info);
4302 r_type = ELF_R_TYPE (abfd, rel->r_info);
4304 if (r_symndx < extsymoff)
4306 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
4308 (*_bfd_error_handler)
4309 (_("%s: Malformed reloc detected for section %s"),
4310 bfd_archive_filename (abfd), name);
4311 bfd_set_error (bfd_error_bad_value);
4316 h = sym_hashes[r_symndx - extsymoff];
4318 /* This may be an indirect symbol created because of a version. */
4321 while (h->root.type == bfd_link_hash_indirect)
4322 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4326 /* Some relocs require a global offset table. */
4327 if (dynobj == NULL || sgot == NULL)
4333 case R_MIPS_CALL_HI16:
4334 case R_MIPS_CALL_LO16:
4335 case R_MIPS_GOT_HI16:
4336 case R_MIPS_GOT_LO16:
4337 case R_MIPS_GOT_PAGE:
4338 case R_MIPS_GOT_OFST:
4339 case R_MIPS_GOT_DISP:
4341 elf_hash_table (info)->dynobj = dynobj = abfd;
4342 if (! mips_elf_create_got_section (dynobj, info))
4344 g = mips_elf_got_info (dynobj, &sgot);
4351 && (info->shared || h != NULL)
4352 && (sec->flags & SEC_ALLOC) != 0)
4353 elf_hash_table (info)->dynobj = dynobj = abfd;
4361 if (!h && (r_type == R_MIPS_CALL_LO16
4362 || r_type == R_MIPS_GOT_LO16
4363 || r_type == R_MIPS_GOT_DISP))
4365 /* We may need a local GOT entry for this relocation. We
4366 don't count R_MIPS_GOT_PAGE because we can estimate the
4367 maximum number of pages needed by looking at the size of
4368 the segment. Similar comments apply to R_MIPS_GOT16 and
4369 R_MIPS_CALL16. We don't count R_MIPS_GOT_HI16, or
4370 R_MIPS_CALL_HI16 because these are always followed by an
4371 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
4373 This estimation is very conservative since we can merge
4374 duplicate entries in the GOT. In order to be less
4375 conservative, we could actually build the GOT here,
4376 rather than in relocate_section. */
4378 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
4386 (*_bfd_error_handler)
4387 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
4388 bfd_archive_filename (abfd), (unsigned long) rel->r_offset);
4389 bfd_set_error (bfd_error_bad_value);
4394 case R_MIPS_CALL_HI16:
4395 case R_MIPS_CALL_LO16:
4398 /* This symbol requires a global offset table entry. */
4399 if (! mips_elf_record_global_got_symbol (h, info, g))
4402 /* We need a stub, not a plt entry for the undefined
4403 function. But we record it as if it needs plt. See
4404 elf_adjust_dynamic_symbol in elflink.h. */
4405 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4411 case R_MIPS_GOT_HI16:
4412 case R_MIPS_GOT_LO16:
4413 case R_MIPS_GOT_DISP:
4414 /* This symbol requires a global offset table entry. */
4415 if (h && ! mips_elf_record_global_got_symbol (h, info, g))
4422 if ((info->shared || h != NULL)
4423 && (sec->flags & SEC_ALLOC) != 0)
4427 const char *dname = ".rel.dyn";
4429 sreloc = bfd_get_section_by_name (dynobj, dname);
4432 sreloc = bfd_make_section (dynobj, dname);
4434 || ! bfd_set_section_flags (dynobj, sreloc,
4439 | SEC_LINKER_CREATED
4441 || ! bfd_set_section_alignment (dynobj, sreloc,
4446 #define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
4449 /* When creating a shared object, we must copy these
4450 reloc types into the output file as R_MIPS_REL32
4451 relocs. We make room for this reloc in the
4452 .rel.dyn reloc section. */
4453 mips_elf_allocate_dynamic_relocations (dynobj, 1);
4454 if ((sec->flags & MIPS_READONLY_SECTION)
4455 == MIPS_READONLY_SECTION)
4456 /* We tell the dynamic linker that there are
4457 relocations against the text segment. */
4458 info->flags |= DF_TEXTREL;
4462 struct mips_elf_link_hash_entry *hmips;
4464 /* We only need to copy this reloc if the symbol is
4465 defined in a dynamic object. */
4466 hmips = (struct mips_elf_link_hash_entry *) h;
4467 ++hmips->possibly_dynamic_relocs;
4468 if ((sec->flags & MIPS_READONLY_SECTION)
4469 == MIPS_READONLY_SECTION)
4470 /* We need it to tell the dynamic linker if there
4471 are relocations against the text segment. */
4472 hmips->readonly_reloc = true;
4475 /* Even though we don't directly need a GOT entry for
4476 this symbol, a symbol must have a dynamic symbol
4477 table index greater that DT_MIPS_GOTSYM if there are
4478 dynamic relocations against it. */
4480 && ! mips_elf_record_global_got_symbol (h, info, g))
4484 if (SGI_COMPAT (abfd))
4485 mips_elf_hash_table (info)->compact_rel_size +=
4486 sizeof (Elf32_External_crinfo);
4490 case R_MIPS_GPREL16:
4491 case R_MIPS_LITERAL:
4492 case R_MIPS_GPREL32:
4493 if (SGI_COMPAT (abfd))
4494 mips_elf_hash_table (info)->compact_rel_size +=
4495 sizeof (Elf32_External_crinfo);
4498 /* This relocation describes the C++ object vtable hierarchy.
4499 Reconstruct it for later use during GC. */
4500 case R_MIPS_GNU_VTINHERIT:
4501 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4505 /* This relocation describes which C++ vtable entries are actually
4506 used. Record for later use during GC. */
4507 case R_MIPS_GNU_VTENTRY:
4508 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4516 /* We must not create a stub for a symbol that has relocations
4517 related to taking the function's address. */
4523 struct mips_elf_link_hash_entry *mh;
4525 mh = (struct mips_elf_link_hash_entry *) h;
4526 mh->no_fn_stub = true;
4530 case R_MIPS_CALL_HI16:
4531 case R_MIPS_CALL_LO16:
4535 /* If this reloc is not a 16 bit call, and it has a global
4536 symbol, then we will need the fn_stub if there is one.
4537 References from a stub section do not count. */
4539 && r_type != R_MIPS16_26
4540 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
4541 sizeof FN_STUB - 1) != 0
4542 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
4543 sizeof CALL_STUB - 1) != 0
4544 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
4545 sizeof CALL_FP_STUB - 1) != 0)
4547 struct mips_elf_link_hash_entry *mh;
4549 mh = (struct mips_elf_link_hash_entry *) h;
4550 mh->need_fn_stub = true;
4557 /* Adjust a symbol defined by a dynamic object and referenced by a
4558 regular object. The current definition is in some section of the
4559 dynamic object, but we're not including those sections. We have to
4560 change the definition to something the rest of the link can
4564 _bfd_mips_elf_adjust_dynamic_symbol (info, h)
4565 struct bfd_link_info *info;
4566 struct elf_link_hash_entry *h;
4569 struct mips_elf_link_hash_entry *hmips;
4572 dynobj = elf_hash_table (info)->dynobj;
4574 /* Make sure we know what is going on here. */
4575 BFD_ASSERT (dynobj != NULL
4576 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
4577 || h->weakdef != NULL
4578 || ((h->elf_link_hash_flags
4579 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4580 && (h->elf_link_hash_flags
4581 & ELF_LINK_HASH_REF_REGULAR) != 0
4582 && (h->elf_link_hash_flags
4583 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
4585 /* If this symbol is defined in a dynamic object, we need to copy
4586 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
4588 hmips = (struct mips_elf_link_hash_entry *) h;
4589 if (! info->relocateable
4590 && hmips->possibly_dynamic_relocs != 0
4591 && (h->root.type == bfd_link_hash_defweak
4592 || (h->elf_link_hash_flags
4593 & ELF_LINK_HASH_DEF_REGULAR) == 0))
4595 mips_elf_allocate_dynamic_relocations (dynobj,
4596 hmips->possibly_dynamic_relocs);
4597 if (hmips->readonly_reloc)
4598 /* We tell the dynamic linker that there are relocations
4599 against the text segment. */
4600 info->flags |= DF_TEXTREL;
4603 /* For a function, create a stub, if allowed. */
4604 if (! hmips->no_fn_stub
4605 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4607 if (! elf_hash_table (info)->dynamic_sections_created)
4610 /* If this symbol is not defined in a regular file, then set
4611 the symbol to the stub location. This is required to make
4612 function pointers compare as equal between the normal
4613 executable and the shared library. */
4614 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4616 /* We need .stub section. */
4617 s = bfd_get_section_by_name (dynobj,
4618 MIPS_ELF_STUB_SECTION_NAME (dynobj));
4619 BFD_ASSERT (s != NULL);
4621 h->root.u.def.section = s;
4622 h->root.u.def.value = s->_raw_size;
4624 /* XXX Write this stub address somewhere. */
4625 h->plt.offset = s->_raw_size;
4627 /* Make room for this stub code. */
4628 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4630 /* The last half word of the stub will be filled with the index
4631 of this symbol in .dynsym section. */
4635 else if ((h->type == STT_FUNC)
4636 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4638 /* This will set the entry for this symbol in the GOT to 0, and
4639 the dynamic linker will take care of this. */
4640 h->root.u.def.value = 0;
4644 /* If this is a weak symbol, and there is a real definition, the
4645 processor independent code will have arranged for us to see the
4646 real definition first, and we can just use the same value. */
4647 if (h->weakdef != NULL)
4649 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4650 || h->weakdef->root.type == bfd_link_hash_defweak);
4651 h->root.u.def.section = h->weakdef->root.u.def.section;
4652 h->root.u.def.value = h->weakdef->root.u.def.value;
4656 /* This is a reference to a symbol defined by a dynamic object which
4657 is not a function. */
4662 /* This function is called after all the input files have been read,
4663 and the input sections have been assigned to output sections. We
4664 check for any mips16 stub sections that we can discard. */
4667 _bfd_mips_elf_always_size_sections (output_bfd, info)
4669 struct bfd_link_info *info;
4673 /* The .reginfo section has a fixed size. */
4674 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
4676 bfd_set_section_size (output_bfd, ri,
4677 (bfd_size_type) sizeof (Elf32_External_RegInfo));
4679 if (info->relocateable
4680 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
4683 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4684 mips_elf_check_mips16_stubs,
4690 /* Set the sizes of the dynamic sections. */
4693 _bfd_mips_elf_size_dynamic_sections (output_bfd, info)
4695 struct bfd_link_info *info;
4700 struct mips_got_info *g = NULL;
4702 dynobj = elf_hash_table (info)->dynobj;
4703 BFD_ASSERT (dynobj != NULL);
4705 if (elf_hash_table (info)->dynamic_sections_created)
4707 /* Set the contents of the .interp section to the interpreter. */
4710 s = bfd_get_section_by_name (dynobj, ".interp");
4711 BFD_ASSERT (s != NULL);
4713 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
4715 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
4719 /* The check_relocs and adjust_dynamic_symbol entry points have
4720 determined the sizes of the various dynamic sections. Allocate
4723 for (s = dynobj->sections; s != NULL; s = s->next)
4728 /* It's OK to base decisions on the section name, because none
4729 of the dynobj section names depend upon the input files. */
4730 name = bfd_get_section_name (dynobj, s);
4732 if ((s->flags & SEC_LINKER_CREATED) == 0)
4737 if (strncmp (name, ".rel", 4) == 0)
4739 if (s->_raw_size == 0)
4741 /* We only strip the section if the output section name
4742 has the same name. Otherwise, there might be several
4743 input sections for this output section. FIXME: This
4744 code is probably not needed these days anyhow, since
4745 the linker now does not create empty output sections. */
4746 if (s->output_section != NULL
4748 bfd_get_section_name (s->output_section->owner,
4749 s->output_section)) == 0)
4754 const char *outname;
4757 /* If this relocation section applies to a read only
4758 section, then we probably need a DT_TEXTREL entry.
4759 If the relocation section is .rel.dyn, we always
4760 assert a DT_TEXTREL entry rather than testing whether
4761 there exists a relocation to a read only section or
4763 outname = bfd_get_section_name (output_bfd,
4765 target = bfd_get_section_by_name (output_bfd, outname + 4);
4767 && (target->flags & SEC_READONLY) != 0
4768 && (target->flags & SEC_ALLOC) != 0)
4769 || strcmp (outname, ".rel.dyn") == 0)
4772 /* We use the reloc_count field as a counter if we need
4773 to copy relocs into the output file. */
4774 if (strcmp (name, ".rel.dyn") != 0)
4778 else if (strncmp (name, ".got", 4) == 0)
4781 bfd_size_type loadable_size = 0;
4782 bfd_size_type local_gotno;
4785 BFD_ASSERT (elf_section_data (s) != NULL);
4786 g = (struct mips_got_info *) elf_section_data (s)->tdata;
4787 BFD_ASSERT (g != NULL);
4789 /* Calculate the total loadable size of the output. That
4790 will give us the maximum number of GOT_PAGE entries
4792 for (sub = info->input_bfds; sub; sub = sub->link_next)
4794 asection *subsection;
4796 for (subsection = sub->sections;
4798 subsection = subsection->next)
4800 if ((subsection->flags & SEC_ALLOC) == 0)
4802 loadable_size += ((subsection->_raw_size + 0xf)
4803 &~ (bfd_size_type) 0xf);
4806 loadable_size += MIPS_FUNCTION_STUB_SIZE;
4808 /* Assume there are two loadable segments consisting of
4809 contiguous sections. Is 5 enough? */
4810 local_gotno = (loadable_size >> 16) + 5;
4811 if (NEWABI_P (output_bfd))
4812 /* It's possible we will need GOT_PAGE entries as well as
4813 GOT16 entries. Often, these will be able to share GOT
4814 entries, but not always. */
4817 g->local_gotno += local_gotno;
4818 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
4820 /* There has to be a global GOT entry for every symbol with
4821 a dynamic symbol table index of DT_MIPS_GOTSYM or
4822 higher. Therefore, it make sense to put those symbols
4823 that need GOT entries at the end of the symbol table. We
4825 if (! mips_elf_sort_hash_table (info, 1))
4828 if (g->global_gotsym != NULL)
4829 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
4831 /* If there are no global symbols, or none requiring
4832 relocations, then GLOBAL_GOTSYM will be NULL. */
4834 g->global_gotno = i;
4835 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
4837 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
4839 /* IRIX rld assumes that the function stub isn't at the end
4840 of .text section. So put a dummy. XXX */
4841 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4843 else if (! info->shared
4844 && ! mips_elf_hash_table (info)->use_rld_obj_head
4845 && strncmp (name, ".rld_map", 8) == 0)
4847 /* We add a room for __rld_map. It will be filled in by the
4848 rtld to contain a pointer to the _r_debug structure. */
4851 else if (SGI_COMPAT (output_bfd)
4852 && strncmp (name, ".compact_rel", 12) == 0)
4853 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
4854 else if (strcmp (name, ".msym") == 0)
4855 s->_raw_size = (sizeof (Elf32_External_Msym)
4856 * (elf_hash_table (info)->dynsymcount
4857 + bfd_count_sections (output_bfd)));
4858 else if (strncmp (name, ".init", 5) != 0)
4860 /* It's not one of our sections, so don't allocate space. */
4866 _bfd_strip_section_from_output (info, s);
4870 /* Allocate memory for the section contents. */
4871 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4872 if (s->contents == NULL && s->_raw_size != 0)
4874 bfd_set_error (bfd_error_no_memory);
4879 if (elf_hash_table (info)->dynamic_sections_created)
4881 /* Add some entries to the .dynamic section. We fill in the
4882 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
4883 must add the entries now so that we get the correct size for
4884 the .dynamic section. The DT_DEBUG entry is filled in by the
4885 dynamic linker and used by the debugger. */
4888 /* SGI object has the equivalence of DT_DEBUG in the
4889 DT_MIPS_RLD_MAP entry. */
4890 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
4892 if (!SGI_COMPAT (output_bfd))
4894 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
4900 /* Shared libraries on traditional mips have DT_DEBUG. */
4901 if (!SGI_COMPAT (output_bfd))
4903 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
4908 if (reltext && SGI_COMPAT (output_bfd))
4909 info->flags |= DF_TEXTREL;
4911 if ((info->flags & DF_TEXTREL) != 0)
4913 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
4917 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
4920 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
4922 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
4925 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
4928 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
4932 if (SGI_COMPAT (output_bfd))
4934 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
4938 if (SGI_COMPAT (output_bfd))
4940 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
4944 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
4946 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
4949 s = bfd_get_section_by_name (dynobj, ".liblist");
4950 BFD_ASSERT (s != NULL);
4952 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
4956 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
4959 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
4963 /* Time stamps in executable files are a bad idea. */
4964 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
4969 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
4974 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
4978 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
4981 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
4984 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
4987 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
4990 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
4993 if (IRIX_COMPAT (dynobj) == ict_irix5
4994 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
4997 if (IRIX_COMPAT (dynobj) == ict_irix6
4998 && (bfd_get_section_by_name
4999 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
5000 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
5003 if (bfd_get_section_by_name (dynobj, ".msym")
5004 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
5011 /* Relocate a MIPS ELF section. */
5014 _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
5015 contents, relocs, local_syms, local_sections)
5017 struct bfd_link_info *info;
5019 asection *input_section;
5021 Elf_Internal_Rela *relocs;
5022 Elf_Internal_Sym *local_syms;
5023 asection **local_sections;
5025 Elf_Internal_Rela *rel;
5026 const Elf_Internal_Rela *relend;
5028 boolean use_saved_addend_p = false;
5029 struct elf_backend_data *bed;
5031 bed = get_elf_backend_data (output_bfd);
5032 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
5033 for (rel = relocs; rel < relend; ++rel)
5037 reloc_howto_type *howto;
5038 boolean require_jalx;
5039 /* True if the relocation is a RELA relocation, rather than a
5041 boolean rela_relocation_p = true;
5042 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5043 const char * msg = (const char *) NULL;
5045 /* Find the relocation howto for this relocation. */
5046 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
5048 /* Some 32-bit code uses R_MIPS_64. In particular, people use
5049 64-bit code, but make sure all their addresses are in the
5050 lowermost or uppermost 32-bit section of the 64-bit address
5051 space. Thus, when they use an R_MIPS_64 they mean what is
5052 usually meant by R_MIPS_32, with the exception that the
5053 stored value is sign-extended to 64 bits. */
5054 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
5056 /* On big-endian systems, we need to lie about the position
5058 if (bfd_big_endian (input_bfd))
5062 /* NewABI defaults to RELA relocations. */
5063 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
5064 NEWABI_P (input_bfd)
5065 && (MIPS_RELOC_RELA_P
5066 (input_bfd, input_section,
5069 if (!use_saved_addend_p)
5071 Elf_Internal_Shdr *rel_hdr;
5073 /* If these relocations were originally of the REL variety,
5074 we must pull the addend out of the field that will be
5075 relocated. Otherwise, we simply use the contents of the
5076 RELA relocation. To determine which flavor or relocation
5077 this is, we depend on the fact that the INPUT_SECTION's
5078 REL_HDR is read before its REL_HDR2. */
5079 rel_hdr = &elf_section_data (input_section)->rel_hdr;
5080 if ((size_t) (rel - relocs)
5081 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
5082 rel_hdr = elf_section_data (input_section)->rel_hdr2;
5083 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
5085 /* Note that this is a REL relocation. */
5086 rela_relocation_p = false;
5088 /* Get the addend, which is stored in the input file. */
5089 addend = mips_elf_obtain_contents (howto, rel, input_bfd,
5091 addend &= howto->src_mask;
5092 addend <<= howto->rightshift;
5094 /* For some kinds of relocations, the ADDEND is a
5095 combination of the addend stored in two different
5097 if (r_type == R_MIPS_HI16
5098 || r_type == R_MIPS_GNU_REL_HI16
5099 || (r_type == R_MIPS_GOT16
5100 && mips_elf_local_relocation_p (input_bfd, rel,
5101 local_sections, false)))
5104 const Elf_Internal_Rela *lo16_relocation;
5105 reloc_howto_type *lo16_howto;
5108 /* The combined value is the sum of the HI16 addend,
5109 left-shifted by sixteen bits, and the LO16
5110 addend, sign extended. (Usually, the code does
5111 a `lui' of the HI16 value, and then an `addiu' of
5114 Scan ahead to find a matching LO16 relocation. */
5115 if (r_type == R_MIPS_GNU_REL_HI16)
5116 lo = R_MIPS_GNU_REL_LO16;
5119 lo16_relocation = mips_elf_next_relocation (input_bfd, lo,
5121 if (lo16_relocation == NULL)
5124 /* Obtain the addend kept there. */
5125 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, lo, false);
5126 l = mips_elf_obtain_contents (lo16_howto, lo16_relocation,
5127 input_bfd, contents);
5128 l &= lo16_howto->src_mask;
5129 l <<= lo16_howto->rightshift;
5130 l = mips_elf_sign_extend (l, 16);
5134 /* Compute the combined addend. */
5137 /* If PC-relative, subtract the difference between the
5138 address of the LO part of the reloc and the address of
5139 the HI part. The relocation is relative to the LO
5140 part, but mips_elf_calculate_relocation() doesn't
5141 know its address or the difference from the HI part, so
5142 we subtract that difference here. See also the
5143 comment in mips_elf_calculate_relocation(). */
5144 if (r_type == R_MIPS_GNU_REL_HI16)
5145 addend -= (lo16_relocation->r_offset - rel->r_offset);
5147 else if (r_type == R_MIPS16_GPREL)
5149 /* The addend is scrambled in the object file. See
5150 mips_elf_perform_relocation for details on the
5152 addend = (((addend & 0x1f0000) >> 5)
5153 | ((addend & 0x7e00000) >> 16)
5158 addend = rel->r_addend;
5161 if (info->relocateable)
5163 Elf_Internal_Sym *sym;
5164 unsigned long r_symndx;
5166 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
5167 && bfd_big_endian (input_bfd))
5170 /* Since we're just relocating, all we need to do is copy
5171 the relocations back out to the object file, unless
5172 they're against a section symbol, in which case we need
5173 to adjust by the section offset, or unless they're GP
5174 relative in which case we need to adjust by the amount
5175 that we're adjusting GP in this relocateable object. */
5177 if (! mips_elf_local_relocation_p (input_bfd, rel, local_sections,
5179 /* There's nothing to do for non-local relocations. */
5182 if (r_type == R_MIPS16_GPREL
5183 || r_type == R_MIPS_GPREL16
5184 || r_type == R_MIPS_GPREL32
5185 || r_type == R_MIPS_LITERAL)
5186 addend -= (_bfd_get_gp_value (output_bfd)
5187 - _bfd_get_gp_value (input_bfd));
5189 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
5190 sym = local_syms + r_symndx;
5191 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5192 /* Adjust the addend appropriately. */
5193 addend += local_sections[r_symndx]->output_offset;
5195 if (howto->partial_inplace)
5197 /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
5198 then we only want to write out the high-order 16 bits.
5199 The subsequent R_MIPS_LO16 will handle the low-order bits.
5201 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16
5202 || r_type == R_MIPS_GNU_REL_HI16)
5203 addend = mips_elf_high (addend);
5204 else if (r_type == R_MIPS_HIGHER)
5205 addend = mips_elf_higher (addend);
5206 else if (r_type == R_MIPS_HIGHEST)
5207 addend = mips_elf_highest (addend);
5210 if (rela_relocation_p)
5211 /* If this is a RELA relocation, just update the addend.
5212 We have to cast away constness for REL. */
5213 rel->r_addend = addend;
5216 /* Otherwise, we have to write the value back out. Note
5217 that we use the source mask, rather than the
5218 destination mask because the place to which we are
5219 writing will be source of the addend in the final
5221 addend >>= howto->rightshift;
5222 addend &= howto->src_mask;
5224 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
5225 /* See the comment above about using R_MIPS_64 in the 32-bit
5226 ABI. Here, we need to update the addend. It would be
5227 possible to get away with just using the R_MIPS_32 reloc
5228 but for endianness. */
5234 if (addend & ((bfd_vma) 1 << 31))
5236 sign_bits = ((bfd_vma) 1 << 32) - 1;
5243 /* If we don't know that we have a 64-bit type,
5244 do two separate stores. */
5245 if (bfd_big_endian (input_bfd))
5247 /* Store the sign-bits (which are most significant)
5249 low_bits = sign_bits;
5255 high_bits = sign_bits;
5257 bfd_put_32 (input_bfd, low_bits,
5258 contents + rel->r_offset);
5259 bfd_put_32 (input_bfd, high_bits,
5260 contents + rel->r_offset + 4);
5264 if (! mips_elf_perform_relocation (info, howto, rel, addend,
5265 input_bfd, input_section,
5270 /* Go on to the next relocation. */
5274 /* In the N32 and 64-bit ABIs there may be multiple consecutive
5275 relocations for the same offset. In that case we are
5276 supposed to treat the output of each relocation as the addend
5278 if (rel + 1 < relend
5279 && rel->r_offset == rel[1].r_offset
5280 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
5281 use_saved_addend_p = true;
5283 use_saved_addend_p = false;
5285 addend >>= howto->rightshift;
5287 /* Figure out what value we are supposed to relocate. */
5288 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
5289 input_section, info, rel,
5290 addend, howto, local_syms,
5291 local_sections, &value,
5292 &name, &require_jalx,
5293 use_saved_addend_p))
5295 case bfd_reloc_continue:
5296 /* There's nothing to do. */
5299 case bfd_reloc_undefined:
5300 /* mips_elf_calculate_relocation already called the
5301 undefined_symbol callback. There's no real point in
5302 trying to perform the relocation at this point, so we
5303 just skip ahead to the next relocation. */
5306 case bfd_reloc_notsupported:
5307 msg = _("internal error: unsupported relocation error");
5308 info->callbacks->warning
5309 (info, msg, name, input_bfd, input_section, rel->r_offset);
5312 case bfd_reloc_overflow:
5313 if (use_saved_addend_p)
5314 /* Ignore overflow until we reach the last relocation for
5315 a given location. */
5319 BFD_ASSERT (name != NULL);
5320 if (! ((*info->callbacks->reloc_overflow)
5321 (info, name, howto->name, (bfd_vma) 0,
5322 input_bfd, input_section, rel->r_offset)))
5335 /* If we've got another relocation for the address, keep going
5336 until we reach the last one. */
5337 if (use_saved_addend_p)
5343 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
5344 /* See the comment above about using R_MIPS_64 in the 32-bit
5345 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
5346 that calculated the right value. Now, however, we
5347 sign-extend the 32-bit result to 64-bits, and store it as a
5348 64-bit value. We are especially generous here in that we
5349 go to extreme lengths to support this usage on systems with
5350 only a 32-bit VMA. */
5356 if (value & ((bfd_vma) 1 << 31))
5358 sign_bits = ((bfd_vma) 1 << 32) - 1;
5365 /* If we don't know that we have a 64-bit type,
5366 do two separate stores. */
5367 if (bfd_big_endian (input_bfd))
5369 /* Undo what we did above. */
5371 /* Store the sign-bits (which are most significant)
5373 low_bits = sign_bits;
5379 high_bits = sign_bits;
5381 bfd_put_32 (input_bfd, low_bits,
5382 contents + rel->r_offset);
5383 bfd_put_32 (input_bfd, high_bits,
5384 contents + rel->r_offset + 4);
5388 /* Actually perform the relocation. */
5389 if (! mips_elf_perform_relocation (info, howto, rel, value,
5390 input_bfd, input_section,
5391 contents, require_jalx))
5398 /* If NAME is one of the special IRIX6 symbols defined by the linker,
5399 adjust it appropriately now. */
5402 mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
5403 bfd *abfd ATTRIBUTE_UNUSED;
5405 Elf_Internal_Sym *sym;
5407 /* The linker script takes care of providing names and values for
5408 these, but we must place them into the right sections. */
5409 static const char* const text_section_symbols[] = {
5412 "__dso_displacement",
5414 "__program_header_table",
5418 static const char* const data_section_symbols[] = {
5426 const char* const *p;
5429 for (i = 0; i < 2; ++i)
5430 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
5433 if (strcmp (*p, name) == 0)
5435 /* All of these symbols are given type STT_SECTION by the
5437 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5439 /* The IRIX linker puts these symbols in special sections. */
5441 sym->st_shndx = SHN_MIPS_TEXT;
5443 sym->st_shndx = SHN_MIPS_DATA;
5449 /* Finish up dynamic symbol handling. We set the contents of various
5450 dynamic sections here. */
5453 _bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5455 struct bfd_link_info *info;
5456 struct elf_link_hash_entry *h;
5457 Elf_Internal_Sym *sym;
5463 struct mips_got_info *g;
5465 struct mips_elf_link_hash_entry *mh;
5467 dynobj = elf_hash_table (info)->dynobj;
5468 gval = sym->st_value;
5469 mh = (struct mips_elf_link_hash_entry *) h;
5471 if (h->plt.offset != (bfd_vma) -1)
5474 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
5476 /* This symbol has a stub. Set it up. */
5478 BFD_ASSERT (h->dynindx != -1);
5480 s = bfd_get_section_by_name (dynobj,
5481 MIPS_ELF_STUB_SECTION_NAME (dynobj));
5482 BFD_ASSERT (s != NULL);
5484 /* FIXME: Can h->dynindex be more than 64K? */
5485 if (h->dynindx & 0xffff0000)
5488 /* Fill the stub. */
5489 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub);
5490 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + 4);
5491 bfd_put_32 (output_bfd, STUB_JALR, stub + 8);
5492 bfd_put_32 (output_bfd, STUB_LI16 (output_bfd) + h->dynindx, stub + 12);
5494 BFD_ASSERT (h->plt.offset <= s->_raw_size);
5495 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
5497 /* Mark the symbol as undefined. plt.offset != -1 occurs
5498 only for the referenced symbol. */
5499 sym->st_shndx = SHN_UNDEF;
5501 /* The run-time linker uses the st_value field of the symbol
5502 to reset the global offset table entry for this external
5503 to its stub address when unlinking a shared object. */
5504 gval = s->output_section->vma + s->output_offset + h->plt.offset;
5505 sym->st_value = gval;
5508 BFD_ASSERT (h->dynindx != -1
5509 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0);
5511 sgot = mips_elf_got_section (dynobj);
5512 BFD_ASSERT (sgot != NULL);
5513 BFD_ASSERT (elf_section_data (sgot) != NULL);
5514 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5515 BFD_ASSERT (g != NULL);
5517 /* Run through the global symbol table, creating GOT entries for all
5518 the symbols that need them. */
5519 if (g->global_gotsym != NULL
5520 && h->dynindx >= g->global_gotsym->dynindx)
5526 value = sym->st_value;
5529 /* For an entity defined in a shared object, this will be
5530 NULL. (For functions in shared objects for
5531 which we have created stubs, ST_VALUE will be non-NULL.
5532 That's because such the functions are now no longer defined
5533 in a shared object.) */
5535 if (info->shared && h->root.type == bfd_link_hash_undefined)
5538 value = h->root.u.def.value;
5540 offset = mips_elf_global_got_index (dynobj, h);
5541 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
5544 /* Create a .msym entry, if appropriate. */
5545 smsym = bfd_get_section_by_name (dynobj, ".msym");
5548 Elf32_Internal_Msym msym;
5550 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
5551 /* It is undocumented what the `1' indicates, but IRIX6 uses
5553 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
5554 bfd_mips_elf_swap_msym_out
5556 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
5559 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5560 name = h->root.root.string;
5561 if (strcmp (name, "_DYNAMIC") == 0
5562 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
5563 sym->st_shndx = SHN_ABS;
5564 else if (strcmp (name, "_DYNAMIC_LINK") == 0
5565 || strcmp (name, "_DYNAMIC_LINKING") == 0)
5567 sym->st_shndx = SHN_ABS;
5568 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5571 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
5573 sym->st_shndx = SHN_ABS;
5574 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5575 sym->st_value = elf_gp (output_bfd);
5577 else if (SGI_COMPAT (output_bfd))
5579 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
5580 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
5582 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5583 sym->st_other = STO_PROTECTED;
5585 sym->st_shndx = SHN_MIPS_DATA;
5587 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
5589 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5590 sym->st_other = STO_PROTECTED;
5591 sym->st_value = mips_elf_hash_table (info)->procedure_count;
5592 sym->st_shndx = SHN_ABS;
5594 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
5596 if (h->type == STT_FUNC)
5597 sym->st_shndx = SHN_MIPS_TEXT;
5598 else if (h->type == STT_OBJECT)
5599 sym->st_shndx = SHN_MIPS_DATA;
5603 /* Handle the IRIX6-specific symbols. */
5604 if (IRIX_COMPAT (output_bfd) == ict_irix6)
5605 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
5609 if (! mips_elf_hash_table (info)->use_rld_obj_head
5610 && (strcmp (name, "__rld_map") == 0
5611 || strcmp (name, "__RLD_MAP") == 0))
5613 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
5614 BFD_ASSERT (s != NULL);
5615 sym->st_value = s->output_section->vma + s->output_offset;
5616 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
5617 if (mips_elf_hash_table (info)->rld_value == 0)
5618 mips_elf_hash_table (info)->rld_value = sym->st_value;
5620 else if (mips_elf_hash_table (info)->use_rld_obj_head
5621 && strcmp (name, "__rld_obj_head") == 0)
5623 /* IRIX6 does not use a .rld_map section. */
5624 if (IRIX_COMPAT (output_bfd) == ict_irix5
5625 || IRIX_COMPAT (output_bfd) == ict_none)
5626 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
5628 mips_elf_hash_table (info)->rld_value = sym->st_value;
5632 /* If this is a mips16 symbol, force the value to be even. */
5633 if (sym->st_other == STO_MIPS16
5634 && (sym->st_value & 1) != 0)
5640 /* Finish up the dynamic sections. */
5643 _bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
5645 struct bfd_link_info *info;
5650 struct mips_got_info *g;
5652 dynobj = elf_hash_table (info)->dynobj;
5654 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5656 sgot = bfd_get_section_by_name (dynobj, ".got");
5661 BFD_ASSERT (elf_section_data (sgot) != NULL);
5662 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5663 BFD_ASSERT (g != NULL);
5666 if (elf_hash_table (info)->dynamic_sections_created)
5670 BFD_ASSERT (sdyn != NULL);
5671 BFD_ASSERT (g != NULL);
5673 for (b = sdyn->contents;
5674 b < sdyn->contents + sdyn->_raw_size;
5675 b += MIPS_ELF_DYN_SIZE (dynobj))
5677 Elf_Internal_Dyn dyn;
5683 /* Read in the current dynamic entry. */
5684 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
5686 /* Assume that we're going to modify it and write it out. */
5692 s = (bfd_get_section_by_name (dynobj, ".rel.dyn"));
5693 BFD_ASSERT (s != NULL);
5694 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
5698 /* Rewrite DT_STRSZ. */
5700 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5706 case DT_MIPS_CONFLICT:
5709 case DT_MIPS_LIBLIST:
5712 s = bfd_get_section_by_name (output_bfd, name);
5713 BFD_ASSERT (s != NULL);
5714 dyn.d_un.d_ptr = s->vma;
5717 case DT_MIPS_RLD_VERSION:
5718 dyn.d_un.d_val = 1; /* XXX */
5722 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
5725 case DT_MIPS_CONFLICTNO:
5727 elemsize = sizeof (Elf32_Conflict);
5730 case DT_MIPS_LIBLISTNO:
5732 elemsize = sizeof (Elf32_Lib);
5734 s = bfd_get_section_by_name (output_bfd, name);
5737 if (s->_cooked_size != 0)
5738 dyn.d_un.d_val = s->_cooked_size / elemsize;
5740 dyn.d_un.d_val = s->_raw_size / elemsize;
5746 case DT_MIPS_TIME_STAMP:
5747 time ((time_t *) &dyn.d_un.d_val);
5750 case DT_MIPS_ICHECKSUM:
5755 case DT_MIPS_IVERSION:
5760 case DT_MIPS_BASE_ADDRESS:
5761 s = output_bfd->sections;
5762 BFD_ASSERT (s != NULL);
5763 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
5766 case DT_MIPS_LOCAL_GOTNO:
5767 dyn.d_un.d_val = g->local_gotno;
5770 case DT_MIPS_UNREFEXTNO:
5771 /* The index into the dynamic symbol table which is the
5772 entry of the first external symbol that is not
5773 referenced within the same object. */
5774 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
5777 case DT_MIPS_GOTSYM:
5778 if (g->global_gotsym)
5780 dyn.d_un.d_val = g->global_gotsym->dynindx;
5783 /* In case if we don't have global got symbols we default
5784 to setting DT_MIPS_GOTSYM to the same value as
5785 DT_MIPS_SYMTABNO, so we just fall through. */
5787 case DT_MIPS_SYMTABNO:
5789 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
5790 s = bfd_get_section_by_name (output_bfd, name);
5791 BFD_ASSERT (s != NULL);
5793 if (s->_cooked_size != 0)
5794 dyn.d_un.d_val = s->_cooked_size / elemsize;
5796 dyn.d_un.d_val = s->_raw_size / elemsize;
5799 case DT_MIPS_HIPAGENO:
5800 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
5803 case DT_MIPS_RLD_MAP:
5804 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
5807 case DT_MIPS_OPTIONS:
5808 s = (bfd_get_section_by_name
5809 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
5810 dyn.d_un.d_ptr = s->vma;
5814 s = (bfd_get_section_by_name (output_bfd, ".msym"));
5815 dyn.d_un.d_ptr = s->vma;
5824 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
5829 /* The first entry of the global offset table will be filled at
5830 runtime. The second entry will be used by some runtime loaders.
5831 This isn't the case of IRIX rld. */
5832 if (sgot != NULL && sgot->_raw_size > 0)
5834 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
5835 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
5836 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
5840 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
5841 = MIPS_ELF_GOT_SIZE (output_bfd);
5846 Elf32_compact_rel cpt;
5848 /* ??? The section symbols for the output sections were set up in
5849 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
5850 symbols. Should we do so? */
5852 smsym = bfd_get_section_by_name (dynobj, ".msym");
5855 Elf32_Internal_Msym msym;
5857 msym.ms_hash_value = 0;
5858 msym.ms_info = ELF32_MS_INFO (0, 1);
5860 for (s = output_bfd->sections; s != NULL; s = s->next)
5862 long dynindx = elf_section_data (s)->dynindx;
5864 bfd_mips_elf_swap_msym_out
5866 (((Elf32_External_Msym *) smsym->contents)
5871 if (SGI_COMPAT (output_bfd))
5873 /* Write .compact_rel section out. */
5874 s = bfd_get_section_by_name (dynobj, ".compact_rel");
5878 cpt.num = s->reloc_count;
5880 cpt.offset = (s->output_section->filepos
5881 + sizeof (Elf32_External_compact_rel));
5884 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
5885 ((Elf32_External_compact_rel *)
5888 /* Clean up a dummy stub function entry in .text. */
5889 s = bfd_get_section_by_name (dynobj,
5890 MIPS_ELF_STUB_SECTION_NAME (dynobj));
5893 file_ptr dummy_offset;
5895 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
5896 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
5897 memset (s->contents + dummy_offset, 0,
5898 MIPS_FUNCTION_STUB_SIZE);
5903 /* We need to sort the entries of the dynamic relocation section. */
5905 if (!ABI_64_P (output_bfd))
5909 reldyn = bfd_get_section_by_name (dynobj, ".rel.dyn");
5910 if (reldyn != NULL && reldyn->reloc_count > 2)
5912 reldyn_sorting_bfd = output_bfd;
5913 qsort ((Elf32_External_Rel *) reldyn->contents + 1,
5914 (size_t) reldyn->reloc_count - 1,
5915 sizeof (Elf32_External_Rel), sort_dynamic_relocs);
5919 /* Clean up a first relocation in .rel.dyn. */
5920 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5921 if (s != NULL && s->_raw_size > 0)
5922 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
5928 /* The final processing done just before writing out a MIPS ELF object
5929 file. This gets the MIPS architecture right based on the machine
5930 number. This is used by both the 32-bit and the 64-bit ABI. */
5933 _bfd_mips_elf_final_write_processing (abfd, linker)
5935 boolean linker ATTRIBUTE_UNUSED;
5939 Elf_Internal_Shdr **hdrpp;
5943 switch (bfd_get_mach (abfd))
5946 case bfd_mach_mips3000:
5947 val = E_MIPS_ARCH_1;
5950 case bfd_mach_mips3900:
5951 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
5954 case bfd_mach_mips6000:
5955 val = E_MIPS_ARCH_2;
5958 case bfd_mach_mips4000:
5959 case bfd_mach_mips4300:
5960 case bfd_mach_mips4400:
5961 case bfd_mach_mips4600:
5962 val = E_MIPS_ARCH_3;
5965 case bfd_mach_mips4010:
5966 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
5969 case bfd_mach_mips4100:
5970 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
5973 case bfd_mach_mips4111:
5974 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
5977 case bfd_mach_mips4120:
5978 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
5981 case bfd_mach_mips4650:
5982 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
5985 case bfd_mach_mips5400:
5986 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
5989 case bfd_mach_mips5500:
5990 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
5993 case bfd_mach_mips5000:
5994 case bfd_mach_mips8000:
5995 case bfd_mach_mips10000:
5996 case bfd_mach_mips12000:
5997 val = E_MIPS_ARCH_4;
6000 case bfd_mach_mips5:
6001 val = E_MIPS_ARCH_5;
6004 case bfd_mach_mips_sb1:
6005 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
6008 case bfd_mach_mipsisa32:
6009 val = E_MIPS_ARCH_32;
6012 case bfd_mach_mipsisa64:
6013 val = E_MIPS_ARCH_64;
6016 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
6017 elf_elfheader (abfd)->e_flags |= val;
6019 /* Set the sh_info field for .gptab sections and other appropriate
6020 info for each special section. */
6021 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
6022 i < elf_numsections (abfd);
6025 switch ((*hdrpp)->sh_type)
6028 case SHT_MIPS_LIBLIST:
6029 sec = bfd_get_section_by_name (abfd, ".dynstr");
6031 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6034 case SHT_MIPS_GPTAB:
6035 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
6036 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
6037 BFD_ASSERT (name != NULL
6038 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
6039 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
6040 BFD_ASSERT (sec != NULL);
6041 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
6044 case SHT_MIPS_CONTENT:
6045 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
6046 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
6047 BFD_ASSERT (name != NULL
6048 && strncmp (name, ".MIPS.content",
6049 sizeof ".MIPS.content" - 1) == 0);
6050 sec = bfd_get_section_by_name (abfd,
6051 name + sizeof ".MIPS.content" - 1);
6052 BFD_ASSERT (sec != NULL);
6053 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6056 case SHT_MIPS_SYMBOL_LIB:
6057 sec = bfd_get_section_by_name (abfd, ".dynsym");
6059 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6060 sec = bfd_get_section_by_name (abfd, ".liblist");
6062 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
6065 case SHT_MIPS_EVENTS:
6066 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
6067 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
6068 BFD_ASSERT (name != NULL);
6069 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
6070 sec = bfd_get_section_by_name (abfd,
6071 name + sizeof ".MIPS.events" - 1);
6074 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
6075 sizeof ".MIPS.post_rel" - 1) == 0);
6076 sec = bfd_get_section_by_name (abfd,
6078 + sizeof ".MIPS.post_rel" - 1));
6080 BFD_ASSERT (sec != NULL);
6081 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6088 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
6092 _bfd_mips_elf_additional_program_headers (abfd)
6098 /* See if we need a PT_MIPS_REGINFO segment. */
6099 s = bfd_get_section_by_name (abfd, ".reginfo");
6100 if (s && (s->flags & SEC_LOAD))
6103 /* See if we need a PT_MIPS_OPTIONS segment. */
6104 if (IRIX_COMPAT (abfd) == ict_irix6
6105 && bfd_get_section_by_name (abfd,
6106 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
6109 /* See if we need a PT_MIPS_RTPROC segment. */
6110 if (IRIX_COMPAT (abfd) == ict_irix5
6111 && bfd_get_section_by_name (abfd, ".dynamic")
6112 && bfd_get_section_by_name (abfd, ".mdebug"))
6118 /* Modify the segment map for an IRIX5 executable. */
6121 _bfd_mips_elf_modify_segment_map (abfd)
6125 struct elf_segment_map *m, **pm;
6128 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
6130 s = bfd_get_section_by_name (abfd, ".reginfo");
6131 if (s != NULL && (s->flags & SEC_LOAD) != 0)
6133 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
6134 if (m->p_type == PT_MIPS_REGINFO)
6139 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6143 m->p_type = PT_MIPS_REGINFO;
6147 /* We want to put it after the PHDR and INTERP segments. */
6148 pm = &elf_tdata (abfd)->segment_map;
6150 && ((*pm)->p_type == PT_PHDR
6151 || (*pm)->p_type == PT_INTERP))
6159 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
6160 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
6161 PT_OPTIONS segment immediately following the program header
6164 /* On non-IRIX6 new abi, we'll have already created a segment
6165 for this section, so don't create another. I'm not sure this
6166 is not also the case for IRIX 6, but I can't test it right
6168 && IRIX_COMPAT (abfd) == ict_irix6)
6170 for (s = abfd->sections; s; s = s->next)
6171 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
6176 struct elf_segment_map *options_segment;
6178 /* Usually, there's a program header table. But, sometimes
6179 there's not (like when running the `ld' testsuite). So,
6180 if there's no program header table, we just put the
6181 options segment at the end. */
6182 for (pm = &elf_tdata (abfd)->segment_map;
6185 if ((*pm)->p_type == PT_PHDR)
6188 amt = sizeof (struct elf_segment_map);
6189 options_segment = bfd_zalloc (abfd, amt);
6190 options_segment->next = *pm;
6191 options_segment->p_type = PT_MIPS_OPTIONS;
6192 options_segment->p_flags = PF_R;
6193 options_segment->p_flags_valid = true;
6194 options_segment->count = 1;
6195 options_segment->sections[0] = s;
6196 *pm = options_segment;
6201 if (IRIX_COMPAT (abfd) == ict_irix5)
6203 /* If there are .dynamic and .mdebug sections, we make a room
6204 for the RTPROC header. FIXME: Rewrite without section names. */
6205 if (bfd_get_section_by_name (abfd, ".interp") == NULL
6206 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
6207 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
6209 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
6210 if (m->p_type == PT_MIPS_RTPROC)
6215 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6219 m->p_type = PT_MIPS_RTPROC;
6221 s = bfd_get_section_by_name (abfd, ".rtproc");
6226 m->p_flags_valid = 1;
6234 /* We want to put it after the DYNAMIC segment. */
6235 pm = &elf_tdata (abfd)->segment_map;
6236 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
6246 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
6247 .dynstr, .dynsym, and .hash sections, and everything in
6249 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
6251 if ((*pm)->p_type == PT_DYNAMIC)
6254 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
6256 /* For a normal mips executable the permissions for the PT_DYNAMIC
6257 segment are read, write and execute. We do that here since
6258 the code in elf.c sets only the read permission. This matters
6259 sometimes for the dynamic linker. */
6260 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
6262 m->p_flags = PF_R | PF_W | PF_X;
6263 m->p_flags_valid = 1;
6267 && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
6269 static const char *sec_names[] =
6271 ".dynamic", ".dynstr", ".dynsym", ".hash"
6275 struct elf_segment_map *n;
6279 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
6281 s = bfd_get_section_by_name (abfd, sec_names[i]);
6282 if (s != NULL && (s->flags & SEC_LOAD) != 0)
6288 sz = s->_cooked_size;
6291 if (high < s->vma + sz)
6297 for (s = abfd->sections; s != NULL; s = s->next)
6298 if ((s->flags & SEC_LOAD) != 0
6301 + (s->_cooked_size !=
6302 0 ? s->_cooked_size : s->_raw_size)) <= high))
6305 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
6306 n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6313 for (s = abfd->sections; s != NULL; s = s->next)
6315 if ((s->flags & SEC_LOAD) != 0
6318 + (s->_cooked_size != 0 ?
6319 s->_cooked_size : s->_raw_size)) <= high))
6333 /* Return the section that should be marked against GC for a given
6337 _bfd_mips_elf_gc_mark_hook (sec, info, rel, h, sym)
6339 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6340 Elf_Internal_Rela *rel;
6341 struct elf_link_hash_entry *h;
6342 Elf_Internal_Sym *sym;
6344 /* ??? Do mips16 stub sections need to be handled special? */
6348 switch (ELF_R_TYPE (sec->owner, rel->r_info))
6350 case R_MIPS_GNU_VTINHERIT:
6351 case R_MIPS_GNU_VTENTRY:
6355 switch (h->root.type)
6357 case bfd_link_hash_defined:
6358 case bfd_link_hash_defweak:
6359 return h->root.u.def.section;
6361 case bfd_link_hash_common:
6362 return h->root.u.c.p->section;
6370 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6375 /* Update the got entry reference counts for the section being removed. */
6378 _bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6379 bfd *abfd ATTRIBUTE_UNUSED;
6380 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6381 asection *sec ATTRIBUTE_UNUSED;
6382 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
6385 Elf_Internal_Shdr *symtab_hdr;
6386 struct elf_link_hash_entry **sym_hashes;
6387 bfd_signed_vma *local_got_refcounts;
6388 const Elf_Internal_Rela *rel, *relend;
6389 unsigned long r_symndx;
6390 struct elf_link_hash_entry *h;
6392 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6393 sym_hashes = elf_sym_hashes (abfd);
6394 local_got_refcounts = elf_local_got_refcounts (abfd);
6396 relend = relocs + sec->reloc_count;
6397 for (rel = relocs; rel < relend; rel++)
6398 switch (ELF_R_TYPE (abfd, rel->r_info))
6402 case R_MIPS_CALL_HI16:
6403 case R_MIPS_CALL_LO16:
6404 case R_MIPS_GOT_HI16:
6405 case R_MIPS_GOT_LO16:
6406 case R_MIPS_GOT_DISP:
6407 case R_MIPS_GOT_PAGE:
6408 case R_MIPS_GOT_OFST:
6409 /* ??? It would seem that the existing MIPS code does no sort
6410 of reference counting or whatnot on its GOT and PLT entries,
6411 so it is not possible to garbage collect them at this time. */
6422 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
6423 hiding the old indirect symbol. Process additional relocation
6424 information. Also called for weakdefs, in which case we just let
6425 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
6428 _bfd_mips_elf_copy_indirect_symbol (bed, dir, ind)
6429 struct elf_backend_data *bed;
6430 struct elf_link_hash_entry *dir, *ind;
6432 struct mips_elf_link_hash_entry *dirmips, *indmips;
6434 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
6436 if (ind->root.type != bfd_link_hash_indirect)
6439 dirmips = (struct mips_elf_link_hash_entry *) dir;
6440 indmips = (struct mips_elf_link_hash_entry *) ind;
6441 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
6442 if (indmips->readonly_reloc)
6443 dirmips->readonly_reloc = true;
6444 if (dirmips->min_dyn_reloc_index == 0
6445 || (indmips->min_dyn_reloc_index != 0
6446 && indmips->min_dyn_reloc_index < dirmips->min_dyn_reloc_index))
6447 dirmips->min_dyn_reloc_index = indmips->min_dyn_reloc_index;
6448 if (indmips->no_fn_stub)
6449 dirmips->no_fn_stub = true;
6453 _bfd_mips_elf_hide_symbol (info, entry, force_local)
6454 struct bfd_link_info *info;
6455 struct elf_link_hash_entry *entry;
6456 boolean force_local;
6460 struct mips_got_info *g;
6461 struct mips_elf_link_hash_entry *h;
6463 h = (struct mips_elf_link_hash_entry *) entry;
6464 if (h->forced_local)
6466 h->forced_local = true;
6468 dynobj = elf_hash_table (info)->dynobj;
6469 got = bfd_get_section_by_name (dynobj, ".got");
6470 g = (struct mips_got_info *) elf_section_data (got)->tdata;
6472 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
6474 /* FIXME: Do we allocate too much GOT space here? */
6476 got->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
6482 _bfd_mips_elf_discard_info (abfd, cookie, info)
6484 struct elf_reloc_cookie *cookie;
6485 struct bfd_link_info *info;
6488 boolean ret = false;
6489 unsigned char *tdata;
6492 o = bfd_get_section_by_name (abfd, ".pdr");
6495 if (o->_raw_size == 0)
6497 if (o->_raw_size % PDR_SIZE != 0)
6499 if (o->output_section != NULL
6500 && bfd_is_abs_section (o->output_section))
6503 tdata = bfd_zmalloc (o->_raw_size / PDR_SIZE);
6507 cookie->rels = (MNAME(abfd,_bfd_elf,link_read_relocs)
6508 (abfd, o, (PTR) NULL,
6509 (Elf_Internal_Rela *) NULL,
6510 info->keep_memory));
6517 cookie->rel = cookie->rels;
6518 cookie->relend = cookie->rels + o->reloc_count;
6520 for (i = 0, skip = 0; i < o->_raw_size; i ++)
6522 if (MNAME(abfd,_bfd_elf,reloc_symbol_deleted_p) (i * PDR_SIZE, cookie))
6531 elf_section_data (o)->tdata = tdata;
6532 o->_cooked_size = o->_raw_size - skip * PDR_SIZE;
6538 if (! info->keep_memory)
6539 free (cookie->rels);
6545 _bfd_mips_elf_ignore_discarded_relocs (sec)
6548 if (strcmp (sec->name, ".pdr") == 0)
6554 _bfd_mips_elf_write_section (output_bfd, sec, contents)
6559 bfd_byte *to, *from, *end;
6562 if (strcmp (sec->name, ".pdr") != 0)
6565 if (elf_section_data (sec)->tdata == NULL)
6569 end = contents + sec->_raw_size;
6570 for (from = contents, i = 0;
6572 from += PDR_SIZE, i++)
6574 if (((unsigned char *) elf_section_data (sec)->tdata)[i] == 1)
6577 memcpy (to, from, PDR_SIZE);
6580 bfd_set_section_contents (output_bfd, sec->output_section, contents,
6581 (file_ptr) sec->output_offset,
6586 /* MIPS ELF uses a special find_nearest_line routine in order the
6587 handle the ECOFF debugging information. */
6589 struct mips_elf_find_line
6591 struct ecoff_debug_info d;
6592 struct ecoff_find_line i;
6596 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
6597 functionname_ptr, line_ptr)
6602 const char **filename_ptr;
6603 const char **functionname_ptr;
6604 unsigned int *line_ptr;
6608 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6609 filename_ptr, functionname_ptr,
6613 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6614 filename_ptr, functionname_ptr,
6616 (unsigned) (ABI_64_P (abfd) ? 8 : 0),
6617 &elf_tdata (abfd)->dwarf2_find_line_info))
6620 msec = bfd_get_section_by_name (abfd, ".mdebug");
6624 struct mips_elf_find_line *fi;
6625 const struct ecoff_debug_swap * const swap =
6626 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
6628 /* If we are called during a link, mips_elf_final_link may have
6629 cleared the SEC_HAS_CONTENTS field. We force it back on here
6630 if appropriate (which it normally will be). */
6631 origflags = msec->flags;
6632 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
6633 msec->flags |= SEC_HAS_CONTENTS;
6635 fi = elf_tdata (abfd)->find_line_info;
6638 bfd_size_type external_fdr_size;
6641 struct fdr *fdr_ptr;
6642 bfd_size_type amt = sizeof (struct mips_elf_find_line);
6644 fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
6647 msec->flags = origflags;
6651 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
6653 msec->flags = origflags;
6657 /* Swap in the FDR information. */
6658 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
6659 fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
6660 if (fi->d.fdr == NULL)
6662 msec->flags = origflags;
6665 external_fdr_size = swap->external_fdr_size;
6666 fdr_ptr = fi->d.fdr;
6667 fraw_src = (char *) fi->d.external_fdr;
6668 fraw_end = (fraw_src
6669 + fi->d.symbolic_header.ifdMax * external_fdr_size);
6670 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
6671 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
6673 elf_tdata (abfd)->find_line_info = fi;
6675 /* Note that we don't bother to ever free this information.
6676 find_nearest_line is either called all the time, as in
6677 objdump -l, so the information should be saved, or it is
6678 rarely called, as in ld error messages, so the memory
6679 wasted is unimportant. Still, it would probably be a
6680 good idea for free_cached_info to throw it away. */
6683 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
6684 &fi->i, filename_ptr, functionname_ptr,
6687 msec->flags = origflags;
6691 msec->flags = origflags;
6694 /* Fall back on the generic ELF find_nearest_line routine. */
6696 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
6697 filename_ptr, functionname_ptr,
6701 /* When are writing out the .options or .MIPS.options section,
6702 remember the bytes we are writing out, so that we can install the
6703 GP value in the section_processing routine. */
6706 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
6711 bfd_size_type count;
6713 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
6717 if (elf_section_data (section) == NULL)
6719 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
6720 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
6721 if (elf_section_data (section) == NULL)
6724 c = (bfd_byte *) elf_section_data (section)->tdata;
6729 if (section->_cooked_size != 0)
6730 size = section->_cooked_size;
6732 size = section->_raw_size;
6733 c = (bfd_byte *) bfd_zalloc (abfd, size);
6736 elf_section_data (section)->tdata = (PTR) c;
6739 memcpy (c + offset, location, (size_t) count);
6742 return _bfd_elf_set_section_contents (abfd, section, location, offset,
6746 /* This is almost identical to bfd_generic_get_... except that some
6747 MIPS relocations need to be handled specially. Sigh. */
6750 _bfd_elf_mips_get_relocated_section_contents (abfd, link_info, link_order,
6751 data, relocateable, symbols)
6753 struct bfd_link_info *link_info;
6754 struct bfd_link_order *link_order;
6756 boolean relocateable;
6759 /* Get enough memory to hold the stuff */
6760 bfd *input_bfd = link_order->u.indirect.section->owner;
6761 asection *input_section = link_order->u.indirect.section;
6763 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
6764 arelent **reloc_vector = NULL;
6770 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
6771 if (reloc_vector == NULL && reloc_size != 0)
6774 /* read in the section */
6775 if (!bfd_get_section_contents (input_bfd,
6779 input_section->_raw_size))
6782 /* We're not relaxing the section, so just copy the size info */
6783 input_section->_cooked_size = input_section->_raw_size;
6784 input_section->reloc_done = true;
6786 reloc_count = bfd_canonicalize_reloc (input_bfd,
6790 if (reloc_count < 0)
6793 if (reloc_count > 0)
6798 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
6801 struct bfd_hash_entry *h;
6802 struct bfd_link_hash_entry *lh;
6803 /* Skip all this stuff if we aren't mixing formats. */
6804 if (abfd && input_bfd
6805 && abfd->xvec == input_bfd->xvec)
6809 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
6810 lh = (struct bfd_link_hash_entry *) h;
6817 case bfd_link_hash_undefined:
6818 case bfd_link_hash_undefweak:
6819 case bfd_link_hash_common:
6822 case bfd_link_hash_defined:
6823 case bfd_link_hash_defweak:
6825 gp = lh->u.def.value;
6827 case bfd_link_hash_indirect:
6828 case bfd_link_hash_warning:
6830 /* @@FIXME ignoring warning for now */
6832 case bfd_link_hash_new:
6841 for (parent = reloc_vector; *parent != (arelent *) NULL;
6844 char *error_message = (char *) NULL;
6845 bfd_reloc_status_type r;
6847 /* Specific to MIPS: Deal with relocation types that require
6848 knowing the gp of the output bfd. */
6849 asymbol *sym = *(*parent)->sym_ptr_ptr;
6850 if (bfd_is_abs_section (sym->section) && abfd)
6852 /* The special_function wouldn't get called anyway. */
6856 /* The gp isn't there; let the special function code
6857 fall over on its own. */
6859 else if ((*parent)->howto->special_function
6860 == _bfd_mips_elf32_gprel16_reloc)
6862 /* bypass special_function call */
6863 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
6864 input_section, relocateable,
6866 goto skip_bfd_perform_relocation;
6868 /* end mips specific stuff */
6870 r = bfd_perform_relocation (input_bfd,
6874 relocateable ? abfd : (bfd *) NULL,
6876 skip_bfd_perform_relocation:
6880 asection *os = input_section->output_section;
6882 /* A partial link, so keep the relocs */
6883 os->orelocation[os->reloc_count] = *parent;
6887 if (r != bfd_reloc_ok)
6891 case bfd_reloc_undefined:
6892 if (!((*link_info->callbacks->undefined_symbol)
6893 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6894 input_bfd, input_section, (*parent)->address,
6898 case bfd_reloc_dangerous:
6899 BFD_ASSERT (error_message != (char *) NULL);
6900 if (!((*link_info->callbacks->reloc_dangerous)
6901 (link_info, error_message, input_bfd, input_section,
6902 (*parent)->address)))
6905 case bfd_reloc_overflow:
6906 if (!((*link_info->callbacks->reloc_overflow)
6907 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6908 (*parent)->howto->name, (*parent)->addend,
6909 input_bfd, input_section, (*parent)->address)))
6912 case bfd_reloc_outofrange:
6921 if (reloc_vector != NULL)
6922 free (reloc_vector);
6926 if (reloc_vector != NULL)
6927 free (reloc_vector);
6931 /* Create a MIPS ELF linker hash table. */
6933 struct bfd_link_hash_table *
6934 _bfd_mips_elf_link_hash_table_create (abfd)
6937 struct mips_elf_link_hash_table *ret;
6938 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
6940 ret = (struct mips_elf_link_hash_table *) bfd_malloc (amt);
6941 if (ret == (struct mips_elf_link_hash_table *) NULL)
6944 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
6945 mips_elf_link_hash_newfunc))
6952 /* We no longer use this. */
6953 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
6954 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
6956 ret->procedure_count = 0;
6957 ret->compact_rel_size = 0;
6958 ret->use_rld_obj_head = false;
6960 ret->mips16_stubs_seen = false;
6962 return &ret->root.root;
6965 /* We need to use a special link routine to handle the .reginfo and
6966 the .mdebug sections. We need to merge all instances of these
6967 sections together, not write them all out sequentially. */
6970 _bfd_mips_elf_final_link (abfd, info)
6972 struct bfd_link_info *info;
6976 struct bfd_link_order *p;
6977 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
6978 asection *rtproc_sec;
6979 Elf32_RegInfo reginfo;
6980 struct ecoff_debug_info debug;
6981 const struct ecoff_debug_swap *swap
6982 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
6983 HDRR *symhdr = &debug.symbolic_header;
6984 PTR mdebug_handle = NULL;
6990 static const char * const secname[] =
6992 ".text", ".init", ".fini", ".data",
6993 ".rodata", ".sdata", ".sbss", ".bss"
6995 static const int sc[] =
6997 scText, scInit, scFini, scData,
6998 scRData, scSData, scSBss, scBss
7001 /* If all the things we linked together were PIC, but we're
7002 producing an executable (rather than a shared object), then the
7003 resulting file is CPIC (i.e., it calls PIC code.) */
7005 && !info->relocateable
7006 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
7008 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
7009 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
7012 /* We'd carefully arranged the dynamic symbol indices, and then the
7013 generic size_dynamic_sections renumbered them out from under us.
7014 Rather than trying somehow to prevent the renumbering, just do
7016 if (elf_hash_table (info)->dynamic_sections_created)
7020 struct mips_got_info *g;
7022 /* When we resort, we must tell mips_elf_sort_hash_table what
7023 the lowest index it may use is. That's the number of section
7024 symbols we're going to add. The generic ELF linker only
7025 adds these symbols when building a shared object. Note that
7026 we count the sections after (possibly) removing the .options
7028 if (! mips_elf_sort_hash_table (info, (info->shared
7029 ? bfd_count_sections (abfd) + 1
7033 /* Make sure we didn't grow the global .got region. */
7034 dynobj = elf_hash_table (info)->dynobj;
7035 got = bfd_get_section_by_name (dynobj, ".got");
7036 g = (struct mips_got_info *) elf_section_data (got)->tdata;
7038 if (g->global_gotsym != NULL)
7039 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
7040 - g->global_gotsym->dynindx)
7041 <= g->global_gotno);
7045 /* We want to set the GP value for ld -r. */
7046 /* On IRIX5, we omit the .options section. On IRIX6, however, we
7047 include it, even though we don't process it quite right. (Some
7048 entries are supposed to be merged.) Empirically, we seem to be
7049 better off including it then not. */
7050 if (IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
7051 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
7053 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
7055 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
7056 if (p->type == bfd_indirect_link_order)
7057 p->u.indirect.section->flags &= ~SEC_HAS_CONTENTS;
7058 (*secpp)->link_order_head = NULL;
7059 bfd_section_list_remove (abfd, secpp);
7060 --abfd->section_count;
7066 /* We include .MIPS.options, even though we don't process it quite right.
7067 (Some entries are supposed to be merged.) At IRIX6 empirically we seem
7068 to be better off including it than not. */
7069 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
7071 if (strcmp ((*secpp)->name, ".MIPS.options") == 0)
7073 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
7074 if (p->type == bfd_indirect_link_order)
7075 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
7076 (*secpp)->link_order_head = NULL;
7077 bfd_section_list_remove (abfd, secpp);
7078 --abfd->section_count;
7085 /* Get a value for the GP register. */
7086 if (elf_gp (abfd) == 0)
7088 struct bfd_link_hash_entry *h;
7090 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
7091 if (h != (struct bfd_link_hash_entry *) NULL
7092 && h->type == bfd_link_hash_defined)
7093 elf_gp (abfd) = (h->u.def.value
7094 + h->u.def.section->output_section->vma
7095 + h->u.def.section->output_offset);
7096 else if (info->relocateable)
7098 bfd_vma lo = MINUS_ONE;
7100 /* Find the GP-relative section with the lowest offset. */
7101 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
7103 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
7106 /* And calculate GP relative to that. */
7107 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
7111 /* If the relocate_section function needs to do a reloc
7112 involving the GP value, it should make a reloc_dangerous
7113 callback to warn that GP is not defined. */
7117 /* Go through the sections and collect the .reginfo and .mdebug
7121 gptab_data_sec = NULL;
7122 gptab_bss_sec = NULL;
7123 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
7125 if (strcmp (o->name, ".reginfo") == 0)
7127 memset (®info, 0, sizeof reginfo);
7129 /* We have found the .reginfo section in the output file.
7130 Look through all the link_orders comprising it and merge
7131 the information together. */
7132 for (p = o->link_order_head;
7133 p != (struct bfd_link_order *) NULL;
7136 asection *input_section;
7138 Elf32_External_RegInfo ext;
7141 if (p->type != bfd_indirect_link_order)
7143 if (p->type == bfd_data_link_order)
7148 input_section = p->u.indirect.section;
7149 input_bfd = input_section->owner;
7151 /* The linker emulation code has probably clobbered the
7152 size to be zero bytes. */
7153 if (input_section->_raw_size == 0)
7154 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
7156 if (! bfd_get_section_contents (input_bfd, input_section,
7159 (bfd_size_type) sizeof ext))
7162 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
7164 reginfo.ri_gprmask |= sub.ri_gprmask;
7165 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
7166 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
7167 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
7168 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
7170 /* ri_gp_value is set by the function
7171 mips_elf32_section_processing when the section is
7172 finally written out. */
7174 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7175 elf_link_input_bfd ignores this section. */
7176 input_section->flags &= ~SEC_HAS_CONTENTS;
7179 /* Size has been set in _bfd_mips_elf_always_size_sections. */
7180 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
7182 /* Skip this section later on (I don't think this currently
7183 matters, but someday it might). */
7184 o->link_order_head = (struct bfd_link_order *) NULL;
7189 if (strcmp (o->name, ".mdebug") == 0)
7191 struct extsym_info einfo;
7194 /* We have found the .mdebug section in the output file.
7195 Look through all the link_orders comprising it and merge
7196 the information together. */
7197 symhdr->magic = swap->sym_magic;
7198 /* FIXME: What should the version stamp be? */
7200 symhdr->ilineMax = 0;
7204 symhdr->isymMax = 0;
7205 symhdr->ioptMax = 0;
7206 symhdr->iauxMax = 0;
7208 symhdr->issExtMax = 0;
7211 symhdr->iextMax = 0;
7213 /* We accumulate the debugging information itself in the
7214 debug_info structure. */
7216 debug.external_dnr = NULL;
7217 debug.external_pdr = NULL;
7218 debug.external_sym = NULL;
7219 debug.external_opt = NULL;
7220 debug.external_aux = NULL;
7222 debug.ssext = debug.ssext_end = NULL;
7223 debug.external_fdr = NULL;
7224 debug.external_rfd = NULL;
7225 debug.external_ext = debug.external_ext_end = NULL;
7227 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
7228 if (mdebug_handle == (PTR) NULL)
7232 esym.cobol_main = 0;
7236 esym.asym.iss = issNil;
7237 esym.asym.st = stLocal;
7238 esym.asym.reserved = 0;
7239 esym.asym.index = indexNil;
7241 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
7243 esym.asym.sc = sc[i];
7244 s = bfd_get_section_by_name (abfd, secname[i]);
7247 esym.asym.value = s->vma;
7248 last = s->vma + s->_raw_size;
7251 esym.asym.value = last;
7252 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
7257 for (p = o->link_order_head;
7258 p != (struct bfd_link_order *) NULL;
7261 asection *input_section;
7263 const struct ecoff_debug_swap *input_swap;
7264 struct ecoff_debug_info input_debug;
7268 if (p->type != bfd_indirect_link_order)
7270 if (p->type == bfd_data_link_order)
7275 input_section = p->u.indirect.section;
7276 input_bfd = input_section->owner;
7278 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
7279 || (get_elf_backend_data (input_bfd)
7280 ->elf_backend_ecoff_debug_swap) == NULL)
7282 /* I don't know what a non MIPS ELF bfd would be
7283 doing with a .mdebug section, but I don't really
7284 want to deal with it. */
7288 input_swap = (get_elf_backend_data (input_bfd)
7289 ->elf_backend_ecoff_debug_swap);
7291 BFD_ASSERT (p->size == input_section->_raw_size);
7293 /* The ECOFF linking code expects that we have already
7294 read in the debugging information and set up an
7295 ecoff_debug_info structure, so we do that now. */
7296 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
7300 if (! (bfd_ecoff_debug_accumulate
7301 (mdebug_handle, abfd, &debug, swap, input_bfd,
7302 &input_debug, input_swap, info)))
7305 /* Loop through the external symbols. For each one with
7306 interesting information, try to find the symbol in
7307 the linker global hash table and save the information
7308 for the output external symbols. */
7309 eraw_src = input_debug.external_ext;
7310 eraw_end = (eraw_src
7311 + (input_debug.symbolic_header.iextMax
7312 * input_swap->external_ext_size));
7314 eraw_src < eraw_end;
7315 eraw_src += input_swap->external_ext_size)
7319 struct mips_elf_link_hash_entry *h;
7321 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
7322 if (ext.asym.sc == scNil
7323 || ext.asym.sc == scUndefined
7324 || ext.asym.sc == scSUndefined)
7327 name = input_debug.ssext + ext.asym.iss;
7328 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
7329 name, false, false, true);
7330 if (h == NULL || h->esym.ifd != -2)
7336 < input_debug.symbolic_header.ifdMax);
7337 ext.ifd = input_debug.ifdmap[ext.ifd];
7343 /* Free up the information we just read. */
7344 free (input_debug.line);
7345 free (input_debug.external_dnr);
7346 free (input_debug.external_pdr);
7347 free (input_debug.external_sym);
7348 free (input_debug.external_opt);
7349 free (input_debug.external_aux);
7350 free (input_debug.ss);
7351 free (input_debug.ssext);
7352 free (input_debug.external_fdr);
7353 free (input_debug.external_rfd);
7354 free (input_debug.external_ext);
7356 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7357 elf_link_input_bfd ignores this section. */
7358 input_section->flags &= ~SEC_HAS_CONTENTS;
7361 if (SGI_COMPAT (abfd) && info->shared)
7363 /* Create .rtproc section. */
7364 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
7365 if (rtproc_sec == NULL)
7367 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
7368 | SEC_LINKER_CREATED | SEC_READONLY);
7370 rtproc_sec = bfd_make_section (abfd, ".rtproc");
7371 if (rtproc_sec == NULL
7372 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
7373 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
7377 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
7383 /* Build the external symbol information. */
7386 einfo.debug = &debug;
7388 einfo.failed = false;
7389 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7390 mips_elf_output_extsym,
7395 /* Set the size of the .mdebug section. */
7396 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
7398 /* Skip this section later on (I don't think this currently
7399 matters, but someday it might). */
7400 o->link_order_head = (struct bfd_link_order *) NULL;
7405 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
7407 const char *subname;
7410 Elf32_External_gptab *ext_tab;
7413 /* The .gptab.sdata and .gptab.sbss sections hold
7414 information describing how the small data area would
7415 change depending upon the -G switch. These sections
7416 not used in executables files. */
7417 if (! info->relocateable)
7419 for (p = o->link_order_head;
7420 p != (struct bfd_link_order *) NULL;
7423 asection *input_section;
7425 if (p->type != bfd_indirect_link_order)
7427 if (p->type == bfd_data_link_order)
7432 input_section = p->u.indirect.section;
7434 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7435 elf_link_input_bfd ignores this section. */
7436 input_section->flags &= ~SEC_HAS_CONTENTS;
7439 /* Skip this section later on (I don't think this
7440 currently matters, but someday it might). */
7441 o->link_order_head = (struct bfd_link_order *) NULL;
7443 /* Really remove the section. */
7444 for (secpp = &abfd->sections;
7446 secpp = &(*secpp)->next)
7448 bfd_section_list_remove (abfd, secpp);
7449 --abfd->section_count;
7454 /* There is one gptab for initialized data, and one for
7455 uninitialized data. */
7456 if (strcmp (o->name, ".gptab.sdata") == 0)
7458 else if (strcmp (o->name, ".gptab.sbss") == 0)
7462 (*_bfd_error_handler)
7463 (_("%s: illegal section name `%s'"),
7464 bfd_get_filename (abfd), o->name);
7465 bfd_set_error (bfd_error_nonrepresentable_section);
7469 /* The linker script always combines .gptab.data and
7470 .gptab.sdata into .gptab.sdata, and likewise for
7471 .gptab.bss and .gptab.sbss. It is possible that there is
7472 no .sdata or .sbss section in the output file, in which
7473 case we must change the name of the output section. */
7474 subname = o->name + sizeof ".gptab" - 1;
7475 if (bfd_get_section_by_name (abfd, subname) == NULL)
7477 if (o == gptab_data_sec)
7478 o->name = ".gptab.data";
7480 o->name = ".gptab.bss";
7481 subname = o->name + sizeof ".gptab" - 1;
7482 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
7485 /* Set up the first entry. */
7487 amt = c * sizeof (Elf32_gptab);
7488 tab = (Elf32_gptab *) bfd_malloc (amt);
7491 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
7492 tab[0].gt_header.gt_unused = 0;
7494 /* Combine the input sections. */
7495 for (p = o->link_order_head;
7496 p != (struct bfd_link_order *) NULL;
7499 asection *input_section;
7503 bfd_size_type gpentry;
7505 if (p->type != bfd_indirect_link_order)
7507 if (p->type == bfd_data_link_order)
7512 input_section = p->u.indirect.section;
7513 input_bfd = input_section->owner;
7515 /* Combine the gptab entries for this input section one
7516 by one. We know that the input gptab entries are
7517 sorted by ascending -G value. */
7518 size = bfd_section_size (input_bfd, input_section);
7520 for (gpentry = sizeof (Elf32_External_gptab);
7522 gpentry += sizeof (Elf32_External_gptab))
7524 Elf32_External_gptab ext_gptab;
7525 Elf32_gptab int_gptab;
7531 if (! (bfd_get_section_contents
7532 (input_bfd, input_section, (PTR) &ext_gptab,
7534 (bfd_size_type) sizeof (Elf32_External_gptab))))
7540 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
7542 val = int_gptab.gt_entry.gt_g_value;
7543 add = int_gptab.gt_entry.gt_bytes - last;
7546 for (look = 1; look < c; look++)
7548 if (tab[look].gt_entry.gt_g_value >= val)
7549 tab[look].gt_entry.gt_bytes += add;
7551 if (tab[look].gt_entry.gt_g_value == val)
7557 Elf32_gptab *new_tab;
7560 /* We need a new table entry. */
7561 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
7562 new_tab = (Elf32_gptab *) bfd_realloc ((PTR) tab, amt);
7563 if (new_tab == NULL)
7569 tab[c].gt_entry.gt_g_value = val;
7570 tab[c].gt_entry.gt_bytes = add;
7572 /* Merge in the size for the next smallest -G
7573 value, since that will be implied by this new
7576 for (look = 1; look < c; look++)
7578 if (tab[look].gt_entry.gt_g_value < val
7580 || (tab[look].gt_entry.gt_g_value
7581 > tab[max].gt_entry.gt_g_value)))
7585 tab[c].gt_entry.gt_bytes +=
7586 tab[max].gt_entry.gt_bytes;
7591 last = int_gptab.gt_entry.gt_bytes;
7594 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7595 elf_link_input_bfd ignores this section. */
7596 input_section->flags &= ~SEC_HAS_CONTENTS;
7599 /* The table must be sorted by -G value. */
7601 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
7603 /* Swap out the table. */
7604 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
7605 ext_tab = (Elf32_External_gptab *) bfd_alloc (abfd, amt);
7606 if (ext_tab == NULL)
7612 for (j = 0; j < c; j++)
7613 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
7616 o->_raw_size = c * sizeof (Elf32_External_gptab);
7617 o->contents = (bfd_byte *) ext_tab;
7619 /* Skip this section later on (I don't think this currently
7620 matters, but someday it might). */
7621 o->link_order_head = (struct bfd_link_order *) NULL;
7625 /* Invoke the regular ELF backend linker to do all the work. */
7626 if (!MNAME(abfd,bfd_elf,bfd_final_link) (abfd, info))
7629 /* Now write out the computed sections. */
7631 if (reginfo_sec != (asection *) NULL)
7633 Elf32_External_RegInfo ext;
7635 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
7636 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
7638 (bfd_size_type) sizeof ext))
7642 if (mdebug_sec != (asection *) NULL)
7644 BFD_ASSERT (abfd->output_has_begun);
7645 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
7647 mdebug_sec->filepos))
7650 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
7653 if (gptab_data_sec != (asection *) NULL)
7655 if (! bfd_set_section_contents (abfd, gptab_data_sec,
7656 gptab_data_sec->contents,
7658 gptab_data_sec->_raw_size))
7662 if (gptab_bss_sec != (asection *) NULL)
7664 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
7665 gptab_bss_sec->contents,
7667 gptab_bss_sec->_raw_size))
7671 if (SGI_COMPAT (abfd))
7673 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
7674 if (rtproc_sec != NULL)
7676 if (! bfd_set_section_contents (abfd, rtproc_sec,
7677 rtproc_sec->contents,
7679 rtproc_sec->_raw_size))
7687 /* Return true if machine EXTENSION is an extension of machine BASE,
7688 meaning that it should be safe to link code for the two machines
7689 and set the output machine to EXTENSION. EXTENSION and BASE are
7690 both submasks of EF_MIPS_MACH. */
7693 _bfd_mips_elf_mach_extends_p (base, extension)
7694 flagword base, extension;
7696 /* The vr5500 ISA is an extension of the core vr5400 ISA, but doesn't
7697 include the multimedia stuff. It seems better to allow vr5400
7698 and vr5500 code to be merged anyway, since many libraries will
7699 just use the core ISA. Perhaps we could add some sort of ASE
7700 flag if this ever proves a problem. */
7702 || (base == E_MIPS_MACH_5400 && extension == E_MIPS_MACH_5500)
7703 || (base == E_MIPS_MACH_4100 && extension == E_MIPS_MACH_4111)
7704 || (base == E_MIPS_MACH_4100 && extension == E_MIPS_MACH_4120));
7707 /* Merge backend specific data from an object file to the output
7708 object file when linking. */
7711 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
7718 boolean null_input_bfd = true;
7721 /* Check if we have the same endianess */
7722 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
7725 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7726 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7729 new_flags = elf_elfheader (ibfd)->e_flags;
7730 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
7731 old_flags = elf_elfheader (obfd)->e_flags;
7733 if (! elf_flags_init (obfd))
7735 elf_flags_init (obfd) = true;
7736 elf_elfheader (obfd)->e_flags = new_flags;
7737 elf_elfheader (obfd)->e_ident[EI_CLASS]
7738 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
7740 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
7741 && bfd_get_arch_info (obfd)->the_default)
7743 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
7744 bfd_get_mach (ibfd)))
7751 /* Check flag compatibility. */
7753 new_flags &= ~EF_MIPS_NOREORDER;
7754 old_flags &= ~EF_MIPS_NOREORDER;
7756 if (new_flags == old_flags)
7759 /* Check to see if the input BFD actually contains any sections.
7760 If not, its flags may not have been initialised either, but it cannot
7761 actually cause any incompatibility. */
7762 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7764 /* Ignore synthetic sections and empty .text, .data and .bss sections
7765 which are automatically generated by gas. */
7766 if (strcmp (sec->name, ".reginfo")
7767 && strcmp (sec->name, ".mdebug")
7768 && ((!strcmp (sec->name, ".text")
7769 || !strcmp (sec->name, ".data")
7770 || !strcmp (sec->name, ".bss"))
7771 && sec->_raw_size != 0))
7773 null_input_bfd = false;
7782 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
7784 new_flags &= ~EF_MIPS_PIC;
7785 old_flags &= ~EF_MIPS_PIC;
7786 (*_bfd_error_handler)
7787 (_("%s: linking PIC files with non-PIC files"),
7788 bfd_archive_filename (ibfd));
7792 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
7794 new_flags &= ~EF_MIPS_CPIC;
7795 old_flags &= ~EF_MIPS_CPIC;
7796 (*_bfd_error_handler)
7797 (_("%s: linking abicalls files with non-abicalls files"),
7798 bfd_archive_filename (ibfd));
7802 /* Compare the ISA's. */
7803 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
7804 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
7806 int new_mach = new_flags & EF_MIPS_MACH;
7807 int old_mach = old_flags & EF_MIPS_MACH;
7808 int new_isa = elf_mips_isa (new_flags);
7809 int old_isa = elf_mips_isa (old_flags);
7811 /* If either has no machine specified, just compare the general isa's.
7812 Some combinations of machines are ok, if the isa's match. */
7813 if (new_mach == old_mach
7814 || _bfd_mips_elf_mach_extends_p (new_mach, old_mach)
7815 || _bfd_mips_elf_mach_extends_p (old_mach, new_mach))
7817 /* Don't warn about mixing code using 32-bit ISAs, or mixing code
7818 using 64-bit ISAs. They will normally use the same data sizes
7819 and calling conventions. */
7821 if (( (new_isa == 1 || new_isa == 2 || new_isa == 32)
7822 ^ (old_isa == 1 || old_isa == 2 || old_isa == 32)) != 0)
7824 (*_bfd_error_handler)
7825 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
7826 bfd_archive_filename (ibfd), new_isa, old_isa);
7831 /* Do we need to update the mach field? */
7832 if (_bfd_mips_elf_mach_extends_p (old_mach, new_mach))
7834 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_MACH;
7835 elf_elfheader (obfd)->e_flags |= new_mach;
7838 /* Do we need to update the ISA field? */
7839 if (new_isa > old_isa)
7841 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_ARCH;
7842 elf_elfheader (obfd)->e_flags
7843 |= new_flags & EF_MIPS_ARCH;
7849 (*_bfd_error_handler)
7850 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
7851 bfd_archive_filename (ibfd),
7852 _bfd_elf_mips_mach (new_flags),
7853 _bfd_elf_mips_mach (old_flags));
7857 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7858 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7861 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
7862 does set EI_CLASS differently from any 32-bit ABI. */
7863 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
7864 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7865 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
7867 /* Only error if both are set (to different values). */
7868 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
7869 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7870 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
7872 (*_bfd_error_handler)
7873 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
7874 bfd_archive_filename (ibfd),
7875 elf_mips_abi_name (ibfd),
7876 elf_mips_abi_name (obfd));
7879 new_flags &= ~EF_MIPS_ABI;
7880 old_flags &= ~EF_MIPS_ABI;
7883 /* For now, allow arbitrary mixing of ASEs (retain the union). */
7884 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
7886 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
7888 new_flags &= ~ EF_MIPS_ARCH_ASE;
7889 old_flags &= ~ EF_MIPS_ARCH_ASE;
7892 /* Warn about any other mismatches */
7893 if (new_flags != old_flags)
7895 (*_bfd_error_handler)
7896 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
7897 bfd_archive_filename (ibfd), (unsigned long) new_flags,
7898 (unsigned long) old_flags);
7904 bfd_set_error (bfd_error_bad_value);
7911 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
7914 _bfd_mips_elf_set_private_flags (abfd, flags)
7918 BFD_ASSERT (!elf_flags_init (abfd)
7919 || elf_elfheader (abfd)->e_flags == flags);
7921 elf_elfheader (abfd)->e_flags = flags;
7922 elf_flags_init (abfd) = true;
7927 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
7931 FILE *file = (FILE *) ptr;
7933 BFD_ASSERT (abfd != NULL && ptr != NULL);
7935 /* Print normal ELF private data. */
7936 _bfd_elf_print_private_bfd_data (abfd, ptr);
7938 /* xgettext:c-format */
7939 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
7941 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
7942 fprintf (file, _(" [abi=O32]"));
7943 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
7944 fprintf (file, _(" [abi=O64]"));
7945 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
7946 fprintf (file, _(" [abi=EABI32]"));
7947 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7948 fprintf (file, _(" [abi=EABI64]"));
7949 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
7950 fprintf (file, _(" [abi unknown]"));
7951 else if (ABI_N32_P (abfd))
7952 fprintf (file, _(" [abi=N32]"));
7953 else if (ABI_64_P (abfd))
7954 fprintf (file, _(" [abi=64]"));
7956 fprintf (file, _(" [no abi set]"));
7958 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
7959 fprintf (file, _(" [mips1]"));
7960 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
7961 fprintf (file, _(" [mips2]"));
7962 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
7963 fprintf (file, _(" [mips3]"));
7964 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
7965 fprintf (file, _(" [mips4]"));
7966 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
7967 fprintf (file, _(" [mips5]"));
7968 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
7969 fprintf (file, _(" [mips32]"));
7970 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
7971 fprintf (file, _(" [mips64]"));
7973 fprintf (file, _(" [unknown ISA]"));
7975 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
7976 fprintf (file, _(" [mdmx]"));
7978 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
7979 fprintf (file, _(" [mips16]"));
7981 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
7982 fprintf (file, _(" [32bitmode]"));
7984 fprintf (file, _(" [not 32bitmode]"));