19990502 sourceware import
[deliverable/binutils-gdb.git] / bfd / coff-m68k.c
1 /* BFD back-end for Motorola 68000 COFF binaries.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Cygnus Support.
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 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "coff/m68k.h"
25 #include "coff/internal.h"
26 #include "libcoff.h"
27
28 /* This source file is compiled multiple times for various m68k COFF
29 variants. The following macros control its behaviour:
30
31 TARGET_SYM
32 The C name of the BFD target vector. The default is m68kcoff_vec.
33 TARGET_NAME
34 The user visible target name. The default is "coff-m68k".
35 NAMES_HAVE_UNDERSCORE
36 Whether symbol names have an underscore.
37 ONLY_DECLARE_RELOCS
38 Only declare the relocation howto array. Don't actually compile
39 it. The actual array will be picked up in another version of the
40 file.
41 STATIC_RELOCS
42 Make the relocation howto array, and associated functions, static.
43 COFF_COMMON_ADDEND
44 If this is defined, then, for a relocation against a common
45 symbol, the object file holds the value (the size) of the common
46 symbol. If this is not defined, then, for a relocation against a
47 common symbol, the object file holds zero. */
48
49 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
50
51 #ifndef COFF_PAGE_SIZE
52 /* The page size is a guess based on ELF. */
53 #define COFF_PAGE_SIZE 0x2000
54 #endif
55
56 #ifndef COFF_COMMON_ADDEND
57 #define RELOC_SPECIAL_FN 0
58 #else
59 static bfd_reloc_status_type m68kcoff_common_addend_special_fn
60 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
61 static reloc_howto_type *m68kcoff_common_addend_rtype_to_howto
62 PARAMS ((bfd *, asection *, struct internal_reloc *,
63 struct coff_link_hash_entry *, struct internal_syment *,
64 bfd_vma *));
65 #define RELOC_SPECIAL_FN m68kcoff_common_addend_special_fn
66 #endif
67
68 static boolean m68k_coff_is_local_label_name PARAMS ((bfd *, const char *));
69
70 /* On the delta, a symbol starting with L% is local. We won't see
71 such a symbol on other platforms, so it should be safe to always
72 consider it local here. */
73
74 static boolean
75 m68k_coff_is_local_label_name (abfd, name)
76 bfd *abfd;
77 const char *name;
78 {
79 if (name[0] == 'L' && name[1] == '%')
80 return true;
81
82 return _bfd_coff_is_local_label_name (abfd, name);
83 }
84
85 #ifndef STATIC_RELOCS
86 /* Clean up namespace. */
87 #define m68kcoff_howto_table _bfd_m68kcoff_howto_table
88 #define m68k_rtype2howto _bfd_m68kcoff_rtype2howto
89 #define m68k_howto2rtype _bfd_m68kcoff_howto2rtype
90 #define m68k_reloc_type_lookup _bfd_m68kcoff_reloc_type_lookup
91 #endif
92
93 #ifdef ONLY_DECLARE_RELOCS
94 extern reloc_howto_type m68kcoff_howto_table[];
95 #else
96 #ifdef STATIC_RELOCS
97 static
98 #endif
99 reloc_howto_type m68kcoff_howto_table[] =
100 {
101 HOWTO(R_RELBYTE, 0, 0, 8, false, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "8", true, 0x000000ff,0x000000ff, false),
102 HOWTO(R_RELWORD, 0, 1, 16, false, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "16", true, 0x0000ffff,0x0000ffff, false),
103 HOWTO(R_RELLONG, 0, 2, 32, false, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "32", true, 0xffffffff,0xffffffff, false),
104 HOWTO(R_PCRBYTE, 0, 0, 8, true, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP8", true, 0x000000ff,0x000000ff, false),
105 HOWTO(R_PCRWORD, 0, 1, 16, true, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP16", true, 0x0000ffff,0x0000ffff, false),
106 HOWTO(R_PCRLONG, 0, 2, 32, true, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP32", true, 0xffffffff,0xffffffff, false),
107 HOWTO(R_RELLONG_NEG, 0, -2, 32, false, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "-32", true, 0xffffffff,0xffffffff, false),
108 };
109 #endif /* not ONLY_DECLARE_RELOCS */
110
111 #ifndef BADMAG
112 #define BADMAG(x) M68KBADMAG(x)
113 #endif
114 #define M68 1 /* Customize coffcode.h */
115
116 /* Turn a howto into a reloc number */
117
118 #ifdef ONLY_DECLARE_RELOCS
119 extern void m68k_rtype2howto PARAMS ((arelent *internal, int relocentry));
120 extern int m68k_howto2rtype PARAMS ((reloc_howto_type *));
121 extern reloc_howto_type *m68k_reloc_type_lookup
122 PARAMS ((bfd *, bfd_reloc_code_real_type));
123 #else
124 #ifdef STATIC_RELOCS
125 static
126 #endif
127 void
128 m68k_rtype2howto(internal, relocentry)
129 arelent *internal;
130 int relocentry;
131 {
132 switch (relocentry)
133 {
134 case R_RELBYTE: internal->howto = m68kcoff_howto_table + 0; break;
135 case R_RELWORD: internal->howto = m68kcoff_howto_table + 1; break;
136 case R_RELLONG: internal->howto = m68kcoff_howto_table + 2; break;
137 case R_PCRBYTE: internal->howto = m68kcoff_howto_table + 3; break;
138 case R_PCRWORD: internal->howto = m68kcoff_howto_table + 4; break;
139 case R_PCRLONG: internal->howto = m68kcoff_howto_table + 5; break;
140 case R_RELLONG_NEG: internal->howto = m68kcoff_howto_table + 6; break;
141 }
142 }
143
144 #ifdef STATIC_RELOCS
145 static
146 #endif
147 int
148 m68k_howto2rtype (internal)
149 reloc_howto_type *internal;
150 {
151 if (internal->pc_relative)
152 {
153 switch (internal->bitsize)
154 {
155 case 32: return R_PCRLONG;
156 case 16: return R_PCRWORD;
157 case 8: return R_PCRBYTE;
158 }
159 }
160 else
161 {
162 switch (internal->bitsize)
163 {
164 case 32: return R_RELLONG;
165 case 16: return R_RELWORD;
166 case 8: return R_RELBYTE;
167 }
168 }
169 return R_RELLONG;
170 }
171
172 #ifdef STATIC_RELOCS
173 static
174 #endif
175 reloc_howto_type *
176 m68k_reloc_type_lookup (abfd, code)
177 bfd *abfd;
178 bfd_reloc_code_real_type code;
179 {
180 switch (code)
181 {
182 default: return NULL;
183 case BFD_RELOC_8: return m68kcoff_howto_table + 0;
184 case BFD_RELOC_16: return m68kcoff_howto_table + 1;
185 case BFD_RELOC_CTOR:
186 case BFD_RELOC_32: return m68kcoff_howto_table + 2;
187 case BFD_RELOC_8_PCREL: return m68kcoff_howto_table + 3;
188 case BFD_RELOC_16_PCREL: return m68kcoff_howto_table + 4;
189 case BFD_RELOC_32_PCREL: return m68kcoff_howto_table + 5;
190 /* FIXME: There doesn't seem to be a code for R_RELLONG_NEG. */
191 }
192 /*NOTREACHED*/
193 }
194
195 #endif /* not ONLY_DECLARE_RELOCS */
196
197 #define RTYPE2HOWTO(internal, relocentry) \
198 m68k_rtype2howto(internal, (relocentry)->r_type)
199
200 #define SELECT_RELOC(external, internal) \
201 external.r_type = m68k_howto2rtype (internal)
202
203 #define coff_bfd_reloc_type_lookup m68k_reloc_type_lookup
204
205 #ifndef COFF_COMMON_ADDEND
206 #ifndef coff_rtype_to_howto
207
208 #define coff_rtype_to_howto m68kcoff_rtype_to_howto
209
210 static reloc_howto_type *m68kcoff_rtype_to_howto
211 PARAMS ((bfd *, asection *, struct internal_reloc *,
212 struct coff_link_hash_entry *, struct internal_syment *,
213 bfd_vma *));
214
215 /*ARGSUSED*/
216 static reloc_howto_type *
217 m68kcoff_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
218 bfd *abfd;
219 asection *sec;
220 struct internal_reloc *rel;
221 struct coff_link_hash_entry *h;
222 struct internal_syment *sym;
223 bfd_vma *addendp;
224 {
225 arelent relent;
226 reloc_howto_type *howto;
227
228 RTYPE2HOWTO (&relent, rel);
229
230 howto = relent.howto;
231
232 if (howto->pc_relative)
233 *addendp += sec->vma;
234
235 return howto;
236 }
237
238 #endif /* ! defined (coff_rtype_to_howto) */
239 #endif /* ! defined (COFF_COMMON_ADDEND) */
240 \f
241 #ifdef COFF_COMMON_ADDEND
242
243 /* If COFF_COMMON_ADDEND is defined, then when using m68k COFF the
244 value stored in the .text section for a reference to a common
245 symbol is the value itself plus any desired offset. (taken from
246 work done by Ian Taylor, Cygnus Support, for I386 COFF). */
247
248 /* If we are producing relocateable output, we need to do some
249 adjustments to the object file that are not done by the
250 bfd_perform_relocation function. This function is called by every
251 reloc type to make any required adjustments. */
252
253 static bfd_reloc_status_type
254 m68kcoff_common_addend_special_fn (abfd, reloc_entry, symbol, data,
255 input_section, output_bfd, error_message)
256 bfd *abfd;
257 arelent *reloc_entry;
258 asymbol *symbol;
259 PTR data;
260 asection *input_section;
261 bfd *output_bfd;
262 char **error_message;
263 {
264 symvalue diff;
265
266 if (output_bfd == (bfd *) NULL)
267 return bfd_reloc_continue;
268
269 if (bfd_is_com_section (symbol->section))
270 {
271 /* We are relocating a common symbol. The current value in the
272 object file is ORIG + OFFSET, where ORIG is the value of the
273 common symbol as seen by the object file when it was compiled
274 (this may be zero if the symbol was undefined) and OFFSET is
275 the offset into the common symbol (normally zero, but may be
276 non-zero when referring to a field in a common structure).
277 ORIG is the negative of reloc_entry->addend, which is set by
278 the CALC_ADDEND macro below. We want to replace the value in
279 the object file with NEW + OFFSET, where NEW is the value of
280 the common symbol which we are going to put in the final
281 object file. NEW is symbol->value. */
282 diff = symbol->value + reloc_entry->addend;
283 }
284 else
285 {
286 /* For some reason bfd_perform_relocation always effectively
287 ignores the addend for a COFF target when producing
288 relocateable output. This seems to be always wrong for 386
289 COFF, so we handle the addend here instead. */
290 diff = reloc_entry->addend;
291 }
292
293 #define DOIT(x) \
294 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
295
296 if (diff != 0)
297 {
298 reloc_howto_type *howto = reloc_entry->howto;
299 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
300
301 switch (howto->size)
302 {
303 case 0:
304 {
305 char x = bfd_get_8 (abfd, addr);
306 DOIT (x);
307 bfd_put_8 (abfd, x, addr);
308 }
309 break;
310
311 case 1:
312 {
313 short x = bfd_get_16 (abfd, addr);
314 DOIT (x);
315 bfd_put_16 (abfd, x, addr);
316 }
317 break;
318
319 case 2:
320 {
321 long x = bfd_get_32 (abfd, addr);
322 DOIT (x);
323 bfd_put_32 (abfd, x, addr);
324 }
325 break;
326
327 default:
328 abort ();
329 }
330 }
331
332 /* Now let bfd_perform_relocation finish everything up. */
333 return bfd_reloc_continue;
334 }
335
336 /* Compute the addend of a reloc. If the reloc is to a common symbol,
337 the object file contains the value of the common symbol. By the
338 time this is called, the linker may be using a different symbol
339 from a different object file with a different value. Therefore, we
340 hack wildly to locate the original symbol from this file so that we
341 can make the correct adjustment. This macro sets coffsym to the
342 symbol from the original file, and uses it to set the addend value
343 correctly. If this is not a common symbol, the usual addend
344 calculation is done, except that an additional tweak is needed for
345 PC relative relocs.
346 FIXME: This macro refers to symbols and asect; these are from the
347 calling function, not the macro arguments. */
348
349 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
350 { \
351 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
352 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
353 coffsym = (obj_symbols (abfd) \
354 + (cache_ptr->sym_ptr_ptr - symbols)); \
355 else if (ptr) \
356 coffsym = coff_symbol_from (abfd, ptr); \
357 if (coffsym != (coff_symbol_type *) NULL \
358 && coffsym->native->u.syment.n_scnum == 0) \
359 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
360 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
361 && ptr->section != (asection *) NULL) \
362 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
363 else \
364 cache_ptr->addend = 0; \
365 if (ptr && (reloc.r_type == R_PCRBYTE \
366 || reloc.r_type == R_PCRWORD \
367 || reloc.r_type == R_PCRLONG)) \
368 cache_ptr->addend += asect->vma; \
369 }
370
371 #ifndef coff_rtype_to_howto
372
373 /* coff-m68k.c uses the special COFF backend linker. We need to
374 adjust common symbols. */
375
376 /*ARGSUSED*/
377 static reloc_howto_type *
378 m68kcoff_common_addend_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
379 bfd *abfd;
380 asection *sec;
381 struct internal_reloc *rel;
382 struct coff_link_hash_entry *h;
383 struct internal_syment *sym;
384 bfd_vma *addendp;
385 {
386 arelent relent;
387 reloc_howto_type *howto;
388
389 RTYPE2HOWTO (&relent, rel);
390
391 howto = relent.howto;
392
393 if (howto->pc_relative)
394 *addendp += sec->vma;
395
396 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
397 {
398 /* This is a common symbol. The section contents include the
399 size (sym->n_value) as an addend. The relocate_section
400 function will be adding in the final value of the symbol. We
401 need to subtract out the current size in order to get the
402 correct result. */
403 BFD_ASSERT (h != NULL);
404 *addendp -= sym->n_value;
405 }
406
407 /* If the output symbol is common (in which case this must be a
408 relocateable link), we need to add in the final size of the
409 common symbol. */
410 if (h != NULL && h->root.type == bfd_link_hash_common)
411 *addendp += h->root.u.c.size;
412
413 return howto;
414 }
415
416 #define coff_rtype_to_howto m68kcoff_common_addend_rtype_to_howto
417
418 #endif /* ! defined (coff_rtype_to_howto) */
419
420 #endif /* COFF_COMMON_ADDEND */
421 \f
422 #define coff_bfd_is_local_label_name m68k_coff_is_local_label_name
423
424 #define coff_relocate_section _bfd_coff_generic_relocate_section
425
426 #include "coffcode.h"
427
428 #ifndef TARGET_SYM
429 #define TARGET_SYM m68kcoff_vec
430 #endif
431
432 #ifndef TARGET_NAME
433 #define TARGET_NAME "coff-m68k"
434 #endif
435
436 const bfd_target TARGET_SYM =
437 {
438 TARGET_NAME,
439 bfd_target_coff_flavour,
440 BFD_ENDIAN_BIG, /* data byte order is big */
441 BFD_ENDIAN_BIG, /* header byte order is big */
442
443 (HAS_RELOC | EXEC_P | /* object flags */
444 HAS_LINENO | HAS_DEBUG |
445 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
446
447 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
448 #ifdef NAMES_HAVE_UNDERSCORE
449 '_',
450 #else
451 0, /* leading underscore */
452 #endif
453 '/', /* ar_pad_char */
454 15, /* ar_max_namelen */
455 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
456 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
457 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
458 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
459 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
460 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
461
462 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
463 bfd_generic_archive_p, _bfd_dummy_target},
464 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
465 bfd_false},
466 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
467 _bfd_write_archive_contents, bfd_false},
468
469 BFD_JUMP_TABLE_GENERIC (coff),
470 BFD_JUMP_TABLE_COPY (coff),
471 BFD_JUMP_TABLE_CORE (_bfd_nocore),
472 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
473 BFD_JUMP_TABLE_SYMBOLS (coff),
474 BFD_JUMP_TABLE_RELOCS (coff),
475 BFD_JUMP_TABLE_WRITE (coff),
476 BFD_JUMP_TABLE_LINK (coff),
477 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
478
479 COFF_SWAP_TABLE
480 };
This page took 0.039669 seconds and 4 git commands to generate.