* targets.c (bfd_target): Remove unused align_power_min field.
[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
269 static reloc_howto_type *
270 coff_arm_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
271 bfd *abfd;
272 asection *sec;
273 struct internal_reloc *rel;
274 struct coff_link_hash_entry *h;
275 struct internal_syment *sym;
276 bfd_vma *addendp;
277 {
278 reloc_howto_type *howto;
279
280 howto = aoutarm_std_reloc_howto + rel->r_type;
281
282 if (rel->r_type == 11)
283 {
284 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
285 }
286 return howto;
287
288 }
289 /* Used by the assembler. */
290
291 static bfd_reloc_status_type
292 aoutarm_fix_pcrel_26_done (abfd, reloc_entry, symbol, data, input_section,
293 output_bfd, error_message)
294 bfd *abfd;
295 arelent *reloc_entry;
296 asymbol *symbol;
297 PTR data;
298 asection *input_section;
299 bfd *output_bfd;
300 char **error_message;
301 {
302 /* This is dead simple at present. */
303 return bfd_reloc_ok;
304 }
305
306 /* Used by the assembler. */
307
308 static bfd_reloc_status_type
309 aoutarm_fix_pcrel_26 (abfd, reloc_entry, symbol, data, input_section,
310 output_bfd, error_message)
311 bfd *abfd;
312 arelent *reloc_entry;
313 asymbol *symbol;
314 PTR data;
315 asection *input_section;
316 bfd *output_bfd;
317 char **error_message;
318 {
319 bfd_vma relocation;
320 bfd_size_type addr = reloc_entry->address;
321 long target = bfd_get_32 (abfd, (bfd_byte *) data + addr);
322 bfd_reloc_status_type flag = bfd_reloc_ok;
323
324 /* If this is an undefined symbol, return error */
325 if (symbol->section == &bfd_und_section
326 && (symbol->flags & BSF_WEAK) == 0)
327 return output_bfd ? bfd_reloc_continue : bfd_reloc_undefined;
328
329 /* If the sections are different, and we are doing a partial relocation,
330 just ignore it for now. */
331 if (symbol->section->name != input_section->name
332 && output_bfd != (bfd *)NULL)
333 return bfd_reloc_continue;
334
335 relocation = (target & 0x00ffffff) << 2;
336 relocation = (relocation ^ 0x02000000) - 0x02000000; /* Sign extend */
337 relocation += symbol->value;
338 relocation += symbol->section->output_section->vma;
339 relocation += symbol->section->output_offset;
340 relocation += reloc_entry->addend;
341 relocation -= input_section->output_section->vma;
342 relocation -= input_section->output_offset;
343 relocation -= addr;
344 if (relocation & 3)
345 return bfd_reloc_overflow;
346
347 /* Check for overflow */
348 if (relocation & 0x02000000)
349 {
350 if ((relocation & ~0x03ffffff) != ~0x03ffffff)
351 flag = bfd_reloc_overflow;
352 }
353 else if (relocation & ~0x03ffffff)
354 flag = bfd_reloc_overflow;
355
356 target &= ~0x00ffffff;
357 target |= (relocation >> 2) & 0x00ffffff;
358 bfd_put_32 (abfd, target, (bfd_byte *) data + addr);
359
360 /* Now the ARM magic... Change the reloc type so that it is marked as done.
361 Strictly this is only necessary if we are doing a partial relocation. */
362 reloc_entry->howto = &aoutarm_std_reloc_howto[7];
363
364 return flag;
365 }
366
367
368 static CONST struct reloc_howto_struct *
369 arm_reloc_type_lookup(abfd,code)
370 bfd *abfd;
371 bfd_reloc_code_real_type code;
372 {
373 #define ASTD(i,j) case i: return &aoutarm_std_reloc_howto[j]
374 if (code == BFD_RELOC_CTOR)
375 switch (bfd_get_arch_info (abfd)->bits_per_address)
376 {
377 case 32:
378 code = BFD_RELOC_32;
379 break;
380 default: return (CONST struct reloc_howto_struct *) 0;
381 }
382
383 switch (code)
384 {
385 ASTD (BFD_RELOC_16, 1);
386 ASTD (BFD_RELOC_32, 2);
387 ASTD (BFD_RELOC_ARM_PCREL_BRANCH, 3);
388 ASTD (BFD_RELOC_8_PCREL, 4);
389 ASTD (BFD_RELOC_16_PCREL, 5);
390 ASTD (BFD_RELOC_32_PCREL, 6);
391 ASTD (BFD_RELOC_RVA, 11);
392 default: return (CONST struct reloc_howto_struct *) 0;
393 }
394 }
395
396
397 #define coff_bfd_reloc_type_lookup arm_reloc_type_lookup
398
399 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
400 #define COFF_PAGE_SIZE 0x1000
401 /* Turn a howto into a reloc nunmber */
402
403 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
404 #define BADMAG(x) ARMBADMAG(x)
405 #define ARM 1 /* Customize coffcode.h */
406
407
408 /* We use the special COFF backend linker. */
409 #define coff_relocate_section _bfd_coff_generic_relocate_section
410
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 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
436 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
437 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
438 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
439 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
440 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
441
442 /* Note that we allow an object file to be treated as a core file as well. */
443 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
444 bfd_generic_archive_p, coff_object_p},
445 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
446 bfd_false},
447 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
448 _bfd_write_archive_contents, bfd_false},
449
450 BFD_JUMP_TABLE_GENERIC (coff),
451 BFD_JUMP_TABLE_COPY (coff),
452 BFD_JUMP_TABLE_CORE (_bfd_nocore),
453 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
454 BFD_JUMP_TABLE_SYMBOLS (coff),
455 BFD_JUMP_TABLE_RELOCS (coff),
456 BFD_JUMP_TABLE_WRITE (coff),
457 BFD_JUMP_TABLE_LINK (coff),
458 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
459
460 COFF_SWAP_TABLE,
461 };
462 #endif
463
464 #ifdef TARGET_BIG_SYM
465 const bfd_target TARGET_BIG_SYM =
466 {
467 TARGET_BIG_NAME, /* name or coff-arm-big */
468 bfd_target_coff_flavour,
469 true, /* data byte order is big */
470 true, /* header byte order is big */
471
472 (HAS_RELOC | EXEC_P | /* object flags */
473 HAS_LINENO | HAS_DEBUG |
474 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
475
476 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
477 #ifdef TARGET_UNDERSCORE
478 TARGET_UNDERSCORE, /* leading underscore */
479 #else
480 0, /* leading underscore */
481 #endif
482 '/', /* ar_pad_char */
483 15, /* ar_max_namelen */
484
485 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
486 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
487 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
488 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
489 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
490 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
491
492 /* Note that we allow an object file to be treated as a core file as well. */
493 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
494 bfd_generic_archive_p, coff_object_p},
495 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
496 bfd_false},
497 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
498 _bfd_write_archive_contents, bfd_false},
499
500 BFD_JUMP_TABLE_GENERIC (coff),
501 BFD_JUMP_TABLE_COPY (coff),
502 BFD_JUMP_TABLE_CORE (_bfd_nocore),
503 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
504 BFD_JUMP_TABLE_SYMBOLS (coff),
505 BFD_JUMP_TABLE_RELOCS (coff),
506 BFD_JUMP_TABLE_WRITE (coff),
507 BFD_JUMP_TABLE_LINK (coff),
508 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
509
510 COFF_SWAP_TABLE,
511 };
512 #endif