Add support for OpenRISC 32-bit embedded processor
[deliverable/binutils-gdb.git] / bfd / coff-or32.c
1 /* BFD back-end for OpenRISC 1000 COFF binaries.
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Ivan Guzvinec <ivang@opencores.org>
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
20
21 #define OR32 1
22
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"
29
30 static long get_symbol_value PARAMS ((asymbol *));
31 static bfd_reloc_status_type or1_reloc PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
32 static boolean coff_or1_relocate_section PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, struct internal_reloc *, struct internal_syment *, asection **));
33 static boolean coff_or1_adjust_symndx PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, struct internal_reloc *, boolean *));
34
35 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
36
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))
43
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))
50
51 /* Provided the symbol, returns the value reffed. */
52
53 static long
54 get_symbol_value (symbol)
55 asymbol *symbol;
56 {
57 long relocation = 0;
58
59 if (bfd_is_com_section (symbol->section))
60 relocation = 0;
61 else
62 relocation = symbol->value +
63 symbol->section->output_section->vma +
64 symbol->section->output_offset;
65
66 return relocation;
67 }
68
69 /* This function is in charge of performing all the or32 relocations. */
70
71 static bfd_reloc_status_type
72 or32_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
73 error_message)
74 bfd *abfd;
75 arelent *reloc_entry;
76 asymbol *symbol_in;
77 PTR data;
78 asection *input_section;
79 bfd *output_bfd;
80 char **error_message;
81 {
82 /* The consth relocation comes in two parts, we have to remember
83 the state between calls, in these variables. */
84 static boolean part1_consth_active = false;
85 static unsigned long part1_consth_value;
86
87 unsigned long insn;
88 unsigned long sym_value;
89 unsigned long unsigned_value;
90 unsigned short r_type;
91 long signed_value;
92
93 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
94 bfd_byte *hit_data =addr + (bfd_byte *)(data);
95
96 r_type = reloc_entry->howto->type;
97
98 if (output_bfd)
99 {
100 /* Partial linking - do nothing. */
101 reloc_entry->address += input_section->output_offset;
102 return bfd_reloc_ok;
103 }
104
105 if (symbol_in != NULL
106 && bfd_is_und_section (symbol_in->section))
107 {
108 /* Keep the state machine happy in case we're called again. */
109 if (r_type == R_IHIHALF)
110 {
111 part1_consth_active = true;
112 part1_consth_value = 0;
113 }
114
115 return bfd_reloc_undefined;
116 }
117
118 if ((part1_consth_active) && (r_type != R_IHCONST))
119 {
120 part1_consth_active = false;
121 *error_message = (char *) "Missing IHCONST";
122
123 return bfd_reloc_dangerous;
124 }
125
126 sym_value = get_symbol_value (symbol_in);
127
128 switch (r_type)
129 {
130 case R_IREL:
131 insn = bfd_get_32(abfd, hit_data);
132
133 /* Take the value in the field and sign extend it. */
134 signed_value = EXTRACT_JUMPTARG (insn);
135 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
136 signed_value <<= 2;
137
138 /* See the note on the R_IREL reloc in coff_or32_relocate_section. */
139 if (signed_value == - (long) reloc_entry->address)
140 signed_value = 0;
141
142 signed_value += sym_value + reloc_entry->addend;
143 #if 0
144 if ((signed_value & ~0x3ffff) == 0)
145 { /* Absolute jmp/call. */
146 insn |= (1<<24); /* Make it absolute. */
147 /* FIXME: Should we change r_type to R_IABS. */
148 }
149 else
150 #endif
151 {
152 /* Relative jmp/call, so subtract from the value the
153 address of the place we're coming from. */
154 signed_value -= (reloc_entry->address
155 + input_section->output_section->vma
156 + input_section->output_offset);
157 if (signed_value > 0x7ffffff || signed_value < -0x8000000)
158 return bfd_reloc_overflow;
159 }
160 signed_value >>= 2;
161 insn = INSERT_JUMPTARG (insn, signed_value);
162 bfd_put_32 (abfd, insn, hit_data);
163 break;
164
165 case R_ILOHALF:
166 insn = bfd_get_32 (abfd, hit_data);
167 unsigned_value = EXTRACT_HWORD (insn);
168 unsigned_value += sym_value + reloc_entry->addend;
169 insn = INSERT_HWORD (insn, unsigned_value);
170 bfd_put_32 (abfd, insn, hit_data);
171 break;
172
173 case R_IHIHALF:
174 insn = bfd_get_32 (abfd, hit_data);
175
176 /* consth, part 1
177 Just get the symbol value that is referenced. */
178 part1_consth_active = true;
179 part1_consth_value = sym_value + reloc_entry->addend;
180
181 /* Don't modify insn until R_IHCONST. */
182 break;
183
184 case R_IHCONST:
185 insn = bfd_get_32 (abfd, hit_data);
186
187 /* consth, part 2
188 Now relocate the reference. */
189 if (part1_consth_active == false)
190 {
191 *error_message = (char *) "Missing IHIHALF";
192 return bfd_reloc_dangerous;
193 }
194
195 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
196 unsigned_value = 0; /*EXTRACT_HWORD(insn) << 16;*/
197 unsigned_value += reloc_entry->addend; /* r_symndx */
198 unsigned_value += part1_consth_value;
199 unsigned_value = unsigned_value >> 16;
200 insn = INSERT_HWORD (insn, unsigned_value);
201 part1_consth_active = false;
202 bfd_put_32 (abfd, insn, hit_data);
203 break;
204
205 case R_BYTE:
206 insn = bfd_get_8 (abfd, hit_data);
207 unsigned_value = insn + sym_value + reloc_entry->addend;
208 if (unsigned_value & 0xffffff00)
209 return bfd_reloc_overflow;
210 bfd_put_8 (abfd, unsigned_value, hit_data);
211 break;
212
213 case R_HWORD:
214 insn = bfd_get_16 (abfd, hit_data);
215 unsigned_value = insn + sym_value + reloc_entry->addend;
216 if (unsigned_value & 0xffff0000)
217 return bfd_reloc_overflow;
218 bfd_put_16 (abfd, insn, hit_data);
219 break;
220
221 case R_WORD:
222 insn = bfd_get_32 (abfd, hit_data);
223 insn += sym_value + reloc_entry->addend;
224 bfd_put_32 (abfd, insn, hit_data);
225 break;
226
227 default:
228 *error_message = _("Unrecognized reloc");
229 return bfd_reloc_dangerous;
230 }
231
232 return bfd_reloc_ok;
233 }
234
235 /* type rightshift
236 size
237 bitsize
238 pc-relative
239 bitpos
240 absolute
241 complain_on_overflow
242 special_function
243 relocation name
244 partial_inplace
245 src_mask
246 */
247
248 /* FIXME: I'm not real sure about this table. */
249 static reloc_howto_type howto_table[] =
250 {
251 { R_ABS, 0, 3, 32, false, 0, complain_overflow_bitfield, or32_reloc, "ABS", true, 0xffffffff,0xffffffff, false },
252 {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10},
253 {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20},
254 {21}, {22}, {23},
255 { R_IREL, 0, 3, 32, true, 0, complain_overflow_signed, or32_reloc, "IREL", true, 0xffffffff,0xffffffff, false },
256 { R_IABS, 0, 3, 32, false, 0, complain_overflow_bitfield, or32_reloc, "IABS", true, 0xffffffff,0xffffffff, false },
257 { R_ILOHALF, 0, 3, 16, true, 0, complain_overflow_signed, or32_reloc, "ILOHALF", true, 0x0000ffff,0x0000ffff, false },
258 { R_IHIHALF, 0, 3, 16, true, 16,complain_overflow_signed, or32_reloc, "IHIHALF", true, 0xffff0000,0xffff0000, false },
259 { R_IHCONST, 0, 3, 16, true, 0, complain_overflow_signed, or32_reloc, "IHCONST", true, 0xffff0000,0xffff0000, false },
260 { R_BYTE, 0, 0, 8, false, 0, complain_overflow_bitfield, or32_reloc, "BYTE", true, 0x000000ff,0x000000ff, false },
261 { R_HWORD, 0, 1, 16, false, 0, complain_overflow_bitfield, or32_reloc, "HWORD", true, 0x0000ffff,0x0000ffff, false },
262 { R_WORD, 0, 2, 32, false, 0, complain_overflow_bitfield, or32_reloc, "WORD", true, 0xffffffff,0xffffffff, false },
263 };
264
265 #define BADMAG(x) OR32BADMAG (x)
266
267 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
268 reloc_processing (relent, reloc, symbols, abfd, section)
269
270 static void
271 reloc_processing (relent,reloc, symbols, abfd, section)
272 arelent *relent;
273 struct internal_reloc *reloc;
274 asymbol **symbols;
275 bfd *abfd;
276 asection *section;
277 {
278 static bfd_vma ihihalf_vaddr = (bfd_vma) -1;
279
280 relent->address = reloc->r_vaddr;
281 relent->howto = howto_table + reloc->r_type;
282
283 if (reloc->r_type == R_IHCONST)
284 {
285 /* The address of an R_IHCONST should always be the address of
286 the immediately preceding R_IHIHALF. relocs generated by gas
287 are correct, but relocs generated by High C are different (I
288 can't figure out what the address means for High C). We can
289 handle both gas and High C by ignoring the address here, and
290 simply reusing the address saved for R_IHIHALF. */
291 if (ihihalf_vaddr == (bfd_vma) -1)
292 abort ();
293
294 relent->address = ihihalf_vaddr;
295 ihihalf_vaddr = (bfd_vma) -1;
296 relent->addend = reloc->r_symndx;
297 relent->sym_ptr_ptr= bfd_abs_section_ptr->symbol_ptr_ptr;
298 }
299 else
300 {
301 asymbol *ptr;
302 relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
303
304 ptr = *(relent->sym_ptr_ptr);
305
306 relent->addend = 0;
307 relent->address-= section->vma;
308
309 if (reloc->r_type == R_IHIHALF)
310 ihihalf_vaddr = relent->address;
311 else if (ihihalf_vaddr != (bfd_vma) -1)
312 abort ();
313 }
314 }
315
316 /* The reloc processing routine for the optimized COFF linker. */
317
318 static boolean
319 coff_or32_relocate_section (output_bfd, info, input_bfd, input_section,
320 contents, relocs, syms, sections)
321 bfd *output_bfd;
322 struct bfd_link_info *info;
323 bfd *input_bfd;
324 asection *input_section;
325 bfd_byte *contents;
326 struct internal_reloc *relocs;
327 struct internal_syment *syms;
328 asection **sections;
329 {
330 struct internal_reloc *rel;
331 struct internal_reloc *relend;
332 boolean hihalf;
333 bfd_vma hihalf_val;
334
335 /* If we are performing a relocateable link, we don't need to do a
336 thing. The caller will take care of adjusting the reloc
337 addresses and symbol indices. */
338 if (info->relocateable)
339 return true;
340
341 hihalf = false;
342 hihalf_val = 0;
343
344 rel = relocs;
345 relend = rel + input_section->reloc_count;
346
347 for (; rel < relend; rel++)
348 {
349 long symndx;
350 bfd_byte *loc;
351 struct coff_link_hash_entry *h;
352 struct internal_syment *sym;
353 asection *sec;
354 bfd_vma val;
355 boolean overflow;
356 unsigned long insn;
357 long signed_value;
358 unsigned long unsigned_value;
359 bfd_reloc_status_type rstat;
360
361 symndx = rel->r_symndx;
362 loc = contents + rel->r_vaddr - input_section->vma;
363
364 if (symndx == -1 || rel->r_type == R_IHCONST)
365 h = NULL;
366 else
367 h = obj_coff_sym_hashes (input_bfd)[symndx];
368
369 sym = NULL;
370 sec = NULL;
371 val = 0;
372
373 /* An R_IHCONST reloc does not have a symbol. Instead, the
374 symbol index is an addend. R_IHCONST is always used in
375 conjunction with R_IHHALF. */
376 if (rel->r_type != R_IHCONST)
377 {
378 if (h == NULL)
379 {
380 if (symndx == -1)
381 sec = bfd_abs_section_ptr;
382 else
383 {
384 sym = syms + symndx;
385 sec = sections[symndx];
386 val = (sec->output_section->vma
387 + sec->output_offset
388 + sym->n_value
389 - sec->vma);
390 }
391 }
392 else
393 {
394 if (h->root.type == bfd_link_hash_defined
395 || h->root.type == bfd_link_hash_defweak)
396 {
397 sec = h->root.u.def.section;
398 val = (h->root.u.def.value
399 + sec->output_section->vma
400 + sec->output_offset);
401 }
402 else
403 {
404 if (! ((*info->callbacks->undefined_symbol)
405 (info, h->root.root.string, input_bfd, input_section,
406 rel->r_vaddr - input_section->vma, true)))
407 return false;
408 }
409 }
410
411 if (hihalf)
412 {
413 if (! ((*info->callbacks->reloc_dangerous)
414 (info, "missing IHCONST reloc", input_bfd,
415 input_section, rel->r_vaddr - input_section->vma)))
416 return false;
417 hihalf = false;
418 }
419 }
420
421 overflow = false;
422
423 switch (rel->r_type)
424 {
425 default:
426 bfd_set_error (bfd_error_bad_value);
427 return false;
428
429 case R_IREL:
430 insn = bfd_get_32 (input_bfd, loc);
431
432 /* Extract the addend. */
433 signed_value = EXTRACT_JUMPTARG (insn);
434 signed_value = SIGN_EXTEND_JUMPTARG (signed_value);
435 signed_value <<= 2;
436
437 /* Determine the destination of the jump. */
438 signed_value += val;
439
440 #if 0
441 if ((signed_value & ~0x3ffff) == 0)
442 {
443 /* We can use an absolute jump. */
444 insn |= (1 << 24);
445 }
446 else
447 #endif
448 {
449 /* Make the destination PC relative. */
450 signed_value -= (input_section->output_section->vma
451 + input_section->output_offset
452 + (rel->r_vaddr - input_section->vma));
453 if (signed_value > 0x7ffffff || signed_value < - 0x8000000)
454 {
455 overflow = true;
456 signed_value = 0;
457 }
458 }
459
460 /* Put the adjusted value back into the instruction. */
461 signed_value >>= 2;
462 insn = INSERT_JUMPTARG(insn, signed_value);
463
464 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
465 break;
466
467 case R_ILOHALF:
468 insn = bfd_get_32 (input_bfd, loc);
469 unsigned_value = EXTRACT_HWORD (insn);
470 unsigned_value += val;
471 insn = INSERT_HWORD (insn, unsigned_value);
472 bfd_put_32 (input_bfd, insn, loc);
473 break;
474
475 case R_IHIHALF:
476 /* Save the value for the R_IHCONST reloc. */
477 hihalf = true;
478 hihalf_val = val;
479 break;
480
481 case R_IHCONST:
482 if (! hihalf)
483 {
484 if (! ((*info->callbacks->reloc_dangerous)
485 (info, "missing IHIHALF reloc", input_bfd,
486 input_section, rel->r_vaddr - input_section->vma)))
487 return false;
488 hihalf_val = 0;
489 }
490
491 insn = bfd_get_32 (input_bfd, loc);
492 unsigned_value = rel->r_symndx + hihalf_val;
493 unsigned_value >>= 16;
494 insn = INSERT_HWORD (insn, unsigned_value);
495 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
496
497 hihalf = false;
498 break;
499
500 case R_BYTE:
501 case R_HWORD:
502 case R_WORD:
503 rstat = _bfd_relocate_contents (howto_table + rel->r_type,
504 input_bfd, val, loc);
505 if (rstat == bfd_reloc_overflow)
506 overflow = true;
507 else if (rstat != bfd_reloc_ok)
508 abort ();
509 break;
510 }
511
512 if (overflow)
513 {
514 const char *name;
515 char buf[SYMNMLEN + 1];
516
517 if (symndx == -1)
518 name = "*ABS*";
519 else if (h != NULL)
520 name = h->root.root.string;
521 else if (sym == NULL)
522 name = "*unknown*";
523 else if (sym->_n._n_n._n_zeroes == 0
524 && sym->_n._n_n._n_offset != 0)
525 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
526 else
527 {
528 strncpy (buf, sym->_n._n_name, SYMNMLEN);
529 buf[SYMNMLEN] = '\0';
530 name = buf;
531 }
532
533 if (! ((*info->callbacks->reloc_overflow)
534 (info, name, howto_table[rel->r_type].name, (bfd_vma) 0,
535 input_bfd, input_section,
536 rel->r_vaddr - input_section->vma)))
537 return false;
538 }
539 }
540
541 return true;
542 }
543
544 #define coff_relocate_section coff_or32_relocate_section
545
546 /* We don't want to change the symndx of a R_IHCONST reloc, since it
547 is actually an addend, not a symbol index at all. */
548
549 static boolean
550 coff_or32_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
551 bfd *obfd ATTRIBUTE_UNUSED;
552 struct bfd_link_info *info ATTRIBUTE_UNUSED;
553 bfd *ibfd ATTRIBUTE_UNUSED;
554 asection *sec ATTRIBUTE_UNUSED;
555 struct internal_reloc *irel;
556 boolean *adjustedp;
557 {
558 if (irel->r_type == R_IHCONST)
559 *adjustedp = true;
560 else
561 *adjustedp = false;
562 return true;
563 }
564
565 #define coff_adjust_symndx coff_or32_adjust_symndx
566
567 #include "coffcode.h"
568
569 const bfd_target or32coff_big_vec =
570 {
571 "coff-or32-big", /* Name. */
572 bfd_target_coff_flavour,
573 BFD_ENDIAN_BIG, /* Data byte order is big. */
574 BFD_ENDIAN_BIG, /* Header byte order is big. */
575
576 (HAS_RELOC | EXEC_P | /* Object flags. */
577 HAS_LINENO | HAS_DEBUG |
578 HAS_SYMS | HAS_LOCALS | WP_TEXT),
579
580 (SEC_HAS_CONTENTS | SEC_ALLOC | /* Section flags. */
581 SEC_LOAD | SEC_RELOC |
582 SEC_READONLY ),
583 '_', /* Leading underscore. */
584 '/', /* ar_pad_char. */
585 15, /* ar_max_namelen. */
586
587 /* Data. */
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,
591
592 /* Headers. */
593 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
594 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
595 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
596
597 {
598 _bfd_dummy_target,
599 coff_object_p,
600 bfd_generic_archive_p,
601 _bfd_dummy_target
602 },
603 {
604 bfd_false,
605 coff_mkobject,
606 _bfd_generic_mkarchive,
607 bfd_false
608 },
609 {
610 bfd_false,
611 coff_write_object_contents,
612 _bfd_write_archive_contents,
613 bfd_false
614 },
615
616 BFD_JUMP_TABLE_GENERIC (coff),
617 BFD_JUMP_TABLE_COPY (coff),
618 BFD_JUMP_TABLE_CORE (_bfd_nocore),
619 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
620 BFD_JUMP_TABLE_SYMBOLS (coff),
621 BFD_JUMP_TABLE_RELOCS (coff),
622 BFD_JUMP_TABLE_WRITE (coff),
623 BFD_JUMP_TABLE_LINK (coff),
624 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
625
626 /* Alternative_target. */
627 #ifdef TARGET_LITTLE_SYM
628 & TARGET_LITTLE_SYM,
629 #else
630 NULL,
631 #endif
632
633 COFF_SWAP_TABLE
634 };
This page took 0.063783 seconds and 4 git commands to generate.