* archures.c (bfd_mach_mn10300): Define.
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
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.
13
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.
18
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. */
22
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. */
26
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"
34
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"
43
44 static bfd_reloc_status_type mips32_64bit_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
47 PARAMS ((bfd *, bfd_reloc_code_real_type));
48 static void mips_info_to_howto_rel
49 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
50 static void bfd_mips_elf32_swap_gptab_in
51 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
52 static void bfd_mips_elf32_swap_gptab_out
53 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
54 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
55 static boolean mips_elf32_object_p PARAMS ((bfd *));
56 static boolean mips_elf_create_procedure_table
57 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
58 struct ecoff_debug_info *));
59 static int mips_elf_additional_program_headers PARAMS ((bfd *));
60 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
61 static INLINE int elf_mips_isa PARAMS ((flagword));
62 static INLINE int elf_mips_mach PARAMS ((flagword));
63 static boolean mips_elf32_section_from_shdr
64 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
65 static boolean mips_elf32_section_processing
66 PARAMS ((bfd *, Elf32_Internal_Shdr *));
67 static boolean mips_elf_is_local_label_name
68 PARAMS ((bfd *, const char *));
69 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
70 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
71 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
72 PARAMS ((bfd *));
73 static int gptab_compare PARAMS ((const void *, const void *));
74 static boolean mips_elf_final_link
75 PARAMS ((bfd *, struct bfd_link_info *));
76 static void mips_elf_relocate_hi16
77 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
78 bfd_vma));
79 static boolean mips_elf_relocate_got_local
80 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
81 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
82 static void mips_elf_relocate_global_got
83 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
84 static bfd_reloc_status_type mips16_jump_reloc
85 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
86 static bfd_reloc_status_type mips16_gprel_reloc
87 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
88 static boolean mips_elf_adjust_dynindx
89 PARAMS ((struct elf_link_hash_entry *, PTR));
90 static boolean mips_elf_relocate_section
91 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
92 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
93 static boolean mips_elf_link_output_symbol_hook
94 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
95 asection *));
96 static boolean mips_elf_create_dynamic_sections
97 PARAMS ((bfd *, struct bfd_link_info *));
98 static boolean mips_elf_create_compact_rel_section
99 PARAMS ((bfd *, struct bfd_link_info *));
100 static boolean mips_elf_create_got_section
101 PARAMS ((bfd *, struct bfd_link_info *));
102 static boolean mips_elf_check_relocs
103 PARAMS ((bfd *, struct bfd_link_info *, asection *,
104 const Elf_Internal_Rela *));
105 static boolean mips_elf_adjust_dynamic_symbol
106 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
107 static boolean mips_elf_always_size_sections
108 PARAMS ((bfd *, struct bfd_link_info *));
109 static boolean mips_elf_size_dynamic_sections
110 PARAMS ((bfd *, struct bfd_link_info *));
111 static boolean mips_elf_finish_dynamic_symbol
112 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
113 Elf_Internal_Sym *));
114 static boolean mips_elf_finish_dynamic_sections
115 PARAMS ((bfd *, struct bfd_link_info *));
116 static boolean mips_elf_add_symbol_hook
117 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
118 const char **, flagword *, asection **, bfd_vma *));
119 static bfd_reloc_status_type mips_elf_final_gp
120 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
121 static bfd_byte *elf32_mips_get_relocated_section_contents
122 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
123 bfd_byte *, boolean, asymbol **));
124
125 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
126 executables. FIXME: At the moment, we default to always generating
127 Irix 5 executables. */
128
129 #define SGI_COMPAT(abfd) (1)
130
131 /* This structure is used to hold .got information when linking. It
132 is stored in the tdata field of the bfd_elf_section_data structure. */
133
134 struct mips_got_info
135 {
136 /* The symbol index of the first global .got symbol. */
137 unsigned long global_gotsym;
138 /* The number of local .got entries. */
139 unsigned int local_gotno;
140 /* The number of local .got entries we have used. */
141 unsigned int assigned_gotno;
142 };
143
144 /* The number of local .got entries we reserve. */
145 #define MIPS_RESERVED_GOTNO (2)
146
147 /* Instructions which appear in a stub. For some reason the stub is
148 slightly different on an SGI system. */
149 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
150 #define STUB_LW(abfd) \
151 (SGI_COMPAT (abfd) \
152 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
153 : 0x8f998000) /* lw t9,0x8000(gp) */
154 #define STUB_MOVE 0x03e07825 /* move t7,ra */
155 #define STUB_JALR 0x0320f809 /* jal t9 */
156 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
157 #define MIPS_FUNCTION_STUB_SIZE (16)
158
159 /* Names of sections which appear in the .dynsym section in an Irix 5
160 executable. */
161
162 static const char * const mips_elf_dynsym_sec_names[] =
163 {
164 ".text",
165 ".init",
166 ".fini",
167 ".data",
168 ".rodata",
169 ".sdata",
170 ".sbss",
171 ".bss",
172 NULL
173 };
174
175 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
176 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
177
178 /* The number of entries in mips_elf_dynsym_sec_names which go in the
179 text segment. */
180
181 #define MIPS_TEXT_DYNSYM_SECNO (3)
182
183 /* The names of the runtime procedure table symbols used on Irix 5. */
184
185 static const char * const mips_elf_dynsym_rtproc_names[] =
186 {
187 "_procedure_table",
188 "_procedure_string_table",
189 "_procedure_table_size",
190 NULL
191 };
192
193 /* These structures are used to generate the .compact_rel section on
194 Irix 5. */
195
196 typedef struct
197 {
198 unsigned long id1; /* Always one? */
199 unsigned long num; /* Number of compact relocation entries. */
200 unsigned long id2; /* Always two? */
201 unsigned long offset; /* The file offset of the first relocation. */
202 unsigned long reserved0; /* Zero? */
203 unsigned long reserved1; /* Zero? */
204 } Elf32_compact_rel;
205
206 typedef struct
207 {
208 bfd_byte id1[4];
209 bfd_byte num[4];
210 bfd_byte id2[4];
211 bfd_byte offset[4];
212 bfd_byte reserved0[4];
213 bfd_byte reserved1[4];
214 } Elf32_External_compact_rel;
215
216 typedef struct
217 {
218 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
219 unsigned int rtype : 4; /* Relocation types. See below. */
220 unsigned int dist2to : 8;
221 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
222 unsigned long konst; /* KONST field. See below. */
223 unsigned long vaddr; /* VADDR to be relocated. */
224 } Elf32_crinfo;
225
226 typedef struct
227 {
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 } Elf32_crinfo2;
234
235 typedef struct
236 {
237 bfd_byte info[4];
238 bfd_byte konst[4];
239 bfd_byte vaddr[4];
240 } Elf32_External_crinfo;
241
242 typedef struct
243 {
244 bfd_byte info[4];
245 bfd_byte konst[4];
246 } Elf32_External_crinfo2;
247
248 /* These are the constants used to swap the bitfields in a crinfo. */
249
250 #define CRINFO_CTYPE (0x1)
251 #define CRINFO_CTYPE_SH (31)
252 #define CRINFO_RTYPE (0xf)
253 #define CRINFO_RTYPE_SH (27)
254 #define CRINFO_DIST2TO (0xff)
255 #define CRINFO_DIST2TO_SH (19)
256 #define CRINFO_RELVADDR (0x7ffff)
257 #define CRINFO_RELVADDR_SH (0)
258
259 /* A compact relocation info has long (3 words) or short (2 words)
260 formats. A short format doesn't have VADDR field and relvaddr
261 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
262 #define CRF_MIPS_LONG 1
263 #define CRF_MIPS_SHORT 0
264
265 /* There are 4 types of compact relocation at least. The value KONST
266 has different meaning for each type:
267
268 (type) (konst)
269 CT_MIPS_REL32 Address in data
270 CT_MIPS_WORD Address in word (XXX)
271 CT_MIPS_GPHI_LO GP - vaddr
272 CT_MIPS_JMPAD Address to jump
273 */
274
275 #define CRT_MIPS_REL32 0xa
276 #define CRT_MIPS_WORD 0xb
277 #define CRT_MIPS_GPHI_LO 0xc
278 #define CRT_MIPS_JMPAD 0xd
279
280 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
281 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
282 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
283 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
284
285 static void bfd_elf32_swap_compact_rel_out
286 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
287 static void bfd_elf32_swap_crinfo_out
288 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
289
290 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
291
292 enum reloc_type
293 {
294 R_MIPS_NONE = 0,
295 R_MIPS_16, R_MIPS_32,
296 R_MIPS_REL32, R_MIPS_26,
297 R_MIPS_HI16, R_MIPS_LO16,
298 R_MIPS_GPREL16, R_MIPS_LITERAL,
299 R_MIPS_GOT16, R_MIPS_PC16,
300 R_MIPS_CALL16, R_MIPS_GPREL32,
301 /* The remaining relocs are defined on Irix, although they are not
302 in the MIPS ELF ABI. */
303 R_MIPS_UNUSED1, R_MIPS_UNUSED2,
304 R_MIPS_UNUSED3,
305 R_MIPS_SHIFT5, R_MIPS_SHIFT6,
306 R_MIPS_64, R_MIPS_GOT_DISP,
307 R_MIPS_GOT_PAGE, R_MIPS_GOT_OFST,
308 R_MIPS_GOT_HI16, R_MIPS_GOT_LO16,
309 R_MIPS_SUB, R_MIPS_INSERT_A,
310 R_MIPS_INSERT_B, R_MIPS_DELETE,
311 R_MIPS_HIGHER, R_MIPS_HIGHEST,
312 R_MIPS_CALL_HI16, R_MIPS_CALL_LO16,
313 /* start-sanitize-r5900 */
314 /* This is used by a mips co-processor instruction. */
315 R_MIPS15_S3,
316 /* end-sanitize-r5900 */
317 R_MIPS_max,
318 /* These relocs are used for the mips16. */
319 R_MIPS16_26 = 100,
320 R_MIPS16_GPREL = 101
321 /* start-sanitize-sky */
322 /* These relocs are for the dvp. */
323 , R_MIPS_DVP_11_PCREL = 120,
324 R_MIPS_DVP_27_S4 = 121
325 /* end-sanitize-sky */
326 };
327
328 static reloc_howto_type elf_mips_howto_table[] =
329 {
330 /* No relocation. */
331 HOWTO (R_MIPS_NONE, /* type */
332 0, /* rightshift */
333 0, /* size (0 = byte, 1 = short, 2 = long) */
334 0, /* bitsize */
335 false, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_dont, /* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_MIPS_NONE", /* name */
340 false, /* partial_inplace */
341 0, /* src_mask */
342 0, /* dst_mask */
343 false), /* pcrel_offset */
344
345 /* 16 bit relocation. */
346 HOWTO (R_MIPS_16, /* type */
347 0, /* rightshift */
348 1, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 false, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_bitfield, /* complain_on_overflow */
353 bfd_elf_generic_reloc, /* special_function */
354 "R_MIPS_16", /* name */
355 true, /* partial_inplace */
356 0xffff, /* src_mask */
357 0xffff, /* dst_mask */
358 false), /* pcrel_offset */
359
360 /* 32 bit relocation. */
361 HOWTO (R_MIPS_32, /* type */
362 0, /* rightshift */
363 2, /* size (0 = byte, 1 = short, 2 = long) */
364 32, /* bitsize */
365 false, /* pc_relative */
366 0, /* bitpos */
367 complain_overflow_bitfield, /* complain_on_overflow */
368 bfd_elf_generic_reloc, /* special_function */
369 "R_MIPS_32", /* name */
370 true, /* partial_inplace */
371 0xffffffff, /* src_mask */
372 0xffffffff, /* dst_mask */
373 false), /* pcrel_offset */
374
375 /* 32 bit symbol relative relocation. */
376 HOWTO (R_MIPS_REL32, /* type */
377 0, /* rightshift */
378 2, /* size (0 = byte, 1 = short, 2 = long) */
379 32, /* bitsize */
380 false, /* pc_relative */
381 0, /* bitpos */
382 complain_overflow_bitfield, /* complain_on_overflow */
383 bfd_elf_generic_reloc, /* special_function */
384 "R_MIPS_REL32", /* name */
385 true, /* partial_inplace */
386 0xffffffff, /* src_mask */
387 0xffffffff, /* dst_mask */
388 false), /* pcrel_offset */
389
390 /* 26 bit branch address. */
391 HOWTO (R_MIPS_26, /* type */
392 2, /* rightshift */
393 2, /* size (0 = byte, 1 = short, 2 = long) */
394 26, /* bitsize */
395 false, /* pc_relative */
396 0, /* bitpos */
397 complain_overflow_dont, /* complain_on_overflow */
398 /* This needs complex overflow
399 detection, because the upper four
400 bits must match the PC. */
401 bfd_elf_generic_reloc, /* special_function */
402 "R_MIPS_26", /* name */
403 true, /* partial_inplace */
404 0x3ffffff, /* src_mask */
405 0x3ffffff, /* dst_mask */
406 false), /* pcrel_offset */
407
408 /* High 16 bits of symbol value. */
409 HOWTO (R_MIPS_HI16, /* type */
410 0, /* rightshift */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
412 16, /* bitsize */
413 false, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_dont, /* complain_on_overflow */
416 _bfd_mips_elf_hi16_reloc, /* special_function */
417 "R_MIPS_HI16", /* name */
418 true, /* partial_inplace */
419 0xffff, /* src_mask */
420 0xffff, /* dst_mask */
421 false), /* pcrel_offset */
422
423 /* Low 16 bits of symbol value. */
424 HOWTO (R_MIPS_LO16, /* type */
425 0, /* rightshift */
426 2, /* size (0 = byte, 1 = short, 2 = long) */
427 16, /* bitsize */
428 false, /* pc_relative */
429 0, /* bitpos */
430 complain_overflow_dont, /* complain_on_overflow */
431 _bfd_mips_elf_lo16_reloc, /* special_function */
432 "R_MIPS_LO16", /* name */
433 true, /* partial_inplace */
434 0xffff, /* src_mask */
435 0xffff, /* dst_mask */
436 false), /* pcrel_offset */
437
438 /* GP relative reference. */
439 HOWTO (R_MIPS_GPREL16, /* type */
440 0, /* rightshift */
441 2, /* size (0 = byte, 1 = short, 2 = long) */
442 16, /* bitsize */
443 false, /* pc_relative */
444 0, /* bitpos */
445 complain_overflow_signed, /* complain_on_overflow */
446 _bfd_mips_elf_gprel16_reloc, /* special_function */
447 "R_MIPS_GPREL16", /* name */
448 true, /* partial_inplace */
449 0xffff, /* src_mask */
450 0xffff, /* dst_mask */
451 false), /* pcrel_offset */
452
453 /* Reference to literal section. */
454 HOWTO (R_MIPS_LITERAL, /* type */
455 0, /* rightshift */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
457 16, /* bitsize */
458 false, /* pc_relative */
459 0, /* bitpos */
460 complain_overflow_signed, /* complain_on_overflow */
461 _bfd_mips_elf_gprel16_reloc, /* special_function */
462 "R_MIPS_LITERAL", /* name */
463 true, /* partial_inplace */
464 0xffff, /* src_mask */
465 0xffff, /* dst_mask */
466 false), /* pcrel_offset */
467
468 /* Reference to global offset table. */
469 HOWTO (R_MIPS_GOT16, /* type */
470 0, /* rightshift */
471 2, /* size (0 = byte, 1 = short, 2 = long) */
472 16, /* bitsize */
473 false, /* pc_relative */
474 0, /* bitpos */
475 complain_overflow_signed, /* complain_on_overflow */
476 _bfd_mips_elf_got16_reloc, /* special_function */
477 "R_MIPS_GOT16", /* name */
478 false, /* partial_inplace */
479 0, /* src_mask */
480 0xffff, /* dst_mask */
481 false), /* pcrel_offset */
482
483 /* 16 bit PC relative reference. */
484 HOWTO (R_MIPS_PC16, /* type */
485 0, /* rightshift */
486 2, /* size (0 = byte, 1 = short, 2 = long) */
487 16, /* bitsize */
488 true, /* pc_relative */
489 0, /* bitpos */
490 complain_overflow_signed, /* complain_on_overflow */
491 bfd_elf_generic_reloc, /* special_function */
492 "R_MIPS_PC16", /* name */
493 true, /* partial_inplace */
494 0xffff, /* src_mask */
495 0xffff, /* dst_mask */
496 false), /* pcrel_offset */
497
498 /* 16 bit call through global offset table. */
499 /* FIXME: This is not handled correctly. */
500 HOWTO (R_MIPS_CALL16, /* type */
501 0, /* rightshift */
502 2, /* size (0 = byte, 1 = short, 2 = long) */
503 16, /* bitsize */
504 false, /* pc_relative */
505 0, /* bitpos */
506 complain_overflow_signed, /* complain_on_overflow */
507 bfd_elf_generic_reloc, /* special_function */
508 "R_MIPS_CALL16", /* name */
509 false, /* partial_inplace */
510 0, /* src_mask */
511 0xffff, /* dst_mask */
512 false), /* pcrel_offset */
513
514 /* 32 bit GP relative reference. */
515 HOWTO (R_MIPS_GPREL32, /* type */
516 0, /* rightshift */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
518 32, /* bitsize */
519 false, /* pc_relative */
520 0, /* bitpos */
521 complain_overflow_bitfield, /* complain_on_overflow */
522 _bfd_mips_elf_gprel32_reloc, /* special_function */
523 "R_MIPS_GPREL32", /* name */
524 true, /* partial_inplace */
525 0xffffffff, /* src_mask */
526 0xffffffff, /* dst_mask */
527 false), /* pcrel_offset */
528
529 /* The remaining relocs are defined on Irix 5, although they are
530 not defined by the ABI. */
531 { 13 },
532 { 14 },
533 { 15 },
534
535 /* A 5 bit shift field. */
536 HOWTO (R_MIPS_SHIFT5, /* type */
537 0, /* rightshift */
538 2, /* size (0 = byte, 1 = short, 2 = long) */
539 5, /* bitsize */
540 false, /* pc_relative */
541 6, /* bitpos */
542 complain_overflow_bitfield, /* complain_on_overflow */
543 bfd_elf_generic_reloc, /* special_function */
544 "R_MIPS_SHIFT5", /* name */
545 true, /* partial_inplace */
546 0x000007c0, /* src_mask */
547 0x000007c0, /* dst_mask */
548 false), /* pcrel_offset */
549
550 /* A 6 bit shift field. */
551 /* FIXME: This is not handled correctly; a special function is
552 needed to put the most significant bit in the right place. */
553 HOWTO (R_MIPS_SHIFT6, /* type */
554 0, /* rightshift */
555 2, /* size (0 = byte, 1 = short, 2 = long) */
556 6, /* bitsize */
557 false, /* pc_relative */
558 6, /* bitpos */
559 complain_overflow_bitfield, /* complain_on_overflow */
560 bfd_elf_generic_reloc, /* special_function */
561 "R_MIPS_SHIFT6", /* name */
562 true, /* partial_inplace */
563 0x000007c4, /* src_mask */
564 0x000007c4, /* dst_mask */
565 false), /* pcrel_offset */
566
567 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
568 are 64 bits long; the upper 32 bits are simply a sign extension.
569 The fields of the howto should be the same as for R_MIPS_32,
570 other than the type, name, and special_function. */
571 HOWTO (R_MIPS_64, /* type */
572 0, /* rightshift */
573 2, /* size (0 = byte, 1 = short, 2 = long) */
574 32, /* bitsize */
575 false, /* pc_relative */
576 0, /* bitpos */
577 complain_overflow_bitfield, /* complain_on_overflow */
578 mips32_64bit_reloc, /* special_function */
579 "R_MIPS_64", /* name */
580 true, /* partial_inplace */
581 0xffffffff, /* src_mask */
582 0xffffffff, /* dst_mask */
583 false), /* pcrel_offset */
584
585 /* Displacement in the global offset table. */
586 /* FIXME: Not handled correctly. */
587 HOWTO (R_MIPS_GOT_DISP, /* type */
588 0, /* rightshift */
589 2, /* size (0 = byte, 1 = short, 2 = long) */
590 16, /* bitsize */
591 false, /* pc_relative */
592 0, /* bitpos */
593 complain_overflow_bitfield, /* complain_on_overflow */
594 bfd_elf_generic_reloc, /* special_function */
595 "R_MIPS_GOT_DISP", /* name */
596 true, /* partial_inplace */
597 0x0000ffff, /* src_mask */
598 0x0000ffff, /* dst_mask */
599 false), /* pcrel_offset */
600
601 /* Displacement to page pointer in the global offset table. */
602 /* FIXME: Not handled correctly. */
603 HOWTO (R_MIPS_GOT_PAGE, /* type */
604 0, /* rightshift */
605 2, /* size (0 = byte, 1 = short, 2 = long) */
606 16, /* bitsize */
607 false, /* pc_relative */
608 0, /* bitpos */
609 complain_overflow_bitfield, /* complain_on_overflow */
610 bfd_elf_generic_reloc, /* special_function */
611 "R_MIPS_GOT_PAGE", /* name */
612 true, /* partial_inplace */
613 0x0000ffff, /* src_mask */
614 0x0000ffff, /* dst_mask */
615 false), /* pcrel_offset */
616
617 /* Offset from page pointer in the global offset table. */
618 /* FIXME: Not handled correctly. */
619 HOWTO (R_MIPS_GOT_OFST, /* type */
620 0, /* rightshift */
621 2, /* size (0 = byte, 1 = short, 2 = long) */
622 16, /* bitsize */
623 false, /* pc_relative */
624 0, /* bitpos */
625 complain_overflow_bitfield, /* complain_on_overflow */
626 bfd_elf_generic_reloc, /* special_function */
627 "R_MIPS_GOT_OFST", /* name */
628 true, /* partial_inplace */
629 0x0000ffff, /* src_mask */
630 0x0000ffff, /* dst_mask */
631 false), /* pcrel_offset */
632
633 /* High 16 bits of displacement in global offset table. */
634 /* FIXME: Not handled correctly. */
635 HOWTO (R_MIPS_GOT_HI16, /* type */
636 0, /* rightshift */
637 2, /* size (0 = byte, 1 = short, 2 = long) */
638 16, /* bitsize */
639 false, /* pc_relative */
640 0, /* bitpos */
641 complain_overflow_dont, /* complain_on_overflow */
642 bfd_elf_generic_reloc, /* special_function */
643 "R_MIPS_GOT_HI16", /* name */
644 true, /* partial_inplace */
645 0x0000ffff, /* src_mask */
646 0x0000ffff, /* dst_mask */
647 false), /* pcrel_offset */
648
649 /* Low 16 bits of displacement in global offset table. */
650 /* FIXME: Not handled correctly. */
651 HOWTO (R_MIPS_GOT_LO16, /* type */
652 0, /* rightshift */
653 2, /* size (0 = byte, 1 = short, 2 = long) */
654 16, /* bitsize */
655 false, /* pc_relative */
656 0, /* bitpos */
657 complain_overflow_dont, /* complain_on_overflow */
658 bfd_elf_generic_reloc, /* special_function */
659 "R_MIPS_GOT_LO16", /* name */
660 true, /* partial_inplace */
661 0x0000ffff, /* src_mask */
662 0x0000ffff, /* dst_mask */
663 false), /* pcrel_offset */
664
665 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
666 { R_MIPS_SUB },
667
668 /* Used to cause the linker to insert and delete instructions? */
669 { R_MIPS_INSERT_A },
670 { R_MIPS_INSERT_B },
671 { R_MIPS_DELETE },
672
673 /* Get the higher values of a 64 bit addend. Presumably not used in
674 32 bit ELF. */
675 { R_MIPS_HIGHER },
676 { R_MIPS_HIGHEST },
677
678 /* High 16 bits of displacement in global offset table. */
679 /* FIXME: Not handled correctly. */
680 HOWTO (R_MIPS_CALL_HI16, /* type */
681 0, /* rightshift */
682 2, /* size (0 = byte, 1 = short, 2 = long) */
683 16, /* bitsize */
684 false, /* pc_relative */
685 0, /* bitpos */
686 complain_overflow_dont, /* complain_on_overflow */
687 bfd_elf_generic_reloc, /* special_function */
688 "R_MIPS_CALL_HI16", /* name */
689 true, /* partial_inplace */
690 0x0000ffff, /* src_mask */
691 0x0000ffff, /* dst_mask */
692 false), /* pcrel_offset */
693
694 /* Low 16 bits of displacement in global offset table. */
695 /* FIXME: Not handled correctly. */
696 HOWTO (R_MIPS_CALL_LO16, /* type */
697 0, /* rightshift */
698 2, /* size (0 = byte, 1 = short, 2 = long) */
699 16, /* bitsize */
700 false, /* pc_relative */
701 0, /* bitpos */
702 complain_overflow_dont, /* complain_on_overflow */
703 bfd_elf_generic_reloc, /* special_function */
704 "R_MIPS_CALL_LO16", /* name */
705 true, /* partial_inplace */
706 0x0000ffff, /* src_mask */
707 0x0000ffff, /* dst_mask */
708 false), /* pcrel_offset */
709
710 /* start-sanitize-r5900 */
711 HOWTO (R_MIPS15_S3, /* type */
712 3, /* rightshift */
713 2, /* size (0 = byte, 1 = short, 2 = long) */
714 15, /* bitsize */
715 false, /* pc_relative */
716 6, /* bitpos */
717 complain_overflow_bitfield, /* complain_on_overflow */
718 bfd_elf_generic_reloc, /* special_function */
719 "R_MIPS15_S3", /* name */
720 true, /* partial_inplace */
721 0x001fffc0, /* src_mask */
722 0x001fffc0, /* dst_mask */
723 false) /* pcrel_offset */
724 /* end-sanitize-r5900 */
725
726 };
727
728 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
729 is a hack to make the linker think that we need 64 bit values. */
730 static reloc_howto_type elf_mips_ctor64_howto =
731 HOWTO (R_MIPS_64, /* type */
732 0, /* rightshift */
733 4, /* size (0 = byte, 1 = short, 2 = long) */
734 32, /* bitsize */
735 false, /* pc_relative */
736 0, /* bitpos */
737 complain_overflow_signed, /* complain_on_overflow */
738 mips32_64bit_reloc, /* special_function */
739 "R_MIPS_64", /* name */
740 true, /* partial_inplace */
741 0xffffffff, /* src_mask */
742 0xffffffff, /* dst_mask */
743 false); /* pcrel_offset */
744
745 /* The reloc used for the mips16 jump instruction. */
746 static reloc_howto_type elf_mips16_jump_howto =
747 HOWTO (R_MIPS16_26, /* type */
748 2, /* rightshift */
749 2, /* size (0 = byte, 1 = short, 2 = long) */
750 26, /* bitsize */
751 false, /* pc_relative */
752 0, /* bitpos */
753 complain_overflow_dont, /* complain_on_overflow */
754 /* This needs complex overflow
755 detection, because the upper four
756 bits must match the PC. */
757 mips16_jump_reloc, /* special_function */
758 "R_MIPS16_26", /* name */
759 true, /* partial_inplace */
760 0x3ffffff, /* src_mask */
761 0x3ffffff, /* dst_mask */
762 false); /* pcrel_offset */
763
764 /* The reloc used for the mips16 gprel instruction. The src_mask and
765 dsk_mask for this howto do not reflect the actual instruction, in
766 which the value is not contiguous; the masks are for the
767 convenience of the relocate_section routine. */
768 static reloc_howto_type elf_mips16_gprel_howto =
769 HOWTO (R_MIPS16_GPREL, /* type */
770 0, /* rightshift */
771 2, /* size (0 = byte, 1 = short, 2 = long) */
772 16, /* bitsize */
773 false, /* pc_relative */
774 0, /* bitpos */
775 complain_overflow_signed, /* complain_on_overflow */
776 mips16_gprel_reloc, /* special_function */
777 "R_MIPS16_GPREL", /* name */
778 true, /* partial_inplace */
779 0xffff, /* src_mask */
780 0xffff, /* dst_mask */
781 false); /* pcrel_offset */
782
783 /* start-sanitize-sky */
784 /* DVP relocations.
785 Note that partial_inplace and pcrel_offset are backwards from the
786 mips port. This is intentional as it seems more reasonable. */
787 static reloc_howto_type elf_mips_dvp_11_pcrel_howto =
788 HOWTO (R_MIPS_DVP_11_PCREL, /* type */
789 3, /* rightshift */
790 2, /* size (0 = byte, 1 = short, 2 = long) */
791 11, /* bitsize */
792 true, /* pc_relative */
793 0, /* bitpos */
794 complain_overflow_signed, /* complain_on_overflow */
795 bfd_elf_generic_reloc, /* special_function */
796 "R_MIPS_DVP_11_PCREL", /* name */
797 false, /* partial_inplace */
798 0x7ff, /* src_mask */
799 0x7ff, /* dst_mask */
800 true); /* pcrel_offset */
801 static reloc_howto_type elf_mips_dvp_27_s4_howto =
802 HOWTO (R_MIPS_DVP_27_S4, /* type */
803 4, /* rightshift */
804 2, /* size (0 = byte, 1 = short, 2 = long) */
805 27, /* bitsize */
806 false, /* pc_relative */
807 4, /* bitpos */
808 complain_overflow_unsigned, /* complain_on_overflow */
809 bfd_elf_generic_reloc, /* special_function */
810 "R_MIPS_DVP_27_S4", /* name */
811 false, /* partial_inplace */
812 0x7ffffff0, /* src_mask */
813 0x7ffffff0, /* dst_mask */
814 false); /* pcrel_offset */
815 /* end-sanitize-sky */
816
817 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
818 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
819 the HI16. Here we just save the information we need; we do the
820 actual relocation when we see the LO16. MIPS ELF requires that the
821 LO16 immediately follow the HI16. As a GNU extension, we permit an
822 arbitrary number of HI16 relocs to be associated with a single LO16
823 reloc. This extension permits gcc to output the HI and LO relocs
824 itself. */
825
826 struct mips_hi16
827 {
828 struct mips_hi16 *next;
829 bfd_byte *addr;
830 bfd_vma addend;
831 };
832
833 /* FIXME: This should not be a static variable. */
834
835 static struct mips_hi16 *mips_hi16_list;
836
837 bfd_reloc_status_type
838 _bfd_mips_elf_hi16_reloc (abfd,
839 reloc_entry,
840 symbol,
841 data,
842 input_section,
843 output_bfd,
844 error_message)
845 bfd *abfd;
846 arelent *reloc_entry;
847 asymbol *symbol;
848 PTR data;
849 asection *input_section;
850 bfd *output_bfd;
851 char **error_message;
852 {
853 bfd_reloc_status_type ret;
854 bfd_vma relocation;
855 struct mips_hi16 *n;
856
857 /* If we're relocating, and this an external symbol, we don't want
858 to change anything. */
859 if (output_bfd != (bfd *) NULL
860 && (symbol->flags & BSF_SECTION_SYM) == 0
861 && reloc_entry->addend == 0)
862 {
863 reloc_entry->address += input_section->output_offset;
864 return bfd_reloc_ok;
865 }
866
867 ret = bfd_reloc_ok;
868
869 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
870 {
871 boolean relocateable;
872 bfd_vma gp;
873
874 if (ret == bfd_reloc_undefined)
875 abort ();
876
877 if (output_bfd != NULL)
878 relocateable = true;
879 else
880 {
881 relocateable = false;
882 output_bfd = symbol->section->output_section->owner;
883 }
884
885 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
886 error_message, &gp);
887 if (ret != bfd_reloc_ok)
888 return ret;
889
890 relocation = gp - reloc_entry->address;
891 }
892 else
893 {
894 if (bfd_is_und_section (symbol->section)
895 && output_bfd == (bfd *) NULL)
896 ret = bfd_reloc_undefined;
897
898 if (bfd_is_com_section (symbol->section))
899 relocation = 0;
900 else
901 relocation = symbol->value;
902 }
903
904 relocation += symbol->section->output_section->vma;
905 relocation += symbol->section->output_offset;
906 relocation += reloc_entry->addend;
907
908 if (reloc_entry->address > input_section->_cooked_size)
909 return bfd_reloc_outofrange;
910
911 /* Save the information, and let LO16 do the actual relocation. */
912 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
913 if (n == NULL)
914 return bfd_reloc_outofrange;
915 n->addr = (bfd_byte *) data + reloc_entry->address;
916 n->addend = relocation;
917 n->next = mips_hi16_list;
918 mips_hi16_list = n;
919
920 if (output_bfd != (bfd *) NULL)
921 reloc_entry->address += input_section->output_offset;
922
923 return ret;
924 }
925
926 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
927 inplace relocation; this function exists in order to do the
928 R_MIPS_HI16 relocation described above. */
929
930 bfd_reloc_status_type
931 _bfd_mips_elf_lo16_reloc (abfd,
932 reloc_entry,
933 symbol,
934 data,
935 input_section,
936 output_bfd,
937 error_message)
938 bfd *abfd;
939 arelent *reloc_entry;
940 asymbol *symbol;
941 PTR data;
942 asection *input_section;
943 bfd *output_bfd;
944 char **error_message;
945 {
946 arelent gp_disp_relent;
947
948 if (mips_hi16_list != NULL)
949 {
950 struct mips_hi16 *l;
951
952 l = mips_hi16_list;
953 while (l != NULL)
954 {
955 unsigned long insn;
956 unsigned long val;
957 unsigned long vallo;
958 struct mips_hi16 *next;
959
960 /* Do the HI16 relocation. Note that we actually don't need
961 to know anything about the LO16 itself, except where to
962 find the low 16 bits of the addend needed by the LO16. */
963 insn = bfd_get_32 (abfd, l->addr);
964 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
965 & 0xffff);
966 val = ((insn & 0xffff) << 16) + vallo;
967 val += l->addend;
968
969 /* The low order 16 bits are always treated as a signed
970 value. Therefore, a negative value in the low order bits
971 requires an adjustment in the high order bits. We need
972 to make this adjustment in two ways: once for the bits we
973 took from the data, and once for the bits we are putting
974 back in to the data. */
975 if ((vallo & 0x8000) != 0)
976 val -= 0x10000;
977 if ((val & 0x8000) != 0)
978 val += 0x10000;
979
980 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
981 bfd_put_32 (abfd, insn, l->addr);
982
983 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
984 {
985 gp_disp_relent = *reloc_entry;
986 reloc_entry = &gp_disp_relent;
987 reloc_entry->addend = l->addend;
988 }
989
990 next = l->next;
991 free (l);
992 l = next;
993 }
994
995 mips_hi16_list = NULL;
996 }
997 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
998 {
999 bfd_reloc_status_type ret;
1000 bfd_vma gp, relocation;
1001
1002 /* FIXME: Does this case ever occur? */
1003
1004 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1005 if (ret != bfd_reloc_ok)
1006 return ret;
1007
1008 relocation = gp - reloc_entry->address;
1009 relocation += symbol->section->output_section->vma;
1010 relocation += symbol->section->output_offset;
1011 relocation += reloc_entry->addend;
1012
1013 if (reloc_entry->address > input_section->_cooked_size)
1014 return bfd_reloc_outofrange;
1015
1016 gp_disp_relent = *reloc_entry;
1017 reloc_entry = &gp_disp_relent;
1018 reloc_entry->addend = relocation - 4;
1019 }
1020
1021 /* Now do the LO16 reloc in the usual way. */
1022 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1023 input_section, output_bfd, error_message);
1024 }
1025
1026 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1027 table used for PIC code. If the symbol is an external symbol, the
1028 instruction is modified to contain the offset of the appropriate
1029 entry in the global offset table. If the symbol is a section
1030 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1031 addends are combined to form the real addend against the section
1032 symbol; the GOT16 is modified to contain the offset of an entry in
1033 the global offset table, and the LO16 is modified to offset it
1034 appropriately. Thus an offset larger than 16 bits requires a
1035 modified value in the global offset table.
1036
1037 This implementation suffices for the assembler, but the linker does
1038 not yet know how to create global offset tables. */
1039
1040 bfd_reloc_status_type
1041 _bfd_mips_elf_got16_reloc (abfd,
1042 reloc_entry,
1043 symbol,
1044 data,
1045 input_section,
1046 output_bfd,
1047 error_message)
1048 bfd *abfd;
1049 arelent *reloc_entry;
1050 asymbol *symbol;
1051 PTR data;
1052 asection *input_section;
1053 bfd *output_bfd;
1054 char **error_message;
1055 {
1056 /* If we're relocating, and this an external symbol, we don't want
1057 to change anything. */
1058 if (output_bfd != (bfd *) NULL
1059 && (symbol->flags & BSF_SECTION_SYM) == 0
1060 && reloc_entry->addend == 0)
1061 {
1062 reloc_entry->address += input_section->output_offset;
1063 return bfd_reloc_ok;
1064 }
1065
1066 /* If we're relocating, and this is a local symbol, we can handle it
1067 just like HI16. */
1068 if (output_bfd != (bfd *) NULL
1069 && (symbol->flags & BSF_SECTION_SYM) != 0)
1070 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1071 input_section, output_bfd, error_message);
1072
1073 abort ();
1074 }
1075
1076 /* We have to figure out the gp value, so that we can adjust the
1077 symbol value correctly. We look up the symbol _gp in the output
1078 BFD. If we can't find it, we're stuck. We cache it in the ELF
1079 target data. We don't need to adjust the symbol value for an
1080 external symbol if we are producing relocateable output. */
1081
1082 static bfd_reloc_status_type
1083 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1084 bfd *output_bfd;
1085 asymbol *symbol;
1086 boolean relocateable;
1087 char **error_message;
1088 bfd_vma *pgp;
1089 {
1090 if (bfd_is_und_section (symbol->section)
1091 && ! relocateable)
1092 {
1093 *pgp = 0;
1094 return bfd_reloc_undefined;
1095 }
1096
1097 *pgp = _bfd_get_gp_value (output_bfd);
1098 if (*pgp == 0
1099 && (! relocateable
1100 || (symbol->flags & BSF_SECTION_SYM) != 0))
1101 {
1102 if (relocateable)
1103 {
1104 /* Make up a value. */
1105 *pgp = symbol->section->output_section->vma + 0x4000;
1106 _bfd_set_gp_value (output_bfd, *pgp);
1107 }
1108 else
1109 {
1110 unsigned int count;
1111 asymbol **sym;
1112 unsigned int i;
1113
1114 count = bfd_get_symcount (output_bfd);
1115 sym = bfd_get_outsymbols (output_bfd);
1116
1117 if (sym == (asymbol **) NULL)
1118 i = count;
1119 else
1120 {
1121 for (i = 0; i < count; i++, sym++)
1122 {
1123 register CONST char *name;
1124
1125 name = bfd_asymbol_name (*sym);
1126 if (*name == '_' && strcmp (name, "_gp") == 0)
1127 {
1128 *pgp = bfd_asymbol_value (*sym);
1129 _bfd_set_gp_value (output_bfd, *pgp);
1130 break;
1131 }
1132 }
1133 }
1134
1135 if (i >= count)
1136 {
1137 /* Only get the error once. */
1138 *pgp = 4;
1139 _bfd_set_gp_value (output_bfd, *pgp);
1140 *error_message =
1141 (char *) _("GP relative relocation when _gp not defined");
1142 return bfd_reloc_dangerous;
1143 }
1144 }
1145 }
1146
1147 return bfd_reloc_ok;
1148 }
1149
1150 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1151 become the offset from the gp register. This function also handles
1152 R_MIPS_LITERAL relocations, although those can be handled more
1153 cleverly because the entries in the .lit8 and .lit4 sections can be
1154 merged. */
1155
1156 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1157 arelent *, asection *,
1158 boolean, PTR, bfd_vma));
1159
1160 bfd_reloc_status_type
1161 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1162 output_bfd, error_message)
1163 bfd *abfd;
1164 arelent *reloc_entry;
1165 asymbol *symbol;
1166 PTR data;
1167 asection *input_section;
1168 bfd *output_bfd;
1169 char **error_message;
1170 {
1171 boolean relocateable;
1172 bfd_reloc_status_type ret;
1173 bfd_vma gp;
1174
1175 /* If we're relocating, and this is an external symbol with no
1176 addend, we don't want to change anything. We will only have an
1177 addend if this is a newly created reloc, not read from an ELF
1178 file. */
1179 if (output_bfd != (bfd *) NULL
1180 && (symbol->flags & BSF_SECTION_SYM) == 0
1181 && reloc_entry->addend == 0)
1182 {
1183 reloc_entry->address += input_section->output_offset;
1184 return bfd_reloc_ok;
1185 }
1186
1187 if (output_bfd != (bfd *) NULL)
1188 relocateable = true;
1189 else
1190 {
1191 relocateable = false;
1192 output_bfd = symbol->section->output_section->owner;
1193 }
1194
1195 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1196 &gp);
1197 if (ret != bfd_reloc_ok)
1198 return ret;
1199
1200 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1201 relocateable, data, gp);
1202 }
1203
1204 static bfd_reloc_status_type
1205 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1206 gp)
1207 bfd *abfd;
1208 asymbol *symbol;
1209 arelent *reloc_entry;
1210 asection *input_section;
1211 boolean relocateable;
1212 PTR data;
1213 bfd_vma gp;
1214 {
1215 bfd_vma relocation;
1216 unsigned long insn;
1217 unsigned long val;
1218
1219 if (bfd_is_com_section (symbol->section))
1220 relocation = 0;
1221 else
1222 relocation = symbol->value;
1223
1224 relocation += symbol->section->output_section->vma;
1225 relocation += symbol->section->output_offset;
1226
1227 if (reloc_entry->address > input_section->_cooked_size)
1228 return bfd_reloc_outofrange;
1229
1230 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1231
1232 /* Set val to the offset into the section or symbol. */
1233 if (reloc_entry->howto->src_mask == 0)
1234 {
1235 /* This case occurs with the 64-bit MIPS ELF ABI. */
1236 val = reloc_entry->addend;
1237 }
1238 else
1239 {
1240 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1241 if (val & 0x8000)
1242 val -= 0x10000;
1243 }
1244
1245 /* Adjust val for the final section location and GP value. If we
1246 are producing relocateable output, we don't want to do this for
1247 an external symbol. */
1248 if (! relocateable
1249 || (symbol->flags & BSF_SECTION_SYM) != 0)
1250 val += relocation - gp;
1251
1252 insn = (insn &~ 0xffff) | (val & 0xffff);
1253 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1254
1255 if (relocateable)
1256 reloc_entry->address += input_section->output_offset;
1257
1258 /* Make sure it fit in 16 bits. */
1259 if (val >= 0x8000 && val < 0xffff8000)
1260 return bfd_reloc_overflow;
1261
1262 return bfd_reloc_ok;
1263 }
1264
1265 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1266 from the gp register? XXX */
1267
1268 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1269 arelent *, asection *,
1270 boolean, PTR, bfd_vma));
1271
1272 bfd_reloc_status_type
1273 _bfd_mips_elf_gprel32_reloc (abfd,
1274 reloc_entry,
1275 symbol,
1276 data,
1277 input_section,
1278 output_bfd,
1279 error_message)
1280 bfd *abfd;
1281 arelent *reloc_entry;
1282 asymbol *symbol;
1283 PTR data;
1284 asection *input_section;
1285 bfd *output_bfd;
1286 char **error_message;
1287 {
1288 boolean relocateable;
1289 bfd_reloc_status_type ret;
1290 bfd_vma gp;
1291
1292 /* If we're relocating, and this is an external symbol with no
1293 addend, we don't want to change anything. We will only have an
1294 addend if this is a newly created reloc, not read from an ELF
1295 file. */
1296 if (output_bfd != (bfd *) NULL
1297 && (symbol->flags & BSF_SECTION_SYM) == 0
1298 && reloc_entry->addend == 0)
1299 {
1300 *error_message = (char *)
1301 _("32bits gp relative relocation occurs for an external symbol");
1302 return bfd_reloc_outofrange;
1303 }
1304
1305 if (output_bfd != (bfd *) NULL)
1306 {
1307 relocateable = true;
1308 gp = _bfd_get_gp_value (output_bfd);
1309 }
1310 else
1311 {
1312 relocateable = false;
1313 output_bfd = symbol->section->output_section->owner;
1314
1315 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1316 error_message, &gp);
1317 if (ret != bfd_reloc_ok)
1318 return ret;
1319 }
1320
1321 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1322 relocateable, data, gp);
1323 }
1324
1325 static bfd_reloc_status_type
1326 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1327 gp)
1328 bfd *abfd;
1329 asymbol *symbol;
1330 arelent *reloc_entry;
1331 asection *input_section;
1332 boolean relocateable;
1333 PTR data;
1334 bfd_vma gp;
1335 {
1336 bfd_vma relocation;
1337 unsigned long val;
1338
1339 if (bfd_is_com_section (symbol->section))
1340 relocation = 0;
1341 else
1342 relocation = symbol->value;
1343
1344 relocation += symbol->section->output_section->vma;
1345 relocation += symbol->section->output_offset;
1346
1347 if (reloc_entry->address > input_section->_cooked_size)
1348 return bfd_reloc_outofrange;
1349
1350 if (reloc_entry->howto->src_mask == 0)
1351 {
1352 /* This case arises with the 64-bit MIPS ELF ABI. */
1353 val = 0;
1354 }
1355 else
1356 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1357
1358 /* Set val to the offset into the section or symbol. */
1359 val += reloc_entry->addend;
1360
1361 /* Adjust val for the final section location and GP value. If we
1362 are producing relocateable output, we don't want to do this for
1363 an external symbol. */
1364 if (! relocateable
1365 || (symbol->flags & BSF_SECTION_SYM) != 0)
1366 val += relocation - gp;
1367
1368 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1369
1370 if (relocateable)
1371 reloc_entry->address += input_section->output_offset;
1372
1373 return bfd_reloc_ok;
1374 }
1375
1376 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1377 generated when addreses are 64 bits. The upper 32 bits are a simle
1378 sign extension. */
1379
1380 static bfd_reloc_status_type
1381 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1382 output_bfd, error_message)
1383 bfd *abfd;
1384 arelent *reloc_entry;
1385 asymbol *symbol;
1386 PTR data;
1387 asection *input_section;
1388 bfd *output_bfd;
1389 char **error_message;
1390 {
1391 bfd_reloc_status_type r;
1392 arelent reloc32;
1393 unsigned long val;
1394 bfd_size_type addr;
1395
1396 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1397 input_section, output_bfd, error_message);
1398 if (r != bfd_reloc_continue)
1399 return r;
1400
1401 /* Do a normal 32 bit relocation on the lower 32 bits. */
1402 reloc32 = *reloc_entry;
1403 if (bfd_big_endian (abfd))
1404 reloc32.address += 4;
1405 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1406 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1407 output_bfd, error_message);
1408
1409 /* Sign extend into the upper 32 bits. */
1410 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1411 if ((val & 0x80000000) != 0)
1412 val = 0xffffffff;
1413 else
1414 val = 0;
1415 addr = reloc_entry->address;
1416 if (bfd_little_endian (abfd))
1417 addr += 4;
1418 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1419
1420 return r;
1421 }
1422
1423 /* Handle a mips16 jump. */
1424
1425 static bfd_reloc_status_type
1426 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1427 output_bfd, error_message)
1428 bfd *abfd;
1429 arelent *reloc_entry;
1430 asymbol *symbol;
1431 PTR data;
1432 asection *input_section;
1433 bfd *output_bfd;
1434 char **error_message;
1435 {
1436 if (output_bfd != (bfd *) NULL
1437 && (symbol->flags & BSF_SECTION_SYM) == 0
1438 && reloc_entry->addend == 0)
1439 {
1440 reloc_entry->address += input_section->output_offset;
1441 return bfd_reloc_ok;
1442 }
1443
1444 /* FIXME. */
1445 {
1446 static boolean warned;
1447
1448 if (! warned)
1449 (*_bfd_error_handler)
1450 (_("Linking mips16 objects into %s format is not supported"),
1451 bfd_get_target (input_section->output_section->owner));
1452 warned = true;
1453 }
1454
1455 return bfd_reloc_undefined;
1456 }
1457
1458 /* Handle a mips16 GP relative reloc. */
1459
1460 static bfd_reloc_status_type
1461 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1462 output_bfd, error_message)
1463 bfd *abfd;
1464 arelent *reloc_entry;
1465 asymbol *symbol;
1466 PTR data;
1467 asection *input_section;
1468 bfd *output_bfd;
1469 char **error_message;
1470 {
1471 boolean relocateable;
1472 bfd_reloc_status_type ret;
1473 bfd_vma gp;
1474 unsigned short extend, insn;
1475 unsigned long final;
1476
1477 /* If we're relocating, and this is an external symbol with no
1478 addend, we don't want to change anything. We will only have an
1479 addend if this is a newly created reloc, not read from an ELF
1480 file. */
1481 if (output_bfd != NULL
1482 && (symbol->flags & BSF_SECTION_SYM) == 0
1483 && reloc_entry->addend == 0)
1484 {
1485 reloc_entry->address += input_section->output_offset;
1486 return bfd_reloc_ok;
1487 }
1488
1489 if (output_bfd != NULL)
1490 relocateable = true;
1491 else
1492 {
1493 relocateable = false;
1494 output_bfd = symbol->section->output_section->owner;
1495 }
1496
1497 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1498 &gp);
1499 if (ret != bfd_reloc_ok)
1500 return ret;
1501
1502 if (reloc_entry->address > input_section->_cooked_size)
1503 return bfd_reloc_outofrange;
1504
1505 /* Pick up the mips16 extend instruction and the real instruction. */
1506 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1507 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1508
1509 /* Stuff the current addend back as a 32 bit value, do the usual
1510 relocation, and then clean up. */
1511 bfd_put_32 (abfd,
1512 (((extend & 0x1f) << 11)
1513 | (extend & 0x7e0)
1514 | (insn & 0x1f)),
1515 (bfd_byte *) data + reloc_entry->address);
1516
1517 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1518 relocateable, data, gp);
1519
1520 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1521 bfd_put_16 (abfd,
1522 ((extend & 0xf800)
1523 | ((final >> 11) & 0x1f)
1524 | (final & 0x7e0)),
1525 (bfd_byte *) data + reloc_entry->address);
1526 bfd_put_16 (abfd,
1527 ((insn & 0xffe0)
1528 | (final & 0x1f)),
1529 (bfd_byte *) data + reloc_entry->address + 2);
1530
1531 return ret;
1532 }
1533
1534 /* Return the ISA for a MIPS e_flags value. */
1535
1536 static INLINE int
1537 elf_mips_isa (flags)
1538 flagword flags;
1539 {
1540 switch (flags & EF_MIPS_ARCH)
1541 {
1542 case E_MIPS_ARCH_1:
1543 return 1;
1544 case E_MIPS_ARCH_2:
1545 return 2;
1546 case E_MIPS_ARCH_3:
1547 return 3;
1548 case E_MIPS_ARCH_4:
1549 return 4;
1550 }
1551 return 4;
1552 }
1553
1554 /* Return the MACH for a MIPS e_flags value. */
1555
1556 static INLINE int
1557 elf_mips_mach (flags)
1558 flagword flags;
1559 {
1560 switch (flags & EF_MIPS_MACH)
1561 {
1562 case E_MIPS_MACH_3900:
1563 return bfd_mach_mips3900;
1564
1565 case E_MIPS_MACH_4010:
1566 return bfd_mach_mips4010;
1567
1568 case E_MIPS_MACH_4100:
1569 return bfd_mach_mips4100;
1570 /* start-sanitize-vr4320 */
1571
1572 case E_MIPS_MACH_4320:
1573 return bfd_mach_mips4320;
1574 /* end-sanitize-vr4320 */
1575
1576 case E_MIPS_MACH_4650:
1577 return bfd_mach_mips4650;
1578 /* start-sanitize-tx49 */
1579
1580 case E_MIPS_MACH_4900:
1581 return bfd_mach_mips4900;
1582 /* end-sanitize-tx49 */
1583 /* start-sanitize-vr5400 */
1584
1585 case E_MIPS_MACH_5400:
1586 return bfd_mach_mips5400;
1587 /* end-sanitize-vr5400 */
1588 /* start-sanitize-r5900 */
1589
1590 case E_MIPS_MACH_5900:
1591 return bfd_mach_mips5900;
1592 /* end-sanitize-r5900 */
1593
1594 default:
1595 switch (flags & EF_MIPS_ARCH)
1596 {
1597 default:
1598 case E_MIPS_ARCH_1:
1599 return bfd_mach_mips3000;
1600 break;
1601
1602 case E_MIPS_ARCH_2:
1603 return bfd_mach_mips6000;
1604 break;
1605
1606 case E_MIPS_ARCH_3:
1607 return bfd_mach_mips4000;
1608 break;
1609
1610 case E_MIPS_ARCH_4:
1611 return bfd_mach_mips8000;
1612 break;
1613 }
1614 }
1615
1616 return 0;
1617 }
1618
1619 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1620
1621 struct elf_reloc_map {
1622 bfd_reloc_code_real_type bfd_reloc_val;
1623 enum reloc_type elf_reloc_val;
1624 };
1625
1626 static CONST struct elf_reloc_map mips_reloc_map[] =
1627 {
1628 { BFD_RELOC_NONE, R_MIPS_NONE, },
1629 { BFD_RELOC_16, R_MIPS_16 },
1630 { BFD_RELOC_32, R_MIPS_32 },
1631 { BFD_RELOC_64, R_MIPS_64 },
1632 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1633 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1634 { BFD_RELOC_LO16, R_MIPS_LO16 },
1635 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1636 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1637 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1638 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1639 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1640 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1641 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1642 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1643 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1644 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1645 /* start-sanitize-r5900 */
1646 { BFD_RELOC_MIPS15_S3, R_MIPS15_S3 },
1647 /* end-sanitize-r5900 */
1648 };
1649
1650 /* Given a BFD reloc type, return a howto structure. */
1651
1652 static reloc_howto_type *
1653 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1654 bfd *abfd;
1655 bfd_reloc_code_real_type code;
1656 {
1657 unsigned int i;
1658
1659 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1660 {
1661 if (mips_reloc_map[i].bfd_reloc_val == code)
1662 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1663 }
1664
1665 /* We need to handle BFD_RELOC_CTOR specially.
1666
1667 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1668 size of addresses on this architecture. */
1669 if (code == BFD_RELOC_CTOR)
1670 {
1671 if (bfd_arch_bits_per_address (abfd) == 32)
1672 return &elf_mips_howto_table[(int) R_MIPS_32];
1673 else
1674 return &elf_mips_ctor64_howto;
1675 }
1676
1677 /* Special handling for the MIPS16 relocs, since they are made up
1678 reloc types with a large value. */
1679 if (code == BFD_RELOC_MIPS16_JMP)
1680 return &elf_mips16_jump_howto;
1681 else if (code == BFD_RELOC_MIPS16_GPREL)
1682 return &elf_mips16_gprel_howto;
1683 /* start-sanitize-sky */
1684 else if (code == BFD_RELOC_MIPS_DVP_11_PCREL)
1685 return &elf_mips_dvp_11_pcrel_howto;
1686 else if (code == BFD_RELOC_MIPS_DVP_27_S4)
1687 return &elf_mips_dvp_27_s4_howto;
1688 /* end-sanitize-sky */
1689
1690 return NULL;
1691 }
1692
1693 /* Given a MIPS reloc type, fill in an arelent structure. */
1694
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;
1700 {
1701 unsigned int r_type;
1702
1703 r_type = ELF32_R_TYPE (dst->r_info);
1704 if (r_type == R_MIPS16_26)
1705 cache_ptr->howto = &elf_mips16_jump_howto;
1706 else if (r_type == R_MIPS16_GPREL)
1707 cache_ptr->howto = &elf_mips16_gprel_howto;
1708 /* start-sanitize-sky */
1709 else if (r_type == R_MIPS_DVP_11_PCREL)
1710 cache_ptr->howto = &elf_mips_dvp_11_pcrel_howto;
1711 else if (r_type == R_MIPS_DVP_27_S4)
1712 cache_ptr->howto = &elf_mips_dvp_27_s4_howto;
1713 /* end-sanitize-sky */
1714 else
1715 {
1716 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1717 cache_ptr->howto = &elf_mips_howto_table[r_type];
1718 }
1719
1720 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1721 value for the object file. We get the addend now, rather than
1722 when we do the relocation, because the symbol manipulations done
1723 by the linker may cause us to lose track of the input BFD. */
1724 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1725 && (r_type == (unsigned int) R_MIPS_GPREL16
1726 || r_type == (unsigned int) R_MIPS_LITERAL))
1727 cache_ptr->addend = elf_gp (abfd);
1728 }
1729 \f
1730 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1731 routines swap this structure in and out. They are used outside of
1732 BFD, so they are globally visible. */
1733
1734 void
1735 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1736 bfd *abfd;
1737 const Elf32_External_RegInfo *ex;
1738 Elf32_RegInfo *in;
1739 {
1740 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1741 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1742 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1743 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1744 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1745 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1746 }
1747
1748 void
1749 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1750 bfd *abfd;
1751 const Elf32_RegInfo *in;
1752 Elf32_External_RegInfo *ex;
1753 {
1754 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1755 (bfd_byte *) ex->ri_gprmask);
1756 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1757 (bfd_byte *) ex->ri_cprmask[0]);
1758 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1759 (bfd_byte *) ex->ri_cprmask[1]);
1760 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1761 (bfd_byte *) ex->ri_cprmask[2]);
1762 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1763 (bfd_byte *) ex->ri_cprmask[3]);
1764 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1765 (bfd_byte *) ex->ri_gp_value);
1766 }
1767
1768 /* In the 64 bit ABI, the .MIPS.options section holds register
1769 information in an Elf64_Reginfo structure. These routines swap
1770 them in and out. They are globally visible because they are used
1771 outside of BFD. These routines are here so that gas can call them
1772 without worrying about whether the 64 bit ABI has been included. */
1773
1774 void
1775 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1776 bfd *abfd;
1777 const Elf64_External_RegInfo *ex;
1778 Elf64_Internal_RegInfo *in;
1779 {
1780 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1781 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1782 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1783 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1784 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1785 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1786 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1787 }
1788
1789 void
1790 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1791 bfd *abfd;
1792 const Elf64_Internal_RegInfo *in;
1793 Elf64_External_RegInfo *ex;
1794 {
1795 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1796 (bfd_byte *) ex->ri_gprmask);
1797 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1798 (bfd_byte *) ex->ri_pad);
1799 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1800 (bfd_byte *) ex->ri_cprmask[0]);
1801 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1802 (bfd_byte *) ex->ri_cprmask[1]);
1803 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1804 (bfd_byte *) ex->ri_cprmask[2]);
1805 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1806 (bfd_byte *) ex->ri_cprmask[3]);
1807 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1808 (bfd_byte *) ex->ri_gp_value);
1809 }
1810
1811 /* Swap an entry in a .gptab section. Note that these routines rely
1812 on the equivalence of the two elements of the union. */
1813
1814 static void
1815 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1816 bfd *abfd;
1817 const Elf32_External_gptab *ex;
1818 Elf32_gptab *in;
1819 {
1820 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1821 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1822 }
1823
1824 static void
1825 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1826 bfd *abfd;
1827 const Elf32_gptab *in;
1828 Elf32_External_gptab *ex;
1829 {
1830 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1831 ex->gt_entry.gt_g_value);
1832 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1833 ex->gt_entry.gt_bytes);
1834 }
1835
1836 static void
1837 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1838 bfd *abfd;
1839 const Elf32_compact_rel *in;
1840 Elf32_External_compact_rel *ex;
1841 {
1842 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1843 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1844 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1845 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1846 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1847 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1848 }
1849
1850 static void
1851 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1852 bfd *abfd;
1853 const Elf32_crinfo *in;
1854 Elf32_External_crinfo *ex;
1855 {
1856 unsigned long l;
1857
1858 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1859 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1860 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1861 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1862 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1863 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1864 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1865 }
1866
1867 /* Swap in an options header. */
1868
1869 void
1870 bfd_mips_elf_swap_options_in (abfd, ex, in)
1871 bfd *abfd;
1872 const Elf_External_Options *ex;
1873 Elf_Internal_Options *in;
1874 {
1875 in->kind = bfd_h_get_8 (abfd, ex->kind);
1876 in->size = bfd_h_get_8 (abfd, ex->size);
1877 in->section = bfd_h_get_16 (abfd, ex->section);
1878 in->info = bfd_h_get_32 (abfd, ex->info);
1879 }
1880
1881 /* Swap out an options header. */
1882
1883 void
1884 bfd_mips_elf_swap_options_out (abfd, in, ex)
1885 bfd *abfd;
1886 const Elf_Internal_Options *in;
1887 Elf_External_Options *ex;
1888 {
1889 bfd_h_put_8 (abfd, in->kind, ex->kind);
1890 bfd_h_put_8 (abfd, in->size, ex->size);
1891 bfd_h_put_16 (abfd, in->section, ex->section);
1892 bfd_h_put_32 (abfd, in->info, ex->info);
1893 }
1894 \f
1895 /* Determine whether a symbol is global for the purposes of splitting
1896 the symbol table into global symbols and local symbols. At least
1897 on Irix 5, this split must be between section symbols and all other
1898 symbols. On most ELF targets the split is between static symbols
1899 and externally visible symbols. */
1900
1901 /*ARGSUSED*/
1902 static boolean
1903 mips_elf_sym_is_global (abfd, sym)
1904 bfd *abfd;
1905 asymbol *sym;
1906 {
1907 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1908 }
1909 \f
1910 /* Set the right machine number for a MIPS ELF file. This is used for
1911 both the 32-bit and the 64-bit ABI. */
1912
1913 boolean
1914 _bfd_mips_elf_object_p (abfd)
1915 bfd *abfd;
1916 {
1917 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
1918 elf_mips_mach (elf_elfheader (abfd)->e_flags));
1919 return true;
1920 }
1921
1922 /* Set the right machine number for a 32-bit MIPS ELF file. */
1923
1924 static boolean
1925 mips_elf32_object_p (abfd)
1926 bfd *abfd;
1927 {
1928 /* Irix 5 is broken. Object file symbol tables are not always
1929 sorted correctly such that local symbols precede global symbols,
1930 and the sh_info field in the symbol table is not always right. */
1931 elf_bad_symtab (abfd) = true;
1932
1933 return _bfd_mips_elf_object_p (abfd);
1934 }
1935
1936 /* The final processing done just before writing out a MIPS ELF object
1937 file. This gets the MIPS architecture right based on the machine
1938 number. This is used by both the 32-bit and the 64-bit ABI. */
1939
1940 /*ARGSUSED*/
1941 void
1942 _bfd_mips_elf_final_write_processing (abfd, linker)
1943 bfd *abfd;
1944 boolean linker;
1945 {
1946 unsigned long val;
1947 unsigned int i;
1948 Elf_Internal_Shdr **hdrpp;
1949 const char *name;
1950 asection *sec;
1951
1952 switch (bfd_get_mach (abfd))
1953 {
1954 default:
1955 case bfd_mach_mips3000:
1956 val = E_MIPS_ARCH_1;
1957 break;
1958
1959 case bfd_mach_mips3900:
1960 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
1961 break;
1962
1963 case bfd_mach_mips6000:
1964 val = E_MIPS_ARCH_2;
1965 break;
1966
1967 case bfd_mach_mips4000:
1968 val = E_MIPS_ARCH_3;
1969 break;
1970
1971 case bfd_mach_mips4010:
1972 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
1973 break;
1974
1975 case bfd_mach_mips4100:
1976 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
1977 break;
1978 /* start-sanitize-vr4320 */
1979
1980 case bfd_mach_mips4320:
1981 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4320;
1982 break;
1983 /* end-sanitize-vr4320 */
1984
1985 case bfd_mach_mips4650:
1986 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
1987 break;
1988 /* start-sanitize-tx49 */
1989
1990 case bfd_mach_mips4900:
1991 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4900;
1992 break;
1993 /* end-sanitize-tx49 */
1994 /* start-sanitize-vr5400 */
1995
1996 case bfd_mach_mips5400:
1997 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5400;
1998 break;
1999 /* end-sanitize-vr5400 */
2000 /* start-sanitize-r5900 */
2001
2002 case bfd_mach_mips5900:
2003 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
2004 break;
2005 /* end-sanitize-r5900 */
2006
2007 case bfd_mach_mips8000:
2008 val = E_MIPS_ARCH_4;
2009 break;
2010 }
2011
2012 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2013 elf_elfheader (abfd)->e_flags |= val;
2014
2015 /* Set the sh_info field for .gptab sections and other appropriate
2016 info for each special section. */
2017 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2018 i < elf_elfheader (abfd)->e_shnum;
2019 i++, hdrpp++)
2020 {
2021 switch ((*hdrpp)->sh_type)
2022 {
2023 case SHT_MIPS_LIBLIST:
2024 sec = bfd_get_section_by_name (abfd, ".dynstr");
2025 if (sec != NULL)
2026 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2027 break;
2028
2029 case SHT_MIPS_GPTAB:
2030 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2031 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2032 BFD_ASSERT (name != NULL
2033 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2034 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2035 BFD_ASSERT (sec != NULL);
2036 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2037 break;
2038
2039 case SHT_MIPS_CONTENT:
2040 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2041 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2042 BFD_ASSERT (name != NULL
2043 && strncmp (name, ".MIPS.content",
2044 sizeof ".MIPS.content" - 1) == 0);
2045 sec = bfd_get_section_by_name (abfd,
2046 name + sizeof ".MIPS.content" - 1);
2047 BFD_ASSERT (sec != NULL);
2048 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2049 break;
2050
2051 case SHT_MIPS_SYMBOL_LIB:
2052 sec = bfd_get_section_by_name (abfd, ".dynsym");
2053 if (sec != NULL)
2054 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2055 sec = bfd_get_section_by_name (abfd, ".liblist");
2056 if (sec != NULL)
2057 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2058 break;
2059
2060 case SHT_MIPS_EVENTS:
2061 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2062 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2063 BFD_ASSERT (name != NULL);
2064 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2065 sec = bfd_get_section_by_name (abfd,
2066 name + sizeof ".MIPS.events" - 1);
2067 else
2068 {
2069 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2070 sizeof ".MIPS.post_rel" - 1) == 0);
2071 sec = bfd_get_section_by_name (abfd,
2072 (name
2073 + sizeof ".MIPS.post_rel" - 1));
2074 }
2075 BFD_ASSERT (sec != NULL);
2076 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2077 break;
2078
2079 /* start-sanitize-sky */
2080 case SHT_DVP_OVERLAY_TABLE:
2081 /* ??? This may not be technically necessary, just going with
2082 the flow ... */
2083 sec = bfd_get_section_by_name (abfd, SHNAME_DVP_OVERLAY_STRTAB);
2084 if (sec != NULL)
2085 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2086 break;
2087 /* end-sanitize-sky */
2088 }
2089 }
2090 }
2091 \f
2092 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2093
2094 boolean
2095 _bfd_mips_elf_set_private_flags (abfd, flags)
2096 bfd *abfd;
2097 flagword flags;
2098 {
2099 BFD_ASSERT (!elf_flags_init (abfd)
2100 || elf_elfheader (abfd)->e_flags == flags);
2101
2102 elf_elfheader (abfd)->e_flags = flags;
2103 elf_flags_init (abfd) = true;
2104 return true;
2105 }
2106
2107 /* Copy backend specific data from one object module to another */
2108
2109 boolean
2110 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2111 bfd *ibfd;
2112 bfd *obfd;
2113 {
2114 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2115 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2116 return true;
2117
2118 BFD_ASSERT (!elf_flags_init (obfd)
2119 || (elf_elfheader (obfd)->e_flags
2120 == elf_elfheader (ibfd)->e_flags));
2121
2122 elf_gp (obfd) = elf_gp (ibfd);
2123 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2124 elf_flags_init (obfd) = true;
2125 return true;
2126 }
2127
2128 /* Merge backend specific data from an object file to the output
2129 object file when linking. */
2130
2131 boolean
2132 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2133 bfd *ibfd;
2134 bfd *obfd;
2135 {
2136 flagword old_flags;
2137 flagword new_flags;
2138 boolean ok;
2139
2140 /* Check if we have the same endianess */
2141 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2142 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2143 {
2144 const char *msg;
2145
2146 if (bfd_big_endian (ibfd))
2147 msg = _("%s: compiled for a big endian system and target is little endian");
2148 else
2149 msg = _("%s: compiled for a little endian system and target is big endian");
2150
2151 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2152
2153 bfd_set_error (bfd_error_wrong_format);
2154 return false;
2155 }
2156
2157 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2158 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2159 return true;
2160
2161 new_flags = elf_elfheader (ibfd)->e_flags;
2162 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2163 old_flags = elf_elfheader (obfd)->e_flags;
2164
2165 if (! elf_flags_init (obfd))
2166 {
2167 elf_flags_init (obfd) = true;
2168 elf_elfheader (obfd)->e_flags = new_flags;
2169
2170 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2171 && bfd_get_arch_info (obfd)->the_default)
2172 {
2173 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2174 bfd_get_mach (ibfd)))
2175 return false;
2176 }
2177
2178 return true;
2179 }
2180
2181 /* Check flag compatibility. */
2182
2183 new_flags &= ~EF_MIPS_NOREORDER;
2184 old_flags &= ~EF_MIPS_NOREORDER;
2185
2186 if (new_flags == old_flags)
2187 return true;
2188
2189 ok = true;
2190
2191 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2192 {
2193 new_flags &= ~EF_MIPS_PIC;
2194 old_flags &= ~EF_MIPS_PIC;
2195 (*_bfd_error_handler)
2196 (_("%s: linking PIC files with non-PIC files"),
2197 bfd_get_filename (ibfd));
2198 ok = false;
2199 }
2200
2201 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2202 {
2203 new_flags &= ~EF_MIPS_CPIC;
2204 old_flags &= ~EF_MIPS_CPIC;
2205 (*_bfd_error_handler)
2206 (_("%s: linking abicalls files with non-abicalls files"),
2207 bfd_get_filename (ibfd));
2208 ok = false;
2209 }
2210
2211 /* Compare the ISA's. */
2212 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2213 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2214 {
2215 /* If either has no machine specified, just compare the general isa's. */
2216 if ( !(new_flags & EF_MIPS_MACH) || !(old_flags & EF_MIPS_MACH))
2217 {
2218 int new_isa, old_isa;
2219
2220 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2221 and -mips4 code. They will normally use the same data sizes and
2222 calling conventions. */
2223
2224 new_isa = elf_mips_isa (new_flags);
2225 old_isa = elf_mips_isa (old_flags);
2226 if ((new_isa == 1 || new_isa == 2)
2227 ? (old_isa != 1 && old_isa != 2)
2228 : (old_isa == 1 || old_isa == 2))
2229 {
2230 (*_bfd_error_handler)
2231 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2232 bfd_get_filename (ibfd), new_isa, old_isa);
2233 ok = false;
2234 }
2235 }
2236
2237 else
2238 {
2239 (*_bfd_error_handler)
2240 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2241 bfd_get_filename (ibfd),
2242 elf_mips_mach (new_flags),
2243 elf_mips_mach (old_flags));
2244 ok = false;
2245 }
2246
2247 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2248 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2249 }
2250
2251 /* Warn about any other mismatches */
2252 if (new_flags != old_flags)
2253 {
2254 (*_bfd_error_handler)
2255 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2256 bfd_get_filename (ibfd), (unsigned long) new_flags,
2257 (unsigned long) old_flags);
2258 ok = false;
2259 }
2260
2261 if (! ok)
2262 {
2263 bfd_set_error (bfd_error_bad_value);
2264 return false;
2265 }
2266
2267 return true;
2268 }
2269 \f
2270 /* Handle a MIPS specific section when reading an object file. This
2271 is called when elfcode.h finds a section with an unknown type.
2272 This routine supports both the 32-bit and 64-bit ELF ABI.
2273
2274 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2275 how to. */
2276
2277 boolean
2278 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2279 bfd *abfd;
2280 Elf_Internal_Shdr *hdr;
2281 const char *name;
2282 {
2283 /* There ought to be a place to keep ELF backend specific flags, but
2284 at the moment there isn't one. We just keep track of the
2285 sections by their name, instead. Fortunately, the ABI gives
2286 suggested names for all the MIPS specific sections, so we will
2287 probably get away with this. */
2288 switch (hdr->sh_type)
2289 {
2290 case SHT_MIPS_LIBLIST:
2291 if (strcmp (name, _(".liblist")) != 0)
2292 return false;
2293 break;
2294 case SHT_MIPS_MSYM:
2295 if (strcmp (name, ".msym") != 0)
2296 return false;
2297 break;
2298 case SHT_MIPS_CONFLICT:
2299 if (strcmp (name, ".conflict") != 0)
2300 return false;
2301 break;
2302 case SHT_MIPS_GPTAB:
2303 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2304 return false;
2305 break;
2306 case SHT_MIPS_UCODE:
2307 if (strcmp (name, ".ucode") != 0)
2308 return false;
2309 break;
2310 case SHT_MIPS_DEBUG:
2311 if (strcmp (name, ".mdebug") != 0)
2312 return false;
2313 break;
2314 case SHT_MIPS_REGINFO:
2315 if (strcmp (name, ".reginfo") != 0
2316 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2317 return false;
2318 break;
2319 case SHT_MIPS_IFACE:
2320 if (strcmp (name, ".MIPS.interfaces") != 0)
2321 return false;
2322 break;
2323 case SHT_MIPS_CONTENT:
2324 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2325 return false;
2326 break;
2327 case SHT_MIPS_OPTIONS:
2328 if (strcmp (name, ".options") != 0
2329 && strcmp (name, ".MIPS.options") != 0)
2330 return false;
2331 break;
2332 case SHT_MIPS_DWARF:
2333 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2334 return false;
2335 break;
2336 case SHT_MIPS_SYMBOL_LIB:
2337 if (strcmp (name, ".MIPS.symlib") != 0)
2338 return false;
2339 break;
2340 case SHT_MIPS_EVENTS:
2341 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2342 && strncmp (name, ".MIPS.post_rel",
2343 sizeof ".MIPS.post_rel" - 1) != 0)
2344 return false;
2345 break;
2346 /* start-sanitize-sky */
2347 case SHT_DVP_OVERLAY_TABLE:
2348 if (strcmp (name, SHNAME_DVP_OVERLAY_TABLE) !=0)
2349 return false;
2350 break;
2351 case SHT_DVP_OVERLAY:
2352 if (strncmp (name, SHNAME_DVP_OVERLAY_PREFIX,
2353 sizeof (SHNAME_DVP_OVERLAY_PREFIX) - 1) !=0)
2354 return false;
2355 break;
2356 /* end-sanitize-sky */
2357 default:
2358 return false;
2359 }
2360
2361 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2362 return false;
2363
2364 if (hdr->sh_type == SHT_MIPS_DEBUG)
2365 {
2366 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2367 (bfd_get_section_flags (abfd,
2368 hdr->bfd_section)
2369 | SEC_DEBUGGING)))
2370 return false;
2371 }
2372
2373 return true;
2374 }
2375
2376 /* Handle a 32-bit MIPS ELF specific section. */
2377
2378 static boolean
2379 mips_elf32_section_from_shdr (abfd, hdr, name)
2380 bfd *abfd;
2381 Elf_Internal_Shdr *hdr;
2382 char *name;
2383 {
2384 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2385 return false;
2386
2387 /* FIXME: We should record sh_info for a .gptab section. */
2388
2389 /* For a .reginfo section, set the gp value in the tdata information
2390 from the contents of this section. We need the gp value while
2391 processing relocs, so we just get it now. The .reginfo section
2392 is not used in the 64-bit MIPS ELF ABI. */
2393 if (hdr->sh_type == SHT_MIPS_REGINFO)
2394 {
2395 Elf32_External_RegInfo ext;
2396 Elf32_RegInfo s;
2397
2398 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2399 (file_ptr) 0, sizeof ext))
2400 return false;
2401 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2402 elf_gp (abfd) = s.ri_gp_value;
2403 }
2404
2405 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2406 set the gp value based on what we find. We may see both
2407 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2408 they should agree. */
2409 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2410 {
2411 bfd_byte *contents, *l, *lend;
2412
2413 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2414 if (contents == NULL)
2415 return false;
2416 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2417 (file_ptr) 0, hdr->sh_size))
2418 {
2419 free (contents);
2420 return false;
2421 }
2422 l = contents;
2423 lend = contents + hdr->sh_size;
2424 while (l + sizeof (Elf_External_Options) <= lend)
2425 {
2426 Elf_Internal_Options intopt;
2427
2428 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2429 &intopt);
2430 if (intopt.kind == ODK_REGINFO)
2431 {
2432 Elf32_RegInfo intreg;
2433
2434 bfd_mips_elf32_swap_reginfo_in
2435 (abfd,
2436 ((Elf32_External_RegInfo *)
2437 (l + sizeof (Elf_External_Options))),
2438 &intreg);
2439 elf_gp (abfd) = intreg.ri_gp_value;
2440 }
2441 l += intopt.size;
2442 }
2443 free (contents);
2444 }
2445
2446 return true;
2447 }
2448
2449 /* Set the correct type for a MIPS ELF section. We do this by the
2450 section name, which is a hack, but ought to work. This routine is
2451 used by both the 32-bit and the 64-bit ABI. */
2452
2453 boolean
2454 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2455 bfd *abfd;
2456 Elf32_Internal_Shdr *hdr;
2457 asection *sec;
2458 {
2459 register const char *name;
2460
2461 name = bfd_get_section_name (abfd, sec);
2462
2463 if (strcmp (name, ".liblist") == 0)
2464 {
2465 hdr->sh_type = SHT_MIPS_LIBLIST;
2466 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2467 /* The sh_link field is set in final_write_processing. */
2468 }
2469 else if (strcmp (name, ".msym") == 0)
2470 {
2471 hdr->sh_type = SHT_MIPS_MSYM;
2472 hdr->sh_entsize = 8;
2473 /* FIXME: Set the sh_info field. */
2474 }
2475 else if (strcmp (name, ".conflict") == 0)
2476 hdr->sh_type = SHT_MIPS_CONFLICT;
2477 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2478 {
2479 hdr->sh_type = SHT_MIPS_GPTAB;
2480 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2481 /* The sh_info field is set in final_write_processing. */
2482 }
2483 else if (strcmp (name, ".ucode") == 0)
2484 hdr->sh_type = SHT_MIPS_UCODE;
2485 else if (strcmp (name, ".mdebug") == 0)
2486 {
2487 hdr->sh_type = SHT_MIPS_DEBUG;
2488 /* In a shared object on Irix 5.3, the .mdebug section has an
2489 entsize of 0. FIXME: Does this matter? */
2490 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2491 hdr->sh_entsize = 0;
2492 else
2493 hdr->sh_entsize = 1;
2494 }
2495 else if (strcmp (name, ".reginfo") == 0)
2496 {
2497 hdr->sh_type = SHT_MIPS_REGINFO;
2498 /* In a shared object on Irix 5.3, the .reginfo section has an
2499 entsize of 0x18. FIXME: Does this matter? */
2500 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2501 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2502 else
2503 hdr->sh_entsize = 1;
2504
2505 /* Force the section size to the correct value, even if the
2506 linker thinks it is larger. The link routine below will only
2507 write out this much data for .reginfo. */
2508 hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
2509 }
2510 else if (SGI_COMPAT (abfd)
2511 && (strcmp (name, ".hash") == 0
2512 || strcmp (name, ".dynamic") == 0
2513 || strcmp (name, ".dynstr") == 0))
2514 {
2515 hdr->sh_entsize = 0;
2516 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2517 }
2518 else if (strcmp (name, ".got") == 0
2519 || strcmp (name, ".sdata") == 0
2520 || strcmp (name, ".sbss") == 0
2521 || strcmp (name, ".lit4") == 0
2522 || strcmp (name, ".lit8") == 0)
2523 hdr->sh_flags |= SHF_MIPS_GPREL;
2524 else if (strcmp (name, ".MIPS.interfaces") == 0)
2525 {
2526 hdr->sh_type = SHT_MIPS_IFACE;
2527 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2528 }
2529 else if (strcmp (name, ".MIPS.content") == 0)
2530 {
2531 hdr->sh_type = SHT_MIPS_CONTENT;
2532 /* The sh_info field is set in final_write_processing. */
2533 }
2534 else if (strcmp (name, ".options") == 0
2535 || strcmp (name, ".MIPS.options") == 0)
2536 {
2537 hdr->sh_type = SHT_MIPS_OPTIONS;
2538 hdr->sh_entsize = 1;
2539 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2540 }
2541 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2542 hdr->sh_type = SHT_MIPS_DWARF;
2543 else if (strcmp (name, ".MIPS.symlib") == 0)
2544 {
2545 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2546 /* The sh_link and sh_info fields are set in
2547 final_write_processing. */
2548 }
2549 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2550 || strncmp (name, ".MIPS.post_rel",
2551 sizeof ".MIPS.post_rel" - 1) == 0)
2552 {
2553 hdr->sh_type = SHT_MIPS_EVENTS;
2554 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2555 /* The sh_link field is set in final_write_processing. */
2556 }
2557 /* start-sanitize-sky */
2558 else if (strcmp (name, SHNAME_DVP_OVERLAY_TABLE) == 0)
2559 {
2560 hdr->sh_type = SHT_DVP_OVERLAY_TABLE;
2561 hdr->sh_entsize = sizeof (Elf64_Dvp_External_Overlay);
2562 /* The sh_link field is set in final_write_processing. */
2563 }
2564 else if (strcmp (name, SHNAME_DVP_OVERLAY_STRTAB) == 0)
2565 hdr->sh_type = SHT_STRTAB;
2566 else if (strncmp (name, SHNAME_DVP_OVERLAY_PREFIX,
2567 sizeof (SHNAME_DVP_OVERLAY_PREFIX) - 1) == 0)
2568 hdr->sh_type = SHT_DVP_OVERLAY;
2569 /* end-sanitize-sky */
2570
2571 return true;
2572 }
2573
2574 /* Given a BFD section, try to locate the corresponding ELF section
2575 index. This is used by both the 32-bit and the 64-bit ABI.
2576 Actually, it's not clear to me that the 64-bit ABI supports these,
2577 but for non-PIC objects we will certainly want support for at least
2578 the .scommon section. */
2579
2580 boolean
2581 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2582 bfd *abfd;
2583 Elf32_Internal_Shdr *hdr;
2584 asection *sec;
2585 int *retval;
2586 {
2587 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2588 {
2589 *retval = SHN_MIPS_SCOMMON;
2590 return true;
2591 }
2592 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2593 {
2594 *retval = SHN_MIPS_ACOMMON;
2595 return true;
2596 }
2597 return false;
2598 }
2599
2600 /* When are writing out the .options or .MIPS.options section,
2601 remember the bytes we are writing out, so that we can install the
2602 GP value in the section_processing routine. */
2603
2604 boolean
2605 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2606 bfd *abfd;
2607 sec_ptr section;
2608 PTR location;
2609 file_ptr offset;
2610 bfd_size_type count;
2611 {
2612 if (strcmp (section->name, ".options") == 0
2613 || strcmp (section->name, ".MIPS.options") == 0)
2614 {
2615 bfd_byte *c;
2616
2617 if (elf_section_data (section) == NULL)
2618 {
2619 section->used_by_bfd =
2620 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2621 if (elf_section_data (section) == NULL)
2622 return false;
2623 }
2624 c = (bfd_byte *) elf_section_data (section)->tdata;
2625 if (c == NULL)
2626 {
2627 bfd_size_type size;
2628
2629 if (section->_cooked_size != 0)
2630 size = section->_cooked_size;
2631 else
2632 size = section->_raw_size;
2633 c = (bfd_byte *) bfd_zalloc (abfd, size);
2634 if (c == NULL)
2635 return false;
2636 elf_section_data (section)->tdata = (PTR) c;
2637 }
2638
2639 memcpy (c + offset, location, count);
2640 }
2641
2642 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2643 count);
2644 }
2645
2646 /* Work over a section just before writing it out. This routine is
2647 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2648 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2649 a better way. */
2650
2651 boolean
2652 _bfd_mips_elf_section_processing (abfd, hdr)
2653 bfd *abfd;
2654 Elf_Internal_Shdr *hdr;
2655 {
2656 if (hdr->bfd_section != NULL)
2657 {
2658 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2659
2660 if (strcmp (name, ".sdata") == 0)
2661 {
2662 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2663 hdr->sh_type = SHT_PROGBITS;
2664 }
2665 else if (strcmp (name, ".sbss") == 0)
2666 {
2667 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2668 hdr->sh_type = SHT_NOBITS;
2669 }
2670 else if (strcmp (name, ".lit8") == 0
2671 || strcmp (name, ".lit4") == 0)
2672 {
2673 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2674 hdr->sh_type = SHT_PROGBITS;
2675 }
2676 else if (strcmp (name, ".compact_rel") == 0)
2677 {
2678 hdr->sh_flags = 0;
2679 hdr->sh_type = SHT_PROGBITS;
2680 }
2681 else if (strcmp (name, ".rtproc") == 0)
2682 {
2683 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2684 {
2685 unsigned int adjust;
2686
2687 adjust = hdr->sh_size % hdr->sh_addralign;
2688 if (adjust != 0)
2689 hdr->sh_size += hdr->sh_addralign - adjust;
2690 }
2691 }
2692 }
2693
2694 return true;
2695 }
2696
2697 /* Work over a section just before writing it out. We update the GP
2698 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2699 on the value we are using. */
2700
2701 static boolean
2702 mips_elf32_section_processing (abfd, hdr)
2703 bfd *abfd;
2704 Elf32_Internal_Shdr *hdr;
2705 {
2706 if (hdr->sh_type == SHT_MIPS_REGINFO)
2707 {
2708 bfd_byte buf[4];
2709
2710 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2711 BFD_ASSERT (hdr->contents == NULL);
2712
2713 if (bfd_seek (abfd,
2714 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2715 SEEK_SET) == -1)
2716 return false;
2717 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2718 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2719 return false;
2720 }
2721
2722 if (hdr->sh_type == SHT_MIPS_OPTIONS
2723 && hdr->bfd_section != NULL
2724 && elf_section_data (hdr->bfd_section) != NULL
2725 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2726 {
2727 bfd_byte *contents, *l, *lend;
2728
2729 /* We stored the section contents in the elf_section_data tdata
2730 field in the set_section_contents routine. We save the
2731 section contents so that we don't have to read them again.
2732 At this point we know that elf_gp is set, so we can look
2733 through the section contents to see if there is an
2734 ODK_REGINFO structure. */
2735
2736 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2737 l = contents;
2738 lend = contents + hdr->sh_size;
2739 while (l + sizeof (Elf_External_Options) <= lend)
2740 {
2741 Elf_Internal_Options intopt;
2742
2743 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2744 &intopt);
2745 if (intopt.kind == ODK_REGINFO)
2746 {
2747 bfd_byte buf[4];
2748
2749 if (bfd_seek (abfd,
2750 (hdr->sh_offset
2751 + (l - contents)
2752 + sizeof (Elf_External_Options)
2753 + (sizeof (Elf32_External_RegInfo) - 4)),
2754 SEEK_SET) == -1)
2755 return false;
2756 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2757 if (bfd_write (buf, 1, 4, abfd) != 4)
2758 return false;
2759 }
2760 l += intopt.size;
2761 }
2762 }
2763
2764 return _bfd_mips_elf_section_processing (abfd, hdr);
2765 }
2766 \f
2767 /* MIPS ELF uses two common sections. One is the usual one, and the
2768 other is for small objects. All the small objects are kept
2769 together, and then referenced via the gp pointer, which yields
2770 faster assembler code. This is what we use for the small common
2771 section. This approach is copied from ecoff.c. */
2772 static asection mips_elf_scom_section;
2773 static asymbol mips_elf_scom_symbol;
2774 static asymbol *mips_elf_scom_symbol_ptr;
2775
2776 /* MIPS ELF also uses an acommon section, which represents an
2777 allocated common symbol which may be overridden by a
2778 definition in a shared library. */
2779 static asection mips_elf_acom_section;
2780 static asymbol mips_elf_acom_symbol;
2781 static asymbol *mips_elf_acom_symbol_ptr;
2782
2783 /* The Irix 5 support uses two virtual sections, which represent
2784 text/data symbols defined in dynamic objects. */
2785 static asection mips_elf_text_section;
2786 static asection *mips_elf_text_section_ptr;
2787 static asymbol mips_elf_text_symbol;
2788 static asymbol *mips_elf_text_symbol_ptr;
2789
2790 static asection mips_elf_data_section;
2791 static asection *mips_elf_data_section_ptr;
2792 static asymbol mips_elf_data_symbol;
2793 static asymbol *mips_elf_data_symbol_ptr;
2794
2795 /* Handle the special MIPS section numbers that a symbol may use.
2796 This is used for both the 32-bit and the 64-bit ABI. */
2797
2798 void
2799 _bfd_mips_elf_symbol_processing (abfd, asym)
2800 bfd *abfd;
2801 asymbol *asym;
2802 {
2803 elf_symbol_type *elfsym;
2804
2805 elfsym = (elf_symbol_type *) asym;
2806 switch (elfsym->internal_elf_sym.st_shndx)
2807 {
2808 case SHN_MIPS_ACOMMON:
2809 /* This section is used in a dynamically linked executable file.
2810 It is an allocated common section. The dynamic linker can
2811 either resolve these symbols to something in a shared
2812 library, or it can just leave them here. For our purposes,
2813 we can consider these symbols to be in a new section. */
2814 if (mips_elf_acom_section.name == NULL)
2815 {
2816 /* Initialize the acommon section. */
2817 mips_elf_acom_section.name = ".acommon";
2818 mips_elf_acom_section.flags = SEC_ALLOC;
2819 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2820 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2821 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2822 mips_elf_acom_symbol.name = ".acommon";
2823 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2824 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2825 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2826 }
2827 asym->section = &mips_elf_acom_section;
2828 break;
2829
2830 case SHN_COMMON:
2831 /* Common symbols less than the GP size are automatically
2832 treated as SHN_MIPS_SCOMMON symbols. */
2833 if (asym->value > elf_gp_size (abfd))
2834 break;
2835 /* Fall through. */
2836 case SHN_MIPS_SCOMMON:
2837 if (mips_elf_scom_section.name == NULL)
2838 {
2839 /* Initialize the small common section. */
2840 mips_elf_scom_section.name = ".scommon";
2841 mips_elf_scom_section.flags = SEC_IS_COMMON;
2842 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2843 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2844 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2845 mips_elf_scom_symbol.name = ".scommon";
2846 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2847 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2848 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2849 }
2850 asym->section = &mips_elf_scom_section;
2851 asym->value = elfsym->internal_elf_sym.st_size;
2852 break;
2853
2854 case SHN_MIPS_SUNDEFINED:
2855 asym->section = bfd_und_section_ptr;
2856 break;
2857
2858 #if 0 /* for SGI_COMPAT */
2859 case SHN_MIPS_TEXT:
2860 asym->section = mips_elf_text_section_ptr;
2861 break;
2862
2863 case SHN_MIPS_DATA:
2864 asym->section = mips_elf_data_section_ptr;
2865 break;
2866 #endif
2867 }
2868 }
2869 \f
2870 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
2871 segments. */
2872
2873 static int
2874 mips_elf_additional_program_headers (abfd)
2875 bfd *abfd;
2876 {
2877 asection *s;
2878 int ret;
2879
2880 ret = 0;
2881
2882 if (! SGI_COMPAT (abfd))
2883 return ret;
2884
2885 s = bfd_get_section_by_name (abfd, ".reginfo");
2886 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2887 {
2888 /* We need a PT_MIPS_REGINFO segment. */
2889 ++ret;
2890 }
2891
2892 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2893 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2894 {
2895 /* We need a PT_MIPS_RTPROC segment. */
2896 ++ret;
2897 }
2898
2899 return ret;
2900 }
2901
2902 /* Modify the segment map for an Irix 5 executable. */
2903
2904 static boolean
2905 mips_elf_modify_segment_map (abfd)
2906 bfd *abfd;
2907 {
2908 asection *s;
2909 struct elf_segment_map *m, **pm;
2910
2911 if (! SGI_COMPAT (abfd))
2912 return true;
2913
2914 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2915 segment. */
2916 s = bfd_get_section_by_name (abfd, ".reginfo");
2917 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2918 {
2919 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2920 if (m->p_type == PT_MIPS_REGINFO)
2921 break;
2922 if (m == NULL)
2923 {
2924 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2925 if (m == NULL)
2926 return false;
2927
2928 m->p_type = PT_MIPS_REGINFO;
2929 m->count = 1;
2930 m->sections[0] = s;
2931
2932 /* We want to put it after the PHDR and INTERP segments. */
2933 pm = &elf_tdata (abfd)->segment_map;
2934 while (*pm != NULL
2935 && ((*pm)->p_type == PT_PHDR
2936 || (*pm)->p_type == PT_INTERP))
2937 pm = &(*pm)->next;
2938
2939 m->next = *pm;
2940 *pm = m;
2941 }
2942 }
2943
2944 /* If there are .dynamic and .mdebug sections, we make a room for
2945 the RTPROC header. FIXME: Rewrite without section names. */
2946 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2947 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2948 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2949 {
2950 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2951 if (m->p_type == PT_MIPS_RTPROC)
2952 break;
2953 if (m == NULL)
2954 {
2955 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2956 if (m == NULL)
2957 return false;
2958
2959 m->p_type = PT_MIPS_RTPROC;
2960
2961 s = bfd_get_section_by_name (abfd, ".rtproc");
2962 if (s == NULL)
2963 {
2964 m->count = 0;
2965 m->p_flags = 0;
2966 m->p_flags_valid = 1;
2967 }
2968 else
2969 {
2970 m->count = 1;
2971 m->sections[0] = s;
2972 }
2973
2974 /* We want to put it after the DYNAMIC segment. */
2975 pm = &elf_tdata (abfd)->segment_map;
2976 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
2977 pm = &(*pm)->next;
2978 if (*pm != NULL)
2979 pm = &(*pm)->next;
2980
2981 m->next = *pm;
2982 *pm = m;
2983 }
2984 }
2985
2986 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2987 .dynsym, and .hash sections, and everything in between. */
2988 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
2989 if ((*pm)->p_type == PT_DYNAMIC)
2990 break;
2991 m = *pm;
2992 if (m != NULL
2993 && m->count == 1
2994 && strcmp (m->sections[0]->name, ".dynamic") == 0)
2995 {
2996 static const char *sec_names[] =
2997 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2998 bfd_vma low, high;
2999 unsigned int i, c;
3000 struct elf_segment_map *n;
3001
3002 low = 0xffffffff;
3003 high = 0;
3004 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3005 {
3006 s = bfd_get_section_by_name (abfd, sec_names[i]);
3007 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3008 {
3009 bfd_size_type sz;
3010
3011 if (low > s->vma)
3012 low = s->vma;
3013 sz = s->_cooked_size;
3014 if (sz == 0)
3015 sz = s->_raw_size;
3016 if (high < s->vma + sz)
3017 high = s->vma + sz;
3018 }
3019 }
3020
3021 c = 0;
3022 for (s = abfd->sections; s != NULL; s = s->next)
3023 if ((s->flags & SEC_LOAD) != 0
3024 && s->vma >= low
3025 && ((s->vma
3026 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3027 <= high))
3028 ++c;
3029
3030 n = ((struct elf_segment_map *)
3031 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3032 if (n == NULL)
3033 return false;
3034 *n = *m;
3035 n->count = c;
3036
3037 i = 0;
3038 for (s = abfd->sections; s != NULL; s = s->next)
3039 {
3040 if ((s->flags & SEC_LOAD) != 0
3041 && s->vma >= low
3042 && ((s->vma
3043 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3044 <= high))
3045 {
3046 n->sections[i] = s;
3047 ++i;
3048 }
3049 }
3050
3051 *pm = n;
3052 }
3053
3054 return true;
3055 }
3056 \f
3057 /* The structure of the runtime procedure descriptor created by the
3058 loader for use by the static exception system. */
3059
3060 typedef struct runtime_pdr {
3061 bfd_vma adr; /* memory address of start of procedure */
3062 long regmask; /* save register mask */
3063 long regoffset; /* save register offset */
3064 long fregmask; /* save floating point register mask */
3065 long fregoffset; /* save floating point register offset */
3066 long frameoffset; /* frame size */
3067 short framereg; /* frame pointer register */
3068 short pcreg; /* offset or reg of return pc */
3069 long irpss; /* index into the runtime string table */
3070 long reserved;
3071 struct exception_info *exception_info;/* pointer to exception array */
3072 } RPDR, *pRPDR;
3073 #define cbRPDR sizeof(RPDR)
3074 #define rpdNil ((pRPDR) 0)
3075
3076 /* Swap RPDR (runtime procedure table entry) for output. */
3077
3078 static void ecoff_swap_rpdr_out
3079 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3080
3081 static void
3082 ecoff_swap_rpdr_out (abfd, in, ex)
3083 bfd *abfd;
3084 const RPDR *in;
3085 struct rpdr_ext *ex;
3086 {
3087 /* ecoff_put_off was defined in ecoffswap.h. */
3088 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3089 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3090 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3091 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3092 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3093 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3094
3095 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3096 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3097
3098 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3099 #if 0 /* FIXME */
3100 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3101 #endif
3102 }
3103 \f
3104 /* Read ECOFF debugging information from a .mdebug section into a
3105 ecoff_debug_info structure. */
3106
3107 boolean
3108 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3109 bfd *abfd;
3110 asection *section;
3111 struct ecoff_debug_info *debug;
3112 {
3113 HDRR *symhdr;
3114 const struct ecoff_debug_swap *swap;
3115 char *ext_hdr = NULL;
3116
3117 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3118
3119 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3120 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3121 goto error_return;
3122
3123 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3124 swap->external_hdr_size)
3125 == false)
3126 goto error_return;
3127
3128 symhdr = &debug->symbolic_header;
3129 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3130
3131 /* The symbolic header contains absolute file offsets and sizes to
3132 read. */
3133 #define READ(ptr, offset, count, size, type) \
3134 if (symhdr->count == 0) \
3135 debug->ptr = NULL; \
3136 else \
3137 { \
3138 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3139 if (debug->ptr == NULL) \
3140 goto error_return; \
3141 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3142 || (bfd_read (debug->ptr, size, symhdr->count, \
3143 abfd) != size * symhdr->count)) \
3144 goto error_return; \
3145 }
3146
3147 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3148 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3149 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3150 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3151 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3152 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3153 union aux_ext *);
3154 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3155 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3156 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3157 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3158 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3159 #undef READ
3160
3161 debug->fdr = NULL;
3162 debug->adjust = NULL;
3163
3164 return true;
3165
3166 error_return:
3167 if (ext_hdr != NULL)
3168 free (ext_hdr);
3169 if (debug->line != NULL)
3170 free (debug->line);
3171 if (debug->external_dnr != NULL)
3172 free (debug->external_dnr);
3173 if (debug->external_pdr != NULL)
3174 free (debug->external_pdr);
3175 if (debug->external_sym != NULL)
3176 free (debug->external_sym);
3177 if (debug->external_opt != NULL)
3178 free (debug->external_opt);
3179 if (debug->external_aux != NULL)
3180 free (debug->external_aux);
3181 if (debug->ss != NULL)
3182 free (debug->ss);
3183 if (debug->ssext != NULL)
3184 free (debug->ssext);
3185 if (debug->external_fdr != NULL)
3186 free (debug->external_fdr);
3187 if (debug->external_rfd != NULL)
3188 free (debug->external_rfd);
3189 if (debug->external_ext != NULL)
3190 free (debug->external_ext);
3191 return false;
3192 }
3193 \f
3194 /* MIPS ELF local labels start with '$', not 'L'. */
3195
3196 /*ARGSUSED*/
3197 static boolean
3198 mips_elf_is_local_label_name (abfd, name)
3199 bfd *abfd;
3200 const char *name;
3201 {
3202 if (name[0] == '$')
3203 return true;
3204
3205 /* On Irix 6, the labels go back to starting with '.', so we accept
3206 the generic ELF local label syntax as well. */
3207 return _bfd_elf_is_local_label_name (abfd, name);
3208 }
3209
3210 /* MIPS ELF uses a special find_nearest_line routine in order the
3211 handle the ECOFF debugging information. */
3212
3213 struct mips_elf_find_line
3214 {
3215 struct ecoff_debug_info d;
3216 struct ecoff_find_line i;
3217 };
3218
3219 boolean
3220 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3221 functionname_ptr, line_ptr)
3222 bfd *abfd;
3223 asection *section;
3224 asymbol **symbols;
3225 bfd_vma offset;
3226 const char **filename_ptr;
3227 const char **functionname_ptr;
3228 unsigned int *line_ptr;
3229 {
3230 asection *msec;
3231
3232 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3233 filename_ptr, functionname_ptr,
3234 line_ptr))
3235 return true;
3236
3237 msec = bfd_get_section_by_name (abfd, ".mdebug");
3238 if (msec != NULL)
3239 {
3240 flagword origflags;
3241 struct mips_elf_find_line *fi;
3242 const struct ecoff_debug_swap * const swap =
3243 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3244
3245 /* If we are called during a link, mips_elf_final_link may have
3246 cleared the SEC_HAS_CONTENTS field. We force it back on here
3247 if appropriate (which it normally will be). */
3248 origflags = msec->flags;
3249 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3250 msec->flags |= SEC_HAS_CONTENTS;
3251
3252 fi = elf_tdata (abfd)->find_line_info;
3253 if (fi == NULL)
3254 {
3255 bfd_size_type external_fdr_size;
3256 char *fraw_src;
3257 char *fraw_end;
3258 struct fdr *fdr_ptr;
3259
3260 fi = ((struct mips_elf_find_line *)
3261 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3262 if (fi == NULL)
3263 {
3264 msec->flags = origflags;
3265 return false;
3266 }
3267
3268 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3269 {
3270 msec->flags = origflags;
3271 return false;
3272 }
3273
3274 /* Swap in the FDR information. */
3275 fi->d.fdr = ((struct fdr *)
3276 bfd_alloc (abfd,
3277 (fi->d.symbolic_header.ifdMax *
3278 sizeof (struct fdr))));
3279 if (fi->d.fdr == NULL)
3280 {
3281 msec->flags = origflags;
3282 return false;
3283 }
3284 external_fdr_size = swap->external_fdr_size;
3285 fdr_ptr = fi->d.fdr;
3286 fraw_src = (char *) fi->d.external_fdr;
3287 fraw_end = (fraw_src
3288 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3289 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3290 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3291
3292 elf_tdata (abfd)->find_line_info = fi;
3293
3294 /* Note that we don't bother to ever free this information.
3295 find_nearest_line is either called all the time, as in
3296 objdump -l, so the information should be saved, or it is
3297 rarely called, as in ld error messages, so the memory
3298 wasted is unimportant. Still, it would probably be a
3299 good idea for free_cached_info to throw it away. */
3300 }
3301
3302 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3303 &fi->i, filename_ptr, functionname_ptr,
3304 line_ptr))
3305 {
3306 msec->flags = origflags;
3307 return true;
3308 }
3309
3310 msec->flags = origflags;
3311 }
3312
3313 /* Fall back on the generic ELF find_nearest_line routine. */
3314
3315 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3316 filename_ptr, functionname_ptr,
3317 line_ptr);
3318 }
3319 \f
3320 /* The mips16 compiler uses a couple of special sections to handle
3321 floating point arguments.
3322
3323 Section names that look like .mips16.fn.FNNAME contain stubs that
3324 copy floating point arguments from the fp regs to the gp regs and
3325 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3326 call should be redirected to the stub instead. If no 32 bit
3327 function calls FNNAME, the stub should be discarded. We need to
3328 consider any reference to the function, not just a call, because
3329 if the address of the function is taken we will need the stub,
3330 since the address might be passed to a 32 bit function.
3331
3332 Section names that look like .mips16.call.FNNAME contain stubs
3333 that copy floating point arguments from the gp regs to the fp
3334 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3335 then any 16 bit function that calls FNNAME should be redirected
3336 to the stub instead. If FNNAME is not a 32 bit function, the
3337 stub should be discarded.
3338
3339 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3340 which call FNNAME and then copy the return value from the fp regs
3341 to the gp regs. These stubs store the return value in $18 while
3342 calling FNNAME; any function which might call one of these stubs
3343 must arrange to save $18 around the call. (This case is not
3344 needed for 32 bit functions that call 16 bit functions, because
3345 16 bit functions always return floating point values in both
3346 $f0/$f1 and $2/$3.)
3347
3348 Note that in all cases FNNAME might be defined statically.
3349 Therefore, FNNAME is not used literally. Instead, the relocation
3350 information will indicate which symbol the section is for.
3351
3352 We record any stubs that we find in the symbol table. */
3353
3354 #define FN_STUB ".mips16.fn."
3355 #define CALL_STUB ".mips16.call."
3356 #define CALL_FP_STUB ".mips16.call.fp."
3357
3358 /* The MIPS ELF linker needs additional information for each symbol in
3359 the global hash table. */
3360
3361 struct mips_elf_link_hash_entry
3362 {
3363 struct elf_link_hash_entry root;
3364
3365 /* External symbol information. */
3366 EXTR esym;
3367
3368 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
3369 unsigned int mips_32_relocs;
3370
3371 /* If there is a stub that 32 bit functions should use to call this
3372 16 bit function, this points to the section containing the stub. */
3373 asection *fn_stub;
3374
3375 /* Whether we need the fn_stub; this is set if this symbol appears
3376 in any relocs other than a 16 bit call. */
3377 boolean need_fn_stub;
3378
3379 /* If there is a stub that 16 bit functions should use to call this
3380 32 bit function, this points to the section containing the stub. */
3381 asection *call_stub;
3382
3383 /* This is like the call_stub field, but it is used if the function
3384 being called returns a floating point value. */
3385 asection *call_fp_stub;
3386 };
3387
3388 /* MIPS ELF linker hash table. */
3389
3390 struct mips_elf_link_hash_table
3391 {
3392 struct elf_link_hash_table root;
3393 /* String section indices for the dynamic section symbols. */
3394 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3395 /* The number of .rtproc entries. */
3396 bfd_size_type procedure_count;
3397 /* The size of the .compact_rel section (if SGI_COMPAT). */
3398 bfd_size_type compact_rel_size;
3399 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3400 entry is set to the address of __rld_obj_head as in Irix 5. */
3401 boolean use_rld_obj_head;
3402 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3403 bfd_vma rld_value;
3404 /* This is set if we see any mips16 stub sections. */
3405 boolean mips16_stubs_seen;
3406 };
3407
3408 /* Look up an entry in a MIPS ELF linker hash table. */
3409
3410 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3411 ((struct mips_elf_link_hash_entry *) \
3412 elf_link_hash_lookup (&(table)->root, (string), (create), \
3413 (copy), (follow)))
3414
3415 /* Traverse a MIPS ELF linker hash table. */
3416
3417 #define mips_elf_link_hash_traverse(table, func, info) \
3418 (elf_link_hash_traverse \
3419 (&(table)->root, \
3420 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3421 (info)))
3422
3423 /* Get the MIPS ELF linker hash table from a link_info structure. */
3424
3425 #define mips_elf_hash_table(p) \
3426 ((struct mips_elf_link_hash_table *) ((p)->hash))
3427
3428 static boolean mips_elf_output_extsym
3429 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3430
3431 /* Create an entry in a MIPS ELF linker hash table. */
3432
3433 static struct bfd_hash_entry *
3434 mips_elf_link_hash_newfunc (entry, table, string)
3435 struct bfd_hash_entry *entry;
3436 struct bfd_hash_table *table;
3437 const char *string;
3438 {
3439 struct mips_elf_link_hash_entry *ret =
3440 (struct mips_elf_link_hash_entry *) entry;
3441
3442 /* Allocate the structure if it has not already been allocated by a
3443 subclass. */
3444 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3445 ret = ((struct mips_elf_link_hash_entry *)
3446 bfd_hash_allocate (table,
3447 sizeof (struct mips_elf_link_hash_entry)));
3448 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3449 return (struct bfd_hash_entry *) ret;
3450
3451 /* Call the allocation method of the superclass. */
3452 ret = ((struct mips_elf_link_hash_entry *)
3453 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3454 table, string));
3455 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3456 {
3457 /* Set local fields. */
3458 memset (&ret->esym, 0, sizeof (EXTR));
3459 /* We use -2 as a marker to indicate that the information has
3460 not been set. -1 means there is no associated ifd. */
3461 ret->esym.ifd = -2;
3462 ret->mips_32_relocs = 0;
3463 ret->fn_stub = NULL;
3464 ret->need_fn_stub = false;
3465 ret->call_stub = NULL;
3466 ret->call_fp_stub = NULL;
3467 }
3468
3469 return (struct bfd_hash_entry *) ret;
3470 }
3471
3472 /* Create a MIPS ELF linker hash table. */
3473
3474 static struct bfd_link_hash_table *
3475 mips_elf_link_hash_table_create (abfd)
3476 bfd *abfd;
3477 {
3478 struct mips_elf_link_hash_table *ret;
3479 unsigned int i;
3480
3481 ret = ((struct mips_elf_link_hash_table *)
3482 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3483 if (ret == (struct mips_elf_link_hash_table *) NULL)
3484 return NULL;
3485
3486 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3487 mips_elf_link_hash_newfunc))
3488 {
3489 bfd_release (abfd, ret);
3490 return NULL;
3491 }
3492
3493 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3494 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3495 ret->procedure_count = 0;
3496 ret->compact_rel_size = 0;
3497 ret->use_rld_obj_head = false;
3498 ret->rld_value = 0;
3499 ret->mips16_stubs_seen = false;
3500
3501 return &ret->root.root;
3502 }
3503
3504 /* Hook called by the linker routine which adds symbols from an object
3505 file. We must handle the special MIPS section numbers here. */
3506
3507 /*ARGSUSED*/
3508 static boolean
3509 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3510 bfd *abfd;
3511 struct bfd_link_info *info;
3512 const Elf_Internal_Sym *sym;
3513 const char **namep;
3514 flagword *flagsp;
3515 asection **secp;
3516 bfd_vma *valp;
3517 {
3518 if (SGI_COMPAT (abfd)
3519 && (abfd->flags & DYNAMIC) != 0
3520 && strcmp (*namep, "_rld_new_interface") == 0)
3521 {
3522 /* Skip Irix 5 rld entry name. */
3523 *namep = NULL;
3524 return true;
3525 }
3526
3527 switch (sym->st_shndx)
3528 {
3529 case SHN_COMMON:
3530 /* Common symbols less than the GP size are automatically
3531 treated as SHN_MIPS_SCOMMON symbols. */
3532 if (sym->st_size > elf_gp_size (abfd))
3533 break;
3534 /* Fall through. */
3535 case SHN_MIPS_SCOMMON:
3536 *secp = bfd_make_section_old_way (abfd, ".scommon");
3537 (*secp)->flags |= SEC_IS_COMMON;
3538 *valp = sym->st_size;
3539 break;
3540
3541 case SHN_MIPS_TEXT:
3542 /* This section is used in a shared object. */
3543 if (mips_elf_text_section_ptr == NULL)
3544 {
3545 /* Initialize the section. */
3546 mips_elf_text_section.name = ".text";
3547 mips_elf_text_section.flags = SEC_NO_FLAGS;
3548 mips_elf_text_section.output_section = NULL;
3549 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3550 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3551 mips_elf_text_symbol.name = ".text";
3552 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3553 mips_elf_text_symbol.section = &mips_elf_text_section;
3554 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3555 mips_elf_text_section_ptr = &mips_elf_text_section;
3556 }
3557 /* This code used to do *secp = bfd_und_section_ptr if
3558 info->shared. I don't know why, and that doesn't make sense,
3559 so I took it out. */
3560 *secp = mips_elf_text_section_ptr;
3561 break;
3562
3563 case SHN_MIPS_ACOMMON:
3564 /* Fall through. XXX Can we treat this as allocated data? */
3565 case SHN_MIPS_DATA:
3566 /* This section is used in a shared object. */
3567 if (mips_elf_data_section_ptr == NULL)
3568 {
3569 /* Initialize the section. */
3570 mips_elf_data_section.name = ".data";
3571 mips_elf_data_section.flags = SEC_NO_FLAGS;
3572 mips_elf_data_section.output_section = NULL;
3573 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3574 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3575 mips_elf_data_symbol.name = ".data";
3576 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3577 mips_elf_data_symbol.section = &mips_elf_data_section;
3578 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3579 mips_elf_data_section_ptr = &mips_elf_data_section;
3580 }
3581 /* This code used to do *secp = bfd_und_section_ptr if
3582 info->shared. I don't know why, and that doesn't make sense,
3583 so I took it out. */
3584 *secp = mips_elf_data_section_ptr;
3585 break;
3586
3587 case SHN_MIPS_SUNDEFINED:
3588 *secp = bfd_und_section_ptr;
3589 break;
3590 }
3591
3592 if (SGI_COMPAT (abfd)
3593 && ! info->shared
3594 && info->hash->creator == abfd->xvec
3595 && strcmp (*namep, "__rld_obj_head") == 0)
3596 {
3597 struct elf_link_hash_entry *h;
3598
3599 /* Mark __rld_obj_head as dynamic. */
3600 h = NULL;
3601 if (! (_bfd_generic_link_add_one_symbol
3602 (info, abfd, *namep, BSF_GLOBAL, *secp,
3603 (bfd_vma) *valp, (const char *) NULL, false,
3604 get_elf_backend_data (abfd)->collect,
3605 (struct bfd_link_hash_entry **) &h)))
3606 return false;
3607 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3608 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3609 h->type = STT_OBJECT;
3610
3611 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3612 return false;
3613
3614 mips_elf_hash_table (info)->use_rld_obj_head = true;
3615 }
3616
3617 /* If this is a mips16 text symbol, add 1 to the value to make it
3618 odd. This will cause something like .word SYM to come up with
3619 the right value when it is loaded into the PC. */
3620 if (sym->st_other == STO_MIPS16)
3621 ++*valp;
3622
3623 return true;
3624 }
3625
3626 /* Structure used to pass information to mips_elf_output_extsym. */
3627
3628 struct extsym_info
3629 {
3630 bfd *abfd;
3631 struct bfd_link_info *info;
3632 struct ecoff_debug_info *debug;
3633 const struct ecoff_debug_swap *swap;
3634 boolean failed;
3635 };
3636
3637 /* This routine is used to write out ECOFF debugging external symbol
3638 information. It is called via mips_elf_link_hash_traverse. The
3639 ECOFF external symbol information must match the ELF external
3640 symbol information. Unfortunately, at this point we don't know
3641 whether a symbol is required by reloc information, so the two
3642 tables may wind up being different. We must sort out the external
3643 symbol information before we can set the final size of the .mdebug
3644 section, and we must set the size of the .mdebug section before we
3645 can relocate any sections, and we can't know which symbols are
3646 required by relocation until we relocate the sections.
3647 Fortunately, it is relatively unlikely that any symbol will be
3648 stripped but required by a reloc. In particular, it can not happen
3649 when generating a final executable. */
3650
3651 static boolean
3652 mips_elf_output_extsym (h, data)
3653 struct mips_elf_link_hash_entry *h;
3654 PTR data;
3655 {
3656 struct extsym_info *einfo = (struct extsym_info *) data;
3657 boolean strip;
3658 asection *sec, *output_section;
3659
3660 if (h->root.indx == -2)
3661 strip = false;
3662 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3663 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3664 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3665 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3666 strip = true;
3667 else if (einfo->info->strip == strip_all
3668 || (einfo->info->strip == strip_some
3669 && bfd_hash_lookup (einfo->info->keep_hash,
3670 h->root.root.root.string,
3671 false, false) == NULL))
3672 strip = true;
3673 else
3674 strip = false;
3675
3676 if (strip)
3677 return true;
3678
3679 if (h->esym.ifd == -2)
3680 {
3681 h->esym.jmptbl = 0;
3682 h->esym.cobol_main = 0;
3683 h->esym.weakext = 0;
3684 h->esym.reserved = 0;
3685 h->esym.ifd = ifdNil;
3686 h->esym.asym.value = 0;
3687 h->esym.asym.st = stGlobal;
3688
3689 if (SGI_COMPAT (einfo->abfd)
3690 && (h->root.root.type == bfd_link_hash_undefined
3691 || h->root.root.type == bfd_link_hash_undefweak))
3692 {
3693 const char *name;
3694
3695 /* Use undefined class. Also, set class and type for some
3696 special symbols. */
3697 name = h->root.root.root.string;
3698 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3699 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3700 {
3701 h->esym.asym.sc = scData;
3702 h->esym.asym.st = stLabel;
3703 h->esym.asym.value = 0;
3704 }
3705 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3706 {
3707 h->esym.asym.sc = scAbs;
3708 h->esym.asym.st = stLabel;
3709 h->esym.asym.value =
3710 mips_elf_hash_table (einfo->info)->procedure_count;
3711 }
3712 else if (strcmp (name, "_gp_disp") == 0)
3713 {
3714 h->esym.asym.sc = scAbs;
3715 h->esym.asym.st = stLabel;
3716 h->esym.asym.value = elf_gp (einfo->abfd);
3717 }
3718 else
3719 h->esym.asym.sc = scUndefined;
3720 }
3721 else if (h->root.root.type != bfd_link_hash_defined
3722 && h->root.root.type != bfd_link_hash_defweak)
3723 h->esym.asym.sc = scAbs;
3724 else
3725 {
3726 const char *name;
3727
3728 sec = h->root.root.u.def.section;
3729 output_section = sec->output_section;
3730
3731 /* When making a shared library and symbol h is the one from
3732 the another shared library, OUTPUT_SECTION may be null. */
3733 if (output_section == NULL)
3734 h->esym.asym.sc = scUndefined;
3735 else
3736 {
3737 name = bfd_section_name (output_section->owner, output_section);
3738
3739 if (strcmp (name, ".text") == 0)
3740 h->esym.asym.sc = scText;
3741 else if (strcmp (name, ".data") == 0)
3742 h->esym.asym.sc = scData;
3743 else if (strcmp (name, ".sdata") == 0)
3744 h->esym.asym.sc = scSData;
3745 else if (strcmp (name, ".rodata") == 0
3746 || strcmp (name, ".rdata") == 0)
3747 h->esym.asym.sc = scRData;
3748 else if (strcmp (name, ".bss") == 0)
3749 h->esym.asym.sc = scBss;
3750 else if (strcmp (name, ".sbss") == 0)
3751 h->esym.asym.sc = scSBss;
3752 else if (strcmp (name, ".init") == 0)
3753 h->esym.asym.sc = scInit;
3754 else if (strcmp (name, ".fini") == 0)
3755 h->esym.asym.sc = scFini;
3756 else
3757 h->esym.asym.sc = scAbs;
3758 }
3759 }
3760
3761 h->esym.asym.reserved = 0;
3762 h->esym.asym.index = indexNil;
3763 }
3764
3765 if (h->root.root.type == bfd_link_hash_common)
3766 h->esym.asym.value = h->root.root.u.c.size;
3767 else if (h->root.root.type == bfd_link_hash_defined
3768 || h->root.root.type == bfd_link_hash_defweak)
3769 {
3770 if (h->esym.asym.sc == scCommon)
3771 h->esym.asym.sc = scBss;
3772 else if (h->esym.asym.sc == scSCommon)
3773 h->esym.asym.sc = scSBss;
3774
3775 sec = h->root.root.u.def.section;
3776 output_section = sec->output_section;
3777 if (output_section != NULL)
3778 h->esym.asym.value = (h->root.root.u.def.value
3779 + sec->output_offset
3780 + output_section->vma);
3781 else
3782 h->esym.asym.value = 0;
3783 }
3784 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3785 {
3786 /* Set type and value for a symbol with a function stub. */
3787 h->esym.asym.st = stProc;
3788 sec = h->root.root.u.def.section;
3789 if (sec == NULL)
3790 h->esym.asym.value = 0;
3791 else
3792 {
3793 output_section = sec->output_section;
3794 if (output_section != NULL)
3795 h->esym.asym.value = (h->root.plt_offset
3796 + sec->output_offset
3797 + output_section->vma);
3798 else
3799 h->esym.asym.value = 0;
3800 }
3801 #if 0 /* FIXME? */
3802 h->esym.ifd = 0;
3803 #endif
3804 }
3805
3806 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3807 h->root.root.root.string,
3808 &h->esym))
3809 {
3810 einfo->failed = true;
3811 return false;
3812 }
3813
3814 return true;
3815 }
3816
3817 /* Create a runtime procedure table from the .mdebug section. */
3818
3819 static boolean
3820 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3821 PTR handle;
3822 bfd *abfd;
3823 struct bfd_link_info *info;
3824 asection *s;
3825 struct ecoff_debug_info *debug;
3826 {
3827 const struct ecoff_debug_swap *swap;
3828 HDRR *hdr = &debug->symbolic_header;
3829 RPDR *rpdr, *rp;
3830 struct rpdr_ext *erp;
3831 PTR rtproc;
3832 struct pdr_ext *epdr;
3833 struct sym_ext *esym;
3834 char *ss, **sv;
3835 char *str;
3836 unsigned long size, count;
3837 unsigned long sindex;
3838 unsigned long i;
3839 PDR pdr;
3840 SYMR sym;
3841 const char *no_name_func = _("static procedure (no name)");
3842
3843 epdr = NULL;
3844 rpdr = NULL;
3845 esym = NULL;
3846 ss = NULL;
3847 sv = NULL;
3848
3849 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3850
3851 sindex = strlen (no_name_func) + 1;
3852 count = hdr->ipdMax;
3853 if (count > 0)
3854 {
3855 size = swap->external_pdr_size;
3856
3857 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3858 if (epdr == NULL)
3859 goto error_return;
3860
3861 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3862 goto error_return;
3863
3864 size = sizeof (RPDR);
3865 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3866 if (rpdr == NULL)
3867 goto error_return;
3868
3869 sv = (char **) bfd_malloc (sizeof (char *) * count);
3870 if (sv == NULL)
3871 goto error_return;
3872
3873 count = hdr->isymMax;
3874 size = swap->external_sym_size;
3875 esym = (struct sym_ext *) bfd_malloc (size * count);
3876 if (esym == NULL)
3877 goto error_return;
3878
3879 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3880 goto error_return;
3881
3882 count = hdr->issMax;
3883 ss = (char *) bfd_malloc (count);
3884 if (ss == NULL)
3885 goto error_return;
3886 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3887 goto error_return;
3888
3889 count = hdr->ipdMax;
3890 for (i = 0; i < count; i++, rp++)
3891 {
3892 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3893 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3894 rp->adr = sym.value;
3895 rp->regmask = pdr.regmask;
3896 rp->regoffset = pdr.regoffset;
3897 rp->fregmask = pdr.fregmask;
3898 rp->fregoffset = pdr.fregoffset;
3899 rp->frameoffset = pdr.frameoffset;
3900 rp->framereg = pdr.framereg;
3901 rp->pcreg = pdr.pcreg;
3902 rp->irpss = sindex;
3903 sv[i] = ss + sym.iss;
3904 sindex += strlen (sv[i]) + 1;
3905 }
3906 }
3907
3908 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3909 size = BFD_ALIGN (size, 16);
3910 rtproc = (PTR) bfd_alloc (abfd, size);
3911 if (rtproc == NULL)
3912 {
3913 mips_elf_hash_table (info)->procedure_count = 0;
3914 goto error_return;
3915 }
3916
3917 mips_elf_hash_table (info)->procedure_count = count + 2;
3918
3919 erp = (struct rpdr_ext *) rtproc;
3920 memset (erp, 0, sizeof (struct rpdr_ext));
3921 erp++;
3922 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3923 strcpy (str, no_name_func);
3924 str += strlen (no_name_func) + 1;
3925 for (i = 0; i < count; i++)
3926 {
3927 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3928 strcpy (str, sv[i]);
3929 str += strlen (sv[i]) + 1;
3930 }
3931 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3932
3933 /* Set the size and contents of .rtproc section. */
3934 s->_raw_size = size;
3935 s->contents = (bfd_byte *) rtproc;
3936
3937 /* Skip this section later on (I don't think this currently
3938 matters, but someday it might). */
3939 s->link_order_head = (struct bfd_link_order *) NULL;
3940
3941 if (epdr != NULL)
3942 free (epdr);
3943 if (rpdr != NULL)
3944 free (rpdr);
3945 if (esym != NULL)
3946 free (esym);
3947 if (ss != NULL)
3948 free (ss);
3949 if (sv != NULL)
3950 free (sv);
3951
3952 return true;
3953
3954 error_return:
3955 if (epdr != NULL)
3956 free (epdr);
3957 if (rpdr != NULL)
3958 free (rpdr);
3959 if (esym != NULL)
3960 free (esym);
3961 if (ss != NULL)
3962 free (ss);
3963 if (sv != NULL)
3964 free (sv);
3965 return false;
3966 }
3967
3968 /* A comparison routine used to sort .gptab entries. */
3969
3970 static int
3971 gptab_compare (p1, p2)
3972 const PTR p1;
3973 const PTR p2;
3974 {
3975 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
3976 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
3977
3978 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3979 }
3980
3981 /* We need to use a special link routine to handle the .reginfo and
3982 the .mdebug sections. We need to merge all instances of these
3983 sections together, not write them all out sequentially. */
3984
3985 static boolean
3986 mips_elf_final_link (abfd, info)
3987 bfd *abfd;
3988 struct bfd_link_info *info;
3989 {
3990 asection **secpp;
3991 asection *o;
3992 struct bfd_link_order *p;
3993 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
3994 asection *rtproc_sec;
3995 Elf32_RegInfo reginfo;
3996 struct ecoff_debug_info debug;
3997 const struct ecoff_debug_swap *swap
3998 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3999 HDRR *symhdr = &debug.symbolic_header;
4000 PTR mdebug_handle = NULL;
4001
4002 /* Drop the .options section, since it has special semantics which I
4003 haven't bothered to figure out. */
4004 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4005 {
4006 if (strcmp ((*secpp)->name, ".options") == 0)
4007 {
4008 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4009 if (p->type == bfd_indirect_link_order)
4010 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4011 (*secpp)->link_order_head = NULL;
4012 *secpp = (*secpp)->next;
4013 --abfd->section_count;
4014 break;
4015 }
4016 }
4017
4018 /* Get a value for the GP register. */
4019 if (elf_gp (abfd) == 0)
4020 {
4021 struct bfd_link_hash_entry *h;
4022
4023 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4024 if (h != (struct bfd_link_hash_entry *) NULL
4025 && h->type == bfd_link_hash_defined)
4026 elf_gp (abfd) = (h->u.def.value
4027 + h->u.def.section->output_section->vma
4028 + h->u.def.section->output_offset);
4029 else if (info->relocateable)
4030 {
4031 bfd_vma lo;
4032
4033 /* Make up a value. */
4034 lo = (bfd_vma) -1;
4035 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4036 {
4037 if (o->vma < lo
4038 && (strcmp (o->name, ".sbss") == 0
4039 || strcmp (o->name, ".sdata") == 0
4040 || strcmp (o->name, ".lit4") == 0
4041 || strcmp (o->name, ".lit8") == 0))
4042 lo = o->vma;
4043 }
4044 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4045 }
4046 else
4047 {
4048 /* If the relocate_section function needs to do a reloc
4049 involving the GP value, it should make a reloc_dangerous
4050 callback to warn that GP is not defined. */
4051 }
4052 }
4053
4054 /* Go through the sections and collect the .reginfo and .mdebug
4055 information. */
4056 reginfo_sec = NULL;
4057 mdebug_sec = NULL;
4058 gptab_data_sec = NULL;
4059 gptab_bss_sec = NULL;
4060 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4061 {
4062 if (strcmp (o->name, ".reginfo") == 0)
4063 {
4064 memset (&reginfo, 0, sizeof reginfo);
4065
4066 /* We have found the .reginfo section in the output file.
4067 Look through all the link_orders comprising it and merge
4068 the information together. */
4069 for (p = o->link_order_head;
4070 p != (struct bfd_link_order *) NULL;
4071 p = p->next)
4072 {
4073 asection *input_section;
4074 bfd *input_bfd;
4075 Elf32_External_RegInfo ext;
4076 Elf32_RegInfo sub;
4077
4078 if (p->type != bfd_indirect_link_order)
4079 {
4080 if (p->type == bfd_fill_link_order)
4081 continue;
4082 abort ();
4083 }
4084
4085 input_section = p->u.indirect.section;
4086 input_bfd = input_section->owner;
4087
4088 /* The linker emulation code has probably clobbered the
4089 size to be zero bytes. */
4090 if (input_section->_raw_size == 0)
4091 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4092
4093 if (! bfd_get_section_contents (input_bfd, input_section,
4094 (PTR) &ext,
4095 (file_ptr) 0,
4096 sizeof ext))
4097 return false;
4098
4099 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4100
4101 reginfo.ri_gprmask |= sub.ri_gprmask;
4102 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4103 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4104 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4105 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4106
4107 /* ri_gp_value is set by the function
4108 mips_elf32_section_processing when the section is
4109 finally written out. */
4110
4111 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4112 elf_link_input_bfd ignores this section. */
4113 input_section->flags &=~ SEC_HAS_CONTENTS;
4114 }
4115
4116 /* Force the section size to the value we want. */
4117 o->_raw_size = sizeof (Elf32_External_RegInfo);
4118
4119 /* Skip this section later on (I don't think this currently
4120 matters, but someday it might). */
4121 o->link_order_head = (struct bfd_link_order *) NULL;
4122
4123 reginfo_sec = o;
4124 }
4125
4126 if (strcmp (o->name, ".mdebug") == 0)
4127 {
4128 struct extsym_info einfo;
4129
4130 /* We have found the .mdebug section in the output file.
4131 Look through all the link_orders comprising it and merge
4132 the information together. */
4133 symhdr->magic = swap->sym_magic;
4134 /* FIXME: What should the version stamp be? */
4135 symhdr->vstamp = 0;
4136 symhdr->ilineMax = 0;
4137 symhdr->cbLine = 0;
4138 symhdr->idnMax = 0;
4139 symhdr->ipdMax = 0;
4140 symhdr->isymMax = 0;
4141 symhdr->ioptMax = 0;
4142 symhdr->iauxMax = 0;
4143 symhdr->issMax = 0;
4144 symhdr->issExtMax = 0;
4145 symhdr->ifdMax = 0;
4146 symhdr->crfd = 0;
4147 symhdr->iextMax = 0;
4148
4149 /* We accumulate the debugging information itself in the
4150 debug_info structure. */
4151 debug.line = NULL;
4152 debug.external_dnr = NULL;
4153 debug.external_pdr = NULL;
4154 debug.external_sym = NULL;
4155 debug.external_opt = NULL;
4156 debug.external_aux = NULL;
4157 debug.ss = NULL;
4158 debug.ssext = debug.ssext_end = NULL;
4159 debug.external_fdr = NULL;
4160 debug.external_rfd = NULL;
4161 debug.external_ext = debug.external_ext_end = NULL;
4162
4163 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4164 if (mdebug_handle == (PTR) NULL)
4165 return false;
4166
4167 if (SGI_COMPAT (abfd))
4168 {
4169 asection *s;
4170 EXTR esym;
4171 bfd_vma last;
4172 unsigned int i;
4173 static const char * const name[] =
4174 { ".text", ".init", ".fini", ".data",
4175 ".rodata", ".sdata", ".sbss", ".bss" };
4176 static const int sc[] = { scText, scInit, scFini, scData,
4177 scRData, scSData, scSBss, scBss };
4178
4179 esym.jmptbl = 0;
4180 esym.cobol_main = 0;
4181 esym.weakext = 0;
4182 esym.reserved = 0;
4183 esym.ifd = ifdNil;
4184 esym.asym.iss = issNil;
4185 esym.asym.st = stLocal;
4186 esym.asym.reserved = 0;
4187 esym.asym.index = indexNil;
4188 last = 0;
4189 for (i = 0; i < 8; i++)
4190 {
4191 esym.asym.sc = sc[i];
4192 s = bfd_get_section_by_name (abfd, name[i]);
4193 if (s != NULL)
4194 {
4195 esym.asym.value = s->vma;
4196 last = s->vma + s->_raw_size;
4197 }
4198 else
4199 esym.asym.value = last;
4200
4201 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4202 name[i], &esym))
4203 return false;
4204 }
4205 }
4206
4207 for (p = o->link_order_head;
4208 p != (struct bfd_link_order *) NULL;
4209 p = p->next)
4210 {
4211 asection *input_section;
4212 bfd *input_bfd;
4213 const struct ecoff_debug_swap *input_swap;
4214 struct ecoff_debug_info input_debug;
4215 char *eraw_src;
4216 char *eraw_end;
4217
4218 if (p->type != bfd_indirect_link_order)
4219 {
4220 if (p->type == bfd_fill_link_order)
4221 continue;
4222 abort ();
4223 }
4224
4225 input_section = p->u.indirect.section;
4226 input_bfd = input_section->owner;
4227
4228 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4229 || (get_elf_backend_data (input_bfd)
4230 ->elf_backend_ecoff_debug_swap) == NULL)
4231 {
4232 /* I don't know what a non MIPS ELF bfd would be
4233 doing with a .mdebug section, but I don't really
4234 want to deal with it. */
4235 continue;
4236 }
4237
4238 input_swap = (get_elf_backend_data (input_bfd)
4239 ->elf_backend_ecoff_debug_swap);
4240
4241 BFD_ASSERT (p->size == input_section->_raw_size);
4242
4243 /* The ECOFF linking code expects that we have already
4244 read in the debugging information and set up an
4245 ecoff_debug_info structure, so we do that now. */
4246 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4247 &input_debug))
4248 return false;
4249
4250 if (! (bfd_ecoff_debug_accumulate
4251 (mdebug_handle, abfd, &debug, swap, input_bfd,
4252 &input_debug, input_swap, info)))
4253 return false;
4254
4255 /* Loop through the external symbols. For each one with
4256 interesting information, try to find the symbol in
4257 the linker global hash table and save the information
4258 for the output external symbols. */
4259 eraw_src = input_debug.external_ext;
4260 eraw_end = (eraw_src
4261 + (input_debug.symbolic_header.iextMax
4262 * input_swap->external_ext_size));
4263 for (;
4264 eraw_src < eraw_end;
4265 eraw_src += input_swap->external_ext_size)
4266 {
4267 EXTR ext;
4268 const char *name;
4269 struct mips_elf_link_hash_entry *h;
4270
4271 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4272 if (ext.asym.sc == scNil
4273 || ext.asym.sc == scUndefined
4274 || ext.asym.sc == scSUndefined)
4275 continue;
4276
4277 name = input_debug.ssext + ext.asym.iss;
4278 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4279 name, false, false, true);
4280 if (h == NULL || h->esym.ifd != -2)
4281 continue;
4282
4283 if (ext.ifd != -1)
4284 {
4285 BFD_ASSERT (ext.ifd
4286 < input_debug.symbolic_header.ifdMax);
4287 ext.ifd = input_debug.ifdmap[ext.ifd];
4288 }
4289
4290 h->esym = ext;
4291 }
4292
4293 /* Free up the information we just read. */
4294 free (input_debug.line);
4295 free (input_debug.external_dnr);
4296 free (input_debug.external_pdr);
4297 free (input_debug.external_sym);
4298 free (input_debug.external_opt);
4299 free (input_debug.external_aux);
4300 free (input_debug.ss);
4301 free (input_debug.ssext);
4302 free (input_debug.external_fdr);
4303 free (input_debug.external_rfd);
4304 free (input_debug.external_ext);
4305
4306 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4307 elf_link_input_bfd ignores this section. */
4308 input_section->flags &=~ SEC_HAS_CONTENTS;
4309 }
4310
4311 if (SGI_COMPAT (abfd) && info->shared)
4312 {
4313 /* Create .rtproc section. */
4314 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4315 if (rtproc_sec == NULL)
4316 {
4317 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4318 | SEC_LINKER_CREATED | SEC_READONLY);
4319
4320 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4321 if (rtproc_sec == NULL
4322 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4323 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4324 return false;
4325 }
4326
4327 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4328 info, rtproc_sec, &debug))
4329 return false;
4330 }
4331
4332 /* Build the external symbol information. */
4333 einfo.abfd = abfd;
4334 einfo.info = info;
4335 einfo.debug = &debug;
4336 einfo.swap = swap;
4337 einfo.failed = false;
4338 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4339 mips_elf_output_extsym,
4340 (PTR) &einfo);
4341 if (einfo.failed)
4342 return false;
4343
4344 /* Set the size of the .mdebug section. */
4345 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4346
4347 /* Skip this section later on (I don't think this currently
4348 matters, but someday it might). */
4349 o->link_order_head = (struct bfd_link_order *) NULL;
4350
4351 mdebug_sec = o;
4352 }
4353
4354 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4355 {
4356 const char *subname;
4357 unsigned int c;
4358 Elf32_gptab *tab;
4359 Elf32_External_gptab *ext_tab;
4360 unsigned int i;
4361
4362 /* The .gptab.sdata and .gptab.sbss sections hold
4363 information describing how the small data area would
4364 change depending upon the -G switch. These sections
4365 not used in executables files. */
4366 if (! info->relocateable)
4367 {
4368 asection **secpp;
4369
4370 for (p = o->link_order_head;
4371 p != (struct bfd_link_order *) NULL;
4372 p = p->next)
4373 {
4374 asection *input_section;
4375
4376 if (p->type != bfd_indirect_link_order)
4377 {
4378 if (p->type == bfd_fill_link_order)
4379 continue;
4380 abort ();
4381 }
4382
4383 input_section = p->u.indirect.section;
4384
4385 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4386 elf_link_input_bfd ignores this section. */
4387 input_section->flags &=~ SEC_HAS_CONTENTS;
4388 }
4389
4390 /* Skip this section later on (I don't think this
4391 currently matters, but someday it might). */
4392 o->link_order_head = (struct bfd_link_order *) NULL;
4393
4394 /* Really remove the section. */
4395 for (secpp = &abfd->sections;
4396 *secpp != o;
4397 secpp = &(*secpp)->next)
4398 ;
4399 *secpp = (*secpp)->next;
4400 --abfd->section_count;
4401
4402 continue;
4403 }
4404
4405 /* There is one gptab for initialized data, and one for
4406 uninitialized data. */
4407 if (strcmp (o->name, ".gptab.sdata") == 0)
4408 gptab_data_sec = o;
4409 else if (strcmp (o->name, ".gptab.sbss") == 0)
4410 gptab_bss_sec = o;
4411 else
4412 {
4413 (*_bfd_error_handler)
4414 (_("%s: illegal section name `%s'"),
4415 bfd_get_filename (abfd), o->name);
4416 bfd_set_error (bfd_error_nonrepresentable_section);
4417 return false;
4418 }
4419
4420 /* The linker script always combines .gptab.data and
4421 .gptab.sdata into .gptab.sdata, and likewise for
4422 .gptab.bss and .gptab.sbss. It is possible that there is
4423 no .sdata or .sbss section in the output file, in which
4424 case we must change the name of the output section. */
4425 subname = o->name + sizeof ".gptab" - 1;
4426 if (bfd_get_section_by_name (abfd, subname) == NULL)
4427 {
4428 if (o == gptab_data_sec)
4429 o->name = ".gptab.data";
4430 else
4431 o->name = ".gptab.bss";
4432 subname = o->name + sizeof ".gptab" - 1;
4433 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4434 }
4435
4436 /* Set up the first entry. */
4437 c = 1;
4438 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4439 if (tab == NULL)
4440 return false;
4441 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4442 tab[0].gt_header.gt_unused = 0;
4443
4444 /* Combine the input sections. */
4445 for (p = o->link_order_head;
4446 p != (struct bfd_link_order *) NULL;
4447 p = p->next)
4448 {
4449 asection *input_section;
4450 bfd *input_bfd;
4451 bfd_size_type size;
4452 unsigned long last;
4453 bfd_size_type gpentry;
4454
4455 if (p->type != bfd_indirect_link_order)
4456 {
4457 if (p->type == bfd_fill_link_order)
4458 continue;
4459 abort ();
4460 }
4461
4462 input_section = p->u.indirect.section;
4463 input_bfd = input_section->owner;
4464
4465 /* Combine the gptab entries for this input section one
4466 by one. We know that the input gptab entries are
4467 sorted by ascending -G value. */
4468 size = bfd_section_size (input_bfd, input_section);
4469 last = 0;
4470 for (gpentry = sizeof (Elf32_External_gptab);
4471 gpentry < size;
4472 gpentry += sizeof (Elf32_External_gptab))
4473 {
4474 Elf32_External_gptab ext_gptab;
4475 Elf32_gptab int_gptab;
4476 unsigned long val;
4477 unsigned long add;
4478 boolean exact;
4479 unsigned int look;
4480
4481 if (! (bfd_get_section_contents
4482 (input_bfd, input_section, (PTR) &ext_gptab,
4483 gpentry, sizeof (Elf32_External_gptab))))
4484 {
4485 free (tab);
4486 return false;
4487 }
4488
4489 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4490 &int_gptab);
4491 val = int_gptab.gt_entry.gt_g_value;
4492 add = int_gptab.gt_entry.gt_bytes - last;
4493
4494 exact = false;
4495 for (look = 1; look < c; look++)
4496 {
4497 if (tab[look].gt_entry.gt_g_value >= val)
4498 tab[look].gt_entry.gt_bytes += add;
4499
4500 if (tab[look].gt_entry.gt_g_value == val)
4501 exact = true;
4502 }
4503
4504 if (! exact)
4505 {
4506 Elf32_gptab *new_tab;
4507 unsigned int max;
4508
4509 /* We need a new table entry. */
4510 new_tab = ((Elf32_gptab *)
4511 bfd_realloc ((PTR) tab,
4512 (c + 1) * sizeof (Elf32_gptab)));
4513 if (new_tab == NULL)
4514 {
4515 free (tab);
4516 return false;
4517 }
4518 tab = new_tab;
4519 tab[c].gt_entry.gt_g_value = val;
4520 tab[c].gt_entry.gt_bytes = add;
4521
4522 /* Merge in the size for the next smallest -G
4523 value, since that will be implied by this new
4524 value. */
4525 max = 0;
4526 for (look = 1; look < c; look++)
4527 {
4528 if (tab[look].gt_entry.gt_g_value < val
4529 && (max == 0
4530 || (tab[look].gt_entry.gt_g_value
4531 > tab[max].gt_entry.gt_g_value)))
4532 max = look;
4533 }
4534 if (max != 0)
4535 tab[c].gt_entry.gt_bytes +=
4536 tab[max].gt_entry.gt_bytes;
4537
4538 ++c;
4539 }
4540
4541 last = int_gptab.gt_entry.gt_bytes;
4542 }
4543
4544 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4545 elf_link_input_bfd ignores this section. */
4546 input_section->flags &=~ SEC_HAS_CONTENTS;
4547 }
4548
4549 /* The table must be sorted by -G value. */
4550 if (c > 2)
4551 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4552
4553 /* Swap out the table. */
4554 ext_tab = ((Elf32_External_gptab *)
4555 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4556 if (ext_tab == NULL)
4557 {
4558 free (tab);
4559 return false;
4560 }
4561
4562 for (i = 0; i < c; i++)
4563 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4564 free (tab);
4565
4566 o->_raw_size = c * sizeof (Elf32_External_gptab);
4567 o->contents = (bfd_byte *) ext_tab;
4568
4569 /* Skip this section later on (I don't think this currently
4570 matters, but someday it might). */
4571 o->link_order_head = (struct bfd_link_order *) NULL;
4572 }
4573 }
4574
4575 /* Invoke the regular ELF backend linker to do all the work. */
4576 if (! bfd_elf32_bfd_final_link (abfd, info))
4577 return false;
4578
4579 /* Now write out the computed sections. */
4580
4581 if (reginfo_sec != (asection *) NULL)
4582 {
4583 Elf32_External_RegInfo ext;
4584
4585 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4586 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4587 (file_ptr) 0, sizeof ext))
4588 return false;
4589 }
4590
4591 if (mdebug_sec != (asection *) NULL)
4592 {
4593 BFD_ASSERT (abfd->output_has_begun);
4594 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4595 swap, info,
4596 mdebug_sec->filepos))
4597 return false;
4598
4599 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4600 }
4601
4602 if (gptab_data_sec != (asection *) NULL)
4603 {
4604 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4605 gptab_data_sec->contents,
4606 (file_ptr) 0,
4607 gptab_data_sec->_raw_size))
4608 return false;
4609 }
4610
4611 if (gptab_bss_sec != (asection *) NULL)
4612 {
4613 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4614 gptab_bss_sec->contents,
4615 (file_ptr) 0,
4616 gptab_bss_sec->_raw_size))
4617 return false;
4618 }
4619
4620 if (SGI_COMPAT (abfd))
4621 {
4622 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4623 if (rtproc_sec != NULL)
4624 {
4625 if (! bfd_set_section_contents (abfd, rtproc_sec,
4626 rtproc_sec->contents,
4627 (file_ptr) 0,
4628 rtproc_sec->_raw_size))
4629 return false;
4630 }
4631 }
4632
4633 return true;
4634 }
4635
4636 /* Handle a MIPS ELF HI16 reloc. */
4637
4638 static void
4639 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4640 bfd *input_bfd;
4641 Elf_Internal_Rela *relhi;
4642 Elf_Internal_Rela *rello;
4643 bfd_byte *contents;
4644 bfd_vma addend;
4645 {
4646 bfd_vma insn;
4647 bfd_vma addlo;
4648
4649 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4650
4651 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4652 addlo &= 0xffff;
4653
4654 addend += ((insn & 0xffff) << 16) + addlo;
4655
4656 if ((addlo & 0x8000) != 0)
4657 addend -= 0x10000;
4658 if ((addend & 0x8000) != 0)
4659 addend += 0x10000;
4660
4661 bfd_put_32 (input_bfd,
4662 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4663 contents + relhi->r_offset);
4664 }
4665
4666 /* Handle a MIPS ELF local GOT16 reloc. */
4667
4668 static boolean
4669 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4670 contents, addend)
4671 bfd *output_bfd;
4672 bfd *input_bfd;
4673 asection *sgot;
4674 Elf_Internal_Rela *relhi;
4675 Elf_Internal_Rela *rello;
4676 bfd_byte *contents;
4677 bfd_vma addend;
4678 {
4679 unsigned int assigned_gotno;
4680 unsigned int i;
4681 bfd_vma insn;
4682 bfd_vma addlo;
4683 bfd_vma address;
4684 bfd_vma hipage;
4685 bfd_byte *got_contents;
4686 struct mips_got_info *g;
4687
4688 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4689
4690 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4691 addlo &= 0xffff;
4692
4693 addend += ((insn & 0xffff) << 16) + addlo;
4694
4695 if ((addlo & 0x8000) != 0)
4696 addend -= 0x10000;
4697 if ((addend & 0x8000) != 0)
4698 addend += 0x10000;
4699
4700 /* Get a got entry representing requested hipage. */
4701 BFD_ASSERT (elf_section_data (sgot) != NULL);
4702 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4703 BFD_ASSERT (g != NULL);
4704
4705 assigned_gotno = g->assigned_gotno;
4706 got_contents = sgot->contents;
4707 hipage = addend & 0xffff0000;
4708
4709 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4710 {
4711 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4712 if (hipage == (address & 0xffff0000))
4713 break;
4714 }
4715
4716 if (i == assigned_gotno)
4717 {
4718 if (assigned_gotno >= g->local_gotno)
4719 {
4720 (*_bfd_error_handler)
4721 (_("more got entries are needed for hipage relocations"));
4722 bfd_set_error (bfd_error_bad_value);
4723 return false;
4724 }
4725
4726 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4727 ++g->assigned_gotno;
4728 }
4729
4730 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4731 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4732 contents + relhi->r_offset);
4733
4734 return true;
4735 }
4736
4737 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4738
4739 static void
4740 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4741 bfd *input_bfd;
4742 Elf_Internal_Rela *rel;
4743 bfd_byte *contents;
4744 bfd_vma offset;
4745 {
4746 bfd_vma insn;
4747
4748 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4749 bfd_put_32 (input_bfd,
4750 (insn & 0xffff0000) | (offset & 0xffff),
4751 contents + rel->r_offset);
4752 }
4753
4754 /* Relocate a MIPS ELF section. */
4755
4756 static boolean
4757 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4758 contents, relocs, local_syms, local_sections)
4759 bfd *output_bfd;
4760 struct bfd_link_info *info;
4761 bfd *input_bfd;
4762 asection *input_section;
4763 bfd_byte *contents;
4764 Elf_Internal_Rela *relocs;
4765 Elf_Internal_Sym *local_syms;
4766 asection **local_sections;
4767 {
4768 Elf_Internal_Shdr *symtab_hdr;
4769 size_t locsymcount;
4770 size_t extsymoff;
4771 asection *sgot, *sreloc, *scpt;
4772 bfd *dynobj;
4773 bfd_vma gp;
4774 Elf_Internal_Rela *rel;
4775 Elf_Internal_Rela *relend;
4776 struct mips_got_info *g;
4777
4778 dynobj = elf_hash_table (info)->dynobj;
4779 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4780
4781 sgot = NULL;
4782 sreloc = NULL;
4783 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4784 scpt = NULL;
4785 else
4786 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4787 g = NULL;
4788
4789 if (elf_bad_symtab (input_bfd))
4790 {
4791 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4792 extsymoff = 0;
4793 }
4794 else
4795 {
4796 locsymcount = symtab_hdr->sh_info;
4797 extsymoff = symtab_hdr->sh_info;
4798 }
4799
4800 gp = _bfd_get_gp_value (output_bfd);
4801
4802 rel = relocs;
4803 relend = relocs + input_section->reloc_count;
4804 for (; rel < relend; rel++)
4805 {
4806 int r_type;
4807 reloc_howto_type *howto;
4808 unsigned long r_symndx;
4809 bfd_vma addend;
4810 struct elf_link_hash_entry *h;
4811 asection *sec;
4812 Elf_Internal_Sym *sym;
4813 struct mips_elf_link_hash_entry *mh;
4814 int other;
4815 bfd_reloc_status_type r;
4816
4817 r_type = ELF32_R_TYPE (rel->r_info);
4818 if ((r_type < 0 || r_type >= (int) R_MIPS_max)
4819 /* start-sanitize-sky */
4820 && r_type != R_MIPS_DVP_11_PCREL
4821 && r_type != R_MIPS_DVP_27_S4
4822 /* end-sanitize-sky */
4823 && r_type != R_MIPS16_26
4824 && r_type != R_MIPS16_GPREL)
4825 {
4826 bfd_set_error (bfd_error_bad_value);
4827 return false;
4828 }
4829 if (r_type == R_MIPS16_26)
4830 howto = &elf_mips16_jump_howto;
4831 else if (r_type == R_MIPS16_GPREL)
4832 howto = &elf_mips16_gprel_howto;
4833 /* start-sanitize-sky */
4834 else if (r_type == R_MIPS_DVP_11_PCREL)
4835 howto = &elf_mips_dvp_11_pcrel_howto;
4836 else if (r_type == R_MIPS_DVP_27_S4)
4837 howto = &elf_mips_dvp_27_s4_howto;
4838 /* end-sanitize-sky */
4839 else
4840 howto = elf_mips_howto_table + r_type;
4841
4842 if (dynobj != NULL
4843 && (r_type == R_MIPS_CALL16
4844 || r_type == R_MIPS_GOT16
4845 || r_type == R_MIPS_CALL_HI16
4846 || r_type == R_MIPS_CALL_LO16
4847 || r_type == R_MIPS_GOT_HI16
4848 || r_type == R_MIPS_GOT_LO16))
4849 {
4850 /* We need the .got section. */
4851 if (sgot == NULL)
4852 {
4853 sgot = bfd_get_section_by_name (dynobj, ".got");
4854 BFD_ASSERT (sgot != NULL);
4855 BFD_ASSERT (elf_section_data (sgot) != NULL);
4856 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4857 BFD_ASSERT (g != NULL);
4858 }
4859 }
4860
4861 r_symndx = ELF32_R_SYM (rel->r_info);
4862
4863 /* Mix in the change in GP address for a GP relative reloc. */
4864 if (r_type != R_MIPS_GPREL16
4865 && r_type != R_MIPS_LITERAL
4866 && r_type != R_MIPS_GPREL32
4867 && r_type != R_MIPS16_GPREL)
4868 addend = 0;
4869 else
4870 {
4871 if (gp == 0)
4872 {
4873 if (! ((*info->callbacks->reloc_dangerous)
4874 (info,
4875 _("GP relative relocation when GP not defined"),
4876 input_bfd, input_section,
4877 rel->r_offset)))
4878 return false;
4879 /* Only give the error once per link. */
4880 gp = 4;
4881 _bfd_set_gp_value (output_bfd, gp);
4882 }
4883
4884 if (r_symndx < extsymoff
4885 || (elf_bad_symtab (input_bfd)
4886 && local_sections[r_symndx] != NULL))
4887 {
4888 /* This is a relocation against a section. The current
4889 addend in the instruction is the difference between
4890 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4891 must change this to be the difference between the
4892 final definition (which will end up in RELOCATION)
4893 and the GP value of OUTPUT_BFD (which is in GP). */
4894 addend = elf_gp (input_bfd) - gp;
4895 }
4896 else if (! info->relocateable)
4897 {
4898 /* We are doing a final link. The current addend in the
4899 instruction is simply the desired offset into the
4900 symbol (normally zero). We want the instruction to
4901 hold the difference between the final definition of
4902 the symbol (which will end up in RELOCATION) and the
4903 GP value of OUTPUT_BFD (which is in GP). */
4904 addend = - gp;
4905 }
4906 else
4907 {
4908 /* We are generating relocateable output, and we aren't
4909 going to define this symbol, so we just leave the
4910 instruction alone. */
4911 addend = 0;
4912 }
4913 }
4914
4915 h = NULL;
4916 sym = NULL;
4917 sec = NULL;
4918 if (info->relocateable)
4919 {
4920 /* This is a relocateable link. We don't have to change
4921 anything, unless the reloc is against a section symbol,
4922 in which case we have to adjust according to where the
4923 section symbol winds up in the output section. */
4924 if (r_symndx >= locsymcount
4925 || (elf_bad_symtab (input_bfd)
4926 && local_sections[r_symndx] == NULL))
4927 r = bfd_reloc_ok;
4928 else
4929 {
4930 sym = local_syms + r_symndx;
4931 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4932 r = bfd_reloc_ok;
4933 else
4934 {
4935 sec = local_sections[r_symndx];
4936
4937 /* It would be logical to add sym->st_value here,
4938 but Irix 5 sometimes generates a garbage symbol
4939 value. */
4940 addend += sec->output_offset;
4941
4942 /* If this is HI16 or GOT16 with an associated LO16,
4943 adjust the addend accordingly. Otherwise, just
4944 relocate. */
4945 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4946 r = _bfd_relocate_contents (howto, input_bfd,
4947 addend,
4948 contents + rel->r_offset + 4);
4949 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
4950 r = _bfd_relocate_contents (howto, input_bfd,
4951 addend,
4952 contents + rel->r_offset);
4953 else
4954 {
4955 Elf_Internal_Rela *lorel;
4956
4957 /* As a GNU extension, permit an arbitrary
4958 number of R_MIPS_HI16 relocs before the
4959 R_MIPS_LO16 reloc. This permits gcc to emit
4960 the HI and LO relocs itself. */
4961 if (r_type == R_MIPS_GOT16)
4962 lorel = rel + 1;
4963 else
4964 {
4965 for (lorel = rel + 1;
4966 (lorel < relend
4967 && (ELF32_R_TYPE (lorel->r_info)
4968 == R_MIPS_HI16));
4969 lorel++)
4970 ;
4971 }
4972 if (lorel < relend
4973 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4974 {
4975 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4976 contents, addend);
4977 r = bfd_reloc_ok;
4978 }
4979 else
4980 r = _bfd_relocate_contents (howto, input_bfd,
4981 addend,
4982 contents + rel->r_offset);
4983 }
4984 }
4985 }
4986 }
4987 else
4988 {
4989 bfd_vma relocation;
4990 boolean local;
4991 boolean undefined_error;
4992
4993 /* This is a final link. */
4994 undefined_error = false;
4995 sym = NULL;
4996 if (r_symndx < extsymoff
4997 || (elf_bad_symtab (input_bfd)
4998 && local_sections[r_symndx] != NULL))
4999 {
5000 local = true;
5001 sym = local_syms + r_symndx;
5002 sec = local_sections[r_symndx];
5003 relocation = (sec->output_section->vma
5004 + sec->output_offset);
5005
5006 /* It would be logical to always add sym->st_value here,
5007 but Irix 5 sometimes generates a garbage symbol
5008 value. */
5009 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5010 relocation += sym->st_value;
5011
5012 /* mips16 text labels should be treated as odd. */
5013 if (sym->st_other == STO_MIPS16)
5014 ++relocation;
5015 }
5016 else
5017 {
5018 long indx;
5019
5020 local = false;
5021 indx = r_symndx - extsymoff;
5022 h = elf_sym_hashes (input_bfd)[indx];
5023 while (h->root.type == bfd_link_hash_indirect
5024 || h->root.type == bfd_link_hash_warning)
5025 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5026 if (strcmp (h->root.root.string, "_gp_disp") == 0)
5027 {
5028 if (gp == 0)
5029 {
5030 if (! ((*info->callbacks->reloc_dangerous)
5031 (info,
5032 _("_gp_disp used when GP not defined"),
5033 input_bfd, input_section,
5034 rel->r_offset)))
5035 return false;
5036 /* Only give the error once per link. */
5037 gp = 4;
5038 _bfd_set_gp_value (output_bfd, gp);
5039 relocation = 0;
5040 }
5041 else
5042 {
5043 sec = input_section;
5044 if (sec->output_section != NULL)
5045 relocation = (gp
5046 - (rel->r_offset
5047 + sec->output_section->vma
5048 + sec->output_offset));
5049 else
5050 relocation = gp - rel->r_offset;
5051 if (r_type == R_MIPS_LO16)
5052 relocation += 4;
5053 }
5054 }
5055 else if (h->root.type == bfd_link_hash_defined
5056 || h->root.type == bfd_link_hash_defweak)
5057 {
5058 sec = h->root.u.def.section;
5059 if (sec->output_section == NULL)
5060 relocation = 0;
5061 else
5062 relocation = (h->root.u.def.value
5063 + sec->output_section->vma
5064 + sec->output_offset);
5065 }
5066 else if (h->root.type == bfd_link_hash_undefweak)
5067 relocation = 0;
5068 else if (info->shared && ! info->symbolic)
5069 relocation = 0;
5070 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
5071 {
5072 /* If this is a dynamic link, we should have created
5073 a _DYNAMIC_LINK symbol in
5074 mips_elf_create_dynamic_sections. Otherwise, we
5075 should define the symbol with a value of 0.
5076 FIXME: It should probably get into the symbol
5077 table somehow as well. */
5078 BFD_ASSERT (! info->shared);
5079 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
5080 ".dynamic") == NULL);
5081 relocation = 0;
5082 }
5083 else
5084 {
5085 if (! ((*info->callbacks->undefined_symbol)
5086 (info, h->root.root.string, input_bfd,
5087 input_section, rel->r_offset)))
5088 return false;
5089 undefined_error = true;
5090 relocation = 0;
5091 }
5092 }
5093
5094 mh = (struct mips_elf_link_hash_entry *) h;
5095 if (h != NULL)
5096 other = h->other;
5097 else if (sym != NULL)
5098 other = sym->st_other;
5099 else
5100 other = 0;
5101
5102 /* If this function has an fn_stub, then it is a mips16
5103 function which needs a stub if it is called by a 32 bit
5104 function. If this reloc is anything other than a 16 bit
5105 call, redirect the reloc to the stub. We don't redirect
5106 relocs from other stub functions. */
5107 if (r_type != R_MIPS16_26
5108 && ((mh != NULL
5109 && mh->fn_stub != NULL)
5110 || (mh == NULL
5111 && elf_tdata (input_bfd)->local_stubs != NULL
5112 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5113 && strncmp (bfd_get_section_name (input_bfd, input_section),
5114 FN_STUB, sizeof FN_STUB - 1) != 0
5115 && strncmp (bfd_get_section_name (input_bfd, input_section),
5116 CALL_STUB, sizeof CALL_STUB - 1) != 0
5117 && strncmp (bfd_get_section_name (input_bfd, input_section),
5118 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
5119 {
5120 if (mh != NULL)
5121 {
5122 BFD_ASSERT (mh->need_fn_stub);
5123 relocation = (mh->fn_stub->output_section->vma
5124 + mh->fn_stub->output_offset);
5125 }
5126 else
5127 {
5128 asection *fn_stub;
5129
5130 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
5131 relocation = (fn_stub->output_section->vma
5132 + fn_stub->output_offset);
5133 }
5134
5135 /* RELOCATION now points to 32 bit code. */
5136 other = 0;
5137 }
5138
5139 /* If this function has a call_stub, then it is called by a
5140 mips16 function; the call needs to go through a stub if
5141 this function is a 32 bit function. If this reloc is a
5142 16 bit call, and the symbol is not a 16 bit function,
5143 then redirect the reloc to the stub. Note that we don't
5144 need to worry about calling the function through a
5145 function pointer; such calls are handled by routing
5146 through a special mips16 routine. We don't have to check
5147 whether this call is from a stub; it can't be, because a
5148 stub contains 32 bit code, and hence can not have a 16
5149 bit reloc. */
5150 if (r_type == R_MIPS16_26
5151 && mh != NULL
5152 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
5153 && other != STO_MIPS16)
5154 {
5155 asection *stub;
5156
5157 /* If both call_stub and call_fp_stub are defined, we
5158 can figure out which one to use by seeing which one
5159 appears in the input file. */
5160 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
5161 {
5162 asection *o;
5163
5164 stub = NULL;
5165 for (o = input_bfd->sections; o != NULL; o = o->next)
5166 {
5167 if (strncmp (bfd_get_section_name (input_bfd, o),
5168 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5169 {
5170 stub = mh->call_fp_stub;
5171 break;
5172 }
5173 }
5174 if (stub == NULL)
5175 stub = mh->call_stub;
5176 }
5177 else if (mh->call_stub != NULL)
5178 stub = mh->call_stub;
5179 else
5180 stub = mh->call_fp_stub;
5181
5182 BFD_ASSERT (stub->_raw_size > 0);
5183 relocation = stub->output_section->vma + stub->output_offset;
5184 }
5185
5186 if (r_type == R_MIPS_HI16)
5187 {
5188 Elf_Internal_Rela *lorel;
5189
5190 /* As a GNU extension, permit an arbitrary number of
5191 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
5192 This permits gcc to emit the HI and LO relocs itself. */
5193 for (lorel = rel + 1;
5194 (lorel < relend
5195 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
5196 lorel++)
5197 ;
5198 if (lorel < relend
5199 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5200 {
5201 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5202 contents, relocation + addend);
5203 r = bfd_reloc_ok;
5204 }
5205 else
5206 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5207 contents, rel->r_offset,
5208 relocation, addend);
5209 }
5210 else if (r_type == R_MIPS_GOT16 && local)
5211 {
5212 /* GOT16 must also have an associated LO16 in the local
5213 case. In this case, the addend is extracted and the
5214 section in which the referenced object is determined.
5215 Then the final address of the object is computed and
5216 the GOT entry for the hipage (an aligned 64kb chunk)
5217 is added to .got section if needed. The offset field
5218 of the GOT16-relocated instruction is replaced by the
5219 index of this GOT entry for the hipage. */
5220 if ((rel + 1) < relend
5221 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
5222 {
5223 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
5224 sgot, rel, rel + 1,
5225 contents,
5226 relocation + addend))
5227 return false;
5228 r = bfd_reloc_ok;
5229 }
5230 else
5231 r = bfd_reloc_outofrange;
5232 }
5233 else if (r_type == R_MIPS_CALL16
5234 || r_type == R_MIPS_GOT16
5235 || r_type == R_MIPS_CALL_LO16
5236 || r_type == R_MIPS_GOT_LO16)
5237 {
5238 bfd_vma offset;
5239
5240 /* This symbol must be registered as a global symbol
5241 having the corresponding got entry. */
5242 BFD_ASSERT (h->got_offset != (bfd_vma) -1);
5243
5244 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5245 BFD_ASSERT (g->local_gotno <= offset
5246 && offset < sgot->_raw_size);
5247 bfd_put_32 (output_bfd, relocation + addend,
5248 sgot->contents + offset);
5249 offset = (sgot->output_section->vma + sgot->output_offset
5250 + offset - gp);
5251 mips_elf_relocate_global_got (input_bfd, rel, contents,
5252 offset);
5253 r = bfd_reloc_ok;
5254 }
5255 else if (r_type == R_MIPS_CALL_HI16
5256 || r_type == R_MIPS_GOT_HI16)
5257 {
5258 bfd_vma offset;
5259
5260 /* This must be a global symbol with a got entry. The
5261 next reloc must be the corresponding LO16 reloc. */
5262 BFD_ASSERT (h != NULL && h->got_offset != (bfd_vma) -1);
5263 BFD_ASSERT ((rel + 1) < relend);
5264 BFD_ASSERT ((int) ELF32_R_TYPE ((rel + 1)->r_info)
5265 == (r_type == R_MIPS_CALL_HI16
5266 ? (int) R_MIPS_CALL_LO16
5267 : (int) R_MIPS_GOT_LO16));
5268
5269 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5270 BFD_ASSERT (g->local_gotno <= offset
5271 && offset < sgot->_raw_size);
5272 bfd_put_32 (output_bfd, relocation + addend,
5273 sgot->contents + offset);
5274 offset = (sgot->output_section->vma + sgot->output_offset
5275 + offset - gp);
5276 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5277 offset);
5278 r = bfd_reloc_ok;
5279 }
5280 else if (r_type == R_MIPS_REL32
5281 || r_type == R_MIPS_32)
5282 {
5283 Elf_Internal_Rel outrel;
5284 Elf32_crinfo cptrel;
5285 bfd_byte *cr;
5286
5287 if ((info->shared
5288 || (elf_hash_table (info)->dynamic_sections_created
5289 && h != NULL
5290 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5291 == 0)))
5292 && (input_section->flags & SEC_ALLOC) != 0)
5293 {
5294 boolean skip;
5295
5296 /* When generating a shared object, these
5297 relocations are copied into the output file to be
5298 resolved at run time. */
5299 if (sreloc == NULL)
5300 {
5301 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5302 BFD_ASSERT (sreloc != NULL);
5303 }
5304
5305 skip = false;
5306
5307 if (elf_section_data (input_section)->stab_info == NULL)
5308 outrel.r_offset = rel->r_offset;
5309 else
5310 {
5311 bfd_vma off;
5312
5313 off = (_bfd_stab_section_offset
5314 (output_bfd, &elf_hash_table (info)->stab_info,
5315 input_section,
5316 &elf_section_data (input_section)->stab_info,
5317 rel->r_offset));
5318 if (off == (bfd_vma) -1)
5319 skip = true;
5320 outrel.r_offset = off;
5321 }
5322
5323 outrel.r_offset += (input_section->output_section->vma
5324 + input_section->output_offset);
5325
5326 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5327
5328 if (skip)
5329 memset (&outrel, 0, sizeof outrel);
5330 else if (h != NULL
5331 && (! info->symbolic
5332 || (h->elf_link_hash_flags
5333 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5334 {
5335 BFD_ASSERT (h->dynindx != -1);
5336 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5337 sec = input_section;
5338 }
5339 else
5340 {
5341 long indx;
5342
5343 if (h == NULL)
5344 sec = local_sections[r_symndx];
5345 else
5346 {
5347 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5348 || (h->root.type
5349 == bfd_link_hash_defweak));
5350 sec = h->root.u.def.section;
5351 }
5352 if (sec != NULL && bfd_is_abs_section (sec))
5353 indx = 0;
5354 else if (sec == NULL || sec->owner == NULL)
5355 {
5356 bfd_set_error (bfd_error_bad_value);
5357 return false;
5358 }
5359 else
5360 {
5361 asection *osec;
5362
5363 osec = sec->output_section;
5364 indx = elf_section_data (osec)->dynindx;
5365 if (indx == 0)
5366 abort ();
5367 }
5368
5369 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5370 addend += relocation;
5371 }
5372
5373 if (! skip)
5374 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5375
5376 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5377 (((Elf32_External_Rel *)
5378 sreloc->contents)
5379 + sreloc->reloc_count));
5380 ++sreloc->reloc_count;
5381
5382 if (! skip && SGI_COMPAT (output_bfd))
5383 {
5384 if (scpt == NULL)
5385 continue;
5386
5387 /* Make an entry of compact relocation info. */
5388 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5389 cptrel.vaddr = (rel->r_offset
5390 + input_section->output_section->vma
5391 + input_section->output_offset);
5392 if (r_type == R_MIPS_REL32)
5393 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5394 else
5395 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5396 mips_elf_set_cr_dist2to (cptrel, 0);
5397 cptrel.konst = addend;
5398
5399 cr = (scpt->contents
5400 + sizeof (Elf32_External_compact_rel));
5401 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5402 ((Elf32_External_crinfo *) cr
5403 + scpt->reloc_count));
5404 ++scpt->reloc_count;
5405 }
5406
5407 /* This reloc will be computed at runtime, so
5408 there's no need to do anything now. */
5409 continue;
5410 }
5411 else
5412 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5413 contents, rel->r_offset,
5414 relocation, addend);
5415 }
5416 else if (r_type == R_MIPS_64)
5417 {
5418 bfd_size_type addr;
5419 unsigned long val;
5420
5421 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5422 addr = rel->r_offset;
5423 if (bfd_big_endian (input_bfd))
5424 addr += 4;
5425 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5426 contents, addr, relocation,
5427 addend);
5428 val = bfd_get_32 (input_bfd, contents + addr);
5429 if ((val & 0x80000000) != 0)
5430 val = 0xffffffff;
5431 else
5432 val = 0;
5433 addr = rel->r_offset;
5434 if (bfd_little_endian (input_bfd))
5435 addr += 4;
5436 bfd_put_32 (input_bfd, val, contents + addr);
5437 }
5438 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5439 {
5440 unsigned long insn;
5441
5442 /* This is a jump to a mips16 routine from a mips32
5443 routine. We need to change jal into jalx. */
5444 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5445 if (((insn >> 26) & 0x3f) != 0x3
5446 && ((insn >> 26) & 0x3f) != 0x1d)
5447 {
5448 (*_bfd_error_handler)
5449 (_("%s: %s+0x%lx: jump to mips16 routine which is not jal"),
5450 bfd_get_filename (input_bfd),
5451 input_section->name,
5452 (unsigned long) rel->r_offset);
5453 bfd_set_error (bfd_error_bad_value);
5454 return false;
5455 }
5456 insn = (insn & 0x3ffffff) | (0x1d << 26);
5457 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5458 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5459 contents, rel->r_offset,
5460 relocation, addend);
5461 }
5462 else if (r_type == R_MIPS16_26)
5463 {
5464 /* It's easiest to do the normal relocation, and then
5465 dig out the instruction and swap the first word the
5466 way the mips16 expects it. If this is little endian,
5467 though, we need to swap the two words first, and then
5468 swap them back again later, so that the address looks
5469 right. */
5470
5471 if (bfd_little_endian (input_bfd))
5472 {
5473 unsigned long insn;
5474
5475 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5476 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5477 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5478 }
5479
5480 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5481 contents, rel->r_offset,
5482 relocation, addend);
5483 if (r == bfd_reloc_ok)
5484 {
5485 unsigned long insn;
5486
5487 if (bfd_little_endian (input_bfd))
5488 {
5489 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5490 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5491 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5492 }
5493
5494 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5495 insn = ((insn & 0xfc00)
5496 | ((insn & 0x1f) << 5)
5497 | ((insn & 0x3e0) >> 5));
5498 /* If this is a jump to a 32 bit routine, then make
5499 it jalx. */
5500 if (other != STO_MIPS16)
5501 insn |= 0x400;
5502 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5503 }
5504 }
5505 else if (r_type == R_MIPS16_GPREL)
5506 {
5507 unsigned short extend, insn;
5508 bfd_byte buf[4];
5509 unsigned long final;
5510
5511 /* Extract the addend into buf, run the regular reloc,
5512 and stuff the resulting value back into the
5513 instructions. */
5514 if (rel->r_offset > input_section->_raw_size)
5515 r = bfd_reloc_outofrange;
5516 else
5517 {
5518 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5519 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5520 bfd_put_32 (input_bfd,
5521 (((extend & 0x1f) << 11)
5522 | (extend & 0x7e0)
5523 | (insn & 0x1f)),
5524 buf);
5525 r = _bfd_final_link_relocate (howto, input_bfd,
5526 input_section, buf,
5527 (bfd_vma) 0, relocation,
5528 addend);
5529 final = bfd_get_32 (input_bfd, buf);
5530 bfd_put_16 (input_bfd,
5531 ((extend & 0xf800)
5532 | ((final >> 11) & 0x1f)
5533 | (final & 0x7e0)),
5534 contents + rel->r_offset);
5535 bfd_put_16 (input_bfd,
5536 ((insn & 0xffe0)
5537 | (final & 0x1f)),
5538 contents + rel->r_offset + 2);
5539 }
5540 }
5541 else
5542 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5543 contents, rel->r_offset,
5544 relocation, addend);
5545
5546 /* The jal instruction can only jump to an address which is
5547 divisible by 4, and it can only jump to an address with
5548 the same upper 4 bits as the PC. */
5549 if (r == bfd_reloc_ok
5550 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5551 {
5552 bfd_vma addr;
5553
5554 addr = relocation;
5555 if (other == STO_MIPS16)
5556 addr &= ~ (bfd_vma) 1;
5557 addr += addend;
5558 if ((addr & 3) != 0
5559 || ((addr & 0xf0000000)
5560 != ((input_section->output_section->vma
5561 + input_section->output_offset
5562 + rel->r_offset)
5563 & 0xf0000000)))
5564 r = bfd_reloc_overflow;
5565 }
5566
5567 /* Don't bother to report a relocation overflow for a call
5568 to a weak undefined symbol with a value of zero. This
5569 permits us to use
5570 if (!f) f();
5571 even if we aren't in range to call address zero. */
5572 if (r == bfd_reloc_overflow
5573 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26)
5574 && relocation + addend == 0
5575 && h != NULL
5576 && h->root.type == bfd_link_hash_undefweak)
5577 r = bfd_reloc_ok;
5578
5579 /* If we've already issued an error for an undefined symbol,
5580 don't issue another useless error. */
5581 if (undefined_error
5582 && (r == bfd_reloc_undefined || r == bfd_reloc_overflow))
5583 r = bfd_reloc_ok;
5584
5585 if (SGI_COMPAT (abfd)
5586 && scpt != NULL
5587 && (input_section->flags & SEC_ALLOC) != 0)
5588 {
5589 Elf32_crinfo cptrel;
5590 bfd_byte *cr;
5591
5592 /* Make an entry of compact relocation info. */
5593 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5594 cptrel.vaddr = (rel->r_offset
5595 + input_section->output_section->vma
5596 + input_section->output_offset);
5597
5598 switch (r_type)
5599 {
5600 case R_MIPS_26:
5601 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5602 /* XXX How should we set dist2to in this case. */
5603 mips_elf_set_cr_dist2to (cptrel, 8);
5604 cptrel.konst = addend + relocation;
5605 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5606 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5607 ((Elf32_External_crinfo *) cr
5608 + scpt->reloc_count));
5609 ++scpt->reloc_count;
5610 break;
5611
5612 case R_MIPS_GPREL16:
5613 case R_MIPS_LITERAL:
5614 case R_MIPS_GPREL32:
5615 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5616 cptrel.konst = gp - cptrel.vaddr;
5617 mips_elf_set_cr_dist2to (cptrel, 4);
5618 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5619 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5620 ((Elf32_External_crinfo *) cr
5621 + scpt->reloc_count));
5622 ++scpt->reloc_count;
5623 break;
5624
5625 default:
5626 break;
5627 }
5628 }
5629 }
5630
5631 if (r != bfd_reloc_ok)
5632 {
5633 switch (r)
5634 {
5635 default:
5636 case bfd_reloc_outofrange:
5637 abort ();
5638 case bfd_reloc_overflow:
5639 {
5640 const char *name;
5641
5642 if (h != NULL)
5643 name = h->root.root.string;
5644 else
5645 {
5646 name = bfd_elf_string_from_elf_section (input_bfd,
5647 symtab_hdr->sh_link,
5648 sym->st_name);
5649 if (name == NULL)
5650 return false;
5651 if (*name == '\0')
5652 name = bfd_section_name (input_bfd, sec);
5653 }
5654 if (! ((*info->callbacks->reloc_overflow)
5655 (info, name, howto->name, (bfd_vma) 0,
5656 input_bfd, input_section, rel->r_offset)))
5657 return false;
5658 }
5659 break;
5660 }
5661 }
5662 }
5663
5664 return true;
5665 }
5666
5667 /* This hook function is called before the linker writes out a global
5668 symbol. We mark symbols as small common if appropriate. This is
5669 also where we undo the increment of the value for a mips16 symbol. */
5670
5671 /*ARGSIGNORED*/
5672 static boolean
5673 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5674 bfd *abfd;
5675 struct bfd_link_info *info;
5676 const char *name;
5677 Elf_Internal_Sym *sym;
5678 asection *input_sec;
5679 {
5680 /* If we see a common symbol, which implies a relocatable link, then
5681 if a symbol was small common in an input file, mark it as small
5682 common in the output file. */
5683 if (sym->st_shndx == SHN_COMMON
5684 && strcmp (input_sec->name, ".scommon") == 0)
5685 sym->st_shndx = SHN_MIPS_SCOMMON;
5686
5687 if (sym->st_other == STO_MIPS16
5688 && (sym->st_value & 1) != 0)
5689 --sym->st_value;
5690
5691 return true;
5692 }
5693 \f
5694 /* Functions for the dynamic linker. */
5695
5696 /* The name of the dynamic interpreter. This is put in the .interp
5697 section. */
5698
5699 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5700
5701 /* Create dynamic sections when linking against a dynamic object. */
5702
5703 static boolean
5704 mips_elf_create_dynamic_sections (abfd, info)
5705 bfd *abfd;
5706 struct bfd_link_info *info;
5707 {
5708 struct elf_link_hash_entry *h;
5709 flagword flags;
5710 register asection *s;
5711 const char * const *namep;
5712
5713 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5714 | SEC_LINKER_CREATED | SEC_READONLY);
5715
5716 /* Mips ABI requests the .dynamic section to be read only. */
5717 s = bfd_get_section_by_name (abfd, ".dynamic");
5718 if (s != NULL)
5719 {
5720 if (! bfd_set_section_flags (abfd, s, flags))
5721 return false;
5722 }
5723
5724 /* We need to create .got section. */
5725 if (! mips_elf_create_got_section (abfd, info))
5726 return false;
5727
5728 /* Create .stub section. */
5729 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5730 {
5731 s = bfd_make_section (abfd, ".stub");
5732 if (s == NULL
5733 || ! bfd_set_section_flags (abfd, s, flags)
5734 || ! bfd_set_section_alignment (abfd, s, 2))
5735 return false;
5736 }
5737
5738 if (SGI_COMPAT (abfd)
5739 && !info->shared
5740 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5741 {
5742 s = bfd_make_section (abfd, ".rld_map");
5743 if (s == NULL
5744 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5745 || ! bfd_set_section_alignment (abfd, s, 2))
5746 return false;
5747 }
5748
5749 if (SGI_COMPAT (abfd))
5750 {
5751 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5752 {
5753 h = NULL;
5754 if (! (_bfd_generic_link_add_one_symbol
5755 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5756 (bfd_vma) 0, (const char *) NULL, false,
5757 get_elf_backend_data (abfd)->collect,
5758 (struct bfd_link_hash_entry **) &h)))
5759 return false;
5760 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5761 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5762 h->type = STT_SECTION;
5763
5764 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5765 return false;
5766 }
5767
5768 /* We need to create a .compact_rel section. */
5769 if (! mips_elf_create_compact_rel_section (abfd, info))
5770 return false;
5771
5772 /* Change aligments of some sections. */
5773 s = bfd_get_section_by_name (abfd, ".hash");
5774 if (s != NULL)
5775 bfd_set_section_alignment (abfd, s, 4);
5776 s = bfd_get_section_by_name (abfd, ".dynsym");
5777 if (s != NULL)
5778 bfd_set_section_alignment (abfd, s, 4);
5779 s = bfd_get_section_by_name (abfd, ".dynstr");
5780 if (s != NULL)
5781 bfd_set_section_alignment (abfd, s, 4);
5782 s = bfd_get_section_by_name (abfd, ".reginfo");
5783 if (s != NULL)
5784 bfd_set_section_alignment (abfd, s, 4);
5785 s = bfd_get_section_by_name (abfd, ".dynamic");
5786 if (s != NULL)
5787 bfd_set_section_alignment (abfd, s, 4);
5788 }
5789
5790 if (!info->shared)
5791 {
5792 h = NULL;
5793 if (! (_bfd_generic_link_add_one_symbol
5794 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5795 (bfd_vma) 0, (const char *) NULL, false,
5796 get_elf_backend_data (abfd)->collect,
5797 (struct bfd_link_hash_entry **) &h)))
5798 return false;
5799 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5800 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5801 h->type = STT_SECTION;
5802
5803 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5804 return false;
5805
5806 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5807 {
5808 /* __rld_map is a four byte word located in the .data section
5809 and is filled in by the rtld to contain a pointer to
5810 the _r_debug structure. Its symbol value will be set in
5811 mips_elf_finish_dynamic_symbol. */
5812 s = bfd_get_section_by_name (abfd, ".rld_map");
5813 BFD_ASSERT (s != NULL);
5814
5815 h = NULL;
5816 if (! (_bfd_generic_link_add_one_symbol
5817 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5818 (bfd_vma) 0, (const char *) NULL, false,
5819 get_elf_backend_data (abfd)->collect,
5820 (struct bfd_link_hash_entry **) &h)))
5821 return false;
5822 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5823 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5824 h->type = STT_OBJECT;
5825
5826 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5827 return false;
5828 }
5829 }
5830
5831 return true;
5832 }
5833
5834 /* Create the .compact_rel section. */
5835
5836 static boolean
5837 mips_elf_create_compact_rel_section (abfd, info)
5838 bfd *abfd;
5839 struct bfd_link_info *info;
5840 {
5841 flagword flags;
5842 register asection *s;
5843
5844 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5845 {
5846 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5847 | SEC_READONLY);
5848
5849 s = bfd_make_section (abfd, ".compact_rel");
5850 if (s == NULL
5851 || ! bfd_set_section_flags (abfd, s, flags)
5852 || ! bfd_set_section_alignment (abfd, s, 2))
5853 return false;
5854
5855 s->_raw_size = sizeof (Elf32_External_compact_rel);
5856 }
5857
5858 return true;
5859 }
5860
5861 /* Create the .got section to hold the global offset table. */
5862
5863 static boolean
5864 mips_elf_create_got_section (abfd, info)
5865 bfd *abfd;
5866 struct bfd_link_info *info;
5867 {
5868 flagword flags;
5869 register asection *s;
5870 struct elf_link_hash_entry *h;
5871 struct mips_got_info *g;
5872
5873 /* This function may be called more than once. */
5874 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5875 return true;
5876
5877 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5878 | SEC_LINKER_CREATED);
5879
5880 s = bfd_make_section (abfd, ".got");
5881 if (s == NULL
5882 || ! bfd_set_section_flags (abfd, s, flags)
5883 || ! bfd_set_section_alignment (abfd, s, 4))
5884 return false;
5885
5886 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5887 linker script because we don't want to define the symbol if we
5888 are not creating a global offset table. */
5889 h = NULL;
5890 if (! (_bfd_generic_link_add_one_symbol
5891 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5892 (bfd_vma) 0, (const char *) NULL, false,
5893 get_elf_backend_data (abfd)->collect,
5894 (struct bfd_link_hash_entry **) &h)))
5895 return false;
5896 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5897 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5898 h->type = STT_OBJECT;
5899
5900 if (info->shared
5901 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5902 return false;
5903
5904 /* The first several global offset table entries are reserved. */
5905 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5906
5907 g = (struct mips_got_info *) bfd_alloc (abfd,
5908 sizeof (struct mips_got_info));
5909 if (g == NULL)
5910 return false;
5911 g->global_gotsym = 0;
5912 g->local_gotno = MIPS_RESERVED_GOTNO;
5913 g->assigned_gotno = MIPS_RESERVED_GOTNO;
5914 if (elf_section_data (s) == NULL)
5915 {
5916 s->used_by_bfd =
5917 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5918 if (elf_section_data (s) == NULL)
5919 return false;
5920 }
5921 elf_section_data (s)->tdata = (PTR) g;
5922
5923 return true;
5924 }
5925
5926 /* Look through the relocs for a section during the first phase, and
5927 allocate space in the global offset table. */
5928
5929 static boolean
5930 mips_elf_check_relocs (abfd, info, sec, relocs)
5931 bfd *abfd;
5932 struct bfd_link_info *info;
5933 asection *sec;
5934 const Elf_Internal_Rela *relocs;
5935 {
5936 const char *name;
5937 bfd *dynobj;
5938 Elf_Internal_Shdr *symtab_hdr;
5939 struct elf_link_hash_entry **sym_hashes;
5940 struct mips_got_info *g;
5941 size_t extsymoff;
5942 const Elf_Internal_Rela *rel;
5943 const Elf_Internal_Rela *rel_end;
5944 asection *sgot;
5945 asection *sreloc;
5946
5947 if (info->relocateable)
5948 return true;
5949
5950 dynobj = elf_hash_table (info)->dynobj;
5951 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5952 sym_hashes = elf_sym_hashes (abfd);
5953 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5954
5955 /* Check for the mips16 stub sections. */
5956
5957 name = bfd_get_section_name (abfd, sec);
5958 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5959 {
5960 unsigned long r_symndx;
5961
5962 /* Look at the relocation information to figure out which symbol
5963 this is for. */
5964
5965 r_symndx = ELF32_R_SYM (relocs->r_info);
5966
5967 if (r_symndx < extsymoff
5968 || sym_hashes[r_symndx - extsymoff] == NULL)
5969 {
5970 asection *o;
5971
5972 /* This stub is for a local symbol. This stub will only be
5973 needed if there is some relocation in this BFD, other
5974 than a 16 bit function call, which refers to this symbol. */
5975 for (o = abfd->sections; o != NULL; o = o->next)
5976 {
5977 Elf_Internal_Rela *sec_relocs;
5978 const Elf_Internal_Rela *r, *rend;
5979
5980 /* We can ignore stub sections when looking for relocs. */
5981 if ((o->flags & SEC_RELOC) == 0
5982 || o->reloc_count == 0
5983 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5984 sizeof FN_STUB - 1) == 0
5985 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5986 sizeof CALL_STUB - 1) == 0
5987 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5988 sizeof CALL_FP_STUB - 1) == 0)
5989 continue;
5990
5991 sec_relocs = (_bfd_elf32_link_read_relocs
5992 (abfd, o, (PTR) NULL,
5993 (Elf_Internal_Rela *) NULL,
5994 info->keep_memory));
5995 if (sec_relocs == NULL)
5996 return false;
5997
5998 rend = sec_relocs + o->reloc_count;
5999 for (r = sec_relocs; r < rend; r++)
6000 if (ELF32_R_SYM (r->r_info) == r_symndx
6001 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6002 break;
6003
6004 if (! info->keep_memory)
6005 free (sec_relocs);
6006
6007 if (r < rend)
6008 break;
6009 }
6010
6011 if (o == NULL)
6012 {
6013 /* There is no non-call reloc for this stub, so we do
6014 not need it. Since this function is called before
6015 the linker maps input sections to output sections, we
6016 can easily discard it by setting the SEC_EXCLUDE
6017 flag. */
6018 sec->flags |= SEC_EXCLUDE;
6019 return true;
6020 }
6021
6022 /* Record this stub in an array of local symbol stubs for
6023 this BFD. */
6024 if (elf_tdata (abfd)->local_stubs == NULL)
6025 {
6026 unsigned long symcount;
6027 asection **n;
6028
6029 if (elf_bad_symtab (abfd))
6030 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6031 else
6032 symcount = symtab_hdr->sh_info;
6033 n = (asection **) bfd_zalloc (abfd,
6034 symcount * sizeof (asection *));
6035 if (n == NULL)
6036 return false;
6037 elf_tdata (abfd)->local_stubs = n;
6038 }
6039
6040 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6041
6042 /* We don't need to set mips16_stubs_seen in this case.
6043 That flag is used to see whether we need to look through
6044 the global symbol table for stubs. We don't need to set
6045 it here, because we just have a local stub. */
6046 }
6047 else
6048 {
6049 struct mips_elf_link_hash_entry *h;
6050
6051 h = ((struct mips_elf_link_hash_entry *)
6052 sym_hashes[r_symndx - extsymoff]);
6053
6054 /* H is the symbol this stub is for. */
6055
6056 h->fn_stub = sec;
6057 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6058 }
6059 }
6060 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6061 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6062 {
6063 unsigned long r_symndx;
6064 struct mips_elf_link_hash_entry *h;
6065 asection **loc;
6066
6067 /* Look at the relocation information to figure out which symbol
6068 this is for. */
6069
6070 r_symndx = ELF32_R_SYM (relocs->r_info);
6071
6072 if (r_symndx < extsymoff
6073 || sym_hashes[r_symndx - extsymoff] == NULL)
6074 {
6075 /* This stub was actually built for a static symbol defined
6076 in the same file. We assume that all static symbols in
6077 mips16 code are themselves mips16, so we can simply
6078 discard this stub. Since this function is called before
6079 the linker maps input sections to output sections, we can
6080 easily discard it by setting the SEC_EXCLUDE flag. */
6081 sec->flags |= SEC_EXCLUDE;
6082 return true;
6083 }
6084
6085 h = ((struct mips_elf_link_hash_entry *)
6086 sym_hashes[r_symndx - extsymoff]);
6087
6088 /* H is the symbol this stub is for. */
6089
6090 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6091 loc = &h->call_fp_stub;
6092 else
6093 loc = &h->call_stub;
6094
6095 /* If we already have an appropriate stub for this function, we
6096 don't need another one, so we can discard this one. Since
6097 this function is called before the linker maps input sections
6098 to output sections, we can easily discard it by setting the
6099 SEC_EXCLUDE flag. We can also discard this section if we
6100 happen to already know that this is a mips16 function; it is
6101 not necessary to check this here, as it is checked later, but
6102 it is slightly faster to check now. */
6103 if (*loc != NULL || h->root.other == STO_MIPS16)
6104 {
6105 sec->flags |= SEC_EXCLUDE;
6106 return true;
6107 }
6108
6109 *loc = sec;
6110 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6111 }
6112
6113 if (dynobj == NULL)
6114 {
6115 sgot = NULL;
6116 g = NULL;
6117 }
6118 else
6119 {
6120 sgot = bfd_get_section_by_name (dynobj, ".got");
6121 if (sgot == NULL)
6122 g = NULL;
6123 else
6124 {
6125 BFD_ASSERT (elf_section_data (sgot) != NULL);
6126 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6127 BFD_ASSERT (g != NULL);
6128 }
6129 }
6130
6131 sreloc = NULL;
6132
6133 rel_end = relocs + sec->reloc_count;
6134 for (rel = relocs; rel < rel_end; rel++)
6135 {
6136 unsigned long r_symndx;
6137 struct elf_link_hash_entry *h;
6138
6139 r_symndx = ELF32_R_SYM (rel->r_info);
6140
6141 if (r_symndx < extsymoff)
6142 h = NULL;
6143 else
6144 {
6145 h = sym_hashes[r_symndx - extsymoff];
6146
6147 /* This may be an indirect symbol created because of a version. */
6148 if (h != NULL)
6149 {
6150 while (h->root.type == bfd_link_hash_indirect)
6151 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6152 }
6153 }
6154
6155 /* Some relocs require a global offset table. */
6156 if (dynobj == NULL || sgot == NULL)
6157 {
6158 switch (ELF32_R_TYPE (rel->r_info))
6159 {
6160 case R_MIPS_GOT16:
6161 case R_MIPS_CALL16:
6162 case R_MIPS_CALL_HI16:
6163 case R_MIPS_CALL_LO16:
6164 case R_MIPS_GOT_HI16:
6165 case R_MIPS_GOT_LO16:
6166 if (dynobj == NULL)
6167 elf_hash_table (info)->dynobj = dynobj = abfd;
6168 if (! mips_elf_create_got_section (dynobj, info))
6169 return false;
6170 sgot = bfd_get_section_by_name (dynobj, ".got");
6171 BFD_ASSERT (sgot != NULL);
6172 BFD_ASSERT (elf_section_data (sgot) != NULL);
6173 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6174 BFD_ASSERT (g != NULL);
6175 break;
6176
6177 case R_MIPS_32:
6178 case R_MIPS_REL32:
6179 if (dynobj == NULL
6180 && (info->shared || h != NULL)
6181 && (sec->flags & SEC_ALLOC) != 0)
6182 elf_hash_table (info)->dynobj = dynobj = abfd;
6183 break;
6184
6185 default:
6186 break;
6187 }
6188 }
6189
6190 switch (ELF32_R_TYPE (rel->r_info))
6191 {
6192 case R_MIPS_CALL16:
6193 case R_MIPS_CALL_HI16:
6194 case R_MIPS_CALL_LO16:
6195 /* This symbol requires a global offset table entry. */
6196
6197 if (h == NULL)
6198 {
6199 (*_bfd_error_handler)
6200 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6201 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6202 bfd_set_error (bfd_error_bad_value);
6203 return false;
6204 }
6205
6206 /* Make sure this symbol is output as a dynamic symbol. */
6207 if (h->dynindx == -1)
6208 {
6209 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6210 return false;
6211 }
6212
6213 if (h->got_offset != (bfd_vma) -1)
6214 {
6215 /* We have already allocated space in the .got. */
6216 break;
6217 }
6218
6219 /* Note the index of the first global got symbol in .dynsym. */
6220 if (g->global_gotsym == 0
6221 || g->global_gotsym > (unsigned long) h->dynindx)
6222 g->global_gotsym = h->dynindx;
6223
6224 /* Make this symbol to have the corresponding got entry. */
6225 h->got_offset = 0;
6226
6227 /* We need a stub, not a plt entry for the undefined
6228 function. But we record it as if it needs plt. See
6229 elf_adjust_dynamic_symbol in elflink.h. */
6230 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6231 h->type = STT_FUNC;
6232
6233 break;
6234
6235 case R_MIPS_GOT16:
6236 case R_MIPS_GOT_HI16:
6237 case R_MIPS_GOT_LO16:
6238 /* This symbol requires a global offset table entry. */
6239
6240 if (h != NULL)
6241 {
6242 /* Make sure this symbol is output as a dynamic symbol. */
6243 if (h->dynindx == -1)
6244 {
6245 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6246 return false;
6247 }
6248
6249 if (h->got_offset != (bfd_vma) -1)
6250 {
6251 /* We have already allocated space in the .got. */
6252 break;
6253 }
6254 /* Note the index of the first global got symbol in
6255 .dynsym. */
6256 if (g->global_gotsym == 0
6257 || g->global_gotsym > (unsigned long) h->dynindx)
6258 g->global_gotsym = h->dynindx;
6259
6260 /* Make this symbol to be the global got symbol. */
6261 h->got_offset = 0;
6262 }
6263
6264 break;
6265
6266 case R_MIPS_32:
6267 case R_MIPS_REL32:
6268 if ((info->shared || h != NULL)
6269 && (sec->flags & SEC_ALLOC) != 0)
6270 {
6271 if (sreloc == NULL)
6272 {
6273 const char *name = ".rel.dyn";
6274
6275 sreloc = bfd_get_section_by_name (dynobj, name);
6276 if (sreloc == NULL)
6277 {
6278 sreloc = bfd_make_section (dynobj, name);
6279 if (sreloc == NULL
6280 || ! bfd_set_section_flags (dynobj, sreloc,
6281 (SEC_ALLOC
6282 | SEC_LOAD
6283 | SEC_HAS_CONTENTS
6284 | SEC_IN_MEMORY
6285 | SEC_LINKER_CREATED
6286 | SEC_READONLY))
6287 || ! bfd_set_section_alignment (dynobj, sreloc,
6288 4))
6289 return false;
6290 }
6291 }
6292 if (info->shared)
6293 {
6294 /* When creating a shared object, we must copy these
6295 reloc types into the output file as R_MIPS_REL32
6296 relocs. We make room for this reloc in the
6297 .rel.dyn reloc section */
6298 if (sreloc->_raw_size == 0)
6299 {
6300 /* Add a null element. */
6301 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6302 ++sreloc->reloc_count;
6303 }
6304 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6305 }
6306 else
6307 {
6308 struct mips_elf_link_hash_entry *hmips;
6309
6310 /* We only need to copy this reloc if the symbol is
6311 defined in a dynamic object. */
6312 hmips = (struct mips_elf_link_hash_entry *) h;
6313 ++hmips->mips_32_relocs;
6314 }
6315 }
6316
6317 if (SGI_COMPAT (abfd))
6318 mips_elf_hash_table (info)->compact_rel_size +=
6319 sizeof (Elf32_External_crinfo);
6320
6321 break;
6322
6323 case R_MIPS_26:
6324 case R_MIPS_GPREL16:
6325 case R_MIPS_LITERAL:
6326 case R_MIPS_GPREL32:
6327 if (SGI_COMPAT (abfd))
6328 mips_elf_hash_table (info)->compact_rel_size +=
6329 sizeof (Elf32_External_crinfo);
6330 break;
6331
6332 default:
6333 break;
6334 }
6335
6336 /* If this reloc is not a 16 bit call, and it has a global
6337 symbol, then we will need the fn_stub if there is one.
6338 References from a stub section do not count. */
6339 if (h != NULL
6340 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6341 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6342 sizeof FN_STUB - 1) != 0
6343 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6344 sizeof CALL_STUB - 1) != 0
6345 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6346 sizeof CALL_FP_STUB - 1) != 0)
6347 {
6348 struct mips_elf_link_hash_entry *mh;
6349
6350 mh = (struct mips_elf_link_hash_entry *) h;
6351 mh->need_fn_stub = true;
6352 }
6353 }
6354
6355 return true;
6356 }
6357
6358 /* Adjust a symbol defined by a dynamic object and referenced by a
6359 regular object. The current definition is in some section of the
6360 dynamic object, but we're not including those sections. We have to
6361 change the definition to something the rest of the link can
6362 understand. */
6363
6364 static boolean
6365 mips_elf_adjust_dynamic_symbol (info, h)
6366 struct bfd_link_info *info;
6367 struct elf_link_hash_entry *h;
6368 {
6369 bfd *dynobj;
6370 struct mips_elf_link_hash_entry *hmips;
6371 asection *s;
6372
6373 dynobj = elf_hash_table (info)->dynobj;
6374
6375 /* Make sure we know what is going on here. */
6376 BFD_ASSERT (dynobj != NULL
6377 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6378 || h->weakdef != NULL
6379 || ((h->elf_link_hash_flags
6380 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6381 && (h->elf_link_hash_flags
6382 & ELF_LINK_HASH_REF_REGULAR) != 0
6383 && (h->elf_link_hash_flags
6384 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6385
6386 /* If this symbol is defined in a dynamic object, we need to copy
6387 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6388 file. */
6389 hmips = (struct mips_elf_link_hash_entry *) h;
6390 if (! info->relocateable
6391 && hmips->mips_32_relocs != 0
6392 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6393 {
6394 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6395 BFD_ASSERT (s != NULL);
6396
6397 if (s->_raw_size == 0)
6398 {
6399 /* Make room for a null element. */
6400 s->_raw_size += sizeof (Elf32_External_Rel);
6401 ++s->reloc_count;
6402 }
6403 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6404 }
6405
6406 /* For a function, create a stub, if needed. */
6407 if (h->type == STT_FUNC
6408 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6409 {
6410 if (! elf_hash_table (info)->dynamic_sections_created)
6411 return true;
6412
6413 /* If this symbol is not defined in a regular file, then set
6414 the symbol to the stub location. This is required to make
6415 function pointers compare as equal between the normal
6416 executable and the shared library. */
6417 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6418 {
6419 /* We need .stub section. */
6420 s = bfd_get_section_by_name (dynobj, ".stub");
6421 BFD_ASSERT (s != NULL);
6422
6423 h->root.u.def.section = s;
6424 h->root.u.def.value = s->_raw_size;
6425
6426 /* XXX Write this stub address somewhere. */
6427 h->plt_offset = s->_raw_size;
6428
6429 /* Make room for this stub code. */
6430 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6431
6432 /* The last half word of the stub will be filled with the index
6433 of this symbol in .dynsym section. */
6434 return true;
6435 }
6436 }
6437
6438 /* If this is a weak symbol, and there is a real definition, the
6439 processor independent code will have arranged for us to see the
6440 real definition first, and we can just use the same value. */
6441 if (h->weakdef != NULL)
6442 {
6443 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6444 || h->weakdef->root.type == bfd_link_hash_defweak);
6445 h->root.u.def.section = h->weakdef->root.u.def.section;
6446 h->root.u.def.value = h->weakdef->root.u.def.value;
6447 return true;
6448 }
6449
6450 /* This is a reference to a symbol defined by a dynamic object which
6451 is not a function. */
6452
6453 return true;
6454 }
6455
6456 /* This function is called after all the input files have been read,
6457 and the input sections have been assigned to output sections. We
6458 check for any mips16 stub sections that we can discard. */
6459
6460 static boolean mips_elf_check_mips16_stubs
6461 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6462
6463 static boolean
6464 mips_elf_always_size_sections (output_bfd, info)
6465 bfd *output_bfd;
6466 struct bfd_link_info *info;
6467 {
6468 if (info->relocateable
6469 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6470 return true;
6471
6472 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6473 mips_elf_check_mips16_stubs,
6474 (PTR) NULL);
6475
6476 return true;
6477 }
6478
6479 /* Check the mips16 stubs for a particular symbol, and see if we can
6480 discard them. */
6481
6482 /*ARGSUSED*/
6483 static boolean
6484 mips_elf_check_mips16_stubs (h, data)
6485 struct mips_elf_link_hash_entry *h;
6486 PTR data;
6487 {
6488 if (h->fn_stub != NULL
6489 && ! h->need_fn_stub)
6490 {
6491 /* We don't need the fn_stub; the only references to this symbol
6492 are 16 bit calls. Clobber the size to 0 to prevent it from
6493 being included in the link. */
6494 h->fn_stub->_raw_size = 0;
6495 h->fn_stub->_cooked_size = 0;
6496 h->fn_stub->flags &= ~ SEC_RELOC;
6497 h->fn_stub->reloc_count = 0;
6498 h->fn_stub->flags |= SEC_EXCLUDE;
6499 }
6500
6501 if (h->call_stub != NULL
6502 && h->root.other == STO_MIPS16)
6503 {
6504 /* We don't need the call_stub; this is a 16 bit function, so
6505 calls from other 16 bit functions are OK. Clobber the size
6506 to 0 to prevent it from being included in the link. */
6507 h->call_stub->_raw_size = 0;
6508 h->call_stub->_cooked_size = 0;
6509 h->call_stub->flags &= ~ SEC_RELOC;
6510 h->call_stub->reloc_count = 0;
6511 h->call_stub->flags |= SEC_EXCLUDE;
6512 }
6513
6514 if (h->call_fp_stub != NULL
6515 && h->root.other == STO_MIPS16)
6516 {
6517 /* We don't need the call_stub; this is a 16 bit function, so
6518 calls from other 16 bit functions are OK. Clobber the size
6519 to 0 to prevent it from being included in the link. */
6520 h->call_fp_stub->_raw_size = 0;
6521 h->call_fp_stub->_cooked_size = 0;
6522 h->call_fp_stub->flags &= ~ SEC_RELOC;
6523 h->call_fp_stub->reloc_count = 0;
6524 h->call_fp_stub->flags |= SEC_EXCLUDE;
6525 }
6526
6527 return true;
6528 }
6529
6530 /* Set the sizes of the dynamic sections. */
6531
6532 static boolean
6533 mips_elf_size_dynamic_sections (output_bfd, info)
6534 bfd *output_bfd;
6535 struct bfd_link_info *info;
6536 {
6537 bfd *dynobj;
6538 asection *s;
6539 boolean reltext;
6540 asection *sgot;
6541 struct mips_got_info *g;
6542
6543 dynobj = elf_hash_table (info)->dynobj;
6544 BFD_ASSERT (dynobj != NULL);
6545
6546 if (elf_hash_table (info)->dynamic_sections_created)
6547 {
6548 /* Set the contents of the .interp section to the interpreter. */
6549 if (! info->shared)
6550 {
6551 s = bfd_get_section_by_name (dynobj, ".interp");
6552 BFD_ASSERT (s != NULL);
6553 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6554 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6555 }
6556 }
6557
6558 /* Recompute the size of .got for local entires (reserved and
6559 hipages) if needed. To estimate it, get the upper bound of total
6560 size of loadable sections. */
6561 sgot = bfd_get_section_by_name (dynobj, ".got");
6562
6563 if (sgot != NULL)
6564 {
6565 bfd_size_type loadable_size = 0;
6566 bfd_size_type local_gotno;
6567 struct _bfd *sub;
6568
6569 BFD_ASSERT (elf_section_data (sgot) != NULL);
6570 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6571 BFD_ASSERT (g != NULL);
6572
6573 for (sub = info->input_bfds; sub; sub = sub->link_next)
6574 for (s = sub->sections; s != NULL; s = s->next)
6575 {
6576 if ((s->flags & SEC_ALLOC) == 0)
6577 continue;
6578 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6579 }
6580
6581 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6582
6583 /* Assume there are two loadable segments consisting of
6584 contiguous sections. Is 5 enough? */
6585 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6586 g->local_gotno = local_gotno;
6587 sgot->_raw_size += local_gotno * 4;
6588 }
6589
6590 /* The check_relocs and adjust_dynamic_symbol entry points have
6591 determined the sizes of the various dynamic sections. Allocate
6592 memory for them. */
6593 reltext = false;
6594 for (s = dynobj->sections; s != NULL; s = s->next)
6595 {
6596 const char *name;
6597 boolean strip;
6598
6599 /* It's OK to base decisions on the section name, because none
6600 of the dynobj section names depend upon the input files. */
6601 name = bfd_get_section_name (dynobj, s);
6602
6603 if ((s->flags & SEC_LINKER_CREATED) == 0)
6604 continue;
6605
6606 strip = false;
6607
6608 if (strncmp (name, ".rel", 4) == 0)
6609 {
6610 if (s->_raw_size == 0)
6611 strip = true;
6612 else
6613 {
6614 const char *outname;
6615 asection *target;
6616
6617 /* If this relocation section applies to a read only
6618 section, then we probably need a DT_TEXTREL entry.
6619 If the relocation section is .rel.dyn, we always
6620 assert a DT_TEXTREL entry rather than testing whether
6621 there exists a relocation to a read only section or
6622 not. */
6623 outname = bfd_get_section_name (output_bfd,
6624 s->output_section);
6625 target = bfd_get_section_by_name (output_bfd, outname + 4);
6626 if ((target != NULL
6627 && (target->flags & SEC_READONLY) != 0
6628 && (target->flags & SEC_ALLOC) != 0)
6629 || strcmp (outname, ".rel.dyn") == 0)
6630 reltext = true;
6631
6632 /* We use the reloc_count field as a counter if we need
6633 to copy relocs into the output file. */
6634 if (strcmp (name, ".rel.dyn") != 0)
6635 s->reloc_count = 0;
6636 }
6637 }
6638 else if (strncmp (name, ".got", 4) == 0)
6639 {
6640 int i;
6641
6642 BFD_ASSERT (elf_section_data (s) != NULL);
6643 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6644 BFD_ASSERT (g != NULL);
6645
6646 /* Fix the size of .got section for the correspondence of
6647 global symbols and got entries. This adds some useless
6648 got entries. Is this required by ABI really? */
6649 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6650 s->_raw_size += i * 4;
6651 }
6652 else if (strncmp (name, ".stub", 5) == 0)
6653 {
6654 /* Irix rld assumes that the function stub isn't at the end
6655 of .text section. So put a dummy. XXX */
6656 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6657 }
6658 else if (! info->shared
6659 && ! mips_elf_hash_table (info)->use_rld_obj_head
6660 && strncmp (name, ".rld_map", 8) == 0)
6661 {
6662 /* We add a room for __rld_map. It will be filled in by the
6663 rtld to contain a pointer to the _r_debug structure. */
6664 s->_raw_size += 4;
6665 }
6666 else if (SGI_COMPAT (output_bfd)
6667 && strncmp (name, ".compact_rel", 12) == 0)
6668 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6669 else if (strncmp (name, ".init", 5) != 0)
6670 {
6671 /* It's not one of our sections, so don't allocate space. */
6672 continue;
6673 }
6674
6675 if (strip)
6676 {
6677 asection **spp;
6678
6679 for (spp = &s->output_section->owner->sections;
6680 *spp != s->output_section;
6681 spp = &(*spp)->next)
6682 ;
6683 *spp = s->output_section->next;
6684 --s->output_section->owner->section_count;
6685
6686 continue;
6687 }
6688
6689 /* Allocate memory for the section contents. */
6690 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6691 if (s->contents == NULL && s->_raw_size != 0)
6692 {
6693 bfd_set_error (bfd_error_no_memory);
6694 return false;
6695 }
6696 memset (s->contents, 0, s->_raw_size);
6697 }
6698
6699 if (elf_hash_table (info)->dynamic_sections_created)
6700 {
6701 /* Add some entries to the .dynamic section. We fill in the
6702 values later, in elf_mips_finish_dynamic_sections, but we
6703 must add the entries now so that we get the correct size for
6704 the .dynamic section. The DT_DEBUG entry is filled in by the
6705 dynamic linker and used by the debugger. */
6706 if (! info->shared)
6707 {
6708 if (SGI_COMPAT (output_bfd))
6709 {
6710 /* SGI object has the equivalence of DT_DEBUG in the
6711 DT_MIPS_RLD_MAP entry. */
6712 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6713 return false;
6714 }
6715 else
6716 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6717 return false;
6718 }
6719
6720 if (reltext)
6721 {
6722 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6723 return false;
6724 }
6725
6726 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6727 return false;
6728
6729 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6730 {
6731 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6732 return false;
6733
6734 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6735 return false;
6736
6737 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6738 return false;
6739 }
6740
6741 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6742 return false;
6743
6744 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6745 return false;
6746
6747 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6748 {
6749 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6750 return false;
6751
6752 s = bfd_get_section_by_name (dynobj, ".liblist");
6753 BFD_ASSERT (s != NULL);
6754
6755 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6756 return false;
6757 }
6758
6759 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6760 return false;
6761
6762 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6763 return false;
6764
6765 #if 0
6766 /* Time stamps in executable files are a bad idea. */
6767 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6768 return false;
6769 #endif
6770
6771 #if 0 /* FIXME */
6772 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6773 return false;
6774 #endif
6775
6776 #if 0 /* FIXME */
6777 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6778 return false;
6779 #endif
6780
6781 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6782 return false;
6783
6784 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6785 return false;
6786
6787 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6788 return false;
6789
6790 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6791 return false;
6792
6793 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6794 return false;
6795
6796 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6797 return false;
6798
6799 #if 0 /* (SGI_COMPAT) */
6800 if (! bfd_get_section_by_name (dynobj, ".init"))
6801 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6802 return false;
6803
6804 if (! bfd_get_section_by_name (dynobj, ".fini"))
6805 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6806 return false;
6807 #endif
6808 }
6809
6810 /* If we use dynamic linking, we generate a section symbol for each
6811 output section. These are local symbols, which means that they
6812 must come first in the dynamic symbol table.
6813 That means we must increment the dynamic symbol index of every
6814 other dynamic symbol. */
6815 {
6816 const char * const *namep;
6817 unsigned int c, i;
6818 bfd_size_type strindex;
6819 struct bfd_strtab_hash *dynstr;
6820 struct mips_got_info *g;
6821
6822 c = 0;
6823 if (elf_hash_table (info)->dynamic_sections_created)
6824 {
6825 if (SGI_COMPAT (output_bfd))
6826 {
6827 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
6828 elf_link_hash_traverse (elf_hash_table (info),
6829 mips_elf_adjust_dynindx,
6830 (PTR) &c);
6831 elf_hash_table (info)->dynsymcount += c;
6832
6833 dynstr = elf_hash_table (info)->dynstr;
6834 BFD_ASSERT (dynstr != NULL);
6835
6836 for (i = 1, namep = mips_elf_dynsym_sec_names;
6837 *namep != NULL;
6838 i++, namep++)
6839 {
6840 s = bfd_get_section_by_name (output_bfd, *namep);
6841 if (s != NULL)
6842 elf_section_data (s)->dynindx = i;
6843
6844 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
6845 if (strindex == (bfd_size_type) -1)
6846 return false;
6847
6848 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
6849 }
6850 }
6851 else
6852 {
6853 c = bfd_count_sections (output_bfd);
6854 elf_link_hash_traverse (elf_hash_table (info),
6855 mips_elf_adjust_dynindx,
6856 (PTR) &c);
6857 elf_hash_table (info)->dynsymcount += c;
6858
6859 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
6860 {
6861 elf_section_data (s)->dynindx = i;
6862 /* These symbols will have no names, so we don't need to
6863 fiddle with dynstr_index. */
6864 }
6865 }
6866 }
6867
6868 if (sgot != NULL)
6869 {
6870 BFD_ASSERT (elf_section_data (sgot) != NULL);
6871 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6872 BFD_ASSERT (g != NULL);
6873
6874 /* If there are no global got symbols, fake the last symbol so
6875 for safety. */
6876 if (g->global_gotsym)
6877 g->global_gotsym += c;
6878 else
6879 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
6880 }
6881 }
6882
6883 return true;
6884 }
6885
6886 /* Increment the index of a dynamic symbol by a given amount. Called
6887 via elf_link_hash_traverse. */
6888
6889 static boolean
6890 mips_elf_adjust_dynindx (h, cparg)
6891 struct elf_link_hash_entry *h;
6892 PTR cparg;
6893 {
6894 unsigned int *cp = (unsigned int *) cparg;
6895
6896 if (h->dynindx != -1)
6897 h->dynindx += *cp;
6898 return true;
6899 }
6900
6901 /* Finish up dynamic symbol handling. We set the contents of various
6902 dynamic sections here. */
6903
6904 static boolean
6905 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6906 bfd *output_bfd;
6907 struct bfd_link_info *info;
6908 struct elf_link_hash_entry *h;
6909 Elf_Internal_Sym *sym;
6910 {
6911 bfd *dynobj;
6912 bfd_vma gval;
6913 asection *sgot;
6914 struct mips_got_info *g;
6915 const char *name;
6916
6917 dynobj = elf_hash_table (info)->dynobj;
6918 gval = sym->st_value;
6919
6920 if (h->plt_offset != (bfd_vma) -1)
6921 {
6922 asection *s;
6923 bfd_byte *p;
6924 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
6925
6926 /* This symbol has a stub. Set it up. */
6927
6928 BFD_ASSERT (h->dynindx != -1);
6929
6930 s = bfd_get_section_by_name (dynobj, ".stub");
6931 BFD_ASSERT (s != NULL);
6932
6933 /* Fill the stub. */
6934 p = stub;
6935 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
6936 p += 4;
6937 bfd_put_32 (output_bfd, STUB_MOVE, p);
6938 p += 4;
6939
6940 /* FIXME: Can h->dynindex be more than 64K? */
6941 if (h->dynindx & 0xffff0000)
6942 return false;
6943
6944 bfd_put_32 (output_bfd, STUB_JALR, p);
6945 p += 4;
6946 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
6947
6948 BFD_ASSERT (h->plt_offset <= s->_raw_size);
6949 memcpy (s->contents + h->plt_offset, stub, MIPS_FUNCTION_STUB_SIZE);
6950
6951 /* Mark the symbol as undefined. plt_offset != -1 occurs
6952 only for the referenced symbol. */
6953 sym->st_shndx = SHN_UNDEF;
6954
6955 /* The run-time linker uses the st_value field of the symbol
6956 to reset the global offset table entry for this external
6957 to its stub address when unlinking a shared object. */
6958 gval = s->output_section->vma + s->output_offset + h->plt_offset;
6959 sym->st_value = gval;
6960 }
6961
6962 BFD_ASSERT (h->dynindx != -1);
6963
6964 sgot = bfd_get_section_by_name (dynobj, ".got");
6965 BFD_ASSERT (sgot != NULL);
6966 BFD_ASSERT (elf_section_data (sgot) != NULL);
6967 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6968 BFD_ASSERT (g != NULL);
6969
6970 if ((unsigned long) h->dynindx >= g->global_gotsym)
6971 {
6972 bfd_size_type offset;
6973
6974 /* This symbol has an entry in the global offset table. Set its
6975 value to the corresponding got entry, if needed. */
6976 if (h->got_offset == (bfd_vma) -1)
6977 {
6978 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
6979 BFD_ASSERT (g->local_gotno * 4 <= offset
6980 && offset < sgot->_raw_size);
6981 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
6982 }
6983 }
6984
6985 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
6986 name = h->root.root.string;
6987 if (strcmp (name, "_DYNAMIC") == 0
6988 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
6989 sym->st_shndx = SHN_ABS;
6990 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
6991 {
6992 sym->st_shndx = SHN_ABS;
6993 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6994 sym->st_value = 1;
6995 }
6996 else if (SGI_COMPAT (output_bfd))
6997 {
6998 if (strcmp (name, "_gp_disp") == 0)
6999 {
7000 sym->st_shndx = SHN_ABS;
7001 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7002 sym->st_value = elf_gp (output_bfd);
7003 }
7004 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7005 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7006 {
7007 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7008 sym->st_other = STO_PROTECTED;
7009 sym->st_value = 0;
7010 sym->st_shndx = SHN_MIPS_DATA;
7011 }
7012 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7013 {
7014 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7015 sym->st_other = STO_PROTECTED;
7016 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7017 sym->st_shndx = SHN_ABS;
7018 }
7019 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7020 {
7021 if (h->type == STT_FUNC)
7022 sym->st_shndx = SHN_MIPS_TEXT;
7023 else if (h->type == STT_OBJECT)
7024 sym->st_shndx = SHN_MIPS_DATA;
7025 }
7026 }
7027
7028 if (SGI_COMPAT (output_bfd)
7029 && ! info->shared)
7030 {
7031 if (! mips_elf_hash_table (info)->use_rld_obj_head
7032 && strcmp (name, "__rld_map") == 0)
7033 {
7034 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7035 BFD_ASSERT (s != NULL);
7036 sym->st_value = s->output_section->vma + s->output_offset;
7037 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7038 if (mips_elf_hash_table (info)->rld_value == 0)
7039 mips_elf_hash_table (info)->rld_value = sym->st_value;
7040 }
7041 else if (mips_elf_hash_table (info)->use_rld_obj_head
7042 && strcmp (name, "__rld_obj_head") == 0)
7043 {
7044 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7045 BFD_ASSERT (s != NULL);
7046 mips_elf_hash_table (info)->rld_value = sym->st_value;
7047 }
7048 }
7049
7050 /* If this is a mips16 symbol, force the value to be even. */
7051 if (sym->st_other == STO_MIPS16
7052 && (sym->st_value & 1) != 0)
7053 --sym->st_value;
7054
7055 return true;
7056 }
7057
7058 /* Finish up the dynamic sections. */
7059
7060 static boolean
7061 mips_elf_finish_dynamic_sections (output_bfd, info)
7062 bfd *output_bfd;
7063 struct bfd_link_info *info;
7064 {
7065 bfd *dynobj;
7066 asection *sdyn;
7067 asection *sgot;
7068 struct mips_got_info *g;
7069
7070 dynobj = elf_hash_table (info)->dynobj;
7071
7072 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7073
7074 sgot = bfd_get_section_by_name (dynobj, ".got");
7075 if (sgot == NULL)
7076 g = NULL;
7077 else
7078 {
7079 BFD_ASSERT (elf_section_data (sgot) != NULL);
7080 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7081 BFD_ASSERT (g != NULL);
7082 }
7083
7084 if (elf_hash_table (info)->dynamic_sections_created)
7085 {
7086 Elf32_External_Dyn *dyncon, *dynconend;
7087
7088 BFD_ASSERT (sdyn != NULL);
7089 BFD_ASSERT (g != NULL);
7090
7091 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7092 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7093 for (; dyncon < dynconend; dyncon++)
7094 {
7095 Elf_Internal_Dyn dyn;
7096 const char *name;
7097 size_t elemsize;
7098 asection *s;
7099
7100 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7101
7102 switch (dyn.d_tag)
7103 {
7104 default:
7105 break;
7106
7107 case DT_RELENT:
7108 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7109 BFD_ASSERT (s != NULL);
7110 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
7111 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7112 break;
7113
7114 case DT_STRSZ:
7115 /* Rewrite DT_STRSZ. */
7116 dyn.d_un.d_val =
7117 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7118 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7119 break;
7120
7121 case DT_PLTGOT:
7122 name = ".got";
7123 goto get_vma;
7124 case DT_MIPS_CONFLICT:
7125 name = ".conflict";
7126 goto get_vma;
7127 case DT_MIPS_LIBLIST:
7128 name = ".liblist";
7129 get_vma:
7130 s = bfd_get_section_by_name (output_bfd, name);
7131 BFD_ASSERT (s != NULL);
7132 dyn.d_un.d_ptr = s->vma;
7133 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7134 break;
7135
7136 case DT_MIPS_RLD_VERSION:
7137 dyn.d_un.d_val = 1; /* XXX */
7138 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7139 break;
7140
7141 case DT_MIPS_FLAGS:
7142 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7143 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7144 break;
7145
7146 case DT_MIPS_CONFLICTNO:
7147 name = ".conflict";
7148 elemsize = sizeof (Elf32_Conflict);
7149 goto set_elemno;
7150
7151 case DT_MIPS_LIBLISTNO:
7152 name = ".liblist";
7153 elemsize = sizeof (Elf32_Lib);
7154 set_elemno:
7155 s = bfd_get_section_by_name (output_bfd, name);
7156 if (s != NULL)
7157 {
7158 if (s->_cooked_size != 0)
7159 dyn.d_un.d_val = s->_cooked_size / elemsize;
7160 else
7161 dyn.d_un.d_val = s->_raw_size / elemsize;
7162 }
7163 else
7164 dyn.d_un.d_val = 0;
7165
7166 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7167 break;
7168
7169 case DT_MIPS_TIME_STAMP:
7170 time ((time_t *) &dyn.d_un.d_val);
7171 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7172 break;
7173
7174 case DT_MIPS_ICHECKSUM:
7175 /* XXX FIXME: */
7176 break;
7177
7178 case DT_MIPS_IVERSION:
7179 /* XXX FIXME: */
7180 break;
7181
7182 case DT_MIPS_BASE_ADDRESS:
7183 s = output_bfd->sections;
7184 BFD_ASSERT (s != NULL);
7185 dyn.d_un.d_ptr = s->vma & ~(0xffff);
7186 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7187 break;
7188
7189 case DT_MIPS_LOCAL_GOTNO:
7190 dyn.d_un.d_val = g->local_gotno;
7191 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7192 break;
7193
7194 case DT_MIPS_SYMTABNO:
7195 name = ".dynsym";
7196 elemsize = sizeof (Elf32_External_Sym);
7197 s = bfd_get_section_by_name (output_bfd, name);
7198 BFD_ASSERT (s != NULL);
7199
7200 if (s->_cooked_size != 0)
7201 dyn.d_un.d_val = s->_cooked_size / elemsize;
7202 else
7203 dyn.d_un.d_val = s->_raw_size / elemsize;
7204 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7205 break;
7206
7207 case DT_MIPS_UNREFEXTNO:
7208 /* XXX FIXME: */
7209 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
7210 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7211 break;
7212
7213 case DT_MIPS_GOTSYM:
7214 dyn.d_un.d_val = g->global_gotsym;
7215 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7216 break;
7217
7218 case DT_MIPS_HIPAGENO:
7219 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7220 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7221 break;
7222
7223 case DT_MIPS_RLD_MAP:
7224 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7225 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7226 break;
7227
7228 }
7229 }
7230 }
7231
7232 /* The first entry of the global offset table will be filled at
7233 runtime. The second entry will be used by some runtime loaders.
7234 This isn't the case of Irix rld. */
7235 if (sgot != NULL && sgot->_raw_size > 0)
7236 {
7237 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7238 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
7239 }
7240
7241 if (sgot != NULL)
7242 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7243
7244 {
7245 asection *sdynsym;
7246 asection *s;
7247 unsigned int i;
7248 bfd_vma last;
7249 Elf_Internal_Sym sym;
7250 long dindx;
7251 const char *name;
7252 const char * const * namep = mips_elf_dynsym_sec_names;
7253 Elf32_compact_rel cpt;
7254
7255 /* Set up the section symbols for the output sections. SGI sets
7256 the STT_NOTYPE attribute for these symbols. Should we do so? */
7257
7258 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
7259 if (sdynsym != NULL)
7260 {
7261 if (SGI_COMPAT (output_bfd))
7262 {
7263 sym.st_size = 0;
7264 sym.st_name = 0;
7265 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7266 sym.st_other = 0;
7267
7268 i = 0;
7269 last = 0;
7270 dindx = 0;
7271 while ((name = *namep++) != NULL)
7272 {
7273 s = bfd_get_section_by_name (output_bfd, name);
7274 if (s != NULL)
7275 {
7276 sym.st_value = s->vma;
7277 dindx = elf_section_data (s)->dynindx;
7278 last = s->vma + s->_raw_size;
7279 }
7280 else
7281 {
7282 sym.st_value = last;
7283 dindx++;
7284 }
7285
7286 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7287 ? SHN_MIPS_TEXT
7288 : SHN_MIPS_DATA);
7289 ++i;
7290 sym.st_name =
7291 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7292
7293 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7294 (((Elf32_External_Sym *)
7295 sdynsym->contents)
7296 + dindx));
7297 }
7298
7299 /* Set the sh_info field of the output .dynsym section to
7300 the index of the first global symbol. */
7301 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7302 SIZEOF_MIPS_DYNSYM_SECNAMES;
7303 }
7304 else
7305 {
7306 sym.st_size = 0;
7307 sym.st_name = 0;
7308 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7309 sym.st_other = 0;
7310
7311 for (s = output_bfd->sections; s != NULL; s = s->next)
7312 {
7313 int indx;
7314
7315 sym.st_value = s->vma;
7316
7317 indx = elf_section_data (s)->this_idx;
7318 BFD_ASSERT (indx > 0);
7319 sym.st_shndx = indx;
7320
7321 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7322 (((Elf32_External_Sym *)
7323 sdynsym->contents)
7324 + elf_section_data (s)->dynindx));
7325 }
7326
7327 /* Set the sh_info field of the output .dynsym section to
7328 the index of the first global symbol. */
7329 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7330 bfd_count_sections (output_bfd) + 1;
7331 }
7332 }
7333
7334 if (SGI_COMPAT (output_bfd))
7335 {
7336 /* Write .compact_rel section out. */
7337 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7338 if (s != NULL)
7339 {
7340 cpt.id1 = 1;
7341 cpt.num = s->reloc_count;
7342 cpt.id2 = 2;
7343 cpt.offset = (s->output_section->filepos
7344 + sizeof (Elf32_External_compact_rel));
7345 cpt.reserved0 = 0;
7346 cpt.reserved1 = 0;
7347 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7348 ((Elf32_External_compact_rel *)
7349 s->contents));
7350
7351 /* Clean up a dummy stub function entry in .text. */
7352 s = bfd_get_section_by_name (dynobj, ".stub");
7353 if (s != NULL)
7354 {
7355 file_ptr dummy_offset;
7356
7357 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7358 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7359 memset (s->contents + dummy_offset, 0,
7360 MIPS_FUNCTION_STUB_SIZE);
7361 }
7362 }
7363 }
7364
7365 /* Clean up a first relocation in .rel.dyn. */
7366 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7367 if (s != NULL && s->_raw_size > 0)
7368 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7369 }
7370
7371 return true;
7372 }
7373 \f
7374 /* This is almost identical to bfd_generic_get_... except that some
7375 MIPS relocations need to be handled specially. Sigh. */
7376
7377 static bfd_byte *
7378 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7379 relocateable, symbols)
7380 bfd *abfd;
7381 struct bfd_link_info *link_info;
7382 struct bfd_link_order *link_order;
7383 bfd_byte *data;
7384 boolean relocateable;
7385 asymbol **symbols;
7386 {
7387 /* Get enough memory to hold the stuff */
7388 bfd *input_bfd = link_order->u.indirect.section->owner;
7389 asection *input_section = link_order->u.indirect.section;
7390
7391 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7392 arelent **reloc_vector = NULL;
7393 long reloc_count;
7394
7395 if (reloc_size < 0)
7396 goto error_return;
7397
7398 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7399 if (reloc_vector == NULL && reloc_size != 0)
7400 goto error_return;
7401
7402 /* read in the section */
7403 if (!bfd_get_section_contents (input_bfd,
7404 input_section,
7405 (PTR) data,
7406 0,
7407 input_section->_raw_size))
7408 goto error_return;
7409
7410 /* We're not relaxing the section, so just copy the size info */
7411 input_section->_cooked_size = input_section->_raw_size;
7412 input_section->reloc_done = true;
7413
7414 reloc_count = bfd_canonicalize_reloc (input_bfd,
7415 input_section,
7416 reloc_vector,
7417 symbols);
7418 if (reloc_count < 0)
7419 goto error_return;
7420
7421 if (reloc_count > 0)
7422 {
7423 arelent **parent;
7424 /* for mips */
7425 int gp_found;
7426 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7427
7428 {
7429 struct bfd_hash_entry *h;
7430 struct bfd_link_hash_entry *lh;
7431 /* Skip all this stuff if we aren't mixing formats. */
7432 if (abfd && input_bfd
7433 && abfd->xvec == input_bfd->xvec)
7434 lh = 0;
7435 else
7436 {
7437 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7438 lh = (struct bfd_link_hash_entry *) h;
7439 }
7440 lookup:
7441 if (lh)
7442 {
7443 switch (lh->type)
7444 {
7445 case bfd_link_hash_undefined:
7446 case bfd_link_hash_undefweak:
7447 case bfd_link_hash_common:
7448 gp_found = 0;
7449 break;
7450 case bfd_link_hash_defined:
7451 case bfd_link_hash_defweak:
7452 gp_found = 1;
7453 gp = lh->u.def.value;
7454 break;
7455 case bfd_link_hash_indirect:
7456 case bfd_link_hash_warning:
7457 lh = lh->u.i.link;
7458 /* @@FIXME ignoring warning for now */
7459 goto lookup;
7460 case bfd_link_hash_new:
7461 default:
7462 abort ();
7463 }
7464 }
7465 else
7466 gp_found = 0;
7467 }
7468 /* end mips */
7469 for (parent = reloc_vector; *parent != (arelent *) NULL;
7470 parent++)
7471 {
7472 char *error_message = (char *) NULL;
7473 bfd_reloc_status_type r;
7474
7475 /* Specific to MIPS: Deal with relocation types that require
7476 knowing the gp of the output bfd. */
7477 asymbol *sym = *(*parent)->sym_ptr_ptr;
7478 if (bfd_is_abs_section (sym->section) && abfd)
7479 {
7480 /* The special_function wouldn't get called anyways. */
7481 }
7482 else if (!gp_found)
7483 {
7484 /* The gp isn't there; let the special function code
7485 fall over on its own. */
7486 }
7487 else if ((*parent)->howto->special_function
7488 == _bfd_mips_elf_gprel16_reloc)
7489 {
7490 /* bypass special_function call */
7491 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7492 relocateable, (PTR) data, gp);
7493 goto skip_bfd_perform_relocation;
7494 }
7495 /* end mips specific stuff */
7496
7497 r = bfd_perform_relocation (input_bfd,
7498 *parent,
7499 (PTR) data,
7500 input_section,
7501 relocateable ? abfd : (bfd *) NULL,
7502 &error_message);
7503 skip_bfd_perform_relocation:
7504
7505 if (relocateable)
7506 {
7507 asection *os = input_section->output_section;
7508
7509 /* A partial link, so keep the relocs */
7510 os->orelocation[os->reloc_count] = *parent;
7511 os->reloc_count++;
7512 }
7513
7514 if (r != bfd_reloc_ok)
7515 {
7516 switch (r)
7517 {
7518 case bfd_reloc_undefined:
7519 if (!((*link_info->callbacks->undefined_symbol)
7520 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7521 input_bfd, input_section, (*parent)->address)))
7522 goto error_return;
7523 break;
7524 case bfd_reloc_dangerous:
7525 BFD_ASSERT (error_message != (char *) NULL);
7526 if (!((*link_info->callbacks->reloc_dangerous)
7527 (link_info, error_message, input_bfd, input_section,
7528 (*parent)->address)))
7529 goto error_return;
7530 break;
7531 case bfd_reloc_overflow:
7532 if (!((*link_info->callbacks->reloc_overflow)
7533 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7534 (*parent)->howto->name, (*parent)->addend,
7535 input_bfd, input_section, (*parent)->address)))
7536 goto error_return;
7537 break;
7538 case bfd_reloc_outofrange:
7539 default:
7540 abort ();
7541 break;
7542 }
7543
7544 }
7545 }
7546 }
7547 if (reloc_vector != NULL)
7548 free (reloc_vector);
7549 return data;
7550
7551 error_return:
7552 if (reloc_vector != NULL)
7553 free (reloc_vector);
7554 return NULL;
7555 }
7556 #define bfd_elf32_bfd_get_relocated_section_contents \
7557 elf32_mips_get_relocated_section_contents
7558 \f
7559 /* ECOFF swapping routines. These are used when dealing with the
7560 .mdebug section, which is in the ECOFF debugging format. */
7561 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7562 {
7563 /* Symbol table magic number. */
7564 magicSym,
7565 /* Alignment of debugging information. E.g., 4. */
7566 4,
7567 /* Sizes of external symbolic information. */
7568 sizeof (struct hdr_ext),
7569 sizeof (struct dnr_ext),
7570 sizeof (struct pdr_ext),
7571 sizeof (struct sym_ext),
7572 sizeof (struct opt_ext),
7573 sizeof (struct fdr_ext),
7574 sizeof (struct rfd_ext),
7575 sizeof (struct ext_ext),
7576 /* Functions to swap in external symbolic data. */
7577 ecoff_swap_hdr_in,
7578 ecoff_swap_dnr_in,
7579 ecoff_swap_pdr_in,
7580 ecoff_swap_sym_in,
7581 ecoff_swap_opt_in,
7582 ecoff_swap_fdr_in,
7583 ecoff_swap_rfd_in,
7584 ecoff_swap_ext_in,
7585 _bfd_ecoff_swap_tir_in,
7586 _bfd_ecoff_swap_rndx_in,
7587 /* Functions to swap out external symbolic data. */
7588 ecoff_swap_hdr_out,
7589 ecoff_swap_dnr_out,
7590 ecoff_swap_pdr_out,
7591 ecoff_swap_sym_out,
7592 ecoff_swap_opt_out,
7593 ecoff_swap_fdr_out,
7594 ecoff_swap_rfd_out,
7595 ecoff_swap_ext_out,
7596 _bfd_ecoff_swap_tir_out,
7597 _bfd_ecoff_swap_rndx_out,
7598 /* Function to read in symbolic data. */
7599 _bfd_mips_elf_read_ecoff_info
7600 };
7601 \f
7602 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
7603 #define TARGET_LITTLE_NAME "elf32-littlemips"
7604 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
7605 #define TARGET_BIG_NAME "elf32-bigmips"
7606 #define ELF_ARCH bfd_arch_mips
7607 #define ELF_MACHINE_CODE EM_MIPS
7608
7609 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7610 a value of 0x1000, and we are compatible. */
7611 #define ELF_MAXPAGESIZE 0x1000
7612
7613 #define elf_backend_collect true
7614 #define elf_backend_type_change_ok true
7615 #define elf_info_to_howto 0
7616 #define elf_info_to_howto_rel mips_info_to_howto_rel
7617 #define elf_backend_sym_is_global mips_elf_sym_is_global
7618 #define elf_backend_object_p mips_elf32_object_p
7619 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
7620 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
7621 #define elf_backend_section_from_bfd_section \
7622 _bfd_mips_elf_section_from_bfd_section
7623 #define elf_backend_section_processing mips_elf32_section_processing
7624 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
7625 #define elf_backend_additional_program_headers \
7626 mips_elf_additional_program_headers
7627 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
7628 #define elf_backend_final_write_processing \
7629 _bfd_mips_elf_final_write_processing
7630 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
7631
7632 #define bfd_elf32_bfd_is_local_label_name \
7633 mips_elf_is_local_label_name
7634 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
7635 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
7636 #define bfd_elf32_bfd_link_hash_table_create \
7637 mips_elf_link_hash_table_create
7638 #define bfd_elf32_bfd_final_link mips_elf_final_link
7639 #define bfd_elf32_bfd_copy_private_bfd_data \
7640 _bfd_mips_elf_copy_private_bfd_data
7641 #define bfd_elf32_bfd_merge_private_bfd_data \
7642 _bfd_mips_elf_merge_private_bfd_data
7643 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7644 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
7645 #define elf_backend_create_dynamic_sections \
7646 mips_elf_create_dynamic_sections
7647 #define elf_backend_check_relocs mips_elf_check_relocs
7648 #define elf_backend_adjust_dynamic_symbol \
7649 mips_elf_adjust_dynamic_symbol
7650 #define elf_backend_always_size_sections \
7651 mips_elf_always_size_sections
7652 #define elf_backend_size_dynamic_sections \
7653 mips_elf_size_dynamic_sections
7654 #define elf_backend_relocate_section mips_elf_relocate_section
7655 #define elf_backend_link_output_symbol_hook \
7656 mips_elf_link_output_symbol_hook
7657 #define elf_backend_finish_dynamic_symbol \
7658 mips_elf_finish_dynamic_symbol
7659 #define elf_backend_finish_dynamic_sections \
7660 mips_elf_finish_dynamic_sections
7661
7662 #include "elf32-target.h"
This page took 0.193219 seconds and 4 git commands to generate.