merge from gcc
[gdb/gnu.git] / bfd / coff-or32.c
blob18835e1373ac04ee4c7c035d44b7d50441645810
1 /* BFD back-end for OpenRISC 1000 COFF binaries.
2 Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Ivan Guzvinec <ivang@opencores.org>
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define OR32 1
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libbfd.h"
28 #include "coff/or32.h"
29 #include "coff/internal.h"
30 #include "libcoff.h"
32 static bfd_reloc_status_type or32_reloc
33 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
35 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
37 #define INSERT_HWORD(WORD,HWORD) \
38 (((WORD) & 0xffff0000) | ((HWORD)& 0x0000ffff))
39 #define EXTRACT_HWORD(WORD) \
40 ((WORD) & 0x0000ffff)
41 #define SIGN_EXTEND_HWORD(HWORD) \
42 ((HWORD) & 0x8000 ? (HWORD)|(~0xffffL) : (HWORD))
44 #define INSERT_JUMPTARG(WORD,JT) \
45 (((WORD) & 0xfc000000) | ((JT)& 0x03ffffff))
46 #define EXTRACT_JUMPTARG(WORD) \
47 ((WORD) & 0x03ffffff)
48 #define SIGN_EXTEND_JUMPTARG(JT) \
49 ((JT) & 0x04000000 ? (JT)|(~0x03ffffffL) : (JT))
51 /* Provided the symbol, returns the value reffed. */
53 static long
54 get_symbol_value (asymbol *symbol)
56 long relocation = 0;
58 if (bfd_is_com_section (symbol->section))
59 relocation = 0;
60 else
61 relocation = symbol->value +
62 symbol->section->output_section->vma +
63 symbol->section->output_offset;
65 return relocation;
68 /* This function is in charge of performing all the or32 relocations. */
70 static bfd_reloc_status_type
71 or32_reloc (bfd *abfd,
72 arelent *reloc_entry,
73 asymbol *symbol_in,
74 void * data,
75 asection *input_section,
76 bfd *output_bfd,
77 char **error_message)
79 /* The consth relocation comes in two parts, we have to remember
80 the state between calls, in these variables. */
81 static bfd_boolean part1_consth_active = FALSE;
82 static unsigned long part1_consth_value;
84 unsigned long insn;
85 unsigned long sym_value;
86 unsigned long unsigned_value;
87 unsigned short r_type;
88 long signed_value;
90 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
91 bfd_byte *hit_data =addr + (bfd_byte *)(data);
93 r_type = reloc_entry->howto->type;
95 if (output_bfd)
97 /* Partial linking - do nothing. */
98 reloc_entry->address += input_section->output_offset;
99 return bfd_reloc_ok;
102 if (symbol_in != NULL
103 && bfd_is_und_section (symbol_in->section))
105 /* Keep the state machine happy in case we're called again. */
106 if (r_type == R_IHIHALF)
108 part1_consth_active = TRUE;
109 part1_consth_value = 0;
112 return bfd_reloc_undefined;
115 if ((part1_consth_active) && (r_type != R_IHCONST))
117 part1_consth_active = FALSE;
118 *error_message = (char *) "Missing IHCONST";
120 return bfd_reloc_dangerous;
123 sym_value = get_symbol_value (symbol_in);
125 switch (r_type)
127 case R_IREL:
128 insn = bfd_get_32(abfd, hit_data);
130 /* Take the value in the field and sign extend it. */
131 signed_value = EXTRACT_JUMPTARG (insn);
132 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
133 signed_value <<= 2;
135 /* See the note on the R_IREL reloc in coff_or32_relocate_section. */
136 if (signed_value == - (long) reloc_entry->address)
137 signed_value = 0;
139 signed_value += sym_value + reloc_entry->addend;
140 /* Relative jmp/call, so subtract from the value the
141 address of the place we're coming from. */
142 signed_value -= (reloc_entry->address
143 + input_section->output_section->vma
144 + input_section->output_offset);
145 if (signed_value > 0x7ffffff || signed_value < -0x8000000)
146 return bfd_reloc_overflow;
148 signed_value >>= 2;
149 insn = INSERT_JUMPTARG (insn, signed_value);
150 bfd_put_32 (abfd, insn, hit_data);
151 break;
153 case R_ILOHALF:
154 insn = bfd_get_32 (abfd, hit_data);
155 unsigned_value = EXTRACT_HWORD (insn);
156 unsigned_value += sym_value + reloc_entry->addend;
157 insn = INSERT_HWORD (insn, unsigned_value);
158 bfd_put_32 (abfd, insn, hit_data);
159 break;
161 case R_IHIHALF:
162 insn = bfd_get_32 (abfd, hit_data);
164 /* consth, part 1
165 Just get the symbol value that is referenced. */
166 part1_consth_active = TRUE;
167 part1_consth_value = sym_value + reloc_entry->addend;
169 /* Don't modify insn until R_IHCONST. */
170 break;
172 case R_IHCONST:
173 insn = bfd_get_32 (abfd, hit_data);
175 /* consth, part 2
176 Now relocate the reference. */
177 if (! part1_consth_active)
179 *error_message = (char *) "Missing IHIHALF";
180 return bfd_reloc_dangerous;
183 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
184 unsigned_value = 0; /*EXTRACT_HWORD(insn) << 16;*/
185 unsigned_value += reloc_entry->addend; /* r_symndx */
186 unsigned_value += part1_consth_value;
187 unsigned_value = unsigned_value >> 16;
188 insn = INSERT_HWORD (insn, unsigned_value);
189 part1_consth_active = FALSE;
190 bfd_put_32 (abfd, insn, hit_data);
191 break;
193 case R_BYTE:
194 insn = bfd_get_8 (abfd, hit_data);
195 unsigned_value = insn + sym_value + reloc_entry->addend;
196 if (unsigned_value & 0xffffff00)
197 return bfd_reloc_overflow;
198 bfd_put_8 (abfd, unsigned_value, hit_data);
199 break;
201 case R_HWORD:
202 insn = bfd_get_16 (abfd, hit_data);
203 unsigned_value = insn + sym_value + reloc_entry->addend;
204 if (unsigned_value & 0xffff0000)
205 return bfd_reloc_overflow;
206 bfd_put_16 (abfd, insn, hit_data);
207 break;
209 case R_WORD:
210 insn = bfd_get_32 (abfd, hit_data);
211 insn += sym_value + reloc_entry->addend;
212 bfd_put_32 (abfd, insn, hit_data);
213 break;
215 default:
216 *error_message = _("Unrecognized reloc");
217 return bfd_reloc_dangerous;
220 return bfd_reloc_ok;
223 /* type rightshift
224 size
225 bitsize
226 pc-relative
227 bitpos
228 absolute
229 complain_on_overflow
230 special_function
231 relocation name
232 partial_inplace
233 src_mask
236 /* FIXME: I'm not real sure about this table. */
237 static reloc_howto_type howto_table[] =
239 { R_ABS, 0, 3, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "ABS", TRUE, 0xffffffff,0xffffffff, FALSE },
240 EMPTY_HOWTO (1),
241 EMPTY_HOWTO (2),
242 EMPTY_HOWTO (3),
243 EMPTY_HOWTO (4),
244 EMPTY_HOWTO (5),
245 EMPTY_HOWTO (6),
246 EMPTY_HOWTO (7),
247 EMPTY_HOWTO (8),
248 EMPTY_HOWTO (9),
249 EMPTY_HOWTO (10),
250 EMPTY_HOWTO (11),
251 EMPTY_HOWTO (12),
252 EMPTY_HOWTO (13),
253 EMPTY_HOWTO (14),
254 EMPTY_HOWTO (15),
255 EMPTY_HOWTO (16),
256 EMPTY_HOWTO (17),
257 EMPTY_HOWTO (18),
258 EMPTY_HOWTO (19),
259 EMPTY_HOWTO (20),
260 EMPTY_HOWTO (21),
261 EMPTY_HOWTO (22),
262 EMPTY_HOWTO (23),
263 { R_IREL, 0, 3, 32, TRUE, 0, complain_overflow_signed, or32_reloc, "IREL", TRUE, 0xffffffff,0xffffffff, FALSE },
264 { R_IABS, 0, 3, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "IABS", TRUE, 0xffffffff,0xffffffff, FALSE },
265 { R_ILOHALF, 0, 3, 16, TRUE, 0, complain_overflow_signed, or32_reloc, "ILOHALF", TRUE, 0x0000ffff,0x0000ffff, FALSE },
266 { R_IHIHALF, 0, 3, 16, TRUE, 16,complain_overflow_signed, or32_reloc, "IHIHALF", TRUE, 0xffff0000,0xffff0000, FALSE },
267 { R_IHCONST, 0, 3, 16, TRUE, 0, complain_overflow_signed, or32_reloc, "IHCONST", TRUE, 0xffff0000,0xffff0000, FALSE },
268 { R_BYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, or32_reloc, "BYTE", TRUE, 0x000000ff,0x000000ff, FALSE },
269 { R_HWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, or32_reloc, "HWORD", TRUE, 0x0000ffff,0x0000ffff, FALSE },
270 { R_WORD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, or32_reloc, "WORD", TRUE, 0xffffffff,0xffffffff, FALSE },
273 #define BADMAG(x) OR32BADMAG (x)
275 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
276 reloc_processing (relent, reloc, symbols, abfd, section)
278 static void
279 reloc_processing (arelent *relent,
280 struct internal_reloc *reloc,
281 asymbol **symbols,
282 bfd *abfd,
283 asection *section)
285 static bfd_vma ihihalf_vaddr = (bfd_vma) -1;
287 relent->address = reloc->r_vaddr;
288 relent->howto = howto_table + reloc->r_type;
290 if (reloc->r_type == R_IHCONST)
292 /* The address of an R_IHCONST should always be the address of
293 the immediately preceding R_IHIHALF. relocs generated by gas
294 are correct, but relocs generated by High C are different (I
295 can't figure out what the address means for High C). We can
296 handle both gas and High C by ignoring the address here, and
297 simply reusing the address saved for R_IHIHALF. */
298 if (ihihalf_vaddr == (bfd_vma) -1)
299 abort ();
301 relent->address = ihihalf_vaddr;
302 ihihalf_vaddr = (bfd_vma) -1;
303 relent->addend = reloc->r_symndx;
304 relent->sym_ptr_ptr= bfd_abs_section_ptr->symbol_ptr_ptr;
306 else
308 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
309 relent->addend = 0;
310 relent->address-= section->vma;
312 if (reloc->r_type == R_IHIHALF)
313 ihihalf_vaddr = relent->address;
314 else if (ihihalf_vaddr != (bfd_vma) -1)
315 abort ();
319 /* The reloc processing routine for the optimized COFF linker. */
321 static bfd_boolean
322 coff_or32_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
323 struct bfd_link_info *info,
324 bfd *input_bfd,
325 asection *input_section,
326 bfd_byte *contents,
327 struct internal_reloc *relocs,
328 struct internal_syment *syms,
329 asection **sections)
331 struct internal_reloc *rel;
332 struct internal_reloc *relend;
333 bfd_boolean hihalf;
334 bfd_vma hihalf_val;
336 /* If we are performing a relocatable link, we don't need to do a
337 thing. The caller will take care of adjusting the reloc
338 addresses and symbol indices. */
339 if (info->relocatable)
340 return TRUE;
342 hihalf = FALSE;
343 hihalf_val = 0;
345 rel = relocs;
346 relend = rel + input_section->reloc_count;
348 for (; rel < relend; rel++)
350 long symndx;
351 bfd_byte *loc;
352 struct coff_link_hash_entry *h;
353 struct internal_syment *sym;
354 asection *sec;
355 bfd_vma val;
356 bfd_boolean overflow;
357 unsigned long insn;
358 long signed_value;
359 unsigned long unsigned_value;
360 bfd_reloc_status_type rstat;
362 symndx = rel->r_symndx;
363 loc = contents + rel->r_vaddr - input_section->vma;
365 if (symndx == -1 || rel->r_type == R_IHCONST)
366 h = NULL;
367 else
368 h = obj_coff_sym_hashes (input_bfd)[symndx];
370 sym = NULL;
371 sec = NULL;
372 val = 0;
374 /* An R_IHCONST reloc does not have a symbol. Instead, the
375 symbol index is an addend. R_IHCONST is always used in
376 conjunction with R_IHHALF. */
377 if (rel->r_type != R_IHCONST)
379 if (h == NULL)
381 if (symndx == -1)
382 sec = bfd_abs_section_ptr;
383 else
385 sym = syms + symndx;
386 sec = sections[symndx];
387 val = (sec->output_section->vma
388 + sec->output_offset
389 + sym->n_value
390 - sec->vma);
393 else
395 if (h->root.type == bfd_link_hash_defined
396 || h->root.type == bfd_link_hash_defweak)
398 sec = h->root.u.def.section;
399 val = (h->root.u.def.value
400 + sec->output_section->vma
401 + sec->output_offset);
403 else
405 if (! ((*info->callbacks->undefined_symbol)
406 (info, h->root.root.string, input_bfd, input_section,
407 rel->r_vaddr - input_section->vma, TRUE)))
408 return FALSE;
412 if (hihalf)
414 if (! ((*info->callbacks->reloc_dangerous)
415 (info, "missing IHCONST reloc", input_bfd,
416 input_section, rel->r_vaddr - input_section->vma)))
417 return FALSE;
418 hihalf = FALSE;
422 overflow = FALSE;
424 switch (rel->r_type)
426 default:
427 bfd_set_error (bfd_error_bad_value);
428 return FALSE;
430 case R_IREL:
431 insn = bfd_get_32 (input_bfd, loc);
433 /* Extract the addend. */
434 signed_value = EXTRACT_JUMPTARG (insn);
435 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
436 signed_value <<= 2;
438 /* Determine the destination of the jump. */
439 signed_value += val;
441 /* Make the destination PC relative. */
442 signed_value -= (input_section->output_section->vma
443 + input_section->output_offset
444 + (rel->r_vaddr - input_section->vma));
445 if (signed_value > 0x7ffffff || signed_value < - 0x8000000)
447 overflow = TRUE;
448 signed_value = 0;
451 /* Put the adjusted value back into the instruction. */
452 signed_value >>= 2;
453 insn = INSERT_JUMPTARG(insn, signed_value);
455 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
456 break;
458 case R_ILOHALF:
459 insn = bfd_get_32 (input_bfd, loc);
460 unsigned_value = EXTRACT_HWORD (insn);
461 unsigned_value += val;
462 insn = INSERT_HWORD (insn, unsigned_value);
463 bfd_put_32 (input_bfd, insn, loc);
464 break;
466 case R_IHIHALF:
467 /* Save the value for the R_IHCONST reloc. */
468 hihalf = TRUE;
469 hihalf_val = val;
470 break;
472 case R_IHCONST:
473 if (! hihalf)
475 if (! ((*info->callbacks->reloc_dangerous)
476 (info, "missing IHIHALF reloc", input_bfd,
477 input_section, rel->r_vaddr - input_section->vma)))
478 return FALSE;
479 hihalf_val = 0;
482 insn = bfd_get_32 (input_bfd, loc);
483 unsigned_value = rel->r_symndx + hihalf_val;
484 unsigned_value >>= 16;
485 insn = INSERT_HWORD (insn, unsigned_value);
486 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
488 hihalf = FALSE;
489 break;
491 case R_BYTE:
492 case R_HWORD:
493 case R_WORD:
494 rstat = _bfd_relocate_contents (howto_table + rel->r_type,
495 input_bfd, val, loc);
496 if (rstat == bfd_reloc_overflow)
497 overflow = TRUE;
498 else if (rstat != bfd_reloc_ok)
499 abort ();
500 break;
503 if (overflow)
505 const char *name;
506 char buf[SYMNMLEN + 1];
508 if (symndx == -1)
509 name = "*ABS*";
510 else if (h != NULL)
511 name = NULL;
512 else if (sym == NULL)
513 name = "*unknown*";
514 else if (sym->_n._n_n._n_zeroes == 0
515 && sym->_n._n_n._n_offset != 0)
516 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
517 else
519 strncpy (buf, sym->_n._n_name, SYMNMLEN);
520 buf[SYMNMLEN] = '\0';
521 name = buf;
524 if (! ((*info->callbacks->reloc_overflow)
525 (info, (h ? &h->root : NULL), name,
526 howto_table[rel->r_type].name, (bfd_vma) 0, input_bfd,
527 input_section, rel->r_vaddr - input_section->vma)))
528 return FALSE;
532 return TRUE;
535 #define coff_relocate_section coff_or32_relocate_section
537 /* We don't want to change the symndx of a R_IHCONST reloc, since it
538 is actually an addend, not a symbol index at all. */
540 static bfd_boolean
541 coff_or32_adjust_symndx (bfd *obfd ATTRIBUTE_UNUSED,
542 struct bfd_link_info *info ATTRIBUTE_UNUSED,
543 bfd *ibfd ATTRIBUTE_UNUSED,
544 asection *sec ATTRIBUTE_UNUSED,
545 struct internal_reloc *irel,
546 bfd_boolean *adjustedp)
548 if (irel->r_type == R_IHCONST)
549 *adjustedp = TRUE;
550 else
551 *adjustedp = FALSE;
552 return TRUE;
555 #define coff_adjust_symndx coff_or32_adjust_symndx
557 #ifndef bfd_pe_print_pdata
558 #define bfd_pe_print_pdata NULL
559 #endif
561 #include "coffcode.h"
563 const bfd_target or32coff_big_vec =
565 "coff-or32-big", /* Name. */
566 bfd_target_coff_flavour,
567 BFD_ENDIAN_BIG, /* Data byte order is big. */
568 BFD_ENDIAN_BIG, /* Header byte order is big. */
570 (HAS_RELOC | EXEC_P | /* Object flags. */
571 HAS_LINENO | HAS_DEBUG |
572 HAS_SYMS | HAS_LOCALS | WP_TEXT),
574 (SEC_HAS_CONTENTS | SEC_ALLOC | /* Section flags. */
575 SEC_LOAD | SEC_RELOC |
576 SEC_READONLY ),
577 '_', /* Leading underscore. */
578 '/', /* ar_pad_char. */
579 15, /* ar_max_namelen. */
580 0, /* match priority. */
582 /* Data. */
583 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
584 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
585 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
587 /* Headers. */
588 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
589 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
590 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
593 _bfd_dummy_target,
594 coff_object_p,
595 bfd_generic_archive_p,
596 _bfd_dummy_target
599 bfd_false,
600 coff_mkobject,
601 _bfd_generic_mkarchive,
602 bfd_false
605 bfd_false,
606 coff_write_object_contents,
607 _bfd_write_archive_contents,
608 bfd_false
611 BFD_JUMP_TABLE_GENERIC (coff),
612 BFD_JUMP_TABLE_COPY (coff),
613 BFD_JUMP_TABLE_CORE (_bfd_nocore),
614 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
615 BFD_JUMP_TABLE_SYMBOLS (coff),
616 BFD_JUMP_TABLE_RELOCS (coff),
617 BFD_JUMP_TABLE_WRITE (coff),
618 BFD_JUMP_TABLE_LINK (coff),
619 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
621 /* Alternative_target. */
622 #ifdef TARGET_LITTLE_SYM
623 & TARGET_LITTLE_SYM,
624 #else
625 NULL,
626 #endif
628 COFF_SWAP_TABLE