* bout.c (bfd_reloc_status_type, callj_callback): Cast void*
[deliverable/binutils-gdb.git] / bfd / bout.c
1 /* BFD back-end for Intel 960 b.out binaries.
2 Copyright (C) 1990-1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "seclet.h"
26 #include "bout.h"
27
28 #include "aout/stab_gnu.h"
29 #include "libaout.h" /* BFD a.out internal data structures */
30
31
32 extern bfd_error_vector_type bfd_error_vector;
33 PROTO (static boolean, b_out_squirt_out_relocs,(bfd *abfd, asection *section));
34 PROTO (static bfd_target *, b_out_callback, (bfd *));
35
36 PROTO (boolean, aout_32_slurp_symbol_table, (bfd *abfd));
37 PROTO (void , aout_32_write_syms, ());
38
39 /* Swaps the information in an executable header taken from a raw byte
40 stream memory image, into the internal exec_header structure. */
41
42 PROTO(void, bout_swap_exec_header_in,
43 (bfd *abfd,
44 struct external_exec *raw_bytes,
45 struct internal_exec *execp));
46
47 void
48 DEFUN(bout_swap_exec_header_in,(abfd, raw_bytes, execp),
49 bfd *abfd AND
50 struct external_exec *raw_bytes AND
51 struct internal_exec *execp)
52 {
53 struct external_exec *bytes = (struct external_exec *)raw_bytes;
54
55 /* Now fill in fields in the execp, from the bytes in the raw data. */
56 execp->a_info = bfd_h_get_32 (abfd, bytes->e_info);
57 execp->a_text = GET_WORD (abfd, bytes->e_text);
58 execp->a_data = GET_WORD (abfd, bytes->e_data);
59 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
60 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
61 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
62 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
63 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
64 execp->a_tload = GET_WORD (abfd, bytes->e_tload);
65 execp->a_dload = GET_WORD (abfd, bytes->e_dload);
66 execp->a_talign = bytes->e_talign[0];
67 execp->a_dalign = bytes->e_dalign[0];
68 execp->a_balign = bytes->e_balign[0];
69 execp->a_relaxable = bytes->e_relaxable[0];
70 }
71
72 /* Swaps the information in an internal exec header structure into the
73 supplied buffer ready for writing to disk. */
74
75 PROTO(void, bout_swap_exec_header_out,
76 (bfd *abfd,
77 struct internal_exec *execp,
78 struct external_exec *raw_bytes));
79 void
80 DEFUN(bout_swap_exec_header_out,(abfd, execp, raw_bytes),
81 bfd *abfd AND
82 struct internal_exec *execp AND
83 struct external_exec *raw_bytes)
84 {
85 struct external_exec *bytes = (struct external_exec *)raw_bytes;
86
87 /* Now fill in fields in the raw data, from the fields in the exec struct. */
88 bfd_h_put_32 (abfd, execp->a_info , bytes->e_info);
89 PUT_WORD (abfd, execp->a_text , bytes->e_text);
90 PUT_WORD (abfd, execp->a_data , bytes->e_data);
91 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
92 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
93 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
94 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
95 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
96 PUT_WORD (abfd, execp->a_tload , bytes->e_tload);
97 PUT_WORD (abfd, execp->a_dload , bytes->e_dload);
98 bytes->e_talign[0] = execp->a_talign;
99 bytes->e_dalign[0] = execp->a_dalign;
100 bytes->e_balign[0] = execp->a_balign;
101 bytes->e_relaxable[0] = execp->a_relaxable;
102 }
103
104
105 static bfd_target *
106 b_out_object_p (abfd)
107 bfd *abfd;
108 {
109 struct internal_exec anexec;
110 struct external_exec exec_bytes;
111
112 if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)
113 != EXEC_BYTES_SIZE) {
114 bfd_error = wrong_format;
115 return 0;
116 }
117
118 anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info);
119
120 if (N_BADMAG (anexec)) {
121 bfd_error = wrong_format;
122 return 0;
123 }
124
125 bout_swap_exec_header_in (abfd, &exec_bytes, &anexec);
126 return aout_32_some_aout_object_p (abfd, &anexec, b_out_callback);
127 }
128
129
130 /* Finish up the opening of a b.out file for reading. Fill in all the
131 fields that are not handled by common code. */
132
133 static bfd_target *
134 b_out_callback (abfd)
135 bfd *abfd;
136 {
137 struct internal_exec *execp = exec_hdr (abfd);
138 unsigned long bss_start;
139
140 /* Architecture and machine type */
141 bfd_set_arch_mach(abfd,
142 bfd_arch_i960, /* B.out only used on i960 */
143 bfd_mach_i960_core /* Default */
144 );
145
146 /* The positions of the string table and symbol table. */
147 obj_str_filepos (abfd) = N_STROFF (*execp);
148 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
149
150 /* The alignments of the sections */
151 obj_textsec (abfd)->alignment_power = execp->a_talign;
152 obj_datasec (abfd)->alignment_power = execp->a_dalign;
153 obj_bsssec (abfd)->alignment_power = execp->a_balign;
154
155 /* The starting addresses of the sections. */
156 obj_textsec (abfd)->vma = execp->a_tload;
157 obj_datasec (abfd)->vma = execp->a_dload;
158
159 /* And reload the sizes, since the aout module zaps them */
160 obj_textsec (abfd)->_raw_size = execp->a_text;
161
162 bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section */
163 obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign);
164
165 /* The file positions of the sections */
166 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
167 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
168
169 /* The file positions of the relocation info */
170 obj_textsec (abfd)->rel_filepos = N_TROFF(*execp);
171 obj_datasec (abfd)->rel_filepos = N_DROFF(*execp);
172
173 adata(abfd).page_size = 1; /* Not applicable. */
174 adata(abfd).segment_size = 1; /* Not applicable. */
175 adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
176
177 if (execp->a_relaxable)
178 abfd->flags |= BFD_IS_RELAXABLE;
179 return abfd->xvec;
180 }
181
182 struct bout_data_struct {
183 struct aoutdata a;
184 struct internal_exec e;
185 };
186
187 static boolean
188 b_out_mkobject (abfd)
189 bfd *abfd;
190 {
191 struct bout_data_struct *rawptr;
192
193 rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct));
194 if (rawptr == NULL) {
195 bfd_error = no_memory;
196 return false;
197 }
198
199 abfd->tdata.bout_data = rawptr;
200 exec_hdr (abfd) = &rawptr->e;
201
202 /* For simplicity's sake we just make all the sections right here. */
203 obj_textsec (abfd) = (asection *)NULL;
204 obj_datasec (abfd) = (asection *)NULL;
205 obj_bsssec (abfd) = (asection *)NULL;
206
207 bfd_make_section (abfd, ".text");
208 bfd_make_section (abfd, ".data");
209 bfd_make_section (abfd, ".bss");
210
211 return true;
212 }
213
214 static boolean
215 b_out_write_object_contents (abfd)
216 bfd *abfd;
217 {
218 struct external_exec swapped_hdr;
219
220 exec_hdr (abfd)->a_info = BMAGIC;
221
222 exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size;
223 exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size;
224 exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size;
225 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
226 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
227 exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
228 sizeof (struct relocation_info));
229 exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
230 sizeof (struct relocation_info));
231
232 exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
233 exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
234 exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
235
236 exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
237 exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
238
239 bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
240
241 bfd_seek (abfd, 0L, SEEK_SET);
242 bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd);
243
244 /* Now write out reloc info, followed by syms and strings */
245 if (bfd_get_symcount (abfd) != 0)
246 {
247 bfd_seek (abfd,
248 (long)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET);
249
250 aout_32_write_syms (abfd);
251
252 bfd_seek (abfd, (long)(N_TROFF(*exec_hdr(abfd))), SEEK_SET);
253
254 if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
255 bfd_seek (abfd, (long)(N_DROFF(*exec_hdr(abfd))), SEEK_SET);
256
257 if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
258 }
259 return true;
260 }
261 \f
262 /** Some reloc hackery */
263
264 #define CALLS 0x66003800 /* Template for 'calls' instruction */
265 #define BAL 0x0b000000 /* Template for 'bal' instruction */
266 #define BALX 0x85000000 /* Template for 'balx' instruction */
267 #define BAL_MASK 0x00ffffff
268 #define CALL 0x09000000
269 #define PCREL13_MASK 0x1fff
270 /* Magic to turn callx into calljx */
271 static bfd_reloc_status_type
272 DEFUN (calljx_callback, (abfd, reloc_entry, src, dst, input_section),
273 bfd *abfd AND
274 arelent *reloc_entry AND
275 PTR src AND
276 PTR dst AND
277
278 asection *input_section)
279 {
280 int word = bfd_get_32(abfd, src);
281 asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr);
282 aout_symbol_type *symbol = aout_symbol(symbol_in);
283
284 if (IS_CALLNAME(symbol->other))
285 {
286
287 aout_symbol_type *balsym = symbol+1;
288 int inst = bfd_get_32(abfd, (bfd_byte *) src-4);
289 /* The next symbol should be an N_BALNAME */
290 BFD_ASSERT(IS_BALNAME(balsym->other));
291 inst &= BAL_MASK;
292 inst |= BALX;
293 bfd_put_32(abfd, inst, (bfd_byte *) dst-4);
294 symbol = balsym;
295 }
296
297 word += symbol->symbol.section->output_offset +
298 symbol->symbol.section->output_section->vma +
299 symbol->symbol.value + reloc_entry->addend;
300
301 bfd_put_32(abfd, word, dst);
302 return bfd_reloc_ok;
303 }
304
305
306 /* Magic to turn call into callj */
307 static bfd_reloc_status_type
308 DEFUN (callj_callback, (abfd, reloc_entry, data, srcidx,dstidx, input_section),
309 bfd *abfd AND
310 arelent *reloc_entry AND
311 PTR data AND
312 unsigned int srcidx AND
313 unsigned int dstidx AND
314 asection *input_section )
315 {
316 int word = bfd_get_32(abfd, (bfd_byte *) data + srcidx);
317 asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr);
318
319 aout_symbol_type *symbol = aout_symbol(symbol_in);
320
321 if (IS_OTHER(symbol->other))
322 {
323 /* Call to a system procedure - replace code with system
324 procedure number */
325 word = CALLS | (symbol->other - 1);
326
327 }
328
329 else if (IS_CALLNAME(symbol->other))
330 {
331 aout_symbol_type *balsym = symbol+1;
332 /* The next symbol should be an N_BALNAME */
333 BFD_ASSERT(IS_BALNAME(balsym->other));
334
335 /* We are calling a leaf - so replace the call instruction
336 with a bal */
337
338 word = BAL |
339 (((word & BAL_MASK) +
340 balsym->symbol.section->output_offset +
341 balsym->symbol.section->output_section->vma+
342 balsym->symbol.value + reloc_entry->addend - dstidx -
343 ( input_section->output_section->vma + input_section->output_offset))
344 & BAL_MASK);
345
346
347 }
348 else
349 {
350
351 word = CALL |
352 (((word & BAL_MASK) +
353 symbol->symbol.section->output_offset +
354 symbol->symbol.section->output_section->vma+
355 symbol->symbol.value + reloc_entry->addend - dstidx -
356 ( input_section->output_section->vma + input_section->output_offset))
357 & BAL_MASK);
358 }
359 bfd_put_32(abfd, word, (bfd_byte *) data + dstidx);
360 return bfd_reloc_ok;
361 }
362
363 /* type rshift size bitsize pcrel bitpos absolute overflow check*/
364
365 #define ABS32CODE 0
366 #define ABS32CODE_SHRUNK 1
367 #define PCREL24 2
368 #define CALLJ 3
369 #define ABS32 4
370 #define PCREL13 5
371 #define ABS32_MAYBE_RELAXABLE 1
372 #define ABS32_WAS_RELAXABLE 2
373
374 static reloc_howto_type howto_reloc_callj =
375 HOWTO(CALLJ, 0, 2, 24, true, 0, true, true, 0,"callj", true, 0x00ffffff, 0x00ffffff,false);
376 static reloc_howto_type howto_reloc_abs32 =
377 HOWTO(ABS32, 0, 2, 32, false, 0, true, true,0,"abs32", true, 0xffffffff,0xffffffff,false);
378 static reloc_howto_type howto_reloc_pcrel24 =
379 HOWTO(PCREL24, 0, 2, 24, true, 0, true, true,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);
380
381 static reloc_howto_type howto_reloc_pcrel13 =
382 HOWTO(PCREL13, 0, 2, 13, true, 0, true, true,0,"pcrel13", true, 0x00001fff,0x00001fff,false);
383
384
385 static reloc_howto_type howto_reloc_abs32codeshrunk =
386 HOWTO(ABS32CODE_SHRUNK, 0, 2, 24, true, 0, true, true, 0,"callx->callj", true, 0x00ffffff, 0x00ffffff,false);
387
388 static reloc_howto_type howto_reloc_abs32code =
389 HOWTO(ABS32CODE, 0, 2, 32, false, 0, true, true,0,"callx", true, 0xffffffff,0xffffffff,false);
390
391 static reloc_howto_type *
392 b_out_reloc_type_lookup (abfd, code)
393 bfd *abfd;
394 bfd_reloc_code_real_type code;
395 {
396 switch (code)
397 {
398 default:
399 return 0;
400 case BFD_RELOC_I960_CALLJ:
401 return &howto_reloc_callj;
402 case BFD_RELOC_32:
403 return &howto_reloc_abs32;
404 case BFD_RELOC_24_PCREL:
405 return &howto_reloc_pcrel24;
406 }
407 }
408
409 /* Allocate enough room for all the reloc entries, plus pointers to them all */
410
411 static boolean
412 b_out_slurp_reloc_table (abfd, asect, symbols)
413 bfd *abfd;
414 sec_ptr asect;
415 asymbol **symbols;
416 {
417 register struct relocation_info *rptr;
418 unsigned int counter ;
419 arelent *cache_ptr ;
420 int extern_mask, pcrel_mask, callj_mask;
421 int incode_mask;
422 int size_mask;
423 bfd_vma prev_addr = 0;
424 unsigned int count;
425 size_t reloc_size;
426 struct relocation_info *relocs;
427 arelent *reloc_cache;
428
429 if (asect->relocation) return true;
430 if (!aout_32_slurp_symbol_table (abfd)) return false;
431
432 if (asect == obj_datasec (abfd)) {
433 reloc_size = exec_hdr(abfd)->a_drsize;
434 goto doit;
435 }
436
437 if (asect == obj_textsec (abfd)) {
438 reloc_size = exec_hdr(abfd)->a_trsize;
439 goto doit;
440 }
441
442 bfd_error = invalid_operation;
443 return false;
444
445 doit:
446 bfd_seek (abfd, (long)(asect->rel_filepos), SEEK_SET);
447 count = reloc_size / sizeof (struct relocation_info);
448
449 relocs = (struct relocation_info *) bfd_xmalloc (reloc_size);
450 if (!relocs) {
451 bfd_error = no_memory;
452 return false;
453 }
454 reloc_cache = (arelent *) bfd_xmalloc ((count+1) * sizeof (arelent));
455 if (!reloc_cache) {
456 free ((char*)relocs);
457 bfd_error = no_memory;
458 return false;
459 }
460
461 if (bfd_read ((PTR) relocs, 1, reloc_size, abfd) != reloc_size) {
462 bfd_error = system_call_error;
463 free (reloc_cache);
464 free (relocs);
465 return false;
466 }
467
468
469
470 if (abfd->xvec->header_byteorder_big_p) {
471 /* big-endian bit field allocation order */
472 pcrel_mask = 0x80;
473 extern_mask = 0x10;
474 incode_mask = 0x08;
475 callj_mask = 0x02;
476 size_mask = 0x20;
477 } else {
478 /* little-endian bit field allocation order */
479 pcrel_mask = 0x01;
480 extern_mask = 0x08;
481 incode_mask = 0x10;
482 callj_mask = 0x40;
483 size_mask = 0x02;
484 }
485
486 for (rptr = relocs, cache_ptr = reloc_cache, counter = 0;
487 counter < count;
488 counter++, rptr++, cache_ptr++)
489 {
490 unsigned char *raw = (unsigned char *)rptr;
491 unsigned int symnum;
492 cache_ptr->address = bfd_h_get_32 (abfd, raw + 0);
493 if (abfd->xvec->header_byteorder_big_p)
494 {
495 symnum = (raw[4] << 16) | (raw[5] << 8) | raw[6];
496 }
497 else
498 {
499 symnum = (raw[6] << 16) | (raw[5] << 8) | raw[4];
500 }
501
502 if (raw[7] & extern_mask)
503 {
504 /* if this is set then the r_index is a index into the symbol table;
505 * if the bit is not set then r_index contains a section map.
506 * we either fill in the sym entry with a pointer to the symbol,
507 * or point to the correct section
508 */
509 cache_ptr->sym_ptr_ptr = symbols + symnum;
510 cache_ptr->addend = 0;
511 } else
512 {
513 /* in a.out symbols are relative to the beginning of the
514 * file rather than sections ?
515 * (look in translate_from_native_sym_flags)
516 * the reloc entry addend has added to it the offset into the
517 * file of the data, so subtract the base to make the reloc
518 * section relative */
519 cache_ptr->sym_ptr_ptr = (asymbol **)NULL;
520 switch (symnum)
521 {
522 case N_TEXT:
523 case N_TEXT | N_EXT:
524 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr;
525 cache_ptr->addend = - obj_textsec(abfd)->vma;
526 break;
527 case N_DATA:
528 case N_DATA | N_EXT:
529 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr;
530 cache_ptr->addend = - obj_datasec(abfd)->vma;
531 break;
532 case N_BSS:
533 case N_BSS | N_EXT:
534 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr;
535 cache_ptr->addend = - obj_bsssec(abfd)->vma;
536 break;
537 case N_ABS:
538 case N_ABS | N_EXT:
539 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr;
540 cache_ptr->addend = 0;
541 break;
542 default:
543 BFD_ASSERT(0);
544 break;
545 }
546
547 }
548
549 /* the i960 only has a few relocation types:
550 abs 32-bit and pcrel 24bit. except for callj's! */
551 if (raw[7] & callj_mask)
552 {
553 cache_ptr->howto = &howto_reloc_callj;
554 }
555 else if ( raw[7] & pcrel_mask)
556 {
557 if (raw[7] & size_mask)
558 cache_ptr->howto = &howto_reloc_pcrel13;
559 else
560 cache_ptr->howto = &howto_reloc_pcrel24;
561 }
562 else
563 {
564 if (raw[7] & incode_mask)
565 {
566 cache_ptr->howto = &howto_reloc_abs32code;
567 }
568 else
569 {
570 cache_ptr->howto = &howto_reloc_abs32;
571 }
572 }
573 if (cache_ptr->address < prev_addr)
574 {
575 /* Ouch! this reloc is out of order, insert into the right place
576 */
577 arelent tmp;
578 arelent *cursor = cache_ptr-1;
579 unsigned int where = counter;
580 bfd_vma stop = cache_ptr->address;
581 tmp = *cache_ptr;
582 while (cursor->address > stop)
583 {
584 cursor[1] = cursor[0];
585 cursor--;
586 }
587 cursor[1] = tmp;
588 }
589 else
590 {
591 prev_addr = cache_ptr->address;
592 }
593 }
594
595
596 free (relocs);
597 asect->relocation = reloc_cache;
598 asect->reloc_count = count;
599
600
601 return true;
602 }
603
604
605 static boolean
606 b_out_squirt_out_relocs (abfd, section)
607 bfd *abfd;
608 asection *section;
609 {
610
611 arelent **generic;
612 int r_extern;
613 int r_idx;
614 int r_addend;
615 int incode_mask;
616 int len_1;
617 unsigned int count = section->reloc_count;
618 struct relocation_info *native, *natptr;
619 size_t natsize = count * sizeof (struct relocation_info);
620 int extern_mask, pcrel_mask, len_2, callj_mask;
621 int len1;
622 if (count == 0) return true;
623 generic = section->orelocation;
624 native = ((struct relocation_info *) bfd_xmalloc (natsize));
625 if (!native) {
626 bfd_error = no_memory;
627 return false;
628 }
629
630 if (abfd->xvec->header_byteorder_big_p)
631 {
632 /* Big-endian bit field allocation order */
633 pcrel_mask = 0x80;
634 extern_mask = 0x10;
635 len_2 = 0x40;
636 len_1 = 0x20;
637 callj_mask = 0x02;
638 incode_mask = 0x08;
639 }
640 else
641 {
642 /* Little-endian bit field allocation order */
643 pcrel_mask = 0x01;
644 extern_mask = 0x08;
645 len_2 = 0x04;
646 len_1 = 0x02;
647 callj_mask = 0x40;
648 incode_mask = 0x10;
649 }
650
651 for (natptr = native; count > 0; --count, ++natptr, ++generic)
652 {
653 arelent *g = *generic;
654 unsigned char *raw = (unsigned char *)natptr;
655 asymbol *sym = *(g->sym_ptr_ptr);
656
657 asection *output_section = sym->section->output_section;
658 bfd_h_put_32(abfd, g->address, raw);
659 /* Find a type in the output format which matches the input howto -
660 * at the moment we assume input format == output format FIXME!!
661 */
662 /* FIXME: Need callj stuff here, and to check the howto entries to
663 be sure they are real for this architecture. */
664 if (g->howto== &howto_reloc_callj)
665 {
666 raw[7] = callj_mask + pcrel_mask + len_2;
667 }
668 else if (g->howto == &howto_reloc_pcrel24)
669 {
670 raw[7] = pcrel_mask + len_2;
671 }
672 else if (g->howto == &howto_reloc_pcrel13)
673 {
674 raw[7] = pcrel_mask + len_1;
675 }
676 else if (g->howto == &howto_reloc_abs32code)
677 {
678 raw[7] = len_2 + incode_mask;
679 }
680 else {
681 raw[7] = len_2;
682 }
683 if (output_section == &bfd_com_section
684 || output_section == &bfd_abs_section
685 || output_section == &bfd_und_section)
686 {
687 /* Fill in symbol */
688 r_extern = 1;
689 r_idx = stoi((*(g->sym_ptr_ptr))->flags);
690 }
691 else
692 {
693 /* Just an ordinary section */
694 r_extern = 0;
695 r_idx = output_section->target_index;
696 }
697
698 if (abfd->xvec->header_byteorder_big_p) {
699 raw[4] = (unsigned char) (r_idx >> 16);
700 raw[5] = (unsigned char) (r_idx >> 8);
701 raw[6] = (unsigned char) (r_idx );
702 } else {
703 raw[6] = (unsigned char) (r_idx >> 16);
704 raw[5] = (unsigned char) (r_idx>> 8);
705 raw[4] = (unsigned char) (r_idx );
706 }
707 if (r_extern)
708 raw[7] |= extern_mask;
709 }
710
711 if (bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
712 free((PTR)native);
713 return false;
714 }
715 free ((PTR)native);
716
717 return true;
718 }
719
720 /* This is stupid. This function should be a boolean predicate */
721 static unsigned int
722 b_out_canonicalize_reloc (abfd, section, relptr, symbols)
723 bfd *abfd;
724 sec_ptr section;
725 arelent **relptr;
726 asymbol **symbols;
727 {
728 arelent *tblptr = section->relocation;
729 unsigned int count = 0;
730
731 if (!(tblptr || b_out_slurp_reloc_table (abfd, section, symbols))) return 0;
732 tblptr = section->relocation;
733 if (!tblptr) return 0;
734
735 for (; count++ < section->reloc_count;)
736 *relptr++ = tblptr++;
737
738 *relptr = 0;
739
740 return section->reloc_count;
741 }
742
743 static unsigned int
744 b_out_get_reloc_upper_bound (abfd, asect)
745 bfd *abfd;
746 sec_ptr asect;
747 {
748 if (bfd_get_format (abfd) != bfd_object) {
749 bfd_error = invalid_operation;
750 return 0;
751 }
752
753 if (asect == obj_datasec (abfd))
754 return (sizeof (arelent *) *
755 ((exec_hdr(abfd)->a_drsize / sizeof (struct relocation_info))
756 +1));
757
758 if (asect == obj_textsec (abfd))
759 return (sizeof (arelent *) *
760 ((exec_hdr(abfd)->a_trsize / sizeof (struct relocation_info))
761 +1));
762
763 bfd_error = invalid_operation;
764 return 0;
765 }
766 \f
767 static boolean
768 b_out_set_section_contents (abfd, section, location, offset, count)
769 bfd *abfd;
770 sec_ptr section;
771 unsigned char *location;
772 file_ptr offset;
773 int count;
774 {
775
776 if (abfd->output_has_begun == false) { /* set by bfd.c handler */
777 if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL) /*||
778 (obj_textsec (abfd)->_cooked_size == 0) || (obj_datasec (abfd)->_cooked_size == 0)*/) {
779 bfd_error = invalid_operation;
780 return false;
781 }
782
783 obj_textsec (abfd)->filepos = sizeof(struct internal_exec);
784 obj_datasec(abfd)->filepos = obj_textsec(abfd)->filepos
785 + obj_textsec (abfd)->_raw_size;
786
787 }
788 /* regardless, once we know what we're doing, we might as well get going */
789 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
790
791 if (count != 0) {
792 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false;
793 }
794 return true;
795 }
796
797 static boolean
798 b_out_set_arch_mach (abfd, arch, machine)
799 bfd *abfd;
800 enum bfd_architecture arch;
801 unsigned long machine;
802 {
803 bfd_default_set_arch_mach(abfd, arch, machine);
804
805 if (arch == bfd_arch_unknown) /* Unknown machine arch is OK */
806 return true;
807 if (arch == bfd_arch_i960) /* i960 default is OK */
808 switch (machine) {
809 case bfd_mach_i960_core:
810 case bfd_mach_i960_kb_sb:
811 case bfd_mach_i960_mc:
812 case bfd_mach_i960_xa:
813 case bfd_mach_i960_ca:
814 case bfd_mach_i960_ka_sa:
815 case 0:
816 return true;
817 default:
818 return false;
819 }
820
821 return false;
822 }
823
824 static int
825 DEFUN(b_out_sizeof_headers,(ignore_abfd, ignore),
826 bfd *ignore_abfd AND
827 boolean ignore)
828 {
829 return sizeof(struct internal_exec);
830 }
831
832
833
834 /************************************************************************/
835 static bfd_vma
836 DEFUN(get_value,(reloc, seclet),
837 arelent *reloc AND
838 bfd_seclet_type *seclet)
839 {
840 bfd_vma value;
841 asymbol *symbol = *(reloc->sym_ptr_ptr);
842 /* A symbol holds a pointer to a section, and an offset from the
843 base of the section. To relocate, we find where the section will
844 live in the output and add that in */
845
846 if (symbol->section == &bfd_und_section)
847 {
848 /* Ouch, this is an undefined symbol.. */
849 bfd_error_vector.undefined_symbol(reloc, seclet);
850 value = symbol->value;
851 }
852 else
853 {
854 value = symbol->value +
855 symbol->section->output_offset +
856 symbol->section->output_section->vma;
857 }
858
859
860 /* Add the value contained in the relocation */
861 value += (short)((reloc->addend) & 0xffff);
862
863 return value;
864 }
865
866 static void
867 DEFUN(perform_slip,(s, slip, input_section, value),
868 asymbol **s AND
869 unsigned int slip AND
870 asection *input_section AND
871 bfd_vma value)
872 {
873
874 /* Find all symbols past this point, and make them know
875 what's happened */
876 while (*s)
877 {
878 asymbol *p = *s;
879 if (p->section == input_section)
880 {
881 /* This was pointing into this section, so mangle it */
882 if (p->value > value)
883 {
884 p->value -=slip;
885 }
886 }
887 s++;
888
889 }
890 }
891
892 /* This routine works out if the thing we want to get to can be
893 reached with a 24bit offset instead of a 32 bit one.
894 If it can, then it changes the amode */
895
896 static int
897 DEFUN(abs32code,(input_section, symbols, r, shrink),
898 asection *input_section AND
899 asymbol **symbols AND
900 arelent *r AND
901 unsigned int shrink)
902 {
903 bfd_vma value = get_value(r,0);
904
905
906 bfd_vma dot = input_section->output_section->vma + input_section->output_offset + r->address;
907 bfd_vma gap;
908
909 /* See if the address we're looking at within 2^23 bytes of where
910 we are, if so then we can use a small branch rather than the
911 jump we were going to */
912
913 gap = value - (dot - shrink);
914
915
916 if (-1<<23 < (long)gap && (long)gap < 1<<23 )
917 {
918
919 /* Change the reloc type from 32bitcode possible 24, to 24bit
920 possible 32 */
921
922 r->howto = &howto_reloc_abs32codeshrunk;
923 /* The place to relc moves back by four bytes */
924 r->address -=4;
925
926 /* This will be four bytes smaller in the long run */
927 shrink += 4 ;
928 perform_slip(symbols, 4, input_section, r->address-shrink +4);
929
930
931 }
932 return shrink;
933 }
934
935
936 static boolean
937 DEFUN(b_out_relax_section,(abfd, i, symbols),
938 bfd *abfd AND
939 asection *i AND
940 asymbol **symbols)
941 {
942
943 /* Get enough memory to hold the stuff */
944 bfd *input_bfd = i->owner;
945 asection *input_section = i;
946 int shrink = 0 ;
947 boolean new = false;
948
949 bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
950 input_section);
951 arelent **reloc_vector = (arelent **)alloca(reloc_size);
952
953 /* Get the relocs and think about them */
954 if (bfd_canonicalize_reloc(input_bfd,
955 input_section,
956 reloc_vector,
957 symbols))
958 {
959 arelent **parent;
960 for (parent = reloc_vector; *parent; parent++)
961 {
962 arelent *r = *parent;
963 switch (r->howto->type) {
964 case ABS32CODE:
965 /* A 32bit reloc in an addressing mode */
966 shrink = abs32code(input_section, symbols, r,shrink);
967 new=true;
968 break;
969 case ABS32CODE_SHRUNK:
970 shrink+=4;
971 break;
972 }
973 }
974 }
975 input_section->_cooked_size = input_section->_raw_size - shrink;
976
977 return new;
978 }
979
980 static bfd_byte *
981 DEFUN(b_out_get_relocated_section_contents,(in_abfd, seclet, data),
982 bfd *in_abfd AND
983 bfd_seclet_type *seclet AND
984 bfd_byte *data)
985
986 {
987 /* Get enough memory to hold the stuff */
988 bfd *input_bfd = seclet->u.indirect.section->owner;
989 asection *input_section = seclet->u.indirect.section;
990 bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
991 input_section);
992 arelent **reloc_vector = (arelent **)alloca(reloc_size);
993
994 /* read in the section */
995 bfd_get_section_contents(input_bfd,
996 input_section,
997 data,
998 0,
999 input_section->_raw_size);
1000
1001
1002 if (bfd_canonicalize_reloc(input_bfd,
1003 input_section,
1004 reloc_vector,
1005 seclet->u.indirect.symbols) )
1006 {
1007 arelent **parent = reloc_vector;
1008 arelent *reloc ;
1009
1010
1011
1012 unsigned int dst_address = 0;
1013 unsigned int src_address = 0;
1014 unsigned int run;
1015 unsigned int idx;
1016
1017 /* Find how long a run we can do */
1018 while (dst_address < seclet->size)
1019 {
1020
1021 reloc = *parent;
1022 if (reloc)
1023 {
1024 /* Note that the relaxing didn't tie up the addresses in the
1025 relocation, so we use the original address to work out the
1026 run of non-relocated data */
1027 run = reloc->address - src_address;
1028 parent++;
1029
1030 }
1031 else
1032 {
1033 run = seclet->size - dst_address;
1034 }
1035 /* Copy the bytes */
1036 for (idx = 0; idx < run; idx++)
1037 {
1038 data[dst_address++] = data[src_address++];
1039 }
1040
1041 /* Now do the relocation */
1042
1043 if (reloc)
1044 {
1045 switch (reloc->howto->type)
1046 {
1047 case ABS32CODE:
1048 calljx_callback(in_abfd, reloc, src_address + data, dst_address+data, input_section);
1049 src_address+=4;
1050 dst_address+=4;
1051 break;
1052 case ABS32:
1053 bfd_put_32(in_abfd, get_value(reloc, seclet), data+dst_address);
1054 src_address+=4;
1055 dst_address+=4;
1056 break;
1057 case CALLJ:
1058 callj_callback(in_abfd, reloc ,data,src_address,dst_address,input_section);
1059 src_address+=4;
1060 dst_address+=4;
1061 break;
1062 case ABS32CODE_SHRUNK:
1063 /* This used to be a callx, but we've found out that a
1064 callj will reach, so do the right thing */
1065 callj_callback(in_abfd, reloc,data,src_address+4, dst_address,input_section);
1066
1067 dst_address+=4;
1068 src_address+=8;
1069 break;
1070 case PCREL24:
1071 {
1072 long int word = bfd_get_32(in_abfd, data+src_address);
1073 asymbol *symbol = *(reloc->sym_ptr_ptr);
1074 word = (word & ~BAL_MASK) |
1075 (((word & BAL_MASK) +
1076 symbol->section->output_offset +
1077 symbol->section->output_section->vma+
1078 symbol->value + reloc->addend - dst_address -
1079 ( input_section->output_section->vma + input_section->output_offset))
1080 & BAL_MASK);
1081
1082 bfd_put_32(in_abfd,word, data+dst_address);
1083 dst_address+=4;
1084 src_address+=4;
1085
1086 }
1087 break;
1088
1089 case PCREL13:
1090 {
1091 long int word = bfd_get_32(in_abfd, data+src_address);
1092 asymbol *symbol = *(reloc->sym_ptr_ptr);
1093 word = (word & ~PCREL13_MASK) |
1094 (((word & PCREL13_MASK) +
1095 symbol->section->output_offset +
1096 symbol->section->output_section->vma+
1097 symbol->value + reloc->addend - dst_address -
1098 ( input_section->output_section->vma + input_section->output_offset))
1099 & PCREL13_MASK);
1100
1101 bfd_put_32(in_abfd,word, data+dst_address);
1102 dst_address+=4;
1103 src_address+=4;
1104
1105 }
1106 break;
1107
1108 default:
1109
1110 abort();
1111 }
1112 }
1113 }
1114 }
1115 return data;
1116 }
1117 /***********************************************************************/
1118
1119 /* Build the transfer vectors for Big and Little-Endian B.OUT files. */
1120
1121 /* We don't have core files. */
1122 #define aout_32_core_file_failing_command _bfd_dummy_core_file_failing_command
1123 #define aout_32_core_file_failing_signal _bfd_dummy_core_file_failing_signal
1124 #define aout_32_core_file_matches_executable_p \
1125 _bfd_dummy_core_file_matches_executable_p
1126
1127 /* We use BSD-Unix generic archive files. */
1128 #define aout_32_openr_next_archived_file bfd_generic_openr_next_archived_file
1129 #define aout_32_generic_stat_arch_elt bfd_generic_stat_arch_elt
1130 #define aout_32_slurp_armap bfd_slurp_bsd_armap
1131 #define aout_32_slurp_extended_name_table bfd_true
1132 #define aout_32_write_armap bsd_write_armap
1133 #define aout_32_truncate_arname bfd_bsd_truncate_arname
1134
1135 /* We override these routines from the usual a.out file routines. */
1136 #define aout_32_canonicalize_reloc b_out_canonicalize_reloc
1137 #define aout_32_get_reloc_upper_bound b_out_get_reloc_upper_bound
1138 #define aout_32_set_section_contents b_out_set_section_contents
1139 #define aout_32_set_arch_mach b_out_set_arch_mach
1140 #define aout_32_sizeof_headers b_out_sizeof_headers
1141
1142 #define aout_32_bfd_debug_info_start bfd_void
1143 #define aout_32_bfd_debug_info_end bfd_void
1144 #define aout_32_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
1145
1146 #define aout_32_bfd_get_relocated_section_contents b_out_get_relocated_section_contents
1147 #define aout_32_bfd_relax_section b_out_relax_section
1148
1149 bfd_target b_out_vec_big_host =
1150 {
1151 "b.out.big", /* name */
1152 bfd_target_aout_flavour,
1153 false, /* data byte order is little */
1154 true, /* hdr byte order is big */
1155 (HAS_RELOC | EXEC_P | /* object flags */
1156 HAS_LINENO | HAS_DEBUG |
1157 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT ),
1158 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1159 ' ', /* ar_pad_char */
1160 16, /* ar_max_namelen */
1161 2, /* minumum alignment power */
1162
1163 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
1164 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
1165 {_bfd_dummy_target, b_out_object_p, /* bfd_check_format */
1166 bfd_generic_archive_p, _bfd_dummy_target},
1167 {bfd_false, b_out_mkobject, /* bfd_set_format */
1168 _bfd_generic_mkarchive, bfd_false},
1169 {bfd_false, b_out_write_object_contents, /* bfd_write_contents */
1170 _bfd_write_archive_contents, bfd_false},
1171
1172 JUMP_TABLE(aout_32),
1173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* COFF stuff?! */
1174 b_out_reloc_type_lookup,
1175 };
1176
1177
1178 bfd_target b_out_vec_little_host =
1179 {
1180 "b.out.little", /* name */
1181 bfd_target_aout_flavour,
1182 false, /* data byte order is little */
1183 false, /* header byte order is little */
1184 (HAS_RELOC | EXEC_P | /* object flags */
1185 HAS_LINENO | HAS_DEBUG |
1186 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT ),
1187 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1188 ' ', /* ar_pad_char */
1189 16, /* ar_max_namelen */
1190 2, /* minum align */
1191 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
1192 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* hdrs */
1193
1194 {_bfd_dummy_target, b_out_object_p, /* bfd_check_format */
1195 bfd_generic_archive_p, _bfd_dummy_target},
1196 {bfd_false, b_out_mkobject, /* bfd_set_format */
1197 _bfd_generic_mkarchive, bfd_false},
1198 {bfd_false, b_out_write_object_contents, /* bfd_write_contents */
1199 _bfd_write_archive_contents, bfd_false},
1200 JUMP_TABLE(aout_32),
1201 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* COFF stuff?! */
1202 b_out_reloc_type_lookup,
1203 };
This page took 0.052958 seconds and 5 git commands to generate.