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