Thu Aug 31 08:00:14 1995 steve chamberlain <sac@slash.cygnus.com>
[binutils-gdb.git] / bfd / coff-arm.c
1 /* BFD back-end for Intel arm COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995 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 "obstack.h"
25
26 #include "coff/arm.h"
27
28 #include "coff/internal.h"
29
30 #ifdef COFF_WITH_PE
31 #include "coff/pe.h"
32 #endif
33
34 #include "libcoff.h"
35
36 static bfd_reloc_status_type
37 aoutarm_fix_pcrel_26_done PARAMS ((bfd *, arelent *, asymbol *, PTR,
38 asection *, bfd *, char **));
39
40 static bfd_reloc_status_type
41 aoutarm_fix_pcrel_26 PARAMS ((bfd *, arelent *, asymbol *, PTR,
42 asection *, bfd *, char **));
43
44
45 static bfd_reloc_status_type coff_arm_reloc
46 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
47
48
49 /* Used by the assembler. */
50 static bfd_reloc_status_type
51 coff_arm_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
52 error_message)
53 bfd *abfd;
54 arelent *reloc_entry;
55 asymbol *symbol;
56 PTR data;
57 asection *input_section;
58 bfd *output_bfd;
59 char **error_message;
60 {
61 symvalue diff;
62 if (output_bfd == (bfd *) NULL)
63 return bfd_reloc_continue;
64
65 diff = reloc_entry->addend;
66
67 #define DOIT(x) \
68 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
69
70 if (diff != 0)
71 {
72 reloc_howto_type *howto = reloc_entry->howto;
73 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
74
75 switch (howto->size)
76 {
77 case 0:
78 {
79 char x = bfd_get_8 (abfd, addr);
80 DOIT (x);
81 bfd_put_8 (abfd, x, addr);
82 }
83 break;
84
85 case 1:
86 {
87 short x = bfd_get_16 (abfd, addr);
88 DOIT (x);
89 bfd_put_16 (abfd, x, addr);
90 }
91 break;
92
93 case 2:
94 {
95 long x = bfd_get_32 (abfd, addr);
96 DOIT (x);
97 bfd_put_32 (abfd, x, addr);
98 }
99 break;
100
101 default:
102 abort ();
103 }
104 }
105
106 /* Now let bfd_perform_relocation finish everything up. */
107 return bfd_reloc_continue;
108 }
109
110 #ifndef PCRELOFFSET
111 #define PCRELOFFSET true
112 #endif
113
114 static reloc_howto_type aoutarm_std_reloc_howto[] =
115 {
116 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
117 HOWTO(0, /* type */
118 0, /* rs */
119 0, /* size */
120 8, /* bsz */
121 false, /* pcrel */
122 0, /* bitpos */
123 complain_overflow_bitfield, /* ovf */
124 coff_arm_reloc, /* sf */
125 "8", /*name */
126 true, /* partial */
127 0x000000ff, /*read mask */
128 0x000000ff, /* setmask */
129 PCRELOFFSET /* pcdone */),
130 HOWTO(1,
131 0,
132 1,
133 16,
134 false,
135 0,
136 complain_overflow_bitfield,
137 coff_arm_reloc,
138 "16",
139 true,
140 0x0000ffff,
141 0x0000ffff,
142 PCRELOFFSET),
143 HOWTO( 2,
144 0,
145 2,
146 32,
147 false,
148 0,
149 complain_overflow_bitfield,
150 coff_arm_reloc,
151 "32",
152 true,
153 0xffffffff,
154 0xffffffff,
155 PCRELOFFSET),
156 HOWTO( 3,
157 2,
158 2,
159 26,
160 true,
161 0,
162 complain_overflow_signed,
163 aoutarm_fix_pcrel_26 ,
164 "ARM26",
165 false,
166 0x00ffffff,
167 0x00ffffff,
168 PCRELOFFSET),
169 HOWTO( 4,
170 0,
171 0,
172 8,
173 true,
174 0,
175 complain_overflow_signed,
176 coff_arm_reloc,
177 "DISP8",
178 true,
179 0x000000ff,
180 0x000000ff,
181 true),
182 HOWTO( 5,
183 0,
184 1,
185 16,
186 true,
187 0,
188 complain_overflow_signed,
189 coff_arm_reloc,
190 "DISP16",
191 true,
192 0x0000ffff,
193 0x0000ffff,
194 true),
195 HOWTO( 6,
196 0,
197 2,
198 32,
199 true,
200 0,
201 complain_overflow_signed,
202 coff_arm_reloc,
203 "DISP32",
204 true,
205 0xffffffff,
206 0xffffffff,
207 true),
208 HOWTO( 7,
209 2,
210 2,
211 26,
212 false,
213 0,
214 complain_overflow_signed,
215 aoutarm_fix_pcrel_26_done,
216 "ARM26D",
217 true,
218 0x00ffffff,
219 0x00ffffff,
220 false),
221 {-1},
222 HOWTO( 9,
223 0,
224 -1,
225 16,
226 false,
227 0,
228 complain_overflow_bitfield,
229 coff_arm_reloc,
230 "NEG16",
231 true,
232 0x0000ffff,
233 0x0000ffff,
234 false),
235 HOWTO( 10,
236 0,
237 -2,
238 32,
239 false,
240 0,
241 complain_overflow_bitfield,
242 coff_arm_reloc,
243 "NEG32",
244 true,
245 0xffffffff,
246 0xffffffff,
247 false),
248 HOWTO( 11,
249 0,
250 2,
251 32,
252 false,
253 0,
254 complain_overflow_bitfield,
255 coff_arm_reloc,
256 "rva32",
257 true,
258 0xffffffff,
259 0xffffffff,
260 PCRELOFFSET),
261 };
262
263
264 #define RTYPE2HOWTO(cache_ptr, dst) \
265 (cache_ptr)->howto = aoutarm_std_reloc_howto + (dst)->r_type;
266
267 #define coff_rtype_to_howto coff_arm_rtype_to_howto
268 static reloc_howto_type *
269 coff_arm_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
270 bfd *abfd;
271 asection *sec;
272 struct internal_reloc *rel;
273 struct coff_link_hash_entry *h;
274 struct internal_syment *sym;
275 bfd_vma *addendp;
276 {
277 reloc_howto_type *howto;
278
279 howto = aoutarm_std_reloc_howto + rel->r_type;
280
281 if (rel->r_type == 11)
282 {
283 /* Gross, where can I get this from ?? */
284 struct bfd_link_info *link_info = coff_data(sec->output_section->owner)->link_info;
285 *addendp -= link_info->pe_info->image_base.value;
286 }
287 return howto;
288
289 }
290 /* Used by the assembler. */
291
292 static bfd_reloc_status_type
293 aoutarm_fix_pcrel_26_done (abfd, reloc_entry, symbol, data, input_section,
294 output_bfd, error_message)
295 bfd *abfd;
296 arelent *reloc_entry;
297 asymbol *symbol;
298 PTR data;
299 asection *input_section;
300 bfd *output_bfd;
301 char **error_message;
302 {
303 /* This is dead simple at present. */
304 return bfd_reloc_ok;
305 }
306
307 /* Used by the assembler. */
308
309 static bfd_reloc_status_type
310 aoutarm_fix_pcrel_26 (abfd, reloc_entry, symbol, data, input_section,
311 output_bfd, error_message)
312 bfd *abfd;
313 arelent *reloc_entry;
314 asymbol *symbol;
315 PTR data;
316 asection *input_section;
317 bfd *output_bfd;
318 char **error_message;
319 {
320 bfd_vma relocation;
321 bfd_size_type addr = reloc_entry->address;
322 long target = bfd_get_32 (abfd, (bfd_byte *) data + addr);
323 bfd_reloc_status_type flag = bfd_reloc_ok;
324
325 /* If this is an undefined symbol, return error */
326 if (symbol->section == &bfd_und_section
327 && (symbol->flags & BSF_WEAK) == 0)
328 return output_bfd ? bfd_reloc_continue : bfd_reloc_undefined;
329
330 /* If the sections are different, and we are doing a partial relocation,
331 just ignore it for now. */
332 if (symbol->section->name != input_section->name
333 && output_bfd != (bfd *)NULL)
334 return bfd_reloc_continue;
335
336 relocation = (target & 0x00ffffff) << 2;
337 relocation = (relocation ^ 0x02000000) - 0x02000000; /* Sign extend */
338 relocation += symbol->value;
339 relocation += symbol->section->output_section->vma;
340 relocation += symbol->section->output_offset;
341 relocation += reloc_entry->addend;
342 relocation -= input_section->output_section->vma;
343 relocation -= input_section->output_offset;
344 relocation -= addr;
345 if (relocation & 3)
346 return bfd_reloc_overflow;
347
348 /* Check for overflow */
349 if (relocation & 0x02000000)
350 {
351 if ((relocation & ~0x03ffffff) != ~0x03ffffff)
352 flag = bfd_reloc_overflow;
353 }
354 else if (relocation & ~0x03ffffff)
355 flag = bfd_reloc_overflow;
356
357 target &= ~0x00ffffff;
358 target |= (relocation >> 2) & 0x00ffffff;
359 bfd_put_32 (abfd, target, (bfd_byte *) data + addr);
360
361 /* Now the ARM magic... Change the reloc type so that it is marked as done.
362 Strictly this is only necessary if we are doing a partial relocation. */
363 reloc_entry->howto = &aoutarm_std_reloc_howto[7];
364
365 return flag;
366 }
367
368
369 static CONST struct reloc_howto_struct *
370 arm_reloc_type_lookup(abfd,code)
371 bfd *abfd;
372 bfd_reloc_code_real_type code;
373 {
374 #define ASTD(i,j) case i: return &aoutarm_std_reloc_howto[j]
375 if (code == BFD_RELOC_CTOR)
376 switch (bfd_get_arch_info (abfd)->bits_per_address)
377 {
378 case 32:
379 code = BFD_RELOC_32;
380 break;
381 default: return (CONST struct reloc_howto_struct *) 0;
382 }
383
384 switch (code)
385 {
386 ASTD (BFD_RELOC_16, 1);
387 ASTD (BFD_RELOC_32, 2);
388 ASTD (BFD_RELOC_ARM_PCREL_BRANCH, 3);
389 ASTD (BFD_RELOC_8_PCREL, 4);
390 ASTD (BFD_RELOC_16_PCREL, 5);
391 ASTD (BFD_RELOC_32_PCREL, 6);
392 ASTD (BFD_RELOC_RVA, 11);
393 default: return (CONST struct reloc_howto_struct *) 0;
394 }
395 }
396
397
398 #define coff_bfd_reloc_type_lookup arm_reloc_type_lookup
399
400 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
401 #define COFF_PAGE_SIZE 0x1000
402 /* Turn a howto into a reloc nunmber */
403
404 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
405 #define BADMAG(x) ARMBADMAG(x)
406 #define ARM 1 /* Customize coffcode.h */
407
408
409 /* We use the special COFF backend linker. */
410 #define coff_relocate_section _bfd_coff_generic_relocate_section
411
412 #include "coffcode.h"
413
414 #ifdef TARGET_LITTLE_SYM
415 const bfd_target TARGET_LITTLE_SYM =
416 {
417 TARGET_LITTLE_NAME, /* name or coff-arm-little */
418 bfd_target_coff_flavour,
419 false, /* data byte order is little */
420 false, /* header byte order is little */
421
422 (HAS_RELOC | EXEC_P | /* object flags */
423 HAS_LINENO | HAS_DEBUG |
424 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
425
426 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
427 #ifdef TARGET_UNDERSCORE
428 TARGET_UNDERSCORE, /* leading underscore */
429 #else
430 0, /* leading underscore */
431 #endif
432 '/', /* ar_pad_char */
433 15, /* ar_max_namelen */
434
435 2, /* minimum alignment power */
436 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
437 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
438 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
439 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
440 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
441 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
442
443 /* Note that we allow an object file to be treated as a core file as well. */
444 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
445 bfd_generic_archive_p, coff_object_p},
446 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
447 bfd_false},
448 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
449 _bfd_write_archive_contents, bfd_false},
450
451 BFD_JUMP_TABLE_GENERIC (coff),
452 BFD_JUMP_TABLE_COPY (coff),
453 BFD_JUMP_TABLE_CORE (_bfd_nocore),
454 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
455 BFD_JUMP_TABLE_SYMBOLS (coff),
456 BFD_JUMP_TABLE_RELOCS (coff),
457 BFD_JUMP_TABLE_WRITE (coff),
458 BFD_JUMP_TABLE_LINK (coff),
459 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
460
461 COFF_SWAP_TABLE,
462 };
463 #endif
464
465 #ifdef TARGET_BIG_SYM
466 const bfd_target TARGET_BIG_SYM =
467 {
468 TARGET_BIG_NAME, /* name or coff-arm-big */
469 bfd_target_coff_flavour,
470 true, /* data byte order is big */
471 true, /* header byte order is big */
472
473 (HAS_RELOC | EXEC_P | /* object flags */
474 HAS_LINENO | HAS_DEBUG |
475 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
476
477 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
478 #ifdef TARGET_UNDERSCORE
479 TARGET_UNDERSCORE, /* leading underscore */
480 #else
481 0, /* leading underscore */
482 #endif
483 '/', /* ar_pad_char */
484 15, /* ar_max_namelen */
485
486 2, /* minimum alignment power */
487 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
488 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
489 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
490 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
491 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
492 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
493
494 /* Note that we allow an object file to be treated as a core file as well. */
495 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
496 bfd_generic_archive_p, coff_object_p},
497 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
498 bfd_false},
499 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
500 _bfd_write_archive_contents, bfd_false},
501
502 BFD_JUMP_TABLE_GENERIC (coff),
503 BFD_JUMP_TABLE_COPY (coff),
504 BFD_JUMP_TABLE_CORE (_bfd_nocore),
505 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
506 BFD_JUMP_TABLE_SYMBOLS (coff),
507 BFD_JUMP_TABLE_RELOCS (coff),
508 BFD_JUMP_TABLE_WRITE (coff),
509 BFD_JUMP_TABLE_LINK (coff),
510 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
511
512 COFF_SWAP_TABLE,
513 };
514 #endif