Initial revision
[binutils.git] / bfd / elf32-mips.c
blob9f03262eb38694f15550eb8bb7bc27c24f01c8c2
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
24 different MIPS ELF from other targets. This matters when linking.
25 This file supports both, switching at runtime. */
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "bfdlink.h"
31 #include "genlink.h"
32 #include "elf-bfd.h"
33 #include "elf/mips.h"
35 /* Get the ECOFF swapping routines. */
36 #include "coff/sym.h"
37 #include "coff/symconst.h"
38 #include "coff/internal.h"
39 #include "coff/ecoff.h"
40 #include "coff/mips.h"
41 #define ECOFF_32
42 #include "ecoffswap.h"
44 static bfd_reloc_status_type mips32_64bit_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
47 PARAMS ((bfd *, bfd_reloc_code_real_type));
48 static void mips_info_to_howto_rel
49 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
50 static void bfd_mips_elf32_swap_gptab_in
51 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
52 static void bfd_mips_elf32_swap_gptab_out
53 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
54 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
55 static boolean mips_elf32_object_p PARAMS ((bfd *));
56 static boolean mips_elf_create_procedure_table
57 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
58 struct ecoff_debug_info *));
59 static int mips_elf_additional_program_headers PARAMS ((bfd *));
60 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
61 static INLINE int elf_mips_isa PARAMS ((flagword));
62 static INLINE int elf_mips_mach PARAMS ((flagword));
63 static INLINE char* elf_mips_abi_name PARAMS ((flagword));
64 static boolean mips_elf32_section_from_shdr
65 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
66 static boolean mips_elf32_section_processing
67 PARAMS ((bfd *, Elf32_Internal_Shdr *));
68 static boolean mips_elf_is_local_label_name
69 PARAMS ((bfd *, const char *));
70 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
71 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
72 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
73 PARAMS ((bfd *));
74 static int gptab_compare PARAMS ((const void *, const void *));
75 static boolean mips_elf_final_link
76 PARAMS ((bfd *, struct bfd_link_info *));
77 static void mips_elf_relocate_hi16
78 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
79 bfd_vma));
80 static boolean mips_elf_relocate_got_local
81 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
82 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
83 static void mips_elf_relocate_global_got
84 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
85 static bfd_reloc_status_type mips16_jump_reloc
86 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
87 static bfd_reloc_status_type mips16_gprel_reloc
88 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
89 static boolean mips_elf_adjust_dynindx
90 PARAMS ((struct elf_link_hash_entry *, PTR));
91 static boolean mips_elf_relocate_section
92 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
93 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
94 static boolean mips_elf_link_output_symbol_hook
95 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
96 asection *));
97 static boolean mips_elf_create_dynamic_sections
98 PARAMS ((bfd *, struct bfd_link_info *));
99 static boolean mips_elf_create_compact_rel_section
100 PARAMS ((bfd *, struct bfd_link_info *));
101 static boolean mips_elf_create_got_section
102 PARAMS ((bfd *, struct bfd_link_info *));
103 static boolean mips_elf_check_relocs
104 PARAMS ((bfd *, struct bfd_link_info *, asection *,
105 const Elf_Internal_Rela *));
106 static boolean mips_elf_adjust_dynamic_symbol
107 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
108 static boolean mips_elf_always_size_sections
109 PARAMS ((bfd *, struct bfd_link_info *));
110 static boolean mips_elf_size_dynamic_sections
111 PARAMS ((bfd *, struct bfd_link_info *));
112 static boolean mips_elf_finish_dynamic_symbol
113 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
114 Elf_Internal_Sym *));
115 static boolean mips_elf_finish_dynamic_sections
116 PARAMS ((bfd *, struct bfd_link_info *));
117 static boolean mips_elf_add_symbol_hook
118 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
119 const char **, flagword *, asection **, bfd_vma *));
120 static bfd_reloc_status_type mips_elf_final_gp
121 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
122 static bfd_byte *elf32_mips_get_relocated_section_contents
123 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
124 bfd_byte *, boolean, asymbol **));
126 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
127 executables. FIXME: At the moment, we default to always generating
128 Irix 5 executables. */
130 #define SGI_COMPAT(abfd) (1)
132 /* This structure is used to hold .got information when linking. It
133 is stored in the tdata field of the bfd_elf_section_data structure. */
135 struct mips_got_info
137 /* The symbol index of the first global .got symbol. */
138 unsigned long global_gotsym;
139 /* The number of local .got entries. */
140 unsigned int local_gotno;
141 /* The number of local .got entries we have used. */
142 unsigned int assigned_gotno;
145 /* The number of local .got entries we reserve. */
146 #define MIPS_RESERVED_GOTNO (2)
148 /* Instructions which appear in a stub. For some reason the stub is
149 slightly different on an SGI system. */
150 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
151 #define STUB_LW(abfd) \
152 (SGI_COMPAT (abfd) \
153 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
154 : 0x8f998000) /* lw t9,0x8000(gp) */
155 #define STUB_MOVE 0x03e07825 /* move t7,ra */
156 #define STUB_JALR 0x0320f809 /* jal t9 */
157 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
158 #define MIPS_FUNCTION_STUB_SIZE (16)
160 #if 0
161 /* We no longer try to identify particular sections for the .dynsym
162 section. When we do, we wind up crashing if there are other random
163 sections with relocations. */
165 /* Names of sections which appear in the .dynsym section in an Irix 5
166 executable. */
168 static const char * const mips_elf_dynsym_sec_names[] =
170 ".text",
171 ".init",
172 ".fini",
173 ".data",
174 ".rodata",
175 ".sdata",
176 ".sbss",
177 ".bss",
178 NULL
181 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
182 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
184 /* The number of entries in mips_elf_dynsym_sec_names which go in the
185 text segment. */
187 #define MIPS_TEXT_DYNSYM_SECNO (3)
189 #endif /* 0 */
191 /* The names of the runtime procedure table symbols used on Irix 5. */
193 static const char * const mips_elf_dynsym_rtproc_names[] =
195 "_procedure_table",
196 "_procedure_string_table",
197 "_procedure_table_size",
198 NULL
201 /* These structures are used to generate the .compact_rel section on
202 Irix 5. */
204 typedef struct
206 unsigned long id1; /* Always one? */
207 unsigned long num; /* Number of compact relocation entries. */
208 unsigned long id2; /* Always two? */
209 unsigned long offset; /* The file offset of the first relocation. */
210 unsigned long reserved0; /* Zero? */
211 unsigned long reserved1; /* Zero? */
212 } Elf32_compact_rel;
214 typedef struct
216 bfd_byte id1[4];
217 bfd_byte num[4];
218 bfd_byte id2[4];
219 bfd_byte offset[4];
220 bfd_byte reserved0[4];
221 bfd_byte reserved1[4];
222 } Elf32_External_compact_rel;
224 typedef struct
226 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
227 unsigned int rtype : 4; /* Relocation types. See below. */
228 unsigned int dist2to : 8;
229 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
230 unsigned long konst; /* KONST field. See below. */
231 unsigned long vaddr; /* VADDR to be relocated. */
232 } Elf32_crinfo;
234 typedef struct
236 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
237 unsigned int rtype : 4; /* Relocation types. See below. */
238 unsigned int dist2to : 8;
239 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
240 unsigned long konst; /* KONST field. See below. */
241 } Elf32_crinfo2;
243 typedef struct
245 bfd_byte info[4];
246 bfd_byte konst[4];
247 bfd_byte vaddr[4];
248 } Elf32_External_crinfo;
250 typedef struct
252 bfd_byte info[4];
253 bfd_byte konst[4];
254 } Elf32_External_crinfo2;
256 /* These are the constants used to swap the bitfields in a crinfo. */
258 #define CRINFO_CTYPE (0x1)
259 #define CRINFO_CTYPE_SH (31)
260 #define CRINFO_RTYPE (0xf)
261 #define CRINFO_RTYPE_SH (27)
262 #define CRINFO_DIST2TO (0xff)
263 #define CRINFO_DIST2TO_SH (19)
264 #define CRINFO_RELVADDR (0x7ffff)
265 #define CRINFO_RELVADDR_SH (0)
267 /* A compact relocation info has long (3 words) or short (2 words)
268 formats. A short format doesn't have VADDR field and relvaddr
269 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
270 #define CRF_MIPS_LONG 1
271 #define CRF_MIPS_SHORT 0
273 /* There are 4 types of compact relocation at least. The value KONST
274 has different meaning for each type:
276 (type) (konst)
277 CT_MIPS_REL32 Address in data
278 CT_MIPS_WORD Address in word (XXX)
279 CT_MIPS_GPHI_LO GP - vaddr
280 CT_MIPS_JMPAD Address to jump
283 #define CRT_MIPS_REL32 0xa
284 #define CRT_MIPS_WORD 0xb
285 #define CRT_MIPS_GPHI_LO 0xc
286 #define CRT_MIPS_JMPAD 0xd
288 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
289 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
290 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
291 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
293 static void bfd_elf32_swap_compact_rel_out
294 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
295 static void bfd_elf32_swap_crinfo_out
296 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
298 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
300 static reloc_howto_type elf_mips_howto_table[] =
302 /* No relocation. */
303 HOWTO (R_MIPS_NONE, /* type */
304 0, /* rightshift */
305 0, /* size (0 = byte, 1 = short, 2 = long) */
306 0, /* bitsize */
307 false, /* pc_relative */
308 0, /* bitpos */
309 complain_overflow_dont, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_MIPS_NONE", /* name */
312 false, /* partial_inplace */
313 0, /* src_mask */
314 0, /* dst_mask */
315 false), /* pcrel_offset */
317 /* 16 bit relocation. */
318 HOWTO (R_MIPS_16, /* type */
319 0, /* rightshift */
320 1, /* size (0 = byte, 1 = short, 2 = long) */
321 16, /* bitsize */
322 false, /* pc_relative */
323 0, /* bitpos */
324 complain_overflow_bitfield, /* complain_on_overflow */
325 bfd_elf_generic_reloc, /* special_function */
326 "R_MIPS_16", /* name */
327 true, /* partial_inplace */
328 0xffff, /* src_mask */
329 0xffff, /* dst_mask */
330 false), /* pcrel_offset */
332 /* 32 bit relocation. */
333 HOWTO (R_MIPS_32, /* type */
334 0, /* rightshift */
335 2, /* size (0 = byte, 1 = short, 2 = long) */
336 32, /* bitsize */
337 false, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_bitfield, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_MIPS_32", /* name */
342 true, /* partial_inplace */
343 0xffffffff, /* src_mask */
344 0xffffffff, /* dst_mask */
345 false), /* pcrel_offset */
347 /* 32 bit symbol relative relocation. */
348 HOWTO (R_MIPS_REL32, /* type */
349 0, /* rightshift */
350 2, /* size (0 = byte, 1 = short, 2 = long) */
351 32, /* bitsize */
352 false, /* pc_relative */
353 0, /* bitpos */
354 complain_overflow_bitfield, /* complain_on_overflow */
355 bfd_elf_generic_reloc, /* special_function */
356 "R_MIPS_REL32", /* name */
357 true, /* partial_inplace */
358 0xffffffff, /* src_mask */
359 0xffffffff, /* dst_mask */
360 false), /* pcrel_offset */
362 /* 26 bit branch address. */
363 HOWTO (R_MIPS_26, /* type */
364 2, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 26, /* bitsize */
367 false, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_dont, /* complain_on_overflow */
370 /* This needs complex overflow
371 detection, because the upper four
372 bits must match the PC. */
373 bfd_elf_generic_reloc, /* special_function */
374 "R_MIPS_26", /* name */
375 true, /* partial_inplace */
376 0x3ffffff, /* src_mask */
377 0x3ffffff, /* dst_mask */
378 false), /* pcrel_offset */
380 /* High 16 bits of symbol value. */
381 HOWTO (R_MIPS_HI16, /* type */
382 0, /* rightshift */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
384 16, /* bitsize */
385 false, /* pc_relative */
386 0, /* bitpos */
387 complain_overflow_dont, /* complain_on_overflow */
388 _bfd_mips_elf_hi16_reloc, /* special_function */
389 "R_MIPS_HI16", /* name */
390 true, /* partial_inplace */
391 0xffff, /* src_mask */
392 0xffff, /* dst_mask */
393 false), /* pcrel_offset */
395 /* Low 16 bits of symbol value. */
396 HOWTO (R_MIPS_LO16, /* type */
397 0, /* rightshift */
398 2, /* size (0 = byte, 1 = short, 2 = long) */
399 16, /* bitsize */
400 false, /* pc_relative */
401 0, /* bitpos */
402 complain_overflow_dont, /* complain_on_overflow */
403 _bfd_mips_elf_lo16_reloc, /* special_function */
404 "R_MIPS_LO16", /* name */
405 true, /* partial_inplace */
406 0xffff, /* src_mask */
407 0xffff, /* dst_mask */
408 false), /* pcrel_offset */
410 /* GP relative reference. */
411 HOWTO (R_MIPS_GPREL16, /* type */
412 0, /* rightshift */
413 2, /* size (0 = byte, 1 = short, 2 = long) */
414 16, /* bitsize */
415 false, /* pc_relative */
416 0, /* bitpos */
417 complain_overflow_signed, /* complain_on_overflow */
418 _bfd_mips_elf_gprel16_reloc, /* special_function */
419 "R_MIPS_GPREL16", /* name */
420 true, /* partial_inplace */
421 0xffff, /* src_mask */
422 0xffff, /* dst_mask */
423 false), /* pcrel_offset */
425 /* Reference to literal section. */
426 HOWTO (R_MIPS_LITERAL, /* type */
427 0, /* rightshift */
428 2, /* size (0 = byte, 1 = short, 2 = long) */
429 16, /* bitsize */
430 false, /* pc_relative */
431 0, /* bitpos */
432 complain_overflow_signed, /* complain_on_overflow */
433 _bfd_mips_elf_gprel16_reloc, /* special_function */
434 "R_MIPS_LITERAL", /* name */
435 true, /* partial_inplace */
436 0xffff, /* src_mask */
437 0xffff, /* dst_mask */
438 false), /* pcrel_offset */
440 /* Reference to global offset table. */
441 HOWTO (R_MIPS_GOT16, /* type */
442 0, /* rightshift */
443 2, /* size (0 = byte, 1 = short, 2 = long) */
444 16, /* bitsize */
445 false, /* pc_relative */
446 0, /* bitpos */
447 complain_overflow_signed, /* complain_on_overflow */
448 _bfd_mips_elf_got16_reloc, /* special_function */
449 "R_MIPS_GOT16", /* name */
450 false, /* partial_inplace */
451 0, /* src_mask */
452 0xffff, /* dst_mask */
453 false), /* pcrel_offset */
455 /* 16 bit PC relative reference. */
456 HOWTO (R_MIPS_PC16, /* type */
457 0, /* rightshift */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
459 16, /* bitsize */
460 true, /* pc_relative */
461 0, /* bitpos */
462 complain_overflow_signed, /* complain_on_overflow */
463 bfd_elf_generic_reloc, /* special_function */
464 "R_MIPS_PC16", /* name */
465 true, /* partial_inplace */
466 0xffff, /* src_mask */
467 0xffff, /* dst_mask */
468 false), /* pcrel_offset */
470 /* 16 bit call through global offset table. */
471 /* FIXME: This is not handled correctly. */
472 HOWTO (R_MIPS_CALL16, /* type */
473 0, /* rightshift */
474 2, /* size (0 = byte, 1 = short, 2 = long) */
475 16, /* bitsize */
476 false, /* pc_relative */
477 0, /* bitpos */
478 complain_overflow_signed, /* complain_on_overflow */
479 bfd_elf_generic_reloc, /* special_function */
480 "R_MIPS_CALL16", /* name */
481 false, /* partial_inplace */
482 0, /* src_mask */
483 0xffff, /* dst_mask */
484 false), /* pcrel_offset */
486 /* 32 bit GP relative reference. */
487 HOWTO (R_MIPS_GPREL32, /* type */
488 0, /* rightshift */
489 2, /* size (0 = byte, 1 = short, 2 = long) */
490 32, /* bitsize */
491 false, /* pc_relative */
492 0, /* bitpos */
493 complain_overflow_bitfield, /* complain_on_overflow */
494 _bfd_mips_elf_gprel32_reloc, /* special_function */
495 "R_MIPS_GPREL32", /* name */
496 true, /* partial_inplace */
497 0xffffffff, /* src_mask */
498 0xffffffff, /* dst_mask */
499 false), /* pcrel_offset */
501 /* The remaining relocs are defined on Irix 5, although they are
502 not defined by the ABI. */
503 { 13 },
504 { 14 },
505 { 15 },
507 /* A 5 bit shift field. */
508 HOWTO (R_MIPS_SHIFT5, /* type */
509 0, /* rightshift */
510 2, /* size (0 = byte, 1 = short, 2 = long) */
511 5, /* bitsize */
512 false, /* pc_relative */
513 6, /* bitpos */
514 complain_overflow_bitfield, /* complain_on_overflow */
515 bfd_elf_generic_reloc, /* special_function */
516 "R_MIPS_SHIFT5", /* name */
517 true, /* partial_inplace */
518 0x000007c0, /* src_mask */
519 0x000007c0, /* dst_mask */
520 false), /* pcrel_offset */
522 /* A 6 bit shift field. */
523 /* FIXME: This is not handled correctly; a special function is
524 needed to put the most significant bit in the right place. */
525 HOWTO (R_MIPS_SHIFT6, /* type */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 6, /* bitsize */
529 false, /* pc_relative */
530 6, /* bitpos */
531 complain_overflow_bitfield, /* complain_on_overflow */
532 bfd_elf_generic_reloc, /* special_function */
533 "R_MIPS_SHIFT6", /* name */
534 true, /* partial_inplace */
535 0x000007c4, /* src_mask */
536 0x000007c4, /* dst_mask */
537 false), /* pcrel_offset */
539 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
540 are 64 bits long; the upper 32 bits are simply a sign extension.
541 The fields of the howto should be the same as for R_MIPS_32,
542 other than the type, name, and special_function. */
543 HOWTO (R_MIPS_64, /* type */
544 0, /* rightshift */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
546 32, /* bitsize */
547 false, /* pc_relative */
548 0, /* bitpos */
549 complain_overflow_bitfield, /* complain_on_overflow */
550 mips32_64bit_reloc, /* special_function */
551 "R_MIPS_64", /* name */
552 true, /* partial_inplace */
553 0xffffffff, /* src_mask */
554 0xffffffff, /* dst_mask */
555 false), /* pcrel_offset */
557 /* Displacement in the global offset table. */
558 /* FIXME: Not handled correctly. */
559 HOWTO (R_MIPS_GOT_DISP, /* type */
560 0, /* rightshift */
561 2, /* size (0 = byte, 1 = short, 2 = long) */
562 16, /* bitsize */
563 false, /* pc_relative */
564 0, /* bitpos */
565 complain_overflow_bitfield, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_MIPS_GOT_DISP", /* name */
568 true, /* partial_inplace */
569 0x0000ffff, /* src_mask */
570 0x0000ffff, /* dst_mask */
571 false), /* pcrel_offset */
573 /* Displacement to page pointer in the global offset table. */
574 /* FIXME: Not handled correctly. */
575 HOWTO (R_MIPS_GOT_PAGE, /* type */
576 0, /* rightshift */
577 2, /* size (0 = byte, 1 = short, 2 = long) */
578 16, /* bitsize */
579 false, /* pc_relative */
580 0, /* bitpos */
581 complain_overflow_bitfield, /* complain_on_overflow */
582 bfd_elf_generic_reloc, /* special_function */
583 "R_MIPS_GOT_PAGE", /* name */
584 true, /* partial_inplace */
585 0x0000ffff, /* src_mask */
586 0x0000ffff, /* dst_mask */
587 false), /* pcrel_offset */
589 /* Offset from page pointer in the global offset table. */
590 /* FIXME: Not handled correctly. */
591 HOWTO (R_MIPS_GOT_OFST, /* type */
592 0, /* rightshift */
593 2, /* size (0 = byte, 1 = short, 2 = long) */
594 16, /* bitsize */
595 false, /* pc_relative */
596 0, /* bitpos */
597 complain_overflow_bitfield, /* complain_on_overflow */
598 bfd_elf_generic_reloc, /* special_function */
599 "R_MIPS_GOT_OFST", /* name */
600 true, /* partial_inplace */
601 0x0000ffff, /* src_mask */
602 0x0000ffff, /* dst_mask */
603 false), /* pcrel_offset */
605 /* High 16 bits of displacement in global offset table. */
606 /* FIXME: Not handled correctly. */
607 HOWTO (R_MIPS_GOT_HI16, /* type */
608 0, /* rightshift */
609 2, /* size (0 = byte, 1 = short, 2 = long) */
610 16, /* bitsize */
611 false, /* pc_relative */
612 0, /* bitpos */
613 complain_overflow_dont, /* complain_on_overflow */
614 bfd_elf_generic_reloc, /* special_function */
615 "R_MIPS_GOT_HI16", /* name */
616 true, /* partial_inplace */
617 0x0000ffff, /* src_mask */
618 0x0000ffff, /* dst_mask */
619 false), /* pcrel_offset */
621 /* Low 16 bits of displacement in global offset table. */
622 /* FIXME: Not handled correctly. */
623 HOWTO (R_MIPS_GOT_LO16, /* type */
624 0, /* rightshift */
625 2, /* size (0 = byte, 1 = short, 2 = long) */
626 16, /* bitsize */
627 false, /* pc_relative */
628 0, /* bitpos */
629 complain_overflow_dont, /* complain_on_overflow */
630 bfd_elf_generic_reloc, /* special_function */
631 "R_MIPS_GOT_LO16", /* name */
632 true, /* partial_inplace */
633 0x0000ffff, /* src_mask */
634 0x0000ffff, /* dst_mask */
635 false), /* pcrel_offset */
637 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
638 { R_MIPS_SUB },
640 /* Used to cause the linker to insert and delete instructions? */
641 { R_MIPS_INSERT_A },
642 { R_MIPS_INSERT_B },
643 { R_MIPS_DELETE },
645 /* Get the higher values of a 64 bit addend. Presumably not used in
646 32 bit ELF. */
647 { R_MIPS_HIGHER },
648 { R_MIPS_HIGHEST },
650 /* High 16 bits of displacement in global offset table. */
651 /* FIXME: Not handled correctly. */
652 HOWTO (R_MIPS_CALL_HI16, /* type */
653 0, /* rightshift */
654 2, /* size (0 = byte, 1 = short, 2 = long) */
655 16, /* bitsize */
656 false, /* pc_relative */
657 0, /* bitpos */
658 complain_overflow_dont, /* complain_on_overflow */
659 bfd_elf_generic_reloc, /* special_function */
660 "R_MIPS_CALL_HI16", /* name */
661 true, /* partial_inplace */
662 0x0000ffff, /* src_mask */
663 0x0000ffff, /* dst_mask */
664 false), /* pcrel_offset */
666 /* Low 16 bits of displacement in global offset table. */
667 /* FIXME: Not handled correctly. */
668 HOWTO (R_MIPS_CALL_LO16, /* type */
669 0, /* rightshift */
670 2, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
672 false, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_dont, /* complain_on_overflow */
675 bfd_elf_generic_reloc, /* special_function */
676 "R_MIPS_CALL_LO16", /* name */
677 true, /* partial_inplace */
678 0x0000ffff, /* src_mask */
679 0x0000ffff, /* dst_mask */
680 false), /* pcrel_offset */
682 { R_MIPS_SCN_DISP },
683 { R_MIPS_REL16 },
684 { R_MIPS_ADD_IMMEDIATE },
685 { R_MIPS_PJUMP },
686 { R_MIPS_RELGOT }
689 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
690 is a hack to make the linker think that we need 64 bit values. */
691 static reloc_howto_type elf_mips_ctor64_howto =
692 HOWTO (R_MIPS_64, /* type */
693 0, /* rightshift */
694 4, /* size (0 = byte, 1 = short, 2 = long) */
695 32, /* bitsize */
696 false, /* pc_relative */
697 0, /* bitpos */
698 complain_overflow_signed, /* complain_on_overflow */
699 mips32_64bit_reloc, /* special_function */
700 "R_MIPS_64", /* name */
701 true, /* partial_inplace */
702 0xffffffff, /* src_mask */
703 0xffffffff, /* dst_mask */
704 false); /* pcrel_offset */
706 /* The reloc used for the mips16 jump instruction. */
707 static reloc_howto_type elf_mips16_jump_howto =
708 HOWTO (R_MIPS16_26, /* type */
709 2, /* rightshift */
710 2, /* size (0 = byte, 1 = short, 2 = long) */
711 26, /* bitsize */
712 false, /* pc_relative */
713 0, /* bitpos */
714 complain_overflow_dont, /* complain_on_overflow */
715 /* This needs complex overflow
716 detection, because the upper four
717 bits must match the PC. */
718 mips16_jump_reloc, /* special_function */
719 "R_MIPS16_26", /* name */
720 true, /* partial_inplace */
721 0x3ffffff, /* src_mask */
722 0x3ffffff, /* dst_mask */
723 false); /* pcrel_offset */
725 /* The reloc used for the mips16 gprel instruction. The src_mask and
726 dsk_mask for this howto do not reflect the actual instruction, in
727 which the value is not contiguous; the masks are for the
728 convenience of the relocate_section routine. */
729 static reloc_howto_type elf_mips16_gprel_howto =
730 HOWTO (R_MIPS16_GPREL, /* type */
731 0, /* rightshift */
732 2, /* size (0 = byte, 1 = short, 2 = long) */
733 16, /* bitsize */
734 false, /* pc_relative */
735 0, /* bitpos */
736 complain_overflow_signed, /* complain_on_overflow */
737 mips16_gprel_reloc, /* special_function */
738 "R_MIPS16_GPREL", /* name */
739 true, /* partial_inplace */
740 0xffff, /* src_mask */
741 0xffff, /* dst_mask */
742 false); /* pcrel_offset */
745 /* GNU extension to record C++ vtable hierarchy */
746 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
747 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
748 0, /* rightshift */
749 2, /* size (0 = byte, 1 = short, 2 = long) */
750 0, /* bitsize */
751 false, /* pc_relative */
752 0, /* bitpos */
753 complain_overflow_dont, /* complain_on_overflow */
754 NULL, /* special_function */
755 "R_MIPS_GNU_VTINHERIT", /* name */
756 false, /* partial_inplace */
757 0, /* src_mask */
758 0, /* dst_mask */
759 false); /* pcrel_offset */
761 /* GNU extension to record C++ vtable member usage */
762 static reloc_howto_type elf_mips_gnu_vtentry_howto =
763 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
764 0, /* rightshift */
765 2, /* size (0 = byte, 1 = short, 2 = long) */
766 0, /* bitsize */
767 false, /* pc_relative */
768 0, /* bitpos */
769 complain_overflow_dont, /* complain_on_overflow */
770 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
771 "R_MIPS_GNU_VTENTRY", /* name */
772 false, /* partial_inplace */
773 0, /* src_mask */
774 0, /* dst_mask */
775 false); /* pcrel_offset */
777 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
778 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
779 the HI16. Here we just save the information we need; we do the
780 actual relocation when we see the LO16. MIPS ELF requires that the
781 LO16 immediately follow the HI16. As a GNU extension, we permit an
782 arbitrary number of HI16 relocs to be associated with a single LO16
783 reloc. This extension permits gcc to output the HI and LO relocs
784 itself. */
786 struct mips_hi16
788 struct mips_hi16 *next;
789 bfd_byte *addr;
790 bfd_vma addend;
793 /* FIXME: This should not be a static variable. */
795 static struct mips_hi16 *mips_hi16_list;
797 bfd_reloc_status_type
798 _bfd_mips_elf_hi16_reloc (abfd,
799 reloc_entry,
800 symbol,
801 data,
802 input_section,
803 output_bfd,
804 error_message)
805 bfd *abfd;
806 arelent *reloc_entry;
807 asymbol *symbol;
808 PTR data;
809 asection *input_section;
810 bfd *output_bfd;
811 char **error_message;
813 bfd_reloc_status_type ret;
814 bfd_vma relocation;
815 struct mips_hi16 *n;
817 /* If we're relocating, and this an external symbol, we don't want
818 to change anything. */
819 if (output_bfd != (bfd *) NULL
820 && (symbol->flags & BSF_SECTION_SYM) == 0
821 && reloc_entry->addend == 0)
823 reloc_entry->address += input_section->output_offset;
824 return bfd_reloc_ok;
827 ret = bfd_reloc_ok;
829 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
831 boolean relocateable;
832 bfd_vma gp;
834 if (ret == bfd_reloc_undefined)
835 abort ();
837 if (output_bfd != NULL)
838 relocateable = true;
839 else
841 relocateable = false;
842 output_bfd = symbol->section->output_section->owner;
845 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
846 error_message, &gp);
847 if (ret != bfd_reloc_ok)
848 return ret;
850 relocation = gp - reloc_entry->address;
852 else
854 if (bfd_is_und_section (symbol->section)
855 && output_bfd == (bfd *) NULL)
856 ret = bfd_reloc_undefined;
858 if (bfd_is_com_section (symbol->section))
859 relocation = 0;
860 else
861 relocation = symbol->value;
864 relocation += symbol->section->output_section->vma;
865 relocation += symbol->section->output_offset;
866 relocation += reloc_entry->addend;
868 if (reloc_entry->address > input_section->_cooked_size)
869 return bfd_reloc_outofrange;
871 /* Save the information, and let LO16 do the actual relocation. */
872 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
873 if (n == NULL)
874 return bfd_reloc_outofrange;
875 n->addr = (bfd_byte *) data + reloc_entry->address;
876 n->addend = relocation;
877 n->next = mips_hi16_list;
878 mips_hi16_list = n;
880 if (output_bfd != (bfd *) NULL)
881 reloc_entry->address += input_section->output_offset;
883 return ret;
886 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
887 inplace relocation; this function exists in order to do the
888 R_MIPS_HI16 relocation described above. */
890 bfd_reloc_status_type
891 _bfd_mips_elf_lo16_reloc (abfd,
892 reloc_entry,
893 symbol,
894 data,
895 input_section,
896 output_bfd,
897 error_message)
898 bfd *abfd;
899 arelent *reloc_entry;
900 asymbol *symbol;
901 PTR data;
902 asection *input_section;
903 bfd *output_bfd;
904 char **error_message;
906 arelent gp_disp_relent;
908 if (mips_hi16_list != NULL)
910 struct mips_hi16 *l;
912 l = mips_hi16_list;
913 while (l != NULL)
915 unsigned long insn;
916 unsigned long val;
917 unsigned long vallo;
918 struct mips_hi16 *next;
920 /* Do the HI16 relocation. Note that we actually don't need
921 to know anything about the LO16 itself, except where to
922 find the low 16 bits of the addend needed by the LO16. */
923 insn = bfd_get_32 (abfd, l->addr);
924 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
925 & 0xffff);
926 val = ((insn & 0xffff) << 16) + vallo;
927 val += l->addend;
929 /* The low order 16 bits are always treated as a signed
930 value. Therefore, a negative value in the low order bits
931 requires an adjustment in the high order bits. We need
932 to make this adjustment in two ways: once for the bits we
933 took from the data, and once for the bits we are putting
934 back in to the data. */
935 if ((vallo & 0x8000) != 0)
936 val -= 0x10000;
937 if ((val & 0x8000) != 0)
938 val += 0x10000;
940 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
941 bfd_put_32 (abfd, insn, l->addr);
943 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
945 gp_disp_relent = *reloc_entry;
946 reloc_entry = &gp_disp_relent;
947 reloc_entry->addend = l->addend;
950 next = l->next;
951 free (l);
952 l = next;
955 mips_hi16_list = NULL;
957 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
959 bfd_reloc_status_type ret;
960 bfd_vma gp, relocation;
962 /* FIXME: Does this case ever occur? */
964 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
965 if (ret != bfd_reloc_ok)
966 return ret;
968 relocation = gp - reloc_entry->address;
969 relocation += symbol->section->output_section->vma;
970 relocation += symbol->section->output_offset;
971 relocation += reloc_entry->addend;
973 if (reloc_entry->address > input_section->_cooked_size)
974 return bfd_reloc_outofrange;
976 gp_disp_relent = *reloc_entry;
977 reloc_entry = &gp_disp_relent;
978 reloc_entry->addend = relocation - 4;
981 /* Now do the LO16 reloc in the usual way. */
982 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
983 input_section, output_bfd, error_message);
986 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
987 table used for PIC code. If the symbol is an external symbol, the
988 instruction is modified to contain the offset of the appropriate
989 entry in the global offset table. If the symbol is a section
990 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
991 addends are combined to form the real addend against the section
992 symbol; the GOT16 is modified to contain the offset of an entry in
993 the global offset table, and the LO16 is modified to offset it
994 appropriately. Thus an offset larger than 16 bits requires a
995 modified value in the global offset table.
997 This implementation suffices for the assembler, but the linker does
998 not yet know how to create global offset tables. */
1000 bfd_reloc_status_type
1001 _bfd_mips_elf_got16_reloc (abfd,
1002 reloc_entry,
1003 symbol,
1004 data,
1005 input_section,
1006 output_bfd,
1007 error_message)
1008 bfd *abfd;
1009 arelent *reloc_entry;
1010 asymbol *symbol;
1011 PTR data;
1012 asection *input_section;
1013 bfd *output_bfd;
1014 char **error_message;
1016 /* If we're relocating, and this an external symbol, we don't want
1017 to change anything. */
1018 if (output_bfd != (bfd *) NULL
1019 && (symbol->flags & BSF_SECTION_SYM) == 0
1020 && reloc_entry->addend == 0)
1022 reloc_entry->address += input_section->output_offset;
1023 return bfd_reloc_ok;
1026 /* If we're relocating, and this is a local symbol, we can handle it
1027 just like HI16. */
1028 if (output_bfd != (bfd *) NULL
1029 && (symbol->flags & BSF_SECTION_SYM) != 0)
1030 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1031 input_section, output_bfd, error_message);
1033 abort ();
1036 /* We have to figure out the gp value, so that we can adjust the
1037 symbol value correctly. We look up the symbol _gp in the output
1038 BFD. If we can't find it, we're stuck. We cache it in the ELF
1039 target data. We don't need to adjust the symbol value for an
1040 external symbol if we are producing relocateable output. */
1042 static bfd_reloc_status_type
1043 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1044 bfd *output_bfd;
1045 asymbol *symbol;
1046 boolean relocateable;
1047 char **error_message;
1048 bfd_vma *pgp;
1050 if (bfd_is_und_section (symbol->section)
1051 && ! relocateable)
1053 *pgp = 0;
1054 return bfd_reloc_undefined;
1057 *pgp = _bfd_get_gp_value (output_bfd);
1058 if (*pgp == 0
1059 && (! relocateable
1060 || (symbol->flags & BSF_SECTION_SYM) != 0))
1062 if (relocateable)
1064 /* Make up a value. */
1065 *pgp = symbol->section->output_section->vma + 0x4000;
1066 _bfd_set_gp_value (output_bfd, *pgp);
1068 else
1070 unsigned int count;
1071 asymbol **sym;
1072 unsigned int i;
1074 count = bfd_get_symcount (output_bfd);
1075 sym = bfd_get_outsymbols (output_bfd);
1077 if (sym == (asymbol **) NULL)
1078 i = count;
1079 else
1081 for (i = 0; i < count; i++, sym++)
1083 register CONST char *name;
1085 name = bfd_asymbol_name (*sym);
1086 if (*name == '_' && strcmp (name, "_gp") == 0)
1088 *pgp = bfd_asymbol_value (*sym);
1089 _bfd_set_gp_value (output_bfd, *pgp);
1090 break;
1095 if (i >= count)
1097 /* Only get the error once. */
1098 *pgp = 4;
1099 _bfd_set_gp_value (output_bfd, *pgp);
1100 *error_message =
1101 (char *) _("GP relative relocation when _gp not defined");
1102 return bfd_reloc_dangerous;
1107 return bfd_reloc_ok;
1110 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1111 become the offset from the gp register. This function also handles
1112 R_MIPS_LITERAL relocations, although those can be handled more
1113 cleverly because the entries in the .lit8 and .lit4 sections can be
1114 merged. */
1116 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1117 arelent *, asection *,
1118 boolean, PTR, bfd_vma));
1120 bfd_reloc_status_type
1121 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1122 output_bfd, error_message)
1123 bfd *abfd;
1124 arelent *reloc_entry;
1125 asymbol *symbol;
1126 PTR data;
1127 asection *input_section;
1128 bfd *output_bfd;
1129 char **error_message;
1131 boolean relocateable;
1132 bfd_reloc_status_type ret;
1133 bfd_vma gp;
1135 /* If we're relocating, and this is an external symbol with no
1136 addend, we don't want to change anything. We will only have an
1137 addend if this is a newly created reloc, not read from an ELF
1138 file. */
1139 if (output_bfd != (bfd *) NULL
1140 && (symbol->flags & BSF_SECTION_SYM) == 0
1141 && reloc_entry->addend == 0)
1143 reloc_entry->address += input_section->output_offset;
1144 return bfd_reloc_ok;
1147 if (output_bfd != (bfd *) NULL)
1148 relocateable = true;
1149 else
1151 relocateable = false;
1152 output_bfd = symbol->section->output_section->owner;
1155 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1156 &gp);
1157 if (ret != bfd_reloc_ok)
1158 return ret;
1160 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1161 relocateable, data, gp);
1164 static bfd_reloc_status_type
1165 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1167 bfd *abfd;
1168 asymbol *symbol;
1169 arelent *reloc_entry;
1170 asection *input_section;
1171 boolean relocateable;
1172 PTR data;
1173 bfd_vma gp;
1175 bfd_vma relocation;
1176 unsigned long insn;
1177 unsigned long val;
1179 if (bfd_is_com_section (symbol->section))
1180 relocation = 0;
1181 else
1182 relocation = symbol->value;
1184 relocation += symbol->section->output_section->vma;
1185 relocation += symbol->section->output_offset;
1187 if (reloc_entry->address > input_section->_cooked_size)
1188 return bfd_reloc_outofrange;
1190 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1192 /* Set val to the offset into the section or symbol. */
1193 if (reloc_entry->howto->src_mask == 0)
1195 /* This case occurs with the 64-bit MIPS ELF ABI. */
1196 val = reloc_entry->addend;
1198 else
1200 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1201 if (val & 0x8000)
1202 val -= 0x10000;
1205 /* Adjust val for the final section location and GP value. If we
1206 are producing relocateable output, we don't want to do this for
1207 an external symbol. */
1208 if (! relocateable
1209 || (symbol->flags & BSF_SECTION_SYM) != 0)
1210 val += relocation - gp;
1212 insn = (insn &~ 0xffff) | (val & 0xffff);
1213 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1215 if (relocateable)
1216 reloc_entry->address += input_section->output_offset;
1218 /* Make sure it fit in 16 bits. */
1219 if (val >= 0x8000 && val < 0xffff8000)
1220 return bfd_reloc_overflow;
1222 return bfd_reloc_ok;
1225 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1226 from the gp register? XXX */
1228 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1229 arelent *, asection *,
1230 boolean, PTR, bfd_vma));
1232 bfd_reloc_status_type
1233 _bfd_mips_elf_gprel32_reloc (abfd,
1234 reloc_entry,
1235 symbol,
1236 data,
1237 input_section,
1238 output_bfd,
1239 error_message)
1240 bfd *abfd;
1241 arelent *reloc_entry;
1242 asymbol *symbol;
1243 PTR data;
1244 asection *input_section;
1245 bfd *output_bfd;
1246 char **error_message;
1248 boolean relocateable;
1249 bfd_reloc_status_type ret;
1250 bfd_vma gp;
1252 /* If we're relocating, and this is an external symbol with no
1253 addend, we don't want to change anything. We will only have an
1254 addend if this is a newly created reloc, not read from an ELF
1255 file. */
1256 if (output_bfd != (bfd *) NULL
1257 && (symbol->flags & BSF_SECTION_SYM) == 0
1258 && reloc_entry->addend == 0)
1260 *error_message = (char *)
1261 _("32bits gp relative relocation occurs for an external symbol");
1262 return bfd_reloc_outofrange;
1265 if (output_bfd != (bfd *) NULL)
1267 relocateable = true;
1268 gp = _bfd_get_gp_value (output_bfd);
1270 else
1272 relocateable = false;
1273 output_bfd = symbol->section->output_section->owner;
1275 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1276 error_message, &gp);
1277 if (ret != bfd_reloc_ok)
1278 return ret;
1281 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1282 relocateable, data, gp);
1285 static bfd_reloc_status_type
1286 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1288 bfd *abfd;
1289 asymbol *symbol;
1290 arelent *reloc_entry;
1291 asection *input_section;
1292 boolean relocateable;
1293 PTR data;
1294 bfd_vma gp;
1296 bfd_vma relocation;
1297 unsigned long val;
1299 if (bfd_is_com_section (symbol->section))
1300 relocation = 0;
1301 else
1302 relocation = symbol->value;
1304 relocation += symbol->section->output_section->vma;
1305 relocation += symbol->section->output_offset;
1307 if (reloc_entry->address > input_section->_cooked_size)
1308 return bfd_reloc_outofrange;
1310 if (reloc_entry->howto->src_mask == 0)
1312 /* This case arises with the 64-bit MIPS ELF ABI. */
1313 val = 0;
1315 else
1316 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1318 /* Set val to the offset into the section or symbol. */
1319 val += reloc_entry->addend;
1321 /* Adjust val for the final section location and GP value. If we
1322 are producing relocateable output, we don't want to do this for
1323 an external symbol. */
1324 if (! relocateable
1325 || (symbol->flags & BSF_SECTION_SYM) != 0)
1326 val += relocation - gp;
1328 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1330 if (relocateable)
1331 reloc_entry->address += input_section->output_offset;
1333 return bfd_reloc_ok;
1336 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1337 generated when addreses are 64 bits. The upper 32 bits are a simle
1338 sign extension. */
1340 static bfd_reloc_status_type
1341 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1342 output_bfd, error_message)
1343 bfd *abfd;
1344 arelent *reloc_entry;
1345 asymbol *symbol;
1346 PTR data;
1347 asection *input_section;
1348 bfd *output_bfd;
1349 char **error_message;
1351 bfd_reloc_status_type r;
1352 arelent reloc32;
1353 unsigned long val;
1354 bfd_size_type addr;
1356 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1357 input_section, output_bfd, error_message);
1358 if (r != bfd_reloc_continue)
1359 return r;
1361 /* Do a normal 32 bit relocation on the lower 32 bits. */
1362 reloc32 = *reloc_entry;
1363 if (bfd_big_endian (abfd))
1364 reloc32.address += 4;
1365 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1366 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1367 output_bfd, error_message);
1369 /* Sign extend into the upper 32 bits. */
1370 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1371 if ((val & 0x80000000) != 0)
1372 val = 0xffffffff;
1373 else
1374 val = 0;
1375 addr = reloc_entry->address;
1376 if (bfd_little_endian (abfd))
1377 addr += 4;
1378 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1380 return r;
1383 /* Handle a mips16 jump. */
1385 static bfd_reloc_status_type
1386 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1387 output_bfd, error_message)
1388 bfd *abfd;
1389 arelent *reloc_entry;
1390 asymbol *symbol;
1391 PTR data;
1392 asection *input_section;
1393 bfd *output_bfd;
1394 char **error_message;
1396 if (output_bfd != (bfd *) NULL
1397 && (symbol->flags & BSF_SECTION_SYM) == 0
1398 && reloc_entry->addend == 0)
1400 reloc_entry->address += input_section->output_offset;
1401 return bfd_reloc_ok;
1404 /* FIXME. */
1406 static boolean warned;
1408 if (! warned)
1409 (*_bfd_error_handler)
1410 (_("Linking mips16 objects into %s format is not supported"),
1411 bfd_get_target (input_section->output_section->owner));
1412 warned = true;
1415 return bfd_reloc_undefined;
1418 /* Handle a mips16 GP relative reloc. */
1420 static bfd_reloc_status_type
1421 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1422 output_bfd, error_message)
1423 bfd *abfd;
1424 arelent *reloc_entry;
1425 asymbol *symbol;
1426 PTR data;
1427 asection *input_section;
1428 bfd *output_bfd;
1429 char **error_message;
1431 boolean relocateable;
1432 bfd_reloc_status_type ret;
1433 bfd_vma gp;
1434 unsigned short extend, insn;
1435 unsigned long final;
1437 /* If we're relocating, and this is an external symbol with no
1438 addend, we don't want to change anything. We will only have an
1439 addend if this is a newly created reloc, not read from an ELF
1440 file. */
1441 if (output_bfd != NULL
1442 && (symbol->flags & BSF_SECTION_SYM) == 0
1443 && reloc_entry->addend == 0)
1445 reloc_entry->address += input_section->output_offset;
1446 return bfd_reloc_ok;
1449 if (output_bfd != NULL)
1450 relocateable = true;
1451 else
1453 relocateable = false;
1454 output_bfd = symbol->section->output_section->owner;
1457 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1458 &gp);
1459 if (ret != bfd_reloc_ok)
1460 return ret;
1462 if (reloc_entry->address > input_section->_cooked_size)
1463 return bfd_reloc_outofrange;
1465 /* Pick up the mips16 extend instruction and the real instruction. */
1466 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1467 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1469 /* Stuff the current addend back as a 32 bit value, do the usual
1470 relocation, and then clean up. */
1471 bfd_put_32 (abfd,
1472 (((extend & 0x1f) << 11)
1473 | (extend & 0x7e0)
1474 | (insn & 0x1f)),
1475 (bfd_byte *) data + reloc_entry->address);
1477 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1478 relocateable, data, gp);
1480 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1481 bfd_put_16 (abfd,
1482 ((extend & 0xf800)
1483 | ((final >> 11) & 0x1f)
1484 | (final & 0x7e0)),
1485 (bfd_byte *) data + reloc_entry->address);
1486 bfd_put_16 (abfd,
1487 ((insn & 0xffe0)
1488 | (final & 0x1f)),
1489 (bfd_byte *) data + reloc_entry->address + 2);
1491 return ret;
1494 /* Return the ISA for a MIPS e_flags value. */
1496 static INLINE int
1497 elf_mips_isa (flags)
1498 flagword flags;
1500 switch (flags & EF_MIPS_ARCH)
1502 case E_MIPS_ARCH_1:
1503 return 1;
1504 case E_MIPS_ARCH_2:
1505 return 2;
1506 case E_MIPS_ARCH_3:
1507 return 3;
1508 case E_MIPS_ARCH_4:
1509 return 4;
1511 return 4;
1514 /* Return the MACH for a MIPS e_flags value. */
1516 static INLINE int
1517 elf_mips_mach (flags)
1518 flagword flags;
1520 switch (flags & EF_MIPS_MACH)
1522 case E_MIPS_MACH_3900:
1523 return bfd_mach_mips3900;
1525 case E_MIPS_MACH_4010:
1526 return bfd_mach_mips4010;
1528 case E_MIPS_MACH_4100:
1529 return bfd_mach_mips4100;
1531 case E_MIPS_MACH_4111:
1532 return bfd_mach_mips4111;
1534 case E_MIPS_MACH_4650:
1535 return bfd_mach_mips4650;
1537 default:
1538 switch (flags & EF_MIPS_ARCH)
1540 default:
1541 case E_MIPS_ARCH_1:
1542 return bfd_mach_mips3000;
1543 break;
1545 case E_MIPS_ARCH_2:
1546 return bfd_mach_mips6000;
1547 break;
1549 case E_MIPS_ARCH_3:
1550 return bfd_mach_mips4000;
1551 break;
1553 case E_MIPS_ARCH_4:
1554 return bfd_mach_mips8000;
1555 break;
1559 return 0;
1562 /* Return printable name for ABI from flagword. */
1564 static INLINE char*
1565 elf_mips_abi_name (flags)
1566 flagword flags;
1568 switch (flags & EF_MIPS_ABI)
1570 case 0:
1571 return "none";
1572 case E_MIPS_ABI_O32:
1573 return "O32";
1574 case E_MIPS_ABI_O64:
1575 return "O64";
1576 case E_MIPS_ABI_EABI32:
1577 return "EABI32";
1578 case E_MIPS_ABI_EABI64:
1579 return "EABI64";
1580 default:
1581 return "unknown abi";
1585 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1587 struct elf_reloc_map {
1588 bfd_reloc_code_real_type bfd_reloc_val;
1589 enum elf_mips_reloc_type elf_reloc_val;
1592 static CONST struct elf_reloc_map mips_reloc_map[] =
1594 { BFD_RELOC_NONE, R_MIPS_NONE, },
1595 { BFD_RELOC_16, R_MIPS_16 },
1596 { BFD_RELOC_32, R_MIPS_32 },
1597 { BFD_RELOC_64, R_MIPS_64 },
1598 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1599 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1600 { BFD_RELOC_LO16, R_MIPS_LO16 },
1601 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1602 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1603 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1604 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1605 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1606 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1607 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1608 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1609 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1610 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 }
1613 /* Given a BFD reloc type, return a howto structure. */
1615 static reloc_howto_type *
1616 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1617 bfd *abfd;
1618 bfd_reloc_code_real_type code;
1620 unsigned int i;
1622 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1624 if (mips_reloc_map[i].bfd_reloc_val == code)
1625 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1628 switch (code)
1630 default:
1631 bfd_set_error (bfd_error_bad_value);
1632 return NULL;
1634 case BFD_RELOC_CTOR:
1635 /* We need to handle BFD_RELOC_CTOR specially.
1636 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1637 size of addresses on this architecture. */
1638 if (bfd_arch_bits_per_address (abfd) == 32)
1639 return &elf_mips_howto_table[(int) R_MIPS_32];
1640 else
1641 return &elf_mips_ctor64_howto;
1643 case BFD_RELOC_MIPS16_JMP:
1644 return &elf_mips16_jump_howto;
1645 case BFD_RELOC_MIPS16_GPREL:
1646 return &elf_mips16_gprel_howto;
1647 case BFD_RELOC_VTABLE_INHERIT:
1648 return &elf_mips_gnu_vtinherit_howto;
1649 case BFD_RELOC_VTABLE_ENTRY:
1650 return &elf_mips_gnu_vtentry_howto;
1654 /* Given a MIPS reloc type, fill in an arelent structure. */
1656 static void
1657 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1658 bfd *abfd;
1659 arelent *cache_ptr;
1660 Elf32_Internal_Rel *dst;
1662 unsigned int r_type;
1664 r_type = ELF32_R_TYPE (dst->r_info);
1665 switch (r_type)
1667 case R_MIPS16_26:
1668 cache_ptr->howto = &elf_mips16_jump_howto;
1669 break;
1670 case R_MIPS16_GPREL:
1671 cache_ptr->howto = &elf_mips16_gprel_howto;
1672 break;
1673 case R_MIPS_GNU_VTINHERIT:
1674 cache_ptr->howto = &elf_mips_gnu_vtinherit_howto;
1675 break;
1676 case R_MIPS_GNU_VTENTRY:
1677 cache_ptr->howto = &elf_mips_gnu_vtentry_howto;
1678 break;
1680 default:
1681 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1682 cache_ptr->howto = &elf_mips_howto_table[r_type];
1683 break;
1686 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1687 value for the object file. We get the addend now, rather than
1688 when we do the relocation, because the symbol manipulations done
1689 by the linker may cause us to lose track of the input BFD. */
1690 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1691 && (r_type == (unsigned int) R_MIPS_GPREL16
1692 || r_type == (unsigned int) R_MIPS_LITERAL))
1693 cache_ptr->addend = elf_gp (abfd);
1696 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1697 routines swap this structure in and out. They are used outside of
1698 BFD, so they are globally visible. */
1700 void
1701 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1702 bfd *abfd;
1703 const Elf32_External_RegInfo *ex;
1704 Elf32_RegInfo *in;
1706 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1707 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1708 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1709 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1710 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1711 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1714 void
1715 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1716 bfd *abfd;
1717 const Elf32_RegInfo *in;
1718 Elf32_External_RegInfo *ex;
1720 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1721 (bfd_byte *) ex->ri_gprmask);
1722 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1723 (bfd_byte *) ex->ri_cprmask[0]);
1724 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1725 (bfd_byte *) ex->ri_cprmask[1]);
1726 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1727 (bfd_byte *) ex->ri_cprmask[2]);
1728 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1729 (bfd_byte *) ex->ri_cprmask[3]);
1730 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1731 (bfd_byte *) ex->ri_gp_value);
1734 /* In the 64 bit ABI, the .MIPS.options section holds register
1735 information in an Elf64_Reginfo structure. These routines swap
1736 them in and out. They are globally visible because they are used
1737 outside of BFD. These routines are here so that gas can call them
1738 without worrying about whether the 64 bit ABI has been included. */
1740 void
1741 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1742 bfd *abfd;
1743 const Elf64_External_RegInfo *ex;
1744 Elf64_Internal_RegInfo *in;
1746 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1747 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1748 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1749 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1750 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1751 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1752 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1755 void
1756 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1757 bfd *abfd;
1758 const Elf64_Internal_RegInfo *in;
1759 Elf64_External_RegInfo *ex;
1761 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1762 (bfd_byte *) ex->ri_gprmask);
1763 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1764 (bfd_byte *) ex->ri_pad);
1765 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1766 (bfd_byte *) ex->ri_cprmask[0]);
1767 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1768 (bfd_byte *) ex->ri_cprmask[1]);
1769 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1770 (bfd_byte *) ex->ri_cprmask[2]);
1771 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1772 (bfd_byte *) ex->ri_cprmask[3]);
1773 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1774 (bfd_byte *) ex->ri_gp_value);
1777 /* Swap an entry in a .gptab section. Note that these routines rely
1778 on the equivalence of the two elements of the union. */
1780 static void
1781 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1782 bfd *abfd;
1783 const Elf32_External_gptab *ex;
1784 Elf32_gptab *in;
1786 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1787 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1790 static void
1791 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1792 bfd *abfd;
1793 const Elf32_gptab *in;
1794 Elf32_External_gptab *ex;
1796 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1797 ex->gt_entry.gt_g_value);
1798 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1799 ex->gt_entry.gt_bytes);
1802 static void
1803 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1804 bfd *abfd;
1805 const Elf32_compact_rel *in;
1806 Elf32_External_compact_rel *ex;
1808 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1809 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1810 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1811 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1812 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1813 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1816 static void
1817 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1818 bfd *abfd;
1819 const Elf32_crinfo *in;
1820 Elf32_External_crinfo *ex;
1822 unsigned long l;
1824 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1825 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1826 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1827 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1828 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1829 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1830 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1833 /* Swap in an options header. */
1835 void
1836 bfd_mips_elf_swap_options_in (abfd, ex, in)
1837 bfd *abfd;
1838 const Elf_External_Options *ex;
1839 Elf_Internal_Options *in;
1841 in->kind = bfd_h_get_8 (abfd, ex->kind);
1842 in->size = bfd_h_get_8 (abfd, ex->size);
1843 in->section = bfd_h_get_16 (abfd, ex->section);
1844 in->info = bfd_h_get_32 (abfd, ex->info);
1847 /* Swap out an options header. */
1849 void
1850 bfd_mips_elf_swap_options_out (abfd, in, ex)
1851 bfd *abfd;
1852 const Elf_Internal_Options *in;
1853 Elf_External_Options *ex;
1855 bfd_h_put_8 (abfd, in->kind, ex->kind);
1856 bfd_h_put_8 (abfd, in->size, ex->size);
1857 bfd_h_put_16 (abfd, in->section, ex->section);
1858 bfd_h_put_32 (abfd, in->info, ex->info);
1861 /* Determine whether a symbol is global for the purposes of splitting
1862 the symbol table into global symbols and local symbols. At least
1863 on Irix 5, this split must be between section symbols and all other
1864 symbols. On most ELF targets the split is between static symbols
1865 and externally visible symbols. */
1867 /*ARGSUSED*/
1868 static boolean
1869 mips_elf_sym_is_global (abfd, sym)
1870 bfd *abfd;
1871 asymbol *sym;
1873 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1876 /* Set the right machine number for a MIPS ELF file. This is used for
1877 both the 32-bit and the 64-bit ABI. */
1879 boolean
1880 _bfd_mips_elf_object_p (abfd)
1881 bfd *abfd;
1883 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
1884 elf_mips_mach (elf_elfheader (abfd)->e_flags));
1885 return true;
1888 /* Set the right machine number for a 32-bit MIPS ELF file. */
1890 static boolean
1891 mips_elf32_object_p (abfd)
1892 bfd *abfd;
1894 /* Irix 5 is broken. Object file symbol tables are not always
1895 sorted correctly such that local symbols precede global symbols,
1896 and the sh_info field in the symbol table is not always right. */
1897 elf_bad_symtab (abfd) = true;
1899 return _bfd_mips_elf_object_p (abfd);
1902 /* The final processing done just before writing out a MIPS ELF object
1903 file. This gets the MIPS architecture right based on the machine
1904 number. This is used by both the 32-bit and the 64-bit ABI. */
1906 /*ARGSUSED*/
1907 void
1908 _bfd_mips_elf_final_write_processing (abfd, linker)
1909 bfd *abfd;
1910 boolean linker;
1912 unsigned long val;
1913 unsigned int i;
1914 Elf_Internal_Shdr **hdrpp;
1915 const char *name;
1916 asection *sec;
1918 switch (bfd_get_mach (abfd))
1920 default:
1921 case bfd_mach_mips3000:
1922 val = E_MIPS_ARCH_1;
1923 break;
1925 case bfd_mach_mips3900:
1926 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
1927 break;
1929 case bfd_mach_mips6000:
1930 val = E_MIPS_ARCH_2;
1931 break;
1933 case bfd_mach_mips4000:
1934 case bfd_mach_mips4300:
1935 val = E_MIPS_ARCH_3;
1936 break;
1938 case bfd_mach_mips4010:
1939 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
1940 break;
1942 case bfd_mach_mips4100:
1943 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
1944 break;
1946 case bfd_mach_mips4111:
1947 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
1948 break;
1950 case bfd_mach_mips4650:
1951 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
1952 break;
1954 case bfd_mach_mips8000:
1955 val = E_MIPS_ARCH_4;
1956 break;
1959 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
1960 elf_elfheader (abfd)->e_flags |= val;
1962 /* Set the sh_info field for .gptab sections and other appropriate
1963 info for each special section. */
1964 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1965 i < elf_elfheader (abfd)->e_shnum;
1966 i++, hdrpp++)
1968 switch ((*hdrpp)->sh_type)
1970 case SHT_MIPS_LIBLIST:
1971 sec = bfd_get_section_by_name (abfd, ".dynstr");
1972 if (sec != NULL)
1973 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1974 break;
1976 case SHT_MIPS_GPTAB:
1977 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1978 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1979 BFD_ASSERT (name != NULL
1980 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
1981 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
1982 BFD_ASSERT (sec != NULL);
1983 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1984 break;
1986 case SHT_MIPS_CONTENT:
1987 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1988 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1989 BFD_ASSERT (name != NULL
1990 && strncmp (name, ".MIPS.content",
1991 sizeof ".MIPS.content" - 1) == 0);
1992 sec = bfd_get_section_by_name (abfd,
1993 name + sizeof ".MIPS.content" - 1);
1994 BFD_ASSERT (sec != NULL);
1995 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1996 break;
1998 case SHT_MIPS_SYMBOL_LIB:
1999 sec = bfd_get_section_by_name (abfd, ".dynsym");
2000 if (sec != NULL)
2001 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2002 sec = bfd_get_section_by_name (abfd, ".liblist");
2003 if (sec != NULL)
2004 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2005 break;
2007 case SHT_MIPS_EVENTS:
2008 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2009 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2010 BFD_ASSERT (name != NULL);
2011 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2012 sec = bfd_get_section_by_name (abfd,
2013 name + sizeof ".MIPS.events" - 1);
2014 else
2016 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2017 sizeof ".MIPS.post_rel" - 1) == 0);
2018 sec = bfd_get_section_by_name (abfd,
2019 (name
2020 + sizeof ".MIPS.post_rel" - 1));
2022 BFD_ASSERT (sec != NULL);
2023 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2024 break;
2030 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2032 boolean
2033 _bfd_mips_elf_set_private_flags (abfd, flags)
2034 bfd *abfd;
2035 flagword flags;
2037 BFD_ASSERT (!elf_flags_init (abfd)
2038 || elf_elfheader (abfd)->e_flags == flags);
2040 elf_elfheader (abfd)->e_flags = flags;
2041 elf_flags_init (abfd) = true;
2042 return true;
2045 /* Copy backend specific data from one object module to another */
2047 boolean
2048 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2049 bfd *ibfd;
2050 bfd *obfd;
2052 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2053 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2054 return true;
2056 BFD_ASSERT (!elf_flags_init (obfd)
2057 || (elf_elfheader (obfd)->e_flags
2058 == elf_elfheader (ibfd)->e_flags));
2060 elf_gp (obfd) = elf_gp (ibfd);
2061 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2062 elf_flags_init (obfd) = true;
2063 return true;
2066 /* Merge backend specific data from an object file to the output
2067 object file when linking. */
2069 boolean
2070 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2071 bfd *ibfd;
2072 bfd *obfd;
2074 flagword old_flags;
2075 flagword new_flags;
2076 boolean ok;
2078 /* Check if we have the same endianess */
2079 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2080 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2082 const char *msg;
2084 if (bfd_big_endian (ibfd))
2085 msg = _("%s: compiled for a big endian system and target is little endian");
2086 else
2087 msg = _("%s: compiled for a little endian system and target is big endian");
2089 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2091 bfd_set_error (bfd_error_wrong_format);
2092 return false;
2095 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2096 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2097 return true;
2099 new_flags = elf_elfheader (ibfd)->e_flags;
2100 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2101 old_flags = elf_elfheader (obfd)->e_flags;
2103 if (! elf_flags_init (obfd))
2105 elf_flags_init (obfd) = true;
2106 elf_elfheader (obfd)->e_flags = new_flags;
2108 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2109 && bfd_get_arch_info (obfd)->the_default)
2111 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2112 bfd_get_mach (ibfd)))
2113 return false;
2116 return true;
2119 /* Check flag compatibility. */
2121 new_flags &= ~EF_MIPS_NOREORDER;
2122 old_flags &= ~EF_MIPS_NOREORDER;
2124 if (new_flags == old_flags)
2125 return true;
2127 ok = true;
2129 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2131 new_flags &= ~EF_MIPS_PIC;
2132 old_flags &= ~EF_MIPS_PIC;
2133 (*_bfd_error_handler)
2134 (_("%s: linking PIC files with non-PIC files"),
2135 bfd_get_filename (ibfd));
2136 ok = false;
2139 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2141 new_flags &= ~EF_MIPS_CPIC;
2142 old_flags &= ~EF_MIPS_CPIC;
2143 (*_bfd_error_handler)
2144 (_("%s: linking abicalls files with non-abicalls files"),
2145 bfd_get_filename (ibfd));
2146 ok = false;
2149 /* Compare the ISA's. */
2150 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2151 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2153 int new_mach = new_flags & EF_MIPS_MACH;
2154 int old_mach = old_flags & EF_MIPS_MACH;
2155 int new_isa = elf_mips_isa (new_flags);
2156 int old_isa = elf_mips_isa (old_flags);
2158 /* If either has no machine specified, just compare the general isa's.
2159 Some combinations of machines are ok, if the isa's match. */
2160 if (! new_mach
2161 || ! old_mach
2162 || new_mach == old_mach
2165 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2166 and -mips4 code. They will normally use the same data sizes and
2167 calling conventions. */
2169 if ((new_isa == 1 || new_isa == 2)
2170 ? (old_isa != 1 && old_isa != 2)
2171 : (old_isa == 1 || old_isa == 2))
2173 (*_bfd_error_handler)
2174 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2175 bfd_get_filename (ibfd), new_isa, old_isa);
2176 ok = false;
2180 else
2182 (*_bfd_error_handler)
2183 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2184 bfd_get_filename (ibfd),
2185 elf_mips_mach (new_flags),
2186 elf_mips_mach (old_flags));
2187 ok = false;
2190 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2191 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2194 /* Compare ABI's */
2195 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI))
2197 /* Only error if both are set (to different values). */
2198 if ((new_flags & EF_MIPS_ABI)
2199 && (old_flags & EF_MIPS_ABI))
2201 (*_bfd_error_handler)
2202 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2203 bfd_get_filename (ibfd),
2204 elf_mips_abi_name (new_flags),
2205 elf_mips_abi_name (old_flags));
2206 ok = false;
2208 new_flags &= ~EF_MIPS_ABI;
2209 old_flags &= ~EF_MIPS_ABI;
2212 /* Warn about any other mismatches */
2213 if (new_flags != old_flags)
2215 (*_bfd_error_handler)
2216 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2217 bfd_get_filename (ibfd), (unsigned long) new_flags,
2218 (unsigned long) old_flags);
2219 ok = false;
2222 if (! ok)
2224 bfd_set_error (bfd_error_bad_value);
2225 return false;
2228 return true;
2231 static boolean
2232 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2233 bfd *abfd;
2234 PTR ptr;
2236 FILE *file = (FILE *) ptr;
2238 BFD_ASSERT (abfd != NULL && ptr != NULL);
2240 /* Print normal ELF private data. */
2241 _bfd_elf_print_private_bfd_data (abfd, ptr);
2243 /* xgettext:c-format */
2244 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2246 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2247 fprintf (file, _ (" [abi=O32]"));
2248 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2249 fprintf (file, _ (" [abi=O64]"));
2250 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2251 fprintf (file, _ (" [abi=EABI32]"));
2252 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2253 fprintf (file, _ (" [abi=EABI64]"));
2254 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2255 fprintf (file, _ (" [abi unknown]"));
2256 else
2257 fprintf (file, _ (" [no abi set]"));
2259 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2260 fprintf (file, _ (" [mips1]"));
2261 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2262 fprintf (file, _ (" [mips2]"));
2263 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2264 fprintf (file, _ (" [mips3]"));
2265 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2266 fprintf (file, _ (" [mips4]"));
2267 else
2268 fprintf (file, _ (" [unknown ISA]"));
2270 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2271 fprintf (file, _ (" [32bitmode]"));
2272 else
2273 fprintf (file, _ (" [not 32bitmode]"));
2275 fputc ('\n', file);
2277 return true;
2280 /* Handle a MIPS specific section when reading an object file. This
2281 is called when elfcode.h finds a section with an unknown type.
2282 This routine supports both the 32-bit and 64-bit ELF ABI.
2284 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2285 how to. */
2287 boolean
2288 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2289 bfd *abfd;
2290 Elf_Internal_Shdr *hdr;
2291 const char *name;
2293 flagword flags = 0;
2295 /* There ought to be a place to keep ELF backend specific flags, but
2296 at the moment there isn't one. We just keep track of the
2297 sections by their name, instead. Fortunately, the ABI gives
2298 suggested names for all the MIPS specific sections, so we will
2299 probably get away with this. */
2300 switch (hdr->sh_type)
2302 case SHT_MIPS_LIBLIST:
2303 if (strcmp (name, ".liblist") != 0)
2304 return false;
2305 break;
2306 case SHT_MIPS_MSYM:
2307 if (strcmp (name, ".msym") != 0)
2308 return false;
2309 break;
2310 case SHT_MIPS_CONFLICT:
2311 if (strcmp (name, ".conflict") != 0)
2312 return false;
2313 break;
2314 case SHT_MIPS_GPTAB:
2315 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2316 return false;
2317 break;
2318 case SHT_MIPS_UCODE:
2319 if (strcmp (name, ".ucode") != 0)
2320 return false;
2321 break;
2322 case SHT_MIPS_DEBUG:
2323 if (strcmp (name, ".mdebug") != 0)
2324 return false;
2325 flags = SEC_DEBUGGING;
2326 break;
2327 case SHT_MIPS_REGINFO:
2328 if (strcmp (name, ".reginfo") != 0
2329 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2330 return false;
2331 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2332 break;
2333 case SHT_MIPS_IFACE:
2334 if (strcmp (name, ".MIPS.interfaces") != 0)
2335 return false;
2336 break;
2337 case SHT_MIPS_CONTENT:
2338 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2339 return false;
2340 break;
2341 case SHT_MIPS_OPTIONS:
2342 if (strcmp (name, ".options") != 0
2343 && strcmp (name, ".MIPS.options") != 0)
2344 return false;
2345 break;
2346 case SHT_MIPS_DWARF:
2347 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2348 return false;
2349 break;
2350 case SHT_MIPS_SYMBOL_LIB:
2351 if (strcmp (name, ".MIPS.symlib") != 0)
2352 return false;
2353 break;
2354 case SHT_MIPS_EVENTS:
2355 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2356 && strncmp (name, ".MIPS.post_rel",
2357 sizeof ".MIPS.post_rel" - 1) != 0)
2358 return false;
2359 break;
2360 default:
2361 return false;
2364 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2365 return false;
2367 if (flags)
2369 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2370 (bfd_get_section_flags (abfd,
2371 hdr->bfd_section)
2372 | flags)))
2373 return false;
2376 return true;
2379 /* Handle a 32-bit MIPS ELF specific section. */
2381 static boolean
2382 mips_elf32_section_from_shdr (abfd, hdr, name)
2383 bfd *abfd;
2384 Elf_Internal_Shdr *hdr;
2385 char *name;
2387 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2388 return false;
2390 /* FIXME: We should record sh_info for a .gptab section. */
2392 /* For a .reginfo section, set the gp value in the tdata information
2393 from the contents of this section. We need the gp value while
2394 processing relocs, so we just get it now. The .reginfo section
2395 is not used in the 64-bit MIPS ELF ABI. */
2396 if (hdr->sh_type == SHT_MIPS_REGINFO)
2398 Elf32_External_RegInfo ext;
2399 Elf32_RegInfo s;
2401 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2402 (file_ptr) 0, sizeof ext))
2403 return false;
2404 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2405 elf_gp (abfd) = s.ri_gp_value;
2408 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2409 set the gp value based on what we find. We may see both
2410 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2411 they should agree. */
2412 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2414 bfd_byte *contents, *l, *lend;
2416 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2417 if (contents == NULL)
2418 return false;
2419 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2420 (file_ptr) 0, hdr->sh_size))
2422 free (contents);
2423 return false;
2425 l = contents;
2426 lend = contents + hdr->sh_size;
2427 while (l + sizeof (Elf_External_Options) <= lend)
2429 Elf_Internal_Options intopt;
2431 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2432 &intopt);
2433 if (intopt.kind == ODK_REGINFO)
2435 Elf32_RegInfo intreg;
2437 bfd_mips_elf32_swap_reginfo_in
2438 (abfd,
2439 ((Elf32_External_RegInfo *)
2440 (l + sizeof (Elf_External_Options))),
2441 &intreg);
2442 elf_gp (abfd) = intreg.ri_gp_value;
2444 l += intopt.size;
2446 free (contents);
2449 return true;
2452 /* Set the correct type for a MIPS ELF section. We do this by the
2453 section name, which is a hack, but ought to work. This routine is
2454 used by both the 32-bit and the 64-bit ABI. */
2456 boolean
2457 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2458 bfd *abfd;
2459 Elf32_Internal_Shdr *hdr;
2460 asection *sec;
2462 register const char *name;
2464 name = bfd_get_section_name (abfd, sec);
2466 if (strcmp (name, ".liblist") == 0)
2468 hdr->sh_type = SHT_MIPS_LIBLIST;
2469 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2470 /* The sh_link field is set in final_write_processing. */
2472 else if (strcmp (name, ".msym") == 0)
2474 hdr->sh_type = SHT_MIPS_MSYM;
2475 hdr->sh_entsize = 8;
2476 /* FIXME: Set the sh_info field. */
2478 else if (strcmp (name, ".conflict") == 0)
2479 hdr->sh_type = SHT_MIPS_CONFLICT;
2480 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2482 hdr->sh_type = SHT_MIPS_GPTAB;
2483 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2484 /* The sh_info field is set in final_write_processing. */
2486 else if (strcmp (name, ".ucode") == 0)
2487 hdr->sh_type = SHT_MIPS_UCODE;
2488 else if (strcmp (name, ".mdebug") == 0)
2490 hdr->sh_type = SHT_MIPS_DEBUG;
2491 /* In a shared object on Irix 5.3, the .mdebug section has an
2492 entsize of 0. FIXME: Does this matter? */
2493 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2494 hdr->sh_entsize = 0;
2495 else
2496 hdr->sh_entsize = 1;
2498 else if (strcmp (name, ".reginfo") == 0)
2500 hdr->sh_type = SHT_MIPS_REGINFO;
2501 /* In a shared object on Irix 5.3, the .reginfo section has an
2502 entsize of 0x18. FIXME: Does this matter? */
2503 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2504 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2505 else
2506 hdr->sh_entsize = 1;
2508 else if (SGI_COMPAT (abfd)
2509 && (strcmp (name, ".hash") == 0
2510 || strcmp (name, ".dynamic") == 0
2511 || strcmp (name, ".dynstr") == 0))
2513 hdr->sh_entsize = 0;
2514 #if 0
2515 /* This isn't how the Irix 6 linker behaves. */
2516 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2517 #endif
2519 else if (strcmp (name, ".got") == 0
2520 || strcmp (name, ".sdata") == 0
2521 || strcmp (name, ".sbss") == 0
2522 || strcmp (name, ".lit4") == 0
2523 || strcmp (name, ".lit8") == 0)
2524 hdr->sh_flags |= SHF_MIPS_GPREL;
2525 else if (strcmp (name, ".MIPS.interfaces") == 0)
2527 hdr->sh_type = SHT_MIPS_IFACE;
2528 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2530 else if (strcmp (name, ".MIPS.content") == 0)
2532 hdr->sh_type = SHT_MIPS_CONTENT;
2533 /* The sh_info field is set in final_write_processing. */
2535 else if (strcmp (name, ".options") == 0
2536 || strcmp (name, ".MIPS.options") == 0)
2538 hdr->sh_type = SHT_MIPS_OPTIONS;
2539 hdr->sh_entsize = 1;
2540 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2542 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2543 hdr->sh_type = SHT_MIPS_DWARF;
2544 else if (strcmp (name, ".MIPS.symlib") == 0)
2546 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2547 /* The sh_link and sh_info fields are set in
2548 final_write_processing. */
2550 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2551 || strncmp (name, ".MIPS.post_rel",
2552 sizeof ".MIPS.post_rel" - 1) == 0)
2554 hdr->sh_type = SHT_MIPS_EVENTS;
2555 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2556 /* The sh_link field is set in final_write_processing. */
2559 return true;
2562 /* Given a BFD section, try to locate the corresponding ELF section
2563 index. This is used by both the 32-bit and the 64-bit ABI.
2564 Actually, it's not clear to me that the 64-bit ABI supports these,
2565 but for non-PIC objects we will certainly want support for at least
2566 the .scommon section. */
2568 boolean
2569 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2570 bfd *abfd;
2571 Elf32_Internal_Shdr *hdr;
2572 asection *sec;
2573 int *retval;
2575 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2577 *retval = SHN_MIPS_SCOMMON;
2578 return true;
2580 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2582 *retval = SHN_MIPS_ACOMMON;
2583 return true;
2585 return false;
2588 /* When are writing out the .options or .MIPS.options section,
2589 remember the bytes we are writing out, so that we can install the
2590 GP value in the section_processing routine. */
2592 boolean
2593 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2594 bfd *abfd;
2595 sec_ptr section;
2596 PTR location;
2597 file_ptr offset;
2598 bfd_size_type count;
2600 if (strcmp (section->name, ".options") == 0
2601 || strcmp (section->name, ".MIPS.options") == 0)
2603 bfd_byte *c;
2605 if (elf_section_data (section) == NULL)
2607 section->used_by_bfd =
2608 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2609 if (elf_section_data (section) == NULL)
2610 return false;
2612 c = (bfd_byte *) elf_section_data (section)->tdata;
2613 if (c == NULL)
2615 bfd_size_type size;
2617 if (section->_cooked_size != 0)
2618 size = section->_cooked_size;
2619 else
2620 size = section->_raw_size;
2621 c = (bfd_byte *) bfd_zalloc (abfd, size);
2622 if (c == NULL)
2623 return false;
2624 elf_section_data (section)->tdata = (PTR) c;
2627 memcpy (c + offset, location, count);
2630 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2631 count);
2634 /* Work over a section just before writing it out. This routine is
2635 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2636 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2637 a better way. */
2639 boolean
2640 _bfd_mips_elf_section_processing (abfd, hdr)
2641 bfd *abfd;
2642 Elf_Internal_Shdr *hdr;
2644 if (hdr->bfd_section != NULL)
2646 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2648 if (strcmp (name, ".sdata") == 0)
2650 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2651 hdr->sh_type = SHT_PROGBITS;
2653 else if (strcmp (name, ".sbss") == 0)
2655 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2656 hdr->sh_type = SHT_NOBITS;
2658 else if (strcmp (name, ".lit8") == 0
2659 || strcmp (name, ".lit4") == 0)
2661 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2662 hdr->sh_type = SHT_PROGBITS;
2664 else if (strcmp (name, ".compact_rel") == 0)
2666 hdr->sh_flags = 0;
2667 hdr->sh_type = SHT_PROGBITS;
2669 else if (strcmp (name, ".rtproc") == 0)
2671 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2673 unsigned int adjust;
2675 adjust = hdr->sh_size % hdr->sh_addralign;
2676 if (adjust != 0)
2677 hdr->sh_size += hdr->sh_addralign - adjust;
2682 return true;
2685 /* Work over a section just before writing it out. We update the GP
2686 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2687 on the value we are using. */
2689 static boolean
2690 mips_elf32_section_processing (abfd, hdr)
2691 bfd *abfd;
2692 Elf32_Internal_Shdr *hdr;
2694 if (hdr->sh_type == SHT_MIPS_REGINFO)
2696 bfd_byte buf[4];
2698 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2699 BFD_ASSERT (hdr->contents == NULL);
2701 if (bfd_seek (abfd,
2702 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2703 SEEK_SET) == -1)
2704 return false;
2705 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2706 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2707 return false;
2710 if (hdr->sh_type == SHT_MIPS_OPTIONS
2711 && hdr->bfd_section != NULL
2712 && elf_section_data (hdr->bfd_section) != NULL
2713 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2715 bfd_byte *contents, *l, *lend;
2717 /* We stored the section contents in the elf_section_data tdata
2718 field in the set_section_contents routine. We save the
2719 section contents so that we don't have to read them again.
2720 At this point we know that elf_gp is set, so we can look
2721 through the section contents to see if there is an
2722 ODK_REGINFO structure. */
2724 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2725 l = contents;
2726 lend = contents + hdr->sh_size;
2727 while (l + sizeof (Elf_External_Options) <= lend)
2729 Elf_Internal_Options intopt;
2731 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2732 &intopt);
2733 if (intopt.kind == ODK_REGINFO)
2735 bfd_byte buf[4];
2737 if (bfd_seek (abfd,
2738 (hdr->sh_offset
2739 + (l - contents)
2740 + sizeof (Elf_External_Options)
2741 + (sizeof (Elf32_External_RegInfo) - 4)),
2742 SEEK_SET) == -1)
2743 return false;
2744 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2745 if (bfd_write (buf, 1, 4, abfd) != 4)
2746 return false;
2748 l += intopt.size;
2752 return _bfd_mips_elf_section_processing (abfd, hdr);
2755 /* MIPS ELF uses two common sections. One is the usual one, and the
2756 other is for small objects. All the small objects are kept
2757 together, and then referenced via the gp pointer, which yields
2758 faster assembler code. This is what we use for the small common
2759 section. This approach is copied from ecoff.c. */
2760 static asection mips_elf_scom_section;
2761 static asymbol mips_elf_scom_symbol;
2762 static asymbol *mips_elf_scom_symbol_ptr;
2764 /* MIPS ELF also uses an acommon section, which represents an
2765 allocated common symbol which may be overridden by a
2766 definition in a shared library. */
2767 static asection mips_elf_acom_section;
2768 static asymbol mips_elf_acom_symbol;
2769 static asymbol *mips_elf_acom_symbol_ptr;
2771 /* The Irix 5 support uses two virtual sections, which represent
2772 text/data symbols defined in dynamic objects. */
2773 static asection mips_elf_text_section;
2774 static asection *mips_elf_text_section_ptr;
2775 static asymbol mips_elf_text_symbol;
2776 static asymbol *mips_elf_text_symbol_ptr;
2778 static asection mips_elf_data_section;
2779 static asection *mips_elf_data_section_ptr;
2780 static asymbol mips_elf_data_symbol;
2781 static asymbol *mips_elf_data_symbol_ptr;
2783 /* Handle the special MIPS section numbers that a symbol may use.
2784 This is used for both the 32-bit and the 64-bit ABI. */
2786 void
2787 _bfd_mips_elf_symbol_processing (abfd, asym)
2788 bfd *abfd;
2789 asymbol *asym;
2791 elf_symbol_type *elfsym;
2793 elfsym = (elf_symbol_type *) asym;
2794 switch (elfsym->internal_elf_sym.st_shndx)
2796 case SHN_MIPS_ACOMMON:
2797 /* This section is used in a dynamically linked executable file.
2798 It is an allocated common section. The dynamic linker can
2799 either resolve these symbols to something in a shared
2800 library, or it can just leave them here. For our purposes,
2801 we can consider these symbols to be in a new section. */
2802 if (mips_elf_acom_section.name == NULL)
2804 /* Initialize the acommon section. */
2805 mips_elf_acom_section.name = ".acommon";
2806 mips_elf_acom_section.flags = SEC_ALLOC;
2807 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2808 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2809 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2810 mips_elf_acom_symbol.name = ".acommon";
2811 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2812 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2813 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2815 asym->section = &mips_elf_acom_section;
2816 break;
2818 case SHN_COMMON:
2819 /* Common symbols less than the GP size are automatically
2820 treated as SHN_MIPS_SCOMMON symbols. */
2821 if (asym->value > elf_gp_size (abfd))
2822 break;
2823 /* Fall through. */
2824 case SHN_MIPS_SCOMMON:
2825 if (mips_elf_scom_section.name == NULL)
2827 /* Initialize the small common section. */
2828 mips_elf_scom_section.name = ".scommon";
2829 mips_elf_scom_section.flags = SEC_IS_COMMON;
2830 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2831 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2832 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2833 mips_elf_scom_symbol.name = ".scommon";
2834 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2835 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2836 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2838 asym->section = &mips_elf_scom_section;
2839 asym->value = elfsym->internal_elf_sym.st_size;
2840 break;
2842 case SHN_MIPS_SUNDEFINED:
2843 asym->section = bfd_und_section_ptr;
2844 break;
2846 #if 0 /* for SGI_COMPAT */
2847 case SHN_MIPS_TEXT:
2848 asym->section = mips_elf_text_section_ptr;
2849 break;
2851 case SHN_MIPS_DATA:
2852 asym->section = mips_elf_data_section_ptr;
2853 break;
2854 #endif
2858 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
2859 segments. */
2861 static int
2862 mips_elf_additional_program_headers (abfd)
2863 bfd *abfd;
2865 asection *s;
2866 int ret;
2868 ret = 0;
2870 if (! SGI_COMPAT (abfd))
2871 return ret;
2873 s = bfd_get_section_by_name (abfd, ".reginfo");
2874 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2876 /* We need a PT_MIPS_REGINFO segment. */
2877 ++ret;
2880 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2881 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2883 /* We need a PT_MIPS_RTPROC segment. */
2884 ++ret;
2887 return ret;
2890 /* Modify the segment map for an Irix 5 executable. */
2892 static boolean
2893 mips_elf_modify_segment_map (abfd)
2894 bfd *abfd;
2896 asection *s;
2897 struct elf_segment_map *m, **pm;
2899 if (! SGI_COMPAT (abfd))
2900 return true;
2902 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2903 segment. */
2904 s = bfd_get_section_by_name (abfd, ".reginfo");
2905 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2907 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2908 if (m->p_type == PT_MIPS_REGINFO)
2909 break;
2910 if (m == NULL)
2912 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2913 if (m == NULL)
2914 return false;
2916 m->p_type = PT_MIPS_REGINFO;
2917 m->count = 1;
2918 m->sections[0] = s;
2920 /* We want to put it after the PHDR and INTERP segments. */
2921 pm = &elf_tdata (abfd)->segment_map;
2922 while (*pm != NULL
2923 && ((*pm)->p_type == PT_PHDR
2924 || (*pm)->p_type == PT_INTERP))
2925 pm = &(*pm)->next;
2927 m->next = *pm;
2928 *pm = m;
2932 /* If there are .dynamic and .mdebug sections, we make a room for
2933 the RTPROC header. FIXME: Rewrite without section names. */
2934 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2935 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2936 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2938 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2939 if (m->p_type == PT_MIPS_RTPROC)
2940 break;
2941 if (m == NULL)
2943 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2944 if (m == NULL)
2945 return false;
2947 m->p_type = PT_MIPS_RTPROC;
2949 s = bfd_get_section_by_name (abfd, ".rtproc");
2950 if (s == NULL)
2952 m->count = 0;
2953 m->p_flags = 0;
2954 m->p_flags_valid = 1;
2956 else
2958 m->count = 1;
2959 m->sections[0] = s;
2962 /* We want to put it after the DYNAMIC segment. */
2963 pm = &elf_tdata (abfd)->segment_map;
2964 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
2965 pm = &(*pm)->next;
2966 if (*pm != NULL)
2967 pm = &(*pm)->next;
2969 m->next = *pm;
2970 *pm = m;
2974 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2975 .dynsym, and .hash sections, and everything in between. */
2976 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
2977 if ((*pm)->p_type == PT_DYNAMIC)
2978 break;
2979 m = *pm;
2980 if (m != NULL
2981 && m->count == 1
2982 && strcmp (m->sections[0]->name, ".dynamic") == 0)
2984 static const char *sec_names[] =
2985 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2986 bfd_vma low, high;
2987 unsigned int i, c;
2988 struct elf_segment_map *n;
2990 low = 0xffffffff;
2991 high = 0;
2992 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
2994 s = bfd_get_section_by_name (abfd, sec_names[i]);
2995 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2997 bfd_size_type sz;
2999 if (low > s->vma)
3000 low = s->vma;
3001 sz = s->_cooked_size;
3002 if (sz == 0)
3003 sz = s->_raw_size;
3004 if (high < s->vma + sz)
3005 high = s->vma + sz;
3009 c = 0;
3010 for (s = abfd->sections; s != NULL; s = s->next)
3011 if ((s->flags & SEC_LOAD) != 0
3012 && s->vma >= low
3013 && ((s->vma
3014 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3015 <= high))
3016 ++c;
3018 n = ((struct elf_segment_map *)
3019 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3020 if (n == NULL)
3021 return false;
3022 *n = *m;
3023 n->count = c;
3025 i = 0;
3026 for (s = abfd->sections; s != NULL; s = s->next)
3028 if ((s->flags & SEC_LOAD) != 0
3029 && s->vma >= low
3030 && ((s->vma
3031 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3032 <= high))
3034 n->sections[i] = s;
3035 ++i;
3039 *pm = n;
3042 return true;
3045 /* The structure of the runtime procedure descriptor created by the
3046 loader for use by the static exception system. */
3048 typedef struct runtime_pdr {
3049 bfd_vma adr; /* memory address of start of procedure */
3050 long regmask; /* save register mask */
3051 long regoffset; /* save register offset */
3052 long fregmask; /* save floating point register mask */
3053 long fregoffset; /* save floating point register offset */
3054 long frameoffset; /* frame size */
3055 short framereg; /* frame pointer register */
3056 short pcreg; /* offset or reg of return pc */
3057 long irpss; /* index into the runtime string table */
3058 long reserved;
3059 struct exception_info *exception_info;/* pointer to exception array */
3060 } RPDR, *pRPDR;
3061 #define cbRPDR sizeof(RPDR)
3062 #define rpdNil ((pRPDR) 0)
3064 /* Swap RPDR (runtime procedure table entry) for output. */
3066 static void ecoff_swap_rpdr_out
3067 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3069 static void
3070 ecoff_swap_rpdr_out (abfd, in, ex)
3071 bfd *abfd;
3072 const RPDR *in;
3073 struct rpdr_ext *ex;
3075 /* ecoff_put_off was defined in ecoffswap.h. */
3076 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3077 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3078 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3079 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3080 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3081 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3083 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3084 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3086 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3087 #if 0 /* FIXME */
3088 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3089 #endif
3092 /* Read ECOFF debugging information from a .mdebug section into a
3093 ecoff_debug_info structure. */
3095 boolean
3096 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3097 bfd *abfd;
3098 asection *section;
3099 struct ecoff_debug_info *debug;
3101 HDRR *symhdr;
3102 const struct ecoff_debug_swap *swap;
3103 char *ext_hdr = NULL;
3105 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3106 memset (debug, 0, sizeof(*debug));
3108 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3109 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3110 goto error_return;
3112 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3113 swap->external_hdr_size)
3114 == false)
3115 goto error_return;
3117 symhdr = &debug->symbolic_header;
3118 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3120 /* The symbolic header contains absolute file offsets and sizes to
3121 read. */
3122 #define READ(ptr, offset, count, size, type) \
3123 if (symhdr->count == 0) \
3124 debug->ptr = NULL; \
3125 else \
3127 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3128 if (debug->ptr == NULL) \
3129 goto error_return; \
3130 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3131 || (bfd_read (debug->ptr, size, symhdr->count, \
3132 abfd) != size * symhdr->count)) \
3133 goto error_return; \
3136 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3137 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3138 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3139 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3140 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3141 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3142 union aux_ext *);
3143 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3144 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3145 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3146 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3147 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3148 #undef READ
3150 debug->fdr = NULL;
3151 debug->adjust = NULL;
3153 return true;
3155 error_return:
3156 if (ext_hdr != NULL)
3157 free (ext_hdr);
3158 if (debug->line != NULL)
3159 free (debug->line);
3160 if (debug->external_dnr != NULL)
3161 free (debug->external_dnr);
3162 if (debug->external_pdr != NULL)
3163 free (debug->external_pdr);
3164 if (debug->external_sym != NULL)
3165 free (debug->external_sym);
3166 if (debug->external_opt != NULL)
3167 free (debug->external_opt);
3168 if (debug->external_aux != NULL)
3169 free (debug->external_aux);
3170 if (debug->ss != NULL)
3171 free (debug->ss);
3172 if (debug->ssext != NULL)
3173 free (debug->ssext);
3174 if (debug->external_fdr != NULL)
3175 free (debug->external_fdr);
3176 if (debug->external_rfd != NULL)
3177 free (debug->external_rfd);
3178 if (debug->external_ext != NULL)
3179 free (debug->external_ext);
3180 return false;
3183 /* MIPS ELF local labels start with '$', not 'L'. */
3185 /*ARGSUSED*/
3186 static boolean
3187 mips_elf_is_local_label_name (abfd, name)
3188 bfd *abfd;
3189 const char *name;
3191 if (name[0] == '$')
3192 return true;
3194 /* On Irix 6, the labels go back to starting with '.', so we accept
3195 the generic ELF local label syntax as well. */
3196 return _bfd_elf_is_local_label_name (abfd, name);
3199 /* MIPS ELF uses a special find_nearest_line routine in order the
3200 handle the ECOFF debugging information. */
3202 struct mips_elf_find_line
3204 struct ecoff_debug_info d;
3205 struct ecoff_find_line i;
3208 boolean
3209 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3210 functionname_ptr, line_ptr)
3211 bfd *abfd;
3212 asection *section;
3213 asymbol **symbols;
3214 bfd_vma offset;
3215 const char **filename_ptr;
3216 const char **functionname_ptr;
3217 unsigned int *line_ptr;
3219 asection *msec;
3221 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3222 filename_ptr, functionname_ptr,
3223 line_ptr))
3224 return true;
3226 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3227 filename_ptr, functionname_ptr,
3228 line_ptr))
3229 return true;
3231 msec = bfd_get_section_by_name (abfd, ".mdebug");
3232 if (msec != NULL)
3234 flagword origflags;
3235 struct mips_elf_find_line *fi;
3236 const struct ecoff_debug_swap * const swap =
3237 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3239 /* If we are called during a link, mips_elf_final_link may have
3240 cleared the SEC_HAS_CONTENTS field. We force it back on here
3241 if appropriate (which it normally will be). */
3242 origflags = msec->flags;
3243 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3244 msec->flags |= SEC_HAS_CONTENTS;
3246 fi = elf_tdata (abfd)->find_line_info;
3247 if (fi == NULL)
3249 bfd_size_type external_fdr_size;
3250 char *fraw_src;
3251 char *fraw_end;
3252 struct fdr *fdr_ptr;
3254 fi = ((struct mips_elf_find_line *)
3255 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3256 if (fi == NULL)
3258 msec->flags = origflags;
3259 return false;
3262 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3264 msec->flags = origflags;
3265 return false;
3268 /* Swap in the FDR information. */
3269 fi->d.fdr = ((struct fdr *)
3270 bfd_alloc (abfd,
3271 (fi->d.symbolic_header.ifdMax *
3272 sizeof (struct fdr))));
3273 if (fi->d.fdr == NULL)
3275 msec->flags = origflags;
3276 return false;
3278 external_fdr_size = swap->external_fdr_size;
3279 fdr_ptr = fi->d.fdr;
3280 fraw_src = (char *) fi->d.external_fdr;
3281 fraw_end = (fraw_src
3282 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3283 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3284 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3286 elf_tdata (abfd)->find_line_info = fi;
3288 /* Note that we don't bother to ever free this information.
3289 find_nearest_line is either called all the time, as in
3290 objdump -l, so the information should be saved, or it is
3291 rarely called, as in ld error messages, so the memory
3292 wasted is unimportant. Still, it would probably be a
3293 good idea for free_cached_info to throw it away. */
3296 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3297 &fi->i, filename_ptr, functionname_ptr,
3298 line_ptr))
3300 msec->flags = origflags;
3301 return true;
3304 msec->flags = origflags;
3307 /* Fall back on the generic ELF find_nearest_line routine. */
3309 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3310 filename_ptr, functionname_ptr,
3311 line_ptr);
3314 /* The mips16 compiler uses a couple of special sections to handle
3315 floating point arguments.
3317 Section names that look like .mips16.fn.FNNAME contain stubs that
3318 copy floating point arguments from the fp regs to the gp regs and
3319 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3320 call should be redirected to the stub instead. If no 32 bit
3321 function calls FNNAME, the stub should be discarded. We need to
3322 consider any reference to the function, not just a call, because
3323 if the address of the function is taken we will need the stub,
3324 since the address might be passed to a 32 bit function.
3326 Section names that look like .mips16.call.FNNAME contain stubs
3327 that copy floating point arguments from the gp regs to the fp
3328 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3329 then any 16 bit function that calls FNNAME should be redirected
3330 to the stub instead. If FNNAME is not a 32 bit function, the
3331 stub should be discarded.
3333 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3334 which call FNNAME and then copy the return value from the fp regs
3335 to the gp regs. These stubs store the return value in $18 while
3336 calling FNNAME; any function which might call one of these stubs
3337 must arrange to save $18 around the call. (This case is not
3338 needed for 32 bit functions that call 16 bit functions, because
3339 16 bit functions always return floating point values in both
3340 $f0/$f1 and $2/$3.)
3342 Note that in all cases FNNAME might be defined statically.
3343 Therefore, FNNAME is not used literally. Instead, the relocation
3344 information will indicate which symbol the section is for.
3346 We record any stubs that we find in the symbol table. */
3348 #define FN_STUB ".mips16.fn."
3349 #define CALL_STUB ".mips16.call."
3350 #define CALL_FP_STUB ".mips16.call.fp."
3352 /* The MIPS ELF linker needs additional information for each symbol in
3353 the global hash table. */
3355 struct mips_elf_link_hash_entry
3357 struct elf_link_hash_entry root;
3359 /* External symbol information. */
3360 EXTR esym;
3362 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
3363 unsigned int mips_32_relocs;
3365 /* If there is a stub that 32 bit functions should use to call this
3366 16 bit function, this points to the section containing the stub. */
3367 asection *fn_stub;
3369 /* Whether we need the fn_stub; this is set if this symbol appears
3370 in any relocs other than a 16 bit call. */
3371 boolean need_fn_stub;
3373 /* If there is a stub that 16 bit functions should use to call this
3374 32 bit function, this points to the section containing the stub. */
3375 asection *call_stub;
3377 /* This is like the call_stub field, but it is used if the function
3378 being called returns a floating point value. */
3379 asection *call_fp_stub;
3382 /* MIPS ELF linker hash table. */
3384 struct mips_elf_link_hash_table
3386 struct elf_link_hash_table root;
3387 #if 0
3388 /* We no longer use this. */
3389 /* String section indices for the dynamic section symbols. */
3390 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3391 #endif
3392 /* The number of .rtproc entries. */
3393 bfd_size_type procedure_count;
3394 /* The size of the .compact_rel section (if SGI_COMPAT). */
3395 bfd_size_type compact_rel_size;
3396 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3397 entry is set to the address of __rld_obj_head as in Irix 5. */
3398 boolean use_rld_obj_head;
3399 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3400 bfd_vma rld_value;
3401 /* This is set if we see any mips16 stub sections. */
3402 boolean mips16_stubs_seen;
3405 /* Look up an entry in a MIPS ELF linker hash table. */
3407 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3408 ((struct mips_elf_link_hash_entry *) \
3409 elf_link_hash_lookup (&(table)->root, (string), (create), \
3410 (copy), (follow)))
3412 /* Traverse a MIPS ELF linker hash table. */
3414 #define mips_elf_link_hash_traverse(table, func, info) \
3415 (elf_link_hash_traverse \
3416 (&(table)->root, \
3417 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3418 (info)))
3420 /* Get the MIPS ELF linker hash table from a link_info structure. */
3422 #define mips_elf_hash_table(p) \
3423 ((struct mips_elf_link_hash_table *) ((p)->hash))
3425 static boolean mips_elf_output_extsym
3426 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3428 /* Create an entry in a MIPS ELF linker hash table. */
3430 static struct bfd_hash_entry *
3431 mips_elf_link_hash_newfunc (entry, table, string)
3432 struct bfd_hash_entry *entry;
3433 struct bfd_hash_table *table;
3434 const char *string;
3436 struct mips_elf_link_hash_entry *ret =
3437 (struct mips_elf_link_hash_entry *) entry;
3439 /* Allocate the structure if it has not already been allocated by a
3440 subclass. */
3441 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3442 ret = ((struct mips_elf_link_hash_entry *)
3443 bfd_hash_allocate (table,
3444 sizeof (struct mips_elf_link_hash_entry)));
3445 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3446 return (struct bfd_hash_entry *) ret;
3448 /* Call the allocation method of the superclass. */
3449 ret = ((struct mips_elf_link_hash_entry *)
3450 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3451 table, string));
3452 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3454 /* Set local fields. */
3455 memset (&ret->esym, 0, sizeof (EXTR));
3456 /* We use -2 as a marker to indicate that the information has
3457 not been set. -1 means there is no associated ifd. */
3458 ret->esym.ifd = -2;
3459 ret->mips_32_relocs = 0;
3460 ret->fn_stub = NULL;
3461 ret->need_fn_stub = false;
3462 ret->call_stub = NULL;
3463 ret->call_fp_stub = NULL;
3466 return (struct bfd_hash_entry *) ret;
3469 /* Create a MIPS ELF linker hash table. */
3471 static struct bfd_link_hash_table *
3472 mips_elf_link_hash_table_create (abfd)
3473 bfd *abfd;
3475 struct mips_elf_link_hash_table *ret;
3477 ret = ((struct mips_elf_link_hash_table *)
3478 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3479 if (ret == (struct mips_elf_link_hash_table *) NULL)
3480 return NULL;
3482 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3483 mips_elf_link_hash_newfunc))
3485 bfd_release (abfd, ret);
3486 return NULL;
3489 #if 0
3490 /* We no longer use this. */
3491 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3492 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3493 #endif
3494 ret->procedure_count = 0;
3495 ret->compact_rel_size = 0;
3496 ret->use_rld_obj_head = false;
3497 ret->rld_value = 0;
3498 ret->mips16_stubs_seen = false;
3500 return &ret->root.root;
3503 /* Hook called by the linker routine which adds symbols from an object
3504 file. We must handle the special MIPS section numbers here. */
3506 /*ARGSUSED*/
3507 static boolean
3508 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3509 bfd *abfd;
3510 struct bfd_link_info *info;
3511 const Elf_Internal_Sym *sym;
3512 const char **namep;
3513 flagword *flagsp;
3514 asection **secp;
3515 bfd_vma *valp;
3517 if (SGI_COMPAT (abfd)
3518 && (abfd->flags & DYNAMIC) != 0
3519 && strcmp (*namep, "_rld_new_interface") == 0)
3521 /* Skip Irix 5 rld entry name. */
3522 *namep = NULL;
3523 return true;
3526 switch (sym->st_shndx)
3528 case SHN_COMMON:
3529 /* Common symbols less than the GP size are automatically
3530 treated as SHN_MIPS_SCOMMON symbols. */
3531 if (sym->st_size > elf_gp_size (abfd))
3532 break;
3533 /* Fall through. */
3534 case SHN_MIPS_SCOMMON:
3535 *secp = bfd_make_section_old_way (abfd, ".scommon");
3536 (*secp)->flags |= SEC_IS_COMMON;
3537 *valp = sym->st_size;
3538 break;
3540 case SHN_MIPS_TEXT:
3541 /* This section is used in a shared object. */
3542 if (mips_elf_text_section_ptr == NULL)
3544 /* Initialize the section. */
3545 mips_elf_text_section.name = ".text";
3546 mips_elf_text_section.flags = SEC_NO_FLAGS;
3547 mips_elf_text_section.output_section = NULL;
3548 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3549 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3550 mips_elf_text_symbol.name = ".text";
3551 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3552 mips_elf_text_symbol.section = &mips_elf_text_section;
3553 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3554 mips_elf_text_section_ptr = &mips_elf_text_section;
3556 /* This code used to do *secp = bfd_und_section_ptr if
3557 info->shared. I don't know why, and that doesn't make sense,
3558 so I took it out. */
3559 *secp = mips_elf_text_section_ptr;
3560 break;
3562 case SHN_MIPS_ACOMMON:
3563 /* Fall through. XXX Can we treat this as allocated data? */
3564 case SHN_MIPS_DATA:
3565 /* This section is used in a shared object. */
3566 if (mips_elf_data_section_ptr == NULL)
3568 /* Initialize the section. */
3569 mips_elf_data_section.name = ".data";
3570 mips_elf_data_section.flags = SEC_NO_FLAGS;
3571 mips_elf_data_section.output_section = NULL;
3572 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3573 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3574 mips_elf_data_symbol.name = ".data";
3575 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3576 mips_elf_data_symbol.section = &mips_elf_data_section;
3577 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3578 mips_elf_data_section_ptr = &mips_elf_data_section;
3580 /* This code used to do *secp = bfd_und_section_ptr if
3581 info->shared. I don't know why, and that doesn't make sense,
3582 so I took it out. */
3583 *secp = mips_elf_data_section_ptr;
3584 break;
3586 case SHN_MIPS_SUNDEFINED:
3587 *secp = bfd_und_section_ptr;
3588 break;
3591 if (SGI_COMPAT (abfd)
3592 && ! info->shared
3593 && info->hash->creator == abfd->xvec
3594 && strcmp (*namep, "__rld_obj_head") == 0)
3596 struct elf_link_hash_entry *h;
3598 /* Mark __rld_obj_head as dynamic. */
3599 h = NULL;
3600 if (! (_bfd_generic_link_add_one_symbol
3601 (info, abfd, *namep, BSF_GLOBAL, *secp,
3602 (bfd_vma) *valp, (const char *) NULL, false,
3603 get_elf_backend_data (abfd)->collect,
3604 (struct bfd_link_hash_entry **) &h)))
3605 return false;
3606 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3607 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3608 h->type = STT_OBJECT;
3610 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3611 return false;
3613 mips_elf_hash_table (info)->use_rld_obj_head = true;
3616 /* If this is a mips16 text symbol, add 1 to the value to make it
3617 odd. This will cause something like .word SYM to come up with
3618 the right value when it is loaded into the PC. */
3619 if (sym->st_other == STO_MIPS16)
3620 ++*valp;
3622 return true;
3625 /* Structure used to pass information to mips_elf_output_extsym. */
3627 struct extsym_info
3629 bfd *abfd;
3630 struct bfd_link_info *info;
3631 struct ecoff_debug_info *debug;
3632 const struct ecoff_debug_swap *swap;
3633 boolean failed;
3636 /* This routine is used to write out ECOFF debugging external symbol
3637 information. It is called via mips_elf_link_hash_traverse. The
3638 ECOFF external symbol information must match the ELF external
3639 symbol information. Unfortunately, at this point we don't know
3640 whether a symbol is required by reloc information, so the two
3641 tables may wind up being different. We must sort out the external
3642 symbol information before we can set the final size of the .mdebug
3643 section, and we must set the size of the .mdebug section before we
3644 can relocate any sections, and we can't know which symbols are
3645 required by relocation until we relocate the sections.
3646 Fortunately, it is relatively unlikely that any symbol will be
3647 stripped but required by a reloc. In particular, it can not happen
3648 when generating a final executable. */
3650 static boolean
3651 mips_elf_output_extsym (h, data)
3652 struct mips_elf_link_hash_entry *h;
3653 PTR data;
3655 struct extsym_info *einfo = (struct extsym_info *) data;
3656 boolean strip;
3657 asection *sec, *output_section;
3659 if (h->root.indx == -2)
3660 strip = false;
3661 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3662 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3663 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3664 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3665 strip = true;
3666 else if (einfo->info->strip == strip_all
3667 || (einfo->info->strip == strip_some
3668 && bfd_hash_lookup (einfo->info->keep_hash,
3669 h->root.root.root.string,
3670 false, false) == NULL))
3671 strip = true;
3672 else
3673 strip = false;
3675 if (strip)
3676 return true;
3678 if (h->esym.ifd == -2)
3680 h->esym.jmptbl = 0;
3681 h->esym.cobol_main = 0;
3682 h->esym.weakext = 0;
3683 h->esym.reserved = 0;
3684 h->esym.ifd = ifdNil;
3685 h->esym.asym.value = 0;
3686 h->esym.asym.st = stGlobal;
3688 if (SGI_COMPAT (einfo->abfd)
3689 && (h->root.root.type == bfd_link_hash_undefined
3690 || h->root.root.type == bfd_link_hash_undefweak))
3692 const char *name;
3694 /* Use undefined class. Also, set class and type for some
3695 special symbols. */
3696 name = h->root.root.root.string;
3697 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3698 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3700 h->esym.asym.sc = scData;
3701 h->esym.asym.st = stLabel;
3702 h->esym.asym.value = 0;
3704 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3706 h->esym.asym.sc = scAbs;
3707 h->esym.asym.st = stLabel;
3708 h->esym.asym.value =
3709 mips_elf_hash_table (einfo->info)->procedure_count;
3711 else if (strcmp (name, "_gp_disp") == 0)
3713 h->esym.asym.sc = scAbs;
3714 h->esym.asym.st = stLabel;
3715 h->esym.asym.value = elf_gp (einfo->abfd);
3717 else
3718 h->esym.asym.sc = scUndefined;
3720 else if (h->root.root.type != bfd_link_hash_defined
3721 && h->root.root.type != bfd_link_hash_defweak)
3722 h->esym.asym.sc = scAbs;
3723 else
3725 const char *name;
3727 sec = h->root.root.u.def.section;
3728 output_section = sec->output_section;
3730 /* When making a shared library and symbol h is the one from
3731 the another shared library, OUTPUT_SECTION may be null. */
3732 if (output_section == NULL)
3733 h->esym.asym.sc = scUndefined;
3734 else
3736 name = bfd_section_name (output_section->owner, output_section);
3738 if (strcmp (name, ".text") == 0)
3739 h->esym.asym.sc = scText;
3740 else if (strcmp (name, ".data") == 0)
3741 h->esym.asym.sc = scData;
3742 else if (strcmp (name, ".sdata") == 0)
3743 h->esym.asym.sc = scSData;
3744 else if (strcmp (name, ".rodata") == 0
3745 || strcmp (name, ".rdata") == 0)
3746 h->esym.asym.sc = scRData;
3747 else if (strcmp (name, ".bss") == 0)
3748 h->esym.asym.sc = scBss;
3749 else if (strcmp (name, ".sbss") == 0)
3750 h->esym.asym.sc = scSBss;
3751 else if (strcmp (name, ".init") == 0)
3752 h->esym.asym.sc = scInit;
3753 else if (strcmp (name, ".fini") == 0)
3754 h->esym.asym.sc = scFini;
3755 else
3756 h->esym.asym.sc = scAbs;
3760 h->esym.asym.reserved = 0;
3761 h->esym.asym.index = indexNil;
3764 if (h->root.root.type == bfd_link_hash_common)
3765 h->esym.asym.value = h->root.root.u.c.size;
3766 else if (h->root.root.type == bfd_link_hash_defined
3767 || h->root.root.type == bfd_link_hash_defweak)
3769 if (h->esym.asym.sc == scCommon)
3770 h->esym.asym.sc = scBss;
3771 else if (h->esym.asym.sc == scSCommon)
3772 h->esym.asym.sc = scSBss;
3774 sec = h->root.root.u.def.section;
3775 output_section = sec->output_section;
3776 if (output_section != NULL)
3777 h->esym.asym.value = (h->root.root.u.def.value
3778 + sec->output_offset
3779 + output_section->vma);
3780 else
3781 h->esym.asym.value = 0;
3783 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3785 /* Set type and value for a symbol with a function stub. */
3786 h->esym.asym.st = stProc;
3787 sec = h->root.root.u.def.section;
3788 if (sec == NULL)
3789 h->esym.asym.value = 0;
3790 else
3792 output_section = sec->output_section;
3793 if (output_section != NULL)
3794 h->esym.asym.value = (h->root.plt.offset
3795 + sec->output_offset
3796 + output_section->vma);
3797 else
3798 h->esym.asym.value = 0;
3800 #if 0 /* FIXME? */
3801 h->esym.ifd = 0;
3802 #endif
3805 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3806 h->root.root.root.string,
3807 &h->esym))
3809 einfo->failed = true;
3810 return false;
3813 return true;
3816 /* Create a runtime procedure table from the .mdebug section. */
3818 static boolean
3819 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3820 PTR handle;
3821 bfd *abfd;
3822 struct bfd_link_info *info;
3823 asection *s;
3824 struct ecoff_debug_info *debug;
3826 const struct ecoff_debug_swap *swap;
3827 HDRR *hdr = &debug->symbolic_header;
3828 RPDR *rpdr, *rp;
3829 struct rpdr_ext *erp;
3830 PTR rtproc;
3831 struct pdr_ext *epdr;
3832 struct sym_ext *esym;
3833 char *ss, **sv;
3834 char *str;
3835 unsigned long size, count;
3836 unsigned long sindex;
3837 unsigned long i;
3838 PDR pdr;
3839 SYMR sym;
3840 const char *no_name_func = _("static procedure (no name)");
3842 epdr = NULL;
3843 rpdr = NULL;
3844 esym = NULL;
3845 ss = NULL;
3846 sv = NULL;
3848 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3850 sindex = strlen (no_name_func) + 1;
3851 count = hdr->ipdMax;
3852 if (count > 0)
3854 size = swap->external_pdr_size;
3856 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3857 if (epdr == NULL)
3858 goto error_return;
3860 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3861 goto error_return;
3863 size = sizeof (RPDR);
3864 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3865 if (rpdr == NULL)
3866 goto error_return;
3868 sv = (char **) bfd_malloc (sizeof (char *) * count);
3869 if (sv == NULL)
3870 goto error_return;
3872 count = hdr->isymMax;
3873 size = swap->external_sym_size;
3874 esym = (struct sym_ext *) bfd_malloc (size * count);
3875 if (esym == NULL)
3876 goto error_return;
3878 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3879 goto error_return;
3881 count = hdr->issMax;
3882 ss = (char *) bfd_malloc (count);
3883 if (ss == NULL)
3884 goto error_return;
3885 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3886 goto error_return;
3888 count = hdr->ipdMax;
3889 for (i = 0; i < count; i++, rp++)
3891 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3892 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3893 rp->adr = sym.value;
3894 rp->regmask = pdr.regmask;
3895 rp->regoffset = pdr.regoffset;
3896 rp->fregmask = pdr.fregmask;
3897 rp->fregoffset = pdr.fregoffset;
3898 rp->frameoffset = pdr.frameoffset;
3899 rp->framereg = pdr.framereg;
3900 rp->pcreg = pdr.pcreg;
3901 rp->irpss = sindex;
3902 sv[i] = ss + sym.iss;
3903 sindex += strlen (sv[i]) + 1;
3907 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3908 size = BFD_ALIGN (size, 16);
3909 rtproc = (PTR) bfd_alloc (abfd, size);
3910 if (rtproc == NULL)
3912 mips_elf_hash_table (info)->procedure_count = 0;
3913 goto error_return;
3916 mips_elf_hash_table (info)->procedure_count = count + 2;
3918 erp = (struct rpdr_ext *) rtproc;
3919 memset (erp, 0, sizeof (struct rpdr_ext));
3920 erp++;
3921 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3922 strcpy (str, no_name_func);
3923 str += strlen (no_name_func) + 1;
3924 for (i = 0; i < count; i++)
3926 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3927 strcpy (str, sv[i]);
3928 str += strlen (sv[i]) + 1;
3930 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3932 /* Set the size and contents of .rtproc section. */
3933 s->_raw_size = size;
3934 s->contents = (bfd_byte *) rtproc;
3936 /* Skip this section later on (I don't think this currently
3937 matters, but someday it might). */
3938 s->link_order_head = (struct bfd_link_order *) NULL;
3940 if (epdr != NULL)
3941 free (epdr);
3942 if (rpdr != NULL)
3943 free (rpdr);
3944 if (esym != NULL)
3945 free (esym);
3946 if (ss != NULL)
3947 free (ss);
3948 if (sv != NULL)
3949 free (sv);
3951 return true;
3953 error_return:
3954 if (epdr != NULL)
3955 free (epdr);
3956 if (rpdr != NULL)
3957 free (rpdr);
3958 if (esym != NULL)
3959 free (esym);
3960 if (ss != NULL)
3961 free (ss);
3962 if (sv != NULL)
3963 free (sv);
3964 return false;
3967 /* A comparison routine used to sort .gptab entries. */
3969 static int
3970 gptab_compare (p1, p2)
3971 const PTR p1;
3972 const PTR p2;
3974 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
3975 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
3977 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3980 /* We need to use a special link routine to handle the .reginfo and
3981 the .mdebug sections. We need to merge all instances of these
3982 sections together, not write them all out sequentially. */
3984 static boolean
3985 mips_elf_final_link (abfd, info)
3986 bfd *abfd;
3987 struct bfd_link_info *info;
3989 asection **secpp;
3990 asection *o;
3991 struct bfd_link_order *p;
3992 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
3993 asection *rtproc_sec;
3994 Elf32_RegInfo reginfo;
3995 struct ecoff_debug_info debug;
3996 const struct ecoff_debug_swap *swap
3997 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3998 HDRR *symhdr = &debug.symbolic_header;
3999 PTR mdebug_handle = NULL;
4001 /* Drop the .options section, since it has special semantics which I
4002 haven't bothered to figure out. */
4003 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4005 if (strcmp ((*secpp)->name, ".options") == 0)
4007 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4008 if (p->type == bfd_indirect_link_order)
4009 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4010 (*secpp)->link_order_head = NULL;
4011 *secpp = (*secpp)->next;
4012 --abfd->section_count;
4013 break;
4017 /* Get a value for the GP register. */
4018 if (elf_gp (abfd) == 0)
4020 struct bfd_link_hash_entry *h;
4022 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4023 if (h != (struct bfd_link_hash_entry *) NULL
4024 && h->type == bfd_link_hash_defined)
4025 elf_gp (abfd) = (h->u.def.value
4026 + h->u.def.section->output_section->vma
4027 + h->u.def.section->output_offset);
4028 else if (info->relocateable)
4030 bfd_vma lo;
4032 /* Make up a value. */
4033 lo = (bfd_vma) -1;
4034 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4036 if (o->vma < lo
4037 && (strcmp (o->name, ".sbss") == 0
4038 || strcmp (o->name, ".sdata") == 0
4039 || strcmp (o->name, ".lit4") == 0
4040 || strcmp (o->name, ".lit8") == 0))
4041 lo = o->vma;
4043 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4045 else
4047 /* If the relocate_section function needs to do a reloc
4048 involving the GP value, it should make a reloc_dangerous
4049 callback to warn that GP is not defined. */
4053 /* Go through the sections and collect the .reginfo and .mdebug
4054 information. */
4055 reginfo_sec = NULL;
4056 mdebug_sec = NULL;
4057 gptab_data_sec = NULL;
4058 gptab_bss_sec = NULL;
4059 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4061 if (strcmp (o->name, ".reginfo") == 0)
4063 memset (&reginfo, 0, sizeof reginfo);
4065 /* We have found the .reginfo section in the output file.
4066 Look through all the link_orders comprising it and merge
4067 the information together. */
4068 for (p = o->link_order_head;
4069 p != (struct bfd_link_order *) NULL;
4070 p = p->next)
4072 asection *input_section;
4073 bfd *input_bfd;
4074 Elf32_External_RegInfo ext;
4075 Elf32_RegInfo sub;
4077 if (p->type != bfd_indirect_link_order)
4079 if (p->type == bfd_fill_link_order)
4080 continue;
4081 abort ();
4084 input_section = p->u.indirect.section;
4085 input_bfd = input_section->owner;
4087 /* The linker emulation code has probably clobbered the
4088 size to be zero bytes. */
4089 if (input_section->_raw_size == 0)
4090 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4092 if (! bfd_get_section_contents (input_bfd, input_section,
4093 (PTR) &ext,
4094 (file_ptr) 0,
4095 sizeof ext))
4096 return false;
4098 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4100 reginfo.ri_gprmask |= sub.ri_gprmask;
4101 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4102 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4103 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4104 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4106 /* ri_gp_value is set by the function
4107 mips_elf32_section_processing when the section is
4108 finally written out. */
4110 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4111 elf_link_input_bfd ignores this section. */
4112 input_section->flags &=~ SEC_HAS_CONTENTS;
4115 /* Size has been set in mips_elf_always_size_sections */
4116 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4118 /* Skip this section later on (I don't think this currently
4119 matters, but someday it might). */
4120 o->link_order_head = (struct bfd_link_order *) NULL;
4122 reginfo_sec = o;
4125 if (strcmp (o->name, ".mdebug") == 0)
4127 struct extsym_info einfo;
4129 /* We have found the .mdebug section in the output file.
4130 Look through all the link_orders comprising it and merge
4131 the information together. */
4132 symhdr->magic = swap->sym_magic;
4133 /* FIXME: What should the version stamp be? */
4134 symhdr->vstamp = 0;
4135 symhdr->ilineMax = 0;
4136 symhdr->cbLine = 0;
4137 symhdr->idnMax = 0;
4138 symhdr->ipdMax = 0;
4139 symhdr->isymMax = 0;
4140 symhdr->ioptMax = 0;
4141 symhdr->iauxMax = 0;
4142 symhdr->issMax = 0;
4143 symhdr->issExtMax = 0;
4144 symhdr->ifdMax = 0;
4145 symhdr->crfd = 0;
4146 symhdr->iextMax = 0;
4148 /* We accumulate the debugging information itself in the
4149 debug_info structure. */
4150 debug.line = NULL;
4151 debug.external_dnr = NULL;
4152 debug.external_pdr = NULL;
4153 debug.external_sym = NULL;
4154 debug.external_opt = NULL;
4155 debug.external_aux = NULL;
4156 debug.ss = NULL;
4157 debug.ssext = debug.ssext_end = NULL;
4158 debug.external_fdr = NULL;
4159 debug.external_rfd = NULL;
4160 debug.external_ext = debug.external_ext_end = NULL;
4162 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4163 if (mdebug_handle == (PTR) NULL)
4164 return false;
4166 if (SGI_COMPAT (abfd))
4168 asection *s;
4169 EXTR esym;
4170 bfd_vma last;
4171 unsigned int i;
4172 static const char * const name[] =
4173 { ".text", ".init", ".fini", ".data",
4174 ".rodata", ".sdata", ".sbss", ".bss" };
4175 static const int sc[] = { scText, scInit, scFini, scData,
4176 scRData, scSData, scSBss, scBss };
4178 esym.jmptbl = 0;
4179 esym.cobol_main = 0;
4180 esym.weakext = 0;
4181 esym.reserved = 0;
4182 esym.ifd = ifdNil;
4183 esym.asym.iss = issNil;
4184 esym.asym.st = stLocal;
4185 esym.asym.reserved = 0;
4186 esym.asym.index = indexNil;
4187 last = 0;
4188 for (i = 0; i < 8; i++)
4190 esym.asym.sc = sc[i];
4191 s = bfd_get_section_by_name (abfd, name[i]);
4192 if (s != NULL)
4194 esym.asym.value = s->vma;
4195 last = s->vma + s->_raw_size;
4197 else
4198 esym.asym.value = last;
4200 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4201 name[i], &esym))
4202 return false;
4206 for (p = o->link_order_head;
4207 p != (struct bfd_link_order *) NULL;
4208 p = p->next)
4210 asection *input_section;
4211 bfd *input_bfd;
4212 const struct ecoff_debug_swap *input_swap;
4213 struct ecoff_debug_info input_debug;
4214 char *eraw_src;
4215 char *eraw_end;
4217 if (p->type != bfd_indirect_link_order)
4219 if (p->type == bfd_fill_link_order)
4220 continue;
4221 abort ();
4224 input_section = p->u.indirect.section;
4225 input_bfd = input_section->owner;
4227 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4228 || (get_elf_backend_data (input_bfd)
4229 ->elf_backend_ecoff_debug_swap) == NULL)
4231 /* I don't know what a non MIPS ELF bfd would be
4232 doing with a .mdebug section, but I don't really
4233 want to deal with it. */
4234 continue;
4237 input_swap = (get_elf_backend_data (input_bfd)
4238 ->elf_backend_ecoff_debug_swap);
4240 BFD_ASSERT (p->size == input_section->_raw_size);
4242 /* The ECOFF linking code expects that we have already
4243 read in the debugging information and set up an
4244 ecoff_debug_info structure, so we do that now. */
4245 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4246 &input_debug))
4247 return false;
4249 if (! (bfd_ecoff_debug_accumulate
4250 (mdebug_handle, abfd, &debug, swap, input_bfd,
4251 &input_debug, input_swap, info)))
4252 return false;
4254 /* Loop through the external symbols. For each one with
4255 interesting information, try to find the symbol in
4256 the linker global hash table and save the information
4257 for the output external symbols. */
4258 eraw_src = input_debug.external_ext;
4259 eraw_end = (eraw_src
4260 + (input_debug.symbolic_header.iextMax
4261 * input_swap->external_ext_size));
4262 for (;
4263 eraw_src < eraw_end;
4264 eraw_src += input_swap->external_ext_size)
4266 EXTR ext;
4267 const char *name;
4268 struct mips_elf_link_hash_entry *h;
4270 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4271 if (ext.asym.sc == scNil
4272 || ext.asym.sc == scUndefined
4273 || ext.asym.sc == scSUndefined)
4274 continue;
4276 name = input_debug.ssext + ext.asym.iss;
4277 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4278 name, false, false, true);
4279 if (h == NULL || h->esym.ifd != -2)
4280 continue;
4282 if (ext.ifd != -1)
4284 BFD_ASSERT (ext.ifd
4285 < input_debug.symbolic_header.ifdMax);
4286 ext.ifd = input_debug.ifdmap[ext.ifd];
4289 h->esym = ext;
4292 /* Free up the information we just read. */
4293 free (input_debug.line);
4294 free (input_debug.external_dnr);
4295 free (input_debug.external_pdr);
4296 free (input_debug.external_sym);
4297 free (input_debug.external_opt);
4298 free (input_debug.external_aux);
4299 free (input_debug.ss);
4300 free (input_debug.ssext);
4301 free (input_debug.external_fdr);
4302 free (input_debug.external_rfd);
4303 free (input_debug.external_ext);
4305 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4306 elf_link_input_bfd ignores this section. */
4307 input_section->flags &=~ SEC_HAS_CONTENTS;
4310 if (SGI_COMPAT (abfd) && info->shared)
4312 /* Create .rtproc section. */
4313 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4314 if (rtproc_sec == NULL)
4316 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4317 | SEC_LINKER_CREATED | SEC_READONLY);
4319 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4320 if (rtproc_sec == NULL
4321 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4322 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4323 return false;
4326 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4327 info, rtproc_sec, &debug))
4328 return false;
4331 /* Build the external symbol information. */
4332 einfo.abfd = abfd;
4333 einfo.info = info;
4334 einfo.debug = &debug;
4335 einfo.swap = swap;
4336 einfo.failed = false;
4337 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4338 mips_elf_output_extsym,
4339 (PTR) &einfo);
4340 if (einfo.failed)
4341 return false;
4343 /* Set the size of the .mdebug section. */
4344 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4346 /* Skip this section later on (I don't think this currently
4347 matters, but someday it might). */
4348 o->link_order_head = (struct bfd_link_order *) NULL;
4350 mdebug_sec = o;
4353 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4355 const char *subname;
4356 unsigned int c;
4357 Elf32_gptab *tab;
4358 Elf32_External_gptab *ext_tab;
4359 unsigned int i;
4361 /* The .gptab.sdata and .gptab.sbss sections hold
4362 information describing how the small data area would
4363 change depending upon the -G switch. These sections
4364 not used in executables files. */
4365 if (! info->relocateable)
4367 asection **secpp;
4369 for (p = o->link_order_head;
4370 p != (struct bfd_link_order *) NULL;
4371 p = p->next)
4373 asection *input_section;
4375 if (p->type != bfd_indirect_link_order)
4377 if (p->type == bfd_fill_link_order)
4378 continue;
4379 abort ();
4382 input_section = p->u.indirect.section;
4384 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4385 elf_link_input_bfd ignores this section. */
4386 input_section->flags &=~ SEC_HAS_CONTENTS;
4389 /* Skip this section later on (I don't think this
4390 currently matters, but someday it might). */
4391 o->link_order_head = (struct bfd_link_order *) NULL;
4393 /* Really remove the section. */
4394 for (secpp = &abfd->sections;
4395 *secpp != o;
4396 secpp = &(*secpp)->next)
4398 *secpp = (*secpp)->next;
4399 --abfd->section_count;
4401 continue;
4404 /* There is one gptab for initialized data, and one for
4405 uninitialized data. */
4406 if (strcmp (o->name, ".gptab.sdata") == 0)
4407 gptab_data_sec = o;
4408 else if (strcmp (o->name, ".gptab.sbss") == 0)
4409 gptab_bss_sec = o;
4410 else
4412 (*_bfd_error_handler)
4413 (_("%s: illegal section name `%s'"),
4414 bfd_get_filename (abfd), o->name);
4415 bfd_set_error (bfd_error_nonrepresentable_section);
4416 return false;
4419 /* The linker script always combines .gptab.data and
4420 .gptab.sdata into .gptab.sdata, and likewise for
4421 .gptab.bss and .gptab.sbss. It is possible that there is
4422 no .sdata or .sbss section in the output file, in which
4423 case we must change the name of the output section. */
4424 subname = o->name + sizeof ".gptab" - 1;
4425 if (bfd_get_section_by_name (abfd, subname) == NULL)
4427 if (o == gptab_data_sec)
4428 o->name = ".gptab.data";
4429 else
4430 o->name = ".gptab.bss";
4431 subname = o->name + sizeof ".gptab" - 1;
4432 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4435 /* Set up the first entry. */
4436 c = 1;
4437 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4438 if (tab == NULL)
4439 return false;
4440 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4441 tab[0].gt_header.gt_unused = 0;
4443 /* Combine the input sections. */
4444 for (p = o->link_order_head;
4445 p != (struct bfd_link_order *) NULL;
4446 p = p->next)
4448 asection *input_section;
4449 bfd *input_bfd;
4450 bfd_size_type size;
4451 unsigned long last;
4452 bfd_size_type gpentry;
4454 if (p->type != bfd_indirect_link_order)
4456 if (p->type == bfd_fill_link_order)
4457 continue;
4458 abort ();
4461 input_section = p->u.indirect.section;
4462 input_bfd = input_section->owner;
4464 /* Combine the gptab entries for this input section one
4465 by one. We know that the input gptab entries are
4466 sorted by ascending -G value. */
4467 size = bfd_section_size (input_bfd, input_section);
4468 last = 0;
4469 for (gpentry = sizeof (Elf32_External_gptab);
4470 gpentry < size;
4471 gpentry += sizeof (Elf32_External_gptab))
4473 Elf32_External_gptab ext_gptab;
4474 Elf32_gptab int_gptab;
4475 unsigned long val;
4476 unsigned long add;
4477 boolean exact;
4478 unsigned int look;
4480 if (! (bfd_get_section_contents
4481 (input_bfd, input_section, (PTR) &ext_gptab,
4482 gpentry, sizeof (Elf32_External_gptab))))
4484 free (tab);
4485 return false;
4488 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4489 &int_gptab);
4490 val = int_gptab.gt_entry.gt_g_value;
4491 add = int_gptab.gt_entry.gt_bytes - last;
4493 exact = false;
4494 for (look = 1; look < c; look++)
4496 if (tab[look].gt_entry.gt_g_value >= val)
4497 tab[look].gt_entry.gt_bytes += add;
4499 if (tab[look].gt_entry.gt_g_value == val)
4500 exact = true;
4503 if (! exact)
4505 Elf32_gptab *new_tab;
4506 unsigned int max;
4508 /* We need a new table entry. */
4509 new_tab = ((Elf32_gptab *)
4510 bfd_realloc ((PTR) tab,
4511 (c + 1) * sizeof (Elf32_gptab)));
4512 if (new_tab == NULL)
4514 free (tab);
4515 return false;
4517 tab = new_tab;
4518 tab[c].gt_entry.gt_g_value = val;
4519 tab[c].gt_entry.gt_bytes = add;
4521 /* Merge in the size for the next smallest -G
4522 value, since that will be implied by this new
4523 value. */
4524 max = 0;
4525 for (look = 1; look < c; look++)
4527 if (tab[look].gt_entry.gt_g_value < val
4528 && (max == 0
4529 || (tab[look].gt_entry.gt_g_value
4530 > tab[max].gt_entry.gt_g_value)))
4531 max = look;
4533 if (max != 0)
4534 tab[c].gt_entry.gt_bytes +=
4535 tab[max].gt_entry.gt_bytes;
4537 ++c;
4540 last = int_gptab.gt_entry.gt_bytes;
4543 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4544 elf_link_input_bfd ignores this section. */
4545 input_section->flags &=~ SEC_HAS_CONTENTS;
4548 /* The table must be sorted by -G value. */
4549 if (c > 2)
4550 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4552 /* Swap out the table. */
4553 ext_tab = ((Elf32_External_gptab *)
4554 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4555 if (ext_tab == NULL)
4557 free (tab);
4558 return false;
4561 for (i = 0; i < c; i++)
4562 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4563 free (tab);
4565 o->_raw_size = c * sizeof (Elf32_External_gptab);
4566 o->contents = (bfd_byte *) ext_tab;
4568 /* Skip this section later on (I don't think this currently
4569 matters, but someday it might). */
4570 o->link_order_head = (struct bfd_link_order *) NULL;
4574 /* Invoke the regular ELF backend linker to do all the work. */
4575 if (! bfd_elf32_bfd_final_link (abfd, info))
4576 return false;
4578 /* Now write out the computed sections. */
4580 if (reginfo_sec != (asection *) NULL)
4582 Elf32_External_RegInfo ext;
4584 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4585 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4586 (file_ptr) 0, sizeof ext))
4587 return false;
4590 if (mdebug_sec != (asection *) NULL)
4592 BFD_ASSERT (abfd->output_has_begun);
4593 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4594 swap, info,
4595 mdebug_sec->filepos))
4596 return false;
4598 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4601 if (gptab_data_sec != (asection *) NULL)
4603 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4604 gptab_data_sec->contents,
4605 (file_ptr) 0,
4606 gptab_data_sec->_raw_size))
4607 return false;
4610 if (gptab_bss_sec != (asection *) NULL)
4612 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4613 gptab_bss_sec->contents,
4614 (file_ptr) 0,
4615 gptab_bss_sec->_raw_size))
4616 return false;
4619 if (SGI_COMPAT (abfd))
4621 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4622 if (rtproc_sec != NULL)
4624 if (! bfd_set_section_contents (abfd, rtproc_sec,
4625 rtproc_sec->contents,
4626 (file_ptr) 0,
4627 rtproc_sec->_raw_size))
4628 return false;
4632 return true;
4635 /* Handle a MIPS ELF HI16 reloc. */
4637 static void
4638 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4639 bfd *input_bfd;
4640 Elf_Internal_Rela *relhi;
4641 Elf_Internal_Rela *rello;
4642 bfd_byte *contents;
4643 bfd_vma addend;
4645 bfd_vma insn;
4646 bfd_vma addlo;
4648 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4650 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4651 addlo &= 0xffff;
4653 addend += ((insn & 0xffff) << 16) + addlo;
4655 if ((addlo & 0x8000) != 0)
4656 addend -= 0x10000;
4657 if ((addend & 0x8000) != 0)
4658 addend += 0x10000;
4660 bfd_put_32 (input_bfd,
4661 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4662 contents + relhi->r_offset);
4665 /* Handle a MIPS ELF local GOT16 reloc. */
4667 static boolean
4668 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4669 contents, addend)
4670 bfd *output_bfd;
4671 bfd *input_bfd;
4672 asection *sgot;
4673 Elf_Internal_Rela *relhi;
4674 Elf_Internal_Rela *rello;
4675 bfd_byte *contents;
4676 bfd_vma addend;
4678 unsigned int assigned_gotno;
4679 unsigned int i;
4680 bfd_vma insn;
4681 bfd_vma addlo;
4682 bfd_vma address;
4683 bfd_vma hipage;
4684 bfd_byte *got_contents;
4685 struct mips_got_info *g;
4687 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4689 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4690 addlo &= 0xffff;
4692 addend += ((insn & 0xffff) << 16) + addlo;
4694 if ((addlo & 0x8000) != 0)
4695 addend -= 0x10000;
4696 if ((addend & 0x8000) != 0)
4697 addend += 0x10000;
4699 /* Get a got entry representing requested hipage. */
4700 BFD_ASSERT (elf_section_data (sgot) != NULL);
4701 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4702 BFD_ASSERT (g != NULL);
4704 assigned_gotno = g->assigned_gotno;
4705 got_contents = sgot->contents;
4706 hipage = addend & 0xffff0000;
4708 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4710 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4711 if (hipage == (address & 0xffff0000))
4712 break;
4715 if (i == assigned_gotno)
4717 if (assigned_gotno >= g->local_gotno)
4719 (*_bfd_error_handler)
4720 (_("more got entries are needed for hipage relocations"));
4721 bfd_set_error (bfd_error_bad_value);
4722 return false;
4725 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4726 ++g->assigned_gotno;
4729 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4730 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4731 contents + relhi->r_offset);
4733 return true;
4736 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4738 static void
4739 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4740 bfd *input_bfd;
4741 Elf_Internal_Rela *rel;
4742 bfd_byte *contents;
4743 bfd_vma offset;
4745 bfd_vma insn;
4747 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4748 bfd_put_32 (input_bfd,
4749 (insn & 0xffff0000) | (offset & 0xffff),
4750 contents + rel->r_offset);
4753 /* Relocate a MIPS ELF section. */
4755 static boolean
4756 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4757 contents, relocs, local_syms, local_sections)
4758 bfd *output_bfd;
4759 struct bfd_link_info *info;
4760 bfd *input_bfd;
4761 asection *input_section;
4762 bfd_byte *contents;
4763 Elf_Internal_Rela *relocs;
4764 Elf_Internal_Sym *local_syms;
4765 asection **local_sections;
4767 Elf_Internal_Shdr *symtab_hdr;
4768 size_t locsymcount;
4769 size_t extsymoff;
4770 asection *sgot, *sreloc, *scpt;
4771 bfd *dynobj;
4772 bfd_vma gp;
4773 Elf_Internal_Rela *rel;
4774 Elf_Internal_Rela *relend;
4775 struct mips_got_info *g;
4777 dynobj = elf_hash_table (info)->dynobj;
4778 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4780 sgot = NULL;
4781 sreloc = NULL;
4782 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4783 scpt = NULL;
4784 else
4785 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4786 g = NULL;
4788 if (elf_bad_symtab (input_bfd))
4790 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4791 extsymoff = 0;
4793 else
4795 locsymcount = symtab_hdr->sh_info;
4796 extsymoff = symtab_hdr->sh_info;
4799 gp = _bfd_get_gp_value (output_bfd);
4801 rel = relocs;
4802 relend = relocs + input_section->reloc_count;
4803 for (; rel < relend; rel++)
4805 int r_type;
4806 reloc_howto_type *howto;
4807 unsigned long r_symndx;
4808 bfd_vma addend;
4809 struct elf_link_hash_entry *h;
4810 asection *sec;
4811 Elf_Internal_Sym *sym;
4812 struct mips_elf_link_hash_entry *mh;
4813 int other;
4814 bfd_reloc_status_type r;
4816 r_type = ELF32_R_TYPE (rel->r_info);
4817 if (r_type == R_MIPS_GNU_VTINHERIT
4818 || r_type == R_MIPS_GNU_VTENTRY)
4819 continue;
4820 if ((r_type < 0 || r_type >= (int) R_MIPS_max)
4821 && r_type != R_MIPS16_26
4822 && r_type != R_MIPS16_GPREL)
4824 bfd_set_error (bfd_error_bad_value);
4825 return false;
4827 if (r_type == R_MIPS16_26)
4828 howto = &elf_mips16_jump_howto;
4829 else if (r_type == R_MIPS16_GPREL)
4830 howto = &elf_mips16_gprel_howto;
4831 else
4832 howto = elf_mips_howto_table + r_type;
4834 if (dynobj != NULL
4835 && (r_type == R_MIPS_CALL16
4836 || r_type == R_MIPS_GOT16
4837 || r_type == R_MIPS_CALL_HI16
4838 || r_type == R_MIPS_CALL_LO16
4839 || r_type == R_MIPS_GOT_HI16
4840 || r_type == R_MIPS_GOT_LO16))
4842 /* We need the .got section. */
4843 if (sgot == NULL)
4845 sgot = bfd_get_section_by_name (dynobj, ".got");
4846 BFD_ASSERT (sgot != NULL);
4847 BFD_ASSERT (elf_section_data (sgot) != NULL);
4848 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4849 BFD_ASSERT (g != NULL);
4853 r_symndx = ELF32_R_SYM (rel->r_info);
4855 /* Mix in the change in GP address for a GP relative reloc. */
4856 if (r_type != R_MIPS_GPREL16
4857 && r_type != R_MIPS_LITERAL
4858 && r_type != R_MIPS_GPREL32
4859 && r_type != R_MIPS16_GPREL)
4860 addend = 0;
4861 else
4863 if (gp == 0)
4865 if (! ((*info->callbacks->reloc_dangerous)
4866 (info,
4867 _("GP relative relocation when GP not defined"),
4868 input_bfd, input_section,
4869 rel->r_offset)))
4870 return false;
4871 /* Only give the error once per link. */
4872 gp = 4;
4873 _bfd_set_gp_value (output_bfd, gp);
4876 if (r_symndx < extsymoff
4877 || (elf_bad_symtab (input_bfd)
4878 && local_sections[r_symndx] != NULL))
4880 /* This is a relocation against a section. The current
4881 addend in the instruction is the difference between
4882 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4883 must change this to be the difference between the
4884 final definition (which will end up in RELOCATION)
4885 and the GP value of OUTPUT_BFD (which is in GP). */
4886 addend = elf_gp (input_bfd) - gp;
4888 else if (! info->relocateable)
4890 /* We are doing a final link. The current addend in the
4891 instruction is simply the desired offset into the
4892 symbol (normally zero). We want the instruction to
4893 hold the difference between the final definition of
4894 the symbol (which will end up in RELOCATION) and the
4895 GP value of OUTPUT_BFD (which is in GP). */
4896 addend = - gp;
4898 else
4900 /* We are generating relocateable output, and we aren't
4901 going to define this symbol, so we just leave the
4902 instruction alone. */
4903 addend = 0;
4907 h = NULL;
4908 sym = NULL;
4909 sec = NULL;
4910 if (info->relocateable)
4912 /* This is a relocateable link. We don't have to change
4913 anything, unless the reloc is against a section symbol,
4914 in which case we have to adjust according to where the
4915 section symbol winds up in the output section. */
4916 if (r_symndx >= locsymcount
4917 || (elf_bad_symtab (input_bfd)
4918 && local_sections[r_symndx] == NULL))
4919 r = bfd_reloc_ok;
4920 else
4922 sym = local_syms + r_symndx;
4923 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4924 r = bfd_reloc_ok;
4925 else
4927 sec = local_sections[r_symndx];
4929 /* It would be logical to add sym->st_value here,
4930 but Irix 5 sometimes generates a garbage symbol
4931 value. */
4932 addend += sec->output_offset;
4934 /* If this is HI16 or GOT16 with an associated LO16,
4935 adjust the addend accordingly. Otherwise, just
4936 relocate. */
4937 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4938 r = _bfd_relocate_contents (howto, input_bfd,
4939 addend,
4940 contents + rel->r_offset + 4);
4941 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
4942 r = _bfd_relocate_contents (howto, input_bfd,
4943 addend,
4944 contents + rel->r_offset);
4945 else
4947 Elf_Internal_Rela *lorel;
4949 /* As a GNU extension, permit an arbitrary
4950 number of R_MIPS_HI16 relocs before the
4951 R_MIPS_LO16 reloc. This permits gcc to emit
4952 the HI and LO relocs itself. */
4953 if (r_type == R_MIPS_GOT16)
4954 lorel = rel + 1;
4955 else
4957 for (lorel = rel + 1;
4958 (lorel < relend
4959 && (ELF32_R_TYPE (lorel->r_info)
4960 == R_MIPS_HI16));
4961 lorel++)
4964 if (lorel < relend
4965 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4967 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4968 contents, addend);
4969 r = bfd_reloc_ok;
4971 else
4972 r = _bfd_relocate_contents (howto, input_bfd,
4973 addend,
4974 contents + rel->r_offset);
4979 else
4981 bfd_vma relocation;
4982 boolean local;
4983 boolean undefined_error;
4985 /* This is a final link. */
4986 undefined_error = false;
4987 sym = NULL;
4988 if (r_symndx < extsymoff
4989 || (elf_bad_symtab (input_bfd)
4990 && local_sections[r_symndx] != NULL))
4992 local = true;
4993 sym = local_syms + r_symndx;
4994 sec = local_sections[r_symndx];
4995 relocation = (sec->output_section->vma
4996 + sec->output_offset);
4998 /* It would be logical to always add sym->st_value here,
4999 but Irix 5 sometimes generates a garbage symbol
5000 value. */
5001 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5002 relocation += sym->st_value;
5004 /* mips16 text labels should be treated as odd. */
5005 if (sym->st_other == STO_MIPS16)
5006 ++relocation;
5008 else
5010 long indx;
5012 local = false;
5013 indx = r_symndx - extsymoff;
5014 h = elf_sym_hashes (input_bfd)[indx];
5015 while (h->root.type == bfd_link_hash_indirect
5016 || h->root.type == bfd_link_hash_warning)
5017 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5018 if (strcmp (h->root.root.string, "_gp_disp") == 0)
5020 if (gp == 0)
5022 if (! ((*info->callbacks->reloc_dangerous)
5023 (info,
5024 _("_gp_disp used when GP not defined"),
5025 input_bfd, input_section,
5026 rel->r_offset)))
5027 return false;
5028 /* Only give the error once per link. */
5029 gp = 4;
5030 _bfd_set_gp_value (output_bfd, gp);
5031 relocation = 0;
5033 else
5035 sec = input_section;
5036 if (sec->output_section != NULL)
5037 relocation = (gp
5038 - (rel->r_offset
5039 + sec->output_section->vma
5040 + sec->output_offset));
5041 else
5042 relocation = gp - rel->r_offset;
5043 if (r_type == R_MIPS_LO16)
5044 relocation += 4;
5047 else if (h->root.type == bfd_link_hash_defined
5048 || h->root.type == bfd_link_hash_defweak)
5050 sec = h->root.u.def.section;
5051 if (sec->output_section == NULL)
5052 relocation = 0;
5053 else
5054 relocation = (h->root.u.def.value
5055 + sec->output_section->vma
5056 + sec->output_offset);
5058 else if (h->root.type == bfd_link_hash_undefweak)
5059 relocation = 0;
5060 else if (info->shared && !info->symbolic && !info->no_undefined)
5061 relocation = 0;
5062 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
5064 /* If this is a dynamic link, we should have created
5065 a _DYNAMIC_LINK symbol in
5066 mips_elf_create_dynamic_sections. Otherwise, we
5067 should define the symbol with a value of 0.
5068 FIXME: It should probably get into the symbol
5069 table somehow as well. */
5070 BFD_ASSERT (! info->shared);
5071 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
5072 ".dynamic") == NULL);
5073 relocation = 0;
5075 else
5077 if (! ((*info->callbacks->undefined_symbol)
5078 (info, h->root.root.string, input_bfd,
5079 input_section, rel->r_offset)))
5080 return false;
5081 undefined_error = true;
5082 relocation = 0;
5086 mh = (struct mips_elf_link_hash_entry *) h;
5087 if (h != NULL)
5088 other = h->other;
5089 else if (sym != NULL)
5090 other = sym->st_other;
5091 else
5092 other = 0;
5094 /* If this function has an fn_stub, then it is a mips16
5095 function which needs a stub if it is called by a 32 bit
5096 function. If this reloc is anything other than a 16 bit
5097 call, redirect the reloc to the stub. We don't redirect
5098 relocs from other stub functions. */
5099 if (r_type != R_MIPS16_26
5100 && ((mh != NULL
5101 && mh->fn_stub != NULL)
5102 || (mh == NULL
5103 && elf_tdata (input_bfd)->local_stubs != NULL
5104 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5105 && strncmp (bfd_get_section_name (input_bfd, input_section),
5106 FN_STUB, sizeof FN_STUB - 1) != 0
5107 && strncmp (bfd_get_section_name (input_bfd, input_section),
5108 CALL_STUB, sizeof CALL_STUB - 1) != 0
5109 && strncmp (bfd_get_section_name (input_bfd, input_section),
5110 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
5112 if (mh != NULL)
5114 BFD_ASSERT (mh->need_fn_stub);
5115 relocation = (mh->fn_stub->output_section->vma
5116 + mh->fn_stub->output_offset);
5118 else
5120 asection *fn_stub;
5122 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
5123 relocation = (fn_stub->output_section->vma
5124 + fn_stub->output_offset);
5127 /* RELOCATION now points to 32 bit code. */
5128 other = 0;
5131 /* If this function has a call_stub, then it is called by a
5132 mips16 function; the call needs to go through a stub if
5133 this function is a 32 bit function. If this reloc is a
5134 16 bit call, and the symbol is not a 16 bit function,
5135 then redirect the reloc to the stub. Note that we don't
5136 need to worry about calling the function through a
5137 function pointer; such calls are handled by routing
5138 through a special mips16 routine. We don't have to check
5139 whether this call is from a stub; it can't be, because a
5140 stub contains 32 bit code, and hence can not have a 16
5141 bit reloc. */
5142 if (r_type == R_MIPS16_26
5143 && mh != NULL
5144 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
5145 && other != STO_MIPS16)
5147 asection *stub;
5149 /* If both call_stub and call_fp_stub are defined, we
5150 can figure out which one to use by seeing which one
5151 appears in the input file. */
5152 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
5154 asection *o;
5156 stub = NULL;
5157 for (o = input_bfd->sections; o != NULL; o = o->next)
5159 if (strncmp (bfd_get_section_name (input_bfd, o),
5160 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5162 stub = mh->call_fp_stub;
5163 break;
5166 if (stub == NULL)
5167 stub = mh->call_stub;
5169 else if (mh->call_stub != NULL)
5170 stub = mh->call_stub;
5171 else
5172 stub = mh->call_fp_stub;
5174 BFD_ASSERT (stub->_raw_size > 0);
5175 relocation = stub->output_section->vma + stub->output_offset;
5178 if (r_type == R_MIPS_HI16)
5180 Elf_Internal_Rela *lorel;
5182 /* As a GNU extension, permit an arbitrary number of
5183 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
5184 This permits gcc to emit the HI and LO relocs itself. */
5185 for (lorel = rel + 1;
5186 (lorel < relend
5187 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
5188 lorel++)
5190 if (lorel < relend
5191 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5193 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5194 contents, relocation + addend);
5195 r = bfd_reloc_ok;
5197 else
5198 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5199 contents, rel->r_offset,
5200 relocation, addend);
5202 else if (r_type == R_MIPS_GOT16 && local)
5204 /* GOT16 must also have an associated LO16 in the local
5205 case. In this case, the addend is extracted and the
5206 section in which the referenced object is determined.
5207 Then the final address of the object is computed and
5208 the GOT entry for the hipage (an aligned 64kb chunk)
5209 is added to .got section if needed. The offset field
5210 of the GOT16-relocated instruction is replaced by the
5211 index of this GOT entry for the hipage. */
5212 if ((rel + 1) < relend
5213 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
5215 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
5216 sgot, rel, rel + 1,
5217 contents,
5218 relocation + addend))
5219 return false;
5220 r = bfd_reloc_ok;
5222 else
5223 r = bfd_reloc_outofrange;
5225 else if (r_type == R_MIPS_CALL16
5226 || r_type == R_MIPS_GOT16
5227 || r_type == R_MIPS_CALL_LO16
5228 || r_type == R_MIPS_GOT_LO16)
5230 bfd_vma offset;
5232 /* This symbol must be registered as a global symbol
5233 having the corresponding got entry. */
5234 BFD_ASSERT (h->got.offset != (bfd_vma) -1);
5236 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5237 BFD_ASSERT (g->local_gotno <= offset
5238 && offset < sgot->_raw_size);
5239 bfd_put_32 (output_bfd, relocation + addend,
5240 sgot->contents + offset);
5241 offset = (sgot->output_section->vma + sgot->output_offset
5242 + offset - gp);
5243 mips_elf_relocate_global_got (input_bfd, rel, contents,
5244 offset);
5245 r = bfd_reloc_ok;
5247 else if (r_type == R_MIPS_CALL_HI16
5248 || r_type == R_MIPS_GOT_HI16)
5250 bfd_vma offset;
5252 /* This must be a global symbol with a got entry. The
5253 next reloc must be the corresponding LO16 reloc. */
5254 BFD_ASSERT (h != NULL && h->got.offset != (bfd_vma) -1);
5255 BFD_ASSERT ((rel + 1) < relend);
5256 BFD_ASSERT ((int) ELF32_R_TYPE ((rel + 1)->r_info)
5257 == (r_type == R_MIPS_CALL_HI16
5258 ? (int) R_MIPS_CALL_LO16
5259 : (int) R_MIPS_GOT_LO16));
5261 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5262 BFD_ASSERT (g->local_gotno <= offset
5263 && offset < sgot->_raw_size);
5264 bfd_put_32 (output_bfd, relocation + addend,
5265 sgot->contents + offset);
5266 offset = (sgot->output_section->vma + sgot->output_offset
5267 + offset - gp);
5268 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5269 offset);
5270 r = bfd_reloc_ok;
5272 else if (r_type == R_MIPS_REL32
5273 || r_type == R_MIPS_32)
5275 Elf_Internal_Rel outrel;
5276 Elf32_crinfo cptrel;
5277 bfd_byte *cr;
5279 if ((info->shared
5280 || (elf_hash_table (info)->dynamic_sections_created
5281 && h != NULL
5282 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5283 == 0)))
5284 && (input_section->flags & SEC_ALLOC) != 0)
5286 boolean skip;
5288 /* When generating a shared object, these
5289 relocations are copied into the output file to be
5290 resolved at run time. */
5291 if (sreloc == NULL)
5293 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5294 BFD_ASSERT (sreloc != NULL);
5297 skip = false;
5299 if (elf_section_data (input_section)->stab_info == NULL)
5300 outrel.r_offset = rel->r_offset;
5301 else
5303 bfd_vma off;
5305 off = (_bfd_stab_section_offset
5306 (output_bfd, &elf_hash_table (info)->stab_info,
5307 input_section,
5308 &elf_section_data (input_section)->stab_info,
5309 rel->r_offset));
5310 if (off == (bfd_vma) -1)
5311 skip = true;
5312 outrel.r_offset = off;
5315 outrel.r_offset += (input_section->output_section->vma
5316 + input_section->output_offset);
5318 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5320 if (skip)
5321 memset (&outrel, 0, sizeof outrel);
5322 else if (h != NULL
5323 && (! info->symbolic
5324 || (h->elf_link_hash_flags
5325 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5327 BFD_ASSERT (h->dynindx != -1);
5328 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5329 sec = input_section;
5331 else
5333 long indx;
5335 if (h == NULL)
5336 sec = local_sections[r_symndx];
5337 else
5339 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5340 || (h->root.type
5341 == bfd_link_hash_defweak));
5342 sec = h->root.u.def.section;
5344 if (sec != NULL && bfd_is_abs_section (sec))
5345 indx = 0;
5346 else if (sec == NULL || sec->owner == NULL)
5348 bfd_set_error (bfd_error_bad_value);
5349 return false;
5351 else
5353 asection *osec;
5355 osec = sec->output_section;
5356 indx = elf_section_data (osec)->dynindx;
5357 if (indx == 0)
5358 abort ();
5361 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5362 addend += relocation;
5365 if (! skip)
5366 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5368 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5369 (((Elf32_External_Rel *)
5370 sreloc->contents)
5371 + sreloc->reloc_count));
5372 ++sreloc->reloc_count;
5374 if (! skip && SGI_COMPAT (output_bfd))
5376 if (scpt == NULL)
5377 continue;
5379 /* Make an entry of compact relocation info. */
5380 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5381 cptrel.vaddr = (rel->r_offset
5382 + input_section->output_section->vma
5383 + input_section->output_offset);
5384 if (r_type == R_MIPS_REL32)
5385 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5386 else
5387 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5388 mips_elf_set_cr_dist2to (cptrel, 0);
5389 cptrel.konst = addend;
5391 cr = (scpt->contents
5392 + sizeof (Elf32_External_compact_rel));
5393 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5394 ((Elf32_External_crinfo *) cr
5395 + scpt->reloc_count));
5396 ++scpt->reloc_count;
5399 /* This reloc will be computed at runtime, so
5400 there's no need to do anything now. */
5401 continue;
5403 else
5404 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5405 contents, rel->r_offset,
5406 relocation, addend);
5408 else if (r_type == R_MIPS_64)
5410 bfd_size_type addr;
5411 unsigned long val;
5413 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5414 addr = rel->r_offset;
5415 if (bfd_big_endian (input_bfd))
5416 addr += 4;
5417 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5418 contents, addr, relocation,
5419 addend);
5420 val = bfd_get_32 (input_bfd, contents + addr);
5421 if ((val & 0x80000000) != 0)
5422 val = 0xffffffff;
5423 else
5424 val = 0;
5425 addr = rel->r_offset;
5426 if (bfd_little_endian (input_bfd))
5427 addr += 4;
5428 bfd_put_32 (input_bfd, val, contents + addr);
5430 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5432 unsigned long insn;
5434 /* This is a jump to a mips16 routine from a mips32
5435 routine. We need to change jal into jalx. */
5436 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5437 if (((insn >> 26) & 0x3f) != 0x3
5438 && ((insn >> 26) & 0x3f) != 0x1d)
5440 (*_bfd_error_handler)
5441 (_("%s: %s+0x%lx: jump to mips16 routine which is not jal"),
5442 bfd_get_filename (input_bfd),
5443 input_section->name,
5444 (unsigned long) rel->r_offset);
5445 bfd_set_error (bfd_error_bad_value);
5446 return false;
5448 insn = (insn & 0x3ffffff) | (0x1d << 26);
5449 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5450 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5451 contents, rel->r_offset,
5452 relocation, addend);
5454 else if (r_type == R_MIPS16_26)
5456 /* It's easiest to do the normal relocation, and then
5457 dig out the instruction and swap the first word the
5458 way the mips16 expects it. If this is little endian,
5459 though, we need to swap the two words first, and then
5460 swap them back again later, so that the address looks
5461 right. */
5463 if (bfd_little_endian (input_bfd))
5465 unsigned long insn;
5467 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5468 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5469 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5472 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5473 contents, rel->r_offset,
5474 relocation, addend);
5475 if (r == bfd_reloc_ok)
5477 unsigned long insn;
5479 if (bfd_little_endian (input_bfd))
5481 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5482 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5483 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5486 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5487 insn = ((insn & 0xfc00)
5488 | ((insn & 0x1f) << 5)
5489 | ((insn & 0x3e0) >> 5));
5490 /* If this is a jump to a 32 bit routine, then make
5491 it jalx. */
5492 if (other != STO_MIPS16)
5493 insn |= 0x400;
5494 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5497 else if (r_type == R_MIPS16_GPREL)
5499 unsigned short extend, insn;
5500 bfd_byte buf[4];
5501 unsigned long final;
5503 /* Extract the addend into buf, run the regular reloc,
5504 and stuff the resulting value back into the
5505 instructions. */
5506 if (rel->r_offset > input_section->_raw_size)
5507 r = bfd_reloc_outofrange;
5508 else
5510 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5511 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5512 bfd_put_32 (input_bfd,
5513 (((extend & 0x1f) << 11)
5514 | (extend & 0x7e0)
5515 | (insn & 0x1f)),
5516 buf);
5517 r = _bfd_final_link_relocate (howto, input_bfd,
5518 input_section, buf,
5519 (bfd_vma) 0, relocation,
5520 addend);
5521 final = bfd_get_32 (input_bfd, buf);
5522 bfd_put_16 (input_bfd,
5523 ((extend & 0xf800)
5524 | ((final >> 11) & 0x1f)
5525 | (final & 0x7e0)),
5526 contents + rel->r_offset);
5527 bfd_put_16 (input_bfd,
5528 ((insn & 0xffe0)
5529 | (final & 0x1f)),
5530 contents + rel->r_offset + 2);
5533 else
5534 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5535 contents, rel->r_offset,
5536 relocation, addend);
5538 /* The jal instruction can only jump to an address which is
5539 divisible by 4, and it can only jump to an address with
5540 the same upper 4 bits as the PC. */
5541 if (r == bfd_reloc_ok
5542 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5544 bfd_vma addr;
5545 bfd_vma pc;
5546 bfd_vma target;
5548 pc = ((input_section->output_section->vma
5549 + input_section->output_offset
5550 + rel->r_offset)
5551 & 0xf0000000);
5552 target = bfd_get_32 (input_bfd, contents + rel->r_offset)
5553 & (howto->dst_mask);
5554 addr = (target << 2) + pc;
5555 if (other == STO_MIPS16)
5556 addr &= ~ (bfd_vma) 1;
5557 if ((addr & 3) != 0
5558 || ((addr & 0xf0000000)
5559 != pc))
5560 r = bfd_reloc_overflow;
5563 /* Don't bother to report a relocation overflow for a call
5564 to a weak undefined symbol with a value of zero. This
5565 permits us to use
5566 if (!f) f();
5567 even if we aren't in range to call address zero. */
5568 if (r == bfd_reloc_overflow
5569 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26)
5570 && relocation + addend == 0
5571 && h != NULL
5572 && h->root.type == bfd_link_hash_undefweak)
5573 r = bfd_reloc_ok;
5575 /* If we've already issued an error for an undefined symbol,
5576 don't issue another useless error. */
5577 if (undefined_error
5578 && (r == bfd_reloc_undefined || r == bfd_reloc_overflow))
5579 r = bfd_reloc_ok;
5581 if (SGI_COMPAT (abfd)
5582 && scpt != NULL
5583 && (input_section->flags & SEC_ALLOC) != 0)
5585 Elf32_crinfo cptrel;
5586 bfd_byte *cr;
5588 /* Make an entry of compact relocation info. */
5589 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5590 cptrel.vaddr = (rel->r_offset
5591 + input_section->output_section->vma
5592 + input_section->output_offset);
5594 switch (r_type)
5596 case R_MIPS_26:
5597 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5598 /* XXX How should we set dist2to in this case. */
5599 mips_elf_set_cr_dist2to (cptrel, 8);
5600 cptrel.konst = addend + relocation;
5601 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5602 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5603 ((Elf32_External_crinfo *) cr
5604 + scpt->reloc_count));
5605 ++scpt->reloc_count;
5606 break;
5608 case R_MIPS_GPREL16:
5609 case R_MIPS_LITERAL:
5610 case R_MIPS_GPREL32:
5611 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5612 cptrel.konst = gp - cptrel.vaddr;
5613 mips_elf_set_cr_dist2to (cptrel, 4);
5614 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5615 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5616 ((Elf32_External_crinfo *) cr
5617 + scpt->reloc_count));
5618 ++scpt->reloc_count;
5619 break;
5621 default:
5622 break;
5627 if (r != bfd_reloc_ok)
5629 switch (r)
5631 default:
5632 case bfd_reloc_outofrange:
5633 abort ();
5634 case bfd_reloc_overflow:
5636 const char *name;
5638 if (h != NULL)
5639 name = h->root.root.string;
5640 else
5642 name = bfd_elf_string_from_elf_section (input_bfd,
5643 symtab_hdr->sh_link,
5644 sym->st_name);
5645 if (name == NULL)
5646 return false;
5647 if (*name == '\0')
5648 name = bfd_section_name (input_bfd, sec);
5650 if (! ((*info->callbacks->reloc_overflow)
5651 (info, name, howto->name, (bfd_vma) 0,
5652 input_bfd, input_section, rel->r_offset)))
5653 return false;
5655 break;
5660 return true;
5663 /* This hook function is called before the linker writes out a global
5664 symbol. We mark symbols as small common if appropriate. This is
5665 also where we undo the increment of the value for a mips16 symbol. */
5667 /*ARGSIGNORED*/
5668 static boolean
5669 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5670 bfd *abfd;
5671 struct bfd_link_info *info;
5672 const char *name;
5673 Elf_Internal_Sym *sym;
5674 asection *input_sec;
5676 /* If we see a common symbol, which implies a relocatable link, then
5677 if a symbol was small common in an input file, mark it as small
5678 common in the output file. */
5679 if (sym->st_shndx == SHN_COMMON
5680 && strcmp (input_sec->name, ".scommon") == 0)
5681 sym->st_shndx = SHN_MIPS_SCOMMON;
5683 if (sym->st_other == STO_MIPS16
5684 && (sym->st_value & 1) != 0)
5685 --sym->st_value;
5687 return true;
5690 /* Functions for the dynamic linker. */
5692 /* The name of the dynamic interpreter. This is put in the .interp
5693 section. */
5695 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5697 /* Create dynamic sections when linking against a dynamic object. */
5699 static boolean
5700 mips_elf_create_dynamic_sections (abfd, info)
5701 bfd *abfd;
5702 struct bfd_link_info *info;
5704 struct elf_link_hash_entry *h;
5705 flagword flags;
5706 register asection *s;
5707 const char * const *namep;
5709 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5710 | SEC_LINKER_CREATED | SEC_READONLY);
5712 /* Mips ABI requests the .dynamic section to be read only. */
5713 s = bfd_get_section_by_name (abfd, ".dynamic");
5714 if (s != NULL)
5716 if (! bfd_set_section_flags (abfd, s, flags))
5717 return false;
5720 /* We need to create .got section. */
5721 if (! mips_elf_create_got_section (abfd, info))
5722 return false;
5724 /* Create .stub section. */
5725 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5727 s = bfd_make_section (abfd, ".stub");
5728 if (s == NULL
5729 || ! bfd_set_section_flags (abfd, s, flags)
5730 || ! bfd_set_section_alignment (abfd, s, 2))
5731 return false;
5734 if (SGI_COMPAT (abfd)
5735 && !info->shared
5736 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5738 s = bfd_make_section (abfd, ".rld_map");
5739 if (s == NULL
5740 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5741 || ! bfd_set_section_alignment (abfd, s, 2))
5742 return false;
5745 if (SGI_COMPAT (abfd))
5747 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5749 h = NULL;
5750 if (! (_bfd_generic_link_add_one_symbol
5751 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5752 (bfd_vma) 0, (const char *) NULL, false,
5753 get_elf_backend_data (abfd)->collect,
5754 (struct bfd_link_hash_entry **) &h)))
5755 return false;
5756 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5757 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5758 h->type = STT_SECTION;
5760 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5761 return false;
5764 /* We need to create a .compact_rel section. */
5765 if (! mips_elf_create_compact_rel_section (abfd, info))
5766 return false;
5768 /* Change aligments of some sections. */
5769 s = bfd_get_section_by_name (abfd, ".hash");
5770 if (s != NULL)
5771 bfd_set_section_alignment (abfd, s, 4);
5772 s = bfd_get_section_by_name (abfd, ".dynsym");
5773 if (s != NULL)
5774 bfd_set_section_alignment (abfd, s, 4);
5775 s = bfd_get_section_by_name (abfd, ".dynstr");
5776 if (s != NULL)
5777 bfd_set_section_alignment (abfd, s, 4);
5778 s = bfd_get_section_by_name (abfd, ".reginfo");
5779 if (s != NULL)
5780 bfd_set_section_alignment (abfd, s, 4);
5781 s = bfd_get_section_by_name (abfd, ".dynamic");
5782 if (s != NULL)
5783 bfd_set_section_alignment (abfd, s, 4);
5786 if (!info->shared)
5788 h = NULL;
5789 if (! (_bfd_generic_link_add_one_symbol
5790 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5791 (bfd_vma) 0, (const char *) NULL, false,
5792 get_elf_backend_data (abfd)->collect,
5793 (struct bfd_link_hash_entry **) &h)))
5794 return false;
5795 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5796 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5797 h->type = STT_SECTION;
5799 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5800 return false;
5802 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5804 /* __rld_map is a four byte word located in the .data section
5805 and is filled in by the rtld to contain a pointer to
5806 the _r_debug structure. Its symbol value will be set in
5807 mips_elf_finish_dynamic_symbol. */
5808 s = bfd_get_section_by_name (abfd, ".rld_map");
5809 BFD_ASSERT (s != NULL);
5811 h = NULL;
5812 if (! (_bfd_generic_link_add_one_symbol
5813 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5814 (bfd_vma) 0, (const char *) NULL, false,
5815 get_elf_backend_data (abfd)->collect,
5816 (struct bfd_link_hash_entry **) &h)))
5817 return false;
5818 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5819 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5820 h->type = STT_OBJECT;
5822 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5823 return false;
5827 return true;
5830 /* Create the .compact_rel section. */
5832 static boolean
5833 mips_elf_create_compact_rel_section (abfd, info)
5834 bfd *abfd;
5835 struct bfd_link_info *info;
5837 flagword flags;
5838 register asection *s;
5840 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5842 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5843 | SEC_READONLY);
5845 s = bfd_make_section (abfd, ".compact_rel");
5846 if (s == NULL
5847 || ! bfd_set_section_flags (abfd, s, flags)
5848 || ! bfd_set_section_alignment (abfd, s, 2))
5849 return false;
5851 s->_raw_size = sizeof (Elf32_External_compact_rel);
5854 return true;
5857 /* Create the .got section to hold the global offset table. */
5859 static boolean
5860 mips_elf_create_got_section (abfd, info)
5861 bfd *abfd;
5862 struct bfd_link_info *info;
5864 flagword flags;
5865 register asection *s;
5866 struct elf_link_hash_entry *h;
5867 struct mips_got_info *g;
5869 /* This function may be called more than once. */
5870 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5871 return true;
5873 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5874 | SEC_LINKER_CREATED);
5876 s = bfd_make_section (abfd, ".got");
5877 if (s == NULL
5878 || ! bfd_set_section_flags (abfd, s, flags)
5879 || ! bfd_set_section_alignment (abfd, s, 4))
5880 return false;
5882 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5883 linker script because we don't want to define the symbol if we
5884 are not creating a global offset table. */
5885 h = NULL;
5886 if (! (_bfd_generic_link_add_one_symbol
5887 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5888 (bfd_vma) 0, (const char *) NULL, false,
5889 get_elf_backend_data (abfd)->collect,
5890 (struct bfd_link_hash_entry **) &h)))
5891 return false;
5892 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5893 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5894 h->type = STT_OBJECT;
5896 if (info->shared
5897 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5898 return false;
5900 /* The first several global offset table entries are reserved. */
5901 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5903 g = (struct mips_got_info *) bfd_alloc (abfd,
5904 sizeof (struct mips_got_info));
5905 if (g == NULL)
5906 return false;
5907 g->global_gotsym = 0;
5908 g->local_gotno = MIPS_RESERVED_GOTNO;
5909 g->assigned_gotno = MIPS_RESERVED_GOTNO;
5910 if (elf_section_data (s) == NULL)
5912 s->used_by_bfd =
5913 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5914 if (elf_section_data (s) == NULL)
5915 return false;
5917 elf_section_data (s)->tdata = (PTR) g;
5919 return true;
5922 /* Look through the relocs for a section during the first phase, and
5923 allocate space in the global offset table. */
5925 static boolean
5926 mips_elf_check_relocs (abfd, info, sec, relocs)
5927 bfd *abfd;
5928 struct bfd_link_info *info;
5929 asection *sec;
5930 const Elf_Internal_Rela *relocs;
5932 const char *name;
5933 bfd *dynobj;
5934 Elf_Internal_Shdr *symtab_hdr;
5935 struct elf_link_hash_entry **sym_hashes;
5936 struct mips_got_info *g;
5937 size_t extsymoff;
5938 const Elf_Internal_Rela *rel;
5939 const Elf_Internal_Rela *rel_end;
5940 asection *sgot;
5941 asection *sreloc;
5943 if (info->relocateable)
5944 return true;
5946 dynobj = elf_hash_table (info)->dynobj;
5947 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5948 sym_hashes = elf_sym_hashes (abfd);
5949 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5951 /* Check for the mips16 stub sections. */
5953 name = bfd_get_section_name (abfd, sec);
5954 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5956 unsigned long r_symndx;
5958 /* Look at the relocation information to figure out which symbol
5959 this is for. */
5961 r_symndx = ELF32_R_SYM (relocs->r_info);
5963 if (r_symndx < extsymoff
5964 || sym_hashes[r_symndx - extsymoff] == NULL)
5966 asection *o;
5968 /* This stub is for a local symbol. This stub will only be
5969 needed if there is some relocation in this BFD, other
5970 than a 16 bit function call, which refers to this symbol. */
5971 for (o = abfd->sections; o != NULL; o = o->next)
5973 Elf_Internal_Rela *sec_relocs;
5974 const Elf_Internal_Rela *r, *rend;
5976 /* We can ignore stub sections when looking for relocs. */
5977 if ((o->flags & SEC_RELOC) == 0
5978 || o->reloc_count == 0
5979 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5980 sizeof FN_STUB - 1) == 0
5981 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5982 sizeof CALL_STUB - 1) == 0
5983 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5984 sizeof CALL_FP_STUB - 1) == 0)
5985 continue;
5987 sec_relocs = (_bfd_elf32_link_read_relocs
5988 (abfd, o, (PTR) NULL,
5989 (Elf_Internal_Rela *) NULL,
5990 info->keep_memory));
5991 if (sec_relocs == NULL)
5992 return false;
5994 rend = sec_relocs + o->reloc_count;
5995 for (r = sec_relocs; r < rend; r++)
5996 if (ELF32_R_SYM (r->r_info) == r_symndx
5997 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
5998 break;
6000 if (! info->keep_memory)
6001 free (sec_relocs);
6003 if (r < rend)
6004 break;
6007 if (o == NULL)
6009 /* There is no non-call reloc for this stub, so we do
6010 not need it. Since this function is called before
6011 the linker maps input sections to output sections, we
6012 can easily discard it by setting the SEC_EXCLUDE
6013 flag. */
6014 sec->flags |= SEC_EXCLUDE;
6015 return true;
6018 /* Record this stub in an array of local symbol stubs for
6019 this BFD. */
6020 if (elf_tdata (abfd)->local_stubs == NULL)
6022 unsigned long symcount;
6023 asection **n;
6025 if (elf_bad_symtab (abfd))
6026 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6027 else
6028 symcount = symtab_hdr->sh_info;
6029 n = (asection **) bfd_zalloc (abfd,
6030 symcount * sizeof (asection *));
6031 if (n == NULL)
6032 return false;
6033 elf_tdata (abfd)->local_stubs = n;
6036 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6038 /* We don't need to set mips16_stubs_seen in this case.
6039 That flag is used to see whether we need to look through
6040 the global symbol table for stubs. We don't need to set
6041 it here, because we just have a local stub. */
6043 else
6045 struct mips_elf_link_hash_entry *h;
6047 h = ((struct mips_elf_link_hash_entry *)
6048 sym_hashes[r_symndx - extsymoff]);
6050 /* H is the symbol this stub is for. */
6052 h->fn_stub = sec;
6053 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6056 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6057 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6059 unsigned long r_symndx;
6060 struct mips_elf_link_hash_entry *h;
6061 asection **loc;
6063 /* Look at the relocation information to figure out which symbol
6064 this is for. */
6066 r_symndx = ELF32_R_SYM (relocs->r_info);
6068 if (r_symndx < extsymoff
6069 || sym_hashes[r_symndx - extsymoff] == NULL)
6071 /* This stub was actually built for a static symbol defined
6072 in the same file. We assume that all static symbols in
6073 mips16 code are themselves mips16, so we can simply
6074 discard this stub. Since this function is called before
6075 the linker maps input sections to output sections, we can
6076 easily discard it by setting the SEC_EXCLUDE flag. */
6077 sec->flags |= SEC_EXCLUDE;
6078 return true;
6081 h = ((struct mips_elf_link_hash_entry *)
6082 sym_hashes[r_symndx - extsymoff]);
6084 /* H is the symbol this stub is for. */
6086 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6087 loc = &h->call_fp_stub;
6088 else
6089 loc = &h->call_stub;
6091 /* If we already have an appropriate stub for this function, we
6092 don't need another one, so we can discard this one. Since
6093 this function is called before the linker maps input sections
6094 to output sections, we can easily discard it by setting the
6095 SEC_EXCLUDE flag. We can also discard this section if we
6096 happen to already know that this is a mips16 function; it is
6097 not necessary to check this here, as it is checked later, but
6098 it is slightly faster to check now. */
6099 if (*loc != NULL || h->root.other == STO_MIPS16)
6101 sec->flags |= SEC_EXCLUDE;
6102 return true;
6105 *loc = sec;
6106 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6109 if (dynobj == NULL)
6111 sgot = NULL;
6112 g = NULL;
6114 else
6116 sgot = bfd_get_section_by_name (dynobj, ".got");
6117 if (sgot == NULL)
6118 g = NULL;
6119 else
6121 BFD_ASSERT (elf_section_data (sgot) != NULL);
6122 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6123 BFD_ASSERT (g != NULL);
6127 sreloc = NULL;
6129 rel_end = relocs + sec->reloc_count;
6130 for (rel = relocs; rel < rel_end; rel++)
6132 unsigned long r_symndx;
6133 struct elf_link_hash_entry *h;
6135 r_symndx = ELF32_R_SYM (rel->r_info);
6137 if (r_symndx < extsymoff)
6138 h = NULL;
6139 else
6141 h = sym_hashes[r_symndx - extsymoff];
6143 /* This may be an indirect symbol created because of a version. */
6144 if (h != NULL)
6146 while (h->root.type == bfd_link_hash_indirect)
6147 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6151 /* Some relocs require a global offset table. */
6152 if (dynobj == NULL || sgot == NULL)
6154 switch (ELF32_R_TYPE (rel->r_info))
6156 case R_MIPS_GOT16:
6157 case R_MIPS_CALL16:
6158 case R_MIPS_CALL_HI16:
6159 case R_MIPS_CALL_LO16:
6160 case R_MIPS_GOT_HI16:
6161 case R_MIPS_GOT_LO16:
6162 if (dynobj == NULL)
6163 elf_hash_table (info)->dynobj = dynobj = abfd;
6164 if (! mips_elf_create_got_section (dynobj, info))
6165 return false;
6166 sgot = bfd_get_section_by_name (dynobj, ".got");
6167 BFD_ASSERT (sgot != NULL);
6168 BFD_ASSERT (elf_section_data (sgot) != NULL);
6169 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6170 BFD_ASSERT (g != NULL);
6171 break;
6173 case R_MIPS_32:
6174 case R_MIPS_REL32:
6175 if (dynobj == NULL
6176 && (info->shared || h != NULL)
6177 && (sec->flags & SEC_ALLOC) != 0)
6178 elf_hash_table (info)->dynobj = dynobj = abfd;
6179 break;
6181 default:
6182 break;
6186 switch (ELF32_R_TYPE (rel->r_info))
6188 case R_MIPS_CALL16:
6189 case R_MIPS_CALL_HI16:
6190 case R_MIPS_CALL_LO16:
6191 /* This symbol requires a global offset table entry. */
6193 if (h == NULL)
6195 (*_bfd_error_handler)
6196 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6197 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6198 bfd_set_error (bfd_error_bad_value);
6199 return false;
6202 /* Make sure this symbol is output as a dynamic symbol. */
6203 if (h->dynindx == -1)
6205 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6206 return false;
6209 if (h->got.offset != (bfd_vma) -1)
6211 /* We have already allocated space in the .got. */
6212 break;
6215 /* Note the index of the first global got symbol in .dynsym. */
6216 if (g->global_gotsym == 0
6217 || g->global_gotsym > (unsigned long) h->dynindx)
6218 g->global_gotsym = h->dynindx;
6220 /* Make this symbol to have the corresponding got entry. */
6221 h->got.offset = 0;
6223 /* We need a stub, not a plt entry for the undefined
6224 function. But we record it as if it needs plt. See
6225 elf_adjust_dynamic_symbol in elflink.h. */
6226 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6227 h->type = STT_FUNC;
6229 break;
6231 case R_MIPS_GOT16:
6232 case R_MIPS_GOT_HI16:
6233 case R_MIPS_GOT_LO16:
6234 /* This symbol requires a global offset table entry. */
6236 if (h != NULL)
6238 /* Make sure this symbol is output as a dynamic symbol. */
6239 if (h->dynindx == -1)
6241 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6242 return false;
6245 if (h->got.offset != (bfd_vma) -1)
6247 /* We have already allocated space in the .got. */
6248 break;
6250 /* Note the index of the first global got symbol in
6251 .dynsym. */
6252 if (g->global_gotsym == 0
6253 || g->global_gotsym > (unsigned long) h->dynindx)
6254 g->global_gotsym = h->dynindx;
6256 /* Make this symbol to be the global got symbol. */
6257 h->got.offset = 0;
6260 break;
6262 case R_MIPS_32:
6263 case R_MIPS_REL32:
6264 if ((info->shared || h != NULL)
6265 && (sec->flags & SEC_ALLOC) != 0)
6267 if (sreloc == NULL)
6269 const char *name = ".rel.dyn";
6271 sreloc = bfd_get_section_by_name (dynobj, name);
6272 if (sreloc == NULL)
6274 sreloc = bfd_make_section (dynobj, name);
6275 if (sreloc == NULL
6276 || ! bfd_set_section_flags (dynobj, sreloc,
6277 (SEC_ALLOC
6278 | SEC_LOAD
6279 | SEC_HAS_CONTENTS
6280 | SEC_IN_MEMORY
6281 | SEC_LINKER_CREATED
6282 | SEC_READONLY))
6283 || ! bfd_set_section_alignment (dynobj, sreloc,
6285 return false;
6288 if (info->shared)
6290 /* When creating a shared object, we must copy these
6291 reloc types into the output file as R_MIPS_REL32
6292 relocs. We make room for this reloc in the
6293 .rel.dyn reloc section */
6294 if (sreloc->_raw_size == 0)
6296 /* Add a null element. */
6297 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6298 ++sreloc->reloc_count;
6300 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6302 else
6304 struct mips_elf_link_hash_entry *hmips;
6306 /* We only need to copy this reloc if the symbol is
6307 defined in a dynamic object. */
6308 hmips = (struct mips_elf_link_hash_entry *) h;
6309 ++hmips->mips_32_relocs;
6313 if (SGI_COMPAT (abfd))
6314 mips_elf_hash_table (info)->compact_rel_size +=
6315 sizeof (Elf32_External_crinfo);
6317 break;
6319 case R_MIPS_26:
6320 case R_MIPS_GPREL16:
6321 case R_MIPS_LITERAL:
6322 case R_MIPS_GPREL32:
6323 if (SGI_COMPAT (abfd))
6324 mips_elf_hash_table (info)->compact_rel_size +=
6325 sizeof (Elf32_External_crinfo);
6326 break;
6328 /* This relocation describes the C++ object vtable hierarchy.
6329 Reconstruct it for later use during GC. */
6330 case R_MIPS_GNU_VTINHERIT:
6331 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6332 return false;
6333 break;
6335 /* This relocation describes which C++ vtable entries are actually
6336 used. Record for later use during GC. */
6337 case R_MIPS_GNU_VTENTRY:
6338 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6339 return false;
6340 break;
6342 default:
6343 break;
6346 /* If this reloc is not a 16 bit call, and it has a global
6347 symbol, then we will need the fn_stub if there is one.
6348 References from a stub section do not count. */
6349 if (h != NULL
6350 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6351 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6352 sizeof FN_STUB - 1) != 0
6353 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6354 sizeof CALL_STUB - 1) != 0
6355 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6356 sizeof CALL_FP_STUB - 1) != 0)
6358 struct mips_elf_link_hash_entry *mh;
6360 mh = (struct mips_elf_link_hash_entry *) h;
6361 mh->need_fn_stub = true;
6365 return true;
6368 /* Return the section that should be marked against GC for a given
6369 relocation. */
6371 static asection *
6372 mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
6373 bfd *abfd;
6374 struct bfd_link_info *info;
6375 Elf_Internal_Rela *rel;
6376 struct elf_link_hash_entry *h;
6377 Elf_Internal_Sym *sym;
6379 /* ??? Do mips16 stub sections need to be handled special? */
6381 if (h != NULL)
6383 switch (ELF32_R_TYPE (rel->r_info))
6385 case R_MIPS_GNU_VTINHERIT:
6386 case R_MIPS_GNU_VTENTRY:
6387 break;
6389 default:
6390 switch (h->root.type)
6392 case bfd_link_hash_defined:
6393 case bfd_link_hash_defweak:
6394 return h->root.u.def.section;
6396 case bfd_link_hash_common:
6397 return h->root.u.c.p->section;
6399 default:
6400 break;
6404 else
6406 if (!(elf_bad_symtab (abfd)
6407 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6408 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
6409 && sym->st_shndx != SHN_COMMON))
6411 return bfd_section_from_elf_index (abfd, sym->st_shndx);
6415 return NULL;
6418 /* Update the got entry reference counts for the section being removed. */
6420 static boolean
6421 mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6422 bfd *abfd;
6423 struct bfd_link_info *info;
6424 asection *sec;
6425 const Elf_Internal_Rela *relocs;
6427 #if 0
6428 Elf_Internal_Shdr *symtab_hdr;
6429 struct elf_link_hash_entry **sym_hashes;
6430 bfd_signed_vma *local_got_refcounts;
6431 const Elf_Internal_Rela *rel, *relend;
6432 unsigned long r_symndx;
6433 struct elf_link_hash_entry *h;
6435 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6436 sym_hashes = elf_sym_hashes (abfd);
6437 local_got_refcounts = elf_local_got_refcounts (abfd);
6439 relend = relocs + sec->reloc_count;
6440 for (rel = relocs; rel < relend; rel++)
6441 switch (ELF32_R_TYPE (rel->r_info))
6443 case R_MIPS_GOT16:
6444 case R_MIPS_CALL16:
6445 case R_MIPS_CALL_HI16:
6446 case R_MIPS_CALL_LO16:
6447 case R_MIPS_GOT_HI16:
6448 case R_MIPS_GOT_LO16:
6449 /* ??? It would seem that the existing MIPS code does no sort
6450 of reference counting or whatnot on its GOT and PLT entries,
6451 so it is not possible to garbage collect them at this time. */
6452 break;
6454 default:
6455 break;
6457 #endif
6459 return true;
6463 /* Adjust a symbol defined by a dynamic object and referenced by a
6464 regular object. The current definition is in some section of the
6465 dynamic object, but we're not including those sections. We have to
6466 change the definition to something the rest of the link can
6467 understand. */
6469 static boolean
6470 mips_elf_adjust_dynamic_symbol (info, h)
6471 struct bfd_link_info *info;
6472 struct elf_link_hash_entry *h;
6474 bfd *dynobj;
6475 struct mips_elf_link_hash_entry *hmips;
6476 asection *s;
6478 dynobj = elf_hash_table (info)->dynobj;
6480 /* Make sure we know what is going on here. */
6481 BFD_ASSERT (dynobj != NULL
6482 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6483 || h->weakdef != NULL
6484 || ((h->elf_link_hash_flags
6485 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6486 && (h->elf_link_hash_flags
6487 & ELF_LINK_HASH_REF_REGULAR) != 0
6488 && (h->elf_link_hash_flags
6489 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6491 /* If this symbol is defined in a dynamic object, we need to copy
6492 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6493 file. */
6494 hmips = (struct mips_elf_link_hash_entry *) h;
6495 if (! info->relocateable
6496 && hmips->mips_32_relocs != 0
6497 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6499 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6500 BFD_ASSERT (s != NULL);
6502 if (s->_raw_size == 0)
6504 /* Make room for a null element. */
6505 s->_raw_size += sizeof (Elf32_External_Rel);
6506 ++s->reloc_count;
6508 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6511 /* For a function, create a stub, if needed. */
6512 if (h->type == STT_FUNC
6513 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6515 if (! elf_hash_table (info)->dynamic_sections_created)
6516 return true;
6518 /* If this symbol is not defined in a regular file, then set
6519 the symbol to the stub location. This is required to make
6520 function pointers compare as equal between the normal
6521 executable and the shared library. */
6522 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6524 /* We need .stub section. */
6525 s = bfd_get_section_by_name (dynobj, ".stub");
6526 BFD_ASSERT (s != NULL);
6528 h->root.u.def.section = s;
6529 h->root.u.def.value = s->_raw_size;
6531 /* XXX Write this stub address somewhere. */
6532 h->plt.offset = s->_raw_size;
6534 /* Make room for this stub code. */
6535 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6537 /* The last half word of the stub will be filled with the index
6538 of this symbol in .dynsym section. */
6539 return true;
6543 /* If this is a weak symbol, and there is a real definition, the
6544 processor independent code will have arranged for us to see the
6545 real definition first, and we can just use the same value. */
6546 if (h->weakdef != NULL)
6548 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6549 || h->weakdef->root.type == bfd_link_hash_defweak);
6550 h->root.u.def.section = h->weakdef->root.u.def.section;
6551 h->root.u.def.value = h->weakdef->root.u.def.value;
6552 return true;
6555 /* This is a reference to a symbol defined by a dynamic object which
6556 is not a function. */
6558 return true;
6561 /* This function is called after all the input files have been read,
6562 and the input sections have been assigned to output sections. We
6563 check for any mips16 stub sections that we can discard. */
6565 static boolean mips_elf_check_mips16_stubs
6566 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6568 static boolean
6569 mips_elf_always_size_sections (output_bfd, info)
6570 bfd *output_bfd;
6571 struct bfd_link_info *info;
6573 asection *ri;
6575 /* The .reginfo section has a fixed size. */
6576 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
6577 if (ri != NULL)
6578 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
6580 if (info->relocateable
6581 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6582 return true;
6584 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6585 mips_elf_check_mips16_stubs,
6586 (PTR) NULL);
6588 return true;
6591 /* Check the mips16 stubs for a particular symbol, and see if we can
6592 discard them. */
6594 /*ARGSUSED*/
6595 static boolean
6596 mips_elf_check_mips16_stubs (h, data)
6597 struct mips_elf_link_hash_entry *h;
6598 PTR data;
6600 if (h->fn_stub != NULL
6601 && ! h->need_fn_stub)
6603 /* We don't need the fn_stub; the only references to this symbol
6604 are 16 bit calls. Clobber the size to 0 to prevent it from
6605 being included in the link. */
6606 h->fn_stub->_raw_size = 0;
6607 h->fn_stub->_cooked_size = 0;
6608 h->fn_stub->flags &= ~ SEC_RELOC;
6609 h->fn_stub->reloc_count = 0;
6610 h->fn_stub->flags |= SEC_EXCLUDE;
6613 if (h->call_stub != NULL
6614 && h->root.other == STO_MIPS16)
6616 /* We don't need the call_stub; this is a 16 bit function, so
6617 calls from other 16 bit functions are OK. Clobber the size
6618 to 0 to prevent it from being included in the link. */
6619 h->call_stub->_raw_size = 0;
6620 h->call_stub->_cooked_size = 0;
6621 h->call_stub->flags &= ~ SEC_RELOC;
6622 h->call_stub->reloc_count = 0;
6623 h->call_stub->flags |= SEC_EXCLUDE;
6626 if (h->call_fp_stub != NULL
6627 && h->root.other == STO_MIPS16)
6629 /* We don't need the call_stub; this is a 16 bit function, so
6630 calls from other 16 bit functions are OK. Clobber the size
6631 to 0 to prevent it from being included in the link. */
6632 h->call_fp_stub->_raw_size = 0;
6633 h->call_fp_stub->_cooked_size = 0;
6634 h->call_fp_stub->flags &= ~ SEC_RELOC;
6635 h->call_fp_stub->reloc_count = 0;
6636 h->call_fp_stub->flags |= SEC_EXCLUDE;
6639 return true;
6642 /* Set the sizes of the dynamic sections. */
6644 static boolean
6645 mips_elf_size_dynamic_sections (output_bfd, info)
6646 bfd *output_bfd;
6647 struct bfd_link_info *info;
6649 bfd *dynobj;
6650 asection *s;
6651 boolean reltext;
6652 asection *sgot;
6653 struct mips_got_info *g;
6655 dynobj = elf_hash_table (info)->dynobj;
6656 BFD_ASSERT (dynobj != NULL);
6658 if (elf_hash_table (info)->dynamic_sections_created)
6660 /* Set the contents of the .interp section to the interpreter. */
6661 if (! info->shared)
6663 s = bfd_get_section_by_name (dynobj, ".interp");
6664 BFD_ASSERT (s != NULL);
6665 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6666 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6670 /* Recompute the size of .got for local entires (reserved and
6671 hipages) if needed. To estimate it, get the upper bound of total
6672 size of loadable sections. */
6673 sgot = bfd_get_section_by_name (dynobj, ".got");
6675 if (sgot != NULL)
6677 bfd_size_type loadable_size = 0;
6678 bfd_size_type local_gotno;
6679 struct _bfd *sub;
6681 BFD_ASSERT (elf_section_data (sgot) != NULL);
6682 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6683 BFD_ASSERT (g != NULL);
6685 for (sub = info->input_bfds; sub; sub = sub->link_next)
6686 for (s = sub->sections; s != NULL; s = s->next)
6688 if ((s->flags & SEC_ALLOC) == 0)
6689 continue;
6690 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6693 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6695 /* Assume there are two loadable segments consisting of
6696 contiguous sections. Is 5 enough? */
6697 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6698 g->local_gotno = local_gotno;
6699 sgot->_raw_size += local_gotno * 4;
6702 /* The check_relocs and adjust_dynamic_symbol entry points have
6703 determined the sizes of the various dynamic sections. Allocate
6704 memory for them. */
6705 reltext = false;
6706 for (s = dynobj->sections; s != NULL; s = s->next)
6708 const char *name;
6709 boolean strip;
6711 /* It's OK to base decisions on the section name, because none
6712 of the dynobj section names depend upon the input files. */
6713 name = bfd_get_section_name (dynobj, s);
6715 if ((s->flags & SEC_LINKER_CREATED) == 0)
6716 continue;
6718 strip = false;
6720 if (strncmp (name, ".rel", 4) == 0)
6722 if (s->_raw_size == 0)
6724 /* We only strip the section if the output section name
6725 has the same name. Otherwise, there might be several
6726 input sections for this output section. FIXME: This
6727 code is probably not needed these days anyhow, since
6728 the linker now does not create empty output sections. */
6729 if (s->output_section != NULL
6730 && strcmp (name,
6731 bfd_get_section_name (s->output_section->owner,
6732 s->output_section)) == 0)
6733 strip = true;
6735 else
6737 const char *outname;
6738 asection *target;
6740 /* If this relocation section applies to a read only
6741 section, then we probably need a DT_TEXTREL entry.
6742 If the relocation section is .rel.dyn, we always
6743 assert a DT_TEXTREL entry rather than testing whether
6744 there exists a relocation to a read only section or
6745 not. */
6746 outname = bfd_get_section_name (output_bfd,
6747 s->output_section);
6748 target = bfd_get_section_by_name (output_bfd, outname + 4);
6749 if ((target != NULL
6750 && (target->flags & SEC_READONLY) != 0
6751 && (target->flags & SEC_ALLOC) != 0)
6752 || strcmp (outname, ".rel.dyn") == 0)
6753 reltext = true;
6755 /* We use the reloc_count field as a counter if we need
6756 to copy relocs into the output file. */
6757 if (strcmp (name, ".rel.dyn") != 0)
6758 s->reloc_count = 0;
6761 else if (strncmp (name, ".got", 4) == 0)
6763 int i;
6765 BFD_ASSERT (elf_section_data (s) != NULL);
6766 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6767 BFD_ASSERT (g != NULL);
6769 /* Fix the size of .got section for the correspondence of
6770 global symbols and got entries. This adds some useless
6771 got entries. Is this required by ABI really? */
6772 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6773 s->_raw_size += i * 4;
6775 else if (strncmp (name, ".stub", 5) == 0)
6777 /* Irix rld assumes that the function stub isn't at the end
6778 of .text section. So put a dummy. XXX */
6779 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6781 else if (! info->shared
6782 && ! mips_elf_hash_table (info)->use_rld_obj_head
6783 && strncmp (name, ".rld_map", 8) == 0)
6785 /* We add a room for __rld_map. It will be filled in by the
6786 rtld to contain a pointer to the _r_debug structure. */
6787 s->_raw_size += 4;
6789 else if (SGI_COMPAT (output_bfd)
6790 && strncmp (name, ".compact_rel", 12) == 0)
6791 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6792 else if (strncmp (name, ".init", 5) != 0)
6794 /* It's not one of our sections, so don't allocate space. */
6795 continue;
6798 if (strip)
6800 _bfd_strip_section_from_output (s);
6801 continue;
6804 /* Allocate memory for the section contents. */
6805 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6806 if (s->contents == NULL && s->_raw_size != 0)
6808 bfd_set_error (bfd_error_no_memory);
6809 return false;
6811 memset (s->contents, 0, s->_raw_size);
6814 if (elf_hash_table (info)->dynamic_sections_created)
6816 /* Add some entries to the .dynamic section. We fill in the
6817 values later, in elf_mips_finish_dynamic_sections, but we
6818 must add the entries now so that we get the correct size for
6819 the .dynamic section. The DT_DEBUG entry is filled in by the
6820 dynamic linker and used by the debugger. */
6821 if (! info->shared)
6823 if (SGI_COMPAT (output_bfd))
6825 /* SGI object has the equivalence of DT_DEBUG in the
6826 DT_MIPS_RLD_MAP entry. */
6827 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6828 return false;
6830 else
6831 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6832 return false;
6835 if (reltext)
6837 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6838 return false;
6841 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6842 return false;
6844 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6846 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6847 return false;
6849 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6850 return false;
6852 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6853 return false;
6856 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6857 return false;
6859 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6860 return false;
6862 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6864 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6865 return false;
6867 s = bfd_get_section_by_name (dynobj, ".liblist");
6868 BFD_ASSERT (s != NULL);
6870 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6871 return false;
6874 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6875 return false;
6877 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6878 return false;
6880 #if 0
6881 /* Time stamps in executable files are a bad idea. */
6882 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6883 return false;
6884 #endif
6886 #if 0 /* FIXME */
6887 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6888 return false;
6889 #endif
6891 #if 0 /* FIXME */
6892 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6893 return false;
6894 #endif
6896 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6897 return false;
6899 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6900 return false;
6902 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6903 return false;
6905 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6906 return false;
6908 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6909 return false;
6911 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6912 return false;
6914 #if 0 /* (SGI_COMPAT) */
6915 if (! bfd_get_section_by_name (dynobj, ".init"))
6916 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6917 return false;
6919 if (! bfd_get_section_by_name (dynobj, ".fini"))
6920 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6921 return false;
6922 #endif
6925 /* If we use dynamic linking, we generate a section symbol for each
6926 output section. These are local symbols, which means that they
6927 must come first in the dynamic symbol table.
6928 That means we must increment the dynamic symbol index of every
6929 other dynamic symbol. */
6931 unsigned int c, i;
6932 struct mips_got_info *g;
6934 c = 0;
6935 if (elf_hash_table (info)->dynamic_sections_created)
6937 #if 0
6938 /* We no longer try to restrict the set of sections which get
6939 dynamic symbol table entries, since it fails if we have
6940 other random sections which need dynamic relocations. */
6941 const char * const *namep;
6942 bfd_size_type strindex;
6943 struct bfd_strtab_hash *dynstr;
6945 if (SGI_COMPAT (output_bfd))
6947 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
6948 elf_link_hash_traverse (elf_hash_table (info),
6949 mips_elf_adjust_dynindx,
6950 (PTR) &c);
6951 elf_hash_table (info)->dynsymcount += c;
6953 dynstr = elf_hash_table (info)->dynstr;
6954 BFD_ASSERT (dynstr != NULL);
6956 for (i = 1, namep = mips_elf_dynsym_sec_names;
6957 *namep != NULL;
6958 i++, namep++)
6960 s = bfd_get_section_by_name (output_bfd, *namep);
6961 if (s != NULL)
6962 elf_section_data (s)->dynindx = i;
6964 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
6965 if (strindex == (bfd_size_type) -1)
6966 return false;
6968 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
6971 else
6972 #endif /* 0 */
6974 c = bfd_count_sections (output_bfd);
6975 elf_link_hash_traverse (elf_hash_table (info),
6976 mips_elf_adjust_dynindx,
6977 (PTR) &c);
6978 elf_hash_table (info)->dynsymcount += c;
6980 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
6982 elf_section_data (s)->dynindx = i;
6983 /* These symbols will have no names, so we don't need to
6984 fiddle with dynstr_index. */
6989 if (sgot != NULL)
6991 BFD_ASSERT (elf_section_data (sgot) != NULL);
6992 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6993 BFD_ASSERT (g != NULL);
6995 /* If there are no global got symbols, fake the last symbol so
6996 for safety. */
6997 if (g->global_gotsym)
6998 g->global_gotsym += c;
6999 else
7000 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
7004 return true;
7007 /* Increment the index of a dynamic symbol by a given amount. Called
7008 via elf_link_hash_traverse. */
7010 static boolean
7011 mips_elf_adjust_dynindx (h, cparg)
7012 struct elf_link_hash_entry *h;
7013 PTR cparg;
7015 unsigned int *cp = (unsigned int *) cparg;
7017 if (h->dynindx != -1)
7018 h->dynindx += *cp;
7019 return true;
7022 /* Finish up dynamic symbol handling. We set the contents of various
7023 dynamic sections here. */
7025 static boolean
7026 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7027 bfd *output_bfd;
7028 struct bfd_link_info *info;
7029 struct elf_link_hash_entry *h;
7030 Elf_Internal_Sym *sym;
7032 bfd *dynobj;
7033 bfd_vma gval;
7034 asection *sgot;
7035 struct mips_got_info *g;
7036 const char *name;
7038 dynobj = elf_hash_table (info)->dynobj;
7039 gval = sym->st_value;
7041 if (h->plt.offset != (bfd_vma) -1)
7043 asection *s;
7044 bfd_byte *p;
7045 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7047 /* This symbol has a stub. Set it up. */
7049 BFD_ASSERT (h->dynindx != -1);
7051 s = bfd_get_section_by_name (dynobj, ".stub");
7052 BFD_ASSERT (s != NULL);
7054 /* Fill the stub. */
7055 p = stub;
7056 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7057 p += 4;
7058 bfd_put_32 (output_bfd, STUB_MOVE, p);
7059 p += 4;
7061 /* FIXME: Can h->dynindex be more than 64K? */
7062 if (h->dynindx & 0xffff0000)
7063 return false;
7065 bfd_put_32 (output_bfd, STUB_JALR, p);
7066 p += 4;
7067 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7069 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7070 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7072 /* Mark the symbol as undefined. plt.offset != -1 occurs
7073 only for the referenced symbol. */
7074 sym->st_shndx = SHN_UNDEF;
7076 /* The run-time linker uses the st_value field of the symbol
7077 to reset the global offset table entry for this external
7078 to its stub address when unlinking a shared object. */
7079 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7080 sym->st_value = gval;
7083 BFD_ASSERT (h->dynindx != -1);
7085 sgot = bfd_get_section_by_name (dynobj, ".got");
7086 BFD_ASSERT (sgot != NULL);
7087 BFD_ASSERT (elf_section_data (sgot) != NULL);
7088 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7089 BFD_ASSERT (g != NULL);
7091 if ((unsigned long) h->dynindx >= g->global_gotsym)
7093 bfd_size_type offset;
7095 /* This symbol has an entry in the global offset table. Set its
7096 value to the corresponding got entry, if needed. */
7097 if (h->got.offset == (bfd_vma) -1)
7099 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
7100 BFD_ASSERT (g->local_gotno * 4 <= offset
7101 && offset < sgot->_raw_size);
7102 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
7106 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7107 name = h->root.root.string;
7108 if (strcmp (name, "_DYNAMIC") == 0
7109 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7110 sym->st_shndx = SHN_ABS;
7111 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7113 sym->st_shndx = SHN_ABS;
7114 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7115 sym->st_value = 1;
7117 else if (SGI_COMPAT (output_bfd))
7119 if (strcmp (name, "_gp_disp") == 0)
7121 sym->st_shndx = SHN_ABS;
7122 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7123 sym->st_value = elf_gp (output_bfd);
7125 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7126 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7128 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7129 sym->st_other = STO_PROTECTED;
7130 sym->st_value = 0;
7131 sym->st_shndx = SHN_MIPS_DATA;
7133 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7135 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7136 sym->st_other = STO_PROTECTED;
7137 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7138 sym->st_shndx = SHN_ABS;
7140 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7142 if (h->type == STT_FUNC)
7143 sym->st_shndx = SHN_MIPS_TEXT;
7144 else if (h->type == STT_OBJECT)
7145 sym->st_shndx = SHN_MIPS_DATA;
7149 if (SGI_COMPAT (output_bfd)
7150 && ! info->shared)
7152 if (! mips_elf_hash_table (info)->use_rld_obj_head
7153 && strcmp (name, "__rld_map") == 0)
7155 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7156 BFD_ASSERT (s != NULL);
7157 sym->st_value = s->output_section->vma + s->output_offset;
7158 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7159 if (mips_elf_hash_table (info)->rld_value == 0)
7160 mips_elf_hash_table (info)->rld_value = sym->st_value;
7162 else if (mips_elf_hash_table (info)->use_rld_obj_head
7163 && strcmp (name, "__rld_obj_head") == 0)
7165 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7166 BFD_ASSERT (s != NULL);
7167 mips_elf_hash_table (info)->rld_value = sym->st_value;
7171 /* If this is a mips16 symbol, force the value to be even. */
7172 if (sym->st_other == STO_MIPS16
7173 && (sym->st_value & 1) != 0)
7174 --sym->st_value;
7176 return true;
7179 /* Finish up the dynamic sections. */
7181 static boolean
7182 mips_elf_finish_dynamic_sections (output_bfd, info)
7183 bfd *output_bfd;
7184 struct bfd_link_info *info;
7186 bfd *dynobj;
7187 asection *sdyn;
7188 asection *sgot;
7189 struct mips_got_info *g;
7191 dynobj = elf_hash_table (info)->dynobj;
7193 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7195 sgot = bfd_get_section_by_name (dynobj, ".got");
7196 if (sgot == NULL)
7197 g = NULL;
7198 else
7200 BFD_ASSERT (elf_section_data (sgot) != NULL);
7201 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7202 BFD_ASSERT (g != NULL);
7205 if (elf_hash_table (info)->dynamic_sections_created)
7207 Elf32_External_Dyn *dyncon, *dynconend;
7209 BFD_ASSERT (sdyn != NULL);
7210 BFD_ASSERT (g != NULL);
7212 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7213 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7214 for (; dyncon < dynconend; dyncon++)
7216 Elf_Internal_Dyn dyn;
7217 const char *name;
7218 size_t elemsize;
7219 asection *s;
7221 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7223 switch (dyn.d_tag)
7225 default:
7226 break;
7228 case DT_RELENT:
7229 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7230 BFD_ASSERT (s != NULL);
7231 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
7232 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7233 break;
7235 case DT_STRSZ:
7236 /* Rewrite DT_STRSZ. */
7237 dyn.d_un.d_val =
7238 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7239 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7240 break;
7242 case DT_PLTGOT:
7243 name = ".got";
7244 goto get_vma;
7245 case DT_MIPS_CONFLICT:
7246 name = ".conflict";
7247 goto get_vma;
7248 case DT_MIPS_LIBLIST:
7249 name = ".liblist";
7250 get_vma:
7251 s = bfd_get_section_by_name (output_bfd, name);
7252 BFD_ASSERT (s != NULL);
7253 dyn.d_un.d_ptr = s->vma;
7254 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7255 break;
7257 case DT_MIPS_RLD_VERSION:
7258 dyn.d_un.d_val = 1; /* XXX */
7259 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7260 break;
7262 case DT_MIPS_FLAGS:
7263 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7264 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7265 break;
7267 case DT_MIPS_CONFLICTNO:
7268 name = ".conflict";
7269 elemsize = sizeof (Elf32_Conflict);
7270 goto set_elemno;
7272 case DT_MIPS_LIBLISTNO:
7273 name = ".liblist";
7274 elemsize = sizeof (Elf32_Lib);
7275 set_elemno:
7276 s = bfd_get_section_by_name (output_bfd, name);
7277 if (s != NULL)
7279 if (s->_cooked_size != 0)
7280 dyn.d_un.d_val = s->_cooked_size / elemsize;
7281 else
7282 dyn.d_un.d_val = s->_raw_size / elemsize;
7284 else
7285 dyn.d_un.d_val = 0;
7287 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7288 break;
7290 case DT_MIPS_TIME_STAMP:
7291 time ((time_t *) &dyn.d_un.d_val);
7292 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7293 break;
7295 case DT_MIPS_ICHECKSUM:
7296 /* XXX FIXME: */
7297 break;
7299 case DT_MIPS_IVERSION:
7300 /* XXX FIXME: */
7301 break;
7303 case DT_MIPS_BASE_ADDRESS:
7304 s = output_bfd->sections;
7305 BFD_ASSERT (s != NULL);
7306 dyn.d_un.d_ptr = s->vma & ~(0xffff);
7307 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7308 break;
7310 case DT_MIPS_LOCAL_GOTNO:
7311 dyn.d_un.d_val = g->local_gotno;
7312 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7313 break;
7315 case DT_MIPS_SYMTABNO:
7316 name = ".dynsym";
7317 elemsize = sizeof (Elf32_External_Sym);
7318 s = bfd_get_section_by_name (output_bfd, name);
7319 BFD_ASSERT (s != NULL);
7321 if (s->_cooked_size != 0)
7322 dyn.d_un.d_val = s->_cooked_size / elemsize;
7323 else
7324 dyn.d_un.d_val = s->_raw_size / elemsize;
7325 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7326 break;
7328 case DT_MIPS_UNREFEXTNO:
7329 #if 0
7330 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
7331 #else
7332 dyn.d_un.d_val = bfd_count_sections (output_bfd);
7333 #endif
7334 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7335 break;
7337 case DT_MIPS_GOTSYM:
7338 dyn.d_un.d_val = g->global_gotsym;
7339 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7340 break;
7342 case DT_MIPS_HIPAGENO:
7343 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7344 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7345 break;
7347 case DT_MIPS_RLD_MAP:
7348 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7349 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7350 break;
7356 /* The first entry of the global offset table will be filled at
7357 runtime. The second entry will be used by some runtime loaders.
7358 This isn't the case of Irix rld. */
7359 if (sgot != NULL && sgot->_raw_size > 0)
7361 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7362 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
7365 if (sgot != NULL)
7366 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7369 asection *sdynsym;
7370 asection *s;
7371 Elf_Internal_Sym sym;
7372 Elf32_compact_rel cpt;
7374 /* Set up the section symbols for the output sections. SGI sets
7375 the STT_NOTYPE attribute for these symbols. Should we do so? */
7377 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
7378 if (sdynsym != NULL)
7380 #if 0
7381 const char *name;
7382 const char * const * namep = mips_elf_dynsym_sec_names;
7383 unsigned int i;
7384 bfd_vma last;
7385 long dindx;
7387 /* We no longer try to restrict the set of sections which get
7388 dynamic symbol table entries, since it fails if we have
7389 other random sections which need dynamic relocations. */
7390 if (SGI_COMPAT (output_bfd))
7392 sym.st_size = 0;
7393 sym.st_name = 0;
7394 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7395 sym.st_other = 0;
7397 i = 0;
7398 last = 0;
7399 dindx = 0;
7400 while ((name = *namep++) != NULL)
7402 s = bfd_get_section_by_name (output_bfd, name);
7403 if (s != NULL)
7405 sym.st_value = s->vma;
7406 dindx = elf_section_data (s)->dynindx;
7407 last = s->vma + s->_raw_size;
7409 else
7411 sym.st_value = last;
7412 dindx++;
7415 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7416 ? SHN_MIPS_TEXT
7417 : SHN_MIPS_DATA);
7418 ++i;
7419 sym.st_name =
7420 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7422 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7423 (((Elf32_External_Sym *)
7424 sdynsym->contents)
7425 + dindx));
7428 /* Set the sh_info field of the output .dynsym section to
7429 the index of the first global symbol. */
7430 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7431 SIZEOF_MIPS_DYNSYM_SECNAMES;
7433 else
7434 #endif /* 0 */
7436 sym.st_size = 0;
7437 sym.st_name = 0;
7438 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7439 sym.st_other = 0;
7441 for (s = output_bfd->sections; s != NULL; s = s->next)
7443 int indx;
7445 sym.st_value = s->vma;
7447 indx = elf_section_data (s)->this_idx;
7448 BFD_ASSERT (indx > 0);
7449 sym.st_shndx = indx;
7451 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7452 (((Elf32_External_Sym *)
7453 sdynsym->contents)
7454 + elf_section_data (s)->dynindx));
7457 /* Set the sh_info field of the output .dynsym section to
7458 the index of the first global symbol. */
7459 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7460 bfd_count_sections (output_bfd) + 1;
7464 if (SGI_COMPAT (output_bfd))
7466 /* Write .compact_rel section out. */
7467 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7468 if (s != NULL)
7470 cpt.id1 = 1;
7471 cpt.num = s->reloc_count;
7472 cpt.id2 = 2;
7473 cpt.offset = (s->output_section->filepos
7474 + sizeof (Elf32_External_compact_rel));
7475 cpt.reserved0 = 0;
7476 cpt.reserved1 = 0;
7477 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7478 ((Elf32_External_compact_rel *)
7479 s->contents));
7481 /* Clean up a dummy stub function entry in .text. */
7482 s = bfd_get_section_by_name (dynobj, ".stub");
7483 if (s != NULL)
7485 file_ptr dummy_offset;
7487 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7488 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7489 memset (s->contents + dummy_offset, 0,
7490 MIPS_FUNCTION_STUB_SIZE);
7495 /* Clean up a first relocation in .rel.dyn. */
7496 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7497 if (s != NULL && s->_raw_size > 0)
7498 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7501 return true;
7504 /* This is almost identical to bfd_generic_get_... except that some
7505 MIPS relocations need to be handled specially. Sigh. */
7507 static bfd_byte *
7508 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7509 relocateable, symbols)
7510 bfd *abfd;
7511 struct bfd_link_info *link_info;
7512 struct bfd_link_order *link_order;
7513 bfd_byte *data;
7514 boolean relocateable;
7515 asymbol **symbols;
7517 /* Get enough memory to hold the stuff */
7518 bfd *input_bfd = link_order->u.indirect.section->owner;
7519 asection *input_section = link_order->u.indirect.section;
7521 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7522 arelent **reloc_vector = NULL;
7523 long reloc_count;
7525 if (reloc_size < 0)
7526 goto error_return;
7528 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7529 if (reloc_vector == NULL && reloc_size != 0)
7530 goto error_return;
7532 /* read in the section */
7533 if (!bfd_get_section_contents (input_bfd,
7534 input_section,
7535 (PTR) data,
7537 input_section->_raw_size))
7538 goto error_return;
7540 /* We're not relaxing the section, so just copy the size info */
7541 input_section->_cooked_size = input_section->_raw_size;
7542 input_section->reloc_done = true;
7544 reloc_count = bfd_canonicalize_reloc (input_bfd,
7545 input_section,
7546 reloc_vector,
7547 symbols);
7548 if (reloc_count < 0)
7549 goto error_return;
7551 if (reloc_count > 0)
7553 arelent **parent;
7554 /* for mips */
7555 int gp_found;
7556 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7559 struct bfd_hash_entry *h;
7560 struct bfd_link_hash_entry *lh;
7561 /* Skip all this stuff if we aren't mixing formats. */
7562 if (abfd && input_bfd
7563 && abfd->xvec == input_bfd->xvec)
7564 lh = 0;
7565 else
7567 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7568 lh = (struct bfd_link_hash_entry *) h;
7570 lookup:
7571 if (lh)
7573 switch (lh->type)
7575 case bfd_link_hash_undefined:
7576 case bfd_link_hash_undefweak:
7577 case bfd_link_hash_common:
7578 gp_found = 0;
7579 break;
7580 case bfd_link_hash_defined:
7581 case bfd_link_hash_defweak:
7582 gp_found = 1;
7583 gp = lh->u.def.value;
7584 break;
7585 case bfd_link_hash_indirect:
7586 case bfd_link_hash_warning:
7587 lh = lh->u.i.link;
7588 /* @@FIXME ignoring warning for now */
7589 goto lookup;
7590 case bfd_link_hash_new:
7591 default:
7592 abort ();
7595 else
7596 gp_found = 0;
7598 /* end mips */
7599 for (parent = reloc_vector; *parent != (arelent *) NULL;
7600 parent++)
7602 char *error_message = (char *) NULL;
7603 bfd_reloc_status_type r;
7605 /* Specific to MIPS: Deal with relocation types that require
7606 knowing the gp of the output bfd. */
7607 asymbol *sym = *(*parent)->sym_ptr_ptr;
7608 if (bfd_is_abs_section (sym->section) && abfd)
7610 /* The special_function wouldn't get called anyways. */
7612 else if (!gp_found)
7614 /* The gp isn't there; let the special function code
7615 fall over on its own. */
7617 else if ((*parent)->howto->special_function
7618 == _bfd_mips_elf_gprel16_reloc)
7620 /* bypass special_function call */
7621 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7622 relocateable, (PTR) data, gp);
7623 goto skip_bfd_perform_relocation;
7625 /* end mips specific stuff */
7627 r = bfd_perform_relocation (input_bfd,
7628 *parent,
7629 (PTR) data,
7630 input_section,
7631 relocateable ? abfd : (bfd *) NULL,
7632 &error_message);
7633 skip_bfd_perform_relocation:
7635 if (relocateable)
7637 asection *os = input_section->output_section;
7639 /* A partial link, so keep the relocs */
7640 os->orelocation[os->reloc_count] = *parent;
7641 os->reloc_count++;
7644 if (r != bfd_reloc_ok)
7646 switch (r)
7648 case bfd_reloc_undefined:
7649 if (!((*link_info->callbacks->undefined_symbol)
7650 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7651 input_bfd, input_section, (*parent)->address)))
7652 goto error_return;
7653 break;
7654 case bfd_reloc_dangerous:
7655 BFD_ASSERT (error_message != (char *) NULL);
7656 if (!((*link_info->callbacks->reloc_dangerous)
7657 (link_info, error_message, input_bfd, input_section,
7658 (*parent)->address)))
7659 goto error_return;
7660 break;
7661 case bfd_reloc_overflow:
7662 if (!((*link_info->callbacks->reloc_overflow)
7663 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7664 (*parent)->howto->name, (*parent)->addend,
7665 input_bfd, input_section, (*parent)->address)))
7666 goto error_return;
7667 break;
7668 case bfd_reloc_outofrange:
7669 default:
7670 abort ();
7671 break;
7677 if (reloc_vector != NULL)
7678 free (reloc_vector);
7679 return data;
7681 error_return:
7682 if (reloc_vector != NULL)
7683 free (reloc_vector);
7684 return NULL;
7686 #define bfd_elf32_bfd_get_relocated_section_contents \
7687 elf32_mips_get_relocated_section_contents
7689 /* ECOFF swapping routines. These are used when dealing with the
7690 .mdebug section, which is in the ECOFF debugging format. */
7691 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7693 /* Symbol table magic number. */
7694 magicSym,
7695 /* Alignment of debugging information. E.g., 4. */
7697 /* Sizes of external symbolic information. */
7698 sizeof (struct hdr_ext),
7699 sizeof (struct dnr_ext),
7700 sizeof (struct pdr_ext),
7701 sizeof (struct sym_ext),
7702 sizeof (struct opt_ext),
7703 sizeof (struct fdr_ext),
7704 sizeof (struct rfd_ext),
7705 sizeof (struct ext_ext),
7706 /* Functions to swap in external symbolic data. */
7707 ecoff_swap_hdr_in,
7708 ecoff_swap_dnr_in,
7709 ecoff_swap_pdr_in,
7710 ecoff_swap_sym_in,
7711 ecoff_swap_opt_in,
7712 ecoff_swap_fdr_in,
7713 ecoff_swap_rfd_in,
7714 ecoff_swap_ext_in,
7715 _bfd_ecoff_swap_tir_in,
7716 _bfd_ecoff_swap_rndx_in,
7717 /* Functions to swap out external symbolic data. */
7718 ecoff_swap_hdr_out,
7719 ecoff_swap_dnr_out,
7720 ecoff_swap_pdr_out,
7721 ecoff_swap_sym_out,
7722 ecoff_swap_opt_out,
7723 ecoff_swap_fdr_out,
7724 ecoff_swap_rfd_out,
7725 ecoff_swap_ext_out,
7726 _bfd_ecoff_swap_tir_out,
7727 _bfd_ecoff_swap_rndx_out,
7728 /* Function to read in symbolic data. */
7729 _bfd_mips_elf_read_ecoff_info
7732 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
7733 #define TARGET_LITTLE_NAME "elf32-littlemips"
7734 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
7735 #define TARGET_BIG_NAME "elf32-bigmips"
7736 #define ELF_ARCH bfd_arch_mips
7737 #define ELF_MACHINE_CODE EM_MIPS
7739 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7740 a value of 0x1000, and we are compatible. */
7741 #define ELF_MAXPAGESIZE 0x1000
7743 #define elf_backend_collect true
7744 #define elf_backend_type_change_ok true
7745 #define elf_backend_can_gc_sections true
7746 #define elf_info_to_howto 0
7747 #define elf_info_to_howto_rel mips_info_to_howto_rel
7748 #define elf_backend_sym_is_global mips_elf_sym_is_global
7749 #define elf_backend_object_p mips_elf32_object_p
7750 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
7751 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
7752 #define elf_backend_section_from_bfd_section \
7753 _bfd_mips_elf_section_from_bfd_section
7754 #define elf_backend_section_processing mips_elf32_section_processing
7755 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
7756 #define elf_backend_additional_program_headers \
7757 mips_elf_additional_program_headers
7758 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
7759 #define elf_backend_final_write_processing \
7760 _bfd_mips_elf_final_write_processing
7761 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
7763 #define bfd_elf32_bfd_is_local_label_name \
7764 mips_elf_is_local_label_name
7765 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
7766 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
7767 #define bfd_elf32_bfd_link_hash_table_create \
7768 mips_elf_link_hash_table_create
7769 #define bfd_elf32_bfd_final_link mips_elf_final_link
7770 #define bfd_elf32_bfd_copy_private_bfd_data \
7771 _bfd_mips_elf_copy_private_bfd_data
7772 #define bfd_elf32_bfd_merge_private_bfd_data \
7773 _bfd_mips_elf_merge_private_bfd_data
7774 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7775 #define bfd_elf32_bfd_print_private_bfd_data \
7776 _bfd_mips_elf_print_private_bfd_data
7777 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
7778 #define elf_backend_create_dynamic_sections \
7779 mips_elf_create_dynamic_sections
7780 #define elf_backend_check_relocs mips_elf_check_relocs
7781 #define elf_backend_adjust_dynamic_symbol \
7782 mips_elf_adjust_dynamic_symbol
7783 #define elf_backend_always_size_sections \
7784 mips_elf_always_size_sections
7785 #define elf_backend_size_dynamic_sections \
7786 mips_elf_size_dynamic_sections
7787 #define elf_backend_relocate_section mips_elf_relocate_section
7788 #define elf_backend_link_output_symbol_hook \
7789 mips_elf_link_output_symbol_hook
7790 #define elf_backend_finish_dynamic_symbol \
7791 mips_elf_finish_dynamic_symbol
7792 #define elf_backend_finish_dynamic_sections \
7793 mips_elf_finish_dynamic_sections
7794 #define elf_backend_gc_mark_hook mips_elf_gc_mark_hook
7795 #define elf_backend_gc_sweep_hook mips_elf_gc_sweep_hook
7797 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
7798 #define elf_backend_plt_header_size 0
7800 #include "elf32-target.h"