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