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