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