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. */
28 #include "coff/or32.h"
29 #include "coff/internal.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) \
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) \
48 #define SIGN_EXTEND_JUMPTARG(JT) \
49 ((JT) & 0x04000000 ? (JT)|(~0x03ffffffL) : (JT))
51 /* Provided the symbol, returns the value reffed. */
54 get_symbol_value (asymbol
*symbol
)
58 if (bfd_is_com_section (symbol
->section
))
61 relocation
= symbol
->value
+
62 symbol
->section
->output_section
->vma
+
63 symbol
->section
->output_offset
;
68 /* This function is in charge of performing all the or32 relocations. */
70 static bfd_reloc_status_type
71 or32_reloc (bfd
*abfd
,
75 asection
*input_section
,
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
;
85 unsigned long sym_value
;
86 unsigned long unsigned_value
;
87 unsigned short r_type
;
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
;
97 /* Partial linking - do nothing. */
98 reloc_entry
->address
+= input_section
->output_offset
;
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
);
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
);
135 /* See the note on the R_IREL reloc in coff_or32_relocate_section. */
136 if (signed_value
== - (long) reloc_entry
->address
)
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
;
149 insn
= INSERT_JUMPTARG (insn
, signed_value
);
150 bfd_put_32 (abfd
, insn
, hit_data
);
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
);
162 insn
= bfd_get_32 (abfd
, hit_data
);
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. */
173 insn
= bfd_get_32 (abfd
, hit_data
);
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
);
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
);
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
);
210 insn
= bfd_get_32 (abfd
, hit_data
);
211 insn
+= sym_value
+ reloc_entry
->addend
;
212 bfd_put_32 (abfd
, insn
, hit_data
);
216 *error_message
= _("Unrecognized reloc");
217 return bfd_reloc_dangerous
;
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
},
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)
279 reloc_processing (arelent
*relent
,
280 struct internal_reloc
*reloc
,
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)
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
;
308 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
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)
319 /* The reloc processing routine for the optimized COFF linker. */
322 coff_or32_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
323 struct bfd_link_info
*info
,
325 asection
*input_section
,
327 struct internal_reloc
*relocs
,
328 struct internal_syment
*syms
,
331 struct internal_reloc
*rel
;
332 struct internal_reloc
*relend
;
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
)
346 relend
= rel
+ input_section
->reloc_count
;
348 for (; rel
< relend
; rel
++)
352 struct coff_link_hash_entry
*h
;
353 struct internal_syment
*sym
;
356 bfd_boolean overflow
;
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
)
368 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
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
)
382 sec
= bfd_abs_section_ptr
;
386 sec
= sections
[symndx
];
387 val
= (sec
->output_section
->vma
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
);
405 if (! ((*info
->callbacks
->undefined_symbol
)
406 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
407 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
414 if (! ((*info
->callbacks
->reloc_dangerous
)
415 (info
, "missing IHCONST reloc", input_bfd
,
416 input_section
, rel
->r_vaddr
- input_section
->vma
)))
427 bfd_set_error (bfd_error_bad_value
);
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
);
438 /* Determine the destination of the jump. */
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)
451 /* Put the adjusted value back into the instruction. */
453 insn
= INSERT_JUMPTARG(insn
, signed_value
);
455 bfd_put_32 (input_bfd
, (bfd_vma
) insn
, loc
);
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
);
467 /* Save the value for the R_IHCONST reloc. */
475 if (! ((*info
->callbacks
->reloc_dangerous
)
476 (info
, "missing IHIHALF reloc", input_bfd
,
477 input_section
, rel
->r_vaddr
- input_section
->vma
)))
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
);
494 rstat
= _bfd_relocate_contents (howto_table
+ rel
->r_type
,
495 input_bfd
, val
, loc
);
496 if (rstat
== bfd_reloc_overflow
)
498 else if (rstat
!= bfd_reloc_ok
)
506 char buf
[SYMNMLEN
+ 1];
512 else if (sym
== NULL
)
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
;
519 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
520 buf
[SYMNMLEN
] = '\0';
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
)))
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. */
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
)
555 #define coff_adjust_symndx coff_or32_adjust_symndx
557 #ifndef bfd_pe_print_pdata
558 #define bfd_pe_print_pdata NULL
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
|
577 '_', /* Leading underscore. */
578 '/', /* ar_pad_char. */
579 15, /* ar_max_namelen. */
580 0, /* match priority. */
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
,
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
,
595 bfd_generic_archive_p
,
601 _bfd_generic_mkarchive
,
606 coff_write_object_contents
,
607 _bfd_write_archive_contents
,
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