update dependencies
[deliverable/binutils-gdb.git] / bfd / coff-a29k.c
CommitLineData
4a8db330 1/* BFD back-end for AMD 29000 COFF binaries.
dfc1c006 2 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3c8a3c56 3 Contributed by David Wood at New York University 7/8/91.
2013f9b4 4
3c8a3c56 5This file is part of BFD, the Binary File Descriptor library.
2013f9b4 6
3c8a3c56 7This program is free software; you can redistribute it and/or modify
2013f9b4 8it under the terms of the GNU General Public License as published by
3c8a3c56
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
2013f9b4 11
3c8a3c56 12This program is distributed in the hope that it will be useful,
2013f9b4
SC
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
3c8a3c56
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
2013f9b4 20
2013f9b4
SC
21#define A29K 1
22
2013f9b4 23#include "bfd.h"
bbc8d484 24#include "sysdep.h"
2013f9b4
SC
25#include "libbfd.h"
26#include "obstack.h"
c3eb25fc
SC
27#include "coff/a29k.h"
28#include "coff/internal.h"
2013f9b4
SC
29#include "libcoff.h"
30
4c3721d5
ILT
31static long get_symbol_value PARAMS ((asymbol *));
32static bfd_reloc_status_type a29k_reloc
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
69645d10
ILT
34static boolean coff_a29k_relocate_section
35 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
36 struct internal_reloc *, struct internal_syment *, asection **));
4c3721d5 37
28d19d03
ILT
38#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
39
2013f9b4 40#define INSERT_HWORD(WORD,HWORD) \
c2ce0738
SC
41 (((WORD) & 0xff00ff00) | (((HWORD) & 0xff00) << 8) | ((HWORD)& 0xff))
42#define EXTRACT_HWORD(WORD) \
43 (((WORD) & 0x00ff0000) >> 8) | ((WORD)& 0xff)
44#define SIGN_EXTEND_HWORD(HWORD) \
45 ((HWORD) & 0x8000 ? (HWORD)|0xffff0000 : (HWORD))
2013f9b4
SC
46
47/* Provided the symbol, returns the value reffed */
4c3721d5
ILT
48static long
49get_symbol_value (symbol)
50 asymbol *symbol;
2013f9b4
SC
51{
52 long relocation = 0;
53
382f2a3d 54 if (bfd_is_com_section (symbol->section))
859f11ff
SC
55 {
56 relocation = 0;
2013f9b4 57 }
859f11ff
SC
58 else
59 {
60 relocation = symbol->value +
61 symbol->section->output_section->vma +
62 symbol->section->output_offset;
63 }
64
2013f9b4
SC
65 return(relocation);
66}
67
c2ce0738
SC
68/* this function is in charge of performing all the 29k relocations */
69
9e2dad8e 70static bfd_reloc_status_type
4c3721d5
ILT
71a29k_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
72 error_message)
73 bfd *abfd;
74 arelent *reloc_entry;
75 asymbol *symbol_in;
76 PTR data;
77 asection *input_section;
78 bfd *output_bfd;
79 char **error_message;
2013f9b4 80{
859f11ff
SC
81 /* the consth relocation comes in two parts, we have to remember
82 the state between calls, in these variables */
83 static boolean part1_consth_active = false;
84 static unsigned long part1_consth_value;
c2ce0738 85
859f11ff
SC
86 unsigned long insn;
87 unsigned long sym_value;
88 unsigned long unsigned_value;
89 unsigned short r_type;
90 long signed_value;
c2ce0738 91
2cfd0562 92 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
859f11ff 93 bfd_byte *hit_data =addr + (bfd_byte *)(data);
2013f9b4 94
859f11ff 95 r_type = reloc_entry->howto->type;
f58809fd 96
2cfd0562
SC
97 if (output_bfd) {
98 /* Partial linking - do nothing */
99 reloc_entry->address += input_section->output_offset;
100 return bfd_reloc_ok;
101
102 }
103
badd23e3
ILT
104 if (symbol_in != NULL
105 && bfd_is_und_section (symbol_in->section))
859f11ff
SC
106 {
107 /* Keep the state machine happy in case we're called again */
108 if (r_type == R_IHIHALF)
c2ce0738 109 {
859f11ff
SC
110 part1_consth_active = true;
111 part1_consth_value = 0;
f58809fd 112 }
859f11ff
SC
113 return(bfd_reloc_undefined);
114 }
f58809fd 115
859f11ff
SC
116 if ((part1_consth_active) && (r_type != R_IHCONST))
117 {
859f11ff 118 part1_consth_active = false;
4c3721d5 119 *error_message = (char *) "Missing IHCONST";
859f11ff
SC
120 return(bfd_reloc_dangerous);
121 }
f58809fd 122
f58809fd 123
859f11ff
SC
124 sym_value = get_symbol_value(symbol_in);
125
126 switch (r_type)
127 {
128 case R_IREL:
129 insn = bfd_get_32(abfd, hit_data);
130 /* Take the value in the field and sign extend it */
533af031 131 signed_value = EXTRACT_HWORD(insn);
859f11ff 132 signed_value = SIGN_EXTEND_HWORD(signed_value);
533af031 133 signed_value <<= 2;
859f11ff 134 signed_value += sym_value + reloc_entry->addend;
69a010f6 135 if (((signed_value + reloc_entry->address) & ~0x3ffff) == 0)
859f11ff
SC
136 { /* Absolute jmp/call */
137 insn |= (1<<24); /* Make it absolute */
69a010f6 138 signed_value += reloc_entry->address;
859f11ff 139 /* FIXME: Should we change r_type to R_IABS */
859f11ff
SC
140 }
141 else
c2ce0738 142 {
859f11ff
SC
143 /* Relative jmp/call, so subtract from the value the
144 address of the place we're coming from */
533af031
ILT
145 signed_value -= (input_section->output_section->vma
146 + input_section->output_offset);
859f11ff 147 if (signed_value>0x1ffff || signed_value<-0x20000)
4c3721d5 148 return(bfd_reloc_overflow);
859f11ff 149 }
bbbd93b8 150 signed_value >>= 2;
859f11ff
SC
151 insn = INSERT_HWORD(insn, signed_value);
152 bfd_put_32(abfd, insn ,hit_data);
153 break;
154 case R_ILOHALF:
155 insn = bfd_get_32(abfd, hit_data);
156 unsigned_value = EXTRACT_HWORD(insn);
157 unsigned_value += sym_value + reloc_entry->addend;
158 insn = INSERT_HWORD(insn, unsigned_value);
159 bfd_put_32(abfd, insn, hit_data);
160 break;
161 case R_IHIHALF:
162 insn = bfd_get_32(abfd, hit_data);
163 /* consth, part 1
164 Just get the symbol value that is referenced */
165 part1_consth_active = true;
166 part1_consth_value = sym_value + reloc_entry->addend;
167 /* Don't modify insn until R_IHCONST */
168 break;
169 case R_IHCONST:
170 insn = bfd_get_32(abfd, hit_data);
171 /* consth, part 2
172 Now relocate the reference */
173 if (part1_consth_active == false) {
4c3721d5 174 *error_message = (char *) "Missing IHIHALF";
859f11ff
SC
175 return(bfd_reloc_dangerous);
176 }
177 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
178 unsigned_value = 0; /*EXTRACT_HWORD(insn) << 16;*/
179 unsigned_value += reloc_entry->addend; /* r_symndx */
180 unsigned_value += part1_consth_value;
181 unsigned_value = unsigned_value >> 16;
182 insn = INSERT_HWORD(insn, unsigned_value);
183 part1_consth_active = false;
184 bfd_put_32(abfd, insn, hit_data);
185 break;
186 case R_BYTE:
187 insn = bfd_get_8(abfd, hit_data);
188 unsigned_value = insn + sym_value + reloc_entry->addend;
189 if (unsigned_value & 0xffffff00) {
190 fprintf(stderr,"Relocation problem : ");
191 fprintf(stderr,"byte value too large in module %s\n",
192 abfd->filename);
193 return(bfd_reloc_overflow);
194 }
ab98fd5d 195 bfd_put_8(abfd, unsigned_value, hit_data);
859f11ff
SC
196 break;
197 case R_HWORD:
198 insn = bfd_get_16(abfd, hit_data);
199 unsigned_value = insn + sym_value + reloc_entry->addend;
200 if (unsigned_value & 0xffff0000) {
201 fprintf(stderr,"Relocation problem : ");
202 fprintf(stderr,"hword value too large in module %s\n",
203 abfd->filename);
204 return(bfd_reloc_overflow);
f58809fd
SC
205 }
206
2cfd0562 207 bfd_put_16(abfd, insn, hit_data);
859f11ff
SC
208 break;
209 case R_WORD:
210 insn = bfd_get_32(abfd, hit_data);
211 insn += sym_value + reloc_entry->addend;
212 bfd_put_32(abfd, insn, hit_data);
213 break;
214 default:
4c3721d5 215 *error_message = "Unrecognized reloc";
859f11ff
SC
216 return (bfd_reloc_dangerous);
217 }
218
219
220 return(bfd_reloc_ok);
2013f9b4
SC
221}
222
223/* type rightshift
224 size
225 bitsize
226 pc-relative
227 bitpos
228 absolute
229 complain_on_overflow
230 special_function
231 relocation name
232 partial_inplace
233 src_mask
234*/
235
236/*FIXME: I'm not real sure about this table */
2013f9b4
SC
237static reloc_howto_type howto_table[] =
238{
66a277ab 239 {R_ABS, 0, 3, 32, false, 0, complain_overflow_bitfield,a29k_reloc,"ABS", true, 0xffffffff,0xffffffff, false},
2013f9b4
SC
240 {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10},
241 {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20},
242 {21}, {22}, {23},
66a277ab
ILT
243 {R_IREL, 0, 3, 32, true, 0, complain_overflow_signed,a29k_reloc,"IREL", true, 0xffffffff,0xffffffff, false},
244 {R_IABS, 0, 3, 32, false, 0, complain_overflow_bitfield, a29k_reloc,"IABS", true, 0xffffffff,0xffffffff, false},
245 {R_ILOHALF, 0, 3, 16, true, 0, complain_overflow_signed, a29k_reloc,"ILOHALF", true, 0x0000ffff,0x0000ffff, false},
246 {R_IHIHALF, 0, 3, 16, true, 16, complain_overflow_signed, a29k_reloc,"IHIHALF", true, 0xffff0000,0xffff0000, false},
247 {R_IHCONST, 0, 3, 16, true, 0, complain_overflow_signed, a29k_reloc,"IHCONST", true, 0xffff0000,0xffff0000, false},
248 {R_BYTE, 0, 0, 8, false, 0, complain_overflow_bitfield, a29k_reloc,"BYTE", true, 0x000000ff,0x000000ff, false},
249 {R_HWORD, 0, 1, 16, false, 0, complain_overflow_bitfield, a29k_reloc,"HWORD", true, 0x0000ffff,0x0000ffff, false},
250 {R_WORD, 0, 2, 32, false, 0, complain_overflow_bitfield, a29k_reloc,"WORD", true, 0xffffffff,0xffffffff, false},
2013f9b4 251};
2013f9b4
SC
252
253#define BADMAG(x) A29KBADMAG(x)
254
6cba8f4b
SC
255#define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
256 reloc_processing(relent, reloc, symbols, abfd, section)
257
57a1867e
DM
258static void
259reloc_processing (relent,reloc, symbols, abfd, section)
260 arelent *relent;
261 struct internal_reloc *reloc;
262 asymbol **symbols;
263 bfd *abfd;
264 asection *section;
6cba8f4b 265{
09ed3666
ILT
266 static bfd_vma ihihalf_vaddr = (bfd_vma) -1;
267
6cba8f4b
SC
268 relent->address = reloc->r_vaddr;
269 relent->howto = howto_table + reloc->r_type;
270 if (reloc->r_type == R_IHCONST)
271 {
09ed3666
ILT
272 /* The address of an R_IHCONST should always be the address of
273 the immediately preceding R_IHIHALF. relocs generated by gas
274 are correct, but relocs generated by High C are different (I
275 can't figure out what the address means for High C). We can
276 handle both gas and High C by ignoring the address here, and
277 simply reusing the address saved for R_IHIHALF. */
278 if (ihihalf_vaddr == (bfd_vma) -1)
279 abort ();
280 relent->address = ihihalf_vaddr;
281 ihihalf_vaddr = (bfd_vma) -1;
6cba8f4b 282 relent->addend = reloc->r_symndx;
badd23e3 283 relent->sym_ptr_ptr= bfd_abs_section_ptr->symbol_ptr_ptr;
6cba8f4b
SC
284 }
285 else
286 {
287 asymbol *ptr;
288 relent->sym_ptr_ptr = symbols + obj_convert(abfd)[reloc->r_symndx];
289
290 ptr = *(relent->sym_ptr_ptr);
291
292 if (ptr
0e238aa7 293 && bfd_asymbol_bfd(ptr) == abfd
859f11ff 294
6cba8f4b
SC
295 && ((ptr->flags & BSF_OLD_COMMON)== 0))
296 {
859f11ff 297 relent->addend = 0;
6cba8f4b
SC
298 }
299 else
300 {
301 relent->addend = 0;
302 }
303 relent->address-= section->vma;
09ed3666
ILT
304 if (reloc->r_type == R_IHIHALF)
305 ihihalf_vaddr = relent->address;
306 else if (ihihalf_vaddr != (bfd_vma) -1)
307 abort ();
6cba8f4b
SC
308 }
309}
3b4f1a5d 310
69645d10
ILT
311/* The reloc processing routine for the optimized COFF linker. */
312
313static boolean
314coff_a29k_relocate_section (output_bfd, info, input_bfd, input_section,
315 contents, relocs, syms, sections)
316 bfd *output_bfd;
317 struct bfd_link_info *info;
318 bfd *input_bfd;
319 asection *input_section;
320 bfd_byte *contents;
321 struct internal_reloc *relocs;
322 struct internal_syment *syms;
323 asection **sections;
324{
325 struct internal_reloc *rel;
326 struct internal_reloc *relend;
327 boolean hihalf;
328 bfd_vma hihalf_val;
329
330 /* If we are performing a relocateable link, we don't need to do a
331 thing. The caller will take care of adjusting the reloc
332 addresses and symbol indices. */
333 if (info->relocateable)
334 return true;
335
336 hihalf = false;
337 hihalf_val = 0;
338
339 rel = relocs;
340 relend = rel + input_section->reloc_count;
341 for (; rel < relend; rel++)
342 {
343 long symndx;
344 bfd_byte *loc;
345 struct coff_link_hash_entry *h;
346 struct internal_syment *sym;
347 asection *sec;
348 bfd_vma val;
349 boolean overflow;
350 unsigned long insn;
351 long signed_value;
352 unsigned long unsigned_value;
353 bfd_reloc_status_type rstat;
354
355 symndx = rel->r_symndx;
356 loc = contents + rel->r_vaddr - input_section->vma;
357
2a895595
ILT
358 if (symndx == -1)
359 h = NULL;
360 else
361 h = obj_coff_sym_hashes (input_bfd)[symndx];
69645d10
ILT
362
363 sym = NULL;
364 sec = NULL;
365 val = 0;
366
367 /* An R_IHCONST reloc does not have a symbol. Instead, the
368 symbol index is an addend. R_IHCONST is always used in
369 conjunction with R_IHHALF. */
370 if (rel->r_type != R_IHCONST)
371 {
372 if (h == NULL)
373 {
2a895595
ILT
374 if (symndx == -1)
375 sec = bfd_abs_section_ptr;
376 else
377 {
378 sym = syms + symndx;
379 sec = sections[symndx];
380 val = (sec->output_section->vma
381 + sec->output_offset
382 + sym->n_value
383 - sec->vma);
384 }
69645d10
ILT
385 }
386 else
387 {
6c97aedf
ILT
388 if (h->root.type == bfd_link_hash_defined
389 || h->root.type == bfd_link_hash_defweak)
69645d10
ILT
390 {
391 sec = h->root.u.def.section;
392 val = (h->root.u.def.value
393 + sec->output_section->vma
394 + sec->output_offset);
395 }
396 else
397 {
398 if (! ((*info->callbacks->undefined_symbol)
399 (info, h->root.root.string, input_bfd, input_section,
400 rel->r_vaddr - input_section->vma)))
401 return false;
402 }
403 }
404
405 if (hihalf)
406 {
407 if (! ((*info->callbacks->reloc_dangerous)
408 (info, "missing IHCONST reloc", input_bfd,
409 input_section, rel->r_vaddr - input_section->vma)))
410 return false;
411 hihalf = false;
412 }
413 }
414
415 overflow = false;
416
417 switch (rel->r_type)
418 {
419 default:
420 bfd_set_error (bfd_error_bad_value);
421 return false;
422
423 case R_IREL:
424 insn = bfd_get_32 (input_bfd, loc);
425
426 /* Extract the addend. */
427 signed_value = EXTRACT_HWORD (insn);
428 signed_value = SIGN_EXTEND_HWORD (signed_value);
429 signed_value <<= 2;
430
431 /* Determine the destination of the jump. */
432 signed_value += val + rel->r_vaddr - input_section->vma;
433
434 if ((signed_value & ~0x3ffff) == 0)
435 {
436 /* We can use an absolute jump. */
437 insn |= (1 << 24);
438 }
439 else
440 {
441 /* Make the destination PC relative. */
442 signed_value -= (input_section->output_section->vma
443 + input_section->output_offset
444 + (rel->r_vaddr - input_section->vma));
445 if (signed_value > 0x1ffff || signed_value < - 0x20000)
446 {
447 overflow = true;
448 signed_value = 0;
449 }
450 }
451
452 /* Put the adjusted value back into the instruction. */
453 signed_value >>= 2;
454 insn = INSERT_HWORD (insn, signed_value);
455
456 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
457
458 break;
459
460 case R_ILOHALF:
461 insn = bfd_get_32 (input_bfd, loc);
462 unsigned_value = EXTRACT_HWORD (insn);
463 unsigned_value += val;
464 insn = INSERT_HWORD (insn, unsigned_value);
465 bfd_put_32 (input_bfd, insn, loc);
466 break;
467
468 case R_IHIHALF:
469 /* Save the value for the R_IHCONST reloc. */
470 hihalf = true;
471 hihalf_val = val;
472 break;
473
474 case R_IHCONST:
475 if (! hihalf)
476 {
477 if (! ((*info->callbacks->reloc_dangerous)
478 (info, "missing IHIHALF reloc", input_bfd,
479 input_section, rel->r_vaddr - input_section->vma)))
480 return false;
481 hihalf_val = 0;
482 }
483
484 insn = bfd_get_32 (input_bfd, loc);
485 unsigned_value = rel->r_symndx + hihalf_val;
486 unsigned_value >>= 16;
487 insn = INSERT_HWORD (insn, unsigned_value);
488 bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
489
490 hihalf = false;
491
492 break;
493
494 case R_BYTE:
495 case R_HWORD:
496 case R_WORD:
497 rstat = _bfd_relocate_contents (howto_table + rel->r_type,
498 input_bfd, val, loc);
499 if (rstat == bfd_reloc_overflow)
500 overflow = true;
501 else if (rstat != bfd_reloc_ok)
502 abort ();
503 break;
504 }
505
506 if (overflow)
507 {
508 const char *name;
509 char buf[SYMNMLEN + 1];
510
2a895595
ILT
511 if (symndx == -1)
512 name = "*ABS*";
513 else if (h != NULL)
69645d10
ILT
514 name = h->root.root.string;
515 else if (sym == NULL)
516 name = "*unknown*";
517 else if (sym->_n._n_n._n_zeroes == 0
518 && sym->_n._n_n._n_offset != 0)
519 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
520 else
521 {
522 strncpy (buf, sym->_n._n_name, SYMNMLEN);
523 buf[SYMNMLEN] = '\0';
524 name = buf;
525 }
526
527 if (! ((*info->callbacks->reloc_overflow)
528 (info, name, howto_table[rel->r_type].name, (bfd_vma) 0,
529 input_bfd, input_section,
530 rel->r_vaddr - input_section->vma)))
531 return false;
532 }
533 }
534
535 return true;
536}
537
538#define coff_relocate_section coff_a29k_relocate_section
539
2013f9b4
SC
540#include "coffcode.h"
541
2f3508ad 542const bfd_target a29kcoff_big_vec =
2013f9b4 543{
294eaca4
SC
544 "coff-a29k-big", /* name */
545 bfd_target_coff_flavour,
546 true, /* data byte order is big */
547 true, /* header byte order is big */
548
549 (HAS_RELOC | EXEC_P | /* object flags */
550 HAS_LINENO | HAS_DEBUG |
9f9af845 551 HAS_SYMS | HAS_LOCALS | WP_TEXT),
294eaca4
SC
552
553 (SEC_HAS_CONTENTS | SEC_ALLOC /* section flags */
554 | SEC_LOAD | SEC_RELOC
555 | SEC_READONLY ),
556 '_', /* leading underscore */
557 '/', /* ar_pad_char */
558 15, /* ar_max_namelen */
559 2, /* minimum section alignment */
560 /* data */
23f44e6f
ILT
561 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
562 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
563 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
294eaca4 564 /* hdrs */
23f44e6f
ILT
565 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
566 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
567 bfd_getb16, bfd_getb_signed_16, bfd_putb16,
294eaca4
SC
568
569 {
c2ce0738 570
294eaca4
SC
571 _bfd_dummy_target,
572 coff_object_p,
573 bfd_generic_archive_p,
574 _bfd_dummy_target
575 },
576 {
577 bfd_false,
578 coff_mkobject,
579 _bfd_generic_mkarchive,
580 bfd_false
581 },
582 {
583 bfd_false,
584 coff_write_object_contents,
585 _bfd_write_archive_contents,
586 bfd_false
587 },
588
6812b607
ILT
589 BFD_JUMP_TABLE_GENERIC (coff),
590 BFD_JUMP_TABLE_COPY (coff),
591 BFD_JUMP_TABLE_CORE (_bfd_nocore),
592 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
593 BFD_JUMP_TABLE_SYMBOLS (coff),
594 BFD_JUMP_TABLE_RELOCS (coff),
595 BFD_JUMP_TABLE_WRITE (coff),
596 BFD_JUMP_TABLE_LINK (coff),
dfc1c006 597 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
6812b607 598
294eaca4 599 COFF_SWAP_TABLE
c2ce0738 600 };
This page took 0.14498 seconds and 4 git commands to generate.