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