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