]>
Commit | Line | Data |
---|---|---|
88dfcd68 SC |
1 | /* BFD semi-generic back-end for a.out binaries |
2 | Copyright (C) 1990-1991 Free Software Foundation, Inc. | |
3 | Written by Cygnus Support. | |
7ed4093a | 4 | |
88dfcd68 | 5 | This file is part of BFD, the Binary File Descriptor library. |
7ed4093a | 6 | |
88dfcd68 | 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 |
88dfcd68 SC |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
7ed4093a | 11 | |
88dfcd68 | 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 | |
88dfcd68 SC |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
7ed4093a | 20 | |
4e41b5aa SC |
21 | /* |
22 | SECTION | |
23 | a.out backends | |
6f715d66 | 24 | |
6f715d66 | 25 | |
4e41b5aa | 26 | DESCRIPTION |
6f715d66 | 27 | |
4e41b5aa SC |
28 | BFD supports a number of different flavours of a.out format, |
29 | though the major differences are only the sizes of the | |
30 | structures on disk, and the shape of the relocation | |
31 | information. | |
6f715d66 | 32 | |
4e41b5aa SC |
33 | The support is split into a basic support file @code{aoutx.h} |
34 | and other files which derive functions from the base. One | |
35 | derivation file is @code{aoutf1.h} (for a.out flavour 1), and | |
36 | adds to the basic a.out functions support for sun3, sun4, 386 | |
37 | and 29k a.out files, to create a target jump vector for a | |
38 | specific target. | |
6f715d66 | 39 | |
4e41b5aa SC |
40 | This information is further split out into more specific files |
41 | for each machine, including @code{sunos.c} for sun3 and sun4, | |
42 | @code{newsos3.c} for the Sony NEWS, and @code{demo64.c} for a | |
43 | demonstration of a 64 bit a.out format. | |
44 | ||
45 | The base file @code{aoutx.h} defines general mechanisms for | |
46 | reading and writing records to and from disk, and various | |
47 | other methods which BFD requires. It is included by | |
48 | @code{aout32.c} and @code{aout64.c} to form the names | |
49 | aout_32_swap_exec_header_in, aout_64_swap_exec_header_in, etc. | |
50 | ||
51 | As an example, this is what goes on to make the back end for a | |
52 | sun4, from aout32.c | |
53 | ||
3f7607af PB |
54 | | #define ARCH_SIZE 32 |
55 | | #include "aoutx.h" | |
4e41b5aa SC |
56 | |
57 | Which exports names: | |
58 | ||
3f7607af PB |
59 | | ... |
60 | | aout_32_canonicalize_reloc | |
61 | | aout_32_find_nearest_line | |
62 | | aout_32_get_lineno | |
63 | | aout_32_get_reloc_upper_bound | |
64 | | ... | |
6f715d66 | 65 | |
4e41b5aa SC |
66 | from sunos.c |
67 | ||
3f7607af PB |
68 | | #define ARCH 32 |
69 | | #define TARGET_NAME "a.out-sunos-big" | |
70 | | #define VECNAME sunos_big_vec | |
71 | | #include "aoutf1.h" | |
4e41b5aa SC |
72 | |
73 | requires all the names from aout32.c, and produces the jump vector | |
6f715d66 | 74 | |
3f7607af | 75 | | sunos_big_vec |
c6705697 | 76 | |
4e41b5aa SC |
77 | The file host-aout.c is a special case. It is for a large set |
78 | of hosts that use ``more or less standard'' a.out files, and | |
79 | for which cross-debugging is not interesting. It uses the | |
80 | standard 32-bit a.out support routines, but determines the | |
81 | file offsets and addresses of the text, data, and BSS | |
82 | sections, the machine architecture and machine type, and the | |
83 | entry point address, in a host-dependent manner. Once these | |
84 | values have been determined, generic code is used to handle | |
85 | the object file. | |
c6705697 | 86 | |
4e41b5aa SC |
87 | When porting it to run on a new system, you must supply: |
88 | ||
3f7607af PB |
89 | | HOST_PAGE_SIZE |
90 | | HOST_SEGMENT_SIZE | |
91 | | HOST_MACHINE_ARCH (optional) | |
92 | | HOST_MACHINE_MACHINE (optional) | |
93 | | HOST_TEXT_START_ADDR | |
94 | | HOST_STACK_END_ADDR | |
c6705697 | 95 | |
3f7607af PB |
96 | in the file <<../include/sys/h-XXX.h>> (for your host). These |
97 | values, plus the structures and macros defined in <<a.out.h>> on | |
4e41b5aa SC |
98 | your host system, will produce a BFD target that will access |
99 | ordinary a.out files on your host. To configure a new machine | |
3f7607af | 100 | to use <<host-aout.c>., specify: |
c6705697 | 101 | |
3f7607af PB |
102 | | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec |
103 | | TDEPFILES= host-aout.o trad-core.o | |
c6705697 | 104 | |
3f7607af PB |
105 | in the <<config/mt-XXX>> file, and modify configure.in to use the |
106 | <<mt-XXX>> file (by setting "<<bfd_target=XXX>>") when your | |
4e41b5aa | 107 | configuration is selected. |
c6705697 | 108 | |
6f715d66 SC |
109 | */ |
110 | ||
c618de01 SC |
111 | #define KEEPIT flags |
112 | #define KEEPITTYPE int | |
67c060c3 SC |
113 | |
114 | #include "bfd.h" | |
7ed4093a SC |
115 | #include <sysdep.h> |
116 | #include <ansidecl.h> | |
117 | ||
9e2dad8e | 118 | struct external_exec; |
6f715d66 | 119 | #include "libaout.h" |
7ed4093a | 120 | #include "libbfd.h" |
c3eb25fc SC |
121 | #include "aout/aout64.h" |
122 | #include "aout/stab_gnu.h" | |
123 | #include "aout/ar.h" | |
7ed4093a SC |
124 | |
125 | void (*bfd_error_trap)(); | |
126 | ||
4e41b5aa SC |
127 | /* |
128 | SUBSECTION | |
129 | relocations | |
130 | ||
131 | DESCRIPTION | |
132 | The file @code{aoutx.h} caters for both the @emph{standard} | |
133 | and @emph{extended} forms of a.out relocation records. | |
134 | ||
135 | The standard records are characterised by containing only an | |
136 | address, a symbol index and a type field. The extended records | |
137 | (used on 29ks and sparcs) also have a full integer for an | |
138 | addend. | |
7ed4093a | 139 | |
6f715d66 | 140 | */ |
7ed4093a | 141 | #define CTOR_TABLE_RELOC_IDX 2 |
67c060c3 | 142 | |
67c060c3 | 143 | |
7ed4093a SC |
144 | static reloc_howto_type howto_table_ext[] = |
145 | { | |
146 | HOWTO(RELOC_8, 0, 0, 8, false, 0, true, true,0,"8", false, 0,0x000000ff, false), | |
147 | HOWTO(RELOC_16, 0, 1, 16, false, 0, true, true,0,"16", false, 0,0x0000ffff, false), | |
148 | HOWTO(RELOC_32, 0, 2, 32, false, 0, true, true,0,"32", false, 0,0xffffffff, false), | |
149 | HOWTO(RELOC_DISP8, 0, 0, 8, true, 0, false, true,0,"DISP8", false, 0,0x000000ff, false), | |
150 | HOWTO(RELOC_DISP16, 0, 1, 16, true, 0, false, true,0,"DISP16", false, 0,0x0000ffff, false), | |
151 | HOWTO(RELOC_DISP32, 0, 2, 32, true, 0, false, true,0,"DISP32", false, 0,0xffffffff, false), | |
152 | HOWTO(RELOC_WDISP30,2, 2, 30, true, 0, false, true,0,"WDISP30", false, 0,0x3fffffff, false), | |
153 | HOWTO(RELOC_WDISP22,2, 2, 22, true, 0, false, true,0,"WDISP22", false, 0,0x003fffff, false), | |
154 | HOWTO(RELOC_HI22, 10, 2, 22, false, 0, false, true,0,"HI22", false, 0,0x003fffff, false), | |
155 | HOWTO(RELOC_22, 0, 2, 22, false, 0, false, true,0,"22", false, 0,0x003fffff, false), | |
156 | HOWTO(RELOC_13, 0, 2, 13, false, 0, false, true,0,"13", false, 0,0x00001fff, false), | |
157 | HOWTO(RELOC_LO10, 0, 2, 10, false, 0, false, true,0,"LO10", false, 0,0x000003ff, false), | |
158 | HOWTO(RELOC_SFA_BASE,0, 2, 32, false, 0, false, true,0,"SFA_BASE", false, 0,0xffffffff, false), | |
159 | HOWTO(RELOC_SFA_OFF13,0,2, 32, false, 0, false, true,0,"SFA_OFF13",false, 0,0xffffffff, false), | |
160 | HOWTO(RELOC_BASE10, 0, 2, 16, false, 0, false, true,0,"BASE10", false, 0,0x0000ffff, false), | |
161 | HOWTO(RELOC_BASE13, 0, 2, 13, false, 0, false, true,0,"BASE13", false, 0,0x00001fff, false), | |
162 | HOWTO(RELOC_BASE22, 0, 2, 0, false, 0, false, true,0,"BASE22", false, 0,0x00000000, false), | |
163 | HOWTO(RELOC_PC10, 0, 2, 10, false, 0, false, true,0,"PC10", false, 0,0x000003ff, false), | |
164 | HOWTO(RELOC_PC22, 0, 2, 22, false, 0, false, true,0,"PC22", false, 0,0x003fffff, false), | |
165 | HOWTO(RELOC_JMP_TBL,0, 2, 32, false, 0, false, true,0,"JMP_TBL", false, 0,0xffffffff, false), | |
166 | HOWTO(RELOC_SEGOFF16,0, 2, 0, false, 0, false, true,0,"SEGOFF16", false, 0,0x00000000, false), | |
167 | HOWTO(RELOC_GLOB_DAT,0, 2, 0, false, 0, false, true,0,"GLOB_DAT", false, 0,0x00000000, false), | |
168 | HOWTO(RELOC_JMP_SLOT,0, 2, 0, false, 0, false, true,0,"JMP_SLOT", false, 0,0x00000000, false), | |
67c060c3 SC |
169 | HOWTO(RELOC_RELATIVE,0, 2, 0, false, 0, false, true,0,"RELATIVE", false, 0,0x00000000, false), |
170 | ||
7ed4093a SC |
171 | }; |
172 | ||
173 | /* Convert standard reloc records to "arelent" format (incl byte swap). */ | |
174 | ||
175 | static reloc_howto_type howto_table_std[] = { | |
176 | /* type rs size bsz pcrel bitpos abs ovrf sf name part_inpl readmask setmask pcdone */ | |
177 | HOWTO( 0, 0, 0, 8, false, 0, true, true,0,"8", true, 0x000000ff,0x000000ff, false), | |
178 | HOWTO( 1, 0, 1, 16, false, 0, true, true,0,"16", true, 0x0000ffff,0x0000ffff, false), | |
179 | HOWTO( 2, 0, 2, 32, false, 0, true, true,0,"32", true, 0xffffffff,0xffffffff, false), | |
180 | HOWTO( 3, 0, 3, 64, false, 0, true, true,0,"64", true, 0xdeaddead,0xdeaddead, false), | |
181 | HOWTO( 4, 0, 0, 8, true, 0, false, true,0,"DISP8", true, 0x000000ff,0x000000ff, false), | |
182 | HOWTO( 5, 0, 1, 16, true, 0, false, true,0,"DISP16", true, 0x0000ffff,0x0000ffff, false), | |
183 | HOWTO( 6, 0, 2, 32, true, 0, false, true,0,"DISP32", true, 0xffffffff,0xffffffff, false), | |
184 | HOWTO( 7, 0, 3, 64, true, 0, false, true,0,"DISP64", true, 0xfeedface,0xfeedface, false), | |
185 | }; | |
186 | ||
187 | ||
188 | bfd_error_vector_type bfd_error_vector; | |
6f715d66 | 189 | |
4e41b5aa SC |
190 | /* |
191 | SUBSECTION | |
192 | Internal Entry Points | |
193 | ||
194 | DESCRIPTION | |
195 | @code{aoutx.h} exports several routines for accessing the | |
196 | contents of an a.out file, which are gathered and exported in | |
197 | turn by various format specific files (eg sunos.c). | |
198 | ||
6f715d66 SC |
199 | */ |
200 | ||
4e41b5aa SC |
201 | /* |
202 | FUNCTION | |
203 | aout_<size>_swap_exec_header_in | |
204 | ||
205 | DESCRIPTION | |
206 | Swaps the information in an executable header taken from a raw | |
207 | byte stream memory image, into the internal exec_header | |
208 | structure. | |
209 | ||
210 | EXAMPLE | |
211 | void aout_<size>_swap_exec_header_in, | |
212 | (bfd *abfd, | |
213 | struct external_exec *raw_bytes, | |
214 | struct internal_exec *execp); | |
6f715d66 SC |
215 | */ |
216 | ||
7ed4093a SC |
217 | void |
218 | DEFUN(NAME(aout,swap_exec_header_in),(abfd, raw_bytes, execp), | |
219 | bfd *abfd AND | |
220 | struct external_exec *raw_bytes AND | |
221 | struct internal_exec *execp) | |
222 | { | |
223 | struct external_exec *bytes = (struct external_exec *)raw_bytes; | |
224 | ||
55c0061e FF |
225 | /* The internal_exec structure has some fields that are unused in this |
226 | configuration (IE for i960), so ensure that all such uninitialized | |
227 | fields are zero'd out. There are places where two of these structs | |
228 | are memcmp'd, and thus the contents do matter. */ | |
229 | memset (execp, 0, sizeof (struct internal_exec)); | |
7ed4093a SC |
230 | /* Now fill in fields in the execp, from the bytes in the raw data. */ |
231 | execp->a_info = bfd_h_get_32 (abfd, bytes->e_info); | |
232 | execp->a_text = GET_WORD (abfd, bytes->e_text); | |
233 | execp->a_data = GET_WORD (abfd, bytes->e_data); | |
234 | execp->a_bss = GET_WORD (abfd, bytes->e_bss); | |
235 | execp->a_syms = GET_WORD (abfd, bytes->e_syms); | |
236 | execp->a_entry = GET_WORD (abfd, bytes->e_entry); | |
237 | execp->a_trsize = GET_WORD (abfd, bytes->e_trsize); | |
238 | execp->a_drsize = GET_WORD (abfd, bytes->e_drsize); | |
239 | } | |
240 | ||
4e41b5aa SC |
241 | /* |
242 | FUNCTION | |
243 | aout_<size>_swap_exec_header_out | |
244 | ||
245 | DESCRIPTION | |
246 | Swaps the information in an internal exec header structure | |
247 | into the supplied buffer ready for writing to disk. | |
248 | ||
249 | EXAMPLE | |
250 | void aout_<size>_swap_exec_header_out | |
6f715d66 SC |
251 | (bfd *abfd, |
252 | struct internal_exec *execp, | |
4e41b5aa | 253 | struct external_exec *raw_bytes); |
6f715d66 | 254 | */ |
7ed4093a SC |
255 | void |
256 | DEFUN(NAME(aout,swap_exec_header_out),(abfd, execp, raw_bytes), | |
257 | bfd *abfd AND | |
258 | struct internal_exec *execp AND | |
259 | struct external_exec *raw_bytes) | |
260 | { | |
261 | struct external_exec *bytes = (struct external_exec *)raw_bytes; | |
262 | ||
263 | /* Now fill in fields in the raw data, from the fields in the exec struct. */ | |
264 | bfd_h_put_32 (abfd, execp->a_info , bytes->e_info); | |
265 | PUT_WORD (abfd, execp->a_text , bytes->e_text); | |
266 | PUT_WORD (abfd, execp->a_data , bytes->e_data); | |
267 | PUT_WORD (abfd, execp->a_bss , bytes->e_bss); | |
268 | PUT_WORD (abfd, execp->a_syms , bytes->e_syms); | |
269 | PUT_WORD (abfd, execp->a_entry , bytes->e_entry); | |
270 | PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize); | |
271 | PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize); | |
272 | } | |
273 | ||
7ed4093a | 274 | |
6f715d66 | 275 | |
4e41b5aa SC |
276 | /* |
277 | FUNCTION | |
278 | aout_<size>_some_aout_object_p | |
6f715d66 | 279 | |
4e41b5aa SC |
280 | DESCRIPTION |
281 | Some A.OUT variant thinks that the file whose format we're | |
282 | checking is an a.out file. Do some more checking, and set up | |
283 | for access if it really is. Call back to the calling | |
284 | environments "finish up" function just before returning, to | |
285 | handle any last-minute setup. | |
6f715d66 | 286 | |
4e41b5aa SC |
287 | EXAMPLE |
288 | bfd_target *aout_<size>_some_aout_object_p | |
6f715d66 | 289 | (bfd *abfd, |
4e41b5aa | 290 | bfd_target *(*callback_to_real_object_p)()); |
6f715d66 | 291 | */ |
7ed4093a SC |
292 | |
293 | bfd_target * | |
7b02b4ed | 294 | DEFUN(NAME(aout,some_aout_object_p),(abfd, execp, callback_to_real_object_p), |
7ed4093a | 295 | bfd *abfd AND |
7b02b4ed | 296 | struct internal_exec *execp AND |
7ed4093a SC |
297 | bfd_target *(*callback_to_real_object_p) ()) |
298 | { | |
6db82ea7 | 299 | struct aout_data_struct *rawptr; |
e6e265ce | 300 | bfd_target *result; |
7ed4093a | 301 | |
6db82ea7 | 302 | rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, sizeof (struct aout_data_struct )); |
7ed4093a SC |
303 | if (rawptr == NULL) { |
304 | bfd_error = no_memory; | |
305 | return 0; | |
306 | } | |
307 | ||
6db82ea7 SC |
308 | abfd->tdata.aout_data = rawptr; |
309 | abfd->tdata.aout_data->a.hdr = &rawptr->e; | |
310 | *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */ | |
311 | execp = abfd->tdata.aout_data->a.hdr; | |
7ed4093a SC |
312 | |
313 | /* Set the file flags */ | |
314 | abfd->flags = NO_FLAGS; | |
315 | if (execp->a_drsize || execp->a_trsize) | |
316 | abfd->flags |= HAS_RELOC; | |
e6e265ce | 317 | /* Setting of EXEC_P has been deferred to the bottom of this function */ |
7ed4093a SC |
318 | if (execp->a_syms) |
319 | abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS; | |
320 | ||
321 | if (N_MAGIC (*execp) == ZMAGIC) abfd->flags |= D_PAGED; | |
322 | if (N_MAGIC (*execp) == NMAGIC) abfd->flags |= WP_TEXT; | |
323 | ||
324 | bfd_get_start_address (abfd) = execp->a_entry; | |
325 | ||
326 | obj_aout_symbols (abfd) = (aout_symbol_type *)NULL; | |
327 | bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist); | |
328 | ||
7ed4093a SC |
329 | /* The default relocation entry size is that of traditional V7 Unix. */ |
330 | obj_reloc_entry_size (abfd) = RELOC_STD_SIZE; | |
331 | ||
7b02b4ed JG |
332 | /* The default symbol entry size is that of traditional Unix. */ |
333 | obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE; | |
334 | ||
7ed4093a SC |
335 | /* create the sections. This is raunchy, but bfd_close wants to reclaim |
336 | them */ | |
6db82ea7 | 337 | |
7ed4093a SC |
338 | obj_textsec (abfd) = (asection *)NULL; |
339 | obj_datasec (abfd) = (asection *)NULL; | |
340 | obj_bsssec (abfd) = (asection *)NULL; | |
6db82ea7 | 341 | |
7ed4093a SC |
342 | (void)bfd_make_section(abfd, ".text"); |
343 | (void)bfd_make_section(abfd, ".data"); | |
344 | (void)bfd_make_section(abfd, ".bss"); | |
6db82ea7 SC |
345 | /* (void)bfd_make_section(abfd, BFD_ABS_SECTION_NAME); |
346 | (void)bfd_make_section (abfd, BFD_UND_SECTION_NAME); | |
347 | (void)bfd_make_section (abfd, BFD_COM_SECTION_NAME);*/ | |
7ed4093a SC |
348 | abfd->sections = obj_textsec (abfd); |
349 | obj_textsec (abfd)->next = obj_datasec (abfd); | |
350 | obj_datasec (abfd)->next = obj_bsssec (abfd); | |
351 | ||
6db82ea7 SC |
352 | obj_datasec (abfd)->_raw_size = execp->a_data; |
353 | obj_bsssec (abfd)->_raw_size = execp->a_bss; | |
7ed4093a | 354 | |
7ed4093a | 355 | obj_textsec (abfd)->flags = (execp->a_trsize != 0 ? |
7b02b4ed JG |
356 | (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_HAS_CONTENTS) : |
357 | (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)); | |
7ed4093a | 358 | obj_datasec (abfd)->flags = (execp->a_drsize != 0 ? |
7b02b4ed JG |
359 | (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_HAS_CONTENTS) : |
360 | (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)); | |
7ed4093a SC |
361 | obj_bsssec (abfd)->flags = SEC_ALLOC; |
362 | ||
363 | #ifdef THIS_IS_ONLY_DOCUMENTATION | |
98d43107 JG |
364 | /* The common code can't fill in these things because they depend |
365 | on either the start address of the text segment, the rounding | |
366 | up of virtual addersses between segments, or the starting file | |
367 | position of the text segment -- all of which varies among different | |
368 | versions of a.out. */ | |
369 | ||
7ed4093a SC |
370 | /* Call back to the format-dependent code to fill in the rest of the |
371 | fields and do any further cleanup. Things that should be filled | |
372 | in by the callback: */ | |
373 | ||
374 | struct exec *execp = exec_hdr (abfd); | |
375 | ||
98d43107 | 376 | obj_textsec (abfd)->size = N_TXTSIZE(*execp); |
6db82ea7 | 377 | obj_textsec (abfd)->raw_size = N_TXTSIZE(*execp); |
98d43107 JG |
378 | /* data and bss are already filled in since they're so standard */ |
379 | ||
7ed4093a | 380 | /* The virtual memory addresses of the sections */ |
7ed4093a | 381 | obj_textsec (abfd)->vma = N_TXTADDR(*execp); |
98d43107 JG |
382 | obj_datasec (abfd)->vma = N_DATADDR(*execp); |
383 | obj_bsssec (abfd)->vma = N_BSSADDR(*execp); | |
7ed4093a SC |
384 | |
385 | /* The file offsets of the sections */ | |
386 | obj_textsec (abfd)->filepos = N_TXTOFF(*execp); | |
387 | obj_datasec (abfd)->filepos = N_DATOFF(*execp); | |
388 | ||
389 | /* The file offsets of the relocation info */ | |
390 | obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp); | |
391 | obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp); | |
392 | ||
393 | /* The file offsets of the string table and symbol table. */ | |
394 | obj_str_filepos (abfd) = N_STROFF (*execp); | |
395 | obj_sym_filepos (abfd) = N_SYMOFF (*execp); | |
396 | ||
7ed4093a SC |
397 | /* Determine the architecture and machine type of the object file. */ |
398 | switch (N_MACHTYPE (*exec_hdr (abfd))) { | |
399 | default: | |
400 | abfd->obj_arch = bfd_arch_obscure; | |
401 | break; | |
402 | } | |
403 | ||
404 | /* Determine the size of a relocation entry */ | |
405 | switch (abfd->obj_arch) { | |
406 | case bfd_arch_sparc: | |
407 | case bfd_arch_a29k: | |
408 | obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE; | |
409 | default: | |
410 | obj_reloc_entry_size (abfd) = RELOC_STD_SIZE; | |
411 | } | |
412 | ||
7b02b4ed JG |
413 | adata(abfd)->page_size = PAGE_SIZE; |
414 | adata(abfd)->segment_size = SEGMENT_SIZE; | |
415 | adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE; | |
416 | ||
7ed4093a SC |
417 | return abfd->xvec; |
418 | ||
419 | /* The architecture is encoded in various ways in various a.out variants, | |
420 | or is not encoded at all in some of them. The relocation size depends | |
421 | on the architecture and the a.out variant. Finally, the return value | |
422 | is the bfd_target vector in use. If an error occurs, return zero and | |
423 | set bfd_error to the appropriate error code. | |
424 | ||
425 | Formats such as b.out, which have additional fields in the a.out | |
426 | header, should cope with them in this callback as well. */ | |
427 | #endif /* DOCUMENTATION */ | |
428 | ||
e6e265ce JG |
429 | result = (*callback_to_real_object_p)(abfd); |
430 | ||
431 | /* Now that the segment addresses have been worked out, take a better | |
432 | guess at whether the file is executable. If the entry point | |
433 | is within the text segment, assume it is. (This makes files | |
434 | executable even if their entry point address is 0, as long as | |
435 | their text starts at zero.) | |
436 | ||
437 | At some point we should probably break down and stat the file and | |
438 | declare it executable if (one of) its 'x' bits are on... */ | |
439 | if ((execp->a_entry >= obj_textsec(abfd)->vma) && | |
6db82ea7 | 440 | (execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size)) |
e6e265ce JG |
441 | abfd->flags |= EXEC_P; |
442 | return result; | |
7ed4093a SC |
443 | } |
444 | ||
4e41b5aa SC |
445 | /* |
446 | FUNCTION | |
447 | aout_<size>_mkobject | |
6f715d66 | 448 | |
4e41b5aa SC |
449 | DESCRIPTION |
450 | This routine initializes a BFD for use with a.out files. | |
6f715d66 | 451 | |
4e41b5aa SC |
452 | EXAMPLE |
453 | boolean aout_<size>_mkobject, (bfd *); | |
6f715d66 | 454 | */ |
7ed4093a SC |
455 | |
456 | boolean | |
457 | DEFUN(NAME(aout,mkobject),(abfd), | |
458 | bfd *abfd) | |
459 | { | |
6db82ea7 | 460 | struct aout_data_struct *rawptr; |
7ed4093a SC |
461 | |
462 | bfd_error = system_call_error; | |
463 | ||
464 | /* Use an intermediate variable for clarity */ | |
6db82ea7 | 465 | rawptr = (struct aout_data_struct *)bfd_zalloc (abfd, sizeof (struct aout_data_struct )); |
7ed4093a SC |
466 | |
467 | if (rawptr == NULL) { | |
468 | bfd_error = no_memory; | |
469 | return false; | |
470 | } | |
471 | ||
6db82ea7 | 472 | abfd->tdata.aout_data = rawptr; |
7ed4093a SC |
473 | exec_hdr (abfd) = &(rawptr->e); |
474 | ||
475 | /* For simplicity's sake we just make all the sections right here. */ | |
476 | ||
477 | obj_textsec (abfd) = (asection *)NULL; | |
478 | obj_datasec (abfd) = (asection *)NULL; | |
479 | obj_bsssec (abfd) = (asection *)NULL; | |
480 | bfd_make_section (abfd, ".text"); | |
481 | bfd_make_section (abfd, ".data"); | |
482 | bfd_make_section (abfd, ".bss"); | |
6db82ea7 SC |
483 | bfd_make_section (abfd, BFD_ABS_SECTION_NAME); |
484 | bfd_make_section (abfd, BFD_UND_SECTION_NAME); | |
485 | bfd_make_section (abfd, BFD_COM_SECTION_NAME); | |
7ed4093a SC |
486 | |
487 | return true; | |
488 | } | |
489 | ||
6f715d66 | 490 | |
4e41b5aa SC |
491 | /* |
492 | FUNCTION | |
493 | aout_<size>_machine_type | |
6f715d66 | 494 | |
4e41b5aa SC |
495 | DESCRIPTION |
496 | Keep track of machine architecture and machine type for | |
497 | a.out's. Return the machine_type for a particular | |
498 | arch&machine, or M_UNKNOWN if that exact arch&machine can't be | |
499 | represented in a.out format. | |
7ed4093a | 500 | |
4e41b5aa SC |
501 | If the architecture is understood, machine type 0 (default) |
502 | should always be understood. | |
6f715d66 | 503 | |
4e41b5aa SC |
504 | EXAMPLE |
505 | enum machine_type aout_<size>_machine_type | |
6f715d66 SC |
506 | (enum bfd_architecture arch, |
507 | unsigned long machine)); | |
508 | */ | |
7ed4093a SC |
509 | |
510 | enum machine_type | |
511 | DEFUN(NAME(aout,machine_type),(arch, machine), | |
512 | enum bfd_architecture arch AND | |
513 | unsigned long machine) | |
514 | { | |
515 | enum machine_type arch_flags; | |
516 | ||
517 | arch_flags = M_UNKNOWN; | |
518 | ||
519 | switch (arch) { | |
520 | case bfd_arch_sparc: | |
521 | if (machine == 0) arch_flags = M_SPARC; | |
522 | break; | |
523 | ||
524 | case bfd_arch_m68k: | |
525 | switch (machine) { | |
526 | case 0: arch_flags = M_68010; break; | |
527 | case 68000: arch_flags = M_UNKNOWN; break; | |
528 | case 68010: arch_flags = M_68010; break; | |
529 | case 68020: arch_flags = M_68020; break; | |
530 | default: arch_flags = M_UNKNOWN; break; | |
531 | } | |
532 | break; | |
533 | ||
534 | case bfd_arch_i386: | |
535 | if (machine == 0) arch_flags = M_386; | |
536 | break; | |
537 | ||
538 | case bfd_arch_a29k: | |
539 | if (machine == 0) arch_flags = M_29K; | |
540 | break; | |
541 | ||
542 | default: | |
543 | arch_flags = M_UNKNOWN; | |
544 | break; | |
545 | } | |
546 | return arch_flags; | |
547 | } | |
548 | ||
9e2dad8e | 549 | |
4e41b5aa SC |
550 | /* |
551 | FUNCTION | |
552 | aout_<size>_set_arch_mach | |
6f715d66 | 553 | |
4e41b5aa SC |
554 | DESCRIPTION |
555 | Sets the architecture and the machine of the BFD to those | |
556 | values supplied. Verifies that the format can support the | |
557 | architecture required. | |
6f715d66 | 558 | |
4e41b5aa SC |
559 | EXAMPLE |
560 | boolean aout_<size>_set_arch_mach, | |
6f715d66 SC |
561 | (bfd *, |
562 | enum bfd_architecture, | |
563 | unsigned long machine)); | |
564 | */ | |
565 | ||
7ed4093a SC |
566 | boolean |
567 | DEFUN(NAME(aout,set_arch_mach),(abfd, arch, machine), | |
568 | bfd *abfd AND | |
569 | enum bfd_architecture arch AND | |
570 | unsigned long machine) | |
571 | { | |
9e2dad8e | 572 | bfd_default_set_arch_mach(abfd, arch, machine); |
7ed4093a SC |
573 | if (arch != bfd_arch_unknown && |
574 | NAME(aout,machine_type) (arch, machine) == M_UNKNOWN) | |
575 | return false; /* We can't represent this type */ | |
576 | return true; /* We're easy ... */ | |
577 | } | |
7ed4093a | 578 | |
4e41b5aa SC |
579 | /* |
580 | FUNCTION | |
581 | aout_<size>new_section_hook | |
9e2dad8e | 582 | |
4e41b5aa SC |
583 | DESCRIPTION |
584 | Called by the BFD in response to a @code{bfd_make_section} | |
585 | request. | |
586 | ||
587 | EXAMPLE | |
588 | boolean aout_<size>_new_section_hook, | |
9e2dad8e JG |
589 | (bfd *abfd, |
590 | asection *newsect)); | |
6f715d66 | 591 | */ |
7ed4093a | 592 | boolean |
3f7607af | 593 | DEFUN(NAME(aout,new_section_hook),(abfd, newsect), |
9e2dad8e JG |
594 | bfd *abfd AND |
595 | asection *newsect) | |
7ed4093a | 596 | { |
6db82ea7 SC |
597 | /* align to double at least */ |
598 | newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power; | |
3f7607af | 599 | |
7ed4093a | 600 | |
6db82ea7 SC |
601 | if (bfd_get_format (abfd) == bfd_object) |
602 | { | |
603 | if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) { | |
604 | obj_textsec(abfd)= newsect; | |
605 | newsect->target_index = N_TEXT | N_EXT; | |
606 | return true; | |
607 | } | |
7ed4093a | 608 | |
6db82ea7 SC |
609 | if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) { |
610 | obj_datasec(abfd) = newsect; | |
611 | newsect->target_index = N_DATA | N_EXT; | |
612 | return true; | |
613 | } | |
7ed4093a | 614 | |
6db82ea7 SC |
615 | if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) { |
616 | obj_bsssec(abfd) = newsect; | |
617 | newsect->target_index = N_BSS | N_EXT; | |
618 | return true; | |
619 | } | |
620 | ||
621 | } | |
7ed4093a | 622 | |
6db82ea7 SC |
623 | /* We allow more than three sections internally */ |
624 | return true; | |
7ed4093a SC |
625 | } |
626 | ||
627 | boolean | |
9e2dad8e JG |
628 | DEFUN(NAME(aout,set_section_contents),(abfd, section, location, offset, count), |
629 | bfd *abfd AND | |
630 | sec_ptr section AND | |
631 | PTR location AND | |
632 | file_ptr offset AND | |
633 | bfd_size_type count) | |
7ed4093a | 634 | { |
7b02b4ed | 635 | file_ptr text_end; |
7b02b4ed | 636 | bfd_size_type text_size; |
7ed4093a | 637 | if (abfd->output_has_begun == false) |
9e2dad8e JG |
638 | { /* set by bfd.c handler */ |
639 | switch (abfd->direction) | |
640 | { | |
641 | case read_direction: | |
642 | case no_direction: | |
643 | bfd_error = invalid_operation; | |
644 | return false; | |
645 | ||
646 | case both_direction: | |
647 | break; | |
648 | ||
649 | case write_direction: | |
650 | if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL)) | |
651 | { | |
652 | bfd_error = invalid_operation; | |
653 | return false; | |
654 | } | |
6db82ea7 SC |
655 | obj_textsec(abfd)->_raw_size = |
656 | align_power(obj_textsec(abfd)->_raw_size, | |
7b02b4ed | 657 | obj_textsec(abfd)->alignment_power); |
6db82ea7 SC |
658 | |
659 | text_size = obj_textsec (abfd)->_raw_size; | |
7b02b4ed JG |
660 | /* Rule (heuristic) for when to pad to a new page. |
661 | * Note that there are (at least) two ways demand-paged | |
662 | * (ZMAGIC) files have been handled. Most Berkeley-based systems | |
663 | * start the text segment at (PAGE_SIZE). However, newer | |
664 | * versions of SUNOS start the text segment right after the | |
665 | * exec header; the latter is counted in the text segment size, | |
666 | * and is paged in by the kernel with the rest of the text. */ | |
667 | if (!(abfd->flags & D_PAGED)) | |
668 | { /* Not demand-paged. */ | |
6db82ea7 | 669 | obj_textsec(abfd)->filepos = adata(abfd).exec_bytes_size; |
7b02b4ed | 670 | } |
6db82ea7 SC |
671 | else if (obj_textsec(abfd)->vma % adata(abfd).page_size |
672 | < adata(abfd).exec_bytes_size) | |
7b02b4ed | 673 | { /* Old-style demand-paged. */ |
6db82ea7 | 674 | obj_textsec(abfd)->filepos = adata(abfd).page_size; |
7b02b4ed JG |
675 | } |
676 | else | |
677 | { /* Sunos-style demand-paged. */ | |
6db82ea7 SC |
678 | obj_textsec(abfd)->filepos = adata(abfd).exec_bytes_size; |
679 | text_size += adata(abfd).exec_bytes_size; | |
7b02b4ed | 680 | } |
6db82ea7 | 681 | text_end = obj_textsec(abfd)->_raw_size + obj_textsec(abfd)->filepos; |
7b02b4ed JG |
682 | if (abfd->flags & (D_PAGED|WP_TEXT)) |
683 | { | |
684 | bfd_size_type text_pad = | |
55c0061e | 685 | BFD_ALIGN(text_size, adata(abfd).page_size) |
8c4a1ace | 686 | - text_size; |
7b02b4ed | 687 | text_end += text_pad; |
6db82ea7 | 688 | obj_textsec(abfd)->_raw_size += text_pad; |
12e7087f | 689 | } |
7b02b4ed | 690 | obj_datasec(abfd)->filepos = text_end; |
6db82ea7 SC |
691 | obj_datasec(abfd)->_raw_size = |
692 | align_power(obj_datasec(abfd)->_raw_size, | |
7b02b4ed | 693 | obj_datasec(abfd)->alignment_power); |
12e7087f | 694 | } |
9e2dad8e | 695 | } |
12e7087f | 696 | |
7ed4093a SC |
697 | /* regardless, once we know what we're doing, we might as well get going */ |
698 | if (section != obj_bsssec(abfd)) | |
699 | { | |
700 | bfd_seek (abfd, section->filepos + offset, SEEK_SET); | |
9e2dad8e | 701 | |
7ed4093a SC |
702 | if (count) { |
703 | return (bfd_write ((PTR)location, 1, count, abfd) == count) ? | |
704 | true : false; | |
705 | } | |
6db82ea7 | 706 | return true; |
7ed4093a SC |
707 | } |
708 | return true; | |
709 | } | |
710 | \f | |
711 | /* Classify stabs symbols */ | |
712 | ||
713 | #define sym_in_text_section(sym) \ | |
9e2dad8e | 714 | (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT) |
7ed4093a SC |
715 | |
716 | #define sym_in_data_section(sym) \ | |
9e2dad8e | 717 | (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA) |
7ed4093a SC |
718 | |
719 | #define sym_in_bss_section(sym) \ | |
9e2dad8e | 720 | (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS) |
7ed4093a SC |
721 | |
722 | /* Symbol is undefined if type is N_UNDF|N_EXT and if it has | |
9e2dad8e JG |
723 | zero in the "value" field. Nonzeroes there are fortrancommon |
724 | symbols. */ | |
7ed4093a | 725 | #define sym_is_undefined(sym) \ |
9e2dad8e | 726 | ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0) |
7ed4093a SC |
727 | |
728 | /* Symbol is a global definition if N_EXT is on and if it has | |
9e2dad8e | 729 | a nonzero type field. */ |
7ed4093a | 730 | #define sym_is_global_defn(sym) \ |
9e2dad8e | 731 | (((sym)->type & N_EXT) && (sym)->type & N_TYPE) |
7ed4093a SC |
732 | |
733 | /* Symbol is debugger info if any bits outside N_TYPE or N_EXT | |
9e2dad8e | 734 | are on. */ |
7ed4093a | 735 | #define sym_is_debugger_info(sym) \ |
9e2dad8e | 736 | ((sym)->type & ~(N_EXT | N_TYPE)) |
7ed4093a SC |
737 | |
738 | #define sym_is_fortrancommon(sym) \ | |
9e2dad8e | 739 | (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0) |
7ed4093a SC |
740 | |
741 | /* Symbol is absolute if it has N_ABS set */ | |
742 | #define sym_is_absolute(sym) \ | |
9e2dad8e | 743 | (((sym)->type & N_TYPE)== N_ABS) |
7ed4093a SC |
744 | |
745 | ||
746 | #define sym_is_indirect(sym) \ | |
9e2dad8e | 747 | (((sym)->type & N_ABS)== N_ABS) |
7ed4093a SC |
748 | |
749 | /* Only in their own functions for ease of debugging; when sym flags have | |
9e2dad8e JG |
750 | stabilised these should be inlined into their (single) caller */ |
751 | ||
7ed4093a SC |
752 | static void |
753 | DEFUN(translate_from_native_sym_flags,(sym_pointer, cache_ptr, abfd), | |
4e41b5aa SC |
754 | struct external_nlist *sym_pointer AND |
755 | aout_symbol_type *cache_ptr AND | |
756 | bfd *abfd) | |
9e2dad8e | 757 | { |
6db82ea7 SC |
758 | switch (cache_ptr->type & N_TYPE) |
759 | { | |
9e2dad8e JG |
760 | case N_SETA: |
761 | case N_SETT: | |
762 | case N_SETD: | |
763 | case N_SETB: | |
6db82ea7 SC |
764 | { |
765 | char *copy = bfd_alloc(abfd, strlen(cache_ptr->symbol.name)+1); | |
766 | asection *section ; | |
767 | asection *into_section; | |
768 | ||
769 | arelent_chain *reloc = (arelent_chain *)bfd_alloc(abfd, sizeof(arelent_chain)); | |
770 | strcpy(copy, cache_ptr->symbol.name); | |
771 | ||
772 | /* Make sure that this bfd has a section with the right contructor | |
773 | name */ | |
774 | section = bfd_get_section_by_name (abfd, copy); | |
775 | if (!section) | |
776 | section = bfd_make_section(abfd,copy); | |
777 | ||
778 | /* Build a relocation entry for the constructor */ | |
779 | switch ( (cache_ptr->type & N_TYPE) ) | |
780 | { | |
781 | case N_SETA: | |
782 | into_section = &bfd_abs_section; | |
783 | break; | |
784 | case N_SETT: | |
785 | into_section = (asection *)obj_textsec(abfd); | |
786 | break; | |
787 | case N_SETD: | |
788 | into_section = (asection *)obj_datasec(abfd); | |
789 | break; | |
790 | case N_SETB: | |
791 | into_section = (asection *)obj_bsssec(abfd); | |
792 | break; | |
793 | default: | |
794 | abort(); | |
795 | } | |
796 | ||
797 | /* Build a relocation pointing into the constuctor section | |
798 | pointing at the symbol in the set vector specified */ | |
799 | ||
800 | reloc->relent.addend = cache_ptr->symbol.value; | |
801 | cache_ptr->symbol.section = into_section->symbol->section; | |
802 | reloc->relent.sym_ptr_ptr = into_section->symbol_ptr_ptr; | |
803 | ||
c618de01 | 804 | |
6db82ea7 SC |
805 | /* We modify the symbol to belong to a section depending upon the |
806 | name of the symbol - probably __CTOR__ or __DTOR__ but we don't | |
807 | really care, and add to the size of the section to contain a | |
808 | pointer to the symbol. Build a reloc entry to relocate to this | |
809 | symbol attached to this section. */ | |
c618de01 | 810 | |
6db82ea7 SC |
811 | section->flags = SEC_CONSTRUCTOR; |
812 | ||
c618de01 | 813 | |
6db82ea7 SC |
814 | section->reloc_count++; |
815 | section->alignment_power = 2; | |
816 | ||
817 | reloc->next = section->constructor_chain; | |
818 | section->constructor_chain = reloc; | |
819 | reloc->relent.address = section->_raw_size; | |
820 | section->_raw_size += sizeof(int *); | |
821 | ||
822 | reloc->relent.howto = howto_table_ext + CTOR_TABLE_RELOC_IDX; | |
823 | cache_ptr->symbol.flags |= BSF_DEBUGGING | BSF_CONSTRUCTOR; | |
824 | } | |
9e2dad8e | 825 | break; |
7ed4093a | 826 | default: |
88dfcd68 | 827 | if (cache_ptr->type == N_WARNING) |
6db82ea7 SC |
828 | { |
829 | /* This symbol is the text of a warning message, the next symbol | |
830 | is the symbol to associate the warning with */ | |
831 | cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING; | |
88dfcd68 | 832 | cache_ptr->symbol.value = (bfd_vma)((cache_ptr+1)); |
6db82ea7 SC |
833 | /* We furgle with the next symbol in place. We don't want it to be undefined, we'll trample the type */ |
834 | (sym_pointer+1)->e_type[0] = 0xff; | |
88dfcd68 SC |
835 | break; |
836 | } | |
6db82ea7 SC |
837 | if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT)) { |
838 | /* Two symbols in a row for an INDR message. The first symbol | |
839 | contains the name we will match, the second symbol contains the | |
840 | name the first name is translated into. It is supplied to us | |
841 | undefined. This is good, since we want to pull in any files which | |
842 | define it */ | |
843 | cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT; | |
844 | cache_ptr->symbol.value = (bfd_vma)((cache_ptr+1)); | |
845 | cache_ptr->symbol.section = &bfd_und_section; | |
846 | break; | |
847 | } | |
88dfcd68 SC |
848 | |
849 | ||
7ed4093a | 850 | if (sym_is_debugger_info (cache_ptr)) { |
6db82ea7 SC |
851 | cache_ptr->symbol.flags = BSF_DEBUGGING ; |
852 | /* Work out the section correct for this symbol */ | |
853 | switch (cache_ptr->type & N_TYPE) | |
854 | { | |
855 | case N_TEXT: | |
856 | case N_FN: | |
857 | cache_ptr->symbol.section = obj_textsec (abfd); | |
858 | cache_ptr->symbol.value -= obj_textsec(abfd)->vma; | |
859 | break; | |
860 | case N_DATA: | |
861 | cache_ptr->symbol.value -= obj_datasec(abfd)->vma; | |
862 | cache_ptr->symbol.section = obj_datasec (abfd); | |
863 | break; | |
864 | case N_BSS : | |
865 | cache_ptr->symbol.section = obj_bsssec (abfd); | |
866 | cache_ptr->symbol.value -= obj_bsssec(abfd)->vma; | |
867 | break; | |
868 | default: | |
869 | case N_ABS: | |
88dfcd68 | 870 | |
6db82ea7 SC |
871 | cache_ptr->symbol.section = &bfd_abs_section; |
872 | break; | |
7ed4093a | 873 | } |
6db82ea7 SC |
874 | } |
875 | else { | |
876 | ||
877 | if (sym_is_fortrancommon (cache_ptr)) | |
878 | { | |
879 | cache_ptr->symbol.flags = 0; | |
880 | cache_ptr->symbol.section = &bfd_com_section; | |
7ed4093a SC |
881 | } |
882 | else { | |
6db82ea7 SC |
883 | |
884 | ||
885 | } | |
88dfcd68 | 886 | |
7ed4093a SC |
887 | /* In a.out, the value of a symbol is always relative to the |
888 | * start of the file, if this is a data symbol we'll subtract | |
889 | * the size of the text section to get the section relative | |
890 | * value. If this is a bss symbol (which would be strange) | |
891 | * we'll subtract the size of the previous two sections | |
892 | * to find the section relative address. | |
893 | */ | |
88dfcd68 | 894 | |
7ed4093a | 895 | if (sym_in_text_section (cache_ptr)) { |
6db82ea7 SC |
896 | cache_ptr->symbol.value -= obj_textsec(abfd)->vma; |
897 | cache_ptr->symbol.section = obj_textsec (abfd); | |
898 | } | |
7ed4093a | 899 | else if (sym_in_data_section (cache_ptr)){ |
6db82ea7 SC |
900 | cache_ptr->symbol.value -= obj_datasec(abfd)->vma; |
901 | cache_ptr->symbol.section = obj_datasec (abfd); | |
902 | } | |
7ed4093a | 903 | else if (sym_in_bss_section(cache_ptr)) { |
6db82ea7 SC |
904 | cache_ptr->symbol.section = obj_bsssec (abfd); |
905 | cache_ptr->symbol.value -= obj_bsssec(abfd)->vma; | |
906 | } | |
907 | else if (sym_is_undefined (cache_ptr)) { | |
908 | cache_ptr->symbol.flags = 0; | |
909 | cache_ptr->symbol.section = &bfd_und_section; | |
910 | } | |
911 | else if (sym_is_absolute(cache_ptr)) | |
912 | { | |
913 | cache_ptr->symbol.section = &bfd_abs_section; | |
7ed4093a | 914 | } |
6db82ea7 SC |
915 | |
916 | if (sym_is_global_defn (cache_ptr)) | |
917 | { | |
918 | cache_ptr->symbol.flags = BSF_GLOBAL | BSF_EXPORT; | |
919 | } | |
920 | else | |
921 | { | |
922 | cache_ptr->symbol.flags = BSF_LOCAL; | |
7ed4093a SC |
923 | } |
924 | } | |
7ed4093a SC |
925 | } |
926 | } | |
927 | ||
6db82ea7 SC |
928 | |
929 | ||
7ed4093a SC |
930 | static void |
931 | DEFUN(translate_to_native_sym_flags,(sym_pointer, cache_ptr, abfd), | |
932 | struct external_nlist *sym_pointer AND | |
933 | asymbol *cache_ptr AND | |
934 | bfd *abfd) | |
935 | { | |
936 | bfd_vma value = cache_ptr->value; | |
937 | ||
10dea9ed DHW |
938 | /* mask out any existing type bits in case copying from one section |
939 | to another */ | |
940 | sym_pointer->e_type[0] &= ~N_TYPE; | |
941 | ||
6db82ea7 | 942 | if (bfd_get_output_section(cache_ptr) == obj_bsssec (abfd)) { |
7ed4093a SC |
943 | sym_pointer->e_type[0] |= N_BSS; |
944 | } | |
6db82ea7 | 945 | else if (bfd_get_output_section(cache_ptr) == obj_datasec (abfd)) { |
7ed4093a SC |
946 | sym_pointer->e_type[0] |= N_DATA; |
947 | } | |
6db82ea7 | 948 | else if (bfd_get_output_section(cache_ptr) == obj_textsec (abfd)) { |
7ed4093a SC |
949 | sym_pointer->e_type[0] |= N_TEXT; |
950 | } | |
6db82ea7 SC |
951 | else if (bfd_get_output_section(cache_ptr) == &bfd_abs_section) |
952 | { | |
7ed4093a SC |
953 | sym_pointer->e_type[0] |= N_ABS; |
954 | } | |
6db82ea7 SC |
955 | else if (bfd_get_output_section(cache_ptr) == &bfd_und_section) |
956 | { | |
7ed4093a SC |
957 | sym_pointer->e_type[0] = (N_UNDF | N_EXT); |
958 | } | |
6db82ea7 SC |
959 | else if (bfd_get_output_section(cache_ptr) == &bfd_com_section) { |
960 | sym_pointer->e_type[0] = (N_UNDF | N_EXT); | |
961 | } | |
962 | else { | |
e7b4046c SC |
963 | if (cache_ptr->section->output_section) |
964 | { | |
965 | ||
966 | bfd_error_vector.nonrepresentable_section(abfd, | |
967 | bfd_get_output_section(cache_ptr)->name); | |
968 | } | |
969 | else | |
970 | { | |
971 | bfd_error_vector.nonrepresentable_section(abfd, | |
972 | cache_ptr->section->name); | |
973 | ||
974 | } | |
975 | ||
7ed4093a | 976 | } |
6db82ea7 | 977 | /* Turn the symbol from section relative to absolute again */ |
7ed4093a | 978 | |
6db82ea7 SC |
979 | value += cache_ptr->section->output_section->vma + cache_ptr->section->output_offset ; |
980 | ||
981 | ||
982 | if (cache_ptr->flags & (BSF_WARNING)) { | |
983 | (sym_pointer+1)->e_type[0] = 1; | |
984 | } | |
985 | ||
986 | if (cache_ptr->flags & (BSF_GLOBAL | BSF_EXPORT)) { | |
7ed4093a SC |
987 | sym_pointer->e_type[0] |= N_EXT; |
988 | } | |
6db82ea7 | 989 | if (cache_ptr->flags & BSF_DEBUGGING) { |
7ed4093a SC |
990 | sym_pointer->e_type [0]= ((aout_symbol_type *)cache_ptr)->type; |
991 | } | |
6db82ea7 | 992 | |
7ed4093a SC |
993 | PUT_WORD(abfd, value, sym_pointer->e_value); |
994 | } | |
995 | \f | |
996 | /* Native-level interface to symbols. */ | |
997 | ||
998 | /* We read the symbols into a buffer, which is discarded when this | |
999 | function exits. We read the strings into a buffer large enough to | |
1000 | hold them all plus all the cached symbol entries. */ | |
1001 | ||
1002 | asymbol * | |
1003 | DEFUN(NAME(aout,make_empty_symbol),(abfd), | |
1004 | bfd *abfd) | |
9e2dad8e JG |
1005 | { |
1006 | aout_symbol_type *new = | |
1007 | (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type)); | |
1008 | new->symbol.the_bfd = abfd; | |
7ed4093a | 1009 | |
9e2dad8e JG |
1010 | return &new->symbol; |
1011 | } | |
7ed4093a SC |
1012 | |
1013 | boolean | |
1014 | DEFUN(NAME(aout,slurp_symbol_table),(abfd), | |
1015 | bfd *abfd) | |
9e2dad8e JG |
1016 | { |
1017 | bfd_size_type symbol_size; | |
1018 | bfd_size_type string_size; | |
1019 | unsigned char string_chars[BYTES_IN_WORD]; | |
1020 | struct external_nlist *syms; | |
1021 | char *strings; | |
1022 | aout_symbol_type *cached; | |
7ed4093a | 1023 | |
9e2dad8e JG |
1024 | /* If there's no work to be done, don't do any */ |
1025 | if (obj_aout_symbols (abfd) != (aout_symbol_type *)NULL) return true; | |
1026 | symbol_size = exec_hdr(abfd)->a_syms; | |
1027 | if (symbol_size == 0) { | |
1028 | bfd_error = no_symbols; | |
1029 | return false; | |
1030 | } | |
7ed4093a | 1031 | |
9e2dad8e JG |
1032 | bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET); |
1033 | if (bfd_read ((PTR)string_chars, BYTES_IN_WORD, 1, abfd) != BYTES_IN_WORD) | |
1034 | return false; | |
1035 | string_size = GET_WORD (abfd, string_chars); | |
7ed4093a | 1036 | |
9e2dad8e JG |
1037 | strings =(char *) bfd_alloc(abfd, string_size + 1); |
1038 | cached = (aout_symbol_type *) | |
1039 | bfd_zalloc(abfd, (bfd_size_type)(bfd_get_symcount (abfd) * sizeof(aout_symbol_type))); | |
1040 | ||
1041 | /* malloc this, so we can free it if simply. The symbol caching | |
1042 | might want to allocate onto the bfd's obstack */ | |
98d43107 | 1043 | syms = (struct external_nlist *) bfd_xmalloc(symbol_size); |
9e2dad8e JG |
1044 | bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET); |
1045 | if (bfd_read ((PTR)syms, 1, symbol_size, abfd) != symbol_size) { | |
1046 | bailout: | |
1047 | if (syms) free (syms); | |
1048 | if (cached) bfd_release (abfd, cached); | |
1049 | if (strings)bfd_release (abfd, strings); | |
1050 | return false; | |
1051 | } | |
7ed4093a | 1052 | |
9e2dad8e JG |
1053 | bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET); |
1054 | if (bfd_read ((PTR)strings, 1, string_size, abfd) != string_size) { | |
1055 | goto bailout; | |
1056 | } | |
7ed4093a | 1057 | |
9e2dad8e JG |
1058 | /* OK, now walk the new symtable, cacheing symbol properties */ |
1059 | { | |
1060 | register struct external_nlist *sym_pointer; | |
1061 | register struct external_nlist *sym_end = syms + bfd_get_symcount (abfd); | |
1062 | register aout_symbol_type *cache_ptr = cached; | |
7ed4093a | 1063 | |
9e2dad8e JG |
1064 | /* Run through table and copy values */ |
1065 | for (sym_pointer = syms, cache_ptr = cached; | |
1066 | sym_pointer < sym_end; sym_pointer++, cache_ptr++) | |
1067 | { | |
1068 | bfd_vma x = GET_WORD(abfd, sym_pointer->e_strx); | |
1069 | cache_ptr->symbol.the_bfd = abfd; | |
1070 | if (x) | |
1071 | cache_ptr->symbol.name = x + strings; | |
1072 | else | |
1073 | cache_ptr->symbol.name = (char *)NULL; | |
7ed4093a | 1074 | |
9e2dad8e JG |
1075 | cache_ptr->symbol.value = GET_SWORD(abfd, sym_pointer->e_value); |
1076 | cache_ptr->desc = bfd_get_16(abfd, sym_pointer->e_desc); | |
1077 | cache_ptr->other =bfd_get_8(abfd, sym_pointer->e_other); | |
1078 | cache_ptr->type = bfd_get_8(abfd, sym_pointer->e_type); | |
1079 | cache_ptr->symbol.udata = 0; | |
1080 | translate_from_native_sym_flags (sym_pointer, cache_ptr, abfd); | |
1081 | } | |
1082 | } | |
7ed4093a | 1083 | |
9e2dad8e JG |
1084 | obj_aout_symbols (abfd) = cached; |
1085 | free((PTR)syms); | |
7ed4093a | 1086 | |
9e2dad8e JG |
1087 | return true; |
1088 | } | |
7ed4093a SC |
1089 | |
1090 | ||
1091 | void | |
1092 | DEFUN(NAME(aout,write_syms),(abfd), | |
1093 | bfd *abfd) | |
1094 | { | |
1095 | unsigned int count ; | |
1096 | asymbol **generic = bfd_get_outsymbols (abfd); | |
1097 | ||
1098 | bfd_size_type stindex = BYTES_IN_WORD; /* initial string length */ | |
1099 | ||
1100 | for (count = 0; count < bfd_get_symcount (abfd); count++) { | |
1101 | asymbol *g = generic[count]; | |
1102 | struct external_nlist nsp; | |
6db82ea7 SC |
1103 | |
1104 | ||
7ed4093a SC |
1105 | if (g->name) { |
1106 | unsigned int length = strlen(g->name) +1; | |
1107 | PUT_WORD (abfd, stindex, (unsigned char *)nsp.e_strx); | |
1108 | stindex += length; | |
1109 | } | |
6db82ea7 SC |
1110 | else |
1111 | ||
1112 | { | |
7ed4093a SC |
1113 | PUT_WORD (abfd, 0, (unsigned char *)nsp.e_strx); |
1114 | } | |
1115 | ||
1116 | if (g->the_bfd->xvec->flavour == abfd->xvec->flavour) | |
1117 | { | |
1118 | bfd_h_put_16(abfd, aout_symbol(g)->desc, nsp.e_desc); | |
1119 | bfd_h_put_8(abfd, aout_symbol(g)->other, nsp.e_other); | |
1120 | bfd_h_put_8(abfd, aout_symbol(g)->type, nsp.e_type); | |
1121 | } | |
1122 | else | |
1123 | { | |
1124 | bfd_h_put_16(abfd,0, nsp.e_desc); | |
1125 | bfd_h_put_8(abfd, 0, nsp.e_other); | |
1126 | bfd_h_put_8(abfd, 0, nsp.e_type); | |
1127 | } | |
7b02b4ed | 1128 | |
7d003262 | 1129 | translate_to_native_sym_flags (&nsp, g, abfd); |
7b02b4ed JG |
1130 | |
1131 | bfd_write((PTR)&nsp,1,EXTERNAL_NLIST_SIZE, abfd); | |
7ed4093a SC |
1132 | } |
1133 | ||
7ed4093a | 1134 | /* Now output the strings. Be sure to put string length into correct |
7b02b4ed | 1135 | byte ordering before writing it. */ |
7ed4093a SC |
1136 | { |
1137 | char buffer[BYTES_IN_WORD]; | |
1138 | PUT_WORD (abfd, stindex, (unsigned char *)buffer); | |
1139 | ||
1140 | bfd_write((PTR)buffer, 1, BYTES_IN_WORD, abfd); | |
1141 | } | |
1142 | generic = bfd_get_outsymbols(abfd); | |
1143 | for (count = 0; count < bfd_get_symcount(abfd); count++) | |
1144 | { | |
1145 | asymbol *g = *(generic++); | |
1146 | ||
1147 | if (g->name) | |
1148 | { | |
1149 | size_t length = strlen(g->name)+1; | |
1150 | bfd_write((PTR)g->name, 1, length, abfd); | |
1151 | } | |
c618de01 | 1152 | g->KEEPIT = (KEEPITTYPE) count; |
7ed4093a SC |
1153 | } |
1154 | } | |
1155 | ||
1156 | ||
1157 | ||
1158 | unsigned int | |
1159 | DEFUN(NAME(aout,get_symtab),(abfd, location), | |
1160 | bfd *abfd AND | |
1161 | asymbol **location) | |
3f7607af | 1162 | { |
7ed4093a SC |
1163 | unsigned int counter = 0; |
1164 | aout_symbol_type *symbase; | |
1165 | ||
1166 | if (!NAME(aout,slurp_symbol_table)(abfd)) return 0; | |
1167 | ||
1168 | for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);) | |
1169 | *(location++) = (asymbol *)( symbase++); | |
1170 | *location++ =0; | |
1171 | return bfd_get_symcount(abfd); | |
3f7607af | 1172 | } |
7ed4093a SC |
1173 | |
1174 | \f | |
1175 | /* Standard reloc stuff */ | |
1176 | /* Output standard relocation information to a file in target byte order. */ | |
1177 | ||
1178 | void | |
1179 | DEFUN(NAME(aout,swap_std_reloc_out),(abfd, g, natptr), | |
1180 | bfd *abfd AND | |
1181 | arelent *g AND | |
1182 | struct reloc_std_external *natptr) | |
3f7607af | 1183 | { |
6db82ea7 SC |
1184 | int r_index; |
1185 | asymbol *sym = *(g->sym_ptr_ptr); | |
1186 | int r_extern; | |
1187 | unsigned int r_length; | |
1188 | int r_pcrel; | |
1189 | int r_baserel, r_jmptable, r_relative; | |
1190 | unsigned int r_addend; | |
1191 | asection *output_section = sym->section->output_section; | |
1192 | ||
1193 | PUT_WORD(abfd, g->address, natptr->r_address); | |
7ed4093a | 1194 | |
6db82ea7 SC |
1195 | r_length = g->howto->size ; /* Size as a power of two */ |
1196 | r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */ | |
1197 | /* r_baserel, r_jmptable, r_relative??? FIXME-soon */ | |
1198 | r_baserel = 0; | |
1199 | r_jmptable = 0; | |
1200 | r_relative = 0; | |
7ed4093a | 1201 | |
6db82ea7 | 1202 | r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma; |
7ed4093a | 1203 | |
6db82ea7 SC |
1204 | /* name was clobbered by aout_write_syms to be symbol index */ |
1205 | ||
10dea9ed DHW |
1206 | if (output_section == &bfd_com_section |
1207 | || output_section == &bfd_abs_section | |
1208 | || output_section == &bfd_und_section) | |
6db82ea7 SC |
1209 | { |
1210 | /* Fill in symbol */ | |
1211 | r_extern = 1; | |
1212 | r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT); | |
1213 | } | |
1214 | else | |
1215 | { | |
1216 | /* Just an ordinary section */ | |
1217 | r_extern = 0; | |
1218 | r_index = output_section->target_index; | |
1219 | } | |
7ed4093a | 1220 | |
6db82ea7 SC |
1221 | /* now the fun stuff */ |
1222 | if (abfd->xvec->header_byteorder_big_p != false) { | |
7ed4093a SC |
1223 | natptr->r_index[0] = r_index >> 16; |
1224 | natptr->r_index[1] = r_index >> 8; | |
1225 | natptr->r_index[2] = r_index; | |
1226 | natptr->r_type[0] = | |
6db82ea7 SC |
1227 | (r_extern? RELOC_STD_BITS_EXTERN_BIG: 0) |
1228 | | (r_pcrel? RELOC_STD_BITS_PCREL_BIG: 0) | |
1229 | | (r_baserel? RELOC_STD_BITS_BASEREL_BIG: 0) | |
1230 | | (r_jmptable? RELOC_STD_BITS_JMPTABLE_BIG: 0) | |
1231 | | (r_relative? RELOC_STD_BITS_RELATIVE_BIG: 0) | |
1232 | | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG); | |
7ed4093a | 1233 | } else { |
6db82ea7 SC |
1234 | natptr->r_index[2] = r_index >> 16; |
1235 | natptr->r_index[1] = r_index >> 8; | |
1236 | natptr->r_index[0] = r_index; | |
1237 | natptr->r_type[0] = | |
1238 | (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0) | |
7ed4093a | 1239 | | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0) |
6db82ea7 SC |
1240 | | (r_baserel? RELOC_STD_BITS_BASEREL_LITTLE: 0) |
1241 | | (r_jmptable? RELOC_STD_BITS_JMPTABLE_LITTLE: 0) | |
1242 | | (r_relative? RELOC_STD_BITS_RELATIVE_LITTLE: 0) | |
1243 | | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE); | |
1244 | } | |
3f7607af | 1245 | } |
7ed4093a SC |
1246 | |
1247 | ||
1248 | /* Extended stuff */ | |
1249 | /* Output extended relocation information to a file in target byte order. */ | |
1250 | ||
1251 | void | |
1252 | DEFUN(NAME(aout,swap_ext_reloc_out),(abfd, g, natptr), | |
1253 | bfd *abfd AND | |
1254 | arelent *g AND | |
1255 | register struct reloc_ext_external *natptr) | |
3f7607af | 1256 | { |
6db82ea7 SC |
1257 | int r_index; |
1258 | int r_extern; | |
1259 | unsigned int r_type; | |
1260 | unsigned int r_addend; | |
1261 | asymbol *sym = *(g->sym_ptr_ptr); | |
1262 | asection *output_section = sym->section->output_section; | |
1263 | ||
1264 | PUT_WORD (abfd, g->address, natptr->r_address); | |
7ed4093a | 1265 | |
6db82ea7 | 1266 | r_type = (unsigned int) g->howto->type; |
7ed4093a | 1267 | |
6db82ea7 | 1268 | r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma; |
7ed4093a | 1269 | |
7ed4093a | 1270 | |
10dea9ed DHW |
1271 | if (output_section == &bfd_com_section |
1272 | || output_section == &bfd_abs_section | |
1273 | || output_section == &bfd_und_section) | |
6db82ea7 SC |
1274 | { |
1275 | /* Fill in symbol */ | |
1276 | r_extern = 1; | |
1277 | r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT); | |
1278 | } | |
1279 | else | |
1280 | { | |
1281 | /* Just an ordinary section */ | |
1282 | r_extern = 0; | |
1283 | r_index = output_section->target_index; | |
1284 | } | |
1285 | ||
1286 | ||
7ed4093a SC |
1287 | /* now the fun stuff */ |
1288 | if (abfd->xvec->header_byteorder_big_p != false) { | |
6db82ea7 SC |
1289 | natptr->r_index[0] = r_index >> 16; |
1290 | natptr->r_index[1] = r_index >> 8; | |
1291 | natptr->r_index[2] = r_index; | |
1292 | natptr->r_type[0] = | |
1293 | (r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0) | |
7ed4093a | 1294 | | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG); |
6db82ea7 SC |
1295 | } else { |
1296 | natptr->r_index[2] = r_index >> 16; | |
1297 | natptr->r_index[1] = r_index >> 8; | |
1298 | natptr->r_index[0] = r_index; | |
1299 | natptr->r_type[0] = | |
1300 | (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0) | |
1301 | | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE); | |
1302 | } | |
7ed4093a SC |
1303 | |
1304 | PUT_WORD (abfd, r_addend, natptr->r_addend); | |
1305 | } | |
1306 | ||
6db82ea7 SC |
1307 | /* BFD deals internally with all things based from the section they're |
1308 | in. so, something in 10 bytes into a text section with a base of | |
1309 | 50 would have a symbol (.text+10) and know .text vma was 50. | |
1310 | ||
1311 | Aout keeps all it's symbols based from zero, so the symbol would | |
1312 | contain 60. This macro subs the base of each section from the value | |
1313 | to give the true offset from the section */ | |
1314 | ||
1315 | ||
7ed4093a SC |
1316 | #define MOVE_ADDRESS(ad) \ |
1317 | if (r_extern) { \ | |
6db82ea7 SC |
1318 | /* undefined symbol */ \ |
1319 | cache_ptr->sym_ptr_ptr = symbols + r_index; \ | |
1320 | cache_ptr->addend = ad; \ | |
1321 | } else { \ | |
1322 | /* defined, section relative. replace symbol with pointer to \ | |
1323 | symbol which points to section */ \ | |
7ed4093a SC |
1324 | switch (r_index) { \ |
1325 | case N_TEXT: \ | |
1326 | case N_TEXT | N_EXT: \ | |
6db82ea7 | 1327 | cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \ |
7ed4093a SC |
1328 | cache_ptr->addend = ad - su->textsec->vma; \ |
1329 | break; \ | |
1330 | case N_DATA: \ | |
1331 | case N_DATA | N_EXT: \ | |
6db82ea7 | 1332 | cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \ |
7ed4093a SC |
1333 | cache_ptr->addend = ad - su->datasec->vma; \ |
1334 | break; \ | |
1335 | case N_BSS: \ | |
1336 | case N_BSS | N_EXT: \ | |
6db82ea7 | 1337 | cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \ |
7ed4093a SC |
1338 | cache_ptr->addend = ad - su->bsssec->vma; \ |
1339 | break; \ | |
6db82ea7 | 1340 | default: \ |
7ed4093a SC |
1341 | case N_ABS: \ |
1342 | case N_ABS | N_EXT: \ | |
6db82ea7 SC |
1343 | cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \ |
1344 | cache_ptr->addend = ad; \ | |
7ed4093a SC |
1345 | break; \ |
1346 | } \ | |
1347 | } \ | |
1348 | ||
1349 | void | |
1350 | DEFUN(NAME(aout,swap_ext_reloc_in), (abfd, bytes, cache_ptr, symbols), | |
1351 | bfd *abfd AND | |
1352 | struct reloc_ext_external *bytes AND | |
1353 | arelent *cache_ptr AND | |
1354 | asymbol **symbols) | |
1355 | { | |
1356 | int r_index; | |
1357 | int r_extern; | |
1358 | unsigned int r_type; | |
6db82ea7 | 1359 | struct aoutdata *su = &(abfd->tdata.aout_data->a); |
7ed4093a SC |
1360 | |
1361 | cache_ptr->address = (GET_SWORD (abfd, bytes->r_address)); | |
1362 | ||
1363 | /* now the fun stuff */ | |
1364 | if (abfd->xvec->header_byteorder_big_p != false) { | |
1365 | r_index = (bytes->r_index[0] << 16) | |
1366 | | (bytes->r_index[1] << 8) | |
1367 | | bytes->r_index[2]; | |
1368 | r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG)); | |
1369 | r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG) | |
1370 | >> RELOC_EXT_BITS_TYPE_SH_BIG; | |
1371 | } else { | |
1372 | r_index = (bytes->r_index[2] << 16) | |
1373 | | (bytes->r_index[1] << 8) | |
1374 | | bytes->r_index[0]; | |
1375 | r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE)); | |
1376 | r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE) | |
1377 | >> RELOC_EXT_BITS_TYPE_SH_LITTLE; | |
1378 | } | |
1379 | ||
1380 | cache_ptr->howto = howto_table_ext + r_type; | |
6db82ea7 | 1381 | MOVE_ADDRESS(GET_SWORD(abfd, bytes->r_addend)); |
7ed4093a SC |
1382 | } |
1383 | ||
1384 | void | |
1385 | DEFUN(NAME(aout,swap_std_reloc_in), (abfd, bytes, cache_ptr, symbols), | |
1386 | bfd *abfd AND | |
1387 | struct reloc_std_external *bytes AND | |
1388 | arelent *cache_ptr AND | |
1389 | asymbol **symbols) | |
1390 | { | |
1391 | int r_index; | |
1392 | int r_extern; | |
1393 | unsigned int r_length; | |
1394 | int r_pcrel; | |
1395 | int r_baserel, r_jmptable, r_relative; | |
6db82ea7 | 1396 | struct aoutdata *su = &(abfd->tdata.aout_data->a); |
7ed4093a SC |
1397 | |
1398 | cache_ptr->address = (int32_type)(bfd_h_get_32 (abfd, bytes->r_address)); | |
1399 | ||
1400 | /* now the fun stuff */ | |
1401 | if (abfd->xvec->header_byteorder_big_p != false) { | |
1402 | r_index = (bytes->r_index[0] << 16) | |
1403 | | (bytes->r_index[1] << 8) | |
1404 | | bytes->r_index[2]; | |
1405 | r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG)); | |
1406 | r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG)); | |
1407 | r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG)); | |
1408 | r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG)); | |
1409 | r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG)); | |
1410 | r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG) | |
1411 | >> RELOC_STD_BITS_LENGTH_SH_BIG; | |
1412 | } else { | |
1413 | r_index = (bytes->r_index[2] << 16) | |
1414 | | (bytes->r_index[1] << 8) | |
1415 | | bytes->r_index[0]; | |
1416 | r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE)); | |
1417 | r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE)); | |
1418 | r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE)); | |
1419 | r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE)); | |
1420 | r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE)); | |
1421 | r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE) | |
1422 | >> RELOC_STD_BITS_LENGTH_SH_LITTLE; | |
1423 | } | |
1424 | ||
1425 | cache_ptr->howto = howto_table_std + r_length + 4 * r_pcrel; | |
1426 | /* FIXME-soon: Roll baserel, jmptable, relative bits into howto setting */ | |
1427 | ||
1428 | MOVE_ADDRESS(0); | |
1429 | } | |
1430 | ||
1431 | /* Reloc hackery */ | |
1432 | ||
1433 | boolean | |
1434 | DEFUN(NAME(aout,slurp_reloc_table),(abfd, asect, symbols), | |
1435 | bfd *abfd AND | |
1436 | sec_ptr asect AND | |
1437 | asymbol **symbols) | |
1438 | { | |
1439 | unsigned int count; | |
1440 | bfd_size_type reloc_size; | |
1441 | PTR relocs; | |
1442 | arelent *reloc_cache; | |
1443 | size_t each_size; | |
1444 | ||
1445 | if (asect->relocation) return true; | |
1446 | ||
1447 | if (asect->flags & SEC_CONSTRUCTOR) return true; | |
1448 | ||
1449 | if (asect == obj_datasec (abfd)) { | |
1450 | reloc_size = exec_hdr(abfd)->a_drsize; | |
1451 | goto doit; | |
1452 | } | |
1453 | ||
1454 | if (asect == obj_textsec (abfd)) { | |
1455 | reloc_size = exec_hdr(abfd)->a_trsize; | |
1456 | goto doit; | |
1457 | } | |
1458 | ||
1459 | bfd_error = invalid_operation; | |
1460 | return false; | |
1461 | ||
1462 | doit: | |
1463 | bfd_seek (abfd, asect->rel_filepos, SEEK_SET); | |
1464 | each_size = obj_reloc_entry_size (abfd); | |
1465 | ||
1466 | count = reloc_size / each_size; | |
1467 | ||
1468 | ||
1469 | reloc_cache = (arelent *) bfd_zalloc (abfd, (size_t)(count * sizeof | |
1470 | (arelent))); | |
1471 | if (!reloc_cache) { | |
1472 | nomem: | |
1473 | bfd_error = no_memory; | |
1474 | return false; | |
1475 | } | |
1476 | ||
1477 | relocs = (PTR) bfd_alloc (abfd, reloc_size); | |
1478 | if (!relocs) { | |
1479 | bfd_release (abfd, reloc_cache); | |
1480 | goto nomem; | |
1481 | } | |
1482 | ||
1483 | if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size) { | |
1484 | bfd_release (abfd, relocs); | |
1485 | bfd_release (abfd, reloc_cache); | |
1486 | bfd_error = system_call_error; | |
1487 | return false; | |
1488 | } | |
1489 | ||
1490 | if (each_size == RELOC_EXT_SIZE) { | |
1491 | register struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs; | |
1492 | unsigned int counter = 0; | |
1493 | arelent *cache_ptr = reloc_cache; | |
1494 | ||
1495 | for (; counter < count; counter++, rptr++, cache_ptr++) { | |
1496 | NAME(aout,swap_ext_reloc_in)(abfd, rptr, cache_ptr, symbols); | |
1497 | } | |
1498 | } else { | |
1499 | register struct reloc_std_external *rptr = (struct reloc_std_external*) relocs; | |
1500 | unsigned int counter = 0; | |
1501 | arelent *cache_ptr = reloc_cache; | |
1502 | ||
1503 | for (; counter < count; counter++, rptr++, cache_ptr++) { | |
1504 | NAME(aout,swap_std_reloc_in)(abfd, rptr, cache_ptr, symbols); | |
1505 | } | |
1506 | ||
1507 | } | |
1508 | ||
1509 | bfd_release (abfd,relocs); | |
1510 | asect->relocation = reloc_cache; | |
1511 | asect->reloc_count = count; | |
1512 | return true; | |
1513 | } | |
1514 | ||
1515 | ||
1516 | ||
1517 | /* Write out a relocation section into an object file. */ | |
1518 | ||
1519 | boolean | |
1520 | DEFUN(NAME(aout,squirt_out_relocs),(abfd, section), | |
1521 | bfd *abfd AND | |
1522 | asection *section) | |
1523 | { | |
1524 | arelent **generic; | |
1525 | unsigned char *native, *natptr; | |
1526 | size_t each_size; | |
1527 | ||
1528 | unsigned int count = section->reloc_count; | |
1529 | size_t natsize; | |
1530 | ||
1531 | if (count == 0) return true; | |
1532 | ||
1533 | each_size = obj_reloc_entry_size (abfd); | |
1534 | natsize = each_size * count; | |
1535 | native = (unsigned char *) bfd_zalloc (abfd, natsize); | |
1536 | if (!native) { | |
1537 | bfd_error = no_memory; | |
1538 | return false; | |
1539 | } | |
1540 | ||
1541 | generic = section->orelocation; | |
1542 | ||
1543 | if (each_size == RELOC_EXT_SIZE) | |
1544 | { | |
1545 | for (natptr = native; | |
1546 | count != 0; | |
1547 | --count, natptr += each_size, ++generic) | |
1548 | NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr); | |
1549 | } | |
1550 | else | |
1551 | { | |
1552 | for (natptr = native; | |
1553 | count != 0; | |
1554 | --count, natptr += each_size, ++generic) | |
1555 | NAME(aout,swap_std_reloc_out)(abfd, *generic, (struct reloc_std_external *)natptr); | |
1556 | } | |
1557 | ||
1558 | if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) { | |
1559 | bfd_release(abfd, native); | |
1560 | return false; | |
1561 | } | |
1562 | bfd_release (abfd, native); | |
1563 | ||
1564 | return true; | |
1565 | } | |
1566 | ||
1567 | /* This is stupid. This function should be a boolean predicate */ | |
1568 | unsigned int | |
1569 | DEFUN(NAME(aout,canonicalize_reloc),(abfd, section, relptr, symbols), | |
1570 | bfd *abfd AND | |
1571 | sec_ptr section AND | |
1572 | arelent **relptr AND | |
1573 | asymbol **symbols) | |
1574 | { | |
1575 | arelent *tblptr = section->relocation; | |
1576 | unsigned int count; | |
1577 | ||
1578 | if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols))) | |
1579 | return 0; | |
1580 | ||
1581 | if (section->flags & SEC_CONSTRUCTOR) { | |
1582 | arelent_chain *chain = section->constructor_chain; | |
1583 | for (count = 0; count < section->reloc_count; count ++) { | |
1584 | *relptr ++ = &chain->relent; | |
1585 | chain = chain->next; | |
1586 | } | |
1587 | } | |
1588 | else { | |
1589 | tblptr = section->relocation; | |
1590 | if (!tblptr) return 0; | |
1591 | ||
1592 | for (count = 0; count++ < section->reloc_count;) | |
1593 | { | |
1594 | *relptr++ = tblptr++; | |
1595 | } | |
1596 | } | |
1597 | *relptr = 0; | |
1598 | ||
1599 | return section->reloc_count; | |
1600 | } | |
1601 | ||
1602 | unsigned int | |
1603 | DEFUN(NAME(aout,get_reloc_upper_bound),(abfd, asect), | |
1604 | bfd *abfd AND | |
1605 | sec_ptr asect) | |
1606 | { | |
1607 | if (bfd_get_format (abfd) != bfd_object) { | |
1608 | bfd_error = invalid_operation; | |
1609 | return 0; | |
1610 | } | |
1611 | if (asect->flags & SEC_CONSTRUCTOR) { | |
1612 | return (sizeof (arelent *) * (asect->reloc_count+1)); | |
1613 | } | |
1614 | ||
1615 | ||
1616 | if (asect == obj_datasec (abfd)) | |
1617 | return (sizeof (arelent *) * | |
1618 | ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd)) | |
1619 | +1)); | |
1620 | ||
1621 | if (asect == obj_textsec (abfd)) | |
1622 | return (sizeof (arelent *) * | |
1623 | ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd)) | |
1624 | +1)); | |
1625 | ||
1626 | bfd_error = invalid_operation; | |
1627 | return 0; | |
1628 | } | |
1629 | ||
1630 | \f | |
1631 | unsigned int | |
1632 | DEFUN(NAME(aout,get_symtab_upper_bound),(abfd), | |
1633 | bfd *abfd) | |
1634 | { | |
1635 | if (!NAME(aout,slurp_symbol_table)(abfd)) return 0; | |
1636 | ||
1637 | return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *)); | |
1638 | } | |
1639 | alent * | |
1640 | DEFUN(NAME(aout,get_lineno),(ignore_abfd, ignore_symbol), | |
1641 | bfd *ignore_abfd AND | |
1642 | asymbol *ignore_symbol) | |
1643 | { | |
1644 | return (alent *)NULL; | |
1645 | } | |
1646 | ||
1647 | ||
1648 | void | |
1649 | DEFUN(NAME(aout,print_symbol),(ignore_abfd, afile, symbol, how), | |
1650 | bfd *ignore_abfd AND | |
1651 | PTR afile AND | |
1652 | asymbol *symbol AND | |
9e2dad8e | 1653 | bfd_print_symbol_type how) |
7ed4093a SC |
1654 | { |
1655 | FILE *file = (FILE *)afile; | |
1656 | ||
1657 | switch (how) { | |
9e2dad8e | 1658 | case bfd_print_symbol_name: |
fb3be09b JG |
1659 | if (symbol->name) |
1660 | fprintf(file,"%s", symbol->name); | |
7ed4093a | 1661 | break; |
9e2dad8e | 1662 | case bfd_print_symbol_more: |
7ed4093a SC |
1663 | fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff), |
1664 | (unsigned)(aout_symbol(symbol)->other & 0xff), | |
1665 | (unsigned)(aout_symbol(symbol)->type)); | |
1666 | break; | |
9e2dad8e | 1667 | case bfd_print_symbol_all: |
7ed4093a | 1668 | { |
6db82ea7 SC |
1669 | CONST char *section_name = symbol->section->name; |
1670 | ||
7ed4093a SC |
1671 | |
1672 | bfd_print_symbol_vandf((PTR)file,symbol); | |
1673 | ||
fb3be09b | 1674 | fprintf(file," %-5s %04x %02x %02x", |
7ed4093a SC |
1675 | section_name, |
1676 | (unsigned)(aout_symbol(symbol)->desc & 0xffff), | |
1677 | (unsigned)(aout_symbol(symbol)->other & 0xff), | |
9e2dad8e | 1678 | (unsigned)(aout_symbol(symbol)->type & 0xff)); |
fb3be09b JG |
1679 | if (symbol->name) |
1680 | fprintf(file," %s", symbol->name); | |
7ed4093a SC |
1681 | } |
1682 | break; | |
98d43107 JG |
1683 | case bfd_print_symbol_nm: |
1684 | { | |
1685 | int section_code = bfd_decode_symclass (symbol); | |
1686 | ||
1687 | if (section_code == 'U') | |
1688 | fprintf(file, " "); | |
55c0061e FF |
1689 | else |
1690 | fprintf_vma(file, symbol->value+symbol->section->vma); | |
98d43107 JG |
1691 | if (section_code == '?') |
1692 | { | |
1693 | int type_code = aout_symbol(symbol)->type & 0xff; | |
7de245d3 | 1694 | char *stab_name = aout_stab_name(type_code); |
98d43107 JG |
1695 | char buf[10]; |
1696 | if (stab_name == NULL) | |
1697 | { | |
1698 | sprintf(buf, "(%d)", type_code); | |
1699 | stab_name = buf; | |
1700 | } | |
1701 | fprintf(file," - %02x %04x %5s", | |
1702 | (unsigned)(aout_symbol(symbol)->other & 0xff), | |
1703 | (unsigned)(aout_symbol(symbol)->desc & 0xffff), | |
1704 | stab_name); | |
1705 | } | |
1706 | else | |
1707 | fprintf(file," %c", section_code); | |
1708 | if (symbol->name) | |
1709 | fprintf(file," %s", symbol->name); | |
1710 | } | |
1711 | break; | |
7ed4093a SC |
1712 | } |
1713 | } | |
1714 | ||
1715 | /* | |
6724ff46 | 1716 | provided a BFD, a section and an offset into the section, calculate |
7ed4093a SC |
1717 | and return the name of the source file and the line nearest to the |
1718 | wanted location. | |
1719 | */ | |
1720 | ||
1721 | boolean | |
1722 | DEFUN(NAME(aout,find_nearest_line),(abfd, | |
1723 | section, | |
1724 | symbols, | |
1725 | offset, | |
1726 | filename_ptr, | |
1727 | functionname_ptr, | |
1728 | line_ptr), | |
1729 | bfd *abfd AND | |
1730 | asection *section AND | |
1731 | asymbol **symbols AND | |
1732 | bfd_vma offset AND | |
1733 | CONST char **filename_ptr AND | |
1734 | CONST char **functionname_ptr AND | |
1735 | unsigned int *line_ptr) | |
1736 | { | |
1737 | /* Run down the file looking for the filename, function and linenumber */ | |
1738 | asymbol **p; | |
1739 | static char buffer[100]; | |
98d43107 | 1740 | static char filename_buffer[200]; |
6db82ea7 SC |
1741 | CONST char *directory_name = NULL; |
1742 | CONST char *main_file_name = NULL; | |
1743 | CONST char *current_file_name = NULL; | |
1744 | CONST char *line_file_name = NULL; /* Value of current_file_name at line number. */ | |
7ed4093a SC |
1745 | bfd_vma high_line_vma = ~0; |
1746 | bfd_vma low_func_vma = 0; | |
1747 | asymbol *func = 0; | |
1748 | *filename_ptr = abfd->filename; | |
1749 | *functionname_ptr = 0; | |
1750 | *line_ptr = 0; | |
1751 | if (symbols != (asymbol **)NULL) { | |
1752 | for (p = symbols; *p; p++) { | |
1753 | aout_symbol_type *q = (aout_symbol_type *)(*p); | |
98d43107 | 1754 | next: |
7ed4093a SC |
1755 | switch (q->type){ |
1756 | case N_SO: | |
3f7607af | 1757 | main_file_name = current_file_name = q->symbol.name; |
98d43107 JG |
1758 | /* Look ahead to next symbol to check if that too is an N_SO. */ |
1759 | p++; | |
1760 | if (*p == NULL) | |
1761 | break; | |
1762 | q = (aout_symbol_type *)(*p); | |
6db82ea7 | 1763 | if (q->type != (int)N_SO) |
98d43107 JG |
1764 | goto next; |
1765 | ||
1766 | /* Found a second N_SO First is directory; second is filename. */ | |
3f7607af PB |
1767 | directory_name = current_file_name; |
1768 | main_file_name = current_file_name = q->symbol.name; | |
1769 | if (obj_textsec(abfd) != section) | |
1770 | goto done; | |
1771 | break; | |
1772 | case N_SOL: | |
1773 | current_file_name = q->symbol.name; | |
7ed4093a | 1774 | break; |
3f7607af | 1775 | |
7ed4093a SC |
1776 | case N_SLINE: |
1777 | ||
1778 | case N_DSLINE: | |
1779 | case N_BSLINE: | |
1780 | /* We'll keep this if it resolves nearer than the one we have already */ | |
1781 | if (q->symbol.value >= offset && | |
1782 | q->symbol.value < high_line_vma) { | |
1783 | *line_ptr = q->desc; | |
1784 | high_line_vma = q->symbol.value; | |
3f7607af | 1785 | line_file_name = current_file_name; |
7ed4093a SC |
1786 | } |
1787 | break; | |
1788 | case N_FUN: | |
1789 | { | |
1790 | /* We'll keep this if it is nearer than the one we have already */ | |
1791 | if (q->symbol.value >= low_func_vma && | |
1792 | q->symbol.value <= offset) { | |
1793 | low_func_vma = q->symbol.value; | |
1794 | func = (asymbol *)q; | |
1795 | } | |
1796 | if (*line_ptr && func) { | |
1797 | CONST char *function = func->name; | |
1798 | char *p; | |
1799 | strncpy(buffer, function, sizeof(buffer)-1); | |
1800 | buffer[sizeof(buffer)-1] = 0; | |
1801 | /* Have to remove : stuff */ | |
1802 | p = strchr(buffer,':'); | |
7b02b4ed | 1803 | if (p != NULL) { *p = '\0'; } |
7ed4093a | 1804 | *functionname_ptr = buffer; |
3f7607af | 1805 | goto done; |
7ed4093a SC |
1806 | |
1807 | } | |
1808 | } | |
1809 | break; | |
1810 | } | |
1811 | } | |
1812 | } | |
3f7607af PB |
1813 | |
1814 | done: | |
1815 | if (*line_ptr) | |
1816 | main_file_name = line_file_name; | |
1817 | if (main_file_name) { | |
1818 | if (main_file_name[0] == '/' || directory_name == NULL) | |
1819 | *filename_ptr = main_file_name; | |
1820 | else { | |
1821 | sprintf(filename_buffer, "%.140s%.50s", | |
1822 | directory_name, main_file_name); | |
1823 | *filename_ptr = filename_buffer; | |
1824 | } | |
1825 | } | |
7ed4093a SC |
1826 | return true; |
1827 | ||
1828 | } | |
1829 | ||
1830 | int | |
cbdc7909 JG |
1831 | DEFUN(NAME(aout,sizeof_headers),(abfd, execable), |
1832 | bfd *abfd AND | |
9e2dad8e | 1833 | boolean execable) |
7ed4093a | 1834 | { |
6db82ea7 | 1835 | return adata(abfd).exec_bytes_size; |
7ed4093a | 1836 | } |