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