Thu Oct 13 15:51:20 1994 Jason Merrill (jason@phydeaux.cygnus.com)
[deliverable/binutils-gdb.git] / bfd / coff-sh.c
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>.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "obstack.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27 #include "coff/sh.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30
31 static bfd_reloc_status_type sh_reloc();
32
33
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};
37
38
39 /*#define SELECT_RELOC(x,y) x->howto = (&r_imm32)*/
40
41
42 #define BADMAG(x) SHBADMAG(x)
43 #define SH 1 /* Customize coffcode.h */
44
45 #define __A_MAGIC_SET__
46
47 /* Code to swap in the reloc */
48 #if 0
49 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
50 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
51 #endif
52 #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
53 dst->r_stuff[0] = 'S'; \
54 dst->r_stuff[1] = 'C';
55
56 /* Code to turn a r_type into a howto ptr, uses the above howto table
57 */
58 static long
59 get_symbol_value (symbol)
60 asymbol *symbol;
61 {
62 long relocation = 0;
63
64 if (bfd_is_com_section (symbol->section))
65 {
66 relocation = 0;
67 }
68 else
69 {
70 relocation = symbol->value +
71 symbol->section->output_section->vma +
72 symbol->section->output_offset;
73 }
74
75 return(relocation);
76 }
77
78 static void
79 rtype2howto (internal, dst)
80 arelent * internal;
81 struct internal_reloc *dst;
82 {
83 switch (dst->r_type)
84 {
85 default:
86 abort();
87 case R_SH_IMM32:
88
89 break;
90 }
91 }
92
93 #define RTYPE2HOWTO(x,y) ((x)->howto = &r_imm32)
94
95 /* Perform any necessaru magic to the addend in a reloc entry */
96
97 #if 0
98 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
99 cache_ptr->addend = ext_reloc.r_offset;
100
101 #endif
102
103
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
113 PC relative relocs.
114 FIXME: This macro refers to symbols and asect; these are from the
115 calling function, not the macro arguments. */
116
117 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
118 { \
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)); \
123 else if (ptr) \
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); \
131 else \
132 cache_ptr->addend = 0; \
133 }
134
135 #if 0
136 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
137 reloc_processing(relent, reloc, symbols, abfd, section)
138 #endif
139
140 /* this function is in charge of performing all the 29k relocations */
141
142 static bfd_reloc_status_type
143 sh_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
144 error_message)
145 bfd *abfd;
146 arelent *reloc_entry;
147 asymbol *symbol_in;
148 PTR data;
149 asection *input_section;
150 bfd *output_bfd;
151 char **error_message;
152 {
153 /* the consth relocation comes in two parts, we have to remember
154 the state between calls, in these variables */
155 unsigned long insn;
156 unsigned long sym_value;
157 unsigned short r_type;
158
159 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
160 bfd_byte *hit_data =addr + (bfd_byte *)(data);
161
162 r_type = reloc_entry->howto->type;
163
164 if (output_bfd) {
165 /* Partial linking - do nothing */
166 reloc_entry->address += input_section->output_offset;
167 return bfd_reloc_ok;
168 }
169
170 if (symbol_in != NULL
171 && bfd_is_und_section (symbol_in->section))
172 {
173 /* Keep the state machine happy in case we're called again */
174 return (bfd_reloc_undefined);
175 }
176
177
178 sym_value = get_symbol_value(symbol_in);
179
180 switch (r_type)
181 {
182 case R_SH_IMM32:
183 /* We ignore the previous contents ! */
184 insn = sym_value + reloc_entry->addend;
185 bfd_put_32(abfd, insn, hit_data);
186 break;
187 default:
188 *error_message = "Unrecognized reloc";
189 return (bfd_reloc_dangerous);
190 }
191
192
193 return(bfd_reloc_ok);
194 }
195
196 /* The reloc processing routine for the optimized COFF linker. */
197
198 static boolean
199 coff_sh_relocate_section (output_bfd, info, input_bfd, input_section,
200 contents, relocs, syms, sections)
201 bfd *output_bfd;
202 struct bfd_link_info *info;
203 bfd *input_bfd;
204 asection *input_section;
205 bfd_byte *contents;
206 struct internal_reloc *relocs;
207 struct internal_syment *syms;
208 asection **sections;
209 {
210 struct internal_reloc *rel;
211 struct internal_reloc *relend;
212
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)
217 return true;
218
219
220 rel = relocs;
221 relend = rel + input_section->reloc_count;
222 for (; rel < relend; rel++)
223 {
224 long symndx;
225 bfd_byte *loc;
226 struct coff_link_hash_entry *h;
227 struct internal_syment *sym;
228 asection *sec;
229 bfd_vma val;
230 bfd_reloc_status_type rstat;
231
232 symndx = rel->r_symndx;
233 loc = contents + rel->r_vaddr - input_section->vma;
234
235 if (symndx == -1)
236 h = NULL;
237 else
238 h = obj_coff_sym_hashes (input_bfd)[symndx];
239
240 sym = NULL;
241 sec = NULL;
242 val = 0;
243
244
245 if (h == NULL)
246 {
247 if (symndx == -1)
248 sec = bfd_abs_section_ptr;
249 else
250 {
251 sym = syms + symndx;
252 sec = sections[symndx];
253 val = (sec->output_section->vma
254 + sec->output_offset
255 + sym->n_value
256 - sec->vma
257 - sec->vma);
258 }
259 }
260 else
261 {
262 if (h->root.type == bfd_link_hash_defined)
263 {
264 sec = h->root.u.def.section;
265 val = (h->root.u.def.value
266 + sec->output_section->vma
267 + sec->output_offset);
268 }
269 else
270 {
271 if (! ((*info->callbacks->undefined_symbol)
272 (info, h->root.root.string, input_bfd, input_section,
273 rel->r_vaddr - input_section->vma)))
274 return false;
275 }
276 }
277
278 switch (rel->r_type)
279 {
280 default:
281 bfd_set_error (bfd_error_bad_value);
282 return false;
283
284 case R_SH_IMM32:
285 {
286 long x = bfd_get_32 (input_bfd, loc);
287 x += val;
288 bfd_put_32 (input_bfd, x, loc);
289
290 }
291
292 break;
293 }
294 }
295
296 return true;
297 }
298
299 static void
300 reloc_processing (relent, reloc, symbols, abfd, section)
301 arelent * relent;
302 struct internal_reloc *reloc;
303 asymbol ** symbols;
304 bfd * abfd;
305 asection * section;
306 {
307 asymbol *ptr;
308 relent->address = reloc->r_vaddr;
309 relent->howto = &r_imm32;
310
311
312 relent->sym_ptr_ptr = symbols + obj_convert(abfd)[reloc->r_symndx];
313
314 ptr = *(relent->sym_ptr_ptr);
315
316 if (ptr
317 && bfd_asymbol_bfd(ptr) == abfd
318
319 && ((ptr->flags & BSF_OLD_COMMON)== 0))
320 {
321 relent->addend = 0;
322 }
323 else
324 {
325 relent->addend = 0;
326 }
327 relent->address-= section->vma;
328
329 }
330
331 #define coff_relocate_section coff_sh_relocate_section
332
333 #include "coffcode.h"
334
335 const bfd_target shcoff_vec =
336 {
337 "coff-sh", /* name */
338 bfd_target_coff_flavour,
339 true, /* data byte order is big */
340 true, /* header byte order is big */
341
342 (HAS_RELOC | EXEC_P | /* object flags */
343 HAS_LINENO | HAS_DEBUG |
344 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
345
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 */
357
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 */
361 bfd_false},
362 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
363 _bfd_write_archive_contents, bfd_false},
364
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),
374
375 COFF_SWAP_TABLE,
376 };
377
This page took 0.036116 seconds and 4 git commands to generate.