document behaviour of dot inside sections.
[binutils.git] / bfd / elf32-mips.c
blobb4de2e93f1d6bc6e0e9a5159b800e8b97c664e18
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 mips_info_to_howto_rela
51 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
52 static void bfd_mips_elf32_swap_gptab_in
53 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
54 static void bfd_mips_elf32_swap_gptab_out
55 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
56 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
57 static boolean mips_elf32_object_p PARAMS ((bfd *));
58 static boolean mips_elf_create_procedure_table
59 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
60 struct ecoff_debug_info *));
61 static int mips_elf_additional_program_headers PARAMS ((bfd *));
62 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
63 static INLINE int elf_mips_isa PARAMS ((flagword));
64 static INLINE int elf_mips_mach PARAMS ((flagword));
65 static INLINE char* elf_mips_abi_name PARAMS ((flagword));
66 static boolean mips_elf32_section_from_shdr
67 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
68 static boolean mips_elf32_section_processing
69 PARAMS ((bfd *, Elf32_Internal_Shdr *));
70 static boolean mips_elf_is_local_label_name
71 PARAMS ((bfd *, const char *));
72 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
73 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
74 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
75 PARAMS ((bfd *));
76 static int gptab_compare PARAMS ((const void *, const void *));
77 static boolean mips_elf_final_link
78 PARAMS ((bfd *, struct bfd_link_info *));
79 static void mips_elf_relocate_hi16
80 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
81 bfd_vma));
82 static boolean mips_elf_relocate_got_local
83 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
84 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
85 static void mips_elf_relocate_global_got
86 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
87 static bfd_reloc_status_type mips16_jump_reloc
88 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
89 static bfd_reloc_status_type mips16_gprel_reloc
90 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
91 static boolean mips_elf_adjust_dynindx
92 PARAMS ((struct elf_link_hash_entry *, PTR));
93 static boolean mips_elf_relocate_section
94 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
95 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
96 static boolean mips_elf_link_output_symbol_hook
97 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
98 asection *));
99 static boolean mips_elf_create_dynamic_sections
100 PARAMS ((bfd *, struct bfd_link_info *));
101 static boolean mips_elf_create_compact_rel_section
102 PARAMS ((bfd *, struct bfd_link_info *));
103 static boolean mips_elf_create_got_section
104 PARAMS ((bfd *, struct bfd_link_info *));
105 static boolean mips_elf_check_relocs
106 PARAMS ((bfd *, struct bfd_link_info *, asection *,
107 const Elf_Internal_Rela *));
108 static boolean mips_elf_adjust_dynamic_symbol
109 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
110 static boolean mips_elf_always_size_sections
111 PARAMS ((bfd *, struct bfd_link_info *));
112 static boolean mips_elf_size_dynamic_sections
113 PARAMS ((bfd *, struct bfd_link_info *));
114 static boolean mips_elf_finish_dynamic_symbol
115 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
116 Elf_Internal_Sym *));
117 static boolean mips_elf_finish_dynamic_sections
118 PARAMS ((bfd *, struct bfd_link_info *));
119 static boolean mips_elf_add_symbol_hook
120 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
121 const char **, flagword *, asection **, bfd_vma *));
122 static bfd_reloc_status_type mips_elf_final_gp
123 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
124 static bfd_byte *elf32_mips_get_relocated_section_contents
125 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
126 bfd_byte *, boolean, asymbol **));
128 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
129 executables. FIXME: At the moment, we default to always generating
130 Irix 5 executables. */
132 #define SGI_COMPAT(abfd) (1)
134 /* This structure is used to hold .got information when linking. It
135 is stored in the tdata field of the bfd_elf_section_data structure. */
137 struct mips_got_info
139 /* The symbol index of the first global .got symbol. */
140 unsigned long global_gotsym;
141 /* The number of local .got entries. */
142 unsigned int local_gotno;
143 /* The number of local .got entries we have used. */
144 unsigned int assigned_gotno;
147 /* The number of local .got entries we reserve. */
148 #define MIPS_RESERVED_GOTNO (2)
150 /* Instructions which appear in a stub. For some reason the stub is
151 slightly different on an SGI system. */
152 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
153 #define STUB_LW(abfd) \
154 (SGI_COMPAT (abfd) \
155 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
156 : 0x8f998000) /* lw t9,0x8000(gp) */
157 #define STUB_MOVE 0x03e07825 /* move t7,ra */
158 #define STUB_JALR 0x0320f809 /* jal t9 */
159 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
160 #define MIPS_FUNCTION_STUB_SIZE (16)
162 #if 0
163 /* We no longer try to identify particular sections for the .dynsym
164 section. When we do, we wind up crashing if there are other random
165 sections with relocations. */
167 /* Names of sections which appear in the .dynsym section in an Irix 5
168 executable. */
170 static const char * const mips_elf_dynsym_sec_names[] =
172 ".text",
173 ".init",
174 ".fini",
175 ".data",
176 ".rodata",
177 ".sdata",
178 ".sbss",
179 ".bss",
180 NULL
183 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
184 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
186 /* The number of entries in mips_elf_dynsym_sec_names which go in the
187 text segment. */
189 #define MIPS_TEXT_DYNSYM_SECNO (3)
191 #endif /* 0 */
193 /* The names of the runtime procedure table symbols used on Irix 5. */
195 static const char * const mips_elf_dynsym_rtproc_names[] =
197 "_procedure_table",
198 "_procedure_string_table",
199 "_procedure_table_size",
200 NULL
203 /* These structures are used to generate the .compact_rel section on
204 Irix 5. */
206 typedef struct
208 unsigned long id1; /* Always one? */
209 unsigned long num; /* Number of compact relocation entries. */
210 unsigned long id2; /* Always two? */
211 unsigned long offset; /* The file offset of the first relocation. */
212 unsigned long reserved0; /* Zero? */
213 unsigned long reserved1; /* Zero? */
214 } Elf32_compact_rel;
216 typedef struct
218 bfd_byte id1[4];
219 bfd_byte num[4];
220 bfd_byte id2[4];
221 bfd_byte offset[4];
222 bfd_byte reserved0[4];
223 bfd_byte reserved1[4];
224 } Elf32_External_compact_rel;
226 typedef struct
228 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
229 unsigned int rtype : 4; /* Relocation types. See below. */
230 unsigned int dist2to : 8;
231 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
232 unsigned long konst; /* KONST field. See below. */
233 unsigned long vaddr; /* VADDR to be relocated. */
234 } Elf32_crinfo;
236 typedef struct
238 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
239 unsigned int rtype : 4; /* Relocation types. See below. */
240 unsigned int dist2to : 8;
241 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
242 unsigned long konst; /* KONST field. See below. */
243 } Elf32_crinfo2;
245 typedef struct
247 bfd_byte info[4];
248 bfd_byte konst[4];
249 bfd_byte vaddr[4];
250 } Elf32_External_crinfo;
252 typedef struct
254 bfd_byte info[4];
255 bfd_byte konst[4];
256 } Elf32_External_crinfo2;
258 /* These are the constants used to swap the bitfields in a crinfo. */
260 #define CRINFO_CTYPE (0x1)
261 #define CRINFO_CTYPE_SH (31)
262 #define CRINFO_RTYPE (0xf)
263 #define CRINFO_RTYPE_SH (27)
264 #define CRINFO_DIST2TO (0xff)
265 #define CRINFO_DIST2TO_SH (19)
266 #define CRINFO_RELVADDR (0x7ffff)
267 #define CRINFO_RELVADDR_SH (0)
269 /* A compact relocation info has long (3 words) or short (2 words)
270 formats. A short format doesn't have VADDR field and relvaddr
271 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
272 #define CRF_MIPS_LONG 1
273 #define CRF_MIPS_SHORT 0
275 /* There are 4 types of compact relocation at least. The value KONST
276 has different meaning for each type:
278 (type) (konst)
279 CT_MIPS_REL32 Address in data
280 CT_MIPS_WORD Address in word (XXX)
281 CT_MIPS_GPHI_LO GP - vaddr
282 CT_MIPS_JMPAD Address to jump
285 #define CRT_MIPS_REL32 0xa
286 #define CRT_MIPS_WORD 0xb
287 #define CRT_MIPS_GPHI_LO 0xc
288 #define CRT_MIPS_JMPAD 0xd
290 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
291 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
292 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
293 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
295 static void bfd_elf32_swap_compact_rel_out
296 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
297 static void bfd_elf32_swap_crinfo_out
298 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
300 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
302 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
303 from smaller values. Start with zero, widen, *then* decrement. */
304 #define MINUS_ONE (((bfd_vma)0) - 1)
306 static reloc_howto_type elf_mips_howto_table[] =
308 /* No relocation. */
309 HOWTO (R_MIPS_NONE, /* type */
310 0, /* rightshift */
311 0, /* size (0 = byte, 1 = short, 2 = long) */
312 0, /* bitsize */
313 false, /* pc_relative */
314 0, /* bitpos */
315 complain_overflow_dont, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* special_function */
317 "R_MIPS_NONE", /* name */
318 false, /* partial_inplace */
319 0, /* src_mask */
320 0, /* dst_mask */
321 false), /* pcrel_offset */
323 /* 16 bit relocation. */
324 HOWTO (R_MIPS_16, /* type */
325 0, /* rightshift */
326 1, /* size (0 = byte, 1 = short, 2 = long) */
327 16, /* bitsize */
328 false, /* pc_relative */
329 0, /* bitpos */
330 complain_overflow_bitfield, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* special_function */
332 "R_MIPS_16", /* name */
333 true, /* partial_inplace */
334 0xffff, /* src_mask */
335 0xffff, /* dst_mask */
336 false), /* pcrel_offset */
338 /* 32 bit relocation. */
339 HOWTO (R_MIPS_32, /* type */
340 0, /* rightshift */
341 2, /* size (0 = byte, 1 = short, 2 = long) */
342 32, /* bitsize */
343 false, /* pc_relative */
344 0, /* bitpos */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 bfd_elf_generic_reloc, /* special_function */
347 "R_MIPS_32", /* name */
348 true, /* partial_inplace */
349 0xffffffff, /* src_mask */
350 0xffffffff, /* dst_mask */
351 false), /* pcrel_offset */
353 /* 32 bit symbol relative relocation. */
354 HOWTO (R_MIPS_REL32, /* type */
355 0, /* rightshift */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
357 32, /* bitsize */
358 false, /* pc_relative */
359 0, /* bitpos */
360 complain_overflow_bitfield, /* complain_on_overflow */
361 bfd_elf_generic_reloc, /* special_function */
362 "R_MIPS_REL32", /* name */
363 true, /* partial_inplace */
364 0xffffffff, /* src_mask */
365 0xffffffff, /* dst_mask */
366 false), /* pcrel_offset */
368 /* 26 bit branch address. */
369 HOWTO (R_MIPS_26, /* type */
370 2, /* rightshift */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
372 26, /* bitsize */
373 false, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_dont, /* complain_on_overflow */
376 /* This needs complex overflow
377 detection, because the upper four
378 bits must match the PC. */
379 bfd_elf_generic_reloc, /* special_function */
380 "R_MIPS_26", /* name */
381 true, /* partial_inplace */
382 0x3ffffff, /* src_mask */
383 0x3ffffff, /* dst_mask */
384 false), /* pcrel_offset */
386 /* High 16 bits of symbol value. */
387 HOWTO (R_MIPS_HI16, /* type */
388 0, /* rightshift */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
390 16, /* bitsize */
391 false, /* pc_relative */
392 0, /* bitpos */
393 complain_overflow_dont, /* complain_on_overflow */
394 _bfd_mips_elf_hi16_reloc, /* special_function */
395 "R_MIPS_HI16", /* name */
396 true, /* partial_inplace */
397 0xffff, /* src_mask */
398 0xffff, /* dst_mask */
399 false), /* pcrel_offset */
401 /* Low 16 bits of symbol value. */
402 HOWTO (R_MIPS_LO16, /* type */
403 0, /* rightshift */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
405 16, /* bitsize */
406 false, /* pc_relative */
407 0, /* bitpos */
408 complain_overflow_dont, /* complain_on_overflow */
409 _bfd_mips_elf_lo16_reloc, /* special_function */
410 "R_MIPS_LO16", /* name */
411 true, /* partial_inplace */
412 0xffff, /* src_mask */
413 0xffff, /* dst_mask */
414 false), /* pcrel_offset */
416 /* GP relative reference. */
417 HOWTO (R_MIPS_GPREL16, /* type */
418 0, /* rightshift */
419 2, /* size (0 = byte, 1 = short, 2 = long) */
420 16, /* bitsize */
421 false, /* pc_relative */
422 0, /* bitpos */
423 complain_overflow_signed, /* complain_on_overflow */
424 _bfd_mips_elf_gprel16_reloc, /* special_function */
425 "R_MIPS_GPREL16", /* name */
426 true, /* partial_inplace */
427 0xffff, /* src_mask */
428 0xffff, /* dst_mask */
429 false), /* pcrel_offset */
431 /* Reference to literal section. */
432 HOWTO (R_MIPS_LITERAL, /* type */
433 0, /* rightshift */
434 2, /* size (0 = byte, 1 = short, 2 = long) */
435 16, /* bitsize */
436 false, /* pc_relative */
437 0, /* bitpos */
438 complain_overflow_signed, /* complain_on_overflow */
439 _bfd_mips_elf_gprel16_reloc, /* special_function */
440 "R_MIPS_LITERAL", /* name */
441 true, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
444 false), /* pcrel_offset */
446 /* Reference to global offset table. */
447 HOWTO (R_MIPS_GOT16, /* type */
448 0, /* rightshift */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
450 16, /* bitsize */
451 false, /* pc_relative */
452 0, /* bitpos */
453 complain_overflow_signed, /* complain_on_overflow */
454 _bfd_mips_elf_got16_reloc, /* special_function */
455 "R_MIPS_GOT16", /* name */
456 false, /* partial_inplace */
457 0, /* src_mask */
458 0xffff, /* dst_mask */
459 false), /* pcrel_offset */
461 /* 16 bit PC relative reference. */
462 HOWTO (R_MIPS_PC16, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 16, /* bitsize */
466 true, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_signed, /* complain_on_overflow */
469 bfd_elf_generic_reloc, /* special_function */
470 "R_MIPS_PC16", /* name */
471 true, /* partial_inplace */
472 0xffff, /* src_mask */
473 0xffff, /* dst_mask */
474 false), /* pcrel_offset */
476 /* 16 bit call through global offset table. */
477 /* FIXME: This is not handled correctly. */
478 HOWTO (R_MIPS_CALL16, /* type */
479 0, /* rightshift */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
481 16, /* bitsize */
482 false, /* pc_relative */
483 0, /* bitpos */
484 complain_overflow_signed, /* complain_on_overflow */
485 bfd_elf_generic_reloc, /* special_function */
486 "R_MIPS_CALL16", /* name */
487 false, /* partial_inplace */
488 0, /* src_mask */
489 0xffff, /* dst_mask */
490 false), /* pcrel_offset */
492 /* 32 bit GP relative reference. */
493 HOWTO (R_MIPS_GPREL32, /* type */
494 0, /* rightshift */
495 2, /* size (0 = byte, 1 = short, 2 = long) */
496 32, /* bitsize */
497 false, /* pc_relative */
498 0, /* bitpos */
499 complain_overflow_bitfield, /* complain_on_overflow */
500 _bfd_mips_elf_gprel32_reloc, /* special_function */
501 "R_MIPS_GPREL32", /* name */
502 true, /* partial_inplace */
503 0xffffffff, /* src_mask */
504 0xffffffff, /* dst_mask */
505 false), /* pcrel_offset */
507 /* The remaining relocs are defined on Irix 5, although they are
508 not defined by the ABI. */
509 { 13 },
510 { 14 },
511 { 15 },
513 /* A 5 bit shift field. */
514 HOWTO (R_MIPS_SHIFT5, /* type */
515 0, /* rightshift */
516 2, /* size (0 = byte, 1 = short, 2 = long) */
517 5, /* bitsize */
518 false, /* pc_relative */
519 6, /* bitpos */
520 complain_overflow_bitfield, /* complain_on_overflow */
521 bfd_elf_generic_reloc, /* special_function */
522 "R_MIPS_SHIFT5", /* name */
523 true, /* partial_inplace */
524 0x000007c0, /* src_mask */
525 0x000007c0, /* dst_mask */
526 false), /* pcrel_offset */
528 /* A 6 bit shift field. */
529 /* FIXME: This is not handled correctly; a special function is
530 needed to put the most significant bit in the right place. */
531 HOWTO (R_MIPS_SHIFT6, /* type */
532 0, /* rightshift */
533 2, /* size (0 = byte, 1 = short, 2 = long) */
534 6, /* bitsize */
535 false, /* pc_relative */
536 6, /* bitpos */
537 complain_overflow_bitfield, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_MIPS_SHIFT6", /* name */
540 true, /* partial_inplace */
541 0x000007c4, /* src_mask */
542 0x000007c4, /* dst_mask */
543 false), /* pcrel_offset */
545 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
546 are 64 bits long; the upper 32 bits are simply a sign extension.
547 The fields of the howto should be the same as for R_MIPS_32,
548 other than the type, name, and special_function. */
549 HOWTO (R_MIPS_64, /* type */
550 0, /* rightshift */
551 2, /* size (0 = byte, 1 = short, 2 = long) */
552 32, /* bitsize */
553 false, /* pc_relative */
554 0, /* bitpos */
555 complain_overflow_bitfield, /* complain_on_overflow */
556 mips32_64bit_reloc, /* special_function */
557 "R_MIPS_64", /* name */
558 true, /* partial_inplace */
559 0xffffffff, /* src_mask */
560 0xffffffff, /* dst_mask */
561 false), /* pcrel_offset */
563 /* Displacement in the global offset table. */
564 /* FIXME: Not handled correctly. */
565 HOWTO (R_MIPS_GOT_DISP, /* type */
566 0, /* rightshift */
567 2, /* size (0 = byte, 1 = short, 2 = long) */
568 16, /* bitsize */
569 false, /* pc_relative */
570 0, /* bitpos */
571 complain_overflow_bitfield, /* complain_on_overflow */
572 bfd_elf_generic_reloc, /* special_function */
573 "R_MIPS_GOT_DISP", /* name */
574 true, /* partial_inplace */
575 0x0000ffff, /* src_mask */
576 0x0000ffff, /* dst_mask */
577 false), /* pcrel_offset */
579 /* Displacement to page pointer in the global offset table. */
580 /* FIXME: Not handled correctly. */
581 HOWTO (R_MIPS_GOT_PAGE, /* type */
582 0, /* rightshift */
583 2, /* size (0 = byte, 1 = short, 2 = long) */
584 16, /* bitsize */
585 false, /* pc_relative */
586 0, /* bitpos */
587 complain_overflow_bitfield, /* complain_on_overflow */
588 bfd_elf_generic_reloc, /* special_function */
589 "R_MIPS_GOT_PAGE", /* name */
590 true, /* partial_inplace */
591 0x0000ffff, /* src_mask */
592 0x0000ffff, /* dst_mask */
593 false), /* pcrel_offset */
595 /* Offset from page pointer in the global offset table. */
596 /* FIXME: Not handled correctly. */
597 HOWTO (R_MIPS_GOT_OFST, /* type */
598 0, /* rightshift */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
600 16, /* bitsize */
601 false, /* pc_relative */
602 0, /* bitpos */
603 complain_overflow_bitfield, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_MIPS_GOT_OFST", /* name */
606 true, /* partial_inplace */
607 0x0000ffff, /* src_mask */
608 0x0000ffff, /* dst_mask */
609 false), /* pcrel_offset */
611 /* High 16 bits of displacement in global offset table. */
612 /* FIXME: Not handled correctly. */
613 HOWTO (R_MIPS_GOT_HI16, /* type */
614 0, /* rightshift */
615 2, /* size (0 = byte, 1 = short, 2 = long) */
616 16, /* bitsize */
617 false, /* pc_relative */
618 0, /* bitpos */
619 complain_overflow_dont, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_MIPS_GOT_HI16", /* name */
622 true, /* partial_inplace */
623 0x0000ffff, /* src_mask */
624 0x0000ffff, /* dst_mask */
625 false), /* pcrel_offset */
627 /* Low 16 bits of displacement in global offset table. */
628 /* FIXME: Not handled correctly. */
629 HOWTO (R_MIPS_GOT_LO16, /* type */
630 0, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 16, /* bitsize */
633 false, /* pc_relative */
634 0, /* bitpos */
635 complain_overflow_dont, /* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_MIPS_GOT_LO16", /* name */
638 true, /* partial_inplace */
639 0x0000ffff, /* src_mask */
640 0x0000ffff, /* dst_mask */
641 false), /* pcrel_offset */
643 /* 64 bit subtraction. Used in the N32 ABI. */
644 /* FIXME: Not handled correctly. */
645 HOWTO (R_MIPS_SUB, /* type */
646 0, /* rightshift */
647 4, /* size (0 = byte, 1 = short, 2 = long) */
648 64, /* bitsize */
649 false, /* pc_relative */
650 0, /* bitpos */
651 complain_overflow_bitfield, /* complain_on_overflow */
652 bfd_elf_generic_reloc, /* special_function */
653 "R_MIPS_SUB", /* name */
654 true, /* partial_inplace */
655 MINUS_ONE, /* src_mask */
656 MINUS_ONE, /* dst_mask */
657 false), /* pcrel_offset */
659 /* Used to cause the linker to insert and delete instructions? */
660 { R_MIPS_INSERT_A },
661 { R_MIPS_INSERT_B },
662 { R_MIPS_DELETE },
664 /* Get the higher values of a 64 bit addend. Presumably not used in
665 32 bit ELF. */
666 { R_MIPS_HIGHER },
667 { R_MIPS_HIGHEST },
669 /* High 16 bits of displacement in global offset table. */
670 /* FIXME: Not handled correctly. */
671 HOWTO (R_MIPS_CALL_HI16, /* type */
672 0, /* rightshift */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
674 16, /* bitsize */
675 false, /* pc_relative */
676 0, /* bitpos */
677 complain_overflow_dont, /* complain_on_overflow */
678 bfd_elf_generic_reloc, /* special_function */
679 "R_MIPS_CALL_HI16", /* name */
680 true, /* partial_inplace */
681 0x0000ffff, /* src_mask */
682 0x0000ffff, /* dst_mask */
683 false), /* pcrel_offset */
685 /* Low 16 bits of displacement in global offset table. */
686 /* FIXME: Not handled correctly. */
687 HOWTO (R_MIPS_CALL_LO16, /* type */
688 0, /* rightshift */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
690 16, /* bitsize */
691 false, /* pc_relative */
692 0, /* bitpos */
693 complain_overflow_dont, /* complain_on_overflow */
694 bfd_elf_generic_reloc, /* special_function */
695 "R_MIPS_CALL_LO16", /* name */
696 true, /* partial_inplace */
697 0x0000ffff, /* src_mask */
698 0x0000ffff, /* dst_mask */
699 false), /* pcrel_offset */
701 { R_MIPS_SCN_DISP },
702 { R_MIPS_REL16 },
703 { R_MIPS_ADD_IMMEDIATE },
704 { R_MIPS_PJUMP },
705 { R_MIPS_RELGOT },
707 /* Protected jump conversion. This is an optimization hint. No
708 relocation is required for correctness. */
709 HOWTO (R_MIPS_JALR, /* type */
710 0, /* rightshift */
711 0, /* size (0 = byte, 1 = short, 2 = long) */
712 0, /* bitsize */
713 false, /* pc_relative */
714 0, /* bitpos */
715 complain_overflow_dont, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* special_function */
717 "R_MIPS_JALR", /* name */
718 false, /* partial_inplace */
719 0x00000000, /* src_mask */
720 0x00000000, /* dst_mask */
721 false), /* pcrel_offset */
724 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
725 is a hack to make the linker think that we need 64 bit values. */
726 static reloc_howto_type elf_mips_ctor64_howto =
727 HOWTO (R_MIPS_64, /* type */
728 0, /* rightshift */
729 4, /* size (0 = byte, 1 = short, 2 = long) */
730 32, /* bitsize */
731 false, /* pc_relative */
732 0, /* bitpos */
733 complain_overflow_signed, /* complain_on_overflow */
734 mips32_64bit_reloc, /* special_function */
735 "R_MIPS_64", /* name */
736 true, /* partial_inplace */
737 0xffffffff, /* src_mask */
738 0xffffffff, /* dst_mask */
739 false); /* pcrel_offset */
741 /* The reloc used for the mips16 jump instruction. */
742 static reloc_howto_type elf_mips16_jump_howto =
743 HOWTO (R_MIPS16_26, /* type */
744 2, /* rightshift */
745 2, /* size (0 = byte, 1 = short, 2 = long) */
746 26, /* bitsize */
747 false, /* pc_relative */
748 0, /* bitpos */
749 complain_overflow_dont, /* complain_on_overflow */
750 /* This needs complex overflow
751 detection, because the upper four
752 bits must match the PC. */
753 mips16_jump_reloc, /* special_function */
754 "R_MIPS16_26", /* name */
755 true, /* partial_inplace */
756 0x3ffffff, /* src_mask */
757 0x3ffffff, /* dst_mask */
758 false); /* pcrel_offset */
760 /* The reloc used for the mips16 gprel instruction. The src_mask and
761 dsk_mask for this howto do not reflect the actual instruction, in
762 which the value is not contiguous; the masks are for the
763 convenience of the relocate_section routine. */
764 static reloc_howto_type elf_mips16_gprel_howto =
765 HOWTO (R_MIPS16_GPREL, /* type */
766 0, /* rightshift */
767 2, /* size (0 = byte, 1 = short, 2 = long) */
768 16, /* bitsize */
769 false, /* pc_relative */
770 0, /* bitpos */
771 complain_overflow_signed, /* complain_on_overflow */
772 mips16_gprel_reloc, /* special_function */
773 "R_MIPS16_GPREL", /* name */
774 true, /* partial_inplace */
775 0xffff, /* src_mask */
776 0xffff, /* dst_mask */
777 false); /* pcrel_offset */
780 /* GNU extension to record C++ vtable hierarchy */
781 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
782 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
783 0, /* rightshift */
784 2, /* size (0 = byte, 1 = short, 2 = long) */
785 0, /* bitsize */
786 false, /* pc_relative */
787 0, /* bitpos */
788 complain_overflow_dont, /* complain_on_overflow */
789 NULL, /* special_function */
790 "R_MIPS_GNU_VTINHERIT", /* name */
791 false, /* partial_inplace */
792 0, /* src_mask */
793 0, /* dst_mask */
794 false); /* pcrel_offset */
796 /* GNU extension to record C++ vtable member usage */
797 static reloc_howto_type elf_mips_gnu_vtentry_howto =
798 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
799 0, /* rightshift */
800 2, /* size (0 = byte, 1 = short, 2 = long) */
801 0, /* bitsize */
802 false, /* pc_relative */
803 0, /* bitpos */
804 complain_overflow_dont, /* complain_on_overflow */
805 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
806 "R_MIPS_GNU_VTENTRY", /* name */
807 false, /* partial_inplace */
808 0, /* src_mask */
809 0, /* dst_mask */
810 false); /* pcrel_offset */
812 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
813 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
814 the HI16. Here we just save the information we need; we do the
815 actual relocation when we see the LO16. MIPS ELF requires that the
816 LO16 immediately follow the HI16. As a GNU extension, we permit an
817 arbitrary number of HI16 relocs to be associated with a single LO16
818 reloc. This extension permits gcc to output the HI and LO relocs
819 itself. */
821 struct mips_hi16
823 struct mips_hi16 *next;
824 bfd_byte *addr;
825 bfd_vma addend;
828 /* FIXME: This should not be a static variable. */
830 static struct mips_hi16 *mips_hi16_list;
832 bfd_reloc_status_type
833 _bfd_mips_elf_hi16_reloc (abfd,
834 reloc_entry,
835 symbol,
836 data,
837 input_section,
838 output_bfd,
839 error_message)
840 bfd *abfd;
841 arelent *reloc_entry;
842 asymbol *symbol;
843 PTR data;
844 asection *input_section;
845 bfd *output_bfd;
846 char **error_message;
848 bfd_reloc_status_type ret;
849 bfd_vma relocation;
850 struct mips_hi16 *n;
852 /* If we're relocating, and this an external symbol, we don't want
853 to change anything. */
854 if (output_bfd != (bfd *) NULL
855 && (symbol->flags & BSF_SECTION_SYM) == 0
856 && reloc_entry->addend == 0)
858 reloc_entry->address += input_section->output_offset;
859 return bfd_reloc_ok;
862 ret = bfd_reloc_ok;
864 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
866 boolean relocateable;
867 bfd_vma gp;
869 if (ret == bfd_reloc_undefined)
870 abort ();
872 if (output_bfd != NULL)
873 relocateable = true;
874 else
876 relocateable = false;
877 output_bfd = symbol->section->output_section->owner;
880 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
881 error_message, &gp);
882 if (ret != bfd_reloc_ok)
883 return ret;
885 relocation = gp - reloc_entry->address;
887 else
889 if (bfd_is_und_section (symbol->section)
890 && output_bfd == (bfd *) NULL)
891 ret = bfd_reloc_undefined;
893 if (bfd_is_com_section (symbol->section))
894 relocation = 0;
895 else
896 relocation = symbol->value;
899 relocation += symbol->section->output_section->vma;
900 relocation += symbol->section->output_offset;
901 relocation += reloc_entry->addend;
903 if (reloc_entry->address > input_section->_cooked_size)
904 return bfd_reloc_outofrange;
906 /* Save the information, and let LO16 do the actual relocation. */
907 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
908 if (n == NULL)
909 return bfd_reloc_outofrange;
910 n->addr = (bfd_byte *) data + reloc_entry->address;
911 n->addend = relocation;
912 n->next = mips_hi16_list;
913 mips_hi16_list = n;
915 if (output_bfd != (bfd *) NULL)
916 reloc_entry->address += input_section->output_offset;
918 return ret;
921 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
922 inplace relocation; this function exists in order to do the
923 R_MIPS_HI16 relocation described above. */
925 bfd_reloc_status_type
926 _bfd_mips_elf_lo16_reloc (abfd,
927 reloc_entry,
928 symbol,
929 data,
930 input_section,
931 output_bfd,
932 error_message)
933 bfd *abfd;
934 arelent *reloc_entry;
935 asymbol *symbol;
936 PTR data;
937 asection *input_section;
938 bfd *output_bfd;
939 char **error_message;
941 arelent gp_disp_relent;
943 if (mips_hi16_list != NULL)
945 struct mips_hi16 *l;
947 l = mips_hi16_list;
948 while (l != NULL)
950 unsigned long insn;
951 unsigned long val;
952 unsigned long vallo;
953 struct mips_hi16 *next;
955 /* Do the HI16 relocation. Note that we actually don't need
956 to know anything about the LO16 itself, except where to
957 find the low 16 bits of the addend needed by the LO16. */
958 insn = bfd_get_32 (abfd, l->addr);
959 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
960 & 0xffff);
961 val = ((insn & 0xffff) << 16) + vallo;
962 val += l->addend;
964 /* The low order 16 bits are always treated as a signed
965 value. Therefore, a negative value in the low order bits
966 requires an adjustment in the high order bits. We need
967 to make this adjustment in two ways: once for the bits we
968 took from the data, and once for the bits we are putting
969 back in to the data. */
970 if ((vallo & 0x8000) != 0)
971 val -= 0x10000;
972 if ((val & 0x8000) != 0)
973 val += 0x10000;
975 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
976 bfd_put_32 (abfd, insn, l->addr);
978 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
980 gp_disp_relent = *reloc_entry;
981 reloc_entry = &gp_disp_relent;
982 reloc_entry->addend = l->addend;
985 next = l->next;
986 free (l);
987 l = next;
990 mips_hi16_list = NULL;
992 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
994 bfd_reloc_status_type ret;
995 bfd_vma gp, relocation;
997 /* FIXME: Does this case ever occur? */
999 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1000 if (ret != bfd_reloc_ok)
1001 return ret;
1003 relocation = gp - reloc_entry->address;
1004 relocation += symbol->section->output_section->vma;
1005 relocation += symbol->section->output_offset;
1006 relocation += reloc_entry->addend;
1008 if (reloc_entry->address > input_section->_cooked_size)
1009 return bfd_reloc_outofrange;
1011 gp_disp_relent = *reloc_entry;
1012 reloc_entry = &gp_disp_relent;
1013 reloc_entry->addend = relocation - 4;
1016 /* Now do the LO16 reloc in the usual way. */
1017 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1018 input_section, output_bfd, error_message);
1021 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1022 table used for PIC code. If the symbol is an external symbol, the
1023 instruction is modified to contain the offset of the appropriate
1024 entry in the global offset table. If the symbol is a section
1025 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1026 addends are combined to form the real addend against the section
1027 symbol; the GOT16 is modified to contain the offset of an entry in
1028 the global offset table, and the LO16 is modified to offset it
1029 appropriately. Thus an offset larger than 16 bits requires a
1030 modified value in the global offset table.
1032 This implementation suffices for the assembler, but the linker does
1033 not yet know how to create global offset tables. */
1035 bfd_reloc_status_type
1036 _bfd_mips_elf_got16_reloc (abfd,
1037 reloc_entry,
1038 symbol,
1039 data,
1040 input_section,
1041 output_bfd,
1042 error_message)
1043 bfd *abfd;
1044 arelent *reloc_entry;
1045 asymbol *symbol;
1046 PTR data;
1047 asection *input_section;
1048 bfd *output_bfd;
1049 char **error_message;
1051 /* If we're relocating, and this an external symbol, we don't want
1052 to change anything. */
1053 if (output_bfd != (bfd *) NULL
1054 && (symbol->flags & BSF_SECTION_SYM) == 0
1055 && reloc_entry->addend == 0)
1057 reloc_entry->address += input_section->output_offset;
1058 return bfd_reloc_ok;
1061 /* If we're relocating, and this is a local symbol, we can handle it
1062 just like HI16. */
1063 if (output_bfd != (bfd *) NULL
1064 && (symbol->flags & BSF_SECTION_SYM) != 0)
1065 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1066 input_section, output_bfd, error_message);
1068 abort ();
1071 /* We have to figure out the gp value, so that we can adjust the
1072 symbol value correctly. We look up the symbol _gp in the output
1073 BFD. If we can't find it, we're stuck. We cache it in the ELF
1074 target data. We don't need to adjust the symbol value for an
1075 external symbol if we are producing relocateable output. */
1077 static bfd_reloc_status_type
1078 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1079 bfd *output_bfd;
1080 asymbol *symbol;
1081 boolean relocateable;
1082 char **error_message;
1083 bfd_vma *pgp;
1085 if (bfd_is_und_section (symbol->section)
1086 && ! relocateable)
1088 *pgp = 0;
1089 return bfd_reloc_undefined;
1092 *pgp = _bfd_get_gp_value (output_bfd);
1093 if (*pgp == 0
1094 && (! relocateable
1095 || (symbol->flags & BSF_SECTION_SYM) != 0))
1097 if (relocateable)
1099 /* Make up a value. */
1100 *pgp = symbol->section->output_section->vma + 0x4000;
1101 _bfd_set_gp_value (output_bfd, *pgp);
1103 else
1105 unsigned int count;
1106 asymbol **sym;
1107 unsigned int i;
1109 count = bfd_get_symcount (output_bfd);
1110 sym = bfd_get_outsymbols (output_bfd);
1112 if (sym == (asymbol **) NULL)
1113 i = count;
1114 else
1116 for (i = 0; i < count; i++, sym++)
1118 register CONST char *name;
1120 name = bfd_asymbol_name (*sym);
1121 if (*name == '_' && strcmp (name, "_gp") == 0)
1123 *pgp = bfd_asymbol_value (*sym);
1124 _bfd_set_gp_value (output_bfd, *pgp);
1125 break;
1130 if (i >= count)
1132 /* Only get the error once. */
1133 *pgp = 4;
1134 _bfd_set_gp_value (output_bfd, *pgp);
1135 *error_message =
1136 (char *) _("GP relative relocation when _gp not defined");
1137 return bfd_reloc_dangerous;
1142 return bfd_reloc_ok;
1145 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1146 become the offset from the gp register. This function also handles
1147 R_MIPS_LITERAL relocations, although those can be handled more
1148 cleverly because the entries in the .lit8 and .lit4 sections can be
1149 merged. */
1151 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1152 arelent *, asection *,
1153 boolean, PTR, bfd_vma));
1155 bfd_reloc_status_type
1156 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1157 output_bfd, error_message)
1158 bfd *abfd;
1159 arelent *reloc_entry;
1160 asymbol *symbol;
1161 PTR data;
1162 asection *input_section;
1163 bfd *output_bfd;
1164 char **error_message;
1166 boolean relocateable;
1167 bfd_reloc_status_type ret;
1168 bfd_vma gp;
1170 /* If we're relocating, and this is an external symbol with no
1171 addend, we don't want to change anything. We will only have an
1172 addend if this is a newly created reloc, not read from an ELF
1173 file. */
1174 if (output_bfd != (bfd *) NULL
1175 && (symbol->flags & BSF_SECTION_SYM) == 0
1176 && reloc_entry->addend == 0)
1178 reloc_entry->address += input_section->output_offset;
1179 return bfd_reloc_ok;
1182 if (output_bfd != (bfd *) NULL)
1183 relocateable = true;
1184 else
1186 relocateable = false;
1187 output_bfd = symbol->section->output_section->owner;
1190 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1191 &gp);
1192 if (ret != bfd_reloc_ok)
1193 return ret;
1195 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1196 relocateable, data, gp);
1199 static bfd_reloc_status_type
1200 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1202 bfd *abfd;
1203 asymbol *symbol;
1204 arelent *reloc_entry;
1205 asection *input_section;
1206 boolean relocateable;
1207 PTR data;
1208 bfd_vma gp;
1210 bfd_vma relocation;
1211 unsigned long insn;
1212 unsigned long val;
1214 if (bfd_is_com_section (symbol->section))
1215 relocation = 0;
1216 else
1217 relocation = symbol->value;
1219 relocation += symbol->section->output_section->vma;
1220 relocation += symbol->section->output_offset;
1222 if (reloc_entry->address > input_section->_cooked_size)
1223 return bfd_reloc_outofrange;
1225 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1227 /* Set val to the offset into the section or symbol. */
1228 if (reloc_entry->howto->src_mask == 0)
1230 /* This case occurs with the 64-bit MIPS ELF ABI. */
1231 val = reloc_entry->addend;
1233 else
1235 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1236 if (val & 0x8000)
1237 val -= 0x10000;
1240 /* Adjust val for the final section location and GP value. If we
1241 are producing relocateable output, we don't want to do this for
1242 an external symbol. */
1243 if (! relocateable
1244 || (symbol->flags & BSF_SECTION_SYM) != 0)
1245 val += relocation - gp;
1247 insn = (insn &~ 0xffff) | (val & 0xffff);
1248 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1250 if (relocateable)
1251 reloc_entry->address += input_section->output_offset;
1253 /* Make sure it fit in 16 bits. */
1254 if (val >= 0x8000 && val < 0xffff8000)
1255 return bfd_reloc_overflow;
1257 return bfd_reloc_ok;
1260 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1261 from the gp register? XXX */
1263 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1264 arelent *, asection *,
1265 boolean, PTR, bfd_vma));
1267 bfd_reloc_status_type
1268 _bfd_mips_elf_gprel32_reloc (abfd,
1269 reloc_entry,
1270 symbol,
1271 data,
1272 input_section,
1273 output_bfd,
1274 error_message)
1275 bfd *abfd;
1276 arelent *reloc_entry;
1277 asymbol *symbol;
1278 PTR data;
1279 asection *input_section;
1280 bfd *output_bfd;
1281 char **error_message;
1283 boolean relocateable;
1284 bfd_reloc_status_type ret;
1285 bfd_vma gp;
1287 /* If we're relocating, and this is an external symbol with no
1288 addend, we don't want to change anything. We will only have an
1289 addend if this is a newly created reloc, not read from an ELF
1290 file. */
1291 if (output_bfd != (bfd *) NULL
1292 && (symbol->flags & BSF_SECTION_SYM) == 0
1293 && reloc_entry->addend == 0)
1295 *error_message = (char *)
1296 _("32bits gp relative relocation occurs for an external symbol");
1297 return bfd_reloc_outofrange;
1300 if (output_bfd != (bfd *) NULL)
1302 relocateable = true;
1303 gp = _bfd_get_gp_value (output_bfd);
1305 else
1307 relocateable = false;
1308 output_bfd = symbol->section->output_section->owner;
1310 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1311 error_message, &gp);
1312 if (ret != bfd_reloc_ok)
1313 return ret;
1316 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1317 relocateable, data, gp);
1320 static bfd_reloc_status_type
1321 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1323 bfd *abfd;
1324 asymbol *symbol;
1325 arelent *reloc_entry;
1326 asection *input_section;
1327 boolean relocateable;
1328 PTR data;
1329 bfd_vma gp;
1331 bfd_vma relocation;
1332 unsigned long val;
1334 if (bfd_is_com_section (symbol->section))
1335 relocation = 0;
1336 else
1337 relocation = symbol->value;
1339 relocation += symbol->section->output_section->vma;
1340 relocation += symbol->section->output_offset;
1342 if (reloc_entry->address > input_section->_cooked_size)
1343 return bfd_reloc_outofrange;
1345 if (reloc_entry->howto->src_mask == 0)
1347 /* This case arises with the 64-bit MIPS ELF ABI. */
1348 val = 0;
1350 else
1351 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1353 /* Set val to the offset into the section or symbol. */
1354 val += reloc_entry->addend;
1356 /* Adjust val for the final section location and GP value. If we
1357 are producing relocateable output, we don't want to do this for
1358 an external symbol. */
1359 if (! relocateable
1360 || (symbol->flags & BSF_SECTION_SYM) != 0)
1361 val += relocation - gp;
1363 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1365 if (relocateable)
1366 reloc_entry->address += input_section->output_offset;
1368 return bfd_reloc_ok;
1371 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1372 generated when addreses are 64 bits. The upper 32 bits are a simle
1373 sign extension. */
1375 static bfd_reloc_status_type
1376 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1377 output_bfd, error_message)
1378 bfd *abfd;
1379 arelent *reloc_entry;
1380 asymbol *symbol;
1381 PTR data;
1382 asection *input_section;
1383 bfd *output_bfd;
1384 char **error_message;
1386 bfd_reloc_status_type r;
1387 arelent reloc32;
1388 unsigned long val;
1389 bfd_size_type addr;
1391 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1392 input_section, output_bfd, error_message);
1393 if (r != bfd_reloc_continue)
1394 return r;
1396 /* Do a normal 32 bit relocation on the lower 32 bits. */
1397 reloc32 = *reloc_entry;
1398 if (bfd_big_endian (abfd))
1399 reloc32.address += 4;
1400 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1401 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1402 output_bfd, error_message);
1404 /* Sign extend into the upper 32 bits. */
1405 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1406 if ((val & 0x80000000) != 0)
1407 val = 0xffffffff;
1408 else
1409 val = 0;
1410 addr = reloc_entry->address;
1411 if (bfd_little_endian (abfd))
1412 addr += 4;
1413 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1415 return r;
1418 /* Handle a mips16 jump. */
1420 static bfd_reloc_status_type
1421 mips16_jump_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 if (output_bfd != (bfd *) NULL
1432 && (symbol->flags & BSF_SECTION_SYM) == 0
1433 && reloc_entry->addend == 0)
1435 reloc_entry->address += input_section->output_offset;
1436 return bfd_reloc_ok;
1439 /* FIXME. */
1441 static boolean warned;
1443 if (! warned)
1444 (*_bfd_error_handler)
1445 (_("Linking mips16 objects into %s format is not supported"),
1446 bfd_get_target (input_section->output_section->owner));
1447 warned = true;
1450 return bfd_reloc_undefined;
1453 /* Handle a mips16 GP relative reloc. */
1455 static bfd_reloc_status_type
1456 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1457 output_bfd, error_message)
1458 bfd *abfd;
1459 arelent *reloc_entry;
1460 asymbol *symbol;
1461 PTR data;
1462 asection *input_section;
1463 bfd *output_bfd;
1464 char **error_message;
1466 boolean relocateable;
1467 bfd_reloc_status_type ret;
1468 bfd_vma gp;
1469 unsigned short extend, insn;
1470 unsigned long final;
1472 /* If we're relocating, and this is an external symbol with no
1473 addend, we don't want to change anything. We will only have an
1474 addend if this is a newly created reloc, not read from an ELF
1475 file. */
1476 if (output_bfd != NULL
1477 && (symbol->flags & BSF_SECTION_SYM) == 0
1478 && reloc_entry->addend == 0)
1480 reloc_entry->address += input_section->output_offset;
1481 return bfd_reloc_ok;
1484 if (output_bfd != NULL)
1485 relocateable = true;
1486 else
1488 relocateable = false;
1489 output_bfd = symbol->section->output_section->owner;
1492 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1493 &gp);
1494 if (ret != bfd_reloc_ok)
1495 return ret;
1497 if (reloc_entry->address > input_section->_cooked_size)
1498 return bfd_reloc_outofrange;
1500 /* Pick up the mips16 extend instruction and the real instruction. */
1501 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1502 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1504 /* Stuff the current addend back as a 32 bit value, do the usual
1505 relocation, and then clean up. */
1506 bfd_put_32 (abfd,
1507 (((extend & 0x1f) << 11)
1508 | (extend & 0x7e0)
1509 | (insn & 0x1f)),
1510 (bfd_byte *) data + reloc_entry->address);
1512 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1513 relocateable, data, gp);
1515 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1516 bfd_put_16 (abfd,
1517 ((extend & 0xf800)
1518 | ((final >> 11) & 0x1f)
1519 | (final & 0x7e0)),
1520 (bfd_byte *) data + reloc_entry->address);
1521 bfd_put_16 (abfd,
1522 ((insn & 0xffe0)
1523 | (final & 0x1f)),
1524 (bfd_byte *) data + reloc_entry->address + 2);
1526 return ret;
1529 /* Return the ISA for a MIPS e_flags value. */
1531 static INLINE int
1532 elf_mips_isa (flags)
1533 flagword flags;
1535 switch (flags & EF_MIPS_ARCH)
1537 case E_MIPS_ARCH_1:
1538 return 1;
1539 case E_MIPS_ARCH_2:
1540 return 2;
1541 case E_MIPS_ARCH_3:
1542 return 3;
1543 case E_MIPS_ARCH_4:
1544 return 4;
1546 return 4;
1549 /* Return the MACH for a MIPS e_flags value. */
1551 static INLINE int
1552 elf_mips_mach (flags)
1553 flagword flags;
1555 switch (flags & EF_MIPS_MACH)
1557 case E_MIPS_MACH_3900:
1558 return bfd_mach_mips3900;
1560 case E_MIPS_MACH_4010:
1561 return bfd_mach_mips4010;
1563 case E_MIPS_MACH_4100:
1564 return bfd_mach_mips4100;
1566 case E_MIPS_MACH_4111:
1567 return bfd_mach_mips4111;
1569 case E_MIPS_MACH_4650:
1570 return bfd_mach_mips4650;
1572 default:
1573 switch (flags & EF_MIPS_ARCH)
1575 default:
1576 case E_MIPS_ARCH_1:
1577 return bfd_mach_mips3000;
1578 break;
1580 case E_MIPS_ARCH_2:
1581 return bfd_mach_mips6000;
1582 break;
1584 case E_MIPS_ARCH_3:
1585 return bfd_mach_mips4000;
1586 break;
1588 case E_MIPS_ARCH_4:
1589 return bfd_mach_mips8000;
1590 break;
1594 return 0;
1597 /* Return printable name for ABI from flagword. */
1599 static INLINE char*
1600 elf_mips_abi_name (flags)
1601 flagword flags;
1603 switch (flags & EF_MIPS_ABI)
1605 case 0:
1606 return "none";
1607 case E_MIPS_ABI_O32:
1608 return "O32";
1609 case E_MIPS_ABI_O64:
1610 return "O64";
1611 case E_MIPS_ABI_EABI32:
1612 return "EABI32";
1613 case E_MIPS_ABI_EABI64:
1614 return "EABI64";
1615 default:
1616 return "unknown abi";
1620 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1622 struct elf_reloc_map {
1623 bfd_reloc_code_real_type bfd_reloc_val;
1624 enum elf_mips_reloc_type elf_reloc_val;
1627 static CONST struct elf_reloc_map mips_reloc_map[] =
1629 { BFD_RELOC_NONE, R_MIPS_NONE, },
1630 { BFD_RELOC_16, R_MIPS_16 },
1631 { BFD_RELOC_32, R_MIPS_32 },
1632 { BFD_RELOC_64, R_MIPS_64 },
1633 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1634 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1635 { BFD_RELOC_LO16, R_MIPS_LO16 },
1636 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1637 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1638 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1639 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1640 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1641 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1642 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1643 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1644 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1645 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1646 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1647 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1648 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1649 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1652 /* Given a BFD reloc type, return a howto structure. */
1654 static reloc_howto_type *
1655 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1656 bfd *abfd;
1657 bfd_reloc_code_real_type code;
1659 unsigned int i;
1661 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1663 if (mips_reloc_map[i].bfd_reloc_val == code)
1664 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1667 switch (code)
1669 default:
1670 bfd_set_error (bfd_error_bad_value);
1671 return NULL;
1673 case BFD_RELOC_CTOR:
1674 /* We need to handle BFD_RELOC_CTOR specially.
1675 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1676 size of addresses on this architecture. */
1677 if (bfd_arch_bits_per_address (abfd) == 32)
1678 return &elf_mips_howto_table[(int) R_MIPS_32];
1679 else
1680 return &elf_mips_ctor64_howto;
1682 case BFD_RELOC_MIPS16_JMP:
1683 return &elf_mips16_jump_howto;
1684 case BFD_RELOC_MIPS16_GPREL:
1685 return &elf_mips16_gprel_howto;
1686 case BFD_RELOC_VTABLE_INHERIT:
1687 return &elf_mips_gnu_vtinherit_howto;
1688 case BFD_RELOC_VTABLE_ENTRY:
1689 return &elf_mips_gnu_vtentry_howto;
1693 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1695 static void
1696 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1697 bfd *abfd;
1698 arelent *cache_ptr;
1699 Elf32_Internal_Rel *dst;
1701 unsigned int r_type;
1703 r_type = ELF32_R_TYPE (dst->r_info);
1704 switch (r_type)
1706 case R_MIPS16_26:
1707 cache_ptr->howto = &elf_mips16_jump_howto;
1708 break;
1709 case R_MIPS16_GPREL:
1710 cache_ptr->howto = &elf_mips16_gprel_howto;
1711 break;
1712 case R_MIPS_GNU_VTINHERIT:
1713 cache_ptr->howto = &elf_mips_gnu_vtinherit_howto;
1714 break;
1715 case R_MIPS_GNU_VTENTRY:
1716 cache_ptr->howto = &elf_mips_gnu_vtentry_howto;
1717 break;
1719 default:
1720 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1721 cache_ptr->howto = &elf_mips_howto_table[r_type];
1722 break;
1725 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1726 value for the object file. We get the addend now, rather than
1727 when we do the relocation, because the symbol manipulations done
1728 by the linker may cause us to lose track of the input BFD. */
1729 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1730 && (r_type == (unsigned int) R_MIPS_GPREL16
1731 || r_type == (unsigned int) R_MIPS_LITERAL))
1732 cache_ptr->addend = elf_gp (abfd);
1735 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1737 static void
1738 mips_info_to_howto_rela (abfd, cache_ptr, dst)
1739 bfd *abfd;
1740 arelent *cache_ptr;
1741 Elf32_Internal_Rela *dst;
1743 /* Since an Elf32_Internal_Rel is an initial prefix of an
1744 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1745 above. */
1746 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1748 /* If we ever need to do any extra processing with dst->r_addend
1749 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1752 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1753 routines swap this structure in and out. They are used outside of
1754 BFD, so they are globally visible. */
1756 void
1757 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1758 bfd *abfd;
1759 const Elf32_External_RegInfo *ex;
1760 Elf32_RegInfo *in;
1762 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1763 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1764 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1765 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1766 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1767 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1770 void
1771 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1772 bfd *abfd;
1773 const Elf32_RegInfo *in;
1774 Elf32_External_RegInfo *ex;
1776 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1777 (bfd_byte *) ex->ri_gprmask);
1778 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1779 (bfd_byte *) ex->ri_cprmask[0]);
1780 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1781 (bfd_byte *) ex->ri_cprmask[1]);
1782 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1783 (bfd_byte *) ex->ri_cprmask[2]);
1784 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1785 (bfd_byte *) ex->ri_cprmask[3]);
1786 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1787 (bfd_byte *) ex->ri_gp_value);
1790 /* In the 64 bit ABI, the .MIPS.options section holds register
1791 information in an Elf64_Reginfo structure. These routines swap
1792 them in and out. They are globally visible because they are used
1793 outside of BFD. These routines are here so that gas can call them
1794 without worrying about whether the 64 bit ABI has been included. */
1796 void
1797 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1798 bfd *abfd;
1799 const Elf64_External_RegInfo *ex;
1800 Elf64_Internal_RegInfo *in;
1802 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1803 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1804 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1805 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1806 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1807 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1808 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1811 void
1812 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1813 bfd *abfd;
1814 const Elf64_Internal_RegInfo *in;
1815 Elf64_External_RegInfo *ex;
1817 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1818 (bfd_byte *) ex->ri_gprmask);
1819 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1820 (bfd_byte *) ex->ri_pad);
1821 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1822 (bfd_byte *) ex->ri_cprmask[0]);
1823 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1824 (bfd_byte *) ex->ri_cprmask[1]);
1825 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1826 (bfd_byte *) ex->ri_cprmask[2]);
1827 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1828 (bfd_byte *) ex->ri_cprmask[3]);
1829 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1830 (bfd_byte *) ex->ri_gp_value);
1833 /* Swap an entry in a .gptab section. Note that these routines rely
1834 on the equivalence of the two elements of the union. */
1836 static void
1837 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1838 bfd *abfd;
1839 const Elf32_External_gptab *ex;
1840 Elf32_gptab *in;
1842 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1843 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1846 static void
1847 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1848 bfd *abfd;
1849 const Elf32_gptab *in;
1850 Elf32_External_gptab *ex;
1852 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1853 ex->gt_entry.gt_g_value);
1854 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1855 ex->gt_entry.gt_bytes);
1858 static void
1859 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1860 bfd *abfd;
1861 const Elf32_compact_rel *in;
1862 Elf32_External_compact_rel *ex;
1864 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1865 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1866 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1867 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1868 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1869 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1872 static void
1873 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1874 bfd *abfd;
1875 const Elf32_crinfo *in;
1876 Elf32_External_crinfo *ex;
1878 unsigned long l;
1880 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1881 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1882 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1883 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1884 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1885 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1886 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1889 /* Swap in an options header. */
1891 void
1892 bfd_mips_elf_swap_options_in (abfd, ex, in)
1893 bfd *abfd;
1894 const Elf_External_Options *ex;
1895 Elf_Internal_Options *in;
1897 in->kind = bfd_h_get_8 (abfd, ex->kind);
1898 in->size = bfd_h_get_8 (abfd, ex->size);
1899 in->section = bfd_h_get_16 (abfd, ex->section);
1900 in->info = bfd_h_get_32 (abfd, ex->info);
1903 /* Swap out an options header. */
1905 void
1906 bfd_mips_elf_swap_options_out (abfd, in, ex)
1907 bfd *abfd;
1908 const Elf_Internal_Options *in;
1909 Elf_External_Options *ex;
1911 bfd_h_put_8 (abfd, in->kind, ex->kind);
1912 bfd_h_put_8 (abfd, in->size, ex->size);
1913 bfd_h_put_16 (abfd, in->section, ex->section);
1914 bfd_h_put_32 (abfd, in->info, ex->info);
1917 /* Determine whether a symbol is global for the purposes of splitting
1918 the symbol table into global symbols and local symbols. At least
1919 on Irix 5, this split must be between section symbols and all other
1920 symbols. On most ELF targets the split is between static symbols
1921 and externally visible symbols. */
1923 /*ARGSUSED*/
1924 static boolean
1925 mips_elf_sym_is_global (abfd, sym)
1926 bfd *abfd;
1927 asymbol *sym;
1929 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1932 /* Set the right machine number for a MIPS ELF file. This is used for
1933 both the 32-bit and the 64-bit ABI. */
1935 boolean
1936 _bfd_mips_elf_object_p (abfd)
1937 bfd *abfd;
1939 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
1940 elf_mips_mach (elf_elfheader (abfd)->e_flags));
1941 return true;
1944 /* Set the right machine number for a 32-bit MIPS ELF file. */
1946 static boolean
1947 mips_elf32_object_p (abfd)
1948 bfd *abfd;
1950 /* Irix 5 is broken. Object file symbol tables are not always
1951 sorted correctly such that local symbols precede global symbols,
1952 and the sh_info field in the symbol table is not always right. */
1953 elf_bad_symtab (abfd) = true;
1955 return _bfd_mips_elf_object_p (abfd);
1958 /* The final processing done just before writing out a MIPS ELF object
1959 file. This gets the MIPS architecture right based on the machine
1960 number. This is used by both the 32-bit and the 64-bit ABI. */
1962 /*ARGSUSED*/
1963 void
1964 _bfd_mips_elf_final_write_processing (abfd, linker)
1965 bfd *abfd;
1966 boolean linker;
1968 unsigned long val;
1969 unsigned int i;
1970 Elf_Internal_Shdr **hdrpp;
1971 const char *name;
1972 asection *sec;
1974 switch (bfd_get_mach (abfd))
1976 default:
1977 case bfd_mach_mips3000:
1978 val = E_MIPS_ARCH_1;
1979 break;
1981 case bfd_mach_mips3900:
1982 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
1983 break;
1985 case bfd_mach_mips6000:
1986 val = E_MIPS_ARCH_2;
1987 break;
1989 case bfd_mach_mips4000:
1990 case bfd_mach_mips4300:
1991 val = E_MIPS_ARCH_3;
1992 break;
1994 case bfd_mach_mips4010:
1995 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
1996 break;
1998 case bfd_mach_mips4100:
1999 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
2000 break;
2002 case bfd_mach_mips4111:
2003 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
2004 break;
2006 case bfd_mach_mips4650:
2007 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
2008 break;
2010 case bfd_mach_mips8000:
2011 val = E_MIPS_ARCH_4;
2012 break;
2015 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2016 elf_elfheader (abfd)->e_flags |= val;
2018 /* Set the sh_info field for .gptab sections and other appropriate
2019 info for each special section. */
2020 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2021 i < elf_elfheader (abfd)->e_shnum;
2022 i++, hdrpp++)
2024 switch ((*hdrpp)->sh_type)
2026 case SHT_MIPS_LIBLIST:
2027 sec = bfd_get_section_by_name (abfd, ".dynstr");
2028 if (sec != NULL)
2029 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2030 break;
2032 case SHT_MIPS_GPTAB:
2033 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2034 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2035 BFD_ASSERT (name != NULL
2036 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2037 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2038 BFD_ASSERT (sec != NULL);
2039 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2040 break;
2042 case SHT_MIPS_CONTENT:
2043 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2044 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2045 BFD_ASSERT (name != NULL
2046 && strncmp (name, ".MIPS.content",
2047 sizeof ".MIPS.content" - 1) == 0);
2048 sec = bfd_get_section_by_name (abfd,
2049 name + sizeof ".MIPS.content" - 1);
2050 BFD_ASSERT (sec != NULL);
2051 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2052 break;
2054 case SHT_MIPS_SYMBOL_LIB:
2055 sec = bfd_get_section_by_name (abfd, ".dynsym");
2056 if (sec != NULL)
2057 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2058 sec = bfd_get_section_by_name (abfd, ".liblist");
2059 if (sec != NULL)
2060 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2061 break;
2063 case SHT_MIPS_EVENTS:
2064 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2065 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2066 BFD_ASSERT (name != NULL);
2067 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2068 sec = bfd_get_section_by_name (abfd,
2069 name + sizeof ".MIPS.events" - 1);
2070 else
2072 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2073 sizeof ".MIPS.post_rel" - 1) == 0);
2074 sec = bfd_get_section_by_name (abfd,
2075 (name
2076 + sizeof ".MIPS.post_rel" - 1));
2078 BFD_ASSERT (sec != NULL);
2079 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2080 break;
2086 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2088 boolean
2089 _bfd_mips_elf_set_private_flags (abfd, flags)
2090 bfd *abfd;
2091 flagword flags;
2093 BFD_ASSERT (!elf_flags_init (abfd)
2094 || elf_elfheader (abfd)->e_flags == flags);
2096 elf_elfheader (abfd)->e_flags = flags;
2097 elf_flags_init (abfd) = true;
2098 return true;
2101 /* Copy backend specific data from one object module to another */
2103 boolean
2104 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2105 bfd *ibfd;
2106 bfd *obfd;
2108 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2109 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2110 return true;
2112 BFD_ASSERT (!elf_flags_init (obfd)
2113 || (elf_elfheader (obfd)->e_flags
2114 == elf_elfheader (ibfd)->e_flags));
2116 elf_gp (obfd) = elf_gp (ibfd);
2117 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2118 elf_flags_init (obfd) = true;
2119 return true;
2122 /* Merge backend specific data from an object file to the output
2123 object file when linking. */
2125 boolean
2126 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2127 bfd *ibfd;
2128 bfd *obfd;
2130 flagword old_flags;
2131 flagword new_flags;
2132 boolean ok;
2134 /* Check if we have the same endianess */
2135 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2136 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2138 const char *msg;
2140 if (bfd_big_endian (ibfd))
2141 msg = _("%s: compiled for a big endian system and target is little endian");
2142 else
2143 msg = _("%s: compiled for a little endian system and target is big endian");
2145 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2147 bfd_set_error (bfd_error_wrong_format);
2148 return false;
2151 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2152 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2153 return true;
2155 new_flags = elf_elfheader (ibfd)->e_flags;
2156 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2157 old_flags = elf_elfheader (obfd)->e_flags;
2159 if (! elf_flags_init (obfd))
2161 elf_flags_init (obfd) = true;
2162 elf_elfheader (obfd)->e_flags = new_flags;
2164 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2165 && bfd_get_arch_info (obfd)->the_default)
2167 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2168 bfd_get_mach (ibfd)))
2169 return false;
2172 return true;
2175 /* Check flag compatibility. */
2177 new_flags &= ~EF_MIPS_NOREORDER;
2178 old_flags &= ~EF_MIPS_NOREORDER;
2180 if (new_flags == old_flags)
2181 return true;
2183 ok = true;
2185 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2187 new_flags &= ~EF_MIPS_PIC;
2188 old_flags &= ~EF_MIPS_PIC;
2189 (*_bfd_error_handler)
2190 (_("%s: linking PIC files with non-PIC files"),
2191 bfd_get_filename (ibfd));
2192 ok = false;
2195 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2197 new_flags &= ~EF_MIPS_CPIC;
2198 old_flags &= ~EF_MIPS_CPIC;
2199 (*_bfd_error_handler)
2200 (_("%s: linking abicalls files with non-abicalls files"),
2201 bfd_get_filename (ibfd));
2202 ok = false;
2205 /* Compare the ISA's. */
2206 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2207 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2209 int new_mach = new_flags & EF_MIPS_MACH;
2210 int old_mach = old_flags & EF_MIPS_MACH;
2211 int new_isa = elf_mips_isa (new_flags);
2212 int old_isa = elf_mips_isa (old_flags);
2214 /* If either has no machine specified, just compare the general isa's.
2215 Some combinations of machines are ok, if the isa's match. */
2216 if (! new_mach
2217 || ! old_mach
2218 || new_mach == old_mach
2221 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2222 and -mips4 code. They will normally use the same data sizes and
2223 calling conventions. */
2225 if ((new_isa == 1 || new_isa == 2)
2226 ? (old_isa != 1 && old_isa != 2)
2227 : (old_isa == 1 || old_isa == 2))
2229 (*_bfd_error_handler)
2230 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2231 bfd_get_filename (ibfd), new_isa, old_isa);
2232 ok = false;
2236 else
2238 (*_bfd_error_handler)
2239 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2240 bfd_get_filename (ibfd),
2241 elf_mips_mach (new_flags),
2242 elf_mips_mach (old_flags));
2243 ok = false;
2246 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2247 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2250 /* Compare ABI's */
2251 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI))
2253 /* Only error if both are set (to different values). */
2254 if ((new_flags & EF_MIPS_ABI)
2255 && (old_flags & EF_MIPS_ABI))
2257 (*_bfd_error_handler)
2258 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2259 bfd_get_filename (ibfd),
2260 elf_mips_abi_name (new_flags),
2261 elf_mips_abi_name (old_flags));
2262 ok = false;
2264 new_flags &= ~EF_MIPS_ABI;
2265 old_flags &= ~EF_MIPS_ABI;
2268 /* Warn about any other mismatches */
2269 if (new_flags != old_flags)
2271 (*_bfd_error_handler)
2272 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2273 bfd_get_filename (ibfd), (unsigned long) new_flags,
2274 (unsigned long) old_flags);
2275 ok = false;
2278 if (! ok)
2280 bfd_set_error (bfd_error_bad_value);
2281 return false;
2284 return true;
2287 static boolean
2288 _bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2289 bfd *abfd;
2290 PTR ptr;
2292 FILE *file = (FILE *) ptr;
2294 BFD_ASSERT (abfd != NULL && ptr != NULL);
2296 /* Print normal ELF private data. */
2297 _bfd_elf_print_private_bfd_data (abfd, ptr);
2299 /* xgettext:c-format */
2300 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2302 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2303 fprintf (file, _ (" [abi=O32]"));
2304 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2305 fprintf (file, _ (" [abi=O64]"));
2306 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2307 fprintf (file, _ (" [abi=EABI32]"));
2308 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2309 fprintf (file, _ (" [abi=EABI64]"));
2310 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2311 fprintf (file, _ (" [abi unknown]"));
2312 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2))
2313 fprintf (file, _ (" [abi=N32]"));
2314 else
2315 fprintf (file, _ (" [no abi set]"));
2317 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2318 fprintf (file, _ (" [mips1]"));
2319 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2320 fprintf (file, _ (" [mips2]"));
2321 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2322 fprintf (file, _ (" [mips3]"));
2323 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2324 fprintf (file, _ (" [mips4]"));
2325 else
2326 fprintf (file, _ (" [unknown ISA]"));
2328 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2329 fprintf (file, _ (" [32bitmode]"));
2330 else
2331 fprintf (file, _ (" [not 32bitmode]"));
2333 fputc ('\n', file);
2335 return true;
2338 /* Handle a MIPS specific section when reading an object file. This
2339 is called when elfcode.h finds a section with an unknown type.
2340 This routine supports both the 32-bit and 64-bit ELF ABI.
2342 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2343 how to. */
2345 boolean
2346 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2347 bfd *abfd;
2348 Elf_Internal_Shdr *hdr;
2349 const char *name;
2351 flagword flags = 0;
2353 /* There ought to be a place to keep ELF backend specific flags, but
2354 at the moment there isn't one. We just keep track of the
2355 sections by their name, instead. Fortunately, the ABI gives
2356 suggested names for all the MIPS specific sections, so we will
2357 probably get away with this. */
2358 switch (hdr->sh_type)
2360 case SHT_MIPS_LIBLIST:
2361 if (strcmp (name, ".liblist") != 0)
2362 return false;
2363 break;
2364 case SHT_MIPS_MSYM:
2365 if (strcmp (name, ".msym") != 0)
2366 return false;
2367 break;
2368 case SHT_MIPS_CONFLICT:
2369 if (strcmp (name, ".conflict") != 0)
2370 return false;
2371 break;
2372 case SHT_MIPS_GPTAB:
2373 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2374 return false;
2375 break;
2376 case SHT_MIPS_UCODE:
2377 if (strcmp (name, ".ucode") != 0)
2378 return false;
2379 break;
2380 case SHT_MIPS_DEBUG:
2381 if (strcmp (name, ".mdebug") != 0)
2382 return false;
2383 flags = SEC_DEBUGGING;
2384 break;
2385 case SHT_MIPS_REGINFO:
2386 if (strcmp (name, ".reginfo") != 0
2387 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2388 return false;
2389 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2390 break;
2391 case SHT_MIPS_IFACE:
2392 if (strcmp (name, ".MIPS.interfaces") != 0)
2393 return false;
2394 break;
2395 case SHT_MIPS_CONTENT:
2396 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2397 return false;
2398 break;
2399 case SHT_MIPS_OPTIONS:
2400 if (strcmp (name, ".options") != 0
2401 && strcmp (name, ".MIPS.options") != 0)
2402 return false;
2403 break;
2404 case SHT_MIPS_DWARF:
2405 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2406 return false;
2407 break;
2408 case SHT_MIPS_SYMBOL_LIB:
2409 if (strcmp (name, ".MIPS.symlib") != 0)
2410 return false;
2411 break;
2412 case SHT_MIPS_EVENTS:
2413 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2414 && strncmp (name, ".MIPS.post_rel",
2415 sizeof ".MIPS.post_rel" - 1) != 0)
2416 return false;
2417 break;
2418 default:
2419 return false;
2422 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2423 return false;
2425 if (flags)
2427 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2428 (bfd_get_section_flags (abfd,
2429 hdr->bfd_section)
2430 | flags)))
2431 return false;
2434 return true;
2437 /* Handle a 32-bit MIPS ELF specific section. */
2439 static boolean
2440 mips_elf32_section_from_shdr (abfd, hdr, name)
2441 bfd *abfd;
2442 Elf_Internal_Shdr *hdr;
2443 char *name;
2445 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2446 return false;
2448 /* FIXME: We should record sh_info for a .gptab section. */
2450 /* For a .reginfo section, set the gp value in the tdata information
2451 from the contents of this section. We need the gp value while
2452 processing relocs, so we just get it now. The .reginfo section
2453 is not used in the 64-bit MIPS ELF ABI. */
2454 if (hdr->sh_type == SHT_MIPS_REGINFO)
2456 Elf32_External_RegInfo ext;
2457 Elf32_RegInfo s;
2459 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2460 (file_ptr) 0, sizeof ext))
2461 return false;
2462 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2463 elf_gp (abfd) = s.ri_gp_value;
2466 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2467 set the gp value based on what we find. We may see both
2468 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2469 they should agree. */
2470 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2472 bfd_byte *contents, *l, *lend;
2474 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2475 if (contents == NULL)
2476 return false;
2477 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2478 (file_ptr) 0, hdr->sh_size))
2480 free (contents);
2481 return false;
2483 l = contents;
2484 lend = contents + hdr->sh_size;
2485 while (l + sizeof (Elf_External_Options) <= lend)
2487 Elf_Internal_Options intopt;
2489 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2490 &intopt);
2491 if (intopt.kind == ODK_REGINFO)
2493 Elf32_RegInfo intreg;
2495 bfd_mips_elf32_swap_reginfo_in
2496 (abfd,
2497 ((Elf32_External_RegInfo *)
2498 (l + sizeof (Elf_External_Options))),
2499 &intreg);
2500 elf_gp (abfd) = intreg.ri_gp_value;
2502 l += intopt.size;
2504 free (contents);
2507 return true;
2510 /* Set the correct type for a MIPS ELF section. We do this by the
2511 section name, which is a hack, but ought to work. This routine is
2512 used by both the 32-bit and the 64-bit ABI. */
2514 boolean
2515 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2516 bfd *abfd;
2517 Elf32_Internal_Shdr *hdr;
2518 asection *sec;
2520 register const char *name;
2522 name = bfd_get_section_name (abfd, sec);
2524 if (strcmp (name, ".liblist") == 0)
2526 hdr->sh_type = SHT_MIPS_LIBLIST;
2527 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2528 /* The sh_link field is set in final_write_processing. */
2530 else if (strcmp (name, ".msym") == 0)
2532 hdr->sh_type = SHT_MIPS_MSYM;
2533 hdr->sh_entsize = 8;
2534 /* FIXME: Set the sh_info field. */
2536 else if (strcmp (name, ".conflict") == 0)
2537 hdr->sh_type = SHT_MIPS_CONFLICT;
2538 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2540 hdr->sh_type = SHT_MIPS_GPTAB;
2541 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2542 /* The sh_info field is set in final_write_processing. */
2544 else if (strcmp (name, ".ucode") == 0)
2545 hdr->sh_type = SHT_MIPS_UCODE;
2546 else if (strcmp (name, ".mdebug") == 0)
2548 hdr->sh_type = SHT_MIPS_DEBUG;
2549 /* In a shared object on Irix 5.3, the .mdebug section has an
2550 entsize of 0. FIXME: Does this matter? */
2551 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2552 hdr->sh_entsize = 0;
2553 else
2554 hdr->sh_entsize = 1;
2556 else if (strcmp (name, ".reginfo") == 0)
2558 hdr->sh_type = SHT_MIPS_REGINFO;
2559 /* In a shared object on Irix 5.3, the .reginfo section has an
2560 entsize of 0x18. FIXME: Does this matter? */
2561 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2562 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2563 else
2564 hdr->sh_entsize = 1;
2566 else if (SGI_COMPAT (abfd)
2567 && (strcmp (name, ".hash") == 0
2568 || strcmp (name, ".dynamic") == 0
2569 || strcmp (name, ".dynstr") == 0))
2571 hdr->sh_entsize = 0;
2572 #if 0
2573 /* This isn't how the Irix 6 linker behaves. */
2574 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2575 #endif
2577 else if (strcmp (name, ".got") == 0
2578 || strcmp (name, ".sdata") == 0
2579 || strcmp (name, ".sbss") == 0
2580 || strcmp (name, ".lit4") == 0
2581 || strcmp (name, ".lit8") == 0)
2582 hdr->sh_flags |= SHF_MIPS_GPREL;
2583 else if (strcmp (name, ".MIPS.interfaces") == 0)
2585 hdr->sh_type = SHT_MIPS_IFACE;
2586 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2588 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
2590 hdr->sh_type = SHT_MIPS_CONTENT;
2591 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2592 /* The sh_info field is set in final_write_processing. */
2594 else if (strcmp (name, ".options") == 0
2595 || strcmp (name, ".MIPS.options") == 0)
2597 hdr->sh_type = SHT_MIPS_OPTIONS;
2598 hdr->sh_entsize = 1;
2599 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2601 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2602 hdr->sh_type = SHT_MIPS_DWARF;
2603 else if (strcmp (name, ".MIPS.symlib") == 0)
2605 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2606 /* The sh_link and sh_info fields are set in
2607 final_write_processing. */
2609 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2610 || strncmp (name, ".MIPS.post_rel",
2611 sizeof ".MIPS.post_rel" - 1) == 0)
2613 hdr->sh_type = SHT_MIPS_EVENTS;
2614 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2615 /* The sh_link field is set in final_write_processing. */
2618 return true;
2621 /* Given a BFD section, try to locate the corresponding ELF section
2622 index. This is used by both the 32-bit and the 64-bit ABI.
2623 Actually, it's not clear to me that the 64-bit ABI supports these,
2624 but for non-PIC objects we will certainly want support for at least
2625 the .scommon section. */
2627 boolean
2628 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2629 bfd *abfd;
2630 Elf32_Internal_Shdr *hdr;
2631 asection *sec;
2632 int *retval;
2634 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2636 *retval = SHN_MIPS_SCOMMON;
2637 return true;
2639 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2641 *retval = SHN_MIPS_ACOMMON;
2642 return true;
2644 return false;
2647 /* When are writing out the .options or .MIPS.options section,
2648 remember the bytes we are writing out, so that we can install the
2649 GP value in the section_processing routine. */
2651 boolean
2652 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2653 bfd *abfd;
2654 sec_ptr section;
2655 PTR location;
2656 file_ptr offset;
2657 bfd_size_type count;
2659 if (strcmp (section->name, ".options") == 0
2660 || strcmp (section->name, ".MIPS.options") == 0)
2662 bfd_byte *c;
2664 if (elf_section_data (section) == NULL)
2666 section->used_by_bfd =
2667 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2668 if (elf_section_data (section) == NULL)
2669 return false;
2671 c = (bfd_byte *) elf_section_data (section)->tdata;
2672 if (c == NULL)
2674 bfd_size_type size;
2676 if (section->_cooked_size != 0)
2677 size = section->_cooked_size;
2678 else
2679 size = section->_raw_size;
2680 c = (bfd_byte *) bfd_zalloc (abfd, size);
2681 if (c == NULL)
2682 return false;
2683 elf_section_data (section)->tdata = (PTR) c;
2686 memcpy (c + offset, location, count);
2689 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2690 count);
2693 /* Work over a section just before writing it out. This routine is
2694 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2695 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2696 a better way. */
2698 boolean
2699 _bfd_mips_elf_section_processing (abfd, hdr)
2700 bfd *abfd;
2701 Elf_Internal_Shdr *hdr;
2703 if (hdr->bfd_section != NULL)
2705 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2707 if (strcmp (name, ".sdata") == 0)
2709 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2710 hdr->sh_type = SHT_PROGBITS;
2712 else if (strcmp (name, ".sbss") == 0)
2714 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2715 hdr->sh_type = SHT_NOBITS;
2717 else if (strcmp (name, ".lit8") == 0
2718 || strcmp (name, ".lit4") == 0)
2720 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2721 hdr->sh_type = SHT_PROGBITS;
2723 else if (strcmp (name, ".compact_rel") == 0)
2725 hdr->sh_flags = 0;
2726 hdr->sh_type = SHT_PROGBITS;
2728 else if (strcmp (name, ".rtproc") == 0)
2730 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2732 unsigned int adjust;
2734 adjust = hdr->sh_size % hdr->sh_addralign;
2735 if (adjust != 0)
2736 hdr->sh_size += hdr->sh_addralign - adjust;
2741 return true;
2744 /* Work over a section just before writing it out. We update the GP
2745 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2746 on the value we are using. */
2748 static boolean
2749 mips_elf32_section_processing (abfd, hdr)
2750 bfd *abfd;
2751 Elf32_Internal_Shdr *hdr;
2753 if (hdr->sh_type == SHT_MIPS_REGINFO
2754 && hdr->sh_size > 0)
2756 bfd_byte buf[4];
2758 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2759 BFD_ASSERT (hdr->contents == NULL);
2761 if (bfd_seek (abfd,
2762 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2763 SEEK_SET) == -1)
2764 return false;
2765 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2766 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2767 return false;
2770 if (hdr->sh_type == SHT_MIPS_OPTIONS
2771 && hdr->bfd_section != NULL
2772 && elf_section_data (hdr->bfd_section) != NULL
2773 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2775 bfd_byte *contents, *l, *lend;
2777 /* We stored the section contents in the elf_section_data tdata
2778 field in the set_section_contents routine. We save the
2779 section contents so that we don't have to read them again.
2780 At this point we know that elf_gp is set, so we can look
2781 through the section contents to see if there is an
2782 ODK_REGINFO structure. */
2784 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2785 l = contents;
2786 lend = contents + hdr->sh_size;
2787 while (l + sizeof (Elf_External_Options) <= lend)
2789 Elf_Internal_Options intopt;
2791 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2792 &intopt);
2793 if (intopt.kind == ODK_REGINFO)
2795 bfd_byte buf[4];
2797 if (bfd_seek (abfd,
2798 (hdr->sh_offset
2799 + (l - contents)
2800 + sizeof (Elf_External_Options)
2801 + (sizeof (Elf32_External_RegInfo) - 4)),
2802 SEEK_SET) == -1)
2803 return false;
2804 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2805 if (bfd_write (buf, 1, 4, abfd) != 4)
2806 return false;
2808 l += intopt.size;
2812 return _bfd_mips_elf_section_processing (abfd, hdr);
2815 /* MIPS ELF uses two common sections. One is the usual one, and the
2816 other is for small objects. All the small objects are kept
2817 together, and then referenced via the gp pointer, which yields
2818 faster assembler code. This is what we use for the small common
2819 section. This approach is copied from ecoff.c. */
2820 static asection mips_elf_scom_section;
2821 static asymbol mips_elf_scom_symbol;
2822 static asymbol *mips_elf_scom_symbol_ptr;
2824 /* MIPS ELF also uses an acommon section, which represents an
2825 allocated common symbol which may be overridden by a
2826 definition in a shared library. */
2827 static asection mips_elf_acom_section;
2828 static asymbol mips_elf_acom_symbol;
2829 static asymbol *mips_elf_acom_symbol_ptr;
2831 /* The Irix 5 support uses two virtual sections, which represent
2832 text/data symbols defined in dynamic objects. */
2833 static asection mips_elf_text_section;
2834 static asection *mips_elf_text_section_ptr;
2835 static asymbol mips_elf_text_symbol;
2836 static asymbol *mips_elf_text_symbol_ptr;
2838 static asection mips_elf_data_section;
2839 static asection *mips_elf_data_section_ptr;
2840 static asymbol mips_elf_data_symbol;
2841 static asymbol *mips_elf_data_symbol_ptr;
2843 /* Handle the special MIPS section numbers that a symbol may use.
2844 This is used for both the 32-bit and the 64-bit ABI. */
2846 void
2847 _bfd_mips_elf_symbol_processing (abfd, asym)
2848 bfd *abfd;
2849 asymbol *asym;
2851 elf_symbol_type *elfsym;
2853 elfsym = (elf_symbol_type *) asym;
2854 switch (elfsym->internal_elf_sym.st_shndx)
2856 case SHN_MIPS_ACOMMON:
2857 /* This section is used in a dynamically linked executable file.
2858 It is an allocated common section. The dynamic linker can
2859 either resolve these symbols to something in a shared
2860 library, or it can just leave them here. For our purposes,
2861 we can consider these symbols to be in a new section. */
2862 if (mips_elf_acom_section.name == NULL)
2864 /* Initialize the acommon section. */
2865 mips_elf_acom_section.name = ".acommon";
2866 mips_elf_acom_section.flags = SEC_ALLOC;
2867 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2868 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2869 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2870 mips_elf_acom_symbol.name = ".acommon";
2871 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2872 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2873 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2875 asym->section = &mips_elf_acom_section;
2876 break;
2878 case SHN_COMMON:
2879 /* Common symbols less than the GP size are automatically
2880 treated as SHN_MIPS_SCOMMON symbols. */
2881 if (asym->value > elf_gp_size (abfd))
2882 break;
2883 /* Fall through. */
2884 case SHN_MIPS_SCOMMON:
2885 if (mips_elf_scom_section.name == NULL)
2887 /* Initialize the small common section. */
2888 mips_elf_scom_section.name = ".scommon";
2889 mips_elf_scom_section.flags = SEC_IS_COMMON;
2890 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2891 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2892 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2893 mips_elf_scom_symbol.name = ".scommon";
2894 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2895 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2896 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2898 asym->section = &mips_elf_scom_section;
2899 asym->value = elfsym->internal_elf_sym.st_size;
2900 break;
2902 case SHN_MIPS_SUNDEFINED:
2903 asym->section = bfd_und_section_ptr;
2904 break;
2906 #if 0 /* for SGI_COMPAT */
2907 case SHN_MIPS_TEXT:
2908 asym->section = mips_elf_text_section_ptr;
2909 break;
2911 case SHN_MIPS_DATA:
2912 asym->section = mips_elf_data_section_ptr;
2913 break;
2914 #endif
2918 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
2919 segments. */
2921 static int
2922 mips_elf_additional_program_headers (abfd)
2923 bfd *abfd;
2925 asection *s;
2926 int ret;
2928 ret = 0;
2930 if (! SGI_COMPAT (abfd))
2931 return ret;
2933 s = bfd_get_section_by_name (abfd, ".reginfo");
2934 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2936 /* We need a PT_MIPS_REGINFO segment. */
2937 ++ret;
2940 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2941 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2943 /* We need a PT_MIPS_RTPROC segment. */
2944 ++ret;
2947 return ret;
2950 /* Modify the segment map for an Irix 5 executable. */
2952 static boolean
2953 mips_elf_modify_segment_map (abfd)
2954 bfd *abfd;
2956 asection *s;
2957 struct elf_segment_map *m, **pm;
2959 if (! SGI_COMPAT (abfd))
2960 return true;
2962 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2963 segment. */
2964 s = bfd_get_section_by_name (abfd, ".reginfo");
2965 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2967 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2968 if (m->p_type == PT_MIPS_REGINFO)
2969 break;
2970 if (m == NULL)
2972 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2973 if (m == NULL)
2974 return false;
2976 m->p_type = PT_MIPS_REGINFO;
2977 m->count = 1;
2978 m->sections[0] = s;
2980 /* We want to put it after the PHDR and INTERP segments. */
2981 pm = &elf_tdata (abfd)->segment_map;
2982 while (*pm != NULL
2983 && ((*pm)->p_type == PT_PHDR
2984 || (*pm)->p_type == PT_INTERP))
2985 pm = &(*pm)->next;
2987 m->next = *pm;
2988 *pm = m;
2992 /* If there are .dynamic and .mdebug sections, we make a room for
2993 the RTPROC header. FIXME: Rewrite without section names. */
2994 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2995 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2996 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2998 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2999 if (m->p_type == PT_MIPS_RTPROC)
3000 break;
3001 if (m == NULL)
3003 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
3004 if (m == NULL)
3005 return false;
3007 m->p_type = PT_MIPS_RTPROC;
3009 s = bfd_get_section_by_name (abfd, ".rtproc");
3010 if (s == NULL)
3012 m->count = 0;
3013 m->p_flags = 0;
3014 m->p_flags_valid = 1;
3016 else
3018 m->count = 1;
3019 m->sections[0] = s;
3022 /* We want to put it after the DYNAMIC segment. */
3023 pm = &elf_tdata (abfd)->segment_map;
3024 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3025 pm = &(*pm)->next;
3026 if (*pm != NULL)
3027 pm = &(*pm)->next;
3029 m->next = *pm;
3030 *pm = m;
3034 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
3035 .dynsym, and .hash sections, and everything in between. */
3036 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3037 if ((*pm)->p_type == PT_DYNAMIC)
3038 break;
3039 m = *pm;
3040 if (m != NULL
3041 && m->count == 1
3042 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3044 static const char *sec_names[] =
3045 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3046 bfd_vma low, high;
3047 unsigned int i, c;
3048 struct elf_segment_map *n;
3050 low = 0xffffffff;
3051 high = 0;
3052 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3054 s = bfd_get_section_by_name (abfd, sec_names[i]);
3055 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3057 bfd_size_type sz;
3059 if (low > s->vma)
3060 low = s->vma;
3061 sz = s->_cooked_size;
3062 if (sz == 0)
3063 sz = s->_raw_size;
3064 if (high < s->vma + sz)
3065 high = s->vma + sz;
3069 c = 0;
3070 for (s = abfd->sections; s != NULL; s = s->next)
3071 if ((s->flags & SEC_LOAD) != 0
3072 && s->vma >= low
3073 && ((s->vma
3074 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3075 <= high))
3076 ++c;
3078 n = ((struct elf_segment_map *)
3079 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3080 if (n == NULL)
3081 return false;
3082 *n = *m;
3083 n->count = c;
3085 i = 0;
3086 for (s = abfd->sections; s != NULL; s = s->next)
3088 if ((s->flags & SEC_LOAD) != 0
3089 && s->vma >= low
3090 && ((s->vma
3091 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3092 <= high))
3094 n->sections[i] = s;
3095 ++i;
3099 *pm = n;
3102 return true;
3105 /* The structure of the runtime procedure descriptor created by the
3106 loader for use by the static exception system. */
3108 typedef struct runtime_pdr {
3109 bfd_vma adr; /* memory address of start of procedure */
3110 long regmask; /* save register mask */
3111 long regoffset; /* save register offset */
3112 long fregmask; /* save floating point register mask */
3113 long fregoffset; /* save floating point register offset */
3114 long frameoffset; /* frame size */
3115 short framereg; /* frame pointer register */
3116 short pcreg; /* offset or reg of return pc */
3117 long irpss; /* index into the runtime string table */
3118 long reserved;
3119 struct exception_info *exception_info;/* pointer to exception array */
3120 } RPDR, *pRPDR;
3121 #define cbRPDR sizeof(RPDR)
3122 #define rpdNil ((pRPDR) 0)
3124 /* Swap RPDR (runtime procedure table entry) for output. */
3126 static void ecoff_swap_rpdr_out
3127 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3129 static void
3130 ecoff_swap_rpdr_out (abfd, in, ex)
3131 bfd *abfd;
3132 const RPDR *in;
3133 struct rpdr_ext *ex;
3135 /* ecoff_put_off was defined in ecoffswap.h. */
3136 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3137 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3138 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3139 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3140 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3141 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3143 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3144 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3146 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3147 #if 0 /* FIXME */
3148 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3149 #endif
3152 /* Read ECOFF debugging information from a .mdebug section into a
3153 ecoff_debug_info structure. */
3155 boolean
3156 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3157 bfd *abfd;
3158 asection *section;
3159 struct ecoff_debug_info *debug;
3161 HDRR *symhdr;
3162 const struct ecoff_debug_swap *swap;
3163 char *ext_hdr = NULL;
3165 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3166 memset (debug, 0, sizeof(*debug));
3168 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3169 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3170 goto error_return;
3172 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3173 swap->external_hdr_size)
3174 == false)
3175 goto error_return;
3177 symhdr = &debug->symbolic_header;
3178 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3180 /* The symbolic header contains absolute file offsets and sizes to
3181 read. */
3182 #define READ(ptr, offset, count, size, type) \
3183 if (symhdr->count == 0) \
3184 debug->ptr = NULL; \
3185 else \
3187 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3188 if (debug->ptr == NULL) \
3189 goto error_return; \
3190 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3191 || (bfd_read (debug->ptr, size, symhdr->count, \
3192 abfd) != size * symhdr->count)) \
3193 goto error_return; \
3196 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3197 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3198 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3199 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3200 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3201 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3202 union aux_ext *);
3203 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3204 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3205 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3206 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3207 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3208 #undef READ
3210 debug->fdr = NULL;
3211 debug->adjust = NULL;
3213 return true;
3215 error_return:
3216 if (ext_hdr != NULL)
3217 free (ext_hdr);
3218 if (debug->line != NULL)
3219 free (debug->line);
3220 if (debug->external_dnr != NULL)
3221 free (debug->external_dnr);
3222 if (debug->external_pdr != NULL)
3223 free (debug->external_pdr);
3224 if (debug->external_sym != NULL)
3225 free (debug->external_sym);
3226 if (debug->external_opt != NULL)
3227 free (debug->external_opt);
3228 if (debug->external_aux != NULL)
3229 free (debug->external_aux);
3230 if (debug->ss != NULL)
3231 free (debug->ss);
3232 if (debug->ssext != NULL)
3233 free (debug->ssext);
3234 if (debug->external_fdr != NULL)
3235 free (debug->external_fdr);
3236 if (debug->external_rfd != NULL)
3237 free (debug->external_rfd);
3238 if (debug->external_ext != NULL)
3239 free (debug->external_ext);
3240 return false;
3243 /* MIPS ELF local labels start with '$', not 'L'. */
3245 /*ARGSUSED*/
3246 static boolean
3247 mips_elf_is_local_label_name (abfd, name)
3248 bfd *abfd;
3249 const char *name;
3251 if (name[0] == '$')
3252 return true;
3254 /* On Irix 6, the labels go back to starting with '.', so we accept
3255 the generic ELF local label syntax as well. */
3256 return _bfd_elf_is_local_label_name (abfd, name);
3259 /* MIPS ELF uses a special find_nearest_line routine in order the
3260 handle the ECOFF debugging information. */
3262 struct mips_elf_find_line
3264 struct ecoff_debug_info d;
3265 struct ecoff_find_line i;
3268 boolean
3269 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3270 functionname_ptr, line_ptr)
3271 bfd *abfd;
3272 asection *section;
3273 asymbol **symbols;
3274 bfd_vma offset;
3275 const char **filename_ptr;
3276 const char **functionname_ptr;
3277 unsigned int *line_ptr;
3279 asection *msec;
3281 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3282 filename_ptr, functionname_ptr,
3283 line_ptr))
3284 return true;
3286 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3287 filename_ptr, functionname_ptr,
3288 line_ptr))
3289 return true;
3291 msec = bfd_get_section_by_name (abfd, ".mdebug");
3292 if (msec != NULL)
3294 flagword origflags;
3295 struct mips_elf_find_line *fi;
3296 const struct ecoff_debug_swap * const swap =
3297 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3299 /* If we are called during a link, mips_elf_final_link may have
3300 cleared the SEC_HAS_CONTENTS field. We force it back on here
3301 if appropriate (which it normally will be). */
3302 origflags = msec->flags;
3303 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3304 msec->flags |= SEC_HAS_CONTENTS;
3306 fi = elf_tdata (abfd)->find_line_info;
3307 if (fi == NULL)
3309 bfd_size_type external_fdr_size;
3310 char *fraw_src;
3311 char *fraw_end;
3312 struct fdr *fdr_ptr;
3314 fi = ((struct mips_elf_find_line *)
3315 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3316 if (fi == NULL)
3318 msec->flags = origflags;
3319 return false;
3322 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3324 msec->flags = origflags;
3325 return false;
3328 /* Swap in the FDR information. */
3329 fi->d.fdr = ((struct fdr *)
3330 bfd_alloc (abfd,
3331 (fi->d.symbolic_header.ifdMax *
3332 sizeof (struct fdr))));
3333 if (fi->d.fdr == NULL)
3335 msec->flags = origflags;
3336 return false;
3338 external_fdr_size = swap->external_fdr_size;
3339 fdr_ptr = fi->d.fdr;
3340 fraw_src = (char *) fi->d.external_fdr;
3341 fraw_end = (fraw_src
3342 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3343 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3344 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3346 elf_tdata (abfd)->find_line_info = fi;
3348 /* Note that we don't bother to ever free this information.
3349 find_nearest_line is either called all the time, as in
3350 objdump -l, so the information should be saved, or it is
3351 rarely called, as in ld error messages, so the memory
3352 wasted is unimportant. Still, it would probably be a
3353 good idea for free_cached_info to throw it away. */
3356 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3357 &fi->i, filename_ptr, functionname_ptr,
3358 line_ptr))
3360 msec->flags = origflags;
3361 return true;
3364 msec->flags = origflags;
3367 /* Fall back on the generic ELF find_nearest_line routine. */
3369 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3370 filename_ptr, functionname_ptr,
3371 line_ptr);
3374 /* The mips16 compiler uses a couple of special sections to handle
3375 floating point arguments.
3377 Section names that look like .mips16.fn.FNNAME contain stubs that
3378 copy floating point arguments from the fp regs to the gp regs and
3379 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3380 call should be redirected to the stub instead. If no 32 bit
3381 function calls FNNAME, the stub should be discarded. We need to
3382 consider any reference to the function, not just a call, because
3383 if the address of the function is taken we will need the stub,
3384 since the address might be passed to a 32 bit function.
3386 Section names that look like .mips16.call.FNNAME contain stubs
3387 that copy floating point arguments from the gp regs to the fp
3388 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3389 then any 16 bit function that calls FNNAME should be redirected
3390 to the stub instead. If FNNAME is not a 32 bit function, the
3391 stub should be discarded.
3393 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3394 which call FNNAME and then copy the return value from the fp regs
3395 to the gp regs. These stubs store the return value in $18 while
3396 calling FNNAME; any function which might call one of these stubs
3397 must arrange to save $18 around the call. (This case is not
3398 needed for 32 bit functions that call 16 bit functions, because
3399 16 bit functions always return floating point values in both
3400 $f0/$f1 and $2/$3.)
3402 Note that in all cases FNNAME might be defined statically.
3403 Therefore, FNNAME is not used literally. Instead, the relocation
3404 information will indicate which symbol the section is for.
3406 We record any stubs that we find in the symbol table. */
3408 #define FN_STUB ".mips16.fn."
3409 #define CALL_STUB ".mips16.call."
3410 #define CALL_FP_STUB ".mips16.call.fp."
3412 /* The MIPS ELF linker needs additional information for each symbol in
3413 the global hash table. */
3415 struct mips_elf_link_hash_entry
3417 struct elf_link_hash_entry root;
3419 /* External symbol information. */
3420 EXTR esym;
3422 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
3423 unsigned int mips_32_relocs;
3425 /* If there is a stub that 32 bit functions should use to call this
3426 16 bit function, this points to the section containing the stub. */
3427 asection *fn_stub;
3429 /* Whether we need the fn_stub; this is set if this symbol appears
3430 in any relocs other than a 16 bit call. */
3431 boolean need_fn_stub;
3433 /* If there is a stub that 16 bit functions should use to call this
3434 32 bit function, this points to the section containing the stub. */
3435 asection *call_stub;
3437 /* This is like the call_stub field, but it is used if the function
3438 being called returns a floating point value. */
3439 asection *call_fp_stub;
3442 /* MIPS ELF linker hash table. */
3444 struct mips_elf_link_hash_table
3446 struct elf_link_hash_table root;
3447 #if 0
3448 /* We no longer use this. */
3449 /* String section indices for the dynamic section symbols. */
3450 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3451 #endif
3452 /* The number of .rtproc entries. */
3453 bfd_size_type procedure_count;
3454 /* The size of the .compact_rel section (if SGI_COMPAT). */
3455 bfd_size_type compact_rel_size;
3456 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3457 entry is set to the address of __rld_obj_head as in Irix 5. */
3458 boolean use_rld_obj_head;
3459 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3460 bfd_vma rld_value;
3461 /* This is set if we see any mips16 stub sections. */
3462 boolean mips16_stubs_seen;
3465 /* Look up an entry in a MIPS ELF linker hash table. */
3467 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3468 ((struct mips_elf_link_hash_entry *) \
3469 elf_link_hash_lookup (&(table)->root, (string), (create), \
3470 (copy), (follow)))
3472 /* Traverse a MIPS ELF linker hash table. */
3474 #define mips_elf_link_hash_traverse(table, func, info) \
3475 (elf_link_hash_traverse \
3476 (&(table)->root, \
3477 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3478 (info)))
3480 /* Get the MIPS ELF linker hash table from a link_info structure. */
3482 #define mips_elf_hash_table(p) \
3483 ((struct mips_elf_link_hash_table *) ((p)->hash))
3485 static boolean mips_elf_output_extsym
3486 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3488 /* Create an entry in a MIPS ELF linker hash table. */
3490 static struct bfd_hash_entry *
3491 mips_elf_link_hash_newfunc (entry, table, string)
3492 struct bfd_hash_entry *entry;
3493 struct bfd_hash_table *table;
3494 const char *string;
3496 struct mips_elf_link_hash_entry *ret =
3497 (struct mips_elf_link_hash_entry *) entry;
3499 /* Allocate the structure if it has not already been allocated by a
3500 subclass. */
3501 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3502 ret = ((struct mips_elf_link_hash_entry *)
3503 bfd_hash_allocate (table,
3504 sizeof (struct mips_elf_link_hash_entry)));
3505 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3506 return (struct bfd_hash_entry *) ret;
3508 /* Call the allocation method of the superclass. */
3509 ret = ((struct mips_elf_link_hash_entry *)
3510 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3511 table, string));
3512 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3514 /* Set local fields. */
3515 memset (&ret->esym, 0, sizeof (EXTR));
3516 /* We use -2 as a marker to indicate that the information has
3517 not been set. -1 means there is no associated ifd. */
3518 ret->esym.ifd = -2;
3519 ret->mips_32_relocs = 0;
3520 ret->fn_stub = NULL;
3521 ret->need_fn_stub = false;
3522 ret->call_stub = NULL;
3523 ret->call_fp_stub = NULL;
3526 return (struct bfd_hash_entry *) ret;
3529 /* Create a MIPS ELF linker hash table. */
3531 static struct bfd_link_hash_table *
3532 mips_elf_link_hash_table_create (abfd)
3533 bfd *abfd;
3535 struct mips_elf_link_hash_table *ret;
3537 ret = ((struct mips_elf_link_hash_table *)
3538 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3539 if (ret == (struct mips_elf_link_hash_table *) NULL)
3540 return NULL;
3542 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3543 mips_elf_link_hash_newfunc))
3545 bfd_release (abfd, ret);
3546 return NULL;
3549 #if 0
3550 /* We no longer use this. */
3551 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3552 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3553 #endif
3554 ret->procedure_count = 0;
3555 ret->compact_rel_size = 0;
3556 ret->use_rld_obj_head = false;
3557 ret->rld_value = 0;
3558 ret->mips16_stubs_seen = false;
3560 return &ret->root.root;
3563 /* Hook called by the linker routine which adds symbols from an object
3564 file. We must handle the special MIPS section numbers here. */
3566 /*ARGSUSED*/
3567 static boolean
3568 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3569 bfd *abfd;
3570 struct bfd_link_info *info;
3571 const Elf_Internal_Sym *sym;
3572 const char **namep;
3573 flagword *flagsp;
3574 asection **secp;
3575 bfd_vma *valp;
3577 if (SGI_COMPAT (abfd)
3578 && (abfd->flags & DYNAMIC) != 0
3579 && strcmp (*namep, "_rld_new_interface") == 0)
3581 /* Skip Irix 5 rld entry name. */
3582 *namep = NULL;
3583 return true;
3586 switch (sym->st_shndx)
3588 case SHN_COMMON:
3589 /* Common symbols less than the GP size are automatically
3590 treated as SHN_MIPS_SCOMMON symbols. */
3591 if (sym->st_size > elf_gp_size (abfd))
3592 break;
3593 /* Fall through. */
3594 case SHN_MIPS_SCOMMON:
3595 *secp = bfd_make_section_old_way (abfd, ".scommon");
3596 (*secp)->flags |= SEC_IS_COMMON;
3597 *valp = sym->st_size;
3598 break;
3600 case SHN_MIPS_TEXT:
3601 /* This section is used in a shared object. */
3602 if (mips_elf_text_section_ptr == NULL)
3604 /* Initialize the section. */
3605 mips_elf_text_section.name = ".text";
3606 mips_elf_text_section.flags = SEC_NO_FLAGS;
3607 mips_elf_text_section.output_section = NULL;
3608 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3609 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3610 mips_elf_text_symbol.name = ".text";
3611 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3612 mips_elf_text_symbol.section = &mips_elf_text_section;
3613 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3614 mips_elf_text_section_ptr = &mips_elf_text_section;
3616 /* This code used to do *secp = bfd_und_section_ptr if
3617 info->shared. I don't know why, and that doesn't make sense,
3618 so I took it out. */
3619 *secp = mips_elf_text_section_ptr;
3620 break;
3622 case SHN_MIPS_ACOMMON:
3623 /* Fall through. XXX Can we treat this as allocated data? */
3624 case SHN_MIPS_DATA:
3625 /* This section is used in a shared object. */
3626 if (mips_elf_data_section_ptr == NULL)
3628 /* Initialize the section. */
3629 mips_elf_data_section.name = ".data";
3630 mips_elf_data_section.flags = SEC_NO_FLAGS;
3631 mips_elf_data_section.output_section = NULL;
3632 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3633 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3634 mips_elf_data_symbol.name = ".data";
3635 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3636 mips_elf_data_symbol.section = &mips_elf_data_section;
3637 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3638 mips_elf_data_section_ptr = &mips_elf_data_section;
3640 /* This code used to do *secp = bfd_und_section_ptr if
3641 info->shared. I don't know why, and that doesn't make sense,
3642 so I took it out. */
3643 *secp = mips_elf_data_section_ptr;
3644 break;
3646 case SHN_MIPS_SUNDEFINED:
3647 *secp = bfd_und_section_ptr;
3648 break;
3651 if (SGI_COMPAT (abfd)
3652 && ! info->shared
3653 && info->hash->creator == abfd->xvec
3654 && strcmp (*namep, "__rld_obj_head") == 0)
3656 struct elf_link_hash_entry *h;
3658 /* Mark __rld_obj_head as dynamic. */
3659 h = NULL;
3660 if (! (_bfd_generic_link_add_one_symbol
3661 (info, abfd, *namep, BSF_GLOBAL, *secp,
3662 (bfd_vma) *valp, (const char *) NULL, false,
3663 get_elf_backend_data (abfd)->collect,
3664 (struct bfd_link_hash_entry **) &h)))
3665 return false;
3666 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3667 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3668 h->type = STT_OBJECT;
3670 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3671 return false;
3673 mips_elf_hash_table (info)->use_rld_obj_head = true;
3676 /* If this is a mips16 text symbol, add 1 to the value to make it
3677 odd. This will cause something like .word SYM to come up with
3678 the right value when it is loaded into the PC. */
3679 if (sym->st_other == STO_MIPS16)
3680 ++*valp;
3682 return true;
3685 /* Structure used to pass information to mips_elf_output_extsym. */
3687 struct extsym_info
3689 bfd *abfd;
3690 struct bfd_link_info *info;
3691 struct ecoff_debug_info *debug;
3692 const struct ecoff_debug_swap *swap;
3693 boolean failed;
3696 /* This routine is used to write out ECOFF debugging external symbol
3697 information. It is called via mips_elf_link_hash_traverse. The
3698 ECOFF external symbol information must match the ELF external
3699 symbol information. Unfortunately, at this point we don't know
3700 whether a symbol is required by reloc information, so the two
3701 tables may wind up being different. We must sort out the external
3702 symbol information before we can set the final size of the .mdebug
3703 section, and we must set the size of the .mdebug section before we
3704 can relocate any sections, and we can't know which symbols are
3705 required by relocation until we relocate the sections.
3706 Fortunately, it is relatively unlikely that any symbol will be
3707 stripped but required by a reloc. In particular, it can not happen
3708 when generating a final executable. */
3710 static boolean
3711 mips_elf_output_extsym (h, data)
3712 struct mips_elf_link_hash_entry *h;
3713 PTR data;
3715 struct extsym_info *einfo = (struct extsym_info *) data;
3716 boolean strip;
3717 asection *sec, *output_section;
3719 if (h->root.indx == -2)
3720 strip = false;
3721 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3722 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3723 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3724 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3725 strip = true;
3726 else if (einfo->info->strip == strip_all
3727 || (einfo->info->strip == strip_some
3728 && bfd_hash_lookup (einfo->info->keep_hash,
3729 h->root.root.root.string,
3730 false, false) == NULL))
3731 strip = true;
3732 else
3733 strip = false;
3735 if (strip)
3736 return true;
3738 if (h->esym.ifd == -2)
3740 h->esym.jmptbl = 0;
3741 h->esym.cobol_main = 0;
3742 h->esym.weakext = 0;
3743 h->esym.reserved = 0;
3744 h->esym.ifd = ifdNil;
3745 h->esym.asym.value = 0;
3746 h->esym.asym.st = stGlobal;
3748 if (SGI_COMPAT (einfo->abfd)
3749 && (h->root.root.type == bfd_link_hash_undefined
3750 || h->root.root.type == bfd_link_hash_undefweak))
3752 const char *name;
3754 /* Use undefined class. Also, set class and type for some
3755 special symbols. */
3756 name = h->root.root.root.string;
3757 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3758 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3760 h->esym.asym.sc = scData;
3761 h->esym.asym.st = stLabel;
3762 h->esym.asym.value = 0;
3764 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3766 h->esym.asym.sc = scAbs;
3767 h->esym.asym.st = stLabel;
3768 h->esym.asym.value =
3769 mips_elf_hash_table (einfo->info)->procedure_count;
3771 else if (strcmp (name, "_gp_disp") == 0)
3773 h->esym.asym.sc = scAbs;
3774 h->esym.asym.st = stLabel;
3775 h->esym.asym.value = elf_gp (einfo->abfd);
3777 else
3778 h->esym.asym.sc = scUndefined;
3780 else if (h->root.root.type != bfd_link_hash_defined
3781 && h->root.root.type != bfd_link_hash_defweak)
3782 h->esym.asym.sc = scAbs;
3783 else
3785 const char *name;
3787 sec = h->root.root.u.def.section;
3788 output_section = sec->output_section;
3790 /* When making a shared library and symbol h is the one from
3791 the another shared library, OUTPUT_SECTION may be null. */
3792 if (output_section == NULL)
3793 h->esym.asym.sc = scUndefined;
3794 else
3796 name = bfd_section_name (output_section->owner, output_section);
3798 if (strcmp (name, ".text") == 0)
3799 h->esym.asym.sc = scText;
3800 else if (strcmp (name, ".data") == 0)
3801 h->esym.asym.sc = scData;
3802 else if (strcmp (name, ".sdata") == 0)
3803 h->esym.asym.sc = scSData;
3804 else if (strcmp (name, ".rodata") == 0
3805 || strcmp (name, ".rdata") == 0)
3806 h->esym.asym.sc = scRData;
3807 else if (strcmp (name, ".bss") == 0)
3808 h->esym.asym.sc = scBss;
3809 else if (strcmp (name, ".sbss") == 0)
3810 h->esym.asym.sc = scSBss;
3811 else if (strcmp (name, ".init") == 0)
3812 h->esym.asym.sc = scInit;
3813 else if (strcmp (name, ".fini") == 0)
3814 h->esym.asym.sc = scFini;
3815 else
3816 h->esym.asym.sc = scAbs;
3820 h->esym.asym.reserved = 0;
3821 h->esym.asym.index = indexNil;
3824 if (h->root.root.type == bfd_link_hash_common)
3825 h->esym.asym.value = h->root.root.u.c.size;
3826 else if (h->root.root.type == bfd_link_hash_defined
3827 || h->root.root.type == bfd_link_hash_defweak)
3829 if (h->esym.asym.sc == scCommon)
3830 h->esym.asym.sc = scBss;
3831 else if (h->esym.asym.sc == scSCommon)
3832 h->esym.asym.sc = scSBss;
3834 sec = h->root.root.u.def.section;
3835 output_section = sec->output_section;
3836 if (output_section != NULL)
3837 h->esym.asym.value = (h->root.root.u.def.value
3838 + sec->output_offset
3839 + output_section->vma);
3840 else
3841 h->esym.asym.value = 0;
3843 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3845 /* Set type and value for a symbol with a function stub. */
3846 h->esym.asym.st = stProc;
3847 sec = h->root.root.u.def.section;
3848 if (sec == NULL)
3849 h->esym.asym.value = 0;
3850 else
3852 output_section = sec->output_section;
3853 if (output_section != NULL)
3854 h->esym.asym.value = (h->root.plt.offset
3855 + sec->output_offset
3856 + output_section->vma);
3857 else
3858 h->esym.asym.value = 0;
3860 #if 0 /* FIXME? */
3861 h->esym.ifd = 0;
3862 #endif
3865 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3866 h->root.root.root.string,
3867 &h->esym))
3869 einfo->failed = true;
3870 return false;
3873 return true;
3876 /* Create a runtime procedure table from the .mdebug section. */
3878 static boolean
3879 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3880 PTR handle;
3881 bfd *abfd;
3882 struct bfd_link_info *info;
3883 asection *s;
3884 struct ecoff_debug_info *debug;
3886 const struct ecoff_debug_swap *swap;
3887 HDRR *hdr = &debug->symbolic_header;
3888 RPDR *rpdr, *rp;
3889 struct rpdr_ext *erp;
3890 PTR rtproc;
3891 struct pdr_ext *epdr;
3892 struct sym_ext *esym;
3893 char *ss, **sv;
3894 char *str;
3895 unsigned long size, count;
3896 unsigned long sindex;
3897 unsigned long i;
3898 PDR pdr;
3899 SYMR sym;
3900 const char *no_name_func = _("static procedure (no name)");
3902 epdr = NULL;
3903 rpdr = NULL;
3904 esym = NULL;
3905 ss = NULL;
3906 sv = NULL;
3908 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3910 sindex = strlen (no_name_func) + 1;
3911 count = hdr->ipdMax;
3912 if (count > 0)
3914 size = swap->external_pdr_size;
3916 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3917 if (epdr == NULL)
3918 goto error_return;
3920 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3921 goto error_return;
3923 size = sizeof (RPDR);
3924 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3925 if (rpdr == NULL)
3926 goto error_return;
3928 sv = (char **) bfd_malloc (sizeof (char *) * count);
3929 if (sv == NULL)
3930 goto error_return;
3932 count = hdr->isymMax;
3933 size = swap->external_sym_size;
3934 esym = (struct sym_ext *) bfd_malloc (size * count);
3935 if (esym == NULL)
3936 goto error_return;
3938 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3939 goto error_return;
3941 count = hdr->issMax;
3942 ss = (char *) bfd_malloc (count);
3943 if (ss == NULL)
3944 goto error_return;
3945 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3946 goto error_return;
3948 count = hdr->ipdMax;
3949 for (i = 0; i < count; i++, rp++)
3951 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3952 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3953 rp->adr = sym.value;
3954 rp->regmask = pdr.regmask;
3955 rp->regoffset = pdr.regoffset;
3956 rp->fregmask = pdr.fregmask;
3957 rp->fregoffset = pdr.fregoffset;
3958 rp->frameoffset = pdr.frameoffset;
3959 rp->framereg = pdr.framereg;
3960 rp->pcreg = pdr.pcreg;
3961 rp->irpss = sindex;
3962 sv[i] = ss + sym.iss;
3963 sindex += strlen (sv[i]) + 1;
3967 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3968 size = BFD_ALIGN (size, 16);
3969 rtproc = (PTR) bfd_alloc (abfd, size);
3970 if (rtproc == NULL)
3972 mips_elf_hash_table (info)->procedure_count = 0;
3973 goto error_return;
3976 mips_elf_hash_table (info)->procedure_count = count + 2;
3978 erp = (struct rpdr_ext *) rtproc;
3979 memset (erp, 0, sizeof (struct rpdr_ext));
3980 erp++;
3981 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3982 strcpy (str, no_name_func);
3983 str += strlen (no_name_func) + 1;
3984 for (i = 0; i < count; i++)
3986 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3987 strcpy (str, sv[i]);
3988 str += strlen (sv[i]) + 1;
3990 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3992 /* Set the size and contents of .rtproc section. */
3993 s->_raw_size = size;
3994 s->contents = (bfd_byte *) rtproc;
3996 /* Skip this section later on (I don't think this currently
3997 matters, but someday it might). */
3998 s->link_order_head = (struct bfd_link_order *) NULL;
4000 if (epdr != NULL)
4001 free (epdr);
4002 if (rpdr != NULL)
4003 free (rpdr);
4004 if (esym != NULL)
4005 free (esym);
4006 if (ss != NULL)
4007 free (ss);
4008 if (sv != NULL)
4009 free (sv);
4011 return true;
4013 error_return:
4014 if (epdr != NULL)
4015 free (epdr);
4016 if (rpdr != NULL)
4017 free (rpdr);
4018 if (esym != NULL)
4019 free (esym);
4020 if (ss != NULL)
4021 free (ss);
4022 if (sv != NULL)
4023 free (sv);
4024 return false;
4027 /* A comparison routine used to sort .gptab entries. */
4029 static int
4030 gptab_compare (p1, p2)
4031 const PTR p1;
4032 const PTR p2;
4034 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4035 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4037 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4040 /* We need to use a special link routine to handle the .reginfo and
4041 the .mdebug sections. We need to merge all instances of these
4042 sections together, not write them all out sequentially. */
4044 static boolean
4045 mips_elf_final_link (abfd, info)
4046 bfd *abfd;
4047 struct bfd_link_info *info;
4049 asection **secpp;
4050 asection *o;
4051 struct bfd_link_order *p;
4052 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4053 asection *rtproc_sec;
4054 Elf32_RegInfo reginfo;
4055 struct ecoff_debug_info debug;
4056 const struct ecoff_debug_swap *swap
4057 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4058 HDRR *symhdr = &debug.symbolic_header;
4059 PTR mdebug_handle = NULL;
4061 /* Drop the .options section, since it has special semantics which I
4062 haven't bothered to figure out. */
4063 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4065 if (strcmp ((*secpp)->name, ".options") == 0)
4067 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4068 if (p->type == bfd_indirect_link_order)
4069 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4070 (*secpp)->link_order_head = NULL;
4071 *secpp = (*secpp)->next;
4072 --abfd->section_count;
4073 break;
4077 /* Get a value for the GP register. */
4078 if (elf_gp (abfd) == 0)
4080 struct bfd_link_hash_entry *h;
4082 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4083 if (h != (struct bfd_link_hash_entry *) NULL
4084 && h->type == bfd_link_hash_defined)
4085 elf_gp (abfd) = (h->u.def.value
4086 + h->u.def.section->output_section->vma
4087 + h->u.def.section->output_offset);
4088 else if (info->relocateable)
4090 bfd_vma lo;
4092 /* Make up a value. */
4093 lo = (bfd_vma) -1;
4094 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4096 if (o->vma < lo
4097 && (strcmp (o->name, ".sbss") == 0
4098 || strcmp (o->name, ".sdata") == 0
4099 || strcmp (o->name, ".lit4") == 0
4100 || strcmp (o->name, ".lit8") == 0))
4101 lo = o->vma;
4103 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4105 else
4107 /* If the relocate_section function needs to do a reloc
4108 involving the GP value, it should make a reloc_dangerous
4109 callback to warn that GP is not defined. */
4113 /* Go through the sections and collect the .reginfo and .mdebug
4114 information. */
4115 reginfo_sec = NULL;
4116 mdebug_sec = NULL;
4117 gptab_data_sec = NULL;
4118 gptab_bss_sec = NULL;
4119 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4121 if (strcmp (o->name, ".reginfo") == 0)
4123 memset (&reginfo, 0, sizeof reginfo);
4125 /* We have found the .reginfo section in the output file.
4126 Look through all the link_orders comprising it and merge
4127 the information together. */
4128 for (p = o->link_order_head;
4129 p != (struct bfd_link_order *) NULL;
4130 p = p->next)
4132 asection *input_section;
4133 bfd *input_bfd;
4134 Elf32_External_RegInfo ext;
4135 Elf32_RegInfo sub;
4137 if (p->type != bfd_indirect_link_order)
4139 if (p->type == bfd_fill_link_order)
4140 continue;
4141 abort ();
4144 input_section = p->u.indirect.section;
4145 input_bfd = input_section->owner;
4147 /* The linker emulation code has probably clobbered the
4148 size to be zero bytes. */
4149 if (input_section->_raw_size == 0)
4150 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4152 if (! bfd_get_section_contents (input_bfd, input_section,
4153 (PTR) &ext,
4154 (file_ptr) 0,
4155 sizeof ext))
4156 return false;
4158 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4160 reginfo.ri_gprmask |= sub.ri_gprmask;
4161 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4162 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4163 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4164 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4166 /* ri_gp_value is set by the function
4167 mips_elf32_section_processing when the section is
4168 finally written out. */
4170 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4171 elf_link_input_bfd ignores this section. */
4172 input_section->flags &=~ SEC_HAS_CONTENTS;
4175 /* Size has been set in mips_elf_always_size_sections */
4176 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4178 /* Skip this section later on (I don't think this currently
4179 matters, but someday it might). */
4180 o->link_order_head = (struct bfd_link_order *) NULL;
4182 reginfo_sec = o;
4185 if (strcmp (o->name, ".mdebug") == 0)
4187 struct extsym_info einfo;
4189 /* We have found the .mdebug section in the output file.
4190 Look through all the link_orders comprising it and merge
4191 the information together. */
4192 symhdr->magic = swap->sym_magic;
4193 /* FIXME: What should the version stamp be? */
4194 symhdr->vstamp = 0;
4195 symhdr->ilineMax = 0;
4196 symhdr->cbLine = 0;
4197 symhdr->idnMax = 0;
4198 symhdr->ipdMax = 0;
4199 symhdr->isymMax = 0;
4200 symhdr->ioptMax = 0;
4201 symhdr->iauxMax = 0;
4202 symhdr->issMax = 0;
4203 symhdr->issExtMax = 0;
4204 symhdr->ifdMax = 0;
4205 symhdr->crfd = 0;
4206 symhdr->iextMax = 0;
4208 /* We accumulate the debugging information itself in the
4209 debug_info structure. */
4210 debug.line = NULL;
4211 debug.external_dnr = NULL;
4212 debug.external_pdr = NULL;
4213 debug.external_sym = NULL;
4214 debug.external_opt = NULL;
4215 debug.external_aux = NULL;
4216 debug.ss = NULL;
4217 debug.ssext = debug.ssext_end = NULL;
4218 debug.external_fdr = NULL;
4219 debug.external_rfd = NULL;
4220 debug.external_ext = debug.external_ext_end = NULL;
4222 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4223 if (mdebug_handle == (PTR) NULL)
4224 return false;
4226 if (SGI_COMPAT (abfd))
4228 asection *s;
4229 EXTR esym;
4230 bfd_vma last;
4231 unsigned int i;
4232 static const char * const name[] =
4233 { ".text", ".init", ".fini", ".data",
4234 ".rodata", ".sdata", ".sbss", ".bss" };
4235 static const int sc[] = { scText, scInit, scFini, scData,
4236 scRData, scSData, scSBss, scBss };
4238 esym.jmptbl = 0;
4239 esym.cobol_main = 0;
4240 esym.weakext = 0;
4241 esym.reserved = 0;
4242 esym.ifd = ifdNil;
4243 esym.asym.iss = issNil;
4244 esym.asym.st = stLocal;
4245 esym.asym.reserved = 0;
4246 esym.asym.index = indexNil;
4247 last = 0;
4248 for (i = 0; i < 8; i++)
4250 esym.asym.sc = sc[i];
4251 s = bfd_get_section_by_name (abfd, name[i]);
4252 if (s != NULL)
4254 esym.asym.value = s->vma;
4255 last = s->vma + s->_raw_size;
4257 else
4258 esym.asym.value = last;
4260 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4261 name[i], &esym))
4262 return false;
4266 for (p = o->link_order_head;
4267 p != (struct bfd_link_order *) NULL;
4268 p = p->next)
4270 asection *input_section;
4271 bfd *input_bfd;
4272 const struct ecoff_debug_swap *input_swap;
4273 struct ecoff_debug_info input_debug;
4274 char *eraw_src;
4275 char *eraw_end;
4277 if (p->type != bfd_indirect_link_order)
4279 if (p->type == bfd_fill_link_order)
4280 continue;
4281 abort ();
4284 input_section = p->u.indirect.section;
4285 input_bfd = input_section->owner;
4287 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4288 || (get_elf_backend_data (input_bfd)
4289 ->elf_backend_ecoff_debug_swap) == NULL)
4291 /* I don't know what a non MIPS ELF bfd would be
4292 doing with a .mdebug section, but I don't really
4293 want to deal with it. */
4294 continue;
4297 input_swap = (get_elf_backend_data (input_bfd)
4298 ->elf_backend_ecoff_debug_swap);
4300 BFD_ASSERT (p->size == input_section->_raw_size);
4302 /* The ECOFF linking code expects that we have already
4303 read in the debugging information and set up an
4304 ecoff_debug_info structure, so we do that now. */
4305 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4306 &input_debug))
4307 return false;
4309 if (! (bfd_ecoff_debug_accumulate
4310 (mdebug_handle, abfd, &debug, swap, input_bfd,
4311 &input_debug, input_swap, info)))
4312 return false;
4314 /* Loop through the external symbols. For each one with
4315 interesting information, try to find the symbol in
4316 the linker global hash table and save the information
4317 for the output external symbols. */
4318 eraw_src = input_debug.external_ext;
4319 eraw_end = (eraw_src
4320 + (input_debug.symbolic_header.iextMax
4321 * input_swap->external_ext_size));
4322 for (;
4323 eraw_src < eraw_end;
4324 eraw_src += input_swap->external_ext_size)
4326 EXTR ext;
4327 const char *name;
4328 struct mips_elf_link_hash_entry *h;
4330 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4331 if (ext.asym.sc == scNil
4332 || ext.asym.sc == scUndefined
4333 || ext.asym.sc == scSUndefined)
4334 continue;
4336 name = input_debug.ssext + ext.asym.iss;
4337 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4338 name, false, false, true);
4339 if (h == NULL || h->esym.ifd != -2)
4340 continue;
4342 if (ext.ifd != -1)
4344 BFD_ASSERT (ext.ifd
4345 < input_debug.symbolic_header.ifdMax);
4346 ext.ifd = input_debug.ifdmap[ext.ifd];
4349 h->esym = ext;
4352 /* Free up the information we just read. */
4353 free (input_debug.line);
4354 free (input_debug.external_dnr);
4355 free (input_debug.external_pdr);
4356 free (input_debug.external_sym);
4357 free (input_debug.external_opt);
4358 free (input_debug.external_aux);
4359 free (input_debug.ss);
4360 free (input_debug.ssext);
4361 free (input_debug.external_fdr);
4362 free (input_debug.external_rfd);
4363 free (input_debug.external_ext);
4365 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4366 elf_link_input_bfd ignores this section. */
4367 input_section->flags &=~ SEC_HAS_CONTENTS;
4370 if (SGI_COMPAT (abfd) && info->shared)
4372 /* Create .rtproc section. */
4373 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4374 if (rtproc_sec == NULL)
4376 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4377 | SEC_LINKER_CREATED | SEC_READONLY);
4379 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4380 if (rtproc_sec == NULL
4381 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4382 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4383 return false;
4386 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4387 info, rtproc_sec, &debug))
4388 return false;
4391 /* Build the external symbol information. */
4392 einfo.abfd = abfd;
4393 einfo.info = info;
4394 einfo.debug = &debug;
4395 einfo.swap = swap;
4396 einfo.failed = false;
4397 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4398 mips_elf_output_extsym,
4399 (PTR) &einfo);
4400 if (einfo.failed)
4401 return false;
4403 /* Set the size of the .mdebug section. */
4404 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4406 /* Skip this section later on (I don't think this currently
4407 matters, but someday it might). */
4408 o->link_order_head = (struct bfd_link_order *) NULL;
4410 mdebug_sec = o;
4413 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4415 const char *subname;
4416 unsigned int c;
4417 Elf32_gptab *tab;
4418 Elf32_External_gptab *ext_tab;
4419 unsigned int i;
4421 /* The .gptab.sdata and .gptab.sbss sections hold
4422 information describing how the small data area would
4423 change depending upon the -G switch. These sections
4424 not used in executables files. */
4425 if (! info->relocateable)
4427 asection **secpp;
4429 for (p = o->link_order_head;
4430 p != (struct bfd_link_order *) NULL;
4431 p = p->next)
4433 asection *input_section;
4435 if (p->type != bfd_indirect_link_order)
4437 if (p->type == bfd_fill_link_order)
4438 continue;
4439 abort ();
4442 input_section = p->u.indirect.section;
4444 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4445 elf_link_input_bfd ignores this section. */
4446 input_section->flags &=~ SEC_HAS_CONTENTS;
4449 /* Skip this section later on (I don't think this
4450 currently matters, but someday it might). */
4451 o->link_order_head = (struct bfd_link_order *) NULL;
4453 /* Really remove the section. */
4454 for (secpp = &abfd->sections;
4455 *secpp != o;
4456 secpp = &(*secpp)->next)
4458 *secpp = (*secpp)->next;
4459 --abfd->section_count;
4461 continue;
4464 /* There is one gptab for initialized data, and one for
4465 uninitialized data. */
4466 if (strcmp (o->name, ".gptab.sdata") == 0)
4467 gptab_data_sec = o;
4468 else if (strcmp (o->name, ".gptab.sbss") == 0)
4469 gptab_bss_sec = o;
4470 else
4472 (*_bfd_error_handler)
4473 (_("%s: illegal section name `%s'"),
4474 bfd_get_filename (abfd), o->name);
4475 bfd_set_error (bfd_error_nonrepresentable_section);
4476 return false;
4479 /* The linker script always combines .gptab.data and
4480 .gptab.sdata into .gptab.sdata, and likewise for
4481 .gptab.bss and .gptab.sbss. It is possible that there is
4482 no .sdata or .sbss section in the output file, in which
4483 case we must change the name of the output section. */
4484 subname = o->name + sizeof ".gptab" - 1;
4485 if (bfd_get_section_by_name (abfd, subname) == NULL)
4487 if (o == gptab_data_sec)
4488 o->name = ".gptab.data";
4489 else
4490 o->name = ".gptab.bss";
4491 subname = o->name + sizeof ".gptab" - 1;
4492 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4495 /* Set up the first entry. */
4496 c = 1;
4497 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4498 if (tab == NULL)
4499 return false;
4500 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4501 tab[0].gt_header.gt_unused = 0;
4503 /* Combine the input sections. */
4504 for (p = o->link_order_head;
4505 p != (struct bfd_link_order *) NULL;
4506 p = p->next)
4508 asection *input_section;
4509 bfd *input_bfd;
4510 bfd_size_type size;
4511 unsigned long last;
4512 bfd_size_type gpentry;
4514 if (p->type != bfd_indirect_link_order)
4516 if (p->type == bfd_fill_link_order)
4517 continue;
4518 abort ();
4521 input_section = p->u.indirect.section;
4522 input_bfd = input_section->owner;
4524 /* Combine the gptab entries for this input section one
4525 by one. We know that the input gptab entries are
4526 sorted by ascending -G value. */
4527 size = bfd_section_size (input_bfd, input_section);
4528 last = 0;
4529 for (gpentry = sizeof (Elf32_External_gptab);
4530 gpentry < size;
4531 gpentry += sizeof (Elf32_External_gptab))
4533 Elf32_External_gptab ext_gptab;
4534 Elf32_gptab int_gptab;
4535 unsigned long val;
4536 unsigned long add;
4537 boolean exact;
4538 unsigned int look;
4540 if (! (bfd_get_section_contents
4541 (input_bfd, input_section, (PTR) &ext_gptab,
4542 gpentry, sizeof (Elf32_External_gptab))))
4544 free (tab);
4545 return false;
4548 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4549 &int_gptab);
4550 val = int_gptab.gt_entry.gt_g_value;
4551 add = int_gptab.gt_entry.gt_bytes - last;
4553 exact = false;
4554 for (look = 1; look < c; look++)
4556 if (tab[look].gt_entry.gt_g_value >= val)
4557 tab[look].gt_entry.gt_bytes += add;
4559 if (tab[look].gt_entry.gt_g_value == val)
4560 exact = true;
4563 if (! exact)
4565 Elf32_gptab *new_tab;
4566 unsigned int max;
4568 /* We need a new table entry. */
4569 new_tab = ((Elf32_gptab *)
4570 bfd_realloc ((PTR) tab,
4571 (c + 1) * sizeof (Elf32_gptab)));
4572 if (new_tab == NULL)
4574 free (tab);
4575 return false;
4577 tab = new_tab;
4578 tab[c].gt_entry.gt_g_value = val;
4579 tab[c].gt_entry.gt_bytes = add;
4581 /* Merge in the size for the next smallest -G
4582 value, since that will be implied by this new
4583 value. */
4584 max = 0;
4585 for (look = 1; look < c; look++)
4587 if (tab[look].gt_entry.gt_g_value < val
4588 && (max == 0
4589 || (tab[look].gt_entry.gt_g_value
4590 > tab[max].gt_entry.gt_g_value)))
4591 max = look;
4593 if (max != 0)
4594 tab[c].gt_entry.gt_bytes +=
4595 tab[max].gt_entry.gt_bytes;
4597 ++c;
4600 last = int_gptab.gt_entry.gt_bytes;
4603 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4604 elf_link_input_bfd ignores this section. */
4605 input_section->flags &=~ SEC_HAS_CONTENTS;
4608 /* The table must be sorted by -G value. */
4609 if (c > 2)
4610 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4612 /* Swap out the table. */
4613 ext_tab = ((Elf32_External_gptab *)
4614 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4615 if (ext_tab == NULL)
4617 free (tab);
4618 return false;
4621 for (i = 0; i < c; i++)
4622 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4623 free (tab);
4625 o->_raw_size = c * sizeof (Elf32_External_gptab);
4626 o->contents = (bfd_byte *) ext_tab;
4628 /* Skip this section later on (I don't think this currently
4629 matters, but someday it might). */
4630 o->link_order_head = (struct bfd_link_order *) NULL;
4634 /* Invoke the regular ELF backend linker to do all the work. */
4635 if (! bfd_elf32_bfd_final_link (abfd, info))
4636 return false;
4638 /* Now write out the computed sections. */
4640 if (reginfo_sec != (asection *) NULL)
4642 Elf32_External_RegInfo ext;
4644 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4645 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4646 (file_ptr) 0, sizeof ext))
4647 return false;
4650 if (mdebug_sec != (asection *) NULL)
4652 BFD_ASSERT (abfd->output_has_begun);
4653 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4654 swap, info,
4655 mdebug_sec->filepos))
4656 return false;
4658 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4661 if (gptab_data_sec != (asection *) NULL)
4663 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4664 gptab_data_sec->contents,
4665 (file_ptr) 0,
4666 gptab_data_sec->_raw_size))
4667 return false;
4670 if (gptab_bss_sec != (asection *) NULL)
4672 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4673 gptab_bss_sec->contents,
4674 (file_ptr) 0,
4675 gptab_bss_sec->_raw_size))
4676 return false;
4679 if (SGI_COMPAT (abfd))
4681 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4682 if (rtproc_sec != NULL)
4684 if (! bfd_set_section_contents (abfd, rtproc_sec,
4685 rtproc_sec->contents,
4686 (file_ptr) 0,
4687 rtproc_sec->_raw_size))
4688 return false;
4692 return true;
4695 /* Handle a MIPS ELF HI16 reloc. */
4697 static void
4698 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4699 bfd *input_bfd;
4700 Elf_Internal_Rela *relhi;
4701 Elf_Internal_Rela *rello;
4702 bfd_byte *contents;
4703 bfd_vma addend;
4705 bfd_vma insn;
4706 bfd_vma addlo;
4708 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4710 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4711 addlo &= 0xffff;
4713 addend += ((insn & 0xffff) << 16) + addlo;
4715 if ((addlo & 0x8000) != 0)
4716 addend -= 0x10000;
4717 if ((addend & 0x8000) != 0)
4718 addend += 0x10000;
4720 bfd_put_32 (input_bfd,
4721 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4722 contents + relhi->r_offset);
4725 /* Handle a MIPS ELF local GOT16 reloc. */
4727 static boolean
4728 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4729 contents, addend)
4730 bfd *output_bfd;
4731 bfd *input_bfd;
4732 asection *sgot;
4733 Elf_Internal_Rela *relhi;
4734 Elf_Internal_Rela *rello;
4735 bfd_byte *contents;
4736 bfd_vma addend;
4738 unsigned int assigned_gotno;
4739 unsigned int i;
4740 bfd_vma insn;
4741 bfd_vma addlo;
4742 bfd_vma address;
4743 bfd_vma hipage;
4744 bfd_byte *got_contents;
4745 struct mips_got_info *g;
4747 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4749 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4750 addlo &= 0xffff;
4752 addend += ((insn & 0xffff) << 16) + addlo;
4754 if ((addlo & 0x8000) != 0)
4755 addend -= 0x10000;
4756 if ((addend & 0x8000) != 0)
4757 addend += 0x10000;
4759 /* Get a got entry representing requested hipage. */
4760 BFD_ASSERT (elf_section_data (sgot) != NULL);
4761 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4762 BFD_ASSERT (g != NULL);
4764 assigned_gotno = g->assigned_gotno;
4765 got_contents = sgot->contents;
4766 hipage = addend & 0xffff0000;
4768 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4770 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4771 if (hipage == (address & 0xffff0000))
4772 break;
4775 if (i == assigned_gotno)
4777 if (assigned_gotno >= g->local_gotno)
4779 (*_bfd_error_handler)
4780 (_("more got entries are needed for hipage relocations"));
4781 bfd_set_error (bfd_error_bad_value);
4782 return false;
4785 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4786 ++g->assigned_gotno;
4789 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4790 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4791 contents + relhi->r_offset);
4793 return true;
4796 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4798 static void
4799 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4800 bfd *input_bfd;
4801 Elf_Internal_Rela *rel;
4802 bfd_byte *contents;
4803 bfd_vma offset;
4805 bfd_vma insn;
4807 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4808 bfd_put_32 (input_bfd,
4809 (insn & 0xffff0000) | (offset & 0xffff),
4810 contents + rel->r_offset);
4813 /* Relocate a MIPS ELF section. */
4815 static boolean
4816 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4817 contents, relocs, local_syms, local_sections)
4818 bfd *output_bfd;
4819 struct bfd_link_info *info;
4820 bfd *input_bfd;
4821 asection *input_section;
4822 bfd_byte *contents;
4823 Elf_Internal_Rela *relocs;
4824 Elf_Internal_Sym *local_syms;
4825 asection **local_sections;
4827 Elf_Internal_Shdr *symtab_hdr;
4828 size_t locsymcount;
4829 size_t extsymoff;
4830 asection *sgot, *sreloc, *scpt;
4831 bfd *dynobj;
4832 bfd_vma gp;
4833 Elf_Internal_Rela *rel;
4834 Elf_Internal_Rela *relend;
4835 struct mips_got_info *g;
4837 dynobj = elf_hash_table (info)->dynobj;
4838 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4840 sgot = NULL;
4841 sreloc = NULL;
4842 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4843 scpt = NULL;
4844 else
4845 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4846 g = NULL;
4848 if (elf_bad_symtab (input_bfd))
4850 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4851 extsymoff = 0;
4853 else
4855 locsymcount = symtab_hdr->sh_info;
4856 extsymoff = symtab_hdr->sh_info;
4859 gp = _bfd_get_gp_value (output_bfd);
4861 rel = relocs;
4862 relend = relocs + input_section->reloc_count;
4863 for (; rel < relend; rel++)
4865 int r_type;
4866 reloc_howto_type *howto;
4867 unsigned long r_symndx;
4868 bfd_vma addend;
4869 struct elf_link_hash_entry *h;
4870 asection *sec;
4871 Elf_Internal_Sym *sym;
4872 struct mips_elf_link_hash_entry *mh;
4873 int other;
4874 bfd_reloc_status_type r;
4876 r_type = ELF32_R_TYPE (rel->r_info);
4877 if (r_type == R_MIPS_GNU_VTINHERIT
4878 || r_type == R_MIPS_GNU_VTENTRY)
4879 continue;
4880 if ((r_type < 0 || r_type >= (int) R_MIPS_max)
4881 && r_type != R_MIPS16_26
4882 && r_type != R_MIPS16_GPREL)
4884 bfd_set_error (bfd_error_bad_value);
4885 return false;
4887 if (r_type == R_MIPS16_26)
4888 howto = &elf_mips16_jump_howto;
4889 else if (r_type == R_MIPS16_GPREL)
4890 howto = &elf_mips16_gprel_howto;
4891 else
4892 howto = elf_mips_howto_table + r_type;
4894 if (dynobj != NULL
4895 && (r_type == R_MIPS_CALL16
4896 || r_type == R_MIPS_GOT16
4897 || r_type == R_MIPS_CALL_HI16
4898 || r_type == R_MIPS_CALL_LO16
4899 || r_type == R_MIPS_GOT_HI16
4900 || r_type == R_MIPS_GOT_LO16))
4902 /* We need the .got section. */
4903 if (sgot == NULL)
4905 sgot = bfd_get_section_by_name (dynobj, ".got");
4906 BFD_ASSERT (sgot != NULL);
4907 BFD_ASSERT (elf_section_data (sgot) != NULL);
4908 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4909 BFD_ASSERT (g != NULL);
4913 r_symndx = ELF32_R_SYM (rel->r_info);
4915 /* Mix in the change in GP address for a GP relative reloc. */
4916 if (r_type != R_MIPS_GPREL16
4917 && r_type != R_MIPS_LITERAL
4918 && r_type != R_MIPS_GPREL32
4919 && r_type != R_MIPS16_GPREL)
4920 addend = 0;
4921 else
4923 if (gp == 0)
4925 if (! ((*info->callbacks->reloc_dangerous)
4926 (info,
4927 _("GP relative relocation when GP not defined"),
4928 input_bfd, input_section,
4929 rel->r_offset)))
4930 return false;
4931 /* Only give the error once per link. */
4932 gp = 4;
4933 _bfd_set_gp_value (output_bfd, gp);
4936 if (r_symndx < extsymoff
4937 || (elf_bad_symtab (input_bfd)
4938 && local_sections[r_symndx] != NULL))
4940 /* This is a relocation against a section. The current
4941 addend in the instruction is the difference between
4942 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4943 must change this to be the difference between the
4944 final definition (which will end up in RELOCATION)
4945 and the GP value of OUTPUT_BFD (which is in GP). */
4946 addend = elf_gp (input_bfd) - gp;
4948 else if (! info->relocateable)
4950 /* We are doing a final link. The current addend in the
4951 instruction is simply the desired offset into the
4952 symbol (normally zero). We want the instruction to
4953 hold the difference between the final definition of
4954 the symbol (which will end up in RELOCATION) and the
4955 GP value of OUTPUT_BFD (which is in GP). */
4956 addend = - gp;
4958 else
4960 /* We are generating relocateable output, and we aren't
4961 going to define this symbol, so we just leave the
4962 instruction alone. */
4963 addend = 0;
4967 h = NULL;
4968 sym = NULL;
4969 sec = NULL;
4970 if (info->relocateable)
4972 /* This is a relocateable link. We don't have to change
4973 anything, unless the reloc is against a section symbol,
4974 in which case we have to adjust according to where the
4975 section symbol winds up in the output section. */
4976 if (r_symndx >= locsymcount
4977 || (elf_bad_symtab (input_bfd)
4978 && local_sections[r_symndx] == NULL))
4979 r = bfd_reloc_ok;
4980 else
4982 sym = local_syms + r_symndx;
4983 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4984 r = bfd_reloc_ok;
4985 else
4987 sec = local_sections[r_symndx];
4989 /* It would be logical to add sym->st_value here,
4990 but Irix 5 sometimes generates a garbage symbol
4991 value. */
4992 addend += sec->output_offset;
4994 /* If this is HI16 or GOT16 with an associated LO16,
4995 adjust the addend accordingly. Otherwise, just
4996 relocate. */
4997 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4998 r = _bfd_relocate_contents (howto, input_bfd,
4999 addend,
5000 contents + rel->r_offset + 4);
5001 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
5002 r = _bfd_relocate_contents (howto, input_bfd,
5003 addend,
5004 contents + rel->r_offset);
5005 else
5007 Elf_Internal_Rela *lorel;
5009 /* As a GNU extension, permit an arbitrary
5010 number of R_MIPS_HI16 relocs before the
5011 R_MIPS_LO16 reloc. This permits gcc to emit
5012 the HI and LO relocs itself. */
5013 if (r_type == R_MIPS_GOT16)
5014 lorel = rel + 1;
5015 else
5017 for (lorel = rel + 1;
5018 (lorel < relend
5019 && (ELF32_R_TYPE (lorel->r_info)
5020 == R_MIPS_HI16));
5021 lorel++)
5024 if (lorel < relend
5025 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5027 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5028 contents, addend);
5029 r = bfd_reloc_ok;
5031 else
5032 r = _bfd_relocate_contents (howto, input_bfd,
5033 addend,
5034 contents + rel->r_offset);
5039 else
5041 bfd_vma relocation;
5042 boolean local;
5043 boolean undefined_error;
5045 /* This is a final link. */
5046 undefined_error = false;
5047 sym = NULL;
5048 if (r_symndx < extsymoff
5049 || (elf_bad_symtab (input_bfd)
5050 && local_sections[r_symndx] != NULL))
5052 local = true;
5053 sym = local_syms + r_symndx;
5054 sec = local_sections[r_symndx];
5055 relocation = (sec->output_section->vma
5056 + sec->output_offset);
5058 /* It would be logical to always add sym->st_value here,
5059 but Irix 5 sometimes generates a garbage symbol
5060 value. */
5061 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5062 relocation += sym->st_value;
5064 /* mips16 text labels should be treated as odd. */
5065 if (sym->st_other == STO_MIPS16)
5066 ++relocation;
5068 else
5070 long indx;
5072 local = false;
5073 indx = r_symndx - extsymoff;
5074 h = elf_sym_hashes (input_bfd)[indx];
5075 while (h->root.type == bfd_link_hash_indirect
5076 || h->root.type == bfd_link_hash_warning)
5077 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5078 if (strcmp (h->root.root.string, "_gp_disp") == 0)
5080 if (gp == 0)
5082 if (! ((*info->callbacks->reloc_dangerous)
5083 (info,
5084 _("_gp_disp used when GP not defined"),
5085 input_bfd, input_section,
5086 rel->r_offset)))
5087 return false;
5088 /* Only give the error once per link. */
5089 gp = 4;
5090 _bfd_set_gp_value (output_bfd, gp);
5091 relocation = 0;
5093 else
5095 sec = input_section;
5096 if (sec->output_section != NULL)
5097 relocation = (gp
5098 - (rel->r_offset
5099 + sec->output_section->vma
5100 + sec->output_offset));
5101 else
5102 relocation = gp - rel->r_offset;
5103 if (r_type == R_MIPS_LO16)
5104 relocation += 4;
5107 else if (h->root.type == bfd_link_hash_defined
5108 || h->root.type == bfd_link_hash_defweak)
5110 sec = h->root.u.def.section;
5111 if (sec->output_section == NULL)
5112 relocation = 0;
5113 else
5114 relocation = (h->root.u.def.value
5115 + sec->output_section->vma
5116 + sec->output_offset);
5118 else if (h->root.type == bfd_link_hash_undefweak)
5119 relocation = 0;
5120 else if (info->shared && !info->symbolic && !info->no_undefined)
5121 relocation = 0;
5122 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
5124 /* If this is a dynamic link, we should have created
5125 a _DYNAMIC_LINK symbol in
5126 mips_elf_create_dynamic_sections. Otherwise, we
5127 should define the symbol with a value of 0.
5128 FIXME: It should probably get into the symbol
5129 table somehow as well. */
5130 BFD_ASSERT (! info->shared);
5131 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
5132 ".dynamic") == NULL);
5133 relocation = 0;
5135 else
5137 if (! ((*info->callbacks->undefined_symbol)
5138 (info, h->root.root.string, input_bfd,
5139 input_section, rel->r_offset)))
5140 return false;
5141 undefined_error = true;
5142 relocation = 0;
5146 mh = (struct mips_elf_link_hash_entry *) h;
5147 if (h != NULL)
5148 other = h->other;
5149 else if (sym != NULL)
5150 other = sym->st_other;
5151 else
5152 other = 0;
5154 /* If this function has an fn_stub, then it is a mips16
5155 function which needs a stub if it is called by a 32 bit
5156 function. If this reloc is anything other than a 16 bit
5157 call, redirect the reloc to the stub. We don't redirect
5158 relocs from other stub functions. */
5159 if (r_type != R_MIPS16_26
5160 && ((mh != NULL
5161 && mh->fn_stub != NULL)
5162 || (mh == NULL
5163 && elf_tdata (input_bfd)->local_stubs != NULL
5164 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5165 && strncmp (bfd_get_section_name (input_bfd, input_section),
5166 FN_STUB, sizeof FN_STUB - 1) != 0
5167 && strncmp (bfd_get_section_name (input_bfd, input_section),
5168 CALL_STUB, sizeof CALL_STUB - 1) != 0
5169 && strncmp (bfd_get_section_name (input_bfd, input_section),
5170 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
5172 if (mh != NULL)
5174 BFD_ASSERT (mh->need_fn_stub);
5175 relocation = (mh->fn_stub->output_section->vma
5176 + mh->fn_stub->output_offset);
5178 else
5180 asection *fn_stub;
5182 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
5183 relocation = (fn_stub->output_section->vma
5184 + fn_stub->output_offset);
5187 /* RELOCATION now points to 32 bit code. */
5188 other = 0;
5191 /* If this function has a call_stub, then it is called by a
5192 mips16 function; the call needs to go through a stub if
5193 this function is a 32 bit function. If this reloc is a
5194 16 bit call, and the symbol is not a 16 bit function,
5195 then redirect the reloc to the stub. Note that we don't
5196 need to worry about calling the function through a
5197 function pointer; such calls are handled by routing
5198 through a special mips16 routine. We don't have to check
5199 whether this call is from a stub; it can't be, because a
5200 stub contains 32 bit code, and hence can not have a 16
5201 bit reloc. */
5202 if (r_type == R_MIPS16_26
5203 && mh != NULL
5204 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
5205 && other != STO_MIPS16)
5207 asection *stub;
5209 /* If both call_stub and call_fp_stub are defined, we
5210 can figure out which one to use by seeing which one
5211 appears in the input file. */
5212 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
5214 asection *o;
5216 stub = NULL;
5217 for (o = input_bfd->sections; o != NULL; o = o->next)
5219 if (strncmp (bfd_get_section_name (input_bfd, o),
5220 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5222 stub = mh->call_fp_stub;
5223 break;
5226 if (stub == NULL)
5227 stub = mh->call_stub;
5229 else if (mh->call_stub != NULL)
5230 stub = mh->call_stub;
5231 else
5232 stub = mh->call_fp_stub;
5234 BFD_ASSERT (stub->_raw_size > 0);
5235 relocation = stub->output_section->vma + stub->output_offset;
5238 if (r_type == R_MIPS_HI16)
5240 Elf_Internal_Rela *lorel;
5242 /* As a GNU extension, permit an arbitrary number of
5243 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
5244 This permits gcc to emit the HI and LO relocs itself. */
5245 for (lorel = rel + 1;
5246 (lorel < relend
5247 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
5248 lorel++)
5250 if (lorel < relend
5251 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5253 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5254 contents, relocation + addend);
5255 r = bfd_reloc_ok;
5257 else
5258 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5259 contents, rel->r_offset,
5260 relocation, addend);
5262 else if (r_type == R_MIPS_GOT16 && local)
5264 /* GOT16 must also have an associated LO16 in the local
5265 case. In this case, the addend is extracted and the
5266 section in which the referenced object is determined.
5267 Then the final address of the object is computed and
5268 the GOT entry for the hipage (an aligned 64kb chunk)
5269 is added to .got section if needed. The offset field
5270 of the GOT16-relocated instruction is replaced by the
5271 index of this GOT entry for the hipage. */
5272 if ((rel + 1) < relend
5273 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
5275 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
5276 sgot, rel, rel + 1,
5277 contents,
5278 relocation + addend))
5279 return false;
5280 r = bfd_reloc_ok;
5282 else
5283 r = bfd_reloc_outofrange;
5285 else if (r_type == R_MIPS_CALL16
5286 || r_type == R_MIPS_GOT16
5287 || r_type == R_MIPS_CALL_LO16
5288 || r_type == R_MIPS_GOT_LO16)
5290 bfd_vma offset;
5292 /* This symbol must be registered as a global symbol
5293 having the corresponding got entry. */
5294 BFD_ASSERT (h->got.offset != (bfd_vma) -1);
5296 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5297 BFD_ASSERT (g->local_gotno <= offset
5298 && offset < sgot->_raw_size);
5299 bfd_put_32 (output_bfd, relocation + addend,
5300 sgot->contents + offset);
5301 offset = (sgot->output_section->vma + sgot->output_offset
5302 + offset - gp);
5303 mips_elf_relocate_global_got (input_bfd, rel, contents,
5304 offset);
5305 r = bfd_reloc_ok;
5307 else if (r_type == R_MIPS_CALL_HI16
5308 || r_type == R_MIPS_GOT_HI16)
5310 bfd_vma offset;
5312 /* This must be a global symbol with a got entry. The
5313 next reloc must be the corresponding LO16 reloc. */
5314 BFD_ASSERT (h != NULL && h->got.offset != (bfd_vma) -1);
5315 BFD_ASSERT ((rel + 1) < relend);
5316 BFD_ASSERT ((int) ELF32_R_TYPE ((rel + 1)->r_info)
5317 == (r_type == R_MIPS_CALL_HI16
5318 ? (int) R_MIPS_CALL_LO16
5319 : (int) R_MIPS_GOT_LO16));
5321 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5322 BFD_ASSERT (g->local_gotno <= offset
5323 && offset < sgot->_raw_size);
5324 bfd_put_32 (output_bfd, relocation + addend,
5325 sgot->contents + offset);
5326 offset = (sgot->output_section->vma + sgot->output_offset
5327 + offset - gp);
5328 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5329 offset);
5330 r = bfd_reloc_ok;
5332 else if (r_type == R_MIPS_REL32
5333 || r_type == R_MIPS_32)
5335 Elf_Internal_Rel outrel;
5336 Elf32_crinfo cptrel;
5337 bfd_byte *cr;
5339 if ((info->shared
5340 || (elf_hash_table (info)->dynamic_sections_created
5341 && h != NULL
5342 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5343 == 0)))
5344 && (input_section->flags & SEC_ALLOC) != 0)
5346 boolean skip;
5348 /* When generating a shared object, these
5349 relocations are copied into the output file to be
5350 resolved at run time. */
5351 if (sreloc == NULL)
5353 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5354 BFD_ASSERT (sreloc != NULL);
5357 skip = false;
5359 if (elf_section_data (input_section)->stab_info == NULL)
5360 outrel.r_offset = rel->r_offset;
5361 else
5363 bfd_vma off;
5365 off = (_bfd_stab_section_offset
5366 (output_bfd, &elf_hash_table (info)->stab_info,
5367 input_section,
5368 &elf_section_data (input_section)->stab_info,
5369 rel->r_offset));
5370 if (off == (bfd_vma) -1)
5371 skip = true;
5372 outrel.r_offset = off;
5375 outrel.r_offset += (input_section->output_section->vma
5376 + input_section->output_offset);
5378 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5380 if (skip)
5381 memset (&outrel, 0, sizeof outrel);
5382 else if (h != NULL
5383 && (! info->symbolic
5384 || (h->elf_link_hash_flags
5385 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5387 BFD_ASSERT (h->dynindx != -1);
5388 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5389 sec = input_section;
5391 else
5393 long indx;
5395 if (h == NULL)
5396 sec = local_sections[r_symndx];
5397 else
5399 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5400 || (h->root.type
5401 == bfd_link_hash_defweak));
5402 sec = h->root.u.def.section;
5404 if (sec != NULL && bfd_is_abs_section (sec))
5405 indx = 0;
5406 else if (sec == NULL || sec->owner == NULL)
5408 bfd_set_error (bfd_error_bad_value);
5409 return false;
5411 else
5413 asection *osec;
5415 osec = sec->output_section;
5416 indx = elf_section_data (osec)->dynindx;
5417 if (indx == 0)
5418 abort ();
5421 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5422 addend += relocation;
5425 if (! skip)
5426 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5428 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5429 (((Elf32_External_Rel *)
5430 sreloc->contents)
5431 + sreloc->reloc_count));
5432 ++sreloc->reloc_count;
5434 if (! skip && SGI_COMPAT (output_bfd))
5436 if (scpt == NULL)
5437 continue;
5439 /* Make an entry of compact relocation info. */
5440 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5441 cptrel.vaddr = (rel->r_offset
5442 + input_section->output_section->vma
5443 + input_section->output_offset);
5444 if (r_type == R_MIPS_REL32)
5445 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5446 else
5447 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5448 mips_elf_set_cr_dist2to (cptrel, 0);
5449 cptrel.konst = addend;
5451 cr = (scpt->contents
5452 + sizeof (Elf32_External_compact_rel));
5453 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5454 ((Elf32_External_crinfo *) cr
5455 + scpt->reloc_count));
5456 ++scpt->reloc_count;
5459 /* This reloc will be computed at runtime, so
5460 there's no need to do anything now. */
5461 continue;
5463 else
5464 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5465 contents, rel->r_offset,
5466 relocation, addend);
5468 else if (r_type == R_MIPS_64)
5470 bfd_size_type addr;
5471 unsigned long val;
5473 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5474 addr = rel->r_offset;
5475 if (bfd_big_endian (input_bfd))
5476 addr += 4;
5477 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5478 contents, addr, relocation,
5479 addend);
5480 val = bfd_get_32 (input_bfd, contents + addr);
5481 if ((val & 0x80000000) != 0)
5482 val = 0xffffffff;
5483 else
5484 val = 0;
5485 addr = rel->r_offset;
5486 if (bfd_little_endian (input_bfd))
5487 addr += 4;
5488 bfd_put_32 (input_bfd, val, contents + addr);
5490 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5492 unsigned long insn;
5494 /* This is a jump to a mips16 routine from a mips32
5495 routine. We need to change jal into jalx. */
5496 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5497 if (((insn >> 26) & 0x3f) != 0x3
5498 && ((insn >> 26) & 0x3f) != 0x1d)
5500 (*_bfd_error_handler)
5501 (_("%s: %s+0x%lx: jump to mips16 routine which is not jal"),
5502 bfd_get_filename (input_bfd),
5503 input_section->name,
5504 (unsigned long) rel->r_offset);
5505 bfd_set_error (bfd_error_bad_value);
5506 return false;
5508 insn = (insn & 0x3ffffff) | (0x1d << 26);
5509 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5510 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5511 contents, rel->r_offset,
5512 relocation, addend);
5514 else if (r_type == R_MIPS16_26)
5516 /* It's easiest to do the normal relocation, and then
5517 dig out the instruction and swap the first word the
5518 way the mips16 expects it. If this is little endian,
5519 though, we need to swap the two words first, and then
5520 swap them back again later, so that the address looks
5521 right. */
5523 if (bfd_little_endian (input_bfd))
5525 unsigned long insn;
5527 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5528 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5529 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5532 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5533 contents, rel->r_offset,
5534 relocation, addend);
5535 if (r == bfd_reloc_ok)
5537 unsigned long insn;
5539 if (bfd_little_endian (input_bfd))
5541 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5542 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5543 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5546 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5547 insn = ((insn & 0xfc00)
5548 | ((insn & 0x1f) << 5)
5549 | ((insn & 0x3e0) >> 5));
5550 /* If this is a jump to a 32 bit routine, then make
5551 it jalx. */
5552 if (other != STO_MIPS16)
5553 insn |= 0x400;
5554 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5557 else if (r_type == R_MIPS16_GPREL)
5559 unsigned short extend, insn;
5560 bfd_byte buf[4];
5561 unsigned long final;
5563 /* Extract the addend into buf, run the regular reloc,
5564 and stuff the resulting value back into the
5565 instructions. */
5566 if (rel->r_offset > input_section->_raw_size)
5567 r = bfd_reloc_outofrange;
5568 else
5570 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5571 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5572 bfd_put_32 (input_bfd,
5573 (((extend & 0x1f) << 11)
5574 | (extend & 0x7e0)
5575 | (insn & 0x1f)),
5576 buf);
5577 r = _bfd_final_link_relocate (howto, input_bfd,
5578 input_section, buf,
5579 (bfd_vma) 0, relocation,
5580 addend);
5581 final = bfd_get_32 (input_bfd, buf);
5582 bfd_put_16 (input_bfd,
5583 ((extend & 0xf800)
5584 | ((final >> 11) & 0x1f)
5585 | (final & 0x7e0)),
5586 contents + rel->r_offset);
5587 bfd_put_16 (input_bfd,
5588 ((insn & 0xffe0)
5589 | (final & 0x1f)),
5590 contents + rel->r_offset + 2);
5593 else
5594 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5595 contents, rel->r_offset,
5596 relocation, addend);
5598 /* The jal instruction can only jump to an address which is
5599 divisible by 4, and it can only jump to an address with
5600 the same upper 4 bits as the PC. */
5601 if (r == bfd_reloc_ok
5602 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5604 bfd_vma addr;
5605 bfd_vma pc;
5606 bfd_vma target;
5608 pc = ((input_section->output_section->vma
5609 + input_section->output_offset
5610 + rel->r_offset)
5611 & 0xf0000000);
5612 target = bfd_get_32 (input_bfd, contents + rel->r_offset)
5613 & (howto->dst_mask);
5614 addr = (target << 2) + pc;
5615 if (other == STO_MIPS16)
5616 addr &= ~ (bfd_vma) 1;
5617 if ((addr & 3) != 0
5618 || ((addr & 0xf0000000)
5619 != pc))
5620 r = bfd_reloc_overflow;
5623 /* Don't bother to report a relocation overflow for a call
5624 to a weak undefined symbol with a value of zero. This
5625 permits us to use
5626 if (!f) f();
5627 even if we aren't in range to call address zero. */
5628 if (r == bfd_reloc_overflow
5629 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26)
5630 && relocation + addend == 0
5631 && h != NULL
5632 && h->root.type == bfd_link_hash_undefweak)
5633 r = bfd_reloc_ok;
5635 /* If we've already issued an error for an undefined symbol,
5636 don't issue another useless error. */
5637 if (undefined_error
5638 && (r == bfd_reloc_undefined || r == bfd_reloc_overflow))
5639 r = bfd_reloc_ok;
5641 if (SGI_COMPAT (abfd)
5642 && scpt != NULL
5643 && (input_section->flags & SEC_ALLOC) != 0)
5645 Elf32_crinfo cptrel;
5646 bfd_byte *cr;
5648 /* Make an entry of compact relocation info. */
5649 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5650 cptrel.vaddr = (rel->r_offset
5651 + input_section->output_section->vma
5652 + input_section->output_offset);
5654 switch (r_type)
5656 case R_MIPS_26:
5657 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5658 /* XXX How should we set dist2to in this case. */
5659 mips_elf_set_cr_dist2to (cptrel, 8);
5660 cptrel.konst = addend + relocation;
5661 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5662 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5663 ((Elf32_External_crinfo *) cr
5664 + scpt->reloc_count));
5665 ++scpt->reloc_count;
5666 break;
5668 case R_MIPS_GPREL16:
5669 case R_MIPS_LITERAL:
5670 case R_MIPS_GPREL32:
5671 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5672 cptrel.konst = gp - cptrel.vaddr;
5673 mips_elf_set_cr_dist2to (cptrel, 4);
5674 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5675 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5676 ((Elf32_External_crinfo *) cr
5677 + scpt->reloc_count));
5678 ++scpt->reloc_count;
5679 break;
5681 default:
5682 break;
5687 if (r != bfd_reloc_ok)
5689 switch (r)
5691 default:
5692 case bfd_reloc_outofrange:
5693 abort ();
5694 case bfd_reloc_overflow:
5696 const char *name;
5698 if (h != NULL)
5699 name = h->root.root.string;
5700 else
5702 name = bfd_elf_string_from_elf_section (input_bfd,
5703 symtab_hdr->sh_link,
5704 sym->st_name);
5705 if (name == NULL)
5706 return false;
5707 if (*name == '\0')
5708 name = bfd_section_name (input_bfd, sec);
5710 if (! ((*info->callbacks->reloc_overflow)
5711 (info, name, howto->name, (bfd_vma) 0,
5712 input_bfd, input_section, rel->r_offset)))
5713 return false;
5715 break;
5720 return true;
5723 /* This hook function is called before the linker writes out a global
5724 symbol. We mark symbols as small common if appropriate. This is
5725 also where we undo the increment of the value for a mips16 symbol. */
5727 /*ARGSIGNORED*/
5728 static boolean
5729 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5730 bfd *abfd;
5731 struct bfd_link_info *info;
5732 const char *name;
5733 Elf_Internal_Sym *sym;
5734 asection *input_sec;
5736 /* If we see a common symbol, which implies a relocatable link, then
5737 if a symbol was small common in an input file, mark it as small
5738 common in the output file. */
5739 if (sym->st_shndx == SHN_COMMON
5740 && strcmp (input_sec->name, ".scommon") == 0)
5741 sym->st_shndx = SHN_MIPS_SCOMMON;
5743 if (sym->st_other == STO_MIPS16
5744 && (sym->st_value & 1) != 0)
5745 --sym->st_value;
5747 return true;
5750 /* Functions for the dynamic linker. */
5752 /* The name of the dynamic interpreter. This is put in the .interp
5753 section. */
5755 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5757 /* Create dynamic sections when linking against a dynamic object. */
5759 static boolean
5760 mips_elf_create_dynamic_sections (abfd, info)
5761 bfd *abfd;
5762 struct bfd_link_info *info;
5764 struct elf_link_hash_entry *h;
5765 flagword flags;
5766 register asection *s;
5767 const char * const *namep;
5769 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5770 | SEC_LINKER_CREATED | SEC_READONLY);
5772 /* Mips ABI requests the .dynamic section to be read only. */
5773 s = bfd_get_section_by_name (abfd, ".dynamic");
5774 if (s != NULL)
5776 if (! bfd_set_section_flags (abfd, s, flags))
5777 return false;
5780 /* We need to create .got section. */
5781 if (! mips_elf_create_got_section (abfd, info))
5782 return false;
5784 /* Create .stub section. */
5785 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5787 s = bfd_make_section (abfd, ".stub");
5788 if (s == NULL
5789 || ! bfd_set_section_flags (abfd, s, flags)
5790 || ! bfd_set_section_alignment (abfd, s, 2))
5791 return false;
5794 if (SGI_COMPAT (abfd)
5795 && !info->shared
5796 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5798 s = bfd_make_section (abfd, ".rld_map");
5799 if (s == NULL
5800 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5801 || ! bfd_set_section_alignment (abfd, s, 2))
5802 return false;
5805 if (SGI_COMPAT (abfd))
5807 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5809 h = NULL;
5810 if (! (_bfd_generic_link_add_one_symbol
5811 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5812 (bfd_vma) 0, (const char *) NULL, false,
5813 get_elf_backend_data (abfd)->collect,
5814 (struct bfd_link_hash_entry **) &h)))
5815 return false;
5816 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5817 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5818 h->type = STT_SECTION;
5820 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5821 return false;
5824 /* We need to create a .compact_rel section. */
5825 if (! mips_elf_create_compact_rel_section (abfd, info))
5826 return false;
5828 /* Change aligments of some sections. */
5829 s = bfd_get_section_by_name (abfd, ".hash");
5830 if (s != NULL)
5831 bfd_set_section_alignment (abfd, s, 4);
5832 s = bfd_get_section_by_name (abfd, ".dynsym");
5833 if (s != NULL)
5834 bfd_set_section_alignment (abfd, s, 4);
5835 s = bfd_get_section_by_name (abfd, ".dynstr");
5836 if (s != NULL)
5837 bfd_set_section_alignment (abfd, s, 4);
5838 s = bfd_get_section_by_name (abfd, ".reginfo");
5839 if (s != NULL)
5840 bfd_set_section_alignment (abfd, s, 4);
5841 s = bfd_get_section_by_name (abfd, ".dynamic");
5842 if (s != NULL)
5843 bfd_set_section_alignment (abfd, s, 4);
5846 if (!info->shared)
5848 h = NULL;
5849 if (! (_bfd_generic_link_add_one_symbol
5850 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5851 (bfd_vma) 0, (const char *) NULL, false,
5852 get_elf_backend_data (abfd)->collect,
5853 (struct bfd_link_hash_entry **) &h)))
5854 return false;
5855 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5856 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5857 h->type = STT_SECTION;
5859 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5860 return false;
5862 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5864 /* __rld_map is a four byte word located in the .data section
5865 and is filled in by the rtld to contain a pointer to
5866 the _r_debug structure. Its symbol value will be set in
5867 mips_elf_finish_dynamic_symbol. */
5868 s = bfd_get_section_by_name (abfd, ".rld_map");
5869 BFD_ASSERT (s != NULL);
5871 h = NULL;
5872 if (! (_bfd_generic_link_add_one_symbol
5873 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5874 (bfd_vma) 0, (const char *) NULL, false,
5875 get_elf_backend_data (abfd)->collect,
5876 (struct bfd_link_hash_entry **) &h)))
5877 return false;
5878 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5879 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5880 h->type = STT_OBJECT;
5882 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5883 return false;
5887 return true;
5890 /* Create the .compact_rel section. */
5892 static boolean
5893 mips_elf_create_compact_rel_section (abfd, info)
5894 bfd *abfd;
5895 struct bfd_link_info *info;
5897 flagword flags;
5898 register asection *s;
5900 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5902 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5903 | SEC_READONLY);
5905 s = bfd_make_section (abfd, ".compact_rel");
5906 if (s == NULL
5907 || ! bfd_set_section_flags (abfd, s, flags)
5908 || ! bfd_set_section_alignment (abfd, s, 2))
5909 return false;
5911 s->_raw_size = sizeof (Elf32_External_compact_rel);
5914 return true;
5917 /* Create the .got section to hold the global offset table. */
5919 static boolean
5920 mips_elf_create_got_section (abfd, info)
5921 bfd *abfd;
5922 struct bfd_link_info *info;
5924 flagword flags;
5925 register asection *s;
5926 struct elf_link_hash_entry *h;
5927 struct mips_got_info *g;
5929 /* This function may be called more than once. */
5930 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5931 return true;
5933 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5934 | SEC_LINKER_CREATED);
5936 s = bfd_make_section (abfd, ".got");
5937 if (s == NULL
5938 || ! bfd_set_section_flags (abfd, s, flags)
5939 || ! bfd_set_section_alignment (abfd, s, 4))
5940 return false;
5942 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5943 linker script because we don't want to define the symbol if we
5944 are not creating a global offset table. */
5945 h = NULL;
5946 if (! (_bfd_generic_link_add_one_symbol
5947 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5948 (bfd_vma) 0, (const char *) NULL, false,
5949 get_elf_backend_data (abfd)->collect,
5950 (struct bfd_link_hash_entry **) &h)))
5951 return false;
5952 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5953 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5954 h->type = STT_OBJECT;
5956 if (info->shared
5957 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5958 return false;
5960 /* The first several global offset table entries are reserved. */
5961 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5963 g = (struct mips_got_info *) bfd_alloc (abfd,
5964 sizeof (struct mips_got_info));
5965 if (g == NULL)
5966 return false;
5967 g->global_gotsym = 0;
5968 g->local_gotno = MIPS_RESERVED_GOTNO;
5969 g->assigned_gotno = MIPS_RESERVED_GOTNO;
5970 if (elf_section_data (s) == NULL)
5972 s->used_by_bfd =
5973 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5974 if (elf_section_data (s) == NULL)
5975 return false;
5977 elf_section_data (s)->tdata = (PTR) g;
5979 return true;
5982 /* Look through the relocs for a section during the first phase, and
5983 allocate space in the global offset table. */
5985 static boolean
5986 mips_elf_check_relocs (abfd, info, sec, relocs)
5987 bfd *abfd;
5988 struct bfd_link_info *info;
5989 asection *sec;
5990 const Elf_Internal_Rela *relocs;
5992 const char *name;
5993 bfd *dynobj;
5994 Elf_Internal_Shdr *symtab_hdr;
5995 struct elf_link_hash_entry **sym_hashes;
5996 struct mips_got_info *g;
5997 size_t extsymoff;
5998 const Elf_Internal_Rela *rel;
5999 const Elf_Internal_Rela *rel_end;
6000 asection *sgot;
6001 asection *sreloc;
6003 if (info->relocateable)
6004 return true;
6006 dynobj = elf_hash_table (info)->dynobj;
6007 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6008 sym_hashes = elf_sym_hashes (abfd);
6009 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6011 /* Check for the mips16 stub sections. */
6013 name = bfd_get_section_name (abfd, sec);
6014 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
6016 unsigned long r_symndx;
6018 /* Look at the relocation information to figure out which symbol
6019 this is for. */
6021 r_symndx = ELF32_R_SYM (relocs->r_info);
6023 if (r_symndx < extsymoff
6024 || sym_hashes[r_symndx - extsymoff] == NULL)
6026 asection *o;
6028 /* This stub is for a local symbol. This stub will only be
6029 needed if there is some relocation in this BFD, other
6030 than a 16 bit function call, which refers to this symbol. */
6031 for (o = abfd->sections; o != NULL; o = o->next)
6033 Elf_Internal_Rela *sec_relocs;
6034 const Elf_Internal_Rela *r, *rend;
6036 /* We can ignore stub sections when looking for relocs. */
6037 if ((o->flags & SEC_RELOC) == 0
6038 || o->reloc_count == 0
6039 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6040 sizeof FN_STUB - 1) == 0
6041 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6042 sizeof CALL_STUB - 1) == 0
6043 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6044 sizeof CALL_FP_STUB - 1) == 0)
6045 continue;
6047 sec_relocs = (_bfd_elf32_link_read_relocs
6048 (abfd, o, (PTR) NULL,
6049 (Elf_Internal_Rela *) NULL,
6050 info->keep_memory));
6051 if (sec_relocs == NULL)
6052 return false;
6054 rend = sec_relocs + o->reloc_count;
6055 for (r = sec_relocs; r < rend; r++)
6056 if (ELF32_R_SYM (r->r_info) == r_symndx
6057 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6058 break;
6060 if (! info->keep_memory)
6061 free (sec_relocs);
6063 if (r < rend)
6064 break;
6067 if (o == NULL)
6069 /* There is no non-call reloc for this stub, so we do
6070 not need it. Since this function is called before
6071 the linker maps input sections to output sections, we
6072 can easily discard it by setting the SEC_EXCLUDE
6073 flag. */
6074 sec->flags |= SEC_EXCLUDE;
6075 return true;
6078 /* Record this stub in an array of local symbol stubs for
6079 this BFD. */
6080 if (elf_tdata (abfd)->local_stubs == NULL)
6082 unsigned long symcount;
6083 asection **n;
6085 if (elf_bad_symtab (abfd))
6086 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6087 else
6088 symcount = symtab_hdr->sh_info;
6089 n = (asection **) bfd_zalloc (abfd,
6090 symcount * sizeof (asection *));
6091 if (n == NULL)
6092 return false;
6093 elf_tdata (abfd)->local_stubs = n;
6096 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6098 /* We don't need to set mips16_stubs_seen in this case.
6099 That flag is used to see whether we need to look through
6100 the global symbol table for stubs. We don't need to set
6101 it here, because we just have a local stub. */
6103 else
6105 struct mips_elf_link_hash_entry *h;
6107 h = ((struct mips_elf_link_hash_entry *)
6108 sym_hashes[r_symndx - extsymoff]);
6110 /* H is the symbol this stub is for. */
6112 h->fn_stub = sec;
6113 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6116 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6117 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6119 unsigned long r_symndx;
6120 struct mips_elf_link_hash_entry *h;
6121 asection **loc;
6123 /* Look at the relocation information to figure out which symbol
6124 this is for. */
6126 r_symndx = ELF32_R_SYM (relocs->r_info);
6128 if (r_symndx < extsymoff
6129 || sym_hashes[r_symndx - extsymoff] == NULL)
6131 /* This stub was actually built for a static symbol defined
6132 in the same file. We assume that all static symbols in
6133 mips16 code are themselves mips16, so we can simply
6134 discard this stub. Since this function is called before
6135 the linker maps input sections to output sections, we can
6136 easily discard it by setting the SEC_EXCLUDE flag. */
6137 sec->flags |= SEC_EXCLUDE;
6138 return true;
6141 h = ((struct mips_elf_link_hash_entry *)
6142 sym_hashes[r_symndx - extsymoff]);
6144 /* H is the symbol this stub is for. */
6146 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6147 loc = &h->call_fp_stub;
6148 else
6149 loc = &h->call_stub;
6151 /* If we already have an appropriate stub for this function, we
6152 don't need another one, so we can discard this one. Since
6153 this function is called before the linker maps input sections
6154 to output sections, we can easily discard it by setting the
6155 SEC_EXCLUDE flag. We can also discard this section if we
6156 happen to already know that this is a mips16 function; it is
6157 not necessary to check this here, as it is checked later, but
6158 it is slightly faster to check now. */
6159 if (*loc != NULL || h->root.other == STO_MIPS16)
6161 sec->flags |= SEC_EXCLUDE;
6162 return true;
6165 *loc = sec;
6166 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6169 if (dynobj == NULL)
6171 sgot = NULL;
6172 g = NULL;
6174 else
6176 sgot = bfd_get_section_by_name (dynobj, ".got");
6177 if (sgot == NULL)
6178 g = NULL;
6179 else
6181 BFD_ASSERT (elf_section_data (sgot) != NULL);
6182 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6183 BFD_ASSERT (g != NULL);
6187 sreloc = NULL;
6189 rel_end = relocs + sec->reloc_count;
6190 for (rel = relocs; rel < rel_end; rel++)
6192 unsigned long r_symndx;
6193 struct elf_link_hash_entry *h;
6195 r_symndx = ELF32_R_SYM (rel->r_info);
6197 if (r_symndx < extsymoff)
6198 h = NULL;
6199 else
6201 h = sym_hashes[r_symndx - extsymoff];
6203 /* This may be an indirect symbol created because of a version. */
6204 if (h != NULL)
6206 while (h->root.type == bfd_link_hash_indirect)
6207 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6211 /* Some relocs require a global offset table. */
6212 if (dynobj == NULL || sgot == NULL)
6214 switch (ELF32_R_TYPE (rel->r_info))
6216 case R_MIPS_GOT16:
6217 case R_MIPS_CALL16:
6218 case R_MIPS_CALL_HI16:
6219 case R_MIPS_CALL_LO16:
6220 case R_MIPS_GOT_HI16:
6221 case R_MIPS_GOT_LO16:
6222 if (dynobj == NULL)
6223 elf_hash_table (info)->dynobj = dynobj = abfd;
6224 if (! mips_elf_create_got_section (dynobj, info))
6225 return false;
6226 sgot = bfd_get_section_by_name (dynobj, ".got");
6227 BFD_ASSERT (sgot != NULL);
6228 BFD_ASSERT (elf_section_data (sgot) != NULL);
6229 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6230 BFD_ASSERT (g != NULL);
6231 break;
6233 case R_MIPS_32:
6234 case R_MIPS_REL32:
6235 if (dynobj == NULL
6236 && (info->shared || h != NULL)
6237 && (sec->flags & SEC_ALLOC) != 0)
6238 elf_hash_table (info)->dynobj = dynobj = abfd;
6239 break;
6241 default:
6242 break;
6246 switch (ELF32_R_TYPE (rel->r_info))
6248 case R_MIPS_CALL16:
6249 case R_MIPS_CALL_HI16:
6250 case R_MIPS_CALL_LO16:
6251 /* This symbol requires a global offset table entry. */
6253 if (h == NULL)
6255 (*_bfd_error_handler)
6256 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6257 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6258 bfd_set_error (bfd_error_bad_value);
6259 return false;
6262 /* Make sure this symbol is output as a dynamic symbol. */
6263 if (h->dynindx == -1)
6265 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6266 return false;
6269 if (h->got.offset != (bfd_vma) -1)
6271 /* We have already allocated space in the .got. */
6272 break;
6275 /* Note the index of the first global got symbol in .dynsym. */
6276 if (g->global_gotsym == 0
6277 || g->global_gotsym > (unsigned long) h->dynindx)
6278 g->global_gotsym = h->dynindx;
6280 /* Make this symbol to have the corresponding got entry. */
6281 h->got.offset = 0;
6283 /* We need a stub, not a plt entry for the undefined
6284 function. But we record it as if it needs plt. See
6285 elf_adjust_dynamic_symbol in elflink.h. */
6286 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6287 h->type = STT_FUNC;
6289 break;
6291 case R_MIPS_GOT16:
6292 case R_MIPS_GOT_HI16:
6293 case R_MIPS_GOT_LO16:
6294 /* This symbol requires a global offset table entry. */
6296 if (h != NULL)
6298 /* Make sure this symbol is output as a dynamic symbol. */
6299 if (h->dynindx == -1)
6301 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6302 return false;
6305 if (h->got.offset != (bfd_vma) -1)
6307 /* We have already allocated space in the .got. */
6308 break;
6310 /* Note the index of the first global got symbol in
6311 .dynsym. */
6312 if (g->global_gotsym == 0
6313 || g->global_gotsym > (unsigned long) h->dynindx)
6314 g->global_gotsym = h->dynindx;
6316 /* Make this symbol to be the global got symbol. */
6317 h->got.offset = 0;
6320 break;
6322 case R_MIPS_32:
6323 case R_MIPS_REL32:
6324 if ((info->shared || h != NULL)
6325 && (sec->flags & SEC_ALLOC) != 0)
6327 if (sreloc == NULL)
6329 const char *name = ".rel.dyn";
6331 sreloc = bfd_get_section_by_name (dynobj, name);
6332 if (sreloc == NULL)
6334 sreloc = bfd_make_section (dynobj, name);
6335 if (sreloc == NULL
6336 || ! bfd_set_section_flags (dynobj, sreloc,
6337 (SEC_ALLOC
6338 | SEC_LOAD
6339 | SEC_HAS_CONTENTS
6340 | SEC_IN_MEMORY
6341 | SEC_LINKER_CREATED
6342 | SEC_READONLY))
6343 || ! bfd_set_section_alignment (dynobj, sreloc,
6345 return false;
6348 if (info->shared)
6350 /* When creating a shared object, we must copy these
6351 reloc types into the output file as R_MIPS_REL32
6352 relocs. We make room for this reloc in the
6353 .rel.dyn reloc section */
6354 if (sreloc->_raw_size == 0)
6356 /* Add a null element. */
6357 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6358 ++sreloc->reloc_count;
6360 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6362 else
6364 struct mips_elf_link_hash_entry *hmips;
6366 /* We only need to copy this reloc if the symbol is
6367 defined in a dynamic object. */
6368 hmips = (struct mips_elf_link_hash_entry *) h;
6369 ++hmips->mips_32_relocs;
6373 if (SGI_COMPAT (abfd))
6374 mips_elf_hash_table (info)->compact_rel_size +=
6375 sizeof (Elf32_External_crinfo);
6377 break;
6379 case R_MIPS_26:
6380 case R_MIPS_GPREL16:
6381 case R_MIPS_LITERAL:
6382 case R_MIPS_GPREL32:
6383 if (SGI_COMPAT (abfd))
6384 mips_elf_hash_table (info)->compact_rel_size +=
6385 sizeof (Elf32_External_crinfo);
6386 break;
6388 /* This relocation describes the C++ object vtable hierarchy.
6389 Reconstruct it for later use during GC. */
6390 case R_MIPS_GNU_VTINHERIT:
6391 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6392 return false;
6393 break;
6395 /* This relocation describes which C++ vtable entries are actually
6396 used. Record for later use during GC. */
6397 case R_MIPS_GNU_VTENTRY:
6398 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6399 return false;
6400 break;
6402 default:
6403 break;
6406 /* If this reloc is not a 16 bit call, and it has a global
6407 symbol, then we will need the fn_stub if there is one.
6408 References from a stub section do not count. */
6409 if (h != NULL
6410 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6411 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6412 sizeof FN_STUB - 1) != 0
6413 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6414 sizeof CALL_STUB - 1) != 0
6415 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6416 sizeof CALL_FP_STUB - 1) != 0)
6418 struct mips_elf_link_hash_entry *mh;
6420 mh = (struct mips_elf_link_hash_entry *) h;
6421 mh->need_fn_stub = true;
6425 return true;
6428 /* Return the section that should be marked against GC for a given
6429 relocation. */
6431 static asection *
6432 mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
6433 bfd *abfd;
6434 struct bfd_link_info *info;
6435 Elf_Internal_Rela *rel;
6436 struct elf_link_hash_entry *h;
6437 Elf_Internal_Sym *sym;
6439 /* ??? Do mips16 stub sections need to be handled special? */
6441 if (h != NULL)
6443 switch (ELF32_R_TYPE (rel->r_info))
6445 case R_MIPS_GNU_VTINHERIT:
6446 case R_MIPS_GNU_VTENTRY:
6447 break;
6449 default:
6450 switch (h->root.type)
6452 case bfd_link_hash_defined:
6453 case bfd_link_hash_defweak:
6454 return h->root.u.def.section;
6456 case bfd_link_hash_common:
6457 return h->root.u.c.p->section;
6459 default:
6460 break;
6464 else
6466 if (!(elf_bad_symtab (abfd)
6467 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6468 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
6469 && sym->st_shndx != SHN_COMMON))
6471 return bfd_section_from_elf_index (abfd, sym->st_shndx);
6475 return NULL;
6478 /* Update the got entry reference counts for the section being removed. */
6480 static boolean
6481 mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6482 bfd *abfd;
6483 struct bfd_link_info *info;
6484 asection *sec;
6485 const Elf_Internal_Rela *relocs;
6487 #if 0
6488 Elf_Internal_Shdr *symtab_hdr;
6489 struct elf_link_hash_entry **sym_hashes;
6490 bfd_signed_vma *local_got_refcounts;
6491 const Elf_Internal_Rela *rel, *relend;
6492 unsigned long r_symndx;
6493 struct elf_link_hash_entry *h;
6495 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6496 sym_hashes = elf_sym_hashes (abfd);
6497 local_got_refcounts = elf_local_got_refcounts (abfd);
6499 relend = relocs + sec->reloc_count;
6500 for (rel = relocs; rel < relend; rel++)
6501 switch (ELF32_R_TYPE (rel->r_info))
6503 case R_MIPS_GOT16:
6504 case R_MIPS_CALL16:
6505 case R_MIPS_CALL_HI16:
6506 case R_MIPS_CALL_LO16:
6507 case R_MIPS_GOT_HI16:
6508 case R_MIPS_GOT_LO16:
6509 /* ??? It would seem that the existing MIPS code does no sort
6510 of reference counting or whatnot on its GOT and PLT entries,
6511 so it is not possible to garbage collect them at this time. */
6512 break;
6514 default:
6515 break;
6517 #endif
6519 return true;
6523 /* Adjust a symbol defined by a dynamic object and referenced by a
6524 regular object. The current definition is in some section of the
6525 dynamic object, but we're not including those sections. We have to
6526 change the definition to something the rest of the link can
6527 understand. */
6529 static boolean
6530 mips_elf_adjust_dynamic_symbol (info, h)
6531 struct bfd_link_info *info;
6532 struct elf_link_hash_entry *h;
6534 bfd *dynobj;
6535 struct mips_elf_link_hash_entry *hmips;
6536 asection *s;
6538 dynobj = elf_hash_table (info)->dynobj;
6540 /* Make sure we know what is going on here. */
6541 BFD_ASSERT (dynobj != NULL
6542 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6543 || h->weakdef != NULL
6544 || ((h->elf_link_hash_flags
6545 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6546 && (h->elf_link_hash_flags
6547 & ELF_LINK_HASH_REF_REGULAR) != 0
6548 && (h->elf_link_hash_flags
6549 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6551 /* If this symbol is defined in a dynamic object, we need to copy
6552 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6553 file. */
6554 hmips = (struct mips_elf_link_hash_entry *) h;
6555 if (! info->relocateable
6556 && hmips->mips_32_relocs != 0
6557 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6559 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6560 BFD_ASSERT (s != NULL);
6562 if (s->_raw_size == 0)
6564 /* Make room for a null element. */
6565 s->_raw_size += sizeof (Elf32_External_Rel);
6566 ++s->reloc_count;
6568 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6571 /* For a function, create a stub, if needed. */
6572 if (h->type == STT_FUNC
6573 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6575 if (! elf_hash_table (info)->dynamic_sections_created)
6576 return true;
6578 /* If this symbol is not defined in a regular file, then set
6579 the symbol to the stub location. This is required to make
6580 function pointers compare as equal between the normal
6581 executable and the shared library. */
6582 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6584 /* We need .stub section. */
6585 s = bfd_get_section_by_name (dynobj, ".stub");
6586 BFD_ASSERT (s != NULL);
6588 h->root.u.def.section = s;
6589 h->root.u.def.value = s->_raw_size;
6591 /* XXX Write this stub address somewhere. */
6592 h->plt.offset = s->_raw_size;
6594 /* Make room for this stub code. */
6595 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6597 /* The last half word of the stub will be filled with the index
6598 of this symbol in .dynsym section. */
6599 return true;
6603 /* If this is a weak symbol, and there is a real definition, the
6604 processor independent code will have arranged for us to see the
6605 real definition first, and we can just use the same value. */
6606 if (h->weakdef != NULL)
6608 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6609 || h->weakdef->root.type == bfd_link_hash_defweak);
6610 h->root.u.def.section = h->weakdef->root.u.def.section;
6611 h->root.u.def.value = h->weakdef->root.u.def.value;
6612 return true;
6615 /* This is a reference to a symbol defined by a dynamic object which
6616 is not a function. */
6618 return true;
6621 /* This function is called after all the input files have been read,
6622 and the input sections have been assigned to output sections. We
6623 check for any mips16 stub sections that we can discard. */
6625 static boolean mips_elf_check_mips16_stubs
6626 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6628 static boolean
6629 mips_elf_always_size_sections (output_bfd, info)
6630 bfd *output_bfd;
6631 struct bfd_link_info *info;
6633 asection *ri;
6635 /* The .reginfo section has a fixed size. */
6636 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
6637 if (ri != NULL)
6638 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
6640 if (info->relocateable
6641 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6642 return true;
6644 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6645 mips_elf_check_mips16_stubs,
6646 (PTR) NULL);
6648 return true;
6651 /* Check the mips16 stubs for a particular symbol, and see if we can
6652 discard them. */
6654 /*ARGSUSED*/
6655 static boolean
6656 mips_elf_check_mips16_stubs (h, data)
6657 struct mips_elf_link_hash_entry *h;
6658 PTR data;
6660 if (h->fn_stub != NULL
6661 && ! h->need_fn_stub)
6663 /* We don't need the fn_stub; the only references to this symbol
6664 are 16 bit calls. Clobber the size to 0 to prevent it from
6665 being included in the link. */
6666 h->fn_stub->_raw_size = 0;
6667 h->fn_stub->_cooked_size = 0;
6668 h->fn_stub->flags &= ~ SEC_RELOC;
6669 h->fn_stub->reloc_count = 0;
6670 h->fn_stub->flags |= SEC_EXCLUDE;
6673 if (h->call_stub != NULL
6674 && h->root.other == STO_MIPS16)
6676 /* We don't need the call_stub; this is a 16 bit function, so
6677 calls from other 16 bit functions are OK. Clobber the size
6678 to 0 to prevent it from being included in the link. */
6679 h->call_stub->_raw_size = 0;
6680 h->call_stub->_cooked_size = 0;
6681 h->call_stub->flags &= ~ SEC_RELOC;
6682 h->call_stub->reloc_count = 0;
6683 h->call_stub->flags |= SEC_EXCLUDE;
6686 if (h->call_fp_stub != NULL
6687 && h->root.other == STO_MIPS16)
6689 /* We don't need the call_stub; this is a 16 bit function, so
6690 calls from other 16 bit functions are OK. Clobber the size
6691 to 0 to prevent it from being included in the link. */
6692 h->call_fp_stub->_raw_size = 0;
6693 h->call_fp_stub->_cooked_size = 0;
6694 h->call_fp_stub->flags &= ~ SEC_RELOC;
6695 h->call_fp_stub->reloc_count = 0;
6696 h->call_fp_stub->flags |= SEC_EXCLUDE;
6699 return true;
6702 /* Set the sizes of the dynamic sections. */
6704 static boolean
6705 mips_elf_size_dynamic_sections (output_bfd, info)
6706 bfd *output_bfd;
6707 struct bfd_link_info *info;
6709 bfd *dynobj;
6710 asection *s;
6711 boolean reltext;
6712 asection *sgot;
6713 struct mips_got_info *g;
6715 dynobj = elf_hash_table (info)->dynobj;
6716 BFD_ASSERT (dynobj != NULL);
6718 if (elf_hash_table (info)->dynamic_sections_created)
6720 /* Set the contents of the .interp section to the interpreter. */
6721 if (! info->shared)
6723 s = bfd_get_section_by_name (dynobj, ".interp");
6724 BFD_ASSERT (s != NULL);
6725 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6726 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6730 /* Recompute the size of .got for local entires (reserved and
6731 hipages) if needed. To estimate it, get the upper bound of total
6732 size of loadable sections. */
6733 sgot = bfd_get_section_by_name (dynobj, ".got");
6735 if (sgot != NULL)
6737 bfd_size_type loadable_size = 0;
6738 bfd_size_type local_gotno;
6739 struct _bfd *sub;
6741 BFD_ASSERT (elf_section_data (sgot) != NULL);
6742 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6743 BFD_ASSERT (g != NULL);
6745 for (sub = info->input_bfds; sub; sub = sub->link_next)
6746 for (s = sub->sections; s != NULL; s = s->next)
6748 if ((s->flags & SEC_ALLOC) == 0)
6749 continue;
6750 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6753 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6755 /* Assume there are two loadable segments consisting of
6756 contiguous sections. Is 5 enough? */
6757 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6758 g->local_gotno = local_gotno;
6759 sgot->_raw_size += local_gotno * 4;
6762 /* The check_relocs and adjust_dynamic_symbol entry points have
6763 determined the sizes of the various dynamic sections. Allocate
6764 memory for them. */
6765 reltext = false;
6766 for (s = dynobj->sections; s != NULL; s = s->next)
6768 const char *name;
6769 boolean strip;
6771 /* It's OK to base decisions on the section name, because none
6772 of the dynobj section names depend upon the input files. */
6773 name = bfd_get_section_name (dynobj, s);
6775 if ((s->flags & SEC_LINKER_CREATED) == 0)
6776 continue;
6778 strip = false;
6780 if (strncmp (name, ".rel", 4) == 0)
6782 if (s->_raw_size == 0)
6784 /* We only strip the section if the output section name
6785 has the same name. Otherwise, there might be several
6786 input sections for this output section. FIXME: This
6787 code is probably not needed these days anyhow, since
6788 the linker now does not create empty output sections. */
6789 if (s->output_section != NULL
6790 && strcmp (name,
6791 bfd_get_section_name (s->output_section->owner,
6792 s->output_section)) == 0)
6793 strip = true;
6795 else
6797 const char *outname;
6798 asection *target;
6800 /* If this relocation section applies to a read only
6801 section, then we probably need a DT_TEXTREL entry.
6802 If the relocation section is .rel.dyn, we always
6803 assert a DT_TEXTREL entry rather than testing whether
6804 there exists a relocation to a read only section or
6805 not. */
6806 outname = bfd_get_section_name (output_bfd,
6807 s->output_section);
6808 target = bfd_get_section_by_name (output_bfd, outname + 4);
6809 if ((target != NULL
6810 && (target->flags & SEC_READONLY) != 0
6811 && (target->flags & SEC_ALLOC) != 0)
6812 || strcmp (outname, ".rel.dyn") == 0)
6813 reltext = true;
6815 /* We use the reloc_count field as a counter if we need
6816 to copy relocs into the output file. */
6817 if (strcmp (name, ".rel.dyn") != 0)
6818 s->reloc_count = 0;
6821 else if (strncmp (name, ".got", 4) == 0)
6823 int i;
6825 BFD_ASSERT (elf_section_data (s) != NULL);
6826 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6827 BFD_ASSERT (g != NULL);
6829 /* Fix the size of .got section for the correspondence of
6830 global symbols and got entries. This adds some useless
6831 got entries. Is this required by ABI really? */
6832 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6833 s->_raw_size += i * 4;
6835 else if (strncmp (name, ".stub", 5) == 0)
6837 /* Irix rld assumes that the function stub isn't at the end
6838 of .text section. So put a dummy. XXX */
6839 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6841 else if (! info->shared
6842 && ! mips_elf_hash_table (info)->use_rld_obj_head
6843 && strncmp (name, ".rld_map", 8) == 0)
6845 /* We add a room for __rld_map. It will be filled in by the
6846 rtld to contain a pointer to the _r_debug structure. */
6847 s->_raw_size += 4;
6849 else if (SGI_COMPAT (output_bfd)
6850 && strncmp (name, ".compact_rel", 12) == 0)
6851 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6852 else if (strncmp (name, ".init", 5) != 0)
6854 /* It's not one of our sections, so don't allocate space. */
6855 continue;
6858 if (strip)
6860 _bfd_strip_section_from_output (s);
6861 continue;
6864 /* Allocate memory for the section contents. */
6865 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6866 if (s->contents == NULL && s->_raw_size != 0)
6868 bfd_set_error (bfd_error_no_memory);
6869 return false;
6871 memset (s->contents, 0, s->_raw_size);
6874 if (elf_hash_table (info)->dynamic_sections_created)
6876 /* Add some entries to the .dynamic section. We fill in the
6877 values later, in elf_mips_finish_dynamic_sections, but we
6878 must add the entries now so that we get the correct size for
6879 the .dynamic section. The DT_DEBUG entry is filled in by the
6880 dynamic linker and used by the debugger. */
6881 if (! info->shared)
6883 if (SGI_COMPAT (output_bfd))
6885 /* SGI object has the equivalence of DT_DEBUG in the
6886 DT_MIPS_RLD_MAP entry. */
6887 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6888 return false;
6890 else
6891 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6892 return false;
6895 if (reltext)
6897 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6898 return false;
6901 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6902 return false;
6904 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6906 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6907 return false;
6909 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6910 return false;
6912 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6913 return false;
6916 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6917 return false;
6919 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6920 return false;
6922 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6924 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6925 return false;
6927 s = bfd_get_section_by_name (dynobj, ".liblist");
6928 BFD_ASSERT (s != NULL);
6930 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6931 return false;
6934 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6935 return false;
6937 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6938 return false;
6940 #if 0
6941 /* Time stamps in executable files are a bad idea. */
6942 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6943 return false;
6944 #endif
6946 #if 0 /* FIXME */
6947 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6948 return false;
6949 #endif
6951 #if 0 /* FIXME */
6952 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6953 return false;
6954 #endif
6956 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6957 return false;
6959 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6960 return false;
6962 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6963 return false;
6965 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6966 return false;
6968 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6969 return false;
6971 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6972 return false;
6974 #if 0 /* (SGI_COMPAT) */
6975 if (! bfd_get_section_by_name (dynobj, ".init"))
6976 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6977 return false;
6979 if (! bfd_get_section_by_name (dynobj, ".fini"))
6980 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6981 return false;
6982 #endif
6985 /* If we use dynamic linking, we generate a section symbol for each
6986 output section. These are local symbols, which means that they
6987 must come first in the dynamic symbol table.
6988 That means we must increment the dynamic symbol index of every
6989 other dynamic symbol. */
6991 unsigned int c, i;
6992 struct mips_got_info *g;
6994 c = 0;
6995 if (elf_hash_table (info)->dynamic_sections_created)
6997 #if 0
6998 /* We no longer try to restrict the set of sections which get
6999 dynamic symbol table entries, since it fails if we have
7000 other random sections which need dynamic relocations. */
7001 const char * const *namep;
7002 bfd_size_type strindex;
7003 struct bfd_strtab_hash *dynstr;
7005 if (SGI_COMPAT (output_bfd))
7007 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
7008 elf_link_hash_traverse (elf_hash_table (info),
7009 mips_elf_adjust_dynindx,
7010 (PTR) &c);
7011 elf_hash_table (info)->dynsymcount += c;
7013 dynstr = elf_hash_table (info)->dynstr;
7014 BFD_ASSERT (dynstr != NULL);
7016 for (i = 1, namep = mips_elf_dynsym_sec_names;
7017 *namep != NULL;
7018 i++, namep++)
7020 s = bfd_get_section_by_name (output_bfd, *namep);
7021 if (s != NULL)
7022 elf_section_data (s)->dynindx = i;
7024 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
7025 if (strindex == (bfd_size_type) -1)
7026 return false;
7028 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
7031 else
7032 #endif /* 0 */
7034 c = bfd_count_sections (output_bfd);
7035 elf_link_hash_traverse (elf_hash_table (info),
7036 mips_elf_adjust_dynindx,
7037 (PTR) &c);
7038 elf_hash_table (info)->dynsymcount += c;
7040 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
7042 elf_section_data (s)->dynindx = i;
7043 /* These symbols will have no names, so we don't need to
7044 fiddle with dynstr_index. */
7049 if (sgot != NULL)
7051 BFD_ASSERT (elf_section_data (sgot) != NULL);
7052 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7053 BFD_ASSERT (g != NULL);
7055 /* If there are no global got symbols, fake the last symbol so
7056 for safety. */
7057 if (g->global_gotsym)
7058 g->global_gotsym += c;
7059 else
7060 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
7064 return true;
7067 /* Increment the index of a dynamic symbol by a given amount. Called
7068 via elf_link_hash_traverse. */
7070 static boolean
7071 mips_elf_adjust_dynindx (h, cparg)
7072 struct elf_link_hash_entry *h;
7073 PTR cparg;
7075 unsigned int *cp = (unsigned int *) cparg;
7077 if (h->dynindx != -1)
7078 h->dynindx += *cp;
7079 return true;
7082 /* Finish up dynamic symbol handling. We set the contents of various
7083 dynamic sections here. */
7085 static boolean
7086 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7087 bfd *output_bfd;
7088 struct bfd_link_info *info;
7089 struct elf_link_hash_entry *h;
7090 Elf_Internal_Sym *sym;
7092 bfd *dynobj;
7093 bfd_vma gval;
7094 asection *sgot;
7095 struct mips_got_info *g;
7096 const char *name;
7098 dynobj = elf_hash_table (info)->dynobj;
7099 gval = sym->st_value;
7101 if (h->plt.offset != (bfd_vma) -1)
7103 asection *s;
7104 bfd_byte *p;
7105 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7107 /* This symbol has a stub. Set it up. */
7109 BFD_ASSERT (h->dynindx != -1);
7111 s = bfd_get_section_by_name (dynobj, ".stub");
7112 BFD_ASSERT (s != NULL);
7114 /* Fill the stub. */
7115 p = stub;
7116 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7117 p += 4;
7118 bfd_put_32 (output_bfd, STUB_MOVE, p);
7119 p += 4;
7121 /* FIXME: Can h->dynindex be more than 64K? */
7122 if (h->dynindx & 0xffff0000)
7123 return false;
7125 bfd_put_32 (output_bfd, STUB_JALR, p);
7126 p += 4;
7127 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7129 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7130 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7132 /* Mark the symbol as undefined. plt.offset != -1 occurs
7133 only for the referenced symbol. */
7134 sym->st_shndx = SHN_UNDEF;
7136 /* The run-time linker uses the st_value field of the symbol
7137 to reset the global offset table entry for this external
7138 to its stub address when unlinking a shared object. */
7139 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7140 sym->st_value = gval;
7143 BFD_ASSERT (h->dynindx != -1);
7145 sgot = bfd_get_section_by_name (dynobj, ".got");
7146 BFD_ASSERT (sgot != NULL);
7147 BFD_ASSERT (elf_section_data (sgot) != NULL);
7148 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7149 BFD_ASSERT (g != NULL);
7151 if ((unsigned long) h->dynindx >= g->global_gotsym)
7153 bfd_size_type offset;
7155 /* This symbol has an entry in the global offset table. Set its
7156 value to the corresponding got entry, if needed. */
7157 if (h->got.offset == (bfd_vma) -1)
7159 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
7160 BFD_ASSERT (g->local_gotno * 4 <= offset
7161 && offset < sgot->_raw_size);
7162 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
7166 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7167 name = h->root.root.string;
7168 if (strcmp (name, "_DYNAMIC") == 0
7169 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7170 sym->st_shndx = SHN_ABS;
7171 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7173 sym->st_shndx = SHN_ABS;
7174 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7175 sym->st_value = 1;
7177 else if (SGI_COMPAT (output_bfd))
7179 if (strcmp (name, "_gp_disp") == 0)
7181 sym->st_shndx = SHN_ABS;
7182 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7183 sym->st_value = elf_gp (output_bfd);
7185 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7186 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7188 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7189 sym->st_other = STO_PROTECTED;
7190 sym->st_value = 0;
7191 sym->st_shndx = SHN_MIPS_DATA;
7193 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7195 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7196 sym->st_other = STO_PROTECTED;
7197 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7198 sym->st_shndx = SHN_ABS;
7200 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7202 if (h->type == STT_FUNC)
7203 sym->st_shndx = SHN_MIPS_TEXT;
7204 else if (h->type == STT_OBJECT)
7205 sym->st_shndx = SHN_MIPS_DATA;
7209 if (SGI_COMPAT (output_bfd)
7210 && ! info->shared)
7212 if (! mips_elf_hash_table (info)->use_rld_obj_head
7213 && strcmp (name, "__rld_map") == 0)
7215 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7216 BFD_ASSERT (s != NULL);
7217 sym->st_value = s->output_section->vma + s->output_offset;
7218 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7219 if (mips_elf_hash_table (info)->rld_value == 0)
7220 mips_elf_hash_table (info)->rld_value = sym->st_value;
7222 else if (mips_elf_hash_table (info)->use_rld_obj_head
7223 && strcmp (name, "__rld_obj_head") == 0)
7225 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7226 BFD_ASSERT (s != NULL);
7227 mips_elf_hash_table (info)->rld_value = sym->st_value;
7231 /* If this is a mips16 symbol, force the value to be even. */
7232 if (sym->st_other == STO_MIPS16
7233 && (sym->st_value & 1) != 0)
7234 --sym->st_value;
7236 return true;
7239 /* Finish up the dynamic sections. */
7241 static boolean
7242 mips_elf_finish_dynamic_sections (output_bfd, info)
7243 bfd *output_bfd;
7244 struct bfd_link_info *info;
7246 bfd *dynobj;
7247 asection *sdyn;
7248 asection *sgot;
7249 struct mips_got_info *g;
7251 dynobj = elf_hash_table (info)->dynobj;
7253 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7255 sgot = bfd_get_section_by_name (dynobj, ".got");
7256 if (sgot == NULL)
7257 g = NULL;
7258 else
7260 BFD_ASSERT (elf_section_data (sgot) != NULL);
7261 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7262 BFD_ASSERT (g != NULL);
7265 if (elf_hash_table (info)->dynamic_sections_created)
7267 Elf32_External_Dyn *dyncon, *dynconend;
7269 BFD_ASSERT (sdyn != NULL);
7270 BFD_ASSERT (g != NULL);
7272 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7273 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7274 for (; dyncon < dynconend; dyncon++)
7276 Elf_Internal_Dyn dyn;
7277 const char *name;
7278 size_t elemsize;
7279 asection *s;
7281 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7283 switch (dyn.d_tag)
7285 default:
7286 break;
7288 case DT_RELENT:
7289 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7290 BFD_ASSERT (s != NULL);
7291 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
7292 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7293 break;
7295 case DT_STRSZ:
7296 /* Rewrite DT_STRSZ. */
7297 dyn.d_un.d_val =
7298 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7299 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7300 break;
7302 case DT_PLTGOT:
7303 name = ".got";
7304 goto get_vma;
7305 case DT_MIPS_CONFLICT:
7306 name = ".conflict";
7307 goto get_vma;
7308 case DT_MIPS_LIBLIST:
7309 name = ".liblist";
7310 get_vma:
7311 s = bfd_get_section_by_name (output_bfd, name);
7312 BFD_ASSERT (s != NULL);
7313 dyn.d_un.d_ptr = s->vma;
7314 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7315 break;
7317 case DT_MIPS_RLD_VERSION:
7318 dyn.d_un.d_val = 1; /* XXX */
7319 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7320 break;
7322 case DT_MIPS_FLAGS:
7323 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7324 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7325 break;
7327 case DT_MIPS_CONFLICTNO:
7328 name = ".conflict";
7329 elemsize = sizeof (Elf32_Conflict);
7330 goto set_elemno;
7332 case DT_MIPS_LIBLISTNO:
7333 name = ".liblist";
7334 elemsize = sizeof (Elf32_Lib);
7335 set_elemno:
7336 s = bfd_get_section_by_name (output_bfd, name);
7337 if (s != NULL)
7339 if (s->_cooked_size != 0)
7340 dyn.d_un.d_val = s->_cooked_size / elemsize;
7341 else
7342 dyn.d_un.d_val = s->_raw_size / elemsize;
7344 else
7345 dyn.d_un.d_val = 0;
7347 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7348 break;
7350 case DT_MIPS_TIME_STAMP:
7351 time ((time_t *) &dyn.d_un.d_val);
7352 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7353 break;
7355 case DT_MIPS_ICHECKSUM:
7356 /* XXX FIXME: */
7357 break;
7359 case DT_MIPS_IVERSION:
7360 /* XXX FIXME: */
7361 break;
7363 case DT_MIPS_BASE_ADDRESS:
7364 s = output_bfd->sections;
7365 BFD_ASSERT (s != NULL);
7366 dyn.d_un.d_ptr = s->vma & ~(0xffff);
7367 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7368 break;
7370 case DT_MIPS_LOCAL_GOTNO:
7371 dyn.d_un.d_val = g->local_gotno;
7372 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7373 break;
7375 case DT_MIPS_SYMTABNO:
7376 name = ".dynsym";
7377 elemsize = sizeof (Elf32_External_Sym);
7378 s = bfd_get_section_by_name (output_bfd, name);
7379 BFD_ASSERT (s != NULL);
7381 if (s->_cooked_size != 0)
7382 dyn.d_un.d_val = s->_cooked_size / elemsize;
7383 else
7384 dyn.d_un.d_val = s->_raw_size / elemsize;
7385 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7386 break;
7388 case DT_MIPS_UNREFEXTNO:
7389 #if 0
7390 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
7391 #else
7392 dyn.d_un.d_val = bfd_count_sections (output_bfd);
7393 #endif
7394 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7395 break;
7397 case DT_MIPS_GOTSYM:
7398 dyn.d_un.d_val = g->global_gotsym;
7399 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7400 break;
7402 case DT_MIPS_HIPAGENO:
7403 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7404 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7405 break;
7407 case DT_MIPS_RLD_MAP:
7408 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7409 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7410 break;
7416 /* The first entry of the global offset table will be filled at
7417 runtime. The second entry will be used by some runtime loaders.
7418 This isn't the case of Irix rld. */
7419 if (sgot != NULL && sgot->_raw_size > 0)
7421 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7422 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
7425 if (sgot != NULL)
7426 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7429 asection *sdynsym;
7430 asection *s;
7431 Elf_Internal_Sym sym;
7432 Elf32_compact_rel cpt;
7434 /* Set up the section symbols for the output sections. SGI sets
7435 the STT_NOTYPE attribute for these symbols. Should we do so? */
7437 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
7438 if (sdynsym != NULL)
7440 #if 0
7441 const char *name;
7442 const char * const * namep = mips_elf_dynsym_sec_names;
7443 unsigned int i;
7444 bfd_vma last;
7445 long dindx;
7447 /* We no longer try to restrict the set of sections which get
7448 dynamic symbol table entries, since it fails if we have
7449 other random sections which need dynamic relocations. */
7450 if (SGI_COMPAT (output_bfd))
7452 sym.st_size = 0;
7453 sym.st_name = 0;
7454 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7455 sym.st_other = 0;
7457 i = 0;
7458 last = 0;
7459 dindx = 0;
7460 while ((name = *namep++) != NULL)
7462 s = bfd_get_section_by_name (output_bfd, name);
7463 if (s != NULL)
7465 sym.st_value = s->vma;
7466 dindx = elf_section_data (s)->dynindx;
7467 last = s->vma + s->_raw_size;
7469 else
7471 sym.st_value = last;
7472 dindx++;
7475 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7476 ? SHN_MIPS_TEXT
7477 : SHN_MIPS_DATA);
7478 ++i;
7479 sym.st_name =
7480 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7482 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7483 (((Elf32_External_Sym *)
7484 sdynsym->contents)
7485 + dindx));
7488 /* Set the sh_info field of the output .dynsym section to
7489 the index of the first global symbol. */
7490 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7491 SIZEOF_MIPS_DYNSYM_SECNAMES;
7493 else
7494 #endif /* 0 */
7496 sym.st_size = 0;
7497 sym.st_name = 0;
7498 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7499 sym.st_other = 0;
7501 for (s = output_bfd->sections; s != NULL; s = s->next)
7503 int indx;
7505 sym.st_value = s->vma;
7507 indx = elf_section_data (s)->this_idx;
7508 BFD_ASSERT (indx > 0);
7509 sym.st_shndx = indx;
7511 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7512 (((Elf32_External_Sym *)
7513 sdynsym->contents)
7514 + elf_section_data (s)->dynindx));
7517 /* Set the sh_info field of the output .dynsym section to
7518 the index of the first global symbol. */
7519 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7520 bfd_count_sections (output_bfd) + 1;
7524 if (SGI_COMPAT (output_bfd))
7526 /* Write .compact_rel section out. */
7527 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7528 if (s != NULL)
7530 cpt.id1 = 1;
7531 cpt.num = s->reloc_count;
7532 cpt.id2 = 2;
7533 cpt.offset = (s->output_section->filepos
7534 + sizeof (Elf32_External_compact_rel));
7535 cpt.reserved0 = 0;
7536 cpt.reserved1 = 0;
7537 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7538 ((Elf32_External_compact_rel *)
7539 s->contents));
7541 /* Clean up a dummy stub function entry in .text. */
7542 s = bfd_get_section_by_name (dynobj, ".stub");
7543 if (s != NULL)
7545 file_ptr dummy_offset;
7547 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7548 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7549 memset (s->contents + dummy_offset, 0,
7550 MIPS_FUNCTION_STUB_SIZE);
7555 /* Clean up a first relocation in .rel.dyn. */
7556 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7557 if (s != NULL && s->_raw_size > 0)
7558 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7561 return true;
7564 /* This is almost identical to bfd_generic_get_... except that some
7565 MIPS relocations need to be handled specially. Sigh. */
7567 static bfd_byte *
7568 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7569 relocateable, symbols)
7570 bfd *abfd;
7571 struct bfd_link_info *link_info;
7572 struct bfd_link_order *link_order;
7573 bfd_byte *data;
7574 boolean relocateable;
7575 asymbol **symbols;
7577 /* Get enough memory to hold the stuff */
7578 bfd *input_bfd = link_order->u.indirect.section->owner;
7579 asection *input_section = link_order->u.indirect.section;
7581 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7582 arelent **reloc_vector = NULL;
7583 long reloc_count;
7585 if (reloc_size < 0)
7586 goto error_return;
7588 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7589 if (reloc_vector == NULL && reloc_size != 0)
7590 goto error_return;
7592 /* read in the section */
7593 if (!bfd_get_section_contents (input_bfd,
7594 input_section,
7595 (PTR) data,
7597 input_section->_raw_size))
7598 goto error_return;
7600 /* We're not relaxing the section, so just copy the size info */
7601 input_section->_cooked_size = input_section->_raw_size;
7602 input_section->reloc_done = true;
7604 reloc_count = bfd_canonicalize_reloc (input_bfd,
7605 input_section,
7606 reloc_vector,
7607 symbols);
7608 if (reloc_count < 0)
7609 goto error_return;
7611 if (reloc_count > 0)
7613 arelent **parent;
7614 /* for mips */
7615 int gp_found;
7616 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7619 struct bfd_hash_entry *h;
7620 struct bfd_link_hash_entry *lh;
7621 /* Skip all this stuff if we aren't mixing formats. */
7622 if (abfd && input_bfd
7623 && abfd->xvec == input_bfd->xvec)
7624 lh = 0;
7625 else
7627 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7628 lh = (struct bfd_link_hash_entry *) h;
7630 lookup:
7631 if (lh)
7633 switch (lh->type)
7635 case bfd_link_hash_undefined:
7636 case bfd_link_hash_undefweak:
7637 case bfd_link_hash_common:
7638 gp_found = 0;
7639 break;
7640 case bfd_link_hash_defined:
7641 case bfd_link_hash_defweak:
7642 gp_found = 1;
7643 gp = lh->u.def.value;
7644 break;
7645 case bfd_link_hash_indirect:
7646 case bfd_link_hash_warning:
7647 lh = lh->u.i.link;
7648 /* @@FIXME ignoring warning for now */
7649 goto lookup;
7650 case bfd_link_hash_new:
7651 default:
7652 abort ();
7655 else
7656 gp_found = 0;
7658 /* end mips */
7659 for (parent = reloc_vector; *parent != (arelent *) NULL;
7660 parent++)
7662 char *error_message = (char *) NULL;
7663 bfd_reloc_status_type r;
7665 /* Specific to MIPS: Deal with relocation types that require
7666 knowing the gp of the output bfd. */
7667 asymbol *sym = *(*parent)->sym_ptr_ptr;
7668 if (bfd_is_abs_section (sym->section) && abfd)
7670 /* The special_function wouldn't get called anyways. */
7672 else if (!gp_found)
7674 /* The gp isn't there; let the special function code
7675 fall over on its own. */
7677 else if ((*parent)->howto->special_function
7678 == _bfd_mips_elf_gprel16_reloc)
7680 /* bypass special_function call */
7681 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7682 relocateable, (PTR) data, gp);
7683 goto skip_bfd_perform_relocation;
7685 /* end mips specific stuff */
7687 r = bfd_perform_relocation (input_bfd,
7688 *parent,
7689 (PTR) data,
7690 input_section,
7691 relocateable ? abfd : (bfd *) NULL,
7692 &error_message);
7693 skip_bfd_perform_relocation:
7695 if (relocateable)
7697 asection *os = input_section->output_section;
7699 /* A partial link, so keep the relocs */
7700 os->orelocation[os->reloc_count] = *parent;
7701 os->reloc_count++;
7704 if (r != bfd_reloc_ok)
7706 switch (r)
7708 case bfd_reloc_undefined:
7709 if (!((*link_info->callbacks->undefined_symbol)
7710 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7711 input_bfd, input_section, (*parent)->address)))
7712 goto error_return;
7713 break;
7714 case bfd_reloc_dangerous:
7715 BFD_ASSERT (error_message != (char *) NULL);
7716 if (!((*link_info->callbacks->reloc_dangerous)
7717 (link_info, error_message, input_bfd, input_section,
7718 (*parent)->address)))
7719 goto error_return;
7720 break;
7721 case bfd_reloc_overflow:
7722 if (!((*link_info->callbacks->reloc_overflow)
7723 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7724 (*parent)->howto->name, (*parent)->addend,
7725 input_bfd, input_section, (*parent)->address)))
7726 goto error_return;
7727 break;
7728 case bfd_reloc_outofrange:
7729 default:
7730 abort ();
7731 break;
7737 if (reloc_vector != NULL)
7738 free (reloc_vector);
7739 return data;
7741 error_return:
7742 if (reloc_vector != NULL)
7743 free (reloc_vector);
7744 return NULL;
7746 #define bfd_elf32_bfd_get_relocated_section_contents \
7747 elf32_mips_get_relocated_section_contents
7749 /* ECOFF swapping routines. These are used when dealing with the
7750 .mdebug section, which is in the ECOFF debugging format. */
7751 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7753 /* Symbol table magic number. */
7754 magicSym,
7755 /* Alignment of debugging information. E.g., 4. */
7757 /* Sizes of external symbolic information. */
7758 sizeof (struct hdr_ext),
7759 sizeof (struct dnr_ext),
7760 sizeof (struct pdr_ext),
7761 sizeof (struct sym_ext),
7762 sizeof (struct opt_ext),
7763 sizeof (struct fdr_ext),
7764 sizeof (struct rfd_ext),
7765 sizeof (struct ext_ext),
7766 /* Functions to swap in external symbolic data. */
7767 ecoff_swap_hdr_in,
7768 ecoff_swap_dnr_in,
7769 ecoff_swap_pdr_in,
7770 ecoff_swap_sym_in,
7771 ecoff_swap_opt_in,
7772 ecoff_swap_fdr_in,
7773 ecoff_swap_rfd_in,
7774 ecoff_swap_ext_in,
7775 _bfd_ecoff_swap_tir_in,
7776 _bfd_ecoff_swap_rndx_in,
7777 /* Functions to swap out external symbolic data. */
7778 ecoff_swap_hdr_out,
7779 ecoff_swap_dnr_out,
7780 ecoff_swap_pdr_out,
7781 ecoff_swap_sym_out,
7782 ecoff_swap_opt_out,
7783 ecoff_swap_fdr_out,
7784 ecoff_swap_rfd_out,
7785 ecoff_swap_ext_out,
7786 _bfd_ecoff_swap_tir_out,
7787 _bfd_ecoff_swap_rndx_out,
7788 /* Function to read in symbolic data. */
7789 _bfd_mips_elf_read_ecoff_info
7792 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
7793 #define TARGET_LITTLE_NAME "elf32-littlemips"
7794 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
7795 #define TARGET_BIG_NAME "elf32-bigmips"
7796 #define ELF_ARCH bfd_arch_mips
7797 #define ELF_MACHINE_CODE EM_MIPS
7799 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7800 a value of 0x1000, and we are compatible. */
7801 #define ELF_MAXPAGESIZE 0x1000
7803 #define elf_backend_collect true
7804 #define elf_backend_type_change_ok true
7805 #define elf_backend_can_gc_sections true
7806 #define elf_info_to_howto mips_info_to_howto_rela
7807 #define elf_info_to_howto_rel mips_info_to_howto_rel
7808 #define elf_backend_sym_is_global mips_elf_sym_is_global
7809 #define elf_backend_object_p mips_elf32_object_p
7810 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
7811 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
7812 #define elf_backend_section_from_bfd_section \
7813 _bfd_mips_elf_section_from_bfd_section
7814 #define elf_backend_section_processing mips_elf32_section_processing
7815 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
7816 #define elf_backend_additional_program_headers \
7817 mips_elf_additional_program_headers
7818 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
7819 #define elf_backend_final_write_processing \
7820 _bfd_mips_elf_final_write_processing
7821 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
7823 #define bfd_elf32_bfd_is_local_label_name \
7824 mips_elf_is_local_label_name
7825 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
7826 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
7827 #define bfd_elf32_bfd_link_hash_table_create \
7828 mips_elf_link_hash_table_create
7829 #define bfd_elf32_bfd_final_link mips_elf_final_link
7830 #define bfd_elf32_bfd_copy_private_bfd_data \
7831 _bfd_mips_elf_copy_private_bfd_data
7832 #define bfd_elf32_bfd_merge_private_bfd_data \
7833 _bfd_mips_elf_merge_private_bfd_data
7834 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7835 #define bfd_elf32_bfd_print_private_bfd_data \
7836 _bfd_mips_elf_print_private_bfd_data
7837 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
7838 #define elf_backend_create_dynamic_sections \
7839 mips_elf_create_dynamic_sections
7840 #define elf_backend_check_relocs mips_elf_check_relocs
7841 #define elf_backend_adjust_dynamic_symbol \
7842 mips_elf_adjust_dynamic_symbol
7843 #define elf_backend_always_size_sections \
7844 mips_elf_always_size_sections
7845 #define elf_backend_size_dynamic_sections \
7846 mips_elf_size_dynamic_sections
7847 #define elf_backend_relocate_section mips_elf_relocate_section
7848 #define elf_backend_link_output_symbol_hook \
7849 mips_elf_link_output_symbol_hook
7850 #define elf_backend_finish_dynamic_symbol \
7851 mips_elf_finish_dynamic_symbol
7852 #define elf_backend_finish_dynamic_sections \
7853 mips_elf_finish_dynamic_sections
7854 #define elf_backend_gc_mark_hook mips_elf_gc_mark_hook
7855 #define elf_backend_gc_sweep_hook mips_elf_gc_sweep_hook
7857 #define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
7858 #define elf_backend_plt_header_size 0
7860 #include "elf32-target.h"