* opncls.c (bfd_openstreamr): New function.
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27 #include "genlink.h"
28 #include "libelf.h"
29 #include "elf/mips.h"
30
31 /* Get the ECOFF swapping routines. */
32 #include "coff/sym.h"
33 #include "coff/symconst.h"
34 #include "coff/internal.h"
35 #include "coff/ecoff.h"
36 #include "coff/mips.h"
37 #define ECOFF_32
38 #include "ecoffswap.h"
39
40 static bfd_reloc_status_type mips_elf_hi16_reloc PARAMS ((bfd *abfd,
41 arelent *reloc,
42 asymbol *symbol,
43 PTR data,
44 asection *section,
45 bfd *output_bfd,
46 char **error));
47 static bfd_reloc_status_type mips_elf_got16_reloc PARAMS ((bfd *abfd,
48 arelent *reloc,
49 asymbol *symbol,
50 PTR data,
51 asection *section,
52 bfd *output_bfd,
53 char **error));
54 static bfd_reloc_status_type mips_elf_lo16_reloc PARAMS ((bfd *abfd,
55 arelent *reloc,
56 asymbol *symbol,
57 PTR data,
58 asection *section,
59 bfd *output_bfd,
60 char **error));
61 static bfd_reloc_status_type mips_elf_gprel16_reloc PARAMS ((bfd *abfd,
62 arelent *reloc,
63 asymbol *symbol,
64 PTR data,
65 asection *section,
66 bfd *output_bfd,
67 char **error));
68 static const struct reloc_howto_struct *bfd_elf32_bfd_reloc_type_lookup
69 PARAMS ((bfd *, bfd_reloc_code_real_type));
70 static void mips_info_to_howto_rel
71 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
72 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
73 static boolean mips_elf_object_p PARAMS ((bfd *));
74 static void mips_elf_final_write_processing
75 PARAMS ((bfd *, boolean));
76 static boolean mips_elf_section_from_shdr
77 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
78 static boolean mips_elf_fake_sections
79 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
80 static boolean mips_elf_section_from_bfd_section
81 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
82 static boolean mips_elf_section_processing
83 PARAMS ((bfd *, Elf32_Internal_Shdr *));
84 static void mips_elf_symbol_processing PARAMS ((bfd *, asymbol *));
85 static boolean mips_elf_read_ecoff_info
86 PARAMS ((bfd *, asection *, struct ecoff_debug_info *));
87 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
88 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
89 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
90 PARAMS ((bfd *));
91 static int gptab_compare PARAMS ((const void *, const void *));
92 static boolean mips_elf_final_link
93 PARAMS ((bfd *, struct bfd_link_info *));
94 static void mips_elf_relocate_hi16
95 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
96 bfd_vma));
97 static boolean mips_elf_relocate_section
98 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
99 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
100 static boolean mips_elf_add_symbol_hook
101 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
102 const char **, flagword *, asection **, bfd_vma *));
103
104 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
105
106 enum reloc_type
107 {
108 R_MIPS_NONE = 0,
109 R_MIPS_16, R_MIPS_32,
110 R_MIPS_REL32, R_MIPS_26,
111 R_MIPS_HI16, R_MIPS_LO16,
112 R_MIPS_GPREL16, R_MIPS_LITERAL,
113 R_MIPS_GOT16, R_MIPS_PC16,
114 R_MIPS_CALL16, R_MIPS_GPREL32,
115 /* The remaining relocs are defined on Irix, although they are not
116 in the MIPS ELF ABI. */
117 R_MIPS_UNUSED1, R_MIPS_UNUSED2,
118 R_MIPS_UNUSED3,
119 R_MIPS_SHIFT5, R_MIPS_SHIFT6,
120 R_MIPS_64, R_MIPS_GOT_DISP,
121 R_MIPS_GOT_PAGE, R_MIPS_GOT_OFST,
122 R_MIPS_GOT_HI16, R_MIPS_GOT_LO16,
123 R_MIPS_SUB, R_MIPS_INSERT_A,
124 R_MIPS_INSERT_B, R_MIPS_DELETE,
125 R_MIPS_HIGHER, R_MIPS_HIGHEST,
126 R_MIPS_CALL_HI16, R_MIPS_CALL_LO16,
127 R_MIPS_max
128 };
129
130 static reloc_howto_type elf_mips_howto_table[] =
131 {
132 /* No relocation. */
133 HOWTO (R_MIPS_NONE, /* type */
134 0, /* rightshift */
135 0, /* size (0 = byte, 1 = short, 2 = long) */
136 0, /* bitsize */
137 false, /* pc_relative */
138 0, /* bitpos */
139 complain_overflow_dont, /* complain_on_overflow */
140 bfd_elf_generic_reloc, /* special_function */
141 "R_MIPS_NONE", /* name */
142 false, /* partial_inplace */
143 0, /* src_mask */
144 0, /* dst_mask */
145 false), /* pcrel_offset */
146
147 /* 16 bit relocation. */
148 HOWTO (R_MIPS_16, /* type */
149 0, /* rightshift */
150 1, /* size (0 = byte, 1 = short, 2 = long) */
151 16, /* bitsize */
152 false, /* pc_relative */
153 0, /* bitpos */
154 complain_overflow_bitfield, /* complain_on_overflow */
155 bfd_elf_generic_reloc, /* special_function */
156 "R_MIPS_16", /* name */
157 true, /* partial_inplace */
158 0xffff, /* src_mask */
159 0xffff, /* dst_mask */
160 false), /* pcrel_offset */
161
162 /* 32 bit relocation. */
163 HOWTO (R_MIPS_32, /* type */
164 0, /* rightshift */
165 2, /* size (0 = byte, 1 = short, 2 = long) */
166 32, /* bitsize */
167 false, /* pc_relative */
168 0, /* bitpos */
169 complain_overflow_bitfield, /* complain_on_overflow */
170 bfd_elf_generic_reloc, /* special_function */
171 "R_MIPS_32", /* name */
172 true, /* partial_inplace */
173 0xffffffff, /* src_mask */
174 0xffffffff, /* dst_mask */
175 false), /* pcrel_offset */
176
177 /* 32 bit symbol relative relocation. */
178 HOWTO (R_MIPS_REL32, /* type */
179 0, /* rightshift */
180 2, /* size (0 = byte, 1 = short, 2 = long) */
181 32, /* bitsize */
182 false, /* pc_relative */
183 0, /* bitpos */
184 complain_overflow_bitfield, /* complain_on_overflow */
185 bfd_elf_generic_reloc, /* special_function */
186 "R_MIPS_REL32", /* name */
187 true, /* partial_inplace */
188 0xffffffff, /* src_mask */
189 0xffffffff, /* dst_mask */
190 false), /* pcrel_offset */
191
192 /* 26 bit branch address. */
193 HOWTO (R_MIPS_26, /* type */
194 2, /* rightshift */
195 2, /* size (0 = byte, 1 = short, 2 = long) */
196 26, /* bitsize */
197 false, /* pc_relative */
198 0, /* bitpos */
199 complain_overflow_dont, /* complain_on_overflow */
200 /* This needs complex overflow
201 detection, because the upper four
202 bits must match the PC. */
203 bfd_elf_generic_reloc, /* special_function */
204 "R_MIPS_26", /* name */
205 true, /* partial_inplace */
206 0x3ffffff, /* src_mask */
207 0x3ffffff, /* dst_mask */
208 false), /* pcrel_offset */
209
210 /* High 16 bits of symbol value. */
211 HOWTO (R_MIPS_HI16, /* type */
212 0, /* rightshift */
213 2, /* size (0 = byte, 1 = short, 2 = long) */
214 16, /* bitsize */
215 false, /* pc_relative */
216 0, /* bitpos */
217 complain_overflow_dont, /* complain_on_overflow */
218 mips_elf_hi16_reloc, /* special_function */
219 "R_MIPS_HI16", /* name */
220 true, /* partial_inplace */
221 0xffff, /* src_mask */
222 0xffff, /* dst_mask */
223 false), /* pcrel_offset */
224
225 /* Low 16 bits of symbol value. */
226 HOWTO (R_MIPS_LO16, /* type */
227 0, /* rightshift */
228 2, /* size (0 = byte, 1 = short, 2 = long) */
229 16, /* bitsize */
230 false, /* pc_relative */
231 0, /* bitpos */
232 complain_overflow_dont, /* complain_on_overflow */
233 mips_elf_lo16_reloc, /* special_function */
234 "R_MIPS_LO16", /* name */
235 true, /* partial_inplace */
236 0xffff, /* src_mask */
237 0xffff, /* dst_mask */
238 false), /* pcrel_offset */
239
240 /* GP relative reference. */
241 HOWTO (R_MIPS_GPREL16, /* type */
242 0, /* rightshift */
243 2, /* size (0 = byte, 1 = short, 2 = long) */
244 16, /* bitsize */
245 false, /* pc_relative */
246 0, /* bitpos */
247 complain_overflow_signed, /* complain_on_overflow */
248 mips_elf_gprel16_reloc, /* special_function */
249 "R_MIPS_GPREL16", /* name */
250 true, /* partial_inplace */
251 0xffff, /* src_mask */
252 0xffff, /* dst_mask */
253 false), /* pcrel_offset */
254
255 /* Reference to literal section. */
256 HOWTO (R_MIPS_LITERAL, /* type */
257 0, /* rightshift */
258 2, /* size (0 = byte, 1 = short, 2 = long) */
259 16, /* bitsize */
260 false, /* pc_relative */
261 0, /* bitpos */
262 complain_overflow_signed, /* complain_on_overflow */
263 mips_elf_gprel16_reloc, /* special_function */
264 "R_MIPS_LITERAL", /* name */
265 true, /* partial_inplace */
266 0xffff, /* src_mask */
267 0xffff, /* dst_mask */
268 false), /* pcrel_offset */
269
270 /* Reference to global offset table. */
271 /* FIXME: This is not handled correctly. */
272 HOWTO (R_MIPS_GOT16, /* type */
273 0, /* rightshift */
274 2, /* size (0 = byte, 1 = short, 2 = long) */
275 16, /* bitsize */
276 false, /* pc_relative */
277 0, /* bitpos */
278 complain_overflow_signed, /* complain_on_overflow */
279 mips_elf_got16_reloc, /* special_function */
280 "R_MIPS_GOT16", /* name */
281 false, /* partial_inplace */
282 0, /* src_mask */
283 0xffff, /* dst_mask */
284 false), /* pcrel_offset */
285
286 /* 16 bit PC relative reference. */
287 HOWTO (R_MIPS_PC16, /* type */
288 0, /* rightshift */
289 2, /* size (0 = byte, 1 = short, 2 = long) */
290 16, /* bitsize */
291 true, /* pc_relative */
292 0, /* bitpos */
293 complain_overflow_signed, /* complain_on_overflow */
294 bfd_elf_generic_reloc, /* special_function */
295 "R_MIPS_PC16", /* name */
296 true, /* partial_inplace */
297 0xffff, /* src_mask */
298 0xffff, /* dst_mask */
299 false), /* pcrel_offset */
300
301 /* 16 bit call through global offset table. */
302 /* FIXME: This is not handled correctly. */
303 HOWTO (R_MIPS_CALL16, /* type */
304 0, /* rightshift */
305 2, /* size (0 = byte, 1 = short, 2 = long) */
306 16, /* bitsize */
307 false, /* pc_relative */
308 0, /* bitpos */
309 complain_overflow_signed, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_MIPS_CALL16", /* name */
312 false, /* partial_inplace */
313 0, /* src_mask */
314 0xffff, /* dst_mask */
315 false), /* pcrel_offset */
316
317 /* 32 bit GP relative reference. */
318 /* FIXME: This is not handled correctly. */
319 HOWTO (R_MIPS_GPREL32, /* type */
320 0, /* rightshift */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
322 32, /* bitsize */
323 false, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* special_function */
327 "R_MIPS_GPREL32", /* name */
328 true, /* partial_inplace */
329 0xffffffff, /* src_mask */
330 0xffffffff, /* dst_mask */
331 false), /* pcrel_offset */
332
333 /* The remaining relocs are defined on Irix 5, although they are
334 not defined by the ABI. */
335 { 13 },
336 { 14 },
337 { 15 },
338
339 /* A 5 bit shift field. */
340 HOWTO (R_MIPS_SHIFT5, /* type */
341 0, /* rightshift */
342 2, /* size (0 = byte, 1 = short, 2 = long) */
343 5, /* bitsize */
344 false, /* pc_relative */
345 6, /* bitpos */
346 complain_overflow_bitfield, /* complain_on_overflow */
347 bfd_elf_generic_reloc, /* special_function */
348 "R_MIPS_SHIFT5", /* name */
349 true, /* partial_inplace */
350 0x000007c0, /* src_mask */
351 0x000007c0, /* dst_mask */
352 false), /* pcrel_offset */
353
354 /* A 6 bit shift field. */
355 /* FIXME: This is not handled correctly; a special function is
356 needed to put the most significant bit in the right place. */
357 HOWTO (R_MIPS_SHIFT6, /* type */
358 0, /* rightshift */
359 2, /* size (0 = byte, 1 = short, 2 = long) */
360 6, /* bitsize */
361 false, /* pc_relative */
362 6, /* bitpos */
363 complain_overflow_bitfield, /* complain_on_overflow */
364 bfd_elf_generic_reloc, /* special_function */
365 "R_MIPS_SHIFT6", /* name */
366 true, /* partial_inplace */
367 0x000007c4, /* src_mask */
368 0x000007c4, /* dst_mask */
369 false), /* pcrel_offset */
370
371 /* A 64 bit relocation. Presumably not used in 32 bit ELF. */
372 { R_MIPS_64 },
373
374 /* Displacement in the global offset table. */
375 /* FIXME: Not handled correctly. */
376 HOWTO (R_MIPS_GOT_DISP, /* type */
377 0, /* rightshift */
378 2, /* size (0 = byte, 1 = short, 2 = long) */
379 16, /* 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_GOT_DISP", /* name */
385 true, /* partial_inplace */
386 0x0000ffff, /* src_mask */
387 0x0000ffff, /* dst_mask */
388 false), /* pcrel_offset */
389
390 /* Displacement to page pointer in the global offset table. */
391 /* FIXME: Not handled correctly. */
392 HOWTO (R_MIPS_GOT_PAGE, /* type */
393 0, /* rightshift */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
395 16, /* bitsize */
396 false, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_bitfield, /* complain_on_overflow */
399 bfd_elf_generic_reloc, /* special_function */
400 "R_MIPS_GOT_PAGE", /* name */
401 true, /* partial_inplace */
402 0x0000ffff, /* src_mask */
403 0x0000ffff, /* dst_mask */
404 false), /* pcrel_offset */
405
406 /* Offset from page pointer in the global offset table. */
407 /* FIXME: Not handled correctly. */
408 HOWTO (R_MIPS_GOT_OFST, /* type */
409 0, /* rightshift */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
411 16, /* bitsize */
412 false, /* pc_relative */
413 0, /* bitpos */
414 complain_overflow_bitfield, /* complain_on_overflow */
415 bfd_elf_generic_reloc, /* special_function */
416 "R_MIPS_GOT_OFST", /* name */
417 true, /* partial_inplace */
418 0x0000ffff, /* src_mask */
419 0x0000ffff, /* dst_mask */
420 false), /* pcrel_offset */
421
422 /* High 16 bits of displacement in global offset table. */
423 /* FIXME: Not handled correctly. */
424 HOWTO (R_MIPS_GOT_HI16, /* 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_elf_generic_reloc, /* special_function */
432 "R_MIPS_GOT_HI16", /* name */
433 true, /* partial_inplace */
434 0x0000ffff, /* src_mask */
435 0x0000ffff, /* dst_mask */
436 false), /* pcrel_offset */
437
438 /* Low 16 bits of displacement in global offset table. */
439 /* FIXME: Not handled correctly. */
440 HOWTO (R_MIPS_GOT_LO16, /* type */
441 0, /* rightshift */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
443 16, /* bitsize */
444 false, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_dont, /* complain_on_overflow */
447 bfd_elf_generic_reloc, /* special_function */
448 "R_MIPS_GOT_LO16", /* name */
449 true, /* partial_inplace */
450 0x0000ffff, /* src_mask */
451 0x0000ffff, /* dst_mask */
452 false), /* pcrel_offset */
453
454 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
455 { R_MIPS_SUB },
456
457 /* Used to cause the linker to insert and delete instructions? */
458 { R_MIPS_INSERT_A },
459 { R_MIPS_INSERT_B },
460 { R_MIPS_DELETE },
461
462 /* Get the higher values of a 64 bit addend. Presumably not used in
463 32 bit ELF. */
464 { R_MIPS_HIGHER },
465 { R_MIPS_HIGHEST },
466
467 /* High 16 bits of displacement in global offset table. */
468 /* FIXME: Not handled correctly. */
469 HOWTO (R_MIPS_CALL_HI16, /* 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_dont, /* complain_on_overflow */
476 bfd_elf_generic_reloc, /* special_function */
477 "R_MIPS_CALL_HI16", /* name */
478 true, /* partial_inplace */
479 0x0000ffff, /* src_mask */
480 0x0000ffff, /* dst_mask */
481 false), /* pcrel_offset */
482
483 /* Low 16 bits of displacement in global offset table. */
484 /* FIXME: Not handled correctly. */
485 HOWTO (R_MIPS_CALL_LO16, /* type */
486 0, /* rightshift */
487 2, /* size (0 = byte, 1 = short, 2 = long) */
488 16, /* bitsize */
489 false, /* pc_relative */
490 0, /* bitpos */
491 complain_overflow_dont, /* complain_on_overflow */
492 bfd_elf_generic_reloc, /* special_function */
493 "R_MIPS_CALL_LO16", /* name */
494 true, /* partial_inplace */
495 0x0000ffff, /* src_mask */
496 0x0000ffff, /* dst_mask */
497 false) /* pcrel_offset */
498 };
499
500 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
501 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
502 the HI16. Here we just save the information we need; we do the
503 actual relocation when we see the LO16. MIPS ELF requires that the
504 LO16 immediately follow the HI16, so this ought to work. */
505
506 static bfd_byte *mips_hi16_addr;
507 static bfd_vma mips_hi16_addend;
508
509 static bfd_reloc_status_type
510 mips_elf_hi16_reloc (abfd,
511 reloc_entry,
512 symbol,
513 data,
514 input_section,
515 output_bfd,
516 error_message)
517 bfd *abfd;
518 arelent *reloc_entry;
519 asymbol *symbol;
520 PTR data;
521 asection *input_section;
522 bfd *output_bfd;
523 char **error_message;
524 {
525 bfd_reloc_status_type ret;
526 bfd_vma relocation;
527
528 /* If we're relocating, and this an external symbol, we don't want
529 to change anything. */
530 if (output_bfd != (bfd *) NULL
531 && (symbol->flags & BSF_SECTION_SYM) == 0
532 && reloc_entry->addend == 0)
533 {
534 reloc_entry->address += input_section->output_offset;
535 return bfd_reloc_ok;
536 }
537
538 /* FIXME: The symbol _gp_disp requires special handling, which we do
539 not do. */
540 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
541 abort ();
542
543 ret = bfd_reloc_ok;
544 if (bfd_is_und_section (symbol->section)
545 && output_bfd == (bfd *) NULL)
546 ret = bfd_reloc_undefined;
547
548 if (bfd_is_com_section (symbol->section))
549 relocation = 0;
550 else
551 relocation = symbol->value;
552
553 relocation += symbol->section->output_section->vma;
554 relocation += symbol->section->output_offset;
555 relocation += reloc_entry->addend;
556
557 if (reloc_entry->address > input_section->_cooked_size)
558 return bfd_reloc_outofrange;
559
560 /* Save the information, and let LO16 do the actual relocation. */
561 mips_hi16_addr = (bfd_byte *) data + reloc_entry->address;
562 mips_hi16_addend = relocation;
563
564 if (output_bfd != (bfd *) NULL)
565 reloc_entry->address += input_section->output_offset;
566
567 return ret;
568 }
569
570 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
571 inplace relocation; this function exists in order to do the
572 R_MIPS_HI16 relocation described above. */
573
574 static bfd_reloc_status_type
575 mips_elf_lo16_reloc (abfd,
576 reloc_entry,
577 symbol,
578 data,
579 input_section,
580 output_bfd,
581 error_message)
582 bfd *abfd;
583 arelent *reloc_entry;
584 asymbol *symbol;
585 PTR data;
586 asection *input_section;
587 bfd *output_bfd;
588 char **error_message;
589 {
590 /* FIXME: The symbol _gp_disp requires special handling, which we do
591 not do. */
592 if (output_bfd == (bfd *) NULL
593 && strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
594 abort ();
595
596 if (mips_hi16_addr != (bfd_byte *) NULL)
597 {
598 unsigned long insn;
599 unsigned long val;
600 unsigned long vallo;
601
602 /* Do the HI16 relocation. Note that we actually don't need to
603 know anything about the LO16 itself, except where to find the
604 low 16 bits of the addend needed by the LO16. */
605 insn = bfd_get_32 (abfd, mips_hi16_addr);
606 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
607 & 0xffff);
608 val = ((insn & 0xffff) << 16) + vallo;
609 val += mips_hi16_addend;
610
611 /* The low order 16 bits are always treated as a signed value.
612 Therefore, a negative value in the low order bits requires an
613 adjustment in the high order bits. We need to make this
614 adjustment in two ways: once for the bits we took from the
615 data, and once for the bits we are putting back in to the
616 data. */
617 if ((vallo & 0x8000) != 0)
618 val -= 0x10000;
619 if ((val & 0x8000) != 0)
620 val += 0x10000;
621
622 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
623 bfd_put_32 (abfd, insn, mips_hi16_addr);
624
625 mips_hi16_addr = (bfd_byte *) NULL;
626 }
627
628 /* Now do the LO16 reloc in the usual way. */
629 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
630 input_section, output_bfd, error_message);
631 }
632
633 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
634 table used for PIC code. If the symbol is an external symbol, the
635 instruction is modified to contain the offset of the appropriate
636 entry in the global offset table. If the symbol is a section
637 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
638 addends are combined to form the real addend against the section
639 symbol; the GOT16 is modified to contain the offset of an entry in
640 the global offset table, and the LO16 is modified to offset it
641 appropriately. Thus an offset larger than 16 bits requires a
642 modified value in the global offset table.
643
644 This implementation suffices for the assembler, but the linker does
645 not yet know how to create global offset tables. */
646
647 static bfd_reloc_status_type
648 mips_elf_got16_reloc (abfd,
649 reloc_entry,
650 symbol,
651 data,
652 input_section,
653 output_bfd,
654 error_message)
655 bfd *abfd;
656 arelent *reloc_entry;
657 asymbol *symbol;
658 PTR data;
659 asection *input_section;
660 bfd *output_bfd;
661 char **error_message;
662 {
663 /* If we're relocating, and this an external symbol, we don't want
664 to change anything. */
665 if (output_bfd != (bfd *) NULL
666 && (symbol->flags & BSF_SECTION_SYM) == 0
667 && reloc_entry->addend == 0)
668 {
669 reloc_entry->address += input_section->output_offset;
670 return bfd_reloc_ok;
671 }
672
673 /* If we're relocating, and this is a local symbol, we can handle it
674 just like HI16. */
675 if (output_bfd != (bfd *) NULL
676 && (symbol->flags & BSF_SECTION_SYM) != 0)
677 return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
678 input_section, output_bfd, error_message);
679
680 abort ();
681 }
682
683 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
684 become the offset from the gp register. This function also handles
685 R_MIPS_LITERAL relocations, although those can be handled more
686 cleverly because the entries in the .lit8 and .lit4 sections can be
687 merged. */
688
689 static bfd_reloc_status_type
690 mips_elf_gprel16_reloc (abfd,
691 reloc_entry,
692 symbol,
693 data,
694 input_section,
695 output_bfd,
696 error_message)
697 bfd *abfd;
698 arelent *reloc_entry;
699 asymbol *symbol;
700 PTR data;
701 asection *input_section;
702 bfd *output_bfd;
703 char **error_message;
704 {
705 boolean relocateable;
706 bfd_vma relocation;
707 unsigned long val;
708 unsigned long insn;
709
710 /* If we're relocating, and this is an external symbol with no
711 addend, we don't want to change anything. We will only have an
712 addend if this is a newly created reloc, not read from an ELF
713 file. */
714 if (output_bfd != (bfd *) NULL
715 && (symbol->flags & BSF_SECTION_SYM) == 0
716 && reloc_entry->addend == 0)
717 {
718 reloc_entry->address += input_section->output_offset;
719 return bfd_reloc_ok;
720 }
721
722 if (output_bfd != (bfd *) NULL)
723 relocateable = true;
724 else
725 {
726 relocateable = false;
727 output_bfd = symbol->section->output_section->owner;
728 }
729
730 if (bfd_is_und_section (symbol->section)
731 && relocateable == false)
732 return bfd_reloc_undefined;
733
734 /* We have to figure out the gp value, so that we can adjust the
735 symbol value correctly. We look up the symbol _gp in the output
736 BFD. If we can't find it, we're stuck. We cache it in the ELF
737 target data. We don't need to adjust the symbol value for an
738 external symbol if we are producing relocateable output. */
739 if (elf_gp (output_bfd) == 0
740 && (relocateable == false
741 || (symbol->flags & BSF_SECTION_SYM) != 0))
742 {
743 if (relocateable != false)
744 {
745 /* Make up a value. */
746 elf_gp (output_bfd) =
747 symbol->section->output_section->vma + 0x4000;
748 }
749 else
750 {
751 unsigned int count;
752 asymbol **sym;
753 unsigned int i;
754
755 count = bfd_get_symcount (output_bfd);
756 sym = bfd_get_outsymbols (output_bfd);
757
758 if (sym == (asymbol **) NULL)
759 i = count;
760 else
761 {
762 for (i = 0; i < count; i++, sym++)
763 {
764 register CONST char *name;
765
766 name = bfd_asymbol_name (*sym);
767 if (*name == '_' && strcmp (name, "_gp") == 0)
768 {
769 elf_gp (output_bfd) = bfd_asymbol_value (*sym);
770 break;
771 }
772 }
773 }
774
775 if (i >= count)
776 {
777 /* Only get the error once. */
778 elf_gp (output_bfd) = 4;
779 *error_message =
780 (char *) "GP relative relocation when _gp not defined";
781 return bfd_reloc_dangerous;
782 }
783 }
784 }
785
786 if (bfd_is_com_section (symbol->section))
787 relocation = 0;
788 else
789 relocation = symbol->value;
790
791 relocation += symbol->section->output_section->vma;
792 relocation += symbol->section->output_offset;
793
794 if (reloc_entry->address > input_section->_cooked_size)
795 return bfd_reloc_outofrange;
796
797 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
798
799 /* Set val to the offset into the section or symbol. */
800 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
801 if (val & 0x8000)
802 val -= 0x10000;
803
804 /* Adjust val for the final section location and GP value. If we
805 are producing relocateable output, we don't want to do this for
806 an external symbol. */
807 if (relocateable == false
808 || (symbol->flags & BSF_SECTION_SYM) != 0)
809 val += relocation - elf_gp (output_bfd);
810
811 insn = (insn &~ 0xffff) | (val & 0xffff);
812 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
813
814 if (relocateable != false)
815 reloc_entry->address += input_section->output_offset;
816
817 /* Make sure it fit in 16 bits. */
818 if (val >= 0x8000 && val < 0xffff8000)
819 return bfd_reloc_overflow;
820
821 return bfd_reloc_ok;
822 }
823
824 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
825
826 struct elf_reloc_map {
827 bfd_reloc_code_real_type bfd_reloc_val;
828 enum reloc_type elf_reloc_val;
829 };
830
831 static CONST struct elf_reloc_map mips_reloc_map[] =
832 {
833 { BFD_RELOC_NONE, R_MIPS_NONE, },
834 { BFD_RELOC_16, R_MIPS_16 },
835 { BFD_RELOC_32, R_MIPS_32 },
836 { BFD_RELOC_CTOR, R_MIPS_32 },
837 { BFD_RELOC_32_PCREL, R_MIPS_REL32 },
838 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
839 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
840 { BFD_RELOC_LO16, R_MIPS_LO16 },
841 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
842 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
843 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
844 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
845 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
846 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 }
847 };
848
849 /* Given a BFD reloc type, return a howto structure. */
850
851 static const struct reloc_howto_struct *
852 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
853 bfd *abfd;
854 bfd_reloc_code_real_type code;
855 {
856 int i;
857
858 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
859 {
860 if (mips_reloc_map[i].bfd_reloc_val == code)
861 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
862 }
863 return NULL;
864 }
865
866 /* Given a MIPS reloc type, fill in an arelent structure. */
867
868 static void
869 mips_info_to_howto_rel (abfd, cache_ptr, dst)
870 bfd *abfd;
871 arelent *cache_ptr;
872 Elf32_Internal_Rel *dst;
873 {
874 unsigned int r_type;
875
876 r_type = ELF32_R_TYPE (dst->r_info);
877 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
878 cache_ptr->howto = &elf_mips_howto_table[r_type];
879
880 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
881 value for the object file. We get the addend now, rather than
882 when we do the relocation, because the symbol manipulations done
883 by the linker may cause us to lose track of the input BFD. */
884 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
885 && (r_type == (unsigned int) R_MIPS_GPREL16
886 || r_type == (unsigned int) R_MIPS_LITERAL))
887 cache_ptr->addend = elf_gp (abfd);
888 }
889 \f
890 /* A .reginfo section holds a single Elf32_RegInfo structure. These
891 routines swap this structure in and out. They are used outside of
892 BFD, so they are globally visible. */
893
894 void
895 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
896 bfd *abfd;
897 const Elf32_External_RegInfo *ex;
898 Elf32_RegInfo *in;
899 {
900 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
901 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
902 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
903 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
904 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
905 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
906 }
907
908 void
909 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
910 bfd *abfd;
911 const Elf32_RegInfo *in;
912 Elf32_External_RegInfo *ex;
913 {
914 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
915 (bfd_byte *) ex->ri_gprmask);
916 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
917 (bfd_byte *) ex->ri_cprmask[0]);
918 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
919 (bfd_byte *) ex->ri_cprmask[1]);
920 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
921 (bfd_byte *) ex->ri_cprmask[2]);
922 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
923 (bfd_byte *) ex->ri_cprmask[3]);
924 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
925 (bfd_byte *) ex->ri_gp_value);
926 }
927
928 /* Swap an entry in a .gptab section. Note that these routines rely
929 on the equivalence of the two elements of the union. */
930
931 static void
932 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
933 bfd *abfd;
934 const Elf32_External_gptab *ex;
935 Elf32_gptab *in;
936 {
937 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
938 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
939 }
940
941 static void
942 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
943 bfd *abfd;
944 const Elf32_gptab *in;
945 Elf32_External_gptab *ex;
946 {
947 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
948 ex->gt_entry.gt_g_value);
949 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
950 ex->gt_entry.gt_bytes);
951 }
952 \f
953 /* Determine whether a symbol is global for the purposes of splitting
954 the symbol table into global symbols and local symbols. At least
955 on Irix 5, this split must be between section symbols and all other
956 symbols. On most ELF targets the split is between static symbols
957 and externally visible symbols. */
958
959 /*ARGSUSED*/
960 static boolean
961 mips_elf_sym_is_global (abfd, sym)
962 bfd *abfd;
963 asymbol *sym;
964 {
965 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
966 }
967 \f
968 /* Set the right machine number for a MIPS ELF file. */
969
970 static boolean
971 mips_elf_object_p (abfd)
972 bfd *abfd;
973 {
974 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
975 {
976 default:
977 case E_MIPS_ARCH_1:
978 /* Just use the default, which was set in elfcode.h. */
979 break;
980
981 case E_MIPS_ARCH_2:
982 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
983 break;
984
985 case E_MIPS_ARCH_3:
986 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
987 break;
988 }
989
990 /* Irix 5 is broken. Object file symbol tables are not always
991 sorted correctly such that local symbols precede global symbols,
992 and the sh_info field in the symbol table is not always right.
993 We try to quickly check whether the symbol table is broken for
994 this BFD, and, if it is, we set elf_bad_symtab in tdata. */
995 if (elf_onesymtab (abfd) != 0)
996 {
997 Elf_Internal_Shdr *symtab_hdr;
998 Elf32_External_Sym esym;
999
1000 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1001 if (bfd_seek (abfd,
1002 (symtab_hdr->sh_offset
1003 + symtab_hdr->sh_size
1004 - sizeof (Elf32_External_Sym)),
1005 SEEK_SET) != 0
1006 || (bfd_read ((PTR) &esym, 1, sizeof (Elf32_External_Sym), abfd)
1007 != sizeof (Elf32_External_Sym)))
1008 return false;
1009 if (ELF_ST_BIND (bfd_h_get_8 (abfd, (bfd_byte *) esym.st_info))
1010 == STB_LOCAL)
1011 elf_bad_symtab (abfd) = true;
1012 }
1013
1014 return true;
1015 }
1016
1017 /* The final processing done just before writing out a MIPS ELF object
1018 file. This gets the MIPS architecture right based on the machine
1019 number. */
1020
1021 /*ARGSUSED*/
1022 static void
1023 mips_elf_final_write_processing (abfd, linker)
1024 bfd *abfd;
1025 boolean linker;
1026 {
1027 unsigned long val;
1028 unsigned int i;
1029 Elf_Internal_Shdr **hdrpp;
1030
1031 switch (bfd_get_mach (abfd))
1032 {
1033 case 3000:
1034 val = E_MIPS_ARCH_1;
1035 break;
1036
1037 case 6000:
1038 val = E_MIPS_ARCH_2;
1039 break;
1040
1041 case 4000:
1042 val = E_MIPS_ARCH_3;
1043 break;
1044
1045 default:
1046 return;
1047 }
1048
1049 elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
1050 elf_elfheader (abfd)->e_flags |= val;
1051
1052 /* Set the sh_info field for .gptab sections. */
1053 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1054 i < elf_elfheader (abfd)->e_shnum;
1055 i++, hdrpp++)
1056 {
1057 if ((*hdrpp)->sh_type == SHT_MIPS_GPTAB)
1058 {
1059 const char *name;
1060 asection *sec;
1061
1062 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1063 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1064 BFD_ASSERT (name != NULL
1065 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
1066 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
1067 BFD_ASSERT (sec != NULL);
1068 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1069 }
1070 }
1071 }
1072 \f
1073 /* Handle a MIPS specific section when reading an object file. This
1074 is called when elfcode.h finds a section with an unknown type.
1075 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1076 how to. */
1077
1078 static boolean
1079 mips_elf_section_from_shdr (abfd, hdr, name)
1080 bfd *abfd;
1081 Elf32_Internal_Shdr *hdr;
1082 char *name;
1083 {
1084 asection *newsect;
1085
1086 /* There ought to be a place to keep ELF backend specific flags, but
1087 at the moment there isn't one. We just keep track of the
1088 sections by their name, instead. Fortunately, the ABI gives
1089 suggested names for all the MIPS specific sections, so we will
1090 probably get away with this. */
1091 switch (hdr->sh_type)
1092 {
1093 case SHT_MIPS_LIBLIST:
1094 if (strcmp (name, ".liblist") != 0)
1095 return false;
1096 break;
1097 case SHT_MIPS_MSYM:
1098 if (strcmp (name, ".msym") != 0)
1099 return false;
1100 break;
1101 case SHT_MIPS_CONFLICT:
1102 if (strcmp (name, ".conflict") != 0)
1103 return false;
1104 break;
1105 case SHT_MIPS_GPTAB:
1106 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
1107 return false;
1108 break;
1109 case SHT_MIPS_UCODE:
1110 if (strcmp (name, ".ucode") != 0)
1111 return false;
1112 break;
1113 case SHT_MIPS_DEBUG:
1114 if (strcmp (name, ".mdebug") != 0)
1115 return false;
1116 break;
1117 case SHT_MIPS_REGINFO:
1118 if (strcmp (name, ".reginfo") != 0
1119 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
1120 return false;
1121 break;
1122 case SHT_MIPS_OPTIONS:
1123 if (strcmp (name, ".options") != 0)
1124 return false;
1125 break;
1126 case SHT_MIPS_DWARF:
1127 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
1128 return false;
1129 break;
1130 case SHT_MIPS_EVENTS:
1131 if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) != 0)
1132 return false;
1133 break;
1134 default:
1135 return false;
1136 }
1137
1138 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1139 return false;
1140 newsect = hdr->bfd_section;
1141
1142 if (hdr->sh_type == SHT_MIPS_DEBUG)
1143 {
1144 if (! bfd_set_section_flags (abfd, newsect,
1145 (bfd_get_section_flags (abfd, newsect)
1146 | SEC_DEBUGGING)))
1147 return false;
1148 }
1149
1150 /* FIXME: We should record sh_info for a .gptab section. */
1151
1152 /* For a .reginfo section, set the gp value in the tdata information
1153 from the contents of this section. We need the gp value while
1154 processing relocs, so we just get it now. */
1155 if (hdr->sh_type == SHT_MIPS_REGINFO)
1156 {
1157 Elf32_External_RegInfo ext;
1158 Elf32_RegInfo s;
1159
1160 if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
1161 (file_ptr) 0, sizeof ext))
1162 return false;
1163 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
1164 elf_gp (abfd) = s.ri_gp_value;
1165 }
1166
1167 return true;
1168 }
1169
1170 /* Set the correct type for a MIPS ELF section. We do this by the
1171 section name, which is a hack, but ought to work. */
1172
1173 static boolean
1174 mips_elf_fake_sections (abfd, hdr, sec)
1175 bfd *abfd;
1176 Elf32_Internal_Shdr *hdr;
1177 asection *sec;
1178 {
1179 register const char *name;
1180
1181 name = bfd_get_section_name (abfd, sec);
1182
1183 if (strcmp (name, ".liblist") == 0)
1184 {
1185 hdr->sh_type = SHT_MIPS_LIBLIST;
1186 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
1187 /* FIXME: Set the sh_link field. */
1188 }
1189 else if (strcmp (name, ".msym") == 0)
1190 {
1191 hdr->sh_type = SHT_MIPS_MSYM;
1192 hdr->sh_entsize = 8;
1193 /* FIXME: Set the sh_info field. */
1194 }
1195 else if (strcmp (name, ".conflict") == 0)
1196 hdr->sh_type = SHT_MIPS_CONFLICT;
1197 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
1198 {
1199 hdr->sh_type = SHT_MIPS_GPTAB;
1200 hdr->sh_entsize = sizeof (Elf32_External_gptab);
1201 /* The sh_info field is set in mips_elf_final_write_processing. */
1202 }
1203 else if (strcmp (name, ".ucode") == 0)
1204 hdr->sh_type = SHT_MIPS_UCODE;
1205 else if (strcmp (name, ".mdebug") == 0)
1206 {
1207 hdr->sh_type = SHT_MIPS_DEBUG;
1208 hdr->sh_entsize = 1;
1209 }
1210 else if (strcmp (name, ".reginfo") == 0)
1211 {
1212 hdr->sh_type = SHT_MIPS_REGINFO;
1213 hdr->sh_entsize = 1;
1214
1215 /* Force the section size to the correct value, even if the
1216 linker thinks it is larger. The link routine below will only
1217 write out this much data for .reginfo. */
1218 hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
1219 }
1220 else if (strcmp (name, ".options") == 0)
1221 {
1222 hdr->sh_type = SHT_MIPS_OPTIONS;
1223 hdr->sh_entsize = 1;
1224 }
1225 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
1226 hdr->sh_type = SHT_MIPS_DWARF;
1227 else if (strncmp (name, ".MIPS.events.", sizeof ".MIPS.events." - 1) == 0)
1228 hdr->sh_type = SHT_MIPS_EVENTS;
1229
1230 return true;
1231 }
1232
1233 /* Given a BFD section, try to locate the corresponding ELF section
1234 index. */
1235
1236 static boolean
1237 mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
1238 bfd *abfd;
1239 Elf32_Internal_Shdr *hdr;
1240 asection *sec;
1241 int *retval;
1242 {
1243 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1244 {
1245 *retval = SHN_MIPS_SCOMMON;
1246 return true;
1247 }
1248 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
1249 {
1250 *retval = SHN_MIPS_ACOMMON;
1251 return true;
1252 }
1253 return false;
1254 }
1255
1256 /* Work over a section just before writing it out. We update the GP
1257 value in the .reginfo section based on the value we are using.
1258 FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
1259 name; there has to be a better way. */
1260
1261 static boolean
1262 mips_elf_section_processing (abfd, hdr)
1263 bfd *abfd;
1264 Elf32_Internal_Shdr *hdr;
1265 {
1266 if (hdr->sh_type == SHT_MIPS_REGINFO)
1267 {
1268 bfd_byte buf[4];
1269
1270 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
1271 BFD_ASSERT (hdr->contents == NULL);
1272
1273 if (bfd_seek (abfd,
1274 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
1275 SEEK_SET) == -1)
1276 return false;
1277 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
1278 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
1279 return false;
1280 }
1281
1282 if (hdr->bfd_section != NULL)
1283 {
1284 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
1285
1286 if (strcmp (name, ".sdata") == 0)
1287 {
1288 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1289 hdr->sh_type = SHT_PROGBITS;
1290 }
1291 else if (strcmp (name, ".sbss") == 0)
1292 {
1293 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1294 hdr->sh_type = SHT_NOBITS;
1295 }
1296 else if (strcmp (name, ".lit8") == 0
1297 || strcmp (name, ".lit4") == 0)
1298 {
1299 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
1300 hdr->sh_type = SHT_PROGBITS;
1301 }
1302 }
1303
1304 return true;
1305 }
1306 \f
1307 /* MIPS ELF uses two common sections. One is the usual one, and the
1308 other is for small objects. All the small objects are kept
1309 together, and then referenced via the gp pointer, which yields
1310 faster assembler code. This is what we use for the small common
1311 section. This approach is copied from ecoff.c. */
1312 static asection mips_elf_scom_section;
1313 static asymbol mips_elf_scom_symbol;
1314 static asymbol *mips_elf_scom_symbol_ptr;
1315
1316 /* MIPS ELF also uses an acommon section, which represents an
1317 allocated common symbol which may be overridden by a
1318 definition in a shared library. */
1319 static asection mips_elf_acom_section;
1320 static asymbol mips_elf_acom_symbol;
1321 static asymbol *mips_elf_acom_symbol_ptr;
1322
1323 /* Handle the special MIPS section numbers that a symbol may use. */
1324
1325 static void
1326 mips_elf_symbol_processing (abfd, asym)
1327 bfd *abfd;
1328 asymbol *asym;
1329 {
1330 elf_symbol_type *elfsym;
1331
1332 elfsym = (elf_symbol_type *) asym;
1333 switch (elfsym->internal_elf_sym.st_shndx)
1334 {
1335 case SHN_MIPS_ACOMMON:
1336 /* This section is used in a dynamically linked executable file.
1337 It is an allocated common section. The dynamic linker can
1338 either resolve these symbols to something in a shared
1339 library, or it can just leave them here. For our purposes,
1340 we can consider these symbols to be in a new section. */
1341 if (mips_elf_acom_section.name == NULL)
1342 {
1343 /* Initialize the acommon section. */
1344 mips_elf_acom_section.name = ".acommon";
1345 mips_elf_acom_section.flags = SEC_NO_FLAGS;
1346 mips_elf_acom_section.output_section = &mips_elf_acom_section;
1347 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
1348 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
1349 mips_elf_acom_symbol.name = ".acommon";
1350 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
1351 mips_elf_acom_symbol.section = &mips_elf_acom_section;
1352 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
1353 }
1354 asym->section = &mips_elf_acom_section;
1355 break;
1356
1357 case SHN_COMMON:
1358 /* Common symbols less than the GP size are automatically
1359 treated as SHN_MIPS_SCOMMON symbols. */
1360 if (asym->value > elf_gp_size (abfd))
1361 break;
1362 /* Fall through. */
1363 case SHN_MIPS_SCOMMON:
1364 if (mips_elf_scom_section.name == NULL)
1365 {
1366 /* Initialize the small common section. */
1367 mips_elf_scom_section.name = ".scommon";
1368 mips_elf_scom_section.flags = SEC_IS_COMMON;
1369 mips_elf_scom_section.output_section = &mips_elf_scom_section;
1370 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
1371 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
1372 mips_elf_scom_symbol.name = ".scommon";
1373 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
1374 mips_elf_scom_symbol.section = &mips_elf_scom_section;
1375 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
1376 }
1377 asym->section = &mips_elf_scom_section;
1378 asym->value = elfsym->internal_elf_sym.st_size;
1379 break;
1380
1381 case SHN_MIPS_SUNDEFINED:
1382 asym->section = bfd_und_section_ptr;
1383 break;
1384 }
1385 }
1386 \f
1387 /* Read ECOFF debugging information from a .mdebug section into a
1388 ecoff_debug_info structure. */
1389
1390 static boolean
1391 mips_elf_read_ecoff_info (abfd, section, debug)
1392 bfd *abfd;
1393 asection *section;
1394 struct ecoff_debug_info *debug;
1395 {
1396 HDRR *symhdr;
1397 const struct ecoff_debug_swap *swap;
1398 char *ext_hdr = NULL;
1399
1400 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1401
1402 ext_hdr = (char *) malloc (swap->external_hdr_size);
1403 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1404 {
1405 bfd_set_error (bfd_error_no_memory);
1406 goto error_return;
1407 }
1408
1409 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1410 swap->external_hdr_size)
1411 == false)
1412 goto error_return;
1413
1414 symhdr = &debug->symbolic_header;
1415 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1416
1417 /* The symbolic header contains absolute file offsets and sizes to
1418 read. */
1419 #define READ(ptr, offset, count, size, type) \
1420 if (symhdr->count == 0) \
1421 debug->ptr = NULL; \
1422 else \
1423 { \
1424 debug->ptr = (type) malloc (size * symhdr->count); \
1425 if (debug->ptr == NULL) \
1426 { \
1427 bfd_set_error (bfd_error_no_memory); \
1428 goto error_return; \
1429 } \
1430 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
1431 || (bfd_read (debug->ptr, size, symhdr->count, \
1432 abfd) != size * symhdr->count)) \
1433 goto error_return; \
1434 }
1435
1436 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1437 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1438 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1439 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1440 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1441 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1442 union aux_ext *);
1443 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1444 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1445 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1446 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
1447 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
1448 #undef READ
1449
1450 debug->fdr = NULL;
1451 debug->adjust = NULL;
1452
1453 return true;
1454
1455 error_return:
1456 if (ext_hdr != NULL)
1457 free (ext_hdr);
1458 if (debug->line != NULL)
1459 free (debug->line);
1460 if (debug->external_dnr != NULL)
1461 free (debug->external_dnr);
1462 if (debug->external_pdr != NULL)
1463 free (debug->external_pdr);
1464 if (debug->external_sym != NULL)
1465 free (debug->external_sym);
1466 if (debug->external_opt != NULL)
1467 free (debug->external_opt);
1468 if (debug->external_aux != NULL)
1469 free (debug->external_aux);
1470 if (debug->ss != NULL)
1471 free (debug->ss);
1472 if (debug->ssext != NULL)
1473 free (debug->ssext);
1474 if (debug->external_fdr != NULL)
1475 free (debug->external_fdr);
1476 if (debug->external_rfd != NULL)
1477 free (debug->external_rfd);
1478 if (debug->external_ext != NULL)
1479 free (debug->external_ext);
1480 return false;
1481 }
1482
1483 /* The MIPS ELF linker needs additional information for each symbol in
1484 the global hash table. */
1485
1486 struct mips_elf_link_hash_entry
1487 {
1488 struct elf_link_hash_entry root;
1489
1490 /* External symbol information. */
1491 EXTR esym;
1492 };
1493
1494 /* MIPS ELF linker hash table. */
1495
1496 struct mips_elf_link_hash_table
1497 {
1498 struct elf_link_hash_table root;
1499 };
1500
1501 /* Look up an entry in a MIPS ELF linker hash table. */
1502
1503 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1504 ((struct mips_elf_link_hash_entry *) \
1505 elf_link_hash_lookup (&(table)->root, (string), (create), \
1506 (copy), (follow)))
1507
1508 /* Traverse a MIPS ELF linker hash table. */
1509
1510 #define mips_elf_link_hash_traverse(table, func, info) \
1511 (elf_link_hash_traverse \
1512 (&(table)->root, \
1513 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1514 (info)))
1515
1516 /* Get the MIPS ELF linker hash table from a link_info structure. */
1517
1518 #define mips_elf_hash_table(p) \
1519 ((struct mips_elf_link_hash_table *) ((p)->hash))
1520
1521 static boolean mips_elf_output_extsym
1522 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
1523
1524 /* Create an entry in a MIPS ELF linker hash table. */
1525
1526 static struct bfd_hash_entry *
1527 mips_elf_link_hash_newfunc (entry, table, string)
1528 struct bfd_hash_entry *entry;
1529 struct bfd_hash_table *table;
1530 const char *string;
1531 {
1532 struct mips_elf_link_hash_entry *ret =
1533 (struct mips_elf_link_hash_entry *) entry;
1534
1535 /* Allocate the structure if it has not already been allocated by a
1536 subclass. */
1537 if (ret == (struct mips_elf_link_hash_entry *) NULL)
1538 ret = ((struct mips_elf_link_hash_entry *)
1539 bfd_hash_allocate (table,
1540 sizeof (struct mips_elf_link_hash_entry)));
1541 if (ret == (struct mips_elf_link_hash_entry *) NULL)
1542 {
1543 bfd_set_error (bfd_error_no_memory);
1544 return (struct bfd_hash_entry *) ret;
1545 }
1546
1547 /* Call the allocation method of the superclass. */
1548 ret = ((struct mips_elf_link_hash_entry *)
1549 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1550 table, string));
1551 if (ret != (struct mips_elf_link_hash_entry *) NULL)
1552 {
1553 /* Set local fields. */
1554 memset (&ret->esym, 0, sizeof (EXTR));
1555 /* We use -2 as a marker to indicate that the information has
1556 not been set. -1 means there is no associated ifd. */
1557 ret->esym.ifd = -2;
1558 }
1559
1560 return (struct bfd_hash_entry *) ret;
1561 }
1562
1563 /* Create a MIPS ELF linker hash table. */
1564
1565 static struct bfd_link_hash_table *
1566 mips_elf_link_hash_table_create (abfd)
1567 bfd *abfd;
1568 {
1569 struct mips_elf_link_hash_table *ret;
1570
1571 ret = ((struct mips_elf_link_hash_table *)
1572 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
1573 if (ret == (struct mips_elf_link_hash_table *) NULL)
1574 {
1575 bfd_set_error (bfd_error_no_memory);
1576 return NULL;
1577 }
1578
1579 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
1580 mips_elf_link_hash_newfunc))
1581 {
1582 bfd_release (abfd, ret);
1583 return NULL;
1584 }
1585
1586 return &ret->root.root;
1587 }
1588
1589 /* Hook called by the linker routine which adds symbols from an object
1590 file. We must handle the special MIPS section numbers here. */
1591
1592 /*ARGSUSED*/
1593 static boolean
1594 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1595 bfd *abfd;
1596 struct bfd_link_info *info;
1597 const Elf_Internal_Sym *sym;
1598 const char **namep;
1599 flagword *flagsp;
1600 asection **secp;
1601 bfd_vma *valp;
1602 {
1603 switch (sym->st_shndx)
1604 {
1605 case SHN_COMMON:
1606 /* Common symbols less than the GP size are automatically
1607 treated as SHN_MIPS_SCOMMON symbols. */
1608 if (sym->st_size > elf_gp_size (abfd))
1609 break;
1610 /* Fall through. */
1611 case SHN_MIPS_SCOMMON:
1612 *secp = bfd_make_section_old_way (abfd, ".scommon");
1613 (*secp)->flags |= SEC_IS_COMMON;
1614 *valp = sym->st_size;
1615 break;
1616
1617 case SHN_MIPS_SUNDEFINED:
1618 *secp = bfd_und_section_ptr;
1619 break;
1620 }
1621
1622 return true;
1623 }
1624
1625 /* Structure used to pass information to mips_elf_output_extsym. */
1626
1627 struct extsym_info
1628 {
1629 bfd *abfd;
1630 struct bfd_link_info *info;
1631 struct ecoff_debug_info *debug;
1632 const struct ecoff_debug_swap *swap;
1633 boolean failed;
1634 };
1635
1636 /* This routine is used to write out ECOFF debugging external symbol
1637 information. It is called via mips_elf_link_hash_traverse. The
1638 ECOFF external symbol information must match the ELF external
1639 symbol information. Unfortunately, at this point we don't know
1640 whether a symbol is required by reloc information, so the two
1641 tables may wind up being different. We must sort out the external
1642 symbol information before we can set the final size of the .mdebug
1643 section, and we must set the size of the .mdebug section before we
1644 can relocate any sections, and we can't know which symbols are
1645 required by relocation until we relocate the sections.
1646 Fortunately, it is relatively unlikely that any symbol will be
1647 stripped but required by a reloc. In particular, it can not happen
1648 when generating a final executable. */
1649
1650 static boolean
1651 mips_elf_output_extsym (h, data)
1652 struct mips_elf_link_hash_entry *h;
1653 PTR data;
1654 {
1655 struct extsym_info *einfo = (struct extsym_info *) data;
1656 boolean strip;
1657
1658 if (h->root.indx == -2)
1659 strip = false;
1660 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1661 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
1662 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
1663 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
1664 strip = true;
1665 else if (einfo->info->strip == strip_all
1666 || (einfo->info->strip == strip_some
1667 && bfd_hash_lookup (einfo->info->keep_hash,
1668 h->root.root.root.string,
1669 false, false) == NULL))
1670 strip = true;
1671 else
1672 strip = false;
1673
1674 if (strip)
1675 return true;
1676
1677 if (h->esym.ifd == -2)
1678 {
1679 h->esym.jmptbl = 0;
1680 h->esym.cobol_main = 0;
1681 h->esym.weakext = 0;
1682 h->esym.reserved = 0;
1683 h->esym.ifd = ifdNil;
1684 h->esym.asym.value = 0;
1685 h->esym.asym.st = stGlobal;
1686
1687 if (h->root.root.type != bfd_link_hash_defined)
1688 h->esym.asym.sc = scAbs;
1689 else
1690 {
1691 asection *output_section;
1692 const char *name;
1693
1694 output_section = h->root.root.u.def.section->output_section;
1695 name = bfd_section_name (output_section->owner, output_section);
1696
1697 if (strcmp (name, ".text") == 0)
1698 h->esym.asym.sc = scText;
1699 else if (strcmp (name, ".data") == 0)
1700 h->esym.asym.sc = scData;
1701 else if (strcmp (name, ".sdata") == 0)
1702 h->esym.asym.sc = scSData;
1703 else if (strcmp (name, ".rodata") == 0
1704 || strcmp (name, ".rdata") == 0)
1705 h->esym.asym.sc = scRData;
1706 else if (strcmp (name, ".bss") == 0)
1707 h->esym.asym.sc = scBss;
1708 else if (strcmp (name, ".sbss") == 0)
1709 h->esym.asym.sc = scSBss;
1710 else if (strcmp (name, ".init") == 0)
1711 h->esym.asym.sc = scInit;
1712 else if (strcmp (name, ".fini") == 0)
1713 h->esym.asym.sc = scFini;
1714 else
1715 h->esym.asym.sc = scAbs;
1716 }
1717
1718 h->esym.asym.reserved = 0;
1719 h->esym.asym.index = indexNil;
1720 }
1721
1722
1723 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1724 h->root.root.root.string,
1725 &h->esym))
1726 {
1727 einfo->failed = true;
1728 return false;
1729 }
1730
1731 return true;
1732 }
1733
1734 /* A comparison routine used to sort .gptab entries. */
1735
1736 static int
1737 gptab_compare (p1, p2)
1738 const PTR p1;
1739 const PTR p2;
1740 {
1741 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
1742 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
1743
1744 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1745 }
1746
1747 /* We need to use a special link routine to handle the .reginfo and
1748 the .mdebug sections. We need to merge all instances of these
1749 sections together, not write them all out sequentially. */
1750
1751 static boolean
1752 mips_elf_final_link (abfd, info)
1753 bfd *abfd;
1754 struct bfd_link_info *info;
1755 {
1756 asection **secpp;
1757 asection *o;
1758 struct bfd_link_order *p;
1759 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
1760 Elf32_RegInfo reginfo;
1761 struct ecoff_debug_info debug;
1762 const struct ecoff_debug_swap *swap
1763 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1764 HDRR *symhdr = &debug.symbolic_header;
1765 PTR mdebug_handle = NULL;
1766
1767 /* Drop the .options section, since it has special semantics which I
1768 haven't bothered to figure out. */
1769 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
1770 {
1771 if (strcmp ((*secpp)->name, ".options") == 0)
1772 {
1773 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
1774 if (p->type == bfd_indirect_link_order)
1775 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
1776 (*secpp)->link_order_head = NULL;
1777 *secpp = (*secpp)->next;
1778 --abfd->section_count;
1779 break;
1780 }
1781 }
1782
1783 /* Go through the sections and collect the .reginfo and .mdebug
1784 information. */
1785 reginfo_sec = NULL;
1786 mdebug_sec = NULL;
1787 gptab_data_sec = NULL;
1788 gptab_bss_sec = NULL;
1789 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
1790 {
1791 if (strcmp (o->name, ".reginfo") == 0)
1792 {
1793 memset (&reginfo, 0, sizeof reginfo);
1794
1795 /* We have found the .reginfo section in the output file.
1796 Look through all the link_orders comprising it and merge
1797 the information together. */
1798 for (p = o->link_order_head;
1799 p != (struct bfd_link_order *) NULL;
1800 p = p->next)
1801 {
1802 asection *input_section;
1803 bfd *input_bfd;
1804 Elf32_External_RegInfo ext;
1805 Elf32_RegInfo sub;
1806
1807 if (p->type != bfd_indirect_link_order)
1808 {
1809 if (p->type == bfd_fill_link_order)
1810 continue;
1811 abort ();
1812 }
1813
1814 input_section = p->u.indirect.section;
1815 input_bfd = input_section->owner;
1816 BFD_ASSERT (input_section->_raw_size
1817 == sizeof (Elf32_External_RegInfo));
1818 if (! bfd_get_section_contents (input_bfd, input_section,
1819 (PTR) &ext,
1820 (file_ptr) 0,
1821 sizeof ext))
1822 return false;
1823
1824 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
1825
1826 reginfo.ri_gprmask |= sub.ri_gprmask;
1827 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
1828 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
1829 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
1830 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
1831
1832 /* ri_gp_value is set by the function
1833 mips_elf_section_processing when the section is
1834 finally written out. */
1835
1836 /* Hack: reset the SEC_HAS_CONTENTS flag so that
1837 elf_link_input_bfd ignores this section. */
1838 input_section->flags &=~ SEC_HAS_CONTENTS;
1839 }
1840
1841 /* Force the section size to the value we want. */
1842 o->_raw_size = sizeof (Elf32_External_RegInfo);
1843
1844 /* Skip this section later on (I don't think this currently
1845 matters, but someday it might). */
1846 o->link_order_head = (struct bfd_link_order *) NULL;
1847
1848 reginfo_sec = o;
1849 }
1850
1851 if (strcmp (o->name, ".mdebug") == 0)
1852 {
1853 struct extsym_info einfo;
1854
1855 /* We have found the .mdebug section in the output file.
1856 Look through all the link_orders comprising it and merge
1857 the information together. */
1858 symhdr->magic = swap->sym_magic;
1859 /* FIXME: What should the version stamp be? */
1860 symhdr->vstamp = 0;
1861 symhdr->ilineMax = 0;
1862 symhdr->cbLine = 0;
1863 symhdr->idnMax = 0;
1864 symhdr->ipdMax = 0;
1865 symhdr->isymMax = 0;
1866 symhdr->ioptMax = 0;
1867 symhdr->iauxMax = 0;
1868 symhdr->issMax = 0;
1869 symhdr->issExtMax = 0;
1870 symhdr->ifdMax = 0;
1871 symhdr->crfd = 0;
1872 symhdr->iextMax = 0;
1873
1874 /* We accumulate the debugging information itself in the
1875 debug_info structure. */
1876 debug.line = NULL;
1877 debug.external_dnr = NULL;
1878 debug.external_pdr = NULL;
1879 debug.external_sym = NULL;
1880 debug.external_opt = NULL;
1881 debug.external_aux = NULL;
1882 debug.ss = NULL;
1883 debug.ssext = debug.ssext_end = NULL;
1884 debug.external_fdr = NULL;
1885 debug.external_rfd = NULL;
1886 debug.external_ext = debug.external_ext_end = NULL;
1887
1888 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
1889 if (mdebug_handle == (PTR) NULL)
1890 return false;
1891
1892 for (p = o->link_order_head;
1893 p != (struct bfd_link_order *) NULL;
1894 p = p->next)
1895 {
1896 asection *input_section;
1897 bfd *input_bfd;
1898 const struct ecoff_debug_swap *input_swap;
1899 struct ecoff_debug_info input_debug;
1900 char *eraw_src;
1901 char *eraw_end;
1902
1903 if (p->type != bfd_indirect_link_order)
1904 {
1905 if (p->type == bfd_fill_link_order)
1906 continue;
1907 abort ();
1908 }
1909
1910 input_section = p->u.indirect.section;
1911 input_bfd = input_section->owner;
1912
1913 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
1914 || (get_elf_backend_data (input_bfd)
1915 ->elf_backend_ecoff_debug_swap) == NULL)
1916 {
1917 /* I don't know what a non MIPS ELF bfd would be
1918 doing with a .mdebug section, but I don't really
1919 want to deal with it. */
1920 continue;
1921 }
1922
1923 input_swap = (get_elf_backend_data (input_bfd)
1924 ->elf_backend_ecoff_debug_swap);
1925
1926 BFD_ASSERT (p->size == input_section->_raw_size);
1927
1928 /* The ECOFF linking code expects that we have already
1929 read in the debugging information and set up an
1930 ecoff_debug_info structure, so we do that now. */
1931 if (! mips_elf_read_ecoff_info (input_bfd, input_section,
1932 &input_debug))
1933 return false;
1934
1935 if (! (bfd_ecoff_debug_accumulate
1936 (mdebug_handle, abfd, &debug, swap, input_bfd,
1937 &input_debug, input_swap, info)))
1938 return false;
1939
1940 /* Loop through the external symbols. For each one with
1941 interesting information, try to find the symbol in
1942 the linker global hash table and save the information
1943 for the output external symbols. */
1944 eraw_src = input_debug.external_ext;
1945 eraw_end = (eraw_src
1946 + (input_debug.symbolic_header.iextMax
1947 * input_swap->external_ext_size));
1948 for (;
1949 eraw_src < eraw_end;
1950 eraw_src += input_swap->external_ext_size)
1951 {
1952 EXTR ext;
1953 const char *name;
1954 struct mips_elf_link_hash_entry *h;
1955
1956 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
1957 if (ext.asym.sc == scNil
1958 || ext.asym.sc == scUndefined
1959 || ext.asym.sc == scSUndefined)
1960 continue;
1961
1962 name = input_debug.ssext + ext.asym.iss;
1963 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
1964 name, false, false, true);
1965 if (h == NULL || h->esym.ifd != -2)
1966 continue;
1967
1968 if (ext.ifd != -1)
1969 {
1970 BFD_ASSERT (ext.ifd
1971 < input_debug.symbolic_header.ifdMax);
1972 ext.ifd = input_debug.ifdmap[ext.ifd];
1973 }
1974
1975 h->esym = ext;
1976 }
1977
1978 /* Free up the information we just read. */
1979 free (input_debug.line);
1980 free (input_debug.external_dnr);
1981 free (input_debug.external_pdr);
1982 free (input_debug.external_sym);
1983 free (input_debug.external_opt);
1984 free (input_debug.external_aux);
1985 free (input_debug.ss);
1986 free (input_debug.ssext);
1987 free (input_debug.external_fdr);
1988 free (input_debug.external_rfd);
1989 free (input_debug.external_ext);
1990
1991 /* Hack: reset the SEC_HAS_CONTENTS flag so that
1992 elf_link_input_bfd ignores this section. */
1993 input_section->flags &=~ SEC_HAS_CONTENTS;
1994 }
1995
1996 /* Build the external symbol information. */
1997 einfo.abfd = abfd;
1998 einfo.info = info;
1999 einfo.debug = &debug;
2000 einfo.swap = swap;
2001 einfo.failed = false;
2002 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
2003 mips_elf_output_extsym,
2004 (PTR) &einfo);
2005 if (einfo.failed)
2006 return false;
2007
2008 /* Set the size of the .mdebug section. */
2009 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
2010
2011 /* Skip this section later on (I don't think this currently
2012 matters, but someday it might). */
2013 o->link_order_head = (struct bfd_link_order *) NULL;
2014
2015 mdebug_sec = o;
2016 }
2017
2018 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
2019 {
2020 const char *subname;
2021 unsigned int c;
2022 Elf32_gptab *tab;
2023 Elf32_External_gptab *ext_tab;
2024 unsigned int i;
2025
2026 /* The .gptab.sdata and .gptab.sbss sections hold
2027 information describing how the small data area would
2028 change depending upon the -G switch. These sections
2029 not used in executables files. */
2030 if (! info->relocateable)
2031 {
2032 asection **secpp;
2033
2034 for (p = o->link_order_head;
2035 p != (struct bfd_link_order *) NULL;
2036 p = p->next)
2037 {
2038 asection *input_section;
2039
2040 if (p->type != bfd_indirect_link_order)
2041 {
2042 if (p->type == bfd_fill_link_order)
2043 continue;
2044 abort ();
2045 }
2046
2047 input_section = p->u.indirect.section;
2048
2049 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2050 elf_link_input_bfd ignores this section. */
2051 input_section->flags &=~ SEC_HAS_CONTENTS;
2052 }
2053
2054 /* Skip this section later on (I don't think this
2055 currently matters, but someday it might). */
2056 o->link_order_head = (struct bfd_link_order *) NULL;
2057
2058 /* Really remove the section. */
2059 for (secpp = &abfd->sections;
2060 *secpp != o;
2061 secpp = &(*secpp)->next)
2062 ;
2063 *secpp = (*secpp)->next;
2064 --abfd->section_count;
2065
2066 continue;
2067 }
2068
2069 /* There is one gptab for initialized data, and one for
2070 uninitialized data. */
2071 if (strcmp (o->name, ".gptab.sdata") == 0)
2072 gptab_data_sec = o;
2073 else if (strcmp (o->name, ".gptab.sbss") == 0)
2074 gptab_bss_sec = o;
2075 else
2076 {
2077 bfd_set_error (bfd_error_nonrepresentable_section);
2078 return false;
2079 }
2080
2081 /* The linker script always combines .gptab.data and
2082 .gptab.sdata into .gptab.sdata, and likewise for
2083 .gptab.bss and .gptab.sbss. It is possible that there is
2084 no .sdata or .sbss section in the output file, in which
2085 case we must change the name of the output section. */
2086 subname = o->name + sizeof ".gptab" - 1;
2087 if (bfd_get_section_by_name (abfd, subname) == NULL)
2088 {
2089 if (o == gptab_data_sec)
2090 o->name = ".gptab.data";
2091 else
2092 o->name = ".gptab.bss";
2093 subname = o->name + sizeof ".gptab" - 1;
2094 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
2095 }
2096
2097 /* Set up the first entry. */
2098 c = 1;
2099 tab = (Elf32_gptab *) malloc (c * sizeof (Elf32_gptab));
2100 if (tab == NULL)
2101 {
2102 bfd_set_error (bfd_error_no_memory);
2103 return false;
2104 }
2105 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
2106 tab[0].gt_header.gt_unused = 0;
2107
2108 /* Combine the input sections. */
2109 for (p = o->link_order_head;
2110 p != (struct bfd_link_order *) NULL;
2111 p = p->next)
2112 {
2113 asection *input_section;
2114 bfd *input_bfd;
2115 bfd_size_type size;
2116 unsigned long last;
2117 bfd_size_type gpentry;
2118
2119 if (p->type != bfd_indirect_link_order)
2120 {
2121 if (p->type == bfd_fill_link_order)
2122 continue;
2123 abort ();
2124 }
2125
2126 input_section = p->u.indirect.section;
2127 input_bfd = input_section->owner;
2128
2129 /* Combine the gptab entries for this input section one
2130 by one. We know that the input gptab entries are
2131 sorted by ascending -G value. */
2132 size = bfd_section_size (input_bfd, input_section);
2133 last = 0;
2134 for (gpentry = sizeof (Elf32_External_gptab);
2135 gpentry < size;
2136 gpentry += sizeof (Elf32_External_gptab))
2137 {
2138 Elf32_External_gptab ext_gptab;
2139 Elf32_gptab int_gptab;
2140 unsigned long val;
2141 unsigned long add;
2142 boolean exact;
2143 unsigned int look;
2144
2145 if (! (bfd_get_section_contents
2146 (input_bfd, input_section, (PTR) &ext_gptab,
2147 gpentry, sizeof (Elf32_External_gptab))))
2148 {
2149 free (tab);
2150 return false;
2151 }
2152
2153 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
2154 &int_gptab);
2155 val = int_gptab.gt_entry.gt_g_value;
2156 add = int_gptab.gt_entry.gt_bytes - last;
2157
2158 exact = false;
2159 for (look = 1; look < c; look++)
2160 {
2161 if (tab[look].gt_entry.gt_g_value >= val)
2162 tab[look].gt_entry.gt_bytes += add;
2163
2164 if (tab[look].gt_entry.gt_g_value == val)
2165 exact = true;
2166 }
2167
2168 if (! exact)
2169 {
2170 Elf32_gptab *new_tab;
2171 unsigned int max;
2172
2173 /* We need a new table entry. */
2174 new_tab = ((Elf32_gptab *)
2175 realloc ((PTR) tab,
2176 (c + 1) * sizeof (Elf32_gptab)));
2177 if (new_tab == NULL)
2178 {
2179 bfd_set_error (bfd_error_no_memory);
2180 free (tab);
2181 return false;
2182 }
2183 tab = new_tab;
2184 tab[c].gt_entry.gt_g_value = val;
2185 tab[c].gt_entry.gt_bytes = add;
2186
2187 /* Merge in the size for the next smallest -G
2188 value, since that will be implied by this new
2189 value. */
2190 max = 0;
2191 for (look = 1; look < c; look++)
2192 {
2193 if (tab[look].gt_entry.gt_g_value < val
2194 && (max == 0
2195 || (tab[look].gt_entry.gt_g_value
2196 > tab[max].gt_entry.gt_g_value)))
2197 max = look;
2198 }
2199 if (max != 0)
2200 tab[c].gt_entry.gt_bytes +=
2201 tab[max].gt_entry.gt_bytes;
2202
2203 ++c;
2204 }
2205
2206 last = int_gptab.gt_entry.gt_bytes;
2207 }
2208
2209 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2210 elf_link_input_bfd ignores this section. */
2211 input_section->flags &=~ SEC_HAS_CONTENTS;
2212 }
2213
2214 /* The table must be sorted by -G value. */
2215 if (c > 2)
2216 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
2217
2218 /* Swap out the table. */
2219 ext_tab = ((Elf32_External_gptab *)
2220 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
2221 if (ext_tab == NULL)
2222 {
2223 bfd_set_error (bfd_error_no_memory);
2224 free (tab);
2225 return false;
2226 }
2227
2228 for (i = 0; i < c; i++)
2229 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
2230 free (tab);
2231
2232 o->_raw_size = c * sizeof (Elf32_External_gptab);
2233 o->contents = (bfd_byte *) ext_tab;
2234
2235 /* Skip this section later on (I don't think this currently
2236 matters, but someday it might). */
2237 o->link_order_head = (struct bfd_link_order *) NULL;
2238 }
2239 }
2240
2241 /* Get a value for the GP register. */
2242 if (elf_gp (abfd) == 0)
2243 {
2244 struct bfd_link_hash_entry *h;
2245
2246 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
2247 if (h != (struct bfd_link_hash_entry *) NULL
2248 && h->type == bfd_link_hash_defined)
2249 elf_gp (abfd) = (h->u.def.value
2250 + h->u.def.section->output_section->vma
2251 + h->u.def.section->output_offset);
2252 else if (info->relocateable)
2253 {
2254 bfd_vma lo;
2255
2256 /* Make up a value. */
2257 lo = (bfd_vma) -1;
2258 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2259 {
2260 if (o->vma < lo
2261 && (strcmp (o->name, ".sbss") == 0
2262 || strcmp (o->name, ".sdata") == 0
2263 || strcmp (o->name, ".lit4") == 0
2264 || strcmp (o->name, ".lit8") == 0))
2265 lo = o->vma;
2266 }
2267 elf_gp (abfd) = lo + 0x8000;
2268 }
2269 else
2270 {
2271 /* If the relocate_section function needs to do a reloc
2272 involving the GP value, it should make a reloc_dangerous
2273 callback to warn that GP is not defined. */
2274 }
2275 }
2276
2277 /* Invoke the regular ELF backend linker to do all the work. */
2278 if (! bfd_elf32_bfd_final_link (abfd, info))
2279 return false;
2280
2281 /* Now write out the computed sections. */
2282
2283 if (reginfo_sec != (asection *) NULL)
2284 {
2285 Elf32_External_RegInfo ext;
2286
2287 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
2288 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
2289 (file_ptr) 0, sizeof ext))
2290 return false;
2291 }
2292
2293 if (mdebug_sec != (asection *) NULL)
2294 {
2295 BFD_ASSERT (abfd->output_has_begun);
2296 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
2297 swap, info,
2298 mdebug_sec->filepos))
2299 return false;
2300
2301 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
2302 }
2303
2304 if (gptab_data_sec != (asection *) NULL)
2305 {
2306 if (! bfd_set_section_contents (abfd, gptab_data_sec,
2307 gptab_data_sec->contents,
2308 (file_ptr) 0,
2309 gptab_data_sec->_raw_size))
2310 return false;
2311 }
2312
2313 if (gptab_bss_sec != (asection *) NULL)
2314 {
2315 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
2316 gptab_bss_sec->contents,
2317 (file_ptr) 0,
2318 gptab_bss_sec->_raw_size))
2319 return false;
2320 }
2321
2322 return true;
2323 }
2324
2325 /* Handle a MIPS ELF HI16 reloc. */
2326
2327 static void
2328 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
2329 bfd *input_bfd;
2330 Elf_Internal_Rela *relhi;
2331 Elf_Internal_Rela *rello;
2332 bfd_byte *contents;
2333 bfd_vma addend;
2334 {
2335 bfd_vma insn;
2336 bfd_vma addlo;
2337
2338 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
2339
2340 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
2341 addlo &= 0xffff;
2342
2343 addend += ((insn & 0xffff) << 16) + addlo;
2344
2345 if ((addlo & 0x8000) != 0)
2346 addend -= 0x10000;
2347 if ((addend & 0x8000) != 0)
2348 addend += 0x10000;
2349
2350 bfd_put_32 (input_bfd,
2351 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
2352 contents + relhi->r_offset);
2353 }
2354
2355 /* Relocate a MIPS ELF section. */
2356
2357 static boolean
2358 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2359 contents, relocs, local_syms, local_sections)
2360 bfd *output_bfd;
2361 struct bfd_link_info *info;
2362 bfd *input_bfd;
2363 asection *input_section;
2364 bfd_byte *contents;
2365 Elf_Internal_Rela *relocs;
2366 Elf_Internal_Sym *local_syms;
2367 asection **local_sections;
2368 {
2369 Elf_Internal_Shdr *symtab_hdr;
2370 size_t locsymcount;
2371 size_t extsymoff;
2372 Elf_Internal_Rela *rel;
2373 Elf_Internal_Rela *relend;
2374
2375 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2376
2377 if (elf_bad_symtab (input_bfd))
2378 {
2379 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
2380 extsymoff = 0;
2381 }
2382 else
2383 {
2384 locsymcount = symtab_hdr->sh_info;
2385 extsymoff = symtab_hdr->sh_info;
2386 }
2387
2388 rel = relocs;
2389 relend = relocs + input_section->reloc_count;
2390 for (; rel < relend; rel++)
2391 {
2392 int r_type;
2393 reloc_howto_type *howto;
2394 long r_symndx;
2395 bfd_vma addend;
2396 struct elf_link_hash_entry *h;
2397 asection *sec;
2398 Elf_Internal_Sym *sym;
2399 bfd_reloc_status_type r;
2400
2401 r_type = ELF32_R_TYPE (rel->r_info);
2402 if (r_type < 0 || r_type >= (int) R_MIPS_max)
2403 {
2404 bfd_set_error (bfd_error_bad_value);
2405 return false;
2406 }
2407 howto = elf_mips_howto_table + r_type;
2408
2409 r_symndx = ELF32_R_SYM (rel->r_info);
2410
2411 /* Mix in the change in GP address for a GP relative reloc. */
2412 if (r_type != R_MIPS_GPREL16
2413 && r_type != R_MIPS_LITERAL
2414 && r_type != R_MIPS_GPREL32)
2415 addend = 0;
2416 else
2417 {
2418 if (elf_gp (output_bfd) == 0)
2419 {
2420 if (! ((*info->callbacks->reloc_dangerous)
2421 (info,
2422 "GP relative relocation when GP not defined",
2423 input_bfd, input_section,
2424 rel->r_offset)))
2425 return false;
2426 /* Only give the error once per link. */
2427 elf_gp (output_bfd) = 4;
2428 }
2429
2430 if (r_symndx < extsymoff
2431 || (elf_bad_symtab (input_bfd)
2432 && local_sections[r_symndx] != NULL))
2433 {
2434 /* This is a relocation against a section. The current
2435 addend in the instruction is the difference between
2436 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
2437 must change this to be the difference between the
2438 final definition (which will end up in RELOCATION)
2439 and the GP value of OUTPUT_BFD (which is in GP). */
2440 addend = elf_gp (input_bfd) - elf_gp (output_bfd);
2441 }
2442 else if (! info->relocateable)
2443 {
2444 /* We are doing a final link. The current addend in the
2445 instruction is simply the desired offset into the
2446 symbol (normally zero). We want the instruction to
2447 hold the difference between the final definition of
2448 the symbol (which will end up in RELOCATION) and the
2449 GP value of OUTPUT_BFD (which is in GP). */
2450 addend = - elf_gp (output_bfd);
2451 }
2452 else
2453 {
2454 /* We are generating relocateable output, and we aren't
2455 going to define this symbol, so we just leave the
2456 instruction alone. */
2457 addend = 0;
2458 }
2459 }
2460
2461 h = NULL;
2462 sym = NULL;
2463 sec = NULL;
2464 if (info->relocateable)
2465 {
2466 /* This is a relocateable link. We don't have to change
2467 anything, unless the reloc is against a section symbol,
2468 in which case we have to adjust according to where the
2469 section symbol winds up in the output section. */
2470 if (r_symndx >= locsymcount
2471 || (elf_bad_symtab (input_bfd)
2472 && local_sections[r_symndx] == NULL))
2473 r = bfd_reloc_ok;
2474 else
2475 {
2476 sym = local_syms + r_symndx;
2477 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2478 r = bfd_reloc_ok;
2479 else
2480 {
2481 sec = local_sections[r_symndx];
2482
2483 /* It would be logical to add sym->st_value here,
2484 but Irix 5 sometimes generates a garbage symbol
2485 value. */
2486 addend += sec->output_offset;
2487
2488 /* If this is HI16 with an associated LO16, adjust
2489 the addend accordingly. Otherwise, just
2490 relocate. */
2491 if (r_type != R_MIPS_HI16
2492 || (rel + 1) >= relend
2493 || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
2494 r = _bfd_relocate_contents (howto, input_bfd,
2495 addend,
2496 contents + rel->r_offset);
2497 else
2498 {
2499 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
2500 contents, addend);
2501 r = bfd_reloc_ok;
2502 }
2503 }
2504 }
2505 }
2506 else
2507 {
2508 bfd_vma relocation;
2509
2510 /* This is a final link. */
2511 sym = NULL;
2512 if (r_symndx < extsymoff
2513 || (elf_bad_symtab (input_bfd)
2514 && local_sections[r_symndx] != NULL))
2515 {
2516 sym = local_syms + r_symndx;
2517 sec = local_sections[r_symndx];
2518 relocation = (sec->output_section->vma
2519 + sec->output_offset);
2520
2521 /* It would be logical to always add sym->st_value here,
2522 but Irix 5 sometimes generates a garbage symbol
2523 value. */
2524 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2525 relocation += sym->st_value;
2526 }
2527 else
2528 {
2529 long indx;
2530
2531 indx = r_symndx - extsymoff;
2532 h = elf_sym_hashes (input_bfd)[indx];
2533 if (h->root.type == bfd_link_hash_defined)
2534 {
2535 sec = h->root.u.def.section;
2536 relocation = (h->root.u.def.value
2537 + sec->output_section->vma
2538 + sec->output_offset);
2539 }
2540 else if (h->root.type == bfd_link_hash_weak)
2541 relocation = 0;
2542 else
2543 {
2544 if (! ((*info->callbacks->undefined_symbol)
2545 (info, h->root.root.string, input_bfd,
2546 input_section, rel->r_offset)))
2547 return false;
2548 relocation = 0;
2549 }
2550 }
2551
2552 if (r_type != R_MIPS_HI16
2553 || (rel + 1) >= relend
2554 || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
2555 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2556 contents, rel->r_offset,
2557 relocation, addend);
2558 else
2559 {
2560 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
2561 contents, relocation + addend);
2562 r = bfd_reloc_ok;
2563 }
2564 }
2565
2566 if (r != bfd_reloc_ok)
2567 {
2568 switch (r)
2569 {
2570 default:
2571 case bfd_reloc_outofrange:
2572 abort ();
2573 case bfd_reloc_overflow:
2574 {
2575 const char *name;
2576
2577 if (h != NULL)
2578 name = h->root.root.string;
2579 else
2580 {
2581 name = elf_string_from_elf_section (input_bfd,
2582 symtab_hdr->sh_link,
2583 sym->st_name);
2584 if (name == NULL)
2585 return false;
2586 if (*name == '\0')
2587 name = bfd_section_name (input_bfd, sec);
2588 }
2589 if (! ((*info->callbacks->reloc_overflow)
2590 (info, name, howto->name, (bfd_vma) 0,
2591 input_bfd, input_section, rel->r_offset)))
2592 return false;
2593 }
2594 break;
2595 }
2596 }
2597 }
2598
2599 return true;
2600 }
2601 \f
2602 /* ECOFF swapping routines. These are used when dealing with the
2603 .mdebug section, which is in the ECOFF debugging format. */
2604 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap =
2605 {
2606 /* Symbol table magic number. */
2607 magicSym,
2608 /* Alignment of debugging information. E.g., 4. */
2609 4,
2610 /* Sizes of external symbolic information. */
2611 sizeof (struct hdr_ext),
2612 sizeof (struct dnr_ext),
2613 sizeof (struct pdr_ext),
2614 sizeof (struct sym_ext),
2615 sizeof (struct opt_ext),
2616 sizeof (struct fdr_ext),
2617 sizeof (struct rfd_ext),
2618 sizeof (struct ext_ext),
2619 /* Functions to swap in external symbolic data. */
2620 ecoff_swap_hdr_in,
2621 ecoff_swap_dnr_in,
2622 ecoff_swap_pdr_in,
2623 ecoff_swap_sym_in,
2624 ecoff_swap_opt_in,
2625 ecoff_swap_fdr_in,
2626 ecoff_swap_rfd_in,
2627 ecoff_swap_ext_in,
2628 _bfd_ecoff_swap_tir_in,
2629 _bfd_ecoff_swap_rndx_in,
2630 /* Functions to swap out external symbolic data. */
2631 ecoff_swap_hdr_out,
2632 ecoff_swap_dnr_out,
2633 ecoff_swap_pdr_out,
2634 ecoff_swap_sym_out,
2635 ecoff_swap_opt_out,
2636 ecoff_swap_fdr_out,
2637 ecoff_swap_rfd_out,
2638 ecoff_swap_ext_out,
2639 _bfd_ecoff_swap_tir_out,
2640 _bfd_ecoff_swap_rndx_out,
2641 /* Function to read in symbolic data. */
2642 mips_elf_read_ecoff_info
2643 };
2644 \f
2645 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
2646 #define TARGET_LITTLE_NAME "elf32-littlemips"
2647 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
2648 #define TARGET_BIG_NAME "elf32-bigmips"
2649 #define ELF_ARCH bfd_arch_mips
2650 #define ELF_MACHINE_CODE EM_MIPS
2651 #define ELF_MAXPAGESIZE 0x10000
2652 #define elf_backend_collect true
2653 #define elf_info_to_howto 0
2654 #define elf_info_to_howto_rel mips_info_to_howto_rel
2655 #define elf_backend_sym_is_global mips_elf_sym_is_global
2656 #define elf_backend_object_p mips_elf_object_p
2657 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
2658 #define elf_backend_fake_sections mips_elf_fake_sections
2659 #define elf_backend_section_from_bfd_section \
2660 mips_elf_section_from_bfd_section
2661 #define elf_backend_section_processing mips_elf_section_processing
2662 #define elf_backend_symbol_processing mips_elf_symbol_processing
2663 #define elf_backend_final_write_processing \
2664 mips_elf_final_write_processing
2665 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
2666
2667 #define bfd_elf32_bfd_link_hash_table_create \
2668 mips_elf_link_hash_table_create
2669 #define bfd_elf32_bfd_final_link mips_elf_final_link
2670 #define elf_backend_relocate_section mips_elf_relocate_section
2671 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
2672
2673 #include "elf32-target.h"
This page took 0.084422 seconds and 4 git commands to generate.