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