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