* elf32-mips.c (mips_elf_output_extsym): Set the value of the
[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 reloc_howto_type *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 reloc_howto_type *
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 if (h->root.root.type == bfd_link_hash_common)
1723 h->esym.asym.value = h->root.root.u.c.size;
1724 else if (h->root.root.type == bfd_link_hash_defined)
1725 {
1726 asection *sec;
1727
1728 if (h->esym.asym.sc == scCommon)
1729 h->esym.asym.sc = scBss;
1730 else if (h->esym.asym.sc == scSCommon)
1731 h->esym.asym.sc = scSBss;
1732
1733 sec = h->root.root.u.def.section;
1734 h->esym.asym.value = (h->root.root.u.def.value
1735 + sec->output_offset
1736 + sec->output_section->vma);
1737 }
1738
1739 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1740 h->root.root.root.string,
1741 &h->esym))
1742 {
1743 einfo->failed = true;
1744 return false;
1745 }
1746
1747 return true;
1748 }
1749
1750 /* A comparison routine used to sort .gptab entries. */
1751
1752 static int
1753 gptab_compare (p1, p2)
1754 const PTR p1;
1755 const PTR p2;
1756 {
1757 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
1758 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
1759
1760 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1761 }
1762
1763 /* We need to use a special link routine to handle the .reginfo and
1764 the .mdebug sections. We need to merge all instances of these
1765 sections together, not write them all out sequentially. */
1766
1767 static boolean
1768 mips_elf_final_link (abfd, info)
1769 bfd *abfd;
1770 struct bfd_link_info *info;
1771 {
1772 asection **secpp;
1773 asection *o;
1774 struct bfd_link_order *p;
1775 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
1776 Elf32_RegInfo reginfo;
1777 struct ecoff_debug_info debug;
1778 const struct ecoff_debug_swap *swap
1779 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1780 HDRR *symhdr = &debug.symbolic_header;
1781 PTR mdebug_handle = NULL;
1782
1783 /* Drop the .options section, since it has special semantics which I
1784 haven't bothered to figure out. */
1785 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
1786 {
1787 if (strcmp ((*secpp)->name, ".options") == 0)
1788 {
1789 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
1790 if (p->type == bfd_indirect_link_order)
1791 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
1792 (*secpp)->link_order_head = NULL;
1793 *secpp = (*secpp)->next;
1794 --abfd->section_count;
1795 break;
1796 }
1797 }
1798
1799 /* Go through the sections and collect the .reginfo and .mdebug
1800 information. */
1801 reginfo_sec = NULL;
1802 mdebug_sec = NULL;
1803 gptab_data_sec = NULL;
1804 gptab_bss_sec = NULL;
1805 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
1806 {
1807 if (strcmp (o->name, ".reginfo") == 0)
1808 {
1809 memset (&reginfo, 0, sizeof reginfo);
1810
1811 /* We have found the .reginfo section in the output file.
1812 Look through all the link_orders comprising it and merge
1813 the information together. */
1814 for (p = o->link_order_head;
1815 p != (struct bfd_link_order *) NULL;
1816 p = p->next)
1817 {
1818 asection *input_section;
1819 bfd *input_bfd;
1820 Elf32_External_RegInfo ext;
1821 Elf32_RegInfo sub;
1822
1823 if (p->type != bfd_indirect_link_order)
1824 {
1825 if (p->type == bfd_fill_link_order)
1826 continue;
1827 abort ();
1828 }
1829
1830 input_section = p->u.indirect.section;
1831 input_bfd = input_section->owner;
1832 BFD_ASSERT (input_section->_raw_size
1833 == sizeof (Elf32_External_RegInfo));
1834 if (! bfd_get_section_contents (input_bfd, input_section,
1835 (PTR) &ext,
1836 (file_ptr) 0,
1837 sizeof ext))
1838 return false;
1839
1840 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
1841
1842 reginfo.ri_gprmask |= sub.ri_gprmask;
1843 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
1844 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
1845 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
1846 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
1847
1848 /* ri_gp_value is set by the function
1849 mips_elf_section_processing when the section is
1850 finally written out. */
1851
1852 /* Hack: reset the SEC_HAS_CONTENTS flag so that
1853 elf_link_input_bfd ignores this section. */
1854 input_section->flags &=~ SEC_HAS_CONTENTS;
1855 }
1856
1857 /* Force the section size to the value we want. */
1858 o->_raw_size = sizeof (Elf32_External_RegInfo);
1859
1860 /* Skip this section later on (I don't think this currently
1861 matters, but someday it might). */
1862 o->link_order_head = (struct bfd_link_order *) NULL;
1863
1864 reginfo_sec = o;
1865 }
1866
1867 if (strcmp (o->name, ".mdebug") == 0)
1868 {
1869 struct extsym_info einfo;
1870
1871 /* We have found the .mdebug section in the output file.
1872 Look through all the link_orders comprising it and merge
1873 the information together. */
1874 symhdr->magic = swap->sym_magic;
1875 /* FIXME: What should the version stamp be? */
1876 symhdr->vstamp = 0;
1877 symhdr->ilineMax = 0;
1878 symhdr->cbLine = 0;
1879 symhdr->idnMax = 0;
1880 symhdr->ipdMax = 0;
1881 symhdr->isymMax = 0;
1882 symhdr->ioptMax = 0;
1883 symhdr->iauxMax = 0;
1884 symhdr->issMax = 0;
1885 symhdr->issExtMax = 0;
1886 symhdr->ifdMax = 0;
1887 symhdr->crfd = 0;
1888 symhdr->iextMax = 0;
1889
1890 /* We accumulate the debugging information itself in the
1891 debug_info structure. */
1892 debug.line = NULL;
1893 debug.external_dnr = NULL;
1894 debug.external_pdr = NULL;
1895 debug.external_sym = NULL;
1896 debug.external_opt = NULL;
1897 debug.external_aux = NULL;
1898 debug.ss = NULL;
1899 debug.ssext = debug.ssext_end = NULL;
1900 debug.external_fdr = NULL;
1901 debug.external_rfd = NULL;
1902 debug.external_ext = debug.external_ext_end = NULL;
1903
1904 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
1905 if (mdebug_handle == (PTR) NULL)
1906 return false;
1907
1908 for (p = o->link_order_head;
1909 p != (struct bfd_link_order *) NULL;
1910 p = p->next)
1911 {
1912 asection *input_section;
1913 bfd *input_bfd;
1914 const struct ecoff_debug_swap *input_swap;
1915 struct ecoff_debug_info input_debug;
1916 char *eraw_src;
1917 char *eraw_end;
1918
1919 if (p->type != bfd_indirect_link_order)
1920 {
1921 if (p->type == bfd_fill_link_order)
1922 continue;
1923 abort ();
1924 }
1925
1926 input_section = p->u.indirect.section;
1927 input_bfd = input_section->owner;
1928
1929 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
1930 || (get_elf_backend_data (input_bfd)
1931 ->elf_backend_ecoff_debug_swap) == NULL)
1932 {
1933 /* I don't know what a non MIPS ELF bfd would be
1934 doing with a .mdebug section, but I don't really
1935 want to deal with it. */
1936 continue;
1937 }
1938
1939 input_swap = (get_elf_backend_data (input_bfd)
1940 ->elf_backend_ecoff_debug_swap);
1941
1942 BFD_ASSERT (p->size == input_section->_raw_size);
1943
1944 /* The ECOFF linking code expects that we have already
1945 read in the debugging information and set up an
1946 ecoff_debug_info structure, so we do that now. */
1947 if (! mips_elf_read_ecoff_info (input_bfd, input_section,
1948 &input_debug))
1949 return false;
1950
1951 if (! (bfd_ecoff_debug_accumulate
1952 (mdebug_handle, abfd, &debug, swap, input_bfd,
1953 &input_debug, input_swap, info)))
1954 return false;
1955
1956 /* Loop through the external symbols. For each one with
1957 interesting information, try to find the symbol in
1958 the linker global hash table and save the information
1959 for the output external symbols. */
1960 eraw_src = input_debug.external_ext;
1961 eraw_end = (eraw_src
1962 + (input_debug.symbolic_header.iextMax
1963 * input_swap->external_ext_size));
1964 for (;
1965 eraw_src < eraw_end;
1966 eraw_src += input_swap->external_ext_size)
1967 {
1968 EXTR ext;
1969 const char *name;
1970 struct mips_elf_link_hash_entry *h;
1971
1972 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
1973 if (ext.asym.sc == scNil
1974 || ext.asym.sc == scUndefined
1975 || ext.asym.sc == scSUndefined)
1976 continue;
1977
1978 name = input_debug.ssext + ext.asym.iss;
1979 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
1980 name, false, false, true);
1981 if (h == NULL || h->esym.ifd != -2)
1982 continue;
1983
1984 if (ext.ifd != -1)
1985 {
1986 BFD_ASSERT (ext.ifd
1987 < input_debug.symbolic_header.ifdMax);
1988 ext.ifd = input_debug.ifdmap[ext.ifd];
1989 }
1990
1991 h->esym = ext;
1992 }
1993
1994 /* Free up the information we just read. */
1995 free (input_debug.line);
1996 free (input_debug.external_dnr);
1997 free (input_debug.external_pdr);
1998 free (input_debug.external_sym);
1999 free (input_debug.external_opt);
2000 free (input_debug.external_aux);
2001 free (input_debug.ss);
2002 free (input_debug.ssext);
2003 free (input_debug.external_fdr);
2004 free (input_debug.external_rfd);
2005 free (input_debug.external_ext);
2006
2007 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2008 elf_link_input_bfd ignores this section. */
2009 input_section->flags &=~ SEC_HAS_CONTENTS;
2010 }
2011
2012 /* Build the external symbol information. */
2013 einfo.abfd = abfd;
2014 einfo.info = info;
2015 einfo.debug = &debug;
2016 einfo.swap = swap;
2017 einfo.failed = false;
2018 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
2019 mips_elf_output_extsym,
2020 (PTR) &einfo);
2021 if (einfo.failed)
2022 return false;
2023
2024 /* Set the size of the .mdebug section. */
2025 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
2026
2027 /* Skip this section later on (I don't think this currently
2028 matters, but someday it might). */
2029 o->link_order_head = (struct bfd_link_order *) NULL;
2030
2031 mdebug_sec = o;
2032 }
2033
2034 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
2035 {
2036 const char *subname;
2037 unsigned int c;
2038 Elf32_gptab *tab;
2039 Elf32_External_gptab *ext_tab;
2040 unsigned int i;
2041
2042 /* The .gptab.sdata and .gptab.sbss sections hold
2043 information describing how the small data area would
2044 change depending upon the -G switch. These sections
2045 not used in executables files. */
2046 if (! info->relocateable)
2047 {
2048 asection **secpp;
2049
2050 for (p = o->link_order_head;
2051 p != (struct bfd_link_order *) NULL;
2052 p = p->next)
2053 {
2054 asection *input_section;
2055
2056 if (p->type != bfd_indirect_link_order)
2057 {
2058 if (p->type == bfd_fill_link_order)
2059 continue;
2060 abort ();
2061 }
2062
2063 input_section = p->u.indirect.section;
2064
2065 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2066 elf_link_input_bfd ignores this section. */
2067 input_section->flags &=~ SEC_HAS_CONTENTS;
2068 }
2069
2070 /* Skip this section later on (I don't think this
2071 currently matters, but someday it might). */
2072 o->link_order_head = (struct bfd_link_order *) NULL;
2073
2074 /* Really remove the section. */
2075 for (secpp = &abfd->sections;
2076 *secpp != o;
2077 secpp = &(*secpp)->next)
2078 ;
2079 *secpp = (*secpp)->next;
2080 --abfd->section_count;
2081
2082 continue;
2083 }
2084
2085 /* There is one gptab for initialized data, and one for
2086 uninitialized data. */
2087 if (strcmp (o->name, ".gptab.sdata") == 0)
2088 gptab_data_sec = o;
2089 else if (strcmp (o->name, ".gptab.sbss") == 0)
2090 gptab_bss_sec = o;
2091 else
2092 {
2093 bfd_set_error (bfd_error_nonrepresentable_section);
2094 return false;
2095 }
2096
2097 /* The linker script always combines .gptab.data and
2098 .gptab.sdata into .gptab.sdata, and likewise for
2099 .gptab.bss and .gptab.sbss. It is possible that there is
2100 no .sdata or .sbss section in the output file, in which
2101 case we must change the name of the output section. */
2102 subname = o->name + sizeof ".gptab" - 1;
2103 if (bfd_get_section_by_name (abfd, subname) == NULL)
2104 {
2105 if (o == gptab_data_sec)
2106 o->name = ".gptab.data";
2107 else
2108 o->name = ".gptab.bss";
2109 subname = o->name + sizeof ".gptab" - 1;
2110 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
2111 }
2112
2113 /* Set up the first entry. */
2114 c = 1;
2115 tab = (Elf32_gptab *) malloc (c * sizeof (Elf32_gptab));
2116 if (tab == NULL)
2117 {
2118 bfd_set_error (bfd_error_no_memory);
2119 return false;
2120 }
2121 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
2122 tab[0].gt_header.gt_unused = 0;
2123
2124 /* Combine the input sections. */
2125 for (p = o->link_order_head;
2126 p != (struct bfd_link_order *) NULL;
2127 p = p->next)
2128 {
2129 asection *input_section;
2130 bfd *input_bfd;
2131 bfd_size_type size;
2132 unsigned long last;
2133 bfd_size_type gpentry;
2134
2135 if (p->type != bfd_indirect_link_order)
2136 {
2137 if (p->type == bfd_fill_link_order)
2138 continue;
2139 abort ();
2140 }
2141
2142 input_section = p->u.indirect.section;
2143 input_bfd = input_section->owner;
2144
2145 /* Combine the gptab entries for this input section one
2146 by one. We know that the input gptab entries are
2147 sorted by ascending -G value. */
2148 size = bfd_section_size (input_bfd, input_section);
2149 last = 0;
2150 for (gpentry = sizeof (Elf32_External_gptab);
2151 gpentry < size;
2152 gpentry += sizeof (Elf32_External_gptab))
2153 {
2154 Elf32_External_gptab ext_gptab;
2155 Elf32_gptab int_gptab;
2156 unsigned long val;
2157 unsigned long add;
2158 boolean exact;
2159 unsigned int look;
2160
2161 if (! (bfd_get_section_contents
2162 (input_bfd, input_section, (PTR) &ext_gptab,
2163 gpentry, sizeof (Elf32_External_gptab))))
2164 {
2165 free (tab);
2166 return false;
2167 }
2168
2169 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
2170 &int_gptab);
2171 val = int_gptab.gt_entry.gt_g_value;
2172 add = int_gptab.gt_entry.gt_bytes - last;
2173
2174 exact = false;
2175 for (look = 1; look < c; look++)
2176 {
2177 if (tab[look].gt_entry.gt_g_value >= val)
2178 tab[look].gt_entry.gt_bytes += add;
2179
2180 if (tab[look].gt_entry.gt_g_value == val)
2181 exact = true;
2182 }
2183
2184 if (! exact)
2185 {
2186 Elf32_gptab *new_tab;
2187 unsigned int max;
2188
2189 /* We need a new table entry. */
2190 new_tab = ((Elf32_gptab *)
2191 realloc ((PTR) tab,
2192 (c + 1) * sizeof (Elf32_gptab)));
2193 if (new_tab == NULL)
2194 {
2195 bfd_set_error (bfd_error_no_memory);
2196 free (tab);
2197 return false;
2198 }
2199 tab = new_tab;
2200 tab[c].gt_entry.gt_g_value = val;
2201 tab[c].gt_entry.gt_bytes = add;
2202
2203 /* Merge in the size for the next smallest -G
2204 value, since that will be implied by this new
2205 value. */
2206 max = 0;
2207 for (look = 1; look < c; look++)
2208 {
2209 if (tab[look].gt_entry.gt_g_value < val
2210 && (max == 0
2211 || (tab[look].gt_entry.gt_g_value
2212 > tab[max].gt_entry.gt_g_value)))
2213 max = look;
2214 }
2215 if (max != 0)
2216 tab[c].gt_entry.gt_bytes +=
2217 tab[max].gt_entry.gt_bytes;
2218
2219 ++c;
2220 }
2221
2222 last = int_gptab.gt_entry.gt_bytes;
2223 }
2224
2225 /* Hack: reset the SEC_HAS_CONTENTS flag so that
2226 elf_link_input_bfd ignores this section. */
2227 input_section->flags &=~ SEC_HAS_CONTENTS;
2228 }
2229
2230 /* The table must be sorted by -G value. */
2231 if (c > 2)
2232 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
2233
2234 /* Swap out the table. */
2235 ext_tab = ((Elf32_External_gptab *)
2236 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
2237 if (ext_tab == NULL)
2238 {
2239 bfd_set_error (bfd_error_no_memory);
2240 free (tab);
2241 return false;
2242 }
2243
2244 for (i = 0; i < c; i++)
2245 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
2246 free (tab);
2247
2248 o->_raw_size = c * sizeof (Elf32_External_gptab);
2249 o->contents = (bfd_byte *) ext_tab;
2250
2251 /* Skip this section later on (I don't think this currently
2252 matters, but someday it might). */
2253 o->link_order_head = (struct bfd_link_order *) NULL;
2254 }
2255 }
2256
2257 /* Get a value for the GP register. */
2258 if (elf_gp (abfd) == 0)
2259 {
2260 struct bfd_link_hash_entry *h;
2261
2262 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
2263 if (h != (struct bfd_link_hash_entry *) NULL
2264 && h->type == bfd_link_hash_defined)
2265 elf_gp (abfd) = (h->u.def.value
2266 + h->u.def.section->output_section->vma
2267 + h->u.def.section->output_offset);
2268 else if (info->relocateable)
2269 {
2270 bfd_vma lo;
2271
2272 /* Make up a value. */
2273 lo = (bfd_vma) -1;
2274 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2275 {
2276 if (o->vma < lo
2277 && (strcmp (o->name, ".sbss") == 0
2278 || strcmp (o->name, ".sdata") == 0
2279 || strcmp (o->name, ".lit4") == 0
2280 || strcmp (o->name, ".lit8") == 0))
2281 lo = o->vma;
2282 }
2283 elf_gp (abfd) = lo + 0x8000;
2284 }
2285 else
2286 {
2287 /* If the relocate_section function needs to do a reloc
2288 involving the GP value, it should make a reloc_dangerous
2289 callback to warn that GP is not defined. */
2290 }
2291 }
2292
2293 /* Invoke the regular ELF backend linker to do all the work. */
2294 if (! bfd_elf32_bfd_final_link (abfd, info))
2295 return false;
2296
2297 /* Now write out the computed sections. */
2298
2299 if (reginfo_sec != (asection *) NULL)
2300 {
2301 Elf32_External_RegInfo ext;
2302
2303 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
2304 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
2305 (file_ptr) 0, sizeof ext))
2306 return false;
2307 }
2308
2309 if (mdebug_sec != (asection *) NULL)
2310 {
2311 BFD_ASSERT (abfd->output_has_begun);
2312 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
2313 swap, info,
2314 mdebug_sec->filepos))
2315 return false;
2316
2317 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
2318 }
2319
2320 if (gptab_data_sec != (asection *) NULL)
2321 {
2322 if (! bfd_set_section_contents (abfd, gptab_data_sec,
2323 gptab_data_sec->contents,
2324 (file_ptr) 0,
2325 gptab_data_sec->_raw_size))
2326 return false;
2327 }
2328
2329 if (gptab_bss_sec != (asection *) NULL)
2330 {
2331 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
2332 gptab_bss_sec->contents,
2333 (file_ptr) 0,
2334 gptab_bss_sec->_raw_size))
2335 return false;
2336 }
2337
2338 return true;
2339 }
2340
2341 /* Handle a MIPS ELF HI16 reloc. */
2342
2343 static void
2344 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
2345 bfd *input_bfd;
2346 Elf_Internal_Rela *relhi;
2347 Elf_Internal_Rela *rello;
2348 bfd_byte *contents;
2349 bfd_vma addend;
2350 {
2351 bfd_vma insn;
2352 bfd_vma addlo;
2353
2354 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
2355
2356 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
2357 addlo &= 0xffff;
2358
2359 addend += ((insn & 0xffff) << 16) + addlo;
2360
2361 if ((addlo & 0x8000) != 0)
2362 addend -= 0x10000;
2363 if ((addend & 0x8000) != 0)
2364 addend += 0x10000;
2365
2366 bfd_put_32 (input_bfd,
2367 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
2368 contents + relhi->r_offset);
2369 }
2370
2371 /* Relocate a MIPS ELF section. */
2372
2373 static boolean
2374 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2375 contents, relocs, local_syms, local_sections)
2376 bfd *output_bfd;
2377 struct bfd_link_info *info;
2378 bfd *input_bfd;
2379 asection *input_section;
2380 bfd_byte *contents;
2381 Elf_Internal_Rela *relocs;
2382 Elf_Internal_Sym *local_syms;
2383 asection **local_sections;
2384 {
2385 Elf_Internal_Shdr *symtab_hdr;
2386 size_t locsymcount;
2387 size_t extsymoff;
2388 Elf_Internal_Rela *rel;
2389 Elf_Internal_Rela *relend;
2390
2391 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2392
2393 if (elf_bad_symtab (input_bfd))
2394 {
2395 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
2396 extsymoff = 0;
2397 }
2398 else
2399 {
2400 locsymcount = symtab_hdr->sh_info;
2401 extsymoff = symtab_hdr->sh_info;
2402 }
2403
2404 rel = relocs;
2405 relend = relocs + input_section->reloc_count;
2406 for (; rel < relend; rel++)
2407 {
2408 int r_type;
2409 reloc_howto_type *howto;
2410 long r_symndx;
2411 bfd_vma addend;
2412 struct elf_link_hash_entry *h;
2413 asection *sec;
2414 Elf_Internal_Sym *sym;
2415 bfd_reloc_status_type r;
2416
2417 r_type = ELF32_R_TYPE (rel->r_info);
2418 if (r_type < 0 || r_type >= (int) R_MIPS_max)
2419 {
2420 bfd_set_error (bfd_error_bad_value);
2421 return false;
2422 }
2423 howto = elf_mips_howto_table + r_type;
2424
2425 r_symndx = ELF32_R_SYM (rel->r_info);
2426
2427 /* Mix in the change in GP address for a GP relative reloc. */
2428 if (r_type != R_MIPS_GPREL16
2429 && r_type != R_MIPS_LITERAL
2430 && r_type != R_MIPS_GPREL32)
2431 addend = 0;
2432 else
2433 {
2434 if (elf_gp (output_bfd) == 0)
2435 {
2436 if (! ((*info->callbacks->reloc_dangerous)
2437 (info,
2438 "GP relative relocation when GP not defined",
2439 input_bfd, input_section,
2440 rel->r_offset)))
2441 return false;
2442 /* Only give the error once per link. */
2443 elf_gp (output_bfd) = 4;
2444 }
2445
2446 if (r_symndx < extsymoff
2447 || (elf_bad_symtab (input_bfd)
2448 && local_sections[r_symndx] != NULL))
2449 {
2450 /* This is a relocation against a section. The current
2451 addend in the instruction is the difference between
2452 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
2453 must change this to be the difference between the
2454 final definition (which will end up in RELOCATION)
2455 and the GP value of OUTPUT_BFD (which is in GP). */
2456 addend = elf_gp (input_bfd) - elf_gp (output_bfd);
2457 }
2458 else if (! info->relocateable)
2459 {
2460 /* We are doing a final link. The current addend in the
2461 instruction is simply the desired offset into the
2462 symbol (normally zero). We want the instruction to
2463 hold the difference between the final definition of
2464 the symbol (which will end up in RELOCATION) and the
2465 GP value of OUTPUT_BFD (which is in GP). */
2466 addend = - elf_gp (output_bfd);
2467 }
2468 else
2469 {
2470 /* We are generating relocateable output, and we aren't
2471 going to define this symbol, so we just leave the
2472 instruction alone. */
2473 addend = 0;
2474 }
2475 }
2476
2477 h = NULL;
2478 sym = NULL;
2479 sec = NULL;
2480 if (info->relocateable)
2481 {
2482 /* This is a relocateable link. We don't have to change
2483 anything, unless the reloc is against a section symbol,
2484 in which case we have to adjust according to where the
2485 section symbol winds up in the output section. */
2486 if (r_symndx >= locsymcount
2487 || (elf_bad_symtab (input_bfd)
2488 && local_sections[r_symndx] == NULL))
2489 r = bfd_reloc_ok;
2490 else
2491 {
2492 sym = local_syms + r_symndx;
2493 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2494 r = bfd_reloc_ok;
2495 else
2496 {
2497 sec = local_sections[r_symndx];
2498
2499 /* It would be logical to add sym->st_value here,
2500 but Irix 5 sometimes generates a garbage symbol
2501 value. */
2502 addend += sec->output_offset;
2503
2504 /* If this is HI16 with an associated LO16, adjust
2505 the addend accordingly. Otherwise, just
2506 relocate. */
2507 if (r_type != R_MIPS_HI16
2508 || (rel + 1) >= relend
2509 || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
2510 r = _bfd_relocate_contents (howto, input_bfd,
2511 addend,
2512 contents + rel->r_offset);
2513 else
2514 {
2515 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
2516 contents, addend);
2517 r = bfd_reloc_ok;
2518 }
2519 }
2520 }
2521 }
2522 else
2523 {
2524 bfd_vma relocation;
2525
2526 /* This is a final link. */
2527 sym = NULL;
2528 if (r_symndx < extsymoff
2529 || (elf_bad_symtab (input_bfd)
2530 && local_sections[r_symndx] != NULL))
2531 {
2532 sym = local_syms + r_symndx;
2533 sec = local_sections[r_symndx];
2534 relocation = (sec->output_section->vma
2535 + sec->output_offset);
2536
2537 /* It would be logical to always add sym->st_value here,
2538 but Irix 5 sometimes generates a garbage symbol
2539 value. */
2540 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2541 relocation += sym->st_value;
2542 }
2543 else
2544 {
2545 long indx;
2546
2547 indx = r_symndx - extsymoff;
2548 h = elf_sym_hashes (input_bfd)[indx];
2549 if (h->root.type == bfd_link_hash_defined)
2550 {
2551 sec = h->root.u.def.section;
2552 relocation = (h->root.u.def.value
2553 + sec->output_section->vma
2554 + sec->output_offset);
2555 }
2556 else if (h->root.type == bfd_link_hash_weak)
2557 relocation = 0;
2558 else
2559 {
2560 if (! ((*info->callbacks->undefined_symbol)
2561 (info, h->root.root.string, input_bfd,
2562 input_section, rel->r_offset)))
2563 return false;
2564 relocation = 0;
2565 }
2566 }
2567
2568 if (r_type != R_MIPS_HI16
2569 || (rel + 1) >= relend
2570 || ELF32_R_TYPE ((rel + 1)->r_info) != R_MIPS_LO16)
2571 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2572 contents, rel->r_offset,
2573 relocation, addend);
2574 else
2575 {
2576 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1,
2577 contents, relocation + addend);
2578 r = bfd_reloc_ok;
2579 }
2580 }
2581
2582 if (r != bfd_reloc_ok)
2583 {
2584 switch (r)
2585 {
2586 default:
2587 case bfd_reloc_outofrange:
2588 abort ();
2589 case bfd_reloc_overflow:
2590 {
2591 const char *name;
2592
2593 if (h != NULL)
2594 name = h->root.root.string;
2595 else
2596 {
2597 name = elf_string_from_elf_section (input_bfd,
2598 symtab_hdr->sh_link,
2599 sym->st_name);
2600 if (name == NULL)
2601 return false;
2602 if (*name == '\0')
2603 name = bfd_section_name (input_bfd, sec);
2604 }
2605 if (! ((*info->callbacks->reloc_overflow)
2606 (info, name, howto->name, (bfd_vma) 0,
2607 input_bfd, input_section, rel->r_offset)))
2608 return false;
2609 }
2610 break;
2611 }
2612 }
2613 }
2614
2615 return true;
2616 }
2617 \f
2618 /* ECOFF swapping routines. These are used when dealing with the
2619 .mdebug section, which is in the ECOFF debugging format. */
2620 static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap =
2621 {
2622 /* Symbol table magic number. */
2623 magicSym,
2624 /* Alignment of debugging information. E.g., 4. */
2625 4,
2626 /* Sizes of external symbolic information. */
2627 sizeof (struct hdr_ext),
2628 sizeof (struct dnr_ext),
2629 sizeof (struct pdr_ext),
2630 sizeof (struct sym_ext),
2631 sizeof (struct opt_ext),
2632 sizeof (struct fdr_ext),
2633 sizeof (struct rfd_ext),
2634 sizeof (struct ext_ext),
2635 /* Functions to swap in external symbolic data. */
2636 ecoff_swap_hdr_in,
2637 ecoff_swap_dnr_in,
2638 ecoff_swap_pdr_in,
2639 ecoff_swap_sym_in,
2640 ecoff_swap_opt_in,
2641 ecoff_swap_fdr_in,
2642 ecoff_swap_rfd_in,
2643 ecoff_swap_ext_in,
2644 _bfd_ecoff_swap_tir_in,
2645 _bfd_ecoff_swap_rndx_in,
2646 /* Functions to swap out external symbolic data. */
2647 ecoff_swap_hdr_out,
2648 ecoff_swap_dnr_out,
2649 ecoff_swap_pdr_out,
2650 ecoff_swap_sym_out,
2651 ecoff_swap_opt_out,
2652 ecoff_swap_fdr_out,
2653 ecoff_swap_rfd_out,
2654 ecoff_swap_ext_out,
2655 _bfd_ecoff_swap_tir_out,
2656 _bfd_ecoff_swap_rndx_out,
2657 /* Function to read in symbolic data. */
2658 mips_elf_read_ecoff_info
2659 };
2660 \f
2661 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
2662 #define TARGET_LITTLE_NAME "elf32-littlemips"
2663 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
2664 #define TARGET_BIG_NAME "elf32-bigmips"
2665 #define ELF_ARCH bfd_arch_mips
2666 #define ELF_MACHINE_CODE EM_MIPS
2667 #define ELF_MAXPAGESIZE 0x10000
2668 #define elf_backend_collect true
2669 #define elf_info_to_howto 0
2670 #define elf_info_to_howto_rel mips_info_to_howto_rel
2671 #define elf_backend_sym_is_global mips_elf_sym_is_global
2672 #define elf_backend_object_p mips_elf_object_p
2673 #define elf_backend_section_from_shdr mips_elf_section_from_shdr
2674 #define elf_backend_fake_sections mips_elf_fake_sections
2675 #define elf_backend_section_from_bfd_section \
2676 mips_elf_section_from_bfd_section
2677 #define elf_backend_section_processing mips_elf_section_processing
2678 #define elf_backend_symbol_processing mips_elf_symbol_processing
2679 #define elf_backend_final_write_processing \
2680 mips_elf_final_write_processing
2681 #define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
2682
2683 #define bfd_elf32_bfd_link_hash_table_create \
2684 mips_elf_link_hash_table_create
2685 #define bfd_elf32_bfd_final_link mips_elf_final_link
2686 #define elf_backend_relocate_section mips_elf_relocate_section
2687 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
2688
2689 #include "elf32-target.h"