1 /* BFD back-end for Hitachi Super-H COFF binaries.
2 Copyright 1993, 1994 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4 Written by Steve Chamberlain, <sac@cygnus.com>.
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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include "coff/internal.h"
31 static bfd_reloc_status_type
sh_reloc();
34 static reloc_howto_type r_imm32
=
35 {R_SH_IMM32
, 0, 2, 32, false, 0,
36 complain_overflow_bitfield
, sh_reloc
,"r_imm32", true, 0xffffffff,0xffffffff, false};
39 /*#define SELECT_RELOC(x,y) x->howto = (&r_imm32)*/
42 #define BADMAG(x) SHBADMAG(x)
43 #define SH 1 /* Customize coffcode.h */
45 #define __A_MAGIC_SET__
47 /* Code to swap in the reloc */
49 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
50 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
52 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
53 dst->r_stuff[0] = 'S'; \
54 dst->r_stuff[1] = 'C';
56 /* Code to turn a r_type into a howto ptr, uses the above howto table
59 get_symbol_value (symbol
)
64 if (bfd_is_com_section (symbol
->section
))
70 relocation
= symbol
->value
+
71 symbol
->section
->output_section
->vma
+
72 symbol
->section
->output_offset
;
79 rtype2howto (internal
, dst
)
81 struct internal_reloc
*dst
;
93 #define RTYPE2HOWTO(x,y) ((x)->howto = &r_imm32)
95 /* Perform any necessaru magic to the addend in a reloc entry */
98 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
99 cache_ptr->addend = ext_reloc.r_offset;
104 /* Compute the addend of a reloc. If the reloc is to a common symbol,
105 the object file contains the value of the common symbol. By the
106 time this is called, the linker may be using a different symbol
107 from a different object file with a different value. Therefore, we
108 hack wildly to locate the original symbol from this file so that we
109 can make the correct adjustment. This macro sets coffsym to the
110 symbol from the original file, and uses it to set the addend value
111 correctly. If this is not a common symbol, the usual addend
112 calculation is done, except that an additional tweak is needed for
114 FIXME: This macro refers to symbols and asect; these are from the
115 calling function, not the macro arguments. */
117 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
119 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
120 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
121 coffsym = (obj_symbols (abfd) \
122 + (cache_ptr->sym_ptr_ptr - symbols)); \
124 coffsym = coff_symbol_from (abfd, ptr); \
125 if (coffsym != (coff_symbol_type *) NULL \
126 && coffsym->native->u.syment.n_scnum == 0) \
127 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
128 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
129 && ptr->section != (asection *) NULL) \
130 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
132 cache_ptr->addend = 0; \
136 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
137 reloc_processing(relent, reloc, symbols, abfd, section)
140 /* this function is in charge of performing all the 29k relocations */
142 static bfd_reloc_status_type
143 sh_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
146 arelent
*reloc_entry
;
149 asection
*input_section
;
151 char **error_message
;
153 /* the consth relocation comes in two parts, we have to remember
154 the state between calls, in these variables */
156 unsigned long sym_value
;
157 unsigned short r_type
;
159 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
160 bfd_byte
*hit_data
=addr
+ (bfd_byte
*)(data
);
162 r_type
= reloc_entry
->howto
->type
;
165 /* Partial linking - do nothing */
166 reloc_entry
->address
+= input_section
->output_offset
;
170 if (symbol_in
!= NULL
171 && bfd_is_und_section (symbol_in
->section
))
173 /* Keep the state machine happy in case we're called again */
174 return (bfd_reloc_undefined
);
178 sym_value
= get_symbol_value(symbol_in
);
183 /* We ignore the previous contents ! */
184 insn
= sym_value
+ reloc_entry
->addend
;
185 bfd_put_32(abfd
, insn
, hit_data
);
188 *error_message
= "Unrecognized reloc";
189 return (bfd_reloc_dangerous
);
193 return(bfd_reloc_ok
);
196 /* The reloc processing routine for the optimized COFF linker. */
199 coff_sh_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
200 contents
, relocs
, syms
, sections
)
202 struct bfd_link_info
*info
;
204 asection
*input_section
;
206 struct internal_reloc
*relocs
;
207 struct internal_syment
*syms
;
210 struct internal_reloc
*rel
;
211 struct internal_reloc
*relend
;
213 /* If we are performing a relocateable link, we don't need to do a
214 thing. The caller will take care of adjusting the reloc
215 addresses and symbol indices. */
216 if (info
->relocateable
)
221 relend
= rel
+ input_section
->reloc_count
;
222 for (; rel
< relend
; rel
++)
226 struct coff_link_hash_entry
*h
;
227 struct internal_syment
*sym
;
230 bfd_reloc_status_type rstat
;
232 symndx
= rel
->r_symndx
;
233 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
238 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
248 sec
= bfd_abs_section_ptr
;
252 sec
= sections
[symndx
];
253 val
= (sec
->output_section
->vma
262 if (h
->root
.type
== bfd_link_hash_defined
)
264 sec
= h
->root
.u
.def
.section
;
265 val
= (h
->root
.u
.def
.value
266 + sec
->output_section
->vma
267 + sec
->output_offset
);
271 if (! ((*info
->callbacks
->undefined_symbol
)
272 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
273 rel
->r_vaddr
- input_section
->vma
)))
281 bfd_set_error (bfd_error_bad_value
);
286 long x
= bfd_get_32 (input_bfd
, loc
);
288 bfd_put_32 (input_bfd
, x
, loc
);
300 reloc_processing (relent
, reloc
, symbols
, abfd
, section
)
302 struct internal_reloc
*reloc
;
308 relent
->address
= reloc
->r_vaddr
;
309 relent
->howto
= &r_imm32
;
312 relent
->sym_ptr_ptr
= symbols
+ obj_convert(abfd
)[reloc
->r_symndx
];
314 ptr
= *(relent
->sym_ptr_ptr
);
317 && bfd_asymbol_bfd(ptr
) == abfd
319 && ((ptr
->flags
& BSF_OLD_COMMON
)== 0))
327 relent
->address
-= section
->vma
;
331 #define coff_relocate_section coff_sh_relocate_section
333 #include "coffcode.h"
335 const bfd_target shcoff_vec
=
337 "coff-sh", /* name */
338 bfd_target_coff_flavour
,
339 true, /* data byte order is big */
340 true, /* header byte order is big */
342 (HAS_RELOC
| EXEC_P
| /* object flags */
343 HAS_LINENO
| HAS_DEBUG
|
344 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| BFD_IS_RELAXABLE
),
346 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
347 '_', /* leading symbol underscore */
348 '/', /* ar_pad_char */
349 15, /* ar_max_namelen */
350 2, /* minimum section alignment */
351 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
352 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
353 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
354 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
355 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
356 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
358 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
359 bfd_generic_archive_p
, _bfd_dummy_target
},
360 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
362 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
363 _bfd_write_archive_contents
, bfd_false
},
365 BFD_JUMP_TABLE_GENERIC (coff
),
366 BFD_JUMP_TABLE_COPY (coff
),
367 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
368 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
369 BFD_JUMP_TABLE_SYMBOLS (coff
),
370 BFD_JUMP_TABLE_RELOCS (coff
),
371 BFD_JUMP_TABLE_WRITE (coff
),
372 BFD_JUMP_TABLE_LINK (coff
),
373 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),