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