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