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