comment on v9 stuff
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
CommitLineData
b3c0fc57 1/* MIPS-specific support for 32-bit ELF
9783e04a 2 Copyright 1993, 1994 Free Software Foundation, Inc.
6b4b4d17 3
b3c0fc57
ILT
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6
6b4b4d17
JK
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23#include "bfd.h"
24#include "sysdep.h"
25#include "libbfd.h"
6e07e54f
ILT
26#include "bfdlink.h"
27#include "genlink.h"
6b4b4d17 28#include "libelf.h"
b3c0fc57
ILT
29#include "elf/mips.h"
30
6e07e54f
ILT
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
b3c0fc57
ILT
40static bfd_reloc_status_type mips_elf_hi16_reloc PARAMS ((bfd *abfd,
41 arelent *reloc,
42 asymbol *symbol,
43 PTR data,
44 asection *section,
6e07e54f
ILT
45 bfd *output_bfd,
46 char **error));
47static 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));
b3c0fc57
ILT
54static bfd_reloc_status_type mips_elf_lo16_reloc PARAMS ((bfd *abfd,
55 arelent *reloc,
56 asymbol *symbol,
57 PTR data,
58 asection *section,
6e07e54f
ILT
59 bfd *output_bfd,
60 char **error));
b3c0fc57
ILT
61static bfd_reloc_status_type mips_elf_gprel16_reloc PARAMS ((bfd *abfd,
62 arelent *reloc,
63 asymbol *symbol,
64 PTR data,
65 asection *section,
6e07e54f
ILT
66 bfd *output_bfd,
67 char **error));
b3c0fc57
ILT
68
69#define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
70
71enum reloc_type
72{
73 R_MIPS_NONE = 0,
74 R_MIPS_16, R_MIPS_32,
75 R_MIPS_REL32, R_MIPS_26,
76 R_MIPS_HI16, R_MIPS_LO16,
77 R_MIPS_GPREL16, R_MIPS_LITERAL,
78 R_MIPS_GOT16, R_MIPS_PC16,
79 R_MIPS_CALL16, R_MIPS_GPREL32,
80 R_MIPS_max
81};
82
83static reloc_howto_type elf_mips_howto_table[] =
84{
85 /* No relocation. */
86 HOWTO (R_MIPS_NONE, /* type */
87 0, /* rightshift */
88 0, /* size (0 = byte, 1 = short, 2 = long) */
89 0, /* bitsize */
90 false, /* pc_relative */
91 0, /* bitpos */
92 complain_overflow_dont, /* complain_on_overflow */
93 bfd_elf_generic_reloc, /* special_function */
94 "R_MIPS_NONE", /* name */
95 false, /* partial_inplace */
96 0, /* src_mask */
97 0, /* dst_mask */
98 false), /* pcrel_offset */
99
100 /* 16 bit relocation. */
101 HOWTO (R_MIPS_16, /* type */
102 0, /* rightshift */
103 1, /* size (0 = byte, 1 = short, 2 = long) */
104 16, /* bitsize */
105 false, /* pc_relative */
106 0, /* bitpos */
107 complain_overflow_bitfield, /* complain_on_overflow */
108 bfd_elf_generic_reloc, /* special_function */
109 "R_MIPS_16", /* name */
110 true, /* partial_inplace */
111 0xffff, /* src_mask */
112 0xffff, /* dst_mask */
113 false), /* pcrel_offset */
114
115 /* 32 bit relocation. */
116 HOWTO (R_MIPS_32, /* type */
117 0, /* rightshift */
118 2, /* size (0 = byte, 1 = short, 2 = long) */
119 32, /* bitsize */
120 false, /* pc_relative */
121 0, /* bitpos */
122 complain_overflow_bitfield, /* complain_on_overflow */
123 bfd_elf_generic_reloc, /* special_function */
124 "R_MIPS_32", /* name */
125 true, /* partial_inplace */
126 0xffffffff, /* src_mask */
127 0xffffffff, /* dst_mask */
128 false), /* pcrel_offset */
129
130 /* 32 bit symbol relative relocation. */
131 HOWTO (R_MIPS_REL32, /* type */
132 0, /* rightshift */
133 2, /* size (0 = byte, 1 = short, 2 = long) */
134 32, /* bitsize */
135 false, /* pc_relative */
136 0, /* bitpos */
137 complain_overflow_bitfield, /* complain_on_overflow */
138 bfd_elf_generic_reloc, /* special_function */
139 "R_MIPS_REL32", /* name */
140 true, /* partial_inplace */
141 0xffffffff, /* src_mask */
142 0xffffffff, /* dst_mask */
143 false), /* pcrel_offset */
144
145 /* 26 bit branch address. */
146 HOWTO (R_MIPS_26, /* type */
147 2, /* rightshift */
148 2, /* size (0 = byte, 1 = short, 2 = long) */
149 26, /* bitsize */
150 false, /* pc_relative */
151 0, /* bitpos */
6e07e54f
ILT
152 complain_overflow_dont, /* complain_on_overflow */
153 /* This needs complex overflow
154 detection, because the upper four
155 bits must match the PC. */
b3c0fc57
ILT
156 bfd_elf_generic_reloc, /* special_function */
157 "R_MIPS_26", /* name */
158 true, /* partial_inplace */
159 0x3ffffff, /* src_mask */
160 0x3ffffff, /* dst_mask */
161 false), /* pcrel_offset */
162
163 /* High 16 bits of symbol value. */
164 HOWTO (R_MIPS_HI16, /* type */
165 0, /* rightshift */
166 2, /* size (0 = byte, 1 = short, 2 = long) */
167 16, /* bitsize */
168 false, /* pc_relative */
169 0, /* bitpos */
170 complain_overflow_dont, /* complain_on_overflow */
171 mips_elf_hi16_reloc, /* special_function */
172 "R_MIPS_HI16", /* name */
173 true, /* partial_inplace */
174 0xffff, /* src_mask */
175 0xffff, /* dst_mask */
176 false), /* pcrel_offset */
177
178 /* Low 16 bits of symbol value. */
179 HOWTO (R_MIPS_LO16, /* type */
180 0, /* rightshift */
181 2, /* size (0 = byte, 1 = short, 2 = long) */
182 16, /* bitsize */
183 false, /* pc_relative */
184 0, /* bitpos */
185 complain_overflow_dont, /* complain_on_overflow */
186 mips_elf_lo16_reloc, /* special_function */
187 "R_MIPS_LO16", /* name */
188 true, /* partial_inplace */
189 0xffff, /* src_mask */
190 0xffff, /* dst_mask */
191 false), /* pcrel_offset */
192
193 /* GP relative reference. */
194 HOWTO (R_MIPS_GPREL16, /* type */
195 0, /* rightshift */
196 2, /* size (0 = byte, 1 = short, 2 = long) */
197 16, /* bitsize */
198 false, /* pc_relative */
199 0, /* bitpos */
200 complain_overflow_signed, /* complain_on_overflow */
201 mips_elf_gprel16_reloc, /* special_function */
202 "R_MIPS_GPREL16", /* name */
203 true, /* partial_inplace */
204 0xffff, /* src_mask */
205 0xffff, /* dst_mask */
206 false), /* pcrel_offset */
207
208 /* Reference to literal section. */
209 HOWTO (R_MIPS_LITERAL, /* type */
210 0, /* rightshift */
211 2, /* size (0 = byte, 1 = short, 2 = long) */
212 16, /* bitsize */
213 false, /* pc_relative */
214 0, /* bitpos */
215 complain_overflow_signed, /* complain_on_overflow */
216 mips_elf_gprel16_reloc, /* special_function */
217 "R_MIPS_LITERAL", /* name */
218 true, /* partial_inplace */
219 0xffff, /* src_mask */
220 0xffff, /* dst_mask */
221 false), /* pcrel_offset */
222
223 /* Reference to global offset table. */
224 /* FIXME: This is not handled correctly. */
225 HOWTO (R_MIPS_GOT16, /* type */
226 0, /* rightshift */
227 2, /* size (0 = byte, 1 = short, 2 = long) */
228 16, /* bitsize */
229 false, /* pc_relative */
230 0, /* bitpos */
231 complain_overflow_signed, /* complain_on_overflow */
6e07e54f 232 mips_elf_got16_reloc, /* special_function */
b3c0fc57
ILT
233 "R_MIPS_GOT16", /* name */
234 false, /* partial_inplace */
235 0, /* src_mask */
236 0xffff, /* dst_mask */
237 false), /* pcrel_offset */
238
239 /* 16 bit PC relative reference. */
240 HOWTO (R_MIPS_PC16, /* type */
241 0, /* rightshift */
242 2, /* size (0 = byte, 1 = short, 2 = long) */
243 16, /* bitsize */
244 true, /* pc_relative */
245 0, /* bitpos */
246 complain_overflow_signed, /* complain_on_overflow */
247 bfd_elf_generic_reloc, /* special_function */
248 "R_MIPS_PC16", /* name */
249 true, /* partial_inplace */
250 0xffff, /* src_mask */
251 0xffff, /* dst_mask */
252 false), /* pcrel_offset */
253
254 /* 16 bit call through global offset table. */
255 /* FIXME: This is not handled correctly. */
256 HOWTO (R_MIPS_CALL16, /* type */
257 0, /* rightshift */
258 2, /* size (0 = byte, 1 = short, 2 = long) */
259 16, /* bitsize */
260 false, /* pc_relative */
261 0, /* bitpos */
262 complain_overflow_signed, /* complain_on_overflow */
263 bfd_elf_generic_reloc, /* special_function */
264 "R_MIPS_CALL16", /* name */
265 false, /* partial_inplace */
266 0, /* src_mask */
267 0xffff, /* dst_mask */
268 false), /* pcrel_offset */
269
270 /* 32 bit GP relative reference. */
271 /* FIXME: This is not handled correctly. */
272 HOWTO (R_MIPS_GPREL32, /* type */
273 0, /* rightshift */
274 2, /* size (0 = byte, 1 = short, 2 = long) */
275 32, /* bitsize */
276 false, /* pc_relative */
277 0, /* bitpos */
278 complain_overflow_bitfield, /* complain_on_overflow */
279 bfd_elf_generic_reloc, /* special_function */
280 "R_MIPS_GPREL32", /* name */
281 true, /* partial_inplace */
282 0xffffffff, /* src_mask */
283 0xffffffff, /* dst_mask */
284 false) /* pcrel_offset */
285};
286
287/* Do a R_MIPS_HI16 relocation. This has to be done in combination
288 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
289 the HI16. Here we just save the information we need; we do the
290 actual relocation when we see the LO16. MIPS ELF requires that the
291 LO16 immediately follow the HI16, so this ought to work. */
292
293static bfd_byte *mips_hi16_addr;
294static bfd_vma mips_hi16_addend;
295
296static bfd_reloc_status_type
297mips_elf_hi16_reloc (abfd,
298 reloc_entry,
299 symbol,
300 data,
301 input_section,
6e07e54f
ILT
302 output_bfd,
303 error_message)
b3c0fc57
ILT
304 bfd *abfd;
305 arelent *reloc_entry;
306 asymbol *symbol;
307 PTR data;
308 asection *input_section;
309 bfd *output_bfd;
6e07e54f 310 char **error_message;
b3c0fc57
ILT
311{
312 bfd_reloc_status_type ret;
313 bfd_vma relocation;
314
b3c0fc57
ILT
315 /* If we're relocating, and this an external symbol, we don't want
316 to change anything. */
317 if (output_bfd != (bfd *) NULL
318 && (symbol->flags & BSF_SECTION_SYM) == 0
319 && reloc_entry->addend == 0)
320 {
321 reloc_entry->address += input_section->output_offset;
322 return bfd_reloc_ok;
323 }
324
6e07e54f
ILT
325 /* FIXME: The symbol _gp_disp requires special handling, which we do
326 not do. */
327 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
328 abort ();
329
b3c0fc57
ILT
330 ret = bfd_reloc_ok;
331 if (symbol->section == &bfd_und_section
332 && output_bfd == (bfd *) NULL)
333 ret = bfd_reloc_undefined;
334
335 if (bfd_is_com_section (symbol->section))
336 relocation = 0;
337 else
338 relocation = symbol->value;
339
340 relocation += symbol->section->output_section->vma;
341 relocation += symbol->section->output_offset;
342 relocation += reloc_entry->addend;
343
344 if (reloc_entry->address > input_section->_cooked_size)
345 return bfd_reloc_outofrange;
346
347 /* Save the information, and let LO16 do the actual relocation. */
348 mips_hi16_addr = (bfd_byte *) data + reloc_entry->address;
349 mips_hi16_addend = relocation;
350
351 if (output_bfd != (bfd *) NULL)
352 reloc_entry->address += input_section->output_offset;
353
354 return ret;
355}
356
357/* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
358 inplace relocation; this function exists in order to do the
359 R_MIPS_HI16 relocation described above. */
360
361static bfd_reloc_status_type
362mips_elf_lo16_reloc (abfd,
363 reloc_entry,
364 symbol,
365 data,
366 input_section,
6e07e54f
ILT
367 output_bfd,
368 error_message)
b3c0fc57
ILT
369 bfd *abfd;
370 arelent *reloc_entry;
371 asymbol *symbol;
372 PTR data;
373 asection *input_section;
374 bfd *output_bfd;
6e07e54f 375 char **error_message;
b3c0fc57
ILT
376{
377 /* FIXME: The symbol _gp_disp requires special handling, which we do
378 not do. */
6e07e54f
ILT
379 if (output_bfd == (bfd *) NULL
380 && strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
b3c0fc57
ILT
381 abort ();
382
383 if (mips_hi16_addr != (bfd_byte *) NULL)
384 {
385 unsigned long insn;
386 unsigned long val;
387 unsigned long vallo;
388
389 /* Do the HI16 relocation. Note that we actually don't need to
390 know anything about the LO16 itself, except where to find the
391 low 16 bits of the addend needed by the LO16. */
392 insn = bfd_get_32 (abfd, mips_hi16_addr);
393 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
394 & 0xffff);
395 val = ((insn & 0xffff) << 16) + vallo;
396 val += mips_hi16_addend;
397
398 /* The low order 16 bits are always treated as a signed value.
399 Therefore, a negative value in the low order bits requires an
400 adjustment in the high order bits. We need to make this
401 adjustment in two ways: once for the bits we took from the
402 data, and once for the bits we are putting back in to the
403 data. */
404 if ((vallo & 0x8000) != 0)
405 val -= 0x10000;
406 if ((val & 0x8000) != 0)
407 val += 0x10000;
408
409 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
410 bfd_put_32 (abfd, insn, mips_hi16_addr);
411
412 mips_hi16_addr = (bfd_byte *) NULL;
413 }
414
415 /* Now do the LO16 reloc in the usual way. */
416 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
6e07e54f
ILT
417 input_section, output_bfd, error_message);
418}
419
420/* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
421 table used for PIC code. If the symbol is an external symbol, the
422 instruction is modified to contain the offset of the appropriate
423 entry in the global offset table. If the symbol is a section
424 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
425 addends are combined to form the real addend against the section
426 symbol; the GOT16 is modified to contain the offset of an entry in
427 the global offset table, and the LO16 is modified to offset it
428 appropriately. Thus an offset larger than 16 bits requires a
429 modified value in the global offset table.
430
431 This implementation suffices for the assembler, but the linker does
432 not yet know how to create global offset tables. */
433
434static bfd_reloc_status_type
435mips_elf_got16_reloc (abfd,
436 reloc_entry,
437 symbol,
438 data,
439 input_section,
440 output_bfd,
441 error_message)
442 bfd *abfd;
443 arelent *reloc_entry;
444 asymbol *symbol;
445 PTR data;
446 asection *input_section;
447 bfd *output_bfd;
448 char **error_message;
449{
450 /* If we're relocating, and this an external symbol, we don't want
451 to change anything. */
452 if (output_bfd != (bfd *) NULL
453 && (symbol->flags & BSF_SECTION_SYM) == 0
454 && reloc_entry->addend == 0)
455 {
456 reloc_entry->address += input_section->output_offset;
457 return bfd_reloc_ok;
458 }
459
460 /* If we're relocating, and this is a local symbol, we can handle it
461 just like HI16. */
462 if (output_bfd != (bfd *) NULL
463 && (symbol->flags & BSF_SECTION_SYM) != 0)
464 return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
465 input_section, output_bfd, error_message);
466
467 abort ();
b3c0fc57
ILT
468}
469
470/* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
471 become the offset from the gp register. This function also handles
472 R_MIPS_LITERAL relocations, although those can be handled more
473 cleverly because the entries in the .lit8 and .lit4 sections can be
474 merged. */
475
476static bfd_reloc_status_type
477mips_elf_gprel16_reloc (abfd,
478 reloc_entry,
479 symbol,
480 data,
481 input_section,
6e07e54f
ILT
482 output_bfd,
483 error_message)
b3c0fc57
ILT
484 bfd *abfd;
485 arelent *reloc_entry;
486 asymbol *symbol;
487 PTR data;
488 asection *input_section;
489 bfd *output_bfd;
6e07e54f 490 char **error_message;
b3c0fc57
ILT
491{
492 boolean relocateable;
493 bfd_vma relocation;
494 unsigned long val;
495 unsigned long insn;
496
497 /* If we're relocating, and this is an external symbol with no
498 addend, we don't want to change anything. We will only have an
499 addend if this is a newly created reloc, not read from an ELF
500 file. */
501 if (output_bfd != (bfd *) NULL
502 && (symbol->flags & BSF_SECTION_SYM) == 0
503 && reloc_entry->addend == 0)
504 {
505 reloc_entry->address += input_section->output_offset;
506 return bfd_reloc_ok;
507 }
508
509 if (output_bfd != (bfd *) NULL)
510 relocateable = true;
511 else
512 {
513 relocateable = false;
514 output_bfd = symbol->section->output_section->owner;
515 }
516
517 if (symbol->section == &bfd_und_section
518 && relocateable == false)
519 return bfd_reloc_undefined;
520
521 /* We have to figure out the gp value, so that we can adjust the
522 symbol value correctly. We look up the symbol _gp in the output
523 BFD. If we can't find it, we're stuck. We cache it in the ELF
524 target data. We don't need to adjust the symbol value for an
525 external symbol if we are producing relocateable output. */
526 if (elf_gp (output_bfd) == 0
527 && (relocateable == false
528 || (symbol->flags & BSF_SECTION_SYM) != 0))
529 {
530 if (relocateable != false)
531 {
532 /* Make up a value. */
533 elf_gp (output_bfd) =
534 symbol->section->output_section->vma + 0x4000;
535 }
536 else
537 {
538 unsigned int count;
539 asymbol **sym;
540 unsigned int i;
541
542 count = bfd_get_symcount (output_bfd);
543 sym = bfd_get_outsymbols (output_bfd);
544
545 if (sym == (asymbol **) NULL)
546 i = count;
547 else
548 {
549 for (i = 0; i < count; i++, sym++)
550 {
551 register CONST char *name;
552
553 name = bfd_asymbol_name (*sym);
554 if (*name == '_' && strcmp (name, "_gp") == 0)
555 {
556 elf_gp (output_bfd) = bfd_asymbol_value (*sym);
557 break;
558 }
559 }
560 }
561
562 if (i >= count)
563 {
564 /* Only get the error once. */
565 elf_gp (output_bfd) = 4;
6e07e54f
ILT
566 *error_message =
567 (char *) "GP relative relocation when _gp not defined";
b3c0fc57
ILT
568 return bfd_reloc_dangerous;
569 }
570 }
571 }
572
573 if (bfd_is_com_section (symbol->section))
574 relocation = 0;
575 else
576 relocation = symbol->value;
577
578 relocation += symbol->section->output_section->vma;
579 relocation += symbol->section->output_offset;
580
581 if (reloc_entry->address > input_section->_cooked_size)
582 return bfd_reloc_outofrange;
583
584 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
585
586 /* Set val to the offset into the section or symbol. */
587 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
588 if (val & 0x8000)
589 val -= 0x10000;
590
591 /* Adjust val for the final section location and GP value. If we
592 are producing relocateable output, we don't want to do this for
593 an external symbol. */
594 if (relocateable == false
595 || (symbol->flags & BSF_SECTION_SYM) != 0)
596 val += relocation - elf_gp (output_bfd);
597
598 insn = (insn &~ 0xffff) | (val & 0xffff);
599 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
600
601 if (relocateable != false)
602 reloc_entry->address += input_section->output_offset;
603
604 /* Make sure it fit in 16 bits. */
605 if (val >= 0x8000 && val < 0xffff8000)
6e07e54f 606 return bfd_reloc_overflow;
b3c0fc57
ILT
607
608 return bfd_reloc_ok;
609}
6b4b4d17 610
b3c0fc57 611/* A mapping from BFD reloc types to MIPS ELF reloc types. */
6b4b4d17 612
b3c0fc57
ILT
613struct elf_reloc_map {
614 bfd_reloc_code_real_type bfd_reloc_val;
615 enum reloc_type elf_reloc_val;
616};
617
618static CONST struct elf_reloc_map mips_reloc_map[] =
619{
620 { BFD_RELOC_NONE, R_MIPS_NONE, },
621 { BFD_RELOC_16, R_MIPS_16 },
622 { BFD_RELOC_32, R_MIPS_32 },
623 { BFD_RELOC_CTOR, R_MIPS_32 },
624 { BFD_RELOC_32_PCREL, R_MIPS_REL32 },
625 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
626 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
627 { BFD_RELOC_LO16, R_MIPS_LO16 },
628 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
629 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
630 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
631 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
632 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
633 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 }
634};
635
636/* Given a BFD reloc type, return a howto structure. */
637
638static CONST struct reloc_howto_struct *
639bfd_elf32_bfd_reloc_type_lookup (abfd, code)
640 bfd *abfd;
641 bfd_reloc_code_real_type code;
642{
643 int i;
644
645 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
646 {
647 if (mips_reloc_map[i].bfd_reloc_val == code)
648 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
649 }
650 return NULL;
651}
652
653/* Given a MIPS reloc type, fill in an arelent structure. */
654
655static void
656mips_info_to_howto_rel (abfd, cache_ptr, dst)
657 bfd *abfd;
658 arelent *cache_ptr;
659 Elf32_Internal_Rel *dst;
660{
661 unsigned int r_type;
662
663 r_type = ELF32_R_TYPE (dst->r_info);
664 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
665 cache_ptr->howto = &elf_mips_howto_table[r_type];
666
667 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
668 value for the object file. We get the addend now, rather than
669 when we do the relocation, because the symbol manipulations done
670 by the linker may cause us to lose track of the input BFD. */
671 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
672 && (r_type == (unsigned int) R_MIPS_GPREL16
673 || r_type == (unsigned int) R_MIPS_LITERAL))
674 cache_ptr->addend = elf_gp (abfd);
675}
676\f
677/* A .reginfo section holds a single Elf32_RegInfo structure. These
678 routines swap this structure in and out. They are used outside of
679 BFD, so they are globally visible. */
680
681void
682bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
683 bfd *abfd;
684 const Elf32_External_RegInfo *ex;
685 Elf32_RegInfo *in;
686{
687 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
688 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
689 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
690 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
691 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
692 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
693}
694
695void
696bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
697 bfd *abfd;
698 const Elf32_RegInfo *in;
699 Elf32_External_RegInfo *ex;
700{
701 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
702 (bfd_byte *) ex->ri_gprmask);
703 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
704 (bfd_byte *) ex->ri_cprmask[0]);
705 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
706 (bfd_byte *) ex->ri_cprmask[1]);
707 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
708 (bfd_byte *) ex->ri_cprmask[2]);
709 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
710 (bfd_byte *) ex->ri_cprmask[3]);
711 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
712 (bfd_byte *) ex->ri_gp_value);
713}
714\f
6e07e54f
ILT
715/* Determine whether a symbol is global for the purposes of splitting
716 the symbol table into global symbols and local symbols. At least
717 on Irix 5, this split must be between section symbols and all other
718 symbols. On most ELF targets the split is between static symbols
719 and externally visible symbols. */
720
721/*ARGSUSED*/
722static boolean
723mips_elf_sym_is_global (abfd, sym)
724 bfd *abfd;
725 asymbol *sym;
726{
727 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
728}
729\f
730/* Set the right machine number for a MIPS ELF file. */
731
732static boolean
733mips_elf_object_p (abfd)
734 bfd *abfd;
735{
736 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
737 {
738 default:
739 case E_MIPS_ARCH_1:
740 /* Just use the default, which was set in elfcode.h. */
741 break;
742
743 case E_MIPS_ARCH_2:
744 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
745 break;
746
747 case E_MIPS_ARCH_3:
748 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
749 break;
750 }
751
752 return true;
753}
754
755/* The final processing done just before writing out a MIPS ELF object
756 file. This gets the MIPS architecture right based on the machine
757 number. */
758
759static void
760mips_elf_final_write_processing (abfd)
761 bfd *abfd;
762{
763 unsigned long val;
764
765 switch (bfd_get_mach (abfd))
766 {
767 case 3000:
768 val = E_MIPS_ARCH_1;
769 break;
770
771 case 6000:
772 val = E_MIPS_ARCH_2;
773 break;
774
775 case 4000:
776 val = E_MIPS_ARCH_3;
777 break;
778
779 default:
780 return;
781 }
782
783 elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
784 elf_elfheader (abfd)->e_flags |= val;
785}
786\f
b3c0fc57
ILT
787/* Handle a MIPS specific section when reading an object file. This
788 is called when elfcode.h finds a section with an unknown type.
789 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
790 how to. */
791
792static boolean
793mips_elf_section_from_shdr (abfd, hdr, name)
794 bfd *abfd;
795 Elf32_Internal_Shdr *hdr;
796 char *name;
797{
497c5434
ILT
798 asection *newsect;
799
b3c0fc57
ILT
800 /* There ought to be a place to keep ELF backend specific flags, but
801 at the moment there isn't one. We just keep track of the
802 sections by their name, instead. Fortunately, the ABI gives
803 suggested names for all the MIPS specific sections, so we will
804 probably get away with this. */
805 switch (hdr->sh_type)
806 {
807 case SHT_MIPS_LIBLIST:
808 if (strcmp (name, ".liblist") != 0)
809 return false;
810 break;
811 case SHT_MIPS_CONFLICT:
812 if (strcmp (name, ".conflict") != 0)
813 return false;
814 break;
815 case SHT_MIPS_GPTAB:
816 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
817 return false;
818 break;
819 case SHT_MIPS_UCODE:
820 if (strcmp (name, ".ucode") != 0)
821 return false;
822 break;
823 case SHT_MIPS_DEBUG:
824 if (strcmp (name, ".mdebug") != 0)
825 return false;
826 break;
827 case SHT_MIPS_REGINFO:
828 if (strcmp (name, ".reginfo") != 0
829 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
830 return false;
831 break;
6e07e54f
ILT
832 case SHT_MIPS_OPTIONS:
833 if (strcmp (name, ".options") != 0)
834 return false;
835 break;
b3c0fc57
ILT
836 default:
837 return false;
838 }
839
497c5434
ILT
840 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
841 return false;
842 newsect = (asection *) hdr->rawdata;
b3c0fc57 843
497c5434
ILT
844 if (hdr->sh_type == SHT_MIPS_DEBUG)
845 {
846 if (! bfd_set_section_flags (abfd, newsect,
847 (bfd_get_section_flags (abfd, newsect)
848 | SEC_DEBUGGING)))
849 return false;
850 }
b3c0fc57 851
497c5434 852 /* FIXME: We should record sh_info for a .gptab section. */
b3c0fc57 853
497c5434
ILT
854 /* For a .reginfo section, set the gp value in the tdata information
855 from the contents of this section. We need the gp value while
856 processing relocs, so we just get it now. */
857 if (hdr->sh_type == SHT_MIPS_REGINFO)
858 {
859 Elf32_External_RegInfo ext;
860 Elf32_RegInfo s;
b3c0fc57 861
497c5434
ILT
862 if (! bfd_get_section_contents (abfd, newsect, (PTR) &ext,
863 (file_ptr) 0, sizeof ext))
864 return false;
865 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
866 elf_gp (abfd) = s.ri_gp_value;
b3c0fc57
ILT
867 }
868
869 return true;
870}
871
872/* Set the correct type for a MIPS ELF section. We do this by the
873 section name, which is a hack, but ought to work. */
874
875static boolean
876mips_elf_fake_sections (abfd, hdr, sec)
877 bfd *abfd;
878 Elf32_Internal_Shdr *hdr;
879 asection *sec;
880{
881 register const char *name;
882
883 name = bfd_get_section_name (abfd, sec);
884
885 if (strcmp (name, ".liblist") == 0)
886 {
887 hdr->sh_type = SHT_MIPS_LIBLIST;
888 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
889 /* FIXME: Set the sh_link field. */
890 }
891 else if (strcmp (name, ".conflict") == 0)
892 hdr->sh_type = SHT_MIPS_CONFLICT;
893 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
894 {
895 hdr->sh_type = SHT_MIPS_GPTAB;
896 /* FIXME: Set the sh_info field. */
897 }
898 else if (strcmp (name, ".ucode") == 0)
899 hdr->sh_type = SHT_MIPS_UCODE;
900 else if (strcmp (name, ".mdebug") == 0)
6e07e54f
ILT
901 {
902 hdr->sh_type = SHT_MIPS_DEBUG;
903 hdr->sh_entsize = 1;
904 }
b3c0fc57
ILT
905 else if (strcmp (name, ".reginfo") == 0)
906 {
907 hdr->sh_type = SHT_MIPS_REGINFO;
6e07e54f 908 hdr->sh_entsize = 1;
b3c0fc57
ILT
909
910 /* Force the section size to the correct value, even if the
911 linker thinks it is larger. The link routine below will only
912 write out this much data for .reginfo. */
913 hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
914 }
6e07e54f
ILT
915 else if (strcmp (name, ".options") == 0)
916 {
917 hdr->sh_type = SHT_MIPS_OPTIONS;
918 hdr->sh_entsize = 1;
919 }
b3c0fc57
ILT
920
921 return true;
922}
923
924/* Given a BFD section, try to locate the corresponding ELF section
925 index. */
926
927static boolean
928mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
929 bfd *abfd;
930 Elf32_Internal_Shdr *hdr;
931 asection *sec;
932 int *retval;
933{
934 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
935 {
936 *retval = SHN_MIPS_SCOMMON;
937 return true;
938 }
939 if ((asection *) hdr->rawdata == sec)
940 return true;
941 return false;
942}
943
944/* Work over a section just before writing it out. We update the GP
945 value in the .reginfo section based on the value we are using.
946 FIXME: We recognize sections that need the SHF_MIPS_GPREL flag by
947 name; there has to be a better way. */
948
949static boolean
950mips_elf_section_processing (abfd, hdr)
951 bfd *abfd;
952 Elf32_Internal_Shdr *hdr;
953{
954 if (hdr->sh_type == SHT_MIPS_REGINFO)
955 {
956 bfd_byte buf[4];
957
958 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
959 BFD_ASSERT (hdr->contents == NULL);
960
961 if (bfd_seek (abfd,
962 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
963 SEEK_SET) == -1)
964 return false;
965 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
966 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
967 return false;
968 }
969
970 if (hdr->rawdata != NULL)
971 {
972 const char *name = ((asection *) hdr->rawdata)->name;
973
974 if (strcmp (name, ".sdata") == 0)
975 {
976 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
977 hdr->sh_type = SHT_PROGBITS;
978 }
979 else if (strcmp (name, ".sbss") == 0)
980 {
981 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
982 hdr->sh_type = SHT_NOBITS;
983 }
984 else if (strcmp (name, ".lit8") == 0
985 || strcmp (name, ".lit4") == 0)
986 {
987 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
988 hdr->sh_type = SHT_PROGBITS;
989 }
990 }
991
992 return true;
993}
994\f
6e07e54f
ILT
995/* Read ECOFF debugging information from a .mdebug section into a
996 ecoff_debug_info structure. */
b3c0fc57
ILT
997
998static boolean
6e07e54f 999mips_elf_read_ecoff_info (abfd, section, debug)
b3c0fc57 1000 bfd *abfd;
6e07e54f
ILT
1001 asection *section;
1002 struct ecoff_debug_info *debug;
b3c0fc57 1003{
6e07e54f
ILT
1004 HDRR *symhdr;
1005 const struct ecoff_debug_swap *swap;
a3a33af3 1006 char *ext_hdr = NULL;
6e07e54f
ILT
1007
1008 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1009
a3a33af3
ILT
1010 ext_hdr = (char *) malloc (swap->external_hdr_size);
1011 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1012 {
1013 bfd_set_error (bfd_error_no_memory);
1014 goto error_return;
1015 }
6e07e54f
ILT
1016
1017 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
1018 swap->external_hdr_size)
1019 == false)
a3a33af3 1020 goto error_return;
6e07e54f
ILT
1021
1022 symhdr = &debug->symbolic_header;
1023 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1024
1025 /* The symbolic header contains absolute file offsets and sizes to
1026 read. */
1027#define READ(ptr, offset, count, size, type) \
1028 if (symhdr->count == 0) \
1029 debug->ptr = NULL; \
1030 else \
1031 { \
1032 debug->ptr = (type) malloc (size * symhdr->count); \
1033 if (debug->ptr == NULL) \
1034 { \
a3a33af3
ILT
1035 bfd_set_error (bfd_error_no_memory); \
1036 goto error_return; \
6e07e54f
ILT
1037 } \
1038 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
1039 || (bfd_read (debug->ptr, size, symhdr->count, \
1040 abfd) != size * symhdr->count)) \
a3a33af3 1041 goto error_return; \
6e07e54f
ILT
1042 }
1043
9783e04a 1044 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
6e07e54f
ILT
1045 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1046 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
1047 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
1048 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
1049 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
1050 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1051 union aux_ext *);
1052 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1053 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1054 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
1055 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
9783e04a
DM
1056
1057 debug->fdr = NULL;
a3a33af3 1058 debug->adjust = NULL;
6e07e54f
ILT
1059
1060 return true;
a3a33af3
ILT
1061
1062 error_return:
1063 if (ext_hdr != NULL)
1064 free (ext_hdr);
1065 if (debug->external_ext != NULL)
1066 free (debug->external_ext);
1067 if (debug->line != NULL)
1068 free (debug->line);
1069 if (debug->external_dnr != NULL)
1070 free (debug->external_dnr);
1071 if (debug->external_pdr != NULL)
1072 free (debug->external_pdr);
1073 if (debug->external_sym != NULL)
1074 free (debug->external_sym);
1075 if (debug->external_opt != NULL)
1076 free (debug->external_opt);
1077 if (debug->external_aux != NULL)
1078 free (debug->external_aux);
1079 if (debug->ss != NULL)
1080 free (debug->ss);
1081 if (debug->ssext != NULL)
1082 free (debug->ssext);
1083 if (debug->external_fdr != NULL)
1084 free (debug->external_fdr);
1085 if (debug->external_rfd != NULL)
1086 free (debug->external_rfd);
1087 return false;
6e07e54f
ILT
1088}
1089
1090/* Get EXTR information for a symbol. */
1091
1092static boolean
1093mips_elf_get_extr (sym, esym)
1094 asymbol *sym;
1095 EXTR *esym;
1096{
9783e04a
DM
1097 const struct ecoff_debug_swap *swap;
1098
6e07e54f
ILT
1099 if (sym->flags & BSF_SECTION_SYM)
1100 return false;
1101
1102 if (bfd_asymbol_flavour (sym) != bfd_target_elf_flavour
1103 || ((elf_symbol_type *) sym)->tc_data.mips_extr == NULL)
1104 {
1105 esym->jmptbl = 0;
1106 esym->cobol_main = 0;
1107 esym->weakext = 0;
1108 esym->reserved = 0;
1109 esym->ifd = ifdNil;
1110 /* FIXME: we can do better than this for st and sc. */
1111 esym->asym.st = stGlobal;
1112 esym->asym.sc = scAbs;
1113 esym->asym.reserved = 0;
1114 esym->asym.index = indexNil;
1115 return true;
1116 }
1117
9783e04a
DM
1118 swap = (get_elf_backend_data (bfd_asymbol_bfd (sym))
1119 ->elf_backend_ecoff_debug_swap);
1120 (*swap->swap_ext_in) (bfd_asymbol_bfd (sym),
1121 ((elf_symbol_type *) sym)->tc_data.mips_extr,
1122 esym);
6e07e54f
ILT
1123
1124 return true;
1125}
1126
1127/* Set the symbol index for an external symbol. This is actually not
1128 needed for ELF. */
1129
1130/*ARGSUSED*/
1131static void
1132mips_elf_set_index (sym, indx)
1133 asymbol *sym;
1134 bfd_size_type indx;
1135{
1136}
1137
1138/* We need to use a special link routine to handle the .reginfo and
1139 the .mdebug sections. We need to merge all instances of these
1140 sections together, not write them all out sequentially. */
1141
1142static boolean
1143mips_elf_final_link (abfd, info)
1144 bfd *abfd;
1145 struct bfd_link_info *info;
1146{
1147 bfd *sub;
1148 size_t outsymalloc;
1149 struct generic_write_global_symbol_info wginfo;
1150 asection **secpp;
1151 asection *o;
1152 struct bfd_link_order *p;
1153 asection *reginfo_sec, *mdebug_sec;
b3c0fc57 1154 Elf32_RegInfo reginfo;
6e07e54f
ILT
1155 struct ecoff_debug_info debug;
1156 const struct ecoff_debug_swap *swap
1157 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1158 HDRR *symhdr = &debug.symbolic_header;
9783e04a 1159 PTR mdebug_handle = NULL;
6e07e54f
ILT
1160
1161 abfd->outsymbols = (asymbol **) NULL;
1162 abfd->symcount = 0;
1163 outsymalloc = 0;
1164
1165 /* Build the output symbol table. This also reads in the symbols
1166 for all the input BFDs, keeping them in the outsymbols field. */
1167 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
1168 if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
1169 return false;
b3c0fc57 1170
6e07e54f 1171 /* Accumulate the global symbols. */
9783e04a 1172 wginfo.info = info;
6e07e54f
ILT
1173 wginfo.output_bfd = abfd;
1174 wginfo.psymalloc = &outsymalloc;
1175 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
1176 _bfd_generic_link_write_global_symbol,
1177 (PTR) &wginfo);
1178
497c5434
ILT
1179 /* Drop the .options section, since it has special semantics which I
1180 haven't bothered to figure out. Also drop the .gptab sections,
1181 which also require special handling which is not currently done.
1182 Removing the .gptab sections is required for Irix 5
1183 compatibility; I don't know about .options. */
6e07e54f
ILT
1184 secpp = &abfd->sections;
1185 while (*secpp != NULL)
1186 {
497c5434 1187 if (strcmp ((*secpp)->name, ".options") == 0
6e07e54f
ILT
1188 || strncmp ((*secpp)->name, ".gptab", 6) == 0)
1189 {
1190 *secpp = (*secpp)->next;
1191 --abfd->section_count;
1192 }
1193 else
1194 secpp = &(*secpp)->next;
1195 }
b3c0fc57 1196
6e07e54f
ILT
1197 /* Go through the sections and collect the .reginfo and .mdebug
1198 information. We don't write out the information until we have
1199 set the section sizes, because the ELF backend only assigns space
1200 in the file once. */
9783e04a
DM
1201 reginfo_sec = NULL;
1202 mdebug_sec = NULL;
6e07e54f 1203 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
b3c0fc57 1204 {
6e07e54f 1205 if (strcmp (o->name, ".reginfo") == 0)
b3c0fc57 1206 {
6e07e54f 1207 memset (&reginfo, 0, sizeof reginfo);
b3c0fc57
ILT
1208
1209 /* We have found the .reginfo section in the output file.
6e07e54f
ILT
1210 Look through all the link_orders comprising it and merge
1211 the information together. */
1212 for (p = o->link_order_head;
1213 p != (struct bfd_link_order *) NULL;
b3c0fc57
ILT
1214 p = p->next)
1215 {
6e07e54f
ILT
1216 asection *input_section;
1217 bfd *input_bfd;
1218 Elf32_External_RegInfo ext;
1219 Elf32_RegInfo sub;
1220
1221 if (p->type != bfd_indirect_link_order)
1222 continue;
1223
1224 input_section = p->u.indirect.section;
1225 input_bfd = input_section->owner;
1226 BFD_ASSERT (input_section->_raw_size
1227 == sizeof (Elf32_External_RegInfo));
1228 if (! bfd_get_section_contents (input_bfd, input_section,
1229 (PTR) &ext,
1230 (file_ptr) 0,
1231 sizeof ext))
1232 return false;
1233
1234 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
1235
1236 reginfo.ri_gprmask |= sub.ri_gprmask;
1237 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
1238 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
1239 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
1240 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
1241
1242 /* ri_gp_value is set by the function
1243 mips_elf_section_processing when the section is
1244 finally written out. */
1245 }
1246
1247 /* Force the section size to the value we want. */
1248 o->_raw_size = sizeof (Elf32_External_RegInfo);
1249
1250 /* Skip this section later on. */
1251 o->link_order_head = (struct bfd_link_order *) NULL;
1252
1253 reginfo_sec = o;
1254 }
1255
1256 if (strcmp (o->name, ".mdebug") == 0)
1257 {
1258 /* We have found the .mdebug section in the output file.
1259 Look through all the link_orders comprising it and merge
1260 the information together. */
1261 symhdr->magic = swap->sym_magic;
1262 /* FIXME: What should the version stamp be? */
1263 symhdr->vstamp = 0;
1264 symhdr->ilineMax = 0;
1265 symhdr->cbLine = 0;
1266 symhdr->idnMax = 0;
1267 symhdr->ipdMax = 0;
1268 symhdr->isymMax = 0;
1269 symhdr->ioptMax = 0;
1270 symhdr->iauxMax = 0;
1271 symhdr->issMax = 0;
1272 symhdr->issExtMax = 0;
1273 symhdr->ifdMax = 0;
1274 symhdr->crfd = 0;
1275 symhdr->iextMax = 0;
1276
1277 /* We accumulate the debugging information itself in the
1278 debug_info structure. */
9783e04a
DM
1279 debug.line = NULL;
1280 debug.external_dnr = NULL;
1281 debug.external_pdr = NULL;
1282 debug.external_sym = NULL;
1283 debug.external_opt = NULL;
1284 debug.external_aux = NULL;
1285 debug.ss = NULL;
6e07e54f 1286 debug.ssext = debug.ssext_end = NULL;
9783e04a
DM
1287 debug.external_fdr = NULL;
1288 debug.external_rfd = NULL;
6e07e54f
ILT
1289 debug.external_ext = debug.external_ext_end = NULL;
1290
9783e04a
DM
1291 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
1292 if (mdebug_handle == (PTR) NULL)
1293 return false;
1294
6e07e54f
ILT
1295 for (p = o->link_order_head;
1296 p != (struct bfd_link_order *) NULL;
1297 p = p->next)
1298 {
1299 asection *input_section;
1300 bfd *input_bfd;
1301 const struct ecoff_debug_swap *input_swap;
1302 struct ecoff_debug_info input_debug;
1303
1304 if (p->type != bfd_indirect_link_order)
1305 continue;
1306
1307 input_section = p->u.indirect.section;
1308 input_bfd = input_section->owner;
1309
1310 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
1311 || (get_elf_backend_data (input_bfd)
1312 ->elf_backend_ecoff_debug_swap) == NULL)
b3c0fc57 1313 {
6e07e54f
ILT
1314 /* I don't know what a non MIPS ELF bfd would be
1315 doing with a .mdebug section, but I don't really
1316 want to deal with it. */
1317 continue;
b3c0fc57 1318 }
6e07e54f
ILT
1319
1320 input_swap = (get_elf_backend_data (input_bfd)
1321 ->elf_backend_ecoff_debug_swap);
1322
1323 BFD_ASSERT (p->size == input_section->_raw_size);
1324
1325 /* The ECOFF linking code expects that we have already
1326 read in the debugging information and set up an
1327 ecoff_debug_info structure, so we do that now. */
1328 if (! mips_elf_read_ecoff_info (input_bfd, input_section,
1329 &input_debug))
1330 return false;
1331
1332 if (! (bfd_ecoff_debug_accumulate
9783e04a
DM
1333 (mdebug_handle, abfd, &debug, swap, input_bfd,
1334 &input_debug, input_swap, info)))
6e07e54f
ILT
1335 return false;
1336
1337 /* Loop through the external symbols. For each one with
1338 interesting information, try to find the symbol on
1339 the symbol table of abfd and save the information in
1340 order to put it into the final external symbols. */
1341 if (info->hash->creator == input_bfd->xvec)
1342 {
1343 char *eraw_src;
1344 char *eraw_end;
1345
1346 eraw_src = input_debug.external_ext;
1347 eraw_end = (eraw_src
1348 + (input_debug.symbolic_header.iextMax
1349 * input_swap->external_ext_size));
1350 for (;
1351 eraw_src < eraw_end;
1352 eraw_src += input_swap->external_ext_size)
1353 {
1354 EXTR ext;
1355 const char *name;
1356 struct generic_link_hash_entry *h;
1357 elf_symbol_type *elf_sym;
1358
1359 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src,
1360 &ext);
1361 if (ext.asym.sc == scNil
1362 || ext.asym.sc == scUndefined
1363 || ext.asym.sc == scSUndefined)
1364 continue;
1365
1366 name = input_debug.ssext + ext.asym.iss;
1367 h = ((struct generic_link_hash_entry *)
1368 bfd_link_hash_lookup (info->hash, name, false,
1369 false, true));
1370 if (h == (struct generic_link_hash_entry *) NULL
1371 || h->sym == (asymbol *) NULL)
1372 continue;
1373
1374 elf_sym = (elf_symbol_type *) (h->sym);
1375
1376 if (elf_sym->tc_data.mips_extr != NULL)
1377 continue;
1378
9783e04a
DM
1379 if (ext.ifd != -1)
1380 {
1381 BFD_ASSERT (ext.ifd
1382 < input_debug.symbolic_header.ifdMax);
1383 ext.ifd = input_debug.ifdmap[ext.ifd];
1384 }
6e07e54f 1385
9783e04a
DM
1386 (*input_swap->swap_ext_out) (input_bfd, &ext,
1387 (PTR) eraw_src);
1388 elf_sym->tc_data.mips_extr = (PTR) eraw_src;
6e07e54f
ILT
1389 }
1390 }
1391
9783e04a
DM
1392 /* Free up the information we just read, except for the
1393 external symbols which we may have pointers to. */
6e07e54f
ILT
1394 free (input_debug.line);
1395 free (input_debug.external_dnr);
1396 free (input_debug.external_pdr);
1397 free (input_debug.external_sym);
1398 free (input_debug.external_opt);
1399 free (input_debug.external_aux);
1400 free (input_debug.ss);
1401 free (input_debug.ssext);
1402 free (input_debug.external_fdr);
1403 free (input_debug.external_rfd);
b3c0fc57
ILT
1404 }
1405
6e07e54f
ILT
1406 /* Build the external symbol information. */
1407 if (! bfd_ecoff_debug_externals (abfd, &debug, swap,
1408 info->relocateable,
1409 mips_elf_get_extr,
1410 mips_elf_set_index))
b3c0fc57
ILT
1411 return false;
1412
6e07e54f
ILT
1413 /* Set the size of the section. */
1414 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
1415
1416 /* Skip this section later on. */
1417 o->link_order_head = (struct bfd_link_order *) NULL;
1418
1419 mdebug_sec = o;
1420 }
1421 }
1422
1423 if (info->relocateable)
1424 {
1425 /* Allocate space for the output relocs for each section. */
1426 for (o = abfd->sections;
1427 o != (asection *) NULL;
1428 o = o->next)
1429 {
1430 o->reloc_count = 0;
1431 for (p = o->link_order_head;
1432 p != (struct bfd_link_order *) NULL;
1433 p = p->next)
1434 {
a3a33af3
ILT
1435 if (p->type == bfd_section_reloc_link_order
1436 || p->type == bfd_symbol_reloc_link_order)
1437 ++o->reloc_count;
1438 else if (p->type == bfd_indirect_link_order)
6e07e54f
ILT
1439 {
1440 asection *input_section;
1441 bfd *input_bfd;
326e32d7 1442 long relsize;
6e07e54f 1443 arelent **relocs;
326e32d7 1444 long reloc_count;
6e07e54f
ILT
1445
1446 input_section = p->u.indirect.section;
1447 input_bfd = input_section->owner;
1448 relsize = bfd_get_reloc_upper_bound (input_bfd,
1449 input_section);
326e32d7
ILT
1450 if (relsize < 0)
1451 return false;
9783e04a 1452 relocs = (arelent **) malloc (relsize);
a3a33af3 1453 if (!relocs && relsize != 0)
9783e04a 1454 {
a3a33af3 1455 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1456 return false;
1457 }
6e07e54f
ILT
1458 reloc_count =
1459 bfd_canonicalize_reloc (input_bfd, input_section,
1460 relocs,
1461 bfd_get_outsymbols (input_bfd));
326e32d7
ILT
1462 if (reloc_count < 0)
1463 return false;
6e07e54f
ILT
1464 BFD_ASSERT (reloc_count == input_section->reloc_count);
1465 o->reloc_count += reloc_count;
1466 free (relocs);
1467 }
1468 }
1469 if (o->reloc_count > 0)
1470 {
1471 o->orelocation = ((arelent **)
1472 bfd_alloc (abfd,
1473 (o->reloc_count
1474 * sizeof (arelent *))));
9783e04a
DM
1475 if (!o->orelocation)
1476 {
a3a33af3 1477 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1478 return false;
1479 }
a3a33af3 1480 o->flags |= SEC_RELOC;
6e07e54f
ILT
1481 /* Reset the count so that it can be used as an index
1482 when putting in the output relocs. */
1483 o->reloc_count = 0;
1484 }
1485 }
1486 }
b3c0fc57 1487
6e07e54f
ILT
1488 /* Write out the information we have accumulated. */
1489 if (reginfo_sec != (asection *) NULL)
1490 {
1491 Elf32_External_RegInfo ext;
1492
1493 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
1494 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
1495 (file_ptr) 0, sizeof ext))
1496 return false;
1497 }
b3c0fc57 1498
6e07e54f
ILT
1499 if (mdebug_sec != (asection *) NULL)
1500 {
1501 if (! abfd->output_has_begun)
1502 {
1503 /* Force the section to be given a file position. */
1504 bfd_set_section_contents (abfd, mdebug_sec, (PTR) NULL,
1505 (file_ptr) 0, (bfd_size_type) 0);
1506 BFD_ASSERT (abfd->output_has_begun);
b3c0fc57 1507 }
9783e04a
DM
1508 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
1509 swap, info,
1510 mdebug_sec->filepos))
6e07e54f 1511 return false;
9783e04a
DM
1512
1513 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
b3c0fc57
ILT
1514 }
1515
6e07e54f
ILT
1516 /* Handle all the link order information for the sections. */
1517 for (o = abfd->sections;
1518 o != (asection *) NULL;
1519 o = o->next)
1520 {
1521 for (p = o->link_order_head;
1522 p != (struct bfd_link_order *) NULL;
1523 p = p->next)
1524 {
a3a33af3
ILT
1525 switch (p->type)
1526 {
1527 case bfd_section_reloc_link_order:
1528 case bfd_symbol_reloc_link_order:
1529 if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
1530 return false;
1531 break;
1532 default:
1533 if (! _bfd_default_link_order (abfd, info, o, p))
1534 return false;
1535 break;
1536 }
6e07e54f
ILT
1537 }
1538 }
1539
1540 return true;
b3c0fc57
ILT
1541}
1542\f
1543/* MIPS ELF uses two common sections. One is the usual one, and the
1544 other is for small objects. All the small objects are kept
1545 together, and then referenced via the gp pointer, which yields
1546 faster assembler code. This is what we use for the small common
1547 section. This approach is copied from ecoff.c. */
1548static asection mips_elf_scom_section;
1549static asymbol mips_elf_scom_symbol;
1550static asymbol *mips_elf_scom_symbol_ptr;
1551
6e07e54f
ILT
1552/* MIPS ELF also uses an acommon section, which represents an
1553 allocated common symbol which may be overridden by a
1554 definition in a shared library. */
1555static asection mips_elf_acom_section;
1556static asymbol mips_elf_acom_symbol;
1557static asymbol *mips_elf_acom_symbol_ptr;
1558
b3c0fc57
ILT
1559/* Handle the special MIPS section numbers that a symbol may use. */
1560
1561static void
1562mips_elf_symbol_processing (abfd, asym)
1563 bfd *abfd;
1564 asymbol *asym;
1565{
1566 elf_symbol_type *elfsym;
1567
1568 elfsym = (elf_symbol_type *) asym;
1569 switch (elfsym->internal_elf_sym.st_shndx)
1570 {
1571 case SHN_MIPS_ACOMMON:
6e07e54f
ILT
1572 /* This section is used in a dynamically linked executable file.
1573 It is an allocated common section. The dynamic linker can
1574 either resolve these symbols to something in a shared
1575 library, or it can just leave them here. For our purposes,
1576 we can consider these symbols to be in a new section. */
1577 if (mips_elf_acom_section.name == NULL)
1578 {
1579 /* Initialize the acommon section. */
1580 mips_elf_acom_section.name = ".acommon";
1581 mips_elf_acom_section.flags = SEC_NO_FLAGS;
1582 mips_elf_acom_section.output_section = &mips_elf_acom_section;
1583 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
1584 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
1585 mips_elf_acom_symbol.name = ".acommon";
1586 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
1587 mips_elf_acom_symbol.section = &mips_elf_acom_section;
1588 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
1589 }
1590 asym->section = &mips_elf_acom_section;
b3c0fc57
ILT
1591 break;
1592
1593 case SHN_COMMON:
1594 /* Common symbols less than the GP size are automatically
1595 treated as SHN_MIPS_SCOMMON symbols. */
1596 if (asym->value > elf_gp_size (abfd))
1597 break;
1598 /* Fall through. */
1599 case SHN_MIPS_SCOMMON:
1600 if (mips_elf_scom_section.name == NULL)
1601 {
1602 /* Initialize the small common section. */
1603 mips_elf_scom_section.name = ".scommon";
1604 mips_elf_scom_section.flags = SEC_IS_COMMON;
1605 mips_elf_scom_section.output_section = &mips_elf_scom_section;
1606 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
1607 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
1608 mips_elf_scom_symbol.name = ".scommon";
1609 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
1610 mips_elf_scom_symbol.section = &mips_elf_scom_section;
1611 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
1612 }
1613 asym->section = &mips_elf_scom_section;
1614 asym->value = elfsym->internal_elf_sym.st_size;
1615 break;
1616
1617 case SHN_MIPS_SUNDEFINED:
1618 asym->section = &bfd_und_section;
1619 break;
1620 }
1621}
1622\f
6e07e54f
ILT
1623/* ECOFF swapping routines. These are used when dealing with the
1624 .mdebug section, which is in the ECOFF debugging format. */
1625static const struct ecoff_debug_swap mips_elf_ecoff_debug_swap =
1626{
1627 /* Symbol table magic number. */
1628 magicSym,
1629 /* Alignment of debugging information. E.g., 4. */
1630 4,
1631 /* Sizes of external symbolic information. */
1632 sizeof (struct hdr_ext),
1633 sizeof (struct dnr_ext),
1634 sizeof (struct pdr_ext),
1635 sizeof (struct sym_ext),
1636 sizeof (struct opt_ext),
1637 sizeof (struct fdr_ext),
1638 sizeof (struct rfd_ext),
1639 sizeof (struct ext_ext),
1640 /* Functions to swap in external symbolic data. */
1641 ecoff_swap_hdr_in,
1642 ecoff_swap_dnr_in,
1643 ecoff_swap_pdr_in,
1644 ecoff_swap_sym_in,
1645 ecoff_swap_opt_in,
1646 ecoff_swap_fdr_in,
1647 ecoff_swap_rfd_in,
1648 ecoff_swap_ext_in,
1649 /* Functions to swap out external symbolic data. */
1650 ecoff_swap_hdr_out,
1651 ecoff_swap_dnr_out,
1652 ecoff_swap_pdr_out,
1653 ecoff_swap_sym_out,
1654 ecoff_swap_opt_out,
1655 ecoff_swap_fdr_out,
1656 ecoff_swap_rfd_out,
1657 ecoff_swap_ext_out
1658};
1659\f
6b4b4d17
JK
1660#define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
1661#define TARGET_LITTLE_NAME "elf32-littlemips"
1662#define TARGET_BIG_SYM bfd_elf32_bigmips_vec
1663#define TARGET_BIG_NAME "elf32-bigmips"
1664#define ELF_ARCH bfd_arch_mips
6e07e54f 1665#define ELF_MACHINE_CODE EM_MIPS
b3c0fc57 1666#define ELF_MAXPAGESIZE 0x10000
497c5434 1667#define elf_backend_collect true
b3c0fc57
ILT
1668#define elf_info_to_howto 0
1669#define elf_info_to_howto_rel mips_info_to_howto_rel
6e07e54f
ILT
1670#define elf_backend_sym_is_global mips_elf_sym_is_global
1671#define elf_backend_object_p mips_elf_object_p
b3c0fc57
ILT
1672#define elf_backend_section_from_shdr mips_elf_section_from_shdr
1673#define elf_backend_fake_sections mips_elf_fake_sections
1674#define elf_backend_section_from_bfd_section \
1675 mips_elf_section_from_bfd_section
1676#define elf_backend_section_processing mips_elf_section_processing
1677#define elf_backend_symbol_processing mips_elf_symbol_processing
6e07e54f
ILT
1678#define elf_backend_final_write_processing \
1679 mips_elf_final_write_processing
1680#define elf_backend_ecoff_debug_swap &mips_elf_ecoff_debug_swap
b3c0fc57 1681
a3a33af3 1682#define bfd_elf32_bfd_link_add_symbols _bfd_generic_link_add_symbols_collect
6e07e54f 1683#define bfd_elf32_bfd_final_link mips_elf_final_link
6b4b4d17
JK
1684
1685#include "elf32-target.h"
This page took 0.133657 seconds and 4 git commands to generate.