2004-04-02 Andrew Cagney <cagney@redhat.com>
[binutils.git] / bfd / coff-or32.c
blobb21cdf3f4ade1fe5d20818569442e3bdefccd830
1 /* BFD back-end for OpenRISC 1000 COFF binaries.
2 Copyright 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Ivan Guzvinec <ivang@opencores.org>
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #define OR32 1
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "coff/or32.h"
27 #include "coff/internal.h"
28 #include "libcoff.h"
30 static long get_symbol_value
31 PARAMS ((asymbol *));
32 static bfd_reloc_status_type or32_reloc
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34 static bfd_boolean coff_or32_relocate_section
35 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
36 struct internal_reloc *, struct internal_syment *, asection **));
37 static bfd_boolean coff_or32_adjust_symndx
38 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
39 struct internal_reloc *, bfd_boolean *));
40 static void reloc_processing
41 PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
43 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
45 #define INSERT_HWORD(WORD,HWORD) \
46 (((WORD) & 0xffff0000) | ((HWORD)& 0x0000ffff))
47 #define EXTRACT_HWORD(WORD) \
48 ((WORD) & 0x0000ffff)
49 #define SIGN_EXTEND_HWORD(HWORD) \
50 ((HWORD) & 0x8000 ? (HWORD)|(~0xffffL) : (HWORD))
52 #define INSERT_JUMPTARG(WORD,JT) \
53 (((WORD) & 0xfc000000) | ((JT)& 0x03ffffff))
54 #define EXTRACT_JUMPTARG(WORD) \
55 ((WORD) & 0x03ffffff)
56 #define SIGN_EXTEND_JUMPTARG(JT) \
57 ((JT) & 0x04000000 ? (JT)|(~0x03ffffffL) : (JT))
59 /* Provided the symbol, returns the value reffed. */
61 static long
62 get_symbol_value (symbol)
63 asymbol *symbol;
65 long relocation = 0;
67 if (bfd_is_com_section (symbol->section))
68 relocation = 0;
69 else
70 relocation = symbol->value +
71 symbol->section->output_section->vma +
72 symbol->section->output_offset;
74 return relocation;
77 /* This function is in charge of performing all the or32 relocations. */
79 static bfd_reloc_status_type
80 or32_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
81 error_message)
82 bfd *abfd;
83 arelent *reloc_entry;
84 asymbol *symbol_in;
85 PTR data;
86 asection *input_section;
87 bfd *output_bfd;
88 char **error_message;
90 /* The consth relocation comes in two parts, we have to remember
91 the state between calls, in these variables. */
92 static bfd_boolean part1_consth_active = FALSE;
93 static unsigned long part1_consth_value;
95 unsigned long insn;
96 unsigned long sym_value;
97 unsigned long unsigned_value;
98 unsigned short r_type;
99 long signed_value;
101 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
102 bfd_byte *hit_data =addr + (bfd_byte *)(data);
104 r_type = reloc_entry->howto->type;
106 if (output_bfd)
108 /* Partial linking - do nothing. */
109 reloc_entry->address += input_section->output_offset;
110 return bfd_reloc_ok;
113 if (symbol_in != NULL
114 && bfd_is_und_section (symbol_in->section))
116 /* Keep the state machine happy in case we're called again. */
117 if (r_type == R_IHIHALF)
119 part1_consth_active = TRUE;
120 part1_consth_value = 0;
123 return bfd_reloc_undefined;
126 if ((part1_consth_active) && (r_type != R_IHCONST))
128 part1_consth_active = FALSE;
129 *error_message = (char *) "Missing IHCONST";
131 return bfd_reloc_dangerous;
134 sym_value = get_symbol_value (symbol_in);
136 switch (r_type)
138 case R_IREL:
139 insn = bfd_get_32(abfd, hit_data);
141 /* Take the value in the field and sign extend it. */
142 signed_value = EXTRACT_JUMPTARG (insn);
143 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
144 signed_value <<= 2;
146 /* See the note on the R_IREL reloc in coff_or32_relocate_section. */
147 if (signed_value == - (long) reloc_entry->address)
148 signed_value = 0;
150 signed_value += sym_value + reloc_entry->addend;
151 #if 0
152 if ((signed_value & ~0x3ffff) == 0)
153 { /* Absolute jmp/call. */
154 insn |= (1<<24); /* Make it absolute. */
155 /* FIXME: Should we change r_type to R_IABS. */
157 else
158 #endif
160 /* Relative jmp/call, so subtract from the value the
161 address of the place we're coming from. */
162 signed_value -= (reloc_entry->address
163 + input_section->output_section->vma
164 + input_section->output_offset);
165 if (signed_value > 0x7ffffff || signed_value < -0x8000000)
166 return bfd_reloc_overflow;
168 signed_value >>= 2;
169 insn = INSERT_JUMPTARG (insn, signed_value);
170 bfd_put_32 (abfd, insn, hit_data);
171 break;
173 case R_ILOHALF:
174 insn = bfd_get_32 (abfd, hit_data);
175 unsigned_value = EXTRACT_HWORD (insn);
176 unsigned_value += sym_value + reloc_entry->addend;
177 insn = INSERT_HWORD (insn, unsigned_value);
178 bfd_put_32 (abfd, insn, hit_data);
179 break;
181 case R_IHIHALF:
182 insn = bfd_get_32 (abfd, hit_data);
184 /* consth, part 1
185 Just get the symbol value that is referenced. */
186 part1_consth_active = TRUE;
187 part1_consth_value = sym_value + reloc_entry->addend;
189 /* Don't modify insn until R_IHCONST. */
190 break;
192 case R_IHCONST:
193 insn = bfd_get_32 (abfd, hit_data);
195 /* consth, part 2
196 Now relocate the reference. */
197 if (! part1_consth_active)
199 *error_message = (char *) "Missing IHIHALF";
200 return bfd_reloc_dangerous;
203 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
204 unsigned_value = 0; /*EXTRACT_HWORD(insn) << 16;*/
205 unsigned_value += reloc_entry->addend; /* r_symndx */
206 unsigned_value += part1_consth_value;
207 unsigned_value = unsigned_value >> 16;
208 insn = INSERT_HWORD (insn, unsigned_value);
209 part1_consth_active = FALSE;
210 bfd_put_32 (abfd, insn, hit_data);
211 break;
213 case R_BYTE:
214 insn = bfd_get_8 (abfd, hit_data);
215 unsigned_value = insn + sym_value + reloc_entry->addend;
216 if (unsigned_value & 0xffffff00)
217 return bfd_reloc_overflow;
218 bfd_put_8 (abfd, unsigned_value, hit_data);
219 break;
221 case R_HWORD:
222 insn = bfd_get_16 (abfd, hit_data);
223 unsigned_value = insn + sym_value + reloc_entry->addend;
224 if (unsigned_value & 0xffff0000)
225 return bfd_reloc_overflow;
226 bfd_put_16 (abfd, insn, hit_data);
227 break;
229 case R_WORD:
230 insn = bfd_get_32 (abfd, hit_data);
231 insn += sym_value + reloc_entry->addend;
232 bfd_put_32 (abfd, insn, hit_data);
233 break;
235 default:
236 *error_message = _("Unrecognized reloc");
237 return bfd_reloc_dangerous;
240 return bfd_reloc_ok;
243 /* type rightshift
244 size
245 bitsize
246 pc-relative
247 bitpos
248 absolute
249 complain_on_overflow
250 special_function
251 relocation name
252 partial_inplace
253 src_mask
256 /* FIXME: I'm not real sure about this table. */
257 static reloc_howto_type howto_table[] =
259 { R_ABS, 0, 3, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "ABS", TRUE, 0xffffffff,0xffffffff, FALSE },
260 EMPTY_HOWTO (1),
261 EMPTY_HOWTO (2),
262 EMPTY_HOWTO (3),
263 EMPTY_HOWTO (4),
264 EMPTY_HOWTO (5),
265 EMPTY_HOWTO (6),
266 EMPTY_HOWTO (7),
267 EMPTY_HOWTO (8),
268 EMPTY_HOWTO (9),
269 EMPTY_HOWTO (10),
270 EMPTY_HOWTO (11),
271 EMPTY_HOWTO (12),
272 EMPTY_HOWTO (13),
273 EMPTY_HOWTO (14),
274 EMPTY_HOWTO (15),
275 EMPTY_HOWTO (16),
276 EMPTY_HOWTO (17),
277 EMPTY_HOWTO (18),
278 EMPTY_HOWTO (19),
279 EMPTY_HOWTO (20),
280 EMPTY_HOWTO (21),
281 EMPTY_HOWTO (22),
282 EMPTY_HOWTO (23),
283 { R_IREL, 0, 3, 32, TRUE, 0, complain_overflow_signed, or32_reloc, "IREL", TRUE, 0xffffffff,0xffffffff, FALSE },
284 { R_IABS, 0, 3, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "IABS", TRUE, 0xffffffff,0xffffffff, FALSE },
285 { R_ILOHALF, 0, 3, 16, TRUE, 0, complain_overflow_signed, or32_reloc, "ILOHALF", TRUE, 0x0000ffff,0x0000ffff, FALSE },
286 { R_IHIHALF, 0, 3, 16, TRUE, 16,complain_overflow_signed, or32_reloc, "IHIHALF", TRUE, 0xffff0000,0xffff0000, FALSE },
287 { R_IHCONST, 0, 3, 16, TRUE, 0, complain_overflow_signed, or32_reloc, "IHCONST", TRUE, 0xffff0000,0xffff0000, FALSE },
288 { R_BYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, or32_reloc, "BYTE", TRUE, 0x000000ff,0x000000ff, FALSE },
289 { R_HWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, or32_reloc, "HWORD", TRUE, 0x0000ffff,0x0000ffff, FALSE },
290 { R_WORD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "WORD", TRUE, 0xffffffff,0xffffffff, FALSE },
293 #define BADMAG(x) OR32BADMAG (x)
295 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
296 reloc_processing (relent, reloc, symbols, abfd, section)
298 static void
299 reloc_processing (relent,reloc, symbols, abfd, section)
300 arelent *relent;
301 struct internal_reloc *reloc;
302 asymbol **symbols;
303 bfd *abfd;
304 asection *section;
306 static bfd_vma ihihalf_vaddr = (bfd_vma) -1;
308 relent->address = reloc->r_vaddr;
309 relent->howto = howto_table + reloc->r_type;
311 if (reloc->r_type == R_IHCONST)
313 /* The address of an R_IHCONST should always be the address of
314 the immediately preceding R_IHIHALF. relocs generated by gas
315 are correct, but relocs generated by High C are different (I
316 can't figure out what the address means for High C). We can
317 handle both gas and High C by ignoring the address here, and
318 simply reusing the address saved for R_IHIHALF. */
319 if (ihihalf_vaddr == (bfd_vma) -1)
320 abort ();
322 relent->address = ihihalf_vaddr;
323 ihihalf_vaddr = (bfd_vma) -1;
324 relent->addend = reloc->r_symndx;
325 relent->sym_ptr_ptr= bfd_abs_section_ptr->symbol_ptr_ptr;
327 else
329 asymbol *ptr;
330 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
332 ptr = *(relent->sym_ptr_ptr);
334 relent->addend = 0;
335 relent->address-= section->vma;
337 if (reloc->r_type == R_IHIHALF)
338 ihihalf_vaddr = relent->address;
339 else if (ihihalf_vaddr != (bfd_vma) -1)
340 abort ();
344 /* The reloc processing routine for the optimized COFF linker. */
346 static bfd_boolean
347 coff_or32_relocate_section (output_bfd, info, input_bfd, input_section,
348 contents, relocs, syms, sections)
349 bfd *output_bfd ATTRIBUTE_UNUSED;
350 struct bfd_link_info *info;
351 bfd *input_bfd;
352 asection *input_section;
353 bfd_byte *contents;
354 struct internal_reloc *relocs;
355 struct internal_syment *syms;
356 asection **sections;
358 struct internal_reloc *rel;
359 struct internal_reloc *relend;
360 bfd_boolean hihalf;
361 bfd_vma hihalf_val;
363 /* If we are performing a relocatable link, we don't need to do a
364 thing. The caller will take care of adjusting the reloc
365 addresses and symbol indices. */
366 if (info->relocatable)
367 return TRUE;
369 hihalf = FALSE;
370 hihalf_val = 0;
372 rel = relocs;
373 relend = rel + input_section->reloc_count;
375 for (; rel < relend; rel++)
377 long symndx;
378 bfd_byte *loc;
379 struct coff_link_hash_entry *h;
380 struct internal_syment *sym;
381 asection *sec;
382 bfd_vma val;
383 bfd_boolean overflow;
384 unsigned long insn;
385 long signed_value;
386 unsigned long unsigned_value;
387 bfd_reloc_status_type rstat;
389 symndx = rel->r_symndx;
390 loc = contents + rel->r_vaddr - input_section->vma;
392 if (symndx == -1 || rel->r_type == R_IHCONST)
393 h = NULL;
394 else
395 h = obj_coff_sym_hashes (input_bfd)[symndx];
397 sym = NULL;
398 sec = NULL;
399 val = 0;
401 /* An R_IHCONST reloc does not have a symbol. Instead, the
402 symbol index is an addend. R_IHCONST is always used in
403 conjunction with R_IHHALF. */
404 if (rel->r_type != R_IHCONST)
406 if (h == NULL)
408 if (symndx == -1)
409 sec = bfd_abs_section_ptr;
410 else
412 sym = syms + symndx;
413 sec = sections[symndx];
414 val = (sec->output_section->vma
415 + sec->output_offset
416 + sym->n_value
417 - sec->vma);
420 else
422 if (h->root.type == bfd_link_hash_defined
423 || h->root.type == bfd_link_hash_defweak)
425 sec = h->root.u.def.section;
426 val = (h->root.u.def.value
427 + sec->output_section->vma
428 + sec->output_offset);
430 else
432 if (! ((*info->callbacks->undefined_symbol)
433 (info, h->root.root.string, input_bfd, input_section,
434 rel->r_vaddr - input_section->vma, TRUE)))
435 return FALSE;
439 if (hihalf)
441 if (! ((*info->callbacks->reloc_dangerous)
442 (info, "missing IHCONST reloc", input_bfd,
443 input_section, rel->r_vaddr - input_section->vma)))
444 return FALSE;
445 hihalf = FALSE;
449 overflow = FALSE;
451 switch (rel->r_type)
453 default:
454 bfd_set_error (bfd_error_bad_value);
455 return FALSE;
457 case R_IREL:
458 insn = bfd_get_32 (input_bfd, loc);
460 /* Extract the addend. */
461 signed_value = EXTRACT_JUMPTARG (insn);
462 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
463 signed_value <<= 2;
465 /* Determine the destination of the jump. */
466 signed_value += val;
468 #if 0
469 if ((signed_value & ~0x3ffff) == 0)
471 /* We can use an absolute jump. */
472 insn |= (1 << 24);
474 else
475 #endif
477 /* Make the destination PC relative. */
478 signed_value -= (input_section->output_section->vma
479 + input_section->output_offset
480 + (rel->r_vaddr - input_section->vma));
481 if (signed_value > 0x7ffffff || signed_value < - 0x8000000)
483 overflow = TRUE;
484 signed_value = 0;
488 /* Put the adjusted value back into the instruction. */
489 signed_value >>= 2;
490 insn = INSERT_JUMPTARG(insn, signed_value);
492 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
493 break;
495 case R_ILOHALF:
496 insn = bfd_get_32 (input_bfd, loc);
497 unsigned_value = EXTRACT_HWORD (insn);
498 unsigned_value += val;
499 insn = INSERT_HWORD (insn, unsigned_value);
500 bfd_put_32 (input_bfd, insn, loc);
501 break;
503 case R_IHIHALF:
504 /* Save the value for the R_IHCONST reloc. */
505 hihalf = TRUE;
506 hihalf_val = val;
507 break;
509 case R_IHCONST:
510 if (! hihalf)
512 if (! ((*info->callbacks->reloc_dangerous)
513 (info, "missing IHIHALF reloc", input_bfd,
514 input_section, rel->r_vaddr - input_section->vma)))
515 return FALSE;
516 hihalf_val = 0;
519 insn = bfd_get_32 (input_bfd, loc);
520 unsigned_value = rel->r_symndx + hihalf_val;
521 unsigned_value >>= 16;
522 insn = INSERT_HWORD (insn, unsigned_value);
523 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
525 hihalf = FALSE;
526 break;
528 case R_BYTE:
529 case R_HWORD:
530 case R_WORD:
531 rstat = _bfd_relocate_contents (howto_table + rel->r_type,
532 input_bfd, val, loc);
533 if (rstat == bfd_reloc_overflow)
534 overflow = TRUE;
535 else if (rstat != bfd_reloc_ok)
536 abort ();
537 break;
540 if (overflow)
542 const char *name;
543 char buf[SYMNMLEN + 1];
545 if (symndx == -1)
546 name = "*ABS*";
547 else if (h != NULL)
548 name = h->root.root.string;
549 else if (sym == NULL)
550 name = "*unknown*";
551 else if (sym->_n._n_n._n_zeroes == 0
552 && sym->_n._n_n._n_offset != 0)
553 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
554 else
556 strncpy (buf, sym->_n._n_name, SYMNMLEN);
557 buf[SYMNMLEN] = '\0';
558 name = buf;
561 if (! ((*info->callbacks->reloc_overflow)
562 (info, name, howto_table[rel->r_type].name, (bfd_vma) 0,
563 input_bfd, input_section,
564 rel->r_vaddr - input_section->vma)))
565 return FALSE;
569 return TRUE;
572 #define coff_relocate_section coff_or32_relocate_section
574 /* We don't want to change the symndx of a R_IHCONST reloc, since it
575 is actually an addend, not a symbol index at all. */
577 static bfd_boolean
578 coff_or32_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
579 bfd *obfd ATTRIBUTE_UNUSED;
580 struct bfd_link_info *info ATTRIBUTE_UNUSED;
581 bfd *ibfd ATTRIBUTE_UNUSED;
582 asection *sec ATTRIBUTE_UNUSED;
583 struct internal_reloc *irel;
584 bfd_boolean *adjustedp;
586 if (irel->r_type == R_IHCONST)
587 *adjustedp = TRUE;
588 else
589 *adjustedp = FALSE;
590 return TRUE;
593 #define coff_adjust_symndx coff_or32_adjust_symndx
595 #include "coffcode.h"
597 const bfd_target or32coff_big_vec =
599 "coff-or32-big", /* Name. */
600 bfd_target_coff_flavour,
601 BFD_ENDIAN_BIG, /* Data byte order is big. */
602 BFD_ENDIAN_BIG, /* Header byte order is big. */
604 (HAS_RELOC | EXEC_P | /* Object flags. */
605 HAS_LINENO | HAS_DEBUG |
606 HAS_SYMS | HAS_LOCALS | WP_TEXT),
608 (SEC_HAS_CONTENTS | SEC_ALLOC | /* Section flags. */
609 SEC_LOAD | SEC_RELOC |
610 SEC_READONLY ),
611 '_', /* Leading underscore. */
612 '/', /* ar_pad_char. */
613 15, /* ar_max_namelen. */
615 /* Data. */
616 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
617 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
618 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
620 /* Headers. */
621 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
622 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
623 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
626 _bfd_dummy_target,
627 coff_object_p,
628 bfd_generic_archive_p,
629 _bfd_dummy_target
632 bfd_false,
633 coff_mkobject,
634 _bfd_generic_mkarchive,
635 bfd_false
638 bfd_false,
639 coff_write_object_contents,
640 _bfd_write_archive_contents,
641 bfd_false
644 BFD_JUMP_TABLE_GENERIC (coff),
645 BFD_JUMP_TABLE_COPY (coff),
646 BFD_JUMP_TABLE_CORE (_bfd_nocore),
647 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
648 BFD_JUMP_TABLE_SYMBOLS (coff),
649 BFD_JUMP_TABLE_RELOCS (coff),
650 BFD_JUMP_TABLE_WRITE (coff),
651 BFD_JUMP_TABLE_LINK (coff),
652 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
654 /* Alternative_target. */
655 #ifdef TARGET_LITTLE_SYM
656 & TARGET_LITTLE_SYM,
657 #else
658 NULL,
659 #endif
661 COFF_SWAP_TABLE