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