1 /* BFD back-end for OpenRISC 1000 COFF binaries.
2 Copyright (C) 2002-2014 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 3 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,
20 MA 02110-1301, USA. */
27 #include "coff/or32.h"
28 #include "coff/internal.h"
31 static bfd_reloc_status_type or32_reloc
32 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
34 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
36 #define INSERT_HWORD(WORD,HWORD) \
37 (((WORD) & 0xffff0000) | ((HWORD)& 0x0000ffff))
38 #define EXTRACT_HWORD(WORD) \
40 #define SIGN_EXTEND_HWORD(HWORD) \
41 ((HWORD) & 0x8000 ? (HWORD)|(~0xffffL) : (HWORD))
43 #define INSERT_JUMPTARG(WORD,JT) \
44 (((WORD) & 0xfc000000) | ((JT)& 0x03ffffff))
45 #define EXTRACT_JUMPTARG(WORD) \
47 #define SIGN_EXTEND_JUMPTARG(JT) \
48 ((JT) & 0x04000000 ? (JT)|(~0x03ffffffL) : (JT))
50 /* Provided the symbol, returns the value reffed. */
53 get_symbol_value (asymbol
*symbol
)
57 if (bfd_is_com_section (symbol
->section
))
60 relocation
= symbol
->value
+
61 symbol
->section
->output_section
->vma
+
62 symbol
->section
->output_offset
;
67 /* This function is in charge of performing all the or32 relocations. */
69 static bfd_reloc_status_type
70 or32_reloc (bfd
*abfd
,
74 asection
*input_section
,
78 /* The consth relocation comes in two parts, we have to remember
79 the state between calls, in these variables. */
80 static bfd_boolean part1_consth_active
= FALSE
;
81 static unsigned long part1_consth_value
;
84 unsigned long sym_value
;
85 unsigned long unsigned_value
;
86 unsigned short r_type
;
89 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
90 bfd_byte
*hit_data
=addr
+ (bfd_byte
*)(data
);
92 r_type
= reloc_entry
->howto
->type
;
96 /* Partial linking - do nothing. */
97 reloc_entry
->address
+= input_section
->output_offset
;
101 if (symbol_in
!= NULL
102 && bfd_is_und_section (symbol_in
->section
))
104 /* Keep the state machine happy in case we're called again. */
105 if (r_type
== R_IHIHALF
)
107 part1_consth_active
= TRUE
;
108 part1_consth_value
= 0;
111 return bfd_reloc_undefined
;
114 if ((part1_consth_active
) && (r_type
!= R_IHCONST
))
116 part1_consth_active
= FALSE
;
117 *error_message
= (char *) "Missing IHCONST";
119 return bfd_reloc_dangerous
;
122 sym_value
= get_symbol_value (symbol_in
);
127 insn
= bfd_get_32(abfd
, hit_data
);
129 /* Take the value in the field and sign extend it. */
130 signed_value
= EXTRACT_JUMPTARG (insn
);
131 signed_value
= SIGN_EXTEND_JUMPTARG (signed_value
);
134 /* See the note on the R_IREL reloc in coff_or32_relocate_section. */
135 if (signed_value
== - (long) reloc_entry
->address
)
138 signed_value
+= sym_value
+ reloc_entry
->addend
;
139 /* Relative jmp/call, so subtract from the value the
140 address of the place we're coming from. */
141 signed_value
-= (reloc_entry
->address
142 + input_section
->output_section
->vma
143 + input_section
->output_offset
);
144 if (signed_value
> 0x7ffffff || signed_value
< -0x8000000)
145 return bfd_reloc_overflow
;
148 insn
= INSERT_JUMPTARG (insn
, signed_value
);
149 bfd_put_32 (abfd
, insn
, hit_data
);
153 insn
= bfd_get_32 (abfd
, hit_data
);
154 unsigned_value
= EXTRACT_HWORD (insn
);
155 unsigned_value
+= sym_value
+ reloc_entry
->addend
;
156 insn
= INSERT_HWORD (insn
, unsigned_value
);
157 bfd_put_32 (abfd
, insn
, hit_data
);
161 insn
= bfd_get_32 (abfd
, hit_data
);
164 Just get the symbol value that is referenced. */
165 part1_consth_active
= TRUE
;
166 part1_consth_value
= sym_value
+ reloc_entry
->addend
;
168 /* Don't modify insn until R_IHCONST. */
172 insn
= bfd_get_32 (abfd
, hit_data
);
175 Now relocate the reference. */
176 if (! part1_consth_active
)
178 *error_message
= (char *) "Missing IHIHALF";
179 return bfd_reloc_dangerous
;
182 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
183 unsigned_value
= 0; /*EXTRACT_HWORD(insn) << 16;*/
184 unsigned_value
+= reloc_entry
->addend
; /* r_symndx */
185 unsigned_value
+= part1_consth_value
;
186 unsigned_value
= unsigned_value
>> 16;
187 insn
= INSERT_HWORD (insn
, unsigned_value
);
188 part1_consth_active
= FALSE
;
189 bfd_put_32 (abfd
, insn
, hit_data
);
193 insn
= bfd_get_8 (abfd
, hit_data
);
194 unsigned_value
= insn
+ sym_value
+ reloc_entry
->addend
;
195 if (unsigned_value
& 0xffffff00)
196 return bfd_reloc_overflow
;
197 bfd_put_8 (abfd
, unsigned_value
, hit_data
);
201 insn
= bfd_get_16 (abfd
, hit_data
);
202 unsigned_value
= insn
+ sym_value
+ reloc_entry
->addend
;
203 if (unsigned_value
& 0xffff0000)
204 return bfd_reloc_overflow
;
205 bfd_put_16 (abfd
, insn
, hit_data
);
209 insn
= bfd_get_32 (abfd
, hit_data
);
210 insn
+= sym_value
+ reloc_entry
->addend
;
211 bfd_put_32 (abfd
, insn
, hit_data
);
215 *error_message
= _("Unrecognized reloc");
216 return bfd_reloc_dangerous
;
235 /* FIXME: I'm not real sure about this table. */
236 static reloc_howto_type howto_table
[] =
238 { R_ABS
, 0, 3, 32, FALSE
, 0, complain_overflow_bitfield
, or32_reloc
, "ABS", TRUE
, 0xffffffff,0xffffffff, FALSE
},
262 { R_IREL
, 0, 3, 32, TRUE
, 0, complain_overflow_signed
, or32_reloc
, "IREL", TRUE
, 0xffffffff,0xffffffff, FALSE
},
263 { R_IABS
, 0, 3, 32, FALSE
, 0, complain_overflow_bitfield
, or32_reloc
, "IABS", TRUE
, 0xffffffff,0xffffffff, FALSE
},
264 { R_ILOHALF
, 0, 3, 16, TRUE
, 0, complain_overflow_signed
, or32_reloc
, "ILOHALF", TRUE
, 0x0000ffff,0x0000ffff, FALSE
},
265 { R_IHIHALF
, 0, 3, 16, TRUE
, 16,complain_overflow_signed
, or32_reloc
, "IHIHALF", TRUE
, 0xffff0000,0xffff0000, FALSE
},
266 { R_IHCONST
, 0, 3, 16, TRUE
, 0, complain_overflow_signed
, or32_reloc
, "IHCONST", TRUE
, 0xffff0000,0xffff0000, FALSE
},
267 { R_BYTE
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
, or32_reloc
, "BYTE", TRUE
, 0x000000ff,0x000000ff, FALSE
},
268 { R_HWORD
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
, or32_reloc
, "HWORD", TRUE
, 0x0000ffff,0x0000ffff, FALSE
},
269 { R_WORD
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
, or32_reloc
, "WORD", TRUE
, 0xffffffff,0xffffffff, FALSE
},
272 #define BADMAG(x) OR32BADMAG (x)
274 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
275 reloc_processing (relent, reloc, symbols, abfd, section)
278 reloc_processing (arelent
*relent
,
279 struct internal_reloc
*reloc
,
284 static bfd_vma ihihalf_vaddr
= (bfd_vma
) -1;
286 relent
->address
= reloc
->r_vaddr
;
287 relent
->howto
= howto_table
+ reloc
->r_type
;
289 if (reloc
->r_type
== R_IHCONST
)
291 /* The address of an R_IHCONST should always be the address of
292 the immediately preceding R_IHIHALF. relocs generated by gas
293 are correct, but relocs generated by High C are different (I
294 can't figure out what the address means for High C). We can
295 handle both gas and High C by ignoring the address here, and
296 simply reusing the address saved for R_IHIHALF. */
297 if (ihihalf_vaddr
== (bfd_vma
) -1)
300 relent
->address
= ihihalf_vaddr
;
301 ihihalf_vaddr
= (bfd_vma
) -1;
302 relent
->addend
= reloc
->r_symndx
;
303 relent
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
307 relent
->sym_ptr_ptr
= symbols
+ obj_convert (abfd
)[reloc
->r_symndx
];
309 relent
->address
-= section
->vma
;
311 if (reloc
->r_type
== R_IHIHALF
)
312 ihihalf_vaddr
= relent
->address
;
313 else if (ihihalf_vaddr
!= (bfd_vma
) -1)
318 /* The reloc processing routine for the optimized COFF linker. */
321 coff_or32_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
322 struct bfd_link_info
*info
,
324 asection
*input_section
,
326 struct internal_reloc
*relocs
,
327 struct internal_syment
*syms
,
330 struct internal_reloc
*rel
;
331 struct internal_reloc
*relend
;
335 /* If we are performing a relocatable 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
->relocatable
)
345 relend
= rel
+ input_section
->reloc_count
;
347 for (; rel
< relend
; rel
++)
351 struct coff_link_hash_entry
*h
;
352 struct internal_syment
*sym
;
355 bfd_boolean overflow
;
358 unsigned long unsigned_value
;
359 bfd_reloc_status_type rstat
;
361 symndx
= rel
->r_symndx
;
362 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
364 if (symndx
== -1 || rel
->r_type
== R_IHCONST
)
367 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
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
)
381 sec
= bfd_abs_section_ptr
;
385 sec
= sections
[symndx
];
386 val
= (sec
->output_section
->vma
394 if (h
->root
.type
== bfd_link_hash_defined
395 || h
->root
.type
== bfd_link_hash_defweak
)
397 sec
= h
->root
.u
.def
.section
;
398 val
= (h
->root
.u
.def
.value
399 + sec
->output_section
->vma
400 + sec
->output_offset
);
404 if (! ((*info
->callbacks
->undefined_symbol
)
405 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
406 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
413 if (! ((*info
->callbacks
->reloc_dangerous
)
414 (info
, "missing IHCONST reloc", input_bfd
,
415 input_section
, rel
->r_vaddr
- input_section
->vma
)))
426 bfd_set_error (bfd_error_bad_value
);
430 insn
= bfd_get_32 (input_bfd
, loc
);
432 /* Extract the addend. */
433 signed_value
= EXTRACT_JUMPTARG (insn
);
434 signed_value
= SIGN_EXTEND_JUMPTARG (signed_value
);
437 /* Determine the destination of the jump. */
440 /* Make the destination PC relative. */
441 signed_value
-= (input_section
->output_section
->vma
442 + input_section
->output_offset
443 + (rel
->r_vaddr
- input_section
->vma
));
444 if (signed_value
> 0x7ffffff || signed_value
< - 0x8000000)
450 /* Put the adjusted value back into the instruction. */
452 insn
= INSERT_JUMPTARG(insn
, signed_value
);
454 bfd_put_32 (input_bfd
, (bfd_vma
) insn
, loc
);
458 insn
= bfd_get_32 (input_bfd
, loc
);
459 unsigned_value
= EXTRACT_HWORD (insn
);
460 unsigned_value
+= val
;
461 insn
= INSERT_HWORD (insn
, unsigned_value
);
462 bfd_put_32 (input_bfd
, insn
, loc
);
466 /* Save the value for the R_IHCONST reloc. */
474 if (! ((*info
->callbacks
->reloc_dangerous
)
475 (info
, "missing IHIHALF reloc", input_bfd
,
476 input_section
, rel
->r_vaddr
- input_section
->vma
)))
481 insn
= bfd_get_32 (input_bfd
, loc
);
482 unsigned_value
= rel
->r_symndx
+ hihalf_val
;
483 unsigned_value
>>= 16;
484 insn
= INSERT_HWORD (insn
, unsigned_value
);
485 bfd_put_32 (input_bfd
, (bfd_vma
) insn
, loc
);
493 rstat
= _bfd_relocate_contents (howto_table
+ rel
->r_type
,
494 input_bfd
, val
, loc
);
495 if (rstat
== bfd_reloc_overflow
)
497 else if (rstat
!= bfd_reloc_ok
)
505 char buf
[SYMNMLEN
+ 1];
511 else if (sym
== NULL
)
513 else if (sym
->_n
._n_n
._n_zeroes
== 0
514 && sym
->_n
._n_n
._n_offset
!= 0)
515 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
518 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
519 buf
[SYMNMLEN
] = '\0';
523 if (! ((*info
->callbacks
->reloc_overflow
)
524 (info
, (h
? &h
->root
: NULL
), name
,
525 howto_table
[rel
->r_type
].name
, (bfd_vma
) 0, input_bfd
,
526 input_section
, rel
->r_vaddr
- input_section
->vma
)))
534 #define coff_relocate_section coff_or32_relocate_section
536 /* We don't want to change the symndx of a R_IHCONST reloc, since it
537 is actually an addend, not a symbol index at all. */
540 coff_or32_adjust_symndx (bfd
*obfd ATTRIBUTE_UNUSED
,
541 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
542 bfd
*ibfd ATTRIBUTE_UNUSED
,
543 asection
*sec ATTRIBUTE_UNUSED
,
544 struct internal_reloc
*irel
,
545 bfd_boolean
*adjustedp
)
547 if (irel
->r_type
== R_IHCONST
)
554 #define coff_adjust_symndx coff_or32_adjust_symndx
556 #ifndef bfd_pe_print_pdata
557 #define bfd_pe_print_pdata NULL
560 #include "coffcode.h"
562 const bfd_target or32coff_big_vec
=
564 "coff-or32-big", /* Name. */
565 bfd_target_coff_flavour
,
566 BFD_ENDIAN_BIG
, /* Data byte order is big. */
567 BFD_ENDIAN_BIG
, /* Header byte order is big. */
569 (HAS_RELOC
| EXEC_P
| /* Object flags. */
570 HAS_LINENO
| HAS_DEBUG
|
571 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
573 (SEC_HAS_CONTENTS
| SEC_ALLOC
| /* Section flags. */
574 SEC_LOAD
| SEC_RELOC
|
576 '_', /* Leading underscore. */
577 '/', /* ar_pad_char. */
578 15, /* ar_max_namelen. */
579 0, /* match priority. */
582 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
583 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
584 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
,
587 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
588 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
589 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
,
594 bfd_generic_archive_p
,
600 _bfd_generic_mkarchive
,
605 coff_write_object_contents
,
606 _bfd_write_archive_contents
,
610 BFD_JUMP_TABLE_GENERIC (coff
),
611 BFD_JUMP_TABLE_COPY (coff
),
612 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
613 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
614 BFD_JUMP_TABLE_SYMBOLS (coff
),
615 BFD_JUMP_TABLE_RELOCS (coff
),
616 BFD_JUMP_TABLE_WRITE (coff
),
617 BFD_JUMP_TABLE_LINK (coff
),
618 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
620 /* Alternative_target. */
621 #ifdef TARGET_LITTLE_SYM