Fix obvious typo in comment.
[binutils.git] / bfd / coff-or32.c
blobc8f0fe03601af0ad472f94978c5c4cd80546ea02
1 /* BFD back-end for OpenRISC 1000 COFF binaries.
2 Copyright 2002, 2003, 2004, 2005 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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 /* Relative jmp/call, so subtract from the value the
152 address of the place we're coming from. */
153 signed_value -= (reloc_entry->address
154 + input_section->output_section->vma
155 + input_section->output_offset);
156 if (signed_value > 0x7ffffff || signed_value < -0x8000000)
157 return bfd_reloc_overflow;
159 signed_value >>= 2;
160 insn = INSERT_JUMPTARG (insn, signed_value);
161 bfd_put_32 (abfd, insn, hit_data);
162 break;
164 case R_ILOHALF:
165 insn = bfd_get_32 (abfd, hit_data);
166 unsigned_value = EXTRACT_HWORD (insn);
167 unsigned_value += sym_value + reloc_entry->addend;
168 insn = INSERT_HWORD (insn, unsigned_value);
169 bfd_put_32 (abfd, insn, hit_data);
170 break;
172 case R_IHIHALF:
173 insn = bfd_get_32 (abfd, hit_data);
175 /* consth, part 1
176 Just get the symbol value that is referenced. */
177 part1_consth_active = TRUE;
178 part1_consth_value = sym_value + reloc_entry->addend;
180 /* Don't modify insn until R_IHCONST. */
181 break;
183 case R_IHCONST:
184 insn = bfd_get_32 (abfd, hit_data);
186 /* consth, part 2
187 Now relocate the reference. */
188 if (! part1_consth_active)
190 *error_message = (char *) "Missing IHIHALF";
191 return bfd_reloc_dangerous;
194 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
195 unsigned_value = 0; /*EXTRACT_HWORD(insn) << 16;*/
196 unsigned_value += reloc_entry->addend; /* r_symndx */
197 unsigned_value += part1_consth_value;
198 unsigned_value = unsigned_value >> 16;
199 insn = INSERT_HWORD (insn, unsigned_value);
200 part1_consth_active = FALSE;
201 bfd_put_32 (abfd, insn, hit_data);
202 break;
204 case R_BYTE:
205 insn = bfd_get_8 (abfd, hit_data);
206 unsigned_value = insn + sym_value + reloc_entry->addend;
207 if (unsigned_value & 0xffffff00)
208 return bfd_reloc_overflow;
209 bfd_put_8 (abfd, unsigned_value, hit_data);
210 break;
212 case R_HWORD:
213 insn = bfd_get_16 (abfd, hit_data);
214 unsigned_value = insn + sym_value + reloc_entry->addend;
215 if (unsigned_value & 0xffff0000)
216 return bfd_reloc_overflow;
217 bfd_put_16 (abfd, insn, hit_data);
218 break;
220 case R_WORD:
221 insn = bfd_get_32 (abfd, hit_data);
222 insn += sym_value + reloc_entry->addend;
223 bfd_put_32 (abfd, insn, hit_data);
224 break;
226 default:
227 *error_message = _("Unrecognized reloc");
228 return bfd_reloc_dangerous;
231 return bfd_reloc_ok;
234 /* type rightshift
235 size
236 bitsize
237 pc-relative
238 bitpos
239 absolute
240 complain_on_overflow
241 special_function
242 relocation name
243 partial_inplace
244 src_mask
247 /* FIXME: I'm not real sure about this table. */
248 static reloc_howto_type howto_table[] =
250 { R_ABS, 0, 3, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "ABS", TRUE, 0xffffffff,0xffffffff, FALSE },
251 EMPTY_HOWTO (1),
252 EMPTY_HOWTO (2),
253 EMPTY_HOWTO (3),
254 EMPTY_HOWTO (4),
255 EMPTY_HOWTO (5),
256 EMPTY_HOWTO (6),
257 EMPTY_HOWTO (7),
258 EMPTY_HOWTO (8),
259 EMPTY_HOWTO (9),
260 EMPTY_HOWTO (10),
261 EMPTY_HOWTO (11),
262 EMPTY_HOWTO (12),
263 EMPTY_HOWTO (13),
264 EMPTY_HOWTO (14),
265 EMPTY_HOWTO (15),
266 EMPTY_HOWTO (16),
267 EMPTY_HOWTO (17),
268 EMPTY_HOWTO (18),
269 EMPTY_HOWTO (19),
270 EMPTY_HOWTO (20),
271 EMPTY_HOWTO (21),
272 EMPTY_HOWTO (22),
273 EMPTY_HOWTO (23),
274 { R_IREL, 0, 3, 32, TRUE, 0, complain_overflow_signed, or32_reloc, "IREL", TRUE, 0xffffffff,0xffffffff, FALSE },
275 { R_IABS, 0, 3, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "IABS", TRUE, 0xffffffff,0xffffffff, FALSE },
276 { R_ILOHALF, 0, 3, 16, TRUE, 0, complain_overflow_signed, or32_reloc, "ILOHALF", TRUE, 0x0000ffff,0x0000ffff, FALSE },
277 { R_IHIHALF, 0, 3, 16, TRUE, 16,complain_overflow_signed, or32_reloc, "IHIHALF", TRUE, 0xffff0000,0xffff0000, FALSE },
278 { R_IHCONST, 0, 3, 16, TRUE, 0, complain_overflow_signed, or32_reloc, "IHCONST", TRUE, 0xffff0000,0xffff0000, FALSE },
279 { R_BYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, or32_reloc, "BYTE", TRUE, 0x000000ff,0x000000ff, FALSE },
280 { R_HWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, or32_reloc, "HWORD", TRUE, 0x0000ffff,0x0000ffff, FALSE },
281 { R_WORD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "WORD", TRUE, 0xffffffff,0xffffffff, FALSE },
284 #define BADMAG(x) OR32BADMAG (x)
286 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
287 reloc_processing (relent, reloc, symbols, abfd, section)
289 static void
290 reloc_processing (relent,reloc, symbols, abfd, section)
291 arelent *relent;
292 struct internal_reloc *reloc;
293 asymbol **symbols;
294 bfd *abfd;
295 asection *section;
297 static bfd_vma ihihalf_vaddr = (bfd_vma) -1;
299 relent->address = reloc->r_vaddr;
300 relent->howto = howto_table + reloc->r_type;
302 if (reloc->r_type == R_IHCONST)
304 /* The address of an R_IHCONST should always be the address of
305 the immediately preceding R_IHIHALF. relocs generated by gas
306 are correct, but relocs generated by High C are different (I
307 can't figure out what the address means for High C). We can
308 handle both gas and High C by ignoring the address here, and
309 simply reusing the address saved for R_IHIHALF. */
310 if (ihihalf_vaddr == (bfd_vma) -1)
311 abort ();
313 relent->address = ihihalf_vaddr;
314 ihihalf_vaddr = (bfd_vma) -1;
315 relent->addend = reloc->r_symndx;
316 relent->sym_ptr_ptr= bfd_abs_section_ptr->symbol_ptr_ptr;
318 else
320 asymbol *ptr;
321 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
323 ptr = *(relent->sym_ptr_ptr);
325 relent->addend = 0;
326 relent->address-= section->vma;
328 if (reloc->r_type == R_IHIHALF)
329 ihihalf_vaddr = relent->address;
330 else if (ihihalf_vaddr != (bfd_vma) -1)
331 abort ();
335 /* The reloc processing routine for the optimized COFF linker. */
337 static bfd_boolean
338 coff_or32_relocate_section (output_bfd, info, input_bfd, input_section,
339 contents, relocs, syms, sections)
340 bfd *output_bfd ATTRIBUTE_UNUSED;
341 struct bfd_link_info *info;
342 bfd *input_bfd;
343 asection *input_section;
344 bfd_byte *contents;
345 struct internal_reloc *relocs;
346 struct internal_syment *syms;
347 asection **sections;
349 struct internal_reloc *rel;
350 struct internal_reloc *relend;
351 bfd_boolean hihalf;
352 bfd_vma hihalf_val;
354 /* If we are performing a relocatable link, we don't need to do a
355 thing. The caller will take care of adjusting the reloc
356 addresses and symbol indices. */
357 if (info->relocatable)
358 return TRUE;
360 hihalf = FALSE;
361 hihalf_val = 0;
363 rel = relocs;
364 relend = rel + input_section->reloc_count;
366 for (; rel < relend; rel++)
368 long symndx;
369 bfd_byte *loc;
370 struct coff_link_hash_entry *h;
371 struct internal_syment *sym;
372 asection *sec;
373 bfd_vma val;
374 bfd_boolean overflow;
375 unsigned long insn;
376 long signed_value;
377 unsigned long unsigned_value;
378 bfd_reloc_status_type rstat;
380 symndx = rel->r_symndx;
381 loc = contents + rel->r_vaddr - input_section->vma;
383 if (symndx == -1 || rel->r_type == R_IHCONST)
384 h = NULL;
385 else
386 h = obj_coff_sym_hashes (input_bfd)[symndx];
388 sym = NULL;
389 sec = NULL;
390 val = 0;
392 /* An R_IHCONST reloc does not have a symbol. Instead, the
393 symbol index is an addend. R_IHCONST is always used in
394 conjunction with R_IHHALF. */
395 if (rel->r_type != R_IHCONST)
397 if (h == NULL)
399 if (symndx == -1)
400 sec = bfd_abs_section_ptr;
401 else
403 sym = syms + symndx;
404 sec = sections[symndx];
405 val = (sec->output_section->vma
406 + sec->output_offset
407 + sym->n_value
408 - sec->vma);
411 else
413 if (h->root.type == bfd_link_hash_defined
414 || h->root.type == bfd_link_hash_defweak)
416 sec = h->root.u.def.section;
417 val = (h->root.u.def.value
418 + sec->output_section->vma
419 + sec->output_offset);
421 else
423 if (! ((*info->callbacks->undefined_symbol)
424 (info, h->root.root.string, input_bfd, input_section,
425 rel->r_vaddr - input_section->vma, TRUE)))
426 return FALSE;
430 if (hihalf)
432 if (! ((*info->callbacks->reloc_dangerous)
433 (info, "missing IHCONST reloc", input_bfd,
434 input_section, rel->r_vaddr - input_section->vma)))
435 return FALSE;
436 hihalf = FALSE;
440 overflow = FALSE;
442 switch (rel->r_type)
444 default:
445 bfd_set_error (bfd_error_bad_value);
446 return FALSE;
448 case R_IREL:
449 insn = bfd_get_32 (input_bfd, loc);
451 /* Extract the addend. */
452 signed_value = EXTRACT_JUMPTARG (insn);
453 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
454 signed_value <<= 2;
456 /* Determine the destination of the jump. */
457 signed_value += val;
459 /* Make the destination PC relative. */
460 signed_value -= (input_section->output_section->vma
461 + input_section->output_offset
462 + (rel->r_vaddr - input_section->vma));
463 if (signed_value > 0x7ffffff || signed_value < - 0x8000000)
465 overflow = TRUE;
466 signed_value = 0;
469 /* Put the adjusted value back into the instruction. */
470 signed_value >>= 2;
471 insn = INSERT_JUMPTARG(insn, signed_value);
473 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
474 break;
476 case R_ILOHALF:
477 insn = bfd_get_32 (input_bfd, loc);
478 unsigned_value = EXTRACT_HWORD (insn);
479 unsigned_value += val;
480 insn = INSERT_HWORD (insn, unsigned_value);
481 bfd_put_32 (input_bfd, insn, loc);
482 break;
484 case R_IHIHALF:
485 /* Save the value for the R_IHCONST reloc. */
486 hihalf = TRUE;
487 hihalf_val = val;
488 break;
490 case R_IHCONST:
491 if (! hihalf)
493 if (! ((*info->callbacks->reloc_dangerous)
494 (info, "missing IHIHALF reloc", input_bfd,
495 input_section, rel->r_vaddr - input_section->vma)))
496 return FALSE;
497 hihalf_val = 0;
500 insn = bfd_get_32 (input_bfd, loc);
501 unsigned_value = rel->r_symndx + hihalf_val;
502 unsigned_value >>= 16;
503 insn = INSERT_HWORD (insn, unsigned_value);
504 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
506 hihalf = FALSE;
507 break;
509 case R_BYTE:
510 case R_HWORD:
511 case R_WORD:
512 rstat = _bfd_relocate_contents (howto_table + rel->r_type,
513 input_bfd, val, loc);
514 if (rstat == bfd_reloc_overflow)
515 overflow = TRUE;
516 else if (rstat != bfd_reloc_ok)
517 abort ();
518 break;
521 if (overflow)
523 const char *name;
524 char buf[SYMNMLEN + 1];
526 if (symndx == -1)
527 name = "*ABS*";
528 else if (h != NULL)
529 name = NULL;
530 else if (sym == NULL)
531 name = "*unknown*";
532 else if (sym->_n._n_n._n_zeroes == 0
533 && sym->_n._n_n._n_offset != 0)
534 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
535 else
537 strncpy (buf, sym->_n._n_name, SYMNMLEN);
538 buf[SYMNMLEN] = '\0';
539 name = buf;
542 if (! ((*info->callbacks->reloc_overflow)
543 (info, (h ? &h->root : NULL), name,
544 howto_table[rel->r_type].name, (bfd_vma) 0, input_bfd,
545 input_section, rel->r_vaddr - input_section->vma)))
546 return FALSE;
550 return TRUE;
553 #define coff_relocate_section coff_or32_relocate_section
555 /* We don't want to change the symndx of a R_IHCONST reloc, since it
556 is actually an addend, not a symbol index at all. */
558 static bfd_boolean
559 coff_or32_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
560 bfd *obfd ATTRIBUTE_UNUSED;
561 struct bfd_link_info *info ATTRIBUTE_UNUSED;
562 bfd *ibfd ATTRIBUTE_UNUSED;
563 asection *sec ATTRIBUTE_UNUSED;
564 struct internal_reloc *irel;
565 bfd_boolean *adjustedp;
567 if (irel->r_type == R_IHCONST)
568 *adjustedp = TRUE;
569 else
570 *adjustedp = FALSE;
571 return TRUE;
574 #define coff_adjust_symndx coff_or32_adjust_symndx
576 #include "coffcode.h"
578 const bfd_target or32coff_big_vec =
580 "coff-or32-big", /* Name. */
581 bfd_target_coff_flavour,
582 BFD_ENDIAN_BIG, /* Data byte order is big. */
583 BFD_ENDIAN_BIG, /* Header byte order is big. */
585 (HAS_RELOC | EXEC_P | /* Object flags. */
586 HAS_LINENO | HAS_DEBUG |
587 HAS_SYMS | HAS_LOCALS | WP_TEXT),
589 (SEC_HAS_CONTENTS | SEC_ALLOC | /* Section flags. */
590 SEC_LOAD | SEC_RELOC |
591 SEC_READONLY ),
592 '_', /* Leading underscore. */
593 '/', /* ar_pad_char. */
594 15, /* ar_max_namelen. */
596 /* Data. */
597 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
598 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
599 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
601 /* Headers. */
602 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
603 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
604 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
607 _bfd_dummy_target,
608 coff_object_p,
609 bfd_generic_archive_p,
610 _bfd_dummy_target
613 bfd_false,
614 coff_mkobject,
615 _bfd_generic_mkarchive,
616 bfd_false
619 bfd_false,
620 coff_write_object_contents,
621 _bfd_write_archive_contents,
622 bfd_false
625 BFD_JUMP_TABLE_GENERIC (coff),
626 BFD_JUMP_TABLE_COPY (coff),
627 BFD_JUMP_TABLE_CORE (_bfd_nocore),
628 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
629 BFD_JUMP_TABLE_SYMBOLS (coff),
630 BFD_JUMP_TABLE_RELOCS (coff),
631 BFD_JUMP_TABLE_WRITE (coff),
632 BFD_JUMP_TABLE_LINK (coff),
633 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
635 /* Alternative_target. */
636 #ifdef TARGET_LITTLE_SYM
637 & TARGET_LITTLE_SYM,
638 #else
639 NULL,
640 #endif
642 COFF_SWAP_TABLE